ListView.h revision 4ee47fe9
1/*
2 * Copyright 2002-2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _LIST_VIEW_H
6#define _LIST_VIEW_H
7
8
9#include <Invoker.h>
10#include <List.h>
11#include <ListItem.h>
12#include <View.h>
13
14
15struct track_data;
16
17
18enum list_view_type {
19	B_SINGLE_SELECTION_LIST,
20	B_MULTIPLE_SELECTION_LIST
21};
22
23
24class BListView : public BView, public BInvoker {
25public:
26								BListView(BRect frame, const char* name,
27									list_view_type type
28										= B_SINGLE_SELECTION_LIST,
29									uint32 resizeMask = B_FOLLOW_LEFT_TOP,
30									uint32 flags = B_WILL_DRAW
31										| B_FRAME_EVENTS | B_NAVIGABLE);
32								BListView(const char* name,
33									list_view_type type
34										= B_SINGLE_SELECTION_LIST,
35									uint32 flags = B_WILL_DRAW
36										| B_FRAME_EVENTS | B_NAVIGABLE);
37								BListView(list_view_type type
38									= B_SINGLE_SELECTION_LIST);
39								BListView(BMessage* data);
40
41	virtual						~BListView();
42
43	static	BArchivable*		Instantiate(BMessage* data);
44	virtual status_t			Archive(BMessage* data,
45									bool deep = true) const;
46
47	virtual void				Draw(BRect updateRect);
48
49	virtual void				AttachedToWindow();
50	virtual void				DetachedFromWindow();
51	virtual void				AllAttached();
52	virtual void				AllDetached();
53	virtual void				FrameResized(float newWidth, float newHeight);
54	virtual void				FrameMoved(BPoint newPosition);
55	virtual void				TargetedByScrollView(BScrollView* view);
56	virtual void				WindowActivated(bool active);
57
58	virtual void				MessageReceived(BMessage* message);
59	virtual void				KeyDown(const char* bytes, int32 numBytes);
60	virtual void				MouseDown(BPoint where);
61	virtual void				MouseUp(BPoint where);
62	virtual void				MouseMoved(BPoint where, uint32 code,
63									const BMessage* dragMessage);
64
65	virtual void				ResizeToPreferred();
66	virtual void				GetPreferredSize(float *_width,
67									float *_height);
68
69	virtual	BSize				MinSize();
70	virtual	BSize				MaxSize();
71	virtual	BSize				PreferredSize();
72
73	virtual void				MakeFocus(bool state = true);
74
75	virtual	void				SetFont(const BFont* font, uint32 mask
76									= B_FONT_ALL);
77	virtual void				ScrollTo(BPoint where);
78	inline	void				ScrollTo(float x, float y);
79
80	virtual bool				AddItem(BListItem* item);
81	virtual bool				AddItem(BListItem* item, int32 atIndex);
82	virtual bool				AddList(BList* newItems);
83	virtual bool				AddList(BList* newItems, int32 atIndex);
84	virtual bool				RemoveItem(BListItem* item);
85	virtual BListItem*			RemoveItem(int32 index);
86	virtual bool				RemoveItems(int32 index, int32 count);
87
88	virtual void				SetSelectionMessage(BMessage* message);
89	virtual void				SetInvocationMessage(BMessage* message);
90
91			BMessage*			SelectionMessage() const;
92			uint32				SelectionCommand() const;
93			BMessage*			InvocationMessage() const;
94			uint32				InvocationCommand() const;
95
96	virtual void				SetListType(list_view_type type);
97			list_view_type		ListType() const;
98
99			BListItem*			ItemAt(int32 index) const;
100			int32				IndexOf(BPoint point) const;
101			int32				IndexOf(BListItem* item) const;
102			BListItem*			FirstItem() const;
103			BListItem*			LastItem() const;
104			bool				HasItem(BListItem* item) const;
105			int32				CountItems() const;
106	virtual void				MakeEmpty();
107			bool				IsEmpty() const;
108			void				DoForEach(bool (*func)(BListItem* item));
109			void				DoForEach(bool (*func)(BListItem* item,
110									void* arg), void* arg);
111			const BListItem**	Items() const;
112			void				InvalidateItem(int32 index);
113			void				ScrollToSelection();
114
115			void				Select(int32 index, bool extend = false);
116			void				Select(int32 from, int32 to,
117									bool extend = false);
118			bool				IsItemSelected(int32 index) const;
119			int32				CurrentSelection(int32 index = 0) const;
120	virtual status_t			Invoke(BMessage* message = NULL);
121
122			void				DeselectAll();
123			void				DeselectExcept(int32 exceptFrom,
124									int32 exceptTo);
125			void				Deselect(int32 index);
126
127	virtual void				SelectionChanged();
128
129	virtual bool				InitiateDrag(BPoint where, int32 index,
130									bool wasSelected);
131
132			void				SortItems(int (*cmp)(const void*,
133									const void*));
134
135	// These functions bottleneck through DoMiscellaneous()
136			bool				SwapItems(int32 a, int32 b);
137			bool				MoveItem(int32 from, int32 to);
138			bool				ReplaceItem(int32 index, BListItem* item);
139
140			BRect				ItemFrame(int32 index);
141
142	virtual BHandler*			ResolveSpecifier(BMessage* message,
143									int32 index, BMessage* specifier,
144									int32 what, const char* property);
145	virtual status_t			GetSupportedSuites(BMessage* data);
146
147	virtual status_t			Perform(perform_code code, void* arg);
148
149private:
150	virtual	void				_ReservedListView2();
151	virtual	void				_ReservedListView3();
152	virtual	void				_ReservedListView4();
153
154			BListView&			operator=(const BListView& other);
155
156protected:
157	enum MiscCode { B_NO_OP, B_REPLACE_OP, B_MOVE_OP, B_SWAP_OP };
158	union MiscData {
159		struct Spare { int32 data[5]; };
160		struct Replace { int32 index; BListItem *item; } replace;
161		struct Move { int32 from; int32 to; } move;
162		struct Swap { int32 a; int32 b; } swap;
163	};
164
165	virtual bool				DoMiscellaneous(MiscCode code, MiscData* data);
166
167private:
168	friend class BOutlineListView;
169
170			void				_InitObject(list_view_type type);
171			void				_FixupScrollBar();
172			void				_InvalidateFrom(int32 index);
173			status_t			_PostMessage(BMessage* message);
174			void				_UpdateItems();
175			int32				_RangeCheck(int32 index);
176			bool				_Select(int32 index, bool extend);
177			bool				_Select(int32 from, int32 to, bool extend);
178			bool				_Deselect(int32 index);
179			bool				_DeselectAll(int32 exceptFrom, int32 exceptTo);
180			int32				_CalcFirstSelected(int32 after);
181			int32				_CalcLastSelected(int32 before);
182			void				_RecalcItemTops(int32 start, int32 end = -1);
183
184	virtual void				DrawItem(BListItem* item, BRect itemRect,
185									bool complete = false);
186
187			bool				_SwapItems(int32 a, int32 b);
188			bool				_MoveItem(int32 from, int32 to);
189			bool				_ReplaceItem(int32 index, BListItem* item);
190			void				_RescanSelection(int32 from, int32 to);
191
192			void				_DoneTracking(BPoint where);
193			void				_Track(BPoint where, uint32);
194
195private:
196			BList				fList;
197			list_view_type		fListType;
198			int32				fFirstSelected;
199			int32				fLastSelected;
200			int32				fAnchorIndex;
201			BMessage*			fSelectMessage;
202			BScrollView*		fScrollView;
203			track_data*			fTrack;
204
205			uint32				_reserved[4];
206};
207
208
209inline void
210BListView::ScrollTo(float x, float y)
211{
212	ScrollTo(BPoint(x, y));
213}
214
215#endif // _LIST_VIEW_H
216