1/*
2 * Copyright 2006-2007, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stephan Aßmus <superstippi@gmx.de>
7 */
8#ifndef LIST_VIEWS_H
9#define LIST_VIEWS_H
10
11#include <ListItem.h>
12#include <ListView.h>
13#include <Message.h>
14
15enum {
16	FLAGS_NONE			= 0x00,
17	FLAGS_TINTED_LINE	= 0x01,
18	FLAGS_FOCUSED		= 0x02,
19};
20
21// portion of the listviews height that triggers autoscrolling
22// when the mouse is over it with a dragmessage
23#define SCROLL_AREA			0.1
24
25class BMessageRunner;
26class BMessageFilter;
27class InterfaceWindow;
28class BScrollView;
29
30// SimpleItem
31class SimpleItem : public BStringItem {
32 public:
33							SimpleItem(const char* name);
34		virtual				~SimpleItem();
35
36		virtual	void		Draw(BView* owner, BRect frame,
37								 uint32 flags);
38		virtual	void		DrawBackground(BView* owner, BRect frame,
39								  uint32 flags);
40};
41
42// DragSortableListView
43class DragSortableListView : public BListView {
44 public:
45							DragSortableListView(BRect frame, const char* name,
46								list_view_type type = B_SINGLE_SELECTION_LIST,
47								uint32 resizingMode = B_FOLLOW_LEFT
48									| B_FOLLOW_TOP,
49								uint32 flags = B_WILL_DRAW | B_NAVIGABLE
50									| B_FRAME_EVENTS);
51	virtual					~DragSortableListView();
52
53	// BListView interface
54	virtual	void			AttachedToWindow();
55	virtual	void			DetachedFromWindow();
56	virtual	void			FrameResized(float width, float height);
57	virtual	void			Draw(BRect updateRect);
58	virtual	void			ScrollTo(BPoint where);
59	virtual	void			TargetedByScrollView(BScrollView* scrollView);
60	virtual	bool			InitiateDrag(BPoint point, int32 index,
61										 bool wasSelected);
62	virtual void			MessageReceived(BMessage* message);
63	virtual	void			KeyDown(const char* bytes, int32 numBytes);
64	virtual	void			MouseDown(BPoint where);
65	virtual void			MouseMoved(BPoint where, uint32 transit,
66									   const BMessage* dragMessage);
67	virtual void			MouseUp(BPoint where);
68	virtual	void			WindowActivated(bool active);
69	virtual void			DrawItem(BListItem *item, BRect itemFrame,
70									 bool complete = false);
71
72	// DragSortableListView
73	virtual	void			SetDragCommand(uint32 command);
74	virtual	void			ModifiersChanged();	// called by window
75	virtual	void			DoubleClicked(int32 index) {}
76
77	virtual	void			SetItemFocused(int32 index);
78
79	virtual	bool			AcceptDragMessage(const BMessage* message) const;
80	virtual	void			SetDropTargetRect(const BMessage* message,
81											  BPoint where);
82
83	// autoscrolling
84			void			SetAutoScrolling(bool enable);
85			bool			DoesAutoScrolling() const;
86			BScrollView*	ScrollView() const
87								{ return fScrollView; }
88			void			ScrollTo(int32 index);
89
90			bool			MouseWheelChanged(float x, float y);
91
92	virtual	void			MoveItems(const BList& indices, int32 toIndex);
93	virtual	void			CopyItems(const BList& indices, int32 toIndex);
94	virtual	void			RemoveItemList(const BList& indices);
95
96			void			GetSelectedItems(BList& indices);
97			void			RemoveSelected(); // uses RemoveItemList()
98			void			RemoveAll(); // uses RemoveItemList()
99			int32			CountSelectedItems() const;
100			void			SelectAll();
101	virtual	bool			DeleteItem(int32 index);
102
103	virtual	BListItem*		CloneItem(int32 atIndex) const = 0;
104	virtual	void			DrawListItem(BView* owner, int32 index,
105										 BRect itemFrame) const = 0;
106	virtual	void			MakeDragMessage(BMessage* message) const = 0;
107
108 private:
109			void			_RemoveDropAnticipationRect();
110			void			_SetDragMessage(const BMessage* message);
111
112	BRect					fDropRect;
113	BMessage				fDragMessageCopy;
114	BMessageRunner*			fScrollPulse;
115	BPoint					fLastMousePos;
116
117 protected:
118			void			_SetDropAnticipationRect(BRect r);
119			void			_SetDropIndex(int32 index);
120
121	int32					fDropIndex;
122	BListItem*				fLastClickedItem;
123	BScrollView*			fScrollView;
124	uint32					fDragCommand;
125	int32					fFocusedIndex;
126};
127
128// SimpleListView
129class SimpleListView : public DragSortableListView {
130 public:
131							SimpleListView(BRect frame,
132								BMessage* selectionChangeMessage = NULL);
133							SimpleListView(BRect frame, const char* name,
134								BMessage* selectionChangeMessage = NULL,
135								list_view_type type = B_MULTIPLE_SELECTION_LIST,
136								uint32 resizingMode = B_FOLLOW_ALL_SIDES,
137								uint32 flags = B_WILL_DRAW | B_NAVIGABLE
138									| B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE);
139							~SimpleListView();
140
141	// DragSortableListView interface
142	virtual void			MessageReceived(BMessage* message);
143	virtual	void			SelectionChanged();
144
145	virtual	BListItem*		CloneItem(int32 atIndex) const;
146	virtual	void			DrawListItem(BView* owner, int32 index,
147										 BRect itemFrame) const;
148	virtual	void			MakeDragMessage(BMessage* message) const;
149
150 private:
151
152	BMessage*				fSelectionChangeMessage;
153};
154
155#endif // LIST_VIEWS_H
156