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