]> git.saurik.com Git - wxWidgets.git/blob - include/wx/os2/treectrl.h
d7f7db8bdb2bb66ccc4e8dcd02ce2feb969b6f93
[wxWidgets.git] / include / wx / os2 / treectrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/os2/treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: David Webster
5 // Modified by:
6 // Created: 01/23/03
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #ifdef __GNUG__
20 #pragma interface "treectrl.h"
21 #endif
22
23 #if wxUSE_TREECTRL
24
25 #include "wx/textctrl.h"
26 #include "wx/dynarray.h"
27 #include "wx/treebase.h"
28 #include "wx/hashmap.h"
29
30 // the type for "untyped" data
31 typedef long wxDataType;
32
33 // fwd decl
34 class WXDLLEXPORT wxImageList;
35 class WXDLLEXPORT wxDragImage;
36 struct WXDLLEXPORT wxTreeViewItem;
37
38 // a callback function used for sorting tree items, it should return -1 if the
39 // first item precedes the second, +1 if the second precedes the first or 0 if
40 // they're equivalent
41 class wxTreeItemData;
42
43 // flags for deprecated `Expand(int action)'
44 enum
45 {
46 wxTREE_EXPAND_EXPAND,
47 wxTREE_EXPAND_COLLAPSE,
48 wxTREE_EXPAND_COLLAPSE_RESET,
49 wxTREE_EXPAND_TOGGLE
50 };
51
52 // flags for deprecated InsertItem() variant
53 #define wxTREE_INSERT_FIRST 0xFFFF0001
54 #define wxTREE_INSERT_LAST 0xFFFF0002
55
56 // hash storing attributes for our items
57 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr *, wxMapTreeAttr);
58
59 // ----------------------------------------------------------------------------
60 // wxTreeCtrl
61 // ----------------------------------------------------------------------------
62 class WXDLLEXPORT wxTreeCtrl : public wxControl
63 {
64 public:
65 // creation
66 // --------
67 wxTreeCtrl() { Init(); }
68
69 wxTreeCtrl( wxWindow* pParent
70 ,wxWindowID vId = -1
71 ,const wxPoint& rPos = wxDefaultPosition
72 ,const wxSize& rSize = wxDefaultSize
73 ,long lStyle = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT
74 ,const wxValidator& rValidator = wxDefaultValidator
75 ,const wxString& rsName = wxTreeCtrlNameStr
76 )
77 {
78 Create( pParent
79 ,vId
80 ,rPos
81 ,rSize
82 ,lStyle
83 ,rValidator
84 ,rsName
85 );
86 }
87 virtual ~wxTreeCtrl();
88
89 bool Create( wxWindow* pParent
90 ,wxWindowID vId = -1
91 ,const wxPoint& rPos = wxDefaultPosition
92 ,const wxSize& rSize = wxDefaultSize
93 ,long lStyle = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT
94 ,const wxValidator& rValidator = wxDefaultValidator
95 ,const wxString& rsName = wxTreeCtrlNameStr
96 );
97
98 //
99 // Accessors
100 // ---------
101 //
102
103 //
104 // Get the total number of items in the control
105 //
106 size_t GetCount(void) const;
107
108 //
109 // Indent is the number of pixels the children are indented relative to
110 // the parents position. SetIndent() also redraws the control
111 // immediately.
112 //
113 unsigned int GetIndent(void) const;
114 void SetIndent(unsigned int uIndent);
115
116 //
117 // Spacing is the number of pixels between the start and the Text
118 //
119 unsigned int GetSpacing(void) const { return 18; } // return wxGTK default
120 void SetSpacing(unsigned int uSpacing) { }
121
122 //
123 // Image list: these functions allow to associate an image list with
124 // the control and retrieve it. Note that the control does _not_ delete
125 // the associated image list when it's deleted in order to allow image
126 // lists to be shared between different controls.
127 //
128 // OS/2 doesn't really use imagelists as MSW does, but since the MSW
129 // control is the basis for this one, until I decide how to get rid of
130 // the need for them they are here for now.
131 //
132 wxImageList* GetImageList(void) const;
133 wxImageList* GetStateImageList(void) const;
134
135 void AssignImageList(wxImageList* pImageList);
136 void AssignStateImageList(wxImageList* pImageList);
137 void SetImageList(wxImageList* pImageList);
138 void SetStateImageList(wxImageList* pImageList);
139
140 //
141 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
142 // member functions of wxTreeItem because they must know the tree the item
143 // belongs to for Windows implementation and storing the pointer to
144 // wxTreeCtrl in each wxTreeItem is just too much waste.
145
146 //
147 // Item's label
148 //
149 wxString GetItemText(const wxTreeItemId& rItem) const;
150 void SetItemText( const wxTreeItemId& rItem
151 ,const wxString& rsText
152 );
153
154 //
155 // One of the images associated with the item (normal by default)
156 //
157 int GetItemImage( const wxTreeItemId& rItem
158 ,wxTreeItemIcon vWhich = wxTreeItemIcon_Normal
159 ) const;
160 void SetItemImage( const wxTreeItemId& rItem
161 ,int nImage
162 ,wxTreeItemIcon vWhich = wxTreeItemIcon_Normal
163 );
164
165 //
166 // Data associated with the item
167 //
168 wxTreeItemData* GetItemData(const wxTreeItemId& rItem) const;
169 void SetItemData( const wxTreeItemId& rItem
170 ,wxTreeItemData* pData
171 );
172
173 //
174 // Item's text colour
175 //
176 wxColour GetItemTextColour(const wxTreeItemId& rItem) const;
177 void SetItemTextColour( const wxTreeItemId& rItem
178 ,const wxColour& rColor
179 );
180
181 //
182 // Item's background colour
183 //
184 wxColour GetItemBackgroundColour(const wxTreeItemId& rItem) const;
185 void SetItemBackgroundColour( const wxTreeItemId& rItem
186 ,const wxColour& rColour
187 );
188
189 //
190 // Item's font
191 //
192 wxFont GetItemFont(const wxTreeItemId& rItem) const;
193 void SetItemFont( const wxTreeItemId& rItem
194 ,const wxFont& rFont
195 );
196
197 //
198 // Force appearance of [+] button near the item. This is useful to
199 // allow the user to expand the items which don't have any children now
200 // - but instead add them only when needed, thus minimizing memory
201 // usage and loading time.
202 //
203 void SetItemHasChildren( const wxTreeItemId& rItem
204 ,bool bHas = true
205 );
206
207 //
208 // The item will be shown in bold
209 //
210 void SetItemBold( const wxTreeItemId& rItem
211 ,bool bBold = true
212 );
213
214 //
215 // The item will be shown with a drop highlight
216 //
217 void SetItemDropHighlight( const wxTreeItemId& rItem
218 ,bool bHighlight = true
219 );
220
221 //
222 // Item status inquiries
223 // ---------------------
224 //
225
226 //
227 // Is the item visible (it might be outside the view or not expanded)?
228 //
229 bool IsVisible(const wxTreeItemId& rItem) const;
230
231 //
232 // Does the item has any children?
233 //
234 bool ItemHasChildren(const wxTreeItemId& rItem) const;
235
236 //
237 // Is the item expanded (only makes sense if HasChildren())?
238 //
239 bool IsExpanded(const wxTreeItemId& rItem) const;
240
241 //
242 // Is this item currently selected (the same as has focus)?
243 //
244 bool IsSelected(const wxTreeItemId& rItem) const;
245
246 //
247 // Is item text in bold font?
248 //
249 bool IsBold(const wxTreeItemId& rItem) const;
250
251 //
252 // Number of children
253 // ------------------
254 //
255
256 //
257 // If 'bRecursively' is false, only immediate children count, otherwise
258 // the returned number is the number of all items in this branch
259 //
260 size_t GetChildrenCount( const wxTreeItemId& rItem
261 ,bool bRecursively = true
262 ) const;
263
264 //
265 // Navigation
266 // ----------
267 //
268
269 //
270 // Get the root tree item
271 //
272 wxTreeItemId GetRootItem(void) const;
273
274 //
275 // Get the item currently selected (may return NULL if no selection)
276 //
277 wxTreeItemId GetSelection(void) const;
278
279 //
280 // Get the items currently selected, return the number of such item
281 //
282 size_t GetSelections(wxArrayTreeItemIds& rSelections) const;
283
284 //
285 // Get the parent of this item (may return NULL if root)
286 //
287 wxTreeItemId GetItemParent(const wxTreeItemId& rItem) const;
288
289 #if WXWIN_COMPATIBILITY_2_2
290 // deprecated: Use GetItemParent instead.
291 wxDEPRECATED( wxTreeItemId GetParent(const wxTreeItemId& item) const);
292
293 // Expose the base class method hidden by the one above. Not deprecatable.
294 wxWindow *GetParent() const { return wxControl::GetParent(); }
295 #endif // WXWIN_COMPATIBILITY_2_2
296
297 // for this enumeration function you must pass in a "cookie" parameter
298 // which is opaque for the application but is necessary for the library
299 // to make these functions reentrant (i.e. allow more than one
300 // enumeration on one and the same object simultaneously). Of course,
301 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
302 // the same!
303
304 // get the first child of this item
305 wxTreeItemId GetFirstChild(const wxTreeItemId& item,
306 wxTreeItemIdValue& cookie) const;
307 // get the next child
308 wxTreeItemId GetNextChild(const wxTreeItemId& item,
309 wxTreeItemIdValue& cookie) const;
310
311 //
312 // Get the last child of this item - this method doesn't use cookies
313 //
314 wxTreeItemId GetLastChild(const wxTreeItemId& rItem) const;
315
316 //
317 // Get the next sibling of this item
318 //
319 wxTreeItemId GetNextSibling(const wxTreeItemId& rItem) const;
320
321 //
322 // Get the previous sibling
323 //
324 wxTreeItemId GetPrevSibling(const wxTreeItemId& rItem) const;
325
326 //
327 // Get first visible item
328 //
329 wxTreeItemId GetFirstVisibleItem(void) const;
330
331 //
332 // Get the next visible item: item must be visible itself!
333 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
334 //
335 wxTreeItemId GetNextVisible(const wxTreeItemId& rItem) const;
336
337 //
338 // Get the previous visible item: item must be visible itself!
339 //
340 wxTreeItemId GetPrevVisible(const wxTreeItemId& rItem) const;
341
342 //
343 // Operations
344 // ----------
345 //
346
347 //
348 // Add the root node to the tree
349 //
350 wxTreeItemId AddRoot( const wxString& rsText
351 ,int nImage = -1
352 ,int nSelectedImage = -1
353 ,wxTreeItemData* pData = NULL
354 );
355
356 //
357 // Insert a new item in as the first child of the parent
358 //
359 wxTreeItemId PrependItem( const wxTreeItemId& rParent
360 ,const wxString& rsText
361 ,int nImage = -1
362 ,int nSelectedImage = -1
363 ,wxTreeItemData* pData = NULL
364 );
365
366 //
367 // Insert a new item after a given one
368 //
369 wxTreeItemId InsertItem( const wxTreeItemId& rParent
370 ,const wxTreeItemId& rIdPrevious
371 ,const wxString& rsText
372 ,int nImage = -1
373 ,int nSelectedImage = -1
374 ,wxTreeItemData* pData = NULL
375 );
376
377 //
378 // Insert a new item before the one with the given index
379 //
380 wxTreeItemId InsertItem( const wxTreeItemId& pParent
381 ,size_t nIndex
382 ,const wxString& rsText
383 ,int nImage = -1
384 ,int nSelectedImage = -1
385 ,wxTreeItemData* pData = NULL
386 );
387
388 //
389 // Insert a new item in as the last child of the parent
390 //
391 wxTreeItemId AppendItem( const wxTreeItemId& rParent
392 ,const wxString& rsText
393 ,int nImage = -1
394 ,int nSelectedImage = -1
395 ,wxTreeItemData* pData = NULL
396 );
397
398 //
399 // Delete this item and associated data if any
400 //
401 void Delete(const wxTreeItemId& rItem);
402
403 //
404 // Delete all children (but don't delete the item itself)
405 //
406 void DeleteChildren(const wxTreeItemId& rItem);
407
408 //
409 // Delete all items from the tree
410 //
411 void DeleteAllItems(void);
412
413 //
414 // Expand this item
415 //
416 void Expand(const wxTreeItemId& rItem);
417
418 //
419 // Collapse the item without removing its children
420 //
421 void Collapse(const wxTreeItemId& rItem);
422
423 //
424 // Collapse the item and remove all children
425 //
426 void CollapseAndReset(const wxTreeItemId& rItem);
427
428 //
429 // Toggles the current state
430 //
431 void Toggle(const wxTreeItemId& rItem);
432
433 //
434 // Remove the selection from currently selected item (if any)
435 //
436 void Unselect(void);
437
438 //
439 // Unselect all items (only makes sense for multiple selection control)
440 //
441 void UnselectAll(void);
442
443 //
444 // Select this item
445 //
446 void SelectItem(const wxTreeItemId& rItem);
447
448 //
449 // Make sure this item is visible (expanding the parent item and/or
450 // scrolling to this item if necessary)
451 //
452 void EnsureVisible(const wxTreeItemId& rItem);
453
454 //
455 // Scroll to this item (but don't expand its parent)
456 //
457 void ScrollTo(const wxTreeItemId& rItem);
458
459 //
460 // OS/2 does not use a separate edit field for editting text. Here for
461 // interface compatibility, only.
462 //
463 wxTextCtrl* EditLabel( const wxTreeItemId& rItem
464 ,wxClassInfo* pTextCtrlClass = CLASSINFO(wxTextCtrl)
465 );
466
467 //
468 // returns NULL for OS/2 in ALL cases
469 //
470 wxTextCtrl* GetEditControl(void) const {return (wxTextCtrl*)NULL;}
471
472 //
473 // End editing and accept or discard the changes to item label
474 //
475 void EndEditLabel( const wxTreeItemId& rItem
476 ,bool bDiscardChanges = false
477 );
478
479 //
480 // Sorting
481 // -------
482 //
483
484 //
485 // This function is called to compare 2 items and should return -1, 0
486 // or +1 if the first item is less than, equal to or greater than the
487 // second one. The base class version performs alphabetic comparaison
488 // of item labels (GetText)
489 //
490 virtual int OnCompareItems( const wxTreeItemId& rItem1
491 ,const wxTreeItemId& rItem2
492 );
493
494 //
495 // Sort the children of this item using OnCompareItems
496 //
497 void SortChildren(const wxTreeItemId& rItem);
498
499 //
500 // Helpers
501 // -------
502 //
503
504 //
505 // Determine to which item (if any) belongs the given point (the
506 // coordinates specified are relative to the client area of tree ctrl)
507 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
508 // constants.
509
510 //
511 // The first function is more portable (because easier to implement
512 // on other platforms), but the second one returns some extra info.
513 //
514 wxTreeItemId HitTest(const wxPoint& rPoint)
515 { int nDummy = 0; return HitTest(rPoint, nDummy); }
516 wxTreeItemId HitTest( const wxPoint& rPoint
517 ,int& rFlags
518 );
519
520 //
521 // Get the bounding rectangle of the item (or of its label only)
522 //
523 bool GetBoundingRect( const wxTreeItemId& rItem
524 ,wxRect& rRect
525 ,bool bTextOnly = false
526 ) const;
527
528 //
529 // Deprecated
530 // ----------
531
532 #if WXWIN_COMPATIBILITY_2_4
533 // These methods are deprecated and will be removed in future versions of
534 // wxWidgets, they're here for compatibility only, don't use them in new
535 // code (the comments indicate why these methods are now useless and how to
536 // replace them)
537 //
538
539 //
540 // Use Expand, Collapse, CollapseAndReset or Toggle
541 //
542 wxDEPRECATED( void ExpandItem( const wxTreeItemId& rItem
543 ,int nAction
544 ) );
545
546 //
547 // Use AddRoot, PrependItem or AppendItem
548 //
549 wxDEPRECATED( wxTreeItemId InsertItem( const wxTreeItemId& pParent
550 ,const wxString& rsText
551 ,int nImage = -1
552 ,int nSelImage = -1
553 ,long lInsertAfter = wxTREE_INSERT_LAST
554 ) );
555
556 //
557 // Use Set/GetImageList and Set/GetStateImageList
558 //
559 wxImageList* GetImageList(int nVal) const
560 { return GetImageList(); }
561 void SetImageList(wxImageList* pImageList, int nVal)
562 { SetImageList(pImageList); }
563
564 //
565 // Use Set/GetItemImage directly
566 //
567 int GetItemSelectedImage(const wxTreeItemId& rItem) const
568 { return GetItemImage(rItem, wxTreeItemIcon_Selected); }
569 void SetItemSelectedImage(const wxTreeItemId& rItem, int nImage)
570 { SetItemImage(rItem, nImage, wxTreeItemIcon_Selected); }
571 //
572 // For this enumeration function you must pass in a "cookie" parameter
573 // which is opaque for the application but is necessary for the library
574 // to make these functions reentrant (i.e. allow more than one
575 // enumeration on one and the same object simultaneously). Of course,
576 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
577 // the same!
578 //
579
580 //
581 // Get the first child of this item
582 //
583 wxDEPRECATED( wxTreeItemId GetFirstChild( const wxTreeItemId& rItem
584 ,long& rCookie
585 ) const );
586
587 //
588 // Get the next child
589 //
590 wxDEPRECATED( wxTreeItemId GetNextChild( const wxTreeItemId& rItem
591 ,long& rCookie
592 ) const );
593 #endif // WXWIN_COMPATIBILITY_2_4
594
595 //
596 // Implementation
597 // --------------
598 //
599
600 virtual MRESULT OS2WindowProc( WXUINT uMsg
601 ,WXWPARAM wParam
602 ,WXLPARAM lParam
603 );
604 virtual bool OS2Command( WXUINT uParam
605 ,WXWORD wId
606 );
607 // virtual bool OMSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
608
609 //
610 // Override some base class virtuals
611 //
612 virtual bool SetBackgroundColour(const wxColour& rColour);
613 virtual bool SetForegroundColour(const wxColour& rColour);
614
615 //
616 // Get/set the check state for the item (only for wxTR_MULTIPLE)
617 //
618 bool IsItemChecked(const wxTreeItemId& rItem) const;
619 void SetItemCheck( const wxTreeItemId& rItem
620 ,bool bCheck = true
621 );
622
623 protected:
624 //
625 // SetImageList helper
626 //
627 void SetAnyImageList( wxImageList* pImageList
628 ,int nWhich
629 );
630
631 //
632 // Refresh a single item
633 //
634 void RefreshItem(const wxTreeItemId& rItem);
635
636 wxImageList* m_pImageListNormal; // images for tree elements
637 wxImageList* m_pImageListState; // special images for app defined states
638 bool m_bOwnsImageListNormal;
639 bool m_bOwnsImageListState;
640
641 private:
642
643 //
644 // The common part of all ctors
645 //
646 void Init(void);
647
648 //
649 // Helper functions
650 //
651 inline bool DoGetItem(wxTreeViewItem* pTvItem) const;
652 inline void DoSetItem(wxTreeViewItem* pTvItem);
653
654 inline void DoExpand( const wxTreeItemId& rItem
655 ,int nFlag
656 );
657 wxTreeItemId DoInsertItem( const wxTreeItemId& pParent
658 ,wxTreeItemId hInsertAfter
659 ,const wxString& rsText
660 ,int nImage
661 ,int nSelectedImage
662 ,wxTreeItemData* pData
663 );
664 int DoGetItemImageFromData( const wxTreeItemId& rItem
665 ,wxTreeItemIcon vWhich
666 ) const;
667 void DoSetItemImageFromData( const wxTreeItemId& rItem
668 ,int nImage
669 ,wxTreeItemIcon vWhich
670 ) const;
671 void DoSetItemImages( const wxTreeItemId& rItem
672 ,int nImage
673 ,int nImageSel
674 );
675 void DeleteTextCtrl() { };
676
677 //
678 // support for additional item images which we implement using
679 // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
680 //
681 void SetIndirectItemData( const wxTreeItemId& rItem
682 ,class wxTreeItemIndirectData* pData
683 );
684 bool HasIndirectData(const wxTreeItemId& rItem) const;
685 bool IsDataIndirect(wxTreeItemData* pData) const
686 { return pData && pData->GetId().m_pItem == 0; }
687
688 //
689 // The hash storing the items attributes (indexed by items ids)
690 //
691 wxMapTreeAttr m_vAttrs;
692
693 //
694 // true if the hash above is not empty
695 //
696 bool m_bHasAnyAttr;
697
698 //
699 // Used for dragging
700 //
701 wxDragImage* m_pDragImage;
702
703 // Virtual root item, if wxTR_HIDE_ROOT is set.
704 // void* m_pVirtualRoot;
705
706 // the starting item for selection with Shift
707 // WXHTREEITEM m_htSelStart;
708 //
709 friend class wxTreeItemIndirectData;
710 friend class wxTreeSortHelper;
711
712 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
713 DECLARE_NO_COPY_CLASS(wxTreeCtrl)
714 }; // end of CLASS wxTreeCtrl
715
716 #endif // wxUSE_TREECTRL
717
718 #endif
719 // _WX_TREECTRL_H_