1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2001-2002, OpenBeOS 3 // Distributed under the terms of the OpenBeOS license 4 // 5 // File: ListView.h 6 // Author: Axel Doerfler 7 // Description: BListView represents a one-dimensional list view. 8 // 9 //------------------------------------------------------------------------------ 10 11 #ifndef _LIST_VIEW_H 12 #define _LIST_VIEW_H 13 14 #include <Invoker.h> 15 #include <View.h> 16 #include <List.h> 17 #include <ListItem.h> 18 19 struct track_data; 20 21 enum list_view_type { 22 B_SINGLE_SELECTION_LIST, 23 B_MULTIPLE_SELECTION_LIST 24 }; 25 26 #ifdef USE_OPENBEOS_NAMESPACE 27 namespace OpenBeOS { 28 #endif 29 30 //----- BListView class ------------------------------------------ 31 32 class BListView : public BView, public BInvoker 33 { 34 public: 35 BListView(BRect frame, const char *name, 36 list_view_type type = B_SINGLE_SELECTION_LIST, 37 uint32 resizeMask = B_FOLLOW_LEFT | B_FOLLOW_TOP, 38 uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE); 39 BListView(BMessage *data); 40 41 virtual ~BListView() ; 42 43 static BArchivable *Instantiate(BMessage *data); 44 virtual status_t Archive(BMessage *data, bool deep = true) const; 45 virtual void Draw(BRect updateRect); 46 virtual void MessageReceived(BMessage *msg); 47 virtual void MouseDown(BPoint where); 48 virtual void KeyDown(const char *bytes, int32 numBytes); 49 virtual void MakeFocus(bool state = true); 50 virtual void FrameResized(float newWidth, float newHeight); 51 virtual void TargetedByScrollView(BScrollView *scroller); 52 void ScrollTo(float x, float y); 53 virtual void ScrollTo(BPoint where); 54 virtual bool AddItem(BListItem *item); 55 virtual bool AddItem(BListItem *item, int32 atIndex); 56 virtual bool AddList(BList *newItems); 57 virtual bool AddList(BList *newItems, int32 atIndex); 58 virtual bool RemoveItem(BListItem *item); 59 virtual BListItem *RemoveItem(int32 index); 60 virtual bool RemoveItems(int32 index, int32 count); 61 62 virtual void SetSelectionMessage(BMessage *message); 63 virtual void SetInvocationMessage(BMessage *message); 64 65 BMessage *SelectionMessage() const; 66 uint32 SelectionCommand() const; 67 BMessage *InvocationMessage() const; 68 uint32 InvocationCommand() const; 69 70 virtual void SetListType(list_view_type type); 71 list_view_type ListType() const; 72 73 BListItem *ItemAt(int32 index) const; 74 int32 IndexOf(BPoint point) const; 75 int32 IndexOf(BListItem *item) const; 76 BListItem *FirstItem() const; 77 BListItem *LastItem() const; 78 bool HasItem(BListItem *item) const; 79 int32 CountItems() const; 80 virtual void MakeEmpty(); 81 bool IsEmpty() const; 82 void DoForEach(bool (*func)(BListItem *)); 83 void DoForEach(bool (*func)(BListItem *, void *), void *); 84 const BListItem **Items() const; 85 void InvalidateItem(int32 index); 86 void ScrollToSelection(); 87 88 void Select(int32 index, bool extend = false); 89 void Select(int32 from, int32 to, bool extend = false); 90 bool IsItemSelected(int32 index) const; 91 int32 CurrentSelection(int32 index = 0) const; 92 virtual status_t Invoke(BMessage *msg = NULL); 93 94 void DeselectAll(); 95 void DeselectExcept(int32 except_from, int32 except_to); 96 void Deselect(int32 index); 97 98 virtual void SelectionChanged(); 99 100 void SortItems(int (*cmp)(const void *, const void *)); 101 102 /* These functions bottleneck through DoMiscellaneous() */ 103 bool SwapItems(int32 a, int32 b); 104 bool MoveItem(int32 from, int32 to); 105 bool ReplaceItem(int32 index, BListItem *item); 106 107 virtual void AttachedToWindow(); 108 virtual void FrameMoved(BPoint new_position); 109 110 BRect ItemFrame(int32 index); 111 112 virtual BHandler *ResolveSpecifier(BMessage *msg, int32 index, 113 BMessage *specifier, int32 form, const char *property); 114 virtual status_t GetSupportedSuites(BMessage *data); 115 116 virtual status_t Perform(perform_code code, void *arg); 117 118 virtual void WindowActivated(bool state); 119 virtual void MouseUp(BPoint point); 120 virtual void MouseMoved(BPoint point, uint32 code, const BMessage *msg); 121 virtual void DetachedFromWindow(); 122 virtual bool InitiateDrag(BPoint point, int32 itemIndex, bool initialySelected); 123 124 virtual void ResizeToPreferred(); 125 virtual void GetPreferredSize(float *width, float *height); 126 virtual void AllAttached(); 127 virtual void AllDetached(); 128 129 protected: 130 enum MiscCode { B_NO_OP, B_REPLACE_OP, B_MOVE_OP, B_SWAP_OP }; 131 union MiscData { 132 struct Spare { int32 data[5]; }; 133 struct Replace { int32 index; BListItem *item; } replace; 134 struct Move { int32 from; int32 to; } move; 135 struct Swap { int32 a; int32 b; } swap; 136 }; 137 138 virtual bool DoMiscellaneous(MiscCode code, MiscData *data); 139 140 //----- Private or reserved ----------------------------------------- 141 142 private: 143 friend class BOutlineListView; 144 145 virtual void _ReservedListView2(); 146 virtual void _ReservedListView3(); 147 virtual void _ReservedListView4(); 148 149 BListView &operator=(const BListView &); 150 151 void _InitObject(list_view_type type); 152 void _FixupScrollBar(); 153 void _InvalidateFrom(int32 index); 154 status_t _PostMessage(BMessage *msg); 155 void _FontChanged(); 156 int32 _RangeCheck(int32 index); 157 bool _Select(int32 index, bool extend); 158 bool _Select(int32 from, int32 to, bool extend); 159 bool _Deselect(int32 index); 160 // void _Deselect(int32 from, int32 to); 161 bool _DeselectAll(int32 except_from, int32 except_to); 162 // void PerformDelayedSelect(); 163 bool _TryInitiateDrag(BPoint where); 164 int32 _CalcFirstSelected(int32 after); 165 int32 _CalcLastSelected(int32 before); 166 virtual void DrawItem(BListItem *item, BRect itemRect, bool complete = false); 167 168 bool _SwapItems(int32 a, int32 b); 169 bool _MoveItem(int32 from, int32 to); 170 bool _ReplaceItem(int32 index, BListItem *item); 171 void _RescanSelection(int32 from, int32 to); 172 173 BList fList; 174 list_view_type fListType; 175 int32 fFirstSelected; 176 int32 fLastSelected; 177 int32 fAnchorIndex; 178 float fWidth; 179 BMessage* fSelectMessage; 180 BScrollView* fScrollView; 181 track_data* fTrack; 182 183 uint32 _reserved[3]; 184 }; 185 186 187 inline void 188 BListView::ScrollTo(float x, float y) 189 { 190 /* OK, no private parts */ 191 ScrollTo(BPoint(x, y)); 192 } 193 194 /*-------------------------------------------------------------*/ 195 196 #ifdef USE_OPENBEOS_NAMESPACE 197 } 198 #endif 199 200 #endif /* _LIST_VIEW_H */ 201