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