]>
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) | |
97a9929e | 5 | // Modified by: Robin Dunn, Vadim Zeitlin |
f85afd4e MB |
6 | // Created: 1/08/1999 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
758cbedf VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
f85afd4e MB |
21 | #pragma implementation "grid.h" |
22 | #endif | |
23 | ||
f6bcfd97 | 24 | // For compilers that support precompilatixon, includes "wx/wx.h". |
4d85bcd1 JS |
25 | #include "wx/wxprec.h" |
26 | ||
27 | #include "wx/defs.h" | |
f85afd4e MB |
28 | |
29 | #ifdef __BORLANDC__ | |
30 | #pragma hdrstop | |
31 | #endif | |
32 | ||
27b92ca4 VZ |
33 | #if wxUSE_GRID |
34 | ||
f85afd4e MB |
35 | #ifndef WX_PRECOMP |
36 | #include "wx/utils.h" | |
37 | #include "wx/dcclient.h" | |
38 | #include "wx/settings.h" | |
39 | #include "wx/log.h" | |
508011ce VZ |
40 | #include "wx/textctrl.h" |
41 | #include "wx/checkbox.h" | |
4ee5fc9c | 42 | #include "wx/combobox.h" |
816be743 | 43 | #include "wx/valtext.h" |
f85afd4e MB |
44 | #endif |
45 | ||
cb5df486 | 46 | #include "wx/textfile.h" |
816be743 | 47 | #include "wx/spinctrl.h" |
c4608a8a | 48 | #include "wx/tokenzr.h" |
6d004f67 | 49 | |
07296f0b | 50 | #include "wx/grid.h" |
b5808881 | 51 | #include "wx/generic/gridsel.h" |
07296f0b | 52 | |
0b7e6e7d SN |
53 | #if defined(__WXMOTIF__) |
54 | #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier) | |
c78b3acd | 55 | #else |
0b7e6e7d | 56 | #define WXUNUSED_MOTIF(identifier) identifier |
c78b3acd SN |
57 | #endif |
58 | ||
59 | #if defined(__WXGTK__) | |
60 | #define WXUNUSED_GTK(identifier) WXUNUSED(identifier) | |
61 | #else | |
62 | #define WXUNUSED_GTK(identifier) identifier | |
63 | #endif | |
64 | ||
3f8e5072 JS |
65 | // Required for wxIs... functions |
66 | #include <ctype.h> | |
67 | ||
b99be8fb | 68 | // ---------------------------------------------------------------------------- |
758cbedf | 69 | // array classes |
b99be8fb VZ |
70 | // ---------------------------------------------------------------------------- |
71 | ||
d5d29b8a | 72 | WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr *, wxArrayAttrs, |
160ba750 | 73 | class WXDLLIMPEXP_ADV); |
758cbedf | 74 | |
b99be8fb VZ |
75 | struct wxGridCellWithAttr |
76 | { | |
2e9a6788 VZ |
77 | wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_) |
78 | : coords(row, col), attr(attr_) | |
b99be8fb VZ |
79 | { |
80 | } | |
81 | ||
2e9a6788 VZ |
82 | ~wxGridCellWithAttr() |
83 | { | |
84 | attr->DecRef(); | |
85 | } | |
86 | ||
b99be8fb | 87 | wxGridCellCoords coords; |
2e9a6788 | 88 | wxGridCellAttr *attr; |
22f3361e VZ |
89 | |
90 | // Cannot do this: | |
91 | // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr) | |
92 | // without rewriting the macros, which require a public copy constructor. | |
b99be8fb VZ |
93 | }; |
94 | ||
160ba750 VS |
95 | WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr, wxGridCellWithAttrArray, |
96 | class WXDLLIMPEXP_ADV); | |
b99be8fb VZ |
97 | |
98 | #include "wx/arrimpl.cpp" | |
99 | ||
100 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) | |
101 | WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray) | |
102 | ||
0f442030 RR |
103 | // ---------------------------------------------------------------------------- |
104 | // events | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
2e4df4bf VZ |
107 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK) |
108 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK) | |
109 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK) | |
110 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK) | |
111 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK) | |
112 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK) | |
113 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK) | |
114 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK) | |
115 | DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE) | |
116 | DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE) | |
117 | DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT) | |
118 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE) | |
119 | DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL) | |
120 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN) | |
121 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN) | |
bf7945ce | 122 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED) |
0f442030 | 123 | |
b99be8fb VZ |
124 | // ---------------------------------------------------------------------------- |
125 | // private classes | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
12f190b0 | 128 | class WXDLLIMPEXP_ADV wxGridRowLabelWindow : public wxWindow |
b99be8fb VZ |
129 | { |
130 | public: | |
131 | wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; } | |
132 | wxGridRowLabelWindow( wxGrid *parent, wxWindowID id, | |
133 | const wxPoint &pos, const wxSize &size ); | |
134 | ||
135 | private: | |
136 | wxGrid *m_owner; | |
137 | ||
138 | void OnPaint( wxPaintEvent& event ); | |
139 | void OnMouseEvent( wxMouseEvent& event ); | |
b51c3f27 | 140 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb | 141 | void OnKeyDown( wxKeyEvent& event ); |
f6bcfd97 | 142 | void OnKeyUp( wxKeyEvent& ); |
b99be8fb VZ |
143 | |
144 | DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow) | |
145 | DECLARE_EVENT_TABLE() | |
22f3361e | 146 | DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow) |
b99be8fb VZ |
147 | }; |
148 | ||
149 | ||
12f190b0 | 150 | class WXDLLIMPEXP_ADV wxGridColLabelWindow : public wxWindow |
b99be8fb VZ |
151 | { |
152 | public: | |
153 | wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; } | |
154 | wxGridColLabelWindow( wxGrid *parent, wxWindowID id, | |
155 | const wxPoint &pos, const wxSize &size ); | |
156 | ||
157 | private: | |
158 | wxGrid *m_owner; | |
159 | ||
160 | void OnPaint( wxPaintEvent &event ); | |
161 | void OnMouseEvent( wxMouseEvent& event ); | |
b51c3f27 | 162 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb | 163 | void OnKeyDown( wxKeyEvent& event ); |
f6bcfd97 | 164 | void OnKeyUp( wxKeyEvent& ); |
b99be8fb VZ |
165 | |
166 | DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow) | |
167 | DECLARE_EVENT_TABLE() | |
22f3361e | 168 | DECLARE_NO_COPY_CLASS(wxGridColLabelWindow) |
b99be8fb VZ |
169 | }; |
170 | ||
171 | ||
12f190b0 | 172 | class WXDLLIMPEXP_ADV wxGridCornerLabelWindow : public wxWindow |
b99be8fb VZ |
173 | { |
174 | public: | |
175 | wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; } | |
176 | wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id, | |
177 | const wxPoint &pos, const wxSize &size ); | |
178 | ||
179 | private: | |
180 | wxGrid *m_owner; | |
181 | ||
182 | void OnMouseEvent( wxMouseEvent& event ); | |
b51c3f27 | 183 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb | 184 | void OnKeyDown( wxKeyEvent& event ); |
f6bcfd97 | 185 | void OnKeyUp( wxKeyEvent& ); |
b99be8fb VZ |
186 | void OnPaint( wxPaintEvent& event ); |
187 | ||
188 | DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow) | |
189 | DECLARE_EVENT_TABLE() | |
22f3361e | 190 | DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow) |
b99be8fb VZ |
191 | }; |
192 | ||
12f190b0 | 193 | class WXDLLIMPEXP_ADV wxGridWindow : public wxWindow |
b99be8fb VZ |
194 | { |
195 | public: | |
196 | wxGridWindow() | |
197 | { | |
198 | m_owner = (wxGrid *)NULL; | |
199 | m_rowLabelWin = (wxGridRowLabelWindow *)NULL; | |
200 | m_colLabelWin = (wxGridColLabelWindow *)NULL; | |
201 | } | |
202 | ||
203 | wxGridWindow( wxGrid *parent, | |
204 | wxGridRowLabelWindow *rowLblWin, | |
205 | wxGridColLabelWindow *colLblWin, | |
206 | wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
207 | ~wxGridWindow(); | |
208 | ||
209 | void ScrollWindow( int dx, int dy, const wxRect *rect ); | |
210 | ||
b819b854 JS |
211 | wxGrid* GetOwner() { return m_owner; } |
212 | ||
b99be8fb VZ |
213 | private: |
214 | wxGrid *m_owner; | |
215 | wxGridRowLabelWindow *m_rowLabelWin; | |
216 | wxGridColLabelWindow *m_colLabelWin; | |
217 | ||
218 | void OnPaint( wxPaintEvent &event ); | |
b51c3f27 | 219 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb VZ |
220 | void OnMouseEvent( wxMouseEvent& event ); |
221 | void OnKeyDown( wxKeyEvent& ); | |
f6bcfd97 | 222 | void OnKeyUp( wxKeyEvent& ); |
2796cce3 | 223 | void OnEraseBackground( wxEraseEvent& ); |
80acaf25 | 224 | void OnFocus( wxFocusEvent& ); |
b99be8fb VZ |
225 | |
226 | DECLARE_DYNAMIC_CLASS(wxGridWindow) | |
227 | DECLARE_EVENT_TABLE() | |
22f3361e | 228 | DECLARE_NO_COPY_CLASS(wxGridWindow) |
b99be8fb VZ |
229 | }; |
230 | ||
2796cce3 RD |
231 | |
232 | ||
233 | class wxGridCellEditorEvtHandler : public wxEvtHandler | |
234 | { | |
235 | public: | |
236 | wxGridCellEditorEvtHandler() | |
237 | : m_grid(0), m_editor(0) | |
238 | { } | |
239 | wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor) | |
240 | : m_grid(grid), m_editor(editor) | |
241 | { } | |
242 | ||
243 | void OnKeyDown(wxKeyEvent& event); | |
fb0de762 | 244 | void OnChar(wxKeyEvent& event); |
2796cce3 RD |
245 | |
246 | private: | |
247 | wxGrid* m_grid; | |
248 | wxGridCellEditor* m_editor; | |
249 | DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler) | |
250 | DECLARE_EVENT_TABLE() | |
22f3361e | 251 | DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler) |
2796cce3 RD |
252 | }; |
253 | ||
254 | ||
255 | IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler ) | |
256 | BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler ) | |
257 | EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown ) | |
fb0de762 | 258 | EVT_CHAR( wxGridCellEditorEvtHandler::OnChar ) |
2796cce3 RD |
259 | END_EVENT_TABLE() |
260 | ||
261 | ||
262 | ||
758cbedf | 263 | // ---------------------------------------------------------------------------- |
b99be8fb | 264 | // the internal data representation used by wxGridCellAttrProvider |
758cbedf VZ |
265 | // ---------------------------------------------------------------------------- |
266 | ||
267 | // this class stores attributes set for cells | |
12f190b0 | 268 | class WXDLLIMPEXP_ADV wxGridCellAttrData |
b99be8fb VZ |
269 | { |
270 | public: | |
2e9a6788 | 271 | void SetAttr(wxGridCellAttr *attr, int row, int col); |
b99be8fb | 272 | wxGridCellAttr *GetAttr(int row, int col) const; |
4d60017a SN |
273 | void UpdateAttrRows( size_t pos, int numRows ); |
274 | void UpdateAttrCols( size_t pos, int numCols ); | |
b99be8fb VZ |
275 | |
276 | private: | |
277 | // searches for the attr for given cell, returns wxNOT_FOUND if not found | |
278 | int FindIndex(int row, int col) const; | |
279 | ||
280 | wxGridCellWithAttrArray m_attrs; | |
281 | }; | |
282 | ||
758cbedf | 283 | // this class stores attributes set for rows or columns |
12f190b0 | 284 | class WXDLLIMPEXP_ADV wxGridRowOrColAttrData |
758cbedf VZ |
285 | { |
286 | public: | |
ee6694a7 VZ |
287 | // empty ctor to suppress warnings |
288 | wxGridRowOrColAttrData() { } | |
758cbedf VZ |
289 | ~wxGridRowOrColAttrData(); |
290 | ||
291 | void SetAttr(wxGridCellAttr *attr, int rowOrCol); | |
292 | wxGridCellAttr *GetAttr(int rowOrCol) const; | |
4d60017a | 293 | void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ); |
758cbedf VZ |
294 | |
295 | private: | |
296 | wxArrayInt m_rowsOrCols; | |
297 | wxArrayAttrs m_attrs; | |
298 | }; | |
299 | ||
300 | // NB: this is just a wrapper around 3 objects: one which stores cell | |
301 | // attributes, and 2 others for row/col ones | |
12f190b0 | 302 | class WXDLLIMPEXP_ADV wxGridCellAttrProviderData |
758cbedf VZ |
303 | { |
304 | public: | |
305 | wxGridCellAttrData m_cellAttrs; | |
306 | wxGridRowOrColAttrData m_rowAttrs, | |
307 | m_colAttrs; | |
308 | }; | |
309 | ||
f2d76237 RD |
310 | |
311 | // ---------------------------------------------------------------------------- | |
312 | // data structures used for the data type registry | |
313 | // ---------------------------------------------------------------------------- | |
314 | ||
b94ae1ea VZ |
315 | struct wxGridDataTypeInfo |
316 | { | |
f2d76237 RD |
317 | wxGridDataTypeInfo(const wxString& typeName, |
318 | wxGridCellRenderer* renderer, | |
319 | wxGridCellEditor* editor) | |
320 | : m_typeName(typeName), m_renderer(renderer), m_editor(editor) | |
321 | { } | |
322 | ||
39bcce60 VZ |
323 | ~wxGridDataTypeInfo() |
324 | { | |
325 | wxSafeDecRef(m_renderer); | |
326 | wxSafeDecRef(m_editor); | |
327 | } | |
f2d76237 RD |
328 | |
329 | wxString m_typeName; | |
330 | wxGridCellRenderer* m_renderer; | |
331 | wxGridCellEditor* m_editor; | |
22f3361e VZ |
332 | |
333 | DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo) | |
f2d76237 RD |
334 | }; |
335 | ||
336 | ||
d5d29b8a | 337 | WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo*, wxGridDataTypeInfoArray, |
160ba750 | 338 | class WXDLLIMPEXP_ADV); |
f2d76237 RD |
339 | |
340 | ||
12f190b0 | 341 | class WXDLLIMPEXP_ADV wxGridTypeRegistry |
b94ae1ea | 342 | { |
f2d76237 | 343 | public: |
c78b3acd | 344 | wxGridTypeRegistry() {} |
f2d76237 | 345 | ~wxGridTypeRegistry(); |
b94ae1ea | 346 | |
f2d76237 RD |
347 | void RegisterDataType(const wxString& typeName, |
348 | wxGridCellRenderer* renderer, | |
349 | wxGridCellEditor* editor); | |
c4608a8a VZ |
350 | |
351 | // find one of already registered data types | |
352 | int FindRegisteredDataType(const wxString& typeName); | |
353 | ||
354 | // try to FindRegisteredDataType(), if this fails and typeName is one of | |
355 | // standard typenames, register it and return its index | |
f2d76237 | 356 | int FindDataType(const wxString& typeName); |
c4608a8a VZ |
357 | |
358 | // try to FindDataType(), if it fails see if it is not one of already | |
359 | // registered data types with some params in which case clone the | |
360 | // registered data type and set params for it | |
361 | int FindOrCloneDataType(const wxString& typeName); | |
362 | ||
f2d76237 RD |
363 | wxGridCellRenderer* GetRenderer(int index); |
364 | wxGridCellEditor* GetEditor(int index); | |
365 | ||
366 | private: | |
367 | wxGridDataTypeInfoArray m_typeinfo; | |
368 | }; | |
369 | ||
b99be8fb VZ |
370 | // ---------------------------------------------------------------------------- |
371 | // conditional compilation | |
372 | // ---------------------------------------------------------------------------- | |
373 | ||
9496deb5 MB |
374 | #ifndef WXGRID_DRAW_LINES |
375 | #define WXGRID_DRAW_LINES 1 | |
796df70a SN |
376 | #endif |
377 | ||
0a976765 VZ |
378 | // ---------------------------------------------------------------------------- |
379 | // globals | |
380 | // ---------------------------------------------------------------------------- | |
381 | ||
382 | //#define DEBUG_ATTR_CACHE | |
383 | #ifdef DEBUG_ATTR_CACHE | |
384 | static size_t gs_nAttrCacheHits = 0; | |
385 | static size_t gs_nAttrCacheMisses = 0; | |
386 | #endif // DEBUG_ATTR_CACHE | |
f85afd4e | 387 | |
43947979 VZ |
388 | // ---------------------------------------------------------------------------- |
389 | // constants | |
390 | // ---------------------------------------------------------------------------- | |
391 | ||
f85afd4e MB |
392 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); |
393 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); | |
394 | ||
f0102d2a | 395 | // scroll line size |
faec5a43 SN |
396 | // TODO: this doesn't work at all, grid cells have different sizes and approx |
397 | // calculations don't work as because of the size mismatch scrollbars | |
398 | // sometimes fail to be shown when they should be or vice versa | |
b51c3f27 RD |
399 | // |
400 | // The scroll bars may be a little flakey once in a while, but that is | |
401 | // surely much less horrible than having scroll lines of only 1!!! | |
402 | // -- Robin | |
97a9929e VZ |
403 | // |
404 | // Well, it's still seriously broken so it might be better but needs | |
405 | // fixing anyhow | |
406 | // -- Vadim | |
407 | static const size_t GRID_SCROLL_LINE_X = 15; // 1; | |
408 | static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X; | |
f85afd4e | 409 | |
43947979 VZ |
410 | // the size of hash tables used a bit everywhere (the max number of elements |
411 | // in these hash tables is the number of rows/columns) | |
412 | static const int GRID_HASH_SIZE = 100; | |
413 | ||
97a9929e VZ |
414 | // ---------------------------------------------------------------------------- |
415 | // private functions | |
416 | // ---------------------------------------------------------------------------- | |
417 | ||
d0eb7e56 | 418 | static inline int GetScrollX(int x) |
97a9929e VZ |
419 | { |
420 | return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X; | |
421 | } | |
422 | ||
d0eb7e56 | 423 | static inline int GetScrollY(int y) |
97a9929e VZ |
424 | { |
425 | return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y; | |
426 | } | |
427 | ||
ab79958a VZ |
428 | // ============================================================================ |
429 | // implementation | |
430 | // ============================================================================ | |
431 | ||
2796cce3 RD |
432 | // ---------------------------------------------------------------------------- |
433 | // wxGridCellEditor | |
434 | // ---------------------------------------------------------------------------- | |
435 | ||
436 | wxGridCellEditor::wxGridCellEditor() | |
437 | { | |
438 | m_control = NULL; | |
1bd71df9 | 439 | m_attr = NULL; |
2796cce3 RD |
440 | } |
441 | ||
442 | ||
443 | wxGridCellEditor::~wxGridCellEditor() | |
444 | { | |
445 | Destroy(); | |
446 | } | |
447 | ||
508011ce VZ |
448 | void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent), |
449 | wxWindowID WXUNUSED(id), | |
450 | wxEvtHandler* evtHandler) | |
451 | { | |
189d0213 | 452 | if ( evtHandler ) |
508011ce VZ |
453 | m_control->PushEventHandler(evtHandler); |
454 | } | |
2796cce3 | 455 | |
189d0213 VZ |
456 | void wxGridCellEditor::PaintBackground(const wxRect& rectCell, |
457 | wxGridCellAttr *attr) | |
458 | { | |
459 | // erase the background because we might not fill the cell | |
460 | wxClientDC dc(m_control->GetParent()); | |
b819b854 JS |
461 | wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow); |
462 | if (gridWindow) | |
463 | gridWindow->GetOwner()->PrepareDC(dc); | |
ef5df12b | 464 | |
189d0213 VZ |
465 | dc.SetPen(*wxTRANSPARENT_PEN); |
466 | dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID)); | |
467 | dc.DrawRectangle(rectCell); | |
468 | ||
469 | // redraw the control we just painted over | |
470 | m_control->Refresh(); | |
471 | } | |
472 | ||
2796cce3 RD |
473 | void wxGridCellEditor::Destroy() |
474 | { | |
508011ce VZ |
475 | if (m_control) |
476 | { | |
b94ae1ea VZ |
477 | m_control->PopEventHandler(TRUE /* delete it*/); |
478 | ||
2796cce3 RD |
479 | m_control->Destroy(); |
480 | m_control = NULL; | |
481 | } | |
482 | } | |
483 | ||
3da93aae | 484 | void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr) |
2796cce3 RD |
485 | { |
486 | wxASSERT_MSG(m_control, | |
487 | wxT("The wxGridCellEditor must be Created first!")); | |
488 | m_control->Show(show); | |
3da93aae VZ |
489 | |
490 | if ( show ) | |
491 | { | |
492 | // set the colours/fonts if we have any | |
493 | if ( attr ) | |
494 | { | |
f2d76237 RD |
495 | m_colFgOld = m_control->GetForegroundColour(); |
496 | m_control->SetForegroundColour(attr->GetTextColour()); | |
3da93aae | 497 | |
f2d76237 RD |
498 | m_colBgOld = m_control->GetBackgroundColour(); |
499 | m_control->SetBackgroundColour(attr->GetBackgroundColour()); | |
3da93aae | 500 | |
f2d76237 RD |
501 | m_fontOld = m_control->GetFont(); |
502 | m_control->SetFont(attr->GetFont()); | |
3da93aae VZ |
503 | |
504 | // can't do anything more in the base class version, the other | |
505 | // attributes may only be used by the derived classes | |
506 | } | |
507 | } | |
508 | else | |
509 | { | |
510 | // restore the standard colours fonts | |
511 | if ( m_colFgOld.Ok() ) | |
512 | { | |
513 | m_control->SetForegroundColour(m_colFgOld); | |
514 | m_colFgOld = wxNullColour; | |
515 | } | |
516 | ||
517 | if ( m_colBgOld.Ok() ) | |
518 | { | |
519 | m_control->SetBackgroundColour(m_colBgOld); | |
520 | m_colBgOld = wxNullColour; | |
521 | } | |
522 | ||
523 | if ( m_fontOld.Ok() ) | |
524 | { | |
525 | m_control->SetFont(m_fontOld); | |
526 | m_fontOld = wxNullFont; | |
527 | } | |
528 | } | |
2796cce3 RD |
529 | } |
530 | ||
531 | void wxGridCellEditor::SetSize(const wxRect& rect) | |
532 | { | |
533 | wxASSERT_MSG(m_control, | |
534 | wxT("The wxGridCellEditor must be Created first!")); | |
28a77bc4 | 535 | m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); |
2796cce3 RD |
536 | } |
537 | ||
538 | void wxGridCellEditor::HandleReturn(wxKeyEvent& event) | |
539 | { | |
540 | event.Skip(); | |
541 | } | |
542 | ||
f6bcfd97 BP |
543 | bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event) |
544 | { | |
545 | // accept the simple key presses, not anything with Ctrl/Alt/Meta | |
a4f7bf58 | 546 | return !(event.ControlDown() || event.AltDown()); |
f6bcfd97 | 547 | } |
2796cce3 | 548 | |
2c9a89e0 RD |
549 | void wxGridCellEditor::StartingKey(wxKeyEvent& event) |
550 | { | |
e195a54c VZ |
551 | event.Skip(); |
552 | } | |
2c9a89e0 | 553 | |
e195a54c VZ |
554 | void wxGridCellEditor::StartingClick() |
555 | { | |
b54ba671 | 556 | } |
2c9a89e0 | 557 | |
3a8c693a VZ |
558 | #if wxUSE_TEXTCTRL |
559 | ||
b54ba671 VZ |
560 | // ---------------------------------------------------------------------------- |
561 | // wxGridCellTextEditor | |
562 | // ---------------------------------------------------------------------------- | |
2c9a89e0 | 563 | |
2796cce3 RD |
564 | wxGridCellTextEditor::wxGridCellTextEditor() |
565 | { | |
c4608a8a | 566 | m_maxChars = 0; |
2796cce3 RD |
567 | } |
568 | ||
569 | void wxGridCellTextEditor::Create(wxWindow* parent, | |
570 | wxWindowID id, | |
2796cce3 RD |
571 | wxEvtHandler* evtHandler) |
572 | { | |
508011ce | 573 | m_control = new wxTextCtrl(parent, id, wxEmptyString, |
2c9a89e0 | 574 | wxDefaultPosition, wxDefaultSize |
2796cce3 | 575 | #if defined(__WXMSW__) |
aaae069f | 576 | , wxTE_PROCESS_TAB | wxTE_AUTO_SCROLL |
2796cce3 | 577 | #endif |
508011ce | 578 | ); |
2796cce3 | 579 | |
46a5010a RD |
580 | // set max length allowed in the textctrl, if the parameter was set |
581 | if (m_maxChars != 0) | |
582 | { | |
583 | ((wxTextCtrl*)m_control)->SetMaxLength(m_maxChars); | |
584 | } | |
c4608a8a | 585 | |
508011ce | 586 | wxGridCellEditor::Create(parent, id, evtHandler); |
2796cce3 RD |
587 | } |
588 | ||
189d0213 VZ |
589 | void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell), |
590 | wxGridCellAttr * WXUNUSED(attr)) | |
591 | { | |
592 | // as we fill the entire client area, don't do anything here to minimize | |
593 | // flicker | |
594 | } | |
2796cce3 | 595 | |
99306db2 VZ |
596 | void wxGridCellTextEditor::SetSize(const wxRect& rectOrig) |
597 | { | |
598 | wxRect rect(rectOrig); | |
599 | ||
600 | // Make the edit control large enough to allow for internal | |
601 | // margins | |
602 | // | |
603 | // TODO: remove this if the text ctrl sizing is improved esp. for | |
604 | // unix | |
605 | // | |
606 | #if defined(__WXGTK__) | |
b0e282b3 RR |
607 | if (rect.x != 0) |
608 | { | |
609 | rect.x += 1; | |
610 | rect.y += 1; | |
611 | rect.width -= 1; | |
612 | rect.height -= 1; | |
613 | } | |
99306db2 | 614 | #else // !GTK |
cb105ad4 | 615 | int extra_x = ( rect.x > 2 )? 2 : 1; |
84912ef8 RD |
616 | |
617 | // MB: treat MSW separately here otherwise the caret doesn't show | |
618 | // when the editor is in the first row. | |
a0948e27 MB |
619 | #if defined(__WXMSW__) |
620 | int extra_y = 2; | |
621 | #else | |
cb105ad4 | 622 | int extra_y = ( rect.y > 2 )? 2 : 1; |
a0948e27 MB |
623 | #endif // MSW |
624 | ||
99306db2 | 625 | #if defined(__WXMOTIF__) |
cb105ad4 SN |
626 | extra_x *= 2; |
627 | extra_y *= 2; | |
99306db2 | 628 | #endif |
cb105ad4 SN |
629 | rect.SetLeft( wxMax(0, rect.x - extra_x) ); |
630 | rect.SetTop( wxMax(0, rect.y - extra_y) ); | |
631 | rect.SetRight( rect.GetRight() + 2*extra_x ); | |
632 | rect.SetBottom( rect.GetBottom() + 2*extra_y ); | |
99306db2 VZ |
633 | #endif // GTK/!GTK |
634 | ||
635 | wxGridCellEditor::SetSize(rect); | |
636 | } | |
637 | ||
3da93aae | 638 | void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid) |
2796cce3 RD |
639 | { |
640 | wxASSERT_MSG(m_control, | |
641 | wxT("The wxGridCellEditor must be Created first!")); | |
642 | ||
643 | m_startValue = grid->GetTable()->GetValue(row, col); | |
816be743 VZ |
644 | |
645 | DoBeginEdit(m_startValue); | |
646 | } | |
647 | ||
648 | void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue) | |
649 | { | |
650 | Text()->SetValue(startValue); | |
b54ba671 | 651 | Text()->SetInsertionPointEnd(); |
505f70de | 652 | Text()->SetSelection(-1,-1); |
b54ba671 | 653 | Text()->SetFocus(); |
2796cce3 RD |
654 | } |
655 | ||
3324d5f5 | 656 | bool wxGridCellTextEditor::EndEdit(int row, int col, |
3da93aae | 657 | wxGrid* grid) |
2796cce3 RD |
658 | { |
659 | wxASSERT_MSG(m_control, | |
660 | wxT("The wxGridCellEditor must be Created first!")); | |
661 | ||
662 | bool changed = FALSE; | |
b54ba671 | 663 | wxString value = Text()->GetValue(); |
2796cce3 RD |
664 | if (value != m_startValue) |
665 | changed = TRUE; | |
666 | ||
667 | if (changed) | |
668 | grid->GetTable()->SetValue(row, col, value); | |
2c9a89e0 | 669 | |
3da93aae | 670 | m_startValue = wxEmptyString; |
7b519e5e JS |
671 | // No point in setting the text of the hidden control |
672 | //Text()->SetValue(m_startValue); | |
2796cce3 RD |
673 | |
674 | return changed; | |
675 | } | |
676 | ||
677 | ||
678 | void wxGridCellTextEditor::Reset() | |
679 | { | |
680 | wxASSERT_MSG(m_control, | |
681 | wxT("The wxGridCellEditor must be Created first!")); | |
682 | ||
816be743 VZ |
683 | DoReset(m_startValue); |
684 | } | |
685 | ||
686 | void wxGridCellTextEditor::DoReset(const wxString& startValue) | |
687 | { | |
688 | Text()->SetValue(startValue); | |
b54ba671 | 689 | Text()->SetInsertionPointEnd(); |
2796cce3 RD |
690 | } |
691 | ||
f6bcfd97 BP |
692 | bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event) |
693 | { | |
694 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
695 | { | |
696 | int keycode = event.GetKeyCode(); | |
697 | switch ( keycode ) | |
698 | { | |
699 | case WXK_NUMPAD0: | |
700 | case WXK_NUMPAD1: | |
701 | case WXK_NUMPAD2: | |
702 | case WXK_NUMPAD3: | |
703 | case WXK_NUMPAD4: | |
704 | case WXK_NUMPAD5: | |
705 | case WXK_NUMPAD6: | |
706 | case WXK_NUMPAD7: | |
707 | case WXK_NUMPAD8: | |
708 | case WXK_NUMPAD9: | |
709 | case WXK_MULTIPLY: | |
710 | case WXK_NUMPAD_MULTIPLY: | |
711 | case WXK_ADD: | |
712 | case WXK_NUMPAD_ADD: | |
713 | case WXK_SUBTRACT: | |
714 | case WXK_NUMPAD_SUBTRACT: | |
715 | case WXK_DECIMAL: | |
716 | case WXK_NUMPAD_DECIMAL: | |
717 | case WXK_DIVIDE: | |
718 | case WXK_NUMPAD_DIVIDE: | |
719 | return TRUE; | |
720 | ||
721 | default: | |
722 | // accept 8 bit chars too if isprint() agrees | |
1c193821 | 723 | if ( (keycode < 255) && (wxIsprint(keycode)) ) |
f6bcfd97 BP |
724 | return TRUE; |
725 | } | |
726 | } | |
727 | ||
728 | return FALSE; | |
729 | } | |
730 | ||
2c9a89e0 RD |
731 | void wxGridCellTextEditor::StartingKey(wxKeyEvent& event) |
732 | { | |
94af7d45 | 733 | if ( !Text()->EmulateKeyPress(event) ) |
f6bcfd97 BP |
734 | { |
735 | event.Skip(); | |
736 | } | |
b54ba671 | 737 | } |
2c9a89e0 | 738 | |
c78b3acd | 739 | void wxGridCellTextEditor::HandleReturn( wxKeyEvent& |
0b7e6e7d | 740 | WXUNUSED_GTK(WXUNUSED_MOTIF(event)) ) |
2796cce3 RD |
741 | { |
742 | #if defined(__WXMOTIF__) || defined(__WXGTK__) | |
743 | // wxMotif needs a little extra help... | |
6fc0f38f | 744 | size_t pos = (size_t)( Text()->GetInsertionPoint() ); |
b54ba671 | 745 | wxString s( Text()->GetValue() ); |
8dd8f875 | 746 | s = s.Left(pos) + wxT("\n") + s.Mid(pos); |
b54ba671 VZ |
747 | Text()->SetValue(s); |
748 | Text()->SetInsertionPoint( pos ); | |
2796cce3 RD |
749 | #else |
750 | // the other ports can handle a Return key press | |
751 | // | |
752 | event.Skip(); | |
753 | #endif | |
754 | } | |
755 | ||
c4608a8a VZ |
756 | void wxGridCellTextEditor::SetParameters(const wxString& params) |
757 | { | |
758 | if ( !params ) | |
759 | { | |
760 | // reset to default | |
761 | m_maxChars = 0; | |
762 | } | |
763 | else | |
764 | { | |
765 | long tmp; | |
766 | if ( !params.ToLong(&tmp) ) | |
767 | { | |
f6bcfd97 | 768 | wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str()); |
c4608a8a VZ |
769 | } |
770 | else | |
771 | { | |
772 | m_maxChars = (size_t)tmp; | |
773 | } | |
774 | } | |
775 | } | |
776 | ||
73145b0e JS |
777 | // return the value in the text control |
778 | wxString wxGridCellTextEditor::GetValue() const | |
779 | { | |
780 | return Text()->GetValue(); | |
781 | } | |
782 | ||
816be743 VZ |
783 | // ---------------------------------------------------------------------------- |
784 | // wxGridCellNumberEditor | |
785 | // ---------------------------------------------------------------------------- | |
786 | ||
787 | wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max) | |
788 | { | |
789 | m_min = min; | |
790 | m_max = max; | |
791 | } | |
792 | ||
793 | void wxGridCellNumberEditor::Create(wxWindow* parent, | |
794 | wxWindowID id, | |
795 | wxEvtHandler* evtHandler) | |
796 | { | |
797 | if ( HasRange() ) | |
798 | { | |
799 | // create a spin ctrl | |
800 | m_control = new wxSpinCtrl(parent, -1, wxEmptyString, | |
801 | wxDefaultPosition, wxDefaultSize, | |
802 | wxSP_ARROW_KEYS, | |
803 | m_min, m_max); | |
804 | ||
805 | wxGridCellEditor::Create(parent, id, evtHandler); | |
806 | } | |
807 | else | |
808 | { | |
809 | // just a text control | |
810 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
811 | ||
812 | #if wxUSE_VALIDATORS | |
85bc0351 | 813 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); |
816be743 VZ |
814 | #endif // wxUSE_VALIDATORS |
815 | } | |
816 | } | |
817 | ||
818 | void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid) | |
819 | { | |
820 | // first get the value | |
821 | wxGridTableBase *table = grid->GetTable(); | |
822 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
823 | { | |
824 | m_valueOld = table->GetValueAsLong(row, col); | |
825 | } | |
826 | else | |
827 | { | |
8a60ff0a | 828 | m_valueOld = 0; |
a5777624 | 829 | wxString sValue = table->GetValue(row, col); |
8a60ff0a | 830 | if (! sValue.ToLong(&m_valueOld) && ! sValue.IsEmpty()) |
a5777624 RD |
831 | { |
832 | wxFAIL_MSG( _T("this cell doesn't have numeric value") ); | |
833 | return; | |
834 | } | |
816be743 VZ |
835 | } |
836 | ||
837 | if ( HasRange() ) | |
838 | { | |
4a64bee4 | 839 | Spin()->SetValue((int)m_valueOld); |
f6bcfd97 | 840 | Spin()->SetFocus(); |
816be743 VZ |
841 | } |
842 | else | |
843 | { | |
844 | DoBeginEdit(GetString()); | |
845 | } | |
846 | } | |
847 | ||
3324d5f5 | 848 | bool wxGridCellNumberEditor::EndEdit(int row, int col, |
816be743 VZ |
849 | wxGrid* grid) |
850 | { | |
851 | bool changed; | |
8a60ff0a RD |
852 | long value = 0; |
853 | wxString text; | |
816be743 VZ |
854 | |
855 | if ( HasRange() ) | |
856 | { | |
857 | value = Spin()->GetValue(); | |
858 | changed = value != m_valueOld; | |
8a60ff0a RD |
859 | if (changed) |
860 | text = wxString::Format(wxT("%ld"), value); | |
816be743 VZ |
861 | } |
862 | else | |
863 | { | |
8a60ff0a RD |
864 | text = Text()->GetValue(); |
865 | changed = (text.IsEmpty() || text.ToLong(&value)) && (value != m_valueOld); | |
816be743 VZ |
866 | } |
867 | ||
868 | if ( changed ) | |
869 | { | |
a5777624 RD |
870 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER)) |
871 | grid->GetTable()->SetValueAsLong(row, col, value); | |
872 | else | |
8a60ff0a | 873 | grid->GetTable()->SetValue(row, col, text); |
816be743 VZ |
874 | } |
875 | ||
876 | return changed; | |
877 | } | |
878 | ||
879 | void wxGridCellNumberEditor::Reset() | |
880 | { | |
881 | if ( HasRange() ) | |
882 | { | |
4a64bee4 | 883 | Spin()->SetValue((int)m_valueOld); |
816be743 VZ |
884 | } |
885 | else | |
886 | { | |
887 | DoReset(GetString()); | |
888 | } | |
889 | } | |
890 | ||
f6bcfd97 BP |
891 | bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event) |
892 | { | |
893 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
894 | { | |
895 | int keycode = event.GetKeyCode(); | |
896 | switch ( keycode ) | |
897 | { | |
898 | case WXK_NUMPAD0: | |
899 | case WXK_NUMPAD1: | |
900 | case WXK_NUMPAD2: | |
901 | case WXK_NUMPAD3: | |
902 | case WXK_NUMPAD4: | |
903 | case WXK_NUMPAD5: | |
904 | case WXK_NUMPAD6: | |
905 | case WXK_NUMPAD7: | |
906 | case WXK_NUMPAD8: | |
907 | case WXK_NUMPAD9: | |
908 | case WXK_ADD: | |
909 | case WXK_NUMPAD_ADD: | |
910 | case WXK_SUBTRACT: | |
911 | case WXK_NUMPAD_SUBTRACT: | |
912 | case WXK_UP: | |
913 | case WXK_DOWN: | |
914 | return TRUE; | |
915 | ||
916 | default: | |
4676948b | 917 | if ( (keycode < 128) && wxIsdigit(keycode) ) |
f6bcfd97 BP |
918 | return TRUE; |
919 | } | |
920 | } | |
921 | ||
922 | return FALSE; | |
923 | } | |
924 | ||
816be743 VZ |
925 | void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event) |
926 | { | |
927 | if ( !HasRange() ) | |
928 | { | |
12a3f227 | 929 | int keycode = event.GetKeyCode(); |
4676948b | 930 | if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-' |
85d8c319 JS |
931 | || keycode == WXK_NUMPAD0 |
932 | || keycode == WXK_NUMPAD1 | |
933 | || keycode == WXK_NUMPAD2 | |
934 | || keycode == WXK_NUMPAD3 | |
935 | || keycode == WXK_NUMPAD4 | |
936 | || keycode == WXK_NUMPAD5 | |
937 | || keycode == WXK_NUMPAD6 | |
938 | || keycode == WXK_NUMPAD7 | |
939 | || keycode == WXK_NUMPAD8 | |
940 | || keycode == WXK_NUMPAD9 | |
941 | || keycode == WXK_ADD | |
942 | || keycode == WXK_NUMPAD_ADD | |
943 | || keycode == WXK_SUBTRACT | |
944 | || keycode == WXK_NUMPAD_SUBTRACT) | |
816be743 VZ |
945 | { |
946 | wxGridCellTextEditor::StartingKey(event); | |
947 | ||
948 | // skip Skip() below | |
949 | return; | |
950 | } | |
951 | } | |
952 | ||
953 | event.Skip(); | |
954 | } | |
9c4ba614 | 955 | |
c4608a8a VZ |
956 | void wxGridCellNumberEditor::SetParameters(const wxString& params) |
957 | { | |
958 | if ( !params ) | |
959 | { | |
960 | // reset to default | |
961 | m_min = | |
962 | m_max = -1; | |
963 | } | |
964 | else | |
965 | { | |
966 | long tmp; | |
967 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
968 | { | |
969 | m_min = (int)tmp; | |
970 | ||
971 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
972 | { | |
973 | m_max = (int)tmp; | |
974 | ||
975 | // skip the error message below | |
976 | return; | |
977 | } | |
978 | } | |
979 | ||
f6bcfd97 | 980 | wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str()); |
c4608a8a VZ |
981 | } |
982 | } | |
983 | ||
73145b0e JS |
984 | // return the value in the spin control if it is there (the text control otherwise) |
985 | wxString wxGridCellNumberEditor::GetValue() const | |
986 | { | |
987 | wxString s; | |
988 | ||
989 | if( HasRange() ) | |
990 | { | |
14ac4f3c | 991 | long value = Spin()->GetValue(); |
73145b0e JS |
992 | s.Printf(wxT("%ld"), value); |
993 | } | |
994 | else | |
995 | { | |
996 | s = Text()->GetValue(); | |
997 | } | |
998 | return s; | |
999 | } | |
1000 | ||
816be743 VZ |
1001 | // ---------------------------------------------------------------------------- |
1002 | // wxGridCellFloatEditor | |
1003 | // ---------------------------------------------------------------------------- | |
1004 | ||
f6bcfd97 BP |
1005 | wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision) |
1006 | { | |
1007 | m_width = width; | |
1008 | m_precision = precision; | |
1009 | } | |
1010 | ||
816be743 VZ |
1011 | void wxGridCellFloatEditor::Create(wxWindow* parent, |
1012 | wxWindowID id, | |
1013 | wxEvtHandler* evtHandler) | |
1014 | { | |
1015 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
1016 | ||
1017 | #if wxUSE_VALIDATORS | |
85bc0351 | 1018 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); |
816be743 VZ |
1019 | #endif // wxUSE_VALIDATORS |
1020 | } | |
1021 | ||
1022 | void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1023 | { | |
1024 | // first get the value | |
1025 | wxGridTableBase *table = grid->GetTable(); | |
1026 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
1027 | { | |
1028 | m_valueOld = table->GetValueAsDouble(row, col); | |
1029 | } | |
1030 | else | |
1031 | { | |
8a60ff0a | 1032 | m_valueOld = 0.0; |
a5777624 | 1033 | wxString sValue = table->GetValue(row, col); |
8a60ff0a | 1034 | if (! sValue.ToDouble(&m_valueOld) && ! sValue.IsEmpty()) |
a5777624 RD |
1035 | { |
1036 | wxFAIL_MSG( _T("this cell doesn't have float value") ); | |
1037 | return; | |
1038 | } | |
816be743 VZ |
1039 | } |
1040 | ||
1041 | DoBeginEdit(GetString()); | |
1042 | } | |
1043 | ||
3324d5f5 | 1044 | bool wxGridCellFloatEditor::EndEdit(int row, int col, |
816be743 VZ |
1045 | wxGrid* grid) |
1046 | { | |
8a60ff0a RD |
1047 | double value = 0.0; |
1048 | wxString text(Text()->GetValue()); | |
1049 | ||
1050 | if ( (text.IsEmpty() || text.ToDouble(&value)) && (value != m_valueOld) ) | |
816be743 | 1051 | { |
a5777624 RD |
1052 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT)) |
1053 | grid->GetTable()->SetValueAsDouble(row, col, value); | |
1054 | else | |
8a60ff0a | 1055 | grid->GetTable()->SetValue(row, col, text); |
816be743 VZ |
1056 | |
1057 | return TRUE; | |
1058 | } | |
ae31cc57 | 1059 | return FALSE; |
816be743 VZ |
1060 | } |
1061 | ||
1062 | void wxGridCellFloatEditor::Reset() | |
1063 | { | |
1064 | DoReset(GetString()); | |
1065 | } | |
1066 | ||
1067 | void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event) | |
1068 | { | |
12a3f227 | 1069 | int keycode = event.GetKeyCode(); |
4676948b | 1070 | if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-' || keycode == '.' |
85d8c319 JS |
1071 | || keycode == WXK_NUMPAD0 |
1072 | || keycode == WXK_NUMPAD1 | |
1073 | || keycode == WXK_NUMPAD2 | |
1074 | || keycode == WXK_NUMPAD3 | |
1075 | || keycode == WXK_NUMPAD4 | |
1076 | || keycode == WXK_NUMPAD5 | |
1077 | || keycode == WXK_NUMPAD6 | |
1078 | || keycode == WXK_NUMPAD7 | |
1079 | || keycode == WXK_NUMPAD8 | |
1080 | || keycode == WXK_NUMPAD9 | |
1081 | || keycode == WXK_ADD | |
1082 | || keycode == WXK_NUMPAD_ADD | |
1083 | || keycode == WXK_SUBTRACT | |
1084 | || keycode == WXK_NUMPAD_SUBTRACT) | |
816be743 VZ |
1085 | { |
1086 | wxGridCellTextEditor::StartingKey(event); | |
1087 | ||
1088 | // skip Skip() below | |
1089 | return; | |
1090 | } | |
1091 | ||
1092 | event.Skip(); | |
1093 | } | |
1094 | ||
f6bcfd97 BP |
1095 | void wxGridCellFloatEditor::SetParameters(const wxString& params) |
1096 | { | |
1097 | if ( !params ) | |
1098 | { | |
1099 | // reset to default | |
1100 | m_width = | |
1101 | m_precision = -1; | |
1102 | } | |
1103 | else | |
1104 | { | |
1105 | long tmp; | |
1106 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
1107 | { | |
1108 | m_width = (int)tmp; | |
1109 | ||
1110 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
1111 | { | |
1112 | m_precision = (int)tmp; | |
1113 | ||
1114 | // skip the error message below | |
1115 | return; | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str()); | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | wxString wxGridCellFloatEditor::GetString() const | |
1124 | { | |
1125 | wxString fmt; | |
1126 | if ( m_width == -1 ) | |
1127 | { | |
1128 | // default width/precision | |
ec53826c | 1129 | fmt = _T("%f"); |
f6bcfd97 BP |
1130 | } |
1131 | else if ( m_precision == -1 ) | |
1132 | { | |
1133 | // default precision | |
ec53826c | 1134 | fmt.Printf(_T("%%%d.f"), m_width); |
f6bcfd97 BP |
1135 | } |
1136 | else | |
1137 | { | |
ec53826c | 1138 | fmt.Printf(_T("%%%d.%df"), m_width, m_precision); |
f6bcfd97 BP |
1139 | } |
1140 | ||
1141 | return wxString::Format(fmt, m_valueOld); | |
1142 | } | |
1143 | ||
1144 | bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event) | |
1145 | { | |
1146 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
1147 | { | |
1148 | int keycode = event.GetKeyCode(); | |
1149 | switch ( keycode ) | |
1150 | { | |
1151 | case WXK_NUMPAD0: | |
1152 | case WXK_NUMPAD1: | |
1153 | case WXK_NUMPAD2: | |
1154 | case WXK_NUMPAD3: | |
1155 | case WXK_NUMPAD4: | |
1156 | case WXK_NUMPAD5: | |
1157 | case WXK_NUMPAD6: | |
1158 | case WXK_NUMPAD7: | |
1159 | case WXK_NUMPAD8: | |
1160 | case WXK_NUMPAD9: | |
1161 | case WXK_ADD: | |
1162 | case WXK_NUMPAD_ADD: | |
1163 | case WXK_SUBTRACT: | |
1164 | case WXK_NUMPAD_SUBTRACT: | |
1165 | case WXK_DECIMAL: | |
1166 | case WXK_NUMPAD_DECIMAL: | |
1167 | return TRUE; | |
1168 | ||
1169 | default: | |
1170 | // additionally accept 'e' as in '1e+6' | |
1171 | if ( (keycode < 128) && | |
4676948b | 1172 | (wxIsdigit(keycode) || tolower(keycode) == 'e') ) |
f6bcfd97 BP |
1173 | return TRUE; |
1174 | } | |
1175 | } | |
1176 | ||
1177 | return FALSE; | |
1178 | } | |
1179 | ||
3a8c693a VZ |
1180 | #endif // wxUSE_TEXTCTRL |
1181 | ||
1182 | #if wxUSE_CHECKBOX | |
1183 | ||
508011ce VZ |
1184 | // ---------------------------------------------------------------------------- |
1185 | // wxGridCellBoolEditor | |
1186 | // ---------------------------------------------------------------------------- | |
1187 | ||
1188 | void wxGridCellBoolEditor::Create(wxWindow* parent, | |
1189 | wxWindowID id, | |
1190 | wxEvtHandler* evtHandler) | |
1191 | { | |
1192 | m_control = new wxCheckBox(parent, id, wxEmptyString, | |
1193 | wxDefaultPosition, wxDefaultSize, | |
1194 | wxNO_BORDER); | |
1195 | ||
1196 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1197 | } | |
1198 | ||
1199 | void wxGridCellBoolEditor::SetSize(const wxRect& r) | |
1200 | { | |
b94ae1ea VZ |
1201 | bool resize = FALSE; |
1202 | wxSize size = m_control->GetSize(); | |
1203 | wxCoord minSize = wxMin(r.width, r.height); | |
1204 | ||
1205 | // check if the checkbox is not too big/small for this cell | |
1206 | wxSize sizeBest = m_control->GetBestSize(); | |
1207 | if ( !(size == sizeBest) ) | |
1208 | { | |
1209 | // reset to default size if it had been made smaller | |
1210 | size = sizeBest; | |
1211 | ||
1212 | resize = TRUE; | |
1213 | } | |
1214 | ||
1215 | if ( size.x >= minSize || size.y >= minSize ) | |
1216 | { | |
1217 | // leave 1 pixel margin | |
1218 | size.x = size.y = minSize - 2; | |
1219 | ||
1220 | resize = TRUE; | |
1221 | } | |
1222 | ||
1223 | if ( resize ) | |
1224 | { | |
1225 | m_control->SetSize(size); | |
1226 | } | |
1227 | ||
508011ce | 1228 | // position it in the centre of the rectangle (TODO: support alignment?) |
508011ce | 1229 | |
b94ae1ea | 1230 | #if defined(__WXGTK__) || defined (__WXMOTIF__) |
508011ce VZ |
1231 | // the checkbox without label still has some space to the right in wxGTK, |
1232 | // so shift it to the right | |
b94ae1ea VZ |
1233 | size.x -= 8; |
1234 | #elif defined(__WXMSW__) | |
a95e38c0 VZ |
1235 | // here too, but in other way |
1236 | size.x += 1; | |
b94ae1ea VZ |
1237 | size.y -= 2; |
1238 | #endif | |
508011ce | 1239 | |
1bd71df9 JS |
1240 | int hAlign = wxALIGN_CENTRE; |
1241 | int vAlign = wxALIGN_CENTRE; | |
1242 | if (GetCellAttr()) | |
1243 | GetCellAttr()->GetAlignment(& hAlign, & vAlign); | |
52d6f640 | 1244 | |
1bd71df9 JS |
1245 | int x = 0, y = 0; |
1246 | if (hAlign == wxALIGN_LEFT) | |
1247 | { | |
1248 | x = r.x + 2; | |
1249 | #ifdef __WXMSW__ | |
1250 | x += 2; | |
52d6f640 | 1251 | #endif |
1bd71df9 JS |
1252 | y = r.y + r.height/2 - size.y/2; |
1253 | } | |
1254 | else if (hAlign == wxALIGN_RIGHT) | |
1255 | { | |
1256 | x = r.x + r.width - size.x - 2; | |
1257 | y = r.y + r.height/2 - size.y/2; | |
1258 | } | |
1259 | else if (hAlign == wxALIGN_CENTRE) | |
1260 | { | |
1261 | x = r.x + r.width/2 - size.x/2; | |
1262 | y = r.y + r.height/2 - size.y/2; | |
1263 | } | |
52d6f640 | 1264 | |
1bd71df9 | 1265 | m_control->Move(x, y); |
508011ce VZ |
1266 | } |
1267 | ||
1268 | void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr) | |
1269 | { | |
99306db2 VZ |
1270 | m_control->Show(show); |
1271 | ||
189d0213 | 1272 | if ( show ) |
508011ce | 1273 | { |
189d0213 VZ |
1274 | wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY; |
1275 | CBox()->SetBackgroundColour(colBg); | |
508011ce | 1276 | } |
508011ce VZ |
1277 | } |
1278 | ||
1279 | void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1280 | { | |
1281 | wxASSERT_MSG(m_control, | |
1282 | wxT("The wxGridCellEditor must be Created first!")); | |
1283 | ||
28a77bc4 | 1284 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) |
f2d76237 RD |
1285 | m_startValue = grid->GetTable()->GetValueAsBool(row, col); |
1286 | else | |
695a3263 MB |
1287 | { |
1288 | wxString cellval( grid->GetTable()->GetValue(row, col) ); | |
8dd8f875 | 1289 | m_startValue = !( !cellval || (cellval == wxT("0")) ); |
695a3263 | 1290 | } |
508011ce VZ |
1291 | CBox()->SetValue(m_startValue); |
1292 | CBox()->SetFocus(); | |
1293 | } | |
1294 | ||
1295 | bool wxGridCellBoolEditor::EndEdit(int row, int col, | |
508011ce VZ |
1296 | wxGrid* grid) |
1297 | { | |
1298 | wxASSERT_MSG(m_control, | |
1299 | wxT("The wxGridCellEditor must be Created first!")); | |
1300 | ||
1301 | bool changed = FALSE; | |
1302 | bool value = CBox()->GetValue(); | |
1303 | if ( value != m_startValue ) | |
1304 | changed = TRUE; | |
1305 | ||
1306 | if ( changed ) | |
1307 | { | |
28a77bc4 | 1308 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) |
f2d76237 RD |
1309 | grid->GetTable()->SetValueAsBool(row, col, value); |
1310 | else | |
1311 | grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString); | |
508011ce VZ |
1312 | } |
1313 | ||
1314 | return changed; | |
1315 | } | |
1316 | ||
1317 | void wxGridCellBoolEditor::Reset() | |
1318 | { | |
1319 | wxASSERT_MSG(m_control, | |
1320 | wxT("The wxGridCellEditor must be Created first!")); | |
1321 | ||
1322 | CBox()->SetValue(m_startValue); | |
1323 | } | |
1324 | ||
e195a54c | 1325 | void wxGridCellBoolEditor::StartingClick() |
508011ce | 1326 | { |
e195a54c | 1327 | CBox()->SetValue(!CBox()->GetValue()); |
508011ce VZ |
1328 | } |
1329 | ||
f6bcfd97 BP |
1330 | bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event) |
1331 | { | |
1332 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
1333 | { | |
1334 | int keycode = event.GetKeyCode(); | |
1335 | switch ( keycode ) | |
1336 | { | |
1337 | case WXK_MULTIPLY: | |
1338 | case WXK_NUMPAD_MULTIPLY: | |
1339 | case WXK_ADD: | |
1340 | case WXK_NUMPAD_ADD: | |
1341 | case WXK_SUBTRACT: | |
1342 | case WXK_NUMPAD_SUBTRACT: | |
1343 | case WXK_SPACE: | |
1344 | case '+': | |
1345 | case '-': | |
1346 | return TRUE; | |
1347 | } | |
1348 | } | |
1349 | ||
1350 | return FALSE; | |
1351 | } | |
04418332 | 1352 | |
73145b0e JS |
1353 | // return the value as "1" for true and the empty string for false |
1354 | wxString wxGridCellBoolEditor::GetValue() const | |
1355 | { | |
1356 | bool bSet = CBox()->GetValue(); | |
04418332 | 1357 | return bSet ? _T("1") : wxEmptyString; |
73145b0e | 1358 | } |
f6bcfd97 | 1359 | |
3a8c693a VZ |
1360 | #endif // wxUSE_CHECKBOX |
1361 | ||
1362 | #if wxUSE_COMBOBOX | |
1363 | ||
4ee5fc9c VZ |
1364 | // ---------------------------------------------------------------------------- |
1365 | // wxGridCellChoiceEditor | |
1366 | // ---------------------------------------------------------------------------- | |
1367 | ||
7db33cc3 MB |
1368 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString& choices, |
1369 | bool allowOthers) | |
1370 | : m_choices(choices), | |
1371 | m_allowOthers(allowOthers) { } | |
1372 | ||
4ee5fc9c | 1373 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count, |
f6bcfd97 | 1374 | const wxString choices[], |
4ee5fc9c VZ |
1375 | bool allowOthers) |
1376 | : m_allowOthers(allowOthers) | |
1377 | { | |
c4608a8a | 1378 | if ( count ) |
4ee5fc9c | 1379 | { |
c4608a8a VZ |
1380 | m_choices.Alloc(count); |
1381 | for ( size_t n = 0; n < count; n++ ) | |
1382 | { | |
1383 | m_choices.Add(choices[n]); | |
1384 | } | |
4ee5fc9c VZ |
1385 | } |
1386 | } | |
1387 | ||
c4608a8a VZ |
1388 | wxGridCellEditor *wxGridCellChoiceEditor::Clone() const |
1389 | { | |
1390 | wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor; | |
1391 | editor->m_allowOthers = m_allowOthers; | |
1392 | editor->m_choices = m_choices; | |
1393 | ||
1394 | return editor; | |
1395 | } | |
1396 | ||
4ee5fc9c VZ |
1397 | void wxGridCellChoiceEditor::Create(wxWindow* parent, |
1398 | wxWindowID id, | |
1399 | wxEvtHandler* evtHandler) | |
1400 | { | |
1401 | size_t count = m_choices.GetCount(); | |
1402 | wxString *choices = new wxString[count]; | |
1403 | for ( size_t n = 0; n < count; n++ ) | |
1404 | { | |
1405 | choices[n] = m_choices[n]; | |
1406 | } | |
1407 | ||
1408 | m_control = new wxComboBox(parent, id, wxEmptyString, | |
1409 | wxDefaultPosition, wxDefaultSize, | |
1410 | count, choices, | |
1411 | m_allowOthers ? 0 : wxCB_READONLY); | |
1412 | ||
1413 | delete [] choices; | |
1414 | ||
1415 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1416 | } | |
1417 | ||
a5777624 RD |
1418 | void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell, |
1419 | wxGridCellAttr * attr) | |
4ee5fc9c VZ |
1420 | { |
1421 | // as we fill the entire client area, don't do anything here to minimize | |
1422 | // flicker | |
a5777624 RD |
1423 | |
1424 | // TODO: It doesn't actually fill the client area since the height of a | |
1425 | // combo always defaults to the standard... Until someone has time to | |
1426 | // figure out the right rectangle to paint, just do it the normal way... | |
1427 | wxGridCellEditor::PaintBackground(rectCell, attr); | |
4ee5fc9c VZ |
1428 | } |
1429 | ||
1430 | void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1431 | { | |
1432 | wxASSERT_MSG(m_control, | |
1433 | wxT("The wxGridCellEditor must be Created first!")); | |
1434 | ||
1435 | m_startValue = grid->GetTable()->GetValue(row, col); | |
1436 | ||
2b5f62a0 VZ |
1437 | if (m_allowOthers) |
1438 | Combo()->SetValue(m_startValue); | |
1439 | else | |
28a77bc4 | 1440 | { |
2b5f62a0 VZ |
1441 | // find the right position, or default to the first if not found |
1442 | int pos = Combo()->FindString(m_startValue); | |
1443 | if (pos == -1) | |
1444 | pos = 0; | |
1445 | Combo()->SetSelection(pos); | |
28a77bc4 | 1446 | } |
4ee5fc9c VZ |
1447 | Combo()->SetInsertionPointEnd(); |
1448 | Combo()->SetFocus(); | |
1449 | } | |
1450 | ||
28a77bc4 | 1451 | bool wxGridCellChoiceEditor::EndEdit(int row, int col, |
4ee5fc9c VZ |
1452 | wxGrid* grid) |
1453 | { | |
1454 | wxString value = Combo()->GetValue(); | |
1455 | bool changed = value != m_startValue; | |
1456 | ||
1457 | if ( changed ) | |
1458 | grid->GetTable()->SetValue(row, col, value); | |
1459 | ||
1460 | m_startValue = wxEmptyString; | |
2b5f62a0 VZ |
1461 | if (m_allowOthers) |
1462 | Combo()->SetValue(m_startValue); | |
1463 | else | |
1464 | Combo()->SetSelection(0); | |
4ee5fc9c VZ |
1465 | |
1466 | return changed; | |
1467 | } | |
1468 | ||
1469 | void wxGridCellChoiceEditor::Reset() | |
1470 | { | |
1471 | Combo()->SetValue(m_startValue); | |
1472 | Combo()->SetInsertionPointEnd(); | |
1473 | } | |
1474 | ||
c4608a8a VZ |
1475 | void wxGridCellChoiceEditor::SetParameters(const wxString& params) |
1476 | { | |
1477 | if ( !params ) | |
1478 | { | |
1479 | // what can we do? | |
1480 | return; | |
1481 | } | |
1482 | ||
1483 | m_choices.Empty(); | |
1484 | ||
1485 | wxStringTokenizer tk(params, _T(',')); | |
1486 | while ( tk.HasMoreTokens() ) | |
1487 | { | |
1488 | m_choices.Add(tk.GetNextToken()); | |
1489 | } | |
1490 | } | |
1491 | ||
73145b0e JS |
1492 | // return the value in the text control |
1493 | wxString wxGridCellChoiceEditor::GetValue() const | |
1494 | { | |
1495 | return Combo()->GetValue(); | |
1496 | } | |
04418332 | 1497 | |
3a8c693a VZ |
1498 | #endif // wxUSE_COMBOBOX |
1499 | ||
508011ce VZ |
1500 | // ---------------------------------------------------------------------------- |
1501 | // wxGridCellEditorEvtHandler | |
1502 | // ---------------------------------------------------------------------------- | |
2796cce3 RD |
1503 | |
1504 | void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event) | |
1505 | { | |
12a3f227 | 1506 | switch ( event.GetKeyCode() ) |
2796cce3 RD |
1507 | { |
1508 | case WXK_ESCAPE: | |
1509 | m_editor->Reset(); | |
b54ba671 | 1510 | m_grid->DisableCellEditControl(); |
2796cce3 RD |
1511 | break; |
1512 | ||
2c9a89e0 | 1513 | case WXK_TAB: |
b51c3f27 | 1514 | m_grid->GetEventHandler()->ProcessEvent( event ); |
9b4aede2 RD |
1515 | break; |
1516 | ||
2796cce3 | 1517 | case WXK_RETURN: |
faec5a43 SN |
1518 | case WXK_NUMPAD_ENTER: |
1519 | if (!m_grid->GetEventHandler()->ProcessEvent(event)) | |
2796cce3 RD |
1520 | m_editor->HandleReturn(event); |
1521 | break; | |
1522 | ||
2796cce3 RD |
1523 | |
1524 | default: | |
1525 | event.Skip(); | |
1526 | } | |
1527 | } | |
1528 | ||
fb0de762 RD |
1529 | void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event) |
1530 | { | |
12a3f227 | 1531 | switch ( event.GetKeyCode() ) |
fb0de762 RD |
1532 | { |
1533 | case WXK_ESCAPE: | |
1534 | case WXK_TAB: | |
1535 | case WXK_RETURN: | |
a4f7bf58 | 1536 | case WXK_NUMPAD_ENTER: |
fb0de762 RD |
1537 | break; |
1538 | ||
1539 | default: | |
1540 | event.Skip(); | |
1541 | } | |
1542 | } | |
1543 | ||
c4608a8a VZ |
1544 | // ---------------------------------------------------------------------------- |
1545 | // wxGridCellWorker is an (almost) empty common base class for | |
1546 | // wxGridCellRenderer and wxGridCellEditor managing ref counting | |
1547 | // ---------------------------------------------------------------------------- | |
1548 | ||
1549 | void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params)) | |
1550 | { | |
1551 | // nothing to do | |
1552 | } | |
1553 | ||
1554 | wxGridCellWorker::~wxGridCellWorker() | |
1555 | { | |
1556 | } | |
1557 | ||
508011ce VZ |
1558 | // ============================================================================ |
1559 | // renderer classes | |
1560 | // ============================================================================ | |
1561 | ||
ab79958a VZ |
1562 | // ---------------------------------------------------------------------------- |
1563 | // wxGridCellRenderer | |
1564 | // ---------------------------------------------------------------------------- | |
1565 | ||
1566 | void wxGridCellRenderer::Draw(wxGrid& grid, | |
2796cce3 | 1567 | wxGridCellAttr& attr, |
ab79958a VZ |
1568 | wxDC& dc, |
1569 | const wxRect& rect, | |
c78b3acd | 1570 | int WXUNUSED(row), int WXUNUSED(col), |
ab79958a VZ |
1571 | bool isSelected) |
1572 | { | |
1573 | dc.SetBackgroundMode( wxSOLID ); | |
1574 | ||
04418332 | 1575 | // grey out fields if the grid is disabled |
73145b0e | 1576 | if( grid.IsEnabled() ) |
ab79958a | 1577 | { |
73145b0e JS |
1578 | if ( isSelected ) |
1579 | { | |
1580 | dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) ); | |
1581 | } | |
1582 | else | |
1583 | { | |
1584 | dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) ); | |
1585 | } | |
52d6f640 | 1586 | } |
ab79958a VZ |
1587 | else |
1588 | { | |
73145b0e | 1589 | dc.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE), wxSOLID)); |
ab79958a VZ |
1590 | } |
1591 | ||
1592 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
ab79958a VZ |
1593 | dc.DrawRectangle(rect); |
1594 | } | |
1595 | ||
508011ce VZ |
1596 | // ---------------------------------------------------------------------------- |
1597 | // wxGridCellStringRenderer | |
1598 | // ---------------------------------------------------------------------------- | |
1599 | ||
816be743 VZ |
1600 | void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid, |
1601 | wxGridCellAttr& attr, | |
1602 | wxDC& dc, | |
1603 | bool isSelected) | |
ab79958a | 1604 | { |
ab79958a VZ |
1605 | dc.SetBackgroundMode( wxTRANSPARENT ); |
1606 | ||
283b7808 VZ |
1607 | // TODO some special colours for attr.IsReadOnly() case? |
1608 | ||
04418332 | 1609 | // different coloured text when the grid is disabled |
73145b0e | 1610 | if( grid.IsEnabled() ) |
ab79958a | 1611 | { |
73145b0e JS |
1612 | if ( isSelected ) |
1613 | { | |
1614 | dc.SetTextBackground( grid.GetSelectionBackground() ); | |
1615 | dc.SetTextForeground( grid.GetSelectionForeground() ); | |
1616 | } | |
1617 | else | |
1618 | { | |
1619 | dc.SetTextBackground( attr.GetBackgroundColour() ); | |
1620 | dc.SetTextForeground( attr.GetTextColour() ); | |
1621 | } | |
ab79958a VZ |
1622 | } |
1623 | else | |
1624 | { | |
73145b0e JS |
1625 | dc.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE)); |
1626 | dc.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT)); | |
ab79958a | 1627 | } |
816be743 | 1628 | |
2796cce3 | 1629 | dc.SetFont( attr.GetFont() ); |
816be743 VZ |
1630 | } |
1631 | ||
65e4e78e VZ |
1632 | wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr, |
1633 | wxDC& dc, | |
1634 | const wxString& text) | |
1635 | { | |
f6bcfd97 | 1636 | wxCoord x = 0, y = 0, max_x = 0; |
65e4e78e | 1637 | dc.SetFont(attr.GetFont()); |
f6bcfd97 BP |
1638 | wxStringTokenizer tk(text, _T('\n')); |
1639 | while ( tk.HasMoreTokens() ) | |
1640 | { | |
1641 | dc.GetTextExtent(tk.GetNextToken(), &x, &y); | |
1642 | max_x = wxMax(max_x, x); | |
1643 | } | |
1644 | ||
1645 | y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines. | |
65e4e78e | 1646 | |
f6bcfd97 | 1647 | return wxSize(max_x, y); |
65e4e78e VZ |
1648 | } |
1649 | ||
1650 | wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid, | |
1651 | wxGridCellAttr& attr, | |
1652 | wxDC& dc, | |
1653 | int row, int col) | |
1654 | { | |
1655 | return DoGetBestSize(attr, dc, grid.GetCellValue(row, col)); | |
1656 | } | |
1657 | ||
816be743 VZ |
1658 | void wxGridCellStringRenderer::Draw(wxGrid& grid, |
1659 | wxGridCellAttr& attr, | |
1660 | wxDC& dc, | |
1661 | const wxRect& rectCell, | |
1662 | int row, int col, | |
1663 | bool isSelected) | |
1664 | { | |
27f35b66 | 1665 | wxRect rect = rectCell; |
dc1f566f SN |
1666 | rect.Inflate(-1); |
1667 | ||
1668 | // erase only this cells background, overflow cells should have been erased | |
1669 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1670 | ||
1671 | int hAlign, vAlign; | |
1672 | attr.GetAlignment(&hAlign, &vAlign); | |
27f35b66 | 1673 | |
39b80349 SN |
1674 | int overflowCols = 0; |
1675 | ||
27f35b66 SN |
1676 | if (attr.GetOverflow()) |
1677 | { | |
1678 | int cols = grid.GetNumberCols(); | |
1679 | int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth(); | |
1680 | int cell_rows, cell_cols; | |
1681 | attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <=0 | |
1682 | if ((best_width > rectCell.width) && (col < cols) && grid.GetTable()) | |
1683 | { | |
1684 | int i, c_cols, c_rows; | |
1685 | for (i = col+cell_cols; i < cols; i++) | |
1686 | { | |
2b5f62a0 | 1687 | bool is_empty = TRUE; |
39b80349 | 1688 | for (int j=row; j<row+cell_rows; j++) |
27f35b66 | 1689 | { |
39b80349 | 1690 | // check w/ anchor cell for multicell block |
ef4d6ce8 | 1691 | grid.GetCellSize(j, i, &c_rows, &c_cols); |
39b80349 | 1692 | if (c_rows > 0) c_rows = 0; |
ef4d6ce8 | 1693 | if (!grid.GetTable()->IsEmptyCell(j+c_rows, i)) |
39b80349 | 1694 | { |
2b5f62a0 | 1695 | is_empty = FALSE; |
ef4d6ce8 | 1696 | break; |
39b80349 | 1697 | } |
27f35b66 | 1698 | } |
39b80349 SN |
1699 | if (is_empty) |
1700 | rect.width += grid.GetColSize(i); | |
dc1f566f SN |
1701 | else |
1702 | { | |
1703 | i--; | |
1704 | break; | |
1705 | } | |
39b80349 | 1706 | if (rect.width >= best_width) break; |
2b5f62a0 | 1707 | } |
39b80349 | 1708 | overflowCols = i - col - cell_cols + 1; |
2b5f62a0 | 1709 | if (overflowCols >= cols) overflowCols = cols - 1; |
39b80349 | 1710 | } |
27f35b66 | 1711 | |
dc1f566f SN |
1712 | if (overflowCols > 0) // redraw overflow cells w/ proper hilight |
1713 | { | |
39b80349 | 1714 | hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned |
2b5f62a0 | 1715 | wxRect clip = rect; |
39b80349 | 1716 | clip.x += rectCell.width; |
dc1f566f SN |
1717 | // draw each overflow cell individually |
1718 | int col_end = col+cell_cols+overflowCols; | |
1719 | if (col_end >= grid.GetNumberCols()) | |
2b5f62a0 | 1720 | col_end = grid.GetNumberCols() - 1; |
dc1f566f SN |
1721 | for (int i = col+cell_cols; i <= col_end; i++) |
1722 | { | |
dc1f566f SN |
1723 | clip.width = grid.GetColSize(i) - 1; |
1724 | dc.DestroyClippingRegion(); | |
1725 | dc.SetClippingRegion(clip); | |
39b80349 SN |
1726 | |
1727 | SetTextColoursAndFont(grid, attr, dc, | |
2b5f62a0 | 1728 | grid.IsInSelection(row,i)); |
39b80349 | 1729 | |
dc1f566f | 1730 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), |
2b5f62a0 | 1731 | rect, hAlign, vAlign); |
39b80349 | 1732 | clip.x += grid.GetColSize(i) - 1; |
dc1f566f | 1733 | } |
ab79958a | 1734 | |
39b80349 | 1735 | rect = rectCell; |
2b5f62a0 | 1736 | rect.Inflate(-1); |
dc1f566f | 1737 | rect.width++; |
39b80349 | 1738 | dc.DestroyClippingRegion(); |
dc1f566f | 1739 | } |
39b80349 | 1740 | } |
ab79958a | 1741 | |
dc1f566f SN |
1742 | // now we only have to draw the text |
1743 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
39b80349 | 1744 | |
ab79958a VZ |
1745 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), |
1746 | rect, hAlign, vAlign); | |
1747 | } | |
1748 | ||
65e4e78e VZ |
1749 | // ---------------------------------------------------------------------------- |
1750 | // wxGridCellNumberRenderer | |
1751 | // ---------------------------------------------------------------------------- | |
1752 | ||
1753 | wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col) | |
1754 | { | |
1755 | wxGridTableBase *table = grid.GetTable(); | |
1756 | wxString text; | |
1757 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
1758 | { | |
1759 | text.Printf(_T("%ld"), table->GetValueAsLong(row, col)); | |
1760 | } | |
9c4ba614 VZ |
1761 | else |
1762 | { | |
1763 | text = table->GetValue(row, col); | |
1764 | } | |
65e4e78e VZ |
1765 | |
1766 | return text; | |
1767 | } | |
1768 | ||
816be743 VZ |
1769 | void wxGridCellNumberRenderer::Draw(wxGrid& grid, |
1770 | wxGridCellAttr& attr, | |
1771 | wxDC& dc, | |
1772 | const wxRect& rectCell, | |
1773 | int row, int col, | |
1774 | bool isSelected) | |
1775 | { | |
1776 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1777 | ||
1778 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1779 | ||
1780 | // draw the text right aligned by default | |
1781 | int hAlign, vAlign; | |
1782 | attr.GetAlignment(&hAlign, &vAlign); | |
4c7277db | 1783 | hAlign = wxALIGN_RIGHT; |
816be743 VZ |
1784 | |
1785 | wxRect rect = rectCell; | |
1786 | rect.Inflate(-1); | |
1787 | ||
65e4e78e VZ |
1788 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
1789 | } | |
816be743 | 1790 | |
65e4e78e VZ |
1791 | wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid, |
1792 | wxGridCellAttr& attr, | |
1793 | wxDC& dc, | |
1794 | int row, int col) | |
1795 | { | |
1796 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
816be743 VZ |
1797 | } |
1798 | ||
1799 | // ---------------------------------------------------------------------------- | |
1800 | // wxGridCellFloatRenderer | |
1801 | // ---------------------------------------------------------------------------- | |
1802 | ||
1803 | wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision) | |
1804 | { | |
1805 | SetWidth(width); | |
1806 | SetPrecision(precision); | |
1807 | } | |
1808 | ||
e72b4213 VZ |
1809 | wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const |
1810 | { | |
1811 | wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer; | |
1812 | renderer->m_width = m_width; | |
1813 | renderer->m_precision = m_precision; | |
1814 | renderer->m_format = m_format; | |
1815 | ||
1816 | return renderer; | |
1817 | } | |
1818 | ||
65e4e78e VZ |
1819 | wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col) |
1820 | { | |
1821 | wxGridTableBase *table = grid.GetTable(); | |
0b190b0f VZ |
1822 | |
1823 | bool hasDouble; | |
1824 | double val; | |
65e4e78e VZ |
1825 | wxString text; |
1826 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
1827 | { | |
0b190b0f VZ |
1828 | val = table->GetValueAsDouble(row, col); |
1829 | hasDouble = TRUE; | |
65e4e78e | 1830 | } |
9c4ba614 VZ |
1831 | else |
1832 | { | |
1833 | text = table->GetValue(row, col); | |
0b190b0f | 1834 | hasDouble = text.ToDouble(&val); |
9c4ba614 | 1835 | } |
65e4e78e | 1836 | |
0b190b0f VZ |
1837 | if ( hasDouble ) |
1838 | { | |
1839 | if ( !m_format ) | |
1840 | { | |
1841 | if ( m_width == -1 ) | |
1842 | { | |
19d7140e VZ |
1843 | if ( m_precision == -1 ) |
1844 | { | |
2b5f62a0 VZ |
1845 | // default width/precision |
1846 | m_format = _T("%f"); | |
1847 | } | |
19d7140e VZ |
1848 | else |
1849 | { | |
1850 | m_format.Printf(_T("%%.%df"), m_precision); | |
1851 | } | |
0b190b0f VZ |
1852 | } |
1853 | else if ( m_precision == -1 ) | |
1854 | { | |
1855 | // default precision | |
1856 | m_format.Printf(_T("%%%d.f"), m_width); | |
1857 | } | |
1858 | else | |
1859 | { | |
1860 | m_format.Printf(_T("%%%d.%df"), m_width, m_precision); | |
1861 | } | |
1862 | } | |
1863 | ||
1864 | text.Printf(m_format, val); | |
19d7140e | 1865 | |
0b190b0f VZ |
1866 | } |
1867 | //else: text already contains the string | |
1868 | ||
65e4e78e VZ |
1869 | return text; |
1870 | } | |
1871 | ||
816be743 VZ |
1872 | void wxGridCellFloatRenderer::Draw(wxGrid& grid, |
1873 | wxGridCellAttr& attr, | |
1874 | wxDC& dc, | |
1875 | const wxRect& rectCell, | |
1876 | int row, int col, | |
1877 | bool isSelected) | |
1878 | { | |
1879 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1880 | ||
1881 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1882 | ||
1883 | // draw the text right aligned by default | |
1884 | int hAlign, vAlign; | |
1885 | attr.GetAlignment(&hAlign, &vAlign); | |
4c7277db | 1886 | hAlign = wxALIGN_RIGHT; |
816be743 VZ |
1887 | |
1888 | wxRect rect = rectCell; | |
1889 | rect.Inflate(-1); | |
1890 | ||
65e4e78e VZ |
1891 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
1892 | } | |
816be743 | 1893 | |
65e4e78e VZ |
1894 | wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid, |
1895 | wxGridCellAttr& attr, | |
1896 | wxDC& dc, | |
1897 | int row, int col) | |
1898 | { | |
1899 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
816be743 VZ |
1900 | } |
1901 | ||
0b190b0f VZ |
1902 | void wxGridCellFloatRenderer::SetParameters(const wxString& params) |
1903 | { | |
0b190b0f VZ |
1904 | if ( !params ) |
1905 | { | |
1906 | // reset to defaults | |
1907 | SetWidth(-1); | |
1908 | SetPrecision(-1); | |
1909 | } | |
1910 | else | |
1911 | { | |
1912 | wxString tmp = params.BeforeFirst(_T(',')); | |
1913 | if ( !!tmp ) | |
1914 | { | |
1915 | long width; | |
19d7140e | 1916 | if ( tmp.ToLong(&width) ) |
0b190b0f | 1917 | { |
19d7140e | 1918 | SetWidth((int)width); |
0b190b0f VZ |
1919 | } |
1920 | else | |
1921 | { | |
19d7140e VZ |
1922 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str()); |
1923 | } | |
0b190b0f | 1924 | |
19d7140e | 1925 | } |
0b190b0f VZ |
1926 | tmp = params.AfterFirst(_T(',')); |
1927 | if ( !!tmp ) | |
1928 | { | |
1929 | long precision; | |
19d7140e | 1930 | if ( tmp.ToLong(&precision) ) |
0b190b0f | 1931 | { |
19d7140e | 1932 | SetPrecision((int)precision); |
0b190b0f VZ |
1933 | } |
1934 | else | |
1935 | { | |
19d7140e | 1936 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str()); |
0b190b0f VZ |
1937 | } |
1938 | ||
0b190b0f VZ |
1939 | } |
1940 | } | |
1941 | } | |
1942 | ||
19d7140e | 1943 | |
508011ce VZ |
1944 | // ---------------------------------------------------------------------------- |
1945 | // wxGridCellBoolRenderer | |
1946 | // ---------------------------------------------------------------------------- | |
1947 | ||
65e4e78e | 1948 | wxSize wxGridCellBoolRenderer::ms_sizeCheckMark; |
508011ce | 1949 | |
b94ae1ea VZ |
1950 | // FIXME these checkbox size calculations are really ugly... |
1951 | ||
65e4e78e | 1952 | // between checkmark and box |
a95e38c0 | 1953 | static const wxCoord wxGRID_CHECKMARK_MARGIN = 2; |
508011ce | 1954 | |
65e4e78e VZ |
1955 | wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid, |
1956 | wxGridCellAttr& WXUNUSED(attr), | |
1957 | wxDC& WXUNUSED(dc), | |
1958 | int WXUNUSED(row), | |
1959 | int WXUNUSED(col)) | |
1960 | { | |
1961 | // compute it only once (no locks for MT safeness in GUI thread...) | |
1962 | if ( !ms_sizeCheckMark.x ) | |
297da4ba | 1963 | { |
65e4e78e | 1964 | // get checkbox size |
297da4ba VZ |
1965 | wxCheckBox *checkbox = new wxCheckBox(&grid, -1, wxEmptyString); |
1966 | wxSize size = checkbox->GetBestSize(); | |
999836aa | 1967 | wxCoord checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN; |
297da4ba | 1968 | |
65e4e78e | 1969 | // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result |
69d8f612 | 1970 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
65e4e78e | 1971 | checkSize -= size.y / 2; |
297da4ba VZ |
1972 | #endif |
1973 | ||
1974 | delete checkbox; | |
65e4e78e VZ |
1975 | |
1976 | ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize; | |
297da4ba VZ |
1977 | } |
1978 | ||
65e4e78e VZ |
1979 | return ms_sizeCheckMark; |
1980 | } | |
1981 | ||
1982 | void wxGridCellBoolRenderer::Draw(wxGrid& grid, | |
1983 | wxGridCellAttr& attr, | |
1984 | wxDC& dc, | |
1985 | const wxRect& rect, | |
1986 | int row, int col, | |
1987 | bool isSelected) | |
1988 | { | |
1989 | wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); | |
1990 | ||
297da4ba | 1991 | // draw a check mark in the centre (ignoring alignment - TODO) |
65e4e78e | 1992 | wxSize size = GetBestSize(grid, attr, dc, row, col); |
b94ae1ea VZ |
1993 | |
1994 | // don't draw outside the cell | |
1995 | wxCoord minSize = wxMin(rect.width, rect.height); | |
1996 | if ( size.x >= minSize || size.y >= minSize ) | |
1997 | { | |
1998 | // and even leave (at least) 1 pixel margin | |
1999 | size.x = size.y = minSize - 2; | |
2000 | } | |
2001 | ||
2002 | // draw a border around checkmark | |
1bd71df9 JS |
2003 | int vAlign, hAlign; |
2004 | attr.GetAlignment(& hAlign, &vAlign); | |
52d6f640 | 2005 | |
a95e38c0 | 2006 | wxRect rectBorder; |
1bd71df9 JS |
2007 | if (hAlign == wxALIGN_CENTRE) |
2008 | { | |
2009 | rectBorder.x = rect.x + rect.width/2 - size.x/2; | |
2010 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2011 | rectBorder.width = size.x; | |
2012 | rectBorder.height = size.y; | |
2013 | } | |
2014 | else if (hAlign == wxALIGN_LEFT) | |
2015 | { | |
2016 | rectBorder.x = rect.x + 2; | |
2017 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2018 | rectBorder.width = size.x; | |
52d6f640 | 2019 | rectBorder.height = size.y; |
1bd71df9 JS |
2020 | } |
2021 | else if (hAlign == wxALIGN_RIGHT) | |
2022 | { | |
2023 | rectBorder.x = rect.x + rect.width - size.x - 2; | |
2024 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2025 | rectBorder.width = size.x; | |
52d6f640 | 2026 | rectBorder.height = size.y; |
1bd71df9 | 2027 | } |
b94ae1ea | 2028 | |
f2d76237 | 2029 | bool value; |
b94ae1ea | 2030 | if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) |
f2d76237 RD |
2031 | value = grid.GetTable()->GetValueAsBool(row, col); |
2032 | else | |
695a3263 MB |
2033 | { |
2034 | wxString cellval( grid.GetTable()->GetValue(row, col) ); | |
8dd8f875 | 2035 | value = !( !cellval || (cellval == wxT("0")) ); |
695a3263 | 2036 | } |
f2d76237 RD |
2037 | |
2038 | if ( value ) | |
508011ce | 2039 | { |
a95e38c0 VZ |
2040 | wxRect rectMark = rectBorder; |
2041 | #ifdef __WXMSW__ | |
2042 | // MSW DrawCheckMark() is weird (and should probably be changed...) | |
2043 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2); | |
2044 | rectMark.x++; | |
2045 | rectMark.y++; | |
2046 | #else // !MSW | |
2047 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN); | |
2048 | #endif // MSW/!MSW | |
2049 | ||
508011ce VZ |
2050 | dc.SetTextForeground(attr.GetTextColour()); |
2051 | dc.DrawCheckMark(rectMark); | |
2052 | } | |
a95e38c0 VZ |
2053 | |
2054 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
2055 | dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID)); | |
2056 | dc.DrawRectangle(rectBorder); | |
508011ce VZ |
2057 | } |
2058 | ||
2796cce3 RD |
2059 | // ---------------------------------------------------------------------------- |
2060 | // wxGridCellAttr | |
2061 | // ---------------------------------------------------------------------------- | |
2062 | ||
1df4050d VZ |
2063 | void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) |
2064 | { | |
2065 | m_nRef = 1; | |
2066 | ||
2067 | m_isReadOnly = Unset; | |
2068 | ||
2069 | m_renderer = NULL; | |
2070 | m_editor = NULL; | |
2071 | ||
2072 | m_attrkind = wxGridCellAttr::Cell; | |
2073 | ||
27f35b66 | 2074 | m_sizeRows = m_sizeCols = 1; |
b63fce94 | 2075 | m_overflow = UnsetOverflow; |
27f35b66 | 2076 | |
1df4050d VZ |
2077 | SetDefAttr(attrDefault); |
2078 | } | |
2079 | ||
39bcce60 | 2080 | wxGridCellAttr *wxGridCellAttr::Clone() const |
a68c1246 | 2081 | { |
1df4050d VZ |
2082 | wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr); |
2083 | ||
a68c1246 VZ |
2084 | if ( HasTextColour() ) |
2085 | attr->SetTextColour(GetTextColour()); | |
2086 | if ( HasBackgroundColour() ) | |
2087 | attr->SetBackgroundColour(GetBackgroundColour()); | |
2088 | if ( HasFont() ) | |
2089 | attr->SetFont(GetFont()); | |
2090 | if ( HasAlignment() ) | |
2091 | attr->SetAlignment(m_hAlign, m_vAlign); | |
2092 | ||
27f35b66 SN |
2093 | attr->SetSize( m_sizeRows, m_sizeCols ); |
2094 | ||
a68c1246 VZ |
2095 | if ( m_renderer ) |
2096 | { | |
2097 | attr->SetRenderer(m_renderer); | |
39bcce60 | 2098 | m_renderer->IncRef(); |
a68c1246 VZ |
2099 | } |
2100 | if ( m_editor ) | |
2101 | { | |
2102 | attr->SetEditor(m_editor); | |
39bcce60 | 2103 | m_editor->IncRef(); |
a68c1246 VZ |
2104 | } |
2105 | ||
2106 | if ( IsReadOnly() ) | |
2107 | attr->SetReadOnly(); | |
2108 | ||
19d7140e VZ |
2109 | attr->SetKind( m_attrkind ); |
2110 | ||
a68c1246 VZ |
2111 | return attr; |
2112 | } | |
2113 | ||
19d7140e VZ |
2114 | void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) |
2115 | { | |
2116 | if ( !HasTextColour() && mergefrom->HasTextColour() ) | |
2117 | SetTextColour(mergefrom->GetTextColour()); | |
2118 | if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() ) | |
2119 | SetBackgroundColour(mergefrom->GetBackgroundColour()); | |
2120 | if ( !HasFont() && mergefrom->HasFont() ) | |
2121 | SetFont(mergefrom->GetFont()); | |
7e48d7d9 | 2122 | if ( !HasAlignment() && mergefrom->HasAlignment() ){ |
19d7140e VZ |
2123 | int hAlign, vAlign; |
2124 | mergefrom->GetAlignment( &hAlign, &vAlign); | |
2125 | SetAlignment(hAlign, vAlign); | |
2126 | } | |
2127 | ||
27f35b66 SN |
2128 | mergefrom->GetSize( &m_sizeRows, &m_sizeCols ); |
2129 | ||
19d7140e VZ |
2130 | // Directly access member functions as GetRender/Editor don't just return |
2131 | // m_renderer/m_editor | |
2132 | // | |
2133 | // Maybe add support for merge of Render and Editor? | |
2134 | if (!HasRenderer() && mergefrom->HasRenderer() ) | |
bf7945ce | 2135 | { |
19d7140e VZ |
2136 | m_renderer = mergefrom->m_renderer; |
2137 | m_renderer->IncRef(); | |
2138 | } | |
2139 | if ( !HasEditor() && mergefrom->HasEditor() ) | |
2140 | { | |
2141 | m_editor = mergefrom->m_editor; | |
2142 | m_editor->IncRef(); | |
2143 | } | |
2144 | if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) | |
2145 | SetReadOnly(mergefrom->IsReadOnly()); | |
2146 | ||
ef5df12b | 2147 | if (!HasOverflowMode() && mergefrom->HasOverflowMode() ) |
ff699386 | 2148 | SetOverflow(mergefrom->GetOverflow()); |
ef5df12b | 2149 | |
19d7140e VZ |
2150 | SetDefAttr(mergefrom->m_defGridAttr); |
2151 | } | |
2152 | ||
27f35b66 SN |
2153 | void wxGridCellAttr::SetSize(int num_rows, int num_cols) |
2154 | { | |
2155 | // The size of a cell is normally 1,1 | |
2156 | ||
2157 | // If this cell is larger (2,2) then this is the top left cell | |
2158 | // the other cells that will be covered (lower right cells) must be | |
2159 | // set to negative or zero values such that | |
2160 | // row + num_rows of the covered cell points to the larger cell (this cell) | |
2161 | // same goes for the col + num_cols. | |
2162 | ||
2163 | // Size of 0,0 is NOT valid, neither is <=0 and any positive value | |
2164 | ||
2165 | wxASSERT_MSG( (!((num_rows>0)&&(num_cols<=0)) || | |
2166 | !((num_rows<=0)&&(num_cols>0)) || | |
2167 | !((num_rows==0)&&(num_cols==0))), | |
2168 | wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values")); | |
2169 | ||
2170 | m_sizeRows = num_rows; | |
2171 | m_sizeCols = num_cols; | |
2172 | } | |
2173 | ||
2796cce3 RD |
2174 | const wxColour& wxGridCellAttr::GetTextColour() const |
2175 | { | |
2176 | if (HasTextColour()) | |
508011ce | 2177 | { |
2796cce3 | 2178 | return m_colText; |
508011ce | 2179 | } |
0926b2fc | 2180 | else if (m_defGridAttr && m_defGridAttr != this) |
508011ce | 2181 | { |
2796cce3 | 2182 | return m_defGridAttr->GetTextColour(); |
508011ce VZ |
2183 | } |
2184 | else | |
2185 | { | |
2796cce3 RD |
2186 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2187 | return wxNullColour; | |
2188 | } | |
2189 | } | |
2190 | ||
2191 | ||
2192 | const wxColour& wxGridCellAttr::GetBackgroundColour() const | |
2193 | { | |
2194 | if (HasBackgroundColour()) | |
2195 | return m_colBack; | |
0926b2fc | 2196 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2197 | return m_defGridAttr->GetBackgroundColour(); |
508011ce VZ |
2198 | else |
2199 | { | |
2796cce3 RD |
2200 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2201 | return wxNullColour; | |
2202 | } | |
2203 | } | |
2204 | ||
2205 | ||
2206 | const wxFont& wxGridCellAttr::GetFont() const | |
2207 | { | |
2208 | if (HasFont()) | |
2209 | return m_font; | |
0926b2fc | 2210 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2211 | return m_defGridAttr->GetFont(); |
508011ce VZ |
2212 | else |
2213 | { | |
2796cce3 RD |
2214 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2215 | return wxNullFont; | |
2216 | } | |
2217 | } | |
2218 | ||
2219 | ||
2220 | void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const | |
2221 | { | |
508011ce VZ |
2222 | if (HasAlignment()) |
2223 | { | |
2796cce3 RD |
2224 | if ( hAlign ) *hAlign = m_hAlign; |
2225 | if ( vAlign ) *vAlign = m_vAlign; | |
2226 | } | |
0926b2fc | 2227 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2228 | m_defGridAttr->GetAlignment(hAlign, vAlign); |
508011ce VZ |
2229 | else |
2230 | { | |
2796cce3 RD |
2231 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2232 | } | |
2233 | } | |
2234 | ||
27f35b66 SN |
2235 | void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const |
2236 | { | |
2237 | if ( num_rows ) *num_rows = m_sizeRows; | |
2238 | if ( num_cols ) *num_cols = m_sizeCols; | |
2239 | } | |
2796cce3 | 2240 | |
f2d76237 | 2241 | // GetRenderer and GetEditor use a slightly different decision path about |
28a77bc4 RD |
2242 | // which attribute to use. If a non-default attr object has one then it is |
2243 | // used, otherwise the default editor or renderer is fetched from the grid and | |
2244 | // used. It should be the default for the data type of the cell. If it is | |
2245 | // NULL (because the table has a type that the grid does not have in its | |
2246 | // registry,) then the grid's default editor or renderer is used. | |
2247 | ||
2248 | wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const | |
2249 | { | |
3cf883a2 | 2250 | wxGridCellRenderer *renderer; |
28a77bc4 | 2251 | |
3cf883a2 | 2252 | if ( m_renderer && this != m_defGridAttr ) |
0b190b0f | 2253 | { |
3cf883a2 VZ |
2254 | // use the cells renderer if it has one |
2255 | renderer = m_renderer; | |
2256 | renderer->IncRef(); | |
0b190b0f | 2257 | } |
3cf883a2 | 2258 | else // no non default cell renderer |
0b190b0f | 2259 | { |
3cf883a2 VZ |
2260 | // get default renderer for the data type |
2261 | if ( grid ) | |
2262 | { | |
2263 | // GetDefaultRendererForCell() will do IncRef() for us | |
2264 | renderer = grid->GetDefaultRendererForCell(row, col); | |
2265 | } | |
2266 | else | |
2267 | { | |
2268 | renderer = NULL; | |
2269 | } | |
0b190b0f | 2270 | |
3cf883a2 VZ |
2271 | if ( !renderer ) |
2272 | { | |
0926b2fc | 2273 | if (m_defGridAttr && this != m_defGridAttr ) |
3cf883a2 VZ |
2274 | { |
2275 | // if we still don't have one then use the grid default | |
2276 | // (no need for IncRef() here neither) | |
2277 | renderer = m_defGridAttr->GetRenderer(NULL, 0, 0); | |
2278 | } | |
2279 | else // default grid attr | |
2280 | { | |
2281 | // use m_renderer which we had decided not to use initially | |
2282 | renderer = m_renderer; | |
2283 | if ( renderer ) | |
2284 | renderer->IncRef(); | |
2285 | } | |
2286 | } | |
0b190b0f | 2287 | } |
28a77bc4 | 2288 | |
3cf883a2 VZ |
2289 | // we're supposed to always find something |
2290 | wxASSERT_MSG(renderer, wxT("Missing default cell renderer")); | |
28a77bc4 RD |
2291 | |
2292 | return renderer; | |
2796cce3 RD |
2293 | } |
2294 | ||
3cf883a2 | 2295 | // same as above, except for s/renderer/editor/g |
28a77bc4 | 2296 | wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const |
07296f0b | 2297 | { |
3cf883a2 | 2298 | wxGridCellEditor *editor; |
0b190b0f | 2299 | |
3cf883a2 | 2300 | if ( m_editor && this != m_defGridAttr ) |
0b190b0f | 2301 | { |
3cf883a2 VZ |
2302 | // use the cells editor if it has one |
2303 | editor = m_editor; | |
2304 | editor->IncRef(); | |
0b190b0f | 2305 | } |
3cf883a2 | 2306 | else // no non default cell editor |
0b190b0f | 2307 | { |
3cf883a2 VZ |
2308 | // get default editor for the data type |
2309 | if ( grid ) | |
2310 | { | |
2311 | // GetDefaultEditorForCell() will do IncRef() for us | |
2312 | editor = grid->GetDefaultEditorForCell(row, col); | |
2313 | } | |
2314 | else | |
2315 | { | |
2316 | editor = NULL; | |
2317 | } | |
2318 | ||
2319 | if ( !editor ) | |
2320 | { | |
0926b2fc | 2321 | if ( m_defGridAttr && this != m_defGridAttr ) |
3cf883a2 VZ |
2322 | { |
2323 | // if we still don't have one then use the grid default | |
2324 | // (no need for IncRef() here neither) | |
2325 | editor = m_defGridAttr->GetEditor(NULL, 0, 0); | |
2326 | } | |
2327 | else // default grid attr | |
2328 | { | |
2329 | // use m_editor which we had decided not to use initially | |
2330 | editor = m_editor; | |
2331 | if ( editor ) | |
2332 | editor->IncRef(); | |
2333 | } | |
2334 | } | |
0b190b0f | 2335 | } |
28a77bc4 | 2336 | |
3cf883a2 VZ |
2337 | // we're supposed to always find something |
2338 | wxASSERT_MSG(editor, wxT("Missing default cell editor")); | |
2339 | ||
28a77bc4 | 2340 | return editor; |
07296f0b RD |
2341 | } |
2342 | ||
b99be8fb | 2343 | // ---------------------------------------------------------------------------- |
758cbedf | 2344 | // wxGridCellAttrData |
b99be8fb VZ |
2345 | // ---------------------------------------------------------------------------- |
2346 | ||
758cbedf | 2347 | void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) |
b99be8fb VZ |
2348 | { |
2349 | int n = FindIndex(row, col); | |
2350 | if ( n == wxNOT_FOUND ) | |
2351 | { | |
2352 | // add the attribute | |
2353 | m_attrs.Add(new wxGridCellWithAttr(row, col, attr)); | |
2354 | } | |
2355 | else | |
2356 | { | |
6f36917b VZ |
2357 | // free the old attribute |
2358 | m_attrs[(size_t)n].attr->DecRef(); | |
2359 | ||
b99be8fb VZ |
2360 | if ( attr ) |
2361 | { | |
2362 | // change the attribute | |
2e9a6788 | 2363 | m_attrs[(size_t)n].attr = attr; |
b99be8fb VZ |
2364 | } |
2365 | else | |
2366 | { | |
2367 | // remove this attribute | |
2368 | m_attrs.RemoveAt((size_t)n); | |
2369 | } | |
2370 | } | |
b99be8fb VZ |
2371 | } |
2372 | ||
758cbedf | 2373 | wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const |
b99be8fb VZ |
2374 | { |
2375 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2376 | ||
2377 | int n = FindIndex(row, col); | |
2378 | if ( n != wxNOT_FOUND ) | |
2379 | { | |
2e9a6788 VZ |
2380 | attr = m_attrs[(size_t)n].attr; |
2381 | attr->IncRef(); | |
b99be8fb VZ |
2382 | } |
2383 | ||
2384 | return attr; | |
2385 | } | |
2386 | ||
4d60017a SN |
2387 | void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) |
2388 | { | |
2389 | size_t count = m_attrs.GetCount(); | |
2390 | for ( size_t n = 0; n < count; n++ ) | |
2391 | { | |
2392 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
2393 | wxCoord row = coords.GetRow(); |
2394 | if ((size_t)row >= pos) | |
2395 | { | |
2396 | if (numRows > 0) | |
2397 | { | |
2398 | // If rows inserted, include row counter where necessary | |
2399 | coords.SetRow(row + numRows); | |
2400 | } | |
2401 | else if (numRows < 0) | |
2402 | { | |
2403 | // If rows deleted ... | |
2404 | if ((size_t)row >= pos - numRows) | |
2405 | { | |
2406 | // ...either decrement row counter (if row still exists)... | |
2407 | coords.SetRow(row + numRows); | |
2408 | } | |
2409 | else | |
2410 | { | |
2411 | // ...or remove the attribute | |
2412 | m_attrs.RemoveAt((size_t)n); | |
2413 | n--; count--; | |
2414 | } | |
2415 | } | |
4d60017a SN |
2416 | } |
2417 | } | |
2418 | } | |
2419 | ||
2420 | void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) | |
2421 | { | |
2422 | size_t count = m_attrs.GetCount(); | |
2423 | for ( size_t n = 0; n < count; n++ ) | |
2424 | { | |
2425 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
2426 | wxCoord col = coords.GetCol(); |
2427 | if ( (size_t)col >= pos ) | |
2428 | { | |
2429 | if ( numCols > 0 ) | |
2430 | { | |
2431 | // If rows inserted, include row counter where necessary | |
2432 | coords.SetCol(col + numCols); | |
2433 | } | |
2434 | else if (numCols < 0) | |
2435 | { | |
2436 | // If rows deleted ... | |
2437 | if ((size_t)col >= pos - numCols) | |
2438 | { | |
2439 | // ...either decrement row counter (if row still exists)... | |
2440 | coords.SetCol(col + numCols); | |
2441 | } | |
2442 | else | |
2443 | { | |
2444 | // ...or remove the attribute | |
2445 | m_attrs.RemoveAt((size_t)n); | |
2446 | n--; count--; | |
2447 | } | |
2448 | } | |
4d60017a SN |
2449 | } |
2450 | } | |
2451 | } | |
2452 | ||
758cbedf | 2453 | int wxGridCellAttrData::FindIndex(int row, int col) const |
b99be8fb VZ |
2454 | { |
2455 | size_t count = m_attrs.GetCount(); | |
2456 | for ( size_t n = 0; n < count; n++ ) | |
2457 | { | |
2458 | const wxGridCellCoords& coords = m_attrs[n].coords; | |
2459 | if ( (coords.GetRow() == row) && (coords.GetCol() == col) ) | |
2460 | { | |
2461 | return n; | |
2462 | } | |
2463 | } | |
2464 | ||
2465 | return wxNOT_FOUND; | |
2466 | } | |
2467 | ||
758cbedf VZ |
2468 | // ---------------------------------------------------------------------------- |
2469 | // wxGridRowOrColAttrData | |
2470 | // ---------------------------------------------------------------------------- | |
2471 | ||
2472 | wxGridRowOrColAttrData::~wxGridRowOrColAttrData() | |
2473 | { | |
2474 | size_t count = m_attrs.Count(); | |
2475 | for ( size_t n = 0; n < count; n++ ) | |
2476 | { | |
2477 | m_attrs[n]->DecRef(); | |
2478 | } | |
2479 | } | |
2480 | ||
2481 | wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const | |
2482 | { | |
2483 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2484 | ||
2485 | int n = m_rowsOrCols.Index(rowOrCol); | |
2486 | if ( n != wxNOT_FOUND ) | |
2487 | { | |
2488 | attr = m_attrs[(size_t)n]; | |
2489 | attr->IncRef(); | |
2490 | } | |
2491 | ||
2492 | return attr; | |
2493 | } | |
2494 | ||
2495 | void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) | |
2496 | { | |
a95e38c0 VZ |
2497 | int i = m_rowsOrCols.Index(rowOrCol); |
2498 | if ( i == wxNOT_FOUND ) | |
758cbedf VZ |
2499 | { |
2500 | // add the attribute | |
2501 | m_rowsOrCols.Add(rowOrCol); | |
2502 | m_attrs.Add(attr); | |
2503 | } | |
2504 | else | |
2505 | { | |
a95e38c0 | 2506 | size_t n = (size_t)i; |
758cbedf VZ |
2507 | if ( attr ) |
2508 | { | |
2509 | // change the attribute | |
a95e38c0 VZ |
2510 | m_attrs[n]->DecRef(); |
2511 | m_attrs[n] = attr; | |
758cbedf VZ |
2512 | } |
2513 | else | |
2514 | { | |
2515 | // remove this attribute | |
a95e38c0 VZ |
2516 | m_attrs[n]->DecRef(); |
2517 | m_rowsOrCols.RemoveAt(n); | |
2518 | m_attrs.RemoveAt(n); | |
758cbedf VZ |
2519 | } |
2520 | } | |
2521 | } | |
2522 | ||
4d60017a SN |
2523 | void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) |
2524 | { | |
2525 | size_t count = m_attrs.GetCount(); | |
2526 | for ( size_t n = 0; n < count; n++ ) | |
2527 | { | |
2528 | int & rowOrCol = m_rowsOrCols[n]; | |
d1c0b4f9 VZ |
2529 | if ( (size_t)rowOrCol >= pos ) |
2530 | { | |
2531 | if ( numRowsOrCols > 0 ) | |
2532 | { | |
2533 | // If rows inserted, include row counter where necessary | |
2534 | rowOrCol += numRowsOrCols; | |
2535 | } | |
2536 | else if ( numRowsOrCols < 0) | |
2537 | { | |
2538 | // If rows deleted, either decrement row counter (if row still exists) | |
2539 | if ((size_t)rowOrCol >= pos - numRowsOrCols) | |
2540 | rowOrCol += numRowsOrCols; | |
2541 | else | |
2542 | { | |
2543 | m_rowsOrCols.RemoveAt((size_t)n); | |
2544 | m_attrs.RemoveAt((size_t)n); | |
2545 | n--; count--; | |
2546 | } | |
2547 | } | |
4d60017a SN |
2548 | } |
2549 | } | |
2550 | } | |
2551 | ||
b99be8fb VZ |
2552 | // ---------------------------------------------------------------------------- |
2553 | // wxGridCellAttrProvider | |
2554 | // ---------------------------------------------------------------------------- | |
2555 | ||
2556 | wxGridCellAttrProvider::wxGridCellAttrProvider() | |
2557 | { | |
2558 | m_data = (wxGridCellAttrProviderData *)NULL; | |
2559 | } | |
2560 | ||
2561 | wxGridCellAttrProvider::~wxGridCellAttrProvider() | |
2562 | { | |
2563 | delete m_data; | |
2564 | } | |
2565 | ||
2566 | void wxGridCellAttrProvider::InitData() | |
2567 | { | |
2568 | m_data = new wxGridCellAttrProviderData; | |
2569 | } | |
2570 | ||
19d7140e VZ |
2571 | wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col, |
2572 | wxGridCellAttr::wxAttrKind kind ) const | |
b99be8fb | 2573 | { |
758cbedf VZ |
2574 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
2575 | if ( m_data ) | |
2576 | { | |
19d7140e | 2577 | switch(kind) |
758cbedf | 2578 | { |
19d7140e VZ |
2579 | case (wxGridCellAttr::Any): |
2580 | //Get cached merge attributes. | |
2581 | // Currenlty not used as no cache implemented as not mutiable | |
2582 | // attr = m_data->m_mergeAttr.GetAttr(row, col); | |
2583 | if(!attr) | |
2584 | { | |
2585 | //Basicaly implement old version. | |
2586 | //Also check merge cache, so we don't have to re-merge every time.. | |
999836aa VZ |
2587 | wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col); |
2588 | wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row); | |
2589 | wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col); | |
19d7140e | 2590 | |
bf7945ce | 2591 | if((attrcell != attrrow) && (attrrow !=attrcol) && (attrcell != attrcol)){ |
19d7140e VZ |
2592 | // Two or move are non NULL |
2593 | attr = new wxGridCellAttr; | |
2594 | attr->SetKind(wxGridCellAttr::Merged); | |
2595 | ||
bf7945ce | 2596 | //Order important.. |
19d7140e VZ |
2597 | if(attrcell){ |
2598 | attr->MergeWith(attrcell); | |
2599 | attrcell->DecRef(); | |
2600 | } | |
2601 | if(attrcol){ | |
2602 | attr->MergeWith(attrcol); | |
2603 | attrcol->DecRef(); | |
2604 | } | |
2605 | if(attrrow){ | |
2606 | attr->MergeWith(attrrow); | |
2607 | attrrow->DecRef(); | |
2608 | } | |
2609 | //store merge attr if cache implemented | |
2610 | //attr->IncRef(); | |
2611 | //m_data->m_mergeAttr.SetAttr(attr, row, col); | |
2612 | } | |
2613 | else | |
758cbedf | 2614 | { |
19d7140e VZ |
2615 | // one or none is non null return it or null. |
2616 | if(attrrow) attr = attrrow; | |
2617 | if(attrcol) attr = attrcol; | |
bf7945ce | 2618 | if(attrcell) attr = attrcell; |
19d7140e VZ |
2619 | } |
2620 | } | |
2621 | break; | |
2622 | case (wxGridCellAttr::Cell): | |
2623 | attr = m_data->m_cellAttrs.GetAttr(row, col); | |
2624 | break; | |
2625 | case (wxGridCellAttr::Col): | |
2626 | attr = m_data->m_colAttrs.GetAttr(col); | |
2627 | break; | |
2628 | case (wxGridCellAttr::Row): | |
758cbedf | 2629 | attr = m_data->m_rowAttrs.GetAttr(row); |
19d7140e VZ |
2630 | break; |
2631 | default: | |
2632 | // unused as yet... | |
2633 | // (wxGridCellAttr::Default): | |
2634 | // (wxGridCellAttr::Merged): | |
2635 | break; | |
758cbedf VZ |
2636 | } |
2637 | } | |
758cbedf | 2638 | return attr; |
b99be8fb VZ |
2639 | } |
2640 | ||
2e9a6788 | 2641 | void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, |
b99be8fb VZ |
2642 | int row, int col) |
2643 | { | |
2644 | if ( !m_data ) | |
2645 | InitData(); | |
2646 | ||
758cbedf VZ |
2647 | m_data->m_cellAttrs.SetAttr(attr, row, col); |
2648 | } | |
2649 | ||
2650 | void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row) | |
2651 | { | |
2652 | if ( !m_data ) | |
2653 | InitData(); | |
2654 | ||
2655 | m_data->m_rowAttrs.SetAttr(attr, row); | |
2656 | } | |
2657 | ||
2658 | void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) | |
2659 | { | |
2660 | if ( !m_data ) | |
2661 | InitData(); | |
2662 | ||
2663 | m_data->m_colAttrs.SetAttr(attr, col); | |
b99be8fb VZ |
2664 | } |
2665 | ||
4d60017a SN |
2666 | void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) |
2667 | { | |
2668 | if ( m_data ) | |
2669 | { | |
2670 | m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); | |
2671 | ||
d1c0b4f9 | 2672 | m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); |
4d60017a SN |
2673 | } |
2674 | } | |
2675 | ||
2676 | void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) | |
2677 | { | |
2678 | if ( m_data ) | |
2679 | { | |
2680 | m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); | |
2681 | ||
d1c0b4f9 | 2682 | m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); |
4d60017a SN |
2683 | } |
2684 | } | |
2685 | ||
f2d76237 RD |
2686 | // ---------------------------------------------------------------------------- |
2687 | // wxGridTypeRegistry | |
2688 | // ---------------------------------------------------------------------------- | |
2689 | ||
2690 | wxGridTypeRegistry::~wxGridTypeRegistry() | |
2691 | { | |
b94ae1ea VZ |
2692 | size_t count = m_typeinfo.Count(); |
2693 | for ( size_t i = 0; i < count; i++ ) | |
f2d76237 RD |
2694 | delete m_typeinfo[i]; |
2695 | } | |
2696 | ||
2697 | ||
2698 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, | |
2699 | wxGridCellRenderer* renderer, | |
2700 | wxGridCellEditor* editor) | |
2701 | { | |
f2d76237 RD |
2702 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); |
2703 | ||
2704 | // is it already registered? | |
c4608a8a | 2705 | int loc = FindRegisteredDataType(typeName); |
39bcce60 VZ |
2706 | if ( loc != wxNOT_FOUND ) |
2707 | { | |
f2d76237 RD |
2708 | delete m_typeinfo[loc]; |
2709 | m_typeinfo[loc] = info; | |
2710 | } | |
39bcce60 VZ |
2711 | else |
2712 | { | |
f2d76237 RD |
2713 | m_typeinfo.Add(info); |
2714 | } | |
2715 | } | |
2716 | ||
c4608a8a VZ |
2717 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) |
2718 | { | |
2719 | size_t count = m_typeinfo.GetCount(); | |
2720 | for ( size_t i = 0; i < count; i++ ) | |
2721 | { | |
2722 | if ( typeName == m_typeinfo[i]->m_typeName ) | |
2723 | { | |
2724 | return i; | |
2725 | } | |
2726 | } | |
2727 | ||
2728 | return wxNOT_FOUND; | |
2729 | } | |
2730 | ||
f2d76237 RD |
2731 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) |
2732 | { | |
c4608a8a VZ |
2733 | int index = FindRegisteredDataType(typeName); |
2734 | if ( index == wxNOT_FOUND ) | |
2735 | { | |
2736 | // check whether this is one of the standard ones, in which case | |
2737 | // register it "on the fly" | |
3a8c693a | 2738 | #if wxUSE_TEXTCTRL |
c4608a8a VZ |
2739 | if ( typeName == wxGRID_VALUE_STRING ) |
2740 | { | |
2741 | RegisterDataType(wxGRID_VALUE_STRING, | |
2742 | new wxGridCellStringRenderer, | |
2743 | new wxGridCellTextEditor); | |
3a8c693a VZ |
2744 | } else |
2745 | #endif // wxUSE_TEXTCTRL | |
2746 | #if wxUSE_CHECKBOX | |
2747 | if ( typeName == wxGRID_VALUE_BOOL ) | |
c4608a8a VZ |
2748 | { |
2749 | RegisterDataType(wxGRID_VALUE_BOOL, | |
2750 | new wxGridCellBoolRenderer, | |
2751 | new wxGridCellBoolEditor); | |
3a8c693a VZ |
2752 | } else |
2753 | #endif // wxUSE_CHECKBOX | |
2754 | #if wxUSE_TEXTCTRL | |
2755 | if ( typeName == wxGRID_VALUE_NUMBER ) | |
c4608a8a VZ |
2756 | { |
2757 | RegisterDataType(wxGRID_VALUE_NUMBER, | |
2758 | new wxGridCellNumberRenderer, | |
2759 | new wxGridCellNumberEditor); | |
2760 | } | |
2761 | else if ( typeName == wxGRID_VALUE_FLOAT ) | |
2762 | { | |
2763 | RegisterDataType(wxGRID_VALUE_FLOAT, | |
2764 | new wxGridCellFloatRenderer, | |
2765 | new wxGridCellFloatEditor); | |
3a8c693a VZ |
2766 | } else |
2767 | #endif // wxUSE_TEXTCTRL | |
2768 | #if wxUSE_COMBOBOX | |
2769 | if ( typeName == wxGRID_VALUE_CHOICE ) | |
c4608a8a VZ |
2770 | { |
2771 | RegisterDataType(wxGRID_VALUE_CHOICE, | |
2772 | new wxGridCellStringRenderer, | |
2773 | new wxGridCellChoiceEditor); | |
3a8c693a VZ |
2774 | } else |
2775 | #endif // wxUSE_COMBOBOX | |
c4608a8a VZ |
2776 | { |
2777 | return wxNOT_FOUND; | |
2778 | } | |
f2d76237 | 2779 | |
c4608a8a VZ |
2780 | // we get here only if just added the entry for this type, so return |
2781 | // the last index | |
2782 | index = m_typeinfo.GetCount() - 1; | |
2783 | } | |
2784 | ||
2785 | return index; | |
2786 | } | |
2787 | ||
2788 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) | |
2789 | { | |
2790 | int index = FindDataType(typeName); | |
2791 | if ( index == wxNOT_FOUND ) | |
2792 | { | |
2793 | // the first part of the typename is the "real" type, anything after ':' | |
2794 | // are the parameters for the renderer | |
2795 | index = FindDataType(typeName.BeforeFirst(_T(':'))); | |
2796 | if ( index == wxNOT_FOUND ) | |
2797 | { | |
2798 | return wxNOT_FOUND; | |
f2d76237 | 2799 | } |
c4608a8a VZ |
2800 | |
2801 | wxGridCellRenderer *renderer = GetRenderer(index); | |
2802 | wxGridCellRenderer *rendererOld = renderer; | |
2803 | renderer = renderer->Clone(); | |
2804 | rendererOld->DecRef(); | |
2805 | ||
2806 | wxGridCellEditor *editor = GetEditor(index); | |
2807 | wxGridCellEditor *editorOld = editor; | |
2808 | editor = editor->Clone(); | |
2809 | editorOld->DecRef(); | |
2810 | ||
2811 | // do it even if there are no parameters to reset them to defaults | |
2812 | wxString params = typeName.AfterFirst(_T(':')); | |
2813 | renderer->SetParameters(params); | |
2814 | editor->SetParameters(params); | |
2815 | ||
2816 | // register the new typename | |
c4608a8a VZ |
2817 | RegisterDataType(typeName, renderer, editor); |
2818 | ||
2819 | // we just registered it, it's the last one | |
2820 | index = m_typeinfo.GetCount() - 1; | |
f2d76237 RD |
2821 | } |
2822 | ||
c4608a8a | 2823 | return index; |
f2d76237 RD |
2824 | } |
2825 | ||
2826 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) | |
2827 | { | |
2828 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; | |
faec5a43 SN |
2829 | if (renderer) |
2830 | renderer->IncRef(); | |
f2d76237 RD |
2831 | return renderer; |
2832 | } | |
2833 | ||
0b190b0f | 2834 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) |
f2d76237 RD |
2835 | { |
2836 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; | |
faec5a43 SN |
2837 | if (editor) |
2838 | editor->IncRef(); | |
f2d76237 RD |
2839 | return editor; |
2840 | } | |
2841 | ||
758cbedf VZ |
2842 | // ---------------------------------------------------------------------------- |
2843 | // wxGridTableBase | |
2844 | // ---------------------------------------------------------------------------- | |
2845 | ||
f85afd4e MB |
2846 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) |
2847 | ||
2848 | ||
2849 | wxGridTableBase::wxGridTableBase() | |
f85afd4e MB |
2850 | { |
2851 | m_view = (wxGrid *) NULL; | |
b99be8fb | 2852 | m_attrProvider = (wxGridCellAttrProvider *) NULL; |
f85afd4e MB |
2853 | } |
2854 | ||
2855 | wxGridTableBase::~wxGridTableBase() | |
2856 | { | |
b99be8fb VZ |
2857 | delete m_attrProvider; |
2858 | } | |
2859 | ||
2860 | void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider) | |
2861 | { | |
2862 | delete m_attrProvider; | |
2863 | m_attrProvider = attrProvider; | |
f85afd4e MB |
2864 | } |
2865 | ||
f2d76237 RD |
2866 | bool wxGridTableBase::CanHaveAttributes() |
2867 | { | |
2868 | if ( ! GetAttrProvider() ) | |
2869 | { | |
2870 | // use the default attr provider by default | |
2871 | SetAttrProvider(new wxGridCellAttrProvider); | |
2872 | } | |
2873 | return TRUE; | |
2874 | } | |
2875 | ||
19d7140e | 2876 | wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) |
b99be8fb VZ |
2877 | { |
2878 | if ( m_attrProvider ) | |
19d7140e | 2879 | return m_attrProvider->GetAttr(row, col, kind); |
b99be8fb VZ |
2880 | else |
2881 | return (wxGridCellAttr *)NULL; | |
2882 | } | |
2883 | ||
758cbedf | 2884 | void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) |
b99be8fb VZ |
2885 | { |
2886 | if ( m_attrProvider ) | |
2887 | { | |
19d7140e | 2888 | attr->SetKind(wxGridCellAttr::Cell); |
b99be8fb VZ |
2889 | m_attrProvider->SetAttr(attr, row, col); |
2890 | } | |
2891 | else | |
2892 | { | |
2893 | // as we take ownership of the pointer and don't store it, we must | |
2894 | // free it now | |
39bcce60 | 2895 | wxSafeDecRef(attr); |
b99be8fb VZ |
2896 | } |
2897 | } | |
2898 | ||
758cbedf VZ |
2899 | void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) |
2900 | { | |
2901 | if ( m_attrProvider ) | |
2902 | { | |
19d7140e | 2903 | attr->SetKind(wxGridCellAttr::Row); |
758cbedf VZ |
2904 | m_attrProvider->SetRowAttr(attr, row); |
2905 | } | |
2906 | else | |
2907 | { | |
2908 | // as we take ownership of the pointer and don't store it, we must | |
2909 | // free it now | |
39bcce60 | 2910 | wxSafeDecRef(attr); |
758cbedf VZ |
2911 | } |
2912 | } | |
2913 | ||
2914 | void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) | |
2915 | { | |
2916 | if ( m_attrProvider ) | |
2917 | { | |
19d7140e | 2918 | attr->SetKind(wxGridCellAttr::Col); |
758cbedf VZ |
2919 | m_attrProvider->SetColAttr(attr, col); |
2920 | } | |
2921 | else | |
2922 | { | |
2923 | // as we take ownership of the pointer and don't store it, we must | |
2924 | // free it now | |
39bcce60 | 2925 | wxSafeDecRef(attr); |
758cbedf VZ |
2926 | } |
2927 | } | |
2928 | ||
aa5e1f75 SN |
2929 | bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos), |
2930 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 2931 | { |
f6bcfd97 | 2932 | wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); |
8f177c8e | 2933 | |
f85afd4e MB |
2934 | return FALSE; |
2935 | } | |
2936 | ||
aa5e1f75 | 2937 | bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) ) |
f85afd4e | 2938 | { |
f6bcfd97 | 2939 | wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); |
8f177c8e | 2940 | |
f85afd4e MB |
2941 | return FALSE; |
2942 | } | |
2943 | ||
aa5e1f75 SN |
2944 | bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos), |
2945 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 2946 | { |
f6bcfd97 | 2947 | wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); |
8f177c8e | 2948 | |
f85afd4e MB |
2949 | return FALSE; |
2950 | } | |
2951 | ||
aa5e1f75 SN |
2952 | bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos), |
2953 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 2954 | { |
f6bcfd97 | 2955 | wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); |
8f177c8e | 2956 | |
f85afd4e MB |
2957 | return FALSE; |
2958 | } | |
2959 | ||
aa5e1f75 | 2960 | bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) ) |
f85afd4e | 2961 | { |
f6bcfd97 | 2962 | wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); |
8f177c8e | 2963 | |
f85afd4e MB |
2964 | return FALSE; |
2965 | } | |
2966 | ||
aa5e1f75 SN |
2967 | bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos), |
2968 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 2969 | { |
f6bcfd97 | 2970 | wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); |
8f177c8e | 2971 | |
f85afd4e MB |
2972 | return FALSE; |
2973 | } | |
2974 | ||
2975 | ||
2976 | wxString wxGridTableBase::GetRowLabelValue( int row ) | |
2977 | { | |
2978 | wxString s; | |
f2d76237 RD |
2979 | s << row + 1; // RD: Starting the rows at zero confuses users, no matter |
2980 | // how much it makes sense to us geeks. | |
f85afd4e MB |
2981 | return s; |
2982 | } | |
2983 | ||
2984 | wxString wxGridTableBase::GetColLabelValue( int col ) | |
2985 | { | |
2986 | // default col labels are: | |
2987 | // cols 0 to 25 : A-Z | |
2988 | // cols 26 to 675 : AA-ZZ | |
2989 | // etc. | |
2990 | ||
2991 | wxString s; | |
2992 | unsigned int i, n; | |
2993 | for ( n = 1; ; n++ ) | |
2994 | { | |
f0102d2a | 2995 | s += (_T('A') + (wxChar)( col%26 )); |
f85afd4e MB |
2996 | col = col/26 - 1; |
2997 | if ( col < 0 ) break; | |
2998 | } | |
2999 | ||
3000 | // reverse the string... | |
3001 | wxString s2; | |
3002 | for ( i = 0; i < n; i++ ) | |
3003 | { | |
3004 | s2 += s[n-i-1]; | |
3005 | } | |
3006 | ||
3007 | return s2; | |
3008 | } | |
3009 | ||
3010 | ||
f2d76237 RD |
3011 | wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) ) |
3012 | { | |
816be743 | 3013 | return wxGRID_VALUE_STRING; |
f2d76237 RD |
3014 | } |
3015 | ||
3016 | bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col), | |
3017 | const wxString& typeName ) | |
3018 | { | |
816be743 | 3019 | return typeName == wxGRID_VALUE_STRING; |
f2d76237 RD |
3020 | } |
3021 | ||
3022 | bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName ) | |
3023 | { | |
3024 | return CanGetValueAs(row, col, typeName); | |
3025 | } | |
3026 | ||
3027 | long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) ) | |
3028 | { | |
3029 | return 0; | |
3030 | } | |
3031 | ||
3032 | double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) ) | |
3033 | { | |
3034 | return 0.0; | |
3035 | } | |
3036 | ||
3037 | bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) ) | |
3038 | { | |
3039 | return FALSE; | |
3040 | } | |
3041 | ||
3042 | void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col), | |
3043 | long WXUNUSED(value) ) | |
3044 | { | |
3045 | } | |
3046 | ||
3047 | void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col), | |
3048 | double WXUNUSED(value) ) | |
3049 | { | |
3050 | } | |
3051 | ||
3052 | void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col), | |
3053 | bool WXUNUSED(value) ) | |
3054 | { | |
3055 | } | |
3056 | ||
3057 | ||
3058 | void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3059 | const wxString& WXUNUSED(typeName) ) | |
3060 | { | |
3061 | return NULL; | |
3062 | } | |
3063 | ||
3064 | void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3065 | const wxString& WXUNUSED(typeName), | |
3066 | void* WXUNUSED(value) ) | |
3067 | { | |
3068 | } | |
3069 | ||
f85afd4e MB |
3070 | ////////////////////////////////////////////////////////////////////// |
3071 | // | |
3072 | // Message class for the grid table to send requests and notifications | |
3073 | // to the grid view | |
3074 | // | |
3075 | ||
3076 | wxGridTableMessage::wxGridTableMessage() | |
3077 | { | |
3078 | m_table = (wxGridTableBase *) NULL; | |
3079 | m_id = -1; | |
3080 | m_comInt1 = -1; | |
3081 | m_comInt2 = -1; | |
3082 | } | |
3083 | ||
3084 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, | |
3085 | int commandInt1, int commandInt2 ) | |
3086 | { | |
3087 | m_table = table; | |
3088 | m_id = id; | |
3089 | m_comInt1 = commandInt1; | |
3090 | m_comInt2 = commandInt2; | |
3091 | } | |
3092 | ||
3093 | ||
3094 | ||
3095 | ////////////////////////////////////////////////////////////////////// | |
3096 | // | |
3097 | // A basic grid table for string data. An object of this class will | |
3098 | // created by wxGrid if you don't specify an alternative table class. | |
3099 | // | |
3100 | ||
223d09f6 | 3101 | WX_DEFINE_OBJARRAY(wxGridStringArray) |
f85afd4e MB |
3102 | |
3103 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) | |
3104 | ||
3105 | wxGridStringTable::wxGridStringTable() | |
3106 | : wxGridTableBase() | |
3107 | { | |
3108 | } | |
3109 | ||
3110 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) | |
3111 | : wxGridTableBase() | |
3112 | { | |
f85afd4e MB |
3113 | m_data.Alloc( numRows ); |
3114 | ||
3115 | wxArrayString sa; | |
3116 | sa.Alloc( numCols ); | |
27f35b66 | 3117 | sa.Add( wxEmptyString, numCols ); |
8f177c8e | 3118 | |
27f35b66 | 3119 | m_data.Add( sa, numRows ); |
f85afd4e MB |
3120 | } |
3121 | ||
3122 | wxGridStringTable::~wxGridStringTable() | |
3123 | { | |
3124 | } | |
3125 | ||
e32352cf | 3126 | int wxGridStringTable::GetNumberRows() |
f85afd4e MB |
3127 | { |
3128 | return m_data.GetCount(); | |
3129 | } | |
3130 | ||
e32352cf | 3131 | int wxGridStringTable::GetNumberCols() |
f85afd4e MB |
3132 | { |
3133 | if ( m_data.GetCount() > 0 ) | |
3134 | return m_data[0].GetCount(); | |
3135 | else | |
3136 | return 0; | |
3137 | } | |
3138 | ||
3139 | wxString wxGridStringTable::GetValue( int row, int col ) | |
3140 | { | |
3e13956a RD |
3141 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
3142 | wxEmptyString, | |
3143 | _T("invalid row or column index in wxGridStringTable") ); | |
af547d51 | 3144 | |
f85afd4e MB |
3145 | return m_data[row][col]; |
3146 | } | |
3147 | ||
f2d76237 | 3148 | void wxGridStringTable::SetValue( int row, int col, const wxString& value ) |
f85afd4e | 3149 | { |
3e13956a RD |
3150 | wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()), |
3151 | _T("invalid row or column index in wxGridStringTable") ); | |
af547d51 | 3152 | |
f2d76237 | 3153 | m_data[row][col] = value; |
f85afd4e MB |
3154 | } |
3155 | ||
3156 | bool wxGridStringTable::IsEmptyCell( int row, int col ) | |
3157 | { | |
3e13956a RD |
3158 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
3159 | true, | |
af547d51 VZ |
3160 | _T("invalid row or column index in wxGridStringTable") ); |
3161 | ||
f85afd4e MB |
3162 | return (m_data[row][col] == wxEmptyString); |
3163 | } | |
3164 | ||
f85afd4e MB |
3165 | void wxGridStringTable::Clear() |
3166 | { | |
3167 | int row, col; | |
3168 | int numRows, numCols; | |
8f177c8e | 3169 | |
f85afd4e MB |
3170 | numRows = m_data.GetCount(); |
3171 | if ( numRows > 0 ) | |
3172 | { | |
3173 | numCols = m_data[0].GetCount(); | |
3174 | ||
3175 | for ( row = 0; row < numRows; row++ ) | |
3176 | { | |
3177 | for ( col = 0; col < numCols; col++ ) | |
3178 | { | |
3179 | m_data[row][col] = wxEmptyString; | |
3180 | } | |
3181 | } | |
3182 | } | |
3183 | } | |
3184 | ||
3185 | ||
3186 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) | |
3187 | { | |
f85afd4e | 3188 | size_t curNumRows = m_data.GetCount(); |
f6bcfd97 BP |
3189 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3190 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3191 | |
f85afd4e MB |
3192 | if ( pos >= curNumRows ) |
3193 | { | |
3194 | return AppendRows( numRows ); | |
3195 | } | |
8f177c8e | 3196 | |
f85afd4e MB |
3197 | wxArrayString sa; |
3198 | sa.Alloc( curNumCols ); | |
27f35b66 SN |
3199 | sa.Add( wxEmptyString, curNumCols ); |
3200 | m_data.Insert( sa, pos, numRows ); | |
f85afd4e MB |
3201 | if ( GetView() ) |
3202 | { | |
3203 | wxGridTableMessage msg( this, | |
3204 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, | |
3205 | pos, | |
3206 | numRows ); | |
8f177c8e | 3207 | |
f85afd4e MB |
3208 | GetView()->ProcessTableMessage( msg ); |
3209 | } | |
3210 | ||
3211 | return TRUE; | |
3212 | } | |
3213 | ||
3214 | bool wxGridStringTable::AppendRows( size_t numRows ) | |
3215 | { | |
f85afd4e | 3216 | size_t curNumRows = m_data.GetCount(); |
f6bcfd97 BP |
3217 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3218 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3219 | |
f85afd4e MB |
3220 | wxArrayString sa; |
3221 | if ( curNumCols > 0 ) | |
3222 | { | |
3223 | sa.Alloc( curNumCols ); | |
27f35b66 | 3224 | sa.Add( wxEmptyString, curNumCols ); |
f85afd4e | 3225 | } |
8f177c8e | 3226 | |
27f35b66 | 3227 | m_data.Add( sa, numRows ); |
f85afd4e MB |
3228 | |
3229 | if ( GetView() ) | |
3230 | { | |
3231 | wxGridTableMessage msg( this, | |
3232 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, | |
3233 | numRows ); | |
8f177c8e | 3234 | |
f85afd4e MB |
3235 | GetView()->ProcessTableMessage( msg ); |
3236 | } | |
3237 | ||
8f177c8e | 3238 | return TRUE; |
f85afd4e MB |
3239 | } |
3240 | ||
3241 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) | |
3242 | { | |
f85afd4e | 3243 | size_t curNumRows = m_data.GetCount(); |
8f177c8e | 3244 | |
f85afd4e MB |
3245 | if ( pos >= curNumRows ) |
3246 | { | |
e91d2033 VZ |
3247 | wxFAIL_MSG( wxString::Format |
3248 | ( | |
3249 | wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), | |
3250 | (unsigned long)pos, | |
3251 | (unsigned long)numRows, | |
3252 | (unsigned long)curNumRows | |
3253 | ) ); | |
3254 | ||
f85afd4e MB |
3255 | return FALSE; |
3256 | } | |
3257 | ||
3258 | if ( numRows > curNumRows - pos ) | |
3259 | { | |
3260 | numRows = curNumRows - pos; | |
3261 | } | |
8f177c8e | 3262 | |
f85afd4e MB |
3263 | if ( numRows >= curNumRows ) |
3264 | { | |
d57ad377 | 3265 | m_data.Clear(); |
f85afd4e MB |
3266 | } |
3267 | else | |
3268 | { | |
27f35b66 | 3269 | m_data.RemoveAt( pos, numRows ); |
f85afd4e | 3270 | } |
f85afd4e MB |
3271 | if ( GetView() ) |
3272 | { | |
3273 | wxGridTableMessage msg( this, | |
3274 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, | |
3275 | pos, | |
3276 | numRows ); | |
8f177c8e | 3277 | |
f85afd4e MB |
3278 | GetView()->ProcessTableMessage( msg ); |
3279 | } | |
3280 | ||
8f177c8e | 3281 | return TRUE; |
f85afd4e MB |
3282 | } |
3283 | ||
3284 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) | |
3285 | { | |
3286 | size_t row, col; | |
3287 | ||
3288 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 BP |
3289 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3290 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3291 | |
f85afd4e MB |
3292 | if ( pos >= curNumCols ) |
3293 | { | |
3294 | return AppendCols( numCols ); | |
3295 | } | |
3296 | ||
3297 | for ( row = 0; row < curNumRows; row++ ) | |
3298 | { | |
3299 | for ( col = pos; col < pos + numCols; col++ ) | |
3300 | { | |
3301 | m_data[row].Insert( wxEmptyString, col ); | |
3302 | } | |
3303 | } | |
f85afd4e MB |
3304 | if ( GetView() ) |
3305 | { | |
3306 | wxGridTableMessage msg( this, | |
3307 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, | |
3308 | pos, | |
3309 | numCols ); | |
8f177c8e | 3310 | |
f85afd4e MB |
3311 | GetView()->ProcessTableMessage( msg ); |
3312 | } | |
3313 | ||
3314 | return TRUE; | |
3315 | } | |
3316 | ||
3317 | bool wxGridStringTable::AppendCols( size_t numCols ) | |
3318 | { | |
27f35b66 | 3319 | size_t row; |
f85afd4e MB |
3320 | |
3321 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 | 3322 | #if 0 |
f85afd4e MB |
3323 | if ( !curNumRows ) |
3324 | { | |
3325 | // TODO: something better than this ? | |
3326 | // | |
f6bcfd97 | 3327 | wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") ); |
f85afd4e MB |
3328 | return FALSE; |
3329 | } | |
f6bcfd97 | 3330 | #endif |
8f177c8e | 3331 | |
f85afd4e MB |
3332 | for ( row = 0; row < curNumRows; row++ ) |
3333 | { | |
27f35b66 | 3334 | m_data[row].Add( wxEmptyString, numCols ); |
f85afd4e MB |
3335 | } |
3336 | ||
3337 | if ( GetView() ) | |
3338 | { | |
3339 | wxGridTableMessage msg( this, | |
3340 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, | |
3341 | numCols ); | |
8f177c8e | 3342 | |
f85afd4e MB |
3343 | GetView()->ProcessTableMessage( msg ); |
3344 | } | |
3345 | ||
3346 | return TRUE; | |
3347 | } | |
3348 | ||
3349 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) | |
3350 | { | |
27f35b66 | 3351 | size_t row; |
f85afd4e MB |
3352 | |
3353 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 BP |
3354 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3355 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3356 | |
f85afd4e MB |
3357 | if ( pos >= curNumCols ) |
3358 | { | |
e91d2033 VZ |
3359 | wxFAIL_MSG( wxString::Format |
3360 | ( | |
3361 | wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), | |
3362 | (unsigned long)pos, | |
3363 | (unsigned long)numCols, | |
3364 | (unsigned long)curNumCols | |
3365 | ) ); | |
8f177c8e | 3366 | return FALSE; |
f85afd4e MB |
3367 | } |
3368 | ||
3369 | if ( numCols > curNumCols - pos ) | |
3370 | { | |
8f177c8e | 3371 | numCols = curNumCols - pos; |
f85afd4e MB |
3372 | } |
3373 | ||
3374 | for ( row = 0; row < curNumRows; row++ ) | |
3375 | { | |
3376 | if ( numCols >= curNumCols ) | |
3377 | { | |
dcdce64e | 3378 | m_data[row].Clear(); |
f85afd4e MB |
3379 | } |
3380 | else | |
3381 | { | |
27f35b66 | 3382 | m_data[row].RemoveAt( pos, numCols ); |
f85afd4e MB |
3383 | } |
3384 | } | |
f85afd4e MB |
3385 | if ( GetView() ) |
3386 | { | |
3387 | wxGridTableMessage msg( this, | |
3388 | wxGRIDTABLE_NOTIFY_COLS_DELETED, | |
3389 | pos, | |
3390 | numCols ); | |
8f177c8e | 3391 | |
f85afd4e MB |
3392 | GetView()->ProcessTableMessage( msg ); |
3393 | } | |
3394 | ||
8f177c8e | 3395 | return TRUE; |
f85afd4e MB |
3396 | } |
3397 | ||
3398 | wxString wxGridStringTable::GetRowLabelValue( int row ) | |
3399 | { | |
3400 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3401 | { | |
3402 | // using default label | |
3403 | // | |
3404 | return wxGridTableBase::GetRowLabelValue( row ); | |
3405 | } | |
3406 | else | |
3407 | { | |
3408 | return m_rowLabels[ row ]; | |
3409 | } | |
3410 | } | |
3411 | ||
3412 | wxString wxGridStringTable::GetColLabelValue( int col ) | |
3413 | { | |
3414 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3415 | { | |
3416 | // using default label | |
3417 | // | |
3418 | return wxGridTableBase::GetColLabelValue( col ); | |
3419 | } | |
3420 | else | |
3421 | { | |
3422 | return m_colLabels[ col ]; | |
3423 | } | |
3424 | } | |
3425 | ||
3426 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) | |
3427 | { | |
3428 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3429 | { | |
3430 | int n = m_rowLabels.GetCount(); | |
3431 | int i; | |
3432 | for ( i = n; i <= row; i++ ) | |
3433 | { | |
3434 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); | |
3435 | } | |
3436 | } | |
3437 | ||
3438 | m_rowLabels[row] = value; | |
3439 | } | |
3440 | ||
3441 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) | |
3442 | { | |
3443 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3444 | { | |
3445 | int n = m_colLabels.GetCount(); | |
3446 | int i; | |
3447 | for ( i = n; i <= col; i++ ) | |
3448 | { | |
3449 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); | |
3450 | } | |
3451 | } | |
3452 | ||
3453 | m_colLabels[col] = value; | |
3454 | } | |
3455 | ||
3456 | ||
3457 | ||
f85afd4e | 3458 | ////////////////////////////////////////////////////////////////////// |
2d66e025 MB |
3459 | ////////////////////////////////////////////////////////////////////// |
3460 | ||
3461 | IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow ) | |
3462 | ||
3463 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow ) | |
3464 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) | |
b51c3f27 | 3465 | EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel) |
2d66e025 MB |
3466 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) |
3467 | EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown ) | |
f6bcfd97 | 3468 | EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp ) |
2d66e025 MB |
3469 | END_EVENT_TABLE() |
3470 | ||
60ff3b99 VZ |
3471 | wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent, |
3472 | wxWindowID id, | |
2d66e025 | 3473 | const wxPoint &pos, const wxSize &size ) |
0bbeb42d | 3474 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE ) |
2d66e025 MB |
3475 | { |
3476 | m_owner = parent; | |
3477 | } | |
3478 | ||
aa5e1f75 | 3479 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
3480 | { |
3481 | wxPaintDC dc(this); | |
3482 | ||
3483 | // NO - don't do this because it will set both the x and y origin | |
3484 | // coords to match the parent scrolled window and we just want to | |
3485 | // set the y coord - MB | |
3486 | // | |
3487 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 3488 | |
790ad94f | 3489 | int x, y; |
2d66e025 MB |
3490 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
3491 | dc.SetDeviceOrigin( 0, -y ); | |
60ff3b99 | 3492 | |
d10f4bf9 VZ |
3493 | wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); |
3494 | m_owner->DrawRowLabels( dc , rows ); | |
2d66e025 MB |
3495 | } |
3496 | ||
3497 | ||
3498 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3499 | { | |
3500 | m_owner->ProcessRowLabelMouseEvent( event ); | |
3501 | } | |
3502 | ||
3503 | ||
b51c3f27 RD |
3504 | void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3505 | { | |
3506 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3507 | } | |
3508 | ||
3509 | ||
2d66e025 MB |
3510 | // This seems to be required for wxMotif otherwise the mouse |
3511 | // cursor must be in the cell edit control to get key events | |
3512 | // | |
3513 | void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3514 | { | |
ffdd3c98 | 3515 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3516 | } |
3517 | ||
f6bcfd97 BP |
3518 | void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3519 | { | |
ffdd3c98 | 3520 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3521 | } |
3522 | ||
2d66e025 MB |
3523 | |
3524 | ||
3525 | ////////////////////////////////////////////////////////////////////// | |
3526 | ||
3527 | IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow ) | |
3528 | ||
3529 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow ) | |
3530 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) | |
b51c3f27 | 3531 | EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel) |
2d66e025 MB |
3532 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) |
3533 | EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown ) | |
f6bcfd97 | 3534 | EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp ) |
2d66e025 MB |
3535 | END_EVENT_TABLE() |
3536 | ||
60ff3b99 VZ |
3537 | wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent, |
3538 | wxWindowID id, | |
2d66e025 | 3539 | const wxPoint &pos, const wxSize &size ) |
0bbeb42d | 3540 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE ) |
2d66e025 MB |
3541 | { |
3542 | m_owner = parent; | |
3543 | } | |
3544 | ||
aa5e1f75 | 3545 | void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
3546 | { |
3547 | wxPaintDC dc(this); | |
3548 | ||
3549 | // NO - don't do this because it will set both the x and y origin | |
3550 | // coords to match the parent scrolled window and we just want to | |
3551 | // set the x coord - MB | |
3552 | // | |
3553 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 3554 | |
790ad94f | 3555 | int x, y; |
2d66e025 MB |
3556 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
3557 | dc.SetDeviceOrigin( -x, 0 ); | |
3558 | ||
d10f4bf9 VZ |
3559 | wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() ); |
3560 | m_owner->DrawColLabels( dc , cols ); | |
2d66e025 MB |
3561 | } |
3562 | ||
3563 | ||
3564 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3565 | { | |
3566 | m_owner->ProcessColLabelMouseEvent( event ); | |
3567 | } | |
3568 | ||
b51c3f27 RD |
3569 | void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3570 | { | |
3571 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3572 | } | |
3573 | ||
2d66e025 MB |
3574 | |
3575 | // This seems to be required for wxMotif otherwise the mouse | |
3576 | // cursor must be in the cell edit control to get key events | |
3577 | // | |
3578 | void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3579 | { | |
ffdd3c98 | 3580 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3581 | } |
3582 | ||
f6bcfd97 BP |
3583 | void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3584 | { | |
ffdd3c98 | 3585 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3586 | } |
3587 | ||
2d66e025 MB |
3588 | |
3589 | ||
3590 | ////////////////////////////////////////////////////////////////////// | |
3591 | ||
3592 | IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow ) | |
3593 | ||
3594 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow ) | |
b51c3f27 | 3595 | EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel) |
2d66e025 | 3596 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) |
d2fdd8d2 | 3597 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint) |
2d66e025 | 3598 | EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown ) |
f6bcfd97 | 3599 | EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp ) |
2d66e025 MB |
3600 | END_EVENT_TABLE() |
3601 | ||
60ff3b99 VZ |
3602 | wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent, |
3603 | wxWindowID id, | |
2d66e025 | 3604 | const wxPoint &pos, const wxSize &size ) |
0bbeb42d | 3605 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE ) |
2d66e025 MB |
3606 | { |
3607 | m_owner = parent; | |
3608 | } | |
3609 | ||
d2fdd8d2 RR |
3610 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
3611 | { | |
3612 | wxPaintDC dc(this); | |
b99be8fb | 3613 | |
d2fdd8d2 RR |
3614 | int client_height = 0; |
3615 | int client_width = 0; | |
3616 | GetClientSize( &client_width, &client_height ); | |
b99be8fb | 3617 | |
73145b0e | 3618 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) ); |
d2fdd8d2 RR |
3619 | dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 ); |
3620 | dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 ); | |
d2fdd8d2 RR |
3621 | dc.DrawLine( 0, 0, client_width, 0 ); |
3622 | dc.DrawLine( 0, 0, 0, client_height ); | |
73145b0e JS |
3623 | |
3624 | dc.SetPen( *wxWHITE_PEN ); | |
3625 | dc.DrawLine( 1, 1, client_width-1, 1 ); | |
3626 | dc.DrawLine( 1, 1, 1, client_height-1 ); | |
d2fdd8d2 RR |
3627 | } |
3628 | ||
2d66e025 MB |
3629 | |
3630 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3631 | { | |
3632 | m_owner->ProcessCornerLabelMouseEvent( event ); | |
3633 | } | |
3634 | ||
3635 | ||
b51c3f27 RD |
3636 | void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3637 | { | |
3638 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3639 | } | |
3640 | ||
2d66e025 MB |
3641 | // This seems to be required for wxMotif otherwise the mouse |
3642 | // cursor must be in the cell edit control to get key events | |
3643 | // | |
3644 | void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3645 | { | |
ffdd3c98 | 3646 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3647 | } |
3648 | ||
f6bcfd97 BP |
3649 | void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3650 | { | |
ffdd3c98 | 3651 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3652 | } |
3653 | ||
2d66e025 MB |
3654 | |
3655 | ||
f85afd4e MB |
3656 | ////////////////////////////////////////////////////////////////////// |
3657 | ||
59ddac01 | 3658 | IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow ) |
2d66e025 | 3659 | |
59ddac01 | 3660 | BEGIN_EVENT_TABLE( wxGridWindow, wxWindow ) |
2d66e025 | 3661 | EVT_PAINT( wxGridWindow::OnPaint ) |
b51c3f27 | 3662 | EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel) |
2d66e025 MB |
3663 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) |
3664 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) | |
f6bcfd97 | 3665 | EVT_KEY_UP( wxGridWindow::OnKeyUp ) |
80acaf25 JS |
3666 | EVT_SET_FOCUS( wxGridWindow::OnFocus ) |
3667 | EVT_KILL_FOCUS( wxGridWindow::OnFocus ) | |
2796cce3 | 3668 | EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground ) |
2d66e025 MB |
3669 | END_EVENT_TABLE() |
3670 | ||
60ff3b99 VZ |
3671 | wxGridWindow::wxGridWindow( wxGrid *parent, |
3672 | wxGridRowLabelWindow *rowLblWin, | |
2d66e025 | 3673 | wxGridColLabelWindow *colLblWin, |
04418332 VZ |
3674 | wxWindowID id, |
3675 | const wxPoint &pos, | |
3676 | const wxSize &size ) | |
0bbeb42d | 3677 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN, |
04418332 | 3678 | wxT("grid window") ) |
73145b0e | 3679 | |
2d66e025 MB |
3680 | { |
3681 | m_owner = parent; | |
3682 | m_rowLabelWin = rowLblWin; | |
3683 | m_colLabelWin = colLblWin; | |
edb89f7e | 3684 | SetBackgroundColour(_T("WHITE")); |
2d66e025 MB |
3685 | } |
3686 | ||
3687 | ||
3688 | wxGridWindow::~wxGridWindow() | |
3689 | { | |
3690 | } | |
3691 | ||
3692 | ||
3693 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
3694 | { | |
3695 | wxPaintDC dc( this ); | |
3696 | m_owner->PrepareDC( dc ); | |
796df70a | 3697 | wxRegion reg = GetUpdateRegion(); |
d10f4bf9 VZ |
3698 | wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg ); |
3699 | m_owner->DrawGridCellArea( dc , DirtyCells); | |
9496deb5 | 3700 | #if WXGRID_DRAW_LINES |
796df70a SN |
3701 | m_owner->DrawAllGridLines( dc, reg ); |
3702 | #endif | |
a5777624 | 3703 | m_owner->DrawGridSpace( dc ); |
d10f4bf9 | 3704 | m_owner->DrawHighlight( dc , DirtyCells ); |
2d66e025 MB |
3705 | } |
3706 | ||
3707 | ||
3708 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
3709 | { | |
59ddac01 | 3710 | wxWindow::ScrollWindow( dx, dy, rect ); |
2d66e025 MB |
3711 | m_rowLabelWin->ScrollWindow( 0, dy, rect ); |
3712 | m_colLabelWin->ScrollWindow( dx, 0, rect ); | |
3713 | } | |
3714 | ||
3715 | ||
3716 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) | |
3717 | { | |
3718 | m_owner->ProcessGridCellMouseEvent( event ); | |
3719 | } | |
3720 | ||
b51c3f27 RD |
3721 | void wxGridWindow::OnMouseWheel( wxMouseEvent& event ) |
3722 | { | |
3723 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3724 | } | |
2d66e025 | 3725 | |
f6bcfd97 | 3726 | // This seems to be required for wxMotif/wxGTK otherwise the mouse |
2d66e025 MB |
3727 | // cursor must be in the cell edit control to get key events |
3728 | // | |
3729 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) | |
3730 | { | |
ffdd3c98 | 3731 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 | 3732 | } |
f85afd4e | 3733 | |
f6bcfd97 BP |
3734 | void wxGridWindow::OnKeyUp( wxKeyEvent& event ) |
3735 | { | |
ffdd3c98 | 3736 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 | 3737 | } |
7c8a8ad5 | 3738 | |
aa5e1f75 | 3739 | void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) |
8dd4f536 | 3740 | { |
8dd4f536 | 3741 | } |
025562fe | 3742 | |
80acaf25 JS |
3743 | void wxGridWindow::OnFocus(wxFocusEvent& event) |
3744 | { | |
3745 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3746 | event.Skip(); | |
3747 | } | |
2d66e025 MB |
3748 | |
3749 | ////////////////////////////////////////////////////////////////////// | |
3750 | ||
33188aa4 SN |
3751 | // Internal Helper function for computing row or column from some |
3752 | // (unscrolled) coordinate value, using either | |
70e8d961 | 3753 | // m_defaultRowHeight/m_defaultColWidth or binary search on array |
33188aa4 SN |
3754 | // of m_rowBottoms/m_ColRights to speed up the search! |
3755 | ||
3756 | // Internal helper macros for simpler use of that function | |
3757 | ||
3758 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, | |
64e15340 | 3759 | const wxArrayInt& BorderArray, int nMax, |
a967f048 | 3760 | bool clipToMinMax); |
33188aa4 SN |
3761 | |
3762 | #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \ | |
b8d24d4e | 3763 | m_minAcceptableColWidth, \ |
64e15340 | 3764 | m_colRights, m_numCols, TRUE) |
33188aa4 | 3765 | #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \ |
b8d24d4e | 3766 | m_minAcceptableRowHeight, \ |
64e15340 | 3767 | m_rowBottoms, m_numRows, TRUE) |
33188aa4 | 3768 | ///////////////////////////////////////////////////////////////////// |
07296f0b | 3769 | |
b0a877ec | 3770 | #if wxUSE_EXTENDED_RTTI |
73c36334 JS |
3771 | WX_DEFINE_FLAGS( wxGridStyle ) |
3772 | ||
3ff066a4 | 3773 | wxBEGIN_FLAGS( wxGridStyle ) |
73c36334 JS |
3774 | // new style border flags, we put them first to |
3775 | // use them for streaming out | |
3ff066a4 SC |
3776 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
3777 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
3778 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
3779 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
3780 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
3781 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
73c36334 JS |
3782 | |
3783 | // old style border flags | |
3ff066a4 SC |
3784 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
3785 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
3786 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
3787 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
3788 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 3789 | wxFLAGS_MEMBER(wxBORDER) |
73c36334 JS |
3790 | |
3791 | // standard window styles | |
3ff066a4 SC |
3792 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
3793 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
3794 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
3795 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 3796 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
3797 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
3798 | wxFLAGS_MEMBER(wxVSCROLL) | |
3799 | wxFLAGS_MEMBER(wxHSCROLL) | |
73c36334 | 3800 | |
3ff066a4 | 3801 | wxEND_FLAGS( wxGridStyle ) |
73c36334 | 3802 | |
b0a877ec SC |
3803 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h") |
3804 | ||
3ff066a4 SC |
3805 | wxBEGIN_PROPERTIES_TABLE(wxGrid) |
3806 | wxHIDE_PROPERTY( Children ) | |
3807 | wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
3808 | wxEND_PROPERTIES_TABLE() | |
b0a877ec | 3809 | |
3ff066a4 SC |
3810 | wxBEGIN_HANDLERS_TABLE(wxGrid) |
3811 | wxEND_HANDLERS_TABLE() | |
b0a877ec | 3812 | |
3ff066a4 | 3813 | wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
b0a877ec SC |
3814 | |
3815 | /* | |
3816 | Content-type: text/html ]>