fixed setting more than one attr for a cell
[wxWidgets.git] / include / wx / generic / grid.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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
13 #include "wx/defs.h"
14
15 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
16 #include "gridg.h"
17 #else
18
19 #ifndef __WXGRID_H__
20 #define __WXGRID_H__
21
22 #ifdef __GNUG__
23 #pragma interface "grid.h"
24 #endif
25
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/textctrl.h"
32 #include "wx/combobox.h"
33 #include "wx/dynarray.h"
34
35
36 // Default parameters for wxGrid
37 //
38 #define WXGRID_DEFAULT_NUMBER_ROWS 10
39 #define WXGRID_DEFAULT_NUMBER_COLS 10
40 #ifdef __WXMSW__
41 #define WXGRID_DEFAULT_ROW_HEIGHT 25
42 #else
43 #define WXGRID_DEFAULT_ROW_HEIGHT 30
44 #endif // __WXMSW__
45 #define WXGRID_DEFAULT_COL_WIDTH 80
46 #define WXGRID_DEFAULT_COL_LABEL_HEIGHT 32
47 #define WXGRID_DEFAULT_ROW_LABEL_WIDTH 82
48 #define WXGRID_LABEL_EDGE_ZONE 5
49 #define WXGRID_MIN_ROW_HEIGHT 15
50 #define WXGRID_MIN_COL_WIDTH 15
51 #define WXGRID_DEFAULT_SCROLLBAR_WIDTH 16
52
53 // ----------------------------------------------------------------------------
54 // forward declarations
55 // ----------------------------------------------------------------------------
56
57 class WXDLLEXPORT wxGridCellAttrProviderData;
58 class WXDLLEXPORT wxGridRowLabelWindow;
59 class WXDLLEXPORT wxGridColLabelWindow;
60 class WXDLLEXPORT wxGridCornerLabelWindow;
61 class WXDLLEXPORT wxGridWindow;
62 class WXDLLEXPORT wxGrid;
63
64 // ----------------------------------------------------------------------------
65 // wxGridCellAttr: this class can be used to alter the cells appearance in
66 // the grid by changing their colour/font/... from default. An object of this
67 // class may be returned by wxGridTable::GetAttr().
68 // ----------------------------------------------------------------------------
69
70 class WXDLLEXPORT wxGridCellAttr
71 {
72 public:
73 // ctors
74 wxGridCellAttr()
75 {
76 Init();
77 SetAlignment(0, 0);
78 }
79
80 wxGridCellAttr(const wxColour& colText,
81 const wxColour& colBack,
82 const wxFont& font,
83 int hAlign,
84 int vAlign)
85 : m_colText(colText), m_colBack(colBack), m_font(font)
86 {
87 Init();
88 SetAlignment(hAlign, vAlign);
89 }
90
91 // default copy ctor ok
92
93 // this class is ref counted: it is created with ref count of 1, so
94 // calling DecRef() once will delete it. Calling IncRef() allows to lock
95 // it until the matching DecRef() is called
96 void IncRef() { m_nRef++; }
97 void DecRef() { if ( !--m_nRef ) delete this; }
98 void SafeDecRef() { if ( this ) DecRef(); }
99
100 // setters
101 void SetTextColour(const wxColour& colText) { m_colText = colText; }
102 void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
103 void SetFont(const wxFont& font) { m_font = font; }
104 void SetAlignment(int hAlign, int vAlign)
105 {
106 m_hAlign = hAlign;
107 m_vAlign = vAlign;
108 }
109
110 // accessors
111 bool HasTextColour() const { return m_colText.Ok(); }
112 bool HasBackgroundColour() const { return m_colBack.Ok(); }
113 bool HasFont() const { return m_font.Ok(); }
114 bool HasAlignment() const { return m_hAlign || m_vAlign; }
115
116 const wxColour& GetTextColour() const { return m_colText; }
117 const wxColour& GetBackgroundColour() const { return m_colBack; }
118 const wxFont& GetFont() const { return m_font; }
119 void GetAlignment(int *hAlign, int *vAlign)
120 {
121 if ( hAlign ) *hAlign = m_hAlign;
122 if ( vAlign ) *vAlign = m_vAlign;
123 }
124
125 private:
126 // the common part of all ctors
127 void Init() { m_nRef = 1; }
128
129 // the dtor is private because only DecRef() can delete us
130 ~wxGridCellAttr() { }
131
132 // the ref count - when it goes to 0, we die
133 size_t m_nRef;
134
135 wxColour m_colText,
136 m_colBack;
137 wxFont m_font;
138 int m_hAlign,
139 m_vAlign;
140
141 // suppress the stupid gcc warning about the class having private dtor and
142 // no friends
143 friend class wxGridCellAttrDummyFriend;
144 };
145
146 // ----------------------------------------------------------------------------
147 // wxGridCellAttrProvider: class used by wxGridTableBase to retrieve/store the
148 // cell attributes.
149 // ----------------------------------------------------------------------------
150
151 // implementation note: we separate it from wxGridTableBase because we wish to
152 // avoid deriving a new table class if possible, and sometimes it will be
153 // enough to just derive another wxGridCellAttrProvider instead
154
155 class WXDLLEXPORT wxGridCellAttrProvider
156 {
157 public:
158 wxGridCellAttrProvider();
159 virtual ~wxGridCellAttrProvider();
160
161 // DecRef() must be called on the returned pointer
162 virtual wxGridCellAttr *GetAttr(int row, int col) const;
163
164 // takes ownership of the pointer, don't call DecRef() on it
165 virtual void SetAttr(wxGridCellAttr *attr, int row, int col);
166
167 private:
168 void InitData();
169
170 wxGridCellAttrProviderData *m_data;
171 };
172
173 //////////////////////////////////////////////////////////////////////
174 //
175 // Grid table classes
176 //
177 //////////////////////////////////////////////////////////////////////
178
179
180 class WXDLLEXPORT wxGridTableBase : public wxObject
181 {
182 public:
183 wxGridTableBase();
184 virtual ~wxGridTableBase();
185
186 // You must override these functions in a derived table class
187 //
188 virtual long GetNumberRows() = 0;
189 virtual long GetNumberCols() = 0;
190 virtual wxString GetValue( int row, int col ) = 0;
191 virtual void SetValue( int row, int col, const wxString& s ) = 0;
192 virtual bool IsEmptyCell( int row, int col ) = 0;
193
194 // Overriding these is optional
195 //
196 virtual void SetView( wxGrid *grid ) { m_view = grid; }
197 virtual wxGrid * GetView() const { return m_view; }
198
199 virtual void Clear() {}
200 virtual bool InsertRows( size_t pos = 0, size_t numRows = 1 );
201 virtual bool AppendRows( size_t numRows = 1 );
202 virtual bool DeleteRows( size_t pos = 0, size_t numRows = 1 );
203 virtual bool InsertCols( size_t pos = 0, size_t numCols = 1 );
204 virtual bool AppendCols( size_t numCols = 1 );
205 virtual bool DeleteCols( size_t pos = 0, size_t numCols = 1 );
206
207 virtual wxString GetRowLabelValue( int row );
208 virtual wxString GetColLabelValue( int col );
209 virtual void SetRowLabelValue( int WXUNUSED(row), const wxString& ) {}
210 virtual void SetColLabelValue( int WXUNUSED(col), const wxString& ) {}
211
212 // Attribute handling
213 //
214
215 // give us the attr provider to use - we take ownership of the pointer
216 void SetAttrProvider(wxGridCellAttrProvider *attrProvider);
217
218 // get the currently used attr provider (may be NULL)
219 wxGridCellAttrProvider *GetAttrProvider() const { return m_attrProvider; }
220
221 // by default forwarded to wxGridCellAttrProvider if any. May be
222 // overridden to handle attributes directly in this class.
223 virtual wxGridCellAttr *GetAttr( int row, int col );
224
225 // takes ownership of the pointer
226 virtual void SetAttr(wxGridCellAttr *attr, int row, int col );
227
228 private:
229 wxGrid * m_view;
230 wxGridCellAttrProvider *m_attrProvider;
231
232 DECLARE_ABSTRACT_CLASS( wxGridTableBase );
233 };
234
235
236 // ----------------------------------------------------------------------------
237 // wxGridTableMessage
238 // ----------------------------------------------------------------------------
239
240 // IDs for messages sent from grid table to view
241 //
242 enum wxGridTableRequest
243 {
244 wxGRIDTABLE_REQUEST_VIEW_GET_VALUES = 2000,
245 wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES,
246 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
247 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
248 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
249 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
250 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
251 wxGRIDTABLE_NOTIFY_COLS_DELETED
252 };
253
254 class WXDLLEXPORT wxGridTableMessage
255 {
256 public:
257 wxGridTableMessage();
258 wxGridTableMessage( wxGridTableBase *table, int id,
259 int comInt1 = -1,
260 int comInt2 = -1 );
261
262 void SetTableObject( wxGridTableBase *table ) { m_table = table; }
263 wxGridTableBase * GetTableObject() const { return m_table; }
264 void SetId( int id ) { m_id = id; }
265 int GetId() { return m_id; }
266 void SetCommandInt( int comInt1 ) { m_comInt1 = comInt1; }
267 int GetCommandInt() { return m_comInt1; }
268 void SetCommandInt2( int comInt2 ) { m_comInt2 = comInt2; }
269 int GetCommandInt2() { return m_comInt2; }
270
271 private:
272 wxGridTableBase *m_table;
273 int m_id;
274 int m_comInt1;
275 int m_comInt2;
276 };
277
278
279
280 // ------ wxGridStringArray
281 // A 2-dimensional array of strings for data values
282 //
283
284 WX_DECLARE_EXPORTED_OBJARRAY(wxArrayString, wxGridStringArray);
285
286
287
288 // ------ wxGridStringTable
289 //
290 // Simplest type of data table for a grid for small tables of strings
291 // that are stored in memory
292 //
293
294 class WXDLLEXPORT wxGridStringTable : public wxGridTableBase
295 {
296 public:
297 wxGridStringTable();
298 wxGridStringTable( int numRows, int numCols );
299 ~wxGridStringTable();
300
301 // these are pure virtual in wxGridTableBase
302 //
303 long GetNumberRows();
304 long GetNumberCols();
305 wxString GetValue( int row, int col );
306 void SetValue( int row, int col, const wxString& s );
307 bool IsEmptyCell( int row, int col );
308
309 // overridden functions from wxGridTableBase
310 //
311 void Clear();
312 bool InsertRows( size_t pos = 0, size_t numRows = 1 );
313 bool AppendRows( size_t numRows = 1 );
314 bool DeleteRows( size_t pos = 0, size_t numRows = 1 );
315 bool InsertCols( size_t pos = 0, size_t numCols = 1 );
316 bool AppendCols( size_t numCols = 1 );
317 bool DeleteCols( size_t pos = 0, size_t numCols = 1 );
318
319 void SetRowLabelValue( int row, const wxString& );
320 void SetColLabelValue( int col, const wxString& );
321 wxString GetRowLabelValue( int row );
322 wxString GetColLabelValue( int col );
323
324 private:
325 wxGridStringArray m_data;
326
327 // These only get used if you set your own labels, otherwise the
328 // GetRow/ColLabelValue functions return wxGridTableBase defaults
329 //
330 wxArrayString m_rowLabels;
331 wxArrayString m_colLabels;
332
333 DECLARE_DYNAMIC_CLASS( wxGridStringTable )
334 };
335
336
337
338 //////////////////////////////////////////////////////////////////////
339 //
340 // Grid view classes
341 //
342 //////////////////////////////////////////////////////////////////////
343
344 class WXDLLEXPORT wxGridCellCoords
345 {
346 public:
347 wxGridCellCoords() { m_row = m_col = -1; }
348 wxGridCellCoords( int r, int c ) { m_row = r; m_col = c; }
349
350 // default copy ctor is ok
351
352 long GetRow() const { return m_row; }
353 void SetRow( long n ) { m_row = n; }
354 long GetCol() const { return m_col; }
355 void SetCol( long n ) { m_col = n; }
356 void Set( long row, long col ) { m_row = row; m_col = col; }
357
358 wxGridCellCoords& operator=( const wxGridCellCoords& other )
359 {
360 if ( &other != this )
361 {
362 m_row=other.m_row;
363 m_col=other.m_col;
364 }
365 return *this;
366 }
367
368 bool operator==( const wxGridCellCoords& other )
369 {
370 return (m_row == other.m_row && m_col == other.m_col);
371 }
372
373 bool operator!=( const wxGridCellCoords& other )
374 {
375 return (m_row != other.m_row || m_col != other.m_col);
376 }
377
378 bool operator!()
379 {
380 return (m_row == -1 && m_col == -1 );
381 }
382
383 private:
384 long m_row;
385 long m_col;
386 };
387
388
389 // For comparisons...
390 //
391 extern wxGridCellCoords wxGridNoCellCoords;
392 extern wxRect wxGridNoCellRect;
393
394 // An array of cell coords...
395 //
396 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellCoords, wxGridCellCoordsArray);
397
398
399
400 // This set of classes is to provide for the use of different types of
401 // cell edit controls in the grid while avoiding the wx class info
402 // system in deference to wxPython
403
404 class WXDLLEXPORT wxGridTextCtrl : public wxTextCtrl
405 {
406 public:
407 wxGridTextCtrl() {}
408 wxGridTextCtrl( wxWindow *,
409 wxGrid *,
410 bool isCellControl,
411 wxWindowID id,
412 const wxString& value = wxEmptyString,
413 const wxPoint& pos = wxDefaultPosition,
414 const wxSize& size = wxDefaultSize,
415 long style = 0 );
416
417 void SetStartValue( const wxString& );
418 wxString GetStartValue() { return startValue; }
419
420 private:
421 wxGrid *m_grid;
422
423 // TRUE for controls placed over cells,
424 // FALSE for a control on a grid control panel
425 bool m_isCellControl;
426
427 wxString startValue;
428
429 void OnKeyDown( wxKeyEvent& );
430
431 DECLARE_DYNAMIC_CLASS( wxGridTextCtrl )
432 DECLARE_EVENT_TABLE()
433 };
434
435 // ----------------------------------------------------------------------------
436 // wxGrid
437 // ----------------------------------------------------------------------------
438
439 class WXDLLEXPORT wxGrid : public wxScrolledWindow
440 {
441 public:
442 wxGrid()
443 {
444 m_table = (wxGridTableBase *) NULL;
445 m_gridWin = (wxGridWindow *) NULL;
446 m_rowLabelWin = (wxGridRowLabelWindow *) NULL;
447 m_colLabelWin = (wxGridColLabelWindow *) NULL;
448 m_cornerLabelWin = (wxGridCornerLabelWindow *) NULL;
449 m_cellEditCtrl = (wxWindow *) NULL;
450 }
451
452 wxGrid( wxWindow *parent,
453 wxWindowID id,
454 const wxPoint& pos = wxDefaultPosition,
455 const wxSize& size = wxDefaultSize,
456 long style = 0,
457 const wxString& name = wxPanelNameStr );
458
459 ~wxGrid();
460
461 bool CreateGrid( int numRows, int numCols );
462
463
464 // ------ grid dimensions
465 //
466 int GetNumberRows() { return m_numRows; }
467 int GetNumberCols() { return m_numCols; }
468
469
470 // ------ display update functions
471 //
472 void CalcRowLabelsExposed( wxRegion& reg );
473
474 void CalcColLabelsExposed( wxRegion& reg );
475 void CalcCellsExposed( wxRegion& reg );
476
477
478 // ------ event handlers
479 //
480 void ProcessRowLabelMouseEvent( wxMouseEvent& event );
481 void ProcessColLabelMouseEvent( wxMouseEvent& event );
482 void ProcessCornerLabelMouseEvent( wxMouseEvent& event );
483 void ProcessGridCellMouseEvent( wxMouseEvent& event );
484 bool ProcessTableMessage( wxGridTableMessage& );
485
486 void DoEndDragResizeRow();
487 void DoEndDragResizeCol();
488
489 wxGridTableBase * GetTable() const { return m_table; }
490 void SetTable( wxGridTableBase *table ) { m_table = table; }
491
492 void ClearGrid();
493 bool InsertRows( int pos = 0, int numRows = 1, bool updateLabels=TRUE );
494 bool AppendRows( int numRows = 1, bool updateLabels=TRUE );
495 bool DeleteRows( int pos = 0, int numRows = 1, bool updateLabels=TRUE );
496 bool InsertCols( int pos = 0, int numCols = 1, bool updateLabels=TRUE );
497 bool AppendCols( int numCols = 1, bool updateLabels=TRUE );
498 bool DeleteCols( int pos = 0, int numCols = 1, bool updateLabels=TRUE );
499
500 void DrawGridCellArea( wxDC& dc );
501 void DrawCellBorder( wxDC& dc, const wxGridCellCoords& );
502 void DrawAllGridLines( wxDC& dc, const wxRegion & reg );
503 void DrawCell( wxDC& dc, const wxGridCellCoords& );
504 void DrawCellBackground( wxDC& dc, const wxGridCellCoords& );
505 void DrawCellValue( wxDC& dc, const wxGridCellCoords& );
506
507 void DrawRowLabels( wxDC& dc );
508 void DrawRowLabel( wxDC& dc, int row );
509
510 void DrawColLabels( wxDC& dc );
511 void DrawColLabel( wxDC& dc, int col );
512
513
514 // ------ Cell text drawing functions
515 //
516 void DrawTextRectangle( wxDC& dc, const wxString&, const wxRect&,
517 int horizontalAlignment = wxLEFT,
518 int verticalAlignment = wxTOP );
519
520 // Split a string containing newline chararcters into an array of
521 // strings and return the number of lines
522 //
523 void StringToLines( const wxString& value, wxArrayString& lines );
524
525 void GetTextBoxSize( wxDC& dc,
526 wxArrayString& lines,
527 long *width, long *height );
528
529
530 // ------
531 // Code that does a lot of grid modification can be enclosed
532 // between BeginBatch() and EndBatch() calls to avoid screen
533 // flicker
534 //
535 void BeginBatch() { m_batchCount++; }
536 void EndBatch() { if ( m_batchCount > 0 ) m_batchCount--; }
537 int GetBatchCount() { return m_batchCount; }
538
539
540 // ------ edit control functions
541 //
542 bool IsEditable() { return m_editable; }
543 void EnableEditing( bool edit );
544
545 #if 0 // at the moment the cell edit control is always active
546 void EnableCellEditControl( bool enable );
547 #endif
548
549 bool IsCellEditControlEnabled()
550 { return (m_cellEditCtrl && m_cellEditCtrlEnabled); }
551
552 void ShowCellEditControl();
553 void HideCellEditControl();
554 void SetEditControlValue( const wxString& s = wxEmptyString );
555 void SaveEditControlValue();
556
557
558 // ------ grid location functions
559 // Note that all of these functions work with the logical coordinates of
560 // grid cells and labels so you will need to convert from device
561 // coordinates for mouse events etc.
562 //
563 void XYToCell( int x, int y, wxGridCellCoords& );
564 int YToRow( int y );
565 int XToCol( int x );
566
567 int YToEdgeOfRow( int y );
568 int XToEdgeOfCol( int x );
569
570 wxRect CellToRect( int row, int col );
571 wxRect CellToRect( const wxGridCellCoords& coords )
572 { return CellToRect( coords.GetRow(), coords.GetCol() ); }
573
574 int GetGridCursorRow() { return m_currentCellCoords.GetRow(); }
575 int GetGridCursorCol() { return m_currentCellCoords.GetCol(); }
576
577 // check to see if a cell is either wholly visible (the default arg) or
578 // at least partially visible in the grid window
579 //
580 bool IsVisible( int row, int col, bool wholeCellVisible = TRUE );
581 bool IsVisible( const wxGridCellCoords& coords, bool wholeCellVisible = TRUE )
582 { return IsVisible( coords.GetRow(), coords.GetCol(), wholeCellVisible ); }
583 void MakeCellVisible( int row, int col );
584 void MakeCellVisible( const wxGridCellCoords& coords )
585 { MakeCellVisible( coords.GetRow(), coords.GetCol() ); }
586
587
588 // ------ grid cursor movement functions
589 //
590 void SetGridCursor( int row, int col )
591 { SetCurrentCell( wxGridCellCoords(row, col) ); }
592
593 bool MoveCursorUp();
594 bool MoveCursorDown();
595 bool MoveCursorLeft();
596 bool MoveCursorRight();
597 bool MovePageDown();
598 bool MovePageUp();
599 bool MoveCursorUpBlock();
600 bool MoveCursorDownBlock();
601 bool MoveCursorLeftBlock();
602 bool MoveCursorRightBlock();
603
604
605 // ------ label and gridline formatting
606 //
607 int GetDefaultRowLabelSize() { return WXGRID_DEFAULT_ROW_LABEL_WIDTH; }
608 int GetRowLabelSize() { return m_rowLabelWidth; }
609 int GetDefaultColLabelSize() { return WXGRID_DEFAULT_COL_LABEL_HEIGHT; }
610 int GetColLabelSize() { return m_colLabelHeight; }
611 wxColour GetLabelBackgroundColour() { return m_labelBackgroundColour; }
612 wxColour GetLabelTextColour() { return m_labelTextColour; }
613 wxFont GetLabelFont() { return m_labelFont; }
614 void GetRowLabelAlignment( int *horiz, int *vert );
615 void GetColLabelAlignment( int *horiz, int *vert );
616 wxString GetRowLabelValue( int row );
617 wxString GetColLabelValue( int col );
618 wxColour GetGridLineColour() { return m_gridLineColour; }
619
620 void SetRowLabelSize( int width );
621 void SetColLabelSize( int height );
622 void SetLabelBackgroundColour( const wxColour& );
623 void SetLabelTextColour( const wxColour& );
624 void SetLabelFont( const wxFont& );
625 void SetRowLabelAlignment( int horiz, int vert );
626 void SetColLabelAlignment( int horiz, int vert );
627 void SetRowLabelValue( int row, const wxString& );
628 void SetColLabelValue( int col, const wxString& );
629 void SetGridLineColour( const wxColour& );
630
631 void EnableGridLines( bool enable = TRUE );
632 bool GridLinesEnabled() { return m_gridLinesEnabled; }
633
634
635 // ------ row and col formatting
636 //
637 int GetDefaultRowSize();
638 int GetRowSize( int row );
639 int GetDefaultColSize();
640 int GetColSize( int col );
641 wxColour GetDefaultCellBackgroundColour();
642 wxColour GetCellBackgroundColour( int row, int col );
643 wxColour GetDefaultCellTextColour();
644 wxColour GetCellTextColour( int row, int col );
645 wxFont GetDefaultCellFont();
646 wxFont GetCellFont( int row, int col );
647 void GetDefaultCellAlignment( int *horiz, int *vert );
648 void GetCellAlignment( int row, int col, int *horiz, int *vert );
649
650 void SetDefaultRowSize( int height, bool resizeExistingRows = FALSE );
651 void SetRowSize( int row, int height );
652 void SetDefaultColSize( int width, bool resizeExistingCols = FALSE );
653
654 void SetColSize( int col, int width );
655 void SetDefaultCellBackgroundColour( const wxColour& );
656 void SetCellBackgroundColour( int row, int col, const wxColour& );
657 void SetDefaultCellTextColour( const wxColour& );
658
659 void SetCellTextColour( int row, int col, const wxColour& );
660 void SetDefaultCellFont( const wxFont& );
661 void SetCellFont( int row, int col, const wxFont& );
662 void SetDefaultCellAlignment( int horiz, int vert );
663 void SetCellAlignment( int row, int col, int horiz, int vert );
664
665
666 // ------ cell value accessors
667 //
668 wxString GetCellValue( int row, int col )
669 {
670 if ( m_table )
671 {
672 return m_table->GetValue( row, col );
673 }
674 else
675 {
676 return wxEmptyString;
677 }
678 }
679
680 wxString GetCellValue( const wxGridCellCoords& coords )
681 { return GetCellValue( coords.GetRow(), coords.GetCol() ); }
682
683 void SetCellValue( int row, int col, const wxString& s );
684 void SetCellValue( const wxGridCellCoords& coords, const wxString& s )
685 { SetCellValue( coords.GetRow(), coords.GetCol(), s ); }
686
687
688
689 // ------ selections of blocks of cells
690 //
691 void SelectRow( int row, bool addToSelected = FALSE );
692 void SelectCol( int col, bool addToSelected = FALSE );
693
694 void SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol );
695
696 void SelectBlock( const wxGridCellCoords& topLeft,
697 const wxGridCellCoords& bottomRight )
698 { SelectBlock( topLeft.GetRow(), topLeft.GetCol(),
699 bottomRight.GetRow(), bottomRight.GetCol() ); }
700
701 void SelectAll();
702
703 bool IsSelection()
704 { return ( m_selectedTopLeft != wxGridNoCellCoords &&
705 m_selectedBottomRight != wxGridNoCellCoords );
706 }
707
708 void ClearSelection();
709
710 bool IsInSelection( int row, int col )
711 { return ( IsSelection() &&
712 row >= m_selectedTopLeft.GetRow() &&
713 col >= m_selectedTopLeft.GetCol() &&
714 row <= m_selectedBottomRight.GetRow() &&
715 col <= m_selectedBottomRight.GetCol() );
716 }
717
718 bool IsInSelection( const wxGridCellCoords& coords )
719 { return IsInSelection( coords.GetRow(), coords.GetCol() ); }
720
721 void GetSelection( int* topRow, int* leftCol, int* bottomRow, int* rightCol )
722 {
723 // these will all be -1 if there is no selected block
724 //
725 *topRow = m_selectedTopLeft.GetRow();
726 *leftCol = m_selectedTopLeft.GetCol();
727 *bottomRow = m_selectedBottomRight.GetRow();
728 *rightCol = m_selectedBottomRight.GetCol();
729 }
730
731
732 // This function returns the rectangle that encloses the block of cells
733 // limited by TopLeft and BottomRight cell in device coords and clipped
734 // to the client size of the grid window.
735 //
736 wxRect BlockToDeviceRect( const wxGridCellCoords & topLeft,
737 const wxGridCellCoords & bottomRight );
738
739 // This function returns the rectangle that encloses the selected cells
740 // in device coords and clipped to the client size of the grid window.
741 //
742 wxRect SelectionToDeviceRect()
743 {
744 return BlockToDeviceRect( m_selectedTopLeft,
745 m_selectedBottomRight );
746 }
747
748
749 // ------ For compatibility with previous wxGrid only...
750 //
751 // ************************************************
752 // ** Don't use these in new code because they **
753 // ** are liable to disappear in a future **
754 // ** revision **
755 // ************************************************
756 //
757
758 wxGrid( wxWindow *parent,
759 int x = -1, int y = -1, int w = -1, int h = -1,
760 long style = 0,
761 const wxString& name = wxPanelNameStr )
762 : wxScrolledWindow( parent, -1, wxPoint(x,y), wxSize(w,h), style, name )
763 {
764 Create();
765 }
766
767 void SetCellValue( const wxString& val, int row, int col )
768 { SetCellValue( row, col, val ); }
769
770 void UpdateDimensions()
771 { CalcDimensions(); }
772
773 int GetRows() { return GetNumberRows(); }
774 int GetCols() { return GetNumberCols(); }
775 int GetCursorRow() { return GetGridCursorRow(); }
776 int GetCursorColumn() { return GetGridCursorCol(); }
777
778 int GetScrollPosX() { return 0; }
779 int GetScrollPosY() { return 0; }
780
781 void SetScrollX( int x ) { }
782 void SetScrollY( int y ) { }
783
784 void SetColumnWidth( int col, int width )
785 { SetColSize( col, width ); }
786
787 int GetColumnWidth( int col )
788 { return GetColSize( col ); }
789
790 void SetRowHeight( int row, int height )
791 { SetRowSize( row, height ); }
792
793 int GetRowHeight( int row )
794 { return GetRowSize( row ); }
795
796 int GetViewHeight() // returned num whole rows visible
797 { return 0; }
798
799 int GetViewWidth() // returned num whole cols visible
800 { return 0; }
801
802 void SetLabelSize( int orientation, int sz )
803 {
804 if ( orientation == wxHORIZONTAL )
805 SetColLabelSize( sz );
806 else
807 SetRowLabelSize( sz );
808 }
809
810 int GetLabelSize( int orientation )
811 {
812 if ( orientation == wxHORIZONTAL )
813 return GetColLabelSize();
814 else
815 return GetRowLabelSize();
816 }
817
818 void SetLabelAlignment( int orientation, int align )
819 {
820 if ( orientation == wxHORIZONTAL )
821 SetColLabelAlignment( align, -1 );
822 else
823 SetRowLabelAlignment( align, -1 );
824 }
825
826 int GetLabelAlignment( int orientation, int WXUNUSED(align) )
827 {
828 int h, v;
829 if ( orientation == wxHORIZONTAL )
830 {
831 GetColLabelAlignment( &h, &v );
832 return h;
833 }
834 else
835 {
836 GetRowLabelAlignment( &h, &v );
837 return h;
838 }
839 }
840
841 void SetLabelValue( int orientation, const wxString& val, int pos )
842 {
843 if ( orientation == wxHORIZONTAL )
844 SetColLabelValue( pos, val );
845 else
846 SetRowLabelValue( pos, val );
847 }
848
849 wxString GetLabelValue( int orientation, int pos)
850 {
851 if ( orientation == wxHORIZONTAL )
852 return GetColLabelValue( pos );
853 else
854 return GetRowLabelValue( pos );
855 }
856
857 wxFont GetCellTextFont() const
858 { return m_defaultCellFont; }
859
860 wxFont GetCellTextFont(int WXUNUSED(row), int WXUNUSED(col)) const
861 { return m_defaultCellFont; }
862
863 void SetCellTextFont(const wxFont& fnt)
864 { SetDefaultCellFont( fnt ); }
865
866 void SetCellTextFont(const wxFont& fnt, int row, int col)
867 { SetCellFont( row, col, fnt ); }
868
869 void SetCellTextColour(const wxColour& val, int row, int col)
870 { SetCellTextColour( row, col, val ); }
871
872 void SetCellTextColour(const wxColour& col)
873 { SetDefaultCellTextColour( col ); }
874
875 void SetCellBackgroundColour(const wxColour& col)
876 { SetDefaultCellBackgroundColour( col ); }
877
878 void SetCellBackgroundColour(const wxColour& colour, int row, int col)
879 { SetCellBackgroundColour( row, col, colour ); }
880
881 bool GetEditable() { return IsEditable(); }
882 void SetEditable( bool edit = TRUE ) { EnableEditing( edit ); }
883 bool GetEditInPlace() { return IsCellEditControlEnabled(); }
884
885 void SetEditInPlace(bool edit = TRUE) { }
886
887 void SetCellAlignment( int align, int row, int col)
888 { SetCellAlignment(row, col, align, wxCENTER); }
889 void SetCellAlignment( int WXUNUSED(align) ) {}
890 void SetCellBitmap(wxBitmap *WXUNUSED(bitmap), int WXUNUSED(row), int WXUNUSED(col))
891 { }
892 void SetDividerPen(const wxPen& WXUNUSED(pen)) { }
893 wxPen& GetDividerPen() const { return wxNullPen; }
894 void OnActivate(bool WXUNUSED(active)) {}
895
896 // ******** End of compatibility functions **********
897
898
899
900 // ------ control IDs
901 enum { wxGRID_CELLCTRL = 2000,
902 wxGRID_TOPCTRL };
903
904 // ------ control types
905 enum { wxGRID_TEXTCTRL = 2100,
906 wxGRID_CHECKBOX,
907 wxGRID_CHOICE,
908 wxGRID_COMBOBOX };
909
910 protected:
911 bool m_created;
912 bool m_displayed;
913
914 wxGridWindow *m_gridWin;
915 wxGridRowLabelWindow *m_rowLabelWin;
916 wxGridColLabelWindow *m_colLabelWin;
917 wxGridCornerLabelWindow *m_cornerLabelWin;
918
919 wxGridTableBase *m_table;
920
921 int m_left;
922 int m_top;
923 int m_right;
924 int m_bottom;
925
926 int m_numRows;
927 int m_numCols;
928
929 wxGridCellCoords m_currentCellCoords;
930
931 wxGridCellCoords m_selectedTopLeft;
932 wxGridCellCoords m_selectedBottomRight;
933
934 int m_defaultRowHeight;
935 wxArrayInt m_rowHeights;
936 wxArrayInt m_rowBottoms;
937
938 int m_defaultColWidth;
939 wxArrayInt m_colWidths;
940 wxArrayInt m_colRights;
941
942 int m_rowLabelWidth;
943 int m_colLabelHeight;
944
945 wxColour m_labelBackgroundColour;
946 wxColour m_labelTextColour;
947 wxFont m_labelFont;
948
949 int m_rowLabelHorizAlign;
950 int m_rowLabelVertAlign;
951 int m_colLabelHorizAlign;
952 int m_colLabelVertAlign;
953
954 bool m_defaultRowLabelValues;
955 bool m_defaultColLabelValues;
956
957 wxColour m_gridLineColour;
958 bool m_gridLinesEnabled;
959
960 // default cell attributes
961 wxFont m_defaultCellFont;
962 int m_defaultCellHAlign,
963 m_defaultCellVAlign;
964
965 // do we have some place to store attributes in?
966 bool CanHaveAttributes();
967
968 // returns the attribute we may modify in place: a new one if this cell
969 // doesn't have any yet or the existing one if it does
970 //
971 // DecRef() must be called on the returned pointer, as usual
972 wxGridCellAttr *GetCellAttr(int row, int col) const;
973
974 wxGridCellCoordsArray m_cellsExposed;
975 wxArrayInt m_rowsExposed;
976 wxArrayInt m_colsExposed;
977 wxArrayInt m_rowLabelsExposed;
978 wxArrayInt m_colLabelsExposed;
979
980 bool m_inOnKeyDown;
981 int m_batchCount;
982
983 enum CursorMode
984 {
985 WXGRID_CURSOR_SELECT_CELL,
986 WXGRID_CURSOR_RESIZE_ROW,
987 WXGRID_CURSOR_RESIZE_COL,
988 WXGRID_CURSOR_SELECT_ROW,
989 WXGRID_CURSOR_SELECT_COL
990 };
991
992 // this method not only sets m_cursorMode but also sets the correct cursor
993 // for the given mode and, if captureMouse is not FALSE releases the mouse
994 // if it was captured and captures it if it must be captured
995 //
996 // for this to work, you should always use it and not set m_cursorMode
997 // directly!
998 void ChangeCursorMode(CursorMode mode,
999 wxWindow *win = (wxWindow *)NULL,
1000 bool captureMouse = TRUE);
1001
1002 wxWindow *m_winCapture; // the window which captured the mouse
1003 CursorMode m_cursorMode;
1004
1005 int m_dragLastPos;
1006 int m_dragRowOrCol;
1007 bool m_isDragging;
1008
1009 wxGridCellCoords m_selectionStart;
1010
1011 wxCursor m_rowResizeCursor;
1012 wxCursor m_colResizeCursor;
1013
1014 bool m_editable; // applies to whole grid
1015 int m_editCtrlType; // for current cell
1016 wxWindow* m_cellEditCtrl;
1017 bool m_cellEditCtrlEnabled;
1018
1019
1020 void Create();
1021 void Init();
1022 void CalcDimensions();
1023 void CalcWindowSizes();
1024 bool Redimension( wxGridTableMessage& );
1025
1026
1027 bool SendEvent( const wxEventType,
1028 int row, int col,
1029 wxMouseEvent& );
1030
1031 bool SendEvent( const wxEventType,
1032 int row, int col );
1033
1034
1035 void OnPaint( wxPaintEvent& );
1036 void OnSize( wxSizeEvent& );
1037 void OnKeyDown( wxKeyEvent& );
1038
1039
1040 void SetCurrentCell( const wxGridCellCoords& coords );
1041 void SetCurrentCell( int row, int col )
1042 { SetCurrentCell( wxGridCellCoords(row, col) ); }
1043
1044
1045 // ------ functions to get/send data (see also public functions)
1046 //
1047 bool GetModelValues();
1048 bool SetModelValues();
1049
1050
1051 DECLARE_DYNAMIC_CLASS( wxGrid )
1052 DECLARE_EVENT_TABLE()
1053 };
1054
1055
1056
1057
1058
1059 //
1060 // ------ Grid event class and event types
1061 //
1062
1063 class WXDLLEXPORT wxGridEvent : public wxNotifyEvent
1064 {
1065 public:
1066 wxGridEvent()
1067 : wxNotifyEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
1068 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1069 {
1070 }
1071
1072 wxGridEvent(int id, wxEventType type, wxObject* obj,
1073 int row=-1, int col=-1, int x=-1, int y=-1,
1074 bool control=FALSE, bool shift=FALSE, bool alt=FALSE, bool meta=FALSE);
1075
1076 virtual int GetRow() { return m_row; }
1077 virtual int GetCol() { return m_col; }
1078 wxPoint GetPosition() { return wxPoint( m_x, m_y ); }
1079 bool ControlDown() { return m_control; }
1080 bool MetaDown() { return m_meta; }
1081 bool ShiftDown() { return m_shift; }
1082 bool AltDown() { return m_alt; }
1083
1084 protected:
1085 int m_row;
1086 int m_col;
1087 int m_x;
1088 int m_y;
1089 bool m_control;
1090 bool m_meta;
1091 bool m_shift;
1092 bool m_alt;
1093
1094 DECLARE_DYNAMIC_CLASS(wxGridEvent)
1095 };
1096
1097
1098 class WXDLLEXPORT wxGridSizeEvent : public wxNotifyEvent
1099 {
1100 public:
1101 wxGridSizeEvent()
1102 : wxNotifyEvent(), m_rowOrCol(-1), m_x(-1), m_y(-1),
1103 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1104 {
1105 }
1106
1107 wxGridSizeEvent(int id, wxEventType type, wxObject* obj,
1108 int rowOrCol=-1, int x=-1, int y=-1,
1109 bool control=FALSE, bool shift=FALSE, bool alt=FALSE, bool meta=FALSE);
1110
1111 int GetRowOrCol() { return m_rowOrCol; }
1112 wxPoint GetPosition() { return wxPoint( m_x, m_y ); }
1113 bool ControlDown() { return m_control; }
1114 bool MetaDown() { return m_meta; }
1115 bool ShiftDown() { return m_shift; }
1116 bool AltDown() { return m_alt; }
1117
1118 protected:
1119 int m_rowOrCol;
1120 int m_x;
1121 int m_y;
1122 bool m_control;
1123 bool m_meta;
1124 bool m_shift;
1125 bool m_alt;
1126
1127 DECLARE_DYNAMIC_CLASS(wxGridSizeEvent)
1128 };
1129
1130
1131 class WXDLLEXPORT wxGridRangeSelectEvent : public wxNotifyEvent
1132 {
1133 public:
1134 wxGridRangeSelectEvent()
1135 : wxNotifyEvent()
1136 {
1137 m_topLeft = wxGridNoCellCoords;
1138 m_bottomRight = wxGridNoCellCoords;
1139 m_control = FALSE;
1140 m_meta = FALSE;
1141 m_shift = FALSE;
1142 m_alt = FALSE;
1143 }
1144
1145 wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
1146 const wxGridCellCoords& topLeft,
1147 const wxGridCellCoords& bottomRight,
1148 bool control=FALSE, bool shift=FALSE,
1149 bool alt=FALSE, bool meta=FALSE);
1150
1151 wxGridCellCoords GetTopLeftCoords() { return m_topLeft; }
1152 wxGridCellCoords GetBottomRightCoords() { return m_bottomRight; }
1153 int GetTopRow() { return m_topLeft.GetRow(); }
1154 int GetBottomRow() { return m_bottomRight.GetRow(); }
1155 int GetLeftCol() { return m_topLeft.GetCol(); }
1156 int GetRightCol() { return m_bottomRight.GetCol(); }
1157 bool ControlDown() { return m_control; }
1158 bool MetaDown() { return m_meta; }
1159 bool ShiftDown() { return m_shift; }
1160 bool AltDown() { return m_alt; }
1161
1162 protected:
1163 wxGridCellCoords m_topLeft;
1164 wxGridCellCoords m_bottomRight;
1165 bool m_control;
1166 bool m_meta;
1167 bool m_shift;
1168 bool m_alt;
1169
1170 DECLARE_DYNAMIC_CLASS(wxGridRangeSelectEvent)
1171 };
1172
1173
1174 const wxEventType EVT_GRID_CELL_LEFT_CLICK = wxEVT_FIRST + 1580;
1175 const wxEventType EVT_GRID_CELL_RIGHT_CLICK = wxEVT_FIRST + 1581;
1176 const wxEventType EVT_GRID_CELL_LEFT_DCLICK = wxEVT_FIRST + 1582;
1177 const wxEventType EVT_GRID_CELL_RIGHT_DCLICK = wxEVT_FIRST + 1583;
1178 const wxEventType EVT_GRID_LABEL_LEFT_CLICK = wxEVT_FIRST + 1584;
1179 const wxEventType EVT_GRID_LABEL_RIGHT_CLICK = wxEVT_FIRST + 1585;
1180 const wxEventType EVT_GRID_LABEL_LEFT_DCLICK = wxEVT_FIRST + 1586;
1181 const wxEventType EVT_GRID_LABEL_RIGHT_DCLICK = wxEVT_FIRST + 1587;
1182 const wxEventType EVT_GRID_ROW_SIZE = wxEVT_FIRST + 1588;
1183 const wxEventType EVT_GRID_COL_SIZE = wxEVT_FIRST + 1589;
1184 const wxEventType EVT_GRID_RANGE_SELECT = wxEVT_FIRST + 1590;
1185 const wxEventType EVT_GRID_CELL_CHANGE = wxEVT_FIRST + 1591;
1186 const wxEventType EVT_GRID_SELECT_CELL = wxEVT_FIRST + 1592;
1187
1188
1189 typedef void (wxEvtHandler::*wxGridEventFunction)(wxGridEvent&);
1190 typedef void (wxEvtHandler::*wxGridSizeEventFunction)(wxGridSizeEvent&);
1191 typedef void (wxEvtHandler::*wxGridRangeSelectEventFunction)(wxGridRangeSelectEvent&);
1192
1193 #define EVT_GRID_CELL_LEFT_CLICK(fn) { EVT_GRID_CELL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1194 #define EVT_GRID_CELL_RIGHT_CLICK(fn) { EVT_GRID_CELL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1195 #define EVT_GRID_CELL_LEFT_DCLICK(fn) { EVT_GRID_CELL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1196 #define EVT_GRID_CELL_RIGHT_DCLICK(fn) { EVT_GRID_CELL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1197 #define EVT_GRID_LABEL_LEFT_CLICK(fn) { EVT_GRID_LABEL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1198 #define EVT_GRID_LABEL_RIGHT_CLICK(fn) { EVT_GRID_LABEL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1199 #define EVT_GRID_LABEL_LEFT_DCLICK(fn) { EVT_GRID_LABEL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1200 #define EVT_GRID_LABEL_RIGHT_DCLICK(fn) { EVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1201 #define EVT_GRID_ROW_SIZE(fn) { EVT_GRID_ROW_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1202 #define EVT_GRID_COL_SIZE(fn) { EVT_GRID_COL_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1203 #define EVT_GRID_RANGE_SELECT(fn) { EVT_GRID_RANGE_SELECT, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridRangeSelectEventFunction) &fn, NULL },
1204 #define EVT_GRID_CELL_CHANGE(fn) { EVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1205 #define EVT_GRID_SELECT_CELL(fn) { EVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1206
1207
1208 #if 0 // TODO: implement these ? others ?
1209
1210 const wxEventType EVT_GRID_CREATE_CELL = wxEVT_FIRST + 1576;
1211 const wxEventType EVT_GRID_CHANGE_LABELS = wxEVT_FIRST + 1577;
1212 const wxEventType EVT_GRID_CHANGE_SEL_LABEL = wxEVT_FIRST + 1578;
1213
1214 #define EVT_GRID_CREATE_CELL(fn) { EVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1215 #define EVT_GRID_CHANGE_LABELS(fn) { EVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1216 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { EVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1217
1218 #endif
1219
1220 #endif // #ifndef __WXGRID_H__
1221
1222 #endif // ifndef wxUSE_NEW_GRID
1223