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