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