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