| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/msw/treectrl.cpp |
| 3 | // Purpose: wxTreeCtrl |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98 |
| 6 | // Created: 1997 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | // For compilers that support precompilation, includes "wx.h". |
| 21 | #include "wx/wxprec.h" |
| 22 | |
| 23 | #ifdef __BORLANDC__ |
| 24 | #pragma hdrstop |
| 25 | #endif |
| 26 | |
| 27 | #if wxUSE_TREECTRL |
| 28 | |
| 29 | #include "wx/treectrl.h" |
| 30 | |
| 31 | #ifndef WX_PRECOMP |
| 32 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
| 33 | #include "wx/msw/missing.h" |
| 34 | #include "wx/dynarray.h" |
| 35 | #include "wx/log.h" |
| 36 | #include "wx/app.h" |
| 37 | #include "wx/settings.h" |
| 38 | #endif |
| 39 | |
| 40 | #include "wx/msw/private.h" |
| 41 | |
| 42 | // Set this to 1 to be _absolutely_ sure that repainting will work for all |
| 43 | // comctl32.dll versions |
| 44 | #define wxUSE_COMCTL32_SAFELY 0 |
| 45 | |
| 46 | #include "wx/imaglist.h" |
| 47 | #include "wx/msw/dragimag.h" |
| 48 | |
| 49 | // macros to hide the cast ugliness |
| 50 | // -------------------------------- |
| 51 | |
| 52 | // get HTREEITEM from wxTreeItemId |
| 53 | #define HITEM(item) ((HTREEITEM)(((item).m_pItem))) |
| 54 | |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | // private functions |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | |
| 59 | // wrappers for TreeView_GetItem/TreeView_SetItem |
| 60 | static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem) |
| 61 | { |
| 62 | |
| 63 | TV_ITEM tvi; |
| 64 | tvi.mask = TVIF_STATE | TVIF_HANDLE; |
| 65 | tvi.stateMask = TVIS_SELECTED; |
| 66 | tvi.hItem = hItem; |
| 67 | |
| 68 | if ( !TreeView_GetItem(hwndTV, &tvi) ) |
| 69 | { |
| 70 | wxLogLastError(wxT("TreeView_GetItem")); |
| 71 | } |
| 72 | |
| 73 | return (tvi.state & TVIS_SELECTED) != 0; |
| 74 | } |
| 75 | |
| 76 | static bool SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = true) |
| 77 | { |
| 78 | TV_ITEM tvi; |
| 79 | tvi.mask = TVIF_STATE | TVIF_HANDLE; |
| 80 | tvi.stateMask = TVIS_SELECTED; |
| 81 | tvi.state = select ? TVIS_SELECTED : 0; |
| 82 | tvi.hItem = hItem; |
| 83 | |
| 84 | if ( TreeView_SetItem(hwndTV, &tvi) == -1 ) |
| 85 | { |
| 86 | wxLogLastError(wxT("TreeView_SetItem")); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem) |
| 94 | { |
| 95 | SelectItem(hwndTV, htItem, false); |
| 96 | } |
| 97 | |
| 98 | // helper function which selects all items in a range and, optionally, |
| 99 | // unselects all others |
| 100 | static void SelectRange(HWND hwndTV, |
| 101 | HTREEITEM htFirst, |
| 102 | HTREEITEM htLast, |
| 103 | bool unselectOthers = true) |
| 104 | { |
| 105 | // find the first (or last) item and select it |
| 106 | bool cont = true; |
| 107 | HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV); |
| 108 | while ( htItem && cont ) |
| 109 | { |
| 110 | if ( (htItem == htFirst) || (htItem == htLast) ) |
| 111 | { |
| 112 | if ( !IsItemSelected(hwndTV, htItem) ) |
| 113 | { |
| 114 | SelectItem(hwndTV, htItem); |
| 115 | } |
| 116 | |
| 117 | cont = false; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | if ( unselectOthers && IsItemSelected(hwndTV, htItem) ) |
| 122 | { |
| 123 | UnselectItem(hwndTV, htItem); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); |
| 128 | } |
| 129 | |
| 130 | // select the items in range |
| 131 | cont = htFirst != htLast; |
| 132 | while ( htItem && cont ) |
| 133 | { |
| 134 | if ( !IsItemSelected(hwndTV, htItem) ) |
| 135 | { |
| 136 | SelectItem(hwndTV, htItem); |
| 137 | } |
| 138 | |
| 139 | cont = (htItem != htFirst) && (htItem != htLast); |
| 140 | |
| 141 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); |
| 142 | } |
| 143 | |
| 144 | // unselect the rest |
| 145 | if ( unselectOthers ) |
| 146 | { |
| 147 | while ( htItem ) |
| 148 | { |
| 149 | if ( IsItemSelected(hwndTV, htItem) ) |
| 150 | { |
| 151 | UnselectItem(hwndTV, htItem); |
| 152 | } |
| 153 | |
| 154 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // seems to be necessary - otherwise the just selected items don't always |
| 159 | // appear as selected |
| 160 | UpdateWindow(hwndTV); |
| 161 | } |
| 162 | |
| 163 | // helper function which tricks the standard control into changing the focused |
| 164 | // item without changing anything else (if someone knows why Microsoft doesn't |
| 165 | // allow to do it by just setting TVIS_FOCUSED flag, please tell me!) |
| 166 | // |
| 167 | // returns true if the focus was changed, false if the given item was already |
| 168 | // the focused one |
| 169 | static bool SetFocus(HWND hwndTV, HTREEITEM htItem) |
| 170 | { |
| 171 | // the current focus |
| 172 | HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV); |
| 173 | |
| 174 | if ( htItem == htFocus ) |
| 175 | return false; |
| 176 | |
| 177 | if ( htItem ) |
| 178 | { |
| 179 | // remember the selection state of the item |
| 180 | bool wasSelected = IsItemSelected(hwndTV, htItem); |
| 181 | |
| 182 | if ( htFocus && IsItemSelected(hwndTV, htFocus) ) |
| 183 | { |
| 184 | // prevent the tree from unselecting the old focus which it |
| 185 | // would do by default (TreeView_SelectItem unselects the |
| 186 | // focused item) |
| 187 | TreeView_SelectItem(hwndTV, 0); |
| 188 | SelectItem(hwndTV, htFocus); |
| 189 | } |
| 190 | |
| 191 | TreeView_SelectItem(hwndTV, htItem); |
| 192 | |
| 193 | if ( !wasSelected ) |
| 194 | { |
| 195 | // need to clear the selection which TreeView_SelectItem() gave |
| 196 | // us |
| 197 | UnselectItem(hwndTV, htItem); |
| 198 | } |
| 199 | //else: was selected, still selected - ok |
| 200 | } |
| 201 | else // reset focus |
| 202 | { |
| 203 | bool wasFocusSelected = IsItemSelected(hwndTV, htFocus); |
| 204 | |
| 205 | // just clear the focus |
| 206 | TreeView_SelectItem(hwndTV, 0); |
| 207 | |
| 208 | if ( wasFocusSelected ) |
| 209 | { |
| 210 | // restore the selection state |
| 211 | SelectItem(hwndTV, htFocus); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | // ---------------------------------------------------------------------------- |
| 219 | // private classes |
| 220 | // ---------------------------------------------------------------------------- |
| 221 | |
| 222 | // a convenient wrapper around TV_ITEM struct which adds a ctor |
| 223 | #ifdef __VISUALC__ |
| 224 | #pragma warning( disable : 4097 ) // inheriting from typedef |
| 225 | #endif |
| 226 | |
| 227 | struct wxTreeViewItem : public TV_ITEM |
| 228 | { |
| 229 | wxTreeViewItem(const wxTreeItemId& item, // the item handle |
| 230 | UINT mask_, // fields which are valid |
| 231 | UINT stateMask_ = 0) // for TVIF_STATE only |
| 232 | { |
| 233 | wxZeroMemory(*this); |
| 234 | |
| 235 | // hItem member is always valid |
| 236 | mask = mask_ | TVIF_HANDLE; |
| 237 | stateMask = stateMask_; |
| 238 | hItem = HITEM(item); |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | // ---------------------------------------------------------------------------- |
| 243 | // This class is our userdata/lParam for the TV_ITEMs stored in the treeview. |
| 244 | // |
| 245 | // We need this for a couple of reasons: |
| 246 | // |
| 247 | // 1) This class is needed for support of different images: the Win32 common |
| 248 | // control natively supports only 2 images (the normal one and another for the |
| 249 | // selected state). We wish to provide support for 2 more of them for folder |
| 250 | // items (i.e. those which have children): for expanded state and for expanded |
| 251 | // selected state. For this we use this structure to store the additional items |
| 252 | // images. |
| 253 | // |
| 254 | // 2) This class is also needed to hold the HITEM so that we can sort |
| 255 | // it correctly in the MSW sort callback. |
| 256 | // |
| 257 | // In addition it makes other workarounds such as this easier and helps |
| 258 | // simplify the code. |
| 259 | // ---------------------------------------------------------------------------- |
| 260 | |
| 261 | class wxTreeItemParam |
| 262 | { |
| 263 | public: |
| 264 | wxTreeItemParam() |
| 265 | { |
| 266 | m_data = NULL; |
| 267 | |
| 268 | for ( size_t n = 0; n < WXSIZEOF(m_images); n++ ) |
| 269 | { |
| 270 | m_images[n] = -1; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // dtor deletes the associated data as well |
| 275 | virtual ~wxTreeItemParam() { delete m_data; } |
| 276 | |
| 277 | // accessors |
| 278 | // get the real data associated with the item |
| 279 | wxTreeItemData *GetData() const { return m_data; } |
| 280 | // change it |
| 281 | void SetData(wxTreeItemData *data) { m_data = data; } |
| 282 | |
| 283 | // do we have such image? |
| 284 | bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; } |
| 285 | // get image, falling back to the other images if this one is not |
| 286 | // specified |
| 287 | int GetImage(wxTreeItemIcon which) const |
| 288 | { |
| 289 | int image = m_images[which]; |
| 290 | if ( image == -1 ) |
| 291 | { |
| 292 | switch ( which ) |
| 293 | { |
| 294 | case wxTreeItemIcon_SelectedExpanded: |
| 295 | image = GetImage(wxTreeItemIcon_Expanded); |
| 296 | if ( image != -1 ) |
| 297 | break; |
| 298 | //else: fall through |
| 299 | |
| 300 | case wxTreeItemIcon_Selected: |
| 301 | case wxTreeItemIcon_Expanded: |
| 302 | image = GetImage(wxTreeItemIcon_Normal); |
| 303 | break; |
| 304 | |
| 305 | case wxTreeItemIcon_Normal: |
| 306 | // no fallback |
| 307 | break; |
| 308 | |
| 309 | default: |
| 310 | wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") ); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return image; |
| 315 | } |
| 316 | // change the given image |
| 317 | void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; } |
| 318 | |
| 319 | // get item |
| 320 | const wxTreeItemId& GetItem() const { return m_item; } |
| 321 | // set item |
| 322 | void SetItem(const wxTreeItemId& item) { m_item = item; } |
| 323 | |
| 324 | protected: |
| 325 | // all the images associated with the item |
| 326 | int m_images[wxTreeItemIcon_Max]; |
| 327 | |
| 328 | // item for sort callbacks |
| 329 | wxTreeItemId m_item; |
| 330 | |
| 331 | // the real client data |
| 332 | wxTreeItemData *m_data; |
| 333 | |
| 334 | DECLARE_NO_COPY_CLASS(wxTreeItemParam) |
| 335 | }; |
| 336 | |
| 337 | // wxVirutalNode is used in place of a single root when 'hidden' root is |
| 338 | // specified. |
| 339 | class wxVirtualNode : public wxTreeViewItem |
| 340 | { |
| 341 | public: |
| 342 | wxVirtualNode(wxTreeItemParam *param) |
| 343 | : wxTreeViewItem(TVI_ROOT, 0) |
| 344 | { |
| 345 | m_param = param; |
| 346 | } |
| 347 | |
| 348 | ~wxVirtualNode() |
| 349 | { |
| 350 | delete m_param; |
| 351 | } |
| 352 | |
| 353 | wxTreeItemParam *GetParam() const { return m_param; } |
| 354 | void SetParam(wxTreeItemParam *param) { delete m_param; m_param = param; } |
| 355 | |
| 356 | private: |
| 357 | wxTreeItemParam *m_param; |
| 358 | |
| 359 | DECLARE_NO_COPY_CLASS(wxVirtualNode) |
| 360 | }; |
| 361 | |
| 362 | #ifdef __VISUALC__ |
| 363 | #pragma warning( default : 4097 ) |
| 364 | #endif |
| 365 | |
| 366 | // a macro to get the virtual root, returns NULL if none |
| 367 | #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot) |
| 368 | |
| 369 | // returns true if the item is the virtual root |
| 370 | #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT) |
| 371 | |
| 372 | // a class which encapsulates the tree traversal logic: it vists all (unless |
| 373 | // OnVisit() returns false) items under the given one |
| 374 | class wxTreeTraversal |
| 375 | { |
| 376 | public: |
| 377 | wxTreeTraversal(const wxTreeCtrl *tree) |
| 378 | { |
| 379 | m_tree = tree; |
| 380 | } |
| 381 | |
| 382 | // give it a virtual dtor: not really needed as the class is never used |
| 383 | // polymorphically and not even allocated on heap at all, but this is safer |
| 384 | // (in case it ever is) and silences the compiler warnings for now |
| 385 | virtual ~wxTreeTraversal() { } |
| 386 | |
| 387 | // do traverse the tree: visit all items (recursively by default) under the |
| 388 | // given one; return true if all items were traversed or false if the |
| 389 | // traversal was aborted because OnVisit returned false |
| 390 | bool DoTraverse(const wxTreeItemId& root, bool recursively = true); |
| 391 | |
| 392 | // override this function to do whatever is needed for each item, return |
| 393 | // false to stop traversing |
| 394 | virtual bool OnVisit(const wxTreeItemId& item) = 0; |
| 395 | |
| 396 | protected: |
| 397 | const wxTreeCtrl *GetTree() const { return m_tree; } |
| 398 | |
| 399 | private: |
| 400 | bool Traverse(const wxTreeItemId& root, bool recursively); |
| 401 | |
| 402 | const wxTreeCtrl *m_tree; |
| 403 | |
| 404 | DECLARE_NO_COPY_CLASS(wxTreeTraversal) |
| 405 | }; |
| 406 | |
| 407 | // internal class for getting the selected items |
| 408 | class TraverseSelections : public wxTreeTraversal |
| 409 | { |
| 410 | public: |
| 411 | TraverseSelections(const wxTreeCtrl *tree, |
| 412 | wxArrayTreeItemIds& selections) |
| 413 | : wxTreeTraversal(tree), m_selections(selections) |
| 414 | { |
| 415 | m_selections.Empty(); |
| 416 | |
| 417 | if (tree->GetCount() > 0) |
| 418 | DoTraverse(tree->GetRootItem()); |
| 419 | } |
| 420 | |
| 421 | virtual bool OnVisit(const wxTreeItemId& item) |
| 422 | { |
| 423 | const wxTreeCtrl * const tree = GetTree(); |
| 424 | |
| 425 | // can't visit a virtual node. |
| 426 | if ( (tree->GetRootItem() == item) && tree->HasFlag(wxTR_HIDE_ROOT) ) |
| 427 | { |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | if ( ::IsItemSelected(GetHwndOf(tree), HITEM(item)) ) |
| 432 | { |
| 433 | m_selections.Add(item); |
| 434 | } |
| 435 | |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | size_t GetCount() const { return m_selections.GetCount(); } |
| 440 | |
| 441 | private: |
| 442 | wxArrayTreeItemIds& m_selections; |
| 443 | |
| 444 | DECLARE_NO_COPY_CLASS(TraverseSelections) |
| 445 | }; |
| 446 | |
| 447 | // internal class for counting tree items |
| 448 | class TraverseCounter : public wxTreeTraversal |
| 449 | { |
| 450 | public: |
| 451 | TraverseCounter(const wxTreeCtrl *tree, |
| 452 | const wxTreeItemId& root, |
| 453 | bool recursively) |
| 454 | : wxTreeTraversal(tree) |
| 455 | { |
| 456 | m_count = 0; |
| 457 | |
| 458 | DoTraverse(root, recursively); |
| 459 | } |
| 460 | |
| 461 | virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item)) |
| 462 | { |
| 463 | m_count++; |
| 464 | |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | size_t GetCount() const { return m_count; } |
| 469 | |
| 470 | private: |
| 471 | size_t m_count; |
| 472 | |
| 473 | DECLARE_NO_COPY_CLASS(TraverseCounter) |
| 474 | }; |
| 475 | |
| 476 | // ---------------------------------------------------------------------------- |
| 477 | // wxWin macros |
| 478 | // ---------------------------------------------------------------------------- |
| 479 | |
| 480 | #if wxUSE_EXTENDED_RTTI |
| 481 | WX_DEFINE_FLAGS( wxTreeCtrlStyle ) |
| 482 | |
| 483 | wxBEGIN_FLAGS( wxTreeCtrlStyle ) |
| 484 | // new style border flags, we put them first to |
| 485 | // use them for streaming out |
| 486 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
| 487 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) |
| 488 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) |
| 489 | wxFLAGS_MEMBER(wxBORDER_RAISED) |
| 490 | wxFLAGS_MEMBER(wxBORDER_STATIC) |
| 491 | wxFLAGS_MEMBER(wxBORDER_NONE) |
| 492 | |
| 493 | // old style border flags |
| 494 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
| 495 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) |
| 496 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) |
| 497 | wxFLAGS_MEMBER(wxRAISED_BORDER) |
| 498 | wxFLAGS_MEMBER(wxSTATIC_BORDER) |
| 499 | wxFLAGS_MEMBER(wxBORDER) |
| 500 | |
| 501 | // standard window styles |
| 502 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
| 503 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) |
| 504 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) |
| 505 | wxFLAGS_MEMBER(wxWANTS_CHARS) |
| 506 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
| 507 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
| 508 | wxFLAGS_MEMBER(wxVSCROLL) |
| 509 | wxFLAGS_MEMBER(wxHSCROLL) |
| 510 | |
| 511 | wxFLAGS_MEMBER(wxTR_EDIT_LABELS) |
| 512 | wxFLAGS_MEMBER(wxTR_NO_BUTTONS) |
| 513 | wxFLAGS_MEMBER(wxTR_HAS_BUTTONS) |
| 514 | wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS) |
| 515 | wxFLAGS_MEMBER(wxTR_NO_LINES) |
| 516 | wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT) |
| 517 | wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT) |
| 518 | wxFLAGS_MEMBER(wxTR_HIDE_ROOT) |
| 519 | wxFLAGS_MEMBER(wxTR_ROW_LINES) |
| 520 | wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT) |
| 521 | wxFLAGS_MEMBER(wxTR_SINGLE) |
| 522 | wxFLAGS_MEMBER(wxTR_MULTIPLE) |
| 523 | #if WXWIN_COMPATIBILITY_2_8 |
| 524 | wxFLAGS_MEMBER(wxTR_EXTENDED) |
| 525 | #endif |
| 526 | wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE) |
| 527 | |
| 528 | wxEND_FLAGS( wxTreeCtrlStyle ) |
| 529 | |
| 530 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl, wxControl,"wx/treectrl.h") |
| 531 | |
| 532 | wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl) |
| 533 | wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent ) |
| 534 | wxEVENT_RANGE_PROPERTY( TreeEvent , wxEVT_COMMAND_TREE_BEGIN_DRAG , wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK , wxTreeEvent ) |
| 535 | wxPROPERTY_FLAGS( WindowStyle , wxTreeCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
| 536 | wxEND_PROPERTIES_TABLE() |
| 537 | |
| 538 | wxBEGIN_HANDLERS_TABLE(wxTreeCtrl) |
| 539 | wxEND_HANDLERS_TABLE() |
| 540 | |
| 541 | wxCONSTRUCTOR_5( wxTreeCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
| 542 | #else |
| 543 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) |
| 544 | #endif |
| 545 | |
| 546 | // ---------------------------------------------------------------------------- |
| 547 | // constants |
| 548 | // ---------------------------------------------------------------------------- |
| 549 | |
| 550 | // indices in gs_expandEvents table below |
| 551 | enum |
| 552 | { |
| 553 | IDX_COLLAPSE, |
| 554 | IDX_EXPAND, |
| 555 | IDX_WHAT_MAX |
| 556 | }; |
| 557 | |
| 558 | enum |
| 559 | { |
| 560 | IDX_DONE, |
| 561 | IDX_DOING, |
| 562 | IDX_HOW_MAX |
| 563 | }; |
| 564 | |
| 565 | // handy table for sending events - it has to be initialized during run-time |
| 566 | // now so can't be const any more |
| 567 | static /* const */ wxEventType gs_expandEvents[IDX_WHAT_MAX][IDX_HOW_MAX]; |
| 568 | |
| 569 | /* |
| 570 | but logically it's a const table with the following entries: |
| 571 | = |
| 572 | { |
| 573 | { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING }, |
| 574 | { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING } |
| 575 | }; |
| 576 | */ |
| 577 | |
| 578 | // ============================================================================ |
| 579 | // implementation |
| 580 | // ============================================================================ |
| 581 | |
| 582 | // ---------------------------------------------------------------------------- |
| 583 | // tree traversal |
| 584 | // ---------------------------------------------------------------------------- |
| 585 | |
| 586 | bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively) |
| 587 | { |
| 588 | if ( !OnVisit(root) ) |
| 589 | return false; |
| 590 | |
| 591 | return Traverse(root, recursively); |
| 592 | } |
| 593 | |
| 594 | bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively) |
| 595 | { |
| 596 | wxTreeItemIdValue cookie; |
| 597 | wxTreeItemId child = m_tree->GetFirstChild(root, cookie); |
| 598 | while ( child.IsOk() ) |
| 599 | { |
| 600 | // depth first traversal |
| 601 | if ( recursively && !Traverse(child, true) ) |
| 602 | return false; |
| 603 | |
| 604 | if ( !OnVisit(child) ) |
| 605 | return false; |
| 606 | |
| 607 | child = m_tree->GetNextChild(root, cookie); |
| 608 | } |
| 609 | |
| 610 | return true; |
| 611 | } |
| 612 | |
| 613 | // ---------------------------------------------------------------------------- |
| 614 | // construction and destruction |
| 615 | // ---------------------------------------------------------------------------- |
| 616 | |
| 617 | void wxTreeCtrl::Init() |
| 618 | { |
| 619 | m_textCtrl = NULL; |
| 620 | m_hasAnyAttr = false; |
| 621 | #if wxUSE_DRAGIMAGE |
| 622 | m_dragImage = NULL; |
| 623 | #endif |
| 624 | m_pVirtualRoot = NULL; |
| 625 | |
| 626 | // initialize the global array of events now as it can't be done statically |
| 627 | // with the wxEVT_XXX values being allocated during run-time only |
| 628 | gs_expandEvents[IDX_COLLAPSE][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED; |
| 629 | gs_expandEvents[IDX_COLLAPSE][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING; |
| 630 | gs_expandEvents[IDX_EXPAND][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_EXPANDED; |
| 631 | gs_expandEvents[IDX_EXPAND][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_EXPANDING; |
| 632 | } |
| 633 | |
| 634 | bool wxTreeCtrl::Create(wxWindow *parent, |
| 635 | wxWindowID id, |
| 636 | const wxPoint& pos, |
| 637 | const wxSize& size, |
| 638 | long style, |
| 639 | const wxValidator& validator, |
| 640 | const wxString& name) |
| 641 | { |
| 642 | Init(); |
| 643 | |
| 644 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) |
| 645 | style |= wxBORDER_SUNKEN; |
| 646 | |
| 647 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
| 648 | return false; |
| 649 | |
| 650 | WXDWORD exStyle = 0; |
| 651 | DWORD wstyle = MSWGetStyle(m_windowStyle, & exStyle); |
| 652 | wstyle |= WS_TABSTOP | TVS_SHOWSELALWAYS; |
| 653 | |
| 654 | if ((m_windowStyle & wxTR_NO_LINES) == 0) |
| 655 | wstyle |= TVS_HASLINES; |
| 656 | if ( m_windowStyle & wxTR_HAS_BUTTONS ) |
| 657 | wstyle |= TVS_HASBUTTONS; |
| 658 | |
| 659 | if ( m_windowStyle & wxTR_EDIT_LABELS ) |
| 660 | wstyle |= TVS_EDITLABELS; |
| 661 | |
| 662 | if ( m_windowStyle & wxTR_LINES_AT_ROOT ) |
| 663 | wstyle |= TVS_LINESATROOT; |
| 664 | |
| 665 | if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT ) |
| 666 | { |
| 667 | if ( wxApp::GetComCtl32Version() >= 471 ) |
| 668 | wstyle |= TVS_FULLROWSELECT; |
| 669 | } |
| 670 | |
| 671 | #if !defined(__WXWINCE__) && defined(TVS_INFOTIP) |
| 672 | // Need so that TVN_GETINFOTIP messages will be sent |
| 673 | wstyle |= TVS_INFOTIP; |
| 674 | #endif |
| 675 | |
| 676 | // Create the tree control. |
| 677 | if ( !MSWCreateControl(WC_TREEVIEW, wstyle, pos, size) ) |
| 678 | return false; |
| 679 | |
| 680 | #if wxUSE_COMCTL32_SAFELY |
| 681 | wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
| 682 | wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
| 683 | #elif 1 |
| 684 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
| 685 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
| 686 | #else |
| 687 | // This works around a bug in the Windows tree control whereby for some versions |
| 688 | // of comctrl32, setting any colour actually draws the background in black. |
| 689 | // This will initialise the background to the system colour. |
| 690 | // THIS FIX NOW REVERTED since it caused problems on _other_ systems. |
| 691 | // Assume the user has an updated comctl32.dll. |
| 692 | ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1); |
| 693 | wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
| 694 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
| 695 | #endif |
| 696 | |
| 697 | |
| 698 | // VZ: this is some experimental code which may be used to get the |
| 699 | // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71. |
| 700 | // AFAIK, the standard DLL does about the same thing anyhow. |
| 701 | #if 0 |
| 702 | if ( m_windowStyle & wxTR_MULTIPLE ) |
| 703 | { |
| 704 | wxBitmap bmp; |
| 705 | |
| 706 | // create the DC compatible with the current screen |
| 707 | HDC hdcMem = CreateCompatibleDC(NULL); |
| 708 | |
| 709 | // create a mono bitmap of the standard size |
| 710 | int x = ::GetSystemMetrics(SM_CXMENUCHECK); |
| 711 | int y = ::GetSystemMetrics(SM_CYMENUCHECK); |
| 712 | wxImageList imagelistCheckboxes(x, y, false, 2); |
| 713 | HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size |
| 714 | 1, // # of color planes |
| 715 | 1, // # bits needed for one pixel |
| 716 | 0); // array containing colour data |
| 717 | SelectObject(hdcMem, hbmpCheck); |
| 718 | |
| 719 | // then draw a check mark into it |
| 720 | RECT rect = { 0, 0, x, y }; |
| 721 | if ( !::DrawFrameControl(hdcMem, &rect, |
| 722 | DFC_BUTTON, |
| 723 | DFCS_BUTTONCHECK | DFCS_CHECKED) ) |
| 724 | { |
| 725 | wxLogLastError(wxT("DrawFrameControl(check)")); |
| 726 | } |
| 727 | |
| 728 | bmp.SetHBITMAP((WXHBITMAP)hbmpCheck); |
| 729 | imagelistCheckboxes.Add(bmp); |
| 730 | |
| 731 | if ( !::DrawFrameControl(hdcMem, &rect, |
| 732 | DFC_BUTTON, |
| 733 | DFCS_BUTTONCHECK) ) |
| 734 | { |
| 735 | wxLogLastError(wxT("DrawFrameControl(uncheck)")); |
| 736 | } |
| 737 | |
| 738 | bmp.SetHBITMAP((WXHBITMAP)hbmpCheck); |
| 739 | imagelistCheckboxes.Add(bmp); |
| 740 | |
| 741 | // clean up |
| 742 | ::DeleteDC(hdcMem); |
| 743 | |
| 744 | // set the imagelist |
| 745 | SetStateImageList(&imagelistCheckboxes); |
| 746 | } |
| 747 | #endif // 0 |
| 748 | |
| 749 | wxSetCCUnicodeFormat(GetHwnd()); |
| 750 | |
| 751 | return true; |
| 752 | } |
| 753 | |
| 754 | wxTreeCtrl::~wxTreeCtrl() |
| 755 | { |
| 756 | // delete any attributes |
| 757 | if ( m_hasAnyAttr ) |
| 758 | { |
| 759 | WX_CLEAR_HASH_MAP(wxMapTreeAttr, m_attrs); |
| 760 | |
| 761 | // prevent TVN_DELETEITEM handler from deleting the attributes again! |
| 762 | m_hasAnyAttr = false; |
| 763 | } |
| 764 | |
| 765 | DeleteTextCtrl(); |
| 766 | |
| 767 | // delete user data to prevent memory leaks |
| 768 | // also deletes hidden root node storage. |
| 769 | DeleteAllItems(); |
| 770 | } |
| 771 | |
| 772 | // ---------------------------------------------------------------------------- |
| 773 | // accessors |
| 774 | // ---------------------------------------------------------------------------- |
| 775 | |
| 776 | /* static */ wxVisualAttributes |
| 777 | wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant) |
| 778 | { |
| 779 | wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant); |
| 780 | |
| 781 | // common controls have their own default font |
| 782 | attrs.font = wxGetCCDefaultFont(); |
| 783 | |
| 784 | return attrs; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | // simple wrappers which add error checking in debug mode |
| 789 | |
| 790 | bool wxTreeCtrl::DoGetItem(wxTreeViewItem *tvItem) const |
| 791 | { |
| 792 | wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false, |
| 793 | _T("can't retrieve virtual root item") ); |
| 794 | |
| 795 | if ( !TreeView_GetItem(GetHwnd(), tvItem) ) |
| 796 | { |
| 797 | wxLogLastError(wxT("TreeView_GetItem")); |
| 798 | |
| 799 | return false; |
| 800 | } |
| 801 | |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | void wxTreeCtrl::DoSetItem(wxTreeViewItem *tvItem) |
| 806 | { |
| 807 | if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 ) |
| 808 | { |
| 809 | wxLogLastError(wxT("TreeView_SetItem")); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | unsigned int wxTreeCtrl::GetCount() const |
| 814 | { |
| 815 | return (unsigned int)TreeView_GetCount(GetHwnd()); |
| 816 | } |
| 817 | |
| 818 | unsigned int wxTreeCtrl::GetIndent() const |
| 819 | { |
| 820 | return TreeView_GetIndent(GetHwnd()); |
| 821 | } |
| 822 | |
| 823 | void wxTreeCtrl::SetIndent(unsigned int indent) |
| 824 | { |
| 825 | TreeView_SetIndent(GetHwnd(), indent); |
| 826 | } |
| 827 | |
| 828 | void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which) |
| 829 | { |
| 830 | // no error return |
| 831 | (void) TreeView_SetImageList(GetHwnd(), |
| 832 | imageList ? imageList->GetHIMAGELIST() : 0, |
| 833 | which); |
| 834 | } |
| 835 | |
| 836 | void wxTreeCtrl::SetImageList(wxImageList *imageList) |
| 837 | { |
| 838 | if (m_ownsImageListNormal) |
| 839 | delete m_imageListNormal; |
| 840 | |
| 841 | SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL); |
| 842 | m_ownsImageListNormal = false; |
| 843 | } |
| 844 | |
| 845 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) |
| 846 | { |
| 847 | if (m_ownsImageListState) delete m_imageListState; |
| 848 | SetAnyImageList(m_imageListState = imageList, TVSIL_STATE); |
| 849 | m_ownsImageListState = false; |
| 850 | } |
| 851 | |
| 852 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, |
| 853 | bool recursively) const |
| 854 | { |
| 855 | wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") ); |
| 856 | |
| 857 | TraverseCounter counter(this, item, recursively); |
| 858 | return counter.GetCount() - 1; |
| 859 | } |
| 860 | |
| 861 | // ---------------------------------------------------------------------------- |
| 862 | // control colours |
| 863 | // ---------------------------------------------------------------------------- |
| 864 | |
| 865 | bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour) |
| 866 | { |
| 867 | #if !wxUSE_COMCTL32_SAFELY |
| 868 | if ( !wxWindowBase::SetBackgroundColour(colour) ) |
| 869 | return false; |
| 870 | |
| 871 | ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel()); |
| 872 | #endif |
| 873 | |
| 874 | return true; |
| 875 | } |
| 876 | |
| 877 | bool wxTreeCtrl::SetForegroundColour(const wxColour &colour) |
| 878 | { |
| 879 | #if !wxUSE_COMCTL32_SAFELY |
| 880 | if ( !wxWindowBase::SetForegroundColour(colour) ) |
| 881 | return false; |
| 882 | |
| 883 | ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel()); |
| 884 | #endif |
| 885 | |
| 886 | return true; |
| 887 | } |
| 888 | |
| 889 | // ---------------------------------------------------------------------------- |
| 890 | // Item access |
| 891 | // ---------------------------------------------------------------------------- |
| 892 | |
| 893 | bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId& item) const |
| 894 | { |
| 895 | return HITEM(item) == TVI_ROOT && HasFlag(wxTR_HIDE_ROOT); |
| 896 | } |
| 897 | |
| 898 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const |
| 899 | { |
| 900 | wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") ); |
| 901 | |
| 902 | wxChar buf[512]; // the size is arbitrary... |
| 903 | |
| 904 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
| 905 | tvItem.pszText = buf; |
| 906 | tvItem.cchTextMax = WXSIZEOF(buf); |
| 907 | if ( !DoGetItem(&tvItem) ) |
| 908 | { |
| 909 | // don't return some garbage which was on stack, but an empty string |
| 910 | buf[0] = wxT('\0'); |
| 911 | } |
| 912 | |
| 913 | return wxString(buf); |
| 914 | } |
| 915 | |
| 916 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) |
| 917 | { |
| 918 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 919 | |
| 920 | if ( IS_VIRTUAL_ROOT(item) ) |
| 921 | return; |
| 922 | |
| 923 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
| 924 | tvItem.pszText = (wxChar *)text.wx_str(); // conversion is ok |
| 925 | DoSetItem(&tvItem); |
| 926 | |
| 927 | // when setting the text of the item being edited, the text control should |
| 928 | // be updated to reflect the new text as well, otherwise calling |
| 929 | // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect |
| 930 | // |
| 931 | // don't use GetEditControl() here because m_textCtrl is not set yet |
| 932 | HWND hwndEdit = TreeView_GetEditControl(GetHwnd()); |
| 933 | if ( hwndEdit ) |
| 934 | { |
| 935 | if ( item == m_idEdited ) |
| 936 | { |
| 937 | ::SetWindowText(hwndEdit, text.wx_str()); |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item, |
| 943 | wxTreeItemIcon which) const |
| 944 | { |
| 945 | wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") ); |
| 946 | |
| 947 | if ( IsHiddenRoot(item) ) |
| 948 | { |
| 949 | // no images for hidden root item |
| 950 | return -1; |
| 951 | } |
| 952 | |
| 953 | wxTreeItemParam *param = GetItemParam(item); |
| 954 | |
| 955 | return param && param->HasImage(which) ? param->GetImage(which) : -1; |
| 956 | } |
| 957 | |
| 958 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image, |
| 959 | wxTreeItemIcon which) |
| 960 | { |
| 961 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 962 | wxCHECK_RET( which >= 0 && |
| 963 | which < wxTreeItemIcon_Max, |
| 964 | wxT("invalid image index")); |
| 965 | |
| 966 | |
| 967 | if ( IsHiddenRoot(item) ) |
| 968 | { |
| 969 | // no images for hidden root item |
| 970 | return; |
| 971 | } |
| 972 | |
| 973 | wxTreeItemParam *data = GetItemParam(item); |
| 974 | if ( !data ) |
| 975 | return; |
| 976 | |
| 977 | data->SetImage(image, which); |
| 978 | |
| 979 | RefreshItem(item); |
| 980 | } |
| 981 | |
| 982 | wxTreeItemParam *wxTreeCtrl::GetItemParam(const wxTreeItemId& item) const |
| 983 | { |
| 984 | wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") ); |
| 985 | |
| 986 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
| 987 | |
| 988 | // hidden root may still have data. |
| 989 | if ( IS_VIRTUAL_ROOT(item) ) |
| 990 | { |
| 991 | return GET_VIRTUAL_ROOT()->GetParam(); |
| 992 | } |
| 993 | |
| 994 | // visible node. |
| 995 | if ( !DoGetItem(&tvItem) ) |
| 996 | { |
| 997 | return NULL; |
| 998 | } |
| 999 | |
| 1000 | return (wxTreeItemParam *)tvItem.lParam; |
| 1001 | } |
| 1002 | |
| 1003 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const |
| 1004 | { |
| 1005 | wxTreeItemParam *data = GetItemParam(item); |
| 1006 | |
| 1007 | return data ? data->GetData() : NULL; |
| 1008 | } |
| 1009 | |
| 1010 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) |
| 1011 | { |
| 1012 | // first, associate this piece of data with this item |
| 1013 | if ( data ) |
| 1014 | { |
| 1015 | data->SetId(item); |
| 1016 | } |
| 1017 | |
| 1018 | wxTreeItemParam *param = GetItemParam(item); |
| 1019 | |
| 1020 | wxCHECK_RET( param, wxT("failed to change tree items data") ); |
| 1021 | |
| 1022 | param->SetData(data); |
| 1023 | } |
| 1024 | |
| 1025 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) |
| 1026 | { |
| 1027 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1028 | |
| 1029 | if ( IS_VIRTUAL_ROOT(item) ) |
| 1030 | return; |
| 1031 | |
| 1032 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
| 1033 | tvItem.cChildren = (int)has; |
| 1034 | DoSetItem(&tvItem); |
| 1035 | } |
| 1036 | |
| 1037 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) |
| 1038 | { |
| 1039 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1040 | |
| 1041 | if ( IS_VIRTUAL_ROOT(item) ) |
| 1042 | return; |
| 1043 | |
| 1044 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); |
| 1045 | tvItem.state = bold ? TVIS_BOLD : 0; |
| 1046 | DoSetItem(&tvItem); |
| 1047 | } |
| 1048 | |
| 1049 | void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight) |
| 1050 | { |
| 1051 | if ( IS_VIRTUAL_ROOT(item) ) |
| 1052 | return; |
| 1053 | |
| 1054 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED); |
| 1055 | tvItem.state = highlight ? TVIS_DROPHILITED : 0; |
| 1056 | DoSetItem(&tvItem); |
| 1057 | } |
| 1058 | |
| 1059 | void wxTreeCtrl::RefreshItem(const wxTreeItemId& item) |
| 1060 | { |
| 1061 | if ( IS_VIRTUAL_ROOT(item) ) |
| 1062 | return; |
| 1063 | |
| 1064 | wxRect rect; |
| 1065 | if ( GetBoundingRect(item, rect) ) |
| 1066 | { |
| 1067 | RefreshRect(rect); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const |
| 1072 | { |
| 1073 | wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") ); |
| 1074 | |
| 1075 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
| 1076 | return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour(); |
| 1077 | } |
| 1078 | |
| 1079 | wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const |
| 1080 | { |
| 1081 | wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") ); |
| 1082 | |
| 1083 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
| 1084 | return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour(); |
| 1085 | } |
| 1086 | |
| 1087 | wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const |
| 1088 | { |
| 1089 | wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") ); |
| 1090 | |
| 1091 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
| 1092 | return it == m_attrs.end() ? wxNullFont : it->second->GetFont(); |
| 1093 | } |
| 1094 | |
| 1095 | void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item, |
| 1096 | const wxColour& col) |
| 1097 | { |
| 1098 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1099 | |
| 1100 | wxTreeItemAttr *attr; |
| 1101 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
| 1102 | if ( it == m_attrs.end() ) |
| 1103 | { |
| 1104 | m_hasAnyAttr = true; |
| 1105 | |
| 1106 | m_attrs[item.m_pItem] = |
| 1107 | attr = new wxTreeItemAttr; |
| 1108 | } |
| 1109 | else |
| 1110 | { |
| 1111 | attr = it->second; |
| 1112 | } |
| 1113 | |
| 1114 | attr->SetTextColour(col); |
| 1115 | |
| 1116 | RefreshItem(item); |
| 1117 | } |
| 1118 | |
| 1119 | void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item, |
| 1120 | const wxColour& col) |
| 1121 | { |
| 1122 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1123 | |
| 1124 | wxTreeItemAttr *attr; |
| 1125 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
| 1126 | if ( it == m_attrs.end() ) |
| 1127 | { |
| 1128 | m_hasAnyAttr = true; |
| 1129 | |
| 1130 | m_attrs[item.m_pItem] = |
| 1131 | attr = new wxTreeItemAttr; |
| 1132 | } |
| 1133 | else // already in the hash |
| 1134 | { |
| 1135 | attr = it->second; |
| 1136 | } |
| 1137 | |
| 1138 | attr->SetBackgroundColour(col); |
| 1139 | |
| 1140 | RefreshItem(item); |
| 1141 | } |
| 1142 | |
| 1143 | void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font) |
| 1144 | { |
| 1145 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1146 | |
| 1147 | wxTreeItemAttr *attr; |
| 1148 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
| 1149 | if ( it == m_attrs.end() ) |
| 1150 | { |
| 1151 | m_hasAnyAttr = true; |
| 1152 | |
| 1153 | m_attrs[item.m_pItem] = |
| 1154 | attr = new wxTreeItemAttr; |
| 1155 | } |
| 1156 | else // already in the hash |
| 1157 | { |
| 1158 | attr = it->second; |
| 1159 | } |
| 1160 | |
| 1161 | attr->SetFont(font); |
| 1162 | |
| 1163 | // Reset the item's text to ensure that the bounding rect will be adjusted |
| 1164 | // for the new font. |
| 1165 | SetItemText(item, GetItemText(item)); |
| 1166 | |
| 1167 | RefreshItem(item); |
| 1168 | } |
| 1169 | |
| 1170 | // ---------------------------------------------------------------------------- |
| 1171 | // Item status |
| 1172 | // ---------------------------------------------------------------------------- |
| 1173 | |
| 1174 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const |
| 1175 | { |
| 1176 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1177 | |
| 1178 | if ( item == wxTreeItemId(TVI_ROOT) ) |
| 1179 | { |
| 1180 | // virtual (hidden) root is never visible |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
| 1184 | // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect |
| 1185 | RECT rect; |
| 1186 | |
| 1187 | // this ugliness comes directly from MSDN - it *is* the correct way to pass |
| 1188 | // the HTREEITEM with TVM_GETITEMRECT |
| 1189 | *(HTREEITEM *)&rect = HITEM(item); |
| 1190 | |
| 1191 | // true means to get rect for just the text, not the whole line |
| 1192 | if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT, true, (LPARAM)&rect) ) |
| 1193 | { |
| 1194 | // if TVM_GETITEMRECT returned false, then the item is definitely not |
| 1195 | // visible (because its parent is not expanded) |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
| 1199 | // however if it returned true, the item might still be outside the |
| 1200 | // currently visible part of the tree, test for it (notice that partly |
| 1201 | // visible means visible here) |
| 1202 | return rect.bottom > 0 && rect.top < GetClientSize().y; |
| 1203 | } |
| 1204 | |
| 1205 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const |
| 1206 | { |
| 1207 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1208 | |
| 1209 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
| 1210 | DoGetItem(&tvItem); |
| 1211 | |
| 1212 | return tvItem.cChildren != 0; |
| 1213 | } |
| 1214 | |
| 1215 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const |
| 1216 | { |
| 1217 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1218 | |
| 1219 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED); |
| 1220 | DoGetItem(&tvItem); |
| 1221 | |
| 1222 | return (tvItem.state & TVIS_EXPANDED) != 0; |
| 1223 | } |
| 1224 | |
| 1225 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const |
| 1226 | { |
| 1227 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1228 | |
| 1229 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED); |
| 1230 | DoGetItem(&tvItem); |
| 1231 | |
| 1232 | return (tvItem.state & TVIS_SELECTED) != 0; |
| 1233 | } |
| 1234 | |
| 1235 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const |
| 1236 | { |
| 1237 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1238 | |
| 1239 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); |
| 1240 | DoGetItem(&tvItem); |
| 1241 | |
| 1242 | return (tvItem.state & TVIS_BOLD) != 0; |
| 1243 | } |
| 1244 | |
| 1245 | // ---------------------------------------------------------------------------- |
| 1246 | // navigation |
| 1247 | // ---------------------------------------------------------------------------- |
| 1248 | |
| 1249 | wxTreeItemId wxTreeCtrl::GetRootItem() const |
| 1250 | { |
| 1251 | // Root may be real (visible) or virtual (hidden). |
| 1252 | if ( GET_VIRTUAL_ROOT() ) |
| 1253 | return TVI_ROOT; |
| 1254 | |
| 1255 | return wxTreeItemId(TreeView_GetRoot(GetHwnd())); |
| 1256 | } |
| 1257 | |
| 1258 | wxTreeItemId wxTreeCtrl::GetSelection() const |
| 1259 | { |
| 1260 | wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxTreeItemId(), |
| 1261 | wxT("this only works with single selection controls") ); |
| 1262 | |
| 1263 | return wxTreeItemId(TreeView_GetSelection(GetHwnd())); |
| 1264 | } |
| 1265 | |
| 1266 | wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const |
| 1267 | { |
| 1268 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1269 | |
| 1270 | HTREEITEM hItem; |
| 1271 | |
| 1272 | if ( IS_VIRTUAL_ROOT(item) ) |
| 1273 | { |
| 1274 | // no parent for the virtual root |
| 1275 | hItem = 0; |
| 1276 | } |
| 1277 | else // normal item |
| 1278 | { |
| 1279 | hItem = TreeView_GetParent(GetHwnd(), HITEM(item)); |
| 1280 | if ( !hItem && HasFlag(wxTR_HIDE_ROOT) ) |
| 1281 | { |
| 1282 | // the top level items should have the virtual root as their parent |
| 1283 | hItem = TVI_ROOT; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | return wxTreeItemId(hItem); |
| 1288 | } |
| 1289 | |
| 1290 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, |
| 1291 | wxTreeItemIdValue& cookie) const |
| 1292 | { |
| 1293 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1294 | |
| 1295 | // remember the last child returned in 'cookie' |
| 1296 | cookie = TreeView_GetChild(GetHwnd(), HITEM(item)); |
| 1297 | |
| 1298 | return wxTreeItemId(cookie); |
| 1299 | } |
| 1300 | |
| 1301 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), |
| 1302 | wxTreeItemIdValue& cookie) const |
| 1303 | { |
| 1304 | wxTreeItemId fromCookie(cookie); |
| 1305 | |
| 1306 | HTREEITEM hitem = HITEM(fromCookie); |
| 1307 | |
| 1308 | hitem = TreeView_GetNextSibling(GetHwnd(), hitem); |
| 1309 | |
| 1310 | wxTreeItemId item(hitem); |
| 1311 | |
| 1312 | cookie = item.m_pItem; |
| 1313 | |
| 1314 | return item; |
| 1315 | } |
| 1316 | |
| 1317 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const |
| 1318 | { |
| 1319 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1320 | |
| 1321 | // can this be done more efficiently? |
| 1322 | wxTreeItemIdValue cookie; |
| 1323 | |
| 1324 | wxTreeItemId childLast, |
| 1325 | child = GetFirstChild(item, cookie); |
| 1326 | while ( child.IsOk() ) |
| 1327 | { |
| 1328 | childLast = child; |
| 1329 | child = GetNextChild(item, cookie); |
| 1330 | } |
| 1331 | |
| 1332 | return childLast; |
| 1333 | } |
| 1334 | |
| 1335 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const |
| 1336 | { |
| 1337 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1338 | return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item))); |
| 1339 | } |
| 1340 | |
| 1341 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const |
| 1342 | { |
| 1343 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1344 | return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item))); |
| 1345 | } |
| 1346 | |
| 1347 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const |
| 1348 | { |
| 1349 | return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd())); |
| 1350 | } |
| 1351 | |
| 1352 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const |
| 1353 | { |
| 1354 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1355 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!")); |
| 1356 | |
| 1357 | return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item))); |
| 1358 | } |
| 1359 | |
| 1360 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const |
| 1361 | { |
| 1362 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1363 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!")); |
| 1364 | |
| 1365 | return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item))); |
| 1366 | } |
| 1367 | |
| 1368 | // ---------------------------------------------------------------------------- |
| 1369 | // multiple selections emulation |
| 1370 | // ---------------------------------------------------------------------------- |
| 1371 | |
| 1372 | bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const |
| 1373 | { |
| 1374 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1375 | |
| 1376 | // receive the desired information. |
| 1377 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); |
| 1378 | DoGetItem(&tvItem); |
| 1379 | |
| 1380 | // state image indices are 1 based |
| 1381 | return ((tvItem.state >> 12) - 1) == 1; |
| 1382 | } |
| 1383 | |
| 1384 | void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check) |
| 1385 | { |
| 1386 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1387 | |
| 1388 | // receive the desired information. |
| 1389 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); |
| 1390 | |
| 1391 | DoGetItem(&tvItem); |
| 1392 | |
| 1393 | // state images are one-based |
| 1394 | tvItem.state = (check ? 2 : 1) << 12; |
| 1395 | |
| 1396 | DoSetItem(&tvItem); |
| 1397 | } |
| 1398 | |
| 1399 | size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const |
| 1400 | { |
| 1401 | TraverseSelections selector(this, selections); |
| 1402 | |
| 1403 | return selector.GetCount(); |
| 1404 | } |
| 1405 | |
| 1406 | // ---------------------------------------------------------------------------- |
| 1407 | // Usual operations |
| 1408 | // ---------------------------------------------------------------------------- |
| 1409 | |
| 1410 | wxTreeItemId wxTreeCtrl::DoInsertAfter(const wxTreeItemId& parent, |
| 1411 | const wxTreeItemId& hInsertAfter, |
| 1412 | const wxString& text, |
| 1413 | int image, int selectedImage, |
| 1414 | wxTreeItemData *data) |
| 1415 | { |
| 1416 | wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()), |
| 1417 | wxTreeItemId(), |
| 1418 | _T("can't have more than one root in the tree") ); |
| 1419 | |
| 1420 | TV_INSERTSTRUCT tvIns; |
| 1421 | tvIns.hParent = HITEM(parent); |
| 1422 | tvIns.hInsertAfter = HITEM(hInsertAfter); |
| 1423 | |
| 1424 | // this is how we insert the item as the first child: supply a NULL |
| 1425 | // hInsertAfter |
| 1426 | if ( !tvIns.hInsertAfter ) |
| 1427 | { |
| 1428 | tvIns.hInsertAfter = TVI_FIRST; |
| 1429 | } |
| 1430 | |
| 1431 | UINT mask = 0; |
| 1432 | if ( !text.empty() ) |
| 1433 | { |
| 1434 | mask |= TVIF_TEXT; |
| 1435 | tvIns.item.pszText = (wxChar *)text.wx_str(); // cast is ok |
| 1436 | } |
| 1437 | else |
| 1438 | { |
| 1439 | tvIns.item.pszText = NULL; |
| 1440 | tvIns.item.cchTextMax = 0; |
| 1441 | } |
| 1442 | |
| 1443 | // create the param which will store the other item parameters |
| 1444 | wxTreeItemParam *param = new wxTreeItemParam; |
| 1445 | |
| 1446 | // we return the images on demand as they depend on whether the item is |
| 1447 | // expanded or collapsed too in our case |
| 1448 | mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE; |
| 1449 | tvIns.item.iImage = I_IMAGECALLBACK; |
| 1450 | tvIns.item.iSelectedImage = I_IMAGECALLBACK; |
| 1451 | |
| 1452 | param->SetImage(image, wxTreeItemIcon_Normal); |
| 1453 | param->SetImage(selectedImage, wxTreeItemIcon_Selected); |
| 1454 | |
| 1455 | mask |= TVIF_PARAM; |
| 1456 | tvIns.item.lParam = (LPARAM)param; |
| 1457 | tvIns.item.mask = mask; |
| 1458 | |
| 1459 | HTREEITEM id = TreeView_InsertItem(GetHwnd(), &tvIns); |
| 1460 | if ( id == 0 ) |
| 1461 | { |
| 1462 | wxLogLastError(wxT("TreeView_InsertItem")); |
| 1463 | } |
| 1464 | |
| 1465 | // associate the application tree item with Win32 tree item handle |
| 1466 | param->SetItem(id); |
| 1467 | |
| 1468 | // setup wxTreeItemData |
| 1469 | if ( data != NULL ) |
| 1470 | { |
| 1471 | param->SetData(data); |
| 1472 | data->SetId(id); |
| 1473 | } |
| 1474 | |
| 1475 | return wxTreeItemId(id); |
| 1476 | } |
| 1477 | |
| 1478 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, |
| 1479 | int image, int selectedImage, |
| 1480 | wxTreeItemData *data) |
| 1481 | { |
| 1482 | if ( HasFlag(wxTR_HIDE_ROOT) ) |
| 1483 | { |
| 1484 | wxASSERT_MSG( !m_pVirtualRoot, _T("tree can have only a single root") ); |
| 1485 | |
| 1486 | // create a virtual root item, the parent for all the others |
| 1487 | wxTreeItemParam *param = new wxTreeItemParam; |
| 1488 | param->SetData(data); |
| 1489 | |
| 1490 | m_pVirtualRoot = new wxVirtualNode(param); |
| 1491 | |
| 1492 | return TVI_ROOT; |
| 1493 | } |
| 1494 | |
| 1495 | return DoInsertAfter(wxTreeItemId(), wxTreeItemId(), |
| 1496 | text, image, selectedImage, data); |
| 1497 | } |
| 1498 | |
| 1499 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent, |
| 1500 | size_t index, |
| 1501 | const wxString& text, |
| 1502 | int image, int selectedImage, |
| 1503 | wxTreeItemData *data) |
| 1504 | { |
| 1505 | wxTreeItemId idPrev; |
| 1506 | if ( index == (size_t)-1 ) |
| 1507 | { |
| 1508 | // special value: append to the end |
| 1509 | idPrev = TVI_LAST; |
| 1510 | } |
| 1511 | else // find the item from index |
| 1512 | { |
| 1513 | wxTreeItemIdValue cookie; |
| 1514 | wxTreeItemId idCur = GetFirstChild(parent, cookie); |
| 1515 | while ( index != 0 && idCur.IsOk() ) |
| 1516 | { |
| 1517 | index--; |
| 1518 | |
| 1519 | idPrev = idCur; |
| 1520 | idCur = GetNextChild(parent, cookie); |
| 1521 | } |
| 1522 | |
| 1523 | // assert, not check: if the index is invalid, we will append the item |
| 1524 | // to the end |
| 1525 | wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") ); |
| 1526 | } |
| 1527 | |
| 1528 | return DoInsertAfter(parent, idPrev, text, image, selectedImage, data); |
| 1529 | } |
| 1530 | |
| 1531 | void wxTreeCtrl::Delete(const wxTreeItemId& item) |
| 1532 | { |
| 1533 | if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) ) |
| 1534 | { |
| 1535 | wxLogLastError(wxT("TreeView_DeleteItem")); |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | // delete all children (but don't delete the item itself) |
| 1540 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item) |
| 1541 | { |
| 1542 | wxTreeItemIdValue cookie; |
| 1543 | |
| 1544 | wxArrayTreeItemIds children; |
| 1545 | wxTreeItemId child = GetFirstChild(item, cookie); |
| 1546 | while ( child.IsOk() ) |
| 1547 | { |
| 1548 | children.Add(child); |
| 1549 | |
| 1550 | child = GetNextChild(item, cookie); |
| 1551 | } |
| 1552 | |
| 1553 | size_t nCount = children.Count(); |
| 1554 | for ( size_t n = 0; n < nCount; n++ ) |
| 1555 | { |
| 1556 | if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children[n])) ) |
| 1557 | { |
| 1558 | wxLogLastError(wxT("TreeView_DeleteItem")); |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | void wxTreeCtrl::DeleteAllItems() |
| 1564 | { |
| 1565 | // delete the "virtual" root item. |
| 1566 | if ( GET_VIRTUAL_ROOT() ) |
| 1567 | { |
| 1568 | delete GET_VIRTUAL_ROOT(); |
| 1569 | m_pVirtualRoot = NULL; |
| 1570 | } |
| 1571 | |
| 1572 | // and all the real items |
| 1573 | |
| 1574 | if ( !TreeView_DeleteAllItems(GetHwnd()) ) |
| 1575 | { |
| 1576 | wxLogLastError(wxT("TreeView_DeleteAllItems")); |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) |
| 1581 | { |
| 1582 | wxASSERT_MSG( flag == TVE_COLLAPSE || |
| 1583 | flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) || |
| 1584 | flag == TVE_EXPAND || |
| 1585 | flag == TVE_TOGGLE, |
| 1586 | wxT("Unknown flag in wxTreeCtrl::DoExpand") ); |
| 1587 | |
| 1588 | // A hidden root can be neither expanded nor collapsed. |
| 1589 | wxCHECK_RET( !IsHiddenRoot(item), |
| 1590 | wxT("Can't expand/collapse hidden root node!") ); |
| 1591 | |
| 1592 | // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must |
| 1593 | // emulate them. This behaviour has changed slightly with comctl32.dll |
| 1594 | // v 4.70 - now it does send them but only the first time. To maintain |
| 1595 | // compatible behaviour and also in order to not have surprises with the |
| 1596 | // future versions, don't rely on this and still do everything ourselves. |
| 1597 | // To avoid that the messages be sent twice when the item is expanded for |
| 1598 | // the first time we must clear TVIS_EXPANDEDONCE style manually. |
| 1599 | |
| 1600 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE); |
| 1601 | tvItem.state = 0; |
| 1602 | DoSetItem(&tvItem); |
| 1603 | |
| 1604 | if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 ) |
| 1605 | { |
| 1606 | // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand() |
| 1607 | // itself |
| 1608 | wxTreeEvent event(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND |
| 1609 | : IDX_COLLAPSE] |
| 1610 | [IDX_DONE], |
| 1611 | this, item); |
| 1612 | (void)GetEventHandler()->ProcessEvent(event); |
| 1613 | } |
| 1614 | //else: change didn't took place, so do nothing at all |
| 1615 | } |
| 1616 | |
| 1617 | void wxTreeCtrl::Expand(const wxTreeItemId& item) |
| 1618 | { |
| 1619 | DoExpand(item, TVE_EXPAND); |
| 1620 | } |
| 1621 | |
| 1622 | void wxTreeCtrl::Collapse(const wxTreeItemId& item) |
| 1623 | { |
| 1624 | DoExpand(item, TVE_COLLAPSE); |
| 1625 | } |
| 1626 | |
| 1627 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) |
| 1628 | { |
| 1629 | DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET); |
| 1630 | } |
| 1631 | |
| 1632 | void wxTreeCtrl::Toggle(const wxTreeItemId& item) |
| 1633 | { |
| 1634 | DoExpand(item, TVE_TOGGLE); |
| 1635 | } |
| 1636 | |
| 1637 | void wxTreeCtrl::Unselect() |
| 1638 | { |
| 1639 | wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), |
| 1640 | wxT("doesn't make sense, may be you want UnselectAll()?") ); |
| 1641 | |
| 1642 | // just remove the selection |
| 1643 | SelectItem(wxTreeItemId()); |
| 1644 | } |
| 1645 | |
| 1646 | void wxTreeCtrl::UnselectAll() |
| 1647 | { |
| 1648 | if ( m_windowStyle & wxTR_MULTIPLE ) |
| 1649 | { |
| 1650 | wxArrayTreeItemIds selections; |
| 1651 | size_t count = GetSelections(selections); |
| 1652 | for ( size_t n = 0; n < count; n++ ) |
| 1653 | { |
| 1654 | ::UnselectItem(GetHwnd(), HITEM(selections[n])); |
| 1655 | } |
| 1656 | |
| 1657 | m_htSelStart.Unset(); |
| 1658 | } |
| 1659 | else |
| 1660 | { |
| 1661 | // just remove the selection |
| 1662 | Unselect(); |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select) |
| 1667 | { |
| 1668 | wxCHECK_RET( !IsHiddenRoot(item), _T("can't select hidden root item") ); |
| 1669 | |
| 1670 | wxASSERT_MSG( select || HasFlag(wxTR_MULTIPLE), |
| 1671 | _T("SelectItem(false) works only for multiselect") ); |
| 1672 | |
| 1673 | wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item); |
| 1674 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) |
| 1675 | { |
| 1676 | if ( HasFlag(wxTR_MULTIPLE) ) |
| 1677 | { |
| 1678 | if ( !::SelectItem(GetHwnd(), HITEM(item), select) ) |
| 1679 | { |
| 1680 | wxLogLastError(wxT("TreeView_SelectItem")); |
| 1681 | return; |
| 1682 | } |
| 1683 | } |
| 1684 | else // single selection |
| 1685 | { |
| 1686 | // use TreeView_SelectItem() to deselect the previous selection |
| 1687 | if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) ) |
| 1688 | { |
| 1689 | wxLogLastError(wxT("TreeView_SelectItem")); |
| 1690 | return; |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); |
| 1695 | (void)GetEventHandler()->ProcessEvent(event); |
| 1696 | } |
| 1697 | //else: program vetoed the change |
| 1698 | } |
| 1699 | |
| 1700 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) |
| 1701 | { |
| 1702 | wxCHECK_RET( !IsHiddenRoot(item), _T("can't show hidden root item") ); |
| 1703 | |
| 1704 | // no error return |
| 1705 | TreeView_EnsureVisible(GetHwnd(), HITEM(item)); |
| 1706 | } |
| 1707 | |
| 1708 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) |
| 1709 | { |
| 1710 | if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) ) |
| 1711 | { |
| 1712 | wxLogLastError(wxT("TreeView_SelectSetFirstVisible")); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | wxTextCtrl *wxTreeCtrl::GetEditControl() const |
| 1717 | { |
| 1718 | return m_textCtrl; |
| 1719 | } |
| 1720 | |
| 1721 | void wxTreeCtrl::DeleteTextCtrl() |
| 1722 | { |
| 1723 | if ( m_textCtrl ) |
| 1724 | { |
| 1725 | // the HWND corresponding to this control is deleted by the tree |
| 1726 | // control itself and we don't know when exactly this happens, so check |
| 1727 | // if the window still exists before calling UnsubclassWin() |
| 1728 | if ( !::IsWindow(GetHwndOf(m_textCtrl)) ) |
| 1729 | { |
| 1730 | m_textCtrl->SetHWND(0); |
| 1731 | } |
| 1732 | |
| 1733 | m_textCtrl->UnsubclassWin(); |
| 1734 | m_textCtrl->SetHWND(0); |
| 1735 | delete m_textCtrl; |
| 1736 | m_textCtrl = NULL; |
| 1737 | |
| 1738 | m_idEdited.Unset(); |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | wxTextCtrl *wxTreeCtrl::EditLabel(const wxTreeItemId& item, |
| 1743 | wxClassInfo *textControlClass) |
| 1744 | { |
| 1745 | wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); |
| 1746 | |
| 1747 | DeleteTextCtrl(); |
| 1748 | |
| 1749 | m_idEdited = item; |
| 1750 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); |
| 1751 | HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item)); |
| 1752 | |
| 1753 | // this is not an error - the TVN_BEGINLABELEDIT handler might have |
| 1754 | // returned false |
| 1755 | if ( !hWnd ) |
| 1756 | { |
| 1757 | delete m_textCtrl; |
| 1758 | m_textCtrl = NULL; |
| 1759 | return NULL; |
| 1760 | } |
| 1761 | |
| 1762 | // textctrl is subclassed in MSWOnNotify |
| 1763 | return m_textCtrl; |
| 1764 | } |
| 1765 | |
| 1766 | // End label editing, optionally cancelling the edit |
| 1767 | void wxTreeCtrl::DoEndEditLabel(bool discardChanges) |
| 1768 | { |
| 1769 | TreeView_EndEditLabelNow(GetHwnd(), discardChanges); |
| 1770 | |
| 1771 | DeleteTextCtrl(); |
| 1772 | } |
| 1773 | |
| 1774 | wxTreeItemId wxTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const |
| 1775 | { |
| 1776 | TV_HITTESTINFO hitTestInfo; |
| 1777 | hitTestInfo.pt.x = (int)point.x; |
| 1778 | hitTestInfo.pt.y = (int)point.y; |
| 1779 | |
| 1780 | (void) TreeView_HitTest(GetHwnd(), &hitTestInfo); |
| 1781 | |
| 1782 | flags = 0; |
| 1783 | |
| 1784 | // avoid repetition |
| 1785 | #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \ |
| 1786 | flags |= wxTREE_HITTEST_##flag |
| 1787 | |
| 1788 | TRANSLATE_FLAG(ABOVE); |
| 1789 | TRANSLATE_FLAG(BELOW); |
| 1790 | TRANSLATE_FLAG(NOWHERE); |
| 1791 | TRANSLATE_FLAG(ONITEMBUTTON); |
| 1792 | TRANSLATE_FLAG(ONITEMICON); |
| 1793 | TRANSLATE_FLAG(ONITEMINDENT); |
| 1794 | TRANSLATE_FLAG(ONITEMLABEL); |
| 1795 | TRANSLATE_FLAG(ONITEMRIGHT); |
| 1796 | TRANSLATE_FLAG(ONITEMSTATEICON); |
| 1797 | TRANSLATE_FLAG(TOLEFT); |
| 1798 | TRANSLATE_FLAG(TORIGHT); |
| 1799 | |
| 1800 | #undef TRANSLATE_FLAG |
| 1801 | |
| 1802 | return wxTreeItemId(hitTestInfo.hItem); |
| 1803 | } |
| 1804 | |
| 1805 | bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item, |
| 1806 | wxRect& rect, |
| 1807 | bool textOnly) const |
| 1808 | { |
| 1809 | RECT rc; |
| 1810 | |
| 1811 | // Virtual root items have no bounding rectangle |
| 1812 | if ( IS_VIRTUAL_ROOT(item) ) |
| 1813 | { |
| 1814 | return false; |
| 1815 | } |
| 1816 | |
| 1817 | if ( TreeView_GetItemRect(GetHwnd(), HITEM(item), |
| 1818 | &rc, textOnly) ) |
| 1819 | { |
| 1820 | rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom)); |
| 1821 | |
| 1822 | return true; |
| 1823 | } |
| 1824 | else |
| 1825 | { |
| 1826 | // couldn't retrieve rect: for example, item isn't visible |
| 1827 | return false; |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | // ---------------------------------------------------------------------------- |
| 1832 | // sorting stuff |
| 1833 | // ---------------------------------------------------------------------------- |
| 1834 | |
| 1835 | // this is just a tiny namespace which is friend to wxTreeCtrl and so can use |
| 1836 | // functions such as IsDataIndirect() |
| 1837 | class wxTreeSortHelper |
| 1838 | { |
| 1839 | public: |
| 1840 | static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree); |
| 1841 | |
| 1842 | private: |
| 1843 | static wxTreeItemId GetIdFromData(LPARAM lParam) |
| 1844 | { |
| 1845 | return ((wxTreeItemParam*)lParam)->GetItem(); |
| 1846 | } |
| 1847 | }; |
| 1848 | |
| 1849 | int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1, |
| 1850 | LPARAM pItem2, |
| 1851 | LPARAM htree) |
| 1852 | { |
| 1853 | wxCHECK_MSG( pItem1 && pItem2, 0, |
| 1854 | wxT("sorting tree without data doesn't make sense") ); |
| 1855 | |
| 1856 | wxTreeCtrl *tree = (wxTreeCtrl *)htree; |
| 1857 | |
| 1858 | return tree->OnCompareItems(GetIdFromData(pItem1), |
| 1859 | GetIdFromData(pItem2)); |
| 1860 | } |
| 1861 | |
| 1862 | void wxTreeCtrl::SortChildren(const wxTreeItemId& item) |
| 1863 | { |
| 1864 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1865 | |
| 1866 | // rely on the fact that TreeView_SortChildren does the same thing as our |
| 1867 | // default behaviour, i.e. sorts items alphabetically and so call it |
| 1868 | // directly if we're not in derived class (much more efficient!) |
| 1869 | // RN: Note that if you find you're code doesn't sort as expected this |
| 1870 | // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS |
| 1871 | // combo for your derived wxTreeCtrl if will sort without |
| 1872 | // OnCompareItems |
| 1873 | if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) ) |
| 1874 | { |
| 1875 | TreeView_SortChildren(GetHwnd(), HITEM(item), 0); |
| 1876 | } |
| 1877 | else |
| 1878 | { |
| 1879 | TV_SORTCB tvSort; |
| 1880 | tvSort.hParent = HITEM(item); |
| 1881 | tvSort.lpfnCompare = wxTreeSortHelper::Compare; |
| 1882 | tvSort.lParam = (LPARAM)this; |
| 1883 | TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */); |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | // ---------------------------------------------------------------------------- |
| 1888 | // implementation |
| 1889 | // ---------------------------------------------------------------------------- |
| 1890 | |
| 1891 | bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG* msg) |
| 1892 | { |
| 1893 | if ( msg->message == WM_KEYDOWN ) |
| 1894 | { |
| 1895 | if ( msg->wParam == VK_RETURN ) |
| 1896 | { |
| 1897 | // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED |
| 1898 | return false; |
| 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg); |
| 1903 | } |
| 1904 | |
| 1905 | bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id_) |
| 1906 | { |
| 1907 | const int id = (signed short)id_; |
| 1908 | |
| 1909 | if ( cmd == EN_UPDATE ) |
| 1910 | { |
| 1911 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); |
| 1912 | event.SetEventObject( this ); |
| 1913 | ProcessCommand(event); |
| 1914 | } |
| 1915 | else if ( cmd == EN_KILLFOCUS ) |
| 1916 | { |
| 1917 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); |
| 1918 | event.SetEventObject( this ); |
| 1919 | ProcessCommand(event); |
| 1920 | } |
| 1921 | else |
| 1922 | { |
| 1923 | // nothing done |
| 1924 | return false; |
| 1925 | } |
| 1926 | |
| 1927 | // command processed |
| 1928 | return true; |
| 1929 | } |
| 1930 | |
| 1931 | // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we |
| 1932 | // only do it during dragging, minimize wxWin overhead (this is important for |
| 1933 | // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly |
| 1934 | // instead of passing by wxWin events |
| 1935 | WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
| 1936 | { |
| 1937 | bool processed = false; |
| 1938 | WXLRESULT rc = 0; |
| 1939 | bool isMultiple = HasFlag(wxTR_MULTIPLE); |
| 1940 | |
| 1941 | // This message is sent after a right-click, or when the "menu" key is pressed |
| 1942 | if ( nMsg == WM_CONTEXTMENU ) |
| 1943 | { |
| 1944 | int x = GET_X_LPARAM(lParam), |
| 1945 | y = GET_Y_LPARAM(lParam); |
| 1946 | |
| 1947 | // the item for which the menu should be shown |
| 1948 | wxTreeItemId item; |
| 1949 | |
| 1950 | // the position where the menu should be shown in client coordinates |
| 1951 | // (so that it can be passed directly to PopupMenu()) |
| 1952 | wxPoint pt; |
| 1953 | |
| 1954 | if ( x == -1 || y == -1 ) |
| 1955 | { |
| 1956 | // this means that the event was generated from keyboard (e.g. with |
| 1957 | // Shift-F10 or special Windows menu key) |
| 1958 | // |
| 1959 | // use the Explorer standard of putting the menu at the left edge |
| 1960 | // of the text, in the vertical middle of the text |
| 1961 | item = wxTreeItemId(TreeView_GetSelection(GetHwnd())); |
| 1962 | if ( item.IsOk() ) |
| 1963 | { |
| 1964 | // Use the bounding rectangle of only the text part |
| 1965 | wxRect rect; |
| 1966 | GetBoundingRect(item, rect, true); |
| 1967 | pt = wxPoint(rect.GetX(), rect.GetY() + rect.GetHeight() / 2); |
| 1968 | } |
| 1969 | } |
| 1970 | else // event from mouse, use mouse position |
| 1971 | { |
| 1972 | pt = ScreenToClient(wxPoint(x, y)); |
| 1973 | |
| 1974 | TV_HITTESTINFO tvhti; |
| 1975 | tvhti.pt.x = pt.x; |
| 1976 | tvhti.pt.y = pt.y; |
| 1977 | if ( TreeView_HitTest(GetHwnd(), &tvhti) ) |
| 1978 | item = wxTreeItemId(tvhti.hItem); |
| 1979 | } |
| 1980 | |
| 1981 | // create the event |
| 1982 | wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_MENU, this, item); |
| 1983 | |
| 1984 | event.m_pointDrag = pt; |
| 1985 | |
| 1986 | if ( GetEventHandler()->ProcessEvent(event) ) |
| 1987 | processed = true; |
| 1988 | //else: continue with generating wxEVT_CONTEXT_MENU in base class code |
| 1989 | } |
| 1990 | else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) ) |
| 1991 | { |
| 1992 | // we only process mouse messages here and these parameters have the |
| 1993 | // same meaning for all of them |
| 1994 | int x = GET_X_LPARAM(lParam), |
| 1995 | y = GET_Y_LPARAM(lParam); |
| 1996 | |
| 1997 | TV_HITTESTINFO tvht; |
| 1998 | tvht.pt.x = x; |
| 1999 | tvht.pt.y = y; |
| 2000 | |
| 2001 | HTREEITEM htItem = TreeView_HitTest(GetHwnd(), &tvht); |
| 2002 | |
| 2003 | switch ( nMsg ) |
| 2004 | { |
| 2005 | case WM_LBUTTONDOWN: |
| 2006 | if ( htItem && isMultiple && (tvht.flags & TVHT_ONITEM) != 0 ) |
| 2007 | { |
| 2008 | m_htClickedItem = (WXHTREEITEM) htItem; |
| 2009 | m_ptClick = wxPoint(x, y); |
| 2010 | |
| 2011 | if ( wParam & MK_CONTROL ) |
| 2012 | { |
| 2013 | SetFocus(); |
| 2014 | |
| 2015 | // toggle selected state |
| 2016 | ToggleItemSelection(htItem); |
| 2017 | |
| 2018 | ::SetFocus(GetHwnd(), htItem); |
| 2019 | |
| 2020 | // reset on any click without Shift |
| 2021 | m_htSelStart.Unset(); |
| 2022 | |
| 2023 | processed = true; |
| 2024 | } |
| 2025 | else if ( wParam & MK_SHIFT ) |
| 2026 | { |
| 2027 | // this selects all items between the starting one and |
| 2028 | // the current |
| 2029 | |
| 2030 | if ( !m_htSelStart ) |
| 2031 | { |
| 2032 | // take the focused item |
| 2033 | m_htSelStart = TreeView_GetSelection(GetHwnd()); |
| 2034 | } |
| 2035 | |
| 2036 | if ( m_htSelStart ) |
| 2037 | SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem, |
| 2038 | !(wParam & MK_CONTROL)); |
| 2039 | else |
| 2040 | ::SelectItem(GetHwnd(), htItem); |
| 2041 | |
| 2042 | ::SetFocus(GetHwnd(), htItem); |
| 2043 | |
| 2044 | processed = true; |
| 2045 | } |
| 2046 | else // normal click |
| 2047 | { |
| 2048 | // avoid doing anything if we click on the only |
| 2049 | // currently selected item |
| 2050 | |
| 2051 | SetFocus(); |
| 2052 | |
| 2053 | wxArrayTreeItemIds selections; |
| 2054 | size_t count = GetSelections(selections); |
| 2055 | if ( count == 0 || |
| 2056 | count > 1 || |
| 2057 | HITEM(selections[0]) != htItem ) |
| 2058 | { |
| 2059 | // clear the previously selected items, if the |
| 2060 | // user clicked outside of the present selection. |
| 2061 | // otherwise, perform the deselection on mouse-up. |
| 2062 | // this allows multiple drag and drop to work. |
| 2063 | |
| 2064 | if (!IsItemSelected(GetHwnd(), htItem)) |
| 2065 | { |
| 2066 | UnselectAll(); |
| 2067 | |
| 2068 | // prevent the click from starting in-place editing |
| 2069 | // which should only happen if we click on the |
| 2070 | // already selected item (and nothing else is |
| 2071 | // selected) |
| 2072 | |
| 2073 | TreeView_SelectItem(GetHwnd(), 0); |
| 2074 | ::SelectItem(GetHwnd(), htItem); |
| 2075 | } |
| 2076 | ::SetFocus(GetHwnd(), htItem); |
| 2077 | processed = true; |
| 2078 | } |
| 2079 | else // click on a single selected item |
| 2080 | { |
| 2081 | // don't interfere with the default processing in |
| 2082 | // WM_MOUSEMOVE handler below as the default window |
| 2083 | // proc will start the drag itself if we let have |
| 2084 | // WM_LBUTTONDOWN |
| 2085 | m_htClickedItem.Unset(); |
| 2086 | } |
| 2087 | |
| 2088 | // reset on any click without Shift |
| 2089 | m_htSelStart.Unset(); |
| 2090 | } |
| 2091 | } |
| 2092 | break; |
| 2093 | |
| 2094 | case WM_MOUSEMOVE: |
| 2095 | #ifndef __WXWINCE__ |
| 2096 | if ( m_htClickedItem ) |
| 2097 | { |
| 2098 | int cx = abs(m_ptClick.x - x); |
| 2099 | int cy = abs(m_ptClick.y - y); |
| 2100 | |
| 2101 | if ( cx > ::GetSystemMetrics(SM_CXDRAG) || |
| 2102 | cy > ::GetSystemMetrics(SM_CYDRAG) ) |
| 2103 | { |
| 2104 | NM_TREEVIEW tv; |
| 2105 | wxZeroMemory(tv); |
| 2106 | |
| 2107 | tv.hdr.hwndFrom = GetHwnd(); |
| 2108 | tv.hdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID); |
| 2109 | tv.hdr.code = TVN_BEGINDRAG; |
| 2110 | |
| 2111 | tv.itemNew.hItem = HITEM(m_htClickedItem); |
| 2112 | |
| 2113 | |
| 2114 | TVITEM tviAux; |
| 2115 | wxZeroMemory(tviAux); |
| 2116 | |
| 2117 | tviAux.hItem = HITEM(m_htClickedItem); |
| 2118 | tviAux.mask = TVIF_STATE | TVIF_PARAM; |
| 2119 | tviAux.stateMask = 0xffffffff; |
| 2120 | TreeView_GetItem(GetHwnd(), &tviAux); |
| 2121 | |
| 2122 | tv.itemNew.state = tviAux.state; |
| 2123 | tv.itemNew.lParam = tviAux.lParam; |
| 2124 | |
| 2125 | tv.ptDrag.x = x; |
| 2126 | tv.ptDrag.y = y; |
| 2127 | |
| 2128 | // do it before SendMessage() call below to avoid |
| 2129 | // reentrancies here if there is another WM_MOUSEMOVE |
| 2130 | // in the queue already |
| 2131 | m_htClickedItem.Unset(); |
| 2132 | |
| 2133 | ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY, |
| 2134 | tv.hdr.idFrom, (LPARAM)&tv ); |
| 2135 | |
| 2136 | // don't pass it to the default window proc, it would |
| 2137 | // start dragging again |
| 2138 | processed = true; |
| 2139 | } |
| 2140 | } |
| 2141 | #endif // __WXWINCE__ |
| 2142 | |
| 2143 | #if wxUSE_DRAGIMAGE |
| 2144 | if ( m_dragImage ) |
| 2145 | { |
| 2146 | m_dragImage->Move(wxPoint(x, y)); |
| 2147 | if ( htItem ) |
| 2148 | { |
| 2149 | // highlight the item as target (hiding drag image is |
| 2150 | // necessary - otherwise the display will be corrupted) |
| 2151 | m_dragImage->Hide(); |
| 2152 | TreeView_SelectDropTarget(GetHwnd(), htItem); |
| 2153 | m_dragImage->Show(); |
| 2154 | } |
| 2155 | } |
| 2156 | #endif // wxUSE_DRAGIMAGE |
| 2157 | break; |
| 2158 | |
| 2159 | case WM_LBUTTONUP: |
| 2160 | |
| 2161 | // facilitates multiple drag-and-drop |
| 2162 | if (htItem && isMultiple) |
| 2163 | { |
| 2164 | wxArrayTreeItemIds selections; |
| 2165 | size_t count = GetSelections(selections); |
| 2166 | |
| 2167 | if (count > 1 && |
| 2168 | !(wParam & MK_CONTROL) && |
| 2169 | !(wParam & MK_SHIFT)) |
| 2170 | { |
| 2171 | UnselectAll(); |
| 2172 | TreeView_SelectItem(GetHwnd(), htItem); |
| 2173 | ::SelectItem(GetHwnd(), htItem); |
| 2174 | ::SetFocus(GetHwnd(), htItem); |
| 2175 | } |
| 2176 | m_htClickedItem.Unset(); |
| 2177 | } |
| 2178 | |
| 2179 | // fall through |
| 2180 | |
| 2181 | case WM_RBUTTONUP: |
| 2182 | #if wxUSE_DRAGIMAGE |
| 2183 | if ( m_dragImage ) |
| 2184 | { |
| 2185 | m_dragImage->EndDrag(); |
| 2186 | delete m_dragImage; |
| 2187 | m_dragImage = NULL; |
| 2188 | |
| 2189 | // generate the drag end event |
| 2190 | wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, this, htItem); |
| 2191 | event.m_pointDrag = wxPoint(x, y); |
| 2192 | |
| 2193 | (void)GetEventHandler()->ProcessEvent(event); |
| 2194 | |
| 2195 | // if we don't do it, the tree seems to think that 2 items |
| 2196 | // are selected simultaneously which is quite weird |
| 2197 | TreeView_SelectDropTarget(GetHwnd(), 0); |
| 2198 | } |
| 2199 | #endif // wxUSE_DRAGIMAGE |
| 2200 | break; |
| 2201 | } |
| 2202 | } |
| 2203 | else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple ) |
| 2204 | { |
| 2205 | // the tree control greys out the selected item when it loses focus and |
| 2206 | // paints it as selected again when it regains it, but it won't do it |
| 2207 | // for the other items itself - help it |
| 2208 | wxArrayTreeItemIds selections; |
| 2209 | size_t count = GetSelections(selections); |
| 2210 | RECT rect; |
| 2211 | for ( size_t n = 0; n < count; n++ ) |
| 2212 | { |
| 2213 | // TreeView_GetItemRect() will return false if item is not visible, |
| 2214 | // which may happen perfectly well |
| 2215 | if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]), |
| 2216 | &rect, TRUE) ) |
| 2217 | { |
| 2218 | ::InvalidateRect(GetHwnd(), &rect, FALSE); |
| 2219 | } |
| 2220 | } |
| 2221 | } |
| 2222 | else if ( nMsg == WM_KEYDOWN && isMultiple ) |
| 2223 | { |
| 2224 | bool bCtrl = wxIsCtrlDown(), |
| 2225 | bShift = wxIsShiftDown(); |
| 2226 | |
| 2227 | HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd()); |
| 2228 | switch ( wParam ) |
| 2229 | { |
| 2230 | case VK_SPACE: |
| 2231 | if ( bCtrl ) |
| 2232 | { |
| 2233 | ToggleItemSelection(htSel); |
| 2234 | } |
| 2235 | else |
| 2236 | { |
| 2237 | UnselectAll(); |
| 2238 | |
| 2239 | ::SelectItem(GetHwnd(), htSel); |
| 2240 | } |
| 2241 | |
| 2242 | processed = true; |
| 2243 | break; |
| 2244 | |
| 2245 | case VK_UP: |
| 2246 | case VK_DOWN: |
| 2247 | if ( !bCtrl && !bShift ) |
| 2248 | { |
| 2249 | // no modifiers, just clear selection and then let the default |
| 2250 | // processing to take place |
| 2251 | UnselectAll(); |
| 2252 | } |
| 2253 | else if ( htSel ) |
| 2254 | { |
| 2255 | (void)wxControl::MSWWindowProc(nMsg, wParam, lParam); |
| 2256 | |
| 2257 | HTREEITEM htNext = (HTREEITEM) |
| 2258 | TreeView_GetNextItem |
| 2259 | ( |
| 2260 | GetHwnd(), |
| 2261 | htSel, |
| 2262 | wParam == VK_UP ? TVGN_PREVIOUSVISIBLE |
| 2263 | : TVGN_NEXTVISIBLE |
| 2264 | ); |
| 2265 | |
| 2266 | if ( !htNext ) |
| 2267 | { |
| 2268 | // at the top/bottom |
| 2269 | htNext = htSel; |
| 2270 | } |
| 2271 | |
| 2272 | if ( bShift ) |
| 2273 | { |
| 2274 | if ( !m_htSelStart ) |
| 2275 | m_htSelStart = htSel; |
| 2276 | |
| 2277 | SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext); |
| 2278 | } |
| 2279 | else // bCtrl |
| 2280 | { |
| 2281 | // without changing selection |
| 2282 | ::SetFocus(GetHwnd(), htNext); |
| 2283 | } |
| 2284 | |
| 2285 | processed = true; |
| 2286 | } |
| 2287 | break; |
| 2288 | |
| 2289 | case VK_HOME: |
| 2290 | case VK_END: |
| 2291 | case VK_PRIOR: |
| 2292 | case VK_NEXT: |
| 2293 | // TODO: handle Shift/Ctrl with these keys |
| 2294 | if ( !bCtrl && !bShift ) |
| 2295 | { |
| 2296 | UnselectAll(); |
| 2297 | |
| 2298 | m_htSelStart.Unset(); |
| 2299 | } |
| 2300 | } |
| 2301 | } |
| 2302 | else if ( nMsg == WM_COMMAND ) |
| 2303 | { |
| 2304 | // if we receive a EN_KILLFOCUS command from the in-place edit control |
| 2305 | // used for label editing, make sure to end editing |
| 2306 | WORD id, cmd; |
| 2307 | WXHWND hwnd; |
| 2308 | UnpackCommand(wParam, lParam, &id, &hwnd, &cmd); |
| 2309 | |
| 2310 | if ( cmd == EN_KILLFOCUS ) |
| 2311 | { |
| 2312 | if ( m_textCtrl && m_textCtrl->GetHandle() == hwnd ) |
| 2313 | { |
| 2314 | DoEndEditLabel(); |
| 2315 | |
| 2316 | processed = true; |
| 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | if ( !processed ) |
| 2322 | rc = wxControl::MSWWindowProc(nMsg, wParam, lParam); |
| 2323 | |
| 2324 | return rc; |
| 2325 | } |
| 2326 | |
| 2327 | WXLRESULT |
| 2328 | wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
| 2329 | { |
| 2330 | if ( nMsg == WM_CHAR ) |
| 2331 | { |
| 2332 | // don't let the control process Space and Return keys because it |
| 2333 | // doesn't do anything useful with them anyhow but always beeps |
| 2334 | // annoyingly when it receives them and there is no way to turn it off |
| 2335 | // simply if you just process TREEITEM_ACTIVATED event to which Space |
| 2336 | // and Enter presses are mapped in your code |
| 2337 | if ( wParam == VK_SPACE || wParam == VK_RETURN ) |
| 2338 | return 0; |
| 2339 | } |
| 2340 | #if wxUSE_DRAGIMAGE |
| 2341 | else if ( nMsg == WM_KEYDOWN ) |
| 2342 | { |
| 2343 | if ( wParam == VK_ESCAPE ) |
| 2344 | { |
| 2345 | if ( m_dragImage ) |
| 2346 | { |
| 2347 | m_dragImage->EndDrag(); |
| 2348 | delete m_dragImage; |
| 2349 | m_dragImage = NULL; |
| 2350 | |
| 2351 | // if we don't do it, the tree seems to think that 2 items |
| 2352 | // are selected simultaneously which is quite weird |
| 2353 | TreeView_SelectDropTarget(GetHwnd(), 0); |
| 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | #endif // wxUSE_DRAGIMAGE |
| 2358 | |
| 2359 | return wxControl::MSWDefWindowProc(nMsg, wParam, lParam); |
| 2360 | } |
| 2361 | |
| 2362 | // process WM_NOTIFY Windows message |
| 2363 | bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
| 2364 | { |
| 2365 | wxTreeEvent event(wxEVT_NULL, this); |
| 2366 | wxEventType eventType = wxEVT_NULL; |
| 2367 | NMHDR *hdr = (NMHDR *)lParam; |
| 2368 | |
| 2369 | switch ( hdr->code ) |
| 2370 | { |
| 2371 | case TVN_BEGINDRAG: |
| 2372 | eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; |
| 2373 | // fall through |
| 2374 | |
| 2375 | case TVN_BEGINRDRAG: |
| 2376 | { |
| 2377 | if ( eventType == wxEVT_NULL ) |
| 2378 | eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; |
| 2379 | //else: left drag, already set above |
| 2380 | |
| 2381 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; |
| 2382 | |
| 2383 | event.m_item = tv->itemNew.hItem; |
| 2384 | event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y); |
| 2385 | |
| 2386 | // don't allow dragging by default: the user code must |
| 2387 | // explicitly say that it wants to allow it to avoid breaking |
| 2388 | // the old apps |
| 2389 | event.Veto(); |
| 2390 | } |
| 2391 | break; |
| 2392 | |
| 2393 | case TVN_BEGINLABELEDIT: |
| 2394 | { |
| 2395 | eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; |
| 2396 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; |
| 2397 | |
| 2398 | // although the user event handler may still veto it, it is |
| 2399 | // important to set it now so that calls to SetItemText() from |
| 2400 | // the event handler would change the text controls contents |
| 2401 | m_idEdited = |
| 2402 | event.m_item = info->item.hItem; |
| 2403 | event.m_label = info->item.pszText; |
| 2404 | event.m_editCancelled = false; |
| 2405 | } |
| 2406 | break; |
| 2407 | |
| 2408 | case TVN_DELETEITEM: |
| 2409 | { |
| 2410 | eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; |
| 2411 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; |
| 2412 | |
| 2413 | event.m_item = tv->itemOld.hItem; |
| 2414 | |
| 2415 | if ( m_hasAnyAttr ) |
| 2416 | { |
| 2417 | wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem); |
| 2418 | if ( it != m_attrs.end() ) |
| 2419 | { |
| 2420 | delete it->second; |
| 2421 | m_attrs.erase(it); |
| 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | break; |
| 2426 | |
| 2427 | case TVN_ENDLABELEDIT: |
| 2428 | { |
| 2429 | eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; |
| 2430 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; |
| 2431 | |
| 2432 | event.m_item = info->item.hItem; |
| 2433 | event.m_label = info->item.pszText; |
| 2434 | event.m_editCancelled = info->item.pszText == NULL; |
| 2435 | break; |
| 2436 | } |
| 2437 | |
| 2438 | #ifndef __WXWINCE__ |
| 2439 | // These *must* not be removed or TVN_GETINFOTIP will |
| 2440 | // not be processed each time the mouse is moved |
| 2441 | // and the tooltip will only ever update once. |
| 2442 | case TTN_NEEDTEXTA: |
| 2443 | case TTN_NEEDTEXTW: |
| 2444 | { |
| 2445 | *result = 0; |
| 2446 | |
| 2447 | break; |
| 2448 | } |
| 2449 | |
| 2450 | #ifdef TVN_GETINFOTIP |
| 2451 | case TVN_GETINFOTIP: |
| 2452 | { |
| 2453 | eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP; |
| 2454 | NMTVGETINFOTIP *info = (NMTVGETINFOTIP*)lParam; |
| 2455 | |
| 2456 | // Which item are we trying to get a tooltip for? |
| 2457 | event.m_item = info->hItem; |
| 2458 | |
| 2459 | break; |
| 2460 | } |
| 2461 | #endif // TVN_GETINFOTIP |
| 2462 | #endif // !__WXWINCE__ |
| 2463 | |
| 2464 | case TVN_GETDISPINFO: |
| 2465 | eventType = wxEVT_COMMAND_TREE_GET_INFO; |
| 2466 | // fall through |
| 2467 | |
| 2468 | case TVN_SETDISPINFO: |
| 2469 | { |
| 2470 | if ( eventType == wxEVT_NULL ) |
| 2471 | eventType = wxEVT_COMMAND_TREE_SET_INFO; |
| 2472 | //else: get, already set above |
| 2473 | |
| 2474 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; |
| 2475 | |
| 2476 | event.m_item = info->item.hItem; |
| 2477 | break; |
| 2478 | } |
| 2479 | |
| 2480 | case TVN_ITEMEXPANDING: |
| 2481 | case TVN_ITEMEXPANDED: |
| 2482 | { |
| 2483 | NM_TREEVIEW *tv = (NM_TREEVIEW*)lParam; |
| 2484 | |
| 2485 | int what; |
| 2486 | switch ( tv->action ) |
| 2487 | { |
| 2488 | default: |
| 2489 | wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action); |
| 2490 | // fall through |
| 2491 | |
| 2492 | case TVE_EXPAND: |
| 2493 | what = IDX_EXPAND; |
| 2494 | break; |
| 2495 | |
| 2496 | case TVE_COLLAPSE: |
| 2497 | what = IDX_COLLAPSE; |
| 2498 | break; |
| 2499 | } |
| 2500 | |
| 2501 | int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING |
| 2502 | : IDX_DONE; |
| 2503 | |
| 2504 | eventType = gs_expandEvents[what][how]; |
| 2505 | |
| 2506 | event.m_item = tv->itemNew.hItem; |
| 2507 | } |
| 2508 | break; |
| 2509 | |
| 2510 | case TVN_KEYDOWN: |
| 2511 | { |
| 2512 | eventType = wxEVT_COMMAND_TREE_KEY_DOWN; |
| 2513 | TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; |
| 2514 | |
| 2515 | // fabricate the lParam and wParam parameters sufficiently |
| 2516 | // similar to the ones from a "real" WM_KEYDOWN so that |
| 2517 | // CreateKeyEvent() works correctly |
| 2518 | const bool isAltDown = ::GetKeyState(VK_MENU) < 0; |
| 2519 | WXLPARAM lParam = (isAltDown ? KF_ALTDOWN : 0) << 16; |
| 2520 | |
| 2521 | WXWPARAM wParam = info->wVKey; |
| 2522 | |
| 2523 | int keyCode = wxCharCodeMSWToWX(wParam); |
| 2524 | if ( !keyCode ) |
| 2525 | { |
| 2526 | // wxCharCodeMSWToWX() returns 0 to indicate that this is a |
| 2527 | // simple ASCII key |
| 2528 | keyCode = wParam; |
| 2529 | } |
| 2530 | |
| 2531 | event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN, |
| 2532 | keyCode, |
| 2533 | lParam, |
| 2534 | wParam); |
| 2535 | |
| 2536 | // a separate event for Space/Return |
| 2537 | if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown && |
| 2538 | ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) ) |
| 2539 | { |
| 2540 | wxTreeItemId item; |
| 2541 | if ( !HasFlag(wxTR_MULTIPLE) ) |
| 2542 | item = GetSelection(); |
| 2543 | |
| 2544 | wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, |
| 2545 | this, item); |
| 2546 | (void)GetEventHandler()->ProcessEvent(event2); |
| 2547 | } |
| 2548 | } |
| 2549 | break; |
| 2550 | |
| 2551 | // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of |
| 2552 | // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore |
| 2553 | // we have to handle both messages: |
| 2554 | case TVN_SELCHANGEDA: |
| 2555 | case TVN_SELCHANGEDW: |
| 2556 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; |
| 2557 | // fall through |
| 2558 | |
| 2559 | case TVN_SELCHANGINGA: |
| 2560 | case TVN_SELCHANGINGW: |
| 2561 | { |
| 2562 | if ( eventType == wxEVT_NULL ) |
| 2563 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; |
| 2564 | //else: already set above |
| 2565 | |
| 2566 | if (hdr->code == TVN_SELCHANGINGW || |
| 2567 | hdr->code == TVN_SELCHANGEDW) |
| 2568 | { |
| 2569 | NM_TREEVIEWW *tv = (NM_TREEVIEWW *)lParam; |
| 2570 | event.m_item = tv->itemNew.hItem; |
| 2571 | event.m_itemOld = tv->itemOld.hItem; |
| 2572 | } |
| 2573 | else |
| 2574 | { |
| 2575 | NM_TREEVIEWA *tv = (NM_TREEVIEWA *)lParam; |
| 2576 | event.m_item = tv->itemNew.hItem; |
| 2577 | event.m_itemOld = tv->itemOld.hItem; |
| 2578 | } |
| 2579 | } |
| 2580 | break; |
| 2581 | |
| 2582 | // instead of explicitly checking for _WIN32_IE, check if the |
| 2583 | // required symbols are available in the headers |
| 2584 | #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY |
| 2585 | case NM_CUSTOMDRAW: |
| 2586 | { |
| 2587 | LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam; |
| 2588 | NMCUSTOMDRAW& nmcd = lptvcd->nmcd; |
| 2589 | switch ( nmcd.dwDrawStage ) |
| 2590 | { |
| 2591 | case CDDS_PREPAINT: |
| 2592 | // if we've got any items with non standard attributes, |
| 2593 | // notify us before painting each item |
| 2594 | *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW |
| 2595 | : CDRF_DODEFAULT; |
| 2596 | break; |
| 2597 | |
| 2598 | case CDDS_ITEMPREPAINT: |
| 2599 | { |
| 2600 | wxMapTreeAttr::iterator |
| 2601 | it = m_attrs.find((void *)nmcd.dwItemSpec); |
| 2602 | |
| 2603 | if ( it == m_attrs.end() ) |
| 2604 | { |
| 2605 | // nothing to do for this item |
| 2606 | *result = CDRF_DODEFAULT; |
| 2607 | break; |
| 2608 | } |
| 2609 | |
| 2610 | wxTreeItemAttr * const attr = it->second; |
| 2611 | |
| 2612 | wxTreeViewItem tvItem((void *)nmcd.dwItemSpec, |
| 2613 | TVIF_STATE, TVIS_DROPHILITED); |
| 2614 | DoGetItem(&tvItem); |
| 2615 | const UINT tvItemState = tvItem.state; |
| 2616 | |
| 2617 | // selection colours should override ours, |
| 2618 | // otherwise it is too confusing to the user |
| 2619 | if ( !(nmcd.uItemState & CDIS_SELECTED) && |
| 2620 | !(tvItemState & TVIS_DROPHILITED) ) |
| 2621 | { |
| 2622 | wxColour colBack; |
| 2623 | if ( attr->HasBackgroundColour() ) |
| 2624 | { |
| 2625 | colBack = attr->GetBackgroundColour(); |
| 2626 | lptvcd->clrTextBk = wxColourToRGB(colBack); |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | // but we still want to keep the special foreground |
| 2631 | // colour when we don't have focus (we can't keep |
| 2632 | // it when we do, it would usually be unreadable on |
| 2633 | // the almost inverted bg colour...) |
| 2634 | if ( ( !(nmcd.uItemState & CDIS_SELECTED) || |
| 2635 | FindFocus() != this ) && |
| 2636 | !(tvItemState & TVIS_DROPHILITED) ) |
| 2637 | { |
| 2638 | wxColour colText; |
| 2639 | if ( attr->HasTextColour() ) |
| 2640 | { |
| 2641 | colText = attr->GetTextColour(); |
| 2642 | lptvcd->clrText = wxColourToRGB(colText); |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | if ( attr->HasFont() ) |
| 2647 | { |
| 2648 | HFONT hFont = GetHfontOf(attr->GetFont()); |
| 2649 | |
| 2650 | ::SelectObject(nmcd.hdc, hFont); |
| 2651 | |
| 2652 | *result = CDRF_NEWFONT; |
| 2653 | } |
| 2654 | else // no specific font |
| 2655 | { |
| 2656 | *result = CDRF_DODEFAULT; |
| 2657 | } |
| 2658 | } |
| 2659 | break; |
| 2660 | |
| 2661 | default: |
| 2662 | *result = CDRF_DODEFAULT; |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | // we always process it |
| 2667 | return true; |
| 2668 | #endif // have owner drawn support in headers |
| 2669 | |
| 2670 | case NM_CLICK: |
| 2671 | { |
| 2672 | DWORD pos = GetMessagePos(); |
| 2673 | POINT point; |
| 2674 | point.x = LOWORD(pos); |
| 2675 | point.y = HIWORD(pos); |
| 2676 | ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1); |
| 2677 | int flags = 0; |
| 2678 | wxTreeItemId item = HitTest(wxPoint(point.x, point.y), flags); |
| 2679 | if (flags & wxTREE_HITTEST_ONITEMSTATEICON) |
| 2680 | { |
| 2681 | event.m_item = item; |
| 2682 | eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK; |
| 2683 | } |
| 2684 | break; |
| 2685 | } |
| 2686 | |
| 2687 | case NM_DBLCLK: |
| 2688 | case NM_RCLICK: |
| 2689 | { |
| 2690 | TV_HITTESTINFO tvhti; |
| 2691 | ::GetCursorPos(&tvhti.pt); |
| 2692 | ::ScreenToClient(GetHwnd(), &tvhti.pt); |
| 2693 | if ( TreeView_HitTest(GetHwnd(), &tvhti) ) |
| 2694 | { |
| 2695 | if ( tvhti.flags & TVHT_ONITEM ) |
| 2696 | { |
| 2697 | event.m_item = tvhti.hItem; |
| 2698 | eventType = (int)hdr->code == NM_DBLCLK |
| 2699 | ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED |
| 2700 | : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK; |
| 2701 | |
| 2702 | event.m_pointDrag.x = tvhti.pt.x; |
| 2703 | event.m_pointDrag.y = tvhti.pt.y; |
| 2704 | } |
| 2705 | |
| 2706 | break; |
| 2707 | } |
| 2708 | } |
| 2709 | // fall through |
| 2710 | |
| 2711 | default: |
| 2712 | return wxControl::MSWOnNotify(idCtrl, lParam, result); |
| 2713 | } |
| 2714 | |
| 2715 | event.SetEventType(eventType); |
| 2716 | |
| 2717 | if ( event.m_item.IsOk() ) |
| 2718 | event.SetClientObject(GetItemData(event.m_item)); |
| 2719 | |
| 2720 | bool processed = GetEventHandler()->ProcessEvent(event); |
| 2721 | |
| 2722 | // post processing |
| 2723 | switch ( hdr->code ) |
| 2724 | { |
| 2725 | case NM_DBLCLK: |
| 2726 | // we translate NM_DBLCLK into ACTIVATED event, so don't interpret |
| 2727 | // the return code of this event handler as the return value for |
| 2728 | // NM_DBLCLK - otherwise, double clicking the item to toggle its |
| 2729 | // expanded status would never work |
| 2730 | *result = false; |
| 2731 | break; |
| 2732 | |
| 2733 | case NM_RCLICK: |
| 2734 | // prevent tree control from sending WM_CONTEXTMENU to our parent |
| 2735 | // (which it does if NM_RCLICK is not handled) because we want to |
| 2736 | // send it to the control itself |
| 2737 | *result = |
| 2738 | processed = true; |
| 2739 | |
| 2740 | ::SendMessage(GetHwnd(), WM_CONTEXTMENU, |
| 2741 | (WPARAM)GetHwnd(), ::GetMessagePos()); |
| 2742 | break; |
| 2743 | |
| 2744 | case TVN_BEGINDRAG: |
| 2745 | case TVN_BEGINRDRAG: |
| 2746 | #if wxUSE_DRAGIMAGE |
| 2747 | if ( event.IsAllowed() ) |
| 2748 | { |
| 2749 | // normally this is impossible because the m_dragImage is |
| 2750 | // deleted once the drag operation is over |
| 2751 | wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") ); |
| 2752 | |
| 2753 | m_dragImage = new wxDragImage(*this, event.m_item); |
| 2754 | m_dragImage->BeginDrag(wxPoint(0,0), this); |
| 2755 | m_dragImage->Show(); |
| 2756 | } |
| 2757 | #endif // wxUSE_DRAGIMAGE |
| 2758 | break; |
| 2759 | |
| 2760 | case TVN_DELETEITEM: |
| 2761 | { |
| 2762 | // NB: we might process this message using wxWidgets event |
| 2763 | // tables, but due to overhead of wxWin event system we |
| 2764 | // prefer to do it here ourself (otherwise deleting a tree |
| 2765 | // with many items is just too slow) |
| 2766 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; |
| 2767 | |
| 2768 | wxTreeItemParam *param = |
| 2769 | (wxTreeItemParam *)tv->itemOld.lParam; |
| 2770 | delete param; |
| 2771 | |
| 2772 | processed = true; // Make sure we don't get called twice |
| 2773 | } |
| 2774 | break; |
| 2775 | |
| 2776 | case TVN_BEGINLABELEDIT: |
| 2777 | // return true to cancel label editing |
| 2778 | *result = !event.IsAllowed(); |
| 2779 | |
| 2780 | // set ES_WANTRETURN ( like we do in BeginLabelEdit ) |
| 2781 | if ( event.IsAllowed() ) |
| 2782 | { |
| 2783 | HWND hText = TreeView_GetEditControl(GetHwnd()); |
| 2784 | if ( hText ) |
| 2785 | { |
| 2786 | // MBN: if m_textCtrl already has an HWND, it is a stale |
| 2787 | // pointer from a previous edit (because the user |
| 2788 | // didn't modify the label before dismissing the control, |
| 2789 | // and TVN_ENDLABELEDIT was not sent), so delete it |
| 2790 | if ( m_textCtrl && m_textCtrl->GetHWND() ) |
| 2791 | DeleteTextCtrl(); |
| 2792 | if ( !m_textCtrl ) |
| 2793 | m_textCtrl = new wxTextCtrl(); |
| 2794 | m_textCtrl->SetParent(this); |
| 2795 | m_textCtrl->SetHWND((WXHWND)hText); |
| 2796 | m_textCtrl->SubclassWin((WXHWND)hText); |
| 2797 | |
| 2798 | // set wxTE_PROCESS_ENTER style for the text control to |
| 2799 | // force it to process the Enter presses itself, otherwise |
| 2800 | // they could be stolen from it by the dialog |
| 2801 | // navigation code |
| 2802 | m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() |
| 2803 | | wxTE_PROCESS_ENTER); |
| 2804 | } |
| 2805 | } |
| 2806 | else // we had set m_idEdited before |
| 2807 | { |
| 2808 | m_idEdited.Unset(); |
| 2809 | } |
| 2810 | break; |
| 2811 | |
| 2812 | case TVN_ENDLABELEDIT: |
| 2813 | // return true to set the label to the new string: note that we |
| 2814 | // also must pretend that we did process the message or it is going |
| 2815 | // to be passed to DefWindowProc() which will happily return false |
| 2816 | // cancelling the label change |
| 2817 | *result = event.IsAllowed(); |
| 2818 | processed = true; |
| 2819 | |
| 2820 | // ensure that we don't have the text ctrl which is going to be |
| 2821 | // deleted any more |
| 2822 | DeleteTextCtrl(); |
| 2823 | break; |
| 2824 | |
| 2825 | #ifndef __WXWINCE__ |
| 2826 | #ifdef TVN_GETINFOTIP |
| 2827 | case TVN_GETINFOTIP: |
| 2828 | { |
| 2829 | // If the user permitted a tooltip change, change it |
| 2830 | if (event.IsAllowed()) |
| 2831 | { |
| 2832 | SetToolTip(event.m_label); |
| 2833 | } |
| 2834 | } |
| 2835 | break; |
| 2836 | #endif |
| 2837 | #endif |
| 2838 | |
| 2839 | case TVN_SELCHANGING: |
| 2840 | case TVN_ITEMEXPANDING: |
| 2841 | // return true to prevent the action from happening |
| 2842 | *result = !event.IsAllowed(); |
| 2843 | break; |
| 2844 | |
| 2845 | case TVN_ITEMEXPANDED: |
| 2846 | // the item is not refreshed properly after expansion when it has |
| 2847 | // an image depending on the expanded/collapsed state - bug in |
| 2848 | // comctl32.dll or our code? |
| 2849 | { |
| 2850 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; |
| 2851 | wxTreeItemId id(tv->itemNew.hItem); |
| 2852 | |
| 2853 | int image = GetItemImage(id, wxTreeItemIcon_Expanded); |
| 2854 | if ( image != -1 ) |
| 2855 | { |
| 2856 | RefreshItem(id); |
| 2857 | } |
| 2858 | } |
| 2859 | break; |
| 2860 | |
| 2861 | case TVN_GETDISPINFO: |
| 2862 | // NB: so far the user can't set the image himself anyhow, so do it |
| 2863 | // anyway - but this may change later |
| 2864 | //if ( /* !processed && */ ) |
| 2865 | { |
| 2866 | wxTreeItemId item = event.m_item; |
| 2867 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; |
| 2868 | |
| 2869 | const wxTreeItemParam * const param = GetItemParam(item); |
| 2870 | if ( !param ) |
| 2871 | break; |
| 2872 | |
| 2873 | if ( info->item.mask & TVIF_IMAGE ) |
| 2874 | { |
| 2875 | info->item.iImage = |
| 2876 | param->GetImage |
| 2877 | ( |
| 2878 | IsExpanded(item) ? wxTreeItemIcon_Expanded |
| 2879 | : wxTreeItemIcon_Normal |
| 2880 | ); |
| 2881 | } |
| 2882 | if ( info->item.mask & TVIF_SELECTEDIMAGE ) |
| 2883 | { |
| 2884 | info->item.iSelectedImage = |
| 2885 | param->GetImage |
| 2886 | ( |
| 2887 | IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded |
| 2888 | : wxTreeItemIcon_Selected |
| 2889 | ); |
| 2890 | } |
| 2891 | } |
| 2892 | break; |
| 2893 | |
| 2894 | //default: |
| 2895 | // for the other messages the return value is ignored and there is |
| 2896 | // nothing special to do |
| 2897 | } |
| 2898 | return processed; |
| 2899 | } |
| 2900 | |
| 2901 | // ---------------------------------------------------------------------------- |
| 2902 | // State control. |
| 2903 | // ---------------------------------------------------------------------------- |
| 2904 | |
| 2905 | // why do they define INDEXTOSTATEIMAGEMASK but not the inverse? |
| 2906 | #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12) |
| 2907 | |
| 2908 | void wxTreeCtrl::SetState(const wxTreeItemId& node, int state) |
| 2909 | { |
| 2910 | TV_ITEM tvi; |
| 2911 | tvi.hItem = (HTREEITEM)node.m_pItem; |
| 2912 | tvi.mask = TVIF_STATE; |
| 2913 | tvi.stateMask = TVIS_STATEIMAGEMASK; |
| 2914 | |
| 2915 | // Select the specified state, or -1 == cycle to the next one. |
| 2916 | if ( state == -1 ) |
| 2917 | { |
| 2918 | TreeView_GetItem(GetHwnd(), &tvi); |
| 2919 | |
| 2920 | state = STATEIMAGEMASKTOINDEX(tvi.state) + 1; |
| 2921 | if ( state == m_imageListState->GetImageCount() ) |
| 2922 | state = 1; |
| 2923 | } |
| 2924 | |
| 2925 | wxCHECK_RET( state < m_imageListState->GetImageCount(), |
| 2926 | _T("wxTreeCtrl::SetState(): item index out of bounds") ); |
| 2927 | |
| 2928 | tvi.state = INDEXTOSTATEIMAGEMASK(state); |
| 2929 | |
| 2930 | TreeView_SetItem(GetHwnd(), &tvi); |
| 2931 | } |
| 2932 | |
| 2933 | int wxTreeCtrl::GetState(const wxTreeItemId& node) |
| 2934 | { |
| 2935 | TV_ITEM tvi; |
| 2936 | tvi.hItem = (HTREEITEM)node.m_pItem; |
| 2937 | tvi.mask = TVIF_STATE; |
| 2938 | tvi.stateMask = TVIS_STATEIMAGEMASK; |
| 2939 | TreeView_GetItem(GetHwnd(), &tvi); |
| 2940 | |
| 2941 | return STATEIMAGEMASKTOINDEX(tvi.state); |
| 2942 | } |
| 2943 | |
| 2944 | #endif // wxUSE_TREECTRL |