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