]>
Commit | Line | Data |
---|---|---|
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/popupwin.h" | |
42 | #include "wx/renderer.h" | |
43 | #include "wx/dcbuffer.h" | |
44 | #include "wx/icon.h" | |
45 | #include "wx/list.h" | |
46 | #include "wx/listimpl.cpp" | |
47 | #include "wx/imaglist.h" | |
48 | #include "wx/headerctrl.h" | |
49 | #include "wx/dnd.h" | |
50 | #include "wx/stopwatch.h" | |
51 | #include "wx/weakref.h" | |
52 | ||
53 | //----------------------------------------------------------------------------- | |
54 | // classes | |
55 | //----------------------------------------------------------------------------- | |
56 | ||
57 | class wxDataViewColumn; | |
58 | class wxDataViewHeaderWindow; | |
59 | class wxDataViewCtrl; | |
60 | ||
61 | //----------------------------------------------------------------------------- | |
62 | // classes | |
63 | //----------------------------------------------------------------------------- | |
64 | ||
65 | static const int SCROLL_UNIT_X = 15; | |
66 | ||
67 | // the cell padding on the left/right | |
68 | static const int PADDING_RIGHTLEFT = 3; | |
69 | ||
70 | // the expander space margin | |
71 | static const int EXPANDER_MARGIN = 4; | |
72 | ||
73 | #ifdef __WXMSW__ | |
74 | static const int EXPANDER_OFFSET = 4; | |
75 | #else | |
76 | static const int EXPANDER_OFFSET = 1; | |
77 | #endif | |
78 | ||
79 | // Below is the compare stuff. | |
80 | // For the generic implementation, both the leaf nodes and the nodes are sorted for | |
81 | // fast search when needed | |
82 | static wxDataViewModel* g_model; | |
83 | static int g_column = -2; | |
84 | static bool g_asending = true; | |
85 | ||
86 | // ---------------------------------------------------------------------------- | |
87 | // helper functions | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
90 | namespace | |
91 | { | |
92 | ||
93 | // Return the expander column or, if it is not set, the first column and also | |
94 | // set it as the expander one for the future. | |
95 | wxDataViewColumn* GetExpanderColumnOrFirstOne(wxDataViewCtrl* dataview) | |
96 | { | |
97 | wxDataViewColumn* expander = dataview->GetExpanderColumn(); | |
98 | if (!expander) | |
99 | { | |
100 | // TODO-RTL: last column for RTL support | |
101 | expander = dataview->GetColumnAt( 0 ); | |
102 | dataview->SetExpanderColumn(expander); | |
103 | } | |
104 | ||
105 | return expander; | |
106 | } | |
107 | ||
108 | } // anonymous namespace | |
109 | ||
110 | //----------------------------------------------------------------------------- | |
111 | // wxDataViewColumn | |
112 | //----------------------------------------------------------------------------- | |
113 | ||
114 | void wxDataViewColumn::Init(int width, wxAlignment align, int flags) | |
115 | { | |
116 | m_width = width; | |
117 | m_minWidth = 0; | |
118 | m_align = align; | |
119 | m_flags = flags; | |
120 | m_sort = false; | |
121 | m_sortAscending = true; | |
122 | } | |
123 | ||
124 | int wxDataViewColumn::GetWidth() const | |
125 | { | |
126 | switch ( m_width ) | |
127 | { | |
128 | case wxCOL_WIDTH_DEFAULT: | |
129 | return wxDVC_DEFAULT_WIDTH; | |
130 | ||
131 | case wxCOL_WIDTH_AUTOSIZE: | |
132 | wxCHECK_MSG( m_owner, wxDVC_DEFAULT_WIDTH, "no owner control" ); | |
133 | return m_owner->GetBestColumnWidth(m_owner->GetColumnIndex(this)); | |
134 | ||
135 | default: | |
136 | return m_width; | |
137 | } | |
138 | } | |
139 | ||
140 | void wxDataViewColumn::UpdateDisplay() | |
141 | { | |
142 | if (m_owner) | |
143 | { | |
144 | int idx = m_owner->GetColumnIndex( this ); | |
145 | m_owner->OnColumnChange( idx ); | |
146 | } | |
147 | } | |
148 | ||
149 | void wxDataViewColumn::SetSortOrder(bool ascending) | |
150 | { | |
151 | if ( !m_owner ) | |
152 | return; | |
153 | ||
154 | // First unset the old sort column if any. | |
155 | int oldSortKey = m_owner->GetSortingColumnIndex(); | |
156 | if ( oldSortKey != wxNOT_FOUND ) | |
157 | { | |
158 | m_owner->GetColumn(oldSortKey)->UnsetAsSortKey(); | |
159 | } | |
160 | ||
161 | // Now set this one as the new sort column. | |
162 | const int idx = m_owner->GetColumnIndex(this); | |
163 | m_owner->SetSortingColumnIndex(idx); | |
164 | ||
165 | m_sort = true; | |
166 | m_sortAscending = ascending; | |
167 | ||
168 | // Call this directly instead of using UpdateDisplay() as we already have | |
169 | // the column index, no need to look it up again. | |
170 | m_owner->OnColumnChange(idx); | |
171 | } | |
172 | ||
173 | //----------------------------------------------------------------------------- | |
174 | // wxDataViewHeaderWindow | |
175 | //----------------------------------------------------------------------------- | |
176 | ||
177 | class wxDataViewHeaderWindow : public wxHeaderCtrl | |
178 | { | |
179 | public: | |
180 | wxDataViewHeaderWindow(wxDataViewCtrl *parent) | |
181 | : wxHeaderCtrl(parent) | |
182 | { | |
183 | } | |
184 | ||
185 | wxDataViewCtrl *GetOwner() const | |
186 | { return static_cast<wxDataViewCtrl *>(GetParent()); } | |
187 | ||
188 | protected: | |
189 | // implement/override wxHeaderCtrl functions by forwarding them to the main | |
190 | // control | |
191 | virtual const wxHeaderColumn& GetColumn(unsigned int idx) const | |
192 | { | |
193 | return *(GetOwner()->GetColumn(idx)); | |
194 | } | |
195 | ||
196 | virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle) | |
197 | { | |
198 | wxDataViewCtrl * const owner = GetOwner(); | |
199 | ||
200 | int widthContents = owner->GetBestColumnWidth(idx); | |
201 | owner->GetColumn(idx)->SetWidth(wxMax(widthTitle, widthContents)); | |
202 | owner->OnColumnChange(idx); | |
203 | ||
204 | return true; | |
205 | } | |
206 | ||
207 | private: | |
208 | bool SendEvent(wxEventType type, unsigned int n) | |
209 | { | |
210 | wxDataViewCtrl * const owner = GetOwner(); | |
211 | wxDataViewEvent event(type, owner->GetId()); | |
212 | ||
213 | event.SetEventObject(owner); | |
214 | event.SetColumn(n); | |
215 | event.SetDataViewColumn(owner->GetColumn(n)); | |
216 | event.SetModel(owner->GetModel()); | |
217 | ||
218 | // for events created by wxDataViewHeaderWindow the | |
219 | // row / value fields are not valid | |
220 | return owner->ProcessWindowEvent(event); | |
221 | } | |
222 | ||
223 | void OnClick(wxHeaderCtrlEvent& event) | |
224 | { | |
225 | const unsigned idx = event.GetColumn(); | |
226 | ||
227 | if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, idx) ) | |
228 | return; | |
229 | ||
230 | // default handling for the column click is to sort by this column or | |
231 | // toggle its sort order | |
232 | wxDataViewCtrl * const owner = GetOwner(); | |
233 | wxDataViewColumn * const col = owner->GetColumn(idx); | |
234 | if ( !col->IsSortable() ) | |
235 | { | |
236 | // no default handling for non-sortable columns | |
237 | event.Skip(); | |
238 | return; | |
239 | } | |
240 | ||
241 | if ( col->IsSortKey() ) | |
242 | { | |
243 | // already using this column for sorting, just change the order | |
244 | col->ToggleSortOrder(); | |
245 | } | |
246 | else // not using this column for sorting yet | |
247 | { | |
248 | col->SetSortOrder(true); | |
249 | } | |
250 | ||
251 | wxDataViewModel * const model = owner->GetModel(); | |
252 | if ( model ) | |
253 | model->Resort(); | |
254 | ||
255 | owner->OnColumnChange(idx); | |
256 | ||
257 | SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, idx); | |
258 | } | |
259 | ||
260 | void OnRClick(wxHeaderCtrlEvent& event) | |
261 | { | |
262 | if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, | |
263 | event.GetColumn()) ) | |
264 | event.Skip(); | |
265 | } | |
266 | ||
267 | void OnResize(wxHeaderCtrlEvent& event) | |
268 | { | |
269 | wxDataViewCtrl * const owner = GetOwner(); | |
270 | ||
271 | const unsigned col = event.GetColumn(); | |
272 | owner->GetColumn(col)->SetWidth(event.GetWidth()); | |
273 | GetOwner()->OnColumnChange(col); | |
274 | } | |
275 | ||
276 | void OnEndReorder(wxHeaderCtrlEvent& event) | |
277 | { | |
278 | wxDataViewCtrl * const owner = GetOwner(); | |
279 | owner->ColumnMoved(owner->GetColumn(event.GetColumn()), | |
280 | event.GetNewOrder()); | |
281 | } | |
282 | ||
283 | DECLARE_EVENT_TABLE() | |
284 | wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow); | |
285 | }; | |
286 | ||
287 | BEGIN_EVENT_TABLE(wxDataViewHeaderWindow, wxHeaderCtrl) | |
288 | EVT_HEADER_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnClick) | |
289 | EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnRClick) | |
290 | ||
291 | EVT_HEADER_RESIZING(wxID_ANY, wxDataViewHeaderWindow::OnResize) | |
292 | EVT_HEADER_END_RESIZE(wxID_ANY, wxDataViewHeaderWindow::OnResize) | |
293 | ||
294 | EVT_HEADER_END_REORDER(wxID_ANY, wxDataViewHeaderWindow::OnEndReorder) | |
295 | END_EVENT_TABLE() | |
296 | ||
297 | //----------------------------------------------------------------------------- | |
298 | // wxDataViewRenameTimer | |
299 | //----------------------------------------------------------------------------- | |
300 | ||
301 | class wxDataViewRenameTimer: public wxTimer | |
302 | { | |
303 | private: | |
304 | wxDataViewMainWindow *m_owner; | |
305 | ||
306 | public: | |
307 | wxDataViewRenameTimer( wxDataViewMainWindow *owner ); | |
308 | void Notify(); | |
309 | }; | |
310 | ||
311 | //----------------------------------------------------------------------------- | |
312 | // wxDataViewTreeNode | |
313 | //----------------------------------------------------------------------------- | |
314 | ||
315 | class wxDataViewTreeNode; | |
316 | WX_DEFINE_ARRAY( wxDataViewTreeNode *, wxDataViewTreeNodes ); | |
317 | ||
318 | int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1, | |
319 | wxDataViewTreeNode ** node2); | |
320 | ||
321 | class wxDataViewTreeNode | |
322 | { | |
323 | public: | |
324 | wxDataViewTreeNode(wxDataViewTreeNode *parent, const wxDataViewItem& item) | |
325 | : m_parent(parent), | |
326 | m_item(item), | |
327 | m_branchData(NULL) | |
328 | { | |
329 | } | |
330 | ||
331 | ~wxDataViewTreeNode() | |
332 | { | |
333 | if ( m_branchData ) | |
334 | { | |
335 | wxDataViewTreeNodes& nodes = m_branchData->children; | |
336 | for ( wxDataViewTreeNodes::iterator i = nodes.begin(); | |
337 | i != nodes.end(); | |
338 | ++i ) | |
339 | { | |
340 | delete *i; | |
341 | } | |
342 | ||
343 | delete m_branchData; | |
344 | } | |
345 | } | |
346 | ||
347 | static wxDataViewTreeNode* CreateRootNode() | |
348 | { | |
349 | wxDataViewTreeNode *n = new wxDataViewTreeNode(NULL, wxDataViewItem()); | |
350 | n->SetHasChildren(true); | |
351 | n->m_branchData->open = true; | |
352 | return n; | |
353 | } | |
354 | ||
355 | wxDataViewTreeNode * GetParent() const { return m_parent; } | |
356 | ||
357 | const wxDataViewTreeNodes& GetChildNodes() const | |
358 | { | |
359 | wxASSERT( m_branchData != NULL ); | |
360 | return m_branchData->children; | |
361 | } | |
362 | ||
363 | void InsertChild(wxDataViewTreeNode *node, unsigned index) | |
364 | { | |
365 | if ( !m_branchData ) | |
366 | m_branchData = new BranchNodeData; | |
367 | ||
368 | m_branchData->children.Insert(node, index); | |
369 | ||
370 | // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n) | |
371 | if (g_column >= -1) | |
372 | m_branchData->children.Sort( &wxGenericTreeModelNodeCmp ); | |
373 | } | |
374 | ||
375 | void RemoveChild(wxDataViewTreeNode *node) | |
376 | { | |
377 | wxCHECK_RET( m_branchData != NULL, "leaf node doesn't have children" ); | |
378 | m_branchData->children.Remove(node); | |
379 | } | |
380 | ||
381 | // returns position of child node for given item in children list or wxNOT_FOUND | |
382 | int FindChildByItem(const wxDataViewItem& item) const | |
383 | { | |
384 | if ( !m_branchData ) | |
385 | return wxNOT_FOUND; | |
386 | ||
387 | const wxDataViewTreeNodes& nodes = m_branchData->children; | |
388 | const int len = nodes.size(); | |
389 | for ( int i = 0; i < len; i++ ) | |
390 | { | |
391 | if ( nodes[i]->m_item == item ) | |
392 | return i; | |
393 | } | |
394 | return wxNOT_FOUND; | |
395 | } | |
396 | ||
397 | const wxDataViewItem & GetItem() const { return m_item; } | |
398 | void SetItem( const wxDataViewItem & item ) { m_item = item; } | |
399 | ||
400 | int GetIndentLevel() const | |
401 | { | |
402 | int ret = 0; | |
403 | const wxDataViewTreeNode * node = this; | |
404 | while( node->GetParent()->GetParent() != NULL ) | |
405 | { | |
406 | node = node->GetParent(); | |
407 | ret ++; | |
408 | } | |
409 | return ret; | |
410 | } | |
411 | ||
412 | bool IsOpen() const | |
413 | { | |
414 | return m_branchData && m_branchData->open; | |
415 | } | |
416 | ||
417 | void ToggleOpen() | |
418 | { | |
419 | wxCHECK_RET( m_branchData != NULL, "can't open leaf node" ); | |
420 | ||
421 | int sum = 0; | |
422 | ||
423 | const wxDataViewTreeNodes& nodes = m_branchData->children; | |
424 | const int len = nodes.GetCount(); | |
425 | for ( int i = 0;i < len; i ++) | |
426 | sum += 1 + nodes[i]->GetSubTreeCount(); | |
427 | ||
428 | if (m_branchData->open) | |
429 | { | |
430 | ChangeSubTreeCount(-sum); | |
431 | m_branchData->open = !m_branchData->open; | |
432 | } | |
433 | else | |
434 | { | |
435 | m_branchData->open = !m_branchData->open; | |
436 | ChangeSubTreeCount(+sum); | |
437 | } | |
438 | } | |
439 | ||
440 | // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true | |
441 | // even if GetChildNodes() is empty; see below. | |
442 | bool HasChildren() const | |
443 | { | |
444 | return m_branchData != NULL; | |
445 | } | |
446 | ||
447 | void SetHasChildren(bool has) | |
448 | { | |
449 | if ( !has ) | |
450 | { | |
451 | wxDELETE(m_branchData); | |
452 | } | |
453 | else if ( m_branchData == NULL ) | |
454 | { | |
455 | m_branchData = new BranchNodeData; | |
456 | } | |
457 | } | |
458 | ||
459 | int GetSubTreeCount() const | |
460 | { | |
461 | return m_branchData ? m_branchData->subTreeCount : 0; | |
462 | } | |
463 | ||
464 | void ChangeSubTreeCount( int num ) | |
465 | { | |
466 | wxASSERT( m_branchData != NULL ); | |
467 | ||
468 | if( !m_branchData->open ) | |
469 | return; | |
470 | ||
471 | m_branchData->subTreeCount += num; | |
472 | wxASSERT( m_branchData->subTreeCount >= 0 ); | |
473 | ||
474 | if( m_parent ) | |
475 | m_parent->ChangeSubTreeCount(num); | |
476 | } | |
477 | ||
478 | void Resort() | |
479 | { | |
480 | if ( !m_branchData ) | |
481 | return; | |
482 | ||
483 | if (g_column >= -1) | |
484 | { | |
485 | wxDataViewTreeNodes& nodes = m_branchData->children; | |
486 | ||
487 | nodes.Sort( &wxGenericTreeModelNodeCmp ); | |
488 | int len = nodes.GetCount(); | |
489 | for (int i = 0; i < len; i ++) | |
490 | { | |
491 | if ( nodes[i]->HasChildren() ) | |
492 | nodes[i]->Resort(); | |
493 | } | |
494 | } | |
495 | } | |
496 | ||
497 | ||
498 | private: | |
499 | wxDataViewTreeNode *m_parent; | |
500 | ||
501 | // Corresponding model item. | |
502 | wxDataViewItem m_item; | |
503 | ||
504 | // Data specific to non-leaf (branch, inner) nodes. They are kept in a | |
505 | // separate struct in order to conserve memory. | |
506 | struct BranchNodeData | |
507 | { | |
508 | BranchNodeData() | |
509 | : open(false), | |
510 | subTreeCount(0) | |
511 | { | |
512 | } | |
513 | ||
514 | // Child nodes. Note that this may be empty even if m_hasChildren in | |
515 | // case this branch of the tree wasn't expanded and realized yet. | |
516 | wxDataViewTreeNodes children; | |
517 | ||
518 | // Is the branch node currently open (expanded)? | |
519 | bool open; | |
520 | ||
521 | // Total count of expanded (i.e. visible with the help of some | |
522 | // scrolling) items in the subtree, but excluding this node. I.e. it is | |
523 | // 0 for leaves and is the number of rows the subtree occupies for | |
524 | // branch nodes. | |
525 | int subTreeCount; | |
526 | }; | |
527 | ||
528 | BranchNodeData *m_branchData; | |
529 | }; | |
530 | ||
531 | ||
532 | int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1, | |
533 | wxDataViewTreeNode ** node2) | |
534 | { | |
535 | return g_model->Compare( (*node1)->GetItem(), (*node2)->GetItem(), g_column, g_asending ); | |
536 | } | |
537 | ||
538 | ||
539 | //----------------------------------------------------------------------------- | |
540 | // wxDataViewMainWindow | |
541 | //----------------------------------------------------------------------------- | |
542 | ||
543 | WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection); | |
544 | ||
545 | class wxDataViewMainWindow: public wxWindow | |
546 | { | |
547 | public: | |
548 | wxDataViewMainWindow( wxDataViewCtrl *parent, | |
549 | wxWindowID id, | |
550 | const wxPoint &pos = wxDefaultPosition, | |
551 | const wxSize &size = wxDefaultSize, | |
552 | const wxString &name = wxT("wxdataviewctrlmainwindow") ); | |
553 | virtual ~wxDataViewMainWindow(); | |
554 | ||
555 | bool IsList() const { return GetModel()->IsListModel(); } | |
556 | bool IsVirtualList() const { return m_root == NULL; } | |
557 | ||
558 | // notifications from wxDataViewModel | |
559 | bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ); | |
560 | bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ); | |
561 | bool ItemChanged( const wxDataViewItem &item ); | |
562 | bool ValueChanged( const wxDataViewItem &item, unsigned int model_column ); | |
563 | bool Cleared(); | |
564 | void Resort() | |
565 | { | |
566 | if (!IsVirtualList()) | |
567 | { | |
568 | SortPrepare(); | |
569 | m_root->Resort(); | |
570 | } | |
571 | UpdateDisplay(); | |
572 | } | |
573 | ||
574 | void SortPrepare() | |
575 | { | |
576 | g_model = GetModel(); | |
577 | wxDataViewColumn* col = GetOwner()->GetSortingColumn(); | |
578 | if( !col ) | |
579 | { | |
580 | if (g_model->HasDefaultCompare()) | |
581 | g_column = -1; | |
582 | else | |
583 | g_column = -2; | |
584 | ||
585 | g_asending = true; | |
586 | return; | |
587 | } | |
588 | g_column = col->GetModelColumn(); | |
589 | g_asending = col->IsSortOrderAscending(); | |
590 | } | |
591 | ||
592 | void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; } | |
593 | wxDataViewCtrl *GetOwner() { return m_owner; } | |
594 | const wxDataViewCtrl *GetOwner() const { return m_owner; } | |
595 | ||
596 | wxDataViewModel* GetModel() { return GetOwner()->GetModel(); } | |
597 | const wxDataViewModel* GetModel() const { return GetOwner()->GetModel(); } | |
598 | ||
599 | #if wxUSE_DRAG_AND_DROP | |
600 | wxBitmap CreateItemBitmap( unsigned int row, int &indent ); | |
601 | #endif // wxUSE_DRAG_AND_DROP | |
602 | void OnPaint( wxPaintEvent &event ); | |
603 | void OnCharHook( wxKeyEvent &event ); | |
604 | void OnChar( wxKeyEvent &event ); | |
605 | void OnVerticalNavigation(unsigned int newCurrent, const wxKeyEvent& event); | |
606 | void OnLeftKey(); | |
607 | void OnRightKey(); | |
608 | void OnMouse( wxMouseEvent &event ); | |
609 | void OnSetFocus( wxFocusEvent &event ); | |
610 | void OnKillFocus( wxFocusEvent &event ); | |
611 | ||
612 | void UpdateDisplay(); | |
613 | void RecalculateDisplay(); | |
614 | void OnInternalIdle(); | |
615 | ||
616 | void OnRenameTimer(); | |
617 | ||
618 | void ScrollWindow( int dx, int dy, const wxRect *rect = NULL ); | |
619 | void ScrollTo( int rows, int column ); | |
620 | ||
621 | unsigned GetCurrentRow() const { return m_currentRow; } | |
622 | bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; } | |
623 | void ChangeCurrentRow( unsigned int row ); | |
624 | bool TryAdvanceCurrentColumn(wxDataViewTreeNode *node, bool forward); | |
625 | ||
626 | wxDataViewColumn *GetCurrentColumn() const { return m_currentCol; } | |
627 | void ClearCurrentColumn() { m_currentCol = NULL; } | |
628 | ||
629 | bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); } | |
630 | bool IsEmpty() { return GetRowCount() == 0; } | |
631 | ||
632 | int GetCountPerPage() const; | |
633 | int GetEndOfLastCol() const; | |
634 | unsigned int GetFirstVisibleRow() const; | |
635 | ||
636 | // I change this method to un const because in the tree view, | |
637 | // the displaying number of the tree are changing along with the | |
638 | // expanding/collapsing of the tree nodes | |
639 | unsigned int GetLastVisibleRow(); | |
640 | unsigned int GetRowCount() const; | |
641 | ||
642 | const wxDataViewSelection& GetSelections() const { return m_selection; } | |
643 | void SetSelections( const wxDataViewSelection & sel ) | |
644 | { m_selection = sel; UpdateDisplay(); } | |
645 | void Select( const wxArrayInt& aSelections ); | |
646 | void SelectAllRows( bool on ); | |
647 | void SelectRow( unsigned int row, bool on ); | |
648 | void SelectRows( unsigned int from, unsigned int to, bool on ); | |
649 | void ReverseRowSelection( unsigned int row ); | |
650 | bool IsRowSelected( unsigned int row ); | |
651 | void SendSelectionChangedEvent( const wxDataViewItem& item); | |
652 | ||
653 | void RefreshRow( unsigned int row ); | |
654 | void RefreshRows( unsigned int from, unsigned int to ); | |
655 | void RefreshRowsAfter( unsigned int firstRow ); | |
656 | ||
657 | // returns the colour to be used for drawing the rules | |
658 | wxColour GetRuleColour() const | |
659 | { | |
660 | return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT); | |
661 | } | |
662 | ||
663 | wxRect GetLineRect( unsigned int row ) const; | |
664 | ||
665 | int GetLineStart( unsigned int row ) const; // row * m_lineHeight in fixed mode | |
666 | int GetLineHeight( unsigned int row ) const; // m_lineHeight in fixed mode | |
667 | int GetLineAt( unsigned int y ) const; // y / m_lineHeight in fixed mode | |
668 | ||
669 | void SetRowHeight( int lineHeight ) { m_lineHeight = lineHeight; } | |
670 | int GetRowHeight() const { return m_lineHeight; } | |
671 | ||
672 | // Some useful functions for row and item mapping | |
673 | wxDataViewItem GetItemByRow( unsigned int row ) const; | |
674 | int GetRowByItem( const wxDataViewItem & item ) const; | |
675 | ||
676 | wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row ) const; | |
677 | // We did not need this temporarily | |
678 | // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item ); | |
679 | ||
680 | // Methods for building the mapping tree | |
681 | void BuildTree( wxDataViewModel * model ); | |
682 | void DestroyTree(); | |
683 | void HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column ); | |
684 | wxRect GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column ); | |
685 | ||
686 | void Expand( unsigned int row ); | |
687 | void Collapse( unsigned int row ); | |
688 | bool IsExpanded( unsigned int row ) const; | |
689 | bool HasChildren( unsigned int row ) const; | |
690 | ||
691 | #if wxUSE_DRAG_AND_DROP | |
692 | bool EnableDragSource( const wxDataFormat &format ); | |
693 | bool EnableDropTarget( const wxDataFormat &format ); | |
694 | ||
695 | void RemoveDropHint(); | |
696 | wxDragResult OnDragOver( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def ); | |
697 | bool OnDrop( wxDataFormat format, wxCoord x, wxCoord y ); | |
698 | wxDragResult OnData( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def ); | |
699 | void OnLeave(); | |
700 | #endif // wxUSE_DRAG_AND_DROP | |
701 | ||
702 | void OnColumnsCountChanged(); | |
703 | ||
704 | // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the | |
705 | // specified item in the given column. | |
706 | void StartEditing(const wxDataViewItem& item, const wxDataViewColumn* col); | |
707 | ||
708 | private: | |
709 | int RecalculateCount() const; | |
710 | ||
711 | // Return false only if the event was vetoed by its handler. | |
712 | bool SendExpanderEvent(wxEventType type, const wxDataViewItem& item); | |
713 | ||
714 | wxDataViewTreeNode * FindNode( const wxDataViewItem & item ); | |
715 | ||
716 | wxDataViewColumn *FindColumnForEditing(const wxDataViewItem& item, wxDataViewCellMode mode); | |
717 | ||
718 | bool IsCellEditableInMode(const wxDataViewItem& item, const wxDataViewColumn *col, wxDataViewCellMode mode) const; | |
719 | ||
720 | void DrawCellBackground( wxDataViewRenderer* cell, wxDC& dc, const wxRect& rect ); | |
721 | ||
722 | private: | |
723 | wxDataViewCtrl *m_owner; | |
724 | int m_lineHeight; | |
725 | bool m_dirty; | |
726 | ||
727 | wxDataViewColumn *m_currentCol; | |
728 | unsigned int m_currentRow; | |
729 | wxDataViewSelection m_selection; | |
730 | ||
731 | wxDataViewRenameTimer *m_renameTimer; | |
732 | bool m_lastOnSame; | |
733 | ||
734 | bool m_hasFocus; | |
735 | bool m_useCellFocus; | |
736 | bool m_currentColSetByKeyboard; | |
737 | ||
738 | #if wxUSE_DRAG_AND_DROP | |
739 | int m_dragCount; | |
740 | wxPoint m_dragStart; | |
741 | ||
742 | bool m_dragEnabled; | |
743 | wxDataFormat m_dragFormat; | |
744 | ||
745 | bool m_dropEnabled; | |
746 | wxDataFormat m_dropFormat; | |
747 | bool m_dropHint; | |
748 | unsigned int m_dropHintLine; | |
749 | #endif // wxUSE_DRAG_AND_DROP | |
750 | ||
751 | // for double click logic | |
752 | unsigned int m_lineLastClicked, | |
753 | m_lineBeforeLastClicked, | |
754 | m_lineSelectSingleOnUp; | |
755 | ||
756 | // the pen used to draw horiz/vertical rules | |
757 | wxPen m_penRule; | |
758 | ||
759 | // the pen used to draw the expander and the lines | |
760 | wxPen m_penExpander; | |
761 | ||
762 | // This is the tree structure of the model | |
763 | wxDataViewTreeNode * m_root; | |
764 | int m_count; | |
765 | ||
766 | // This is the tree node under the cursor | |
767 | wxDataViewTreeNode * m_underMouse; | |
768 | ||
769 | // The control used for editing or NULL. | |
770 | wxWeakRef<wxWindow> m_editorCtrl; | |
771 | ||
772 | // Id m_editorCtrl is non-NULL, pointer to the associated renderer. | |
773 | wxDataViewRenderer* m_editorRenderer; | |
774 | ||
775 | private: | |
776 | DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow) | |
777 | DECLARE_EVENT_TABLE() | |
778 | }; | |
779 | ||
780 | // --------------------------------------------------------- | |
781 | // wxGenericDataViewModelNotifier | |
782 | // --------------------------------------------------------- | |
783 | ||
784 | class wxGenericDataViewModelNotifier: public wxDataViewModelNotifier | |
785 | { | |
786 | public: | |
787 | wxGenericDataViewModelNotifier( wxDataViewMainWindow *mainWindow ) | |
788 | { m_mainWindow = mainWindow; } | |
789 | ||
790 | virtual bool ItemAdded( const wxDataViewItem & parent, const wxDataViewItem & item ) | |
791 | { return m_mainWindow->ItemAdded( parent , item ); } | |
792 | virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ) | |
793 | { return m_mainWindow->ItemDeleted( parent, item ); } | |
794 | virtual bool ItemChanged( const wxDataViewItem & item ) | |
795 | { return m_mainWindow->ItemChanged(item); } | |
796 | virtual bool ValueChanged( const wxDataViewItem & item , unsigned int col ) | |
797 | { return m_mainWindow->ValueChanged( item, col ); } | |
798 | virtual bool Cleared() | |
799 | { return m_mainWindow->Cleared(); } | |
800 | virtual void Resort() | |
801 | { m_mainWindow->Resort(); } | |
802 | ||
803 | wxDataViewMainWindow *m_mainWindow; | |
804 | }; | |
805 | ||
806 | // --------------------------------------------------------- | |
807 | // wxDataViewRenderer | |
808 | // --------------------------------------------------------- | |
809 | ||
810 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase) | |
811 | ||
812 | wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, | |
813 | wxDataViewCellMode mode, | |
814 | int align) : | |
815 | wxDataViewCustomRendererBase( varianttype, mode, align ) | |
816 | { | |
817 | m_align = align; | |
818 | m_mode = mode; | |
819 | m_ellipsizeMode = wxELLIPSIZE_MIDDLE; | |
820 | m_dc = NULL; | |
821 | } | |
822 | ||
823 | wxDataViewRenderer::~wxDataViewRenderer() | |
824 | { | |
825 | delete m_dc; | |
826 | } | |
827 | ||
828 | wxDC *wxDataViewRenderer::GetDC() | |
829 | { | |
830 | if (m_dc == NULL) | |
831 | { | |
832 | if (GetOwner() == NULL) | |
833 | return NULL; | |
834 | if (GetOwner()->GetOwner() == NULL) | |
835 | return NULL; | |
836 | m_dc = new wxClientDC( GetOwner()->GetOwner() ); | |
837 | } | |
838 | ||
839 | return m_dc; | |
840 | } | |
841 | ||
842 | void wxDataViewRenderer::SetAlignment( int align ) | |
843 | { | |
844 | m_align=align; | |
845 | } | |
846 | ||
847 | int wxDataViewRenderer::GetAlignment() const | |
848 | { | |
849 | return m_align; | |
850 | } | |
851 | ||
852 | // --------------------------------------------------------- | |
853 | // wxDataViewCustomRenderer | |
854 | // --------------------------------------------------------- | |
855 | ||
856 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer) | |
857 | ||
858 | wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype, | |
859 | wxDataViewCellMode mode, int align ) : | |
860 | wxDataViewRenderer( varianttype, mode, align ) | |
861 | { | |
862 | } | |
863 | ||
864 | // --------------------------------------------------------- | |
865 | // wxDataViewTextRenderer | |
866 | // --------------------------------------------------------- | |
867 | ||
868 | IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer) | |
869 | ||
870 | wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, | |
871 | wxDataViewCellMode mode, int align ) : | |
872 | wxDataViewRenderer( varianttype, mode, align ) | |
873 | { | |
874 | } | |
875 | ||
876 | bool wxDataViewTextRenderer::SetValue( const wxVariant &value ) | |
877 | { | |
878 | m_text = value.GetString(); | |
879 | ||
880 | return true; | |
881 | } | |
882 | ||
883 | bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const | |
884 | { | |
885 | return false; | |
886 | } | |
887 | ||
888 | bool wxDataViewTextRenderer::HasEditorCtrl() const | |
889 | { | |
890 | return true; | |
891 | } | |
892 | ||
893 | wxWindow* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent, | |
894 | wxRect labelRect, const wxVariant &value ) | |
895 | { | |
896 | wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, value, | |
897 | wxPoint(labelRect.x,labelRect.y), | |
898 | wxSize(labelRect.width,labelRect.height), | |
899 | wxTE_PROCESS_ENTER ); | |
900 | ||
901 | // select the text in the control an place the cursor at the end | |
902 | ctrl->SetInsertionPointEnd(); | |
903 | ctrl->SelectAll(); | |
904 | ||
905 | return ctrl; | |
906 | } | |
907 | ||
908 | bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow *editor, wxVariant &value ) | |
909 | { | |
910 | wxTextCtrl *text = (wxTextCtrl*) editor; | |
911 | value = text->GetValue(); | |
912 | return true; | |
913 | } | |
914 | ||
915 | bool wxDataViewTextRenderer::Render(wxRect rect, wxDC *dc, int state) | |
916 | { | |
917 | RenderText(m_text, 0, rect, dc, state); | |
918 | return true; | |
919 | } | |
920 | ||
921 | wxSize wxDataViewTextRenderer::GetSize() const | |
922 | { | |
923 | if (!m_text.empty()) | |
924 | return GetTextExtent(m_text); | |
925 | else | |
926 | return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE); | |
927 | } | |
928 | ||
929 | // --------------------------------------------------------- | |
930 | // wxDataViewBitmapRenderer | |
931 | // --------------------------------------------------------- | |
932 | ||
933 | IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer) | |
934 | ||
935 | wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, | |
936 | wxDataViewCellMode mode, int align ) : | |
937 | wxDataViewRenderer( varianttype, mode, align ) | |
938 | { | |
939 | } | |
940 | ||
941 | bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value ) | |
942 | { | |
943 | if (value.GetType() == wxT("wxBitmap")) | |
944 | m_bitmap << value; | |
945 | if (value.GetType() == wxT("wxIcon")) | |
946 | m_icon << value; | |
947 | ||
948 | return true; | |
949 | } | |
950 | ||
951 | bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) ) const | |
952 | { | |
953 | return false; | |
954 | } | |
955 | ||
956 | bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) | |
957 | { | |
958 | if (m_bitmap.IsOk()) | |
959 | dc->DrawBitmap( m_bitmap, cell.x, cell.y ); | |
960 | else if (m_icon.IsOk()) | |
961 | dc->DrawIcon( m_icon, cell.x, cell.y ); | |
962 | ||
963 | return true; | |
964 | } | |
965 | ||
966 | wxSize wxDataViewBitmapRenderer::GetSize() const | |
967 | { | |
968 | if (m_bitmap.IsOk()) | |
969 | return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() ); | |
970 | else if (m_icon.IsOk()) | |
971 | return wxSize( m_icon.GetWidth(), m_icon.GetHeight() ); | |
972 | ||
973 | return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE); | |
974 | } | |
975 | ||
976 | // --------------------------------------------------------- | |
977 | // wxDataViewToggleRenderer | |
978 | // --------------------------------------------------------- | |
979 | ||
980 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer) | |
981 | ||
982 | wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype, | |
983 | wxDataViewCellMode mode, int align ) : | |
984 | wxDataViewRenderer( varianttype, mode, align ) | |
985 | { | |
986 | m_toggle = false; | |
987 | } | |
988 | ||
989 | bool wxDataViewToggleRenderer::SetValue( const wxVariant &value ) | |
990 | { | |
991 | m_toggle = value.GetBool(); | |
992 | ||
993 | return true; | |
994 | } | |
995 | ||
996 | bool wxDataViewToggleRenderer::GetValue( wxVariant &WXUNUSED(value) ) const | |
997 | { | |
998 | return false; | |
999 | } | |
1000 | ||
1001 | bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) | |
1002 | { | |
1003 | int flags = 0; | |
1004 | if (m_toggle) | |
1005 | flags |= wxCONTROL_CHECKED; | |
1006 | if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE || | |
1007 | GetEnabled() == false) | |
1008 | flags |= wxCONTROL_DISABLED; | |
1009 | ||
1010 | // Ensure that the check boxes always have at least the minimal required | |
1011 | // size, otherwise DrawCheckBox() doesn't really work well. If this size is | |
1012 | // greater than the cell size, the checkbox will be truncated but this is a | |
1013 | // lesser evil. | |
1014 | wxSize size = cell.GetSize(); | |
1015 | size.IncTo(GetSize()); | |
1016 | cell.SetSize(size); | |
1017 | ||
1018 | wxRendererNative::Get().DrawCheckBox( | |
1019 | GetOwner()->GetOwner(), | |
1020 | *dc, | |
1021 | cell, | |
1022 | flags ); | |
1023 | ||
1024 | return true; | |
1025 | } | |
1026 | ||
1027 | bool wxDataViewToggleRenderer::WXActivateCell(const wxRect& WXUNUSED(cell), | |
1028 | wxDataViewModel *model, | |
1029 | const wxDataViewItem& item, | |
1030 | unsigned int col, | |
1031 | const wxMouseEvent *mouseEvent) | |
1032 | { | |
1033 | if ( mouseEvent ) | |
1034 | { | |
1035 | // only react to clicks directly on the checkbox, not elsewhere in the same cell: | |
1036 | if ( !wxRect(GetSize()).Contains(mouseEvent->GetPosition()) ) | |
1037 | return false; | |
1038 | } | |
1039 | ||
1040 | model->ChangeValue(!m_toggle, item, col); | |
1041 | return true; | |
1042 | } | |
1043 | ||
1044 | wxSize wxDataViewToggleRenderer::GetSize() const | |
1045 | { | |
1046 | // the window parameter is not used by GetCheckBoxSize() so it's | |
1047 | // safe to pass NULL | |
1048 | return wxRendererNative::Get().GetCheckBoxSize(NULL); | |
1049 | } | |
1050 | ||
1051 | // --------------------------------------------------------- | |
1052 | // wxDataViewProgressRenderer | |
1053 | // --------------------------------------------------------- | |
1054 | ||
1055 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer, wxDataViewRenderer) | |
1056 | ||
1057 | wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label, | |
1058 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : | |
1059 | wxDataViewRenderer( varianttype, mode, align ) | |
1060 | { | |
1061 | m_label = label; | |
1062 | m_value = 0; | |
1063 | } | |
1064 | ||
1065 | bool wxDataViewProgressRenderer::SetValue( const wxVariant &value ) | |
1066 | { | |
1067 | m_value = (long) value; | |
1068 | ||
1069 | if (m_value < 0) m_value = 0; | |
1070 | if (m_value > 100) m_value = 100; | |
1071 | ||
1072 | return true; | |
1073 | } | |
1074 | ||
1075 | bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const | |
1076 | { | |
1077 | value = (long) m_value; | |
1078 | return true; | |
1079 | } | |
1080 | ||
1081 | bool | |
1082 | wxDataViewProgressRenderer::Render(wxRect rect, wxDC *dc, int WXUNUSED(state)) | |
1083 | { | |
1084 | // deflate the rect to leave a small border between bars in adjacent rows | |
1085 | wxRect bar = rect.Deflate(0, 1); | |
1086 | ||
1087 | dc->SetBrush( *wxTRANSPARENT_BRUSH ); | |
1088 | dc->SetPen( *wxBLACK_PEN ); | |
1089 | dc->DrawRectangle( bar ); | |
1090 | ||
1091 | bar.width = (int)(bar.width * m_value / 100.); | |
1092 | dc->SetPen( *wxTRANSPARENT_PEN ); | |
1093 | ||
1094 | const wxDataViewItemAttr& attr = GetAttr(); | |
1095 | dc->SetBrush( attr.HasColour() ? wxBrush(attr.GetColour()) | |
1096 | : *wxBLUE_BRUSH ); | |
1097 | dc->DrawRectangle( bar ); | |
1098 | ||
1099 | return true; | |
1100 | } | |
1101 | ||
1102 | wxSize wxDataViewProgressRenderer::GetSize() const | |
1103 | { | |
1104 | return wxSize(40,12); | |
1105 | } | |
1106 | ||
1107 | // --------------------------------------------------------- | |
1108 | // wxDataViewIconTextRenderer | |
1109 | // --------------------------------------------------------- | |
1110 | ||
1111 | IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewRenderer) | |
1112 | ||
1113 | wxDataViewIconTextRenderer::wxDataViewIconTextRenderer( | |
1114 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : | |
1115 | wxDataViewRenderer( varianttype, mode, align ) | |
1116 | { | |
1117 | SetMode(mode); | |
1118 | SetAlignment(align); | |
1119 | } | |
1120 | ||
1121 | bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value ) | |
1122 | { | |
1123 | m_value << value; | |
1124 | return true; | |
1125 | } | |
1126 | ||
1127 | bool wxDataViewIconTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const | |
1128 | { | |
1129 | return false; | |
1130 | } | |
1131 | ||
1132 | bool wxDataViewIconTextRenderer::Render(wxRect rect, wxDC *dc, int state) | |
1133 | { | |
1134 | int xoffset = 0; | |
1135 | ||
1136 | const wxIcon& icon = m_value.GetIcon(); | |
1137 | if ( icon.IsOk() ) | |
1138 | { | |
1139 | dc->DrawIcon(icon, rect.x, rect.y + (rect.height - icon.GetHeight())/2); | |
1140 | xoffset = icon.GetWidth()+4; | |
1141 | } | |
1142 | ||
1143 | RenderText(m_value.GetText(), xoffset, rect, dc, state); | |
1144 | ||
1145 | return true; | |
1146 | } | |
1147 | ||
1148 | wxSize wxDataViewIconTextRenderer::GetSize() const | |
1149 | { | |
1150 | if (!m_value.GetText().empty()) | |
1151 | { | |
1152 | wxSize size = GetTextExtent(m_value.GetText()); | |
1153 | ||
1154 | if (m_value.GetIcon().IsOk()) | |
1155 | size.x += m_value.GetIcon().GetWidth() + 4; | |
1156 | return size; | |
1157 | } | |
1158 | return wxSize(80,20); | |
1159 | } | |
1160 | ||
1161 | wxWindow* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value) | |
1162 | { | |
1163 | wxDataViewIconText iconText; | |
1164 | iconText << value; | |
1165 | ||
1166 | wxString text = iconText.GetText(); | |
1167 | ||
1168 | // adjust the label rect to take the width of the icon into account | |
1169 | if (iconText.GetIcon().IsOk()) | |
1170 | { | |
1171 | int w = iconText.GetIcon().GetWidth() + 4; | |
1172 | labelRect.x += w; | |
1173 | labelRect.width -= w; | |
1174 | } | |
1175 | ||
1176 | wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, text, | |
1177 | wxPoint(labelRect.x,labelRect.y), | |
1178 | wxSize(labelRect.width,labelRect.height), | |
1179 | wxTE_PROCESS_ENTER ); | |
1180 | ||
1181 | // select the text in the control an place the cursor at the end | |
1182 | ctrl->SetInsertionPointEnd(); | |
1183 | ctrl->SelectAll(); | |
1184 | ||
1185 | return ctrl; | |
1186 | } | |
1187 | ||
1188 | bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow *editor, wxVariant& value ) | |
1189 | { | |
1190 | wxTextCtrl *text = (wxTextCtrl*) editor; | |
1191 | ||
1192 | // The icon can't be edited so get its old value and reuse it. | |
1193 | wxVariant valueOld; | |
1194 | wxDataViewColumn* const col = GetOwner(); | |
1195 | GetView()->GetModel()->GetValue(valueOld, m_item, col->GetModelColumn()); | |
1196 | ||
1197 | wxDataViewIconText iconText; | |
1198 | iconText << valueOld; | |
1199 | ||
1200 | // But replace the text with the value entered by user. | |
1201 | iconText.SetText(text->GetValue()); | |
1202 | ||
1203 | value << iconText; | |
1204 | return true; | |
1205 | } | |
1206 | ||
1207 | //----------------------------------------------------------------------------- | |
1208 | // wxDataViewDropTarget | |
1209 | //----------------------------------------------------------------------------- | |
1210 | ||
1211 | #if wxUSE_DRAG_AND_DROP | |
1212 | ||
1213 | class wxBitmapCanvas: public wxWindow | |
1214 | { | |
1215 | public: | |
1216 | wxBitmapCanvas( wxWindow *parent, const wxBitmap &bitmap, const wxSize &size ) : | |
1217 | wxWindow( parent, wxID_ANY, wxPoint(0,0), size ) | |
1218 | { | |
1219 | m_bitmap = bitmap; | |
1220 | Connect( wxEVT_PAINT, wxPaintEventHandler(wxBitmapCanvas::OnPaint) ); | |
1221 | } | |
1222 | ||
1223 | void OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
1224 | { | |
1225 | wxPaintDC dc(this); | |
1226 | dc.DrawBitmap( m_bitmap, 0, 0); | |
1227 | } | |
1228 | ||
1229 | wxBitmap m_bitmap; | |
1230 | }; | |
1231 | ||
1232 | class wxDataViewDropSource: public wxDropSource | |
1233 | { | |
1234 | public: | |
1235 | wxDataViewDropSource( wxDataViewMainWindow *win, unsigned int row ) : | |
1236 | wxDropSource( win ) | |
1237 | { | |
1238 | m_win = win; | |
1239 | m_row = row; | |
1240 | m_hint = NULL; | |
1241 | } | |
1242 | ||
1243 | ~wxDataViewDropSource() | |
1244 | { | |
1245 | delete m_hint; | |
1246 | } | |
1247 | ||
1248 | virtual bool GiveFeedback( wxDragResult WXUNUSED(effect) ) | |
1249 | { | |
1250 | wxPoint pos = wxGetMousePosition(); | |
1251 | ||
1252 | if (!m_hint) | |
1253 | { | |
1254 | int liney = m_win->GetLineStart( m_row ); | |
1255 | int linex = 0; | |
1256 | m_win->GetOwner()->CalcUnscrolledPosition( 0, liney, NULL, &liney ); | |
1257 | m_win->ClientToScreen( &linex, &liney ); | |
1258 | m_dist_x = pos.x - linex; | |
1259 | m_dist_y = pos.y - liney; | |
1260 | ||
1261 | int indent = 0; | |
1262 | wxBitmap ib = m_win->CreateItemBitmap( m_row, indent ); | |
1263 | m_dist_x -= indent; | |
1264 | m_hint = new wxFrame( m_win->GetParent(), wxID_ANY, wxEmptyString, | |
1265 | wxPoint(pos.x - m_dist_x, pos.y + 5 ), | |
1266 | ib.GetSize(), | |
1267 | wxFRAME_TOOL_WINDOW | | |
1268 | wxFRAME_FLOAT_ON_PARENT | | |
1269 | wxFRAME_NO_TASKBAR | | |
1270 | wxNO_BORDER ); | |
1271 | new wxBitmapCanvas( m_hint, ib, ib.GetSize() ); | |
1272 | m_hint->Show(); | |
1273 | } | |
1274 | else | |
1275 | { | |
1276 | m_hint->Move( pos.x - m_dist_x, pos.y + 5 ); | |
1277 | m_hint->SetTransparent( 128 ); | |
1278 | } | |
1279 | ||
1280 | return false; | |
1281 | } | |
1282 | ||
1283 | wxDataViewMainWindow *m_win; | |
1284 | unsigned int m_row; | |
1285 | wxFrame *m_hint; | |
1286 | int m_dist_x,m_dist_y; | |
1287 | }; | |
1288 | ||
1289 | ||
1290 | class wxDataViewDropTarget: public wxDropTarget | |
1291 | { | |
1292 | public: | |
1293 | wxDataViewDropTarget( wxDataObject *obj, wxDataViewMainWindow *win ) : | |
1294 | wxDropTarget( obj ) | |
1295 | { | |
1296 | m_win = win; | |
1297 | } | |
1298 | ||
1299 | virtual wxDragResult OnDragOver( wxCoord x, wxCoord y, wxDragResult def ) | |
1300 | { | |
1301 | wxDataFormat format = GetMatchingPair(); | |
1302 | if (format == wxDF_INVALID) | |
1303 | return wxDragNone; | |
1304 | return m_win->OnDragOver( format, x, y, def); | |
1305 | } | |
1306 | ||
1307 | virtual bool OnDrop( wxCoord x, wxCoord y ) | |
1308 | { | |
1309 | wxDataFormat format = GetMatchingPair(); | |
1310 | if (format == wxDF_INVALID) | |
1311 | return false; | |
1312 | return m_win->OnDrop( format, x, y ); | |
1313 | } | |
1314 | ||
1315 | virtual wxDragResult OnData( wxCoord x, wxCoord y, wxDragResult def ) | |
1316 | { | |
1317 | wxDataFormat format = GetMatchingPair(); | |
1318 | if (format == wxDF_INVALID) | |
1319 | return wxDragNone; | |
1320 | if (!GetData()) | |
1321 | return wxDragNone; | |
1322 | return m_win->OnData( format, x, y, def ); | |
1323 | } | |
1324 | ||
1325 | virtual void OnLeave() | |
1326 | { m_win->OnLeave(); } | |
1327 | ||
1328 | wxDataViewMainWindow *m_win; | |
1329 | }; | |
1330 | ||
1331 | #endif // wxUSE_DRAG_AND_DROP | |
1332 | ||
1333 | //----------------------------------------------------------------------------- | |
1334 | // wxDataViewRenameTimer | |
1335 | //----------------------------------------------------------------------------- | |
1336 | ||
1337 | wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner ) | |
1338 | { | |
1339 | m_owner = owner; | |
1340 | } | |
1341 | ||
1342 | void wxDataViewRenameTimer::Notify() | |
1343 | { | |
1344 | m_owner->OnRenameTimer(); | |
1345 | } | |
1346 | ||
1347 | //----------------------------------------------------------------------------- | |
1348 | // wxDataViewMainWindow | |
1349 | //----------------------------------------------------------------------------- | |
1350 | ||
1351 | // The tree building helper, declared firstly | |
1352 | static void BuildTreeHelper( const wxDataViewModel * model, const wxDataViewItem & item, | |
1353 | wxDataViewTreeNode * node); | |
1354 | ||
1355 | int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 ) | |
1356 | { | |
1357 | if (row1 > row2) return 1; | |
1358 | if (row1 == row2) return 0; | |
1359 | return -1; | |
1360 | } | |
1361 | ||
1362 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow) | |
1363 | ||
1364 | BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow) | |
1365 | EVT_PAINT (wxDataViewMainWindow::OnPaint) | |
1366 | EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse) | |
1367 | EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus) | |
1368 | EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus) | |
1369 | EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook) | |
1370 | EVT_CHAR (wxDataViewMainWindow::OnChar) | |
1371 | END_EVENT_TABLE() | |
1372 | ||
1373 | wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id, | |
1374 | const wxPoint &pos, const wxSize &size, const wxString &name ) : | |
1375 | wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE, name ), | |
1376 | m_selection( wxDataViewSelectionCmp ) | |
1377 | ||
1378 | { | |
1379 | SetOwner( parent ); | |
1380 | ||
1381 | m_editorRenderer = NULL; | |
1382 | ||
1383 | m_lastOnSame = false; | |
1384 | m_renameTimer = new wxDataViewRenameTimer( this ); | |
1385 | ||
1386 | // TODO: user better initial values/nothing selected | |
1387 | m_currentCol = NULL; | |
1388 | m_currentColSetByKeyboard = false; | |
1389 | m_useCellFocus = false; | |
1390 | m_currentRow = 0; | |
1391 | ||
1392 | #ifdef __WXMSW__ | |
1393 | // We would like to use the same line height that Explorer uses. This is | |
1394 | // different from standard ListView control since Vista. | |
1395 | if ( wxGetWinVersion() >= wxWinVersion_Vista ) | |
1396 | m_lineHeight = wxMax(16, GetCharHeight()) + 6; // 16 = mini icon height | |
1397 | else | |
1398 | #endif // __WXMSW__ | |
1399 | m_lineHeight = wxMax(16, GetCharHeight()) + 1; // 16 = mini icon height | |
1400 | ||
1401 | #if wxUSE_DRAG_AND_DROP | |
1402 | m_dragCount = 0; | |
1403 | m_dragStart = wxPoint(0,0); | |
1404 | ||
1405 | m_dragEnabled = false; | |
1406 | m_dropEnabled = false; | |
1407 | m_dropHint = false; | |
1408 | m_dropHintLine = (unsigned int) -1; | |
1409 | #endif // wxUSE_DRAG_AND_DROP | |
1410 | ||
1411 | m_lineLastClicked = (unsigned int) -1; | |
1412 | m_lineBeforeLastClicked = (unsigned int) -1; | |
1413 | m_lineSelectSingleOnUp = (unsigned int) -1; | |
1414 | ||
1415 | m_hasFocus = false; | |
1416 | ||
1417 | SetBackgroundColour( *wxWHITE ); | |
1418 | ||
1419 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); | |
1420 | ||
1421 | m_penRule = wxPen(GetRuleColour()); | |
1422 | ||
1423 | // compose a pen whichcan draw black lines | |
1424 | // TODO: maybe there is something system colour to use | |
1425 | m_penExpander = wxPen(wxColour(0,0,0)); | |
1426 | ||
1427 | m_root = wxDataViewTreeNode::CreateRootNode(); | |
1428 | ||
1429 | // Make m_count = -1 will cause the class recaculate the real displaying number of rows. | |
1430 | m_count = -1; | |
1431 | m_underMouse = NULL; | |
1432 | UpdateDisplay(); | |
1433 | } | |
1434 | ||
1435 | wxDataViewMainWindow::~wxDataViewMainWindow() | |
1436 | { | |
1437 | DestroyTree(); | |
1438 | delete m_renameTimer; | |
1439 | } | |
1440 | ||
1441 | ||
1442 | #if wxUSE_DRAG_AND_DROP | |
1443 | bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat &format ) | |
1444 | { | |
1445 | m_dragFormat = format; | |
1446 | m_dragEnabled = format != wxDF_INVALID; | |
1447 | ||
1448 | return true; | |
1449 | } | |
1450 | ||
1451 | bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat &format ) | |
1452 | { | |
1453 | m_dropFormat = format; | |
1454 | m_dropEnabled = format != wxDF_INVALID; | |
1455 | ||
1456 | if (m_dropEnabled) | |
1457 | SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format ), this ) ); | |
1458 | ||
1459 | return true; | |
1460 | } | |
1461 | ||
1462 | void wxDataViewMainWindow::RemoveDropHint() | |
1463 | { | |
1464 | if (m_dropHint) | |
1465 | { | |
1466 | m_dropHint = false; | |
1467 | RefreshRow( m_dropHintLine ); | |
1468 | m_dropHintLine = (unsigned int) -1; | |
1469 | } | |
1470 | } | |
1471 | ||
1472 | wxDragResult wxDataViewMainWindow::OnDragOver( wxDataFormat format, wxCoord x, | |
1473 | wxCoord y, wxDragResult def ) | |
1474 | { | |
1475 | int xx = x; | |
1476 | int yy = y; | |
1477 | m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy ); | |
1478 | unsigned int row = GetLineAt( yy ); | |
1479 | ||
1480 | if ((row >= GetRowCount()) || (xx > GetEndOfLastCol())) | |
1481 | { | |
1482 | RemoveDropHint(); | |
1483 | return wxDragNone; | |
1484 | } | |
1485 | ||
1486 | wxDataViewItem item = GetItemByRow( row ); | |
1487 | ||
1488 | wxDataViewModel *model = GetModel(); | |
1489 | ||
1490 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() ); | |
1491 | event.SetEventObject( m_owner ); | |
1492 | event.SetItem( item ); | |
1493 | event.SetModel( model ); | |
1494 | event.SetDataFormat( format ); | |
1495 | event.SetDropEffect( def ); | |
1496 | if (!m_owner->HandleWindowEvent( event )) | |
1497 | { | |
1498 | RemoveDropHint(); | |
1499 | return wxDragNone; | |
1500 | } | |
1501 | ||
1502 | if (!event.IsAllowed()) | |
1503 | { | |
1504 | RemoveDropHint(); | |
1505 | return wxDragNone; | |
1506 | } | |
1507 | ||
1508 | ||
1509 | if (m_dropHint && (row != m_dropHintLine)) | |
1510 | RefreshRow( m_dropHintLine ); | |
1511 | m_dropHint = true; | |
1512 | m_dropHintLine = row; | |
1513 | RefreshRow( row ); | |
1514 | ||
1515 | return def; | |
1516 | } | |
1517 | ||
1518 | bool wxDataViewMainWindow::OnDrop( wxDataFormat format, wxCoord x, wxCoord y ) | |
1519 | { | |
1520 | RemoveDropHint(); | |
1521 | ||
1522 | int xx = x; | |
1523 | int yy = y; | |
1524 | m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy ); | |
1525 | unsigned int row = GetLineAt( yy ); | |
1526 | ||
1527 | if ((row >= GetRowCount()) || (xx > GetEndOfLastCol())) | |
1528 | return false; | |
1529 | ||
1530 | wxDataViewItem item = GetItemByRow( row ); | |
1531 | ||
1532 | wxDataViewModel *model = GetModel(); | |
1533 | ||
1534 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() ); | |
1535 | event.SetEventObject( m_owner ); | |
1536 | event.SetItem( item ); | |
1537 | event.SetModel( model ); | |
1538 | event.SetDataFormat( format ); | |
1539 | if (!m_owner->HandleWindowEvent( event )) | |
1540 | return false; | |
1541 | ||
1542 | if (!event.IsAllowed()) | |
1543 | return false; | |
1544 | ||
1545 | return true; | |
1546 | } | |
1547 | ||
1548 | wxDragResult wxDataViewMainWindow::OnData( wxDataFormat format, wxCoord x, wxCoord y, | |
1549 | wxDragResult def ) | |
1550 | { | |
1551 | int xx = x; | |
1552 | int yy = y; | |
1553 | m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy ); | |
1554 | unsigned int row = GetLineAt( yy ); | |
1555 | ||
1556 | if ((row >= GetRowCount()) || (xx > GetEndOfLastCol())) | |
1557 | return wxDragNone; | |
1558 | ||
1559 | wxDataViewItem item = GetItemByRow( row ); | |
1560 | ||
1561 | wxDataViewModel *model = GetModel(); | |
1562 | ||
1563 | wxCustomDataObject *obj = (wxCustomDataObject *) GetDropTarget()->GetDataObject(); | |
1564 | ||
1565 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() ); | |
1566 | event.SetEventObject( m_owner ); | |
1567 | event.SetItem( item ); | |
1568 | event.SetModel( model ); | |
1569 | event.SetDataFormat( format ); | |
1570 | event.SetDataSize( obj->GetSize() ); | |
1571 | event.SetDataBuffer( obj->GetData() ); | |
1572 | event.SetDropEffect( def ); | |
1573 | if (!m_owner->HandleWindowEvent( event )) | |
1574 | return wxDragNone; | |
1575 | ||
1576 | if (!event.IsAllowed()) | |
1577 | return wxDragNone; | |
1578 | ||
1579 | return def; | |
1580 | } | |
1581 | ||
1582 | void wxDataViewMainWindow::OnLeave() | |
1583 | { | |
1584 | RemoveDropHint(); | |
1585 | } | |
1586 | ||
1587 | wxBitmap wxDataViewMainWindow::CreateItemBitmap( unsigned int row, int &indent ) | |
1588 | { | |
1589 | int height = GetLineHeight( row ); | |
1590 | int width = 0; | |
1591 | unsigned int cols = GetOwner()->GetColumnCount(); | |
1592 | unsigned int col; | |
1593 | for (col = 0; col < cols; col++) | |
1594 | { | |
1595 | wxDataViewColumn *column = GetOwner()->GetColumnAt(col); | |
1596 | if (column->IsHidden()) | |
1597 | continue; // skip it! | |
1598 | width += column->GetWidth(); | |
1599 | } | |
1600 | ||
1601 | indent = 0; | |
1602 | if (!IsList()) | |
1603 | { | |
1604 | wxDataViewTreeNode *node = GetTreeNodeByRow(row); | |
1605 | indent = GetOwner()->GetIndent() * node->GetIndentLevel(); | |
1606 | indent = indent + m_lineHeight; | |
1607 | // try to use the m_lineHeight as the expander space | |
1608 | } | |
1609 | width -= indent; | |
1610 | ||
1611 | wxBitmap bitmap( width, height ); | |
1612 | wxMemoryDC dc( bitmap ); | |
1613 | dc.SetFont( GetFont() ); | |
1614 | dc.SetPen( *wxBLACK_PEN ); | |
1615 | dc.SetBrush( *wxWHITE_BRUSH ); | |
1616 | dc.DrawRectangle( 0,0,width,height ); | |
1617 | ||
1618 | wxDataViewModel *model = m_owner->GetModel(); | |
1619 | ||
1620 | wxDataViewColumn * const | |
1621 | expander = GetExpanderColumnOrFirstOne(GetOwner()); | |
1622 | ||
1623 | int x = 0; | |
1624 | for (col = 0; col < cols; col++) | |
1625 | { | |
1626 | wxDataViewColumn *column = GetOwner()->GetColumnAt( col ); | |
1627 | wxDataViewRenderer *cell = column->GetRenderer(); | |
1628 | ||
1629 | if (column->IsHidden()) | |
1630 | continue; // skip it! | |
1631 | ||
1632 | width = column->GetWidth(); | |
1633 | ||
1634 | if (column == expander) | |
1635 | width -= indent; | |
1636 | ||
1637 | wxDataViewItem item = GetItemByRow( row ); | |
1638 | cell->PrepareForItem(model, item, column->GetModelColumn()); | |
1639 | ||
1640 | wxRect item_rect(x, 0, width, height); | |
1641 | item_rect.Deflate(PADDING_RIGHTLEFT, 0); | |
1642 | ||
1643 | // dc.SetClippingRegion( item_rect ); | |
1644 | cell->WXCallRender(item_rect, &dc, 0); | |
1645 | // dc.DestroyClippingRegion(); | |
1646 | ||
1647 | x += width; | |
1648 | } | |
1649 | ||
1650 | return bitmap; | |
1651 | } | |
1652 | ||
1653 | #endif // wxUSE_DRAG_AND_DROP | |
1654 | ||
1655 | ||
1656 | // Draw focus rect for individual cell. Unlike native focus rect, we render | |
1657 | // this in foreground text color (typically white) to enhance contrast and | |
1658 | // make it visible. | |
1659 | static void DrawSelectedCellFocusRect(wxDC& dc, const wxRect& rect) | |
1660 | { | |
1661 | // (This code is based on wxRendererGeneric::DrawFocusRect and modified.) | |
1662 | ||
1663 | // draw the pixels manually because the "dots" in wxPen with wxDOT style | |
1664 | // may be short traits and not really dots | |
1665 | // | |
1666 | // note that to behave in the same manner as DrawRect(), we must exclude | |
1667 | // the bottom and right borders from the rectangle | |
1668 | wxCoord x1 = rect.GetLeft(), | |
1669 | y1 = rect.GetTop(), | |
1670 | x2 = rect.GetRight(), | |
1671 | y2 = rect.GetBottom(); | |
1672 | ||
1673 | wxDCPenChanger pen(dc, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT)); | |
1674 | ||
1675 | wxCoord z; | |
1676 | for ( z = x1 + 1; z < x2; z += 2 ) | |
1677 | dc.DrawPoint(z, rect.GetTop()); | |
1678 | ||
1679 | wxCoord shift = z == x2 ? 0 : 1; | |
1680 | for ( z = y1 + shift; z < y2; z += 2 ) | |
1681 | dc.DrawPoint(x2, z); | |
1682 | ||
1683 | shift = z == y2 ? 0 : 1; | |
1684 | for ( z = x2 - shift; z > x1; z -= 2 ) | |
1685 | dc.DrawPoint(z, y2); | |
1686 | ||
1687 | shift = z == x1 ? 0 : 1; | |
1688 | for ( z = y2 - shift; z > y1; z -= 2 ) | |
1689 | dc.DrawPoint(x1, z); | |
1690 | } | |
1691 | ||
1692 | ||
1693 | void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
1694 | { | |
1695 | wxDataViewModel *model = GetModel(); | |
1696 | wxAutoBufferedPaintDC dc( this ); | |
1697 | ||
1698 | #ifdef __WXMSW__ | |
1699 | dc.SetBrush(GetOwner()->GetBackgroundColour()); | |
1700 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
1701 | dc.DrawRectangle(GetClientSize()); | |
1702 | #endif | |
1703 | ||
1704 | if ( IsEmpty() ) | |
1705 | { | |
1706 | // No items to draw. | |
1707 | return; | |
1708 | } | |
1709 | ||
1710 | // prepare the DC | |
1711 | GetOwner()->PrepareDC( dc ); | |
1712 | dc.SetFont( GetFont() ); | |
1713 | ||
1714 | wxRect update = GetUpdateRegion().GetBox(); | |
1715 | m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y ); | |
1716 | ||
1717 | // compute which items needs to be redrawn | |
1718 | unsigned int item_start = GetLineAt( wxMax(0,update.y) ); | |
1719 | unsigned int item_count = | |
1720 | wxMin( (int)( GetLineAt( wxMax(0,update.y+update.height) ) - item_start + 1), | |
1721 | (int)(GetRowCount( ) - item_start)); | |
1722 | unsigned int item_last = item_start + item_count; | |
1723 | ||
1724 | // Send the event to wxDataViewCtrl itself. | |
1725 | wxWindow * const parent = GetParent(); | |
1726 | wxDataViewEvent cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT, parent->GetId()); | |
1727 | cache_event.SetEventObject(parent); | |
1728 | cache_event.SetCache(item_start, item_last - 1); | |
1729 | parent->ProcessWindowEvent(cache_event); | |
1730 | ||
1731 | // compute which columns needs to be redrawn | |
1732 | unsigned int cols = GetOwner()->GetColumnCount(); | |
1733 | if ( !cols ) | |
1734 | { | |
1735 | // we assume that we have at least one column below and painting an | |
1736 | // empty control is unnecessary anyhow | |
1737 | return; | |
1738 | } | |
1739 | ||
1740 | unsigned int col_start = 0; | |
1741 | unsigned int x_start; | |
1742 | for (x_start = 0; col_start < cols; col_start++) | |
1743 | { | |
1744 | wxDataViewColumn *col = GetOwner()->GetColumnAt(col_start); | |
1745 | if (col->IsHidden()) | |
1746 | continue; // skip it! | |
1747 | ||
1748 | unsigned int w = col->GetWidth(); | |
1749 | if (x_start+w >= (unsigned int)update.x) | |
1750 | break; | |
1751 | ||
1752 | x_start += w; | |
1753 | } | |
1754 | ||
1755 | unsigned int col_last = col_start; | |
1756 | unsigned int x_last = x_start; | |
1757 | for (; col_last < cols; col_last++) | |
1758 | { | |
1759 | wxDataViewColumn *col = GetOwner()->GetColumnAt(col_last); | |
1760 | if (col->IsHidden()) | |
1761 | continue; // skip it! | |
1762 | ||
1763 | if (x_last > (unsigned int)update.GetRight()) | |
1764 | break; | |
1765 | ||
1766 | x_last += col->GetWidth(); | |
1767 | } | |
1768 | ||
1769 | // Draw background of alternate rows specially if required | |
1770 | if ( m_owner->HasFlag(wxDV_ROW_LINES) ) | |
1771 | { | |
1772 | wxColour altRowColour = m_owner->m_alternateRowColour; | |
1773 | if ( !altRowColour.IsOk() ) | |
1774 | { | |
1775 | // Determine the alternate rows colour automatically from the | |
1776 | // background colour. | |
1777 | const wxColour bgColour = m_owner->GetBackgroundColour(); | |
1778 | ||
1779 | // Depending on the background, alternate row color | |
1780 | // will be 3% more dark or 50% brighter. | |
1781 | int alpha = bgColour.GetRGB() > 0x808080 ? 97 : 150; | |
1782 | altRowColour = bgColour.ChangeLightness(alpha); | |
1783 | } | |
1784 | ||
1785 | dc.SetPen(*wxTRANSPARENT_PEN); | |
1786 | dc.SetBrush(wxBrush(altRowColour)); | |
1787 | ||
1788 | for (unsigned int item = item_start; item < item_last; item++) | |
1789 | { | |
1790 | if ( item % 2 ) | |
1791 | { | |
1792 | dc.DrawRectangle(x_start, | |
1793 | GetLineStart(item), | |
1794 | GetClientSize().GetWidth(), | |
1795 | GetLineHeight(item)); | |
1796 | } | |
1797 | } | |
1798 | } | |
1799 | ||
1800 | // Draw horizontal rules if required | |
1801 | if ( m_owner->HasFlag(wxDV_HORIZ_RULES) ) | |
1802 | { | |
1803 | dc.SetPen(m_penRule); | |
1804 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
1805 | ||
1806 | for (unsigned int i = item_start; i <= item_last; i++) | |
1807 | { | |
1808 | int y = GetLineStart( i ); | |
1809 | dc.DrawLine(x_start, y, x_last, y); | |
1810 | } | |
1811 | } | |
1812 | ||
1813 | // Draw vertical rules if required | |
1814 | if ( m_owner->HasFlag(wxDV_VERT_RULES) ) | |
1815 | { | |
1816 | dc.SetPen(m_penRule); | |
1817 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
1818 | ||
1819 | // NB: Vertical rules are drawn in the last pixel of a column so that | |
1820 | // they align perfectly with native MSW wxHeaderCtrl as well as for | |
1821 | // consistency with MSW native list control. There's no vertical | |
1822 | // rule at the most-left side of the control. | |
1823 | ||
1824 | int x = x_start - 1; | |
1825 | for (unsigned int i = col_start; i < col_last; i++) | |
1826 | { | |
1827 | wxDataViewColumn *col = GetOwner()->GetColumnAt(i); | |
1828 | if (col->IsHidden()) | |
1829 | continue; // skip it | |
1830 | ||
1831 | x += col->GetWidth(); | |
1832 | ||
1833 | dc.DrawLine(x, GetLineStart( item_start ), | |
1834 | x, GetLineStart( item_last ) ); | |
1835 | } | |
1836 | } | |
1837 | ||
1838 | // redraw the background for the items which are selected/current | |
1839 | for (unsigned int item = item_start; item < item_last; item++) | |
1840 | { | |
1841 | bool selected = m_selection.Index( item ) != wxNOT_FOUND; | |
1842 | ||
1843 | if (selected || item == m_currentRow) | |
1844 | { | |
1845 | wxRect rect( x_start, GetLineStart( item ), | |
1846 | x_last - x_start, GetLineHeight( item ) ); | |
1847 | ||
1848 | // draw selection and whole-item focus: | |
1849 | if ( selected ) | |
1850 | { | |
1851 | int flags = wxCONTROL_SELECTED; | |
1852 | if (m_hasFocus) | |
1853 | flags |= wxCONTROL_FOCUSED; | |
1854 | ||
1855 | wxRendererNative::Get().DrawItemSelectionRect | |
1856 | ( | |
1857 | this, | |
1858 | dc, | |
1859 | rect, | |
1860 | flags | |
1861 | ); | |
1862 | } | |
1863 | ||
1864 | // draw keyboard focus rect if applicable | |
1865 | if ( item == m_currentRow && m_hasFocus ) | |
1866 | { | |
1867 | bool renderColumnFocus = false; | |
1868 | ||
1869 | if ( m_useCellFocus && m_currentCol && m_currentColSetByKeyboard ) | |
1870 | { | |
1871 | renderColumnFocus = true; | |
1872 | ||
1873 | // If this is container node without columns, render full-row focus: | |
1874 | if ( !IsList() ) | |
1875 | { | |
1876 | wxDataViewTreeNode *node = GetTreeNodeByRow(item); | |
1877 | if ( node->HasChildren() && !model->HasContainerColumns(node->GetItem()) ) | |
1878 | renderColumnFocus = false; | |
1879 | } | |
1880 | } | |
1881 | ||
1882 | if ( renderColumnFocus ) | |
1883 | { | |
1884 | for ( unsigned int i = col_start; i < col_last; i++ ) | |
1885 | { | |
1886 | wxDataViewColumn *col = GetOwner()->GetColumnAt(i); | |
1887 | if ( col->IsHidden() ) | |
1888 | continue; | |
1889 | ||
1890 | rect.width = col->GetWidth(); | |
1891 | ||
1892 | if ( col == m_currentCol ) | |
1893 | { | |
1894 | // make the rect more visible by adding a small | |
1895 | // margin around it: | |
1896 | rect.Deflate(1, 1); | |
1897 | ||
1898 | if ( selected ) | |
1899 | { | |
1900 | // DrawFocusRect() uses XOR and is all but | |
1901 | // invisible against dark-blue background. Use | |
1902 | // the same color used for selected text. | |
1903 | DrawSelectedCellFocusRect(dc, rect); | |
1904 | } | |
1905 | else | |
1906 | { | |
1907 | wxRendererNative::Get().DrawFocusRect | |
1908 | ( | |
1909 | this, | |
1910 | dc, | |
1911 | rect, | |
1912 | 0 | |
1913 | ); | |
1914 | } | |
1915 | break; | |
1916 | } | |
1917 | ||
1918 | rect.x += rect.width; | |
1919 | } | |
1920 | } | |
1921 | else | |
1922 | { | |
1923 | // render focus rectangle for the whole row | |
1924 | wxRendererNative::Get().DrawFocusRect | |
1925 | ( | |
1926 | this, | |
1927 | dc, | |
1928 | rect, | |
1929 | selected ? (int)wxCONTROL_SELECTED : 0 | |
1930 | ); | |
1931 | } | |
1932 | } | |
1933 | } | |
1934 | } | |
1935 | ||
1936 | #if wxUSE_DRAG_AND_DROP | |
1937 | if (m_dropHint) | |
1938 | { | |
1939 | wxRect rect( x_start, GetLineStart( m_dropHintLine ), | |
1940 | x_last - x_start, GetLineHeight( m_dropHintLine ) ); | |
1941 | dc.SetPen( *wxBLACK_PEN ); | |
1942 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); | |
1943 | dc.DrawRectangle( rect ); | |
1944 | } | |
1945 | #endif // wxUSE_DRAG_AND_DROP | |
1946 | ||
1947 | wxDataViewColumn * const | |
1948 | expander = GetExpanderColumnOrFirstOne(GetOwner()); | |
1949 | ||
1950 | // redraw all cells for all rows which must be repainted and all columns | |
1951 | wxRect cell_rect; | |
1952 | cell_rect.x = x_start; | |
1953 | for (unsigned int i = col_start; i < col_last; i++) | |
1954 | { | |
1955 | wxDataViewColumn *col = GetOwner()->GetColumnAt( i ); | |
1956 | wxDataViewRenderer *cell = col->GetRenderer(); | |
1957 | cell_rect.width = col->GetWidth(); | |
1958 | ||
1959 | if ( col->IsHidden() || cell_rect.width <= 0 ) | |
1960 | continue; // skip it! | |
1961 | ||
1962 | for (unsigned int item = item_start; item < item_last; item++) | |
1963 | { | |
1964 | // get the cell value and set it into the renderer | |
1965 | wxDataViewTreeNode *node = NULL; | |
1966 | wxDataViewItem dataitem; | |
1967 | ||
1968 | if (!IsVirtualList()) | |
1969 | { | |
1970 | node = GetTreeNodeByRow(item); | |
1971 | if( node == NULL ) | |
1972 | continue; | |
1973 | ||
1974 | dataitem = node->GetItem(); | |
1975 | ||
1976 | // Skip all columns of "container" rows except the expander | |
1977 | // column itself unless HasContainerColumns() overrides this. | |
1978 | if ( col != expander && | |
1979 | model->IsContainer(dataitem) && | |
1980 | !model->HasContainerColumns(dataitem) ) | |
1981 | continue; | |
1982 | } | |
1983 | else | |
1984 | { | |
1985 | dataitem = wxDataViewItem( wxUIntToPtr(item+1) ); | |
1986 | } | |
1987 | ||
1988 | cell->PrepareForItem(model, dataitem, col->GetModelColumn()); | |
1989 | ||
1990 | // update cell_rect | |
1991 | cell_rect.y = GetLineStart( item ); | |
1992 | cell_rect.height = GetLineHeight( item ); | |
1993 | ||
1994 | // draw the background | |
1995 | bool selected = m_selection.Index( item ) != wxNOT_FOUND; | |
1996 | if ( !selected ) | |
1997 | DrawCellBackground( cell, dc, cell_rect ); | |
1998 | ||
1999 | // deal with the expander | |
2000 | int indent = 0; | |
2001 | if ((!IsList()) && (col == expander)) | |
2002 | { | |
2003 | // Calculate the indent first | |
2004 | indent = GetOwner()->GetIndent() * node->GetIndentLevel(); | |
2005 | ||
2006 | // we reserve m_lineHeight of horizontal space for the expander | |
2007 | // but leave EXPANDER_MARGIN around the expander itself | |
2008 | int exp_x = cell_rect.x + indent + EXPANDER_MARGIN; | |
2009 | ||
2010 | indent += m_lineHeight; | |
2011 | ||
2012 | // draw expander if needed and visible | |
2013 | if ( node->HasChildren() && exp_x < cell_rect.GetRight() ) | |
2014 | { | |
2015 | dc.SetPen( m_penExpander ); | |
2016 | dc.SetBrush( wxNullBrush ); | |
2017 | ||
2018 | int exp_size = m_lineHeight - 2*EXPANDER_MARGIN; | |
2019 | int exp_y = cell_rect.y + (cell_rect.height - exp_size)/2 | |
2020 | + EXPANDER_MARGIN - EXPANDER_OFFSET; | |
2021 | ||
2022 | const wxRect rect(exp_x, exp_y, exp_size, exp_size); | |
2023 | ||
2024 | int flag = 0; | |
2025 | if ( m_underMouse == node ) | |
2026 | flag |= wxCONTROL_CURRENT; | |
2027 | if ( node->IsOpen() ) | |
2028 | flag |= wxCONTROL_EXPANDED; | |
2029 | ||
2030 | // ensure that we don't overflow the cell (which might | |
2031 | // happen if the column is very narrow) | |
2032 | wxDCClipper clip(dc, cell_rect); | |
2033 | ||
2034 | wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag); | |
2035 | } | |
2036 | ||
2037 | // force the expander column to left-center align | |
2038 | cell->SetAlignment( wxALIGN_CENTER_VERTICAL ); | |
2039 | } | |
2040 | ||
2041 | wxRect item_rect = cell_rect; | |
2042 | item_rect.Deflate(PADDING_RIGHTLEFT, 0); | |
2043 | ||
2044 | // account for the tree indent (harmless if we're not indented) | |
2045 | item_rect.x += indent; | |
2046 | item_rect.width -= indent; | |
2047 | ||
2048 | if ( item_rect.width <= 0 ) | |
2049 | continue; | |
2050 | ||
2051 | int state = 0; | |
2052 | if (m_hasFocus && selected) | |
2053 | state |= wxDATAVIEW_CELL_SELECTED; | |
2054 | ||
2055 | // TODO: it would be much more efficient to create a clipping | |
2056 | // region for the entire column being rendered (in the OnPaint | |
2057 | // of wxDataViewMainWindow) instead of a single clip region for | |
2058 | // each cell. However it would mean that each renderer should | |
2059 | // respect the given wxRect's top & bottom coords, eventually | |
2060 | // violating only the left & right coords - however the user can | |
2061 | // make its own renderer and thus we cannot be sure of that. | |
2062 | wxDCClipper clip(dc, item_rect); | |
2063 | ||
2064 | cell->WXCallRender(item_rect, &dc, state); | |
2065 | } | |
2066 | ||
2067 | cell_rect.x += cell_rect.width; | |
2068 | } | |
2069 | } | |
2070 | ||
2071 | ||
2072 | void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer* cell, wxDC& dc, const wxRect& rect ) | |
2073 | { | |
2074 | wxRect rectBg( rect ); | |
2075 | ||
2076 | // don't overlap the horizontal rules | |
2077 | if ( m_owner->HasFlag(wxDV_HORIZ_RULES) ) | |
2078 | { | |
2079 | rectBg.x++; | |
2080 | rectBg.width--; | |
2081 | } | |
2082 | ||
2083 | // don't overlap the vertical rules | |
2084 | if ( m_owner->HasFlag(wxDV_VERT_RULES) ) | |
2085 | { | |
2086 | rectBg.y++; | |
2087 | rectBg.height--; | |
2088 | } | |
2089 | ||
2090 | cell->RenderBackground(&dc, rectBg); | |
2091 | } | |
2092 | ||
2093 | void wxDataViewMainWindow::OnRenameTimer() | |
2094 | { | |
2095 | // We have to call this here because changes may just have | |
2096 | // been made and no screen update taken place. | |
2097 | if ( m_dirty ) | |
2098 | { | |
2099 | // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead | |
2100 | // (needs to be tested!) | |
2101 | wxSafeYield(); | |
2102 | } | |
2103 | ||
2104 | wxDataViewItem item = GetItemByRow( m_currentRow ); | |
2105 | ||
2106 | StartEditing( item, m_currentCol ); | |
2107 | } | |
2108 | ||
2109 | void | |
2110 | wxDataViewMainWindow::StartEditing(const wxDataViewItem& item, | |
2111 | const wxDataViewColumn* col) | |
2112 | { | |
2113 | wxDataViewRenderer* renderer = col->GetRenderer(); | |
2114 | if ( !IsCellEditableInMode(item, col, wxDATAVIEW_CELL_EDITABLE) ) | |
2115 | return; | |
2116 | ||
2117 | const wxRect itemRect = GetItemRect(item, col); | |
2118 | if ( renderer->StartEditing(item, itemRect) ) | |
2119 | { | |
2120 | // Save the renderer to be able to finish/cancel editing it later and | |
2121 | // save the control to be able to detect if we're still editing it. | |
2122 | m_editorRenderer = renderer; | |
2123 | m_editorCtrl = renderer->GetEditorCtrl(); | |
2124 | } | |
2125 | } | |
2126 | ||
2127 | //----------------------------------------------------------------------------- | |
2128 | // Helper class for do operation on the tree node | |
2129 | //----------------------------------------------------------------------------- | |
2130 | class DoJob | |
2131 | { | |
2132 | public: | |
2133 | DoJob() { } | |
2134 | virtual ~DoJob() { } | |
2135 | ||
2136 | // The return value control how the tree-walker tranverse the tree | |
2137 | enum | |
2138 | { | |
2139 | DONE, // Job done, stop traversing and return | |
2140 | SKIP_SUBTREE, // Ignore the current node's subtree and continue | |
2141 | CONTINUE // Job not done, continue | |
2142 | }; | |
2143 | ||
2144 | virtual int operator() ( wxDataViewTreeNode * node ) = 0; | |
2145 | }; | |
2146 | ||
2147 | bool Walker( wxDataViewTreeNode * node, DoJob & func ) | |
2148 | { | |
2149 | wxCHECK_MSG( node, false, "can't walk NULL node" ); | |
2150 | ||
2151 | switch( func( node ) ) | |
2152 | { | |
2153 | case DoJob::DONE: | |
2154 | return true; | |
2155 | case DoJob::SKIP_SUBTREE: | |
2156 | return false; | |
2157 | case DoJob::CONTINUE: | |
2158 | break; | |
2159 | } | |
2160 | ||
2161 | if ( node->HasChildren() ) | |
2162 | { | |
2163 | const wxDataViewTreeNodes& nodes = node->GetChildNodes(); | |
2164 | ||
2165 | for ( wxDataViewTreeNodes::const_iterator i = nodes.begin(); | |
2166 | i != nodes.end(); | |
2167 | ++i ) | |
2168 | { | |
2169 | if ( Walker(*i, func) ) | |
2170 | return true; | |
2171 | } | |
2172 | } | |
2173 | ||
2174 | return false; | |
2175 | } | |
2176 | ||
2177 | bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item) | |
2178 | { | |
2179 | if (IsVirtualList()) | |
2180 | { | |
2181 | wxDataViewVirtualListModel *list_model = | |
2182 | (wxDataViewVirtualListModel*) GetModel(); | |
2183 | m_count = list_model->GetCount(); | |
2184 | } | |
2185 | else | |
2186 | { | |
2187 | SortPrepare(); | |
2188 | ||
2189 | wxDataViewTreeNode *parentNode = FindNode(parent); | |
2190 | ||
2191 | if ( !parentNode ) | |
2192 | return false; | |
2193 | ||
2194 | wxDataViewItemArray modelSiblings; | |
2195 | GetModel()->GetChildren(parent, modelSiblings); | |
2196 | const int modelSiblingsSize = modelSiblings.size(); | |
2197 | ||
2198 | int posInModel = modelSiblings.Index(item, /*fromEnd=*/true); | |
2199 | wxCHECK_MSG( posInModel != wxNOT_FOUND, false, "adding non-existent item?" ); | |
2200 | ||
2201 | wxDataViewTreeNode *itemNode = new wxDataViewTreeNode(parentNode, item); | |
2202 | itemNode->SetHasChildren(GetModel()->IsContainer(item)); | |
2203 | ||
2204 | parentNode->SetHasChildren(true); | |
2205 | ||
2206 | const wxDataViewTreeNodes& nodeSiblings = parentNode->GetChildNodes(); | |
2207 | const int nodeSiblingsSize = nodeSiblings.size(); | |
2208 | ||
2209 | int nodePos = 0; | |
2210 | ||
2211 | if ( posInModel == modelSiblingsSize - 1 ) | |
2212 | { | |
2213 | nodePos = nodeSiblingsSize; | |
2214 | } | |
2215 | else if ( modelSiblingsSize == nodeSiblingsSize + 1 ) | |
2216 | { | |
2217 | // This is the simple case when our node tree already matches the | |
2218 | // model and only this one item is missing. | |
2219 | nodePos = posInModel; | |
2220 | } | |
2221 | else | |
2222 | { | |
2223 | // It's possible that a larger discrepancy between the model and | |
2224 | // our realization exists. This can happen e.g. when adding a bunch | |
2225 | // of items to the model and then calling ItemsAdded() just once | |
2226 | // afterwards. In this case, we must find the right position by | |
2227 | // looking at sibling items. | |
2228 | ||
2229 | // append to the end if we won't find a better position: | |
2230 | nodePos = nodeSiblingsSize; | |
2231 | ||
2232 | for ( int nextItemPos = posInModel + 1; | |
2233 | nextItemPos < modelSiblingsSize; | |
2234 | nextItemPos++ ) | |
2235 | { | |
2236 | int nextNodePos = parentNode->FindChildByItem(modelSiblings[nextItemPos]); | |
2237 | if ( nextNodePos != wxNOT_FOUND ) | |
2238 | { | |
2239 | nodePos = nextNodePos; | |
2240 | break; | |
2241 | } | |
2242 | } | |
2243 | } | |
2244 | ||
2245 | parentNode->ChangeSubTreeCount(+1); | |
2246 | parentNode->InsertChild(itemNode, nodePos); | |
2247 | ||
2248 | m_count = -1; | |
2249 | } | |
2250 | ||
2251 | GetOwner()->InvalidateColBestWidths(); | |
2252 | UpdateDisplay(); | |
2253 | ||
2254 | return true; | |
2255 | } | |
2256 | ||
2257 | bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent, | |
2258 | const wxDataViewItem& item) | |
2259 | { | |
2260 | if (IsVirtualList()) | |
2261 | { | |
2262 | wxDataViewVirtualListModel *list_model = | |
2263 | (wxDataViewVirtualListModel*) GetModel(); | |
2264 | m_count = list_model->GetCount(); | |
2265 | ||
2266 | if ( !m_selection.empty() ) | |
2267 | { | |
2268 | const int row = GetRowByItem(item); | |
2269 | ||
2270 | int rowIndexInSelection = wxNOT_FOUND; | |
2271 | ||
2272 | const size_t selCount = m_selection.size(); | |
2273 | for ( size_t i = 0; i < selCount; i++ ) | |
2274 | { | |
2275 | if ( m_selection[i] == (unsigned)row ) | |
2276 | rowIndexInSelection = i; | |
2277 | else if ( m_selection[i] > (unsigned)row ) | |
2278 | m_selection[i]--; | |
2279 | } | |
2280 | ||
2281 | if ( rowIndexInSelection != wxNOT_FOUND ) | |
2282 | m_selection.RemoveAt(rowIndexInSelection); | |
2283 | } | |
2284 | ||
2285 | } | |
2286 | else // general case | |
2287 | { | |
2288 | wxDataViewTreeNode *parentNode = FindNode(parent); | |
2289 | ||
2290 | // Notice that it is possible that the item being deleted is not in the | |
2291 | // tree at all, for example we could be deleting a never shown (because | |
2292 | // collapsed) item in a tree model. So it's not an error if we don't know | |
2293 | // about this item, just return without doing anything then. | |
2294 | if ( !parentNode ) | |
2295 | return true; | |
2296 | ||
2297 | wxCHECK_MSG( parentNode->HasChildren(), false, "parent node doesn't have children?" ); | |
2298 | const wxDataViewTreeNodes& parentsChildren = parentNode->GetChildNodes(); | |
2299 | ||
2300 | // We can't use FindNode() to find 'item', because it was already | |
2301 | // removed from the model by the time ItemDeleted() is called, so we | |
2302 | // have to do it manually. We keep track of its position as well for | |
2303 | // later use. | |
2304 | int itemPosInNode = 0; | |
2305 | wxDataViewTreeNode *itemNode = NULL; | |
2306 | for ( wxDataViewTreeNodes::const_iterator i = parentsChildren.begin(); | |
2307 | i != parentsChildren.end(); | |
2308 | ++i, ++itemPosInNode ) | |
2309 | { | |
2310 | if( (*i)->GetItem() == item ) | |
2311 | { | |
2312 | itemNode = *i; | |
2313 | break; | |
2314 | } | |
2315 | } | |
2316 | ||
2317 | // If the parent wasn't expanded, it's possible that we didn't have a | |
2318 | // node corresponding to 'item' and so there's nothing left to do. | |
2319 | if ( !itemNode ) | |
2320 | { | |
2321 | // If this was the last child to be removed, it's possible the parent | |
2322 | // node became a leaf. Let's ask the model about it. | |
2323 | if ( parentNode->GetChildNodes().empty() ) | |
2324 | parentNode->SetHasChildren(GetModel()->IsContainer(parent)); | |
2325 | ||
2326 | return true; | |
2327 | } | |
2328 | ||
2329 | // Delete the item from wxDataViewTreeNode representation: | |
2330 | const int itemsDeleted = 1 + itemNode->GetSubTreeCount(); | |
2331 | ||
2332 | parentNode->RemoveChild(itemNode); | |
2333 | delete itemNode; | |
2334 | parentNode->ChangeSubTreeCount(-itemsDeleted); | |
2335 | ||
2336 | // Make the row number invalid and get a new valid one when user call GetRowCount | |
2337 | m_count = -1; | |
2338 | ||
2339 | // If this was the last child to be removed, it's possible the parent | |
2340 | // node became a leaf. Let's ask the model about it. | |
2341 | if ( parentNode->GetChildNodes().empty() ) | |
2342 | { | |
2343 | bool isContainer = GetModel()->IsContainer(parent); | |
2344 | parentNode->SetHasChildren(isContainer); | |
2345 | if ( isContainer ) | |
2346 | { | |
2347 | // If it's still a container, make sure we show "+" icon for it | |
2348 | // and not "-" one as there is nothing to collapse any more. | |
2349 | if ( parentNode->IsOpen() ) | |
2350 | parentNode->ToggleOpen(); | |
2351 | } | |
2352 | } | |
2353 | ||
2354 | // Update selection by removing 'item' and its entire children tree from the selection. | |
2355 | if ( !m_selection.empty() ) | |
2356 | { | |
2357 | // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from | |
2358 | // the parent ('parentNode') and position in its list of children | |
2359 | int itemRow; | |
2360 | if ( itemPosInNode == 0 ) | |
2361 | { | |
2362 | // 1st child, row number is that of the parent parentNode + 1 | |
2363 | itemRow = GetRowByItem(parentNode->GetItem()) + 1; | |
2364 | } | |
2365 | else | |
2366 | { | |
2367 | // row number is that of the sibling above 'item' + its subtree if any + 1 | |
2368 | const wxDataViewTreeNode *siblingNode = parentNode->GetChildNodes()[itemPosInNode - 1]; | |
2369 | ||
2370 | itemRow = GetRowByItem(siblingNode->GetItem()) + | |
2371 | siblingNode->GetSubTreeCount() + | |
2372 | 1; | |
2373 | } | |
2374 | ||
2375 | wxDataViewSelection newsel(wxDataViewSelectionCmp); | |
2376 | ||
2377 | const size_t numSelections = m_selection.size(); | |
2378 | for ( size_t i = 0; i < numSelections; ++i ) | |
2379 | { | |
2380 | const int s = m_selection[i]; | |
2381 | if ( s < itemRow ) | |
2382 | newsel.push_back(s); | |
2383 | else if ( s >= itemRow + itemsDeleted ) | |
2384 | newsel.push_back(s - itemsDeleted); | |
2385 | // else: deleted item, remove from selection | |
2386 | } | |
2387 | ||
2388 | m_selection = newsel; | |
2389 | } | |
2390 | } | |
2391 | ||
2392 | // Change the current row to the last row if the current exceed the max row number | |
2393 | if( m_currentRow > GetRowCount() ) | |
2394 | ChangeCurrentRow(m_count - 1); | |
2395 | ||
2396 | GetOwner()->InvalidateColBestWidths(); | |
2397 | UpdateDisplay(); | |
2398 | ||
2399 | return true; | |
2400 | } | |
2401 | ||
2402 | bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item) | |
2403 | { | |
2404 | SortPrepare(); | |
2405 | g_model->Resort(); | |
2406 | ||
2407 | GetOwner()->InvalidateColBestWidths(); | |
2408 | ||
2409 | // Send event | |
2410 | wxWindow *parent = GetParent(); | |
2411 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId()); | |
2412 | le.SetEventObject(parent); | |
2413 | le.SetModel(GetModel()); | |
2414 | le.SetItem(item); | |
2415 | parent->ProcessWindowEvent(le); | |
2416 | ||
2417 | return true; | |
2418 | } | |
2419 | ||
2420 | bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int model_column ) | |
2421 | { | |
2422 | int view_column = -1; | |
2423 | unsigned int n_col = m_owner->GetColumnCount(); | |
2424 | for (unsigned i = 0; i < n_col; i++) | |
2425 | { | |
2426 | wxDataViewColumn *column = m_owner->GetColumn( i ); | |
2427 | if (column->GetModelColumn() == model_column) | |
2428 | { | |
2429 | view_column = (int) i; | |
2430 | break; | |
2431 | } | |
2432 | } | |
2433 | if (view_column == -1) | |
2434 | return false; | |
2435 | ||
2436 | // NOTE: to be valid, we cannot use e.g. INT_MAX - 1 | |
2437 | /*#define MAX_VIRTUAL_WIDTH 100000 | |
2438 | ||
2439 | wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight ); | |
2440 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
2441 | Refresh( true, &rect ); | |
2442 | ||
2443 | return true; | |
2444 | */ | |
2445 | SortPrepare(); | |
2446 | g_model->Resort(); | |
2447 | ||
2448 | GetOwner()->InvalidateColBestWidth(view_column); | |
2449 | ||
2450 | // Send event | |
2451 | wxWindow *parent = GetParent(); | |
2452 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId()); | |
2453 | le.SetEventObject(parent); | |
2454 | le.SetModel(GetModel()); | |
2455 | le.SetItem(item); | |
2456 | le.SetColumn(view_column); | |
2457 | le.SetDataViewColumn(GetOwner()->GetColumn(view_column)); | |
2458 | parent->ProcessWindowEvent(le); | |
2459 | ||
2460 | return true; | |
2461 | } | |
2462 | ||
2463 | bool wxDataViewMainWindow::Cleared() | |
2464 | { | |
2465 | DestroyTree(); | |
2466 | m_selection.Clear(); | |
2467 | ||
2468 | SortPrepare(); | |
2469 | BuildTree( GetModel() ); | |
2470 | ||
2471 | GetOwner()->InvalidateColBestWidths(); | |
2472 | UpdateDisplay(); | |
2473 | ||
2474 | return true; | |
2475 | } | |
2476 | ||
2477 | void wxDataViewMainWindow::UpdateDisplay() | |
2478 | { | |
2479 | m_dirty = true; | |
2480 | m_underMouse = NULL; | |
2481 | } | |
2482 | ||
2483 | void wxDataViewMainWindow::OnInternalIdle() | |
2484 | { | |
2485 | wxWindow::OnInternalIdle(); | |
2486 | ||
2487 | if (m_dirty) | |
2488 | { | |
2489 | RecalculateDisplay(); | |
2490 | m_dirty = false; | |
2491 | } | |
2492 | } | |
2493 | ||
2494 | void wxDataViewMainWindow::RecalculateDisplay() | |
2495 | { | |
2496 | wxDataViewModel *model = GetModel(); | |
2497 | if (!model) | |
2498 | { | |
2499 | Refresh(); | |
2500 | return; | |
2501 | } | |
2502 | ||
2503 | int width = GetEndOfLastCol(); | |
2504 | int height = GetLineStart( GetRowCount() ); | |
2505 | ||
2506 | SetVirtualSize( width, height ); | |
2507 | GetOwner()->SetScrollRate( 10, m_lineHeight ); | |
2508 | ||
2509 | Refresh(); | |
2510 | } | |
2511 | ||
2512 | void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
2513 | { | |
2514 | m_underMouse = NULL; | |
2515 | ||
2516 | wxWindow::ScrollWindow( dx, dy, rect ); | |
2517 | ||
2518 | if (GetOwner()->m_headerArea) | |
2519 | GetOwner()->m_headerArea->ScrollWindow( dx, 0 ); | |
2520 | } | |
2521 | ||
2522 | void wxDataViewMainWindow::ScrollTo( int rows, int column ) | |
2523 | { | |
2524 | m_underMouse = NULL; | |
2525 | ||
2526 | int x, y; | |
2527 | m_owner->GetScrollPixelsPerUnit( &x, &y ); | |
2528 | int sy = GetLineStart( rows )/y; | |
2529 | int sx = -1; | |
2530 | if( column != -1 ) | |
2531 | { | |
2532 | wxRect rect = GetClientRect(); | |
2533 | int colnum = 0; | |
2534 | int x_start, w = 0; | |
2535 | int xx, yy, xe; | |
2536 | m_owner->CalcUnscrolledPosition( rect.x, rect.y, &xx, &yy ); | |
2537 | for (x_start = 0; colnum < column; colnum++) | |
2538 | { | |
2539 | wxDataViewColumn *col = GetOwner()->GetColumnAt(colnum); | |
2540 | if (col->IsHidden()) | |
2541 | continue; // skip it! | |
2542 | ||
2543 | w = col->GetWidth(); | |
2544 | x_start += w; | |
2545 | } | |
2546 | ||
2547 | int x_end = x_start + w; | |
2548 | xe = xx + rect.width; | |
2549 | if( x_end > xe ) | |
2550 | { | |
2551 | sx = ( xx + x_end - xe )/x; | |
2552 | } | |
2553 | if( x_start < xx ) | |
2554 | { | |
2555 | sx = x_start/x; | |
2556 | } | |
2557 | } | |
2558 | m_owner->Scroll( sx, sy ); | |
2559 | } | |
2560 | ||
2561 | int wxDataViewMainWindow::GetCountPerPage() const | |
2562 | { | |
2563 | wxSize size = GetClientSize(); | |
2564 | return size.y / m_lineHeight; | |
2565 | } | |
2566 | ||
2567 | int wxDataViewMainWindow::GetEndOfLastCol() const | |
2568 | { | |
2569 | int width = 0; | |
2570 | unsigned int i; | |
2571 | for (i = 0; i < GetOwner()->GetColumnCount(); i++) | |
2572 | { | |
2573 | const wxDataViewColumn *c = | |
2574 | const_cast<wxDataViewCtrl*>(GetOwner())->GetColumnAt( i ); | |
2575 | ||
2576 | if (!c->IsHidden()) | |
2577 | width += c->GetWidth(); | |
2578 | } | |
2579 | return width; | |
2580 | } | |
2581 | ||
2582 | unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const | |
2583 | { | |
2584 | int x = 0; | |
2585 | int y = 0; | |
2586 | m_owner->CalcUnscrolledPosition( x, y, &x, &y ); | |
2587 | ||
2588 | return GetLineAt( y ); | |
2589 | } | |
2590 | ||
2591 | unsigned int wxDataViewMainWindow::GetLastVisibleRow() | |
2592 | { | |
2593 | wxSize client_size = GetClientSize(); | |
2594 | m_owner->CalcUnscrolledPosition( client_size.x, client_size.y, | |
2595 | &client_size.x, &client_size.y ); | |
2596 | ||
2597 | // we should deal with the pixel here | |
2598 | unsigned int row = GetLineAt(client_size.y) - 1; | |
2599 | ||
2600 | return wxMin( GetRowCount()-1, row ); | |
2601 | } | |
2602 | ||
2603 | unsigned int wxDataViewMainWindow::GetRowCount() const | |
2604 | { | |
2605 | if ( m_count == -1 ) | |
2606 | { | |
2607 | wxDataViewMainWindow* const | |
2608 | self = const_cast<wxDataViewMainWindow*>(this); | |
2609 | self->m_count = RecalculateCount(); | |
2610 | self->UpdateDisplay(); | |
2611 | } | |
2612 | return m_count; | |
2613 | } | |
2614 | ||
2615 | void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row ) | |
2616 | { | |
2617 | m_currentRow = row; | |
2618 | ||
2619 | // send event | |
2620 | } | |
2621 | ||
2622 | void wxDataViewMainWindow::SelectAllRows( bool on ) | |
2623 | { | |
2624 | if (IsEmpty()) | |
2625 | return; | |
2626 | ||
2627 | if (on) | |
2628 | { | |
2629 | m_selection.Clear(); | |
2630 | for (unsigned int i = 0; i < GetRowCount(); i++) | |
2631 | m_selection.Add( i ); | |
2632 | Refresh(); | |
2633 | } | |
2634 | else | |
2635 | { | |
2636 | unsigned int first_visible = GetFirstVisibleRow(); | |
2637 | unsigned int last_visible = GetLastVisibleRow(); | |
2638 | unsigned int i; | |
2639 | for (i = 0; i < m_selection.GetCount(); i++) | |
2640 | { | |
2641 | unsigned int row = m_selection[i]; | |
2642 | if ((row >= first_visible) && (row <= last_visible)) | |
2643 | RefreshRow( row ); | |
2644 | } | |
2645 | m_selection.Clear(); | |
2646 | } | |
2647 | } | |
2648 | ||
2649 | void wxDataViewMainWindow::SelectRow( unsigned int row, bool on ) | |
2650 | { | |
2651 | if (m_selection.Index( row ) == wxNOT_FOUND) | |
2652 | { | |
2653 | if (on) | |
2654 | { | |
2655 | m_selection.Add( row ); | |
2656 | RefreshRow( row ); | |
2657 | } | |
2658 | } | |
2659 | else | |
2660 | { | |
2661 | if (!on) | |
2662 | { | |
2663 | m_selection.Remove( row ); | |
2664 | RefreshRow( row ); | |
2665 | } | |
2666 | } | |
2667 | } | |
2668 | ||
2669 | void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on ) | |
2670 | { | |
2671 | if (from > to) | |
2672 | { | |
2673 | unsigned int tmp = from; | |
2674 | from = to; | |
2675 | to = tmp; | |
2676 | } | |
2677 | ||
2678 | unsigned int i; | |
2679 | for (i = from; i <= to; i++) | |
2680 | { | |
2681 | if (m_selection.Index( i ) == wxNOT_FOUND) | |
2682 | { | |
2683 | if (on) | |
2684 | m_selection.Add( i ); | |
2685 | } | |
2686 | else | |
2687 | { | |
2688 | if (!on) | |
2689 | m_selection.Remove( i ); | |
2690 | } | |
2691 | } | |
2692 | RefreshRows( from, to ); | |
2693 | } | |
2694 | ||
2695 | void wxDataViewMainWindow::Select( const wxArrayInt& aSelections ) | |
2696 | { | |
2697 | for (size_t i=0; i < aSelections.GetCount(); i++) | |
2698 | { | |
2699 | int n = aSelections[i]; | |
2700 | ||
2701 | m_selection.Add( n ); | |
2702 | RefreshRow( n ); | |
2703 | } | |
2704 | } | |
2705 | ||
2706 | void wxDataViewMainWindow::ReverseRowSelection( unsigned int row ) | |
2707 | { | |
2708 | if (m_selection.Index( row ) == wxNOT_FOUND) | |
2709 | m_selection.Add( row ); | |
2710 | else | |
2711 | m_selection.Remove( row ); | |
2712 | RefreshRow( row ); | |
2713 | } | |
2714 | ||
2715 | bool wxDataViewMainWindow::IsRowSelected( unsigned int row ) | |
2716 | { | |
2717 | return (m_selection.Index( row ) != wxNOT_FOUND); | |
2718 | } | |
2719 | ||
2720 | void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem& item) | |
2721 | { | |
2722 | wxWindow *parent = GetParent(); | |
2723 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, parent->GetId()); | |
2724 | ||
2725 | le.SetEventObject(parent); | |
2726 | le.SetModel(GetModel()); | |
2727 | le.SetItem( item ); | |
2728 | ||
2729 | parent->ProcessWindowEvent(le); | |
2730 | } | |
2731 | ||
2732 | void wxDataViewMainWindow::RefreshRow( unsigned int row ) | |
2733 | { | |
2734 | wxRect rect( 0, GetLineStart( row ), GetEndOfLastCol(), GetLineHeight( row ) ); | |
2735 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
2736 | ||
2737 | wxSize client_size = GetClientSize(); | |
2738 | wxRect client_rect( 0, 0, client_size.x, client_size.y ); | |
2739 | wxRect intersect_rect = client_rect.Intersect( rect ); | |
2740 | if (intersect_rect.width > 0) | |
2741 | Refresh( true, &intersect_rect ); | |
2742 | } | |
2743 | ||
2744 | void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to ) | |
2745 | { | |
2746 | if (from > to) | |
2747 | { | |
2748 | unsigned int tmp = to; | |
2749 | to = from; | |
2750 | from = tmp; | |
2751 | } | |
2752 | ||
2753 | wxRect rect( 0, GetLineStart( from ), GetEndOfLastCol(), GetLineStart( (to-from+1) ) ); | |
2754 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
2755 | ||
2756 | wxSize client_size = GetClientSize(); | |
2757 | wxRect client_rect( 0, 0, client_size.x, client_size.y ); | |
2758 | wxRect intersect_rect = client_rect.Intersect( rect ); | |
2759 | if (intersect_rect.width > 0) | |
2760 | Refresh( true, &intersect_rect ); | |
2761 | } | |
2762 | ||
2763 | void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow ) | |
2764 | { | |
2765 | wxSize client_size = GetClientSize(); | |
2766 | int start = GetLineStart( firstRow ); | |
2767 | m_owner->CalcScrolledPosition( start, 0, &start, NULL ); | |
2768 | if (start > client_size.y) return; | |
2769 | ||
2770 | wxRect rect( 0, start, client_size.x, client_size.y - start ); | |
2771 | ||
2772 | Refresh( true, &rect ); | |
2773 | } | |
2774 | ||
2775 | wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const | |
2776 | { | |
2777 | wxRect rect; | |
2778 | rect.x = 0; | |
2779 | rect.y = GetLineStart( row ); | |
2780 | rect.width = GetEndOfLastCol(); | |
2781 | rect.height = GetLineHeight( row ); | |
2782 | ||
2783 | return rect; | |
2784 | } | |
2785 | ||
2786 | int wxDataViewMainWindow::GetLineStart( unsigned int row ) const | |
2787 | { | |
2788 | const wxDataViewModel *model = GetModel(); | |
2789 | ||
2790 | if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) | |
2791 | { | |
2792 | // TODO make more efficient | |
2793 | ||
2794 | int start = 0; | |
2795 | ||
2796 | unsigned int r; | |
2797 | for (r = 0; r < row; r++) | |
2798 | { | |
2799 | const wxDataViewTreeNode* node = GetTreeNodeByRow(r); | |
2800 | if (!node) return start; | |
2801 | ||
2802 | wxDataViewItem item = node->GetItem(); | |
2803 | ||
2804 | unsigned int cols = GetOwner()->GetColumnCount(); | |
2805 | unsigned int col; | |
2806 | int height = m_lineHeight; | |
2807 | for (col = 0; col < cols; col++) | |
2808 | { | |
2809 | const wxDataViewColumn *column = GetOwner()->GetColumn(col); | |
2810 | if (column->IsHidden()) | |
2811 | continue; // skip it! | |
2812 | ||
2813 | if ((col != 0) && | |
2814 | model->IsContainer(item) && | |
2815 | !model->HasContainerColumns(item)) | |
2816 | continue; // skip it! | |
2817 | ||
2818 | wxDataViewRenderer *renderer = | |
2819 | const_cast<wxDataViewRenderer*>(column->GetRenderer()); | |
2820 | renderer->PrepareForItem(model, item, column->GetModelColumn()); | |
2821 | ||
2822 | height = wxMax( height, renderer->GetSize().y ); | |
2823 | } | |
2824 | ||
2825 | start += height; | |
2826 | } | |
2827 | ||
2828 | return start; | |
2829 | } | |
2830 | else | |
2831 | { | |
2832 | return row * m_lineHeight; | |
2833 | } | |
2834 | } | |
2835 | ||
2836 | int wxDataViewMainWindow::GetLineAt( unsigned int y ) const | |
2837 | { | |
2838 | const wxDataViewModel *model = GetModel(); | |
2839 | ||
2840 | // check for the easy case first | |
2841 | if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT) ) | |
2842 | return y / m_lineHeight; | |
2843 | ||
2844 | // TODO make more efficient | |
2845 | unsigned int row = 0; | |
2846 | unsigned int yy = 0; | |
2847 | for (;;) | |
2848 | { | |
2849 | const wxDataViewTreeNode* node = GetTreeNodeByRow(row); | |
2850 | if (!node) | |
2851 | { | |
2852 | // not really correct... | |
2853 | return row + ((y-yy) / m_lineHeight); | |
2854 | } | |
2855 | ||
2856 | wxDataViewItem item = node->GetItem(); | |
2857 | ||
2858 | unsigned int cols = GetOwner()->GetColumnCount(); | |
2859 | unsigned int col; | |
2860 | int height = m_lineHeight; | |
2861 | for (col = 0; col < cols; col++) | |
2862 | { | |
2863 | const wxDataViewColumn *column = GetOwner()->GetColumn(col); | |
2864 | if (column->IsHidden()) | |
2865 | continue; // skip it! | |
2866 | ||
2867 | if ((col != 0) && | |
2868 | model->IsContainer(item) && | |
2869 | !model->HasContainerColumns(item)) | |
2870 | continue; // skip it! | |
2871 | ||
2872 | wxDataViewRenderer *renderer = | |
2873 | const_cast<wxDataViewRenderer*>(column->GetRenderer()); | |
2874 | renderer->PrepareForItem(model, item, column->GetModelColumn()); | |
2875 | ||
2876 | height = wxMax( height, renderer->GetSize().y ); | |
2877 | } | |
2878 | ||
2879 | yy += height; | |
2880 | if (y < yy) | |
2881 | return row; | |
2882 | ||
2883 | row++; | |
2884 | } | |
2885 | } | |
2886 | ||
2887 | int wxDataViewMainWindow::GetLineHeight( unsigned int row ) const | |
2888 | { | |
2889 | const wxDataViewModel *model = GetModel(); | |
2890 | ||
2891 | if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) | |
2892 | { | |
2893 | wxASSERT( !IsVirtualList() ); | |
2894 | ||
2895 | const wxDataViewTreeNode* node = GetTreeNodeByRow(row); | |
2896 | // wxASSERT( node ); | |
2897 | if (!node) return m_lineHeight; | |
2898 | ||
2899 | wxDataViewItem item = node->GetItem(); | |
2900 | ||
2901 | int height = m_lineHeight; | |
2902 | ||
2903 | unsigned int cols = GetOwner()->GetColumnCount(); | |
2904 | unsigned int col; | |
2905 | for (col = 0; col < cols; col++) | |
2906 | { | |
2907 | const wxDataViewColumn *column = GetOwner()->GetColumn(col); | |
2908 | if (column->IsHidden()) | |
2909 | continue; // skip it! | |
2910 | ||
2911 | if ((col != 0) && | |
2912 | model->IsContainer(item) && | |
2913 | !model->HasContainerColumns(item)) | |
2914 | continue; // skip it! | |
2915 | ||
2916 | wxDataViewRenderer *renderer = | |
2917 | const_cast<wxDataViewRenderer*>(column->GetRenderer()); | |
2918 | renderer->PrepareForItem(model, item, column->GetModelColumn()); | |
2919 | ||
2920 | height = wxMax( height, renderer->GetSize().y ); | |
2921 | } | |
2922 | ||
2923 | return height; | |
2924 | } | |
2925 | else | |
2926 | { | |
2927 | return m_lineHeight; | |
2928 | } | |
2929 | } | |
2930 | ||
2931 | ||
2932 | class RowToTreeNodeJob: public DoJob | |
2933 | { | |
2934 | public: | |
2935 | RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node ) | |
2936 | { | |
2937 | this->row = row; | |
2938 | this->current = current; | |
2939 | ret = NULL; | |
2940 | parent = node; | |
2941 | } | |
2942 | ||
2943 | virtual int operator() ( wxDataViewTreeNode * node ) | |
2944 | { | |
2945 | current ++; | |
2946 | if( current == static_cast<int>(row)) | |
2947 | { | |
2948 | ret = node; | |
2949 | return DoJob::DONE; | |
2950 | } | |
2951 | ||
2952 | if( node->GetSubTreeCount() + current < static_cast<int>(row) ) | |
2953 | { | |
2954 | current += node->GetSubTreeCount(); | |
2955 | return DoJob::SKIP_SUBTREE; | |
2956 | } | |
2957 | else | |
2958 | { | |
2959 | parent = node; | |
2960 | ||
2961 | // If the current node has only leaf children, we can find the | |
2962 | // desired node directly. This can speed up finding the node | |
2963 | // in some cases, and will have a very good effect for list views. | |
2964 | if ( node->HasChildren() && | |
2965 | (int)node->GetChildNodes().size() == node->GetSubTreeCount() ) | |
2966 | { | |
2967 | const int index = static_cast<int>(row) - current - 1; | |
2968 | ret = node->GetChildNodes()[index]; | |
2969 | return DoJob::DONE; | |
2970 | } | |
2971 | ||
2972 | return DoJob::CONTINUE; | |
2973 | } | |
2974 | } | |
2975 | ||
2976 | wxDataViewTreeNode * GetResult() const | |
2977 | { return ret; } | |
2978 | ||
2979 | private: | |
2980 | unsigned int row; | |
2981 | int current; | |
2982 | wxDataViewTreeNode * ret; | |
2983 | wxDataViewTreeNode * parent; | |
2984 | }; | |
2985 | ||
2986 | wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) const | |
2987 | { | |
2988 | wxASSERT( !IsVirtualList() ); | |
2989 | ||
2990 | RowToTreeNodeJob job( row , -2, m_root ); | |
2991 | Walker( m_root , job ); | |
2992 | return job.GetResult(); | |
2993 | } | |
2994 | ||
2995 | wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const | |
2996 | { | |
2997 | wxDataViewItem item; | |
2998 | if (IsVirtualList()) | |
2999 | { | |
3000 | if ( row < GetRowCount() ) | |
3001 | item = wxDataViewItem(wxUIntToPtr(row+1)); | |
3002 | } | |
3003 | else | |
3004 | { | |
3005 | wxDataViewTreeNode *node = GetTreeNodeByRow(row); | |
3006 | if ( node ) | |
3007 | item = node->GetItem(); | |
3008 | } | |
3009 | ||
3010 | return item; | |
3011 | } | |
3012 | ||
3013 | bool | |
3014 | wxDataViewMainWindow::SendExpanderEvent(wxEventType type, | |
3015 | const wxDataViewItem& item) | |
3016 | { | |
3017 | wxWindow *parent = GetParent(); | |
3018 | wxDataViewEvent le(type, parent->GetId()); | |
3019 | ||
3020 | le.SetEventObject(parent); | |
3021 | le.SetModel(GetModel()); | |
3022 | le.SetItem( item ); | |
3023 | ||
3024 | return !parent->ProcessWindowEvent(le) || le.IsAllowed(); | |
3025 | } | |
3026 | ||
3027 | bool wxDataViewMainWindow::IsExpanded( unsigned int row ) const | |
3028 | { | |
3029 | if (IsList()) | |
3030 | return false; | |
3031 | ||
3032 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); | |
3033 | if (!node) | |
3034 | return false; | |
3035 | ||
3036 | if (!node->HasChildren()) | |
3037 | return false; | |
3038 | ||
3039 | return node->IsOpen(); | |
3040 | } | |
3041 | ||
3042 | bool wxDataViewMainWindow::HasChildren( unsigned int row ) const | |
3043 | { | |
3044 | if (IsList()) | |
3045 | return false; | |
3046 | ||
3047 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); | |
3048 | if (!node) | |
3049 | return false; | |
3050 | ||
3051 | if (!node->HasChildren()) | |
3052 | return false; | |
3053 | ||
3054 | return true; | |
3055 | } | |
3056 | ||
3057 | void wxDataViewMainWindow::Expand( unsigned int row ) | |
3058 | { | |
3059 | if (IsList()) | |
3060 | return; | |
3061 | ||
3062 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); | |
3063 | if (!node) | |
3064 | return; | |
3065 | ||
3066 | if (!node->HasChildren()) | |
3067 | return; | |
3068 | ||
3069 | if (!node->IsOpen()) | |
3070 | { | |
3071 | if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, node->GetItem()) ) | |
3072 | { | |
3073 | // Vetoed by the event handler. | |
3074 | return; | |
3075 | } | |
3076 | ||
3077 | node->ToggleOpen(); | |
3078 | ||
3079 | // build the children of current node | |
3080 | if( node->GetChildNodes().empty() ) | |
3081 | { | |
3082 | SortPrepare(); | |
3083 | ::BuildTreeHelper(GetModel(), node->GetItem(), node); | |
3084 | } | |
3085 | ||
3086 | // By expanding the node all row indices that are currently in the selection list | |
3087 | // and are greater than our node have become invalid. So we have to correct that now. | |
3088 | const unsigned rowAdjustment = node->GetSubTreeCount(); | |
3089 | for(unsigned i=0; i<m_selection.size(); ++i) | |
3090 | { | |
3091 | const unsigned testRow = m_selection[i]; | |
3092 | // all rows above us are not affected, so skip them | |
3093 | if(testRow <= row) | |
3094 | continue; | |
3095 | ||
3096 | m_selection[i] += rowAdjustment; | |
3097 | } | |
3098 | ||
3099 | if(m_currentRow > row) | |
3100 | ChangeCurrentRow(m_currentRow + rowAdjustment); | |
3101 | ||
3102 | m_count = -1; | |
3103 | UpdateDisplay(); | |
3104 | // Send the expanded event | |
3105 | SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,node->GetItem()); | |
3106 | } | |
3107 | } | |
3108 | ||
3109 | void wxDataViewMainWindow::Collapse(unsigned int row) | |
3110 | { | |
3111 | if (IsList()) | |
3112 | return; | |
3113 | ||
3114 | wxDataViewTreeNode *node = GetTreeNodeByRow(row); | |
3115 | if (!node) | |
3116 | return; | |
3117 | ||
3118 | if (!node->HasChildren()) | |
3119 | return; | |
3120 | ||
3121 | if (node->IsOpen()) | |
3122 | { | |
3123 | if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,node->GetItem()) ) | |
3124 | { | |
3125 | // Vetoed by the event handler. | |
3126 | return; | |
3127 | } | |
3128 | ||
3129 | // Find out if there are selected items below the current node. | |
3130 | bool selectCollapsingRow = false; | |
3131 | const unsigned rowAdjustment = node->GetSubTreeCount(); | |
3132 | unsigned maxRowToBeTested = row + rowAdjustment; | |
3133 | for(unsigned i=0; i<m_selection.size(); ++i) | |
3134 | { | |
3135 | const unsigned testRow = m_selection[i]; | |
3136 | if(testRow > row && testRow <= maxRowToBeTested) | |
3137 | { | |
3138 | selectCollapsingRow = true; | |
3139 | // get out as soon as we have found a node that is selected | |
3140 | break; | |
3141 | } | |
3142 | } | |
3143 | ||
3144 | node->ToggleOpen(); | |
3145 | ||
3146 | // If the node to be closed has selected items the user won't see those any longer. | |
3147 | // We select the collapsing node in this case. | |
3148 | if(selectCollapsingRow) | |
3149 | { | |
3150 | SelectAllRows(false); | |
3151 | ChangeCurrentRow(row); | |
3152 | SelectRow(row, true); | |
3153 | SendSelectionChangedEvent(GetItemByRow(row)); | |
3154 | } | |
3155 | else | |
3156 | { | |
3157 | // if there were no selected items below our node we still need to "fix" the | |
3158 | // selection list to adjust for the changing of the row indices. | |
3159 | // We actually do the opposite of what we are doing in Expand(). | |
3160 | for(unsigned i=0; i<m_selection.size(); ++i) | |
3161 | { | |
3162 | const unsigned testRow = m_selection[i]; | |
3163 | // all rows above us are not affected, so skip them | |
3164 | if(testRow <= row) | |
3165 | continue; | |
3166 | ||
3167 | m_selection[i] -= rowAdjustment; | |
3168 | } | |
3169 | ||
3170 | // if the "current row" is being collapsed away we change it to the current row ;-) | |
3171 | if(m_currentRow > row && m_currentRow <= maxRowToBeTested) | |
3172 | ChangeCurrentRow(row); | |
3173 | else if(m_currentRow > row) | |
3174 | ChangeCurrentRow(m_currentRow - rowAdjustment); | |
3175 | } | |
3176 | ||
3177 | m_count = -1; | |
3178 | UpdateDisplay(); | |
3179 | SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,node->GetItem()); | |
3180 | } | |
3181 | } | |
3182 | ||
3183 | wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item ) | |
3184 | { | |
3185 | const wxDataViewModel * model = GetModel(); | |
3186 | if( model == NULL ) | |
3187 | return NULL; | |
3188 | ||
3189 | if (!item.IsOk()) | |
3190 | return m_root; | |
3191 | ||
3192 | // Compose the parent-chain for the item we are looking for | |
3193 | wxVector<wxDataViewItem> parentChain; | |
3194 | wxDataViewItem it( item ); | |
3195 | while( it.IsOk() ) | |
3196 | { | |
3197 | parentChain.push_back(it); | |
3198 | it = model->GetParent(it); | |
3199 | } | |
3200 | ||
3201 | // Find the item along the parent-chain. | |
3202 | // This algorithm is designed to speed up the node-finding method | |
3203 | wxDataViewTreeNode* node = m_root; | |
3204 | for( unsigned iter = parentChain.size()-1; ; --iter ) | |
3205 | { | |
3206 | if( node->HasChildren() ) | |
3207 | { | |
3208 | if( node->GetChildNodes().empty() ) | |
3209 | { | |
3210 | // Even though the item is a container, it doesn't have any | |
3211 | // child nodes in the control's representation yet. We have | |
3212 | // to realize its subtree now. | |
3213 | SortPrepare(); | |
3214 | ::BuildTreeHelper(model, node->GetItem(), node); | |
3215 | } | |
3216 | ||
3217 | const wxDataViewTreeNodes& nodes = node->GetChildNodes(); | |
3218 | bool found = false; | |
3219 | ||
3220 | for (unsigned i = 0; i < nodes.GetCount(); ++i) | |
3221 | { | |
3222 | wxDataViewTreeNode* currentNode = nodes[i]; | |
3223 | if (currentNode->GetItem() == parentChain[iter]) | |
3224 | { | |
3225 | if (currentNode->GetItem() == item) | |
3226 | return currentNode; | |
3227 | ||
3228 | node = currentNode; | |
3229 | found = true; | |
3230 | break; | |
3231 | } | |
3232 | } | |
3233 | if (!found) | |
3234 | return NULL; | |
3235 | } | |
3236 | else | |
3237 | return NULL; | |
3238 | ||
3239 | if ( !iter ) | |
3240 | break; | |
3241 | } | |
3242 | return NULL; | |
3243 | } | |
3244 | ||
3245 | void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item, | |
3246 | wxDataViewColumn* &column ) | |
3247 | { | |
3248 | wxDataViewColumn *col = NULL; | |
3249 | unsigned int cols = GetOwner()->GetColumnCount(); | |
3250 | unsigned int colnum = 0; | |
3251 | int x, y; | |
3252 | m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y ); | |
3253 | for (unsigned x_start = 0; colnum < cols; colnum++) | |
3254 | { | |
3255 | col = GetOwner()->GetColumnAt(colnum); | |
3256 | if (col->IsHidden()) | |
3257 | continue; // skip it! | |
3258 | ||
3259 | unsigned int w = col->GetWidth(); | |
3260 | if (x_start+w >= (unsigned int)x) | |
3261 | break; | |
3262 | ||
3263 | x_start += w; | |
3264 | } | |
3265 | ||
3266 | column = col; | |
3267 | item = GetItemByRow( GetLineAt( y ) ); | |
3268 | } | |
3269 | ||
3270 | wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item, | |
3271 | const wxDataViewColumn* column ) | |
3272 | { | |
3273 | int xpos = 0; | |
3274 | int width = 0; | |
3275 | ||
3276 | unsigned int cols = GetOwner()->GetColumnCount(); | |
3277 | // If column is null the loop will compute the combined width of all columns. | |
3278 | // Otherwise, it will compute the x position of the column we are looking for. | |
3279 | for (unsigned int i = 0; i < cols; i++) | |
3280 | { | |
3281 | wxDataViewColumn* col = GetOwner()->GetColumnAt( i ); | |
3282 | ||
3283 | if (col == column) | |
3284 | break; | |
3285 | ||
3286 | if (col->IsHidden()) | |
3287 | continue; // skip it! | |
3288 | ||
3289 | xpos += col->GetWidth(); | |
3290 | width += col->GetWidth(); | |
3291 | } | |
3292 | ||
3293 | if(column != 0) | |
3294 | { | |
3295 | // If we have a column, we need can get its width directly. | |
3296 | if(column->IsHidden()) | |
3297 | width = 0; | |
3298 | else | |
3299 | width = column->GetWidth(); | |
3300 | ||
3301 | } | |
3302 | else | |
3303 | { | |
3304 | // If we have no column, we reset the x position back to zero. | |
3305 | xpos = 0; | |
3306 | } | |
3307 | ||
3308 | // we have to take an expander column into account and compute its indentation | |
3309 | // to get the correct x position where the actual text is | |
3310 | int indent = 0; | |
3311 | int row = GetRowByItem(item); | |
3312 | if (!IsList() && | |
3313 | (column == 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column) ) | |
3314 | { | |
3315 | wxDataViewTreeNode* node = GetTreeNodeByRow(row); | |
3316 | indent = GetOwner()->GetIndent() * node->GetIndentLevel(); | |
3317 | indent = indent + m_lineHeight; // use m_lineHeight as the width of the expander | |
3318 | } | |
3319 | ||
3320 | wxRect itemRect( xpos + indent, | |
3321 | GetLineStart( row ), | |
3322 | width - indent, | |
3323 | GetLineHeight( row ) ); | |
3324 | ||
3325 | GetOwner()->CalcScrolledPosition( itemRect.x, itemRect.y, | |
3326 | &itemRect.x, &itemRect.y ); | |
3327 | ||
3328 | return itemRect; | |
3329 | } | |
3330 | ||
3331 | int wxDataViewMainWindow::RecalculateCount() const | |
3332 | { | |
3333 | if (IsVirtualList()) | |
3334 | { | |
3335 | wxDataViewVirtualListModel *list_model = | |
3336 | (wxDataViewVirtualListModel*) GetModel(); | |
3337 | ||
3338 | return list_model->GetCount(); | |
3339 | } | |
3340 | else | |
3341 | { | |
3342 | return m_root->GetSubTreeCount(); | |
3343 | } | |
3344 | } | |
3345 | ||
3346 | class ItemToRowJob : public DoJob | |
3347 | { | |
3348 | public: | |
3349 | ItemToRowJob(const wxDataViewItem& item_, wxVector<wxDataViewItem>::reverse_iterator iter) | |
3350 | : m_iter(iter), | |
3351 | item(item_) | |
3352 | { | |
3353 | ret = -1; | |
3354 | } | |
3355 | ||
3356 | // Maybe binary search will help to speed up this process | |
3357 | virtual int operator() ( wxDataViewTreeNode * node) | |
3358 | { | |
3359 | ret ++; | |
3360 | if( node->GetItem() == item ) | |
3361 | { | |
3362 | return DoJob::DONE; | |
3363 | } | |
3364 | ||
3365 | if( node->GetItem() == *m_iter ) | |
3366 | { | |
3367 | m_iter++; | |
3368 | return DoJob::CONTINUE; | |
3369 | } | |
3370 | else | |
3371 | { | |
3372 | ret += node->GetSubTreeCount(); | |
3373 | return DoJob::SKIP_SUBTREE; | |
3374 | } | |
3375 | ||
3376 | } | |
3377 | ||
3378 | // the row number is begin from zero | |
3379 | int GetResult() const | |
3380 | { return ret -1; } | |
3381 | ||
3382 | private: | |
3383 | wxVector<wxDataViewItem>::reverse_iterator m_iter; | |
3384 | wxDataViewItem item; | |
3385 | int ret; | |
3386 | ||
3387 | }; | |
3388 | ||
3389 | int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) const | |
3390 | { | |
3391 | const wxDataViewModel * model = GetModel(); | |
3392 | if( model == NULL ) | |
3393 | return -1; | |
3394 | ||
3395 | if (IsVirtualList()) | |
3396 | { | |
3397 | return wxPtrToUInt( item.GetID() ) -1; | |
3398 | } | |
3399 | else | |
3400 | { | |
3401 | if( !item.IsOk() ) | |
3402 | return -1; | |
3403 | ||
3404 | // Compose the parent-chain of the item we are looking for | |
3405 | wxVector<wxDataViewItem> parentChain; | |
3406 | wxDataViewItem it( item ); | |
3407 | while( it.IsOk() ) | |
3408 | { | |
3409 | parentChain.push_back(it); | |
3410 | it = model->GetParent(it); | |
3411 | } | |
3412 | ||
3413 | // add an 'invalid' item to represent our 'invisible' root node | |
3414 | parentChain.push_back(wxDataViewItem()); | |
3415 | ||
3416 | // the parent chain was created by adding the deepest parent first. | |
3417 | // so if we want to start at the root node, we have to iterate backwards through the vector | |
3418 | ItemToRowJob job( item, parentChain.rbegin() ); | |
3419 | Walker( m_root, job ); | |
3420 | return job.GetResult(); | |
3421 | } | |
3422 | } | |
3423 | ||
3424 | static void BuildTreeHelper( const wxDataViewModel * model, const wxDataViewItem & item, | |
3425 | wxDataViewTreeNode * node) | |
3426 | { | |
3427 | if( !model->IsContainer( item ) ) | |
3428 | return; | |
3429 | ||
3430 | wxDataViewItemArray children; | |
3431 | unsigned int num = model->GetChildren( item, children); | |
3432 | ||
3433 | for ( unsigned int index = 0; index < num; index++ ) | |
3434 | { | |
3435 | wxDataViewTreeNode *n = new wxDataViewTreeNode(node, children[index]); | |
3436 | ||
3437 | if( model->IsContainer(children[index]) ) | |
3438 | n->SetHasChildren( true ); | |
3439 | ||
3440 | node->InsertChild(n, index); | |
3441 | } | |
3442 | ||
3443 | wxASSERT( node->IsOpen() ); | |
3444 | node->ChangeSubTreeCount(+num); | |
3445 | } | |
3446 | ||
3447 | void wxDataViewMainWindow::BuildTree(wxDataViewModel * model) | |
3448 | { | |
3449 | DestroyTree(); | |
3450 | ||
3451 | if (GetModel()->IsVirtualListModel()) | |
3452 | { | |
3453 | m_count = -1; | |
3454 | return; | |
3455 | } | |
3456 | ||
3457 | m_root = wxDataViewTreeNode::CreateRootNode(); | |
3458 | ||
3459 | // First we define a invalid item to fetch the top-level elements | |
3460 | wxDataViewItem item; | |
3461 | SortPrepare(); | |
3462 | BuildTreeHelper( model, item, m_root); | |
3463 | m_count = -1; | |
3464 | } | |
3465 | ||
3466 | void wxDataViewMainWindow::DestroyTree() | |
3467 | { | |
3468 | if (!IsVirtualList()) | |
3469 | { | |
3470 | wxDELETE(m_root); | |
3471 | m_count = 0; | |
3472 | } | |
3473 | } | |
3474 | ||
3475 | wxDataViewColumn* | |
3476 | wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem& item, wxDataViewCellMode mode) | |
3477 | { | |
3478 | // Edit the current column editable in 'mode'. If no column is focused | |
3479 | // (typically because the user has full row selected), try to find the | |
3480 | // first editable column (this would typically be a checkbox for | |
3481 | // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set | |
3482 | // focus on the checkbox column; or on the only editable text column). | |
3483 | ||
3484 | wxDataViewColumn *candidate = m_currentCol; | |
3485 | ||
3486 | if ( candidate && | |
3487 | !IsCellEditableInMode(item, candidate, mode) && | |
3488 | !m_currentColSetByKeyboard ) | |
3489 | { | |
3490 | // If current column was set by mouse to something not editable (in | |
3491 | // 'mode') and the user pressed Space/F2 to edit it, treat the | |
3492 | // situation as if there was whole-row focus, because that's what is | |
3493 | // visually indicated and the mouse click could very well be targeted | |
3494 | // on the row rather than on an individual cell. | |
3495 | // | |
3496 | // But if it was done by keyboard, respect that even if the column | |
3497 | // isn't editable, because focus is visually on that column and editing | |
3498 | // something else would be surprising. | |
3499 | candidate = NULL; | |
3500 | } | |
3501 | ||
3502 | if ( !candidate ) | |
3503 | { | |
3504 | const unsigned cols = GetOwner()->GetColumnCount(); | |
3505 | for ( unsigned i = 0; i < cols; i++ ) | |
3506 | { | |
3507 | wxDataViewColumn *c = GetOwner()->GetColumnAt(i); | |
3508 | if ( c->IsHidden() ) | |
3509 | continue; | |
3510 | ||
3511 | if ( IsCellEditableInMode(item, c, mode) ) | |
3512 | { | |
3513 | candidate = c; | |
3514 | break; | |
3515 | } | |
3516 | } | |
3517 | } | |
3518 | ||
3519 | // If on container item without columns, only the expander column | |
3520 | // may be directly editable: | |
3521 | if ( candidate && | |
3522 | GetOwner()->GetExpanderColumn() != candidate && | |
3523 | GetModel()->IsContainer(item) && | |
3524 | !GetModel()->HasContainerColumns(item) ) | |
3525 | { | |
3526 | candidate = GetOwner()->GetExpanderColumn(); | |
3527 | } | |
3528 | ||
3529 | if ( !candidate ) | |
3530 | return NULL; | |
3531 | ||
3532 | if ( !IsCellEditableInMode(item, candidate, mode) ) | |
3533 | return NULL; | |
3534 | ||
3535 | return candidate; | |
3536 | } | |
3537 | ||
3538 | bool wxDataViewMainWindow::IsCellEditableInMode(const wxDataViewItem& item, | |
3539 | const wxDataViewColumn *col, | |
3540 | wxDataViewCellMode mode) const | |
3541 | { | |
3542 | if ( col->GetRenderer()->GetMode() != mode ) | |
3543 | return false; | |
3544 | ||
3545 | if ( !GetModel()->IsEnabled(item, col->GetModelColumn()) ) | |
3546 | return false; | |
3547 | ||
3548 | return true; | |
3549 | } | |
3550 | ||
3551 | void wxDataViewMainWindow::OnCharHook(wxKeyEvent& event) | |
3552 | { | |
3553 | if ( m_editorCtrl ) | |
3554 | { | |
3555 | // Handle any keys special for the in-place editor and return without | |
3556 | // calling Skip() below. | |
3557 | switch ( event.GetKeyCode() ) | |
3558 | { | |
3559 | case WXK_ESCAPE: | |
3560 | m_editorRenderer->CancelEditing(); | |
3561 | return; | |
3562 | ||
3563 | case WXK_RETURN: | |
3564 | m_editorRenderer->FinishEditing(); | |
3565 | return; | |
3566 | } | |
3567 | } | |
3568 | ||
3569 | event.Skip(); | |
3570 | } | |
3571 | ||
3572 | void wxDataViewMainWindow::OnChar( wxKeyEvent &event ) | |
3573 | { | |
3574 | wxWindow * const parent = GetParent(); | |
3575 | ||
3576 | // propagate the char event upwards | |
3577 | wxKeyEvent eventForParent(event); | |
3578 | eventForParent.SetEventObject(parent); | |
3579 | if ( parent->ProcessWindowEvent(eventForParent) ) | |
3580 | return; | |
3581 | ||
3582 | if ( parent->HandleAsNavigationKey(event) ) | |
3583 | return; | |
3584 | ||
3585 | // no item -> nothing to do | |
3586 | if (!HasCurrentRow()) | |
3587 | { | |
3588 | event.Skip(); | |
3589 | return; | |
3590 | } | |
3591 | ||
3592 | // don't use m_linesPerPage directly as it might not be computed yet | |
3593 | const int pageSize = GetCountPerPage(); | |
3594 | wxCHECK_RET( pageSize, wxT("should have non zero page size") ); | |
3595 | ||
3596 | switch ( event.GetKeyCode() ) | |
3597 | { | |
3598 | case WXK_RETURN: | |
3599 | if ( event.HasModifiers() ) | |
3600 | { | |
3601 | event.Skip(); | |
3602 | break; | |
3603 | } | |
3604 | else | |
3605 | { | |
3606 | // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to | |
3607 | // it. Only if that event is not handled do we activate column renderer (which | |
3608 | // is normally done by Space) or even inline editing. | |
3609 | ||
3610 | const wxDataViewItem item = GetItemByRow(m_currentRow); | |
3611 | ||
3612 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, | |
3613 | parent->GetId()); | |
3614 | le.SetItem(item); | |
3615 | le.SetEventObject(parent); | |
3616 | le.SetModel(GetModel()); | |
3617 | ||
3618 | if ( parent->ProcessWindowEvent(le) ) | |
3619 | break; | |
3620 | // else: fall through to WXK_SPACE handling | |
3621 | } | |
3622 | ||
3623 | case WXK_SPACE: | |
3624 | if ( event.HasModifiers() ) | |
3625 | { | |
3626 | event.Skip(); | |
3627 | break; | |
3628 | } | |
3629 | else | |
3630 | { | |
3631 | // Space toggles activatable items or -- if not activatable -- | |
3632 | // starts inline editing (this is normally done using F2 on | |
3633 | // Windows, but Space is common everywhere else, so use it too | |
3634 | // for greater cross-platform compatibility). | |
3635 | ||
3636 | const wxDataViewItem item = GetItemByRow(m_currentRow); | |
3637 | ||
3638 | // Activate the current activatable column. If not column is focused (typically | |
3639 | // because the user has full row selected), try to find the first activatable | |
3640 | // column (this would typically be a checkbox and we don't want to force the user | |
3641 | // to set focus on the checkbox column). | |
3642 | wxDataViewColumn *activatableCol = FindColumnForEditing(item, wxDATAVIEW_CELL_ACTIVATABLE); | |
3643 | ||
3644 | if ( activatableCol ) | |
3645 | { | |
3646 | const unsigned colIdx = activatableCol->GetModelColumn(); | |
3647 | const wxRect cell_rect = GetOwner()->GetItemRect(item, activatableCol); | |
3648 | ||
3649 | wxDataViewRenderer *cell = activatableCol->GetRenderer(); | |
3650 | cell->PrepareForItem(GetModel(), item, colIdx); | |
3651 | cell->WXActivateCell(cell_rect, GetModel(), item, colIdx, NULL); | |
3652 | ||
3653 | break; | |
3654 | } | |
3655 | // else: fall through to WXK_F2 handling | |
3656 | } | |
3657 | ||
3658 | case WXK_F2: | |
3659 | if ( event.HasModifiers() ) | |
3660 | { | |
3661 | event.Skip(); | |
3662 | break; | |
3663 | } | |
3664 | else | |
3665 | { | |
3666 | if( !m_selection.empty() ) | |
3667 | { | |
3668 | // Mimic Windows 7 behavior: edit the item that has focus | |
3669 | // if it is selected and the first selected item if focus | |
3670 | // is out of selection. | |
3671 | int sel; | |
3672 | if ( m_selection.Index(m_currentRow) != wxNOT_FOUND ) | |
3673 | sel = m_currentRow; | |
3674 | else | |
3675 | sel = m_selection[0]; | |
3676 | ||
3677 | ||
3678 | const wxDataViewItem item = GetItemByRow(sel); | |
3679 | ||
3680 | // Edit the current column. If no column is focused | |
3681 | // (typically because the user has full row selected), try | |
3682 | // to find the first editable column. | |
3683 | wxDataViewColumn *editableCol = FindColumnForEditing(item, wxDATAVIEW_CELL_EDITABLE); | |
3684 | ||
3685 | if ( editableCol ) | |
3686 | GetOwner()->EditItem(item, editableCol); | |
3687 | } | |
3688 | } | |
3689 | break; | |
3690 | ||
3691 | case WXK_UP: | |
3692 | if ( m_currentRow > 0 ) | |
3693 | OnVerticalNavigation( m_currentRow - 1, event ); | |
3694 | break; | |
3695 | ||
3696 | case WXK_DOWN: | |
3697 | if ( m_currentRow + 1 < GetRowCount() ) | |
3698 | OnVerticalNavigation( m_currentRow + 1, event ); | |
3699 | break; | |
3700 | // Add the process for tree expanding/collapsing | |
3701 | case WXK_LEFT: | |
3702 | OnLeftKey(); | |
3703 | break; | |
3704 | ||
3705 | case WXK_RIGHT: | |
3706 | OnRightKey(); | |
3707 | break; | |
3708 | ||
3709 | case WXK_END: | |
3710 | { | |
3711 | if (!IsEmpty()) | |
3712 | OnVerticalNavigation( GetRowCount() - 1, event ); | |
3713 | break; | |
3714 | } | |
3715 | case WXK_HOME: | |
3716 | if (!IsEmpty()) | |
3717 | OnVerticalNavigation( 0, event ); | |
3718 | break; | |
3719 | ||
3720 | case WXK_PAGEUP: | |
3721 | { | |
3722 | int steps = pageSize - 1; | |
3723 | int index = m_currentRow - steps; | |
3724 | if (index < 0) | |
3725 | index = 0; | |
3726 | ||
3727 | OnVerticalNavigation( index, event ); | |
3728 | } | |
3729 | break; | |
3730 | ||
3731 | case WXK_PAGEDOWN: | |
3732 | { | |
3733 | int steps = pageSize - 1; | |
3734 | unsigned int index = m_currentRow + steps; | |
3735 | unsigned int count = GetRowCount(); | |
3736 | if ( index >= count ) | |
3737 | index = count - 1; | |
3738 | ||
3739 | OnVerticalNavigation( index, event ); | |
3740 | } | |
3741 | break; | |
3742 | ||
3743 | default: | |
3744 | event.Skip(); | |
3745 | } | |
3746 | } | |
3747 | ||
3748 | void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent, const wxKeyEvent& event) | |
3749 | { | |
3750 | wxCHECK_RET( newCurrent < GetRowCount(), | |
3751 | wxT("invalid item index in OnVerticalNavigation()") ); | |
3752 | ||
3753 | // if there is no selection, we cannot move it anywhere | |
3754 | if (!HasCurrentRow()) | |
3755 | return; | |
3756 | ||
3757 | unsigned int oldCurrent = m_currentRow; | |
3758 | ||
3759 | // in single selection we just ignore Shift as we can't select several | |
3760 | // items anyhow | |
3761 | if ( event.ShiftDown() && !IsSingleSel() ) | |
3762 | { | |
3763 | RefreshRow( oldCurrent ); | |
3764 | ||
3765 | ChangeCurrentRow( newCurrent ); | |
3766 | ||
3767 | // select all the items between the old and the new one | |
3768 | if ( oldCurrent > newCurrent ) | |
3769 | { | |
3770 | newCurrent = oldCurrent; | |
3771 | oldCurrent = m_currentRow; | |
3772 | } | |
3773 | ||
3774 | SelectRows( oldCurrent, newCurrent, true ); | |
3775 | if (oldCurrent!=newCurrent) | |
3776 | SendSelectionChangedEvent(GetItemByRow(m_selection[0])); | |
3777 | } | |
3778 | else // !shift | |
3779 | { | |
3780 | RefreshRow( oldCurrent ); | |
3781 | ||
3782 | // all previously selected items are unselected unless ctrl is held | |
3783 | if ( !event.ControlDown() ) | |
3784 | SelectAllRows(false); | |
3785 | ||
3786 | ChangeCurrentRow( newCurrent ); | |
3787 | ||
3788 | if ( !event.ControlDown() ) | |
3789 | { | |
3790 | SelectRow( m_currentRow, true ); | |
3791 | SendSelectionChangedEvent(GetItemByRow(m_currentRow)); | |
3792 | } | |
3793 | else | |
3794 | RefreshRow( m_currentRow ); | |
3795 | } | |
3796 | ||
3797 | GetOwner()->EnsureVisible( m_currentRow, -1 ); | |
3798 | } | |
3799 | ||
3800 | void wxDataViewMainWindow::OnLeftKey() | |
3801 | { | |
3802 | if ( IsList() ) | |
3803 | { | |
3804 | TryAdvanceCurrentColumn(NULL, /*forward=*/false); | |
3805 | } | |
3806 | else | |
3807 | { | |
3808 | wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow); | |
3809 | ||
3810 | if ( TryAdvanceCurrentColumn(node, /*forward=*/false) ) | |
3811 | return; | |
3812 | ||
3813 | // Because TryAdvanceCurrentColumn() return false, we are at the first | |
3814 | // column or using whole-row selection. In this situation, we can use | |
3815 | // the standard TreeView handling of the left key. | |
3816 | if (node->HasChildren() && node->IsOpen()) | |
3817 | { | |
3818 | Collapse(m_currentRow); | |
3819 | } | |
3820 | else | |
3821 | { | |
3822 | // if the node is already closed, we move the selection to its parent | |
3823 | wxDataViewTreeNode *parent_node = node->GetParent(); | |
3824 | ||
3825 | if (parent_node) | |
3826 | { | |
3827 | int parent = GetRowByItem( parent_node->GetItem() ); | |
3828 | if ( parent >= 0 ) | |
3829 | { | |
3830 | unsigned int row = m_currentRow; | |
3831 | SelectRow( row, false); | |
3832 | SelectRow( parent, true ); | |
3833 | ChangeCurrentRow( parent ); | |
3834 | GetOwner()->EnsureVisible( parent, -1 ); | |
3835 | SendSelectionChangedEvent( parent_node->GetItem() ); | |
3836 | } | |
3837 | } | |
3838 | } | |
3839 | } | |
3840 | } | |
3841 | ||
3842 | void wxDataViewMainWindow::OnRightKey() | |
3843 | { | |
3844 | if ( IsList() ) | |
3845 | { | |
3846 | TryAdvanceCurrentColumn(NULL, /*forward=*/true); | |
3847 | } | |
3848 | else | |
3849 | { | |
3850 | wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow); | |
3851 | ||
3852 | if ( node->HasChildren() ) | |
3853 | { | |
3854 | if ( !node->IsOpen() ) | |
3855 | { | |
3856 | Expand( m_currentRow ); | |
3857 | } | |
3858 | else | |
3859 | { | |
3860 | // if the node is already open, we move the selection to the first child | |
3861 | unsigned int row = m_currentRow; | |
3862 | SelectRow( row, false ); | |
3863 | SelectRow( row + 1, true ); | |
3864 | ChangeCurrentRow( row + 1 ); | |
3865 | GetOwner()->EnsureVisible( row + 1, -1 ); | |
3866 | SendSelectionChangedEvent( GetItemByRow(row+1) ); | |
3867 | } | |
3868 | } | |
3869 | else | |
3870 | { | |
3871 | TryAdvanceCurrentColumn(node, /*forward=*/true); | |
3872 | } | |
3873 | } | |
3874 | } | |
3875 | ||
3876 | bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode *node, bool forward) | |
3877 | { | |
3878 | if ( GetOwner()->GetColumnCount() == 0 ) | |
3879 | return false; | |
3880 | ||
3881 | if ( !m_useCellFocus ) | |
3882 | return false; | |
3883 | ||
3884 | if ( node ) | |
3885 | { | |
3886 | // navigation shouldn't work in branch nodes without other columns: | |
3887 | if ( node->HasChildren() && !GetModel()->HasContainerColumns(node->GetItem()) ) | |
3888 | return false; | |
3889 | } | |
3890 | ||
3891 | if ( m_currentCol == NULL || !m_currentColSetByKeyboard ) | |
3892 | { | |
3893 | if ( forward ) | |
3894 | { | |
3895 | m_currentCol = GetOwner()->GetColumnAt(1); | |
3896 | m_currentColSetByKeyboard = true; | |
3897 | RefreshRow(m_currentRow); | |
3898 | return true; | |
3899 | } | |
3900 | else | |
3901 | return false; | |
3902 | } | |
3903 | ||
3904 | int idx = GetOwner()->GetColumnIndex(m_currentCol) + (forward ? +1 : -1); | |
3905 | ||
3906 | if ( idx >= (int)GetOwner()->GetColumnCount() ) | |
3907 | return false; | |
3908 | ||
3909 | GetOwner()->EnsureVisible(m_currentRow, idx); | |
3910 | ||
3911 | if ( idx < 1 ) | |
3912 | { | |
3913 | // We are going to the left of the second column. Reset to whole-row | |
3914 | // focus (which means first column would be edited). | |
3915 | m_currentCol = NULL; | |
3916 | RefreshRow(m_currentRow); | |
3917 | return true; | |
3918 | } | |
3919 | ||
3920 | m_currentCol = GetOwner()->GetColumnAt(idx); | |
3921 | m_currentColSetByKeyboard = true; | |
3922 | RefreshRow(m_currentRow); | |
3923 | return true; | |
3924 | } | |
3925 | ||
3926 | void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) | |
3927 | { | |
3928 | if (event.GetEventType() == wxEVT_MOUSEWHEEL) | |
3929 | { | |
3930 | // let the base handle mouse wheel events. | |
3931 | event.Skip(); | |
3932 | return; | |
3933 | } | |
3934 | ||
3935 | // set the focus to ourself if any of the mouse buttons are pressed | |
3936 | if(event.ButtonDown() && !HasFocus()) | |
3937 | SetFocus(); | |
3938 | ||
3939 | int x = event.GetX(); | |
3940 | int y = event.GetY(); | |
3941 | m_owner->CalcUnscrolledPosition( x, y, &x, &y ); | |
3942 | wxDataViewColumn *col = NULL; | |
3943 | ||
3944 | int xpos = 0; | |
3945 | unsigned int cols = GetOwner()->GetColumnCount(); | |
3946 | unsigned int i; | |
3947 | for (i = 0; i < cols; i++) | |
3948 | { | |
3949 | wxDataViewColumn *c = GetOwner()->GetColumnAt( i ); | |
3950 | if (c->IsHidden()) | |
3951 | continue; // skip it! | |
3952 | ||
3953 | if (x < xpos + c->GetWidth()) | |
3954 | { | |
3955 | col = c; | |
3956 | break; | |
3957 | } | |
3958 | xpos += c->GetWidth(); | |
3959 | } | |
3960 | ||
3961 | wxDataViewModel* const model = GetModel(); | |
3962 | ||
3963 | const unsigned int current = GetLineAt( y ); | |
3964 | const wxDataViewItem item = GetItemByRow(current); | |
3965 | ||
3966 | // Handle right clicking here, before everything else as context menu | |
3967 | // events should be sent even when we click outside of any item, unlike all | |
3968 | // the other ones. | |
3969 | if (event.RightUp()) | |
3970 | { | |
3971 | wxWindow *parent = GetParent(); | |
3972 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, parent->GetId()); | |
3973 | le.SetEventObject(parent); | |
3974 | le.SetModel(model); | |
3975 | ||
3976 | if ( item.IsOk() && col ) | |
3977 | { | |
3978 | le.SetItem( item ); | |
3979 | le.SetColumn( col->GetModelColumn() ); | |
3980 | le.SetDataViewColumn( col ); | |
3981 | ||
3982 | wxVariant value; | |
3983 | model->GetValue( value, item, col->GetModelColumn() ); | |
3984 | le.SetValue(value); | |
3985 | } | |
3986 | ||
3987 | parent->ProcessWindowEvent(le); | |
3988 | return; | |
3989 | } | |
3990 | ||
3991 | if (!col) | |
3992 | { | |
3993 | event.Skip(); | |
3994 | return; | |
3995 | } | |
3996 | ||
3997 | wxDataViewRenderer *cell = col->GetRenderer(); | |
3998 | if ((current >= GetRowCount()) || (x > GetEndOfLastCol())) | |
3999 | { | |
4000 | // Unselect all if below the last row ? | |
4001 | event.Skip(); | |
4002 | return; | |
4003 | } | |
4004 | ||
4005 | wxDataViewColumn* const | |
4006 | expander = GetExpanderColumnOrFirstOne(GetOwner()); | |
4007 | ||
4008 | // Test whether the mouse is hovering over the expander (a.k.a tree "+" | |
4009 | // button) and also determine the offset of the real cell start, skipping | |
4010 | // the indentation and the expander itself. | |
4011 | bool hoverOverExpander = false; | |
4012 | int itemOffset = 0; | |
4013 | if ((!IsList()) && (expander == col)) | |
4014 | { | |
4015 | wxDataViewTreeNode * node = GetTreeNodeByRow(current); | |
4016 | ||
4017 | int indent = node->GetIndentLevel(); | |
4018 | itemOffset = GetOwner()->GetIndent()*indent; | |
4019 | ||
4020 | if ( node->HasChildren() ) | |
4021 | { | |
4022 | // we make the rectangle we are looking in a bit bigger than the actual | |
4023 | // visual expander so the user can hit that little thing reliably | |
4024 | wxRect rect(itemOffset, | |
4025 | GetLineStart( current ) + (GetLineHeight(current) - m_lineHeight)/2, | |
4026 | m_lineHeight, m_lineHeight); | |
4027 | ||
4028 | if( rect.Contains(x, y) ) | |
4029 | { | |
4030 | // So the mouse is over the expander | |
4031 | hoverOverExpander = true; | |
4032 | if (m_underMouse && m_underMouse != node) | |
4033 | { | |
4034 | // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem())); | |
4035 | RefreshRow(GetRowByItem(m_underMouse->GetItem())); | |
4036 | } | |
4037 | if (m_underMouse != node) | |
4038 | { | |
4039 | // wxLogMessage("Do the row: %d", current); | |
4040 | RefreshRow(current); | |
4041 | } | |
4042 | m_underMouse = node; | |
4043 | } | |
4044 | } | |
4045 | ||
4046 | // Account for the expander as well, even if this item doesn't have it, | |
4047 | // its parent does so it still counts for the offset. | |
4048 | itemOffset += m_lineHeight; | |
4049 | } | |
4050 | if (!hoverOverExpander) | |
4051 | { | |
4052 | if (m_underMouse != NULL) | |
4053 | { | |
4054 | // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem())); | |
4055 | RefreshRow(GetRowByItem(m_underMouse->GetItem())); | |
4056 | m_underMouse = NULL; | |
4057 | } | |
4058 | } | |
4059 | ||
4060 | #if wxUSE_DRAG_AND_DROP | |
4061 | if (event.Dragging()) | |
4062 | { | |
4063 | if (m_dragCount == 0) | |
4064 | { | |
4065 | // we have to report the raw, physical coords as we want to be | |
4066 | // able to call HitTest(event.m_pointDrag) from the user code to | |
4067 | // get the item being dragged | |
4068 | m_dragStart = event.GetPosition(); | |
4069 | } | |
4070 | ||
4071 | m_dragCount++; | |
4072 | ||
4073 | if (m_dragCount != 3) | |
4074 | return; | |
4075 | ||
4076 | if (event.LeftIsDown()) | |
4077 | { | |
4078 | m_owner->CalcUnscrolledPosition( m_dragStart.x, m_dragStart.y, | |
4079 | &m_dragStart.x, &m_dragStart.y ); | |
4080 | unsigned int drag_item_row = GetLineAt( m_dragStart.y ); | |
4081 | wxDataViewItem itemDragged = GetItemByRow( drag_item_row ); | |
4082 | ||
4083 | // Notify cell about drag | |
4084 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() ); | |
4085 | event.SetEventObject( m_owner ); | |
4086 | event.SetItem( itemDragged ); | |
4087 | event.SetModel( model ); | |
4088 | if (!m_owner->HandleWindowEvent( event )) | |
4089 | return; | |
4090 | ||
4091 | if (!event.IsAllowed()) | |
4092 | return; | |
4093 | ||
4094 | wxDataObject *obj = event.GetDataObject(); | |
4095 | if (!obj) | |
4096 | return; | |
4097 | ||
4098 | wxDataViewDropSource drag( this, drag_item_row ); | |
4099 | drag.SetData( *obj ); | |
4100 | /* wxDragResult res = */ drag.DoDragDrop(event.GetDragFlags()); | |
4101 | delete obj; | |
4102 | } | |
4103 | return; | |
4104 | } | |
4105 | else | |
4106 | { | |
4107 | m_dragCount = 0; | |
4108 | } | |
4109 | #endif // wxUSE_DRAG_AND_DROP | |
4110 | ||
4111 | bool simulateClick = false; | |
4112 | ||
4113 | if (event.ButtonDClick()) | |
4114 | { | |
4115 | m_renameTimer->Stop(); | |
4116 | m_lastOnSame = false; | |
4117 | } | |
4118 | ||
4119 | bool ignore_other_columns = | |
4120 | ((expander != col) && | |
4121 | (model->IsContainer(item)) && | |
4122 | (!model->HasContainerColumns(item))); | |
4123 | ||
4124 | if (event.LeftDClick()) | |
4125 | { | |
4126 | if(hoverOverExpander) | |
4127 | { | |
4128 | // a double click on the expander will be converted into a "simulated" normal click | |
4129 | simulateClick = true; | |
4130 | } | |
4131 | else if ( current == m_lineLastClicked ) | |
4132 | { | |
4133 | wxWindow *parent = GetParent(); | |
4134 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId()); | |
4135 | le.SetItem( item ); | |
4136 | le.SetColumn( col->GetModelColumn() ); | |
4137 | le.SetDataViewColumn( col ); | |
4138 | le.SetEventObject(parent); | |
4139 | le.SetModel(GetModel()); | |
4140 | ||
4141 | parent->ProcessWindowEvent(le); | |
4142 | return; | |
4143 | } | |
4144 | else | |
4145 | { | |
4146 | // The first click was on another item, so don't interpret this as | |
4147 | // a double click, but as a simple click instead | |
4148 | simulateClick = true; | |
4149 | } | |
4150 | } | |
4151 | ||
4152 | if (event.LeftUp() && !hoverOverExpander) | |
4153 | { | |
4154 | if (m_lineSelectSingleOnUp != (unsigned int)-1) | |
4155 | { | |
4156 | // select single line | |
4157 | SelectAllRows( false ); | |
4158 | SelectRow( m_lineSelectSingleOnUp, true ); | |
4159 | SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) ); | |
4160 | } | |
4161 | ||
4162 | // If the user click the expander, we do not do editing even if the column | |
4163 | // with expander are editable | |
4164 | if (m_lastOnSame && !ignore_other_columns) | |
4165 | { | |
4166 | if ((col == m_currentCol) && (current == m_currentRow) && | |
4167 | IsCellEditableInMode(item, col, wxDATAVIEW_CELL_EDITABLE) ) | |
4168 | { | |
4169 | m_renameTimer->Start( 100, true ); | |
4170 | } | |
4171 | } | |
4172 | ||
4173 | m_lastOnSame = false; | |
4174 | m_lineSelectSingleOnUp = (unsigned int)-1; | |
4175 | } | |
4176 | else if(!event.LeftUp()) | |
4177 | { | |
4178 | // This is necessary, because after a DnD operation in | |
4179 | // from and to ourself, the up event is swallowed by the | |
4180 | // DnD code. So on next non-up event (which means here and | |
4181 | // now) m_lineSelectSingleOnUp should be reset. | |
4182 | m_lineSelectSingleOnUp = (unsigned int)-1; | |
4183 | } | |
4184 | ||
4185 | if (event.RightDown()) | |
4186 | { | |
4187 | m_lineBeforeLastClicked = m_lineLastClicked; | |
4188 | m_lineLastClicked = current; | |
4189 | ||
4190 | // If the item is already selected, do not update the selection. | |
4191 | // Multi-selections should not be cleared if a selected item is clicked. | |
4192 | if (!IsRowSelected(current)) | |
4193 | { | |
4194 | SelectAllRows(false); | |
4195 | const unsigned oldCurrent = m_currentRow; | |
4196 | ChangeCurrentRow(current); | |
4197 | SelectRow(m_currentRow,true); | |
4198 | RefreshRow(oldCurrent); | |
4199 | SendSelectionChangedEvent(GetItemByRow( m_currentRow ) ); | |
4200 | } | |
4201 | } | |
4202 | else if (event.MiddleDown()) | |
4203 | { | |
4204 | } | |
4205 | ||
4206 | if((event.LeftDown() || simulateClick) && hoverOverExpander) | |
4207 | { | |
4208 | wxDataViewTreeNode* node = GetTreeNodeByRow(current); | |
4209 | ||
4210 | // hoverOverExpander being true tells us that our node must be | |
4211 | // valid and have children. | |
4212 | // So we don't need any extra checks. | |
4213 | if( node->IsOpen() ) | |
4214 | Collapse(current); | |
4215 | else | |
4216 | Expand(current); | |
4217 | } | |
4218 | else if ((event.LeftDown() || simulateClick) && !hoverOverExpander) | |
4219 | { | |
4220 | m_lineBeforeLastClicked = m_lineLastClicked; | |
4221 | m_lineLastClicked = current; | |
4222 | ||
4223 | unsigned int oldCurrentRow = m_currentRow; | |
4224 | bool oldWasSelected = IsRowSelected(m_currentRow); | |
4225 | ||
4226 | bool cmdModifierDown = event.CmdDown(); | |
4227 | if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) ) | |
4228 | { | |
4229 | if ( IsSingleSel() || !IsRowSelected(current) ) | |
4230 | { | |
4231 | SelectAllRows( false ); | |
4232 | ChangeCurrentRow(current); | |
4233 | SelectRow(m_currentRow,true); | |
4234 | SendSelectionChangedEvent(GetItemByRow( m_currentRow ) ); | |
4235 | } | |
4236 | else // multi sel & current is highlighted & no mod keys | |
4237 | { | |
4238 | m_lineSelectSingleOnUp = current; | |
4239 | ChangeCurrentRow(current); // change focus | |
4240 | } | |
4241 | } | |
4242 | else // multi sel & either ctrl or shift is down | |
4243 | { | |
4244 | if (cmdModifierDown) | |
4245 | { | |
4246 | ChangeCurrentRow(current); | |
4247 | ReverseRowSelection(m_currentRow); | |
4248 | SendSelectionChangedEvent(GetItemByRow(m_currentRow)); | |
4249 | } | |
4250 | else if (event.ShiftDown()) | |
4251 | { | |
4252 | ChangeCurrentRow(current); | |
4253 | ||
4254 | unsigned int lineFrom = oldCurrentRow, | |
4255 | lineTo = current; | |
4256 | ||
4257 | if ( lineTo < lineFrom ) | |
4258 | { | |
4259 | lineTo = lineFrom; | |
4260 | lineFrom = m_currentRow; | |
4261 | } | |
4262 | ||
4263 | SelectRows(lineFrom, lineTo, true); | |
4264 | SendSelectionChangedEvent(GetItemByRow(m_selection[0]) ); | |
4265 | } | |
4266 | else // !ctrl, !shift | |
4267 | { | |
4268 | // test in the enclosing if should make it impossible | |
4269 | wxFAIL_MSG( wxT("how did we get here?") ); | |
4270 | } | |
4271 | } | |
4272 | ||
4273 | if (m_currentRow != oldCurrentRow) | |
4274 | RefreshRow( oldCurrentRow ); | |
4275 | ||
4276 | wxDataViewColumn *oldCurrentCol = m_currentCol; | |
4277 | ||
4278 | // Update selection here... | |
4279 | m_currentCol = col; | |
4280 | m_currentColSetByKeyboard = false; | |
4281 | ||
4282 | m_lastOnSame = !simulateClick && ((col == oldCurrentCol) && | |
4283 | (current == oldCurrentRow)) && oldWasSelected; | |
4284 | ||
4285 | // Call ActivateCell() after everything else as under GTK+ | |
4286 | if ( IsCellEditableInMode(item, col, wxDATAVIEW_CELL_ACTIVATABLE) ) | |
4287 | { | |
4288 | // notify cell about click | |
4289 | cell->PrepareForItem(model, item, col->GetModelColumn()); | |
4290 | ||
4291 | wxRect cell_rect( xpos + itemOffset, | |
4292 | GetLineStart( current ), | |
4293 | col->GetWidth() - itemOffset, | |
4294 | GetLineHeight( current ) ); | |
4295 | ||
4296 | // Report position relative to the cell's custom area, i.e. | |
4297 | // no the entire space as given by the control but the one | |
4298 | // used by the renderer after calculation of alignment etc. | |
4299 | ||
4300 | // adjust the rectangle ourselves to account for the alignment | |
4301 | wxRect rectItem = cell_rect; | |
4302 | const int align = cell->GetAlignment(); | |
4303 | if ( align != wxDVR_DEFAULT_ALIGNMENT ) | |
4304 | { | |
4305 | const wxSize size = cell->GetSize(); | |
4306 | ||
4307 | if ( size.x >= 0 && size.x < cell_rect.width ) | |
4308 | { | |
4309 | if ( align & wxALIGN_CENTER_HORIZONTAL ) | |
4310 | rectItem.x += (cell_rect.width - size.x)/2; | |
4311 | else if ( align & wxALIGN_RIGHT ) | |
4312 | rectItem.x += cell_rect.width - size.x; | |
4313 | // else: wxALIGN_LEFT is the default | |
4314 | } | |
4315 | ||
4316 | if ( size.y >= 0 && size.y < cell_rect.height ) | |
4317 | { | |
4318 | if ( align & wxALIGN_CENTER_VERTICAL ) | |
4319 | rectItem.y += (cell_rect.height - size.y)/2; | |
4320 | else if ( align & wxALIGN_BOTTOM ) | |
4321 | rectItem.y += cell_rect.height - size.y; | |
4322 | // else: wxALIGN_TOP is the default | |
4323 | } | |
4324 | } | |
4325 | ||
4326 | wxMouseEvent event2(event); | |
4327 | event2.m_x -= rectItem.x; | |
4328 | event2.m_y -= rectItem.y; | |
4329 | m_owner->CalcUnscrolledPosition(event2.m_x, event2.m_y, &event2.m_x, &event2.m_y); | |
4330 | ||
4331 | /* ignore ret */ cell->WXActivateCell | |
4332 | ( | |
4333 | cell_rect, | |
4334 | model, | |
4335 | item, | |
4336 | col->GetModelColumn(), | |
4337 | &event2 | |
4338 | ); | |
4339 | } | |
4340 | } | |
4341 | } | |
4342 | ||
4343 | void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event ) | |
4344 | { | |
4345 | m_hasFocus = true; | |
4346 | ||
4347 | if (HasCurrentRow()) | |
4348 | Refresh(); | |
4349 | ||
4350 | event.Skip(); | |
4351 | } | |
4352 | ||
4353 | void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event ) | |
4354 | { | |
4355 | m_hasFocus = false; | |
4356 | ||
4357 | if (HasCurrentRow()) | |
4358 | Refresh(); | |
4359 | ||
4360 | event.Skip(); | |
4361 | } | |
4362 | ||
4363 | void wxDataViewMainWindow::OnColumnsCountChanged() | |
4364 | { | |
4365 | int editableCount = 0; | |
4366 | ||
4367 | const unsigned cols = GetOwner()->GetColumnCount(); | |
4368 | for ( unsigned i = 0; i < cols; i++ ) | |
4369 | { | |
4370 | wxDataViewColumn *c = GetOwner()->GetColumnAt(i); | |
4371 | if ( c->IsHidden() ) | |
4372 | continue; | |
4373 | if ( c->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT ) | |
4374 | editableCount++; | |
4375 | } | |
4376 | ||
4377 | m_useCellFocus = (editableCount > 1); | |
4378 | ||
4379 | UpdateDisplay(); | |
4380 | } | |
4381 | ||
4382 | //----------------------------------------------------------------------------- | |
4383 | // wxDataViewCtrl | |
4384 | //----------------------------------------------------------------------------- | |
4385 | ||
4386 | WX_DEFINE_LIST(wxDataViewColumnList) | |
4387 | ||
4388 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase) | |
4389 | BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase) | |
4390 | EVT_SIZE(wxDataViewCtrl::OnSize) | |
4391 | END_EVENT_TABLE() | |
4392 | ||
4393 | wxDataViewCtrl::~wxDataViewCtrl() | |
4394 | { | |
4395 | if (m_notifier) | |
4396 | GetModel()->RemoveNotifier( m_notifier ); | |
4397 | ||
4398 | m_cols.Clear(); | |
4399 | m_colsBestWidths.clear(); | |
4400 | } | |
4401 | ||
4402 | void wxDataViewCtrl::Init() | |
4403 | { | |
4404 | m_cols.DeleteContents(true); | |
4405 | m_notifier = NULL; | |
4406 | ||
4407 | // No sorting column at start | |
4408 | m_sortingColumnIdx = wxNOT_FOUND; | |
4409 | ||
4410 | m_headerArea = NULL; | |
4411 | ||
4412 | m_colsDirty = false; | |
4413 | } | |
4414 | ||
4415 | bool wxDataViewCtrl::Create(wxWindow *parent, | |
4416 | wxWindowID id, | |
4417 | const wxPoint& pos, | |
4418 | const wxSize& size, | |
4419 | long style, | |
4420 | const wxValidator& validator, | |
4421 | const wxString& name) | |
4422 | { | |
4423 | // if ( (style & wxBORDER_MASK) == 0) | |
4424 | // style |= wxBORDER_SUNKEN; | |
4425 | ||
4426 | Init(); | |
4427 | ||
4428 | if (!wxControl::Create( parent, id, pos, size, | |
4429 | style | wxScrolledWindowStyle, validator, name)) | |
4430 | return false; | |
4431 | ||
4432 | SetInitialSize(size); | |
4433 | ||
4434 | #ifdef __WXMAC__ | |
4435 | MacSetClipChildren( true ); | |
4436 | #endif | |
4437 | ||
4438 | m_clientArea = new wxDataViewMainWindow( this, wxID_ANY ); | |
4439 | ||
4440 | // We use the cursor keys for moving the selection, not scrolling, so call | |
4441 | // this method to ensure wxScrollHelperEvtHandler doesn't catch all | |
4442 | // keyboard events forwarded to us from wxListMainWindow. | |
4443 | DisableKeyboardScrolling(); | |
4444 | ||
4445 | if (HasFlag(wxDV_NO_HEADER)) | |
4446 | m_headerArea = NULL; | |
4447 | else | |
4448 | m_headerArea = new wxDataViewHeaderWindow(this); | |
4449 | ||
4450 | SetTargetWindow( m_clientArea ); | |
4451 | ||
4452 | wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL ); | |
4453 | if (m_headerArea) | |
4454 | sizer->Add( m_headerArea, 0, wxGROW ); | |
4455 | sizer->Add( m_clientArea, 1, wxGROW ); | |
4456 | SetSizer( sizer ); | |
4457 | ||
4458 | return true; | |
4459 | } | |
4460 | ||
4461 | wxBorder wxDataViewCtrl::GetDefaultBorder() const | |
4462 | { | |
4463 | return wxBORDER_THEME; | |
4464 | } | |
4465 | ||
4466 | #ifdef __WXMSW__ | |
4467 | WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg, | |
4468 | WXWPARAM wParam, | |
4469 | WXLPARAM lParam) | |
4470 | { | |
4471 | WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam); | |
4472 | ||
4473 | #ifndef __WXWINCE__ | |
4474 | // we need to process arrows ourselves for scrolling | |
4475 | if ( nMsg == WM_GETDLGCODE ) | |
4476 | { | |
4477 | rc |= DLGC_WANTARROWS; | |
4478 | } | |
4479 | #endif | |
4480 | ||
4481 | return rc; | |
4482 | } | |
4483 | #endif | |
4484 | ||
4485 | wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size) | |
4486 | { | |
4487 | wxSize newsize = size; | |
4488 | if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea)) | |
4489 | newsize.y -= m_headerArea->GetSize().y; | |
4490 | ||
4491 | return newsize; | |
4492 | } | |
4493 | ||
4494 | void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
4495 | { | |
4496 | // We need to override OnSize so that our scrolled | |
4497 | // window a) does call Layout() to use sizers for | |
4498 | // positioning the controls but b) does not query | |
4499 | // the sizer for their size and use that for setting | |
4500 | // the scrollable area as set that ourselves by | |
4501 | // calling SetScrollbar() further down. | |
4502 | ||
4503 | Layout(); | |
4504 | ||
4505 | AdjustScrollbars(); | |
4506 | ||
4507 | // We must redraw the headers if their height changed. Normally this | |
4508 | // shouldn't happen as the control shouldn't let itself be resized beneath | |
4509 | // its minimal height but avoid the display artefacts that appear if it | |
4510 | // does happen, e.g. because there is really not enough vertical space. | |
4511 | if ( !HasFlag(wxDV_NO_HEADER) && m_headerArea && | |
4512 | m_headerArea->GetSize().y <= m_headerArea->GetBestSize(). y ) | |
4513 | { | |
4514 | m_headerArea->Refresh(); | |
4515 | } | |
4516 | } | |
4517 | ||
4518 | void wxDataViewCtrl::SetFocus() | |
4519 | { | |
4520 | if (m_clientArea) | |
4521 | m_clientArea->SetFocus(); | |
4522 | } | |
4523 | ||
4524 | bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model ) | |
4525 | { | |
4526 | if (!wxDataViewCtrlBase::AssociateModel( model )) | |
4527 | return false; | |
4528 | ||
4529 | m_notifier = new wxGenericDataViewModelNotifier( m_clientArea ); | |
4530 | ||
4531 | model->AddNotifier( m_notifier ); | |
4532 | ||
4533 | m_clientArea->DestroyTree(); | |
4534 | ||
4535 | m_clientArea->BuildTree(model); | |
4536 | ||
4537 | m_clientArea->UpdateDisplay(); | |
4538 | ||
4539 | return true; | |
4540 | } | |
4541 | ||
4542 | #if wxUSE_DRAG_AND_DROP | |
4543 | ||
4544 | bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format ) | |
4545 | { | |
4546 | return m_clientArea->EnableDragSource( format ); | |
4547 | } | |
4548 | ||
4549 | bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format ) | |
4550 | { | |
4551 | return m_clientArea->EnableDropTarget( format ); | |
4552 | } | |
4553 | ||
4554 | #endif // wxUSE_DRAG_AND_DROP | |
4555 | ||
4556 | bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col ) | |
4557 | { | |
4558 | if (!wxDataViewCtrlBase::AppendColumn(col)) | |
4559 | return false; | |
4560 | ||
4561 | m_cols.Append( col ); | |
4562 | m_colsBestWidths.push_back(CachedColWidthInfo()); | |
4563 | OnColumnsCountChanged(); | |
4564 | return true; | |
4565 | } | |
4566 | ||
4567 | bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col ) | |
4568 | { | |
4569 | if (!wxDataViewCtrlBase::PrependColumn(col)) | |
4570 | return false; | |
4571 | ||
4572 | m_cols.Insert( col ); | |
4573 | m_colsBestWidths.insert(m_colsBestWidths.begin(), CachedColWidthInfo()); | |
4574 | OnColumnsCountChanged(); | |
4575 | return true; | |
4576 | } | |
4577 | ||
4578 | bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col ) | |
4579 | { | |
4580 | if (!wxDataViewCtrlBase::InsertColumn(pos,col)) | |
4581 | return false; | |
4582 | ||
4583 | m_cols.Insert( pos, col ); | |
4584 | m_colsBestWidths.insert(m_colsBestWidths.begin() + pos, CachedColWidthInfo()); | |
4585 | OnColumnsCountChanged(); | |
4586 | return true; | |
4587 | } | |
4588 | ||
4589 | void wxDataViewCtrl::OnColumnChange(unsigned int idx) | |
4590 | { | |
4591 | if ( m_headerArea ) | |
4592 | m_headerArea->UpdateColumn(idx); | |
4593 | ||
4594 | m_clientArea->UpdateDisplay(); | |
4595 | } | |
4596 | ||
4597 | void wxDataViewCtrl::OnColumnsCountChanged() | |
4598 | { | |
4599 | if (m_headerArea) | |
4600 | m_headerArea->SetColumnCount(GetColumnCount()); | |
4601 | ||
4602 | m_clientArea->OnColumnsCountChanged(); | |
4603 | } | |
4604 | ||
4605 | void wxDataViewCtrl::DoSetExpanderColumn() | |
4606 | { | |
4607 | m_clientArea->UpdateDisplay(); | |
4608 | } | |
4609 | ||
4610 | void wxDataViewCtrl::DoSetIndent() | |
4611 | { | |
4612 | m_clientArea->UpdateDisplay(); | |
4613 | } | |
4614 | ||
4615 | unsigned int wxDataViewCtrl::GetColumnCount() const | |
4616 | { | |
4617 | return m_cols.GetCount(); | |
4618 | } | |
4619 | ||
4620 | bool wxDataViewCtrl::SetRowHeight( int lineHeight ) | |
4621 | { | |
4622 | if ( !m_clientArea ) | |
4623 | return false; | |
4624 | ||
4625 | m_clientArea->SetRowHeight(lineHeight); | |
4626 | ||
4627 | return true; | |
4628 | } | |
4629 | ||
4630 | wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const | |
4631 | { | |
4632 | return m_cols[idx]; | |
4633 | } | |
4634 | ||
4635 | wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const | |
4636 | { | |
4637 | // columns can't be reordered if there is no header window which allows | |
4638 | // to do this | |
4639 | const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos] | |
4640 | : pos; | |
4641 | ||
4642 | return GetColumn(idx); | |
4643 | } | |
4644 | ||
4645 | int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const | |
4646 | { | |
4647 | const unsigned count = m_cols.size(); | |
4648 | for ( unsigned n = 0; n < count; n++ ) | |
4649 | { | |
4650 | if ( m_cols[n] == column ) | |
4651 | return n; | |
4652 | } | |
4653 | ||
4654 | return wxNOT_FOUND; | |
4655 | } | |
4656 | ||
4657 | unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx) const | |
4658 | { | |
4659 | if ( m_colsBestWidths[idx].width != 0 ) | |
4660 | return m_colsBestWidths[idx].width; | |
4661 | ||
4662 | const int count = m_clientArea->GetRowCount(); | |
4663 | wxDataViewColumn *column = GetColumn(idx); | |
4664 | wxDataViewRenderer *renderer = | |
4665 | const_cast<wxDataViewRenderer*>(column->GetRenderer()); | |
4666 | ||
4667 | class MaxWidthCalculator | |
4668 | { | |
4669 | public: | |
4670 | MaxWidthCalculator(const wxDataViewCtrl *dvc, | |
4671 | wxDataViewMainWindow *clientArea, | |
4672 | wxDataViewRenderer *renderer, | |
4673 | const wxDataViewModel *model, | |
4674 | unsigned column, | |
4675 | int expanderSize) | |
4676 | : m_width(0), | |
4677 | m_dvc(dvc), | |
4678 | m_clientArea(clientArea), | |
4679 | m_renderer(renderer), | |
4680 | m_model(model), | |
4681 | m_column(column), | |
4682 | m_expanderSize(expanderSize) | |
4683 | ||
4684 | { | |
4685 | m_isExpanderCol = | |
4686 | !clientArea->IsList() && | |
4687 | (column == 0 || | |
4688 | GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl*>(dvc)) == dvc->GetColumnAt(column)); | |
4689 | } | |
4690 | ||
4691 | void UpdateWithWidth(int width) | |
4692 | { | |
4693 | m_width = wxMax(m_width, width); | |
4694 | } | |
4695 | ||
4696 | void UpdateWithRow(int row) | |
4697 | { | |
4698 | int indent = 0; | |
4699 | wxDataViewItem item; | |
4700 | ||
4701 | if ( m_isExpanderCol ) | |
4702 | { | |
4703 | wxDataViewTreeNode *node = m_clientArea->GetTreeNodeByRow(row); | |
4704 | item = node->GetItem(); | |
4705 | indent = m_dvc->GetIndent() * node->GetIndentLevel() + m_expanderSize; | |
4706 | } | |
4707 | else | |
4708 | { | |
4709 | item = m_clientArea->GetItemByRow(row); | |
4710 | } | |
4711 | ||
4712 | m_renderer->PrepareForItem(m_model, item, m_column); | |
4713 | m_width = wxMax(m_width, m_renderer->GetSize().x + indent); | |
4714 | } | |
4715 | ||
4716 | int GetMaxWidth() const { return m_width; } | |
4717 | ||
4718 | private: | |
4719 | int m_width; | |
4720 | const wxDataViewCtrl *m_dvc; | |
4721 | wxDataViewMainWindow *m_clientArea; | |
4722 | wxDataViewRenderer *m_renderer; | |
4723 | const wxDataViewModel *m_model; | |
4724 | unsigned m_column; | |
4725 | bool m_isExpanderCol; | |
4726 | int m_expanderSize; | |
4727 | }; | |
4728 | ||
4729 | MaxWidthCalculator calculator(this, m_clientArea, renderer, | |
4730 | GetModel(), column->GetModelColumn(), | |
4731 | m_clientArea->GetRowHeight()); | |
4732 | ||
4733 | calculator.UpdateWithWidth(column->GetMinWidth()); | |
4734 | ||
4735 | if ( m_headerArea ) | |
4736 | calculator.UpdateWithWidth(m_headerArea->GetColumnTitleWidth(*column)); | |
4737 | ||
4738 | // The code below deserves some explanation. For very large controls, we | |
4739 | // simply can't afford to calculate sizes for all items, it takes too | |
4740 | // long. So the best we can do is to check the first and the last N/2 | |
4741 | // items in the control for some sufficiently large N and calculate best | |
4742 | // sizes from that. That can result in the calculated best width being too | |
4743 | // small for some outliers, but it's better to get slightly imperfect | |
4744 | // result than to wait several seconds after every update. To avoid highly | |
4745 | // visible miscalculations, we also include all currently visible items | |
4746 | // no matter what. Finally, the value of N is determined dynamically by | |
4747 | // measuring how much time we spent on the determining item widths so far. | |
4748 | ||
4749 | #if wxUSE_STOPWATCH | |
4750 | int top_part_end = count; | |
4751 | static const long CALC_TIMEOUT = 20/*ms*/; | |
4752 | // don't call wxStopWatch::Time() too often | |
4753 | static const unsigned CALC_CHECK_FREQ = 100; | |
4754 | wxStopWatch timer; | |
4755 | #else | |
4756 | // use some hard-coded limit, that's the best we can do without timer | |
4757 | int top_part_end = wxMin(500, count); | |
4758 | #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH | |
4759 | ||
4760 | int row = 0; | |
4761 | ||
4762 | for ( row = 0; row < top_part_end; row++ ) | |
4763 | { | |
4764 | #if wxUSE_STOPWATCH | |
4765 | if ( row % CALC_CHECK_FREQ == CALC_CHECK_FREQ-1 && | |
4766 | timer.Time() > CALC_TIMEOUT ) | |
4767 | break; | |
4768 | #endif // wxUSE_STOPWATCH | |
4769 | calculator.UpdateWithRow(row); | |
4770 | } | |
4771 | ||
4772 | // row is the first unmeasured item now; that's our value of N/2 | |
4773 | ||
4774 | if ( row < count ) | |
4775 | { | |
4776 | top_part_end = row; | |
4777 | ||
4778 | // add bottom N/2 items now: | |
4779 | const int bottom_part_start = wxMax(row, count - row); | |
4780 | for ( row = bottom_part_start; row < count; row++ ) | |
4781 | { | |
4782 | calculator.UpdateWithRow(row); | |
4783 | } | |
4784 | ||
4785 | // finally, include currently visible items in the calculation: | |
4786 | const wxPoint origin = CalcUnscrolledPosition(wxPoint(0, 0)); | |
4787 | int first_visible = m_clientArea->GetLineAt(origin.y); | |
4788 | int last_visible = m_clientArea->GetLineAt(origin.y + GetClientSize().y); | |
4789 | ||
4790 | first_visible = wxMax(first_visible, top_part_end); | |
4791 | last_visible = wxMin(bottom_part_start, last_visible); | |
4792 | ||
4793 | for ( row = first_visible; row < last_visible; row++ ) | |
4794 | { | |
4795 | calculator.UpdateWithRow(row); | |
4796 | } | |
4797 | ||
4798 | wxLogTrace("dataview", | |
4799 | "determined best size from %d top, %d bottom plus %d more visible items out of %d total", | |
4800 | top_part_end, | |
4801 | count - bottom_part_start, | |
4802 | wxMax(0, last_visible - first_visible), | |
4803 | count); | |
4804 | } | |
4805 | ||
4806 | int max_width = calculator.GetMaxWidth(); | |
4807 | if ( max_width > 0 ) | |
4808 | max_width += 2 * PADDING_RIGHTLEFT; | |
4809 | ||
4810 | const_cast<wxDataViewCtrl*>(this)->m_colsBestWidths[idx].width = max_width; | |
4811 | return max_width; | |
4812 | } | |
4813 | ||
4814 | void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col), | |
4815 | unsigned int WXUNUSED(new_pos)) | |
4816 | { | |
4817 | // do _not_ reorder m_cols elements here, they should always be in the | |
4818 | // order in which columns were added, we only display the columns in | |
4819 | // different order | |
4820 | m_clientArea->UpdateDisplay(); | |
4821 | } | |
4822 | ||
4823 | bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column ) | |
4824 | { | |
4825 | wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column ); | |
4826 | if (!ret) | |
4827 | return false; | |
4828 | ||
4829 | m_colsBestWidths.erase(m_colsBestWidths.begin() + GetColumnIndex(column)); | |
4830 | m_cols.Erase(ret); | |
4831 | ||
4832 | if ( m_clientArea->GetCurrentColumn() == column ) | |
4833 | m_clientArea->ClearCurrentColumn(); | |
4834 | ||
4835 | OnColumnsCountChanged(); | |
4836 | ||
4837 | return true; | |
4838 | } | |
4839 | ||
4840 | bool wxDataViewCtrl::ClearColumns() | |
4841 | { | |
4842 | SetExpanderColumn(NULL); | |
4843 | m_cols.Clear(); | |
4844 | m_colsBestWidths.clear(); | |
4845 | ||
4846 | m_clientArea->ClearCurrentColumn(); | |
4847 | ||
4848 | OnColumnsCountChanged(); | |
4849 | ||
4850 | return true; | |
4851 | } | |
4852 | ||
4853 | void wxDataViewCtrl::InvalidateColBestWidth(int idx) | |
4854 | { | |
4855 | m_colsBestWidths[idx].width = 0; | |
4856 | m_colsBestWidths[idx].dirty = true; | |
4857 | m_colsDirty = true; | |
4858 | } | |
4859 | ||
4860 | void wxDataViewCtrl::InvalidateColBestWidths() | |
4861 | { | |
4862 | // mark all columns as dirty: | |
4863 | m_colsBestWidths.clear(); | |
4864 | m_colsBestWidths.resize(m_cols.size()); | |
4865 | m_colsDirty = true; | |
4866 | } | |
4867 | ||
4868 | void wxDataViewCtrl::UpdateColWidths() | |
4869 | { | |
4870 | m_colsDirty = false; | |
4871 | ||
4872 | if ( !m_headerArea ) | |
4873 | return; | |
4874 | ||
4875 | const unsigned len = m_colsBestWidths.size(); | |
4876 | for ( unsigned i = 0; i < len; i++ ) | |
4877 | { | |
4878 | // Note that we have to have an explicit 'dirty' flag here instead of | |
4879 | // checking if the width==0, as is done in GetBestColumnWidth(). | |
4880 | // | |
4881 | // Testing width==0 wouldn't work correctly if some code called | |
4882 | // GetWidth() after col. width invalidation but before | |
4883 | // wxDataViewCtrl::UpdateColWidths() was called at idle time. This | |
4884 | // would result in the header's column width getting out of sync with | |
4885 | // the control itself. | |
4886 | if ( m_colsBestWidths[i].dirty ) | |
4887 | { | |
4888 | m_headerArea->UpdateColumn(i); | |
4889 | m_colsBestWidths[i].dirty = false; | |
4890 | } | |
4891 | } | |
4892 | } | |
4893 | ||
4894 | void wxDataViewCtrl::OnInternalIdle() | |
4895 | { | |
4896 | wxDataViewCtrlBase::OnInternalIdle(); | |
4897 | ||
4898 | if ( m_colsDirty ) | |
4899 | UpdateColWidths(); | |
4900 | } | |
4901 | ||
4902 | int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const | |
4903 | { | |
4904 | unsigned int len = GetColumnCount(); | |
4905 | for ( unsigned int i = 0; i < len; i++ ) | |
4906 | { | |
4907 | wxDataViewColumn * col = GetColumnAt(i); | |
4908 | if (column==col) | |
4909 | return i; | |
4910 | } | |
4911 | ||
4912 | return wxNOT_FOUND; | |
4913 | } | |
4914 | ||
4915 | wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const | |
4916 | { | |
4917 | return m_sortingColumnIdx == wxNOT_FOUND ? NULL | |
4918 | : GetColumn(m_sortingColumnIdx); | |
4919 | } | |
4920 | ||
4921 | wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const | |
4922 | { | |
4923 | return GetItemByRow(m_clientArea->GetCurrentRow()); | |
4924 | } | |
4925 | ||
4926 | void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item) | |
4927 | { | |
4928 | const int row = m_clientArea->GetRowByItem(item); | |
4929 | ||
4930 | const unsigned oldCurrent = m_clientArea->GetCurrentRow(); | |
4931 | if ( static_cast<unsigned>(row) != oldCurrent ) | |
4932 | { | |
4933 | m_clientArea->ChangeCurrentRow(row); | |
4934 | m_clientArea->RefreshRow(oldCurrent); | |
4935 | m_clientArea->RefreshRow(row); | |
4936 | } | |
4937 | } | |
4938 | ||
4939 | wxDataViewColumn *wxDataViewCtrl::GetCurrentColumn() const | |
4940 | { | |
4941 | return m_clientArea->GetCurrentColumn(); | |
4942 | } | |
4943 | ||
4944 | int wxDataViewCtrl::GetSelectedItemsCount() const | |
4945 | { | |
4946 | return m_clientArea->GetSelections().size(); | |
4947 | } | |
4948 | ||
4949 | int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const | |
4950 | { | |
4951 | sel.Empty(); | |
4952 | const wxDataViewSelection& selections = m_clientArea->GetSelections(); | |
4953 | ||
4954 | const size_t len = selections.size(); | |
4955 | for ( size_t i = 0; i < len; i++ ) | |
4956 | { | |
4957 | wxDataViewItem item = m_clientArea->GetItemByRow(selections[i]); | |
4958 | if ( item.IsOk() ) | |
4959 | { | |
4960 | sel.Add(item); | |
4961 | } | |
4962 | else | |
4963 | { | |
4964 | wxFAIL_MSG( "invalid item in selection - bad internal state" ); | |
4965 | } | |
4966 | } | |
4967 | ||
4968 | return sel.size(); | |
4969 | } | |
4970 | ||
4971 | void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel ) | |
4972 | { | |
4973 | wxDataViewSelection selection(wxDataViewSelectionCmp); | |
4974 | ||
4975 | wxDataViewItem last_parent; | |
4976 | ||
4977 | int len = sel.GetCount(); | |
4978 | for( int i = 0; i < len; i ++ ) | |
4979 | { | |
4980 | wxDataViewItem item = sel[i]; | |
4981 | wxDataViewItem parent = GetModel()->GetParent( item ); | |
4982 | if (parent) | |
4983 | { | |
4984 | if (parent != last_parent) | |
4985 | ExpandAncestors(item); | |
4986 | } | |
4987 | ||
4988 | last_parent = parent; | |
4989 | int row = m_clientArea->GetRowByItem( item ); | |
4990 | if( row >= 0 ) | |
4991 | selection.Add( static_cast<unsigned int>(row) ); | |
4992 | } | |
4993 | ||
4994 | m_clientArea->SetSelections( selection ); | |
4995 | } | |
4996 | ||
4997 | void wxDataViewCtrl::Select( const wxDataViewItem & item ) | |
4998 | { | |
4999 | ExpandAncestors( item ); | |
5000 | ||
5001 | int row = m_clientArea->GetRowByItem( item ); | |
5002 | if( row >= 0 ) | |
5003 | { | |
5004 | // Unselect all rows before select another in the single select mode | |
5005 | if (m_clientArea->IsSingleSel()) | |
5006 | m_clientArea->SelectAllRows(false); | |
5007 | ||
5008 | m_clientArea->SelectRow(row, true); | |
5009 | ||
5010 | // Also set focus to the selected item | |
5011 | m_clientArea->ChangeCurrentRow( row ); | |
5012 | } | |
5013 | } | |
5014 | ||
5015 | void wxDataViewCtrl::Unselect( const wxDataViewItem & item ) | |
5016 | { | |
5017 | int row = m_clientArea->GetRowByItem( item ); | |
5018 | if( row >= 0 ) | |
5019 | m_clientArea->SelectRow(row, false); | |
5020 | } | |
5021 | ||
5022 | bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const | |
5023 | { | |
5024 | int row = m_clientArea->GetRowByItem( item ); | |
5025 | if( row >= 0 ) | |
5026 | { | |
5027 | return m_clientArea->IsRowSelected(row); | |
5028 | } | |
5029 | return false; | |
5030 | } | |
5031 | ||
5032 | void wxDataViewCtrl::SetAlternateRowColour(const wxColour& colour) | |
5033 | { | |
5034 | m_alternateRowColour = colour; | |
5035 | } | |
5036 | ||
5037 | void wxDataViewCtrl::SelectAll() | |
5038 | { | |
5039 | m_clientArea->SelectAllRows(true); | |
5040 | } | |
5041 | ||
5042 | void wxDataViewCtrl::UnselectAll() | |
5043 | { | |
5044 | m_clientArea->SelectAllRows(false); | |
5045 | } | |
5046 | ||
5047 | void wxDataViewCtrl::EnsureVisible( int row, int column ) | |
5048 | { | |
5049 | if( row < 0 ) | |
5050 | row = 0; | |
5051 | if( row > (int) m_clientArea->GetRowCount() ) | |
5052 | row = m_clientArea->GetRowCount(); | |
5053 | ||
5054 | int first = m_clientArea->GetFirstVisibleRow(); | |
5055 | int last = m_clientArea->GetLastVisibleRow(); | |
5056 | if( row < first ) | |
5057 | m_clientArea->ScrollTo( row, column ); | |
5058 | else if( row > last ) | |
5059 | m_clientArea->ScrollTo( row - last + first, column ); | |
5060 | else | |
5061 | m_clientArea->ScrollTo( first, column ); | |
5062 | } | |
5063 | ||
5064 | void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column ) | |
5065 | { | |
5066 | ExpandAncestors( item ); | |
5067 | ||
5068 | m_clientArea->RecalculateDisplay(); | |
5069 | ||
5070 | int row = m_clientArea->GetRowByItem(item); | |
5071 | if( row >= 0 ) | |
5072 | { | |
5073 | if( column == NULL ) | |
5074 | EnsureVisible(row, -1); | |
5075 | else | |
5076 | EnsureVisible( row, GetColumnIndex(column) ); | |
5077 | } | |
5078 | ||
5079 | } | |
5080 | ||
5081 | void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item, | |
5082 | wxDataViewColumn* &column ) const | |
5083 | { | |
5084 | m_clientArea->HitTest(point, item, column); | |
5085 | } | |
5086 | ||
5087 | wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item, | |
5088 | const wxDataViewColumn* column ) const | |
5089 | { | |
5090 | return m_clientArea->GetItemRect(item, column); | |
5091 | } | |
5092 | ||
5093 | wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const | |
5094 | { | |
5095 | return m_clientArea->GetItemByRow( row ); | |
5096 | } | |
5097 | ||
5098 | int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const | |
5099 | { | |
5100 | return m_clientArea->GetRowByItem( item ); | |
5101 | } | |
5102 | ||
5103 | void wxDataViewCtrl::Expand( const wxDataViewItem & item ) | |
5104 | { | |
5105 | ExpandAncestors( item ); | |
5106 | ||
5107 | int row = m_clientArea->GetRowByItem( item ); | |
5108 | if (row != -1) | |
5109 | { | |
5110 | m_clientArea->Expand(row); | |
5111 | InvalidateColBestWidths(); | |
5112 | } | |
5113 | } | |
5114 | ||
5115 | void wxDataViewCtrl::Collapse( const wxDataViewItem & item ) | |
5116 | { | |
5117 | int row = m_clientArea->GetRowByItem( item ); | |
5118 | if (row != -1) | |
5119 | { | |
5120 | m_clientArea->Collapse(row); | |
5121 | InvalidateColBestWidths(); | |
5122 | } | |
5123 | } | |
5124 | ||
5125 | bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const | |
5126 | { | |
5127 | int row = m_clientArea->GetRowByItem( item ); | |
5128 | if (row != -1) | |
5129 | return m_clientArea->IsExpanded(row); | |
5130 | return false; | |
5131 | } | |
5132 | ||
5133 | void wxDataViewCtrl::EditItem(const wxDataViewItem& item, const wxDataViewColumn *column) | |
5134 | { | |
5135 | wxCHECK_RET( item.IsOk(), "invalid item" ); | |
5136 | wxCHECK_RET( column, "no column provided" ); | |
5137 | ||
5138 | m_clientArea->StartEditing(item, column); | |
5139 | } | |
5140 | ||
5141 | #endif // !wxUSE_GENERICDATAVIEWCTRL | |
5142 | ||
5143 | #endif // wxUSE_DATAVIEWCTRL |