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