1 /*
2  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3  * Distributed under the MIT License
4  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5  */
6 
7 #ifndef MYGUI_TAB_CONTROL_H_
8 #define MYGUI_TAB_CONTROL_H_
9 
10 #include "MyGUI_Prerequest.h"
11 #include "MyGUI_Widget.h"
12 #include "MyGUI_Any.h"
13 #include "MyGUI_EventPair.h"
14 #include "MyGUI_ControllerFadeAlpha.h"
15 #include "MyGUI_IItem.h"
16 #include "MyGUI_IItemContainer.h"
17 
18 namespace MyGUI
19 {
20 
21 	typedef delegates::CMultiDelegate2<TabControl*, size_t> EventHandle_TabPtrSizeT;
22 
23 	/** \brief @wpage{TabControl}
24 		TabControl widget description should be here.
25 	*/
26 	class MYGUI_EXPORT TabControl :
27 		public Widget,
28 		public IItemContainer,
29 		public MemberObsolete<TabControl>
30 	{
31 		// для уведобления об удалении
32 		//FIXME
33 		friend class TabItem;
34 
35 		MYGUI_RTTI_DERIVED( TabControl )
36 
37 	public:
38 		TabControl();
39 
40 		struct TabItemInfo
41 		{
TabItemInfoTabItemInfo42 			TabItemInfo(int _width, const UString& _name, TabItem* _item, Any _data) :
43 				width(_width),
44 				name(_name),
45 				item(_item),
46 				data(_data)
47 			{
48 			}
49 
50 			int width;
51 			UString name;
52 			TabItem* item;
53 			Any data;
54 		};
55 
56 		typedef std::vector<TabItemInfo> VectorTabItemInfo;
57 
58 		//! @copydoc Widget::setPosition(const IntPoint& _value)
59 		void setPosition(const IntPoint& _value) override;
60 		//! @copydoc Widget::setSize(const IntSize& _value)
61 		void setSize(const IntSize& _value) override;
62 		//! @copydoc Widget::setCoord(const IntCoord& _value)
63 		void setCoord(const IntCoord& _value) override;
64 
65 		using Widget::setPosition;
66 		using Widget::setSize;
67 		using Widget::setCoord;
68 
69 		//------------------------------------------------------------------------------//
70 		// манипуляции айтемами
71 
72 		//! Get number of items
73 		size_t getItemCount() const;
74 
75 		//! Insert an item into a array at a specified position
76 		TabItem* insertItemAt(size_t _index, const UString& _name, Any _data = Any::Null);
77 		//! Insert an item into a array
78 		TabItem* insertItem(TabItem* _to, const UString& _name, Any _data = Any::Null);
79 
80 		//! Add an item to the end of a array
81 		TabItem* addItem(const UString& _name, Any _data = Any::Null);
82 
83 		//! Remove item at a specified position
84 		void removeItemAt(size_t _index);
85 		//! Remove item
86 		void removeItem(TabItem* _item);
87 
88 		//! Remove all items
89 		void removeAllItems();
90 
91 
92 		//! Get item from specified position
93 		TabItem* getItemAt(size_t _index);
94 
95 		//! Get item index
96 		size_t getItemIndex(TabItem* _item);
97 
98 		//! Search item, returns the position of the first occurrence in array or ITEM_NONE if item not found
99 		size_t findItemIndex(TabItem* _item);
100 
101 		//! Search item, returns the position of the first occurrence in array or ITEM_NONE if item not found
102 		size_t findItemIndexWith(const UString& _name);
103 
104 		//! Search item, returns the item of the first occurrence in array or nullptr if item not found
105 		TabItem* findItemWith(const UString& _name);
106 
107 		void swapItems(size_t _index1, size_t _index2);
108 
109 		//------------------------------------------------------------------------------//
110 		// манипуляции выделениями
111 
112 		//! Get index of selected item (ITEM_NONE if none selected)
113 		size_t getIndexSelected() const;
114 
115 		//! Get selected item (nullptr if none selected)
116 		TabItem* getItemSelected();
117 
118 		//! Select specified _index
119 		void setIndexSelected(size_t _index);
120 
121 		//! Select item
122 		void setItemSelected(TabItem* _item);
123 
124 
125 		//------------------------------------------------------------------------------//
126 		// манипуляции данными
127 
128 		//! Replace an item data at a specified position
129 		void setItemDataAt(size_t _index, Any _data);
130 		//! Replace an item data
131 		void setItemData(TabItem* _item, Any _data);
132 
133 		//! Clear an item data at a specified position
134 		void clearItemDataAt(size_t _index);
135 		//! Clear an item data
136 		void clearItemData(TabItem* _item);
137 
138 		//! Get item data from specified position
139 		template <typename ValueType>
140 		ValueType* getItemDataAt(size_t _index, bool _throw = true)
141 		{
142 			MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::getItemDataAt");
143 			return mItemsInfo[_index].data.castType<ValueType>(_throw);
144 		}
145 		//! Get item data
146 		template <typename ValueType>
147 		ValueType* getItemData(TabItem* _item, bool _throw = true)
148 		{
149 			return getItemDataAt<ValueType>(getItemIndex(_item), _throw);
150 		}
151 
152 
153 		//------------------------------------------------------------------------------//
154 		// манипуляции отображением
155 
156 		//! Replace an item name at a specified position
157 		void setItemNameAt(size_t _index, const UString& _name);
158 
159 		//! Replace an item name
160 		void setItemName(TabItem* _item, const UString& _name);
161 
162 		//! Get item name from specified position
163 		const UString& getItemNameAt(size_t _index);
164 
165 		//! Get item name
166 		const UString& getItemName(TabItem* _item);
167 
168 
169 		//------------------------------------------------------------------------------//
170 		// манипуляции выдимостью
171 
172 		//! Move all elements so specified becomes visible
173 		void beginToItemAt(size_t _index);
174 
175 		//! Move all elements so specified becomes visible
176 		void beginToItem(TabItem* _item);
177 		//! Move all elements so first becomes visible
178 		void beginToItemFirst();
179 		//! Move all elements so last becomes visible
180 		void beginToItemLast();
181 		//! Move all elements so selected becomes visible
182 		void beginToItemSelected();
183 
184 
185 		//------------------------------------------------------------------------------//
186 		// остальные манипуляции
187 
188 		//! Set button width at a specified position
189 		void setButtonWidthAt(size_t _index, int _width = DEFAULT);
190 		//! Set button width
191 		void setButtonWidth(TabItem* _item, int _width = DEFAULT);
192 
193 		//! Get button width at a specified position
194 		int getButtonWidthAt(size_t _index);
195 		//! Get button width
196 		int getButtonWidth(TabItem* _item);
197 
198 		//------------------------------------------------------------------------------//
199 
200 		/** Set default button width and disable autowidth mode */
201 		void setButtonDefaultWidth(int _value);
202 		/** Get default button width */
203 		int getButtonDefaultWidth() const;
204 
205 		/** Enable or disable button auto width */
206 		void setButtonAutoWidth(bool _value);
207 		/** Get button auto width flag */
208 		bool getButtonAutoWidth() const;
209 
210 		/** Enable or disable smooth sheets showing */
211 		void setSmoothShow(bool _value);
212 		/** Get smooth sheets showing flag */
213 		bool getSmoothShow() const;
214 
215 		/*events:*/
216 		/** Event : Active TabControl sheet changed \n
217 			signature : void method(MyGUI::TabControl* _sender, size_t _index)\n
218 			@param _sender widget that called this event
219 			@param _index Index of selected sheet
220 		*/
221 		EventPair<EventHandle_WidgetSizeT, EventHandle_TabPtrSizeT> eventTabChangeSelect;
222 
223 		/*internal:*/
224 		// IItemContainer impl
225 		size_t _getItemCount() override;
226 		void _addItem(const MyGUI::UString& _name) override;
227 		void _removeItemAt(size_t _index) override;
228 		Widget* _getItemAt(size_t _index) override;
229 		void _setItemNameAt(size_t _index, const UString& _name) override;
230 		const UString& _getItemNameAt(size_t _index) override;
231 
232 	protected:
233 		void initialiseOverride() override;
234 		void shutdownOverride() override;
235 
236 		void updateBar();
237 
238 		void notifyPressedButtonEvent(MyGUI::Widget* _sender);
239 		void notifyPressedBarButtonEvent(MyGUI::Widget* _sender);
240 
241 		int _getTextWidth(const UString& _text);
242 
243 		void _showItem(TabItem* _sheet, bool _show, bool _smooth);
244 
245 		void _createItemButton();
246 
247 		void _insertItem(size_t _index, const UString& _name, TabItem* _sheet, Any _data);
248 
249 		// вкладка при удалении уведомляет таб
250 		void _notifyDeleteItem(TabItem* _item);
251 
252 		void onWidgetCreated(Widget* _widget) override;
253 
254 		void setPropertyOverride(const std::string& _key, const std::string& _value) override;
255 
256 	private:
257 		void actionWidgetHide(Widget* _widget, ControllerItem* _controller);
258 
259 		ControllerFadeAlpha* createControllerFadeAlpha(float _alpha, float _coef, bool _enable);
260 
261 		Widget* _getWidgetTemplate();
262 		Widget* _getWidgetBar();
263 
264 		Button* createButton();
265 		void updateBarOld();
266 		void updateBarNew();
267 
268 	private:
269 		int mOffsetTab; // смещение бара при показе кнопок
270 		bool mButtonShow;
271 		int mWidthBar; // ширина в которую помещаються все кнопки
272 		std::vector<Button*> mItemButton; // список кнопок, не должно равно списку страниц
273 		std::string mButtonSkinName;
274 		std::string mEmptySkinName;
275 
276 		Widget* mWidgetBar;
277 		Button* mButtonLeft;
278 		Button* mButtonRight;
279 		Widget* mButtonDecor;
280 		VectorWidgetPtr mWidgetsPatch; // список виджетов которые нужно показать при показе кнопки
281 		Widget* mEmptyBarWidget;
282 		Widget* mItemTemplate;
283 
284 		// информация о вкладках
285 		VectorTabItemInfo mItemsInfo;
286 		size_t mStartIndex;
287 		size_t mIndexSelect;
288 
289 		int mButtonDefaultWidth;
290 		bool mSmoothShow;
291 		bool mButtonAutoWidth;
292 
293 		// флаг, чтобы отсеч уведомления от вкладок, при общем шутдауне виджета
294 		bool mShutdown;
295 
296 		Widget* mHeaderPlace;
297 		Widget* mControls;
298 		Widget* mEmpty;
299 	};
300 
301 } // namespace MyGUI
302 
303 #endif // MYGUI_TAB_CONTROL_H_
304