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