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