1. more corrections for ref counted editors/renderers (doesn't crash any more,
[wxWidgets.git] / include / wx / generic / grid.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/grid.h
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by:
6 // Created: 1/08/1999
7 // RCS-ID: $Id$
8 // Copyright: (c) Michael Bedward
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/defs.h"
13
14 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
15 #include "gridg.h"
16 #else
17
18 #ifndef __WXGRID_H__
19 #define __WXGRID_H__
20
21 #ifdef __GNUG__
22 #pragma interface "grid.h"
23 #endif
24
25 #include "wx/hash.h"
26 #include "wx/panel.h"
27 #include "wx/scrolwin.h"
28 #include "wx/string.h"
29 #include "wx/scrolbar.h"
30 #include "wx/event.h"
31 #include "wx/combobox.h"
32 #include "wx/dynarray.h"
33 #include "wx/timer.h"
34
35 // ----------------------------------------------------------------------------
36 // constants
37 // ----------------------------------------------------------------------------
38
39 // Default parameters for wxGrid
40 //
41 #define WXGRID_DEFAULT_NUMBER_ROWS 10
42 #define WXGRID_DEFAULT_NUMBER_COLS 10
43 #ifdef __WXMSW__
44 #define WXGRID_DEFAULT_ROW_HEIGHT 25
45 #else
46 #define WXGRID_DEFAULT_ROW_HEIGHT 30
47 #endif // __WXMSW__
48 #define WXGRID_DEFAULT_COL_WIDTH 80
49 #define WXGRID_DEFAULT_COL_LABEL_HEIGHT 32
50 #define WXGRID_DEFAULT_ROW_LABEL_WIDTH 82
51 #define WXGRID_LABEL_EDGE_ZONE 5
52 #define WXGRID_MIN_ROW_HEIGHT 15
53 #define WXGRID_MIN_COL_WIDTH 15
54 #define WXGRID_DEFAULT_SCROLLBAR_WIDTH 16
55
56 // type names for grid table values
57 #define wxGRID_VALUE_STRING _T("string")
58 #define wxGRID_VALUE_BOOL _T("bool")
59 #define wxGRID_VALUE_NUMBER _T("long")
60 #define wxGRID_VALUE_FLOAT _T("double")
61
62 #define wxGRID_VALUE_TEXT wxGRID_VALUE_STRING
63 #define wxGRID_VALUE_LONG wxGRID_VALUE_NUMBER
64
65 // ----------------------------------------------------------------------------
66 // forward declarations
67 // ----------------------------------------------------------------------------
68
69 class WXDLLEXPORT wxGrid;
70 class WXDLLEXPORT wxGridCellAttr;
71 class WXDLLEXPORT wxGridCellAttrProviderData;
72 class WXDLLEXPORT wxGridColLabelWindow;
73 class WXDLLEXPORT wxGridCornerLabelWindow;
74 class WXDLLEXPORT wxGridRowLabelWindow;
75 class WXDLLEXPORT wxGridTableBase;
76 class WXDLLEXPORT wxGridWindow;
77 class WXDLLEXPORT wxGridTypeRegistry;
78
79 class WXDLLEXPORT wxCheckBox;
80 class WXDLLEXPORT wxComboBox;
81 class WXDLLEXPORT wxTextCtrl;
82 class WXDLLEXPORT wxSpinCtrl;
83
84 // ----------------------------------------------------------------------------
85 // macros
86 // ----------------------------------------------------------------------------
87
88 #define wxSafeIncRef(p) if ( p ) (p)->IncRef()
89 #define wxSafeDecRef(p) if ( p ) (p)->DecRef()
90
91 // ----------------------------------------------------------------------------
92 // wxGridCellRenderer: this class is responsible for actually drawing the cell
93 // in the grid. You may pass it to the wxGridCellAttr (below) to change the
94 // format of one given cell or to wxGrid::SetDefaultRenderer() to change the
95 // view of all cells. This is an ABC, you will normally use one of the
96 // predefined derived classes or derive your own class from it.
97 // ----------------------------------------------------------------------------
98
99 class WXDLLEXPORT wxGridCellRenderer
100 {
101 public:
102 wxGridCellRenderer() { m_nRef = 1; }
103
104 // this class is ref counted: it is created with ref count of 1, so
105 // calling DecRef() once will delete it. Calling IncRef() allows to lock
106 // it until the matching DecRef() is called
107 void IncRef() { m_nRef++; }
108 void DecRef() { if ( !--m_nRef ) delete this; }
109
110 // draw the given cell on the provided DC inside the given rectangle
111 // using the style specified by the attribute and the default or selected
112 // state corresponding to the isSelected value.
113 //
114 // this pure virtual function has a default implementation which will
115 // prepare the DC using the given attribute: it will draw the rectangle
116 // with the bg colour from attr and set the text colour and font
117 virtual void Draw(wxGrid& grid,
118 wxGridCellAttr& attr,
119 wxDC& dc,
120 const wxRect& rect,
121 int row, int col,
122 bool isSelected) = 0;
123
124 // get the preferred size of the cell for its contents
125 virtual wxSize GetBestSize(wxGrid& grid,
126 wxGridCellAttr& attr,
127 wxDC& dc,
128 int row, int col) = 0;
129
130 // interpret renderer parameters: arbitrary string whose interpretatin is
131 // left to the derived classes
132 virtual void SetParameters(const wxString& params);
133
134 protected:
135 // virtual dtor for any base class - private because only DecRef() can
136 // delete us
137 virtual ~wxGridCellRenderer();
138
139 private:
140 size_t m_nRef;
141
142 // suppress the stupid gcc warning about the class having private dtor and
143 // no friends
144 friend class wxGridCellRendererDummyFriend;
145 };
146
147 // the default renderer for the cells containing string data
148 class WXDLLEXPORT wxGridCellStringRenderer : public wxGridCellRenderer
149 {
150 public:
151 // draw the string
152 virtual void Draw(wxGrid& grid,
153 wxGridCellAttr& attr,
154 wxDC& dc,
155 const wxRect& rect,
156 int row, int col,
157 bool isSelected);
158
159 // return the string extent
160 virtual wxSize GetBestSize(wxGrid& grid,
161 wxGridCellAttr& attr,
162 wxDC& dc,
163 int row, int col);
164
165 protected:
166 // set the text colours before drawing
167 void SetTextColoursAndFont(wxGrid& grid,
168 wxGridCellAttr& attr,
169 wxDC& dc,
170 bool isSelected);
171
172 // calc the string extent for given string/font
173 wxSize DoGetBestSize(wxGridCellAttr& attr,
174 wxDC& dc,
175 const wxString& text);
176 };
177
178 // the default renderer for the cells containing numeric (long) data
179 class WXDLLEXPORT wxGridCellNumberRenderer : public wxGridCellStringRenderer
180 {
181 public:
182 // draw the string right aligned
183 virtual void Draw(wxGrid& grid,
184 wxGridCellAttr& attr,
185 wxDC& dc,
186 const wxRect& rect,
187 int row, int col,
188 bool isSelected);
189
190 virtual wxSize GetBestSize(wxGrid& grid,
191 wxGridCellAttr& attr,
192 wxDC& dc,
193 int row, int col);
194
195 protected:
196 wxString GetString(wxGrid& grid, int row, int col);
197 };
198
199 class WXDLLEXPORT wxGridCellFloatRenderer : public wxGridCellStringRenderer
200 {
201 public:
202 wxGridCellFloatRenderer(int width = -1, int precision = -1);
203
204 // get/change formatting parameters
205 int GetWidth() const { return m_width; }
206 void SetWidth(int width) { m_width = width; m_format.clear(); }
207 int GetPrecision() const { return m_precision; }
208 void SetPrecision(int precision) { m_precision = precision; m_format.clear(); }
209
210 // draw the string right aligned with given width/precision
211 virtual void Draw(wxGrid& grid,
212 wxGridCellAttr& attr,
213 wxDC& dc,
214 const wxRect& rect,
215 int row, int col,
216 bool isSelected);
217
218 virtual wxSize GetBestSize(wxGrid& grid,
219 wxGridCellAttr& attr,
220 wxDC& dc,
221 int row, int col);
222
223 // parameters string format is "width[,precision]"
224 virtual void SetParameters(const wxString& params);
225
226 protected:
227 wxString GetString(wxGrid& grid, int row, int col);
228
229 private:
230 // formatting parameters
231 int m_width,
232 m_precision;
233
234 wxString m_format;
235 };
236
237 // renderer for boolean fields
238 class WXDLLEXPORT wxGridCellBoolRenderer : public wxGridCellRenderer
239 {
240 public:
241 // draw a check mark or nothing
242 virtual void Draw(wxGrid& grid,
243 wxGridCellAttr& attr,
244 wxDC& dc,
245 const wxRect& rect,
246 int row, int col,
247 bool isSelected);
248
249 // return the checkmark size
250 virtual wxSize GetBestSize(wxGrid& grid,
251 wxGridCellAttr& attr,
252 wxDC& dc,
253 int row, int col);
254
255 private:
256 static wxSize ms_sizeCheckMark;
257 };
258
259 // ----------------------------------------------------------------------------
260 // wxGridCellEditor: This class is responsible for providing and manipulating
261 // the in-place edit controls for the grid. Instances of wxGridCellEditor
262 // (actually, instances of derived classes since it is an ABC) can be
263 // associated with the cell attributes for individual cells, rows, columns, or
264 // even for the entire grid.
265 // ----------------------------------------------------------------------------
266
267 class WXDLLEXPORT wxGridCellEditor
268 {
269 public:
270 wxGridCellEditor();
271
272 // this class is ref counted: it is created with ref count of 1, so
273 // calling DecRef() once will delete it. Calling IncRef() allows to lock
274 // it until the matching DecRef() is called
275 void IncRef() { m_nRef++; }
276 void DecRef() { if ( !--m_nRef ) delete this; }
277
278 bool IsCreated() { return m_control != NULL; }
279
280 // Creates the actual edit control
281 virtual void Create(wxWindow* parent,
282 wxWindowID id,
283 wxEvtHandler* evtHandler) = 0;
284
285 // Size and position the edit control
286 virtual void SetSize(const wxRect& rect);
287
288 // Show or hide the edit control, use the specified attributes to set
289 // colours/fonts for it
290 virtual void Show(bool show, wxGridCellAttr *attr = (wxGridCellAttr *)NULL);
291
292 // Draws the part of the cell not occupied by the control: the base class
293 // version just fills it with background colour from the attribute
294 virtual void PaintBackground(const wxRect& rectCell, wxGridCellAttr *attr);
295
296 // Fetch the value from the table and prepare the edit control
297 // to begin editing. Set the focus to the edit control.
298 virtual void BeginEdit(int row, int col, wxGrid* grid) = 0;
299
300 // Complete the editing of the current cell. Returns true if the value has
301 // changed. If necessary, the control may be destroyed.
302 virtual bool EndEdit(int row, int col, wxGrid* grid) = 0;
303
304 // Reset the value in the control back to its starting value
305 virtual void Reset() = 0;
306
307 // If the editor is enabled by pressing keys on the grid,
308 // this will be called to let the editor do something about
309 // that first key if desired.
310 virtual void StartingKey(wxKeyEvent& event);
311
312 // if the editor is enabled by clicking on the cell, this method will be
313 // called
314 virtual void StartingClick();
315
316 // Some types of controls on some platforms may need some help
317 // with the Return key.
318 virtual void HandleReturn(wxKeyEvent& event);
319
320 // Final cleanup
321 virtual void Destroy();
322
323 protected:
324 // the dtor is private because only DecRef() can delete us
325 virtual ~wxGridCellEditor();
326
327 // the ref count - when it goes to 0, we die
328 size_t m_nRef;
329
330 // the control we show on screen
331 wxControl* m_control;
332
333 // if we change the colours/font of the control from the default ones, we
334 // must restore the default later and we save them here between calls to
335 // Show(TRUE) and Show(FALSE)
336 wxColour m_colFgOld,
337 m_colBgOld;
338 wxFont m_fontOld;
339
340 // suppress the stupid gcc warning about the class having private dtor and
341 // no friends
342 friend class wxGridCellEditorDummyFriend;
343 };
344
345 // the editor for string/text data
346 class WXDLLEXPORT wxGridCellTextEditor : public wxGridCellEditor
347 {
348 public:
349 wxGridCellTextEditor();
350
351 virtual void Create(wxWindow* parent,
352 wxWindowID id,
353 wxEvtHandler* evtHandler);
354 virtual void SetSize(const wxRect& rect);
355
356 virtual void PaintBackground(const wxRect& rectCell, wxGridCellAttr *attr);
357
358 virtual void BeginEdit(int row, int col, wxGrid* grid);
359 virtual bool EndEdit(int row, int col, wxGrid* grid);
360
361 virtual void Reset();
362 virtual void StartingKey(wxKeyEvent& event);
363 virtual void HandleReturn(wxKeyEvent& event);
364
365 protected:
366 wxTextCtrl *Text() const { return (wxTextCtrl *)m_control; }
367
368 // parts of our virtual functions reused by the derived classes
369 void DoBeginEdit(const wxString& startValue);
370 void DoReset(const wxString& startValue);
371
372 private:
373 wxString m_startValue;
374 };
375
376 // the editor for numeric (long) data
377 class WXDLLEXPORT wxGridCellNumberEditor : public wxGridCellTextEditor
378 {
379 public:
380 // allows to specify the range - if min == max == -1, no range checking is
381 // done
382 wxGridCellNumberEditor(int min = -1, int max = -1);
383
384 virtual void Create(wxWindow* parent,
385 wxWindowID id,
386 wxEvtHandler* evtHandler);
387
388 virtual void BeginEdit(int row, int col, wxGrid* grid);
389 virtual bool EndEdit(int row, int col, wxGrid* grid);
390
391 virtual void Reset();
392 virtual void StartingKey(wxKeyEvent& event);
393
394 protected:
395 wxSpinCtrl *Spin() const { return (wxSpinCtrl *)m_control; }
396
397 // if HasRange(), we use wxSpinCtrl - otherwise wxTextCtrl
398 bool HasRange() const { return m_min != m_max; }
399
400 // string representation of m_valueOld
401 wxString GetString() const
402 { return wxString::Format(_T("%ld"), m_valueOld); }
403
404 private:
405 int m_min,
406 m_max;
407
408 long m_valueOld;
409 };
410
411 // the editor for floating point numbers (double) data
412 class WXDLLEXPORT wxGridCellFloatEditor : public wxGridCellTextEditor
413 {
414 public:
415 virtual void Create(wxWindow* parent,
416 wxWindowID id,
417 wxEvtHandler* evtHandler);
418
419 virtual void BeginEdit(int row, int col, wxGrid* grid);
420 virtual bool EndEdit(int row, int col, wxGrid* grid);
421
422 virtual void Reset();
423 virtual void StartingKey(wxKeyEvent& event);
424
425 protected:
426 // string representation of m_valueOld
427 wxString GetString() const
428 { return wxString::Format(_T("%f"), m_valueOld); }
429
430 private:
431 double m_valueOld;
432 };
433
434 // the editor for boolean data
435 class WXDLLEXPORT wxGridCellBoolEditor : public wxGridCellEditor
436 {
437 public:
438 virtual void Create(wxWindow* parent,
439 wxWindowID id,
440 wxEvtHandler* evtHandler);
441
442 virtual void SetSize(const wxRect& rect);
443 virtual void Show(bool show, wxGridCellAttr *attr = (wxGridCellAttr *)NULL);
444
445 virtual void BeginEdit(int row, int col, wxGrid* grid);
446 virtual bool EndEdit(int row, int col, wxGrid* grid);
447
448 virtual void Reset();
449 virtual void StartingClick();
450
451 protected:
452 wxCheckBox *CBox() const { return (wxCheckBox *)m_control; }
453
454 private:
455 bool m_startValue;
456 };
457
458 // the editor for string data allowing to choose from the list of strings
459 class WXDLLEXPORT wxGridCellChoiceEditor : public wxGridCellEditor
460 {
461 public:
462 // if !allowOthers, user can't type a string not in choices array
463 wxGridCellChoiceEditor(size_t count, const wxChar* choices[],
464 bool allowOthers = FALSE);
465
466 virtual void Create(wxWindow* parent,
467 wxWindowID id,
468 wxEvtHandler* evtHandler);
469
470 virtual void PaintBackground(const wxRect& rectCell, wxGridCellAttr *attr);
471
472 virtual void BeginEdit(int row, int col, wxGrid* grid);
473 virtual bool EndEdit(int row, int col, wxGrid* grid);
474
475 virtual void Reset();
476
477 protected:
478 wxComboBox *Combo() const { return (wxComboBox *)m_control; }
479
480 private:
481 wxString m_startValue;
482 wxArrayString m_choices;
483 bool m_allowOthers;
484 };
485
486 // ----------------------------------------------------------------------------
487 // wxGridCellAttr: this class can be used to alter the cells appearance in
488 // the grid by changing their colour/font/... from default. An object of this
489 // class may be returned by wxGridTable::GetAttr().
490 // ----------------------------------------------------------------------------
491
492 class WXDLLEXPORT wxGridCellAttr
493 {
494 public:
495 // ctors
496 wxGridCellAttr()
497 {
498 Init();
499 SetAlignment(0, 0);
500 }
501
502 // VZ: considering the number of members wxGridCellAttr has now, this ctor
503 // seems to be pretty useless... may be we should just remove it?
504 wxGridCellAttr(const wxColour& colText,
505 const wxColour& colBack,
506 const wxFont& font,
507 int hAlign,
508 int vAlign)
509 : m_colText(colText), m_colBack(colBack), m_font(font)
510 {
511 Init();
512 SetAlignment(hAlign, vAlign);
513 }
514
515 // creates a new copy of this object
516 wxGridCellAttr *Clone() const;
517
518 // this class is ref counted: it is created with ref count of 1, so
519 // calling DecRef() once will delete it. Calling IncRef() allows to lock
520 // it until the matching DecRef() is called
521 void IncRef() { m_nRef++; }
522 void DecRef() { if ( !--m_nRef ) delete this; }
523
524 // setters
525 void SetTextColour(const wxColour& colText) { m_colText = colText; }
526 void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
527 void SetFont(const wxFont& font) { m_font = font; }
528 void SetAlignment(int hAlign, int vAlign)
529 {
530 m_hAlign = hAlign;
531 m_vAlign = vAlign;
532 }
533 void SetReadOnly(bool isReadOnly = TRUE) { m_isReadOnly = isReadOnly; }
534
535 // takes ownership of the pointer
536 void SetRenderer(wxGridCellRenderer *renderer)
537 { wxSafeDecRef(m_renderer); m_renderer = renderer; }
538 void SetEditor(wxGridCellEditor* editor)
539 { wxSafeDecRef(m_editor); m_editor = editor; }
540
541 // accessors
542 bool HasTextColour() const { return m_colText.Ok(); }
543 bool HasBackgroundColour() const { return m_colBack.Ok(); }
544 bool HasFont() const { return m_font.Ok(); }
545 bool HasAlignment() const { return m_hAlign || m_vAlign; }
546 bool HasRenderer() const { return m_renderer != NULL; }
547 bool HasEditor() const { return m_editor != NULL; }
548
549 const wxColour& GetTextColour() const;
550 const wxColour& GetBackgroundColour() const;
551 const wxFont& GetFont() const;
552 void GetAlignment(int *hAlign, int *vAlign) const;
553 wxGridCellRenderer *GetRenderer(wxGrid* grid, int row, int col) const;
554 wxGridCellEditor *GetEditor(wxGrid* grid, int row, int col) const;
555
556 bool IsReadOnly() const { return m_isReadOnly; }
557
558 void SetDefAttr(wxGridCellAttr* defAttr) { m_defGridAttr = defAttr; }
559
560 private:
561 // the common part of all ctors
562 void Init()
563 {
564 m_nRef = 1;
565
566 m_isReadOnly = FALSE;
567
568 m_renderer = NULL;
569 m_editor = NULL;
570 }
571
572 // the dtor is private because only DecRef() can delete us
573 ~wxGridCellAttr()
574 {
575 wxSafeDecRef(m_renderer);
576 wxSafeDecRef(m_editor);
577 }
578
579 // the ref count - when it goes to 0, we die
580 size_t m_nRef;
581
582 wxColour m_colText,
583 m_colBack;
584 wxFont m_font;
585 int m_hAlign,
586 m_vAlign;
587
588 wxGridCellRenderer* m_renderer;
589 wxGridCellEditor* m_editor;
590 wxGridCellAttr* m_defGridAttr;
591
592 bool m_isReadOnly;
593
594 // use Clone() instead
595 DECLARE_NO_COPY_CLASS(wxGridCellAttr);
596
597 // suppress the stupid gcc warning about the class having private dtor and
598 // no friends
599 friend class wxGridCellAttrDummyFriend;
600 };
601
602 // ----------------------------------------------------------------------------
603 // wxGridCellAttrProvider: class used by wxGridTableBase to retrieve/store the
604 // cell attributes.
605 // ----------------------------------------------------------------------------
606
607 // implementation note: we separate it from wxGridTableBase because we wish to
608 // avoid deriving a new table class if possible, and sometimes it will be
609 // enough to just derive another wxGridCellAttrProvider instead
610 //
611 // the default implementation is reasonably efficient for the generic case,
612 // but you might still wish to implement your own for some specific situations
613 // if you have performance problems with the stock one
614 class WXDLLEXPORT wxGridCellAttrProvider
615 {
616 public:
617 wxGridCellAttrProvider();
618 virtual ~wxGridCellAttrProvider();
619
620 // DecRef() must be called on the returned pointer
621 virtual wxGridCellAttr *GetAttr(int row, int col) const;
622
623 // all these functions take ownership of the pointer, don't call DecRef()
624 // on it
625 virtual void SetAttr(wxGridCellAttr *attr, int row, int col);
626 virtual void SetRowAttr(wxGridCellAttr *attr, int row);
627 virtual void SetColAttr(wxGridCellAttr *attr, int col);
628
629 // these functions must be called whenever some rows/cols are deleted
630 // because the internal data must be updated then
631 void UpdateAttrRows( size_t pos, int numRows );
632 void UpdateAttrCols( size_t pos, int numCols );
633
634 private:
635 void InitData();
636
637 wxGridCellAttrProviderData *m_data;
638 };
639
640 //////////////////////////////////////////////////////////////////////
641 //
642 // Grid table classes
643 //
644 //////////////////////////////////////////////////////////////////////
645
646
647 class WXDLLEXPORT wxGridTableBase : public wxObject
648 {
649 public:
650 wxGridTableBase();
651 virtual ~wxGridTableBase();
652
653 // You must override these functions in a derived table class
654 //
655 virtual long GetNumberRows() = 0;
656 virtual long GetNumberCols() = 0;
657 virtual bool IsEmptyCell( int row, int col ) = 0;
658 virtual wxString GetValue( int row, int col ) = 0;
659 virtual void SetValue( int row, int col, const wxString& value ) = 0;
660
661 // Data type determination and value access
662 virtual wxString GetTypeName( int row, int col );
663 virtual bool CanGetValueAs( int row, int col, const wxString& typeName );
664 virtual bool CanSetValueAs( int row, int col, const wxString& typeName );
665
666 virtual long GetValueAsLong( int row, int col );
667 virtual double GetValueAsDouble( int row, int col );
668 virtual bool GetValueAsBool( int row, int col );
669
670 virtual void SetValueAsLong( int row, int col, long value );
671 virtual void SetValueAsDouble( int row, int col, double value );
672 virtual void SetValueAsBool( int row, int col, bool value );
673
674 // For user defined types
675 virtual void* GetValueAsCustom( int row, int col, const wxString& typeName );
676 virtual void SetValueAsCustom( int row, int col, const wxString& typeName, void* value );
677
678
679 // Overriding these is optional
680 //
681 virtual void SetView( wxGrid *grid ) { m_view = grid; }
682 virtual wxGrid * GetView() const { return m_view; }
683
684 virtual void Clear() {}
685 virtual bool InsertRows( size_t pos = 0, size_t numRows = 1 );
686 virtual bool AppendRows( size_t numRows = 1 );
687 virtual bool DeleteRows( size_t pos = 0, size_t numRows = 1 );
688 virtual bool InsertCols( size_t pos = 0, size_t numCols = 1 );
689 virtual bool AppendCols( size_t numCols = 1 );
690 virtual bool DeleteCols( size_t pos = 0, size_t numCols = 1 );
691
692 virtual wxString GetRowLabelValue( int row );
693 virtual wxString GetColLabelValue( int col );
694 virtual void SetRowLabelValue( int WXUNUSED(row), const wxString& ) {}
695 virtual void SetColLabelValue( int WXUNUSED(col), const wxString& ) {}
696
697 // Attribute handling
698 //
699
700 // give us the attr provider to use - we take ownership of the pointer
701 void SetAttrProvider(wxGridCellAttrProvider *attrProvider);
702
703 // get the currently used attr provider (may be NULL)
704 wxGridCellAttrProvider *GetAttrProvider() const { return m_attrProvider; }
705
706 // Does this table allow attributes? Default implementation creates
707 // a wxGridCellAttrProvider if necessary.
708 virtual bool CanHaveAttributes();
709
710
711 // change row/col number in attribute if needed
712 virtual void UpdateAttrRows( size_t pos, int numRows );
713 virtual void UpdateAttrCols( size_t pos, int numCols );
714
715 // by default forwarded to wxGridCellAttrProvider if any. May be
716 // overridden to handle attributes directly in the table.
717 virtual wxGridCellAttr *GetAttr( int row, int col );
718
719 // these functions take ownership of the pointer
720 virtual void SetAttr(wxGridCellAttr* attr, int row, int col);
721 virtual void SetRowAttr(wxGridCellAttr *attr, int row);
722 virtual void SetColAttr(wxGridCellAttr *attr, int col);
723
724 private:
725 wxGrid * m_view;
726 wxGridCellAttrProvider *m_attrProvider;
727
728 DECLARE_ABSTRACT_CLASS( wxGridTableBase );
729 };
730
731
732 // ----------------------------------------------------------------------------
733 // wxGridTableMessage
734 // ----------------------------------------------------------------------------
735
736 // IDs for messages sent from grid table to view
737 //
738 enum wxGridTableRequest
739 {
740 wxGRIDTABLE_REQUEST_VIEW_GET_VALUES = 2000,
741 wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES,
742 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
743 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
744 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
745 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
746 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
747 wxGRIDTABLE_NOTIFY_COLS_DELETED
748 };
749
750 class WXDLLEXPORT wxGridTableMessage
751 {
752 public:
753 wxGridTableMessage();
754 wxGridTableMessage( wxGridTableBase *table, int id,
755 int comInt1 = -1,
756 int comInt2 = -1 );
757
758 void SetTableObject( wxGridTableBase *table ) { m_table = table; }
759 wxGridTableBase * GetTableObject() const { return m_table; }
760 void SetId( int id ) { m_id = id; }
761 int GetId() { return m_id; }
762 void SetCommandInt( int comInt1 ) { m_comInt1 = comInt1; }
763 int GetCommandInt() { return m_comInt1; }
764 void SetCommandInt2( int comInt2 ) { m_comInt2 = comInt2; }
765 int GetCommandInt2() { return m_comInt2; }
766
767 private:
768 wxGridTableBase *m_table;
769 int m_id;
770 int m_comInt1;
771 int m_comInt2;
772 };
773
774
775
776 // ------ wxGridStringArray
777 // A 2-dimensional array of strings for data values
778 //
779
780 WX_DECLARE_EXPORTED_OBJARRAY(wxArrayString, wxGridStringArray);
781
782
783
784 // ------ wxGridStringTable
785 //
786 // Simplest type of data table for a grid for small tables of strings
787 // that are stored in memory
788 //
789
790 class WXDLLEXPORT wxGridStringTable : public wxGridTableBase
791 {
792 public:
793 wxGridStringTable();
794 wxGridStringTable( int numRows, int numCols );
795 ~wxGridStringTable();
796
797 // these are pure virtual in wxGridTableBase
798 //
799 long GetNumberRows();
800 long GetNumberCols();
801 wxString GetValue( int row, int col );
802 void SetValue( int row, int col, const wxString& s );
803 bool IsEmptyCell( int row, int col );
804
805 // overridden functions from wxGridTableBase
806 //
807 void Clear();
808 bool InsertRows( size_t pos = 0, size_t numRows = 1 );
809 bool AppendRows( size_t numRows = 1 );
810 bool DeleteRows( size_t pos = 0, size_t numRows = 1 );
811 bool InsertCols( size_t pos = 0, size_t numCols = 1 );
812 bool AppendCols( size_t numCols = 1 );
813 bool DeleteCols( size_t pos = 0, size_t numCols = 1 );
814
815 void SetRowLabelValue( int row, const wxString& );
816 void SetColLabelValue( int col, const wxString& );
817 wxString GetRowLabelValue( int row );
818 wxString GetColLabelValue( int col );
819
820 private:
821 wxGridStringArray m_data;
822
823 // These only get used if you set your own labels, otherwise the
824 // GetRow/ColLabelValue functions return wxGridTableBase defaults
825 //
826 wxArrayString m_rowLabels;
827 wxArrayString m_colLabels;
828
829 DECLARE_DYNAMIC_CLASS( wxGridStringTable )
830 };
831
832
833
834 // ============================================================================
835 // Grid view classes
836 // ============================================================================
837
838 // ----------------------------------------------------------------------------
839 // wxGridCellCoords: location of a cell in the grid
840 // ----------------------------------------------------------------------------
841
842 class WXDLLEXPORT wxGridCellCoords
843 {
844 public:
845 wxGridCellCoords() { m_row = m_col = -1; }
846 wxGridCellCoords( int r, int c ) { m_row = r; m_col = c; }
847
848 // default copy ctor is ok
849
850 long GetRow() const { return m_row; }
851 void SetRow( long n ) { m_row = n; }
852 long GetCol() const { return m_col; }
853 void SetCol( long n ) { m_col = n; }
854 void Set( long row, long col ) { m_row = row; m_col = col; }
855
856 wxGridCellCoords& operator=( const wxGridCellCoords& other )
857 {
858 if ( &other != this )
859 {
860 m_row=other.m_row;
861 m_col=other.m_col;
862 }
863 return *this;
864 }
865
866 bool operator==( const wxGridCellCoords& other ) const
867 {
868 return (m_row == other.m_row && m_col == other.m_col);
869 }
870
871 bool operator!=( const wxGridCellCoords& other ) const
872 {
873 return (m_row != other.m_row || m_col != other.m_col);
874 }
875
876 bool operator!() const
877 {
878 return (m_row == -1 && m_col == -1 );
879 }
880
881 private:
882 long m_row;
883 long m_col;
884 };
885
886
887 // For comparisons...
888 //
889 extern wxGridCellCoords wxGridNoCellCoords;
890 extern wxRect wxGridNoCellRect;
891
892 // An array of cell coords...
893 //
894 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellCoords, wxGridCellCoordsArray);
895
896 // ----------------------------------------------------------------------------
897 // wxGrid
898 // ----------------------------------------------------------------------------
899
900 class WXDLLEXPORT wxGrid : public wxScrolledWindow
901 {
902 public:
903 wxGrid()
904 {
905 Create();
906 }
907
908 wxGrid( wxWindow *parent,
909 wxWindowID id,
910 const wxPoint& pos = wxDefaultPosition,
911 const wxSize& size = wxDefaultSize,
912 long style = wxWANTS_CHARS,
913 const wxString& name = wxPanelNameStr );
914
915 ~wxGrid();
916
917 bool CreateGrid( int numRows, int numCols );
918
919
920 // ------ grid dimensions
921 //
922 int GetNumberRows() { return m_numRows; }
923 int GetNumberCols() { return m_numCols; }
924
925
926 // ------ display update functions
927 //
928 void CalcRowLabelsExposed( wxRegion& reg );
929
930 void CalcColLabelsExposed( wxRegion& reg );
931 void CalcCellsExposed( wxRegion& reg );
932
933
934 // ------ event handlers
935 //
936 void ProcessRowLabelMouseEvent( wxMouseEvent& event );
937 void ProcessColLabelMouseEvent( wxMouseEvent& event );
938 void ProcessCornerLabelMouseEvent( wxMouseEvent& event );
939 void ProcessGridCellMouseEvent( wxMouseEvent& event );
940 bool ProcessTableMessage( wxGridTableMessage& );
941
942 void DoEndDragResizeRow();
943 void DoEndDragResizeCol();
944
945 wxGridTableBase * GetTable() const { return m_table; }
946 bool SetTable( wxGridTableBase *table, bool takeOwnership=FALSE );
947
948 void ClearGrid();
949 bool InsertRows( int pos = 0, int numRows = 1, bool updateLabels=TRUE );
950 bool AppendRows( int numRows = 1, bool updateLabels=TRUE );
951 bool DeleteRows( int pos = 0, int numRows = 1, bool updateLabels=TRUE );
952 bool InsertCols( int pos = 0, int numCols = 1, bool updateLabels=TRUE );
953 bool AppendCols( int numCols = 1, bool updateLabels=TRUE );
954 bool DeleteCols( int pos = 0, int numCols = 1, bool updateLabels=TRUE );
955
956 void DrawGridCellArea( wxDC& dc );
957 void DrawGridSpace( wxDC& dc );
958 void DrawCellBorder( wxDC& dc, const wxGridCellCoords& );
959 void DrawAllGridLines( wxDC& dc, const wxRegion & reg );
960 void DrawCell( wxDC& dc, const wxGridCellCoords& );
961 void DrawHighlight(wxDC& dc);
962
963 // this function is called when the current cell highlight must be redrawn
964 // and may be overridden by the user
965 virtual void DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr );
966
967 void DrawRowLabels( wxDC& dc );
968 void DrawRowLabel( wxDC& dc, int row );
969
970 void DrawColLabels( wxDC& dc );
971 void DrawColLabel( wxDC& dc, int col );
972
973
974 // ------ Cell text drawing functions
975 //
976 void DrawTextRectangle( wxDC& dc, const wxString&, const wxRect&,
977 int horizontalAlignment = wxLEFT,
978 int verticalAlignment = wxTOP );
979
980 // Split a string containing newline chararcters into an array of
981 // strings and return the number of lines
982 //
983 void StringToLines( const wxString& value, wxArrayString& lines );
984
985 void GetTextBoxSize( wxDC& dc,
986 wxArrayString& lines,
987 long *width, long *height );
988
989
990 // ------
991 // Code that does a lot of grid modification can be enclosed
992 // between BeginBatch() and EndBatch() calls to avoid screen
993 // flicker
994 //
995 void BeginBatch() { m_batchCount++; }
996 void EndBatch() { if ( m_batchCount > 0 ) m_batchCount--; }
997 int GetBatchCount() { return m_batchCount; }
998
999
1000 // ------ edit control functions
1001 //
1002 bool IsEditable() { return m_editable; }
1003 void EnableEditing( bool edit );
1004
1005 void EnableCellEditControl( bool enable = TRUE );
1006 void DisableCellEditControl() { EnableCellEditControl(FALSE); }
1007 bool CanEnableCellControl() const;
1008 bool IsCellEditControlEnabled() const;
1009
1010 bool IsCurrentCellReadOnly() const;
1011
1012 void ShowCellEditControl();
1013 void HideCellEditControl();
1014 void SaveEditControlValue();
1015
1016
1017 // ------ grid location functions
1018 // Note that all of these functions work with the logical coordinates of
1019 // grid cells and labels so you will need to convert from device
1020 // coordinates for mouse events etc.
1021 //
1022 void XYToCell( int x, int y, wxGridCellCoords& );
1023 int YToRow( int y );
1024 int XToCol( int x );
1025
1026 int YToEdgeOfRow( int y );
1027 int XToEdgeOfCol( int x );
1028
1029 wxRect CellToRect( int row, int col );
1030 wxRect CellToRect( const wxGridCellCoords& coords )
1031 { return CellToRect( coords.GetRow(), coords.GetCol() ); }
1032
1033 int GetGridCursorRow() { return m_currentCellCoords.GetRow(); }
1034 int GetGridCursorCol() { return m_currentCellCoords.GetCol(); }
1035
1036 // check to see if a cell is either wholly visible (the default arg) or
1037 // at least partially visible in the grid window
1038 //
1039 bool IsVisible( int row, int col, bool wholeCellVisible = TRUE );
1040 bool IsVisible( const wxGridCellCoords& coords, bool wholeCellVisible = TRUE )
1041 { return IsVisible( coords.GetRow(), coords.GetCol(), wholeCellVisible ); }
1042 void MakeCellVisible( int row, int col );
1043 void MakeCellVisible( const wxGridCellCoords& coords )
1044 { MakeCellVisible( coords.GetRow(), coords.GetCol() ); }
1045
1046
1047 // ------ grid cursor movement functions
1048 //
1049 void SetGridCursor( int row, int col )
1050 { SetCurrentCell( wxGridCellCoords(row, col) ); }
1051
1052 bool MoveCursorUp();
1053 bool MoveCursorDown();
1054 bool MoveCursorLeft();
1055 bool MoveCursorRight();
1056 bool MovePageDown();
1057 bool MovePageUp();
1058 bool MoveCursorUpBlock();
1059 bool MoveCursorDownBlock();
1060 bool MoveCursorLeftBlock();
1061 bool MoveCursorRightBlock();
1062
1063
1064 // ------ label and gridline formatting
1065 //
1066 int GetDefaultRowLabelSize() { return WXGRID_DEFAULT_ROW_LABEL_WIDTH; }
1067 int GetRowLabelSize() { return m_rowLabelWidth; }
1068 int GetDefaultColLabelSize() { return WXGRID_DEFAULT_COL_LABEL_HEIGHT; }
1069 int GetColLabelSize() { return m_colLabelHeight; }
1070 wxColour GetLabelBackgroundColour() { return m_labelBackgroundColour; }
1071 wxColour GetLabelTextColour() { return m_labelTextColour; }
1072 wxFont GetLabelFont() { return m_labelFont; }
1073 void GetRowLabelAlignment( int *horiz, int *vert );
1074 void GetColLabelAlignment( int *horiz, int *vert );
1075 wxString GetRowLabelValue( int row );
1076 wxString GetColLabelValue( int col );
1077 wxColour GetGridLineColour() { return m_gridLineColour; }
1078
1079 void SetRowLabelSize( int width );
1080 void SetColLabelSize( int height );
1081 void SetLabelBackgroundColour( const wxColour& );
1082 void SetLabelTextColour( const wxColour& );
1083 void SetLabelFont( const wxFont& );
1084 void SetRowLabelAlignment( int horiz, int vert );
1085 void SetColLabelAlignment( int horiz, int vert );
1086 void SetRowLabelValue( int row, const wxString& );
1087 void SetColLabelValue( int col, const wxString& );
1088 void SetGridLineColour( const wxColour& );
1089
1090 void EnableDragRowSize( bool enable = TRUE );
1091 void DisableDragRowSize() { EnableDragRowSize( FALSE ); }
1092 bool CanDragRowSize() { return m_canDragRowSize; }
1093 void EnableDragColSize( bool enable = TRUE );
1094 void DisableDragColSize() { EnableDragColSize( FALSE ); }
1095 bool CanDragColSize() { return m_canDragColSize; }
1096 void EnableDragGridSize(bool enable = TRUE);
1097 void DisableDragGridSize() { EnableDragGridSize(FALSE); }
1098 bool CanDragGridSize() { return m_canDragGridSize; }
1099
1100 // this sets the specified attribute for all cells in this row/col
1101 void SetRowAttr(int row, wxGridCellAttr *attr);
1102 void SetColAttr(int col, wxGridCellAttr *attr);
1103
1104 // shortcuts for setting the column parameters
1105
1106 // set the format for the data in the column: default is string
1107 void SetColFormatBool(int col);
1108 void SetColFormatNumber(int col);
1109 void SetColFormatFloat(int col, int width = -1, int precision = -1);
1110 void SetColFormatCustom(int col, const wxString& typeName);
1111
1112 void EnableGridLines( bool enable = TRUE );
1113 bool GridLinesEnabled() { return m_gridLinesEnabled; }
1114
1115 // ------ row and col formatting
1116 //
1117 int GetDefaultRowSize();
1118 int GetRowSize( int row );
1119 int GetDefaultColSize();
1120 int GetColSize( int col );
1121 wxColour GetDefaultCellBackgroundColour();
1122 wxColour GetCellBackgroundColour( int row, int col );
1123 wxColour GetDefaultCellTextColour();
1124 wxColour GetCellTextColour( int row, int col );
1125 wxFont GetDefaultCellFont();
1126 wxFont GetCellFont( int row, int col );
1127 void GetDefaultCellAlignment( int *horiz, int *vert );
1128 void GetCellAlignment( int row, int col, int *horiz, int *vert );
1129
1130 void SetDefaultRowSize( int height, bool resizeExistingRows = FALSE );
1131 void SetRowSize( int row, int height );
1132 void SetDefaultColSize( int width, bool resizeExistingCols = FALSE );
1133
1134 void SetColSize( int col, int width );
1135
1136 // automatically size the column or row to fit to its contents, if
1137 // setAsMin is TRUE, this optimal width will also be set as minimal width
1138 // for this column
1139 void AutoSizeColumn( int col, bool setAsMin = TRUE )
1140 { AutoSizeColOrRow(col, setAsMin, TRUE); }
1141 void AutoSizeRow( int row, bool setAsMin = TRUE )
1142 { AutoSizeColOrRow(row, setAsMin, FALSE); }
1143
1144 // auto size all columns (very ineffective for big grids!)
1145 void AutoSizeColumns( bool setAsMin = TRUE )
1146 { (void)SetOrCalcColumnSizes(FALSE, setAsMin); }
1147
1148 void AutoSizeRows( bool setAsMin = TRUE )
1149 { (void)SetOrCalcRowSizes(FALSE, setAsMin); }
1150
1151 // auto size the grid, that is make the columns/rows of the "right" size
1152 // and also set the grid size to just fit its contents
1153 void AutoSize();
1154
1155 // column won't be resized to be lesser width - this must be called during
1156 // the grid creation because it won't resize the column if it's already
1157 // narrower than the minimal width
1158 void SetColMinimalWidth( int col, int width );
1159 void SetRowMinimalHeight( int row, int width );
1160
1161 void SetDefaultCellBackgroundColour( const wxColour& );
1162 void SetCellBackgroundColour( int row, int col, const wxColour& );
1163 void SetDefaultCellTextColour( const wxColour& );
1164
1165 void SetCellTextColour( int row, int col, const wxColour& );
1166 void SetDefaultCellFont( const wxFont& );
1167 void SetCellFont( int row, int col, const wxFont& );
1168 void SetDefaultCellAlignment( int horiz, int vert );
1169 void SetCellAlignment( int row, int col, int horiz, int vert );
1170
1171 // takes ownership of the pointer
1172 void SetDefaultRenderer(wxGridCellRenderer *renderer);
1173 void SetCellRenderer(int row, int col, wxGridCellRenderer *renderer);
1174 wxGridCellRenderer *GetDefaultRenderer() const;
1175 wxGridCellRenderer* GetCellRenderer(int row, int col);
1176
1177 // takes ownership of the pointer
1178 void SetDefaultEditor(wxGridCellEditor *editor);
1179 void SetCellEditor(int row, int col, wxGridCellEditor *editor);
1180 wxGridCellEditor *GetDefaultEditor() const;
1181 wxGridCellEditor* GetCellEditor(int row, int col);
1182
1183
1184
1185 // ------ cell value accessors
1186 //
1187 wxString GetCellValue( int row, int col )
1188 {
1189 if ( m_table )
1190 {
1191 return m_table->GetValue( row, col );
1192 }
1193 else
1194 {
1195 return wxEmptyString;
1196 }
1197 }
1198
1199 wxString GetCellValue( const wxGridCellCoords& coords )
1200 { return GetCellValue( coords.GetRow(), coords.GetCol() ); }
1201
1202 void SetCellValue( int row, int col, const wxString& s );
1203 void SetCellValue( const wxGridCellCoords& coords, const wxString& s )
1204 { SetCellValue( coords.GetRow(), coords.GetCol(), s ); }
1205
1206 // returns TRUE if the cell can't be edited
1207 bool IsReadOnly(int row, int col) const;
1208
1209 // make the cell editable/readonly
1210 void SetReadOnly(int row, int col, bool isReadOnly = TRUE);
1211
1212 // ------ selections of blocks of cells
1213 //
1214 void SelectRow( int row, bool addToSelected = FALSE );
1215 void SelectCol( int col, bool addToSelected = FALSE );
1216
1217 void SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol );
1218
1219 void SelectBlock( const wxGridCellCoords& topLeft,
1220 const wxGridCellCoords& bottomRight )
1221 { SelectBlock( topLeft.GetRow(), topLeft.GetCol(),
1222 bottomRight.GetRow(), bottomRight.GetCol() ); }
1223
1224 void SelectAll();
1225
1226 bool IsSelection()
1227 { return ( m_selectedTopLeft != wxGridNoCellCoords &&
1228 m_selectedBottomRight != wxGridNoCellCoords );
1229 }
1230
1231 void ClearSelection();
1232
1233 bool IsInSelection( int row, int col )
1234 { return ( IsSelection() &&
1235 row >= m_selectedTopLeft.GetRow() &&
1236 col >= m_selectedTopLeft.GetCol() &&
1237 row <= m_selectedBottomRight.GetRow() &&
1238 col <= m_selectedBottomRight.GetCol() );
1239 }
1240
1241 bool IsInSelection( const wxGridCellCoords& coords )
1242 { return IsInSelection( coords.GetRow(), coords.GetCol() ); }
1243
1244 void GetSelection( int* topRow, int* leftCol, int* bottomRow, int* rightCol )
1245 {
1246 // these will all be -1 if there is no selected block
1247 //
1248 *topRow = m_selectedTopLeft.GetRow();
1249 *leftCol = m_selectedTopLeft.GetCol();
1250 *bottomRow = m_selectedBottomRight.GetRow();
1251 *rightCol = m_selectedBottomRight.GetCol();
1252 }
1253
1254
1255 // This function returns the rectangle that encloses the block of cells
1256 // limited by TopLeft and BottomRight cell in device coords and clipped
1257 // to the client size of the grid window.
1258 //
1259 wxRect BlockToDeviceRect( const wxGridCellCoords & topLeft,
1260 const wxGridCellCoords & bottomRight );
1261
1262 // This function returns the rectangle that encloses the selected cells
1263 // in device coords and clipped to the client size of the grid window.
1264 //
1265 wxRect SelectionToDeviceRect()
1266 {
1267 return BlockToDeviceRect( m_selectedTopLeft,
1268 m_selectedBottomRight );
1269 }
1270
1271 // Access or update the selection fore/back colours
1272 wxColour GetSelectionBackground() const
1273 { return m_selectionBackground; }
1274 wxColour GetSelectionForeground() const
1275 { return m_selectionForeground; }
1276
1277 void SetSelectionBackground(const wxColour& c) { m_selectionBackground = c; }
1278 void SetSelectionForeground(const wxColour& c) { m_selectionForeground = c; }
1279
1280
1281 // Methods for a registry for mapping data types to Renderers/Editors
1282 void RegisterDataType(const wxString& typeName,
1283 wxGridCellRenderer* renderer,
1284 wxGridCellEditor* editor);
1285 wxGridCellEditor* GetDefaultEditorForCell(int row, int col) const;
1286 wxGridCellEditor* GetDefaultEditorForCell(const wxGridCellCoords& c) const
1287 { return GetDefaultEditorForCell(c.GetRow(), c.GetCol()); }
1288 wxGridCellRenderer* GetDefaultRendererForCell(int row, int col) const;
1289 wxGridCellEditor* GetDefaultEditorForType(const wxString& typeName) const;
1290 wxGridCellRenderer* GetDefaultRendererForType(const wxString& typeName) const;
1291
1292 // grid may occupy more space than needed for its rows/columns, this
1293 // function allows to set how big this extra space is
1294 void SetMargins(int extraWidth, int extraHeight)
1295 {
1296 m_extraWidth = extraWidth;
1297 m_extraHeight = extraHeight;
1298 }
1299
1300 // ------ For compatibility with previous wxGrid only...
1301 //
1302 // ************************************************
1303 // ** Don't use these in new code because they **
1304 // ** are liable to disappear in a future **
1305 // ** revision **
1306 // ************************************************
1307 //
1308
1309 wxGrid( wxWindow *parent,
1310 int x, int y, int w = -1, int h = -1,
1311 long style = wxWANTS_CHARS,
1312 const wxString& name = wxPanelNameStr )
1313 : wxScrolledWindow( parent, -1, wxPoint(x,y), wxSize(w,h),
1314 (style|wxWANTS_CHARS), name )
1315 {
1316 Create();
1317 }
1318
1319 void SetCellValue( const wxString& val, int row, int col )
1320 { SetCellValue( row, col, val ); }
1321
1322 void UpdateDimensions()
1323 { CalcDimensions(); }
1324
1325 int GetRows() { return GetNumberRows(); }
1326 int GetCols() { return GetNumberCols(); }
1327 int GetCursorRow() { return GetGridCursorRow(); }
1328 int GetCursorColumn() { return GetGridCursorCol(); }
1329
1330 int GetScrollPosX() { return 0; }
1331 int GetScrollPosY() { return 0; }
1332
1333 void SetScrollX( int x ) { }
1334 void SetScrollY( int y ) { }
1335
1336 void SetColumnWidth( int col, int width )
1337 { SetColSize( col, width ); }
1338
1339 int GetColumnWidth( int col )
1340 { return GetColSize( col ); }
1341
1342 void SetRowHeight( int row, int height )
1343 { SetRowSize( row, height ); }
1344
1345 // GetRowHeight() is below
1346
1347 int GetViewHeight() // returned num whole rows visible
1348 { return 0; }
1349
1350 int GetViewWidth() // returned num whole cols visible
1351 { return 0; }
1352
1353 void SetLabelSize( int orientation, int sz )
1354 {
1355 if ( orientation == wxHORIZONTAL )
1356 SetColLabelSize( sz );
1357 else
1358 SetRowLabelSize( sz );
1359 }
1360
1361 int GetLabelSize( int orientation )
1362 {
1363 if ( orientation == wxHORIZONTAL )
1364 return GetColLabelSize();
1365 else
1366 return GetRowLabelSize();
1367 }
1368
1369 void SetLabelAlignment( int orientation, int align )
1370 {
1371 if ( orientation == wxHORIZONTAL )
1372 SetColLabelAlignment( align, -1 );
1373 else
1374 SetRowLabelAlignment( align, -1 );
1375 }
1376
1377 int GetLabelAlignment( int orientation, int WXUNUSED(align) )
1378 {
1379 int h, v;
1380 if ( orientation == wxHORIZONTAL )
1381 {
1382 GetColLabelAlignment( &h, &v );
1383 return h;
1384 }
1385 else
1386 {
1387 GetRowLabelAlignment( &h, &v );
1388 return h;
1389 }
1390 }
1391
1392 void SetLabelValue( int orientation, const wxString& val, int pos )
1393 {
1394 if ( orientation == wxHORIZONTAL )
1395 SetColLabelValue( pos, val );
1396 else
1397 SetRowLabelValue( pos, val );
1398 }
1399
1400 wxString GetLabelValue( int orientation, int pos)
1401 {
1402 if ( orientation == wxHORIZONTAL )
1403 return GetColLabelValue( pos );
1404 else
1405 return GetRowLabelValue( pos );
1406 }
1407
1408 wxFont GetCellTextFont() const
1409 { return m_defaultCellAttr->GetFont(); }
1410
1411 wxFont GetCellTextFont(int WXUNUSED(row), int WXUNUSED(col)) const
1412 { return m_defaultCellAttr->GetFont(); }
1413
1414 void SetCellTextFont(const wxFont& fnt)
1415 { SetDefaultCellFont( fnt ); }
1416
1417 void SetCellTextFont(const wxFont& fnt, int row, int col)
1418 { SetCellFont( row, col, fnt ); }
1419
1420 void SetCellTextColour(const wxColour& val, int row, int col)
1421 { SetCellTextColour( row, col, val ); }
1422
1423 void SetCellTextColour(const wxColour& col)
1424 { SetDefaultCellTextColour( col ); }
1425
1426 void SetCellBackgroundColour(const wxColour& col)
1427 { SetDefaultCellBackgroundColour( col ); }
1428
1429 void SetCellBackgroundColour(const wxColour& colour, int row, int col)
1430 { SetCellBackgroundColour( row, col, colour ); }
1431
1432 bool GetEditable() { return IsEditable(); }
1433 void SetEditable( bool edit = TRUE ) { EnableEditing( edit ); }
1434 bool GetEditInPlace() { return IsCellEditControlEnabled(); }
1435
1436 void SetEditInPlace(bool edit = TRUE) { }
1437
1438 void SetCellAlignment( int align, int row, int col)
1439 { SetCellAlignment(row, col, align, wxCENTER); }
1440 void SetCellAlignment( int WXUNUSED(align) ) {}
1441 void SetCellBitmap(wxBitmap *WXUNUSED(bitmap), int WXUNUSED(row), int WXUNUSED(col))
1442 { }
1443 void SetDividerPen(const wxPen& WXUNUSED(pen)) { }
1444 wxPen& GetDividerPen() const { return wxNullPen; }
1445 void OnActivate(bool WXUNUSED(active)) {}
1446
1447 // ******** End of compatibility functions **********
1448
1449
1450
1451 // ------ control IDs
1452 enum { wxGRID_CELLCTRL = 2000,
1453 wxGRID_TOPCTRL };
1454
1455 // ------ control types
1456 enum { wxGRID_TEXTCTRL = 2100,
1457 wxGRID_CHECKBOX,
1458 wxGRID_CHOICE,
1459 wxGRID_COMBOBOX };
1460
1461 // overridden wxWindow methods
1462 virtual void Fit();
1463
1464 protected:
1465 virtual wxSize DoGetBestSize() const;
1466
1467 bool m_created;
1468 bool m_displayed;
1469
1470 wxGridWindow *m_gridWin;
1471 wxGridRowLabelWindow *m_rowLabelWin;
1472 wxGridColLabelWindow *m_colLabelWin;
1473 wxGridCornerLabelWindow *m_cornerLabelWin;
1474
1475 wxGridTableBase *m_table;
1476 bool m_ownTable;
1477
1478 int m_left;
1479 int m_top;
1480 int m_right;
1481 int m_bottom;
1482
1483 int m_numRows;
1484 int m_numCols;
1485
1486 wxGridCellCoords m_currentCellCoords;
1487
1488 wxGridCellCoords m_selectedTopLeft;
1489 wxGridCellCoords m_selectedBottomRight;
1490 wxColour m_selectionBackground;
1491 wxColour m_selectionForeground;
1492
1493 // NB: *never* access m_row/col arrays directly because they are created
1494 // on demand, *always* use accessor functions instead!
1495
1496 // init the m_rowHeights/Bottoms arrays with default values
1497 void InitRowHeights();
1498
1499 int m_defaultRowHeight;
1500 wxArrayInt m_rowHeights;
1501 wxArrayInt m_rowBottoms;
1502
1503 // init the m_colWidths/Rights arrays
1504 void InitColWidths();
1505
1506 int m_defaultColWidth;
1507 wxArrayInt m_colWidths;
1508 wxArrayInt m_colRights;
1509
1510 // get the col/row coords
1511 int GetColWidth(int col) const;
1512 int GetColLeft(int col) const;
1513 int GetColRight(int col) const;
1514
1515 // this function must be public for compatibility...
1516 public:
1517 int GetRowHeight(int row) const;
1518 protected:
1519
1520 int GetRowTop(int row) const;
1521 int GetRowBottom(int row) const;
1522
1523 int m_rowLabelWidth;
1524 int m_colLabelHeight;
1525
1526 // the size of the margin left to the right and bottom of the cell area
1527 int m_extraWidth,
1528 m_extraHeight;
1529
1530 wxColour m_labelBackgroundColour;
1531 wxColour m_labelTextColour;
1532 wxFont m_labelFont;
1533
1534 int m_rowLabelHorizAlign;
1535 int m_rowLabelVertAlign;
1536 int m_colLabelHorizAlign;
1537 int m_colLabelVertAlign;
1538
1539 bool m_defaultRowLabelValues;
1540 bool m_defaultColLabelValues;
1541
1542 wxColour m_gridLineColour;
1543 bool m_gridLinesEnabled;
1544
1545 // common part of AutoSizeColumn/Row() and GetBestSize()
1546 int SetOrCalcColumnSizes(bool calcOnly, bool setAsMin = TRUE);
1547 int SetOrCalcRowSizes(bool calcOnly, bool setAsMin = TRUE);
1548
1549 // common part of AutoSizeColumn/Row()
1550 void AutoSizeColOrRow(int n, bool setAsMin, bool column /* or row? */);
1551
1552 // if a column has a minimal width, it will be the value for it in this
1553 // hash table
1554 wxHashTableLong m_colMinWidths,
1555 m_rowMinHeights;
1556
1557 // get the minimal width of the given column/row
1558 int GetColMinimalWidth(int col) const;
1559 int GetRowMinimalHeight(int col) const;
1560
1561 // do we have some place to store attributes in?
1562 bool CanHaveAttributes();
1563
1564 // returns the attribute we may modify in place: a new one if this cell
1565 // doesn't have any yet or the existing one if it does
1566 //
1567 // DecRef() must be called on the returned pointer, as usual
1568 wxGridCellAttr *GetOrCreateCellAttr(int row, int col) const;
1569
1570 // cell attribute cache (currently we only cache 1, may be will do
1571 // more/better later)
1572 struct CachedAttr
1573 {
1574 int row, col;
1575 wxGridCellAttr *attr;
1576 } m_attrCache;
1577
1578 // invalidates the attribute cache
1579 void ClearAttrCache();
1580
1581 // adds an attribute to cache
1582 void CacheAttr(int row, int col, wxGridCellAttr *attr) const;
1583
1584 // looks for an attr in cache, returns TRUE if found
1585 bool LookupAttr(int row, int col, wxGridCellAttr **attr) const;
1586
1587 // looks for the attr in cache, if not found asks the table and caches the
1588 // result
1589 wxGridCellAttr *GetCellAttr(int row, int col) const;
1590 wxGridCellAttr *GetCellAttr(const wxGridCellCoords& coords )
1591 { return GetCellAttr( coords.GetRow(), coords.GetCol() ); }
1592
1593 // the default cell attr object for cells that don't have their own
1594 wxGridCellAttr* m_defaultCellAttr;
1595
1596
1597 wxGridCellCoordsArray m_cellsExposed;
1598 wxArrayInt m_rowsExposed;
1599 wxArrayInt m_colsExposed;
1600 wxArrayInt m_rowLabelsExposed;
1601 wxArrayInt m_colLabelsExposed;
1602
1603 bool m_inOnKeyDown;
1604 int m_batchCount;
1605
1606
1607 wxGridTypeRegistry* m_typeRegistry;
1608
1609 enum CursorMode
1610 {
1611 WXGRID_CURSOR_SELECT_CELL,
1612 WXGRID_CURSOR_RESIZE_ROW,
1613 WXGRID_CURSOR_RESIZE_COL,
1614 WXGRID_CURSOR_SELECT_ROW,
1615 WXGRID_CURSOR_SELECT_COL
1616 };
1617
1618 // this method not only sets m_cursorMode but also sets the correct cursor
1619 // for the given mode and, if captureMouse is not FALSE releases the mouse
1620 // if it was captured and captures it if it must be captured
1621 //
1622 // for this to work, you should always use it and not set m_cursorMode
1623 // directly!
1624 void ChangeCursorMode(CursorMode mode,
1625 wxWindow *win = (wxWindow *)NULL,
1626 bool captureMouse = TRUE);
1627
1628 wxWindow *m_winCapture; // the window which captured the mouse
1629 CursorMode m_cursorMode;
1630
1631 bool m_canDragRowSize;
1632 bool m_canDragColSize;
1633 bool m_canDragGridSize;
1634 int m_dragLastPos;
1635 int m_dragRowOrCol;
1636 bool m_isDragging;
1637 wxPoint m_startDragPos;
1638
1639 bool m_waitForSlowClick;
1640
1641 wxGridCellCoords m_selectionStart;
1642
1643 wxCursor m_rowResizeCursor;
1644 wxCursor m_colResizeCursor;
1645
1646 bool m_editable; // applies to whole grid
1647 bool m_cellEditCtrlEnabled; // is in-place edit currently shown?
1648
1649
1650 void Create();
1651 void Init();
1652 void CalcDimensions();
1653 void CalcWindowSizes();
1654 bool Redimension( wxGridTableMessage& );
1655
1656
1657 bool SendEvent( const wxEventType, int row, int col, wxMouseEvent& );
1658 bool SendEvent( const wxEventType, int row, int col );
1659 bool SendEvent( const wxEventType type)
1660 {
1661 return SendEvent(type,
1662 m_currentCellCoords.GetRow(),
1663 m_currentCellCoords.GetCol());
1664 }
1665
1666 void OnPaint( wxPaintEvent& );
1667 void OnSize( wxSizeEvent& );
1668 void OnKeyDown( wxKeyEvent& );
1669 void OnEraseBackground( wxEraseEvent& );
1670
1671
1672 void SetCurrentCell( const wxGridCellCoords& coords );
1673 void SetCurrentCell( int row, int col )
1674 { SetCurrentCell( wxGridCellCoords(row, col) ); }
1675
1676
1677 // ------ functions to get/send data (see also public functions)
1678 //
1679 bool GetModelValues();
1680 bool SetModelValues();
1681
1682
1683 DECLARE_DYNAMIC_CLASS( wxGrid )
1684 DECLARE_EVENT_TABLE()
1685 };
1686
1687 // ----------------------------------------------------------------------------
1688 // Grid event class and event types
1689 // ----------------------------------------------------------------------------
1690
1691 class WXDLLEXPORT wxGridEvent : public wxNotifyEvent
1692 {
1693 public:
1694 wxGridEvent()
1695 : wxNotifyEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
1696 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1697 {
1698 }
1699
1700 wxGridEvent(int id, wxEventType type, wxObject* obj,
1701 int row=-1, int col=-1, int x=-1, int y=-1,
1702 bool control=FALSE, bool shift=FALSE, bool alt=FALSE, bool meta=FALSE);
1703
1704 virtual int GetRow() { return m_row; }
1705 virtual int GetCol() { return m_col; }
1706 wxPoint GetPosition() { return wxPoint( m_x, m_y ); }
1707 bool ControlDown() { return m_control; }
1708 bool MetaDown() { return m_meta; }
1709 bool ShiftDown() { return m_shift; }
1710 bool AltDown() { return m_alt; }
1711
1712 protected:
1713 int m_row;
1714 int m_col;
1715 int m_x;
1716 int m_y;
1717 bool m_control;
1718 bool m_meta;
1719 bool m_shift;
1720 bool m_alt;
1721
1722 DECLARE_DYNAMIC_CLASS(wxGridEvent)
1723 };
1724
1725 class WXDLLEXPORT wxGridSizeEvent : public wxNotifyEvent
1726 {
1727 public:
1728 wxGridSizeEvent()
1729 : wxNotifyEvent(), m_rowOrCol(-1), m_x(-1), m_y(-1),
1730 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1731 {
1732 }
1733
1734 wxGridSizeEvent(int id, wxEventType type, wxObject* obj,
1735 int rowOrCol=-1, int x=-1, int y=-1,
1736 bool control=FALSE, bool shift=FALSE, bool alt=FALSE, bool meta=FALSE);
1737
1738 int GetRowOrCol() { return m_rowOrCol; }
1739 wxPoint GetPosition() { return wxPoint( m_x, m_y ); }
1740 bool ControlDown() { return m_control; }
1741 bool MetaDown() { return m_meta; }
1742 bool ShiftDown() { return m_shift; }
1743 bool AltDown() { return m_alt; }
1744
1745 protected:
1746 int m_rowOrCol;
1747 int m_x;
1748 int m_y;
1749 bool m_control;
1750 bool m_meta;
1751 bool m_shift;
1752 bool m_alt;
1753
1754 DECLARE_DYNAMIC_CLASS(wxGridSizeEvent)
1755 };
1756
1757
1758 class WXDLLEXPORT wxGridRangeSelectEvent : public wxNotifyEvent
1759 {
1760 public:
1761 wxGridRangeSelectEvent()
1762 : wxNotifyEvent()
1763 {
1764 m_topLeft = wxGridNoCellCoords;
1765 m_bottomRight = wxGridNoCellCoords;
1766 m_control = FALSE;
1767 m_meta = FALSE;
1768 m_shift = FALSE;
1769 m_alt = FALSE;
1770 }
1771
1772 wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
1773 const wxGridCellCoords& topLeft,
1774 const wxGridCellCoords& bottomRight,
1775 bool control=FALSE, bool shift=FALSE,
1776 bool alt=FALSE, bool meta=FALSE);
1777
1778 wxGridCellCoords GetTopLeftCoords() { return m_topLeft; }
1779 wxGridCellCoords GetBottomRightCoords() { return m_bottomRight; }
1780 int GetTopRow() { return m_topLeft.GetRow(); }
1781 int GetBottomRow() { return m_bottomRight.GetRow(); }
1782 int GetLeftCol() { return m_topLeft.GetCol(); }
1783 int GetRightCol() { return m_bottomRight.GetCol(); }
1784 bool ControlDown() { return m_control; }
1785 bool MetaDown() { return m_meta; }
1786 bool ShiftDown() { return m_shift; }
1787 bool AltDown() { return m_alt; }
1788
1789 protected:
1790 wxGridCellCoords m_topLeft;
1791 wxGridCellCoords m_bottomRight;
1792 bool m_control;
1793 bool m_meta;
1794 bool m_shift;
1795 bool m_alt;
1796
1797 DECLARE_DYNAMIC_CLASS(wxGridRangeSelectEvent)
1798 };
1799
1800 // TODO move to wx/event.h
1801 const wxEventType wxEVT_GRID_CELL_LEFT_CLICK = wxEVT_FIRST + 1580;
1802 const wxEventType wxEVT_GRID_CELL_RIGHT_CLICK = wxEVT_FIRST + 1581;
1803 const wxEventType wxEVT_GRID_CELL_LEFT_DCLICK = wxEVT_FIRST + 1582;
1804 const wxEventType wxEVT_GRID_CELL_RIGHT_DCLICK = wxEVT_FIRST + 1583;
1805 const wxEventType wxEVT_GRID_LABEL_LEFT_CLICK = wxEVT_FIRST + 1584;
1806 const wxEventType wxEVT_GRID_LABEL_RIGHT_CLICK = wxEVT_FIRST + 1585;
1807 const wxEventType wxEVT_GRID_LABEL_LEFT_DCLICK = wxEVT_FIRST + 1586;
1808 const wxEventType wxEVT_GRID_LABEL_RIGHT_DCLICK = wxEVT_FIRST + 1587;
1809 const wxEventType wxEVT_GRID_ROW_SIZE = wxEVT_FIRST + 1588;
1810 const wxEventType wxEVT_GRID_COL_SIZE = wxEVT_FIRST + 1589;
1811 const wxEventType wxEVT_GRID_RANGE_SELECT = wxEVT_FIRST + 1590;
1812 const wxEventType wxEVT_GRID_CELL_CHANGE = wxEVT_FIRST + 1591;
1813 const wxEventType wxEVT_GRID_SELECT_CELL = wxEVT_FIRST + 1592;
1814 const wxEventType wxEVT_GRID_EDITOR_SHOWN = wxEVT_FIRST + 1593;
1815 const wxEventType wxEVT_GRID_EDITOR_HIDDEN = wxEVT_FIRST + 1594;
1816
1817
1818 typedef void (wxEvtHandler::*wxGridEventFunction)(wxGridEvent&);
1819 typedef void (wxEvtHandler::*wxGridSizeEventFunction)(wxGridSizeEvent&);
1820 typedef void (wxEvtHandler::*wxGridRangeSelectEventFunction)(wxGridRangeSelectEvent&);
1821
1822 #define EVT_GRID_CELL_LEFT_CLICK(fn) { wxEVT_GRID_CELL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1823 #define EVT_GRID_CELL_RIGHT_CLICK(fn) { wxEVT_GRID_CELL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1824 #define EVT_GRID_CELL_LEFT_DCLICK(fn) { wxEVT_GRID_CELL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1825 #define EVT_GRID_CELL_RIGHT_DCLICK(fn) { wxEVT_GRID_CELL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1826 #define EVT_GRID_LABEL_LEFT_CLICK(fn) { wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1827 #define EVT_GRID_LABEL_RIGHT_CLICK(fn) { wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1828 #define EVT_GRID_LABEL_LEFT_DCLICK(fn) { wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1829 #define EVT_GRID_LABEL_RIGHT_DCLICK(fn) { wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1830 #define EVT_GRID_ROW_SIZE(fn) { wxEVT_GRID_ROW_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1831 #define EVT_GRID_COL_SIZE(fn) { wxEVT_GRID_COL_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1832 #define EVT_GRID_RANGE_SELECT(fn) { wxEVT_GRID_RANGE_SELECT, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridRangeSelectEventFunction) &fn, NULL },
1833 #define EVT_GRID_CELL_CHANGE(fn) { wxEVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1834 #define EVT_GRID_SELECT_CELL(fn) { wxEVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1835 #define EVT_GRID_EDITOR_SHOWN(fn) { wxEVT_GRID_EDITOR_SHOWN, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1836 #define EVT_GRID_EDITOR_HIDDEN(fn) { wxEVT_GRID_EDITOR_HIDDEN, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1837
1838
1839 #if 0 // TODO: implement these ? others ?
1840
1841 const wxEventType wxEVT_GRID_CREATE_CELL = wxEVT_FIRST + 1576;
1842 const wxEventType wxEVT_GRID_CHANGE_LABELS = wxEVT_FIRST + 1577;
1843 const wxEventType wxEVT_GRID_CHANGE_SEL_LABEL = wxEVT_FIRST + 1578;
1844
1845 #define EVT_GRID_CREATE_CELL(fn) { wxEVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1846 #define EVT_GRID_CHANGE_LABELS(fn) { wxEVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1847 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { wxEVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1848
1849 #endif
1850
1851 #endif // #ifndef __WXGRID_H__
1852
1853 #endif // ifndef wxUSE_NEW_GRID
1854