]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/grid.h
65ed902b90779f1eb26a8917e4fa4683e51a9194
[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
462 wxGridTableBase * GetTable() const { return m_table; }
463 void SetTable( wxGridTableBase *table ) { m_table = table; }
464
465 void ClearGrid();
466 bool InsertRows( int pos = 0, int numRows = 1, bool updateLabels=TRUE );
467 bool AppendRows( int numRows = 1, bool updateLabels=TRUE );
468 bool DeleteRows( int pos = 0, int numRows = 1, bool updateLabels=TRUE );
469 bool InsertCols( int pos = 0, int numCols = 1, bool updateLabels=TRUE );
470 bool AppendCols( int numCols = 1, bool updateLabels=TRUE );
471 bool DeleteCols( int pos = 0, int numCols = 1, bool updateLabels=TRUE );
472
473 void DrawGridCellArea( wxDC& dc );
474 void DrawCellBorder( wxDC& dc, const wxGridCellCoords& );
475 void DrawAllGridLines( wxDC& dc, const wxRegion & reg );
476 void DrawCell( wxDC& dc, const wxGridCellCoords& );
477 void DrawCellBackground( wxDC& dc, const wxGridCellCoords& );
478 void DrawCellValue( wxDC& dc, const wxGridCellCoords& );
479
480 void DrawRowLabels( wxDC& dc );
481 void DrawRowLabel( wxDC& dc, int row );
482
483 void DrawColLabels( wxDC& dc );
484 void DrawColLabel( wxDC& dc, int col );
485
486
487 // ------ Cell text drawing functions
488 //
489 void DrawTextRectangle( wxDC& dc, const wxString&, const wxRect&,
490 int horizontalAlignment = wxLEFT,
491 int verticalAlignment = wxTOP );
492
493 // Split a string containing newline chararcters into an array of
494 // strings and return the number of lines
495 //
496 void StringToLines( const wxString& value, wxArrayString& lines );
497
498 void GetTextBoxSize( wxDC& dc,
499 wxArrayString& lines,
500 long *width, long *height );
501
502
503 // ------
504 // Code that does a lot of grid modification can be enclosed
505 // between BeginBatch() and EndBatch() calls to avoid screen
506 // flicker
507 //
508 void BeginBatch() { m_batchCount++; }
509 void EndBatch() { if ( m_batchCount > 0 ) m_batchCount--; }
510 int GetBatchCount() { return m_batchCount; }
511
512
513 // ------ edit control functions
514 //
515 bool IsEditable() { return m_editable; }
516 void EnableEditing( bool edit );
517
518 #if 0 // at the moment the cell edit control is always active
519 void EnableCellEditControl( bool enable );
520 #endif
521
522 bool IsCellEditControlEnabled()
523 { return (m_cellEditCtrl && m_cellEditCtrlEnabled); }
524
525 void ShowCellEditControl();
526 void HideCellEditControl();
527 void SetEditControlValue( const wxString& s = wxEmptyString );
528 void SaveEditControlValue();
529
530
531 // ------ grid location functions
532 // Note that all of these functions work with the logical coordinates of
533 // grid cells and labels so you will need to convert from device
534 // coordinates for mouse events etc.
535 //
536 void XYToCell( int x, int y, wxGridCellCoords& );
537 int YToRow( int y );
538 int XToCol( int x );
539
540 int YToEdgeOfRow( int y );
541 int XToEdgeOfCol( int x );
542
543 wxRect CellToRect( int row, int col );
544 wxRect CellToRect( const wxGridCellCoords& coords )
545 { return CellToRect( coords.GetRow(), coords.GetCol() ); }
546
547 int GetGridCursorRow() { return m_currentCellCoords.GetRow(); }
548 int GetGridCursorCol() { return m_currentCellCoords.GetCol(); }
549
550 // check to see if a cell is either wholly visible (the default arg) or
551 // at least partially visible in the grid window
552 //
553 bool IsVisible( int row, int col, bool wholeCellVisible = TRUE );
554 bool IsVisible( const wxGridCellCoords& coords, bool wholeCellVisible = TRUE )
555 { return IsVisible( coords.GetRow(), coords.GetCol(), wholeCellVisible ); }
556 void MakeCellVisible( int row, int col );
557 void MakeCellVisible( const wxGridCellCoords& coords )
558 { MakeCellVisible( coords.GetRow(), coords.GetCol() ); }
559
560
561 // ------ grid cursor movement functions
562 //
563 void SetGridCursor( int row, int col )
564 { SetCurrentCell( wxGridCellCoords(row, col) ); }
565
566 bool MoveCursorUp();
567 bool MoveCursorDown();
568 bool MoveCursorLeft();
569 bool MoveCursorRight();
570 bool MovePageDown();
571 bool MovePageUp();
572 bool MoveCursorUpBlock();
573 bool MoveCursorDownBlock();
574 bool MoveCursorLeftBlock();
575 bool MoveCursorRightBlock();
576
577
578 // ------ label and gridline formatting
579 //
580 int GetDefaultRowLabelSize() { return WXGRID_DEFAULT_ROW_LABEL_WIDTH; }
581 int GetRowLabelSize() { return m_rowLabelWidth; }
582 int GetDefaultColLabelSize() { return WXGRID_DEFAULT_COL_LABEL_HEIGHT; }
583 int GetColLabelSize() { return m_colLabelHeight; }
584 wxColour GetLabelBackgroundColour() { return m_labelBackgroundColour; }
585 wxColour GetLabelTextColour() { return m_labelTextColour; }
586 wxFont GetLabelFont() { return m_labelFont; }
587 void GetRowLabelAlignment( int *horiz, int *vert );
588 void GetColLabelAlignment( int *horiz, int *vert );
589 wxString GetRowLabelValue( int row );
590 wxString GetColLabelValue( int col );
591 wxColour GetGridLineColour() { return m_gridLineColour; }
592
593 void SetRowLabelSize( int width );
594 void SetColLabelSize( int height );
595 void SetLabelBackgroundColour( const wxColour& );
596 void SetLabelTextColour( const wxColour& );
597 void SetLabelFont( const wxFont& );
598 void SetRowLabelAlignment( int horiz, int vert );
599 void SetColLabelAlignment( int horiz, int vert );
600 void SetRowLabelValue( int row, const wxString& );
601 void SetColLabelValue( int col, const wxString& );
602 void SetGridLineColour( const wxColour& );
603
604 void EnableGridLines( bool enable = TRUE );
605 bool GridLinesEnabled() { return m_gridLinesEnabled; }
606
607
608 // ------ row and col formatting
609 //
610 int GetDefaultRowSize();
611 int GetRowSize( int row );
612 int GetDefaultColSize();
613 int GetColSize( int col );
614 wxColour GetDefaultCellBackgroundColour();
615 wxColour GetCellBackgroundColour( int row, int col );
616 wxColour GetDefaultCellTextColour();
617 wxColour GetCellTextColour( int row, int col );
618 wxFont GetDefaultCellFont();
619 wxFont GetCellFont( int row, int col );
620 void GetDefaultCellAlignment( int *horiz, int *vert );
621 void GetCellAlignment( int row, int col, int *horiz, int *vert );
622
623 void SetDefaultRowSize( int height, bool resizeExistingRows = FALSE );
624 void SetRowSize( int row, int height );
625 void SetDefaultColSize( int width, bool resizeExistingCols = FALSE );
626
627 void SetColSize( int col, int width );
628 void SetDefaultCellBackgroundColour( const wxColour& );
629 void SetCellBackgroundColour( int row, int col, const wxColour& );
630 void SetDefaultCellTextColour( const wxColour& );
631
632 void SetCellTextColour( int row, int col, const wxColour& );
633 void SetDefaultCellFont( const wxFont& );
634 void SetCellFont( int row, int col, const wxFont& );
635 void SetDefaultCellAlignment( int horiz, int vert );
636 void SetCellAlignment( int row, int col, int horiz, int vert );
637
638
639 // ------ cell value accessors
640 //
641 wxString GetCellValue( int row, int col )
642 {
643 if ( m_table )
644 {
645 return m_table->GetValue( row, col );
646 }
647 else
648 {
649 return wxEmptyString;
650 }
651 }
652
653 wxString GetCellValue( const wxGridCellCoords& coords )
654 { return GetCellValue( coords.GetRow(), coords.GetCol() ); }
655
656 void SetCellValue( int row, int col, const wxString& s );
657 void SetCellValue( const wxGridCellCoords& coords, const wxString& s )
658 { SetCellValue( coords.GetRow(), coords.GetCol(), s ); }
659
660
661
662 // ------ selections of blocks of cells
663 //
664 void SelectRow( int row, bool addToSelected = FALSE );
665 void SelectCol( int col, bool addToSelected = FALSE );
666
667 void SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol );
668
669 void SelectBlock( const wxGridCellCoords& topLeft,
670 const wxGridCellCoords& bottomRight )
671 { SelectBlock( topLeft.GetRow(), topLeft.GetCol(),
672 bottomRight.GetRow(), bottomRight.GetCol() ); }
673
674 void SelectAll();
675
676 bool IsSelection()
677 { return ( m_selectedTopLeft != wxGridNoCellCoords &&
678 m_selectedBottomRight != wxGridNoCellCoords );
679 }
680
681 void ClearSelection();
682
683 bool IsInSelection( int row, int col )
684 { return ( IsSelection() &&
685 row >= m_selectedTopLeft.GetRow() &&
686 col >= m_selectedTopLeft.GetCol() &&
687 row <= m_selectedBottomRight.GetRow() &&
688 col <= m_selectedBottomRight.GetCol() );
689 }
690
691 bool IsInSelection( const wxGridCellCoords& coords )
692 { return IsInSelection( coords.GetRow(), coords.GetCol() ); }
693
694 void GetSelection( int* topRow, int* leftCol, int* bottomRow, int* rightCol )
695 {
696 // these will all be -1 if there is no selected block
697 //
698 *topRow = m_selectedTopLeft.GetRow();
699 *leftCol = m_selectedTopLeft.GetCol();
700 *bottomRow = m_selectedBottomRight.GetRow();
701 *rightCol = m_selectedBottomRight.GetCol();
702 }
703
704
705 // This function returns the rectangle that encloses the block of cells
706 // limited by TopLeft and BottomRight cell in device coords and clipped
707 // to the client size of the grid window.
708 //
709 wxRect BlockToDeviceRect( const wxGridCellCoords & topLeft,
710 const wxGridCellCoords & bottomRight );
711
712 // This function returns the rectangle that encloses the selected cells
713 // in device coords and clipped to the client size of the grid window.
714 //
715 wxRect SelectionToDeviceRect()
716 {
717 return BlockToDeviceRect( m_selectedTopLeft,
718 m_selectedBottomRight );
719 }
720
721
722 // ------ For compatibility with previous wxGrid only...
723 //
724 // ************************************************
725 // ** Don't use these in new code because they **
726 // ** are liable to disappear in a future **
727 // ** revision **
728 // ************************************************
729 //
730
731 wxGrid( wxWindow *parent,
732 int x = -1, int y = -1, int w = -1, int h = -1,
733 long style = 0,
734 const wxString& name = wxPanelNameStr )
735 : wxScrolledWindow( parent, -1, wxPoint(x,y), wxSize(w,h), style, name )
736 {
737 Create();
738 }
739
740 void SetCellValue( const wxString& val, int row, int col )
741 { SetCellValue( row, col, val ); }
742
743 void UpdateDimensions()
744 { CalcDimensions(); }
745
746 int GetRows() { return GetNumberRows(); }
747 int GetCols() { return GetNumberCols(); }
748 int GetCursorRow() { return GetGridCursorRow(); }
749 int GetCursorColumn() { return GetGridCursorCol(); }
750
751 int GetScrollPosX() { return 0; }
752 int GetScrollPosY() { return 0; }
753
754 void SetScrollX( int x ) { }
755 void SetScrollY( int y ) { }
756
757 void SetColumnWidth( int col, int width )
758 { SetColSize( col, width ); }
759
760 int GetColumnWidth( int col )
761 { return GetColSize( col ); }
762
763 void SetRowHeight( int row, int height )
764 { SetRowSize( row, height ); }
765
766 int GetRowHeight( int row )
767 { return GetRowSize( row ); }
768
769 int GetViewHeight() // returned num whole rows visible
770 { return 0; }
771
772 int GetViewWidth() // returned num whole cols visible
773 { return 0; }
774
775 void SetLabelSize( int orientation, int sz )
776 {
777 if ( orientation == wxHORIZONTAL )
778 SetColLabelSize( sz );
779 else
780 SetRowLabelSize( sz );
781 }
782
783 int GetLabelSize( int orientation )
784 {
785 if ( orientation == wxHORIZONTAL )
786 return GetColLabelSize();
787 else
788 return GetRowLabelSize();
789 }
790
791 void SetLabelAlignment( int orientation, int align )
792 {
793 if ( orientation == wxHORIZONTAL )
794 SetColLabelAlignment( align, -1 );
795 else
796 SetRowLabelAlignment( align, -1 );
797 }
798
799 int GetLabelAlignment( int orientation, int WXUNUSED(align) )
800 {
801 int h, v;
802 if ( orientation == wxHORIZONTAL )
803 {
804 GetColLabelAlignment( &h, &v );
805 return h;
806 }
807 else
808 {
809 GetRowLabelAlignment( &h, &v );
810 return h;
811 }
812 }
813
814 void SetLabelValue( int orientation, const wxString& val, int pos )
815 {
816 if ( orientation == wxHORIZONTAL )
817 SetColLabelValue( pos, val );
818 else
819 SetRowLabelValue( pos, val );
820 }
821
822 wxString GetLabelValue( int orientation, int pos)
823 {
824 if ( orientation == wxHORIZONTAL )
825 return GetColLabelValue( pos );
826 else
827 return GetRowLabelValue( pos );
828 }
829
830 wxFont GetCellTextFont() const
831 { return m_defaultCellFont; }
832
833 wxFont GetCellTextFont(int WXUNUSED(row), int WXUNUSED(col)) const
834 { return m_defaultCellFont; }
835
836 void SetCellTextFont(const wxFont& fnt)
837 { SetDefaultCellFont( fnt ); }
838
839 void SetCellTextFont(const wxFont& fnt, int row, int col)
840 { SetCellFont( row, col, fnt ); }
841
842 void SetCellTextColour(const wxColour& val, int row, int col)
843 { SetCellTextColour( row, col, val ); }
844
845 void SetCellTextColour(const wxColour& col)
846 { SetDefaultCellTextColour( col ); }
847
848 void SetCellBackgroundColour(const wxColour& col)
849 { SetDefaultCellBackgroundColour( col ); }
850
851 void SetCellBackgroundColour(const wxColour& colour, int row, int col)
852 { SetCellBackgroundColour( row, col, colour ); }
853
854 bool GetEditable() { return IsEditable(); }
855 void SetEditable( bool edit = TRUE ) { EnableEditing( edit ); }
856 bool GetEditInPlace() { return IsCellEditControlEnabled(); }
857
858 void SetEditInPlace(bool edit = TRUE) { }
859
860 void SetCellAlignment( int align, int row, int col)
861 { SetCellAlignment(row, col, align, wxCENTER); }
862 void SetCellAlignment( int WXUNUSED(align) ) {}
863 void SetCellBitmap(wxBitmap *WXUNUSED(bitmap), int WXUNUSED(row), int WXUNUSED(col))
864 { }
865 void SetDividerPen(const wxPen& WXUNUSED(pen)) { }
866 wxPen& GetDividerPen() const { return wxNullPen; }
867 void OnActivate(bool WXUNUSED(active)) {}
868
869 // ******** End of compatibility functions **********
870
871
872
873 // ------ control IDs
874 enum { wxGRID_CELLCTRL = 2000,
875 wxGRID_TOPCTRL };
876
877 // ------ control types
878 enum { wxGRID_TEXTCTRL = 2100,
879 wxGRID_CHECKBOX,
880 wxGRID_CHOICE,
881 wxGRID_COMBOBOX };
882
883 protected:
884 bool m_created;
885 bool m_displayed;
886
887 wxGridWindow *m_gridWin;
888 wxGridRowLabelWindow *m_rowLabelWin;
889 wxGridColLabelWindow *m_colLabelWin;
890 wxGridCornerLabelWindow *m_cornerLabelWin;
891
892 wxGridTableBase *m_table;
893
894 int m_left;
895 int m_top;
896 int m_right;
897 int m_bottom;
898
899 int m_numRows;
900 int m_numCols;
901
902 wxGridCellCoords m_currentCellCoords;
903
904 wxGridCellCoords m_selectedTopLeft;
905 wxGridCellCoords m_selectedBottomRight;
906
907 int m_defaultRowHeight;
908 wxArrayInt m_rowHeights;
909 wxArrayInt m_rowBottoms;
910
911 int m_defaultColWidth;
912 wxArrayInt m_colWidths;
913 wxArrayInt m_colRights;
914
915 int m_rowLabelWidth;
916 int m_colLabelHeight;
917
918 wxColour m_labelBackgroundColour;
919 wxColour m_labelTextColour;
920 wxFont m_labelFont;
921
922 int m_rowLabelHorizAlign;
923 int m_rowLabelVertAlign;
924 int m_colLabelHorizAlign;
925 int m_colLabelVertAlign;
926
927 bool m_defaultRowLabelValues;
928 bool m_defaultColLabelValues;
929
930 wxColour m_gridLineColour;
931 bool m_gridLinesEnabled;
932
933 // default cell attributes
934 wxFont m_defaultCellFont;
935 int m_defaultCellHAlign,
936 m_defaultCellVAlign;
937
938 // do we have some place to store attributes in?
939 bool CanHaveAttributes();
940
941 wxGridCellCoordsArray m_cellsExposed;
942 wxArrayInt m_rowsExposed;
943 wxArrayInt m_colsExposed;
944 wxArrayInt m_rowLabelsExposed;
945 wxArrayInt m_colLabelsExposed;
946
947 bool m_inOnKeyDown;
948 int m_batchCount;
949
950 int m_cursorMode;
951 enum { WXGRID_CURSOR_SELECT_CELL,
952 WXGRID_CURSOR_RESIZE_ROW,
953 WXGRID_CURSOR_RESIZE_COL,
954 WXGRID_CURSOR_SELECT_ROW,
955 WXGRID_CURSOR_SELECT_COL
956 };
957
958 int m_dragLastPos;
959 int m_dragRowOrCol;
960 bool m_isDragging;
961
962 wxGridCellCoords m_selectionStart;
963
964 wxCursor m_rowResizeCursor;
965 wxCursor m_colResizeCursor;
966
967 bool m_editable; // applies to whole grid
968 int m_editCtrlType; // for current cell
969 wxWindow* m_cellEditCtrl;
970 bool m_cellEditCtrlEnabled;
971
972
973 void Create();
974 void Init();
975 void CalcDimensions();
976 void CalcWindowSizes();
977 bool Redimension( wxGridTableMessage& );
978
979
980 bool SendEvent( const wxEventType,
981 int row, int col,
982 wxMouseEvent& );
983
984 bool SendEvent( const wxEventType,
985 int row, int col );
986
987
988 void OnPaint( wxPaintEvent& );
989 void OnSize( wxSizeEvent& );
990 void OnKeyDown( wxKeyEvent& );
991
992
993 void SetCurrentCell( const wxGridCellCoords& coords );
994 void SetCurrentCell( int row, int col )
995 { SetCurrentCell( wxGridCellCoords(row, col) ); }
996
997
998 // ------ functions to get/send data (see also public functions)
999 //
1000 bool GetModelValues();
1001 bool SetModelValues();
1002
1003
1004 DECLARE_DYNAMIC_CLASS( wxGrid )
1005 DECLARE_EVENT_TABLE()
1006 };
1007
1008
1009
1010
1011
1012 //
1013 // ------ Grid event class and event types
1014 //
1015
1016 class WXDLLEXPORT wxGridEvent : public wxNotifyEvent
1017 {
1018 public:
1019 wxGridEvent()
1020 : wxNotifyEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
1021 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1022 {
1023 }
1024
1025 wxGridEvent(int id, wxEventType type, wxObject* obj,
1026 int row=-1, int col=-1, int x=-1, int y=-1,
1027 bool control=FALSE, bool shift=FALSE, bool alt=FALSE, bool meta=FALSE);
1028
1029 virtual int GetRow() { return m_row; }
1030 virtual int GetCol() { return m_col; }
1031 wxPoint GetPosition() { return wxPoint( m_x, m_y ); }
1032 bool ControlDown() { return m_control; }
1033 bool MetaDown() { return m_meta; }
1034 bool ShiftDown() { return m_shift; }
1035 bool AltDown() { return m_alt; }
1036
1037 protected:
1038 int m_row;
1039 int m_col;
1040 int m_x;
1041 int m_y;
1042 bool m_control;
1043 bool m_meta;
1044 bool m_shift;
1045 bool m_alt;
1046
1047 DECLARE_DYNAMIC_CLASS(wxGridEvent)
1048 };
1049
1050
1051 class WXDLLEXPORT wxGridSizeEvent : public wxNotifyEvent
1052 {
1053 public:
1054 wxGridSizeEvent()
1055 : wxNotifyEvent(), m_rowOrCol(-1), m_x(-1), m_y(-1),
1056 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1057 {
1058 }
1059
1060 wxGridSizeEvent(int id, wxEventType type, wxObject* obj,
1061 int rowOrCol=-1, int x=-1, int y=-1,
1062 bool control=FALSE, bool shift=FALSE, bool alt=FALSE, bool meta=FALSE);
1063
1064 int GetRowOrCol() { return m_rowOrCol; }
1065 wxPoint GetPosition() { return wxPoint( m_x, m_y ); }
1066 bool ControlDown() { return m_control; }
1067 bool MetaDown() { return m_meta; }
1068 bool ShiftDown() { return m_shift; }
1069 bool AltDown() { return m_alt; }
1070
1071 protected:
1072 int m_rowOrCol;
1073 int m_x;
1074 int m_y;
1075 bool m_control;
1076 bool m_meta;
1077 bool m_shift;
1078 bool m_alt;
1079
1080 DECLARE_DYNAMIC_CLASS(wxGridSizeEvent)
1081 };
1082
1083
1084 class WXDLLEXPORT wxGridRangeSelectEvent : public wxNotifyEvent
1085 {
1086 public:
1087 wxGridRangeSelectEvent()
1088 : wxNotifyEvent()
1089 {
1090 m_topLeft = wxGridNoCellCoords;
1091 m_bottomRight = wxGridNoCellCoords;
1092 m_control = FALSE;
1093 m_meta = FALSE;
1094 m_shift = FALSE;
1095 m_alt = FALSE;
1096 }
1097
1098 wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
1099 const wxGridCellCoords& topLeft,
1100 const wxGridCellCoords& bottomRight,
1101 bool control=FALSE, bool shift=FALSE,
1102 bool alt=FALSE, bool meta=FALSE);
1103
1104 wxGridCellCoords GetTopLeftCoords() { return m_topLeft; }
1105 wxGridCellCoords GetBottomRightCoords() { return m_bottomRight; }
1106 int GetTopRow() { return m_topLeft.GetRow(); }
1107 int GetBottomRow() { return m_bottomRight.GetRow(); }
1108 int GetLeftCol() { return m_topLeft.GetCol(); }
1109 int GetRightCol() { return m_bottomRight.GetCol(); }
1110 bool ControlDown() { return m_control; }
1111 bool MetaDown() { return m_meta; }
1112 bool ShiftDown() { return m_shift; }
1113 bool AltDown() { return m_alt; }
1114
1115 protected:
1116 wxGridCellCoords m_topLeft;
1117 wxGridCellCoords m_bottomRight;
1118 bool m_control;
1119 bool m_meta;
1120 bool m_shift;
1121 bool m_alt;
1122
1123 DECLARE_DYNAMIC_CLASS(wxGridRangeSelectEvent)
1124 };
1125
1126
1127 const wxEventType EVT_GRID_CELL_LEFT_CLICK = wxEVT_FIRST + 1580;
1128 const wxEventType EVT_GRID_CELL_RIGHT_CLICK = wxEVT_FIRST + 1581;
1129 const wxEventType EVT_GRID_CELL_LEFT_DCLICK = wxEVT_FIRST + 1582;
1130 const wxEventType EVT_GRID_CELL_RIGHT_DCLICK = wxEVT_FIRST + 1583;
1131 const wxEventType EVT_GRID_LABEL_LEFT_CLICK = wxEVT_FIRST + 1584;
1132 const wxEventType EVT_GRID_LABEL_RIGHT_CLICK = wxEVT_FIRST + 1585;
1133 const wxEventType EVT_GRID_LABEL_LEFT_DCLICK = wxEVT_FIRST + 1586;
1134 const wxEventType EVT_GRID_LABEL_RIGHT_DCLICK = wxEVT_FIRST + 1587;
1135 const wxEventType EVT_GRID_ROW_SIZE = wxEVT_FIRST + 1588;
1136 const wxEventType EVT_GRID_COL_SIZE = wxEVT_FIRST + 1589;
1137 const wxEventType EVT_GRID_RANGE_SELECT = wxEVT_FIRST + 1590;
1138 const wxEventType EVT_GRID_CELL_CHANGE = wxEVT_FIRST + 1591;
1139 const wxEventType EVT_GRID_SELECT_CELL = wxEVT_FIRST + 1592;
1140
1141
1142 typedef void (wxEvtHandler::*wxGridEventFunction)(wxGridEvent&);
1143 typedef void (wxEvtHandler::*wxGridSizeEventFunction)(wxGridSizeEvent&);
1144 typedef void (wxEvtHandler::*wxGridRangeSelectEventFunction)(wxGridRangeSelectEvent&);
1145
1146 #define EVT_GRID_CELL_LEFT_CLICK(fn) { EVT_GRID_CELL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1147 #define EVT_GRID_CELL_RIGHT_CLICK(fn) { EVT_GRID_CELL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1148 #define EVT_GRID_CELL_LEFT_DCLICK(fn) { EVT_GRID_CELL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1149 #define EVT_GRID_CELL_RIGHT_DCLICK(fn) { EVT_GRID_CELL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1150 #define EVT_GRID_LABEL_LEFT_CLICK(fn) { EVT_GRID_LABEL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1151 #define EVT_GRID_LABEL_RIGHT_CLICK(fn) { EVT_GRID_LABEL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1152 #define EVT_GRID_LABEL_LEFT_DCLICK(fn) { EVT_GRID_LABEL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1153 #define EVT_GRID_LABEL_RIGHT_DCLICK(fn) { EVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1154 #define EVT_GRID_ROW_SIZE(fn) { EVT_GRID_ROW_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1155 #define EVT_GRID_COL_SIZE(fn) { EVT_GRID_COL_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1156 #define EVT_GRID_RANGE_SELECT(fn) { EVT_GRID_RANGE_SELECT, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridRangeSelectEventFunction) &fn, NULL },
1157 #define EVT_GRID_CELL_CHANGE(fn) { EVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1158 #define EVT_GRID_SELECT_CELL(fn) { EVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1159
1160
1161 #if 0 // TODO: implement these ? others ?
1162
1163 const wxEventType EVT_GRID_CREATE_CELL = wxEVT_FIRST + 1576;
1164 const wxEventType EVT_GRID_CHANGE_LABELS = wxEVT_FIRST + 1577;
1165 const wxEventType EVT_GRID_CHANGE_SEL_LABEL = wxEVT_FIRST + 1578;
1166
1167 #define EVT_GRID_CREATE_CELL(fn) { EVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1168 #define EVT_GRID_CHANGE_LABELS(fn) { EVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1169 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { EVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1170
1171 #endif
1172
1173 #endif // #ifndef __WXGRID_H__
1174
1175 #endif // ifndef wxUSE_NEW_GRID
1176