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