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