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