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