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