| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: treectrl.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, Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================= |
| 13 | // declarations |
| 14 | // ============================================================================= |
| 15 | |
| 16 | // ----------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ----------------------------------------------------------------------------- |
| 19 | |
| 20 | #ifdef __GNUG__ |
| 21 | #pragma implementation "treectrl.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifdef __BORLANDC__ |
| 28 | #pragma hdrstop |
| 29 | #endif |
| 30 | |
| 31 | #include "wx/treectrl.h" |
| 32 | #include "wx/generic/imaglist.h" |
| 33 | #include "wx/settings.h" |
| 34 | #include "wx/log.h" |
| 35 | #include "wx/intl.h" |
| 36 | #include "wx/dynarray.h" |
| 37 | #include "wx/arrimpl.cpp" |
| 38 | #include "wx/dcclient.h" |
| 39 | #include "wx/msgdlg.h" |
| 40 | |
| 41 | // ----------------------------------------------------------------------------- |
| 42 | // array types |
| 43 | // ----------------------------------------------------------------------------- |
| 44 | |
| 45 | class WXDLLEXPORT wxGenericTreeItem; |
| 46 | |
| 47 | WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems); |
| 48 | WX_DEFINE_OBJARRAY(wxArrayTreeItemIds); |
| 49 | |
| 50 | // ---------------------------------------------------------------------------- |
| 51 | // constants |
| 52 | // ---------------------------------------------------------------------------- |
| 53 | |
| 54 | static const int NO_IMAGE = -1; |
| 55 | |
| 56 | #define PIXELS_PER_UNIT 10 |
| 57 | |
| 58 | // ----------------------------------------------------------------------------- |
| 59 | // private classes |
| 60 | // ----------------------------------------------------------------------------- |
| 61 | |
| 62 | // timer used for enabling in-place edit |
| 63 | class WXDLLEXPORT wxTreeRenameTimer: public wxTimer |
| 64 | { |
| 65 | public: |
| 66 | wxTreeRenameTimer( wxTreeCtrl *owner ); |
| 67 | |
| 68 | void Notify(); |
| 69 | |
| 70 | private: |
| 71 | wxTreeCtrl *m_owner; |
| 72 | }; |
| 73 | |
| 74 | // control used for in-place edit |
| 75 | class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl |
| 76 | { |
| 77 | public: |
| 78 | wxTreeTextCtrl() { } |
| 79 | wxTreeTextCtrl( wxWindow *parent, |
| 80 | const wxWindowID id, |
| 81 | bool *accept, |
| 82 | wxString *res, |
| 83 | wxTreeCtrl *owner, |
| 84 | const wxString &value = wxEmptyString, |
| 85 | const wxPoint &pos = wxDefaultPosition, |
| 86 | const wxSize &size = wxDefaultSize, |
| 87 | int style = 0, |
| 88 | const wxValidator& validator = wxDefaultValidator, |
| 89 | const wxString &name = wxTextCtrlNameStr ); |
| 90 | |
| 91 | void OnChar( wxKeyEvent &event ); |
| 92 | void OnKillFocus( wxFocusEvent &event ); |
| 93 | |
| 94 | private: |
| 95 | bool *m_accept; |
| 96 | wxString *m_res; |
| 97 | wxTreeCtrl *m_owner; |
| 98 | wxString m_startValue; |
| 99 | |
| 100 | DECLARE_EVENT_TABLE() |
| 101 | DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl); |
| 102 | }; |
| 103 | |
| 104 | // a tree item |
| 105 | class WXDLLEXPORT wxGenericTreeItem |
| 106 | { |
| 107 | public: |
| 108 | // ctors & dtor |
| 109 | wxGenericTreeItem() { m_data = NULL; } |
| 110 | wxGenericTreeItem( wxGenericTreeItem *parent, |
| 111 | const wxString& text, |
| 112 | wxDC& dc, |
| 113 | int image, int selImage, |
| 114 | wxTreeItemData *data ); |
| 115 | |
| 116 | ~wxGenericTreeItem(); |
| 117 | |
| 118 | // trivial accessors |
| 119 | wxArrayGenericTreeItems& GetChildren() { return m_children; } |
| 120 | |
| 121 | const wxString& GetText() const { return m_text; } |
| 122 | int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const |
| 123 | { return m_images[which]; } |
| 124 | wxTreeItemData *GetData() const { return m_data; } |
| 125 | |
| 126 | // returns the current image for the item (depending on its |
| 127 | // selected/expanded/whatever state) |
| 128 | int GetCurrentImage() const; |
| 129 | |
| 130 | void SetText( const wxString &text ); |
| 131 | void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; } |
| 132 | void SetData(wxTreeItemData *data) { m_data = data; } |
| 133 | |
| 134 | void SetHasPlus(bool has = TRUE) { m_hasPlus = has; } |
| 135 | |
| 136 | void SetBold(bool bold) { m_isBold = bold; } |
| 137 | |
| 138 | int GetX() const { return m_x; } |
| 139 | int GetY() const { return m_y; } |
| 140 | |
| 141 | void SetX(int x) { m_x = x; } |
| 142 | void SetY(int y) { m_y = y; } |
| 143 | |
| 144 | int GetHeight() const { return m_height; } |
| 145 | int GetWidth() const { return m_width; } |
| 146 | |
| 147 | void SetHeight(int h) { m_height = h; } |
| 148 | void SetWidth(int w) { m_width = w; } |
| 149 | |
| 150 | |
| 151 | wxGenericTreeItem *GetParent() const { return m_parent; } |
| 152 | |
| 153 | // operations |
| 154 | // deletes all children notifying the treectrl about it if !NULL |
| 155 | // pointer given |
| 156 | void DeleteChildren(wxTreeCtrl *tree = NULL); |
| 157 | // FIXME don't know what is it for |
| 158 | void Reset(); |
| 159 | |
| 160 | // get count of all children (and grand children if 'recursively') |
| 161 | size_t GetChildrenCount(bool recursively = TRUE) const; |
| 162 | |
| 163 | void Insert(wxGenericTreeItem *child, size_t index) |
| 164 | { m_children.Insert(child, index); } |
| 165 | |
| 166 | void SetCross( int x, int y ); |
| 167 | void GetSize( int &x, int &y, const wxTreeCtrl* ); |
| 168 | |
| 169 | // return the item at given position (or NULL if no item), onButton is |
| 170 | // TRUE if the point belongs to the item's button, otherwise it lies |
| 171 | // on the button's label |
| 172 | wxGenericTreeItem *HitTest( const wxPoint& point, const wxTreeCtrl *, int &flags); |
| 173 | |
| 174 | void Expand() { m_isCollapsed = FALSE; } |
| 175 | void Collapse() { m_isCollapsed = TRUE; } |
| 176 | |
| 177 | void SetHilight( bool set = TRUE ) { m_hasHilight = set; } |
| 178 | |
| 179 | // status inquiries |
| 180 | bool HasChildren() const { return !m_children.IsEmpty(); } |
| 181 | bool IsSelected() const { return m_hasHilight; } |
| 182 | bool IsExpanded() const { return !m_isCollapsed; } |
| 183 | bool HasPlus() const { return m_hasPlus || HasChildren(); } |
| 184 | bool IsBold() const { return m_isBold; } |
| 185 | |
| 186 | // attributes |
| 187 | // get them - may be NULL |
| 188 | wxTreeItemAttr *GetAttributes() const { return m_attr; } |
| 189 | // get them ensuring that the pointer is not NULL |
| 190 | wxTreeItemAttr& Attr() |
| 191 | { |
| 192 | if ( !m_attr ) |
| 193 | m_attr = new wxTreeItemAttr; |
| 194 | |
| 195 | return *m_attr; |
| 196 | } |
| 197 | |
| 198 | private: |
| 199 | wxString m_text; |
| 200 | |
| 201 | // tree ctrl images for the normal, selected, expanded and |
| 202 | // expanded+selected states |
| 203 | int m_images[wxTreeItemIcon_Max]; |
| 204 | |
| 205 | wxTreeItemData *m_data; |
| 206 | |
| 207 | // use bitfields to save size |
| 208 | int m_isCollapsed :1; |
| 209 | int m_hasHilight :1; // same as focused |
| 210 | int m_hasPlus :1; // used for item which doesn't have |
| 211 | // children but has a [+] button |
| 212 | int m_isBold :1; // render the label in bold font |
| 213 | |
| 214 | wxCoord m_x, m_y; |
| 215 | wxCoord m_height, m_width; |
| 216 | int m_xCross, m_yCross; |
| 217 | int m_level; |
| 218 | |
| 219 | wxArrayGenericTreeItems m_children; |
| 220 | wxGenericTreeItem *m_parent; |
| 221 | |
| 222 | wxTreeItemAttr *m_attr; |
| 223 | }; |
| 224 | |
| 225 | // ============================================================================= |
| 226 | // implementation |
| 227 | // ============================================================================= |
| 228 | |
| 229 | // ---------------------------------------------------------------------------- |
| 230 | // private functions |
| 231 | // ---------------------------------------------------------------------------- |
| 232 | |
| 233 | // translate the key or mouse event flags to the type of selection we're |
| 234 | // dealing with |
| 235 | static void EventFlagsToSelType(long style, |
| 236 | bool shiftDown, |
| 237 | bool ctrlDown, |
| 238 | bool &is_multiple, |
| 239 | bool &extended_select, |
| 240 | bool &unselect_others) |
| 241 | { |
| 242 | is_multiple = (style & wxTR_MULTIPLE) != 0; |
| 243 | extended_select = shiftDown && is_multiple; |
| 244 | unselect_others = !(extended_select || (ctrlDown && is_multiple)); |
| 245 | } |
| 246 | |
| 247 | // ----------------------------------------------------------------------------- |
| 248 | // wxTreeRenameTimer (internal) |
| 249 | // ----------------------------------------------------------------------------- |
| 250 | |
| 251 | wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl *owner ) |
| 252 | { |
| 253 | m_owner = owner; |
| 254 | } |
| 255 | |
| 256 | void wxTreeRenameTimer::Notify() |
| 257 | { |
| 258 | m_owner->OnRenameTimer(); |
| 259 | } |
| 260 | |
| 261 | //----------------------------------------------------------------------------- |
| 262 | // wxTreeTextCtrl (internal) |
| 263 | //----------------------------------------------------------------------------- |
| 264 | |
| 265 | IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl,wxTextCtrl); |
| 266 | |
| 267 | BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl) |
| 268 | EVT_CHAR (wxTreeTextCtrl::OnChar) |
| 269 | EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus) |
| 270 | END_EVENT_TABLE() |
| 271 | |
| 272 | wxTreeTextCtrl::wxTreeTextCtrl( wxWindow *parent, |
| 273 | const wxWindowID id, |
| 274 | bool *accept, |
| 275 | wxString *res, |
| 276 | wxTreeCtrl *owner, |
| 277 | const wxString &value, |
| 278 | const wxPoint &pos, |
| 279 | const wxSize &size, |
| 280 | int style, |
| 281 | const wxValidator& validator, |
| 282 | const wxString &name ) |
| 283 | : wxTextCtrl( parent, id, value, pos, size, style, validator, name ) |
| 284 | { |
| 285 | m_res = res; |
| 286 | m_accept = accept; |
| 287 | m_owner = owner; |
| 288 | (*m_accept) = FALSE; |
| 289 | (*m_res) = wxEmptyString; |
| 290 | m_startValue = value; |
| 291 | } |
| 292 | |
| 293 | void wxTreeTextCtrl::OnChar( wxKeyEvent &event ) |
| 294 | { |
| 295 | if (event.m_keyCode == WXK_RETURN) |
| 296 | { |
| 297 | (*m_accept) = TRUE; |
| 298 | (*m_res) = GetValue(); |
| 299 | |
| 300 | if (!wxPendingDelete.Member(this)) |
| 301 | wxPendingDelete.Append(this); |
| 302 | |
| 303 | if ((*m_accept) && ((*m_res) != m_startValue)) |
| 304 | m_owner->OnRenameAccept(); |
| 305 | |
| 306 | return; |
| 307 | } |
| 308 | if (event.m_keyCode == WXK_ESCAPE) |
| 309 | { |
| 310 | (*m_accept) = FALSE; |
| 311 | (*m_res) = ""; |
| 312 | |
| 313 | if (!wxPendingDelete.Member(this)) |
| 314 | wxPendingDelete.Append(this); |
| 315 | |
| 316 | return; |
| 317 | } |
| 318 | event.Skip(); |
| 319 | } |
| 320 | |
| 321 | void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) ) |
| 322 | { |
| 323 | if (!wxPendingDelete.Member(this)) |
| 324 | wxPendingDelete.Append(this); |
| 325 | |
| 326 | if ((*m_accept) && ((*m_res) != m_startValue)) |
| 327 | m_owner->OnRenameAccept(); |
| 328 | } |
| 329 | |
| 330 | // ----------------------------------------------------------------------------- |
| 331 | // wxTreeEvent |
| 332 | // ----------------------------------------------------------------------------- |
| 333 | |
| 334 | IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent) |
| 335 | |
| 336 | wxTreeEvent::wxTreeEvent( wxEventType commandType, int id ) |
| 337 | : wxNotifyEvent( commandType, id ) |
| 338 | { |
| 339 | m_code = 0; |
| 340 | m_itemOld = (wxGenericTreeItem *)NULL; |
| 341 | } |
| 342 | |
| 343 | // ----------------------------------------------------------------------------- |
| 344 | // wxGenericTreeItem |
| 345 | // ----------------------------------------------------------------------------- |
| 346 | |
| 347 | wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent, |
| 348 | const wxString& text, |
| 349 | wxDC& WXUNUSED(dc), |
| 350 | int image, int selImage, |
| 351 | wxTreeItemData *data) |
| 352 | : m_text(text) |
| 353 | { |
| 354 | m_images[wxTreeItemIcon_Normal] = image; |
| 355 | m_images[wxTreeItemIcon_Selected] = selImage; |
| 356 | m_images[wxTreeItemIcon_Expanded] = NO_IMAGE; |
| 357 | m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE; |
| 358 | |
| 359 | m_data = data; |
| 360 | m_x = m_y = 0; |
| 361 | m_xCross = m_yCross = 0; |
| 362 | |
| 363 | m_level = 0; |
| 364 | |
| 365 | m_isCollapsed = TRUE; |
| 366 | m_hasHilight = FALSE; |
| 367 | m_hasPlus = FALSE; |
| 368 | m_isBold = FALSE; |
| 369 | |
| 370 | m_parent = parent; |
| 371 | |
| 372 | m_attr = (wxTreeItemAttr *)NULL; |
| 373 | |
| 374 | // We don't know the height here yet. |
| 375 | m_width = 0; |
| 376 | m_height = 0; |
| 377 | } |
| 378 | |
| 379 | wxGenericTreeItem::~wxGenericTreeItem() |
| 380 | { |
| 381 | delete m_data; |
| 382 | |
| 383 | delete m_attr; |
| 384 | |
| 385 | wxASSERT_MSG( m_children.IsEmpty(), |
| 386 | wxT("please call DeleteChildren() before deleting the item") ); |
| 387 | } |
| 388 | |
| 389 | void wxGenericTreeItem::DeleteChildren(wxTreeCtrl *tree) |
| 390 | { |
| 391 | size_t count = m_children.Count(); |
| 392 | for ( size_t n = 0; n < count; n++ ) |
| 393 | { |
| 394 | wxGenericTreeItem *child = m_children[n]; |
| 395 | if (tree) |
| 396 | tree->SendDeleteEvent(child); |
| 397 | |
| 398 | child->DeleteChildren(tree); |
| 399 | delete child; |
| 400 | } |
| 401 | |
| 402 | m_children.Empty(); |
| 403 | } |
| 404 | |
| 405 | void wxGenericTreeItem::SetText( const wxString &text ) |
| 406 | { |
| 407 | m_text = text; |
| 408 | } |
| 409 | |
| 410 | void wxGenericTreeItem::Reset() |
| 411 | { |
| 412 | m_text.Empty(); |
| 413 | for ( int i = 0; i < wxTreeItemIcon_Max; i++ ) |
| 414 | { |
| 415 | m_images[i] = NO_IMAGE; |
| 416 | } |
| 417 | |
| 418 | m_data = NULL; |
| 419 | m_x = m_y = |
| 420 | m_height = m_width = 0; |
| 421 | m_xCross = |
| 422 | m_yCross = 0; |
| 423 | |
| 424 | m_level = 0; |
| 425 | |
| 426 | DeleteChildren(); |
| 427 | m_isCollapsed = TRUE; |
| 428 | |
| 429 | m_parent = (wxGenericTreeItem *)NULL; |
| 430 | } |
| 431 | |
| 432 | size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const |
| 433 | { |
| 434 | size_t count = m_children.Count(); |
| 435 | if ( !recursively ) |
| 436 | return count; |
| 437 | |
| 438 | size_t total = count; |
| 439 | for (size_t n = 0; n < count; ++n) |
| 440 | { |
| 441 | total += m_children[n]->GetChildrenCount(); |
| 442 | } |
| 443 | |
| 444 | return total; |
| 445 | } |
| 446 | |
| 447 | void wxGenericTreeItem::SetCross( int x, int y ) |
| 448 | { |
| 449 | m_xCross = x; |
| 450 | m_yCross = y; |
| 451 | } |
| 452 | |
| 453 | void wxGenericTreeItem::GetSize( int &x, int &y, const wxTreeCtrl *theTree ) |
| 454 | { |
| 455 | int bottomY=m_y+theTree->GetLineHeight(this); |
| 456 | if ( y < bottomY ) y = bottomY; |
| 457 | int width = m_x + m_width; |
| 458 | if ( x < width ) x = width; |
| 459 | |
| 460 | if (IsExpanded()) |
| 461 | { |
| 462 | size_t count = m_children.Count(); |
| 463 | for ( size_t n = 0; n < count; ++n ) |
| 464 | { |
| 465 | m_children[n]->GetSize( x, y, theTree ); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point, |
| 471 | const wxTreeCtrl *theTree, |
| 472 | int &flags) |
| 473 | { |
| 474 | if ((point.y > m_y) && (point.y < m_y + theTree->GetLineHeight(this))) |
| 475 | { |
| 476 | if (point.y < m_y+theTree->GetLineHeight(this)/2 ) |
| 477 | flags |= wxTREE_HITTEST_ONITEMUPPERPART; |
| 478 | else |
| 479 | flags |= wxTREE_HITTEST_ONITEMLOWERPART; |
| 480 | |
| 481 | // 5 is the size of the plus sign |
| 482 | if ((point.x > m_xCross-5) && (point.x < m_xCross+5) && |
| 483 | (point.y > m_yCross-5) && (point.y < m_yCross+5) && |
| 484 | (IsExpanded() || HasPlus())) |
| 485 | { |
| 486 | flags|=wxTREE_HITTEST_ONITEMBUTTON; |
| 487 | return this; |
| 488 | } |
| 489 | |
| 490 | if ((point.x >= m_x) && (point.x <= m_x+m_width)) |
| 491 | { |
| 492 | int image_w = -1; |
| 493 | int image_h; |
| 494 | |
| 495 | // assuming every image (normal and selected ) has the same size ! |
| 496 | if ( (GetImage() != NO_IMAGE) && theTree->m_imageListNormal ) |
| 497 | theTree->m_imageListNormal->GetSize(GetImage(), image_w, image_h); |
| 498 | |
| 499 | if ((image_w != -1) && (point.x <= m_x + image_w + 1)) |
| 500 | flags |= wxTREE_HITTEST_ONITEMICON; |
| 501 | else |
| 502 | flags |= wxTREE_HITTEST_ONITEMLABEL; |
| 503 | |
| 504 | return this; |
| 505 | } |
| 506 | |
| 507 | if (point.x < m_x) |
| 508 | flags |= wxTREE_HITTEST_ONITEMINDENT; |
| 509 | if (point.x > m_x+m_width) |
| 510 | flags |= wxTREE_HITTEST_ONITEMRIGHT; |
| 511 | |
| 512 | return this; |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | if (!m_isCollapsed) |
| 517 | { |
| 518 | size_t count = m_children.Count(); |
| 519 | for ( size_t n = 0; n < count; n++ ) |
| 520 | { |
| 521 | wxGenericTreeItem *res = m_children[n]->HitTest( point, theTree, flags ); |
| 522 | if ( res != NULL ) |
| 523 | return res; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | flags|=wxTREE_HITTEST_NOWHERE; |
| 529 | |
| 530 | return (wxGenericTreeItem*) NULL; |
| 531 | } |
| 532 | |
| 533 | int wxGenericTreeItem::GetCurrentImage() const |
| 534 | { |
| 535 | int image = NO_IMAGE; |
| 536 | if ( IsExpanded() ) |
| 537 | { |
| 538 | if ( IsSelected() ) |
| 539 | { |
| 540 | image = GetImage(wxTreeItemIcon_SelectedExpanded); |
| 541 | } |
| 542 | |
| 543 | if ( image == NO_IMAGE ) |
| 544 | { |
| 545 | // we usually fall back to the normal item, but try just the |
| 546 | // expanded one (and not selected) first in this case |
| 547 | image = GetImage(wxTreeItemIcon_Expanded); |
| 548 | } |
| 549 | } |
| 550 | else // not expanded |
| 551 | { |
| 552 | if ( IsSelected() ) |
| 553 | image = GetImage(wxTreeItemIcon_Selected); |
| 554 | } |
| 555 | |
| 556 | // may be it doesn't have the specific image we want, try the default one |
| 557 | // instead |
| 558 | if ( image == NO_IMAGE ) |
| 559 | { |
| 560 | image = GetImage(); |
| 561 | } |
| 562 | |
| 563 | return image; |
| 564 | } |
| 565 | |
| 566 | // ----------------------------------------------------------------------------- |
| 567 | // wxTreeCtrl implementation |
| 568 | // ----------------------------------------------------------------------------- |
| 569 | |
| 570 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow) |
| 571 | |
| 572 | BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow) |
| 573 | EVT_PAINT (wxTreeCtrl::OnPaint) |
| 574 | EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse) |
| 575 | EVT_CHAR (wxTreeCtrl::OnChar) |
| 576 | EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus) |
| 577 | EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus) |
| 578 | EVT_IDLE (wxTreeCtrl::OnIdle) |
| 579 | END_EVENT_TABLE() |
| 580 | |
| 581 | // ----------------------------------------------------------------------------- |
| 582 | // construction/destruction |
| 583 | // ----------------------------------------------------------------------------- |
| 584 | |
| 585 | void wxTreeCtrl::Init() |
| 586 | { |
| 587 | m_current = |
| 588 | m_key_current = |
| 589 | m_anchor = (wxGenericTreeItem *) NULL; |
| 590 | m_hasFocus = FALSE; |
| 591 | m_dirty = FALSE; |
| 592 | |
| 593 | m_xScroll = 0; |
| 594 | m_yScroll = 0; |
| 595 | m_lineHeight = 10; |
| 596 | m_indent = 15; |
| 597 | m_spacing = 18; |
| 598 | |
| 599 | m_hilightBrush = new wxBrush |
| 600 | ( |
| 601 | wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), |
| 602 | wxSOLID |
| 603 | ); |
| 604 | |
| 605 | m_imageListNormal = |
| 606 | m_imageListState = (wxImageList *) NULL; |
| 607 | |
| 608 | m_dragCount = 0; |
| 609 | m_isDragging = FALSE; |
| 610 | m_dropTarget = |
| 611 | m_oldSelection = (wxGenericTreeItem *)NULL; |
| 612 | |
| 613 | m_renameTimer = new wxTreeRenameTimer( this ); |
| 614 | m_lastOnSame = FALSE; |
| 615 | |
| 616 | m_normalFont = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ); |
| 617 | m_boldFont = wxFont( m_normalFont.GetPointSize(), |
| 618 | m_normalFont.GetFamily(), |
| 619 | m_normalFont.GetStyle(), |
| 620 | wxBOLD, |
| 621 | m_normalFont.GetUnderlined()); |
| 622 | } |
| 623 | |
| 624 | bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, |
| 625 | const wxPoint& pos, const wxSize& size, |
| 626 | long style, |
| 627 | const wxValidator &validator, |
| 628 | const wxString& name ) |
| 629 | { |
| 630 | Init(); |
| 631 | |
| 632 | wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name ); |
| 633 | |
| 634 | #if wxUSE_VALIDATORS |
| 635 | SetValidator( validator ); |
| 636 | #endif |
| 637 | |
| 638 | SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) ); |
| 639 | // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86 |
| 640 | m_dottedPen = wxPen( "grey", 0, 0 ); |
| 641 | |
| 642 | return TRUE; |
| 643 | } |
| 644 | |
| 645 | wxTreeCtrl::~wxTreeCtrl() |
| 646 | { |
| 647 | wxDELETE( m_hilightBrush ); |
| 648 | |
| 649 | DeleteAllItems(); |
| 650 | |
| 651 | delete m_renameTimer; |
| 652 | } |
| 653 | |
| 654 | // ----------------------------------------------------------------------------- |
| 655 | // accessors |
| 656 | // ----------------------------------------------------------------------------- |
| 657 | |
| 658 | size_t wxTreeCtrl::GetCount() const |
| 659 | { |
| 660 | return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount(); |
| 661 | } |
| 662 | |
| 663 | void wxTreeCtrl::SetIndent(unsigned int indent) |
| 664 | { |
| 665 | m_indent = indent; |
| 666 | m_dirty = TRUE; |
| 667 | } |
| 668 | |
| 669 | void wxTreeCtrl::SetSpacing(unsigned int spacing) |
| 670 | { |
| 671 | m_spacing = spacing; |
| 672 | m_dirty = TRUE; |
| 673 | } |
| 674 | |
| 675 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively) |
| 676 | { |
| 677 | wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") ); |
| 678 | |
| 679 | return item.m_pItem->GetChildrenCount(recursively); |
| 680 | } |
| 681 | |
| 682 | // ----------------------------------------------------------------------------- |
| 683 | // functions to work with tree items |
| 684 | // ----------------------------------------------------------------------------- |
| 685 | |
| 686 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const |
| 687 | { |
| 688 | wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") ); |
| 689 | |
| 690 | return item.m_pItem->GetText(); |
| 691 | } |
| 692 | |
| 693 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item, |
| 694 | wxTreeItemIcon which) const |
| 695 | { |
| 696 | wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") ); |
| 697 | |
| 698 | return item.m_pItem->GetImage(which); |
| 699 | } |
| 700 | |
| 701 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const |
| 702 | { |
| 703 | wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") ); |
| 704 | |
| 705 | return item.m_pItem->GetData(); |
| 706 | } |
| 707 | |
| 708 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) |
| 709 | { |
| 710 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 711 | |
| 712 | wxClientDC dc(this); |
| 713 | wxGenericTreeItem *pItem = item.m_pItem; |
| 714 | pItem->SetText(text); |
| 715 | CalculateSize(pItem, dc); |
| 716 | RefreshLine(pItem); |
| 717 | } |
| 718 | |
| 719 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, |
| 720 | int image, |
| 721 | wxTreeItemIcon which) |
| 722 | { |
| 723 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 724 | |
| 725 | wxGenericTreeItem *pItem = item.m_pItem; |
| 726 | pItem->SetImage(image, which); |
| 727 | |
| 728 | wxClientDC dc(this); |
| 729 | CalculateSize(pItem, dc); |
| 730 | RefreshLine(pItem); |
| 731 | } |
| 732 | |
| 733 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) |
| 734 | { |
| 735 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 736 | |
| 737 | item.m_pItem->SetData(data); |
| 738 | } |
| 739 | |
| 740 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) |
| 741 | { |
| 742 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 743 | |
| 744 | wxGenericTreeItem *pItem = item.m_pItem; |
| 745 | pItem->SetHasPlus(has); |
| 746 | RefreshLine(pItem); |
| 747 | } |
| 748 | |
| 749 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) |
| 750 | { |
| 751 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 752 | |
| 753 | // avoid redrawing the tree if no real change |
| 754 | wxGenericTreeItem *pItem = item.m_pItem; |
| 755 | if ( pItem->IsBold() != bold ) |
| 756 | { |
| 757 | pItem->SetBold(bold); |
| 758 | RefreshLine(pItem); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item, |
| 763 | const wxColour& col) |
| 764 | { |
| 765 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 766 | |
| 767 | wxGenericTreeItem *pItem = item.m_pItem; |
| 768 | pItem->Attr().SetTextColour(col); |
| 769 | RefreshLine(pItem); |
| 770 | } |
| 771 | |
| 772 | void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item, |
| 773 | const wxColour& col) |
| 774 | { |
| 775 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 776 | |
| 777 | wxGenericTreeItem *pItem = item.m_pItem; |
| 778 | pItem->Attr().SetBackgroundColour(col); |
| 779 | RefreshLine(pItem); |
| 780 | } |
| 781 | |
| 782 | void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font) |
| 783 | { |
| 784 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
| 785 | |
| 786 | wxGenericTreeItem *pItem = item.m_pItem; |
| 787 | pItem->Attr().SetFont(font); |
| 788 | RefreshLine(pItem); |
| 789 | } |
| 790 | |
| 791 | // ----------------------------------------------------------------------------- |
| 792 | // item status inquiries |
| 793 | // ----------------------------------------------------------------------------- |
| 794 | |
| 795 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const |
| 796 | { |
| 797 | wxFAIL_MSG(wxT("not implemented")); |
| 798 | |
| 799 | return TRUE; |
| 800 | } |
| 801 | |
| 802 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const |
| 803 | { |
| 804 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); |
| 805 | |
| 806 | return !item.m_pItem->GetChildren().IsEmpty(); |
| 807 | } |
| 808 | |
| 809 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const |
| 810 | { |
| 811 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); |
| 812 | |
| 813 | return item.m_pItem->IsExpanded(); |
| 814 | } |
| 815 | |
| 816 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const |
| 817 | { |
| 818 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); |
| 819 | |
| 820 | return item.m_pItem->IsSelected(); |
| 821 | } |
| 822 | |
| 823 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const |
| 824 | { |
| 825 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); |
| 826 | |
| 827 | return item.m_pItem->IsBold(); |
| 828 | } |
| 829 | |
| 830 | // ----------------------------------------------------------------------------- |
| 831 | // navigation |
| 832 | // ----------------------------------------------------------------------------- |
| 833 | |
| 834 | wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const |
| 835 | { |
| 836 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 837 | |
| 838 | return item.m_pItem->GetParent(); |
| 839 | } |
| 840 | |
| 841 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const |
| 842 | { |
| 843 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 844 | |
| 845 | cookie = 0; |
| 846 | return GetNextChild(item, cookie); |
| 847 | } |
| 848 | |
| 849 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const |
| 850 | { |
| 851 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 852 | |
| 853 | wxArrayGenericTreeItems& children = item.m_pItem->GetChildren(); |
| 854 | if ( (size_t)cookie < children.Count() ) |
| 855 | { |
| 856 | return children.Item((size_t)cookie++); |
| 857 | } |
| 858 | else |
| 859 | { |
| 860 | // there are no more of them |
| 861 | return wxTreeItemId(); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const |
| 866 | { |
| 867 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 868 | |
| 869 | wxArrayGenericTreeItems& children = item.m_pItem->GetChildren(); |
| 870 | return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last())); |
| 871 | } |
| 872 | |
| 873 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const |
| 874 | { |
| 875 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 876 | |
| 877 | wxGenericTreeItem *i = item.m_pItem; |
| 878 | wxGenericTreeItem *parent = i->GetParent(); |
| 879 | if ( parent == NULL ) |
| 880 | { |
| 881 | // root item doesn't have any siblings |
| 882 | return wxTreeItemId(); |
| 883 | } |
| 884 | |
| 885 | wxArrayGenericTreeItems& siblings = parent->GetChildren(); |
| 886 | int index = siblings.Index(i); |
| 887 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? |
| 888 | |
| 889 | size_t n = (size_t)(index + 1); |
| 890 | return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]); |
| 891 | } |
| 892 | |
| 893 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const |
| 894 | { |
| 895 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 896 | |
| 897 | wxGenericTreeItem *i = item.m_pItem; |
| 898 | wxGenericTreeItem *parent = i->GetParent(); |
| 899 | if ( parent == NULL ) |
| 900 | { |
| 901 | // root item doesn't have any siblings |
| 902 | return wxTreeItemId(); |
| 903 | } |
| 904 | |
| 905 | wxArrayGenericTreeItems& siblings = parent->GetChildren(); |
| 906 | int index = siblings.Index(i); |
| 907 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? |
| 908 | |
| 909 | return index == 0 ? wxTreeItemId() |
| 910 | : wxTreeItemId(siblings[(size_t)(index - 1)]); |
| 911 | } |
| 912 | |
| 913 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const |
| 914 | { |
| 915 | wxFAIL_MSG(wxT("not implemented")); |
| 916 | |
| 917 | return wxTreeItemId(); |
| 918 | } |
| 919 | |
| 920 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const |
| 921 | { |
| 922 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 923 | |
| 924 | wxFAIL_MSG(wxT("not implemented")); |
| 925 | |
| 926 | return wxTreeItemId(); |
| 927 | } |
| 928 | |
| 929 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const |
| 930 | { |
| 931 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
| 932 | |
| 933 | wxFAIL_MSG(wxT("not implemented")); |
| 934 | |
| 935 | return wxTreeItemId(); |
| 936 | } |
| 937 | |
| 938 | // ----------------------------------------------------------------------------- |
| 939 | // operations |
| 940 | // ----------------------------------------------------------------------------- |
| 941 | |
| 942 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId, |
| 943 | size_t previous, |
| 944 | const wxString& text, |
| 945 | int image, int selImage, |
| 946 | wxTreeItemData *data) |
| 947 | { |
| 948 | wxGenericTreeItem *parent = parentId.m_pItem; |
| 949 | if ( !parent ) |
| 950 | { |
| 951 | // should we give a warning here? |
| 952 | return AddRoot(text, image, selImage, data); |
| 953 | } |
| 954 | |
| 955 | wxClientDC dc(this); |
| 956 | wxGenericTreeItem *item = |
| 957 | new wxGenericTreeItem( parent, text, dc, image, selImage, data ); |
| 958 | |
| 959 | if ( data != NULL ) |
| 960 | { |
| 961 | data->m_pItem = item; |
| 962 | } |
| 963 | |
| 964 | parent->Insert( item, previous ); |
| 965 | |
| 966 | m_dirty = TRUE; |
| 967 | |
| 968 | return item; |
| 969 | } |
| 970 | |
| 971 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, |
| 972 | int image, int selImage, |
| 973 | wxTreeItemData *data) |
| 974 | { |
| 975 | wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") ); |
| 976 | |
| 977 | wxClientDC dc(this); |
| 978 | m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc, |
| 979 | image, selImage, data); |
| 980 | if ( data != NULL ) |
| 981 | { |
| 982 | data->m_pItem = m_anchor; |
| 983 | } |
| 984 | |
| 985 | if (!HasFlag(wxTR_MULTIPLE)) |
| 986 | { |
| 987 | m_current = m_key_current = m_anchor; |
| 988 | m_current->SetHilight( TRUE ); |
| 989 | } |
| 990 | |
| 991 | m_dirty = TRUE; |
| 992 | |
| 993 | return m_anchor; |
| 994 | } |
| 995 | |
| 996 | wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, |
| 997 | const wxString& text, |
| 998 | int image, int selImage, |
| 999 | wxTreeItemData *data) |
| 1000 | { |
| 1001 | return DoInsertItem(parent, 0u, text, image, selImage, data); |
| 1002 | } |
| 1003 | |
| 1004 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId, |
| 1005 | const wxTreeItemId& idPrevious, |
| 1006 | const wxString& text, |
| 1007 | int image, int selImage, |
| 1008 | wxTreeItemData *data) |
| 1009 | { |
| 1010 | wxGenericTreeItem *parent = parentId.m_pItem; |
| 1011 | if ( !parent ) |
| 1012 | { |
| 1013 | // should we give a warning here? |
| 1014 | return AddRoot(text, image, selImage, data); |
| 1015 | } |
| 1016 | |
| 1017 | int index = parent->GetChildren().Index(idPrevious.m_pItem); |
| 1018 | wxASSERT_MSG( index != wxNOT_FOUND, |
| 1019 | wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") ); |
| 1020 | |
| 1021 | return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data); |
| 1022 | } |
| 1023 | |
| 1024 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId, |
| 1025 | size_t before, |
| 1026 | const wxString& text, |
| 1027 | int image, int selImage, |
| 1028 | wxTreeItemData *data) |
| 1029 | { |
| 1030 | wxGenericTreeItem *parent = parentId.m_pItem; |
| 1031 | if ( !parent ) |
| 1032 | { |
| 1033 | // should we give a warning here? |
| 1034 | return AddRoot(text, image, selImage, data); |
| 1035 | } |
| 1036 | |
| 1037 | return DoInsertItem(parentId, before, text, image, selImage, data); |
| 1038 | } |
| 1039 | |
| 1040 | wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId, |
| 1041 | const wxString& text, |
| 1042 | int image, int selImage, |
| 1043 | wxTreeItemData *data) |
| 1044 | { |
| 1045 | wxGenericTreeItem *parent = parentId.m_pItem; |
| 1046 | if ( !parent ) |
| 1047 | { |
| 1048 | // should we give a warning here? |
| 1049 | return AddRoot(text, image, selImage, data); |
| 1050 | } |
| 1051 | |
| 1052 | return DoInsertItem( parent, parent->GetChildren().Count(), text, |
| 1053 | image, selImage, data); |
| 1054 | } |
| 1055 | |
| 1056 | void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item) |
| 1057 | { |
| 1058 | wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() ); |
| 1059 | event.m_item = item; |
| 1060 | event.SetEventObject( this ); |
| 1061 | ProcessEvent( event ); |
| 1062 | } |
| 1063 | |
| 1064 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId) |
| 1065 | { |
| 1066 | wxGenericTreeItem *item = itemId.m_pItem; |
| 1067 | item->DeleteChildren(this); |
| 1068 | |
| 1069 | m_dirty = TRUE; |
| 1070 | } |
| 1071 | |
| 1072 | void wxTreeCtrl::Delete(const wxTreeItemId& itemId) |
| 1073 | { |
| 1074 | wxGenericTreeItem *item = itemId.m_pItem; |
| 1075 | |
| 1076 | // don't stay with invalid m_key_current or we will crash in the next call |
| 1077 | // to OnChar() |
| 1078 | bool changeKeyCurrent = FALSE; |
| 1079 | wxGenericTreeItem *itemKey = m_key_current; |
| 1080 | while ( itemKey && !changeKeyCurrent ) |
| 1081 | { |
| 1082 | if ( itemKey == item ) |
| 1083 | { |
| 1084 | // m_key_current is a descendant of the item being deleted |
| 1085 | changeKeyCurrent = TRUE; |
| 1086 | } |
| 1087 | else |
| 1088 | { |
| 1089 | itemKey = itemKey->GetParent(); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | wxGenericTreeItem *parent = item->GetParent(); |
| 1094 | if ( parent ) |
| 1095 | { |
| 1096 | parent->GetChildren().Remove( item ); // remove by value |
| 1097 | } |
| 1098 | |
| 1099 | if ( changeKeyCurrent ) |
| 1100 | { |
| 1101 | // may be NULL or not |
| 1102 | m_key_current = parent; |
| 1103 | } |
| 1104 | |
| 1105 | item->DeleteChildren(this); |
| 1106 | SendDeleteEvent(item); |
| 1107 | delete item; |
| 1108 | |
| 1109 | m_dirty = TRUE; |
| 1110 | } |
| 1111 | |
| 1112 | void wxTreeCtrl::DeleteAllItems() |
| 1113 | { |
| 1114 | if ( m_anchor ) |
| 1115 | { |
| 1116 | m_anchor->DeleteChildren(this); |
| 1117 | delete m_anchor; |
| 1118 | |
| 1119 | m_anchor = NULL; |
| 1120 | |
| 1121 | m_dirty = TRUE; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | void wxTreeCtrl::Expand(const wxTreeItemId& itemId) |
| 1126 | { |
| 1127 | wxGenericTreeItem *item = itemId.m_pItem; |
| 1128 | |
| 1129 | wxCHECK_RET( item, _T("invalid item in wxTreeCtrl::Expand") ); |
| 1130 | |
| 1131 | if ( !item->HasPlus() ) |
| 1132 | return; |
| 1133 | |
| 1134 | if ( item->IsExpanded() ) |
| 1135 | return; |
| 1136 | |
| 1137 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() ); |
| 1138 | event.m_item = item; |
| 1139 | event.SetEventObject( this ); |
| 1140 | |
| 1141 | if ( ProcessEvent( event ) && !event.IsAllowed() ) |
| 1142 | { |
| 1143 | // cancelled by program |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | item->Expand(); |
| 1148 | CalculatePositions(); |
| 1149 | |
| 1150 | RefreshSubtree(item); |
| 1151 | |
| 1152 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED); |
| 1153 | ProcessEvent( event ); |
| 1154 | } |
| 1155 | |
| 1156 | void wxTreeCtrl::ExpandAll(const wxTreeItemId& item) |
| 1157 | { |
| 1158 | Expand(item); |
| 1159 | if ( IsExpanded(item) ) |
| 1160 | { |
| 1161 | long cookie; |
| 1162 | wxTreeItemId child = GetFirstChild(item, cookie); |
| 1163 | while ( child.IsOk() ) |
| 1164 | { |
| 1165 | ExpandAll(child); |
| 1166 | |
| 1167 | child = GetNextChild(item, cookie); |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | void wxTreeCtrl::Collapse(const wxTreeItemId& itemId) |
| 1173 | { |
| 1174 | wxGenericTreeItem *item = itemId.m_pItem; |
| 1175 | |
| 1176 | if ( !item->IsExpanded() ) |
| 1177 | return; |
| 1178 | |
| 1179 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() ); |
| 1180 | event.m_item = item; |
| 1181 | event.SetEventObject( this ); |
| 1182 | if ( ProcessEvent( event ) && !event.IsAllowed() ) |
| 1183 | { |
| 1184 | // cancelled by program |
| 1185 | return; |
| 1186 | } |
| 1187 | |
| 1188 | item->Collapse(); |
| 1189 | |
| 1190 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 1191 | size_t count = children.Count(); |
| 1192 | for ( size_t n = 0; n < count; n++ ) |
| 1193 | { |
| 1194 | Collapse(children[n]); |
| 1195 | } |
| 1196 | |
| 1197 | CalculatePositions(); |
| 1198 | |
| 1199 | RefreshSubtree(item); |
| 1200 | |
| 1201 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED); |
| 1202 | ProcessEvent( event ); |
| 1203 | } |
| 1204 | |
| 1205 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) |
| 1206 | { |
| 1207 | Collapse(item); |
| 1208 | DeleteChildren(item); |
| 1209 | } |
| 1210 | |
| 1211 | void wxTreeCtrl::Toggle(const wxTreeItemId& itemId) |
| 1212 | { |
| 1213 | wxGenericTreeItem *item = itemId.m_pItem; |
| 1214 | |
| 1215 | if (item->IsExpanded()) |
| 1216 | Collapse(itemId); |
| 1217 | else |
| 1218 | Expand(itemId); |
| 1219 | } |
| 1220 | |
| 1221 | void wxTreeCtrl::Unselect() |
| 1222 | { |
| 1223 | if (m_current) |
| 1224 | { |
| 1225 | m_current->SetHilight( FALSE ); |
| 1226 | RefreshLine( m_current ); |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item) |
| 1231 | { |
| 1232 | if (item->IsSelected()) |
| 1233 | { |
| 1234 | item->SetHilight(FALSE); |
| 1235 | RefreshLine(item); |
| 1236 | } |
| 1237 | |
| 1238 | if (item->HasChildren()) |
| 1239 | { |
| 1240 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 1241 | size_t count = children.Count(); |
| 1242 | for ( size_t n = 0; n < count; ++n ) |
| 1243 | { |
| 1244 | UnselectAllChildren(children[n]); |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | void wxTreeCtrl::UnselectAll() |
| 1250 | { |
| 1251 | UnselectAllChildren(GetRootItem().m_pItem); |
| 1252 | } |
| 1253 | |
| 1254 | // Recursive function ! |
| 1255 | // To stop we must have crt_item<last_item |
| 1256 | // Algorithm : |
| 1257 | // Tag all next children, when no more children, |
| 1258 | // Move to parent (not to tag) |
| 1259 | // Keep going... if we found last_item, we stop. |
| 1260 | bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select) |
| 1261 | { |
| 1262 | wxGenericTreeItem *parent = crt_item->GetParent(); |
| 1263 | |
| 1264 | if (parent == NULL) // This is root item |
| 1265 | return TagAllChildrenUntilLast(crt_item, last_item, select); |
| 1266 | |
| 1267 | wxArrayGenericTreeItems& children = parent->GetChildren(); |
| 1268 | int index = children.Index(crt_item); |
| 1269 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? |
| 1270 | |
| 1271 | size_t count = children.Count(); |
| 1272 | for (size_t n=(size_t)(index+1); n<count; ++n) |
| 1273 | { |
| 1274 | if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE; |
| 1275 | } |
| 1276 | |
| 1277 | return TagNextChildren(parent, last_item, select); |
| 1278 | } |
| 1279 | |
| 1280 | bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select) |
| 1281 | { |
| 1282 | crt_item->SetHilight(select); |
| 1283 | RefreshLine(crt_item); |
| 1284 | |
| 1285 | if (crt_item==last_item) |
| 1286 | return TRUE; |
| 1287 | |
| 1288 | if (crt_item->HasChildren()) |
| 1289 | { |
| 1290 | wxArrayGenericTreeItems& children = crt_item->GetChildren(); |
| 1291 | size_t count = children.Count(); |
| 1292 | for ( size_t n = 0; n < count; ++n ) |
| 1293 | { |
| 1294 | if (TagAllChildrenUntilLast(children[n], last_item, select)) |
| 1295 | return TRUE; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | return FALSE; |
| 1300 | } |
| 1301 | |
| 1302 | void wxTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2) |
| 1303 | { |
| 1304 | // item2 is not necessary after item1 |
| 1305 | wxGenericTreeItem *first=NULL, *last=NULL; |
| 1306 | |
| 1307 | // choice first' and 'last' between item1 and item2 |
| 1308 | if (item1->GetY()<item2->GetY()) |
| 1309 | { |
| 1310 | first=item1; |
| 1311 | last=item2; |
| 1312 | } |
| 1313 | else |
| 1314 | { |
| 1315 | first=item2; |
| 1316 | last=item1; |
| 1317 | } |
| 1318 | |
| 1319 | bool select = m_current->IsSelected(); |
| 1320 | |
| 1321 | if ( TagAllChildrenUntilLast(first,last,select) ) |
| 1322 | return; |
| 1323 | |
| 1324 | TagNextChildren(first,last,select); |
| 1325 | } |
| 1326 | |
| 1327 | void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId, |
| 1328 | bool unselect_others, |
| 1329 | bool extended_select) |
| 1330 | { |
| 1331 | wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") ); |
| 1332 | |
| 1333 | bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE); |
| 1334 | wxGenericTreeItem *item = itemId.m_pItem; |
| 1335 | |
| 1336 | //wxCHECK_RET( ( (!unselect_others) && is_single), |
| 1337 | // wxT("this is a single selection tree") ); |
| 1338 | |
| 1339 | // to keep going anyhow !!! |
| 1340 | if (is_single) |
| 1341 | { |
| 1342 | if (item->IsSelected()) |
| 1343 | return; // nothing to do |
| 1344 | unselect_others = TRUE; |
| 1345 | extended_select = FALSE; |
| 1346 | } |
| 1347 | else if ( unselect_others && item->IsSelected() ) |
| 1348 | { |
| 1349 | // selection change if there is more than one item currently selected |
| 1350 | wxArrayTreeItemIds selected_items; |
| 1351 | if ( GetSelections(selected_items) == 1 ) |
| 1352 | return; |
| 1353 | } |
| 1354 | |
| 1355 | wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() ); |
| 1356 | event.m_item = item; |
| 1357 | event.m_itemOld = m_current; |
| 1358 | event.SetEventObject( this ); |
| 1359 | // TODO : Here we don't send any selection mode yet ! |
| 1360 | |
| 1361 | if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() ) |
| 1362 | return; |
| 1363 | |
| 1364 | // ctrl press |
| 1365 | if (unselect_others) |
| 1366 | { |
| 1367 | if (is_single) Unselect(); // to speed up thing |
| 1368 | else UnselectAll(); |
| 1369 | } |
| 1370 | |
| 1371 | // shift press |
| 1372 | if (extended_select) |
| 1373 | { |
| 1374 | if ( !m_current ) |
| 1375 | { |
| 1376 | m_current = |
| 1377 | m_key_current = GetRootItem().m_pItem; |
| 1378 | } |
| 1379 | |
| 1380 | // don't change the mark (m_current) |
| 1381 | SelectItemRange(m_current, item); |
| 1382 | } |
| 1383 | else |
| 1384 | { |
| 1385 | bool select=TRUE; // the default |
| 1386 | |
| 1387 | // Check if we need to toggle hilight (ctrl mode) |
| 1388 | if (!unselect_others) |
| 1389 | select=!item->IsSelected(); |
| 1390 | |
| 1391 | m_current = m_key_current = item; |
| 1392 | m_current->SetHilight(select); |
| 1393 | RefreshLine( m_current ); |
| 1394 | } |
| 1395 | |
| 1396 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); |
| 1397 | GetEventHandler()->ProcessEvent( event ); |
| 1398 | } |
| 1399 | |
| 1400 | void wxTreeCtrl::FillArray(wxGenericTreeItem *item, |
| 1401 | wxArrayTreeItemIds &array) const |
| 1402 | { |
| 1403 | if ( item->IsSelected() ) |
| 1404 | array.Add(wxTreeItemId(item)); |
| 1405 | |
| 1406 | if ( item->HasChildren() ) |
| 1407 | { |
| 1408 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 1409 | size_t count = children.GetCount(); |
| 1410 | for ( size_t n = 0; n < count; ++n ) |
| 1411 | FillArray(children[n], array); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const |
| 1416 | { |
| 1417 | array.Empty(); |
| 1418 | wxTreeItemId idRoot = GetRootItem(); |
| 1419 | if ( idRoot.IsOk() ) |
| 1420 | { |
| 1421 | FillArray(idRoot.m_pItem, array); |
| 1422 | } |
| 1423 | //else: the tree is empty, so no selections |
| 1424 | |
| 1425 | return array.Count(); |
| 1426 | } |
| 1427 | |
| 1428 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) |
| 1429 | { |
| 1430 | if (!item.IsOk()) return; |
| 1431 | |
| 1432 | wxGenericTreeItem *gitem = item.m_pItem; |
| 1433 | |
| 1434 | // first expand all parent branches |
| 1435 | wxGenericTreeItem *parent = gitem->GetParent(); |
| 1436 | while ( parent ) |
| 1437 | { |
| 1438 | Expand(parent); |
| 1439 | parent = parent->GetParent(); |
| 1440 | } |
| 1441 | |
| 1442 | //if (parent) CalculatePositions(); |
| 1443 | |
| 1444 | ScrollTo(item); |
| 1445 | } |
| 1446 | |
| 1447 | void wxTreeCtrl::ScrollTo(const wxTreeItemId &item) |
| 1448 | { |
| 1449 | if (!item.IsOk()) return; |
| 1450 | |
| 1451 | // We have to call this here because the label in |
| 1452 | // question might just have been added and no screen |
| 1453 | // update taken place. |
| 1454 | if (m_dirty) wxYield(); |
| 1455 | |
| 1456 | wxGenericTreeItem *gitem = item.m_pItem; |
| 1457 | |
| 1458 | // now scroll to the item |
| 1459 | int item_y = gitem->GetY(); |
| 1460 | |
| 1461 | int start_x = 0; |
| 1462 | int start_y = 0; |
| 1463 | ViewStart( &start_x, &start_y ); |
| 1464 | start_y *= PIXELS_PER_UNIT; |
| 1465 | |
| 1466 | int client_h = 0; |
| 1467 | int client_w = 0; |
| 1468 | GetClientSize( &client_w, &client_h ); |
| 1469 | |
| 1470 | if (item_y < start_y+3) |
| 1471 | { |
| 1472 | // going down |
| 1473 | int x = 0; |
| 1474 | int y = 0; |
| 1475 | m_anchor->GetSize( x, y, this ); |
| 1476 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 1477 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 1478 | int x_pos = GetScrollPos( wxHORIZONTAL ); |
| 1479 | // Item should appear at top |
| 1480 | SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT ); |
| 1481 | } |
| 1482 | else if (item_y+GetLineHeight(gitem) > start_y+client_h) |
| 1483 | { |
| 1484 | // going up |
| 1485 | int x = 0; |
| 1486 | int y = 0; |
| 1487 | m_anchor->GetSize( x, y, this ); |
| 1488 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 1489 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 1490 | item_y += PIXELS_PER_UNIT+2; |
| 1491 | int x_pos = GetScrollPos( wxHORIZONTAL ); |
| 1492 | // Item should appear at bottom |
| 1493 | 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 ); |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | // FIXME: tree sorting functions are not reentrant and not MT-safe! |
| 1498 | static wxTreeCtrl *s_treeBeingSorted = NULL; |
| 1499 | |
| 1500 | static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1, |
| 1501 | wxGenericTreeItem **item2) |
| 1502 | { |
| 1503 | wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxTreeCtrl::SortChildren()") ); |
| 1504 | |
| 1505 | return s_treeBeingSorted->OnCompareItems(*item1, *item2); |
| 1506 | } |
| 1507 | |
| 1508 | int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
| 1509 | const wxTreeItemId& item2) |
| 1510 | { |
| 1511 | return wxStrcmp(GetItemText(item1), GetItemText(item2)); |
| 1512 | } |
| 1513 | |
| 1514 | void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId) |
| 1515 | { |
| 1516 | wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") ); |
| 1517 | |
| 1518 | wxGenericTreeItem *item = itemId.m_pItem; |
| 1519 | |
| 1520 | wxCHECK_RET( !s_treeBeingSorted, |
| 1521 | wxT("wxTreeCtrl::SortChildren is not reentrant") ); |
| 1522 | |
| 1523 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 1524 | if ( children.Count() > 1 ) |
| 1525 | { |
| 1526 | s_treeBeingSorted = this; |
| 1527 | children.Sort(tree_ctrl_compare_func); |
| 1528 | s_treeBeingSorted = NULL; |
| 1529 | |
| 1530 | m_dirty = TRUE; |
| 1531 | } |
| 1532 | //else: don't make the tree dirty as nothing changed |
| 1533 | } |
| 1534 | |
| 1535 | wxImageList *wxTreeCtrl::GetImageList() const |
| 1536 | { |
| 1537 | return m_imageListNormal; |
| 1538 | } |
| 1539 | |
| 1540 | wxImageList *wxTreeCtrl::GetStateImageList() const |
| 1541 | { |
| 1542 | return m_imageListState; |
| 1543 | } |
| 1544 | |
| 1545 | void wxTreeCtrl::SetImageList(wxImageList *imageList) |
| 1546 | { |
| 1547 | m_imageListNormal = imageList; |
| 1548 | |
| 1549 | if ( !m_imageListNormal ) |
| 1550 | return; |
| 1551 | |
| 1552 | // Calculate a m_lineHeight value from the image sizes. |
| 1553 | // May be toggle off. Then wxTreeCtrl will spread when |
| 1554 | // necessary (which might look ugly). |
| 1555 | wxClientDC dc(this); |
| 1556 | m_lineHeight = (int)(dc.GetCharHeight() + 4); |
| 1557 | int width = 0, height = 0, |
| 1558 | n = m_imageListNormal->GetImageCount(); |
| 1559 | |
| 1560 | for (int i = 0; i < n ; i++) |
| 1561 | { |
| 1562 | m_imageListNormal->GetSize(i, width, height); |
| 1563 | if (height > m_lineHeight) m_lineHeight = height; |
| 1564 | } |
| 1565 | |
| 1566 | if (m_lineHeight < 40) |
| 1567 | m_lineHeight += 2; // at least 2 pixels |
| 1568 | else |
| 1569 | m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing |
| 1570 | } |
| 1571 | |
| 1572 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) |
| 1573 | { |
| 1574 | m_imageListState = imageList; |
| 1575 | } |
| 1576 | |
| 1577 | // ----------------------------------------------------------------------------- |
| 1578 | // helpers |
| 1579 | // ----------------------------------------------------------------------------- |
| 1580 | |
| 1581 | void wxTreeCtrl::AdjustMyScrollbars() |
| 1582 | { |
| 1583 | if (m_anchor) |
| 1584 | { |
| 1585 | int x = 0; |
| 1586 | int y = 0; |
| 1587 | m_anchor->GetSize( x, y, this ); |
| 1588 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 1589 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
| 1590 | int x_pos = GetScrollPos( wxHORIZONTAL ); |
| 1591 | int y_pos = GetScrollPos( wxVERTICAL ); |
| 1592 | SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos ); |
| 1593 | } |
| 1594 | else |
| 1595 | { |
| 1596 | SetScrollbars( 0, 0, 0, 0 ); |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | int wxTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const |
| 1601 | { |
| 1602 | if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT) |
| 1603 | return item->GetHeight(); |
| 1604 | else |
| 1605 | return m_lineHeight; |
| 1606 | } |
| 1607 | |
| 1608 | void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc) |
| 1609 | { |
| 1610 | wxTreeItemAttr *attr = item->GetAttributes(); |
| 1611 | if ( attr && attr->HasFont() ) |
| 1612 | dc.SetFont(attr->GetFont()); |
| 1613 | else if (item->IsBold()) |
| 1614 | dc.SetFont(m_boldFont); |
| 1615 | |
| 1616 | long text_w = 0; |
| 1617 | long text_h = 0; |
| 1618 | dc.GetTextExtent( item->GetText(), &text_w, &text_h ); |
| 1619 | |
| 1620 | int image_h = 0; |
| 1621 | int image_w = 0; |
| 1622 | int image = item->GetCurrentImage(); |
| 1623 | if ( image != NO_IMAGE ) |
| 1624 | { |
| 1625 | if ( m_imageListNormal ) |
| 1626 | { |
| 1627 | m_imageListNormal->GetSize( image, image_w, image_h ); |
| 1628 | image_w += 4; |
| 1629 | } |
| 1630 | else |
| 1631 | { |
| 1632 | image = NO_IMAGE; |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | int total_h = GetLineHeight(item); |
| 1637 | |
| 1638 | if (item->IsSelected()) |
| 1639 | dc.SetBrush(*m_hilightBrush); |
| 1640 | else |
| 1641 | { |
| 1642 | wxColour colBg; |
| 1643 | if ( attr && attr->HasBackgroundColour() ) |
| 1644 | colBg = attr->GetBackgroundColour(); |
| 1645 | else |
| 1646 | colBg = m_backgroundColour; |
| 1647 | dc.SetBrush(wxBrush(colBg, wxSOLID)); |
| 1648 | } |
| 1649 | |
| 1650 | dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h ); |
| 1651 | |
| 1652 | if ( image != NO_IMAGE ) |
| 1653 | { |
| 1654 | dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h ); |
| 1655 | m_imageListNormal->Draw( image, dc, |
| 1656 | item->GetX(), |
| 1657 | item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0), |
| 1658 | wxIMAGELIST_DRAW_TRANSPARENT ); |
| 1659 | dc.DestroyClippingRegion(); |
| 1660 | } |
| 1661 | |
| 1662 | dc.SetBackgroundMode(wxTRANSPARENT); |
| 1663 | int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0; |
| 1664 | dc.DrawText( item->GetText(), |
| 1665 | (wxCoord)(image_w + item->GetX()), |
| 1666 | (wxCoord)(item->GetY() + extraH)); |
| 1667 | |
| 1668 | // restore normal font |
| 1669 | dc.SetFont( m_normalFont ); |
| 1670 | } |
| 1671 | |
| 1672 | // Now y stands for the top of the item, whereas it used to stand for middle ! |
| 1673 | void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ) |
| 1674 | { |
| 1675 | int horizX = level*m_indent; |
| 1676 | |
| 1677 | item->SetX( horizX+m_indent+m_spacing ); |
| 1678 | item->SetY( y ); |
| 1679 | |
| 1680 | int oldY = y; |
| 1681 | y+=GetLineHeight(item)/2; |
| 1682 | |
| 1683 | item->SetCross( horizX+m_indent, y ); |
| 1684 | |
| 1685 | int exposed_x = dc.LogicalToDeviceX( 0 ); |
| 1686 | int exposed_y = dc.LogicalToDeviceY( item->GetY() ); |
| 1687 | |
| 1688 | if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much |
| 1689 | { |
| 1690 | int startX = horizX; |
| 1691 | int endX = horizX + (m_indent-5); |
| 1692 | |
| 1693 | // if (!item->HasChildren()) endX += (m_indent+5); |
| 1694 | if (!item->HasChildren()) endX += 20; |
| 1695 | |
| 1696 | dc.DrawLine( startX, y, endX, y ); |
| 1697 | |
| 1698 | if (item->HasPlus()) |
| 1699 | { |
| 1700 | dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y ); |
| 1701 | dc.SetPen( *wxGREY_PEN ); |
| 1702 | dc.SetBrush( *wxWHITE_BRUSH ); |
| 1703 | dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 ); |
| 1704 | |
| 1705 | dc.SetPen( *wxBLACK_PEN ); |
| 1706 | dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y ); |
| 1707 | if (!item->IsExpanded()) |
| 1708 | dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 ); |
| 1709 | |
| 1710 | dc.SetPen( m_dottedPen ); |
| 1711 | } |
| 1712 | |
| 1713 | wxPen *pen = wxTRANSPARENT_PEN; |
| 1714 | wxColour colText; |
| 1715 | |
| 1716 | if ( item->IsSelected() ) |
| 1717 | { |
| 1718 | colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ); |
| 1719 | |
| 1720 | if ( m_hasFocus ) |
| 1721 | pen = wxBLACK_PEN; |
| 1722 | |
| 1723 | } |
| 1724 | else |
| 1725 | { |
| 1726 | wxTreeItemAttr *attr = item->GetAttributes(); |
| 1727 | if ( attr && attr->HasTextColour() ) |
| 1728 | colText = attr->GetTextColour(); |
| 1729 | else |
| 1730 | colText = *wxBLACK; |
| 1731 | } |
| 1732 | |
| 1733 | // prepare to draw |
| 1734 | dc.SetTextForeground(colText); |
| 1735 | dc.SetPen(*pen); |
| 1736 | |
| 1737 | // draw |
| 1738 | PaintItem(item, dc); |
| 1739 | |
| 1740 | // restore DC objects |
| 1741 | dc.SetBrush( *wxWHITE_BRUSH ); |
| 1742 | dc.SetPen( m_dottedPen ); |
| 1743 | dc.SetTextForeground( *wxBLACK ); |
| 1744 | } |
| 1745 | |
| 1746 | y = oldY+GetLineHeight(item); |
| 1747 | |
| 1748 | if (item->IsExpanded()) |
| 1749 | { |
| 1750 | oldY+=GetLineHeight(item)/2; |
| 1751 | int semiOldY=0; |
| 1752 | |
| 1753 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 1754 | size_t n, count = children.Count(); |
| 1755 | for ( n = 0; n < count; ++n ) |
| 1756 | { |
| 1757 | semiOldY=y; |
| 1758 | PaintLevel( children[n], dc, level+1, y ); |
| 1759 | } |
| 1760 | |
| 1761 | // it may happen that the item is expanded but has no items (when you |
| 1762 | // delete all its children for example) - don't draw the vertical line |
| 1763 | // in this case |
| 1764 | if (count > 0) |
| 1765 | { |
| 1766 | semiOldY+=GetLineHeight(children[--n])/2; |
| 1767 | dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY ); |
| 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | void wxTreeCtrl::DrawDropEffect(wxGenericTreeItem *item) |
| 1773 | { |
| 1774 | if ( item ) |
| 1775 | { |
| 1776 | if ( item->HasPlus() ) |
| 1777 | { |
| 1778 | // it's a folder, indicate it by a border |
| 1779 | DrawBorder(item); |
| 1780 | } |
| 1781 | else |
| 1782 | { |
| 1783 | // draw a line under the drop target because the item will be |
| 1784 | // dropped there |
| 1785 | DrawLine(item, TRUE /* below */); |
| 1786 | } |
| 1787 | |
| 1788 | SetCursor(wxCURSOR_BULLSEYE); |
| 1789 | } |
| 1790 | else |
| 1791 | { |
| 1792 | // can't drop here |
| 1793 | SetCursor(wxCURSOR_NO_ENTRY); |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | void wxTreeCtrl::DrawBorder(const wxTreeItemId &item) |
| 1798 | { |
| 1799 | wxCHECK_RET( item.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") ); |
| 1800 | |
| 1801 | wxGenericTreeItem *i = item.m_pItem; |
| 1802 | |
| 1803 | wxClientDC dc(this); |
| 1804 | PrepareDC( dc ); |
| 1805 | dc.SetLogicalFunction(wxINVERT); |
| 1806 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
| 1807 | |
| 1808 | int w = i->GetWidth() + 2; |
| 1809 | int h = GetLineHeight(i) + 2; |
| 1810 | |
| 1811 | dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h); |
| 1812 | } |
| 1813 | |
| 1814 | void wxTreeCtrl::DrawLine(const wxTreeItemId &item, bool below) |
| 1815 | { |
| 1816 | wxCHECK_RET( item.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") ); |
| 1817 | |
| 1818 | wxGenericTreeItem *i = item.m_pItem; |
| 1819 | |
| 1820 | wxClientDC dc(this); |
| 1821 | PrepareDC( dc ); |
| 1822 | dc.SetLogicalFunction(wxINVERT); |
| 1823 | |
| 1824 | int x = i->GetX(), |
| 1825 | y = i->GetY(); |
| 1826 | if ( below ) |
| 1827 | { |
| 1828 | y += GetLineHeight(i) - 1; |
| 1829 | } |
| 1830 | |
| 1831 | dc.DrawLine( x, y, x + i->GetWidth(), y); |
| 1832 | } |
| 1833 | |
| 1834 | // ----------------------------------------------------------------------------- |
| 1835 | // wxWindows callbacks |
| 1836 | // ----------------------------------------------------------------------------- |
| 1837 | |
| 1838 | void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
| 1839 | { |
| 1840 | if ( !m_anchor) |
| 1841 | return; |
| 1842 | |
| 1843 | wxPaintDC dc(this); |
| 1844 | PrepareDC( dc ); |
| 1845 | |
| 1846 | dc.SetFont( m_normalFont ); |
| 1847 | dc.SetPen( m_dottedPen ); |
| 1848 | |
| 1849 | // this is now done dynamically |
| 1850 | //if(GetImageList() == NULL) |
| 1851 | // m_lineHeight = (int)(dc.GetCharHeight() + 4); |
| 1852 | |
| 1853 | int y = 2; |
| 1854 | PaintLevel( m_anchor, dc, 0, y ); |
| 1855 | } |
| 1856 | |
| 1857 | void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) ) |
| 1858 | { |
| 1859 | m_hasFocus = TRUE; |
| 1860 | |
| 1861 | if (m_current) RefreshLine( m_current ); |
| 1862 | } |
| 1863 | |
| 1864 | void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) ) |
| 1865 | { |
| 1866 | m_hasFocus = FALSE; |
| 1867 | |
| 1868 | if (m_current) RefreshLine( m_current ); |
| 1869 | } |
| 1870 | |
| 1871 | void wxTreeCtrl::OnChar( wxKeyEvent &event ) |
| 1872 | { |
| 1873 | wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() ); |
| 1874 | te.m_code = (int)event.KeyCode(); |
| 1875 | te.SetEventObject( this ); |
| 1876 | GetEventHandler()->ProcessEvent( te ); |
| 1877 | |
| 1878 | if ( (m_current == 0) || (m_key_current == 0) ) |
| 1879 | { |
| 1880 | event.Skip(); |
| 1881 | return; |
| 1882 | } |
| 1883 | |
| 1884 | // how should the selection work for this event? |
| 1885 | bool is_multiple, extended_select, unselect_others; |
| 1886 | EventFlagsToSelType(GetWindowStyleFlag(), |
| 1887 | event.ShiftDown(), |
| 1888 | event.ControlDown(), |
| 1889 | is_multiple, extended_select, unselect_others); |
| 1890 | |
| 1891 | // + : Expand |
| 1892 | // - : Collaspe |
| 1893 | // * : Expand all/Collapse all |
| 1894 | // ' ' | return : activate |
| 1895 | // up : go up (not last children!) |
| 1896 | // down : go down |
| 1897 | // left : go to parent |
| 1898 | // right : open if parent and go next |
| 1899 | // home : go to root |
| 1900 | // end : go to last item without opening parents |
| 1901 | switch (event.KeyCode()) |
| 1902 | { |
| 1903 | case '+': |
| 1904 | case WXK_ADD: |
| 1905 | if (m_current->HasPlus() && !IsExpanded(m_current)) |
| 1906 | { |
| 1907 | Expand(m_current); |
| 1908 | } |
| 1909 | break; |
| 1910 | |
| 1911 | case '*': |
| 1912 | case WXK_MULTIPLY: |
| 1913 | if ( !IsExpanded(m_current) ) |
| 1914 | { |
| 1915 | // expand all |
| 1916 | ExpandAll(m_current); |
| 1917 | break; |
| 1918 | } |
| 1919 | //else: fall through to Collapse() it |
| 1920 | |
| 1921 | case '-': |
| 1922 | case WXK_SUBTRACT: |
| 1923 | if (IsExpanded(m_current)) |
| 1924 | { |
| 1925 | Collapse(m_current); |
| 1926 | } |
| 1927 | break; |
| 1928 | |
| 1929 | case ' ': |
| 1930 | case WXK_RETURN: |
| 1931 | { |
| 1932 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() ); |
| 1933 | event.m_item = m_current; |
| 1934 | event.m_code = 0; |
| 1935 | event.SetEventObject( this ); |
| 1936 | GetEventHandler()->ProcessEvent( event ); |
| 1937 | } |
| 1938 | break; |
| 1939 | |
| 1940 | // up goes to the previous sibling or to the last of its children if |
| 1941 | // it's expanded |
| 1942 | case WXK_UP: |
| 1943 | { |
| 1944 | wxTreeItemId prev = GetPrevSibling( m_key_current ); |
| 1945 | if (!prev) |
| 1946 | { |
| 1947 | prev = GetParent( m_key_current ); |
| 1948 | if (prev) |
| 1949 | { |
| 1950 | long cockie = 0; |
| 1951 | wxTreeItemId current = m_key_current; |
| 1952 | if (current == GetFirstChild( prev, cockie )) |
| 1953 | { |
| 1954 | // otherwise we return to where we came from |
| 1955 | SelectItem( prev, unselect_others, extended_select ); |
| 1956 | m_key_current=prev.m_pItem; |
| 1957 | EnsureVisible( prev ); |
| 1958 | break; |
| 1959 | } |
| 1960 | } |
| 1961 | } |
| 1962 | if (prev) |
| 1963 | { |
| 1964 | while ( IsExpanded(prev) && HasChildren(prev) ) |
| 1965 | { |
| 1966 | wxTreeItemId child = GetLastChild(prev); |
| 1967 | if ( child ) |
| 1968 | { |
| 1969 | prev = child; |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | SelectItem( prev, unselect_others, extended_select ); |
| 1974 | m_key_current=prev.m_pItem; |
| 1975 | EnsureVisible( prev ); |
| 1976 | } |
| 1977 | } |
| 1978 | break; |
| 1979 | |
| 1980 | // left arrow goes to the parent |
| 1981 | case WXK_LEFT: |
| 1982 | { |
| 1983 | wxTreeItemId prev = GetParent( m_current ); |
| 1984 | if (prev) |
| 1985 | { |
| 1986 | EnsureVisible( prev ); |
| 1987 | SelectItem( prev, unselect_others, extended_select ); |
| 1988 | } |
| 1989 | } |
| 1990 | break; |
| 1991 | |
| 1992 | case WXK_RIGHT: |
| 1993 | // this works the same as the down arrow except that we also expand the |
| 1994 | // item if it wasn't expanded yet |
| 1995 | Expand(m_current); |
| 1996 | // fall through |
| 1997 | |
| 1998 | case WXK_DOWN: |
| 1999 | { |
| 2000 | if (IsExpanded(m_key_current) && HasChildren(m_key_current)) |
| 2001 | { |
| 2002 | long cookie = 0; |
| 2003 | wxTreeItemId child = GetFirstChild( m_key_current, cookie ); |
| 2004 | SelectItem( child, unselect_others, extended_select ); |
| 2005 | m_key_current=child.m_pItem; |
| 2006 | EnsureVisible( child ); |
| 2007 | } |
| 2008 | else |
| 2009 | { |
| 2010 | wxTreeItemId next = GetNextSibling( m_key_current ); |
| 2011 | if (!next) |
| 2012 | { |
| 2013 | wxTreeItemId current = m_key_current; |
| 2014 | while (current && !next) |
| 2015 | { |
| 2016 | current = GetParent( current ); |
| 2017 | if (current) next = GetNextSibling( current ); |
| 2018 | } |
| 2019 | } |
| 2020 | if (next) |
| 2021 | { |
| 2022 | SelectItem( next, unselect_others, extended_select ); |
| 2023 | m_key_current=next.m_pItem; |
| 2024 | EnsureVisible( next ); |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | break; |
| 2029 | |
| 2030 | // <End> selects the last visible tree item |
| 2031 | case WXK_END: |
| 2032 | { |
| 2033 | wxTreeItemId last = GetRootItem(); |
| 2034 | |
| 2035 | while ( last.IsOk() && IsExpanded(last) ) |
| 2036 | { |
| 2037 | wxTreeItemId lastChild = GetLastChild(last); |
| 2038 | |
| 2039 | // it may happen if the item was expanded but then all of |
| 2040 | // its children have been deleted - so IsExpanded() returned |
| 2041 | // TRUE, but GetLastChild() returned invalid item |
| 2042 | if ( !lastChild ) |
| 2043 | break; |
| 2044 | |
| 2045 | last = lastChild; |
| 2046 | } |
| 2047 | |
| 2048 | if ( last.IsOk() ) |
| 2049 | { |
| 2050 | EnsureVisible( last ); |
| 2051 | SelectItem( last, unselect_others, extended_select ); |
| 2052 | } |
| 2053 | } |
| 2054 | break; |
| 2055 | |
| 2056 | // <Home> selects the root item |
| 2057 | case WXK_HOME: |
| 2058 | { |
| 2059 | wxTreeItemId prev = GetRootItem(); |
| 2060 | if (prev) |
| 2061 | { |
| 2062 | EnsureVisible( prev ); |
| 2063 | SelectItem( prev, unselect_others, extended_select ); |
| 2064 | } |
| 2065 | } |
| 2066 | break; |
| 2067 | |
| 2068 | default: |
| 2069 | event.Skip(); |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags) |
| 2074 | { |
| 2075 | // We have to call this here because the label in |
| 2076 | // question might just have been added and no screen |
| 2077 | // update taken place. |
| 2078 | if (m_dirty) wxYield(); |
| 2079 | |
| 2080 | wxClientDC dc(this); |
| 2081 | PrepareDC(dc); |
| 2082 | wxCoord x = dc.DeviceToLogicalX( point.x ); |
| 2083 | wxCoord y = dc.DeviceToLogicalY( point.y ); |
| 2084 | int w, h; |
| 2085 | GetSize(&w, &h); |
| 2086 | |
| 2087 | flags=0; |
| 2088 | if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT; |
| 2089 | if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT; |
| 2090 | if (point.y<0) flags|=wxTREE_HITTEST_ABOVE; |
| 2091 | if (point.y>h) flags|=wxTREE_HITTEST_BELOW; |
| 2092 | |
| 2093 | return m_anchor->HitTest( wxPoint(x, y), this, flags); |
| 2094 | } |
| 2095 | |
| 2096 | /* **** */ |
| 2097 | |
| 2098 | void wxTreeCtrl::Edit( const wxTreeItemId& item ) |
| 2099 | { |
| 2100 | if (!item.IsOk()) return; |
| 2101 | |
| 2102 | m_currentEdit = item.m_pItem; |
| 2103 | |
| 2104 | wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() ); |
| 2105 | te.m_item = m_currentEdit; |
| 2106 | te.SetEventObject( this ); |
| 2107 | GetEventHandler()->ProcessEvent( te ); |
| 2108 | |
| 2109 | if (!te.IsAllowed()) return; |
| 2110 | |
| 2111 | // We have to call this here because the label in |
| 2112 | // question might just have been added and no screen |
| 2113 | // update taken place. |
| 2114 | if (m_dirty) wxYield(); |
| 2115 | |
| 2116 | wxString s = m_currentEdit->GetText(); |
| 2117 | int x = m_currentEdit->GetX(); |
| 2118 | int y = m_currentEdit->GetY(); |
| 2119 | int w = m_currentEdit->GetWidth(); |
| 2120 | int h = m_currentEdit->GetHeight(); |
| 2121 | |
| 2122 | int image_h = 0; |
| 2123 | int image_w = 0; |
| 2124 | |
| 2125 | int image = m_currentEdit->GetCurrentImage(); |
| 2126 | if ( image != NO_IMAGE ) |
| 2127 | { |
| 2128 | if ( m_imageListNormal ) |
| 2129 | { |
| 2130 | m_imageListNormal->GetSize( image, image_w, image_h ); |
| 2131 | image_w += 4; |
| 2132 | } |
| 2133 | else |
| 2134 | { |
| 2135 | wxFAIL_MSG(_T("you must create an image list to use images!")); |
| 2136 | } |
| 2137 | } |
| 2138 | x += image_w; |
| 2139 | w -= image_w + 4; // I don't know why +4 is needed |
| 2140 | |
| 2141 | wxClientDC dc(this); |
| 2142 | PrepareDC( dc ); |
| 2143 | x = dc.LogicalToDeviceX( x ); |
| 2144 | y = dc.LogicalToDeviceY( y ); |
| 2145 | |
| 2146 | wxTreeTextCtrl *text = new wxTreeTextCtrl( |
| 2147 | this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) ); |
| 2148 | text->SetFocus(); |
| 2149 | } |
| 2150 | |
| 2151 | void wxTreeCtrl::OnRenameTimer() |
| 2152 | { |
| 2153 | Edit( m_current ); |
| 2154 | } |
| 2155 | |
| 2156 | void wxTreeCtrl::OnRenameAccept() |
| 2157 | { |
| 2158 | wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() ); |
| 2159 | le.m_item = m_currentEdit; |
| 2160 | le.SetEventObject( this ); |
| 2161 | le.m_label = m_renameRes; |
| 2162 | GetEventHandler()->ProcessEvent( le ); |
| 2163 | |
| 2164 | if (!le.IsAllowed()) return; |
| 2165 | |
| 2166 | SetItemText( m_currentEdit, m_renameRes ); |
| 2167 | } |
| 2168 | |
| 2169 | void wxTreeCtrl::OnMouse( wxMouseEvent &event ) |
| 2170 | { |
| 2171 | if ( !m_anchor ) return; |
| 2172 | |
| 2173 | // we process left mouse up event (enables in-place edit), right down |
| 2174 | // (pass to the user code), left dbl click (activate item) and |
| 2175 | // dragging/moving events for items drag-and-drop |
| 2176 | if ( !(event.LeftDown() || |
| 2177 | event.LeftUp() || |
| 2178 | event.RightDown() || |
| 2179 | event.LeftDClick() || |
| 2180 | event.Dragging() || |
| 2181 | ((event.Moving() || event.RightUp()) && m_isDragging)) ) |
| 2182 | { |
| 2183 | event.Skip(); |
| 2184 | |
| 2185 | return; |
| 2186 | } |
| 2187 | |
| 2188 | wxClientDC dc(this); |
| 2189 | PrepareDC(dc); |
| 2190 | wxCoord x = dc.DeviceToLogicalX( event.GetX() ); |
| 2191 | wxCoord y = dc.DeviceToLogicalY( event.GetY() ); |
| 2192 | |
| 2193 | int flags = 0; |
| 2194 | wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags); |
| 2195 | |
| 2196 | if ( event.Dragging() && !m_isDragging ) |
| 2197 | { |
| 2198 | if (m_dragCount == 0) |
| 2199 | m_dragStart = wxPoint(x,y); |
| 2200 | |
| 2201 | m_dragCount++; |
| 2202 | |
| 2203 | if (m_dragCount != 3) |
| 2204 | { |
| 2205 | // wait until user drags a bit further... |
| 2206 | return; |
| 2207 | } |
| 2208 | |
| 2209 | wxEventType command = event.RightIsDown() |
| 2210 | ? wxEVT_COMMAND_TREE_BEGIN_RDRAG |
| 2211 | : wxEVT_COMMAND_TREE_BEGIN_DRAG; |
| 2212 | |
| 2213 | wxTreeEvent nevent( command, GetId() ); |
| 2214 | nevent.m_item = m_current; |
| 2215 | nevent.SetEventObject(this); |
| 2216 | |
| 2217 | // by default the dragging is not supported, the user code must |
| 2218 | // explicitly allow the event for it to take place |
| 2219 | nevent.Veto(); |
| 2220 | |
| 2221 | if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() ) |
| 2222 | { |
| 2223 | // we're going to drag this item |
| 2224 | m_isDragging = TRUE; |
| 2225 | |
| 2226 | // remember the old cursor because we will change it while |
| 2227 | // dragging |
| 2228 | m_oldCursor = m_cursor; |
| 2229 | |
| 2230 | // in a single selection control, hide the selection temporarily |
| 2231 | if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) ) |
| 2232 | { |
| 2233 | m_oldSelection = GetSelection().m_pItem; |
| 2234 | |
| 2235 | if ( m_oldSelection ) |
| 2236 | { |
| 2237 | m_oldSelection->SetHilight(FALSE); |
| 2238 | RefreshLine(m_oldSelection); |
| 2239 | } |
| 2240 | } |
| 2241 | |
| 2242 | CaptureMouse(); |
| 2243 | } |
| 2244 | } |
| 2245 | else if ( event.Moving() ) |
| 2246 | { |
| 2247 | if ( item != m_dropTarget ) |
| 2248 | { |
| 2249 | // unhighlight the previous drop target |
| 2250 | DrawDropEffect(m_dropTarget); |
| 2251 | |
| 2252 | m_dropTarget = item; |
| 2253 | |
| 2254 | // highlight the current drop target if any |
| 2255 | DrawDropEffect(m_dropTarget); |
| 2256 | |
| 2257 | wxYield(); |
| 2258 | } |
| 2259 | } |
| 2260 | else if ( (event.LeftUp() || event.RightUp()) && m_isDragging ) |
| 2261 | { |
| 2262 | // erase the highlighting |
| 2263 | DrawDropEffect(m_dropTarget); |
| 2264 | |
| 2265 | // generate the drag end event |
| 2266 | wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId()); |
| 2267 | |
| 2268 | event.m_item = item; |
| 2269 | event.m_pointDrag = wxPoint(x, y); |
| 2270 | event.SetEventObject(this); |
| 2271 | |
| 2272 | (void)GetEventHandler()->ProcessEvent(event); |
| 2273 | |
| 2274 | m_isDragging = FALSE; |
| 2275 | m_dropTarget = (wxGenericTreeItem *)NULL; |
| 2276 | |
| 2277 | if ( m_oldSelection ) |
| 2278 | { |
| 2279 | m_oldSelection->SetHilight(TRUE); |
| 2280 | RefreshLine(m_oldSelection); |
| 2281 | m_oldSelection = (wxGenericTreeItem *)NULL; |
| 2282 | } |
| 2283 | |
| 2284 | ReleaseMouse(); |
| 2285 | |
| 2286 | SetCursor(m_oldCursor); |
| 2287 | |
| 2288 | wxYield(); |
| 2289 | } |
| 2290 | else |
| 2291 | { |
| 2292 | // here we process only the messages which happen on tree items |
| 2293 | |
| 2294 | m_dragCount = 0; |
| 2295 | |
| 2296 | if (item == NULL) return; /* we hit the blank area */ |
| 2297 | |
| 2298 | if ( event.RightDown() ) |
| 2299 | { |
| 2300 | wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId()); |
| 2301 | nevent.m_item = item; |
| 2302 | nevent.m_code = 0; |
| 2303 | nevent.SetEventObject(this); |
| 2304 | GetEventHandler()->ProcessEvent(nevent); |
| 2305 | } |
| 2306 | else if ( event.LeftUp() && m_lastOnSame ) |
| 2307 | { |
| 2308 | if ( (item == m_current) && |
| 2309 | (flags & wxTREE_HITTEST_ONITEMLABEL) && |
| 2310 | HasFlag(wxTR_EDIT_LABELS) ) |
| 2311 | { |
| 2312 | m_renameTimer->Start( 100, TRUE ); |
| 2313 | } |
| 2314 | |
| 2315 | m_lastOnSame = FALSE; |
| 2316 | } |
| 2317 | else |
| 2318 | { |
| 2319 | if ( event.LeftDown() ) |
| 2320 | { |
| 2321 | m_lastOnSame = item == m_current; |
| 2322 | } |
| 2323 | |
| 2324 | // how should the selection work for this event? |
| 2325 | bool is_multiple, extended_select, unselect_others; |
| 2326 | EventFlagsToSelType(GetWindowStyleFlag(), |
| 2327 | event.ShiftDown(), |
| 2328 | event.ControlDown(), |
| 2329 | is_multiple, extended_select, unselect_others); |
| 2330 | |
| 2331 | if ( (flags & wxTREE_HITTEST_ONITEMBUTTON) && event.LeftDown() ) |
| 2332 | { |
| 2333 | Toggle( item ); |
| 2334 | if ( is_multiple ) |
| 2335 | return; |
| 2336 | } |
| 2337 | |
| 2338 | SelectItem(item, unselect_others, extended_select); |
| 2339 | |
| 2340 | if ( event.LeftDClick() ) |
| 2341 | { |
| 2342 | // double clicking should not start editing the item label |
| 2343 | m_renameTimer->Stop(); |
| 2344 | m_lastOnSame = FALSE; |
| 2345 | |
| 2346 | wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() ); |
| 2347 | nevent.m_item = item; |
| 2348 | nevent.m_code = 0; |
| 2349 | nevent.SetEventObject( this ); |
| 2350 | GetEventHandler()->ProcessEvent( nevent ); |
| 2351 | } |
| 2352 | } |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) ) |
| 2357 | { |
| 2358 | /* after all changes have been done to the tree control, |
| 2359 | * we actually redraw the tree when everything is over */ |
| 2360 | |
| 2361 | if (!m_dirty) |
| 2362 | return; |
| 2363 | |
| 2364 | m_dirty = FALSE; |
| 2365 | |
| 2366 | CalculatePositions(); |
| 2367 | Refresh(); |
| 2368 | AdjustMyScrollbars(); |
| 2369 | } |
| 2370 | |
| 2371 | void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc ) |
| 2372 | { |
| 2373 | wxCoord text_w = 0; |
| 2374 | wxCoord text_h = 0; |
| 2375 | |
| 2376 | if (item->IsBold()) |
| 2377 | dc.SetFont(m_boldFont); |
| 2378 | |
| 2379 | dc.GetTextExtent( item->GetText(), &text_w, &text_h ); |
| 2380 | text_h+=2; |
| 2381 | |
| 2382 | // restore normal font |
| 2383 | dc.SetFont( m_normalFont ); |
| 2384 | |
| 2385 | int image_h = 0; |
| 2386 | int image_w = 0; |
| 2387 | int image = item->GetCurrentImage(); |
| 2388 | if ( image != NO_IMAGE ) |
| 2389 | { |
| 2390 | if ( m_imageListNormal ) |
| 2391 | { |
| 2392 | m_imageListNormal->GetSize( image, image_w, image_h ); |
| 2393 | image_w += 4; |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | int total_h = (image_h > text_h) ? image_h : text_h; |
| 2398 | |
| 2399 | if (total_h < 40) |
| 2400 | total_h += 2; // at least 2 pixels |
| 2401 | else |
| 2402 | total_h += total_h/10; // otherwise 10% extra spacing |
| 2403 | |
| 2404 | item->SetHeight(total_h); |
| 2405 | if (total_h>m_lineHeight) |
| 2406 | m_lineHeight=total_h; |
| 2407 | |
| 2408 | item->SetWidth(image_w+text_w+2); |
| 2409 | } |
| 2410 | |
| 2411 | // ----------------------------------------------------------------------------- |
| 2412 | // for developper : y is now the top of the level |
| 2413 | // not the middle of it ! |
| 2414 | void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ) |
| 2415 | { |
| 2416 | int horizX = level*m_indent; |
| 2417 | |
| 2418 | CalculateSize( item, dc ); |
| 2419 | |
| 2420 | // set its position |
| 2421 | item->SetX( horizX+m_indent+m_spacing ); |
| 2422 | item->SetY( y ); |
| 2423 | y+=GetLineHeight(item); |
| 2424 | |
| 2425 | if ( !item->IsExpanded() ) |
| 2426 | { |
| 2427 | // we dont need to calculate collapsed branches |
| 2428 | return; |
| 2429 | } |
| 2430 | |
| 2431 | wxArrayGenericTreeItems& children = item->GetChildren(); |
| 2432 | size_t n, count = children.Count(); |
| 2433 | for (n = 0; n < count; ++n ) |
| 2434 | CalculateLevel( children[n], dc, level+1, y ); // recurse |
| 2435 | } |
| 2436 | |
| 2437 | void wxTreeCtrl::CalculatePositions() |
| 2438 | { |
| 2439 | if ( !m_anchor ) return; |
| 2440 | |
| 2441 | wxClientDC dc(this); |
| 2442 | PrepareDC( dc ); |
| 2443 | |
| 2444 | dc.SetFont( m_normalFont ); |
| 2445 | |
| 2446 | dc.SetPen( m_dottedPen ); |
| 2447 | //if(GetImageList() == NULL) |
| 2448 | // m_lineHeight = (int)(dc.GetCharHeight() + 4); |
| 2449 | |
| 2450 | int y = 2; |
| 2451 | CalculateLevel( m_anchor, dc, 0, y ); // start recursion |
| 2452 | } |
| 2453 | |
| 2454 | void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item) |
| 2455 | { |
| 2456 | if (m_dirty) return; |
| 2457 | |
| 2458 | wxClientDC dc(this); |
| 2459 | PrepareDC(dc); |
| 2460 | |
| 2461 | int cw = 0; |
| 2462 | int ch = 0; |
| 2463 | GetClientSize( &cw, &ch ); |
| 2464 | |
| 2465 | wxRect rect; |
| 2466 | rect.x = dc.LogicalToDeviceX( 0 ); |
| 2467 | rect.width = cw; |
| 2468 | rect.y = dc.LogicalToDeviceY( item->GetY() ); |
| 2469 | rect.height = ch; |
| 2470 | |
| 2471 | Refresh( TRUE, &rect ); |
| 2472 | |
| 2473 | AdjustMyScrollbars(); |
| 2474 | } |
| 2475 | |
| 2476 | void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item ) |
| 2477 | { |
| 2478 | if (m_dirty) return; |
| 2479 | |
| 2480 | wxClientDC dc(this); |
| 2481 | PrepareDC( dc ); |
| 2482 | |
| 2483 | int cw = 0; |
| 2484 | int ch = 0; |
| 2485 | GetClientSize( &cw, &ch ); |
| 2486 | |
| 2487 | wxRect rect; |
| 2488 | rect.x = dc.LogicalToDeviceX( 0 ); |
| 2489 | rect.y = dc.LogicalToDeviceY( item->GetY() ); |
| 2490 | rect.width = cw; |
| 2491 | rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6; |
| 2492 | |
| 2493 | Refresh( TRUE, &rect ); |
| 2494 | } |
| 2495 | |