]>
Commit | Line | Data |
---|---|---|
4ed7af08 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: datavgen.cpp | |
3 | // Purpose: wxDataViewCtrl generic implementation | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #include "wx/defs.h" | |
14 | ||
15 | #if wxUSE_DATAVIEWCTRL | |
16 | ||
17 | #include "wx/dataview.h" | |
18 | ||
19 | #ifdef wxUSE_GENERICDATAVIEWCTRL | |
20 | ||
21 | #include "wx/stockitem.h" | |
22 | #include "wx/dcclient.h" | |
23 | #include "wx/calctrl.h" | |
24 | #include "wx/popupwin.h" | |
25 | #include "wx/sizer.h" | |
26 | #include "wx/log.h" | |
27 | #include "wx/renderer.h" | |
28 | ||
29 | #ifdef __WXMSW__ | |
30 | #include <windows.h> // for DLGC_WANTARROWS | |
31 | #include "wx/msw/winundef.h" | |
32 | #endif | |
33 | ||
34 | //----------------------------------------------------------------------------- | |
35 | // classes | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | class wxDataViewCtrl; | |
39 | ||
a0f3af5f RR |
40 | //----------------------------------------------------------------------------- |
41 | // wxDataViewHeaderWindow | |
42 | //----------------------------------------------------------------------------- | |
4ed7af08 | 43 | |
a0f3af5f | 44 | class wxDataViewHeaderWindow: public wxWindow |
4ed7af08 RR |
45 | { |
46 | public: | |
a0f3af5f RR |
47 | wxDataViewHeaderWindow( wxDataViewCtrl *parent, |
48 | wxWindowID id, | |
49 | const wxPoint &pos = wxDefaultPosition, | |
50 | const wxSize &size = wxDefaultSize, | |
51 | const wxString &name = wxT("wxdataviewctrlheaderwindow") ); | |
52 | ~wxDataViewHeaderWindow(); | |
4ed7af08 | 53 | |
a0f3af5f RR |
54 | void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; } |
55 | wxDataViewCtrl *GetOwner() { return m_owner; } | |
4ed7af08 | 56 | |
a0f3af5f RR |
57 | void OnPaint( wxPaintEvent &event ); |
58 | void OnMouse( wxMouseEvent &event ); | |
59 | void OnSetFocus( wxFocusEvent &event ); | |
4ed7af08 | 60 | |
a0f3af5f RR |
61 | private: |
62 | wxDataViewCtrl *m_owner; | |
63 | wxCursor *m_resizeCursor; | |
4ed7af08 | 64 | |
a0f3af5f RR |
65 | private: |
66 | DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindow) | |
67 | DECLARE_EVENT_TABLE() | |
68 | }; | |
4ed7af08 | 69 | |
a0f3af5f RR |
70 | //----------------------------------------------------------------------------- |
71 | // wxDataViewMainWindow | |
72 | //----------------------------------------------------------------------------- | |
4ed7af08 | 73 | |
a0f3af5f | 74 | class wxDataViewMainWindow: public wxWindow |
4ed7af08 | 75 | { |
a0f3af5f RR |
76 | public: |
77 | wxDataViewMainWindow( wxDataViewCtrl *parent, | |
78 | wxWindowID id, | |
79 | const wxPoint &pos = wxDefaultPosition, | |
80 | const wxSize &size = wxDefaultSize, | |
81 | const wxString &name = wxT("wxdataviewctrlmainwindow") ); | |
82 | ~wxDataViewMainWindow(); | |
4ed7af08 | 83 | |
a0f3af5f RR |
84 | // notifications from wxDataViewListModel |
85 | bool RowAppended(); | |
86 | bool RowPrepended(); | |
87 | bool RowInserted( size_t before ); | |
88 | bool RowDeleted( size_t row ); | |
89 | bool RowChanged( size_t row ); | |
90 | bool ValueChanged( size_t col, size_t row ); | |
91 | bool RowsReordered( size_t *new_order ); | |
92 | bool Cleared(); | |
4ed7af08 | 93 | |
a0f3af5f RR |
94 | void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; } |
95 | wxDataViewCtrl *GetOwner() { return m_owner; } | |
4ed7af08 | 96 | |
a0f3af5f RR |
97 | void OnPaint( wxPaintEvent &event ); |
98 | void OnMouse( wxMouseEvent &event ); | |
99 | void OnSetFocus( wxFocusEvent &event ); | |
4ed7af08 | 100 | |
a0f3af5f RR |
101 | void UpdateDisplay(); |
102 | void RecalculateDisplay(); | |
103 | void OnInternalIdle(); | |
4ed7af08 | 104 | |
a0f3af5f RR |
105 | void ScrollWindow( int dx, int dy, const wxRect *rect ); |
106 | private: | |
107 | wxDataViewCtrl *m_owner; | |
108 | int m_lineHeight; | |
109 | bool m_dirty; | |
110 | ||
111 | private: | |
112 | DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow) | |
113 | DECLARE_EVENT_TABLE() | |
114 | }; | |
4ed7af08 | 115 | |
a0f3af5f RR |
116 | // --------------------------------------------------------- |
117 | // wxGenericDataViewListModelNotifier | |
118 | // --------------------------------------------------------- | |
119 | ||
120 | class wxGenericDataViewListModelNotifier: public wxDataViewListModelNotifier | |
4ed7af08 | 121 | { |
a0f3af5f RR |
122 | public: |
123 | wxGenericDataViewListModelNotifier( wxDataViewMainWindow *mainWindow ) | |
124 | { m_mainWindow = mainWindow; } | |
125 | ||
126 | virtual bool RowAppended() | |
127 | { return m_mainWindow->RowAppended(); } | |
128 | virtual bool RowPrepended() | |
129 | { return m_mainWindow->RowPrepended(); } | |
130 | virtual bool RowInserted( size_t before ) | |
131 | { return m_mainWindow->RowInserted( before ); } | |
132 | virtual bool RowDeleted( size_t row ) | |
133 | { return m_mainWindow->RowDeleted( row ); } | |
134 | virtual bool RowChanged( size_t row ) | |
135 | { return m_mainWindow->RowChanged( row ); } | |
136 | virtual bool ValueChanged( size_t col, size_t row ) | |
137 | { return m_mainWindow->ValueChanged( col, row ); } | |
138 | virtual bool RowsReordered( size_t *new_order ) | |
139 | { return m_mainWindow->RowsReordered( new_order ); } | |
140 | virtual bool Cleared() | |
141 | { return m_mainWindow->Cleared(); } | |
142 | ||
143 | wxDataViewMainWindow *m_mainWindow; | |
144 | }; | |
4ed7af08 RR |
145 | |
146 | // --------------------------------------------------------- | |
147 | // wxDataViewCell | |
148 | // --------------------------------------------------------- | |
149 | ||
150 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell, wxDataViewCellBase) | |
151 | ||
152 | wxDataViewCell::wxDataViewCell( const wxString &varianttype, wxDataViewCellMode mode ) : | |
153 | wxDataViewCellBase( varianttype, mode ) | |
154 | { | |
3d9d7cc4 | 155 | m_dc = NULL; |
4ed7af08 RR |
156 | } |
157 | ||
3d9d7cc4 RR |
158 | wxDataViewCell::~wxDataViewCell() |
159 | { | |
160 | if (m_dc) | |
161 | delete m_dc; | |
162 | } | |
163 | ||
164 | wxDC *wxDataViewCell::GetDC() | |
165 | { | |
166 | if (m_dc == NULL) | |
167 | { | |
168 | if (GetOwner() == NULL) | |
169 | return NULL; | |
170 | if (GetOwner()->GetOwner() == NULL) | |
171 | return NULL; | |
172 | m_dc = new wxClientDC( GetOwner()->GetOwner() ); | |
173 | } | |
174 | ||
175 | return m_dc; | |
176 | } | |
177 | ||
178 | // --------------------------------------------------------- | |
179 | // wxDataViewCustomCell | |
180 | // --------------------------------------------------------- | |
181 | ||
182 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell, wxDataViewCell) | |
183 | ||
184 | wxDataViewCustomCell::wxDataViewCustomCell( const wxString &varianttype, | |
185 | wxDataViewCellMode mode ) : | |
186 | wxDataViewCell( varianttype, mode ) | |
187 | { | |
188 | } | |
189 | ||
4ed7af08 RR |
190 | // --------------------------------------------------------- |
191 | // wxDataViewTextCell | |
192 | // --------------------------------------------------------- | |
193 | ||
3d9d7cc4 | 194 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell, wxDataViewCustomCell) |
4ed7af08 RR |
195 | |
196 | wxDataViewTextCell::wxDataViewTextCell( const wxString &varianttype, wxDataViewCellMode mode ) : | |
3d9d7cc4 | 197 | wxDataViewCustomCell( varianttype, mode ) |
4ed7af08 RR |
198 | { |
199 | } | |
200 | ||
201 | bool wxDataViewTextCell::SetValue( const wxVariant &value ) | |
202 | { | |
90675b95 RR |
203 | m_text = value.GetString(); |
204 | ||
205 | return true; | |
4ed7af08 RR |
206 | } |
207 | ||
208 | bool wxDataViewTextCell::GetValue( wxVariant &value ) | |
209 | { | |
210 | return false; | |
211 | } | |
212 | ||
3d9d7cc4 RR |
213 | bool wxDataViewTextCell::Render( wxRect cell, wxDC *dc, int state ) |
214 | { | |
90675b95 RR |
215 | dc->DrawText( m_text, cell.x, cell.y ); |
216 | ||
217 | return true; | |
3d9d7cc4 RR |
218 | } |
219 | ||
220 | wxSize wxDataViewTextCell::GetSize() | |
221 | { | |
222 | return wxSize(80,20); | |
223 | } | |
224 | ||
4ed7af08 RR |
225 | // --------------------------------------------------------- |
226 | // wxDataViewToggleCell | |
227 | // --------------------------------------------------------- | |
228 | ||
3d9d7cc4 | 229 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell, wxDataViewCustomCell) |
4ed7af08 RR |
230 | |
231 | wxDataViewToggleCell::wxDataViewToggleCell( const wxString &varianttype, | |
232 | wxDataViewCellMode mode ) : | |
3d9d7cc4 | 233 | wxDataViewCustomCell( varianttype, mode ) |
4ed7af08 | 234 | { |
90675b95 | 235 | m_toggle = false; |
4ed7af08 RR |
236 | } |
237 | ||
238 | bool wxDataViewToggleCell::SetValue( const wxVariant &value ) | |
239 | { | |
90675b95 RR |
240 | m_toggle = value.GetBool(); |
241 | ||
242 | return true;; | |
4ed7af08 RR |
243 | } |
244 | ||
245 | bool wxDataViewToggleCell::GetValue( wxVariant &value ) | |
246 | { | |
247 | return false; | |
248 | } | |
249 | ||
3d9d7cc4 | 250 | bool wxDataViewToggleCell::Render( wxRect cell, wxDC *dc, int state ) |
4ed7af08 | 251 | { |
90675b95 RR |
252 | // User wxRenderer here |
253 | ||
0fdc2321 RR |
254 | if (GetMode() == wxDATAVIEW_CELL_ACTIVATABLE) |
255 | dc->SetPen( *wxBLACK_PEN ); | |
256 | else | |
257 | dc->SetPen( *wxGREY_PEN ); | |
90675b95 RR |
258 | dc->SetBrush( *wxTRANSPARENT_BRUSH ); |
259 | wxRect rect; | |
260 | rect.x = cell.x + cell.width/2 - 10; | |
261 | rect.width = 20; | |
262 | rect.y = cell.y + cell.height/2 - 10; | |
263 | rect.height = 20; | |
264 | dc->DrawRectangle( rect ); | |
265 | if (m_toggle) | |
266 | { | |
267 | rect.x += 2; | |
268 | rect.y += 2; | |
269 | rect.width -= 4; | |
270 | rect.height -= 4; | |
271 | dc->DrawLine( rect.x, rect.y, rect.x+rect.width, rect.y+rect.height ); | |
272 | dc->DrawLine( rect.x+rect.width, rect.y, rect.x, rect.y+rect.height ); | |
273 | } | |
274 | ||
275 | return true; | |
4ed7af08 RR |
276 | } |
277 | ||
0fdc2321 RR |
278 | bool wxDataViewToggleCell::Activate( wxRect cell, wxDataViewListModel *model, size_t col, size_t row ) |
279 | { | |
280 | bool value = !m_toggle; | |
281 | wxVariant variant = value; | |
282 | model->SetValue( variant, col, row ); | |
283 | model->ValueChanged( col, row ); | |
284 | return true; | |
285 | } | |
286 | ||
3d9d7cc4 | 287 | wxSize wxDataViewToggleCell::GetSize() |
4ed7af08 | 288 | { |
3d9d7cc4 | 289 | return wxSize(20,20); |
4ed7af08 RR |
290 | } |
291 | ||
4ed7af08 RR |
292 | // --------------------------------------------------------- |
293 | // wxDataViewProgressCell | |
294 | // --------------------------------------------------------- | |
295 | ||
296 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell, wxDataViewCustomCell) | |
297 | ||
298 | wxDataViewProgressCell::wxDataViewProgressCell( const wxString &label, | |
299 | const wxString &varianttype, wxDataViewCellMode mode ) : | |
300 | wxDataViewCustomCell( varianttype, mode ) | |
301 | { | |
302 | m_label = label; | |
303 | m_value = 0; | |
304 | } | |
305 | ||
306 | wxDataViewProgressCell::~wxDataViewProgressCell() | |
307 | { | |
308 | } | |
309 | ||
310 | bool wxDataViewProgressCell::SetValue( const wxVariant &value ) | |
311 | { | |
312 | m_value = (long) value; | |
313 | ||
314 | if (m_value < 0) m_value = 0; | |
315 | if (m_value > 100) m_value = 100; | |
316 | ||
317 | return true; | |
318 | } | |
319 | ||
320 | bool wxDataViewProgressCell::Render( wxRect cell, wxDC *dc, int state ) | |
321 | { | |
322 | double pct = (double)m_value / 100.0; | |
323 | wxRect bar = cell; | |
324 | bar.width = (int)(cell.width * pct); | |
325 | dc->SetPen( *wxTRANSPARENT_PEN ); | |
326 | dc->SetBrush( *wxBLUE_BRUSH ); | |
327 | dc->DrawRectangle( bar ); | |
328 | ||
329 | dc->SetBrush( *wxTRANSPARENT_BRUSH ); | |
330 | dc->SetPen( *wxBLACK_PEN ); | |
331 | dc->DrawRectangle( cell ); | |
332 | ||
333 | return true; | |
334 | } | |
335 | ||
336 | wxSize wxDataViewProgressCell::GetSize() | |
337 | { | |
338 | return wxSize(40,12); | |
339 | } | |
340 | ||
341 | // --------------------------------------------------------- | |
342 | // wxDataViewDateCell | |
343 | // --------------------------------------------------------- | |
344 | ||
345 | class wxDataViewDateCellPopupTransient: public wxPopupTransientWindow | |
346 | { | |
347 | public: | |
348 | wxDataViewDateCellPopupTransient( wxWindow* parent, wxDateTime *value, | |
349 | wxDataViewListModel *model, size_t col, size_t row ) : | |
350 | wxPopupTransientWindow( parent, wxBORDER_SIMPLE ) | |
351 | { | |
352 | m_model = model; | |
353 | m_col = col; | |
354 | m_row = row; | |
355 | m_cal = new wxCalendarCtrl( this, -1, *value ); | |
356 | wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); | |
357 | sizer->Add( m_cal, 1, wxGROW ); | |
358 | SetSizer( sizer ); | |
359 | sizer->Fit( this ); | |
360 | } | |
361 | ||
362 | virtual void OnDismiss() | |
363 | { | |
364 | } | |
365 | ||
366 | void OnCalendar( wxCalendarEvent &event ); | |
367 | ||
368 | wxCalendarCtrl *m_cal; | |
369 | wxDataViewListModel *m_model; | |
370 | size_t m_col; | |
371 | size_t m_row; | |
372 | ||
373 | private: | |
374 | DECLARE_EVENT_TABLE() | |
375 | }; | |
376 | ||
377 | BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient,wxPopupTransientWindow) | |
378 | EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar ) | |
379 | END_EVENT_TABLE() | |
380 | ||
381 | void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent &event ) | |
382 | { | |
383 | wxDateTime date = event.GetDate(); | |
384 | wxVariant value = date; | |
385 | m_model->SetValue( value, m_col, m_row ); | |
386 | m_model->ValueChanged( m_col, m_row ); | |
387 | DismissAndNotify(); | |
388 | } | |
389 | ||
390 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell, wxDataViewCustomCell) | |
391 | ||
392 | wxDataViewDateCell::wxDataViewDateCell( const wxString &varianttype, | |
393 | wxDataViewCellMode mode ) : | |
394 | wxDataViewCustomCell( varianttype, mode ) | |
395 | { | |
396 | } | |
397 | ||
398 | bool wxDataViewDateCell::SetValue( const wxVariant &value ) | |
399 | { | |
400 | m_date = value.GetDateTime(); | |
401 | ||
402 | return true; | |
403 | } | |
404 | ||
405 | bool wxDataViewDateCell::Render( wxRect cell, wxDC *dc, int state ) | |
406 | { | |
407 | dc->SetFont( GetOwner()->GetOwner()->GetFont() ); | |
408 | wxString tmp = m_date.FormatDate(); | |
409 | dc->DrawText( tmp, cell.x, cell.y ); | |
410 | ||
411 | return true; | |
412 | } | |
413 | ||
414 | wxSize wxDataViewDateCell::GetSize() | |
415 | { | |
416 | wxDataViewCtrl* view = GetOwner()->GetOwner(); | |
417 | wxString tmp = m_date.FormatDate(); | |
418 | wxCoord x,y,d; | |
419 | view->GetTextExtent( tmp, &x, &y, &d ); | |
420 | return wxSize(x,y+d); | |
421 | } | |
422 | ||
423 | bool wxDataViewDateCell::Activate( wxRect cell, wxDataViewListModel *model, size_t col, size_t row ) | |
424 | { | |
425 | wxVariant variant; | |
426 | model->GetValue( variant, col, row ); | |
427 | wxDateTime value = variant.GetDateTime(); | |
428 | ||
429 | wxDataViewDateCellPopupTransient *popup = new wxDataViewDateCellPopupTransient( | |
430 | GetOwner()->GetOwner()->GetParent(), &value, model, col, row ); | |
431 | wxPoint pos = wxGetMousePosition(); | |
432 | popup->Move( pos ); | |
433 | popup->Layout(); | |
434 | popup->Popup( popup->m_cal ); | |
435 | ||
436 | return true; | |
437 | } | |
438 | ||
439 | // --------------------------------------------------------- | |
440 | // wxDataViewColumn | |
441 | // --------------------------------------------------------- | |
442 | ||
443 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn, wxDataViewColumnBase) | |
444 | ||
445 | wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewCell *cell, | |
446 | size_t model_column, int flags ) : | |
447 | wxDataViewColumnBase( title, cell, model_column, flags ) | |
448 | { | |
4b3feaa7 | 449 | m_width = 80; |
4ed7af08 RR |
450 | } |
451 | ||
452 | wxDataViewColumn::~wxDataViewColumn() | |
453 | { | |
454 | } | |
455 | ||
456 | void wxDataViewColumn::SetTitle( const wxString &title ) | |
457 | { | |
458 | wxDataViewColumnBase::SetTitle( title ); | |
459 | ||
460 | } | |
461 | ||
462 | //----------------------------------------------------------------------------- | |
463 | // wxDataViewHeaderWindow | |
464 | //----------------------------------------------------------------------------- | |
465 | ||
45778c96 | 466 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindow, wxWindow) |
4ed7af08 RR |
467 | |
468 | BEGIN_EVENT_TABLE(wxDataViewHeaderWindow,wxWindow) | |
469 | EVT_PAINT (wxDataViewHeaderWindow::OnPaint) | |
470 | EVT_MOUSE_EVENTS (wxDataViewHeaderWindow::OnMouse) | |
471 | EVT_SET_FOCUS (wxDataViewHeaderWindow::OnSetFocus) | |
472 | END_EVENT_TABLE() | |
473 | ||
474 | wxDataViewHeaderWindow::wxDataViewHeaderWindow( wxDataViewCtrl *parent, wxWindowID id, | |
475 | const wxPoint &pos, const wxSize &size, const wxString &name ) : | |
476 | wxWindow( parent, id, pos, size, 0, name ) | |
477 | { | |
478 | SetOwner( parent ); | |
4b3feaa7 | 479 | |
4ed7af08 RR |
480 | m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE ); |
481 | ||
482 | wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes(); | |
483 | SetOwnForegroundColour( attr.colFg ); | |
484 | SetOwnBackgroundColour( attr.colBg ); | |
485 | if (!m_hasFont) | |
486 | SetOwnFont( attr.font ); | |
487 | } | |
488 | ||
489 | wxDataViewHeaderWindow::~wxDataViewHeaderWindow() | |
490 | { | |
491 | delete m_resizeCursor; | |
492 | } | |
493 | ||
494 | void wxDataViewHeaderWindow::OnPaint( wxPaintEvent &event ) | |
495 | { | |
4b3feaa7 RR |
496 | int w, h; |
497 | GetClientSize( &w, &h ); | |
498 | ||
499 | wxPaintDC dc( this ); | |
4ed7af08 RR |
500 | |
501 | int xpix; | |
502 | m_owner->GetScrollPixelsPerUnit( &xpix, NULL ); | |
503 | ||
504 | int x; | |
505 | m_owner->GetViewStart( &x, NULL ); | |
506 | ||
507 | // account for the horz scrollbar offset | |
508 | dc.SetDeviceOrigin( -x * xpix, 0 ); | |
509 | ||
510 | dc.SetFont( GetFont() ); | |
511 | ||
4b3feaa7 RR |
512 | size_t cols = GetOwner()->GetNumberOfColumns(); |
513 | size_t i; | |
514 | int xpos = 0; | |
515 | for (i = 0; i < cols; i++) | |
516 | { | |
517 | wxDataViewColumn *col = GetOwner()->GetColumn( i ); | |
518 | int width = col->GetWidth(); | |
519 | ||
520 | // the width of the rect to draw: make it smaller to fit entirely | |
521 | // inside the column rect | |
522 | #ifdef __WXMAC__ | |
523 | int cw = width; | |
524 | int ch = h; | |
525 | #else | |
526 | int cw = width - 2; | |
527 | int ch = h - 2; | |
528 | #endif | |
529 | ||
530 | wxRendererNative::Get().DrawHeaderButton | |
531 | ( | |
532 | this, | |
533 | dc, | |
534 | wxRect(xpos, 0, cw, ch), | |
535 | m_parent->IsEnabled() ? 0 | |
536 | : (int)wxCONTROL_DISABLED | |
537 | ); | |
538 | ||
539 | dc.DrawText( col->GetTitle(), xpos+3, 3 ); | |
540 | ||
541 | xpos += width; | |
542 | } | |
4ed7af08 RR |
543 | } |
544 | ||
545 | void wxDataViewHeaderWindow::OnMouse( wxMouseEvent &event ) | |
546 | { | |
547 | } | |
548 | ||
549 | void wxDataViewHeaderWindow::OnSetFocus( wxFocusEvent &event ) | |
550 | { | |
551 | event.Skip(); | |
552 | } | |
553 | ||
554 | //----------------------------------------------------------------------------- | |
555 | // wxDataViewMainWindow | |
556 | //----------------------------------------------------------------------------- | |
557 | ||
45778c96 | 558 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow) |
4ed7af08 RR |
559 | |
560 | BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow) | |
561 | EVT_PAINT (wxDataViewMainWindow::OnPaint) | |
562 | EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse) | |
563 | EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus) | |
564 | END_EVENT_TABLE() | |
565 | ||
566 | wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id, | |
567 | const wxPoint &pos, const wxSize &size, const wxString &name ) : | |
568 | wxWindow( parent, id, pos, size, 0, name ) | |
569 | { | |
570 | SetOwner( parent ); | |
4b3feaa7 RR |
571 | |
572 | // We need to calculate this smartly.. | |
573 | m_lineHeight = 20; | |
574 | ||
575 | UpdateDisplay(); | |
4ed7af08 RR |
576 | } |
577 | ||
578 | wxDataViewMainWindow::~wxDataViewMainWindow() | |
579 | { | |
580 | } | |
581 | ||
a0f3af5f RR |
582 | bool wxDataViewMainWindow::RowAppended() |
583 | { | |
584 | return false; | |
585 | } | |
586 | ||
587 | bool wxDataViewMainWindow::RowPrepended() | |
588 | { | |
589 | return false; | |
590 | } | |
591 | ||
592 | bool wxDataViewMainWindow::RowInserted( size_t before ) | |
593 | { | |
594 | return false; | |
595 | } | |
596 | ||
597 | bool wxDataViewMainWindow::RowDeleted( size_t row ) | |
598 | { | |
599 | return false; | |
600 | } | |
601 | ||
602 | bool wxDataViewMainWindow::RowChanged( size_t row ) | |
603 | { | |
604 | return false; | |
605 | } | |
606 | ||
607 | bool wxDataViewMainWindow::ValueChanged( size_t col, size_t row ) | |
608 | { | |
0fdc2321 RR |
609 | wxRect rect( 0, row*m_lineHeight, 10000, m_lineHeight ); |
610 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
611 | Refresh( true, &rect ); | |
612 | ||
613 | return true; | |
a0f3af5f RR |
614 | } |
615 | ||
616 | bool wxDataViewMainWindow::RowsReordered( size_t *new_order ) | |
617 | { | |
618 | return false; | |
619 | } | |
620 | ||
621 | bool wxDataViewMainWindow::Cleared() | |
622 | { | |
623 | return false; | |
624 | } | |
625 | ||
4b3feaa7 RR |
626 | void wxDataViewMainWindow::UpdateDisplay() |
627 | { | |
628 | m_dirty = true; | |
629 | } | |
630 | ||
631 | void wxDataViewMainWindow::OnInternalIdle() | |
632 | { | |
633 | wxWindow::OnInternalIdle(); | |
634 | ||
635 | if (m_dirty) | |
636 | { | |
637 | RecalculateDisplay(); | |
638 | m_dirty = false; | |
639 | } | |
640 | } | |
641 | ||
642 | void wxDataViewMainWindow::RecalculateDisplay() | |
643 | { | |
644 | wxDataViewListModel *model = GetOwner()->GetModel(); | |
645 | if (!model) | |
646 | { | |
647 | Refresh(); | |
648 | return; | |
649 | } | |
650 | ||
651 | int width = 0; | |
652 | size_t cols = GetOwner()->GetNumberOfColumns(); | |
653 | size_t i; | |
654 | for (i = 0; i < cols; i++) | |
655 | { | |
656 | wxDataViewColumn *col = GetOwner()->GetColumn( i ); | |
657 | width += col->GetWidth(); | |
658 | } | |
659 | ||
660 | int height = model->GetNumberOfRows() * m_lineHeight; | |
661 | ||
662 | SetVirtualSize( width, height ); | |
663 | GetOwner()->SetScrollRate( 10, m_lineHeight ); | |
664 | ||
665 | Refresh(); | |
666 | } | |
667 | ||
668 | void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
669 | { | |
670 | wxWindow::ScrollWindow( dx, dy, rect ); | |
671 | GetOwner()->m_headerArea->ScrollWindow( dx, 0 ); | |
672 | } | |
673 | ||
4ed7af08 RR |
674 | void wxDataViewMainWindow::OnPaint( wxPaintEvent &event ) |
675 | { | |
676 | wxPaintDC dc( this ); | |
677 | ||
4b3feaa7 | 678 | GetOwner()->PrepareDC( dc ); |
4ed7af08 RR |
679 | |
680 | dc.SetFont( GetFont() ); | |
90675b95 RR |
681 | |
682 | wxRect update = GetUpdateRegion().GetBox(); | |
683 | m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y ); | |
4ed7af08 | 684 | |
90675b95 RR |
685 | wxDataViewListModel *model = GetOwner()->GetModel(); |
686 | ||
687 | size_t item_start = update.y / m_lineHeight; | |
688 | size_t item_count = (update.height / m_lineHeight) + 1; | |
689 | ||
690 | wxRect cell_rect; | |
691 | cell_rect.x = 0; | |
692 | cell_rect.height = m_lineHeight; | |
693 | size_t cols = GetOwner()->GetNumberOfColumns(); | |
694 | size_t i; | |
695 | for (i = 0; i < cols; i++) | |
696 | { | |
697 | wxDataViewColumn *col = GetOwner()->GetColumn( i ); | |
698 | wxDataViewCell *cell = col->GetCell(); | |
699 | cell_rect.width = col->GetWidth(); | |
700 | ||
701 | size_t item; | |
702 | for (item = item_start; item <= item_start+item_count; item++) | |
703 | { | |
704 | cell_rect.y = item*m_lineHeight; | |
705 | wxVariant value; | |
706 | model->GetValue( value, col->GetModelColumn(), item ); | |
707 | cell->SetValue( value ); | |
4064f7de RR |
708 | wxSize size = cell->GetSize(); |
709 | // cannot be bigger than allocated space | |
710 | size.x = wxMin( size.x, cell_rect.width ); | |
711 | size.y = wxMin( size.y, cell_rect.height ); | |
712 | // TODO: check for left/right/centre alignment here | |
713 | wxRect item_rect; | |
714 | // for now: centre | |
715 | item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2); | |
716 | item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2); | |
717 | ||
718 | item_rect.width = size.x; | |
719 | item_rect.height= size.y; | |
720 | cell->Render( item_rect, &dc, 0 ); | |
90675b95 RR |
721 | } |
722 | ||
723 | cell_rect.x += cell_rect.width; | |
724 | } | |
4ed7af08 RR |
725 | } |
726 | ||
727 | void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) | |
728 | { | |
0fdc2321 RR |
729 | int x = event.GetX(); |
730 | int y = event.GetY(); | |
731 | m_owner->CalcUnscrolledPosition( x, y, &x, &y ); | |
732 | ||
733 | wxDataViewColumn *col = NULL; | |
734 | ||
735 | int xpos = 0; | |
736 | size_t cols = GetOwner()->GetNumberOfColumns(); | |
737 | size_t i; | |
738 | for (i = 0; i < cols; i++) | |
739 | { | |
740 | wxDataViewColumn *c = GetOwner()->GetColumn( i ); | |
741 | if (x < xpos + c->GetWidth()) | |
742 | { | |
743 | col = c; | |
744 | break; | |
745 | } | |
746 | xpos += c->GetWidth(); | |
747 | } | |
748 | if (!col) | |
749 | return; | |
750 | wxDataViewCell *cell = col->GetCell(); | |
751 | ||
752 | size_t row = y / m_lineHeight; | |
753 | ||
754 | wxDataViewListModel *model = GetOwner()->GetModel(); | |
755 | ||
756 | if (event.LeftDClick()) | |
757 | { | |
758 | if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE) | |
759 | { | |
760 | wxVariant value; | |
761 | model->GetValue( value, col->GetModelColumn(), row ); | |
762 | cell->SetValue( value ); | |
763 | wxRect cell_rect( xpos, row * m_lineHeight, col->GetWidth(), m_lineHeight ); | |
764 | cell->Activate( cell_rect, model, col->GetModelColumn(), row ); | |
765 | } | |
766 | ||
767 | return; | |
768 | } | |
769 | ||
4ed7af08 RR |
770 | event.Skip(); |
771 | } | |
772 | ||
773 | void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event ) | |
774 | { | |
775 | event.Skip(); | |
776 | } | |
777 | ||
778 | //----------------------------------------------------------------------------- | |
779 | // wxDataViewCtrl | |
780 | //----------------------------------------------------------------------------- | |
781 | ||
782 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase) | |
783 | ||
4b3feaa7 RR |
784 | BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase) |
785 | EVT_SIZE(wxDataViewCtrl::OnSize) | |
786 | END_EVENT_TABLE() | |
787 | ||
4ed7af08 RR |
788 | wxDataViewCtrl::~wxDataViewCtrl() |
789 | { | |
790 | if (m_notifier) | |
791 | GetModel()->RemoveNotifier( m_notifier ); | |
792 | } | |
793 | ||
794 | void wxDataViewCtrl::Init() | |
795 | { | |
796 | m_notifier = NULL; | |
797 | } | |
798 | ||
799 | bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id, | |
800 | const wxPoint& pos, const wxSize& size, | |
801 | long style, const wxValidator& validator ) | |
802 | { | |
4b3feaa7 RR |
803 | if (!wxControl::Create( parent, id, pos, size, style | wxScrolledWindowStyle|wxSUNKEN_BORDER, validator)) |
804 | return false; | |
805 | ||
4ed7af08 RR |
806 | Init(); |
807 | ||
808 | #ifdef __WXMAC__ | |
809 | MacSetClipChildren( true ) ; | |
810 | #endif | |
811 | ||
812 | m_clientArea = new wxDataViewMainWindow( this, -1 ); | |
813 | m_headerArea = new wxDataViewHeaderWindow( this, -1, wxDefaultPosition, wxSize(-1,25) ); | |
814 | ||
815 | SetTargetWindow( m_clientArea ); | |
816 | ||
817 | wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL ); | |
818 | sizer->Add( m_headerArea, 0, wxGROW ); | |
819 | sizer->Add( m_clientArea, 1, wxGROW ); | |
820 | SetSizer( sizer ); | |
821 | ||
822 | return true; | |
823 | } | |
824 | ||
825 | #ifdef __WXMSW__ | |
826 | WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg, | |
827 | WXWPARAM wParam, | |
828 | WXLPARAM lParam) | |
829 | { | |
b910a8ad | 830 | WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam); |
4ed7af08 RR |
831 | |
832 | #ifndef __WXWINCE__ | |
833 | // we need to process arrows ourselves for scrolling | |
834 | if ( nMsg == WM_GETDLGCODE ) | |
835 | { | |
836 | rc |= DLGC_WANTARROWS; | |
837 | } | |
838 | #endif | |
839 | ||
840 | return rc; | |
841 | } | |
842 | #endif | |
843 | ||
4b3feaa7 | 844 | void wxDataViewCtrl::OnSize( wxSizeEvent &event ) |
4ed7af08 | 845 | { |
4b3feaa7 RR |
846 | // We need to override OnSize so that our scrolled |
847 | // window a) does call Layout() to use sizers for | |
848 | // positioning the controls but b) does not query | |
849 | // the sizer for their size and use that for setting | |
850 | // the scrollable area as set that ourselves by | |
851 | // calling SetScrollbar() further down. | |
852 | ||
853 | Layout(); | |
854 | ||
855 | AdjustScrollbars(); | |
4ed7af08 RR |
856 | } |
857 | ||
858 | bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model ) | |
859 | { | |
860 | if (!wxDataViewCtrlBase::AssociateModel( model )) | |
861 | return false; | |
862 | ||
a0f3af5f | 863 | m_notifier = new wxGenericDataViewListModelNotifier( m_clientArea ); |
4ed7af08 RR |
864 | |
865 | model->AddNotifier( m_notifier ); | |
866 | ||
4b3feaa7 RR |
867 | m_clientArea->UpdateDisplay(); |
868 | ||
4ed7af08 RR |
869 | return true; |
870 | } | |
871 | ||
872 | bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col ) | |
873 | { | |
874 | if (!wxDataViewCtrlBase::AppendColumn(col)) | |
875 | return false; | |
4b3feaa7 RR |
876 | |
877 | m_clientArea->UpdateDisplay(); | |
878 | ||
4ed7af08 RR |
879 | return true; |
880 | } | |
881 | ||
882 | #endif | |
883 | // !wxUSE_GENERICDATAVIEWCTRL | |
884 | ||
885 | #endif | |
886 | // wxUSE_DATAVIEWCTRL | |
887 |