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