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