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