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