xref: /haiku/headers/private/interface/ColumnListView.h (revision f5821a1aee77d3b9a979b42c68a79e50b5ebaefe)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 /*******************************************************************************
36 /
37 /	File:			ColumnListView.h
38 /
39 /   Description:    Experimental multi-column list view.
40 /
41 /	Copyright 2000+, Be Incorporated, All Rights Reserved
42 /
43 *******************************************************************************/
44 
45 
46 #ifndef _COLUMN_LIST_VIEW_H
47 #define _COLUMN_LIST_VIEW_H
48 
49 #include <BeBuild.h>
50 #include <View.h>
51 #include <List.h>
52 #include <Invoker.h>
53 #include <ListView.h>
54 
55 class BScrollBar;
56 
57 namespace BPrivate {
58 
59 class OutlineView;
60 class TitleView;
61 class BRowContainer;
62 class RecursiveOutlineIterator;
63 
64 }	// ns BPrivate
65 
66 class BField;
67 class BRow;
68 class BColumn;
69 class BColumnListView;
70 
71 enum LatchType {
72 	B_NO_LATCH					= 0,
73 	B_OPEN_LATCH				= 1,
74 	B_PRESSED_LATCH				= 2,
75 	B_CLOSED_LATCH				= 3
76 };
77 
78 typedef enum {
79 	B_ALLOW_COLUMN_NONE			= 0,
80 	B_ALLOW_COLUMN_MOVE			= 1,
81 	B_ALLOW_COLUMN_RESIZE		= 2,
82 	B_ALLOW_COLUMN_POPUP		= 4,
83 	B_ALLOW_COLUMN_REMOVE		= 8
84 } column_flags;
85 
86 enum ColumnListViewColor {
87 	B_COLOR_BACKGROUND			= 0,
88 	B_COLOR_TEXT				= 1,
89 	B_COLOR_ROW_DIVIDER			= 2,
90 	B_COLOR_SELECTION			= 3,
91 	B_COLOR_SELECTION_TEXT		= 4,
92 	B_COLOR_NON_FOCUS_SELECTION	= 5,
93 	B_COLOR_EDIT_BACKGROUND		= 6,
94 	B_COLOR_EDIT_TEXT			= 7,
95 	B_COLOR_HEADER_BACKGROUND	= 8,
96 	B_COLOR_HEADER_TEXT			= 9,
97 	B_COLOR_SEPARATOR_LINE		= 10,
98 	B_COLOR_SEPARATOR_BORDER	= 11,
99 
100 	B_COLOR_TOTAL				= 12
101 };
102 
103 enum ColumnListViewFont {
104 	B_FONT_ROW					= 0,
105 	B_FONT_HEADER				= 1,
106 
107 	B_FONT_TOTAL				= 2
108 };
109 
110 
111 // A single row/column intersection in the list.
112 class BField {
113 public:
114 								BField();
115 	virtual						~BField();
116 };
117 
118 // A single line in the list.  Each line contains a BField object
119 // for each column in the list, associated by their "logical field"
120 // index.  Hierarchies are formed by adding other BRow objects as
121 // a parent of a row, using the AddRow() function in BColumnListView().
122 class BRow {
123 public:
124 								BRow(float height = 16.0);
125 	virtual 					~BRow();
126 	virtual bool		 		HasLatch() const;
127 
128 			int32				CountFields() const;
129 			BField*				GetField(int32 logicalFieldIndex);
130 	const	BField*				GetField(int32 logicalFieldIndex) const;
131 			void				SetField(BField* field,
132 									int32 logicalFieldIndex);
133 
134 			float 				Height() const;
135 			bool 				IsExpanded() const;
136 			bool				IsSelected() const;
137 
138 private:
139 	// Blows up into the debugger if the validation fails.
140 			void				ValidateFields() const;
141 			void				ValidateField(const BField* field,
142 									int32 logicalFieldIndex) const;
143 private:
144 			BList				fFields;
145 			BPrivate::
146 			BRowContainer*		fChildList;
147 			bool				fIsExpanded;
148 			float				fHeight;
149 			BRow*				fNextSelected;
150 			BRow*				fPrevSelected;
151 			BRow*				fParent;
152 			BColumnListView*	fList;
153 
154 
155 	friend class BColumnListView;
156 	friend class BPrivate::RecursiveOutlineIterator;
157 	friend class BPrivate::OutlineView;
158 };
159 
160 // Information about a single column in the list.  A column knows
161 // how to display the BField objects that occur at its location in
162 // each of the list's rows.  See ColumnTypes.h for particular
163 // subclasses of BField and BColumn that handle common data types.
164 class BColumn {
165 public:
166 								BColumn(float width, float minWidth,
167 									float maxWidth,
168 									alignment align = B_ALIGN_LEFT);
169 	virtual 					~BColumn();
170 
171 			float				Width() const;
172 			void				SetWidth(float width);
173 			float				MinWidth() const;
174 			float				MaxWidth() const;
175 
176 	virtual	void				DrawTitle(BRect rect, BView* targetView);
177 	virtual	void				DrawField(BField* field, BRect rect,
178 									BView* targetView);
179 	virtual	int					CompareFields(BField* field1, BField* field2);
180 
181 	virtual void				MouseMoved(BColumnListView* parent, BRow* row,
182 									BField* field, BRect fieldRect,
183 									BPoint point, uint32 buttons, int32 code);
184 	virtual void				MouseDown(BColumnListView* parent, BRow* row,
185 									BField* field, BRect fieldRect,
186 									BPoint point, uint32 buttons);
187 	virtual	void				MouseUp(BColumnListView* parent, BRow* row,
188 									BField* field);
189 
190 	virtual	void				GetColumnName(BString* into) const;
191 	virtual	float				GetPreferredWidth(BField* field,
192 									BView* parent) const;
193 
194 			bool				IsVisible() const;
195 			void				SetVisible(bool);
196 
197 			bool				WantsEvents() const;
198 			void				SetWantsEvents(bool);
199 
200 			bool				ShowHeading() const;
201 			void				SetShowHeading(bool);
202 
203 			alignment			Alignment() const;
204 			void				SetAlignment(alignment);
205 
206 			int32				LogicalFieldNum() const;
207 
208 	/*!
209 		\param field The BField derivative to validate.
210 
211 			Implement this function on your BColumn derivatives to validate
212 			BField derivatives that your BColumn will be drawing/manipulating.
213 
214 			This function will be called when BFields are added to the Column,
215 			use dynamic_cast<> to determine if it is of a kind that your
216 			BColumn know how ot handle. return false if it is not.
217 
218 			\note The debugger will be called if you return false from here
219 			with information about what type of BField and BColumn and the
220 			logical field index where it occured.
221 
222 			\note Do not call the inherited version of this, it just returns
223 			true;
224 	  */
225 	virtual	bool				AcceptsField(const BField* field) const;
226 
227 private:
228 			float				fWidth;
229 			float 				fMinWidth;
230 			float				fMaxWidth;
231 			bool				fVisible;
232 			int32				fFieldID;
233 			BColumnListView*	fList;
234 			bool				fSortAscending;
235 			bool				fWantsEvents;
236 			bool				fShowHeading;
237 			alignment			fAlignment;
238 
239 	friend class BPrivate::OutlineView;
240 	friend class BColumnListView;
241 	friend class BPrivate::TitleView;
242 };
243 
244 // The column list view class.
245 class BColumnListView : public BView, public BInvoker {
246 public:
247 								BColumnListView(BRect rect,
248 									const char* name, uint32 resizingMode,
249 									uint32 flags, border_style = B_NO_BORDER,
250 									bool showHorizontalScrollbar = true);
251 								BColumnListView(const char* name,
252 									uint32 flags, border_style = B_NO_BORDER,
253 									bool showHorizontalScrollbar = true);
254 	virtual						~BColumnListView();
255 
256 	// Interaction
257 	virtual	bool				InitiateDrag(BPoint, bool wasSelected);
258 	virtual	void				MessageDropped(BMessage*, BPoint point);
259 	virtual	void				ExpandOrCollapse(BRow* row, bool expand);
260 	virtual	status_t			Invoke(BMessage* message = NULL);
261 	virtual	void				ItemInvoked();
262 	virtual	void				SetInvocationMessage(BMessage* message);
263 			BMessage* 			InvocationMessage() const;
264 			uint32 				InvocationCommand() const;
265 			BRow* 				FocusRow() const;
266 			void 				SetFocusRow(int32 index, bool select = false);
267 			void 				SetFocusRow(BRow* row, bool select = false);
268 			void 				SetMouseTrackingEnabled(bool);
269 
270 	// Selection
271 			list_view_type		SelectionMode() const;
272 			void 				Deselect(BRow* row);
273 			void 				AddToSelection(BRow* row);
274 			void 				DeselectAll();
275 			BRow*				CurrentSelection(BRow* lastSelected = 0) const;
276 	virtual	void				SelectionChanged();
277 	virtual	void				SetSelectionMessage(BMessage* message);
278 			BMessage*			SelectionMessage();
279 			uint32				SelectionCommand() const;
280 			void				SetSelectionMode(list_view_type type);
281 				// list_view_type is defined in ListView.h.
282 
283 	// Sorting
284 			void				SetSortingEnabled(bool);
285 			bool				SortingEnabled() const;
286 			void				SetSortColumn(BColumn* column, bool add,
287 									bool ascending);
288 			void				ClearSortColumns();
289 
290 	// The status view is a little area in the lower left hand corner.
291 			void				AddStatusView(BView* view);
292 			BView*				RemoveStatusView();
293 
294 	// Column Manipulation
295 			void				AddColumn(BColumn* column,
296 									int32 logicalFieldIndex);
297 			void				MoveColumn(BColumn* column, int32 index);
298 			void				RemoveColumn(BColumn* column);
299 			int32				CountColumns() const;
300 			BColumn*			ColumnAt(int32 index) const;
301 			BColumn*			ColumnAt(BPoint point) const;
302 			void				SetColumnVisible(BColumn* column,
303 									bool isVisible);
304 			void				SetColumnVisible(int32, bool);
305 			bool				IsColumnVisible(int32) const;
306 			void				SetColumnFlags(column_flags flags);
307 			void				ResizeColumnToPreferred(int32 index);
308 			void				ResizeAllColumnsToPreferred();
309 
310 	// Row manipulation
311 			const BRow*			RowAt(int32 index, BRow *parent = 0) const;
312 			BRow*				RowAt(int32 index, BRow *parent = 0);
313 			const BRow*			RowAt(BPoint) const;
314 			BRow*				RowAt(BPoint);
315 			bool				GetRowRect(const BRow* row, BRect* _rect) const;
316 			bool				FindParent(BRow* row, BRow** _parent,
317 									bool *_isVisible) const;
318 			int32				IndexOf(BRow* row);
319 			int32				CountRows(BRow* parent = 0) const;
320 			void				AddRow(BRow* row, BRow* parent = NULL);
321 			void				AddRow(BRow* row, int32 index,
322 									BRow* parent = NULL);
323 
324 			void				ScrollTo(const BRow* Row);
325 			void				ScrollTo(BPoint point);
326 
327 	// Does not delete row or children at this time.
328 	// todo: Make delete row and children
329 			void				RemoveRow(BRow* row);
330 			void				UpdateRow(BRow* row);
331 			bool				SwapRows(int32 index1, int32 index2, BRow*
332 									parentRow1 = NULL, BRow* parentRow2 = NULL);
333 			void				Clear();
334 
335 	// Appearance (DEPRECATED)
336 			void				GetFont(BFont* font) const
337 									{ BView::GetFont(font); }
338 	virtual	void				SetFont(const BFont* font,
339 									uint32 mask = B_FONT_ALL);
340 	virtual	void				SetHighColor(rgb_color);
341 			void				SetSelectionColor(rgb_color);
342 			void				SetBackgroundColor(rgb_color);
343 			void				SetEditColor(rgb_color);
344 			const rgb_color		SelectionColor() const;
345 			const rgb_color		BackgroundColor() const;
346 			const rgb_color		EditColor() const;
347 
348 	// Appearance (NEW STYLE)
349 			void				SetColor(ColumnListViewColor colorIndex,
350 									rgb_color color);
351 			void				SetFont(ColumnListViewFont fontIndex,
352 									const BFont* font,
353 									uint32 mask = B_FONT_ALL);
354 			rgb_color			Color(ColumnListViewColor colorIndex) const;
355 			void				GetFont(ColumnListViewFont fontIndex,
356 									BFont* font) const;
357 
358 			BPoint				SuggestTextPosition(const BRow* row,
359 									const BColumn* column = NULL) const;
360 
361 			void				SetLatchWidth(float width);
362 			float				LatchWidth() const;
363 	virtual	void				DrawLatch(BView* view, BRect frame,
364 									LatchType type, BRow* row);
365 	virtual	void				MakeFocus(bool isfocus = true);
366 			void				SaveState(BMessage* archive);
367 			void				LoadState(BMessage* archive);
368 
369 			BView*				ScrollView() const
370 									{ return (BView*)fOutlineView; }
371 			void				SetEditMode(bool state);
372 			void				Refresh();
373 
374 	virtual BSize				MinSize();
375 	virtual BSize				PreferredSize();
376 	virtual BSize				MaxSize();
377 
378 
379 protected:
380 	virtual	void 				MessageReceived(BMessage* message);
381 	virtual	void 				KeyDown(const char* bytes, int32 numBytes);
382 	virtual	void 				AttachedToWindow();
383 	virtual	void 				WindowActivated(bool active);
384 	virtual	void 				Draw(BRect updateRect);
385 
386 	virtual	void				LayoutInvalidated(bool descendants = false);
387 	virtual	void				DoLayout();
388 
389 private:
390 			void				_Init();
391 			void				_GetChildViewRects(const BRect& bounds,
392 									BRect& titleRect, BRect& outlineRect,
393 									BRect& vScrollBarRect,
394 									BRect& hScrollBarRect);
395 
396 			rgb_color 			fColorList[B_COLOR_TOTAL];
397 			BPrivate::TitleView* fTitleView;
398 			BPrivate::OutlineView* fOutlineView;
399 			BList 				fColumns;
400 			BScrollBar*			fHorizontalScrollBar;
401 			BScrollBar* 		fVerticalScrollBar;
402 			BList				fSortColumns;
403 			BView*				fStatusView;
404 			BMessage*			fSelectionMessage;
405 			bool				fSortingEnabled;
406 			float				fLatchWidth;
407 			border_style		fBorderStyle;
408 			bool				fShowingHorizontalScrollBar;
409 };
410 
411 #endif // _COLUMN_LIST_VIEW_H
412