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#include "MouseWheelFilter.h"
18#include "Observer.h"
19
20// portion of the listviews height that triggers autoscrolling
21// when the mouse is over it with a dragmessage
22#define SCROLL_AREA			0.1
23
24class BMessageFilter;
25class BMessageRunner;
26class BScrollView;
27class Selectable;
28class Selection;
29
30// SimpleItem
31class SimpleItem : public BStringItem
32{
33 public:
34							SimpleItem(const char* name);
35		virtual				~SimpleItem();
36
37		virtual	void		DrawItem(BView*, BRect, bool even = false);
38		virtual	void		DrawBackground(BView*, BRect, bool even);
39};
40
41
42class DragSortableListView : public BListView,
43	public MouseWheelTarget, public Observer {
44 public:
45							DragSortableListView(BRect frame,
46								const char* name,
47								list_view_type type = B_SINGLE_SELECTION_LIST,
48								uint32 resizingMode
49									= B_FOLLOW_LEFT | B_FOLLOW_TOP,
50								uint32 flags = B_WILL_DRAW | B_NAVIGABLE
51									| B_FRAME_EVENTS);
52	virtual					~DragSortableListView();
53
54	// BListView interface
55	virtual	void			AttachedToWindow();
56	virtual	void			DetachedFromWindow();
57	virtual	void			FrameResized(float width, float height);
58//	virtual	void			MakeFocus(bool focused);
59	virtual	void			TargetedByScrollView(BScrollView*);
60	virtual void			MessageReceived(BMessage* message);
61	virtual	void			KeyDown(const char* bytes, int32 numBytes);
62	virtual	void			MouseDown(BPoint where);
63	virtual void			MouseMoved(BPoint where, uint32, const BMessage*);
64	virtual void			MouseUp(BPoint where);
65	virtual	void			WindowActivated(bool active);
66
67	// MouseWheelTarget interface
68	virtual	bool			MouseWheelChanged(float x, float y);
69
70	// Observer interface (watching Selection)
71	virtual	void			ObjectChanged(const Observable* object);
72
73	// DragSortableListView
74	virtual	void			SetDragCommand(uint32 command);
75	virtual	void			ModifiersChanged();	// called by window
76	virtual	void			DoubleClicked(int32 index) {}
77
78	virtual	void			SetItemFocused(int32 index);
79
80	virtual	bool			AcceptDragMessage(const BMessage* message) const;
81	virtual	bool			HandleDropMessage(const BMessage* message,
82								int32 dropIndex);
83	virtual	void			SetDropTargetRect(const BMessage* message,
84								BPoint where);
85
86			bool			DoesAutoScrolling() const;
87			BScrollView*	ScrollView() const
88								{ return fScrollView; }
89
90	virtual	void			MoveItems(BList& items, int32 toIndex);
91	virtual	void			CopyItems(BList& items, int32 toIndex);
92	virtual	void			RemoveItemList(BList& indices);
93			void			RemoveSelected(); // uses RemoveItemList()
94	virtual	bool			DeleteItem(int32 index);
95
96							// selection
97			void			SetSelection(Selection* selection);
98
99	virtual	int32			IndexOfSelectable(Selectable* selectable) const;
100	virtual	Selectable*		SelectableFor(BListItem* item) const;
101
102			void			SetDeselectAllInGlobalSelection(bool deselect);
103
104			void			SelectAll();
105			int32			CountSelectedItems() const;
106	virtual	void			SelectionChanged();
107
108	virtual	BListItem*		CloneItem(int32 atIndex) const = 0;
109	virtual	void			MakeDragMessage(BMessage* message) const = 0;
110
111 protected:
112			void			InvalidateDropRect();
113			void			SetDragMessage(const BMessage* message);
114
115	BRect					fDropRect;
116	BMessage				fDragMessageCopy;
117	BMessageFilter*			fMouseWheelFilter;
118	BMessageRunner*			fScrollPulse;
119	BPoint					fLastMousePos;
120
121 protected:
122			void			SetDropRect(BRect rect);
123			void			SetDropIndex(int32 index);
124
125	int32					fDropIndex;
126	BListItem*				fLastClickedItem;
127	BScrollView*			fScrollView;
128	uint32					fDragCommand;
129	int32					fFocusedIndex;
130
131	Selection*				fSelection;
132	bool					fSyncingToSelection;
133	bool					fModifyingSelection;
134};
135
136// SimpleListView
137class SimpleListView : public DragSortableListView {
138 public:
139							SimpleListView(BRect frame,
140								BMessage* selectionChanged = NULL);
141							SimpleListView(BRect frame, const char* name,
142								BMessage* selectionChanged = NULL,
143								list_view_type type
144									= B_MULTIPLE_SELECTION_LIST,
145								uint32 resizingMode = B_FOLLOW_ALL_SIDES,
146								uint32 flags = B_WILL_DRAW | B_NAVIGABLE
147									| B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE);
148							~SimpleListView();
149
150							// BListView
151	virtual	void			DetachedFromWindow();
152	virtual	void			Draw(BRect updateRect);
153	virtual	bool			InitiateDrag(BPoint, int32, bool);
154	virtual void			MessageReceived(BMessage* message);
155
156	virtual	BListItem*		CloneItem(int32 atIndex) const;
157
158	/*! Archive the selected items.
159		The information should be sufficient for \c InstantiateSelection to
160		create a new copy of the objects without relying on the original object.
161	*/
162	virtual	status_t		ArchiveSelection(BMessage*, bool = true) const = 0;
163	/*! Put a copy of the items archived by \c ArchiveSelection into the list.
164		This method should ensure whether the item is truly meant for the list
165		view.
166	*/
167	virtual	bool			InstantiateSelection(const BMessage*, int32) = 0;
168
169	virtual	void			MakeDragMessage(BMessage* message) const;
170	virtual	bool			HandleDropMessage(const BMessage* message,
171								int32 dropIndex);
172			bool			HandlePaste(const BMessage* archive);
173
174 protected:
175			void			_MakeEmpty();
176
177 private:
178
179	BMessage*				fSelectionChangeMessage;
180};
181
182#endif // LIST_VIEWS_H
183