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