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