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