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