]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
Applied another Marco patch (5)
[wxWidgets.git] / src / generic / grid.cpp
CommitLineData
2796cce3 1///////////////////////////////////////////////////////////////////////////
d16c04bb 2// Name: generic/grid.cpp
f85afd4e
MB
3// Purpose: wxGrid and related classes
4// Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
97a9929e 5// Modified by: Robin Dunn, Vadim Zeitlin
f85afd4e
MB
6// Created: 1/08/1999
7// RCS-ID: $Id$
8// Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
758cbedf
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
f85afd4e
MB
20#ifdef __GNUG__
21 #pragma implementation "grid.h"
22#endif
23
f6bcfd97 24// For compilers that support precompilatixon, includes "wx/wx.h".
4d85bcd1
JS
25#include "wx/wxprec.h"
26
27#include "wx/defs.h"
f85afd4e
MB
28
29#ifdef __BORLANDC__
30 #pragma hdrstop
31#endif
32
27b92ca4
VZ
33#if wxUSE_GRID
34
8f177c8e 35#if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
27b92ca4
VZ
36 #include "gridg.cpp"
37#else // wxUSE_NEW_GRID
4d85bcd1 38
f85afd4e
MB
39#ifndef WX_PRECOMP
40 #include "wx/utils.h"
41 #include "wx/dcclient.h"
42 #include "wx/settings.h"
43 #include "wx/log.h"
508011ce
VZ
44 #include "wx/textctrl.h"
45 #include "wx/checkbox.h"
4ee5fc9c 46 #include "wx/combobox.h"
816be743 47 #include "wx/valtext.h"
f85afd4e
MB
48#endif
49
cb5df486 50#include "wx/textfile.h"
816be743 51#include "wx/spinctrl.h"
c4608a8a 52#include "wx/tokenzr.h"
6d004f67 53
07296f0b 54#include "wx/grid.h"
b5808881 55#include "wx/generic/gridsel.h"
07296f0b 56
0b7e6e7d
SN
57#if defined(__WXMOTIF__)
58 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
c78b3acd 59#else
0b7e6e7d 60 #define WXUNUSED_MOTIF(identifier) identifier
c78b3acd
SN
61#endif
62
63#if defined(__WXGTK__)
64 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
65#else
66 #define WXUNUSED_GTK(identifier) identifier
67#endif
68
3f8e5072
JS
69// Required for wxIs... functions
70#include <ctype.h>
71
b99be8fb 72// ----------------------------------------------------------------------------
758cbedf 73// array classes
b99be8fb
VZ
74// ----------------------------------------------------------------------------
75
f6bcfd97 76WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr *, wxArrayAttrs);
758cbedf 77
b99be8fb
VZ
78struct wxGridCellWithAttr
79{
2e9a6788
VZ
80 wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
81 : coords(row, col), attr(attr_)
b99be8fb
VZ
82 {
83 }
84
2e9a6788
VZ
85 ~wxGridCellWithAttr()
86 {
87 attr->DecRef();
88 }
89
b99be8fb 90 wxGridCellCoords coords;
2e9a6788 91 wxGridCellAttr *attr;
b99be8fb
VZ
92};
93
f6bcfd97 94WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray);
b99be8fb
VZ
95
96#include "wx/arrimpl.cpp"
97
98WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
99WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
100
0f442030
RR
101// ----------------------------------------------------------------------------
102// events
103// ----------------------------------------------------------------------------
104
2e4df4bf
VZ
105DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK)
106DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK)
107DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK)
108DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK)
109DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK)
110DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK)
111DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK)
112DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK)
113DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE)
114DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE)
115DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT)
116DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE)
117DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL)
118DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN)
119DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN)
bf7945ce 120DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED)
0f442030 121
b99be8fb
VZ
122// ----------------------------------------------------------------------------
123// private classes
124// ----------------------------------------------------------------------------
125
126class WXDLLEXPORT wxGridRowLabelWindow : public wxWindow
127{
128public:
129 wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; }
130 wxGridRowLabelWindow( wxGrid *parent, wxWindowID id,
131 const wxPoint &pos, const wxSize &size );
132
133private:
134 wxGrid *m_owner;
135
136 void OnPaint( wxPaintEvent& event );
137 void OnMouseEvent( wxMouseEvent& event );
b51c3f27 138 void OnMouseWheel( wxMouseEvent& event );
b99be8fb 139 void OnKeyDown( wxKeyEvent& event );
f6bcfd97 140 void OnKeyUp( wxKeyEvent& );
b99be8fb
VZ
141
142 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow)
143 DECLARE_EVENT_TABLE()
144};
145
146
147class WXDLLEXPORT wxGridColLabelWindow : public wxWindow
148{
149public:
150 wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; }
151 wxGridColLabelWindow( wxGrid *parent, wxWindowID id,
152 const wxPoint &pos, const wxSize &size );
153
154private:
155 wxGrid *m_owner;
156
157 void OnPaint( wxPaintEvent &event );
158 void OnMouseEvent( wxMouseEvent& event );
b51c3f27 159 void OnMouseWheel( wxMouseEvent& event );
b99be8fb 160 void OnKeyDown( wxKeyEvent& event );
f6bcfd97 161 void OnKeyUp( wxKeyEvent& );
b99be8fb
VZ
162
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow)
164 DECLARE_EVENT_TABLE()
165};
166
167
168class WXDLLEXPORT wxGridCornerLabelWindow : public wxWindow
169{
170public:
171 wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; }
172 wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id,
173 const wxPoint &pos, const wxSize &size );
174
175private:
176 wxGrid *m_owner;
177
178 void OnMouseEvent( wxMouseEvent& event );
b51c3f27 179 void OnMouseWheel( wxMouseEvent& event );
b99be8fb 180 void OnKeyDown( wxKeyEvent& event );
f6bcfd97 181 void OnKeyUp( wxKeyEvent& );
b99be8fb
VZ
182 void OnPaint( wxPaintEvent& event );
183
184 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow)
185 DECLARE_EVENT_TABLE()
186};
187
59ddac01 188class WXDLLEXPORT wxGridWindow : public wxWindow
b99be8fb
VZ
189{
190public:
191 wxGridWindow()
192 {
193 m_owner = (wxGrid *)NULL;
194 m_rowLabelWin = (wxGridRowLabelWindow *)NULL;
195 m_colLabelWin = (wxGridColLabelWindow *)NULL;
196 }
197
198 wxGridWindow( wxGrid *parent,
199 wxGridRowLabelWindow *rowLblWin,
200 wxGridColLabelWindow *colLblWin,
201 wxWindowID id, const wxPoint &pos, const wxSize &size );
202 ~wxGridWindow();
203
204 void ScrollWindow( int dx, int dy, const wxRect *rect );
205
206private:
207 wxGrid *m_owner;
208 wxGridRowLabelWindow *m_rowLabelWin;
209 wxGridColLabelWindow *m_colLabelWin;
210
211 void OnPaint( wxPaintEvent &event );
b51c3f27 212 void OnMouseWheel( wxMouseEvent& event );
b99be8fb
VZ
213 void OnMouseEvent( wxMouseEvent& event );
214 void OnKeyDown( wxKeyEvent& );
f6bcfd97 215 void OnKeyUp( wxKeyEvent& );
2796cce3
RD
216 void OnEraseBackground( wxEraseEvent& );
217
b99be8fb
VZ
218
219 DECLARE_DYNAMIC_CLASS(wxGridWindow)
220 DECLARE_EVENT_TABLE()
221};
222
2796cce3
RD
223
224
225class wxGridCellEditorEvtHandler : public wxEvtHandler
226{
227public:
228 wxGridCellEditorEvtHandler()
229 : m_grid(0), m_editor(0)
230 { }
231 wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor)
232 : m_grid(grid), m_editor(editor)
233 { }
234
235 void OnKeyDown(wxKeyEvent& event);
fb0de762 236 void OnChar(wxKeyEvent& event);
2796cce3
RD
237
238private:
239 wxGrid* m_grid;
240 wxGridCellEditor* m_editor;
241 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
242 DECLARE_EVENT_TABLE()
243};
244
245
246IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler )
247BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
248 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
fb0de762 249 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
2796cce3
RD
250END_EVENT_TABLE()
251
252
253
758cbedf 254// ----------------------------------------------------------------------------
b99be8fb 255// the internal data representation used by wxGridCellAttrProvider
758cbedf
VZ
256// ----------------------------------------------------------------------------
257
258// this class stores attributes set for cells
259class WXDLLEXPORT wxGridCellAttrData
b99be8fb
VZ
260{
261public:
2e9a6788 262 void SetAttr(wxGridCellAttr *attr, int row, int col);
b99be8fb 263 wxGridCellAttr *GetAttr(int row, int col) const;
4d60017a
SN
264 void UpdateAttrRows( size_t pos, int numRows );
265 void UpdateAttrCols( size_t pos, int numCols );
b99be8fb
VZ
266
267private:
268 // searches for the attr for given cell, returns wxNOT_FOUND if not found
269 int FindIndex(int row, int col) const;
270
271 wxGridCellWithAttrArray m_attrs;
272};
273
758cbedf
VZ
274// this class stores attributes set for rows or columns
275class WXDLLEXPORT wxGridRowOrColAttrData
276{
277public:
ee6694a7
VZ
278 // empty ctor to suppress warnings
279 wxGridRowOrColAttrData() { }
758cbedf
VZ
280 ~wxGridRowOrColAttrData();
281
282 void SetAttr(wxGridCellAttr *attr, int rowOrCol);
283 wxGridCellAttr *GetAttr(int rowOrCol) const;
4d60017a 284 void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols );
758cbedf
VZ
285
286private:
287 wxArrayInt m_rowsOrCols;
288 wxArrayAttrs m_attrs;
289};
290
291// NB: this is just a wrapper around 3 objects: one which stores cell
292// attributes, and 2 others for row/col ones
293class WXDLLEXPORT wxGridCellAttrProviderData
294{
295public:
296 wxGridCellAttrData m_cellAttrs;
297 wxGridRowOrColAttrData m_rowAttrs,
298 m_colAttrs;
299};
300
f2d76237
RD
301
302// ----------------------------------------------------------------------------
303// data structures used for the data type registry
304// ----------------------------------------------------------------------------
305
b94ae1ea
VZ
306struct wxGridDataTypeInfo
307{
f2d76237
RD
308 wxGridDataTypeInfo(const wxString& typeName,
309 wxGridCellRenderer* renderer,
310 wxGridCellEditor* editor)
311 : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
312 { }
313
39bcce60
VZ
314 ~wxGridDataTypeInfo()
315 {
316 wxSafeDecRef(m_renderer);
317 wxSafeDecRef(m_editor);
318 }
f2d76237
RD
319
320 wxString m_typeName;
321 wxGridCellRenderer* m_renderer;
322 wxGridCellEditor* m_editor;
323};
324
325
f6bcfd97 326WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo*, wxGridDataTypeInfoArray);
f2d76237
RD
327
328
b94ae1ea
VZ
329class WXDLLEXPORT wxGridTypeRegistry
330{
f2d76237 331public:
c78b3acd 332 wxGridTypeRegistry() {}
f2d76237 333 ~wxGridTypeRegistry();
b94ae1ea 334
f2d76237
RD
335 void RegisterDataType(const wxString& typeName,
336 wxGridCellRenderer* renderer,
337 wxGridCellEditor* editor);
c4608a8a
VZ
338
339 // find one of already registered data types
340 int FindRegisteredDataType(const wxString& typeName);
341
342 // try to FindRegisteredDataType(), if this fails and typeName is one of
343 // standard typenames, register it and return its index
f2d76237 344 int FindDataType(const wxString& typeName);
c4608a8a
VZ
345
346 // try to FindDataType(), if it fails see if it is not one of already
347 // registered data types with some params in which case clone the
348 // registered data type and set params for it
349 int FindOrCloneDataType(const wxString& typeName);
350
f2d76237
RD
351 wxGridCellRenderer* GetRenderer(int index);
352 wxGridCellEditor* GetEditor(int index);
353
354private:
355 wxGridDataTypeInfoArray m_typeinfo;
356};
357
b99be8fb
VZ
358// ----------------------------------------------------------------------------
359// conditional compilation
360// ----------------------------------------------------------------------------
361
9496deb5
MB
362#ifndef WXGRID_DRAW_LINES
363#define WXGRID_DRAW_LINES 1
796df70a
SN
364#endif
365
0a976765
VZ
366// ----------------------------------------------------------------------------
367// globals
368// ----------------------------------------------------------------------------
369
370//#define DEBUG_ATTR_CACHE
371#ifdef DEBUG_ATTR_CACHE
372 static size_t gs_nAttrCacheHits = 0;
373 static size_t gs_nAttrCacheMisses = 0;
374#endif // DEBUG_ATTR_CACHE
f85afd4e 375
43947979
VZ
376// ----------------------------------------------------------------------------
377// constants
378// ----------------------------------------------------------------------------
379
f85afd4e
MB
380wxGridCellCoords wxGridNoCellCoords( -1, -1 );
381wxRect wxGridNoCellRect( -1, -1, -1, -1 );
382
f0102d2a 383// scroll line size
faec5a43
SN
384// TODO: this doesn't work at all, grid cells have different sizes and approx
385// calculations don't work as because of the size mismatch scrollbars
386// sometimes fail to be shown when they should be or vice versa
b51c3f27
RD
387//
388// The scroll bars may be a little flakey once in a while, but that is
389// surely much less horrible than having scroll lines of only 1!!!
390// -- Robin
97a9929e
VZ
391//
392// Well, it's still seriously broken so it might be better but needs
393// fixing anyhow
394// -- Vadim
395static const size_t GRID_SCROLL_LINE_X = 15; // 1;
396static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X;
f85afd4e 397
43947979
VZ
398// the size of hash tables used a bit everywhere (the max number of elements
399// in these hash tables is the number of rows/columns)
400static const int GRID_HASH_SIZE = 100;
401
97a9929e
VZ
402// ----------------------------------------------------------------------------
403// private functions
404// ----------------------------------------------------------------------------
405
d0eb7e56 406static inline int GetScrollX(int x)
97a9929e
VZ
407{
408 return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X;
409}
410
d0eb7e56 411static inline int GetScrollY(int y)
97a9929e
VZ
412{
413 return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y;
414}
415
ab79958a
VZ
416// ============================================================================
417// implementation
418// ============================================================================
419
2796cce3
RD
420// ----------------------------------------------------------------------------
421// wxGridCellEditor
422// ----------------------------------------------------------------------------
423
424wxGridCellEditor::wxGridCellEditor()
425{
426 m_control = NULL;
427}
428
429
430wxGridCellEditor::~wxGridCellEditor()
431{
432 Destroy();
433}
434
508011ce
VZ
435void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
436 wxWindowID WXUNUSED(id),
437 wxEvtHandler* evtHandler)
438{
189d0213 439 if ( evtHandler )
508011ce
VZ
440 m_control->PushEventHandler(evtHandler);
441}
2796cce3 442
189d0213
VZ
443void wxGridCellEditor::PaintBackground(const wxRect& rectCell,
444 wxGridCellAttr *attr)
445{
446 // erase the background because we might not fill the cell
447 wxClientDC dc(m_control->GetParent());
448 dc.SetPen(*wxTRANSPARENT_PEN);
449 dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
450 dc.DrawRectangle(rectCell);
451
452 // redraw the control we just painted over
453 m_control->Refresh();
454}
455
2796cce3
RD
456void wxGridCellEditor::Destroy()
457{
508011ce
VZ
458 if (m_control)
459 {
b94ae1ea
VZ
460 m_control->PopEventHandler(TRUE /* delete it*/);
461
2796cce3
RD
462 m_control->Destroy();
463 m_control = NULL;
464 }
465}
466
3da93aae 467void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr)
2796cce3
RD
468{
469 wxASSERT_MSG(m_control,
470 wxT("The wxGridCellEditor must be Created first!"));
471 m_control->Show(show);
3da93aae
VZ
472
473 if ( show )
474 {
475 // set the colours/fonts if we have any
476 if ( attr )
477 {
f2d76237
RD
478 m_colFgOld = m_control->GetForegroundColour();
479 m_control->SetForegroundColour(attr->GetTextColour());
3da93aae 480
f2d76237
RD
481 m_colBgOld = m_control->GetBackgroundColour();
482 m_control->SetBackgroundColour(attr->GetBackgroundColour());
3da93aae 483
f2d76237
RD
484 m_fontOld = m_control->GetFont();
485 m_control->SetFont(attr->GetFont());
3da93aae
VZ
486
487 // can't do anything more in the base class version, the other
488 // attributes may only be used by the derived classes
489 }
490 }
491 else
492 {
493 // restore the standard colours fonts
494 if ( m_colFgOld.Ok() )
495 {
496 m_control->SetForegroundColour(m_colFgOld);
497 m_colFgOld = wxNullColour;
498 }
499
500 if ( m_colBgOld.Ok() )
501 {
502 m_control->SetBackgroundColour(m_colBgOld);
503 m_colBgOld = wxNullColour;
504 }
505
506 if ( m_fontOld.Ok() )
507 {
508 m_control->SetFont(m_fontOld);
509 m_fontOld = wxNullFont;
510 }
511 }
2796cce3
RD
512}
513
514void wxGridCellEditor::SetSize(const wxRect& rect)
515{
516 wxASSERT_MSG(m_control,
517 wxT("The wxGridCellEditor must be Created first!"));
28a77bc4 518 m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
2796cce3
RD
519}
520
521void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
522{
523 event.Skip();
524}
525
f6bcfd97
BP
526bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event)
527{
528 // accept the simple key presses, not anything with Ctrl/Alt/Meta
a4f7bf58 529 return !(event.ControlDown() || event.AltDown());
f6bcfd97 530}
2796cce3 531
2c9a89e0
RD
532void wxGridCellEditor::StartingKey(wxKeyEvent& event)
533{
e195a54c
VZ
534 event.Skip();
535}
2c9a89e0 536
e195a54c
VZ
537void wxGridCellEditor::StartingClick()
538{
b54ba671 539}
2c9a89e0 540
3a8c693a
VZ
541#if wxUSE_TEXTCTRL
542
b54ba671
VZ
543// ----------------------------------------------------------------------------
544// wxGridCellTextEditor
545// ----------------------------------------------------------------------------
2c9a89e0 546
2796cce3
RD
547wxGridCellTextEditor::wxGridCellTextEditor()
548{
c4608a8a 549 m_maxChars = 0;
2796cce3
RD
550}
551
552void wxGridCellTextEditor::Create(wxWindow* parent,
553 wxWindowID id,
2796cce3
RD
554 wxEvtHandler* evtHandler)
555{
508011ce 556 m_control = new wxTextCtrl(parent, id, wxEmptyString,
2c9a89e0 557 wxDefaultPosition, wxDefaultSize
2796cce3 558#if defined(__WXMSW__)
84912ef8
RD
559 , wxTE_PROCESS_TAB | wxTE_MULTILINE |
560 wxTE_NO_VSCROLL | wxTE_AUTO_SCROLL
2796cce3 561#endif
508011ce 562 );
2796cce3 563
c4608a8a
VZ
564 // TODO: use m_maxChars
565
508011ce 566 wxGridCellEditor::Create(parent, id, evtHandler);
2796cce3
RD
567}
568
189d0213
VZ
569void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell),
570 wxGridCellAttr * WXUNUSED(attr))
571{
572 // as we fill the entire client area, don't do anything here to minimize
573 // flicker
574}
2796cce3 575
99306db2
VZ
576void wxGridCellTextEditor::SetSize(const wxRect& rectOrig)
577{
578 wxRect rect(rectOrig);
579
580 // Make the edit control large enough to allow for internal
581 // margins
582 //
583 // TODO: remove this if the text ctrl sizing is improved esp. for
584 // unix
585 //
586#if defined(__WXGTK__)
b0e282b3
RR
587 if (rect.x != 0)
588 {
589 rect.x += 1;
590 rect.y += 1;
591 rect.width -= 1;
592 rect.height -= 1;
593 }
99306db2 594#else // !GTK
cb105ad4 595 int extra_x = ( rect.x > 2 )? 2 : 1;
84912ef8
RD
596
597// MB: treat MSW separately here otherwise the caret doesn't show
598// when the editor is in the first row.
a0948e27
MB
599#if defined(__WXMSW__)
600 int extra_y = 2;
601#else
cb105ad4 602 int extra_y = ( rect.y > 2 )? 2 : 1;
a0948e27
MB
603#endif // MSW
604
99306db2 605#if defined(__WXMOTIF__)
cb105ad4
SN
606 extra_x *= 2;
607 extra_y *= 2;
99306db2 608#endif
cb105ad4
SN
609 rect.SetLeft( wxMax(0, rect.x - extra_x) );
610 rect.SetTop( wxMax(0, rect.y - extra_y) );
611 rect.SetRight( rect.GetRight() + 2*extra_x );
612 rect.SetBottom( rect.GetBottom() + 2*extra_y );
99306db2
VZ
613#endif // GTK/!GTK
614
615 wxGridCellEditor::SetSize(rect);
616}
617
3da93aae 618void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid)
2796cce3
RD
619{
620 wxASSERT_MSG(m_control,
621 wxT("The wxGridCellEditor must be Created first!"));
622
623 m_startValue = grid->GetTable()->GetValue(row, col);
816be743
VZ
624
625 DoBeginEdit(m_startValue);
626}
627
628void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
629{
630 Text()->SetValue(startValue);
b54ba671 631 Text()->SetInsertionPointEnd();
505f70de 632 Text()->SetSelection(-1,-1);
b54ba671 633 Text()->SetFocus();
2796cce3
RD
634}
635
3324d5f5 636bool wxGridCellTextEditor::EndEdit(int row, int col,
3da93aae 637 wxGrid* grid)
2796cce3
RD
638{
639 wxASSERT_MSG(m_control,
640 wxT("The wxGridCellEditor must be Created first!"));
641
642 bool changed = FALSE;
b54ba671 643 wxString value = Text()->GetValue();
2796cce3
RD
644 if (value != m_startValue)
645 changed = TRUE;
646
647 if (changed)
648 grid->GetTable()->SetValue(row, col, value);
2c9a89e0 649
3da93aae 650 m_startValue = wxEmptyString;
b54ba671 651 Text()->SetValue(m_startValue);
2796cce3
RD
652
653 return changed;
654}
655
656
657void wxGridCellTextEditor::Reset()
658{
659 wxASSERT_MSG(m_control,
660 wxT("The wxGridCellEditor must be Created first!"));
661
816be743
VZ
662 DoReset(m_startValue);
663}
664
665void wxGridCellTextEditor::DoReset(const wxString& startValue)
666{
667 Text()->SetValue(startValue);
b54ba671 668 Text()->SetInsertionPointEnd();
2796cce3
RD
669}
670
f6bcfd97
BP
671bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event)
672{
673 if ( wxGridCellEditor::IsAcceptedKey(event) )
674 {
675 int keycode = event.GetKeyCode();
676 switch ( keycode )
677 {
678 case WXK_NUMPAD0:
679 case WXK_NUMPAD1:
680 case WXK_NUMPAD2:
681 case WXK_NUMPAD3:
682 case WXK_NUMPAD4:
683 case WXK_NUMPAD5:
684 case WXK_NUMPAD6:
685 case WXK_NUMPAD7:
686 case WXK_NUMPAD8:
687 case WXK_NUMPAD9:
688 case WXK_MULTIPLY:
689 case WXK_NUMPAD_MULTIPLY:
690 case WXK_ADD:
691 case WXK_NUMPAD_ADD:
692 case WXK_SUBTRACT:
693 case WXK_NUMPAD_SUBTRACT:
694 case WXK_DECIMAL:
695 case WXK_NUMPAD_DECIMAL:
696 case WXK_DIVIDE:
697 case WXK_NUMPAD_DIVIDE:
698 return TRUE;
699
700 default:
701 // accept 8 bit chars too if isprint() agrees
702 if ( (keycode < 255) && (isprint(keycode)) )
703 return TRUE;
704 }
705 }
706
707 return FALSE;
708}
709
2c9a89e0
RD
710void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
711{
94af7d45 712 if ( !Text()->EmulateKeyPress(event) )
f6bcfd97
BP
713 {
714 event.Skip();
715 }
b54ba671 716}
2c9a89e0 717
c78b3acd 718void wxGridCellTextEditor::HandleReturn( wxKeyEvent&
0b7e6e7d 719 WXUNUSED_GTK(WXUNUSED_MOTIF(event)) )
2796cce3
RD
720{
721#if defined(__WXMOTIF__) || defined(__WXGTK__)
722 // wxMotif needs a little extra help...
6fc0f38f 723 size_t pos = (size_t)( Text()->GetInsertionPoint() );
b54ba671 724 wxString s( Text()->GetValue() );
8dd8f875 725 s = s.Left(pos) + wxT("\n") + s.Mid(pos);
b54ba671
VZ
726 Text()->SetValue(s);
727 Text()->SetInsertionPoint( pos );
2796cce3
RD
728#else
729 // the other ports can handle a Return key press
730 //
731 event.Skip();
732#endif
733}
734
c4608a8a
VZ
735void wxGridCellTextEditor::SetParameters(const wxString& params)
736{
737 if ( !params )
738 {
739 // reset to default
740 m_maxChars = 0;
741 }
742 else
743 {
744 long tmp;
745 if ( !params.ToLong(&tmp) )
746 {
f6bcfd97 747 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str());
c4608a8a
VZ
748 }
749 else
750 {
751 m_maxChars = (size_t)tmp;
752 }
753 }
754}
755
73145b0e
JS
756// DJC MAPTEK
757// return the value in the text control
758wxString wxGridCellTextEditor::GetValue() const
759{
760 return Text()->GetValue();
761}
762
816be743
VZ
763// ----------------------------------------------------------------------------
764// wxGridCellNumberEditor
765// ----------------------------------------------------------------------------
766
767wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
768{
769 m_min = min;
770 m_max = max;
771}
772
773void wxGridCellNumberEditor::Create(wxWindow* parent,
774 wxWindowID id,
775 wxEvtHandler* evtHandler)
776{
777 if ( HasRange() )
778 {
779 // create a spin ctrl
780 m_control = new wxSpinCtrl(parent, -1, wxEmptyString,
781 wxDefaultPosition, wxDefaultSize,
782 wxSP_ARROW_KEYS,
783 m_min, m_max);
784
785 wxGridCellEditor::Create(parent, id, evtHandler);
786 }
787 else
788 {
789 // just a text control
790 wxGridCellTextEditor::Create(parent, id, evtHandler);
791
792#if wxUSE_VALIDATORS
85bc0351 793 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
816be743
VZ
794#endif // wxUSE_VALIDATORS
795 }
796}
797
798void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
799{
800 // first get the value
801 wxGridTableBase *table = grid->GetTable();
802 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
803 {
804 m_valueOld = table->GetValueAsLong(row, col);
805 }
806 else
807 {
8a60ff0a 808 m_valueOld = 0;
a5777624 809 wxString sValue = table->GetValue(row, col);
8a60ff0a 810 if (! sValue.ToLong(&m_valueOld) && ! sValue.IsEmpty())
a5777624
RD
811 {
812 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
813 return;
814 }
816be743
VZ
815 }
816
817 if ( HasRange() )
818 {
4a64bee4 819 Spin()->SetValue((int)m_valueOld);
f6bcfd97 820 Spin()->SetFocus();
816be743
VZ
821 }
822 else
823 {
824 DoBeginEdit(GetString());
825 }
826}
827
3324d5f5 828bool wxGridCellNumberEditor::EndEdit(int row, int col,
816be743
VZ
829 wxGrid* grid)
830{
831 bool changed;
8a60ff0a
RD
832 long value = 0;
833 wxString text;
816be743
VZ
834
835 if ( HasRange() )
836 {
837 value = Spin()->GetValue();
838 changed = value != m_valueOld;
8a60ff0a
RD
839 if (changed)
840 text = wxString::Format(wxT("%ld"), value);
816be743
VZ
841 }
842 else
843 {
8a60ff0a
RD
844 text = Text()->GetValue();
845 changed = (text.IsEmpty() || text.ToLong(&value)) && (value != m_valueOld);
816be743
VZ
846 }
847
848 if ( changed )
849 {
a5777624
RD
850 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
851 grid->GetTable()->SetValueAsLong(row, col, value);
852 else
8a60ff0a 853 grid->GetTable()->SetValue(row, col, text);
816be743
VZ
854 }
855
856 return changed;
857}
858
859void wxGridCellNumberEditor::Reset()
860{
861 if ( HasRange() )
862 {
4a64bee4 863 Spin()->SetValue((int)m_valueOld);
816be743
VZ
864 }
865 else
866 {
867 DoReset(GetString());
868 }
869}
870
f6bcfd97
BP
871bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event)
872{
873 if ( wxGridCellEditor::IsAcceptedKey(event) )
874 {
875 int keycode = event.GetKeyCode();
876 switch ( keycode )
877 {
878 case WXK_NUMPAD0:
879 case WXK_NUMPAD1:
880 case WXK_NUMPAD2:
881 case WXK_NUMPAD3:
882 case WXK_NUMPAD4:
883 case WXK_NUMPAD5:
884 case WXK_NUMPAD6:
885 case WXK_NUMPAD7:
886 case WXK_NUMPAD8:
887 case WXK_NUMPAD9:
888 case WXK_ADD:
889 case WXK_NUMPAD_ADD:
890 case WXK_SUBTRACT:
891 case WXK_NUMPAD_SUBTRACT:
892 case WXK_UP:
893 case WXK_DOWN:
894 return TRUE;
895
896 default:
897 if ( (keycode < 128) && isdigit(keycode) )
898 return TRUE;
899 }
900 }
901
902 return FALSE;
903}
904
816be743
VZ
905void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
906{
907 if ( !HasRange() )
908 {
4a64bee4 909 int keycode = (int) event.KeyCode();
85d8c319
JS
910 if ( isdigit(keycode) || keycode == '+' || keycode == '-'
911 || keycode == WXK_NUMPAD0
912 || keycode == WXK_NUMPAD1
913 || keycode == WXK_NUMPAD2
914 || keycode == WXK_NUMPAD3
915 || keycode == WXK_NUMPAD4
916 || keycode == WXK_NUMPAD5
917 || keycode == WXK_NUMPAD6
918 || keycode == WXK_NUMPAD7
919 || keycode == WXK_NUMPAD8
920 || keycode == WXK_NUMPAD9
921 || keycode == WXK_ADD
922 || keycode == WXK_NUMPAD_ADD
923 || keycode == WXK_SUBTRACT
924 || keycode == WXK_NUMPAD_SUBTRACT)
816be743
VZ
925 {
926 wxGridCellTextEditor::StartingKey(event);
927
928 // skip Skip() below
929 return;
930 }
931 }
932
933 event.Skip();
934}
9c4ba614 935
c4608a8a
VZ
936void wxGridCellNumberEditor::SetParameters(const wxString& params)
937{
938 if ( !params )
939 {
940 // reset to default
941 m_min =
942 m_max = -1;
943 }
944 else
945 {
946 long tmp;
947 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
948 {
949 m_min = (int)tmp;
950
951 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
952 {
953 m_max = (int)tmp;
954
955 // skip the error message below
956 return;
957 }
958 }
959
f6bcfd97 960 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str());
c4608a8a
VZ
961 }
962}
963
73145b0e
JS
964// DJC MAPTEK
965// return the value in the spin control if it is there (the text control otherwise)
966wxString wxGridCellNumberEditor::GetValue() const
967{
968 wxString s;
969
970 if( HasRange() )
971 {
972 int value = Spin()->GetValue();
973 s.Printf(wxT("%ld"), value);
974 }
975 else
976 {
977 s = Text()->GetValue();
978 }
979 return s;
980}
981
816be743
VZ
982// ----------------------------------------------------------------------------
983// wxGridCellFloatEditor
984// ----------------------------------------------------------------------------
985
f6bcfd97
BP
986wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision)
987{
988 m_width = width;
989 m_precision = precision;
990}
991
816be743
VZ
992void wxGridCellFloatEditor::Create(wxWindow* parent,
993 wxWindowID id,
994 wxEvtHandler* evtHandler)
995{
996 wxGridCellTextEditor::Create(parent, id, evtHandler);
997
998#if wxUSE_VALIDATORS
85bc0351 999 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
816be743
VZ
1000#endif // wxUSE_VALIDATORS
1001}
1002
1003void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
1004{
1005 // first get the value
1006 wxGridTableBase *table = grid->GetTable();
1007 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1008 {
1009 m_valueOld = table->GetValueAsDouble(row, col);
1010 }
1011 else
1012 {
8a60ff0a 1013 m_valueOld = 0.0;
a5777624 1014 wxString sValue = table->GetValue(row, col);
8a60ff0a 1015 if (! sValue.ToDouble(&m_valueOld) && ! sValue.IsEmpty())
a5777624
RD
1016 {
1017 wxFAIL_MSG( _T("this cell doesn't have float value") );
1018 return;
1019 }
816be743
VZ
1020 }
1021
1022 DoBeginEdit(GetString());
1023}
1024
3324d5f5 1025bool wxGridCellFloatEditor::EndEdit(int row, int col,
816be743
VZ
1026 wxGrid* grid)
1027{
8a60ff0a
RD
1028 double value = 0.0;
1029 wxString text(Text()->GetValue());
1030
1031 if ( (text.IsEmpty() || text.ToDouble(&value)) && (value != m_valueOld) )
816be743 1032 {
a5777624
RD
1033 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT))
1034 grid->GetTable()->SetValueAsDouble(row, col, value);
1035 else
8a60ff0a 1036 grid->GetTable()->SetValue(row, col, text);
816be743
VZ
1037
1038 return TRUE;
1039 }
ae31cc57 1040 return FALSE;
816be743
VZ
1041}
1042
1043void wxGridCellFloatEditor::Reset()
1044{
1045 DoReset(GetString());
1046}
1047
1048void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
1049{
4a64bee4 1050 int keycode = (int)event.KeyCode();
85d8c319
JS
1051 if ( isdigit(keycode) || keycode == '+' || keycode == '-' || keycode == '.'
1052 || keycode == WXK_NUMPAD0
1053 || keycode == WXK_NUMPAD1
1054 || keycode == WXK_NUMPAD2
1055 || keycode == WXK_NUMPAD3
1056 || keycode == WXK_NUMPAD4
1057 || keycode == WXK_NUMPAD5
1058 || keycode == WXK_NUMPAD6
1059 || keycode == WXK_NUMPAD7
1060 || keycode == WXK_NUMPAD8
1061 || keycode == WXK_NUMPAD9
1062 || keycode == WXK_ADD
1063 || keycode == WXK_NUMPAD_ADD
1064 || keycode == WXK_SUBTRACT
1065 || keycode == WXK_NUMPAD_SUBTRACT)
816be743
VZ
1066 {
1067 wxGridCellTextEditor::StartingKey(event);
1068
1069 // skip Skip() below
1070 return;
1071 }
1072
1073 event.Skip();
1074}
1075
f6bcfd97
BP
1076void wxGridCellFloatEditor::SetParameters(const wxString& params)
1077{
1078 if ( !params )
1079 {
1080 // reset to default
1081 m_width =
1082 m_precision = -1;
1083 }
1084 else
1085 {
1086 long tmp;
1087 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
1088 {
1089 m_width = (int)tmp;
1090
1091 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
1092 {
1093 m_precision = (int)tmp;
1094
1095 // skip the error message below
1096 return;
1097 }
1098 }
1099
1100 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str());
1101 }
1102}
1103
1104wxString wxGridCellFloatEditor::GetString() const
1105{
1106 wxString fmt;
1107 if ( m_width == -1 )
1108 {
1109 // default width/precision
ec53826c 1110 fmt = _T("%f");
f6bcfd97
BP
1111 }
1112 else if ( m_precision == -1 )
1113 {
1114 // default precision
ec53826c 1115 fmt.Printf(_T("%%%d.f"), m_width);
f6bcfd97
BP
1116 }
1117 else
1118 {
ec53826c 1119 fmt.Printf(_T("%%%d.%df"), m_width, m_precision);
f6bcfd97
BP
1120 }
1121
1122 return wxString::Format(fmt, m_valueOld);
1123}
1124
1125bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
1126{
1127 if ( wxGridCellEditor::IsAcceptedKey(event) )
1128 {
1129 int keycode = event.GetKeyCode();
1130 switch ( keycode )
1131 {
1132 case WXK_NUMPAD0:
1133 case WXK_NUMPAD1:
1134 case WXK_NUMPAD2:
1135 case WXK_NUMPAD3:
1136 case WXK_NUMPAD4:
1137 case WXK_NUMPAD5:
1138 case WXK_NUMPAD6:
1139 case WXK_NUMPAD7:
1140 case WXK_NUMPAD8:
1141 case WXK_NUMPAD9:
1142 case WXK_ADD:
1143 case WXK_NUMPAD_ADD:
1144 case WXK_SUBTRACT:
1145 case WXK_NUMPAD_SUBTRACT:
1146 case WXK_DECIMAL:
1147 case WXK_NUMPAD_DECIMAL:
1148 return TRUE;
1149
1150 default:
1151 // additionally accept 'e' as in '1e+6'
1152 if ( (keycode < 128) &&
c6707d16 1153 (isdigit(keycode) || tolower(keycode) == 'e') )
f6bcfd97
BP
1154 return TRUE;
1155 }
1156 }
1157
1158 return FALSE;
1159}
1160
3a8c693a
VZ
1161#endif // wxUSE_TEXTCTRL
1162
1163#if wxUSE_CHECKBOX
1164
508011ce
VZ
1165// ----------------------------------------------------------------------------
1166// wxGridCellBoolEditor
1167// ----------------------------------------------------------------------------
1168
1169void wxGridCellBoolEditor::Create(wxWindow* parent,
1170 wxWindowID id,
1171 wxEvtHandler* evtHandler)
1172{
1173 m_control = new wxCheckBox(parent, id, wxEmptyString,
1174 wxDefaultPosition, wxDefaultSize,
1175 wxNO_BORDER);
1176
1177 wxGridCellEditor::Create(parent, id, evtHandler);
1178}
1179
1180void wxGridCellBoolEditor::SetSize(const wxRect& r)
1181{
b94ae1ea
VZ
1182 bool resize = FALSE;
1183 wxSize size = m_control->GetSize();
1184 wxCoord minSize = wxMin(r.width, r.height);
1185
1186 // check if the checkbox is not too big/small for this cell
1187 wxSize sizeBest = m_control->GetBestSize();
1188 if ( !(size == sizeBest) )
1189 {
1190 // reset to default size if it had been made smaller
1191 size = sizeBest;
1192
1193 resize = TRUE;
1194 }
1195
1196 if ( size.x >= minSize || size.y >= minSize )
1197 {
1198 // leave 1 pixel margin
1199 size.x = size.y = minSize - 2;
1200
1201 resize = TRUE;
1202 }
1203
1204 if ( resize )
1205 {
1206 m_control->SetSize(size);
1207 }
1208
508011ce 1209 // position it in the centre of the rectangle (TODO: support alignment?)
508011ce 1210
b94ae1ea 1211#if defined(__WXGTK__) || defined (__WXMOTIF__)
508011ce
VZ
1212 // the checkbox without label still has some space to the right in wxGTK,
1213 // so shift it to the right
b94ae1ea
VZ
1214 size.x -= 8;
1215#elif defined(__WXMSW__)
a95e38c0
VZ
1216 // here too, but in other way
1217 size.x += 1;
b94ae1ea
VZ
1218 size.y -= 2;
1219#endif
508011ce 1220
b94ae1ea 1221 m_control->Move(r.x + r.width/2 - size.x/2, r.y + r.height/2 - size.y/2);
508011ce
VZ
1222}
1223
1224void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
1225{
99306db2
VZ
1226 m_control->Show(show);
1227
189d0213 1228 if ( show )
508011ce 1229 {
189d0213
VZ
1230 wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
1231 CBox()->SetBackgroundColour(colBg);
508011ce 1232 }
508011ce
VZ
1233}
1234
1235void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
1236{
1237 wxASSERT_MSG(m_control,
1238 wxT("The wxGridCellEditor must be Created first!"));
1239
28a77bc4 1240 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
f2d76237
RD
1241 m_startValue = grid->GetTable()->GetValueAsBool(row, col);
1242 else
695a3263
MB
1243 {
1244 wxString cellval( grid->GetTable()->GetValue(row, col) );
8dd8f875 1245 m_startValue = !( !cellval || (cellval == wxT("0")) );
695a3263 1246 }
508011ce
VZ
1247 CBox()->SetValue(m_startValue);
1248 CBox()->SetFocus();
1249}
1250
1251bool wxGridCellBoolEditor::EndEdit(int row, int col,
508011ce
VZ
1252 wxGrid* grid)
1253{
1254 wxASSERT_MSG(m_control,
1255 wxT("The wxGridCellEditor must be Created first!"));
1256
1257 bool changed = FALSE;
1258 bool value = CBox()->GetValue();
1259 if ( value != m_startValue )
1260 changed = TRUE;
1261
1262 if ( changed )
1263 {
28a77bc4 1264 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
f2d76237
RD
1265 grid->GetTable()->SetValueAsBool(row, col, value);
1266 else
1267 grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
508011ce
VZ
1268 }
1269
1270 return changed;
1271}
1272
1273void wxGridCellBoolEditor::Reset()
1274{
1275 wxASSERT_MSG(m_control,
1276 wxT("The wxGridCellEditor must be Created first!"));
1277
1278 CBox()->SetValue(m_startValue);
1279}
1280
e195a54c 1281void wxGridCellBoolEditor::StartingClick()
508011ce 1282{
e195a54c 1283 CBox()->SetValue(!CBox()->GetValue());
508011ce
VZ
1284}
1285
f6bcfd97
BP
1286bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event)
1287{
1288 if ( wxGridCellEditor::IsAcceptedKey(event) )
1289 {
1290 int keycode = event.GetKeyCode();
1291 switch ( keycode )
1292 {
1293 case WXK_MULTIPLY:
1294 case WXK_NUMPAD_MULTIPLY:
1295 case WXK_ADD:
1296 case WXK_NUMPAD_ADD:
1297 case WXK_SUBTRACT:
1298 case WXK_NUMPAD_SUBTRACT:
1299 case WXK_SPACE:
1300 case '+':
1301 case '-':
1302 return TRUE;
1303 }
1304 }
1305
1306 return FALSE;
1307}
73145b0e
JS
1308// DJC MAPTEK
1309// return the value as "1" for true and the empty string for false
1310wxString wxGridCellBoolEditor::GetValue() const
1311{
1312 bool bSet = CBox()->GetValue();
1313 return bSet ? "1" : wxEmptyString;
1314}
f6bcfd97 1315
3a8c693a
VZ
1316#endif // wxUSE_CHECKBOX
1317
1318#if wxUSE_COMBOBOX
1319
4ee5fc9c
VZ
1320// ----------------------------------------------------------------------------
1321// wxGridCellChoiceEditor
1322// ----------------------------------------------------------------------------
1323
1324wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
f6bcfd97 1325 const wxString choices[],
4ee5fc9c
VZ
1326 bool allowOthers)
1327 : m_allowOthers(allowOthers)
1328{
c4608a8a 1329 if ( count )
4ee5fc9c 1330 {
c4608a8a
VZ
1331 m_choices.Alloc(count);
1332 for ( size_t n = 0; n < count; n++ )
1333 {
1334 m_choices.Add(choices[n]);
1335 }
4ee5fc9c
VZ
1336 }
1337}
1338
c4608a8a
VZ
1339wxGridCellEditor *wxGridCellChoiceEditor::Clone() const
1340{
1341 wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor;
1342 editor->m_allowOthers = m_allowOthers;
1343 editor->m_choices = m_choices;
1344
1345 return editor;
1346}
1347
4ee5fc9c
VZ
1348void wxGridCellChoiceEditor::Create(wxWindow* parent,
1349 wxWindowID id,
1350 wxEvtHandler* evtHandler)
1351{
1352 size_t count = m_choices.GetCount();
1353 wxString *choices = new wxString[count];
1354 for ( size_t n = 0; n < count; n++ )
1355 {
1356 choices[n] = m_choices[n];
1357 }
1358
1359 m_control = new wxComboBox(parent, id, wxEmptyString,
1360 wxDefaultPosition, wxDefaultSize,
1361 count, choices,
1362 m_allowOthers ? 0 : wxCB_READONLY);
1363
1364 delete [] choices;
1365
1366 wxGridCellEditor::Create(parent, id, evtHandler);
1367}
1368
a5777624
RD
1369void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell,
1370 wxGridCellAttr * attr)
4ee5fc9c
VZ
1371{
1372 // as we fill the entire client area, don't do anything here to minimize
1373 // flicker
a5777624
RD
1374
1375 // TODO: It doesn't actually fill the client area since the height of a
1376 // combo always defaults to the standard... Until someone has time to
1377 // figure out the right rectangle to paint, just do it the normal way...
1378 wxGridCellEditor::PaintBackground(rectCell, attr);
4ee5fc9c
VZ
1379}
1380
1381void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
1382{
1383 wxASSERT_MSG(m_control,
1384 wxT("The wxGridCellEditor must be Created first!"));
1385
1386 m_startValue = grid->GetTable()->GetValue(row, col);
1387
2b5f62a0
VZ
1388 if (m_allowOthers)
1389 Combo()->SetValue(m_startValue);
1390 else
28a77bc4 1391 {
2b5f62a0
VZ
1392 // find the right position, or default to the first if not found
1393 int pos = Combo()->FindString(m_startValue);
1394 if (pos == -1)
1395 pos = 0;
1396 Combo()->SetSelection(pos);
28a77bc4 1397 }
4ee5fc9c
VZ
1398 Combo()->SetInsertionPointEnd();
1399 Combo()->SetFocus();
1400}
1401
28a77bc4 1402bool wxGridCellChoiceEditor::EndEdit(int row, int col,
4ee5fc9c
VZ
1403 wxGrid* grid)
1404{
1405 wxString value = Combo()->GetValue();
1406 bool changed = value != m_startValue;
1407
1408 if ( changed )
1409 grid->GetTable()->SetValue(row, col, value);
1410
1411 m_startValue = wxEmptyString;
2b5f62a0
VZ
1412 if (m_allowOthers)
1413 Combo()->SetValue(m_startValue);
1414 else
1415 Combo()->SetSelection(0);
4ee5fc9c
VZ
1416
1417 return changed;
1418}
1419
1420void wxGridCellChoiceEditor::Reset()
1421{
1422 Combo()->SetValue(m_startValue);
1423 Combo()->SetInsertionPointEnd();
1424}
1425
c4608a8a
VZ
1426void wxGridCellChoiceEditor::SetParameters(const wxString& params)
1427{
1428 if ( !params )
1429 {
1430 // what can we do?
1431 return;
1432 }
1433
1434 m_choices.Empty();
1435
1436 wxStringTokenizer tk(params, _T(','));
1437 while ( tk.HasMoreTokens() )
1438 {
1439 m_choices.Add(tk.GetNextToken());
1440 }
1441}
1442
73145b0e
JS
1443// DJC MAPTEK
1444// return the value in the text control
1445wxString wxGridCellChoiceEditor::GetValue() const
1446{
1447 return Combo()->GetValue();
1448}
3a8c693a
VZ
1449#endif // wxUSE_COMBOBOX
1450
508011ce
VZ
1451// ----------------------------------------------------------------------------
1452// wxGridCellEditorEvtHandler
1453// ----------------------------------------------------------------------------
2796cce3
RD
1454
1455void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
1456{
1457 switch ( event.KeyCode() )
1458 {
1459 case WXK_ESCAPE:
1460 m_editor->Reset();
b54ba671 1461 m_grid->DisableCellEditControl();
2796cce3
RD
1462 break;
1463
2c9a89e0 1464 case WXK_TAB:
b51c3f27 1465 m_grid->GetEventHandler()->ProcessEvent( event );
9b4aede2
RD
1466 break;
1467
2796cce3 1468 case WXK_RETURN:
faec5a43
SN
1469 case WXK_NUMPAD_ENTER:
1470 if (!m_grid->GetEventHandler()->ProcessEvent(event))
2796cce3
RD
1471 m_editor->HandleReturn(event);
1472 break;
1473
2796cce3
RD
1474
1475 default:
1476 event.Skip();
1477 }
1478}
1479
fb0de762
RD
1480void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event)
1481{
1482 switch ( event.KeyCode() )
1483 {
1484 case WXK_ESCAPE:
1485 case WXK_TAB:
1486 case WXK_RETURN:
a4f7bf58 1487 case WXK_NUMPAD_ENTER:
fb0de762
RD
1488 break;
1489
1490 default:
1491 event.Skip();
1492 }
1493}
1494
c4608a8a
VZ
1495// ----------------------------------------------------------------------------
1496// wxGridCellWorker is an (almost) empty common base class for
1497// wxGridCellRenderer and wxGridCellEditor managing ref counting
1498// ----------------------------------------------------------------------------
1499
1500void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
1501{
1502 // nothing to do
1503}
1504
1505wxGridCellWorker::~wxGridCellWorker()
1506{
1507}
1508
508011ce
VZ
1509// ============================================================================
1510// renderer classes
1511// ============================================================================
1512
ab79958a
VZ
1513// ----------------------------------------------------------------------------
1514// wxGridCellRenderer
1515// ----------------------------------------------------------------------------
1516
1517void wxGridCellRenderer::Draw(wxGrid& grid,
2796cce3 1518 wxGridCellAttr& attr,
ab79958a
VZ
1519 wxDC& dc,
1520 const wxRect& rect,
c78b3acd 1521 int WXUNUSED(row), int WXUNUSED(col),
ab79958a
VZ
1522 bool isSelected)
1523{
1524 dc.SetBackgroundMode( wxSOLID );
1525
73145b0e
JS
1526// DJC (MAPTEK) grey out fields if the grid is disabled
1527 if( grid.IsEnabled() )
ab79958a 1528 {
73145b0e
JS
1529 if ( isSelected )
1530 {
1531 dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
1532 }
1533 else
1534 {
1535 dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
1536 }
1537 }
ab79958a
VZ
1538 else
1539 {
73145b0e 1540 dc.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE), wxSOLID));
ab79958a
VZ
1541 }
1542
1543 dc.SetPen( *wxTRANSPARENT_PEN );
ab79958a
VZ
1544 dc.DrawRectangle(rect);
1545}
1546
508011ce
VZ
1547// ----------------------------------------------------------------------------
1548// wxGridCellStringRenderer
1549// ----------------------------------------------------------------------------
1550
816be743
VZ
1551void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid,
1552 wxGridCellAttr& attr,
1553 wxDC& dc,
1554 bool isSelected)
ab79958a 1555{
ab79958a
VZ
1556 dc.SetBackgroundMode( wxTRANSPARENT );
1557
283b7808
VZ
1558 // TODO some special colours for attr.IsReadOnly() case?
1559
73145b0e
JS
1560 // DJC (MAPTEK) different coloured text when the grid is disabled
1561 if( grid.IsEnabled() )
ab79958a 1562 {
73145b0e
JS
1563 if ( isSelected )
1564 {
1565 dc.SetTextBackground( grid.GetSelectionBackground() );
1566 dc.SetTextForeground( grid.GetSelectionForeground() );
1567 }
1568 else
1569 {
1570 dc.SetTextBackground( attr.GetBackgroundColour() );
1571 dc.SetTextForeground( attr.GetTextColour() );
1572 }
ab79958a
VZ
1573 }
1574 else
1575 {
73145b0e
JS
1576 dc.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE));
1577 dc.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT));
ab79958a 1578 }
816be743 1579
2796cce3 1580 dc.SetFont( attr.GetFont() );
816be743
VZ
1581}
1582
65e4e78e
VZ
1583wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr,
1584 wxDC& dc,
1585 const wxString& text)
1586{
f6bcfd97 1587 wxCoord x = 0, y = 0, max_x = 0;
65e4e78e 1588 dc.SetFont(attr.GetFont());
f6bcfd97
BP
1589 wxStringTokenizer tk(text, _T('\n'));
1590 while ( tk.HasMoreTokens() )
1591 {
1592 dc.GetTextExtent(tk.GetNextToken(), &x, &y);
1593 max_x = wxMax(max_x, x);
1594 }
1595
1596 y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines.
65e4e78e 1597
f6bcfd97 1598 return wxSize(max_x, y);
65e4e78e
VZ
1599}
1600
1601wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
1602 wxGridCellAttr& attr,
1603 wxDC& dc,
1604 int row, int col)
1605{
1606 return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
1607}
1608
816be743
VZ
1609void wxGridCellStringRenderer::Draw(wxGrid& grid,
1610 wxGridCellAttr& attr,
1611 wxDC& dc,
1612 const wxRect& rectCell,
1613 int row, int col,
1614 bool isSelected)
1615{
27f35b66 1616 wxRect rect = rectCell;
dc1f566f
SN
1617 rect.Inflate(-1);
1618
1619 // erase only this cells background, overflow cells should have been erased
1620 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1621
1622 int hAlign, vAlign;
1623 attr.GetAlignment(&hAlign, &vAlign);
27f35b66 1624
39b80349
SN
1625 int overflowCols = 0;
1626
27f35b66
SN
1627 if (attr.GetOverflow())
1628 {
1629 int cols = grid.GetNumberCols();
1630 int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth();
1631 int cell_rows, cell_cols;
1632 attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <=0
1633 if ((best_width > rectCell.width) && (col < cols) && grid.GetTable())
1634 {
1635 int i, c_cols, c_rows;
1636 for (i = col+cell_cols; i < cols; i++)
1637 {
2b5f62a0 1638 bool is_empty = TRUE;
39b80349 1639 for (int j=row; j<row+cell_rows; j++)
27f35b66 1640 {
39b80349 1641 // check w/ anchor cell for multicell block
ef4d6ce8 1642 grid.GetCellSize(j, i, &c_rows, &c_cols);
39b80349 1643 if (c_rows > 0) c_rows = 0;
ef4d6ce8 1644 if (!grid.GetTable()->IsEmptyCell(j+c_rows, i))
39b80349 1645 {
2b5f62a0 1646 is_empty = FALSE;
ef4d6ce8 1647 break;
39b80349 1648 }
27f35b66 1649 }
39b80349
SN
1650 if (is_empty)
1651 rect.width += grid.GetColSize(i);
dc1f566f
SN
1652 else
1653 {
1654 i--;
1655 break;
1656 }
39b80349 1657 if (rect.width >= best_width) break;
2b5f62a0 1658 }
39b80349 1659 overflowCols = i - col - cell_cols + 1;
2b5f62a0 1660 if (overflowCols >= cols) overflowCols = cols - 1;
39b80349 1661 }
27f35b66 1662
dc1f566f
SN
1663 if (overflowCols > 0) // redraw overflow cells w/ proper hilight
1664 {
39b80349 1665 hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned
2b5f62a0 1666 wxRect clip = rect;
39b80349 1667 clip.x += rectCell.width;
dc1f566f
SN
1668 // draw each overflow cell individually
1669 int col_end = col+cell_cols+overflowCols;
1670 if (col_end >= grid.GetNumberCols())
2b5f62a0 1671 col_end = grid.GetNumberCols() - 1;
dc1f566f
SN
1672 for (int i = col+cell_cols; i <= col_end; i++)
1673 {
dc1f566f
SN
1674 clip.width = grid.GetColSize(i) - 1;
1675 dc.DestroyClippingRegion();
1676 dc.SetClippingRegion(clip);
39b80349
SN
1677
1678 SetTextColoursAndFont(grid, attr, dc,
2b5f62a0 1679 grid.IsInSelection(row,i));
39b80349 1680
dc1f566f 1681 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
2b5f62a0 1682 rect, hAlign, vAlign);
39b80349 1683 clip.x += grid.GetColSize(i) - 1;
dc1f566f 1684 }
ab79958a 1685
39b80349 1686 rect = rectCell;
2b5f62a0 1687 rect.Inflate(-1);
dc1f566f 1688 rect.width++;
39b80349 1689 dc.DestroyClippingRegion();
dc1f566f 1690 }
39b80349 1691 }
ab79958a 1692
dc1f566f
SN
1693 // now we only have to draw the text
1694 SetTextColoursAndFont(grid, attr, dc, isSelected);
39b80349 1695
ab79958a
VZ
1696 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
1697 rect, hAlign, vAlign);
1698}
1699
65e4e78e
VZ
1700// ----------------------------------------------------------------------------
1701// wxGridCellNumberRenderer
1702// ----------------------------------------------------------------------------
1703
1704wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col)
1705{
1706 wxGridTableBase *table = grid.GetTable();
1707 wxString text;
1708 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1709 {
1710 text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
1711 }
9c4ba614
VZ
1712 else
1713 {
1714 text = table->GetValue(row, col);
1715 }
65e4e78e
VZ
1716
1717 return text;
1718}
1719
816be743
VZ
1720void wxGridCellNumberRenderer::Draw(wxGrid& grid,
1721 wxGridCellAttr& attr,
1722 wxDC& dc,
1723 const wxRect& rectCell,
1724 int row, int col,
1725 bool isSelected)
1726{
1727 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1728
1729 SetTextColoursAndFont(grid, attr, dc, isSelected);
1730
1731 // draw the text right aligned by default
1732 int hAlign, vAlign;
1733 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 1734 hAlign = wxALIGN_RIGHT;
816be743
VZ
1735
1736 wxRect rect = rectCell;
1737 rect.Inflate(-1);
1738
65e4e78e
VZ
1739 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1740}
816be743 1741
65e4e78e
VZ
1742wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
1743 wxGridCellAttr& attr,
1744 wxDC& dc,
1745 int row, int col)
1746{
1747 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1748}
1749
1750// ----------------------------------------------------------------------------
1751// wxGridCellFloatRenderer
1752// ----------------------------------------------------------------------------
1753
1754wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
1755{
1756 SetWidth(width);
1757 SetPrecision(precision);
1758}
1759
e72b4213
VZ
1760wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
1761{
1762 wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
1763 renderer->m_width = m_width;
1764 renderer->m_precision = m_precision;
1765 renderer->m_format = m_format;
1766
1767 return renderer;
1768}
1769
65e4e78e
VZ
1770wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col)
1771{
1772 wxGridTableBase *table = grid.GetTable();
0b190b0f
VZ
1773
1774 bool hasDouble;
1775 double val;
65e4e78e
VZ
1776 wxString text;
1777 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1778 {
0b190b0f
VZ
1779 val = table->GetValueAsDouble(row, col);
1780 hasDouble = TRUE;
65e4e78e 1781 }
9c4ba614
VZ
1782 else
1783 {
1784 text = table->GetValue(row, col);
0b190b0f 1785 hasDouble = text.ToDouble(&val);
9c4ba614 1786 }
65e4e78e 1787
0b190b0f
VZ
1788 if ( hasDouble )
1789 {
1790 if ( !m_format )
1791 {
1792 if ( m_width == -1 )
1793 {
19d7140e
VZ
1794 if ( m_precision == -1 )
1795 {
2b5f62a0
VZ
1796 // default width/precision
1797 m_format = _T("%f");
1798 }
19d7140e
VZ
1799 else
1800 {
1801 m_format.Printf(_T("%%.%df"), m_precision);
1802 }
0b190b0f
VZ
1803 }
1804 else if ( m_precision == -1 )
1805 {
1806 // default precision
1807 m_format.Printf(_T("%%%d.f"), m_width);
1808 }
1809 else
1810 {
1811 m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
1812 }
1813 }
1814
1815 text.Printf(m_format, val);
19d7140e 1816
0b190b0f
VZ
1817 }
1818 //else: text already contains the string
1819
65e4e78e
VZ
1820 return text;
1821}
1822
816be743
VZ
1823void wxGridCellFloatRenderer::Draw(wxGrid& grid,
1824 wxGridCellAttr& attr,
1825 wxDC& dc,
1826 const wxRect& rectCell,
1827 int row, int col,
1828 bool isSelected)
1829{
1830 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1831
1832 SetTextColoursAndFont(grid, attr, dc, isSelected);
1833
1834 // draw the text right aligned by default
1835 int hAlign, vAlign;
1836 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 1837 hAlign = wxALIGN_RIGHT;
816be743
VZ
1838
1839 wxRect rect = rectCell;
1840 rect.Inflate(-1);
1841
65e4e78e
VZ
1842 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1843}
816be743 1844
65e4e78e
VZ
1845wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
1846 wxGridCellAttr& attr,
1847 wxDC& dc,
1848 int row, int col)
1849{
1850 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1851}
1852
0b190b0f
VZ
1853void wxGridCellFloatRenderer::SetParameters(const wxString& params)
1854{
0b190b0f
VZ
1855 if ( !params )
1856 {
1857 // reset to defaults
1858 SetWidth(-1);
1859 SetPrecision(-1);
1860 }
1861 else
1862 {
1863 wxString tmp = params.BeforeFirst(_T(','));
1864 if ( !!tmp )
1865 {
1866 long width;
19d7140e 1867 if ( tmp.ToLong(&width) )
0b190b0f 1868 {
19d7140e 1869 SetWidth((int)width);
0b190b0f
VZ
1870 }
1871 else
1872 {
19d7140e
VZ
1873 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str());
1874 }
0b190b0f 1875
19d7140e 1876 }
0b190b0f
VZ
1877 tmp = params.AfterFirst(_T(','));
1878 if ( !!tmp )
1879 {
1880 long precision;
19d7140e 1881 if ( tmp.ToLong(&precision) )
0b190b0f 1882 {
19d7140e 1883 SetPrecision((int)precision);
0b190b0f
VZ
1884 }
1885 else
1886 {
19d7140e 1887 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str());
0b190b0f
VZ
1888 }
1889
0b190b0f
VZ
1890 }
1891 }
1892}
1893
19d7140e 1894
508011ce
VZ
1895// ----------------------------------------------------------------------------
1896// wxGridCellBoolRenderer
1897// ----------------------------------------------------------------------------
1898
65e4e78e 1899wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
508011ce 1900
b94ae1ea
VZ
1901// FIXME these checkbox size calculations are really ugly...
1902
65e4e78e 1903// between checkmark and box
a95e38c0 1904static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
508011ce 1905
65e4e78e
VZ
1906wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
1907 wxGridCellAttr& WXUNUSED(attr),
1908 wxDC& WXUNUSED(dc),
1909 int WXUNUSED(row),
1910 int WXUNUSED(col))
1911{
1912 // compute it only once (no locks for MT safeness in GUI thread...)
1913 if ( !ms_sizeCheckMark.x )
297da4ba 1914 {
65e4e78e
VZ
1915 // get checkbox size
1916 wxCoord checkSize = 0;
297da4ba
VZ
1917 wxCheckBox *checkbox = new wxCheckBox(&grid, -1, wxEmptyString);
1918 wxSize size = checkbox->GetBestSize();
a95e38c0 1919 checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN;
297da4ba 1920
65e4e78e 1921 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
69d8f612 1922#if defined(__WXGTK__) || defined(__WXMOTIF__)
65e4e78e 1923 checkSize -= size.y / 2;
297da4ba
VZ
1924#endif
1925
1926 delete checkbox;
65e4e78e
VZ
1927
1928 ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
297da4ba
VZ
1929 }
1930
65e4e78e
VZ
1931 return ms_sizeCheckMark;
1932}
1933
1934void wxGridCellBoolRenderer::Draw(wxGrid& grid,
1935 wxGridCellAttr& attr,
1936 wxDC& dc,
1937 const wxRect& rect,
1938 int row, int col,
1939 bool isSelected)
1940{
1941 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
1942
297da4ba 1943 // draw a check mark in the centre (ignoring alignment - TODO)
65e4e78e 1944 wxSize size = GetBestSize(grid, attr, dc, row, col);
b94ae1ea
VZ
1945
1946 // don't draw outside the cell
1947 wxCoord minSize = wxMin(rect.width, rect.height);
1948 if ( size.x >= minSize || size.y >= minSize )
1949 {
1950 // and even leave (at least) 1 pixel margin
1951 size.x = size.y = minSize - 2;
1952 }
1953
1954 // draw a border around checkmark
a95e38c0
VZ
1955 wxRect rectBorder;
1956 rectBorder.x = rect.x + rect.width/2 - size.x/2;
1957 rectBorder.y = rect.y + rect.height/2 - size.y/2;
1958 rectBorder.width = size.x;
1959 rectBorder.height = size.y;
b94ae1ea 1960
f2d76237 1961 bool value;
b94ae1ea 1962 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
f2d76237
RD
1963 value = grid.GetTable()->GetValueAsBool(row, col);
1964 else
695a3263
MB
1965 {
1966 wxString cellval( grid.GetTable()->GetValue(row, col) );
8dd8f875 1967 value = !( !cellval || (cellval == wxT("0")) );
695a3263 1968 }
f2d76237
RD
1969
1970 if ( value )
508011ce 1971 {
a95e38c0
VZ
1972 wxRect rectMark = rectBorder;
1973#ifdef __WXMSW__
1974 // MSW DrawCheckMark() is weird (and should probably be changed...)
1975 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2);
1976 rectMark.x++;
1977 rectMark.y++;
1978#else // !MSW
1979 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
1980#endif // MSW/!MSW
1981
508011ce
VZ
1982 dc.SetTextForeground(attr.GetTextColour());
1983 dc.DrawCheckMark(rectMark);
1984 }
a95e38c0
VZ
1985
1986 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1987 dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
1988 dc.DrawRectangle(rectBorder);
508011ce
VZ
1989}
1990
2796cce3
RD
1991// ----------------------------------------------------------------------------
1992// wxGridCellAttr
1993// ----------------------------------------------------------------------------
1994
1df4050d
VZ
1995void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
1996{
1997 m_nRef = 1;
1998
1999 m_isReadOnly = Unset;
2000
2001 m_renderer = NULL;
2002 m_editor = NULL;
2003
2004 m_attrkind = wxGridCellAttr::Cell;
2005
27f35b66
SN
2006 m_sizeRows = m_sizeCols = 1;
2007 m_overflow = TRUE;
2008
1df4050d
VZ
2009 SetDefAttr(attrDefault);
2010}
2011
39bcce60 2012wxGridCellAttr *wxGridCellAttr::Clone() const
a68c1246 2013{
1df4050d
VZ
2014 wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr);
2015
a68c1246
VZ
2016 if ( HasTextColour() )
2017 attr->SetTextColour(GetTextColour());
2018 if ( HasBackgroundColour() )
2019 attr->SetBackgroundColour(GetBackgroundColour());
2020 if ( HasFont() )
2021 attr->SetFont(GetFont());
2022 if ( HasAlignment() )
2023 attr->SetAlignment(m_hAlign, m_vAlign);
2024
27f35b66
SN
2025 attr->SetSize( m_sizeRows, m_sizeCols );
2026
a68c1246
VZ
2027 if ( m_renderer )
2028 {
2029 attr->SetRenderer(m_renderer);
39bcce60 2030 m_renderer->IncRef();
a68c1246
VZ
2031 }
2032 if ( m_editor )
2033 {
2034 attr->SetEditor(m_editor);
39bcce60 2035 m_editor->IncRef();
a68c1246
VZ
2036 }
2037
2038 if ( IsReadOnly() )
2039 attr->SetReadOnly();
2040
19d7140e
VZ
2041 attr->SetKind( m_attrkind );
2042
a68c1246
VZ
2043 return attr;
2044}
2045
19d7140e
VZ
2046void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom)
2047{
2048 if ( !HasTextColour() && mergefrom->HasTextColour() )
2049 SetTextColour(mergefrom->GetTextColour());
2050 if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() )
2051 SetBackgroundColour(mergefrom->GetBackgroundColour());
2052 if ( !HasFont() && mergefrom->HasFont() )
2053 SetFont(mergefrom->GetFont());
7e48d7d9 2054 if ( !HasAlignment() && mergefrom->HasAlignment() ){
19d7140e
VZ
2055 int hAlign, vAlign;
2056 mergefrom->GetAlignment( &hAlign, &vAlign);
2057 SetAlignment(hAlign, vAlign);
2058 }
2059
27f35b66
SN
2060 mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
2061
19d7140e
VZ
2062 // Directly access member functions as GetRender/Editor don't just return
2063 // m_renderer/m_editor
2064 //
2065 // Maybe add support for merge of Render and Editor?
2066 if (!HasRenderer() && mergefrom->HasRenderer() )
bf7945ce 2067 {
19d7140e
VZ
2068 m_renderer = mergefrom->m_renderer;
2069 m_renderer->IncRef();
2070 }
2071 if ( !HasEditor() && mergefrom->HasEditor() )
2072 {
2073 m_editor = mergefrom->m_editor;
2074 m_editor->IncRef();
2075 }
2076 if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() )
2077 SetReadOnly(mergefrom->IsReadOnly());
2078
2079 SetDefAttr(mergefrom->m_defGridAttr);
2080}
2081
27f35b66
SN
2082void wxGridCellAttr::SetSize(int num_rows, int num_cols)
2083{
2084 // The size of a cell is normally 1,1
2085
2086 // If this cell is larger (2,2) then this is the top left cell
2087 // the other cells that will be covered (lower right cells) must be
2088 // set to negative or zero values such that
2089 // row + num_rows of the covered cell points to the larger cell (this cell)
2090 // same goes for the col + num_cols.
2091
2092 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2093
2094 wxASSERT_MSG( (!((num_rows>0)&&(num_cols<=0)) ||
2095 !((num_rows<=0)&&(num_cols>0)) ||
2096 !((num_rows==0)&&(num_cols==0))),
2097 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2098
2099 m_sizeRows = num_rows;
2100 m_sizeCols = num_cols;
2101}
2102
2796cce3
RD
2103const wxColour& wxGridCellAttr::GetTextColour() const
2104{
2105 if (HasTextColour())
508011ce 2106 {
2796cce3 2107 return m_colText;
508011ce 2108 }
0926b2fc 2109 else if (m_defGridAttr && m_defGridAttr != this)
508011ce 2110 {
2796cce3 2111 return m_defGridAttr->GetTextColour();
508011ce
VZ
2112 }
2113 else
2114 {
2796cce3
RD
2115 wxFAIL_MSG(wxT("Missing default cell attribute"));
2116 return wxNullColour;
2117 }
2118}
2119
2120
2121const wxColour& wxGridCellAttr::GetBackgroundColour() const
2122{
2123 if (HasBackgroundColour())
2124 return m_colBack;
0926b2fc 2125 else if (m_defGridAttr && m_defGridAttr != this)
2796cce3 2126 return m_defGridAttr->GetBackgroundColour();
508011ce
VZ
2127 else
2128 {
2796cce3
RD
2129 wxFAIL_MSG(wxT("Missing default cell attribute"));
2130 return wxNullColour;
2131 }
2132}
2133
2134
2135const wxFont& wxGridCellAttr::GetFont() const
2136{
2137 if (HasFont())
2138 return m_font;
0926b2fc 2139 else if (m_defGridAttr && m_defGridAttr != this)
2796cce3 2140 return m_defGridAttr->GetFont();
508011ce
VZ
2141 else
2142 {
2796cce3
RD
2143 wxFAIL_MSG(wxT("Missing default cell attribute"));
2144 return wxNullFont;
2145 }
2146}
2147
2148
2149void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
2150{
508011ce
VZ
2151 if (HasAlignment())
2152 {
2796cce3
RD
2153 if ( hAlign ) *hAlign = m_hAlign;
2154 if ( vAlign ) *vAlign = m_vAlign;
2155 }
0926b2fc 2156 else if (m_defGridAttr && m_defGridAttr != this)
2796cce3 2157 m_defGridAttr->GetAlignment(hAlign, vAlign);
508011ce
VZ
2158 else
2159 {
2796cce3
RD
2160 wxFAIL_MSG(wxT("Missing default cell attribute"));
2161 }
2162}
2163
27f35b66
SN
2164void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
2165{
2166 if ( num_rows ) *num_rows = m_sizeRows;
2167 if ( num_cols ) *num_cols = m_sizeCols;
2168}
2796cce3 2169
f2d76237 2170// GetRenderer and GetEditor use a slightly different decision path about
28a77bc4
RD
2171// which attribute to use. If a non-default attr object has one then it is
2172// used, otherwise the default editor or renderer is fetched from the grid and
2173// used. It should be the default for the data type of the cell. If it is
2174// NULL (because the table has a type that the grid does not have in its
2175// registry,) then the grid's default editor or renderer is used.
2176
2177wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
2178{
3cf883a2 2179 wxGridCellRenderer *renderer;
28a77bc4 2180
3cf883a2 2181 if ( m_renderer && this != m_defGridAttr )
0b190b0f 2182 {
3cf883a2
VZ
2183 // use the cells renderer if it has one
2184 renderer = m_renderer;
2185 renderer->IncRef();
0b190b0f 2186 }
3cf883a2 2187 else // no non default cell renderer
0b190b0f 2188 {
3cf883a2
VZ
2189 // get default renderer for the data type
2190 if ( grid )
2191 {
2192 // GetDefaultRendererForCell() will do IncRef() for us
2193 renderer = grid->GetDefaultRendererForCell(row, col);
2194 }
2195 else
2196 {
2197 renderer = NULL;
2198 }
0b190b0f 2199
3cf883a2
VZ
2200 if ( !renderer )
2201 {
0926b2fc 2202 if (m_defGridAttr && this != m_defGridAttr )
3cf883a2
VZ
2203 {
2204 // if we still don't have one then use the grid default
2205 // (no need for IncRef() here neither)
2206 renderer = m_defGridAttr->GetRenderer(NULL, 0, 0);
2207 }
2208 else // default grid attr
2209 {
2210 // use m_renderer which we had decided not to use initially
2211 renderer = m_renderer;
2212 if ( renderer )
2213 renderer->IncRef();
2214 }
2215 }
0b190b0f 2216 }
28a77bc4 2217
3cf883a2
VZ
2218 // we're supposed to always find something
2219 wxASSERT_MSG(renderer, wxT("Missing default cell renderer"));
28a77bc4
RD
2220
2221 return renderer;
2796cce3
RD
2222}
2223
3cf883a2 2224// same as above, except for s/renderer/editor/g
28a77bc4 2225wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
07296f0b 2226{
3cf883a2 2227 wxGridCellEditor *editor;
0b190b0f 2228
3cf883a2 2229 if ( m_editor && this != m_defGridAttr )
0b190b0f 2230 {
3cf883a2
VZ
2231 // use the cells editor if it has one
2232 editor = m_editor;
2233 editor->IncRef();
0b190b0f 2234 }
3cf883a2 2235 else // no non default cell editor
0b190b0f 2236 {
3cf883a2
VZ
2237 // get default editor for the data type
2238 if ( grid )
2239 {
2240 // GetDefaultEditorForCell() will do IncRef() for us
2241 editor = grid->GetDefaultEditorForCell(row, col);
2242 }
2243 else
2244 {
2245 editor = NULL;
2246 }
2247
2248 if ( !editor )
2249 {
0926b2fc 2250 if ( m_defGridAttr && this != m_defGridAttr )
3cf883a2
VZ
2251 {
2252 // if we still don't have one then use the grid default
2253 // (no need for IncRef() here neither)
2254 editor = m_defGridAttr->GetEditor(NULL, 0, 0);
2255 }
2256 else // default grid attr
2257 {
2258 // use m_editor which we had decided not to use initially
2259 editor = m_editor;
2260 if ( editor )
2261 editor->IncRef();
2262 }
2263 }
0b190b0f 2264 }
28a77bc4 2265
3cf883a2
VZ
2266 // we're supposed to always find something
2267 wxASSERT_MSG(editor, wxT("Missing default cell editor"));
2268
28a77bc4 2269 return editor;
07296f0b
RD
2270}
2271
b99be8fb 2272// ----------------------------------------------------------------------------
758cbedf 2273// wxGridCellAttrData
b99be8fb
VZ
2274// ----------------------------------------------------------------------------
2275
758cbedf 2276void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
b99be8fb
VZ
2277{
2278 int n = FindIndex(row, col);
2279 if ( n == wxNOT_FOUND )
2280 {
2281 // add the attribute
2282 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
2283 }
2284 else
2285 {
6f36917b
VZ
2286 // free the old attribute
2287 m_attrs[(size_t)n].attr->DecRef();
2288
b99be8fb
VZ
2289 if ( attr )
2290 {
2291 // change the attribute
2e9a6788 2292 m_attrs[(size_t)n].attr = attr;
b99be8fb
VZ
2293 }
2294 else
2295 {
2296 // remove this attribute
2297 m_attrs.RemoveAt((size_t)n);
2298 }
2299 }
b99be8fb
VZ
2300}
2301
758cbedf 2302wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
b99be8fb
VZ
2303{
2304 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2305
2306 int n = FindIndex(row, col);
2307 if ( n != wxNOT_FOUND )
2308 {
2e9a6788
VZ
2309 attr = m_attrs[(size_t)n].attr;
2310 attr->IncRef();
b99be8fb
VZ
2311 }
2312
2313 return attr;
2314}
2315
4d60017a
SN
2316void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
2317{
2318 size_t count = m_attrs.GetCount();
2319 for ( size_t n = 0; n < count; n++ )
2320 {
2321 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
2322 wxCoord row = coords.GetRow();
2323 if ((size_t)row >= pos)
2324 {
2325 if (numRows > 0)
2326 {
2327 // If rows inserted, include row counter where necessary
2328 coords.SetRow(row + numRows);
2329 }
2330 else if (numRows < 0)
2331 {
2332 // If rows deleted ...
2333 if ((size_t)row >= pos - numRows)
2334 {
2335 // ...either decrement row counter (if row still exists)...
2336 coords.SetRow(row + numRows);
2337 }
2338 else
2339 {
2340 // ...or remove the attribute
2341 m_attrs.RemoveAt((size_t)n);
2342 n--; count--;
2343 }
2344 }
4d60017a
SN
2345 }
2346 }
2347}
2348
2349void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
2350{
2351 size_t count = m_attrs.GetCount();
2352 for ( size_t n = 0; n < count; n++ )
2353 {
2354 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
2355 wxCoord col = coords.GetCol();
2356 if ( (size_t)col >= pos )
2357 {
2358 if ( numCols > 0 )
2359 {
2360 // If rows inserted, include row counter where necessary
2361 coords.SetCol(col + numCols);
2362 }
2363 else if (numCols < 0)
2364 {
2365 // If rows deleted ...
2366 if ((size_t)col >= pos - numCols)
2367 {
2368 // ...either decrement row counter (if row still exists)...
2369 coords.SetCol(col + numCols);
2370 }
2371 else
2372 {
2373 // ...or remove the attribute
2374 m_attrs.RemoveAt((size_t)n);
2375 n--; count--;
2376 }
2377 }
4d60017a
SN
2378 }
2379 }
2380}
2381
758cbedf 2382int wxGridCellAttrData::FindIndex(int row, int col) const
b99be8fb
VZ
2383{
2384 size_t count = m_attrs.GetCount();
2385 for ( size_t n = 0; n < count; n++ )
2386 {
2387 const wxGridCellCoords& coords = m_attrs[n].coords;
2388 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
2389 {
2390 return n;
2391 }
2392 }
2393
2394 return wxNOT_FOUND;
2395}
2396
758cbedf
VZ
2397// ----------------------------------------------------------------------------
2398// wxGridRowOrColAttrData
2399// ----------------------------------------------------------------------------
2400
2401wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2402{
2403 size_t count = m_attrs.Count();
2404 for ( size_t n = 0; n < count; n++ )
2405 {
2406 m_attrs[n]->DecRef();
2407 }
2408}
2409
2410wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
2411{
2412 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2413
2414 int n = m_rowsOrCols.Index(rowOrCol);
2415 if ( n != wxNOT_FOUND )
2416 {
2417 attr = m_attrs[(size_t)n];
2418 attr->IncRef();
2419 }
2420
2421 return attr;
2422}
2423
2424void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
2425{
a95e38c0
VZ
2426 int i = m_rowsOrCols.Index(rowOrCol);
2427 if ( i == wxNOT_FOUND )
758cbedf
VZ
2428 {
2429 // add the attribute
2430 m_rowsOrCols.Add(rowOrCol);
2431 m_attrs.Add(attr);
2432 }
2433 else
2434 {
a95e38c0 2435 size_t n = (size_t)i;
758cbedf
VZ
2436 if ( attr )
2437 {
2438 // change the attribute
a95e38c0
VZ
2439 m_attrs[n]->DecRef();
2440 m_attrs[n] = attr;
758cbedf
VZ
2441 }
2442 else
2443 {
2444 // remove this attribute
a95e38c0
VZ
2445 m_attrs[n]->DecRef();
2446 m_rowsOrCols.RemoveAt(n);
2447 m_attrs.RemoveAt(n);
758cbedf
VZ
2448 }
2449 }
2450}
2451
4d60017a
SN
2452void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
2453{
2454 size_t count = m_attrs.GetCount();
2455 for ( size_t n = 0; n < count; n++ )
2456 {
2457 int & rowOrCol = m_rowsOrCols[n];
d1c0b4f9
VZ
2458 if ( (size_t)rowOrCol >= pos )
2459 {
2460 if ( numRowsOrCols > 0 )
2461 {
2462 // If rows inserted, include row counter where necessary
2463 rowOrCol += numRowsOrCols;
2464 }
2465 else if ( numRowsOrCols < 0)
2466 {
2467 // If rows deleted, either decrement row counter (if row still exists)
2468 if ((size_t)rowOrCol >= pos - numRowsOrCols)
2469 rowOrCol += numRowsOrCols;
2470 else
2471 {
2472 m_rowsOrCols.RemoveAt((size_t)n);
2473 m_attrs.RemoveAt((size_t)n);
2474 n--; count--;
2475 }
2476 }
4d60017a
SN
2477 }
2478 }
2479}
2480
b99be8fb
VZ
2481// ----------------------------------------------------------------------------
2482// wxGridCellAttrProvider
2483// ----------------------------------------------------------------------------
2484
2485wxGridCellAttrProvider::wxGridCellAttrProvider()
2486{
2487 m_data = (wxGridCellAttrProviderData *)NULL;
2488}
2489
2490wxGridCellAttrProvider::~wxGridCellAttrProvider()
2491{
2492 delete m_data;
2493}
2494
2495void wxGridCellAttrProvider::InitData()
2496{
2497 m_data = new wxGridCellAttrProviderData;
2498}
2499
19d7140e
VZ
2500wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col,
2501 wxGridCellAttr::wxAttrKind kind ) const
b99be8fb 2502{
758cbedf
VZ
2503 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2504 if ( m_data )
2505 {
19d7140e 2506 switch(kind)
758cbedf 2507 {
19d7140e
VZ
2508 case (wxGridCellAttr::Any):
2509 //Get cached merge attributes.
2510 // Currenlty not used as no cache implemented as not mutiable
2511 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2512 if(!attr)
2513 {
2514 //Basicaly implement old version.
2515 //Also check merge cache, so we don't have to re-merge every time..
2516 wxGridCellAttr *attrcell = (wxGridCellAttr *)NULL,
2517 *attrrow = (wxGridCellAttr *)NULL,
bf7945ce
RD
2518 *attrcol = (wxGridCellAttr *)NULL;
2519
19d7140e
VZ
2520 attrcell = m_data->m_cellAttrs.GetAttr(row, col);
2521 attrcol = m_data->m_colAttrs.GetAttr(col);
2522 attrrow = m_data->m_rowAttrs.GetAttr(row);
2523
bf7945ce 2524 if((attrcell != attrrow) && (attrrow !=attrcol) && (attrcell != attrcol)){
19d7140e
VZ
2525 // Two or move are non NULL
2526 attr = new wxGridCellAttr;
2527 attr->SetKind(wxGridCellAttr::Merged);
2528
bf7945ce 2529 //Order important..
19d7140e
VZ
2530 if(attrcell){
2531 attr->MergeWith(attrcell);
2532 attrcell->DecRef();
2533 }
2534 if(attrcol){
2535 attr->MergeWith(attrcol);
2536 attrcol->DecRef();
2537 }
2538 if(attrrow){
2539 attr->MergeWith(attrrow);
2540 attrrow->DecRef();
2541 }
2542 //store merge attr if cache implemented
2543 //attr->IncRef();
2544 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2545 }
2546 else
758cbedf 2547 {
19d7140e
VZ
2548 // one or none is non null return it or null.
2549 if(attrrow) attr = attrrow;
2550 if(attrcol) attr = attrcol;
bf7945ce 2551 if(attrcell) attr = attrcell;
19d7140e
VZ
2552 }
2553 }
2554 break;
2555 case (wxGridCellAttr::Cell):
2556 attr = m_data->m_cellAttrs.GetAttr(row, col);
2557 break;
2558 case (wxGridCellAttr::Col):
2559 attr = m_data->m_colAttrs.GetAttr(col);
2560 break;
2561 case (wxGridCellAttr::Row):
758cbedf 2562 attr = m_data->m_rowAttrs.GetAttr(row);
19d7140e
VZ
2563 break;
2564 default:
2565 // unused as yet...
2566 // (wxGridCellAttr::Default):
2567 // (wxGridCellAttr::Merged):
2568 break;
758cbedf
VZ
2569 }
2570 }
758cbedf 2571 return attr;
b99be8fb
VZ
2572}
2573
2e9a6788 2574void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
b99be8fb
VZ
2575 int row, int col)
2576{
2577 if ( !m_data )
2578 InitData();
2579
758cbedf
VZ
2580 m_data->m_cellAttrs.SetAttr(attr, row, col);
2581}
2582
2583void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
2584{
2585 if ( !m_data )
2586 InitData();
2587
2588 m_data->m_rowAttrs.SetAttr(attr, row);
2589}
2590
2591void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
2592{
2593 if ( !m_data )
2594 InitData();
2595
2596 m_data->m_colAttrs.SetAttr(attr, col);
b99be8fb
VZ
2597}
2598
4d60017a
SN
2599void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
2600{
2601 if ( m_data )
2602 {
2603 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
2604
d1c0b4f9 2605 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
4d60017a
SN
2606 }
2607}
2608
2609void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
2610{
2611 if ( m_data )
2612 {
2613 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
2614
d1c0b4f9 2615 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
4d60017a
SN
2616 }
2617}
2618
f2d76237
RD
2619// ----------------------------------------------------------------------------
2620// wxGridTypeRegistry
2621// ----------------------------------------------------------------------------
2622
2623wxGridTypeRegistry::~wxGridTypeRegistry()
2624{
b94ae1ea
VZ
2625 size_t count = m_typeinfo.Count();
2626 for ( size_t i = 0; i < count; i++ )
f2d76237
RD
2627 delete m_typeinfo[i];
2628}
2629
2630
2631void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
2632 wxGridCellRenderer* renderer,
2633 wxGridCellEditor* editor)
2634{
f2d76237
RD
2635 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
2636
2637 // is it already registered?
c4608a8a 2638 int loc = FindRegisteredDataType(typeName);
39bcce60
VZ
2639 if ( loc != wxNOT_FOUND )
2640 {
f2d76237
RD
2641 delete m_typeinfo[loc];
2642 m_typeinfo[loc] = info;
2643 }
39bcce60
VZ
2644 else
2645 {
f2d76237
RD
2646 m_typeinfo.Add(info);
2647 }
2648}
2649
c4608a8a
VZ
2650int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
2651{
2652 size_t count = m_typeinfo.GetCount();
2653 for ( size_t i = 0; i < count; i++ )
2654 {
2655 if ( typeName == m_typeinfo[i]->m_typeName )
2656 {
2657 return i;
2658 }
2659 }
2660
2661 return wxNOT_FOUND;
2662}
2663
f2d76237
RD
2664int wxGridTypeRegistry::FindDataType(const wxString& typeName)
2665{
c4608a8a
VZ
2666 int index = FindRegisteredDataType(typeName);
2667 if ( index == wxNOT_FOUND )
2668 {
2669 // check whether this is one of the standard ones, in which case
2670 // register it "on the fly"
3a8c693a 2671#if wxUSE_TEXTCTRL
c4608a8a
VZ
2672 if ( typeName == wxGRID_VALUE_STRING )
2673 {
2674 RegisterDataType(wxGRID_VALUE_STRING,
2675 new wxGridCellStringRenderer,
2676 new wxGridCellTextEditor);
3a8c693a
VZ
2677 } else
2678#endif // wxUSE_TEXTCTRL
2679#if wxUSE_CHECKBOX
2680 if ( typeName == wxGRID_VALUE_BOOL )
c4608a8a
VZ
2681 {
2682 RegisterDataType(wxGRID_VALUE_BOOL,
2683 new wxGridCellBoolRenderer,
2684 new wxGridCellBoolEditor);
3a8c693a
VZ
2685 } else
2686#endif // wxUSE_CHECKBOX
2687#if wxUSE_TEXTCTRL
2688 if ( typeName == wxGRID_VALUE_NUMBER )
c4608a8a
VZ
2689 {
2690 RegisterDataType(wxGRID_VALUE_NUMBER,
2691 new wxGridCellNumberRenderer,
2692 new wxGridCellNumberEditor);
2693 }
2694 else if ( typeName == wxGRID_VALUE_FLOAT )
2695 {
2696 RegisterDataType(wxGRID_VALUE_FLOAT,
2697 new wxGridCellFloatRenderer,
2698 new wxGridCellFloatEditor);
3a8c693a
VZ
2699 } else
2700#endif // wxUSE_TEXTCTRL
2701#if wxUSE_COMBOBOX
2702 if ( typeName == wxGRID_VALUE_CHOICE )
c4608a8a
VZ
2703 {
2704 RegisterDataType(wxGRID_VALUE_CHOICE,
2705 new wxGridCellStringRenderer,
2706 new wxGridCellChoiceEditor);
3a8c693a
VZ
2707 } else
2708#endif // wxUSE_COMBOBOX
c4608a8a
VZ
2709 {
2710 return wxNOT_FOUND;
2711 }
f2d76237 2712
c4608a8a
VZ
2713 // we get here only if just added the entry for this type, so return
2714 // the last index
2715 index = m_typeinfo.GetCount() - 1;
2716 }
2717
2718 return index;
2719}
2720
2721int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
2722{
2723 int index = FindDataType(typeName);
2724 if ( index == wxNOT_FOUND )
2725 {
2726 // the first part of the typename is the "real" type, anything after ':'
2727 // are the parameters for the renderer
2728 index = FindDataType(typeName.BeforeFirst(_T(':')));
2729 if ( index == wxNOT_FOUND )
2730 {
2731 return wxNOT_FOUND;
f2d76237 2732 }
c4608a8a
VZ
2733
2734 wxGridCellRenderer *renderer = GetRenderer(index);
2735 wxGridCellRenderer *rendererOld = renderer;
2736 renderer = renderer->Clone();
2737 rendererOld->DecRef();
2738
2739 wxGridCellEditor *editor = GetEditor(index);
2740 wxGridCellEditor *editorOld = editor;
2741 editor = editor->Clone();
2742 editorOld->DecRef();
2743
2744 // do it even if there are no parameters to reset them to defaults
2745 wxString params = typeName.AfterFirst(_T(':'));
2746 renderer->SetParameters(params);
2747 editor->SetParameters(params);
2748
2749 // register the new typename
c4608a8a
VZ
2750 RegisterDataType(typeName, renderer, editor);
2751
2752 // we just registered it, it's the last one
2753 index = m_typeinfo.GetCount() - 1;
f2d76237
RD
2754 }
2755
c4608a8a 2756 return index;
f2d76237
RD
2757}
2758
2759wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
2760{
2761 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
faec5a43
SN
2762 if (renderer)
2763 renderer->IncRef();
f2d76237
RD
2764 return renderer;
2765}
2766
0b190b0f 2767wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
f2d76237
RD
2768{
2769 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
faec5a43
SN
2770 if (editor)
2771 editor->IncRef();
f2d76237
RD
2772 return editor;
2773}
2774
758cbedf
VZ
2775// ----------------------------------------------------------------------------
2776// wxGridTableBase
2777// ----------------------------------------------------------------------------
2778
f85afd4e
MB
2779IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
2780
2781
2782wxGridTableBase::wxGridTableBase()
f85afd4e
MB
2783{
2784 m_view = (wxGrid *) NULL;
b99be8fb 2785 m_attrProvider = (wxGridCellAttrProvider *) NULL;
f85afd4e
MB
2786}
2787
2788wxGridTableBase::~wxGridTableBase()
2789{
b99be8fb
VZ
2790 delete m_attrProvider;
2791}
2792
2793void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
2794{
2795 delete m_attrProvider;
2796 m_attrProvider = attrProvider;
f85afd4e
MB
2797}
2798
f2d76237
RD
2799bool wxGridTableBase::CanHaveAttributes()
2800{
2801 if ( ! GetAttrProvider() )
2802 {
2803 // use the default attr provider by default
2804 SetAttrProvider(new wxGridCellAttrProvider);
2805 }
2806 return TRUE;
2807}
2808
19d7140e 2809wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind)
b99be8fb
VZ
2810{
2811 if ( m_attrProvider )
19d7140e 2812 return m_attrProvider->GetAttr(row, col, kind);
b99be8fb
VZ
2813 else
2814 return (wxGridCellAttr *)NULL;
2815}
2816
758cbedf 2817void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
b99be8fb
VZ
2818{
2819 if ( m_attrProvider )
2820 {
19d7140e 2821 attr->SetKind(wxGridCellAttr::Cell);
b99be8fb
VZ
2822 m_attrProvider->SetAttr(attr, row, col);
2823 }
2824 else
2825 {
2826 // as we take ownership of the pointer and don't store it, we must
2827 // free it now
39bcce60 2828 wxSafeDecRef(attr);
b99be8fb
VZ
2829 }
2830}
2831
758cbedf
VZ
2832void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
2833{
2834 if ( m_attrProvider )
2835 {
19d7140e 2836 attr->SetKind(wxGridCellAttr::Row);
758cbedf
VZ
2837 m_attrProvider->SetRowAttr(attr, row);
2838 }
2839 else
2840 {
2841 // as we take ownership of the pointer and don't store it, we must
2842 // free it now
39bcce60 2843 wxSafeDecRef(attr);
758cbedf
VZ
2844 }
2845}
2846
2847void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
2848{
2849 if ( m_attrProvider )
2850 {
19d7140e 2851 attr->SetKind(wxGridCellAttr::Col);
758cbedf
VZ
2852 m_attrProvider->SetColAttr(attr, col);
2853 }
2854 else
2855 {
2856 // as we take ownership of the pointer and don't store it, we must
2857 // free it now
39bcce60 2858 wxSafeDecRef(attr);
758cbedf
VZ
2859 }
2860}
2861
aa5e1f75
SN
2862bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
2863 size_t WXUNUSED(numRows) )
f85afd4e 2864{
f6bcfd97 2865 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
8f177c8e 2866
f85afd4e
MB
2867 return FALSE;
2868}
2869
aa5e1f75 2870bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
f85afd4e 2871{
f6bcfd97 2872 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
8f177c8e 2873
f85afd4e
MB
2874 return FALSE;
2875}
2876
aa5e1f75
SN
2877bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
2878 size_t WXUNUSED(numRows) )
f85afd4e 2879{
f6bcfd97 2880 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
8f177c8e 2881
f85afd4e
MB
2882 return FALSE;
2883}
2884
aa5e1f75
SN
2885bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
2886 size_t WXUNUSED(numCols) )
f85afd4e 2887{
f6bcfd97 2888 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
8f177c8e 2889
f85afd4e
MB
2890 return FALSE;
2891}
2892
aa5e1f75 2893bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
f85afd4e 2894{
f6bcfd97 2895 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
8f177c8e 2896
f85afd4e
MB
2897 return FALSE;
2898}
2899
aa5e1f75
SN
2900bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
2901 size_t WXUNUSED(numCols) )
f85afd4e 2902{
f6bcfd97 2903 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
8f177c8e 2904
f85afd4e
MB
2905 return FALSE;
2906}
2907
2908
2909wxString wxGridTableBase::GetRowLabelValue( int row )
2910{
2911 wxString s;
f2d76237
RD
2912 s << row + 1; // RD: Starting the rows at zero confuses users, no matter
2913 // how much it makes sense to us geeks.
f85afd4e
MB
2914 return s;
2915}
2916
2917wxString wxGridTableBase::GetColLabelValue( int col )
2918{
2919 // default col labels are:
2920 // cols 0 to 25 : A-Z
2921 // cols 26 to 675 : AA-ZZ
2922 // etc.
2923
2924 wxString s;
2925 unsigned int i, n;
2926 for ( n = 1; ; n++ )
2927 {
f0102d2a 2928 s += (_T('A') + (wxChar)( col%26 ));
f85afd4e
MB
2929 col = col/26 - 1;
2930 if ( col < 0 ) break;
2931 }
2932
2933 // reverse the string...
2934 wxString s2;
2935 for ( i = 0; i < n; i++ )
2936 {
2937 s2 += s[n-i-1];
2938 }
2939
2940 return s2;
2941}
2942
2943
f2d76237
RD
2944wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
2945{
816be743 2946 return wxGRID_VALUE_STRING;
f2d76237
RD
2947}
2948
2949bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
2950 const wxString& typeName )
2951{
816be743 2952 return typeName == wxGRID_VALUE_STRING;
f2d76237
RD
2953}
2954
2955bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
2956{
2957 return CanGetValueAs(row, col, typeName);
2958}
2959
2960long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
2961{
2962 return 0;
2963}
2964
2965double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
2966{
2967 return 0.0;
2968}
2969
2970bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
2971{
2972 return FALSE;
2973}
2974
2975void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
2976 long WXUNUSED(value) )
2977{
2978}
2979
2980void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
2981 double WXUNUSED(value) )
2982{
2983}
2984
2985void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
2986 bool WXUNUSED(value) )
2987{
2988}
2989
2990
2991void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
2992 const wxString& WXUNUSED(typeName) )
2993{
2994 return NULL;
2995}
2996
2997void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
2998 const wxString& WXUNUSED(typeName),
2999 void* WXUNUSED(value) )
3000{
3001}
3002
f85afd4e
MB
3003//////////////////////////////////////////////////////////////////////
3004//
3005// Message class for the grid table to send requests and notifications
3006// to the grid view
3007//
3008
3009wxGridTableMessage::wxGridTableMessage()
3010{
3011 m_table = (wxGridTableBase *) NULL;
3012 m_id = -1;
3013 m_comInt1 = -1;
3014 m_comInt2 = -1;
3015}
3016
3017wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
3018 int commandInt1, int commandInt2 )
3019{
3020 m_table = table;
3021 m_id = id;
3022 m_comInt1 = commandInt1;
3023 m_comInt2 = commandInt2;
3024}
3025
3026
3027
3028//////////////////////////////////////////////////////////////////////
3029//
3030// A basic grid table for string data. An object of this class will
3031// created by wxGrid if you don't specify an alternative table class.
3032//
3033
223d09f6 3034WX_DEFINE_OBJARRAY(wxGridStringArray)
f85afd4e
MB
3035
3036IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
3037
3038wxGridStringTable::wxGridStringTable()
3039 : wxGridTableBase()
3040{
3041}
3042
3043wxGridStringTable::wxGridStringTable( int numRows, int numCols )
3044 : wxGridTableBase()
3045{
f85afd4e
MB
3046 m_data.Alloc( numRows );
3047
3048 wxArrayString sa;
3049 sa.Alloc( numCols );
27f35b66 3050 sa.Add( wxEmptyString, numCols );
8f177c8e 3051
27f35b66 3052 m_data.Add( sa, numRows );
f85afd4e
MB
3053}
3054
3055wxGridStringTable::~wxGridStringTable()
3056{
3057}
3058
e32352cf 3059int wxGridStringTable::GetNumberRows()
f85afd4e
MB
3060{
3061 return m_data.GetCount();
3062}
3063
e32352cf 3064int wxGridStringTable::GetNumberCols()
f85afd4e
MB
3065{
3066 if ( m_data.GetCount() > 0 )
3067 return m_data[0].GetCount();
3068 else
3069 return 0;
3070}
3071
3072wxString wxGridStringTable::GetValue( int row, int col )
3073{
831243b1 3074 wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
af547d51
VZ
3075 _T("invalid row or column index in wxGridStringTable") );
3076
f85afd4e
MB
3077 return m_data[row][col];
3078}
3079
f2d76237 3080void wxGridStringTable::SetValue( int row, int col, const wxString& value )
f85afd4e 3081{
831243b1 3082 wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
af547d51
VZ
3083 _T("invalid row or column index in wxGridStringTable") );
3084
f2d76237 3085 m_data[row][col] = value;
f85afd4e
MB
3086}
3087
3088bool wxGridStringTable::IsEmptyCell( int row, int col )
3089{
831243b1 3090 wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
af547d51
VZ
3091 _T("invalid row or column index in wxGridStringTable") );
3092
f85afd4e
MB
3093 return (m_data[row][col] == wxEmptyString);
3094}
3095
f85afd4e
MB
3096void wxGridStringTable::Clear()
3097{
3098 int row, col;
3099 int numRows, numCols;
8f177c8e 3100
f85afd4e
MB
3101 numRows = m_data.GetCount();
3102 if ( numRows > 0 )
3103 {
3104 numCols = m_data[0].GetCount();
3105
3106 for ( row = 0; row < numRows; row++ )
3107 {
3108 for ( col = 0; col < numCols; col++ )
3109 {
3110 m_data[row][col] = wxEmptyString;
3111 }
3112 }
3113 }
3114}
3115
3116
3117bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
3118{
f85afd4e 3119 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3120 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3121 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3122
f85afd4e
MB
3123 if ( pos >= curNumRows )
3124 {
3125 return AppendRows( numRows );
3126 }
8f177c8e 3127
f85afd4e
MB
3128 wxArrayString sa;
3129 sa.Alloc( curNumCols );
27f35b66
SN
3130 sa.Add( wxEmptyString, curNumCols );
3131 m_data.Insert( sa, pos, numRows );
f85afd4e
MB
3132 if ( GetView() )
3133 {
3134 wxGridTableMessage msg( this,
3135 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
3136 pos,
3137 numRows );
8f177c8e 3138
f85afd4e
MB
3139 GetView()->ProcessTableMessage( msg );
3140 }
3141
3142 return TRUE;
3143}
3144
3145bool wxGridStringTable::AppendRows( size_t numRows )
3146{
f85afd4e 3147 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3148 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3149 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3150
f85afd4e
MB
3151 wxArrayString sa;
3152 if ( curNumCols > 0 )
3153 {
3154 sa.Alloc( curNumCols );
27f35b66 3155 sa.Add( wxEmptyString, curNumCols );
f85afd4e 3156 }
8f177c8e 3157
27f35b66 3158 m_data.Add( sa, numRows );
f85afd4e
MB
3159
3160 if ( GetView() )
3161 {
3162 wxGridTableMessage msg( this,
3163 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
3164 numRows );
8f177c8e 3165
f85afd4e
MB
3166 GetView()->ProcessTableMessage( msg );
3167 }
3168
8f177c8e 3169 return TRUE;
f85afd4e
MB
3170}
3171
3172bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
3173{
f85afd4e 3174 size_t curNumRows = m_data.GetCount();
8f177c8e 3175
f85afd4e
MB
3176 if ( pos >= curNumRows )
3177 {
e91d2033
VZ
3178 wxFAIL_MSG( wxString::Format
3179 (
3180 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3181 (unsigned long)pos,
3182 (unsigned long)numRows,
3183 (unsigned long)curNumRows
3184 ) );
3185
f85afd4e
MB
3186 return FALSE;
3187 }
3188
3189 if ( numRows > curNumRows - pos )
3190 {
3191 numRows = curNumRows - pos;
3192 }
8f177c8e 3193
f85afd4e
MB
3194 if ( numRows >= curNumRows )
3195 {
d57ad377 3196 m_data.Clear();
f85afd4e
MB
3197 }
3198 else
3199 {
27f35b66 3200 m_data.RemoveAt( pos, numRows );
f85afd4e 3201 }
f85afd4e
MB
3202 if ( GetView() )
3203 {
3204 wxGridTableMessage msg( this,
3205 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
3206 pos,
3207 numRows );
8f177c8e 3208
f85afd4e
MB
3209 GetView()->ProcessTableMessage( msg );
3210 }
3211
8f177c8e 3212 return TRUE;
f85afd4e
MB
3213}
3214
3215bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
3216{
3217 size_t row, col;
3218
3219 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3220 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3221 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3222
f85afd4e
MB
3223 if ( pos >= curNumCols )
3224 {
3225 return AppendCols( numCols );
3226 }
3227
3228 for ( row = 0; row < curNumRows; row++ )
3229 {
3230 for ( col = pos; col < pos + numCols; col++ )
3231 {
3232 m_data[row].Insert( wxEmptyString, col );
3233 }
3234 }
f85afd4e
MB
3235 if ( GetView() )
3236 {
3237 wxGridTableMessage msg( this,
3238 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
3239 pos,
3240 numCols );
8f177c8e 3241
f85afd4e
MB
3242 GetView()->ProcessTableMessage( msg );
3243 }
3244
3245 return TRUE;
3246}
3247
3248bool wxGridStringTable::AppendCols( size_t numCols )
3249{
27f35b66 3250 size_t row;
f85afd4e
MB
3251
3252 size_t curNumRows = m_data.GetCount();
f6bcfd97 3253#if 0
f85afd4e
MB
3254 if ( !curNumRows )
3255 {
3256 // TODO: something better than this ?
3257 //
f6bcfd97 3258 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
f85afd4e
MB
3259 return FALSE;
3260 }
f6bcfd97 3261#endif
8f177c8e 3262
f85afd4e
MB
3263 for ( row = 0; row < curNumRows; row++ )
3264 {
27f35b66 3265 m_data[row].Add( wxEmptyString, numCols );
f85afd4e
MB
3266 }
3267
3268 if ( GetView() )
3269 {
3270 wxGridTableMessage msg( this,
3271 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
3272 numCols );
8f177c8e 3273
f85afd4e
MB
3274 GetView()->ProcessTableMessage( msg );
3275 }
3276
3277 return TRUE;
3278}
3279
3280bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
3281{
27f35b66 3282 size_t row;
f85afd4e
MB
3283
3284 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3285 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3286 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3287
f85afd4e
MB
3288 if ( pos >= curNumCols )
3289 {
e91d2033
VZ
3290 wxFAIL_MSG( wxString::Format
3291 (
3292 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3293 (unsigned long)pos,
3294 (unsigned long)numCols,
3295 (unsigned long)curNumCols
3296 ) );
8f177c8e 3297 return FALSE;
f85afd4e
MB
3298 }
3299
3300 if ( numCols > curNumCols - pos )
3301 {
8f177c8e 3302 numCols = curNumCols - pos;
f85afd4e
MB
3303 }
3304
3305 for ( row = 0; row < curNumRows; row++ )
3306 {
3307 if ( numCols >= curNumCols )
3308 {
dcdce64e 3309 m_data[row].Clear();
f85afd4e
MB
3310 }
3311 else
3312 {
27f35b66 3313 m_data[row].RemoveAt( pos, numCols );
f85afd4e
MB
3314 }
3315 }
f85afd4e
MB
3316 if ( GetView() )
3317 {
3318 wxGridTableMessage msg( this,
3319 wxGRIDTABLE_NOTIFY_COLS_DELETED,
3320 pos,
3321 numCols );
8f177c8e 3322
f85afd4e
MB
3323 GetView()->ProcessTableMessage( msg );
3324 }
3325
8f177c8e 3326 return TRUE;
f85afd4e
MB
3327}
3328
3329wxString wxGridStringTable::GetRowLabelValue( int row )
3330{
3331 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3332 {
3333 // using default label
3334 //
3335 return wxGridTableBase::GetRowLabelValue( row );
3336 }
3337 else
3338 {
3339 return m_rowLabels[ row ];
3340 }
3341}
3342
3343wxString wxGridStringTable::GetColLabelValue( int col )
3344{
3345 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3346 {
3347 // using default label
3348 //
3349 return wxGridTableBase::GetColLabelValue( col );
3350 }
3351 else
3352 {
3353 return m_colLabels[ col ];
3354 }
3355}
3356
3357void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
3358{
3359 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3360 {
3361 int n = m_rowLabels.GetCount();
3362 int i;
3363 for ( i = n; i <= row; i++ )
3364 {
3365 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
3366 }
3367 }
3368
3369 m_rowLabels[row] = value;
3370}
3371
3372void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
3373{
3374 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3375 {
3376 int n = m_colLabels.GetCount();
3377 int i;
3378 for ( i = n; i <= col; i++ )
3379 {
3380 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
3381 }
3382 }
3383
3384 m_colLabels[col] = value;
3385}
3386
3387
3388
f85afd4e 3389//////////////////////////////////////////////////////////////////////
2d66e025
MB
3390//////////////////////////////////////////////////////////////////////
3391
3392IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
3393
3394BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow )
3395 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
b51c3f27 3396 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel)
2d66e025
MB
3397 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
3398 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown )
f6bcfd97 3399 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp )
2d66e025
MB
3400END_EVENT_TABLE()
3401
60ff3b99
VZ
3402wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
3403 wxWindowID id,
2d66e025 3404 const wxPoint &pos, const wxSize &size )
ebd773c6 3405 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2d66e025
MB
3406{
3407 m_owner = parent;
3408}
3409
aa5e1f75 3410void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3411{
3412 wxPaintDC dc(this);
3413
3414 // NO - don't do this because it will set both the x and y origin
3415 // coords to match the parent scrolled window and we just want to
3416 // set the y coord - MB
3417 //
3418 // m_owner->PrepareDC( dc );
60ff3b99 3419
790ad94f 3420 int x, y;
2d66e025
MB
3421 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3422 dc.SetDeviceOrigin( 0, -y );
60ff3b99 3423
d10f4bf9
VZ
3424 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
3425 m_owner->DrawRowLabels( dc , rows );
2d66e025
MB
3426}
3427
3428
3429void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
3430{
3431 m_owner->ProcessRowLabelMouseEvent( event );
3432}
3433
3434
b51c3f27
RD
3435void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
3436{
3437 m_owner->GetEventHandler()->ProcessEvent(event);
3438}
3439
3440
2d66e025
MB
3441// This seems to be required for wxMotif otherwise the mouse
3442// cursor must be in the cell edit control to get key events
3443//
3444void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
3445{
ffdd3c98 3446 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3447}
3448
f6bcfd97
BP
3449void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event )
3450{
ffdd3c98 3451 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3452}
3453
2d66e025
MB
3454
3455
3456//////////////////////////////////////////////////////////////////////
3457
3458IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
3459
3460BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
3461 EVT_PAINT( wxGridColLabelWindow::OnPaint )
b51c3f27 3462 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel)
2d66e025
MB
3463 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
3464 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
f6bcfd97 3465 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp )
2d66e025
MB
3466END_EVENT_TABLE()
3467
60ff3b99
VZ
3468wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
3469 wxWindowID id,
2d66e025 3470 const wxPoint &pos, const wxSize &size )
ebd773c6 3471 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2d66e025
MB
3472{
3473 m_owner = parent;
3474}
3475
aa5e1f75 3476void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3477{
3478 wxPaintDC dc(this);
3479
3480 // NO - don't do this because it will set both the x and y origin
3481 // coords to match the parent scrolled window and we just want to
3482 // set the x coord - MB
3483 //
3484 // m_owner->PrepareDC( dc );
60ff3b99 3485
790ad94f 3486 int x, y;
2d66e025
MB
3487 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3488 dc.SetDeviceOrigin( -x, 0 );
3489
d10f4bf9
VZ
3490 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
3491 m_owner->DrawColLabels( dc , cols );
2d66e025
MB
3492}
3493
3494
3495void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
3496{
3497 m_owner->ProcessColLabelMouseEvent( event );
3498}
3499
b51c3f27
RD
3500void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
3501{
3502 m_owner->GetEventHandler()->ProcessEvent(event);
3503}
3504
2d66e025
MB
3505
3506// This seems to be required for wxMotif otherwise the mouse
3507// cursor must be in the cell edit control to get key events
3508//
3509void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
3510{
ffdd3c98 3511 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3512}
3513
f6bcfd97
BP
3514void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event )
3515{
ffdd3c98 3516 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3517}
3518
2d66e025
MB
3519
3520
3521//////////////////////////////////////////////////////////////////////
3522
3523IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
3524
3525BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
b51c3f27 3526 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel)
2d66e025 3527 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
d2fdd8d2 3528 EVT_PAINT( wxGridCornerLabelWindow::OnPaint)
2d66e025 3529 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
f6bcfd97 3530 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp )
2d66e025
MB
3531END_EVENT_TABLE()
3532
60ff3b99
VZ
3533wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
3534 wxWindowID id,
2d66e025 3535 const wxPoint &pos, const wxSize &size )
ebd773c6 3536 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2d66e025
MB
3537{
3538 m_owner = parent;
3539}
3540
d2fdd8d2
RR
3541void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3542{
3543 wxPaintDC dc(this);
b99be8fb 3544
d2fdd8d2
RR
3545 int client_height = 0;
3546 int client_width = 0;
3547 GetClientSize( &client_width, &client_height );
b99be8fb 3548
73145b0e 3549 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
d2fdd8d2
RR
3550 dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 );
3551 dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 );
d2fdd8d2
RR
3552 dc.DrawLine( 0, 0, client_width, 0 );
3553 dc.DrawLine( 0, 0, 0, client_height );
73145b0e
JS
3554
3555 dc.SetPen( *wxWHITE_PEN );
3556 dc.DrawLine( 1, 1, client_width-1, 1 );
3557 dc.DrawLine( 1, 1, 1, client_height-1 );
d2fdd8d2
RR
3558}
3559
2d66e025
MB
3560
3561void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
3562{
3563 m_owner->ProcessCornerLabelMouseEvent( event );
3564}
3565
3566
b51c3f27
RD
3567void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
3568{
3569 m_owner->GetEventHandler()->ProcessEvent(event);
3570}
3571
2d66e025
MB
3572// This seems to be required for wxMotif otherwise the mouse
3573// cursor must be in the cell edit control to get key events
3574//
3575void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
3576{
ffdd3c98 3577 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3578}
3579
f6bcfd97
BP
3580void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event )
3581{
ffdd3c98 3582 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3583}
3584
2d66e025
MB
3585
3586
f85afd4e
MB
3587//////////////////////////////////////////////////////////////////////
3588
59ddac01 3589IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow )
2d66e025 3590
59ddac01 3591BEGIN_EVENT_TABLE( wxGridWindow, wxWindow )
2d66e025 3592 EVT_PAINT( wxGridWindow::OnPaint )
b51c3f27 3593 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel)
2d66e025
MB
3594 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
3595 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
f6bcfd97 3596 EVT_KEY_UP( wxGridWindow::OnKeyUp )
2796cce3 3597 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2d66e025
MB
3598END_EVENT_TABLE()
3599
73145b0e 3600// DJC (MAPTEK) 19-Jun-2001 use wxCLIP_CHILDREN as well
60ff3b99
VZ
3601wxGridWindow::wxGridWindow( wxGrid *parent,
3602 wxGridRowLabelWindow *rowLblWin,
2d66e025
MB
3603 wxGridColLabelWindow *colLblWin,
3604 wxWindowID id, const wxPoint &pos, const wxSize &size )
73145b0e
JS
3605 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxCLIP_CHILDREN, wxT("grid window") )
3606
2d66e025
MB
3607{
3608 m_owner = parent;
3609 m_rowLabelWin = rowLblWin;
3610 m_colLabelWin = colLblWin;
edb89f7e 3611 SetBackgroundColour(_T("WHITE"));
2d66e025
MB
3612}
3613
3614
3615wxGridWindow::~wxGridWindow()
3616{
3617}
3618
3619
3620void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3621{
3622 wxPaintDC dc( this );
3623 m_owner->PrepareDC( dc );
796df70a 3624 wxRegion reg = GetUpdateRegion();
d10f4bf9
VZ
3625 wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg );
3626 m_owner->DrawGridCellArea( dc , DirtyCells);
9496deb5 3627#if WXGRID_DRAW_LINES
796df70a
SN
3628 m_owner->DrawAllGridLines( dc, reg );
3629#endif
a5777624 3630 m_owner->DrawGridSpace( dc );
d10f4bf9 3631 m_owner->DrawHighlight( dc , DirtyCells );
2d66e025
MB
3632}
3633
3634
3635void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3636{
59ddac01 3637 wxWindow::ScrollWindow( dx, dy, rect );
2d66e025
MB
3638 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3639 m_colLabelWin->ScrollWindow( dx, 0, rect );
3640}
3641
3642
3643void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3644{
3645 m_owner->ProcessGridCellMouseEvent( event );
3646}
3647
b51c3f27
RD
3648void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
3649{
3650 m_owner->GetEventHandler()->ProcessEvent(event);
3651}
2d66e025 3652
f6bcfd97 3653// This seems to be required for wxMotif/wxGTK otherwise the mouse
2d66e025
MB
3654// cursor must be in the cell edit control to get key events
3655//
3656void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3657{
ffdd3c98 3658 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025 3659}
f85afd4e 3660
f6bcfd97
BP
3661void wxGridWindow::OnKeyUp( wxKeyEvent& event )
3662{
ffdd3c98 3663 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97 3664}
7c8a8ad5 3665
aa5e1f75 3666void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8dd4f536 3667{
8dd4f536 3668}
025562fe 3669
2d66e025
MB
3670
3671//////////////////////////////////////////////////////////////////////
3672
33188aa4
SN
3673// Internal Helper function for computing row or column from some
3674// (unscrolled) coordinate value, using either
70e8d961 3675// m_defaultRowHeight/m_defaultColWidth or binary search on array
33188aa4
SN
3676// of m_rowBottoms/m_ColRights to speed up the search!
3677
3678// Internal helper macros for simpler use of that function
3679
3680static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
64e15340
JS
3681 const wxArrayInt& BorderArray, int nMax,
3682 bool maxOnOverflow);
33188aa4
SN
3683
3684#define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3685 WXGRID_MIN_COL_WIDTH, \
64e15340 3686 m_colRights, m_numCols, TRUE)
33188aa4
SN
3687#define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3688 WXGRID_MIN_ROW_HEIGHT, \
64e15340 3689 m_rowBottoms, m_numRows, TRUE)
33188aa4 3690/////////////////////////////////////////////////////////////////////
07296f0b 3691
2d66e025
MB
3692IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
3693
3694BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
f85afd4e
MB
3695 EVT_PAINT( wxGrid::OnPaint )
3696 EVT_SIZE( wxGrid::OnSize )
f85afd4e 3697 EVT_KEY_DOWN( wxGrid::OnKeyDown )
f6bcfd97 3698 EVT_KEY_UP( wxGrid::OnKeyUp )
2796cce3 3699 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
f85afd4e 3700END_EVENT_TABLE()
8f177c8e 3701
2d66e025
MB
3702wxGrid::wxGrid( wxWindow *parent,
3703 wxWindowID id,
3704 const wxPoint& pos,
3705 const wxSize& size,
3706 long style,
3707 const wxString& name )
ebd773c6 3708 : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
af547d51
VZ
3709 m_colMinWidths(GRID_HASH_SIZE),
3710 m_rowMinHeights(GRID_HASH_SIZE)
2d66e025
MB
3711{
3712 Create();
58dd5b3b
MB
3713}
3714
3715
3716wxGrid::~wxGrid()
3717{
606b005f
JS
3718 // Must do this or ~wxScrollHelper will pop the wrong event handler
3719 SetTargetWindow(this);
0a976765 3720 ClearAttrCache();
39bcce60 3721 wxSafeDecRef(m_defaultCellAttr);
0a976765
VZ
3722
3723#ifdef DEBUG_ATTR_CACHE
3724 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
3725 wxPrintf(_T("wxGrid attribute cache statistics: "
3726 "total: %u, hits: %u (%u%%)\n"),
3727 total, gs_nAttrCacheHits,
3728 total ? (gs_nAttrCacheHits*100) / total : 0);
3729#endif
3730
2796cce3
RD
3731 if (m_ownTable)
3732 delete m_table;
f2d76237
RD
3733
3734 delete m_typeRegistry;
b5808881 3735 delete m_selection;
58dd5b3b
MB
3736}
3737
2d66e025 3738
58dd5b3b
MB
3739//
3740// ----- internal init and update functions
3741//
3742
3743void wxGrid::Create()
f0102d2a 3744{
4634a5d6 3745 m_created = FALSE; // set to TRUE by CreateGrid
4634a5d6
MB
3746
3747 m_table = (wxGridTableBase *) NULL;
2796cce3 3748 m_ownTable = FALSE;
2c9a89e0
RD
3749
3750 m_cellEditCtrlEnabled = FALSE;
4634a5d6 3751
ccd970b1 3752 m_defaultCellAttr = new wxGridCellAttr();
f2d76237
RD
3753
3754 // Set default cell attributes
ccd970b1 3755 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
19d7140e 3756 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
f2d76237 3757 m_defaultCellAttr->SetFont(GetFont());
4c7277db 3758 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
f2d76237 3759 m_defaultCellAttr->SetTextColour(
a756f210 3760 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
f2d76237 3761 m_defaultCellAttr->SetBackgroundColour(
a756f210 3762 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f2d76237
RD
3763 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
3764 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
2796cce3
RD
3765
3766
4634a5d6
MB
3767 m_numRows = 0;
3768 m_numCols = 0;
3769 m_currentCellCoords = wxGridNoCellCoords;
b99be8fb 3770
18f9565d
MB
3771 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3772 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2d66e025 3773
c4608a8a 3774 // create the type registry
f2d76237 3775 m_typeRegistry = new wxGridTypeRegistry;
3f3dc2ef
VZ
3776 m_selection = NULL;
3777
f2d76237 3778 // subwindow components that make up the wxGrid
7807d81c
MB
3779 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
3780 -1,
18f9565d
MB
3781 wxDefaultPosition,
3782 wxDefaultSize );
7807d81c 3783
60ff3b99
VZ
3784 m_rowLabelWin = new wxGridRowLabelWindow( this,
3785 -1,
18f9565d
MB
3786 wxDefaultPosition,
3787 wxDefaultSize );
2d66e025
MB
3788
3789 m_colLabelWin = new wxGridColLabelWindow( this,
3790 -1,
18f9565d
MB
3791 wxDefaultPosition,
3792 wxDefaultSize );
60ff3b99 3793
60ff3b99
VZ
3794 m_gridWin = new wxGridWindow( this,
3795 m_rowLabelWin,
3796 m_colLabelWin,
3797 -1,
18f9565d 3798 wxDefaultPosition,
2d66e025
MB
3799 wxDefaultSize );
3800
3801 SetTargetWindow( m_gridWin );
6f36917b
VZ
3802
3803 Init();
2d66e025 3804}
f85afd4e 3805
2d66e025 3806
b5808881
SN
3807bool wxGrid::CreateGrid( int numRows, int numCols,
3808 wxGrid::wxGridSelectionModes selmode )
2d66e025 3809{
f6bcfd97
BP
3810 wxCHECK_MSG( !m_created,
3811 FALSE,
3812 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3813
3814 m_numRows = numRows;
3815 m_numCols = numCols;
3816
3817 m_table = new wxGridStringTable( m_numRows, m_numCols );
3818 m_table->SetView( this );
3819 m_ownTable = TRUE;
3820 m_selection = new wxGridSelection( this, selmode );
6f36917b
VZ
3821
3822 CalcDimensions();
3823
f6bcfd97 3824 m_created = TRUE;
2d66e025 3825
2796cce3
RD
3826 return m_created;
3827}
3828
f1567cdd
SN
3829void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
3830{
6f36917b
VZ
3831 wxCHECK_RET( m_created,
3832 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3833
3834 m_selection->SetSelectionMode( selmode );
f1567cdd
SN
3835}
3836
aa5b8857
SN
3837wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
3838{
3839 wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells,
3840 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3841
3842 return m_selection->GetSelectionMode();
3843}
3844
043d16b2
SN
3845bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
3846 wxGrid::wxGridSelectionModes selmode )
2796cce3
RD
3847{
3848 if ( m_created )
3849 {
fb295790 3850 // RD: Actually, this should probably be allowed. I think it would be
3f3dc2ef
VZ
3851 // nice to be able to switch multiple Tables in and out of a single
3852 // View at runtime. Is there anything in the implementation that
3853 // would prevent this?
fb295790 3854
d95b0c2b 3855 // At least, you now have to cope with m_selection
2796cce3
RD
3856 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3857 return FALSE;
3858 }
3859 else
3860 {
3861 m_numRows = table->GetNumberRows();
3862 m_numCols = table->GetNumberCols();
3863
3864 m_table = table;
3865 m_table->SetView( this );
3866 if (takeOwnership)
3867 m_ownTable = TRUE;
043d16b2 3868 m_selection = new wxGridSelection( this, selmode );
6f36917b
VZ
3869
3870 CalcDimensions();
3871
2d66e025
MB
3872 m_created = TRUE;
3873 }
f85afd4e 3874
2d66e025 3875 return m_created;
f85afd4e
MB
3876}
3877
2d66e025 3878
f85afd4e
MB
3879void wxGrid::Init()
3880{
f85afd4e
MB
3881 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3882 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3883
60ff3b99
VZ
3884 if ( m_rowLabelWin )
3885 {
3886 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
3887 }
3888 else
3889 {
3890 m_labelBackgroundColour = wxColour( _T("WHITE") );
3891 }
3892
3893 m_labelTextColour = wxColour( _T("BLACK") );
f85afd4e 3894
0a976765
VZ
3895 // init attr cache
3896 m_attrCache.row = -1;
2b5f62a0
VZ
3897 m_attrCache.col = -1;
3898 m_attrCache.attr = NULL;
0a976765 3899
f85afd4e
MB
3900 // TODO: something better than this ?
3901 //
3902 m_labelFont = this->GetFont();
73145b0e
JS
3903// m_labelFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
3904// m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
8f177c8e 3905
73145b0e 3906 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4c7277db 3907 m_rowLabelVertAlign = wxALIGN_CENTRE;
f85afd4e 3908
4c7277db 3909 m_colLabelHorizAlign = wxALIGN_CENTRE;
73145b0e 3910 m_colLabelVertAlign = wxALIGN_CENTRE;
f85afd4e 3911
f85afd4e 3912 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
1f1ce288
MB
3913 m_defaultRowHeight = m_gridWin->GetCharHeight();
3914
d2fdd8d2 3915#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1f1ce288
MB
3916 m_defaultRowHeight += 8;
3917#else
3918 m_defaultRowHeight += 4;
3919#endif
3920
73145b0e 3921 m_gridLineColour = wxColour( 192,192,192 );
f85afd4e 3922 m_gridLinesEnabled = TRUE;
73145b0e 3923 m_cellHighlightColour = *wxBLACK;
bf7945ce 3924 m_cellHighlightPenWidth = 2;
d2520c85 3925 m_cellHighlightROPenWidth = 1;
8f177c8e 3926
58dd5b3b 3927 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
e2b42eeb 3928 m_winCapture = (wxWindow *)NULL;
6e8524b1
MB
3929 m_canDragRowSize = TRUE;
3930 m_canDragColSize = TRUE;
4cfa5de6 3931 m_canDragGridSize = TRUE;
f85afd4e
MB
3932 m_dragLastPos = -1;
3933 m_dragRowOrCol = -1;
3934 m_isDragging = FALSE;
07296f0b 3935 m_startDragPos = wxDefaultPosition;
f85afd4e 3936
07296f0b 3937 m_waitForSlowClick = FALSE;
025562fe 3938
f85afd4e
MB
3939 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
3940 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
3941
3942 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 3943
b5808881
SN
3944 m_selectingTopLeft = wxGridNoCellCoords;
3945 m_selectingBottomRight = wxGridNoCellCoords;
73145b0e
JS
3946// m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
3947// m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
3948 m_selectionBackground = *wxBLACK;
3949 m_selectionForeground = *wxWHITE;
8f177c8e 3950
f85afd4e
MB
3951 m_editable = TRUE; // default for whole grid
3952
2d66e025
MB
3953 m_inOnKeyDown = FALSE;
3954 m_batchCount = 0;
3c79cf49 3955
266e8367 3956 m_extraWidth =
526dbb95 3957 m_extraHeight = 0;
7c1cb261
VZ
3958}
3959
3960// ----------------------------------------------------------------------------
3961// the idea is to call these functions only when necessary because they create
3962// quite big arrays which eat memory mostly unnecessary - in particular, if
3963// default widths/heights are used for all rows/columns, we may not use these
3964// arrays at all
3965//
3966// with some extra code, it should be possible to only store the
3967// widths/heights different from default ones but this will be done later...
3968// ----------------------------------------------------------------------------
3969
3970void wxGrid::InitRowHeights()
3971{
3972 m_rowHeights.Empty();
3973 m_rowBottoms.Empty();
3974
3975 m_rowHeights.Alloc( m_numRows );
3976 m_rowBottoms.Alloc( m_numRows );
3977
3978 int rowBottom = 0;
2b5f62a0 3979
27f35b66
SN
3980 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
3981
7c1cb261
VZ
3982 for ( int i = 0; i < m_numRows; i++ )
3983 {
7c1cb261
VZ
3984 rowBottom += m_defaultRowHeight;
3985 m_rowBottoms.Add( rowBottom );
3986 }
3987}
3988
3989void wxGrid::InitColWidths()
3990{
3991 m_colWidths.Empty();
3992 m_colRights.Empty();
3993
3994 m_colWidths.Alloc( m_numCols );
3995 m_colRights.Alloc( m_numCols );
3996 int colRight = 0;
27f35b66
SN
3997
3998 m_colWidths.Add( m_defaultColWidth, m_numCols );
3999
7c1cb261
VZ
4000 for ( int i = 0; i < m_numCols; i++ )
4001 {
7c1cb261
VZ
4002 colRight += m_defaultColWidth;
4003 m_colRights.Add( colRight );
4004 }
4005}
4006
4007int wxGrid::GetColWidth(int col) const
4008{
4009 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4010}
4011
4012int wxGrid::GetColLeft(int col) const
4013{
4014 return m_colRights.IsEmpty() ? col * m_defaultColWidth
4015 : m_colRights[col] - m_colWidths[col];
4016}
4017
4018int wxGrid::GetColRight(int col) const
4019{
4020 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
4021 : m_colRights[col];
4022}
4023
4024int wxGrid::GetRowHeight(int row) const
4025{
4026 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
4027}
2d66e025 4028
7c1cb261
VZ
4029int wxGrid::GetRowTop(int row) const
4030{
4031 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
4032 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
4033}
4034
7c1cb261
VZ
4035int wxGrid::GetRowBottom(int row) const
4036{
4037 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
4038 : m_rowBottoms[row];
4039}
f85afd4e
MB
4040
4041void wxGrid::CalcDimensions()
4042{
f85afd4e 4043 int cw, ch;
2d66e025 4044 GetClientSize( &cw, &ch );
f85afd4e 4045
faec5a43 4046 if ( m_rowLabelWin->IsShown() )
ae1d0c6c
VZ
4047 cw -= m_rowLabelWidth;
4048 if ( m_colLabelWin->IsShown() )
faec5a43 4049 ch -= m_colLabelHeight;
60ff3b99 4050
faec5a43
SN
4051 // grid total size
4052 int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0;
4053 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
4054
73145b0e
JS
4055// DJC (MAPTEK) 19-Jun-2001 account for editor since it could possibly
4056// be larger than the cell
4057 // take into account editor if shown
4058 if( IsCellEditControlShown() )
4059 {
4060 int w2, h2;
4061 int r = m_currentCellCoords.GetRow();
4062 int c = m_currentCellCoords.GetCol();
4063 int x = GetColLeft(c);
4064 int y = GetRowTop(r);
4065
4066 // how big is the editor
4067 wxGridCellAttr* attr = GetCellAttr(r, c);
4068 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
4069 editor->GetControl()->GetSize(&w2, &h2);
4070 w2 += x;
4071 h2 += y;
4072 if( w2 > w ) w = w2;
4073 if( h2 > h ) h = h2;
4074 editor->DecRef();
4075 attr->DecRef();
4076 }
4077
faec5a43
SN
4078 // preserve (more or less) the previous position
4079 int x, y;
4080 GetViewStart( &x, &y );
97a9929e
VZ
4081
4082 // maybe we don't need scrollbars at all?
4083 //
4084 // also adjust the position to be valid for the new scroll rangs
faec5a43
SN
4085 if ( w <= cw )
4086 {
97a9929e 4087 w = x = 0;
faec5a43
SN
4088 }
4089 else
4090 {
edb89f7e
VZ
4091 if ( x >= w )
4092 x = w - 1;
f85afd4e 4093 }
97a9929e 4094
faec5a43
SN
4095 if ( h <= ch )
4096 {
97a9929e 4097 h = y = 0;
faec5a43
SN
4098 }
4099 else
4100 {
edb89f7e
VZ
4101 if ( y >= h )
4102 y = h - 1;
faec5a43
SN
4103 }
4104
4105 // do set scrollbar parameters
97a9929e
VZ
4106 SetScrollbars( GRID_SCROLL_LINE_X, GRID_SCROLL_LINE_Y,
4107 GetScrollX(w), GetScrollY(h), x, y,
4108 GetBatchCount() != 0);
12314291
VZ
4109
4110 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4111 // still must reposition the children
4112 CalcWindowSizes();
f85afd4e
MB
4113}
4114
4115
7807d81c
MB
4116void wxGrid::CalcWindowSizes()
4117{
4118 int cw, ch;
4119 GetClientSize( &cw, &ch );
b99be8fb 4120
7807d81c
MB
4121 if ( m_cornerLabelWin->IsShown() )
4122 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
4123
4124 if ( m_colLabelWin->IsShown() )
4125 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
4126
4127 if ( m_rowLabelWin->IsShown() )
4128 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
4129
4130 if ( m_gridWin->IsShown() )
4131 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
4132}
4133
4134
f85afd4e
MB
4135// this is called when the grid table sends a message to say that it
4136// has been redimensioned
4137//
4138bool wxGrid::Redimension( wxGridTableMessage& msg )
4139{
4140 int i;
f6bcfd97 4141 bool result = FALSE;
8f177c8e 4142
a6794685
SN
4143 // Clear the attribute cache as the attribute might refer to a different
4144 // cell than stored in the cache after adding/removing rows/columns.
4145 ClearAttrCache();
7e48d7d9
SN
4146 // By the same reasoning, the editor should be dismissed if columns are
4147 // added or removed. And for consistency, it should IMHO always be
4148 // removed, not only if the cell "underneath" it actually changes.
4149 // For now, I intentionally do not save the editor's content as the
4150 // cell it might want to save that stuff to might no longer exist.
bca7bfc8 4151 HideCellEditControl();
f6bcfd97 4152#if 0
7c1cb261
VZ
4153 // if we were using the default widths/heights so far, we must change them
4154 // now
4155 if ( m_colWidths.IsEmpty() )
4156 {
4157 InitColWidths();
4158 }
4159
4160 if ( m_rowHeights.IsEmpty() )
4161 {
4162 InitRowHeights();
4163 }
f6bcfd97 4164#endif
7c1cb261 4165
f85afd4e
MB
4166 switch ( msg.GetId() )
4167 {
4168 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4169 {
4170 size_t pos = msg.GetCommandInt();
4171 int numRows = msg.GetCommandInt2();
f6bcfd97 4172
f85afd4e 4173 m_numRows += numRows;
2d66e025 4174
f6bcfd97
BP
4175 if ( !m_rowHeights.IsEmpty() )
4176 {
27f35b66
SN
4177 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4178 m_rowBottoms.Insert( 0, pos, numRows );
f6bcfd97
BP
4179
4180 int bottom = 0;
4181 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
60ff3b99 4182
f6bcfd97
BP
4183 for ( i = pos; i < m_numRows; i++ )
4184 {
4185 bottom += m_rowHeights[i];
4186 m_rowBottoms[i] = bottom;
4187 }
4188 }
4189 if ( m_currentCellCoords == wxGridNoCellCoords )
4190 {
4191 // if we have just inserted cols into an empty grid the current
4192 // cell will be undefined...
4193 //
4194 SetCurrentCell( 0, 0 );
4195 }
3f3dc2ef
VZ
4196
4197 if ( m_selection )
4198 m_selection->UpdateRows( pos, numRows );
f6bcfd97
BP
4199 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4200 if (attrProvider)
4201 attrProvider->UpdateAttrRows( pos, numRows );
4202
4203 if ( !GetBatchCount() )
2d66e025 4204 {
f6bcfd97
BP
4205 CalcDimensions();
4206 m_rowLabelWin->Refresh();
2d66e025 4207 }
f85afd4e 4208 }
f6bcfd97
BP
4209 result = TRUE;
4210 break;
f85afd4e
MB
4211
4212 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4213 {
4214 int numRows = msg.GetCommandInt();
2d66e025 4215 int oldNumRows = m_numRows;
f85afd4e 4216 m_numRows += numRows;
2d66e025 4217
f6bcfd97
BP
4218 if ( !m_rowHeights.IsEmpty() )
4219 {
27f35b66
SN
4220 m_rowHeights.Add( m_defaultRowHeight, numRows );
4221 m_rowBottoms.Add( 0, numRows );
60ff3b99 4222
f6bcfd97
BP
4223 int bottom = 0;
4224 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
4225
4226 for ( i = oldNumRows; i < m_numRows; i++ )
4227 {
4228 bottom += m_rowHeights[i];
4229 m_rowBottoms[i] = bottom;
4230 }
4231 }
4232 if ( m_currentCellCoords == wxGridNoCellCoords )
4233 {
4234 // if we have just inserted cols into an empty grid the current
4235 // cell will be undefined...
4236 //
4237 SetCurrentCell( 0, 0 );
4238 }
4239 if ( !GetBatchCount() )
2d66e025 4240 {
f6bcfd97
BP
4241 CalcDimensions();
4242 m_rowLabelWin->Refresh();
2d66e025 4243 }
f85afd4e 4244 }
f6bcfd97
BP
4245 result = TRUE;
4246 break;
f85afd4e
MB
4247
4248 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4249 {
4250 size_t pos = msg.GetCommandInt();
4251 int numRows = msg.GetCommandInt2();
f85afd4e
MB
4252 m_numRows -= numRows;
4253
f6bcfd97 4254 if ( !m_rowHeights.IsEmpty() )
f85afd4e 4255 {
27f35b66
SN
4256 m_rowHeights.RemoveAt( pos, numRows );
4257 m_rowBottoms.RemoveAt( pos, numRows );
2d66e025
MB
4258
4259 int h = 0;
4260 for ( i = 0; i < m_numRows; i++ )
4261 {
4262 h += m_rowHeights[i];
4263 m_rowBottoms[i] = h;
4264 }
f85afd4e 4265 }
f6bcfd97
BP
4266 if ( !m_numRows )
4267 {
4268 m_currentCellCoords = wxGridNoCellCoords;
4269 }
4270 else
4271 {
4272 if ( m_currentCellCoords.GetRow() >= m_numRows )
4273 m_currentCellCoords.Set( 0, 0 );
4274 }
3f3dc2ef
VZ
4275
4276 if ( m_selection )
4277 m_selection->UpdateRows( pos, -((int)numRows) );
f6bcfd97
BP
4278 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4279 if (attrProvider) {
4280 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
84912ef8
RD
4281// ifdef'd out following patch from Paul Gammans
4282#if 0
3ca6a5f0 4283 // No need to touch column attributes, unless we
f6bcfd97
BP
4284 // removed _all_ rows, in this case, we remove
4285 // all column attributes.
4286 // I hate to do this here, but the
4287 // needed data is not available inside UpdateAttrRows.
4288 if ( !GetNumberRows() )
4289 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
84912ef8 4290#endif
f6bcfd97
BP
4291 }
4292 if ( !GetBatchCount() )
4293 {
4294 CalcDimensions();
4295 m_rowLabelWin->Refresh();
4296 }
f85afd4e 4297 }
f6bcfd97
BP
4298 result = TRUE;
4299 break;
f85afd4e
MB
4300
4301 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4302 {
4303 size_t pos = msg.GetCommandInt();
4304 int numCols = msg.GetCommandInt2();
f85afd4e 4305 m_numCols += numCols;
2d66e025 4306
f6bcfd97
BP
4307 if ( !m_colWidths.IsEmpty() )
4308 {
27f35b66
SN
4309 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4310 m_colRights.Insert( 0, pos, numCols );
f6bcfd97
BP
4311
4312 int right = 0;
4313 if ( pos > 0 ) right = m_colRights[pos-1];
60ff3b99 4314
f6bcfd97
BP
4315 for ( i = pos; i < m_numCols; i++ )
4316 {
4317 right += m_colWidths[i];
4318 m_colRights[i] = right;
4319 }
4320 }
4321 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4322 {
f6bcfd97
BP
4323 // if we have just inserted cols into an empty grid the current
4324 // cell will be undefined...
4325 //
4326 SetCurrentCell( 0, 0 );
2d66e025 4327 }
3f3dc2ef
VZ
4328
4329 if ( m_selection )
4330 m_selection->UpdateCols( pos, numCols );
f6bcfd97
BP
4331 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4332 if (attrProvider)
4333 attrProvider->UpdateAttrCols( pos, numCols );
4334 if ( !GetBatchCount() )
4335 {
4336 CalcDimensions();
4337 m_colLabelWin->Refresh();
4338 }
4339
f85afd4e 4340 }
f6bcfd97
BP
4341 result = TRUE;
4342 break;
f85afd4e
MB
4343
4344 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4345 {
4346 int numCols = msg.GetCommandInt();
2d66e025 4347 int oldNumCols = m_numCols;
f85afd4e 4348 m_numCols += numCols;
f6bcfd97
BP
4349 if ( !m_colWidths.IsEmpty() )
4350 {
27f35b66
SN
4351 m_colWidths.Add( m_defaultColWidth, numCols );
4352 m_colRights.Add( 0, numCols );
2d66e025 4353
f6bcfd97
BP
4354 int right = 0;
4355 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
60ff3b99 4356
f6bcfd97
BP
4357 for ( i = oldNumCols; i < m_numCols; i++ )
4358 {
4359 right += m_colWidths[i];
4360 m_colRights[i] = right;
4361 }
4362 }
4363 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4364 {
f6bcfd97
BP
4365 // if we have just inserted cols into an empty grid the current
4366 // cell will be undefined...
4367 //
4368 SetCurrentCell( 0, 0 );
4369 }
4370 if ( !GetBatchCount() )
4371 {
4372 CalcDimensions();
4373 m_colLabelWin->Refresh();
2d66e025 4374 }
f85afd4e 4375 }
f6bcfd97
BP
4376 result = TRUE;
4377 break;
f85afd4e
MB
4378
4379 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4380 {
4381 size_t pos = msg.GetCommandInt();
4382 int numCols = msg.GetCommandInt2();
f85afd4e 4383 m_numCols -= numCols;
f85afd4e 4384
f6bcfd97 4385 if ( !m_colWidths.IsEmpty() )
f85afd4e 4386 {
27f35b66
SN
4387 m_colWidths.RemoveAt( pos, numCols );
4388 m_colRights.RemoveAt( pos, numCols );
2d66e025
MB
4389
4390 int w = 0;
4391 for ( i = 0; i < m_numCols; i++ )
4392 {
4393 w += m_colWidths[i];
4394 m_colRights[i] = w;
4395 }
f85afd4e 4396 }
f6bcfd97
BP
4397 if ( !m_numCols )
4398 {
4399 m_currentCellCoords = wxGridNoCellCoords;
4400 }
4401 else
4402 {
4403 if ( m_currentCellCoords.GetCol() >= m_numCols )
4404 m_currentCellCoords.Set( 0, 0 );
4405 }
3f3dc2ef
VZ
4406
4407 if ( m_selection )
4408 m_selection->UpdateCols( pos, -((int)numCols) );
f6bcfd97
BP
4409 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4410 if (attrProvider) {
4411 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
84912ef8
RD
4412// ifdef'd out following patch from Paul Gammans
4413#if 0
f6bcfd97
BP
4414 // No need to touch row attributes, unless we
4415 // removed _all_ columns, in this case, we remove
4416 // all row attributes.
4417 // I hate to do this here, but the
4418 // needed data is not available inside UpdateAttrCols.
4419 if ( !GetNumberCols() )
4420 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
84912ef8 4421#endif
f6bcfd97
BP
4422 }
4423 if ( !GetBatchCount() )
4424 {
4425 CalcDimensions();
4426 m_colLabelWin->Refresh();
4427 }
f85afd4e 4428 }
faec5a43 4429 result = TRUE;
f6bcfd97 4430 break;
f85afd4e
MB
4431 }
4432
f6bcfd97
BP
4433 if (result && !GetBatchCount() )
4434 m_gridWin->Refresh();
4435 return result;
f85afd4e
MB
4436}
4437
4438
d10f4bf9 4439wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg )
f85afd4e 4440{
2d66e025
MB
4441 wxRegionIterator iter( reg );
4442 wxRect r;
f85afd4e 4443
275c4ae4
RD
4444 wxArrayInt rowlabels;
4445
2d66e025
MB
4446 int top, bottom;
4447 while ( iter )
f85afd4e 4448 {
2d66e025 4449 r = iter.GetRect();
f85afd4e 4450
2d66e025
MB
4451 // TODO: remove this when we can...
4452 // There is a bug in wxMotif that gives garbage update
4453 // rectangles if you jump-scroll a long way by clicking the
4454 // scrollbar with middle button. This is a work-around
4455 //
4456#if defined(__WXMOTIF__)
4457 int cw, ch;
4458 m_gridWin->GetClientSize( &cw, &ch );
4459 if ( r.GetTop() > ch ) r.SetTop( 0 );
4460 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4461#endif
f85afd4e 4462
2d66e025
MB
4463 // logical bounds of update region
4464 //
4465 int dummy;
4466 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
4467 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
4468
4469 // find the row labels within these bounds
4470 //
4471 int row;
33188aa4 4472 for ( row = internalYToRow(top); row < m_numRows; row++ )
2d66e025 4473 {
7c1cb261
VZ
4474 if ( GetRowBottom(row) < top )
4475 continue;
2d66e025 4476
6d55126d 4477 if ( GetRowTop(row) > bottom )
7c1cb261 4478 break;
60ff3b99 4479
d10f4bf9 4480 rowlabels.Add( row );
2d66e025 4481 }
60ff3b99 4482
2d66e025 4483 iter++ ;
f85afd4e 4484 }
d10f4bf9
VZ
4485
4486 return rowlabels;
f85afd4e
MB
4487}
4488
4489
d10f4bf9 4490wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg )
f85afd4e 4491{
2d66e025
MB
4492 wxRegionIterator iter( reg );
4493 wxRect r;
f85afd4e 4494
d10f4bf9 4495 wxArrayInt colLabels;
f85afd4e 4496
2d66e025
MB
4497 int left, right;
4498 while ( iter )
f85afd4e 4499 {
2d66e025 4500 r = iter.GetRect();
f85afd4e 4501
2d66e025
MB
4502 // TODO: remove this when we can...
4503 // There is a bug in wxMotif that gives garbage update
4504 // rectangles if you jump-scroll a long way by clicking the
4505 // scrollbar with middle button. This is a work-around
4506 //
4507#if defined(__WXMOTIF__)
4508 int cw, ch;
4509 m_gridWin->GetClientSize( &cw, &ch );
4510 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4511 r.SetRight( wxMin( r.GetRight(), cw ) );
4512#endif
4513
4514 // logical bounds of update region
4515 //
4516 int dummy;
4517 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
4518 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
4519
4520 // find the cells within these bounds
4521 //
4522 int col;
33188aa4 4523 for ( col = internalXToCol(left); col < m_numCols; col++ )
2d66e025 4524 {
7c1cb261
VZ
4525 if ( GetColRight(col) < left )
4526 continue;
60ff3b99 4527
7c1cb261
VZ
4528 if ( GetColLeft(col) > right )
4529 break;
2d66e025 4530
d10f4bf9 4531 colLabels.Add( col );
2d66e025 4532 }
60ff3b99 4533
2d66e025 4534 iter++ ;
f85afd4e 4535 }
d10f4bf9 4536 return colLabels;
f85afd4e
MB
4537}
4538
4539
d10f4bf9 4540wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg )
f85afd4e 4541{
2d66e025
MB
4542 wxRegionIterator iter( reg );
4543 wxRect r;
f85afd4e 4544
d10f4bf9 4545 wxGridCellCoordsArray cellsExposed;
f85afd4e 4546
2d66e025
MB
4547 int left, top, right, bottom;
4548 while ( iter )
4549 {
4550 r = iter.GetRect();
f85afd4e 4551
2d66e025
MB
4552 // TODO: remove this when we can...
4553 // There is a bug in wxMotif that gives garbage update
4554 // rectangles if you jump-scroll a long way by clicking the
4555 // scrollbar with middle button. This is a work-around
4556 //
4557#if defined(__WXMOTIF__)
f85afd4e 4558 int cw, ch;
2d66e025
MB
4559 m_gridWin->GetClientSize( &cw, &ch );
4560 if ( r.GetTop() > ch ) r.SetTop( 0 );
4561 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4562 r.SetRight( wxMin( r.GetRight(), cw ) );
4563 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4564#endif
8f177c8e 4565
2d66e025
MB
4566 // logical bounds of update region
4567 //
4568 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
4569 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 4570
2d66e025 4571 // find the cells within these bounds
f85afd4e 4572 //
2d66e025 4573 int row, col;
33188aa4 4574 for ( row = internalYToRow(top); row < m_numRows; row++ )
f85afd4e 4575 {
7c1cb261
VZ
4576 if ( GetRowBottom(row) <= top )
4577 continue;
f85afd4e 4578
7c1cb261
VZ
4579 if ( GetRowTop(row) > bottom )
4580 break;
60ff3b99 4581
33188aa4 4582 for ( col = internalXToCol(left); col < m_numCols; col++ )
2d66e025 4583 {
7c1cb261
VZ
4584 if ( GetColRight(col) <= left )
4585 continue;
60ff3b99 4586
7c1cb261
VZ
4587 if ( GetColLeft(col) > right )
4588 break;
60ff3b99 4589
d10f4bf9 4590 cellsExposed.Add( wxGridCellCoords( row, col ) );
2d66e025
MB
4591 }
4592 }
60ff3b99 4593
7c1cb261 4594 iter++;
f85afd4e 4595 }
d10f4bf9
VZ
4596
4597 return cellsExposed;
f85afd4e
MB
4598}
4599
4600
2d66e025 4601void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 4602{
2d66e025
MB
4603 int x, y, row;
4604 wxPoint pos( event.GetPosition() );
4605 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 4606
2d66e025 4607 if ( event.Dragging() )
f85afd4e 4608 {
426b2d87
JS
4609 if (!m_isDragging)
4610 {
4611 m_isDragging = TRUE;
4612 m_rowLabelWin->CaptureMouse();
4613 }
8f177c8e 4614
2d66e025 4615 if ( event.LeftIsDown() )
f85afd4e
MB
4616 {
4617 switch( m_cursorMode )
4618 {
f85afd4e
MB
4619 case WXGRID_CURSOR_RESIZE_ROW:
4620 {
2d66e025
MB
4621 int cw, ch, left, dummy;
4622 m_gridWin->GetClientSize( &cw, &ch );
4623 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 4624
2d66e025
MB
4625 wxClientDC dc( m_gridWin );
4626 PrepareDC( dc );
af547d51
VZ
4627 y = wxMax( y,
4628 GetRowTop(m_dragRowOrCol) +
4629 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 4630 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
4631 if ( m_dragLastPos >= 0 )
4632 {
2d66e025 4633 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 4634 }
2d66e025
MB
4635 dc.DrawLine( left, y, left+cw, y );
4636 m_dragLastPos = y;
f85afd4e
MB
4637 }
4638 break;
4639
4640 case WXGRID_CURSOR_SELECT_ROW:
e32352cf 4641 if ( (row = YToRow( y )) >= 0 )
aa5e1f75 4642 {
3f3dc2ef
VZ
4643 if ( m_selection )
4644 {
4645 m_selection->SelectRow( row,
4646 event.ControlDown(),
4647 event.ShiftDown(),
4648 event.AltDown(),
4649 event.MetaDown() );
4650 }
f85afd4e 4651 }
e2b42eeb
VZ
4652
4653 // default label to suppress warnings about "enumeration value
4654 // 'xxx' not handled in switch
4655 default:
4656 break;
f85afd4e
MB
4657 }
4658 }
4659 return;
4660 }
4661
426b2d87
JS
4662 if ( m_isDragging && (event.Entering() || event.Leaving()) )
4663 return;
8f177c8e 4664
426b2d87
JS
4665 if (m_isDragging)
4666 {
70e8d961 4667 if (m_rowLabelWin->HasCapture()) m_rowLabelWin->ReleaseMouse();
426b2d87
JS
4668 m_isDragging = FALSE;
4669 }
60ff3b99 4670
6d004f67
MB
4671 // ------------ Entering or leaving the window
4672 //
4673 if ( event.Entering() || event.Leaving() )
4674 {
e2b42eeb 4675 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
4676 }
4677
e2b42eeb 4678
2d66e025 4679 // ------------ Left button pressed
f85afd4e 4680 //
6d004f67 4681 else if ( event.LeftDown() )
f85afd4e 4682 {
2d66e025
MB
4683 // don't send a label click event for a hit on the
4684 // edge of the row label - this is probably the user
4685 // wanting to resize the row
4686 //
4687 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 4688 {
2d66e025 4689 row = YToRow(y);
58dd5b3b 4690 if ( row >= 0 &&
b54ba671 4691 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 4692 {
aa5e1f75
SN
4693 if ( !event.ShiftDown() && !event.ControlDown() )
4694 ClearSelection();
64e15340 4695 if ( m_selection )
3f3dc2ef
VZ
4696 {
4697 if ( event.ShiftDown() )
4698 {
4699 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4700 0,
4701 row,
4702 GetNumberCols() - 1,
4703 event.ControlDown(),
4704 event.ShiftDown(),
4705 event.AltDown(),
4706 event.MetaDown() );
4707 }
4708 else
4709 {
4710 m_selection->SelectRow( row,
4711 event.ControlDown(),
4712 event.ShiftDown(),
4713 event.AltDown(),
4714 event.MetaDown() );
4715 }
4716 }
4717
e2b42eeb 4718 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 4719 }
2d66e025
MB
4720 }
4721 else
4722 {
4723 // starting to drag-resize a row
4724 //
6e8524b1
MB
4725 if ( CanDragRowSize() )
4726 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
4727 }
4728 }
f85afd4e 4729
f85afd4e 4730
2d66e025
MB
4731 // ------------ Left double click
4732 //
4733 else if (event.LeftDClick() )
4734 {
4735 if ( YToEdgeOfRow(y) < 0 )
4736 {
4737 row = YToRow(y);
b54ba671 4738 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
f85afd4e
MB
4739 }
4740 }
60ff3b99
VZ
4741
4742
2d66e025 4743 // ------------ Left button released
f85afd4e 4744 //
2d66e025 4745 else if ( event.LeftUp() )
f85afd4e 4746 {
2d66e025 4747 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 4748 {
6d004f67 4749 DoEndDragResizeRow();
60ff3b99 4750
6d004f67
MB
4751 // Note: we are ending the event *after* doing
4752 // default processing in this case
4753 //
b54ba671 4754 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 4755 }
f85afd4e 4756
e2b42eeb
VZ
4757 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4758 m_dragLastPos = -1;
2d66e025 4759 }
f85afd4e 4760
f85afd4e 4761
2d66e025
MB
4762 // ------------ Right button down
4763 //
4764 else if ( event.RightDown() )
4765 {
4766 row = YToRow(y);
b54ba671 4767 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
4768 {
4769 // no default action at the moment
f85afd4e
MB
4770 }
4771 }
60ff3b99
VZ
4772
4773
2d66e025 4774 // ------------ Right double click
f85afd4e 4775 //
2d66e025
MB
4776 else if ( event.RightDClick() )
4777 {
4778 row = YToRow(y);
b54ba671 4779 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
4780 {
4781 // no default action at the moment
4782 }
4783 }
60ff3b99
VZ
4784
4785
2d66e025 4786 // ------------ No buttons down and mouse moving
f85afd4e 4787 //
2d66e025 4788 else if ( event.Moving() )
f85afd4e 4789 {
2d66e025
MB
4790 m_dragRowOrCol = YToEdgeOfRow( y );
4791 if ( m_dragRowOrCol >= 0 )
8f177c8e 4792 {
2d66e025 4793 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 4794 {
e2b42eeb 4795 // don't capture the mouse yet
6e8524b1
MB
4796 if ( CanDragRowSize() )
4797 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
8f177c8e 4798 }
2d66e025 4799 }
6d004f67 4800 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 4801 {
e2b42eeb 4802 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
8f177c8e 4803 }
f85afd4e 4804 }
2d66e025
MB
4805}
4806
4807
4808void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
4809{
4810 int x, y, col;
4811 wxPoint pos( event.GetPosition() );
4812 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 4813
2d66e025 4814 if ( event.Dragging() )
f85afd4e 4815 {
fe77cf60
JS
4816 if (!m_isDragging)
4817 {
4818 m_isDragging = TRUE;
4819 m_colLabelWin->CaptureMouse();
4820 }
8f177c8e 4821
2d66e025 4822 if ( event.LeftIsDown() )
8f177c8e 4823 {
2d66e025 4824 switch( m_cursorMode )
8f177c8e 4825 {
2d66e025 4826 case WXGRID_CURSOR_RESIZE_COL:
8f177c8e 4827 {
2d66e025
MB
4828 int cw, ch, dummy, top;
4829 m_gridWin->GetClientSize( &cw, &ch );
4830 CalcUnscrolledPosition( 0, 0, &dummy, &top );
60ff3b99 4831
2d66e025
MB
4832 wxClientDC dc( m_gridWin );
4833 PrepareDC( dc );
43947979
VZ
4834
4835 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
4836 GetColMinimalWidth(m_dragRowOrCol));
d2fdd8d2 4837 dc.SetLogicalFunction(wxINVERT);
2d66e025
MB
4838 if ( m_dragLastPos >= 0 )
4839 {
4840 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4841 }
4842 dc.DrawLine( x, top, x, top+ch );
4843 m_dragLastPos = x;
f85afd4e 4844 }
2d66e025 4845 break;
f85afd4e 4846
2d66e025 4847 case WXGRID_CURSOR_SELECT_COL:
e32352cf 4848 if ( (col = XToCol( x )) >= 0 )
aa5e1f75 4849 {
3f3dc2ef
VZ
4850 if ( m_selection )
4851 {
4852 m_selection->SelectCol( col,
4853 event.ControlDown(),
4854 event.ShiftDown(),
4855 event.AltDown(),
4856 event.MetaDown() );
4857 }
2d66e025 4858 }
e2b42eeb
VZ
4859
4860 // default label to suppress warnings about "enumeration value
4861 // 'xxx' not handled in switch
4862 default:
4863 break;
2d66e025 4864 }
f85afd4e 4865 }
2d66e025 4866 return;
f85afd4e 4867 }
2d66e025 4868
fe77cf60
JS
4869 if ( m_isDragging && (event.Entering() || event.Leaving()) )
4870 return;
2d66e025 4871
fe77cf60
JS
4872 if (m_isDragging)
4873 {
70e8d961 4874 if (m_colLabelWin->HasCapture()) m_colLabelWin->ReleaseMouse();
fe77cf60
JS
4875 m_isDragging = FALSE;
4876 }
60ff3b99 4877
6d004f67
MB
4878 // ------------ Entering or leaving the window
4879 //
4880 if ( event.Entering() || event.Leaving() )
4881 {
e2b42eeb 4882 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
6d004f67
MB
4883 }
4884
e2b42eeb 4885
2d66e025 4886 // ------------ Left button pressed
f85afd4e 4887 //
6d004f67 4888 else if ( event.LeftDown() )
f85afd4e 4889 {
2d66e025
MB
4890 // don't send a label click event for a hit on the
4891 // edge of the col label - this is probably the user
4892 // wanting to resize the col
4893 //
4894 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 4895 {
2d66e025 4896 col = XToCol(x);
58dd5b3b 4897 if ( col >= 0 &&
b54ba671 4898 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 4899 {
aa5e1f75
SN
4900 if ( !event.ShiftDown() && !event.ControlDown() )
4901 ClearSelection();
3f3dc2ef
VZ
4902 if ( m_selection )
4903 {
4904 if ( event.ShiftDown() )
4905 {
4906 m_selection->SelectBlock( 0,
4907 m_currentCellCoords.GetCol(),
4908 GetNumberRows() - 1, col,
4909 event.ControlDown(),
4910 event.ShiftDown(),
4911 event.AltDown(),
4912 event.MetaDown() );
4913 }
4914 else
4915 {
4916 m_selection->SelectCol( col,
4917 event.ControlDown(),
4918 event.ShiftDown(),
4919 event.AltDown(),
4920 event.MetaDown() );
4921 }
4922 }
4923
e2b42eeb 4924 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
f85afd4e 4925 }
2d66e025
MB
4926 }
4927 else
4928 {
4929 // starting to drag-resize a col
4930 //
6e8524b1
MB
4931 if ( CanDragColSize() )
4932 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
2d66e025
MB
4933 }
4934 }
f85afd4e 4935
f85afd4e 4936
2d66e025
MB
4937 // ------------ Left double click
4938 //
4939 if ( event.LeftDClick() )
4940 {
4941 if ( XToEdgeOfCol(x) < 0 )
4942 {
4943 col = XToCol(x);
b54ba671 4944 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
2d66e025
MB
4945 }
4946 }
60ff3b99
VZ
4947
4948
2d66e025
MB
4949 // ------------ Left button released
4950 //
4951 else if ( event.LeftUp() )
4952 {
4953 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4954 {
6d004f67 4955 DoEndDragResizeCol();
e2b42eeb 4956
6d004f67
MB
4957 // Note: we are ending the event *after* doing
4958 // default processing in this case
4959 //
b54ba671 4960 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
2d66e025 4961 }
f85afd4e 4962
e2b42eeb 4963 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
2d66e025 4964 m_dragLastPos = -1;
60ff3b99
VZ
4965 }
4966
4967
2d66e025
MB
4968 // ------------ Right button down
4969 //
4970 else if ( event.RightDown() )
4971 {
4972 col = XToCol(x);
b54ba671 4973 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
4974 {
4975 // no default action at the moment
f85afd4e
MB
4976 }
4977 }
60ff3b99
VZ
4978
4979
2d66e025 4980 // ------------ Right double click
f85afd4e 4981 //
2d66e025
MB
4982 else if ( event.RightDClick() )
4983 {
4984 col = XToCol(x);
b54ba671 4985 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
4986 {
4987 // no default action at the moment
4988 }
4989 }
60ff3b99
VZ
4990
4991
2d66e025 4992 // ------------ No buttons down and mouse moving
f85afd4e 4993 //
2d66e025 4994 else if ( event.Moving() )
f85afd4e 4995 {
2d66e025
MB
4996 m_dragRowOrCol = XToEdgeOfCol( x );
4997 if ( m_dragRowOrCol >= 0 )
f85afd4e 4998 {
2d66e025 4999 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 5000 {
e2b42eeb 5001 // don't capture the cursor yet
6e8524b1
MB
5002 if ( CanDragColSize() )
5003 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
f85afd4e 5004 }
2d66e025 5005 }
6d004f67 5006 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 5007 {
e2b42eeb 5008 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
8f177c8e 5009 }
f85afd4e
MB
5010 }
5011}
5012
5013
2d66e025 5014void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 5015{
2d66e025 5016 if ( event.LeftDown() )
f85afd4e 5017 {
2d66e025
MB
5018 // indicate corner label by having both row and
5019 // col args == -1
f85afd4e 5020 //
b54ba671 5021 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
5022 {
5023 SelectAll();
5024 }
f85afd4e
MB
5025 }
5026
2d66e025
MB
5027 else if ( event.LeftDClick() )
5028 {
b54ba671 5029 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 5030 }
8f177c8e 5031
2d66e025 5032 else if ( event.RightDown() )
f85afd4e 5033 {
b54ba671 5034 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 5035 {
2d66e025
MB
5036 // no default action at the moment
5037 }
5038 }
f85afd4e 5039
2d66e025
MB
5040 else if ( event.RightDClick() )
5041 {
b54ba671 5042 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
5043 {
5044 // no default action at the moment
5045 }
5046 }
5047}
f85afd4e 5048
e2b42eeb
VZ
5049void wxGrid::ChangeCursorMode(CursorMode mode,
5050 wxWindow *win,
5051 bool captureMouse)
5052{
5053#ifdef __WXDEBUG__
5054 static const wxChar *cursorModes[] =
5055 {
5056 _T("SELECT_CELL"),
5057 _T("RESIZE_ROW"),
5058 _T("RESIZE_COL"),
5059 _T("SELECT_ROW"),
5060 _T("SELECT_COL")
5061 };
5062
181bfffd
VZ
5063 wxLogTrace(_T("grid"),
5064 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
e2b42eeb
VZ
5065 win == m_colLabelWin ? _T("colLabelWin")
5066 : win ? _T("rowLabelWin")
5067 : _T("gridWin"),
5068 cursorModes[m_cursorMode], cursorModes[mode]);
5069#endif // __WXDEBUG__
5070
faec5a43
SN
5071 if ( mode == m_cursorMode &&
5072 win == m_winCapture &&
5073 captureMouse == (m_winCapture != NULL))
e2b42eeb
VZ
5074 return;
5075
5076 if ( !win )
5077 {
5078 // by default use the grid itself
5079 win = m_gridWin;
5080 }
5081
5082 if ( m_winCapture )
5083 {
70e8d961 5084 if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse();
e2b42eeb
VZ
5085 m_winCapture = (wxWindow *)NULL;
5086 }
5087
5088 m_cursorMode = mode;
5089
5090 switch ( m_cursorMode )
5091 {
5092 case WXGRID_CURSOR_RESIZE_ROW:
5093 win->SetCursor( m_rowResizeCursor );
5094 break;
5095
5096 case WXGRID_CURSOR_RESIZE_COL:
5097 win->SetCursor( m_colResizeCursor );
5098 break;
5099
5100 default:
5101 win->SetCursor( *wxSTANDARD_CURSOR );
5102 }
5103
5104 // we need to capture mouse when resizing
5105 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5106 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5107
5108 if ( captureMouse && resize )
5109 {
5110 win->CaptureMouse();
5111 m_winCapture = win;
5112 }
5113}
8f177c8e 5114
2d66e025
MB
5115void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5116{
5117 int x, y;
5118 wxPoint pos( event.GetPosition() );
5119 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5120
2d66e025
MB
5121 wxGridCellCoords coords;
5122 XYToCell( x, y, coords );
60ff3b99 5123
27f35b66
SN
5124 int cell_rows, cell_cols;
5125 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5126 if ((cell_rows < 0) || (cell_cols < 0))
5127 {
5128 coords.SetRow(coords.GetRow() + cell_rows);
5129 coords.SetCol(coords.GetCol() + cell_cols);
5130 }
5131
2d66e025
MB
5132 if ( event.Dragging() )
5133 {
07296f0b
RD
5134 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5135
5136 // Don't start doing anything until the mouse has been drug at
5137 // least 3 pixels in any direction...
508011ce
VZ
5138 if (! m_isDragging)
5139 {
5140 if (m_startDragPos == wxDefaultPosition)
5141 {
07296f0b
RD
5142 m_startDragPos = pos;
5143 return;
5144 }
5145 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5146 return;
5147 }
5148
2d66e025 5149 m_isDragging = TRUE;
2d66e025
MB
5150 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5151 {
f0102d2a
VZ
5152 // Hide the edit control, so it
5153 // won't interfer with drag-shrinking.
f6bcfd97 5154 if ( IsCellEditControlShown() )
a5777624 5155 {
f0102d2a 5156 HideCellEditControl();
a5777624
RD
5157 SaveEditControlValue();
5158 }
07296f0b
RD
5159
5160 // Have we captured the mouse yet?
508011ce
VZ
5161 if (! m_winCapture)
5162 {
07296f0b
RD
5163 m_winCapture = m_gridWin;
5164 m_winCapture->CaptureMouse();
5165 }
5166
2d66e025
MB
5167 if ( coords != wxGridNoCellCoords )
5168 {
aa5e1f75
SN
5169 if ( event.ControlDown() )
5170 {
5171 if ( m_selectingKeyboard == wxGridNoCellCoords)
5172 m_selectingKeyboard = coords;
c9097836 5173 HighlightBlock ( m_selectingKeyboard, coords );
aa5e1f75
SN
5174 }
5175 else
5176 {
5177 if ( !IsSelection() )
5178 {
c9097836 5179 HighlightBlock( coords, coords );
aa5e1f75
SN
5180 }
5181 else
5182 {
c9097836 5183 HighlightBlock( m_currentCellCoords, coords );
aa5e1f75 5184 }
f85afd4e 5185 }
07296f0b 5186
508011ce
VZ
5187 if (! IsVisible(coords))
5188 {
07296f0b
RD
5189 MakeCellVisible(coords);
5190 // TODO: need to introduce a delay or something here. The
e32352cf 5191 // scrolling is way to fast, at least on MSW - also on GTK.
07296f0b 5192 }
2d66e025
MB
5193 }
5194 }
6d004f67
MB
5195 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5196 {
5197 int cw, ch, left, dummy;
5198 m_gridWin->GetClientSize( &cw, &ch );
5199 CalcUnscrolledPosition( 0, 0, &left, &dummy );
8f177c8e 5200
6d004f67
MB
5201 wxClientDC dc( m_gridWin );
5202 PrepareDC( dc );
a95e38c0
VZ
5203 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
5204 GetRowMinimalHeight(m_dragRowOrCol) );
6d004f67
MB
5205 dc.SetLogicalFunction(wxINVERT);
5206 if ( m_dragLastPos >= 0 )
5207 {
5208 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5209 }
5210 dc.DrawLine( left, y, left+cw, y );
5211 m_dragLastPos = y;
5212 }
5213 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5214 {
5215 int cw, ch, dummy, top;
5216 m_gridWin->GetClientSize( &cw, &ch );
5217 CalcUnscrolledPosition( 0, 0, &dummy, &top );
e2b42eeb 5218
6d004f67
MB
5219 wxClientDC dc( m_gridWin );
5220 PrepareDC( dc );
43947979
VZ
5221 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5222 GetColMinimalWidth(m_dragRowOrCol) );
6d004f67
MB
5223 dc.SetLogicalFunction(wxINVERT);
5224 if ( m_dragLastPos >= 0 )
5225 {
5226 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5227 }
5228 dc.DrawLine( x, top, x, top+ch );
5229 m_dragLastPos = x;
5230 }
e2b42eeb 5231
2d66e025
MB
5232 return;
5233 }
66242c80 5234
2d66e025 5235 m_isDragging = FALSE;
07296f0b
RD
5236 m_startDragPos = wxDefaultPosition;
5237
a5777624
RD
5238 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5239 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5240 // wxGTK
e2b42eeb 5241#if 0
a5777624
RD
5242 if ( event.Entering() || event.Leaving() )
5243 {
5244 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5245 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
5246 }
5247 else
e2b42eeb
VZ
5248#endif // 0
5249
a5777624
RD
5250 // ------------ Left button pressed
5251 //
5252 if ( event.LeftDown() && coords != wxGridNoCellCoords )
f6bcfd97
BP
5253 {
5254 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
5255 coords.GetRow(),
5256 coords.GetCol(),
5257 event ) )
a5777624 5258 {
f6bcfd97
BP
5259 if ( !event.ControlDown() )
5260 ClearSelection();
5261 if ( event.ShiftDown() )
5262 {
3f3dc2ef
VZ
5263 if ( m_selection )
5264 {
5265 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5266 m_currentCellCoords.GetCol(),
5267 coords.GetRow(),
5268 coords.GetCol(),
5269 event.ControlDown(),
5270 event.ShiftDown(),
5271 event.AltDown(),
5272 event.MetaDown() );
5273 }
f6bcfd97
BP
5274 }
5275 else if ( XToEdgeOfCol(x) < 0 &&
5276 YToEdgeOfRow(y) < 0 )
58dd5b3b 5277 {
a5777624
RD
5278 DisableCellEditControl();
5279 MakeCellVisible( coords );
5280
73145b0e 5281 if ( event.ControlDown() )
58dd5b3b 5282 {
73145b0e
JS
5283 if ( m_selection )
5284 {
5285 m_selection->ToggleCellSelection( coords.GetRow(),
5286 coords.GetCol(),
5287 event.ControlDown(),
5288 event.ShiftDown(),
5289 event.AltDown(),
5290 event.MetaDown() );
5291 }
5292 m_selectingTopLeft = wxGridNoCellCoords;
5293 m_selectingBottomRight = wxGridNoCellCoords;
5294 m_selectingKeyboard = coords;
a5777624
RD
5295 }
5296 else
5297 {
73145b0e
JS
5298 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
5299 SetCurrentCell( coords );
5300 if ( m_selection )
aa5e1f75 5301 {
73145b0e
JS
5302 if ( m_selection->GetSelectionMode() !=
5303 wxGrid::wxGridSelectCells )
3f3dc2ef 5304 {
73145b0e 5305 HighlightBlock( coords, coords );
3f3dc2ef 5306 }
aa5e1f75 5307 }
58dd5b3b
MB
5308 }
5309 }
f85afd4e 5310 }
a5777624 5311 }
f85afd4e 5312
f85afd4e 5313
a5777624
RD
5314 // ------------ Left double click
5315 //
5316 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
5317 {
5318 DisableCellEditControl();
5319
5320 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
58dd5b3b 5321 {
a5777624
RD
5322 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
5323 coords.GetRow(),
5324 coords.GetCol(),
5325 event );
58dd5b3b 5326 }
a5777624 5327 }
f85afd4e 5328
8f177c8e 5329
a5777624
RD
5330 // ------------ Left button released
5331 //
5332 else if ( event.LeftUp() )
5333 {
5334 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 5335 {
b5808881
SN
5336 if ( m_selectingTopLeft != wxGridNoCellCoords &&
5337 m_selectingBottomRight != wxGridNoCellCoords )
52068ea5 5338 {
a5777624 5339 if (m_winCapture)
58dd5b3b 5340 {
70e8d961 5341 if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse();
a5777624 5342 m_winCapture = NULL;
58dd5b3b 5343 }
3f3dc2ef
VZ
5344
5345 if ( m_selection )
5346 {
5347 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5348 m_selectingTopLeft.GetCol(),
5349 m_selectingBottomRight.GetRow(),
5350 m_selectingBottomRight.GetCol(),
5351 event.ControlDown(),
5352 event.ShiftDown(),
5353 event.AltDown(),
5354 event.MetaDown() );
5355 }
5356
5c8fc7c1
SN
5357 m_selectingTopLeft = wxGridNoCellCoords;
5358 m_selectingBottomRight = wxGridNoCellCoords;
2d66e025 5359
73145b0e
JS
5360 // Show the edit control, if it has been hidden for
5361 // drag-shrinking.
5362 ShowCellEditControl();
5363 }
5364 else
5365 {
5366 if( m_waitForSlowClick && CanEnableCellControl())
5367 {
5368 EnableCellEditControl();
5369
5370 wxGridCellAttr* attr = GetCellAttr(coords);
5371 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
5372 editor->StartingClick();
5373 editor->DecRef();
5374 attr->DecRef();
5375
5376 m_waitForSlowClick = FALSE;
5377 }
5378 }
a5777624
RD
5379 }
5380 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5381 {
5382 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5383 DoEndDragResizeRow();
e2b42eeb 5384
a5777624
RD
5385 // Note: we are ending the event *after* doing
5386 // default processing in this case
5387 //
5388 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5389 }
5390 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5391 {
5392 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5393 DoEndDragResizeCol();
e2b42eeb 5394
a5777624
RD
5395 // Note: we are ending the event *after* doing
5396 // default processing in this case
5397 //
5398 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
58dd5b3b 5399 }
f85afd4e 5400
a5777624
RD
5401 m_dragLastPos = -1;
5402 }
5403
f85afd4e 5404
a5777624
RD
5405 // ------------ Right button down
5406 //
5407 else if ( event.RightDown() && coords != wxGridNoCellCoords )
5408 {
5409 DisableCellEditControl();
5410 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
5411 coords.GetRow(),
5412 coords.GetCol(),
5413 event ) )
f85afd4e 5414 {
a5777624 5415 // no default action at the moment
60ff3b99 5416 }
a5777624 5417 }
2d66e025 5418
60ff3b99 5419
a5777624
RD
5420 // ------------ Right double click
5421 //
5422 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
5423 {
5424 DisableCellEditControl();
5425 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
5426 coords.GetRow(),
5427 coords.GetCol(),
5428 event ) )
f85afd4e 5429 {
a5777624 5430 // no default action at the moment
60ff3b99 5431 }
a5777624
RD
5432 }
5433
5434 // ------------ Moving and no button action
5435 //
5436 else if ( event.Moving() && !event.IsButton() )
5437 {
d57ad377
SN
5438 if( coords.GetRow() < 0 || coords.GetCol() < 0 )
5439 {
5440 // out of grid cell area
5441 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5442 return;
5443 }
5444
a5777624
RD
5445 int dragRow = YToEdgeOfRow( y );
5446 int dragCol = XToEdgeOfCol( x );
790cc417 5447
a5777624
RD
5448 // Dragging on the corner of a cell to resize in both
5449 // directions is not implemented yet...
790cc417 5450 //
a5777624 5451 if ( dragRow >= 0 && dragCol >= 0 )
790cc417 5452 {
a5777624
RD
5453 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5454 return;
5455 }
6d004f67 5456
d57ad377 5457 if ( dragRow >= 0 )
a5777624
RD
5458 {
5459 m_dragRowOrCol = dragRow;
6d004f67 5460
a5777624 5461 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 5462 {
a5777624
RD
5463 if ( CanDragRowSize() && CanDragGridSize() )
5464 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
6d004f67 5465 }
e2b42eeb 5466
b94ae1ea
VZ
5467 if ( dragCol >= 0 )
5468 {
5469 m_dragRowOrCol = dragCol;
122603f1 5470 }
b94ae1ea 5471
a5777624
RD
5472 return;
5473 }
e2b42eeb 5474
d57ad377 5475 if ( dragCol >= 0 )
a5777624
RD
5476 {
5477 m_dragRowOrCol = dragCol;
e2b42eeb 5478
a5777624 5479 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 5480 {
a5777624
RD
5481 if ( CanDragColSize() && CanDragGridSize() )
5482 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
6d004f67 5483 }
a5777624
RD
5484
5485 return;
6d004f67 5486 }
a5777624
RD
5487
5488 // Neither on a row or col edge
5489 //
5490 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5491 {
5492 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5493 }
5494 }
6d004f67
MB
5495}
5496
5497
5498void wxGrid::DoEndDragResizeRow()
5499{
5500 if ( m_dragLastPos >= 0 )
5501 {
5502 // erase the last line and resize the row
5503 //
5504 int cw, ch, left, dummy;
5505 m_gridWin->GetClientSize( &cw, &ch );
5506 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5507
5508 wxClientDC dc( m_gridWin );
5509 PrepareDC( dc );
5510 dc.SetLogicalFunction( wxINVERT );
5511 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5512 HideCellEditControl();
a5777624 5513 SaveEditControlValue();
6d004f67 5514
7c1cb261 5515 int rowTop = GetRowTop(m_dragRowOrCol);
6d004f67
MB
5516 SetRowSize( m_dragRowOrCol,
5517 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
e2b42eeb 5518
6d004f67
MB
5519 if ( !GetBatchCount() )
5520 {
5521 // Only needed to get the correct rect.y:
5522 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
5523 rect.x = 0;
5524 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5525 rect.width = m_rowLabelWidth;
5526 rect.height = ch - rect.y;
5527 m_rowLabelWin->Refresh( TRUE, &rect );
5528 rect.width = cw;
27f35b66
SN
5529 // if there is a multicell block, paint all of it
5530 if (m_table)
5531 {
5532 int i, cell_rows, cell_cols, subtract_rows = 0;
5533 int leftCol = XToCol(left);
5534 int rightCol = XToCol(left+cw);
5535 if (leftCol >= 0)
5536 {
5537 if (rightCol < 0) rightCol = m_numCols;
5538 for (i=leftCol; i<rightCol; i++)
5539 {
5540 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
5541 if (cell_rows < subtract_rows)
5542 subtract_rows = cell_rows;
5543 }
5544 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
5545 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5546 rect.height = ch - rect.y;
5547 }
5548 }
6d004f67
MB
5549 m_gridWin->Refresh( FALSE, &rect );
5550 }
5551
5552 ShowCellEditControl();
5553 }
5554}
5555
5556
5557void wxGrid::DoEndDragResizeCol()
5558{
5559 if ( m_dragLastPos >= 0 )
5560 {
5561 // erase the last line and resize the col
5562 //
5563 int cw, ch, dummy, top;
5564 m_gridWin->GetClientSize( &cw, &ch );
5565 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5566
5567 wxClientDC dc( m_gridWin );
5568 PrepareDC( dc );
5569 dc.SetLogicalFunction( wxINVERT );
5570 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5571 HideCellEditControl();
a5777624 5572 SaveEditControlValue();
6d004f67 5573
7c1cb261 5574 int colLeft = GetColLeft(m_dragRowOrCol);
6d004f67 5575 SetColSize( m_dragRowOrCol,
43947979
VZ
5576 wxMax( m_dragLastPos - colLeft,
5577 GetColMinimalWidth(m_dragRowOrCol) ) );
6d004f67
MB
5578
5579 if ( !GetBatchCount() )
5580 {
5581 // Only needed to get the correct rect.x:
5582 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
5583 rect.y = 0;
5584 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5585 rect.width = cw - rect.x;
5586 rect.height = m_colLabelHeight;
5587 m_colLabelWin->Refresh( TRUE, &rect );
5588 rect.height = ch;
27f35b66
SN
5589 // if there is a multicell block, paint all of it
5590 if (m_table)
5591 {
5592 int i, cell_rows, cell_cols, subtract_cols = 0;
5593 int topRow = YToRow(top);
5594 int bottomRow = YToRow(top+cw);
5595 if (topRow >= 0)
5596 {
5597 if (bottomRow < 0) bottomRow = m_numRows;
5598 for (i=topRow; i<bottomRow; i++)
5599 {
5600 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
5601 if (cell_cols < subtract_cols)
5602 subtract_cols = cell_cols;
5603 }
5604 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
5605 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5606 rect.width = cw - rect.x;
5607 }
5608 }
6d004f67 5609 m_gridWin->Refresh( FALSE, &rect );
790cc417 5610 }
e2b42eeb 5611
6d004f67 5612 ShowCellEditControl();
f85afd4e 5613 }
f85afd4e
MB
5614}
5615
5616
6d004f67 5617
2d66e025
MB
5618//
5619// ------ interaction with data model
5620//
5621bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 5622{
2d66e025 5623 switch ( msg.GetId() )
17732cec 5624 {
2d66e025
MB
5625 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
5626 return GetModelValues();
17732cec 5627
2d66e025
MB
5628 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
5629 return SetModelValues();
f85afd4e 5630
2d66e025
MB
5631 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
5632 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
5633 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
5634 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
5635 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
5636 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5637 return Redimension( msg );
5638
5639 default:
5640 return FALSE;
f85afd4e 5641 }
2d66e025 5642}
f85afd4e 5643
f85afd4e 5644
f85afd4e 5645
2d66e025
MB
5646// The behaviour of this function depends on the grid table class
5647// Clear() function. For the default wxGridStringTable class the
5648// behavious is to replace all cell contents with wxEmptyString but
5649// not to change the number of rows or cols.
5650//
5651void wxGrid::ClearGrid()
5652{
5653 if ( m_table )
f85afd4e 5654 {
4cfa5de6
RD
5655 if (IsCellEditControlEnabled())
5656 DisableCellEditControl();
5657
2d66e025 5658 m_table->Clear();
1f1ce288 5659 if ( !GetBatchCount() ) m_gridWin->Refresh();
f85afd4e
MB
5660 }
5661}
5662
5663
2d66e025 5664bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 5665{
2d66e025 5666 // TODO: something with updateLabels flag
8f177c8e 5667
2d66e025 5668 if ( !m_created )
f85afd4e 5669 {
bd3c6758 5670 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2d66e025
MB
5671 return FALSE;
5672 }
8f177c8e 5673
2d66e025
MB
5674 if ( m_table )
5675 {
b7fff980 5676 if (IsCellEditControlEnabled())
b54ba671 5677 DisableCellEditControl();
b7fff980 5678
f6bcfd97 5679 return m_table->InsertRows( pos, numRows );
f85afd4e 5680
2d66e025
MB
5681 // the table will have sent the results of the insert row
5682 // operation to this view object as a grid table message
f85afd4e 5683 }
f6bcfd97 5684 return FALSE;
f85afd4e
MB
5685}
5686
5687
2d66e025 5688bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 5689{
2d66e025
MB
5690 // TODO: something with updateLabels flag
5691
5692 if ( !m_created )
f85afd4e 5693 {
bd3c6758 5694 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2d66e025
MB
5695 return FALSE;
5696 }
5697
f6bcfd97
BP
5698 return ( m_table && m_table->AppendRows( numRows ) );
5699 // the table will have sent the results of the append row
5700 // operation to this view object as a grid table message
f85afd4e
MB
5701}
5702
2d66e025
MB
5703
5704bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 5705{
2d66e025
MB
5706 // TODO: something with updateLabels flag
5707
5708 if ( !m_created )
f85afd4e 5709 {
bd3c6758 5710 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2d66e025
MB
5711 return FALSE;
5712 }
5713
b7fff980 5714 if ( m_table )
2d66e025 5715 {
b7fff980 5716 if (IsCellEditControlEnabled())
b54ba671 5717 DisableCellEditControl();
f85afd4e 5718
f6bcfd97
BP
5719 return (m_table->DeleteRows( pos, numRows ));
5720 // the table will have sent the results of the delete row
5721 // operation to this view object as a grid table message
2d66e025 5722 }
b7fff980 5723 return FALSE;
2d66e025 5724}
f85afd4e 5725
f85afd4e 5726
2d66e025
MB
5727bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
5728{
5729 // TODO: something with updateLabels flag
8f177c8e 5730
2d66e025
MB
5731 if ( !m_created )
5732 {
bd3c6758 5733 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2d66e025
MB
5734 return FALSE;
5735 }
f85afd4e 5736
2d66e025
MB
5737 if ( m_table )
5738 {
b7fff980 5739 if (IsCellEditControlEnabled())
b54ba671 5740 DisableCellEditControl();
b7fff980 5741
f6bcfd97 5742 return m_table->InsertCols( pos, numCols );
2d66e025
MB
5743 // the table will have sent the results of the insert col
5744 // operation to this view object as a grid table message
f85afd4e 5745 }
f6bcfd97 5746 return FALSE;
f85afd4e
MB
5747}
5748
2d66e025
MB
5749
5750bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 5751{
2d66e025
MB
5752 // TODO: something with updateLabels flag
5753
5754 if ( !m_created )
f85afd4e 5755 {
bd3c6758 5756 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2d66e025
MB
5757 return FALSE;
5758 }
f85afd4e 5759
f6bcfd97
BP
5760 return ( m_table && m_table->AppendCols( numCols ) );
5761 // the table will have sent the results of the append col
5762 // operation to this view object as a grid table message
f85afd4e
MB
5763}
5764
5765
2d66e025 5766bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 5767{
2d66e025 5768 // TODO: something with updateLabels flag
8f177c8e 5769
2d66e025 5770 if ( !m_created )
f85afd4e 5771 {
bd3c6758 5772 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2d66e025 5773 return FALSE;
f85afd4e
MB
5774 }
5775
b7fff980 5776 if ( m_table )
2d66e025 5777 {
b7fff980 5778 if (IsCellEditControlEnabled())
b54ba671 5779 DisableCellEditControl();
8f177c8e 5780
f6bcfd97
BP
5781 return ( m_table->DeleteCols( pos, numCols ) );
5782 // the table will have sent the results of the delete col
5783 // operation to this view object as a grid table message
f85afd4e 5784 }
b7fff980 5785 return FALSE;
f85afd4e
MB
5786}
5787
5788
2d66e025
MB
5789
5790//
5791// ----- event handlers
f85afd4e 5792//
8f177c8e 5793
2d66e025
MB
5794// Generate a grid event based on a mouse event and
5795// return the result of ProcessEvent()
5796//
fe7b9ed6 5797int wxGrid::SendEvent( const wxEventType type,
2d66e025
MB
5798 int row, int col,
5799 wxMouseEvent& mouseEv )
5800{
fe7b9ed6 5801 bool claimed;
1bcf0c7d 5802 bool vetoed= FALSE;
fe7b9ed6 5803
97a9929e
VZ
5804 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
5805 {
5806 int rowOrCol = (row == -1 ? col : row);
5807
5808 wxGridSizeEvent gridEvt( GetId(),
5809 type,
5810 this,
5811 rowOrCol,
5812 mouseEv.GetX() + GetRowLabelSize(),
5813 mouseEv.GetY() + GetColLabelSize(),
5814 mouseEv.ControlDown(),
5815 mouseEv.ShiftDown(),
5816 mouseEv.AltDown(),
5817 mouseEv.MetaDown() );
5818
5819 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5820 vetoed = !gridEvt.IsAllowed();
5821 }
fe7b9ed6 5822 else if ( type == wxEVT_GRID_RANGE_SELECT )
97a9929e
VZ
5823 {
5824 // Right now, it should _never_ end up here!
5825 wxGridRangeSelectEvent gridEvt( GetId(),
5826 type,
5827 this,
5828 m_selectingTopLeft,
5829 m_selectingBottomRight,
5830 TRUE,
5831 mouseEv.ControlDown(),
5832 mouseEv.ShiftDown(),
5833 mouseEv.AltDown(),
5834 mouseEv.MetaDown() );
5835
5836 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5837 vetoed = !gridEvt.IsAllowed();
5838 }
fe7b9ed6 5839 else
97a9929e
VZ
5840 {
5841 wxGridEvent gridEvt( GetId(),
5842 type,
5843 this,
5844 row, col,
5845 mouseEv.GetX() + GetRowLabelSize(),
5846 mouseEv.GetY() + GetColLabelSize(),
5847 FALSE,
5848 mouseEv.ControlDown(),
5849 mouseEv.ShiftDown(),
5850 mouseEv.AltDown(),
5851 mouseEv.MetaDown() );
5852 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5853 vetoed = !gridEvt.IsAllowed();
5854 }
5855
5856 // A Veto'd event may not be `claimed' so test this first
5857 if (vetoed) return -1;
5858 return claimed ? 1 : 0;
f85afd4e
MB
5859}
5860
5861
2d66e025
MB
5862// Generate a grid event of specified type and return the result
5863// of ProcessEvent().
f85afd4e 5864//
fe7b9ed6 5865int wxGrid::SendEvent( const wxEventType type,
2d66e025 5866 int row, int col )
f85afd4e 5867{
fe7b9ed6 5868 bool claimed;
1bcf0c7d 5869 bool vetoed= FALSE;
fe7b9ed6 5870
b54ba671 5871 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 5872 {
2d66e025 5873 int rowOrCol = (row == -1 ? col : row);
f85afd4e 5874
2d66e025
MB
5875 wxGridSizeEvent gridEvt( GetId(),
5876 type,
5877 this,
5878 rowOrCol );
5879
fe7b9ed6
VZ
5880 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5881 vetoed = !gridEvt.IsAllowed();
2d66e025
MB
5882 }
5883 else
5884 {
5885 wxGridEvent gridEvt( GetId(),
5886 type,
5887 this,
5888 row, col );
8f177c8e 5889
fe7b9ed6
VZ
5890 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5891 vetoed = !gridEvt.IsAllowed();
5892 }
5893
97a9929e
VZ
5894 // A Veto'd event may not be `claimed' so test this first
5895 if (vetoed) return -1;
5896 return claimed ? 1 : 0;
f85afd4e
MB
5897}
5898
5899
2d66e025 5900void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 5901{
f6bcfd97 5902 wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW
8f177c8e 5903}
f85afd4e 5904
bca7bfc8 5905void wxGrid::Refresh(bool eraseb, const wxRect* rect)
27f35b66 5906{
ad603bf7
VZ
5907 // Don't do anything if between Begin/EndBatch...
5908 // EndBatch() will do all this on the last nested one anyway.
27f35b66
SN
5909 if (! GetBatchCount())
5910 {
bca7bfc8 5911 // Refresh to get correct scrolled position:
27f35b66
SN
5912 wxScrolledWindow::Refresh(eraseb,rect);
5913
27f35b66
SN
5914 if (rect)
5915 {
bca7bfc8
SN
5916 int rect_x, rect_y, rectWidth, rectHeight;
5917 int width_label, width_cell, height_label, height_cell;
5918 int x, y;
5919
27f35b66 5920 //Copy rectangle can get scroll offsets..
bca7bfc8
SN
5921 rect_x = rect->GetX();
5922 rect_y = rect->GetY();
5923 rectWidth = rect->GetWidth();
5924 rectHeight = rect->GetHeight();
27f35b66 5925
bca7bfc8
SN
5926 width_label = m_rowLabelWidth - rect_x;
5927 if (width_label > rectWidth) width_label = rectWidth;
27f35b66 5928
bca7bfc8
SN
5929 height_label = m_colLabelHeight - rect_y;
5930 if (height_label > rectHeight) height_label = rectHeight;
27f35b66 5931
bca7bfc8
SN
5932 if (rect_x > m_rowLabelWidth)
5933 {
5934 x = rect_x - m_rowLabelWidth;
5935 width_cell = rectWidth;
5936 }
5937 else
5938 {
5939 x = 0;
5940 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
5941 }
5942
5943 if (rect_y > m_colLabelHeight)
5944 {
5945 y = rect_y - m_colLabelHeight;
5946 height_cell = rectHeight;
5947 }
5948 else
5949 {
5950 y = 0;
5951 height_cell = rectHeight - (m_colLabelHeight - rect_y);
5952 }
5953
5954 // Paint corner label part intersecting rect.
5955 if ( width_label > 0 && height_label > 0 )
5956 {
5957 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
5958 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
5959 }
5960
5961 // Paint col labels part intersecting rect.
5962 if ( width_cell > 0 && height_label > 0 )
5963 {
5964 wxRect anotherrect(x, rect_y, width_cell, height_label);
5965 m_colLabelWin->Refresh(eraseb, &anotherrect);
5966 }
5967
5968 // Paint row labels part intersecting rect.
5969 if ( width_label > 0 && height_cell > 0 )
5970 {
5971 wxRect anotherrect(rect_x, y, width_label, height_cell);
5972 m_rowLabelWin->Refresh(eraseb, &anotherrect);
5973 }
5974
5975 // Paint cell area part intersecting rect.
5976 if ( width_cell > 0 && height_cell > 0 )
5977 {
5978 wxRect anotherrect(x, y, width_cell, height_cell);
5979 m_gridWin->Refresh(eraseb, &anotherrect);
5980 }
5981 }
5982 else
5983 {
5984 m_cornerLabelWin->Refresh(eraseb, NULL);
2b5f62a0 5985 m_colLabelWin->Refresh(eraseb, NULL);
bca7bfc8
SN
5986 m_rowLabelWin->Refresh(eraseb, NULL);
5987 m_gridWin->Refresh(eraseb, NULL);
5988 }
27f35b66
SN
5989 }
5990}
f85afd4e 5991
97a9929e 5992void wxGrid::OnSize( wxSizeEvent& event )
f85afd4e 5993{
97a9929e 5994 // position the child windows
7807d81c 5995 CalcWindowSizes();
97a9929e
VZ
5996
5997 // don't call CalcDimensions() from here, the base class handles the size
5998 // changes itself
5999 event.Skip();
f85afd4e
MB
6000}
6001
f85afd4e 6002
2d66e025 6003void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 6004{
2d66e025 6005 if ( m_inOnKeyDown )
f85afd4e 6006 {
2d66e025
MB
6007 // shouldn't be here - we are going round in circles...
6008 //
07296f0b 6009 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
6010 }
6011
2d66e025 6012 m_inOnKeyDown = TRUE;
f85afd4e 6013
2d66e025
MB
6014 // propagate the event up and see if it gets processed
6015 //
6016 wxWindow *parent = GetParent();
6017 wxKeyEvent keyEvt( event );
6018 keyEvt.SetEventObject( parent );
6019
6020 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 6021 {
9b4aede2 6022
2d66e025
MB
6023 // try local handlers
6024 //
6025 switch ( event.KeyCode() )
6026 {
6027 case WXK_UP:
6028 if ( event.ControlDown() )
6029 {
5c8fc7c1 6030 MoveCursorUpBlock( event.ShiftDown() );
2d66e025
MB
6031 }
6032 else
6033 {
5c8fc7c1 6034 MoveCursorUp( event.ShiftDown() );
2d66e025
MB
6035 }
6036 break;
f85afd4e 6037
2d66e025
MB
6038 case WXK_DOWN:
6039 if ( event.ControlDown() )
6040 {
5c8fc7c1 6041 MoveCursorDownBlock( event.ShiftDown() );
2d66e025
MB
6042 }
6043 else
6044 {
5c8fc7c1 6045 MoveCursorDown( event.ShiftDown() );
2d66e025
MB
6046 }
6047 break;
8f177c8e 6048
2d66e025
MB
6049 case WXK_LEFT:
6050 if ( event.ControlDown() )
6051 {
5c8fc7c1 6052 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025
MB
6053 }
6054 else
6055 {
5c8fc7c1 6056 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
6057 }
6058 break;
6059
6060 case WXK_RIGHT:
6061 if ( event.ControlDown() )
6062 {
5c8fc7c1 6063 MoveCursorRightBlock( event.ShiftDown() );
2d66e025
MB
6064 }
6065 else
6066 {
5c8fc7c1 6067 MoveCursorRight( event.ShiftDown() );
2d66e025
MB
6068 }
6069 break;
b99be8fb 6070
2d66e025 6071 case WXK_RETURN:
a4f7bf58 6072 case WXK_NUMPAD_ENTER:
58dd5b3b
MB
6073 if ( event.ControlDown() )
6074 {
6075 event.Skip(); // to let the edit control have the return
6076 }
6077 else
6078 {
f6bcfd97
BP
6079 if ( GetGridCursorRow() < GetNumberRows()-1 )
6080 {
6081 MoveCursorDown( event.ShiftDown() );
6082 }
6083 else
6084 {
6085 // at the bottom of a column
6086 HideCellEditControl();
6087 SaveEditControlValue();
6088 }
58dd5b3b 6089 }
2d66e025
MB
6090 break;
6091
5c8fc7c1 6092 case WXK_ESCAPE:
e32352cf 6093 ClearSelection();
5c8fc7c1
SN
6094 break;
6095
2c9a89e0
RD
6096 case WXK_TAB:
6097 if (event.ShiftDown())
f6bcfd97
BP
6098 {
6099 if ( GetGridCursorCol() > 0 )
6100 {
6101 MoveCursorLeft( FALSE );
6102 }
6103 else
6104 {
6105 // at left of grid
6106 HideCellEditControl();
6107 SaveEditControlValue();
6108 }
6109 }
2c9a89e0 6110 else
f6bcfd97
BP
6111 {
6112 if ( GetGridCursorCol() < GetNumberCols()-1 )
6113 {
6114 MoveCursorRight( FALSE );
6115 }
6116 else
6117 {
6118 // at right of grid
6119 HideCellEditControl();
6120 SaveEditControlValue();
6121 }
6122 }
2c9a89e0
RD
6123 break;
6124
2d66e025
MB
6125 case WXK_HOME:
6126 if ( event.ControlDown() )
6127 {
6128 MakeCellVisible( 0, 0 );
6129 SetCurrentCell( 0, 0 );
6130 }
6131 else
6132 {
6133 event.Skip();
6134 }
6135 break;
6136
6137 case WXK_END:
6138 if ( event.ControlDown() )
6139 {
6140 MakeCellVisible( m_numRows-1, m_numCols-1 );
6141 SetCurrentCell( m_numRows-1, m_numCols-1 );
6142 }
6143 else
6144 {
6145 event.Skip();
6146 }
6147 break;
6148
6149 case WXK_PRIOR:
6150 MovePageUp();
6151 break;
6152
6153 case WXK_NEXT:
6154 MovePageDown();
6155 break;
6156
07296f0b 6157 case WXK_SPACE:
aa5e1f75
SN
6158 if ( event.ControlDown() )
6159 {
3f3dc2ef
VZ
6160 if ( m_selection )
6161 {
6162 m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
6163 m_currentCellCoords.GetCol(),
6164 event.ControlDown(),
6165 event.ShiftDown(),
6166 event.AltDown(),
6167 event.MetaDown() );
6168 }
aa5e1f75
SN
6169 break;
6170 }
07296f0b
RD
6171 if ( !IsEditable() )
6172 {
5c8fc7c1 6173 MoveCursorRight( FALSE );
07296f0b
RD
6174 break;
6175 }
6176 // Otherwise fall through to default
6177
2d66e025 6178 default:
f6bcfd97
BP
6179 // is it possible to edit the current cell at all?
6180 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
b54ba671 6181 {
f6bcfd97 6182 // yes, now check whether the cells editor accepts the key
f2d76237
RD
6183 int row = m_currentCellCoords.GetRow();
6184 int col = m_currentCellCoords.GetCol();
6185 wxGridCellAttr* attr = GetCellAttr(row, col);
0b190b0f 6186 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
f6bcfd97
BP
6187
6188 // <F2> is special and will always start editing, for
6189 // other keys - ask the editor itself
6190 if ( (event.KeyCode() == WXK_F2 && !event.HasModifiers())
6191 || editor->IsAcceptedKey(event) )
6192 {
73145b0e
JS
6193 // DJC MAPTEK - ensure cell is visble
6194 MakeCellVisible(row, col);
f6bcfd97 6195 EnableCellEditControl();
73145b0e
JS
6196 // DJC MAPTEK - a problem can arise if the cell is not
6197 // completely visible (even after calling MakeCellVisible
6198 // the control is not created and calling StartingKey will
6199 // crash the app
6200 if( editor->IsCreated() && m_cellEditCtrlEnabled ) editor->StartingKey(event);
f6bcfd97
BP
6201 }
6202 else
6203 {
6204 event.Skip();
6205 }
6206
0b190b0f 6207 editor->DecRef();
2c9a89e0
RD
6208 attr->DecRef();
6209 }
b3a7510d
VZ
6210 else
6211 {
b94ae1ea
VZ
6212 // let others process char events with modifiers or all
6213 // char events for readonly cells
b3a7510d
VZ
6214 event.Skip();
6215 }
025562fe 6216 break;
2d66e025 6217 }
f85afd4e
MB
6218 }
6219
2d66e025 6220 m_inOnKeyDown = FALSE;
f85afd4e
MB
6221}
6222
f6bcfd97
BP
6223void wxGrid::OnKeyUp( wxKeyEvent& event )
6224{
6225 // try local handlers
6226 //
6227 if ( event.KeyCode() == WXK_SHIFT )
6228 {
6229 if ( m_selectingTopLeft != wxGridNoCellCoords &&
6230 m_selectingBottomRight != wxGridNoCellCoords )
3f3dc2ef
VZ
6231 {
6232 if ( m_selection )
6233 {
6234 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6235 m_selectingTopLeft.GetCol(),
6236 m_selectingBottomRight.GetRow(),
6237 m_selectingBottomRight.GetCol(),
6238 event.ControlDown(),
6239 TRUE,
6240 event.AltDown(),
6241 event.MetaDown() );
6242 }
6243 }
6244
f6bcfd97
BP
6245 m_selectingTopLeft = wxGridNoCellCoords;
6246 m_selectingBottomRight = wxGridNoCellCoords;
6247 m_selectingKeyboard = wxGridNoCellCoords;
6248 }
6249}
6250
2796cce3 6251void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
6252{
6253}
07296f0b 6254
2d66e025 6255void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 6256{
f6bcfd97
BP
6257 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
6258 {
6259 // the event has been intercepted - do nothing
6260 return;
6261 }
6262
6263 wxClientDC dc(m_gridWin);
6264 PrepareDC(dc);
6265
2a7750d9 6266 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 6267 {
2d66e025 6268 HideCellEditControl();
b54ba671 6269 DisableCellEditControl();
07296f0b 6270
3ca6a5f0 6271 if ( IsVisible( m_currentCellCoords, FALSE ) )
f6bcfd97
BP
6272 {
6273 wxRect r;
3ed884a0 6274 r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
f6bcfd97
BP
6275 if ( !m_gridLinesEnabled )
6276 {
6277 r.x--;
6278 r.y--;
6279 r.width++;
6280 r.height++;
6281 }
d1c0b4f9 6282
3ed884a0 6283 wxGridCellCoordsArray cells = CalcCellsExposed( r );
84912ef8 6284
f6bcfd97
BP
6285 // Otherwise refresh redraws the highlight!
6286 m_currentCellCoords = coords;
275c4ae4 6287
d10f4bf9 6288 DrawGridCellArea(dc,cells);
f6bcfd97
BP
6289 DrawAllGridLines( dc, r );
6290 }
66242c80 6291 }
8f177c8e 6292
2d66e025
MB
6293 m_currentCellCoords = coords;
6294
2a7750d9
MB
6295 wxGridCellAttr* attr = GetCellAttr(coords);
6296 DrawCellHighlight(dc, attr);
6297 attr->DecRef();
66242c80
MB
6298}
6299
2d66e025 6300
c9097836
MB
6301void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
6302{
6303 int temp;
6304 wxGridCellCoords updateTopLeft, updateBottomRight;
6305
3f3dc2ef 6306 if ( m_selection )
c9097836 6307 {
3f3dc2ef
VZ
6308 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
6309 {
6310 leftCol = 0;
6311 rightCol = GetNumberCols() - 1;
6312 }
6313 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
6314 {
6315 topRow = 0;
6316 bottomRow = GetNumberRows() - 1;
6317 }
c9097836 6318 }
3f3dc2ef 6319
c9097836
MB
6320 if ( topRow > bottomRow )
6321 {
6322 temp = topRow;
6323 topRow = bottomRow;
6324 bottomRow = temp;
6325 }
6326
6327 if ( leftCol > rightCol )
6328 {
6329 temp = leftCol;
6330 leftCol = rightCol;
6331 rightCol = temp;
6332 }
6333
6334 updateTopLeft = wxGridCellCoords( topRow, leftCol );
6335 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
6336
3ed884a0
SN
6337 // First the case that we selected a completely new area
6338 if ( m_selectingTopLeft == wxGridNoCellCoords ||
6339 m_selectingBottomRight == wxGridNoCellCoords )
6340 {
6341 wxRect rect;
6342 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
6343 wxGridCellCoords ( bottomRow, rightCol ) );
2b5f62a0 6344 m_gridWin->Refresh( FALSE, &rect );
3ed884a0
SN
6345 }
6346 // Now handle changing an existing selection area.
6347 else if ( m_selectingTopLeft != updateTopLeft ||
6348 m_selectingBottomRight != updateBottomRight )
c9097836
MB
6349 {
6350 // Compute two optimal update rectangles:
6351 // Either one rectangle is a real subset of the
6352 // other, or they are (almost) disjoint!
6353 wxRect rect[4];
6354 bool need_refresh[4];
6355 need_refresh[0] =
6356 need_refresh[1] =
6357 need_refresh[2] =
6358 need_refresh[3] = FALSE;
6359 int i;
6360
6361 // Store intermediate values
6362 wxCoord oldLeft = m_selectingTopLeft.GetCol();
6363 wxCoord oldTop = m_selectingTopLeft.GetRow();
6364 wxCoord oldRight = m_selectingBottomRight.GetCol();
6365 wxCoord oldBottom = m_selectingBottomRight.GetRow();
6366
6367 // Determine the outer/inner coordinates.
6368 if (oldLeft > leftCol)
6369 {
6370 temp = oldLeft;
6371 oldLeft = leftCol;
6372 leftCol = temp;
6373 }
6374 if (oldTop > topRow )
6375 {
6376 temp = oldTop;
6377 oldTop = topRow;
6378 topRow = temp;
6379 }
6380 if (oldRight < rightCol )
6381 {
6382 temp = oldRight;
6383 oldRight = rightCol;
6384 rightCol = temp;
6385 }
6386 if (oldBottom < bottomRow)
6387 {
6388 temp = oldBottom;
6389 oldBottom = bottomRow;
6390 bottomRow = temp;
6391 }
6392
6393 // Now, either the stuff marked old is the outer
6394 // rectangle or we don't have a situation where one
6395 // is contained in the other.
6396
6397 if ( oldLeft < leftCol )
6398 {
3ed884a0
SN
6399 // Refresh the newly selected or deselected
6400 // area to the left of the old or new selection.
c9097836
MB
6401 need_refresh[0] = TRUE;
6402 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6403 oldLeft ),
6404 wxGridCellCoords ( oldBottom,
6405 leftCol - 1 ) );
6406 }
6407
6408 if ( oldTop < topRow )
6409 {
3ed884a0
SN
6410 // Refresh the newly selected or deselected
6411 // area above the old or new selection.
c9097836
MB
6412 need_refresh[1] = TRUE;
6413 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6414 leftCol ),
6415 wxGridCellCoords ( topRow - 1,
6416 rightCol ) );
6417 }
6418
6419 if ( oldRight > rightCol )
6420 {
3ed884a0
SN
6421 // Refresh the newly selected or deselected
6422 // area to the right of the old or new selection.
c9097836
MB
6423 need_refresh[2] = TRUE;
6424 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6425 rightCol + 1 ),
6426 wxGridCellCoords ( oldBottom,
6427 oldRight ) );
6428 }
6429
6430 if ( oldBottom > bottomRow )
6431 {
3ed884a0
SN
6432 // Refresh the newly selected or deselected
6433 // area below the old or new selection.
c9097836
MB
6434 need_refresh[3] = TRUE;
6435 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
6436 leftCol ),
6437 wxGridCellCoords ( oldBottom,
6438 rightCol ) );
6439 }
6440
c9097836
MB
6441 // various Refresh() calls
6442 for (i = 0; i < 4; i++ )
6443 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
6444 m_gridWin->Refresh( FALSE, &(rect[i]) );
6445 }
3ed884a0
SN
6446 // Change Selection
6447 m_selectingTopLeft = updateTopLeft;
6448 m_selectingBottomRight = updateBottomRight;
c9097836
MB
6449}
6450
2d66e025
MB
6451//
6452// ------ functions to get/send data (see also public functions)
6453//
6454
6455bool wxGrid::GetModelValues()
66242c80 6456{
bca7bfc8
SN
6457 // Hide the editor, so it won't hide a changed value.
6458 HideCellEditControl();
c6707d16 6459
2d66e025 6460 if ( m_table )
66242c80 6461 {
2d66e025 6462 // all we need to do is repaint the grid
66242c80 6463 //
2d66e025 6464 m_gridWin->Refresh();
66242c80
MB
6465 return TRUE;
6466 }
8f177c8e 6467
66242c80
MB
6468 return FALSE;
6469}
6470
2d66e025
MB
6471
6472bool wxGrid::SetModelValues()
f85afd4e 6473{
2d66e025 6474 int row, col;
8f177c8e 6475
c6707d16
SN
6476 // Disable the editor, so it won't hide a changed value.
6477 // Do we also want to save the current value of the editor first?
6478 // I think so ...
c6707d16
SN
6479 DisableCellEditControl();
6480
2d66e025
MB
6481 if ( m_table )
6482 {
6483 for ( row = 0; row < m_numRows; row++ )
f85afd4e 6484 {
2d66e025 6485 for ( col = 0; col < m_numCols; col++ )
f85afd4e 6486 {
2d66e025 6487 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
6488 }
6489 }
8f177c8e 6490
f85afd4e
MB
6491 return TRUE;
6492 }
6493
6494 return FALSE;
6495}
6496
2d66e025
MB
6497
6498
6499// Note - this function only draws cells that are in the list of
6500// exposed cells (usually set from the update region by
6501// CalcExposedCells)
6502//
d10f4bf9 6503void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
f85afd4e 6504{
2d66e025 6505 if ( !m_numRows || !m_numCols ) return;
60ff3b99 6506
dc1f566f 6507 int i, numCells = cells.GetCount();
27f35b66
SN
6508 int row, col, cell_rows, cell_cols;
6509 wxGridCellCoordsArray redrawCells;
60ff3b99 6510
27f35b66 6511 for ( i = numCells-1; i >= 0; i-- )
f85afd4e 6512 {
27f35b66
SN
6513 row = cells[i].GetRow();
6514 col = cells[i].GetCol();
6515 GetCellSize( row, col, &cell_rows, &cell_cols );
6516
6517 // If this cell is part of a multicell block, find owner for repaint
6518 if ( cell_rows <= 0 || cell_cols <= 0 )
6519 {
6520 wxGridCellCoords cell(row+cell_rows, col+cell_cols);
6521 bool marked = FALSE;
dc1f566f 6522 for ( int j = 0; j < numCells; j++ )
27f35b66
SN
6523 {
6524 if ( cell == cells[j] )
6525 {
6526 marked = TRUE;
3ed884a0 6527 break;
27f35b66
SN
6528 }
6529 }
6530 if (!marked)
6531 {
6532 int count = redrawCells.GetCount();
dc1f566f 6533 for (int j = 0; j < count; j++)
27f35b66
SN
6534 {
6535 if ( cell == redrawCells[j] )
6536 {
6537 marked = TRUE;
6538 break;
6539 }
6540 }
6541 if (!marked) redrawCells.Add( cell );
6542 }
6543 continue; // don't bother drawing this cell
6544 }
6545
6546 // If this cell is empty, find cell to left that might want to overflow
6547 if (m_table && m_table->IsEmptyCell(row, col))
6548 {
dc1f566f 6549 for ( int l = 0; l < cell_rows; l++ )
27f35b66 6550 {
dc1f566f
SN
6551 // find a cell in this row to left alreay marked for repaint
6552 int left = col;
6553 for (int k = 0; k < int(redrawCells.GetCount()); k++)
6554 if ((redrawCells[k].GetCol() < left) &&
6555 (redrawCells[k].GetRow() == row))
6556 left=redrawCells[k].GetCol();
6557
6558 if (left == col) left = 0; // oh well
6559
6560 for (int j = col-1; j >= left; j--)
27f35b66
SN
6561 {
6562 if (!m_table->IsEmptyCell(row+l, j))
6563 {
6564 if (GetCellOverflow(row+l, j))
6565 {
6566 wxGridCellCoords cell(row+l, j);
6567 bool marked = FALSE;
6568
dc1f566f 6569 for (int k = 0; k < numCells; k++)
27f35b66
SN
6570 {
6571 if ( cell == cells[k] )
6572 {
6573 marked = TRUE;
6574 break;
6575 }
6576 }
6577 if (!marked)
6578 {
6579 int count = redrawCells.GetCount();
dc1f566f 6580 for (int k = 0; k < count; k++)
27f35b66
SN
6581 {
6582 if ( cell == redrawCells[k] )
6583 {
6584 marked = TRUE;
6585 break;
6586 }
6587 }
6588 if (!marked) redrawCells.Add( cell );
6589 }
6590 }
6591 break;
6592 }
6593 }
6594 }
6595 }
d10f4bf9 6596 DrawCell( dc, cells[i] );
2d66e025 6597 }
27f35b66
SN
6598
6599 numCells = redrawCells.GetCount();
6600
6601 for ( i = numCells - 1; i >= 0; i-- )
6602 {
6603 DrawCell( dc, redrawCells[i] );
6604 }
2d66e025 6605}
8f177c8e 6606
8f177c8e 6607
7c8a8ad5
MB
6608void wxGrid::DrawGridSpace( wxDC& dc )
6609{
f6bcfd97
BP
6610 int cw, ch;
6611 m_gridWin->GetClientSize( &cw, &ch );
7c8a8ad5 6612
f6bcfd97
BP
6613 int right, bottom;
6614 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7c8a8ad5 6615
f6bcfd97
BP
6616 int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0;
6617 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0 ;
7c8a8ad5 6618
f6bcfd97
BP
6619 if ( right > rightCol || bottom > bottomRow )
6620 {
6621 int left, top;
6622 CalcUnscrolledPosition( 0, 0, &left, &top );
7c8a8ad5 6623
f6bcfd97
BP
6624 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
6625 dc.SetPen( *wxTRANSPARENT_PEN );
7c8a8ad5 6626
f6bcfd97
BP
6627 if ( right > rightCol )
6628 {
6629 dc.DrawRectangle( rightCol, top, right - rightCol, ch);
6630 }
6631
6632 if ( bottom > bottomRow )
6633 {
6634 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow);
6635 }
6636 }
7c8a8ad5
MB
6637}
6638
6639
2d66e025
MB
6640void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
6641{
ab79958a
VZ
6642 int row = coords.GetRow();
6643 int col = coords.GetCol();
60ff3b99 6644
7c1cb261 6645 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
6646 return;
6647
6648 // we draw the cell border ourselves
9496deb5 6649#if !WXGRID_DRAW_LINES
2d66e025
MB
6650 if ( m_gridLinesEnabled )
6651 DrawCellBorder( dc, coords );
796df70a 6652#endif
f85afd4e 6653
189d0213
VZ
6654 wxGridCellAttr* attr = GetCellAttr(row, col);
6655
6656 bool isCurrent = coords == m_currentCellCoords;
508011ce 6657
f6bcfd97 6658 wxRect rect = CellToRect( row, col );
f85afd4e 6659
189d0213 6660 // if the editor is shown, we should use it and not the renderer
f6bcfd97
BP
6661 // Note: However, only if it is really _shown_, i.e. not hidden!
6662 if ( isCurrent && IsCellEditControlShown() )
189d0213 6663 {
0b190b0f
VZ
6664 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6665 editor->PaintBackground(rect, attr);
6666 editor->DecRef();
189d0213
VZ
6667 }
6668 else
6669 {
6670 // but all the rest is drawn by the cell renderer and hence may be
6671 // customized
0b190b0f
VZ
6672 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
6673 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
6674 renderer->DecRef();
189d0213 6675 }
07296f0b 6676
283b7808
VZ
6677 attr->DecRef();
6678}
07296f0b 6679
283b7808 6680void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b
RD
6681{
6682 int row = m_currentCellCoords.GetRow();
6683 int col = m_currentCellCoords.GetCol();
6684
7c1cb261 6685 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
6686 return;
6687
f6bcfd97 6688 wxRect rect = CellToRect(row, col);
07296f0b 6689
b3a7510d
VZ
6690 // hmmm... what could we do here to show that the cell is disabled?
6691 // for now, I just draw a thinner border than for the other ones, but
6692 // it doesn't look really good
b3a7510d 6693
d2520c85
RD
6694 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
6695
27f35b66
SN
6696 if (penWidth > 0)
6697 {
d2520c85
RD
6698 // The center of th drawn line is where the position/width/height of
6699 // the rectangle is actually at, (on wxMSW atr least,) so we will
6700 // reduce the size of the rectangle to compensate for the thickness of
6701 // the line. If this is too strange on non wxMSW platforms then
6702 // please #ifdef this appropriately.
6703 rect.x += penWidth/2;
6704 rect.y += penWidth/2;
6705 rect.width -= penWidth-1;
6706 rect.height -= penWidth-1;
6707
6708
6709 // Now draw the rectangle
73145b0e
JS
6710 // use the cellHighlightColour if the cell is inside a selection, this
6711 // will ensure the cell is always visible.
6712 dc.SetPen(wxPen(IsInSelection(row,col)?m_selectionForeground:m_cellHighlightColour, penWidth, wxSOLID));
d2520c85
RD
6713 dc.SetBrush(*wxTRANSPARENT_BRUSH);
6714 dc.DrawRectangle(rect);
6715 }
07296f0b 6716
283b7808 6717#if 0
b3a7510d 6718 // VZ: my experiments with 3d borders...
283b7808 6719
b3a7510d 6720 // how to properly set colours for arbitrary bg?
283b7808
VZ
6721 wxCoord x1 = rect.x,
6722 y1 = rect.y,
00cf1208
RR
6723 x2 = rect.x + rect.width -1,
6724 y2 = rect.y + rect.height -1;
283b7808
VZ
6725
6726 dc.SetPen(*wxWHITE_PEN);
00cf1208
RR
6727 dc.DrawLine(x1, y1, x2, y1);
6728 dc.DrawLine(x1, y1, x1, y2);
283b7808 6729
283b7808 6730 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
00cf1208 6731 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
283b7808
VZ
6732
6733 dc.SetPen(*wxBLACK_PEN);
6734 dc.DrawLine(x1, y2, x2, y2);
00cf1208 6735 dc.DrawLine(x2, y1, x2, y2+1);
b3a7510d 6736#endif // 0
2d66e025 6737}
f85afd4e 6738
f2d76237 6739
2d66e025
MB
6740void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
6741{
2d66e025
MB
6742 int row = coords.GetRow();
6743 int col = coords.GetCol();
7c1cb261
VZ
6744 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6745 return;
6746
6747 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
60ff3b99 6748
27f35b66
SN
6749 wxRect rect = CellToRect( row, col );
6750
2d66e025
MB
6751 // right hand border
6752 //
27f35b66
SN
6753 dc.DrawLine( rect.x + rect.width, rect.y,
6754 rect.x + rect.width, rect.y + rect.height + 1 );
2d66e025
MB
6755
6756 // bottom border
6757 //
27f35b66
SN
6758 dc.DrawLine( rect.x, rect.y + rect.height,
6759 rect.x + rect.width, rect.y + rect.height);
f85afd4e
MB
6760}
6761
d10f4bf9 6762void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells)
b3a7510d 6763{
2a7750d9
MB
6764 // This if block was previously in wxGrid::OnPaint but that doesn't
6765 // seem to get called under wxGTK - MB
6766 //
6767 if ( m_currentCellCoords == wxGridNoCellCoords &&
6768 m_numRows && m_numCols )
6769 {
6770 m_currentCellCoords.Set(0, 0);
6771 }
6772
f6bcfd97 6773 if ( IsCellEditControlShown() )
99306db2
VZ
6774 {
6775 // don't show highlight when the edit control is shown
6776 return;
6777 }
6778
b3a7510d
VZ
6779 // if the active cell was repainted, repaint its highlight too because it
6780 // might have been damaged by the grid lines
d10f4bf9 6781 size_t count = cells.GetCount();
b3a7510d
VZ
6782 for ( size_t n = 0; n < count; n++ )
6783 {
d10f4bf9 6784 if ( cells[n] == m_currentCellCoords )
b3a7510d
VZ
6785 {
6786 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
6787 DrawCellHighlight(dc, attr);
6788 attr->DecRef();
6789
6790 break;
6791 }
6792 }
6793}
2d66e025 6794
2d66e025
MB
6795// TODO: remove this ???
6796// This is used to redraw all grid lines e.g. when the grid line colour
6797// has been changed
6798//
33ac7e6f 6799void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
f85afd4e 6800{
27f35b66
SN
6801#if !WXGRID_DRAW_LINES
6802 return;
6803#endif
6804
60ff3b99
VZ
6805 if ( !m_gridLinesEnabled ||
6806 !m_numRows ||
2d66e025 6807 !m_numCols ) return;
f85afd4e 6808
2d66e025 6809 int top, bottom, left, right;
796df70a 6810
f6bcfd97 6811#if 0 //#ifndef __WXGTK__
508011ce
VZ
6812 if (reg.IsEmpty())
6813 {
796df70a
SN
6814 int cw, ch;
6815 m_gridWin->GetClientSize(&cw, &ch);
6816
6817 // virtual coords of visible area
6818 //
6819 CalcUnscrolledPosition( 0, 0, &left, &top );
6820 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6821 }
508011ce
VZ
6822 else
6823 {
796df70a
SN
6824 wxCoord x, y, w, h;
6825 reg.GetBox(x, y, w, h);
6826 CalcUnscrolledPosition( x, y, &left, &top );
6827 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
6828 }
8e217128
RR
6829#else
6830 int cw, ch;
6831 m_gridWin->GetClientSize(&cw, &ch);
6832 CalcUnscrolledPosition( 0, 0, &left, &top );
6833 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6834#endif
f85afd4e 6835
9496deb5
MB
6836 // avoid drawing grid lines past the last row and col
6837 //
7c1cb261
VZ
6838 right = wxMin( right, GetColRight(m_numCols - 1) );
6839 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
9496deb5 6840
27f35b66
SN
6841 // no gridlines inside multicells, clip them out
6842 int leftCol = XToCol(left);
6843 int topRow = YToRow(top);
6844 int rightCol = XToCol(right);
6845 int bottomRow = YToRow(bottom);
6846 wxRegion clippedcells(0, 0, cw, ch);
6847
6848 if ((leftCol >= 0) && (topRow >= 0))
6849 {
6850 if (rightCol < 0) rightCol = m_numCols;
6851 if (bottomRow < 0) bottomRow = m_numRows;
6852
6853 int i, j, cell_rows, cell_cols;
6854 wxRect rect;
6855
6856 for (j=topRow; j<bottomRow; j++)
6857 {
6858 for (i=leftCol; i<rightCol; i++)
6859 {
6860 GetCellSize( j, i, &cell_rows, &cell_cols );
6861 if ((cell_rows > 1) || (cell_cols > 1))
6862 {
6863 rect = CellToRect(j,i);
6864 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
6865 clippedcells.Subtract(rect);
6866 }
6867 else if ((cell_rows < 0) || (cell_cols < 0))
6868 {
6869 rect = CellToRect(j+cell_rows, i+cell_cols);
6870 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
6871 clippedcells.Subtract(rect);
6872 }
6873 }
6874 }
6875 }
6876 dc.SetClippingRegion( clippedcells );
6877
2d66e025 6878 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
f85afd4e 6879
2d66e025 6880 // horizontal grid lines
f85afd4e 6881 //
60ff3b99 6882 int i;
33188aa4 6883 for ( i = internalYToRow(top); i < m_numRows; i++ )
f85afd4e 6884 {
6d55126d 6885 int bot = GetRowBottom(i) - 1;
7c1cb261 6886
6d55126d 6887 if ( bot > bottom )
2d66e025
MB
6888 {
6889 break;
6890 }
7c1cb261 6891
6d55126d 6892 if ( bot >= top )
2d66e025 6893 {
6d55126d 6894 dc.DrawLine( left, bot, right, bot );
2d66e025 6895 }
f85afd4e
MB
6896 }
6897
f85afd4e 6898
2d66e025
MB
6899 // vertical grid lines
6900 //
33188aa4 6901 for ( i = internalXToCol(left); i < m_numCols; i++ )
f85afd4e 6902 {
7c1cb261
VZ
6903 int colRight = GetColRight(i) - 1;
6904 if ( colRight > right )
2d66e025
MB
6905 {
6906 break;
6907 }
7c1cb261
VZ
6908
6909 if ( colRight >= left )
2d66e025 6910 {
7c1cb261 6911 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
6912 }
6913 }
27f35b66 6914 dc.DestroyClippingRegion();
2d66e025 6915}
f85afd4e 6916
8f177c8e 6917
d10f4bf9 6918void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
2d66e025 6919{
f6bcfd97 6920 if ( !m_numRows ) return;
60ff3b99 6921
2d66e025 6922 size_t i;
d10f4bf9 6923 size_t numLabels = rows.GetCount();
60ff3b99 6924
2d66e025
MB
6925 for ( i = 0; i < numLabels; i++ )
6926 {
d10f4bf9 6927 DrawRowLabel( dc, rows[i] );
60ff3b99 6928 }
f85afd4e
MB
6929}
6930
6931
2d66e025 6932void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 6933{
7c1cb261
VZ
6934 if ( GetRowHeight(row) <= 0 )
6935 return;
60ff3b99 6936
7c1cb261
VZ
6937 int rowTop = GetRowTop(row),
6938 rowBottom = GetRowBottom(row) - 1;
b99be8fb 6939
73145b0e 6940 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
b0d6ff2f 6941 dc.DrawLine( m_rowLabelWidth-1, rowTop,
7c1cb261 6942 m_rowLabelWidth-1, rowBottom );
b99be8fb 6943
73145b0e
JS
6944 dc.DrawLine( 0, rowTop, 0, rowBottom );
6945
6946 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
b99be8fb 6947
2d66e025 6948 dc.SetPen( *wxWHITE_PEN );
73145b0e
JS
6949 dc.DrawLine( 1, rowTop, 1, rowBottom );
6950 dc.DrawLine( 1, rowTop, m_rowLabelWidth-1, rowTop );
60ff3b99 6951
f85afd4e 6952 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
6953 dc.SetTextForeground( GetLabelTextColour() );
6954 dc.SetFont( GetLabelFont() );
8f177c8e 6955
f85afd4e 6956 int hAlign, vAlign;
2d66e025 6957 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 6958
2d66e025
MB
6959 wxRect rect;
6960 rect.SetX( 2 );
7c1cb261 6961 rect.SetY( GetRowTop(row) + 2 );
2d66e025 6962 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 6963 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 6964 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
6965}
6966
6967
d10f4bf9 6968void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
f85afd4e 6969{
f6bcfd97 6970 if ( !m_numCols ) return;
60ff3b99 6971
2d66e025 6972 size_t i;
d10f4bf9 6973 size_t numLabels = cols.GetCount();
60ff3b99 6974
2d66e025 6975 for ( i = 0; i < numLabels; i++ )
f85afd4e 6976 {
d10f4bf9 6977 DrawColLabel( dc, cols[i] );
60ff3b99 6978 }
f85afd4e
MB
6979}
6980
6981
2d66e025 6982void wxGrid::DrawColLabel( wxDC& dc, int col )
f85afd4e 6983{
7c1cb261
VZ
6984 if ( GetColWidth(col) <= 0 )
6985 return;
60ff3b99 6986
7c1cb261
VZ
6987 int colLeft = GetColLeft(col),
6988 colRight = GetColRight(col) - 1;
b99be8fb 6989
73145b0e 6990 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
7c1cb261
VZ
6991 dc.DrawLine( colRight, 0,
6992 colRight, m_colLabelHeight-1 );
b99be8fb 6993
73145b0e
JS
6994 dc.DrawLine( colLeft, 0, colRight, 0 );
6995
b0d6ff2f 6996 dc.DrawLine( colLeft, m_colLabelHeight-1,
73145b0e 6997 colRight+1, m_colLabelHeight-1 );
b99be8fb 6998
f85afd4e 6999 dc.SetPen( *wxWHITE_PEN );
73145b0e
JS
7000 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
7001 dc.DrawLine( colLeft, 1, colRight, 1 );
f85afd4e 7002
b0d6ff2f
MB
7003 dc.SetBackgroundMode( wxTRANSPARENT );
7004 dc.SetTextForeground( GetLabelTextColour() );
7005 dc.SetFont( GetLabelFont() );
8f177c8e 7006
f85afd4e 7007 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
7008 dc.SetTextForeground( GetLabelTextColour() );
7009 dc.SetFont( GetLabelFont() );
8f177c8e 7010
f85afd4e 7011 int hAlign, vAlign;
2d66e025 7012 GetColLabelAlignment( &hAlign, &vAlign );
60ff3b99 7013
2d66e025 7014 wxRect rect;
7c1cb261 7015 rect.SetX( colLeft + 2 );
2d66e025 7016 rect.SetY( 2 );
7c1cb261 7017 rect.SetWidth( GetColWidth(col) - 4 );
2d66e025 7018 rect.SetHeight( m_colLabelHeight - 4 );
60ff3b99 7019 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
f85afd4e
MB
7020}
7021
2d66e025
MB
7022void wxGrid::DrawTextRectangle( wxDC& dc,
7023 const wxString& value,
7024 const wxRect& rect,
7025 int horizAlign,
7026 int vertAlign )
f85afd4e 7027{
2d66e025 7028 wxArrayString lines;
f85afd4e 7029
2d66e025 7030 StringToLines( value, lines );
d10f4bf9
VZ
7031
7032
7033 //Forward to new API.
7034 DrawTextRectangle( dc,
7035 lines,
7036 rect,
7037 horizAlign,
7038 vertAlign );
7039
7040}
7041
7042void wxGrid::DrawTextRectangle( wxDC& dc,
7043 const wxArrayString& lines,
7044 const wxRect& rect,
7045 int horizAlign,
7046 int vertAlign )
7047{
7048 long textWidth, textHeight;
7049 long lineWidth, lineHeight;
73145b0e 7050 int nLines;
d10f4bf9 7051
a373d23b 7052 dc.SetClippingRegion( rect );
73145b0e
JS
7053
7054 nLines = lines.GetCount();
7055 if( nLines > 0 )
2d66e025 7056 {
73145b0e
JS
7057 int l;
7058 float x, y;
7059 GetTextBoxSize(dc, lines, &textWidth, &textHeight);
7060 switch( vertAlign )
7061 {
7062 case wxALIGN_BOTTOM:
7063 y = rect.y + (rect.height - textHeight - 1);
7064 break;
f85afd4e 7065
73145b0e
JS
7066 case wxALIGN_CENTRE:
7067 y = rect.y + ((rect.height - textHeight)/2);
7068 break;
f85afd4e 7069
73145b0e
JS
7070 case wxALIGN_TOP:
7071 default:
7072 y = rect.y + 1;
7073 break;
7074 }
f85afd4e 7075
73145b0e
JS
7076 // Align each line of a multi-line label
7077 for( l = 0; l < nLines; l++ )
7078 {
7079 dc.GetTextExtent(lines[l], &lineWidth, &lineHeight);
f85afd4e 7080
73145b0e 7081 switch( horizAlign )
2d66e025 7082 {
73145b0e
JS
7083 case wxALIGN_RIGHT:
7084 x = rect.x + (rect.width - lineWidth - 1);
7085 break;
f85afd4e 7086
73145b0e
JS
7087 case wxALIGN_CENTRE:
7088 x = rect.x + ((rect.width - lineWidth)/2);
7089 break;
f85afd4e 7090
73145b0e
JS
7091 case wxALIGN_LEFT:
7092 default:
7093 x = rect.x + 1;
7094 break;
f85afd4e
MB
7095 }
7096
73145b0e
JS
7097 dc.DrawText( lines[l], (int)x, (int)y );
7098 y += lineHeight;
7099 }
f85afd4e 7100 }
f85afd4e 7101 dc.DestroyClippingRegion();
f85afd4e
MB
7102}
7103
7104
7105// Split multi line text up into an array of strings. Any existing
7106// contents of the string array are preserved.
7107//
7108void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
7109{
f85afd4e
MB
7110 int startPos = 0;
7111 int pos;
6d004f67 7112 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 7113 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 7114
2433bb2e 7115 while ( startPos < (int)tVal.Length() )
f85afd4e 7116 {
2433bb2e 7117 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
7118 if ( pos < 0 )
7119 {
7120 break;
7121 }
7122 else if ( pos == 0 )
7123 {
7124 lines.Add( wxEmptyString );
7125 }
7126 else
7127 {
2433bb2e 7128 lines.Add( value.Mid(startPos, pos) );
f85afd4e
MB
7129 }
7130 startPos += pos+1;
7131 }
b540eb2b 7132 if ( startPos < (int)value.Length() )
f85afd4e
MB
7133 {
7134 lines.Add( value.Mid( startPos ) );
7135 }
7136}
7137
7138
7139void wxGrid::GetTextBoxSize( wxDC& dc,
d10f4bf9 7140 const wxArrayString& lines,
f85afd4e
MB
7141 long *width, long *height )
7142{
7143 long w = 0;
7144 long h = 0;
7145 long lineW, lineH;
7146
b540eb2b 7147 size_t i;
f85afd4e
MB
7148 for ( i = 0; i < lines.GetCount(); i++ )
7149 {
7150 dc.GetTextExtent( lines[i], &lineW, &lineH );
7151 w = wxMax( w, lineW );
7152 h += lineH;
7153 }
7154
7155 *width = w;
7156 *height = h;
7157}
7158
f6bcfd97
BP
7159//
7160// ------ Batch processing.
7161//
7162void wxGrid::EndBatch()
7163{
7164 if ( m_batchCount > 0 )
7165 {
7166 m_batchCount--;
7167 if ( !m_batchCount )
7168 {
7169 CalcDimensions();
7170 m_rowLabelWin->Refresh();
7171 m_colLabelWin->Refresh();
7172 m_cornerLabelWin->Refresh();
7173 m_gridWin->Refresh();
7174 }
7175 }
7176}
f85afd4e 7177
d8232393
MB
7178// Use this, rather than wxWindow::Refresh(), to force an immediate
7179// repainting of the grid. Has no effect if you are already inside a
7180// BeginBatch / EndBatch block.
7181//
7182void wxGrid::ForceRefresh()
7183{
7184 BeginBatch();
7185 EndBatch();
7186}
7187
7188
f85afd4e 7189//
2d66e025 7190// ------ Edit control functions
f85afd4e
MB
7191//
7192
2d66e025
MB
7193
7194void wxGrid::EnableEditing( bool edit )
f85afd4e 7195{
2d66e025
MB
7196 // TODO: improve this ?
7197 //
7198 if ( edit != m_editable )
f85afd4e 7199 {
632efa47 7200 if(!edit) EnableCellEditControl(edit);
2d66e025 7201 m_editable = edit;
f85afd4e 7202 }
f85afd4e
MB
7203}
7204
7205
2d66e025 7206void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 7207{
2c9a89e0
RD
7208 if (! m_editable)
7209 return;
7210
dcfe4c3d
SN
7211 if ( m_currentCellCoords == wxGridNoCellCoords )
7212 SetCurrentCell( 0, 0 );
60ff3b99 7213
2c9a89e0
RD
7214 if ( enable != m_cellEditCtrlEnabled )
7215 {
dcfe4c3d 7216 if ( enable )
f85afd4e 7217 {
97a9929e
VZ
7218 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
7219 return;
fe7b9ed6 7220
97a9929e 7221 // this should be checked by the caller!
b54ba671
VZ
7222 wxASSERT_MSG( CanEnableCellControl(),
7223 _T("can't enable editing for this cell!") );
7224
7225 // do it before ShowCellEditControl()
dcfe4c3d 7226 m_cellEditCtrlEnabled = enable;
b54ba671 7227
2d66e025 7228 ShowCellEditControl();
2d66e025
MB
7229 }
7230 else
7231 {
97a9929e
VZ
7232 //FIXME:add veto support
7233 SendEvent( wxEVT_GRID_EDITOR_HIDDEN);
fe7b9ed6 7234
97a9929e 7235 HideCellEditControl();
2d66e025 7236 SaveEditControlValue();
b54ba671
VZ
7237
7238 // do it after HideCellEditControl()
dcfe4c3d 7239 m_cellEditCtrlEnabled = enable;
f85afd4e 7240 }
f85afd4e 7241 }
f85afd4e 7242}
f85afd4e 7243
b54ba671 7244bool wxGrid::IsCurrentCellReadOnly() const
283b7808 7245{
b54ba671
VZ
7246 // const_cast
7247 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
7248 bool readonly = attr->IsReadOnly();
7249 attr->DecRef();
283b7808 7250
b54ba671
VZ
7251 return readonly;
7252}
283b7808 7253
b54ba671
VZ
7254bool wxGrid::CanEnableCellControl() const
7255{
7256 return m_editable && !IsCurrentCellReadOnly();
7257}
7258
7259bool wxGrid::IsCellEditControlEnabled() const
7260{
7261 // the cell edit control might be disable for all cells or just for the
7262 // current one if it's read only
7263 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
283b7808
VZ
7264}
7265
f6bcfd97
BP
7266bool wxGrid::IsCellEditControlShown() const
7267{
7268 bool isShown = FALSE;
7269
7270 if ( m_cellEditCtrlEnabled )
7271 {
7272 int row = m_currentCellCoords.GetRow();
7273 int col = m_currentCellCoords.GetCol();
7274 wxGridCellAttr* attr = GetCellAttr(row, col);
7275 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
7276 attr->DecRef();
7277
7278 if ( editor )
7279 {
7280 if ( editor->IsCreated() )
7281 {
7282 isShown = editor->GetControl()->IsShown();
7283 }
7284
7285 editor->DecRef();
7286 }
7287 }
7288
7289 return isShown;
7290}
7291
2d66e025 7292void wxGrid::ShowCellEditControl()
f85afd4e 7293{
2d66e025 7294 if ( IsCellEditControlEnabled() )
f85afd4e 7295 {
2d66e025
MB
7296 if ( !IsVisible( m_currentCellCoords ) )
7297 {
3ca6a5f0 7298 m_cellEditCtrlEnabled = FALSE;
2d66e025
MB
7299 return;
7300 }
7301 else
7302 {
2c9a89e0
RD
7303 wxRect rect = CellToRect( m_currentCellCoords );
7304 int row = m_currentCellCoords.GetRow();
7305 int col = m_currentCellCoords.GetCol();
2d66e025 7306
27f35b66
SN
7307 // if this is part of a multicell, find owner (topleft)
7308 int cell_rows, cell_cols;
7309 GetCellSize( row, col, &cell_rows, &cell_cols );
7310 if ( cell_rows <= 0 || cell_cols <= 0 )
7311 {
7312 row += cell_rows;
7313 col += cell_cols;
7314 m_currentCellCoords.SetRow( row );
7315 m_currentCellCoords.SetCol( col );
7316 }
7317
2d66e025
MB
7318 // convert to scrolled coords
7319 //
99306db2 7320 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
508011ce 7321
99306db2
VZ
7322 // done in PaintBackground()
7323#if 0
7324 // erase the highlight and the cell contents because the editor
7325 // might not cover the entire cell
7326 wxClientDC dc( m_gridWin );
7327 PrepareDC( dc );
7328 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7329 dc.SetPen(*wxTRANSPARENT_PEN);
7330 dc.DrawRectangle(rect);
7331#endif // 0
d2fdd8d2 7332
99306db2 7333 // cell is shifted by one pixel
68c5a31c 7334 // However, don't allow x or y to become negative
70e8d961 7335 // since the SetSize() method interprets that as
68c5a31c
SN
7336 // "don't change."
7337 if (rect.x > 0)
7338 rect.x--;
7339 if (rect.y > 0)
7340 rect.y--;
f0102d2a 7341
508011ce 7342 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7343 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
7344 if ( !editor->IsCreated() )
7345 {
2c9a89e0
RD
7346 editor->Create(m_gridWin, -1,
7347 new wxGridCellEditorEvtHandler(this, editor));
bf7945ce
RD
7348
7349 wxGridEditorCreatedEvent evt(GetId(),
7350 wxEVT_GRID_EDITOR_CREATED,
7351 this,
7352 row,
7353 col,
7354 editor->GetControl());
7355 GetEventHandler()->ProcessEvent(evt);
2d66e025
MB
7356 }
7357
0b190b0f 7358
27f35b66 7359 // resize editor to overflow into righthand cells if allowed
39b80349 7360 int maxWidth = rect.width;
27f35b66
SN
7361 wxString value = GetCellValue(row, col);
7362 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
7363 {
39b80349
SN
7364 int y;
7365 GetTextExtent(value, &maxWidth, &y,
2b5f62a0 7366 NULL, NULL, &attr->GetFont());
39b80349
SN
7367 if (maxWidth < rect.width) maxWidth = rect.width;
7368 }
7369 int client_right = m_gridWin->GetClientSize().GetWidth();
7370 if (rect.x+maxWidth > client_right)
2b5f62a0 7371 maxWidth = client_right - rect.x;
39b80349 7372
2b5f62a0
VZ
7373 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
7374 {
39b80349 7375 GetCellSize( row, col, &cell_rows, &cell_cols );
2b5f62a0
VZ
7376 // may have changed earlier
7377 for (int i = col+cell_cols; i < m_numCols; i++)
7378 {
39b80349
SN
7379 int c_rows, c_cols;
7380 GetCellSize( row, i, &c_rows, &c_cols );
2b5f62a0
VZ
7381 // looks weird going over a multicell
7382 if (m_table->IsEmptyCell(row,i) &&
7383 (rect.width < maxWidth) && (c_rows == 1))
7384 rect.width += GetColWidth(i);
7385 else
7386 break;
7387 }
39b80349 7388 if (rect.GetRight() > client_right)
2b5f62a0 7389 rect.SetRight(client_right-1);
27f35b66 7390 }
73145b0e
JS
7391
7392// DJC (MAPTEK) 19-Feb-2001 do set size prior to showing the control
2c9a89e0 7393 editor->SetSize( rect );
73145b0e
JS
7394 editor->Show( TRUE, attr );
7395// DJC (MAPTEK) 19-Jun-2001 recalc dimensions in case we need to
7396// expand the scrolled window to account for editor
7397 CalcDimensions();
99306db2 7398
3da93aae 7399 editor->BeginEdit(row, col, this);
0b190b0f
VZ
7400
7401 editor->DecRef();
2c9a89e0 7402 attr->DecRef();
2b5f62a0 7403 }
f85afd4e
MB
7404 }
7405}
7406
7407
2d66e025 7408void wxGrid::HideCellEditControl()
f85afd4e 7409{
2d66e025 7410 if ( IsCellEditControlEnabled() )
f85afd4e 7411 {
2c9a89e0
RD
7412 int row = m_currentCellCoords.GetRow();
7413 int col = m_currentCellCoords.GetCol();
7414
3da93aae 7415 wxGridCellAttr* attr = GetCellAttr(row, col);
0b190b0f
VZ
7416 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7417 editor->Show( FALSE );
7418 editor->DecRef();
2c9a89e0
RD
7419 attr->DecRef();
7420 m_gridWin->SetFocus();
27f35b66
SN
7421 // refresh whole row to the right
7422 wxRect rect( CellToRect(row, col) );
7423 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
7424 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
f6bcfd97 7425 m_gridWin->Refresh( FALSE, &rect );
f85afd4e 7426 }
2d66e025 7427}
8f177c8e 7428
2d66e025 7429
2d66e025
MB
7430void wxGrid::SaveEditControlValue()
7431{
3da93aae
VZ
7432 if ( IsCellEditControlEnabled() )
7433 {
2c9a89e0
RD
7434 int row = m_currentCellCoords.GetRow();
7435 int col = m_currentCellCoords.GetCol();
8f177c8e 7436
fe7b9ed6
VZ
7437 wxString oldval = GetCellValue(row,col);
7438
3da93aae 7439 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7440 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3324d5f5 7441 bool changed = editor->EndEdit(row, col, this);
2d66e025 7442
0b190b0f 7443 editor->DecRef();
2c9a89e0 7444 attr->DecRef();
2d66e025 7445
3da93aae
VZ
7446 if (changed)
7447 {
fe7b9ed6 7448 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
2d66e025 7449 m_currentCellCoords.GetRow(),
fe7b9ed6
VZ
7450 m_currentCellCoords.GetCol() ) < 0 ) {
7451
97a9929e
VZ
7452 // Event has been vetoed, set the data back.
7453 SetCellValue(row,col,oldval);
7454 }
2d66e025 7455 }
f85afd4e
MB
7456 }
7457}
7458
2d66e025
MB
7459
7460//
60ff3b99
VZ
7461// ------ Grid location functions
7462// Note that all of these functions work with the logical coordinates of
2d66e025
MB
7463// grid cells and labels so you will need to convert from device
7464// coordinates for mouse events etc.
7465//
7466
7467void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
f85afd4e 7468{
58dd5b3b
MB
7469 int row = YToRow(y);
7470 int col = XToCol(x);
7471
7472 if ( row == -1 || col == -1 )
7473 {
7474 coords = wxGridNoCellCoords;
7475 }
7476 else
7477 {
7478 coords.Set( row, col );
7479 }
2d66e025 7480}
f85afd4e 7481
8f177c8e 7482
b1da8107
SN
7483// Internal Helper function for computing row or column from some
7484// (unscrolled) coordinate value, using either
70e8d961 7485// m_defaultRowHeight/m_defaultColWidth or binary search on array
b1da8107
SN
7486// of m_rowBottoms/m_ColRights to speed up the search!
7487
7488static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
64e15340
JS
7489 const wxArrayInt& BorderArray, int nMax,
7490 bool maxOnOverflow)
2d66e025 7491{
b1da8107
SN
7492 if (!defaultDist)
7493 defaultDist = 1;
1af546bf
VZ
7494 size_t i_max = coord / defaultDist,
7495 i_min = 0;
d57ad377 7496
b1da8107
SN
7497 if (BorderArray.IsEmpty())
7498 {
d57ad377 7499 if((int) i_max < nMax)
64e15340 7500 return i_max;
d57ad377 7501 return maxOnOverflow ? nMax - 1 : -1;
b1da8107 7502 }
8f177c8e 7503
b1da8107
SN
7504 if ( i_max >= BorderArray.GetCount())
7505 i_max = BorderArray.GetCount() - 1;
70e8d961 7506 else
f85afd4e 7507 {
b1da8107
SN
7508 if ( coord >= BorderArray[i_max])
7509 {
7510 i_min = i_max;
7511 i_max = coord / minDist;
7512 }
7513 if ( i_max >= BorderArray.GetCount())
7514 i_max = BorderArray.GetCount() - 1;
f85afd4e 7515 }
33188aa4 7516 if ( coord >= BorderArray[i_max])
1af546bf 7517 return maxOnOverflow ? (int)i_max : -1;
68c5a31c
SN
7518 if ( coord < BorderArray[0] )
7519 return 0;
2d66e025 7520
68c5a31c 7521 while ( i_max - i_min > 0 )
b1da8107
SN
7522 {
7523 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
33188aa4 7524 0, _T("wxGrid: internal error in CoordToRowOrCol"));
b1da8107 7525 if (coord >= BorderArray[ i_max - 1])
b1da8107 7526 return i_max;
b1da8107
SN
7527 else
7528 i_max--;
7529 int median = i_min + (i_max - i_min + 1) / 2;
7530 if (coord < BorderArray[median])
7531 i_max = median;
7532 else
7533 i_min = median;
7534 }
7535 return i_max;
f85afd4e
MB
7536}
7537
b1da8107 7538int wxGrid::YToRow( int y )
f85afd4e 7539{
b1da8107 7540 return CoordToRowOrCol(y, m_defaultRowHeight,
64e15340 7541 WXGRID_MIN_ROW_HEIGHT, m_rowBottoms, m_numRows, FALSE);
b1da8107 7542}
8f177c8e 7543
f85afd4e 7544
b1da8107
SN
7545int wxGrid::XToCol( int x )
7546{
7547 return CoordToRowOrCol(x, m_defaultColWidth,
64e15340 7548 WXGRID_MIN_COL_WIDTH, m_colRights, m_numCols, FALSE);
2d66e025 7549}
8f177c8e 7550
2d66e025
MB
7551
7552// return the row number that that the y coord is near the edge of, or
7553// -1 if not near an edge
7554//
7555int wxGrid::YToEdgeOfRow( int y )
7556{
33188aa4
SN
7557 int i;
7558 i = internalYToRow(y);
7559
7560 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
2d66e025 7561 {
33188aa4
SN
7562 // We know that we are in row i, test whether we are
7563 // close enough to lower or upper border, respectively.
7564 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
7565 return i;
7566 else if( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
7567 return i - 1;
f85afd4e 7568 }
2d66e025
MB
7569
7570 return -1;
7571}
7572
7573
7574// return the col number that that the x coord is near the edge of, or
7575// -1 if not near an edge
7576//
7577int wxGrid::XToEdgeOfCol( int x )
7578{
33188aa4
SN
7579 int i;
7580 i = internalXToCol(x);
7581
7582 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
f85afd4e 7583 {
33188aa4
SN
7584 // We know that we are in column i, test whether we are
7585 // close enough to right or left border, respectively.
7586 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
7587 return i;
7588 else if( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
7589 return i - 1;
f85afd4e 7590 }
2d66e025
MB
7591
7592 return -1;
f85afd4e
MB
7593}
7594
2d66e025
MB
7595
7596wxRect wxGrid::CellToRect( int row, int col )
f85afd4e 7597{
2d66e025 7598 wxRect rect( -1, -1, -1, -1 );
f85afd4e 7599
2d66e025
MB
7600 if ( row >= 0 && row < m_numRows &&
7601 col >= 0 && col < m_numCols )
f85afd4e 7602 {
27f35b66
SN
7603 int i, cell_rows, cell_cols;
7604 rect.width = rect.height = 0;
7605 GetCellSize( row, col, &cell_rows, &cell_cols );
7606 // if negative then find multicell owner
7607 if (cell_rows < 0) row += cell_rows;
7608 if (cell_cols < 0) col += cell_cols;
7609 GetCellSize( row, col, &cell_rows, &cell_cols );
7610
7c1cb261
VZ
7611 rect.x = GetColLeft(col);
7612 rect.y = GetRowTop(row);
27f35b66
SN
7613 for (i=col; i<col+cell_cols; i++)
7614 rect.width += GetColWidth(i);
7615 for (i=row; i<row+cell_rows; i++)
7616 rect.height += GetRowHeight(i);
f85afd4e
MB
7617 }
7618
f6bcfd97
BP
7619 // if grid lines are enabled, then the area of the cell is a bit smaller
7620 if (m_gridLinesEnabled) {
7621 rect.width -= 1;
7622 rect.height -= 1;
7623 }
2d66e025
MB
7624 return rect;
7625}
7626
7627
7628bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
7629{
7630 // get the cell rectangle in logical coords
7631 //
7632 wxRect r( CellToRect( row, col ) );
60ff3b99 7633
2d66e025
MB
7634 // convert to device coords
7635 //
7636 int left, top, right, bottom;
7637 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7638 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 7639
2d66e025
MB
7640 // check against the client area of the grid window
7641 //
7642 int cw, ch;
7643 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 7644
2d66e025 7645 if ( wholeCellVisible )
f85afd4e 7646 {
2d66e025 7647 // is the cell wholly visible ?
8f177c8e 7648 //
2d66e025
MB
7649 return ( left >= 0 && right <= cw &&
7650 top >= 0 && bottom <= ch );
7651 }
7652 else
7653 {
7654 // is the cell partly visible ?
7655 //
7656 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
7657 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
7658 }
7659}
7660
7661
7662// make the specified cell location visible by doing a minimal amount
7663// of scrolling
7664//
7665void wxGrid::MakeCellVisible( int row, int col )
7666{
275c4ae4 7667
2d66e025 7668 int i;
60ff3b99 7669 int xpos = -1, ypos = -1;
2d66e025
MB
7670
7671 if ( row >= 0 && row < m_numRows &&
7672 col >= 0 && col < m_numCols )
7673 {
7674 // get the cell rectangle in logical coords
7675 //
7676 wxRect r( CellToRect( row, col ) );
60ff3b99 7677
2d66e025
MB
7678 // convert to device coords
7679 //
7680 int left, top, right, bottom;
7681 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7682 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 7683
2d66e025
MB
7684 int cw, ch;
7685 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 7686
2d66e025 7687 if ( top < 0 )
3f296516 7688 {
2d66e025 7689 ypos = r.GetTop();
3f296516 7690 }
2d66e025
MB
7691 else if ( bottom > ch )
7692 {
7693 int h = r.GetHeight();
7694 ypos = r.GetTop();
7695 for ( i = row-1; i >= 0; i-- )
7696 {
7c1cb261
VZ
7697 int rowHeight = GetRowHeight(i);
7698 if ( h + rowHeight > ch )
7699 break;
2d66e025 7700
7c1cb261
VZ
7701 h += rowHeight;
7702 ypos -= rowHeight;
2d66e025 7703 }
f0102d2a
VZ
7704
7705 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7706 // have rounding errors (this is important, because if we do, we
7707 // might not scroll at all and some cells won't be redrawn)
275c4ae4
RD
7708 //
7709 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7710 // scroll unit...
97a9929e 7711 ypos += GRID_SCROLL_LINE_Y;
2d66e025
MB
7712 }
7713
7714 if ( left < 0 )
7715 {
7716 xpos = r.GetLeft();
7717 }
7718 else if ( right > cw )
7719 {
73145b0e
JS
7720 // DJC MAPTEK
7721 // position the view so that the cell is on the right
7722 int x0, y0;
7723 CalcUnscrolledPosition(0, 0, &x0, &y0);
7724 xpos = x0 + (right - cw);
f0102d2a
VZ
7725
7726 // see comment for ypos above
97a9929e 7727 xpos += GRID_SCROLL_LINE_X;
2d66e025
MB
7728 }
7729
7730 if ( xpos != -1 || ypos != -1 )
7731 {
97a9929e
VZ
7732 if ( xpos != -1 )
7733 xpos /= GRID_SCROLL_LINE_X;
7734 if ( ypos != -1 )
7735 ypos /= GRID_SCROLL_LINE_Y;
2d66e025
MB
7736 Scroll( xpos, ypos );
7737 AdjustScrollbars();
7738 }
7739 }
7740}
7741
7742
7743//
7744// ------ Grid cursor movement functions
7745//
7746
5c8fc7c1 7747bool wxGrid::MoveCursorUp( bool expandSelection )
2d66e025
MB
7748{
7749 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7750 m_currentCellCoords.GetRow() >= 0 )
2d66e025 7751 {
f6bcfd97 7752 if ( expandSelection)
d95b0c2b
SN
7753 {
7754 if ( m_selectingKeyboard == wxGridNoCellCoords )
7755 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7756 if ( m_selectingKeyboard.GetRow() > 0 )
7757 {
7758 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
7759 MakeCellVisible( m_selectingKeyboard.GetRow(),
7760 m_selectingKeyboard.GetCol() );
c9097836 7761 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7762 }
d95b0c2b 7763 }
f6bcfd97 7764 else if ( m_currentCellCoords.GetRow() > 0 )
aa5e1f75
SN
7765 {
7766 ClearSelection();
7767 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
7768 m_currentCellCoords.GetCol() );
d95b0c2b
SN
7769 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
7770 m_currentCellCoords.GetCol() );
aa5e1f75 7771 }
f6bcfd97
BP
7772 else
7773 return FALSE;
8f177c8e 7774 return TRUE;
f85afd4e 7775 }
2d66e025
MB
7776
7777 return FALSE;
7778}
7779
7780
5c8fc7c1 7781bool wxGrid::MoveCursorDown( bool expandSelection )
2d66e025 7782{
2d66e025 7783 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7784 m_currentCellCoords.GetRow() < m_numRows )
f85afd4e 7785 {
5c8fc7c1 7786 if ( expandSelection )
d95b0c2b
SN
7787 {
7788 if ( m_selectingKeyboard == wxGridNoCellCoords )
7789 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7790 if ( m_selectingKeyboard.GetRow() < m_numRows-1 )
7791 {
7792 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
7793 MakeCellVisible( m_selectingKeyboard.GetRow(),
7794 m_selectingKeyboard.GetCol() );
c9097836 7795 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7796 }
d95b0c2b 7797 }
f6bcfd97 7798 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
aa5e1f75
SN
7799 {
7800 ClearSelection();
7801 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
7802 m_currentCellCoords.GetCol() );
d95b0c2b
SN
7803 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
7804 m_currentCellCoords.GetCol() );
aa5e1f75 7805 }
f6bcfd97
BP
7806 else
7807 return FALSE;
2d66e025 7808 return TRUE;
f85afd4e 7809 }
2d66e025
MB
7810
7811 return FALSE;
f85afd4e
MB
7812}
7813
2d66e025 7814
5c8fc7c1 7815bool wxGrid::MoveCursorLeft( bool expandSelection )
f85afd4e 7816{
2d66e025 7817 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7818 m_currentCellCoords.GetCol() >= 0 )
2d66e025 7819 {
5c8fc7c1 7820 if ( expandSelection )
d95b0c2b
SN
7821 {
7822 if ( m_selectingKeyboard == wxGridNoCellCoords )
7823 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7824 if ( m_selectingKeyboard.GetCol() > 0 )
7825 {
7826 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
7827 MakeCellVisible( m_selectingKeyboard.GetRow(),
7828 m_selectingKeyboard.GetCol() );
c9097836 7829 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7830 }
d95b0c2b 7831 }
f6bcfd97 7832 else if ( m_currentCellCoords.GetCol() > 0 )
aa5e1f75
SN
7833 {
7834 ClearSelection();
7835 MakeCellVisible( m_currentCellCoords.GetRow(),
7836 m_currentCellCoords.GetCol() - 1 );
d95b0c2b
SN
7837 SetCurrentCell( m_currentCellCoords.GetRow(),
7838 m_currentCellCoords.GetCol() - 1 );
aa5e1f75 7839 }
f6bcfd97
BP
7840 else
7841 return FALSE;
2d66e025
MB
7842 return TRUE;
7843 }
7844
7845 return FALSE;
7846}
7847
7848
5c8fc7c1 7849bool wxGrid::MoveCursorRight( bool expandSelection )
2d66e025
MB
7850{
7851 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7852 m_currentCellCoords.GetCol() < m_numCols )
f85afd4e 7853 {
5c8fc7c1 7854 if ( expandSelection )
d95b0c2b
SN
7855 {
7856 if ( m_selectingKeyboard == wxGridNoCellCoords )
7857 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7858 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
7859 {
7860 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
7861 MakeCellVisible( m_selectingKeyboard.GetRow(),
7862 m_selectingKeyboard.GetCol() );
c9097836 7863 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7864 }
d95b0c2b 7865 }
f6bcfd97 7866 else if ( m_currentCellCoords.GetCol() < m_numCols - 1 )
aa5e1f75
SN
7867 {
7868 ClearSelection();
7869 MakeCellVisible( m_currentCellCoords.GetRow(),
7870 m_currentCellCoords.GetCol() + 1 );
7871 SetCurrentCell( m_currentCellCoords.GetRow(),
d95b0c2b 7872 m_currentCellCoords.GetCol() + 1 );
aa5e1f75 7873 }
f6bcfd97
BP
7874 else
7875 return FALSE;
2d66e025 7876 return TRUE;
f85afd4e 7877 }
8f177c8e 7878
2d66e025
MB
7879 return FALSE;
7880}
7881
7882
7883bool wxGrid::MovePageUp()
7884{
7885 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 7886
2d66e025
MB
7887 int row = m_currentCellCoords.GetRow();
7888 if ( row > 0 )
f85afd4e 7889 {
2d66e025
MB
7890 int cw, ch;
7891 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 7892
7c1cb261 7893 int y = GetRowTop(row);
2d66e025
MB
7894 int newRow = YToRow( y - ch + 1 );
7895 if ( newRow == -1 )
7896 {
7897 newRow = 0;
7898 }
60ff3b99 7899 else if ( newRow == row )
2d66e025
MB
7900 {
7901 newRow = row - 1;
7902 }
8f177c8e 7903
2d66e025
MB
7904 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
7905 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 7906
f85afd4e
MB
7907 return TRUE;
7908 }
2d66e025
MB
7909
7910 return FALSE;
7911}
7912
7913bool wxGrid::MovePageDown()
7914{
7915 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 7916
2d66e025
MB
7917 int row = m_currentCellCoords.GetRow();
7918 if ( row < m_numRows )
f85afd4e 7919 {
2d66e025
MB
7920 int cw, ch;
7921 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 7922
7c1cb261 7923 int y = GetRowTop(row);
2d66e025
MB
7924 int newRow = YToRow( y + ch );
7925 if ( newRow == -1 )
7926 {
7927 newRow = m_numRows - 1;
7928 }
60ff3b99 7929 else if ( newRow == row )
2d66e025
MB
7930 {
7931 newRow = row + 1;
7932 }
7933
7934 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
7935 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 7936
2d66e025 7937 return TRUE;
f85afd4e 7938 }
2d66e025
MB
7939
7940 return FALSE;
f85afd4e 7941}
8f177c8e 7942
5c8fc7c1 7943bool wxGrid::MoveCursorUpBlock( bool expandSelection )
2d66e025
MB
7944{
7945 if ( m_table &&
7946 m_currentCellCoords != wxGridNoCellCoords &&
7947 m_currentCellCoords.GetRow() > 0 )
7948 {
7949 int row = m_currentCellCoords.GetRow();
7950 int col = m_currentCellCoords.GetCol();
7951
7952 if ( m_table->IsEmptyCell(row, col) )
7953 {
7954 // starting in an empty cell: find the next block of
7955 // non-empty cells
7956 //
7957 while ( row > 0 )
7958 {
7959 row-- ;
7960 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7961 }
7962 }
7963 else if ( m_table->IsEmptyCell(row-1, col) )
7964 {
7965 // starting at the top of a block: find the next block
7966 //
7967 row--;
7968 while ( row > 0 )
7969 {
7970 row-- ;
7971 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7972 }
7973 }
7974 else
7975 {
7976 // starting within a block: find the top of the block
7977 //
7978 while ( row > 0 )
7979 {
7980 row-- ;
7981 if ( m_table->IsEmptyCell(row, col) )
7982 {
7983 row++ ;
7984 break;
7985 }
7986 }
7987 }
f85afd4e 7988
2d66e025 7989 MakeCellVisible( row, col );
5c8fc7c1 7990 if ( expandSelection )
d95b0c2b
SN
7991 {
7992 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 7993 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
7994 }
7995 else
aa5e1f75
SN
7996 {
7997 ClearSelection();
7998 SetCurrentCell( row, col );
7999 }
2d66e025
MB
8000 return TRUE;
8001 }
f85afd4e 8002
2d66e025
MB
8003 return FALSE;
8004}
f85afd4e 8005
5c8fc7c1 8006bool wxGrid::MoveCursorDownBlock( bool expandSelection )
f85afd4e 8007{
2d66e025
MB
8008 if ( m_table &&
8009 m_currentCellCoords != wxGridNoCellCoords &&
8010 m_currentCellCoords.GetRow() < m_numRows-1 )
f85afd4e 8011 {
2d66e025
MB
8012 int row = m_currentCellCoords.GetRow();
8013 int col = m_currentCellCoords.GetCol();
8014
8015 if ( m_table->IsEmptyCell(row, col) )
8016 {
8017 // starting in an empty cell: find the next block of
8018 // non-empty cells
8019 //
8020 while ( row < m_numRows-1 )
8021 {
8022 row++ ;
8023 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8024 }
8025 }
8026 else if ( m_table->IsEmptyCell(row+1, col) )
8027 {
8028 // starting at the bottom of a block: find the next block
8029 //
8030 row++;
8031 while ( row < m_numRows-1 )
8032 {
8033 row++ ;
8034 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8035 }
8036 }
8037 else
8038 {
8039 // starting within a block: find the bottom of the block
8040 //
8041 while ( row < m_numRows-1 )
8042 {
8043 row++ ;
8044 if ( m_table->IsEmptyCell(row, col) )
8045 {
8046 row-- ;
8047 break;
8048 }
8049 }
8050 }
8051
8052 MakeCellVisible( row, col );
5c8fc7c1 8053 if ( expandSelection )
d95b0c2b
SN
8054 {
8055 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 8056 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
8057 }
8058 else
aa5e1f75
SN
8059 {
8060 ClearSelection();
8061 SetCurrentCell( row, col );
8062 }
2d66e025
MB
8063
8064 return TRUE;
f85afd4e 8065 }
f85afd4e 8066
2d66e025
MB
8067 return FALSE;
8068}
f85afd4e 8069
5c8fc7c1 8070bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
f85afd4e 8071{
2d66e025
MB
8072 if ( m_table &&
8073 m_currentCellCoords != wxGridNoCellCoords &&
8074 m_currentCellCoords.GetCol() > 0 )
f85afd4e 8075 {
2d66e025
MB
8076 int row = m_currentCellCoords.GetRow();
8077 int col = m_currentCellCoords.GetCol();
8078
8079 if ( m_table->IsEmptyCell(row, col) )
8080 {
8081 // starting in an empty cell: find the next block of
8082 // non-empty cells
8083 //
8084 while ( col > 0 )
8085 {
8086 col-- ;
8087 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8088 }
8089 }
8090 else if ( m_table->IsEmptyCell(row, col-1) )
8091 {
8092 // starting at the left of a block: find the next block
8093 //
8094 col--;
8095 while ( col > 0 )
8096 {
8097 col-- ;
8098 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8099 }
8100 }
8101 else
8102 {
8103 // starting within a block: find the left of the block
8104 //
8105 while ( col > 0 )
8106 {
8107 col-- ;
8108 if ( m_table->IsEmptyCell(row, col) )
8109 {
8110 col++ ;
8111 break;
8112 }
8113 }
8114 }
f85afd4e 8115
2d66e025 8116 MakeCellVisible( row, col );
5c8fc7c1 8117 if ( expandSelection )
d95b0c2b
SN
8118 {
8119 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 8120 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
8121 }
8122 else
aa5e1f75
SN
8123 {
8124 ClearSelection();
8125 SetCurrentCell( row, col );
8126 }
8f177c8e 8127
2d66e025 8128 return TRUE;
f85afd4e 8129 }
2d66e025
MB
8130
8131 return FALSE;
f85afd4e
MB
8132}
8133
5c8fc7c1 8134bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 8135{
2d66e025
MB
8136 if ( m_table &&
8137 m_currentCellCoords != wxGridNoCellCoords &&
8138 m_currentCellCoords.GetCol() < m_numCols-1 )
f85afd4e 8139 {
2d66e025
MB
8140 int row = m_currentCellCoords.GetRow();
8141 int col = m_currentCellCoords.GetCol();
8f177c8e 8142
2d66e025
MB
8143 if ( m_table->IsEmptyCell(row, col) )
8144 {
8145 // starting in an empty cell: find the next block of
8146 // non-empty cells
8147 //
8148 while ( col < m_numCols-1 )
8149 {
8150 col++ ;
8151 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8152 }
8153 }
8154 else if ( m_table->IsEmptyCell(row, col+1) )
8155 {
8156 // starting at the right of a block: find the next block
8157 //
8158 col++;
8159 while ( col < m_numCols-1 )
8160 {
8161 col++ ;
8162 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8163 }
8164 }
8165 else
8166 {
8167 // starting within a block: find the right of the block
8168 //
8169 while ( col < m_numCols-1 )
8170 {
8171 col++ ;
8172 if ( m_table->IsEmptyCell(row, col) )
8173 {
8174 col-- ;
8175 break;
8176 }
8177 }
8178 }
8f177c8e 8179
2d66e025 8180 MakeCellVisible( row, col );
5c8fc7c1 8181 if ( expandSelection )
d95b0c2b
SN
8182 {
8183 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 8184 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
8185 }
8186 else
aa5e1f75
SN
8187 {
8188 ClearSelection();
8189 SetCurrentCell( row, col );
8190 }
f85afd4e 8191
2d66e025 8192 return TRUE;
f85afd4e 8193 }
2d66e025
MB
8194
8195 return FALSE;
f85afd4e
MB
8196}
8197
8198
2d66e025 8199
f85afd4e 8200//
2d66e025 8201// ------ Label values and formatting
f85afd4e
MB
8202//
8203
8204void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
8205{
8206 *horiz = m_rowLabelHorizAlign;
8207 *vert = m_rowLabelVertAlign;
8208}
8209
8210void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
8211{
8212 *horiz = m_colLabelHorizAlign;
8213 *vert = m_colLabelVertAlign;
8214}
8215
8216wxString wxGrid::GetRowLabelValue( int row )
8217{
8218 if ( m_table )
8219 {
8220 return m_table->GetRowLabelValue( row );
8221 }
8222 else
8223 {
8224 wxString s;
8225 s << row;
8226 return s;
8227 }
8228}
8229
8230wxString wxGrid::GetColLabelValue( int col )
8231{
8232 if ( m_table )
8233 {
8234 return m_table->GetColLabelValue( col );
8235 }
8236 else
8237 {
8238 wxString s;
8239 s << col;
8240 return s;
8241 }
8242}
8243
2e8cd977 8244
f85afd4e
MB
8245void wxGrid::SetRowLabelSize( int width )
8246{
2e8cd977
MB
8247 width = wxMax( width, 0 );
8248 if ( width != m_rowLabelWidth )
8249 {
2e8cd977
MB
8250 if ( width == 0 )
8251 {
8252 m_rowLabelWin->Show( FALSE );
7807d81c 8253 m_cornerLabelWin->Show( FALSE );
2e8cd977 8254 }
7807d81c 8255 else if ( m_rowLabelWidth == 0 )
2e8cd977 8256 {
7807d81c
MB
8257 m_rowLabelWin->Show( TRUE );
8258 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 8259 }
b99be8fb 8260
2e8cd977 8261 m_rowLabelWidth = width;
7807d81c 8262 CalcWindowSizes();
27f35b66 8263 wxScrolledWindow::Refresh( TRUE );
2e8cd977 8264 }
f85afd4e
MB
8265}
8266
2e8cd977 8267
f85afd4e
MB
8268void wxGrid::SetColLabelSize( int height )
8269{
7807d81c 8270 height = wxMax( height, 0 );
2e8cd977
MB
8271 if ( height != m_colLabelHeight )
8272 {
2e8cd977
MB
8273 if ( height == 0 )
8274 {
2e8cd977 8275 m_colLabelWin->Show( FALSE );
7807d81c 8276 m_cornerLabelWin->Show( FALSE );
2e8cd977 8277 }
7807d81c 8278 else if ( m_colLabelHeight == 0 )
2e8cd977 8279 {
7807d81c
MB
8280 m_colLabelWin->Show( TRUE );
8281 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 8282 }
7807d81c 8283
2e8cd977 8284 m_colLabelHeight = height;
7807d81c 8285 CalcWindowSizes();
27f35b66 8286 wxScrolledWindow::Refresh( TRUE );
2e8cd977 8287 }
f85afd4e
MB
8288}
8289
2e8cd977 8290
f85afd4e
MB
8291void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
8292{
2d66e025
MB
8293 if ( m_labelBackgroundColour != colour )
8294 {
8295 m_labelBackgroundColour = colour;
8296 m_rowLabelWin->SetBackgroundColour( colour );
8297 m_colLabelWin->SetBackgroundColour( colour );
8298 m_cornerLabelWin->SetBackgroundColour( colour );
8299
8300 if ( !GetBatchCount() )
8301 {
8302 m_rowLabelWin->Refresh();
8303 m_colLabelWin->Refresh();
8304 m_cornerLabelWin->Refresh();
8305 }
8306 }
f85afd4e
MB
8307}
8308
8309void wxGrid::SetLabelTextColour( const wxColour& colour )
8310{
2d66e025
MB
8311 if ( m_labelTextColour != colour )
8312 {
8313 m_labelTextColour = colour;
8314 if ( !GetBatchCount() )
8315 {
8316 m_rowLabelWin->Refresh();
8317 m_colLabelWin->Refresh();
8318 }
8319 }
f85afd4e
MB
8320}
8321
8322void wxGrid::SetLabelFont( const wxFont& font )
8323{
8324 m_labelFont = font;
2d66e025
MB
8325 if ( !GetBatchCount() )
8326 {
8327 m_rowLabelWin->Refresh();
8328 m_colLabelWin->Refresh();
8329 }
f85afd4e
MB
8330}
8331
8332void wxGrid::SetRowLabelAlignment( int horiz, int vert )
8333{
4c7277db
MB
8334 // allow old (incorrect) defs to be used
8335 switch ( horiz )
8336 {
8337 case wxLEFT: horiz = wxALIGN_LEFT; break;
8338 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8339 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8340 }
84912ef8 8341
4c7277db
MB
8342 switch ( vert )
8343 {
8344 case wxTOP: vert = wxALIGN_TOP; break;
8345 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8346 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8347 }
84912ef8 8348
4c7277db 8349 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
8350 {
8351 m_rowLabelHorizAlign = horiz;
8352 }
8f177c8e 8353
4c7277db 8354 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
8355 {
8356 m_rowLabelVertAlign = vert;
8357 }
8358
2d66e025
MB
8359 if ( !GetBatchCount() )
8360 {
8361 m_rowLabelWin->Refresh();
60ff3b99 8362 }
f85afd4e
MB
8363}
8364
8365void wxGrid::SetColLabelAlignment( int horiz, int vert )
8366{
4c7277db
MB
8367 // allow old (incorrect) defs to be used
8368 switch ( horiz )
8369 {
8370 case wxLEFT: horiz = wxALIGN_LEFT; break;
8371 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8372 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8373 }
84912ef8 8374
4c7277db
MB
8375 switch ( vert )
8376 {
8377 case wxTOP: vert = wxALIGN_TOP; break;
8378 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8379 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8380 }
84912ef8 8381
4c7277db 8382 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
8383 {
8384 m_colLabelHorizAlign = horiz;
8385 }
8f177c8e 8386
4c7277db 8387 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
8388 {
8389 m_colLabelVertAlign = vert;
8390 }
8391
2d66e025
MB
8392 if ( !GetBatchCount() )
8393 {
2d66e025 8394 m_colLabelWin->Refresh();
60ff3b99 8395 }
f85afd4e
MB
8396}
8397
8398void wxGrid::SetRowLabelValue( int row, const wxString& s )
8399{
8400 if ( m_table )
8401 {
8402 m_table->SetRowLabelValue( row, s );
2d66e025
MB
8403 if ( !GetBatchCount() )
8404 {
70c7a608
SN
8405 wxRect rect = CellToRect( row, 0);
8406 if ( rect.height > 0 )
8407 {
cb309039 8408 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
b1944ebc 8409 rect.x = 0;
70c7a608
SN
8410 rect.width = m_rowLabelWidth;
8411 m_rowLabelWin->Refresh( TRUE, &rect );
8412 }
2d66e025 8413 }
f85afd4e
MB
8414 }
8415}
8416
8417void wxGrid::SetColLabelValue( int col, const wxString& s )
8418{
8419 if ( m_table )
8420 {
8421 m_table->SetColLabelValue( col, s );
2d66e025
MB
8422 if ( !GetBatchCount() )
8423 {
70c7a608
SN
8424 wxRect rect = CellToRect( 0, col );
8425 if ( rect.width > 0 )
8426 {
cb309039 8427 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
b1944ebc 8428 rect.y = 0;
70c7a608
SN
8429 rect.height = m_colLabelHeight;
8430 m_colLabelWin->Refresh( TRUE, &rect );
8431 }
2d66e025 8432 }
f85afd4e
MB
8433 }
8434}
8435
8436void wxGrid::SetGridLineColour( const wxColour& colour )
8437{
2d66e025
MB
8438 if ( m_gridLineColour != colour )
8439 {
8440 m_gridLineColour = colour;
60ff3b99 8441
2d66e025
MB
8442 wxClientDC dc( m_gridWin );
8443 PrepareDC( dc );
c6a51dcd 8444 DrawAllGridLines( dc, wxRegion() );
2d66e025 8445 }
f85afd4e
MB
8446}
8447
f6bcfd97
BP
8448
8449void wxGrid::SetCellHighlightColour( const wxColour& colour )
8450{
8451 if ( m_cellHighlightColour != colour )
8452 {
8453 m_cellHighlightColour = colour;
8454
8455 wxClientDC dc( m_gridWin );
8456 PrepareDC( dc );
8457 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
8458 DrawCellHighlight(dc, attr);
8459 attr->DecRef();
8460 }
8461}
8462
d2520c85
RD
8463void wxGrid::SetCellHighlightPenWidth(int width)
8464{
8465 if (m_cellHighlightPenWidth != width) {
8466 m_cellHighlightPenWidth = width;
8467
8468 // Just redrawing the cell highlight is not enough since that won't
8469 // make any visible change if the the thickness is getting smaller.
8470 int row = m_currentCellCoords.GetRow();
8471 int col = m_currentCellCoords.GetCol();
8472 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
8473 return;
8474 wxRect rect = CellToRect(row, col);
8475 m_gridWin->Refresh(TRUE, &rect);
8476 }
8477}
8478
8479void wxGrid::SetCellHighlightROPenWidth(int width)
8480{
8481 if (m_cellHighlightROPenWidth != width) {
8482 m_cellHighlightROPenWidth = width;
8483
8484 // Just redrawing the cell highlight is not enough since that won't
8485 // make any visible change if the the thickness is getting smaller.
8486 int row = m_currentCellCoords.GetRow();
8487 int col = m_currentCellCoords.GetCol();
8488 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
8489 return;
8490 wxRect rect = CellToRect(row, col);
8491 m_gridWin->Refresh(TRUE, &rect);
8492 }
8493}
8494
f85afd4e
MB
8495void wxGrid::EnableGridLines( bool enable )
8496{
8497 if ( enable != m_gridLinesEnabled )
8498 {
8499 m_gridLinesEnabled = enable;
2d66e025
MB
8500
8501 if ( !GetBatchCount() )
8502 {
8503 if ( enable )
8504 {
8505 wxClientDC dc( m_gridWin );
8506 PrepareDC( dc );
c6a51dcd 8507 DrawAllGridLines( dc, wxRegion() );
2d66e025
MB
8508 }
8509 else
8510 {
8511 m_gridWin->Refresh();
8512 }
8513 }
f85afd4e
MB
8514 }
8515}
8516
8517
8518int wxGrid::GetDefaultRowSize()
8519{
8520 return m_defaultRowHeight;
8521}
8522
8523int wxGrid::GetRowSize( int row )
8524{
b99be8fb
VZ
8525 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
8526
7c1cb261 8527 return GetRowHeight(row);
f85afd4e
MB
8528}
8529
8530int wxGrid::GetDefaultColSize()
8531{
8532 return m_defaultColWidth;
8533}
8534
8535int wxGrid::GetColSize( int col )
8536{
b99be8fb
VZ
8537 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
8538
7c1cb261 8539 return GetColWidth(col);
f85afd4e
MB
8540}
8541
2e9a6788
VZ
8542// ============================================================================
8543// access to the grid attributes: each of them has a default value in the grid
8544// itself and may be overidden on a per-cell basis
8545// ============================================================================
8546
8547// ----------------------------------------------------------------------------
8548// setting default attributes
8549// ----------------------------------------------------------------------------
8550
8551void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
8552{
2796cce3 8553 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
8554#ifdef __WXGTK__
8555 m_gridWin->SetBackgroundColour(col);
8556#endif
2e9a6788
VZ
8557}
8558
8559void wxGrid::SetDefaultCellTextColour( const wxColour& col )
8560{
2796cce3 8561 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
8562}
8563
8564void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
8565{
2796cce3 8566 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
8567}
8568
27f35b66
SN
8569void wxGrid::SetDefaultCellOverflow( bool allow )
8570{
8571 m_defaultCellAttr->SetOverflow(allow);
8572}
8573
2e9a6788
VZ
8574void wxGrid::SetDefaultCellFont( const wxFont& font )
8575{
2796cce3
RD
8576 m_defaultCellAttr->SetFont(font);
8577}
8578
0ba143c9
RD
8579void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
8580{
8581 m_defaultCellAttr->SetRenderer(renderer);
8582}
2e9a6788 8583
0ba143c9
RD
8584void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
8585{
8586 m_defaultCellAttr->SetEditor(editor);
8587}
9b4aede2 8588
2e9a6788
VZ
8589// ----------------------------------------------------------------------------
8590// access to the default attrbiutes
8591// ----------------------------------------------------------------------------
8592
f85afd4e
MB
8593wxColour wxGrid::GetDefaultCellBackgroundColour()
8594{
2796cce3 8595 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
8596}
8597
2e9a6788
VZ
8598wxColour wxGrid::GetDefaultCellTextColour()
8599{
2796cce3 8600 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
8601}
8602
8603wxFont wxGrid::GetDefaultCellFont()
8604{
2796cce3 8605 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
8606}
8607
8608void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
8609{
2796cce3 8610 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
8611}
8612
27f35b66
SN
8613bool wxGrid::GetDefaultCellOverflow()
8614{
8615 return m_defaultCellAttr->GetOverflow();
8616}
8617
0ba143c9
RD
8618wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
8619{
0b190b0f 8620 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 8621}
ab79958a 8622
0ba143c9
RD
8623wxGridCellEditor *wxGrid::GetDefaultEditor() const
8624{
28a77bc4 8625 return m_defaultCellAttr->GetEditor(NULL,0,0);
0ba143c9 8626}
9b4aede2 8627
2e9a6788
VZ
8628// ----------------------------------------------------------------------------
8629// access to cell attributes
8630// ----------------------------------------------------------------------------
8631
b99be8fb 8632wxColour wxGrid::GetCellBackgroundColour(int row, int col)
f85afd4e 8633{
0a976765 8634 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 8635 wxColour colour = attr->GetBackgroundColour();
39bcce60 8636 attr->DecRef();
b99be8fb 8637 return colour;
f85afd4e
MB
8638}
8639
b99be8fb 8640wxColour wxGrid::GetCellTextColour( int row, int col )
f85afd4e 8641{
0a976765 8642 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 8643 wxColour colour = attr->GetTextColour();
39bcce60 8644 attr->DecRef();
b99be8fb 8645 return colour;
f85afd4e
MB
8646}
8647
b99be8fb 8648wxFont wxGrid::GetCellFont( int row, int col )
f85afd4e 8649{
0a976765 8650 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 8651 wxFont font = attr->GetFont();
39bcce60 8652 attr->DecRef();
b99be8fb 8653 return font;
f85afd4e
MB
8654}
8655
b99be8fb 8656void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
f85afd4e 8657{
0a976765 8658 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 8659 attr->GetAlignment(horiz, vert);
39bcce60 8660 attr->DecRef();
2e9a6788
VZ
8661}
8662
27f35b66
SN
8663bool wxGrid::GetCellOverflow( int row, int col )
8664{
8665 wxGridCellAttr *attr = GetCellAttr(row, col);
8666 bool allow = attr->GetOverflow();
8667 attr->DecRef();
8668 return allow;
8669}
8670
8671void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols )
8672{
8673 wxGridCellAttr *attr = GetCellAttr(row, col);
8674 attr->GetSize( num_rows, num_cols );
8675 attr->DecRef();
8676}
8677
2796cce3
RD
8678wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
8679{
8680 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8681 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 8682 attr->DecRef();
0b190b0f 8683
2796cce3
RD
8684 return renderer;
8685}
8686
9b4aede2
RD
8687wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
8688{
8689 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8690 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 8691 attr->DecRef();
0b190b0f 8692
9b4aede2
RD
8693 return editor;
8694}
8695
283b7808
VZ
8696bool wxGrid::IsReadOnly(int row, int col) const
8697{
8698 wxGridCellAttr* attr = GetCellAttr(row, col);
8699 bool isReadOnly = attr->IsReadOnly();
8700 attr->DecRef();
8701 return isReadOnly;
8702}
8703
2e9a6788 8704// ----------------------------------------------------------------------------
758cbedf 8705// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
8706// ----------------------------------------------------------------------------
8707
8708bool wxGrid::CanHaveAttributes()
8709{
8710 if ( !m_table )
8711 {
8712 return FALSE;
8713 }
8714
f2d76237 8715 return m_table->CanHaveAttributes();
2e9a6788
VZ
8716}
8717
0a976765
VZ
8718void wxGrid::ClearAttrCache()
8719{
8720 if ( m_attrCache.row != -1 )
8721 {
39bcce60 8722 wxSafeDecRef(m_attrCache.attr);
19d7140e 8723 m_attrCache.attr = NULL;
0a976765
VZ
8724 m_attrCache.row = -1;
8725 }
8726}
8727
8728void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
8729{
2b5f62a0
VZ
8730 if ( attr != NULL )
8731 {
8732 wxGrid *self = (wxGrid *)this; // const_cast
0a976765 8733
2b5f62a0
VZ
8734 self->ClearAttrCache();
8735 self->m_attrCache.row = row;
8736 self->m_attrCache.col = col;
8737 self->m_attrCache.attr = attr;
8738 wxSafeIncRef(attr);
8739 }
0a976765
VZ
8740}
8741
8742bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
8743{
8744 if ( row == m_attrCache.row && col == m_attrCache.col )
8745 {
8746 *attr = m_attrCache.attr;
39bcce60 8747 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
8748
8749#ifdef DEBUG_ATTR_CACHE
8750 gs_nAttrCacheHits++;
8751#endif
8752
8753 return TRUE;
8754 }
8755 else
8756 {
8757#ifdef DEBUG_ATTR_CACHE
8758 gs_nAttrCacheMisses++;
8759#endif
8760 return FALSE;
8761 }
8762}
8763
2e9a6788
VZ
8764wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
8765{
a373d23b
SN
8766 wxGridCellAttr *attr = NULL;
8767 // Additional test to avoid looking at the cache e.g. for
8768 // wxNoCellCoords, as this will confuse memory management.
8769 if ( row >= 0 )
8770 {
3ed884a0
SN
8771 if ( !LookupAttr(row, col, &attr) )
8772 {
8773 attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any)
8774 : (wxGridCellAttr *)NULL;
8775 CacheAttr(row, col, attr);
8776 }
0a976765 8777 }
508011ce
VZ
8778 if (attr)
8779 {
2796cce3 8780 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
8781 }
8782 else
8783 {
2796cce3
RD
8784 attr = m_defaultCellAttr;
8785 attr->IncRef();
8786 }
2e9a6788 8787
0a976765
VZ
8788 return attr;
8789}
8790
8791wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
8792{
19d7140e 8793 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
0a976765 8794
1df4050d
VZ
8795 wxCHECK_MSG( m_table, attr,
8796 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8797
8798 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
8799 if ( !attr )
8800 {
8801 attr = new wxGridCellAttr(m_defaultCellAttr);
8802
8803 // artificially inc the ref count to match DecRef() in caller
8804 attr->IncRef();
8805 m_table->SetAttr(attr, row, col);
8806 }
0a976765 8807
2e9a6788
VZ
8808 return attr;
8809}
8810
0b190b0f
VZ
8811// ----------------------------------------------------------------------------
8812// setting column attributes (wrappers around SetColAttr)
8813// ----------------------------------------------------------------------------
8814
8815void wxGrid::SetColFormatBool(int col)
8816{
8817 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
8818}
8819
8820void wxGrid::SetColFormatNumber(int col)
8821{
8822 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
8823}
8824
8825void wxGrid::SetColFormatFloat(int col, int width, int precision)
8826{
8827 wxString typeName = wxGRID_VALUE_FLOAT;
8828 if ( (width != -1) || (precision != -1) )
8829 {
8830 typeName << _T(':') << width << _T(',') << precision;
8831 }
8832
8833 SetColFormatCustom(col, typeName);
8834}
8835
8836void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
8837{
19d7140e
VZ
8838 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
8839
8840 attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
8841 if(!attr)
8842 attr = new wxGridCellAttr;
0b190b0f
VZ
8843 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
8844 attr->SetRenderer(renderer);
8845
8846 SetColAttr(col, attr);
19d7140e 8847
0b190b0f
VZ
8848}
8849
758cbedf
VZ
8850// ----------------------------------------------------------------------------
8851// setting cell attributes: this is forwarded to the table
8852// ----------------------------------------------------------------------------
8853
27f35b66
SN
8854void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
8855{
8856 if ( CanHaveAttributes() )
8857 {
8858 m_table->SetAttr(attr, row, col);
8859 ClearAttrCache();
8860 }
8861 else
8862 {
8863 wxSafeDecRef(attr);
8864 }
8865}
8866
758cbedf
VZ
8867void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
8868{
8869 if ( CanHaveAttributes() )
8870 {
8871 m_table->SetRowAttr(attr, row);
19d7140e 8872 ClearAttrCache();
758cbedf
VZ
8873 }
8874 else
8875 {
39bcce60 8876 wxSafeDecRef(attr);
758cbedf
VZ
8877 }
8878}
8879
8880void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
8881{
8882 if ( CanHaveAttributes() )
8883 {
8884 m_table->SetColAttr(attr, col);
19d7140e 8885 ClearAttrCache();
758cbedf
VZ
8886 }
8887 else
8888 {
39bcce60 8889 wxSafeDecRef(attr);
758cbedf
VZ
8890 }
8891}
8892
2e9a6788
VZ
8893void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
8894{
8895 if ( CanHaveAttributes() )
8896 {
0a976765 8897 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8898 attr->SetBackgroundColour(colour);
8899 attr->DecRef();
8900 }
8901}
8902
8903void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
8904{
8905 if ( CanHaveAttributes() )
8906 {
0a976765 8907 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8908 attr->SetTextColour(colour);
8909 attr->DecRef();
8910 }
8911}
8912
8913void wxGrid::SetCellFont( int row, int col, const wxFont& font )
8914{
8915 if ( CanHaveAttributes() )
8916 {
0a976765 8917 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8918 attr->SetFont(font);
8919 attr->DecRef();
8920 }
8921}
8922
8923void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
8924{
8925 if ( CanHaveAttributes() )
8926 {
0a976765 8927 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8928 attr->SetAlignment(horiz, vert);
8929 attr->DecRef();
ab79958a
VZ
8930 }
8931}
8932
27f35b66
SN
8933void wxGrid::SetCellOverflow( int row, int col, bool allow )
8934{
8935 if ( CanHaveAttributes() )
8936 {
8937 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8938 attr->SetOverflow(allow);
8939 attr->DecRef();
8940 }
8941}
8942
8943void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
8944{
8945 if ( CanHaveAttributes() )
8946 {
8947 int cell_rows, cell_cols;
8948
8949 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8950 attr->GetSize(&cell_rows, &cell_cols);
8951 attr->SetSize(num_rows, num_cols);
8952 attr->DecRef();
8953
8954 // Cannot set the size of a cell to 0 or negative values
8955 // While it is perfectly legal to do that, this function cannot
8956 // handle all the possibilies, do it by hand by getting the CellAttr.
8957 // You can only set the size of a cell to 1,1 or greater with this fn
8958 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
8959 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
8960 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
8961 wxT("wxGrid::SetCellSize setting cell size to < 1"));
8962
8963 // if this was already a multicell then "turn off" the other cells first
8964 if ((cell_rows > 1) || (cell_rows > 1))
8965 {
8966 int i, j;
8967 for (j=row; j<row+cell_rows; j++)
8968 {
8969 for (i=col; i<col+cell_cols; i++)
8970 {
8971 if ((i != col) || (j != row))
8972 {
8973 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
8974 attr_stub->SetSize( 1, 1 );
8975 attr_stub->DecRef();
8976 }
8977 }
8978 }
8979 }
8980
8981 // mark the cells that will be covered by this cell to
8982 // negative or zero values to point back at this cell
8983 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
8984 {
8985 int i, j;
8986 for (j=row; j<row+num_rows; j++)
8987 {
8988 for (i=col; i<col+num_cols; i++)
8989 {
8990 if ((i != col) || (j != row))
8991 {
8992 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
8993 attr_stub->SetSize( row-j, col-i );
8994 attr_stub->DecRef();
8995 }
8996 }
8997 }
8998 }
8999 }
9000}
9001
ab79958a
VZ
9002void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
9003{
9004 if ( CanHaveAttributes() )
9005 {
0a976765 9006 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
9007 attr->SetRenderer(renderer);
9008 attr->DecRef();
9b4aede2
RD
9009 }
9010}
9011
9012void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
9013{
9014 if ( CanHaveAttributes() )
9015 {
9016 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9017 attr->SetEditor(editor);
9018 attr->DecRef();
283b7808
VZ
9019 }
9020}
9021
9022void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
9023{
9024 if ( CanHaveAttributes() )
9025 {
9026 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9027 attr->SetReadOnly(isReadOnly);
9028 attr->DecRef();
2e9a6788 9029 }
f85afd4e
MB
9030}
9031
f2d76237
RD
9032// ----------------------------------------------------------------------------
9033// Data type registration
9034// ----------------------------------------------------------------------------
9035
9036void wxGrid::RegisterDataType(const wxString& typeName,
9037 wxGridCellRenderer* renderer,
9038 wxGridCellEditor* editor)
9039{
9040 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
9041}
9042
9043
99306db2 9044wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
9045{
9046 wxString typeName = m_table->GetTypeName(row, col);
9047 return GetDefaultEditorForType(typeName);
9048}
9049
99306db2 9050wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
9051{
9052 wxString typeName = m_table->GetTypeName(row, col);
9053 return GetDefaultRendererForType(typeName);
9054}
9055
99306db2
VZ
9056wxGridCellEditor*
9057wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 9058{
c4608a8a 9059 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
9060 if ( index == wxNOT_FOUND )
9061 {
9062 wxFAIL_MSG(wxT("Unknown data type name"));
9063
f2d76237
RD
9064 return NULL;
9065 }
0b190b0f 9066
f2d76237
RD
9067 return m_typeRegistry->GetEditor(index);
9068}
9069
99306db2
VZ
9070wxGridCellRenderer*
9071wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 9072{
c4608a8a 9073 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
9074 if ( index == wxNOT_FOUND )
9075 {
c4608a8a 9076 wxFAIL_MSG(wxT("Unknown data type name"));
0b190b0f 9077
c4608a8a 9078 return NULL;
e72b4213 9079 }
0b190b0f 9080
c4608a8a 9081 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
9082}
9083
9084
2e9a6788
VZ
9085// ----------------------------------------------------------------------------
9086// row/col size
9087// ----------------------------------------------------------------------------
9088
6e8524b1
MB
9089void wxGrid::EnableDragRowSize( bool enable )
9090{
9091 m_canDragRowSize = enable;
9092}
9093
9094
9095void wxGrid::EnableDragColSize( bool enable )
9096{
9097 m_canDragColSize = enable;
9098}
9099
4cfa5de6
RD
9100void wxGrid::EnableDragGridSize( bool enable )
9101{
9102 m_canDragGridSize = enable;
9103}
9104
6e8524b1 9105
f85afd4e
MB
9106void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
9107{
9108 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
9109
9110 if ( resizeExistingRows )
9111 {
b1da8107
SN
9112 // since we are resizing all rows to the default row size,
9113 // we can simply clear the row heights and row bottoms
9114 // arrays (which also allows us to take advantage of
9115 // some speed optimisations)
9116 m_rowHeights.Empty();
9117 m_rowBottoms.Empty();
edb89f7e
VZ
9118 if ( !GetBatchCount() )
9119 CalcDimensions();
f85afd4e
MB
9120 }
9121}
9122
9123void wxGrid::SetRowSize( int row, int height )
9124{
b99be8fb 9125 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
60ff3b99 9126
7c1cb261
VZ
9127 if ( m_rowHeights.IsEmpty() )
9128 {
9129 // need to really create the array
9130 InitRowHeights();
9131 }
60ff3b99 9132
b99be8fb
VZ
9133 int h = wxMax( 0, height );
9134 int diff = h - m_rowHeights[row];
60ff3b99 9135
b99be8fb 9136 m_rowHeights[row] = h;
7c1cb261 9137 int i;
b99be8fb 9138 for ( i = row; i < m_numRows; i++ )
f85afd4e 9139 {
b99be8fb 9140 m_rowBottoms[i] += diff;
f85afd4e 9141 }
faec5a43
SN
9142 if ( !GetBatchCount() )
9143 CalcDimensions();
f85afd4e
MB
9144}
9145
9146void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
9147{
9148 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
9149
9150 if ( resizeExistingCols )
9151 {
b1da8107
SN
9152 // since we are resizing all columns to the default column size,
9153 // we can simply clear the col widths and col rights
9154 // arrays (which also allows us to take advantage of
9155 // some speed optimisations)
9156 m_colWidths.Empty();
9157 m_colRights.Empty();
edb89f7e
VZ
9158 if ( !GetBatchCount() )
9159 CalcDimensions();
f85afd4e
MB
9160 }
9161}
9162
9163void wxGrid::SetColSize( int col, int width )
9164{
b99be8fb 9165 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
60ff3b99 9166
43947979
VZ
9167 // should we check that it's bigger than GetColMinimalWidth(col) here?
9168
7c1cb261
VZ
9169 if ( m_colWidths.IsEmpty() )
9170 {
9171 // need to really create the array
9172 InitColWidths();
9173 }
f85afd4e 9174
73145b0e
JS
9175 // DJC MAPTEK if < 0 calc new width from label
9176 if( width < 0 )
9177 {
9178 long w, h;
9179 wxArrayString lines;
9180 wxClientDC dc(m_colLabelWin);
9181 dc.SetFont(GetLabelFont());
9182 StringToLines(GetColLabelValue(col), lines);
9183 GetTextBoxSize(dc, lines, &w, &h);
9184 width = w + 6;
9185 }
b99be8fb
VZ
9186 int w = wxMax( 0, width );
9187 int diff = w - m_colWidths[col];
9188 m_colWidths[col] = w;
60ff3b99 9189
7c1cb261 9190 int i;
b99be8fb 9191 for ( i = col; i < m_numCols; i++ )
f85afd4e 9192 {
b99be8fb 9193 m_colRights[i] += diff;
f85afd4e 9194 }
faec5a43
SN
9195 if ( !GetBatchCount() )
9196 CalcDimensions();
f85afd4e
MB
9197}
9198
2d66e025 9199
43947979
VZ
9200void wxGrid::SetColMinimalWidth( int col, int width )
9201{
af547d51
VZ
9202 m_colMinWidths.Put(col, width);
9203}
9204
9205void wxGrid::SetRowMinimalHeight( int row, int width )
9206{
9207 m_rowMinHeights.Put(row, width);
43947979
VZ
9208}
9209
9210int wxGrid::GetColMinimalWidth(int col) const
9211{
af547d51
VZ
9212 long value = m_colMinWidths.Get(col);
9213 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH;
9214}
9215
9216int wxGrid::GetRowMinimalHeight(int row) const
9217{
9218 long value = m_rowMinHeights.Get(row);
9219 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT;
43947979
VZ
9220}
9221
57c086ef
VZ
9222// ----------------------------------------------------------------------------
9223// auto sizing
9224// ----------------------------------------------------------------------------
9225
af547d51 9226void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
65e4e78e
VZ
9227{
9228 wxClientDC dc(m_gridWin);
9229
a95e38c0
VZ
9230 // init both of them to avoid compiler warnings, even if weo nly need one
9231 int row = -1,
9232 col = -1;
af547d51
VZ
9233 if ( column )
9234 col = colOrRow;
9235 else
9236 row = colOrRow;
9237
9238 wxCoord extent, extentMax = 0;
9239 int max = column ? m_numRows : m_numCols;
39bcce60 9240 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 9241 {
af547d51
VZ
9242 if ( column )
9243 row = rowOrCol;
9244 else
9245 col = rowOrCol;
9246
65e4e78e 9247 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 9248 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
9249 if ( renderer )
9250 {
af547d51
VZ
9251 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
9252 extent = column ? size.x : size.y;
9253 if ( extent > extentMax )
65e4e78e 9254 {
af547d51 9255 extentMax = extent;
65e4e78e 9256 }
0b190b0f
VZ
9257
9258 renderer->DecRef();
65e4e78e
VZ
9259 }
9260
9261 attr->DecRef();
9262 }
9263
af547d51
VZ
9264 // now also compare with the column label extent
9265 wxCoord w, h;
65e4e78e 9266 dc.SetFont( GetLabelFont() );
294d195c
MB
9267
9268 if ( column )
9269 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
9270 else
ee495e4c 9271 dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
294d195c 9272
af547d51
VZ
9273 extent = column ? w : h;
9274 if ( extent > extentMax )
65e4e78e 9275 {
af547d51 9276 extentMax = extent;
65e4e78e
VZ
9277 }
9278
af547d51 9279 if ( !extentMax )
65e4e78e 9280 {
af547d51
VZ
9281 // empty column - give default extent (notice that if extentMax is less
9282 // than default extent but != 0, it's ok)
9283 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
9284 }
9285 else
9286 {
a95e38c0
VZ
9287 if ( column )
9288 {
9289 // leave some space around text
9290 extentMax += 10;
9291 }
f6bcfd97
BP
9292 else
9293 {
9294 extentMax += 6;
9295 }
65e4e78e
VZ
9296 }
9297
edb89f7e
VZ
9298 if ( column )
9299 {
af547d51 9300 SetColSize(col, extentMax);
faec5a43
SN
9301 if ( !GetBatchCount() )
9302 {
edb89f7e
VZ
9303 int cw, ch, dummy;
9304 m_gridWin->GetClientSize( &cw, &ch );
9305 wxRect rect ( CellToRect( 0, col ) );
9306 rect.y = 0;
9307 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
9308 rect.width = cw - rect.x;
9309 rect.height = m_colLabelHeight;
9310 m_colLabelWin->Refresh( TRUE, &rect );
9311 }
9312 }
9313 else
9314 {
39bcce60 9315 SetRowSize(row, extentMax);
faec5a43
SN
9316 if ( !GetBatchCount() )
9317 {
edb89f7e
VZ
9318 int cw, ch, dummy;
9319 m_gridWin->GetClientSize( &cw, &ch );
9320 wxRect rect ( CellToRect( row, 0 ) );
9321 rect.x = 0;
9322 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
9323 rect.width = m_rowLabelWidth;
faec5a43 9324 rect.height = ch - rect.y;
edb89f7e
VZ
9325 m_rowLabelWin->Refresh( TRUE, &rect );
9326 }
faec5a43 9327 }
65e4e78e
VZ
9328 if ( setAsMin )
9329 {
af547d51
VZ
9330 if ( column )
9331 SetColMinimalWidth(col, extentMax);
9332 else
39bcce60 9333 SetRowMinimalHeight(row, extentMax);
65e4e78e
VZ
9334 }
9335}
9336
266e8367 9337int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 9338{
57c086ef
VZ
9339 int width = m_rowLabelWidth;
9340
faec5a43
SN
9341 if ( !calcOnly )
9342 BeginBatch();
97a9929e 9343
65e4e78e
VZ
9344 for ( int col = 0; col < m_numCols; col++ )
9345 {
266e8367
VZ
9346 if ( !calcOnly )
9347 {
9348 AutoSizeColumn(col, setAsMin);
9349 }
57c086ef
VZ
9350
9351 width += GetColWidth(col);
65e4e78e 9352 }
97a9929e 9353
faec5a43
SN
9354 if ( !calcOnly )
9355 EndBatch();
97a9929e 9356
266e8367 9357 return width;
65e4e78e
VZ
9358}
9359
266e8367 9360int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
9361{
9362 int height = m_colLabelHeight;
9363
faec5a43
SN
9364 if ( !calcOnly )
9365 BeginBatch();
97a9929e 9366
57c086ef
VZ
9367 for ( int row = 0; row < m_numRows; row++ )
9368 {
af547d51
VZ
9369 if ( !calcOnly )
9370 {
9371 AutoSizeRow(row, setAsMin);
9372 }
57c086ef
VZ
9373
9374 height += GetRowHeight(row);
9375 }
97a9929e 9376
faec5a43
SN
9377 if ( !calcOnly )
9378 EndBatch();
97a9929e 9379
266e8367 9380 return height;
57c086ef
VZ
9381}
9382
9383void wxGrid::AutoSize()
9384{
97a9929e
VZ
9385 BeginBatch();
9386
9387 wxSize size(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE));
9388
9389 // round up the size to a multiple of scroll step - this ensures that we
9390 // won't get the scrollbars if we're sized exactly to this width
2b5f62a0
VZ
9391 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9392 // scrollbar steps
9393 wxSize sizeFit(GetScrollX(size.x + m_extraWidth + 1) * GRID_SCROLL_LINE_X,
9394 GetScrollY(size.y + m_extraHeight + 1) * GRID_SCROLL_LINE_Y);
97a9929e 9395
2b5f62a0 9396 // distribute the extra space between the columns/rows to avoid having
97a9929e 9397 // extra white space
2b5f62a0
VZ
9398
9399 // Remove the extra m_extraWidth + 1 added above
9400 wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1);
9401 if ( diff && m_numCols )
97a9929e
VZ
9402 {
9403 // try to resize the columns uniformly
9404 wxCoord diffPerCol = diff / m_numCols;
9405 if ( diffPerCol )
9406 {
9407 for ( int col = 0; col < m_numCols; col++ )
9408 {
9409 SetColSize(col, GetColWidth(col) + diffPerCol);
9410 }
9411 }
9412
9413 // add remaining amount to the last columns
9414 diff -= diffPerCol * m_numCols;
9415 if ( diff )
9416 {
9417 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
9418 {
9419 SetColSize(col, GetColWidth(col) + 1);
9420 }
9421 }
9422 }
9423
9424 // same for rows
2b5f62a0
VZ
9425 diff = sizeFit.y - size.y - (m_extraHeight + 1);
9426 if ( diff && m_numRows )
97a9929e
VZ
9427 {
9428 // try to resize the columns uniformly
9429 wxCoord diffPerRow = diff / m_numRows;
9430 if ( diffPerRow )
9431 {
9432 for ( int row = 0; row < m_numRows; row++ )
9433 {
9434 SetRowSize(row, GetRowHeight(row) + diffPerRow);
9435 }
9436 }
9437
9438 // add remaining amount to the last rows
9439 diff -= diffPerRow * m_numRows;
9440 if ( diff )
9441 {
9442 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
9443 {
9444 SetRowSize(row, GetRowHeight(row) + 1);
9445 }
9446 }
9447 }
9448
9449 EndBatch();
9450
9451 SetClientSize(sizeFit);
266e8367
VZ
9452}
9453
9454wxSize wxGrid::DoGetBestSize() const
9455{
9456 // don't set sizes, only calculate them
9457 wxGrid *self = (wxGrid *)this; // const_cast
9458
84035150
SN
9459 int width, height;
9460 width = self->SetOrCalcColumnSizes(TRUE);
9461 height = self->SetOrCalcRowSizes(TRUE);
9462
9463 int maxwidth, maxheight;
9464 wxDisplaySize( & maxwidth, & maxheight );
9465
9466 if ( width > maxwidth ) width = maxwidth;
9467 if ( height > maxheight ) height = maxheight;
9468
9469 return wxSize( width, height );
266e8367
VZ
9470}
9471
9472void wxGrid::Fit()
9473{
9474 AutoSize();
57c086ef
VZ
9475}
9476
6fc0f38f
SN
9477
9478wxPen& wxGrid::GetDividerPen() const
9479{
9480 return wxNullPen;
9481}
9482
57c086ef
VZ
9483// ----------------------------------------------------------------------------
9484// cell value accessor functions
9485// ----------------------------------------------------------------------------
f85afd4e
MB
9486
9487void wxGrid::SetCellValue( int row, int col, const wxString& s )
9488{
9489 if ( m_table )
9490 {
f6bcfd97 9491 m_table->SetValue( row, col, s );
2d66e025
MB
9492 if ( !GetBatchCount() )
9493 {
27f35b66
SN
9494 int dummy;
9495 wxRect rect( CellToRect( row, col ) );
9496 rect.x = 0;
9497 rect.width = m_gridWin->GetClientSize().GetWidth();
9498 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
9499 m_gridWin->Refresh( FALSE, &rect );
2d66e025 9500 }
60ff3b99 9501
f85afd4e 9502 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6 9503 m_currentCellCoords.GetCol() == col &&
f6bcfd97
BP
9504 IsCellEditControlShown())
9505 // Note: If we are using IsCellEditControlEnabled,
9506 // this interacts badly with calling SetCellValue from
9507 // an EVT_GRID_CELL_CHANGE handler.
f85afd4e 9508 {
4cfa5de6
RD
9509 HideCellEditControl();
9510 ShowCellEditControl(); // will reread data from table
f85afd4e 9511 }
f85afd4e
MB
9512 }
9513}
9514
9515
9516//
2d66e025 9517// ------ Block, row and col selection
f85afd4e
MB
9518//
9519
9520void wxGrid::SelectRow( int row, bool addToSelected )
9521{
b5808881 9522 if ( IsSelection() && !addToSelected )
e32352cf 9523 ClearSelection();
70c7a608 9524
3f3dc2ef
VZ
9525 if ( m_selection )
9526 m_selection->SelectRow( row, FALSE, addToSelected );
f85afd4e
MB
9527}
9528
9529
9530void wxGrid::SelectCol( int col, bool addToSelected )
9531{
b5808881 9532 if ( IsSelection() && !addToSelected )
e32352cf 9533 ClearSelection();
f85afd4e 9534
3f3dc2ef
VZ
9535 if ( m_selection )
9536 m_selection->SelectCol( col, FALSE, addToSelected );
f85afd4e
MB
9537}
9538
9539
84912ef8 9540void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
c9097836 9541 bool addToSelected )
f85afd4e 9542{
c9097836
MB
9543 if ( IsSelection() && !addToSelected )
9544 ClearSelection();
f85afd4e 9545
3f3dc2ef
VZ
9546 if ( m_selection )
9547 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
9548 FALSE, addToSelected );
f85afd4e
MB
9549}
9550
c9097836 9551
f85afd4e
MB
9552void wxGrid::SelectAll()
9553{
f74d0b57 9554 if ( m_numRows > 0 && m_numCols > 0 )
3f3dc2ef
VZ
9555 {
9556 if ( m_selection )
9557 m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
9558 }
b5808881 9559}
f85afd4e 9560
f7b4b343
VZ
9561//
9562// ------ Cell, row and col deselection
9563//
9564
9565void wxGrid::DeselectRow( int row )
9566{
3f3dc2ef
VZ
9567 if ( !m_selection )
9568 return;
9569
f7b4b343
VZ
9570 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
9571 {
9572 if ( m_selection->IsInSelection(row, 0 ) )
9573 m_selection->ToggleCellSelection( row, 0);
ffdd3c98 9574 }
f7b4b343
VZ
9575 else
9576 {
9577 int nCols = GetNumberCols();
9578 for ( int i = 0; i < nCols ; i++ )
9579 {
9580 if ( m_selection->IsInSelection(row, i ) )
9581 m_selection->ToggleCellSelection( row, i);
9582 }
9583 }
9584}
9585
9586void wxGrid::DeselectCol( int col )
9587{
3f3dc2ef
VZ
9588 if ( !m_selection )
9589 return;
9590
f7b4b343
VZ
9591 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
9592 {
9593 if ( m_selection->IsInSelection(0, col ) )
9594 m_selection->ToggleCellSelection( 0, col);
9595 }
9596 else
9597 {
9598 int nRows = GetNumberRows();
9599 for ( int i = 0; i < nRows ; i++ )
9600 {
9601 if ( m_selection->IsInSelection(i, col ) )
9602 m_selection->ToggleCellSelection(i, col);
9603 }
9604 }
9605}
9606
9607void wxGrid::DeselectCell( int row, int col )
9608{
3f3dc2ef 9609 if ( m_selection && m_selection->IsInSelection(row, col) )
f7b4b343
VZ
9610 m_selection->ToggleCellSelection(row, col);
9611}
9612
b5808881
SN
9613bool wxGrid::IsSelection()
9614{
3f3dc2ef 9615 return ( m_selection && (m_selection->IsSelection() ||
b5808881 9616 ( m_selectingTopLeft != wxGridNoCellCoords &&
3f3dc2ef 9617 m_selectingBottomRight != wxGridNoCellCoords) ) );
f85afd4e
MB
9618}
9619
84035150 9620bool wxGrid::IsInSelection( int row, int col ) const
b5808881 9621{
3f3dc2ef 9622 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
b5808881
SN
9623 ( row >= m_selectingTopLeft.GetRow() &&
9624 col >= m_selectingTopLeft.GetCol() &&
9625 row <= m_selectingBottomRight.GetRow() &&
3f3dc2ef 9626 col <= m_selectingBottomRight.GetCol() )) );
b5808881 9627}
f85afd4e 9628
aa5b8857
SN
9629wxGridCellCoordsArray wxGrid::GetSelectedCells() const
9630{
9631 if (!m_selection) { wxGridCellCoordsArray a; return a; }
9632 return m_selection->m_cellSelection;
9633}
9634wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
9635{
9636 if (!m_selection) { wxGridCellCoordsArray a; return a; }
9637 return m_selection->m_blockSelectionTopLeft;
9638}
9639wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
9640{
9641 if (!m_selection) { wxGridCellCoordsArray a; return a; }
a8de8190 9642 return m_selection->m_blockSelectionBottomRight;
aa5b8857
SN
9643}
9644wxArrayInt wxGrid::GetSelectedRows() const
9645{
9646 if (!m_selection) { wxArrayInt a; return a; }
9647 return m_selection->m_rowSelection;
9648}
9649wxArrayInt wxGrid::GetSelectedCols() const
9650{
9651 if (!m_selection) { wxArrayInt a; return a; }
9652 return m_selection->m_colSelection;
9653}
9654
9655
f85afd4e
MB
9656void wxGrid::ClearSelection()
9657{
b5808881
SN
9658 m_selectingTopLeft = wxGridNoCellCoords;
9659 m_selectingBottomRight = wxGridNoCellCoords;
3f3dc2ef
VZ
9660 if ( m_selection )
9661 m_selection->ClearSelection();
8f177c8e 9662}
f85afd4e
MB
9663
9664
da6af900 9665// This function returns the rectangle that encloses the given block
2d66e025
MB
9666// in device coords clipped to the client size of the grid window.
9667//
58dd5b3b
MB
9668wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
9669 const wxGridCellCoords &bottomRight )
f85afd4e 9670{
58dd5b3b 9671 wxRect rect( wxGridNoCellRect );
f85afd4e
MB
9672 wxRect cellRect;
9673
58dd5b3b
MB
9674 cellRect = CellToRect( topLeft );
9675 if ( cellRect != wxGridNoCellRect )
f85afd4e 9676 {
58dd5b3b
MB
9677 rect = cellRect;
9678 }
9679 else
9680 {
9681 rect = wxRect( 0, 0, 0, 0 );
9682 }
60ff3b99 9683
58dd5b3b
MB
9684 cellRect = CellToRect( bottomRight );
9685 if ( cellRect != wxGridNoCellRect )
9686 {
9687 rect += cellRect;
2d66e025
MB
9688 }
9689 else
9690 {
9691 return wxGridNoCellRect;
f85afd4e
MB
9692 }
9693
27f35b66
SN
9694 int i, j;
9695 int left = rect.GetLeft();
9696 int top = rect.GetTop();
9697 int right = rect.GetRight();
9698 int bottom = rect.GetBottom();
9699
9700 int leftCol = topLeft.GetCol();
9701 int topRow = topLeft.GetRow();
9702 int rightCol = bottomRight.GetCol();
9703 int bottomRow = bottomRight.GetRow();
9704
3ed884a0
SN
9705 if (left > right)
9706 {
9707 i = left;
9708 left = right;
9709 right = i;
9710 i = leftCol;
9711 leftCol=rightCol;
9712 rightCol = i;
9713 }
9714
9715 if (top > bottom)
9716 {
9717 i = top;
9718 top = bottom;
9719 bottom = i;
9720 i = topRow;
9721 topRow = bottomRow;
9722 bottomRow = i;
9723 }
9724
9725
27f35b66
SN
9726 for ( j = topRow; j <= bottomRow; j++ )
9727 {
9728 for ( i = leftCol; i <= rightCol; i++ )
9729 {
9730 if ((j==topRow) || (j==bottomRow) || (i==leftCol) || (i==rightCol))
9731 {
9732 cellRect = CellToRect( j, i );
9733
9734 if (cellRect.x < left)
9735 left = cellRect.x;
9736 if (cellRect.y < top)
9737 top = cellRect.y;
9738 if (cellRect.x + cellRect.width > right)
9739 right = cellRect.x + cellRect.width;
9740 if (cellRect.y + cellRect.height > bottom)
9741 bottom = cellRect.y + cellRect.height;
9742 }
3ed884a0 9743 else i = rightCol; // jump over inner cells.
27f35b66
SN
9744 }
9745 }
9746
58dd5b3b
MB
9747 // convert to scrolled coords
9748 //
27f35b66
SN
9749 CalcScrolledPosition( left, top, &left, &top );
9750 CalcScrolledPosition( right, bottom, &right, &bottom );
58dd5b3b
MB
9751
9752 int cw, ch;
9753 m_gridWin->GetClientSize( &cw, &ch );
9754
f6bcfd97
BP
9755 if (right < 0 || bottom < 0 || left > cw || top > ch)
9756 return wxRect( 0, 0, 0, 0);
9757
58dd5b3b
MB
9758 rect.SetLeft( wxMax(0, left) );
9759 rect.SetTop( wxMax(0, top) );
9760 rect.SetRight( wxMin(cw, right) );
9761 rect.SetBottom( wxMin(ch, bottom) );
9762
f85afd4e
MB
9763 return rect;
9764}
9765
9766
58dd5b3b 9767
f85afd4e
MB
9768//
9769// ------ Grid event classes
9770//
9771
bf7945ce 9772IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
f85afd4e
MB
9773
9774wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 9775 int row, int col, int x, int y, bool sel,
f85afd4e
MB
9776 bool control, bool shift, bool alt, bool meta )
9777 : wxNotifyEvent( type, id )
9778{
9779 m_row = row;
9780 m_col = col;
9781 m_x = x;
9782 m_y = y;
5c8fc7c1 9783 m_selecting = sel;
f85afd4e
MB
9784 m_control = control;
9785 m_shift = shift;
9786 m_alt = alt;
9787 m_meta = meta;
8f177c8e 9788
f85afd4e
MB
9789 SetEventObject(obj);
9790}
9791
9792
bf7945ce 9793IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
f85afd4e
MB
9794
9795wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
9796 int rowOrCol, int x, int y,
9797 bool control, bool shift, bool alt, bool meta )
9798 : wxNotifyEvent( type, id )
9799{
9800 m_rowOrCol = rowOrCol;
9801 m_x = x;
9802 m_y = y;
9803 m_control = control;
9804 m_shift = shift;
9805 m_alt = alt;
9806 m_meta = meta;
8f177c8e 9807
f85afd4e
MB
9808 SetEventObject(obj);
9809}
9810
9811
bf7945ce 9812IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
f85afd4e
MB
9813
9814wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
9815 const wxGridCellCoords& topLeft,
9816 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
9817 bool sel, bool control,
9818 bool shift, bool alt, bool meta )
8f177c8e 9819 : wxNotifyEvent( type, id )
f85afd4e
MB
9820{
9821 m_topLeft = topLeft;
9822 m_bottomRight = bottomRight;
5c8fc7c1 9823 m_selecting = sel;
f85afd4e
MB
9824 m_control = control;
9825 m_shift = shift;
9826 m_alt = alt;
9827 m_meta = meta;
9828
9829 SetEventObject(obj);
9830}
9831
9832
bf7945ce
RD
9833IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
9834
9835wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
9836 wxObject* obj, int row,
9837 int col, wxControl* ctrl)
9838 : wxCommandEvent(type, id)
9839{
9840 SetEventObject(obj);
9841 m_row = row;
9842 m_col = col;
9843 m_ctrl = ctrl;
9844}
9845
9846
27b92ca4
VZ
9847#endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9848
9849#endif // wxUSE_GRID