]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/grid.h
move the grid-specific workaround for scrollbar hysteresis to wxScrollHelper itself...
[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: Santiago Palacios
6 // Created: 1/08/1999
7 // RCS-ID: $Id$
8 // Copyright: (c) Michael Bedward
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_GENERIC_GRID_H_
13 #define _WX_GENERIC_GRID_H_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_GRID
18
19 #include "wx/scrolwin.h"
20
21 // ----------------------------------------------------------------------------
22 // constants
23 // ----------------------------------------------------------------------------
24
25 extern WXDLLIMPEXP_DATA_ADV(const wxChar) wxGridNameStr[];
26
27 // Default parameters for wxGrid
28 //
29 #define WXGRID_DEFAULT_NUMBER_ROWS 10
30 #define WXGRID_DEFAULT_NUMBER_COLS 10
31 #if defined(__WXMSW__) || defined(__WXGTK20__)
32 #define WXGRID_DEFAULT_ROW_HEIGHT 25
33 #else
34 #define WXGRID_DEFAULT_ROW_HEIGHT 30
35 #endif // __WXMSW__
36 #define WXGRID_DEFAULT_COL_WIDTH 80
37 #define WXGRID_DEFAULT_COL_LABEL_HEIGHT 32
38 #define WXGRID_DEFAULT_ROW_LABEL_WIDTH 82
39 #define WXGRID_LABEL_EDGE_ZONE 2
40 #define WXGRID_MIN_ROW_HEIGHT 15
41 #define WXGRID_MIN_COL_WIDTH 15
42 #define WXGRID_DEFAULT_SCROLLBAR_WIDTH 16
43
44 // type names for grid table values
45 #define wxGRID_VALUE_STRING _T("string")
46 #define wxGRID_VALUE_BOOL _T("bool")
47 #define wxGRID_VALUE_NUMBER _T("long")
48 #define wxGRID_VALUE_FLOAT _T("double")
49 #define wxGRID_VALUE_CHOICE _T("choice")
50
51 #define wxGRID_VALUE_TEXT wxGRID_VALUE_STRING
52 #define wxGRID_VALUE_LONG wxGRID_VALUE_NUMBER
53
54 // magic constant which tells (to some functions) to automatically calculate
55 // the appropriate size
56 #define wxGRID_AUTOSIZE (-1)
57
58 // many wxGrid methods work either with columns or rows, this enum is used for
59 // the parameter indicating which one should it be
60 enum wxGridDirection
61 {
62 wxGRID_COLUMN,
63 wxGRID_ROW
64 };
65
66 // ----------------------------------------------------------------------------
67 // forward declarations
68 // ----------------------------------------------------------------------------
69
70 class WXDLLIMPEXP_FWD_ADV wxGrid;
71 class WXDLLIMPEXP_FWD_ADV wxGridCellAttr;
72 class WXDLLIMPEXP_FWD_ADV wxGridCellAttrProviderData;
73 class WXDLLIMPEXP_FWD_ADV wxGridColLabelWindow;
74 class WXDLLIMPEXP_FWD_ADV wxGridCornerLabelWindow;
75 class WXDLLIMPEXP_FWD_ADV wxGridRowLabelWindow;
76 class WXDLLIMPEXP_FWD_ADV wxGridWindow;
77 class WXDLLIMPEXP_FWD_ADV wxGridTypeRegistry;
78 class WXDLLIMPEXP_FWD_ADV wxGridSelection;
79
80 class WXDLLIMPEXP_FWD_CORE wxCheckBox;
81 class WXDLLIMPEXP_FWD_CORE wxComboBox;
82 class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
83 #if wxUSE_SPINCTRL
84 class WXDLLIMPEXP_FWD_CORE wxSpinCtrl;
85 #endif
86
87 // ----------------------------------------------------------------------------
88 // macros
89 // ----------------------------------------------------------------------------
90
91 #define wxSafeIncRef(p) if ( p ) (p)->IncRef()
92 #define wxSafeDecRef(p) if ( p ) (p)->DecRef()
93
94 // ----------------------------------------------------------------------------
95 // wxGridCellWorker: common base class for wxGridCellRenderer and
96 // wxGridCellEditor
97 //
98 // NB: this is more an implementation convenience than a design issue, so this
99 // class is not documented and is not public at all
100 // ----------------------------------------------------------------------------
101
102 class WXDLLIMPEXP_ADV wxGridCellWorker : public wxClientDataContainer
103 {
104 public:
105 wxGridCellWorker() { m_nRef = 1; }
106
107 // this class is ref counted: it is created with ref count of 1, so
108 // calling DecRef() once will delete it. Calling IncRef() allows to lock
109 // it until the matching DecRef() is called
110 void IncRef() { m_nRef++; }
111 void DecRef() { if ( --m_nRef == 0 ) delete this; }
112
113 // interpret renderer parameters: arbitrary string whose interpretatin is
114 // left to the derived classes
115 virtual void SetParameters(const wxString& params);
116
117 protected:
118 // virtual dtor for any base class - private because only DecRef() can
119 // delete us
120 virtual ~wxGridCellWorker();
121
122 private:
123 size_t m_nRef;
124
125 // suppress the stupid gcc warning about the class having private dtor and
126 // no friends
127 friend class wxGridCellWorkerDummyFriend;
128 };
129
130 // ----------------------------------------------------------------------------
131 // wxGridCellRenderer: this class is responsible for actually drawing the cell
132 // in the grid. You may pass it to the wxGridCellAttr (below) to change the
133 // format of one given cell or to wxGrid::SetDefaultRenderer() to change the
134 // view of all cells. This is an ABC, you will normally use one of the
135 // predefined derived classes or derive your own class from it.
136 // ----------------------------------------------------------------------------
137
138 class WXDLLIMPEXP_ADV wxGridCellRenderer : public wxGridCellWorker
139 {
140 public:
141 // draw the given cell on the provided DC inside the given rectangle
142 // using the style specified by the attribute and the default or selected
143 // state corresponding to the isSelected value.
144 //
145 // this pure virtual function has a default implementation which will
146 // prepare the DC using the given attribute: it will draw the rectangle
147 // with the bg colour from attr and set the text colour and font
148 virtual void Draw(wxGrid& grid,
149 wxGridCellAttr& attr,
150 wxDC& dc,
151 const wxRect& rect,
152 int row, int col,
153 bool isSelected) = 0;
154
155 // get the preferred size of the cell for its contents
156 virtual wxSize GetBestSize(wxGrid& grid,
157 wxGridCellAttr& attr,
158 wxDC& dc,
159 int row, int col) = 0;
160
161 // create a new object which is the copy of this one
162 virtual wxGridCellRenderer *Clone() const = 0;
163 };
164
165 // the default renderer for the cells containing string data
166 class WXDLLIMPEXP_ADV wxGridCellStringRenderer : public wxGridCellRenderer
167 {
168 public:
169 // draw the string
170 virtual void Draw(wxGrid& grid,
171 wxGridCellAttr& attr,
172 wxDC& dc,
173 const wxRect& rect,
174 int row, int col,
175 bool isSelected);
176
177 // return the string extent
178 virtual wxSize GetBestSize(wxGrid& grid,
179 wxGridCellAttr& attr,
180 wxDC& dc,
181 int row, int col);
182
183 virtual wxGridCellRenderer *Clone() const
184 { return new wxGridCellStringRenderer; }
185
186 protected:
187 // set the text colours before drawing
188 void SetTextColoursAndFont(const wxGrid& grid,
189 const wxGridCellAttr& attr,
190 wxDC& dc,
191 bool isSelected);
192
193 // calc the string extent for given string/font
194 wxSize DoGetBestSize(const wxGridCellAttr& attr,
195 wxDC& dc,
196 const wxString& text);
197 };
198
199 // the default renderer for the cells containing numeric (long) data
200 class WXDLLIMPEXP_ADV wxGridCellNumberRenderer : public wxGridCellStringRenderer
201 {
202 public:
203 // draw the string right aligned
204 virtual void Draw(wxGrid& grid,
205 wxGridCellAttr& attr,
206 wxDC& dc,
207 const wxRect& rect,
208 int row, int col,
209 bool isSelected);
210
211 virtual wxSize GetBestSize(wxGrid& grid,
212 wxGridCellAttr& attr,
213 wxDC& dc,
214 int row, int col);
215
216 virtual wxGridCellRenderer *Clone() const
217 { return new wxGridCellNumberRenderer; }
218
219 protected:
220 wxString GetString(const wxGrid& grid, int row, int col);
221 };
222
223 class WXDLLIMPEXP_ADV wxGridCellFloatRenderer : public wxGridCellStringRenderer
224 {
225 public:
226 wxGridCellFloatRenderer(int width = -1, int precision = -1);
227
228 // get/change formatting parameters
229 int GetWidth() const { return m_width; }
230 void SetWidth(int width) { m_width = width; m_format.clear(); }
231 int GetPrecision() const { return m_precision; }
232 void SetPrecision(int precision) { m_precision = precision; m_format.clear(); }
233
234 // draw the string right aligned with given width/precision
235 virtual void Draw(wxGrid& grid,
236 wxGridCellAttr& attr,
237 wxDC& dc,
238 const wxRect& rect,
239 int row, int col,
240 bool isSelected);
241
242 virtual wxSize GetBestSize(wxGrid& grid,
243 wxGridCellAttr& attr,
244 wxDC& dc,
245 int row, int col);
246
247 // parameters string format is "width[,precision]"
248 virtual void SetParameters(const wxString& params);
249
250 virtual wxGridCellRenderer *Clone() const;
251
252 protected:
253 wxString GetString(const wxGrid& grid, int row, int col);
254
255 private:
256 // formatting parameters
257 int m_width,
258 m_precision;
259
260 wxString m_format;
261 };
262
263 // renderer for boolean fields
264 class WXDLLIMPEXP_ADV wxGridCellBoolRenderer : public wxGridCellRenderer
265 {
266 public:
267 // draw a check mark or nothing
268 virtual void Draw(wxGrid& grid,
269 wxGridCellAttr& attr,
270 wxDC& dc,
271 const wxRect& rect,
272 int row, int col,
273 bool isSelected);
274
275 // return the checkmark size
276 virtual wxSize GetBestSize(wxGrid& grid,
277 wxGridCellAttr& attr,
278 wxDC& dc,
279 int row, int col);
280
281 virtual wxGridCellRenderer *Clone() const
282 { return new wxGridCellBoolRenderer; }
283
284 private:
285 static wxSize ms_sizeCheckMark;
286 };
287
288 // ----------------------------------------------------------------------------
289 // wxGridCellEditor: This class is responsible for providing and manipulating
290 // the in-place edit controls for the grid. Instances of wxGridCellEditor
291 // (actually, instances of derived classes since it is an ABC) can be
292 // associated with the cell attributes for individual cells, rows, columns, or
293 // even for the entire grid.
294 // ----------------------------------------------------------------------------
295
296 class WXDLLIMPEXP_ADV wxGridCellEditor : public wxGridCellWorker
297 {
298 public:
299 wxGridCellEditor();
300
301 bool IsCreated() { return m_control != NULL; }
302 wxControl* GetControl() { return m_control; }
303 void SetControl(wxControl* control) { m_control = control; }
304
305 wxGridCellAttr* GetCellAttr() { return m_attr; }
306 void SetCellAttr(wxGridCellAttr* attr) { m_attr = attr; }
307
308 // Creates the actual edit control
309 virtual void Create(wxWindow* parent,
310 wxWindowID id,
311 wxEvtHandler* evtHandler) = 0;
312
313 // Size and position the edit control
314 virtual void SetSize(const wxRect& rect);
315
316 // Show or hide the edit control, use the specified attributes to set
317 // colours/fonts for it
318 virtual void Show(bool show, wxGridCellAttr *attr = (wxGridCellAttr *)NULL);
319
320 // Draws the part of the cell not occupied by the control: the base class
321 // version just fills it with background colour from the attribute
322 virtual void PaintBackground(const wxRect& rectCell, wxGridCellAttr *attr);
323
324 // Fetch the value from the table and prepare the edit control
325 // to begin editing. Set the focus to the edit control.
326 virtual void BeginEdit(int row, int col, wxGrid* grid) = 0;
327
328 // Complete the editing of the current cell. Returns true if the value has
329 // changed. If necessary, the control may be destroyed.
330 virtual bool EndEdit(int row, int col, wxGrid* grid) = 0;
331
332 // Reset the value in the control back to its starting value
333 virtual void Reset() = 0;
334
335 // return true to allow the given key to start editing: the base class
336 // version only checks that the event has no modifiers. The derived
337 // classes are supposed to do "if ( base::IsAcceptedKey() && ... )" in
338 // their IsAcceptedKey() implementation, although, of course, it is not a
339 // mandatory requirment.
340 //
341 // NB: if the key is F2 (special), editing will always start and this
342 // method will not be called at all (but StartingKey() will)
343 virtual bool IsAcceptedKey(wxKeyEvent& event);
344
345 // If the editor is enabled by pressing keys on the grid, this will be
346 // called to let the editor do something about that first key if desired
347 virtual void StartingKey(wxKeyEvent& event);
348
349 // if the editor is enabled by clicking on the cell, this method will be
350 // called
351 virtual void StartingClick();
352
353 // Some types of controls on some platforms may need some help
354 // with the Return key.
355 virtual void HandleReturn(wxKeyEvent& event);
356
357 // Final cleanup
358 virtual void Destroy();
359
360 // create a new object which is the copy of this one
361 virtual wxGridCellEditor *Clone() const = 0;
362
363 // added GetValue so we can get the value which is in the control
364 virtual wxString GetValue() const = 0;
365
366 protected:
367 // the dtor is private because only DecRef() can delete us
368 virtual ~wxGridCellEditor();
369
370 // the control we show on screen
371 wxControl* m_control;
372
373 // a temporary pointer to the attribute being edited
374 wxGridCellAttr* m_attr;
375
376 // if we change the colours/font of the control from the default ones, we
377 // must restore the default later and we save them here between calls to
378 // Show(true) and Show(false)
379 wxColour m_colFgOld,
380 m_colBgOld;
381 wxFont m_fontOld;
382
383 // suppress the stupid gcc warning about the class having private dtor and
384 // no friends
385 friend class wxGridCellEditorDummyFriend;
386
387 DECLARE_NO_COPY_CLASS(wxGridCellEditor)
388 };
389
390 #if wxUSE_TEXTCTRL
391
392 // the editor for string/text data
393 class WXDLLIMPEXP_ADV wxGridCellTextEditor : public wxGridCellEditor
394 {
395 public:
396 wxGridCellTextEditor();
397
398 virtual void Create(wxWindow* parent,
399 wxWindowID id,
400 wxEvtHandler* evtHandler);
401 virtual void SetSize(const wxRect& rect);
402
403 virtual void PaintBackground(const wxRect& rectCell, wxGridCellAttr *attr);
404
405 virtual bool IsAcceptedKey(wxKeyEvent& event);
406 virtual void BeginEdit(int row, int col, wxGrid* grid);
407 virtual bool EndEdit(int row, int col, wxGrid* grid);
408
409 virtual void Reset();
410 virtual void StartingKey(wxKeyEvent& event);
411 virtual void HandleReturn(wxKeyEvent& event);
412
413 // parameters string format is "max_width"
414 virtual void SetParameters(const wxString& params);
415
416 virtual wxGridCellEditor *Clone() const
417 { return new wxGridCellTextEditor; }
418
419 // added GetValue so we can get the value which is in the control
420 virtual wxString GetValue() const;
421
422 protected:
423 wxTextCtrl *Text() const { return (wxTextCtrl *)m_control; }
424
425 // parts of our virtual functions reused by the derived classes
426 void DoCreate(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler,
427 long style = 0);
428 void DoBeginEdit(const wxString& startValue);
429 void DoReset(const wxString& startValue);
430
431 private:
432 size_t m_maxChars; // max number of chars allowed
433 wxString m_startValue;
434
435 DECLARE_NO_COPY_CLASS(wxGridCellTextEditor)
436 };
437
438 // the editor for numeric (long) data
439 class WXDLLIMPEXP_ADV wxGridCellNumberEditor : public wxGridCellTextEditor
440 {
441 public:
442 // allows to specify the range - if min == max == -1, no range checking is
443 // done
444 wxGridCellNumberEditor(int min = -1, int max = -1);
445
446 virtual void Create(wxWindow* parent,
447 wxWindowID id,
448 wxEvtHandler* evtHandler);
449
450 virtual bool IsAcceptedKey(wxKeyEvent& event);
451 virtual void BeginEdit(int row, int col, wxGrid* grid);
452 virtual bool EndEdit(int row, int col, wxGrid* grid);
453
454 virtual void Reset();
455 virtual void StartingKey(wxKeyEvent& event);
456
457 // parameters string format is "min,max"
458 virtual void SetParameters(const wxString& params);
459
460 virtual wxGridCellEditor *Clone() const
461 { return new wxGridCellNumberEditor(m_min, m_max); }
462
463 // added GetValue so we can get the value which is in the control
464 virtual wxString GetValue() const;
465
466 protected:
467 #if wxUSE_SPINCTRL
468 wxSpinCtrl *Spin() const { return (wxSpinCtrl *)m_control; }
469 #endif
470
471 // if HasRange(), we use wxSpinCtrl - otherwise wxTextCtrl
472 bool HasRange() const
473 {
474 #if wxUSE_SPINCTRL
475 return m_min != m_max;
476 #else
477 return false;
478 #endif
479 }
480
481 // string representation of m_valueOld
482 wxString GetString() const
483 { return wxString::Format(_T("%ld"), m_valueOld); }
484
485 private:
486 int m_min,
487 m_max;
488
489 long m_valueOld;
490
491 DECLARE_NO_COPY_CLASS(wxGridCellNumberEditor)
492 };
493
494 // the editor for floating point numbers (double) data
495 class WXDLLIMPEXP_ADV wxGridCellFloatEditor : public wxGridCellTextEditor
496 {
497 public:
498 wxGridCellFloatEditor(int width = -1, int precision = -1);
499
500 virtual void Create(wxWindow* parent,
501 wxWindowID id,
502 wxEvtHandler* evtHandler);
503
504 virtual bool IsAcceptedKey(wxKeyEvent& event);
505 virtual void BeginEdit(int row, int col, wxGrid* grid);
506 virtual bool EndEdit(int row, int col, wxGrid* grid);
507
508 virtual void Reset();
509 virtual void StartingKey(wxKeyEvent& event);
510
511 virtual wxGridCellEditor *Clone() const
512 { return new wxGridCellFloatEditor(m_width, m_precision); }
513
514 // parameters string format is "width,precision"
515 virtual void SetParameters(const wxString& params);
516
517 protected:
518 // string representation of m_valueOld
519 wxString GetString() const;
520
521 private:
522 int m_width,
523 m_precision;
524 double m_valueOld;
525
526 DECLARE_NO_COPY_CLASS(wxGridCellFloatEditor)
527 };
528
529 #endif // wxUSE_TEXTCTRL
530
531 #if wxUSE_CHECKBOX
532
533 // the editor for boolean data
534 class WXDLLIMPEXP_ADV wxGridCellBoolEditor : public wxGridCellEditor
535 {
536 public:
537 wxGridCellBoolEditor() { }
538
539 virtual void Create(wxWindow* parent,
540 wxWindowID id,
541 wxEvtHandler* evtHandler);
542
543 virtual void SetSize(const wxRect& rect);
544 virtual void Show(bool show, wxGridCellAttr *attr = NULL);
545
546 virtual bool IsAcceptedKey(wxKeyEvent& event);
547 virtual void BeginEdit(int row, int col, wxGrid* grid);
548 virtual bool EndEdit(int row, int col, wxGrid* grid);
549
550 virtual void Reset();
551 virtual void StartingClick();
552 virtual void StartingKey(wxKeyEvent& event);
553
554 virtual wxGridCellEditor *Clone() const
555 { return new wxGridCellBoolEditor; }
556
557 // added GetValue so we can get the value which is in the control, see
558 // also UseStringValues()
559 virtual wxString GetValue() const;
560
561 // set the string values returned by GetValue() for the true and false
562 // states, respectively
563 static void UseStringValues(const wxString& valueTrue = _T("1"),
564 const wxString& valueFalse = wxEmptyString);
565
566 // return true if the given string is equal to the string representation of
567 // true value which we currently use
568 static bool IsTrueValue(const wxString& value);
569
570 protected:
571 wxCheckBox *CBox() const { return (wxCheckBox *)m_control; }
572
573 private:
574 bool m_startValue;
575
576 static wxString ms_stringValues[2];
577
578 DECLARE_NO_COPY_CLASS(wxGridCellBoolEditor)
579 };
580
581 #endif // wxUSE_CHECKBOX
582
583 #if wxUSE_COMBOBOX
584
585 // the editor for string data allowing to choose from the list of strings
586 class WXDLLIMPEXP_ADV wxGridCellChoiceEditor : public wxGridCellEditor
587 {
588 public:
589 // if !allowOthers, user can't type a string not in choices array
590 wxGridCellChoiceEditor(size_t count = 0,
591 const wxString choices[] = NULL,
592 bool allowOthers = false);
593 wxGridCellChoiceEditor(const wxArrayString& choices,
594 bool allowOthers = false);
595
596 virtual void Create(wxWindow* parent,
597 wxWindowID id,
598 wxEvtHandler* evtHandler);
599
600 virtual void PaintBackground(const wxRect& rectCell, wxGridCellAttr *attr);
601
602 virtual void BeginEdit(int row, int col, wxGrid* grid);
603 virtual bool EndEdit(int row, int col, wxGrid* grid);
604
605 virtual void Reset();
606
607 // parameters string format is "item1[,item2[...,itemN]]"
608 virtual void SetParameters(const wxString& params);
609
610 virtual wxGridCellEditor *Clone() const;
611
612 // added GetValue so we can get the value which is in the control
613 virtual wxString GetValue() const;
614
615 protected:
616 wxComboBox *Combo() const { return (wxComboBox *)m_control; }
617
618 // DJC - (MAPTEK) you at least need access to m_choices if you
619 // wish to override this class
620 protected:
621 wxString m_startValue;
622 wxArrayString m_choices;
623 bool m_allowOthers;
624
625 DECLARE_NO_COPY_CLASS(wxGridCellChoiceEditor)
626 };
627
628 #endif // wxUSE_COMBOBOX
629
630 // ----------------------------------------------------------------------------
631 // wxGridCellAttr: this class can be used to alter the cells appearance in
632 // the grid by changing their colour/font/... from default. An object of this
633 // class may be returned by wxGridTable::GetAttr().
634 // ----------------------------------------------------------------------------
635
636 class WXDLLIMPEXP_ADV wxGridCellAttr : public wxClientDataContainer
637 {
638 public:
639 enum wxAttrKind
640 {
641 Any,
642 Default,
643 Cell,
644 Row,
645 Col,
646 Merged
647 };
648
649 // ctors
650 wxGridCellAttr(wxGridCellAttr *attrDefault = NULL)
651 {
652 Init(attrDefault);
653
654 // MB: args used to be 0,0 here but wxALIGN_LEFT is 0
655 SetAlignment(-1, -1);
656 }
657
658 // VZ: considering the number of members wxGridCellAttr has now, this ctor
659 // seems to be pretty useless... may be we should just remove it?
660 wxGridCellAttr(const wxColour& colText,
661 const wxColour& colBack,
662 const wxFont& font,
663 int hAlign,
664 int vAlign)
665 : m_colText(colText), m_colBack(colBack), m_font(font)
666 {
667 Init();
668 SetAlignment(hAlign, vAlign);
669 }
670
671 // creates a new copy of this object
672 wxGridCellAttr *Clone() const;
673 void MergeWith(wxGridCellAttr *mergefrom);
674
675 // this class is ref counted: it is created with ref count of 1, so
676 // calling DecRef() once will delete it. Calling IncRef() allows to lock
677 // it until the matching DecRef() is called
678 void IncRef() { m_nRef++; }
679 void DecRef() { if ( --m_nRef == 0 ) delete this; }
680
681 // setters
682 void SetTextColour(const wxColour& colText) { m_colText = colText; }
683 void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
684 void SetFont(const wxFont& font) { m_font = font; }
685 void SetAlignment(int hAlign, int vAlign)
686 {
687 m_hAlign = hAlign;
688 m_vAlign = vAlign;
689 }
690 void SetSize(int num_rows, int num_cols);
691 void SetOverflow(bool allow = true)
692 { m_overflow = allow ? Overflow : SingleCell; }
693 void SetReadOnly(bool isReadOnly = true)
694 { m_isReadOnly = isReadOnly ? ReadOnly : ReadWrite; }
695
696 // takes ownership of the pointer
697 void SetRenderer(wxGridCellRenderer *renderer)
698 { wxSafeDecRef(m_renderer); m_renderer = renderer; }
699 void SetEditor(wxGridCellEditor* editor)
700 { wxSafeDecRef(m_editor); m_editor = editor; }
701
702 void SetKind(wxAttrKind kind) { m_attrkind = kind; }
703
704 // accessors
705 bool HasTextColour() const { return m_colText.Ok(); }
706 bool HasBackgroundColour() const { return m_colBack.Ok(); }
707 bool HasFont() const { return m_font.Ok(); }
708 bool HasAlignment() const { return (m_hAlign != -1 || m_vAlign != -1); }
709 bool HasRenderer() const { return m_renderer != NULL; }
710 bool HasEditor() const { return m_editor != NULL; }
711 bool HasReadWriteMode() const { return m_isReadOnly != Unset; }
712 bool HasOverflowMode() const { return m_overflow != UnsetOverflow; }
713 bool HasSize() const { return m_sizeRows != 1 || m_sizeCols != 1; }
714
715 const wxColour& GetTextColour() const;
716 const wxColour& GetBackgroundColour() const;
717 const wxFont& GetFont() const;
718 void GetAlignment(int *hAlign, int *vAlign) const;
719 void GetSize(int *num_rows, int *num_cols) const;
720 bool GetOverflow() const
721 { return m_overflow != SingleCell; }
722 wxGridCellRenderer *GetRenderer(const wxGrid* grid, int row, int col) const;
723 wxGridCellEditor *GetEditor(const wxGrid* grid, int row, int col) const;
724
725 bool IsReadOnly() const { return m_isReadOnly == wxGridCellAttr::ReadOnly; }
726
727 wxAttrKind GetKind() { return m_attrkind; }
728
729 void SetDefAttr(wxGridCellAttr* defAttr) { m_defGridAttr = defAttr; }
730
731 protected:
732 // the dtor is private because only DecRef() can delete us
733 virtual ~wxGridCellAttr()
734 {
735 wxSafeDecRef(m_renderer);
736 wxSafeDecRef(m_editor);
737 }
738
739 private:
740 enum wxAttrReadMode
741 {
742 Unset = -1,
743 ReadWrite,
744 ReadOnly
745 };
746
747 enum wxAttrOverflowMode
748 {
749 UnsetOverflow = -1,
750 Overflow,
751 SingleCell
752 };
753
754 // the common part of all ctors
755 void Init(wxGridCellAttr *attrDefault = NULL);
756
757
758 // the ref count - when it goes to 0, we die
759 size_t m_nRef;
760
761 wxColour m_colText,
762 m_colBack;
763 wxFont m_font;
764 int m_hAlign,
765 m_vAlign;
766 int m_sizeRows,
767 m_sizeCols;
768
769 wxAttrOverflowMode m_overflow;
770
771 wxGridCellRenderer* m_renderer;
772 wxGridCellEditor* m_editor;
773 wxGridCellAttr* m_defGridAttr;
774
775 wxAttrReadMode m_isReadOnly;
776
777 wxAttrKind m_attrkind;
778
779 // use Clone() instead
780 DECLARE_NO_COPY_CLASS(wxGridCellAttr)
781
782 // suppress the stupid gcc warning about the class having private dtor and
783 // no friends
784 friend class wxGridCellAttrDummyFriend;
785 };
786
787 // ----------------------------------------------------------------------------
788 // wxGridCellAttrProvider: class used by wxGridTableBase to retrieve/store the
789 // cell attributes.
790 // ----------------------------------------------------------------------------
791
792 // implementation note: we separate it from wxGridTableBase because we wish to
793 // avoid deriving a new table class if possible, and sometimes it will be
794 // enough to just derive another wxGridCellAttrProvider instead
795 //
796 // the default implementation is reasonably efficient for the generic case,
797 // but you might still wish to implement your own for some specific situations
798 // if you have performance problems with the stock one
799 class WXDLLIMPEXP_ADV wxGridCellAttrProvider : public wxClientDataContainer
800 {
801 public:
802 wxGridCellAttrProvider();
803 virtual ~wxGridCellAttrProvider();
804
805 // DecRef() must be called on the returned pointer
806 virtual wxGridCellAttr *GetAttr(int row, int col,
807 wxGridCellAttr::wxAttrKind kind ) const;
808
809 // all these functions take ownership of the pointer, don't call DecRef()
810 // on it
811 virtual void SetAttr(wxGridCellAttr *attr, int row, int col);
812 virtual void SetRowAttr(wxGridCellAttr *attr, int row);
813 virtual void SetColAttr(wxGridCellAttr *attr, int col);
814
815 // these functions must be called whenever some rows/cols are deleted
816 // because the internal data must be updated then
817 void UpdateAttrRows( size_t pos, int numRows );
818 void UpdateAttrCols( size_t pos, int numCols );
819
820 private:
821 void InitData();
822
823 wxGridCellAttrProviderData *m_data;
824
825 DECLARE_NO_COPY_CLASS(wxGridCellAttrProvider)
826 };
827
828 //////////////////////////////////////////////////////////////////////
829 //
830 // Grid table classes
831 //
832 //////////////////////////////////////////////////////////////////////
833
834
835 class WXDLLIMPEXP_ADV wxGridTableBase : public wxObject, public wxClientDataContainer
836 {
837 public:
838 wxGridTableBase();
839 virtual ~wxGridTableBase();
840
841 // You must override these functions in a derived table class
842 //
843 virtual int GetNumberRows() = 0;
844 virtual int GetNumberCols() = 0;
845 virtual bool IsEmptyCell( int row, int col ) = 0;
846 virtual wxString GetValue( int row, int col ) = 0;
847 virtual void SetValue( int row, int col, const wxString& value ) = 0;
848
849 // Data type determination and value access
850 virtual wxString GetTypeName( int row, int col );
851 virtual bool CanGetValueAs( int row, int col, const wxString& typeName );
852 virtual bool CanSetValueAs( int row, int col, const wxString& typeName );
853
854 virtual long GetValueAsLong( int row, int col );
855 virtual double GetValueAsDouble( int row, int col );
856 virtual bool GetValueAsBool( int row, int col );
857
858 virtual void SetValueAsLong( int row, int col, long value );
859 virtual void SetValueAsDouble( int row, int col, double value );
860 virtual void SetValueAsBool( int row, int col, bool value );
861
862 // For user defined types
863 virtual void* GetValueAsCustom( int row, int col, const wxString& typeName );
864 virtual void SetValueAsCustom( int row, int col, const wxString& typeName, void* value );
865
866
867 // Overriding these is optional
868 //
869 virtual void SetView( wxGrid *grid ) { m_view = grid; }
870 virtual wxGrid * GetView() const { return m_view; }
871
872 virtual void Clear() {}
873 virtual bool InsertRows( size_t pos = 0, size_t numRows = 1 );
874 virtual bool AppendRows( size_t numRows = 1 );
875 virtual bool DeleteRows( size_t pos = 0, size_t numRows = 1 );
876 virtual bool InsertCols( size_t pos = 0, size_t numCols = 1 );
877 virtual bool AppendCols( size_t numCols = 1 );
878 virtual bool DeleteCols( size_t pos = 0, size_t numCols = 1 );
879
880 virtual wxString GetRowLabelValue( int row );
881 virtual wxString GetColLabelValue( int col );
882 virtual void SetRowLabelValue( int WXUNUSED(row), const wxString& ) {}
883 virtual void SetColLabelValue( int WXUNUSED(col), const wxString& ) {}
884
885 // Attribute handling
886 //
887
888 // give us the attr provider to use - we take ownership of the pointer
889 void SetAttrProvider(wxGridCellAttrProvider *attrProvider);
890
891 // get the currently used attr provider (may be NULL)
892 wxGridCellAttrProvider *GetAttrProvider() const { return m_attrProvider; }
893
894 // Does this table allow attributes? Default implementation creates
895 // a wxGridCellAttrProvider if necessary.
896 virtual bool CanHaveAttributes();
897
898 // by default forwarded to wxGridCellAttrProvider if any. May be
899 // overridden to handle attributes directly in the table.
900 virtual wxGridCellAttr *GetAttr( int row, int col,
901 wxGridCellAttr::wxAttrKind kind );
902
903
904 // these functions take ownership of the pointer
905 virtual void SetAttr(wxGridCellAttr* attr, int row, int col);
906 virtual void SetRowAttr(wxGridCellAttr *attr, int row);
907 virtual void SetColAttr(wxGridCellAttr *attr, int col);
908
909 private:
910 wxGrid * m_view;
911 wxGridCellAttrProvider *m_attrProvider;
912
913 DECLARE_ABSTRACT_CLASS(wxGridTableBase)
914 DECLARE_NO_COPY_CLASS(wxGridTableBase)
915 };
916
917
918 // ----------------------------------------------------------------------------
919 // wxGridTableMessage
920 // ----------------------------------------------------------------------------
921
922 // IDs for messages sent from grid table to view
923 //
924 enum wxGridTableRequest
925 {
926 wxGRIDTABLE_REQUEST_VIEW_GET_VALUES = 2000,
927 wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES,
928 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
929 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
930 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
931 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
932 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
933 wxGRIDTABLE_NOTIFY_COLS_DELETED
934 };
935
936 class WXDLLIMPEXP_ADV wxGridTableMessage
937 {
938 public:
939 wxGridTableMessage();
940 wxGridTableMessage( wxGridTableBase *table, int id,
941 int comInt1 = -1,
942 int comInt2 = -1 );
943
944 void SetTableObject( wxGridTableBase *table ) { m_table = table; }
945 wxGridTableBase * GetTableObject() const { return m_table; }
946 void SetId( int id ) { m_id = id; }
947 int GetId() { return m_id; }
948 void SetCommandInt( int comInt1 ) { m_comInt1 = comInt1; }
949 int GetCommandInt() { return m_comInt1; }
950 void SetCommandInt2( int comInt2 ) { m_comInt2 = comInt2; }
951 int GetCommandInt2() { return m_comInt2; }
952
953 private:
954 wxGridTableBase *m_table;
955 int m_id;
956 int m_comInt1;
957 int m_comInt2;
958
959 DECLARE_NO_COPY_CLASS(wxGridTableMessage)
960 };
961
962
963
964 // ------ wxGridStringArray
965 // A 2-dimensional array of strings for data values
966 //
967
968 WX_DECLARE_OBJARRAY_WITH_DECL(wxArrayString, wxGridStringArray,
969 class WXDLLIMPEXP_ADV);
970
971
972
973 // ------ wxGridStringTable
974 //
975 // Simplest type of data table for a grid for small tables of strings
976 // that are stored in memory
977 //
978
979 class WXDLLIMPEXP_ADV wxGridStringTable : public wxGridTableBase
980 {
981 public:
982 wxGridStringTable();
983 wxGridStringTable( int numRows, int numCols );
984 virtual ~wxGridStringTable();
985
986 // these are pure virtual in wxGridTableBase
987 //
988 int GetNumberRows();
989 int GetNumberCols();
990 wxString GetValue( int row, int col );
991 void SetValue( int row, int col, const wxString& s );
992 bool IsEmptyCell( int row, int col );
993
994 // overridden functions from wxGridTableBase
995 //
996 void Clear();
997 bool InsertRows( size_t pos = 0, size_t numRows = 1 );
998 bool AppendRows( size_t numRows = 1 );
999 bool DeleteRows( size_t pos = 0, size_t numRows = 1 );
1000 bool InsertCols( size_t pos = 0, size_t numCols = 1 );
1001 bool AppendCols( size_t numCols = 1 );
1002 bool DeleteCols( size_t pos = 0, size_t numCols = 1 );
1003
1004 void SetRowLabelValue( int row, const wxString& );
1005 void SetColLabelValue( int col, const wxString& );
1006 wxString GetRowLabelValue( int row );
1007 wxString GetColLabelValue( int col );
1008
1009 private:
1010 wxGridStringArray m_data;
1011
1012 // These only get used if you set your own labels, otherwise the
1013 // GetRow/ColLabelValue functions return wxGridTableBase defaults
1014 //
1015 wxArrayString m_rowLabels;
1016 wxArrayString m_colLabels;
1017
1018 DECLARE_DYNAMIC_CLASS_NO_COPY( wxGridStringTable )
1019 };
1020
1021
1022
1023 // ============================================================================
1024 // Grid view classes
1025 // ============================================================================
1026
1027 // ----------------------------------------------------------------------------
1028 // wxGridCellCoords: location of a cell in the grid
1029 // ----------------------------------------------------------------------------
1030
1031 class WXDLLIMPEXP_ADV wxGridCellCoords
1032 {
1033 public:
1034 wxGridCellCoords() { m_row = m_col = -1; }
1035 wxGridCellCoords( int r, int c ) { m_row = r; m_col = c; }
1036
1037 // default copy ctor is ok
1038
1039 int GetRow() const { return m_row; }
1040 void SetRow( int n ) { m_row = n; }
1041 int GetCol() const { return m_col; }
1042 void SetCol( int n ) { m_col = n; }
1043 void Set( int row, int col ) { m_row = row; m_col = col; }
1044
1045 wxGridCellCoords& operator=( const wxGridCellCoords& other )
1046 {
1047 if ( &other != this )
1048 {
1049 m_row=other.m_row;
1050 m_col=other.m_col;
1051 }
1052 return *this;
1053 }
1054
1055 bool operator==( const wxGridCellCoords& other ) const
1056 {
1057 return (m_row == other.m_row && m_col == other.m_col);
1058 }
1059
1060 bool operator!=( const wxGridCellCoords& other ) const
1061 {
1062 return (m_row != other.m_row || m_col != other.m_col);
1063 }
1064
1065 bool operator!() const
1066 {
1067 return (m_row == -1 && m_col == -1 );
1068 }
1069
1070 private:
1071 int m_row;
1072 int m_col;
1073 };
1074
1075
1076 // For comparisons...
1077 //
1078 extern WXDLLIMPEXP_ADV wxGridCellCoords wxGridNoCellCoords;
1079 extern WXDLLIMPEXP_ADV wxRect wxGridNoCellRect;
1080
1081 // An array of cell coords...
1082 //
1083 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellCoords, wxGridCellCoordsArray,
1084 class WXDLLIMPEXP_ADV);
1085
1086 // ----------------------------------------------------------------------------
1087 // wxGrid
1088 // ----------------------------------------------------------------------------
1089
1090 class WXDLLIMPEXP_ADV wxGrid : public wxScrolledWindow
1091 {
1092 public:
1093 wxGrid();
1094
1095 wxGrid( wxWindow *parent,
1096 wxWindowID id,
1097 const wxPoint& pos = wxDefaultPosition,
1098 const wxSize& size = wxDefaultSize,
1099 long style = wxWANTS_CHARS,
1100 const wxString& name = wxGridNameStr );
1101
1102 bool Create( wxWindow *parent,
1103 wxWindowID id,
1104 const wxPoint& pos = wxDefaultPosition,
1105 const wxSize& size = wxDefaultSize,
1106 long style = wxWANTS_CHARS,
1107 const wxString& name = wxGridNameStr );
1108
1109 virtual ~wxGrid();
1110
1111 enum wxGridSelectionModes
1112 {
1113 wxGridSelectCells,
1114 wxGridSelectRows,
1115 wxGridSelectColumns
1116 };
1117
1118 bool CreateGrid( int numRows, int numCols,
1119 wxGridSelectionModes selmode = wxGridSelectCells );
1120
1121 void SetSelectionMode(wxGridSelectionModes selmode);
1122 wxGridSelectionModes GetSelectionMode() const;
1123
1124 // ------ grid dimensions
1125 //
1126 int GetNumberRows() const { return m_numRows; }
1127 int GetNumberCols() const { return m_numCols; }
1128
1129
1130 // ------ display update functions
1131 //
1132 wxArrayInt CalcRowLabelsExposed( const wxRegion& reg ) const;
1133
1134 wxArrayInt CalcColLabelsExposed( const wxRegion& reg ) const;
1135 wxGridCellCoordsArray CalcCellsExposed( const wxRegion& reg ) const;
1136
1137
1138 // ------ event handlers
1139 //
1140 void ProcessRowLabelMouseEvent( wxMouseEvent& event );
1141 void ProcessColLabelMouseEvent( wxMouseEvent& event );
1142 void ProcessCornerLabelMouseEvent( wxMouseEvent& event );
1143 void ProcessGridCellMouseEvent( wxMouseEvent& event );
1144 bool ProcessTableMessage( wxGridTableMessage& );
1145
1146 void DoEndDragResizeRow();
1147 void DoEndDragResizeCol();
1148 void DoEndDragMoveCol();
1149
1150 wxGridTableBase * GetTable() const { return m_table; }
1151 bool SetTable( wxGridTableBase *table, bool takeOwnership = false,
1152 wxGridSelectionModes selmode = wxGridSelectCells );
1153
1154 void ClearGrid();
1155 bool InsertRows( int pos = 0, int numRows = 1, bool updateLabels = true );
1156 bool AppendRows( int numRows = 1, bool updateLabels = true );
1157 bool DeleteRows( int pos = 0, int numRows = 1, bool updateLabels = true );
1158 bool InsertCols( int pos = 0, int numCols = 1, bool updateLabels = true );
1159 bool AppendCols( int numCols = 1, bool updateLabels = true );
1160 bool DeleteCols( int pos = 0, int numCols = 1, bool updateLabels = true );
1161
1162 void DrawGridCellArea( wxDC& dc , const wxGridCellCoordsArray& cells );
1163 void DrawGridSpace( wxDC& dc );
1164 void DrawCellBorder( wxDC& dc, const wxGridCellCoords& );
1165 void DrawAllGridLines( wxDC& dc, const wxRegion & reg );
1166 void DrawCell( wxDC& dc, const wxGridCellCoords& );
1167 void DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells);
1168
1169 // this function is called when the current cell highlight must be redrawn
1170 // and may be overridden by the user
1171 virtual void DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr );
1172
1173 virtual void DrawRowLabels( wxDC& dc, const wxArrayInt& rows );
1174 virtual void DrawRowLabel( wxDC& dc, int row );
1175
1176 virtual void DrawColLabels( wxDC& dc, const wxArrayInt& cols );
1177 virtual void DrawColLabel( wxDC& dc, int col );
1178
1179
1180 // ------ Cell text drawing functions
1181 //
1182 void DrawTextRectangle( wxDC& dc, const wxString&, const wxRect&,
1183 int horizontalAlignment = wxALIGN_LEFT,
1184 int verticalAlignment = wxALIGN_TOP,
1185 int textOrientation = wxHORIZONTAL );
1186
1187 void DrawTextRectangle( wxDC& dc, const wxArrayString& lines, const wxRect&,
1188 int horizontalAlignment = wxALIGN_LEFT,
1189 int verticalAlignment = wxALIGN_TOP,
1190 int textOrientation = wxHORIZONTAL );
1191
1192
1193 // Split a string containing newline characters into an array of
1194 // strings and return the number of lines
1195 //
1196 void StringToLines( const wxString& value, wxArrayString& lines ) const;
1197
1198 void GetTextBoxSize( const wxDC& dc,
1199 const wxArrayString& lines,
1200 long *width, long *height ) const;
1201
1202
1203 // ------
1204 // Code that does a lot of grid modification can be enclosed
1205 // between BeginBatch() and EndBatch() calls to avoid screen
1206 // flicker
1207 //
1208 void BeginBatch() { m_batchCount++; }
1209 void EndBatch();
1210
1211 int GetBatchCount() { return m_batchCount; }
1212
1213 virtual void Refresh(bool eraseb = true,
1214 const wxRect* rect = (const wxRect *) NULL);
1215
1216 // Use this, rather than wxWindow::Refresh(), to force an
1217 // immediate repainting of the grid. Has no effect if you are
1218 // already inside a BeginBatch / EndBatch block.
1219 //
1220 // This function is necessary because wxGrid has a minimal OnPaint()
1221 // handler to reduce screen flicker.
1222 //
1223 void ForceRefresh();
1224
1225
1226 // ------ edit control functions
1227 //
1228 bool IsEditable() const { return m_editable; }
1229 void EnableEditing( bool edit );
1230
1231 void EnableCellEditControl( bool enable = true );
1232 void DisableCellEditControl() { EnableCellEditControl(false); }
1233 bool CanEnableCellControl() const;
1234 bool IsCellEditControlEnabled() const;
1235 bool IsCellEditControlShown() const;
1236
1237 bool IsCurrentCellReadOnly() const;
1238
1239 void ShowCellEditControl();
1240 void HideCellEditControl();
1241 void SaveEditControlValue();
1242
1243
1244 // ------ grid location functions
1245 // Note that all of these functions work with the logical coordinates of
1246 // grid cells and labels so you will need to convert from device
1247 // coordinates for mouse events etc.
1248 //
1249 void XYToCell( int x, int y, wxGridCellCoords& ) const;
1250 int YToRow( int y ) const;
1251 int XToCol( int x, bool clipToMinMax = false ) const;
1252
1253 int YToEdgeOfRow( int y ) const;
1254 int XToEdgeOfCol( int x ) const;
1255
1256 wxRect CellToRect( int row, int col ) const;
1257 wxRect CellToRect( const wxGridCellCoords& coords ) const
1258 { return CellToRect( coords.GetRow(), coords.GetCol() ); }
1259
1260 int GetGridCursorRow() const { return m_currentCellCoords.GetRow(); }
1261 int GetGridCursorCol() const { return m_currentCellCoords.GetCol(); }
1262
1263 // check to see if a cell is either wholly visible (the default arg) or
1264 // at least partially visible in the grid window
1265 //
1266 bool IsVisible( int row, int col, bool wholeCellVisible = true ) const;
1267 bool IsVisible( const wxGridCellCoords& coords, bool wholeCellVisible = true ) const
1268 { return IsVisible( coords.GetRow(), coords.GetCol(), wholeCellVisible ); }
1269 void MakeCellVisible( int row, int col );
1270 void MakeCellVisible( const wxGridCellCoords& coords )
1271 { MakeCellVisible( coords.GetRow(), coords.GetCol() ); }
1272
1273
1274 // ------ grid cursor movement functions
1275 //
1276 void SetGridCursor( int row, int col )
1277 { SetCurrentCell( wxGridCellCoords(row, col) ); }
1278
1279 bool MoveCursorUp( bool expandSelection );
1280 bool MoveCursorDown( bool expandSelection );
1281 bool MoveCursorLeft( bool expandSelection );
1282 bool MoveCursorRight( bool expandSelection );
1283 bool MovePageDown();
1284 bool MovePageUp();
1285 bool MoveCursorUpBlock( bool expandSelection );
1286 bool MoveCursorDownBlock( bool expandSelection );
1287 bool MoveCursorLeftBlock( bool expandSelection );
1288 bool MoveCursorRightBlock( bool expandSelection );
1289
1290
1291 // ------ label and gridline formatting
1292 //
1293 int GetDefaultRowLabelSize() const { return WXGRID_DEFAULT_ROW_LABEL_WIDTH; }
1294 int GetRowLabelSize() const { return m_rowLabelWidth; }
1295 int GetDefaultColLabelSize() const { return WXGRID_DEFAULT_COL_LABEL_HEIGHT; }
1296 int GetColLabelSize() const { return m_colLabelHeight; }
1297 wxColour GetLabelBackgroundColour() const { return m_labelBackgroundColour; }
1298 wxColour GetLabelTextColour() const { return m_labelTextColour; }
1299 wxFont GetLabelFont() const { return m_labelFont; }
1300 void GetRowLabelAlignment( int *horiz, int *vert ) const;
1301 void GetColLabelAlignment( int *horiz, int *vert ) const;
1302 int GetColLabelTextOrientation() const;
1303 wxString GetRowLabelValue( int row ) const;
1304 wxString GetColLabelValue( int col ) const;
1305 wxColour GetGridLineColour() const { return m_gridLineColour; }
1306
1307 // these methods may be overridden to customize individual grid lines
1308 // appearance
1309 virtual wxPen GetDefaultGridLinePen();
1310 virtual wxPen GetRowGridLinePen(int row);
1311 virtual wxPen GetColGridLinePen(int col);
1312 wxColour GetCellHighlightColour() const { return m_cellHighlightColour; }
1313 int GetCellHighlightPenWidth() const { return m_cellHighlightPenWidth; }
1314 int GetCellHighlightROPenWidth() const { return m_cellHighlightROPenWidth; }
1315
1316 void SetUseNativeColLabels( bool native = true );
1317 void SetRowLabelSize( int width );
1318 void SetColLabelSize( int height );
1319 void HideRowLabels() { SetRowLabelSize( 0 ); }
1320 void HideColLabels() { SetColLabelSize( 0 ); }
1321 void SetLabelBackgroundColour( const wxColour& );
1322 void SetLabelTextColour( const wxColour& );
1323 void SetLabelFont( const wxFont& );
1324 void SetRowLabelAlignment( int horiz, int vert );
1325 void SetColLabelAlignment( int horiz, int vert );
1326 void SetColLabelTextOrientation( int textOrientation );
1327 void SetRowLabelValue( int row, const wxString& );
1328 void SetColLabelValue( int col, const wxString& );
1329 void SetGridLineColour( const wxColour& );
1330 void SetCellHighlightColour( const wxColour& );
1331 void SetCellHighlightPenWidth(int width);
1332 void SetCellHighlightROPenWidth(int width);
1333
1334 void EnableDragRowSize( bool enable = true );
1335 void DisableDragRowSize() { EnableDragRowSize( false ); }
1336 bool CanDragRowSize() const { return m_canDragRowSize; }
1337 void EnableDragColSize( bool enable = true );
1338 void DisableDragColSize() { EnableDragColSize( false ); }
1339 bool CanDragColSize() const { return m_canDragColSize; }
1340 void EnableDragColMove( bool enable = true );
1341 void DisableDragColMove() { EnableDragColMove( false ); }
1342 bool CanDragColMove() const { return m_canDragColMove; }
1343 void EnableDragGridSize(bool enable = true);
1344 void DisableDragGridSize() { EnableDragGridSize(false); }
1345 bool CanDragGridSize() const { return m_canDragGridSize; }
1346
1347 void EnableDragCell( bool enable = true );
1348 void DisableDragCell() { EnableDragCell( false ); }
1349 bool CanDragCell() const { return m_canDragCell; }
1350
1351 // this sets the specified attribute for this cell or in this row/col
1352 void SetAttr(int row, int col, wxGridCellAttr *attr);
1353 void SetRowAttr(int row, wxGridCellAttr *attr);
1354 void SetColAttr(int col, wxGridCellAttr *attr);
1355
1356 // returns the attribute we may modify in place: a new one if this cell
1357 // doesn't have any yet or the existing one if it does
1358 //
1359 // DecRef() must be called on the returned pointer, as usual
1360 wxGridCellAttr *GetOrCreateCellAttr(int row, int col) const;
1361
1362
1363 // shortcuts for setting the column parameters
1364
1365 // set the format for the data in the column: default is string
1366 void SetColFormatBool(int col);
1367 void SetColFormatNumber(int col);
1368 void SetColFormatFloat(int col, int width = -1, int precision = -1);
1369 void SetColFormatCustom(int col, const wxString& typeName);
1370
1371 void EnableGridLines( bool enable = true );
1372 bool GridLinesEnabled() const { return m_gridLinesEnabled; }
1373
1374 // ------ row and col formatting
1375 //
1376 int GetDefaultRowSize() const;
1377 int GetRowSize( int row ) const;
1378 int GetDefaultColSize() const;
1379 int GetColSize( int col ) const;
1380 wxColour GetDefaultCellBackgroundColour() const;
1381 wxColour GetCellBackgroundColour( int row, int col ) const;
1382 wxColour GetDefaultCellTextColour() const;
1383 wxColour GetCellTextColour( int row, int col ) const;
1384 wxFont GetDefaultCellFont() const;
1385 wxFont GetCellFont( int row, int col ) const;
1386 void GetDefaultCellAlignment( int *horiz, int *vert ) const;
1387 void GetCellAlignment( int row, int col, int *horiz, int *vert ) const;
1388 bool GetDefaultCellOverflow() const;
1389 bool GetCellOverflow( int row, int col ) const;
1390 void GetCellSize( int row, int col, int *num_rows, int *num_cols ) const;
1391
1392 void SetDefaultRowSize( int height, bool resizeExistingRows = false );
1393 void SetRowSize( int row, int height );
1394 void SetDefaultColSize( int width, bool resizeExistingCols = false );
1395
1396 void SetColSize( int col, int width );
1397
1398 //Column positions
1399 int GetColAt( int colPos ) const
1400 {
1401 if ( m_colAt.IsEmpty() )
1402 return colPos;
1403 else
1404 return m_colAt[colPos];
1405 }
1406
1407 void SetColPos( int colID, int newPos );
1408
1409 int GetColPos( int colID ) const
1410 {
1411 if ( m_colAt.IsEmpty() )
1412 return colID;
1413 else
1414 {
1415 for ( int i = 0; i < m_numCols; i++ )
1416 {
1417 if ( m_colAt[i] == colID )
1418 return i;
1419 }
1420 }
1421
1422 return -1;
1423 }
1424
1425 // automatically size the column or row to fit to its contents, if
1426 // setAsMin is true, this optimal width will also be set as minimal width
1427 // for this column
1428 void AutoSizeColumn( int col, bool setAsMin = true )
1429 { AutoSizeColOrRow(col, setAsMin, wxGRID_COLUMN); }
1430 void AutoSizeRow( int row, bool setAsMin = true )
1431 { AutoSizeColOrRow(row, setAsMin, wxGRID_ROW); }
1432
1433 // auto size all columns (very ineffective for big grids!)
1434 void AutoSizeColumns( bool setAsMin = true )
1435 { (void)SetOrCalcColumnSizes(false, setAsMin); }
1436
1437 void AutoSizeRows( bool setAsMin = true )
1438 { (void)SetOrCalcRowSizes(false, setAsMin); }
1439
1440 // auto size the grid, that is make the columns/rows of the "right" size
1441 // and also set the grid size to just fit its contents
1442 void AutoSize();
1443
1444 // Note for both AutoSizeRowLabelSize and AutoSizeColLabelSize:
1445 // If col equals to wxGRID_AUTOSIZE value then function autosizes labels column
1446 // instead of data column. Note that this operation may be slow for large
1447 // tables.
1448 // autosize row height depending on label text
1449 void AutoSizeRowLabelSize( int row );
1450
1451 // autosize column width depending on label text
1452 void AutoSizeColLabelSize( int col );
1453
1454 // column won't be resized to be lesser width - this must be called during
1455 // the grid creation because it won't resize the column if it's already
1456 // narrower than the minimal width
1457 void SetColMinimalWidth( int col, int width );
1458 void SetRowMinimalHeight( int row, int width );
1459
1460 /* These members can be used to query and modify the minimal
1461 * acceptable size of grid rows and columns. Call this function in
1462 * your code which creates the grid if you want to display cells
1463 * with a size smaller than the default acceptable minimum size.
1464 * Like the members SetColMinimalWidth and SetRowMinimalWidth,
1465 * the existing rows or columns will not be checked/resized.
1466 */
1467 void SetColMinimalAcceptableWidth( int width );
1468 void SetRowMinimalAcceptableHeight( int width );
1469 int GetColMinimalAcceptableWidth() const;
1470 int GetRowMinimalAcceptableHeight() const;
1471
1472 void SetDefaultCellBackgroundColour( const wxColour& );
1473 void SetCellBackgroundColour( int row, int col, const wxColour& );
1474 void SetDefaultCellTextColour( const wxColour& );
1475
1476 void SetCellTextColour( int row, int col, const wxColour& );
1477 void SetDefaultCellFont( const wxFont& );
1478 void SetCellFont( int row, int col, const wxFont& );
1479 void SetDefaultCellAlignment( int horiz, int vert );
1480 void SetCellAlignment( int row, int col, int horiz, int vert );
1481 void SetDefaultCellOverflow( bool allow );
1482 void SetCellOverflow( int row, int col, bool allow );
1483 void SetCellSize( int row, int col, int num_rows, int num_cols );
1484
1485 // takes ownership of the pointer
1486 void SetDefaultRenderer(wxGridCellRenderer *renderer);
1487 void SetCellRenderer(int row, int col, wxGridCellRenderer *renderer);
1488 wxGridCellRenderer *GetDefaultRenderer() const;
1489 wxGridCellRenderer* GetCellRenderer(int row, int col) const;
1490
1491 // takes ownership of the pointer
1492 void SetDefaultEditor(wxGridCellEditor *editor);
1493 void SetCellEditor(int row, int col, wxGridCellEditor *editor);
1494 wxGridCellEditor *GetDefaultEditor() const;
1495 wxGridCellEditor* GetCellEditor(int row, int col) const;
1496
1497
1498
1499 // ------ cell value accessors
1500 //
1501 wxString GetCellValue( int row, int col ) const
1502 {
1503 if ( m_table )
1504 {
1505 return m_table->GetValue( row, col );
1506 }
1507 else
1508 {
1509 return wxEmptyString;
1510 }
1511 }
1512
1513 wxString GetCellValue( const wxGridCellCoords& coords ) const
1514 { return GetCellValue( coords.GetRow(), coords.GetCol() ); }
1515
1516 void SetCellValue( int row, int col, const wxString& s );
1517 void SetCellValue( const wxGridCellCoords& coords, const wxString& s )
1518 { SetCellValue( coords.GetRow(), coords.GetCol(), s ); }
1519
1520 // returns true if the cell can't be edited
1521 bool IsReadOnly(int row, int col) const;
1522
1523 // make the cell editable/readonly
1524 void SetReadOnly(int row, int col, bool isReadOnly = true);
1525
1526 // ------ select blocks of cells
1527 //
1528 void SelectRow( int row, bool addToSelected = false );
1529 void SelectCol( int col, bool addToSelected = false );
1530
1531 void SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
1532 bool addToSelected = false );
1533
1534 void SelectBlock( const wxGridCellCoords& topLeft,
1535 const wxGridCellCoords& bottomRight,
1536 bool addToSelected = false )
1537 { SelectBlock( topLeft.GetRow(), topLeft.GetCol(),
1538 bottomRight.GetRow(), bottomRight.GetCol(),
1539 addToSelected ); }
1540
1541 void SelectAll();
1542
1543 bool IsSelection() const;
1544
1545 // ------ deselect blocks or cells
1546 //
1547 void DeselectRow( int row );
1548 void DeselectCol( int col );
1549 void DeselectCell( int row, int col );
1550
1551 void ClearSelection();
1552
1553 bool IsInSelection( int row, int col ) const;
1554
1555 bool IsInSelection( const wxGridCellCoords& coords ) const
1556 { return IsInSelection( coords.GetRow(), coords.GetCol() ); }
1557
1558 wxGridCellCoordsArray GetSelectedCells() const;
1559 wxGridCellCoordsArray GetSelectionBlockTopLeft() const;
1560 wxGridCellCoordsArray GetSelectionBlockBottomRight() const;
1561 wxArrayInt GetSelectedRows() const;
1562 wxArrayInt GetSelectedCols() const;
1563
1564 // This function returns the rectangle that encloses the block of cells
1565 // limited by TopLeft and BottomRight cell in device coords and clipped
1566 // to the client size of the grid window.
1567 //
1568 wxRect BlockToDeviceRect( const wxGridCellCoords & topLeft,
1569 const wxGridCellCoords & bottomRight ) const;
1570
1571 // Access or update the selection fore/back colours
1572 wxColour GetSelectionBackground() const
1573 { return m_selectionBackground; }
1574 wxColour GetSelectionForeground() const
1575 { return m_selectionForeground; }
1576
1577 void SetSelectionBackground(const wxColour& c) { m_selectionBackground = c; }
1578 void SetSelectionForeground(const wxColour& c) { m_selectionForeground = c; }
1579
1580
1581 // Methods for a registry for mapping data types to Renderers/Editors
1582 void RegisterDataType(const wxString& typeName,
1583 wxGridCellRenderer* renderer,
1584 wxGridCellEditor* editor);
1585 // DJC MAPTEK
1586 virtual wxGridCellEditor* GetDefaultEditorForCell(int row, int col) const;
1587 wxGridCellEditor* GetDefaultEditorForCell(const wxGridCellCoords& c) const
1588 { return GetDefaultEditorForCell(c.GetRow(), c.GetCol()); }
1589 virtual wxGridCellRenderer* GetDefaultRendererForCell(int row, int col) const;
1590 virtual wxGridCellEditor* GetDefaultEditorForType(const wxString& typeName) const;
1591 virtual wxGridCellRenderer* GetDefaultRendererForType(const wxString& typeName) const;
1592
1593 // grid may occupy more space than needed for its rows/columns, this
1594 // function allows to set how big this extra space is
1595 void SetMargins(int extraWidth, int extraHeight)
1596 {
1597 m_extraWidth = extraWidth;
1598 m_extraHeight = extraHeight;
1599
1600 CalcDimensions();
1601 }
1602
1603 // Accessors for component windows
1604 wxWindow* GetGridWindow() const { return (wxWindow*)m_gridWin; }
1605 wxWindow* GetGridRowLabelWindow() const { return (wxWindow*)m_rowLabelWin; }
1606 wxWindow* GetGridColLabelWindow() const { return (wxWindow*)m_colLabelWin; }
1607 wxWindow* GetGridCornerLabelWindow() const { return (wxWindow*)m_cornerLabelWin; }
1608
1609 // Allow adjustment of scroll increment. The default is (15, 15).
1610 void SetScrollLineX(int x) { m_scrollLineX = x; }
1611 void SetScrollLineY(int y) { m_scrollLineY = y; }
1612 int GetScrollLineX() const { return m_scrollLineX; }
1613 int GetScrollLineY() const { return m_scrollLineY; }
1614
1615 // Implementation
1616 int GetScrollX(int x) const
1617 {
1618 return (x + GetScrollLineX() - 1) / GetScrollLineX();
1619 }
1620
1621 int GetScrollY(int y) const
1622 {
1623 return (y + GetScrollLineY() - 1) / GetScrollLineY();
1624 }
1625
1626
1627 // override some base class functions
1628 virtual bool Enable(bool enable = true);
1629 virtual wxWindow *GetMainWindowOfCompositeControl()
1630 { return (wxWindow*)m_gridWin; }
1631
1632 // ------- drag and drop
1633 #if wxUSE_DRAG_AND_DROP
1634 virtual void SetDropTarget(wxDropTarget *dropTarget);
1635 #endif // wxUSE_DRAG_AND_DROP
1636
1637 // ------ For compatibility with previous wxGrid only...
1638 //
1639 // ************************************************
1640 // ** Don't use these in new code because they **
1641 // ** are liable to disappear in a future **
1642 // ** revision **
1643 // ************************************************
1644 //
1645
1646 wxGrid( wxWindow *parent,
1647 int x, int y, int w = wxDefaultCoord, int h = wxDefaultCoord,
1648 long style = wxWANTS_CHARS,
1649 const wxString& name = wxPanelNameStr )
1650 : wxScrolledWindow( parent, wxID_ANY, wxPoint(x,y), wxSize(w,h),
1651 (style|wxWANTS_CHARS), name )
1652 {
1653 InitVars();
1654 Create();
1655 }
1656
1657 void SetCellValue( const wxString& val, int row, int col )
1658 { SetCellValue( row, col, val ); }
1659
1660 void UpdateDimensions()
1661 { CalcDimensions(); }
1662
1663 int GetRows() const { return GetNumberRows(); }
1664 int GetCols() const { return GetNumberCols(); }
1665 int GetCursorRow() const { return GetGridCursorRow(); }
1666 int GetCursorColumn() const { return GetGridCursorCol(); }
1667
1668 int GetScrollPosX() const { return 0; }
1669 int GetScrollPosY() const { return 0; }
1670
1671 void SetScrollX( int WXUNUSED(x) ) { }
1672 void SetScrollY( int WXUNUSED(y) ) { }
1673
1674 void SetColumnWidth( int col, int width )
1675 { SetColSize( col, width ); }
1676
1677 int GetColumnWidth( int col ) const
1678 { return GetColSize( col ); }
1679
1680 void SetRowHeight( int row, int height )
1681 { SetRowSize( row, height ); }
1682
1683 // GetRowHeight() is below
1684
1685 int GetViewHeight() const // returned num whole rows visible
1686 { return 0; }
1687
1688 int GetViewWidth() const // returned num whole cols visible
1689 { return 0; }
1690
1691 void SetLabelSize( int orientation, int sz )
1692 {
1693 if ( orientation == wxHORIZONTAL )
1694 SetColLabelSize( sz );
1695 else
1696 SetRowLabelSize( sz );
1697 }
1698
1699 int GetLabelSize( int orientation ) const
1700 {
1701 if ( orientation == wxHORIZONTAL )
1702 return GetColLabelSize();
1703 else
1704 return GetRowLabelSize();
1705 }
1706
1707 void SetLabelAlignment( int orientation, int align )
1708 {
1709 if ( orientation == wxHORIZONTAL )
1710 SetColLabelAlignment( align, -1 );
1711 else
1712 SetRowLabelAlignment( align, -1 );
1713 }
1714
1715 int GetLabelAlignment( int orientation, int WXUNUSED(align) ) const
1716 {
1717 int h, v;
1718 if ( orientation == wxHORIZONTAL )
1719 {
1720 GetColLabelAlignment( &h, &v );
1721 return h;
1722 }
1723 else
1724 {
1725 GetRowLabelAlignment( &h, &v );
1726 return h;
1727 }
1728 }
1729
1730 void SetLabelValue( int orientation, const wxString& val, int pos )
1731 {
1732 if ( orientation == wxHORIZONTAL )
1733 SetColLabelValue( pos, val );
1734 else
1735 SetRowLabelValue( pos, val );
1736 }
1737
1738 wxString GetLabelValue( int orientation, int pos) const
1739 {
1740 if ( orientation == wxHORIZONTAL )
1741 return GetColLabelValue( pos );
1742 else
1743 return GetRowLabelValue( pos );
1744 }
1745
1746 wxFont GetCellTextFont() const
1747 { return m_defaultCellAttr->GetFont(); }
1748
1749 wxFont GetCellTextFont(int WXUNUSED(row), int WXUNUSED(col)) const
1750 { return m_defaultCellAttr->GetFont(); }
1751
1752 void SetCellTextFont(const wxFont& fnt)
1753 { SetDefaultCellFont( fnt ); }
1754
1755 void SetCellTextFont(const wxFont& fnt, int row, int col)
1756 { SetCellFont( row, col, fnt ); }
1757
1758 void SetCellTextColour(const wxColour& val, int row, int col)
1759 { SetCellTextColour( row, col, val ); }
1760
1761 void SetCellTextColour(const wxColour& col)
1762 { SetDefaultCellTextColour( col ); }
1763
1764 void SetCellBackgroundColour(const wxColour& col)
1765 { SetDefaultCellBackgroundColour( col ); }
1766
1767 void SetCellBackgroundColour(const wxColour& colour, int row, int col)
1768 { SetCellBackgroundColour( row, col, colour ); }
1769
1770 bool GetEditable() const { return IsEditable(); }
1771 void SetEditable( bool edit = true ) { EnableEditing( edit ); }
1772 bool GetEditInPlace() const { return IsCellEditControlEnabled(); }
1773
1774 void SetEditInPlace(bool WXUNUSED(edit) = true) { }
1775
1776 void SetCellAlignment( int align, int row, int col)
1777 { SetCellAlignment(row, col, align, wxALIGN_CENTER); }
1778 void SetCellAlignment( int WXUNUSED(align) ) {}
1779 void SetCellBitmap(wxBitmap *WXUNUSED(bitmap), int WXUNUSED(row), int WXUNUSED(col))
1780 { }
1781 void SetDividerPen(const wxPen& WXUNUSED(pen)) { }
1782 wxPen& GetDividerPen() const;
1783 void OnActivate(bool WXUNUSED(active)) {}
1784
1785 // ******** End of compatibility functions **********
1786
1787
1788
1789 // ------ control IDs
1790 enum { wxGRID_CELLCTRL = 2000,
1791 wxGRID_TOPCTRL };
1792
1793 // ------ control types
1794 enum { wxGRID_TEXTCTRL = 2100,
1795 wxGRID_CHECKBOX,
1796 wxGRID_CHOICE,
1797 wxGRID_COMBOBOX };
1798
1799 // overridden wxWindow methods
1800 virtual void Fit();
1801
1802 // implementation only
1803 void CancelMouseCapture();
1804
1805 protected:
1806 virtual wxSize DoGetBestSize() const;
1807
1808 bool m_created;
1809
1810 wxGridWindow *m_gridWin;
1811 wxGridRowLabelWindow *m_rowLabelWin;
1812 wxGridColLabelWindow *m_colLabelWin;
1813 wxGridCornerLabelWindow *m_cornerLabelWin;
1814
1815 wxGridTableBase *m_table;
1816 bool m_ownTable;
1817
1818 int m_numRows;
1819 int m_numCols;
1820
1821 wxGridCellCoords m_currentCellCoords;
1822
1823 wxGridCellCoords m_selectingTopLeft;
1824 wxGridCellCoords m_selectingBottomRight;
1825 wxGridCellCoords m_selectingKeyboard;
1826 wxGridSelection *m_selection;
1827 wxColour m_selectionBackground;
1828 wxColour m_selectionForeground;
1829
1830 // NB: *never* access m_row/col arrays directly because they are created
1831 // on demand, *always* use accessor functions instead!
1832
1833 // init the m_rowHeights/Bottoms arrays with default values
1834 void InitRowHeights();
1835
1836 int m_defaultRowHeight;
1837 int m_minAcceptableRowHeight;
1838 wxArrayInt m_rowHeights;
1839 wxArrayInt m_rowBottoms;
1840
1841 // init the m_colWidths/Rights arrays
1842 void InitColWidths();
1843
1844 int m_defaultColWidth;
1845 int m_minAcceptableColWidth;
1846 wxArrayInt m_colWidths;
1847 wxArrayInt m_colRights;
1848
1849 bool m_nativeColumnLabels;
1850
1851 // get the col/row coords
1852 int GetColWidth(int col) const;
1853 int GetColLeft(int col) const;
1854 int GetColRight(int col) const;
1855
1856 // this function must be public for compatibility...
1857 public:
1858 int GetRowHeight(int row) const;
1859 protected:
1860
1861 int GetRowTop(int row) const;
1862 int GetRowBottom(int row) const;
1863
1864 int m_rowLabelWidth;
1865 int m_colLabelHeight;
1866
1867 // the size of the margin left to the right and bottom of the cell area
1868 int m_extraWidth,
1869 m_extraHeight;
1870
1871 wxColour m_labelBackgroundColour;
1872 wxColour m_labelTextColour;
1873 wxFont m_labelFont;
1874
1875 int m_rowLabelHorizAlign;
1876 int m_rowLabelVertAlign;
1877 int m_colLabelHorizAlign;
1878 int m_colLabelVertAlign;
1879 int m_colLabelTextOrientation;
1880
1881 bool m_defaultRowLabelValues;
1882 bool m_defaultColLabelValues;
1883
1884 wxColour m_gridLineColour;
1885 bool m_gridLinesEnabled;
1886 wxColour m_cellHighlightColour;
1887 int m_cellHighlightPenWidth;
1888 int m_cellHighlightROPenWidth;
1889
1890
1891 // common part of AutoSizeColumn/Row() and GetBestSize()
1892 int SetOrCalcColumnSizes(bool calcOnly, bool setAsMin = true);
1893 int SetOrCalcRowSizes(bool calcOnly, bool setAsMin = true);
1894
1895 // common part of AutoSizeColumn/Row()
1896 void AutoSizeColOrRow(int n, bool setAsMin, wxGridDirection direction);
1897
1898 // Calculate the minimum acceptable size for labels area
1899 wxCoord CalcColOrRowLabelAreaMinSize(wxGridDirection direction);
1900
1901 // if a column has a minimal width, it will be the value for it in this
1902 // hash table
1903 wxLongToLongHashMap m_colMinWidths,
1904 m_rowMinHeights;
1905
1906 // get the minimal width of the given column/row
1907 int GetColMinimalWidth(int col) const;
1908 int GetRowMinimalHeight(int col) const;
1909
1910 // do we have some place to store attributes in?
1911 bool CanHaveAttributes() const;
1912
1913 // cell attribute cache (currently we only cache 1, may be will do
1914 // more/better later)
1915 struct CachedAttr
1916 {
1917 int row, col;
1918 wxGridCellAttr *attr;
1919 } m_attrCache;
1920
1921 // invalidates the attribute cache
1922 void ClearAttrCache();
1923
1924 // adds an attribute to cache
1925 void CacheAttr(int row, int col, wxGridCellAttr *attr) const;
1926
1927 // looks for an attr in cache, returns true if found
1928 bool LookupAttr(int row, int col, wxGridCellAttr **attr) const;
1929
1930 // looks for the attr in cache, if not found asks the table and caches the
1931 // result
1932 wxGridCellAttr *GetCellAttr(int row, int col) const;
1933 wxGridCellAttr *GetCellAttr(const wxGridCellCoords& coords ) const
1934 { return GetCellAttr( coords.GetRow(), coords.GetCol() ); }
1935
1936 // the default cell attr object for cells that don't have their own
1937 wxGridCellAttr* m_defaultCellAttr;
1938
1939
1940 bool m_inOnKeyDown;
1941 int m_batchCount;
1942
1943
1944 wxGridTypeRegistry* m_typeRegistry;
1945
1946 enum CursorMode
1947 {
1948 WXGRID_CURSOR_SELECT_CELL,
1949 WXGRID_CURSOR_RESIZE_ROW,
1950 WXGRID_CURSOR_RESIZE_COL,
1951 WXGRID_CURSOR_SELECT_ROW,
1952 WXGRID_CURSOR_SELECT_COL,
1953 WXGRID_CURSOR_MOVE_COL
1954 };
1955
1956 // this method not only sets m_cursorMode but also sets the correct cursor
1957 // for the given mode and, if captureMouse is not false releases the mouse
1958 // if it was captured and captures it if it must be captured
1959 //
1960 // for this to work, you should always use it and not set m_cursorMode
1961 // directly!
1962 void ChangeCursorMode(CursorMode mode,
1963 wxWindow *win = (wxWindow *)NULL,
1964 bool captureMouse = true);
1965
1966 wxWindow *m_winCapture; // the window which captured the mouse
1967 CursorMode m_cursorMode;
1968
1969 //Column positions
1970 wxArrayInt m_colAt;
1971 int m_moveToCol;
1972
1973 bool m_canDragRowSize;
1974 bool m_canDragColSize;
1975 bool m_canDragColMove;
1976 bool m_canDragGridSize;
1977 bool m_canDragCell;
1978 int m_dragLastPos;
1979 int m_dragRowOrCol;
1980 bool m_isDragging;
1981 wxPoint m_startDragPos;
1982
1983 bool m_waitForSlowClick;
1984
1985 wxGridCellCoords m_selectionStart;
1986
1987 wxCursor m_rowResizeCursor;
1988 wxCursor m_colResizeCursor;
1989
1990 bool m_editable; // applies to whole grid
1991 bool m_cellEditCtrlEnabled; // is in-place edit currently shown?
1992
1993 int m_scrollLineX; // X scroll increment
1994 int m_scrollLineY; // Y scroll increment
1995
1996 void Create();
1997 void Init();
1998 void InitVars();
1999 void CalcDimensions();
2000 void CalcWindowSizes();
2001 bool Redimension( wxGridTableMessage& );
2002
2003
2004 int SendEvent( const wxEventType, int row, int col, wxMouseEvent& );
2005 int SendEvent( const wxEventType, int row, int col );
2006 int SendEvent( const wxEventType type)
2007 {
2008 return SendEvent(type,
2009 m_currentCellCoords.GetRow(),
2010 m_currentCellCoords.GetCol());
2011 }
2012
2013 void OnPaint( wxPaintEvent& );
2014 void OnSize( wxSizeEvent& );
2015 void OnKeyDown( wxKeyEvent& );
2016 void OnKeyUp( wxKeyEvent& );
2017 void OnChar( wxKeyEvent& );
2018 void OnEraseBackground( wxEraseEvent& );
2019
2020
2021 void SetCurrentCell( const wxGridCellCoords& coords );
2022 void SetCurrentCell( int row, int col )
2023 { SetCurrentCell( wxGridCellCoords(row, col) ); }
2024
2025 void HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol );
2026
2027 void HighlightBlock( const wxGridCellCoords& topLeft,
2028 const wxGridCellCoords& bottomRight )
2029 { HighlightBlock( topLeft.GetRow(), topLeft.GetCol(),
2030 bottomRight.GetRow(), bottomRight.GetCol() ); }
2031
2032 // ------ functions to get/send data (see also public functions)
2033 //
2034 bool GetModelValues();
2035 bool SetModelValues();
2036
2037 friend class WXDLLIMPEXP_FWD_ADV wxGridSelection;
2038
2039 private:
2040 // implement wxScrolledWindow method to return m_gridWin size
2041 virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size);
2042
2043 DECLARE_DYNAMIC_CLASS( wxGrid )
2044 DECLARE_EVENT_TABLE()
2045 DECLARE_NO_COPY_CLASS(wxGrid)
2046 };
2047
2048 // ----------------------------------------------------------------------------
2049 // wxGridUpdateLocker prevents updates to a grid during its lifetime
2050 // ----------------------------------------------------------------------------
2051
2052 class WXDLLIMPEXP_ADV wxGridUpdateLocker
2053 {
2054 public:
2055 // if the pointer is NULL, Create() can be called later
2056 wxGridUpdateLocker(wxGrid *grid = NULL)
2057 {
2058 Init(grid);
2059 }
2060
2061 // can be called if ctor was used with a NULL pointer, must not be called
2062 // more than once
2063 void Create(wxGrid *grid)
2064 {
2065 wxASSERT_MSG( !m_grid, _T("shouldn't be called more than once") );
2066
2067 Init(grid);
2068 }
2069
2070 ~wxGridUpdateLocker()
2071 {
2072 if ( m_grid )
2073 m_grid->EndBatch();
2074 }
2075
2076 private:
2077 void Init(wxGrid *grid)
2078 {
2079 m_grid = grid;
2080 if ( m_grid )
2081 m_grid->BeginBatch();
2082 }
2083
2084 wxGrid *m_grid;
2085
2086 DECLARE_NO_COPY_CLASS(wxGridUpdateLocker)
2087 };
2088
2089 // ----------------------------------------------------------------------------
2090 // Grid event class and event types
2091 // ----------------------------------------------------------------------------
2092
2093 class WXDLLIMPEXP_ADV wxGridEvent : public wxNotifyEvent
2094 {
2095 public:
2096 wxGridEvent()
2097 : wxNotifyEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
2098 m_selecting(0), m_control(0), m_meta(0), m_shift(0), m_alt(0)
2099 {
2100 }
2101
2102 wxGridEvent(int id, wxEventType type, wxObject* obj,
2103 int row=-1, int col=-1, int x=-1, int y=-1, bool sel = true,
2104 bool control = false, bool shift = false, bool alt = false, bool meta = false);
2105
2106 virtual int GetRow() { return m_row; }
2107 virtual int GetCol() { return m_col; }
2108 wxPoint GetPosition() { return wxPoint( m_x, m_y ); }
2109 bool Selecting() { return m_selecting; }
2110 bool ControlDown() { return m_control; }
2111 bool MetaDown() { return m_meta; }
2112 bool ShiftDown() { return m_shift; }
2113 bool AltDown() { return m_alt; }
2114 bool CmdDown()
2115 {
2116 #if defined(__WXMAC__) || defined(__WXCOCOA__)
2117 return MetaDown();
2118 #else
2119 return ControlDown();
2120 #endif
2121 }
2122
2123 virtual wxEvent *Clone() const { return new wxGridEvent(*this); }
2124
2125 protected:
2126 int m_row;
2127 int m_col;
2128 int m_x;
2129 int m_y;
2130 bool m_selecting;
2131 bool m_control;
2132 bool m_meta;
2133 bool m_shift;
2134 bool m_alt;
2135
2136 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxGridEvent)
2137 };
2138
2139 class WXDLLIMPEXP_ADV wxGridSizeEvent : public wxNotifyEvent
2140 {
2141 public:
2142 wxGridSizeEvent()
2143 : wxNotifyEvent(), m_rowOrCol(-1), m_x(-1), m_y(-1),
2144 m_control(0), m_meta(0), m_shift(0), m_alt(0)
2145 {
2146 }
2147
2148 wxGridSizeEvent(int id, wxEventType type, wxObject* obj,
2149 int rowOrCol=-1, int x=-1, int y=-1,
2150 bool control = false, bool shift = false, bool alt = false, bool meta = false);
2151
2152 int GetRowOrCol() { return m_rowOrCol; }
2153 wxPoint GetPosition() { return wxPoint( m_x, m_y ); }
2154 bool ControlDown() { return m_control; }
2155 bool MetaDown() { return m_meta; }
2156 bool ShiftDown() { return m_shift; }
2157 bool AltDown() { return m_alt; }
2158 bool CmdDown()
2159 {
2160 #if defined(__WXMAC__) || defined(__WXCOCOA__)
2161 return MetaDown();
2162 #else
2163 return ControlDown();
2164 #endif
2165 }
2166
2167 virtual wxEvent *Clone() const { return new wxGridSizeEvent(*this); }
2168
2169 protected:
2170 int m_rowOrCol;
2171 int m_x;
2172 int m_y;
2173 bool m_control;
2174 bool m_meta;
2175 bool m_shift;
2176 bool m_alt;
2177
2178 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxGridSizeEvent)
2179 };
2180
2181
2182 class WXDLLIMPEXP_ADV wxGridRangeSelectEvent : public wxNotifyEvent
2183 {
2184 public:
2185 wxGridRangeSelectEvent()
2186 : wxNotifyEvent()
2187 {
2188 m_topLeft = wxGridNoCellCoords;
2189 m_bottomRight = wxGridNoCellCoords;
2190 m_selecting = false;
2191 m_control = false;
2192 m_meta = false;
2193 m_shift = false;
2194 m_alt = false;
2195 }
2196
2197 wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
2198 const wxGridCellCoords& topLeft,
2199 const wxGridCellCoords& bottomRight,
2200 bool sel = true,
2201 bool control = false, bool shift = false,
2202 bool alt = false, bool meta = false);
2203
2204 wxGridCellCoords GetTopLeftCoords() { return m_topLeft; }
2205 wxGridCellCoords GetBottomRightCoords() { return m_bottomRight; }
2206 int GetTopRow() { return m_topLeft.GetRow(); }
2207 int GetBottomRow() { return m_bottomRight.GetRow(); }
2208 int GetLeftCol() { return m_topLeft.GetCol(); }
2209 int GetRightCol() { return m_bottomRight.GetCol(); }
2210 bool Selecting() { return m_selecting; }
2211 bool ControlDown() { return m_control; }
2212 bool MetaDown() { return m_meta; }
2213 bool ShiftDown() { return m_shift; }
2214 bool AltDown() { return m_alt; }
2215 bool CmdDown()
2216 {
2217 #if defined(__WXMAC__) || defined(__WXCOCOA__)
2218 return MetaDown();
2219 #else
2220 return ControlDown();
2221 #endif
2222 }
2223
2224 virtual wxEvent *Clone() const { return new wxGridRangeSelectEvent(*this); }
2225
2226 protected:
2227 wxGridCellCoords m_topLeft;
2228 wxGridCellCoords m_bottomRight;
2229 bool m_selecting;
2230 bool m_control;
2231 bool m_meta;
2232 bool m_shift;
2233 bool m_alt;
2234
2235 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxGridRangeSelectEvent)
2236 };
2237
2238
2239 class WXDLLIMPEXP_ADV wxGridEditorCreatedEvent : public wxCommandEvent {
2240 public:
2241 wxGridEditorCreatedEvent()
2242 : wxCommandEvent()
2243 {
2244 m_row = 0;
2245 m_col = 0;
2246 m_ctrl = NULL;
2247 }
2248
2249 wxGridEditorCreatedEvent(int id, wxEventType type, wxObject* obj,
2250 int row, int col, wxControl* ctrl);
2251
2252 int GetRow() { return m_row; }
2253 int GetCol() { return m_col; }
2254 wxControl* GetControl() { return m_ctrl; }
2255 void SetRow(int row) { m_row = row; }
2256 void SetCol(int col) { m_col = col; }
2257 void SetControl(wxControl* ctrl) { m_ctrl = ctrl; }
2258
2259 virtual wxEvent *Clone() const { return new wxGridEditorCreatedEvent(*this); }
2260
2261 private:
2262 int m_row;
2263 int m_col;
2264 wxControl* m_ctrl;
2265
2266 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxGridEditorCreatedEvent)
2267 };
2268
2269
2270 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_CELL_LEFT_CLICK;
2271 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_CELL_RIGHT_CLICK;
2272 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_CELL_LEFT_DCLICK;
2273 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_CELL_RIGHT_DCLICK;
2274 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_LABEL_LEFT_CLICK;
2275 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_LABEL_RIGHT_CLICK;
2276 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_LABEL_LEFT_DCLICK;
2277 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_LABEL_RIGHT_DCLICK;
2278 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_ROW_SIZE;
2279 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_COL_SIZE;
2280 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_RANGE_SELECT;
2281 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_CELL_CHANGE;
2282 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_SELECT_CELL;
2283 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_EDITOR_SHOWN;
2284 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_EDITOR_HIDDEN;
2285 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_EDITOR_CREATED;
2286 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_CELL_BEGIN_DRAG;
2287 extern WXDLLIMPEXP_ADV const wxEventType wxEVT_GRID_COL_MOVE;
2288
2289
2290 typedef void (wxEvtHandler::*wxGridEventFunction)(wxGridEvent&);
2291 typedef void (wxEvtHandler::*wxGridSizeEventFunction)(wxGridSizeEvent&);
2292 typedef void (wxEvtHandler::*wxGridRangeSelectEventFunction)(wxGridRangeSelectEvent&);
2293 typedef void (wxEvtHandler::*wxGridEditorCreatedEventFunction)(wxGridEditorCreatedEvent&);
2294
2295 #define wxGridEventHandler(func) \
2296 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxGridEventFunction, &func)
2297
2298 #define wxGridSizeEventHandler(func) \
2299 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxGridSizeEventFunction, &func)
2300
2301 #define wxGridRangeSelectEventHandler(func) \
2302 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxGridRangeSelectEventFunction, &func)
2303
2304 #define wxGridEditorCreatedEventHandler(func) \
2305 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxGridEditorCreatedEventFunction, &func)
2306
2307 #define wx__DECLARE_GRIDEVT(evt, id, fn) \
2308 wx__DECLARE_EVT1(wxEVT_GRID_ ## evt, id, wxGridEventHandler(fn))
2309
2310 #define wx__DECLARE_GRIDSIZEEVT(evt, id, fn) \
2311 wx__DECLARE_EVT1(wxEVT_GRID_ ## evt, id, wxGridSizeEventHandler(fn))
2312
2313 #define wx__DECLARE_GRIDRANGESELEVT(evt, id, fn) \
2314 wx__DECLARE_EVT1(wxEVT_GRID_ ## evt, id, wxGridRangeSelectEventHandler(fn))
2315
2316 #define wx__DECLARE_GRIDEDITOREVT(evt, id, fn) \
2317 wx__DECLARE_EVT1(wxEVT_GRID_ ## evt, id, wxGridEditorCreatedEventHandler(fn))
2318
2319 #define EVT_GRID_CMD_CELL_LEFT_CLICK(id, fn) wx__DECLARE_GRIDEVT(CELL_LEFT_CLICK, id, fn)
2320 #define EVT_GRID_CMD_CELL_RIGHT_CLICK(id, fn) wx__DECLARE_GRIDEVT(CELL_RIGHT_CLICK, id, fn)
2321 #define EVT_GRID_CMD_CELL_LEFT_DCLICK(id, fn) wx__DECLARE_GRIDEVT(CELL_LEFT_DCLICK, id, fn)
2322 #define EVT_GRID_CMD_CELL_RIGHT_DCLICK(id, fn) wx__DECLARE_GRIDEVT(CELL_RIGHT_DCLICK, id, fn)
2323 #define EVT_GRID_CMD_LABEL_LEFT_CLICK(id, fn) wx__DECLARE_GRIDEVT(LABEL_LEFT_CLICK, id, fn)
2324 #define EVT_GRID_CMD_LABEL_RIGHT_CLICK(id, fn) wx__DECLARE_GRIDEVT(LABEL_RIGHT_CLICK, id, fn)
2325 #define EVT_GRID_CMD_LABEL_LEFT_DCLICK(id, fn) wx__DECLARE_GRIDEVT(LABEL_LEFT_DCLICK, id, fn)
2326 #define EVT_GRID_CMD_LABEL_RIGHT_DCLICK(id, fn) wx__DECLARE_GRIDEVT(LABEL_RIGHT_DCLICK, id, fn)
2327 #define EVT_GRID_CMD_ROW_SIZE(id, fn) wx__DECLARE_GRIDSIZEEVT(ROW_SIZE, id, fn)
2328 #define EVT_GRID_CMD_COL_SIZE(id, fn) wx__DECLARE_GRIDSIZEEVT(COL_SIZE, id, fn)
2329 #define EVT_GRID_CMD_COL_MOVE(id, fn) wx__DECLARE_GRIDEVT(COL_MOVE, id, fn)
2330 #define EVT_GRID_CMD_RANGE_SELECT(id, fn) wx__DECLARE_GRIDRANGESELEVT(RANGE_SELECT, id, fn)
2331 #define EVT_GRID_CMD_CELL_CHANGE(id, fn) wx__DECLARE_GRIDEVT(CELL_CHANGE, id, fn)
2332 #define EVT_GRID_CMD_SELECT_CELL(id, fn) wx__DECLARE_GRIDEVT(SELECT_CELL, id, fn)
2333 #define EVT_GRID_CMD_EDITOR_SHOWN(id, fn) wx__DECLARE_GRIDEVT(EDITOR_SHOWN, id, fn)
2334 #define EVT_GRID_CMD_EDITOR_HIDDEN(id, fn) wx__DECLARE_GRIDEVT(EDITOR_HIDDEN, id, fn)
2335 #define EVT_GRID_CMD_EDITOR_CREATED(id, fn) wx__DECLARE_GRIDEDITOREVT(EDITOR_CREATED, id, fn)
2336 #define EVT_GRID_CMD_CELL_BEGIN_DRAG(id, fn) wx__DECLARE_GRIDEVT(CELL_BEGIN_DRAG, id, fn)
2337
2338 // same as above but for any id (exists mainly for backwards compatibility but
2339 // then it's also true that you rarely have multiple grid in the same window)
2340 #define EVT_GRID_CELL_LEFT_CLICK(fn) EVT_GRID_CMD_CELL_LEFT_CLICK(wxID_ANY, fn)
2341 #define EVT_GRID_CELL_RIGHT_CLICK(fn) EVT_GRID_CMD_CELL_RIGHT_CLICK(wxID_ANY, fn)
2342 #define EVT_GRID_CELL_LEFT_DCLICK(fn) EVT_GRID_CMD_CELL_LEFT_DCLICK(wxID_ANY, fn)
2343 #define EVT_GRID_CELL_RIGHT_DCLICK(fn) EVT_GRID_CMD_CELL_RIGHT_DCLICK(wxID_ANY, fn)
2344 #define EVT_GRID_LABEL_LEFT_CLICK(fn) EVT_GRID_CMD_LABEL_LEFT_CLICK(wxID_ANY, fn)
2345 #define EVT_GRID_LABEL_RIGHT_CLICK(fn) EVT_GRID_CMD_LABEL_RIGHT_CLICK(wxID_ANY, fn)
2346 #define EVT_GRID_LABEL_LEFT_DCLICK(fn) EVT_GRID_CMD_LABEL_LEFT_DCLICK(wxID_ANY, fn)
2347 #define EVT_GRID_LABEL_RIGHT_DCLICK(fn) EVT_GRID_CMD_LABEL_RIGHT_DCLICK(wxID_ANY, fn)
2348 #define EVT_GRID_ROW_SIZE(fn) EVT_GRID_CMD_ROW_SIZE(wxID_ANY, fn)
2349 #define EVT_GRID_COL_SIZE(fn) EVT_GRID_CMD_COL_SIZE(wxID_ANY, fn)
2350 #define EVT_GRID_COL_MOVE(fn) EVT_GRID_CMD_COL_MOVE(wxID_ANY, fn)
2351 #define EVT_GRID_RANGE_SELECT(fn) EVT_GRID_CMD_RANGE_SELECT(wxID_ANY, fn)
2352 #define EVT_GRID_CELL_CHANGE(fn) EVT_GRID_CMD_CELL_CHANGE(wxID_ANY, fn)
2353 #define EVT_GRID_SELECT_CELL(fn) EVT_GRID_CMD_SELECT_CELL(wxID_ANY, fn)
2354 #define EVT_GRID_EDITOR_SHOWN(fn) EVT_GRID_CMD_EDITOR_SHOWN(wxID_ANY, fn)
2355 #define EVT_GRID_EDITOR_HIDDEN(fn) EVT_GRID_CMD_EDITOR_HIDDEN(wxID_ANY, fn)
2356 #define EVT_GRID_EDITOR_CREATED(fn) EVT_GRID_CMD_EDITOR_CREATED(wxID_ANY, fn)
2357 #define EVT_GRID_CELL_BEGIN_DRAG(fn) EVT_GRID_CMD_CELL_BEGIN_DRAG(wxID_ANY, fn)
2358
2359 #if 0 // TODO: implement these ? others ?
2360
2361 extern const int wxEVT_GRID_CREATE_CELL;
2362 extern const int wxEVT_GRID_CHANGE_LABELS;
2363 extern const int wxEVT_GRID_CHANGE_SEL_LABEL;
2364
2365 #define EVT_GRID_CREATE_CELL(fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_GRID_CREATE_CELL, wxID_ANY, wxID_ANY, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxGridEventFunction, &fn ), NULL ),
2366 #define EVT_GRID_CHANGE_LABELS(fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_GRID_CHANGE_LABELS, wxID_ANY, wxID_ANY, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxGridEventFunction, &fn ), NULL ),
2367 #define EVT_GRID_CHANGE_SEL_LABEL(fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_GRID_CHANGE_SEL_LABEL, wxID_ANY, wxID_ANY, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxGridEventFunction, &fn ), NULL ),
2368
2369 #endif
2370
2371 #endif // wxUSE_GRID
2372 #endif // _WX_GENERIC_GRID_H_