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