]>
Commit | Line | Data |
---|---|---|
2796cce3 | 1 | /////////////////////////////////////////////////////////////////////////// |
d16c04bb | 2 | // Name: generic/grid.cpp |
f85afd4e MB |
3 | // Purpose: wxGrid and related classes |
4 | // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn) | |
5 | // Modified by: | |
6 | // Created: 1/08/1999 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
758cbedf VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
f85afd4e MB |
20 | #ifdef __GNUG__ |
21 | #pragma implementation "grid.h" | |
22 | #endif | |
23 | ||
4d85bcd1 JS |
24 | // For compilers that support precompilation, includes "wx/wx.h". |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #include "wx/defs.h" | |
f85afd4e MB |
28 | |
29 | #ifdef __BORLANDC__ | |
30 | #pragma hdrstop | |
31 | #endif | |
32 | ||
8f177c8e | 33 | #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID) |
4d85bcd1 JS |
34 | #include "gridg.cpp" |
35 | #else | |
36 | ||
f85afd4e MB |
37 | #ifndef WX_PRECOMP |
38 | #include "wx/utils.h" | |
39 | #include "wx/dcclient.h" | |
40 | #include "wx/settings.h" | |
41 | #include "wx/log.h" | |
508011ce VZ |
42 | #include "wx/textctrl.h" |
43 | #include "wx/checkbox.h" | |
4ee5fc9c | 44 | #include "wx/combobox.h" |
816be743 | 45 | #include "wx/valtext.h" |
f85afd4e MB |
46 | #endif |
47 | ||
cb5df486 | 48 | #include "wx/textfile.h" |
816be743 | 49 | #include "wx/spinctrl.h" |
c4608a8a | 50 | #include "wx/tokenzr.h" |
6d004f67 | 51 | |
07296f0b | 52 | #include "wx/grid.h" |
b5808881 | 53 | #include "wx/generic/gridsel.h" |
07296f0b | 54 | |
b99be8fb | 55 | // ---------------------------------------------------------------------------- |
758cbedf | 56 | // array classes |
b99be8fb VZ |
57 | // ---------------------------------------------------------------------------- |
58 | ||
758cbedf VZ |
59 | WX_DEFINE_ARRAY(wxGridCellAttr *, wxArrayAttrs); |
60 | ||
b99be8fb VZ |
61 | struct wxGridCellWithAttr |
62 | { | |
2e9a6788 VZ |
63 | wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_) |
64 | : coords(row, col), attr(attr_) | |
b99be8fb VZ |
65 | { |
66 | } | |
67 | ||
2e9a6788 VZ |
68 | ~wxGridCellWithAttr() |
69 | { | |
70 | attr->DecRef(); | |
71 | } | |
72 | ||
b99be8fb | 73 | wxGridCellCoords coords; |
2e9a6788 | 74 | wxGridCellAttr *attr; |
b99be8fb VZ |
75 | }; |
76 | ||
77 | WX_DECLARE_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray); | |
78 | ||
79 | #include "wx/arrimpl.cpp" | |
80 | ||
81 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) | |
82 | WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray) | |
83 | ||
84 | // ---------------------------------------------------------------------------- | |
85 | // private classes | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
88 | class WXDLLEXPORT wxGridRowLabelWindow : public wxWindow | |
89 | { | |
90 | public: | |
91 | wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; } | |
92 | wxGridRowLabelWindow( wxGrid *parent, wxWindowID id, | |
93 | const wxPoint &pos, const wxSize &size ); | |
94 | ||
95 | private: | |
96 | wxGrid *m_owner; | |
97 | ||
98 | void OnPaint( wxPaintEvent& event ); | |
99 | void OnMouseEvent( wxMouseEvent& event ); | |
100 | void OnKeyDown( wxKeyEvent& event ); | |
101 | ||
102 | DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow) | |
103 | DECLARE_EVENT_TABLE() | |
104 | }; | |
105 | ||
106 | ||
107 | class WXDLLEXPORT wxGridColLabelWindow : public wxWindow | |
108 | { | |
109 | public: | |
110 | wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; } | |
111 | wxGridColLabelWindow( wxGrid *parent, wxWindowID id, | |
112 | const wxPoint &pos, const wxSize &size ); | |
113 | ||
114 | private: | |
115 | wxGrid *m_owner; | |
116 | ||
117 | void OnPaint( wxPaintEvent &event ); | |
118 | void OnMouseEvent( wxMouseEvent& event ); | |
119 | void OnKeyDown( wxKeyEvent& event ); | |
120 | ||
121 | DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow) | |
122 | DECLARE_EVENT_TABLE() | |
123 | }; | |
124 | ||
125 | ||
126 | class WXDLLEXPORT wxGridCornerLabelWindow : public wxWindow | |
127 | { | |
128 | public: | |
129 | wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; } | |
130 | wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id, | |
131 | const wxPoint &pos, const wxSize &size ); | |
132 | ||
133 | private: | |
134 | wxGrid *m_owner; | |
135 | ||
136 | void OnMouseEvent( wxMouseEvent& event ); | |
137 | void OnKeyDown( wxKeyEvent& event ); | |
138 | void OnPaint( wxPaintEvent& event ); | |
139 | ||
140 | DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow) | |
141 | DECLARE_EVENT_TABLE() | |
142 | }; | |
143 | ||
144 | class WXDLLEXPORT wxGridWindow : public wxPanel | |
145 | { | |
146 | public: | |
147 | wxGridWindow() | |
148 | { | |
149 | m_owner = (wxGrid *)NULL; | |
150 | m_rowLabelWin = (wxGridRowLabelWindow *)NULL; | |
151 | m_colLabelWin = (wxGridColLabelWindow *)NULL; | |
152 | } | |
153 | ||
154 | wxGridWindow( wxGrid *parent, | |
155 | wxGridRowLabelWindow *rowLblWin, | |
156 | wxGridColLabelWindow *colLblWin, | |
157 | wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
158 | ~wxGridWindow(); | |
159 | ||
160 | void ScrollWindow( int dx, int dy, const wxRect *rect ); | |
161 | ||
162 | private: | |
163 | wxGrid *m_owner; | |
164 | wxGridRowLabelWindow *m_rowLabelWin; | |
165 | wxGridColLabelWindow *m_colLabelWin; | |
166 | ||
167 | void OnPaint( wxPaintEvent &event ); | |
168 | void OnMouseEvent( wxMouseEvent& event ); | |
169 | void OnKeyDown( wxKeyEvent& ); | |
2796cce3 RD |
170 | void OnEraseBackground( wxEraseEvent& ); |
171 | ||
b99be8fb VZ |
172 | |
173 | DECLARE_DYNAMIC_CLASS(wxGridWindow) | |
174 | DECLARE_EVENT_TABLE() | |
175 | }; | |
176 | ||
2796cce3 RD |
177 | |
178 | ||
179 | class wxGridCellEditorEvtHandler : public wxEvtHandler | |
180 | { | |
181 | public: | |
182 | wxGridCellEditorEvtHandler() | |
183 | : m_grid(0), m_editor(0) | |
184 | { } | |
185 | wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor) | |
186 | : m_grid(grid), m_editor(editor) | |
187 | { } | |
188 | ||
189 | void OnKeyDown(wxKeyEvent& event); | |
fb0de762 | 190 | void OnChar(wxKeyEvent& event); |
2796cce3 RD |
191 | |
192 | private: | |
193 | wxGrid* m_grid; | |
194 | wxGridCellEditor* m_editor; | |
195 | DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler) | |
196 | DECLARE_EVENT_TABLE() | |
197 | }; | |
198 | ||
199 | ||
200 | IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler ) | |
201 | BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler ) | |
202 | EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown ) | |
fb0de762 | 203 | EVT_CHAR( wxGridCellEditorEvtHandler::OnChar ) |
2796cce3 RD |
204 | END_EVENT_TABLE() |
205 | ||
206 | ||
207 | ||
758cbedf | 208 | // ---------------------------------------------------------------------------- |
b99be8fb | 209 | // the internal data representation used by wxGridCellAttrProvider |
758cbedf VZ |
210 | // ---------------------------------------------------------------------------- |
211 | ||
212 | // this class stores attributes set for cells | |
213 | class WXDLLEXPORT wxGridCellAttrData | |
b99be8fb VZ |
214 | { |
215 | public: | |
2e9a6788 | 216 | void SetAttr(wxGridCellAttr *attr, int row, int col); |
b99be8fb | 217 | wxGridCellAttr *GetAttr(int row, int col) const; |
4d60017a SN |
218 | void UpdateAttrRows( size_t pos, int numRows ); |
219 | void UpdateAttrCols( size_t pos, int numCols ); | |
b99be8fb VZ |
220 | |
221 | private: | |
222 | // searches for the attr for given cell, returns wxNOT_FOUND if not found | |
223 | int FindIndex(int row, int col) const; | |
224 | ||
225 | wxGridCellWithAttrArray m_attrs; | |
226 | }; | |
227 | ||
758cbedf VZ |
228 | // this class stores attributes set for rows or columns |
229 | class WXDLLEXPORT wxGridRowOrColAttrData | |
230 | { | |
231 | public: | |
ee6694a7 VZ |
232 | // empty ctor to suppress warnings |
233 | wxGridRowOrColAttrData() { } | |
758cbedf VZ |
234 | ~wxGridRowOrColAttrData(); |
235 | ||
236 | void SetAttr(wxGridCellAttr *attr, int rowOrCol); | |
237 | wxGridCellAttr *GetAttr(int rowOrCol) const; | |
4d60017a | 238 | void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ); |
758cbedf VZ |
239 | |
240 | private: | |
241 | wxArrayInt m_rowsOrCols; | |
242 | wxArrayAttrs m_attrs; | |
243 | }; | |
244 | ||
245 | // NB: this is just a wrapper around 3 objects: one which stores cell | |
246 | // attributes, and 2 others for row/col ones | |
247 | class WXDLLEXPORT wxGridCellAttrProviderData | |
248 | { | |
249 | public: | |
250 | wxGridCellAttrData m_cellAttrs; | |
251 | wxGridRowOrColAttrData m_rowAttrs, | |
252 | m_colAttrs; | |
253 | }; | |
254 | ||
f2d76237 RD |
255 | |
256 | // ---------------------------------------------------------------------------- | |
257 | // data structures used for the data type registry | |
258 | // ---------------------------------------------------------------------------- | |
259 | ||
b94ae1ea VZ |
260 | struct wxGridDataTypeInfo |
261 | { | |
f2d76237 RD |
262 | wxGridDataTypeInfo(const wxString& typeName, |
263 | wxGridCellRenderer* renderer, | |
264 | wxGridCellEditor* editor) | |
265 | : m_typeName(typeName), m_renderer(renderer), m_editor(editor) | |
266 | { } | |
267 | ||
39bcce60 VZ |
268 | ~wxGridDataTypeInfo() |
269 | { | |
270 | wxSafeDecRef(m_renderer); | |
271 | wxSafeDecRef(m_editor); | |
272 | } | |
f2d76237 RD |
273 | |
274 | wxString m_typeName; | |
275 | wxGridCellRenderer* m_renderer; | |
276 | wxGridCellEditor* m_editor; | |
277 | }; | |
278 | ||
279 | ||
280 | WX_DEFINE_ARRAY(wxGridDataTypeInfo*, wxGridDataTypeInfoArray); | |
281 | ||
282 | ||
b94ae1ea VZ |
283 | class WXDLLEXPORT wxGridTypeRegistry |
284 | { | |
f2d76237 RD |
285 | public: |
286 | ~wxGridTypeRegistry(); | |
b94ae1ea | 287 | |
f2d76237 RD |
288 | void RegisterDataType(const wxString& typeName, |
289 | wxGridCellRenderer* renderer, | |
290 | wxGridCellEditor* editor); | |
c4608a8a VZ |
291 | |
292 | // find one of already registered data types | |
293 | int FindRegisteredDataType(const wxString& typeName); | |
294 | ||
295 | // try to FindRegisteredDataType(), if this fails and typeName is one of | |
296 | // standard typenames, register it and return its index | |
f2d76237 | 297 | int FindDataType(const wxString& typeName); |
c4608a8a VZ |
298 | |
299 | // try to FindDataType(), if it fails see if it is not one of already | |
300 | // registered data types with some params in which case clone the | |
301 | // registered data type and set params for it | |
302 | int FindOrCloneDataType(const wxString& typeName); | |
303 | ||
f2d76237 RD |
304 | wxGridCellRenderer* GetRenderer(int index); |
305 | wxGridCellEditor* GetEditor(int index); | |
306 | ||
307 | private: | |
308 | wxGridDataTypeInfoArray m_typeinfo; | |
309 | }; | |
310 | ||
b99be8fb VZ |
311 | // ---------------------------------------------------------------------------- |
312 | // conditional compilation | |
313 | // ---------------------------------------------------------------------------- | |
314 | ||
9496deb5 MB |
315 | #ifndef WXGRID_DRAW_LINES |
316 | #define WXGRID_DRAW_LINES 1 | |
796df70a SN |
317 | #endif |
318 | ||
0a976765 VZ |
319 | // ---------------------------------------------------------------------------- |
320 | // globals | |
321 | // ---------------------------------------------------------------------------- | |
322 | ||
323 | //#define DEBUG_ATTR_CACHE | |
324 | #ifdef DEBUG_ATTR_CACHE | |
325 | static size_t gs_nAttrCacheHits = 0; | |
326 | static size_t gs_nAttrCacheMisses = 0; | |
327 | #endif // DEBUG_ATTR_CACHE | |
f85afd4e | 328 | |
43947979 VZ |
329 | // ---------------------------------------------------------------------------- |
330 | // constants | |
331 | // ---------------------------------------------------------------------------- | |
332 | ||
f85afd4e MB |
333 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); |
334 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); | |
335 | ||
f0102d2a VZ |
336 | // scroll line size |
337 | // TODO: fixed so far - make configurable later (and also different for x/y) | |
338 | static const size_t GRID_SCROLL_LINE = 10; | |
f85afd4e | 339 | |
43947979 VZ |
340 | // the size of hash tables used a bit everywhere (the max number of elements |
341 | // in these hash tables is the number of rows/columns) | |
342 | static const int GRID_HASH_SIZE = 100; | |
343 | ||
ab79958a VZ |
344 | // ============================================================================ |
345 | // implementation | |
346 | // ============================================================================ | |
347 | ||
2796cce3 RD |
348 | // ---------------------------------------------------------------------------- |
349 | // wxGridCellEditor | |
350 | // ---------------------------------------------------------------------------- | |
351 | ||
352 | wxGridCellEditor::wxGridCellEditor() | |
353 | { | |
354 | m_control = NULL; | |
355 | } | |
356 | ||
357 | ||
358 | wxGridCellEditor::~wxGridCellEditor() | |
359 | { | |
360 | Destroy(); | |
361 | } | |
362 | ||
508011ce VZ |
363 | void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent), |
364 | wxWindowID WXUNUSED(id), | |
365 | wxEvtHandler* evtHandler) | |
366 | { | |
189d0213 | 367 | if ( evtHandler ) |
508011ce VZ |
368 | m_control->PushEventHandler(evtHandler); |
369 | } | |
2796cce3 | 370 | |
189d0213 VZ |
371 | void wxGridCellEditor::PaintBackground(const wxRect& rectCell, |
372 | wxGridCellAttr *attr) | |
373 | { | |
374 | // erase the background because we might not fill the cell | |
375 | wxClientDC dc(m_control->GetParent()); | |
376 | dc.SetPen(*wxTRANSPARENT_PEN); | |
377 | dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID)); | |
378 | dc.DrawRectangle(rectCell); | |
379 | ||
380 | // redraw the control we just painted over | |
381 | m_control->Refresh(); | |
382 | } | |
383 | ||
2796cce3 RD |
384 | void wxGridCellEditor::Destroy() |
385 | { | |
508011ce VZ |
386 | if (m_control) |
387 | { | |
b94ae1ea VZ |
388 | m_control->PopEventHandler(TRUE /* delete it*/); |
389 | ||
2796cce3 RD |
390 | m_control->Destroy(); |
391 | m_control = NULL; | |
392 | } | |
393 | } | |
394 | ||
3da93aae | 395 | void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr) |
2796cce3 RD |
396 | { |
397 | wxASSERT_MSG(m_control, | |
398 | wxT("The wxGridCellEditor must be Created first!")); | |
399 | m_control->Show(show); | |
3da93aae VZ |
400 | |
401 | if ( show ) | |
402 | { | |
403 | // set the colours/fonts if we have any | |
404 | if ( attr ) | |
405 | { | |
f2d76237 RD |
406 | m_colFgOld = m_control->GetForegroundColour(); |
407 | m_control->SetForegroundColour(attr->GetTextColour()); | |
3da93aae | 408 | |
f2d76237 RD |
409 | m_colBgOld = m_control->GetBackgroundColour(); |
410 | m_control->SetBackgroundColour(attr->GetBackgroundColour()); | |
3da93aae | 411 | |
f2d76237 RD |
412 | m_fontOld = m_control->GetFont(); |
413 | m_control->SetFont(attr->GetFont()); | |
3da93aae VZ |
414 | |
415 | // can't do anything more in the base class version, the other | |
416 | // attributes may only be used by the derived classes | |
417 | } | |
418 | } | |
419 | else | |
420 | { | |
421 | // restore the standard colours fonts | |
422 | if ( m_colFgOld.Ok() ) | |
423 | { | |
424 | m_control->SetForegroundColour(m_colFgOld); | |
425 | m_colFgOld = wxNullColour; | |
426 | } | |
427 | ||
428 | if ( m_colBgOld.Ok() ) | |
429 | { | |
430 | m_control->SetBackgroundColour(m_colBgOld); | |
431 | m_colBgOld = wxNullColour; | |
432 | } | |
433 | ||
434 | if ( m_fontOld.Ok() ) | |
435 | { | |
436 | m_control->SetFont(m_fontOld); | |
437 | m_fontOld = wxNullFont; | |
438 | } | |
439 | } | |
2796cce3 RD |
440 | } |
441 | ||
442 | void wxGridCellEditor::SetSize(const wxRect& rect) | |
443 | { | |
444 | wxASSERT_MSG(m_control, | |
445 | wxT("The wxGridCellEditor must be Created first!")); | |
28a77bc4 | 446 | m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); |
2796cce3 RD |
447 | } |
448 | ||
449 | void wxGridCellEditor::HandleReturn(wxKeyEvent& event) | |
450 | { | |
451 | event.Skip(); | |
452 | } | |
453 | ||
454 | ||
2c9a89e0 RD |
455 | void wxGridCellEditor::StartingKey(wxKeyEvent& event) |
456 | { | |
e195a54c VZ |
457 | event.Skip(); |
458 | } | |
2c9a89e0 | 459 | |
e195a54c VZ |
460 | void wxGridCellEditor::StartingClick() |
461 | { | |
b54ba671 | 462 | } |
2c9a89e0 | 463 | |
b54ba671 VZ |
464 | // ---------------------------------------------------------------------------- |
465 | // wxGridCellTextEditor | |
466 | // ---------------------------------------------------------------------------- | |
2c9a89e0 | 467 | |
2796cce3 RD |
468 | wxGridCellTextEditor::wxGridCellTextEditor() |
469 | { | |
c4608a8a | 470 | m_maxChars = 0; |
2796cce3 RD |
471 | } |
472 | ||
473 | void wxGridCellTextEditor::Create(wxWindow* parent, | |
474 | wxWindowID id, | |
2796cce3 RD |
475 | wxEvtHandler* evtHandler) |
476 | { | |
508011ce | 477 | m_control = new wxTextCtrl(parent, id, wxEmptyString, |
2c9a89e0 | 478 | wxDefaultPosition, wxDefaultSize |
2796cce3 | 479 | #if defined(__WXMSW__) |
2c9a89e0 | 480 | , wxTE_MULTILINE | wxTE_NO_VSCROLL // necessary ??? |
2796cce3 | 481 | #endif |
508011ce | 482 | ); |
2796cce3 | 483 | |
c4608a8a VZ |
484 | // TODO: use m_maxChars |
485 | ||
508011ce | 486 | wxGridCellEditor::Create(parent, id, evtHandler); |
2796cce3 RD |
487 | } |
488 | ||
189d0213 VZ |
489 | void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell), |
490 | wxGridCellAttr * WXUNUSED(attr)) | |
491 | { | |
492 | // as we fill the entire client area, don't do anything here to minimize | |
493 | // flicker | |
494 | } | |
2796cce3 | 495 | |
99306db2 VZ |
496 | void wxGridCellTextEditor::SetSize(const wxRect& rectOrig) |
497 | { | |
498 | wxRect rect(rectOrig); | |
499 | ||
500 | // Make the edit control large enough to allow for internal | |
501 | // margins | |
502 | // | |
503 | // TODO: remove this if the text ctrl sizing is improved esp. for | |
504 | // unix | |
505 | // | |
506 | #if defined(__WXGTK__) | |
b0e282b3 RR |
507 | if (rect.x != 0) |
508 | { | |
509 | rect.x += 1; | |
510 | rect.y += 1; | |
511 | rect.width -= 1; | |
512 | rect.height -= 1; | |
513 | } | |
99306db2 | 514 | #else // !GTK |
cb105ad4 SN |
515 | int extra_x = ( rect.x > 2 )? 2 : 1; |
516 | int extra_y = ( rect.y > 2 )? 2 : 1; | |
99306db2 | 517 | #if defined(__WXMOTIF__) |
cb105ad4 SN |
518 | extra_x *= 2; |
519 | extra_y *= 2; | |
99306db2 | 520 | #endif |
cb105ad4 SN |
521 | rect.SetLeft( wxMax(0, rect.x - extra_x) ); |
522 | rect.SetTop( wxMax(0, rect.y - extra_y) ); | |
523 | rect.SetRight( rect.GetRight() + 2*extra_x ); | |
524 | rect.SetBottom( rect.GetBottom() + 2*extra_y ); | |
99306db2 VZ |
525 | #endif // GTK/!GTK |
526 | ||
527 | wxGridCellEditor::SetSize(rect); | |
528 | } | |
529 | ||
3da93aae | 530 | void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid) |
2796cce3 RD |
531 | { |
532 | wxASSERT_MSG(m_control, | |
533 | wxT("The wxGridCellEditor must be Created first!")); | |
534 | ||
535 | m_startValue = grid->GetTable()->GetValue(row, col); | |
816be743 VZ |
536 | |
537 | DoBeginEdit(m_startValue); | |
538 | } | |
539 | ||
540 | void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue) | |
541 | { | |
542 | Text()->SetValue(startValue); | |
b54ba671 VZ |
543 | Text()->SetInsertionPointEnd(); |
544 | Text()->SetFocus(); | |
2796cce3 RD |
545 | } |
546 | ||
3324d5f5 | 547 | bool wxGridCellTextEditor::EndEdit(int row, int col, |
3da93aae | 548 | wxGrid* grid) |
2796cce3 RD |
549 | { |
550 | wxASSERT_MSG(m_control, | |
551 | wxT("The wxGridCellEditor must be Created first!")); | |
552 | ||
553 | bool changed = FALSE; | |
b54ba671 | 554 | wxString value = Text()->GetValue(); |
2796cce3 RD |
555 | if (value != m_startValue) |
556 | changed = TRUE; | |
557 | ||
558 | if (changed) | |
559 | grid->GetTable()->SetValue(row, col, value); | |
2c9a89e0 | 560 | |
3da93aae | 561 | m_startValue = wxEmptyString; |
b54ba671 | 562 | Text()->SetValue(m_startValue); |
2796cce3 RD |
563 | |
564 | return changed; | |
565 | } | |
566 | ||
567 | ||
568 | void wxGridCellTextEditor::Reset() | |
569 | { | |
570 | wxASSERT_MSG(m_control, | |
571 | wxT("The wxGridCellEditor must be Created first!")); | |
572 | ||
816be743 VZ |
573 | DoReset(m_startValue); |
574 | } | |
575 | ||
576 | void wxGridCellTextEditor::DoReset(const wxString& startValue) | |
577 | { | |
578 | Text()->SetValue(startValue); | |
b54ba671 | 579 | Text()->SetInsertionPointEnd(); |
2796cce3 RD |
580 | } |
581 | ||
2c9a89e0 RD |
582 | void wxGridCellTextEditor::StartingKey(wxKeyEvent& event) |
583 | { | |
b54ba671 VZ |
584 | if ( !event.AltDown() && !event.MetaDown() && !event.ControlDown() ) |
585 | { | |
586 | // insert the key in the control | |
587 | long keycode = event.KeyCode(); | |
588 | if ( isprint(keycode) ) | |
589 | { | |
590 | // FIXME this is not going to work for non letters... | |
591 | if ( !event.ShiftDown() ) | |
592 | { | |
593 | keycode = tolower(keycode); | |
594 | } | |
595 | ||
596 | Text()->AppendText((wxChar)keycode); | |
597 | ||
598 | return; | |
599 | } | |
2c9a89e0 | 600 | |
2c9a89e0 | 601 | } |
2c9a89e0 | 602 | |
b54ba671 VZ |
603 | event.Skip(); |
604 | } | |
2c9a89e0 | 605 | |
2796cce3 RD |
606 | void wxGridCellTextEditor::HandleReturn(wxKeyEvent& event) |
607 | { | |
608 | #if defined(__WXMOTIF__) || defined(__WXGTK__) | |
609 | // wxMotif needs a little extra help... | |
b54ba671 VZ |
610 | int pos = Text()->GetInsertionPoint(); |
611 | wxString s( Text()->GetValue() ); | |
2796cce3 | 612 | s = s.Left(pos) + "\n" + s.Mid(pos); |
b54ba671 VZ |
613 | Text()->SetValue(s); |
614 | Text()->SetInsertionPoint( pos ); | |
2796cce3 RD |
615 | #else |
616 | // the other ports can handle a Return key press | |
617 | // | |
618 | event.Skip(); | |
619 | #endif | |
620 | } | |
621 | ||
c4608a8a VZ |
622 | void wxGridCellTextEditor::SetParameters(const wxString& params) |
623 | { | |
624 | if ( !params ) | |
625 | { | |
626 | // reset to default | |
627 | m_maxChars = 0; | |
628 | } | |
629 | else | |
630 | { | |
631 | long tmp; | |
632 | if ( !params.ToLong(&tmp) ) | |
633 | { | |
634 | wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string " | |
635 | "'%s' ignored"), params.c_str()); | |
636 | } | |
637 | else | |
638 | { | |
639 | m_maxChars = (size_t)tmp; | |
640 | } | |
641 | } | |
642 | } | |
643 | ||
816be743 VZ |
644 | // ---------------------------------------------------------------------------- |
645 | // wxGridCellNumberEditor | |
646 | // ---------------------------------------------------------------------------- | |
647 | ||
648 | wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max) | |
649 | { | |
650 | m_min = min; | |
651 | m_max = max; | |
652 | } | |
653 | ||
654 | void wxGridCellNumberEditor::Create(wxWindow* parent, | |
655 | wxWindowID id, | |
656 | wxEvtHandler* evtHandler) | |
657 | { | |
658 | if ( HasRange() ) | |
659 | { | |
660 | // create a spin ctrl | |
661 | m_control = new wxSpinCtrl(parent, -1, wxEmptyString, | |
662 | wxDefaultPosition, wxDefaultSize, | |
663 | wxSP_ARROW_KEYS, | |
664 | m_min, m_max); | |
665 | ||
666 | wxGridCellEditor::Create(parent, id, evtHandler); | |
667 | } | |
668 | else | |
669 | { | |
670 | // just a text control | |
671 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
672 | ||
673 | #if wxUSE_VALIDATORS | |
85bc0351 | 674 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); |
816be743 VZ |
675 | #endif // wxUSE_VALIDATORS |
676 | } | |
677 | } | |
678 | ||
679 | void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid) | |
680 | { | |
681 | // first get the value | |
682 | wxGridTableBase *table = grid->GetTable(); | |
683 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
684 | { | |
685 | m_valueOld = table->GetValueAsLong(row, col); | |
686 | } | |
687 | else | |
688 | { | |
a5777624 RD |
689 | wxString sValue = table->GetValue(row, col); |
690 | if (! sValue.ToLong(&m_valueOld)) | |
691 | { | |
692 | wxFAIL_MSG( _T("this cell doesn't have numeric value") ); | |
693 | return; | |
694 | } | |
816be743 VZ |
695 | } |
696 | ||
697 | if ( HasRange() ) | |
698 | { | |
699 | Spin()->SetValue(m_valueOld); | |
700 | } | |
701 | else | |
702 | { | |
703 | DoBeginEdit(GetString()); | |
704 | } | |
705 | } | |
706 | ||
3324d5f5 | 707 | bool wxGridCellNumberEditor::EndEdit(int row, int col, |
816be743 VZ |
708 | wxGrid* grid) |
709 | { | |
710 | bool changed; | |
711 | long value; | |
712 | ||
713 | if ( HasRange() ) | |
714 | { | |
715 | value = Spin()->GetValue(); | |
716 | changed = value != m_valueOld; | |
717 | } | |
718 | else | |
719 | { | |
720 | changed = Text()->GetValue().ToLong(&value) && (value != m_valueOld); | |
721 | } | |
722 | ||
723 | if ( changed ) | |
724 | { | |
a5777624 RD |
725 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER)) |
726 | grid->GetTable()->SetValueAsLong(row, col, value); | |
727 | else | |
728 | grid->GetTable()->SetValue(row, col, wxString::Format("%ld", value)); | |
816be743 VZ |
729 | } |
730 | ||
731 | return changed; | |
732 | } | |
733 | ||
734 | void wxGridCellNumberEditor::Reset() | |
735 | { | |
736 | if ( HasRange() ) | |
737 | { | |
738 | Spin()->SetValue(m_valueOld); | |
739 | } | |
740 | else | |
741 | { | |
742 | DoReset(GetString()); | |
743 | } | |
744 | } | |
745 | ||
746 | void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event) | |
747 | { | |
748 | if ( !HasRange() ) | |
749 | { | |
750 | long keycode = event.KeyCode(); | |
751 | if ( isdigit(keycode) || keycode == '+' || keycode == '-' ) | |
752 | { | |
753 | wxGridCellTextEditor::StartingKey(event); | |
754 | ||
755 | // skip Skip() below | |
756 | return; | |
757 | } | |
758 | } | |
759 | ||
760 | event.Skip(); | |
761 | } | |
9c4ba614 | 762 | |
c4608a8a VZ |
763 | void wxGridCellNumberEditor::SetParameters(const wxString& params) |
764 | { | |
765 | if ( !params ) | |
766 | { | |
767 | // reset to default | |
768 | m_min = | |
769 | m_max = -1; | |
770 | } | |
771 | else | |
772 | { | |
773 | long tmp; | |
774 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
775 | { | |
776 | m_min = (int)tmp; | |
777 | ||
778 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
779 | { | |
780 | m_max = (int)tmp; | |
781 | ||
782 | // skip the error message below | |
783 | return; | |
784 | } | |
785 | } | |
786 | ||
787 | wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string " | |
788 | "'%s' ignored"), params.c_str()); | |
789 | } | |
790 | } | |
791 | ||
816be743 VZ |
792 | // ---------------------------------------------------------------------------- |
793 | // wxGridCellFloatEditor | |
794 | // ---------------------------------------------------------------------------- | |
795 | ||
796 | void wxGridCellFloatEditor::Create(wxWindow* parent, | |
797 | wxWindowID id, | |
798 | wxEvtHandler* evtHandler) | |
799 | { | |
800 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
801 | ||
802 | #if wxUSE_VALIDATORS | |
85bc0351 | 803 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); |
816be743 VZ |
804 | #endif // wxUSE_VALIDATORS |
805 | } | |
806 | ||
807 | void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid) | |
808 | { | |
809 | // first get the value | |
810 | wxGridTableBase *table = grid->GetTable(); | |
811 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
812 | { | |
813 | m_valueOld = table->GetValueAsDouble(row, col); | |
814 | } | |
815 | else | |
816 | { | |
a5777624 RD |
817 | wxString sValue = table->GetValue(row, col); |
818 | if (! sValue.ToDouble(&m_valueOld)) | |
819 | { | |
820 | wxFAIL_MSG( _T("this cell doesn't have float value") ); | |
821 | return; | |
822 | } | |
816be743 VZ |
823 | } |
824 | ||
825 | DoBeginEdit(GetString()); | |
826 | } | |
827 | ||
3324d5f5 | 828 | bool wxGridCellFloatEditor::EndEdit(int row, int col, |
816be743 VZ |
829 | wxGrid* grid) |
830 | { | |
831 | double value; | |
832 | if ( Text()->GetValue().ToDouble(&value) && (value != m_valueOld) ) | |
833 | { | |
a5777624 RD |
834 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT)) |
835 | grid->GetTable()->SetValueAsDouble(row, col, value); | |
836 | else | |
837 | grid->GetTable()->SetValue(row, col, wxString::Format("%f", value)); | |
816be743 VZ |
838 | |
839 | return TRUE; | |
840 | } | |
841 | else | |
842 | { | |
843 | return FALSE; | |
844 | } | |
845 | } | |
846 | ||
847 | void wxGridCellFloatEditor::Reset() | |
848 | { | |
849 | DoReset(GetString()); | |
850 | } | |
851 | ||
852 | void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event) | |
853 | { | |
854 | long keycode = event.KeyCode(); | |
855 | if ( isdigit(keycode) || | |
856 | keycode == '+' || keycode == '-' || keycode == '.' ) | |
857 | { | |
858 | wxGridCellTextEditor::StartingKey(event); | |
859 | ||
860 | // skip Skip() below | |
861 | return; | |
862 | } | |
863 | ||
864 | event.Skip(); | |
865 | } | |
866 | ||
508011ce VZ |
867 | // ---------------------------------------------------------------------------- |
868 | // wxGridCellBoolEditor | |
869 | // ---------------------------------------------------------------------------- | |
870 | ||
871 | void wxGridCellBoolEditor::Create(wxWindow* parent, | |
872 | wxWindowID id, | |
873 | wxEvtHandler* evtHandler) | |
874 | { | |
875 | m_control = new wxCheckBox(parent, id, wxEmptyString, | |
876 | wxDefaultPosition, wxDefaultSize, | |
877 | wxNO_BORDER); | |
878 | ||
879 | wxGridCellEditor::Create(parent, id, evtHandler); | |
880 | } | |
881 | ||
882 | void wxGridCellBoolEditor::SetSize(const wxRect& r) | |
883 | { | |
b94ae1ea VZ |
884 | bool resize = FALSE; |
885 | wxSize size = m_control->GetSize(); | |
886 | wxCoord minSize = wxMin(r.width, r.height); | |
887 | ||
888 | // check if the checkbox is not too big/small for this cell | |
889 | wxSize sizeBest = m_control->GetBestSize(); | |
890 | if ( !(size == sizeBest) ) | |
891 | { | |
892 | // reset to default size if it had been made smaller | |
893 | size = sizeBest; | |
894 | ||
895 | resize = TRUE; | |
896 | } | |
897 | ||
898 | if ( size.x >= minSize || size.y >= minSize ) | |
899 | { | |
900 | // leave 1 pixel margin | |
901 | size.x = size.y = minSize - 2; | |
902 | ||
903 | resize = TRUE; | |
904 | } | |
905 | ||
906 | if ( resize ) | |
907 | { | |
908 | m_control->SetSize(size); | |
909 | } | |
910 | ||
508011ce | 911 | // position it in the centre of the rectangle (TODO: support alignment?) |
508011ce | 912 | |
b94ae1ea | 913 | #if defined(__WXGTK__) || defined (__WXMOTIF__) |
508011ce VZ |
914 | // the checkbox without label still has some space to the right in wxGTK, |
915 | // so shift it to the right | |
b94ae1ea VZ |
916 | size.x -= 8; |
917 | #elif defined(__WXMSW__) | |
a95e38c0 VZ |
918 | // here too, but in other way |
919 | size.x += 1; | |
b94ae1ea VZ |
920 | size.y -= 2; |
921 | #endif | |
508011ce | 922 | |
b94ae1ea | 923 | m_control->Move(r.x + r.width/2 - size.x/2, r.y + r.height/2 - size.y/2); |
508011ce VZ |
924 | } |
925 | ||
926 | void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr) | |
927 | { | |
99306db2 VZ |
928 | m_control->Show(show); |
929 | ||
189d0213 | 930 | if ( show ) |
508011ce | 931 | { |
189d0213 VZ |
932 | wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY; |
933 | CBox()->SetBackgroundColour(colBg); | |
508011ce | 934 | } |
508011ce VZ |
935 | } |
936 | ||
937 | void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid) | |
938 | { | |
939 | wxASSERT_MSG(m_control, | |
940 | wxT("The wxGridCellEditor must be Created first!")); | |
941 | ||
28a77bc4 | 942 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) |
f2d76237 RD |
943 | m_startValue = grid->GetTable()->GetValueAsBool(row, col); |
944 | else | |
945 | m_startValue = !!grid->GetTable()->GetValue(row, col); | |
508011ce VZ |
946 | CBox()->SetValue(m_startValue); |
947 | CBox()->SetFocus(); | |
948 | } | |
949 | ||
950 | bool wxGridCellBoolEditor::EndEdit(int row, int col, | |
508011ce VZ |
951 | wxGrid* grid) |
952 | { | |
953 | wxASSERT_MSG(m_control, | |
954 | wxT("The wxGridCellEditor must be Created first!")); | |
955 | ||
956 | bool changed = FALSE; | |
957 | bool value = CBox()->GetValue(); | |
958 | if ( value != m_startValue ) | |
959 | changed = TRUE; | |
960 | ||
961 | if ( changed ) | |
962 | { | |
28a77bc4 | 963 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) |
f2d76237 RD |
964 | grid->GetTable()->SetValueAsBool(row, col, value); |
965 | else | |
966 | grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString); | |
508011ce VZ |
967 | } |
968 | ||
969 | return changed; | |
970 | } | |
971 | ||
972 | void wxGridCellBoolEditor::Reset() | |
973 | { | |
974 | wxASSERT_MSG(m_control, | |
975 | wxT("The wxGridCellEditor must be Created first!")); | |
976 | ||
977 | CBox()->SetValue(m_startValue); | |
978 | } | |
979 | ||
e195a54c | 980 | void wxGridCellBoolEditor::StartingClick() |
508011ce | 981 | { |
e195a54c | 982 | CBox()->SetValue(!CBox()->GetValue()); |
508011ce VZ |
983 | } |
984 | ||
4ee5fc9c VZ |
985 | // ---------------------------------------------------------------------------- |
986 | // wxGridCellChoiceEditor | |
987 | // ---------------------------------------------------------------------------- | |
988 | ||
989 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count, | |
990 | const wxChar* choices[], | |
991 | bool allowOthers) | |
992 | : m_allowOthers(allowOthers) | |
993 | { | |
c4608a8a | 994 | if ( count ) |
4ee5fc9c | 995 | { |
c4608a8a VZ |
996 | m_choices.Alloc(count); |
997 | for ( size_t n = 0; n < count; n++ ) | |
998 | { | |
999 | m_choices.Add(choices[n]); | |
1000 | } | |
4ee5fc9c VZ |
1001 | } |
1002 | } | |
1003 | ||
c4608a8a VZ |
1004 | wxGridCellEditor *wxGridCellChoiceEditor::Clone() const |
1005 | { | |
1006 | wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor; | |
1007 | editor->m_allowOthers = m_allowOthers; | |
1008 | editor->m_choices = m_choices; | |
1009 | ||
1010 | return editor; | |
1011 | } | |
1012 | ||
4ee5fc9c VZ |
1013 | void wxGridCellChoiceEditor::Create(wxWindow* parent, |
1014 | wxWindowID id, | |
1015 | wxEvtHandler* evtHandler) | |
1016 | { | |
1017 | size_t count = m_choices.GetCount(); | |
1018 | wxString *choices = new wxString[count]; | |
1019 | for ( size_t n = 0; n < count; n++ ) | |
1020 | { | |
1021 | choices[n] = m_choices[n]; | |
1022 | } | |
1023 | ||
1024 | m_control = new wxComboBox(parent, id, wxEmptyString, | |
1025 | wxDefaultPosition, wxDefaultSize, | |
1026 | count, choices, | |
1027 | m_allowOthers ? 0 : wxCB_READONLY); | |
1028 | ||
1029 | delete [] choices; | |
1030 | ||
1031 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1032 | } | |
1033 | ||
a5777624 RD |
1034 | void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell, |
1035 | wxGridCellAttr * attr) | |
4ee5fc9c VZ |
1036 | { |
1037 | // as we fill the entire client area, don't do anything here to minimize | |
1038 | // flicker | |
a5777624 RD |
1039 | |
1040 | // TODO: It doesn't actually fill the client area since the height of a | |
1041 | // combo always defaults to the standard... Until someone has time to | |
1042 | // figure out the right rectangle to paint, just do it the normal way... | |
1043 | wxGridCellEditor::PaintBackground(rectCell, attr); | |
4ee5fc9c VZ |
1044 | } |
1045 | ||
1046 | void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1047 | { | |
1048 | wxASSERT_MSG(m_control, | |
1049 | wxT("The wxGridCellEditor must be Created first!")); | |
1050 | ||
1051 | m_startValue = grid->GetTable()->GetValue(row, col); | |
1052 | ||
1053 | Combo()->SetValue(m_startValue); | |
28a77bc4 RD |
1054 | size_t count = m_choices.GetCount(); |
1055 | for (size_t i=0; i<count; i++) | |
1056 | { | |
1057 | if (m_startValue == m_choices[i]) | |
1058 | { | |
1059 | Combo()->SetSelection(i); | |
1060 | break; | |
1061 | } | |
1062 | } | |
4ee5fc9c VZ |
1063 | Combo()->SetInsertionPointEnd(); |
1064 | Combo()->SetFocus(); | |
1065 | } | |
1066 | ||
28a77bc4 | 1067 | bool wxGridCellChoiceEditor::EndEdit(int row, int col, |
4ee5fc9c VZ |
1068 | wxGrid* grid) |
1069 | { | |
1070 | wxString value = Combo()->GetValue(); | |
1071 | bool changed = value != m_startValue; | |
1072 | ||
1073 | if ( changed ) | |
1074 | grid->GetTable()->SetValue(row, col, value); | |
1075 | ||
1076 | m_startValue = wxEmptyString; | |
1077 | Combo()->SetValue(m_startValue); | |
1078 | ||
1079 | return changed; | |
1080 | } | |
1081 | ||
1082 | void wxGridCellChoiceEditor::Reset() | |
1083 | { | |
1084 | Combo()->SetValue(m_startValue); | |
1085 | Combo()->SetInsertionPointEnd(); | |
1086 | } | |
1087 | ||
c4608a8a VZ |
1088 | void wxGridCellChoiceEditor::SetParameters(const wxString& params) |
1089 | { | |
1090 | if ( !params ) | |
1091 | { | |
1092 | // what can we do? | |
1093 | return; | |
1094 | } | |
1095 | ||
1096 | m_choices.Empty(); | |
1097 | ||
1098 | wxStringTokenizer tk(params, _T(',')); | |
1099 | while ( tk.HasMoreTokens() ) | |
1100 | { | |
1101 | m_choices.Add(tk.GetNextToken()); | |
1102 | } | |
1103 | } | |
1104 | ||
508011ce VZ |
1105 | // ---------------------------------------------------------------------------- |
1106 | // wxGridCellEditorEvtHandler | |
1107 | // ---------------------------------------------------------------------------- | |
2796cce3 RD |
1108 | |
1109 | void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event) | |
1110 | { | |
1111 | switch ( event.KeyCode() ) | |
1112 | { | |
1113 | case WXK_ESCAPE: | |
1114 | m_editor->Reset(); | |
b54ba671 | 1115 | m_grid->DisableCellEditControl(); |
2796cce3 RD |
1116 | break; |
1117 | ||
2c9a89e0 | 1118 | case WXK_TAB: |
9b4aede2 RD |
1119 | event.Skip( m_grid->ProcessEvent( event ) ); |
1120 | break; | |
1121 | ||
2796cce3 RD |
1122 | case WXK_RETURN: |
1123 | if (!m_grid->ProcessEvent(event)) | |
1124 | m_editor->HandleReturn(event); | |
1125 | break; | |
1126 | ||
2796cce3 RD |
1127 | |
1128 | default: | |
1129 | event.Skip(); | |
1130 | } | |
1131 | } | |
1132 | ||
fb0de762 RD |
1133 | void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event) |
1134 | { | |
1135 | switch ( event.KeyCode() ) | |
1136 | { | |
1137 | case WXK_ESCAPE: | |
1138 | case WXK_TAB: | |
1139 | case WXK_RETURN: | |
1140 | break; | |
1141 | ||
1142 | default: | |
1143 | event.Skip(); | |
1144 | } | |
1145 | } | |
1146 | ||
c4608a8a VZ |
1147 | // ---------------------------------------------------------------------------- |
1148 | // wxGridCellWorker is an (almost) empty common base class for | |
1149 | // wxGridCellRenderer and wxGridCellEditor managing ref counting | |
1150 | // ---------------------------------------------------------------------------- | |
1151 | ||
1152 | void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params)) | |
1153 | { | |
1154 | // nothing to do | |
1155 | } | |
1156 | ||
1157 | wxGridCellWorker::~wxGridCellWorker() | |
1158 | { | |
1159 | } | |
1160 | ||
508011ce VZ |
1161 | // ============================================================================ |
1162 | // renderer classes | |
1163 | // ============================================================================ | |
1164 | ||
ab79958a VZ |
1165 | // ---------------------------------------------------------------------------- |
1166 | // wxGridCellRenderer | |
1167 | // ---------------------------------------------------------------------------- | |
1168 | ||
1169 | void wxGridCellRenderer::Draw(wxGrid& grid, | |
2796cce3 | 1170 | wxGridCellAttr& attr, |
ab79958a VZ |
1171 | wxDC& dc, |
1172 | const wxRect& rect, | |
1173 | int row, int col, | |
1174 | bool isSelected) | |
1175 | { | |
1176 | dc.SetBackgroundMode( wxSOLID ); | |
1177 | ||
1178 | if ( isSelected ) | |
1179 | { | |
2796cce3 | 1180 | dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) ); |
ab79958a VZ |
1181 | } |
1182 | else | |
1183 | { | |
2796cce3 | 1184 | dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) ); |
ab79958a VZ |
1185 | } |
1186 | ||
1187 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
ab79958a VZ |
1188 | dc.DrawRectangle(rect); |
1189 | } | |
1190 | ||
508011ce VZ |
1191 | // ---------------------------------------------------------------------------- |
1192 | // wxGridCellStringRenderer | |
1193 | // ---------------------------------------------------------------------------- | |
1194 | ||
816be743 VZ |
1195 | void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid, |
1196 | wxGridCellAttr& attr, | |
1197 | wxDC& dc, | |
1198 | bool isSelected) | |
ab79958a | 1199 | { |
ab79958a VZ |
1200 | dc.SetBackgroundMode( wxTRANSPARENT ); |
1201 | ||
283b7808 VZ |
1202 | // TODO some special colours for attr.IsReadOnly() case? |
1203 | ||
ab79958a VZ |
1204 | if ( isSelected ) |
1205 | { | |
2796cce3 RD |
1206 | dc.SetTextBackground( grid.GetSelectionBackground() ); |
1207 | dc.SetTextForeground( grid.GetSelectionForeground() ); | |
ab79958a VZ |
1208 | } |
1209 | else | |
1210 | { | |
2796cce3 RD |
1211 | dc.SetTextBackground( attr.GetBackgroundColour() ); |
1212 | dc.SetTextForeground( attr.GetTextColour() ); | |
ab79958a | 1213 | } |
816be743 | 1214 | |
2796cce3 | 1215 | dc.SetFont( attr.GetFont() ); |
816be743 VZ |
1216 | } |
1217 | ||
65e4e78e VZ |
1218 | wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr, |
1219 | wxDC& dc, | |
1220 | const wxString& text) | |
1221 | { | |
1222 | wxCoord x, y; | |
1223 | dc.SetFont(attr.GetFont()); | |
1224 | dc.GetTextExtent(text, &x, &y); | |
1225 | ||
1226 | return wxSize(x, y); | |
1227 | } | |
1228 | ||
1229 | wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid, | |
1230 | wxGridCellAttr& attr, | |
1231 | wxDC& dc, | |
1232 | int row, int col) | |
1233 | { | |
1234 | return DoGetBestSize(attr, dc, grid.GetCellValue(row, col)); | |
1235 | } | |
1236 | ||
816be743 VZ |
1237 | void wxGridCellStringRenderer::Draw(wxGrid& grid, |
1238 | wxGridCellAttr& attr, | |
1239 | wxDC& dc, | |
1240 | const wxRect& rectCell, | |
1241 | int row, int col, | |
1242 | bool isSelected) | |
1243 | { | |
1244 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1245 | ||
1246 | // now we only have to draw the text | |
1247 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
ab79958a VZ |
1248 | |
1249 | int hAlign, vAlign; | |
2796cce3 | 1250 | attr.GetAlignment(&hAlign, &vAlign); |
ab79958a VZ |
1251 | |
1252 | wxRect rect = rectCell; | |
816be743 | 1253 | rect.Inflate(-1); |
ab79958a VZ |
1254 | |
1255 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), | |
1256 | rect, hAlign, vAlign); | |
1257 | } | |
1258 | ||
65e4e78e VZ |
1259 | // ---------------------------------------------------------------------------- |
1260 | // wxGridCellNumberRenderer | |
1261 | // ---------------------------------------------------------------------------- | |
1262 | ||
1263 | wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col) | |
1264 | { | |
1265 | wxGridTableBase *table = grid.GetTable(); | |
1266 | wxString text; | |
1267 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
1268 | { | |
1269 | text.Printf(_T("%ld"), table->GetValueAsLong(row, col)); | |
1270 | } | |
9c4ba614 VZ |
1271 | else |
1272 | { | |
1273 | text = table->GetValue(row, col); | |
1274 | } | |
65e4e78e VZ |
1275 | |
1276 | return text; | |
1277 | } | |
1278 | ||
816be743 VZ |
1279 | void wxGridCellNumberRenderer::Draw(wxGrid& grid, |
1280 | wxGridCellAttr& attr, | |
1281 | wxDC& dc, | |
1282 | const wxRect& rectCell, | |
1283 | int row, int col, | |
1284 | bool isSelected) | |
1285 | { | |
1286 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1287 | ||
1288 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1289 | ||
1290 | // draw the text right aligned by default | |
1291 | int hAlign, vAlign; | |
1292 | attr.GetAlignment(&hAlign, &vAlign); | |
1293 | hAlign = wxRIGHT; | |
1294 | ||
1295 | wxRect rect = rectCell; | |
1296 | rect.Inflate(-1); | |
1297 | ||
65e4e78e VZ |
1298 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
1299 | } | |
816be743 | 1300 | |
65e4e78e VZ |
1301 | wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid, |
1302 | wxGridCellAttr& attr, | |
1303 | wxDC& dc, | |
1304 | int row, int col) | |
1305 | { | |
1306 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
816be743 VZ |
1307 | } |
1308 | ||
1309 | // ---------------------------------------------------------------------------- | |
1310 | // wxGridCellFloatRenderer | |
1311 | // ---------------------------------------------------------------------------- | |
1312 | ||
1313 | wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision) | |
1314 | { | |
1315 | SetWidth(width); | |
1316 | SetPrecision(precision); | |
1317 | } | |
1318 | ||
e72b4213 VZ |
1319 | wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const |
1320 | { | |
1321 | wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer; | |
1322 | renderer->m_width = m_width; | |
1323 | renderer->m_precision = m_precision; | |
1324 | renderer->m_format = m_format; | |
1325 | ||
1326 | return renderer; | |
1327 | } | |
1328 | ||
65e4e78e VZ |
1329 | wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col) |
1330 | { | |
1331 | wxGridTableBase *table = grid.GetTable(); | |
0b190b0f VZ |
1332 | |
1333 | bool hasDouble; | |
1334 | double val; | |
65e4e78e VZ |
1335 | wxString text; |
1336 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
1337 | { | |
0b190b0f VZ |
1338 | val = table->GetValueAsDouble(row, col); |
1339 | hasDouble = TRUE; | |
65e4e78e | 1340 | } |
9c4ba614 VZ |
1341 | else |
1342 | { | |
1343 | text = table->GetValue(row, col); | |
0b190b0f | 1344 | hasDouble = text.ToDouble(&val); |
9c4ba614 | 1345 | } |
65e4e78e | 1346 | |
0b190b0f VZ |
1347 | if ( hasDouble ) |
1348 | { | |
1349 | if ( !m_format ) | |
1350 | { | |
1351 | if ( m_width == -1 ) | |
1352 | { | |
1353 | // default width/precision | |
1354 | m_format = _T("%f"); | |
1355 | } | |
1356 | else if ( m_precision == -1 ) | |
1357 | { | |
1358 | // default precision | |
1359 | m_format.Printf(_T("%%%d.f"), m_width); | |
1360 | } | |
1361 | else | |
1362 | { | |
1363 | m_format.Printf(_T("%%%d.%df"), m_width, m_precision); | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | text.Printf(m_format, val); | |
1368 | } | |
1369 | //else: text already contains the string | |
1370 | ||
65e4e78e VZ |
1371 | return text; |
1372 | } | |
1373 | ||
816be743 VZ |
1374 | void wxGridCellFloatRenderer::Draw(wxGrid& grid, |
1375 | wxGridCellAttr& attr, | |
1376 | wxDC& dc, | |
1377 | const wxRect& rectCell, | |
1378 | int row, int col, | |
1379 | bool isSelected) | |
1380 | { | |
1381 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1382 | ||
1383 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1384 | ||
1385 | // draw the text right aligned by default | |
1386 | int hAlign, vAlign; | |
1387 | attr.GetAlignment(&hAlign, &vAlign); | |
1388 | hAlign = wxRIGHT; | |
1389 | ||
1390 | wxRect rect = rectCell; | |
1391 | rect.Inflate(-1); | |
1392 | ||
65e4e78e VZ |
1393 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
1394 | } | |
816be743 | 1395 | |
65e4e78e VZ |
1396 | wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid, |
1397 | wxGridCellAttr& attr, | |
1398 | wxDC& dc, | |
1399 | int row, int col) | |
1400 | { | |
1401 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
816be743 VZ |
1402 | } |
1403 | ||
0b190b0f VZ |
1404 | void wxGridCellFloatRenderer::SetParameters(const wxString& params) |
1405 | { | |
1406 | bool ok = TRUE; | |
1407 | ||
1408 | if ( !params ) | |
1409 | { | |
1410 | // reset to defaults | |
1411 | SetWidth(-1); | |
1412 | SetPrecision(-1); | |
1413 | } | |
1414 | else | |
1415 | { | |
1416 | wxString tmp = params.BeforeFirst(_T(',')); | |
1417 | if ( !!tmp ) | |
1418 | { | |
1419 | long width; | |
1420 | if ( !tmp.ToLong(&width) ) | |
1421 | { | |
1422 | ok = FALSE; | |
1423 | } | |
1424 | else | |
1425 | { | |
1426 | SetWidth((int)width); | |
1427 | ||
1428 | tmp = params.AfterFirst(_T(',')); | |
1429 | if ( !!tmp ) | |
1430 | { | |
1431 | long precision; | |
1432 | if ( !tmp.ToLong(&precision) ) | |
1433 | { | |
1434 | ok = FALSE; | |
1435 | } | |
1436 | else | |
1437 | { | |
1438 | SetPrecision((int)precision); | |
1439 | } | |
1440 | } | |
1441 | } | |
1442 | } | |
1443 | ||
1444 | if ( !ok ) | |
1445 | { | |
1446 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string " | |
1447 | "'%s ignored"), params.c_str()); | |
1448 | } | |
1449 | } | |
1450 | } | |
1451 | ||
508011ce VZ |
1452 | // ---------------------------------------------------------------------------- |
1453 | // wxGridCellBoolRenderer | |
1454 | // ---------------------------------------------------------------------------- | |
1455 | ||
65e4e78e | 1456 | wxSize wxGridCellBoolRenderer::ms_sizeCheckMark; |
508011ce | 1457 | |
b94ae1ea VZ |
1458 | // FIXME these checkbox size calculations are really ugly... |
1459 | ||
65e4e78e | 1460 | // between checkmark and box |
a95e38c0 | 1461 | static const wxCoord wxGRID_CHECKMARK_MARGIN = 2; |
508011ce | 1462 | |
65e4e78e VZ |
1463 | wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid, |
1464 | wxGridCellAttr& WXUNUSED(attr), | |
1465 | wxDC& WXUNUSED(dc), | |
1466 | int WXUNUSED(row), | |
1467 | int WXUNUSED(col)) | |
1468 | { | |
1469 | // compute it only once (no locks for MT safeness in GUI thread...) | |
1470 | if ( !ms_sizeCheckMark.x ) | |
297da4ba | 1471 | { |
65e4e78e VZ |
1472 | // get checkbox size |
1473 | wxCoord checkSize = 0; | |
297da4ba VZ |
1474 | wxCheckBox *checkbox = new wxCheckBox(&grid, -1, wxEmptyString); |
1475 | wxSize size = checkbox->GetBestSize(); | |
a95e38c0 | 1476 | checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN; |
297da4ba | 1477 | |
65e4e78e | 1478 | // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result |
69d8f612 | 1479 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
65e4e78e | 1480 | checkSize -= size.y / 2; |
297da4ba VZ |
1481 | #endif |
1482 | ||
1483 | delete checkbox; | |
65e4e78e VZ |
1484 | |
1485 | ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize; | |
297da4ba VZ |
1486 | } |
1487 | ||
65e4e78e VZ |
1488 | return ms_sizeCheckMark; |
1489 | } | |
1490 | ||
1491 | void wxGridCellBoolRenderer::Draw(wxGrid& grid, | |
1492 | wxGridCellAttr& attr, | |
1493 | wxDC& dc, | |
1494 | const wxRect& rect, | |
1495 | int row, int col, | |
1496 | bool isSelected) | |
1497 | { | |
1498 | wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); | |
1499 | ||
297da4ba | 1500 | // draw a check mark in the centre (ignoring alignment - TODO) |
65e4e78e | 1501 | wxSize size = GetBestSize(grid, attr, dc, row, col); |
b94ae1ea VZ |
1502 | |
1503 | // don't draw outside the cell | |
1504 | wxCoord minSize = wxMin(rect.width, rect.height); | |
1505 | if ( size.x >= minSize || size.y >= minSize ) | |
1506 | { | |
1507 | // and even leave (at least) 1 pixel margin | |
1508 | size.x = size.y = minSize - 2; | |
1509 | } | |
1510 | ||
1511 | // draw a border around checkmark | |
a95e38c0 VZ |
1512 | wxRect rectBorder; |
1513 | rectBorder.x = rect.x + rect.width/2 - size.x/2; | |
1514 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
1515 | rectBorder.width = size.x; | |
1516 | rectBorder.height = size.y; | |
b94ae1ea | 1517 | |
f2d76237 | 1518 | bool value; |
b94ae1ea | 1519 | if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) |
f2d76237 RD |
1520 | value = grid.GetTable()->GetValueAsBool(row, col); |
1521 | else | |
1522 | value = !!grid.GetTable()->GetValue(row, col); | |
1523 | ||
1524 | if ( value ) | |
508011ce | 1525 | { |
a95e38c0 VZ |
1526 | wxRect rectMark = rectBorder; |
1527 | #ifdef __WXMSW__ | |
1528 | // MSW DrawCheckMark() is weird (and should probably be changed...) | |
1529 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2); | |
1530 | rectMark.x++; | |
1531 | rectMark.y++; | |
1532 | #else // !MSW | |
1533 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN); | |
1534 | #endif // MSW/!MSW | |
1535 | ||
508011ce VZ |
1536 | dc.SetTextForeground(attr.GetTextColour()); |
1537 | dc.DrawCheckMark(rectMark); | |
1538 | } | |
a95e38c0 VZ |
1539 | |
1540 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
1541 | dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID)); | |
1542 | dc.DrawRectangle(rectBorder); | |
508011ce VZ |
1543 | } |
1544 | ||
2796cce3 RD |
1545 | // ---------------------------------------------------------------------------- |
1546 | // wxGridCellAttr | |
1547 | // ---------------------------------------------------------------------------- | |
1548 | ||
39bcce60 | 1549 | wxGridCellAttr *wxGridCellAttr::Clone() const |
a68c1246 VZ |
1550 | { |
1551 | wxGridCellAttr *attr = new wxGridCellAttr; | |
1552 | if ( HasTextColour() ) | |
1553 | attr->SetTextColour(GetTextColour()); | |
1554 | if ( HasBackgroundColour() ) | |
1555 | attr->SetBackgroundColour(GetBackgroundColour()); | |
1556 | if ( HasFont() ) | |
1557 | attr->SetFont(GetFont()); | |
1558 | if ( HasAlignment() ) | |
1559 | attr->SetAlignment(m_hAlign, m_vAlign); | |
1560 | ||
1561 | if ( m_renderer ) | |
1562 | { | |
1563 | attr->SetRenderer(m_renderer); | |
39bcce60 | 1564 | m_renderer->IncRef(); |
a68c1246 VZ |
1565 | } |
1566 | if ( m_editor ) | |
1567 | { | |
1568 | attr->SetEditor(m_editor); | |
39bcce60 | 1569 | m_editor->IncRef(); |
a68c1246 VZ |
1570 | } |
1571 | ||
1572 | if ( IsReadOnly() ) | |
1573 | attr->SetReadOnly(); | |
1574 | ||
1575 | attr->SetDefAttr(m_defGridAttr); | |
1576 | ||
1577 | return attr; | |
1578 | } | |
1579 | ||
2796cce3 RD |
1580 | const wxColour& wxGridCellAttr::GetTextColour() const |
1581 | { | |
1582 | if (HasTextColour()) | |
508011ce | 1583 | { |
2796cce3 | 1584 | return m_colText; |
508011ce | 1585 | } |
2796cce3 | 1586 | else if (m_defGridAttr != this) |
508011ce | 1587 | { |
2796cce3 | 1588 | return m_defGridAttr->GetTextColour(); |
508011ce VZ |
1589 | } |
1590 | else | |
1591 | { | |
2796cce3 RD |
1592 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
1593 | return wxNullColour; | |
1594 | } | |
1595 | } | |
1596 | ||
1597 | ||
1598 | const wxColour& wxGridCellAttr::GetBackgroundColour() const | |
1599 | { | |
1600 | if (HasBackgroundColour()) | |
1601 | return m_colBack; | |
1602 | else if (m_defGridAttr != this) | |
1603 | return m_defGridAttr->GetBackgroundColour(); | |
508011ce VZ |
1604 | else |
1605 | { | |
2796cce3 RD |
1606 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
1607 | return wxNullColour; | |
1608 | } | |
1609 | } | |
1610 | ||
1611 | ||
1612 | const wxFont& wxGridCellAttr::GetFont() const | |
1613 | { | |
1614 | if (HasFont()) | |
1615 | return m_font; | |
1616 | else if (m_defGridAttr != this) | |
1617 | return m_defGridAttr->GetFont(); | |
508011ce VZ |
1618 | else |
1619 | { | |
2796cce3 RD |
1620 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
1621 | return wxNullFont; | |
1622 | } | |
1623 | } | |
1624 | ||
1625 | ||
1626 | void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const | |
1627 | { | |
508011ce VZ |
1628 | if (HasAlignment()) |
1629 | { | |
2796cce3 RD |
1630 | if ( hAlign ) *hAlign = m_hAlign; |
1631 | if ( vAlign ) *vAlign = m_vAlign; | |
1632 | } | |
1633 | else if (m_defGridAttr != this) | |
1634 | m_defGridAttr->GetAlignment(hAlign, vAlign); | |
508011ce VZ |
1635 | else |
1636 | { | |
2796cce3 RD |
1637 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
1638 | } | |
1639 | } | |
1640 | ||
1641 | ||
f2d76237 | 1642 | // GetRenderer and GetEditor use a slightly different decision path about |
28a77bc4 RD |
1643 | // which attribute to use. If a non-default attr object has one then it is |
1644 | // used, otherwise the default editor or renderer is fetched from the grid and | |
1645 | // used. It should be the default for the data type of the cell. If it is | |
1646 | // NULL (because the table has a type that the grid does not have in its | |
1647 | // registry,) then the grid's default editor or renderer is used. | |
1648 | ||
1649 | wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const | |
1650 | { | |
28a77bc4 | 1651 | wxGridCellRenderer* renderer = NULL; |
28a77bc4 | 1652 | |
0b190b0f VZ |
1653 | if ( m_defGridAttr != this || grid == NULL ) |
1654 | { | |
1655 | renderer = m_renderer; // use local attribute | |
1656 | if ( renderer ) | |
1657 | renderer->IncRef(); | |
1658 | } | |
1659 | ||
1660 | if ( !renderer && grid ) // get renderer for the data type | |
1661 | { | |
1662 | // GetDefaultRendererForCell() will do IncRef() for us | |
1663 | renderer = grid->GetDefaultRendererForCell(row, col); | |
1664 | } | |
1665 | ||
1666 | if ( !renderer ) | |
1667 | { | |
28a77bc4 | 1668 | // if we still don't have one then use the grid default |
0b190b0f | 1669 | // (no need for IncRef() here neither) |
28a77bc4 | 1670 | renderer = m_defGridAttr->GetRenderer(NULL,0,0); |
0b190b0f | 1671 | } |
28a77bc4 | 1672 | |
0b190b0f VZ |
1673 | if ( !renderer) |
1674 | { | |
2796cce3 | 1675 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
0b190b0f | 1676 | } |
28a77bc4 RD |
1677 | |
1678 | return renderer; | |
2796cce3 RD |
1679 | } |
1680 | ||
28a77bc4 | 1681 | wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const |
07296f0b | 1682 | { |
28a77bc4 | 1683 | wxGridCellEditor* editor = NULL; |
0b190b0f VZ |
1684 | |
1685 | if ( m_defGridAttr != this || grid == NULL ) | |
1686 | { | |
1687 | editor = m_editor; // use local attribute | |
1688 | if ( editor ) | |
1689 | editor->IncRef(); | |
1690 | } | |
1691 | ||
a95e38c0 VZ |
1692 | if ( !editor && grid ) // get renderer for the data type |
1693 | editor = grid->GetDefaultEditorForCell(row, col); | |
28a77bc4 | 1694 | |
0b190b0f | 1695 | if ( !editor ) |
28a77bc4 RD |
1696 | // if we still don't have one then use the grid default |
1697 | editor = m_defGridAttr->GetEditor(NULL,0,0); | |
1698 | ||
0b190b0f VZ |
1699 | if ( !editor ) |
1700 | { | |
07296f0b | 1701 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
0b190b0f | 1702 | } |
28a77bc4 RD |
1703 | |
1704 | return editor; | |
07296f0b RD |
1705 | } |
1706 | ||
b99be8fb | 1707 | // ---------------------------------------------------------------------------- |
758cbedf | 1708 | // wxGridCellAttrData |
b99be8fb VZ |
1709 | // ---------------------------------------------------------------------------- |
1710 | ||
758cbedf | 1711 | void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) |
b99be8fb VZ |
1712 | { |
1713 | int n = FindIndex(row, col); | |
1714 | if ( n == wxNOT_FOUND ) | |
1715 | { | |
1716 | // add the attribute | |
1717 | m_attrs.Add(new wxGridCellWithAttr(row, col, attr)); | |
1718 | } | |
1719 | else | |
1720 | { | |
1721 | if ( attr ) | |
1722 | { | |
1723 | // change the attribute | |
2e9a6788 | 1724 | m_attrs[(size_t)n].attr = attr; |
b99be8fb VZ |
1725 | } |
1726 | else | |
1727 | { | |
1728 | // remove this attribute | |
1729 | m_attrs.RemoveAt((size_t)n); | |
1730 | } | |
1731 | } | |
b99be8fb VZ |
1732 | } |
1733 | ||
758cbedf | 1734 | wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const |
b99be8fb VZ |
1735 | { |
1736 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
1737 | ||
1738 | int n = FindIndex(row, col); | |
1739 | if ( n != wxNOT_FOUND ) | |
1740 | { | |
2e9a6788 VZ |
1741 | attr = m_attrs[(size_t)n].attr; |
1742 | attr->IncRef(); | |
b99be8fb VZ |
1743 | } |
1744 | ||
1745 | return attr; | |
1746 | } | |
1747 | ||
4d60017a SN |
1748 | void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) |
1749 | { | |
1750 | size_t count = m_attrs.GetCount(); | |
1751 | for ( size_t n = 0; n < count; n++ ) | |
1752 | { | |
1753 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
1754 | wxCoord row = coords.GetRow(); |
1755 | if ((size_t)row >= pos) | |
1756 | { | |
1757 | if (numRows > 0) | |
1758 | { | |
1759 | // If rows inserted, include row counter where necessary | |
1760 | coords.SetRow(row + numRows); | |
1761 | } | |
1762 | else if (numRows < 0) | |
1763 | { | |
1764 | // If rows deleted ... | |
1765 | if ((size_t)row >= pos - numRows) | |
1766 | { | |
1767 | // ...either decrement row counter (if row still exists)... | |
1768 | coords.SetRow(row + numRows); | |
1769 | } | |
1770 | else | |
1771 | { | |
1772 | // ...or remove the attribute | |
1773 | m_attrs.RemoveAt((size_t)n); | |
1774 | n--; count--; | |
1775 | } | |
1776 | } | |
4d60017a SN |
1777 | } |
1778 | } | |
1779 | } | |
1780 | ||
1781 | void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) | |
1782 | { | |
1783 | size_t count = m_attrs.GetCount(); | |
1784 | for ( size_t n = 0; n < count; n++ ) | |
1785 | { | |
1786 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
1787 | wxCoord col = coords.GetCol(); |
1788 | if ( (size_t)col >= pos ) | |
1789 | { | |
1790 | if ( numCols > 0 ) | |
1791 | { | |
1792 | // If rows inserted, include row counter where necessary | |
1793 | coords.SetCol(col + numCols); | |
1794 | } | |
1795 | else if (numCols < 0) | |
1796 | { | |
1797 | // If rows deleted ... | |
1798 | if ((size_t)col >= pos - numCols) | |
1799 | { | |
1800 | // ...either decrement row counter (if row still exists)... | |
1801 | coords.SetCol(col + numCols); | |
1802 | } | |
1803 | else | |
1804 | { | |
1805 | // ...or remove the attribute | |
1806 | m_attrs.RemoveAt((size_t)n); | |
1807 | n--; count--; | |
1808 | } | |
1809 | } | |
4d60017a SN |
1810 | } |
1811 | } | |
1812 | } | |
1813 | ||
758cbedf | 1814 | int wxGridCellAttrData::FindIndex(int row, int col) const |
b99be8fb VZ |
1815 | { |
1816 | size_t count = m_attrs.GetCount(); | |
1817 | for ( size_t n = 0; n < count; n++ ) | |
1818 | { | |
1819 | const wxGridCellCoords& coords = m_attrs[n].coords; | |
1820 | if ( (coords.GetRow() == row) && (coords.GetCol() == col) ) | |
1821 | { | |
1822 | return n; | |
1823 | } | |
1824 | } | |
1825 | ||
1826 | return wxNOT_FOUND; | |
1827 | } | |
1828 | ||
758cbedf VZ |
1829 | // ---------------------------------------------------------------------------- |
1830 | // wxGridRowOrColAttrData | |
1831 | // ---------------------------------------------------------------------------- | |
1832 | ||
1833 | wxGridRowOrColAttrData::~wxGridRowOrColAttrData() | |
1834 | { | |
1835 | size_t count = m_attrs.Count(); | |
1836 | for ( size_t n = 0; n < count; n++ ) | |
1837 | { | |
1838 | m_attrs[n]->DecRef(); | |
1839 | } | |
1840 | } | |
1841 | ||
1842 | wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const | |
1843 | { | |
1844 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
1845 | ||
1846 | int n = m_rowsOrCols.Index(rowOrCol); | |
1847 | if ( n != wxNOT_FOUND ) | |
1848 | { | |
1849 | attr = m_attrs[(size_t)n]; | |
1850 | attr->IncRef(); | |
1851 | } | |
1852 | ||
1853 | return attr; | |
1854 | } | |
1855 | ||
1856 | void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) | |
1857 | { | |
a95e38c0 VZ |
1858 | int i = m_rowsOrCols.Index(rowOrCol); |
1859 | if ( i == wxNOT_FOUND ) | |
758cbedf VZ |
1860 | { |
1861 | // add the attribute | |
1862 | m_rowsOrCols.Add(rowOrCol); | |
1863 | m_attrs.Add(attr); | |
1864 | } | |
1865 | else | |
1866 | { | |
a95e38c0 | 1867 | size_t n = (size_t)i; |
758cbedf VZ |
1868 | if ( attr ) |
1869 | { | |
1870 | // change the attribute | |
a95e38c0 VZ |
1871 | m_attrs[n]->DecRef(); |
1872 | m_attrs[n] = attr; | |
758cbedf VZ |
1873 | } |
1874 | else | |
1875 | { | |
1876 | // remove this attribute | |
a95e38c0 VZ |
1877 | m_attrs[n]->DecRef(); |
1878 | m_rowsOrCols.RemoveAt(n); | |
1879 | m_attrs.RemoveAt(n); | |
758cbedf VZ |
1880 | } |
1881 | } | |
1882 | } | |
1883 | ||
4d60017a SN |
1884 | void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) |
1885 | { | |
1886 | size_t count = m_attrs.GetCount(); | |
1887 | for ( size_t n = 0; n < count; n++ ) | |
1888 | { | |
1889 | int & rowOrCol = m_rowsOrCols[n]; | |
d1c0b4f9 VZ |
1890 | if ( (size_t)rowOrCol >= pos ) |
1891 | { | |
1892 | if ( numRowsOrCols > 0 ) | |
1893 | { | |
1894 | // If rows inserted, include row counter where necessary | |
1895 | rowOrCol += numRowsOrCols; | |
1896 | } | |
1897 | else if ( numRowsOrCols < 0) | |
1898 | { | |
1899 | // If rows deleted, either decrement row counter (if row still exists) | |
1900 | if ((size_t)rowOrCol >= pos - numRowsOrCols) | |
1901 | rowOrCol += numRowsOrCols; | |
1902 | else | |
1903 | { | |
1904 | m_rowsOrCols.RemoveAt((size_t)n); | |
1905 | m_attrs.RemoveAt((size_t)n); | |
1906 | n--; count--; | |
1907 | } | |
1908 | } | |
4d60017a SN |
1909 | } |
1910 | } | |
1911 | } | |
1912 | ||
b99be8fb VZ |
1913 | // ---------------------------------------------------------------------------- |
1914 | // wxGridCellAttrProvider | |
1915 | // ---------------------------------------------------------------------------- | |
1916 | ||
1917 | wxGridCellAttrProvider::wxGridCellAttrProvider() | |
1918 | { | |
1919 | m_data = (wxGridCellAttrProviderData *)NULL; | |
1920 | } | |
1921 | ||
1922 | wxGridCellAttrProvider::~wxGridCellAttrProvider() | |
1923 | { | |
1924 | delete m_data; | |
1925 | } | |
1926 | ||
1927 | void wxGridCellAttrProvider::InitData() | |
1928 | { | |
1929 | m_data = new wxGridCellAttrProviderData; | |
1930 | } | |
1931 | ||
1932 | wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col) const | |
1933 | { | |
758cbedf VZ |
1934 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
1935 | if ( m_data ) | |
1936 | { | |
1937 | // first look for the attribute of this specific cell | |
1938 | attr = m_data->m_cellAttrs.GetAttr(row, col); | |
1939 | ||
1940 | if ( !attr ) | |
1941 | { | |
1942 | // then look for the col attr (col attributes are more common than | |
1943 | // the row ones, hence they have priority) | |
1944 | attr = m_data->m_colAttrs.GetAttr(col); | |
1945 | } | |
1946 | ||
1947 | if ( !attr ) | |
1948 | { | |
1949 | // finally try the row attributes | |
1950 | attr = m_data->m_rowAttrs.GetAttr(row); | |
1951 | } | |
1952 | } | |
1953 | ||
1954 | return attr; | |
b99be8fb VZ |
1955 | } |
1956 | ||
2e9a6788 | 1957 | void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, |
b99be8fb VZ |
1958 | int row, int col) |
1959 | { | |
1960 | if ( !m_data ) | |
1961 | InitData(); | |
1962 | ||
758cbedf VZ |
1963 | m_data->m_cellAttrs.SetAttr(attr, row, col); |
1964 | } | |
1965 | ||
1966 | void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row) | |
1967 | { | |
1968 | if ( !m_data ) | |
1969 | InitData(); | |
1970 | ||
1971 | m_data->m_rowAttrs.SetAttr(attr, row); | |
1972 | } | |
1973 | ||
1974 | void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) | |
1975 | { | |
1976 | if ( !m_data ) | |
1977 | InitData(); | |
1978 | ||
1979 | m_data->m_colAttrs.SetAttr(attr, col); | |
b99be8fb VZ |
1980 | } |
1981 | ||
4d60017a SN |
1982 | void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) |
1983 | { | |
1984 | if ( m_data ) | |
1985 | { | |
1986 | m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); | |
1987 | ||
d1c0b4f9 | 1988 | m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); |
4d60017a SN |
1989 | } |
1990 | } | |
1991 | ||
1992 | void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) | |
1993 | { | |
1994 | if ( m_data ) | |
1995 | { | |
1996 | m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); | |
1997 | ||
d1c0b4f9 | 1998 | m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); |
4d60017a SN |
1999 | } |
2000 | } | |
2001 | ||
f2d76237 RD |
2002 | // ---------------------------------------------------------------------------- |
2003 | // wxGridTypeRegistry | |
2004 | // ---------------------------------------------------------------------------- | |
2005 | ||
2006 | wxGridTypeRegistry::~wxGridTypeRegistry() | |
2007 | { | |
b94ae1ea VZ |
2008 | size_t count = m_typeinfo.Count(); |
2009 | for ( size_t i = 0; i < count; i++ ) | |
f2d76237 RD |
2010 | delete m_typeinfo[i]; |
2011 | } | |
2012 | ||
2013 | ||
2014 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, | |
2015 | wxGridCellRenderer* renderer, | |
2016 | wxGridCellEditor* editor) | |
2017 | { | |
f2d76237 RD |
2018 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); |
2019 | ||
2020 | // is it already registered? | |
c4608a8a | 2021 | int loc = FindRegisteredDataType(typeName); |
39bcce60 VZ |
2022 | if ( loc != wxNOT_FOUND ) |
2023 | { | |
f2d76237 RD |
2024 | delete m_typeinfo[loc]; |
2025 | m_typeinfo[loc] = info; | |
2026 | } | |
39bcce60 VZ |
2027 | else |
2028 | { | |
f2d76237 RD |
2029 | m_typeinfo.Add(info); |
2030 | } | |
2031 | } | |
2032 | ||
c4608a8a VZ |
2033 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) |
2034 | { | |
2035 | size_t count = m_typeinfo.GetCount(); | |
2036 | for ( size_t i = 0; i < count; i++ ) | |
2037 | { | |
2038 | if ( typeName == m_typeinfo[i]->m_typeName ) | |
2039 | { | |
2040 | return i; | |
2041 | } | |
2042 | } | |
2043 | ||
2044 | return wxNOT_FOUND; | |
2045 | } | |
2046 | ||
f2d76237 RD |
2047 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) |
2048 | { | |
c4608a8a VZ |
2049 | int index = FindRegisteredDataType(typeName); |
2050 | if ( index == wxNOT_FOUND ) | |
2051 | { | |
2052 | // check whether this is one of the standard ones, in which case | |
2053 | // register it "on the fly" | |
2054 | if ( typeName == wxGRID_VALUE_STRING ) | |
2055 | { | |
2056 | RegisterDataType(wxGRID_VALUE_STRING, | |
2057 | new wxGridCellStringRenderer, | |
2058 | new wxGridCellTextEditor); | |
2059 | } | |
2060 | else if ( typeName == wxGRID_VALUE_BOOL ) | |
2061 | { | |
2062 | RegisterDataType(wxGRID_VALUE_BOOL, | |
2063 | new wxGridCellBoolRenderer, | |
2064 | new wxGridCellBoolEditor); | |
2065 | } | |
2066 | else if ( typeName == wxGRID_VALUE_NUMBER ) | |
2067 | { | |
2068 | RegisterDataType(wxGRID_VALUE_NUMBER, | |
2069 | new wxGridCellNumberRenderer, | |
2070 | new wxGridCellNumberEditor); | |
2071 | } | |
2072 | else if ( typeName == wxGRID_VALUE_FLOAT ) | |
2073 | { | |
2074 | RegisterDataType(wxGRID_VALUE_FLOAT, | |
2075 | new wxGridCellFloatRenderer, | |
2076 | new wxGridCellFloatEditor); | |
2077 | } | |
2078 | else if ( typeName == wxGRID_VALUE_CHOICE ) | |
2079 | { | |
2080 | RegisterDataType(wxGRID_VALUE_CHOICE, | |
2081 | new wxGridCellStringRenderer, | |
2082 | new wxGridCellChoiceEditor); | |
2083 | } | |
2084 | else | |
2085 | { | |
2086 | return wxNOT_FOUND; | |
2087 | } | |
f2d76237 | 2088 | |
c4608a8a VZ |
2089 | // we get here only if just added the entry for this type, so return |
2090 | // the last index | |
2091 | index = m_typeinfo.GetCount() - 1; | |
2092 | } | |
2093 | ||
2094 | return index; | |
2095 | } | |
2096 | ||
2097 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) | |
2098 | { | |
2099 | int index = FindDataType(typeName); | |
2100 | if ( index == wxNOT_FOUND ) | |
2101 | { | |
2102 | // the first part of the typename is the "real" type, anything after ':' | |
2103 | // are the parameters for the renderer | |
2104 | index = FindDataType(typeName.BeforeFirst(_T(':'))); | |
2105 | if ( index == wxNOT_FOUND ) | |
2106 | { | |
2107 | return wxNOT_FOUND; | |
f2d76237 | 2108 | } |
c4608a8a VZ |
2109 | |
2110 | wxGridCellRenderer *renderer = GetRenderer(index); | |
2111 | wxGridCellRenderer *rendererOld = renderer; | |
2112 | renderer = renderer->Clone(); | |
2113 | rendererOld->DecRef(); | |
2114 | ||
2115 | wxGridCellEditor *editor = GetEditor(index); | |
2116 | wxGridCellEditor *editorOld = editor; | |
2117 | editor = editor->Clone(); | |
2118 | editorOld->DecRef(); | |
2119 | ||
2120 | // do it even if there are no parameters to reset them to defaults | |
2121 | wxString params = typeName.AfterFirst(_T(':')); | |
2122 | renderer->SetParameters(params); | |
2123 | editor->SetParameters(params); | |
2124 | ||
2125 | // register the new typename | |
c4608a8a VZ |
2126 | RegisterDataType(typeName, renderer, editor); |
2127 | ||
2128 | // we just registered it, it's the last one | |
2129 | index = m_typeinfo.GetCount() - 1; | |
f2d76237 RD |
2130 | } |
2131 | ||
c4608a8a | 2132 | return index; |
f2d76237 RD |
2133 | } |
2134 | ||
2135 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) | |
2136 | { | |
2137 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; | |
0b190b0f | 2138 | renderer->IncRef(); |
f2d76237 RD |
2139 | return renderer; |
2140 | } | |
2141 | ||
0b190b0f | 2142 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) |
f2d76237 RD |
2143 | { |
2144 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; | |
0b190b0f | 2145 | editor->IncRef(); |
f2d76237 RD |
2146 | return editor; |
2147 | } | |
2148 | ||
758cbedf VZ |
2149 | // ---------------------------------------------------------------------------- |
2150 | // wxGridTableBase | |
2151 | // ---------------------------------------------------------------------------- | |
2152 | ||
f85afd4e MB |
2153 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) |
2154 | ||
2155 | ||
2156 | wxGridTableBase::wxGridTableBase() | |
f85afd4e MB |
2157 | { |
2158 | m_view = (wxGrid *) NULL; | |
b99be8fb | 2159 | m_attrProvider = (wxGridCellAttrProvider *) NULL; |
f85afd4e MB |
2160 | } |
2161 | ||
2162 | wxGridTableBase::~wxGridTableBase() | |
2163 | { | |
b99be8fb VZ |
2164 | delete m_attrProvider; |
2165 | } | |
2166 | ||
2167 | void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider) | |
2168 | { | |
2169 | delete m_attrProvider; | |
2170 | m_attrProvider = attrProvider; | |
f85afd4e MB |
2171 | } |
2172 | ||
f2d76237 RD |
2173 | bool wxGridTableBase::CanHaveAttributes() |
2174 | { | |
2175 | if ( ! GetAttrProvider() ) | |
2176 | { | |
2177 | // use the default attr provider by default | |
2178 | SetAttrProvider(new wxGridCellAttrProvider); | |
2179 | } | |
2180 | return TRUE; | |
2181 | } | |
2182 | ||
b99be8fb VZ |
2183 | wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col) |
2184 | { | |
2185 | if ( m_attrProvider ) | |
2186 | return m_attrProvider->GetAttr(row, col); | |
2187 | else | |
2188 | return (wxGridCellAttr *)NULL; | |
2189 | } | |
2190 | ||
758cbedf | 2191 | void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) |
b99be8fb VZ |
2192 | { |
2193 | if ( m_attrProvider ) | |
2194 | { | |
2195 | m_attrProvider->SetAttr(attr, row, col); | |
2196 | } | |
2197 | else | |
2198 | { | |
2199 | // as we take ownership of the pointer and don't store it, we must | |
2200 | // free it now | |
39bcce60 | 2201 | wxSafeDecRef(attr); |
b99be8fb VZ |
2202 | } |
2203 | } | |
2204 | ||
758cbedf VZ |
2205 | void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) |
2206 | { | |
2207 | if ( m_attrProvider ) | |
2208 | { | |
2209 | m_attrProvider->SetRowAttr(attr, row); | |
2210 | } | |
2211 | else | |
2212 | { | |
2213 | // as we take ownership of the pointer and don't store it, we must | |
2214 | // free it now | |
39bcce60 | 2215 | wxSafeDecRef(attr); |
758cbedf VZ |
2216 | } |
2217 | } | |
2218 | ||
2219 | void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) | |
2220 | { | |
2221 | if ( m_attrProvider ) | |
2222 | { | |
2223 | m_attrProvider->SetColAttr(attr, col); | |
2224 | } | |
2225 | else | |
2226 | { | |
2227 | // as we take ownership of the pointer and don't store it, we must | |
2228 | // free it now | |
39bcce60 | 2229 | wxSafeDecRef(attr); |
758cbedf VZ |
2230 | } |
2231 | } | |
2232 | ||
4d60017a SN |
2233 | void wxGridTableBase::UpdateAttrRows( size_t pos, int numRows ) |
2234 | { | |
2235 | if ( m_attrProvider ) | |
2236 | { | |
2237 | m_attrProvider->UpdateAttrRows( pos, numRows ); | |
2238 | } | |
2239 | } | |
2240 | ||
2241 | void wxGridTableBase::UpdateAttrCols( size_t pos, int numCols ) | |
2242 | { | |
2243 | if ( m_attrProvider ) | |
2244 | { | |
2245 | m_attrProvider->UpdateAttrCols( pos, numCols ); | |
2246 | } | |
2247 | } | |
2248 | ||
f85afd4e MB |
2249 | bool wxGridTableBase::InsertRows( size_t pos, size_t numRows ) |
2250 | { | |
bd3c6758 MB |
2251 | wxFAIL_MSG( wxT("Called grid table class function InsertRows\n" |
2252 | "but your derived table class does not override this function") ); | |
8f177c8e | 2253 | |
f85afd4e MB |
2254 | return FALSE; |
2255 | } | |
2256 | ||
2257 | bool wxGridTableBase::AppendRows( size_t numRows ) | |
2258 | { | |
bd3c6758 MB |
2259 | wxFAIL_MSG( wxT("Called grid table class function AppendRows\n" |
2260 | "but your derived table class does not override this function")); | |
8f177c8e | 2261 | |
f85afd4e MB |
2262 | return FALSE; |
2263 | } | |
2264 | ||
2265 | bool wxGridTableBase::DeleteRows( size_t pos, size_t numRows ) | |
2266 | { | |
bd3c6758 MB |
2267 | wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n" |
2268 | "but your derived table class does not override this function")); | |
8f177c8e | 2269 | |
f85afd4e MB |
2270 | return FALSE; |
2271 | } | |
2272 | ||
2273 | bool wxGridTableBase::InsertCols( size_t pos, size_t numCols ) | |
2274 | { | |
bd3c6758 MB |
2275 | wxFAIL_MSG( wxT("Called grid table class function InsertCols\n" |
2276 | "but your derived table class does not override this function")); | |
8f177c8e | 2277 | |
f85afd4e MB |
2278 | return FALSE; |
2279 | } | |
2280 | ||
2281 | bool wxGridTableBase::AppendCols( size_t numCols ) | |
2282 | { | |
bd3c6758 MB |
2283 | wxFAIL_MSG(wxT("Called grid table class function AppendCols\n" |
2284 | "but your derived table class does not override this function")); | |
8f177c8e | 2285 | |
f85afd4e MB |
2286 | return FALSE; |
2287 | } | |
2288 | ||
2289 | bool wxGridTableBase::DeleteCols( size_t pos, size_t numCols ) | |
2290 | { | |
bd3c6758 MB |
2291 | wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n" |
2292 | "but your derived table class does not override this function")); | |
8f177c8e | 2293 | |
f85afd4e MB |
2294 | return FALSE; |
2295 | } | |
2296 | ||
2297 | ||
2298 | wxString wxGridTableBase::GetRowLabelValue( int row ) | |
2299 | { | |
2300 | wxString s; | |
f2d76237 RD |
2301 | s << row + 1; // RD: Starting the rows at zero confuses users, no matter |
2302 | // how much it makes sense to us geeks. | |
f85afd4e MB |
2303 | return s; |
2304 | } | |
2305 | ||
2306 | wxString wxGridTableBase::GetColLabelValue( int col ) | |
2307 | { | |
2308 | // default col labels are: | |
2309 | // cols 0 to 25 : A-Z | |
2310 | // cols 26 to 675 : AA-ZZ | |
2311 | // etc. | |
2312 | ||
2313 | wxString s; | |
2314 | unsigned int i, n; | |
2315 | for ( n = 1; ; n++ ) | |
2316 | { | |
f0102d2a | 2317 | s += (_T('A') + (wxChar)( col%26 )); |
f85afd4e MB |
2318 | col = col/26 - 1; |
2319 | if ( col < 0 ) break; | |
2320 | } | |
2321 | ||
2322 | // reverse the string... | |
2323 | wxString s2; | |
2324 | for ( i = 0; i < n; i++ ) | |
2325 | { | |
2326 | s2 += s[n-i-1]; | |
2327 | } | |
2328 | ||
2329 | return s2; | |
2330 | } | |
2331 | ||
2332 | ||
f2d76237 RD |
2333 | wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) ) |
2334 | { | |
816be743 | 2335 | return wxGRID_VALUE_STRING; |
f2d76237 RD |
2336 | } |
2337 | ||
2338 | bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col), | |
2339 | const wxString& typeName ) | |
2340 | { | |
816be743 | 2341 | return typeName == wxGRID_VALUE_STRING; |
f2d76237 RD |
2342 | } |
2343 | ||
2344 | bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName ) | |
2345 | { | |
2346 | return CanGetValueAs(row, col, typeName); | |
2347 | } | |
2348 | ||
2349 | long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) ) | |
2350 | { | |
2351 | return 0; | |
2352 | } | |
2353 | ||
2354 | double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) ) | |
2355 | { | |
2356 | return 0.0; | |
2357 | } | |
2358 | ||
2359 | bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) ) | |
2360 | { | |
2361 | return FALSE; | |
2362 | } | |
2363 | ||
2364 | void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col), | |
2365 | long WXUNUSED(value) ) | |
2366 | { | |
2367 | } | |
2368 | ||
2369 | void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col), | |
2370 | double WXUNUSED(value) ) | |
2371 | { | |
2372 | } | |
2373 | ||
2374 | void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col), | |
2375 | bool WXUNUSED(value) ) | |
2376 | { | |
2377 | } | |
2378 | ||
2379 | ||
2380 | void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
2381 | const wxString& WXUNUSED(typeName) ) | |
2382 | { | |
2383 | return NULL; | |
2384 | } | |
2385 | ||
2386 | void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
2387 | const wxString& WXUNUSED(typeName), | |
2388 | void* WXUNUSED(value) ) | |
2389 | { | |
2390 | } | |
2391 | ||
f85afd4e MB |
2392 | ////////////////////////////////////////////////////////////////////// |
2393 | // | |
2394 | // Message class for the grid table to send requests and notifications | |
2395 | // to the grid view | |
2396 | // | |
2397 | ||
2398 | wxGridTableMessage::wxGridTableMessage() | |
2399 | { | |
2400 | m_table = (wxGridTableBase *) NULL; | |
2401 | m_id = -1; | |
2402 | m_comInt1 = -1; | |
2403 | m_comInt2 = -1; | |
2404 | } | |
2405 | ||
2406 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, | |
2407 | int commandInt1, int commandInt2 ) | |
2408 | { | |
2409 | m_table = table; | |
2410 | m_id = id; | |
2411 | m_comInt1 = commandInt1; | |
2412 | m_comInt2 = commandInt2; | |
2413 | } | |
2414 | ||
2415 | ||
2416 | ||
2417 | ////////////////////////////////////////////////////////////////////// | |
2418 | // | |
2419 | // A basic grid table for string data. An object of this class will | |
2420 | // created by wxGrid if you don't specify an alternative table class. | |
2421 | // | |
2422 | ||
223d09f6 | 2423 | WX_DEFINE_OBJARRAY(wxGridStringArray) |
f85afd4e MB |
2424 | |
2425 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) | |
2426 | ||
2427 | wxGridStringTable::wxGridStringTable() | |
2428 | : wxGridTableBase() | |
2429 | { | |
2430 | } | |
2431 | ||
2432 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) | |
2433 | : wxGridTableBase() | |
2434 | { | |
2435 | int row, col; | |
8f177c8e | 2436 | |
f85afd4e MB |
2437 | m_data.Alloc( numRows ); |
2438 | ||
2439 | wxArrayString sa; | |
2440 | sa.Alloc( numCols ); | |
2441 | for ( col = 0; col < numCols; col++ ) | |
2442 | { | |
2443 | sa.Add( wxEmptyString ); | |
2444 | } | |
8f177c8e | 2445 | |
f85afd4e MB |
2446 | for ( row = 0; row < numRows; row++ ) |
2447 | { | |
2448 | m_data.Add( sa ); | |
2449 | } | |
2450 | } | |
2451 | ||
2452 | wxGridStringTable::~wxGridStringTable() | |
2453 | { | |
2454 | } | |
2455 | ||
2456 | long wxGridStringTable::GetNumberRows() | |
2457 | { | |
2458 | return m_data.GetCount(); | |
2459 | } | |
2460 | ||
2461 | long wxGridStringTable::GetNumberCols() | |
2462 | { | |
2463 | if ( m_data.GetCount() > 0 ) | |
2464 | return m_data[0].GetCount(); | |
2465 | else | |
2466 | return 0; | |
2467 | } | |
2468 | ||
2469 | wxString wxGridStringTable::GetValue( int row, int col ) | |
2470 | { | |
831243b1 | 2471 | wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
af547d51 VZ |
2472 | _T("invalid row or column index in wxGridStringTable") ); |
2473 | ||
f85afd4e MB |
2474 | return m_data[row][col]; |
2475 | } | |
2476 | ||
f2d76237 | 2477 | void wxGridStringTable::SetValue( int row, int col, const wxString& value ) |
f85afd4e | 2478 | { |
831243b1 | 2479 | wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
af547d51 VZ |
2480 | _T("invalid row or column index in wxGridStringTable") ); |
2481 | ||
f2d76237 | 2482 | m_data[row][col] = value; |
f85afd4e MB |
2483 | } |
2484 | ||
2485 | bool wxGridStringTable::IsEmptyCell( int row, int col ) | |
2486 | { | |
831243b1 | 2487 | wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
af547d51 VZ |
2488 | _T("invalid row or column index in wxGridStringTable") ); |
2489 | ||
f85afd4e MB |
2490 | return (m_data[row][col] == wxEmptyString); |
2491 | } | |
2492 | ||
f85afd4e MB |
2493 | void wxGridStringTable::Clear() |
2494 | { | |
2495 | int row, col; | |
2496 | int numRows, numCols; | |
8f177c8e | 2497 | |
f85afd4e MB |
2498 | numRows = m_data.GetCount(); |
2499 | if ( numRows > 0 ) | |
2500 | { | |
2501 | numCols = m_data[0].GetCount(); | |
2502 | ||
2503 | for ( row = 0; row < numRows; row++ ) | |
2504 | { | |
2505 | for ( col = 0; col < numCols; col++ ) | |
2506 | { | |
2507 | m_data[row][col] = wxEmptyString; | |
2508 | } | |
2509 | } | |
2510 | } | |
2511 | } | |
2512 | ||
2513 | ||
2514 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) | |
2515 | { | |
2516 | size_t row, col; | |
2517 | ||
2518 | size_t curNumRows = m_data.GetCount(); | |
2519 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 ); | |
8f177c8e | 2520 | |
f85afd4e MB |
2521 | if ( pos >= curNumRows ) |
2522 | { | |
2523 | return AppendRows( numRows ); | |
2524 | } | |
8f177c8e | 2525 | |
f85afd4e MB |
2526 | wxArrayString sa; |
2527 | sa.Alloc( curNumCols ); | |
2528 | for ( col = 0; col < curNumCols; col++ ) | |
2529 | { | |
2530 | sa.Add( wxEmptyString ); | |
2531 | } | |
2532 | ||
2533 | for ( row = pos; row < pos + numRows; row++ ) | |
2534 | { | |
2535 | m_data.Insert( sa, row ); | |
2536 | } | |
4d60017a | 2537 | UpdateAttrRows( pos, numRows ); |
f85afd4e MB |
2538 | if ( GetView() ) |
2539 | { | |
2540 | wxGridTableMessage msg( this, | |
2541 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, | |
2542 | pos, | |
2543 | numRows ); | |
8f177c8e | 2544 | |
f85afd4e MB |
2545 | GetView()->ProcessTableMessage( msg ); |
2546 | } | |
2547 | ||
2548 | return TRUE; | |
2549 | } | |
2550 | ||
2551 | bool wxGridStringTable::AppendRows( size_t numRows ) | |
2552 | { | |
2553 | size_t row, col; | |
2554 | ||
2555 | size_t curNumRows = m_data.GetCount(); | |
2556 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 ); | |
8f177c8e | 2557 | |
f85afd4e MB |
2558 | wxArrayString sa; |
2559 | if ( curNumCols > 0 ) | |
2560 | { | |
2561 | sa.Alloc( curNumCols ); | |
2562 | for ( col = 0; col < curNumCols; col++ ) | |
2563 | { | |
2564 | sa.Add( wxEmptyString ); | |
2565 | } | |
2566 | } | |
8f177c8e | 2567 | |
f85afd4e MB |
2568 | for ( row = 0; row < numRows; row++ ) |
2569 | { | |
2570 | m_data.Add( sa ); | |
2571 | } | |
2572 | ||
2573 | if ( GetView() ) | |
2574 | { | |
2575 | wxGridTableMessage msg( this, | |
2576 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, | |
2577 | numRows ); | |
8f177c8e | 2578 | |
f85afd4e MB |
2579 | GetView()->ProcessTableMessage( msg ); |
2580 | } | |
2581 | ||
8f177c8e | 2582 | return TRUE; |
f85afd4e MB |
2583 | } |
2584 | ||
2585 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) | |
2586 | { | |
2587 | size_t n; | |
2588 | ||
2589 | size_t curNumRows = m_data.GetCount(); | |
8f177c8e | 2590 | |
f85afd4e MB |
2591 | if ( pos >= curNumRows ) |
2592 | { | |
bd3c6758 MB |
2593 | wxString errmsg; |
2594 | errmsg.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n" | |
2595 | "Pos value is invalid for present table with %d rows", | |
2596 | pos, numRows, curNumRows ); | |
2597 | wxFAIL_MSG( wxT(errmsg) ); | |
f85afd4e MB |
2598 | return FALSE; |
2599 | } | |
2600 | ||
2601 | if ( numRows > curNumRows - pos ) | |
2602 | { | |
2603 | numRows = curNumRows - pos; | |
2604 | } | |
8f177c8e | 2605 | |
f85afd4e MB |
2606 | if ( numRows >= curNumRows ) |
2607 | { | |
2608 | m_data.Empty(); // don't release memory just yet | |
2609 | } | |
2610 | else | |
2611 | { | |
2612 | for ( n = 0; n < numRows; n++ ) | |
2613 | { | |
2614 | m_data.Remove( pos ); | |
2615 | } | |
2616 | } | |
9b4aede2 | 2617 | UpdateAttrRows( pos, -((int)numRows) ); |
f85afd4e MB |
2618 | if ( GetView() ) |
2619 | { | |
2620 | wxGridTableMessage msg( this, | |
2621 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, | |
2622 | pos, | |
2623 | numRows ); | |
8f177c8e | 2624 | |
f85afd4e MB |
2625 | GetView()->ProcessTableMessage( msg ); |
2626 | } | |
2627 | ||
8f177c8e | 2628 | return TRUE; |
f85afd4e MB |
2629 | } |
2630 | ||
2631 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) | |
2632 | { | |
2633 | size_t row, col; | |
2634 | ||
2635 | size_t curNumRows = m_data.GetCount(); | |
2636 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 ); | |
8f177c8e | 2637 | |
f85afd4e MB |
2638 | if ( pos >= curNumCols ) |
2639 | { | |
2640 | return AppendCols( numCols ); | |
2641 | } | |
2642 | ||
2643 | for ( row = 0; row < curNumRows; row++ ) | |
2644 | { | |
2645 | for ( col = pos; col < pos + numCols; col++ ) | |
2646 | { | |
2647 | m_data[row].Insert( wxEmptyString, col ); | |
2648 | } | |
2649 | } | |
4d60017a | 2650 | UpdateAttrCols( pos, numCols ); |
f85afd4e MB |
2651 | if ( GetView() ) |
2652 | { | |
2653 | wxGridTableMessage msg( this, | |
2654 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, | |
2655 | pos, | |
2656 | numCols ); | |
8f177c8e | 2657 | |
f85afd4e MB |
2658 | GetView()->ProcessTableMessage( msg ); |
2659 | } | |
2660 | ||
2661 | return TRUE; | |
2662 | } | |
2663 | ||
2664 | bool wxGridStringTable::AppendCols( size_t numCols ) | |
2665 | { | |
2666 | size_t row, n; | |
2667 | ||
2668 | size_t curNumRows = m_data.GetCount(); | |
2669 | if ( !curNumRows ) | |
2670 | { | |
2671 | // TODO: something better than this ? | |
2672 | // | |
bd3c6758 MB |
2673 | wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n" |
2674 | "Call AppendRows() first") ); | |
f85afd4e MB |
2675 | return FALSE; |
2676 | } | |
8f177c8e | 2677 | |
f85afd4e MB |
2678 | for ( row = 0; row < curNumRows; row++ ) |
2679 | { | |
2680 | for ( n = 0; n < numCols; n++ ) | |
2681 | { | |
2682 | m_data[row].Add( wxEmptyString ); | |
2683 | } | |
2684 | } | |
2685 | ||
2686 | if ( GetView() ) | |
2687 | { | |
2688 | wxGridTableMessage msg( this, | |
2689 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, | |
2690 | numCols ); | |
8f177c8e | 2691 | |
f85afd4e MB |
2692 | GetView()->ProcessTableMessage( msg ); |
2693 | } | |
2694 | ||
2695 | return TRUE; | |
2696 | } | |
2697 | ||
2698 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) | |
2699 | { | |
2700 | size_t row, n; | |
2701 | ||
2702 | size_t curNumRows = m_data.GetCount(); | |
2703 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 ); | |
8f177c8e | 2704 | |
f85afd4e MB |
2705 | if ( pos >= curNumCols ) |
2706 | { | |
bd3c6758 MB |
2707 | wxString errmsg; |
2708 | errmsg.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n" | |
2709 | "Pos value is invalid for present table with %d cols", | |
2710 | pos, numCols, curNumCols ); | |
2711 | wxFAIL_MSG( wxT( errmsg ) ); | |
8f177c8e | 2712 | return FALSE; |
f85afd4e MB |
2713 | } |
2714 | ||
2715 | if ( numCols > curNumCols - pos ) | |
2716 | { | |
8f177c8e | 2717 | numCols = curNumCols - pos; |
f85afd4e MB |
2718 | } |
2719 | ||
2720 | for ( row = 0; row < curNumRows; row++ ) | |
2721 | { | |
2722 | if ( numCols >= curNumCols ) | |
2723 | { | |
2724 | m_data[row].Clear(); | |
2725 | } | |
2726 | else | |
2727 | { | |
2728 | for ( n = 0; n < numCols; n++ ) | |
2729 | { | |
2730 | m_data[row].Remove( pos ); | |
2731 | } | |
2732 | } | |
2733 | } | |
9b4aede2 | 2734 | UpdateAttrCols( pos, -((int)numCols) ); |
f85afd4e MB |
2735 | if ( GetView() ) |
2736 | { | |
2737 | wxGridTableMessage msg( this, | |
2738 | wxGRIDTABLE_NOTIFY_COLS_DELETED, | |
2739 | pos, | |
2740 | numCols ); | |
8f177c8e | 2741 | |
f85afd4e MB |
2742 | GetView()->ProcessTableMessage( msg ); |
2743 | } | |
2744 | ||
8f177c8e | 2745 | return TRUE; |
f85afd4e MB |
2746 | } |
2747 | ||
2748 | wxString wxGridStringTable::GetRowLabelValue( int row ) | |
2749 | { | |
2750 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
2751 | { | |
2752 | // using default label | |
2753 | // | |
2754 | return wxGridTableBase::GetRowLabelValue( row ); | |
2755 | } | |
2756 | else | |
2757 | { | |
2758 | return m_rowLabels[ row ]; | |
2759 | } | |
2760 | } | |
2761 | ||
2762 | wxString wxGridStringTable::GetColLabelValue( int col ) | |
2763 | { | |
2764 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
2765 | { | |
2766 | // using default label | |
2767 | // | |
2768 | return wxGridTableBase::GetColLabelValue( col ); | |
2769 | } | |
2770 | else | |
2771 | { | |
2772 | return m_colLabels[ col ]; | |
2773 | } | |
2774 | } | |
2775 | ||
2776 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) | |
2777 | { | |
2778 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
2779 | { | |
2780 | int n = m_rowLabels.GetCount(); | |
2781 | int i; | |
2782 | for ( i = n; i <= row; i++ ) | |
2783 | { | |
2784 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); | |
2785 | } | |
2786 | } | |
2787 | ||
2788 | m_rowLabels[row] = value; | |
2789 | } | |
2790 | ||
2791 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) | |
2792 | { | |
2793 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
2794 | { | |
2795 | int n = m_colLabels.GetCount(); | |
2796 | int i; | |
2797 | for ( i = n; i <= col; i++ ) | |
2798 | { | |
2799 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); | |
2800 | } | |
2801 | } | |
2802 | ||
2803 | m_colLabels[col] = value; | |
2804 | } | |
2805 | ||
2806 | ||
2807 | ||
f85afd4e | 2808 | ////////////////////////////////////////////////////////////////////// |
2d66e025 MB |
2809 | ////////////////////////////////////////////////////////////////////// |
2810 | ||
2811 | IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow ) | |
2812 | ||
2813 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow ) | |
2814 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) | |
2815 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) | |
2816 | EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown ) | |
2817 | END_EVENT_TABLE() | |
2818 | ||
60ff3b99 VZ |
2819 | wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent, |
2820 | wxWindowID id, | |
2d66e025 | 2821 | const wxPoint &pos, const wxSize &size ) |
ebd773c6 | 2822 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS ) |
2d66e025 MB |
2823 | { |
2824 | m_owner = parent; | |
2825 | } | |
2826 | ||
2827 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent &event ) | |
2828 | { | |
2829 | wxPaintDC dc(this); | |
2830 | ||
2831 | // NO - don't do this because it will set both the x and y origin | |
2832 | // coords to match the parent scrolled window and we just want to | |
2833 | // set the y coord - MB | |
2834 | // | |
2835 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 2836 | |
790ad94f | 2837 | int x, y; |
2d66e025 MB |
2838 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
2839 | dc.SetDeviceOrigin( 0, -y ); | |
60ff3b99 | 2840 | |
2d66e025 MB |
2841 | m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); |
2842 | m_owner->DrawRowLabels( dc ); | |
2843 | } | |
2844 | ||
2845 | ||
2846 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
2847 | { | |
2848 | m_owner->ProcessRowLabelMouseEvent( event ); | |
2849 | } | |
2850 | ||
2851 | ||
2852 | // This seems to be required for wxMotif otherwise the mouse | |
2853 | // cursor must be in the cell edit control to get key events | |
2854 | // | |
2855 | void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
2856 | { | |
2857 | if ( !m_owner->ProcessEvent( event ) ) event.Skip(); | |
2858 | } | |
2859 | ||
2860 | ||
2861 | ||
2862 | ////////////////////////////////////////////////////////////////////// | |
2863 | ||
2864 | IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow ) | |
2865 | ||
2866 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow ) | |
2867 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) | |
2868 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) | |
2869 | EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown ) | |
2870 | END_EVENT_TABLE() | |
2871 | ||
60ff3b99 VZ |
2872 | wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent, |
2873 | wxWindowID id, | |
2d66e025 | 2874 | const wxPoint &pos, const wxSize &size ) |
ebd773c6 | 2875 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS ) |
2d66e025 MB |
2876 | { |
2877 | m_owner = parent; | |
2878 | } | |
2879 | ||
2880 | void wxGridColLabelWindow::OnPaint( wxPaintEvent &event ) | |
2881 | { | |
2882 | wxPaintDC dc(this); | |
2883 | ||
2884 | // NO - don't do this because it will set both the x and y origin | |
2885 | // coords to match the parent scrolled window and we just want to | |
2886 | // set the x coord - MB | |
2887 | // | |
2888 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 2889 | |
790ad94f | 2890 | int x, y; |
2d66e025 MB |
2891 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
2892 | dc.SetDeviceOrigin( -x, 0 ); | |
2893 | ||
60ff3b99 VZ |
2894 | m_owner->CalcColLabelsExposed( GetUpdateRegion() ); |
2895 | m_owner->DrawColLabels( dc ); | |
2d66e025 MB |
2896 | } |
2897 | ||
2898 | ||
2899 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
2900 | { | |
2901 | m_owner->ProcessColLabelMouseEvent( event ); | |
2902 | } | |
2903 | ||
2904 | ||
2905 | // This seems to be required for wxMotif otherwise the mouse | |
2906 | // cursor must be in the cell edit control to get key events | |
2907 | // | |
2908 | void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
2909 | { | |
2910 | if ( !m_owner->ProcessEvent( event ) ) event.Skip(); | |
2911 | } | |
2912 | ||
2913 | ||
2914 | ||
2915 | ////////////////////////////////////////////////////////////////////// | |
2916 | ||
2917 | IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow ) | |
2918 | ||
2919 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow ) | |
2920 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) | |
d2fdd8d2 | 2921 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint) |
2d66e025 MB |
2922 | EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown ) |
2923 | END_EVENT_TABLE() | |
2924 | ||
60ff3b99 VZ |
2925 | wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent, |
2926 | wxWindowID id, | |
2d66e025 | 2927 | const wxPoint &pos, const wxSize &size ) |
ebd773c6 | 2928 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS ) |
2d66e025 MB |
2929 | { |
2930 | m_owner = parent; | |
2931 | } | |
2932 | ||
d2fdd8d2 RR |
2933 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2934 | { | |
2935 | wxPaintDC dc(this); | |
b99be8fb | 2936 | |
d2fdd8d2 RR |
2937 | int client_height = 0; |
2938 | int client_width = 0; | |
2939 | GetClientSize( &client_width, &client_height ); | |
b99be8fb | 2940 | |
d2fdd8d2 RR |
2941 | dc.SetPen( *wxBLACK_PEN ); |
2942 | dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 ); | |
2943 | dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 ); | |
b99be8fb | 2944 | |
d2fdd8d2 RR |
2945 | dc.SetPen( *wxWHITE_PEN ); |
2946 | dc.DrawLine( 0, 0, client_width, 0 ); | |
2947 | dc.DrawLine( 0, 0, 0, client_height ); | |
2948 | } | |
2949 | ||
2d66e025 MB |
2950 | |
2951 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
2952 | { | |
2953 | m_owner->ProcessCornerLabelMouseEvent( event ); | |
2954 | } | |
2955 | ||
2956 | ||
2957 | // This seems to be required for wxMotif otherwise the mouse | |
2958 | // cursor must be in the cell edit control to get key events | |
2959 | // | |
2960 | void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
2961 | { | |
2962 | if ( !m_owner->ProcessEvent( event ) ) event.Skip(); | |
2963 | } | |
2964 | ||
2965 | ||
2966 | ||
f85afd4e MB |
2967 | ////////////////////////////////////////////////////////////////////// |
2968 | ||
2d66e025 MB |
2969 | IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxPanel ) |
2970 | ||
2971 | BEGIN_EVENT_TABLE( wxGridWindow, wxPanel ) | |
2972 | EVT_PAINT( wxGridWindow::OnPaint ) | |
2d66e025 MB |
2973 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) |
2974 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) | |
2796cce3 | 2975 | EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground ) |
2d66e025 MB |
2976 | END_EVENT_TABLE() |
2977 | ||
60ff3b99 VZ |
2978 | wxGridWindow::wxGridWindow( wxGrid *parent, |
2979 | wxGridRowLabelWindow *rowLblWin, | |
2d66e025 MB |
2980 | wxGridColLabelWindow *colLblWin, |
2981 | wxWindowID id, const wxPoint &pos, const wxSize &size ) | |
ebd773c6 | 2982 | : wxPanel( parent, id, pos, size, wxWANTS_CHARS, "grid window" ) |
2d66e025 MB |
2983 | { |
2984 | m_owner = parent; | |
2985 | m_rowLabelWin = rowLblWin; | |
2986 | m_colLabelWin = colLblWin; | |
2d66e025 MB |
2987 | SetBackgroundColour( "WHITE" ); |
2988 | } | |
2989 | ||
2990 | ||
2991 | wxGridWindow::~wxGridWindow() | |
2992 | { | |
2993 | } | |
2994 | ||
2995 | ||
2996 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
2997 | { | |
2998 | wxPaintDC dc( this ); | |
2999 | m_owner->PrepareDC( dc ); | |
796df70a SN |
3000 | wxRegion reg = GetUpdateRegion(); |
3001 | m_owner->CalcCellsExposed( reg ); | |
2d66e025 | 3002 | m_owner->DrawGridCellArea( dc ); |
9496deb5 | 3003 | #if WXGRID_DRAW_LINES |
796df70a SN |
3004 | m_owner->DrawAllGridLines( dc, reg ); |
3005 | #endif | |
a5777624 | 3006 | m_owner->DrawGridSpace( dc ); |
b3a7510d | 3007 | m_owner->DrawHighlight( dc ); |
2d66e025 MB |
3008 | } |
3009 | ||
3010 | ||
3011 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
3012 | { | |
3013 | wxPanel::ScrollWindow( dx, dy, rect ); | |
3014 | m_rowLabelWin->ScrollWindow( 0, dy, rect ); | |
3015 | m_colLabelWin->ScrollWindow( dx, 0, rect ); | |
3016 | } | |
3017 | ||
3018 | ||
3019 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) | |
3020 | { | |
3021 | m_owner->ProcessGridCellMouseEvent( event ); | |
3022 | } | |
3023 | ||
3024 | ||
3025 | // This seems to be required for wxMotif otherwise the mouse | |
3026 | // cursor must be in the cell edit control to get key events | |
3027 | // | |
3028 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) | |
3029 | { | |
3030 | if ( !m_owner->ProcessEvent( event ) ) event.Skip(); | |
3031 | } | |
f85afd4e | 3032 | |
7c8a8ad5 | 3033 | |
8dd4f536 VZ |
3034 | void wxGridWindow::OnEraseBackground(wxEraseEvent& event) |
3035 | { | |
8dd4f536 | 3036 | } |
025562fe | 3037 | |
2d66e025 MB |
3038 | |
3039 | ////////////////////////////////////////////////////////////////////// | |
3040 | ||
07296f0b | 3041 | |
2d66e025 MB |
3042 | IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow ) |
3043 | ||
3044 | BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow ) | |
f85afd4e MB |
3045 | EVT_PAINT( wxGrid::OnPaint ) |
3046 | EVT_SIZE( wxGrid::OnSize ) | |
f85afd4e | 3047 | EVT_KEY_DOWN( wxGrid::OnKeyDown ) |
2796cce3 | 3048 | EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground ) |
f85afd4e | 3049 | END_EVENT_TABLE() |
8f177c8e | 3050 | |
2d66e025 MB |
3051 | wxGrid::wxGrid( wxWindow *parent, |
3052 | wxWindowID id, | |
3053 | const wxPoint& pos, | |
3054 | const wxSize& size, | |
3055 | long style, | |
3056 | const wxString& name ) | |
ebd773c6 | 3057 | : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ), |
af547d51 VZ |
3058 | m_colMinWidths(GRID_HASH_SIZE), |
3059 | m_rowMinHeights(GRID_HASH_SIZE) | |
2d66e025 MB |
3060 | { |
3061 | Create(); | |
58dd5b3b MB |
3062 | } |
3063 | ||
3064 | ||
3065 | wxGrid::~wxGrid() | |
3066 | { | |
0a976765 | 3067 | ClearAttrCache(); |
39bcce60 | 3068 | wxSafeDecRef(m_defaultCellAttr); |
0a976765 VZ |
3069 | |
3070 | #ifdef DEBUG_ATTR_CACHE | |
3071 | size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses; | |
3072 | wxPrintf(_T("wxGrid attribute cache statistics: " | |
3073 | "total: %u, hits: %u (%u%%)\n"), | |
3074 | total, gs_nAttrCacheHits, | |
3075 | total ? (gs_nAttrCacheHits*100) / total : 0); | |
3076 | #endif | |
3077 | ||
2796cce3 RD |
3078 | if (m_ownTable) |
3079 | delete m_table; | |
f2d76237 RD |
3080 | |
3081 | delete m_typeRegistry; | |
b5808881 | 3082 | delete m_selection; |
58dd5b3b MB |
3083 | } |
3084 | ||
2d66e025 | 3085 | |
58dd5b3b MB |
3086 | // |
3087 | // ----- internal init and update functions | |
3088 | // | |
3089 | ||
3090 | void wxGrid::Create() | |
f0102d2a | 3091 | { |
4634a5d6 | 3092 | m_created = FALSE; // set to TRUE by CreateGrid |
4634a5d6 MB |
3093 | |
3094 | m_table = (wxGridTableBase *) NULL; | |
2796cce3 | 3095 | m_ownTable = FALSE; |
2c9a89e0 RD |
3096 | |
3097 | m_cellEditCtrlEnabled = FALSE; | |
4634a5d6 | 3098 | |
2796cce3 RD |
3099 | m_defaultCellAttr = new wxGridCellAttr; |
3100 | m_defaultCellAttr->SetDefAttr(m_defaultCellAttr); | |
f2d76237 RD |
3101 | |
3102 | // Set default cell attributes | |
3103 | m_defaultCellAttr->SetFont(GetFont()); | |
3104 | m_defaultCellAttr->SetAlignment(wxLEFT, wxTOP); | |
3105 | m_defaultCellAttr->SetTextColour( | |
3106 | wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT)); | |
3107 | m_defaultCellAttr->SetBackgroundColour( | |
3108 | wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
3109 | m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer); | |
3110 | m_defaultCellAttr->SetEditor(new wxGridCellTextEditor); | |
2796cce3 RD |
3111 | |
3112 | ||
4634a5d6 MB |
3113 | m_numRows = 0; |
3114 | m_numCols = 0; | |
3115 | m_currentCellCoords = wxGridNoCellCoords; | |
b99be8fb | 3116 | |
18f9565d MB |
3117 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; |
3118 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
2d66e025 | 3119 | |
c4608a8a | 3120 | // create the type registry |
f2d76237 | 3121 | m_typeRegistry = new wxGridTypeRegistry; |
b5808881 | 3122 | m_selection = 0; |
f2d76237 | 3123 | // subwindow components that make up the wxGrid |
7807d81c MB |
3124 | m_cornerLabelWin = new wxGridCornerLabelWindow( this, |
3125 | -1, | |
18f9565d MB |
3126 | wxDefaultPosition, |
3127 | wxDefaultSize ); | |
7807d81c | 3128 | |
60ff3b99 VZ |
3129 | m_rowLabelWin = new wxGridRowLabelWindow( this, |
3130 | -1, | |
18f9565d MB |
3131 | wxDefaultPosition, |
3132 | wxDefaultSize ); | |
2d66e025 MB |
3133 | |
3134 | m_colLabelWin = new wxGridColLabelWindow( this, | |
3135 | -1, | |
18f9565d MB |
3136 | wxDefaultPosition, |
3137 | wxDefaultSize ); | |
60ff3b99 | 3138 | |
60ff3b99 VZ |
3139 | m_gridWin = new wxGridWindow( this, |
3140 | m_rowLabelWin, | |
3141 | m_colLabelWin, | |
3142 | -1, | |
18f9565d | 3143 | wxDefaultPosition, |
2d66e025 MB |
3144 | wxDefaultSize ); |
3145 | ||
3146 | SetTargetWindow( m_gridWin ); | |
2d66e025 | 3147 | } |
f85afd4e | 3148 | |
2d66e025 | 3149 | |
b5808881 SN |
3150 | bool wxGrid::CreateGrid( int numRows, int numCols, |
3151 | wxGrid::wxGridSelectionModes selmode ) | |
2d66e025 MB |
3152 | { |
3153 | if ( m_created ) | |
3154 | { | |
2796cce3 | 3155 | wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); |
2d66e025 MB |
3156 | return FALSE; |
3157 | } | |
3158 | else | |
3159 | { | |
3160 | m_numRows = numRows; | |
3161 | m_numCols = numCols; | |
3162 | ||
3163 | m_table = new wxGridStringTable( m_numRows, m_numCols ); | |
3164 | m_table->SetView( this ); | |
2796cce3 RD |
3165 | m_ownTable = TRUE; |
3166 | Init(); | |
043d16b2 | 3167 | m_selection = new wxGridSelection( this, selmode ); |
2796cce3 RD |
3168 | m_created = TRUE; |
3169 | } | |
2796cce3 RD |
3170 | return m_created; |
3171 | } | |
3172 | ||
f1567cdd SN |
3173 | void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode) |
3174 | { | |
3175 | if ( !m_created ) | |
3176 | { | |
3177 | wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") ); | |
3178 | } | |
3179 | else | |
3180 | m_selection->SetSelectionMode( selmode ); | |
3181 | } | |
3182 | ||
043d16b2 SN |
3183 | bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership, |
3184 | wxGrid::wxGridSelectionModes selmode ) | |
2796cce3 RD |
3185 | { |
3186 | if ( m_created ) | |
3187 | { | |
fb295790 RD |
3188 | // RD: Actually, this should probably be allowed. I think it would be |
3189 | // nice to be able to switch multiple Tables in and out of a single | |
3190 | // View at runtime. Is there anything in the implmentation that would | |
3191 | // prevent this? | |
3192 | ||
d95b0c2b | 3193 | // At least, you now have to cope with m_selection |
2796cce3 RD |
3194 | wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); |
3195 | return FALSE; | |
3196 | } | |
3197 | else | |
3198 | { | |
3199 | m_numRows = table->GetNumberRows(); | |
3200 | m_numCols = table->GetNumberCols(); | |
3201 | ||
3202 | m_table = table; | |
3203 | m_table->SetView( this ); | |
3204 | if (takeOwnership) | |
3205 | m_ownTable = TRUE; | |
2d66e025 | 3206 | Init(); |
043d16b2 | 3207 | m_selection = new wxGridSelection( this, selmode ); |
2d66e025 MB |
3208 | m_created = TRUE; |
3209 | } | |
f85afd4e | 3210 | |
2d66e025 | 3211 | return m_created; |
f85afd4e MB |
3212 | } |
3213 | ||
2d66e025 | 3214 | |
f85afd4e MB |
3215 | void wxGrid::Init() |
3216 | { | |
f85afd4e MB |
3217 | if ( m_numRows <= 0 ) |
3218 | m_numRows = WXGRID_DEFAULT_NUMBER_ROWS; | |
3219 | ||
3220 | if ( m_numCols <= 0 ) | |
3221 | m_numCols = WXGRID_DEFAULT_NUMBER_COLS; | |
3222 | ||
3223 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; | |
3224 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
3225 | ||
60ff3b99 VZ |
3226 | if ( m_rowLabelWin ) |
3227 | { | |
3228 | m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour(); | |
3229 | } | |
3230 | else | |
3231 | { | |
3232 | m_labelBackgroundColour = wxColour( _T("WHITE") ); | |
3233 | } | |
3234 | ||
3235 | m_labelTextColour = wxColour( _T("BLACK") ); | |
f85afd4e | 3236 | |
0a976765 VZ |
3237 | // init attr cache |
3238 | m_attrCache.row = -1; | |
3239 | ||
f85afd4e MB |
3240 | // TODO: something better than this ? |
3241 | // | |
3242 | m_labelFont = this->GetFont(); | |
3243 | m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 ); | |
8f177c8e | 3244 | |
f85afd4e MB |
3245 | m_rowLabelHorizAlign = wxLEFT; |
3246 | m_rowLabelVertAlign = wxCENTRE; | |
3247 | ||
3248 | m_colLabelHorizAlign = wxCENTRE; | |
3249 | m_colLabelVertAlign = wxTOP; | |
3250 | ||
f85afd4e | 3251 | m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH; |
1f1ce288 MB |
3252 | m_defaultRowHeight = m_gridWin->GetCharHeight(); |
3253 | ||
d2fdd8d2 | 3254 | #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl() |
1f1ce288 MB |
3255 | m_defaultRowHeight += 8; |
3256 | #else | |
3257 | m_defaultRowHeight += 4; | |
3258 | #endif | |
3259 | ||
2d66e025 | 3260 | m_gridLineColour = wxColour( 128, 128, 255 ); |
f85afd4e | 3261 | m_gridLinesEnabled = TRUE; |
8f177c8e | 3262 | |
58dd5b3b | 3263 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
e2b42eeb | 3264 | m_winCapture = (wxWindow *)NULL; |
6e8524b1 MB |
3265 | m_canDragRowSize = TRUE; |
3266 | m_canDragColSize = TRUE; | |
4cfa5de6 | 3267 | m_canDragGridSize = TRUE; |
f85afd4e MB |
3268 | m_dragLastPos = -1; |
3269 | m_dragRowOrCol = -1; | |
3270 | m_isDragging = FALSE; | |
07296f0b | 3271 | m_startDragPos = wxDefaultPosition; |
f85afd4e | 3272 | |
07296f0b | 3273 | m_waitForSlowClick = FALSE; |
025562fe | 3274 | |
f85afd4e MB |
3275 | m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS ); |
3276 | m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE ); | |
3277 | ||
3278 | m_currentCellCoords = wxGridNoCellCoords; | |
f85afd4e | 3279 | |
b5808881 SN |
3280 | m_selectingTopLeft = wxGridNoCellCoords; |
3281 | m_selectingBottomRight = wxGridNoCellCoords; | |
2796cce3 RD |
3282 | m_selectionBackground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT); |
3283 | m_selectionForeground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
8f177c8e | 3284 | |
f85afd4e MB |
3285 | m_editable = TRUE; // default for whole grid |
3286 | ||
2d66e025 MB |
3287 | m_inOnKeyDown = FALSE; |
3288 | m_batchCount = 0; | |
3c79cf49 | 3289 | |
266e8367 VZ |
3290 | m_extraWidth = |
3291 | m_extraHeight = 50; | |
3292 | ||
3c79cf49 | 3293 | CalcDimensions(); |
7c1cb261 VZ |
3294 | } |
3295 | ||
3296 | // ---------------------------------------------------------------------------- | |
3297 | // the idea is to call these functions only when necessary because they create | |
3298 | // quite big arrays which eat memory mostly unnecessary - in particular, if | |
3299 | // default widths/heights are used for all rows/columns, we may not use these | |
3300 | // arrays at all | |
3301 | // | |
3302 | // with some extra code, it should be possible to only store the | |
3303 | // widths/heights different from default ones but this will be done later... | |
3304 | // ---------------------------------------------------------------------------- | |
3305 | ||
3306 | void wxGrid::InitRowHeights() | |
3307 | { | |
3308 | m_rowHeights.Empty(); | |
3309 | m_rowBottoms.Empty(); | |
3310 | ||
3311 | m_rowHeights.Alloc( m_numRows ); | |
3312 | m_rowBottoms.Alloc( m_numRows ); | |
3313 | ||
3314 | int rowBottom = 0; | |
3315 | for ( int i = 0; i < m_numRows; i++ ) | |
3316 | { | |
3317 | m_rowHeights.Add( m_defaultRowHeight ); | |
3318 | rowBottom += m_defaultRowHeight; | |
3319 | m_rowBottoms.Add( rowBottom ); | |
3320 | } | |
3321 | } | |
3322 | ||
3323 | void wxGrid::InitColWidths() | |
3324 | { | |
3325 | m_colWidths.Empty(); | |
3326 | m_colRights.Empty(); | |
3327 | ||
3328 | m_colWidths.Alloc( m_numCols ); | |
3329 | m_colRights.Alloc( m_numCols ); | |
3330 | int colRight = 0; | |
3331 | for ( int i = 0; i < m_numCols; i++ ) | |
3332 | { | |
3333 | m_colWidths.Add( m_defaultColWidth ); | |
3334 | colRight += m_defaultColWidth; | |
3335 | m_colRights.Add( colRight ); | |
3336 | } | |
3337 | } | |
3338 | ||
3339 | int wxGrid::GetColWidth(int col) const | |
3340 | { | |
3341 | return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col]; | |
3342 | } | |
3343 | ||
3344 | int wxGrid::GetColLeft(int col) const | |
3345 | { | |
3346 | return m_colRights.IsEmpty() ? col * m_defaultColWidth | |
3347 | : m_colRights[col] - m_colWidths[col]; | |
3348 | } | |
3349 | ||
3350 | int wxGrid::GetColRight(int col) const | |
3351 | { | |
3352 | return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth | |
3353 | : m_colRights[col]; | |
3354 | } | |
3355 | ||
3356 | int wxGrid::GetRowHeight(int row) const | |
3357 | { | |
3358 | return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row]; | |
3359 | } | |
2d66e025 | 3360 | |
7c1cb261 VZ |
3361 | int wxGrid::GetRowTop(int row) const |
3362 | { | |
3363 | return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight | |
3364 | : m_rowBottoms[row] - m_rowHeights[row]; | |
f85afd4e MB |
3365 | } |
3366 | ||
7c1cb261 VZ |
3367 | int wxGrid::GetRowBottom(int row) const |
3368 | { | |
3369 | return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight | |
3370 | : m_rowBottoms[row]; | |
3371 | } | |
f85afd4e MB |
3372 | |
3373 | void wxGrid::CalcDimensions() | |
3374 | { | |
f85afd4e | 3375 | int cw, ch; |
2d66e025 | 3376 | GetClientSize( &cw, &ch ); |
f85afd4e | 3377 | |
2d66e025 | 3378 | if ( m_numRows > 0 && m_numCols > 0 ) |
f85afd4e | 3379 | { |
266e8367 VZ |
3380 | int right = GetColRight( m_numCols-1 ) + m_extraWidth; |
3381 | int bottom = GetRowBottom( m_numRows-1 ) + m_extraHeight; | |
60ff3b99 | 3382 | |
2d66e025 MB |
3383 | // TODO: restore the scroll position that we had before sizing |
3384 | // | |
3385 | int x, y; | |
3386 | GetViewStart( &x, &y ); | |
f0102d2a VZ |
3387 | SetScrollbars( GRID_SCROLL_LINE, GRID_SCROLL_LINE, |
3388 | right/GRID_SCROLL_LINE, bottom/GRID_SCROLL_LINE, | |
be46e4a6 | 3389 | x, y ); |
f85afd4e | 3390 | } |
f85afd4e MB |
3391 | } |
3392 | ||
3393 | ||
7807d81c MB |
3394 | void wxGrid::CalcWindowSizes() |
3395 | { | |
3396 | int cw, ch; | |
3397 | GetClientSize( &cw, &ch ); | |
b99be8fb | 3398 | |
7807d81c MB |
3399 | if ( m_cornerLabelWin->IsShown() ) |
3400 | m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight ); | |
3401 | ||
3402 | if ( m_colLabelWin->IsShown() ) | |
3403 | m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight); | |
3404 | ||
3405 | if ( m_rowLabelWin->IsShown() ) | |
3406 | m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight); | |
3407 | ||
3408 | if ( m_gridWin->IsShown() ) | |
3409 | m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight); | |
3410 | } | |
3411 | ||
3412 | ||
f85afd4e MB |
3413 | // this is called when the grid table sends a message to say that it |
3414 | // has been redimensioned | |
3415 | // | |
3416 | bool wxGrid::Redimension( wxGridTableMessage& msg ) | |
3417 | { | |
3418 | int i; | |
8f177c8e | 3419 | |
7c1cb261 VZ |
3420 | // if we were using the default widths/heights so far, we must change them |
3421 | // now | |
3422 | if ( m_colWidths.IsEmpty() ) | |
3423 | { | |
3424 | InitColWidths(); | |
3425 | } | |
3426 | ||
3427 | if ( m_rowHeights.IsEmpty() ) | |
3428 | { | |
3429 | InitRowHeights(); | |
3430 | } | |
3431 | ||
f85afd4e MB |
3432 | switch ( msg.GetId() ) |
3433 | { | |
3434 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
3435 | { | |
3436 | size_t pos = msg.GetCommandInt(); | |
3437 | int numRows = msg.GetCommandInt2(); | |
3438 | for ( i = 0; i < numRows; i++ ) | |
3439 | { | |
3440 | m_rowHeights.Insert( m_defaultRowHeight, pos ); | |
3441 | m_rowBottoms.Insert( 0, pos ); | |
3442 | } | |
3443 | m_numRows += numRows; | |
2d66e025 MB |
3444 | |
3445 | int bottom = 0; | |
3446 | if ( pos > 0 ) bottom = m_rowBottoms[pos-1]; | |
60ff3b99 | 3447 | |
2d66e025 MB |
3448 | for ( i = pos; i < m_numRows; i++ ) |
3449 | { | |
3450 | bottom += m_rowHeights[i]; | |
3451 | m_rowBottoms[i] = bottom; | |
3452 | } | |
f85afd4e MB |
3453 | CalcDimensions(); |
3454 | } | |
3455 | return TRUE; | |
3456 | ||
3457 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
3458 | { | |
3459 | int numRows = msg.GetCommandInt(); | |
3460 | for ( i = 0; i < numRows; i++ ) | |
3461 | { | |
3462 | m_rowHeights.Add( m_defaultRowHeight ); | |
3463 | m_rowBottoms.Add( 0 ); | |
3464 | } | |
2d66e025 MB |
3465 | |
3466 | int oldNumRows = m_numRows; | |
f85afd4e | 3467 | m_numRows += numRows; |
2d66e025 MB |
3468 | |
3469 | int bottom = 0; | |
3470 | if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1]; | |
60ff3b99 | 3471 | |
2d66e025 MB |
3472 | for ( i = oldNumRows; i < m_numRows; i++ ) |
3473 | { | |
3474 | bottom += m_rowHeights[i]; | |
3475 | m_rowBottoms[i] = bottom; | |
3476 | } | |
f85afd4e MB |
3477 | CalcDimensions(); |
3478 | } | |
3479 | return TRUE; | |
3480 | ||
3481 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
3482 | { | |
3483 | size_t pos = msg.GetCommandInt(); | |
3484 | int numRows = msg.GetCommandInt2(); | |
3485 | for ( i = 0; i < numRows; i++ ) | |
3486 | { | |
3487 | m_rowHeights.Remove( pos ); | |
3488 | m_rowBottoms.Remove( pos ); | |
3489 | } | |
3490 | m_numRows -= numRows; | |
3491 | ||
f85afd4e MB |
3492 | if ( !m_numRows ) |
3493 | { | |
3494 | m_numCols = 0; | |
3495 | m_colWidths.Clear(); | |
3496 | m_colRights.Clear(); | |
3497 | m_currentCellCoords = wxGridNoCellCoords; | |
3498 | } | |
2d66e025 | 3499 | else |
f85afd4e | 3500 | { |
2d66e025 MB |
3501 | if ( m_currentCellCoords.GetRow() >= m_numRows ) |
3502 | m_currentCellCoords.Set( 0, 0 ); | |
3503 | ||
3504 | int h = 0; | |
3505 | for ( i = 0; i < m_numRows; i++ ) | |
3506 | { | |
3507 | h += m_rowHeights[i]; | |
3508 | m_rowBottoms[i] = h; | |
3509 | } | |
f85afd4e | 3510 | } |
60ff3b99 | 3511 | |
f85afd4e MB |
3512 | CalcDimensions(); |
3513 | } | |
3514 | return TRUE; | |
3515 | ||
3516 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
3517 | { | |
3518 | size_t pos = msg.GetCommandInt(); | |
3519 | int numCols = msg.GetCommandInt2(); | |
3520 | for ( i = 0; i < numCols; i++ ) | |
3521 | { | |
3522 | m_colWidths.Insert( m_defaultColWidth, pos ); | |
3523 | m_colRights.Insert( 0, pos ); | |
3524 | } | |
3525 | m_numCols += numCols; | |
2d66e025 MB |
3526 | |
3527 | int right = 0; | |
3528 | if ( pos > 0 ) right = m_colRights[pos-1]; | |
60ff3b99 | 3529 | |
2d66e025 MB |
3530 | for ( i = pos; i < m_numCols; i++ ) |
3531 | { | |
3532 | right += m_colWidths[i]; | |
3533 | m_colRights[i] = right; | |
3534 | } | |
f85afd4e MB |
3535 | CalcDimensions(); |
3536 | } | |
3537 | return TRUE; | |
3538 | ||
3539 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
3540 | { | |
3541 | int numCols = msg.GetCommandInt(); | |
3542 | for ( i = 0; i < numCols; i++ ) | |
3543 | { | |
3544 | m_colWidths.Add( m_defaultColWidth ); | |
3545 | m_colRights.Add( 0 ); | |
3546 | } | |
2d66e025 MB |
3547 | |
3548 | int oldNumCols = m_numCols; | |
f85afd4e | 3549 | m_numCols += numCols; |
2d66e025 MB |
3550 | |
3551 | int right = 0; | |
3552 | if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1]; | |
60ff3b99 | 3553 | |
2d66e025 MB |
3554 | for ( i = oldNumCols; i < m_numCols; i++ ) |
3555 | { | |
3556 | right += m_colWidths[i]; | |
3557 | m_colRights[i] = right; | |
3558 | } | |
f85afd4e MB |
3559 | CalcDimensions(); |
3560 | } | |
3561 | return TRUE; | |
3562 | ||
3563 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
3564 | { | |
3565 | size_t pos = msg.GetCommandInt(); | |
3566 | int numCols = msg.GetCommandInt2(); | |
3567 | for ( i = 0; i < numCols; i++ ) | |
3568 | { | |
3569 | m_colWidths.Remove( pos ); | |
3570 | m_colRights.Remove( pos ); | |
3571 | } | |
3572 | m_numCols -= numCols; | |
f85afd4e MB |
3573 | |
3574 | if ( !m_numCols ) | |
3575 | { | |
3576 | #if 0 // leave the row alone here so that AppendCols will work subsequently | |
3577 | m_numRows = 0; | |
3578 | m_rowHeights.Clear(); | |
3579 | m_rowBottoms.Clear(); | |
8f177c8e | 3580 | #endif |
f85afd4e MB |
3581 | m_currentCellCoords = wxGridNoCellCoords; |
3582 | } | |
60ff3b99 | 3583 | else |
f85afd4e | 3584 | { |
2d66e025 MB |
3585 | if ( m_currentCellCoords.GetCol() >= m_numCols ) |
3586 | m_currentCellCoords.Set( 0, 0 ); | |
3587 | ||
3588 | int w = 0; | |
3589 | for ( i = 0; i < m_numCols; i++ ) | |
3590 | { | |
3591 | w += m_colWidths[i]; | |
3592 | m_colRights[i] = w; | |
3593 | } | |
f85afd4e MB |
3594 | } |
3595 | CalcDimensions(); | |
3596 | } | |
3597 | return TRUE; | |
3598 | } | |
3599 | ||
3600 | return FALSE; | |
3601 | } | |
3602 | ||
3603 | ||
2d66e025 | 3604 | void wxGrid::CalcRowLabelsExposed( wxRegion& reg ) |
f85afd4e | 3605 | { |
2d66e025 MB |
3606 | wxRegionIterator iter( reg ); |
3607 | wxRect r; | |
f85afd4e | 3608 | |
2d66e025 | 3609 | m_rowLabelsExposed.Empty(); |
f85afd4e | 3610 | |
2d66e025 MB |
3611 | int top, bottom; |
3612 | while ( iter ) | |
f85afd4e | 3613 | { |
2d66e025 | 3614 | r = iter.GetRect(); |
f85afd4e | 3615 | |
2d66e025 MB |
3616 | // TODO: remove this when we can... |
3617 | // There is a bug in wxMotif that gives garbage update | |
3618 | // rectangles if you jump-scroll a long way by clicking the | |
3619 | // scrollbar with middle button. This is a work-around | |
3620 | // | |
3621 | #if defined(__WXMOTIF__) | |
3622 | int cw, ch; | |
3623 | m_gridWin->GetClientSize( &cw, &ch ); | |
3624 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
3625 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
3626 | #endif | |
f85afd4e | 3627 | |
2d66e025 MB |
3628 | // logical bounds of update region |
3629 | // | |
3630 | int dummy; | |
3631 | CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top ); | |
3632 | CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom ); | |
3633 | ||
3634 | // find the row labels within these bounds | |
3635 | // | |
3636 | int row; | |
2d66e025 MB |
3637 | for ( row = 0; row < m_numRows; row++ ) |
3638 | { | |
7c1cb261 VZ |
3639 | if ( GetRowBottom(row) < top ) |
3640 | continue; | |
2d66e025 | 3641 | |
6d55126d | 3642 | if ( GetRowTop(row) > bottom ) |
7c1cb261 | 3643 | break; |
60ff3b99 | 3644 | |
2d66e025 MB |
3645 | m_rowLabelsExposed.Add( row ); |
3646 | } | |
60ff3b99 | 3647 | |
2d66e025 | 3648 | iter++ ; |
f85afd4e MB |
3649 | } |
3650 | } | |
3651 | ||
3652 | ||
2d66e025 | 3653 | void wxGrid::CalcColLabelsExposed( wxRegion& reg ) |
f85afd4e | 3654 | { |
2d66e025 MB |
3655 | wxRegionIterator iter( reg ); |
3656 | wxRect r; | |
f85afd4e | 3657 | |
2d66e025 | 3658 | m_colLabelsExposed.Empty(); |
f85afd4e | 3659 | |
2d66e025 MB |
3660 | int left, right; |
3661 | while ( iter ) | |
f85afd4e | 3662 | { |
2d66e025 | 3663 | r = iter.GetRect(); |
f85afd4e | 3664 | |
2d66e025 MB |
3665 | // TODO: remove this when we can... |
3666 | // There is a bug in wxMotif that gives garbage update | |
3667 | // rectangles if you jump-scroll a long way by clicking the | |
3668 | // scrollbar with middle button. This is a work-around | |
3669 | // | |
3670 | #if defined(__WXMOTIF__) | |
3671 | int cw, ch; | |
3672 | m_gridWin->GetClientSize( &cw, &ch ); | |
3673 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
3674 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
3675 | #endif | |
3676 | ||
3677 | // logical bounds of update region | |
3678 | // | |
3679 | int dummy; | |
3680 | CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy ); | |
3681 | CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy ); | |
3682 | ||
3683 | // find the cells within these bounds | |
3684 | // | |
3685 | int col; | |
2d66e025 MB |
3686 | for ( col = 0; col < m_numCols; col++ ) |
3687 | { | |
7c1cb261 VZ |
3688 | if ( GetColRight(col) < left ) |
3689 | continue; | |
60ff3b99 | 3690 | |
7c1cb261 VZ |
3691 | if ( GetColLeft(col) > right ) |
3692 | break; | |
2d66e025 MB |
3693 | |
3694 | m_colLabelsExposed.Add( col ); | |
3695 | } | |
60ff3b99 | 3696 | |
2d66e025 | 3697 | iter++ ; |
f85afd4e MB |
3698 | } |
3699 | } | |
3700 | ||
3701 | ||
2d66e025 | 3702 | void wxGrid::CalcCellsExposed( wxRegion& reg ) |
f85afd4e | 3703 | { |
2d66e025 MB |
3704 | wxRegionIterator iter( reg ); |
3705 | wxRect r; | |
f85afd4e | 3706 | |
2d66e025 MB |
3707 | m_cellsExposed.Empty(); |
3708 | m_rowsExposed.Empty(); | |
3709 | m_colsExposed.Empty(); | |
f85afd4e | 3710 | |
2d66e025 MB |
3711 | int left, top, right, bottom; |
3712 | while ( iter ) | |
3713 | { | |
3714 | r = iter.GetRect(); | |
f85afd4e | 3715 | |
2d66e025 MB |
3716 | // TODO: remove this when we can... |
3717 | // There is a bug in wxMotif that gives garbage update | |
3718 | // rectangles if you jump-scroll a long way by clicking the | |
3719 | // scrollbar with middle button. This is a work-around | |
3720 | // | |
3721 | #if defined(__WXMOTIF__) | |
f85afd4e | 3722 | int cw, ch; |
2d66e025 MB |
3723 | m_gridWin->GetClientSize( &cw, &ch ); |
3724 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
3725 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
3726 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
3727 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
3728 | #endif | |
8f177c8e | 3729 | |
2d66e025 MB |
3730 | // logical bounds of update region |
3731 | // | |
3732 | CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
3733 | CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
f85afd4e | 3734 | |
2d66e025 | 3735 | // find the cells within these bounds |
f85afd4e | 3736 | // |
2d66e025 | 3737 | int row, col; |
2d66e025 | 3738 | for ( row = 0; row < m_numRows; row++ ) |
f85afd4e | 3739 | { |
7c1cb261 VZ |
3740 | if ( GetRowBottom(row) <= top ) |
3741 | continue; | |
f85afd4e | 3742 | |
7c1cb261 VZ |
3743 | if ( GetRowTop(row) > bottom ) |
3744 | break; | |
60ff3b99 | 3745 | |
2d66e025 | 3746 | m_rowsExposed.Add( row ); |
f85afd4e | 3747 | |
2d66e025 MB |
3748 | for ( col = 0; col < m_numCols; col++ ) |
3749 | { | |
7c1cb261 VZ |
3750 | if ( GetColRight(col) <= left ) |
3751 | continue; | |
60ff3b99 | 3752 | |
7c1cb261 VZ |
3753 | if ( GetColLeft(col) > right ) |
3754 | break; | |
60ff3b99 | 3755 | |
7c1cb261 VZ |
3756 | if ( m_colsExposed.Index( col ) == wxNOT_FOUND ) |
3757 | m_colsExposed.Add( col ); | |
2d66e025 MB |
3758 | m_cellsExposed.Add( wxGridCellCoords( row, col ) ); |
3759 | } | |
3760 | } | |
60ff3b99 | 3761 | |
7c1cb261 | 3762 | iter++; |
f85afd4e MB |
3763 | } |
3764 | } | |
3765 | ||
3766 | ||
2d66e025 | 3767 | void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 3768 | { |
2d66e025 MB |
3769 | int x, y, row; |
3770 | wxPoint pos( event.GetPosition() ); | |
3771 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 3772 | |
2d66e025 | 3773 | if ( event.Dragging() ) |
f85afd4e MB |
3774 | { |
3775 | m_isDragging = TRUE; | |
8f177c8e | 3776 | |
2d66e025 | 3777 | if ( event.LeftIsDown() ) |
f85afd4e MB |
3778 | { |
3779 | switch( m_cursorMode ) | |
3780 | { | |
f85afd4e MB |
3781 | case WXGRID_CURSOR_RESIZE_ROW: |
3782 | { | |
2d66e025 MB |
3783 | int cw, ch, left, dummy; |
3784 | m_gridWin->GetClientSize( &cw, &ch ); | |
3785 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
60ff3b99 | 3786 | |
2d66e025 MB |
3787 | wxClientDC dc( m_gridWin ); |
3788 | PrepareDC( dc ); | |
af547d51 VZ |
3789 | y = wxMax( y, |
3790 | GetRowTop(m_dragRowOrCol) + | |
3791 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
d2fdd8d2 | 3792 | dc.SetLogicalFunction(wxINVERT); |
f85afd4e MB |
3793 | if ( m_dragLastPos >= 0 ) |
3794 | { | |
2d66e025 | 3795 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); |
f85afd4e | 3796 | } |
2d66e025 MB |
3797 | dc.DrawLine( left, y, left+cw, y ); |
3798 | m_dragLastPos = y; | |
f85afd4e MB |
3799 | } |
3800 | break; | |
3801 | ||
3802 | case WXGRID_CURSOR_SELECT_ROW: | |
f85afd4e MB |
3803 | if ( (row = YToRow( y )) >= 0 && |
3804 | !IsInSelection( row, 0 ) ) | |
3805 | { | |
3806 | SelectRow( row, TRUE ); | |
3807 | } | |
e2b42eeb VZ |
3808 | |
3809 | // default label to suppress warnings about "enumeration value | |
3810 | // 'xxx' not handled in switch | |
3811 | default: | |
3812 | break; | |
f85afd4e MB |
3813 | } |
3814 | } | |
3815 | return; | |
3816 | } | |
3817 | ||
3818 | m_isDragging = FALSE; | |
8f177c8e | 3819 | |
60ff3b99 | 3820 | |
6d004f67 MB |
3821 | // ------------ Entering or leaving the window |
3822 | // | |
3823 | if ( event.Entering() || event.Leaving() ) | |
3824 | { | |
e2b42eeb | 3825 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
6d004f67 MB |
3826 | } |
3827 | ||
e2b42eeb | 3828 | |
2d66e025 | 3829 | // ------------ Left button pressed |
f85afd4e | 3830 | // |
6d004f67 | 3831 | else if ( event.LeftDown() ) |
f85afd4e | 3832 | { |
2d66e025 MB |
3833 | // don't send a label click event for a hit on the |
3834 | // edge of the row label - this is probably the user | |
3835 | // wanting to resize the row | |
3836 | // | |
3837 | if ( YToEdgeOfRow(y) < 0 ) | |
f85afd4e | 3838 | { |
2d66e025 | 3839 | row = YToRow(y); |
58dd5b3b | 3840 | if ( row >= 0 && |
b54ba671 | 3841 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) ) |
f85afd4e | 3842 | { |
2d66e025 | 3843 | SelectRow( row, event.ShiftDown() ); |
e2b42eeb | 3844 | ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin); |
f85afd4e | 3845 | } |
2d66e025 MB |
3846 | } |
3847 | else | |
3848 | { | |
3849 | // starting to drag-resize a row | |
3850 | // | |
6e8524b1 MB |
3851 | if ( CanDragRowSize() ) |
3852 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin); | |
2d66e025 MB |
3853 | } |
3854 | } | |
f85afd4e | 3855 | |
f85afd4e | 3856 | |
2d66e025 MB |
3857 | // ------------ Left double click |
3858 | // | |
3859 | else if (event.LeftDClick() ) | |
3860 | { | |
3861 | if ( YToEdgeOfRow(y) < 0 ) | |
3862 | { | |
3863 | row = YToRow(y); | |
b54ba671 | 3864 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ); |
f85afd4e MB |
3865 | } |
3866 | } | |
60ff3b99 VZ |
3867 | |
3868 | ||
2d66e025 | 3869 | // ------------ Left button released |
f85afd4e | 3870 | // |
2d66e025 | 3871 | else if ( event.LeftUp() ) |
f85afd4e | 3872 | { |
2d66e025 | 3873 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) |
f85afd4e | 3874 | { |
6d004f67 | 3875 | DoEndDragResizeRow(); |
60ff3b99 | 3876 | |
6d004f67 MB |
3877 | // Note: we are ending the event *after* doing |
3878 | // default processing in this case | |
3879 | // | |
b54ba671 | 3880 | SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); |
2d66e025 | 3881 | } |
f85afd4e | 3882 | |
e2b42eeb VZ |
3883 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
3884 | m_dragLastPos = -1; | |
2d66e025 | 3885 | } |
f85afd4e | 3886 | |
f85afd4e | 3887 | |
2d66e025 MB |
3888 | // ------------ Right button down |
3889 | // | |
3890 | else if ( event.RightDown() ) | |
3891 | { | |
3892 | row = YToRow(y); | |
b54ba671 | 3893 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) |
2d66e025 MB |
3894 | { |
3895 | // no default action at the moment | |
f85afd4e MB |
3896 | } |
3897 | } | |
60ff3b99 VZ |
3898 | |
3899 | ||
2d66e025 | 3900 | // ------------ Right double click |
f85afd4e | 3901 | // |
2d66e025 MB |
3902 | else if ( event.RightDClick() ) |
3903 | { | |
3904 | row = YToRow(y); | |
b54ba671 | 3905 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) |
2d66e025 MB |
3906 | { |
3907 | // no default action at the moment | |
3908 | } | |
3909 | } | |
60ff3b99 VZ |
3910 | |
3911 | ||
2d66e025 | 3912 | // ------------ No buttons down and mouse moving |
f85afd4e | 3913 | // |
2d66e025 | 3914 | else if ( event.Moving() ) |
f85afd4e | 3915 | { |
2d66e025 MB |
3916 | m_dragRowOrCol = YToEdgeOfRow( y ); |
3917 | if ( m_dragRowOrCol >= 0 ) | |
8f177c8e | 3918 | { |
2d66e025 | 3919 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
8f177c8e | 3920 | { |
e2b42eeb | 3921 | // don't capture the mouse yet |
6e8524b1 MB |
3922 | if ( CanDragRowSize() ) |
3923 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE); | |
8f177c8e | 3924 | } |
2d66e025 | 3925 | } |
6d004f67 | 3926 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 3927 | { |
e2b42eeb | 3928 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE); |
8f177c8e | 3929 | } |
f85afd4e | 3930 | } |
2d66e025 MB |
3931 | } |
3932 | ||
3933 | ||
3934 | void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) | |
3935 | { | |
3936 | int x, y, col; | |
3937 | wxPoint pos( event.GetPosition() ); | |
3938 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 3939 | |
2d66e025 | 3940 | if ( event.Dragging() ) |
f85afd4e | 3941 | { |
2d66e025 | 3942 | m_isDragging = TRUE; |
8f177c8e | 3943 | |
2d66e025 | 3944 | if ( event.LeftIsDown() ) |
8f177c8e | 3945 | { |
2d66e025 | 3946 | switch( m_cursorMode ) |
8f177c8e | 3947 | { |
2d66e025 | 3948 | case WXGRID_CURSOR_RESIZE_COL: |
8f177c8e | 3949 | { |
2d66e025 MB |
3950 | int cw, ch, dummy, top; |
3951 | m_gridWin->GetClientSize( &cw, &ch ); | |
3952 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
60ff3b99 | 3953 | |
2d66e025 MB |
3954 | wxClientDC dc( m_gridWin ); |
3955 | PrepareDC( dc ); | |
43947979 VZ |
3956 | |
3957 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + | |
3958 | GetColMinimalWidth(m_dragRowOrCol)); | |
d2fdd8d2 | 3959 | dc.SetLogicalFunction(wxINVERT); |
2d66e025 MB |
3960 | if ( m_dragLastPos >= 0 ) |
3961 | { | |
3962 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
3963 | } | |
3964 | dc.DrawLine( x, top, x, top+ch ); | |
3965 | m_dragLastPos = x; | |
f85afd4e | 3966 | } |
2d66e025 | 3967 | break; |
f85afd4e | 3968 | |
2d66e025 | 3969 | case WXGRID_CURSOR_SELECT_COL: |
2d66e025 MB |
3970 | if ( (col = XToCol( x )) >= 0 && |
3971 | !IsInSelection( 0, col ) ) | |
3972 | { | |
3973 | SelectCol( col, TRUE ); | |
3974 | } | |
e2b42eeb VZ |
3975 | |
3976 | // default label to suppress warnings about "enumeration value | |
3977 | // 'xxx' not handled in switch | |
3978 | default: | |
3979 | break; | |
2d66e025 | 3980 | } |
f85afd4e | 3981 | } |
2d66e025 | 3982 | return; |
f85afd4e | 3983 | } |
2d66e025 MB |
3984 | |
3985 | m_isDragging = FALSE; | |
3986 | ||
60ff3b99 | 3987 | |
6d004f67 MB |
3988 | // ------------ Entering or leaving the window |
3989 | // | |
3990 | if ( event.Entering() || event.Leaving() ) | |
3991 | { | |
e2b42eeb | 3992 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); |
6d004f67 MB |
3993 | } |
3994 | ||
e2b42eeb | 3995 | |
2d66e025 | 3996 | // ------------ Left button pressed |
f85afd4e | 3997 | // |
6d004f67 | 3998 | else if ( event.LeftDown() ) |
f85afd4e | 3999 | { |
2d66e025 MB |
4000 | // don't send a label click event for a hit on the |
4001 | // edge of the col label - this is probably the user | |
4002 | // wanting to resize the col | |
4003 | // | |
4004 | if ( XToEdgeOfCol(x) < 0 ) | |
8f177c8e | 4005 | { |
2d66e025 | 4006 | col = XToCol(x); |
58dd5b3b | 4007 | if ( col >= 0 && |
b54ba671 | 4008 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) ) |
8f177c8e | 4009 | { |
2d66e025 | 4010 | SelectCol( col, event.ShiftDown() ); |
e2b42eeb | 4011 | ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin); |
f85afd4e | 4012 | } |
2d66e025 MB |
4013 | } |
4014 | else | |
4015 | { | |
4016 | // starting to drag-resize a col | |
4017 | // | |
6e8524b1 MB |
4018 | if ( CanDragColSize() ) |
4019 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin); | |
2d66e025 MB |
4020 | } |
4021 | } | |
f85afd4e | 4022 | |
f85afd4e | 4023 | |
2d66e025 MB |
4024 | // ------------ Left double click |
4025 | // | |
4026 | if ( event.LeftDClick() ) | |
4027 | { | |
4028 | if ( XToEdgeOfCol(x) < 0 ) | |
4029 | { | |
4030 | col = XToCol(x); | |
b54ba671 | 4031 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ); |
2d66e025 MB |
4032 | } |
4033 | } | |
60ff3b99 VZ |
4034 | |
4035 | ||
2d66e025 MB |
4036 | // ------------ Left button released |
4037 | // | |
4038 | else if ( event.LeftUp() ) | |
4039 | { | |
4040 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
4041 | { | |
6d004f67 | 4042 | DoEndDragResizeCol(); |
e2b42eeb | 4043 | |
6d004f67 MB |
4044 | // Note: we are ending the event *after* doing |
4045 | // default processing in this case | |
4046 | // | |
b54ba671 | 4047 | SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); |
2d66e025 | 4048 | } |
f85afd4e | 4049 | |
e2b42eeb | 4050 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); |
2d66e025 | 4051 | m_dragLastPos = -1; |
60ff3b99 VZ |
4052 | } |
4053 | ||
4054 | ||
2d66e025 MB |
4055 | // ------------ Right button down |
4056 | // | |
4057 | else if ( event.RightDown() ) | |
4058 | { | |
4059 | col = XToCol(x); | |
b54ba671 | 4060 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) |
2d66e025 MB |
4061 | { |
4062 | // no default action at the moment | |
f85afd4e MB |
4063 | } |
4064 | } | |
60ff3b99 VZ |
4065 | |
4066 | ||
2d66e025 | 4067 | // ------------ Right double click |
f85afd4e | 4068 | // |
2d66e025 MB |
4069 | else if ( event.RightDClick() ) |
4070 | { | |
4071 | col = XToCol(x); | |
b54ba671 | 4072 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) |
2d66e025 MB |
4073 | { |
4074 | // no default action at the moment | |
4075 | } | |
4076 | } | |
60ff3b99 VZ |
4077 | |
4078 | ||
2d66e025 | 4079 | // ------------ No buttons down and mouse moving |
f85afd4e | 4080 | // |
2d66e025 | 4081 | else if ( event.Moving() ) |
f85afd4e | 4082 | { |
2d66e025 MB |
4083 | m_dragRowOrCol = XToEdgeOfCol( x ); |
4084 | if ( m_dragRowOrCol >= 0 ) | |
f85afd4e | 4085 | { |
2d66e025 | 4086 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
f85afd4e | 4087 | { |
e2b42eeb | 4088 | // don't capture the cursor yet |
6e8524b1 MB |
4089 | if ( CanDragColSize() ) |
4090 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE); | |
f85afd4e | 4091 | } |
2d66e025 | 4092 | } |
6d004f67 | 4093 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 4094 | { |
e2b42eeb | 4095 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE); |
8f177c8e | 4096 | } |
f85afd4e MB |
4097 | } |
4098 | } | |
4099 | ||
4100 | ||
2d66e025 | 4101 | void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 4102 | { |
2d66e025 | 4103 | if ( event.LeftDown() ) |
f85afd4e | 4104 | { |
2d66e025 MB |
4105 | // indicate corner label by having both row and |
4106 | // col args == -1 | |
f85afd4e | 4107 | // |
b54ba671 | 4108 | if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) ) |
2d66e025 MB |
4109 | { |
4110 | SelectAll(); | |
4111 | } | |
f85afd4e MB |
4112 | } |
4113 | ||
2d66e025 MB |
4114 | else if ( event.LeftDClick() ) |
4115 | { | |
b54ba671 | 4116 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event ); |
2d66e025 | 4117 | } |
8f177c8e | 4118 | |
2d66e025 | 4119 | else if ( event.RightDown() ) |
f85afd4e | 4120 | { |
b54ba671 | 4121 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) ) |
f85afd4e | 4122 | { |
2d66e025 MB |
4123 | // no default action at the moment |
4124 | } | |
4125 | } | |
f85afd4e | 4126 | |
2d66e025 MB |
4127 | else if ( event.RightDClick() ) |
4128 | { | |
b54ba671 | 4129 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) ) |
2d66e025 MB |
4130 | { |
4131 | // no default action at the moment | |
4132 | } | |
4133 | } | |
4134 | } | |
f85afd4e | 4135 | |
e2b42eeb VZ |
4136 | void wxGrid::ChangeCursorMode(CursorMode mode, |
4137 | wxWindow *win, | |
4138 | bool captureMouse) | |
4139 | { | |
4140 | #ifdef __WXDEBUG__ | |
4141 | static const wxChar *cursorModes[] = | |
4142 | { | |
4143 | _T("SELECT_CELL"), | |
4144 | _T("RESIZE_ROW"), | |
4145 | _T("RESIZE_COL"), | |
4146 | _T("SELECT_ROW"), | |
4147 | _T("SELECT_COL") | |
4148 | }; | |
4149 | ||
181bfffd VZ |
4150 | wxLogTrace(_T("grid"), |
4151 | _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"), | |
e2b42eeb VZ |
4152 | win == m_colLabelWin ? _T("colLabelWin") |
4153 | : win ? _T("rowLabelWin") | |
4154 | : _T("gridWin"), | |
4155 | cursorModes[m_cursorMode], cursorModes[mode]); | |
4156 | #endif // __WXDEBUG__ | |
4157 | ||
4158 | if ( mode == m_cursorMode ) | |
4159 | return; | |
4160 | ||
4161 | if ( !win ) | |
4162 | { | |
4163 | // by default use the grid itself | |
4164 | win = m_gridWin; | |
4165 | } | |
4166 | ||
4167 | if ( m_winCapture ) | |
4168 | { | |
4169 | m_winCapture->ReleaseMouse(); | |
4170 | m_winCapture = (wxWindow *)NULL; | |
4171 | } | |
4172 | ||
4173 | m_cursorMode = mode; | |
4174 | ||
4175 | switch ( m_cursorMode ) | |
4176 | { | |
4177 | case WXGRID_CURSOR_RESIZE_ROW: | |
4178 | win->SetCursor( m_rowResizeCursor ); | |
4179 | break; | |
4180 | ||
4181 | case WXGRID_CURSOR_RESIZE_COL: | |
4182 | win->SetCursor( m_colResizeCursor ); | |
4183 | break; | |
4184 | ||
4185 | default: | |
4186 | win->SetCursor( *wxSTANDARD_CURSOR ); | |
4187 | } | |
4188 | ||
4189 | // we need to capture mouse when resizing | |
4190 | bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW || | |
4191 | m_cursorMode == WXGRID_CURSOR_RESIZE_COL; | |
4192 | ||
4193 | if ( captureMouse && resize ) | |
4194 | { | |
4195 | win->CaptureMouse(); | |
4196 | m_winCapture = win; | |
4197 | } | |
4198 | } | |
8f177c8e | 4199 | |
2d66e025 MB |
4200 | void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) |
4201 | { | |
4202 | int x, y; | |
4203 | wxPoint pos( event.GetPosition() ); | |
4204 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 4205 | |
2d66e025 MB |
4206 | wxGridCellCoords coords; |
4207 | XYToCell( x, y, coords ); | |
60ff3b99 | 4208 | |
2d66e025 MB |
4209 | if ( event.Dragging() ) |
4210 | { | |
07296f0b RD |
4211 | //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol()); |
4212 | ||
4213 | // Don't start doing anything until the mouse has been drug at | |
4214 | // least 3 pixels in any direction... | |
508011ce VZ |
4215 | if (! m_isDragging) |
4216 | { | |
4217 | if (m_startDragPos == wxDefaultPosition) | |
4218 | { | |
07296f0b RD |
4219 | m_startDragPos = pos; |
4220 | return; | |
4221 | } | |
4222 | if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4) | |
4223 | return; | |
4224 | } | |
4225 | ||
2d66e025 | 4226 | m_isDragging = TRUE; |
2d66e025 MB |
4227 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
4228 | { | |
f0102d2a VZ |
4229 | // Hide the edit control, so it |
4230 | // won't interfer with drag-shrinking. | |
4231 | if ( IsCellEditControlEnabled() ) | |
a5777624 | 4232 | { |
f0102d2a | 4233 | HideCellEditControl(); |
a5777624 RD |
4234 | SaveEditControlValue(); |
4235 | } | |
07296f0b RD |
4236 | |
4237 | // Have we captured the mouse yet? | |
508011ce VZ |
4238 | if (! m_winCapture) |
4239 | { | |
07296f0b RD |
4240 | m_winCapture = m_gridWin; |
4241 | m_winCapture->CaptureMouse(); | |
4242 | } | |
4243 | ||
2d66e025 MB |
4244 | if ( coords != wxGridNoCellCoords ) |
4245 | { | |
ca4f2b72 SN |
4246 | if ( event.ControlDown() ) |
4247 | { | |
4248 | if ( m_selectingKeyboard == wxGridNoCellCoords) | |
4249 | m_selectingKeyboard = coords; | |
4250 | SelectBlock ( m_selectingKeyboard, coords ); | |
4251 | } | |
4252 | else | |
4253 | { | |
4254 | if ( !IsSelection() ) | |
4255 | { | |
4256 | SelectBlock( coords, coords ); | |
4257 | } | |
4258 | else | |
4259 | { | |
4260 | SelectBlock( m_currentCellCoords, coords ); | |
4261 | } | |
f85afd4e | 4262 | } |
07296f0b | 4263 | |
508011ce VZ |
4264 | if (! IsVisible(coords)) |
4265 | { | |
07296f0b RD |
4266 | MakeCellVisible(coords); |
4267 | // TODO: need to introduce a delay or something here. The | |
4268 | // scrolling is way to fast, at least on MSW. | |
4269 | } | |
2d66e025 MB |
4270 | } |
4271 | } | |
6d004f67 MB |
4272 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) |
4273 | { | |
4274 | int cw, ch, left, dummy; | |
4275 | m_gridWin->GetClientSize( &cw, &ch ); | |
4276 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
8f177c8e | 4277 | |
6d004f67 MB |
4278 | wxClientDC dc( m_gridWin ); |
4279 | PrepareDC( dc ); | |
a95e38c0 VZ |
4280 | y = wxMax( y, GetRowTop(m_dragRowOrCol) + |
4281 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
6d004f67 MB |
4282 | dc.SetLogicalFunction(wxINVERT); |
4283 | if ( m_dragLastPos >= 0 ) | |
4284 | { | |
4285 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
4286 | } | |
4287 | dc.DrawLine( left, y, left+cw, y ); | |
4288 | m_dragLastPos = y; | |
4289 | } | |
4290 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
4291 | { | |
4292 | int cw, ch, dummy, top; | |
4293 | m_gridWin->GetClientSize( &cw, &ch ); | |
4294 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
e2b42eeb | 4295 | |
6d004f67 MB |
4296 | wxClientDC dc( m_gridWin ); |
4297 | PrepareDC( dc ); | |
43947979 VZ |
4298 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + |
4299 | GetColMinimalWidth(m_dragRowOrCol) ); | |
6d004f67 MB |
4300 | dc.SetLogicalFunction(wxINVERT); |
4301 | if ( m_dragLastPos >= 0 ) | |
4302 | { | |
4303 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
4304 | } | |
4305 | dc.DrawLine( x, top, x, top+ch ); | |
4306 | m_dragLastPos = x; | |
4307 | } | |
e2b42eeb | 4308 | |
2d66e025 MB |
4309 | return; |
4310 | } | |
66242c80 | 4311 | |
2d66e025 | 4312 | m_isDragging = FALSE; |
07296f0b RD |
4313 | m_startDragPos = wxDefaultPosition; |
4314 | ||
a5777624 RD |
4315 | // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL |
4316 | // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under | |
4317 | // wxGTK | |
e2b42eeb | 4318 | #if 0 |
a5777624 RD |
4319 | if ( event.Entering() || event.Leaving() ) |
4320 | { | |
4321 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
4322 | m_gridWin->SetCursor( *wxSTANDARD_CURSOR ); | |
4323 | } | |
4324 | else | |
e2b42eeb VZ |
4325 | #endif // 0 |
4326 | ||
a5777624 RD |
4327 | // ------------ Left button pressed |
4328 | // | |
4329 | if ( event.LeftDown() && coords != wxGridNoCellCoords ) | |
4330 | { | |
b5808881 SN |
4331 | if ( !event.ShiftDown() && !event.ControlDown() ) |
4332 | ClearSelection(); | |
a5777624 | 4333 | if ( event.ShiftDown() ) |
2d66e025 | 4334 | { |
5c8fc7c1 SN |
4335 | m_selection->SelectBlock( m_currentCellCoords.GetRow(), |
4336 | m_currentCellCoords.GetCol(), | |
4337 | coords.GetRow(), | |
d95b0c2b SN |
4338 | coords.GetCol(), |
4339 | event.ControlDown(), | |
4340 | event.ShiftDown(), | |
4341 | event.AltDown(), | |
4342 | event.MetaDown() ); | |
a5777624 RD |
4343 | } |
4344 | else if ( XToEdgeOfCol(x) < 0 && | |
4345 | YToEdgeOfRow(y) < 0 ) | |
4346 | { | |
4347 | if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK, | |
4348 | coords.GetRow(), | |
4349 | coords.GetCol(), | |
4350 | event ) ) | |
58dd5b3b | 4351 | { |
a5777624 RD |
4352 | DisableCellEditControl(); |
4353 | MakeCellVisible( coords ); | |
4354 | ||
4355 | // if this is the second click on this cell then start | |
4356 | // the edit control | |
4357 | if ( m_waitForSlowClick && | |
4358 | (coords == m_currentCellCoords) && | |
4359 | CanEnableCellControl()) | |
58dd5b3b | 4360 | { |
a5777624 | 4361 | EnableCellEditControl(); |
e195a54c | 4362 | |
a5777624 | 4363 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); |
0b190b0f VZ |
4364 | wxGridCellEditor *editor = attr->GetEditor(this, |
4365 | coords.GetRow(), | |
4366 | coords.GetCol()); | |
4367 | editor->StartingClick(); | |
4368 | editor->DecRef(); | |
a5777624 | 4369 | attr->DecRef(); |
e195a54c | 4370 | |
a5777624 RD |
4371 | m_waitForSlowClick = FALSE; |
4372 | } | |
4373 | else | |
4374 | { | |
ca4f2b72 SN |
4375 | if ( event.ControlDown() ) |
4376 | { | |
4377 | m_selection->ToggleCellSelection( coords.GetRow(), | |
4378 | coords.GetCol(), | |
4379 | event.ControlDown(), | |
4380 | event.ShiftDown(), | |
4381 | event.AltDown(), | |
4382 | event.MetaDown() ); | |
4383 | m_selectingTopLeft = wxGridNoCellCoords; | |
4384 | m_selectingBottomRight = wxGridNoCellCoords; | |
4385 | m_selectingKeyboard = coords; | |
4386 | } | |
4387 | else | |
4388 | SetCurrentCell( coords ); | |
a5777624 | 4389 | m_waitForSlowClick = TRUE; |
58dd5b3b MB |
4390 | } |
4391 | } | |
f85afd4e | 4392 | } |
a5777624 | 4393 | } |
f85afd4e | 4394 | |
f85afd4e | 4395 | |
a5777624 RD |
4396 | // ------------ Left double click |
4397 | // | |
4398 | else if ( event.LeftDClick() && coords != wxGridNoCellCoords ) | |
4399 | { | |
4400 | DisableCellEditControl(); | |
4401 | ||
4402 | if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 ) | |
58dd5b3b | 4403 | { |
a5777624 RD |
4404 | SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK, |
4405 | coords.GetRow(), | |
4406 | coords.GetCol(), | |
4407 | event ); | |
58dd5b3b | 4408 | } |
a5777624 | 4409 | } |
f85afd4e | 4410 | |
8f177c8e | 4411 | |
a5777624 RD |
4412 | // ------------ Left button released |
4413 | // | |
4414 | else if ( event.LeftUp() ) | |
4415 | { | |
4416 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
f85afd4e | 4417 | { |
b5808881 SN |
4418 | if ( m_selectingTopLeft != wxGridNoCellCoords && |
4419 | m_selectingBottomRight != wxGridNoCellCoords ) | |
52068ea5 | 4420 | { |
a5777624 | 4421 | if (m_winCapture) |
58dd5b3b | 4422 | { |
a5777624 RD |
4423 | m_winCapture->ReleaseMouse(); |
4424 | m_winCapture = NULL; | |
58dd5b3b | 4425 | } |
5c8fc7c1 SN |
4426 | m_selection->SelectBlock( m_selectingTopLeft.GetRow(), |
4427 | m_selectingTopLeft.GetCol(), | |
4428 | m_selectingBottomRight.GetRow(), | |
4429 | m_selectingBottomRight.GetCol(), | |
d95b0c2b SN |
4430 | event.ControlDown(), |
4431 | event.ShiftDown(), | |
4432 | event.AltDown(), | |
4433 | event.MetaDown() ); | |
5c8fc7c1 SN |
4434 | m_selectingTopLeft = wxGridNoCellCoords; |
4435 | m_selectingBottomRight = wxGridNoCellCoords; | |
6d004f67 | 4436 | } |
2d66e025 | 4437 | |
a5777624 RD |
4438 | // Show the edit control, if it has been hidden for |
4439 | // drag-shrinking. | |
4440 | ShowCellEditControl(); | |
4441 | } | |
4442 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
4443 | { | |
4444 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
4445 | DoEndDragResizeRow(); | |
e2b42eeb | 4446 | |
a5777624 RD |
4447 | // Note: we are ending the event *after* doing |
4448 | // default processing in this case | |
4449 | // | |
4450 | SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); | |
4451 | } | |
4452 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
4453 | { | |
4454 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
4455 | DoEndDragResizeCol(); | |
e2b42eeb | 4456 | |
a5777624 RD |
4457 | // Note: we are ending the event *after* doing |
4458 | // default processing in this case | |
4459 | // | |
4460 | SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); | |
58dd5b3b | 4461 | } |
f85afd4e | 4462 | |
a5777624 RD |
4463 | m_dragLastPos = -1; |
4464 | } | |
4465 | ||
f85afd4e | 4466 | |
a5777624 RD |
4467 | // ------------ Right button down |
4468 | // | |
4469 | else if ( event.RightDown() && coords != wxGridNoCellCoords ) | |
4470 | { | |
4471 | DisableCellEditControl(); | |
4472 | if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK, | |
4473 | coords.GetRow(), | |
4474 | coords.GetCol(), | |
4475 | event ) ) | |
f85afd4e | 4476 | { |
a5777624 | 4477 | // no default action at the moment |
60ff3b99 | 4478 | } |
a5777624 | 4479 | } |
2d66e025 | 4480 | |
60ff3b99 | 4481 | |
a5777624 RD |
4482 | // ------------ Right double click |
4483 | // | |
4484 | else if ( event.RightDClick() && coords != wxGridNoCellCoords ) | |
4485 | { | |
4486 | DisableCellEditControl(); | |
4487 | if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK, | |
4488 | coords.GetRow(), | |
4489 | coords.GetCol(), | |
4490 | event ) ) | |
f85afd4e | 4491 | { |
a5777624 | 4492 | // no default action at the moment |
60ff3b99 | 4493 | } |
a5777624 RD |
4494 | } |
4495 | ||
4496 | // ------------ Moving and no button action | |
4497 | // | |
4498 | else if ( event.Moving() && !event.IsButton() ) | |
4499 | { | |
4500 | int dragRow = YToEdgeOfRow( y ); | |
4501 | int dragCol = XToEdgeOfCol( x ); | |
790cc417 | 4502 | |
a5777624 RD |
4503 | // Dragging on the corner of a cell to resize in both |
4504 | // directions is not implemented yet... | |
790cc417 | 4505 | // |
a5777624 | 4506 | if ( dragRow >= 0 && dragCol >= 0 ) |
790cc417 | 4507 | { |
a5777624 RD |
4508 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); |
4509 | return; | |
4510 | } | |
6d004f67 | 4511 | |
a5777624 RD |
4512 | if ( dragRow >= 0 ) |
4513 | { | |
4514 | m_dragRowOrCol = dragRow; | |
6d004f67 | 4515 | |
a5777624 | 4516 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
6d004f67 | 4517 | { |
a5777624 RD |
4518 | if ( CanDragRowSize() && CanDragGridSize() ) |
4519 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW); | |
6d004f67 | 4520 | } |
e2b42eeb | 4521 | |
b94ae1ea VZ |
4522 | if ( dragCol >= 0 ) |
4523 | { | |
4524 | m_dragRowOrCol = dragCol; | |
122603f1 | 4525 | } |
b94ae1ea | 4526 | |
a5777624 RD |
4527 | return; |
4528 | } | |
e2b42eeb | 4529 | |
a5777624 RD |
4530 | if ( dragCol >= 0 ) |
4531 | { | |
4532 | m_dragRowOrCol = dragCol; | |
e2b42eeb | 4533 | |
a5777624 | 4534 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
6d004f67 | 4535 | { |
a5777624 RD |
4536 | if ( CanDragColSize() && CanDragGridSize() ) |
4537 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL); | |
6d004f67 | 4538 | } |
a5777624 RD |
4539 | |
4540 | return; | |
6d004f67 | 4541 | } |
a5777624 RD |
4542 | |
4543 | // Neither on a row or col edge | |
4544 | // | |
4545 | if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) | |
4546 | { | |
4547 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
4548 | } | |
4549 | } | |
6d004f67 MB |
4550 | } |
4551 | ||
4552 | ||
4553 | void wxGrid::DoEndDragResizeRow() | |
4554 | { | |
4555 | if ( m_dragLastPos >= 0 ) | |
4556 | { | |
4557 | // erase the last line and resize the row | |
4558 | // | |
4559 | int cw, ch, left, dummy; | |
4560 | m_gridWin->GetClientSize( &cw, &ch ); | |
4561 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
4562 | ||
4563 | wxClientDC dc( m_gridWin ); | |
4564 | PrepareDC( dc ); | |
4565 | dc.SetLogicalFunction( wxINVERT ); | |
4566 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
4567 | HideCellEditControl(); | |
a5777624 | 4568 | SaveEditControlValue(); |
6d004f67 | 4569 | |
7c1cb261 | 4570 | int rowTop = GetRowTop(m_dragRowOrCol); |
6d004f67 MB |
4571 | SetRowSize( m_dragRowOrCol, |
4572 | wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) ); | |
e2b42eeb | 4573 | |
6d004f67 MB |
4574 | if ( !GetBatchCount() ) |
4575 | { | |
4576 | // Only needed to get the correct rect.y: | |
4577 | wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) ); | |
4578 | rect.x = 0; | |
4579 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
4580 | rect.width = m_rowLabelWidth; | |
4581 | rect.height = ch - rect.y; | |
4582 | m_rowLabelWin->Refresh( TRUE, &rect ); | |
4583 | rect.width = cw; | |
4584 | m_gridWin->Refresh( FALSE, &rect ); | |
4585 | } | |
4586 | ||
4587 | ShowCellEditControl(); | |
4588 | } | |
4589 | } | |
4590 | ||
4591 | ||
4592 | void wxGrid::DoEndDragResizeCol() | |
4593 | { | |
4594 | if ( m_dragLastPos >= 0 ) | |
4595 | { | |
4596 | // erase the last line and resize the col | |
4597 | // | |
4598 | int cw, ch, dummy, top; | |
4599 | m_gridWin->GetClientSize( &cw, &ch ); | |
4600 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
4601 | ||
4602 | wxClientDC dc( m_gridWin ); | |
4603 | PrepareDC( dc ); | |
4604 | dc.SetLogicalFunction( wxINVERT ); | |
4605 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
4606 | HideCellEditControl(); | |
a5777624 | 4607 | SaveEditControlValue(); |
6d004f67 | 4608 | |
7c1cb261 | 4609 | int colLeft = GetColLeft(m_dragRowOrCol); |
6d004f67 | 4610 | SetColSize( m_dragRowOrCol, |
43947979 VZ |
4611 | wxMax( m_dragLastPos - colLeft, |
4612 | GetColMinimalWidth(m_dragRowOrCol) ) ); | |
6d004f67 MB |
4613 | |
4614 | if ( !GetBatchCount() ) | |
4615 | { | |
4616 | // Only needed to get the correct rect.x: | |
4617 | wxRect rect ( CellToRect( 0, m_dragRowOrCol ) ); | |
4618 | rect.y = 0; | |
4619 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
4620 | rect.width = cw - rect.x; | |
4621 | rect.height = m_colLabelHeight; | |
4622 | m_colLabelWin->Refresh( TRUE, &rect ); | |
4623 | rect.height = ch; | |
4624 | m_gridWin->Refresh( FALSE, &rect ); | |
790cc417 | 4625 | } |
e2b42eeb | 4626 | |
6d004f67 | 4627 | ShowCellEditControl(); |
f85afd4e | 4628 | } |
f85afd4e MB |
4629 | } |
4630 | ||
4631 | ||
6d004f67 | 4632 | |
2d66e025 MB |
4633 | // |
4634 | // ------ interaction with data model | |
4635 | // | |
4636 | bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg ) | |
f85afd4e | 4637 | { |
2d66e025 | 4638 | switch ( msg.GetId() ) |
17732cec | 4639 | { |
2d66e025 MB |
4640 | case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES: |
4641 | return GetModelValues(); | |
17732cec | 4642 | |
2d66e025 MB |
4643 | case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES: |
4644 | return SetModelValues(); | |
f85afd4e | 4645 | |
2d66e025 MB |
4646 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: |
4647 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
4648 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
4649 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
4650 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
4651 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
4652 | return Redimension( msg ); | |
4653 | ||
4654 | default: | |
4655 | return FALSE; | |
f85afd4e | 4656 | } |
2d66e025 | 4657 | } |
f85afd4e | 4658 | |
f85afd4e | 4659 | |
f85afd4e | 4660 | |
2d66e025 MB |
4661 | // The behaviour of this function depends on the grid table class |
4662 | // Clear() function. For the default wxGridStringTable class the | |
4663 | // behavious is to replace all cell contents with wxEmptyString but | |
4664 | // not to change the number of rows or cols. | |
4665 | // | |
4666 | void wxGrid::ClearGrid() | |
4667 | { | |
4668 | if ( m_table ) | |
f85afd4e | 4669 | { |
4cfa5de6 RD |
4670 | if (IsCellEditControlEnabled()) |
4671 | DisableCellEditControl(); | |
4672 | ||
2d66e025 | 4673 | m_table->Clear(); |
1f1ce288 | 4674 | if ( !GetBatchCount() ) m_gridWin->Refresh(); |
f85afd4e MB |
4675 | } |
4676 | } | |
4677 | ||
4678 | ||
2d66e025 | 4679 | bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) |
f85afd4e | 4680 | { |
2d66e025 | 4681 | // TODO: something with updateLabels flag |
8f177c8e | 4682 | |
2d66e025 | 4683 | if ( !m_created ) |
f85afd4e | 4684 | { |
bd3c6758 | 4685 | wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") ); |
2d66e025 MB |
4686 | return FALSE; |
4687 | } | |
8f177c8e | 4688 | |
2d66e025 MB |
4689 | if ( m_table ) |
4690 | { | |
b7fff980 | 4691 | if (IsCellEditControlEnabled()) |
b54ba671 | 4692 | DisableCellEditControl(); |
b7fff980 | 4693 | |
2d66e025 | 4694 | bool ok = m_table->InsertRows( pos, numRows ); |
f85afd4e | 4695 | |
2d66e025 MB |
4696 | // the table will have sent the results of the insert row |
4697 | // operation to this view object as a grid table message | |
4698 | // | |
4699 | if ( ok ) | |
4700 | { | |
4701 | if ( m_numCols == 0 ) | |
f85afd4e | 4702 | { |
2d66e025 MB |
4703 | m_table->AppendCols( WXGRID_DEFAULT_NUMBER_COLS ); |
4704 | // | |
4705 | // TODO: perhaps instead of appending the default number of cols | |
4706 | // we should remember what the last non-zero number of cols was ? | |
4707 | // | |
4708 | } | |
8f177c8e | 4709 | |
2d66e025 MB |
4710 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
4711 | { | |
4712 | // if we have just inserted cols into an empty grid the current | |
4713 | // cell will be undefined... | |
4714 | // | |
4715 | SetCurrentCell( 0, 0 ); | |
f85afd4e MB |
4716 | } |
4717 | ||
b5808881 | 4718 | m_selection->UpdateRows( pos, numRows ); |
2d66e025 | 4719 | if ( !GetBatchCount() ) Refresh(); |
f85afd4e | 4720 | } |
2d66e025 | 4721 | |
2d66e025 MB |
4722 | return ok; |
4723 | } | |
4724 | else | |
4725 | { | |
4726 | return FALSE; | |
f85afd4e MB |
4727 | } |
4728 | } | |
4729 | ||
4730 | ||
2d66e025 | 4731 | bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) ) |
f85afd4e | 4732 | { |
2d66e025 MB |
4733 | // TODO: something with updateLabels flag |
4734 | ||
4735 | if ( !m_created ) | |
f85afd4e | 4736 | { |
bd3c6758 | 4737 | wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") ); |
2d66e025 MB |
4738 | return FALSE; |
4739 | } | |
4740 | ||
4741 | if ( m_table && m_table->AppendRows( numRows ) ) | |
4742 | { | |
4743 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4744 | { | |
4745 | // if we have just inserted cols into an empty grid the current | |
4746 | // cell will be undefined... | |
4747 | // | |
4748 | SetCurrentCell( 0, 0 ); | |
4749 | } | |
4750 | ||
4751 | // the table will have sent the results of the append row | |
4752 | // operation to this view object as a grid table message | |
4753 | // | |
2d66e025 MB |
4754 | if ( !GetBatchCount() ) Refresh(); |
4755 | return TRUE; | |
4756 | } | |
4757 | else | |
4758 | { | |
4759 | return FALSE; | |
f85afd4e MB |
4760 | } |
4761 | } | |
4762 | ||
2d66e025 MB |
4763 | |
4764 | bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 4765 | { |
2d66e025 MB |
4766 | // TODO: something with updateLabels flag |
4767 | ||
4768 | if ( !m_created ) | |
f85afd4e | 4769 | { |
bd3c6758 | 4770 | wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") ); |
2d66e025 MB |
4771 | return FALSE; |
4772 | } | |
4773 | ||
b7fff980 | 4774 | if ( m_table ) |
2d66e025 | 4775 | { |
b7fff980 | 4776 | if (IsCellEditControlEnabled()) |
b54ba671 | 4777 | DisableCellEditControl(); |
f85afd4e | 4778 | |
b7fff980 RD |
4779 | if (m_table->DeleteRows( pos, numRows )) |
4780 | { | |
4781 | ||
4782 | // the table will have sent the results of the delete row | |
4783 | // operation to this view object as a grid table message | |
4784 | // | |
b5808881 | 4785 | m_selection->UpdateRows( pos, -((int)numRows) ); |
b7fff980 RD |
4786 | if ( !GetBatchCount() ) Refresh(); |
4787 | return TRUE; | |
4788 | } | |
2d66e025 | 4789 | } |
b7fff980 | 4790 | return FALSE; |
2d66e025 | 4791 | } |
f85afd4e | 4792 | |
f85afd4e | 4793 | |
2d66e025 MB |
4794 | bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) |
4795 | { | |
4796 | // TODO: something with updateLabels flag | |
8f177c8e | 4797 | |
2d66e025 MB |
4798 | if ( !m_created ) |
4799 | { | |
bd3c6758 | 4800 | wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") ); |
2d66e025 MB |
4801 | return FALSE; |
4802 | } | |
f85afd4e | 4803 | |
2d66e025 MB |
4804 | if ( m_table ) |
4805 | { | |
b7fff980 | 4806 | if (IsCellEditControlEnabled()) |
b54ba671 | 4807 | DisableCellEditControl(); |
b7fff980 | 4808 | |
2d66e025 MB |
4809 | bool ok = m_table->InsertCols( pos, numCols ); |
4810 | ||
4811 | // the table will have sent the results of the insert col | |
4812 | // operation to this view object as a grid table message | |
4813 | // | |
4814 | if ( ok ) | |
f85afd4e | 4815 | { |
2d66e025 | 4816 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
f85afd4e | 4817 | { |
2d66e025 MB |
4818 | // if we have just inserted cols into an empty grid the current |
4819 | // cell will be undefined... | |
4820 | // | |
4821 | SetCurrentCell( 0, 0 ); | |
f85afd4e | 4822 | } |
2d66e025 | 4823 | |
b5808881 | 4824 | m_selection->UpdateCols( pos, numCols ); |
2d66e025 | 4825 | if ( !GetBatchCount() ) Refresh(); |
f85afd4e | 4826 | } |
2d66e025 | 4827 | |
2d66e025 MB |
4828 | return ok; |
4829 | } | |
4830 | else | |
4831 | { | |
4832 | return FALSE; | |
f85afd4e MB |
4833 | } |
4834 | } | |
4835 | ||
2d66e025 MB |
4836 | |
4837 | bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 4838 | { |
2d66e025 MB |
4839 | // TODO: something with updateLabels flag |
4840 | ||
4841 | if ( !m_created ) | |
f85afd4e | 4842 | { |
bd3c6758 | 4843 | wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") ); |
2d66e025 MB |
4844 | return FALSE; |
4845 | } | |
f85afd4e | 4846 | |
2d66e025 MB |
4847 | if ( m_table && m_table->AppendCols( numCols ) ) |
4848 | { | |
4849 | // the table will have sent the results of the append col | |
4850 | // operation to this view object as a grid table message | |
4851 | // | |
4852 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
f85afd4e | 4853 | { |
2d66e025 MB |
4854 | // if we have just inserted cols into an empty grid the current |
4855 | // cell will be undefined... | |
4856 | // | |
4857 | SetCurrentCell( 0, 0 ); | |
f85afd4e | 4858 | } |
8f177c8e | 4859 | |
2d66e025 MB |
4860 | if ( !GetBatchCount() ) Refresh(); |
4861 | return TRUE; | |
f85afd4e | 4862 | } |
2d66e025 | 4863 | else |
f85afd4e | 4864 | { |
2d66e025 | 4865 | return FALSE; |
f85afd4e | 4866 | } |
f85afd4e MB |
4867 | } |
4868 | ||
4869 | ||
2d66e025 | 4870 | bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) |
f85afd4e | 4871 | { |
2d66e025 | 4872 | // TODO: something with updateLabels flag |
8f177c8e | 4873 | |
2d66e025 | 4874 | if ( !m_created ) |
f85afd4e | 4875 | { |
bd3c6758 | 4876 | wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") ); |
2d66e025 | 4877 | return FALSE; |
f85afd4e MB |
4878 | } |
4879 | ||
b7fff980 | 4880 | if ( m_table ) |
2d66e025 | 4881 | { |
b7fff980 | 4882 | if (IsCellEditControlEnabled()) |
b54ba671 | 4883 | DisableCellEditControl(); |
8f177c8e | 4884 | |
b7fff980 RD |
4885 | if ( m_table->DeleteCols( pos, numCols ) ) |
4886 | { | |
4887 | // the table will have sent the results of the delete col | |
4888 | // operation to this view object as a grid table message | |
4889 | // | |
b5808881 | 4890 | m_selection->UpdateCols( pos, -((int)numCols) ); |
b7fff980 RD |
4891 | if ( !GetBatchCount() ) Refresh(); |
4892 | return TRUE; | |
4893 | } | |
f85afd4e | 4894 | } |
b7fff980 | 4895 | return FALSE; |
f85afd4e MB |
4896 | } |
4897 | ||
4898 | ||
2d66e025 MB |
4899 | |
4900 | // | |
4901 | // ----- event handlers | |
f85afd4e | 4902 | // |
8f177c8e | 4903 | |
2d66e025 MB |
4904 | // Generate a grid event based on a mouse event and |
4905 | // return the result of ProcessEvent() | |
4906 | // | |
4907 | bool wxGrid::SendEvent( const wxEventType type, | |
4908 | int row, int col, | |
4909 | wxMouseEvent& mouseEv ) | |
4910 | { | |
b54ba671 | 4911 | if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE ) |
f85afd4e | 4912 | { |
2d66e025 MB |
4913 | int rowOrCol = (row == -1 ? col : row); |
4914 | ||
4915 | wxGridSizeEvent gridEvt( GetId(), | |
4916 | type, | |
4917 | this, | |
4918 | rowOrCol, | |
4919 | mouseEv.GetX(), mouseEv.GetY(), | |
4920 | mouseEv.ControlDown(), | |
4921 | mouseEv.ShiftDown(), | |
4922 | mouseEv.AltDown(), | |
4923 | mouseEv.MetaDown() ); | |
4924 | ||
4925 | return GetEventHandler()->ProcessEvent(gridEvt); | |
f85afd4e | 4926 | } |
b54ba671 | 4927 | else if ( type == wxEVT_GRID_RANGE_SELECT ) |
2d66e025 | 4928 | { |
5c8fc7c1 | 4929 | // Right now, it should _never_ end up here! |
2d66e025 MB |
4930 | wxGridRangeSelectEvent gridEvt( GetId(), |
4931 | type, | |
4932 | this, | |
b5808881 SN |
4933 | m_selectingTopLeft, |
4934 | m_selectingBottomRight, | |
5c8fc7c1 | 4935 | TRUE, |
2d66e025 MB |
4936 | mouseEv.ControlDown(), |
4937 | mouseEv.ShiftDown(), | |
4938 | mouseEv.AltDown(), | |
4939 | mouseEv.MetaDown() ); | |
f85afd4e | 4940 | |
2d66e025 MB |
4941 | return GetEventHandler()->ProcessEvent(gridEvt); |
4942 | } | |
4943 | else | |
4944 | { | |
4945 | wxGridEvent gridEvt( GetId(), | |
4946 | type, | |
4947 | this, | |
4948 | row, col, | |
d95b0c2b | 4949 | FALSE, |
2d66e025 MB |
4950 | mouseEv.GetX(), mouseEv.GetY(), |
4951 | mouseEv.ControlDown(), | |
4952 | mouseEv.ShiftDown(), | |
4953 | mouseEv.AltDown(), | |
4954 | mouseEv.MetaDown() ); | |
8f177c8e | 4955 | |
2d66e025 MB |
4956 | return GetEventHandler()->ProcessEvent(gridEvt); |
4957 | } | |
f85afd4e MB |
4958 | } |
4959 | ||
4960 | ||
2d66e025 MB |
4961 | // Generate a grid event of specified type and return the result |
4962 | // of ProcessEvent(). | |
f85afd4e | 4963 | // |
2d66e025 MB |
4964 | bool wxGrid::SendEvent( const wxEventType type, |
4965 | int row, int col ) | |
f85afd4e | 4966 | { |
b54ba671 | 4967 | if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE ) |
f85afd4e | 4968 | { |
2d66e025 | 4969 | int rowOrCol = (row == -1 ? col : row); |
f85afd4e | 4970 | |
2d66e025 MB |
4971 | wxGridSizeEvent gridEvt( GetId(), |
4972 | type, | |
4973 | this, | |
4974 | rowOrCol ); | |
4975 | ||
4976 | return GetEventHandler()->ProcessEvent(gridEvt); | |
4977 | } | |
4978 | else | |
4979 | { | |
4980 | wxGridEvent gridEvt( GetId(), | |
4981 | type, | |
4982 | this, | |
4983 | row, col ); | |
8f177c8e | 4984 | |
2d66e025 MB |
4985 | return GetEventHandler()->ProcessEvent(gridEvt); |
4986 | } | |
f85afd4e MB |
4987 | } |
4988 | ||
4989 | ||
2d66e025 | 4990 | void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
f85afd4e | 4991 | { |
2d66e025 | 4992 | wxPaintDC dc( this ); |
8f177c8e | 4993 | } |
f85afd4e MB |
4994 | |
4995 | ||
18f9565d MB |
4996 | // This is just here to make sure that CalcDimensions gets called when |
4997 | // the grid view is resized... then the size event is skipped to allow | |
4998 | // the box sizers to handle everything | |
4999 | // | |
2d66e025 | 5000 | void wxGrid::OnSize( wxSizeEvent& event ) |
f85afd4e | 5001 | { |
7807d81c | 5002 | CalcWindowSizes(); |
2d66e025 | 5003 | CalcDimensions(); |
f85afd4e MB |
5004 | } |
5005 | ||
f85afd4e | 5006 | |
2d66e025 | 5007 | void wxGrid::OnKeyDown( wxKeyEvent& event ) |
f85afd4e | 5008 | { |
2d66e025 | 5009 | if ( m_inOnKeyDown ) |
f85afd4e | 5010 | { |
2d66e025 MB |
5011 | // shouldn't be here - we are going round in circles... |
5012 | // | |
07296f0b | 5013 | wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") ); |
f85afd4e MB |
5014 | } |
5015 | ||
2d66e025 | 5016 | m_inOnKeyDown = TRUE; |
f85afd4e | 5017 | |
2d66e025 MB |
5018 | // propagate the event up and see if it gets processed |
5019 | // | |
5020 | wxWindow *parent = GetParent(); | |
5021 | wxKeyEvent keyEvt( event ); | |
5022 | keyEvt.SetEventObject( parent ); | |
5023 | ||
5024 | if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) ) | |
f85afd4e | 5025 | { |
9b4aede2 | 5026 | |
2d66e025 MB |
5027 | // try local handlers |
5028 | // | |
d95b0c2b SN |
5029 | if ( !event.ShiftDown() && |
5030 | m_selectingKeyboard != wxGridNoCellCoords ) | |
5031 | { | |
6b105754 SN |
5032 | if ( m_selectingTopLeft != wxGridNoCellCoords && |
5033 | m_selectingBottomRight != wxGridNoCellCoords ) | |
5034 | m_selection->SelectBlock( m_selectingTopLeft.GetRow(), | |
5035 | m_selectingTopLeft.GetCol(), | |
5036 | m_selectingBottomRight.GetRow(), | |
5037 | m_selectingBottomRight.GetCol(), | |
5038 | event.ControlDown(), | |
5039 | event.ShiftDown(), | |
5040 | event.AltDown(), | |
5041 | event.MetaDown() ); | |
d95b0c2b SN |
5042 | m_selectingTopLeft = wxGridNoCellCoords; |
5043 | m_selectingBottomRight = wxGridNoCellCoords; | |
5044 | m_selectingKeyboard = wxGridNoCellCoords; | |
5045 | } | |
5046 | ||
2d66e025 MB |
5047 | switch ( event.KeyCode() ) |
5048 | { | |
5049 | case WXK_UP: | |
5050 | if ( event.ControlDown() ) | |
5051 | { | |
5c8fc7c1 | 5052 | MoveCursorUpBlock( event.ShiftDown() ); |
2d66e025 MB |
5053 | } |
5054 | else | |
5055 | { | |
5c8fc7c1 | 5056 | MoveCursorUp( event.ShiftDown() ); |
2d66e025 MB |
5057 | } |
5058 | break; | |
f85afd4e | 5059 | |
2d66e025 MB |
5060 | case WXK_DOWN: |
5061 | if ( event.ControlDown() ) | |
5062 | { | |
5c8fc7c1 | 5063 | MoveCursorDownBlock( event.ShiftDown() ); |
2d66e025 MB |
5064 | } |
5065 | else | |
5066 | { | |
5c8fc7c1 | 5067 | MoveCursorDown( event.ShiftDown() ); |
2d66e025 MB |
5068 | } |
5069 | break; | |
8f177c8e | 5070 | |
2d66e025 MB |
5071 | case WXK_LEFT: |
5072 | if ( event.ControlDown() ) | |
5073 | { | |
5c8fc7c1 | 5074 | MoveCursorLeftBlock( event.ShiftDown() ); |
2d66e025 MB |
5075 | } |
5076 | else | |
5077 | { | |
5c8fc7c1 | 5078 | MoveCursorLeft( event.ShiftDown() ); |
2d66e025 MB |
5079 | } |
5080 | break; | |
5081 | ||
5082 | case WXK_RIGHT: | |
5083 | if ( event.ControlDown() ) | |
5084 | { | |
5c8fc7c1 | 5085 | MoveCursorRightBlock( event.ShiftDown() ); |
2d66e025 MB |
5086 | } |
5087 | else | |
5088 | { | |
5c8fc7c1 | 5089 | MoveCursorRight( event.ShiftDown() ); |
2d66e025 MB |
5090 | } |
5091 | break; | |
b99be8fb | 5092 | |
2d66e025 | 5093 | case WXK_RETURN: |
58dd5b3b MB |
5094 | if ( event.ControlDown() ) |
5095 | { | |
5096 | event.Skip(); // to let the edit control have the return | |
5097 | } | |
5098 | else | |
5099 | { | |
5c8fc7c1 | 5100 | MoveCursorDown( event.ShiftDown() ); |
58dd5b3b | 5101 | } |
2d66e025 MB |
5102 | break; |
5103 | ||
5c8fc7c1 SN |
5104 | case WXK_ESCAPE: |
5105 | m_selection->ClearSelection(); | |
5106 | break; | |
5107 | ||
2c9a89e0 RD |
5108 | case WXK_TAB: |
5109 | if (event.ShiftDown()) | |
5c8fc7c1 | 5110 | MoveCursorLeft( FALSE ); |
2c9a89e0 | 5111 | else |
5c8fc7c1 | 5112 | MoveCursorRight( FALSE ); |
2c9a89e0 RD |
5113 | break; |
5114 | ||
2d66e025 MB |
5115 | case WXK_HOME: |
5116 | if ( event.ControlDown() ) | |
5117 | { | |
5118 | MakeCellVisible( 0, 0 ); | |
5119 | SetCurrentCell( 0, 0 ); | |
5120 | } | |
5121 | else | |
5122 | { | |
5123 | event.Skip(); | |
5124 | } | |
5125 | break; | |
5126 | ||
5127 | case WXK_END: | |
5128 | if ( event.ControlDown() ) | |
5129 | { | |
5130 | MakeCellVisible( m_numRows-1, m_numCols-1 ); | |
5131 | SetCurrentCell( m_numRows-1, m_numCols-1 ); | |
5132 | } | |
5133 | else | |
5134 | { | |
5135 | event.Skip(); | |
5136 | } | |
5137 | break; | |
5138 | ||
5139 | case WXK_PRIOR: | |
5140 | MovePageUp(); | |
5141 | break; | |
5142 | ||
5143 | case WXK_NEXT: | |
5144 | MovePageDown(); | |
5145 | break; | |
5146 | ||
07296f0b | 5147 | case WXK_SPACE: |
6b105754 SN |
5148 | if ( event.ControlDown() ) |
5149 | { | |
5150 | m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(), | |
5151 | m_currentCellCoords.GetCol(), | |
5152 | event.ControlDown(), | |
5153 | event.ShiftDown(), | |
5154 | event.AltDown(), | |
5155 | event.MetaDown() ); | |
5156 | break; | |
5157 | } | |
07296f0b RD |
5158 | if ( !IsEditable() ) |
5159 | { | |
5c8fc7c1 | 5160 | MoveCursorRight( FALSE ); |
07296f0b RD |
5161 | break; |
5162 | } | |
5163 | // Otherwise fall through to default | |
5164 | ||
2d66e025 | 5165 | default: |
c239a4bb VZ |
5166 | // alphanumeric keys or F2 (special key just for this) enable |
5167 | // the cell edit control | |
5c8fc7c1 SN |
5168 | // On just Shift/Control I get values for event.KeyCode() |
5169 | // that are outside the range where isalnum's behaviour is | |
5170 | // well defined, so do an additional sanity check. | |
b94ae1ea VZ |
5171 | if ( !(event.AltDown() || |
5172 | event.MetaDown() || | |
5173 | event.ControlDown()) && | |
5c8fc7c1 SN |
5174 | ((isalnum(event.KeyCode()) && |
5175 | (event.KeyCode() < 256 && event.KeyCode() >= 0)) || | |
5176 | event.KeyCode() == WXK_F2) && | |
b94ae1ea VZ |
5177 | !IsCellEditControlEnabled() && |
5178 | CanEnableCellControl() ) | |
b54ba671 VZ |
5179 | { |
5180 | EnableCellEditControl(); | |
f2d76237 RD |
5181 | int row = m_currentCellCoords.GetRow(); |
5182 | int col = m_currentCellCoords.GetCol(); | |
5183 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
0b190b0f VZ |
5184 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
5185 | editor->StartingKey(event); | |
5186 | editor->DecRef(); | |
2c9a89e0 RD |
5187 | attr->DecRef(); |
5188 | } | |
b3a7510d VZ |
5189 | else |
5190 | { | |
b94ae1ea VZ |
5191 | // let others process char events with modifiers or all |
5192 | // char events for readonly cells | |
b3a7510d VZ |
5193 | event.Skip(); |
5194 | } | |
025562fe | 5195 | break; |
2d66e025 | 5196 | } |
f85afd4e MB |
5197 | } |
5198 | ||
2d66e025 | 5199 | m_inOnKeyDown = FALSE; |
f85afd4e MB |
5200 | } |
5201 | ||
2796cce3 | 5202 | void wxGrid::OnEraseBackground(wxEraseEvent&) |
508011ce VZ |
5203 | { |
5204 | } | |
07296f0b | 5205 | |
2d66e025 | 5206 | void wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) |
66242c80 | 5207 | { |
2a7750d9 | 5208 | if ( m_currentCellCoords != wxGridNoCellCoords ) |
2d66e025 | 5209 | { |
2d66e025 | 5210 | HideCellEditControl(); |
b54ba671 | 5211 | DisableCellEditControl(); |
07296f0b RD |
5212 | |
5213 | // Clear the old current cell highlight | |
5214 | wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords); | |
d1c0b4f9 VZ |
5215 | |
5216 | // Otherwise refresh redraws the highlight! | |
5217 | m_currentCellCoords = coords; | |
5218 | ||
07296f0b | 5219 | m_gridWin->Refresh( FALSE, &r ); |
66242c80 | 5220 | } |
8f177c8e | 5221 | |
2d66e025 MB |
5222 | m_currentCellCoords = coords; |
5223 | ||
2a7750d9 MB |
5224 | wxClientDC dc(m_gridWin); |
5225 | PrepareDC(dc); | |
5226 | ||
5227 | wxGridCellAttr* attr = GetCellAttr(coords); | |
5228 | DrawCellHighlight(dc, attr); | |
5229 | attr->DecRef(); | |
283b7808 | 5230 | |
b5808881 SN |
5231 | #if 0 |
5232 | // SN: For my extended selection code, automatic | |
5233 | // deselection is definitely not a good idea. | |
4634a5d6 MB |
5234 | if ( IsSelection() ) |
5235 | { | |
5236 | wxRect r( SelectionToDeviceRect() ); | |
5237 | ClearSelection(); | |
5238 | if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r ); | |
5239 | } | |
b5808881 | 5240 | #endif |
66242c80 MB |
5241 | } |
5242 | ||
2d66e025 MB |
5243 | |
5244 | // | |
5245 | // ------ functions to get/send data (see also public functions) | |
5246 | // | |
5247 | ||
5248 | bool wxGrid::GetModelValues() | |
66242c80 | 5249 | { |
2d66e025 | 5250 | if ( m_table ) |
66242c80 | 5251 | { |
2d66e025 | 5252 | // all we need to do is repaint the grid |
66242c80 | 5253 | // |
2d66e025 | 5254 | m_gridWin->Refresh(); |
66242c80 MB |
5255 | return TRUE; |
5256 | } | |
8f177c8e | 5257 | |
66242c80 MB |
5258 | return FALSE; |
5259 | } | |
5260 | ||
2d66e025 MB |
5261 | |
5262 | bool wxGrid::SetModelValues() | |
f85afd4e | 5263 | { |
2d66e025 | 5264 | int row, col; |
8f177c8e | 5265 | |
2d66e025 MB |
5266 | if ( m_table ) |
5267 | { | |
5268 | for ( row = 0; row < m_numRows; row++ ) | |
f85afd4e | 5269 | { |
2d66e025 | 5270 | for ( col = 0; col < m_numCols; col++ ) |
f85afd4e | 5271 | { |
2d66e025 | 5272 | m_table->SetValue( row, col, GetCellValue(row, col) ); |
f85afd4e MB |
5273 | } |
5274 | } | |
8f177c8e | 5275 | |
f85afd4e MB |
5276 | return TRUE; |
5277 | } | |
5278 | ||
5279 | return FALSE; | |
5280 | } | |
5281 | ||
2d66e025 MB |
5282 | |
5283 | ||
5284 | // Note - this function only draws cells that are in the list of | |
5285 | // exposed cells (usually set from the update region by | |
5286 | // CalcExposedCells) | |
5287 | // | |
5288 | void wxGrid::DrawGridCellArea( wxDC& dc ) | |
f85afd4e | 5289 | { |
2d66e025 | 5290 | if ( !m_numRows || !m_numCols ) return; |
60ff3b99 | 5291 | |
2d66e025 MB |
5292 | size_t i; |
5293 | size_t numCells = m_cellsExposed.GetCount(); | |
60ff3b99 | 5294 | |
2d66e025 | 5295 | for ( i = 0; i < numCells; i++ ) |
f85afd4e | 5296 | { |
2d66e025 MB |
5297 | DrawCell( dc, m_cellsExposed[i] ); |
5298 | } | |
5299 | } | |
8f177c8e | 5300 | |
8f177c8e | 5301 | |
7c8a8ad5 MB |
5302 | void wxGrid::DrawGridSpace( wxDC& dc ) |
5303 | { | |
5304 | if ( m_numRows && m_numCols ) | |
5305 | { | |
5306 | int cw, ch; | |
5307 | m_gridWin->GetClientSize( &cw, &ch ); | |
5308 | ||
5309 | int right, bottom; | |
5310 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
5311 | ||
5312 | if ( right > GetColRight(m_numCols-1) || | |
5313 | bottom > GetRowBottom(m_numRows-1) ) | |
5314 | { | |
5315 | int left, top; | |
5316 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
5317 | ||
5318 | dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) ); | |
5319 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
5320 | ||
5321 | if ( right > GetColRight(m_numCols-1) ) | |
a5777624 | 5322 | dc.DrawRectangle( GetColRight(m_numCols-1), top, |
7c8a8ad5 MB |
5323 | right - GetColRight(m_numCols-1), ch ); |
5324 | ||
5325 | if ( bottom > GetRowBottom(m_numRows-1) ) | |
a5777624 | 5326 | dc.DrawRectangle( left, GetRowBottom(m_numRows-1), |
7c8a8ad5 MB |
5327 | cw, bottom - GetRowBottom(m_numRows-1) ); |
5328 | } | |
5329 | } | |
5330 | } | |
5331 | ||
5332 | ||
2d66e025 MB |
5333 | void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) |
5334 | { | |
ab79958a VZ |
5335 | int row = coords.GetRow(); |
5336 | int col = coords.GetCol(); | |
60ff3b99 | 5337 | |
7c1cb261 | 5338 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
ab79958a VZ |
5339 | return; |
5340 | ||
5341 | // we draw the cell border ourselves | |
9496deb5 | 5342 | #if !WXGRID_DRAW_LINES |
2d66e025 MB |
5343 | if ( m_gridLinesEnabled ) |
5344 | DrawCellBorder( dc, coords ); | |
796df70a | 5345 | #endif |
f85afd4e | 5346 | |
189d0213 VZ |
5347 | wxGridCellAttr* attr = GetCellAttr(row, col); |
5348 | ||
5349 | bool isCurrent = coords == m_currentCellCoords; | |
508011ce | 5350 | |
ab79958a | 5351 | wxRect rect; |
7c1cb261 VZ |
5352 | rect.x = GetColLeft(col); |
5353 | rect.y = GetRowTop(row); | |
5354 | rect.width = GetColWidth(col) - 1; | |
5355 | rect.height = GetRowHeight(row) - 1; | |
f85afd4e | 5356 | |
189d0213 VZ |
5357 | // if the editor is shown, we should use it and not the renderer |
5358 | if ( isCurrent && IsCellEditControlEnabled() ) | |
5359 | { | |
0b190b0f VZ |
5360 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
5361 | editor->PaintBackground(rect, attr); | |
5362 | editor->DecRef(); | |
189d0213 VZ |
5363 | } |
5364 | else | |
5365 | { | |
5366 | // but all the rest is drawn by the cell renderer and hence may be | |
5367 | // customized | |
0b190b0f VZ |
5368 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); |
5369 | renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); | |
5370 | renderer->DecRef(); | |
189d0213 | 5371 | } |
07296f0b | 5372 | |
283b7808 VZ |
5373 | attr->DecRef(); |
5374 | } | |
07296f0b | 5375 | |
283b7808 | 5376 | void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ) |
07296f0b RD |
5377 | { |
5378 | int row = m_currentCellCoords.GetRow(); | |
5379 | int col = m_currentCellCoords.GetCol(); | |
5380 | ||
7c1cb261 | 5381 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
07296f0b RD |
5382 | return; |
5383 | ||
5384 | wxRect rect; | |
7c1cb261 VZ |
5385 | rect.x = GetColLeft(col); |
5386 | rect.y = GetRowTop(row); | |
5387 | rect.width = GetColWidth(col) - 1; | |
5388 | rect.height = GetRowHeight(row) - 1; | |
07296f0b | 5389 | |
b3a7510d VZ |
5390 | // hmmm... what could we do here to show that the cell is disabled? |
5391 | // for now, I just draw a thinner border than for the other ones, but | |
5392 | // it doesn't look really good | |
5393 | dc.SetPen(wxPen(m_gridLineColour, attr->IsReadOnly() ? 1 : 3, wxSOLID)); | |
5394 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
5395 | ||
5396 | dc.DrawRectangle(rect); | |
07296f0b | 5397 | |
283b7808 | 5398 | #if 0 |
b3a7510d | 5399 | // VZ: my experiments with 3d borders... |
283b7808 | 5400 | |
b3a7510d | 5401 | // how to properly set colours for arbitrary bg? |
283b7808 VZ |
5402 | wxCoord x1 = rect.x, |
5403 | y1 = rect.y, | |
00cf1208 RR |
5404 | x2 = rect.x + rect.width -1, |
5405 | y2 = rect.y + rect.height -1; | |
283b7808 VZ |
5406 | |
5407 | dc.SetPen(*wxWHITE_PEN); | |
00cf1208 RR |
5408 | dc.DrawLine(x1, y1, x2, y1); |
5409 | dc.DrawLine(x1, y1, x1, y2); | |
283b7808 | 5410 | |
283b7808 | 5411 | dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1); |
00cf1208 | 5412 | dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 ); |
283b7808 VZ |
5413 | |
5414 | dc.SetPen(*wxBLACK_PEN); | |
5415 | dc.DrawLine(x1, y2, x2, y2); | |
00cf1208 | 5416 | dc.DrawLine(x2, y1, x2, y2+1); |
b3a7510d | 5417 | #endif // 0 |
2d66e025 | 5418 | } |
f85afd4e | 5419 | |
f2d76237 | 5420 | |
2d66e025 MB |
5421 | void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) |
5422 | { | |
2d66e025 MB |
5423 | int row = coords.GetRow(); |
5424 | int col = coords.GetCol(); | |
7c1cb261 VZ |
5425 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
5426 | return; | |
5427 | ||
5428 | dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) ); | |
60ff3b99 | 5429 | |
2d66e025 MB |
5430 | // right hand border |
5431 | // | |
7c1cb261 VZ |
5432 | dc.DrawLine( GetColRight(col), GetRowTop(row), |
5433 | GetColRight(col), GetRowBottom(row) ); | |
2d66e025 MB |
5434 | |
5435 | // bottom border | |
5436 | // | |
7c1cb261 VZ |
5437 | dc.DrawLine( GetColLeft(col), GetRowBottom(row), |
5438 | GetColRight(col), GetRowBottom(row) ); | |
f85afd4e MB |
5439 | } |
5440 | ||
b3a7510d VZ |
5441 | void wxGrid::DrawHighlight(wxDC& dc) |
5442 | { | |
2a7750d9 MB |
5443 | // This if block was previously in wxGrid::OnPaint but that doesn't |
5444 | // seem to get called under wxGTK - MB | |
5445 | // | |
5446 | if ( m_currentCellCoords == wxGridNoCellCoords && | |
5447 | m_numRows && m_numCols ) | |
5448 | { | |
5449 | m_currentCellCoords.Set(0, 0); | |
5450 | } | |
5451 | ||
99306db2 VZ |
5452 | if ( IsCellEditControlEnabled() ) |
5453 | { | |
5454 | // don't show highlight when the edit control is shown | |
5455 | return; | |
5456 | } | |
5457 | ||
b3a7510d VZ |
5458 | // if the active cell was repainted, repaint its highlight too because it |
5459 | // might have been damaged by the grid lines | |
5460 | size_t count = m_cellsExposed.GetCount(); | |
5461 | for ( size_t n = 0; n < count; n++ ) | |
5462 | { | |
5463 | if ( m_cellsExposed[n] == m_currentCellCoords ) | |
5464 | { | |
5465 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
5466 | DrawCellHighlight(dc, attr); | |
5467 | attr->DecRef(); | |
5468 | ||
5469 | break; | |
5470 | } | |
5471 | } | |
5472 | } | |
2d66e025 | 5473 | |
2d66e025 MB |
5474 | // TODO: remove this ??? |
5475 | // This is used to redraw all grid lines e.g. when the grid line colour | |
5476 | // has been changed | |
5477 | // | |
796df70a | 5478 | void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & reg ) |
f85afd4e | 5479 | { |
60ff3b99 VZ |
5480 | if ( !m_gridLinesEnabled || |
5481 | !m_numRows || | |
2d66e025 | 5482 | !m_numCols ) return; |
f85afd4e | 5483 | |
2d66e025 | 5484 | int top, bottom, left, right; |
796df70a | 5485 | |
8e217128 | 5486 | #ifndef __WXGTK__ |
508011ce VZ |
5487 | if (reg.IsEmpty()) |
5488 | { | |
796df70a SN |
5489 | int cw, ch; |
5490 | m_gridWin->GetClientSize(&cw, &ch); | |
5491 | ||
5492 | // virtual coords of visible area | |
5493 | // | |
5494 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
5495 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
5496 | } | |
508011ce VZ |
5497 | else |
5498 | { | |
796df70a SN |
5499 | wxCoord x, y, w, h; |
5500 | reg.GetBox(x, y, w, h); | |
5501 | CalcUnscrolledPosition( x, y, &left, &top ); | |
5502 | CalcUnscrolledPosition( x + w, y + h, &right, &bottom ); | |
5503 | } | |
8e217128 RR |
5504 | #else |
5505 | int cw, ch; | |
5506 | m_gridWin->GetClientSize(&cw, &ch); | |
5507 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
5508 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
5509 | #endif | |
f85afd4e | 5510 | |
9496deb5 MB |
5511 | // avoid drawing grid lines past the last row and col |
5512 | // | |
7c1cb261 VZ |
5513 | right = wxMin( right, GetColRight(m_numCols - 1) ); |
5514 | bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) ); | |
9496deb5 | 5515 | |
2d66e025 | 5516 | dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) ); |
f85afd4e | 5517 | |
2d66e025 | 5518 | // horizontal grid lines |
f85afd4e | 5519 | // |
60ff3b99 | 5520 | int i; |
9496deb5 | 5521 | for ( i = 0; i < m_numRows; i++ ) |
f85afd4e | 5522 | { |
6d55126d | 5523 | int bot = GetRowBottom(i) - 1; |
7c1cb261 | 5524 | |
6d55126d | 5525 | if ( bot > bottom ) |
2d66e025 MB |
5526 | { |
5527 | break; | |
5528 | } | |
7c1cb261 | 5529 | |
6d55126d | 5530 | if ( bot >= top ) |
2d66e025 | 5531 | { |
6d55126d | 5532 | dc.DrawLine( left, bot, right, bot ); |
2d66e025 | 5533 | } |
f85afd4e MB |
5534 | } |
5535 | ||
f85afd4e | 5536 | |
2d66e025 MB |
5537 | // vertical grid lines |
5538 | // | |
9496deb5 | 5539 | for ( i = 0; i < m_numCols; i++ ) |
f85afd4e | 5540 | { |
7c1cb261 VZ |
5541 | int colRight = GetColRight(i) - 1; |
5542 | if ( colRight > right ) | |
2d66e025 MB |
5543 | { |
5544 | break; | |
5545 | } | |
7c1cb261 VZ |
5546 | |
5547 | if ( colRight >= left ) | |
2d66e025 | 5548 | { |
7c1cb261 | 5549 | dc.DrawLine( colRight, top, colRight, bottom ); |
2d66e025 MB |
5550 | } |
5551 | } | |
5552 | } | |
f85afd4e | 5553 | |
8f177c8e | 5554 | |
2d66e025 MB |
5555 | void wxGrid::DrawRowLabels( wxDC& dc ) |
5556 | { | |
5557 | if ( !m_numRows || !m_numCols ) return; | |
60ff3b99 | 5558 | |
2d66e025 MB |
5559 | size_t i; |
5560 | size_t numLabels = m_rowLabelsExposed.GetCount(); | |
60ff3b99 | 5561 | |
2d66e025 MB |
5562 | for ( i = 0; i < numLabels; i++ ) |
5563 | { | |
5564 | DrawRowLabel( dc, m_rowLabelsExposed[i] ); | |
60ff3b99 | 5565 | } |
f85afd4e MB |
5566 | } |
5567 | ||
5568 | ||
2d66e025 | 5569 | void wxGrid::DrawRowLabel( wxDC& dc, int row ) |
f85afd4e | 5570 | { |
7c1cb261 VZ |
5571 | if ( GetRowHeight(row) <= 0 ) |
5572 | return; | |
60ff3b99 | 5573 | |
7c1cb261 VZ |
5574 | int rowTop = GetRowTop(row), |
5575 | rowBottom = GetRowBottom(row) - 1; | |
b99be8fb | 5576 | |
2d66e025 | 5577 | dc.SetPen( *wxBLACK_PEN ); |
b0d6ff2f | 5578 | dc.DrawLine( m_rowLabelWidth-1, rowTop, |
7c1cb261 | 5579 | m_rowLabelWidth-1, rowBottom ); |
b99be8fb | 5580 | |
7c1cb261 | 5581 | dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom ); |
b99be8fb | 5582 | |
2d66e025 | 5583 | dc.SetPen( *wxWHITE_PEN ); |
7c1cb261 | 5584 | dc.DrawLine( 0, rowTop, 0, rowBottom ); |
b0d6ff2f | 5585 | dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop ); |
60ff3b99 | 5586 | |
f85afd4e | 5587 | dc.SetBackgroundMode( wxTRANSPARENT ); |
f85afd4e MB |
5588 | dc.SetTextForeground( GetLabelTextColour() ); |
5589 | dc.SetFont( GetLabelFont() ); | |
8f177c8e | 5590 | |
f85afd4e | 5591 | int hAlign, vAlign; |
2d66e025 | 5592 | GetRowLabelAlignment( &hAlign, &vAlign ); |
60ff3b99 | 5593 | |
2d66e025 MB |
5594 | wxRect rect; |
5595 | rect.SetX( 2 ); | |
7c1cb261 | 5596 | rect.SetY( GetRowTop(row) + 2 ); |
2d66e025 | 5597 | rect.SetWidth( m_rowLabelWidth - 4 ); |
7c1cb261 | 5598 | rect.SetHeight( GetRowHeight(row) - 4 ); |
60ff3b99 | 5599 | DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign ); |
f85afd4e MB |
5600 | } |
5601 | ||
5602 | ||
2d66e025 | 5603 | void wxGrid::DrawColLabels( wxDC& dc ) |
f85afd4e | 5604 | { |
2d66e025 | 5605 | if ( !m_numRows || !m_numCols ) return; |
60ff3b99 | 5606 | |
2d66e025 MB |
5607 | size_t i; |
5608 | size_t numLabels = m_colLabelsExposed.GetCount(); | |
60ff3b99 | 5609 | |
2d66e025 | 5610 | for ( i = 0; i < numLabels; i++ ) |
f85afd4e | 5611 | { |
2d66e025 | 5612 | DrawColLabel( dc, m_colLabelsExposed[i] ); |
60ff3b99 | 5613 | } |
f85afd4e MB |
5614 | } |
5615 | ||
5616 | ||
2d66e025 | 5617 | void wxGrid::DrawColLabel( wxDC& dc, int col ) |
f85afd4e | 5618 | { |
7c1cb261 VZ |
5619 | if ( GetColWidth(col) <= 0 ) |
5620 | return; | |
60ff3b99 | 5621 | |
7c1cb261 VZ |
5622 | int colLeft = GetColLeft(col), |
5623 | colRight = GetColRight(col) - 1; | |
b99be8fb | 5624 | |
2d66e025 | 5625 | dc.SetPen( *wxBLACK_PEN ); |
7c1cb261 VZ |
5626 | dc.DrawLine( colRight, 0, |
5627 | colRight, m_colLabelHeight-1 ); | |
b99be8fb | 5628 | |
b0d6ff2f | 5629 | dc.DrawLine( colLeft, m_colLabelHeight-1, |
7c1cb261 | 5630 | colRight, m_colLabelHeight-1 ); |
b99be8fb | 5631 | |
f85afd4e | 5632 | dc.SetPen( *wxWHITE_PEN ); |
b0d6ff2f | 5633 | dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 ); |
7c1cb261 | 5634 | dc.DrawLine( colLeft, 0, colRight, 0 ); |
f85afd4e | 5635 | |
b0d6ff2f MB |
5636 | dc.SetBackgroundMode( wxTRANSPARENT ); |
5637 | dc.SetTextForeground( GetLabelTextColour() ); | |
5638 | dc.SetFont( GetLabelFont() ); | |
8f177c8e | 5639 | |
f85afd4e | 5640 | dc.SetBackgroundMode( wxTRANSPARENT ); |
f85afd4e MB |
5641 | dc.SetTextForeground( GetLabelTextColour() ); |
5642 | dc.SetFont( GetLabelFont() ); | |
8f177c8e | 5643 | |
f85afd4e | 5644 | int hAlign, vAlign; |
2d66e025 | 5645 | GetColLabelAlignment( &hAlign, &vAlign ); |
60ff3b99 | 5646 | |
2d66e025 | 5647 | wxRect rect; |
7c1cb261 | 5648 | rect.SetX( colLeft + 2 ); |
2d66e025 | 5649 | rect.SetY( 2 ); |
7c1cb261 | 5650 | rect.SetWidth( GetColWidth(col) - 4 ); |
2d66e025 | 5651 | rect.SetHeight( m_colLabelHeight - 4 ); |
60ff3b99 | 5652 | DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign ); |
f85afd4e MB |
5653 | } |
5654 | ||
5655 | ||
2d66e025 MB |
5656 | void wxGrid::DrawTextRectangle( wxDC& dc, |
5657 | const wxString& value, | |
5658 | const wxRect& rect, | |
5659 | int horizAlign, | |
5660 | int vertAlign ) | |
f85afd4e | 5661 | { |
2d66e025 MB |
5662 | long textWidth, textHeight; |
5663 | long lineWidth, lineHeight; | |
5664 | wxArrayString lines; | |
f85afd4e | 5665 | |
2d66e025 MB |
5666 | dc.SetClippingRegion( rect ); |
5667 | StringToLines( value, lines ); | |
5668 | if ( lines.GetCount() ) | |
5669 | { | |
5670 | GetTextBoxSize( dc, lines, &textWidth, &textHeight ); | |
5671 | dc.GetTextExtent( lines[0], &lineWidth, &lineHeight ); | |
f85afd4e | 5672 | |
2d66e025 MB |
5673 | float x, y; |
5674 | switch ( horizAlign ) | |
5675 | { | |
5676 | case wxRIGHT: | |
5677 | x = rect.x + (rect.width - textWidth - 1); | |
5678 | break; | |
f85afd4e | 5679 | |
2d66e025 MB |
5680 | case wxCENTRE: |
5681 | x = rect.x + ((rect.width - textWidth)/2); | |
5682 | break; | |
f85afd4e | 5683 | |
2d66e025 MB |
5684 | case wxLEFT: |
5685 | default: | |
5686 | x = rect.x + 1; | |
5687 | break; | |
5688 | } | |
f85afd4e | 5689 | |
2d66e025 MB |
5690 | switch ( vertAlign ) |
5691 | { | |
5692 | case wxBOTTOM: | |
5693 | y = rect.y + (rect.height - textHeight - 1); | |
5694 | break; | |
f85afd4e MB |
5695 | |
5696 | case wxCENTRE: | |
8f177c8e | 5697 | y = rect.y + ((rect.height - textHeight)/2); |
f85afd4e MB |
5698 | break; |
5699 | ||
5700 | case wxTOP: | |
5701 | default: | |
8f177c8e | 5702 | y = rect.y + 1; |
f85afd4e MB |
5703 | break; |
5704 | } | |
5705 | ||
b540eb2b | 5706 | for ( size_t i = 0; i < lines.GetCount(); i++ ) |
f85afd4e MB |
5707 | { |
5708 | dc.DrawText( lines[i], (long)x, (long)y ); | |
5709 | y += lineHeight; | |
5710 | } | |
5711 | } | |
8f177c8e | 5712 | |
f85afd4e | 5713 | dc.DestroyClippingRegion(); |
f85afd4e MB |
5714 | } |
5715 | ||
5716 | ||
5717 | // Split multi line text up into an array of strings. Any existing | |
5718 | // contents of the string array are preserved. | |
5719 | // | |
5720 | void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) | |
5721 | { | |
f85afd4e MB |
5722 | int startPos = 0; |
5723 | int pos; | |
6d004f67 | 5724 | wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix ); |
2433bb2e | 5725 | wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix ); |
e2b42eeb | 5726 | |
2433bb2e | 5727 | while ( startPos < (int)tVal.Length() ) |
f85afd4e | 5728 | { |
2433bb2e | 5729 | pos = tVal.Mid(startPos).Find( eol ); |
f85afd4e MB |
5730 | if ( pos < 0 ) |
5731 | { | |
5732 | break; | |
5733 | } | |
5734 | else if ( pos == 0 ) | |
5735 | { | |
5736 | lines.Add( wxEmptyString ); | |
5737 | } | |
5738 | else | |
5739 | { | |
2433bb2e | 5740 | lines.Add( value.Mid(startPos, pos) ); |
f85afd4e MB |
5741 | } |
5742 | startPos += pos+1; | |
5743 | } | |
b540eb2b | 5744 | if ( startPos < (int)value.Length() ) |
f85afd4e MB |
5745 | { |
5746 | lines.Add( value.Mid( startPos ) ); | |
5747 | } | |
5748 | } | |
5749 | ||
5750 | ||
5751 | void wxGrid::GetTextBoxSize( wxDC& dc, | |
5752 | wxArrayString& lines, | |
5753 | long *width, long *height ) | |
5754 | { | |
5755 | long w = 0; | |
5756 | long h = 0; | |
5757 | long lineW, lineH; | |
5758 | ||
b540eb2b | 5759 | size_t i; |
f85afd4e MB |
5760 | for ( i = 0; i < lines.GetCount(); i++ ) |
5761 | { | |
5762 | dc.GetTextExtent( lines[i], &lineW, &lineH ); | |
5763 | w = wxMax( w, lineW ); | |
5764 | h += lineH; | |
5765 | } | |
5766 | ||
5767 | *width = w; | |
5768 | *height = h; | |
5769 | } | |
5770 | ||
5771 | ||
5772 | // | |
2d66e025 | 5773 | // ------ Edit control functions |
f85afd4e MB |
5774 | // |
5775 | ||
2d66e025 MB |
5776 | |
5777 | void wxGrid::EnableEditing( bool edit ) | |
f85afd4e | 5778 | { |
2d66e025 MB |
5779 | // TODO: improve this ? |
5780 | // | |
5781 | if ( edit != m_editable ) | |
f85afd4e | 5782 | { |
2d66e025 | 5783 | m_editable = edit; |
1f1ce288 | 5784 | |
b54ba671 VZ |
5785 | // FIXME IMHO this won't disable the edit control if edit == FALSE |
5786 | // because of the check in the beginning of | |
5787 | // EnableCellEditControl() just below (VZ) | |
2c9a89e0 | 5788 | EnableCellEditControl(m_editable); |
f85afd4e | 5789 | } |
f85afd4e MB |
5790 | } |
5791 | ||
5792 | ||
2d66e025 | 5793 | void wxGrid::EnableCellEditControl( bool enable ) |
f85afd4e | 5794 | { |
2c9a89e0 RD |
5795 | if (! m_editable) |
5796 | return; | |
5797 | ||
dcfe4c3d SN |
5798 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
5799 | SetCurrentCell( 0, 0 ); | |
60ff3b99 | 5800 | |
2c9a89e0 RD |
5801 | if ( enable != m_cellEditCtrlEnabled ) |
5802 | { | |
b54ba671 VZ |
5803 | // TODO allow the app to Veto() this event? |
5804 | SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN); | |
5805 | ||
dcfe4c3d | 5806 | if ( enable ) |
f85afd4e | 5807 | { |
b54ba671 VZ |
5808 | // this should be checked by the caller! |
5809 | wxASSERT_MSG( CanEnableCellControl(), | |
5810 | _T("can't enable editing for this cell!") ); | |
5811 | ||
5812 | // do it before ShowCellEditControl() | |
dcfe4c3d | 5813 | m_cellEditCtrlEnabled = enable; |
b54ba671 | 5814 | |
2d66e025 | 5815 | ShowCellEditControl(); |
2d66e025 MB |
5816 | } |
5817 | else | |
5818 | { | |
2d66e025 MB |
5819 | HideCellEditControl(); |
5820 | SaveEditControlValue(); | |
b54ba671 VZ |
5821 | |
5822 | // do it after HideCellEditControl() | |
dcfe4c3d | 5823 | m_cellEditCtrlEnabled = enable; |
f85afd4e | 5824 | } |
f85afd4e | 5825 | } |
f85afd4e | 5826 | } |
f85afd4e | 5827 | |
b54ba671 | 5828 | bool wxGrid::IsCurrentCellReadOnly() const |
283b7808 | 5829 | { |
b54ba671 VZ |
5830 | // const_cast |
5831 | wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords); | |
5832 | bool readonly = attr->IsReadOnly(); | |
5833 | attr->DecRef(); | |
283b7808 | 5834 | |
b54ba671 VZ |
5835 | return readonly; |
5836 | } | |
283b7808 | 5837 | |
b54ba671 VZ |
5838 | bool wxGrid::CanEnableCellControl() const |
5839 | { | |
5840 | return m_editable && !IsCurrentCellReadOnly(); | |
5841 | } | |
5842 | ||
5843 | bool wxGrid::IsCellEditControlEnabled() const | |
5844 | { | |
5845 | // the cell edit control might be disable for all cells or just for the | |
5846 | // current one if it's read only | |
5847 | return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE; | |
283b7808 VZ |
5848 | } |
5849 | ||
2d66e025 | 5850 | void wxGrid::ShowCellEditControl() |
f85afd4e | 5851 | { |
2d66e025 | 5852 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 5853 | { |
2d66e025 MB |
5854 | if ( !IsVisible( m_currentCellCoords ) ) |
5855 | { | |
5856 | return; | |
5857 | } | |
5858 | else | |
5859 | { | |
2c9a89e0 RD |
5860 | wxRect rect = CellToRect( m_currentCellCoords ); |
5861 | int row = m_currentCellCoords.GetRow(); | |
5862 | int col = m_currentCellCoords.GetCol(); | |
2d66e025 MB |
5863 | |
5864 | // convert to scrolled coords | |
5865 | // | |
99306db2 | 5866 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
508011ce | 5867 | |
99306db2 VZ |
5868 | // done in PaintBackground() |
5869 | #if 0 | |
5870 | // erase the highlight and the cell contents because the editor | |
5871 | // might not cover the entire cell | |
5872 | wxClientDC dc( m_gridWin ); | |
5873 | PrepareDC( dc ); | |
5874 | dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID)); | |
5875 | dc.SetPen(*wxTRANSPARENT_PEN); | |
5876 | dc.DrawRectangle(rect); | |
5877 | #endif // 0 | |
d2fdd8d2 | 5878 | |
99306db2 VZ |
5879 | // cell is shifted by one pixel |
5880 | rect.x--; | |
5881 | rect.y--; | |
f0102d2a | 5882 | |
508011ce | 5883 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 5884 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
3da93aae VZ |
5885 | if ( !editor->IsCreated() ) |
5886 | { | |
2c9a89e0 RD |
5887 | editor->Create(m_gridWin, -1, |
5888 | new wxGridCellEditorEvtHandler(this, editor)); | |
2d66e025 MB |
5889 | } |
5890 | ||
b0e282b3 | 5891 | editor->Show( TRUE, attr ); |
0b190b0f | 5892 | |
2c9a89e0 | 5893 | editor->SetSize( rect ); |
99306db2 | 5894 | |
3da93aae | 5895 | editor->BeginEdit(row, col, this); |
0b190b0f VZ |
5896 | |
5897 | editor->DecRef(); | |
2c9a89e0 RD |
5898 | attr->DecRef(); |
5899 | } | |
f85afd4e MB |
5900 | } |
5901 | } | |
5902 | ||
5903 | ||
2d66e025 | 5904 | void wxGrid::HideCellEditControl() |
f85afd4e | 5905 | { |
2d66e025 | 5906 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 5907 | { |
2c9a89e0 RD |
5908 | int row = m_currentCellCoords.GetRow(); |
5909 | int col = m_currentCellCoords.GetCol(); | |
5910 | ||
3da93aae | 5911 | wxGridCellAttr* attr = GetCellAttr(row, col); |
0b190b0f VZ |
5912 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
5913 | editor->Show( FALSE ); | |
5914 | editor->DecRef(); | |
2c9a89e0 RD |
5915 | attr->DecRef(); |
5916 | m_gridWin->SetFocus(); | |
f85afd4e | 5917 | } |
2d66e025 | 5918 | } |
8f177c8e | 5919 | |
2d66e025 | 5920 | |
2d66e025 MB |
5921 | void wxGrid::SaveEditControlValue() |
5922 | { | |
3da93aae VZ |
5923 | if ( IsCellEditControlEnabled() ) |
5924 | { | |
2c9a89e0 RD |
5925 | int row = m_currentCellCoords.GetRow(); |
5926 | int col = m_currentCellCoords.GetCol(); | |
8f177c8e | 5927 | |
3da93aae | 5928 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 5929 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
3324d5f5 | 5930 | bool changed = editor->EndEdit(row, col, this); |
2d66e025 | 5931 | |
0b190b0f | 5932 | editor->DecRef(); |
2c9a89e0 | 5933 | attr->DecRef(); |
2d66e025 | 5934 | |
3da93aae VZ |
5935 | if (changed) |
5936 | { | |
b54ba671 | 5937 | SendEvent( wxEVT_GRID_CELL_CHANGE, |
2d66e025 MB |
5938 | m_currentCellCoords.GetRow(), |
5939 | m_currentCellCoords.GetCol() ); | |
5940 | } | |
f85afd4e MB |
5941 | } |
5942 | } | |
5943 | ||
2d66e025 MB |
5944 | |
5945 | // | |
60ff3b99 VZ |
5946 | // ------ Grid location functions |
5947 | // Note that all of these functions work with the logical coordinates of | |
2d66e025 MB |
5948 | // grid cells and labels so you will need to convert from device |
5949 | // coordinates for mouse events etc. | |
5950 | // | |
5951 | ||
5952 | void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords ) | |
f85afd4e | 5953 | { |
58dd5b3b MB |
5954 | int row = YToRow(y); |
5955 | int col = XToCol(x); | |
5956 | ||
5957 | if ( row == -1 || col == -1 ) | |
5958 | { | |
5959 | coords = wxGridNoCellCoords; | |
5960 | } | |
5961 | else | |
5962 | { | |
5963 | coords.Set( row, col ); | |
5964 | } | |
2d66e025 | 5965 | } |
f85afd4e | 5966 | |
8f177c8e | 5967 | |
2d66e025 MB |
5968 | int wxGrid::YToRow( int y ) |
5969 | { | |
5970 | int i; | |
8f177c8e | 5971 | |
2d66e025 | 5972 | for ( i = 0; i < m_numRows; i++ ) |
f85afd4e | 5973 | { |
7c1cb261 VZ |
5974 | if ( y < GetRowBottom(i) ) |
5975 | return i; | |
f85afd4e | 5976 | } |
2d66e025 | 5977 | |
4ee5fc9c | 5978 | return -1; |
f85afd4e MB |
5979 | } |
5980 | ||
2d66e025 MB |
5981 | |
5982 | int wxGrid::XToCol( int x ) | |
f85afd4e | 5983 | { |
2d66e025 | 5984 | int i; |
8f177c8e | 5985 | |
2d66e025 | 5986 | for ( i = 0; i < m_numCols; i++ ) |
f85afd4e | 5987 | { |
7c1cb261 VZ |
5988 | if ( x < GetColRight(i) ) |
5989 | return i; | |
f85afd4e MB |
5990 | } |
5991 | ||
4ee5fc9c | 5992 | return -1; |
2d66e025 | 5993 | } |
8f177c8e | 5994 | |
2d66e025 MB |
5995 | |
5996 | // return the row number that that the y coord is near the edge of, or | |
5997 | // -1 if not near an edge | |
5998 | // | |
5999 | int wxGrid::YToEdgeOfRow( int y ) | |
6000 | { | |
6001 | int i, d; | |
6002 | ||
6003 | for ( i = 0; i < m_numRows; i++ ) | |
6004 | { | |
7c1cb261 | 6005 | if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE ) |
f85afd4e | 6006 | { |
7c1cb261 VZ |
6007 | d = abs( y - GetRowBottom(i) ); |
6008 | if ( d < WXGRID_LABEL_EDGE_ZONE ) | |
6009 | return i; | |
f85afd4e | 6010 | } |
f85afd4e | 6011 | } |
2d66e025 MB |
6012 | |
6013 | return -1; | |
6014 | } | |
6015 | ||
6016 | ||
6017 | // return the col number that that the x coord is near the edge of, or | |
6018 | // -1 if not near an edge | |
6019 | // | |
6020 | int wxGrid::XToEdgeOfCol( int x ) | |
6021 | { | |
6022 | int i, d; | |
6023 | ||
6024 | for ( i = 0; i < m_numCols; i++ ) | |
f85afd4e | 6025 | { |
7c1cb261 | 6026 | if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE ) |
2d66e025 | 6027 | { |
7c1cb261 VZ |
6028 | d = abs( x - GetColRight(i) ); |
6029 | if ( d < WXGRID_LABEL_EDGE_ZONE ) | |
6030 | return i; | |
2d66e025 | 6031 | } |
f85afd4e | 6032 | } |
2d66e025 MB |
6033 | |
6034 | return -1; | |
f85afd4e MB |
6035 | } |
6036 | ||
2d66e025 MB |
6037 | |
6038 | wxRect wxGrid::CellToRect( int row, int col ) | |
f85afd4e | 6039 | { |
2d66e025 | 6040 | wxRect rect( -1, -1, -1, -1 ); |
f85afd4e | 6041 | |
2d66e025 MB |
6042 | if ( row >= 0 && row < m_numRows && |
6043 | col >= 0 && col < m_numCols ) | |
f85afd4e | 6044 | { |
7c1cb261 VZ |
6045 | rect.x = GetColLeft(col); |
6046 | rect.y = GetRowTop(row); | |
6047 | rect.width = GetColWidth(col); | |
6048 | rect.height = GetRowHeight(row); | |
f85afd4e MB |
6049 | } |
6050 | ||
2d66e025 MB |
6051 | return rect; |
6052 | } | |
6053 | ||
6054 | ||
6055 | bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) | |
6056 | { | |
6057 | // get the cell rectangle in logical coords | |
6058 | // | |
6059 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 6060 | |
2d66e025 MB |
6061 | // convert to device coords |
6062 | // | |
6063 | int left, top, right, bottom; | |
6064 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
6065 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 6066 | |
2d66e025 MB |
6067 | // check against the client area of the grid window |
6068 | // | |
6069 | int cw, ch; | |
6070 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 6071 | |
2d66e025 | 6072 | if ( wholeCellVisible ) |
f85afd4e | 6073 | { |
2d66e025 | 6074 | // is the cell wholly visible ? |
8f177c8e | 6075 | // |
2d66e025 MB |
6076 | return ( left >= 0 && right <= cw && |
6077 | top >= 0 && bottom <= ch ); | |
6078 | } | |
6079 | else | |
6080 | { | |
6081 | // is the cell partly visible ? | |
6082 | // | |
6083 | return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) && | |
6084 | ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) ); | |
6085 | } | |
6086 | } | |
6087 | ||
6088 | ||
6089 | // make the specified cell location visible by doing a minimal amount | |
6090 | // of scrolling | |
6091 | // | |
6092 | void wxGrid::MakeCellVisible( int row, int col ) | |
6093 | { | |
6094 | int i; | |
60ff3b99 | 6095 | int xpos = -1, ypos = -1; |
2d66e025 MB |
6096 | |
6097 | if ( row >= 0 && row < m_numRows && | |
6098 | col >= 0 && col < m_numCols ) | |
6099 | { | |
6100 | // get the cell rectangle in logical coords | |
6101 | // | |
6102 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 6103 | |
2d66e025 MB |
6104 | // convert to device coords |
6105 | // | |
6106 | int left, top, right, bottom; | |
6107 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
6108 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 6109 | |
2d66e025 MB |
6110 | int cw, ch; |
6111 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 6112 | |
2d66e025 | 6113 | if ( top < 0 ) |
3f296516 | 6114 | { |
2d66e025 | 6115 | ypos = r.GetTop(); |
3f296516 | 6116 | } |
2d66e025 MB |
6117 | else if ( bottom > ch ) |
6118 | { | |
6119 | int h = r.GetHeight(); | |
6120 | ypos = r.GetTop(); | |
6121 | for ( i = row-1; i >= 0; i-- ) | |
6122 | { | |
7c1cb261 VZ |
6123 | int rowHeight = GetRowHeight(i); |
6124 | if ( h + rowHeight > ch ) | |
6125 | break; | |
2d66e025 | 6126 | |
7c1cb261 VZ |
6127 | h += rowHeight; |
6128 | ypos -= rowHeight; | |
2d66e025 | 6129 | } |
f0102d2a VZ |
6130 | |
6131 | // we divide it later by GRID_SCROLL_LINE, make sure that we don't | |
6132 | // have rounding errors (this is important, because if we do, we | |
6133 | // might not scroll at all and some cells won't be redrawn) | |
6134 | ypos += GRID_SCROLL_LINE / 2; | |
2d66e025 MB |
6135 | } |
6136 | ||
6137 | if ( left < 0 ) | |
6138 | { | |
6139 | xpos = r.GetLeft(); | |
6140 | } | |
6141 | else if ( right > cw ) | |
6142 | { | |
6143 | int w = r.GetWidth(); | |
6144 | xpos = r.GetLeft(); | |
6145 | for ( i = col-1; i >= 0; i-- ) | |
6146 | { | |
7c1cb261 VZ |
6147 | int colWidth = GetColWidth(i); |
6148 | if ( w + colWidth > cw ) | |
6149 | break; | |
2d66e025 | 6150 | |
7c1cb261 VZ |
6151 | w += colWidth; |
6152 | xpos -= colWidth; | |
2d66e025 | 6153 | } |
f0102d2a VZ |
6154 | |
6155 | // see comment for ypos above | |
6156 | xpos += GRID_SCROLL_LINE / 2; | |
2d66e025 MB |
6157 | } |
6158 | ||
6159 | if ( xpos != -1 || ypos != -1 ) | |
6160 | { | |
f0102d2a VZ |
6161 | if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE; |
6162 | if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE; | |
2d66e025 MB |
6163 | Scroll( xpos, ypos ); |
6164 | AdjustScrollbars(); | |
6165 | } | |
6166 | } | |
6167 | } | |
6168 | ||
6169 | ||
6170 | // | |
6171 | // ------ Grid cursor movement functions | |
6172 | // | |
6173 | ||
5c8fc7c1 | 6174 | bool wxGrid::MoveCursorUp( bool expandSelection ) |
2d66e025 MB |
6175 | { |
6176 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
6177 | m_currentCellCoords.GetRow() > 0 ) | |
6178 | { | |
5c8fc7c1 | 6179 | if ( expandSelection ) |
d95b0c2b SN |
6180 | { |
6181 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
6182 | m_selectingKeyboard = m_currentCellCoords; | |
6183 | m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 ); | |
6b105754 SN |
6184 | MakeCellVisible( m_selectingKeyboard.GetRow(), |
6185 | m_selectingKeyboard.GetCol() ); | |
d95b0c2b SN |
6186 | SelectBlock( m_currentCellCoords, m_selectingKeyboard ); |
6187 | } | |
6188 | else | |
ca4f2b72 SN |
6189 | { |
6190 | ClearSelection(); | |
6b105754 SN |
6191 | MakeCellVisible( m_currentCellCoords.GetRow() - 1, |
6192 | m_currentCellCoords.GetCol() ); | |
d95b0c2b SN |
6193 | SetCurrentCell( m_currentCellCoords.GetRow() - 1, |
6194 | m_currentCellCoords.GetCol() ); | |
ca4f2b72 | 6195 | } |
8f177c8e | 6196 | return TRUE; |
f85afd4e | 6197 | } |
2d66e025 MB |
6198 | |
6199 | return FALSE; | |
6200 | } | |
6201 | ||
6202 | ||
5c8fc7c1 | 6203 | bool wxGrid::MoveCursorDown( bool expandSelection ) |
2d66e025 | 6204 | { |
2d66e025 MB |
6205 | if ( m_currentCellCoords != wxGridNoCellCoords && |
6206 | m_currentCellCoords.GetRow() < m_numRows-1 ) | |
f85afd4e | 6207 | { |
5c8fc7c1 | 6208 | if ( expandSelection ) |
d95b0c2b SN |
6209 | { |
6210 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
6211 | m_selectingKeyboard = m_currentCellCoords; | |
6212 | m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 ); | |
6b105754 SN |
6213 | MakeCellVisible( m_selectingKeyboard.GetRow(), |
6214 | m_selectingKeyboard.GetCol() ); | |
d95b0c2b SN |
6215 | SelectBlock( m_currentCellCoords, m_selectingKeyboard ); |
6216 | } | |
6217 | else | |
ca4f2b72 SN |
6218 | { |
6219 | ClearSelection(); | |
6b105754 SN |
6220 | MakeCellVisible( m_currentCellCoords.GetRow() + 1, |
6221 | m_currentCellCoords.GetCol() ); | |
d95b0c2b SN |
6222 | SetCurrentCell( m_currentCellCoords.GetRow() + 1, |
6223 | m_currentCellCoords.GetCol() ); | |
ca4f2b72 | 6224 | } |
2d66e025 | 6225 | return TRUE; |
f85afd4e | 6226 | } |
2d66e025 MB |
6227 | |
6228 | return FALSE; | |
f85afd4e MB |
6229 | } |
6230 | ||
2d66e025 | 6231 | |
5c8fc7c1 | 6232 | bool wxGrid::MoveCursorLeft( bool expandSelection ) |
f85afd4e | 6233 | { |
2d66e025 MB |
6234 | if ( m_currentCellCoords != wxGridNoCellCoords && |
6235 | m_currentCellCoords.GetCol() > 0 ) | |
6236 | { | |
5c8fc7c1 | 6237 | if ( expandSelection ) |
d95b0c2b SN |
6238 | { |
6239 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
6240 | m_selectingKeyboard = m_currentCellCoords; | |
6241 | m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 ); | |
6b105754 SN |
6242 | MakeCellVisible( m_selectingKeyboard.GetRow(), |
6243 | m_selectingKeyboard.GetCol() ); | |
d95b0c2b SN |
6244 | SelectBlock( m_currentCellCoords, m_selectingKeyboard ); |
6245 | } | |
6246 | else | |
ca4f2b72 SN |
6247 | { |
6248 | ClearSelection(); | |
6b105754 SN |
6249 | MakeCellVisible( m_currentCellCoords.GetRow(), |
6250 | m_currentCellCoords.GetCol() - 1 ); | |
d95b0c2b SN |
6251 | SetCurrentCell( m_currentCellCoords.GetRow(), |
6252 | m_currentCellCoords.GetCol() - 1 ); | |
ca4f2b72 | 6253 | } |
2d66e025 MB |
6254 | return TRUE; |
6255 | } | |
6256 | ||
6257 | return FALSE; | |
6258 | } | |
6259 | ||
6260 | ||
5c8fc7c1 | 6261 | bool wxGrid::MoveCursorRight( bool expandSelection ) |
2d66e025 MB |
6262 | { |
6263 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
6264 | m_currentCellCoords.GetCol() < m_numCols - 1 ) | |
f85afd4e | 6265 | { |
5c8fc7c1 | 6266 | if ( expandSelection ) |
d95b0c2b SN |
6267 | { |
6268 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
6269 | m_selectingKeyboard = m_currentCellCoords; | |
6270 | m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 ); | |
6b105754 SN |
6271 | MakeCellVisible( m_selectingKeyboard.GetRow(), |
6272 | m_selectingKeyboard.GetCol() ); | |
d95b0c2b SN |
6273 | SelectBlock( m_currentCellCoords, m_selectingKeyboard ); |
6274 | } | |
6275 | else | |
ca4f2b72 SN |
6276 | { |
6277 | ClearSelection(); | |
6b105754 SN |
6278 | MakeCellVisible( m_currentCellCoords.GetRow(), |
6279 | m_currentCellCoords.GetCol() + 1 ); | |
6280 | SetCurrentCell( m_currentCellCoords.GetRow(), | |
d95b0c2b | 6281 | m_currentCellCoords.GetCol() + 1 ); |
ca4f2b72 | 6282 | } |
2d66e025 | 6283 | return TRUE; |
f85afd4e | 6284 | } |
8f177c8e | 6285 | |
2d66e025 MB |
6286 | return FALSE; |
6287 | } | |
6288 | ||
6289 | ||
6290 | bool wxGrid::MovePageUp() | |
6291 | { | |
6292 | if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE; | |
60ff3b99 | 6293 | |
2d66e025 MB |
6294 | int row = m_currentCellCoords.GetRow(); |
6295 | if ( row > 0 ) | |
f85afd4e | 6296 | { |
2d66e025 MB |
6297 | int cw, ch; |
6298 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 6299 | |
7c1cb261 | 6300 | int y = GetRowTop(row); |
2d66e025 MB |
6301 | int newRow = YToRow( y - ch + 1 ); |
6302 | if ( newRow == -1 ) | |
6303 | { | |
6304 | newRow = 0; | |
6305 | } | |
60ff3b99 | 6306 | else if ( newRow == row ) |
2d66e025 MB |
6307 | { |
6308 | newRow = row - 1; | |
6309 | } | |
8f177c8e | 6310 | |
2d66e025 MB |
6311 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); |
6312 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
60ff3b99 | 6313 | |
f85afd4e MB |
6314 | return TRUE; |
6315 | } | |
2d66e025 MB |
6316 | |
6317 | return FALSE; | |
6318 | } | |
6319 | ||
6320 | bool wxGrid::MovePageDown() | |
6321 | { | |
6322 | if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE; | |
60ff3b99 | 6323 | |
2d66e025 MB |
6324 | int row = m_currentCellCoords.GetRow(); |
6325 | if ( row < m_numRows ) | |
f85afd4e | 6326 | { |
2d66e025 MB |
6327 | int cw, ch; |
6328 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 6329 | |
7c1cb261 | 6330 | int y = GetRowTop(row); |
2d66e025 MB |
6331 | int newRow = YToRow( y + ch ); |
6332 | if ( newRow == -1 ) | |
6333 | { | |
6334 | newRow = m_numRows - 1; | |
6335 | } | |
60ff3b99 | 6336 | else if ( newRow == row ) |
2d66e025 MB |
6337 | { |
6338 | newRow = row + 1; | |
6339 | } | |
6340 | ||
6341 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); | |
6342 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
60ff3b99 | 6343 | |
2d66e025 | 6344 | return TRUE; |
f85afd4e | 6345 | } |
2d66e025 MB |
6346 | |
6347 | return FALSE; | |
f85afd4e | 6348 | } |
8f177c8e | 6349 | |
5c8fc7c1 | 6350 | bool wxGrid::MoveCursorUpBlock( bool expandSelection ) |
2d66e025 MB |
6351 | { |
6352 | if ( m_table && | |
6353 | m_currentCellCoords != wxGridNoCellCoords && | |
6354 | m_currentCellCoords.GetRow() > 0 ) | |
6355 | { | |
6356 | int row = m_currentCellCoords.GetRow(); | |
6357 | int col = m_currentCellCoords.GetCol(); | |
6358 | ||
6359 | if ( m_table->IsEmptyCell(row, col) ) | |
6360 | { | |
6361 | // starting in an empty cell: find the next block of | |
6362 | // non-empty cells | |
6363 | // | |
6364 | while ( row > 0 ) | |
6365 | { | |
6366 | row-- ; | |
6367 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
6368 | } | |
6369 | } | |
6370 | else if ( m_table->IsEmptyCell(row-1, col) ) | |
6371 | { | |
6372 | // starting at the top of a block: find the next block | |
6373 | // | |
6374 | row--; | |
6375 | while ( row > 0 ) | |
6376 | { | |
6377 | row-- ; | |
6378 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
6379 | } | |
6380 | } | |
6381 | else | |
6382 | { | |
6383 | // starting within a block: find the top of the block | |
6384 | // | |
6385 | while ( row > 0 ) | |
6386 | { | |
6387 | row-- ; | |
6388 | if ( m_table->IsEmptyCell(row, col) ) | |
6389 | { | |
6390 | row++ ; | |
6391 | break; | |
6392 | } | |
6393 | } | |
6394 | } | |
f85afd4e | 6395 | |
2d66e025 | 6396 | MakeCellVisible( row, col ); |
5c8fc7c1 | 6397 | if ( expandSelection ) |
d95b0c2b SN |
6398 | { |
6399 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
6400 | SelectBlock( m_currentCellCoords, m_selectingKeyboard ); | |
6401 | } | |
6402 | else | |
ca4f2b72 SN |
6403 | { |
6404 | ClearSelection(); | |
6405 | SetCurrentCell( row, col ); | |
6406 | } | |
2d66e025 MB |
6407 | return TRUE; |
6408 | } | |
f85afd4e | 6409 | |
2d66e025 MB |
6410 | return FALSE; |
6411 | } | |
f85afd4e | 6412 | |
5c8fc7c1 | 6413 | bool wxGrid::MoveCursorDownBlock( bool expandSelection ) |
f85afd4e | 6414 | { |
2d66e025 MB |
6415 | if ( m_table && |
6416 | m_currentCellCoords != wxGridNoCellCoords && | |
6417 | m_currentCellCoords.GetRow() < m_numRows-1 ) | |
f85afd4e | 6418 | { |
2d66e025 MB |
6419 | int row = m_currentCellCoords.GetRow(); |
6420 | int col = m_currentCellCoords.GetCol(); | |
6421 | ||
6422 | if ( m_table->IsEmptyCell(row, col) ) | |
6423 | { | |
6424 | // starting in an empty cell: find the next block of | |
6425 | // non-empty cells | |
6426 | // | |
6427 | while ( row < m_numRows-1 ) | |
6428 | { | |
6429 | row++ ; | |
6430 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
6431 | } | |
6432 | } | |
6433 | else if ( m_table->IsEmptyCell(row+1, col) ) | |
6434 | { | |
6435 | // starting at the bottom of a block: find the next block | |
6436 | // | |
6437 | row++; | |
6438 | while ( row < m_numRows-1 ) | |
6439 | { | |
6440 | row++ ; | |
6441 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
6442 | } | |
6443 | } | |
6444 | else | |
6445 | { | |
6446 | // starting within a block: find the bottom of the block | |
6447 | // | |
6448 | while ( row < m_numRows-1 ) | |
6449 | { | |
6450 | row++ ; | |
6451 | if ( m_table->IsEmptyCell(row, col) ) | |
6452 | { | |
6453 | row-- ; | |
6454 | break; | |
6455 | } | |
6456 | } | |
6457 | } | |
6458 | ||
6459 | MakeCellVisible( row, col ); | |
5c8fc7c1 | 6460 | if ( expandSelection ) |
d95b0c2b SN |
6461 | { |
6462 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
6463 | SelectBlock( m_currentCellCoords, m_selectingKeyboard ); | |
6464 | } | |
6465 | else | |
ca4f2b72 SN |
6466 | { |
6467 | ClearSelection(); | |
6468 | SetCurrentCell( row, col ); | |
6469 | } | |
2d66e025 MB |
6470 | |
6471 | return TRUE; | |
f85afd4e | 6472 | } |
f85afd4e | 6473 | |
2d66e025 MB |
6474 | return FALSE; |
6475 | } | |
f85afd4e | 6476 | |
5c8fc7c1 | 6477 | bool wxGrid::MoveCursorLeftBlock( bool expandSelection ) |
f85afd4e | 6478 | { |
2d66e025 MB |
6479 | if ( m_table && |
6480 | m_currentCellCoords != wxGridNoCellCoords && | |
6481 | m_currentCellCoords.GetCol() > 0 ) | |
f85afd4e | 6482 | { |
2d66e025 MB |
6483 | int row = m_currentCellCoords.GetRow(); |
6484 | int col = m_currentCellCoords.GetCol(); | |
6485 | ||
6486 | if ( m_table->IsEmptyCell(row, col) ) | |
6487 | { | |
6488 | // starting in an empty cell: find the next block of | |
6489 | // non-empty cells | |
6490 | // | |
6491 | while ( col > 0 ) | |
6492 | { | |
6493 | col-- ; | |
6494 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
6495 | } | |
6496 | } | |
6497 | else if ( m_table->IsEmptyCell(row, col-1) ) | |
6498 | { | |
6499 | // starting at the left of a block: find the next block | |
6500 | // | |
6501 | col--; | |
6502 | while ( col > 0 ) | |
6503 | { | |
6504 | col-- ; | |
6505 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
6506 | } | |
6507 | } | |
6508 | else | |
6509 | { | |
6510 | // starting within a block: find the left of the block | |
6511 | // | |
6512 | while ( col > 0 ) | |
6513 | { | |
6514 | col-- ; | |
6515 | if ( m_table->IsEmptyCell(row, col) ) | |
6516 | { | |
6517 | col++ ; | |
6518 | break; | |
6519 | } | |
6520 | } | |
6521 | } | |
f85afd4e | 6522 | |
2d66e025 | 6523 | MakeCellVisible( row, col ); |
5c8fc7c1 | 6524 | if ( expandSelection ) |
d95b0c2b SN |
6525 | { |
6526 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
6527 | SelectBlock( m_currentCellCoords, m_selectingKeyboard ); | |
6528 | } | |
6529 | else | |
ca4f2b72 SN |
6530 | { |
6531 | ClearSelection(); | |
6532 | SetCurrentCell( row, col ); | |
6533 | } | |
8f177c8e | 6534 | |
2d66e025 | 6535 | return TRUE; |
f85afd4e | 6536 | } |
2d66e025 MB |
6537 | |
6538 | return FALSE; | |
f85afd4e MB |
6539 | } |
6540 | ||
5c8fc7c1 | 6541 | bool wxGrid::MoveCursorRightBlock( bool expandSelection ) |
f85afd4e | 6542 | { |
2d66e025 MB |
6543 | if ( m_table && |
6544 | m_currentCellCoords != wxGridNoCellCoords && | |
6545 | m_currentCellCoords.GetCol() < m_numCols-1 ) | |
f85afd4e | 6546 | { |
2d66e025 MB |
6547 | int row = m_currentCellCoords.GetRow(); |
6548 | int col = m_currentCellCoords.GetCol(); | |
8f177c8e | 6549 | |
2d66e025 MB |
6550 | if ( m_table->IsEmptyCell(row, col) ) |
6551 | { | |
6552 | // starting in an empty cell: find the next block of | |
6553 | // non-empty cells | |
6554 | // | |
6555 | while ( col < m_numCols-1 ) | |
6556 | { | |
6557 | col++ ; | |
6558 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
6559 | } | |
6560 | } | |
6561 | else if ( m_table->IsEmptyCell(row, col+1) ) | |
6562 | { | |
6563 | // starting at the right of a block: find the next block | |
6564 | // | |
6565 | col++; | |
6566 | while ( col < m_numCols-1 ) | |
6567 | { | |
6568 | col++ ; | |
6569 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
6570 | } | |
6571 | } | |
6572 | else | |
6573 | { | |
6574 | // starting within a block: find the right of the block | |
6575 | // | |
6576 | while ( col < m_numCols-1 ) | |
6577 | { | |
6578 | col++ ; | |
6579 | if ( m_table->IsEmptyCell(row, col) ) | |
6580 | { | |
6581 | col-- ; | |
6582 | break; | |
6583 | } | |
6584 | } | |
6585 | } | |
8f177c8e | 6586 | |
2d66e025 | 6587 | MakeCellVisible( row, col ); |
5c8fc7c1 | 6588 | if ( expandSelection ) |
d95b0c2b SN |
6589 | { |
6590 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
6591 | SelectBlock( m_currentCellCoords, m_selectingKeyboard ); | |
6592 | } | |
6593 | else | |
ca4f2b72 SN |
6594 | { |
6595 | ClearSelection(); | |
6596 | SetCurrentCell( row, col ); | |
6597 | } | |
f85afd4e | 6598 | |
2d66e025 | 6599 | return TRUE; |
f85afd4e | 6600 | } |
2d66e025 MB |
6601 | |
6602 | return FALSE; | |
f85afd4e MB |
6603 | } |
6604 | ||
6605 | ||
2d66e025 | 6606 | |
f85afd4e | 6607 | // |
2d66e025 | 6608 | // ------ Label values and formatting |
f85afd4e MB |
6609 | // |
6610 | ||
6611 | void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) | |
6612 | { | |
6613 | *horiz = m_rowLabelHorizAlign; | |
6614 | *vert = m_rowLabelVertAlign; | |
6615 | } | |
6616 | ||
6617 | void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) | |
6618 | { | |
6619 | *horiz = m_colLabelHorizAlign; | |
6620 | *vert = m_colLabelVertAlign; | |
6621 | } | |
6622 | ||
6623 | wxString wxGrid::GetRowLabelValue( int row ) | |
6624 | { | |
6625 | if ( m_table ) | |
6626 | { | |
6627 | return m_table->GetRowLabelValue( row ); | |
6628 | } | |
6629 | else | |
6630 | { | |
6631 | wxString s; | |
6632 | s << row; | |
6633 | return s; | |
6634 | } | |
6635 | } | |
6636 | ||
6637 | wxString wxGrid::GetColLabelValue( int col ) | |
6638 | { | |
6639 | if ( m_table ) | |
6640 | { | |
6641 | return m_table->GetColLabelValue( col ); | |
6642 | } | |
6643 | else | |
6644 | { | |
6645 | wxString s; | |
6646 | s << col; | |
6647 | return s; | |
6648 | } | |
6649 | } | |
6650 | ||
2e8cd977 | 6651 | |
f85afd4e MB |
6652 | void wxGrid::SetRowLabelSize( int width ) |
6653 | { | |
2e8cd977 MB |
6654 | width = wxMax( width, 0 ); |
6655 | if ( width != m_rowLabelWidth ) | |
6656 | { | |
2e8cd977 MB |
6657 | if ( width == 0 ) |
6658 | { | |
6659 | m_rowLabelWin->Show( FALSE ); | |
7807d81c | 6660 | m_cornerLabelWin->Show( FALSE ); |
2e8cd977 | 6661 | } |
7807d81c | 6662 | else if ( m_rowLabelWidth == 0 ) |
2e8cd977 | 6663 | { |
7807d81c MB |
6664 | m_rowLabelWin->Show( TRUE ); |
6665 | if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE ); | |
2e8cd977 | 6666 | } |
b99be8fb | 6667 | |
2e8cd977 | 6668 | m_rowLabelWidth = width; |
7807d81c MB |
6669 | CalcWindowSizes(); |
6670 | Refresh( TRUE ); | |
2e8cd977 | 6671 | } |
f85afd4e MB |
6672 | } |
6673 | ||
2e8cd977 | 6674 | |
f85afd4e MB |
6675 | void wxGrid::SetColLabelSize( int height ) |
6676 | { | |
7807d81c | 6677 | height = wxMax( height, 0 ); |
2e8cd977 MB |
6678 | if ( height != m_colLabelHeight ) |
6679 | { | |
2e8cd977 MB |
6680 | if ( height == 0 ) |
6681 | { | |
2e8cd977 | 6682 | m_colLabelWin->Show( FALSE ); |
7807d81c | 6683 | m_cornerLabelWin->Show( FALSE ); |
2e8cd977 | 6684 | } |
7807d81c | 6685 | else if ( m_colLabelHeight == 0 ) |
2e8cd977 | 6686 | { |
7807d81c MB |
6687 | m_colLabelWin->Show( TRUE ); |
6688 | if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE ); | |
2e8cd977 | 6689 | } |
7807d81c | 6690 | |
2e8cd977 | 6691 | m_colLabelHeight = height; |
7807d81c MB |
6692 | CalcWindowSizes(); |
6693 | Refresh( TRUE ); | |
2e8cd977 | 6694 | } |
f85afd4e MB |
6695 | } |
6696 | ||
2e8cd977 | 6697 | |
f85afd4e MB |
6698 | void wxGrid::SetLabelBackgroundColour( const wxColour& colour ) |
6699 | { | |
2d66e025 MB |
6700 | if ( m_labelBackgroundColour != colour ) |
6701 | { | |
6702 | m_labelBackgroundColour = colour; | |
6703 | m_rowLabelWin->SetBackgroundColour( colour ); | |
6704 | m_colLabelWin->SetBackgroundColour( colour ); | |
6705 | m_cornerLabelWin->SetBackgroundColour( colour ); | |
6706 | ||
6707 | if ( !GetBatchCount() ) | |
6708 | { | |
6709 | m_rowLabelWin->Refresh(); | |
6710 | m_colLabelWin->Refresh(); | |
6711 | m_cornerLabelWin->Refresh(); | |
6712 | } | |
6713 | } | |
f85afd4e MB |
6714 | } |
6715 | ||
6716 | void wxGrid::SetLabelTextColour( const wxColour& colour ) | |
6717 | { | |
2d66e025 MB |
6718 | if ( m_labelTextColour != colour ) |
6719 | { | |
6720 | m_labelTextColour = colour; | |
6721 | if ( !GetBatchCount() ) | |
6722 | { | |
6723 | m_rowLabelWin->Refresh(); | |
6724 | m_colLabelWin->Refresh(); | |
6725 | } | |
6726 | } | |
f85afd4e MB |
6727 | } |
6728 | ||
6729 | void wxGrid::SetLabelFont( const wxFont& font ) | |
6730 | { | |
6731 | m_labelFont = font; | |
2d66e025 MB |
6732 | if ( !GetBatchCount() ) |
6733 | { | |
6734 | m_rowLabelWin->Refresh(); | |
6735 | m_colLabelWin->Refresh(); | |
6736 | } | |
f85afd4e MB |
6737 | } |
6738 | ||
6739 | void wxGrid::SetRowLabelAlignment( int horiz, int vert ) | |
6740 | { | |
6741 | if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT ) | |
6742 | { | |
6743 | m_rowLabelHorizAlign = horiz; | |
6744 | } | |
8f177c8e | 6745 | |
f85afd4e MB |
6746 | if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM ) |
6747 | { | |
6748 | m_rowLabelVertAlign = vert; | |
6749 | } | |
6750 | ||
2d66e025 MB |
6751 | if ( !GetBatchCount() ) |
6752 | { | |
6753 | m_rowLabelWin->Refresh(); | |
60ff3b99 | 6754 | } |
f85afd4e MB |
6755 | } |
6756 | ||
6757 | void wxGrid::SetColLabelAlignment( int horiz, int vert ) | |
6758 | { | |
6759 | if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT ) | |
6760 | { | |
6761 | m_colLabelHorizAlign = horiz; | |
6762 | } | |
8f177c8e | 6763 | |
f85afd4e MB |
6764 | if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM ) |
6765 | { | |
6766 | m_colLabelVertAlign = vert; | |
6767 | } | |
6768 | ||
2d66e025 MB |
6769 | if ( !GetBatchCount() ) |
6770 | { | |
2d66e025 | 6771 | m_colLabelWin->Refresh(); |
60ff3b99 | 6772 | } |
f85afd4e MB |
6773 | } |
6774 | ||
6775 | void wxGrid::SetRowLabelValue( int row, const wxString& s ) | |
6776 | { | |
6777 | if ( m_table ) | |
6778 | { | |
6779 | m_table->SetRowLabelValue( row, s ); | |
2d66e025 MB |
6780 | if ( !GetBatchCount() ) |
6781 | { | |
70c7a608 SN |
6782 | wxRect rect = CellToRect( row, 0); |
6783 | if ( rect.height > 0 ) | |
6784 | { | |
cb309039 | 6785 | CalcScrolledPosition(0, rect.y, &rect.x, &rect.y); |
70c7a608 SN |
6786 | rect.x = m_left; |
6787 | rect.width = m_rowLabelWidth; | |
6788 | m_rowLabelWin->Refresh( TRUE, &rect ); | |
6789 | } | |
2d66e025 | 6790 | } |
f85afd4e MB |
6791 | } |
6792 | } | |
6793 | ||
6794 | void wxGrid::SetColLabelValue( int col, const wxString& s ) | |
6795 | { | |
6796 | if ( m_table ) | |
6797 | { | |
6798 | m_table->SetColLabelValue( col, s ); | |
2d66e025 MB |
6799 | if ( !GetBatchCount() ) |
6800 | { | |
70c7a608 SN |
6801 | wxRect rect = CellToRect( 0, col ); |
6802 | if ( rect.width > 0 ) | |
6803 | { | |
cb309039 | 6804 | CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y); |
70c7a608 SN |
6805 | rect.y = m_top; |
6806 | rect.height = m_colLabelHeight; | |
6807 | m_colLabelWin->Refresh( TRUE, &rect ); | |
6808 | } | |
2d66e025 | 6809 | } |
f85afd4e MB |
6810 | } |
6811 | } | |
6812 | ||
6813 | void wxGrid::SetGridLineColour( const wxColour& colour ) | |
6814 | { | |
2d66e025 MB |
6815 | if ( m_gridLineColour != colour ) |
6816 | { | |
6817 | m_gridLineColour = colour; | |
60ff3b99 | 6818 | |
2d66e025 MB |
6819 | wxClientDC dc( m_gridWin ); |
6820 | PrepareDC( dc ); | |
c6a51dcd | 6821 | DrawAllGridLines( dc, wxRegion() ); |
2d66e025 | 6822 | } |
f85afd4e MB |
6823 | } |
6824 | ||
6825 | void wxGrid::EnableGridLines( bool enable ) | |
6826 | { | |
6827 | if ( enable != m_gridLinesEnabled ) | |
6828 | { | |
6829 | m_gridLinesEnabled = enable; | |
2d66e025 MB |
6830 | |
6831 | if ( !GetBatchCount() ) | |
6832 | { | |
6833 | if ( enable ) | |
6834 | { | |
6835 | wxClientDC dc( m_gridWin ); | |
6836 | PrepareDC( dc ); | |
c6a51dcd | 6837 | DrawAllGridLines( dc, wxRegion() ); |
2d66e025 MB |
6838 | } |
6839 | else | |
6840 | { | |
6841 | m_gridWin->Refresh(); | |
6842 | } | |
6843 | } | |
f85afd4e MB |
6844 | } |
6845 | } | |
6846 | ||
6847 | ||
6848 | int wxGrid::GetDefaultRowSize() | |
6849 | { | |
6850 | return m_defaultRowHeight; | |
6851 | } | |
6852 | ||
6853 | int wxGrid::GetRowSize( int row ) | |
6854 | { | |
b99be8fb VZ |
6855 | wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") ); |
6856 | ||
7c1cb261 | 6857 | return GetRowHeight(row); |
f85afd4e MB |
6858 | } |
6859 | ||
6860 | int wxGrid::GetDefaultColSize() | |
6861 | { | |
6862 | return m_defaultColWidth; | |
6863 | } | |
6864 | ||
6865 | int wxGrid::GetColSize( int col ) | |
6866 | { | |
b99be8fb VZ |
6867 | wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") ); |
6868 | ||
7c1cb261 | 6869 | return GetColWidth(col); |
f85afd4e MB |
6870 | } |
6871 | ||
2e9a6788 VZ |
6872 | // ============================================================================ |
6873 | // access to the grid attributes: each of them has a default value in the grid | |
6874 | // itself and may be overidden on a per-cell basis | |
6875 | // ============================================================================ | |
6876 | ||
6877 | // ---------------------------------------------------------------------------- | |
6878 | // setting default attributes | |
6879 | // ---------------------------------------------------------------------------- | |
6880 | ||
6881 | void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col ) | |
6882 | { | |
2796cce3 | 6883 | m_defaultCellAttr->SetBackgroundColour(col); |
c916e13b RR |
6884 | #ifdef __WXGTK__ |
6885 | m_gridWin->SetBackgroundColour(col); | |
6886 | #endif | |
2e9a6788 VZ |
6887 | } |
6888 | ||
6889 | void wxGrid::SetDefaultCellTextColour( const wxColour& col ) | |
6890 | { | |
2796cce3 | 6891 | m_defaultCellAttr->SetTextColour(col); |
2e9a6788 VZ |
6892 | } |
6893 | ||
6894 | void wxGrid::SetDefaultCellAlignment( int horiz, int vert ) | |
6895 | { | |
2796cce3 | 6896 | m_defaultCellAttr->SetAlignment(horiz, vert); |
2e9a6788 VZ |
6897 | } |
6898 | ||
6899 | void wxGrid::SetDefaultCellFont( const wxFont& font ) | |
6900 | { | |
2796cce3 RD |
6901 | m_defaultCellAttr->SetFont(font); |
6902 | } | |
6903 | ||
0ba143c9 RD |
6904 | void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer) |
6905 | { | |
6906 | m_defaultCellAttr->SetRenderer(renderer); | |
6907 | } | |
2e9a6788 | 6908 | |
0ba143c9 RD |
6909 | void wxGrid::SetDefaultEditor(wxGridCellEditor *editor) |
6910 | { | |
6911 | m_defaultCellAttr->SetEditor(editor); | |
6912 | } | |
9b4aede2 | 6913 | |
2e9a6788 VZ |
6914 | // ---------------------------------------------------------------------------- |
6915 | // access to the default attrbiutes | |
6916 | // ---------------------------------------------------------------------------- | |
6917 | ||
f85afd4e MB |
6918 | wxColour wxGrid::GetDefaultCellBackgroundColour() |
6919 | { | |
2796cce3 | 6920 | return m_defaultCellAttr->GetBackgroundColour(); |
f85afd4e MB |
6921 | } |
6922 | ||
2e9a6788 VZ |
6923 | wxColour wxGrid::GetDefaultCellTextColour() |
6924 | { | |
2796cce3 | 6925 | return m_defaultCellAttr->GetTextColour(); |
2e9a6788 VZ |
6926 | } |
6927 | ||
6928 | wxFont wxGrid::GetDefaultCellFont() | |
6929 | { | |
2796cce3 | 6930 | return m_defaultCellAttr->GetFont(); |
2e9a6788 VZ |
6931 | } |
6932 | ||
6933 | void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) | |
6934 | { | |
2796cce3 | 6935 | m_defaultCellAttr->GetAlignment(horiz, vert); |
2e9a6788 VZ |
6936 | } |
6937 | ||
0ba143c9 RD |
6938 | wxGridCellRenderer *wxGrid::GetDefaultRenderer() const |
6939 | { | |
0b190b0f | 6940 | return m_defaultCellAttr->GetRenderer(NULL, 0, 0); |
0ba143c9 | 6941 | } |
ab79958a | 6942 | |
0ba143c9 RD |
6943 | wxGridCellEditor *wxGrid::GetDefaultEditor() const |
6944 | { | |
28a77bc4 | 6945 | return m_defaultCellAttr->GetEditor(NULL,0,0); |
0ba143c9 | 6946 | } |
9b4aede2 | 6947 | |
2e9a6788 VZ |
6948 | // ---------------------------------------------------------------------------- |
6949 | // access to cell attributes | |
6950 | // ---------------------------------------------------------------------------- | |
6951 | ||
b99be8fb | 6952 | wxColour wxGrid::GetCellBackgroundColour(int row, int col) |
f85afd4e | 6953 | { |
0a976765 | 6954 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 6955 | wxColour colour = attr->GetBackgroundColour(); |
39bcce60 | 6956 | attr->DecRef(); |
b99be8fb | 6957 | return colour; |
f85afd4e MB |
6958 | } |
6959 | ||
b99be8fb | 6960 | wxColour wxGrid::GetCellTextColour( int row, int col ) |
f85afd4e | 6961 | { |
0a976765 | 6962 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 6963 | wxColour colour = attr->GetTextColour(); |
39bcce60 | 6964 | attr->DecRef(); |
b99be8fb | 6965 | return colour; |
f85afd4e MB |
6966 | } |
6967 | ||
b99be8fb | 6968 | wxFont wxGrid::GetCellFont( int row, int col ) |
f85afd4e | 6969 | { |
0a976765 | 6970 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 6971 | wxFont font = attr->GetFont(); |
39bcce60 | 6972 | attr->DecRef(); |
b99be8fb | 6973 | return font; |
f85afd4e MB |
6974 | } |
6975 | ||
b99be8fb | 6976 | void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) |
f85afd4e | 6977 | { |
0a976765 | 6978 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 6979 | attr->GetAlignment(horiz, vert); |
39bcce60 | 6980 | attr->DecRef(); |
2e9a6788 VZ |
6981 | } |
6982 | ||
2796cce3 RD |
6983 | wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) |
6984 | { | |
6985 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 6986 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); |
2796cce3 | 6987 | attr->DecRef(); |
0b190b0f | 6988 | |
2796cce3 RD |
6989 | return renderer; |
6990 | } | |
6991 | ||
9b4aede2 RD |
6992 | wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) |
6993 | { | |
6994 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 6995 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
9b4aede2 | 6996 | attr->DecRef(); |
0b190b0f | 6997 | |
9b4aede2 RD |
6998 | return editor; |
6999 | } | |
7000 | ||
283b7808 VZ |
7001 | bool wxGrid::IsReadOnly(int row, int col) const |
7002 | { | |
7003 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
7004 | bool isReadOnly = attr->IsReadOnly(); | |
7005 | attr->DecRef(); | |
7006 | return isReadOnly; | |
7007 | } | |
7008 | ||
2e9a6788 | 7009 | // ---------------------------------------------------------------------------- |
758cbedf | 7010 | // attribute support: cache, automatic provider creation, ... |
2e9a6788 VZ |
7011 | // ---------------------------------------------------------------------------- |
7012 | ||
7013 | bool wxGrid::CanHaveAttributes() | |
7014 | { | |
7015 | if ( !m_table ) | |
7016 | { | |
7017 | return FALSE; | |
7018 | } | |
7019 | ||
f2d76237 | 7020 | return m_table->CanHaveAttributes(); |
2e9a6788 VZ |
7021 | } |
7022 | ||
0a976765 VZ |
7023 | void wxGrid::ClearAttrCache() |
7024 | { | |
7025 | if ( m_attrCache.row != -1 ) | |
7026 | { | |
39bcce60 | 7027 | wxSafeDecRef(m_attrCache.attr); |
0a976765 VZ |
7028 | m_attrCache.row = -1; |
7029 | } | |
7030 | } | |
7031 | ||
7032 | void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const | |
7033 | { | |
7034 | wxGrid *self = (wxGrid *)this; // const_cast | |
7035 | ||
7036 | self->ClearAttrCache(); | |
7037 | self->m_attrCache.row = row; | |
7038 | self->m_attrCache.col = col; | |
7039 | self->m_attrCache.attr = attr; | |
39bcce60 | 7040 | wxSafeIncRef(attr); |
0a976765 VZ |
7041 | } |
7042 | ||
7043 | bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const | |
7044 | { | |
7045 | if ( row == m_attrCache.row && col == m_attrCache.col ) | |
7046 | { | |
7047 | *attr = m_attrCache.attr; | |
39bcce60 | 7048 | wxSafeIncRef(m_attrCache.attr); |
0a976765 VZ |
7049 | |
7050 | #ifdef DEBUG_ATTR_CACHE | |
7051 | gs_nAttrCacheHits++; | |
7052 | #endif | |
7053 | ||
7054 | return TRUE; | |
7055 | } | |
7056 | else | |
7057 | { | |
7058 | #ifdef DEBUG_ATTR_CACHE | |
7059 | gs_nAttrCacheMisses++; | |
7060 | #endif | |
7061 | return FALSE; | |
7062 | } | |
7063 | } | |
7064 | ||
2e9a6788 VZ |
7065 | wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const |
7066 | { | |
0a976765 VZ |
7067 | wxGridCellAttr *attr; |
7068 | if ( !LookupAttr(row, col, &attr) ) | |
2e9a6788 | 7069 | { |
0a976765 VZ |
7070 | attr = m_table ? m_table->GetAttr(row, col) : (wxGridCellAttr *)NULL; |
7071 | CacheAttr(row, col, attr); | |
7072 | } | |
508011ce VZ |
7073 | if (attr) |
7074 | { | |
2796cce3 | 7075 | attr->SetDefAttr(m_defaultCellAttr); |
508011ce VZ |
7076 | } |
7077 | else | |
7078 | { | |
2796cce3 RD |
7079 | attr = m_defaultCellAttr; |
7080 | attr->IncRef(); | |
7081 | } | |
2e9a6788 | 7082 | |
0a976765 VZ |
7083 | return attr; |
7084 | } | |
7085 | ||
7086 | wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const | |
7087 | { | |
7088 | wxGridCellAttr *attr; | |
7089 | if ( !LookupAttr(row, col, &attr) || !attr ) | |
7090 | { | |
7091 | wxASSERT_MSG( m_table, | |
7092 | _T("we may only be called if CanHaveAttributes() " | |
7093 | "returned TRUE and then m_table should be !NULL") ); | |
7094 | ||
7095 | attr = m_table->GetAttr(row, col); | |
7096 | if ( !attr ) | |
7097 | { | |
7098 | attr = new wxGridCellAttr; | |
7099 | ||
7100 | // artificially inc the ref count to match DecRef() in caller | |
7101 | attr->IncRef(); | |
7102 | ||
7103 | m_table->SetAttr(attr, row, col); | |
7104 | } | |
2e9a6788 | 7105 | |
0a976765 | 7106 | CacheAttr(row, col, attr); |
2e9a6788 | 7107 | } |
2796cce3 | 7108 | attr->SetDefAttr(m_defaultCellAttr); |
2e9a6788 VZ |
7109 | return attr; |
7110 | } | |
7111 | ||
0b190b0f VZ |
7112 | // ---------------------------------------------------------------------------- |
7113 | // setting column attributes (wrappers around SetColAttr) | |
7114 | // ---------------------------------------------------------------------------- | |
7115 | ||
7116 | void wxGrid::SetColFormatBool(int col) | |
7117 | { | |
7118 | SetColFormatCustom(col, wxGRID_VALUE_BOOL); | |
7119 | } | |
7120 | ||
7121 | void wxGrid::SetColFormatNumber(int col) | |
7122 | { | |
7123 | SetColFormatCustom(col, wxGRID_VALUE_NUMBER); | |
7124 | } | |
7125 | ||
7126 | void wxGrid::SetColFormatFloat(int col, int width, int precision) | |
7127 | { | |
7128 | wxString typeName = wxGRID_VALUE_FLOAT; | |
7129 | if ( (width != -1) || (precision != -1) ) | |
7130 | { | |
7131 | typeName << _T(':') << width << _T(',') << precision; | |
7132 | } | |
7133 | ||
7134 | SetColFormatCustom(col, typeName); | |
7135 | } | |
7136 | ||
7137 | void wxGrid::SetColFormatCustom(int col, const wxString& typeName) | |
7138 | { | |
7139 | wxGridCellAttr *attr = new wxGridCellAttr; | |
7140 | wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName); | |
7141 | attr->SetRenderer(renderer); | |
7142 | ||
7143 | SetColAttr(col, attr); | |
7144 | } | |
7145 | ||
758cbedf VZ |
7146 | // ---------------------------------------------------------------------------- |
7147 | // setting cell attributes: this is forwarded to the table | |
7148 | // ---------------------------------------------------------------------------- | |
7149 | ||
7150 | void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr) | |
7151 | { | |
7152 | if ( CanHaveAttributes() ) | |
7153 | { | |
7154 | m_table->SetRowAttr(attr, row); | |
7155 | } | |
7156 | else | |
7157 | { | |
39bcce60 | 7158 | wxSafeDecRef(attr); |
758cbedf VZ |
7159 | } |
7160 | } | |
7161 | ||
7162 | void wxGrid::SetColAttr(int col, wxGridCellAttr *attr) | |
7163 | { | |
7164 | if ( CanHaveAttributes() ) | |
7165 | { | |
7166 | m_table->SetColAttr(attr, col); | |
7167 | } | |
7168 | else | |
7169 | { | |
39bcce60 | 7170 | wxSafeDecRef(attr); |
758cbedf VZ |
7171 | } |
7172 | } | |
7173 | ||
2e9a6788 VZ |
7174 | void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour ) |
7175 | { | |
7176 | if ( CanHaveAttributes() ) | |
7177 | { | |
0a976765 | 7178 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7179 | attr->SetBackgroundColour(colour); |
7180 | attr->DecRef(); | |
7181 | } | |
7182 | } | |
7183 | ||
7184 | void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour ) | |
7185 | { | |
7186 | if ( CanHaveAttributes() ) | |
7187 | { | |
0a976765 | 7188 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7189 | attr->SetTextColour(colour); |
7190 | attr->DecRef(); | |
7191 | } | |
7192 | } | |
7193 | ||
7194 | void wxGrid::SetCellFont( int row, int col, const wxFont& font ) | |
7195 | { | |
7196 | if ( CanHaveAttributes() ) | |
7197 | { | |
0a976765 | 7198 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7199 | attr->SetFont(font); |
7200 | attr->DecRef(); | |
7201 | } | |
7202 | } | |
7203 | ||
7204 | void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert ) | |
7205 | { | |
7206 | if ( CanHaveAttributes() ) | |
7207 | { | |
0a976765 | 7208 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7209 | attr->SetAlignment(horiz, vert); |
7210 | attr->DecRef(); | |
ab79958a VZ |
7211 | } |
7212 | } | |
7213 | ||
7214 | void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer) | |
7215 | { | |
7216 | if ( CanHaveAttributes() ) | |
7217 | { | |
0a976765 | 7218 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
ab79958a VZ |
7219 | attr->SetRenderer(renderer); |
7220 | attr->DecRef(); | |
9b4aede2 RD |
7221 | } |
7222 | } | |
7223 | ||
7224 | void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor) | |
7225 | { | |
7226 | if ( CanHaveAttributes() ) | |
7227 | { | |
7228 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7229 | attr->SetEditor(editor); | |
7230 | attr->DecRef(); | |
283b7808 VZ |
7231 | } |
7232 | } | |
7233 | ||
7234 | void wxGrid::SetReadOnly(int row, int col, bool isReadOnly) | |
7235 | { | |
7236 | if ( CanHaveAttributes() ) | |
7237 | { | |
7238 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7239 | attr->SetReadOnly(isReadOnly); | |
7240 | attr->DecRef(); | |
2e9a6788 | 7241 | } |
f85afd4e MB |
7242 | } |
7243 | ||
f2d76237 RD |
7244 | // ---------------------------------------------------------------------------- |
7245 | // Data type registration | |
7246 | // ---------------------------------------------------------------------------- | |
7247 | ||
7248 | void wxGrid::RegisterDataType(const wxString& typeName, | |
7249 | wxGridCellRenderer* renderer, | |
7250 | wxGridCellEditor* editor) | |
7251 | { | |
7252 | m_typeRegistry->RegisterDataType(typeName, renderer, editor); | |
7253 | } | |
7254 | ||
7255 | ||
99306db2 | 7256 | wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const |
f2d76237 RD |
7257 | { |
7258 | wxString typeName = m_table->GetTypeName(row, col); | |
7259 | return GetDefaultEditorForType(typeName); | |
7260 | } | |
7261 | ||
99306db2 | 7262 | wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const |
f2d76237 RD |
7263 | { |
7264 | wxString typeName = m_table->GetTypeName(row, col); | |
7265 | return GetDefaultRendererForType(typeName); | |
7266 | } | |
7267 | ||
99306db2 VZ |
7268 | wxGridCellEditor* |
7269 | wxGrid::GetDefaultEditorForType(const wxString& typeName) const | |
f2d76237 | 7270 | { |
c4608a8a | 7271 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
7272 | if ( index == wxNOT_FOUND ) |
7273 | { | |
7274 | wxFAIL_MSG(wxT("Unknown data type name")); | |
7275 | ||
f2d76237 RD |
7276 | return NULL; |
7277 | } | |
0b190b0f | 7278 | |
f2d76237 RD |
7279 | return m_typeRegistry->GetEditor(index); |
7280 | } | |
7281 | ||
99306db2 VZ |
7282 | wxGridCellRenderer* |
7283 | wxGrid::GetDefaultRendererForType(const wxString& typeName) const | |
f2d76237 | 7284 | { |
c4608a8a | 7285 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
7286 | if ( index == wxNOT_FOUND ) |
7287 | { | |
c4608a8a | 7288 | wxFAIL_MSG(wxT("Unknown data type name")); |
0b190b0f | 7289 | |
c4608a8a | 7290 | return NULL; |
e72b4213 | 7291 | } |
0b190b0f | 7292 | |
c4608a8a | 7293 | return m_typeRegistry->GetRenderer(index); |
f2d76237 RD |
7294 | } |
7295 | ||
7296 | ||
2e9a6788 VZ |
7297 | // ---------------------------------------------------------------------------- |
7298 | // row/col size | |
7299 | // ---------------------------------------------------------------------------- | |
7300 | ||
6e8524b1 MB |
7301 | void wxGrid::EnableDragRowSize( bool enable ) |
7302 | { | |
7303 | m_canDragRowSize = enable; | |
7304 | } | |
7305 | ||
7306 | ||
7307 | void wxGrid::EnableDragColSize( bool enable ) | |
7308 | { | |
7309 | m_canDragColSize = enable; | |
7310 | } | |
7311 | ||
4cfa5de6 RD |
7312 | void wxGrid::EnableDragGridSize( bool enable ) |
7313 | { | |
7314 | m_canDragGridSize = enable; | |
7315 | } | |
7316 | ||
6e8524b1 | 7317 | |
f85afd4e MB |
7318 | void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) |
7319 | { | |
7320 | m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT ); | |
7321 | ||
7322 | if ( resizeExistingRows ) | |
7323 | { | |
7c1cb261 VZ |
7324 | InitRowHeights(); |
7325 | ||
f85afd4e | 7326 | CalcDimensions(); |
f85afd4e MB |
7327 | } |
7328 | } | |
7329 | ||
7330 | void wxGrid::SetRowSize( int row, int height ) | |
7331 | { | |
b99be8fb | 7332 | wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") ); |
60ff3b99 | 7333 | |
7c1cb261 VZ |
7334 | if ( m_rowHeights.IsEmpty() ) |
7335 | { | |
7336 | // need to really create the array | |
7337 | InitRowHeights(); | |
7338 | } | |
60ff3b99 | 7339 | |
b99be8fb VZ |
7340 | int h = wxMax( 0, height ); |
7341 | int diff = h - m_rowHeights[row]; | |
60ff3b99 | 7342 | |
b99be8fb | 7343 | m_rowHeights[row] = h; |
7c1cb261 | 7344 | int i; |
b99be8fb | 7345 | for ( i = row; i < m_numRows; i++ ) |
f85afd4e | 7346 | { |
b99be8fb | 7347 | m_rowBottoms[i] += diff; |
f85afd4e | 7348 | } |
b99be8fb | 7349 | CalcDimensions(); |
f85afd4e MB |
7350 | } |
7351 | ||
7352 | void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) | |
7353 | { | |
7354 | m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH ); | |
7355 | ||
7356 | if ( resizeExistingCols ) | |
7357 | { | |
7c1cb261 VZ |
7358 | InitColWidths(); |
7359 | ||
f85afd4e | 7360 | CalcDimensions(); |
f85afd4e MB |
7361 | } |
7362 | } | |
7363 | ||
7364 | void wxGrid::SetColSize( int col, int width ) | |
7365 | { | |
b99be8fb | 7366 | wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") ); |
60ff3b99 | 7367 | |
43947979 VZ |
7368 | // should we check that it's bigger than GetColMinimalWidth(col) here? |
7369 | ||
7c1cb261 VZ |
7370 | if ( m_colWidths.IsEmpty() ) |
7371 | { | |
7372 | // need to really create the array | |
7373 | InitColWidths(); | |
7374 | } | |
f85afd4e | 7375 | |
b99be8fb VZ |
7376 | int w = wxMax( 0, width ); |
7377 | int diff = w - m_colWidths[col]; | |
7378 | m_colWidths[col] = w; | |
60ff3b99 | 7379 | |
7c1cb261 | 7380 | int i; |
b99be8fb | 7381 | for ( i = col; i < m_numCols; i++ ) |
f85afd4e | 7382 | { |
b99be8fb | 7383 | m_colRights[i] += diff; |
f85afd4e | 7384 | } |
b99be8fb | 7385 | CalcDimensions(); |
f85afd4e MB |
7386 | } |
7387 | ||
2d66e025 | 7388 | |
43947979 VZ |
7389 | void wxGrid::SetColMinimalWidth( int col, int width ) |
7390 | { | |
af547d51 VZ |
7391 | m_colMinWidths.Put(col, width); |
7392 | } | |
7393 | ||
7394 | void wxGrid::SetRowMinimalHeight( int row, int width ) | |
7395 | { | |
7396 | m_rowMinHeights.Put(row, width); | |
43947979 VZ |
7397 | } |
7398 | ||
7399 | int wxGrid::GetColMinimalWidth(int col) const | |
7400 | { | |
af547d51 VZ |
7401 | long value = m_colMinWidths.Get(col); |
7402 | return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH; | |
7403 | } | |
7404 | ||
7405 | int wxGrid::GetRowMinimalHeight(int row) const | |
7406 | { | |
7407 | long value = m_rowMinHeights.Get(row); | |
7408 | return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT; | |
43947979 VZ |
7409 | } |
7410 | ||
57c086ef VZ |
7411 | // ---------------------------------------------------------------------------- |
7412 | // auto sizing | |
7413 | // ---------------------------------------------------------------------------- | |
7414 | ||
af547d51 | 7415 | void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) |
65e4e78e VZ |
7416 | { |
7417 | wxClientDC dc(m_gridWin); | |
7418 | ||
a95e38c0 VZ |
7419 | // init both of them to avoid compiler warnings, even if weo nly need one |
7420 | int row = -1, | |
7421 | col = -1; | |
af547d51 VZ |
7422 | if ( column ) |
7423 | col = colOrRow; | |
7424 | else | |
7425 | row = colOrRow; | |
7426 | ||
7427 | wxCoord extent, extentMax = 0; | |
7428 | int max = column ? m_numRows : m_numCols; | |
39bcce60 | 7429 | for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ ) |
65e4e78e | 7430 | { |
af547d51 VZ |
7431 | if ( column ) |
7432 | row = rowOrCol; | |
7433 | else | |
7434 | col = rowOrCol; | |
7435 | ||
65e4e78e | 7436 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 7437 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); |
65e4e78e VZ |
7438 | if ( renderer ) |
7439 | { | |
af547d51 VZ |
7440 | wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col); |
7441 | extent = column ? size.x : size.y; | |
7442 | if ( extent > extentMax ) | |
65e4e78e | 7443 | { |
af547d51 | 7444 | extentMax = extent; |
65e4e78e | 7445 | } |
0b190b0f VZ |
7446 | |
7447 | renderer->DecRef(); | |
65e4e78e VZ |
7448 | } |
7449 | ||
7450 | attr->DecRef(); | |
7451 | } | |
7452 | ||
af547d51 VZ |
7453 | // now also compare with the column label extent |
7454 | wxCoord w, h; | |
65e4e78e | 7455 | dc.SetFont( GetLabelFont() ); |
294d195c MB |
7456 | |
7457 | if ( column ) | |
7458 | dc.GetTextExtent( GetColLabelValue(col), &w, &h ); | |
7459 | else | |
7460 | dc.GetTextExtent( GetRowLabelValue(col), &w, &h ); | |
7461 | ||
af547d51 VZ |
7462 | extent = column ? w : h; |
7463 | if ( extent > extentMax ) | |
65e4e78e | 7464 | { |
af547d51 | 7465 | extentMax = extent; |
65e4e78e VZ |
7466 | } |
7467 | ||
af547d51 | 7468 | if ( !extentMax ) |
65e4e78e | 7469 | { |
af547d51 VZ |
7470 | // empty column - give default extent (notice that if extentMax is less |
7471 | // than default extent but != 0, it's ok) | |
7472 | extentMax = column ? m_defaultColWidth : m_defaultRowHeight; | |
65e4e78e VZ |
7473 | } |
7474 | else | |
7475 | { | |
a95e38c0 VZ |
7476 | if ( column ) |
7477 | { | |
7478 | // leave some space around text | |
7479 | extentMax += 10; | |
7480 | } | |
65e4e78e VZ |
7481 | } |
7482 | ||
af547d51 VZ |
7483 | if ( column ) |
7484 | SetColSize(col, extentMax); | |
7485 | else | |
39bcce60 | 7486 | SetRowSize(row, extentMax); |
af547d51 | 7487 | |
65e4e78e VZ |
7488 | if ( setAsMin ) |
7489 | { | |
af547d51 VZ |
7490 | if ( column ) |
7491 | SetColMinimalWidth(col, extentMax); | |
7492 | else | |
39bcce60 | 7493 | SetRowMinimalHeight(row, extentMax); |
65e4e78e VZ |
7494 | } |
7495 | } | |
7496 | ||
266e8367 | 7497 | int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) |
65e4e78e | 7498 | { |
57c086ef VZ |
7499 | int width = m_rowLabelWidth; |
7500 | ||
65e4e78e VZ |
7501 | for ( int col = 0; col < m_numCols; col++ ) |
7502 | { | |
266e8367 VZ |
7503 | if ( !calcOnly ) |
7504 | { | |
7505 | AutoSizeColumn(col, setAsMin); | |
7506 | } | |
57c086ef VZ |
7507 | |
7508 | width += GetColWidth(col); | |
65e4e78e | 7509 | } |
57c086ef | 7510 | |
266e8367 | 7511 | return width; |
65e4e78e VZ |
7512 | } |
7513 | ||
266e8367 | 7514 | int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin) |
57c086ef VZ |
7515 | { |
7516 | int height = m_colLabelHeight; | |
7517 | ||
7518 | for ( int row = 0; row < m_numRows; row++ ) | |
7519 | { | |
af547d51 VZ |
7520 | if ( !calcOnly ) |
7521 | { | |
7522 | AutoSizeRow(row, setAsMin); | |
7523 | } | |
57c086ef VZ |
7524 | |
7525 | height += GetRowHeight(row); | |
7526 | } | |
7527 | ||
266e8367 | 7528 | return height; |
57c086ef VZ |
7529 | } |
7530 | ||
7531 | void wxGrid::AutoSize() | |
7532 | { | |
266e8367 VZ |
7533 | // set the size too |
7534 | SetSize(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE)); | |
7535 | } | |
7536 | ||
7537 | wxSize wxGrid::DoGetBestSize() const | |
7538 | { | |
7539 | // don't set sizes, only calculate them | |
7540 | wxGrid *self = (wxGrid *)this; // const_cast | |
7541 | ||
7542 | return wxSize(self->SetOrCalcColumnSizes(TRUE), | |
7543 | self->SetOrCalcRowSizes(TRUE)); | |
7544 | } | |
7545 | ||
7546 | void wxGrid::Fit() | |
7547 | { | |
7548 | AutoSize(); | |
57c086ef VZ |
7549 | } |
7550 | ||
7551 | // ---------------------------------------------------------------------------- | |
7552 | // cell value accessor functions | |
7553 | // ---------------------------------------------------------------------------- | |
f85afd4e MB |
7554 | |
7555 | void wxGrid::SetCellValue( int row, int col, const wxString& s ) | |
7556 | { | |
7557 | if ( m_table ) | |
7558 | { | |
7559 | m_table->SetValue( row, col, s.c_str() ); | |
2d66e025 MB |
7560 | if ( !GetBatchCount() ) |
7561 | { | |
7562 | wxClientDC dc( m_gridWin ); | |
7563 | PrepareDC( dc ); | |
7564 | DrawCell( dc, wxGridCellCoords(row, col) ); | |
7565 | } | |
60ff3b99 | 7566 | |
f85afd4e | 7567 | if ( m_currentCellCoords.GetRow() == row && |
4cfa5de6 RD |
7568 | m_currentCellCoords.GetCol() == col && |
7569 | IsCellEditControlEnabled()) | |
f85afd4e | 7570 | { |
4cfa5de6 RD |
7571 | HideCellEditControl(); |
7572 | ShowCellEditControl(); // will reread data from table | |
f85afd4e | 7573 | } |
f85afd4e MB |
7574 | } |
7575 | } | |
7576 | ||
7577 | ||
7578 | // | |
2d66e025 | 7579 | // ------ Block, row and col selection |
f85afd4e MB |
7580 | // |
7581 | ||
7582 | void wxGrid::SelectRow( int row, bool addToSelected ) | |
7583 | { | |
b5808881 SN |
7584 | if ( IsSelection() && !addToSelected ) |
7585 | m_selection->ClearSelection(); | |
70c7a608 | 7586 | |
b5808881 | 7587 | m_selection->SelectRow( row ); |
f85afd4e MB |
7588 | } |
7589 | ||
7590 | ||
7591 | void wxGrid::SelectCol( int col, bool addToSelected ) | |
7592 | { | |
b5808881 SN |
7593 | if ( IsSelection() && !addToSelected ) |
7594 | m_selection->ClearSelection(); | |
f85afd4e | 7595 | |
b5808881 | 7596 | m_selection->SelectCol( col ); |
f85afd4e MB |
7597 | } |
7598 | ||
7599 | ||
7600 | void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol ) | |
7601 | { | |
7602 | int temp; | |
da6af900 | 7603 | wxGridCellCoords updateTopLeft, updateBottomRight; |
8f177c8e | 7604 | |
f85afd4e MB |
7605 | if ( topRow > bottomRow ) |
7606 | { | |
7607 | temp = topRow; | |
7608 | topRow = bottomRow; | |
7609 | bottomRow = temp; | |
7610 | } | |
7611 | ||
7612 | if ( leftCol > rightCol ) | |
7613 | { | |
7614 | temp = leftCol; | |
7615 | leftCol = rightCol; | |
7616 | rightCol = temp; | |
7617 | } | |
f0102d2a | 7618 | |
70c7a608 SN |
7619 | updateTopLeft = wxGridCellCoords( topRow, leftCol ); |
7620 | updateBottomRight = wxGridCellCoords( bottomRow, rightCol ); | |
da6af900 | 7621 | |
b5808881 SN |
7622 | if ( m_selectingTopLeft != updateTopLeft || |
7623 | m_selectingBottomRight != updateBottomRight ) | |
da6af900 | 7624 | { |
70c7a608 SN |
7625 | // Compute two optimal update rectangles: |
7626 | // Either one rectangle is a real subset of the | |
7627 | // other, or they are (almost) disjoint! | |
7628 | wxRect rect[4]; | |
ee6694a7 VZ |
7629 | bool need_refresh[4]; |
7630 | need_refresh[0] = | |
7631 | need_refresh[1] = | |
7632 | need_refresh[2] = | |
7633 | need_refresh[3] = FALSE; | |
70c7a608 SN |
7634 | int i; |
7635 | ||
7636 | // Store intermediate values | |
b5808881 SN |
7637 | wxCoord oldLeft = m_selectingTopLeft.GetCol(); |
7638 | wxCoord oldTop = m_selectingTopLeft.GetRow(); | |
7639 | wxCoord oldRight = m_selectingBottomRight.GetCol(); | |
7640 | wxCoord oldBottom = m_selectingBottomRight.GetRow(); | |
70c7a608 SN |
7641 | |
7642 | // Determine the outer/inner coordinates. | |
7643 | if (oldLeft > leftCol) | |
f0102d2a | 7644 | { |
70c7a608 SN |
7645 | temp = oldLeft; |
7646 | oldLeft = leftCol; | |
7647 | leftCol = temp; | |
cb309039 | 7648 | } |
70c7a608 | 7649 | if (oldTop > topRow ) |
f0102d2a | 7650 | { |
70c7a608 SN |
7651 | temp = oldTop; |
7652 | oldTop = topRow; | |
7653 | topRow = temp; | |
cb309039 SN |
7654 | } |
7655 | if (oldRight < rightCol ) | |
70c7a608 SN |
7656 | { |
7657 | temp = oldRight; | |
7658 | oldRight = rightCol; | |
7659 | rightCol = temp; | |
cb309039 SN |
7660 | } |
7661 | if (oldBottom < bottomRow) | |
7662 | { | |
70c7a608 SN |
7663 | temp = oldBottom; |
7664 | oldBottom = bottomRow; | |
7665 | bottomRow = temp; | |
cb309039 | 7666 | } |
70c7a608 SN |
7667 | |
7668 | // Now, either the stuff marked old is the outer | |
7669 | // rectangle or we don't have a situation where one | |
7670 | // is contained in the other. | |
b99be8fb | 7671 | |
cb309039 SN |
7672 | if ( oldLeft < leftCol ) |
7673 | { | |
7674 | need_refresh[0] = TRUE; | |
7675 | rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
7676 | oldLeft ), | |
b99be8fb | 7677 | wxGridCellCoords ( oldBottom, |
cb309039 SN |
7678 | leftCol - 1 ) ); |
7679 | } | |
7680 | ||
7681 | if ( oldTop < topRow ) | |
7682 | { | |
7683 | need_refresh[1] = TRUE; | |
7684 | rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
7685 | leftCol ), | |
b99be8fb | 7686 | wxGridCellCoords ( topRow - 1, |
cb309039 SN |
7687 | rightCol ) ); |
7688 | } | |
7689 | ||
7690 | if ( oldRight > rightCol ) | |
7691 | { | |
7692 | need_refresh[2] = TRUE; | |
7693 | rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
7694 | rightCol + 1 ), | |
7695 | wxGridCellCoords ( oldBottom, | |
7696 | oldRight ) ); | |
7697 | } | |
7698 | ||
7699 | if ( oldBottom > bottomRow ) | |
7700 | { | |
7701 | need_refresh[3] = TRUE; | |
7702 | rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1, | |
7703 | leftCol ), | |
7704 | wxGridCellCoords ( oldBottom, | |
7705 | rightCol ) ); | |
7706 | } | |
70c7a608 SN |
7707 | |
7708 | ||
7709 | // Change Selection | |
b5808881 SN |
7710 | m_selectingTopLeft = updateTopLeft; |
7711 | m_selectingBottomRight = updateBottomRight; | |
b99be8fb | 7712 | |
70c7a608 SN |
7713 | // various Refresh() calls |
7714 | for (i = 0; i < 4; i++ ) | |
7715 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
c6a51dcd | 7716 | m_gridWin->Refresh( FALSE, &(rect[i]) ); |
da6af900 | 7717 | } |
f85afd4e | 7718 | |
5c8fc7c1 SN |
7719 | // never generate an event as it will be generated from |
7720 | // wxGridSelection::SelectBlock! | |
f85afd4e MB |
7721 | } |
7722 | ||
7723 | void wxGrid::SelectAll() | |
7724 | { | |
b5808881 SN |
7725 | m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 ); |
7726 | } | |
f85afd4e | 7727 | |
b5808881 SN |
7728 | bool wxGrid::IsSelection() |
7729 | { | |
7730 | return ( m_selection->IsSelection() || | |
7731 | ( m_selectingTopLeft != wxGridNoCellCoords && | |
7732 | m_selectingBottomRight != wxGridNoCellCoords ) ); | |
f85afd4e MB |
7733 | } |
7734 | ||
b5808881 SN |
7735 | bool wxGrid::IsInSelection( int row, int col ) |
7736 | { | |
7737 | return ( m_selection->IsInSelection( row, col ) || | |
7738 | ( row >= m_selectingTopLeft.GetRow() && | |
7739 | col >= m_selectingTopLeft.GetCol() && | |
7740 | row <= m_selectingBottomRight.GetRow() && | |
7741 | col <= m_selectingBottomRight.GetCol() ) ); | |
7742 | } | |
f85afd4e MB |
7743 | |
7744 | void wxGrid::ClearSelection() | |
7745 | { | |
b5808881 SN |
7746 | m_selectingTopLeft = wxGridNoCellCoords; |
7747 | m_selectingBottomRight = wxGridNoCellCoords; | |
7748 | m_selection->ClearSelection(); | |
8f177c8e | 7749 | } |
f85afd4e MB |
7750 | |
7751 | ||
da6af900 | 7752 | // This function returns the rectangle that encloses the given block |
2d66e025 MB |
7753 | // in device coords clipped to the client size of the grid window. |
7754 | // | |
58dd5b3b MB |
7755 | wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft, |
7756 | const wxGridCellCoords &bottomRight ) | |
f85afd4e | 7757 | { |
58dd5b3b | 7758 | wxRect rect( wxGridNoCellRect ); |
f85afd4e MB |
7759 | wxRect cellRect; |
7760 | ||
58dd5b3b MB |
7761 | cellRect = CellToRect( topLeft ); |
7762 | if ( cellRect != wxGridNoCellRect ) | |
f85afd4e | 7763 | { |
58dd5b3b MB |
7764 | rect = cellRect; |
7765 | } | |
7766 | else | |
7767 | { | |
7768 | rect = wxRect( 0, 0, 0, 0 ); | |
7769 | } | |
60ff3b99 | 7770 | |
58dd5b3b MB |
7771 | cellRect = CellToRect( bottomRight ); |
7772 | if ( cellRect != wxGridNoCellRect ) | |
7773 | { | |
7774 | rect += cellRect; | |
2d66e025 MB |
7775 | } |
7776 | else | |
7777 | { | |
7778 | return wxGridNoCellRect; | |
f85afd4e MB |
7779 | } |
7780 | ||
58dd5b3b MB |
7781 | // convert to scrolled coords |
7782 | // | |
7783 | int left, top, right, bottom; | |
7784 | CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top ); | |
7785 | CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom ); | |
7786 | ||
7787 | int cw, ch; | |
7788 | m_gridWin->GetClientSize( &cw, &ch ); | |
7789 | ||
7790 | rect.SetLeft( wxMax(0, left) ); | |
7791 | rect.SetTop( wxMax(0, top) ); | |
7792 | rect.SetRight( wxMin(cw, right) ); | |
7793 | rect.SetBottom( wxMin(ch, bottom) ); | |
7794 | ||
f85afd4e MB |
7795 | return rect; |
7796 | } | |
7797 | ||
7798 | ||
58dd5b3b | 7799 | |
f85afd4e MB |
7800 | // |
7801 | // ------ Grid event classes | |
7802 | // | |
7803 | ||
7804 | IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxEvent ) | |
7805 | ||
7806 | wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, | |
5c8fc7c1 | 7807 | int row, int col, int x, int y, bool sel, |
f85afd4e MB |
7808 | bool control, bool shift, bool alt, bool meta ) |
7809 | : wxNotifyEvent( type, id ) | |
7810 | { | |
7811 | m_row = row; | |
7812 | m_col = col; | |
7813 | m_x = x; | |
7814 | m_y = y; | |
5c8fc7c1 | 7815 | m_selecting = sel; |
f85afd4e MB |
7816 | m_control = control; |
7817 | m_shift = shift; | |
7818 | m_alt = alt; | |
7819 | m_meta = meta; | |
8f177c8e | 7820 | |
f85afd4e MB |
7821 | SetEventObject(obj); |
7822 | } | |
7823 | ||
7824 | ||
7825 | IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxEvent ) | |
7826 | ||
7827 | wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj, | |
7828 | int rowOrCol, int x, int y, | |
7829 | bool control, bool shift, bool alt, bool meta ) | |
7830 | : wxNotifyEvent( type, id ) | |
7831 | { | |
7832 | m_rowOrCol = rowOrCol; | |
7833 | m_x = x; | |
7834 | m_y = y; | |
7835 | m_control = control; | |
7836 | m_shift = shift; | |
7837 | m_alt = alt; | |
7838 | m_meta = meta; | |
8f177c8e | 7839 | |
f85afd4e MB |
7840 | SetEventObject(obj); |
7841 | } | |
7842 | ||
7843 | ||
7844 | IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxEvent ) | |
7845 | ||
7846 | wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, | |
8f177c8e VZ |
7847 | const wxGridCellCoords& topLeft, |
7848 | const wxGridCellCoords& bottomRight, | |
5c8fc7c1 SN |
7849 | bool sel, bool control, |
7850 | bool shift, bool alt, bool meta ) | |
8f177c8e | 7851 | : wxNotifyEvent( type, id ) |
f85afd4e MB |
7852 | { |
7853 | m_topLeft = topLeft; | |
7854 | m_bottomRight = bottomRight; | |
5c8fc7c1 | 7855 | m_selecting = sel; |
f85afd4e MB |
7856 | m_control = control; |
7857 | m_shift = shift; | |
7858 | m_alt = alt; | |
7859 | m_meta = meta; | |
7860 | ||
7861 | SetEventObject(obj); | |
7862 | } | |
7863 | ||
7864 | ||
7865 | #endif // ifndef wxUSE_NEW_GRID |