MyGUI  3.0.1
MyGUI_WidgetManager.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_Gui.h"
25 #include "MyGUI_WidgetManager.h"
26 #include "MyGUI_LayerManager.h"
27 #include "MyGUI_Widget.h"
28 #include "MyGUI_IWidgetCreator.h"
29 #include "MyGUI_IWidgetFactory.h"
30 #include "MyGUI_FactoryManager.h"
31 
32 #include "MyGUI_Button.h"
33 #include "MyGUI_Canvas.h"
34 #include "MyGUI_ComboBox.h"
35 #include "MyGUI_DDContainer.h"
36 #include "MyGUI_Edit.h"
37 #include "MyGUI_HScroll.h"
38 #include "MyGUI_ItemBox.h"
39 #include "MyGUI_List.h"
40 #include "MyGUI_ListBox.h"
41 #include "MyGUI_ListCtrl.h"
42 #include "MyGUI_MenuBar.h"
43 #include "MyGUI_MenuCtrl.h"
44 #include "MyGUI_MenuItem.h"
45 #include "MyGUI_Message.h"
46 #include "MyGUI_MultiList.h"
47 #include "MyGUI_PopupMenu.h"
48 #include "MyGUI_Progress.h"
49 #include "MyGUI_ScrollView.h"
50 #include "MyGUI_StaticImage.h"
51 #include "MyGUI_StaticText.h"
52 #include "MyGUI_Tab.h"
53 #include "MyGUI_TabItem.h"
54 #include "MyGUI_VScroll.h"
55 #include "MyGUI_Widget.h"
56 #include "MyGUI_Window.h"
57 
58 namespace MyGUI
59 {
60 
61  MYGUI_INSTANCE_IMPLEMENT( WidgetManager )
62 
63  void WidgetManager::initialise()
64  {
65  MYGUI_ASSERT(!mIsInitialise, INSTANCE_TYPE_NAME << " initialised twice");
66  MYGUI_LOG(Info, "* Initialise: " << INSTANCE_TYPE_NAME);
67 
68  //registerUnlinker(this);
69 
71 
72  factory.registerFactory<Button>("Widget");
73  factory.registerFactory<Canvas>("Widget");
74  factory.registerFactory<ComboBox>("Widget");
75  factory.registerFactory<DDContainer>("Widget");
76  factory.registerFactory<Edit>("Widget");
77  factory.registerFactory<HScroll>("Widget");
78  factory.registerFactory<ItemBox>("Widget");
79  factory.registerFactory<List>("Widget");
80  factory.registerFactory<ListBox>("Widget");
81  factory.registerFactory<ListCtrl>("Widget");
82  factory.registerFactory<MenuBar>("Widget");
83  factory.registerFactory<MenuCtrl>("Widget");
84  factory.registerFactory<MenuItem>("Widget");
85  factory.registerFactory<Message>("Widget");
86  factory.registerFactory<MultiList>("Widget");
87  factory.registerFactory<PopupMenu>("Widget");
88  factory.registerFactory<Progress>("Widget");
89  factory.registerFactory<ScrollView>("Widget");
90  factory.registerFactory<StaticImage>("Widget");
91  factory.registerFactory<StaticText>("Widget");
92  factory.registerFactory<Tab>("Widget");
93  factory.registerFactory<TabItem>("Widget");
94  factory.registerFactory<VScroll>("Widget");
95  factory.registerFactory<Widget>("Widget");
96  factory.registerFactory<Window>("Widget");
97 
98 #ifndef MYGUI_DONT_USE_OBSOLETE
99 
100  factory.registerFactory<RenderBox>("Widget");
101  factory.registerFactory<Sheet>("Widget");
102 
103 #endif // MYGUI_DONT_USE_OBSOLETE
104 
105  MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully initialized");
106  mIsInitialise = true;
107  }
108 
110  {
111  if (!mIsInitialise) return;
112  MYGUI_LOG(Info, "* Shutdown: " << INSTANCE_TYPE_NAME);
113 
114  //unregisterUnlinker(this);
115 
116  mFactoryList.clear();
117  mDelegates.clear();
118  mVectorIUnlinkWidget.clear();
119 
121 
122  MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully shutdown");
123  mIsInitialise = false;
124  }
125 
126  Widget* WidgetManager::createWidget(WidgetStyle _style, const std::string& _type, const std::string& _skin, const IntCoord& _coord, Align _align, Widget* _parent, ICroppedRectangle * _cropeedParent, IWidgetCreator * _creator, const std::string& _name)
127  {
128  IObject* object = FactoryManager::getInstance().createObject("Widget", _type);
129  if (object != nullptr)
130  {
131  Widget* widget = object->castType<Widget>();
132  ResourceSkin* skin = SkinManager::getInstance().getByName(_skin);
133  widget->_initialise(_style, _coord, _align, skin, _parent, _cropeedParent, _creator, _name);
134 
135  return widget;
136  }
137 
138  // старый вариант создания
139  for (SetWidgetFactory::iterator factory = mFactoryList.begin(); factory != mFactoryList.end(); ++factory)
140  {
141  if ((*factory)->getTypeName() == _type)
142  {
143  Widget* widget = (*factory)->createWidget(_style, _skin, _coord, _align, _parent, _cropeedParent, _creator, _name);
144  return widget;
145  }
146  }
147 
148  MYGUI_EXCEPT("factory '" << _type << "' not found");
149  return nullptr;
150  }
151 
153  {
154  // иначе возможен бесконечный цикл
155  MYGUI_ASSERT(_widget != nullptr, "widget is deleted");
156 
157  // делегирует удаление отцу виджета
158  IWidgetCreator * creator = _widget->_getIWidgetCreator();
159  creator->_destroyChildWidget(_widget);
160  }
161 
163  {
164  for (VectorWidgetPtr::const_iterator iter = _widgets.begin(); iter != _widgets.end(); ++iter)
165  {
166  destroyWidget(*iter);
167  }
168  }
169 
171  {
172  VectorWidgetPtr widgets;
173  while (_widgets.next())
174  {
175  widgets.push_back(_widgets.current());
176  }
177  destroyWidgets(widgets);
178  }
179 
181  {
182  unregisterUnlinker(_unlink);
183  mVectorIUnlinkWidget.push_back(_unlink);
184  }
185 
187  {
188  for (size_t pos=0; pos<mVectorIUnlinkWidget.size(); pos++)
189  {
190  if (mVectorIUnlinkWidget[pos] == _unlink)
191  {
193  mVectorIUnlinkWidget.pop_back();
194  break;
195  }
196  }
197  }
198 
200  {
201  for (VectorIUnlinkWidget::iterator iter = mVectorIUnlinkWidget.begin(); iter!=mVectorIUnlinkWidget.end(); ++iter)
202  {
203  (*iter)->_unlinkWidget(_widget);
204  }
205  // вызывать последним, обнулится
206  removeWidgetFromUnlink(_widget);
207  }
208 
210  {
211  if (_widget) mUnlinkWidgets.push_back(_widget);
212  }
213 
215  {
216  VectorWidgetPtr::iterator iter = std::find(mUnlinkWidgets.begin(), mUnlinkWidgets.end(), _widget);
217  if (iter != mUnlinkWidgets.end())
218  {
219  (*iter) = mUnlinkWidgets.back();
220  mUnlinkWidgets.pop_back();
221  }
222  else
223  {
224  _widget = nullptr;
225  }
226  }
227 
228  bool WidgetManager::isFactoryExist(const std::string& _type)
229  {
230  if (FactoryManager::getInstance().isFactoryExist("Widget", _type))
231  {
232  return true;
233  }
234 
235  // старый вариант
236  for (SetWidgetFactory::iterator factory = mFactoryList.begin(); factory != mFactoryList.end(); ++factory)
237  {
238  if ((*factory)->getTypeName() == _type)
239  {
240  return true;
241  }
242  }
243 
244  return false;
245  }
246 
247 #ifndef MYGUI_DONT_USE_OBSOLETE
248  Widget* WidgetManager::findWidgetT(const std::string& _name, bool _throw)
249  {
250  return Gui::getInstance().findWidgetT(_name, _throw);
251  }
252 
253  Widget* WidgetManager::findWidgetT(const std::string& _name, const std::string& _prefix, bool _throw)
254  {
255  return Gui::getInstance().findWidgetT(_name, _prefix, _throw);
256  }
257 
259  {
260  mFactoryList.insert(_factory);
261  MYGUI_LOG(Info, "* Register widget factory '" << _factory->getTypeName() << "'");
262  }
263 
265  {
266  SetWidgetFactory::iterator iter = mFactoryList.find(_factory);
267  if (iter != mFactoryList.end()) mFactoryList.erase(iter);
268  MYGUI_LOG(Info, "* Unregister widget factory '" << _factory->getTypeName() << "'");
269  }
270 
271  void WidgetManager::parse(Widget* _widget, const std::string &_key, const std::string &_value)
272  {
273  MapDelegate::iterator iter = mDelegates.find(_key);
274  if (iter == mDelegates.end())
275  {
276  //MYGUI_LOG(Error, "Unknown key '" << _key << "' with value '" << _value << "'");
277  _widget->setProperty(_key, _value);
278  return;
279  }
280  iter->second(_widget, _key, _value);
281  }
282 
284  {
285  MapDelegate::iterator iter = mDelegates.find(_key);
286  MYGUI_ASSERT(iter == mDelegates.end(), "delegate with name '" << _key << "' already exist");
287  return (mDelegates[_key] = ParseDelegate());
288  }
289 
290  void WidgetManager::unregisterDelegate(const std::string& _key)
291  {
292  MapDelegate::iterator iter = mDelegates.find(_key);
293  if (iter != mDelegates.end()) mDelegates.erase(iter);
294  }
295 #endif // MYGUI_DONT_USE_OBSOLETE
296 } // namespace MyGUI