1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 // 07.10.2005 - Multicolor-Listbox addet by A. Buschhueter (Acki)
6 //                                          A_Buschhueter@gmx.de
7 
8 #ifndef __C_GUI_TABLE_BAR_H_INCLUDED__
9 #define __C_GUI_TABLE_BAR_H_INCLUDED__
10 
11 #include "IrrCompileConfig.h"
12 #ifdef _IRR_COMPILE_WITH_GUI_
13 
14 #include "IGUITable.h"
15 #include "irrArray.h"
16 
17 namespace irr
18 {
19 namespace gui
20 {
21 
22 	class IGUIFont;
23 	class IGUIScrollBar;
24 
25 	class CGUITable : public IGUITable
26 	{
27 	public:
28 		//! constructor
29 		CGUITable(IGUIEnvironment* environment, IGUIElement* parent,
30 			s32 id, const core::rect<s32>& rectangle, bool clip=true,
31 			bool drawBack=false, bool moveOverSelect=true);
32 
33 		//! destructor
34 		~CGUITable();
35 
36 		//! Adds a column
37 		//! If columnIndex is outside the current range, do push new colum at the end
38 		virtual void addColumn(const wchar_t* caption, s32 columnIndex=-1);
39 
40 		//! remove a column from the table
41 		virtual void removeColumn(u32 columnIndex);
42 
43 		//! Returns the number of columns in the table control
44 		virtual s32 getColumnCount() const;
45 
46 		//! Makes a column active. This will trigger an ordering process.
47 		/** \param idx: The id of the column to make active.
48 		\return True if successful. */
49 		virtual bool setActiveColumn(s32 columnIndex, bool doOrder=false);
50 
51 		//! Returns which header is currently active
52 		virtual s32 getActiveColumn() const;
53 
54 		//! Returns the ordering used by the currently active column
55 		virtual EGUI_ORDERING_MODE getActiveColumnOrdering() const;
56 
57 		//! set a column width
58 		virtual void setColumnWidth(u32 columnIndex, u32 width);
59 
60 		//! Get the width of a column
61 		virtual u32 getColumnWidth(u32 columnIndex) const;
62 
63 		//! columns can be resized by drag 'n drop
64 		virtual void setResizableColumns(bool resizable);
65 
66 		//! can columns be resized by dran 'n drop?
67 		virtual bool hasResizableColumns() const;
68 
69 		//! This tells the table control which ordering mode should be used when
70 		//! a column header is clicked.
71 		/** \param columnIndex: The index of the column header.
72 		\param state: If true, a EGET_TABLE_HEADER_CHANGED message will be sent and you can order the table data as you whish.*/
73 		//! \param mode: One of the modes defined in EGUI_COLUMN_ORDERING
74 		virtual void setColumnOrdering(u32 columnIndex, EGUI_COLUMN_ORDERING mode);
75 
76 		//! Returns which row is currently selected
77 		virtual s32 getSelected() const;
78 
79 		//! set wich row is currently selected
80 		virtual void setSelected( s32 index );
81 
82 		//! Returns amount of rows in the tabcontrol
83 		virtual s32 getRowCount() const;
84 
85 		//! adds a row to the table
86 		/** \param rowIndex: zero based index of rows. The row will be
87 			inserted at this position. If a row already exists
88 			there, it will be placed after it. If the row is larger
89 			than the actual number of rows by more than one, it
90 			won't be created. Note that if you create a row that is
91 			not at the end, there might be performance issues*/
92 		virtual u32 addRow(u32 rowIndex);
93 
94 		//! Remove a row from the table
95 		virtual void removeRow(u32 rowIndex);
96 
97 		//! clear the table rows, but keep the columns intact
98 		virtual void clearRows();
99 
100 		//! Swap two row positions. This is useful for a custom ordering algo.
101 		virtual void swapRows(u32 rowIndexA, u32 rowIndexB);
102 
103 		//! This tells the table to start ordering all the rows. You
104 		//! need to explicitly tell the table to reorder the rows when
105 		//! a new row is added or the cells data is changed. This makes
106 		//! the system more flexible and doesn't make you pay the cost
107 		//! of ordering when adding a lot of rows.
108 		//! \param columnIndex: When set to -1 the active column is used.
109 		virtual void orderRows(s32 columnIndex=-1, EGUI_ORDERING_MODE mode=EGOM_NONE);
110 
111 
112 		//! Set the text of a cell
113 		virtual void setCellText(u32 rowIndex, u32 columnIndex, const core::stringw& text);
114 
115 		//! Set the text of a cell, and set a color of this cell.
116 		virtual void setCellText(u32 rowIndex, u32 columnIndex, const core::stringw& text, video::SColor color);
117 
118 		//! Set the data of a cell
119 		//! data will not be serialized.
120 		virtual void setCellData(u32 rowIndex, u32 columnIndex, void *data);
121 
122 		//! Set the color of a cell text
123 		virtual void setCellColor(u32 rowIndex, u32 columnIndex, video::SColor color);
124 
125 		//! Get the text of a cell
126 		virtual const wchar_t* getCellText(u32 rowIndex, u32 columnIndex ) const;
127 
128 		//! Get the data of a cell
129 		virtual void* getCellData(u32 rowIndex, u32 columnIndex ) const;
130 
131 		//! clears the table, deletes all items in the table
132 		virtual void clear();
133 
134 		//! called if an event happened.
135 		virtual bool OnEvent(const SEvent &event);
136 
137 		//! draws the element and its children
138 		virtual void draw();
139 
140 		//! Set flags, as defined in EGUI_TABLE_DRAW_FLAGS, which influence the layout
141 		virtual void setDrawFlags(s32 flags);
142 
143 		//! Get the flags, as defined in EGUI_TABLE_DRAW_FLAGS, which influence the layout
144 		virtual s32 getDrawFlags() const;
145 
146 		//! Writes attributes of the object.
147 		//! Implement this to expose the attributes of your scene node animator for
148 		//! scripting languages, editors, debuggers or xml serialization purposes.
149 		virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const;
150 
151 		//! Reads attributes of the object.
152 		//! Implement this to set the attributes of your scene node animator for
153 		//! scripting languages, editors, debuggers or xml deserialization purposes.
154 		virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0);
155 
156 	protected:
157 		virtual void refreshControls();
158 		virtual void checkScrollbars();
159 
160 	private:
161 
162 		struct Cell
163 		{
CellCell164 			Cell() : IsOverrideColor(false), Data(0)  {}
165 			core::stringw Text;
166 			core::stringw BrokenText;
167 			bool IsOverrideColor;
168 			video::SColor Color;
169 			void *Data;
170 		};
171 
172 		struct Row
173 		{
RowRow174 			Row() {}
175 			core::array<Cell> Items;
176 		};
177 
178 		struct Column
179 		{
ColumnColumn180 			Column() : Width(0), OrderingMode(EGCO_NONE) {}
181 			core::stringw Name;
182 			u32 Width;
183 			EGUI_COLUMN_ORDERING OrderingMode;
184 		};
185 
186 		void breakText(const core::stringw &text, core::stringw & brokenText, u32 cellWidth);
187 		void selectNew(s32 ypos, bool onlyHover=false);
188 		bool selectColumnHeader(s32 xpos, s32 ypos);
189 		bool dragColumnStart(s32 xpos, s32 ypos);
190 		bool dragColumnUpdate(s32 xpos);
191 		void recalculateHeights();
192 		void recalculateWidths();
193 
194 		core::array< Column > Columns;
195 		core::array< Row > Rows;
196 		gui::IGUIFont* Font;
197 		gui::IGUIScrollBar* VerticalScrollBar;
198 		gui::IGUIScrollBar* HorizontalScrollBar;
199 		bool Clip;
200 		bool DrawBack;
201 		bool MoveOverSelect;
202 		bool Selecting;
203 		s32  CurrentResizedColumn;
204 		s32  ResizeStart;
205 		bool ResizableColumns;
206 
207 		s32 ItemHeight;
208 		s32 TotalItemHeight;
209 		s32 TotalItemWidth;
210 		s32 Selected;
211 		s32 CellHeightPadding;
212 		s32 CellWidthPadding;
213 		s32 ActiveTab;
214 		EGUI_ORDERING_MODE CurrentOrdering;
215 		s32 DrawFlags;
216 	};
217 
218 } // end namespace gui
219 } // end namespace irr
220 
221 #endif // _IRR_COMPILE_WITH_GUI_
222 
223 #endif
224 
225