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