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