]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/grid.cpp
Declare strptime when using MSL headers on OS X.
[wxWidgets.git] / src / generic / grid.cpp
... / ...
Content-type: text/html ]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/grid.cpp


500 - Internal Server Error

Malformed UTF-8 character (fatal) at /usr/lib/x86_64-linux-gnu/perl5/5.40/HTML/Entities.pm line 485, <$fd> line 3856.
CommitLineData
1///////////////////////////////////////////////////////////////////////////
2// Name: generic/grid.cpp
3// Purpose: wxGrid and related classes
4// Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5// Modified by: Robin Dunn, Vadim Zeitlin
6// Created: 1/08/1999
7// RCS-ID: $Id$
8// Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "grid.h"
22#endif
23
24// For compilers that support precompilatixon, includes "wx/wx.h".
25#include "wx/wxprec.h"
26
27#include "wx/defs.h"
28
29#ifdef __BORLANDC__
30 #pragma hdrstop
31#endif
32
33#if wxUSE_GRID
34
35#ifndef WX_PRECOMP
36 #include "wx/utils.h"
37 #include "wx/dcclient.h"
38 #include "wx/settings.h"
39 #include "wx/log.h"
40 #include "wx/textctrl.h"
41 #include "wx/checkbox.h"
42 #include "wx/combobox.h"
43 #include "wx/valtext.h"
44 #include "wx/intl.h"
45#endif
46
47#include "wx/textfile.h"
48#include "wx/spinctrl.h"
49#include "wx/tokenzr.h"
50#include "wx/renderer.h"
51
52#include "wx/grid.h"
53#include "wx/generic/gridsel.h"
54
55#if defined(__WXMOTIF__)
56 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
57#else
58 #define WXUNUSED_MOTIF(identifier) identifier
59#endif
60
61#if defined(__WXGTK__)
62 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
63#else
64 #define WXUNUSED_GTK(identifier) identifier
65#endif
66
67// Required for wxIs... functions
68#include <ctype.h>
69
70// ----------------------------------------------------------------------------
71// array classes
72// ----------------------------------------------------------------------------
73
74WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr *, wxArrayAttrs,
75 class WXDLLIMPEXP_ADV);
76
77struct wxGridCellWithAttr
78{
79 wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
80 : coords(row, col), attr(attr_)
81 {
82 }
83
84 ~wxGridCellWithAttr()
85 {
86 attr->DecRef();
87 }
88
89 wxGridCellCoords coords;
90 wxGridCellAttr *attr;
91
92// Cannot do this:
93// DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
94// without rewriting the macros, which require a public copy constructor.
95};
96
97WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr, wxGridCellWithAttrArray,
98 class WXDLLIMPEXP_ADV);
99
100#include "wx/arrimpl.cpp"
101
102WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
103WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
104
105// ----------------------------------------------------------------------------
106// events
107// ----------------------------------------------------------------------------
108
109DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK)
110DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK)
111DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK)
112DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK)
113DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG)
114DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK)
115DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK)
116DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK)
117DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK)
118DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE)
119DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE)
120DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT)
121DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE)
122DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL)
123DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN)
124DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN)
125DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED)
126
127// ----------------------------------------------------------------------------
128// private classes
129// ----------------------------------------------------------------------------
130
131class WXDLLIMPEXP_ADV wxGridRowLabelWindow : public wxWindow
132{
133public:
134 wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; }
135 wxGridRowLabelWindow( wxGrid *parent, wxWindowID id,
136 const wxPoint &pos, const wxSize &size );
137
138private:
139 wxGrid *m_owner;
140
141 void OnPaint( wxPaintEvent& event );
142 void OnMouseEvent( wxMouseEvent& event );
143 void OnMouseWheel( wxMouseEvent& event );
144 void OnKeyDown( wxKeyEvent& event );
145 void OnKeyUp( wxKeyEvent& );
146 void OnChar( wxKeyEvent& );
147
148 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow)
149 DECLARE_EVENT_TABLE()
150 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow)
151};
152
153
154class WXDLLIMPEXP_ADV wxGridColLabelWindow : public wxWindow
155{
156public:
157 wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; }
158 wxGridColLabelWindow( wxGrid *parent, wxWindowID id,
159 const wxPoint &pos, const wxSize &size );
160
161private:
162 wxGrid *m_owner;
163
164 void OnPaint( wxPaintEvent &event );
165 void OnMouseEvent( wxMouseEvent& event );
166 void OnMouseWheel( wxMouseEvent& event );
167 void OnKeyDown( wxKeyEvent& event );
168 void OnKeyUp( wxKeyEvent& );
169 void OnChar( wxKeyEvent& );
170
171 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow)
172 DECLARE_EVENT_TABLE()
173 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow)
174};
175
176
177class WXDLLIMPEXP_ADV wxGridCornerLabelWindow : public wxWindow
178{
179public:
180 wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; }
181 wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id,
182 const wxPoint &pos, const wxSize &size );
183
184private:
185 wxGrid *m_owner;
186
187 void OnMouseEvent( wxMouseEvent& event );
188 void OnMouseWheel( wxMouseEvent& event );
189 void OnKeyDown( wxKeyEvent& event );
190 void OnKeyUp( wxKeyEvent& );
191 void OnChar( wxKeyEvent& );
192 void OnPaint( wxPaintEvent& event );
193
194 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow)
195 DECLARE_EVENT_TABLE()
196 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow)
197};
198
199class WXDLLIMPEXP_ADV wxGridWindow : public wxWindow
200{
201public:
202 wxGridWindow()
203 {
204 m_owner = (wxGrid *)NULL;
205 m_rowLabelWin = (wxGridRowLabelWindow *)NULL;
206 m_colLabelWin = (wxGridColLabelWindow *)NULL;
207 }
208
209 wxGridWindow( wxGrid *parent,
210 wxGridRowLabelWindow *rowLblWin,
211 wxGridColLabelWindow *colLblWin,
212 wxWindowID id, const wxPoint &pos, const wxSize &size );
213 ~wxGridWindow(){}
214
215 void ScrollWindow( int dx, int dy, const wxRect *rect );
216
217 wxGrid* GetOwner() { return m_owner; }
218
219private:
220 wxGrid *m_owner;
221 wxGridRowLabelWindow *m_rowLabelWin;
222 wxGridColLabelWindow *m_colLabelWin;
223
224 void OnPaint( wxPaintEvent &event );
225 void OnMouseWheel( wxMouseEvent& event );
226 void OnMouseEvent( wxMouseEvent& event );
227 void OnKeyDown( wxKeyEvent& );
228 void OnKeyUp( wxKeyEvent& );
229 void OnChar( wxKeyEvent& );
230 void OnEraseBackground( wxEraseEvent& );
231 void OnFocus( wxFocusEvent& );
232
233 DECLARE_DYNAMIC_CLASS(wxGridWindow)
234 DECLARE_EVENT_TABLE()
235 DECLARE_NO_COPY_CLASS(wxGridWindow)
236};
237
238
239
240class wxGridCellEditorEvtHandler : public wxEvtHandler
241{
242public:
243 wxGridCellEditorEvtHandler()
244 : m_grid(0), m_editor(0)
245 { }
246 wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor)
247 : m_grid(grid), m_editor(editor)
248 { }
249
250 void OnKeyDown(wxKeyEvent& event);
251 void OnChar(wxKeyEvent& event);
252
253private:
254 wxGrid* m_grid;
255 wxGridCellEditor* m_editor;
256 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
257 DECLARE_EVENT_TABLE()
258 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler)
259};
260
261
262IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler )
263BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
264 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
265 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
266END_EVENT_TABLE()
267
268
269
270// ----------------------------------------------------------------------------
271// the internal data representation used by wxGridCellAttrProvider
272// ----------------------------------------------------------------------------
273
274// this class stores attributes set for cells
275class WXDLLIMPEXP_ADV wxGridCellAttrData
276{
277public:
278 void SetAttr(wxGridCellAttr *attr, int row, int col);
279 wxGridCellAttr *GetAttr(int row, int col) const;
280 void UpdateAttrRows( size_t pos, int numRows );
281 void UpdateAttrCols( size_t pos, int numCols );
282
283private:
284 // searches for the attr for given cell, returns wxNOT_FOUND if not found
285 int FindIndex(int row, int col) const;
286
287 wxGridCellWithAttrArray m_attrs;
288};
289
290// this class stores attributes set for rows or columns
291class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
292{
293public:
294 // empty ctor to suppress warnings
295 wxGridRowOrColAttrData() { }
296 ~wxGridRowOrColAttrData();
297
298 void SetAttr(wxGridCellAttr *attr, int rowOrCol);
299 wxGridCellAttr *GetAttr(int rowOrCol) const;
300 void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols );
301
302private:
303 wxArrayInt m_rowsOrCols;
304 wxArrayAttrs m_attrs;
305};
306
307// NB: this is just a wrapper around 3 objects: one which stores cell
308// attributes, and 2 others for row/col ones
309class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
310{
311public:
312 wxGridCellAttrData m_cellAttrs;
313 wxGridRowOrColAttrData m_rowAttrs,
314 m_colAttrs;
315};
316
317
318// ----------------------------------------------------------------------------
319// data structures used for the data type registry
320// ----------------------------------------------------------------------------
321
322struct wxGridDataTypeInfo
323{
324 wxGridDataTypeInfo(const wxString& typeName,
325 wxGridCellRenderer* renderer,
326 wxGridCellEditor* editor)
327 : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
328 { }
329
330 ~wxGridDataTypeInfo()
331 {
332 wxSafeDecRef(m_renderer);
333 wxSafeDecRef(m_editor);
334 }
335
336 wxString m_typeName;
337 wxGridCellRenderer* m_renderer;
338 wxGridCellEditor* m_editor;
339
340 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo)
341};
342
343
344WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo*, wxGridDataTypeInfoArray,
345 class WXDLLIMPEXP_ADV);
346
347
348class WXDLLIMPEXP_ADV wxGridTypeRegistry
349{
350public:
351 wxGridTypeRegistry() {}
352 ~wxGridTypeRegistry();
353
354 void RegisterDataType(const wxString& typeName,
355 wxGridCellRenderer* renderer,
356 wxGridCellEditor* editor);
357
358 // find one of already registered data types
359 int FindRegisteredDataType(const wxString& typeName);
360
361 // try to FindRegisteredDataType(), if this fails and typeName is one of
362 // standard typenames, register it and return its index
363 int FindDataType(const wxString& typeName);
364
365 // try to FindDataType(), if it fails see if it is not one of already
366 // registered data types with some params in which case clone the
367 // registered data type and set params for it
368 int FindOrCloneDataType(const wxString& typeName);
369
370 wxGridCellRenderer* GetRenderer(int index);
371 wxGridCellEditor* GetEditor(int index);
372
373private:
374 wxGridDataTypeInfoArray m_typeinfo;
375};
376
377// ----------------------------------------------------------------------------
378// conditional compilation
379// ----------------------------------------------------------------------------
380
381#ifndef WXGRID_DRAW_LINES
382#define WXGRID_DRAW_LINES 1
383#endif
384
385// ----------------------------------------------------------------------------
386// globals
387// ----------------------------------------------------------------------------
388
389//#define DEBUG_ATTR_CACHE
390#ifdef DEBUG_ATTR_CACHE
391 static size_t gs_nAttrCacheHits = 0;
392 static size_t gs_nAttrCacheMisses = 0;
393#endif // DEBUG_ATTR_CACHE
394
395// ----------------------------------------------------------------------------
396// constants
397// ----------------------------------------------------------------------------
398
399wxGridCellCoords wxGridNoCellCoords( -1, -1 );
400wxRect wxGridNoCellRect( -1, -1, -1, -1 );
401
402// scroll line size
403// TODO: this doesn't work at all, grid cells have different sizes and approx
404// calculations don't work as because of the size mismatch scrollbars
405// sometimes fail to be shown when they should be or vice versa
406//
407// The scroll bars may be a little flakey once in a while, but that is
408// surely much less horrible than having scroll lines of only 1!!!
409// -- Robin
410//
411// Well, it's still seriously broken so it might be better but needs
412// fixing anyhow
413// -- Vadim
414static const size_t GRID_SCROLL_LINE_X = 15; // 1;
415static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X;
416
417// the size of hash tables used a bit everywhere (the max number of elements
418// in these hash tables is the number of rows/columns)
419static const int GRID_HASH_SIZE = 100;
420
421#if 0
422// ----------------------------------------------------------------------------
423// private functions
424// ----------------------------------------------------------------------------
425
426static inline int GetScrollX(int x)
427{
428 return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X;
429}
430
431static inline int GetScrollY(int y)
432{
433 return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y;
434}
435#endif
436
437// ============================================================================
438// implementation
439// ============================================================================
440
441// ----------------------------------------------------------------------------
442// wxGridCellEditor
443// ----------------------------------------------------------------------------
444
445wxGridCellEditor::wxGridCellEditor()
446{
447 m_control = NULL;
448 m_attr = NULL;
449}
450
451
452wxGridCellEditor::~wxGridCellEditor()
453{
454 Destroy();
455}
456
457void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
458 wxWindowID WXUNUSED(id),
459 wxEvtHandler* evtHandler)
460{
461 if ( evtHandler )
462 m_control->PushEventHandler(evtHandler);
463}
464
465void wxGridCellEditor::PaintBackground(const wxRect& rectCell,
466 wxGridCellAttr *attr)
467{
468 // erase the background because we might not fill the cell
469 wxClientDC dc(m_control->GetParent());
470 wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow);
471 if (gridWindow)
472 gridWindow->GetOwner()->PrepareDC(dc);
473
474 dc.SetPen(*wxTRANSPARENT_PEN);
475 dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
476 dc.DrawRectangle(rectCell);
477
478 // redraw the control we just painted over
479 m_control->Refresh();
480}
481
482void wxGridCellEditor::Destroy()
483{
484 if (m_control)
485 {
486 m_control->PopEventHandler(true /* delete it*/);
487
488 m_control->Destroy();
489 m_control = NULL;
490 }
491}
492
493void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr)
494{
495 wxASSERT_MSG(m_control,
496 wxT("The wxGridCellEditor must be Created first!"));
497 m_control->Show(show);
498
499 if ( show )
500 {
501 // set the colours/fonts if we have any
502 if ( attr )
503 {
504 m_colFgOld = m_control->GetForegroundColour();
505 m_control->SetForegroundColour(attr->GetTextColour());
506
507 m_colBgOld = m_control->GetBackgroundColour();
508 m_control->SetBackgroundColour(attr->GetBackgroundColour());
509
510// Workaround for GTK+1 font setting problem on some platforms
511#if !defined(__WXGTK__) || defined(__WXGTK20__)
512 m_fontOld = m_control->GetFont();
513 m_control->SetFont(attr->GetFont());
514#endif
515 // can't do anything more in the base class version, the other
516 // attributes may only be used by the derived classes
517 }
518 }
519 else
520 {
521 // restore the standard colours fonts
522 if ( m_colFgOld.Ok() )
523 {
524 m_control->SetForegroundColour(m_colFgOld);
525 m_colFgOld = wxNullColour;
526 }
527
528 if ( m_colBgOld.Ok() )
529 {
530 m_control->SetBackgroundColour(m_colBgOld);
531 m_colBgOld = wxNullColour;
532 }
533// Workaround for GTK+1 font setting problem on some platforms
534#if !defined(__WXGTK__) || defined(__WXGTK20__)
535 if ( m_fontOld.Ok() )
536 {
537 m_control->SetFont(m_fontOld);
538 m_fontOld = wxNullFont;
539 }
540#endif
541 }
542}
543
544void wxGridCellEditor::SetSize(const wxRect& rect)
545{
546 wxASSERT_MSG(m_control,
547 wxT("The wxGridCellEditor must be Created first!"));
548 m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
549}
550
551void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
552{
553 event.Skip();
554}
555
556bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event)
557{
558 bool ctrl = event.ControlDown();
559 bool alt = event.AltDown();
560#ifdef __WXMAC__
561 // On the Mac the Alt key is more like shift and is used for entry of
562 // valid characters, so check for Ctrl and Meta instead.
563 alt = event.MetaDown();
564#endif
565
566 // Assume it's not a valid char if ctrl or alt is down, but if both are
567 // down then it may be because of an AltGr key combination, so let them
568 // through in that case.
569 if ((ctrl || alt) && !(ctrl && alt))
570 return false;
571
572#if wxUSE_UNICODE
573 int key = event.GetUnicodeKey();
574 bool keyOk = true;
575
576 // if the unicode key code is not really a unicode character (it may
577 // be a function key or etc., the platforms appear to always give us a
578 // small value in this case) then fallback to the ascii key code but
579 // don't do anything for function keys or etc.
580 if (key <= 127)
581 {
582 key = event.GetKeyCode();
583 keyOk = (key <= 127);
584 }
585 return keyOk;
586#else // !wxUSE_UNICODE
587 int key = event.GetKeyCode();
588 if (key <= 255)
589 return true;
590 return false;
591#endif // wxUSE_UNICODE/!wxUSE_UNICODE
592}
593
594void wxGridCellEditor::StartingKey(wxKeyEvent& event)
595{
596 event.Skip();
597}
598
599void wxGridCellEditor::StartingClick()
600{
601}
602
603#if wxUSE_TEXTCTRL
604
605// ----------------------------------------------------------------------------
606// wxGridCellTextEditor
607// ----------------------------------------------------------------------------
608
609wxGridCellTextEditor::wxGridCellTextEditor()
610{
611 m_maxChars = 0;
612}
613
614void wxGridCellTextEditor::Create(wxWindow* parent,
615 wxWindowID id,
616 wxEvtHandler* evtHandler)
617{
618 m_control = new wxTextCtrl(parent, id, wxEmptyString,
619 wxDefaultPosition, wxDefaultSize
620#if defined(__WXMSW__)
621 , wxTE_PROCESS_TAB | wxTE_AUTO_SCROLL
622#endif
623 );
624
625 // set max length allowed in the textctrl, if the parameter was set
626 if (m_maxChars != 0)
627 {
628 ((wxTextCtrl*)m_control)->SetMaxLength(m_maxChars);
629 }
630
631 wxGridCellEditor::Create(parent, id, evtHandler);
632}
633
634void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell),
635 wxGridCellAttr * WXUNUSED(attr))
636{
637 // as we fill the entire client area, don't do anything here to minimize
638 // flicker
639}
640
641void wxGridCellTextEditor::SetSize(const wxRect& rectOrig)
642{
643 wxRect rect(rectOrig);
644
645 // Make the edit control large enough to allow for internal
646 // margins
647 //
648 // TODO: remove this if the text ctrl sizing is improved esp. for
649 // unix
650 //
651#if defined(__WXGTK__)
652 if (rect.x != 0)
653 {
654 rect.x += 1;
655 rect.y += 1;
656 rect.width -= 1;
657 rect.height -= 1;
658 }
659#else // !GTK
660 int extra_x = ( rect.x > 2 )? 2 : 1;
661
662// MB: treat MSW separately here otherwise the caret doesn't show
663// when the editor is in the first row.
664#if defined(__WXMSW__)
665 int extra_y = 2;
666#else
667 int extra_y = ( rect.y > 2 )? 2 : 1;
668#endif // MSW
669
670#if defined(__WXMOTIF__)
671 extra_x *= 2;
672 extra_y *= 2;
673#endif
674 rect.SetLeft( wxMax(0, rect.x - extra_x) );
675 rect.SetTop( wxMax(0, rect.y - extra_y) );
676 rect.SetRight( rect.GetRight() + 2*extra_x );
677 rect.SetBottom( rect.GetBottom() + 2*extra_y );
678#endif // GTK/!GTK
679
680 wxGridCellEditor::SetSize(rect);
681}
682
683void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid)
684{
685 wxASSERT_MSG(m_control,
686 wxT("The wxGridCellEditor must be Created first!"));
687
688 m_startValue = grid->GetTable()->GetValue(row, col);
689
690 DoBeginEdit(m_startValue);
691}
692
693void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
694{
695 Text()->SetValue(startValue);
696 Text()->SetInsertionPointEnd();
697 Text()->SetSelection(-1,-1);
698 Text()->SetFocus();
699}
700
701bool wxGridCellTextEditor::EndEdit(int row, int col,
702 wxGrid* grid)
703{
704 wxASSERT_MSG(m_control,
705 wxT("The wxGridCellEditor must be Created first!"));
706
707 bool changed = false;
708 wxString value = Text()->GetValue();
709 if (value != m_startValue)
710 changed = true;
711
712 if (changed)
713 grid->GetTable()->SetValue(row, col, value);
714
715 m_startValue = wxEmptyString;
716 // No point in setting the text of the hidden control
717 //Text()->SetValue(m_startValue);
718
719 return changed;
720}
721
722
723void wxGridCellTextEditor::Reset()
724{
725 wxASSERT_MSG(m_control,
726 wxT("The wxGridCellEditor must be Created first!"));
727
728 DoReset(m_startValue);
729}
730
731void wxGridCellTextEditor::DoReset(const wxString& startValue)
732{
733 Text()->SetValue(startValue);
734 Text()->SetInsertionPointEnd();
735}
736
737bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event)
738{
739 return wxGridCellEditor::IsAcceptedKey(event);
740}
741
742void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
743{
744 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
745 // longer an appropriate way to get the character into the text control.
746 // Do it ourselves instead. We know that if we get this far that we have
747 // a valid character, so not a whole lot of testing needs to be done.
748
749 wxTextCtrl* tc = Text();
750 wxChar ch;
751 long pos;
752
753#if wxUSE_UNICODE
754 ch = event.GetUnicodeKey();
755 if (ch <= 127)
756 ch = (wxChar)event.GetKeyCode();
757#else
758 ch = (wxChar)event.GetKeyCode();
759#endif
760 switch (ch)
761 {
762 case WXK_DELETE:
763 // delete the character at the cursor
764 pos = tc->GetInsertionPoint();
765 if (pos < tc->GetLastPosition())
766 tc->Remove(pos, pos+1);
767 break;
768
769 case WXK_BACK:
770 // delete the character before the cursor
771 pos = tc->GetInsertionPoint();
772 if (pos > 0)
773 tc->Remove(pos-1, pos);
774 break;
775
776 default:
777 tc->WriteText(ch);
778 break;
779 }
780}
781
782void wxGridCellTextEditor::HandleReturn( wxKeyEvent&
783 WXUNUSED_GTK(WXUNUSED_MOTIF(event)) )
784{
785#if defined(__WXMOTIF__) || defined(__WXGTK__)
786 // wxMotif needs a little extra help...
787 size_t pos = (size_t)( Text()->GetInsertionPoint() );
788 wxString s( Text()->GetValue() );
789 s = s.Left(pos) + wxT("\n") + s.Mid(pos);
790 Text()->SetValue(s);
791 Text()->SetInsertionPoint( pos );
792#else
793 // the other ports can handle a Return key press
794 //
795 event.Skip();
796#endif
797}
798
799void wxGridCellTextEditor::SetParameters(const wxString& params)
800{
801 if ( !params )
802 {
803 // reset to default
804 m_maxChars = 0;
805 }
806 else
807 {
808 long tmp;
809 if ( !params.ToLong(&tmp) )
810 {
811 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str());
812 }
813 else
814 {
815 m_maxChars = (size_t)tmp;
816 }
817 }
818}
819
820// return the value in the text control
821wxString wxGridCellTextEditor::GetValue() const
822{
823 return Text()->GetValue();
824}
825
826// ----------------------------------------------------------------------------
827// wxGridCellNumberEditor
828// ----------------------------------------------------------------------------
829
830wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
831{
832 m_min = min;
833 m_max = max;
834}
835
836void wxGridCellNumberEditor::Create(wxWindow* parent,
837 wxWindowID id,
838 wxEvtHandler* evtHandler)
839{
840#if wxUSE_SPINCTRL
841 if ( HasRange() )
842 {
843 // create a spin ctrl
844 m_control = new wxSpinCtrl(parent, wxID_ANY, wxEmptyString,
845 wxDefaultPosition, wxDefaultSize,
846 wxSP_ARROW_KEYS,
847 m_min, m_max);
848
849 wxGridCellEditor::Create(parent, id, evtHandler);
850 }
851 else
852#endif
853 {
854 // just a text control
855 wxGridCellTextEditor::Create(parent, id, evtHandler);
856
857#if wxUSE_VALIDATORS
858 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
859#endif // wxUSE_VALIDATORS
860 }
861}
862
863void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
864{
865 // first get the value
866 wxGridTableBase *table = grid->GetTable();
867 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
868 {
869 m_valueOld = table->GetValueAsLong(row, col);
870 }
871 else
872 {
873 m_valueOld = 0;
874 wxString sValue = table->GetValue(row, col);
875 if (! sValue.ToLong(&m_valueOld) && ! sValue.empty())
876 {
877 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
878 return;
879 }
880 }
881
882#if wxUSE_SPINCTRL
883 if ( HasRange() )
884 {
885 Spin()->SetValue((int)m_valueOld);
886 Spin()->SetFocus();
887 }
888 else
889#endif
890 {
891 DoBeginEdit(GetString());
892 }
893}
894
895bool wxGridCellNumberEditor::EndEdit(int row, int col,
896 wxGrid* grid)
897{
898 bool changed;
899 long value = 0;
900 wxString text;
901
902#if wxUSE_SPINCTRL
903 if ( HasRange() )
904 {
905 value = Spin()->GetValue();
906 changed = value != m_valueOld;
907 if (changed)
908 text = wxString::Format(wxT("%ld"), value);
909 }
910 else
911#endif
912 {
913 text = Text()->GetValue();
914 changed = (text.empty() || text.ToLong(&value)) && (value != m_valueOld);
915 }
916
917 if ( changed )
918 {
919 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
920 grid->GetTable()->SetValueAsLong(row, col, value);
921 else
922 grid->GetTable()->SetValue(row, col, text);
923 }
924
925 return changed;
926}
927
928void wxGridCellNumberEditor::Reset()
929{
930#if wxUSE_SPINCTRL
931 if ( HasRange() )
932 {
933 Spin()->SetValue((int)m_valueOld);
934 }
935 else
936#endif
937 {
938 DoReset(GetString());
939 }
940}
941
942bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event)
943{
944 if ( wxGridCellEditor::IsAcceptedKey(event) )
945 {
946 int keycode = event.GetKeyCode();
947 if ( (keycode < 128) &&
948 (wxIsdigit(keycode) || keycode == '+' || keycode == '-'))
949 {
950 return true;
951 }
952 }
953
954 return false;
955}
956
957void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
958{
959 if ( !HasRange() )
960 {
961 int keycode = event.GetKeyCode();
962 if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-')
963 {
964 wxGridCellTextEditor::StartingKey(event);
965
966 // skip Skip() below
967 return;
968 }
969 }
970
971 event.Skip();
972}
973
974void wxGridCellNumberEditor::SetParameters(const wxString& params)
975{
976 if ( !params )
977 {
978 // reset to default
979 m_min =
980 m_max = -1;
981 }
982 else
983 {
984 long tmp;
985 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
986 {
987 m_min = (int)tmp;
988
989 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
990 {
991 m_max = (int)tmp;
992
993 // skip the error message below
994 return;
995 }
996 }
997
998 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str());
999 }
1000}
1001
1002// return the value in the spin control if it is there (the text control otherwise)
1003wxString wxGridCellNumberEditor::GetValue() const
1004{
1005 wxString s;
1006
1007#if wxUSE_SPINCTRL
1008 if( HasRange() )
1009 {
1010 long value = Spin()->GetValue();
1011 s.Printf(wxT("%ld"), value);
1012 }
1013 else
1014#endif
1015 {
1016 s = Text()->GetValue();
1017 }
1018
1019 return s;
1020}
1021
1022// ----------------------------------------------------------------------------
1023// wxGridCellFloatEditor
1024// ----------------------------------------------------------------------------
1025
1026wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision)
1027{
1028 m_width = width;
1029 m_precision = precision;
1030}
1031
1032void wxGridCellFloatEditor::Create(wxWindow* parent,
1033 wxWindowID id,
1034 wxEvtHandler* evtHandler)
1035{
1036 wxGridCellTextEditor::Create(parent, id, evtHandler);
1037
1038#if wxUSE_VALIDATORS
1039 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
1040#endif // wxUSE_VALIDATORS
1041}
1042
1043void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
1044{
1045 // first get the value
1046 wxGridTableBase *table = grid->GetTable();
1047 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1048 {
1049 m_valueOld = table->GetValueAsDouble(row, col);
1050 }
1051 else
1052 {
1053 m_valueOld = 0.0;
1054 wxString sValue = table->GetValue(row, col);
1055 if (! sValue.ToDouble(&m_valueOld) && ! sValue.empty())
1056 {
1057 wxFAIL_MSG( _T("this cell doesn't have float value") );
1058 return;
1059 }
1060 }
1061
1062 DoBeginEdit(GetString());
1063}
1064
1065bool wxGridCellFloatEditor::EndEdit(int row, int col,
1066 wxGrid* grid)
1067{
1068 double value = 0.0;
1069 wxString text(Text()->GetValue());
1070
1071 if ( (text.empty() || text.ToDouble(&value)) && (value != m_valueOld) )
1072 {
1073 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT))
1074 grid->GetTable()->SetValueAsDouble(row, col, value);
1075 else
1076 grid->GetTable()->SetValue(row, col, text);
1077
1078 return true;
1079 }
1080 return false;
1081}
1082
1083void wxGridCellFloatEditor::Reset()
1084{
1085 DoReset(GetString());
1086}
1087
1088void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
1089{
1090 int keycode = event.GetKeyCode();
1091 char tmpbuf[2];
1092 tmpbuf[0] = (char) keycode;
1093 tmpbuf[1] = '\0';
1094 wxString strbuf(tmpbuf, *wxConvCurrent);
1095 bool is_decimal_point = ( strbuf ==
1096 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER) );
1097 if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-'
1098 || is_decimal_point )
1099 {
1100 wxGridCellTextEditor::StartingKey(event);
1101
1102 // skip Skip() below
1103 return;
1104 }
1105
1106 event.Skip();
1107}
1108
1109void wxGridCellFloatEditor::SetParameters(const wxString& params)
1110{
1111 if ( !params )
1112 {
1113 // reset to default
1114 m_width =
1115 m_precision = -1;
1116 }
1117 else
1118 {
1119 long tmp;
1120 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
1121 {
1122 m_width = (int)tmp;
1123
1124 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
1125 {
1126 m_precision = (int)tmp;
1127
1128 // skip the error message below
1129 return;
1130 }
1131 }
1132
1133 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str());
1134 }
1135}
1136
1137wxString wxGridCellFloatEditor::GetString() const
1138{
1139 wxString fmt;
1140 if ( m_width == -1 )
1141 {
1142 // default width/precision
1143 fmt = _T("%f");
1144 }
1145 else if ( m_precision == -1 )
1146 {
1147 // default precision
1148 fmt.Printf(_T("%%%d.f"), m_width);
1149 }
1150 else
1151 {
1152 fmt.Printf(_T("%%%d.%df"), m_width, m_precision);
1153 }
1154
1155 return wxString::Format(fmt, m_valueOld);
1156}
1157
1158bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
1159{
1160 if ( wxGridCellEditor::IsAcceptedKey(event) )
1161 {
1162 int keycode = event.GetKeyCode();
1163 printf("%d\n", keycode);
1164 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1165 char tmpbuf[2];
1166 tmpbuf[0] = (char) keycode;
1167 tmpbuf[1] = '\0';
1168 wxString strbuf(tmpbuf, *wxConvCurrent);
1169 bool is_decimal_point =
1170 ( strbuf == wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT,
1171 wxLOCALE_CAT_NUMBER) );
1172 if ( (keycode < 128) &&
1173 (wxIsdigit(keycode) || tolower(keycode) == 'e' ||
1174 is_decimal_point || keycode == '+' || keycode == '-') )
1175 return true;
1176 }
1177
1178 return false;
1179}
1180
1181#endif // wxUSE_TEXTCTRL
1182
1183#if wxUSE_CHECKBOX
1184
1185// ----------------------------------------------------------------------------
1186// wxGridCellBoolEditor
1187// ----------------------------------------------------------------------------
1188
1189void wxGridCellBoolEditor::Create(wxWindow* parent,
1190 wxWindowID id,
1191 wxEvtHandler* evtHandler)
1192{
1193 m_control = new wxCheckBox(parent, id, wxEmptyString,
1194 wxDefaultPosition, wxDefaultSize,
1195 wxNO_BORDER);
1196
1197 wxGridCellEditor::Create(parent, id, evtHandler);
1198}
1199
1200void wxGridCellBoolEditor::SetSize(const wxRect& r)
1201{
1202 bool resize = false;
1203 wxSize size = m_control->GetSize();
1204 wxCoord minSize = wxMin(r.width, r.height);
1205
1206 // check if the checkbox is not too big/small for this cell
1207 wxSize sizeBest = m_control->GetBestSize();
1208 if ( !(size == sizeBest) )
1209 {
1210 // reset to default size if it had been made smaller
1211 size = sizeBest;
1212
1213 resize = true;
1214 }
1215
1216 if ( size.x >= minSize || size.y >= minSize )
1217 {
1218 // leave 1 pixel margin
1219 size.x = size.y = minSize - 2;
1220
1221 resize = true;
1222 }
1223
1224 if ( resize )
1225 {
1226 m_control->SetSize(size);
1227 }
1228
1229 // position it in the centre of the rectangle (TODO: support alignment?)
1230
1231#if defined(__WXGTK__) || defined (__WXMOTIF__)
1232 // the checkbox without label still has some space to the right in wxGTK,
1233 // so shift it to the right
1234 size.x -= 8;
1235#elif defined(__WXMSW__)
1236 // here too, but in other way
1237 size.x += 1;
1238 size.y -= 2;
1239#endif
1240
1241 int hAlign = wxALIGN_CENTRE;
1242 int vAlign = wxALIGN_CENTRE;
1243 if (GetCellAttr())
1244 GetCellAttr()->GetAlignment(& hAlign, & vAlign);
1245
1246 int x = 0, y = 0;
1247 if (hAlign == wxALIGN_LEFT)
1248 {
1249 x = r.x + 2;
1250#ifdef __WXMSW__
1251 x += 2;
1252#endif
1253 y = r.y + r.height/2 - size.y/2;
1254 }
1255 else if (hAlign == wxALIGN_RIGHT)
1256 {
1257 x = r.x + r.width - size.x - 2;
1258 y = r.y + r.height/2 - size.y/2;
1259 }
1260 else if (hAlign == wxALIGN_CENTRE)
1261 {
1262 x = r.x + r.width/2 - size.x/2;
1263 y = r.y + r.height/2 - size.y/2;
1264 }
1265
1266 m_control->Move(x, y);
1267}
1268
1269void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
1270{
1271 m_control->Show(show);
1272
1273 if ( show )
1274 {
1275 wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
1276 CBox()->SetBackgroundColour(colBg);
1277 }
1278}
1279
1280void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
1281{
1282 wxASSERT_MSG(m_control,
1283 wxT("The wxGridCellEditor must be Created first!"));
1284
1285 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
1286 m_startValue = grid->GetTable()->GetValueAsBool(row, col);
1287 else
1288 {
1289 wxString cellval( grid->GetTable()->GetValue(row, col) );
1290 m_startValue = !( !cellval || (cellval == wxT("0")) );
1291 }
1292 CBox()->SetValue(m_startValue);
1293 CBox()->SetFocus();
1294}
1295
1296bool wxGridCellBoolEditor::EndEdit(int row, int col,
1297 wxGrid* grid)
1298{
1299 wxASSERT_MSG(m_control,
1300 wxT("The wxGridCellEditor must be Created first!"));
1301
1302 bool changed = false;
1303 bool value = CBox()->GetValue();
1304 if ( value != m_startValue )
1305 changed = true;
1306
1307 if ( changed )
1308 {
1309 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
1310 grid->GetTable()->SetValueAsBool(row, col, value);
1311 else
1312 grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
1313 }
1314
1315 return changed;
1316}
1317
1318void wxGridCellBoolEditor::Reset()
1319{
1320 wxASSERT_MSG(m_control,
1321 wxT("The wxGridCellEditor must be Created first!"));
1322
1323 CBox()->SetValue(m_startValue);
1324}
1325
1326void wxGridCellBoolEditor::StartingClick()
1327{
1328 CBox()->SetValue(!CBox()->GetValue());
1329}
1330
1331bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event)
1332{
1333 if ( wxGridCellEditor::IsAcceptedKey(event) )
1334 {
1335 int keycode = event.GetKeyCode();
1336 switch ( keycode )
1337 {
1338 case WXK_SPACE:
1339 case '+':
1340 case '-':
1341 return true;
1342 }
1343 }
1344
1345 return false;
1346}
1347
1348void wxGridCellBoolEditor::StartingKey(wxKeyEvent& event)
1349{
1350 int keycode = event.GetKeyCode();
1351 switch ( keycode )
1352 {
1353 case WXK_SPACE:
1354 CBox()->SetValue(!CBox()->GetValue());
1355 break;
1356
1357 case '+':
1358 CBox()->SetValue(true);
1359 break;
1360
1361 case '-':
1362 CBox()->SetValue(false);
1363 break;
1364 }
1365}
1366
1367
1368// return the value as "1" for true and the empty string for false
1369wxString wxGridCellBoolEditor::GetValue() const
1370{
1371 bool bSet = CBox()->GetValue();
1372 return bSet ? _T("1") : wxEmptyString;
1373}
1374
1375#endif // wxUSE_CHECKBOX
1376
1377#if wxUSE_COMBOBOX
1378
1379// ----------------------------------------------------------------------------
1380// wxGridCellChoiceEditor
1381// ----------------------------------------------------------------------------
1382
1383wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString& choices,
1384 bool allowOthers)
1385 : m_choices(choices),
1386 m_allowOthers(allowOthers) { }
1387
1388wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
1389 const wxString choices[],
1390 bool allowOthers)
1391 : m_allowOthers(allowOthers)
1392{
1393 if ( count )
1394 {
1395 m_choices.Alloc(count);
1396 for ( size_t n = 0; n < count; n++ )
1397 {
1398 m_choices.Add(choices[n]);
1399 }
1400 }
1401}
1402
1403wxGridCellEditor *wxGridCellChoiceEditor::Clone() const
1404{
1405 wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor;
1406 editor->m_allowOthers = m_allowOthers;
1407 editor->m_choices = m_choices;
1408
1409 return editor;
1410}
1411
1412void wxGridCellChoiceEditor::Create(wxWindow* parent,
1413 wxWindowID id,
1414 wxEvtHandler* evtHandler)
1415{
1416 m_control = new wxComboBox(parent, id, wxEmptyString,
1417 wxDefaultPosition, wxDefaultSize,
1418 m_choices,
1419 m_allowOthers ? 0 : wxCB_READONLY);
1420
1421 wxGridCellEditor::Create(parent, id, evtHandler);
1422}
1423
1424void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell,
1425 wxGridCellAttr * attr)
1426{
1427 // as we fill the entire client area, don't do anything here to minimize
1428 // flicker
1429
1430 // TODO: It doesn't actually fill the client area since the height of a
1431 // combo always defaults to the standard... Until someone has time to
1432 // figure out the right rectangle to paint, just do it the normal way...
1433 wxGridCellEditor::PaintBackground(rectCell, attr);
1434}
1435
1436void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
1437{
1438 wxASSERT_MSG(m_control,
1439 wxT("The wxGridCellEditor must be Created first!"));
1440
1441 m_startValue = grid->GetTable()->GetValue(row, col);
1442
1443 if (m_allowOthers)
1444 Combo()->SetValue(m_startValue);
1445 else
1446 {
1447 // find the right position, or default to the first if not found
1448 int pos = Combo()->FindString(m_startValue);
1449 if (pos == -1)
1450 pos = 0;
1451 Combo()->SetSelection(pos);
1452 }
1453 Combo()->SetInsertionPointEnd();
1454 Combo()->SetFocus();
1455}
1456
1457bool wxGridCellChoiceEditor::EndEdit(int row, int col,
1458 wxGrid* grid)
1459{
1460 wxString value = Combo()->GetValue();
1461 if ( value == m_startValue )
1462 return false;
1463
1464 grid->GetTable()->SetValue(row, col, value);
1465
1466 return true;
1467}
1468
1469void wxGridCellChoiceEditor::Reset()
1470{
1471 Combo()->SetValue(m_startValue);
1472 Combo()->SetInsertionPointEnd();
1473}
1474
1475void wxGridCellChoiceEditor::SetParameters(const wxString& params)
1476{
1477 if ( !params )
1478 {
1479 // what can we do?
1480 return;
1481 }
1482
1483 m_choices.Empty();
1484
1485 wxStringTokenizer tk(params, _T(','));
1486 while ( tk.HasMoreTokens() )
1487 {
1488 m_choices.Add(tk.GetNextToken());
1489 }
1490}
1491
1492// return the value in the text control
1493wxString wxGridCellChoiceEditor::GetValue() const
1494{
1495 return Combo()->GetValue();
1496}
1497
1498#endif // wxUSE_COMBOBOX
1499
1500// ----------------------------------------------------------------------------
1501// wxGridCellEditorEvtHandler
1502// ----------------------------------------------------------------------------
1503
1504void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
1505{
1506 switch ( event.GetKeyCode() )
1507 {
1508 case WXK_ESCAPE:
1509 m_editor->Reset();
1510 m_grid->DisableCellEditControl();
1511 break;
1512
1513 case WXK_TAB:
1514 m_grid->GetEventHandler()->ProcessEvent( event );
1515 break;
1516
1517 case WXK_RETURN:
1518 case WXK_NUMPAD_ENTER:
1519 if (!m_grid->GetEventHandler()->ProcessEvent(event))
1520 m_editor->HandleReturn(event);
1521 break;
1522
1523 default:
1524 event.Skip();
1525 }
1526}
1527
1528void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event)
1529{
1530 switch ( event.GetKeyCode() )
1531 {
1532 case WXK_ESCAPE:
1533 case WXK_TAB:
1534 case WXK_RETURN:
1535 case WXK_NUMPAD_ENTER:
1536 break;
1537
1538 default:
1539 event.Skip();
1540 }
1541}
1542
1543// ----------------------------------------------------------------------------
1544// wxGridCellWorker is an (almost) empty common base class for
1545// wxGridCellRenderer and wxGridCellEditor managing ref counting
1546// ----------------------------------------------------------------------------
1547
1548void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
1549{
1550 // nothing to do
1551}
1552
1553wxGridCellWorker::~wxGridCellWorker()
1554{
1555}
1556
1557// ============================================================================
1558// renderer classes
1559// ============================================================================
1560
1561// ----------------------------------------------------------------------------
1562// wxGridCellRenderer
1563// ----------------------------------------------------------------------------
1564
1565void wxGridCellRenderer::Draw(wxGrid& grid,
1566 wxGridCellAttr& attr,
1567 wxDC& dc,
1568 const wxRect& rect,
1569 int WXUNUSED(row), int WXUNUSED(col),
1570 bool isSelected)
1571{
1572 dc.SetBackgroundMode( wxSOLID );
1573
1574 // grey out fields if the grid is disabled
1575 if( grid.IsEnabled() )
1576 {
1577 if ( isSelected )
1578 {
1579 dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
1580 }
1581 else
1582 {
1583 dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
1584 }
1585 }
1586 else
1587 {
1588 dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), wxSOLID));
1589 }
1590
1591 dc.SetPen( *wxTRANSPARENT_PEN );
1592 dc.DrawRectangle(rect);
1593}
1594
1595// ----------------------------------------------------------------------------
1596// wxGridCellStringRenderer
1597// ----------------------------------------------------------------------------
1598
1599void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid,
1600 wxGridCellAttr& attr,
1601 wxDC& dc,
1602 bool isSelected)
1603{
1604 dc.SetBackgroundMode( wxTRANSPARENT );
1605
1606 // TODO some special colours for attr.IsReadOnly() case?
1607
1608 // different coloured text when the grid is disabled
1609 if( grid.IsEnabled() )
1610 {
1611 if ( isSelected )
1612 {
1613 dc.SetTextBackground( grid.GetSelectionBackground() );
1614 dc.SetTextForeground( grid.GetSelectionForeground() );
1615 }
1616 else
1617 {
1618 dc.SetTextBackground( attr.GetBackgroundColour() );
1619 dc.SetTextForeground( attr.GetTextColour() );
1620 }
1621 }
1622 else
1623 {
1624 dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
1625 dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
1626 }
1627
1628 dc.SetFont( attr.GetFont() );
1629}
1630
1631wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr,
1632 wxDC& dc,
1633 const wxString& text)
1634{
1635 wxCoord x = 0, y = 0, max_x = 0;
1636 dc.SetFont(attr.GetFont());
1637 wxStringTokenizer tk(text, _T('\n'));
1638 while ( tk.HasMoreTokens() )
1639 {
1640 dc.GetTextExtent(tk.GetNextToken(), &x, &y);
1641 max_x = wxMax(max_x, x);
1642 }
1643
1644 y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines.
1645
1646 return wxSize(max_x, y);
1647}
1648
1649wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
1650 wxGridCellAttr& attr,
1651 wxDC& dc,
1652 int row, int col)
1653{
1654 return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
1655}
1656
1657void wxGridCellStringRenderer::Draw(wxGrid& grid,
1658 wxGridCellAttr& attr,
1659 wxDC& dc,
1660 const wxRect& rectCell,
1661 int row, int col,
1662 bool isSelected)
1663{
1664 wxRect rect = rectCell;
1665 rect.Inflate(-1);
1666
1667 // erase only this cells background, overflow cells should have been erased
1668 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1669
1670 int hAlign, vAlign;
1671 attr.GetAlignment(&hAlign, &vAlign);
1672
1673 int overflowCols = 0;
1674
1675 if (attr.GetOverflow())
1676 {
1677 int cols = grid.GetNumberCols();
1678 int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth();
1679 int cell_rows, cell_cols;
1680 attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <=0
1681 if ((best_width > rectCell.width) && (col < cols) && grid.GetTable())
1682 {
1683 int i, c_cols, c_rows;
1684 for (i = col+cell_cols; i < cols; i++)
1685 {
1686 bool is_empty = true;
1687 for (int j=row; j<row+cell_rows; j++)
1688 {
1689 // check w/ anchor cell for multicell block
1690 grid.GetCellSize(j, i, &c_rows, &c_cols);
1691 if (c_rows > 0) c_rows = 0;
1692 if (!grid.GetTable()->IsEmptyCell(j+c_rows, i))
1693 {
1694 is_empty = false;
1695 break;
1696 }
1697 }
1698 if (is_empty)
1699 rect.width += grid.GetColSize(i);
1700 else
1701 {
1702 i--;
1703 break;
1704 }
1705 if (rect.width >= best_width) break;
1706 }
1707 overflowCols = i - col - cell_cols + 1;
1708 if (overflowCols >= cols) overflowCols = cols - 1;
1709 }
1710
1711 if (overflowCols > 0) // redraw overflow cells w/ proper hilight
1712 {
1713 hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned
1714 wxRect clip = rect;
1715 clip.x += rectCell.width;
1716 // draw each overflow cell individually
1717 int col_end = col+cell_cols+overflowCols;
1718 if (col_end >= grid.GetNumberCols())
1719 col_end = grid.GetNumberCols() - 1;
1720 for (int i = col+cell_cols; i <= col_end; i++)
1721 {
1722 clip.width = grid.GetColSize(i) - 1;
1723 dc.DestroyClippingRegion();
1724 dc.SetClippingRegion(clip);
1725
1726 SetTextColoursAndFont(grid, attr, dc,
1727 grid.IsInSelection(row,i));
1728
1729 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
1730 rect, hAlign, vAlign);
1731 clip.x += grid.GetColSize(i) - 1;
1732 }
1733
1734 rect = rectCell;
1735 rect.Inflate(-1);
1736 rect.width++;
1737 dc.DestroyClippingRegion();
1738 }
1739 }
1740
1741 // now we only have to draw the text
1742 SetTextColoursAndFont(grid, attr, dc, isSelected);
1743
1744 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
1745 rect, hAlign, vAlign);
1746}
1747
1748// ----------------------------------------------------------------------------
1749// wxGridCellNumberRenderer
1750// ----------------------------------------------------------------------------
1751
1752wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col)
1753{
1754 wxGridTableBase *table = grid.GetTable();
1755 wxString text;
1756 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1757 {
1758 text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
1759 }
1760 else
1761 {
1762 text = table->GetValue(row, col);
1763 }
1764
1765 return text;
1766}
1767
1768void wxGridCellNumberRenderer::Draw(wxGrid& grid,
1769 wxGridCellAttr& attr,
1770 wxDC& dc,
1771 const wxRect& rectCell,
1772 int row, int col,
1773 bool isSelected)
1774{
1775 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1776
1777 SetTextColoursAndFont(grid, attr, dc, isSelected);
1778
1779 // draw the text right aligned by default
1780 int hAlign, vAlign;
1781 attr.GetAlignment(&hAlign, &vAlign);
1782 hAlign = wxALIGN_RIGHT;
1783
1784 wxRect rect = rectCell;
1785 rect.Inflate(-1);
1786
1787 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1788}
1789
1790wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
1791 wxGridCellAttr& attr,
1792 wxDC& dc,
1793 int row, int col)
1794{
1795 return DoGetBestSize(attr, dc, GetString(grid, row, col));
1796}
1797
1798// ----------------------------------------------------------------------------
1799// wxGridCellFloatRenderer
1800// ----------------------------------------------------------------------------
1801
1802wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
1803{
1804 SetWidth(width);
1805 SetPrecision(precision);
1806}
1807
1808wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
1809{
1810 wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
1811 renderer->m_width = m_width;
1812 renderer->m_precision = m_precision;
1813 renderer->m_format = m_format;
1814
1815 return renderer;
1816}
1817
1818wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col)
1819{
1820 wxGridTableBase *table = grid.GetTable();
1821
1822 bool hasDouble;
1823 double val;
1824 wxString text;
1825 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1826 {
1827 val = table->GetValueAsDouble(row, col);
1828 hasDouble = true;
1829 }
1830 else
1831 {
1832 text = table->GetValue(row, col);
1833 hasDouble = text.ToDouble(&val);
1834 }
1835
1836 if ( hasDouble )
1837 {
1838 if ( !m_format )
1839 {
1840 if ( m_width == -1 )
1841 {
1842 if ( m_precision == -1 )
1843 {
1844 // default width/precision
1845 m_format = _T("%f");
1846 }
1847 else
1848 {
1849 m_format.Printf(_T("%%.%df"), m_precision);
1850 }
1851 }
1852 else if ( m_precision == -1 )
1853 {
1854 // default precision
1855 m_format.Printf(_T("%%%d.f"), m_width);
1856 }
1857 else
1858 {
1859 m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
1860 }
1861 }
1862
1863 text.Printf(m_format, val);
1864
1865 }
1866 //else: text already contains the string
1867
1868 return text;
1869}
1870
1871void wxGridCellFloatRenderer::Draw(wxGrid& grid,
1872 wxGridCellAttr& attr,
1873 wxDC& dc,
1874 const wxRect& rectCell,
1875 int row, int col,
1876 bool isSelected)
1877{
1878 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1879
1880 SetTextColoursAndFont(grid, attr, dc, isSelected);
1881
1882 // draw the text right aligned by default
1883 int hAlign, vAlign;
1884 attr.GetAlignment(&hAlign, &vAlign);
1885 hAlign = wxALIGN_RIGHT;
1886
1887 wxRect rect = rectCell;
1888 rect.Inflate(-1);
1889
1890 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1891}
1892
1893wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
1894 wxGridCellAttr& attr,
1895 wxDC& dc,
1896 int row, int col)
1897{
1898 return DoGetBestSize(attr, dc, GetString(grid, row, col));
1899}
1900
1901void wxGridCellFloatRenderer::SetParameters(const wxString& params)
1902{
1903 if ( !params )
1904 {
1905 // reset to defaults
1906 SetWidth(-1);
1907 SetPrecision(-1);
1908 }
1909 else
1910 {
1911 wxString tmp = params.BeforeFirst(_T(','));
1912 if ( !tmp.empty() )
1913 {
1914 long width;
1915 if ( tmp.ToLong(&width) )
1916 {
1917 SetWidth((int)width);
1918 }
1919 else
1920 {
1921 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str());
1922 }
1923
1924 }
1925 tmp = params.AfterFirst(_T(','));
1926 if ( !tmp.empty() )
1927 {
1928 long precision;
1929 if ( tmp.ToLong(&precision) )
1930 {
1931 SetPrecision((int)precision);
1932 }
1933 else
1934 {
1935 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str());
1936 }
1937
1938 }
1939 }
1940}
1941
1942
1943// ----------------------------------------------------------------------------
1944// wxGridCellBoolRenderer
1945// ----------------------------------------------------------------------------
1946
1947wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
1948
1949// FIXME these checkbox size calculations are really ugly...
1950
1951// between checkmark and box
1952static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
1953
1954wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
1955 wxGridCellAttr& WXUNUSED(attr),
1956 wxDC& WXUNUSED(dc),
1957 int WXUNUSED(row),
1958 int WXUNUSED(col))
1959{
1960 // compute it only once (no locks for MT safeness in GUI thread...)
1961 if ( !ms_sizeCheckMark.x )
1962 {
1963 // get checkbox size
1964 wxCheckBox *checkbox = new wxCheckBox(&grid, wxID_ANY, wxEmptyString);
1965 wxSize size = checkbox->GetBestSize();
1966 wxCoord checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN;
1967
1968 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1969#if defined(__WXGTK__) || defined(__WXMOTIF__)
1970 checkSize -= size.y / 2;
1971#endif
1972
1973 delete checkbox;
1974
1975 ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
1976 }
1977
1978 return ms_sizeCheckMark;
1979}
1980
1981void wxGridCellBoolRenderer::Draw(wxGrid& grid,
1982 wxGridCellAttr& attr,
1983 wxDC& dc,
1984 const wxRect& rect,
1985 int row, int col,
1986 bool isSelected)
1987{
1988 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
1989
1990 // draw a check mark in the centre (ignoring alignment - TODO)
1991 wxSize size = GetBestSize(grid, attr, dc, row, col);
1992
1993 // don't draw outside the cell
1994 wxCoord minSize = wxMin(rect.width, rect.height);
1995 if ( size.x >= minSize || size.y >= minSize )
1996 {
1997 // and even leave (at least) 1 pixel margin
1998 size.x = size.y = minSize - 2;
1999 }
2000
2001 // draw a border around checkmark
2002 int vAlign, hAlign;
2003 attr.GetAlignment(& hAlign, &vAlign);
2004
2005 wxRect rectBorder;
2006 if (hAlign == wxALIGN_CENTRE)
2007 {
2008 rectBorder.x = rect.x + rect.width/2 - size.x/2;
2009 rectBorder.y = rect.y + rect.height/2 - size.y/2;
2010 rectBorder.width = size.x;
2011 rectBorder.height = size.y;
2012 }
2013 else if (hAlign == wxALIGN_LEFT)
2014 {
2015 rectBorder.x = rect.x + 2;
2016 rectBorder.y = rect.y + rect.height/2 - size.y/2;
2017 rectBorder.width = size.x;
2018 rectBorder.height = size.y;
2019 }
2020 else if (hAlign == wxALIGN_RIGHT)
2021 {
2022 rectBorder.x = rect.x + rect.width - size.x - 2;
2023 rectBorder.y = rect.y + rect.height/2 - size.y/2;
2024 rectBorder.width = size.x;
2025 rectBorder.height = size.y;
2026 }
2027
2028 bool value;
2029 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
2030 value = grid.GetTable()->GetValueAsBool(row, col);
2031 else
2032 {
2033 wxString cellval( grid.GetTable()->GetValue(row, col) );
2034 value = !( !cellval || (cellval == wxT("0")) );
2035 }
2036
2037 if ( value )
2038 {
2039 wxRect rectMark = rectBorder;
2040#ifdef __WXMSW__
2041 // MSW DrawCheckMark() is weird (and should probably be changed...)
2042 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2);
2043 rectMark.x++;
2044 rectMark.y++;
2045#else // !MSW
2046 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
2047#endif // MSW/!MSW
2048
2049 dc.SetTextForeground(attr.GetTextColour());
2050 dc.DrawCheckMark(rectMark);
2051 }
2052
2053 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2054 dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
2055 dc.DrawRectangle(rectBorder);
2056}
2057
2058// ----------------------------------------------------------------------------
2059// wxGridCellAttr
2060// ----------------------------------------------------------------------------
2061
2062void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
2063{
2064 m_nRef = 1;
2065
2066 m_isReadOnly = Unset;
2067
2068 m_renderer = NULL;
2069 m_editor = NULL;
2070
2071 m_attrkind = wxGridCellAttr::Cell;
2072
2073 m_sizeRows = m_sizeCols = 1;
2074 m_overflow = UnsetOverflow;
2075
2076 SetDefAttr(attrDefault);
2077}
2078
2079wxGridCellAttr *wxGridCellAttr::Clone() const
2080{
2081 wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr);
2082
2083 if ( HasTextColour() )
2084 attr->SetTextColour(GetTextColour());
2085 if ( HasBackgroundColour() )
2086 attr->SetBackgroundColour(GetBackgroundColour());
2087 if ( HasFont() )
2088 attr->SetFont(GetFont());
2089 if ( HasAlignment() )
2090 attr->SetAlignment(m_hAlign, m_vAlign);
2091
2092 attr->SetSize( m_sizeRows, m_sizeCols );
2093
2094 if ( m_renderer )
2095 {
2096 attr->SetRenderer(m_renderer);
2097 m_renderer->IncRef();
2098 }
2099 if ( m_editor )
2100 {
2101 attr->SetEditor(m_editor);
2102 m_editor->IncRef();
2103 }
2104
2105 if ( IsReadOnly() )
2106 attr->SetReadOnly();
2107
2108 attr->SetKind( m_attrkind );
2109
2110 return attr;
2111}
2112
2113void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom)
2114{
2115 if ( !HasTextColour() && mergefrom->HasTextColour() )
2116 SetTextColour(mergefrom->GetTextColour());
2117 if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() )
2118 SetBackgroundColour(mergefrom->GetBackgroundColour());
2119 if ( !HasFont() && mergefrom->HasFont() )
2120 SetFont(mergefrom->GetFont());
2121 if ( !HasAlignment() && mergefrom->HasAlignment() ){
2122 int hAlign, vAlign;
2123 mergefrom->GetAlignment( &hAlign, &vAlign);
2124 SetAlignment(hAlign, vAlign);
2125 }
2126
2127 mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
2128
2129 // Directly access member functions as GetRender/Editor don't just return
2130 // m_renderer/m_editor
2131 //
2132 // Maybe add support for merge of Render and Editor?
2133 if (!HasRenderer() && mergefrom->HasRenderer() )
2134 {
2135 m_renderer = mergefrom->m_renderer;
2136 m_renderer->IncRef();
2137 }
2138 if ( !HasEditor() && mergefrom->HasEditor() )
2139 {
2140 m_editor = mergefrom->m_editor;
2141 m_editor->IncRef();
2142 }
2143 if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() )
2144 SetReadOnly(mergefrom->IsReadOnly());
2145
2146 if (!HasOverflowMode() && mergefrom->HasOverflowMode() )
2147 SetOverflow(mergefrom->GetOverflow());
2148
2149 SetDefAttr(mergefrom->m_defGridAttr);
2150}
2151
2152void wxGridCellAttr::SetSize(int num_rows, int num_cols)
2153{
2154 // The size of a cell is normally 1,1
2155
2156 // If this cell is larger (2,2) then this is the top left cell
2157 // the other cells that will be covered (lower right cells) must be
2158 // set to negative or zero values such that
2159 // row + num_rows of the covered cell points to the larger cell (this cell)
2160 // same goes for the col + num_cols.
2161
2162 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2163
2164 wxASSERT_MSG( (!((num_rows>0)&&(num_cols<=0)) ||
2165 !((num_rows<=0)&&(num_cols>0)) ||
2166 !((num_rows==0)&&(num_cols==0))),
2167 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2168
2169 m_sizeRows = num_rows;
2170 m_sizeCols = num_cols;
2171}
2172
2173const wxColour& wxGridCellAttr::GetTextColour() const
2174{
2175 if (HasTextColour())
2176 {
2177 return m_colText;
2178 }
2179 else if (m_defGridAttr && m_defGridAttr != this)
2180 {
2181 return m_defGridAttr->GetTextColour();
2182 }
2183 else
2184 {
2185 wxFAIL_MSG(wxT("Missing default cell attribute"));
2186 return wxNullColour;
2187 }
2188}
2189
2190
2191const wxColour& wxGridCellAttr::GetBackgroundColour() const
2192{
2193 if (HasBackgroundColour())
2194 return m_colBack;
2195 else if (m_defGridAttr && m_defGridAttr != this)
2196 return m_defGridAttr->GetBackgroundColour();
2197 else
2198 {
2199 wxFAIL_MSG(wxT("Missing default cell attribute"));
2200 return wxNullColour;
2201 }
2202}
2203
2204
2205const wxFont& wxGridCellAttr::GetFont() const
2206{
2207 if (HasFont())
2208 return m_font;
2209 else if (m_defGridAttr && m_defGridAttr != this)
2210 return m_defGridAttr->GetFont();
2211 else
2212 {
2213 wxFAIL_MSG(wxT("Missing default cell attribute"));
2214 return wxNullFont;
2215 }
2216}
2217
2218
2219void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
2220{
2221 if (HasAlignment())
2222 {
2223 if ( hAlign ) *hAlign = m_hAlign;
2224 if ( vAlign ) *vAlign = m_vAlign;
2225 }
2226 else if (m_defGridAttr && m_defGridAttr != this)
2227 m_defGridAttr->GetAlignment(hAlign, vAlign);
2228 else
2229 {
2230 wxFAIL_MSG(wxT("Missing default cell attribute"));
2231 }
2232}
2233
2234void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
2235{
2236 if ( num_rows ) *num_rows = m_sizeRows;
2237 if ( num_cols ) *num_cols = m_sizeCols;
2238}
2239
2240// GetRenderer and GetEditor use a slightly different decision path about
2241// which attribute to use. If a non-default attr object has one then it is
2242// used, otherwise the default editor or renderer is fetched from the grid and
2243// used. It should be the default for the data type of the cell. If it is
2244// NULL (because the table has a type that the grid does not have in its
2245// registry,) then the grid's default editor or renderer is used.
2246
2247wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
2248{
2249 wxGridCellRenderer *renderer;
2250
2251 if ( m_renderer && this != m_defGridAttr )
2252 {
2253 // use the cells renderer if it has one
2254 renderer = m_renderer;
2255 renderer->IncRef();
2256 }
2257 else // no non default cell renderer
2258 {
2259 // get default renderer for the data type
2260 if ( grid )
2261 {
2262 // GetDefaultRendererForCell() will do IncRef() for us
2263 renderer = grid->GetDefaultRendererForCell(row, col);
2264 }
2265 else
2266 {
2267 renderer = NULL;
2268 }
2269
2270 if ( !renderer )
2271 {
2272 if (m_defGridAttr && this != m_defGridAttr )
2273 {
2274 // if we still don't have one then use the grid default
2275 // (no need for IncRef() here neither)
2276 renderer = m_defGridAttr->GetRenderer(NULL, 0, 0);
2277 }
2278 else // default grid attr
2279 {
2280 // use m_renderer which we had decided not to use initially
2281 renderer = m_renderer;
2282 if ( renderer )
2283 renderer->IncRef();
2284 }
2285 }
2286 }
2287
2288 // we're supposed to always find something
2289 wxASSERT_MSG(renderer, wxT("Missing default cell renderer"));
2290
2291 return renderer;
2292}
2293
2294// same as above, except for s/renderer/editor/g
2295wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
2296{
2297 wxGridCellEditor *editor;
2298
2299 if ( m_editor && this != m_defGridAttr )
2300 {
2301 // use the cells editor if it has one
2302 editor = m_editor;
2303 editor->IncRef();
2304 }
2305 else // no non default cell editor
2306 {
2307 // get default editor for the data type
2308 if ( grid )
2309 {
2310 // GetDefaultEditorForCell() will do IncRef() for us
2311 editor = grid->GetDefaultEditorForCell(row, col);
2312 }
2313 else
2314 {
2315 editor = NULL;
2316 }
2317
2318 if ( !editor )
2319 {
2320 if ( m_defGridAttr && this != m_defGridAttr )
2321 {
2322 // if we still don't have one then use the grid default
2323 // (no need for IncRef() here neither)
2324 editor = m_defGridAttr->GetEditor(NULL, 0, 0);
2325 }
2326 else // default grid attr
2327 {
2328 // use m_editor which we had decided not to use initially
2329 editor = m_editor;
2330 if ( editor )
2331 editor->IncRef();
2332 }
2333 }
2334 }
2335
2336 // we're supposed to always find something
2337 wxASSERT_MSG(editor, wxT("Missing default cell editor"));
2338
2339 return editor;
2340}
2341
2342// ----------------------------------------------------------------------------
2343// wxGridCellAttrData
2344// ----------------------------------------------------------------------------
2345
2346void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
2347{
2348 int n = FindIndex(row, col);
2349 if ( n == wxNOT_FOUND )
2350 {
2351 // add the attribute
2352 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
2353 }
2354 else
2355 {
2356 // free the old attribute
2357 m_attrs[(size_t)n].attr->DecRef();
2358
2359 if ( attr )
2360 {
2361 // change the attribute
2362 m_attrs[(size_t)n].attr = attr;
2363 }
2364 else
2365 {
2366 // remove this attribute
2367 m_attrs.RemoveAt((size_t)n);
2368 }
2369 }
2370}
2371
2372wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
2373{
2374 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2375
2376 int n = FindIndex(row, col);
2377 if ( n != wxNOT_FOUND )
2378 {
2379 attr = m_attrs[(size_t)n].attr;
2380 attr->IncRef();
2381 }
2382
2383 return attr;
2384}
2385
2386void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
2387{
2388 size_t count = m_attrs.GetCount();
2389 for ( size_t n = 0; n < count; n++ )
2390 {
2391 wxGridCellCoords& coords = m_attrs[n].coords;
2392 wxCoord row = coords.GetRow();
2393 if ((size_t)row >= pos)
2394 {
2395 if (numRows > 0)
2396 {
2397 // If rows inserted, include row counter where necessary
2398 coords.SetRow(row + numRows);
2399 }
2400 else if (numRows < 0)
2401 {
2402 // If rows deleted ...
2403 if ((size_t)row >= pos - numRows)
2404 {
2405 // ...either decrement row counter (if row still exists)...
2406 coords.SetRow(row + numRows);
2407 }
2408 else
2409 {
2410 // ...or remove the attribute
2411 // No need to DecRef the attribute itself since this is
2412 // done be wxGridCellWithAttr's destructor!
2413 m_attrs.RemoveAt(n);
2414 n--; count--;
2415 }
2416 }
2417 }
2418 }
2419}
2420
2421void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
2422{
2423 size_t count = m_attrs.GetCount();
2424 for ( size_t n = 0; n < count; n++ )
2425 {
2426 wxGridCellCoords& coords = m_attrs[n].coords;
2427 wxCoord col = coords.GetCol();
2428 if ( (size_t)col >= pos )
2429 {
2430 if ( numCols > 0 )
2431 {
2432 // If rows inserted, include row counter where necessary
2433 coords.SetCol(col + numCols);
2434 }
2435 else if (numCols < 0)
2436 {
2437 // If rows deleted ...
2438 if ((size_t)col >= pos - numCols)
2439 {
2440 // ...either decrement row counter (if row still exists)...
2441 coords.SetCol(col + numCols);
2442 }
2443 else
2444 {
2445 // ...or remove the attribute
2446 // No need to DecRef the attribute itself since this is
2447 // done be wxGridCellWithAttr's destructor!
2448 m_attrs.RemoveAt(n);
2449 n--; count--;
2450 }
2451 }
2452 }
2453 }
2454}
2455
2456int wxGridCellAttrData::FindIndex(int row, int col) const
2457{
2458 size_t count = m_attrs.GetCount();
2459 for ( size_t n = 0; n < count; n++ )
2460 {
2461 const wxGridCellCoords& coords = m_attrs[n].coords;
2462 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
2463 {
2464 return n;
2465 }
2466 }
2467
2468 return wxNOT_FOUND;
2469}
2470
2471// ----------------------------------------------------------------------------
2472// wxGridRowOrColAttrData
2473// ----------------------------------------------------------------------------
2474
2475wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2476{
2477 size_t count = m_attrs.Count();
2478 for ( size_t n = 0; n < count; n++ )
2479 {
2480 m_attrs[n]->DecRef();
2481 }
2482}
2483
2484wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
2485{
2486 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2487
2488 int n = m_rowsOrCols.Index(rowOrCol);
2489 if ( n != wxNOT_FOUND )
2490 {
2491 attr = m_attrs[(size_t)n];
2492 attr->IncRef();
2493 }
2494
2495 return attr;
2496}
2497
2498void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
2499{
2500 int i = m_rowsOrCols.Index(rowOrCol);
2501 if ( i == wxNOT_FOUND )
2502 {
2503 // add the attribute
2504 m_rowsOrCols.Add(rowOrCol);
2505 m_attrs.Add(attr);
2506 }
2507 else
2508 {
2509 size_t n = (size_t)i;
2510 if ( attr )
2511 {
2512 // change the attribute
2513 m_attrs[n]->DecRef();
2514 m_attrs[n] = attr;
2515 }
2516 else
2517 {
2518 // remove this attribute
2519 m_attrs[n]->DecRef();
2520 m_rowsOrCols.RemoveAt(n);
2521 m_attrs.RemoveAt(n);
2522 }
2523 }
2524}
2525
2526void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
2527{
2528 size_t count = m_attrs.GetCount();
2529 for ( size_t n = 0; n < count; n++ )
2530 {
2531 int & rowOrCol = m_rowsOrCols[n];
2532 if ( (size_t)rowOrCol >= pos )
2533 {
2534 if ( numRowsOrCols > 0 )
2535 {
2536 // If rows inserted, include row counter where necessary
2537 rowOrCol += numRowsOrCols;
2538 }
2539 else if ( numRowsOrCols < 0)
2540 {
2541 // If rows deleted, either decrement row counter (if row still exists)
2542 if ((size_t)rowOrCol >= pos - numRowsOrCols)
2543 rowOrCol += numRowsOrCols;
2544 else
2545 {
2546 m_rowsOrCols.RemoveAt(n);
2547 m_attrs[n]->DecRef();
2548 m_attrs.RemoveAt(n);
2549 n--; count--;
2550 }
2551 }
2552 }
2553 }
2554}
2555
2556// ----------------------------------------------------------------------------
2557// wxGridCellAttrProvider
2558// ----------------------------------------------------------------------------
2559
2560wxGridCellAttrProvider::wxGridCellAttrProvider()
2561{
2562 m_data = (wxGridCellAttrProviderData *)NULL;
2563}
2564
2565wxGridCellAttrProvider::~wxGridCellAttrProvider()
2566{
2567 delete m_data;
2568}
2569
2570void wxGridCellAttrProvider::InitData()
2571{
2572 m_data = new wxGridCellAttrProviderData;
2573}
2574
2575wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col,
2576 wxGridCellAttr::wxAttrKind kind ) const
2577{
2578 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2579 if ( m_data )
2580 {
2581 switch(kind)
2582 {
2583 case (wxGridCellAttr::Any):
2584 //Get cached merge attributes.
2585 // Currenlty not used as no cache implemented as not mutiable
2586 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2587 if(!attr)
2588 {
2589 //Basicaly implement old version.
2590 //Also check merge cache, so we don't have to re-merge every time..
2591 wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col);
2592 wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row);
2593 wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col);
2594
2595 if((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol)){
2596 // Two or more are non NULL
2597 attr = new wxGridCellAttr;
2598 attr->SetKind(wxGridCellAttr::Merged);
2599
2600 //Order important..
2601 if(attrcell){
2602 attr->MergeWith(attrcell);
2603 attrcell->DecRef();
2604 }
2605 if(attrcol){
2606 attr->MergeWith(attrcol);
2607 attrcol->DecRef();
2608 }
2609 if(attrrow){
2610 attr->MergeWith(attrrow);
2611 attrrow->DecRef();
2612 }
2613 //store merge attr if cache implemented
2614 //attr->IncRef();
2615 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2616 }
2617 else
2618 {
2619 // one or none is non null return it or null.
2620 if(attrrow) attr = attrrow;
2621 if(attrcol)
2622 {
2623 if(attr)
2624 attr->DecRef();
2625 attr = attrcol;
2626 }
2627 if(attrcell)
2628 {
2629 if(attr)
2630 attr->DecRef();
2631 attr = attrcell;
2632 }
2633 }
2634 }
2635 break;
2636 case (wxGridCellAttr::Cell):
2637 attr = m_data->m_cellAttrs.GetAttr(row, col);
2638 break;
2639 case (wxGridCellAttr::Col):
2640 attr = m_data->m_colAttrs.GetAttr(col);
2641 break;
2642 case (wxGridCellAttr::Row):
2643 attr = m_data->m_rowAttrs.GetAttr(row);
2644 break;
2645 default:
2646 // unused as yet...
2647 // (wxGridCellAttr::Default):
2648 // (wxGridCellAttr::Merged):
2649 break;
2650 }
2651 }
2652 return attr;
2653}
2654
2655void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
2656 int row, int col)
2657{
2658 if ( !m_data )
2659 InitData();
2660
2661 m_data->m_cellAttrs.SetAttr(attr, row, col);
2662}
2663
2664void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
2665{
2666 if ( !m_data )
2667 InitData();
2668
2669 m_data->m_rowAttrs.SetAttr(attr, row);
2670}
2671
2672void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
2673{
2674 if ( !m_data )
2675 InitData();
2676
2677 m_data->m_colAttrs.SetAttr(attr, col);
2678}
2679
2680void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
2681{
2682 if ( m_data )
2683 {
2684 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
2685
2686 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
2687 }
2688}
2689
2690void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
2691{
2692 if ( m_data )
2693 {
2694 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
2695
2696 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
2697 }
2698}
2699
2700// ----------------------------------------------------------------------------
2701// wxGridTypeRegistry
2702// ----------------------------------------------------------------------------
2703
2704wxGridTypeRegistry::~wxGridTypeRegistry()
2705{
2706 size_t count = m_typeinfo.Count();
2707 for ( size_t i = 0; i < count; i++ )
2708 delete m_typeinfo[i];
2709}
2710
2711
2712void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
2713 wxGridCellRenderer* renderer,
2714 wxGridCellEditor* editor)
2715{
2716 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
2717
2718 // is it already registered?
2719 int loc = FindRegisteredDataType(typeName);
2720 if ( loc != wxNOT_FOUND )
2721 {
2722 delete m_typeinfo[loc];
2723 m_typeinfo[loc] = info;
2724 }
2725 else
2726 {
2727 m_typeinfo.Add(info);
2728 }
2729}
2730
2731int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
2732{
2733 size_t count = m_typeinfo.GetCount();
2734 for ( size_t i = 0; i < count; i++ )
2735 {
2736 if ( typeName == m_typeinfo[i]->m_typeName )
2737 {
2738 return i;
2739 }
2740 }
2741
2742 return wxNOT_FOUND;
2743}
2744
2745int wxGridTypeRegistry::FindDataType(const wxString& typeName)
2746{
2747 int index = FindRegisteredDataType(typeName);
2748 if ( index == wxNOT_FOUND )
2749 {
2750 // check whether this is one of the standard ones, in which case
2751 // register it "on the fly"
2752#if wxUSE_TEXTCTRL
2753 if ( typeName == wxGRID_VALUE_STRING )
2754 {
2755 RegisterDataType(wxGRID_VALUE_STRING,
2756 new wxGridCellStringRenderer,
2757 new wxGridCellTextEditor);
2758 } else
2759#endif // wxUSE_TEXTCTRL
2760#if wxUSE_CHECKBOX
2761 if ( typeName == wxGRID_VALUE_BOOL )
2762 {
2763 RegisterDataType(wxGRID_VALUE_BOOL,
2764 new wxGridCellBoolRenderer,
2765 new wxGridCellBoolEditor);
2766 } else
2767#endif // wxUSE_CHECKBOX
2768#if wxUSE_TEXTCTRL
2769 if ( typeName == wxGRID_VALUE_NUMBER )
2770 {
2771 RegisterDataType(wxGRID_VALUE_NUMBER,
2772 new wxGridCellNumberRenderer,
2773 new wxGridCellNumberEditor);
2774 }
2775 else if ( typeName == wxGRID_VALUE_FLOAT )
2776 {
2777 RegisterDataType(wxGRID_VALUE_FLOAT,
2778 new wxGridCellFloatRenderer,
2779 new wxGridCellFloatEditor);
2780 } else
2781#endif // wxUSE_TEXTCTRL
2782#if wxUSE_COMBOBOX
2783 if ( typeName == wxGRID_VALUE_CHOICE )
2784 {
2785 RegisterDataType(wxGRID_VALUE_CHOICE,
2786 new wxGridCellStringRenderer,
2787 new wxGridCellChoiceEditor);
2788 } else
2789#endif // wxUSE_COMBOBOX
2790 {
2791 return wxNOT_FOUND;
2792 }
2793
2794 // we get here only if just added the entry for this type, so return
2795 // the last index
2796 index = m_typeinfo.GetCount() - 1;
2797 }
2798
2799 return index;
2800}
2801
2802int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
2803{
2804 int index = FindDataType(typeName);
2805 if ( index == wxNOT_FOUND )
2806 {
2807 // the first part of the typename is the "real" type, anything after ':'
2808 // are the parameters for the renderer
2809 index = FindDataType(typeName.BeforeFirst(_T(':')));
2810 if ( index == wxNOT_FOUND )
2811 {
2812 return wxNOT_FOUND;
2813 }
2814
2815 wxGridCellRenderer *renderer = GetRenderer(index);
2816 wxGridCellRenderer *rendererOld = renderer;
2817 renderer = renderer->Clone();
2818 rendererOld->DecRef();
2819
2820 wxGridCellEditor *editor = GetEditor(index);
2821 wxGridCellEditor *editorOld = editor;
2822 editor = editor->Clone();
2823 editorOld->DecRef();
2824
2825 // do it even if there are no parameters to reset them to defaults
2826 wxString params = typeName.AfterFirst(_T(':'));
2827 renderer->SetParameters(params);
2828 editor->SetParameters(params);
2829
2830 // register the new typename
2831 RegisterDataType(typeName, renderer, editor);
2832
2833 // we just registered it, it's the last one
2834 index = m_typeinfo.GetCount() - 1;
2835 }
2836
2837 return index;
2838}
2839
2840wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
2841{
2842 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
2843 if (renderer)
2844 renderer->IncRef();
2845 return renderer;
2846}
2847
2848wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
2849{
2850 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
2851 if (editor)
2852 editor->IncRef();
2853 return editor;
2854}
2855
2856// ----------------------------------------------------------------------------
2857// wxGridTableBase
2858// ----------------------------------------------------------------------------
2859
2860IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
2861
2862
2863wxGridTableBase::wxGridTableBase()
2864{
2865 m_view = (wxGrid *) NULL;
2866 m_attrProvider = (wxGridCellAttrProvider *) NULL;
2867}
2868
2869wxGridTableBase::~wxGridTableBase()
2870{
2871 delete m_attrProvider;
2872}
2873
2874void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
2875{
2876 delete m_attrProvider;
2877 m_attrProvider = attrProvider;
2878}
2879
2880bool wxGridTableBase::CanHaveAttributes()
2881{
2882 if ( ! GetAttrProvider() )
2883 {
2884 // use the default attr provider by default
2885 SetAttrProvider(new wxGridCellAttrProvider);
2886 }
2887 return true;
2888}
2889
2890wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind)
2891{
2892 if ( m_attrProvider )
2893 return m_attrProvider->GetAttr(row, col, kind);
2894 else
2895 return (wxGridCellAttr *)NULL;
2896}
2897
2898void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
2899{
2900 if ( m_attrProvider )
2901 {
2902 attr->SetKind(wxGridCellAttr::Cell);
2903 m_attrProvider->SetAttr(attr, row, col);
2904 }
2905 else
2906 {
2907 // as we take ownership of the pointer and don't store it, we must
2908 // free it now
2909 wxSafeDecRef(attr);
2910 }
2911}
2912
2913void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
2914{
2915 if ( m_attrProvider )
2916 {
2917 attr->SetKind(wxGridCellAttr::Row);
2918 m_attrProvider->SetRowAttr(attr, row);
2919 }
2920 else
2921 {
2922 // as we take ownership of the pointer and don't store it, we must
2923 // free it now
2924 wxSafeDecRef(attr);
2925 }
2926}
2927
2928void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
2929{
2930 if ( m_attrProvider )
2931 {
2932 attr->SetKind(wxGridCellAttr::Col);
2933 m_attrProvider->SetColAttr(attr, col);
2934 }
2935 else
2936 {
2937 // as we take ownership of the pointer and don't store it, we must
2938 // free it now
2939 wxSafeDecRef(attr);
2940 }
2941}
2942
2943bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
2944 size_t WXUNUSED(numRows) )
2945{
2946 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2947
2948 return false;
2949}
2950
2951bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
2952{
2953 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2954
2955 return false;
2956}
2957
2958bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
2959 size_t WXUNUSED(numRows) )
2960{
2961 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2962
2963 return false;
2964}
2965
2966bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
2967 size_t WXUNUSED(numCols) )
2968{
2969 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2970
2971 return false;
2972}
2973
2974bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
2975{
2976 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2977
2978 return false;
2979}
2980
2981bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
2982 size_t WXUNUSED(numCols) )
2983{
2984 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2985
2986 return false;
2987}
2988
2989
2990wxString wxGridTableBase::GetRowLabelValue( int row )
2991{
2992 wxString s;
2993 s << row + 1; // RD: Starting the rows at zero confuses users, no matter
2994 // how much it makes sense to us geeks.
2995 return s;
2996}
2997
2998wxString wxGridTableBase::GetColLabelValue( int col )
2999{
3000 // default col labels are:
3001 // cols 0 to 25 : A-Z
3002 // cols 26 to 675 : AA-ZZ
3003 // etc.
3004
3005 wxString s;
3006 unsigned int i, n;
3007 for ( n = 1; ; n++ )
3008 {
3009 s += (wxChar) (_T('A') + (wxChar)( col%26 ));
3010 col = col/26 - 1;
3011 if ( col < 0 ) break;
3012 }
3013
3014 // reverse the string...
3015 wxString s2;
3016 for ( i = 0; i < n; i++ )
3017 {
3018 s2 += s[n-i-1];
3019 }
3020
3021 return s2;
3022}
3023
3024
3025wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
3026{
3027 return wxGRID_VALUE_STRING;
3028}
3029
3030bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
3031 const wxString& typeName )
3032{
3033 return typeName == wxGRID_VALUE_STRING;
3034}
3035
3036bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
3037{
3038 return CanGetValueAs(row, col, typeName);
3039}
3040
3041long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
3042{
3043 return 0;
3044}
3045
3046double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
3047{
3048 return 0.0;
3049}
3050
3051bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
3052{
3053 return false;
3054}
3055
3056void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
3057 long WXUNUSED(value) )
3058{
3059}
3060
3061void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
3062 double WXUNUSED(value) )
3063{
3064}
3065
3066void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
3067 bool WXUNUSED(value) )
3068{
3069}
3070
3071
3072void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3073 const wxString& WXUNUSED(typeName) )
3074{
3075 return NULL;
3076}
3077
3078void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3079 const wxString& WXUNUSED(typeName),
3080 void* WXUNUSED(value) )
3081{
3082}
3083
3084//////////////////////////////////////////////////////////////////////
3085//
3086// Message class for the grid table to send requests and notifications
3087// to the grid view
3088//
3089
3090wxGridTableMessage::wxGridTableMessage()
3091{
3092 m_table = (wxGridTableBase *) NULL;
3093 m_id = -1;
3094 m_comInt1 = -1;
3095 m_comInt2 = -1;
3096}
3097
3098wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
3099 int commandInt1, int commandInt2 )
3100{
3101 m_table = table;
3102 m_id = id;
3103 m_comInt1 = commandInt1;
3104 m_comInt2 = commandInt2;
3105}
3106
3107
3108
3109//////////////////////////////////////////////////////////////////////
3110//
3111// A basic grid table for string data. An object of this class will
3112// created by wxGrid if you don't specify an alternative table class.
3113//
3114
3115WX_DEFINE_OBJARRAY(wxGridStringArray)
3116
3117IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
3118
3119wxGridStringTable::wxGridStringTable()
3120 : wxGridTableBase()
3121{
3122}
3123
3124wxGridStringTable::wxGridStringTable( int numRows, int numCols )
3125 : wxGridTableBase()
3126{
3127 m_data.Alloc( numRows );
3128
3129 wxArrayString sa;
3130 sa.Alloc( numCols );
3131 sa.Add( wxEmptyString, numCols );
3132
3133 m_data.Add( sa, numRows );
3134}
3135
3136wxGridStringTable::~wxGridStringTable()
3137{
3138}
3139
3140int wxGridStringTable::GetNumberRows()
3141{
3142 return m_data.GetCount();
3143}
3144
3145int wxGridStringTable::GetNumberCols()
3146{
3147 if ( m_data.GetCount() > 0 )
3148 return m_data[0].GetCount();
3149 else
3150 return 0;
3151}
3152
3153wxString wxGridStringTable::GetValue( int row, int col )
3154{
3155 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3156 wxEmptyString,
3157 _T("invalid row or column index in wxGridStringTable") );
3158
3159 return m_data[row][col];
3160}
3161
3162void wxGridStringTable::SetValue( int row, int col, const wxString& value )
3163{
3164 wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()),
3165 _T("invalid row or column index in wxGridStringTable") );
3166
3167 m_data[row][col] = value;
3168}
3169
3170bool wxGridStringTable::IsEmptyCell( int row, int col )
3171{
3172 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3173 true,
3174 _T("invalid row or column index in wxGridStringTable") );
3175
3176 return (m_data[row][col] == wxEmptyString);
3177}
3178
3179void wxGridStringTable::Clear()
3180{
3181 int row, col;
3182 int numRows, numCols;
3183
3184 numRows = m_data.GetCount();
3185 if ( numRows > 0 )
3186 {
3187 numCols = m_data[0].GetCount();
3188
3189 for ( row = 0; row < numRows; row++ )
3190 {
3191 for ( col = 0; col < numCols; col++ )
3192 {
3193 m_data[row][col] = wxEmptyString;
3194 }
3195 }
3196 }
3197}
3198
3199
3200bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
3201{
3202 size_t curNumRows = m_data.GetCount();
3203 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3204 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3205
3206 if ( pos >= curNumRows )
3207 {
3208 return AppendRows( numRows );
3209 }
3210
3211 wxArrayString sa;
3212 sa.Alloc( curNumCols );
3213 sa.Add( wxEmptyString, curNumCols );
3214 m_data.Insert( sa, pos, numRows );
3215 if ( GetView() )
3216 {
3217 wxGridTableMessage msg( this,
3218 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
3219 pos,
3220 numRows );
3221
3222 GetView()->ProcessTableMessage( msg );
3223 }
3224
3225 return true;
3226}
3227
3228bool wxGridStringTable::AppendRows( size_t numRows )
3229{
3230 size_t curNumRows = m_data.GetCount();
3231 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3232 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3233
3234 wxArrayString sa;
3235 if ( curNumCols > 0 )
3236 {
3237 sa.Alloc( curNumCols );
3238 sa.Add( wxEmptyString, curNumCols );
3239 }
3240
3241 m_data.Add( sa, numRows );
3242
3243 if ( GetView() )
3244 {
3245 wxGridTableMessage msg( this,
3246 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
3247 numRows );
3248
3249 GetView()->ProcessTableMessage( msg );
3250 }
3251
3252 return true;
3253}
3254
3255bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
3256{
3257 size_t curNumRows = m_data.GetCount();
3258
3259 if ( pos >= curNumRows )
3260 {
3261 wxFAIL_MSG( wxString::Format
3262 (
3263 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3264 (unsigned long)pos,
3265 (unsigned long)numRows,
3266 (unsigned long)curNumRows
3267 ) );
3268
3269 return false;
3270 }
3271
3272 if ( numRows > curNumRows - pos )
3273 {
3274 numRows = curNumRows - pos;
3275 }
3276
3277 if ( numRows >= curNumRows )
3278 {
3279 m_data.Clear();
3280 }
3281 else
3282 {
3283 m_data.RemoveAt( pos, numRows );
3284 }
3285 if ( GetView() )
3286 {
3287 wxGridTableMessage msg( this,
3288 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
3289 pos,
3290 numRows );
3291
3292 GetView()->ProcessTableMessage( msg );
3293 }
3294
3295 return true;
3296}
3297
3298bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
3299{
3300 size_t row, col;
3301
3302 size_t curNumRows = m_data.GetCount();
3303 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3304 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3305
3306 if ( pos >= curNumCols )
3307 {
3308 return AppendCols( numCols );
3309 }
3310
3311 for ( row = 0; row < curNumRows; row++ )
3312 {
3313 for ( col = pos; col < pos + numCols; col++ )
3314 {
3315 m_data[row].Insert( wxEmptyString, col );
3316 }
3317 }
3318 if ( GetView() )
3319 {
3320 wxGridTableMessage msg( this,
3321 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
3322 pos,
3323 numCols );
3324
3325 GetView()->ProcessTableMessage( msg );
3326 }
3327
3328 return true;
3329}
3330
3331bool wxGridStringTable::AppendCols( size_t numCols )
3332{
3333 size_t row;
3334
3335 size_t curNumRows = m_data.GetCount();
3336#if 0
3337 if ( !curNumRows )
3338 {
3339 // TODO: something better than this ?
3340 //
3341 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3342 return false;
3343 }
3344#endif
3345
3346 for ( row = 0; row < curNumRows; row++ )
3347 {
3348 m_data[row].Add( wxEmptyString, numCols );
3349 }
3350
3351 if ( GetView() )
3352 {
3353 wxGridTableMessage msg( this,
3354 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
3355 numCols );
3356
3357 GetView()->ProcessTableMessage( msg );
3358 }
3359
3360 return true;
3361}
3362
3363bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
3364{
3365 size_t row;
3366
3367 size_t curNumRows = m_data.GetCount();
3368 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3369 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3370
3371 if ( pos >= curNumCols )
3372 {
3373 wxFAIL_MSG( wxString::Format
3374 (
3375 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3376 (unsigned long)pos,
3377 (unsigned long)numCols,
3378 (unsigned long)curNumCols
3379 ) );
3380 return false;
3381 }
3382
3383 if ( numCols > curNumCols - pos )
3384 {
3385 numCols = curNumCols - pos;
3386 }
3387
3388 for ( row = 0; row < curNumRows; row++ )
3389 {
3390 if ( numCols >= curNumCols )
3391 {
3392 m_data[row].Clear();
3393 }
3394 else
3395 {
3396 m_data[row].RemoveAt( pos, numCols );
3397 }
3398 }
3399 if ( GetView() )
3400 {
3401 wxGridTableMessage msg( this,
3402 wxGRIDTABLE_NOTIFY_COLS_DELETED,
3403 pos,
3404 numCols );
3405
3406 GetView()->ProcessTableMessage( msg );
3407 }
3408
3409 return true;
3410}
3411
3412wxString wxGridStringTable::GetRowLabelValue( int row )
3413{
3414 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3415 {
3416 // using default label
3417 //
3418 return wxGridTableBase::GetRowLabelValue( row );
3419 }
3420 else
3421 {
3422 return m_rowLabels[ row ];
3423 }
3424}
3425
3426wxString wxGridStringTable::GetColLabelValue( int col )
3427{
3428 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3429 {
3430 // using default label
3431 //
3432 return wxGridTableBase::GetColLabelValue( col );
3433 }
3434 else
3435 {
3436 return m_colLabels[ col ];
3437 }
3438}
3439
3440void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
3441{
3442 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3443 {
3444 int n = m_rowLabels.GetCount();
3445 int i;
3446 for ( i = n; i <= row; i++ )
3447 {
3448 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
3449 }
3450 }
3451
3452 m_rowLabels[row] = value;
3453}
3454
3455void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
3456{
3457 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3458 {
3459 int n = m_colLabels.GetCount();
3460 int i;
3461 for ( i = n; i <= col; i++ )
3462 {
3463 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
3464 }
3465 }
3466
3467 m_colLabels[col] = value;
3468}
3469
3470
3471
3472//////////////////////////////////////////////////////////////////////
3473//////////////////////////////////////////////////////////////////////
3474
3475IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
3476
3477BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow )
3478 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
3479 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel)
3480 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
3481 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown )
3482 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp )
3483 EVT_CHAR ( wxGridRowLabelWindow::OnChar )
3484END_EVENT_TABLE()
3485
3486wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
3487 wxWindowID id,
3488 const wxPoint &pos, const wxSize &size )
3489 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
3490{
3491 m_owner = parent;
3492}
3493
3494void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3495{
3496 wxPaintDC dc(this);
3497
3498 // NO - don't do this because it will set both the x and y origin
3499 // coords to match the parent scrolled window and we just want to
3500 // set the y coord - MB
3501 //
3502 // m_owner->PrepareDC( dc );
3503
3504 int x, y;
3505 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3506 dc.SetDeviceOrigin( 0, -y );
3507
3508 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
3509 m_owner->DrawRowLabels( dc , rows );
3510}
3511
3512
3513void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
3514{
3515 m_owner->ProcessRowLabelMouseEvent( event );
3516}
3517
3518
3519void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
3520{
3521 m_owner->GetEventHandler()->ProcessEvent(event);
3522}
3523
3524
3525// This seems to be required for wxMotif otherwise the mouse
3526// cursor must be in the cell edit control to get key events
3527//
3528void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
3529{
3530 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3531}
3532
3533void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event )
3534{
3535 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3536}
3537
3538void wxGridRowLabelWindow::OnChar( wxKeyEvent& event )
3539{
3540 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3541}
3542
3543
3544
3545//////////////////////////////////////////////////////////////////////
3546
3547IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
3548
3549BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
3550 EVT_PAINT( wxGridColLabelWindow::OnPaint )
3551 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel)
3552 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
3553 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
3554 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp )
3555 EVT_CHAR ( wxGridColLabelWindow::OnChar )
3556END_EVENT_TABLE()
3557
3558wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
3559 wxWindowID id,
3560 const wxPoint &pos, const wxSize &size )
3561 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
3562{
3563 m_owner = parent;
3564}
3565
3566void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3567{
3568 wxPaintDC dc(this);
3569
3570 // NO - don't do this because it will set both the x and y origin
3571 // coords to match the parent scrolled window and we just want to
3572 // set the x coord - MB
3573 //
3574 // m_owner->PrepareDC( dc );
3575
3576 int x, y;
3577 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3578 dc.SetDeviceOrigin( -x, 0 );
3579
3580 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
3581 m_owner->DrawColLabels( dc , cols );
3582}
3583
3584
3585void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
3586{
3587 m_owner->ProcessColLabelMouseEvent( event );
3588}
3589
3590void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
3591{
3592 m_owner->GetEventHandler()->ProcessEvent(event);
3593}
3594
3595
3596// This seems to be required for wxMotif otherwise the mouse
3597// cursor must be in the cell edit control to get key events
3598//
3599void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
3600{
3601 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3602}
3603
3604void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event )
3605{
3606 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3607}
3608
3609void wxGridColLabelWindow::OnChar( wxKeyEvent& event )
3610{
3611 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3612}
3613
3614
3615//////////////////////////////////////////////////////////////////////
3616
3617IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
3618
3619BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
3620 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel)
3621 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
3622 EVT_PAINT( wxGridCornerLabelWindow::OnPaint)
3623 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
3624 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp )
3625 EVT_CHAR ( wxGridCornerLabelWindow::OnChar )
3626END_EVENT_TABLE()
3627
3628wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
3629 wxWindowID id,
3630 const wxPoint &pos, const wxSize &size )
3631 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
3632{
3633 m_owner = parent;
3634}
3635
3636void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3637{
3638 wxPaintDC dc(this);
3639
3640 int client_height = 0;
3641 int client_width = 0;
3642 GetClientSize( &client_width, &client_height );
3643
3644#if __WXGTK__
3645 wxRect rect;
3646 rect.SetX( 1 );
3647 rect.SetY( 1 );
3648 rect.SetWidth( client_width - 2 );
3649 rect.SetHeight( client_height - 2 );
3650
3651 wxRendererNative::Get().DrawHeaderButton( this, dc, rect, 0 );
3652#else
3653 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
3654 dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 );
3655 dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 );
3656 dc.DrawLine( 0, 0, client_width, 0 );
3657 dc.DrawLine( 0, 0, 0, client_height );
3658
3659 dc.SetPen( *wxWHITE_PEN );
3660 dc.DrawLine( 1, 1, client_width-1, 1 );
3661 dc.DrawLine( 1, 1, 1, client_height-1 );
3662#endif
3663}
3664
3665
3666void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
3667{
3668 m_owner->ProcessCornerLabelMouseEvent( event );
3669}
3670
3671
3672void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
3673{
3674 m_owner->GetEventHandler()->ProcessEvent(event);
3675}
3676
3677// This seems to be required for wxMotif otherwise the mouse
3678// cursor must be in the cell edit control to get key events
3679//
3680void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
3681{
3682 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3683}
3684
3685void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event )
3686{
3687 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3688}
3689
3690void wxGridCornerLabelWindow::OnChar( wxKeyEvent& event )
3691{
3692 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3693}
3694
3695
3696//////////////////////////////////////////////////////////////////////
3697
3698IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow )
3699
3700BEGIN_EVENT_TABLE( wxGridWindow, wxWindow )
3701 EVT_PAINT( wxGridWindow::OnPaint )
3702 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel)
3703 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
3704 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
3705 EVT_KEY_UP( wxGridWindow::OnKeyUp )
3706 EVT_CHAR ( wxGridWindow::OnChar )
3707 EVT_SET_FOCUS( wxGridWindow::OnFocus )
3708 EVT_KILL_FOCUS( wxGridWindow::OnFocus )
3709 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
3710END_EVENT_TABLE()
3711
3712wxGridWindow::wxGridWindow( wxGrid *parent,
3713 wxGridRowLabelWindow *rowLblWin,
3714 wxGridColLabelWindow *colLblWin,
3715 wxWindowID id,
3716 const wxPoint &pos,
3717 const wxSize &size )
3718 : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN|wxFULL_REPAINT_ON_RESIZE,
3719 wxT("grid window") )
3720
3721{
3722 m_owner = parent;
3723 m_rowLabelWin = rowLblWin;
3724 m_colLabelWin = colLblWin;
3725}
3726
3727
3728void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3729{
3730 wxPaintDC dc( this );
3731 m_owner->PrepareDC( dc );
3732 wxRegion reg = GetUpdateRegion();
3733 wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg );
3734 m_owner->DrawGridCellArea( dc , DirtyCells);
3735#if WXGRID_DRAW_LINES
3736 m_owner->DrawAllGridLines( dc, reg );
3737#endif
3738 m_owner->DrawGridSpace( dc );
3739 m_owner->DrawHighlight( dc , DirtyCells );
3740}
3741
3742
3743void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3744{
3745 wxWindow::ScrollWindow( dx, dy, rect );
3746 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3747 m_colLabelWin->ScrollWindow( dx, 0, rect );
3748}
3749
3750
3751void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3752{
3753 m_owner->ProcessGridCellMouseEvent( event );
3754}
3755
3756void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
3757{
3758 m_owner->GetEventHandler()->ProcessEvent(event);
3759}
3760
3761// This seems to be required for wxMotif/wxGTK otherwise the mouse
3762// cursor must be in the cell edit control to get key events
3763//
3764void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3765{
3766 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3767}
3768
3769void wxGridWindow::OnKeyUp( wxKeyEvent& event )
3770{
3771 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3772}
3773
3774void wxGridWindow::OnChar( wxKeyEvent& event )
3775{
3776 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3777}
3778
3779void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
3780{
3781}
3782
3783void wxGridWindow::OnFocus(wxFocusEvent& event)
3784{
3785 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3786 event.Skip();
3787}
3788
3789//////////////////////////////////////////////////////////////////////
3790
3791// Internal Helper function for computing row or column from some
3792// (unscrolled) coordinate value, using either
3793// m_defaultRowHeight/m_defaultColWidth or binary search on array
3794// of m_rowBottoms/m_ColRights to speed up the search!
3795
3796// Internal helper macros for simpler use of that function
3797
3798static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
3799 const wxArrayInt& BorderArray, int nMax,
3800 bool clipToMinMax);
3801
3802#define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3803 m_minAcceptableColWidth, \
3804 m_colRights, m_numCols, true)
3805#define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3806 m_minAcceptableRowHeight, \
3807 m_rowBottoms, m_numRows, true)
3808/////////////////////////////////////////////////////////////////////
3809
3810#if wxUSE_EXTENDED_RTTI
3811WX_DEFINE_FLAGS( wxGridStyle )
3812
3813wxBEGIN_FLAGS( wxGridStyle )
3814 // new style border flags, we put them first to
3815 // use them for streaming out
3816 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
3817 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
3818 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
3819 wxFLAGS_MEMBER(wxBORDER_RAISED)
3820 wxFLAGS_MEMBER(wxBORDER_STATIC)
3821 wxFLAGS_MEMBER(wxBORDER_NONE)
3822
3823 // old style border flags
3824 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
3825 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
3826 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
3827 wxFLAGS_MEMBER(wxRAISED_BORDER)
3828 wxFLAGS_MEMBER(wxSTATIC_BORDER)
3829 wxFLAGS_MEMBER(wxBORDER)
3830
3831 // standard window styles
3832 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
3833 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
3834 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
3835 wxFLAGS_MEMBER(wxWANTS_CHARS)
3836 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3837 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
3838 wxFLAGS_MEMBER(wxVSCROLL)
3839 wxFLAGS_MEMBER(wxHSCROLL)
3840
3841wxEND_FLAGS( wxGridStyle )
3842
3843IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
3844
3845wxBEGIN_PROPERTIES_TABLE(wxGrid)
3846 wxHIDE_PROPERTY( Children )
3847 wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3848wxEND_PROPERTIES_TABLE()
3849
3850wxBEGIN_HANDLERS_TABLE(wxGrid)
3851wxEND_HANDLERS_TABLE()
3852
3853wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
3854
3855/*
3856