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