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