xref: /haiku/src/apps/icon-o-matic/generic/gui/ListViews.h (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 /*
2  * Copyright 2006, 2023, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  *		Zardshard
8  */
9 
10 #ifndef LIST_VIEWS_H
11 #define LIST_VIEWS_H
12 
13 #include <ListItem.h>
14 #include <ListView.h>
15 #include <Message.h>
16 
17 #ifdef LIB_LAYOUT
18 #  include <layout.h>
19 #endif
20 
21 #include "MouseWheelFilter.h"
22 #include "Observer.h"
23 
24 enum
25 {
26 	FLAGS_NONE			= 0x00,
27 	FLAGS_TINTED_LINE	= 0x01,
28 	FLAGS_FOCUSED		= 0x02,
29 };
30 
31 // portion of the listviews height that triggers autoscrolling
32 // when the mouse is over it with a dragmessage
33 #define SCROLL_AREA			0.1
34 
35 class BMessageFilter;
36 class BMessageRunner;
37 class BScrollView;
38 class Selectable;
39 class Selection;
40 
41 // SimpleItem
42 class SimpleItem : public BStringItem
43 {
44  public:
45 							SimpleItem(const char* name);
46 		virtual				~SimpleItem();
47 
48 		virtual	void		Draw(BView* owner, BRect frame,
49 								 uint32 flags);
50 		virtual	void		DrawBackground(BView* owner, BRect frame,
51 								  uint32 flags);
52 
53 							// let the item know what's going on
54 /*		virtual	void		AttachedToListView(SimpleListView* owner);
55 		virtual	void		DetachedFromListView(SimpleListView* owner);
56 
57 		virtual	void		SetItemFrame(BRect frame);*/
58 
59  private:
60 
61 };
62 
63 // DragSortableListView
64 class DragSortableListView : public MouseWheelTarget,
65 							 public BListView,
66 							 public Observer {
67  public:
68 							DragSortableListView(BRect frame,
69 												 const char* name,
70 												 list_view_type type
71 														= B_SINGLE_SELECTION_LIST,
72 												 uint32 resizingMode
73 														= B_FOLLOW_LEFT
74 														  | B_FOLLOW_TOP,
75 												 uint32 flags
76 														= B_WILL_DRAW
77 														  | B_NAVIGABLE
78 														  | B_FRAME_EVENTS);
79 	virtual					~DragSortableListView();
80 
81 	// BListView interface
82 	virtual	void			AttachedToWindow();
83 	virtual	void			DetachedFromWindow();
84 	virtual	void			FrameResized(float width, float height);
85 //	virtual	void			MakeFocus(bool focused);
86 	virtual	void			Draw(BRect updateRect);
87 	virtual	void			ScrollTo(BPoint where);
88 	virtual	void			TargetedByScrollView(BScrollView* scrollView);
89 	virtual	bool			InitiateDrag(BPoint point, int32 index,
90 										 bool wasSelected);
91 	virtual void			MessageReceived(BMessage* message);
92 	virtual	void			KeyDown(const char* bytes, int32 numBytes);
93 	virtual	void			MouseDown(BPoint where);
94 	virtual void			MouseMoved(BPoint where, uint32 transit,
95 									   const BMessage* dragMessage);
96 	virtual void			MouseUp(BPoint where);
97 	virtual	void			WindowActivated(bool active);
98 	virtual void			DrawItem(BListItem *item, BRect itemFrame,
99 									 bool complete = false);
100 
101 	// MouseWheelTarget interface
102 	virtual	bool			MouseWheelChanged(float x, float y);
103 
104 	// Observer interface (watching Selection)
105 	virtual	void			ObjectChanged(const Observable* object);
106 
107 	// DragSortableListView
108 	virtual	void			SetDragCommand(uint32 command);
109 	virtual	void			ModifiersChanged();	// called by window
110 	virtual	void			DoubleClicked(int32 index) {}
111 
112 	virtual	void			SetItemFocused(int32 index);
113 
114 	virtual	bool			AcceptDragMessage(const BMessage* message) const;
115 	virtual	bool			HandleDropMessage(const BMessage* message,
116 								int32 dropIndex);
117 	virtual	void			SetDropTargetRect(const BMessage* message,
118 								BPoint where);
119 
120 							// autoscrolling
121 			void			SetAutoScrolling(bool enable);
122 			bool			DoesAutoScrolling() const;
123 			BScrollView*	ScrollView() const
124 								{ return fScrollView; }
125 			void			ScrollTo(int32 index);
126 
127 	virtual	void			MoveItems(BList& items, int32 toIndex);
128 	virtual	void			CopyItems(BList& items, int32 toIndex);
129 	virtual	void			RemoveItemList(BList& indices);
130 			void			RemoveSelected(); // uses RemoveItemList()
131 	virtual	bool			DeleteItem(int32 index);
132 
133 							// selection
134 			void			SetSelection(Selection* selection);
135 
136 	virtual	int32			IndexOfSelectable(Selectable* selectable) const;
137 	virtual	Selectable*		SelectableFor(BListItem* item) const;
138 
139 			void			SetDeselectAllInGlobalSelection(bool deselect);
140 
141 			void			SelectAll();
142 			int32			CountSelectedItems() const;
143 	virtual	void			SelectionChanged();
144 
145 	virtual	BListItem*		CloneItem(int32 atIndex) const = 0;
146 	virtual	void			DrawListItem(BView* owner, int32 index,
147 										 BRect itemFrame) const = 0;
148 	virtual	void			MakeDragMessage(BMessage* message) const = 0;
149 
150  private:
151 			void			_RemoveDropAnticipationRect();
152 			void			_SetDragMessage(const BMessage* message);
153 
154 	BRect					fDropRect;
155 	BMessage				fDragMessageCopy;
156 	BMessageFilter*			fMouseWheelFilter;
157 	BMessageRunner*			fScrollPulse;
158 	BPoint					fLastMousePos;
159 
160  protected:
161 			void			_SetDropAnticipationRect(BRect r);
162 			void			_SetDropIndex(int32 index);
163 
164 	int32					fDropIndex;
165 	BListItem*				fLastClickedItem;
166 	BScrollView*			fScrollView;
167 	uint32					fDragCommand;
168 	int32					fFocusedIndex;
169 
170 	Selection*				fSelection;
171 	bool					fSyncingToSelection;
172 	bool					fModifyingSelection;
173 };
174 
175 // SimpleListView
176 class SimpleListView :
177 					   #ifdef LIB_LAYOUT
178 					   public MView,
179 					   #endif
180 					   public DragSortableListView {
181  public:
182 							SimpleListView(BRect frame,
183 										   BMessage* selectionChangeMessage = NULL);
184 							SimpleListView(BRect frame,
185 										   const char* name,
186 										   BMessage* selectionChangeMessage = NULL,
187 										   list_view_type type
188 												= B_MULTIPLE_SELECTION_LIST,
189 										   uint32 resizingMode
190 												= B_FOLLOW_ALL_SIDES,
191 										   uint32 flags
192 												= B_WILL_DRAW | B_NAVIGABLE
193 												  | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE);
194 							~SimpleListView();
195 
196 	#ifdef LIB_LAYOUT
197 							// MView
198 	virtual	minimax			layoutprefs();
199 	virtual	BRect			layout(BRect frame);
200 	#endif
201 							// BListView
202 	virtual	void			DetachedFromWindow();
203 	virtual void			MessageReceived(BMessage* message);
204 
205 	virtual	BListItem*		CloneItem(int32 atIndex) const;
206 	virtual	void			DrawListItem(BView* owner, int32 index,
207 										 BRect itemFrame) const;
208 
209 	/*! Archive the selected items.
210 		The information should be sufficient for \c InstantiateSelection to
211 		create a new copy of the objects without relying on the original object.
212 	*/
213 	virtual	status_t		ArchiveSelection(BMessage* into, bool deep = true) const = 0;
214 	/*! Put a copy of the items archived by \c ArchiveSelection into the list.
215 		This method should ensure whether the item is truly meant for the list
216 		view.
217 	*/
218 	virtual	bool			InstantiateSelection(const BMessage* archive, int32 dropIndex) = 0;
219 
220 	virtual	void			MakeDragMessage(BMessage* message) const;
221 	virtual	bool			HandleDropMessage(const BMessage* message,
222 								int32 dropIndex);
223 			bool			HandlePaste(const BMessage* archive);
224 
225  protected:
226 			void			_MakeEmpty();
227 
228  private:
229 
230 	BMessage*				fSelectionChangeMessage;
231 };
232 
233 #endif // LIST_VIEWS_H
234