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