| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/generic/datavgen.cpp |
| 3 | // Purpose: wxDataViewCtrl generic implementation |
| 4 | // Author: Robert Roebling |
| 5 | // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang |
| 6 | // Id: $Id$ |
| 7 | // Copyright: (c) 1998 Robert Roebling |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | // For compilers that support precompilation, includes "wx.h". |
| 12 | #include "wx/wxprec.h" |
| 13 | |
| 14 | #ifdef __BORLANDC__ |
| 15 | #pragma hdrstop |
| 16 | #endif |
| 17 | |
| 18 | #if wxUSE_DATAVIEWCTRL |
| 19 | |
| 20 | #include "wx/dataview.h" |
| 21 | |
| 22 | #ifdef wxUSE_GENERICDATAVIEWCTRL |
| 23 | |
| 24 | #ifndef WX_PRECOMP |
| 25 | #ifdef __WXMSW__ |
| 26 | #include "wx/msw/private.h" |
| 27 | #include "wx/msw/wrapwin.h" |
| 28 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
| 29 | #endif |
| 30 | #include "wx/sizer.h" |
| 31 | #include "wx/log.h" |
| 32 | #include "wx/dcclient.h" |
| 33 | #include "wx/timer.h" |
| 34 | #include "wx/settings.h" |
| 35 | #include "wx/msgdlg.h" |
| 36 | #include "wx/dcscreen.h" |
| 37 | #include "wx/frame.h" |
| 38 | #endif |
| 39 | |
| 40 | #include "wx/stockitem.h" |
| 41 | #include "wx/calctrl.h" |
| 42 | #include "wx/popupwin.h" |
| 43 | #include "wx/renderer.h" |
| 44 | #include "wx/dcbuffer.h" |
| 45 | #include "wx/icon.h" |
| 46 | #include "wx/list.h" |
| 47 | #include "wx/listimpl.cpp" |
| 48 | #include "wx/imaglist.h" |
| 49 | #include "wx/headerctrl.h" |
| 50 | #include "wx/dnd.h" |
| 51 | |
| 52 | //----------------------------------------------------------------------------- |
| 53 | // classes |
| 54 | //----------------------------------------------------------------------------- |
| 55 | |
| 56 | class wxDataViewCtrl; |
| 57 | |
| 58 | static const int SCROLL_UNIT_X = 15; |
| 59 | |
| 60 | // the cell padding on the left/right |
| 61 | static const int PADDING_RIGHTLEFT = 3; |
| 62 | |
| 63 | // the expander space margin |
| 64 | static const int EXPANDER_MARGIN = 4; |
| 65 | |
| 66 | #ifdef __WXMSW__ |
| 67 | static const int EXPANDER_OFFSET = 4; |
| 68 | #else |
| 69 | static const int EXPANDER_OFFSET = 1; |
| 70 | #endif |
| 71 | |
| 72 | // Below is the compare stuff. |
| 73 | // For the generic implementation, both the leaf nodes and the nodes are sorted for |
| 74 | // fast search when needed |
| 75 | static wxDataViewModel* g_model; |
| 76 | static int g_column = -2; |
| 77 | static bool g_asending = true; |
| 78 | |
| 79 | //----------------------------------------------------------------------------- |
| 80 | // wxDataViewHeaderWindow |
| 81 | //----------------------------------------------------------------------------- |
| 82 | |
| 83 | class wxDataViewHeaderWindow : public wxHeaderCtrl |
| 84 | { |
| 85 | public: |
| 86 | wxDataViewHeaderWindow(wxDataViewCtrl *parent) |
| 87 | : wxHeaderCtrl(parent) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | wxDataViewCtrl *GetOwner() const |
| 92 | { return static_cast<wxDataViewCtrl *>(GetParent()); } |
| 93 | |
| 94 | protected: |
| 95 | // implement/override wxHeaderCtrl functions by forwarding them to the main |
| 96 | // control |
| 97 | virtual const wxHeaderColumn& GetColumn(unsigned int idx) const |
| 98 | { |
| 99 | return *(GetOwner()->GetColumn(idx)); |
| 100 | } |
| 101 | |
| 102 | // FIXME: currently unused |
| 103 | virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle) |
| 104 | { |
| 105 | wxDataViewCtrl * const owner = GetOwner(); |
| 106 | |
| 107 | int widthContents = owner->GetBestColumnWidth(idx); |
| 108 | owner->GetColumn(idx)->SetWidth(wxMax(widthTitle, widthContents)); |
| 109 | owner->OnColumnChange(idx); |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | bool SendEvent(wxEventType type, unsigned int n) |
| 116 | { |
| 117 | wxDataViewCtrl * const owner = GetOwner(); |
| 118 | wxDataViewEvent event(type, owner->GetId()); |
| 119 | |
| 120 | event.SetEventObject(owner); |
| 121 | event.SetColumn(n); |
| 122 | event.SetDataViewColumn(owner->GetColumn(n)); |
| 123 | event.SetModel(owner->GetModel()); |
| 124 | |
| 125 | // for events created by wxDataViewHeaderWindow the |
| 126 | // row / value fields are not valid |
| 127 | return owner->GetEventHandler()->ProcessEvent(event); |
| 128 | } |
| 129 | |
| 130 | void OnClick(wxHeaderCtrlEvent& event) |
| 131 | { |
| 132 | const unsigned idx = event.GetColumn(); |
| 133 | |
| 134 | if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, idx) ) |
| 135 | return; |
| 136 | |
| 137 | // default handling for the column click is to sort by this column or |
| 138 | // toggle its sort order |
| 139 | wxDataViewCtrl * const owner = GetOwner(); |
| 140 | wxDataViewColumn * const col = owner->GetColumn(idx); |
| 141 | if ( !col->IsSortable() ) |
| 142 | { |
| 143 | // no default handling for non-sortable columns |
| 144 | event.Skip(); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | if ( col->IsSortKey() ) |
| 149 | { |
| 150 | // already using this column for sorting, just change the order |
| 151 | col->ToggleSortOrder(); |
| 152 | } |
| 153 | else // not using this column for sorting yet |
| 154 | { |
| 155 | // first unset the old sort column if any |
| 156 | int oldSortKey = owner->GetSortingColumnIndex(); |
| 157 | if ( oldSortKey != wxNOT_FOUND ) |
| 158 | { |
| 159 | owner->GetColumn(oldSortKey)->UnsetAsSortKey(); |
| 160 | owner->OnColumnChange(oldSortKey); |
| 161 | } |
| 162 | |
| 163 | owner->SetSortingColumnIndex(idx); |
| 164 | col->SetAsSortKey(); |
| 165 | } |
| 166 | |
| 167 | wxDataViewModel * const model = owner->GetModel(); |
| 168 | if ( model ) |
| 169 | model->Resort(); |
| 170 | |
| 171 | owner->OnColumnChange(idx); |
| 172 | } |
| 173 | |
| 174 | void OnRClick(wxHeaderCtrlEvent& event) |
| 175 | { |
| 176 | if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, |
| 177 | event.GetColumn()) ) |
| 178 | event.Skip(); |
| 179 | } |
| 180 | |
| 181 | void OnResize(wxHeaderCtrlEvent& event) |
| 182 | { |
| 183 | wxDataViewCtrl * const owner = GetOwner(); |
| 184 | |
| 185 | const unsigned col = event.GetColumn(); |
| 186 | owner->GetColumn(col)->SetWidth(event.GetWidth()); |
| 187 | GetOwner()->OnColumnChange(col); |
| 188 | } |
| 189 | |
| 190 | void OnEndReorder(wxHeaderCtrlEvent& event) |
| 191 | { |
| 192 | wxDataViewCtrl * const owner = GetOwner(); |
| 193 | owner->ColumnMoved(owner->GetColumn(event.GetColumn()), |
| 194 | event.GetNewOrder()); |
| 195 | } |
| 196 | |
| 197 | DECLARE_EVENT_TABLE() |
| 198 | wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow); |
| 199 | }; |
| 200 | |
| 201 | BEGIN_EVENT_TABLE(wxDataViewHeaderWindow, wxHeaderCtrl) |
| 202 | EVT_HEADER_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnClick) |
| 203 | EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnRClick) |
| 204 | |
| 205 | EVT_HEADER_RESIZING(wxID_ANY, wxDataViewHeaderWindow::OnResize) |
| 206 | EVT_HEADER_END_RESIZE(wxID_ANY, wxDataViewHeaderWindow::OnResize) |
| 207 | |
| 208 | EVT_HEADER_END_REORDER(wxID_ANY, wxDataViewHeaderWindow::OnEndReorder) |
| 209 | END_EVENT_TABLE() |
| 210 | |
| 211 | //----------------------------------------------------------------------------- |
| 212 | // wxDataViewRenameTimer |
| 213 | //----------------------------------------------------------------------------- |
| 214 | |
| 215 | class wxDataViewRenameTimer: public wxTimer |
| 216 | { |
| 217 | private: |
| 218 | wxDataViewMainWindow *m_owner; |
| 219 | |
| 220 | public: |
| 221 | wxDataViewRenameTimer( wxDataViewMainWindow *owner ); |
| 222 | void Notify(); |
| 223 | }; |
| 224 | |
| 225 | //----------------------------------------------------------------------------- |
| 226 | // wxDataViewTreeNode |
| 227 | //----------------------------------------------------------------------------- |
| 228 | |
| 229 | class wxDataViewTreeNode; |
| 230 | WX_DEFINE_ARRAY( wxDataViewTreeNode *, wxDataViewTreeNodes ); |
| 231 | WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves); |
| 232 | |
| 233 | int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1, |
| 234 | wxDataViewTreeNode ** node2); |
| 235 | int LINKAGEMODE wxGenericTreeModelItemCmp( void ** id1, void ** id2); |
| 236 | |
| 237 | class wxDataViewTreeNode |
| 238 | { |
| 239 | public: |
| 240 | wxDataViewTreeNode( wxDataViewTreeNode * parent = NULL ) |
| 241 | { |
| 242 | m_parent = parent; |
| 243 | if (!parent) |
| 244 | m_open = true; |
| 245 | else |
| 246 | m_open = false; |
| 247 | m_hasChildren = false; |
| 248 | m_subTreeCount = 0; |
| 249 | } |
| 250 | |
| 251 | ~wxDataViewTreeNode() |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | wxDataViewTreeNode * GetParent() const { return m_parent; } |
| 256 | void SetParent( wxDataViewTreeNode * parent ) { m_parent = parent; } |
| 257 | wxDataViewTreeNodes & GetNodes() { return m_nodes; } |
| 258 | wxDataViewTreeLeaves & GetChildren() { return m_leaves; } |
| 259 | |
| 260 | void AddNode( wxDataViewTreeNode * node ) |
| 261 | { |
| 262 | m_leaves.Add( node->GetItem().GetID() ); |
| 263 | if (g_column >= -1) |
| 264 | m_leaves.Sort( &wxGenericTreeModelItemCmp ); |
| 265 | m_nodes.Add( node ); |
| 266 | if (g_column >= -1) |
| 267 | m_nodes.Sort( &wxGenericTreeModelNodeCmp ); |
| 268 | } |
| 269 | void AddLeaf( void * leaf ) |
| 270 | { |
| 271 | m_leaves.Add( leaf ); |
| 272 | if (g_column >= -1) |
| 273 | m_leaves.Sort( &wxGenericTreeModelItemCmp ); |
| 274 | } |
| 275 | |
| 276 | wxDataViewItem & GetItem() { return m_item; } |
| 277 | const wxDataViewItem & GetItem() const { return m_item; } |
| 278 | void SetItem( const wxDataViewItem & item ) { m_item = item; } |
| 279 | |
| 280 | unsigned int GetChildrenNumber() const { return m_leaves.GetCount(); } |
| 281 | unsigned int GetNodeNumber() const { return m_nodes.GetCount(); } |
| 282 | int GetIndentLevel() const |
| 283 | { |
| 284 | int ret = 0; |
| 285 | const wxDataViewTreeNode * node = this; |
| 286 | while( node->GetParent()->GetParent() != NULL ) |
| 287 | { |
| 288 | node = node->GetParent(); |
| 289 | ret ++; |
| 290 | } |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | bool IsOpen() const |
| 295 | { |
| 296 | return m_open; |
| 297 | } |
| 298 | |
| 299 | void ToggleOpen() |
| 300 | { |
| 301 | int len = m_nodes.GetCount(); |
| 302 | int sum = 0; |
| 303 | for ( int i = 0;i < len; i ++) |
| 304 | sum += m_nodes[i]->GetSubTreeCount(); |
| 305 | |
| 306 | sum += m_leaves.GetCount(); |
| 307 | if (m_open) |
| 308 | { |
| 309 | ChangeSubTreeCount(-sum); |
| 310 | m_open = !m_open; |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | m_open = !m_open; |
| 315 | ChangeSubTreeCount(sum); |
| 316 | } |
| 317 | } |
| 318 | bool HasChildren() const { return m_hasChildren; } |
| 319 | void SetHasChildren( bool has ){ m_hasChildren = has; } |
| 320 | |
| 321 | void SetSubTreeCount( int num ) { m_subTreeCount = num; } |
| 322 | int GetSubTreeCount() const { return m_subTreeCount; } |
| 323 | void ChangeSubTreeCount( int num ) |
| 324 | { |
| 325 | if( !m_open ) |
| 326 | return; |
| 327 | m_subTreeCount += num; |
| 328 | if( m_parent ) |
| 329 | m_parent->ChangeSubTreeCount(num); |
| 330 | } |
| 331 | |
| 332 | void Resort() |
| 333 | { |
| 334 | if (g_column >= -1) |
| 335 | { |
| 336 | m_nodes.Sort( &wxGenericTreeModelNodeCmp ); |
| 337 | int len = m_nodes.GetCount(); |
| 338 | for (int i = 0; i < len; i ++) |
| 339 | m_nodes[i]->Resort(); |
| 340 | m_leaves.Sort( &wxGenericTreeModelItemCmp ); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | private: |
| 345 | wxDataViewTreeNode *m_parent; |
| 346 | wxDataViewTreeNodes m_nodes; |
| 347 | wxDataViewTreeLeaves m_leaves; |
| 348 | wxDataViewItem m_item; |
| 349 | bool m_open; |
| 350 | bool m_hasChildren; |
| 351 | int m_subTreeCount; |
| 352 | }; |
| 353 | |
| 354 | int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1, |
| 355 | wxDataViewTreeNode ** node2) |
| 356 | { |
| 357 | return g_model->Compare( (*node1)->GetItem(), (*node2)->GetItem(), g_column, g_asending ); |
| 358 | } |
| 359 | |
| 360 | int LINKAGEMODE wxGenericTreeModelItemCmp( void ** id1, void ** id2) |
| 361 | { |
| 362 | return g_model->Compare( *id1, *id2, g_column, g_asending ); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | //----------------------------------------------------------------------------- |
| 367 | // wxDataViewMainWindow |
| 368 | //----------------------------------------------------------------------------- |
| 369 | |
| 370 | WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection, |
| 371 | WXDLLIMPEXP_ADV); |
| 372 | WX_DECLARE_LIST(wxDataViewItem, ItemList); |
| 373 | WX_DEFINE_LIST(ItemList) |
| 374 | |
| 375 | class wxDataViewMainWindow: public wxWindow |
| 376 | { |
| 377 | public: |
| 378 | wxDataViewMainWindow( wxDataViewCtrl *parent, |
| 379 | wxWindowID id, |
| 380 | const wxPoint &pos = wxDefaultPosition, |
| 381 | const wxSize &size = wxDefaultSize, |
| 382 | const wxString &name = wxT("wxdataviewctrlmainwindow") ); |
| 383 | virtual ~wxDataViewMainWindow(); |
| 384 | |
| 385 | bool IsVirtualList() const { return m_root == NULL; } |
| 386 | |
| 387 | // notifications from wxDataViewModel |
| 388 | bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ); |
| 389 | bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ); |
| 390 | bool ItemChanged( const wxDataViewItem &item ); |
| 391 | bool ValueChanged( const wxDataViewItem &item, unsigned int col ); |
| 392 | bool Cleared(); |
| 393 | void Resort() |
| 394 | { |
| 395 | if (!IsVirtualList()) |
| 396 | { |
| 397 | SortPrepare(); |
| 398 | m_root->Resort(); |
| 399 | } |
| 400 | UpdateDisplay(); |
| 401 | } |
| 402 | |
| 403 | void SortPrepare() |
| 404 | { |
| 405 | g_model = GetOwner()->GetModel(); |
| 406 | wxDataViewColumn* col = GetOwner()->GetSortingColumn(); |
| 407 | if( !col ) |
| 408 | { |
| 409 | if (g_model->HasDefaultCompare()) |
| 410 | g_column = -1; |
| 411 | else |
| 412 | g_column = -2; |
| 413 | |
| 414 | g_asending = true; |
| 415 | return; |
| 416 | } |
| 417 | g_column = col->GetModelColumn(); |
| 418 | g_asending = col->IsSortOrderAscending(); |
| 419 | } |
| 420 | |
| 421 | void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; } |
| 422 | wxDataViewCtrl *GetOwner() { return m_owner; } |
| 423 | const wxDataViewCtrl *GetOwner() const { return m_owner; } |
| 424 | |
| 425 | #if wxUSE_DRAG_AND_DROP |
| 426 | wxBitmap CreateItemBitmap( unsigned int row, int &indent ); |
| 427 | #endif // wxUSE_DRAG_AND_DROP |
| 428 | void OnPaint( wxPaintEvent &event ); |
| 429 | void OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event); |
| 430 | void OnChar( wxKeyEvent &event ); |
| 431 | void OnMouse( wxMouseEvent &event ); |
| 432 | void OnSetFocus( wxFocusEvent &event ); |
| 433 | void OnKillFocus( wxFocusEvent &event ); |
| 434 | |
| 435 | void UpdateDisplay(); |
| 436 | void RecalculateDisplay(); |
| 437 | void OnInternalIdle(); |
| 438 | |
| 439 | void OnRenameTimer(); |
| 440 | |
| 441 | void ScrollWindow( int dx, int dy, const wxRect *rect = NULL ); |
| 442 | void ScrollTo( int rows, int column ); |
| 443 | |
| 444 | bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; } |
| 445 | void ChangeCurrentRow( unsigned int row ); |
| 446 | |
| 447 | bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); } |
| 448 | bool IsEmpty() { return GetRowCount() == 0; } |
| 449 | |
| 450 | int GetCountPerPage() const; |
| 451 | int GetEndOfLastCol() const; |
| 452 | unsigned int GetFirstVisibleRow() const; |
| 453 | |
| 454 | // I change this method to un const because in the tree view, |
| 455 | // the displaying number of the tree are changing along with the |
| 456 | // expanding/collapsing of the tree nodes |
| 457 | unsigned int GetLastVisibleRow(); |
| 458 | unsigned int GetRowCount(); |
| 459 | |
| 460 | wxDataViewItem GetSelection() const; |
| 461 | wxDataViewSelection GetSelections(){ return m_selection; } |
| 462 | void SetSelections( const wxDataViewSelection & sel ) |
| 463 | { m_selection = sel; UpdateDisplay(); } |
| 464 | void Select( const wxArrayInt& aSelections ); |
| 465 | void SelectAllRows( bool on ); |
| 466 | void SelectRow( unsigned int row, bool on ); |
| 467 | void SelectRows( unsigned int from, unsigned int to, bool on ); |
| 468 | void ReverseRowSelection( unsigned int row ); |
| 469 | bool IsRowSelected( unsigned int row ); |
| 470 | void SendSelectionChangedEvent( const wxDataViewItem& item); |
| 471 | |
| 472 | void RefreshRow( unsigned int row ); |
| 473 | void RefreshRows( unsigned int from, unsigned int to ); |
| 474 | void RefreshRowsAfter( unsigned int firstRow ); |
| 475 | |
| 476 | // returns the colour to be used for drawing the rules |
| 477 | wxColour GetRuleColour() const |
| 478 | { |
| 479 | return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT); |
| 480 | } |
| 481 | |
| 482 | wxRect GetLineRect( unsigned int row ) const; |
| 483 | |
| 484 | int GetLineStart( unsigned int row ) const; // row * m_lineHeight in fixed mode |
| 485 | int GetLineHeight( unsigned int row ) const; // m_lineHeight in fixed mode |
| 486 | int GetLineAt( unsigned int y ) const; // y / m_lineHeight in fixed mode |
| 487 | |
| 488 | // Some useful functions for row and item mapping |
| 489 | wxDataViewItem GetItemByRow( unsigned int row ) const; |
| 490 | int GetRowByItem( const wxDataViewItem & item ) const; |
| 491 | |
| 492 | // Methods for building the mapping tree |
| 493 | void BuildTree( wxDataViewModel * model ); |
| 494 | void DestroyTree(); |
| 495 | void HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column ); |
| 496 | wxRect GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column ); |
| 497 | |
| 498 | void Expand( unsigned int row ); |
| 499 | void Collapse( unsigned int row ); |
| 500 | bool IsExpanded( unsigned int row ) const; |
| 501 | bool HasChildren( unsigned int row ) const; |
| 502 | |
| 503 | #if wxUSE_DRAG_AND_DROP |
| 504 | bool EnableDragSource( const wxDataFormat &format ); |
| 505 | bool EnableDropTarget( const wxDataFormat &format ); |
| 506 | |
| 507 | void RemoveDropHint(); |
| 508 | wxDragResult OnDragOver( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def ); |
| 509 | bool OnDrop( wxDataFormat format, wxCoord x, wxCoord y ); |
| 510 | wxDragResult OnData( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def ); |
| 511 | void OnLeave(); |
| 512 | #endif // wxUSE_DRAG_AND_DROP |
| 513 | |
| 514 | private: |
| 515 | wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row ) const; |
| 516 | // We did not need this temporarily |
| 517 | // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item ); |
| 518 | |
| 519 | int RecalculateCount(); |
| 520 | |
| 521 | wxDataViewEvent SendExpanderEvent( wxEventType type, const wxDataViewItem & item ); |
| 522 | |
| 523 | wxDataViewTreeNode * FindNode( const wxDataViewItem & item ); |
| 524 | |
| 525 | private: |
| 526 | wxDataViewCtrl *m_owner; |
| 527 | int m_lineHeight; |
| 528 | bool m_dirty; |
| 529 | |
| 530 | wxDataViewColumn *m_currentCol; |
| 531 | unsigned int m_currentRow; |
| 532 | wxDataViewSelection m_selection; |
| 533 | |
| 534 | wxDataViewRenameTimer *m_renameTimer; |
| 535 | bool m_lastOnSame; |
| 536 | |
| 537 | bool m_hasFocus; |
| 538 | |
| 539 | #if wxUSE_DRAG_AND_DROP |
| 540 | int m_dragCount; |
| 541 | wxPoint m_dragStart; |
| 542 | |
| 543 | bool m_dragEnabled; |
| 544 | wxDataFormat m_dragFormat; |
| 545 | |
| 546 | bool m_dropEnabled; |
| 547 | wxDataFormat m_dropFormat; |
| 548 | bool m_dropHint; |
| 549 | unsigned int m_dropHintLine; |
| 550 | #endif // wxUSE_DRAG_AND_DROP |
| 551 | |
| 552 | // for double click logic |
| 553 | unsigned int m_lineLastClicked, |
| 554 | m_lineBeforeLastClicked, |
| 555 | m_lineSelectSingleOnUp; |
| 556 | |
| 557 | // the pen used to draw horiz/vertical rules |
| 558 | wxPen m_penRule; |
| 559 | |
| 560 | // the pen used to draw the expander and the lines |
| 561 | wxPen m_penExpander; |
| 562 | |
| 563 | // This is the tree structure of the model |
| 564 | wxDataViewTreeNode * m_root; |
| 565 | int m_count; |
| 566 | |
| 567 | // This is the tree node under the cursor |
| 568 | wxDataViewTreeNode * m_underMouse; |
| 569 | |
| 570 | private: |
| 571 | DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow) |
| 572 | DECLARE_EVENT_TABLE() |
| 573 | }; |
| 574 | |
| 575 | // --------------------------------------------------------- |
| 576 | // wxGenericDataViewModelNotifier |
| 577 | // --------------------------------------------------------- |
| 578 | |
| 579 | class wxGenericDataViewModelNotifier: public wxDataViewModelNotifier |
| 580 | { |
| 581 | public: |
| 582 | wxGenericDataViewModelNotifier( wxDataViewMainWindow *mainWindow ) |
| 583 | { m_mainWindow = mainWindow; } |
| 584 | |
| 585 | virtual bool ItemAdded( const wxDataViewItem & parent, const wxDataViewItem & item ) |
| 586 | { return m_mainWindow->ItemAdded( parent , item ); } |
| 587 | virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ) |
| 588 | { return m_mainWindow->ItemDeleted( parent, item ); } |
| 589 | virtual bool ItemChanged( const wxDataViewItem & item ) |
| 590 | { return m_mainWindow->ItemChanged(item); } |
| 591 | virtual bool ValueChanged( const wxDataViewItem & item , unsigned int col ) |
| 592 | { return m_mainWindow->ValueChanged( item, col ); } |
| 593 | virtual bool Cleared() |
| 594 | { return m_mainWindow->Cleared(); } |
| 595 | virtual void Resort() |
| 596 | { m_mainWindow->Resort(); } |
| 597 | |
| 598 | wxDataViewMainWindow *m_mainWindow; |
| 599 | }; |
| 600 | |
| 601 | // --------------------------------------------------------- |
| 602 | // wxDataViewRenderer |
| 603 | // --------------------------------------------------------- |
| 604 | |
| 605 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase) |
| 606 | |
| 607 | wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, |
| 608 | wxDataViewCellMode mode, |
| 609 | int align) : |
| 610 | wxDataViewRendererBase( varianttype, mode, align ) |
| 611 | { |
| 612 | m_dc = NULL; |
| 613 | m_align = align; |
| 614 | m_mode = mode; |
| 615 | m_wantsAttr = false; |
| 616 | m_hasAttr = false; |
| 617 | } |
| 618 | |
| 619 | wxDataViewRenderer::~wxDataViewRenderer() |
| 620 | { |
| 621 | if (m_dc) |
| 622 | delete m_dc; |
| 623 | } |
| 624 | |
| 625 | wxDC *wxDataViewRenderer::GetDC() |
| 626 | { |
| 627 | if (m_dc == NULL) |
| 628 | { |
| 629 | if (GetOwner() == NULL) |
| 630 | return NULL; |
| 631 | if (GetOwner()->GetOwner() == NULL) |
| 632 | return NULL; |
| 633 | m_dc = new wxClientDC( GetOwner()->GetOwner() ); |
| 634 | } |
| 635 | |
| 636 | return m_dc; |
| 637 | } |
| 638 | |
| 639 | void wxDataViewRenderer::SetAlignment( int align ) |
| 640 | { |
| 641 | m_align=align; |
| 642 | } |
| 643 | |
| 644 | int wxDataViewRenderer::GetAlignment() const |
| 645 | { |
| 646 | return m_align; |
| 647 | } |
| 648 | |
| 649 | int wxDataViewRenderer::CalculateAlignment() const |
| 650 | { |
| 651 | if (m_align == wxDVR_DEFAULT_ALIGNMENT) |
| 652 | { |
| 653 | if (GetOwner() == NULL) |
| 654 | return wxALIGN_LEFT | wxALIGN_CENTRE_VERTICAL; |
| 655 | |
| 656 | return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL; |
| 657 | } |
| 658 | |
| 659 | return m_align; |
| 660 | } |
| 661 | |
| 662 | // --------------------------------------------------------- |
| 663 | // wxDataViewCustomRenderer |
| 664 | // --------------------------------------------------------- |
| 665 | |
| 666 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer) |
| 667 | |
| 668 | wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype, |
| 669 | wxDataViewCellMode mode, int align ) : |
| 670 | wxDataViewRenderer( varianttype, mode, align ) |
| 671 | { |
| 672 | } |
| 673 | |
| 674 | void wxDataViewCustomRenderer::RenderText( const wxString &text, int xoffset, |
| 675 | wxRect cell, wxDC *dc, int state ) |
| 676 | { |
| 677 | wxDataViewCtrl *view = GetOwner()->GetOwner(); |
| 678 | wxColour col = (state & wxDATAVIEW_CELL_SELECTED) ? |
| 679 | wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) : |
| 680 | view->GetForegroundColour(); |
| 681 | dc->SetTextForeground(col); |
| 682 | dc->DrawText( text, |
| 683 | cell.x + xoffset, |
| 684 | cell.y + ((cell.height - dc->GetCharHeight()) / 2)); |
| 685 | } |
| 686 | |
| 687 | // --------------------------------------------------------- |
| 688 | // wxDataViewTextRenderer |
| 689 | // --------------------------------------------------------- |
| 690 | |
| 691 | IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewCustomRenderer) |
| 692 | |
| 693 | wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, |
| 694 | wxDataViewCellMode mode, int align ) : |
| 695 | wxDataViewCustomRenderer( varianttype, mode, align ) |
| 696 | { |
| 697 | } |
| 698 | |
| 699 | bool wxDataViewTextRenderer::SetValue( const wxVariant &value ) |
| 700 | { |
| 701 | m_text = value.GetString(); |
| 702 | |
| 703 | return true; |
| 704 | } |
| 705 | |
| 706 | bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const |
| 707 | { |
| 708 | return false; |
| 709 | } |
| 710 | |
| 711 | bool wxDataViewTextRenderer::HasEditorCtrl() const |
| 712 | { |
| 713 | return true; |
| 714 | } |
| 715 | |
| 716 | wxControl* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent, |
| 717 | wxRect labelRect, const wxVariant &value ) |
| 718 | { |
| 719 | wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, value, |
| 720 | wxPoint(labelRect.x,labelRect.y), |
| 721 | wxSize(labelRect.width,labelRect.height) ); |
| 722 | |
| 723 | // select the text in the control an place the cursor at the end |
| 724 | ctrl->SetInsertionPointEnd(); |
| 725 | ctrl->SelectAll(); |
| 726 | |
| 727 | return ctrl; |
| 728 | } |
| 729 | |
| 730 | bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl *editor, wxVariant &value ) |
| 731 | { |
| 732 | wxTextCtrl *text = (wxTextCtrl*) editor; |
| 733 | value = text->GetValue(); |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | bool wxDataViewTextRenderer::Render( wxRect cell, wxDC *dc, int state ) |
| 738 | { |
| 739 | RenderText( m_text, 0, cell, dc, state ); |
| 740 | return true; |
| 741 | } |
| 742 | |
| 743 | wxSize wxDataViewTextRenderer::GetSize() const |
| 744 | { |
| 745 | const wxDataViewCtrl *view = GetView(); |
| 746 | if (!m_text.empty()) |
| 747 | return view->wxWindowBase::GetTextExtent( m_text ); |
| 748 | return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE); |
| 749 | } |
| 750 | |
| 751 | // --------------------------------------------------------- |
| 752 | // wxDataViewTextRendererAttr |
| 753 | // --------------------------------------------------------- |
| 754 | |
| 755 | IMPLEMENT_CLASS(wxDataViewTextRendererAttr, wxDataViewTextRenderer) |
| 756 | |
| 757 | wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString &varianttype, |
| 758 | wxDataViewCellMode mode, int align ) : |
| 759 | wxDataViewTextRenderer( varianttype, mode, align ) |
| 760 | { |
| 761 | m_wantsAttr = true; |
| 762 | } |
| 763 | |
| 764 | bool wxDataViewTextRendererAttr::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) |
| 765 | { |
| 766 | wxFont font; |
| 767 | wxColour colour; |
| 768 | |
| 769 | if (m_hasAttr) |
| 770 | { |
| 771 | if (m_attr.HasColour()) |
| 772 | { |
| 773 | colour = dc->GetTextForeground(); |
| 774 | dc->SetTextForeground( m_attr.GetColour() ); |
| 775 | } |
| 776 | |
| 777 | if (m_attr.GetBold() || m_attr.GetItalic()) |
| 778 | { |
| 779 | font = dc->GetFont(); |
| 780 | wxFont myfont = font; |
| 781 | if (m_attr.GetBold()) |
| 782 | myfont.SetWeight( wxFONTWEIGHT_BOLD ); |
| 783 | if (m_attr.GetItalic()) |
| 784 | myfont.SetStyle( wxFONTSTYLE_ITALIC ); |
| 785 | dc->SetFont( myfont ); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | dc->DrawText( m_text, cell.x, cell.y + ((cell.height - dc->GetCharHeight()) / 2)); |
| 790 | |
| 791 | // restore dc |
| 792 | if (m_hasAttr) |
| 793 | { |
| 794 | if (m_attr.HasColour()) |
| 795 | dc->SetTextForeground( colour ); |
| 796 | |
| 797 | if (m_attr.GetBold() || m_attr.GetItalic()) |
| 798 | dc->SetFont( font ); |
| 799 | } |
| 800 | |
| 801 | return true; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | // --------------------------------------------------------- |
| 806 | // wxDataViewBitmapRenderer |
| 807 | // --------------------------------------------------------- |
| 808 | |
| 809 | IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewCustomRenderer) |
| 810 | |
| 811 | wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, |
| 812 | wxDataViewCellMode mode, int align ) : |
| 813 | wxDataViewCustomRenderer( varianttype, mode, align ) |
| 814 | { |
| 815 | } |
| 816 | |
| 817 | bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value ) |
| 818 | { |
| 819 | if (value.GetType() == wxT("wxBitmap")) |
| 820 | m_bitmap << value; |
| 821 | if (value.GetType() == wxT("wxIcon")) |
| 822 | m_icon << value; |
| 823 | |
| 824 | return true; |
| 825 | } |
| 826 | |
| 827 | bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) ) const |
| 828 | { |
| 829 | return false; |
| 830 | } |
| 831 | |
| 832 | bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) |
| 833 | { |
| 834 | if (m_bitmap.Ok()) |
| 835 | dc->DrawBitmap( m_bitmap, cell.x, cell.y ); |
| 836 | else if (m_icon.Ok()) |
| 837 | dc->DrawIcon( m_icon, cell.x, cell.y ); |
| 838 | |
| 839 | return true; |
| 840 | } |
| 841 | |
| 842 | wxSize wxDataViewBitmapRenderer::GetSize() const |
| 843 | { |
| 844 | if (m_bitmap.Ok()) |
| 845 | return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() ); |
| 846 | else if (m_icon.Ok()) |
| 847 | return wxSize( m_icon.GetWidth(), m_icon.GetHeight() ); |
| 848 | |
| 849 | return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE); |
| 850 | } |
| 851 | |
| 852 | // --------------------------------------------------------- |
| 853 | // wxDataViewToggleRenderer |
| 854 | // --------------------------------------------------------- |
| 855 | |
| 856 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer, wxDataViewCustomRenderer) |
| 857 | |
| 858 | wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype, |
| 859 | wxDataViewCellMode mode, int align ) : |
| 860 | wxDataViewCustomRenderer( varianttype, mode, align ) |
| 861 | { |
| 862 | m_toggle = false; |
| 863 | } |
| 864 | |
| 865 | bool wxDataViewToggleRenderer::SetValue( const wxVariant &value ) |
| 866 | { |
| 867 | m_toggle = value.GetBool(); |
| 868 | |
| 869 | return true; |
| 870 | } |
| 871 | |
| 872 | bool wxDataViewToggleRenderer::GetValue( wxVariant &WXUNUSED(value) ) const |
| 873 | { |
| 874 | return false; |
| 875 | } |
| 876 | |
| 877 | bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) |
| 878 | { |
| 879 | int flags = 0; |
| 880 | if (m_toggle) |
| 881 | flags |= wxCONTROL_CHECKED; |
| 882 | if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE) |
| 883 | flags |= wxCONTROL_DISABLED; |
| 884 | |
| 885 | wxRendererNative::Get().DrawCheckBox( |
| 886 | GetOwner()->GetOwner(), |
| 887 | *dc, |
| 888 | cell, |
| 889 | flags ); |
| 890 | |
| 891 | return true; |
| 892 | } |
| 893 | |
| 894 | bool wxDataViewToggleRenderer::Activate( wxRect WXUNUSED(cell), |
| 895 | wxDataViewModel *model, |
| 896 | const wxDataViewItem & item, unsigned int col) |
| 897 | { |
| 898 | bool value = !m_toggle; |
| 899 | wxVariant variant = value; |
| 900 | model->SetValue( variant, item, col); |
| 901 | model->ValueChanged( item, col ); |
| 902 | return true; |
| 903 | } |
| 904 | |
| 905 | wxSize wxDataViewToggleRenderer::GetSize() const |
| 906 | { |
| 907 | // the window parameter is not used by GetCheckBoxSize() so it's |
| 908 | // safe to pass NULL |
| 909 | return wxRendererNative::Get().GetCheckBoxSize(NULL); |
| 910 | } |
| 911 | |
| 912 | // --------------------------------------------------------- |
| 913 | // wxDataViewProgressRenderer |
| 914 | // --------------------------------------------------------- |
| 915 | |
| 916 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer) |
| 917 | |
| 918 | wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label, |
| 919 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : |
| 920 | wxDataViewCustomRenderer( varianttype, mode, align ) |
| 921 | { |
| 922 | m_label = label; |
| 923 | m_value = 0; |
| 924 | } |
| 925 | |
| 926 | wxDataViewProgressRenderer::~wxDataViewProgressRenderer() |
| 927 | { |
| 928 | } |
| 929 | |
| 930 | bool wxDataViewProgressRenderer::SetValue( const wxVariant &value ) |
| 931 | { |
| 932 | m_value = (long) value; |
| 933 | |
| 934 | if (m_value < 0) m_value = 0; |
| 935 | if (m_value > 100) m_value = 100; |
| 936 | |
| 937 | return true; |
| 938 | } |
| 939 | |
| 940 | bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const |
| 941 | { |
| 942 | value = (long) m_value; |
| 943 | return true; |
| 944 | } |
| 945 | |
| 946 | bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) |
| 947 | { |
| 948 | double pct = (double)m_value / 100.0; |
| 949 | wxRect bar = cell; |
| 950 | bar.width = (int)(cell.width * pct); |
| 951 | dc->SetPen( *wxTRANSPARENT_PEN ); |
| 952 | dc->SetBrush( *wxBLUE_BRUSH ); |
| 953 | dc->DrawRectangle( bar ); |
| 954 | |
| 955 | dc->SetBrush( *wxTRANSPARENT_BRUSH ); |
| 956 | dc->SetPen( *wxBLACK_PEN ); |
| 957 | dc->DrawRectangle( cell ); |
| 958 | |
| 959 | return true; |
| 960 | } |
| 961 | |
| 962 | wxSize wxDataViewProgressRenderer::GetSize() const |
| 963 | { |
| 964 | return wxSize(40,12); |
| 965 | } |
| 966 | |
| 967 | // --------------------------------------------------------- |
| 968 | // wxDataViewDateRenderer |
| 969 | // --------------------------------------------------------- |
| 970 | |
| 971 | #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN) |
| 972 | |
| 973 | #if wxUSE_DATE_RENDERER_POPUP |
| 974 | |
| 975 | class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow |
| 976 | { |
| 977 | public: |
| 978 | wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value, |
| 979 | wxDataViewModel *model, const wxDataViewItem & item, unsigned int col) : |
| 980 | wxPopupTransientWindow( parent, wxBORDER_SIMPLE ), |
| 981 | m_item( item ) |
| 982 | { |
| 983 | m_model = model; |
| 984 | m_col = col; |
| 985 | m_cal = new wxCalendarCtrl( this, wxID_ANY, *value ); |
| 986 | wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); |
| 987 | sizer->Add( m_cal, 1, wxGROW ); |
| 988 | SetSizer( sizer ); |
| 989 | sizer->Fit( this ); |
| 990 | } |
| 991 | |
| 992 | void OnCalendar( wxCalendarEvent &event ); |
| 993 | |
| 994 | wxCalendarCtrl *m_cal; |
| 995 | wxDataViewModel *m_model; |
| 996 | unsigned int m_col; |
| 997 | const wxDataViewItem & m_item; |
| 998 | |
| 999 | protected: |
| 1000 | virtual void OnDismiss() |
| 1001 | { |
| 1002 | } |
| 1003 | |
| 1004 | private: |
| 1005 | DECLARE_EVENT_TABLE() |
| 1006 | }; |
| 1007 | |
| 1008 | BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow) |
| 1009 | EVT_CALENDAR( wxID_ANY, wxDataViewDateRendererPopupTransient::OnCalendar ) |
| 1010 | END_EVENT_TABLE() |
| 1011 | |
| 1012 | void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event ) |
| 1013 | { |
| 1014 | wxDateTime date = event.GetDate(); |
| 1015 | wxVariant value = date; |
| 1016 | m_model->SetValue( value, m_item, m_col ); |
| 1017 | m_model->ValueChanged( m_item, m_col ); |
| 1018 | DismissAndNotify(); |
| 1019 | } |
| 1020 | |
| 1021 | #endif // wxUSE_DATE_RENDERER_POPUP |
| 1022 | |
| 1023 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer) |
| 1024 | |
| 1025 | wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype, |
| 1026 | wxDataViewCellMode mode, int align ) : |
| 1027 | wxDataViewCustomRenderer( varianttype, mode, align ) |
| 1028 | { |
| 1029 | } |
| 1030 | |
| 1031 | bool wxDataViewDateRenderer::SetValue( const wxVariant &value ) |
| 1032 | { |
| 1033 | m_date = value.GetDateTime(); |
| 1034 | |
| 1035 | return true; |
| 1036 | } |
| 1037 | |
| 1038 | bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const |
| 1039 | { |
| 1040 | value = m_date; |
| 1041 | return true; |
| 1042 | } |
| 1043 | |
| 1044 | bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state ) |
| 1045 | { |
| 1046 | wxString tmp = m_date.FormatDate(); |
| 1047 | RenderText( tmp, 0, cell, dc, state ); |
| 1048 | return true; |
| 1049 | } |
| 1050 | |
| 1051 | wxSize wxDataViewDateRenderer::GetSize() const |
| 1052 | { |
| 1053 | const wxDataViewCtrl* view = GetView(); |
| 1054 | wxString tmp = m_date.FormatDate(); |
| 1055 | wxCoord x,y,d; |
| 1056 | view->GetTextExtent( tmp, &x, &y, &d ); |
| 1057 | return wxSize(x,y+d); |
| 1058 | } |
| 1059 | |
| 1060 | bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model, |
| 1061 | const wxDataViewItem & item, unsigned int col ) |
| 1062 | { |
| 1063 | wxVariant variant; |
| 1064 | model->GetValue( variant, item, col ); |
| 1065 | wxDateTime value = variant.GetDateTime(); |
| 1066 | |
| 1067 | #if wxUSE_DATE_RENDERER_POPUP |
| 1068 | wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient( |
| 1069 | GetOwner()->GetOwner()->GetParent(), &value, model, item, col); |
| 1070 | wxPoint pos = wxGetMousePosition(); |
| 1071 | popup->Move( pos ); |
| 1072 | popup->Layout(); |
| 1073 | popup->Popup( popup->m_cal ); |
| 1074 | #else // !wxUSE_DATE_RENDERER_POPUP |
| 1075 | wxMessageBox(value.Format()); |
| 1076 | #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP |
| 1077 | return true; |
| 1078 | } |
| 1079 | |
| 1080 | // --------------------------------------------------------- |
| 1081 | // wxDataViewIconTextRenderer |
| 1082 | // --------------------------------------------------------- |
| 1083 | |
| 1084 | IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer) |
| 1085 | |
| 1086 | wxDataViewIconTextRenderer::wxDataViewIconTextRenderer( |
| 1087 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : |
| 1088 | wxDataViewCustomRenderer( varianttype, mode, align ) |
| 1089 | { |
| 1090 | SetMode(mode); |
| 1091 | SetAlignment(align); |
| 1092 | } |
| 1093 | |
| 1094 | wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer() |
| 1095 | { |
| 1096 | } |
| 1097 | |
| 1098 | bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value ) |
| 1099 | { |
| 1100 | m_value << value; |
| 1101 | return true; |
| 1102 | } |
| 1103 | |
| 1104 | bool wxDataViewIconTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const |
| 1105 | { |
| 1106 | return false; |
| 1107 | } |
| 1108 | |
| 1109 | bool wxDataViewIconTextRenderer::Render( wxRect cell, wxDC *dc, int state ) |
| 1110 | { |
| 1111 | int xoffset = 0; |
| 1112 | const wxIcon &icon = m_value.GetIcon(); |
| 1113 | if (icon.IsOk()) |
| 1114 | { |
| 1115 | dc->DrawIcon( icon, cell.x, cell.y + ((cell.height - icon.GetHeight()) / 2)); |
| 1116 | xoffset = icon.GetWidth()+4; |
| 1117 | } |
| 1118 | |
| 1119 | RenderText( m_value.GetText(), xoffset, cell, dc, state ); |
| 1120 | |
| 1121 | return true; |
| 1122 | } |
| 1123 | |
| 1124 | wxSize wxDataViewIconTextRenderer::GetSize() const |
| 1125 | { |
| 1126 | const wxDataViewCtrl *view = GetView(); |
| 1127 | if (!m_value.GetText().empty()) |
| 1128 | { |
| 1129 | int x,y; |
| 1130 | view->GetTextExtent( m_value.GetText(), &x, &y ); |
| 1131 | |
| 1132 | if (m_value.GetIcon().IsOk()) |
| 1133 | x += m_value.GetIcon().GetWidth() + 4; |
| 1134 | return wxSize( x, y ); |
| 1135 | } |
| 1136 | return wxSize(80,20); |
| 1137 | } |
| 1138 | |
| 1139 | wxControl* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value) |
| 1140 | { |
| 1141 | wxDataViewIconText iconText; |
| 1142 | iconText << value; |
| 1143 | |
| 1144 | wxString text = iconText.GetText(); |
| 1145 | |
| 1146 | // adjust the label rect to take the width of the icon into account |
| 1147 | if (iconText.GetIcon().IsOk()) |
| 1148 | { |
| 1149 | int w = iconText.GetIcon().GetWidth() + 4; |
| 1150 | labelRect.x += w; |
| 1151 | labelRect.width -= w; |
| 1152 | } |
| 1153 | |
| 1154 | wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, text, |
| 1155 | wxPoint(labelRect.x,labelRect.y), |
| 1156 | wxSize(labelRect.width,labelRect.height) ); |
| 1157 | |
| 1158 | // select the text in the control an place the cursor at the end |
| 1159 | ctrl->SetInsertionPointEnd(); |
| 1160 | ctrl->SelectAll(); |
| 1161 | |
| 1162 | return ctrl; |
| 1163 | } |
| 1164 | |
| 1165 | bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl *editor, wxVariant& value ) |
| 1166 | { |
| 1167 | wxTextCtrl *text = (wxTextCtrl*) editor; |
| 1168 | |
| 1169 | wxDataViewIconText iconText(text->GetValue(), m_value.GetIcon()); |
| 1170 | value << iconText; |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | //----------------------------------------------------------------------------- |
| 1175 | // wxDataViewDropTarget |
| 1176 | //----------------------------------------------------------------------------- |
| 1177 | |
| 1178 | #if wxUSE_DRAG_AND_DROP |
| 1179 | |
| 1180 | class wxBitmapCanvas: public wxWindow |
| 1181 | { |
| 1182 | public: |
| 1183 | wxBitmapCanvas( wxWindow *parent, const wxBitmap &bitmap, const wxSize &size ) : |
| 1184 | wxWindow( parent, wxID_ANY, wxPoint(0,0), size ) |
| 1185 | { |
| 1186 | m_bitmap = bitmap; |
| 1187 | Connect( wxEVT_PAINT, wxPaintEventHandler(wxBitmapCanvas::OnPaint) ); |
| 1188 | } |
| 1189 | |
| 1190 | void OnPaint( wxPaintEvent &WXUNUSED(event) ) |
| 1191 | { |
| 1192 | wxPaintDC dc(this); |
| 1193 | dc.DrawBitmap( m_bitmap, 0, 0); |
| 1194 | } |
| 1195 | |
| 1196 | wxBitmap m_bitmap; |
| 1197 | }; |
| 1198 | |
| 1199 | class wxDataViewDropSource: public wxDropSource |
| 1200 | { |
| 1201 | public: |
| 1202 | wxDataViewDropSource( wxDataViewMainWindow *win, unsigned int row ) : |
| 1203 | wxDropSource( win ) |
| 1204 | { |
| 1205 | m_win = win; |
| 1206 | m_row = row; |
| 1207 | m_hint = NULL; |
| 1208 | } |
| 1209 | |
| 1210 | ~wxDataViewDropSource() |
| 1211 | { |
| 1212 | delete m_hint; |
| 1213 | } |
| 1214 | |
| 1215 | virtual bool GiveFeedback( wxDragResult WXUNUSED(effect) ) |
| 1216 | { |
| 1217 | wxPoint pos = wxGetMousePosition(); |
| 1218 | |
| 1219 | if (!m_hint) |
| 1220 | { |
| 1221 | int liney = m_win->GetLineStart( m_row ); |
| 1222 | int linex = 0; |
| 1223 | m_win->GetOwner()->CalcUnscrolledPosition( 0, liney, NULL, &liney ); |
| 1224 | m_win->ClientToScreen( &linex, &liney ); |
| 1225 | m_dist_x = pos.x - linex; |
| 1226 | m_dist_y = pos.y - liney; |
| 1227 | |
| 1228 | int indent = 0; |
| 1229 | wxBitmap ib = m_win->CreateItemBitmap( m_row, indent ); |
| 1230 | m_dist_x -= indent; |
| 1231 | m_hint = new wxFrame( m_win->GetParent(), wxID_ANY, wxEmptyString, |
| 1232 | wxPoint(pos.x - m_dist_x, pos.y + 5 ), |
| 1233 | ib.GetSize(), |
| 1234 | wxFRAME_TOOL_WINDOW | |
| 1235 | wxFRAME_FLOAT_ON_PARENT | |
| 1236 | wxFRAME_NO_TASKBAR | |
| 1237 | wxNO_BORDER ); |
| 1238 | new wxBitmapCanvas( m_hint, ib, ib.GetSize() ); |
| 1239 | m_hint->Show(); |
| 1240 | } |
| 1241 | else |
| 1242 | { |
| 1243 | m_hint->Move( pos.x - m_dist_x, pos.y + 5 ); |
| 1244 | m_hint->SetTransparent( 128 ); |
| 1245 | } |
| 1246 | |
| 1247 | return false; |
| 1248 | } |
| 1249 | |
| 1250 | wxDataViewMainWindow *m_win; |
| 1251 | unsigned int m_row; |
| 1252 | wxFrame *m_hint; |
| 1253 | int m_dist_x,m_dist_y; |
| 1254 | }; |
| 1255 | |
| 1256 | |
| 1257 | class wxDataViewDropTarget: public wxDropTarget |
| 1258 | { |
| 1259 | public: |
| 1260 | wxDataViewDropTarget( wxDataObject *obj, wxDataViewMainWindow *win ) : |
| 1261 | wxDropTarget( obj ) |
| 1262 | { |
| 1263 | m_win = win; |
| 1264 | } |
| 1265 | |
| 1266 | virtual wxDragResult OnDragOver( wxCoord x, wxCoord y, wxDragResult def ) |
| 1267 | { |
| 1268 | wxDataFormat format = GetMatchingPair(); |
| 1269 | if (format == wxDF_INVALID) |
| 1270 | return wxDragNone; |
| 1271 | return m_win->OnDragOver( format, x, y, def); |
| 1272 | } |
| 1273 | |
| 1274 | virtual bool OnDrop( wxCoord x, wxCoord y ) |
| 1275 | { |
| 1276 | wxDataFormat format = GetMatchingPair(); |
| 1277 | if (format == wxDF_INVALID) |
| 1278 | return false; |
| 1279 | return m_win->OnDrop( format, x, y ); |
| 1280 | } |
| 1281 | |
| 1282 | virtual wxDragResult OnData( wxCoord x, wxCoord y, wxDragResult def ) |
| 1283 | { |
| 1284 | wxDataFormat format = GetMatchingPair(); |
| 1285 | if (format == wxDF_INVALID) |
| 1286 | return wxDragNone; |
| 1287 | if (!GetData()) |
| 1288 | return wxDragNone; |
| 1289 | return m_win->OnData( format, x, y, def ); |
| 1290 | } |
| 1291 | |
| 1292 | virtual void OnLeave() |
| 1293 | { m_win->OnLeave(); } |
| 1294 | |
| 1295 | wxDataViewMainWindow *m_win; |
| 1296 | }; |
| 1297 | |
| 1298 | #endif // wxUSE_DRAG_AND_DROP |
| 1299 | |
| 1300 | //----------------------------------------------------------------------------- |
| 1301 | // wxDataViewRenameTimer |
| 1302 | //----------------------------------------------------------------------------- |
| 1303 | |
| 1304 | wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner ) |
| 1305 | { |
| 1306 | m_owner = owner; |
| 1307 | } |
| 1308 | |
| 1309 | void wxDataViewRenameTimer::Notify() |
| 1310 | { |
| 1311 | m_owner->OnRenameTimer(); |
| 1312 | } |
| 1313 | |
| 1314 | //----------------------------------------------------------------------------- |
| 1315 | // wxDataViewMainWindow |
| 1316 | //----------------------------------------------------------------------------- |
| 1317 | |
| 1318 | // The tree building helper, declared firstly |
| 1319 | static void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, |
| 1320 | wxDataViewTreeNode * node); |
| 1321 | |
| 1322 | int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 ) |
| 1323 | { |
| 1324 | if (row1 > row2) return 1; |
| 1325 | if (row1 == row2) return 0; |
| 1326 | return -1; |
| 1327 | } |
| 1328 | |
| 1329 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow) |
| 1330 | |
| 1331 | BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow) |
| 1332 | EVT_PAINT (wxDataViewMainWindow::OnPaint) |
| 1333 | EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse) |
| 1334 | EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus) |
| 1335 | EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus) |
| 1336 | EVT_CHAR (wxDataViewMainWindow::OnChar) |
| 1337 | END_EVENT_TABLE() |
| 1338 | |
| 1339 | wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id, |
| 1340 | const wxPoint &pos, const wxSize &size, const wxString &name ) : |
| 1341 | wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE, name ), |
| 1342 | m_selection( wxDataViewSelectionCmp ) |
| 1343 | |
| 1344 | { |
| 1345 | SetOwner( parent ); |
| 1346 | |
| 1347 | m_lastOnSame = false; |
| 1348 | m_renameTimer = new wxDataViewRenameTimer( this ); |
| 1349 | |
| 1350 | // TODO: user better initial values/nothing selected |
| 1351 | m_currentCol = NULL; |
| 1352 | m_currentRow = 0; |
| 1353 | |
| 1354 | m_lineHeight = wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1 |
| 1355 | |
| 1356 | #if wxUSE_DRAG_AND_DROP |
| 1357 | m_dragCount = 0; |
| 1358 | m_dragStart = wxPoint(0,0); |
| 1359 | |
| 1360 | m_dragEnabled = false; |
| 1361 | m_dropEnabled = false; |
| 1362 | m_dropHint = false; |
| 1363 | m_dropHintLine = (unsigned int) -1; |
| 1364 | #endif // wxUSE_DRAG_AND_DROP |
| 1365 | |
| 1366 | m_lineLastClicked = (unsigned int) -1; |
| 1367 | m_lineBeforeLastClicked = (unsigned int) -1; |
| 1368 | m_lineSelectSingleOnUp = (unsigned int) -1; |
| 1369 | |
| 1370 | m_hasFocus = false; |
| 1371 | |
| 1372 | SetBackgroundColour( *wxWHITE ); |
| 1373 | |
| 1374 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); |
| 1375 | |
| 1376 | m_penRule = wxPen(GetRuleColour()); |
| 1377 | |
| 1378 | // compose a pen whichcan draw black lines |
| 1379 | // TODO: maybe there is something system colour to use |
| 1380 | m_penExpander = wxPen(wxColour(0,0,0)); |
| 1381 | |
| 1382 | m_root = new wxDataViewTreeNode( NULL ); |
| 1383 | m_root->SetHasChildren(true); |
| 1384 | |
| 1385 | // Make m_count = -1 will cause the class recaculate the real displaying number of rows. |
| 1386 | m_count = -1; |
| 1387 | m_underMouse = NULL; |
| 1388 | UpdateDisplay(); |
| 1389 | } |
| 1390 | |
| 1391 | wxDataViewMainWindow::~wxDataViewMainWindow() |
| 1392 | { |
| 1393 | DestroyTree(); |
| 1394 | delete m_renameTimer; |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | #if wxUSE_DRAG_AND_DROP |
| 1399 | bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat &format ) |
| 1400 | { |
| 1401 | m_dragFormat = format; |
| 1402 | m_dragEnabled = format != wxDF_INVALID; |
| 1403 | |
| 1404 | return true; |
| 1405 | } |
| 1406 | |
| 1407 | bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat &format ) |
| 1408 | { |
| 1409 | m_dropFormat = format; |
| 1410 | m_dropEnabled = format != wxDF_INVALID; |
| 1411 | |
| 1412 | if (m_dropEnabled) |
| 1413 | SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format ), this ) ); |
| 1414 | |
| 1415 | return true; |
| 1416 | } |
| 1417 | |
| 1418 | void wxDataViewMainWindow::RemoveDropHint() |
| 1419 | { |
| 1420 | if (m_dropHint) |
| 1421 | { |
| 1422 | m_dropHint = false; |
| 1423 | RefreshRow( m_dropHintLine ); |
| 1424 | m_dropHintLine = (unsigned int) -1; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | wxDragResult wxDataViewMainWindow::OnDragOver( wxDataFormat format, wxCoord x, |
| 1429 | wxCoord y, wxDragResult def ) |
| 1430 | { |
| 1431 | int xx = x; |
| 1432 | int yy = y; |
| 1433 | m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy ); |
| 1434 | unsigned int row = GetLineAt( yy ); |
| 1435 | |
| 1436 | if ((row >= GetRowCount()) || (yy > GetEndOfLastCol())) |
| 1437 | { |
| 1438 | RemoveDropHint(); |
| 1439 | return wxDragNone; |
| 1440 | } |
| 1441 | |
| 1442 | wxDataViewItem item = GetItemByRow( row ); |
| 1443 | |
| 1444 | wxDataViewModel *model = GetOwner()->GetModel(); |
| 1445 | |
| 1446 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() ); |
| 1447 | event.SetEventObject( m_owner ); |
| 1448 | event.SetItem( item ); |
| 1449 | event.SetModel( model ); |
| 1450 | event.SetDataFormat( format ); |
| 1451 | if (!m_owner->HandleWindowEvent( event )) |
| 1452 | { |
| 1453 | RemoveDropHint(); |
| 1454 | return wxDragNone; |
| 1455 | } |
| 1456 | |
| 1457 | if (!event.IsAllowed()) |
| 1458 | { |
| 1459 | RemoveDropHint(); |
| 1460 | return wxDragNone; |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | if (m_dropHint && (row != m_dropHintLine)) |
| 1465 | RefreshRow( m_dropHintLine ); |
| 1466 | m_dropHint = true; |
| 1467 | m_dropHintLine = row; |
| 1468 | RefreshRow( row ); |
| 1469 | |
| 1470 | return def; |
| 1471 | } |
| 1472 | |
| 1473 | bool wxDataViewMainWindow::OnDrop( wxDataFormat format, wxCoord x, wxCoord y ) |
| 1474 | { |
| 1475 | RemoveDropHint(); |
| 1476 | |
| 1477 | int xx = x; |
| 1478 | int yy = y; |
| 1479 | m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy ); |
| 1480 | unsigned int row = GetLineAt( yy ); |
| 1481 | |
| 1482 | if ((row >= GetRowCount()) || (yy > GetEndOfLastCol())) |
| 1483 | return false; |
| 1484 | |
| 1485 | wxDataViewItem item = GetItemByRow( row ); |
| 1486 | |
| 1487 | wxDataViewModel *model = GetOwner()->GetModel(); |
| 1488 | |
| 1489 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() ); |
| 1490 | event.SetEventObject( m_owner ); |
| 1491 | event.SetItem( item ); |
| 1492 | event.SetModel( model ); |
| 1493 | event.SetDataFormat( format ); |
| 1494 | if (!m_owner->HandleWindowEvent( event )) |
| 1495 | return false; |
| 1496 | |
| 1497 | if (!event.IsAllowed()) |
| 1498 | return false; |
| 1499 | |
| 1500 | return true; |
| 1501 | } |
| 1502 | |
| 1503 | wxDragResult wxDataViewMainWindow::OnData( wxDataFormat format, wxCoord x, wxCoord y, |
| 1504 | wxDragResult def ) |
| 1505 | { |
| 1506 | int xx = x; |
| 1507 | int yy = y; |
| 1508 | m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy ); |
| 1509 | unsigned int row = GetLineAt( yy ); |
| 1510 | |
| 1511 | if ((row >= GetRowCount()) || (yy > GetEndOfLastCol())) |
| 1512 | return wxDragNone; |
| 1513 | |
| 1514 | wxDataViewItem item = GetItemByRow( row ); |
| 1515 | |
| 1516 | wxDataViewModel *model = GetOwner()->GetModel(); |
| 1517 | |
| 1518 | wxCustomDataObject *obj = (wxCustomDataObject *) GetDropTarget()->GetDataObject(); |
| 1519 | |
| 1520 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() ); |
| 1521 | event.SetEventObject( m_owner ); |
| 1522 | event.SetItem( item ); |
| 1523 | event.SetModel( model ); |
| 1524 | event.SetDataFormat( format ); |
| 1525 | event.SetDataSize( obj->GetSize() ); |
| 1526 | event.SetDataBuffer( obj->GetData() ); |
| 1527 | if (!m_owner->HandleWindowEvent( event )) |
| 1528 | return wxDragNone; |
| 1529 | |
| 1530 | if (!event.IsAllowed()) |
| 1531 | return wxDragNone; |
| 1532 | |
| 1533 | return def; |
| 1534 | } |
| 1535 | |
| 1536 | void wxDataViewMainWindow::OnLeave() |
| 1537 | { |
| 1538 | RemoveDropHint(); |
| 1539 | } |
| 1540 | |
| 1541 | wxBitmap wxDataViewMainWindow::CreateItemBitmap( unsigned int row, int &indent ) |
| 1542 | { |
| 1543 | int height = GetLineHeight( row ); |
| 1544 | int width = 0; |
| 1545 | unsigned int cols = GetOwner()->GetColumnCount(); |
| 1546 | unsigned int col; |
| 1547 | for (col = 0; col < cols; col++) |
| 1548 | { |
| 1549 | wxDataViewColumn *column = GetOwner()->GetColumnAt(col); |
| 1550 | if (column->IsHidden()) |
| 1551 | continue; // skip it! |
| 1552 | width += column->GetWidth(); |
| 1553 | } |
| 1554 | |
| 1555 | indent = 0; |
| 1556 | if (!IsVirtualList()) |
| 1557 | { |
| 1558 | wxDataViewTreeNode *node = GetTreeNodeByRow(row); |
| 1559 | indent = GetOwner()->GetIndent() * node->GetIndentLevel(); |
| 1560 | indent = indent + m_lineHeight; |
| 1561 | // try to use the m_lineHeight as the expander space |
| 1562 | |
| 1563 | if(!node->HasChildren()) |
| 1564 | delete node; |
| 1565 | } |
| 1566 | width -= indent; |
| 1567 | |
| 1568 | wxBitmap bitmap( width, height ); |
| 1569 | wxMemoryDC dc( bitmap ); |
| 1570 | dc.SetFont( GetFont() ); |
| 1571 | dc.SetPen( *wxBLACK_PEN ); |
| 1572 | dc.SetBrush( *wxWHITE_BRUSH ); |
| 1573 | dc.DrawRectangle( 0,0,width,height ); |
| 1574 | |
| 1575 | wxDataViewModel *model = m_owner->GetModel(); |
| 1576 | |
| 1577 | wxDataViewColumn *expander = GetOwner()->GetExpanderColumn(); |
| 1578 | if (!expander) |
| 1579 | { |
| 1580 | // TODO-RTL: last column for RTL support |
| 1581 | expander = GetOwner()->GetColumnAt( 0 ); |
| 1582 | GetOwner()->SetExpanderColumn(expander); |
| 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | int x = 0; |
| 1587 | for (col = 0; col < cols; col++) |
| 1588 | { |
| 1589 | wxDataViewColumn *column = GetOwner()->GetColumnAt( col ); |
| 1590 | wxDataViewRenderer *cell = column->GetRenderer(); |
| 1591 | |
| 1592 | if (column->IsHidden()) |
| 1593 | continue; // skip it! |
| 1594 | |
| 1595 | width = column->GetWidth(); |
| 1596 | |
| 1597 | if (column == expander) |
| 1598 | width -= indent; |
| 1599 | |
| 1600 | wxVariant value; |
| 1601 | wxDataViewItem item = GetItemByRow( row ); |
| 1602 | model->GetValue( value, item, column->GetModelColumn()); |
| 1603 | cell->SetValue( value ); |
| 1604 | |
| 1605 | if (cell->GetWantsAttr()) |
| 1606 | { |
| 1607 | wxDataViewItemAttr attr; |
| 1608 | bool ret = model->GetAttr( item, column->GetModelColumn(), attr ); |
| 1609 | if (ret) |
| 1610 | cell->SetAttr( attr ); |
| 1611 | cell->SetHasAttr( ret ); |
| 1612 | } |
| 1613 | |
| 1614 | wxSize size = cell->GetSize(); |
| 1615 | size.x = wxMin( 2*PADDING_RIGHTLEFT + size.x, width ); |
| 1616 | size.y = height; |
| 1617 | wxRect item_rect(x, 0, size.x, size.y); |
| 1618 | |
| 1619 | int align = cell->CalculateAlignment(); |
| 1620 | // horizontal alignment: |
| 1621 | item_rect.x = x; |
| 1622 | if (align & wxALIGN_CENTER_HORIZONTAL) |
| 1623 | item_rect.x = x + (width / 2) - (size.x / 2); |
| 1624 | else if (align & wxALIGN_RIGHT) |
| 1625 | item_rect.x = x + width - size.x; |
| 1626 | // else: wxALIGN_LEFT is the default |
| 1627 | |
| 1628 | // vertical alignment: |
| 1629 | item_rect.y = 0; |
| 1630 | if (align & wxALIGN_CENTER_VERTICAL) |
| 1631 | item_rect.y = (height / 2) - (size.y / 2); |
| 1632 | else if (align & wxALIGN_BOTTOM) |
| 1633 | item_rect.y = height - size.y; |
| 1634 | // else: wxALIGN_TOP is the default |
| 1635 | |
| 1636 | // add padding |
| 1637 | item_rect.x += PADDING_RIGHTLEFT; |
| 1638 | item_rect.width = size.x - 2 * PADDING_RIGHTLEFT; |
| 1639 | |
| 1640 | // dc.SetClippingRegion( item_rect ); |
| 1641 | cell->Render( item_rect, &dc, 0 ); |
| 1642 | // dc.DestroyClippingRegion(); |
| 1643 | |
| 1644 | x += width; |
| 1645 | } |
| 1646 | |
| 1647 | return bitmap; |
| 1648 | } |
| 1649 | |
| 1650 | #endif // wxUSE_DRAG_AND_DROP |
| 1651 | |
| 1652 | |
| 1653 | void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
| 1654 | { |
| 1655 | wxDataViewModel *model = GetOwner()->GetModel(); |
| 1656 | wxAutoBufferedPaintDC dc( this ); |
| 1657 | |
| 1658 | #ifdef __WXMSW__ |
| 1659 | dc.SetPen( *wxTRANSPARENT_PEN ); |
| 1660 | dc.SetBrush( wxBrush( GetBackgroundColour()) ); |
| 1661 | dc.SetBrush( *wxWHITE_BRUSH ); |
| 1662 | wxSize size( GetClientSize() ); |
| 1663 | dc.DrawRectangle( 0,0,size.x,size.y ); |
| 1664 | #endif |
| 1665 | |
| 1666 | // prepare the DC |
| 1667 | GetOwner()->PrepareDC( dc ); |
| 1668 | dc.SetFont( GetFont() ); |
| 1669 | |
| 1670 | wxRect update = GetUpdateRegion().GetBox(); |
| 1671 | m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y ); |
| 1672 | |
| 1673 | // compute which items needs to be redrawn |
| 1674 | unsigned int item_start = GetLineAt( wxMax(0,update.y) ); |
| 1675 | unsigned int item_count = |
| 1676 | wxMin( (int)( GetLineAt( wxMax(0,update.y+update.height) ) - item_start + 1), |
| 1677 | (int)(GetRowCount( ) - item_start)); |
| 1678 | unsigned int item_last = item_start + item_count; |
| 1679 | |
| 1680 | // compute which columns needs to be redrawn |
| 1681 | unsigned int cols = GetOwner()->GetColumnCount(); |
| 1682 | unsigned int col_start = 0; |
| 1683 | unsigned int x_start; |
| 1684 | for (x_start = 0; col_start < cols; col_start++) |
| 1685 | { |
| 1686 | wxDataViewColumn *col = GetOwner()->GetColumnAt(col_start); |
| 1687 | if (col->IsHidden()) |
| 1688 | continue; // skip it! |
| 1689 | |
| 1690 | unsigned int w = col->GetWidth(); |
| 1691 | if (x_start+w >= (unsigned int)update.x) |
| 1692 | break; |
| 1693 | |
| 1694 | x_start += w; |
| 1695 | } |
| 1696 | |
| 1697 | unsigned int col_last = col_start; |
| 1698 | unsigned int x_last = x_start; |
| 1699 | for (; col_last < cols; col_last++) |
| 1700 | { |
| 1701 | wxDataViewColumn *col = GetOwner()->GetColumnAt(col_last); |
| 1702 | if (col->IsHidden()) |
| 1703 | continue; // skip it! |
| 1704 | |
| 1705 | if (x_last > (unsigned int)update.GetRight()) |
| 1706 | break; |
| 1707 | |
| 1708 | x_last += col->GetWidth(); |
| 1709 | } |
| 1710 | |
| 1711 | // Draw horizontal rules if required |
| 1712 | if ( m_owner->HasFlag(wxDV_HORIZ_RULES) ) |
| 1713 | { |
| 1714 | dc.SetPen(m_penRule); |
| 1715 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
| 1716 | |
| 1717 | for (unsigned int i = item_start; i <= item_last; i++) |
| 1718 | { |
| 1719 | int y = GetLineStart( i ); |
| 1720 | dc.DrawLine(x_start, y, x_last, y); |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | // Draw vertical rules if required |
| 1725 | if ( m_owner->HasFlag(wxDV_VERT_RULES) ) |
| 1726 | { |
| 1727 | dc.SetPen(m_penRule); |
| 1728 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
| 1729 | |
| 1730 | int x = x_start; |
| 1731 | for (unsigned int i = col_start; i < col_last; i++) |
| 1732 | { |
| 1733 | wxDataViewColumn *col = GetOwner()->GetColumnAt(i); |
| 1734 | if (col->IsHidden()) |
| 1735 | continue; // skip it |
| 1736 | |
| 1737 | dc.DrawLine(x, GetLineStart( item_start ), |
| 1738 | x, GetLineStart( item_last ) ); |
| 1739 | |
| 1740 | x += col->GetWidth(); |
| 1741 | } |
| 1742 | |
| 1743 | // Draw last vertical rule |
| 1744 | dc.DrawLine(x, GetLineStart( item_start ), |
| 1745 | x, GetLineStart( item_last ) ); |
| 1746 | } |
| 1747 | |
| 1748 | // redraw the background for the items which are selected/current |
| 1749 | for (unsigned int item = item_start; item < item_last; item++) |
| 1750 | { |
| 1751 | bool selected = m_selection.Index( item ) != wxNOT_FOUND; |
| 1752 | if (selected || item == m_currentRow) |
| 1753 | { |
| 1754 | int flags = selected ? (int)wxCONTROL_SELECTED : 0; |
| 1755 | if (item == m_currentRow) |
| 1756 | flags |= wxCONTROL_CURRENT; |
| 1757 | if (m_hasFocus) |
| 1758 | flags |= wxCONTROL_FOCUSED; |
| 1759 | |
| 1760 | wxRect rect( x_start, GetLineStart( item ), x_last, GetLineHeight( item ) ); |
| 1761 | wxRendererNative::Get().DrawItemSelectionRect |
| 1762 | ( |
| 1763 | this, |
| 1764 | dc, |
| 1765 | rect, |
| 1766 | flags |
| 1767 | ); |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | #if wxUSE_DRAG_AND_DROP |
| 1772 | if (m_dropHint) |
| 1773 | { |
| 1774 | wxRect rect( x_start, GetLineStart( m_dropHintLine ), |
| 1775 | x_last, GetLineHeight( m_dropHintLine ) ); |
| 1776 | dc.SetPen( *wxBLACK_PEN ); |
| 1777 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); |
| 1778 | dc.DrawRectangle( rect ); |
| 1779 | } |
| 1780 | #endif // wxUSE_DRAG_AND_DROP |
| 1781 | |
| 1782 | wxDataViewColumn *expander = GetOwner()->GetExpanderColumn(); |
| 1783 | if (!expander) |
| 1784 | { |
| 1785 | // TODO-RTL: last column for RTL support |
| 1786 | expander = GetOwner()->GetColumnAt( 0 ); |
| 1787 | GetOwner()->SetExpanderColumn(expander); |
| 1788 | } |
| 1789 | |
| 1790 | // redraw all cells for all rows which must be repainted and all columns |
| 1791 | wxRect cell_rect; |
| 1792 | cell_rect.x = x_start; |
| 1793 | for (unsigned int i = col_start; i < col_last; i++) |
| 1794 | { |
| 1795 | wxDataViewColumn *col = GetOwner()->GetColumnAt( i ); |
| 1796 | wxDataViewRenderer *cell = col->GetRenderer(); |
| 1797 | cell_rect.width = col->GetWidth(); |
| 1798 | |
| 1799 | if (col->IsHidden()) |
| 1800 | continue; // skip it! |
| 1801 | |
| 1802 | for (unsigned int item = item_start; item < item_last; item++) |
| 1803 | { |
| 1804 | // get the cell value and set it into the renderer |
| 1805 | wxVariant value; |
| 1806 | wxDataViewTreeNode *node = NULL; |
| 1807 | wxDataViewItem dataitem; |
| 1808 | |
| 1809 | if (!IsVirtualList()) |
| 1810 | { |
| 1811 | node = GetTreeNodeByRow(item); |
| 1812 | if( node == NULL ) |
| 1813 | continue; |
| 1814 | |
| 1815 | dataitem = node->GetItem(); |
| 1816 | |
| 1817 | if ((i > 0) && model->IsContainer(dataitem) && |
| 1818 | !model->HasContainerColumns(dataitem)) |
| 1819 | continue; |
| 1820 | } |
| 1821 | else |
| 1822 | { |
| 1823 | dataitem = wxDataViewItem( wxUIntToPtr(item) ); |
| 1824 | } |
| 1825 | |
| 1826 | model->GetValue( value, dataitem, col->GetModelColumn()); |
| 1827 | cell->SetValue( value ); |
| 1828 | |
| 1829 | if (cell->GetWantsAttr()) |
| 1830 | { |
| 1831 | wxDataViewItemAttr attr; |
| 1832 | bool ret = model->GetAttr( dataitem, col->GetModelColumn(), attr ); |
| 1833 | if (ret) |
| 1834 | cell->SetAttr( attr ); |
| 1835 | cell->SetHasAttr( ret ); |
| 1836 | } |
| 1837 | |
| 1838 | // update cell_rect |
| 1839 | cell_rect.y = GetLineStart( item ); |
| 1840 | cell_rect.height = GetLineHeight( item ); |
| 1841 | |
| 1842 | // Draw the expander here. |
| 1843 | int indent = 0; |
| 1844 | if ((!IsVirtualList()) && (col == expander)) |
| 1845 | { |
| 1846 | indent = node->GetIndentLevel(); |
| 1847 | |
| 1848 | // Calculate the indent first |
| 1849 | indent = cell_rect.x + GetOwner()->GetIndent() * indent; |
| 1850 | |
| 1851 | int expander_width = m_lineHeight - 2*EXPANDER_MARGIN; |
| 1852 | |
| 1853 | // change the cell_rect.x to the appropriate pos |
| 1854 | int expander_x = indent + EXPANDER_MARGIN; |
| 1855 | int expander_y = cell_rect.y + EXPANDER_MARGIN + (GetLineHeight(item) / 2) |
| 1856 | - (expander_width/2) - EXPANDER_OFFSET; |
| 1857 | |
| 1858 | indent = indent + m_lineHeight; |
| 1859 | // try to use the m_lineHeight as the expander space |
| 1860 | |
| 1861 | dc.SetPen( m_penExpander ); |
| 1862 | dc.SetBrush( wxNullBrush ); |
| 1863 | if( node->HasChildren() ) |
| 1864 | { |
| 1865 | wxRect rect( expander_x , expander_y, expander_width, expander_width); |
| 1866 | int flag = 0; |
| 1867 | if (m_underMouse == node) |
| 1868 | { |
| 1869 | flag |= wxCONTROL_CURRENT; |
| 1870 | } |
| 1871 | if( node->IsOpen() ) |
| 1872 | wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, |
| 1873 | flag|wxCONTROL_EXPANDED ); |
| 1874 | else |
| 1875 | wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag); |
| 1876 | } |
| 1877 | |
| 1878 | // force the expander column to left-center align |
| 1879 | cell->SetAlignment( wxALIGN_CENTER_VERTICAL ); |
| 1880 | } |
| 1881 | if (node && !node->HasChildren()) |
| 1882 | { |
| 1883 | // Yes, if the node does not have any child, it must be a leaf which |
| 1884 | // mean that it is a temporarily created by GetTreeNodeByRow |
| 1885 | wxDELETE(node); |
| 1886 | } |
| 1887 | |
| 1888 | // cannot be bigger than allocated space |
| 1889 | wxSize size = cell->GetSize(); |
| 1890 | |
| 1891 | // Because of the tree structure indent, here we should minus the width |
| 1892 | // of the cell for drawing |
| 1893 | size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width - indent ); |
| 1894 | // size.y = wxMin( size.y, cell_rect.height ); |
| 1895 | size.y = cell_rect.height; |
| 1896 | |
| 1897 | wxRect item_rect(cell_rect.GetTopLeft(), size); |
| 1898 | int align = cell->CalculateAlignment(); |
| 1899 | |
| 1900 | // horizontal alignment: |
| 1901 | item_rect.x = cell_rect.x; |
| 1902 | if (align & wxALIGN_CENTER_HORIZONTAL) |
| 1903 | item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2); |
| 1904 | else if (align & wxALIGN_RIGHT) |
| 1905 | item_rect.x = cell_rect.x + cell_rect.width - size.x; |
| 1906 | // else: wxALIGN_LEFT is the default |
| 1907 | |
| 1908 | // vertical alignment: |
| 1909 | item_rect.y = cell_rect.y; |
| 1910 | if (align & wxALIGN_CENTER_VERTICAL) |
| 1911 | item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2); |
| 1912 | else if (align & wxALIGN_BOTTOM) |
| 1913 | item_rect.y = cell_rect.y + cell_rect.height - size.y; |
| 1914 | // else: wxALIGN_TOP is the default |
| 1915 | |
| 1916 | // add padding |
| 1917 | item_rect.x += PADDING_RIGHTLEFT; |
| 1918 | item_rect.width = size.x - 2 * PADDING_RIGHTLEFT; |
| 1919 | |
| 1920 | // Here we add the tree indent |
| 1921 | item_rect.x += indent; |
| 1922 | |
| 1923 | int state = 0; |
| 1924 | if (m_hasFocus && (m_selection.Index(item) != wxNOT_FOUND)) |
| 1925 | state |= wxDATAVIEW_CELL_SELECTED; |
| 1926 | |
| 1927 | // TODO: it would be much more efficient to create a clipping |
| 1928 | // region for the entire column being rendered (in the OnPaint |
| 1929 | // of wxDataViewMainWindow) instead of a single clip region for |
| 1930 | // each cell. However it would mean that each renderer should |
| 1931 | // respect the given wxRect's top & bottom coords, eventually |
| 1932 | // violating only the left & right coords - however the user can |
| 1933 | // make its own renderer and thus we cannot be sure of that. |
| 1934 | dc.SetClippingRegion( item_rect ); |
| 1935 | cell->Render( item_rect, &dc, state ); |
| 1936 | dc.DestroyClippingRegion(); |
| 1937 | } |
| 1938 | |
| 1939 | cell_rect.x += cell_rect.width; |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | void wxDataViewMainWindow::OnRenameTimer() |
| 1944 | { |
| 1945 | // We have to call this here because changes may just have |
| 1946 | // been made and no screen update taken place. |
| 1947 | if ( m_dirty ) |
| 1948 | { |
| 1949 | // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead |
| 1950 | // (needs to be tested!) |
| 1951 | wxSafeYield(); |
| 1952 | } |
| 1953 | |
| 1954 | wxDataViewItem item = GetItemByRow( m_currentRow ); |
| 1955 | |
| 1956 | wxRect labelRect = GetItemRect(item, m_currentCol); |
| 1957 | |
| 1958 | m_currentCol->GetRenderer()->StartEditing( item, labelRect ); |
| 1959 | } |
| 1960 | |
| 1961 | //----------------------------------------------------------------------------- |
| 1962 | // Helper class for do operation on the tree node |
| 1963 | //----------------------------------------------------------------------------- |
| 1964 | class DoJob |
| 1965 | { |
| 1966 | public: |
| 1967 | DoJob() { } |
| 1968 | virtual ~DoJob() { } |
| 1969 | |
| 1970 | // The return value control how the tree-walker tranverse the tree |
| 1971 | // 0: Job done, stop tranverse and return |
| 1972 | // 1: Ignore the current node's subtree and continue |
| 1973 | // 2: Job not done, continue |
| 1974 | enum { OK = 0 , IGR = 1, CONT = 2 }; |
| 1975 | virtual int operator() ( wxDataViewTreeNode * node ) = 0; |
| 1976 | virtual int operator() ( void * n ) = 0; |
| 1977 | }; |
| 1978 | |
| 1979 | bool Walker( wxDataViewTreeNode * node, DoJob & func ) |
| 1980 | { |
| 1981 | if( node==NULL ) |
| 1982 | return false; |
| 1983 | |
| 1984 | switch( func( node ) ) |
| 1985 | { |
| 1986 | case DoJob::OK : |
| 1987 | return true; |
| 1988 | case DoJob::IGR: |
| 1989 | return false; |
| 1990 | case DoJob::CONT: |
| 1991 | default: |
| 1992 | ; |
| 1993 | } |
| 1994 | |
| 1995 | wxDataViewTreeNodes nodes = node->GetNodes(); |
| 1996 | wxDataViewTreeLeaves leaves = node->GetChildren(); |
| 1997 | |
| 1998 | int len_nodes = nodes.GetCount(); |
| 1999 | int len = leaves.GetCount(); |
| 2000 | int i = 0, nodes_i = 0; |
| 2001 | |
| 2002 | for(; i < len; i ++ ) |
| 2003 | { |
| 2004 | void * n = leaves[i]; |
| 2005 | if( nodes_i < len_nodes && n == nodes[nodes_i]->GetItem().GetID() ) |
| 2006 | { |
| 2007 | wxDataViewTreeNode * nd = nodes[nodes_i]; |
| 2008 | nodes_i++; |
| 2009 | |
| 2010 | if( Walker( nd , func ) ) |
| 2011 | return true; |
| 2012 | |
| 2013 | } |
| 2014 | else |
| 2015 | switch( func( n ) ) |
| 2016 | { |
| 2017 | case DoJob::OK : |
| 2018 | return true; |
| 2019 | case DoJob::IGR: |
| 2020 | continue; |
| 2021 | case DoJob::CONT: |
| 2022 | default: |
| 2023 | ; |
| 2024 | } |
| 2025 | } |
| 2026 | return false; |
| 2027 | } |
| 2028 | |
| 2029 | bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item) |
| 2030 | { |
| 2031 | if (!m_root) |
| 2032 | { |
| 2033 | m_count++; |
| 2034 | UpdateDisplay(); |
| 2035 | return true; |
| 2036 | } |
| 2037 | |
| 2038 | SortPrepare(); |
| 2039 | |
| 2040 | wxDataViewTreeNode * node; |
| 2041 | node = FindNode(parent); |
| 2042 | |
| 2043 | if( node == NULL ) |
| 2044 | return false; |
| 2045 | |
| 2046 | node->SetHasChildren( true ); |
| 2047 | |
| 2048 | if( g_model->IsContainer( item ) ) |
| 2049 | { |
| 2050 | wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node ); |
| 2051 | newnode->SetItem(item); |
| 2052 | newnode->SetHasChildren( true ); |
| 2053 | node->AddNode( newnode); |
| 2054 | } |
| 2055 | else |
| 2056 | node->AddLeaf( item.GetID() ); |
| 2057 | |
| 2058 | node->ChangeSubTreeCount(1); |
| 2059 | |
| 2060 | m_count = -1; |
| 2061 | UpdateDisplay(); |
| 2062 | |
| 2063 | return true; |
| 2064 | } |
| 2065 | |
| 2066 | static void DestroyTreeHelper( wxDataViewTreeNode * node); |
| 2067 | |
| 2068 | bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent, |
| 2069 | const wxDataViewItem& item) |
| 2070 | { |
| 2071 | if (!m_root) |
| 2072 | { |
| 2073 | m_count--; |
| 2074 | if( m_currentRow > GetRowCount() ) |
| 2075 | m_currentRow = m_count - 1; |
| 2076 | |
| 2077 | m_selection.Empty(); |
| 2078 | |
| 2079 | UpdateDisplay(); |
| 2080 | |
| 2081 | return true; |
| 2082 | } |
| 2083 | |
| 2084 | wxDataViewTreeNode * node = FindNode(parent); |
| 2085 | |
| 2086 | wxCHECK_MSG( node != NULL, false, "item not found" ); |
| 2087 | wxCHECK_MSG( node->GetChildren().Index( item.GetID() ) != wxNOT_FOUND, |
| 2088 | false, "item not found" ); |
| 2089 | |
| 2090 | int sub = -1; |
| 2091 | node->GetChildren().Remove( item.GetID() ); |
| 2092 | // Manipolate selection |
| 2093 | if( m_selection.GetCount() > 1 ) |
| 2094 | { |
| 2095 | m_selection.Empty(); |
| 2096 | } |
| 2097 | bool isContainer = false; |
| 2098 | wxDataViewTreeNodes nds = node->GetNodes(); |
| 2099 | for (size_t i = 0; i < nds.GetCount(); i ++) |
| 2100 | { |
| 2101 | if (nds[i]->GetItem() == item) |
| 2102 | { |
| 2103 | isContainer = true; |
| 2104 | break; |
| 2105 | } |
| 2106 | } |
| 2107 | if( isContainer ) |
| 2108 | { |
| 2109 | wxDataViewTreeNode * n = NULL; |
| 2110 | wxDataViewTreeNodes nodes = node->GetNodes(); |
| 2111 | int len = nodes.GetCount(); |
| 2112 | for( int i = 0; i < len; i ++) |
| 2113 | { |
| 2114 | if( nodes[i]->GetItem() == item ) |
| 2115 | { |
| 2116 | n = nodes[i]; |
| 2117 | break; |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | wxCHECK_MSG( n != NULL, false, "item not found" ); |
| 2122 | |
| 2123 | node->GetNodes().Remove( n ); |
| 2124 | sub -= n->GetSubTreeCount(); |
| 2125 | ::DestroyTreeHelper(n); |
| 2126 | } |
| 2127 | // Make the row number invalid and get a new valid one when user call GetRowCount |
| 2128 | m_count = -1; |
| 2129 | node->ChangeSubTreeCount(sub); |
| 2130 | |
| 2131 | // Change the current row to the last row if the current exceed the max row number |
| 2132 | if( m_currentRow > GetRowCount() ) |
| 2133 | m_currentRow = m_count - 1; |
| 2134 | |
| 2135 | UpdateDisplay(); |
| 2136 | |
| 2137 | return true; |
| 2138 | } |
| 2139 | |
| 2140 | bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item) |
| 2141 | { |
| 2142 | SortPrepare(); |
| 2143 | g_model->Resort(); |
| 2144 | |
| 2145 | // Send event |
| 2146 | wxWindow *parent = GetParent(); |
| 2147 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId()); |
| 2148 | le.SetEventObject(parent); |
| 2149 | le.SetModel(GetOwner()->GetModel()); |
| 2150 | le.SetItem(item); |
| 2151 | parent->GetEventHandler()->ProcessEvent(le); |
| 2152 | |
| 2153 | return true; |
| 2154 | } |
| 2155 | |
| 2156 | bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int col ) |
| 2157 | { |
| 2158 | // NOTE: to be valid, we cannot use e.g. INT_MAX - 1 |
| 2159 | /*#define MAX_VIRTUAL_WIDTH 100000 |
| 2160 | |
| 2161 | wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight ); |
| 2162 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
| 2163 | Refresh( true, &rect ); |
| 2164 | |
| 2165 | return true; |
| 2166 | */ |
| 2167 | SortPrepare(); |
| 2168 | g_model->Resort(); |
| 2169 | |
| 2170 | // Send event |
| 2171 | wxWindow *parent = GetParent(); |
| 2172 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId()); |
| 2173 | le.SetEventObject(parent); |
| 2174 | le.SetModel(GetOwner()->GetModel()); |
| 2175 | le.SetItem(item); |
| 2176 | le.SetColumn(col); |
| 2177 | le.SetDataViewColumn(GetOwner()->GetColumn(col)); |
| 2178 | parent->GetEventHandler()->ProcessEvent(le); |
| 2179 | |
| 2180 | return true; |
| 2181 | } |
| 2182 | |
| 2183 | bool wxDataViewMainWindow::Cleared() |
| 2184 | { |
| 2185 | DestroyTree(); |
| 2186 | |
| 2187 | SortPrepare(); |
| 2188 | BuildTree( GetOwner()->GetModel() ); |
| 2189 | |
| 2190 | UpdateDisplay(); |
| 2191 | |
| 2192 | return true; |
| 2193 | } |
| 2194 | |
| 2195 | void wxDataViewMainWindow::UpdateDisplay() |
| 2196 | { |
| 2197 | m_dirty = true; |
| 2198 | } |
| 2199 | |
| 2200 | void wxDataViewMainWindow::OnInternalIdle() |
| 2201 | { |
| 2202 | wxWindow::OnInternalIdle(); |
| 2203 | |
| 2204 | if (m_dirty) |
| 2205 | { |
| 2206 | RecalculateDisplay(); |
| 2207 | m_dirty = false; |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | void wxDataViewMainWindow::RecalculateDisplay() |
| 2212 | { |
| 2213 | wxDataViewModel *model = GetOwner()->GetModel(); |
| 2214 | if (!model) |
| 2215 | { |
| 2216 | Refresh(); |
| 2217 | return; |
| 2218 | } |
| 2219 | |
| 2220 | int width = GetEndOfLastCol(); |
| 2221 | int height = GetLineStart( GetRowCount() ); |
| 2222 | |
| 2223 | SetVirtualSize( width, height ); |
| 2224 | GetOwner()->SetScrollRate( 10, m_lineHeight ); |
| 2225 | |
| 2226 | Refresh(); |
| 2227 | } |
| 2228 | |
| 2229 | void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) |
| 2230 | { |
| 2231 | wxWindow::ScrollWindow( dx, dy, rect ); |
| 2232 | |
| 2233 | if (GetOwner()->m_headerArea) |
| 2234 | GetOwner()->m_headerArea->ScrollWindow( dx, 0 ); |
| 2235 | } |
| 2236 | |
| 2237 | void wxDataViewMainWindow::ScrollTo( int rows, int column ) |
| 2238 | { |
| 2239 | int x, y; |
| 2240 | m_owner->GetScrollPixelsPerUnit( &x, &y ); |
| 2241 | int sy = GetLineStart( rows )/y; |
| 2242 | int sx = 0; |
| 2243 | if( column != -1 ) |
| 2244 | { |
| 2245 | wxRect rect = GetClientRect(); |
| 2246 | int colnum = 0; |
| 2247 | int x_start, w = 0; |
| 2248 | int xx, yy, xe; |
| 2249 | m_owner->CalcUnscrolledPosition( rect.x, rect.y, &xx, &yy ); |
| 2250 | for (x_start = 0; colnum < column; colnum++) |
| 2251 | { |
| 2252 | wxDataViewColumn *col = GetOwner()->GetColumnAt(colnum); |
| 2253 | if (col->IsHidden()) |
| 2254 | continue; // skip it! |
| 2255 | |
| 2256 | w = col->GetWidth(); |
| 2257 | x_start += w; |
| 2258 | } |
| 2259 | |
| 2260 | int x_end = x_start + w; |
| 2261 | xe = xx + rect.width; |
| 2262 | if( x_end > xe ) |
| 2263 | { |
| 2264 | sx = ( xx + x_end - xe )/x; |
| 2265 | } |
| 2266 | if( x_start < xx ) |
| 2267 | { |
| 2268 | sx = x_start/x; |
| 2269 | } |
| 2270 | } |
| 2271 | m_owner->Scroll( sx, sy ); |
| 2272 | } |
| 2273 | |
| 2274 | int wxDataViewMainWindow::GetCountPerPage() const |
| 2275 | { |
| 2276 | wxSize size = GetClientSize(); |
| 2277 | return size.y / m_lineHeight; |
| 2278 | } |
| 2279 | |
| 2280 | int wxDataViewMainWindow::GetEndOfLastCol() const |
| 2281 | { |
| 2282 | int width = 0; |
| 2283 | unsigned int i; |
| 2284 | for (i = 0; i < GetOwner()->GetColumnCount(); i++) |
| 2285 | { |
| 2286 | const wxDataViewColumn *c = |
| 2287 | const_cast<wxDataViewCtrl*>(GetOwner())->GetColumnAt( i ); |
| 2288 | |
| 2289 | if (!c->IsHidden()) |
| 2290 | width += c->GetWidth(); |
| 2291 | } |
| 2292 | return width; |
| 2293 | } |
| 2294 | |
| 2295 | unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const |
| 2296 | { |
| 2297 | int x = 0; |
| 2298 | int y = 0; |
| 2299 | m_owner->CalcUnscrolledPosition( x, y, &x, &y ); |
| 2300 | |
| 2301 | return GetLineAt( y ); |
| 2302 | } |
| 2303 | |
| 2304 | unsigned int wxDataViewMainWindow::GetLastVisibleRow() |
| 2305 | { |
| 2306 | wxSize client_size = GetClientSize(); |
| 2307 | m_owner->CalcUnscrolledPosition( client_size.x, client_size.y, |
| 2308 | &client_size.x, &client_size.y ); |
| 2309 | |
| 2310 | // we should deal with the pixel here |
| 2311 | unsigned int row = GetLineAt(client_size.y) - 1; |
| 2312 | |
| 2313 | return wxMin( GetRowCount()-1, row ); |
| 2314 | } |
| 2315 | |
| 2316 | unsigned int wxDataViewMainWindow::GetRowCount() |
| 2317 | { |
| 2318 | if ( m_count == -1 ) |
| 2319 | { |
| 2320 | m_count = RecalculateCount(); |
| 2321 | UpdateDisplay(); |
| 2322 | } |
| 2323 | return m_count; |
| 2324 | } |
| 2325 | |
| 2326 | void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row ) |
| 2327 | { |
| 2328 | m_currentRow = row; |
| 2329 | |
| 2330 | // send event |
| 2331 | } |
| 2332 | |
| 2333 | void wxDataViewMainWindow::SelectAllRows( bool on ) |
| 2334 | { |
| 2335 | if (IsEmpty()) |
| 2336 | return; |
| 2337 | |
| 2338 | if (on) |
| 2339 | { |
| 2340 | m_selection.Clear(); |
| 2341 | for (unsigned int i = 0; i < GetRowCount(); i++) |
| 2342 | m_selection.Add( i ); |
| 2343 | Refresh(); |
| 2344 | } |
| 2345 | else |
| 2346 | { |
| 2347 | unsigned int first_visible = GetFirstVisibleRow(); |
| 2348 | unsigned int last_visible = GetLastVisibleRow(); |
| 2349 | unsigned int i; |
| 2350 | for (i = 0; i < m_selection.GetCount(); i++) |
| 2351 | { |
| 2352 | unsigned int row = m_selection[i]; |
| 2353 | if ((row >= first_visible) && (row <= last_visible)) |
| 2354 | RefreshRow( row ); |
| 2355 | } |
| 2356 | m_selection.Clear(); |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | void wxDataViewMainWindow::SelectRow( unsigned int row, bool on ) |
| 2361 | { |
| 2362 | if (m_selection.Index( row ) == wxNOT_FOUND) |
| 2363 | { |
| 2364 | if (on) |
| 2365 | { |
| 2366 | m_selection.Add( row ); |
| 2367 | RefreshRow( row ); |
| 2368 | } |
| 2369 | } |
| 2370 | else |
| 2371 | { |
| 2372 | if (!on) |
| 2373 | { |
| 2374 | m_selection.Remove( row ); |
| 2375 | RefreshRow( row ); |
| 2376 | } |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on ) |
| 2381 | { |
| 2382 | if (from > to) |
| 2383 | { |
| 2384 | unsigned int tmp = from; |
| 2385 | from = to; |
| 2386 | to = tmp; |
| 2387 | } |
| 2388 | |
| 2389 | unsigned int i; |
| 2390 | for (i = from; i <= to; i++) |
| 2391 | { |
| 2392 | if (m_selection.Index( i ) == wxNOT_FOUND) |
| 2393 | { |
| 2394 | if (on) |
| 2395 | m_selection.Add( i ); |
| 2396 | } |
| 2397 | else |
| 2398 | { |
| 2399 | if (!on) |
| 2400 | m_selection.Remove( i ); |
| 2401 | } |
| 2402 | } |
| 2403 | RefreshRows( from, to ); |
| 2404 | } |
| 2405 | |
| 2406 | void wxDataViewMainWindow::Select( const wxArrayInt& aSelections ) |
| 2407 | { |
| 2408 | for (size_t i=0; i < aSelections.GetCount(); i++) |
| 2409 | { |
| 2410 | int n = aSelections[i]; |
| 2411 | |
| 2412 | m_selection.Add( n ); |
| 2413 | RefreshRow( n ); |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | void wxDataViewMainWindow::ReverseRowSelection( unsigned int row ) |
| 2418 | { |
| 2419 | if (m_selection.Index( row ) == wxNOT_FOUND) |
| 2420 | m_selection.Add( row ); |
| 2421 | else |
| 2422 | m_selection.Remove( row ); |
| 2423 | RefreshRow( row ); |
| 2424 | } |
| 2425 | |
| 2426 | bool wxDataViewMainWindow::IsRowSelected( unsigned int row ) |
| 2427 | { |
| 2428 | return (m_selection.Index( row ) != wxNOT_FOUND); |
| 2429 | } |
| 2430 | |
| 2431 | void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem& item) |
| 2432 | { |
| 2433 | wxWindow *parent = GetParent(); |
| 2434 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, parent->GetId()); |
| 2435 | |
| 2436 | le.SetEventObject(parent); |
| 2437 | le.SetModel(GetOwner()->GetModel()); |
| 2438 | le.SetItem( item ); |
| 2439 | |
| 2440 | parent->GetEventHandler()->ProcessEvent(le); |
| 2441 | } |
| 2442 | |
| 2443 | void wxDataViewMainWindow::RefreshRow( unsigned int row ) |
| 2444 | { |
| 2445 | wxRect rect( 0, GetLineStart( row ), GetEndOfLastCol(), GetLineHeight( row ) ); |
| 2446 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
| 2447 | |
| 2448 | wxSize client_size = GetClientSize(); |
| 2449 | wxRect client_rect( 0, 0, client_size.x, client_size.y ); |
| 2450 | wxRect intersect_rect = client_rect.Intersect( rect ); |
| 2451 | if (intersect_rect.width > 0) |
| 2452 | Refresh( true, &intersect_rect ); |
| 2453 | } |
| 2454 | |
| 2455 | void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to ) |
| 2456 | { |
| 2457 | if (from > to) |
| 2458 | { |
| 2459 | unsigned int tmp = to; |
| 2460 | to = from; |
| 2461 | from = tmp; |
| 2462 | } |
| 2463 | |
| 2464 | wxRect rect( 0, GetLineStart( from ), GetEndOfLastCol(), GetLineStart( (to-from+1) ) ); |
| 2465 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
| 2466 | |
| 2467 | wxSize client_size = GetClientSize(); |
| 2468 | wxRect client_rect( 0, 0, client_size.x, client_size.y ); |
| 2469 | wxRect intersect_rect = client_rect.Intersect( rect ); |
| 2470 | if (intersect_rect.width > 0) |
| 2471 | Refresh( true, &intersect_rect ); |
| 2472 | } |
| 2473 | |
| 2474 | void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow ) |
| 2475 | { |
| 2476 | wxSize client_size = GetClientSize(); |
| 2477 | int start = GetLineStart( firstRow ); |
| 2478 | m_owner->CalcScrolledPosition( start, 0, &start, NULL ); |
| 2479 | if (start > client_size.y) return; |
| 2480 | |
| 2481 | wxRect rect( 0, start, client_size.x, client_size.y - start ); |
| 2482 | |
| 2483 | Refresh( true, &rect ); |
| 2484 | } |
| 2485 | |
| 2486 | void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event) |
| 2487 | { |
| 2488 | wxCHECK_RET( newCurrent < GetRowCount(), |
| 2489 | _T("invalid item index in OnArrowChar()") ); |
| 2490 | |
| 2491 | // if there is no selection, we cannot move it anywhere |
| 2492 | if (!HasCurrentRow()) |
| 2493 | return; |
| 2494 | |
| 2495 | unsigned int oldCurrent = m_currentRow; |
| 2496 | |
| 2497 | // in single selection we just ignore Shift as we can't select several |
| 2498 | // items anyhow |
| 2499 | if ( event.ShiftDown() && !IsSingleSel() ) |
| 2500 | { |
| 2501 | RefreshRow( oldCurrent ); |
| 2502 | |
| 2503 | ChangeCurrentRow( newCurrent ); |
| 2504 | |
| 2505 | // select all the items between the old and the new one |
| 2506 | if ( oldCurrent > newCurrent ) |
| 2507 | { |
| 2508 | newCurrent = oldCurrent; |
| 2509 | oldCurrent = m_currentRow; |
| 2510 | } |
| 2511 | |
| 2512 | SelectRows( oldCurrent, newCurrent, true ); |
| 2513 | if (oldCurrent!=newCurrent) |
| 2514 | SendSelectionChangedEvent(GetItemByRow(m_selection[0])); |
| 2515 | } |
| 2516 | else // !shift |
| 2517 | { |
| 2518 | RefreshRow( oldCurrent ); |
| 2519 | |
| 2520 | // all previously selected items are unselected unless ctrl is held |
| 2521 | if ( !event.ControlDown() ) |
| 2522 | SelectAllRows(false); |
| 2523 | |
| 2524 | ChangeCurrentRow( newCurrent ); |
| 2525 | |
| 2526 | if ( !event.ControlDown() ) |
| 2527 | { |
| 2528 | SelectRow( m_currentRow, true ); |
| 2529 | SendSelectionChangedEvent(GetItemByRow(m_currentRow)); |
| 2530 | } |
| 2531 | else |
| 2532 | RefreshRow( m_currentRow ); |
| 2533 | } |
| 2534 | |
| 2535 | GetOwner()->EnsureVisible( m_currentRow, -1 ); |
| 2536 | } |
| 2537 | |
| 2538 | wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const |
| 2539 | { |
| 2540 | wxRect rect; |
| 2541 | rect.x = 0; |
| 2542 | rect.y = GetLineStart( row ); |
| 2543 | rect.width = GetEndOfLastCol(); |
| 2544 | rect.height = GetLineHeight( row ); |
| 2545 | |
| 2546 | return rect; |
| 2547 | } |
| 2548 | |
| 2549 | int wxDataViewMainWindow::GetLineStart( unsigned int row ) const |
| 2550 | { |
| 2551 | const wxDataViewModel *model = GetOwner()->GetModel(); |
| 2552 | |
| 2553 | if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) |
| 2554 | { |
| 2555 | // TODO make more efficient |
| 2556 | |
| 2557 | int start = 0; |
| 2558 | |
| 2559 | unsigned int r; |
| 2560 | for (r = 0; r < row; r++) |
| 2561 | { |
| 2562 | const wxDataViewTreeNode* node = GetTreeNodeByRow(r); |
| 2563 | if (!node) return start; |
| 2564 | |
| 2565 | wxDataViewItem item = node->GetItem(); |
| 2566 | |
| 2567 | if (node && !node->HasChildren()) |
| 2568 | { |
| 2569 | // Yes, if the node does not have any child, it must be a leaf which |
| 2570 | // mean that it is a temporarily created by GetTreeNodeByRow |
| 2571 | wxDELETE(node); |
| 2572 | } |
| 2573 | |
| 2574 | unsigned int cols = GetOwner()->GetColumnCount(); |
| 2575 | unsigned int col; |
| 2576 | int height = m_lineHeight; |
| 2577 | for (col = 0; col < cols; col++) |
| 2578 | { |
| 2579 | const wxDataViewColumn *column = GetOwner()->GetColumn(col); |
| 2580 | if (column->IsHidden()) |
| 2581 | continue; // skip it! |
| 2582 | |
| 2583 | if ((col != 0) && |
| 2584 | model->IsContainer(item) && |
| 2585 | !model->HasContainerColumns(item)) |
| 2586 | continue; // skip it! |
| 2587 | |
| 2588 | wxVariant value; |
| 2589 | model->GetValue( value, item, column->GetModelColumn() ); |
| 2590 | |
| 2591 | wxDataViewRenderer *renderer = |
| 2592 | const_cast<wxDataViewRenderer*>(column->GetRenderer()); |
| 2593 | renderer->SetValue( value ); |
| 2594 | height = wxMax( height, renderer->GetSize().y ); |
| 2595 | } |
| 2596 | |
| 2597 | start += height; |
| 2598 | } |
| 2599 | |
| 2600 | return start; |
| 2601 | } |
| 2602 | else |
| 2603 | { |
| 2604 | return row * m_lineHeight; |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | int wxDataViewMainWindow::GetLineAt( unsigned int y ) const |
| 2609 | { |
| 2610 | const wxDataViewModel *model = GetOwner()->GetModel(); |
| 2611 | |
| 2612 | // check for the easy case first |
| 2613 | if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT) ) |
| 2614 | return y / m_lineHeight; |
| 2615 | |
| 2616 | // TODO make more efficient |
| 2617 | unsigned int row = 0; |
| 2618 | unsigned int yy = 0; |
| 2619 | for (;;) |
| 2620 | { |
| 2621 | const wxDataViewTreeNode* node = GetTreeNodeByRow(row); |
| 2622 | if (!node) |
| 2623 | { |
| 2624 | // not really correct... |
| 2625 | return row + ((y-yy) / m_lineHeight); |
| 2626 | } |
| 2627 | |
| 2628 | wxDataViewItem item = node->GetItem(); |
| 2629 | |
| 2630 | if (node && !node->HasChildren()) |
| 2631 | { |
| 2632 | // Yes, if the node does not have any child, it must be a leaf which |
| 2633 | // mean that it is a temporarily created by GetTreeNodeByRow |
| 2634 | wxDELETE(node); |
| 2635 | } |
| 2636 | |
| 2637 | unsigned int cols = GetOwner()->GetColumnCount(); |
| 2638 | unsigned int col; |
| 2639 | int height = m_lineHeight; |
| 2640 | for (col = 0; col < cols; col++) |
| 2641 | { |
| 2642 | const wxDataViewColumn *column = GetOwner()->GetColumn(col); |
| 2643 | if (column->IsHidden()) |
| 2644 | continue; // skip it! |
| 2645 | |
| 2646 | if ((col != 0) && |
| 2647 | model->IsContainer(item) && |
| 2648 | !model->HasContainerColumns(item)) |
| 2649 | continue; // skip it! |
| 2650 | |
| 2651 | wxVariant value; |
| 2652 | model->GetValue( value, item, column->GetModelColumn() ); |
| 2653 | |
| 2654 | wxDataViewRenderer *renderer = |
| 2655 | const_cast<wxDataViewRenderer*>(column->GetRenderer()); |
| 2656 | renderer->SetValue( value ); |
| 2657 | height = wxMax( height, renderer->GetSize().y ); |
| 2658 | } |
| 2659 | |
| 2660 | yy += height; |
| 2661 | if (y < yy) |
| 2662 | return row; |
| 2663 | |
| 2664 | row++; |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | int wxDataViewMainWindow::GetLineHeight( unsigned int row ) const |
| 2669 | { |
| 2670 | const wxDataViewModel *model = GetOwner()->GetModel(); |
| 2671 | |
| 2672 | if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) |
| 2673 | { |
| 2674 | wxASSERT( !IsVirtualList() ); |
| 2675 | |
| 2676 | const wxDataViewTreeNode* node = GetTreeNodeByRow(row); |
| 2677 | // wxASSERT( node ); |
| 2678 | if (!node) return m_lineHeight; |
| 2679 | |
| 2680 | wxDataViewItem item = node->GetItem(); |
| 2681 | |
| 2682 | if (node && !node->HasChildren()) |
| 2683 | { |
| 2684 | // Yes, if the node does not have any child, it must be a leaf which |
| 2685 | // mean that it is a temporarily created by GetTreeNodeByRow |
| 2686 | wxDELETE(node); |
| 2687 | } |
| 2688 | |
| 2689 | int height = m_lineHeight; |
| 2690 | |
| 2691 | unsigned int cols = GetOwner()->GetColumnCount(); |
| 2692 | unsigned int col; |
| 2693 | for (col = 0; col < cols; col++) |
| 2694 | { |
| 2695 | const wxDataViewColumn *column = GetOwner()->GetColumn(col); |
| 2696 | if (column->IsHidden()) |
| 2697 | continue; // skip it! |
| 2698 | |
| 2699 | if ((col != 0) && |
| 2700 | model->IsContainer(item) && |
| 2701 | !model->HasContainerColumns(item)) |
| 2702 | continue; // skip it! |
| 2703 | |
| 2704 | wxVariant value; |
| 2705 | model->GetValue( value, item, column->GetModelColumn() ); |
| 2706 | |
| 2707 | wxDataViewRenderer *renderer = |
| 2708 | const_cast<wxDataViewRenderer*>(column->GetRenderer()); |
| 2709 | renderer->SetValue( value ); |
| 2710 | height = wxMax( height, renderer->GetSize().y ); |
| 2711 | } |
| 2712 | |
| 2713 | return height; |
| 2714 | } |
| 2715 | else |
| 2716 | { |
| 2717 | return m_lineHeight; |
| 2718 | } |
| 2719 | } |
| 2720 | |
| 2721 | class RowToItemJob: public DoJob |
| 2722 | { |
| 2723 | public: |
| 2724 | RowToItemJob( unsigned int row , int current ) |
| 2725 | { this->row = row; this->current = current; } |
| 2726 | virtual ~RowToItemJob() {} |
| 2727 | |
| 2728 | virtual int operator() ( wxDataViewTreeNode * node ) |
| 2729 | { |
| 2730 | current ++; |
| 2731 | if( current == static_cast<int>(row)) |
| 2732 | { |
| 2733 | ret = node->GetItem(); |
| 2734 | return DoJob::OK; |
| 2735 | } |
| 2736 | |
| 2737 | if( node->GetSubTreeCount() + current < static_cast<int>(row) ) |
| 2738 | { |
| 2739 | current += node->GetSubTreeCount(); |
| 2740 | return DoJob::IGR; |
| 2741 | } |
| 2742 | else |
| 2743 | { |
| 2744 | // If the current has no child node, we can find the desired item of the row |
| 2745 | // number directly. |
| 2746 | // This if can speed up finding in some case, and will has a very good effect |
| 2747 | // when it comes to list view |
| 2748 | if( node->GetNodes().GetCount() == 0) |
| 2749 | { |
| 2750 | int index = static_cast<int>(row) - current - 1; |
| 2751 | ret = node->GetChildren().Item( index ); |
| 2752 | return DoJob::OK; |
| 2753 | } |
| 2754 | return DoJob::CONT; |
| 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | virtual int operator() ( void * n ) |
| 2759 | { |
| 2760 | current ++; |
| 2761 | if( current == static_cast<int>(row)) |
| 2762 | { |
| 2763 | ret = wxDataViewItem( n ); |
| 2764 | return DoJob::OK; |
| 2765 | } |
| 2766 | return DoJob::CONT; |
| 2767 | } |
| 2768 | |
| 2769 | wxDataViewItem GetResult() const |
| 2770 | { return ret; } |
| 2771 | |
| 2772 | private: |
| 2773 | unsigned int row; |
| 2774 | int current; |
| 2775 | wxDataViewItem ret; |
| 2776 | }; |
| 2777 | |
| 2778 | wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const |
| 2779 | { |
| 2780 | if (!m_root) |
| 2781 | { |
| 2782 | return wxDataViewItem( wxUIntToPtr(row) ); |
| 2783 | } |
| 2784 | else |
| 2785 | { |
| 2786 | RowToItemJob job( row, -2 ); |
| 2787 | Walker( m_root , job ); |
| 2788 | return job.GetResult(); |
| 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | class RowToTreeNodeJob: public DoJob |
| 2793 | { |
| 2794 | public: |
| 2795 | RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node ) |
| 2796 | { |
| 2797 | this->row = row; |
| 2798 | this->current = current; |
| 2799 | ret = NULL; |
| 2800 | parent = node; |
| 2801 | } |
| 2802 | virtual ~RowToTreeNodeJob(){ } |
| 2803 | |
| 2804 | virtual int operator() ( wxDataViewTreeNode * node ) |
| 2805 | { |
| 2806 | current ++; |
| 2807 | if( current == static_cast<int>(row)) |
| 2808 | { |
| 2809 | ret = node; |
| 2810 | return DoJob::OK; |
| 2811 | } |
| 2812 | |
| 2813 | if( node->GetSubTreeCount() + current < static_cast<int>(row) ) |
| 2814 | { |
| 2815 | current += node->GetSubTreeCount(); |
| 2816 | return DoJob::IGR; |
| 2817 | } |
| 2818 | else |
| 2819 | { |
| 2820 | parent = node; |
| 2821 | |
| 2822 | // If the current node has no children, we can find the desired item of the |
| 2823 | // row number directly. |
| 2824 | // This if can speed up finding in some case, and will have a very good |
| 2825 | // effect for list views. |
| 2826 | if( node->GetNodes().GetCount() == 0) |
| 2827 | { |
| 2828 | int index = static_cast<int>(row) - current - 1; |
| 2829 | void * n = node->GetChildren().Item( index ); |
| 2830 | ret = new wxDataViewTreeNode( parent ); |
| 2831 | ret->SetItem( wxDataViewItem( n )); |
| 2832 | ret->SetHasChildren(false); |
| 2833 | return DoJob::OK; |
| 2834 | } |
| 2835 | return DoJob::CONT; |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | virtual int operator() ( void * n ) |
| 2840 | { |
| 2841 | current ++; |
| 2842 | if( current == static_cast<int>(row)) |
| 2843 | { |
| 2844 | ret = new wxDataViewTreeNode( parent ); |
| 2845 | ret->SetItem( wxDataViewItem( n )); |
| 2846 | ret->SetHasChildren(false); |
| 2847 | return DoJob::OK; |
| 2848 | } |
| 2849 | |
| 2850 | return DoJob::CONT; |
| 2851 | } |
| 2852 | |
| 2853 | wxDataViewTreeNode * GetResult() const |
| 2854 | { return ret; } |
| 2855 | |
| 2856 | private: |
| 2857 | unsigned int row; |
| 2858 | int current; |
| 2859 | wxDataViewTreeNode * ret; |
| 2860 | wxDataViewTreeNode * parent; |
| 2861 | }; |
| 2862 | |
| 2863 | wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) const |
| 2864 | { |
| 2865 | wxASSERT( !IsVirtualList() ); |
| 2866 | |
| 2867 | RowToTreeNodeJob job( row , -2, m_root ); |
| 2868 | Walker( m_root , job ); |
| 2869 | return job.GetResult(); |
| 2870 | } |
| 2871 | |
| 2872 | wxDataViewEvent wxDataViewMainWindow::SendExpanderEvent( wxEventType type, |
| 2873 | const wxDataViewItem & item ) |
| 2874 | { |
| 2875 | wxWindow *parent = GetParent(); |
| 2876 | wxDataViewEvent le(type, parent->GetId()); |
| 2877 | |
| 2878 | le.SetEventObject(parent); |
| 2879 | le.SetModel(GetOwner()->GetModel()); |
| 2880 | le.SetItem( item ); |
| 2881 | |
| 2882 | parent->GetEventHandler()->ProcessEvent(le); |
| 2883 | return le; |
| 2884 | } |
| 2885 | |
| 2886 | bool wxDataViewMainWindow::IsExpanded( unsigned int row ) const |
| 2887 | { |
| 2888 | if (IsVirtualList()) |
| 2889 | return false; |
| 2890 | |
| 2891 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); |
| 2892 | if (!node) |
| 2893 | return false; |
| 2894 | |
| 2895 | if (!node->HasChildren()) |
| 2896 | { |
| 2897 | delete node; |
| 2898 | return false; |
| 2899 | } |
| 2900 | |
| 2901 | return node->IsOpen(); |
| 2902 | } |
| 2903 | |
| 2904 | bool wxDataViewMainWindow::HasChildren( unsigned int row ) const |
| 2905 | { |
| 2906 | if (IsVirtualList()) |
| 2907 | return false; |
| 2908 | |
| 2909 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); |
| 2910 | if (!node) |
| 2911 | return false; |
| 2912 | |
| 2913 | if (!node->HasChildren()) |
| 2914 | { |
| 2915 | delete node; |
| 2916 | return false; |
| 2917 | } |
| 2918 | |
| 2919 | return true; |
| 2920 | } |
| 2921 | |
| 2922 | void wxDataViewMainWindow::Expand( unsigned int row ) |
| 2923 | { |
| 2924 | if (IsVirtualList()) |
| 2925 | return; |
| 2926 | |
| 2927 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); |
| 2928 | if (!node) |
| 2929 | return; |
| 2930 | |
| 2931 | if (!node->HasChildren()) |
| 2932 | { |
| 2933 | delete node; |
| 2934 | return; |
| 2935 | } |
| 2936 | |
| 2937 | if (!node->IsOpen()) |
| 2938 | { |
| 2939 | wxDataViewEvent e = |
| 2940 | SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, node->GetItem()); |
| 2941 | |
| 2942 | // Check if the user prevent expanding |
| 2943 | if( e.GetSkipped() ) |
| 2944 | return; |
| 2945 | |
| 2946 | node->ToggleOpen(); |
| 2947 | |
| 2948 | // build the children of current node |
| 2949 | if( node->GetChildrenNumber() == 0 ) |
| 2950 | { |
| 2951 | SortPrepare(); |
| 2952 | ::BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node); |
| 2953 | } |
| 2954 | |
| 2955 | // By expanding the node all row indices that are currently in the selection list |
| 2956 | // and are greater than our node have become invalid. So we have to correct that now. |
| 2957 | const unsigned rowAdjustment = node->GetSubTreeCount(); |
| 2958 | for(unsigned i=0; i<m_selection.size(); ++i) |
| 2959 | { |
| 2960 | const unsigned testRow = m_selection[i]; |
| 2961 | // all rows above us are not affected, so skip them |
| 2962 | if(testRow <= row) |
| 2963 | continue; |
| 2964 | |
| 2965 | m_selection[i] += rowAdjustment; |
| 2966 | } |
| 2967 | |
| 2968 | if(m_currentRow > row) |
| 2969 | ChangeCurrentRow(m_currentRow + rowAdjustment); |
| 2970 | |
| 2971 | m_count = -1; |
| 2972 | UpdateDisplay(); |
| 2973 | // Send the expanded event |
| 2974 | SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,node->GetItem()); |
| 2975 | } |
| 2976 | } |
| 2977 | |
| 2978 | void wxDataViewMainWindow::Collapse(unsigned int row) |
| 2979 | { |
| 2980 | if (IsVirtualList()) |
| 2981 | return; |
| 2982 | |
| 2983 | wxDataViewTreeNode *node = GetTreeNodeByRow(row); |
| 2984 | if (!node) |
| 2985 | return; |
| 2986 | |
| 2987 | if (!node->HasChildren()) |
| 2988 | { |
| 2989 | delete node; |
| 2990 | return; |
| 2991 | } |
| 2992 | |
| 2993 | if (node->IsOpen()) |
| 2994 | { |
| 2995 | wxDataViewEvent e = |
| 2996 | SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,node->GetItem()); |
| 2997 | if( e.GetSkipped() ) |
| 2998 | return; |
| 2999 | |
| 3000 | // Find out if there are selected items below the current node. |
| 3001 | bool selectCollapsingRow = false; |
| 3002 | const unsigned rowAdjustment = node->GetSubTreeCount(); |
| 3003 | unsigned maxRowToBeTested = row + rowAdjustment; |
| 3004 | for(unsigned i=0; i<m_selection.size(); ++i) |
| 3005 | { |
| 3006 | const unsigned testRow = m_selection[i]; |
| 3007 | if(testRow > row && testRow <= maxRowToBeTested) |
| 3008 | { |
| 3009 | selectCollapsingRow = true; |
| 3010 | // get out as soon as we have found a node that is selected |
| 3011 | break; |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | node->ToggleOpen(); |
| 3016 | |
| 3017 | // If the node to be closed has selected items the user won't see those any longer. |
| 3018 | // We select the collapsing node in this case. |
| 3019 | if(selectCollapsingRow) |
| 3020 | { |
| 3021 | SelectAllRows(false); |
| 3022 | ChangeCurrentRow(row); |
| 3023 | SelectRow(row, true); |
| 3024 | SendSelectionChangedEvent(GetItemByRow(row)); |
| 3025 | } |
| 3026 | else |
| 3027 | { |
| 3028 | // if there were no selected items below our node we still need to "fix" the |
| 3029 | // selection list to adjust for the changing of the row indices. |
| 3030 | // We actually do the opposite of what we are doing in Expand(). |
| 3031 | for(unsigned i=0; i<m_selection.size(); ++i) |
| 3032 | { |
| 3033 | const unsigned testRow = m_selection[i]; |
| 3034 | // all rows above us are not affected, so skip them |
| 3035 | if(testRow <= row) |
| 3036 | continue; |
| 3037 | |
| 3038 | m_selection[i] -= rowAdjustment; |
| 3039 | } |
| 3040 | |
| 3041 | // if the "current row" is being collapsed away we change it to the current row ;-) |
| 3042 | if(m_currentRow > row && m_currentRow <= maxRowToBeTested) |
| 3043 | ChangeCurrentRow(row); |
| 3044 | else if(m_currentRow > row) |
| 3045 | ChangeCurrentRow(m_currentRow - rowAdjustment); |
| 3046 | } |
| 3047 | |
| 3048 | m_count = -1; |
| 3049 | UpdateDisplay(); |
| 3050 | SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,node->GetItem()); |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item ) |
| 3055 | { |
| 3056 | wxDataViewModel * model = GetOwner()->GetModel(); |
| 3057 | if( model == NULL ) |
| 3058 | return NULL; |
| 3059 | |
| 3060 | if (!item.IsOk()) |
| 3061 | return m_root; |
| 3062 | |
| 3063 | // Compose the a parent-chain of the finding item |
| 3064 | ItemList list; |
| 3065 | list.DeleteContents( true ); |
| 3066 | wxDataViewItem it( item ); |
| 3067 | while( it.IsOk() ) |
| 3068 | { |
| 3069 | wxDataViewItem * pItem = new wxDataViewItem( it ); |
| 3070 | list.Insert( pItem ); |
| 3071 | it = model->GetParent( it ); |
| 3072 | } |
| 3073 | |
| 3074 | // Find the item along the parent-chain. |
| 3075 | // This algorithm is designed to speed up the node-finding method |
| 3076 | wxDataViewTreeNode * node = m_root; |
| 3077 | for( ItemList::const_iterator iter = list.begin(); iter !=list.end(); iter++ ) |
| 3078 | { |
| 3079 | if( node->HasChildren() ) |
| 3080 | { |
| 3081 | if( node->GetChildrenNumber() == 0 ) |
| 3082 | { |
| 3083 | SortPrepare(); |
| 3084 | ::BuildTreeHelper(model, node->GetItem(), node); |
| 3085 | } |
| 3086 | |
| 3087 | wxDataViewTreeNodes nodes = node->GetNodes(); |
| 3088 | unsigned int i; |
| 3089 | bool found = false; |
| 3090 | |
| 3091 | for (i = 0; i < nodes.GetCount(); i ++) |
| 3092 | { |
| 3093 | if (nodes[i]->GetItem() == (**iter)) |
| 3094 | { |
| 3095 | if (nodes[i]->GetItem() == item) |
| 3096 | return nodes[i]; |
| 3097 | |
| 3098 | node = nodes[i]; |
| 3099 | found = true; |
| 3100 | break; |
| 3101 | } |
| 3102 | } |
| 3103 | if (!found) |
| 3104 | return NULL; |
| 3105 | } |
| 3106 | else |
| 3107 | return NULL; |
| 3108 | } |
| 3109 | return NULL; |
| 3110 | } |
| 3111 | |
| 3112 | void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item, |
| 3113 | wxDataViewColumn* &column ) |
| 3114 | { |
| 3115 | wxDataViewColumn *col = NULL; |
| 3116 | unsigned int cols = GetOwner()->GetColumnCount(); |
| 3117 | unsigned int colnum = 0; |
| 3118 | int x, y; |
| 3119 | m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y ); |
| 3120 | for (unsigned x_start = 0; colnum < cols; colnum++) |
| 3121 | { |
| 3122 | col = GetOwner()->GetColumnAt(colnum); |
| 3123 | if (col->IsHidden()) |
| 3124 | continue; // skip it! |
| 3125 | |
| 3126 | unsigned int w = col->GetWidth(); |
| 3127 | if (x_start+w >= (unsigned int)x) |
| 3128 | break; |
| 3129 | |
| 3130 | x_start += w; |
| 3131 | } |
| 3132 | |
| 3133 | column = col; |
| 3134 | item = GetItemByRow( GetLineAt( y ) ); |
| 3135 | } |
| 3136 | |
| 3137 | wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item, |
| 3138 | const wxDataViewColumn* column ) |
| 3139 | { |
| 3140 | int xpos = 0; |
| 3141 | int width = 0; |
| 3142 | |
| 3143 | unsigned int cols = GetOwner()->GetColumnCount(); |
| 3144 | // If column is null the loop will compute the combined width of all columns. |
| 3145 | // Otherwise, it will compute the x position of the column we are looking for. |
| 3146 | for (unsigned int i = 0; i < cols; i++) |
| 3147 | { |
| 3148 | wxDataViewColumn* col = GetOwner()->GetColumnAt( i ); |
| 3149 | |
| 3150 | if (col == column) |
| 3151 | break; |
| 3152 | |
| 3153 | if (col->IsHidden()) |
| 3154 | continue; // skip it! |
| 3155 | |
| 3156 | xpos += col->GetWidth(); |
| 3157 | width += col->GetWidth(); |
| 3158 | } |
| 3159 | |
| 3160 | if(column != 0) |
| 3161 | { |
| 3162 | // If we have a column, we need can get its width directly. |
| 3163 | if(column->IsHidden()) |
| 3164 | width = 0; |
| 3165 | else |
| 3166 | width = column->GetWidth(); |
| 3167 | |
| 3168 | } |
| 3169 | else |
| 3170 | { |
| 3171 | // If we have no column, we reset the x position back to zero. |
| 3172 | xpos = 0; |
| 3173 | } |
| 3174 | |
| 3175 | // we have to take an expander column into account and compute its indentation |
| 3176 | // to get the correct x position where the actual text is |
| 3177 | int indent = 0; |
| 3178 | int row = GetRowByItem(item); |
| 3179 | if (!IsVirtualList() && (column == 0 || GetOwner()->GetExpanderColumn() == column) ) |
| 3180 | { |
| 3181 | wxDataViewTreeNode* node = GetTreeNodeByRow(row); |
| 3182 | indent = GetOwner()->GetIndent() * node->GetIndentLevel(); |
| 3183 | indent = indent + m_lineHeight; // use m_lineHeight as the width of the expander |
| 3184 | |
| 3185 | if(!node->HasChildren()) |
| 3186 | delete node; |
| 3187 | } |
| 3188 | |
| 3189 | wxRect itemRect( xpos + indent, |
| 3190 | GetLineStart( row ), |
| 3191 | width - indent, |
| 3192 | GetLineHeight( row ) ); |
| 3193 | |
| 3194 | GetOwner()->CalcScrolledPosition( itemRect.x, itemRect.y, |
| 3195 | &itemRect.x, &itemRect.y ); |
| 3196 | |
| 3197 | return itemRect; |
| 3198 | } |
| 3199 | |
| 3200 | int wxDataViewMainWindow::RecalculateCount() |
| 3201 | { |
| 3202 | if (!m_root) |
| 3203 | { |
| 3204 | wxDataViewIndexListModel *list_model = |
| 3205 | (wxDataViewIndexListModel*) GetOwner()->GetModel(); |
| 3206 | #ifndef __WXMAC__ |
| 3207 | return list_model->GetLastIndex() + 1; |
| 3208 | #else |
| 3209 | return list_model->GetLastIndex() - 1; |
| 3210 | #endif |
| 3211 | } |
| 3212 | else |
| 3213 | { |
| 3214 | return m_root->GetSubTreeCount(); |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | class ItemToRowJob : public DoJob |
| 3219 | { |
| 3220 | public: |
| 3221 | ItemToRowJob(const wxDataViewItem& item_, ItemList::const_iterator iter) |
| 3222 | : m_iter(iter), |
| 3223 | item(item_) |
| 3224 | { |
| 3225 | ret = -1; |
| 3226 | } |
| 3227 | |
| 3228 | // Maybe binary search will help to speed up this process |
| 3229 | virtual int operator() ( wxDataViewTreeNode * node) |
| 3230 | { |
| 3231 | ret ++; |
| 3232 | if( node->GetItem() == item ) |
| 3233 | { |
| 3234 | return DoJob::OK; |
| 3235 | } |
| 3236 | |
| 3237 | if( node->GetItem() == **m_iter ) |
| 3238 | { |
| 3239 | m_iter++; |
| 3240 | return DoJob::CONT; |
| 3241 | } |
| 3242 | else |
| 3243 | { |
| 3244 | ret += node->GetSubTreeCount(); |
| 3245 | return DoJob::IGR; |
| 3246 | } |
| 3247 | |
| 3248 | } |
| 3249 | |
| 3250 | virtual int operator() ( void * n ) |
| 3251 | { |
| 3252 | ret ++; |
| 3253 | if( n == item.GetID() ) |
| 3254 | return DoJob::OK; |
| 3255 | return DoJob::CONT; |
| 3256 | } |
| 3257 | |
| 3258 | // the row number is begin from zero |
| 3259 | int GetResult() const |
| 3260 | { return ret -1; } |
| 3261 | |
| 3262 | private: |
| 3263 | ItemList::const_iterator m_iter; |
| 3264 | wxDataViewItem item; |
| 3265 | int ret; |
| 3266 | |
| 3267 | }; |
| 3268 | |
| 3269 | int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) const |
| 3270 | { |
| 3271 | const wxDataViewModel * model = GetOwner()->GetModel(); |
| 3272 | if( model == NULL ) |
| 3273 | return -1; |
| 3274 | |
| 3275 | if (!m_root) |
| 3276 | { |
| 3277 | return wxPtrToUInt( item.GetID() ); |
| 3278 | } |
| 3279 | else |
| 3280 | { |
| 3281 | if( !item.IsOk() ) |
| 3282 | return -1; |
| 3283 | |
| 3284 | // Compose the a parent-chain of the finding item |
| 3285 | ItemList list; |
| 3286 | wxDataViewItem * pItem; |
| 3287 | list.DeleteContents( true ); |
| 3288 | wxDataViewItem it( item ); |
| 3289 | while( it.IsOk() ) |
| 3290 | { |
| 3291 | pItem = new wxDataViewItem( it ); |
| 3292 | list.Insert( pItem ); |
| 3293 | it = model->GetParent( it ); |
| 3294 | } |
| 3295 | pItem = new wxDataViewItem( ); |
| 3296 | list.Insert( pItem ); |
| 3297 | |
| 3298 | ItemToRowJob job( item, list.begin() ); |
| 3299 | Walker(m_root , job ); |
| 3300 | return job.GetResult(); |
| 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | static void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, |
| 3305 | wxDataViewTreeNode * node) |
| 3306 | { |
| 3307 | if( !model->IsContainer( item ) ) |
| 3308 | return; |
| 3309 | |
| 3310 | wxDataViewItemArray children; |
| 3311 | unsigned int num = model->GetChildren( item, children); |
| 3312 | |
| 3313 | unsigned int index = 0; |
| 3314 | while( index < num ) |
| 3315 | { |
| 3316 | if( model->IsContainer( children[index] ) ) |
| 3317 | { |
| 3318 | wxDataViewTreeNode * n = new wxDataViewTreeNode( node ); |
| 3319 | n->SetItem(children[index]); |
| 3320 | n->SetHasChildren( true ); |
| 3321 | node->AddNode( n ); |
| 3322 | } |
| 3323 | else |
| 3324 | { |
| 3325 | node->AddLeaf( children[index].GetID() ); |
| 3326 | } |
| 3327 | index ++; |
| 3328 | } |
| 3329 | node->SetSubTreeCount( num ); |
| 3330 | wxDataViewTreeNode * n = node->GetParent(); |
| 3331 | if( n != NULL) |
| 3332 | n->ChangeSubTreeCount(num); |
| 3333 | |
| 3334 | } |
| 3335 | |
| 3336 | void wxDataViewMainWindow::BuildTree(wxDataViewModel * model) |
| 3337 | { |
| 3338 | DestroyTree(); |
| 3339 | |
| 3340 | if (GetOwner()->GetModel()->IsVirtualListModel()) |
| 3341 | { |
| 3342 | m_count = -1; |
| 3343 | return; |
| 3344 | } |
| 3345 | |
| 3346 | m_root = new wxDataViewTreeNode( NULL ); |
| 3347 | m_root->SetHasChildren(true); |
| 3348 | |
| 3349 | // First we define a invalid item to fetch the top-level elements |
| 3350 | wxDataViewItem item; |
| 3351 | SortPrepare(); |
| 3352 | BuildTreeHelper( model, item, m_root); |
| 3353 | m_count = -1; |
| 3354 | } |
| 3355 | |
| 3356 | static void DestroyTreeHelper( wxDataViewTreeNode * node ) |
| 3357 | { |
| 3358 | if( node->GetNodeNumber() != 0 ) |
| 3359 | { |
| 3360 | int len = node->GetNodeNumber(); |
| 3361 | wxDataViewTreeNodes& nodes = node->GetNodes(); |
| 3362 | for (int i = 0; i < len; i++) |
| 3363 | DestroyTreeHelper(nodes[i]); |
| 3364 | } |
| 3365 | delete node; |
| 3366 | } |
| 3367 | |
| 3368 | void wxDataViewMainWindow::DestroyTree() |
| 3369 | { |
| 3370 | if (!IsVirtualList()) |
| 3371 | { |
| 3372 | ::DestroyTreeHelper(m_root); |
| 3373 | m_count = 0; |
| 3374 | m_root = NULL; |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | void wxDataViewMainWindow::OnChar( wxKeyEvent &event ) |
| 3379 | { |
| 3380 | if ( GetParent()->HandleAsNavigationKey(event) ) |
| 3381 | return; |
| 3382 | |
| 3383 | // no item -> nothing to do |
| 3384 | if (!HasCurrentRow()) |
| 3385 | { |
| 3386 | event.Skip(); |
| 3387 | return; |
| 3388 | } |
| 3389 | |
| 3390 | // don't use m_linesPerPage directly as it might not be computed yet |
| 3391 | const int pageSize = GetCountPerPage(); |
| 3392 | wxCHECK_RET( pageSize, _T("should have non zero page size") ); |
| 3393 | |
| 3394 | switch ( event.GetKeyCode() ) |
| 3395 | { |
| 3396 | case WXK_RETURN: |
| 3397 | { |
| 3398 | wxWindow *parent = GetParent(); |
| 3399 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, |
| 3400 | parent->GetId()); |
| 3401 | le.SetItem( GetItemByRow(m_currentRow) ); |
| 3402 | le.SetEventObject(parent); |
| 3403 | le.SetModel(GetOwner()->GetModel()); |
| 3404 | |
| 3405 | parent->GetEventHandler()->ProcessEvent(le); |
| 3406 | } |
| 3407 | break; |
| 3408 | |
| 3409 | case WXK_UP: |
| 3410 | if ( m_currentRow > 0 ) |
| 3411 | OnArrowChar( m_currentRow - 1, event ); |
| 3412 | break; |
| 3413 | |
| 3414 | case WXK_DOWN: |
| 3415 | if ( m_currentRow < GetRowCount() - 1 ) |
| 3416 | OnArrowChar( m_currentRow + 1, event ); |
| 3417 | break; |
| 3418 | // Add the process for tree expanding/collapsing |
| 3419 | case WXK_LEFT: |
| 3420 | { |
| 3421 | if (IsVirtualList()) |
| 3422 | break; |
| 3423 | |
| 3424 | wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow); |
| 3425 | if (!node) |
| 3426 | break; |
| 3427 | |
| 3428 | if (node->HasChildren() && node->IsOpen()) |
| 3429 | { |
| 3430 | Collapse(m_currentRow); |
| 3431 | } |
| 3432 | else // if the node is already closed we move the selection to its parent |
| 3433 | { |
| 3434 | wxDataViewTreeNode *parent_node = node->GetParent(); |
| 3435 | |
| 3436 | if(!node->HasChildren()) |
| 3437 | delete node; |
| 3438 | |
| 3439 | if (parent_node) |
| 3440 | { |
| 3441 | int parent = GetRowByItem( parent_node->GetItem() ); |
| 3442 | if ( parent >= 0 ) |
| 3443 | { |
| 3444 | unsigned int row = m_currentRow; |
| 3445 | SelectRow( row, false); |
| 3446 | SelectRow( parent, true ); |
| 3447 | ChangeCurrentRow( parent ); |
| 3448 | GetOwner()->EnsureVisible( parent, -1 ); |
| 3449 | SendSelectionChangedEvent( parent_node->GetItem() ); |
| 3450 | } |
| 3451 | } |
| 3452 | } |
| 3453 | break; |
| 3454 | } |
| 3455 | case WXK_RIGHT: |
| 3456 | { |
| 3457 | if (!IsExpanded( m_currentRow )) |
| 3458 | Expand( m_currentRow ); |
| 3459 | else |
| 3460 | { |
| 3461 | unsigned int row = m_currentRow; |
| 3462 | SelectRow( row, false ); |
| 3463 | SelectRow( row + 1, true ); |
| 3464 | ChangeCurrentRow( row + 1 ); |
| 3465 | GetOwner()->EnsureVisible( row + 1, -1 ); |
| 3466 | SendSelectionChangedEvent( GetItemByRow(row+1) ); |
| 3467 | } |
| 3468 | break; |
| 3469 | } |
| 3470 | case WXK_END: |
| 3471 | { |
| 3472 | if (!IsEmpty()) |
| 3473 | OnArrowChar( GetRowCount() - 1, event ); |
| 3474 | break; |
| 3475 | } |
| 3476 | case WXK_HOME: |
| 3477 | if (!IsEmpty()) |
| 3478 | OnArrowChar( 0, event ); |
| 3479 | break; |
| 3480 | |
| 3481 | case WXK_PAGEUP: |
| 3482 | { |
| 3483 | int steps = pageSize - 1; |
| 3484 | int index = m_currentRow - steps; |
| 3485 | if (index < 0) |
| 3486 | index = 0; |
| 3487 | |
| 3488 | OnArrowChar( index, event ); |
| 3489 | } |
| 3490 | break; |
| 3491 | |
| 3492 | case WXK_PAGEDOWN: |
| 3493 | { |
| 3494 | int steps = pageSize - 1; |
| 3495 | unsigned int index = m_currentRow + steps; |
| 3496 | unsigned int count = GetRowCount(); |
| 3497 | if ( index >= count ) |
| 3498 | index = count - 1; |
| 3499 | |
| 3500 | OnArrowChar( index, event ); |
| 3501 | } |
| 3502 | break; |
| 3503 | |
| 3504 | case WXK_F2: |
| 3505 | { |
| 3506 | if(m_selection.size() == 1) |
| 3507 | { |
| 3508 | // TODO: we need to revise that when we have a concept for a 'current column' |
| 3509 | GetOwner()->StartEditor(GetItemByRow(m_selection[0]), 0); |
| 3510 | } |
| 3511 | } |
| 3512 | break; |
| 3513 | |
| 3514 | default: |
| 3515 | event.Skip(); |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) |
| 3520 | { |
| 3521 | if (event.GetEventType() == wxEVT_MOUSEWHEEL) |
| 3522 | { |
| 3523 | // let the base handle mouse wheel events. |
| 3524 | event.Skip(); |
| 3525 | return; |
| 3526 | } |
| 3527 | |
| 3528 | int x = event.GetX(); |
| 3529 | int y = event.GetY(); |
| 3530 | m_owner->CalcUnscrolledPosition( x, y, &x, &y ); |
| 3531 | wxDataViewColumn *col = NULL; |
| 3532 | |
| 3533 | int xpos = 0; |
| 3534 | unsigned int cols = GetOwner()->GetColumnCount(); |
| 3535 | unsigned int i; |
| 3536 | for (i = 0; i < cols; i++) |
| 3537 | { |
| 3538 | wxDataViewColumn *c = GetOwner()->GetColumnAt( i ); |
| 3539 | if (c->IsHidden()) |
| 3540 | continue; // skip it! |
| 3541 | |
| 3542 | if (x < xpos + c->GetWidth()) |
| 3543 | { |
| 3544 | col = c; |
| 3545 | break; |
| 3546 | } |
| 3547 | xpos += c->GetWidth(); |
| 3548 | } |
| 3549 | if (!col) |
| 3550 | return; |
| 3551 | |
| 3552 | wxDataViewRenderer *cell = col->GetRenderer(); |
| 3553 | unsigned int current = GetLineAt( y ); |
| 3554 | if ((current >= GetRowCount()) || (x > GetEndOfLastCol())) |
| 3555 | { |
| 3556 | // Unselect all if below the last row ? |
| 3557 | return; |
| 3558 | } |
| 3559 | |
| 3560 | // Test whether the mouse is hovered on the tree item button |
| 3561 | bool hoverOverExpander = false; |
| 3562 | if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col)) |
| 3563 | { |
| 3564 | wxDataViewTreeNode * node = GetTreeNodeByRow(current); |
| 3565 | if( node!=NULL && node->HasChildren() ) |
| 3566 | { |
| 3567 | int indent = node->GetIndentLevel(); |
| 3568 | indent = GetOwner()->GetIndent()*indent; |
| 3569 | |
| 3570 | // we make the rectangle we are looking in a bit bigger than the actual |
| 3571 | // visual expander so the user can hit that little thing reliably |
| 3572 | wxRect rect( xpos + indent, |
| 3573 | GetLineStart( current ) + (GetLineHeight(current) - m_lineHeight)/2, |
| 3574 | m_lineHeight, m_lineHeight); |
| 3575 | if( rect.Contains(x, y) ) |
| 3576 | { |
| 3577 | // So the mouse is over the expander |
| 3578 | hoverOverExpander = true; |
| 3579 | if (m_underMouse && m_underMouse != node) |
| 3580 | { |
| 3581 | // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem())); |
| 3582 | RefreshRow(GetRowByItem(m_underMouse->GetItem())); |
| 3583 | } |
| 3584 | if (m_underMouse != node) |
| 3585 | { |
| 3586 | // wxLogMessage("Do the row: %d", current); |
| 3587 | RefreshRow(current); |
| 3588 | } |
| 3589 | m_underMouse = node; |
| 3590 | } |
| 3591 | } |
| 3592 | if (node!=NULL && !node->HasChildren()) |
| 3593 | delete node; |
| 3594 | } |
| 3595 | if (!hoverOverExpander) |
| 3596 | { |
| 3597 | if (m_underMouse != NULL) |
| 3598 | { |
| 3599 | // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem())); |
| 3600 | RefreshRow(GetRowByItem(m_underMouse->GetItem())); |
| 3601 | m_underMouse = NULL; |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | wxDataViewModel *model = GetOwner()->GetModel(); |
| 3606 | |
| 3607 | #if wxUSE_DRAG_AND_DROP |
| 3608 | if (event.Dragging()) |
| 3609 | { |
| 3610 | if (m_dragCount == 0) |
| 3611 | { |
| 3612 | // we have to report the raw, physical coords as we want to be |
| 3613 | // able to call HitTest(event.m_pointDrag) from the user code to |
| 3614 | // get the item being dragged |
| 3615 | m_dragStart = event.GetPosition(); |
| 3616 | } |
| 3617 | |
| 3618 | m_dragCount++; |
| 3619 | |
| 3620 | if (m_dragCount != 3) |
| 3621 | return; |
| 3622 | |
| 3623 | if (event.LeftIsDown()) |
| 3624 | { |
| 3625 | m_owner->CalcUnscrolledPosition( m_dragStart.x, m_dragStart.y, |
| 3626 | &m_dragStart.x, &m_dragStart.y ); |
| 3627 | unsigned int drag_item_row = GetLineAt( m_dragStart.y ); |
| 3628 | wxDataViewItem item = GetItemByRow( drag_item_row ); |
| 3629 | |
| 3630 | // Notify cell about drag |
| 3631 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() ); |
| 3632 | event.SetEventObject( m_owner ); |
| 3633 | event.SetItem( item ); |
| 3634 | event.SetModel( model ); |
| 3635 | if (!m_owner->HandleWindowEvent( event )) |
| 3636 | return; |
| 3637 | |
| 3638 | if (!event.IsAllowed()) |
| 3639 | return; |
| 3640 | |
| 3641 | wxDataObject *obj = event.GetDataObject(); |
| 3642 | if (!obj) |
| 3643 | return; |
| 3644 | |
| 3645 | wxDataViewDropSource drag( this, drag_item_row ); |
| 3646 | drag.SetData( *obj ); |
| 3647 | /* wxDragResult res = */ drag.DoDragDrop(); |
| 3648 | delete obj; |
| 3649 | } |
| 3650 | return; |
| 3651 | } |
| 3652 | else |
| 3653 | { |
| 3654 | m_dragCount = 0; |
| 3655 | } |
| 3656 | #endif // wxUSE_DRAG_AND_DROP |
| 3657 | |
| 3658 | bool simulateClick = false; |
| 3659 | |
| 3660 | if (event.ButtonDClick()) |
| 3661 | { |
| 3662 | m_renameTimer->Stop(); |
| 3663 | m_lastOnSame = false; |
| 3664 | } |
| 3665 | |
| 3666 | wxDataViewItem item = GetItemByRow(current); |
| 3667 | bool ignore_other_columns = |
| 3668 | ((GetOwner()->GetExpanderColumn() != col) && |
| 3669 | (model->IsContainer(item)) && |
| 3670 | (!model->HasContainerColumns(item))); |
| 3671 | |
| 3672 | if (event.LeftDClick()) |
| 3673 | { |
| 3674 | if(hoverOverExpander) |
| 3675 | { |
| 3676 | // a double click on the expander will be converted into a "simulated" normal click |
| 3677 | simulateClick = true; |
| 3678 | } |
| 3679 | else if ( current == m_lineLastClicked ) |
| 3680 | { |
| 3681 | if ((!ignore_other_columns) && (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE)) |
| 3682 | { |
| 3683 | wxVariant value; |
| 3684 | model->GetValue( value, item, col->GetModelColumn() ); |
| 3685 | cell->SetValue( value ); |
| 3686 | wxRect cell_rect( xpos, GetLineStart( current ), |
| 3687 | col->GetWidth(), GetLineHeight( current ) ); |
| 3688 | cell->Activate( cell_rect, model, item, col->GetModelColumn() ); |
| 3689 | } |
| 3690 | else |
| 3691 | { |
| 3692 | wxWindow *parent = GetParent(); |
| 3693 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId()); |
| 3694 | le.SetItem( item ); |
| 3695 | le.SetEventObject(parent); |
| 3696 | le.SetModel(GetOwner()->GetModel()); |
| 3697 | |
| 3698 | parent->GetEventHandler()->ProcessEvent(le); |
| 3699 | } |
| 3700 | return; |
| 3701 | } |
| 3702 | else |
| 3703 | { |
| 3704 | // The first click was on another item, so don't interpret this as |
| 3705 | // a double click, but as a simple click instead |
| 3706 | simulateClick = true; |
| 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | if (event.LeftUp() && !hoverOverExpander) |
| 3711 | { |
| 3712 | if (m_lineSelectSingleOnUp != (unsigned int)-1) |
| 3713 | { |
| 3714 | // select single line |
| 3715 | SelectAllRows( false ); |
| 3716 | SelectRow( m_lineSelectSingleOnUp, true ); |
| 3717 | SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) ); |
| 3718 | } |
| 3719 | |
| 3720 | // If the user click the expander, we do not do editing even if the column |
| 3721 | // with expander are editable |
| 3722 | if (m_lastOnSame && !ignore_other_columns) |
| 3723 | { |
| 3724 | if ((col == m_currentCol) && (current == m_currentRow) && |
| 3725 | (cell->GetMode() & wxDATAVIEW_CELL_EDITABLE) ) |
| 3726 | { |
| 3727 | m_renameTimer->Start( 100, true ); |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | m_lastOnSame = false; |
| 3732 | m_lineSelectSingleOnUp = (unsigned int)-1; |
| 3733 | } |
| 3734 | else if(!event.LeftUp()) |
| 3735 | { |
| 3736 | // This is necessary, because after a DnD operation in |
| 3737 | // from and to ourself, the up event is swallowed by the |
| 3738 | // DnD code. So on next non-up event (which means here and |
| 3739 | // now) m_lineSelectSingleOnUp should be reset. |
| 3740 | m_lineSelectSingleOnUp = (unsigned int)-1; |
| 3741 | } |
| 3742 | |
| 3743 | if (event.RightDown()) |
| 3744 | { |
| 3745 | m_lineBeforeLastClicked = m_lineLastClicked; |
| 3746 | m_lineLastClicked = current; |
| 3747 | |
| 3748 | // If the item is already selected, do not update the selection. |
| 3749 | // Multi-selections should not be cleared if a selected item is clicked. |
| 3750 | if (!IsRowSelected(current)) |
| 3751 | { |
| 3752 | SelectAllRows(false); |
| 3753 | ChangeCurrentRow(current); |
| 3754 | SelectRow(m_currentRow,true); |
| 3755 | SendSelectionChangedEvent(GetItemByRow( m_currentRow ) ); |
| 3756 | } |
| 3757 | } |
| 3758 | else if (event.RightUp()) |
| 3759 | { |
| 3760 | wxVariant value; |
| 3761 | model->GetValue( value, item, col->GetModelColumn() ); |
| 3762 | wxWindow *parent = GetParent(); |
| 3763 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, parent->GetId()); |
| 3764 | le.SetItem( item ); |
| 3765 | le.SetEventObject(parent); |
| 3766 | le.SetModel(GetOwner()->GetModel()); |
| 3767 | le.SetValue(value); |
| 3768 | parent->GetEventHandler()->ProcessEvent(le); |
| 3769 | } |
| 3770 | else if (event.MiddleDown()) |
| 3771 | { |
| 3772 | } |
| 3773 | |
| 3774 | if((event.LeftDown() || simulateClick) && hoverOverExpander) |
| 3775 | { |
| 3776 | wxDataViewTreeNode* node = GetTreeNodeByRow(current); |
| 3777 | |
| 3778 | // hoverOverExpander being true tells us that our node must be |
| 3779 | // valid and have children. |
| 3780 | // So we don't need any extra checks. |
| 3781 | if( node->IsOpen() ) |
| 3782 | Collapse(current); |
| 3783 | else |
| 3784 | Expand(current); |
| 3785 | } |
| 3786 | else if ((event.LeftDown() || simulateClick) && !hoverOverExpander) |
| 3787 | { |
| 3788 | SetFocus(); |
| 3789 | |
| 3790 | m_lineBeforeLastClicked = m_lineLastClicked; |
| 3791 | m_lineLastClicked = current; |
| 3792 | |
| 3793 | unsigned int oldCurrentRow = m_currentRow; |
| 3794 | bool oldWasSelected = IsRowSelected(m_currentRow); |
| 3795 | |
| 3796 | bool cmdModifierDown = event.CmdDown(); |
| 3797 | if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) ) |
| 3798 | { |
| 3799 | if ( IsSingleSel() || !IsRowSelected(current) ) |
| 3800 | { |
| 3801 | SelectAllRows( false ); |
| 3802 | ChangeCurrentRow(current); |
| 3803 | SelectRow(m_currentRow,true); |
| 3804 | SendSelectionChangedEvent(GetItemByRow( m_currentRow ) ); |
| 3805 | } |
| 3806 | else // multi sel & current is highlighted & no mod keys |
| 3807 | { |
| 3808 | m_lineSelectSingleOnUp = current; |
| 3809 | ChangeCurrentRow(current); // change focus |
| 3810 | } |
| 3811 | } |
| 3812 | else // multi sel & either ctrl or shift is down |
| 3813 | { |
| 3814 | if (cmdModifierDown) |
| 3815 | { |
| 3816 | ChangeCurrentRow(current); |
| 3817 | ReverseRowSelection(m_currentRow); |
| 3818 | SendSelectionChangedEvent(GetItemByRow(m_selection[0]) ); |
| 3819 | } |
| 3820 | else if (event.ShiftDown()) |
| 3821 | { |
| 3822 | ChangeCurrentRow(current); |
| 3823 | |
| 3824 | unsigned int lineFrom = oldCurrentRow, |
| 3825 | lineTo = current; |
| 3826 | |
| 3827 | if ( lineTo < lineFrom ) |
| 3828 | { |
| 3829 | lineTo = lineFrom; |
| 3830 | lineFrom = m_currentRow; |
| 3831 | } |
| 3832 | |
| 3833 | SelectRows(lineFrom, lineTo, true); |
| 3834 | SendSelectionChangedEvent(GetItemByRow(m_selection[0]) ); |
| 3835 | } |
| 3836 | else // !ctrl, !shift |
| 3837 | { |
| 3838 | // test in the enclosing if should make it impossible |
| 3839 | wxFAIL_MSG( _T("how did we get here?") ); |
| 3840 | } |
| 3841 | } |
| 3842 | |
| 3843 | if (m_currentRow != oldCurrentRow) |
| 3844 | RefreshRow( oldCurrentRow ); |
| 3845 | |
| 3846 | wxDataViewColumn *oldCurrentCol = m_currentCol; |
| 3847 | |
| 3848 | // Update selection here... |
| 3849 | m_currentCol = col; |
| 3850 | |
| 3851 | m_lastOnSame = !simulateClick && ((col == oldCurrentCol) && |
| 3852 | (current == oldCurrentRow)) && oldWasSelected; |
| 3853 | |
| 3854 | // Call LeftClick after everything else as under GTK+ |
| 3855 | if (cell->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE) |
| 3856 | { |
| 3857 | // notify cell about right click |
| 3858 | wxVariant value; |
| 3859 | model->GetValue( value, item, col->GetModelColumn() ); |
| 3860 | cell->SetValue( value ); |
| 3861 | wxRect cell_rect( xpos, GetLineStart( current ), |
| 3862 | col->GetWidth(), GetLineHeight( current ) ); |
| 3863 | /* ignore ret */ cell->LeftClick( event.GetPosition(), cell_rect, |
| 3864 | model, item, col->GetModelColumn()); |
| 3865 | } |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event ) |
| 3870 | { |
| 3871 | m_hasFocus = true; |
| 3872 | |
| 3873 | if (HasCurrentRow()) |
| 3874 | Refresh(); |
| 3875 | |
| 3876 | event.Skip(); |
| 3877 | } |
| 3878 | |
| 3879 | void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event ) |
| 3880 | { |
| 3881 | m_hasFocus = false; |
| 3882 | |
| 3883 | if (HasCurrentRow()) |
| 3884 | Refresh(); |
| 3885 | |
| 3886 | event.Skip(); |
| 3887 | } |
| 3888 | |
| 3889 | wxDataViewItem wxDataViewMainWindow::GetSelection() const |
| 3890 | { |
| 3891 | if( m_selection.GetCount() != 1 ) |
| 3892 | return wxDataViewItem(); |
| 3893 | |
| 3894 | return GetItemByRow( m_selection.Item(0)); |
| 3895 | } |
| 3896 | |
| 3897 | //----------------------------------------------------------------------------- |
| 3898 | // wxDataViewCtrl |
| 3899 | //----------------------------------------------------------------------------- |
| 3900 | |
| 3901 | WX_DEFINE_LIST(wxDataViewColumnList) |
| 3902 | |
| 3903 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase) |
| 3904 | BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase) |
| 3905 | EVT_SIZE(wxDataViewCtrl::OnSize) |
| 3906 | END_EVENT_TABLE() |
| 3907 | |
| 3908 | wxDataViewCtrl::~wxDataViewCtrl() |
| 3909 | { |
| 3910 | if (m_notifier) |
| 3911 | GetModel()->RemoveNotifier( m_notifier ); |
| 3912 | |
| 3913 | m_cols.Clear(); |
| 3914 | } |
| 3915 | |
| 3916 | void wxDataViewCtrl::Init() |
| 3917 | { |
| 3918 | m_cols.DeleteContents(true); |
| 3919 | m_notifier = NULL; |
| 3920 | |
| 3921 | // No sorting column at start |
| 3922 | m_sortingColumnIdx = wxNOT_FOUND; |
| 3923 | |
| 3924 | m_headerArea = NULL; |
| 3925 | } |
| 3926 | |
| 3927 | bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id, |
| 3928 | const wxPoint& pos, const wxSize& size, |
| 3929 | long style, const wxValidator& validator ) |
| 3930 | { |
| 3931 | // if ( (style & wxBORDER_MASK) == 0) |
| 3932 | // style |= wxBORDER_SUNKEN; |
| 3933 | |
| 3934 | Init(); |
| 3935 | |
| 3936 | if (!wxControl::Create( parent, id, pos, size, |
| 3937 | style | wxScrolledWindowStyle, validator)) |
| 3938 | return false; |
| 3939 | |
| 3940 | SetInitialSize(size); |
| 3941 | |
| 3942 | #ifdef __WXMAC__ |
| 3943 | MacSetClipChildren( true ); |
| 3944 | #endif |
| 3945 | |
| 3946 | m_clientArea = new wxDataViewMainWindow( this, wxID_ANY ); |
| 3947 | |
| 3948 | if (HasFlag(wxDV_NO_HEADER)) |
| 3949 | m_headerArea = NULL; |
| 3950 | else |
| 3951 | m_headerArea = new wxDataViewHeaderWindow(this); |
| 3952 | |
| 3953 | SetTargetWindow( m_clientArea ); |
| 3954 | |
| 3955 | wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL ); |
| 3956 | if (m_headerArea) |
| 3957 | sizer->Add( m_headerArea, 0, wxGROW ); |
| 3958 | sizer->Add( m_clientArea, 1, wxGROW ); |
| 3959 | SetSizer( sizer ); |
| 3960 | |
| 3961 | return true; |
| 3962 | } |
| 3963 | |
| 3964 | wxBorder wxDataViewCtrl::GetDefaultBorder() const |
| 3965 | { |
| 3966 | return wxBORDER_THEME; |
| 3967 | } |
| 3968 | |
| 3969 | #ifdef __WXMSW__ |
| 3970 | WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg, |
| 3971 | WXWPARAM wParam, |
| 3972 | WXLPARAM lParam) |
| 3973 | { |
| 3974 | WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam); |
| 3975 | |
| 3976 | #ifndef __WXWINCE__ |
| 3977 | // we need to process arrows ourselves for scrolling |
| 3978 | if ( nMsg == WM_GETDLGCODE ) |
| 3979 | { |
| 3980 | rc |= DLGC_WANTARROWS; |
| 3981 | } |
| 3982 | #endif |
| 3983 | |
| 3984 | return rc; |
| 3985 | } |
| 3986 | #endif |
| 3987 | |
| 3988 | wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size) |
| 3989 | { |
| 3990 | wxSize newsize = size; |
| 3991 | if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea)) |
| 3992 | newsize.y -= m_headerArea->GetSize().y; |
| 3993 | |
| 3994 | return newsize; |
| 3995 | } |
| 3996 | |
| 3997 | void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) ) |
| 3998 | { |
| 3999 | // We need to override OnSize so that our scrolled |
| 4000 | // window a) does call Layout() to use sizers for |
| 4001 | // positioning the controls but b) does not query |
| 4002 | // the sizer for their size and use that for setting |
| 4003 | // the scrollable area as set that ourselves by |
| 4004 | // calling SetScrollbar() further down. |
| 4005 | |
| 4006 | Layout(); |
| 4007 | |
| 4008 | AdjustScrollbars(); |
| 4009 | } |
| 4010 | |
| 4011 | void wxDataViewCtrl::SetFocus() |
| 4012 | { |
| 4013 | if (m_clientArea) |
| 4014 | m_clientArea->SetFocus(); |
| 4015 | } |
| 4016 | |
| 4017 | bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model ) |
| 4018 | { |
| 4019 | if (!wxDataViewCtrlBase::AssociateModel( model )) |
| 4020 | return false; |
| 4021 | |
| 4022 | m_notifier = new wxGenericDataViewModelNotifier( m_clientArea ); |
| 4023 | |
| 4024 | model->AddNotifier( m_notifier ); |
| 4025 | |
| 4026 | m_clientArea->DestroyTree(); |
| 4027 | |
| 4028 | m_clientArea->BuildTree(model); |
| 4029 | |
| 4030 | m_clientArea->UpdateDisplay(); |
| 4031 | |
| 4032 | return true; |
| 4033 | } |
| 4034 | |
| 4035 | #if wxUSE_DRAG_AND_DROP |
| 4036 | |
| 4037 | bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format ) |
| 4038 | { |
| 4039 | return m_clientArea->EnableDragSource( format ); |
| 4040 | } |
| 4041 | |
| 4042 | bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format ) |
| 4043 | { |
| 4044 | return m_clientArea->EnableDropTarget( format ); |
| 4045 | } |
| 4046 | |
| 4047 | #endif // wxUSE_DRAG_AND_DROP |
| 4048 | |
| 4049 | bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col ) |
| 4050 | { |
| 4051 | if (!wxDataViewCtrlBase::AppendColumn(col)) |
| 4052 | return false; |
| 4053 | |
| 4054 | m_cols.Append( col ); |
| 4055 | OnColumnsCountChanged(); |
| 4056 | return true; |
| 4057 | } |
| 4058 | |
| 4059 | bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col ) |
| 4060 | { |
| 4061 | if (!wxDataViewCtrlBase::PrependColumn(col)) |
| 4062 | return false; |
| 4063 | |
| 4064 | m_cols.Insert( col ); |
| 4065 | OnColumnsCountChanged(); |
| 4066 | return true; |
| 4067 | } |
| 4068 | |
| 4069 | bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col ) |
| 4070 | { |
| 4071 | if (!wxDataViewCtrlBase::InsertColumn(pos,col)) |
| 4072 | return false; |
| 4073 | |
| 4074 | m_cols.Insert( pos, col ); |
| 4075 | OnColumnsCountChanged(); |
| 4076 | return true; |
| 4077 | } |
| 4078 | |
| 4079 | void wxDataViewCtrl::OnColumnChange(unsigned int idx) |
| 4080 | { |
| 4081 | if ( m_headerArea ) |
| 4082 | m_headerArea->UpdateColumn(idx); |
| 4083 | |
| 4084 | m_clientArea->UpdateDisplay(); |
| 4085 | } |
| 4086 | |
| 4087 | void wxDataViewCtrl::OnColumnsCountChanged() |
| 4088 | { |
| 4089 | if (m_headerArea) |
| 4090 | m_headerArea->SetColumnCount(GetColumnCount()); |
| 4091 | |
| 4092 | m_clientArea->UpdateDisplay(); |
| 4093 | } |
| 4094 | |
| 4095 | void wxDataViewCtrl::DoSetExpanderColumn() |
| 4096 | { |
| 4097 | m_clientArea->UpdateDisplay(); |
| 4098 | } |
| 4099 | |
| 4100 | void wxDataViewCtrl::DoSetIndent() |
| 4101 | { |
| 4102 | m_clientArea->UpdateDisplay(); |
| 4103 | } |
| 4104 | |
| 4105 | unsigned int wxDataViewCtrl::GetColumnCount() const |
| 4106 | { |
| 4107 | return m_cols.GetCount(); |
| 4108 | } |
| 4109 | |
| 4110 | wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const |
| 4111 | { |
| 4112 | return m_cols[idx]; |
| 4113 | } |
| 4114 | |
| 4115 | wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const |
| 4116 | { |
| 4117 | // columns can't be reordered if there is no header window which allows |
| 4118 | // to do this |
| 4119 | const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos] |
| 4120 | : pos; |
| 4121 | |
| 4122 | return GetColumn(idx); |
| 4123 | } |
| 4124 | |
| 4125 | int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const |
| 4126 | { |
| 4127 | const unsigned count = m_cols.size(); |
| 4128 | for ( unsigned n = 0; n < count; n++ ) |
| 4129 | { |
| 4130 | if ( m_cols[n] == column ) |
| 4131 | return n; |
| 4132 | } |
| 4133 | |
| 4134 | return wxNOT_FOUND; |
| 4135 | } |
| 4136 | |
| 4137 | void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col), |
| 4138 | unsigned int WXUNUSED(new_pos)) |
| 4139 | { |
| 4140 | // do _not_ reorder m_cols elements here, they should always be in the |
| 4141 | // order in which columns were added, we only display the columns in |
| 4142 | // different order |
| 4143 | m_clientArea->UpdateDisplay(); |
| 4144 | } |
| 4145 | |
| 4146 | bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column ) |
| 4147 | { |
| 4148 | wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column ); |
| 4149 | if (!ret) |
| 4150 | return false; |
| 4151 | |
| 4152 | m_cols.Erase(ret); |
| 4153 | OnColumnsCountChanged(); |
| 4154 | |
| 4155 | return true; |
| 4156 | } |
| 4157 | |
| 4158 | bool wxDataViewCtrl::ClearColumns() |
| 4159 | { |
| 4160 | m_cols.Clear(); |
| 4161 | OnColumnsCountChanged(); |
| 4162 | return true; |
| 4163 | } |
| 4164 | |
| 4165 | int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const |
| 4166 | { |
| 4167 | int ret = 0, |
| 4168 | dummy = 0; |
| 4169 | unsigned int len = GetColumnCount(); |
| 4170 | for ( unsigned int i = 0; i < len; i++ ) |
| 4171 | { |
| 4172 | wxDataViewColumn * col = GetColumnAt(i); |
| 4173 | if (col->IsHidden()) |
| 4174 | continue; |
| 4175 | ret += col->GetWidth(); |
| 4176 | if (column==col) |
| 4177 | { |
| 4178 | CalcScrolledPosition( ret, dummy, &ret, &dummy ); |
| 4179 | break; |
| 4180 | } |
| 4181 | } |
| 4182 | return ret; |
| 4183 | } |
| 4184 | |
| 4185 | wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const |
| 4186 | { |
| 4187 | return m_sortingColumnIdx == wxNOT_FOUND ? NULL |
| 4188 | : GetColumn(m_sortingColumnIdx); |
| 4189 | } |
| 4190 | |
| 4191 | // Selection code with wxDataViewItem as parameters |
| 4192 | wxDataViewItem wxDataViewCtrl::GetSelection() const |
| 4193 | { |
| 4194 | return m_clientArea->GetSelection(); |
| 4195 | } |
| 4196 | |
| 4197 | int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const |
| 4198 | { |
| 4199 | sel.Empty(); |
| 4200 | wxDataViewSelection selection = m_clientArea->GetSelections(); |
| 4201 | int len = selection.GetCount(); |
| 4202 | for( int i = 0; i < len; i ++) |
| 4203 | { |
| 4204 | unsigned int row = selection[i]; |
| 4205 | sel.Add( m_clientArea->GetItemByRow( row ) ); |
| 4206 | } |
| 4207 | return len; |
| 4208 | } |
| 4209 | |
| 4210 | void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel ) |
| 4211 | { |
| 4212 | wxDataViewSelection selection(wxDataViewSelectionCmp); |
| 4213 | |
| 4214 | wxDataViewItem last_parent; |
| 4215 | |
| 4216 | int len = sel.GetCount(); |
| 4217 | for( int i = 0; i < len; i ++ ) |
| 4218 | { |
| 4219 | wxDataViewItem item = sel[i]; |
| 4220 | wxDataViewItem parent = GetModel()->GetParent( item ); |
| 4221 | if (parent) |
| 4222 | { |
| 4223 | if (parent != last_parent) |
| 4224 | ExpandAncestors(item); |
| 4225 | } |
| 4226 | |
| 4227 | last_parent = parent; |
| 4228 | int row = m_clientArea->GetRowByItem( item ); |
| 4229 | if( row >= 0 ) |
| 4230 | selection.Add( static_cast<unsigned int>(row) ); |
| 4231 | } |
| 4232 | |
| 4233 | m_clientArea->SetSelections( selection ); |
| 4234 | } |
| 4235 | |
| 4236 | void wxDataViewCtrl::Select( const wxDataViewItem & item ) |
| 4237 | { |
| 4238 | ExpandAncestors( item ); |
| 4239 | |
| 4240 | int row = m_clientArea->GetRowByItem( item ); |
| 4241 | if( row >= 0 ) |
| 4242 | { |
| 4243 | // Unselect all rows before select another in the single select mode |
| 4244 | if (m_clientArea->IsSingleSel()) |
| 4245 | m_clientArea->SelectAllRows(false); |
| 4246 | m_clientArea->SelectRow(row, true); |
| 4247 | } |
| 4248 | } |
| 4249 | |
| 4250 | void wxDataViewCtrl::Unselect( const wxDataViewItem & item ) |
| 4251 | { |
| 4252 | int row = m_clientArea->GetRowByItem( item ); |
| 4253 | if( row >= 0 ) |
| 4254 | m_clientArea->SelectRow(row, false); |
| 4255 | } |
| 4256 | |
| 4257 | bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const |
| 4258 | { |
| 4259 | int row = m_clientArea->GetRowByItem( item ); |
| 4260 | if( row >= 0 ) |
| 4261 | { |
| 4262 | return m_clientArea->IsRowSelected(row); |
| 4263 | } |
| 4264 | return false; |
| 4265 | } |
| 4266 | |
| 4267 | // Selection code with row number as parameter |
| 4268 | int wxDataViewCtrl::GetSelections( wxArrayInt & sel ) const |
| 4269 | { |
| 4270 | sel.Empty(); |
| 4271 | wxDataViewSelection selection = m_clientArea->GetSelections(); |
| 4272 | int len = selection.GetCount(); |
| 4273 | for( int i = 0; i < len; i ++) |
| 4274 | { |
| 4275 | unsigned int row = selection[i]; |
| 4276 | sel.Add( row ); |
| 4277 | } |
| 4278 | return len; |
| 4279 | } |
| 4280 | |
| 4281 | void wxDataViewCtrl::SetSelections( const wxArrayInt & sel ) |
| 4282 | { |
| 4283 | wxDataViewSelection selection(wxDataViewSelectionCmp); |
| 4284 | int len = sel.GetCount(); |
| 4285 | for( int i = 0; i < len; i ++ ) |
| 4286 | { |
| 4287 | int row = sel[i]; |
| 4288 | if( row >= 0 ) |
| 4289 | selection.Add( static_cast<unsigned int>(row) ); |
| 4290 | } |
| 4291 | m_clientArea->SetSelections( selection ); |
| 4292 | } |
| 4293 | |
| 4294 | void wxDataViewCtrl::Select( int row ) |
| 4295 | { |
| 4296 | if( row >= 0 ) |
| 4297 | { |
| 4298 | if (m_clientArea->IsSingleSel()) |
| 4299 | m_clientArea->SelectAllRows(false); |
| 4300 | m_clientArea->SelectRow( row, true ); |
| 4301 | } |
| 4302 | } |
| 4303 | |
| 4304 | void wxDataViewCtrl::Unselect( int row ) |
| 4305 | { |
| 4306 | if( row >= 0 ) |
| 4307 | m_clientArea->SelectRow(row, false); |
| 4308 | } |
| 4309 | |
| 4310 | bool wxDataViewCtrl::IsSelected( int row ) const |
| 4311 | { |
| 4312 | if( row >= 0 ) |
| 4313 | return m_clientArea->IsRowSelected(row); |
| 4314 | return false; |
| 4315 | } |
| 4316 | |
| 4317 | void wxDataViewCtrl::SelectRange( int from, int to ) |
| 4318 | { |
| 4319 | wxArrayInt sel; |
| 4320 | for( int i = from; i < to; i ++ ) |
| 4321 | sel.Add( i ); |
| 4322 | m_clientArea->Select(sel); |
| 4323 | } |
| 4324 | |
| 4325 | void wxDataViewCtrl::UnselectRange( int from, int to ) |
| 4326 | { |
| 4327 | wxDataViewSelection sel = m_clientArea->GetSelections(); |
| 4328 | for( int i = from; i < to; i ++ ) |
| 4329 | if( sel.Index( i ) != wxNOT_FOUND ) |
| 4330 | sel.Remove( i ); |
| 4331 | m_clientArea->SetSelections(sel); |
| 4332 | } |
| 4333 | |
| 4334 | void wxDataViewCtrl::SelectAll() |
| 4335 | { |
| 4336 | m_clientArea->SelectAllRows(true); |
| 4337 | } |
| 4338 | |
| 4339 | void wxDataViewCtrl::UnselectAll() |
| 4340 | { |
| 4341 | m_clientArea->SelectAllRows(false); |
| 4342 | } |
| 4343 | |
| 4344 | void wxDataViewCtrl::EnsureVisible( int row, int column ) |
| 4345 | { |
| 4346 | if( row < 0 ) |
| 4347 | row = 0; |
| 4348 | if( row > (int) m_clientArea->GetRowCount() ) |
| 4349 | row = m_clientArea->GetRowCount(); |
| 4350 | |
| 4351 | int first = m_clientArea->GetFirstVisibleRow(); |
| 4352 | int last = m_clientArea->GetLastVisibleRow(); |
| 4353 | if( row < first ) |
| 4354 | m_clientArea->ScrollTo( row, column ); |
| 4355 | else if( row > last ) |
| 4356 | m_clientArea->ScrollTo( row - last + first, column ); |
| 4357 | else |
| 4358 | m_clientArea->ScrollTo( first, column ); |
| 4359 | } |
| 4360 | |
| 4361 | void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column ) |
| 4362 | { |
| 4363 | ExpandAncestors( item ); |
| 4364 | |
| 4365 | m_clientArea->RecalculateDisplay(); |
| 4366 | |
| 4367 | int row = m_clientArea->GetRowByItem(item); |
| 4368 | if( row >= 0 ) |
| 4369 | { |
| 4370 | if( column == NULL ) |
| 4371 | EnsureVisible(row, -1); |
| 4372 | else |
| 4373 | EnsureVisible( row, GetColumnIndex(column) ); |
| 4374 | } |
| 4375 | |
| 4376 | } |
| 4377 | |
| 4378 | void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item, |
| 4379 | wxDataViewColumn* &column ) const |
| 4380 | { |
| 4381 | m_clientArea->HitTest(point, item, column); |
| 4382 | } |
| 4383 | |
| 4384 | wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item, |
| 4385 | const wxDataViewColumn* column ) const |
| 4386 | { |
| 4387 | return m_clientArea->GetItemRect(item, column); |
| 4388 | } |
| 4389 | |
| 4390 | wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const |
| 4391 | { |
| 4392 | return m_clientArea->GetItemByRow( row ); |
| 4393 | } |
| 4394 | |
| 4395 | int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const |
| 4396 | { |
| 4397 | return m_clientArea->GetRowByItem( item ); |
| 4398 | } |
| 4399 | |
| 4400 | void wxDataViewCtrl::Expand( const wxDataViewItem & item ) |
| 4401 | { |
| 4402 | int row = m_clientArea->GetRowByItem( item ); |
| 4403 | if (row != -1) |
| 4404 | m_clientArea->Expand(row); |
| 4405 | } |
| 4406 | |
| 4407 | void wxDataViewCtrl::Collapse( const wxDataViewItem & item ) |
| 4408 | { |
| 4409 | int row = m_clientArea->GetRowByItem( item ); |
| 4410 | if (row != -1) |
| 4411 | m_clientArea->Collapse(row); |
| 4412 | } |
| 4413 | |
| 4414 | bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const |
| 4415 | { |
| 4416 | int row = m_clientArea->GetRowByItem( item ); |
| 4417 | if (row != -1) |
| 4418 | return m_clientArea->IsExpanded(row); |
| 4419 | return false; |
| 4420 | } |
| 4421 | |
| 4422 | void wxDataViewCtrl::StartEditor( const wxDataViewItem & item, unsigned int column ) |
| 4423 | { |
| 4424 | wxDataViewColumn* col = GetColumn( column ); |
| 4425 | if (!col) |
| 4426 | return; |
| 4427 | |
| 4428 | wxRect itemRect = GetItemRect(item, col); |
| 4429 | wxDataViewRenderer* renderer = col->GetRenderer(); |
| 4430 | renderer->StartEditing(item, itemRect); |
| 4431 | } |
| 4432 | |
| 4433 | #endif // !wxUSE_GENERICDATAVIEWCTRL |
| 4434 | |
| 4435 | #endif // wxUSE_DATAVIEWCTRL |