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 virtual BSize MinSize(); 130 virtual BSize PreferredSize(); 131 132 protected: 133 enum MiscCode { B_NO_OP, B_REPLACE_OP, B_MOVE_OP, B_SWAP_OP }; 134 union MiscData { 135 struct Spare { int32 data[5]; }; 136 struct Replace { int32 index; BListItem *item; } replace; 137 struct Move { int32 from; int32 to; } move; 138 struct Swap { int32 a; int32 b; } swap; 139 }; 140 141 virtual bool DoMiscellaneous(MiscCode code, MiscData *data); 142 143 //----- Private or reserved ----------------------------------------- 144 145 private: 146 friend class BOutlineListView; 147 148 virtual void _ReservedListView2(); 149 virtual void _ReservedListView3(); 150 virtual void _ReservedListView4(); 151 152 BListView &operator=(const BListView &); 153 154 void _InitObject(list_view_type type); 155 void _FixupScrollBar(); 156 void _InvalidateFrom(int32 index); 157 status_t _PostMessage(BMessage *msg); 158 void _FontChanged(); 159 int32 _RangeCheck(int32 index); 160 bool _Select(int32 index, bool extend); 161 bool _Select(int32 from, int32 to, bool extend); 162 bool _Deselect(int32 index); 163 // void _Deselect(int32 from, int32 to); 164 bool _DeselectAll(int32 except_from, int32 except_to); 165 // void PerformDelayedSelect(); 166 bool _TryInitiateDrag(BPoint where); 167 int32 _CalcFirstSelected(int32 after); 168 int32 _CalcLastSelected(int32 before); 169 virtual void DrawItem(BListItem *item, BRect itemRect, bool complete = false); 170 171 bool _SwapItems(int32 a, int32 b); 172 bool _MoveItem(int32 from, int32 to); 173 bool _ReplaceItem(int32 index, BListItem *item); 174 void _RescanSelection(int32 from, int32 to); 175 176 BList fList; 177 list_view_type fListType; 178 int32 fFirstSelected; 179 int32 fLastSelected; 180 int32 fAnchorIndex; 181 float fWidth; 182 BMessage* fSelectMessage; 183 BScrollView* fScrollView; 184 track_data* fTrack; 185 186 uint32 _reserved[3]; 187 }; 188 189 190 inline void 191 BListView::ScrollTo(float x, float y) 192 { 193 /* OK, no private parts */ 194 ScrollTo(BPoint(x, y)); 195 } 196 197 /*-------------------------------------------------------------*/ 198 199 #ifdef USE_OPENBEOS_NAMESPACE 200 } 201 #endif 202 203 #endif /* _LIST_VIEW_H */ 204