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