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