]> git.saurik.com Git - wxWidgets.git/blame - src/generic/datavgen.cpp
Implemented text background colour
[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"
f554a14b
WS
36#endif
37
4ed7af08 38#include "wx/stockitem.h"
4ed7af08
RR
39#include "wx/calctrl.h"
40#include "wx/popupwin.h"
4ed7af08 41#include "wx/renderer.h"
2e992e06 42#include "wx/dcbuffer.h"
2586d4a1 43#include "wx/icon.h"
4ed7af08 44
4ed7af08
RR
45//-----------------------------------------------------------------------------
46// classes
47//-----------------------------------------------------------------------------
48
49class wxDataViewCtrl;
50
9861f022
RR
51static const int SCROLL_UNIT_X = 15;
52
53// the cell padding on the left/right
54static const int PADDING_RIGHTLEFT = 3;
55
56// the cell padding on the top/bottom
57static const int PADDING_TOPBOTTOM = 1;
58
59
a0f3af5f
RR
60//-----------------------------------------------------------------------------
61// wxDataViewHeaderWindow
62//-----------------------------------------------------------------------------
4ed7af08 63
87f0efe2
RR
64#define USE_NATIVE_HEADER_WINDOW 1
65
66class wxDataViewHeaderWindowBase : public wxControl
67{
68public:
69 wxDataViewHeaderWindowBase()
70 { m_owner = NULL; }
71
72 bool Create(wxDataViewCtrl *parent, wxWindowID id,
73 const wxPoint &pos, const wxSize &size,
74 const wxString &name)
75 {
76 return wxWindow::Create(parent, id, pos, size, wxNO_BORDER, name);
77 }
78
79 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
80 wxDataViewCtrl *GetOwner() { return m_owner; }
81
82 // called on column addition/removal
83 virtual void UpdateDisplay() { /* by default, do nothing */ }
84
87f0efe2 85 // returns the n-th column
9861f022 86 virtual wxDataViewColumn *GetColumn(unsigned int n)
87f0efe2
RR
87 {
88 wxASSERT(m_owner);
89 wxDataViewColumn *ret = m_owner->GetColumn(n);
90 wxASSERT(ret);
91
92 return ret;
93 }
94
95protected:
96 wxDataViewCtrl *m_owner;
97
98 // sends an event generated from the n-th wxDataViewColumn
99 void SendEvent(wxEventType type, unsigned int n);
100};
101
102// on wxMSW the header window (only that part however) can be made native!
103#if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
104
9861f022 105#define COLUMN_WIDTH_OFFSET 2
87f0efe2
RR
106#define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
107
108class wxDataViewHeaderWindowMSW : public wxDataViewHeaderWindowBase
4ed7af08
RR
109{
110public:
87f0efe2
RR
111
112 wxDataViewHeaderWindowMSW( wxDataViewCtrl *parent,
9861f022
RR
113 wxWindowID id,
114 const wxPoint &pos = wxDefaultPosition,
115 const wxSize &size = wxDefaultSize,
116 const wxString &name = wxT("wxdataviewctrlheaderwindow") )
87f0efe2
RR
117 {
118 Create(parent, id, pos, size, name);
119 }
4ed7af08 120
87f0efe2
RR
121 bool Create(wxDataViewCtrl *parent, wxWindowID id,
122 const wxPoint &pos, const wxSize &size,
123 const wxString &name);
124
125 ~wxDataViewHeaderWindowMSW();
126
9861f022
RR
127 // called when any column setting is changed and/or changed
128 // the column count
87f0efe2
RR
129 virtual void UpdateDisplay();
130
131 // called when the main window gets scrolled
132 virtual void ScrollWindow(int dx, int dy, const wxRect *rect = NULL);
133
134protected:
135 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
9861f022
RR
136 virtual void DoSetSize(int x, int y, int width, int height, int sizeFlags);
137
138 unsigned int GetColumnIdxFromHeader(NMHEADER *nmHDR);
139
140 wxDataViewColumn *GetColumnFromHeader(NMHEADER *nmHDR)
141 { return GetColumn(GetColumnIdxFromHeader(nmHDR)); }
142
87f0efe2
RR
143private:
144 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW)
145};
146
147#else // !defined(__WXMSW__)
148
149#define HEADER_WINDOW_HEIGHT 25
9861f022
RR
150#define HEADER_HORIZ_BORDER 5
151#define HEADER_VERT_BORDER 3
87f0efe2
RR
152#define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
153
154class wxGenericDataViewHeaderWindow : public wxDataViewHeaderWindowBase
155{
156public:
157 wxGenericDataViewHeaderWindow( wxDataViewCtrl *parent,
158 wxWindowID id,
159 const wxPoint &pos = wxDefaultPosition,
160 const wxSize &size = wxDefaultSize,
161 const wxString &name = wxT("wxdataviewctrlheaderwindow") )
162 {
163 Init();
164 Create(parent, id, pos, size, name);
165 }
166
167 bool Create(wxDataViewCtrl *parent, wxWindowID id,
168 const wxPoint &pos, const wxSize &size,
169 const wxString &name);
170
171 ~wxGenericDataViewHeaderWindow()
172 {
173 delete m_resizeCursor;
174 }
175
176 // event handlers:
4ed7af08 177
a0f3af5f
RR
178 void OnPaint( wxPaintEvent &event );
179 void OnMouse( wxMouseEvent &event );
180 void OnSetFocus( wxFocusEvent &event );
f554a14b 181
87f0efe2
RR
182
183protected:
184
185 // vars used for column resizing:
186
a0f3af5f 187 wxCursor *m_resizeCursor;
87f0efe2
RR
188 const wxCursor *m_currentCursor;
189 bool m_isDragging;
190
191 bool m_dirty; // needs refresh?
192 int m_column; // index of the column being resized
193 int m_currentX; // divider line position in logical (unscrolled) coords
194 int m_minX; // minimal position beyond which the divider line
195 // can't be dragged in logical coords
196
9861f022
RR
197 // the pen used to draw the current column width drag line
198 // when resizing the columsn
199 wxPen m_penCurrent;
200
201
87f0efe2
RR
202 // internal utilities:
203
204 void Init()
205 {
206 m_currentCursor = (wxCursor *) NULL;
207 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
208
209 m_isDragging = false;
210 m_dirty = false;
211
212 m_column = wxNOT_FOUND;
213 m_currentX = 0;
214 m_minX = 0;
9861f022
RR
215
216 wxColour col = wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
217 m_penCurrent = wxPen(col, 1, wxSOLID);
87f0efe2
RR
218 }
219
220 void DrawCurrent();
221 void AdjustDC(wxDC& dc);
f554a14b 222
a0f3af5f 223private:
87f0efe2 224 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow)
a0f3af5f
RR
225 DECLARE_EVENT_TABLE()
226};
4ed7af08 227
87f0efe2
RR
228#endif // defined(__WXMSW__)
229
0fcce6b9
RR
230//-----------------------------------------------------------------------------
231// wxDataViewRenameTimer
232//-----------------------------------------------------------------------------
233
234class wxDataViewRenameTimer: public wxTimer
235{
236private:
237 wxDataViewMainWindow *m_owner;
238
239public:
240 wxDataViewRenameTimer( wxDataViewMainWindow *owner );
241 void Notify();
242};
243
244//-----------------------------------------------------------------------------
245// wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
246//-----------------------------------------------------------------------------
247
248class wxDataViewTextCtrlWrapper : public wxEvtHandler
249{
250public:
251 // NB: text must be a valid object but not Create()d yet
252 wxDataViewTextCtrlWrapper( wxDataViewMainWindow *owner,
253 wxTextCtrl *text,
254 wxDataViewListModel *model,
0a71f9e9 255 unsigned int col, unsigned int row,
0fcce6b9
RR
256 wxRect cellLabel );
257
258 wxTextCtrl *GetText() const { return m_text; }
259
260 void AcceptChangesAndFinish();
261
262protected:
263 void OnChar( wxKeyEvent &event );
264 void OnKeyUp( wxKeyEvent &event );
265 void OnKillFocus( wxFocusEvent &event );
266
267 bool AcceptChanges();
268 void Finish();
269
270private:
271 wxDataViewMainWindow *m_owner;
272 wxTextCtrl *m_text;
273 wxString m_startValue;
274 wxDataViewListModel *m_model;
0a71f9e9
RR
275 unsigned int m_col;
276 unsigned int m_row;
0fcce6b9
RR
277 bool m_finished;
278 bool m_aboutToFinish;
279
280 DECLARE_EVENT_TABLE()
281};
282
a0f3af5f
RR
283//-----------------------------------------------------------------------------
284// wxDataViewMainWindow
285//-----------------------------------------------------------------------------
4ed7af08 286
87f0efe2
RR
287WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection,
288 WXDLLIMPEXP_ADV);
cab07038 289
a0f3af5f 290class wxDataViewMainWindow: public wxWindow
4ed7af08 291{
a0f3af5f
RR
292public:
293 wxDataViewMainWindow( wxDataViewCtrl *parent,
294 wxWindowID id,
295 const wxPoint &pos = wxDefaultPosition,
296 const wxSize &size = wxDefaultSize,
297 const wxString &name = wxT("wxdataviewctrlmainwindow") );
d3c7fc99 298 virtual ~wxDataViewMainWindow();
4ed7af08 299
a0f3af5f
RR
300 // notifications from wxDataViewListModel
301 bool RowAppended();
302 bool RowPrepended();
0a71f9e9
RR
303 bool RowInserted( unsigned int before );
304 bool RowDeleted( unsigned int row );
305 bool RowChanged( unsigned int row );
306 bool ValueChanged( unsigned int col, unsigned int row );
307 bool RowsReordered( unsigned int *new_order );
a0f3af5f 308 bool Cleared();
4ed7af08 309
a0f3af5f
RR
310 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
311 wxDataViewCtrl *GetOwner() { return m_owner; }
9861f022 312 const wxDataViewCtrl *GetOwner() const { return m_owner; }
4ed7af08 313
a0f3af5f 314 void OnPaint( wxPaintEvent &event );
0a71f9e9 315 void OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event);
cab07038 316 void OnChar( wxKeyEvent &event );
a0f3af5f
RR
317 void OnMouse( wxMouseEvent &event );
318 void OnSetFocus( wxFocusEvent &event );
cab07038 319 void OnKillFocus( wxFocusEvent &event );
f554a14b 320
a0f3af5f
RR
321 void UpdateDisplay();
322 void RecalculateDisplay();
323 void OnInternalIdle();
f554a14b 324
0fcce6b9
RR
325 void OnRenameTimer();
326 void FinishEditing( wxTextCtrl *text );
327
9861f022 328 void ScrollWindow( int dx, int dy, const wxRect *rect = NULL );
120b9b05 329
0a71f9e9
RR
330 bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; }
331 void ChangeCurrentRow( unsigned int row );
120b9b05 332
cab07038
RR
333 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); };
334 bool IsEmpty() { return GetRowCount() == 0; }
120b9b05 335
9861f022
RR
336 int GetCountPerPage() const;
337 int GetEndOfLastCol() const;
338 unsigned int GetFirstVisibleRow() const;
339 unsigned int GetLastVisibleRow() const;
340 unsigned int GetRowCount() const;
120b9b05 341
87f0efe2 342 void Select( const wxArrayInt& aSelections );
cab07038 343 void SelectAllRows( bool on );
0a71f9e9
RR
344 void SelectRow( unsigned int row, bool on );
345 void SelectRows( unsigned int from, unsigned int to, bool on );
346 void ReverseRowSelection( unsigned int row );
347 bool IsRowSelected( unsigned int row );
120b9b05 348
0a71f9e9
RR
349 void RefreshRow( unsigned int row );
350 void RefreshRows( unsigned int from, unsigned int to );
351 void RefreshRowsAfter( unsigned int firstRow );
120b9b05 352
9861f022
RR
353 // returns the colour to be used for drawing the rules
354 wxColour GetRuleColour() const
355 {
356 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
357 }
358
359 //void EnsureVisible( unsigned int row );
360 wxRect GetLineRect( unsigned int row ) const;
361
a0f3af5f 362private:
0fcce6b9
RR
363 wxDataViewCtrl *m_owner;
364 int m_lineHeight;
365 bool m_dirty;
120b9b05 366
0fcce6b9 367 wxDataViewColumn *m_currentCol;
0a71f9e9 368 unsigned int m_currentRow;
cab07038 369 wxDataViewSelection m_selection;
120b9b05 370
0fcce6b9
RR
371 wxDataViewRenameTimer *m_renameTimer;
372 wxDataViewTextCtrlWrapper *m_textctrlWrapper;
373 bool m_lastOnSame;
f554a14b 374
cab07038
RR
375 bool m_hasFocus;
376
e21f75bd
RR
377 int m_dragCount;
378 wxPoint m_dragStart;
379
380 // for double click logic
0a71f9e9 381 unsigned int m_lineLastClicked,
e21f75bd
RR
382 m_lineBeforeLastClicked,
383 m_lineSelectSingleOnUp;
cab07038 384
9861f022
RR
385 // the pen used to draw horiz/vertical rules
386 wxPen m_penRule;
387
a0f3af5f
RR
388private:
389 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow)
390 DECLARE_EVENT_TABLE()
391};
4ed7af08 392
f554a14b 393// ---------------------------------------------------------
a0f3af5f 394// wxGenericDataViewListModelNotifier
f554a14b 395// ---------------------------------------------------------
a0f3af5f
RR
396
397class wxGenericDataViewListModelNotifier: public wxDataViewListModelNotifier
4ed7af08 398{
a0f3af5f
RR
399public:
400 wxGenericDataViewListModelNotifier( wxDataViewMainWindow *mainWindow )
401 { m_mainWindow = mainWindow; }
f554a14b 402
a0f3af5f
RR
403 virtual bool RowAppended()
404 { return m_mainWindow->RowAppended(); }
405 virtual bool RowPrepended()
406 { return m_mainWindow->RowPrepended(); }
0a71f9e9 407 virtual bool RowInserted( unsigned int before )
a0f3af5f 408 { return m_mainWindow->RowInserted( before ); }
0a71f9e9 409 virtual bool RowDeleted( unsigned int row )
a0f3af5f 410 { return m_mainWindow->RowDeleted( row ); }
0a71f9e9 411 virtual bool RowChanged( unsigned int row )
a0f3af5f 412 { return m_mainWindow->RowChanged( row ); }
0a71f9e9 413 virtual bool ValueChanged( unsigned int col, unsigned int row )
a0f3af5f 414 { return m_mainWindow->ValueChanged( col, row ); }
0a71f9e9 415 virtual bool RowsReordered( unsigned int *new_order )
a0f3af5f
RR
416 { return m_mainWindow->RowsReordered( new_order ); }
417 virtual bool Cleared()
418 { return m_mainWindow->Cleared(); }
f554a14b 419
a0f3af5f
RR
420 wxDataViewMainWindow *m_mainWindow;
421};
4ed7af08 422
f554a14b 423// ---------------------------------------------------------
baa9ebc4 424// wxDataViewRenderer
f554a14b 425// ---------------------------------------------------------
4ed7af08 426
baa9ebc4 427IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
4ed7af08 428
87f0efe2 429wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype,
9861f022
RR
430 wxDataViewCellMode mode,
431 int align) :
432 wxDataViewRendererBase( varianttype, mode, align )
4ed7af08 433{
3d9d7cc4 434 m_dc = NULL;
9861f022
RR
435 m_align = align;
436 m_mode = mode;
4ed7af08
RR
437}
438
baa9ebc4 439wxDataViewRenderer::~wxDataViewRenderer()
3d9d7cc4
RR
440{
441 if (m_dc)
442 delete m_dc;
443}
444
baa9ebc4 445wxDC *wxDataViewRenderer::GetDC()
3d9d7cc4
RR
446{
447 if (m_dc == NULL)
448 {
449 if (GetOwner() == NULL)
450 return NULL;
451 if (GetOwner()->GetOwner() == NULL)
452 return NULL;
453 m_dc = new wxClientDC( GetOwner()->GetOwner() );
454 }
f554a14b 455
3d9d7cc4
RR
456 return m_dc;
457}
458
9861f022 459
f554a14b 460// ---------------------------------------------------------
baa9ebc4 461// wxDataViewCustomRenderer
f554a14b 462// ---------------------------------------------------------
3d9d7cc4 463
baa9ebc4 464IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
3d9d7cc4 465
baa9ebc4 466wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
9861f022
RR
467 wxDataViewCellMode mode, int align ) :
468 wxDataViewRenderer( varianttype, mode, align )
3d9d7cc4
RR
469{
470}
471
f554a14b 472// ---------------------------------------------------------
baa9ebc4 473// wxDataViewTextRenderer
f554a14b 474// ---------------------------------------------------------
4ed7af08 475
baa9ebc4 476IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewCustomRenderer)
4ed7af08 477
87f0efe2 478wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype,
9861f022
RR
479 wxDataViewCellMode mode, int align ) :
480 wxDataViewCustomRenderer( varianttype, mode, align )
4ed7af08
RR
481{
482}
483
baa9ebc4 484bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
4ed7af08 485{
90675b95 486 m_text = value.GetString();
f554a14b 487
90675b95 488 return true;
4ed7af08
RR
489}
490
9861f022 491bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
4ed7af08
RR
492{
493 return false;
494}
495
87f0efe2 496bool wxDataViewTextRenderer::Render( wxRect cell, wxDC *dc, int state )
3d9d7cc4 497{
87f0efe2
RR
498 wxDataViewCtrl *view = GetOwner()->GetOwner();
499 wxColour col = (state & wxDATAVIEW_CELL_SELECTED) ?
500 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) :
501 view->GetForegroundColour();
502
503 dc->SetTextForeground(col);
90675b95
RR
504 dc->DrawText( m_text, cell.x, cell.y );
505
506 return true;
3d9d7cc4
RR
507}
508
9861f022 509wxSize wxDataViewTextRenderer::GetSize() const
3d9d7cc4 510{
9861f022 511 const wxDataViewCtrl *view = GetView();
87f0efe2
RR
512 if (!m_text.empty())
513 {
514 int x,y;
515 view->GetTextExtent( m_text, &x, &y );
516 return wxSize( x, y );
517 }
3d9d7cc4
RR
518 return wxSize(80,20);
519}
520
2586d4a1 521// ---------------------------------------------------------
baa9ebc4 522// wxDataViewBitmapRenderer
2586d4a1
RR
523// ---------------------------------------------------------
524
baa9ebc4 525IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewCustomRenderer)
2586d4a1 526
87f0efe2 527wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype,
9861f022
RR
528 wxDataViewCellMode mode, int align ) :
529 wxDataViewCustomRenderer( varianttype, mode, align )
2586d4a1
RR
530{
531}
532
baa9ebc4 533bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
2586d4a1
RR
534{
535 if (value.GetType() == wxT("wxBitmap"))
536 m_bitmap << value;
537 if (value.GetType() == wxT("wxIcon"))
538 m_icon << value;
539
540 return true;
541}
542
9861f022 543bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
2586d4a1
RR
544{
545 return false;
546}
547
baa9ebc4 548bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
2586d4a1
RR
549{
550 if (m_bitmap.Ok())
551 dc->DrawBitmap( m_bitmap, cell.x, cell.y );
552 else if (m_icon.Ok())
553 dc->DrawIcon( m_icon, cell.x, cell.y );
554
555 return true;
556}
557
9861f022 558wxSize wxDataViewBitmapRenderer::GetSize() const
2586d4a1
RR
559{
560 if (m_bitmap.Ok())
561 return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() );
562 else if (m_icon.Ok())
563 return wxSize( m_icon.GetWidth(), m_icon.GetHeight() );
564
565 return wxSize(16,16);
566}
567
f554a14b 568// ---------------------------------------------------------
baa9ebc4 569// wxDataViewToggleRenderer
f554a14b 570// ---------------------------------------------------------
4ed7af08 571
baa9ebc4 572IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer, wxDataViewCustomRenderer)
4ed7af08 573
baa9ebc4 574wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
9861f022
RR
575 wxDataViewCellMode mode, int align ) :
576 wxDataViewCustomRenderer( varianttype, mode, align )
4ed7af08 577{
90675b95 578 m_toggle = false;
4ed7af08
RR
579}
580
baa9ebc4 581bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
4ed7af08 582{
90675b95 583 m_toggle = value.GetBool();
f554a14b 584
a8461d31 585 return true;
4ed7af08
RR
586}
587
9861f022 588bool wxDataViewToggleRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
4ed7af08
RR
589{
590 return false;
591}
f554a14b 592
baa9ebc4 593bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
4ed7af08 594{
90675b95 595 // User wxRenderer here
f554a14b 596
90675b95
RR
597 wxRect rect;
598 rect.x = cell.x + cell.width/2 - 10;
599 rect.width = 20;
600 rect.y = cell.y + cell.height/2 - 10;
601 rect.height = 20;
120b9b05 602
862d8041 603 int flags = 0;
90675b95 604 if (m_toggle)
862d8041
RR
605 flags |= wxCONTROL_CHECKED;
606 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE)
607 flags |= wxCONTROL_DISABLED;
608
90b903c2 609 wxRendererNative::Get().DrawCheckBox(
862d8041
RR
610 GetOwner()->GetOwner(),
611 *dc,
612 rect,
613 flags );
f554a14b 614
90675b95 615 return true;
4ed7af08
RR
616}
617
87f0efe2
RR
618bool wxDataViewToggleRenderer::Activate( wxRect WXUNUSED(cell),
619 wxDataViewListModel *model,
620 unsigned int col, unsigned int row )
0fdc2321
RR
621{
622 bool value = !m_toggle;
623 wxVariant variant = value;
624 model->SetValue( variant, col, row );
f554a14b 625 model->ValueChanged( col, row );
0fdc2321
RR
626 return true;
627}
628
9861f022 629wxSize wxDataViewToggleRenderer::GetSize() const
4ed7af08 630{
3d9d7cc4 631 return wxSize(20,20);
4ed7af08
RR
632}
633
f554a14b 634// ---------------------------------------------------------
baa9ebc4 635// wxDataViewProgressRenderer
f554a14b 636// ---------------------------------------------------------
4ed7af08 637
baa9ebc4 638IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
4ed7af08 639
baa9ebc4 640wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
9861f022
RR
641 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
642 wxDataViewCustomRenderer( varianttype, mode, align )
4ed7af08
RR
643{
644 m_label = label;
645 m_value = 0;
646}
647
baa9ebc4 648wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
4ed7af08
RR
649{
650}
651
baa9ebc4 652bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
4ed7af08
RR
653{
654 m_value = (long) value;
f554a14b 655
4ed7af08
RR
656 if (m_value < 0) m_value = 0;
657 if (m_value > 100) m_value = 100;
f554a14b 658
4ed7af08
RR
659 return true;
660}
f554a14b 661
9861f022
RR
662bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const
663{
664 value = (long) m_value;
665 return true;
666}
667
baa9ebc4 668bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
4ed7af08
RR
669{
670 double pct = (double)m_value / 100.0;
671 wxRect bar = cell;
672 bar.width = (int)(cell.width * pct);
673 dc->SetPen( *wxTRANSPARENT_PEN );
674 dc->SetBrush( *wxBLUE_BRUSH );
675 dc->DrawRectangle( bar );
676
677 dc->SetBrush( *wxTRANSPARENT_BRUSH );
678 dc->SetPen( *wxBLACK_PEN );
679 dc->DrawRectangle( cell );
f554a14b 680
4ed7af08
RR
681 return true;
682}
683
9861f022 684wxSize wxDataViewProgressRenderer::GetSize() const
4ed7af08
RR
685{
686 return wxSize(40,12);
687}
f554a14b
WS
688
689// ---------------------------------------------------------
baa9ebc4 690// wxDataViewDateRenderer
f554a14b 691// ---------------------------------------------------------
4ed7af08 692
21ead767
VZ
693#define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
694
695#if wxUSE_DATE_RENDERER_POPUP
8d0ca292 696
baa9ebc4 697class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
4ed7af08 698{
f554a14b 699public:
baa9ebc4 700 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
0a71f9e9 701 wxDataViewListModel *model, unsigned int col, unsigned int row ) :
4ed7af08
RR
702 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
703 {
704 m_model = model;
705 m_col = col;
706 m_row = row;
f554a14b 707 m_cal = new wxCalendarCtrl( this, wxID_ANY, *value );
4ed7af08
RR
708 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
709 sizer->Add( m_cal, 1, wxGROW );
710 SetSizer( sizer );
711 sizer->Fit( this );
712 }
f554a14b 713
4ed7af08 714 void OnCalendar( wxCalendarEvent &event );
f554a14b 715
4ed7af08 716 wxCalendarCtrl *m_cal;
f554a14b 717 wxDataViewListModel *m_model;
0a71f9e9
RR
718 unsigned int m_col;
719 unsigned int m_row;
f554a14b 720
a8461d31
PC
721protected:
722 virtual void OnDismiss()
723 {
724 }
725
4ed7af08
RR
726private:
727 DECLARE_EVENT_TABLE()
728};
729
baa9ebc4
RR
730BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
731 EVT_CALENDAR( wxID_ANY, wxDataViewDateRendererPopupTransient::OnCalendar )
4ed7af08
RR
732END_EVENT_TABLE()
733
baa9ebc4 734void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
4ed7af08
RR
735{
736 wxDateTime date = event.GetDate();
737 wxVariant value = date;
738 m_model->SetValue( value, m_col, m_row );
739 m_model->ValueChanged( m_col, m_row );
740 DismissAndNotify();
741}
742
21ead767 743#endif // wxUSE_DATE_RENDERER_POPUP
8d0ca292 744
baa9ebc4 745IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
4ed7af08 746
baa9ebc4 747wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
9861f022
RR
748 wxDataViewCellMode mode, int align ) :
749 wxDataViewCustomRenderer( varianttype, mode, align )
4ed7af08
RR
750{
751}
f554a14b 752
baa9ebc4 753bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
4ed7af08
RR
754{
755 m_date = value.GetDateTime();
f554a14b 756
4ed7af08
RR
757 return true;
758}
759
9861f022
RR
760bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const
761{
762 value = m_date;
763 return true;
764}
765
baa9ebc4 766bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
4ed7af08
RR
767{
768 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
769 wxString tmp = m_date.FormatDate();
770 dc->DrawText( tmp, cell.x, cell.y );
771
772 return true;
773}
774
9861f022 775wxSize wxDataViewDateRenderer::GetSize() const
4ed7af08 776{
9861f022 777 const wxDataViewCtrl* view = GetView();
4ed7af08
RR
778 wxString tmp = m_date.FormatDate();
779 wxCoord x,y,d;
780 view->GetTextExtent( tmp, &x, &y, &d );
781 return wxSize(x,y+d);
782}
783
87f0efe2
RR
784bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewListModel *model,
785 unsigned int col, unsigned int row )
4ed7af08
RR
786{
787 wxVariant variant;
788 model->GetValue( variant, col, row );
789 wxDateTime value = variant.GetDateTime();
790
21ead767 791#if wxUSE_DATE_RENDERER_POPUP
baa9ebc4 792 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
4ed7af08
RR
793 GetOwner()->GetOwner()->GetParent(), &value, model, col, row );
794 wxPoint pos = wxGetMousePosition();
795 popup->Move( pos );
796 popup->Layout();
797 popup->Popup( popup->m_cal );
21ead767 798#else // !wxUSE_DATE_RENDERER_POPUP
8d0ca292 799 wxMessageBox(value.Format());
21ead767 800#endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
4ed7af08
RR
801 return true;
802}
803
f554a14b 804// ---------------------------------------------------------
4ed7af08 805// wxDataViewColumn
f554a14b 806// ---------------------------------------------------------
4ed7af08
RR
807
808IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
809
87f0efe2
RR
810wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
811 unsigned int model_column,
812 int width, wxAlignment align, int flags ) :
813 wxDataViewColumnBase( title, cell, model_column, width, align, flags )
4ed7af08 814{
9861f022
RR
815 SetAlignment(align);
816 SetTitle(title);
817 SetFlags(flags);
818
819 Init(width < 0 ? wxDVC_DEFAULT_WIDTH : width);
4ed7af08
RR
820}
821
87f0efe2
RR
822wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
823 unsigned int model_column,
824 int width, wxAlignment align, int flags ) :
825 wxDataViewColumnBase( bitmap, cell, model_column, width, align, flags )
07a84e7b 826{
9861f022
RR
827 SetAlignment(align);
828 SetFlags(flags);
829
830 Init(width < 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH : width);
07a84e7b
RR
831}
832
9861f022
RR
833wxDataViewColumn::~wxDataViewColumn()
834{
835}
836
837void wxDataViewColumn::Init( int width )
47cef10f 838{
87f0efe2 839 m_width = width;
9861f022 840 m_minWidth = wxDVC_DEFAULT_MINWIDTH;
47cef10f
RR
841}
842
9861f022 843void wxDataViewColumn::SetResizeable( bool resizeable )
31fb32e1 844{
9861f022
RR
845 if (resizeable)
846 m_flags |= wxDATAVIEW_COL_RESIZABLE;
847 else
848 m_flags &= ~wxDATAVIEW_COL_RESIZABLE;
849}
850
851void wxDataViewColumn::SetHidden( bool hidden )
852{
853 if (hidden)
854 m_flags |= wxDATAVIEW_COL_HIDDEN;
855 else
856 m_flags &= ~wxDATAVIEW_COL_HIDDEN;
857
858 // tell our owner to e.g. update its scrollbars:
859 if (GetOwner())
860 GetOwner()->OnColumnChange();
861}
862
863void wxDataViewColumn::SetSortable( bool sortable )
864{
865 if (sortable)
866 m_flags |= wxDATAVIEW_COL_SORTABLE;
867 else
868 m_flags &= ~wxDATAVIEW_COL_SORTABLE;
31fb32e1
RR
869}
870
47cef10f
RR
871void wxDataViewColumn::SetSortOrder( bool WXUNUSED(ascending) )
872{
873 // TODO
874}
875
87f0efe2 876bool wxDataViewColumn::IsSortOrderAscending() const
31fb32e1
RR
877{
878 // TODO
879 return true;
880}
881
9861f022 882void wxDataViewColumn::SetInternalWidth( int width )
4ed7af08 883{
9861f022 884 m_width = width;
f554a14b 885
9861f022
RR
886 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
887 if (m_owner && m_owner->m_clientArea)
888 m_owner->m_clientArea->RecalculateDisplay();
4ed7af08
RR
889}
890
9861f022 891void wxDataViewColumn::SetWidth( int width )
47cef10f 892{
9861f022 893 m_owner->m_headerArea->UpdateDisplay();
47cef10f 894
9861f022 895 SetInternalWidth(width);
47cef10f
RR
896}
897
533544f2 898
4ed7af08 899//-----------------------------------------------------------------------------
87f0efe2 900// wxDataViewHeaderWindowBase
4ed7af08
RR
901//-----------------------------------------------------------------------------
902
87f0efe2
RR
903void wxDataViewHeaderWindowBase::SendEvent(wxEventType type, unsigned int n)
904{
905 wxWindow *parent = GetParent();
906 wxDataViewEvent le(type, parent->GetId());
907
908 le.SetEventObject(parent);
909 le.SetColumn(n);
910 le.SetDataViewColumn(GetColumn(n));
911 le.SetModel(GetOwner()->GetModel());
912
913 // for events created by wxDataViewHeaderWindow the
914 // row / value fields are not valid
915
916 parent->GetEventHandler()->ProcessEvent(le);
917}
918
919#if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
920
87f0efe2
RR
921// implemented in msw/listctrl.cpp:
922unsigned int wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick);
923
5d3f234b 924IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW, wxWindow)
87f0efe2
RR
925
926bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl *parent, wxWindowID id,
927 const wxPoint &pos, const wxSize &size,
928 const wxString &name )
929{
930 m_owner = parent;
931
932 if ( !CreateControl(parent, id, pos, size, 0, wxDefaultValidator, name) )
933 return false;
934
935 int x = pos.x == wxDefaultCoord ? 0 : pos.x,
936 y = pos.y == wxDefaultCoord ? 0 : pos.y,
937 w = size.x == wxDefaultCoord ? 1 : size.x,
938 h = size.y == wxDefaultCoord ? 22 : size.y;
939
940 // create the native WC_HEADER window:
941 WXHWND hwndParent = (HWND)parent->GetHandle();
942 WXDWORD msStyle = WS_CHILD | HDS_BUTTONS | HDS_HORZ | HDS_HOTTRACK | HDS_FULLDRAG;
943 m_hWnd = CreateWindowEx(0,
944 WC_HEADER,
945 (LPCTSTR) NULL,
946 msStyle,
947 x, y, w, h,
948 (HWND)hwndParent,
949 (HMENU)-1,
950 wxGetInstance(),
951 (LPVOID) NULL);
952 if (m_hWnd == NULL)
953 {
954 wxLogLastError(_T("CreateWindowEx"));
955 return false;
956 }
957
5d3f234b 958 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
87f0efe2 959 // to call wxDataViewHeaderWindow::MSWOnNotify
5d3f234b 960 SubclassWin(m_hWnd);
87f0efe2 961
9861f022
RR
962 // the following is required to get the default win's font for
963 // header windows and must be done befor sending the HDM_LAYOUT msg
964 SetFont(GetFont());
87f0efe2 965
9861f022
RR
966 RECT rcParent;
967 HDLAYOUT hdl;
968 WINDOWPOS wp;
87f0efe2
RR
969
970 // Retrieve the bounding rectangle of the parent window's
971 // client area, and then request size and position values
972 // from the header control.
973 ::GetClientRect((HWND)hwndParent, &rcParent);
974
975 hdl.prc = &rcParent;
976 hdl.pwpos = &wp;
977 if (!SendMessage((HWND)m_hWnd, HDM_LAYOUT, 0, (LPARAM) &hdl))
978 {
979 wxLogLastError(_T("SendMessage"));
980 return false;
981 }
982
983 // Set the size, position, and visibility of the header control.
984 SetWindowPos((HWND)m_hWnd,
985 wp.hwndInsertAfter,
986 wp.x, wp.y,
987 wp.cx, wp.cy,
988 wp.flags | SWP_SHOWWINDOW);
989
990 // set our size hints: wxDataViewCtrl will put this wxWindow inside
991 // a wxBoxSizer and in order to avoid super-big header windows,
992 // we need to set our height as fixed
993 SetMinSize(wxSize(-1, wp.cy));
994 SetMaxSize(wxSize(-1, wp.cy));
995
87f0efe2
RR
996 return true;
997}
998
999wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1000{
5d3f234b 1001 UnsubclassWin();
87f0efe2
RR
1002}
1003
1004void wxDataViewHeaderWindowMSW::UpdateDisplay()
1005{
1006 // remove old columns
914e6945 1007 for (int j=0, max=Header_GetItemCount((HWND)m_hWnd); j < max; j++)
87f0efe2
RR
1008 Header_DeleteItem((HWND)m_hWnd, 0);
1009
1010 // add the updated array of columns to the header control
9861f022
RR
1011 unsigned int cols = GetOwner()->GetColumnCount();
1012 unsigned int added = 0;
87f0efe2
RR
1013 for (unsigned int i = 0; i < cols; i++)
1014 {
1015 wxDataViewColumn *col = GetColumn( i );
1016 if (col->IsHidden())
1017 continue; // don't add it!
1018
1019 HDITEM hdi;
1020 hdi.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH;
9861f022 1021 hdi.pszText = (wxChar *) col->GetTitle().c_str();
87f0efe2
RR
1022 hdi.cxy = col->GetWidth();
1023 hdi.cchTextMax = sizeof(hdi.pszText)/sizeof(hdi.pszText[0]);
1024 hdi.fmt = HDF_LEFT | HDF_STRING;
9861f022
RR
1025
1026 // lParam is reserved for application's use:
1027 // we store there the column index to use it later in MSWOnNotify
1028 // (since columns may have been hidden)
1029 hdi.lParam = (LPARAM)i;
1030
1031 // the native wxMSW implementation of the header window
1032 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1033 // on the right: to correct this effect we make the column
1034 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1035 if (i == 0)
1036 hdi.cxy += COLUMN_WIDTH_OFFSET;
1037
1038 switch (col->GetAlignment())
1039 {
1040 case wxALIGN_LEFT:
1041 hdi.fmt |= HDF_LEFT;
1042 break;
1043 case wxALIGN_CENTER:
1044 case wxALIGN_CENTER_HORIZONTAL:
1045 hdi.fmt |= HDF_CENTER;
1046 break;
1047 case wxALIGN_RIGHT:
1048 hdi.fmt |= HDF_RIGHT;
1049 break;
5d3f234b
RR
1050
1051 default:
1052 // such alignment is not allowed for the column header!
1053 wxFAIL;
9861f022 1054 }
87f0efe2 1055
9861f022
RR
1056 SendMessage((HWND)m_hWnd, HDM_INSERTITEM,
1057 (WPARAM)added, (LPARAM)&hdi);
1058 added++;
87f0efe2
RR
1059 }
1060}
1061
9861f022
RR
1062unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER *nmHDR)
1063{
1064 unsigned int idx;
1065
1066 // NOTE: we don't just return nmHDR->iItem because when there are
1067 // hidden columns, nmHDR->iItem may be different from
1068 // nmHDR->pitem->lParam
1069
1070 if (nmHDR->pitem && nmHDR->pitem->mask & HDI_LPARAM)
1071 {
1072 idx = (unsigned int)nmHDR->pitem->lParam;
1073 return idx;
1074 }
1075
1076 HDITEM item;
1077 item.mask = HDI_LPARAM;
1078 Header_GetItem((HWND)m_hWnd, nmHDR->iItem, &item);
1079
1080 return (unsigned int)item.lParam;
1081}
1082
87f0efe2
RR
1083bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1084{
1085 NMHDR *nmhdr = (NMHDR *)lParam;
1086
1087 // is it a message from the header?
1088 if ( nmhdr->hwndFrom != (HWND)m_hWnd )
1089 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
1090
1091 NMHEADER *nmHDR = (NMHEADER *)nmhdr;
1092 switch ( nmhdr->code )
1093 {
1094 case HDN_BEGINTRACK:
1095 // user has started to resize a column:
1096 // do we need to veto it?
1097 if (!GetColumn(nmHDR->iItem)->IsResizeable())
1098 {
1099 // veto it!
1100 *result = TRUE;
1101 }
1102 break;
1103
1104 case HDN_BEGINDRAG:
1105 // user has started to reorder a column
1106 break;
1107
9861f022
RR
1108 case HDN_ITEMCHANGING:
1109 if (nmHDR->pitem != NULL &&
1110 (nmHDR->pitem->mask & HDI_WIDTH) != 0)
1111 {
1112 int minWidth = GetColumnFromHeader(nmHDR)->GetMinWidth();
1113 if (nmHDR->pitem->cxy < minWidth)
1114 {
1115 // do not allow the user to resize this column under
1116 // its minimal width:
1117 *result = TRUE;
1118 }
1119 }
1120 break;
1121
87f0efe2
RR
1122 case HDN_ITEMCHANGED: // user is resizing a column
1123 case HDN_ENDTRACK: // user has finished resizing a column
1124 case HDN_ENDDRAG: // user has finished reordering a column
1125
1126 // update the width of the modified column:
9861f022
RR
1127 if (nmHDR->pitem != NULL &&
1128 (nmHDR->pitem->mask & HDI_WIDTH) != 0)
1129 {
1130 unsigned int idx = GetColumnIdxFromHeader(nmHDR);
1131 unsigned int w = nmHDR->pitem->cxy;
1132 wxDataViewColumn *col = GetColumn(idx);
1133
1134 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1135 if (idx == 0 && w > COLUMN_WIDTH_OFFSET)
1136 w -= COLUMN_WIDTH_OFFSET;
1137
1138 if (w >= (unsigned)col->GetMinWidth())
1139 col->SetInternalWidth(w);
1140 }
87f0efe2
RR
1141 break;
1142
1143 case HDN_ITEMCLICK:
1144 {
9861f022 1145 unsigned int idx = GetColumnIdxFromHeader(nmHDR);
87f0efe2
RR
1146 wxEventType evt = nmHDR->iButton == 0 ?
1147 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK :
1148 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK;
9861f022 1149 SendEvent(evt, idx);
87f0efe2
RR
1150 }
1151 break;
1152
1153 case NM_RCLICK:
1154 {
1155 // NOTE: for some reason (i.e. for a bug in Windows)
1156 // the HDN_ITEMCLICK notification is not sent on
1157 // right clicks, so we need to handle NM_RCLICK
1158
1159 POINT ptClick;
9861f022 1160 int column = wxMSWGetColumnClicked(nmhdr, &ptClick);
87f0efe2 1161 if (column != wxNOT_FOUND)
9861f022
RR
1162 {
1163 HDITEM item;
1164 item.mask = HDI_LPARAM;
1165 Header_GetItem((HWND)m_hWnd, column, &item);
1166
1167 // 'idx' may be different from 'column' if there are
1168 // hidden columns...
1169 unsigned int idx = (unsigned int)item.lParam;
87f0efe2 1170 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK,
9861f022
RR
1171 idx);
1172 }
87f0efe2
RR
1173 }
1174 break;
1175
1176 case HDN_GETDISPINFOW:
1177 // see wxListCtrl::MSWOnNotify for more info!
1178 break;
1179
1180 case HDN_ITEMDBLCLICK:
1181 {
9861f022 1182 unsigned int idx = GetColumnIdxFromHeader(nmHDR);
87f0efe2
RR
1183 int w = GetOwner()->GetBestColumnWidth(idx);
1184
1185 // update the native control:
1186 HDITEM hd;
1187 ZeroMemory(&hd, sizeof(hd));
1188 hd.mask = HDI_WIDTH;
1189 hd.cxy = w;
9861f022
RR
1190 Header_SetItem(GetHwnd(),
1191 nmHDR->iItem, // NOTE: we don't want 'idx' here!
1192 &hd);
87f0efe2
RR
1193
1194 // update the wxDataViewColumn class:
9861f022 1195 GetColumn(idx)->SetInternalWidth(w);
87f0efe2
RR
1196 }
1197 break;
1198
1199 default:
1200 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
1201 }
1202
1203 return true;
1204}
1205
1206void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx), int WXUNUSED(dy),
1207 const wxRect *WXUNUSED(rect))
1208{
1209 wxSize ourSz = GetClientSize();
1210 wxSize ownerSz = m_owner->GetClientSize();
1211
1212 // where should the (logical) origin of this window be placed?
1213 int x1 = 0, y1 = 0;
1214 m_owner->CalcUnscrolledPosition(0, 0, &x1, &y1);
4ed7af08 1215
87f0efe2 1216 // put this window on top of our parent and
9861f022 1217 SetWindowPos((HWND)m_hWnd, HWND_TOP, -x1, 0,
87f0efe2
RR
1218 ownerSz.GetWidth() + x1, ourSz.GetHeight(),
1219 SWP_SHOWWINDOW);
1220}
1221
9861f022
RR
1222void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
1223 int WXUNUSED(w), int WXUNUSED(h),
1224 int WXUNUSED(f))
1225{
1226 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1227 // the wxDataViewCtrl window gets resized: the following dummy call
1228 // to ScrollWindow() is required in order to get this header window
1229 // correctly repainted when it's (horizontally) scrolled:
1230
1231 ScrollWindow(0, 0);
1232}
1233
87f0efe2
RR
1234#else // !defined(__WXMSW__)
1235
1236IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow, wxWindow)
1237BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow, wxWindow)
1238 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint)
1239 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse)
1240 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus)
4ed7af08
RR
1241END_EVENT_TABLE()
1242
87f0efe2
RR
1243bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl *parent, wxWindowID id,
1244 const wxPoint &pos, const wxSize &size,
1245 const wxString &name )
4ed7af08 1246{
87f0efe2 1247 m_owner = parent;
4b3feaa7 1248
87f0efe2
RR
1249 if (!wxDataViewHeaderWindowBase::Create(parent, id, pos, size, name))
1250 return false;
f554a14b 1251
4ed7af08 1252 wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
2e992e06 1253 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
4ed7af08
RR
1254 SetOwnForegroundColour( attr.colFg );
1255 SetOwnBackgroundColour( attr.colBg );
1256 if (!m_hasFont)
1257 SetOwnFont( attr.font );
4ed7af08 1258
87f0efe2
RR
1259 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1260 // a wxBoxSizer and in order to avoid super-big header windows,
1261 // we need to set our height as fixed
1262 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT));
1263 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT));
1264
1265 return true;
4ed7af08
RR
1266}
1267
87f0efe2 1268void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
4ed7af08 1269{
4b3feaa7
RR
1270 int w, h;
1271 GetClientSize( &w, &h );
1272
2e992e06
VZ
1273 wxAutoBufferedPaintDC dc( this );
1274
1275 dc.SetBackground(GetBackgroundColour());
1276 dc.Clear();
f554a14b 1277
4ed7af08
RR
1278 int xpix;
1279 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
1280
1281 int x;
1282 m_owner->GetViewStart( &x, NULL );
1283
1284 // account for the horz scrollbar offset
1285 dc.SetDeviceOrigin( -x * xpix, 0 );
f554a14b 1286
4ed7af08 1287 dc.SetFont( GetFont() );
f554a14b 1288
9861f022 1289 unsigned int cols = GetOwner()->GetColumnCount();
0a71f9e9 1290 unsigned int i;
4b3feaa7
RR
1291 int xpos = 0;
1292 for (i = 0; i < cols; i++)
1293 {
87f0efe2
RR
1294 wxDataViewColumn *col = GetColumn( i );
1295 if (col->IsHidden())
9861f022 1296 continue; // skip it!
f554a14b 1297
87f0efe2 1298 int cw = col->GetWidth();
4b3feaa7 1299 int ch = h;
4b3feaa7
RR
1300
1301 wxRendererNative::Get().DrawHeaderButton
1302 (
1303 this,
1304 dc,
72664514 1305 wxRect(xpos, 0, cw, ch-1),
4b3feaa7
RR
1306 m_parent->IsEnabled() ? 0
1307 : (int)wxCONTROL_DISABLED
1308 );
1309
9861f022
RR
1310 // align as required the column title:
1311 int x = xpos;
1312 wxSize titleSz = dc.GetTextExtent(col->GetTitle());
1313 switch (col->GetAlignment())
1314 {
1315 case wxALIGN_LEFT:
1316 x += HEADER_HORIZ_BORDER;
1317 break;
1318 case wxALIGN_CENTER:
1319 case wxALIGN_CENTER_HORIZONTAL:
1320 x += (cw - titleSz.GetWidth() - 2 * HEADER_HORIZ_BORDER)/2;
1321 break;
1322 case wxALIGN_RIGHT:
1323 x += cw - titleSz.GetWidth() - HEADER_HORIZ_BORDER;
1324 break;
1325 }
1326
1327 // always center the title vertically:
1328 int y = wxMax((ch - titleSz.GetHeight()) / 2, HEADER_VERT_BORDER);
1329
1330 dc.SetClippingRegion( xpos+HEADER_HORIZ_BORDER,
1331 HEADER_VERT_BORDER,
1332 wxMax(cw - 2 * HEADER_HORIZ_BORDER, 1), // width
1333 wxMax(ch - 2 * HEADER_VERT_BORDER, 1)); // height
1334 dc.DrawText( col->GetTitle(), x, y );
1335 dc.DestroyClippingRegion();
f554a14b 1336
87f0efe2 1337 xpos += cw;
4b3feaa7 1338 }
4ed7af08
RR
1339}
1340
87f0efe2 1341void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent &event )
4ed7af08 1342{
87f0efe2
RR
1343 GetParent()->SetFocus();
1344 event.Skip();
4ed7af08
RR
1345}
1346
87f0efe2 1347void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent &event )
4ed7af08 1348{
87f0efe2
RR
1349 // we want to work with logical coords
1350 int x;
1351 m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1352 int y = event.GetY();
1353
1354 if (m_isDragging)
1355 {
1356 // we don't draw the line beyond our window,
1357 // but we allow dragging it there
1358 int w = 0;
1359 GetClientSize( &w, NULL );
1360 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1361 w -= 6;
1362
1363 // erase the line if it was drawn
1364 if (m_currentX < w)
1365 DrawCurrent();
1366
1367 if (event.ButtonUp())
1368 {
1369 m_isDragging = false;
1370 if (HasCapture())
1371 ReleaseMouse();
1372
1373 m_dirty = true;
1374
9861f022 1375 GetColumn(m_column)->SetWidth(m_currentX - m_minX);
87f0efe2
RR
1376
1377 Refresh();
1378 GetOwner()->Refresh();
1379 }
1380 else
1381 {
1382 m_currentX = wxMax(m_minX + 7, x);
1383
1384 // draw in the new location
1385 if (m_currentX < w) DrawCurrent();
1386 }
1387
1388 }
1389 else // not dragging
1390 {
1391 m_minX = 0;
1392 m_column = wxNOT_FOUND;
1393
1394 bool hit_border = false;
1395
1396 // end of the current column
1397 int xpos = 0;
1398
1399 // find the column where this event occured
9861f022 1400 int countCol = m_owner->GetColumnCount();
87f0efe2
RR
1401 for (int column = 0; column < countCol; column++)
1402 {
1403 wxDataViewColumn *p = GetColumn(column);
1404
1405 if (p->IsHidden())
1406 continue; // skip if not shown
1407
1408 xpos += p->GetWidth();
1409 m_column = column;
1410 if ((abs(x-xpos) < 3) && (y < 22))
1411 {
1412 hit_border = true;
1413 break;
1414 }
1415
1416 if (x < xpos)
1417 {
1418 // inside the column
1419 break;
1420 }
1421
1422 m_minX = xpos;
1423 }
1424
1425 if (m_column == wxNOT_FOUND)
1426 return;
1427
1428 bool resizeable = GetColumn(m_column)->IsResizeable();
1429 if (event.LeftDClick() && resizeable)
1430 {
9861f022 1431 GetColumn(m_column)->SetWidth(GetOwner()->GetBestColumnWidth(m_column));
87f0efe2
RR
1432 Refresh();
1433 }
1434 else if (event.LeftDown() || event.RightUp())
1435 {
1436 if (hit_border && event.LeftDown() && resizeable)
1437 {
1438 m_isDragging = true;
1439 CaptureMouse();
1440 m_currentX = x;
1441 DrawCurrent();
1442 }
1443 else // click on a column
1444 {
1445 wxEventType evt = event.LeftDown() ?
1446 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK :
1447 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK;
1448 SendEvent(evt, m_column);
1449 }
1450 }
1451 else if (event.Moving())
1452 {
1453 if (hit_border && resizeable)
1454 m_currentCursor = m_resizeCursor;
1455 else
1456 m_currentCursor = wxSTANDARD_CURSOR;
1457
1458 SetCursor(*m_currentCursor);
1459 }
1460 }
1461}
1462
1463void wxGenericDataViewHeaderWindow::DrawCurrent()
1464{
1465 int x1 = m_currentX;
1466 int y1 = 0;
1467 ClientToScreen (&x1, &y1);
1468
1469 int x2 = m_currentX-1;
1470#ifdef __WXMSW__
1471 ++x2; // but why ????
1472#endif
1473 int y2 = 0;
1474 m_owner->GetClientSize( NULL, &y2 );
1475 m_owner->ClientToScreen( &x2, &y2 );
1476
1477 wxScreenDC dc;
9861f022
RR
1478 dc.SetLogicalFunction(wxINVERT);
1479 dc.SetPen(m_penCurrent);
1480 dc.SetBrush(*wxTRANSPARENT_BRUSH);
87f0efe2 1481 AdjustDC(dc);
9861f022 1482 dc.DrawLine(x1, y1, x2, y2);
87f0efe2
RR
1483}
1484
1485void wxGenericDataViewHeaderWindow::AdjustDC(wxDC& dc)
1486{
1487 int xpix, x;
1488
1489 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
1490 m_owner->GetViewStart( &x, NULL );
1491
1492 // shift the DC origin to match the position of the main window horizontal
1493 // scrollbar: this allows us to always use logical coords
1494 dc.SetDeviceOrigin( -x * xpix, 0 );
4ed7af08
RR
1495}
1496
87f0efe2
RR
1497#endif // defined(__WXMSW__)
1498
0fcce6b9
RR
1499//-----------------------------------------------------------------------------
1500// wxDataViewRenameTimer
1501//-----------------------------------------------------------------------------
1502
1503wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner )
1504{
1505 m_owner = owner;
1506}
1507
1508void wxDataViewRenameTimer::Notify()
1509{
1510 m_owner->OnRenameTimer();
1511}
1512
1513//-----------------------------------------------------------------------------
1514// wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
1515//-----------------------------------------------------------------------------
1516
1517BEGIN_EVENT_TABLE(wxDataViewTextCtrlWrapper, wxEvtHandler)
1518 EVT_CHAR (wxDataViewTextCtrlWrapper::OnChar)
1519 EVT_KEY_UP (wxDataViewTextCtrlWrapper::OnKeyUp)
1520 EVT_KILL_FOCUS (wxDataViewTextCtrlWrapper::OnKillFocus)
1521END_EVENT_TABLE()
1522
1523wxDataViewTextCtrlWrapper::wxDataViewTextCtrlWrapper(
1524 wxDataViewMainWindow *owner,
1525 wxTextCtrl *text,
1526 wxDataViewListModel *model,
0a71f9e9 1527 unsigned int col, unsigned int row,
0fcce6b9
RR
1528 wxRect rectLabel )
1529{
1530 m_owner = owner;
1531 m_model = model;
1532 m_row = row;
1533 m_col = col;
120b9b05
WS
1534 m_text = text;
1535
0fcce6b9
RR
1536 m_finished = false;
1537 m_aboutToFinish = false;
120b9b05 1538
0fcce6b9
RR
1539 wxVariant value;
1540 model->GetValue( value, col, row );
1541 m_startValue = value.GetString();
120b9b05 1542
0fcce6b9
RR
1543 m_owner->GetOwner()->CalcScrolledPosition(
1544 rectLabel.x, rectLabel.y, &rectLabel.x, &rectLabel.y );
1545
1546 m_text->Create( owner, wxID_ANY, m_startValue,
1547 wxPoint(rectLabel.x-2,rectLabel.y-2),
1548 wxSize(rectLabel.width+7,rectLabel.height+4) );
1549 m_text->SetFocus();
120b9b05 1550
0fcce6b9
RR
1551 m_text->PushEventHandler(this);
1552}
1553
1554void wxDataViewTextCtrlWrapper::AcceptChangesAndFinish()
1555{
1556 m_aboutToFinish = true;
1557
1558 // Notify the owner about the changes
1559 AcceptChanges();
1560
1561 // Even if vetoed, close the control (consistent with MSW)
1562 Finish();
1563}
1564
1565void wxDataViewTextCtrlWrapper::OnChar( wxKeyEvent &event )
1566{
1567 switch ( event.m_keyCode )
1568 {
1569 case WXK_RETURN:
1570 AcceptChangesAndFinish();
1571 break;
1572
1573 case WXK_ESCAPE:
1574 // m_owner->OnRenameCancelled( m_itemEdited );
1575 Finish();
1576 break;
1577
1578 default:
1579 event.Skip();
1580 }
1581}
1582
1583void wxDataViewTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
1584{
1585 if (m_finished)
1586 {
1587 event.Skip();
1588 return;
1589 }
1590
1591 // auto-grow the textctrl
1592 wxSize parentSize = m_owner->GetSize();
1593 wxPoint myPos = m_text->GetPosition();
1594 wxSize mySize = m_text->GetSize();
1595 int sx, sy;
1596 m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy);
1597 if (myPos.x + sx > parentSize.x)
1598 sx = parentSize.x - myPos.x;
1599 if (mySize.x > sx)
1600 sx = mySize.x;
1601 m_text->SetSize(sx, wxDefaultCoord);
1602
1603 event.Skip();
1604}
1605
1606void wxDataViewTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
1607{
1608 if ( !m_finished && !m_aboutToFinish )
1609 {
1610 AcceptChanges();
1611 //if ( !AcceptChanges() )
1612 // m_owner->OnRenameCancelled( m_itemEdited );
120b9b05 1613
0fcce6b9
RR
1614 Finish();
1615 }
1616
1617 // We must let the native text control handle focus
1618 event.Skip();
1619}
1620
1621bool wxDataViewTextCtrlWrapper::AcceptChanges()
1622{
1623 const wxString value = m_text->GetValue();
1624
1625 if ( value == m_startValue )
1626 // nothing changed, always accept
1627 return true;
1628
1629// if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
1630 // vetoed by the user
1631// return false;
1632
1633 // accepted, do rename the item
1634 wxVariant variant;
1635 variant = value;
1636 m_model->SetValue( variant, m_col, m_row );
1637 m_model->ValueChanged( m_col, m_row );
1638
1639 return true;
1640}
1641
1642void wxDataViewTextCtrlWrapper::Finish()
1643{
1644 if ( !m_finished )
1645 {
1646 m_finished = true;
1647
1648 m_text->RemoveEventHandler(this);
1649 m_owner->FinishEditing(m_text);
1650
1651 // delete later
1652 wxPendingDelete.Append( this );
1653 }
1654}
1655
4ed7af08
RR
1656//-----------------------------------------------------------------------------
1657// wxDataViewMainWindow
1658//-----------------------------------------------------------------------------
1659
0a71f9e9 1660int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
cab07038
RR
1661{
1662 if (row1 > row2) return 1;
1663 if (row1 == row2) return 0;
1664 return -1;
1665}
1666
1667
45778c96 1668IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow)
4ed7af08
RR
1669
1670BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow)
1671 EVT_PAINT (wxDataViewMainWindow::OnPaint)
1672 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse)
1673 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus)
cab07038
RR
1674 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus)
1675 EVT_CHAR (wxDataViewMainWindow::OnChar)
4ed7af08
RR
1676END_EVENT_TABLE()
1677
1678wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id,
1679 const wxPoint &pos, const wxSize &size, const wxString &name ) :
72664514 1680 wxWindow( parent, id, pos, size, wxWANTS_CHARS, name ),
cab07038 1681 m_selection( wxDataViewSelectionCmp )
120b9b05 1682
4ed7af08
RR
1683{
1684 SetOwner( parent );
f554a14b 1685
0fcce6b9
RR
1686 m_lastOnSame = false;
1687 m_renameTimer = new wxDataViewRenameTimer( this );
1688 m_textctrlWrapper = NULL;
120b9b05 1689
0fcce6b9
RR
1690 // TODO: user better initial values/nothing selected
1691 m_currentCol = NULL;
1692 m_currentRow = 0;
1693
1694 // TODO: we need to calculate this smartly
87f0efe2
RR
1695 m_lineHeight =
1696#ifdef __WXMSW__
1697 17;
1698#else
1699 20;
1700#endif
9861f022 1701 wxASSERT(m_lineHeight > 2*PADDING_TOPBOTTOM);
e21f75bd
RR
1702
1703 m_dragCount = 0;
1704 m_dragStart = wxPoint(0,0);
0a71f9e9
RR
1705 m_lineLastClicked = (unsigned int) -1;
1706 m_lineBeforeLastClicked = (unsigned int) -1;
1707 m_lineSelectSingleOnUp = (unsigned int) -1;
120b9b05 1708
cab07038 1709 m_hasFocus = false;
f554a14b 1710
2e992e06 1711 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
72664514
RR
1712 SetBackgroundColour( *wxWHITE );
1713
9861f022
RR
1714 m_penRule = wxPen(GetRuleColour(), 1, wxSOLID);
1715
4b3feaa7 1716 UpdateDisplay();
4ed7af08
RR
1717}
1718
1719wxDataViewMainWindow::~wxDataViewMainWindow()
1720{
0fcce6b9
RR
1721 delete m_renameTimer;
1722}
1723
1724void wxDataViewMainWindow::OnRenameTimer()
1725{
1726 // We have to call this here because changes may just have
1727 // been made and no screen update taken place.
1728 if ( m_dirty )
1729 wxSafeYield();
1730
1731
1732 int xpos = 0;
9861f022 1733 unsigned int cols = GetOwner()->GetColumnCount();
0a71f9e9 1734 unsigned int i;
0fcce6b9
RR
1735 for (i = 0; i < cols; i++)
1736 {
1737 wxDataViewColumn *c = GetOwner()->GetColumn( i );
9861f022
RR
1738 if (c->IsHidden())
1739 continue; // skip it!
1740
0fcce6b9
RR
1741 if (c == m_currentCol)
1742 break;
1743 xpos += c->GetWidth();
1744 }
87f0efe2
RR
1745 wxRect labelRect( xpos, m_currentRow * m_lineHeight,
1746 m_currentCol->GetWidth(), m_lineHeight );
0fcce6b9
RR
1747
1748 wxClassInfo *textControlClass = CLASSINFO(wxTextCtrl);
1749
1750 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
120b9b05 1751 m_textctrlWrapper = new wxDataViewTextCtrlWrapper(this, text, GetOwner()->GetModel(),
0fcce6b9
RR
1752 m_currentCol->GetModelColumn(), m_currentRow, labelRect );
1753}
1754
1755void wxDataViewMainWindow::FinishEditing( wxTextCtrl *text )
1756{
1757 delete text;
1758 m_textctrlWrapper = NULL;
1759 SetFocus();
1760 // SetFocusIgnoringChildren();
4ed7af08
RR
1761}
1762
a0f3af5f
RR
1763bool wxDataViewMainWindow::RowAppended()
1764{
1765 return false;
1766}
1767
1768bool wxDataViewMainWindow::RowPrepended()
1769{
1770 return false;
1771}
1772
0a71f9e9 1773bool wxDataViewMainWindow::RowInserted( unsigned int WXUNUSED(before) )
a0f3af5f
RR
1774{
1775 return false;
1776}
1777
0a71f9e9 1778bool wxDataViewMainWindow::RowDeleted( unsigned int WXUNUSED(row) )
a0f3af5f
RR
1779{
1780 return false;
1781}
1782
0a71f9e9 1783bool wxDataViewMainWindow::RowChanged( unsigned int WXUNUSED(row) )
a0f3af5f
RR
1784{
1785 return false;
1786}
1787
0a71f9e9 1788bool wxDataViewMainWindow::ValueChanged( unsigned int WXUNUSED(col), unsigned int row )
a0f3af5f 1789{
9861f022
RR
1790 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1791#define MAX_VIRTUAL_WIDTH 100000
1792
1793 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
0fdc2321
RR
1794 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1795 Refresh( true, &rect );
1796
1797 return true;
a0f3af5f
RR
1798}
1799
0a71f9e9 1800bool wxDataViewMainWindow::RowsReordered( unsigned int *WXUNUSED(new_order) )
a0f3af5f 1801{
0fcce6b9 1802 Refresh();
120b9b05 1803
0fcce6b9 1804 return true;
a0f3af5f
RR
1805}
1806
1807bool wxDataViewMainWindow::Cleared()
1808{
1809 return false;
1810}
1811
4b3feaa7
RR
1812void wxDataViewMainWindow::UpdateDisplay()
1813{
1814 m_dirty = true;
1815}
1816
1817void wxDataViewMainWindow::OnInternalIdle()
1818{
1819 wxWindow::OnInternalIdle();
f554a14b 1820
4b3feaa7
RR
1821 if (m_dirty)
1822 {
1823 RecalculateDisplay();
1824 m_dirty = false;
1825 }
1826}
1827
1828void wxDataViewMainWindow::RecalculateDisplay()
1829{
1830 wxDataViewListModel *model = GetOwner()->GetModel();
1831 if (!model)
1832 {
1833 Refresh();
1834 return;
1835 }
f554a14b 1836
9861f022
RR
1837 int width = GetEndOfLastCol();
1838 int height = model->GetRowCount() * m_lineHeight;
4b3feaa7
RR
1839
1840 SetVirtualSize( width, height );
1841 GetOwner()->SetScrollRate( 10, m_lineHeight );
f554a14b 1842
4b3feaa7
RR
1843 Refresh();
1844}
1845
1846void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
1847{
1848 wxWindow::ScrollWindow( dx, dy, rect );
9861f022
RR
1849
1850 if (GetOwner()->m_headerArea)
1851 GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
4b3feaa7
RR
1852}
1853
f554a14b 1854void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
4ed7af08 1855{
9861f022 1856 wxDataViewListModel *model = GetOwner()->GetModel();
2e992e06
VZ
1857 wxAutoBufferedPaintDC dc( this );
1858
9861f022 1859 // prepare the DC
2e992e06
VZ
1860 dc.SetBackground(GetBackgroundColour());
1861 dc.Clear();
4b3feaa7 1862 GetOwner()->PrepareDC( dc );
4ed7af08 1863 dc.SetFont( GetFont() );
90675b95
RR
1864
1865 wxRect update = GetUpdateRegion().GetBox();
1866 m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y );
f554a14b 1867
9861f022 1868 // compute which items needs to be redrawn
0a71f9e9 1869 unsigned int item_start = wxMax( 0, (update.y / m_lineHeight) );
87f0efe2
RR
1870 unsigned int item_count =
1871 wxMin( (int)(((update.y + update.height) / m_lineHeight) - item_start + 1),
9861f022
RR
1872 (int)(model->GetRowCount() - item_start) );
1873 unsigned int item_last = item_start + item_count;
1874
1875 // compute which columns needs to be redrawn
1876 unsigned int cols = GetOwner()->GetColumnCount();
1877 unsigned int col_start = 0;
1878 unsigned int x_start = 0;
1879 for (x_start = 0; col_start < cols; col_start++)
1880 {
1881 wxDataViewColumn *col = GetOwner()->GetColumn(col_start);
1882 if (col->IsHidden())
1883 continue; // skip it!
1884
1885 unsigned int w = col->GetWidth();
1886 if (x_start+w >= (unsigned int)update.x)
1887 break;
1888
1889 x_start += w;
1890 }
90675b95 1891
9861f022
RR
1892 unsigned int col_last = col_start;
1893 unsigned int x_last = x_start;
1894 for (; col_last < cols; col_last++)
1895 {
1896 wxDataViewColumn *col = GetOwner()->GetColumn(col_last);
1897 if (col->IsHidden())
1898 continue; // skip it!
1899
1900 if (x_last > (unsigned int)update.GetRight())
1901 break;
1902
1903 x_last += col->GetWidth();
1904 }
1905
1906 // Draw horizontal rules if required
1907 if ( m_owner->HasFlag(wxDV_HORIZ_RULES) )
1908 {
1909 dc.SetPen(m_penRule);
1910 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1911
1912 for (unsigned int i = item_start; i <= item_last+1; i++)
1913 {
1914 int y = i * m_lineHeight;
1915 dc.DrawLine(x_start, y, x_last, y);
1916 }
1917 }
1918
1919 // Draw vertical rules if required
1920 if ( m_owner->HasFlag(wxDV_VERT_RULES) )
1921 {
1922 dc.SetPen(m_penRule);
1923 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1924
1925 int x = x_start;
1926 for (unsigned int i = col_start; i < col_last; i++)
1927 {
1928 wxDataViewColumn *col = GetOwner()->GetColumn(i);
1929 if (col->IsHidden())
1930 continue; // skip it
1931
1932 dc.DrawLine(x, item_start * m_lineHeight,
1933 x, item_last * m_lineHeight);
1934
1935 x += col->GetWidth();
1936 }
1937
1938 // Draw last vertical rule
1939 dc.DrawLine(x, item_start * m_lineHeight,
1940 x, item_last * m_lineHeight);
1941 }
1942
1943 // redraw the background for the items which are selected/current
1944 for (unsigned int item = item_start; item < item_last; item++)
cab07038 1945 {
87f0efe2
RR
1946 bool selected = m_selection.Index( item ) != wxNOT_FOUND;
1947 if (selected || item == m_currentRow)
cab07038 1948 {
5d3f234b 1949 int flags = selected ? (int)wxCONTROL_SELECTED : 0;
daebb44c
RR
1950 if (item == m_currentRow)
1951 flags |= wxCONTROL_CURRENT;
1952 if (m_hasFocus)
1953 flags |= wxCONTROL_FOCUSED;
9861f022
RR
1954
1955 wxRect rect( x_start, item*m_lineHeight, x_last, m_lineHeight );
daebb44c
RR
1956 wxRendererNative::Get().DrawItemSelectionRect
1957 (
1958 this,
1959 dc,
1960 rect,
1961 flags
1962 );
1963 }
cab07038 1964 }
120b9b05 1965
9861f022 1966 // redraw all cells for all rows which must be repainted and for all columns
90675b95 1967 wxRect cell_rect;
9861f022
RR
1968 cell_rect.x = x_start;
1969 cell_rect.height = m_lineHeight; // -1 is for the horizontal rules
1970 for (unsigned int i = col_start; i < col_last; i++)
90675b95
RR
1971 {
1972 wxDataViewColumn *col = GetOwner()->GetColumn( i );
baa9ebc4 1973 wxDataViewRenderer *cell = col->GetRenderer();
90675b95 1974 cell_rect.width = col->GetWidth();
f554a14b 1975
87f0efe2
RR
1976 if (col->IsHidden())
1977 continue; // skipt it!
1978
9861f022 1979 for (unsigned int item = item_start; item < item_last; item++)
90675b95 1980 {
87f0efe2 1981 // get the cell value and set it into the renderer
90675b95
RR
1982 wxVariant value;
1983 model->GetValue( value, col->GetModelColumn(), item );
1984 cell->SetValue( value );
87f0efe2
RR
1985
1986 // update the y offset
9861f022 1987 cell_rect.y = item * m_lineHeight;
87f0efe2 1988
4064f7de 1989 // cannot be bigger than allocated space
87f0efe2 1990 wxSize size = cell->GetSize();
9861f022
RR
1991 size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width );
1992 size.y = wxMin( size.y + 2*PADDING_TOPBOTTOM, cell_rect.height );
87f0efe2
RR
1993
1994 wxRect item_rect(cell_rect.GetTopLeft(), size);
9861f022 1995 int align = cell->GetAlignment();
87f0efe2
RR
1996
1997 // horizontal alignment:
9861f022
RR
1998 item_rect.x = cell_rect.x;
1999 if (align & wxALIGN_CENTER_HORIZONTAL)
2000 item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2);
2001 else if (align & wxALIGN_RIGHT)
87f0efe2
RR
2002 item_rect.x = cell_rect.x + cell_rect.width - size.x;
2003 //else: wxALIGN_LEFT is the default
2004
2005 // vertical alignment:
9861f022
RR
2006 item_rect.y = cell_rect.y;
2007 if (align & wxALIGN_CENTER_VERTICAL)
87f0efe2 2008 item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2);
9861f022 2009 else if (align & wxALIGN_BOTTOM)
87f0efe2
RR
2010 item_rect.y = cell_rect.y + cell_rect.height - size.y;
2011 //else: wxALIGN_TOP is the default
2012
9861f022
RR
2013 // add padding
2014 item_rect.x += PADDING_RIGHTLEFT;
2015 item_rect.y += PADDING_TOPBOTTOM;
2016 item_rect.width = size.x - 2 * PADDING_RIGHTLEFT;
2017 item_rect.height = size.y - 2 * PADDING_TOPBOTTOM;
64b3c262
VZ
2018
2019 int state = 0;
87f0efe2 2020 if (m_selection.Index(item) != wxNOT_FOUND)
64b3c262 2021 state |= wxDATAVIEW_CELL_SELECTED;
87f0efe2 2022
9861f022
RR
2023 // TODO: it would be much more efficient to create a clipping
2024 // region for the entire column being rendered (in the OnPaint
2025 // of wxDataViewMainWindow) instead of a single clip region for
2026 // each cell. However it would mean that each renderer should
2027 // respect the given wxRect's top & bottom coords, eventually
2028 // violating only the left & right coords - however the user can
2029 // make its own renderer and thus we cannot be sure of that.
2030 dc.SetClippingRegion( item_rect );
64b3c262 2031 cell->Render( item_rect, &dc, state );
9861f022 2032 dc.DestroyClippingRegion();
90675b95 2033 }
f554a14b 2034
90675b95
RR
2035 cell_rect.x += cell_rect.width;
2036 }
4ed7af08
RR
2037}
2038
9861f022 2039int wxDataViewMainWindow::GetCountPerPage() const
cab07038
RR
2040{
2041 wxSize size = GetClientSize();
2042 return size.y / m_lineHeight;
2043}
2044
9861f022 2045int wxDataViewMainWindow::GetEndOfLastCol() const
e21f75bd
RR
2046{
2047 int width = 0;
0a71f9e9 2048 unsigned int i;
9861f022 2049 for (i = 0; i < GetOwner()->GetColumnCount(); i++)
e21f75bd 2050 {
9861f022
RR
2051 const wxDataViewColumn *c =
2052 wx_const_cast(wxDataViewCtrl*, GetOwner())->GetColumn( i );
2053
2054 if (!c->IsHidden())
2055 width += c->GetWidth();
e21f75bd
RR
2056 }
2057 return width;
2058}
2059
9861f022 2060unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
72664514
RR
2061{
2062 int x = 0;
2063 int y = 0;
2064 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
120b9b05 2065
72664514
RR
2066 return y / m_lineHeight;
2067}
2068
9861f022 2069unsigned int wxDataViewMainWindow::GetLastVisibleRow() const
72664514
RR
2070{
2071 wxSize client_size = GetClientSize();
87f0efe2
RR
2072 m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
2073 &client_size.x, &client_size.y );
72664514 2074
5637e131 2075 return wxMin( GetRowCount()-1, ((unsigned)client_size.y/m_lineHeight)+1 );
72664514
RR
2076}
2077
9861f022 2078unsigned int wxDataViewMainWindow::GetRowCount() const
cab07038 2079{
9861f022 2080 return wx_const_cast(wxDataViewCtrl*, GetOwner())->GetModel()->GetRowCount();
cab07038
RR
2081}
2082
0a71f9e9 2083void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
e21f75bd
RR
2084{
2085 m_currentRow = row;
120b9b05 2086
e21f75bd
RR
2087 // send event
2088}
2089
cab07038
RR
2090void wxDataViewMainWindow::SelectAllRows( bool on )
2091{
4a851b11
VZ
2092 if (IsEmpty())
2093 return;
120b9b05 2094
cab07038
RR
2095 if (on)
2096 {
72664514 2097 m_selection.Clear();
0a71f9e9 2098 for (unsigned int i = 0; i < GetRowCount(); i++)
cab07038 2099 m_selection.Add( i );
72664514
RR
2100 Refresh();
2101 }
2102 else
2103 {
0a71f9e9
RR
2104 unsigned int first_visible = GetFirstVisibleRow();
2105 unsigned int last_visible = GetLastVisibleRow();
2106 unsigned int i;
72664514 2107 for (i = 0; i < m_selection.GetCount(); i++)
120b9b05 2108 {
0a71f9e9 2109 unsigned int row = m_selection[i];
72664514
RR
2110 if ((row >= first_visible) && (row <= last_visible))
2111 RefreshRow( row );
2112 }
2113 m_selection.Clear();
cab07038 2114 }
cab07038
RR
2115}
2116
0a71f9e9 2117void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
cab07038
RR
2118{
2119 if (m_selection.Index( row ) == wxNOT_FOUND)
2120 {
2121 if (on)
2122 {
2123 m_selection.Add( row );
2124 RefreshRow( row );
2125 }
2126 }
2127 else
2128 {
2129 if (!on)
2130 {
2131 m_selection.Remove( row );
2132 RefreshRow( row );
2133 }
2134 }
2135}
2136
0a71f9e9 2137void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
cab07038
RR
2138{
2139 if (from > to)
2140 {
0a71f9e9 2141 unsigned int tmp = from;
cab07038
RR
2142 from = to;
2143 to = tmp;
2144 }
2145
0a71f9e9 2146 unsigned int i;
cab07038
RR
2147 for (i = from; i <= to; i++)
2148 {
2149 if (m_selection.Index( i ) == wxNOT_FOUND)
2150 {
2151 if (on)
2152 m_selection.Add( i );
2153 }
2154 else
2155 {
2156 if (!on)
2157 m_selection.Remove( i );
2158 }
2159 }
2160 RefreshRows( from, to );
2161}
2162
87f0efe2
RR
2163void wxDataViewMainWindow::Select( const wxArrayInt& aSelections )
2164{
2165 for (size_t i=0; i < aSelections.GetCount(); i++)
2166 {
2167 int n = aSelections[i];
2168
2169 m_selection.Add( n );
2170 RefreshRow( n );
2171 }
2172}
2173
0a71f9e9 2174void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
cab07038
RR
2175{
2176 if (m_selection.Index( row ) == wxNOT_FOUND)
2177 m_selection.Add( row );
2178 else
2179 m_selection.Remove( row );
120b9b05 2180 RefreshRow( row );
cab07038
RR
2181}
2182
0a71f9e9 2183bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
cab07038
RR
2184{
2185 return (m_selection.Index( row ) != wxNOT_FOUND);
2186}
2187
0a71f9e9 2188void wxDataViewMainWindow::RefreshRow( unsigned int row )
cab07038 2189{
e21f75bd 2190 wxRect rect( 0, row*m_lineHeight, GetEndOfLastCol(), m_lineHeight );
cab07038 2191 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
120b9b05 2192
cab07038
RR
2193 wxSize client_size = GetClientSize();
2194 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2195 wxRect intersect_rect = client_rect.Intersect( rect );
2196 if (intersect_rect.width > 0)
2197 Refresh( true, &intersect_rect );
2198}
2199
0a71f9e9 2200void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
cab07038
RR
2201{
2202 if (from > to)
2203 {
0a71f9e9 2204 unsigned int tmp = to;
cab07038
RR
2205 to = from;
2206 from = tmp;
2207 }
2208
e21f75bd 2209 wxRect rect( 0, from*m_lineHeight, GetEndOfLastCol(), (to-from+1) * m_lineHeight );
cab07038 2210 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
120b9b05 2211
cab07038
RR
2212 wxSize client_size = GetClientSize();
2213 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2214 wxRect intersect_rect = client_rect.Intersect( rect );
2215 if (intersect_rect.width > 0)
2216 Refresh( true, &intersect_rect );
2217}
2218
0a71f9e9 2219void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
cab07038 2220{
0a71f9e9 2221 unsigned int count = GetRowCount();
4a851b11
VZ
2222 if (firstRow > count)
2223 return;
120b9b05 2224
e21f75bd 2225 wxRect rect( 0, firstRow*m_lineHeight, GetEndOfLastCol(), count * m_lineHeight );
cab07038 2226 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
120b9b05 2227
cab07038
RR
2228 wxSize client_size = GetClientSize();
2229 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2230 wxRect intersect_rect = client_rect.Intersect( rect );
2231 if (intersect_rect.width > 0)
2232 Refresh( true, &intersect_rect );
2233}
2234
0a71f9e9 2235void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event)
cab07038 2236{
4a851b11 2237 wxCHECK_RET( newCurrent < GetRowCount(),
cab07038
RR
2238 _T("invalid item index in OnArrowChar()") );
2239
2240 // if there is no selection, we cannot move it anywhere
2241 if (!HasCurrentRow())
2242 return;
2243
0a71f9e9 2244 unsigned int oldCurrent = m_currentRow;
cab07038
RR
2245
2246 // in single selection we just ignore Shift as we can't select several
2247 // items anyhow
e21f75bd 2248 if ( event.ShiftDown() && !IsSingleSel() )
cab07038
RR
2249 {
2250 RefreshRow( oldCurrent );
120b9b05 2251
e21f75bd 2252 ChangeCurrentRow( newCurrent );
cab07038
RR
2253
2254 // select all the items between the old and the new one
2255 if ( oldCurrent > newCurrent )
2256 {
2257 newCurrent = oldCurrent;
2258 oldCurrent = m_currentRow;
2259 }
2260
2261 SelectRows( oldCurrent, newCurrent, true );
2262 }
2263 else // !shift
2264 {
72664514 2265 RefreshRow( oldCurrent );
120b9b05 2266
cab07038
RR
2267 // all previously selected items are unselected unless ctrl is held
2268 if ( !event.ControlDown() )
2269 SelectAllRows(false);
2270
e21f75bd 2271 ChangeCurrentRow( newCurrent );
cab07038
RR
2272
2273 if ( !event.ControlDown() )
2274 SelectRow( m_currentRow, true );
72664514
RR
2275 else
2276 RefreshRow( m_currentRow );
cab07038
RR
2277 }
2278
9861f022
RR
2279 //EnsureVisible( m_currentRow );
2280}
2281
2282wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
2283{
2284 wxRect rect;
2285 rect.x = 0;
2286 rect.y = m_lineHeight * row;
2287 rect.width = GetEndOfLastCol();
2288 rect.height = m_lineHeight;
2289
2290 return rect;
cab07038
RR
2291}
2292
2293void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
2294{
2295 if (event.GetKeyCode() == WXK_TAB)
2296 {
2297 wxNavigationKeyEvent nevent;
2298 nevent.SetWindowChange( event.ControlDown() );
2299 nevent.SetDirection( !event.ShiftDown() );
2300 nevent.SetEventObject( GetParent()->GetParent() );
2301 nevent.SetCurrentFocus( m_parent );
2302 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
2303 return;
2304 }
2305
2306 // no item -> nothing to do
2307 if (!HasCurrentRow())
2308 {
2309 event.Skip();
2310 return;
2311 }
120b9b05 2312
cab07038
RR
2313 // don't use m_linesPerPage directly as it might not be computed yet
2314 const int pageSize = GetCountPerPage();
2315 wxCHECK_RET( pageSize, _T("should have non zero page size") );
2316
2317 switch ( event.GetKeyCode() )
2318 {
2319 case WXK_UP:
2320 if ( m_currentRow > 0 )
2321 OnArrowChar( m_currentRow - 1, event );
2322 break;
2323
2324 case WXK_DOWN:
4a851b11 2325 if ( m_currentRow < GetRowCount() - 1 )
cab07038
RR
2326 OnArrowChar( m_currentRow + 1, event );
2327 break;
2328
2329 case WXK_END:
2330 if (!IsEmpty())
2331 OnArrowChar( GetRowCount() - 1, event );
2332 break;
2333
2334 case WXK_HOME:
2335 if (!IsEmpty())
2336 OnArrowChar( 0, event );
2337 break;
2338
2339 case WXK_PAGEUP:
2340 {
2341 int steps = pageSize - 1;
2342 int index = m_currentRow - steps;
2343 if (index < 0)
2344 index = 0;
2345
2346 OnArrowChar( index, event );
2347 }
2348 break;
2349
2350 case WXK_PAGEDOWN:
2351 {
2352 int steps = pageSize - 1;
0a71f9e9
RR
2353 unsigned int index = m_currentRow + steps;
2354 unsigned int count = GetRowCount();
cab07038
RR
2355 if ( index >= count )
2356 index = count - 1;
2357
2358 OnArrowChar( index, event );
2359 }
2360 break;
2361
2362 default:
2363 event.Skip();
2364 }
2365}
2366
4ed7af08
RR
2367void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
2368{
e21f75bd
RR
2369 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2370 {
2371 // let the base handle mouse wheel events.
2372 event.Skip();
2373 return;
2374 }
2375
0fdc2321
RR
2376 int x = event.GetX();
2377 int y = event.GetY();
2378 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
2379
2380 wxDataViewColumn *col = NULL;
2381
2382 int xpos = 0;
9861f022 2383 unsigned int cols = GetOwner()->GetColumnCount();
0a71f9e9 2384 unsigned int i;
0fdc2321
RR
2385 for (i = 0; i < cols; i++)
2386 {
2387 wxDataViewColumn *c = GetOwner()->GetColumn( i );
9861f022
RR
2388 if (c->IsHidden())
2389 continue; // skip it!
2390
0fdc2321
RR
2391 if (x < xpos + c->GetWidth())
2392 {
2393 col = c;
2394 break;
2395 }
2396 xpos += c->GetWidth();
2397 }
f554a14b 2398 if (!col)
0fdc2321 2399 return;
baa9ebc4 2400 wxDataViewRenderer *cell = col->GetRenderer();
f554a14b 2401
0a71f9e9 2402 unsigned int current = y / m_lineHeight;
120b9b05 2403
e21f75bd
RR
2404 if ((current > GetRowCount()) || (x > GetEndOfLastCol()))
2405 {
2406 // Unselect all if below the last row ?
2407 return;
2408 }
f554a14b 2409
0fdc2321
RR
2410 wxDataViewListModel *model = GetOwner()->GetModel();
2411
e21f75bd
RR
2412 if (event.Dragging())
2413 {
2414 if (m_dragCount == 0)
2415 {
2416 // we have to report the raw, physical coords as we want to be
2417 // able to call HitTest(event.m_pointDrag) from the user code to
2418 // get the item being dragged
2419 m_dragStart = event.GetPosition();
2420 }
2421
2422 m_dragCount++;
2423
2424 if (m_dragCount != 3)
2425 return;
2426
2427 if (event.LeftIsDown())
2428 {
2429 // Notify cell about drag
2430 }
2431 return;
2432 }
2433 else
2434 {
2435 m_dragCount = 0;
2436 }
2437
2438 bool forceClick = false;
2439
0fcce6b9
RR
2440 if (event.ButtonDClick())
2441 {
2442 m_renameTimer->Stop();
2443 m_lastOnSame = false;
2444 }
2445
0fdc2321
RR
2446 if (event.LeftDClick())
2447 {
e21f75bd 2448 if ( current == m_lineLastClicked )
0fdc2321 2449 {
e21f75bd
RR
2450 if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE)
2451 {
2452 wxVariant value;
2453 model->GetValue( value, col->GetModelColumn(), current );
2454 cell->SetValue( value );
87f0efe2
RR
2455 wxRect cell_rect( xpos, current * m_lineHeight,
2456 col->GetWidth(), m_lineHeight );
e21f75bd
RR
2457 cell->Activate( cell_rect, model, col->GetModelColumn(), current );
2458 }
2459 return;
2460 }
2461 else
2462 {
2463 // The first click was on another item, so don't interpret this as
2464 // a double click, but as a simple click instead
2465 forceClick = true;
0fdc2321 2466 }
120b9b05 2467 }
f554a14b 2468
0fcce6b9
RR
2469 if (event.LeftUp())
2470 {
0a71f9e9 2471 if (m_lineSelectSingleOnUp != (unsigned int)-1)
e21f75bd
RR
2472 {
2473 // select single line
2474 SelectAllRows( false );
2475 SelectRow( m_lineSelectSingleOnUp, true );
2476 }
120b9b05 2477
0fcce6b9
RR
2478 if (m_lastOnSame)
2479 {
a8461d31 2480 if ((col == m_currentCol) && (current == m_currentRow) &&
0fcce6b9
RR
2481 (cell->GetMode() == wxDATAVIEW_CELL_EDITABLE) )
2482 {
2483 m_renameTimer->Start( 100, true );
2484 }
2485 }
2486
2487 m_lastOnSame = false;
0a71f9e9 2488 m_lineSelectSingleOnUp = (unsigned int)-1;
120b9b05 2489 }
e21f75bd
RR
2490 else
2491 {
2492 // This is necessary, because after a DnD operation in
2493 // from and to ourself, the up event is swallowed by the
2494 // DnD code. So on next non-up event (which means here and
2495 // now) m_lineSelectSingleOnUp should be reset.
0a71f9e9 2496 m_lineSelectSingleOnUp = (unsigned int)-1;
e21f75bd
RR
2497 }
2498
2499 if (event.RightDown())
2500 {
2501 m_lineBeforeLastClicked = m_lineLastClicked;
2502 m_lineLastClicked = current;
2503
2504 // If the item is already selected, do not update the selection.
2505 // Multi-selections should not be cleared if a selected item is clicked.
2506 if (!IsRowSelected(current))
2507 {
2508 SelectAllRows(false);
2509 ChangeCurrentRow(current);
2510 SelectRow(m_currentRow,true);
2511 }
2512
2513 // notify cell about right click
2514 // cell->...
120b9b05 2515
e21f75bd
RR
2516 // Allow generation of context menu event
2517 event.Skip();
2518 }
2519 else if (event.MiddleDown())
2520 {
2521 // notify cell about middle click
2522 // cell->...
2523 }
2524 if (event.LeftDown() || forceClick)
0fcce6b9 2525 {
72664514
RR
2526#ifdef __WXMSW__
2527 SetFocus();
2528#endif
120b9b05 2529
e21f75bd
RR
2530 m_lineBeforeLastClicked = m_lineLastClicked;
2531 m_lineLastClicked = current;
2532
0a71f9e9 2533 unsigned int oldCurrentRow = m_currentRow;
e21f75bd
RR
2534 bool oldWasSelected = IsRowSelected(m_currentRow);
2535
2536 bool cmdModifierDown = event.CmdDown();
2537 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
2538 {
2539 if ( IsSingleSel() || !IsRowSelected(current) )
2540 {
2541 SelectAllRows( false );
2542
2543 ChangeCurrentRow(current);
2544
2545 SelectRow(m_currentRow,true);
2546 }
2547 else // multi sel & current is highlighted & no mod keys
2548 {
2549 m_lineSelectSingleOnUp = current;
2550 ChangeCurrentRow(current); // change focus
2551 }
2552 }
2553 else // multi sel & either ctrl or shift is down
2554 {
2555 if (cmdModifierDown)
2556 {
2557 ChangeCurrentRow(current);
2558
2559 ReverseRowSelection(m_currentRow);
2560 }
2561 else if (event.ShiftDown())
2562 {
2563 ChangeCurrentRow(current);
2564
0a71f9e9 2565 unsigned int lineFrom = oldCurrentRow,
e21f75bd
RR
2566 lineTo = current;
2567
2568 if ( lineTo < lineFrom )
2569 {
2570 lineTo = lineFrom;
2571 lineFrom = m_currentRow;
2572 }
2573
2574 SelectRows(lineFrom, lineTo, true);
2575 }
2576 else // !ctrl, !shift
2577 {
2578 // test in the enclosing if should make it impossible
2579 wxFAIL_MSG( _T("how did we get here?") );
2580 }
2581 }
2582
72664514
RR
2583 if (m_currentRow != oldCurrentRow)
2584 RefreshRow( oldCurrentRow );
e21f75bd 2585
0fcce6b9 2586 wxDataViewColumn *oldCurrentCol = m_currentCol;
120b9b05 2587
0fcce6b9
RR
2588 // Update selection here...
2589 m_currentCol = col;
120b9b05 2590
87f0efe2
RR
2591 m_lastOnSame = !forceClick && ((col == oldCurrentCol) &&
2592 (current == oldCurrentRow)) && oldWasSelected;
0fdc2321 2593 }
4ed7af08
RR
2594}
2595
2596void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
2597{
cab07038 2598 m_hasFocus = true;
120b9b05 2599
cab07038
RR
2600 if (HasCurrentRow())
2601 Refresh();
120b9b05 2602
cab07038
RR
2603 event.Skip();
2604}
2605
2606void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
2607{
2608 m_hasFocus = false;
120b9b05 2609
cab07038
RR
2610 if (HasCurrentRow())
2611 Refresh();
120b9b05 2612
4ed7af08
RR
2613 event.Skip();
2614}
2615
2616//-----------------------------------------------------------------------------
2617// wxDataViewCtrl
2618//-----------------------------------------------------------------------------
2619
2620IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
2621
4b3feaa7
RR
2622BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
2623 EVT_SIZE(wxDataViewCtrl::OnSize)
2624END_EVENT_TABLE()
2625
4ed7af08
RR
2626wxDataViewCtrl::~wxDataViewCtrl()
2627{
2628 if (m_notifier)
2629 GetModel()->RemoveNotifier( m_notifier );
2630}
2631
2632void wxDataViewCtrl::Init()
2633{
2634 m_notifier = NULL;
2635}
2636
2637bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
f554a14b 2638 const wxPoint& pos, const wxSize& size,
4ed7af08
RR
2639 long style, const wxValidator& validator )
2640{
87f0efe2
RR
2641 if (!wxControl::Create( parent, id, pos, size,
2642 style | wxScrolledWindowStyle|wxSUNKEN_BORDER, validator))
4b3feaa7
RR
2643 return false;
2644
4ed7af08 2645 Init();
f554a14b 2646
4ed7af08
RR
2647#ifdef __WXMAC__
2648 MacSetClipChildren( true ) ;
2649#endif
2650
f554a14b 2651 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
9861f022
RR
2652
2653 if (HasFlag(wxDV_NO_HEADER))
2654 m_headerArea = NULL;
2655 else
2656 m_headerArea = new wxDataViewHeaderWindow( this, wxID_ANY );
f554a14b 2657
4ed7af08 2658 SetTargetWindow( m_clientArea );
f554a14b 2659
4ed7af08 2660 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
9861f022
RR
2661 if (m_headerArea)
2662 sizer->Add( m_headerArea, 0, wxGROW );
4ed7af08
RR
2663 sizer->Add( m_clientArea, 1, wxGROW );
2664 SetSizer( sizer );
2665
2666 return true;
2667}
2668
2669#ifdef __WXMSW__
2670WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
2671 WXWPARAM wParam,
2672 WXLPARAM lParam)
2673{
b910a8ad 2674 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4ed7af08
RR
2675
2676#ifndef __WXWINCE__
2677 // we need to process arrows ourselves for scrolling
2678 if ( nMsg == WM_GETDLGCODE )
2679 {
2680 rc |= DLGC_WANTARROWS;
2681 }
2682#endif
2683
2684 return rc;
2685}
2686#endif
2687
f554a14b 2688void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
4ed7af08 2689{
4b3feaa7
RR
2690 // We need to override OnSize so that our scrolled
2691 // window a) does call Layout() to use sizers for
2692 // positioning the controls but b) does not query
2693 // the sizer for their size and use that for setting
2694 // the scrollable area as set that ourselves by
2695 // calling SetScrollbar() further down.
2696
2697 Layout();
2698
2699 AdjustScrollbars();
4ed7af08
RR
2700}
2701
2702bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model )
2703{
2704 if (!wxDataViewCtrlBase::AssociateModel( model ))
2705 return false;
2706
a0f3af5f 2707 m_notifier = new wxGenericDataViewListModelNotifier( m_clientArea );
4ed7af08 2708
f554a14b 2709 model->AddNotifier( m_notifier );
4ed7af08 2710
4b3feaa7 2711 m_clientArea->UpdateDisplay();
f554a14b 2712
4ed7af08
RR
2713 return true;
2714}
2715
2716bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
2717{
2718 if (!wxDataViewCtrlBase::AppendColumn(col))
2719 return false;
f554a14b 2720
9861f022 2721 OnColumnChange();
4ed7af08
RR
2722 return true;
2723}
2724
9861f022
RR
2725void wxDataViewCtrl::OnColumnChange()
2726{
2727 if (m_headerArea)
2728 m_headerArea->UpdateDisplay();
2729
2730 m_clientArea->UpdateDisplay();
2731}
2732
87f0efe2 2733void wxDataViewCtrl::SetSelection( int row )
6ff7eee7 2734{
87f0efe2 2735 m_clientArea->SelectRow(row, true);
6ff7eee7
RR
2736}
2737
87f0efe2 2738void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
6ff7eee7 2739{
87f0efe2 2740 m_clientArea->SelectRows(from, to, true);
6ff7eee7
RR
2741}
2742
87f0efe2 2743void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections )
6ff7eee7 2744{
87f0efe2 2745 m_clientArea->Select(aSelections);
6ff7eee7 2746}
df387169
WS
2747
2748void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) )
fc211fe5 2749{
df387169 2750 // FIXME - TODO
fc211fe5
RR
2751}
2752
df387169 2753bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const
6ff7eee7 2754{
df387169
WS
2755 // FIXME - TODO
2756
6ff7eee7
RR
2757 return false;
2758}
2759
2760int wxDataViewCtrl::GetSelection() const
2761{
df387169
WS
2762 // FIXME - TODO
2763
6ff7eee7
RR
2764 return -1;
2765}
2766
df387169 2767int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const
6ff7eee7 2768{
df387169
WS
2769 // FIXME - TODO
2770
6ff7eee7
RR
2771 return 0;
2772}
2773
f554a14b 2774#endif
4ed7af08
RR
2775 // !wxUSE_GENERICDATAVIEWCTRL
2776
f554a14b 2777#endif
4ed7af08 2778 // wxUSE_DATAVIEWCTRL