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