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