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