]>
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) | |
1491 | evtHandler->SetInSetFocus(false); | |
4ee5fc9c VZ |
1492 | } |
1493 | ||
28a77bc4 | 1494 | bool wxGridCellChoiceEditor::EndEdit(int row, int col, |
4ee5fc9c VZ |
1495 | wxGrid* grid) |
1496 | { | |
1497 | wxString value = Combo()->GetValue(); | |
faffacec VZ |
1498 | if ( value == m_startValue ) |
1499 | return false; | |
4ee5fc9c | 1500 | |
faffacec | 1501 | grid->GetTable()->SetValue(row, col, value); |
4ee5fc9c | 1502 | |
faffacec | 1503 | return true; |
4ee5fc9c VZ |
1504 | } |
1505 | ||
1506 | void wxGridCellChoiceEditor::Reset() | |
1507 | { | |
1508 | Combo()->SetValue(m_startValue); | |
1509 | Combo()->SetInsertionPointEnd(); | |
1510 | } | |
1511 | ||
c4608a8a VZ |
1512 | void wxGridCellChoiceEditor::SetParameters(const wxString& params) |
1513 | { | |
1514 | if ( !params ) | |
1515 | { | |
1516 | // what can we do? | |
1517 | return; | |
1518 | } | |
1519 | ||
1520 | m_choices.Empty(); | |
1521 | ||
1522 | wxStringTokenizer tk(params, _T(',')); | |
1523 | while ( tk.HasMoreTokens() ) | |
1524 | { | |
1525 | m_choices.Add(tk.GetNextToken()); | |
1526 | } | |
1527 | } | |
1528 | ||
73145b0e JS |
1529 | // return the value in the text control |
1530 | wxString wxGridCellChoiceEditor::GetValue() const | |
1531 | { | |
1532 | return Combo()->GetValue(); | |
1533 | } | |
04418332 | 1534 | |
3a8c693a VZ |
1535 | #endif // wxUSE_COMBOBOX |
1536 | ||
508011ce VZ |
1537 | // ---------------------------------------------------------------------------- |
1538 | // wxGridCellEditorEvtHandler | |
1539 | // ---------------------------------------------------------------------------- | |
2796cce3 | 1540 | |
140954fd VZ |
1541 | void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent& event) |
1542 | { | |
08dd04d0 JS |
1543 | // Don't disable the cell if we're just starting to edit it |
1544 | if (m_inSetFocus) | |
1545 | return; | |
1546 | ||
140954fd VZ |
1547 | // accept changes |
1548 | m_grid->DisableCellEditControl(); | |
1549 | ||
1550 | event.Skip(); | |
1551 | } | |
1552 | ||
2796cce3 RD |
1553 | void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event) |
1554 | { | |
12a3f227 | 1555 | switch ( event.GetKeyCode() ) |
2796cce3 RD |
1556 | { |
1557 | case WXK_ESCAPE: | |
1558 | m_editor->Reset(); | |
b54ba671 | 1559 | m_grid->DisableCellEditControl(); |
2796cce3 RD |
1560 | break; |
1561 | ||
2c9a89e0 | 1562 | case WXK_TAB: |
b51c3f27 | 1563 | m_grid->GetEventHandler()->ProcessEvent( event ); |
9b4aede2 RD |
1564 | break; |
1565 | ||
2796cce3 | 1566 | case WXK_RETURN: |
faec5a43 SN |
1567 | case WXK_NUMPAD_ENTER: |
1568 | if (!m_grid->GetEventHandler()->ProcessEvent(event)) | |
2796cce3 RD |
1569 | m_editor->HandleReturn(event); |
1570 | break; | |
1571 | ||
2796cce3 RD |
1572 | default: |
1573 | event.Skip(); | |
1574 | } | |
1575 | } | |
1576 | ||
fb0de762 RD |
1577 | void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event) |
1578 | { | |
12a3f227 | 1579 | switch ( event.GetKeyCode() ) |
fb0de762 RD |
1580 | { |
1581 | case WXK_ESCAPE: | |
1582 | case WXK_TAB: | |
1583 | case WXK_RETURN: | |
a4f7bf58 | 1584 | case WXK_NUMPAD_ENTER: |
fb0de762 RD |
1585 | break; |
1586 | ||
1587 | default: | |
1588 | event.Skip(); | |
1589 | } | |
1590 | } | |
1591 | ||
c4608a8a VZ |
1592 | // ---------------------------------------------------------------------------- |
1593 | // wxGridCellWorker is an (almost) empty common base class for | |
1594 | // wxGridCellRenderer and wxGridCellEditor managing ref counting | |
1595 | // ---------------------------------------------------------------------------- | |
1596 | ||
1597 | void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params)) | |
1598 | { | |
1599 | // nothing to do | |
1600 | } | |
1601 | ||
1602 | wxGridCellWorker::~wxGridCellWorker() | |
1603 | { | |
1604 | } | |
1605 | ||
508011ce VZ |
1606 | // ============================================================================ |
1607 | // renderer classes | |
1608 | // ============================================================================ | |
1609 | ||
ab79958a VZ |
1610 | // ---------------------------------------------------------------------------- |
1611 | // wxGridCellRenderer | |
1612 | // ---------------------------------------------------------------------------- | |
1613 | ||
1614 | void wxGridCellRenderer::Draw(wxGrid& grid, | |
2796cce3 | 1615 | wxGridCellAttr& attr, |
ab79958a VZ |
1616 | wxDC& dc, |
1617 | const wxRect& rect, | |
c78b3acd | 1618 | int WXUNUSED(row), int WXUNUSED(col), |
ab79958a VZ |
1619 | bool isSelected) |
1620 | { | |
1621 | dc.SetBackgroundMode( wxSOLID ); | |
1622 | ||
04418332 | 1623 | // grey out fields if the grid is disabled |
4db6714b | 1624 | if ( grid.IsEnabled() ) |
ab79958a | 1625 | { |
ec157c8f WS |
1626 | if ( isSelected ) |
1627 | { | |
1628 | dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) ); | |
1629 | } | |
1630 | else | |
1631 | { | |
1632 | dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) ); | |
1633 | } | |
52d6f640 | 1634 | } |
ab79958a VZ |
1635 | else |
1636 | { | |
ec157c8f | 1637 | dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), wxSOLID)); |
ab79958a VZ |
1638 | } |
1639 | ||
1640 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
ab79958a VZ |
1641 | dc.DrawRectangle(rect); |
1642 | } | |
1643 | ||
508011ce VZ |
1644 | // ---------------------------------------------------------------------------- |
1645 | // wxGridCellStringRenderer | |
1646 | // ---------------------------------------------------------------------------- | |
1647 | ||
fbfb8bcc VZ |
1648 | void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid& grid, |
1649 | const wxGridCellAttr& attr, | |
816be743 VZ |
1650 | wxDC& dc, |
1651 | bool isSelected) | |
ab79958a | 1652 | { |
ab79958a VZ |
1653 | dc.SetBackgroundMode( wxTRANSPARENT ); |
1654 | ||
283b7808 VZ |
1655 | // TODO some special colours for attr.IsReadOnly() case? |
1656 | ||
04418332 | 1657 | // different coloured text when the grid is disabled |
4db6714b | 1658 | if ( grid.IsEnabled() ) |
ab79958a | 1659 | { |
73145b0e JS |
1660 | if ( isSelected ) |
1661 | { | |
1662 | dc.SetTextBackground( grid.GetSelectionBackground() ); | |
1663 | dc.SetTextForeground( grid.GetSelectionForeground() ); | |
1664 | } | |
1665 | else | |
1666 | { | |
1667 | dc.SetTextBackground( attr.GetBackgroundColour() ); | |
1668 | dc.SetTextForeground( attr.GetTextColour() ); | |
1669 | } | |
ab79958a VZ |
1670 | } |
1671 | else | |
1672 | { | |
ec157c8f WS |
1673 | dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); |
1674 | dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT)); | |
ab79958a | 1675 | } |
816be743 | 1676 | |
2796cce3 | 1677 | dc.SetFont( attr.GetFont() ); |
816be743 VZ |
1678 | } |
1679 | ||
fbfb8bcc | 1680 | wxSize wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr& attr, |
65e4e78e VZ |
1681 | wxDC& dc, |
1682 | const wxString& text) | |
1683 | { | |
f6bcfd97 | 1684 | wxCoord x = 0, y = 0, max_x = 0; |
65e4e78e | 1685 | dc.SetFont(attr.GetFont()); |
f6bcfd97 BP |
1686 | wxStringTokenizer tk(text, _T('\n')); |
1687 | while ( tk.HasMoreTokens() ) | |
1688 | { | |
1689 | dc.GetTextExtent(tk.GetNextToken(), &x, &y); | |
1690 | max_x = wxMax(max_x, x); | |
1691 | } | |
1692 | ||
1693 | y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines. | |
65e4e78e | 1694 | |
f6bcfd97 | 1695 | return wxSize(max_x, y); |
65e4e78e VZ |
1696 | } |
1697 | ||
1698 | wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid, | |
1699 | wxGridCellAttr& attr, | |
1700 | wxDC& dc, | |
1701 | int row, int col) | |
1702 | { | |
1703 | return DoGetBestSize(attr, dc, grid.GetCellValue(row, col)); | |
1704 | } | |
1705 | ||
816be743 VZ |
1706 | void wxGridCellStringRenderer::Draw(wxGrid& grid, |
1707 | wxGridCellAttr& attr, | |
1708 | wxDC& dc, | |
1709 | const wxRect& rectCell, | |
1710 | int row, int col, | |
1711 | bool isSelected) | |
1712 | { | |
27f35b66 | 1713 | wxRect rect = rectCell; |
dc1f566f SN |
1714 | rect.Inflate(-1); |
1715 | ||
1716 | // erase only this cells background, overflow cells should have been erased | |
1717 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1718 | ||
1719 | int hAlign, vAlign; | |
1720 | attr.GetAlignment(&hAlign, &vAlign); | |
27f35b66 | 1721 | |
39b80349 SN |
1722 | int overflowCols = 0; |
1723 | ||
27f35b66 SN |
1724 | if (attr.GetOverflow()) |
1725 | { | |
1726 | int cols = grid.GetNumberCols(); | |
1727 | int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth(); | |
1728 | int cell_rows, cell_cols; | |
1729 | attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <=0 | |
1730 | if ((best_width > rectCell.width) && (col < cols) && grid.GetTable()) | |
1731 | { | |
1732 | int i, c_cols, c_rows; | |
1733 | for (i = col+cell_cols; i < cols; i++) | |
1734 | { | |
ca65c044 | 1735 | bool is_empty = true; |
39b80349 | 1736 | for (int j=row; j<row+cell_rows; j++) |
27f35b66 | 1737 | { |
39b80349 | 1738 | // check w/ anchor cell for multicell block |
ef4d6ce8 | 1739 | grid.GetCellSize(j, i, &c_rows, &c_cols); |
39b80349 | 1740 | if (c_rows > 0) c_rows = 0; |
ef4d6ce8 | 1741 | if (!grid.GetTable()->IsEmptyCell(j+c_rows, i)) |
39b80349 | 1742 | { |
ca65c044 | 1743 | is_empty = false; |
ef4d6ce8 | 1744 | break; |
39b80349 | 1745 | } |
27f35b66 | 1746 | } |
39b80349 SN |
1747 | if (is_empty) |
1748 | rect.width += grid.GetColSize(i); | |
dc1f566f SN |
1749 | else |
1750 | { | |
1751 | i--; | |
1752 | break; | |
1753 | } | |
4db6714b KH |
1754 | if (rect.width >= best_width) |
1755 | break; | |
2b5f62a0 | 1756 | } |
39b80349 | 1757 | overflowCols = i - col - cell_cols + 1; |
4db6714b KH |
1758 | if (overflowCols >= cols) |
1759 | overflowCols = cols - 1; | |
39b80349 | 1760 | } |
27f35b66 | 1761 | |
dc1f566f SN |
1762 | if (overflowCols > 0) // redraw overflow cells w/ proper hilight |
1763 | { | |
39b80349 | 1764 | hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned |
2b5f62a0 | 1765 | wxRect clip = rect; |
39b80349 | 1766 | clip.x += rectCell.width; |
dc1f566f SN |
1767 | // draw each overflow cell individually |
1768 | int col_end = col+cell_cols+overflowCols; | |
1769 | if (col_end >= grid.GetNumberCols()) | |
2b5f62a0 | 1770 | col_end = grid.GetNumberCols() - 1; |
dc1f566f SN |
1771 | for (int i = col+cell_cols; i <= col_end; i++) |
1772 | { | |
dc1f566f SN |
1773 | clip.width = grid.GetColSize(i) - 1; |
1774 | dc.DestroyClippingRegion(); | |
1775 | dc.SetClippingRegion(clip); | |
39b80349 SN |
1776 | |
1777 | SetTextColoursAndFont(grid, attr, dc, | |
2b5f62a0 | 1778 | grid.IsInSelection(row,i)); |
39b80349 | 1779 | |
dc1f566f | 1780 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), |
2b5f62a0 | 1781 | rect, hAlign, vAlign); |
39b80349 | 1782 | clip.x += grid.GetColSize(i) - 1; |
dc1f566f | 1783 | } |
ab79958a | 1784 | |
39b80349 | 1785 | rect = rectCell; |
2b5f62a0 | 1786 | rect.Inflate(-1); |
dc1f566f | 1787 | rect.width++; |
39b80349 | 1788 | dc.DestroyClippingRegion(); |
dc1f566f | 1789 | } |
39b80349 | 1790 | } |
ab79958a | 1791 | |
dc1f566f SN |
1792 | // now we only have to draw the text |
1793 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
39b80349 | 1794 | |
ab79958a VZ |
1795 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), |
1796 | rect, hAlign, vAlign); | |
1797 | } | |
1798 | ||
65e4e78e VZ |
1799 | // ---------------------------------------------------------------------------- |
1800 | // wxGridCellNumberRenderer | |
1801 | // ---------------------------------------------------------------------------- | |
1802 | ||
fbfb8bcc | 1803 | wxString wxGridCellNumberRenderer::GetString(const wxGrid& grid, int row, int col) |
65e4e78e VZ |
1804 | { |
1805 | wxGridTableBase *table = grid.GetTable(); | |
1806 | wxString text; | |
1807 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
1808 | { | |
1809 | text.Printf(_T("%ld"), table->GetValueAsLong(row, col)); | |
1810 | } | |
9c4ba614 VZ |
1811 | else |
1812 | { | |
1813 | text = table->GetValue(row, col); | |
1814 | } | |
65e4e78e VZ |
1815 | |
1816 | return text; | |
1817 | } | |
1818 | ||
816be743 VZ |
1819 | void wxGridCellNumberRenderer::Draw(wxGrid& grid, |
1820 | wxGridCellAttr& attr, | |
1821 | wxDC& dc, | |
1822 | const wxRect& rectCell, | |
1823 | int row, int col, | |
1824 | bool isSelected) | |
1825 | { | |
1826 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1827 | ||
1828 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1829 | ||
1830 | // draw the text right aligned by default | |
1831 | int hAlign, vAlign; | |
1832 | attr.GetAlignment(&hAlign, &vAlign); | |
4c7277db | 1833 | hAlign = wxALIGN_RIGHT; |
816be743 VZ |
1834 | |
1835 | wxRect rect = rectCell; | |
1836 | rect.Inflate(-1); | |
1837 | ||
65e4e78e VZ |
1838 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
1839 | } | |
816be743 | 1840 | |
65e4e78e VZ |
1841 | wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid, |
1842 | wxGridCellAttr& attr, | |
1843 | wxDC& dc, | |
1844 | int row, int col) | |
1845 | { | |
1846 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
816be743 VZ |
1847 | } |
1848 | ||
1849 | // ---------------------------------------------------------------------------- | |
1850 | // wxGridCellFloatRenderer | |
1851 | // ---------------------------------------------------------------------------- | |
1852 | ||
1853 | wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision) | |
1854 | { | |
1855 | SetWidth(width); | |
1856 | SetPrecision(precision); | |
1857 | } | |
1858 | ||
e72b4213 VZ |
1859 | wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const |
1860 | { | |
1861 | wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer; | |
1862 | renderer->m_width = m_width; | |
1863 | renderer->m_precision = m_precision; | |
1864 | renderer->m_format = m_format; | |
1865 | ||
1866 | return renderer; | |
1867 | } | |
1868 | ||
fbfb8bcc | 1869 | wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col) |
65e4e78e VZ |
1870 | { |
1871 | wxGridTableBase *table = grid.GetTable(); | |
0b190b0f VZ |
1872 | |
1873 | bool hasDouble; | |
1874 | double val; | |
65e4e78e VZ |
1875 | wxString text; |
1876 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
1877 | { | |
0b190b0f | 1878 | val = table->GetValueAsDouble(row, col); |
ca65c044 | 1879 | hasDouble = true; |
65e4e78e | 1880 | } |
9c4ba614 VZ |
1881 | else |
1882 | { | |
1883 | text = table->GetValue(row, col); | |
0b190b0f | 1884 | hasDouble = text.ToDouble(&val); |
9c4ba614 | 1885 | } |
65e4e78e | 1886 | |
0b190b0f VZ |
1887 | if ( hasDouble ) |
1888 | { | |
1889 | if ( !m_format ) | |
1890 | { | |
1891 | if ( m_width == -1 ) | |
1892 | { | |
19d7140e VZ |
1893 | if ( m_precision == -1 ) |
1894 | { | |
2b5f62a0 VZ |
1895 | // default width/precision |
1896 | m_format = _T("%f"); | |
1897 | } | |
19d7140e VZ |
1898 | else |
1899 | { | |
1900 | m_format.Printf(_T("%%.%df"), m_precision); | |
1901 | } | |
0b190b0f VZ |
1902 | } |
1903 | else if ( m_precision == -1 ) | |
1904 | { | |
1905 | // default precision | |
1906 | m_format.Printf(_T("%%%d.f"), m_width); | |
1907 | } | |
1908 | else | |
1909 | { | |
1910 | m_format.Printf(_T("%%%d.%df"), m_width, m_precision); | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | text.Printf(m_format, val); | |
19d7140e | 1915 | |
0b190b0f VZ |
1916 | } |
1917 | //else: text already contains the string | |
1918 | ||
65e4e78e VZ |
1919 | return text; |
1920 | } | |
1921 | ||
816be743 VZ |
1922 | void wxGridCellFloatRenderer::Draw(wxGrid& grid, |
1923 | wxGridCellAttr& attr, | |
1924 | wxDC& dc, | |
1925 | const wxRect& rectCell, | |
1926 | int row, int col, | |
1927 | bool isSelected) | |
1928 | { | |
1929 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1930 | ||
1931 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1932 | ||
1933 | // draw the text right aligned by default | |
1934 | int hAlign, vAlign; | |
1935 | attr.GetAlignment(&hAlign, &vAlign); | |
4c7277db | 1936 | hAlign = wxALIGN_RIGHT; |
816be743 VZ |
1937 | |
1938 | wxRect rect = rectCell; | |
1939 | rect.Inflate(-1); | |
1940 | ||
65e4e78e VZ |
1941 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
1942 | } | |
816be743 | 1943 | |
65e4e78e VZ |
1944 | wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid, |
1945 | wxGridCellAttr& attr, | |
1946 | wxDC& dc, | |
1947 | int row, int col) | |
1948 | { | |
1949 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
816be743 VZ |
1950 | } |
1951 | ||
0b190b0f VZ |
1952 | void wxGridCellFloatRenderer::SetParameters(const wxString& params) |
1953 | { | |
0b190b0f VZ |
1954 | if ( !params ) |
1955 | { | |
1956 | // reset to defaults | |
1957 | SetWidth(-1); | |
1958 | SetPrecision(-1); | |
1959 | } | |
1960 | else | |
1961 | { | |
1962 | wxString tmp = params.BeforeFirst(_T(',')); | |
ec157c8f | 1963 | if ( !tmp.empty() ) |
0b190b0f VZ |
1964 | { |
1965 | long width; | |
19d7140e | 1966 | if ( tmp.ToLong(&width) ) |
0b190b0f | 1967 | { |
19d7140e | 1968 | SetWidth((int)width); |
0b190b0f VZ |
1969 | } |
1970 | else | |
1971 | { | |
19d7140e VZ |
1972 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str()); |
1973 | } | |
19d7140e | 1974 | } |
7448de8d WS |
1975 | tmp = params.AfterFirst(_T(',')); |
1976 | if ( !tmp.empty() ) | |
1977 | { | |
1978 | long precision; | |
19d7140e | 1979 | if ( tmp.ToLong(&precision) ) |
7448de8d | 1980 | { |
19d7140e | 1981 | SetPrecision((int)precision); |
7448de8d WS |
1982 | } |
1983 | else | |
1984 | { | |
19d7140e | 1985 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str()); |
7448de8d | 1986 | } |
0b190b0f VZ |
1987 | } |
1988 | } | |
1989 | } | |
1990 | ||
19d7140e | 1991 | |
508011ce VZ |
1992 | // ---------------------------------------------------------------------------- |
1993 | // wxGridCellBoolRenderer | |
1994 | // ---------------------------------------------------------------------------- | |
1995 | ||
65e4e78e | 1996 | wxSize wxGridCellBoolRenderer::ms_sizeCheckMark; |
508011ce | 1997 | |
b94ae1ea VZ |
1998 | // FIXME these checkbox size calculations are really ugly... |
1999 | ||
65e4e78e | 2000 | // between checkmark and box |
a95e38c0 | 2001 | static const wxCoord wxGRID_CHECKMARK_MARGIN = 2; |
508011ce | 2002 | |
65e4e78e VZ |
2003 | wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid, |
2004 | wxGridCellAttr& WXUNUSED(attr), | |
2005 | wxDC& WXUNUSED(dc), | |
2006 | int WXUNUSED(row), | |
2007 | int WXUNUSED(col)) | |
2008 | { | |
2009 | // compute it only once (no locks for MT safeness in GUI thread...) | |
2010 | if ( !ms_sizeCheckMark.x ) | |
297da4ba | 2011 | { |
65e4e78e | 2012 | // get checkbox size |
ca65c044 | 2013 | wxCheckBox *checkbox = new wxCheckBox(&grid, wxID_ANY, wxEmptyString); |
297da4ba | 2014 | wxSize size = checkbox->GetBestSize(); |
999836aa | 2015 | wxCoord checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN; |
297da4ba | 2016 | |
65e4e78e | 2017 | // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result |
69d8f612 | 2018 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
65e4e78e | 2019 | checkSize -= size.y / 2; |
297da4ba VZ |
2020 | #endif |
2021 | ||
2022 | delete checkbox; | |
65e4e78e VZ |
2023 | |
2024 | ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize; | |
297da4ba VZ |
2025 | } |
2026 | ||
65e4e78e VZ |
2027 | return ms_sizeCheckMark; |
2028 | } | |
2029 | ||
2030 | void wxGridCellBoolRenderer::Draw(wxGrid& grid, | |
2031 | wxGridCellAttr& attr, | |
2032 | wxDC& dc, | |
2033 | const wxRect& rect, | |
2034 | int row, int col, | |
2035 | bool isSelected) | |
2036 | { | |
2037 | wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); | |
2038 | ||
297da4ba | 2039 | // draw a check mark in the centre (ignoring alignment - TODO) |
65e4e78e | 2040 | wxSize size = GetBestSize(grid, attr, dc, row, col); |
b94ae1ea VZ |
2041 | |
2042 | // don't draw outside the cell | |
2043 | wxCoord minSize = wxMin(rect.width, rect.height); | |
2044 | if ( size.x >= minSize || size.y >= minSize ) | |
2045 | { | |
2046 | // and even leave (at least) 1 pixel margin | |
2047 | size.x = size.y = minSize - 2; | |
2048 | } | |
2049 | ||
2050 | // draw a border around checkmark | |
1bd71df9 JS |
2051 | int vAlign, hAlign; |
2052 | attr.GetAlignment(& hAlign, &vAlign); | |
52d6f640 | 2053 | |
a95e38c0 | 2054 | wxRect rectBorder; |
1bd71df9 JS |
2055 | if (hAlign == wxALIGN_CENTRE) |
2056 | { | |
2057 | rectBorder.x = rect.x + rect.width/2 - size.x/2; | |
2058 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2059 | rectBorder.width = size.x; | |
2060 | rectBorder.height = size.y; | |
2061 | } | |
2062 | else if (hAlign == wxALIGN_LEFT) | |
2063 | { | |
2064 | rectBorder.x = rect.x + 2; | |
2065 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2066 | rectBorder.width = size.x; | |
52d6f640 | 2067 | rectBorder.height = size.y; |
1bd71df9 JS |
2068 | } |
2069 | else if (hAlign == wxALIGN_RIGHT) | |
2070 | { | |
2071 | rectBorder.x = rect.x + rect.width - size.x - 2; | |
2072 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2073 | rectBorder.width = size.x; | |
52d6f640 | 2074 | rectBorder.height = size.y; |
1bd71df9 | 2075 | } |
b94ae1ea | 2076 | |
f2d76237 | 2077 | bool value; |
b94ae1ea | 2078 | if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) |
f2d76237 RD |
2079 | value = grid.GetTable()->GetValueAsBool(row, col); |
2080 | else | |
695a3263 MB |
2081 | { |
2082 | wxString cellval( grid.GetTable()->GetValue(row, col) ); | |
8dd8f875 | 2083 | value = !( !cellval || (cellval == wxT("0")) ); |
695a3263 | 2084 | } |
f2d76237 RD |
2085 | |
2086 | if ( value ) | |
508011ce | 2087 | { |
a95e38c0 VZ |
2088 | wxRect rectMark = rectBorder; |
2089 | #ifdef __WXMSW__ | |
2090 | // MSW DrawCheckMark() is weird (and should probably be changed...) | |
2091 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2); | |
2092 | rectMark.x++; | |
2093 | rectMark.y++; | |
2094 | #else // !MSW | |
2095 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN); | |
2096 | #endif // MSW/!MSW | |
2097 | ||
508011ce VZ |
2098 | dc.SetTextForeground(attr.GetTextColour()); |
2099 | dc.DrawCheckMark(rectMark); | |
2100 | } | |
a95e38c0 VZ |
2101 | |
2102 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
2103 | dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID)); | |
2104 | dc.DrawRectangle(rectBorder); | |
508011ce VZ |
2105 | } |
2106 | ||
2796cce3 RD |
2107 | // ---------------------------------------------------------------------------- |
2108 | // wxGridCellAttr | |
2109 | // ---------------------------------------------------------------------------- | |
2110 | ||
1df4050d VZ |
2111 | void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) |
2112 | { | |
2113 | m_nRef = 1; | |
2114 | ||
2115 | m_isReadOnly = Unset; | |
2116 | ||
2117 | m_renderer = NULL; | |
2118 | m_editor = NULL; | |
2119 | ||
2120 | m_attrkind = wxGridCellAttr::Cell; | |
2121 | ||
27f35b66 | 2122 | m_sizeRows = m_sizeCols = 1; |
b63fce94 | 2123 | m_overflow = UnsetOverflow; |
27f35b66 | 2124 | |
1df4050d VZ |
2125 | SetDefAttr(attrDefault); |
2126 | } | |
2127 | ||
39bcce60 | 2128 | wxGridCellAttr *wxGridCellAttr::Clone() const |
a68c1246 | 2129 | { |
1df4050d VZ |
2130 | wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr); |
2131 | ||
a68c1246 VZ |
2132 | if ( HasTextColour() ) |
2133 | attr->SetTextColour(GetTextColour()); | |
2134 | if ( HasBackgroundColour() ) | |
2135 | attr->SetBackgroundColour(GetBackgroundColour()); | |
2136 | if ( HasFont() ) | |
2137 | attr->SetFont(GetFont()); | |
2138 | if ( HasAlignment() ) | |
2139 | attr->SetAlignment(m_hAlign, m_vAlign); | |
2140 | ||
27f35b66 SN |
2141 | attr->SetSize( m_sizeRows, m_sizeCols ); |
2142 | ||
a68c1246 VZ |
2143 | if ( m_renderer ) |
2144 | { | |
2145 | attr->SetRenderer(m_renderer); | |
39bcce60 | 2146 | m_renderer->IncRef(); |
a68c1246 VZ |
2147 | } |
2148 | if ( m_editor ) | |
2149 | { | |
2150 | attr->SetEditor(m_editor); | |
39bcce60 | 2151 | m_editor->IncRef(); |
a68c1246 VZ |
2152 | } |
2153 | ||
2154 | if ( IsReadOnly() ) | |
2155 | attr->SetReadOnly(); | |
2156 | ||
19d7140e VZ |
2157 | attr->SetKind( m_attrkind ); |
2158 | ||
a68c1246 VZ |
2159 | return attr; |
2160 | } | |
2161 | ||
19d7140e VZ |
2162 | void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) |
2163 | { | |
2164 | if ( !HasTextColour() && mergefrom->HasTextColour() ) | |
2165 | SetTextColour(mergefrom->GetTextColour()); | |
2166 | if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() ) | |
2167 | SetBackgroundColour(mergefrom->GetBackgroundColour()); | |
2168 | if ( !HasFont() && mergefrom->HasFont() ) | |
2169 | SetFont(mergefrom->GetFont()); | |
4db6714b KH |
2170 | if ( !HasAlignment() && mergefrom->HasAlignment() ) |
2171 | { | |
19d7140e VZ |
2172 | int hAlign, vAlign; |
2173 | mergefrom->GetAlignment( &hAlign, &vAlign); | |
2174 | SetAlignment(hAlign, vAlign); | |
2175 | } | |
3100c3db RD |
2176 | if ( !HasSize() && mergefrom->HasSize() ) |
2177 | mergefrom->GetSize( &m_sizeRows, &m_sizeCols ); | |
27f35b66 | 2178 | |
19d7140e VZ |
2179 | // Directly access member functions as GetRender/Editor don't just return |
2180 | // m_renderer/m_editor | |
2181 | // | |
2182 | // Maybe add support for merge of Render and Editor? | |
2183 | if (!HasRenderer() && mergefrom->HasRenderer() ) | |
bf7945ce | 2184 | { |
19d7140e VZ |
2185 | m_renderer = mergefrom->m_renderer; |
2186 | m_renderer->IncRef(); | |
2187 | } | |
2188 | if ( !HasEditor() && mergefrom->HasEditor() ) | |
2189 | { | |
2190 | m_editor = mergefrom->m_editor; | |
2191 | m_editor->IncRef(); | |
2192 | } | |
2193 | if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) | |
2194 | SetReadOnly(mergefrom->IsReadOnly()); | |
2195 | ||
ef5df12b | 2196 | if (!HasOverflowMode() && mergefrom->HasOverflowMode() ) |
ff699386 | 2197 | SetOverflow(mergefrom->GetOverflow()); |
ef5df12b | 2198 | |
19d7140e VZ |
2199 | SetDefAttr(mergefrom->m_defGridAttr); |
2200 | } | |
2201 | ||
27f35b66 SN |
2202 | void wxGridCellAttr::SetSize(int num_rows, int num_cols) |
2203 | { | |
2204 | // The size of a cell is normally 1,1 | |
2205 | ||
2206 | // If this cell is larger (2,2) then this is the top left cell | |
2207 | // the other cells that will be covered (lower right cells) must be | |
2208 | // set to negative or zero values such that | |
2209 | // row + num_rows of the covered cell points to the larger cell (this cell) | |
2210 | // same goes for the col + num_cols. | |
2211 | ||
2212 | // Size of 0,0 is NOT valid, neither is <=0 and any positive value | |
2213 | ||
2214 | wxASSERT_MSG( (!((num_rows>0)&&(num_cols<=0)) || | |
2215 | !((num_rows<=0)&&(num_cols>0)) || | |
2216 | !((num_rows==0)&&(num_cols==0))), | |
2217 | wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values")); | |
2218 | ||
2219 | m_sizeRows = num_rows; | |
2220 | m_sizeCols = num_cols; | |
2221 | } | |
2222 | ||
2796cce3 RD |
2223 | const wxColour& wxGridCellAttr::GetTextColour() const |
2224 | { | |
2225 | if (HasTextColour()) | |
508011ce | 2226 | { |
2796cce3 | 2227 | return m_colText; |
508011ce | 2228 | } |
0926b2fc | 2229 | else if (m_defGridAttr && m_defGridAttr != this) |
508011ce | 2230 | { |
2796cce3 | 2231 | return m_defGridAttr->GetTextColour(); |
508011ce VZ |
2232 | } |
2233 | else | |
2234 | { | |
2796cce3 RD |
2235 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2236 | return wxNullColour; | |
2237 | } | |
2238 | } | |
2239 | ||
2240 | ||
2241 | const wxColour& wxGridCellAttr::GetBackgroundColour() const | |
2242 | { | |
2243 | if (HasBackgroundColour()) | |
2244 | return m_colBack; | |
0926b2fc | 2245 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2246 | return m_defGridAttr->GetBackgroundColour(); |
508011ce VZ |
2247 | else |
2248 | { | |
2796cce3 RD |
2249 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2250 | return wxNullColour; | |
2251 | } | |
2252 | } | |
2253 | ||
2254 | ||
2255 | const wxFont& wxGridCellAttr::GetFont() const | |
2256 | { | |
2257 | if (HasFont()) | |
2258 | return m_font; | |
0926b2fc | 2259 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2260 | return m_defGridAttr->GetFont(); |
508011ce VZ |
2261 | else |
2262 | { | |
2796cce3 RD |
2263 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2264 | return wxNullFont; | |
2265 | } | |
2266 | } | |
2267 | ||
2268 | ||
2269 | void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const | |
2270 | { | |
508011ce VZ |
2271 | if (HasAlignment()) |
2272 | { | |
4db6714b KH |
2273 | if ( hAlign ) |
2274 | *hAlign = m_hAlign; | |
2275 | if ( vAlign ) | |
2276 | *vAlign = m_vAlign; | |
2796cce3 | 2277 | } |
0926b2fc | 2278 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2279 | m_defGridAttr->GetAlignment(hAlign, vAlign); |
508011ce VZ |
2280 | else |
2281 | { | |
2796cce3 RD |
2282 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2283 | } | |
2284 | } | |
2285 | ||
27f35b66 SN |
2286 | void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const |
2287 | { | |
4db6714b KH |
2288 | if ( num_rows ) |
2289 | *num_rows = m_sizeRows; | |
2290 | if ( num_cols ) | |
2291 | *num_cols = m_sizeCols; | |
27f35b66 | 2292 | } |
2796cce3 | 2293 | |
f2d76237 | 2294 | // GetRenderer and GetEditor use a slightly different decision path about |
28a77bc4 RD |
2295 | // which attribute to use. If a non-default attr object has one then it is |
2296 | // used, otherwise the default editor or renderer is fetched from the grid and | |
2297 | // used. It should be the default for the data type of the cell. If it is | |
2298 | // NULL (because the table has a type that the grid does not have in its | |
2299 | // registry,) then the grid's default editor or renderer is used. | |
2300 | ||
2301 | wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const | |
2302 | { | |
3cf883a2 | 2303 | wxGridCellRenderer *renderer; |
28a77bc4 | 2304 | |
3cf883a2 | 2305 | if ( m_renderer && this != m_defGridAttr ) |
0b190b0f | 2306 | { |
3cf883a2 VZ |
2307 | // use the cells renderer if it has one |
2308 | renderer = m_renderer; | |
2309 | renderer->IncRef(); | |
0b190b0f | 2310 | } |
3cf883a2 | 2311 | else // no non default cell renderer |
0b190b0f | 2312 | { |
3cf883a2 VZ |
2313 | // get default renderer for the data type |
2314 | if ( grid ) | |
2315 | { | |
2316 | // GetDefaultRendererForCell() will do IncRef() for us | |
2317 | renderer = grid->GetDefaultRendererForCell(row, col); | |
2318 | } | |
2319 | else | |
2320 | { | |
2321 | renderer = NULL; | |
2322 | } | |
0b190b0f | 2323 | |
3cf883a2 VZ |
2324 | if ( !renderer ) |
2325 | { | |
0926b2fc | 2326 | if (m_defGridAttr && this != m_defGridAttr ) |
3cf883a2 VZ |
2327 | { |
2328 | // if we still don't have one then use the grid default | |
2329 | // (no need for IncRef() here neither) | |
2330 | renderer = m_defGridAttr->GetRenderer(NULL, 0, 0); | |
2331 | } | |
2332 | else // default grid attr | |
2333 | { | |
2334 | // use m_renderer which we had decided not to use initially | |
2335 | renderer = m_renderer; | |
2336 | if ( renderer ) | |
2337 | renderer->IncRef(); | |
2338 | } | |
2339 | } | |
0b190b0f | 2340 | } |
28a77bc4 | 2341 | |
3cf883a2 VZ |
2342 | // we're supposed to always find something |
2343 | wxASSERT_MSG(renderer, wxT("Missing default cell renderer")); | |
28a77bc4 RD |
2344 | |
2345 | return renderer; | |
2796cce3 RD |
2346 | } |
2347 | ||
3cf883a2 | 2348 | // same as above, except for s/renderer/editor/g |
28a77bc4 | 2349 | wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const |
07296f0b | 2350 | { |
3cf883a2 | 2351 | wxGridCellEditor *editor; |
0b190b0f | 2352 | |
3cf883a2 | 2353 | if ( m_editor && this != m_defGridAttr ) |
0b190b0f | 2354 | { |
3cf883a2 VZ |
2355 | // use the cells editor if it has one |
2356 | editor = m_editor; | |
2357 | editor->IncRef(); | |
0b190b0f | 2358 | } |
3cf883a2 | 2359 | else // no non default cell editor |
0b190b0f | 2360 | { |
3cf883a2 VZ |
2361 | // get default editor for the data type |
2362 | if ( grid ) | |
2363 | { | |
2364 | // GetDefaultEditorForCell() will do IncRef() for us | |
2365 | editor = grid->GetDefaultEditorForCell(row, col); | |
2366 | } | |
2367 | else | |
2368 | { | |
2369 | editor = NULL; | |
2370 | } | |
2371 | ||
2372 | if ( !editor ) | |
2373 | { | |
0926b2fc | 2374 | if ( m_defGridAttr && this != m_defGridAttr ) |
3cf883a2 VZ |
2375 | { |
2376 | // if we still don't have one then use the grid default | |
2377 | // (no need for IncRef() here neither) | |
2378 | editor = m_defGridAttr->GetEditor(NULL, 0, 0); | |
2379 | } | |
2380 | else // default grid attr | |
2381 | { | |
2382 | // use m_editor which we had decided not to use initially | |
2383 | editor = m_editor; | |
2384 | if ( editor ) | |
2385 | editor->IncRef(); | |
2386 | } | |
2387 | } | |
0b190b0f | 2388 | } |
28a77bc4 | 2389 | |
3cf883a2 VZ |
2390 | // we're supposed to always find something |
2391 | wxASSERT_MSG(editor, wxT("Missing default cell editor")); | |
2392 | ||
28a77bc4 | 2393 | return editor; |
07296f0b RD |
2394 | } |
2395 | ||
b99be8fb | 2396 | // ---------------------------------------------------------------------------- |
758cbedf | 2397 | // wxGridCellAttrData |
b99be8fb VZ |
2398 | // ---------------------------------------------------------------------------- |
2399 | ||
758cbedf | 2400 | void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) |
b99be8fb VZ |
2401 | { |
2402 | int n = FindIndex(row, col); | |
2403 | if ( n == wxNOT_FOUND ) | |
2404 | { | |
2405 | // add the attribute | |
2406 | m_attrs.Add(new wxGridCellWithAttr(row, col, attr)); | |
2407 | } | |
2408 | else | |
2409 | { | |
6f36917b VZ |
2410 | // free the old attribute |
2411 | m_attrs[(size_t)n].attr->DecRef(); | |
2412 | ||
b99be8fb VZ |
2413 | if ( attr ) |
2414 | { | |
2415 | // change the attribute | |
2e9a6788 | 2416 | m_attrs[(size_t)n].attr = attr; |
b99be8fb VZ |
2417 | } |
2418 | else | |
2419 | { | |
2420 | // remove this attribute | |
2421 | m_attrs.RemoveAt((size_t)n); | |
2422 | } | |
2423 | } | |
b99be8fb VZ |
2424 | } |
2425 | ||
758cbedf | 2426 | wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const |
b99be8fb VZ |
2427 | { |
2428 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2429 | ||
2430 | int n = FindIndex(row, col); | |
2431 | if ( n != wxNOT_FOUND ) | |
2432 | { | |
2e9a6788 VZ |
2433 | attr = m_attrs[(size_t)n].attr; |
2434 | attr->IncRef(); | |
b99be8fb VZ |
2435 | } |
2436 | ||
2437 | return attr; | |
2438 | } | |
2439 | ||
4d60017a SN |
2440 | void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) |
2441 | { | |
2442 | size_t count = m_attrs.GetCount(); | |
2443 | for ( size_t n = 0; n < count; n++ ) | |
2444 | { | |
2445 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
2446 | wxCoord row = coords.GetRow(); |
2447 | if ((size_t)row >= pos) | |
2448 | { | |
2449 | if (numRows > 0) | |
2450 | { | |
2451 | // If rows inserted, include row counter where necessary | |
2452 | coords.SetRow(row + numRows); | |
2453 | } | |
2454 | else if (numRows < 0) | |
2455 | { | |
2456 | // If rows deleted ... | |
2457 | if ((size_t)row >= pos - numRows) | |
2458 | { | |
2459 | // ...either decrement row counter (if row still exists)... | |
2460 | coords.SetRow(row + numRows); | |
2461 | } | |
2462 | else | |
2463 | { | |
2464 | // ...or remove the attribute | |
d3e9dd94 SN |
2465 | // No need to DecRef the attribute itself since this is |
2466 | // done be wxGridCellWithAttr's destructor! | |
01dd42b6 | 2467 | m_attrs.RemoveAt(n); |
4db6714b KH |
2468 | n--; |
2469 | count--; | |
d1c0b4f9 VZ |
2470 | } |
2471 | } | |
4d60017a SN |
2472 | } |
2473 | } | |
2474 | } | |
2475 | ||
2476 | void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) | |
2477 | { | |
2478 | size_t count = m_attrs.GetCount(); | |
2479 | for ( size_t n = 0; n < count; n++ ) | |
2480 | { | |
2481 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
2482 | wxCoord col = coords.GetCol(); |
2483 | if ( (size_t)col >= pos ) | |
2484 | { | |
2485 | if ( numCols > 0 ) | |
2486 | { | |
2487 | // If rows inserted, include row counter where necessary | |
2488 | coords.SetCol(col + numCols); | |
2489 | } | |
2490 | else if (numCols < 0) | |
2491 | { | |
2492 | // If rows deleted ... | |
2493 | if ((size_t)col >= pos - numCols) | |
2494 | { | |
2495 | // ...either decrement row counter (if row still exists)... | |
2496 | coords.SetCol(col + numCols); | |
2497 | } | |
2498 | else | |
2499 | { | |
2500 | // ...or remove the attribute | |
d3e9dd94 SN |
2501 | // No need to DecRef the attribute itself since this is |
2502 | // done be wxGridCellWithAttr's destructor! | |
01dd42b6 | 2503 | m_attrs.RemoveAt(n); |
d1c0b4f9 VZ |
2504 | n--; count--; |
2505 | } | |
2506 | } | |
4d60017a SN |
2507 | } |
2508 | } | |
2509 | } | |
2510 | ||
758cbedf | 2511 | int wxGridCellAttrData::FindIndex(int row, int col) const |
b99be8fb VZ |
2512 | { |
2513 | size_t count = m_attrs.GetCount(); | |
2514 | for ( size_t n = 0; n < count; n++ ) | |
2515 | { | |
2516 | const wxGridCellCoords& coords = m_attrs[n].coords; | |
2517 | if ( (coords.GetRow() == row) && (coords.GetCol() == col) ) | |
2518 | { | |
2519 | return n; | |
2520 | } | |
2521 | } | |
2522 | ||
2523 | return wxNOT_FOUND; | |
2524 | } | |
2525 | ||
758cbedf VZ |
2526 | // ---------------------------------------------------------------------------- |
2527 | // wxGridRowOrColAttrData | |
2528 | // ---------------------------------------------------------------------------- | |
2529 | ||
2530 | wxGridRowOrColAttrData::~wxGridRowOrColAttrData() | |
2531 | { | |
2532 | size_t count = m_attrs.Count(); | |
2533 | for ( size_t n = 0; n < count; n++ ) | |
2534 | { | |
2535 | m_attrs[n]->DecRef(); | |
2536 | } | |
2537 | } | |
2538 | ||
2539 | wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const | |
2540 | { | |
2541 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2542 | ||
2543 | int n = m_rowsOrCols.Index(rowOrCol); | |
2544 | if ( n != wxNOT_FOUND ) | |
2545 | { | |
2546 | attr = m_attrs[(size_t)n]; | |
2547 | attr->IncRef(); | |
2548 | } | |
2549 | ||
2550 | return attr; | |
2551 | } | |
2552 | ||
2553 | void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) | |
2554 | { | |
a95e38c0 VZ |
2555 | int i = m_rowsOrCols.Index(rowOrCol); |
2556 | if ( i == wxNOT_FOUND ) | |
758cbedf VZ |
2557 | { |
2558 | // add the attribute | |
2559 | m_rowsOrCols.Add(rowOrCol); | |
2560 | m_attrs.Add(attr); | |
2561 | } | |
2562 | else | |
2563 | { | |
a95e38c0 | 2564 | size_t n = (size_t)i; |
758cbedf VZ |
2565 | if ( attr ) |
2566 | { | |
2567 | // change the attribute | |
a95e38c0 VZ |
2568 | m_attrs[n]->DecRef(); |
2569 | m_attrs[n] = attr; | |
758cbedf VZ |
2570 | } |
2571 | else | |
2572 | { | |
2573 | // remove this attribute | |
a95e38c0 VZ |
2574 | m_attrs[n]->DecRef(); |
2575 | m_rowsOrCols.RemoveAt(n); | |
2576 | m_attrs.RemoveAt(n); | |
758cbedf VZ |
2577 | } |
2578 | } | |
2579 | } | |
2580 | ||
4d60017a SN |
2581 | void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) |
2582 | { | |
2583 | size_t count = m_attrs.GetCount(); | |
2584 | for ( size_t n = 0; n < count; n++ ) | |
2585 | { | |
2586 | int & rowOrCol = m_rowsOrCols[n]; | |
d1c0b4f9 VZ |
2587 | if ( (size_t)rowOrCol >= pos ) |
2588 | { | |
2589 | if ( numRowsOrCols > 0 ) | |
2590 | { | |
2591 | // If rows inserted, include row counter where necessary | |
2592 | rowOrCol += numRowsOrCols; | |
2593 | } | |
2594 | else if ( numRowsOrCols < 0) | |
2595 | { | |
2596 | // If rows deleted, either decrement row counter (if row still exists) | |
2597 | if ((size_t)rowOrCol >= pos - numRowsOrCols) | |
2598 | rowOrCol += numRowsOrCols; | |
2599 | else | |
2600 | { | |
01dd42b6 VZ |
2601 | m_rowsOrCols.RemoveAt(n); |
2602 | m_attrs[n]->DecRef(); | |
2603 | m_attrs.RemoveAt(n); | |
4db6714b KH |
2604 | n--; |
2605 | count--; | |
d1c0b4f9 VZ |
2606 | } |
2607 | } | |
4d60017a SN |
2608 | } |
2609 | } | |
2610 | } | |
2611 | ||
b99be8fb VZ |
2612 | // ---------------------------------------------------------------------------- |
2613 | // wxGridCellAttrProvider | |
2614 | // ---------------------------------------------------------------------------- | |
2615 | ||
2616 | wxGridCellAttrProvider::wxGridCellAttrProvider() | |
2617 | { | |
2618 | m_data = (wxGridCellAttrProviderData *)NULL; | |
2619 | } | |
2620 | ||
2621 | wxGridCellAttrProvider::~wxGridCellAttrProvider() | |
2622 | { | |
2623 | delete m_data; | |
2624 | } | |
2625 | ||
2626 | void wxGridCellAttrProvider::InitData() | |
2627 | { | |
2628 | m_data = new wxGridCellAttrProviderData; | |
2629 | } | |
2630 | ||
19d7140e VZ |
2631 | wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col, |
2632 | wxGridCellAttr::wxAttrKind kind ) const | |
b99be8fb | 2633 | { |
758cbedf VZ |
2634 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
2635 | if ( m_data ) | |
2636 | { | |
19d7140e | 2637 | switch(kind) |
758cbedf | 2638 | { |
19d7140e VZ |
2639 | case (wxGridCellAttr::Any): |
2640 | //Get cached merge attributes. | |
2641 | // Currenlty not used as no cache implemented as not mutiable | |
2642 | // attr = m_data->m_mergeAttr.GetAttr(row, col); | |
4db6714b | 2643 | if (!attr) |
19d7140e | 2644 | { |
4db6714b | 2645 | //Basically implement old version. |
19d7140e | 2646 | //Also check merge cache, so we don't have to re-merge every time.. |
999836aa VZ |
2647 | wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col); |
2648 | wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row); | |
2649 | wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col); | |
19d7140e | 2650 | |
4db6714b KH |
2651 | if ((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol)) |
2652 | { | |
2d0c2e79 | 2653 | // Two or more are non NULL |
19d7140e VZ |
2654 | attr = new wxGridCellAttr; |
2655 | attr->SetKind(wxGridCellAttr::Merged); | |
2656 | ||
bf7945ce | 2657 | //Order important.. |
4db6714b KH |
2658 | if (attrcell) |
2659 | { | |
19d7140e VZ |
2660 | attr->MergeWith(attrcell); |
2661 | attrcell->DecRef(); | |
2662 | } | |
4db6714b KH |
2663 | if (attrcol) |
2664 | { | |
19d7140e VZ |
2665 | attr->MergeWith(attrcol); |
2666 | attrcol->DecRef(); | |
2667 | } | |
4db6714b KH |
2668 | if (attrrow) |
2669 | { | |
19d7140e VZ |
2670 | attr->MergeWith(attrrow); |
2671 | attrrow->DecRef(); | |
2672 | } | |
2673 | //store merge attr if cache implemented | |
2674 | //attr->IncRef(); | |
2675 | //m_data->m_mergeAttr.SetAttr(attr, row, col); | |
2676 | } | |
2677 | else | |
2d0c2e79 | 2678 | { |
19d7140e | 2679 | // one or none is non null return it or null. |
4db6714b KH |
2680 | if (attrrow) |
2681 | attr = attrrow; | |
2682 | if (attrcol) | |
2d0c2e79 RD |
2683 | { |
2684 | if(attr) | |
2685 | attr->DecRef(); | |
2686 | attr = attrcol; | |
2687 | } | |
4db6714b | 2688 | if (attrcell) |
2d0c2e79 | 2689 | { |
4db6714b | 2690 | if (attr) |
2d0c2e79 RD |
2691 | attr->DecRef(); |
2692 | attr = attrcell; | |
2693 | } | |
19d7140e VZ |
2694 | } |
2695 | } | |
2696 | break; | |
4db6714b | 2697 | |
19d7140e VZ |
2698 | case (wxGridCellAttr::Cell): |
2699 | attr = m_data->m_cellAttrs.GetAttr(row, col); | |
2700 | break; | |
4db6714b | 2701 | |
19d7140e | 2702 | case (wxGridCellAttr::Col): |
2d0c2e79 | 2703 | attr = m_data->m_colAttrs.GetAttr(col); |
19d7140e | 2704 | break; |
4db6714b | 2705 | |
19d7140e | 2706 | case (wxGridCellAttr::Row): |
2d0c2e79 | 2707 | attr = m_data->m_rowAttrs.GetAttr(row); |
19d7140e | 2708 | break; |
4db6714b | 2709 | |
19d7140e VZ |
2710 | default: |
2711 | // unused as yet... | |
2712 | // (wxGridCellAttr::Default): | |
2713 | // (wxGridCellAttr::Merged): | |
2714 | break; | |
758cbedf VZ |
2715 | } |
2716 | } | |
758cbedf | 2717 | return attr; |
b99be8fb VZ |
2718 | } |
2719 | ||
2e9a6788 | 2720 | void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, |
b99be8fb VZ |
2721 | int row, int col) |
2722 | { | |
2723 | if ( !m_data ) | |
2724 | InitData(); | |
2725 | ||
758cbedf VZ |
2726 | m_data->m_cellAttrs.SetAttr(attr, row, col); |
2727 | } | |
2728 | ||
2729 | void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row) | |
2730 | { | |
2731 | if ( !m_data ) | |
2732 | InitData(); | |
2733 | ||
2734 | m_data->m_rowAttrs.SetAttr(attr, row); | |
2735 | } | |
2736 | ||
2737 | void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) | |
2738 | { | |
2739 | if ( !m_data ) | |
2740 | InitData(); | |
2741 | ||
2742 | m_data->m_colAttrs.SetAttr(attr, col); | |
b99be8fb VZ |
2743 | } |
2744 | ||
4d60017a SN |
2745 | void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) |
2746 | { | |
2747 | if ( m_data ) | |
2748 | { | |
2749 | m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); | |
2750 | ||
d1c0b4f9 | 2751 | m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); |
4d60017a SN |
2752 | } |
2753 | } | |
2754 | ||
2755 | void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) | |
2756 | { | |
2757 | if ( m_data ) | |
2758 | { | |
2759 | m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); | |
2760 | ||
d1c0b4f9 | 2761 | m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); |
4d60017a SN |
2762 | } |
2763 | } | |
2764 | ||
f2d76237 RD |
2765 | // ---------------------------------------------------------------------------- |
2766 | // wxGridTypeRegistry | |
2767 | // ---------------------------------------------------------------------------- | |
2768 | ||
2769 | wxGridTypeRegistry::~wxGridTypeRegistry() | |
2770 | { | |
b94ae1ea VZ |
2771 | size_t count = m_typeinfo.Count(); |
2772 | for ( size_t i = 0; i < count; i++ ) | |
f2d76237 RD |
2773 | delete m_typeinfo[i]; |
2774 | } | |
2775 | ||
2776 | ||
2777 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, | |
2778 | wxGridCellRenderer* renderer, | |
2779 | wxGridCellEditor* editor) | |
2780 | { | |
f2d76237 RD |
2781 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); |
2782 | ||
2783 | // is it already registered? | |
c4608a8a | 2784 | int loc = FindRegisteredDataType(typeName); |
39bcce60 VZ |
2785 | if ( loc != wxNOT_FOUND ) |
2786 | { | |
f2d76237 RD |
2787 | delete m_typeinfo[loc]; |
2788 | m_typeinfo[loc] = info; | |
2789 | } | |
39bcce60 VZ |
2790 | else |
2791 | { | |
f2d76237 RD |
2792 | m_typeinfo.Add(info); |
2793 | } | |
2794 | } | |
2795 | ||
c4608a8a VZ |
2796 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) |
2797 | { | |
2798 | size_t count = m_typeinfo.GetCount(); | |
2799 | for ( size_t i = 0; i < count; i++ ) | |
2800 | { | |
2801 | if ( typeName == m_typeinfo[i]->m_typeName ) | |
2802 | { | |
2803 | return i; | |
2804 | } | |
2805 | } | |
2806 | ||
2807 | return wxNOT_FOUND; | |
2808 | } | |
2809 | ||
f2d76237 RD |
2810 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) |
2811 | { | |
c4608a8a VZ |
2812 | int index = FindRegisteredDataType(typeName); |
2813 | if ( index == wxNOT_FOUND ) | |
2814 | { | |
2815 | // check whether this is one of the standard ones, in which case | |
2816 | // register it "on the fly" | |
3a8c693a | 2817 | #if wxUSE_TEXTCTRL |
c4608a8a VZ |
2818 | if ( typeName == wxGRID_VALUE_STRING ) |
2819 | { | |
2820 | RegisterDataType(wxGRID_VALUE_STRING, | |
2821 | new wxGridCellStringRenderer, | |
2822 | new wxGridCellTextEditor); | |
4db6714b KH |
2823 | } |
2824 | else | |
3a8c693a VZ |
2825 | #endif // wxUSE_TEXTCTRL |
2826 | #if wxUSE_CHECKBOX | |
2827 | if ( typeName == wxGRID_VALUE_BOOL ) | |
c4608a8a VZ |
2828 | { |
2829 | RegisterDataType(wxGRID_VALUE_BOOL, | |
2830 | new wxGridCellBoolRenderer, | |
2831 | new wxGridCellBoolEditor); | |
4db6714b KH |
2832 | } |
2833 | else | |
3a8c693a VZ |
2834 | #endif // wxUSE_CHECKBOX |
2835 | #if wxUSE_TEXTCTRL | |
2836 | if ( typeName == wxGRID_VALUE_NUMBER ) | |
c4608a8a VZ |
2837 | { |
2838 | RegisterDataType(wxGRID_VALUE_NUMBER, | |
2839 | new wxGridCellNumberRenderer, | |
2840 | new wxGridCellNumberEditor); | |
2841 | } | |
2842 | else if ( typeName == wxGRID_VALUE_FLOAT ) | |
2843 | { | |
2844 | RegisterDataType(wxGRID_VALUE_FLOAT, | |
2845 | new wxGridCellFloatRenderer, | |
2846 | new wxGridCellFloatEditor); | |
4db6714b KH |
2847 | } |
2848 | else | |
3a8c693a VZ |
2849 | #endif // wxUSE_TEXTCTRL |
2850 | #if wxUSE_COMBOBOX | |
2851 | if ( typeName == wxGRID_VALUE_CHOICE ) | |
c4608a8a VZ |
2852 | { |
2853 | RegisterDataType(wxGRID_VALUE_CHOICE, | |
2854 | new wxGridCellStringRenderer, | |
2855 | new wxGridCellChoiceEditor); | |
4db6714b KH |
2856 | } |
2857 | else | |
3a8c693a | 2858 | #endif // wxUSE_COMBOBOX |
c4608a8a VZ |
2859 | { |
2860 | return wxNOT_FOUND; | |
2861 | } | |
f2d76237 | 2862 | |
c4608a8a VZ |
2863 | // we get here only if just added the entry for this type, so return |
2864 | // the last index | |
2865 | index = m_typeinfo.GetCount() - 1; | |
2866 | } | |
2867 | ||
2868 | return index; | |
2869 | } | |
2870 | ||
2871 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) | |
2872 | { | |
2873 | int index = FindDataType(typeName); | |
2874 | if ( index == wxNOT_FOUND ) | |
2875 | { | |
2876 | // the first part of the typename is the "real" type, anything after ':' | |
2877 | // are the parameters for the renderer | |
2878 | index = FindDataType(typeName.BeforeFirst(_T(':'))); | |
2879 | if ( index == wxNOT_FOUND ) | |
2880 | { | |
2881 | return wxNOT_FOUND; | |
f2d76237 | 2882 | } |
c4608a8a VZ |
2883 | |
2884 | wxGridCellRenderer *renderer = GetRenderer(index); | |
2885 | wxGridCellRenderer *rendererOld = renderer; | |
2886 | renderer = renderer->Clone(); | |
2887 | rendererOld->DecRef(); | |
2888 | ||
2889 | wxGridCellEditor *editor = GetEditor(index); | |
2890 | wxGridCellEditor *editorOld = editor; | |
2891 | editor = editor->Clone(); | |
2892 | editorOld->DecRef(); | |
2893 | ||
2894 | // do it even if there are no parameters to reset them to defaults | |
2895 | wxString params = typeName.AfterFirst(_T(':')); | |
2896 | renderer->SetParameters(params); | |
2897 | editor->SetParameters(params); | |
2898 | ||
2899 | // register the new typename | |
c4608a8a VZ |
2900 | RegisterDataType(typeName, renderer, editor); |
2901 | ||
2902 | // we just registered it, it's the last one | |
2903 | index = m_typeinfo.GetCount() - 1; | |
f2d76237 RD |
2904 | } |
2905 | ||
c4608a8a | 2906 | return index; |
f2d76237 RD |
2907 | } |
2908 | ||
2909 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) | |
2910 | { | |
2911 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; | |
faec5a43 SN |
2912 | if (renderer) |
2913 | renderer->IncRef(); | |
f2d76237 RD |
2914 | return renderer; |
2915 | } | |
2916 | ||
0b190b0f | 2917 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) |
f2d76237 RD |
2918 | { |
2919 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; | |
faec5a43 SN |
2920 | if (editor) |
2921 | editor->IncRef(); | |
f2d76237 RD |
2922 | return editor; |
2923 | } | |
2924 | ||
758cbedf VZ |
2925 | // ---------------------------------------------------------------------------- |
2926 | // wxGridTableBase | |
2927 | // ---------------------------------------------------------------------------- | |
2928 | ||
f85afd4e MB |
2929 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) |
2930 | ||
2931 | ||
2932 | wxGridTableBase::wxGridTableBase() | |
f85afd4e MB |
2933 | { |
2934 | m_view = (wxGrid *) NULL; | |
b99be8fb | 2935 | m_attrProvider = (wxGridCellAttrProvider *) NULL; |
f85afd4e MB |
2936 | } |
2937 | ||
2938 | wxGridTableBase::~wxGridTableBase() | |
2939 | { | |
b99be8fb VZ |
2940 | delete m_attrProvider; |
2941 | } | |
2942 | ||
2943 | void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider) | |
2944 | { | |
2945 | delete m_attrProvider; | |
2946 | m_attrProvider = attrProvider; | |
f85afd4e MB |
2947 | } |
2948 | ||
f2d76237 RD |
2949 | bool wxGridTableBase::CanHaveAttributes() |
2950 | { | |
2951 | if ( ! GetAttrProvider() ) | |
2952 | { | |
2953 | // use the default attr provider by default | |
2954 | SetAttrProvider(new wxGridCellAttrProvider); | |
2955 | } | |
ca65c044 | 2956 | return true; |
f2d76237 RD |
2957 | } |
2958 | ||
19d7140e | 2959 | wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) |
b99be8fb VZ |
2960 | { |
2961 | if ( m_attrProvider ) | |
19d7140e | 2962 | return m_attrProvider->GetAttr(row, col, kind); |
b99be8fb VZ |
2963 | else |
2964 | return (wxGridCellAttr *)NULL; | |
2965 | } | |
2966 | ||
3100c3db | 2967 | |
758cbedf | 2968 | void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) |
b99be8fb VZ |
2969 | { |
2970 | if ( m_attrProvider ) | |
2971 | { | |
19d7140e | 2972 | attr->SetKind(wxGridCellAttr::Cell); |
b99be8fb VZ |
2973 | m_attrProvider->SetAttr(attr, row, col); |
2974 | } | |
2975 | else | |
2976 | { | |
2977 | // as we take ownership of the pointer and don't store it, we must | |
2978 | // free it now | |
39bcce60 | 2979 | wxSafeDecRef(attr); |
b99be8fb VZ |
2980 | } |
2981 | } | |
2982 | ||
758cbedf VZ |
2983 | void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) |
2984 | { | |
2985 | if ( m_attrProvider ) | |
2986 | { | |
19d7140e | 2987 | attr->SetKind(wxGridCellAttr::Row); |
758cbedf VZ |
2988 | m_attrProvider->SetRowAttr(attr, row); |
2989 | } | |
2990 | else | |
2991 | { | |
2992 | // as we take ownership of the pointer and don't store it, we must | |
2993 | // free it now | |
39bcce60 | 2994 | wxSafeDecRef(attr); |
758cbedf VZ |
2995 | } |
2996 | } | |
2997 | ||
2998 | void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) | |
2999 | { | |
3000 | if ( m_attrProvider ) | |
3001 | { | |
19d7140e | 3002 | attr->SetKind(wxGridCellAttr::Col); |
758cbedf VZ |
3003 | m_attrProvider->SetColAttr(attr, col); |
3004 | } | |
3005 | else | |
3006 | { | |
3007 | // as we take ownership of the pointer and don't store it, we must | |
3008 | // free it now | |
39bcce60 | 3009 | wxSafeDecRef(attr); |
758cbedf VZ |
3010 | } |
3011 | } | |
3012 | ||
aa5e1f75 SN |
3013 | bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos), |
3014 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 3015 | { |
f6bcfd97 | 3016 | wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); |
8f177c8e | 3017 | |
ca65c044 | 3018 | return false; |
f85afd4e MB |
3019 | } |
3020 | ||
aa5e1f75 | 3021 | bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) ) |
f85afd4e | 3022 | { |
f6bcfd97 | 3023 | wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); |
8f177c8e | 3024 | |
ca65c044 | 3025 | return false; |
f85afd4e MB |
3026 | } |
3027 | ||
aa5e1f75 SN |
3028 | bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos), |
3029 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 3030 | { |
f6bcfd97 | 3031 | wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); |
8f177c8e | 3032 | |
ca65c044 | 3033 | return false; |
f85afd4e MB |
3034 | } |
3035 | ||
aa5e1f75 SN |
3036 | bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos), |
3037 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 3038 | { |
f6bcfd97 | 3039 | wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); |
8f177c8e | 3040 | |
ca65c044 | 3041 | return false; |
f85afd4e MB |
3042 | } |
3043 | ||
aa5e1f75 | 3044 | bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) ) |
f85afd4e | 3045 | { |
f6bcfd97 | 3046 | wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); |
8f177c8e | 3047 | |
ca65c044 | 3048 | return false; |
f85afd4e MB |
3049 | } |
3050 | ||
aa5e1f75 SN |
3051 | bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos), |
3052 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 3053 | { |
f6bcfd97 | 3054 | wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); |
8f177c8e | 3055 | |
ca65c044 | 3056 | return false; |
f85afd4e MB |
3057 | } |
3058 | ||
f85afd4e MB |
3059 | wxString wxGridTableBase::GetRowLabelValue( int row ) |
3060 | { | |
3061 | wxString s; | |
f2d76237 RD |
3062 | s << row + 1; // RD: Starting the rows at zero confuses users, no matter |
3063 | // how much it makes sense to us geeks. | |
f85afd4e MB |
3064 | return s; |
3065 | } | |
3066 | ||
3067 | wxString wxGridTableBase::GetColLabelValue( int col ) | |
3068 | { | |
3069 | // default col labels are: | |
3070 | // cols 0 to 25 : A-Z | |
3071 | // cols 26 to 675 : AA-ZZ | |
3072 | // etc. | |
3073 | ||
3074 | wxString s; | |
3075 | unsigned int i, n; | |
3076 | for ( n = 1; ; n++ ) | |
3077 | { | |
fef5c556 | 3078 | s += (wxChar) (_T('A') + (wxChar)( col%26 )); |
f85afd4e | 3079 | col = col/26 - 1; |
4db6714b KH |
3080 | if ( col < 0 ) |
3081 | break; | |
f85afd4e MB |
3082 | } |
3083 | ||
3084 | // reverse the string... | |
3085 | wxString s2; | |
3086 | for ( i = 0; i < n; i++ ) | |
3087 | { | |
3088 | s2 += s[n-i-1]; | |
3089 | } | |
3090 | ||
3091 | return s2; | |
3092 | } | |
3093 | ||
f2d76237 RD |
3094 | wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) ) |
3095 | { | |
816be743 | 3096 | return wxGRID_VALUE_STRING; |
f2d76237 RD |
3097 | } |
3098 | ||
3099 | bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col), | |
3100 | const wxString& typeName ) | |
3101 | { | |
816be743 | 3102 | return typeName == wxGRID_VALUE_STRING; |
f2d76237 RD |
3103 | } |
3104 | ||
3105 | bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName ) | |
3106 | { | |
3107 | return CanGetValueAs(row, col, typeName); | |
3108 | } | |
3109 | ||
3110 | long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) ) | |
3111 | { | |
3112 | return 0; | |
3113 | } | |
3114 | ||
3115 | double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) ) | |
3116 | { | |
3117 | return 0.0; | |
3118 | } | |
3119 | ||
3120 | bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) ) | |
3121 | { | |
ca65c044 | 3122 | return false; |
f2d76237 RD |
3123 | } |
3124 | ||
3125 | void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col), | |
3126 | long WXUNUSED(value) ) | |
3127 | { | |
3128 | } | |
3129 | ||
3130 | void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col), | |
3131 | double WXUNUSED(value) ) | |
3132 | { | |
3133 | } | |
3134 | ||
3135 | void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col), | |
3136 | bool WXUNUSED(value) ) | |
3137 | { | |
3138 | } | |
3139 | ||
3140 | ||
3141 | void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3142 | const wxString& WXUNUSED(typeName) ) | |
3143 | { | |
3144 | return NULL; | |
3145 | } | |
3146 | ||
3147 | void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3148 | const wxString& WXUNUSED(typeName), | |
3149 | void* WXUNUSED(value) ) | |
3150 | { | |
3151 | } | |
3152 | ||
f85afd4e MB |
3153 | ////////////////////////////////////////////////////////////////////// |
3154 | // | |
3155 | // Message class for the grid table to send requests and notifications | |
3156 | // to the grid view | |
3157 | // | |
3158 | ||
3159 | wxGridTableMessage::wxGridTableMessage() | |
3160 | { | |
3161 | m_table = (wxGridTableBase *) NULL; | |
3162 | m_id = -1; | |
3163 | m_comInt1 = -1; | |
3164 | m_comInt2 = -1; | |
3165 | } | |
3166 | ||
3167 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, | |
3168 | int commandInt1, int commandInt2 ) | |
3169 | { | |
3170 | m_table = table; | |
3171 | m_id = id; | |
3172 | m_comInt1 = commandInt1; | |
3173 | m_comInt2 = commandInt2; | |
3174 | } | |
3175 | ||
3176 | ||
3177 | ||
3178 | ////////////////////////////////////////////////////////////////////// | |
3179 | // | |
3180 | // A basic grid table for string data. An object of this class will | |
3181 | // created by wxGrid if you don't specify an alternative table class. | |
3182 | // | |
3183 | ||
223d09f6 | 3184 | WX_DEFINE_OBJARRAY(wxGridStringArray) |
f85afd4e MB |
3185 | |
3186 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) | |
3187 | ||
3188 | wxGridStringTable::wxGridStringTable() | |
3189 | : wxGridTableBase() | |
3190 | { | |
3191 | } | |
3192 | ||
3193 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) | |
3194 | : wxGridTableBase() | |
3195 | { | |
f85afd4e MB |
3196 | m_data.Alloc( numRows ); |
3197 | ||
3198 | wxArrayString sa; | |
3199 | sa.Alloc( numCols ); | |
27f35b66 | 3200 | sa.Add( wxEmptyString, numCols ); |
8f177c8e | 3201 | |
27f35b66 | 3202 | m_data.Add( sa, numRows ); |
f85afd4e MB |
3203 | } |
3204 | ||
3205 | wxGridStringTable::~wxGridStringTable() | |
3206 | { | |
3207 | } | |
3208 | ||
e32352cf | 3209 | int wxGridStringTable::GetNumberRows() |
f85afd4e MB |
3210 | { |
3211 | return m_data.GetCount(); | |
3212 | } | |
3213 | ||
e32352cf | 3214 | int wxGridStringTable::GetNumberCols() |
f85afd4e MB |
3215 | { |
3216 | if ( m_data.GetCount() > 0 ) | |
3217 | return m_data[0].GetCount(); | |
3218 | else | |
3219 | return 0; | |
3220 | } | |
3221 | ||
3222 | wxString wxGridStringTable::GetValue( int row, int col ) | |
3223 | { | |
3e13956a RD |
3224 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
3225 | wxEmptyString, | |
3226 | _T("invalid row or column index in wxGridStringTable") ); | |
af547d51 | 3227 | |
f85afd4e MB |
3228 | return m_data[row][col]; |
3229 | } | |
3230 | ||
f2d76237 | 3231 | void wxGridStringTable::SetValue( int row, int col, const wxString& value ) |
f85afd4e | 3232 | { |
3e13956a RD |
3233 | wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()), |
3234 | _T("invalid row or column index in wxGridStringTable") ); | |
af547d51 | 3235 | |
f2d76237 | 3236 | m_data[row][col] = value; |
f85afd4e MB |
3237 | } |
3238 | ||
3239 | bool wxGridStringTable::IsEmptyCell( int row, int col ) | |
3240 | { | |
3e13956a RD |
3241 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
3242 | true, | |
af547d51 VZ |
3243 | _T("invalid row or column index in wxGridStringTable") ); |
3244 | ||
f85afd4e MB |
3245 | return (m_data[row][col] == wxEmptyString); |
3246 | } | |
3247 | ||
f85afd4e MB |
3248 | void wxGridStringTable::Clear() |
3249 | { | |
3250 | int row, col; | |
3251 | int numRows, numCols; | |
8f177c8e | 3252 | |
f85afd4e MB |
3253 | numRows = m_data.GetCount(); |
3254 | if ( numRows > 0 ) | |
3255 | { | |
3256 | numCols = m_data[0].GetCount(); | |
3257 | ||
3258 | for ( row = 0; row < numRows; row++ ) | |
3259 | { | |
3260 | for ( col = 0; col < numCols; col++ ) | |
3261 | { | |
3262 | m_data[row][col] = wxEmptyString; | |
3263 | } | |
3264 | } | |
3265 | } | |
3266 | } | |
3267 | ||
3268 | ||
3269 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) | |
3270 | { | |
f85afd4e | 3271 | size_t curNumRows = m_data.GetCount(); |
f6bcfd97 BP |
3272 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3273 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3274 | |
f85afd4e MB |
3275 | if ( pos >= curNumRows ) |
3276 | { | |
3277 | return AppendRows( numRows ); | |
3278 | } | |
8f177c8e | 3279 | |
f85afd4e MB |
3280 | wxArrayString sa; |
3281 | sa.Alloc( curNumCols ); | |
27f35b66 SN |
3282 | sa.Add( wxEmptyString, curNumCols ); |
3283 | m_data.Insert( sa, pos, numRows ); | |
f85afd4e MB |
3284 | if ( GetView() ) |
3285 | { | |
3286 | wxGridTableMessage msg( this, | |
3287 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, | |
3288 | pos, | |
3289 | numRows ); | |
8f177c8e | 3290 | |
f85afd4e MB |
3291 | GetView()->ProcessTableMessage( msg ); |
3292 | } | |
3293 | ||
ca65c044 | 3294 | return true; |
f85afd4e MB |
3295 | } |
3296 | ||
3297 | bool wxGridStringTable::AppendRows( size_t numRows ) | |
3298 | { | |
f85afd4e | 3299 | size_t curNumRows = m_data.GetCount(); |
4db6714b KH |
3300 | size_t curNumCols = ( curNumRows > 0 |
3301 | ? m_data[0].GetCount() | |
3302 | : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3303 | |
f85afd4e MB |
3304 | wxArrayString sa; |
3305 | if ( curNumCols > 0 ) | |
3306 | { | |
3307 | sa.Alloc( curNumCols ); | |
27f35b66 | 3308 | sa.Add( wxEmptyString, curNumCols ); |
f85afd4e | 3309 | } |
8f177c8e | 3310 | |
27f35b66 | 3311 | m_data.Add( sa, numRows ); |
f85afd4e MB |
3312 | |
3313 | if ( GetView() ) | |
3314 | { | |
3315 | wxGridTableMessage msg( this, | |
3316 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, | |
3317 | numRows ); | |
8f177c8e | 3318 | |
f85afd4e MB |
3319 | GetView()->ProcessTableMessage( msg ); |
3320 | } | |
3321 | ||
ca65c044 | 3322 | return true; |
f85afd4e MB |
3323 | } |
3324 | ||
3325 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) | |
3326 | { | |
f85afd4e | 3327 | size_t curNumRows = m_data.GetCount(); |
8f177c8e | 3328 | |
f85afd4e MB |
3329 | if ( pos >= curNumRows ) |
3330 | { | |
e91d2033 VZ |
3331 | wxFAIL_MSG( wxString::Format |
3332 | ( | |
3333 | wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), | |
3334 | (unsigned long)pos, | |
3335 | (unsigned long)numRows, | |
3336 | (unsigned long)curNumRows | |
3337 | ) ); | |
3338 | ||
ca65c044 | 3339 | return false; |
f85afd4e MB |
3340 | } |
3341 | ||
3342 | if ( numRows > curNumRows - pos ) | |
3343 | { | |
3344 | numRows = curNumRows - pos; | |
3345 | } | |
8f177c8e | 3346 | |
f85afd4e MB |
3347 | if ( numRows >= curNumRows ) |
3348 | { | |
d57ad377 | 3349 | m_data.Clear(); |
f85afd4e MB |
3350 | } |
3351 | else | |
3352 | { | |
27f35b66 | 3353 | m_data.RemoveAt( pos, numRows ); |
f85afd4e | 3354 | } |
4db6714b | 3355 | |
f85afd4e MB |
3356 | if ( GetView() ) |
3357 | { | |
3358 | wxGridTableMessage msg( this, | |
3359 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, | |
3360 | pos, | |
3361 | numRows ); | |
8f177c8e | 3362 | |
f85afd4e MB |
3363 | GetView()->ProcessTableMessage( msg ); |
3364 | } | |
3365 | ||
ca65c044 | 3366 | return true; |
f85afd4e MB |
3367 | } |
3368 | ||
3369 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) | |
3370 | { | |
3371 | size_t row, col; | |
3372 | ||
3373 | size_t curNumRows = m_data.GetCount(); | |
4db6714b KH |
3374 | size_t curNumCols = ( curNumRows > 0 |
3375 | ? m_data[0].GetCount() | |
3376 | : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3377 | |
f85afd4e MB |
3378 | if ( pos >= curNumCols ) |
3379 | { | |
3380 | return AppendCols( numCols ); | |
3381 | } | |
3382 | ||
3383 | for ( row = 0; row < curNumRows; row++ ) | |
3384 | { | |
3385 | for ( col = pos; col < pos + numCols; col++ ) | |
3386 | { | |
3387 | m_data[row].Insert( wxEmptyString, col ); | |
3388 | } | |
3389 | } | |
4db6714b | 3390 | |
f85afd4e MB |
3391 | if ( GetView() ) |
3392 | { | |
3393 | wxGridTableMessage msg( this, | |
3394 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, | |
3395 | pos, | |
3396 | numCols ); | |
8f177c8e | 3397 | |
f85afd4e MB |
3398 | GetView()->ProcessTableMessage( msg ); |
3399 | } | |
3400 | ||
ca65c044 | 3401 | return true; |
f85afd4e MB |
3402 | } |
3403 | ||
3404 | bool wxGridStringTable::AppendCols( size_t numCols ) | |
3405 | { | |
27f35b66 | 3406 | size_t row; |
f85afd4e MB |
3407 | |
3408 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 | 3409 | #if 0 |
f85afd4e MB |
3410 | if ( !curNumRows ) |
3411 | { | |
3412 | // TODO: something better than this ? | |
3413 | // | |
f6bcfd97 | 3414 | wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") ); |
ca65c044 | 3415 | return false; |
f85afd4e | 3416 | } |
f6bcfd97 | 3417 | #endif |
8f177c8e | 3418 | |
f85afd4e MB |
3419 | for ( row = 0; row < curNumRows; row++ ) |
3420 | { | |
27f35b66 | 3421 | m_data[row].Add( wxEmptyString, numCols ); |
f85afd4e MB |
3422 | } |
3423 | ||
3424 | if ( GetView() ) | |
3425 | { | |
3426 | wxGridTableMessage msg( this, | |
3427 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, | |
3428 | numCols ); | |
8f177c8e | 3429 | |
f85afd4e MB |
3430 | GetView()->ProcessTableMessage( msg ); |
3431 | } | |
3432 | ||
ca65c044 | 3433 | return true; |
f85afd4e MB |
3434 | } |
3435 | ||
3436 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) | |
3437 | { | |
27f35b66 | 3438 | size_t row; |
f85afd4e MB |
3439 | |
3440 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 BP |
3441 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3442 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3443 | |
f85afd4e MB |
3444 | if ( pos >= curNumCols ) |
3445 | { | |
e91d2033 VZ |
3446 | wxFAIL_MSG( wxString::Format |
3447 | ( | |
3448 | wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), | |
3449 | (unsigned long)pos, | |
3450 | (unsigned long)numCols, | |
3451 | (unsigned long)curNumCols | |
3452 | ) ); | |
ca65c044 | 3453 | return false; |
f85afd4e MB |
3454 | } |
3455 | ||
3456 | if ( numCols > curNumCols - pos ) | |
3457 | { | |
8f177c8e | 3458 | numCols = curNumCols - pos; |
f85afd4e MB |
3459 | } |
3460 | ||
3461 | for ( row = 0; row < curNumRows; row++ ) | |
3462 | { | |
3463 | if ( numCols >= curNumCols ) | |
3464 | { | |
dcdce64e | 3465 | m_data[row].Clear(); |
f85afd4e MB |
3466 | } |
3467 | else | |
3468 | { | |
27f35b66 | 3469 | m_data[row].RemoveAt( pos, numCols ); |
f85afd4e MB |
3470 | } |
3471 | } | |
4db6714b | 3472 | |
f85afd4e MB |
3473 | if ( GetView() ) |
3474 | { | |
3475 | wxGridTableMessage msg( this, | |
3476 | wxGRIDTABLE_NOTIFY_COLS_DELETED, | |
3477 | pos, | |
3478 | numCols ); | |
8f177c8e | 3479 | |
f85afd4e MB |
3480 | GetView()->ProcessTableMessage( msg ); |
3481 | } | |
3482 | ||
ca65c044 | 3483 | return true; |
f85afd4e MB |
3484 | } |
3485 | ||
3486 | wxString wxGridStringTable::GetRowLabelValue( int row ) | |
3487 | { | |
3488 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3489 | { | |
3490 | // using default label | |
3491 | // | |
3492 | return wxGridTableBase::GetRowLabelValue( row ); | |
3493 | } | |
3494 | else | |
3495 | { | |
3496 | return m_rowLabels[ row ]; | |
3497 | } | |
3498 | } | |
3499 | ||
3500 | wxString wxGridStringTable::GetColLabelValue( int col ) | |
3501 | { | |
3502 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3503 | { | |
3504 | // using default label | |
3505 | // | |
3506 | return wxGridTableBase::GetColLabelValue( col ); | |
3507 | } | |
3508 | else | |
3509 | { | |
3510 | return m_colLabels[ col ]; | |
3511 | } | |
3512 | } | |
3513 | ||
3514 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) | |
3515 | { | |
3516 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3517 | { | |
3518 | int n = m_rowLabels.GetCount(); | |
3519 | int i; | |
3520 | for ( i = n; i <= row; i++ ) | |
3521 | { | |
3522 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); | |
3523 | } | |
3524 | } | |
3525 | ||
3526 | m_rowLabels[row] = value; | |
3527 | } | |
3528 | ||
3529 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) | |
3530 | { | |
3531 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3532 | { | |
3533 | int n = m_colLabels.GetCount(); | |
3534 | int i; | |
3535 | for ( i = n; i <= col; i++ ) | |
3536 | { | |
3537 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); | |
3538 | } | |
3539 | } | |
3540 | ||
3541 | m_colLabels[col] = value; | |
3542 | } | |
3543 | ||
3544 | ||
f85afd4e | 3545 | ////////////////////////////////////////////////////////////////////// |
2d66e025 MB |
3546 | ////////////////////////////////////////////////////////////////////// |
3547 | ||
3548 | IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow ) | |
3549 | ||
3550 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow ) | |
3551 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) | |
b51c3f27 | 3552 | EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel) |
2d66e025 MB |
3553 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) |
3554 | EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown ) | |
f6bcfd97 | 3555 | EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp ) |
63e2147c | 3556 | EVT_CHAR ( wxGridRowLabelWindow::OnChar ) |
2d66e025 MB |
3557 | END_EVENT_TABLE() |
3558 | ||
60ff3b99 VZ |
3559 | wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent, |
3560 | wxWindowID id, | |
2d66e025 | 3561 | const wxPoint &pos, const wxSize &size ) |
73bb6776 | 3562 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE ) |
2d66e025 MB |
3563 | { |
3564 | m_owner = parent; | |
3565 | } | |
3566 | ||
aa5e1f75 | 3567 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
3568 | { |
3569 | wxPaintDC dc(this); | |
3570 | ||
3571 | // NO - don't do this because it will set both the x and y origin | |
3572 | // coords to match the parent scrolled window and we just want to | |
3573 | // set the y coord - MB | |
3574 | // | |
3575 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 3576 | |
790ad94f | 3577 | int x, y; |
2d66e025 MB |
3578 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
3579 | dc.SetDeviceOrigin( 0, -y ); | |
60ff3b99 | 3580 | |
d10f4bf9 VZ |
3581 | wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); |
3582 | m_owner->DrawRowLabels( dc , rows ); | |
2d66e025 MB |
3583 | } |
3584 | ||
2d66e025 MB |
3585 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
3586 | { | |
3587 | m_owner->ProcessRowLabelMouseEvent( event ); | |
3588 | } | |
3589 | ||
b51c3f27 RD |
3590 | void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3591 | { | |
3592 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3593 | } | |
3594 | ||
2d66e025 MB |
3595 | // This seems to be required for wxMotif otherwise the mouse |
3596 | // cursor must be in the cell edit control to get key events | |
3597 | // | |
3598 | void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3599 | { | |
ffdd3c98 | 3600 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3601 | } |
3602 | ||
f6bcfd97 BP |
3603 | void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3604 | { | |
ffdd3c98 | 3605 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3606 | } |
3607 | ||
63e2147c RD |
3608 | void wxGridRowLabelWindow::OnChar( wxKeyEvent& event ) |
3609 | { | |
3610 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); | |
3611 | } | |
3612 | ||
2d66e025 | 3613 | |
2d66e025 MB |
3614 | ////////////////////////////////////////////////////////////////////// |
3615 | ||
3616 | IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow ) | |
3617 | ||
3618 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow ) | |
3619 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) | |
b51c3f27 | 3620 | EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel) |
2d66e025 MB |
3621 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) |
3622 | EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown ) | |
f6bcfd97 | 3623 | EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp ) |
63e2147c | 3624 | EVT_CHAR ( wxGridColLabelWindow::OnChar ) |
2d66e025 MB |
3625 | END_EVENT_TABLE() |
3626 | ||
60ff3b99 VZ |
3627 | wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent, |
3628 | wxWindowID id, | |
2d66e025 | 3629 | const wxPoint &pos, const wxSize &size ) |
73bb6776 | 3630 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE ) |
2d66e025 MB |
3631 | { |
3632 | m_owner = parent; | |
3633 | } | |
3634 | ||
aa5e1f75 | 3635 | void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
3636 | { |
3637 | wxPaintDC dc(this); | |
3638 | ||
3639 | // NO - don't do this because it will set both the x and y origin | |
3640 | // coords to match the parent scrolled window and we just want to | |
3641 | // set the x coord - MB | |
3642 | // | |
3643 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 3644 | |
790ad94f | 3645 | int x, y; |
2d66e025 MB |
3646 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
3647 | dc.SetDeviceOrigin( -x, 0 ); | |
3648 | ||
d10f4bf9 VZ |
3649 | wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() ); |
3650 | m_owner->DrawColLabels( dc , cols ); | |
2d66e025 MB |
3651 | } |
3652 | ||
2d66e025 MB |
3653 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
3654 | { | |
3655 | m_owner->ProcessColLabelMouseEvent( event ); | |
3656 | } | |
3657 | ||
b51c3f27 RD |
3658 | void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3659 | { | |
3660 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3661 | } | |
3662 | ||
2d66e025 MB |
3663 | // This seems to be required for wxMotif otherwise the mouse |
3664 | // cursor must be in the cell edit control to get key events | |
3665 | // | |
3666 | void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3667 | { | |
4db6714b KH |
3668 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
3669 | event.Skip(); | |
2d66e025 MB |
3670 | } |
3671 | ||
f6bcfd97 BP |
3672 | void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3673 | { | |
4db6714b KH |
3674 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
3675 | event.Skip(); | |
f6bcfd97 BP |
3676 | } |
3677 | ||
63e2147c RD |
3678 | void wxGridColLabelWindow::OnChar( wxKeyEvent& event ) |
3679 | { | |
4db6714b KH |
3680 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
3681 | event.Skip(); | |
63e2147c | 3682 | } |
2d66e025 MB |
3683 | |
3684 | ||
3685 | ////////////////////////////////////////////////////////////////////// | |
3686 | ||
3687 | IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow ) | |
3688 | ||
3689 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow ) | |
b51c3f27 | 3690 | EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel) |
2d66e025 | 3691 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) |
d2fdd8d2 | 3692 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint) |
2d66e025 | 3693 | EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown ) |
f6bcfd97 | 3694 | EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp ) |
63e2147c | 3695 | EVT_CHAR ( wxGridCornerLabelWindow::OnChar ) |
2d66e025 MB |
3696 | END_EVENT_TABLE() |
3697 | ||
60ff3b99 VZ |
3698 | wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent, |
3699 | wxWindowID id, | |
2d66e025 | 3700 | const wxPoint &pos, const wxSize &size ) |
73bb6776 | 3701 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE ) |
2d66e025 MB |
3702 | { |
3703 | m_owner = parent; | |
3704 | } | |
3705 | ||
d2fdd8d2 RR |
3706 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
3707 | { | |
3708 | wxPaintDC dc(this); | |
b99be8fb | 3709 | |
d2fdd8d2 RR |
3710 | int client_height = 0; |
3711 | int client_width = 0; | |
3712 | GetClientSize( &client_width, &client_height ); | |
b99be8fb | 3713 | |
11850ff3 VZ |
3714 | // VZ: any reason for this ifdef? (FIXME) |
3715 | #ifdef __WXGTK__ | |
4d1bc39c RR |
3716 | wxRect rect; |
3717 | rect.SetX( 1 ); | |
3718 | rect.SetY( 1 ); | |
3719 | rect.SetWidth( client_width - 2 ); | |
3720 | rect.SetHeight( client_height - 2 ); | |
ec157c8f | 3721 | |
4d1bc39c | 3722 | wxRendererNative::Get().DrawHeaderButton( this, dc, rect, 0 ); |
11850ff3 | 3723 | #else // !__WXGTK__ |
73145b0e | 3724 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) ); |
d2fdd8d2 RR |
3725 | dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 ); |
3726 | dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 ); | |
d2fdd8d2 RR |
3727 | dc.DrawLine( 0, 0, client_width, 0 ); |
3728 | dc.DrawLine( 0, 0, 0, client_height ); | |
73145b0e JS |
3729 | |
3730 | dc.SetPen( *wxWHITE_PEN ); | |
3731 | dc.DrawLine( 1, 1, client_width-1, 1 ); | |
3732 | dc.DrawLine( 1, 1, 1, client_height-1 ); | |
11850ff3 | 3733 | #endif // __WXGTK__/!__WXGTK__ |
d2fdd8d2 RR |
3734 | } |
3735 | ||
2d66e025 MB |
3736 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
3737 | { | |
3738 | m_owner->ProcessCornerLabelMouseEvent( event ); | |
3739 | } | |
3740 | ||
b51c3f27 RD |
3741 | void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3742 | { | |
3743 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3744 | } | |
3745 | ||
2d66e025 MB |
3746 | // This seems to be required for wxMotif otherwise the mouse |
3747 | // cursor must be in the cell edit control to get key events | |
3748 | // | |
3749 | void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3750 | { | |
ffdd3c98 | 3751 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3752 | } |
3753 | ||
f6bcfd97 BP |
3754 | void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3755 | { | |
ffdd3c98 | 3756 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3757 | } |
3758 | ||
63e2147c RD |
3759 | void wxGridCornerLabelWindow::OnChar( wxKeyEvent& event ) |
3760 | { | |
3761 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); | |
3762 | } | |
2d66e025 MB |
3763 | |
3764 | ||
f85afd4e MB |
3765 | ////////////////////////////////////////////////////////////////////// |
3766 | ||
59ddac01 | 3767 | IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow ) |
2d66e025 | 3768 | |
59ddac01 | 3769 | BEGIN_EVENT_TABLE( wxGridWindow, wxWindow ) |
2d66e025 | 3770 | EVT_PAINT( wxGridWindow::OnPaint ) |
b51c3f27 | 3771 | EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel) |
2d66e025 MB |
3772 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) |
3773 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) | |
f6bcfd97 | 3774 | EVT_KEY_UP( wxGridWindow::OnKeyUp ) |
63e2147c | 3775 | EVT_CHAR ( wxGridWindow::OnChar ) |
80acaf25 JS |
3776 | EVT_SET_FOCUS( wxGridWindow::OnFocus ) |
3777 | EVT_KILL_FOCUS( wxGridWindow::OnFocus ) | |
2796cce3 | 3778 | EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground ) |
2d66e025 MB |
3779 | END_EVENT_TABLE() |
3780 | ||
60ff3b99 VZ |
3781 | wxGridWindow::wxGridWindow( wxGrid *parent, |
3782 | wxGridRowLabelWindow *rowLblWin, | |
2d66e025 | 3783 | wxGridColLabelWindow *colLblWin, |
04418332 VZ |
3784 | wxWindowID id, |
3785 | const wxPoint &pos, | |
3786 | const wxSize &size ) | |
73bb6776 | 3787 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN|wxFULL_REPAINT_ON_RESIZE, |
04418332 | 3788 | wxT("grid window") ) |
2d66e025 MB |
3789 | { |
3790 | m_owner = parent; | |
3791 | m_rowLabelWin = rowLblWin; | |
3792 | m_colLabelWin = colLblWin; | |
2d66e025 MB |
3793 | } |
3794 | ||
2d66e025 MB |
3795 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
3796 | { | |
3797 | wxPaintDC dc( this ); | |
3798 | m_owner->PrepareDC( dc ); | |
796df70a | 3799 | wxRegion reg = GetUpdateRegion(); |
d10f4bf9 VZ |
3800 | wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg ); |
3801 | m_owner->DrawGridCellArea( dc , DirtyCells); | |
9496deb5 | 3802 | #if WXGRID_DRAW_LINES |
796df70a SN |
3803 | m_owner->DrawAllGridLines( dc, reg ); |
3804 | #endif | |
a5777624 | 3805 | m_owner->DrawGridSpace( dc ); |
d10f4bf9 | 3806 | m_owner->DrawHighlight( dc , DirtyCells ); |
2d66e025 MB |
3807 | } |
3808 | ||
2d66e025 MB |
3809 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) |
3810 | { | |
59ddac01 | 3811 | wxWindow::ScrollWindow( dx, dy, rect ); |
2d66e025 MB |
3812 | m_rowLabelWin->ScrollWindow( 0, dy, rect ); |
3813 | m_colLabelWin->ScrollWindow( dx, 0, rect ); | |
3814 | } | |
3815 | ||
2d66e025 MB |
3816 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) |
3817 | { | |
33e9fc54 RD |
3818 | if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this) |
3819 | SetFocus(); | |
902725ee | 3820 | |
2d66e025 MB |
3821 | m_owner->ProcessGridCellMouseEvent( event ); |
3822 | } | |
3823 | ||
b51c3f27 RD |
3824 | void wxGridWindow::OnMouseWheel( wxMouseEvent& event ) |
3825 | { | |
3826 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3827 | } | |
2d66e025 | 3828 | |
f6bcfd97 | 3829 | // This seems to be required for wxMotif/wxGTK otherwise the mouse |
2d66e025 MB |
3830 | // cursor must be in the cell edit control to get key events |
3831 | // | |
3832 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) | |
3833 | { | |
4db6714b KH |
3834 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
3835 | event.Skip(); | |
2d66e025 | 3836 | } |
f85afd4e | 3837 | |
f6bcfd97 BP |
3838 | void wxGridWindow::OnKeyUp( wxKeyEvent& event ) |
3839 | { | |
4db6714b KH |
3840 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
3841 | event.Skip(); | |
f6bcfd97 | 3842 | } |
7c8a8ad5 | 3843 | |
63e2147c RD |
3844 | void wxGridWindow::OnChar( wxKeyEvent& event ) |
3845 | { | |
4db6714b KH |
3846 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
3847 | event.Skip(); | |
63e2147c RD |
3848 | } |
3849 | ||
aa5e1f75 | 3850 | void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) |
8dd4f536 | 3851 | { |
8dd4f536 | 3852 | } |
025562fe | 3853 | |
80acaf25 JS |
3854 | void wxGridWindow::OnFocus(wxFocusEvent& event) |
3855 | { | |
3856 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3857 | event.Skip(); | |
3858 | } | |
2d66e025 MB |
3859 | |
3860 | ////////////////////////////////////////////////////////////////////// | |
3861 | ||
33188aa4 SN |
3862 | // Internal Helper function for computing row or column from some |
3863 | // (unscrolled) coordinate value, using either | |
70e8d961 | 3864 | // m_defaultRowHeight/m_defaultColWidth or binary search on array |
33188aa4 SN |
3865 | // of m_rowBottoms/m_ColRights to speed up the search! |
3866 | ||
3867 | // Internal helper macros for simpler use of that function | |
3868 | ||
3869 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, | |
64e15340 | 3870 | const wxArrayInt& BorderArray, int nMax, |
a967f048 | 3871 | bool clipToMinMax); |
33188aa4 SN |
3872 | |
3873 | #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \ | |
b8d24d4e | 3874 | m_minAcceptableColWidth, \ |
ca65c044 | 3875 | m_colRights, m_numCols, true) |
33188aa4 | 3876 | #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \ |
b8d24d4e | 3877 | m_minAcceptableRowHeight, \ |
ca65c044 | 3878 | m_rowBottoms, m_numRows, true) |
33188aa4 | 3879 | ///////////////////////////////////////////////////////////////////// |
07296f0b | 3880 | |
b0a877ec | 3881 | #if wxUSE_EXTENDED_RTTI |
73c36334 JS |
3882 | WX_DEFINE_FLAGS( wxGridStyle ) |
3883 | ||
3ff066a4 | 3884 | wxBEGIN_FLAGS( wxGridStyle ) |
73c36334 JS |
3885 | // new style border flags, we put them first to |
3886 | // use them for streaming out | |
3ff066a4 SC |
3887 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
3888 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
3889 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
3890 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
3891 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
3892 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
ca65c044 | 3893 | |
73c36334 | 3894 | // old style border flags |
3ff066a4 SC |
3895 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
3896 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
3897 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
3898 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
3899 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 3900 | wxFLAGS_MEMBER(wxBORDER) |
73c36334 JS |
3901 | |
3902 | // standard window styles | |
3ff066a4 SC |
3903 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
3904 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
3905 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
3906 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 3907 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
3908 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
3909 | wxFLAGS_MEMBER(wxVSCROLL) | |
3910 | wxFLAGS_MEMBER(wxHSCROLL) | |
73c36334 | 3911 | |
3ff066a4 | 3912 | wxEND_FLAGS( wxGridStyle ) |
73c36334 | 3913 | |
b0a877ec SC |
3914 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h") |
3915 | ||
3ff066a4 SC |
3916 | wxBEGIN_PROPERTIES_TABLE(wxGrid) |
3917 | wxHIDE_PROPERTY( Children ) | |
af498247 | 3918 | wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
3ff066a4 | 3919 | wxEND_PROPERTIES_TABLE() |
b0a877ec | 3920 | |
3ff066a4 SC |
3921 | wxBEGIN_HANDLERS_TABLE(wxGrid) |
3922 | wxEND_HANDLERS_TABLE() | |
b0a877ec | 3923 | |
ca65c044 | 3924 | wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
b0a877ec SC |
3925 | |
3926 | /* | |
2d0c2e79 | 3927 | Content-type: text/html ]>