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