UIMultiItem.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "UIMultiItem.h"
  23. using namespace tb;
  24. namespace Atomic
  25. {
  26. UIMultiItem::UIMultiItem(Context* context, const String &colid, const String &widgettype, const String &str, int colwidth, int colheight )
  27. : UISelectItem(context, str, colid)
  28. {
  29. SetID(colid);
  30. AddColumn ( widgettype, str, colwidth );
  31. colHeight_ = colheight;
  32. }
  33. UIMultiItem::~UIMultiItem()
  34. {
  35. }
  36. void UIMultiItem::SetID(const String& id)
  37. {
  38. id_ = TBID(id.CString());
  39. }
  40. void UIMultiItem::AddColumn ( const String& widgettype, const String& str, int cwidth )
  41. {
  42. colStr_.Push(str);
  43. colWidget_.Push(widgettype);
  44. colWidth_.Push(cwidth);
  45. }
  46. const String& UIMultiItem::GetColumnStr( int col )
  47. {
  48. if ( col > -1 )
  49. return colStr_[col];
  50. return ( String::EMPTY );
  51. }
  52. const String& UIMultiItem::GetColumnWidget( int col )
  53. {
  54. if ( col > -1 )
  55. return colWidget_[col];
  56. return ( String::EMPTY );
  57. }
  58. int UIMultiItem::GetColumnWidth( int col )
  59. {
  60. if ( col > -1 )
  61. return colWidth_[col];
  62. return 0;
  63. }
  64. int UIMultiItem::GetNumColumns()
  65. {
  66. return colStr_.Size();
  67. }
  68. tb::MultiItem* UIMultiItem::GetTBItem()
  69. {
  70. tb::MultiItem* item = NULL;
  71. int col = 0;
  72. int numcols = GetNumColumns();
  73. String strx = GetColumnStr(col);
  74. String widx = GetColumnWidget(col);
  75. int wix = GetColumnWidth(col);
  76. item = new tb::MultiItem(id_, widx.CString(), strx.CString(), wix, colHeight_ );
  77. for ( col = 1; col<numcols; col++)
  78. {
  79. strx = GetColumnStr(col);
  80. widx = GetColumnWidget(col);
  81. wix = GetColumnWidth(col);
  82. item->AddColumn( widx.CString(), strx.CString(), wix );
  83. }
  84. return item;
  85. }
  86. // UIMultiItemSource
  87. UIMultiItemSource::UIMultiItemSource(Context* context) : UISelectItemSource(context)
  88. {
  89. }
  90. UIMultiItemSource::~UIMultiItemSource()
  91. {
  92. }
  93. void UIMultiItemSource::RemoveItemWithId(const String& id)
  94. {
  95. tb::TBID test = TBID(id.CString());
  96. for (List<SharedPtr<UIMultiItem> >::Iterator itr = items_.Begin(); itr != items_.End(); itr++)
  97. {
  98. if ((*itr)->GetID() == test) {
  99. items_.Erase(itr);
  100. break;
  101. }
  102. }
  103. }
  104. void UIMultiItemSource::RemoveItemWithStr(const String& str)
  105. {
  106. for (List<SharedPtr<UIMultiItem> >::Iterator itr = items_.Begin(); itr != items_.End(); itr++)
  107. {
  108. if ((*itr)->GetColumnStr(0) == str) {
  109. items_.Erase(itr);
  110. break;
  111. }
  112. }
  113. }
  114. const String& UIMultiItemSource::GetItemStr(int index)
  115. {
  116. int nn = 0;
  117. for (List<SharedPtr<UIMultiItem> >::Iterator itr = items_.Begin(); itr != items_.End(); itr++)
  118. {
  119. if ( nn == index) return (*itr)->GetColumnStr(0);
  120. nn++;
  121. }
  122. return ( String::EMPTY );
  123. }
  124. MultiItemSource *UIMultiItemSource::GetTBItemSource()
  125. {
  126. // caller's responsibility to clean up
  127. MultiItemSource* src = new MultiItemSource();
  128. for (List<SharedPtr<UIMultiItem> >::Iterator itr = items_.Begin(); itr != items_.End(); itr++)
  129. {
  130. tb::MultiItem* tbitem = (*itr)->GetTBItem();
  131. src->AddItem(tbitem);
  132. }
  133. return src;
  134. }
  135. }