1 ///////////////////////////////////////////////////////////////////////////// 2 // Name: wx/generic/treectlg.h 3 // Purpose: wxTreeCtrl class 4 // Author: Robert Roebling 5 // Modified by: 6 // Created: 01/02/97 7 // Copyright: (c) 1997,1998 Robert Roebling 8 // Licence: wxWindows licence 9 ///////////////////////////////////////////////////////////////////////////// 10 11 #ifndef _GENERIC_TREECTRL_H_ 12 #define _GENERIC_TREECTRL_H_ 13 14 #if wxUSE_TREECTRL 15 16 #include "wx/brush.h" 17 #include "wx/pen.h" 18 #include "wx/scrolwin.h" 19 20 // ----------------------------------------------------------------------------- 21 // forward declaration 22 // ----------------------------------------------------------------------------- 23 24 class WXDLLIMPEXP_FWD_CORE wxGenericTreeItem; 25 26 class WXDLLIMPEXP_FWD_CORE wxTreeItemData; 27 28 class WXDLLIMPEXP_FWD_CORE wxTreeRenameTimer; 29 class WXDLLIMPEXP_FWD_CORE wxTreeFindTimer; 30 class WXDLLIMPEXP_FWD_CORE wxTreeTextCtrl; 31 class WXDLLIMPEXP_FWD_CORE wxTextCtrl; 32 33 // ----------------------------------------------------------------------------- 34 // wxGenericTreeCtrl - the tree control 35 // ----------------------------------------------------------------------------- 36 37 class WXDLLIMPEXP_CORE wxGenericTreeCtrl : public wxTreeCtrlBase, 38 public wxScrollHelper 39 { 40 public: 41 // creation 42 // -------- 43 wxGenericTreeCtrl()44 wxGenericTreeCtrl() : wxTreeCtrlBase(), wxScrollHelper(this) { Init(); } 45 46 wxGenericTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY, 47 const wxPoint& pos = wxDefaultPosition, 48 const wxSize& size = wxDefaultSize, 49 long style = wxTR_DEFAULT_STYLE, 50 const wxValidator &validator = wxDefaultValidator, 51 const wxString& name = wxASCII_STR(wxTreeCtrlNameStr)) wxTreeCtrlBase()52 : wxTreeCtrlBase(), 53 wxScrollHelper(this) 54 { 55 Init(); 56 Create(parent, id, pos, size, style, validator, name); 57 } 58 59 virtual ~wxGenericTreeCtrl(); 60 61 bool Create(wxWindow *parent, wxWindowID id = wxID_ANY, 62 const wxPoint& pos = wxDefaultPosition, 63 const wxSize& size = wxDefaultSize, 64 long style = wxTR_DEFAULT_STYLE, 65 const wxValidator &validator = wxDefaultValidator, 66 const wxString& name = wxASCII_STR(wxTreeCtrlNameStr)); 67 68 // implement base class pure virtuals 69 // ---------------------------------- 70 71 virtual unsigned int GetCount() const wxOVERRIDE; 72 GetIndent()73 virtual unsigned int GetIndent() const wxOVERRIDE { return m_indent; } 74 virtual void SetIndent(unsigned int indent) wxOVERRIDE; 75 76 77 virtual void SetImageList(wxImageList *imageList) wxOVERRIDE; 78 virtual void SetStateImageList(wxImageList *imageList) wxOVERRIDE; 79 80 virtual wxString GetItemText(const wxTreeItemId& item) const wxOVERRIDE; 81 virtual int GetItemImage(const wxTreeItemId& item, 82 wxTreeItemIcon which = wxTreeItemIcon_Normal) const wxOVERRIDE; 83 virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const wxOVERRIDE; 84 virtual wxColour GetItemTextColour(const wxTreeItemId& item) const wxOVERRIDE; 85 virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const wxOVERRIDE; 86 virtual wxFont GetItemFont(const wxTreeItemId& item) const wxOVERRIDE; 87 88 virtual void SetItemText(const wxTreeItemId& item, const wxString& text) wxOVERRIDE; 89 virtual void SetItemImage(const wxTreeItemId& item, 90 int image, 91 wxTreeItemIcon which = wxTreeItemIcon_Normal) wxOVERRIDE; 92 virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data) wxOVERRIDE; 93 94 virtual void SetItemHasChildren(const wxTreeItemId& item, bool has = true) wxOVERRIDE; 95 virtual void SetItemBold(const wxTreeItemId& item, bool bold = true) wxOVERRIDE; 96 virtual void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = true) wxOVERRIDE; 97 virtual void SetItemTextColour(const wxTreeItemId& item, const wxColour& col) wxOVERRIDE; 98 virtual void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col) wxOVERRIDE; 99 virtual void SetItemFont(const wxTreeItemId& item, const wxFont& font) wxOVERRIDE; 100 101 virtual bool IsVisible(const wxTreeItemId& item) const wxOVERRIDE; 102 virtual bool ItemHasChildren(const wxTreeItemId& item) const wxOVERRIDE; 103 virtual bool IsExpanded(const wxTreeItemId& item) const wxOVERRIDE; 104 virtual bool IsSelected(const wxTreeItemId& item) const wxOVERRIDE; 105 virtual bool IsBold(const wxTreeItemId& item) const wxOVERRIDE; 106 107 virtual size_t GetChildrenCount(const wxTreeItemId& item, 108 bool recursively = true) const wxOVERRIDE; 109 110 // navigation 111 // ---------- 112 GetRootItem()113 virtual wxTreeItemId GetRootItem() const wxOVERRIDE { return m_anchor; } GetSelection()114 virtual wxTreeItemId GetSelection() const wxOVERRIDE 115 { 116 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE), 117 wxT("must use GetSelections() with this control") ); 118 119 return m_current; 120 } 121 virtual size_t GetSelections(wxArrayTreeItemIds&) const wxOVERRIDE; GetFocusedItem()122 virtual wxTreeItemId GetFocusedItem() const wxOVERRIDE { return m_current; } 123 124 virtual void ClearFocusedItem() wxOVERRIDE; 125 virtual void SetFocusedItem(const wxTreeItemId& item) wxOVERRIDE; 126 127 virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const wxOVERRIDE; 128 virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item, 129 wxTreeItemIdValue& cookie) const wxOVERRIDE; 130 virtual wxTreeItemId GetNextChild(const wxTreeItemId& item, 131 wxTreeItemIdValue& cookie) const wxOVERRIDE; 132 virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const wxOVERRIDE; 133 virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const wxOVERRIDE; 134 virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const wxOVERRIDE; 135 136 virtual wxTreeItemId GetFirstVisibleItem() const wxOVERRIDE; 137 virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const wxOVERRIDE; 138 virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const wxOVERRIDE; 139 140 141 // operations 142 // ---------- 143 144 virtual wxTreeItemId AddRoot(const wxString& text, 145 int image = -1, int selectedImage = -1, 146 wxTreeItemData *data = NULL) wxOVERRIDE; 147 148 virtual void Delete(const wxTreeItemId& item) wxOVERRIDE; 149 virtual void DeleteChildren(const wxTreeItemId& item) wxOVERRIDE; 150 virtual void DeleteAllItems() wxOVERRIDE; 151 152 virtual void Expand(const wxTreeItemId& item) wxOVERRIDE; 153 virtual void Collapse(const wxTreeItemId& item) wxOVERRIDE; 154 virtual void CollapseAndReset(const wxTreeItemId& item) wxOVERRIDE; 155 virtual void Toggle(const wxTreeItemId& item) wxOVERRIDE; 156 157 virtual void Unselect() wxOVERRIDE; 158 virtual void UnselectAll() wxOVERRIDE; 159 virtual void SelectItem(const wxTreeItemId& item, bool select = true) wxOVERRIDE; 160 virtual void SelectChildren(const wxTreeItemId& parent) wxOVERRIDE; 161 162 virtual void EnsureVisible(const wxTreeItemId& item) wxOVERRIDE; 163 virtual void ScrollTo(const wxTreeItemId& item) wxOVERRIDE; 164 165 virtual wxTextCtrl *EditLabel(const wxTreeItemId& item, 166 wxClassInfo* textCtrlClass = wxCLASSINFO(wxTextCtrl)) wxOVERRIDE; 167 virtual wxTextCtrl *GetEditControl() const wxOVERRIDE; 168 virtual void EndEditLabel(const wxTreeItemId& item, 169 bool discardChanges = false) wxOVERRIDE; 170 171 virtual void EnableBellOnNoMatch(bool on = true) wxOVERRIDE; 172 173 virtual void SortChildren(const wxTreeItemId& item) wxOVERRIDE; 174 175 // items geometry 176 // -------------- 177 178 virtual bool GetBoundingRect(const wxTreeItemId& item, 179 wxRect& rect, 180 bool textOnly = false) const wxOVERRIDE; 181 182 183 // this version specific methods 184 // ----------------------------- 185 GetButtonsImageList()186 wxImageList *GetButtonsImageList() const { return m_imageListButtons; } 187 void SetButtonsImageList(wxImageList *imageList); 188 void AssignButtonsImageList(wxImageList *imageList); 189 190 void SetDropEffectAboveItem( bool above = false ) { m_dropEffectAboveItem = above; } GetDropEffectAboveItem()191 bool GetDropEffectAboveItem() const { return m_dropEffectAboveItem; } 192 193 wxTreeItemId GetNext(const wxTreeItemId& item) const; 194 195 // implementation only from now on 196 197 // overridden base class virtuals 198 virtual bool SetBackgroundColour(const wxColour& colour) wxOVERRIDE; 199 virtual bool SetForegroundColour(const wxColour& colour) wxOVERRIDE; 200 201 virtual void Refresh(bool eraseBackground = true, const wxRect *rect = NULL) wxOVERRIDE; 202 203 virtual bool SetFont( const wxFont &font ) wxOVERRIDE; 204 virtual void SetWindowStyleFlag(long styles) wxOVERRIDE; 205 206 // callbacks 207 void OnPaint( wxPaintEvent &event ); 208 void OnSetFocus( wxFocusEvent &event ); 209 void OnKillFocus( wxFocusEvent &event ); 210 void OnKeyDown( wxKeyEvent &event ); 211 void OnChar( wxKeyEvent &event ); 212 void OnMouse( wxMouseEvent &event ); 213 void OnGetToolTip( wxTreeEvent &event ); 214 void OnSize( wxSizeEvent &event ); 215 void OnInternalIdle( ) wxOVERRIDE; 216 GetDefaultAttributes()217 virtual wxVisualAttributes GetDefaultAttributes() const wxOVERRIDE 218 { 219 return GetClassDefaultAttributes(GetWindowVariant()); 220 } 221 222 static wxVisualAttributes 223 GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL); 224 225 // implementation helpers 226 void AdjustMyScrollbars(); 227 228 WX_FORWARD_TO_SCROLL_HELPER() 229 230 protected: 231 friend class wxGenericTreeItem; 232 friend class wxTreeRenameTimer; 233 friend class wxTreeFindTimer; 234 friend class wxTreeTextCtrl; 235 236 wxFont m_normalFont; 237 wxFont m_boldFont; 238 239 wxGenericTreeItem *m_anchor; 240 wxGenericTreeItem *m_current, 241 *m_key_current, 242 // A hint to select a parent item after deleting a child 243 *m_select_me; 244 unsigned short m_indent; 245 int m_lineHeight; 246 wxPen m_dottedPen; 247 wxBrush m_hilightBrush, 248 m_hilightUnfocusedBrush; 249 bool m_hasFocus; 250 bool m_dirty; 251 bool m_ownsImageListButtons; 252 bool m_isDragging; // true between BEGIN/END drag events 253 bool m_lastOnSame; // last click on the same item as prev 254 wxImageList *m_imageListButtons; 255 256 int m_dragCount; 257 wxPoint m_dragStart; 258 wxGenericTreeItem *m_dropTarget; 259 wxCursor m_oldCursor; // cursor is changed while dragging 260 wxGenericTreeItem *m_oldSelection; 261 wxGenericTreeItem *m_underMouse; // for visual effects 262 263 enum { NoEffect, BorderEffect, AboveEffect, BelowEffect } m_dndEffect; 264 wxGenericTreeItem *m_dndEffectItem; 265 266 wxTreeTextCtrl *m_textCtrl; 267 268 269 wxTimer *m_renameTimer; 270 271 // incremental search data 272 wxString m_findPrefix; 273 wxTimer *m_findTimer; 274 // This flag is set to 0 if the bell is disabled, 1 if it is enabled and -1 275 // if it is globally enabled but has been temporarily disabled because we 276 // had already beeped for this particular search. 277 int m_findBell; 278 279 bool m_dropEffectAboveItem; 280 281 // the common part of all ctors 282 void Init(); 283 284 // overridden wxWindow methods 285 virtual void DoThaw() wxOVERRIDE; 286 287 // misc helpers 288 void SendDeleteEvent(wxGenericTreeItem *itemBeingDeleted); 289 290 void DrawBorder(const wxTreeItemId& item); 291 void DrawLine(const wxTreeItemId& item, bool below); 292 void DrawDropEffect(wxGenericTreeItem *item); 293 294 void DoSelectItem(const wxTreeItemId& id, 295 bool unselect_others = true, 296 bool extended_select = false); 297 298 virtual int DoGetItemState(const wxTreeItemId& item) const wxOVERRIDE; 299 virtual void DoSetItemState(const wxTreeItemId& item, int state) wxOVERRIDE; 300 301 virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent, 302 size_t previous, 303 const wxString& text, 304 int image, 305 int selectedImage, 306 wxTreeItemData *data) wxOVERRIDE; 307 virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent, 308 const wxTreeItemId& idPrevious, 309 const wxString& text, 310 int image = -1, int selImage = -1, 311 wxTreeItemData *data = NULL) wxOVERRIDE; 312 virtual wxTreeItemId DoTreeHitTest(const wxPoint& point, int& flags) const wxOVERRIDE; 313 314 // called by wxTextTreeCtrl when it marks itself for deletion 315 void ResetTextControl(); 316 317 // find the first item starting with the given prefix after the given item 318 wxTreeItemId FindItem(const wxTreeItemId& id, const wxString& prefix) const; 319 HasButtons()320 bool HasButtons() const { return HasFlag(wxTR_HAS_BUTTONS); } 321 322 void CalculateLineHeight(); 323 int GetLineHeight(wxGenericTreeItem *item) const; 324 void PaintLevel( wxGenericTreeItem *item, wxDC& dc, int level, int &y ); 325 void PaintItem( wxGenericTreeItem *item, wxDC& dc); 326 327 void CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ); 328 void CalculatePositions(); 329 330 void RefreshSubtree( wxGenericTreeItem *item ); 331 void RefreshLine( wxGenericTreeItem *item ); 332 333 // redraw all selected items 334 void RefreshSelected(); 335 336 // RefreshSelected() recursive helper 337 void RefreshSelectedUnder(wxGenericTreeItem *item); 338 339 void OnRenameTimer(); 340 bool OnRenameAccept(wxGenericTreeItem *item, const wxString& value); 341 void OnRenameCancelled(wxGenericTreeItem *item); 342 343 void FillArray(wxGenericTreeItem*, wxArrayTreeItemIds&) const; 344 void SelectItemRange( wxGenericTreeItem *item1, wxGenericTreeItem *item2 ); 345 bool TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select); 346 bool TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select); 347 void UnselectAllChildren( wxGenericTreeItem *item ); 348 void ChildrenClosing(wxGenericTreeItem* item); 349 350 void DoDirtyProcessing(); 351 352 virtual wxSize DoGetBestSize() const wxOVERRIDE; 353 354 private: OnSysColourChanged(wxSysColourChangedEvent & WXUNUSED (event))355 void OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) 356 { 357 InitVisualAttributes(); 358 } 359 360 // (Re)initialize colours, fonts, pens, brushes used by the control using 361 // the current system colours and font. 362 void InitVisualAttributes(); 363 364 // Reset the state of the last find (i.e. keyboard incremental search) 365 // operation. 366 void ResetFindState(); 367 368 369 // True if we're using custom colours/font, respectively, or false if we're 370 // using the default colours and should update them whenever system colours 371 // change. 372 bool m_hasExplicitFgCol:1, 373 m_hasExplicitBgCol:1, 374 m_hasExplicitFont:1; 375 376 wxDECLARE_EVENT_TABLE(); 377 wxDECLARE_DYNAMIC_CLASS(wxGenericTreeCtrl); 378 wxDECLARE_NO_COPY_CLASS(wxGenericTreeCtrl); 379 }; 380 381 // Also define wxTreeCtrl to be wxGenericTreeCtrl on all platforms without a 382 // native version, i.e. all but MSW and Qt. 383 #if !(defined(__WXMSW__) || defined(__WXQT__)) || defined(__WXUNIVERSAL__) 384 /* 385 * wxTreeCtrl has to be a real class or we have problems with 386 * the run-time information. 387 */ 388 389 class WXDLLIMPEXP_CORE wxTreeCtrl: public wxGenericTreeCtrl 390 { 391 wxDECLARE_DYNAMIC_CLASS(wxTreeCtrl); 392 393 public: wxTreeCtrl()394 wxTreeCtrl() {} 395 396 wxTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY, 397 const wxPoint& pos = wxDefaultPosition, 398 const wxSize& size = wxDefaultSize, 399 long style = wxTR_DEFAULT_STYLE, 400 const wxValidator &validator = wxDefaultValidator, 401 const wxString& name = wxASCII_STR(wxTreeCtrlNameStr)) wxGenericTreeCtrl(parent,id,pos,size,style,validator,name)402 : wxGenericTreeCtrl(parent, id, pos, size, style, validator, name) 403 { 404 } 405 }; 406 #endif // !(__WXMSW__ || __WXQT__) || __WXUNIVERSAL__ 407 408 #endif // wxUSE_TREECTRL 409 410 #endif // _GENERIC_TREECTRL_H_ 411