| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: treectlg.cpp |
| 3 | // Purpose: generic tree control implementation |
| 4 | // Author: Robert Roebling |
| 5 | // Created: 01/02/97 |
| 6 | // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ) |
| 7 | // Id: $Id$ |
| 8 | // Copyright: (c) 1998 Robert Roebling and Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================= |
| 13 | // declarations |
| 14 | // ============================================================================= |
| 15 | |
| 16 | // ----------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ----------------------------------------------------------------------------- |
| 19 | |
| 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 21 | #pragma implementation "treectlg.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifdef __BORLANDC__ |
| 28 | #pragma hdrstop |
| 29 | #endif |
| 30 | |
| 31 | #if wxUSE_TREECTRL |
| 32 | |
| 33 | #include "wx/treebase.h" |
| 34 | #include "wx/generic/treectlg.h" |
| 35 | #include "wx/timer.h" |
| 36 | #include "wx/textctrl.h" |
| 37 | #include "wx/imaglist.h" |
| 38 | #include "wx/settings.h" |
| 39 | #include "wx/dcclient.h" |
| 40 | |
| 41 | #include "wx/renderer.h" |
| 42 | |
| 43 | #ifdef __WXMAC__ |
| 44 | #include "wx/mac/private.h" |
| 45 | #endif |
| 46 | |
| 47 | // ----------------------------------------------------------------------------- |
| 48 | // array types |
| 49 | // ----------------------------------------------------------------------------- |
| 50 | |
| 51 | class WXDLLEXPORT wxGenericTreeItem; |
| 52 | |
| 53 | WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem *, wxArrayGenericTreeItems); |
| 54 | |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | // constants |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | |
| 59 | static const int NO_IMAGE = -1; |
| 60 | |
| 61 | static const int PIXELS_PER_UNIT = 10; |
| 62 | |
| 63 | // ----------------------------------------------------------------------------- |
| 64 | // private classes |
| 65 | // ----------------------------------------------------------------------------- |
| 66 | |
| 67 | // timer used for enabling in-place edit |
| 68 | class WXDLLEXPORT wxTreeRenameTimer: public wxTimer |
| 69 | { |
| 70 | public: |
| 71 | // start editing the current item after half a second (if the mouse hasn't |
| 72 | // been clicked/moved) |
| 73 | enum { DELAY = 500 }; |
| 74 | |
| 75 | wxTreeRenameTimer( wxGenericTreeCtrl *owner ); |
| 76 | |
| 77 | virtual void Notify(); |
| 78 | |
| 79 | private: |
| 80 | wxGenericTreeCtrl *m_owner; |
| 81 | |
| 82 | DECLARE_NO_COPY_CLASS(wxTreeRenameTimer) |
| 83 | }; |
| 84 | |
| 85 | // control used for in-place edit |
| 86 | class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl |
| 87 | { |
| 88 | public: |
| 89 | wxTreeTextCtrl(wxGenericTreeCtrl *owner, wxGenericTreeItem *item); |
| 90 | |
| 91 | void StopEditing() |
| 92 | { |
| 93 | Finish(); |
| 94 | m_owner->OnRenameCancelled(m_itemEdited); |
| 95 | } |
| 96 | const wxGenericTreeItem* item() const { return m_itemEdited; } |
| 97 | |
| 98 | protected: |
| 99 | void OnChar( wxKeyEvent &event ); |
| 100 | void OnKeyUp( wxKeyEvent &event ); |
| 101 | void OnKillFocus( wxFocusEvent &event ); |
| 102 | |
| 103 | bool AcceptChanges(); |
| 104 | void Finish(); |
| 105 | |
| 106 | private: |
| 107 | wxGenericTreeCtrl *m_owner; |
| 108 | wxGenericTreeItem *m_itemEdited; |
| 109 | wxString m_startValue; |
| 110 | bool m_finished; |
| 111 | bool m_aboutToFinish; |
| 112 | |
| 113 | DECLARE_EVENT_TABLE() |
| 114 | DECLARE_NO_COPY_CLASS(wxTreeTextCtrl) |
| 115 | }; |
| 116 | |
| 117 | // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed |
| 118 | // for a sufficiently long time |
| 119 | class WXDLLEXPORT wxTreeFindTimer : public wxTimer |
| 120 | { |
| 121 | public: |
| 122 | // reset the current prefix after half a second of inactivity |
| 123 | enum { DELAY = 500 }; |
| 124 | |
| 125 | wxTreeFindTimer( wxGenericTreeCtrl *owner ) { m_owner = owner; } |
| 126 | |
| 127 | virtual void Notify() { m_owner->m_findPrefix.clear(); } |
| 128 | |
| 129 | private: |
| 130 | wxGenericTreeCtrl *m_owner; |
| 131 | |
| 132 | DECLARE_NO_COPY_CLASS(wxTreeFindTimer) |
| 133 | }; |
| 134 | |
| 135 | // a tree item |
| 136 | class WXDLLEXPORT wxGenericTreeItem |
| 137 | { |
| 138 | public: |
| 139 | // ctors & dtor |
| 140 | wxGenericTreeItem() { m_data = NULL; } |
| 141 | wxGenericTreeItem( wxGenericTreeItem *parent, |
| 142 | const wxString& text, |
| 143 | int image, |
| 144 | int selImage, |
| 145 | wxTreeItemData *data ); |
| 146 | |
| 147 | ~wxGenericTreeItem(); |
| 148 | |
| 149 | // trivial accessors |
| 150 | wxArrayGenericTreeItems& GetChildren() { return m_children; } |
| 151 | |
| 152 | const wxString& GetText() const { return m_text; } |
| 153 | int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const |
| 154 | { return m_images[which]; } |
| 155 | wxTreeItemData *GetData() const { return m_data; } |
| 156 | |
| 157 | // returns the current image for the item (depending on its |
| 158 | // selected/expanded/whatever state) |
| 159 | int GetCurrentImage() const; |
| 160 | |
| 161 | void SetText( const wxString &text ); |
| 162 | void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; } |
| 163 | void SetData(wxTreeItemData *data) { m_data = data; } |
| 164 | |
| 165 | void SetHasPlus(bool has = true) { m_hasPlus = has; } |
| 166 | |
| 167 | void SetBold(bool bold) { m_isBold = bold; } |
| 168 | |
| 169 | int GetX() const { return m_x; } |
| 170 | int GetY() const { return m_y; } |
| 171 | |
| 172 | void SetX(int x) { m_x = x; } |
| 173 | void SetY(int y) { m_y = y; } |
| 174 | |
| 175 | int GetHeight() const { return m_height; } |
| 176 | int GetWidth() const { return m_width; } |
| 177 | |
| 178 | void SetHeight(int h) { m_height = h; } |
| 179 | void SetWidth(int w) { m_width = w; } |
| 180 | |
| 181 | wxGenericTreeItem *GetParent() const { return m_parent; } |
| 182 | |
| 183 | // operations |
| 184 | // deletes all children notifying the treectrl about it if !NULL |
| 185 | // pointer given |
| 186 | void DeleteChildren(wxGenericTreeCtrl *tree = NULL); |
| 187 | |
| 188 | // get count of all children (and grand children if 'recursively') |
| 189 | size_t GetChildrenCount(bool recursively = true) const; |
| 190 | |
| 191 | void Insert(wxGenericTreeItem *child, size_t index) |
| 192 | { m_children.Insert(child, index); } |
| 193 | |
| 194 | void GetSize( int &x, int &y, const wxGenericTreeCtrl* ); |
| 195 | |
| 196 | // return the item at given position (or NULL if no item), onButton is |
| 197 | // true if the point belongs to the item's button, otherwise it lies |
| 198 | // on the item's label |
| 199 | wxGenericTreeItem *HitTest( const wxPoint& point, |
| 200 | const wxGenericTreeCtrl *, |
| 201 | int &flags, |
| 202 | int level ); |
| 203 | |
| 204 | void Expand() { m_isCollapsed = false; } |
| 205 | void Collapse() { m_isCollapsed = true; } |
| 206 | |
| 207 | void SetHilight( bool set = true ) { m_hasHilight = set; } |
| 208 | |
| 209 | // status inquiries |
| 210 | bool HasChildren() const { return !m_children.IsEmpty(); } |
| 211 | bool IsSelected() const { return m_hasHilight != 0; } |
| 212 | bool IsExpanded() const { return !m_isCollapsed; } |
| 213 | bool HasPlus() const { return m_hasPlus || HasChildren(); } |
| 214 | bool IsBold() const { return m_isBold != 0; } |
| 215 | |
| 216 | // attributes |
| 217 | // get them - may be NULL |
| 218 | wxTreeItemAttr *GetAttributes() const { return m_attr; } |
| 219 | // get them ensuring that the pointer is not NULL |
| 220 | wxTreeItemAttr& Attr() |
| 221 | { |
| 222 | if ( !m_attr ) |
| 223 | { |
| 224 | m_attr = new wxTreeItemAttr; |
| 225 | m_ownsAttr = true; |
| 226 | } |
| 227 | return *m_attr; |
| 228 | } |
| 229 | // set them |
| 230 | void SetAttributes(wxTreeItemAttr *attr) |
| 231 | { |
| 232 | if ( m_ownsAttr ) delete m_attr; |
| 233 | m_attr = attr; |
| 234 | m_ownsAttr = false; |
| 235 | } |
| 236 | // set them and delete when done |
| 237 | void AssignAttributes(wxTreeItemAttr *attr) |
| 238 | { |
| 239 | SetAttributes(attr); |
| 240 | m_ownsAttr = true; |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | // since there can be very many of these, we save size by chosing |
| 245 | // the smallest representation for the elements and by ordering |
| 246 | // the members to avoid padding. |
| 247 | wxString m_text; // label to be rendered for item |
| 248 | |
| 249 | wxTreeItemData *m_data; // user-provided data |
| 250 | |
| 251 | wxArrayGenericTreeItems m_children; // list of children |
| 252 | wxGenericTreeItem *m_parent; // parent of this item |
| 253 | |
| 254 | wxTreeItemAttr *m_attr; // attributes??? |
| 255 | |
| 256 | // tree ctrl images for the normal, selected, expanded and |
| 257 | // expanded+selected states |
| 258 | int m_images[wxTreeItemIcon_Max]; |
| 259 | |
| 260 | wxCoord m_x; // (virtual) offset from top |
| 261 | wxCoord m_y; // (virtual) offset from left |
| 262 | int m_width; // width of this item |
| 263 | int m_height; // height of this item |
| 264 | |
| 265 | // use bitfields to save size |
| 266 | int m_isCollapsed :1; |
| 267 | int m_hasHilight :1; // same as focused |
| 268 | int m_hasPlus :1; // used for item which doesn't have |
| 269 | // children but has a [+] button |
| 270 | int m_isBold :1; // render the label in bold font |
| 271 | int m_ownsAttr :1; // delete attribute when done |
| 272 | |
| 273 | DECLARE_NO_COPY_CLASS(wxGenericTreeItem) |
| 274 | }; |
| 275 | |
| 276 | // ============================================================================= |
| 277 | // implementation |
| 278 | // ============================================================================= |
| 279 | |
| 280 | // ---------------------------------------------------------------------------- |
| 281 | // private functions |
| 282 | // ---------------------------------------------------------------------------- |
| 283 | |
| 284 | // translate the key or mouse event flags to the type of selection we're |
| 285 | // dealing with |
| 286 | static void EventFlagsToSelType(long style, |
| 287 | bool shiftDown, |
| 288 | bool ctrlDown, |
| 289 | bool &is_multiple, |
| 290 | bool &extended_select, |
| 291 | bool &unselect_others) |
| 292 | { |
| 293 | is_multiple = (style & wxTR_MULTIPLE) != 0; |
| 294 | extended_select = shiftDown && is_multiple; |
| 295 | unselect_others = !(extended_select || (ctrlDown && is_multiple)); |
| 296 | } |
| 297 | |
| 298 | // check if the given item is under another one |
| 299 | static bool IsDescendantOf(const wxGenericTreeItem *parent, const wxGenericTreeItem *item) |
| 300 | { |
| 301 | while ( item ) |
| 302 | { |
| 303 | if ( item == parent ) |
| 304 | { |
| 305 | // item is a descendant of parent |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | item = item->GetParent(); |
| 310 | } |
| 311 | |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | // ----------------------------------------------------------------------------- |
| 316 | // wxTreeRenameTimer (internal) |
| 317 | // ----------------------------------------------------------------------------- |
| 318 | |
| 319 | wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner ) |
| 320 | { |
| 321 | m_owner = owner; |
| 322 | } |
| 323 | |
| 324 | void wxTreeRenameTimer::Notify() |
| 325 | { |
| 326 | m_owner->OnRenameTimer(); |
| 327 | } |
| 328 | |
| 329 | //----------------------------------------------------------------------------- |
| 330 | // wxTreeTextCtrl (internal) |
| 331 | //----------------------------------------------------------------------------- |
| 332 | |
| 333 | BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl) |
| 334 | EVT_CHAR (wxTreeTextCtrl::OnChar) |
| 335 | EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp) |
| 336 | EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus) |
| 337 | END_EVENT_TABLE() |
| 338 | |
| 339 | wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl *owner, |
| 340 | wxGenericTreeItem *item) |
| 341 | : m_itemEdited(item), m_startValue(item->GetText()) |
| 342 | { |
| 343 | m_owner = owner; |
| 344 | m_finished = false; |
| 345 | m_aboutToFinish = false; |
| 346 | |
| 347 | int w = m_itemEdited->GetWidth(), |
| 348 | h = m_itemEdited->GetHeight(); |
| 349 | |
| 350 | int x, y; |
| 351 | m_owner->CalcScrolledPosition(item->GetX(), item->GetY(), &x, &y); |
| 352 | |
| 353 | int image_h = 0, |
| 354 | image_w = 0; |
| 355 | |
| 356 | int image = item->GetCurrentImage(); |
| 357 | if ( image != NO_IMAGE ) |
| 358 | { |
| 359 | if ( m_owner->m_imageListNormal ) |
| 360 | { |
| 361 | m_owner->m_imageListNormal->GetSize( image, image_w, image_h ); |
| 362 | image_w += 4; |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | wxFAIL_MSG(_T("you must create an image list to use images!")); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // FIXME: what are all these hardcoded 4, 8 and 11s really? |
| 371 | x += image_w; |
| 372 | w -= image_w + 4; |
| 373 | #ifdef __WXMAC__ |
| 374 | wxSize bs = DoGetBestSize() ; |
| 375 | // edit control height |
| 376 | if ( h > bs.y - 8 ) |
| 377 | { |
| 378 | int diff = h - ( bs.y - 8 ) ; |
| 379 | h -= diff ; |
| 380 | y += diff / 2 ; |
| 381 | } |
| 382 | #endif |
| 383 | |
| 384 | (void)Create(m_owner, wxID_ANY, m_startValue, |
| 385 | wxPoint(x - 4, y - 4), wxSize(w + 11, h + 8)); |
| 386 | } |
| 387 | |
| 388 | bool wxTreeTextCtrl::AcceptChanges() |
| 389 | { |
| 390 | const wxString value = GetValue(); |
| 391 | |
| 392 | if ( value == m_startValue ) |
| 393 | { |
| 394 | // nothing changed, always accept |
| 395 | // when an item remains unchanged, the owner |
| 396 | // needs to be notified that the user decided |
| 397 | // not to change the tree item label, and that |
| 398 | // the edit has been cancelled |
| 399 | |
| 400 | m_owner->OnRenameCancelled(m_itemEdited); |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | if ( !m_owner->OnRenameAccept(m_itemEdited, value) ) |
| 405 | { |
| 406 | // vetoed by the user |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | // accepted, do rename the item |
| 411 | m_owner->SetItemText(m_itemEdited, value); |
| 412 | |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | void wxTreeTextCtrl::Finish() |
| 417 | { |
| 418 | if ( !m_finished ) |
| 419 | { |
| 420 | m_owner->ResetTextControl(); |
| 421 | |
| 422 | wxPendingDelete.Append(this); |
| 423 | |
| 424 | m_finished = true; |
| 425 | |
| 426 | m_owner->SetFocusIgnoringChildren(); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | void wxTreeTextCtrl::OnChar( wxKeyEvent &event ) |
| 431 | { |
| 432 | switch ( event.m_keyCode ) |
| 433 | { |
| 434 | case WXK_RETURN: |
| 435 | m_aboutToFinish = true; |
| 436 | // Notify the owner about the changes |
| 437 | AcceptChanges(); |
| 438 | // Even if vetoed, close the control (consistent with MSW) |
| 439 | Finish(); |
| 440 | break; |
| 441 | |
| 442 | case WXK_ESCAPE: |
| 443 | StopEditing(); |
| 444 | break; |
| 445 | |
| 446 | default: |
| 447 | event.Skip(); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void wxTreeTextCtrl::OnKeyUp( wxKeyEvent &event ) |
| 452 | { |
| 453 | if ( !m_finished ) |
| 454 | { |
| 455 | // auto-grow the textctrl: |
| 456 | wxSize parentSize = m_owner->GetSize(); |
| 457 | wxPoint myPos = GetPosition(); |
| 458 | wxSize mySize = GetSize(); |
| 459 | int sx, sy; |
| 460 | GetTextExtent(GetValue() + _T("M"), &sx, &sy); |
| 461 | if (myPos.x + sx > parentSize.x) |
| 462 | sx = parentSize.x - myPos.x; |
| 463 | if (mySize.x > sx) |
| 464 | sx = mySize.x; |
| 465 | SetSize(sx, wxDefaultCoord); |
| 466 | } |
| 467 | |
| 468 | event.Skip(); |
| 469 | } |
| 470 | |
| 471 | void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &event ) |
| 472 | { |
| 473 | if ( !m_finished && !m_aboutToFinish ) |
| 474 | { |
| 475 | // We must finish regardless of success, otherwise we'll get |
| 476 | // focus problems: |
| 477 | Finish(); |
| 478 | |
| 479 | if ( !AcceptChanges() ) |
| 480 | m_owner->OnRenameCancelled( m_itemEdited ); |
| 481 | } |
| 482 | |
| 483 | // We must let the native text control handle focus, too, otherwise |
| 484 | // it could have problems with the cursor (e.g., in wxGTK). |
| 485 | event.Skip(); |
| 486 | } |
| 487 | |
| 488 | // ----------------------------------------------------------------------------- |
| 489 | // wxGenericTreeItem |
| 490 | // ----------------------------------------------------------------------------- |
| 491 | |
| 492 | wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent, |
| 493 | const wxString& text, |
| 494 | int image, int selImage, |
| 495 | wxTreeItemData *data) |
| 496 | : m_text(text) |
| 497 | { |
| 498 | m_images[wxTreeItemIcon_Normal] = image; |
| 499 | m_images[wxTreeItemIcon_Selected] = selImage; |
| 500 | m_images[wxTreeItemIcon_Expanded] = NO_IMAGE; |
| 501 | m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE; |
| 502 | |
| 503 | m_data = data; |
| 504 | m_x = m_y = 0; |
| 505 | |
| 506 | m_isCollapsed = true; |
| 507 | m_hasHilight = false; |
| 508 | m_hasPlus = false; |
| 509 | m_isBold = false; |
| 510 | |
| 511 | m_parent = parent; |
| 512 | |
| 513 | m_attr = (wxTreeItemAttr *)NULL; |
| 514 | m_ownsAttr = false; |
| 515 | |
| 516 | // We don't know the height here yet. |
| 517 | m_width = 0; |
| 518 | m_height = 0; |
| 519 | } |
| 520 | |
| 521 | wxGenericTreeItem::~wxGenericTreeItem() |
| 522 | { |
| 523 | delete m_data; |
| 524 | |
| 525 | if (m_ownsAttr) delete m_attr; |
| 526 | |
| 527 | wxASSERT_MSG( m_children.IsEmpty(), |
| 528 | wxT("please call DeleteChildren() before deleting the item") ); |
| 529 | } |
| 530 | |
| 531 | void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree) |
| 532 | { |
| 533 | size_t count = m_children.Count(); |
| 534 | for ( size_t n = 0; n < count; n++ ) |
| 535 | { |
| 536 | wxGenericTreeItem *child = m_children[n]; |
| 537 | if (tree) |
| 538 | tree->SendDeleteEvent(child); |
| 539 | |
| 540 | child->DeleteChildren(tree); |
| 541 | if (child == tree->m_select_me) |
| 542 | tree->m_select_me = NULL; |
| 543 | delete child; |
| 544 | } |
| 545 | |
| 546 | m_children.Empty(); |
| 547 | } |
| 548 | |
| 549 | void wxGenericTreeItem::SetText( const wxString &text ) |
| 550 | { |
| 551 | m_text = text; |
| 552 | } |
| 553 | |
| 554 | size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const |
| 555 | { |
| 556 | size_t count = m_children.Count(); |
| 557 | if ( !recursively ) |
| 558 | return count; |
| 559 | |
| 560 | size_t total = count; |
| 561 | for (size_t n = 0; n < count; ++n) |
| 562 | { |
| 563 | total += m_children[n]->GetChildrenCount(); |
| 564 | } |
| 565 | |
| 566 | return total; |
| 567 | } |
| 568 | |
| 569 | void wxGenericTreeItem::GetSize( int &x, int &y, |
| 570 | const wxGenericTreeCtrl *theButton ) |
| 571 | { |
| 572 | int bottomY=m_y+theButton->GetLineHeight(this); |
| 573 | if ( y < bottomY ) y = bottomY; |
| 574 | int width = m_x + m_width; |
| 575 | if ( x < width ) x = width; |
| 576 | |
| 577 | if (IsExpanded()) |
| 578 | { |
| 579 | size_t count = m_children.Count(); |
| 580 | for ( size_t n = 0; n < count; ++n ) |
| 581 | { |
| 582 | m_children[n]->GetSize( x, y, theButton ); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | wxGenericTreeItem *wxGenericTreeItem::HitTest(const wxPoint& point, |
| 588 | const wxGenericTreeCtrl *theCtrl, |
| 589 | int &flags, |
| 590 | int level) |
| 591 | { |
| 592 | // for a hidden root node, don't evaluate it, but do evaluate children |
| 593 | if ( !(level == 0 && theCtrl->HasFlag(wxTR_HIDE_ROOT)) ) |
| 594 | { |
| 595 | // evaluate the item |
| 596 | int h = theCtrl->GetLineHeight(this); |
| 597 | if ((point.y > m_y) && (point.y < m_y + h)) |
| 598 | { |
| 599 | int y_mid = m_y + h/2; |
| 600 | if (point.y < y_mid ) |
| 601 | flags |= wxTREE_HITTEST_ONITEMUPPERPART; |
| 602 | else |
| 603 | flags |= wxTREE_HITTEST_ONITEMLOWERPART; |
| 604 | |
| 605 | int xCross = m_x - theCtrl->GetSpacing(); |
| 606 | #ifdef __WXMAC__ |
| 607 | // according to the drawing code the triangels are drawn |
| 608 | // at -4 , -4 from the position up to +10/+10 max |
| 609 | if ((point.x > xCross-4) && (point.x < xCross+10) && |
| 610 | (point.y > y_mid-4) && (point.y < y_mid+10) && |
| 611 | HasPlus() && theCtrl->HasButtons() ) |
| 612 | #else |
| 613 | // 5 is the size of the plus sign |
| 614 | if ((point.x > xCross-6) && (point.x < xCross+6) && |
| 615 | (point.y > y_mid-6) && (point.y < y_mid+6) && |
| 616 | HasPlus() && theCtrl->HasButtons() ) |
| 617 | #endif |
| 618 | { |
| 619 | flags |= wxTREE_HITTEST_ONITEMBUTTON; |
| 620 | return this; |
| 621 | } |
| 622 | |
| 623 | if ((point.x >= m_x) && (point.x <= m_x+m_width)) |
| 624 | { |
| 625 | int image_w = -1; |
| 626 | int image_h; |
| 627 | |
| 628 | // assuming every image (normal and selected) has the same size! |
| 629 | if ( (GetImage() != NO_IMAGE) && theCtrl->m_imageListNormal ) |
| 630 | theCtrl->m_imageListNormal->GetSize(GetImage(), |
| 631 | image_w, image_h); |
| 632 | |
| 633 | if ((image_w != -1) && (point.x <= m_x + image_w + 1)) |
| 634 | flags |= wxTREE_HITTEST_ONITEMICON; |
| 635 | else |
| 636 | flags |= wxTREE_HITTEST_ONITEMLABEL; |
| 637 | |
| 638 | return this; |
| 639 | } |
| 640 | |
| 641 | if (point.x < m_x) |
| 642 | flags |= wxTREE_HITTEST_ONITEMINDENT; |
| 643 | if (point.x > m_x+m_width) |
| 644 | flags |= wxTREE_HITTEST_ONITEMRIGHT; |
| 645 | |
| 646 | return this; |
| 647 | } |
| 648 | |
| 649 | // if children are expanded, fall through to evaluate them |
| 650 | if (m_isCollapsed) return (wxGenericTreeItem*) NULL; |
| 651 | } |
| 652 | |
| 653 | // evaluate children |
| 654 | size_t count = m_children.Count(); |
| 655 | for ( size_t n = 0; n < count; n++ ) |
| 656 | { |
| 657 | wxGenericTreeItem *res = m_children[n]->HitTest( point, |
| 658 | theCtrl, |
| 659 | flags, |
| 660 | level + 1 ); |
| 661 | if ( res != NULL ) |
| 662 | return res; |
| 663 | } |
| 664 | |
| 665 | return (wxGenericTreeItem*) NULL; |
| 666 | } |
| 667 | |
| 668 | int wxGenericTreeItem::GetCurrentImage() const |
| 669 | { |
| 670 | int image = NO_IMAGE; |
| 671 | if ( IsExpanded() ) |
| 672 | { |
| 673 | if ( IsSelected() ) |
| 674 | { |
| 675 | image = GetImage(wxTreeItemIcon_SelectedExpanded); |
| 676 | } |
| 677 | |
| 678 | if ( image == NO_IMAGE ) |
| 679 | { |
| 680 | // we usually fall back to the normal item, but try just the |
| 681 | // expanded one (and not selected) first in this case |
| 682 | image = GetImage(wxTreeItemIcon_Expanded); |
| 683 | } |
| 684 | } |
| 685 | else // not expanded |
| 686 | { |
| 687 | if ( IsSelected() ) |
| 688 | image = GetImage(wxTreeItemIcon_Selected); |
| 689 | } |
| 690 | |
| 691 | // maybe it doesn't have the specific image we want, |
| 692 | // try the default one instead |
| 693 | if ( image == NO_IMAGE ) image = GetImage(); |
| 694 | |
| 695 | return image; |
| 696 | } |
| 697 | |
| 698 | // ----------------------------------------------------------------------------- |
| 699 | // wxGenericTreeCtrl implementation |
| 700 | // ----------------------------------------------------------------------------- |
| 701 | |
| 702 | IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxScrolledWindow) |
| 703 | |
| 704 | BEGIN_EVENT_TABLE(wxGenericTreeCtrl,wxScrolledWindow) |
| 705 | EVT_PAINT (wxGenericTreeCtrl::OnPaint) |
| 706 | EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse) |
| 707 | EVT_CHAR (wxGenericTreeCtrl::OnChar) |
| 708 | EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus) |
| 709 | EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus) |
| 710 | EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY, wxGenericTreeCtrl::OnGetToolTip) |
| 711 | END_EVENT_TABLE() |
| 712 | |
| 713 | #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__) |
| 714 | /* |
| 715 | * wxTreeCtrl has to be a real class or we have problems with |
| 716 | * the run-time information. |
| 717 | */ |
| 718 | |
| 719 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl) |
| 720 | #endif |
| 721 | |
| 722 | // ----------------------------------------------------------------------------- |
| 723 | // construction/destruction |
| 724 | // ----------------------------------------------------------------------------- |
| 725 | |
| 726 | void wxGenericTreeCtrl::Init() |
| 727 | { |
| 728 | m_current = m_key_current = m_anchor = m_select_me = (wxGenericTreeItem *) NULL; |
| 729 | m_hasFocus = false; |
| 730 | m_dirty = false; |
| 731 | |
| 732 | m_lineHeight = 10; |
| 733 | m_indent = 15; |
| 734 | m_spacing = 18; |
| 735 | |
| 736 | m_hilightBrush = new wxBrush |
| 737 | ( |
| 738 | wxSystemSettings::GetColour |
| 739 | ( |
| 740 | wxSYS_COLOUR_HIGHLIGHT |
| 741 | ), |
| 742 | wxSOLID |
| 743 | ); |
| 744 | |
| 745 | m_hilightUnfocusedBrush = new wxBrush |
| 746 | ( |
| 747 | wxSystemSettings::GetColour |
| 748 | ( |
| 749 | wxSYS_COLOUR_BTNSHADOW |
| 750 | ), |
| 751 | wxSOLID |
| 752 | ); |
| 753 | |
| 754 | m_imageListNormal = m_imageListButtons = |
| 755 | m_imageListState = (wxImageList *) NULL; |
| 756 | m_ownsImageListNormal = m_ownsImageListButtons = |
| 757 | m_ownsImageListState = false; |
| 758 | |
| 759 | m_dragCount = 0; |
| 760 | m_isDragging = false; |
| 761 | m_dropTarget = m_oldSelection = NULL; |
| 762 | m_underMouse = NULL; |
| 763 | m_textCtrl = NULL; |
| 764 | |
| 765 | m_renameTimer = NULL; |
| 766 | m_freezeCount = 0; |
| 767 | |
| 768 | m_findTimer = NULL; |
| 769 | |
| 770 | m_dropEffectAboveItem = false; |
| 771 | |
| 772 | m_lastOnSame = false; |
| 773 | |
| 774 | #ifdef __WXMAC_CARBON__ |
| 775 | m_normalFont.MacCreateThemeFont( kThemeViewsFont ) ; |
| 776 | #else |
| 777 | m_normalFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ); |
| 778 | #endif |
| 779 | m_boldFont = wxFont(m_normalFont.GetPointSize(), |
| 780 | m_normalFont.GetFamily(), |
| 781 | m_normalFont.GetStyle(), |
| 782 | wxBOLD, |
| 783 | m_normalFont.GetUnderlined(), |
| 784 | m_normalFont.GetFaceName(), |
| 785 | m_normalFont.GetEncoding()); |
| 786 | } |
| 787 | |
| 788 | bool wxGenericTreeCtrl::Create(wxWindow *parent, |
| 789 | wxWindowID id, |
| 790 | const wxPoint& pos, |
| 791 | const wxSize& size, |
| 792 | long style, |
| 793 | const wxValidator& wxVALIDATOR_PARAM(validator), |
| 794 | const wxString& name ) |
| 795 | { |
| 796 | #ifdef __WXMAC__ |
| 797 | int major,minor; |
| 798 | wxGetOsVersion( &major, &minor ); |
| 799 | |
| 800 | style &= ~wxTR_LINES_AT_ROOT; |
| 801 | style |= wxTR_NO_LINES; |
| 802 | if (major < 10) |
| 803 | style |= wxTR_ROW_LINES; |
| 804 | #endif // __WXMAC__ |
| 805 | |
| 806 | wxScrolledWindow::Create( parent, id, pos, size, |
| 807 | style|wxHSCROLL|wxVSCROLL, name ); |
| 808 | |
| 809 | // If the tree display has no buttons, but does have |
| 810 | // connecting lines, we can use a narrower layout. |
| 811 | // It may not be a good idea to force this... |
| 812 | if (!HasButtons() && !HasFlag(wxTR_NO_LINES)) |
| 813 | { |
| 814 | m_indent= 10; |
| 815 | m_spacing = 10; |
| 816 | } |
| 817 | |
| 818 | #if wxUSE_VALIDATORS |
| 819 | SetValidator( validator ); |
| 820 | #endif |
| 821 | |
| 822 | wxVisualAttributes attr = GetDefaultAttributes(); |
| 823 | SetOwnForegroundColour( attr.colFg ); |
| 824 | SetOwnBackgroundColour( attr.colBg ); |
| 825 | if (!m_hasFont) |
| 826 | SetOwnFont(attr.font); |
| 827 | |
| 828 | // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86 |
| 829 | m_dottedPen = wxPen( wxT("grey"), 0, 0 ); |
| 830 | |
| 831 | SetBestSize(size); |
| 832 | |
| 833 | return true; |
| 834 | } |
| 835 | |
| 836 | wxGenericTreeCtrl::~wxGenericTreeCtrl() |
| 837 | { |
| 838 | delete m_hilightBrush; |
| 839 | delete m_hilightUnfocusedBrush; |
| 840 | |
| 841 | DeleteAllItems(); |
| 842 | |
| 843 | delete m_renameTimer; |
| 844 | delete m_findTimer; |
| 845 | |
| 846 | if (m_ownsImageListNormal) |
| 847 | delete m_imageListNormal; |
| 848 | if (m_ownsImageListState) |
| 849 | delete m_imageListState; |
| 850 | if (m_ownsImageListButtons) |
| 851 | delete m_imageListButtons; |
| 852 | } |
| 853 | |
| 854 | // ----------------------------------------------------------------------------- |
| 855 | // accessors |
| 856 | // ----------------------------------------------------------------------------- |
| 857 | |
| 858 | size_t wxGenericTreeCtrl::GetCount() const |
| 859 | { |
| 860 | if ( !m_anchor ) |
| 861 | { |
| 862 | // the tree is empty |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | size_t count = m_anchor->GetChildrenCount(); |
| 867 | if ( !HasFlag(wxTR_HIDE_ROOT) ) |
| 868 | { |
| 869 | // take the root itself into account |
| 870 | count++; |
| 871 | } |
| 872 | |
| 873 | return count; |
| 874 | } |
| 875 | |
| 876 | void wxGenericTreeCtrl::SetIndent(unsigned int indent) |
| 877 | { |
| 878 | m_indent = (unsigned short) indent; |
| 879 | m_dirty = true; |
| 880 | } |
| 881 | |
| 882 | void wxGenericTreeCtrl::SetSpacing(unsigned int spacing) |
| 883 | { |
| 884 | m_spacing = (unsigned short) spacing; |
| 885 | m_dirty = true; |
| 886 | } |
| 887 | |
| 888 | size_t |
| 889 | wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item, |
| 890 | bool recursively) const |
| 891 | { |
| 892 | wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") ); |
| 893 | |
| 894 | return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively); |
| 895 | } |
| 896 | |
| 897 | void wxGenericTreeCtrl::SetWindowStyle(const long styles) |
| 898 | { |
| 899 | if (!HasFlag(wxTR_HIDE_ROOT) && (styles & wxTR_HIDE_ROOT)) |
| 900 | { |
| 901 | // if we will hide the root, make sure children are visible |
| 902 | m_anchor->SetHasPlus(); |
| 903 | m_anchor->Expand(); |
| 904 | CalculatePositions(); |
| 905 | } |
| 906 | |
| 907 | // right now, just sets the styles. Eventually, we may |
| 908 | // want to update the inherited styles, but right now |
| 909 | // none of the parents has updatable styles |
| 910 | m_windowStyle = styles; |
| 911 | m_dirty = true; |
| 912 | } |
| 913 | |
| 914 | // ----------------------------------------------------------------------------- |
| 915 | // functions to work with tree items |
| 916 | // ----------------------------------------------------------------------------- |
| 917 | |
| 918 | wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const |
| 919 | { |
| 920 | wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") ); |
| 921 | |
| 922 | return ((wxGenericTreeItem*) item.m_pItem)->GetText(); |
| 923 | } |
| 924 | |
| 925 | int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item, |
| 926 | wxTreeItemIcon which) const |
| 927 | { |
| 928 | wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") ); |
| 929 | |
| 930 | return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which); |
| 931 | } |
| 932 | |
| 933 | wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const |
| 934 | { |
| 935 | wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") ); |
| 936 | |
| 937 | return ((wxGenericTreeItem*) item.m_pItem)->GetData(); |
| 938 | } |
| 939 | |
| 940 | wxColour wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const |
| 941 | { |
| 942 | wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") ); |
| 943 | |
| 944 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 945 | return pItem->Attr().GetTextColour(); |
| 946 | } |
| 947 | |
| 948 | wxColour wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const |
| 949 | { |
| 950 | wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") ); |
| 951 | |
| 952 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 953 | return pItem->Attr().GetBackgroundColour(); |
| 954 | } |
| 955 | |
| 956 | wxFont wxGenericTreeCtrl::GetItemFont(const wxTreeItemId& item) const |
| 957 | { |
| 958 | wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") ); |
| 959 | |
| 960 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 961 | return pItem->Attr().GetFont(); |
| 962 | } |
| 963 | |
| 964 | void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) |
| 965 | { |
| 966 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 967 | |
| 968 | wxClientDC dc(this); |
| 969 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 970 | pItem->SetText(text); |
| 971 | CalculateSize(pItem, dc); |
| 972 | RefreshLine(pItem); |
| 973 | } |
| 974 | |
| 975 | void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item, |
| 976 | int image, |
| 977 | wxTreeItemIcon which) |
| 978 | { |
| 979 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 980 | |
| 981 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 982 | pItem->SetImage(image, which); |
| 983 | |
| 984 | wxClientDC dc(this); |
| 985 | CalculateSize(pItem, dc); |
| 986 | RefreshLine(pItem); |
| 987 | } |
| 988 | |
| 989 | void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) |
| 990 | { |
| 991 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 992 | |
| 993 | if (data) |
| 994 | data->SetId( item ); |
| 995 | |
| 996 | ((wxGenericTreeItem*) item.m_pItem)->SetData(data); |
| 997 | } |
| 998 | |
| 999 | void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) |
| 1000 | { |
| 1001 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1002 | |
| 1003 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 1004 | pItem->SetHasPlus(has); |
| 1005 | RefreshLine(pItem); |
| 1006 | } |
| 1007 | |
| 1008 | void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) |
| 1009 | { |
| 1010 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1011 | |
| 1012 | // avoid redrawing the tree if no real change |
| 1013 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 1014 | if ( pItem->IsBold() != bold ) |
| 1015 | { |
| 1016 | pItem->SetBold(bold); |
| 1017 | RefreshLine(pItem); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, |
| 1022 | bool highlight) |
| 1023 | { |
| 1024 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1025 | |
| 1026 | wxColour fg, bg; |
| 1027 | |
| 1028 | if (highlight) |
| 1029 | { |
| 1030 | bg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); |
| 1031 | fg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); |
| 1032 | } |
| 1033 | |
| 1034 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 1035 | pItem->Attr().SetTextColour(fg); |
| 1036 | pItem->Attr().SetBackgroundColour(bg); |
| 1037 | RefreshLine(pItem); |
| 1038 | } |
| 1039 | |
| 1040 | void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item, |
| 1041 | const wxColour& col) |
| 1042 | { |
| 1043 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1044 | |
| 1045 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 1046 | pItem->Attr().SetTextColour(col); |
| 1047 | RefreshLine(pItem); |
| 1048 | } |
| 1049 | |
| 1050 | void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item, |
| 1051 | const wxColour& col) |
| 1052 | { |
| 1053 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1054 | |
| 1055 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 1056 | pItem->Attr().SetBackgroundColour(col); |
| 1057 | RefreshLine(pItem); |
| 1058 | } |
| 1059 | |
| 1060 | void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font) |
| 1061 | { |
| 1062 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1063 | |
| 1064 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 1065 | pItem->Attr().SetFont(font); |
| 1066 | RefreshLine(pItem); |
| 1067 | } |
| 1068 | |
| 1069 | bool wxGenericTreeCtrl::SetFont( const wxFont &font ) |
| 1070 | { |
| 1071 | wxScrolledWindow::SetFont(font); |
| 1072 | |
| 1073 | m_normalFont = font ; |
| 1074 | m_boldFont = wxFont(m_normalFont.GetPointSize(), |
| 1075 | m_normalFont.GetFamily(), |
| 1076 | m_normalFont.GetStyle(), |
| 1077 | wxBOLD, |
| 1078 | m_normalFont.GetUnderlined(), |
| 1079 | m_normalFont.GetFaceName(), |
| 1080 | m_normalFont.GetEncoding()); |
| 1081 | |
| 1082 | return true; |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | // ----------------------------------------------------------------------------- |
| 1087 | // item status inquiries |
| 1088 | // ----------------------------------------------------------------------------- |
| 1089 | |
| 1090 | bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const |
| 1091 | { |
| 1092 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1093 | |
| 1094 | // An item is only visible if it's not a descendant of a collapsed item |
| 1095 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; |
| 1096 | wxGenericTreeItem* parent = pItem->GetParent(); |
| 1097 | while (parent) |
| 1098 | { |
| 1099 | if (!parent->IsExpanded()) |
| 1100 | return false; |
| 1101 | parent = parent->GetParent(); |
| 1102 | } |
| 1103 | |
| 1104 | int startX, startY; |
| 1105 | GetViewStart(& startX, & startY); |
| 1106 | |
| 1107 | wxSize clientSize = GetClientSize(); |
| 1108 | |
| 1109 | wxRect rect; |
| 1110 | if (!GetBoundingRect(item, rect)) |
| 1111 | return false; |
| 1112 | if (rect.GetWidth() == 0 || rect.GetHeight() == 0) |
| 1113 | return false; |
| 1114 | if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y) |
| 1115 | return false; |
| 1116 | if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x) |
| 1117 | return false; |
| 1118 | |
| 1119 | return true; |
| 1120 | } |
| 1121 | |
| 1122 | bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const |
| 1123 | { |
| 1124 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1125 | |
| 1126 | // consider that the item does have children if it has the "+" button: it |
| 1127 | // might not have them (if it had never been expanded yet) but then it |
| 1128 | // could have them as well and it's better to err on this side rather than |
| 1129 | // disabling some operations which are restricted to the items with |
| 1130 | // children for an item which does have them |
| 1131 | return ((wxGenericTreeItem*) item.m_pItem)->HasPlus(); |
| 1132 | } |
| 1133 | |
| 1134 | bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const |
| 1135 | { |
| 1136 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1137 | |
| 1138 | return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded(); |
| 1139 | } |
| 1140 | |
| 1141 | bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const |
| 1142 | { |
| 1143 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1144 | |
| 1145 | return ((wxGenericTreeItem*) item.m_pItem)->IsSelected(); |
| 1146 | } |
| 1147 | |
| 1148 | bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const |
| 1149 | { |
| 1150 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
| 1151 | |
| 1152 | return ((wxGenericTreeItem*) item.m_pItem)->IsBold(); |
| 1153 | } |
| 1154 | |
| 1155 | // ----------------------------------------------------------------------------- |
| 1156 | // navigation |
| 1157 | // ----------------------------------------------------------------------------- |
| 1158 | |
| 1159 | wxTreeItemId wxGenericTreeCtrl::GetItemParent(const wxTreeItemId& item) const |
| 1160 | { |
| 1161 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1162 | |
| 1163 | return ((wxGenericTreeItem*) item.m_pItem)->GetParent(); |
| 1164 | } |
| 1165 | |
| 1166 | wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item, |
| 1167 | wxTreeItemIdValue& cookie) const |
| 1168 | { |
| 1169 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1170 | |
| 1171 | cookie = 0; |
| 1172 | return GetNextChild(item, cookie); |
| 1173 | } |
| 1174 | |
| 1175 | wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item, |
| 1176 | wxTreeItemIdValue& cookie) const |
| 1177 | { |
| 1178 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1179 | |
| 1180 | wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren(); |
| 1181 | |
| 1182 | // it's ok to cast cookie to size_t, we never have indices big enough to |
| 1183 | // overflow "void *" |
| 1184 | size_t *pIndex = (size_t *)&cookie; |
| 1185 | if ( *pIndex < children.Count() ) |
| 1186 | { |
| 1187 | return children.Item((*pIndex)++); |
| 1188 | } |
| 1189 | else |
| 1190 | { |
| 1191 | // there are no more of them |
| 1192 | return wxTreeItemId(); |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | #if WXWIN_COMPATIBILITY_2_4 |
| 1197 | |
| 1198 | wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item, |
| 1199 | long& cookie) const |
| 1200 | { |
| 1201 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1202 | |
| 1203 | cookie = 0; |
| 1204 | return GetNextChild(item, cookie); |
| 1205 | } |
| 1206 | |
| 1207 | wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item, |
| 1208 | long& cookie) const |
| 1209 | { |
| 1210 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1211 | |
| 1212 | wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren(); |
| 1213 | if ( (size_t)cookie < children.Count() ) |
| 1214 | { |
| 1215 | return children.Item((size_t)cookie++); |
| 1216 | } |
| 1217 | else |
| 1218 | { |
| 1219 | // there are no more of them |
| 1220 | return wxTreeItemId(); |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | #endif // WXWIN_COMPATIBILITY_2_4 |
| 1225 | |
| 1226 | wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const |
| 1227 | { |
| 1228 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1229 | |
| 1230 | wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren(); |
| 1231 | return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last())); |
| 1232 | } |
| 1233 | |
| 1234 | wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const |
| 1235 | { |
| 1236 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1237 | |
| 1238 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; |
| 1239 | wxGenericTreeItem *parent = i->GetParent(); |
| 1240 | if ( parent == NULL ) |
| 1241 | { |
| 1242 | // root item doesn't have any siblings |
| 1243 | return wxTreeItemId(); |
| 1244 | } |
| 1245 | |
| 1246 | wxArrayGenericTreeItems& siblings = parent->GetChildren(); |
| 1247 | int index = siblings.Index(i); |
| 1248 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? |
| 1249 | |
| 1250 | size_t n = (size_t)(index + 1); |
| 1251 | return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]); |
| 1252 | } |
| 1253 | |
| 1254 | wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const |
| 1255 | { |
| 1256 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1257 | |
| 1258 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; |
| 1259 | wxGenericTreeItem *parent = i->GetParent(); |
| 1260 | if ( parent == NULL ) |
| 1261 | { |
| 1262 | // root item doesn't have any siblings |
| 1263 | return wxTreeItemId(); |
| 1264 | } |
| 1265 | |
| 1266 | wxArrayGenericTreeItems& siblings = parent->GetChildren(); |
| 1267 | int index = siblings.Index(i); |
| 1268 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? |
| 1269 | |
| 1270 | return index == 0 ? wxTreeItemId() |
| 1271 | : wxTreeItemId(siblings[(size_t)(index - 1)]); |
| 1272 | } |
| 1273 | |
| 1274 | // Only for internal use right now, but should probably be public |
| 1275 | wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const |
| 1276 | { |
| 1277 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1278 | |
| 1279 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; |
| 1280 | |
| 1281 | // First see if there are any children. |
| 1282 | wxArrayGenericTreeItems& children = i->GetChildren(); |
| 1283 | if (children.GetCount() > 0) |
| 1284 | { |
| 1285 | return children.Item(0); |
| 1286 | } |
| 1287 | else |
| 1288 | { |
| 1289 | // Try a sibling of this or ancestor instead |
| 1290 | wxTreeItemId p = item; |
| 1291 | wxTreeItemId toFind; |
| 1292 | do |
| 1293 | { |
| 1294 | toFind = GetNextSibling(p); |
| 1295 | p = GetItemParent(p); |
| 1296 | } while (p.IsOk() && !toFind.IsOk()); |
| 1297 | return toFind; |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const |
| 1302 | { |
| 1303 | wxTreeItemId id = GetRootItem(); |
| 1304 | if (!id.IsOk()) |
| 1305 | return id; |
| 1306 | |
| 1307 | do |
| 1308 | { |
| 1309 | if (IsVisible(id)) |
| 1310 | return id; |
| 1311 | id = GetNext(id); |
| 1312 | } while (id.IsOk()); |
| 1313 | |
| 1314 | return wxTreeItemId(); |
| 1315 | } |
| 1316 | |
| 1317 | wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const |
| 1318 | { |
| 1319 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1320 | |
| 1321 | wxTreeItemId id = item; |
| 1322 | if (id.IsOk()) |
| 1323 | { |
| 1324 | while (id = GetNext(id), id.IsOk()) |
| 1325 | { |
| 1326 | if (IsVisible(id)) |
| 1327 | return id; |
| 1328 | } |
| 1329 | } |
| 1330 | return wxTreeItemId(); |
| 1331 | } |
| 1332 | |
| 1333 | wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const |
| 1334 | { |
| 1335 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 1336 | |
| 1337 | wxFAIL_MSG(wxT("not implemented")); |
| 1338 | |
| 1339 | return wxTreeItemId(); |
| 1340 | } |
| 1341 | |
| 1342 | // called by wxTextTreeCtrl when it marks itself for deletion |
| 1343 | void wxGenericTreeCtrl::ResetTextControl() |
| 1344 | { |
| 1345 | m_textCtrl = NULL; |
| 1346 | } |
| 1347 | |
| 1348 | // find the first item starting with the given prefix after the given item |
| 1349 | wxTreeItemId wxGenericTreeCtrl::FindItem(const wxTreeItemId& idParent, |
| 1350 | const wxString& prefixOrig) const |
| 1351 | { |
| 1352 | // match is case insensitive as this is more convenient to the user: having |
| 1353 | // to press Shift-letter to go to the item starting with a capital letter |
| 1354 | // would be too bothersome |
| 1355 | wxString prefix = prefixOrig.Lower(); |
| 1356 | |
| 1357 | // determine the starting point: we shouldn't take the current item (this |
| 1358 | // allows to switch between two items starting with the same letter just by |
| 1359 | // pressing it) but we shouldn't jump to the next one if the user is |
| 1360 | // continuing to type as otherwise he might easily skip the item he wanted |
| 1361 | wxTreeItemId id = idParent; |
| 1362 | if ( prefix.length() == 1 ) |
| 1363 | { |
| 1364 | id = GetNext(id); |
| 1365 | } |
| 1366 | |
| 1367 | // look for the item starting with the given prefix after it |
| 1368 | while ( id.IsOk() && !GetItemText(id).Lower().StartsWith(prefix) ) |
| 1369 | { |
| 1370 | id = GetNext(id); |
| 1371 | } |
| 1372 | |
| 1373 | // if we haven't found anything... |
| 1374 | if ( !id.IsOk() ) |
| 1375 | { |
| 1376 | // ... wrap to the beginning |
| 1377 | id = GetRootItem(); |
| 1378 | if ( HasFlag(wxTR_HIDE_ROOT) ) |
| 1379 | { |
| 1380 | // can't select virtual root |
| 1381 | id = GetNext(id); |
| 1382 | } |
| 1383 | |
| 1384 | // and try all the items (stop when we get to the one we started from) |
| 1385 | while ( id != idParent && !GetItemText(id).Lower().StartsWith(prefix) ) |
| 1386 | { |
| 1387 | id = GetNext(id); |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | return id; |
| 1392 | } |
| 1393 | |
| 1394 | // ----------------------------------------------------------------------------- |
| 1395 | // operations |
| 1396 | // ----------------------------------------------------------------------------- |
| 1397 | |
| 1398 | wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId, |
| 1399 | size_t previous, |
| 1400 | const wxString& text, |
| 1401 | int image, int selImage, |
| 1402 | wxTreeItemData *data) |
| 1403 | { |
| 1404 | wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem; |
| 1405 | if ( !parent ) |
| 1406 | { |
| 1407 | // should we give a warning here? |
| 1408 | return AddRoot(text, image, selImage, data); |
| 1409 | } |
| 1410 | |
| 1411 | m_dirty = true; // do this first so stuff below doesn't cause flicker |
| 1412 | |
| 1413 | wxGenericTreeItem *item = |
| 1414 | new wxGenericTreeItem( parent, text, image, selImage, data ); |
| 1415 | |
| 1416 | if ( data != NULL ) |
| 1417 | { |
| 1418 | data->m_pItem = item; |
| 1419 | } |
| 1420 | |
| 1421 | parent->Insert( item, previous ); |
| 1422 | |
| 1423 | return item; |
| 1424 | } |
| 1425 | |
| 1426 | wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text, |
| 1427 | int image, int selImage, |
| 1428 | wxTreeItemData *data) |
| 1429 | { |
| 1430 | wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") ); |
| 1431 | |
| 1432 | m_dirty = true; // do this first so stuff below doesn't cause flicker |
| 1433 | |
| 1434 | m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, |
| 1435 | image, selImage, data); |
| 1436 | if ( data != NULL ) |
| 1437 | { |
| 1438 | data->m_pItem = m_anchor; |
| 1439 | } |
| 1440 | |
| 1441 | if (HasFlag(wxTR_HIDE_ROOT)) |
| 1442 | { |
| 1443 | // if root is hidden, make sure we can navigate |
| 1444 | // into children |
| 1445 | m_anchor->SetHasPlus(); |
| 1446 | m_anchor->Expand(); |
| 1447 | CalculatePositions(); |
| 1448 | } |
| 1449 | |
| 1450 | if (!HasFlag(wxTR_MULTIPLE)) |
| 1451 | { |
| 1452 | m_current = m_key_current = m_anchor; |
| 1453 | m_current->SetHilight( true ); |
| 1454 | } |
| 1455 | |
| 1456 | return m_anchor; |
| 1457 | } |
| 1458 | |
| 1459 | wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent, |
| 1460 | const wxString& text, |
| 1461 | int image, int selImage, |
| 1462 | wxTreeItemData *data) |
| 1463 | { |
| 1464 | return DoInsertItem(parent, 0u, text, image, selImage, data); |
| 1465 | } |
| 1466 | |
| 1467 | wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId, |
| 1468 | const wxTreeItemId& idPrevious, |
| 1469 | const wxString& text, |
| 1470 | int image, int selImage, |
| 1471 | wxTreeItemData *data) |
| 1472 | { |
| 1473 | wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem; |
| 1474 | if ( !parent ) |
| 1475 | { |
| 1476 | // should we give a warning here? |
| 1477 | return AddRoot(text, image, selImage, data); |
| 1478 | } |
| 1479 | |
| 1480 | int index = -1; |
| 1481 | if (idPrevious.IsOk()) |
| 1482 | { |
| 1483 | index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem); |
| 1484 | wxASSERT_MSG( index != wxNOT_FOUND, |
| 1485 | wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") ); |
| 1486 | } |
| 1487 | |
| 1488 | return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data); |
| 1489 | } |
| 1490 | |
| 1491 | wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId, |
| 1492 | size_t before, |
| 1493 | const wxString& text, |
| 1494 | int image, int selImage, |
| 1495 | wxTreeItemData *data) |
| 1496 | { |
| 1497 | wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem; |
| 1498 | if ( !parent ) |
| 1499 | { |
| 1500 | // should we give a warning here? |
| 1501 | return AddRoot(text, image, selImage, data); |
| 1502 | } |
| 1503 | |
| 1504 | return DoInsertItem(parentId, before, text, image, selImage, data); |
| 1505 | } |
| 1506 | |
| 1507 | wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId, |
| 1508 | const wxString& text, |
| 1509 | int image, int selImage, |
| 1510 | wxTreeItemData *data) |
| 1511 | { |
| 1512 | wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem; |
| 1513 | if ( !parent ) |
| 1514 | { |
| 1515 | // should we give a warning here? |
| 1516 | return AddRoot(text, image, selImage, data); |
| 1517 | } |
| 1518 | |
| 1519 | return DoInsertItem( parent, parent->GetChildren().Count(), text, |
| 1520 | image, selImage, data); |
| 1521 | } |
| 1522 | |
| 1523 | void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item) |
| 1524 | { |
| 1525 | wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() ); |
| 1526 | event.m_item = item; |
| 1527 | event.SetEventObject( this ); |
| 1528 | ProcessEvent( event ); |
| 1529 | } |
| 1530 | |
| 1531 | // Don't leave edit or selection on a child which is about to disappear |
| 1532 | void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem* item) |
| 1533 | { |
| 1534 | if (m_textCtrl != NULL && item != m_textCtrl->item() && IsDescendantOf(item, m_textCtrl->item())) { |
| 1535 | m_textCtrl->StopEditing(); |
| 1536 | } |
| 1537 | if (item != m_key_current && IsDescendantOf(item, m_key_current)) { |
| 1538 | m_key_current = NULL; |
| 1539 | } |
| 1540 | if (IsDescendantOf(item, m_select_me)) { |
| 1541 | m_select_me = item; |
| 1542 | } |
| 1543 | if (item != m_current && IsDescendantOf(item, m_current)) { |
| 1544 | m_current->SetHilight( false ); |
| 1545 | m_current = NULL; |
| 1546 | m_select_me = item; |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId) |
| 1551 | { |
| 1552 | m_dirty = true; // do this first so stuff below doesn't cause flicker |
| 1553 | |
| 1554 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; |
| 1555 | ChildrenClosing(item); |
| 1556 | item->DeleteChildren(this); |
| 1557 | } |
| 1558 | |
| 1559 | void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId) |
| 1560 | { |
| 1561 | m_dirty = true; // do this first so stuff below doesn't cause flicker |
| 1562 | |
| 1563 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; |
| 1564 | |
| 1565 | if (m_textCtrl != NULL && IsDescendantOf(item, m_textCtrl->item())) |
| 1566 | { |
| 1567 | // can't delete the item being edited, cancel editing it first |
| 1568 | m_textCtrl->StopEditing(); |
| 1569 | } |
| 1570 | |
| 1571 | wxGenericTreeItem *parent = item->GetParent(); |
| 1572 | |
| 1573 | // don't keep stale pointers around! |
| 1574 | if ( IsDescendantOf(item, m_key_current) ) |
| 1575 | { |
| 1576 | // Don't silently change the selection: |
| 1577 | // do it properly in idle time, so event |
| 1578 | // handlers get called. |
| 1579 | |
| 1580 | // m_key_current = parent; |
| 1581 | m_key_current = NULL; |
| 1582 | } |
| 1583 | |
| 1584 | // m_select_me records whether we need to select |
| 1585 | // a different item, in idle time. |
| 1586 | if ( m_select_me && IsDescendantOf(item, m_select_me) ) |
| 1587 | { |
| 1588 | m_select_me = parent; |
| 1589 | } |
| 1590 | |
| 1591 | if ( IsDescendantOf(item, m_current) ) |
| 1592 | { |
| 1593 | // Don't silently change the selection: |
| 1594 | // do it properly in idle time, so event |
| 1595 | // handlers get called. |
| 1596 | |
| 1597 | // m_current = parent; |
| 1598 | m_current = NULL; |
| 1599 | m_select_me = parent; |
| 1600 | } |
| 1601 | |
| 1602 | // remove the item from the tree |
| 1603 | if ( parent ) |
| 1604 | { |
| 1605 | parent->GetChildren().Remove( item ); // remove by value |
| 1606 | } |
| 1607 | else // deleting the root |
| 1608 | { |
| 1609 | // nothing will be left in the tree |
| 1610 | m_anchor = NULL; |
| 1611 | } |
| 1612 | |
| 1613 | // and delete all of its children and the item itself now |
| 1614 | item->DeleteChildren(this); |
| 1615 | SendDeleteEvent(item); |
| 1616 | |
| 1617 | if (item == m_select_me) |
| 1618 | m_select_me = NULL; |
| 1619 | |
| 1620 | delete item; |
| 1621 | } |
| 1622 | |
| 1623 | void wxGenericTreeCtrl::DeleteAllItems() |
| 1624 | { |
| 1625 | if ( m_anchor ) |
| 1626 | { |
| 1627 | Delete(m_anchor); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId) |
| 1632 | { |
| 1633 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; |
| 1634 | |
| 1635 | wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") ); |
| 1636 | wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(), |
| 1637 | _T("can't expand hidden root") ); |
| 1638 | |
| 1639 | if ( !item->HasPlus() ) |
| 1640 | return; |
| 1641 | |
| 1642 | if ( item->IsExpanded() ) |
| 1643 | return; |
| 1644 | |
| 1645 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() ); |
| 1646 | event.m_item = item; |
| 1647 | event.SetEventObject( this ); |
| 1648 | |
| 1649 | if ( ProcessEvent( event ) && !event.IsAllowed() ) |
| 1650 | { |
| 1651 | // cancelled by program |
| 1652 | return; |
| 1653 | } |
| 1654 | |
| 1655 | item->Expand(); |
| 1656 | CalculatePositions(); |
| 1657 | |
| 1658 | RefreshSubtree(item); |
| 1659 | |
| 1660 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED); |
| 1661 | ProcessEvent( event ); |
| 1662 | } |
| 1663 | |
| 1664 | void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item) |
| 1665 | { |
| 1666 | if ( !HasFlag(wxTR_HIDE_ROOT) || item != GetRootItem()) |
| 1667 | { |
| 1668 | Expand(item); |
| 1669 | if ( !IsExpanded(item) ) |
| 1670 | return; |
| 1671 | } |
| 1672 | |
| 1673 | wxTreeItemIdValue cookie; |
| 1674 | wxTreeItemId child = GetFirstChild(item, cookie); |
| 1675 | while ( child.IsOk() ) |
| 1676 | { |
| 1677 | ExpandAll(child); |
| 1678 | |
| 1679 | child = GetNextChild(item, cookie); |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId) |
| 1684 | { |
| 1685 | wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(), |
| 1686 | _T("can't collapse hidden root") ); |
| 1687 | |
| 1688 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; |
| 1689 | |
| 1690 | if ( !item->IsExpanded() ) |
| 1691 | return; |
| 1692 | |
| 1693 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() ); |
| 1694 | event.m_item = item; |
| 1695 | event.SetEventObject( this ); |
| 1696 | if ( ProcessEvent( event ) && !event.IsAllowed() ) |
| 1697 | { |
| 1698 | // cancelled by program |
| 1699 | return; |
| 1700 | } |
| 1701 | |
| 1702 | ChildrenClosing(item); |
| 1703 | item->Collapse(); |
| 1704 | |
| 1705 | #if 0 // TODO why should items be collapsed recursively? |
| 1706 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 1707 | size_t count = children.Count(); |
| 1708 | for ( size_t n = 0; n < count; n++ ) |
| 1709 | { |
| 1710 | Collapse(children[n]); |
| 1711 | } |
| 1712 | #endif |
| 1713 | |
| 1714 | CalculatePositions(); |
| 1715 | |
| 1716 | RefreshSubtree(item); |
| 1717 | |
| 1718 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED); |
| 1719 | ProcessEvent( event ); |
| 1720 | } |
| 1721 | |
| 1722 | void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item) |
| 1723 | { |
| 1724 | Collapse(item); |
| 1725 | DeleteChildren(item); |
| 1726 | } |
| 1727 | |
| 1728 | void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId) |
| 1729 | { |
| 1730 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; |
| 1731 | |
| 1732 | if (item->IsExpanded()) |
| 1733 | Collapse(itemId); |
| 1734 | else |
| 1735 | Expand(itemId); |
| 1736 | } |
| 1737 | |
| 1738 | void wxGenericTreeCtrl::Unselect() |
| 1739 | { |
| 1740 | if (m_current) |
| 1741 | { |
| 1742 | m_current->SetHilight( false ); |
| 1743 | RefreshLine( m_current ); |
| 1744 | |
| 1745 | m_current = NULL; |
| 1746 | m_select_me = NULL; |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item) |
| 1751 | { |
| 1752 | if (item->IsSelected()) |
| 1753 | { |
| 1754 | item->SetHilight(false); |
| 1755 | RefreshLine(item); |
| 1756 | } |
| 1757 | |
| 1758 | if (item->HasChildren()) |
| 1759 | { |
| 1760 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 1761 | size_t count = children.Count(); |
| 1762 | for ( size_t n = 0; n < count; ++n ) |
| 1763 | { |
| 1764 | UnselectAllChildren(children[n]); |
| 1765 | } |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | void wxGenericTreeCtrl::UnselectAll() |
| 1770 | { |
| 1771 | wxTreeItemId rootItem = GetRootItem(); |
| 1772 | |
| 1773 | // the tree might not have the root item at all |
| 1774 | if ( rootItem ) |
| 1775 | { |
| 1776 | UnselectAllChildren((wxGenericTreeItem*) rootItem.m_pItem); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | // Recursive function ! |
| 1781 | // To stop we must have crt_item<last_item |
| 1782 | // Algorithm : |
| 1783 | // Tag all next children, when no more children, |
| 1784 | // Move to parent (not to tag) |
| 1785 | // Keep going... if we found last_item, we stop. |
| 1786 | bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select) |
| 1787 | { |
| 1788 | wxGenericTreeItem *parent = crt_item->GetParent(); |
| 1789 | |
| 1790 | if (parent == NULL) // This is root item |
| 1791 | return TagAllChildrenUntilLast(crt_item, last_item, select); |
| 1792 | |
| 1793 | wxArrayGenericTreeItems& children = parent->GetChildren(); |
| 1794 | int index = children.Index(crt_item); |
| 1795 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? |
| 1796 | |
| 1797 | size_t count = children.Count(); |
| 1798 | for (size_t n=(size_t)(index+1); n<count; ++n) |
| 1799 | { |
| 1800 | if (TagAllChildrenUntilLast(children[n], last_item, select)) return true; |
| 1801 | } |
| 1802 | |
| 1803 | return TagNextChildren(parent, last_item, select); |
| 1804 | } |
| 1805 | |
| 1806 | bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select) |
| 1807 | { |
| 1808 | crt_item->SetHilight(select); |
| 1809 | RefreshLine(crt_item); |
| 1810 | |
| 1811 | if (crt_item==last_item) |
| 1812 | return true; |
| 1813 | |
| 1814 | if (crt_item->HasChildren()) |
| 1815 | { |
| 1816 | wxArrayGenericTreeItems& children = crt_item->GetChildren(); |
| 1817 | size_t count = children.Count(); |
| 1818 | for ( size_t n = 0; n < count; ++n ) |
| 1819 | { |
| 1820 | if (TagAllChildrenUntilLast(children[n], last_item, select)) |
| 1821 | return true; |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | return false; |
| 1826 | } |
| 1827 | |
| 1828 | void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2) |
| 1829 | { |
| 1830 | m_select_me = NULL; |
| 1831 | |
| 1832 | // item2 is not necessary after item1 |
| 1833 | // choice first' and 'last' between item1 and item2 |
| 1834 | wxGenericTreeItem *first= (item1->GetY()<item2->GetY()) ? item1 : item2; |
| 1835 | wxGenericTreeItem *last = (item1->GetY()<item2->GetY()) ? item2 : item1; |
| 1836 | |
| 1837 | bool select = m_current->IsSelected(); |
| 1838 | |
| 1839 | if ( TagAllChildrenUntilLast(first,last,select) ) |
| 1840 | return; |
| 1841 | |
| 1842 | TagNextChildren(first,last,select); |
| 1843 | } |
| 1844 | |
| 1845 | void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId& itemId, |
| 1846 | bool unselect_others, |
| 1847 | bool extended_select) |
| 1848 | { |
| 1849 | wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") ); |
| 1850 | |
| 1851 | m_select_me = NULL; |
| 1852 | |
| 1853 | bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE); |
| 1854 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; |
| 1855 | |
| 1856 | //wxCHECK_RET( ( (!unselect_others) && is_single), |
| 1857 | // wxT("this is a single selection tree") ); |
| 1858 | |
| 1859 | // to keep going anyhow !!! |
| 1860 | if (is_single) |
| 1861 | { |
| 1862 | if (item->IsSelected()) |
| 1863 | return; // nothing to do |
| 1864 | unselect_others = true; |
| 1865 | extended_select = false; |
| 1866 | } |
| 1867 | else if ( unselect_others && item->IsSelected() ) |
| 1868 | { |
| 1869 | // selection change if there is more than one item currently selected |
| 1870 | wxArrayTreeItemIds selected_items; |
| 1871 | if ( GetSelections(selected_items) == 1 ) |
| 1872 | return; |
| 1873 | } |
| 1874 | |
| 1875 | wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() ); |
| 1876 | event.m_item = item; |
| 1877 | event.m_itemOld = m_current; |
| 1878 | event.SetEventObject( this ); |
| 1879 | // TODO : Here we don't send any selection mode yet ! |
| 1880 | |
| 1881 | if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() ) |
| 1882 | return; |
| 1883 | |
| 1884 | wxTreeItemId parent = GetItemParent( itemId ); |
| 1885 | while (parent.IsOk()) |
| 1886 | { |
| 1887 | if (!IsExpanded(parent)) |
| 1888 | Expand( parent ); |
| 1889 | |
| 1890 | parent = GetItemParent( parent ); |
| 1891 | } |
| 1892 | |
| 1893 | EnsureVisible( itemId ); |
| 1894 | |
| 1895 | // ctrl press |
| 1896 | if (unselect_others) |
| 1897 | { |
| 1898 | if (is_single) Unselect(); // to speed up thing |
| 1899 | else UnselectAll(); |
| 1900 | } |
| 1901 | |
| 1902 | // shift press |
| 1903 | if (extended_select) |
| 1904 | { |
| 1905 | if ( !m_current ) |
| 1906 | { |
| 1907 | m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem; |
| 1908 | } |
| 1909 | |
| 1910 | // don't change the mark (m_current) |
| 1911 | SelectItemRange(m_current, item); |
| 1912 | } |
| 1913 | else |
| 1914 | { |
| 1915 | bool select = true; // the default |
| 1916 | |
| 1917 | // Check if we need to toggle hilight (ctrl mode) |
| 1918 | if (!unselect_others) |
| 1919 | select=!item->IsSelected(); |
| 1920 | |
| 1921 | m_current = m_key_current = item; |
| 1922 | m_current->SetHilight(select); |
| 1923 | RefreshLine( m_current ); |
| 1924 | } |
| 1925 | |
| 1926 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); |
| 1927 | GetEventHandler()->ProcessEvent( event ); |
| 1928 | } |
| 1929 | |
| 1930 | void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId, bool select) |
| 1931 | { |
| 1932 | if ( select ) |
| 1933 | { |
| 1934 | DoSelectItem(itemId); |
| 1935 | } |
| 1936 | else // deselect |
| 1937 | { |
| 1938 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; |
| 1939 | wxCHECK_RET( item, wxT("SelectItem(): invalid tree item") ); |
| 1940 | |
| 1941 | item->SetHilight(false); |
| 1942 | RefreshLine(item); |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item, |
| 1947 | wxArrayTreeItemIds &array) const |
| 1948 | { |
| 1949 | if ( item->IsSelected() ) |
| 1950 | array.Add(wxTreeItemId(item)); |
| 1951 | |
| 1952 | if ( item->HasChildren() ) |
| 1953 | { |
| 1954 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 1955 | size_t count = children.GetCount(); |
| 1956 | for ( size_t n = 0; n < count; ++n ) |
| 1957 | FillArray(children[n], array); |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const |
| 1962 | { |
| 1963 | array.Empty(); |
| 1964 | wxTreeItemId idRoot = GetRootItem(); |
| 1965 | if ( idRoot.IsOk() ) |
| 1966 | { |
| 1967 | FillArray((wxGenericTreeItem*) idRoot.m_pItem, array); |
| 1968 | } |
| 1969 | //else: the tree is empty, so no selections |
| 1970 | |
| 1971 | return array.Count(); |
| 1972 | } |
| 1973 | |
| 1974 | void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item) |
| 1975 | { |
| 1976 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 1977 | |
| 1978 | if (!item.IsOk()) return; |
| 1979 | |
| 1980 | wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem; |
| 1981 | |
| 1982 | // first expand all parent branches |
| 1983 | wxGenericTreeItem *parent = gitem->GetParent(); |
| 1984 | |
| 1985 | if ( HasFlag(wxTR_HIDE_ROOT) ) |
| 1986 | { |
| 1987 | while ( parent && parent != m_anchor ) |
| 1988 | { |
| 1989 | Expand(parent); |
| 1990 | parent = parent->GetParent(); |
| 1991 | } |
| 1992 | } |
| 1993 | else |
| 1994 | { |
| 1995 | while ( parent ) |
| 1996 | { |
| 1997 | Expand(parent); |
| 1998 | parent = parent->GetParent(); |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | //if (parent) CalculatePositions(); |
| 2003 | |
| 2004 | ScrollTo(item); |
| 2005 | } |
| 2006 | |
| 2007 | void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item) |
| 2008 | { |
| 2009 | if (!item.IsOk()) return; |
| 2010 | |
| 2011 | // We have to call this here because the label in |
| 2012 | // question might just have been added and no screen |
| 2013 | // update taken place. |
| 2014 | if (m_dirty) |
| 2015 | #if defined( __WXMSW__ ) || defined(__WXMAC__) |
| 2016 | Update(); |
| 2017 | #else |
| 2018 | wxYieldIfNeeded(); |
| 2019 | #endif |
| 2020 | wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem; |
| 2021 | |
| 2022 | // now scroll to the item |
| 2023 | int item_y = gitem->GetY(); |
| 2024 | |
| 2025 | int start_x = 0; |
| 2026 | int start_y = 0; |
| 2027 | GetViewStart( &start_x, &start_y ); |
| 2028 | start_y *= PIXELS_PER_UNIT; |
| 2029 | |
| 2030 | int client_h = 0; |
| 2031 | int client_w = 0; |
| 2032 | GetClientSize( &client_w, &client_h ); |
| 2033 | |
| 2034 | if (item_y < start_y+3) |
| 2035 | { |
| 2036 | // going down |
| 2037 | int x = 0; |
| 2038 | int y = 0; |
| 2039 | m_anchor->GetSize( x, y, this ); |
| 2040 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 2041 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 2042 | int x_pos = GetScrollPos( wxHORIZONTAL ); |
| 2043 | // Item should appear at top |
| 2044 | SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT ); |
| 2045 | } |
| 2046 | else if (item_y+GetLineHeight(gitem) > start_y+client_h) |
| 2047 | { |
| 2048 | // going up |
| 2049 | int x = 0; |
| 2050 | int y = 0; |
| 2051 | m_anchor->GetSize( x, y, this ); |
| 2052 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 2053 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 2054 | item_y += PIXELS_PER_UNIT+2; |
| 2055 | int x_pos = GetScrollPos( wxHORIZONTAL ); |
| 2056 | // Item should appear at bottom |
| 2057 | SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, (item_y+GetLineHeight(gitem)-client_h)/PIXELS_PER_UNIT ); |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | // FIXME: tree sorting functions are not reentrant and not MT-safe! |
| 2062 | static wxGenericTreeCtrl *s_treeBeingSorted = NULL; |
| 2063 | |
| 2064 | static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1, |
| 2065 | wxGenericTreeItem **item2) |
| 2066 | { |
| 2067 | wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") ); |
| 2068 | |
| 2069 | return s_treeBeingSorted->OnCompareItems(*item1, *item2); |
| 2070 | } |
| 2071 | |
| 2072 | int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
| 2073 | const wxTreeItemId& item2) |
| 2074 | { |
| 2075 | return wxStrcmp(GetItemText(item1), GetItemText(item2)); |
| 2076 | } |
| 2077 | |
| 2078 | void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId) |
| 2079 | { |
| 2080 | wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") ); |
| 2081 | |
| 2082 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; |
| 2083 | |
| 2084 | wxCHECK_RET( !s_treeBeingSorted, |
| 2085 | wxT("wxGenericTreeCtrl::SortChildren is not reentrant") ); |
| 2086 | |
| 2087 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 2088 | if ( children.Count() > 1 ) |
| 2089 | { |
| 2090 | m_dirty = true; |
| 2091 | |
| 2092 | s_treeBeingSorted = this; |
| 2093 | children.Sort(tree_ctrl_compare_func); |
| 2094 | s_treeBeingSorted = NULL; |
| 2095 | } |
| 2096 | //else: don't make the tree dirty as nothing changed |
| 2097 | } |
| 2098 | |
| 2099 | wxImageList *wxGenericTreeCtrl::GetImageList() const |
| 2100 | { |
| 2101 | return m_imageListNormal; |
| 2102 | } |
| 2103 | |
| 2104 | wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const |
| 2105 | { |
| 2106 | return m_imageListButtons; |
| 2107 | } |
| 2108 | |
| 2109 | wxImageList *wxGenericTreeCtrl::GetStateImageList() const |
| 2110 | { |
| 2111 | return m_imageListState; |
| 2112 | } |
| 2113 | |
| 2114 | void wxGenericTreeCtrl::CalculateLineHeight() |
| 2115 | { |
| 2116 | wxClientDC dc(this); |
| 2117 | m_lineHeight = (int)(dc.GetCharHeight() + 4); |
| 2118 | |
| 2119 | if ( m_imageListNormal ) |
| 2120 | { |
| 2121 | // Calculate a m_lineHeight value from the normal Image sizes. |
| 2122 | // May be toggle off. Then wxGenericTreeCtrl will spread when |
| 2123 | // necessary (which might look ugly). |
| 2124 | int n = m_imageListNormal->GetImageCount(); |
| 2125 | for (int i = 0; i < n ; i++) |
| 2126 | { |
| 2127 | int width = 0, height = 0; |
| 2128 | m_imageListNormal->GetSize(i, width, height); |
| 2129 | if (height > m_lineHeight) m_lineHeight = height; |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | if (m_imageListButtons) |
| 2134 | { |
| 2135 | // Calculate a m_lineHeight value from the Button image sizes. |
| 2136 | // May be toggle off. Then wxGenericTreeCtrl will spread when |
| 2137 | // necessary (which might look ugly). |
| 2138 | int n = m_imageListButtons->GetImageCount(); |
| 2139 | for (int i = 0; i < n ; i++) |
| 2140 | { |
| 2141 | int width = 0, height = 0; |
| 2142 | m_imageListButtons->GetSize(i, width, height); |
| 2143 | if (height > m_lineHeight) m_lineHeight = height; |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | if (m_lineHeight < 30) |
| 2148 | m_lineHeight += 2; // at least 2 pixels |
| 2149 | else |
| 2150 | m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing |
| 2151 | } |
| 2152 | |
| 2153 | void wxGenericTreeCtrl::SetImageList(wxImageList *imageList) |
| 2154 | { |
| 2155 | if (m_ownsImageListNormal) delete m_imageListNormal; |
| 2156 | m_imageListNormal = imageList; |
| 2157 | m_ownsImageListNormal = false; |
| 2158 | m_dirty = true; |
| 2159 | // Don't do any drawing if we're setting the list to NULL, |
| 2160 | // since we may be in the process of deleting the tree control. |
| 2161 | if (imageList) |
| 2162 | CalculateLineHeight(); |
| 2163 | } |
| 2164 | |
| 2165 | void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList) |
| 2166 | { |
| 2167 | if (m_ownsImageListState) delete m_imageListState; |
| 2168 | m_imageListState = imageList; |
| 2169 | m_ownsImageListState = false; |
| 2170 | } |
| 2171 | |
| 2172 | void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList) |
| 2173 | { |
| 2174 | if (m_ownsImageListButtons) delete m_imageListButtons; |
| 2175 | m_imageListButtons = imageList; |
| 2176 | m_ownsImageListButtons = false; |
| 2177 | m_dirty = true; |
| 2178 | CalculateLineHeight(); |
| 2179 | } |
| 2180 | |
| 2181 | void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList) |
| 2182 | { |
| 2183 | SetImageList(imageList); |
| 2184 | m_ownsImageListNormal = true; |
| 2185 | } |
| 2186 | |
| 2187 | void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList) |
| 2188 | { |
| 2189 | SetStateImageList(imageList); |
| 2190 | m_ownsImageListState = true; |
| 2191 | } |
| 2192 | |
| 2193 | void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList) |
| 2194 | { |
| 2195 | SetButtonsImageList(imageList); |
| 2196 | m_ownsImageListButtons = true; |
| 2197 | } |
| 2198 | |
| 2199 | // ----------------------------------------------------------------------------- |
| 2200 | // helpers |
| 2201 | // ----------------------------------------------------------------------------- |
| 2202 | |
| 2203 | void wxGenericTreeCtrl::AdjustMyScrollbars() |
| 2204 | { |
| 2205 | if (m_anchor) |
| 2206 | { |
| 2207 | int x = 0, y = 0; |
| 2208 | m_anchor->GetSize( x, y, this ); |
| 2209 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 2210 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 2211 | int x_pos = GetScrollPos( wxHORIZONTAL ); |
| 2212 | int y_pos = GetScrollPos( wxVERTICAL ); |
| 2213 | SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos ); |
| 2214 | } |
| 2215 | else |
| 2216 | { |
| 2217 | SetScrollbars( 0, 0, 0, 0 ); |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const |
| 2222 | { |
| 2223 | if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT) |
| 2224 | return item->GetHeight(); |
| 2225 | else |
| 2226 | return m_lineHeight; |
| 2227 | } |
| 2228 | |
| 2229 | void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc) |
| 2230 | { |
| 2231 | // TODO implement "state" icon on items |
| 2232 | |
| 2233 | wxTreeItemAttr *attr = item->GetAttributes(); |
| 2234 | if ( attr && attr->HasFont() ) |
| 2235 | dc.SetFont(attr->GetFont()); |
| 2236 | else if (item->IsBold()) |
| 2237 | dc.SetFont(m_boldFont); |
| 2238 | |
| 2239 | long text_w = 0, text_h = 0; |
| 2240 | dc.GetTextExtent( item->GetText(), &text_w, &text_h ); |
| 2241 | |
| 2242 | int image_h = 0, image_w = 0; |
| 2243 | int image = item->GetCurrentImage(); |
| 2244 | if ( image != NO_IMAGE ) |
| 2245 | { |
| 2246 | if ( m_imageListNormal ) |
| 2247 | { |
| 2248 | m_imageListNormal->GetSize( image, image_w, image_h ); |
| 2249 | image_w += 4; |
| 2250 | } |
| 2251 | else |
| 2252 | { |
| 2253 | image = NO_IMAGE; |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | int total_h = GetLineHeight(item); |
| 2258 | bool drawItemBackground = false; |
| 2259 | |
| 2260 | if ( item->IsSelected() ) |
| 2261 | { |
| 2262 | // under mac selections are only a rectangle in case they don't have the focus |
| 2263 | #ifdef __WXMAC__ |
| 2264 | if ( !m_hasFocus ) |
| 2265 | { |
| 2266 | dc.SetBrush( *wxTRANSPARENT_BRUSH ) ; |
| 2267 | dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) , 1 , wxSOLID ) ) ; |
| 2268 | } |
| 2269 | else |
| 2270 | { |
| 2271 | dc.SetBrush( *m_hilightBrush ) ; |
| 2272 | } |
| 2273 | #else |
| 2274 | dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush)); |
| 2275 | #endif |
| 2276 | drawItemBackground = true; |
| 2277 | } |
| 2278 | else |
| 2279 | { |
| 2280 | wxColour colBg; |
| 2281 | if ( attr && attr->HasBackgroundColour() ) |
| 2282 | { |
| 2283 | drawItemBackground = true; |
| 2284 | colBg = attr->GetBackgroundColour(); |
| 2285 | } |
| 2286 | else |
| 2287 | colBg = m_backgroundColour; |
| 2288 | dc.SetBrush(wxBrush(colBg, wxSOLID)); |
| 2289 | } |
| 2290 | |
| 2291 | int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0; |
| 2292 | |
| 2293 | if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) ) |
| 2294 | { |
| 2295 | int x, y, w, h; |
| 2296 | |
| 2297 | DoGetPosition(&x, &y); |
| 2298 | DoGetSize(&w, &h); |
| 2299 | dc.DrawRectangle(x, item->GetY()+offset, w, total_h-offset); |
| 2300 | } |
| 2301 | else |
| 2302 | { |
| 2303 | if ( item->IsSelected() && image != NO_IMAGE ) |
| 2304 | { |
| 2305 | // If it's selected, and there's an image, then we should |
| 2306 | // take care to leave the area under the image painted in the |
| 2307 | // background colour. |
| 2308 | dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset, |
| 2309 | item->GetWidth() - image_w + 2, total_h-offset ); |
| 2310 | } |
| 2311 | // On GTK+ 2, drawing a 'normal' background is wrong for themes that |
| 2312 | // don't allow backgrounds to be customized. Not drawing the background, |
| 2313 | // except for custom item backgrounds, works for both kinds of theme. |
| 2314 | else if (drawItemBackground) |
| 2315 | { |
| 2316 | dc.DrawRectangle( item->GetX()-2, item->GetY()+offset, |
| 2317 | item->GetWidth()+2, total_h-offset ); |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | if ( image != NO_IMAGE ) |
| 2322 | { |
| 2323 | dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h ); |
| 2324 | m_imageListNormal->Draw( image, dc, |
| 2325 | item->GetX(), |
| 2326 | item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0), |
| 2327 | wxIMAGELIST_DRAW_TRANSPARENT ); |
| 2328 | dc.DestroyClippingRegion(); |
| 2329 | } |
| 2330 | |
| 2331 | dc.SetBackgroundMode(wxTRANSPARENT); |
| 2332 | int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0; |
| 2333 | dc.DrawText( item->GetText(), |
| 2334 | (wxCoord)(image_w + item->GetX()), |
| 2335 | (wxCoord)(item->GetY() + extraH)); |
| 2336 | |
| 2337 | // restore normal font |
| 2338 | dc.SetFont( m_normalFont ); |
| 2339 | } |
| 2340 | |
| 2341 | // Now y stands for the top of the item, whereas it used to stand for middle ! |
| 2342 | void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ) |
| 2343 | { |
| 2344 | int x = level*m_indent; |
| 2345 | if (!HasFlag(wxTR_HIDE_ROOT)) |
| 2346 | { |
| 2347 | x += m_indent; |
| 2348 | } |
| 2349 | else if (level == 0) |
| 2350 | { |
| 2351 | // always expand hidden root |
| 2352 | int origY = y; |
| 2353 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 2354 | int count = children.Count(); |
| 2355 | if (count > 0) |
| 2356 | { |
| 2357 | int n = 0, oldY; |
| 2358 | do { |
| 2359 | oldY = y; |
| 2360 | PaintLevel(children[n], dc, 1, y); |
| 2361 | } while (++n < count); |
| 2362 | |
| 2363 | if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0) |
| 2364 | { |
| 2365 | // draw line down to last child |
| 2366 | origY += GetLineHeight(children[0])>>1; |
| 2367 | oldY += GetLineHeight(children[n-1])>>1; |
| 2368 | dc.DrawLine(3, origY, 3, oldY); |
| 2369 | } |
| 2370 | } |
| 2371 | return; |
| 2372 | } |
| 2373 | |
| 2374 | item->SetX(x+m_spacing); |
| 2375 | item->SetY(y); |
| 2376 | |
| 2377 | int h = GetLineHeight(item); |
| 2378 | int y_top = y; |
| 2379 | int y_mid = y_top + (h>>1); |
| 2380 | y += h; |
| 2381 | |
| 2382 | int exposed_x = dc.LogicalToDeviceX(0); |
| 2383 | int exposed_y = dc.LogicalToDeviceY(y_top); |
| 2384 | |
| 2385 | if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much |
| 2386 | { |
| 2387 | wxPen *pen = |
| 2388 | #ifndef __WXMAC__ |
| 2389 | // don't draw rect outline if we already have the |
| 2390 | // background color under Mac |
| 2391 | (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN : |
| 2392 | #endif // !__WXMAC__ |
| 2393 | wxTRANSPARENT_PEN; |
| 2394 | |
| 2395 | wxColour colText; |
| 2396 | if ( item->IsSelected() ) |
| 2397 | { |
| 2398 | colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); |
| 2399 | } |
| 2400 | else |
| 2401 | { |
| 2402 | wxTreeItemAttr *attr = item->GetAttributes(); |
| 2403 | if (attr && attr->HasTextColour()) |
| 2404 | colText = attr->GetTextColour(); |
| 2405 | else |
| 2406 | colText = GetForegroundColour(); |
| 2407 | } |
| 2408 | |
| 2409 | // prepare to draw |
| 2410 | dc.SetTextForeground(colText); |
| 2411 | dc.SetPen(*pen); |
| 2412 | |
| 2413 | // draw |
| 2414 | PaintItem(item, dc); |
| 2415 | |
| 2416 | if (HasFlag(wxTR_ROW_LINES)) |
| 2417 | { |
| 2418 | // if the background colour is white, choose a |
| 2419 | // contrasting color for the lines |
| 2420 | dc.SetPen(*((GetBackgroundColour() == *wxWHITE) |
| 2421 | ? wxMEDIUM_GREY_PEN : wxWHITE_PEN)); |
| 2422 | dc.DrawLine(0, y_top, 10000, y_top); |
| 2423 | dc.DrawLine(0, y, 10000, y); |
| 2424 | } |
| 2425 | |
| 2426 | // restore DC objects |
| 2427 | dc.SetBrush(*wxWHITE_BRUSH); |
| 2428 | dc.SetPen(m_dottedPen); |
| 2429 | dc.SetTextForeground(*wxBLACK); |
| 2430 | |
| 2431 | if ( !HasFlag(wxTR_NO_LINES) ) |
| 2432 | { |
| 2433 | // draw the horizontal line here |
| 2434 | int x_start = x; |
| 2435 | if (x > (signed)m_indent) |
| 2436 | x_start -= m_indent; |
| 2437 | else if (HasFlag(wxTR_LINES_AT_ROOT)) |
| 2438 | x_start = 3; |
| 2439 | dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid); |
| 2440 | } |
| 2441 | |
| 2442 | // should the item show a button? |
| 2443 | if ( item->HasPlus() && HasButtons() ) |
| 2444 | { |
| 2445 | if ( m_imageListButtons ) |
| 2446 | { |
| 2447 | // draw the image button here |
| 2448 | int image_h = 0, |
| 2449 | image_w = 0; |
| 2450 | int image = item->IsExpanded() ? wxTreeItemIcon_Expanded |
| 2451 | : wxTreeItemIcon_Normal; |
| 2452 | if ( item->IsSelected() ) |
| 2453 | image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal; |
| 2454 | |
| 2455 | m_imageListButtons->GetSize(image, image_w, image_h); |
| 2456 | int xx = x - image_w/2; |
| 2457 | int yy = y_mid - image_h/2; |
| 2458 | |
| 2459 | wxDCClipper clip(dc, xx, yy, image_w, image_h); |
| 2460 | m_imageListButtons->Draw(image, dc, xx, yy, |
| 2461 | wxIMAGELIST_DRAW_TRANSPARENT); |
| 2462 | } |
| 2463 | else // no custom buttons |
| 2464 | { |
| 2465 | static const int wImage = 9; |
| 2466 | static const int hImage = 9; |
| 2467 | |
| 2468 | int flag = 0; |
| 2469 | if (item->IsExpanded()) |
| 2470 | flag |= wxCONTROL_EXPANDED; |
| 2471 | if (item == m_underMouse) |
| 2472 | flag |= wxCONTROL_CURRENT; |
| 2473 | |
| 2474 | wxRendererNative::Get().DrawTreeItemButton |
| 2475 | ( |
| 2476 | this, |
| 2477 | dc, |
| 2478 | wxRect(x - wImage/2, |
| 2479 | y_mid - hImage/2, |
| 2480 | wImage, hImage), |
| 2481 | flag |
| 2482 | ); |
| 2483 | } |
| 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | if (item->IsExpanded()) |
| 2488 | { |
| 2489 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 2490 | int count = children.Count(); |
| 2491 | if (count > 0) |
| 2492 | { |
| 2493 | int n = 0, oldY; |
| 2494 | ++level; |
| 2495 | do { |
| 2496 | oldY = y; |
| 2497 | PaintLevel(children[n], dc, level, y); |
| 2498 | } while (++n < count); |
| 2499 | |
| 2500 | if (!HasFlag(wxTR_NO_LINES) && count > 0) |
| 2501 | { |
| 2502 | // draw line down to last child |
| 2503 | oldY += GetLineHeight(children[n-1])>>1; |
| 2504 | if (HasButtons()) y_mid += 5; |
| 2505 | |
| 2506 | // Only draw the portion of the line that is visible, in case it is huge |
| 2507 | wxCoord xOrigin=0, yOrigin=0, width, height; |
| 2508 | dc.GetDeviceOrigin(&xOrigin, &yOrigin); |
| 2509 | yOrigin = abs(yOrigin); |
| 2510 | GetClientSize(&width, &height); |
| 2511 | |
| 2512 | // Move end points to the begining/end of the view? |
| 2513 | if (y_mid < yOrigin) |
| 2514 | y_mid = yOrigin; |
| 2515 | if (oldY > yOrigin + height) |
| 2516 | oldY = yOrigin + height; |
| 2517 | |
| 2518 | // after the adjustments if y_mid is larger than oldY then the line |
| 2519 | // isn't visible at all so don't draw anything |
| 2520 | if (y_mid < oldY) |
| 2521 | dc.DrawLine(x, y_mid, x, oldY); |
| 2522 | } |
| 2523 | } |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item) |
| 2528 | { |
| 2529 | if ( item ) |
| 2530 | { |
| 2531 | if ( item->HasPlus() ) |
| 2532 | { |
| 2533 | // it's a folder, indicate it by a border |
| 2534 | DrawBorder(item); |
| 2535 | } |
| 2536 | else |
| 2537 | { |
| 2538 | // draw a line under the drop target because the item will be |
| 2539 | // dropped there |
| 2540 | DrawLine(item, !m_dropEffectAboveItem ); |
| 2541 | } |
| 2542 | |
| 2543 | SetCursor(wxCURSOR_BULLSEYE); |
| 2544 | } |
| 2545 | else |
| 2546 | { |
| 2547 | // can't drop here |
| 2548 | SetCursor(wxCURSOR_NO_ENTRY); |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item) |
| 2553 | { |
| 2554 | wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") ); |
| 2555 | |
| 2556 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; |
| 2557 | |
| 2558 | wxClientDC dc(this); |
| 2559 | PrepareDC( dc ); |
| 2560 | dc.SetLogicalFunction(wxINVERT); |
| 2561 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
| 2562 | |
| 2563 | int w = i->GetWidth() + 2; |
| 2564 | int h = GetLineHeight(i) + 2; |
| 2565 | |
| 2566 | dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h); |
| 2567 | } |
| 2568 | |
| 2569 | void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below) |
| 2570 | { |
| 2571 | wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") ); |
| 2572 | |
| 2573 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; |
| 2574 | |
| 2575 | wxClientDC dc(this); |
| 2576 | PrepareDC( dc ); |
| 2577 | dc.SetLogicalFunction(wxINVERT); |
| 2578 | |
| 2579 | int x = i->GetX(), |
| 2580 | y = i->GetY(); |
| 2581 | if ( below ) |
| 2582 | { |
| 2583 | y += GetLineHeight(i) - 1; |
| 2584 | } |
| 2585 | |
| 2586 | dc.DrawLine( x, y, x + i->GetWidth(), y); |
| 2587 | } |
| 2588 | |
| 2589 | // ----------------------------------------------------------------------------- |
| 2590 | // wxWidgets callbacks |
| 2591 | // ----------------------------------------------------------------------------- |
| 2592 | |
| 2593 | void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
| 2594 | { |
| 2595 | wxPaintDC dc(this); |
| 2596 | PrepareDC( dc ); |
| 2597 | |
| 2598 | if ( !m_anchor) |
| 2599 | return; |
| 2600 | |
| 2601 | dc.SetFont( m_normalFont ); |
| 2602 | dc.SetPen( m_dottedPen ); |
| 2603 | |
| 2604 | // this is now done dynamically |
| 2605 | //if(GetImageList() == NULL) |
| 2606 | // m_lineHeight = (int)(dc.GetCharHeight() + 4); |
| 2607 | |
| 2608 | int y = 2; |
| 2609 | PaintLevel( m_anchor, dc, 0, y ); |
| 2610 | } |
| 2611 | |
| 2612 | void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event ) |
| 2613 | { |
| 2614 | m_hasFocus = true; |
| 2615 | |
| 2616 | RefreshSelected(); |
| 2617 | |
| 2618 | event.Skip(); |
| 2619 | } |
| 2620 | |
| 2621 | void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event ) |
| 2622 | { |
| 2623 | m_hasFocus = false; |
| 2624 | |
| 2625 | RefreshSelected(); |
| 2626 | |
| 2627 | event.Skip(); |
| 2628 | } |
| 2629 | |
| 2630 | void wxGenericTreeCtrl::OnChar( wxKeyEvent &event ) |
| 2631 | { |
| 2632 | wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() ); |
| 2633 | te.m_evtKey = event; |
| 2634 | te.SetEventObject( this ); |
| 2635 | if ( GetEventHandler()->ProcessEvent( te ) ) |
| 2636 | { |
| 2637 | // intercepted by the user code |
| 2638 | return; |
| 2639 | } |
| 2640 | |
| 2641 | if ( (m_current == 0) || (m_key_current == 0) ) |
| 2642 | { |
| 2643 | event.Skip(); |
| 2644 | return; |
| 2645 | } |
| 2646 | |
| 2647 | // how should the selection work for this event? |
| 2648 | bool is_multiple, extended_select, unselect_others; |
| 2649 | EventFlagsToSelType(GetWindowStyleFlag(), |
| 2650 | event.ShiftDown(), |
| 2651 | event.CmdDown(), |
| 2652 | is_multiple, extended_select, unselect_others); |
| 2653 | |
| 2654 | // + : Expand |
| 2655 | // - : Collaspe |
| 2656 | // * : Expand all/Collapse all |
| 2657 | // ' ' | return : activate |
| 2658 | // up : go up (not last children!) |
| 2659 | // down : go down |
| 2660 | // left : go to parent |
| 2661 | // right : open if parent and go next |
| 2662 | // home : go to root |
| 2663 | // end : go to last item without opening parents |
| 2664 | // alnum : start or continue searching for the item with this prefix |
| 2665 | int keyCode = event.GetKeyCode(); |
| 2666 | switch ( keyCode ) |
| 2667 | { |
| 2668 | case '+': |
| 2669 | case WXK_ADD: |
| 2670 | if (m_current->HasPlus() && !IsExpanded(m_current)) |
| 2671 | { |
| 2672 | Expand(m_current); |
| 2673 | } |
| 2674 | break; |
| 2675 | |
| 2676 | case '*': |
| 2677 | case WXK_MULTIPLY: |
| 2678 | if ( !IsExpanded(m_current) ) |
| 2679 | { |
| 2680 | // expand all |
| 2681 | ExpandAll(m_current); |
| 2682 | break; |
| 2683 | } |
| 2684 | //else: fall through to Collapse() it |
| 2685 | |
| 2686 | case '-': |
| 2687 | case WXK_SUBTRACT: |
| 2688 | if (IsExpanded(m_current)) |
| 2689 | { |
| 2690 | Collapse(m_current); |
| 2691 | } |
| 2692 | break; |
| 2693 | |
| 2694 | case WXK_MENU: |
| 2695 | { |
| 2696 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_MENU, GetId() ); |
| 2697 | event.m_item = m_current; |
| 2698 | event.SetEventObject( this ); |
| 2699 | GetEventHandler()->ProcessEvent( event ); |
| 2700 | break; |
| 2701 | } |
| 2702 | case ' ': |
| 2703 | case WXK_RETURN: |
| 2704 | if ( !event.HasModifiers() ) |
| 2705 | { |
| 2706 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() ); |
| 2707 | event.m_item = m_current; |
| 2708 | event.SetEventObject( this ); |
| 2709 | GetEventHandler()->ProcessEvent( event ); |
| 2710 | } |
| 2711 | |
| 2712 | // in any case, also generate the normal key event for this key, |
| 2713 | // even if we generated the ACTIVATED event above: this is what |
| 2714 | // wxMSW does and it makes sense because you might not want to |
| 2715 | // process ACTIVATED event at all and handle Space and Return |
| 2716 | // directly (and differently) which would be impossible otherwise |
| 2717 | event.Skip(); |
| 2718 | break; |
| 2719 | |
| 2720 | // up goes to the previous sibling or to the last |
| 2721 | // of its children if it's expanded |
| 2722 | case WXK_UP: |
| 2723 | { |
| 2724 | wxTreeItemId prev = GetPrevSibling( m_key_current ); |
| 2725 | if (!prev) |
| 2726 | { |
| 2727 | prev = GetItemParent( m_key_current ); |
| 2728 | if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT)) |
| 2729 | { |
| 2730 | break; // don't go to root if it is hidden |
| 2731 | } |
| 2732 | if (prev) |
| 2733 | { |
| 2734 | wxTreeItemIdValue cookie; |
| 2735 | wxTreeItemId current = m_key_current; |
| 2736 | // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be? |
| 2737 | if (current == GetFirstChild( prev, cookie )) |
| 2738 | { |
| 2739 | // otherwise we return to where we came from |
| 2740 | DoSelectItem( prev, unselect_others, extended_select ); |
| 2741 | m_key_current= (wxGenericTreeItem*) prev.m_pItem; |
| 2742 | break; |
| 2743 | } |
| 2744 | } |
| 2745 | } |
| 2746 | if (prev) |
| 2747 | { |
| 2748 | while ( IsExpanded(prev) && HasChildren(prev) ) |
| 2749 | { |
| 2750 | wxTreeItemId child = GetLastChild(prev); |
| 2751 | if ( child ) |
| 2752 | { |
| 2753 | prev = child; |
| 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | DoSelectItem( prev, unselect_others, extended_select ); |
| 2758 | m_key_current=(wxGenericTreeItem*) prev.m_pItem; |
| 2759 | } |
| 2760 | } |
| 2761 | break; |
| 2762 | |
| 2763 | // left arrow goes to the parent |
| 2764 | case WXK_LEFT: |
| 2765 | { |
| 2766 | wxTreeItemId prev = GetItemParent( m_current ); |
| 2767 | if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT)) |
| 2768 | { |
| 2769 | // don't go to root if it is hidden |
| 2770 | prev = GetPrevSibling( m_current ); |
| 2771 | } |
| 2772 | if (prev) |
| 2773 | { |
| 2774 | DoSelectItem( prev, unselect_others, extended_select ); |
| 2775 | } |
| 2776 | } |
| 2777 | break; |
| 2778 | |
| 2779 | case WXK_RIGHT: |
| 2780 | // this works the same as the down arrow except that we |
| 2781 | // also expand the item if it wasn't expanded yet |
| 2782 | Expand(m_current); |
| 2783 | // fall through |
| 2784 | |
| 2785 | case WXK_DOWN: |
| 2786 | { |
| 2787 | if (IsExpanded(m_key_current) && HasChildren(m_key_current)) |
| 2788 | { |
| 2789 | wxTreeItemIdValue cookie; |
| 2790 | wxTreeItemId child = GetFirstChild( m_key_current, cookie ); |
| 2791 | DoSelectItem( child, unselect_others, extended_select ); |
| 2792 | m_key_current=(wxGenericTreeItem*) child.m_pItem; |
| 2793 | } |
| 2794 | else |
| 2795 | { |
| 2796 | wxTreeItemId next = GetNextSibling( m_key_current ); |
| 2797 | if (!next) |
| 2798 | { |
| 2799 | wxTreeItemId current = m_key_current; |
| 2800 | while (current.IsOk() && !next) |
| 2801 | { |
| 2802 | current = GetItemParent( current ); |
| 2803 | if (current) next = GetNextSibling( current ); |
| 2804 | } |
| 2805 | } |
| 2806 | if (next) |
| 2807 | { |
| 2808 | DoSelectItem( next, unselect_others, extended_select ); |
| 2809 | m_key_current=(wxGenericTreeItem*) next.m_pItem; |
| 2810 | } |
| 2811 | } |
| 2812 | } |
| 2813 | break; |
| 2814 | |
| 2815 | // <End> selects the last visible tree item |
| 2816 | case WXK_END: |
| 2817 | { |
| 2818 | wxTreeItemId last = GetRootItem(); |
| 2819 | |
| 2820 | while ( last.IsOk() && IsExpanded(last) ) |
| 2821 | { |
| 2822 | wxTreeItemId lastChild = GetLastChild(last); |
| 2823 | |
| 2824 | // it may happen if the item was expanded but then all of |
| 2825 | // its children have been deleted - so IsExpanded() returned |
| 2826 | // true, but GetLastChild() returned invalid item |
| 2827 | if ( !lastChild ) |
| 2828 | break; |
| 2829 | |
| 2830 | last = lastChild; |
| 2831 | } |
| 2832 | |
| 2833 | if ( last.IsOk() ) |
| 2834 | { |
| 2835 | DoSelectItem( last, unselect_others, extended_select ); |
| 2836 | } |
| 2837 | } |
| 2838 | break; |
| 2839 | |
| 2840 | // <Home> selects the root item |
| 2841 | case WXK_HOME: |
| 2842 | { |
| 2843 | wxTreeItemId prev = GetRootItem(); |
| 2844 | if (!prev) |
| 2845 | break; |
| 2846 | |
| 2847 | if ( HasFlag(wxTR_HIDE_ROOT) ) |
| 2848 | { |
| 2849 | wxTreeItemIdValue cookie; |
| 2850 | prev = GetFirstChild(prev, cookie); |
| 2851 | if (!prev) |
| 2852 | break; |
| 2853 | } |
| 2854 | |
| 2855 | DoSelectItem( prev, unselect_others, extended_select ); |
| 2856 | } |
| 2857 | break; |
| 2858 | |
| 2859 | default: |
| 2860 | // do not use wxIsalnum() here |
| 2861 | if ( !event.HasModifiers() && |
| 2862 | ((keyCode >= '0' && keyCode <= '9') || |
| 2863 | (keyCode >= 'a' && keyCode <= 'z') || |
| 2864 | (keyCode >= 'A' && keyCode <= 'Z' ))) |
| 2865 | { |
| 2866 | // find the next item starting with the given prefix |
| 2867 | wxChar ch = (wxChar)keyCode; |
| 2868 | |
| 2869 | wxTreeItemId id = FindItem(m_current, m_findPrefix + ch); |
| 2870 | if ( !id.IsOk() ) |
| 2871 | { |
| 2872 | // no such item |
| 2873 | break; |
| 2874 | } |
| 2875 | |
| 2876 | SelectItem(id); |
| 2877 | |
| 2878 | m_findPrefix += ch; |
| 2879 | |
| 2880 | // also start the timer to reset the current prefix if the user |
| 2881 | // doesn't press any more alnum keys soon -- we wouldn't want |
| 2882 | // to use this prefix for a new item search |
| 2883 | if ( !m_findTimer ) |
| 2884 | { |
| 2885 | m_findTimer = new wxTreeFindTimer(this); |
| 2886 | } |
| 2887 | |
| 2888 | m_findTimer->Start(wxTreeFindTimer::DELAY, wxTIMER_ONE_SHOT); |
| 2889 | } |
| 2890 | else |
| 2891 | { |
| 2892 | event.Skip(); |
| 2893 | } |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags) |
| 2898 | { |
| 2899 | // JACS: removed wxYieldIfNeeded() because it can cause the window |
| 2900 | // to be deleted from under us if a close window event is pending |
| 2901 | |
| 2902 | int w, h; |
| 2903 | GetSize(&w, &h); |
| 2904 | flags=0; |
| 2905 | if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT; |
| 2906 | if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT; |
| 2907 | if (point.y<0) flags |= wxTREE_HITTEST_ABOVE; |
| 2908 | if (point.y>h) flags |= wxTREE_HITTEST_BELOW; |
| 2909 | if (flags) return wxTreeItemId(); |
| 2910 | |
| 2911 | if (m_anchor == NULL) |
| 2912 | { |
| 2913 | flags = wxTREE_HITTEST_NOWHERE; |
| 2914 | return wxTreeItemId(); |
| 2915 | } |
| 2916 | |
| 2917 | wxGenericTreeItem *hit = m_anchor->HitTest(CalcUnscrolledPosition(point), |
| 2918 | this, flags, 0); |
| 2919 | if (hit == NULL) |
| 2920 | { |
| 2921 | flags = wxTREE_HITTEST_NOWHERE; |
| 2922 | return wxTreeItemId(); |
| 2923 | } |
| 2924 | return hit; |
| 2925 | } |
| 2926 | |
| 2927 | // get the bounding rectangle of the item (or of its label only) |
| 2928 | bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item, |
| 2929 | wxRect& rect, |
| 2930 | bool WXUNUSED(textOnly)) const |
| 2931 | { |
| 2932 | wxCHECK_MSG( item.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") ); |
| 2933 | |
| 2934 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; |
| 2935 | |
| 2936 | int startX, startY; |
| 2937 | GetViewStart(& startX, & startY); |
| 2938 | |
| 2939 | rect.x = i->GetX() - startX*PIXELS_PER_UNIT; |
| 2940 | rect.y = i->GetY() - startY*PIXELS_PER_UNIT; |
| 2941 | rect.width = i->GetWidth(); |
| 2942 | //rect.height = i->GetHeight(); |
| 2943 | rect.height = GetLineHeight(i); |
| 2944 | |
| 2945 | return true; |
| 2946 | } |
| 2947 | |
| 2948 | void wxGenericTreeCtrl::Edit( const wxTreeItemId& item ) |
| 2949 | { |
| 2950 | wxCHECK_RET( item.IsOk(), _T("can't edit an invalid item") ); |
| 2951 | |
| 2952 | wxGenericTreeItem *itemEdit = (wxGenericTreeItem *)item.m_pItem; |
| 2953 | |
| 2954 | wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() ); |
| 2955 | te.m_item = itemEdit; |
| 2956 | te.SetEventObject( this ); |
| 2957 | if ( GetEventHandler()->ProcessEvent( te ) && !te.IsAllowed() ) |
| 2958 | { |
| 2959 | // vetoed by user |
| 2960 | return; |
| 2961 | } |
| 2962 | |
| 2963 | // We have to call this here because the label in |
| 2964 | // question might just have been added and no screen |
| 2965 | // update taken place. |
| 2966 | if ( m_dirty ) |
| 2967 | #if defined( __WXMSW__ ) || defined(__WXMAC__) |
| 2968 | Update(); |
| 2969 | #else |
| 2970 | wxYieldIfNeeded(); |
| 2971 | #endif |
| 2972 | |
| 2973 | m_textCtrl = new wxTreeTextCtrl(this, itemEdit); |
| 2974 | |
| 2975 | m_textCtrl->SetFocus(); |
| 2976 | } |
| 2977 | |
| 2978 | // returns a pointer to the text edit control if the item is being |
| 2979 | // edited, NULL otherwise (it's assumed that no more than one item may |
| 2980 | // be edited simultaneously) |
| 2981 | wxTextCtrl* wxGenericTreeCtrl::GetEditControl() const |
| 2982 | { |
| 2983 | return m_textCtrl; |
| 2984 | } |
| 2985 | |
| 2986 | bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem *item, |
| 2987 | const wxString& value) |
| 2988 | { |
| 2989 | wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() ); |
| 2990 | le.m_item = item; |
| 2991 | le.SetEventObject( this ); |
| 2992 | le.m_label = value; |
| 2993 | le.m_editCancelled = false; |
| 2994 | |
| 2995 | return !GetEventHandler()->ProcessEvent( le ) || le.IsAllowed(); |
| 2996 | } |
| 2997 | |
| 2998 | void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem *item) |
| 2999 | { |
| 3000 | // let owner know that the edit was cancelled |
| 3001 | wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() ); |
| 3002 | le.m_item = item; |
| 3003 | le.SetEventObject( this ); |
| 3004 | le.m_label = wxEmptyString; |
| 3005 | le.m_editCancelled = true; |
| 3006 | |
| 3007 | GetEventHandler()->ProcessEvent( le ); |
| 3008 | } |
| 3009 | |
| 3010 | void wxGenericTreeCtrl::OnRenameTimer() |
| 3011 | { |
| 3012 | Edit( m_current ); |
| 3013 | } |
| 3014 | |
| 3015 | void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event ) |
| 3016 | { |
| 3017 | if ( !m_anchor ) return; |
| 3018 | |
| 3019 | wxPoint pt = CalcUnscrolledPosition(event.GetPosition()); |
| 3020 | |
| 3021 | // Is the mouse over a tree item button? |
| 3022 | int flags = 0; |
| 3023 | wxGenericTreeItem *thisItem = m_anchor->HitTest(pt, this, flags, 0); |
| 3024 | wxGenericTreeItem *underMouse = thisItem; |
| 3025 | #if wxUSE_TOOLTIPS |
| 3026 | bool underMouseChanged = (underMouse != m_underMouse) ; |
| 3027 | #endif // wxUSE_TOOLTIPS |
| 3028 | |
| 3029 | if ((underMouse) && |
| 3030 | (flags & wxTREE_HITTEST_ONITEMBUTTON) && |
| 3031 | (!event.LeftIsDown()) && |
| 3032 | (!m_isDragging) && |
| 3033 | (!m_renameTimer || !m_renameTimer->IsRunning())) |
| 3034 | { |
| 3035 | } |
| 3036 | else |
| 3037 | { |
| 3038 | underMouse = NULL; |
| 3039 | } |
| 3040 | |
| 3041 | if (underMouse != m_underMouse) |
| 3042 | { |
| 3043 | if (m_underMouse) |
| 3044 | { |
| 3045 | // unhighlight old item |
| 3046 | wxGenericTreeItem *tmp = m_underMouse; |
| 3047 | m_underMouse = NULL; |
| 3048 | RefreshLine( tmp ); |
| 3049 | } |
| 3050 | |
| 3051 | m_underMouse = underMouse; |
| 3052 | if (m_underMouse) |
| 3053 | RefreshLine( m_underMouse ); |
| 3054 | } |
| 3055 | |
| 3056 | #if wxUSE_TOOLTIPS |
| 3057 | // Determines what item we are hovering over and need a tooltip for |
| 3058 | wxTreeItemId hoverItem = thisItem; |
| 3059 | |
| 3060 | // We do not want a tooltip if we are dragging, or if the rename timer is running |
| 3061 | if (underMouseChanged && hoverItem.IsOk() && !m_isDragging && (!m_renameTimer || !m_renameTimer->IsRunning())) |
| 3062 | { |
| 3063 | // Ask the tree control what tooltip (if any) should be shown |
| 3064 | wxTreeEvent hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP, GetId()); |
| 3065 | hevent.m_item = hoverItem; |
| 3066 | hevent.SetEventObject(this); |
| 3067 | |
| 3068 | if ( GetEventHandler()->ProcessEvent(hevent) && hevent.IsAllowed() ) |
| 3069 | { |
| 3070 | SetToolTip(hevent.m_label); |
| 3071 | } |
| 3072 | } |
| 3073 | #endif |
| 3074 | |
| 3075 | // we process left mouse up event (enables in-place edit), right down |
| 3076 | // (pass to the user code), left dbl click (activate item) and |
| 3077 | // dragging/moving events for items drag-and-drop |
| 3078 | if ( !(event.LeftDown() || |
| 3079 | event.LeftUp() || |
| 3080 | event.RightDown() || |
| 3081 | event.LeftDClick() || |
| 3082 | event.Dragging() || |
| 3083 | ((event.Moving() || event.RightUp()) && m_isDragging)) ) |
| 3084 | { |
| 3085 | event.Skip(); |
| 3086 | |
| 3087 | return; |
| 3088 | } |
| 3089 | |
| 3090 | |
| 3091 | flags = 0; |
| 3092 | wxGenericTreeItem *item = m_anchor->HitTest(pt, this, flags, 0); |
| 3093 | |
| 3094 | if ( event.Dragging() && !m_isDragging ) |
| 3095 | { |
| 3096 | if (m_dragCount == 0) |
| 3097 | m_dragStart = pt; |
| 3098 | |
| 3099 | m_dragCount++; |
| 3100 | |
| 3101 | if (m_dragCount != 3) |
| 3102 | { |
| 3103 | // wait until user drags a bit further... |
| 3104 | return; |
| 3105 | } |
| 3106 | |
| 3107 | wxEventType command = event.RightIsDown() |
| 3108 | ? wxEVT_COMMAND_TREE_BEGIN_RDRAG |
| 3109 | : wxEVT_COMMAND_TREE_BEGIN_DRAG; |
| 3110 | |
| 3111 | wxTreeEvent nevent( command, GetId() ); |
| 3112 | nevent.m_item = m_current; |
| 3113 | nevent.SetEventObject(this); |
| 3114 | nevent.SetPoint(pt); |
| 3115 | |
| 3116 | // by default the dragging is not supported, the user code must |
| 3117 | // explicitly allow the event for it to take place |
| 3118 | nevent.Veto(); |
| 3119 | |
| 3120 | if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() ) |
| 3121 | { |
| 3122 | // we're going to drag this item |
| 3123 | m_isDragging = true; |
| 3124 | |
| 3125 | // remember the old cursor because we will change it while |
| 3126 | // dragging |
| 3127 | m_oldCursor = m_cursor; |
| 3128 | |
| 3129 | // in a single selection control, hide the selection temporarily |
| 3130 | if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) ) |
| 3131 | { |
| 3132 | m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem; |
| 3133 | |
| 3134 | if ( m_oldSelection ) |
| 3135 | { |
| 3136 | m_oldSelection->SetHilight(false); |
| 3137 | RefreshLine(m_oldSelection); |
| 3138 | } |
| 3139 | } |
| 3140 | |
| 3141 | CaptureMouse(); |
| 3142 | } |
| 3143 | } |
| 3144 | else if ( event.Dragging() ) |
| 3145 | { |
| 3146 | if ( item != m_dropTarget ) |
| 3147 | { |
| 3148 | // unhighlight the previous drop target |
| 3149 | DrawDropEffect(m_dropTarget); |
| 3150 | |
| 3151 | m_dropTarget = item; |
| 3152 | |
| 3153 | // highlight the current drop target if any |
| 3154 | DrawDropEffect(m_dropTarget); |
| 3155 | |
| 3156 | #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__) |
| 3157 | Update(); |
| 3158 | #else |
| 3159 | wxYieldIfNeeded(); |
| 3160 | #endif |
| 3161 | } |
| 3162 | } |
| 3163 | else if ( (event.LeftUp() || event.RightUp()) && m_isDragging ) |
| 3164 | { |
| 3165 | // erase the highlighting |
| 3166 | DrawDropEffect(m_dropTarget); |
| 3167 | |
| 3168 | if ( m_oldSelection ) |
| 3169 | { |
| 3170 | m_oldSelection->SetHilight(true); |
| 3171 | RefreshLine(m_oldSelection); |
| 3172 | m_oldSelection = (wxGenericTreeItem *)NULL; |
| 3173 | } |
| 3174 | |
| 3175 | // generate the drag end event |
| 3176 | wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId()); |
| 3177 | |
| 3178 | event.m_item = item; |
| 3179 | event.m_pointDrag = pt; |
| 3180 | event.SetEventObject(this); |
| 3181 | |
| 3182 | (void)GetEventHandler()->ProcessEvent(event); |
| 3183 | |
| 3184 | m_isDragging = false; |
| 3185 | m_dropTarget = (wxGenericTreeItem *)NULL; |
| 3186 | |
| 3187 | ReleaseMouse(); |
| 3188 | |
| 3189 | SetCursor(m_oldCursor); |
| 3190 | |
| 3191 | #if defined( __WXMSW__ ) || defined(__WXMAC__) |
| 3192 | Update(); |
| 3193 | #else |
| 3194 | wxYieldIfNeeded(); |
| 3195 | #endif |
| 3196 | } |
| 3197 | else |
| 3198 | { |
| 3199 | // If we got to this point, we are not dragging or moving the mouse. |
| 3200 | // Because the code in carbon/toplevel.cpp will only set focus to the tree |
| 3201 | // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work. |
| 3202 | // We skip even if we didn't hit an item because we still should |
| 3203 | // restore focus to the tree control even if we didn't exactly hit an item. |
| 3204 | if ( event.LeftDown() ) |
| 3205 | { |
| 3206 | event.Skip(); |
| 3207 | } |
| 3208 | |
| 3209 | // here we process only the messages which happen on tree items |
| 3210 | |
| 3211 | m_dragCount = 0; |
| 3212 | |
| 3213 | if (item == NULL) return; /* we hit the blank area */ |
| 3214 | |
| 3215 | if ( event.RightDown() ) |
| 3216 | { |
| 3217 | // If the item is already selected, do not update the selection. |
| 3218 | // Multi-selections should not be cleared if a selected item is clicked. |
| 3219 | if (!IsSelected(item)) |
| 3220 | { |
| 3221 | DoSelectItem(item, true, false); |
| 3222 | } |
| 3223 | |
| 3224 | wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId()); |
| 3225 | nevent.m_item = item; |
| 3226 | nevent.m_pointDrag = CalcScrolledPosition(pt); |
| 3227 | nevent.SetEventObject(this); |
| 3228 | event.Skip(!GetEventHandler()->ProcessEvent(nevent)); |
| 3229 | } |
| 3230 | else if ( event.LeftUp() ) |
| 3231 | { |
| 3232 | // this facilitates multiple-item drag-and-drop |
| 3233 | |
| 3234 | if (item && HasFlag(wxTR_MULTIPLE)) |
| 3235 | { |
| 3236 | wxArrayTreeItemIds selections; |
| 3237 | size_t count = GetSelections(selections); |
| 3238 | |
| 3239 | if (count > 1 && |
| 3240 | !event.CmdDown() && |
| 3241 | !event.ShiftDown()) |
| 3242 | { |
| 3243 | DoSelectItem(item, true, false); |
| 3244 | } |
| 3245 | } |
| 3246 | |
| 3247 | if ( m_lastOnSame ) |
| 3248 | { |
| 3249 | if ( (item == m_current) && |
| 3250 | (flags & wxTREE_HITTEST_ONITEMLABEL) && |
| 3251 | HasFlag(wxTR_EDIT_LABELS) ) |
| 3252 | { |
| 3253 | if ( m_renameTimer ) |
| 3254 | { |
| 3255 | if ( m_renameTimer->IsRunning() ) |
| 3256 | m_renameTimer->Stop(); |
| 3257 | } |
| 3258 | else |
| 3259 | { |
| 3260 | m_renameTimer = new wxTreeRenameTimer( this ); |
| 3261 | } |
| 3262 | |
| 3263 | m_renameTimer->Start( wxTreeRenameTimer::DELAY, true ); |
| 3264 | } |
| 3265 | |
| 3266 | m_lastOnSame = false; |
| 3267 | } |
| 3268 | } |
| 3269 | else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick() |
| 3270 | { |
| 3271 | if ( event.LeftDown() ) |
| 3272 | { |
| 3273 | m_lastOnSame = item == m_current; |
| 3274 | } |
| 3275 | |
| 3276 | if ( flags & wxTREE_HITTEST_ONITEMBUTTON ) |
| 3277 | { |
| 3278 | // only toggle the item for a single click, double click on |
| 3279 | // the button doesn't do anything (it toggles the item twice) |
| 3280 | if ( event.LeftDown() ) |
| 3281 | { |
| 3282 | Toggle( item ); |
| 3283 | } |
| 3284 | |
| 3285 | // don't select the item if the button was clicked |
| 3286 | return; |
| 3287 | } |
| 3288 | |
| 3289 | |
| 3290 | // clear the previously selected items, if the |
| 3291 | // user clicked outside of the present selection. |
| 3292 | // otherwise, perform the deselection on mouse-up. |
| 3293 | // this allows multiple drag and drop to work. |
| 3294 | // but if Cmd is down, toggle selection of the clicked item |
| 3295 | if (!IsSelected(item) || event.CmdDown()) |
| 3296 | { |
| 3297 | // how should the selection work for this event? |
| 3298 | bool is_multiple, extended_select, unselect_others; |
| 3299 | EventFlagsToSelType(GetWindowStyleFlag(), |
| 3300 | event.ShiftDown(), |
| 3301 | event.CmdDown(), |
| 3302 | is_multiple, extended_select, unselect_others); |
| 3303 | |
| 3304 | DoSelectItem(item, unselect_others, extended_select); |
| 3305 | } |
| 3306 | |
| 3307 | |
| 3308 | // For some reason, Windows isn't recognizing a left double-click, |
| 3309 | // so we need to simulate it here. Allow 200 milliseconds for now. |
| 3310 | if ( event.LeftDClick() ) |
| 3311 | { |
| 3312 | // double clicking should not start editing the item label |
| 3313 | if ( m_renameTimer ) |
| 3314 | m_renameTimer->Stop(); |
| 3315 | |
| 3316 | m_lastOnSame = false; |
| 3317 | |
| 3318 | // send activate event first |
| 3319 | wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() ); |
| 3320 | nevent.m_item = item; |
| 3321 | nevent.m_pointDrag = CalcScrolledPosition(pt); |
| 3322 | nevent.SetEventObject( this ); |
| 3323 | if ( !GetEventHandler()->ProcessEvent( nevent ) ) |
| 3324 | { |
| 3325 | // if the user code didn't process the activate event, |
| 3326 | // handle it ourselves by toggling the item when it is |
| 3327 | // double clicked |
| 3328 | if ( item->HasPlus() ) |
| 3329 | { |
| 3330 | Toggle(item); |
| 3331 | } |
| 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | } |
| 3336 | } |
| 3337 | |
| 3338 | void wxGenericTreeCtrl::OnInternalIdle() |
| 3339 | { |
| 3340 | wxWindow::OnInternalIdle(); |
| 3341 | |
| 3342 | // Check if we need to select the root item |
| 3343 | // because nothing else has been selected. |
| 3344 | // Delaying it means that we can invoke event handlers |
| 3345 | // as required, when a first item is selected. |
| 3346 | if (!HasFlag(wxTR_MULTIPLE) && !GetSelection().IsOk()) |
| 3347 | { |
| 3348 | if (m_select_me) |
| 3349 | SelectItem(m_select_me); |
| 3350 | else if (GetRootItem().IsOk()) |
| 3351 | SelectItem(GetRootItem()); |
| 3352 | } |
| 3353 | |
| 3354 | /* after all changes have been done to the tree control, |
| 3355 | * we actually redraw the tree when everything is over */ |
| 3356 | |
| 3357 | if (!m_dirty) return; |
| 3358 | if (m_freezeCount) return; |
| 3359 | |
| 3360 | m_dirty = false; |
| 3361 | |
| 3362 | CalculatePositions(); |
| 3363 | Refresh(); |
| 3364 | AdjustMyScrollbars(); |
| 3365 | } |
| 3366 | |
| 3367 | void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc ) |
| 3368 | { |
| 3369 | wxCoord text_w = 0; |
| 3370 | wxCoord text_h = 0; |
| 3371 | |
| 3372 | wxTreeItemAttr *attr = item->GetAttributes(); |
| 3373 | if ( attr && attr->HasFont() ) |
| 3374 | dc.SetFont(attr->GetFont()); |
| 3375 | else if ( item->IsBold() ) |
| 3376 | dc.SetFont(m_boldFont); |
| 3377 | else |
| 3378 | dc.SetFont(m_normalFont); |
| 3379 | |
| 3380 | dc.GetTextExtent( item->GetText(), &text_w, &text_h ); |
| 3381 | text_h+=2; |
| 3382 | |
| 3383 | // restore normal font |
| 3384 | dc.SetFont( m_normalFont ); |
| 3385 | |
| 3386 | int image_h = 0; |
| 3387 | int image_w = 0; |
| 3388 | int image = item->GetCurrentImage(); |
| 3389 | if ( image != NO_IMAGE ) |
| 3390 | { |
| 3391 | if ( m_imageListNormal ) |
| 3392 | { |
| 3393 | m_imageListNormal->GetSize( image, image_w, image_h ); |
| 3394 | image_w += 4; |
| 3395 | } |
| 3396 | } |
| 3397 | |
| 3398 | int total_h = (image_h > text_h) ? image_h : text_h; |
| 3399 | |
| 3400 | if (total_h < 30) |
| 3401 | total_h += 2; // at least 2 pixels |
| 3402 | else |
| 3403 | total_h += total_h/10; // otherwise 10% extra spacing |
| 3404 | |
| 3405 | item->SetHeight(total_h); |
| 3406 | if (total_h>m_lineHeight) |
| 3407 | m_lineHeight=total_h; |
| 3408 | |
| 3409 | item->SetWidth(image_w+text_w+2); |
| 3410 | } |
| 3411 | |
| 3412 | // ----------------------------------------------------------------------------- |
| 3413 | // for developper : y is now the top of the level |
| 3414 | // not the middle of it ! |
| 3415 | void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ) |
| 3416 | { |
| 3417 | int x = level*m_indent; |
| 3418 | if (!HasFlag(wxTR_HIDE_ROOT)) |
| 3419 | { |
| 3420 | x += m_indent; |
| 3421 | } |
| 3422 | else if (level == 0) |
| 3423 | { |
| 3424 | // a hidden root is not evaluated, but its |
| 3425 | // children are always calculated |
| 3426 | goto Recurse; |
| 3427 | } |
| 3428 | |
| 3429 | CalculateSize( item, dc ); |
| 3430 | |
| 3431 | // set its position |
| 3432 | item->SetX( x+m_spacing ); |
| 3433 | item->SetY( y ); |
| 3434 | y += GetLineHeight(item); |
| 3435 | |
| 3436 | if ( !item->IsExpanded() ) |
| 3437 | { |
| 3438 | // we don't need to calculate collapsed branches |
| 3439 | return; |
| 3440 | } |
| 3441 | |
| 3442 | Recurse: |
| 3443 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 3444 | size_t n, count = children.Count(); |
| 3445 | ++level; |
| 3446 | for (n = 0; n < count; ++n ) |
| 3447 | CalculateLevel( children[n], dc, level, y ); // recurse |
| 3448 | } |
| 3449 | |
| 3450 | void wxGenericTreeCtrl::CalculatePositions() |
| 3451 | { |
| 3452 | if ( !m_anchor ) return; |
| 3453 | |
| 3454 | wxClientDC dc(this); |
| 3455 | PrepareDC( dc ); |
| 3456 | |
| 3457 | dc.SetFont( m_normalFont ); |
| 3458 | |
| 3459 | dc.SetPen( m_dottedPen ); |
| 3460 | //if(GetImageList() == NULL) |
| 3461 | // m_lineHeight = (int)(dc.GetCharHeight() + 4); |
| 3462 | |
| 3463 | int y = 2; |
| 3464 | CalculateLevel( m_anchor, dc, 0, y ); // start recursion |
| 3465 | } |
| 3466 | |
| 3467 | void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item) |
| 3468 | { |
| 3469 | if (m_dirty) return; |
| 3470 | if (m_freezeCount) return; |
| 3471 | |
| 3472 | wxSize client = GetClientSize(); |
| 3473 | |
| 3474 | wxRect rect; |
| 3475 | CalcScrolledPosition(0, item->GetY(), NULL, &rect.y); |
| 3476 | rect.width = client.x; |
| 3477 | rect.height = client.y; |
| 3478 | |
| 3479 | Refresh(true, &rect); |
| 3480 | |
| 3481 | AdjustMyScrollbars(); |
| 3482 | } |
| 3483 | |
| 3484 | void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item ) |
| 3485 | { |
| 3486 | if (m_dirty) return; |
| 3487 | if (m_freezeCount) return; |
| 3488 | |
| 3489 | wxRect rect; |
| 3490 | CalcScrolledPosition(0, item->GetY(), NULL, &rect.y); |
| 3491 | rect.width = GetClientSize().x; |
| 3492 | rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6; |
| 3493 | |
| 3494 | Refresh(true, &rect); |
| 3495 | } |
| 3496 | |
| 3497 | void wxGenericTreeCtrl::RefreshSelected() |
| 3498 | { |
| 3499 | if (m_freezeCount) return; |
| 3500 | |
| 3501 | // TODO: this is awfully inefficient, we should keep the list of all |
| 3502 | // selected items internally, should be much faster |
| 3503 | if ( m_anchor ) |
| 3504 | RefreshSelectedUnder(m_anchor); |
| 3505 | } |
| 3506 | |
| 3507 | void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item) |
| 3508 | { |
| 3509 | if (m_freezeCount) return; |
| 3510 | |
| 3511 | if ( item->IsSelected() ) |
| 3512 | RefreshLine(item); |
| 3513 | |
| 3514 | const wxArrayGenericTreeItems& children = item->GetChildren(); |
| 3515 | size_t count = children.GetCount(); |
| 3516 | for ( size_t n = 0; n < count; n++ ) |
| 3517 | { |
| 3518 | RefreshSelectedUnder(children[n]); |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | void wxGenericTreeCtrl::Freeze() |
| 3523 | { |
| 3524 | m_freezeCount++; |
| 3525 | } |
| 3526 | |
| 3527 | void wxGenericTreeCtrl::Thaw() |
| 3528 | { |
| 3529 | wxCHECK_RET( m_freezeCount > 0, _T("thawing unfrozen tree control?") ); |
| 3530 | |
| 3531 | if ( !--m_freezeCount ) |
| 3532 | { |
| 3533 | Refresh(); |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | // ---------------------------------------------------------------------------- |
| 3538 | // changing colours: we need to refresh the tree control |
| 3539 | // ---------------------------------------------------------------------------- |
| 3540 | |
| 3541 | bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour) |
| 3542 | { |
| 3543 | if ( !wxWindow::SetBackgroundColour(colour) ) |
| 3544 | return false; |
| 3545 | |
| 3546 | if (m_freezeCount) return true; |
| 3547 | |
| 3548 | Refresh(); |
| 3549 | |
| 3550 | return true; |
| 3551 | } |
| 3552 | |
| 3553 | bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour) |
| 3554 | { |
| 3555 | if ( !wxWindow::SetForegroundColour(colour) ) |
| 3556 | return false; |
| 3557 | |
| 3558 | if (m_freezeCount) return true; |
| 3559 | |
| 3560 | Refresh(); |
| 3561 | |
| 3562 | return true; |
| 3563 | } |
| 3564 | |
| 3565 | // Process the tooltip event, to speed up event processing. |
| 3566 | // Doesn't actually get a tooltip. |
| 3567 | void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent &event ) |
| 3568 | { |
| 3569 | event.Veto(); |
| 3570 | } |
| 3571 | |
| 3572 | |
| 3573 | wxSize wxGenericTreeCtrl::DoGetBestSize() const |
| 3574 | { |
| 3575 | // something is better than nothing... |
| 3576 | // 100x80 is what the MSW version will get from the default |
| 3577 | // wxControl::DoGetBestSize |
| 3578 | return wxSize(100,80); |
| 3579 | } |
| 3580 | |
| 3581 | |
| 3582 | // NOTE: If using the wxListBox visual attributes works everywhere then this can |
| 3583 | // be removed, as well as the #else case below. |
| 3584 | #define _USE_VISATTR 0 |
| 3585 | |
| 3586 | #if _USE_VISATTR |
| 3587 | #include "wx/listbox.h" |
| 3588 | #endif |
| 3589 | |
| 3590 | //static |
| 3591 | wxVisualAttributes |
| 3592 | #if _USE_VISATTR |
| 3593 | wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant) |
| 3594 | #else |
| 3595 | wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
| 3596 | #endif |
| 3597 | { |
| 3598 | #if _USE_VISATTR |
| 3599 | // Use the same color scheme as wxListBox |
| 3600 | return wxListBox::GetClassDefaultAttributes(variant); |
| 3601 | #else |
| 3602 | wxVisualAttributes attr; |
| 3603 | attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); |
| 3604 | attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX); |
| 3605 | attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); |
| 3606 | return attr; |
| 3607 | #endif |
| 3608 | } |
| 3609 | |
| 3610 | #if WXWIN_COMPATIBILITY_2_4 |
| 3611 | |
| 3612 | int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const |
| 3613 | { |
| 3614 | return GetItemImage(item, wxTreeItemIcon_Selected); |
| 3615 | } |
| 3616 | |
| 3617 | void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image) |
| 3618 | { |
| 3619 | SetItemImage(item, image, wxTreeItemIcon_Selected); |
| 3620 | } |
| 3621 | |
| 3622 | #endif // WXWIN_COMPATIBILITY_2_4 |
| 3623 | |
| 3624 | #if WXWIN_COMPATIBILITY_2_2 |
| 3625 | |
| 3626 | wxTreeItemId wxGenericTreeCtrl::GetParent(const wxTreeItemId& item) const |
| 3627 | { |
| 3628 | return GetItemParent( item ); |
| 3629 | } |
| 3630 | |
| 3631 | #endif // WXWIN_COMPATIBILITY_2_2 |
| 3632 | |
| 3633 | #endif // wxUSE_TREECTRL |