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