1 /*!
2 	@file
3 	@author		Albert Semenov
4 	@date		07/2008
5 	@module
6 */
7 
8 #ifndef BASE_ITEM_BOX_H_
9 #define BASE_ITEM_BOX_H_
10 
11 #include <MyGUI.h>
12 #include "BaseLayout/BaseLayout.h"
13 #include "ItemDropInfo.h"
14 
15 namespace wraps
16 {
17 	template<typename CellType>
18 	class BaseItemBox :
19 		public BaseLayout
20 	{
21 	public:
22 		typedef typename CellType::Type DataType;
23 
24 	public:
BaseItemBox(MyGUI::Widget * _parent)25 		BaseItemBox(MyGUI::Widget* _parent) :
26 			BaseLayout("", _parent)
27 		{
28 			mBoxItems = mMainWidget->castType<MyGUI::ItemBox>();
29 			mBoxItems->setUserData(static_cast<BaseLayout*>(this));
30 
31 			mBoxItems->requestCreateWidgetItem = MyGUI::newDelegate(this, &BaseItemBox::requestCreateWidgetItem);
32 			mBoxItems->requestCoordItem = MyGUI::newDelegate(this, &BaseItemBox::requestCoordWidgetItem);
33 			mBoxItems->requestDrawItem = MyGUI::newDelegate(this, &BaseItemBox::requestUpdateWidgetItem);
34 
35 			mBoxItems->eventStartDrag += MyGUI::newDelegate(this, &BaseItemBox::notifyStartDrop);
36 			mBoxItems->eventRequestDrop += MyGUI::newDelegate(this, &BaseItemBox::notifyRequestDrop);
37 			mBoxItems->eventDropResult += MyGUI::newDelegate(this, &BaseItemBox::notifyEndDrop);
38 			mBoxItems->eventChangeDDState += MyGUI::newDelegate(this, &BaseItemBox::notifyDropState);
39 			mBoxItems->eventNotifyItem += MyGUI::newDelegate(this, &BaseItemBox::notifyNotifyItem);
40 
41 			mBoxItems->eventToolTip += MyGUI::newDelegate(this, &BaseItemBox::notifyToolTip);
42 		}
43 
~BaseItemBox()44 		virtual ~BaseItemBox()
45 		{
46 			mBoxItems->requestCreateWidgetItem = nullptr;
47 			mBoxItems->requestCoordItem = nullptr;
48 			mBoxItems->requestDrawItem = nullptr;
49 
50 			mBoxItems->eventStartDrag -= MyGUI::newDelegate(this, &BaseItemBox::notifyStartDrop);
51 			mBoxItems->eventRequestDrop -= MyGUI::newDelegate(this, &BaseItemBox::notifyRequestDrop);
52 			mBoxItems->eventDropResult -= MyGUI::newDelegate(this, &BaseItemBox::notifyEndDrop);
53 			mBoxItems->eventChangeDDState -= MyGUI::newDelegate(this, &BaseItemBox::notifyDropState);
54 			mBoxItems->eventNotifyItem -= MyGUI::newDelegate(this, &BaseItemBox::notifyNotifyItem);
55 
56 			mBoxItems->eventToolTip -= MyGUI::newDelegate(this, &BaseItemBox::notifyToolTip);
57 
58 			for (typename VectorCellView::iterator iter = mListCellView.begin(); iter != mListCellView.end(); ++iter)
59 			{
60 				delete *iter;
61 			}
62 			mListCellView.clear();
63 		}
64 
addItem(DataType _data)65 		void addItem(DataType _data)
66 		{
67 			mBoxItems->addItem(_data);
68 		}
69 
removeItem(size_t _index)70 		void removeItem(size_t _index)
71 		{
72 			mBoxItems->removeItemAt(_index);
73 		}
74 
removeAllItems()75 		void removeAllItems()
76 		{
77 			mBoxItems->removeAllItems();
78 		}
79 
setItemData(size_t _index,DataType _data)80 		void setItemData(size_t _index, DataType _data)
81 		{
82 			mBoxItems->setItemDataAt(_index, _data);
83 		}
84 
85 		template <typename ValueType>
86 		ValueType* getItemDataAt(size_t _index, bool _throw = true)
87 		{
88 			return mBoxItems->getItemDataAt<ValueType>(_index, _throw);
89 		}
90 
91 	private:
requestCreateWidgetItem(MyGUI::ItemBox * _sender,MyGUI::Widget * _item)92 		void requestCreateWidgetItem(MyGUI::ItemBox* _sender, MyGUI::Widget* _item)
93 		{
94 			CellType* cell = new CellType(_item);
95 			_item->setUserData(cell);
96 			mListCellView.push_back(cell);
97 		}
98 
requestCoordWidgetItem(MyGUI::ItemBox * _sender,MyGUI::IntCoord & _coord,bool _drop)99 		void requestCoordWidgetItem(MyGUI::ItemBox* _sender, MyGUI::IntCoord& _coord, bool _drop)
100 		{
101 			CellType::getCellDimension(_sender, _coord, _drop);
102 		}
103 
requestUpdateWidgetItem(MyGUI::ItemBox * _sender,MyGUI::Widget * _item,const MyGUI::IBDrawItemInfo & _data)104 		void requestUpdateWidgetItem(MyGUI::ItemBox* _sender, MyGUI::Widget* _item, const MyGUI::IBDrawItemInfo& _data)
105 		{
106 			CellType* cell = *_item->getUserData<CellType*>();
107 			cell->update(_data, *mBoxItems->getItemDataAt<DataType>(_data.index));
108 		}
109 
notifyStartDrop(MyGUI::DDContainer * _sender,const MyGUI::DDItemInfo & _info,bool & _result)110 		void notifyStartDrop(MyGUI::DDContainer* _sender, const MyGUI::DDItemInfo& _info, bool& _result)
111 		{
112 			eventStartDrag(this, DDItemInfo(_info), _result);
113 		}
114 
notifyRequestDrop(MyGUI::DDContainer * _sender,const MyGUI::DDItemInfo & _info,bool & _result)115 		void notifyRequestDrop(MyGUI::DDContainer* _sender, const MyGUI::DDItemInfo& _info, bool& _result)
116 		{
117 			eventRequestDrop(this, DDItemInfo(_info), _result);
118 		}
119 
notifyEndDrop(MyGUI::DDContainer * _sender,const MyGUI::DDItemInfo & _info,bool _result)120 		void notifyEndDrop(MyGUI::DDContainer* _sender, const MyGUI::DDItemInfo& _info, bool _result)
121 		{
122 			eventDropResult(this, DDItemInfo(_info), _result);
123 		}
124 
notifyDropState(MyGUI::DDContainer * _sender,MyGUI::DDItemState _state)125 		void notifyDropState(MyGUI::DDContainer* _sender, MyGUI::DDItemState _state)
126 		{
127 			eventChangeDDState(this, _state);
128 		}
129 
notifyNotifyItem(MyGUI::ItemBox * _sender,const MyGUI::IBNotifyItemData & _info)130 		void notifyNotifyItem(MyGUI::ItemBox* _sender, const MyGUI::IBNotifyItemData& _info)
131 		{
132 			eventNotifyItem(this, _info);
133 		}
134 
notifyToolTip(MyGUI::Widget * _sender,const MyGUI::ToolTipInfo & _info)135 		void notifyToolTip(MyGUI::Widget* _sender, const MyGUI::ToolTipInfo& _info)
136 		{
137 			if (_info.index == MyGUI::ITEM_NONE)
138 				eventToolTip(this, _info, DataType());
139 			else
140 				eventToolTip(this, _info, *mBoxItems->getItemDataAt<DataType>(_info.index));
141 		}
142 
143 	public:
144 		MyGUI::delegates::CDelegate3<BaseLayout*, DDItemInfo, bool&> eventStartDrag;
145 		MyGUI::delegates::CDelegate3<BaseLayout*, DDItemInfo, bool&> eventRequestDrop;
146 		MyGUI::delegates::CDelegate3<BaseLayout*, DDItemInfo, bool> eventDropResult;
147 		MyGUI::delegates::CDelegate2<BaseLayout*, MyGUI::DDItemState> eventChangeDDState;
148 		MyGUI::delegates::CDelegate2<BaseLayout*, const MyGUI::IBNotifyItemData& > eventNotifyItem;
149 
150 		MyGUI::delegates::CDelegate3<BaseLayout*, const MyGUI::ToolTipInfo&, DataType> eventToolTip;
151 
getItemBox()152 		MyGUI::ItemBox* getItemBox() const
153 		{
154 			return mBoxItems;
155 		}
156 
157 	private:
158 		typedef std::vector<CellType*> VectorCellView;
159 		VectorCellView mListCellView;
160 		MyGUI::ItemBox* mBoxItems;
161 	};
162 
163 } // namespace wraps
164 
165 #endif // BASE_ITEM_BOX_H_
166