]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/treectlg.h
fixing NSFontDescriptor matching for font weights and styles, fixes #11910
[wxWidgets.git] / include / wx / generic / treectlg.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/treectlg.h
3 // Purpose: wxTreeCtrl class
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997,1998 Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _GENERIC_TREECTRL_H_
13 #define _GENERIC_TREECTRL_H_
14
15 #if wxUSE_TREECTRL
16
17 #include "wx/scrolwin.h"
18 #include "wx/pen.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
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 = wxTreeCtrlNameStr)
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 = wxTreeCtrlNameStr);
67
68
69 // implement base class pure virtuals
70 // ----------------------------------
71
72 virtual unsigned int GetCount() const;
73
74 virtual unsigned int GetIndent() const { return m_indent; }
75 virtual void SetIndent(unsigned int indent);
76
77
78 virtual void SetImageList(wxImageList *imageList);
79 virtual void SetStateImageList(wxImageList *imageList);
80
81 virtual wxString GetItemText(const wxTreeItemId& item) const;
82 virtual int GetItemImage(const wxTreeItemId& item,
83 wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
84 virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
85 virtual wxColour GetItemTextColour(const wxTreeItemId& item) const;
86 virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
87 virtual wxFont GetItemFont(const wxTreeItemId& item) const;
88
89 virtual void SetItemText(const wxTreeItemId& item, const wxString& text);
90 virtual void SetItemImage(const wxTreeItemId& item,
91 int image,
92 wxTreeItemIcon which = wxTreeItemIcon_Normal);
93 virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
94
95 virtual void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
96 virtual void SetItemBold(const wxTreeItemId& item, bool bold = true);
97 virtual void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = true);
98 virtual void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
99 virtual void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
100 virtual void SetItemFont(const wxTreeItemId& item, const wxFont& font);
101
102 virtual bool IsVisible(const wxTreeItemId& item) const;
103 virtual bool ItemHasChildren(const wxTreeItemId& item) const;
104 virtual bool IsExpanded(const wxTreeItemId& item) const;
105 virtual bool IsSelected(const wxTreeItemId& item) const;
106 virtual bool IsBold(const wxTreeItemId& item) const;
107
108 virtual size_t GetChildrenCount(const wxTreeItemId& item,
109 bool recursively = true) const;
110
111 // navigation
112 // ----------
113
114 virtual wxTreeItemId GetRootItem() const { return m_anchor; }
115 virtual wxTreeItemId GetSelection() const
116 {
117 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE),
118 wxT("must use GetSelections() with this control") );
119
120 return m_current;
121 }
122 virtual size_t GetSelections(wxArrayTreeItemIds&) const;
123 virtual wxTreeItemId GetFocusedItem() const { return m_current; }
124
125 virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
126 virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item,
127 wxTreeItemIdValue& cookie) const;
128 virtual wxTreeItemId GetNextChild(const wxTreeItemId& item,
129 wxTreeItemIdValue& cookie) const;
130 virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
131 virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
132 virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
133
134 virtual wxTreeItemId GetFirstVisibleItem() const;
135 virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
136 virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
137
138
139 // operations
140 // ----------
141
142 virtual wxTreeItemId AddRoot(const wxString& text,
143 int image = -1, int selectedImage = -1,
144 wxTreeItemData *data = NULL);
145
146 virtual void Delete(const wxTreeItemId& item);
147 virtual void DeleteChildren(const wxTreeItemId& item);
148 virtual void DeleteAllItems();
149
150 virtual void Expand(const wxTreeItemId& item);
151 virtual void Collapse(const wxTreeItemId& item);
152 virtual void CollapseAndReset(const wxTreeItemId& item);
153 virtual void Toggle(const wxTreeItemId& item);
154
155 virtual void Unselect();
156 virtual void UnselectAll();
157 virtual void SelectItem(const wxTreeItemId& item, bool select = true);
158 virtual void SelectChildren(const wxTreeItemId& parent);
159
160 virtual void EnsureVisible(const wxTreeItemId& item);
161 virtual void ScrollTo(const wxTreeItemId& item);
162
163 virtual wxTextCtrl *EditLabel(const wxTreeItemId& item,
164 wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
165 virtual wxTextCtrl *GetEditControl() const;
166 virtual void EndEditLabel(const wxTreeItemId& item,
167 bool discardChanges = false);
168
169 virtual void SortChildren(const wxTreeItemId& item);
170
171 // items geometry
172 // --------------
173
174 virtual bool GetBoundingRect(const wxTreeItemId& item,
175 wxRect& rect,
176 bool textOnly = false) const;
177
178
179 // this version specific methods
180 // -----------------------------
181
182 wxImageList *GetButtonsImageList() const { return m_imageListButtons; }
183 void SetButtonsImageList(wxImageList *imageList);
184 void AssignButtonsImageList(wxImageList *imageList);
185
186 void SetDropEffectAboveItem( bool above = false ) { m_dropEffectAboveItem = above; }
187 bool GetDropEffectAboveItem() const { return m_dropEffectAboveItem; }
188
189 wxTreeItemId GetNext(const wxTreeItemId& item) const;
190
191 #if WXWIN_COMPATIBILITY_2_6
192 // use EditLabel() instead
193 void Edit( const wxTreeItemId& item ) { EditLabel(item); }
194 #endif // WXWIN_COMPATIBILITY_2_6
195
196 // implementation only from now on
197
198 // overridden base class virtuals
199 virtual bool SetBackgroundColour(const wxColour& colour);
200 virtual bool SetForegroundColour(const wxColour& colour);
201
202 virtual void Refresh(bool eraseBackground = true, const wxRect *rect = NULL);
203
204 virtual bool SetFont( const wxFont &font );
205 virtual void SetWindowStyle(const long styles);
206
207 // callbacks
208 void OnPaint( wxPaintEvent &event );
209 void OnSetFocus( wxFocusEvent &event );
210 void OnKillFocus( wxFocusEvent &event );
211 void OnChar( wxKeyEvent &event );
212 void OnMouse( wxMouseEvent &event );
213 void OnGetToolTip( wxTreeEvent &event );
214 void OnSize( wxSizeEvent &event );
215 void OnInternalIdle( );
216
217 virtual wxVisualAttributes GetDefaultAttributes() const
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
275 bool m_dropEffectAboveItem;
276
277 // the common part of all ctors
278 void Init();
279
280 // overridden wxWindow methods
281 virtual void DoThaw();
282
283 // misc helpers
284 void SendDeleteEvent(wxGenericTreeItem *itemBeingDeleted);
285
286 void DrawBorder(const wxTreeItemId& item);
287 void DrawLine(const wxTreeItemId& item, bool below);
288 void DrawDropEffect(wxGenericTreeItem *item);
289
290 void DoSelectItem(const wxTreeItemId& id,
291 bool unselect_others = true,
292 bool extended_select = false);
293
294 virtual int DoGetItemState(const wxTreeItemId& item) const;
295 virtual void DoSetItemState(const wxTreeItemId& item, int state);
296
297 virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
298 size_t previous,
299 const wxString& text,
300 int image,
301 int selectedImage,
302 wxTreeItemData *data);
303 virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent,
304 const wxTreeItemId& idPrevious,
305 const wxString& text,
306 int image = -1, int selImage = -1,
307 wxTreeItemData *data = NULL);
308 virtual wxTreeItemId DoTreeHitTest(const wxPoint& point, int& flags) const;
309
310 // called by wxTextTreeCtrl when it marks itself for deletion
311 void ResetTextControl();
312
313 // find the first item starting with the given prefix after the given item
314 wxTreeItemId FindItem(const wxTreeItemId& id, const wxString& prefix) const;
315
316 bool HasButtons() const { return HasFlag(wxTR_HAS_BUTTONS); }
317
318 void CalculateLineHeight();
319 int GetLineHeight(wxGenericTreeItem *item) const;
320 void PaintLevel( wxGenericTreeItem *item, wxDC& dc, int level, int &y );
321 void PaintItem( wxGenericTreeItem *item, wxDC& dc);
322
323 void CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y );
324 void CalculatePositions();
325
326 void RefreshSubtree( wxGenericTreeItem *item );
327 void RefreshLine( wxGenericTreeItem *item );
328
329 // redraw all selected items
330 void RefreshSelected();
331
332 // RefreshSelected() recursive helper
333 void RefreshSelectedUnder(wxGenericTreeItem *item);
334
335 void OnRenameTimer();
336 bool OnRenameAccept(wxGenericTreeItem *item, const wxString& value);
337 void OnRenameCancelled(wxGenericTreeItem *item);
338
339 void FillArray(wxGenericTreeItem*, wxArrayTreeItemIds&) const;
340 void SelectItemRange( wxGenericTreeItem *item1, wxGenericTreeItem *item2 );
341 bool TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select);
342 bool TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select);
343 void UnselectAllChildren( wxGenericTreeItem *item );
344 void ChildrenClosing(wxGenericTreeItem* item);
345
346 void DoDirtyProcessing();
347
348 virtual wxSize DoGetBestSize() const;
349
350 private:
351 DECLARE_EVENT_TABLE()
352 DECLARE_DYNAMIC_CLASS(wxGenericTreeCtrl)
353 wxDECLARE_NO_COPY_CLASS(wxGenericTreeCtrl);
354 };
355
356 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
357 /*
358 * wxTreeCtrl has to be a real class or we have problems with
359 * the run-time information.
360 */
361
362 class WXDLLIMPEXP_CORE wxTreeCtrl: public wxGenericTreeCtrl
363 {
364 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
365
366 public:
367 wxTreeCtrl() {}
368
369 wxTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY,
370 const wxPoint& pos = wxDefaultPosition,
371 const wxSize& size = wxDefaultSize,
372 long style = wxTR_DEFAULT_STYLE,
373 const wxValidator &validator = wxDefaultValidator,
374 const wxString& name = wxTreeCtrlNameStr)
375 : wxGenericTreeCtrl(parent, id, pos, size, style, validator, name)
376 {
377 }
378 };
379 #endif // !__WXMSW__ || __WXUNIVERSAL__
380
381 #endif // wxUSE_TREECTRL
382
383 #endif // _GENERIC_TREECTRL_H_