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				ScrollTo(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 where, int32 index,
131									bool wasSelected);
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				_UpdateItems();
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			void				_DoSelection(int32 index);
193
194private:
195			BList				fList;
196			list_view_type		fListType;
197			int32				fFirstSelected;
198			int32				fLastSelected;
199			int32				fAnchorIndex;
200			BMessage*			fSelectMessage;
201			BScrollView*		fScrollView;
202			track_data*			fTrack;
203
204			uint32				_reserved[4];
205};
206
207
208inline void
209BListView::ScrollTo(float x, float y)
210{
211	ScrollTo(BPoint(x, y));
212}
213
214#endif // _LIST_VIEW_H
215