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