1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2009, Stephan Aßmus, superstippi@gmx.de. 4 * Distributed under the terms of the MIT License. 5 */ 6 #ifndef HEADER_VIEW_H 7 #define HEADER_VIEW_H 8 9 10 #include <View.h> 11 12 #include <ObjectList.h> 13 #include <Referenceable.h> 14 #include <Variant.h> 15 16 17 class Header; 18 class HeaderModel; 19 class HeaderViewListener; 20 21 22 class HeaderRenderer { 23 public: 24 virtual ~HeaderRenderer(); 25 26 virtual float HeaderHeight(BView* view, const Header* header) 27 = 0; 28 virtual float PreferredHeaderWidth(BView* view, 29 const Header* header) = 0; 30 virtual void DrawHeader(BView* view, BRect frame, 31 BRect updateRect, const Header* header, 32 uint32 flags) = 0; 33 virtual void DrawHeaderBackground(BView* view, BRect frame, 34 BRect updateRect, uint32 flags); 35 }; 36 37 38 class DefaultHeaderRenderer : public HeaderRenderer { 39 public: 40 DefaultHeaderRenderer(); 41 virtual ~DefaultHeaderRenderer(); 42 43 virtual float HeaderHeight(BView* view, const Header* header); 44 virtual float PreferredHeaderWidth(BView* view, 45 const Header* header); 46 virtual void DrawHeader(BView* view, BRect frame, 47 BRect updateRect, const Header* header, 48 uint32 flags); 49 }; 50 51 52 class HeaderListener { 53 public: 54 virtual ~HeaderListener(); 55 56 virtual void HeaderWidthChanged(Header* header); 57 virtual void HeaderWidthRestrictionsChanged(Header* header); 58 virtual void HeaderValueChanged(Header* header); 59 virtual void HeaderRendererChanged(Header* header); 60 }; 61 62 63 class Header { 64 public: 65 Header(int32 modelIndex = 0); 66 Header(float width, float minWidth, 67 float maxWidth, float preferredWidth, 68 int32 modelIndex = 0); 69 70 float Width() const; 71 float MinWidth() const; 72 float MaxWidth() const; 73 float PreferredWidth() const; 74 75 void SetWidth(float width); 76 void SetMinWidth(float width); 77 void SetMaxWidth(float width); 78 void SetPreferredWidth(float width); 79 80 bool IsResizable() const; 81 void SetResizable(bool resizable); 82 83 bool GetValue(BVariant& _value) const; 84 void SetValue(const BVariant& value); 85 86 int32 ModelIndex() const; 87 void SetModelIndex(int32 index); 88 89 HeaderRenderer* GetHeaderRenderer() const; 90 void SetHeaderRenderer(HeaderRenderer* renderer); 91 92 void AddListener(HeaderListener* listener); 93 void RemoveListener(HeaderListener* listener); 94 95 protected: 96 void NotifyWidthChanged(); 97 void NotifyWidthRestrictionsChanged(); 98 void NotifyValueChanged(); 99 void NotifyRendererChanged(); 100 101 private: 102 typedef BObjectList<HeaderListener> ListenerList; 103 104 private: 105 float fWidth; 106 float fMinWidth; 107 float fMaxWidth; 108 float fPreferredWidth; 109 BVariant fValue; 110 HeaderRenderer* fRenderer; 111 int32 fModelIndex; 112 bool fResizable; 113 ListenerList fListeners; 114 }; 115 116 117 class HeaderModelListener { 118 public: 119 virtual ~HeaderModelListener(); 120 121 virtual void HeaderAdded(HeaderModel* model, int32 index); 122 virtual void HeaderRemoved(HeaderModel* model, int32 index); 123 virtual void HeaderMoved(HeaderModel* model, 124 int32 fromIndex, int32 toIndex); 125 }; 126 127 128 class HeaderModel : public BReferenceable { 129 public: 130 HeaderModel(); 131 virtual ~HeaderModel(); 132 133 virtual int32 CountHeaders() const; 134 virtual Header* HeaderAt(int32 index) const; 135 virtual int32 IndexOfHeader(Header* header) const; 136 137 virtual bool AddHeader(Header* header); 138 virtual Header* RemoveHeader(int32 index); 139 virtual void RemoveHeader(Header* header); 140 virtual bool MoveHeader(int32 fromIndex, int32 toIndex); 141 142 virtual void AddListener(HeaderModelListener* listener); 143 virtual void RemoveListener(HeaderModelListener* listener); 144 145 protected: 146 void NotifyHeaderAdded(int32 index); 147 void NotifyHeaderRemoved(int32 index); 148 void NotifyHeaderMoved(int32 fromIndex, 149 int32 toIndex); 150 151 private: 152 typedef BObjectList<Header> HeaderList; 153 typedef BObjectList<HeaderModelListener> ListenerList; 154 155 private: 156 HeaderList fHeaders; 157 ListenerList fListeners; 158 }; 159 160 161 class HeaderView : public BView, private HeaderModelListener, 162 private HeaderListener { 163 public: 164 HeaderView(); 165 virtual ~HeaderView(); 166 167 virtual void Draw(BRect updateRect); 168 169 virtual void MouseDown(BPoint where); 170 virtual void MouseUp(BPoint where); 171 virtual void MouseMoved(BPoint where, uint32 transit, 172 const BMessage* dragMessage); 173 174 virtual BSize MinSize(); 175 virtual BSize MaxSize(); 176 virtual BSize PreferredSize(); 177 178 HeaderModel* Model() const; 179 status_t SetModel(HeaderModel* model); 180 181 void AddListener(HeaderViewListener* listener); 182 void RemoveListener(HeaderViewListener* listener); 183 184 private: 185 struct HeaderEntry; 186 187 typedef BObjectList<HeaderEntry> HeaderEntryList; 188 typedef BObjectList<HeaderViewListener> ListenerList; 189 190 class DragState; 191 192 private: 193 // HeaderModelListener 194 virtual void HeaderAdded(HeaderModel* model, int32 index); 195 virtual void HeaderRemoved(HeaderModel* model, int32 index); 196 virtual void HeaderMoved(HeaderModel* model, 197 int32 fromIndex, int32 toIndex); 198 199 // HeaderListener 200 virtual void HeaderWidthChanged(Header* header); 201 virtual void HeaderWidthRestrictionsChanged(Header* header); 202 virtual void HeaderValueChanged(Header* header); 203 virtual void HeaderRendererChanged(Header* header); 204 205 void _HeaderPropertiesChanged(Header* header, 206 bool redrawNeeded, bool relayoutNeeded); 207 void _InvalidateHeadersLayout(int32 firstIndex); 208 void _InvalidateHeaders(int32 firstIndex, 209 int32 endIndex); 210 void _ValidateHeadersLayout(); 211 212 private: 213 HeaderModel* fModel; 214 HeaderEntryList fHeaderEntries; 215 float fPreferredWidth; 216 float fPreferredHeight; 217 bool fLayoutValid; 218 ListenerList fListeners; 219 DragState* fDragState; 220 }; 221 222 223 class HeaderViewListener { 224 public: 225 virtual ~HeaderViewListener(); 226 }; 227 228 229 #endif // HEADER_VIEW_H 230