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