MyGUI  3.0.1
MyGUI_ListBox.cpp
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #include "MyGUI_Precompiled.h"
24 #include "MyGUI_ListBox.h"
25 #include "MyGUI_Button.h"
26 
27 namespace MyGUI
28 {
29 
31  mHeightLine(0)
32  {
33  requestCreateWidgetItem = MyGUI::newDelegate(this, &ListBox::notifyCreateWidgetItem);
34  requestDrawItem = MyGUI::newDelegate(this, &ListBox::notifyDrawItem);
35  }
36 
37  void ListBox::_initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, Widget* _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name)
38  {
39  Base::_initialise(_style, _coord, _align, _info, _parent, _croppedParent, _creator, _name);
40 
41  initialiseWidgetSkin(_info);
42  }
43 
45  {
46  shutdownWidgetSkin();
47  }
48 
50  {
51  shutdownWidgetSkin();
53  initialiseWidgetSkin(_info);
54  }
55 
56  void ListBox::initialiseWidgetSkin(ResourceSkin* _info)
57  {
58  mHeightLine = 20;
59  mChangeContentByResize = false;
60 
61  const MapString& properties = _info->getProperties();
62  if (!properties.empty())
63  {
64  MapString::const_iterator iter = properties.end();
65  iter = properties.find("SkinLine");
66  if (iter != properties.end()) mSkinLine = iter->second;
67  iter = properties.find("HeightLine");
68  if (iter != properties.end()) mHeightLine = utility::parseInt(iter->second);
69  }
70 
71  _setScrollViewPage(mHeightLine);
72  }
73 
74  void ListBox::shutdownWidgetSkin()
75  {
76  }
77 
78  void ListBox::notifyCreateWidgetItem(MyGUI::ListCtrl* _sender, MyGUI::Widget* _item)
79  {
80  const MyGUI::IntSize& size = _item->getSize();
81 
82  MyGUI::Button* text = _item->createWidget<MyGUI::Button>(mSkinLine, MyGUI::IntCoord(0, 0, size.width, size.height), MyGUI::Align::Stretch);
83 
84  text->setNeedMouseFocus(false);
85 
86  _item->setUserData(text);
87  }
88 
89  void ListBox::notifyDrawItem(MyGUI::ListCtrl* _sender, MyGUI::Widget* _item, const MyGUI::IBDrawItemInfo& _info, MyGUI::IntCoord& _coord)
90  {
91  MyGUI::Button* text = *_item->getUserData<MyGUI::Button*>();
92 
93  if (_info.update)
94  {
95  text->setCaption(mItemsInfo[_info.index]);
96 
97  MyGUI::IntSize size = text->getTextSize() + (text->getSize() - text->getTextRegion().size());
98  size.height = mHeightLine;
99  _coord.set(0, 0, size.width, size.height);
100  }
101 
102  text->setButtonPressed(_info.select);
103  text->_setMouseFocus(_info.active);
104  }
105 
106  void ListBox::insertItemAt(size_t _index, const UString& _name, Any _data)
107  {
108  MYGUI_ASSERT_RANGE_INSERT(_index, mItemsInfo.size(), "ListBox::insertItemAt");
109  if (_index == ITEM_NONE) _index = mItemsInfo.size();
110 
111  mItemsInfo.insert(mItemsInfo.begin() + _index, _name);
112 
113  Base::insertItemAt(_index, _data);
114  }
115 
116  void ListBox::removeItemAt(size_t _index)
117  {
118  MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "ListBox::removeItemAt");
119  mItemsInfo.erase(mItemsInfo.begin() + _index);
120 
121  Base::removeItemAt(_index);
122  }
123 
125  {
126  mItemsInfo.clear();
127 
129  }
130 
131  void ListBox::swapItemsAt(size_t _index1, size_t _index2)
132  {
133  MYGUI_ASSERT_RANGE(_index1, mItemsInfo.size(), "ListBox::swapItemsAt");
134  MYGUI_ASSERT_RANGE(_index2, mItemsInfo.size(), "ListBox::swapItemsAt");
135 
136  if (_index1 == _index2) return;
137 
138  std::swap(mItemsInfo[_index1], mItemsInfo[_index2]);
139 
140  Base::redrawItemAt(_index1);
141  Base::redrawItemAt(_index2);
142  }
143 
144  size_t ListBox::findItemIndexWith(const UString& _name)
145  {
146  for (size_t pos=0; pos<mItemsInfo.size(); pos++)
147  {
148  if (mItemsInfo[pos] == _name) return pos;
149  }
150  return ITEM_NONE;
151  }
152 
153  void ListBox::setItemNameAt(size_t _index, const UString& _name)
154  {
155  MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "ListBox::setItemNameAt");
156 
157  Base::redrawItemAt(_index);
158  }
159 
160  const UString& ListBox::getItemNameAt(size_t _index)
161  {
162  MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "ListBox::getItemNameAt");
163 
164  return mItemsInfo[_index];
165  }
166 
167  void ListBox::beginToItemAt(size_t _index)
168  {
169  MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "ListBox::beginToItemAt");
170 
171  //FIXME
172  }
173 
174 } // namespace MyGUI