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