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