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 void SetDropTargetRect(const BMessage* message, 115 BPoint where); 116 117 // autoscrolling 118 void SetAutoScrolling(bool enable); 119 bool DoesAutoScrolling() const; 120 BScrollView* ScrollView() const 121 { return fScrollView; } 122 void ScrollTo(int32 index); 123 124 virtual void MoveItems(BList& items, int32 toIndex); 125 virtual void CopyItems(BList& items, int32 toIndex); 126 virtual void RemoveItemList(BList& indices); 127 void RemoveSelected(); // uses RemoveItemList() 128 virtual bool DeleteItem(int32 index); 129 130 // selection 131 void SetSelection(Selection* selection); 132 133 virtual int32 IndexOfSelectable(Selectable* selectable) const; 134 virtual Selectable* SelectableFor(BListItem* item) const; 135 136 void SetDeselectAllInGlobalSelection(bool deselect); 137 138 void SelectAll(); 139 int32 CountSelectedItems() const; 140 virtual void SelectionChanged(); 141 142 virtual BListItem* CloneItem(int32 atIndex) const = 0; 143 virtual void DrawListItem(BView* owner, int32 index, 144 BRect itemFrame) const = 0; 145 virtual void MakeDragMessage(BMessage* message) const = 0; 146 147 private: 148 void _RemoveDropAnticipationRect(); 149 void _SetDragMessage(const BMessage* message); 150 151 BRect fDropRect; 152 BMessage fDragMessageCopy; 153 BMessageFilter* fMouseWheelFilter; 154 BMessageRunner* fScrollPulse; 155 BPoint fLastMousePos; 156 157 protected: 158 void _SetDropAnticipationRect(BRect r); 159 void _SetDropIndex(int32 index); 160 161 int32 fDropIndex; 162 BListItem* fLastClickedItem; 163 BScrollView* fScrollView; 164 uint32 fDragCommand; 165 int32 fFocusedIndex; 166 167 Selection* fSelection; 168 bool fSyncingToSelection; 169 bool fModifyingSelection; 170 }; 171 172 // SimpleListView 173 class SimpleListView : 174 #ifdef LIB_LAYOUT 175 public MView, 176 #endif 177 public DragSortableListView { 178 public: 179 SimpleListView(BRect frame, 180 BMessage* selectionChangeMessage = NULL); 181 SimpleListView(BRect frame, 182 const char* name, 183 BMessage* selectionChangeMessage = NULL, 184 list_view_type type 185 = B_MULTIPLE_SELECTION_LIST, 186 uint32 resizingMode 187 = B_FOLLOW_ALL_SIDES, 188 uint32 flags 189 = B_WILL_DRAW | B_NAVIGABLE 190 | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE); 191 ~SimpleListView(); 192 193 #ifdef LIB_LAYOUT 194 // MView 195 virtual minimax layoutprefs(); 196 virtual BRect layout(BRect frame); 197 #endif 198 // BListView 199 virtual void DetachedFromWindow(); 200 virtual void MessageReceived(BMessage* message); 201 202 virtual BListItem* CloneItem(int32 atIndex) const; 203 virtual void DrawListItem(BView* owner, int32 index, 204 BRect itemFrame) const; 205 virtual void MakeDragMessage(BMessage* message) const; 206 207 protected: 208 void _MakeEmpty(); 209 210 private: 211 212 BMessage* fSelectionChangeMessage; 213 }; 214 215 #endif // LIST_VIEWS_H 216