]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/grid.cpp | |
3 | // Purpose: wxGrid and related classes | |
4 | // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn) | |
5 | // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios | |
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 | // For compilers that support precompilation, includes "wx/wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if wxUSE_GRID | |
20 | ||
21 | #include "wx/grid.h" | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/utils.h" | |
25 | #include "wx/dcclient.h" | |
26 | #include "wx/settings.h" | |
27 | #include "wx/log.h" | |
28 | #include "wx/textctrl.h" | |
29 | #include "wx/checkbox.h" | |
30 | #include "wx/combobox.h" | |
31 | #include "wx/valtext.h" | |
32 | #include "wx/intl.h" | |
33 | #include "wx/math.h" | |
34 | #include "wx/listbox.h" | |
35 | #endif | |
36 | ||
37 | #include "wx/textfile.h" | |
38 | #include "wx/spinctrl.h" | |
39 | #include "wx/tokenzr.h" | |
40 | #include "wx/renderer.h" | |
41 | ||
42 | #include "wx/generic/gridsel.h" | |
43 | ||
44 | const wxChar wxGridNameStr[] = wxT("grid"); | |
45 | ||
46 | #if defined(__WXMOTIF__) | |
47 | #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier) | |
48 | #else | |
49 | #define WXUNUSED_MOTIF(identifier) identifier | |
50 | #endif | |
51 | ||
52 | #if defined(__WXGTK__) | |
53 | #define WXUNUSED_GTK(identifier) WXUNUSED(identifier) | |
54 | #else | |
55 | #define WXUNUSED_GTK(identifier) identifier | |
56 | #endif | |
57 | ||
58 | // Required for wxIs... functions | |
59 | #include <ctype.h> | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // array classes | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr *, wxArrayAttrs, | |
66 | class WXDLLIMPEXP_ADV); | |
67 | ||
68 | struct wxGridCellWithAttr | |
69 | { | |
70 | wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_) | |
71 | : coords(row, col), attr(attr_) | |
72 | { | |
73 | } | |
74 | ||
75 | ~wxGridCellWithAttr() | |
76 | { | |
77 | attr->DecRef(); | |
78 | } | |
79 | ||
80 | wxGridCellCoords coords; | |
81 | wxGridCellAttr *attr; | |
82 | ||
83 | // Cannot do this: | |
84 | // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr) | |
85 | // without rewriting the macros, which require a public copy constructor. | |
86 | }; | |
87 | ||
88 | WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr, wxGridCellWithAttrArray, | |
89 | class WXDLLIMPEXP_ADV); | |
90 | ||
91 | #include "wx/arrimpl.cpp" | |
92 | ||
93 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) | |
94 | WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray) | |
95 | ||
96 | // ---------------------------------------------------------------------------- | |
97 | // events | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
100 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK) | |
101 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK) | |
102 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK) | |
103 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK) | |
104 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG) | |
105 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK) | |
106 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK) | |
107 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK) | |
108 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK) | |
109 | DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE) | |
110 | DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE) | |
111 | DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE) | |
112 | DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT) | |
113 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE) | |
114 | DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL) | |
115 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN) | |
116 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN) | |
117 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED) | |
118 | ||
119 | // ---------------------------------------------------------------------------- | |
120 | // private classes | |
121 | // ---------------------------------------------------------------------------- | |
122 | ||
123 | class WXDLLIMPEXP_ADV wxGridRowLabelWindow : public wxWindow | |
124 | { | |
125 | public: | |
126 | wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; } | |
127 | wxGridRowLabelWindow( wxGrid *parent, wxWindowID id, | |
128 | const wxPoint &pos, const wxSize &size ); | |
129 | ||
130 | private: | |
131 | wxGrid *m_owner; | |
132 | ||
133 | void OnPaint( wxPaintEvent& event ); | |
134 | void OnMouseEvent( wxMouseEvent& event ); | |
135 | void OnMouseWheel( wxMouseEvent& event ); | |
136 | void OnKeyDown( wxKeyEvent& event ); | |
137 | void OnKeyUp( wxKeyEvent& ); | |
138 | void OnChar( wxKeyEvent& ); | |
139 | ||
140 | DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow) | |
141 | DECLARE_EVENT_TABLE() | |
142 | DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow) | |
143 | }; | |
144 | ||
145 | ||
146 | class WXDLLIMPEXP_ADV wxGridColLabelWindow : public wxWindow | |
147 | { | |
148 | public: | |
149 | wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; } | |
150 | wxGridColLabelWindow( wxGrid *parent, wxWindowID id, | |
151 | const wxPoint &pos, const wxSize &size ); | |
152 | ||
153 | private: | |
154 | wxGrid *m_owner; | |
155 | ||
156 | void OnPaint( wxPaintEvent& event ); | |
157 | void OnMouseEvent( wxMouseEvent& event ); | |
158 | void OnMouseWheel( wxMouseEvent& event ); | |
159 | void OnKeyDown( wxKeyEvent& event ); | |
160 | void OnKeyUp( wxKeyEvent& ); | |
161 | void OnChar( wxKeyEvent& ); | |
162 | ||
163 | DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow) | |
164 | DECLARE_EVENT_TABLE() | |
165 | DECLARE_NO_COPY_CLASS(wxGridColLabelWindow) | |
166 | }; | |
167 | ||
168 | ||
169 | class WXDLLIMPEXP_ADV wxGridCornerLabelWindow : public wxWindow | |
170 | { | |
171 | public: | |
172 | wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; } | |
173 | wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id, | |
174 | const wxPoint &pos, const wxSize &size ); | |
175 | ||
176 | private: | |
177 | wxGrid *m_owner; | |
178 | ||
179 | void OnMouseEvent( wxMouseEvent& event ); | |
180 | void OnMouseWheel( wxMouseEvent& event ); | |
181 | void OnKeyDown( wxKeyEvent& event ); | |
182 | void OnKeyUp( wxKeyEvent& ); | |
183 | void OnChar( wxKeyEvent& ); | |
184 | void OnPaint( wxPaintEvent& event ); | |
185 | ||
186 | DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow) | |
187 | DECLARE_EVENT_TABLE() | |
188 | DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow) | |
189 | }; | |
190 | ||
191 | class WXDLLIMPEXP_ADV wxGridWindow : public wxWindow | |
192 | { | |
193 | public: | |
194 | wxGridWindow() | |
195 | { | |
196 | m_owner = NULL; | |
197 | m_rowLabelWin = NULL; | |
198 | m_colLabelWin = NULL; | |
199 | } | |
200 | ||
201 | wxGridWindow( wxGrid *parent, | |
202 | wxGridRowLabelWindow *rowLblWin, | |
203 | wxGridColLabelWindow *colLblWin, | |
204 | wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
205 | virtual ~wxGridWindow() {} | |
206 | ||
207 | void ScrollWindow( int dx, int dy, const wxRect *rect ); | |
208 | ||
209 | wxGrid* GetOwner() { return m_owner; } | |
210 | ||
211 | private: | |
212 | wxGrid *m_owner; | |
213 | wxGridRowLabelWindow *m_rowLabelWin; | |
214 | wxGridColLabelWindow *m_colLabelWin; | |
215 | ||
216 | void OnPaint( wxPaintEvent &event ); | |
217 | void OnMouseWheel( wxMouseEvent& event ); | |
218 | void OnMouseEvent( wxMouseEvent& event ); | |
219 | void OnKeyDown( wxKeyEvent& ); | |
220 | void OnKeyUp( wxKeyEvent& ); | |
221 | void OnChar( wxKeyEvent& ); | |
222 | void OnEraseBackground( wxEraseEvent& ); | |
223 | void OnFocus( wxFocusEvent& ); | |
224 | ||
225 | DECLARE_DYNAMIC_CLASS(wxGridWindow) | |
226 | DECLARE_EVENT_TABLE() | |
227 | DECLARE_NO_COPY_CLASS(wxGridWindow) | |
228 | }; | |
229 | ||
230 | ||
231 | class wxGridCellEditorEvtHandler : public wxEvtHandler | |
232 | { | |
233 | public: | |
234 | wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor) | |
235 | : m_grid(grid), | |
236 | m_editor(editor), | |
237 | m_inSetFocus(false) | |
238 | { | |
239 | } | |
240 | ||
241 | void OnKillFocus(wxFocusEvent& event); | |
242 | void OnKeyDown(wxKeyEvent& event); | |
243 | void OnChar(wxKeyEvent& event); | |
244 | ||
245 | void SetInSetFocus(bool inSetFocus) { m_inSetFocus = inSetFocus; } | |
246 | ||
247 | private: | |
248 | wxGrid *m_grid; | |
249 | wxGridCellEditor *m_editor; | |
250 | ||
251 | // Work around the fact that a focus kill event can be sent to | |
252 | // a combobox within a set focus event. | |
253 | bool m_inSetFocus; | |
254 | ||
255 | DECLARE_EVENT_TABLE() | |
256 | DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler) | |
257 | DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler) | |
258 | }; | |
259 | ||
260 | ||
261 | IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler, wxEvtHandler) | |
262 | ||
263 | BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler ) | |
264 | EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus ) | |
265 | EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown ) | |
266 | EVT_CHAR( wxGridCellEditorEvtHandler::OnChar ) | |
267 | END_EVENT_TABLE() | |
268 | ||
269 | ||
270 | // ---------------------------------------------------------------------------- | |
271 | // the internal data representation used by wxGridCellAttrProvider | |
272 | // ---------------------------------------------------------------------------- | |
273 | ||
274 | // this class stores attributes set for cells | |
275 | class WXDLLIMPEXP_ADV wxGridCellAttrData | |
276 | { | |
277 | public: | |
278 | void SetAttr(wxGridCellAttr *attr, int row, int col); | |
279 | wxGridCellAttr *GetAttr(int row, int col) const; | |
280 | void UpdateAttrRows( size_t pos, int numRows ); | |
281 | void UpdateAttrCols( size_t pos, int numCols ); | |
282 | ||
283 | private: | |
284 | // searches for the attr for given cell, returns wxNOT_FOUND if not found | |
285 | int FindIndex(int row, int col) const; | |
286 | ||
287 | wxGridCellWithAttrArray m_attrs; | |
288 | }; | |
289 | ||
290 | // this class stores attributes set for rows or columns | |
291 | class WXDLLIMPEXP_ADV wxGridRowOrColAttrData | |
292 | { | |
293 | public: | |
294 | // empty ctor to suppress warnings | |
295 | wxGridRowOrColAttrData() {} | |
296 | ~wxGridRowOrColAttrData(); | |
297 | ||
298 | void SetAttr(wxGridCellAttr *attr, int rowOrCol); | |
299 | wxGridCellAttr *GetAttr(int rowOrCol) const; | |
300 | void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ); | |
301 | ||
302 | private: | |
303 | wxArrayInt m_rowsOrCols; | |
304 | wxArrayAttrs m_attrs; | |
305 | }; | |
306 | ||
307 | // NB: this is just a wrapper around 3 objects: one which stores cell | |
308 | // attributes, and 2 others for row/col ones | |
309 | class WXDLLIMPEXP_ADV wxGridCellAttrProviderData | |
310 | { | |
311 | public: | |
312 | wxGridCellAttrData m_cellAttrs; | |
313 | wxGridRowOrColAttrData m_rowAttrs, | |
314 | m_colAttrs; | |
315 | }; | |
316 | ||
317 | ||
318 | // ---------------------------------------------------------------------------- | |
319 | // data structures used for the data type registry | |
320 | // ---------------------------------------------------------------------------- | |
321 | ||
322 | struct wxGridDataTypeInfo | |
323 | { | |
324 | wxGridDataTypeInfo(const wxString& typeName, | |
325 | wxGridCellRenderer* renderer, | |
326 | wxGridCellEditor* editor) | |
327 | : m_typeName(typeName), m_renderer(renderer), m_editor(editor) | |
328 | {} | |
329 | ||
330 | ~wxGridDataTypeInfo() | |
331 | { | |
332 | wxSafeDecRef(m_renderer); | |
333 | wxSafeDecRef(m_editor); | |
334 | } | |
335 | ||
336 | wxString m_typeName; | |
337 | wxGridCellRenderer* m_renderer; | |
338 | wxGridCellEditor* m_editor; | |
339 | ||
340 | DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo) | |
341 | }; | |
342 | ||
343 | ||
344 | WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo*, wxGridDataTypeInfoArray, | |
345 | class WXDLLIMPEXP_ADV); | |
346 | ||
347 | ||
348 | class WXDLLIMPEXP_ADV wxGridTypeRegistry | |
349 | { | |
350 | public: | |
351 | wxGridTypeRegistry() {} | |
352 | ~wxGridTypeRegistry(); | |
353 | ||
354 | void RegisterDataType(const wxString& typeName, | |
355 | wxGridCellRenderer* renderer, | |
356 | wxGridCellEditor* editor); | |
357 | ||
358 | // find one of already registered data types | |
359 | int FindRegisteredDataType(const wxString& typeName); | |
360 | ||
361 | // try to FindRegisteredDataType(), if this fails and typeName is one of | |
362 | // standard typenames, register it and return its index | |
363 | int FindDataType(const wxString& typeName); | |
364 | ||
365 | // try to FindDataType(), if it fails see if it is not one of already | |
366 | // registered data types with some params in which case clone the | |
367 | // registered data type and set params for it | |
368 | int FindOrCloneDataType(const wxString& typeName); | |
369 | ||
370 | wxGridCellRenderer* GetRenderer(int index); | |
371 | wxGridCellEditor* GetEditor(int index); | |
372 | ||
373 | private: | |
374 | wxGridDataTypeInfoArray m_typeinfo; | |
375 | }; | |
376 | ||
377 | ||
378 | // ---------------------------------------------------------------------------- | |
379 | // conditional compilation | |
380 | // ---------------------------------------------------------------------------- | |
381 | ||
382 | #ifndef WXGRID_DRAW_LINES | |
383 | #define WXGRID_DRAW_LINES 1 | |
384 | #endif | |
385 | ||
386 | // ---------------------------------------------------------------------------- | |
387 | // globals | |
388 | // ---------------------------------------------------------------------------- | |
389 | ||
390 | //#define DEBUG_ATTR_CACHE | |
391 | #ifdef DEBUG_ATTR_CACHE | |
392 | static size_t gs_nAttrCacheHits = 0; | |
393 | static size_t gs_nAttrCacheMisses = 0; | |
394 | #endif | |
395 | ||
396 | // ---------------------------------------------------------------------------- | |
397 | // constants | |
398 | // ---------------------------------------------------------------------------- | |
399 | ||
400 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); | |
401 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); | |
402 | ||
403 | // scroll line size | |
404 | // TODO: this doesn't work at all, grid cells have different sizes and approx | |
405 | // calculations don't work as because of the size mismatch scrollbars | |
406 | // sometimes fail to be shown when they should be or vice versa | |
407 | // | |
408 | // The scroll bars may be a little flakey once in a while, but that is | |
409 | // surely much less horrible than having scroll lines of only 1!!! | |
410 | // -- Robin | |
411 | // | |
412 | // Well, it's still seriously broken so it might be better but needs | |
413 | // fixing anyhow | |
414 | // -- Vadim | |
415 | static const size_t GRID_SCROLL_LINE_X = 15; // 1; | |
416 | static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X; | |
417 | ||
418 | // the size of hash tables used a bit everywhere (the max number of elements | |
419 | // in these hash tables is the number of rows/columns) | |
420 | static const int GRID_HASH_SIZE = 100; | |
421 | ||
422 | #if 0 | |
423 | // ---------------------------------------------------------------------------- | |
424 | // private functions | |
425 | // ---------------------------------------------------------------------------- | |
426 | ||
427 | static inline int GetScrollX(int x) | |
428 | { | |
429 | return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X; | |
430 | } | |
431 | ||
432 | static inline int GetScrollY(int y) | |
433 | { | |
434 | return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y; | |
435 | } | |
436 | #endif | |
437 | ||
438 | // ============================================================================ | |
439 | // implementation | |
440 | // ============================================================================ | |
441 | ||
442 | // ---------------------------------------------------------------------------- | |
443 | // wxGridCellEditor | |
444 | // ---------------------------------------------------------------------------- | |
445 | ||
446 | wxGridCellEditor::wxGridCellEditor() | |
447 | { | |
448 | m_control = NULL; | |
449 | m_attr = NULL; | |
450 | } | |
451 | ||
452 | wxGridCellEditor::~wxGridCellEditor() | |
453 | { | |
454 | Destroy(); | |
455 | } | |
456 | ||
457 | void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent), | |
458 | wxWindowID WXUNUSED(id), | |
459 | wxEvtHandler* evtHandler) | |
460 | { | |
461 | if ( evtHandler ) | |
462 | m_control->PushEventHandler(evtHandler); | |
463 | } | |
464 | ||
465 | void wxGridCellEditor::PaintBackground(const wxRect& rectCell, | |
466 | wxGridCellAttr *attr) | |
467 | { | |
468 | // erase the background because we might not fill the cell | |
469 | wxClientDC dc(m_control->GetParent()); | |
470 | wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow); | |
471 | if (gridWindow) | |
472 | gridWindow->GetOwner()->PrepareDC(dc); | |
473 | ||
474 | dc.SetPen(*wxTRANSPARENT_PEN); | |
475 | dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID)); | |
476 | dc.DrawRectangle(rectCell); | |
477 | ||
478 | // redraw the control we just painted over | |
479 | m_control->Refresh(); | |
480 | } | |
481 | ||
482 | void wxGridCellEditor::Destroy() | |
483 | { | |
484 | if (m_control) | |
485 | { | |
486 | m_control->PopEventHandler( true /* delete it*/ ); | |
487 | ||
488 | m_control->Destroy(); | |
489 | m_control = NULL; | |
490 | } | |
491 | } | |
492 | ||
493 | void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr) | |
494 | { | |
495 | wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); | |
496 | ||
497 | m_control->Show(show); | |
498 | ||
499 | if ( show ) | |
500 | { | |
501 | // set the colours/fonts if we have any | |
502 | if ( attr ) | |
503 | { | |
504 | m_colFgOld = m_control->GetForegroundColour(); | |
505 | m_control->SetForegroundColour(attr->GetTextColour()); | |
506 | ||
507 | m_colBgOld = m_control->GetBackgroundColour(); | |
508 | m_control->SetBackgroundColour(attr->GetBackgroundColour()); | |
509 | ||
510 | // Workaround for GTK+1 font setting problem on some platforms | |
511 | #if !defined(__WXGTK__) || defined(__WXGTK20__) | |
512 | m_fontOld = m_control->GetFont(); | |
513 | m_control->SetFont(attr->GetFont()); | |
514 | #endif | |
515 | ||
516 | // can't do anything more in the base class version, the other | |
517 | // attributes may only be used by the derived classes | |
518 | } | |
519 | } | |
520 | else | |
521 | { | |
522 | // restore the standard colours fonts | |
523 | if ( m_colFgOld.Ok() ) | |
524 | { | |
525 | m_control->SetForegroundColour(m_colFgOld); | |
526 | m_colFgOld = wxNullColour; | |
527 | } | |
528 | ||
529 | if ( m_colBgOld.Ok() ) | |
530 | { | |
531 | m_control->SetBackgroundColour(m_colBgOld); | |
532 | m_colBgOld = wxNullColour; | |
533 | } | |
534 | ||
535 | // Workaround for GTK+1 font setting problem on some platforms | |
536 | #if !defined(__WXGTK__) || defined(__WXGTK20__) | |
537 | if ( m_fontOld.Ok() ) | |
538 | { | |
539 | m_control->SetFont(m_fontOld); | |
540 | m_fontOld = wxNullFont; | |
541 | } | |
542 | #endif | |
543 | } | |
544 | } | |
545 | ||
546 | void wxGridCellEditor::SetSize(const wxRect& rect) | |
547 | { | |
548 | wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); | |
549 | ||
550 | m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); | |
551 | } | |
552 | ||
553 | void wxGridCellEditor::HandleReturn(wxKeyEvent& event) | |
554 | { | |
555 | event.Skip(); | |
556 | } | |
557 | ||
558 | bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event) | |
559 | { | |
560 | bool ctrl = event.ControlDown(); | |
561 | bool alt = event.AltDown(); | |
562 | ||
563 | #ifdef __WXMAC__ | |
564 | // On the Mac the Alt key is more like shift and is used for entry of | |
565 | // valid characters, so check for Ctrl and Meta instead. | |
566 | alt = event.MetaDown(); | |
567 | #endif | |
568 | ||
569 | // Assume it's not a valid char if ctrl or alt is down, but if both are | |
570 | // down then it may be because of an AltGr key combination, so let them | |
571 | // through in that case. | |
572 | if ((ctrl || alt) && !(ctrl && alt)) | |
573 | return false; | |
574 | ||
575 | int key = 0; | |
576 | bool keyOk = true; | |
577 | ||
578 | #ifdef __WXGTK20__ | |
579 | // If it's a F-Key or other special key then it shouldn't start the | |
580 | // editor. | |
581 | if (event.GetKeyCode() >= WXK_START) | |
582 | return false; | |
583 | #endif | |
584 | #if wxUSE_UNICODE | |
585 | // if the unicode key code is not really a unicode character (it may | |
586 | // be a function key or etc., the platforms appear to always give us a | |
587 | // small value in this case) then fallback to the ASCII key code but | |
588 | // don't do anything for function keys or etc. | |
589 | key = event.GetUnicodeKey(); | |
590 | if (key <= 127) | |
591 | { | |
592 | key = event.GetKeyCode(); | |
593 | keyOk = (key <= 127); | |
594 | } | |
595 | #else | |
596 | key = event.GetKeyCode(); | |
597 | keyOk = (key <= 255); | |
598 | #endif | |
599 | ||
600 | return keyOk; | |
601 | } | |
602 | ||
603 | void wxGridCellEditor::StartingKey(wxKeyEvent& event) | |
604 | { | |
605 | event.Skip(); | |
606 | } | |
607 | ||
608 | void wxGridCellEditor::StartingClick() | |
609 | { | |
610 | } | |
611 | ||
612 | #if wxUSE_TEXTCTRL | |
613 | ||
614 | // ---------------------------------------------------------------------------- | |
615 | // wxGridCellTextEditor | |
616 | // ---------------------------------------------------------------------------- | |
617 | ||
618 | wxGridCellTextEditor::wxGridCellTextEditor() | |
619 | { | |
620 | m_maxChars = 0; | |
621 | } | |
622 | ||
623 | void wxGridCellTextEditor::Create(wxWindow* parent, | |
624 | wxWindowID id, | |
625 | wxEvtHandler* evtHandler) | |
626 | { | |
627 | m_control = new wxTextCtrl(parent, id, wxEmptyString, | |
628 | wxDefaultPosition, wxDefaultSize | |
629 | #if defined(__WXMSW__) | |
630 | , wxTE_PROCESS_TAB | wxTE_AUTO_SCROLL | wxNO_BORDER | |
631 | #endif | |
632 | ); | |
633 | ||
634 | // set max length allowed in the textctrl, if the parameter was set | |
635 | if (m_maxChars != 0) | |
636 | { | |
637 | ((wxTextCtrl*)m_control)->SetMaxLength(m_maxChars); | |
638 | } | |
639 | ||
640 | wxGridCellEditor::Create(parent, id, evtHandler); | |
641 | } | |
642 | ||
643 | void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell), | |
644 | wxGridCellAttr * WXUNUSED(attr)) | |
645 | { | |
646 | // as we fill the entire client area, | |
647 | // don't do anything here to minimize flicker | |
648 | } | |
649 | ||
650 | void wxGridCellTextEditor::SetSize(const wxRect& rectOrig) | |
651 | { | |
652 | wxRect rect(rectOrig); | |
653 | ||
654 | // Make the edit control large enough to allow for internal margins | |
655 | // | |
656 | // TODO: remove this if the text ctrl sizing is improved esp. for unix | |
657 | // | |
658 | #if defined(__WXGTK__) | |
659 | if (rect.x != 0) | |
660 | { | |
661 | rect.x += 1; | |
662 | rect.y += 1; | |
663 | rect.width -= 1; | |
664 | rect.height -= 1; | |
665 | } | |
666 | #elif defined(__WXMSW__) | |
667 | if ( rect.x == 0 ) | |
668 | rect.x += 2; | |
669 | else | |
670 | rect.x += 3; | |
671 | ||
672 | if ( rect.y == 0 ) | |
673 | rect.y += 2; | |
674 | else | |
675 | rect.y += 3; | |
676 | ||
677 | rect.width -= 2; | |
678 | rect.height -= 2; | |
679 | #else | |
680 | int extra_x = ( rect.x > 2 ) ? 2 : 1; | |
681 | int extra_y = ( rect.y > 2 ) ? 2 : 1; | |
682 | ||
683 | #if defined(__WXMOTIF__) | |
684 | extra_x *= 2; | |
685 | extra_y *= 2; | |
686 | #endif | |
687 | ||
688 | rect.SetLeft( wxMax(0, rect.x - extra_x) ); | |
689 | rect.SetTop( wxMax(0, rect.y - extra_y) ); | |
690 | rect.SetRight( rect.GetRight() + 2 * extra_x ); | |
691 | rect.SetBottom( rect.GetBottom() + 2 * extra_y ); | |
692 | #endif | |
693 | ||
694 | wxGridCellEditor::SetSize(rect); | |
695 | } | |
696 | ||
697 | void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid) | |
698 | { | |
699 | wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); | |
700 | ||
701 | m_startValue = grid->GetTable()->GetValue(row, col); | |
702 | ||
703 | DoBeginEdit(m_startValue); | |
704 | } | |
705 | ||
706 | void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue) | |
707 | { | |
708 | Text()->SetValue(startValue); | |
709 | Text()->SetInsertionPointEnd(); | |
710 | Text()->SetSelection(-1, -1); | |
711 | Text()->SetFocus(); | |
712 | } | |
713 | ||
714 | bool wxGridCellTextEditor::EndEdit(int row, int col, wxGrid* grid) | |
715 | { | |
716 | wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); | |
717 | ||
718 | bool changed = false; | |
719 | wxString value = Text()->GetValue(); | |
720 | if (value != m_startValue) | |
721 | changed = true; | |
722 | ||
723 | if (changed) | |
724 | grid->GetTable()->SetValue(row, col, value); | |
725 | ||
726 | m_startValue = wxEmptyString; | |
727 | ||
728 | // No point in setting the text of the hidden control | |
729 | //Text()->SetValue(m_startValue); | |
730 | ||
731 | return changed; | |
732 | } | |
733 | ||
734 | void wxGridCellTextEditor::Reset() | |
735 | { | |
736 | wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); | |
737 | ||
738 | DoReset(m_startValue); | |
739 | } | |
740 | ||
741 | void wxGridCellTextEditor::DoReset(const wxString& startValue) | |
742 | { | |
743 | Text()->SetValue(startValue); | |
744 | Text()->SetInsertionPointEnd(); | |
745 | } | |
746 | ||
747 | bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event) | |
748 | { | |
749 | return wxGridCellEditor::IsAcceptedKey(event); | |
750 | } | |
751 | ||
752 | void wxGridCellTextEditor::StartingKey(wxKeyEvent& event) | |
753 | { | |
754 | // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no | |
755 | // longer an appropriate way to get the character into the text control. | |
756 | // Do it ourselves instead. We know that if we get this far that we have | |
757 | // a valid character, so not a whole lot of testing needs to be done. | |
758 | ||
759 | wxTextCtrl* tc = Text(); | |
760 | wxChar ch; | |
761 | long pos; | |
762 | ||
763 | #if wxUSE_UNICODE | |
764 | ch = event.GetUnicodeKey(); | |
765 | if (ch <= 127) | |
766 | ch = (wxChar)event.GetKeyCode(); | |
767 | #else | |
768 | ch = (wxChar)event.GetKeyCode(); | |
769 | #endif | |
770 | ||
771 | switch (ch) | |
772 | { | |
773 | case WXK_DELETE: | |
774 | // delete the character at the cursor | |
775 | pos = tc->GetInsertionPoint(); | |
776 | if (pos < tc->GetLastPosition()) | |
777 | tc->Remove(pos, pos + 1); | |
778 | break; | |
779 | ||
780 | case WXK_BACK: | |
781 | // delete the character before the cursor | |
782 | pos = tc->GetInsertionPoint(); | |
783 | if (pos > 0) | |
784 | tc->Remove(pos - 1, pos); | |
785 | break; | |
786 | ||
787 | default: | |
788 | tc->WriteText(ch); | |
789 | break; | |
790 | } | |
791 | } | |
792 | ||
793 | void wxGridCellTextEditor::HandleReturn( wxKeyEvent& | |
794 | WXUNUSED_GTK(WXUNUSED_MOTIF(event)) ) | |
795 | { | |
796 | #if defined(__WXMOTIF__) || defined(__WXGTK__) | |
797 | // wxMotif needs a little extra help... | |
798 | size_t pos = (size_t)( Text()->GetInsertionPoint() ); | |
799 | wxString s( Text()->GetValue() ); | |
800 | s = s.Left(pos) + wxT("\n") + s.Mid(pos); | |
801 | Text()->SetValue(s); | |
802 | Text()->SetInsertionPoint( pos ); | |
803 | #else | |
804 | // the other ports can handle a Return key press | |
805 | // | |
806 | event.Skip(); | |
807 | #endif | |
808 | } | |
809 | ||
810 | void wxGridCellTextEditor::SetParameters(const wxString& params) | |
811 | { | |
812 | if ( !params ) | |
813 | { | |
814 | // reset to default | |
815 | m_maxChars = 0; | |
816 | } | |
817 | else | |
818 | { | |
819 | long tmp; | |
820 | if ( params.ToLong(&tmp) ) | |
821 | { | |
822 | m_maxChars = (size_t)tmp; | |
823 | } | |
824 | else | |
825 | { | |
826 | wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str() ); | |
827 | } | |
828 | } | |
829 | } | |
830 | ||
831 | // return the value in the text control | |
832 | wxString wxGridCellTextEditor::GetValue() const | |
833 | { | |
834 | return Text()->GetValue(); | |
835 | } | |
836 | ||
837 | // ---------------------------------------------------------------------------- | |
838 | // wxGridCellNumberEditor | |
839 | // ---------------------------------------------------------------------------- | |
840 | ||
841 | wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max) | |
842 | { | |
843 | m_min = min; | |
844 | m_max = max; | |
845 | } | |
846 | ||
847 | void wxGridCellNumberEditor::Create(wxWindow* parent, | |
848 | wxWindowID id, | |
849 | wxEvtHandler* evtHandler) | |
850 | { | |
851 | #if wxUSE_SPINCTRL | |
852 | if ( HasRange() ) | |
853 | { | |
854 | // create a spin ctrl | |
855 | m_control = new wxSpinCtrl(parent, wxID_ANY, wxEmptyString, | |
856 | wxDefaultPosition, wxDefaultSize, | |
857 | wxSP_ARROW_KEYS, | |
858 | m_min, m_max); | |
859 | ||
860 | wxGridCellEditor::Create(parent, id, evtHandler); | |
861 | } | |
862 | else | |
863 | #endif | |
864 | { | |
865 | // just a text control | |
866 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
867 | ||
868 | #if wxUSE_VALIDATORS | |
869 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); | |
870 | #endif | |
871 | } | |
872 | } | |
873 | ||
874 | void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid) | |
875 | { | |
876 | // first get the value | |
877 | wxGridTableBase *table = grid->GetTable(); | |
878 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
879 | { | |
880 | m_valueOld = table->GetValueAsLong(row, col); | |
881 | } | |
882 | else | |
883 | { | |
884 | m_valueOld = 0; | |
885 | wxString sValue = table->GetValue(row, col); | |
886 | if (! sValue.ToLong(&m_valueOld) && ! sValue.empty()) | |
887 | { | |
888 | wxFAIL_MSG( _T("this cell doesn't have numeric value") ); | |
889 | return; | |
890 | } | |
891 | } | |
892 | ||
893 | #if wxUSE_SPINCTRL | |
894 | if ( HasRange() ) | |
895 | { | |
896 | Spin()->SetValue((int)m_valueOld); | |
897 | Spin()->SetFocus(); | |
898 | } | |
899 | else | |
900 | #endif | |
901 | { | |
902 | DoBeginEdit(GetString()); | |
903 | } | |
904 | } | |
905 | ||
906 | bool wxGridCellNumberEditor::EndEdit(int row, int col, | |
907 | wxGrid* grid) | |
908 | { | |
909 | bool changed; | |
910 | long value = 0; | |
911 | wxString text; | |
912 | ||
913 | #if wxUSE_SPINCTRL | |
914 | if ( HasRange() ) | |
915 | { | |
916 | value = Spin()->GetValue(); | |
917 | changed = value != m_valueOld; | |
918 | if (changed) | |
919 | text = wxString::Format(wxT("%ld"), value); | |
920 | } | |
921 | else | |
922 | #endif | |
923 | { | |
924 | text = Text()->GetValue(); | |
925 | changed = (text.empty() || text.ToLong(&value)) && (value != m_valueOld); | |
926 | } | |
927 | ||
928 | if ( changed ) | |
929 | { | |
930 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER)) | |
931 | grid->GetTable()->SetValueAsLong(row, col, value); | |
932 | else | |
933 | grid->GetTable()->SetValue(row, col, text); | |
934 | } | |
935 | ||
936 | return changed; | |
937 | } | |
938 | ||
939 | void wxGridCellNumberEditor::Reset() | |
940 | { | |
941 | #if wxUSE_SPINCTRL | |
942 | if ( HasRange() ) | |
943 | { | |
944 | Spin()->SetValue((int)m_valueOld); | |
945 | } | |
946 | else | |
947 | #endif | |
948 | { | |
949 | DoReset(GetString()); | |
950 | } | |
951 | } | |
952 | ||
953 | bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event) | |
954 | { | |
955 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
956 | { | |
957 | int keycode = event.GetKeyCode(); | |
958 | if ( (keycode < 128) && | |
959 | (wxIsdigit(keycode) || keycode == '+' || keycode == '-')) | |
960 | { | |
961 | return true; | |
962 | } | |
963 | } | |
964 | ||
965 | return false; | |
966 | } | |
967 | ||
968 | void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event) | |
969 | { | |
970 | int keycode = event.GetKeyCode(); | |
971 | if ( !HasRange() ) | |
972 | { | |
973 | if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-') | |
974 | { | |
975 | wxGridCellTextEditor::StartingKey(event); | |
976 | ||
977 | // skip Skip() below | |
978 | return; | |
979 | } | |
980 | } | |
981 | #if wxUSE_SPINCTRL | |
982 | else | |
983 | { | |
984 | if ( wxIsdigit(keycode) ) | |
985 | { | |
986 | wxSpinCtrl* spin = (wxSpinCtrl*)m_control; | |
987 | spin->SetValue(keycode - '0'); | |
988 | spin->SetSelection(1,1); | |
989 | return; | |
990 | } | |
991 | } | |
992 | #endif | |
993 | ||
994 | event.Skip(); | |
995 | } | |
996 | ||
997 | void wxGridCellNumberEditor::SetParameters(const wxString& params) | |
998 | { | |
999 | if ( !params ) | |
1000 | { | |
1001 | // reset to default | |
1002 | m_min = | |
1003 | m_max = -1; | |
1004 | } | |
1005 | else | |
1006 | { | |
1007 | long tmp; | |
1008 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
1009 | { | |
1010 | m_min = (int)tmp; | |
1011 | ||
1012 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
1013 | { | |
1014 | m_max = (int)tmp; | |
1015 | ||
1016 | // skip the error message below | |
1017 | return; | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str()); | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | // return the value in the spin control if it is there (the text control otherwise) | |
1026 | wxString wxGridCellNumberEditor::GetValue() const | |
1027 | { | |
1028 | wxString s; | |
1029 | ||
1030 | #if wxUSE_SPINCTRL | |
1031 | if ( HasRange() ) | |
1032 | { | |
1033 | long value = Spin()->GetValue(); | |
1034 | s.Printf(wxT("%ld"), value); | |
1035 | } | |
1036 | else | |
1037 | #endif | |
1038 | { | |
1039 | s = Text()->GetValue(); | |
1040 | } | |
1041 | ||
1042 | return s; | |
1043 | } | |
1044 | ||
1045 | // ---------------------------------------------------------------------------- | |
1046 | // wxGridCellFloatEditor | |
1047 | // ---------------------------------------------------------------------------- | |
1048 | ||
1049 | wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision) | |
1050 | { | |
1051 | m_width = width; | |
1052 | m_precision = precision; | |
1053 | } | |
1054 | ||
1055 | void wxGridCellFloatEditor::Create(wxWindow* parent, | |
1056 | wxWindowID id, | |
1057 | wxEvtHandler* evtHandler) | |
1058 | { | |
1059 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
1060 | ||
1061 | #if wxUSE_VALIDATORS | |
1062 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); | |
1063 | #endif | |
1064 | } | |
1065 | ||
1066 | void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1067 | { | |
1068 | // first get the value | |
1069 | wxGridTableBase *table = grid->GetTable(); | |
1070 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
1071 | { | |
1072 | m_valueOld = table->GetValueAsDouble(row, col); | |
1073 | } | |
1074 | else | |
1075 | { | |
1076 | m_valueOld = 0.0; | |
1077 | wxString sValue = table->GetValue(row, col); | |
1078 | if (! sValue.ToDouble(&m_valueOld) && ! sValue.empty()) | |
1079 | { | |
1080 | wxFAIL_MSG( _T("this cell doesn't have float value") ); | |
1081 | return; | |
1082 | } | |
1083 | } | |
1084 | ||
1085 | DoBeginEdit(GetString()); | |
1086 | } | |
1087 | ||
1088 | bool wxGridCellFloatEditor::EndEdit(int row, int col, | |
1089 | wxGrid* grid) | |
1090 | { | |
1091 | double value = 0.0; | |
1092 | wxString text(Text()->GetValue()); | |
1093 | ||
1094 | if ( (text.empty() || text.ToDouble(&value)) && | |
1095 | !wxIsSameDouble(value, m_valueOld) ) | |
1096 | { | |
1097 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT)) | |
1098 | grid->GetTable()->SetValueAsDouble(row, col, value); | |
1099 | else | |
1100 | grid->GetTable()->SetValue(row, col, text); | |
1101 | ||
1102 | return true; | |
1103 | } | |
1104 | ||
1105 | return false; | |
1106 | } | |
1107 | ||
1108 | void wxGridCellFloatEditor::Reset() | |
1109 | { | |
1110 | DoReset(GetString()); | |
1111 | } | |
1112 | ||
1113 | void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event) | |
1114 | { | |
1115 | int keycode = event.GetKeyCode(); | |
1116 | char tmpbuf[2]; | |
1117 | tmpbuf[0] = (char) keycode; | |
1118 | tmpbuf[1] = '\0'; | |
1119 | wxString strbuf(tmpbuf, *wxConvCurrent); | |
1120 | ||
1121 | #if wxUSE_INTL | |
1122 | bool is_decimal_point = ( strbuf == | |
1123 | wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER) ); | |
1124 | #else | |
1125 | bool is_decimal_point = ( strbuf == _T(".") ); | |
1126 | #endif | |
1127 | ||
1128 | if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-' | |
1129 | || is_decimal_point ) | |
1130 | { | |
1131 | wxGridCellTextEditor::StartingKey(event); | |
1132 | ||
1133 | // skip Skip() below | |
1134 | return; | |
1135 | } | |
1136 | ||
1137 | event.Skip(); | |
1138 | } | |
1139 | ||
1140 | void wxGridCellFloatEditor::SetParameters(const wxString& params) | |
1141 | { | |
1142 | if ( !params ) | |
1143 | { | |
1144 | // reset to default | |
1145 | m_width = | |
1146 | m_precision = -1; | |
1147 | } | |
1148 | else | |
1149 | { | |
1150 | long tmp; | |
1151 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
1152 | { | |
1153 | m_width = (int)tmp; | |
1154 | ||
1155 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
1156 | { | |
1157 | m_precision = (int)tmp; | |
1158 | ||
1159 | // skip the error message below | |
1160 | return; | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str()); | |
1165 | } | |
1166 | } | |
1167 | ||
1168 | wxString wxGridCellFloatEditor::GetString() const | |
1169 | { | |
1170 | wxString fmt; | |
1171 | if ( m_precision == -1 && m_width != -1) | |
1172 | { | |
1173 | // default precision | |
1174 | fmt.Printf(_T("%%%d.f"), m_width); | |
1175 | } | |
1176 | else if ( m_precision != -1 && m_width == -1) | |
1177 | { | |
1178 | // default width | |
1179 | fmt.Printf(_T("%%.%df"), m_precision); | |
1180 | } | |
1181 | else if ( m_precision != -1 && m_width != -1 ) | |
1182 | { | |
1183 | fmt.Printf(_T("%%%d.%df"), m_width, m_precision); | |
1184 | } | |
1185 | else | |
1186 | { | |
1187 | // default width/precision | |
1188 | fmt = _T("%f"); | |
1189 | } | |
1190 | ||
1191 | return wxString::Format(fmt, m_valueOld); | |
1192 | } | |
1193 | ||
1194 | bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event) | |
1195 | { | |
1196 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
1197 | { | |
1198 | const int keycode = event.GetKeyCode(); | |
1199 | if ( isascii(keycode) ) | |
1200 | { | |
1201 | char tmpbuf[2]; | |
1202 | tmpbuf[0] = (char) keycode; | |
1203 | tmpbuf[1] = '\0'; | |
1204 | wxString strbuf(tmpbuf, *wxConvCurrent); | |
1205 | ||
1206 | #if wxUSE_INTL | |
1207 | const wxString decimalPoint = | |
1208 | wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER); | |
1209 | #else | |
1210 | const wxString decimalPoint(_T('.')); | |
1211 | #endif | |
1212 | ||
1213 | // accept digits, 'e' as in '1e+6', also '-', '+', and '.' | |
1214 | if ( wxIsdigit(keycode) || | |
1215 | tolower(keycode) == 'e' || | |
1216 | keycode == decimalPoint || | |
1217 | keycode == '+' || | |
1218 | keycode == '-' ) | |
1219 | { | |
1220 | return true; | |
1221 | } | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | return false; | |
1226 | } | |
1227 | ||
1228 | #endif // wxUSE_TEXTCTRL | |
1229 | ||
1230 | #if wxUSE_CHECKBOX | |
1231 | ||
1232 | // ---------------------------------------------------------------------------- | |
1233 | // wxGridCellBoolEditor | |
1234 | // ---------------------------------------------------------------------------- | |
1235 | ||
1236 | void wxGridCellBoolEditor::Create(wxWindow* parent, | |
1237 | wxWindowID id, | |
1238 | wxEvtHandler* evtHandler) | |
1239 | { | |
1240 | m_control = new wxCheckBox(parent, id, wxEmptyString, | |
1241 | wxDefaultPosition, wxDefaultSize, | |
1242 | wxNO_BORDER); | |
1243 | ||
1244 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1245 | } | |
1246 | ||
1247 | void wxGridCellBoolEditor::SetSize(const wxRect& r) | |
1248 | { | |
1249 | bool resize = false; | |
1250 | wxSize size = m_control->GetSize(); | |
1251 | wxCoord minSize = wxMin(r.width, r.height); | |
1252 | ||
1253 | // check if the checkbox is not too big/small for this cell | |
1254 | wxSize sizeBest = m_control->GetBestSize(); | |
1255 | if ( !(size == sizeBest) ) | |
1256 | { | |
1257 | // reset to default size if it had been made smaller | |
1258 | size = sizeBest; | |
1259 | ||
1260 | resize = true; | |
1261 | } | |
1262 | ||
1263 | if ( size.x >= minSize || size.y >= minSize ) | |
1264 | { | |
1265 | // leave 1 pixel margin | |
1266 | size.x = size.y = minSize - 2; | |
1267 | ||
1268 | resize = true; | |
1269 | } | |
1270 | ||
1271 | if ( resize ) | |
1272 | { | |
1273 | m_control->SetSize(size); | |
1274 | } | |
1275 | ||
1276 | // position it in the centre of the rectangle (TODO: support alignment?) | |
1277 | ||
1278 | #if defined(__WXGTK__) || defined (__WXMOTIF__) | |
1279 | // the checkbox without label still has some space to the right in wxGTK, | |
1280 | // so shift it to the right | |
1281 | size.x -= 8; | |
1282 | #elif defined(__WXMSW__) | |
1283 | // here too, but in other way | |
1284 | size.x += 1; | |
1285 | size.y -= 2; | |
1286 | #endif | |
1287 | ||
1288 | int hAlign = wxALIGN_CENTRE; | |
1289 | int vAlign = wxALIGN_CENTRE; | |
1290 | if (GetCellAttr()) | |
1291 | GetCellAttr()->GetAlignment(& hAlign, & vAlign); | |
1292 | ||
1293 | int x = 0, y = 0; | |
1294 | if (hAlign == wxALIGN_LEFT) | |
1295 | { | |
1296 | x = r.x + 2; | |
1297 | ||
1298 | #ifdef __WXMSW__ | |
1299 | x += 2; | |
1300 | #endif | |
1301 | ||
1302 | y = r.y + r.height / 2 - size.y / 2; | |
1303 | } | |
1304 | else if (hAlign == wxALIGN_RIGHT) | |
1305 | { | |
1306 | x = r.x + r.width - size.x - 2; | |
1307 | y = r.y + r.height / 2 - size.y / 2; | |
1308 | } | |
1309 | else if (hAlign == wxALIGN_CENTRE) | |
1310 | { | |
1311 | x = r.x + r.width / 2 - size.x / 2; | |
1312 | y = r.y + r.height / 2 - size.y / 2; | |
1313 | } | |
1314 | ||
1315 | m_control->Move(x, y); | |
1316 | } | |
1317 | ||
1318 | void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr) | |
1319 | { | |
1320 | m_control->Show(show); | |
1321 | ||
1322 | if ( show ) | |
1323 | { | |
1324 | wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY; | |
1325 | CBox()->SetBackgroundColour(colBg); | |
1326 | } | |
1327 | } | |
1328 | ||
1329 | void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1330 | { | |
1331 | wxASSERT_MSG(m_control, | |
1332 | wxT("The wxGridCellEditor must be created first!")); | |
1333 | ||
1334 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) | |
1335 | { | |
1336 | m_startValue = grid->GetTable()->GetValueAsBool(row, col); | |
1337 | } | |
1338 | else | |
1339 | { | |
1340 | wxString cellval( grid->GetTable()->GetValue(row, col) ); | |
1341 | m_startValue = !( !cellval || (cellval == wxT("0")) ); | |
1342 | } | |
1343 | ||
1344 | CBox()->SetValue(m_startValue); | |
1345 | CBox()->SetFocus(); | |
1346 | } | |
1347 | ||
1348 | bool wxGridCellBoolEditor::EndEdit(int row, int col, | |
1349 | wxGrid* grid) | |
1350 | { | |
1351 | wxASSERT_MSG(m_control, | |
1352 | wxT("The wxGridCellEditor must be created first!")); | |
1353 | ||
1354 | bool changed = false; | |
1355 | bool value = CBox()->GetValue(); | |
1356 | if ( value != m_startValue ) | |
1357 | changed = true; | |
1358 | ||
1359 | if ( changed ) | |
1360 | { | |
1361 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) | |
1362 | grid->GetTable()->SetValueAsBool(row, col, value); | |
1363 | else | |
1364 | grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString); | |
1365 | } | |
1366 | ||
1367 | return changed; | |
1368 | } | |
1369 | ||
1370 | void wxGridCellBoolEditor::Reset() | |
1371 | { | |
1372 | wxASSERT_MSG(m_control, | |
1373 | wxT("The wxGridCellEditor must be created first!")); | |
1374 | ||
1375 | CBox()->SetValue(m_startValue); | |
1376 | } | |
1377 | ||
1378 | void wxGridCellBoolEditor::StartingClick() | |
1379 | { | |
1380 | CBox()->SetValue(!CBox()->GetValue()); | |
1381 | } | |
1382 | ||
1383 | bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event) | |
1384 | { | |
1385 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
1386 | { | |
1387 | int keycode = event.GetKeyCode(); | |
1388 | switch ( keycode ) | |
1389 | { | |
1390 | case WXK_SPACE: | |
1391 | case '+': | |
1392 | case '-': | |
1393 | return true; | |
1394 | } | |
1395 | } | |
1396 | ||
1397 | return false; | |
1398 | } | |
1399 | ||
1400 | void wxGridCellBoolEditor::StartingKey(wxKeyEvent& event) | |
1401 | { | |
1402 | int keycode = event.GetKeyCode(); | |
1403 | switch ( keycode ) | |
1404 | { | |
1405 | case WXK_SPACE: | |
1406 | CBox()->SetValue(!CBox()->GetValue()); | |
1407 | break; | |
1408 | ||
1409 | case '+': | |
1410 | CBox()->SetValue(true); | |
1411 | break; | |
1412 | ||
1413 | case '-': | |
1414 | CBox()->SetValue(false); | |
1415 | break; | |
1416 | } | |
1417 | } | |
1418 | ||
1419 | ||
1420 | // return the value as "1" for true and the empty string for false | |
1421 | wxString wxGridCellBoolEditor::GetValue() const | |
1422 | { | |
1423 | bool bSet = CBox()->GetValue(); | |
1424 | return bSet ? _T("1") : wxEmptyString; | |
1425 | } | |
1426 | ||
1427 | #endif // wxUSE_CHECKBOX | |
1428 | ||
1429 | #if wxUSE_COMBOBOX | |
1430 | ||
1431 | // ---------------------------------------------------------------------------- | |
1432 | // wxGridCellChoiceEditor | |
1433 | // ---------------------------------------------------------------------------- | |
1434 | ||
1435 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString& choices, | |
1436 | bool allowOthers) | |
1437 | : m_choices(choices), | |
1438 | m_allowOthers(allowOthers) { } | |
1439 | ||
1440 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count, | |
1441 | const wxString choices[], | |
1442 | bool allowOthers) | |
1443 | : m_allowOthers(allowOthers) | |
1444 | { | |
1445 | if ( count ) | |
1446 | { | |
1447 | m_choices.Alloc(count); | |
1448 | for ( size_t n = 0; n < count; n++ ) | |
1449 | { | |
1450 | m_choices.Add(choices[n]); | |
1451 | } | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | wxGridCellEditor *wxGridCellChoiceEditor::Clone() const | |
1456 | { | |
1457 | wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor; | |
1458 | editor->m_allowOthers = m_allowOthers; | |
1459 | editor->m_choices = m_choices; | |
1460 | ||
1461 | return editor; | |
1462 | } | |
1463 | ||
1464 | void wxGridCellChoiceEditor::Create(wxWindow* parent, | |
1465 | wxWindowID id, | |
1466 | wxEvtHandler* evtHandler) | |
1467 | { | |
1468 | m_control = new wxComboBox(parent, id, wxEmptyString, | |
1469 | wxDefaultPosition, wxDefaultSize, | |
1470 | m_choices, | |
1471 | m_allowOthers ? 0 : wxCB_READONLY); | |
1472 | ||
1473 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1474 | } | |
1475 | ||
1476 | void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell, | |
1477 | wxGridCellAttr * attr) | |
1478 | { | |
1479 | // as we fill the entire client area, don't do anything here to minimize | |
1480 | // flicker | |
1481 | ||
1482 | // TODO: It doesn't actually fill the client area since the height of a | |
1483 | // combo always defaults to the standard. Until someone has time to | |
1484 | // figure out the right rectangle to paint, just do it the normal way. | |
1485 | wxGridCellEditor::PaintBackground(rectCell, attr); | |
1486 | } | |
1487 | ||
1488 | void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1489 | { | |
1490 | wxASSERT_MSG(m_control, | |
1491 | wxT("The wxGridCellEditor must be created first!")); | |
1492 | ||
1493 | wxGridCellEditorEvtHandler* evtHandler = NULL; | |
1494 | if (m_control) | |
1495 | evtHandler = wxDynamicCast(m_control->GetEventHandler(), wxGridCellEditorEvtHandler); | |
1496 | ||
1497 | // Don't immediately end if we get a kill focus event within BeginEdit | |
1498 | if (evtHandler) | |
1499 | evtHandler->SetInSetFocus(true); | |
1500 | ||
1501 | m_startValue = grid->GetTable()->GetValue(row, col); | |
1502 | ||
1503 | if (m_allowOthers) | |
1504 | { | |
1505 | Combo()->SetValue(m_startValue); | |
1506 | } | |
1507 | else | |
1508 | { | |
1509 | // find the right position, or default to the first if not found | |
1510 | int pos = Combo()->FindString(m_startValue); | |
1511 | if (pos == wxNOT_FOUND) | |
1512 | pos = 0; | |
1513 | Combo()->SetSelection(pos); | |
1514 | } | |
1515 | ||
1516 | Combo()->SetInsertionPointEnd(); | |
1517 | Combo()->SetFocus(); | |
1518 | ||
1519 | if (evtHandler) | |
1520 | { | |
1521 | // When dropping down the menu, a kill focus event | |
1522 | // happens after this point, so we can't reset the flag yet. | |
1523 | #if !defined(__WXGTK20__) | |
1524 | evtHandler->SetInSetFocus(false); | |
1525 | #endif | |
1526 | } | |
1527 | } | |
1528 | ||
1529 | bool wxGridCellChoiceEditor::EndEdit(int row, int col, | |
1530 | wxGrid* grid) | |
1531 | { | |
1532 | wxString value = Combo()->GetValue(); | |
1533 | if ( value == m_startValue ) | |
1534 | return false; | |
1535 | ||
1536 | grid->GetTable()->SetValue(row, col, value); | |
1537 | ||
1538 | return true; | |
1539 | } | |
1540 | ||
1541 | void wxGridCellChoiceEditor::Reset() | |
1542 | { | |
1543 | Combo()->SetValue(m_startValue); | |
1544 | Combo()->SetInsertionPointEnd(); | |
1545 | } | |
1546 | ||
1547 | void wxGridCellChoiceEditor::SetParameters(const wxString& params) | |
1548 | { | |
1549 | if ( !params ) | |
1550 | { | |
1551 | // what can we do? | |
1552 | return; | |
1553 | } | |
1554 | ||
1555 | m_choices.Empty(); | |
1556 | ||
1557 | wxStringTokenizer tk(params, _T(',')); | |
1558 | while ( tk.HasMoreTokens() ) | |
1559 | { | |
1560 | m_choices.Add(tk.GetNextToken()); | |
1561 | } | |
1562 | } | |
1563 | ||
1564 | // return the value in the text control | |
1565 | wxString wxGridCellChoiceEditor::GetValue() const | |
1566 | { | |
1567 | return Combo()->GetValue(); | |
1568 | } | |
1569 | ||
1570 | #endif // wxUSE_COMBOBOX | |
1571 | ||
1572 | // ---------------------------------------------------------------------------- | |
1573 | // wxGridCellEditorEvtHandler | |
1574 | // ---------------------------------------------------------------------------- | |
1575 | ||
1576 | void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent& event) | |
1577 | { | |
1578 | // Don't disable the cell if we're just starting to edit it | |
1579 | if (m_inSetFocus) | |
1580 | return; | |
1581 | ||
1582 | // accept changes | |
1583 | m_grid->DisableCellEditControl(); | |
1584 | ||
1585 | event.Skip(); | |
1586 | } | |
1587 | ||
1588 | void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event) | |
1589 | { | |
1590 | switch ( event.GetKeyCode() ) | |
1591 | { | |
1592 | case WXK_ESCAPE: | |
1593 | m_editor->Reset(); | |
1594 | m_grid->DisableCellEditControl(); | |
1595 | break; | |
1596 | ||
1597 | case WXK_TAB: | |
1598 | m_grid->GetEventHandler()->ProcessEvent( event ); | |
1599 | break; | |
1600 | ||
1601 | case WXK_RETURN: | |
1602 | case WXK_NUMPAD_ENTER: | |
1603 | if (!m_grid->GetEventHandler()->ProcessEvent(event)) | |
1604 | m_editor->HandleReturn(event); | |
1605 | break; | |
1606 | ||
1607 | default: | |
1608 | event.Skip(); | |
1609 | break; | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event) | |
1614 | { | |
1615 | int row = m_grid->GetGridCursorRow(); | |
1616 | int col = m_grid->GetGridCursorCol(); | |
1617 | wxRect rect = m_grid->CellToRect( row, col ); | |
1618 | int cw, ch; | |
1619 | m_grid->GetGridWindow()->GetClientSize( &cw, &ch ); | |
1620 | ||
1621 | // if cell width is smaller than grid client area, cell is wholly visible | |
1622 | bool wholeCellVisible = (rect.GetWidth() < cw); | |
1623 | ||
1624 | switch ( event.GetKeyCode() ) | |
1625 | { | |
1626 | case WXK_ESCAPE: | |
1627 | case WXK_TAB: | |
1628 | case WXK_RETURN: | |
1629 | case WXK_NUMPAD_ENTER: | |
1630 | break; | |
1631 | ||
1632 | case WXK_HOME: | |
1633 | { | |
1634 | if ( wholeCellVisible ) | |
1635 | { | |
1636 | // no special processing needed... | |
1637 | event.Skip(); | |
1638 | break; | |
1639 | } | |
1640 | ||
1641 | // do special processing for partly visible cell... | |
1642 | ||
1643 | // get the widths of all cells previous to this one | |
1644 | int colXPos = 0; | |
1645 | for ( int i = 0; i < col; i++ ) | |
1646 | { | |
1647 | colXPos += m_grid->GetColSize(i); | |
1648 | } | |
1649 | ||
1650 | int xUnit = 1, yUnit = 1; | |
1651 | m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit); | |
1652 | if (col != 0) | |
1653 | { | |
1654 | m_grid->Scroll(colXPos / xUnit - 1, m_grid->GetScrollPos(wxVERTICAL)); | |
1655 | } | |
1656 | else | |
1657 | { | |
1658 | m_grid->Scroll(colXPos / xUnit, m_grid->GetScrollPos(wxVERTICAL)); | |
1659 | } | |
1660 | event.Skip(); | |
1661 | break; | |
1662 | } | |
1663 | ||
1664 | case WXK_END: | |
1665 | { | |
1666 | if ( wholeCellVisible ) | |
1667 | { | |
1668 | // no special processing needed... | |
1669 | event.Skip(); | |
1670 | break; | |
1671 | } | |
1672 | ||
1673 | // do special processing for partly visible cell... | |
1674 | ||
1675 | int textWidth = 0; | |
1676 | wxString value = m_grid->GetCellValue(row, col); | |
1677 | if ( wxEmptyString != value ) | |
1678 | { | |
1679 | // get width of cell CONTENTS (text) | |
1680 | int y; | |
1681 | wxFont font = m_grid->GetCellFont(row, col); | |
1682 | m_grid->GetTextExtent(value, &textWidth, &y, NULL, NULL, &font); | |
1683 | ||
1684 | // try to RIGHT align the text by scrolling | |
1685 | int client_right = m_grid->GetGridWindow()->GetClientSize().GetWidth(); | |
1686 | ||
1687 | // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far, | |
1688 | // otherwise the last part of the cell content might be hidden below the scroll bar | |
1689 | // FIXME: maybe there is a more suitable correction? | |
1690 | textWidth -= (client_right - (m_grid->GetScrollLineX() * 2)); | |
1691 | if ( textWidth < 0 ) | |
1692 | { | |
1693 | textWidth = 0; | |
1694 | } | |
1695 | } | |
1696 | ||
1697 | // get the widths of all cells previous to this one | |
1698 | int colXPos = 0; | |
1699 | for ( int i = 0; i < col; i++ ) | |
1700 | { | |
1701 | colXPos += m_grid->GetColSize(i); | |
1702 | } | |
1703 | ||
1704 | // and add the (modified) text width of the cell contents | |
1705 | // as we'd like to see the last part of the cell contents | |
1706 | colXPos += textWidth; | |
1707 | ||
1708 | int xUnit = 1, yUnit = 1; | |
1709 | m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit); | |
1710 | m_grid->Scroll(colXPos / xUnit - 1, m_grid->GetScrollPos(wxVERTICAL)); | |
1711 | event.Skip(); | |
1712 | break; | |
1713 | } | |
1714 | ||
1715 | default: | |
1716 | event.Skip(); | |
1717 | break; | |
1718 | } | |
1719 | } | |
1720 | ||
1721 | // ---------------------------------------------------------------------------- | |
1722 | // wxGridCellWorker is an (almost) empty common base class for | |
1723 | // wxGridCellRenderer and wxGridCellEditor managing ref counting | |
1724 | // ---------------------------------------------------------------------------- | |
1725 | ||
1726 | void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params)) | |
1727 | { | |
1728 | // nothing to do | |
1729 | } | |
1730 | ||
1731 | wxGridCellWorker::~wxGridCellWorker() | |
1732 | { | |
1733 | } | |
1734 | ||
1735 | // ============================================================================ | |
1736 | // renderer classes | |
1737 | // ============================================================================ | |
1738 | ||
1739 | // ---------------------------------------------------------------------------- | |
1740 | // wxGridCellRenderer | |
1741 | // ---------------------------------------------------------------------------- | |
1742 | ||
1743 | void wxGridCellRenderer::Draw(wxGrid& grid, | |
1744 | wxGridCellAttr& attr, | |
1745 | wxDC& dc, | |
1746 | const wxRect& rect, | |
1747 | int WXUNUSED(row), int WXUNUSED(col), | |
1748 | bool isSelected) | |
1749 | { | |
1750 | dc.SetBackgroundMode( wxSOLID ); | |
1751 | ||
1752 | // grey out fields if the grid is disabled | |
1753 | if ( grid.IsEnabled() ) | |
1754 | { | |
1755 | if ( isSelected ) | |
1756 | { | |
1757 | dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) ); | |
1758 | } | |
1759 | else | |
1760 | { | |
1761 | dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) ); | |
1762 | } | |
1763 | } | |
1764 | else | |
1765 | { | |
1766 | dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), wxSOLID)); | |
1767 | } | |
1768 | ||
1769 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
1770 | dc.DrawRectangle(rect); | |
1771 | } | |
1772 | ||
1773 | // ---------------------------------------------------------------------------- | |
1774 | // wxGridCellStringRenderer | |
1775 | // ---------------------------------------------------------------------------- | |
1776 | ||
1777 | void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid& grid, | |
1778 | const wxGridCellAttr& attr, | |
1779 | wxDC& dc, | |
1780 | bool isSelected) | |
1781 | { | |
1782 | dc.SetBackgroundMode( wxTRANSPARENT ); | |
1783 | ||
1784 | // TODO some special colours for attr.IsReadOnly() case? | |
1785 | ||
1786 | // different coloured text when the grid is disabled | |
1787 | if ( grid.IsEnabled() ) | |
1788 | { | |
1789 | if ( isSelected ) | |
1790 | { | |
1791 | dc.SetTextBackground( grid.GetSelectionBackground() ); | |
1792 | dc.SetTextForeground( grid.GetSelectionForeground() ); | |
1793 | } | |
1794 | else | |
1795 | { | |
1796 | dc.SetTextBackground( attr.GetBackgroundColour() ); | |
1797 | dc.SetTextForeground( attr.GetTextColour() ); | |
1798 | } | |
1799 | } | |
1800 | else | |
1801 | { | |
1802 | dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); | |
1803 | dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT)); | |
1804 | } | |
1805 | ||
1806 | dc.SetFont( attr.GetFont() ); | |
1807 | } | |
1808 | ||
1809 | wxSize wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr& attr, | |
1810 | wxDC& dc, | |
1811 | const wxString& text) | |
1812 | { | |
1813 | wxCoord x = 0, y = 0, max_x = 0; | |
1814 | dc.SetFont(attr.GetFont()); | |
1815 | wxStringTokenizer tk(text, _T('\n')); | |
1816 | while ( tk.HasMoreTokens() ) | |
1817 | { | |
1818 | dc.GetTextExtent(tk.GetNextToken(), &x, &y); | |
1819 | max_x = wxMax(max_x, x); | |
1820 | } | |
1821 | ||
1822 | y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines. | |
1823 | ||
1824 | return wxSize(max_x, y); | |
1825 | } | |
1826 | ||
1827 | wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid, | |
1828 | wxGridCellAttr& attr, | |
1829 | wxDC& dc, | |
1830 | int row, int col) | |
1831 | { | |
1832 | return DoGetBestSize(attr, dc, grid.GetCellValue(row, col)); | |
1833 | } | |
1834 | ||
1835 | void wxGridCellStringRenderer::Draw(wxGrid& grid, | |
1836 | wxGridCellAttr& attr, | |
1837 | wxDC& dc, | |
1838 | const wxRect& rectCell, | |
1839 | int row, int col, | |
1840 | bool isSelected) | |
1841 | { | |
1842 | wxRect rect = rectCell; | |
1843 | rect.Inflate(-1); | |
1844 | ||
1845 | // erase only this cells background, overflow cells should have been erased | |
1846 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1847 | ||
1848 | int hAlign, vAlign; | |
1849 | attr.GetAlignment(&hAlign, &vAlign); | |
1850 | ||
1851 | int overflowCols = 0; | |
1852 | ||
1853 | if (attr.GetOverflow()) | |
1854 | { | |
1855 | int cols = grid.GetNumberCols(); | |
1856 | int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth(); | |
1857 | int cell_rows, cell_cols; | |
1858 | attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <= 0 | |
1859 | if ((best_width > rectCell.width) && (col < cols) && grid.GetTable()) | |
1860 | { | |
1861 | int i, c_cols, c_rows; | |
1862 | for (i = col+cell_cols; i < cols; i++) | |
1863 | { | |
1864 | bool is_empty = true; | |
1865 | for (int j=row; j < row + cell_rows; j++) | |
1866 | { | |
1867 | // check w/ anchor cell for multicell block | |
1868 | grid.GetCellSize(j, i, &c_rows, &c_cols); | |
1869 | if (c_rows > 0) | |
1870 | c_rows = 0; | |
1871 | if (!grid.GetTable()->IsEmptyCell(j + c_rows, i)) | |
1872 | { | |
1873 | is_empty = false; | |
1874 | break; | |
1875 | } | |
1876 | } | |
1877 | ||
1878 | if (is_empty) | |
1879 | { | |
1880 | rect.width += grid.GetColSize(i); | |
1881 | } | |
1882 | else | |
1883 | { | |
1884 | i--; | |
1885 | break; | |
1886 | } | |
1887 | ||
1888 | if (rect.width >= best_width) | |
1889 | break; | |
1890 | } | |
1891 | ||
1892 | overflowCols = i - col - cell_cols + 1; | |
1893 | if (overflowCols >= cols) | |
1894 | overflowCols = cols - 1; | |
1895 | } | |
1896 | ||
1897 | if (overflowCols > 0) // redraw overflow cells w/ proper hilight | |
1898 | { | |
1899 | hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned | |
1900 | wxRect clip = rect; | |
1901 | clip.x += rectCell.width; | |
1902 | // draw each overflow cell individually | |
1903 | int col_end = col + cell_cols + overflowCols; | |
1904 | if (col_end >= grid.GetNumberCols()) | |
1905 | col_end = grid.GetNumberCols() - 1; | |
1906 | for (int i = col + cell_cols; i <= col_end; i++) | |
1907 | { | |
1908 | clip.width = grid.GetColSize(i) - 1; | |
1909 | dc.DestroyClippingRegion(); | |
1910 | dc.SetClippingRegion(clip); | |
1911 | ||
1912 | SetTextColoursAndFont(grid, attr, dc, | |
1913 | grid.IsInSelection(row,i)); | |
1914 | ||
1915 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), | |
1916 | rect, hAlign, vAlign); | |
1917 | clip.x += grid.GetColSize(i) - 1; | |
1918 | } | |
1919 | ||
1920 | rect = rectCell; | |
1921 | rect.Inflate(-1); | |
1922 | rect.width++; | |
1923 | dc.DestroyClippingRegion(); | |
1924 | } | |
1925 | } | |
1926 | ||
1927 | // now we only have to draw the text | |
1928 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1929 | ||
1930 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), | |
1931 | rect, hAlign, vAlign); | |
1932 | } | |
1933 | ||
1934 | // ---------------------------------------------------------------------------- | |
1935 | // wxGridCellNumberRenderer | |
1936 | // ---------------------------------------------------------------------------- | |
1937 | ||
1938 | wxString wxGridCellNumberRenderer::GetString(const wxGrid& grid, int row, int col) | |
1939 | { | |
1940 | wxGridTableBase *table = grid.GetTable(); | |
1941 | wxString text; | |
1942 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
1943 | { | |
1944 | text.Printf(_T("%ld"), table->GetValueAsLong(row, col)); | |
1945 | } | |
1946 | else | |
1947 | { | |
1948 | text = table->GetValue(row, col); | |
1949 | } | |
1950 | ||
1951 | return text; | |
1952 | } | |
1953 | ||
1954 | void wxGridCellNumberRenderer::Draw(wxGrid& grid, | |
1955 | wxGridCellAttr& attr, | |
1956 | wxDC& dc, | |
1957 | const wxRect& rectCell, | |
1958 | int row, int col, | |
1959 | bool isSelected) | |
1960 | { | |
1961 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1962 | ||
1963 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1964 | ||
1965 | // draw the text right aligned by default | |
1966 | int hAlign, vAlign; | |
1967 | attr.GetAlignment(&hAlign, &vAlign); | |
1968 | hAlign = wxALIGN_RIGHT; | |
1969 | ||
1970 | wxRect rect = rectCell; | |
1971 | rect.Inflate(-1); | |
1972 | ||
1973 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); | |
1974 | } | |
1975 | ||
1976 | wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid, | |
1977 | wxGridCellAttr& attr, | |
1978 | wxDC& dc, | |
1979 | int row, int col) | |
1980 | { | |
1981 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
1982 | } | |
1983 | ||
1984 | // ---------------------------------------------------------------------------- | |
1985 | // wxGridCellFloatRenderer | |
1986 | // ---------------------------------------------------------------------------- | |
1987 | ||
1988 | wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision) | |
1989 | { | |
1990 | SetWidth(width); | |
1991 | SetPrecision(precision); | |
1992 | } | |
1993 | ||
1994 | wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const | |
1995 | { | |
1996 | wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer; | |
1997 | renderer->m_width = m_width; | |
1998 | renderer->m_precision = m_precision; | |
1999 | renderer->m_format = m_format; | |
2000 | ||
2001 | return renderer; | |
2002 | } | |
2003 | ||
2004 | wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col) | |
2005 | { | |
2006 | wxGridTableBase *table = grid.GetTable(); | |
2007 | ||
2008 | bool hasDouble; | |
2009 | double val; | |
2010 | wxString text; | |
2011 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
2012 | { | |
2013 | val = table->GetValueAsDouble(row, col); | |
2014 | hasDouble = true; | |
2015 | } | |
2016 | else | |
2017 | { | |
2018 | text = table->GetValue(row, col); | |
2019 | hasDouble = text.ToDouble(&val); | |
2020 | } | |
2021 | ||
2022 | if ( hasDouble ) | |
2023 | { | |
2024 | if ( !m_format ) | |
2025 | { | |
2026 | if ( m_width == -1 ) | |
2027 | { | |
2028 | if ( m_precision == -1 ) | |
2029 | { | |
2030 | // default width/precision | |
2031 | m_format = _T("%f"); | |
2032 | } | |
2033 | else | |
2034 | { | |
2035 | m_format.Printf(_T("%%.%df"), m_precision); | |
2036 | } | |
2037 | } | |
2038 | else if ( m_precision == -1 ) | |
2039 | { | |
2040 | // default precision | |
2041 | m_format.Printf(_T("%%%d.f"), m_width); | |
2042 | } | |
2043 | else | |
2044 | { | |
2045 | m_format.Printf(_T("%%%d.%df"), m_width, m_precision); | |
2046 | } | |
2047 | } | |
2048 | ||
2049 | text.Printf(m_format, val); | |
2050 | ||
2051 | } | |
2052 | //else: text already contains the string | |
2053 | ||
2054 | return text; | |
2055 | } | |
2056 | ||
2057 | void wxGridCellFloatRenderer::Draw(wxGrid& grid, | |
2058 | wxGridCellAttr& attr, | |
2059 | wxDC& dc, | |
2060 | const wxRect& rectCell, | |
2061 | int row, int col, | |
2062 | bool isSelected) | |
2063 | { | |
2064 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
2065 | ||
2066 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
2067 | ||
2068 | // draw the text right aligned by default | |
2069 | int hAlign, vAlign; | |
2070 | attr.GetAlignment(&hAlign, &vAlign); | |
2071 | hAlign = wxALIGN_RIGHT; | |
2072 | ||
2073 | wxRect rect = rectCell; | |
2074 | rect.Inflate(-1); | |
2075 | ||
2076 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); | |
2077 | } | |
2078 | ||
2079 | wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid, | |
2080 | wxGridCellAttr& attr, | |
2081 | wxDC& dc, | |
2082 | int row, int col) | |
2083 | { | |
2084 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
2085 | } | |
2086 | ||
2087 | void wxGridCellFloatRenderer::SetParameters(const wxString& params) | |
2088 | { | |
2089 | if ( !params ) | |
2090 | { | |
2091 | // reset to defaults | |
2092 | SetWidth(-1); | |
2093 | SetPrecision(-1); | |
2094 | } | |
2095 | else | |
2096 | { | |
2097 | wxString tmp = params.BeforeFirst(_T(',')); | |
2098 | if ( !tmp.empty() ) | |
2099 | { | |
2100 | long width; | |
2101 | if ( tmp.ToLong(&width) ) | |
2102 | { | |
2103 | SetWidth((int)width); | |
2104 | } | |
2105 | else | |
2106 | { | |
2107 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str()); | |
2108 | } | |
2109 | } | |
2110 | ||
2111 | tmp = params.AfterFirst(_T(',')); | |
2112 | if ( !tmp.empty() ) | |
2113 | { | |
2114 | long precision; | |
2115 | if ( tmp.ToLong(&precision) ) | |
2116 | { | |
2117 | SetPrecision((int)precision); | |
2118 | } | |
2119 | else | |
2120 | { | |
2121 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str()); | |
2122 | } | |
2123 | } | |
2124 | } | |
2125 | } | |
2126 | ||
2127 | // ---------------------------------------------------------------------------- | |
2128 | // wxGridCellBoolRenderer | |
2129 | // ---------------------------------------------------------------------------- | |
2130 | ||
2131 | wxSize wxGridCellBoolRenderer::ms_sizeCheckMark; | |
2132 | ||
2133 | // FIXME these checkbox size calculations are really ugly... | |
2134 | ||
2135 | // between checkmark and box | |
2136 | static const wxCoord wxGRID_CHECKMARK_MARGIN = 2; | |
2137 | ||
2138 | wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid, | |
2139 | wxGridCellAttr& WXUNUSED(attr), | |
2140 | wxDC& WXUNUSED(dc), | |
2141 | int WXUNUSED(row), | |
2142 | int WXUNUSED(col)) | |
2143 | { | |
2144 | // compute it only once (no locks for MT safeness in GUI thread...) | |
2145 | if ( !ms_sizeCheckMark.x ) | |
2146 | { | |
2147 | // get checkbox size | |
2148 | wxCheckBox *checkbox = new wxCheckBox(&grid, wxID_ANY, wxEmptyString); | |
2149 | wxSize size = checkbox->GetBestSize(); | |
2150 | wxCoord checkSize = size.y + 2 * wxGRID_CHECKMARK_MARGIN; | |
2151 | ||
2152 | // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result | |
2153 | #if defined(__WXGTK__) || defined(__WXMOTIF__) | |
2154 | checkSize -= size.y / 2; | |
2155 | #endif | |
2156 | ||
2157 | delete checkbox; | |
2158 | ||
2159 | ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize; | |
2160 | } | |
2161 | ||
2162 | return ms_sizeCheckMark; | |
2163 | } | |
2164 | ||
2165 | void wxGridCellBoolRenderer::Draw(wxGrid& grid, | |
2166 | wxGridCellAttr& attr, | |
2167 | wxDC& dc, | |
2168 | const wxRect& rect, | |
2169 | int row, int col, | |
2170 | bool isSelected) | |
2171 | { | |
2172 | wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); | |
2173 | ||
2174 | // draw a check mark in the centre (ignoring alignment - TODO) | |
2175 | wxSize size = GetBestSize(grid, attr, dc, row, col); | |
2176 | ||
2177 | // don't draw outside the cell | |
2178 | wxCoord minSize = wxMin(rect.width, rect.height); | |
2179 | if ( size.x >= minSize || size.y >= minSize ) | |
2180 | { | |
2181 | // and even leave (at least) 1 pixel margin | |
2182 | size.x = size.y = minSize - 2; | |
2183 | } | |
2184 | ||
2185 | // draw a border around checkmark | |
2186 | int vAlign, hAlign; | |
2187 | attr.GetAlignment(&hAlign, &vAlign); | |
2188 | ||
2189 | wxRect rectBorder; | |
2190 | if (hAlign == wxALIGN_CENTRE) | |
2191 | { | |
2192 | rectBorder.x = rect.x + rect.width / 2 - size.x / 2; | |
2193 | rectBorder.y = rect.y + rect.height / 2 - size.y / 2; | |
2194 | rectBorder.width = size.x; | |
2195 | rectBorder.height = size.y; | |
2196 | } | |
2197 | else if (hAlign == wxALIGN_LEFT) | |
2198 | { | |
2199 | rectBorder.x = rect.x + 2; | |
2200 | rectBorder.y = rect.y + rect.height / 2 - size.y / 2; | |
2201 | rectBorder.width = size.x; | |
2202 | rectBorder.height = size.y; | |
2203 | } | |
2204 | else if (hAlign == wxALIGN_RIGHT) | |
2205 | { | |
2206 | rectBorder.x = rect.x + rect.width - size.x - 2; | |
2207 | rectBorder.y = rect.y + rect.height / 2 - size.y / 2; | |
2208 | rectBorder.width = size.x; | |
2209 | rectBorder.height = size.y; | |
2210 | } | |
2211 | ||
2212 | bool value; | |
2213 | if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) | |
2214 | { | |
2215 | value = grid.GetTable()->GetValueAsBool(row, col); | |
2216 | } | |
2217 | else | |
2218 | { | |
2219 | wxString cellval( grid.GetTable()->GetValue(row, col) ); | |
2220 | value = !( !cellval || (cellval == wxT("0")) ); | |
2221 | } | |
2222 | ||
2223 | if ( value ) | |
2224 | { | |
2225 | wxRect rectMark = rectBorder; | |
2226 | ||
2227 | #ifdef __WXMSW__ | |
2228 | // MSW DrawCheckMark() is weird (and should probably be changed...) | |
2229 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN / 2); | |
2230 | rectMark.x++; | |
2231 | rectMark.y++; | |
2232 | #else | |
2233 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN); | |
2234 | #endif | |
2235 | ||
2236 | dc.SetTextForeground(attr.GetTextColour()); | |
2237 | dc.DrawCheckMark(rectMark); | |
2238 | } | |
2239 | ||
2240 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
2241 | dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID)); | |
2242 | dc.DrawRectangle(rectBorder); | |
2243 | } | |
2244 | ||
2245 | // ---------------------------------------------------------------------------- | |
2246 | // wxGridCellAttr | |
2247 | // ---------------------------------------------------------------------------- | |
2248 | ||
2249 | void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) | |
2250 | { | |
2251 | m_nRef = 1; | |
2252 | ||
2253 | m_isReadOnly = Unset; | |
2254 | ||
2255 | m_renderer = NULL; | |
2256 | m_editor = NULL; | |
2257 | ||
2258 | m_attrkind = wxGridCellAttr::Cell; | |
2259 | ||
2260 | m_sizeRows = m_sizeCols = 1; | |
2261 | m_overflow = UnsetOverflow; | |
2262 | ||
2263 | SetDefAttr(attrDefault); | |
2264 | } | |
2265 | ||
2266 | wxGridCellAttr *wxGridCellAttr::Clone() const | |
2267 | { | |
2268 | wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr); | |
2269 | ||
2270 | if ( HasTextColour() ) | |
2271 | attr->SetTextColour(GetTextColour()); | |
2272 | if ( HasBackgroundColour() ) | |
2273 | attr->SetBackgroundColour(GetBackgroundColour()); | |
2274 | if ( HasFont() ) | |
2275 | attr->SetFont(GetFont()); | |
2276 | if ( HasAlignment() ) | |
2277 | attr->SetAlignment(m_hAlign, m_vAlign); | |
2278 | ||
2279 | attr->SetSize( m_sizeRows, m_sizeCols ); | |
2280 | ||
2281 | if ( m_renderer ) | |
2282 | { | |
2283 | attr->SetRenderer(m_renderer); | |
2284 | m_renderer->IncRef(); | |
2285 | } | |
2286 | if ( m_editor ) | |
2287 | { | |
2288 | attr->SetEditor(m_editor); | |
2289 | m_editor->IncRef(); | |
2290 | } | |
2291 | ||
2292 | if ( IsReadOnly() ) | |
2293 | attr->SetReadOnly(); | |
2294 | ||
2295 | attr->SetOverflow( m_overflow == Overflow ); | |
2296 | attr->SetKind( m_attrkind ); | |
2297 | ||
2298 | return attr; | |
2299 | } | |
2300 | ||
2301 | void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) | |
2302 | { | |
2303 | if ( !HasTextColour() && mergefrom->HasTextColour() ) | |
2304 | SetTextColour(mergefrom->GetTextColour()); | |
2305 | if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() ) | |
2306 | SetBackgroundColour(mergefrom->GetBackgroundColour()); | |
2307 | if ( !HasFont() && mergefrom->HasFont() ) | |
2308 | SetFont(mergefrom->GetFont()); | |
2309 | if ( !HasAlignment() && mergefrom->HasAlignment() ) | |
2310 | { | |
2311 | int hAlign, vAlign; | |
2312 | mergefrom->GetAlignment( &hAlign, &vAlign); | |
2313 | SetAlignment(hAlign, vAlign); | |
2314 | } | |
2315 | if ( !HasSize() && mergefrom->HasSize() ) | |
2316 | mergefrom->GetSize( &m_sizeRows, &m_sizeCols ); | |
2317 | ||
2318 | // Directly access member functions as GetRender/Editor don't just return | |
2319 | // m_renderer/m_editor | |
2320 | // | |
2321 | // Maybe add support for merge of Render and Editor? | |
2322 | if (!HasRenderer() && mergefrom->HasRenderer() ) | |
2323 | { | |
2324 | m_renderer = mergefrom->m_renderer; | |
2325 | m_renderer->IncRef(); | |
2326 | } | |
2327 | if ( !HasEditor() && mergefrom->HasEditor() ) | |
2328 | { | |
2329 | m_editor = mergefrom->m_editor; | |
2330 | m_editor->IncRef(); | |
2331 | } | |
2332 | if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) | |
2333 | SetReadOnly(mergefrom->IsReadOnly()); | |
2334 | ||
2335 | if (!HasOverflowMode() && mergefrom->HasOverflowMode() ) | |
2336 | SetOverflow(mergefrom->GetOverflow()); | |
2337 | ||
2338 | SetDefAttr(mergefrom->m_defGridAttr); | |
2339 | } | |
2340 | ||
2341 | void wxGridCellAttr::SetSize(int num_rows, int num_cols) | |
2342 | { | |
2343 | // The size of a cell is normally 1,1 | |
2344 | ||
2345 | // If this cell is larger (2,2) then this is the top left cell | |
2346 | // the other cells that will be covered (lower right cells) must be | |
2347 | // set to negative or zero values such that | |
2348 | // row + num_rows of the covered cell points to the larger cell (this cell) | |
2349 | // same goes for the col + num_cols. | |
2350 | ||
2351 | // Size of 0,0 is NOT valid, neither is <=0 and any positive value | |
2352 | ||
2353 | wxASSERT_MSG( (!((num_rows > 0) && (num_cols <= 0)) || | |
2354 | !((num_rows <= 0) && (num_cols > 0)) || | |
2355 | !((num_rows == 0) && (num_cols == 0))), | |
2356 | wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values")); | |
2357 | ||
2358 | m_sizeRows = num_rows; | |
2359 | m_sizeCols = num_cols; | |
2360 | } | |
2361 | ||
2362 | const wxColour& wxGridCellAttr::GetTextColour() const | |
2363 | { | |
2364 | if (HasTextColour()) | |
2365 | { | |
2366 | return m_colText; | |
2367 | } | |
2368 | else if (m_defGridAttr && m_defGridAttr != this) | |
2369 | { | |
2370 | return m_defGridAttr->GetTextColour(); | |
2371 | } | |
2372 | else | |
2373 | { | |
2374 | wxFAIL_MSG(wxT("Missing default cell attribute")); | |
2375 | return wxNullColour; | |
2376 | } | |
2377 | } | |
2378 | ||
2379 | const wxColour& wxGridCellAttr::GetBackgroundColour() const | |
2380 | { | |
2381 | if (HasBackgroundColour()) | |
2382 | { | |
2383 | return m_colBack; | |
2384 | } | |
2385 | else if (m_defGridAttr && m_defGridAttr != this) | |
2386 | { | |
2387 | return m_defGridAttr->GetBackgroundColour(); | |
2388 | } | |
2389 | else | |
2390 | { | |
2391 | wxFAIL_MSG(wxT("Missing default cell attribute")); | |
2392 | return wxNullColour; | |
2393 | } | |
2394 | } | |
2395 | ||
2396 | const wxFont& wxGridCellAttr::GetFont() const | |
2397 | { | |
2398 | if (HasFont()) | |
2399 | { | |
2400 | return m_font; | |
2401 | } | |
2402 | else if (m_defGridAttr && m_defGridAttr != this) | |
2403 | { | |
2404 | return m_defGridAttr->GetFont(); | |
2405 | } | |
2406 | else | |
2407 | { | |
2408 | wxFAIL_MSG(wxT("Missing default cell attribute")); | |
2409 | return wxNullFont; | |
2410 | } | |
2411 | } | |
2412 | ||
2413 | void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const | |
2414 | { | |
2415 | if (HasAlignment()) | |
2416 | { | |
2417 | if ( hAlign ) | |
2418 | *hAlign = m_hAlign; | |
2419 | if ( vAlign ) | |
2420 | *vAlign = m_vAlign; | |
2421 | } | |
2422 | else if (m_defGridAttr && m_defGridAttr != this) | |
2423 | { | |
2424 | m_defGridAttr->GetAlignment(hAlign, vAlign); | |
2425 | } | |
2426 | else | |
2427 | { | |
2428 | wxFAIL_MSG(wxT("Missing default cell attribute")); | |
2429 | } | |
2430 | } | |
2431 | ||
2432 | void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const | |
2433 | { | |
2434 | if ( num_rows ) | |
2435 | *num_rows = m_sizeRows; | |
2436 | if ( num_cols ) | |
2437 | *num_cols = m_sizeCols; | |
2438 | } | |
2439 | ||
2440 | // GetRenderer and GetEditor use a slightly different decision path about | |
2441 | // which attribute to use. If a non-default attr object has one then it is | |
2442 | // used, otherwise the default editor or renderer is fetched from the grid and | |
2443 | // used. It should be the default for the data type of the cell. If it is | |
2444 | // NULL (because the table has a type that the grid does not have in its | |
2445 | // registry), then the grid's default editor or renderer is used. | |
2446 | ||
2447 | wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const | |
2448 | { | |
2449 | wxGridCellRenderer *renderer = NULL; | |
2450 | ||
2451 | if ( m_renderer && this != m_defGridAttr ) | |
2452 | { | |
2453 | // use the cells renderer if it has one | |
2454 | renderer = m_renderer; | |
2455 | renderer->IncRef(); | |
2456 | } | |
2457 | else // no non-default cell renderer | |
2458 | { | |
2459 | // get default renderer for the data type | |
2460 | if ( grid ) | |
2461 | { | |
2462 | // GetDefaultRendererForCell() will do IncRef() for us | |
2463 | renderer = grid->GetDefaultRendererForCell(row, col); | |
2464 | } | |
2465 | ||
2466 | if ( renderer == NULL ) | |
2467 | { | |
2468 | if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) ) | |
2469 | { | |
2470 | // if we still don't have one then use the grid default | |
2471 | // (no need for IncRef() here neither) | |
2472 | renderer = m_defGridAttr->GetRenderer(NULL, 0, 0); | |
2473 | } | |
2474 | else // default grid attr | |
2475 | { | |
2476 | // use m_renderer which we had decided not to use initially | |
2477 | renderer = m_renderer; | |
2478 | if ( renderer ) | |
2479 | renderer->IncRef(); | |
2480 | } | |
2481 | } | |
2482 | } | |
2483 | ||
2484 | // we're supposed to always find something | |
2485 | wxASSERT_MSG(renderer, wxT("Missing default cell renderer")); | |
2486 | ||
2487 | return renderer; | |
2488 | } | |
2489 | ||
2490 | // same as above, except for s/renderer/editor/g | |
2491 | wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const | |
2492 | { | |
2493 | wxGridCellEditor *editor = NULL; | |
2494 | ||
2495 | if ( m_editor && this != m_defGridAttr ) | |
2496 | { | |
2497 | // use the cells editor if it has one | |
2498 | editor = m_editor; | |
2499 | editor->IncRef(); | |
2500 | } | |
2501 | else // no non default cell editor | |
2502 | { | |
2503 | // get default editor for the data type | |
2504 | if ( grid ) | |
2505 | { | |
2506 | // GetDefaultEditorForCell() will do IncRef() for us | |
2507 | editor = grid->GetDefaultEditorForCell(row, col); | |
2508 | } | |
2509 | ||
2510 | if ( editor == NULL ) | |
2511 | { | |
2512 | if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) ) | |
2513 | { | |
2514 | // if we still don't have one then use the grid default | |
2515 | // (no need for IncRef() here neither) | |
2516 | editor = m_defGridAttr->GetEditor(NULL, 0, 0); | |
2517 | } | |
2518 | else // default grid attr | |
2519 | { | |
2520 | // use m_editor which we had decided not to use initially | |
2521 | editor = m_editor; | |
2522 | if ( editor ) | |
2523 | editor->IncRef(); | |
2524 | } | |
2525 | } | |
2526 | } | |
2527 | ||
2528 | // we're supposed to always find something | |
2529 | wxASSERT_MSG(editor, wxT("Missing default cell editor")); | |
2530 | ||
2531 | return editor; | |
2532 | } | |
2533 | ||
2534 | // ---------------------------------------------------------------------------- | |
2535 | // wxGridCellAttrData | |
2536 | // ---------------------------------------------------------------------------- | |
2537 | ||
2538 | void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) | |
2539 | { | |
2540 | int n = FindIndex(row, col); | |
2541 | if ( n == wxNOT_FOUND ) | |
2542 | { | |
2543 | // add the attribute | |
2544 | m_attrs.Add(new wxGridCellWithAttr(row, col, attr)); | |
2545 | } | |
2546 | else | |
2547 | { | |
2548 | // free the old attribute | |
2549 | m_attrs[(size_t)n].attr->DecRef(); | |
2550 | ||
2551 | if ( attr ) | |
2552 | { | |
2553 | // change the attribute | |
2554 | m_attrs[(size_t)n].attr = attr; | |
2555 | } | |
2556 | else | |
2557 | { | |
2558 | // remove this attribute | |
2559 | m_attrs.RemoveAt((size_t)n); | |
2560 | } | |
2561 | } | |
2562 | } | |
2563 | ||
2564 | wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const | |
2565 | { | |
2566 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2567 | ||
2568 | int n = FindIndex(row, col); | |
2569 | if ( n != wxNOT_FOUND ) | |
2570 | { | |
2571 | attr = m_attrs[(size_t)n].attr; | |
2572 | attr->IncRef(); | |
2573 | } | |
2574 | ||
2575 | return attr; | |
2576 | } | |
2577 | ||
2578 | void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) | |
2579 | { | |
2580 | size_t count = m_attrs.GetCount(); | |
2581 | for ( size_t n = 0; n < count; n++ ) | |
2582 | { | |
2583 | wxGridCellCoords& coords = m_attrs[n].coords; | |
2584 | wxCoord row = coords.GetRow(); | |
2585 | if ((size_t)row >= pos) | |
2586 | { | |
2587 | if (numRows > 0) | |
2588 | { | |
2589 | // If rows inserted, include row counter where necessary | |
2590 | coords.SetRow(row + numRows); | |
2591 | } | |
2592 | else if (numRows < 0) | |
2593 | { | |
2594 | // If rows deleted ... | |
2595 | if ((size_t)row >= pos - numRows) | |
2596 | { | |
2597 | // ...either decrement row counter (if row still exists)... | |
2598 | coords.SetRow(row + numRows); | |
2599 | } | |
2600 | else | |
2601 | { | |
2602 | // ...or remove the attribute | |
2603 | // No need to DecRef the attribute itself since this is | |
2604 | // done be wxGridCellWithAttr's destructor! | |
2605 | m_attrs.RemoveAt(n); | |
2606 | n--; | |
2607 | count--; | |
2608 | } | |
2609 | } | |
2610 | } | |
2611 | } | |
2612 | } | |
2613 | ||
2614 | void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) | |
2615 | { | |
2616 | size_t count = m_attrs.GetCount(); | |
2617 | for ( size_t n = 0; n < count; n++ ) | |
2618 | { | |
2619 | wxGridCellCoords& coords = m_attrs[n].coords; | |
2620 | wxCoord col = coords.GetCol(); | |
2621 | if ( (size_t)col >= pos ) | |
2622 | { | |
2623 | if ( numCols > 0 ) | |
2624 | { | |
2625 | // If rows inserted, include row counter where necessary | |
2626 | coords.SetCol(col + numCols); | |
2627 | } | |
2628 | else if (numCols < 0) | |
2629 | { | |
2630 | // If rows deleted ... | |
2631 | if ((size_t)col >= pos - numCols) | |
2632 | { | |
2633 | // ...either decrement row counter (if row still exists)... | |
2634 | coords.SetCol(col + numCols); | |
2635 | } | |
2636 | else | |
2637 | { | |
2638 | // ...or remove the attribute | |
2639 | // No need to DecRef the attribute itself since this is | |
2640 | // done be wxGridCellWithAttr's destructor! | |
2641 | m_attrs.RemoveAt(n); | |
2642 | n--; | |
2643 | count--; | |
2644 | } | |
2645 | } | |
2646 | } | |
2647 | } | |
2648 | } | |
2649 | ||
2650 | int wxGridCellAttrData::FindIndex(int row, int col) const | |
2651 | { | |
2652 | size_t count = m_attrs.GetCount(); | |
2653 | for ( size_t n = 0; n < count; n++ ) | |
2654 | { | |
2655 | const wxGridCellCoords& coords = m_attrs[n].coords; | |
2656 | if ( (coords.GetRow() == row) && (coords.GetCol() == col) ) | |
2657 | { | |
2658 | return n; | |
2659 | } | |
2660 | } | |
2661 | ||
2662 | return wxNOT_FOUND; | |
2663 | } | |
2664 | ||
2665 | // ---------------------------------------------------------------------------- | |
2666 | // wxGridRowOrColAttrData | |
2667 | // ---------------------------------------------------------------------------- | |
2668 | ||
2669 | wxGridRowOrColAttrData::~wxGridRowOrColAttrData() | |
2670 | { | |
2671 | size_t count = m_attrs.Count(); | |
2672 | for ( size_t n = 0; n < count; n++ ) | |
2673 | { | |
2674 | m_attrs[n]->DecRef(); | |
2675 | } | |
2676 | } | |
2677 | ||
2678 | wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const | |
2679 | { | |
2680 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2681 | ||
2682 | int n = m_rowsOrCols.Index(rowOrCol); | |
2683 | if ( n != wxNOT_FOUND ) | |
2684 | { | |
2685 | attr = m_attrs[(size_t)n]; | |
2686 | attr->IncRef(); | |
2687 | } | |
2688 | ||
2689 | return attr; | |
2690 | } | |
2691 | ||
2692 | void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) | |
2693 | { | |
2694 | int i = m_rowsOrCols.Index(rowOrCol); | |
2695 | if ( i == wxNOT_FOUND ) | |
2696 | { | |
2697 | // add the attribute | |
2698 | m_rowsOrCols.Add(rowOrCol); | |
2699 | m_attrs.Add(attr); | |
2700 | } | |
2701 | else | |
2702 | { | |
2703 | size_t n = (size_t)i; | |
2704 | if ( attr ) | |
2705 | { | |
2706 | // change the attribute | |
2707 | m_attrs[n]->DecRef(); | |
2708 | m_attrs[n] = attr; | |
2709 | } | |
2710 | else | |
2711 | { | |
2712 | // remove this attribute | |
2713 | m_attrs[n]->DecRef(); | |
2714 | m_rowsOrCols.RemoveAt(n); | |
2715 | m_attrs.RemoveAt(n); | |
2716 | } | |
2717 | } | |
2718 | } | |
2719 | ||
2720 | void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) | |
2721 | { | |
2722 | size_t count = m_attrs.GetCount(); | |
2723 | for ( size_t n = 0; n < count; n++ ) | |
2724 | { | |
2725 | int & rowOrCol = m_rowsOrCols[n]; | |
2726 | if ( (size_t)rowOrCol >= pos ) | |
2727 | { | |
2728 | if ( numRowsOrCols > 0 ) | |
2729 | { | |
2730 | // If rows inserted, include row counter where necessary | |
2731 | rowOrCol += numRowsOrCols; | |
2732 | } | |
2733 | else if ( numRowsOrCols < 0) | |
2734 | { | |
2735 | // If rows deleted, either decrement row counter (if row still exists) | |
2736 | if ((size_t)rowOrCol >= pos - numRowsOrCols) | |
2737 | rowOrCol += numRowsOrCols; | |
2738 | else | |
2739 | { | |
2740 | m_rowsOrCols.RemoveAt(n); | |
2741 | m_attrs[n]->DecRef(); | |
2742 | m_attrs.RemoveAt(n); | |
2743 | n--; | |
2744 | count--; | |
2745 | } | |
2746 | } | |
2747 | } | |
2748 | } | |
2749 | } | |
2750 | ||
2751 | // ---------------------------------------------------------------------------- | |
2752 | // wxGridCellAttrProvider | |
2753 | // ---------------------------------------------------------------------------- | |
2754 | ||
2755 | wxGridCellAttrProvider::wxGridCellAttrProvider() | |
2756 | { | |
2757 | m_data = (wxGridCellAttrProviderData *)NULL; | |
2758 | } | |
2759 | ||
2760 | wxGridCellAttrProvider::~wxGridCellAttrProvider() | |
2761 | { | |
2762 | delete m_data; | |
2763 | } | |
2764 | ||
2765 | void wxGridCellAttrProvider::InitData() | |
2766 | { | |
2767 | m_data = new wxGridCellAttrProviderData; | |
2768 | } | |
2769 | ||
2770 | wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col, | |
2771 | wxGridCellAttr::wxAttrKind kind ) const | |
2772 | { | |
2773 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2774 | if ( m_data ) | |
2775 | { | |
2776 | switch (kind) | |
2777 | { | |
2778 | case (wxGridCellAttr::Any): | |
2779 | // Get cached merge attributes. | |
2780 | // Currently not used as no cache implemented as not mutable | |
2781 | // attr = m_data->m_mergeAttr.GetAttr(row, col); | |
2782 | if (!attr) | |
2783 | { | |
2784 | // Basically implement old version. | |
2785 | // Also check merge cache, so we don't have to re-merge every time.. | |
2786 | wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col); | |
2787 | wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row); | |
2788 | wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col); | |
2789 | ||
2790 | if ((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol)) | |
2791 | { | |
2792 | // Two or more are non NULL | |
2793 | attr = new wxGridCellAttr; | |
2794 | attr->SetKind(wxGridCellAttr::Merged); | |
2795 | ||
2796 | // Order is important.. | |
2797 | if (attrcell) | |
2798 | { | |
2799 | attr->MergeWith(attrcell); | |
2800 | attrcell->DecRef(); | |
2801 | } | |
2802 | if (attrcol) | |
2803 | { | |
2804 | attr->MergeWith(attrcol); | |
2805 | attrcol->DecRef(); | |
2806 | } | |
2807 | if (attrrow) | |
2808 | { | |
2809 | attr->MergeWith(attrrow); | |
2810 | attrrow->DecRef(); | |
2811 | } | |
2812 | ||
2813 | // store merge attr if cache implemented | |
2814 | //attr->IncRef(); | |
2815 | //m_data->m_mergeAttr.SetAttr(attr, row, col); | |
2816 | } | |
2817 | else | |
2818 | { | |
2819 | // one or none is non null return it or null. | |
2820 | if (attrrow) | |
2821 | attr = attrrow; | |
2822 | if (attrcol) | |
2823 | { | |
2824 | if (attr) | |
2825 | attr->DecRef(); | |
2826 | attr = attrcol; | |
2827 | } | |
2828 | if (attrcell) | |
2829 | { | |
2830 | if (attr) | |
2831 | attr->DecRef(); | |
2832 | attr = attrcell; | |
2833 | } | |
2834 | } | |
2835 | } | |
2836 | break; | |
2837 | ||
2838 | case (wxGridCellAttr::Cell): | |
2839 | attr = m_data->m_cellAttrs.GetAttr(row, col); | |
2840 | break; | |
2841 | ||
2842 | case (wxGridCellAttr::Col): | |
2843 | attr = m_data->m_colAttrs.GetAttr(col); | |
2844 | break; | |
2845 | ||
2846 | case (wxGridCellAttr::Row): | |
2847 | attr = m_data->m_rowAttrs.GetAttr(row); | |
2848 | break; | |
2849 | ||
2850 | default: | |
2851 | // unused as yet... | |
2852 | // (wxGridCellAttr::Default): | |
2853 | // (wxGridCellAttr::Merged): | |
2854 | break; | |
2855 | } | |
2856 | } | |
2857 | ||
2858 | return attr; | |
2859 | } | |
2860 | ||
2861 | void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, | |
2862 | int row, int col) | |
2863 | { | |
2864 | if ( !m_data ) | |
2865 | InitData(); | |
2866 | ||
2867 | m_data->m_cellAttrs.SetAttr(attr, row, col); | |
2868 | } | |
2869 | ||
2870 | void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row) | |
2871 | { | |
2872 | if ( !m_data ) | |
2873 | InitData(); | |
2874 | ||
2875 | m_data->m_rowAttrs.SetAttr(attr, row); | |
2876 | } | |
2877 | ||
2878 | void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) | |
2879 | { | |
2880 | if ( !m_data ) | |
2881 | InitData(); | |
2882 | ||
2883 | m_data->m_colAttrs.SetAttr(attr, col); | |
2884 | } | |
2885 | ||
2886 | void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) | |
2887 | { | |
2888 | if ( m_data ) | |
2889 | { | |
2890 | m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); | |
2891 | ||
2892 | m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); | |
2893 | } | |
2894 | } | |
2895 | ||
2896 | void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) | |
2897 | { | |
2898 | if ( m_data ) | |
2899 | { | |
2900 | m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); | |
2901 | ||
2902 | m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); | |
2903 | } | |
2904 | } | |
2905 | ||
2906 | // ---------------------------------------------------------------------------- | |
2907 | // wxGridTypeRegistry | |
2908 | // ---------------------------------------------------------------------------- | |
2909 | ||
2910 | wxGridTypeRegistry::~wxGridTypeRegistry() | |
2911 | { | |
2912 | size_t count = m_typeinfo.Count(); | |
2913 | for ( size_t i = 0; i < count; i++ ) | |
2914 | delete m_typeinfo[i]; | |
2915 | } | |
2916 | ||
2917 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, | |
2918 | wxGridCellRenderer* renderer, | |
2919 | wxGridCellEditor* editor) | |
2920 | { | |
2921 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); | |
2922 | ||
2923 | // is it already registered? | |
2924 | int loc = FindRegisteredDataType(typeName); | |
2925 | if ( loc != wxNOT_FOUND ) | |
2926 | { | |
2927 | delete m_typeinfo[loc]; | |
2928 | m_typeinfo[loc] = info; | |
2929 | } | |
2930 | else | |
2931 | { | |
2932 | m_typeinfo.Add(info); | |
2933 | } | |
2934 | } | |
2935 | ||
2936 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) | |
2937 | { | |
2938 | size_t count = m_typeinfo.GetCount(); | |
2939 | for ( size_t i = 0; i < count; i++ ) | |
2940 | { | |
2941 | if ( typeName == m_typeinfo[i]->m_typeName ) | |
2942 | { | |
2943 | return i; | |
2944 | } | |
2945 | } | |
2946 | ||
2947 | return wxNOT_FOUND; | |
2948 | } | |
2949 | ||
2950 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) | |
2951 | { | |
2952 | int index = FindRegisteredDataType(typeName); | |
2953 | if ( index == wxNOT_FOUND ) | |
2954 | { | |
2955 | // check whether this is one of the standard ones, in which case | |
2956 | // register it "on the fly" | |
2957 | #if wxUSE_TEXTCTRL | |
2958 | if ( typeName == wxGRID_VALUE_STRING ) | |
2959 | { | |
2960 | RegisterDataType(wxGRID_VALUE_STRING, | |
2961 | new wxGridCellStringRenderer, | |
2962 | new wxGridCellTextEditor); | |
2963 | } | |
2964 | else | |
2965 | #endif // wxUSE_TEXTCTRL | |
2966 | #if wxUSE_CHECKBOX | |
2967 | if ( typeName == wxGRID_VALUE_BOOL ) | |
2968 | { | |
2969 | RegisterDataType(wxGRID_VALUE_BOOL, | |
2970 | new wxGridCellBoolRenderer, | |
2971 | new wxGridCellBoolEditor); | |
2972 | } | |
2973 | else | |
2974 | #endif // wxUSE_CHECKBOX | |
2975 | #if wxUSE_TEXTCTRL | |
2976 | if ( typeName == wxGRID_VALUE_NUMBER ) | |
2977 | { | |
2978 | RegisterDataType(wxGRID_VALUE_NUMBER, | |
2979 | new wxGridCellNumberRenderer, | |
2980 | new wxGridCellNumberEditor); | |
2981 | } | |
2982 | else if ( typeName == wxGRID_VALUE_FLOAT ) | |
2983 | { | |
2984 | RegisterDataType(wxGRID_VALUE_FLOAT, | |
2985 | new wxGridCellFloatRenderer, | |
2986 | new wxGridCellFloatEditor); | |
2987 | } | |
2988 | else | |
2989 | #endif // wxUSE_TEXTCTRL | |
2990 | #if wxUSE_COMBOBOX | |
2991 | if ( typeName == wxGRID_VALUE_CHOICE ) | |
2992 | { | |
2993 | RegisterDataType(wxGRID_VALUE_CHOICE, | |
2994 | new wxGridCellStringRenderer, | |
2995 | new wxGridCellChoiceEditor); | |
2996 | } | |
2997 | else | |
2998 | #endif // wxUSE_COMBOBOX | |
2999 | { | |
3000 | return wxNOT_FOUND; | |
3001 | } | |
3002 | ||
3003 | // we get here only if just added the entry for this type, so return | |
3004 | // the last index | |
3005 | index = m_typeinfo.GetCount() - 1; | |
3006 | } | |
3007 | ||
3008 | return index; | |
3009 | } | |
3010 | ||
3011 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) | |
3012 | { | |
3013 | int index = FindDataType(typeName); | |
3014 | if ( index == wxNOT_FOUND ) | |
3015 | { | |
3016 | // the first part of the typename is the "real" type, anything after ':' | |
3017 | // are the parameters for the renderer | |
3018 | index = FindDataType(typeName.BeforeFirst(_T(':'))); | |
3019 | if ( index == wxNOT_FOUND ) | |
3020 | { | |
3021 | return wxNOT_FOUND; | |
3022 | } | |
3023 | ||
3024 | wxGridCellRenderer *renderer = GetRenderer(index); | |
3025 | wxGridCellRenderer *rendererOld = renderer; | |
3026 | renderer = renderer->Clone(); | |
3027 | rendererOld->DecRef(); | |
3028 | ||
3029 | wxGridCellEditor *editor = GetEditor(index); | |
3030 | wxGridCellEditor *editorOld = editor; | |
3031 | editor = editor->Clone(); | |
3032 | editorOld->DecRef(); | |
3033 | ||
3034 | // do it even if there are no parameters to reset them to defaults | |
3035 | wxString params = typeName.AfterFirst(_T(':')); | |
3036 | renderer->SetParameters(params); | |
3037 | editor->SetParameters(params); | |
3038 | ||
3039 | // register the new typename | |
3040 | RegisterDataType(typeName, renderer, editor); | |
3041 | ||
3042 | // we just registered it, it's the last one | |
3043 | index = m_typeinfo.GetCount() - 1; | |
3044 | } | |
3045 | ||
3046 | return index; | |
3047 | } | |
3048 | ||
3049 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) | |
3050 | { | |
3051 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; | |
3052 | if (renderer) | |
3053 | renderer->IncRef(); | |
3054 | ||
3055 | return renderer; | |
3056 | } | |
3057 | ||
3058 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) | |
3059 | { | |
3060 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; | |
3061 | if (editor) | |
3062 | editor->IncRef(); | |
3063 | ||
3064 | return editor; | |
3065 | } | |
3066 | ||
3067 | // ---------------------------------------------------------------------------- | |
3068 | // wxGridTableBase | |
3069 | // ---------------------------------------------------------------------------- | |
3070 | ||
3071 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) | |
3072 | ||
3073 | wxGridTableBase::wxGridTableBase() | |
3074 | { | |
3075 | m_view = (wxGrid *) NULL; | |
3076 | m_attrProvider = (wxGridCellAttrProvider *) NULL; | |
3077 | } | |
3078 | ||
3079 | wxGridTableBase::~wxGridTableBase() | |
3080 | { | |
3081 | delete m_attrProvider; | |
3082 | } | |
3083 | ||
3084 | void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider) | |
3085 | { | |
3086 | delete m_attrProvider; | |
3087 | m_attrProvider = attrProvider; | |
3088 | } | |
3089 | ||
3090 | bool wxGridTableBase::CanHaveAttributes() | |
3091 | { | |
3092 | if ( ! GetAttrProvider() ) | |
3093 | { | |
3094 | // use the default attr provider by default | |
3095 | SetAttrProvider(new wxGridCellAttrProvider); | |
3096 | } | |
3097 | ||
3098 | return true; | |
3099 | } | |
3100 | ||
3101 | wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) | |
3102 | { | |
3103 | if ( m_attrProvider ) | |
3104 | return m_attrProvider->GetAttr(row, col, kind); | |
3105 | else | |
3106 | return (wxGridCellAttr *)NULL; | |
3107 | } | |
3108 | ||
3109 | void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) | |
3110 | { | |
3111 | if ( m_attrProvider ) | |
3112 | { | |
3113 | attr->SetKind(wxGridCellAttr::Cell); | |
3114 | m_attrProvider->SetAttr(attr, row, col); | |
3115 | } | |
3116 | else | |
3117 | { | |
3118 | // as we take ownership of the pointer and don't store it, we must | |
3119 | // free it now | |
3120 | wxSafeDecRef(attr); | |
3121 | } | |
3122 | } | |
3123 | ||
3124 | void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) | |
3125 | { | |
3126 | if ( m_attrProvider ) | |
3127 | { | |
3128 | attr->SetKind(wxGridCellAttr::Row); | |
3129 | m_attrProvider->SetRowAttr(attr, row); | |
3130 | } | |
3131 | else | |
3132 | { | |
3133 | // as we take ownership of the pointer and don't store it, we must | |
3134 | // free it now | |
3135 | wxSafeDecRef(attr); | |
3136 | } | |
3137 | } | |
3138 | ||
3139 | void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) | |
3140 | { | |
3141 | if ( m_attrProvider ) | |
3142 | { | |
3143 | attr->SetKind(wxGridCellAttr::Col); | |
3144 | m_attrProvider->SetColAttr(attr, col); | |
3145 | } | |
3146 | else | |
3147 | { | |
3148 | // as we take ownership of the pointer and don't store it, we must | |
3149 | // free it now | |
3150 | wxSafeDecRef(attr); | |
3151 | } | |
3152 | } | |
3153 | ||
3154 | bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos), | |
3155 | size_t WXUNUSED(numRows) ) | |
3156 | { | |
3157 | wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); | |
3158 | ||
3159 | return false; | |
3160 | } | |
3161 | ||
3162 | bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) ) | |
3163 | { | |
3164 | wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); | |
3165 | ||
3166 | return false; | |
3167 | } | |
3168 | ||
3169 | bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos), | |
3170 | size_t WXUNUSED(numRows) ) | |
3171 | { | |
3172 | wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); | |
3173 | ||
3174 | return false; | |
3175 | } | |
3176 | ||
3177 | bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos), | |
3178 | size_t WXUNUSED(numCols) ) | |
3179 | { | |
3180 | wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); | |
3181 | ||
3182 | return false; | |
3183 | } | |
3184 | ||
3185 | bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) ) | |
3186 | { | |
3187 | wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); | |
3188 | ||
3189 | return false; | |
3190 | } | |
3191 | ||
3192 | bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos), | |
3193 | size_t WXUNUSED(numCols) ) | |
3194 | { | |
3195 | wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); | |
3196 | ||
3197 | return false; | |
3198 | } | |
3199 | ||
3200 | wxString wxGridTableBase::GetRowLabelValue( int row ) | |
3201 | { | |
3202 | wxString s; | |
3203 | ||
3204 | // RD: Starting the rows at zero confuses users, | |
3205 | // no matter how much it makes sense to us geeks. | |
3206 | s << row + 1; | |
3207 | ||
3208 | return s; | |
3209 | } | |
3210 | ||
3211 | wxString wxGridTableBase::GetColLabelValue( int col ) | |
3212 | { | |
3213 | // default col labels are: | |
3214 | // cols 0 to 25 : A-Z | |
3215 | // cols 26 to 675 : AA-ZZ | |
3216 | // etc. | |
3217 | ||
3218 | wxString s; | |
3219 | unsigned int i, n; | |
3220 | for ( n = 1; ; n++ ) | |
3221 | { | |
3222 | s += (wxChar) (_T('A') + (wxChar)(col % 26)); | |
3223 | col = col / 26 - 1; | |
3224 | if ( col < 0 ) | |
3225 | break; | |
3226 | } | |
3227 | ||
3228 | // reverse the string... | |
3229 | wxString s2; | |
3230 | for ( i = 0; i < n; i++ ) | |
3231 | { | |
3232 | s2 += s[n - i - 1]; | |
3233 | } | |
3234 | ||
3235 | return s2; | |
3236 | } | |
3237 | ||
3238 | wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) ) | |
3239 | { | |
3240 | return wxGRID_VALUE_STRING; | |
3241 | } | |
3242 | ||
3243 | bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col), | |
3244 | const wxString& typeName ) | |
3245 | { | |
3246 | return typeName == wxGRID_VALUE_STRING; | |
3247 | } | |
3248 | ||
3249 | bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName ) | |
3250 | { | |
3251 | return CanGetValueAs(row, col, typeName); | |
3252 | } | |
3253 | ||
3254 | long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) ) | |
3255 | { | |
3256 | return 0; | |
3257 | } | |
3258 | ||
3259 | double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) ) | |
3260 | { | |
3261 | return 0.0; | |
3262 | } | |
3263 | ||
3264 | bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) ) | |
3265 | { | |
3266 | return false; | |
3267 | } | |
3268 | ||
3269 | void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col), | |
3270 | long WXUNUSED(value) ) | |
3271 | { | |
3272 | } | |
3273 | ||
3274 | void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col), | |
3275 | double WXUNUSED(value) ) | |
3276 | { | |
3277 | } | |
3278 | ||
3279 | void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col), | |
3280 | bool WXUNUSED(value) ) | |
3281 | { | |
3282 | } | |
3283 | ||
3284 | void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3285 | const wxString& WXUNUSED(typeName) ) | |
3286 | { | |
3287 | return NULL; | |
3288 | } | |
3289 | ||
3290 | void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3291 | const wxString& WXUNUSED(typeName), | |
3292 | void* WXUNUSED(value) ) | |
3293 | { | |
3294 | } | |
3295 | ||
3296 | ////////////////////////////////////////////////////////////////////// | |
3297 | // | |
3298 | // Message class for the grid table to send requests and notifications | |
3299 | // to the grid view | |
3300 | // | |
3301 | ||
3302 | wxGridTableMessage::wxGridTableMessage() | |
3303 | { | |
3304 | m_table = (wxGridTableBase *) NULL; | |
3305 | m_id = -1; | |
3306 | m_comInt1 = -1; | |
3307 | m_comInt2 = -1; | |
3308 | } | |
3309 | ||
3310 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, | |
3311 | int commandInt1, int commandInt2 ) | |
3312 | { | |
3313 | m_table = table; | |
3314 | m_id = id; | |
3315 | m_comInt1 = commandInt1; | |
3316 | m_comInt2 = commandInt2; | |
3317 | } | |
3318 | ||
3319 | ////////////////////////////////////////////////////////////////////// | |
3320 | // | |
3321 | // A basic grid table for string data. An object of this class will | |
3322 | // created by wxGrid if you don't specify an alternative table class. | |
3323 | // | |
3324 | ||
3325 | WX_DEFINE_OBJARRAY(wxGridStringArray) | |
3326 | ||
3327 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) | |
3328 | ||
3329 | wxGridStringTable::wxGridStringTable() | |
3330 | : wxGridTableBase() | |
3331 | { | |
3332 | } | |
3333 | ||
3334 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) | |
3335 | : wxGridTableBase() | |
3336 | { | |
3337 | m_data.Alloc( numRows ); | |
3338 | ||
3339 | wxArrayString sa; | |
3340 | sa.Alloc( numCols ); | |
3341 | sa.Add( wxEmptyString, numCols ); | |
3342 | ||
3343 | m_data.Add( sa, numRows ); | |
3344 | } | |
3345 | ||
3346 | wxGridStringTable::~wxGridStringTable() | |
3347 | { | |
3348 | } | |
3349 | ||
3350 | int wxGridStringTable::GetNumberRows() | |
3351 | { | |
3352 | return m_data.GetCount(); | |
3353 | } | |
3354 | ||
3355 | int wxGridStringTable::GetNumberCols() | |
3356 | { | |
3357 | if ( m_data.GetCount() > 0 ) | |
3358 | return m_data[0].GetCount(); | |
3359 | else | |
3360 | return 0; | |
3361 | } | |
3362 | ||
3363 | wxString wxGridStringTable::GetValue( int row, int col ) | |
3364 | { | |
3365 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), | |
3366 | wxEmptyString, | |
3367 | _T("invalid row or column index in wxGridStringTable") ); | |
3368 | ||
3369 | return m_data[row][col]; | |
3370 | } | |
3371 | ||
3372 | void wxGridStringTable::SetValue( int row, int col, const wxString& value ) | |
3373 | { | |
3374 | wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()), | |
3375 | _T("invalid row or column index in wxGridStringTable") ); | |
3376 | ||
3377 | m_data[row][col] = value; | |
3378 | } | |
3379 | ||
3380 | bool wxGridStringTable::IsEmptyCell( int row, int col ) | |
3381 | { | |
3382 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), | |
3383 | true, | |
3384 | _T("invalid row or column index in wxGridStringTable") ); | |
3385 | ||
3386 | return (m_data[row][col] == wxEmptyString); | |
3387 | } | |
3388 | ||
3389 | void wxGridStringTable::Clear() | |
3390 | { | |
3391 | int row, col; | |
3392 | int numRows, numCols; | |
3393 | ||
3394 | numRows = m_data.GetCount(); | |
3395 | if ( numRows > 0 ) | |
3396 | { | |
3397 | numCols = m_data[0].GetCount(); | |
3398 | ||
3399 | for ( row = 0; row < numRows; row++ ) | |
3400 | { | |
3401 | for ( col = 0; col < numCols; col++ ) | |
3402 | { | |
3403 | m_data[row][col] = wxEmptyString; | |
3404 | } | |
3405 | } | |
3406 | } | |
3407 | } | |
3408 | ||
3409 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) | |
3410 | { | |
3411 | size_t curNumRows = m_data.GetCount(); | |
3412 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : | |
3413 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
3414 | ||
3415 | if ( pos >= curNumRows ) | |
3416 | { | |
3417 | return AppendRows( numRows ); | |
3418 | } | |
3419 | ||
3420 | wxArrayString sa; | |
3421 | sa.Alloc( curNumCols ); | |
3422 | sa.Add( wxEmptyString, curNumCols ); | |
3423 | m_data.Insert( sa, pos, numRows ); | |
3424 | ||
3425 | if ( GetView() ) | |
3426 | { | |
3427 | wxGridTableMessage msg( this, | |
3428 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, | |
3429 | pos, | |
3430 | numRows ); | |
3431 | ||
3432 | GetView()->ProcessTableMessage( msg ); | |
3433 | } | |
3434 | ||
3435 | return true; | |
3436 | } | |
3437 | ||
3438 | bool wxGridStringTable::AppendRows( size_t numRows ) | |
3439 | { | |
3440 | size_t curNumRows = m_data.GetCount(); | |
3441 | size_t curNumCols = ( curNumRows > 0 | |
3442 | ? m_data[0].GetCount() | |
3443 | : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
3444 | ||
3445 | wxArrayString sa; | |
3446 | if ( curNumCols > 0 ) | |
3447 | { | |
3448 | sa.Alloc( curNumCols ); | |
3449 | sa.Add( wxEmptyString, curNumCols ); | |
3450 | } | |
3451 | ||
3452 | m_data.Add( sa, numRows ); | |
3453 | ||
3454 | if ( GetView() ) | |
3455 | { | |
3456 | wxGridTableMessage msg( this, | |
3457 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, | |
3458 | numRows ); | |
3459 | ||
3460 | GetView()->ProcessTableMessage( msg ); | |
3461 | } | |
3462 | ||
3463 | return true; | |
3464 | } | |
3465 | ||
3466 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) | |
3467 | { | |
3468 | size_t curNumRows = m_data.GetCount(); | |
3469 | ||
3470 | if ( pos >= curNumRows ) | |
3471 | { | |
3472 | wxFAIL_MSG( wxString::Format | |
3473 | ( | |
3474 | wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), | |
3475 | (unsigned long)pos, | |
3476 | (unsigned long)numRows, | |
3477 | (unsigned long)curNumRows | |
3478 | ) ); | |
3479 | ||
3480 | return false; | |
3481 | } | |
3482 | ||
3483 | if ( numRows > curNumRows - pos ) | |
3484 | { | |
3485 | numRows = curNumRows - pos; | |
3486 | } | |
3487 | ||
3488 | if ( numRows >= curNumRows ) | |
3489 | { | |
3490 | m_data.Clear(); | |
3491 | } | |
3492 | else | |
3493 | { | |
3494 | m_data.RemoveAt( pos, numRows ); | |
3495 | } | |
3496 | ||
3497 | if ( GetView() ) | |
3498 | { | |
3499 | wxGridTableMessage msg( this, | |
3500 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, | |
3501 | pos, | |
3502 | numRows ); | |
3503 | ||
3504 | GetView()->ProcessTableMessage( msg ); | |
3505 | } | |
3506 | ||
3507 | return true; | |
3508 | } | |
3509 | ||
3510 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) | |
3511 | { | |
3512 | size_t row, col; | |
3513 | ||
3514 | size_t curNumRows = m_data.GetCount(); | |
3515 | size_t curNumCols = ( curNumRows > 0 | |
3516 | ? m_data[0].GetCount() | |
3517 | : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
3518 | ||
3519 | if ( pos >= curNumCols ) | |
3520 | { | |
3521 | return AppendCols( numCols ); | |
3522 | } | |
3523 | ||
3524 | if ( !m_colLabels.IsEmpty() ) | |
3525 | { | |
3526 | m_colLabels.Insert( wxEmptyString, pos, numCols ); | |
3527 | ||
3528 | size_t i; | |
3529 | for ( i = pos; i < pos + numCols; i++ ) | |
3530 | m_colLabels[i] = wxGridTableBase::GetColLabelValue( i ); | |
3531 | } | |
3532 | ||
3533 | for ( row = 0; row < curNumRows; row++ ) | |
3534 | { | |
3535 | for ( col = pos; col < pos + numCols; col++ ) | |
3536 | { | |
3537 | m_data[row].Insert( wxEmptyString, col ); | |
3538 | } | |
3539 | } | |
3540 | ||
3541 | if ( GetView() ) | |
3542 | { | |
3543 | wxGridTableMessage msg( this, | |
3544 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, | |
3545 | pos, | |
3546 | numCols ); | |
3547 | ||
3548 | GetView()->ProcessTableMessage( msg ); | |
3549 | } | |
3550 | ||
3551 | return true; | |
3552 | } | |
3553 | ||
3554 | bool wxGridStringTable::AppendCols( size_t numCols ) | |
3555 | { | |
3556 | size_t row; | |
3557 | ||
3558 | size_t curNumRows = m_data.GetCount(); | |
3559 | ||
3560 | #if 0 | |
3561 | if ( !curNumRows ) | |
3562 | { | |
3563 | // TODO: something better than this ? | |
3564 | // | |
3565 | wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") ); | |
3566 | return false; | |
3567 | } | |
3568 | #endif | |
3569 | ||
3570 | for ( row = 0; row < curNumRows; row++ ) | |
3571 | { | |
3572 | m_data[row].Add( wxEmptyString, numCols ); | |
3573 | } | |
3574 | ||
3575 | if ( GetView() ) | |
3576 | { | |
3577 | wxGridTableMessage msg( this, | |
3578 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, | |
3579 | numCols ); | |
3580 | ||
3581 | GetView()->ProcessTableMessage( msg ); | |
3582 | } | |
3583 | ||
3584 | return true; | |
3585 | } | |
3586 | ||
3587 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) | |
3588 | { | |
3589 | size_t row; | |
3590 | ||
3591 | size_t curNumRows = m_data.GetCount(); | |
3592 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : | |
3593 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
3594 | ||
3595 | if ( pos >= curNumCols ) | |
3596 | { | |
3597 | wxFAIL_MSG( wxString::Format | |
3598 | ( | |
3599 | wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), | |
3600 | (unsigned long)pos, | |
3601 | (unsigned long)numCols, | |
3602 | (unsigned long)curNumCols | |
3603 | ) ); | |
3604 | return false; | |
3605 | } | |
3606 | ||
3607 | int colID; | |
3608 | if ( GetView() ) | |
3609 | colID = GetView()->GetColAt( pos ); | |
3610 | else | |
3611 | colID = pos; | |
3612 | ||
3613 | if ( numCols > curNumCols - colID ) | |
3614 | { | |
3615 | numCols = curNumCols - colID; | |
3616 | } | |
3617 | ||
3618 | if ( !m_colLabels.IsEmpty() ) | |
3619 | { | |
3620 | m_colLabels.RemoveAt( colID, numCols ); | |
3621 | } | |
3622 | ||
3623 | for ( row = 0; row < curNumRows; row++ ) | |
3624 | { | |
3625 | if ( numCols >= curNumCols ) | |
3626 | { | |
3627 | m_data[row].Clear(); | |
3628 | } | |
3629 | else | |
3630 | { | |
3631 | m_data[row].RemoveAt( colID, numCols ); | |
3632 | } | |
3633 | } | |
3634 | ||
3635 | if ( GetView() ) | |
3636 | { | |
3637 | wxGridTableMessage msg( this, | |
3638 | wxGRIDTABLE_NOTIFY_COLS_DELETED, | |
3639 | pos, | |
3640 | numCols ); | |
3641 | ||
3642 | GetView()->ProcessTableMessage( msg ); | |
3643 | } | |
3644 | ||
3645 | return true; | |
3646 | } | |
3647 | ||
3648 | wxString wxGridStringTable::GetRowLabelValue( int row ) | |
3649 | { | |
3650 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3651 | { | |
3652 | // using default label | |
3653 | // | |
3654 | return wxGridTableBase::GetRowLabelValue( row ); | |
3655 | } | |
3656 | else | |
3657 | { | |
3658 | return m_rowLabels[row]; | |
3659 | } | |
3660 | } | |
3661 | ||
3662 | wxString wxGridStringTable::GetColLabelValue( int col ) | |
3663 | { | |
3664 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3665 | { | |
3666 | // using default label | |
3667 | // | |
3668 | return wxGridTableBase::GetColLabelValue( col ); | |
3669 | } | |
3670 | else | |
3671 | { | |
3672 | return m_colLabels[col]; | |
3673 | } | |
3674 | } | |
3675 | ||
3676 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) | |
3677 | { | |
3678 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3679 | { | |
3680 | int n = m_rowLabels.GetCount(); | |
3681 | int i; | |
3682 | ||
3683 | for ( i = n; i <= row; i++ ) | |
3684 | { | |
3685 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); | |
3686 | } | |
3687 | } | |
3688 | ||
3689 | m_rowLabels[row] = value; | |
3690 | } | |
3691 | ||
3692 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) | |
3693 | { | |
3694 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3695 | { | |
3696 | int n = m_colLabels.GetCount(); | |
3697 | int i; | |
3698 | ||
3699 | for ( i = n; i <= col; i++ ) | |
3700 | { | |
3701 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); | |
3702 | } | |
3703 | } | |
3704 | ||
3705 | m_colLabels[col] = value; | |
3706 | } | |
3707 | ||
3708 | ||
3709 | ////////////////////////////////////////////////////////////////////// | |
3710 | ////////////////////////////////////////////////////////////////////// | |
3711 | ||
3712 | IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow ) | |
3713 | ||
3714 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow ) | |
3715 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) | |
3716 | EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel ) | |
3717 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) | |
3718 | EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown ) | |
3719 | EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp ) | |
3720 | EVT_CHAR( wxGridRowLabelWindow::OnChar ) | |
3721 | END_EVENT_TABLE() | |
3722 | ||
3723 | wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent, | |
3724 | wxWindowID id, | |
3725 | const wxPoint &pos, const wxSize &size ) | |
3726 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE ) | |
3727 | { | |
3728 | m_owner = parent; | |
3729 | } | |
3730 | ||
3731 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
3732 | { | |
3733 | wxPaintDC dc(this); | |
3734 | ||
3735 | // NO - don't do this because it will set both the x and y origin | |
3736 | // coords to match the parent scrolled window and we just want to | |
3737 | // set the y coord - MB | |
3738 | // | |
3739 | // m_owner->PrepareDC( dc ); | |
3740 | ||
3741 | int x, y; | |
3742 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); | |
3743 | wxPoint pt = dc.GetDeviceOrigin(); | |
3744 | dc.SetDeviceOrigin( pt.x, pt.y-y ); | |
3745 | ||
3746 | wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); | |
3747 | m_owner->DrawRowLabels( dc, rows ); | |
3748 | } | |
3749 | ||
3750 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3751 | { | |
3752 | m_owner->ProcessRowLabelMouseEvent( event ); | |
3753 | } | |
3754 | ||
3755 | void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event ) | |
3756 | { | |
3757 | m_owner->GetEventHandler()->ProcessEvent( event ); | |
3758 | } | |
3759 | ||
3760 | // This seems to be required for wxMotif otherwise the mouse | |
3761 | // cursor must be in the cell edit control to get key events | |
3762 | // | |
3763 | void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3764 | { | |
3765 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3766 | event.Skip(); | |
3767 | } | |
3768 | ||
3769 | void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event ) | |
3770 | { | |
3771 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3772 | event.Skip(); | |
3773 | } | |
3774 | ||
3775 | void wxGridRowLabelWindow::OnChar( wxKeyEvent& event ) | |
3776 | { | |
3777 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3778 | event.Skip(); | |
3779 | } | |
3780 | ||
3781 | ////////////////////////////////////////////////////////////////////// | |
3782 | ||
3783 | IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow ) | |
3784 | ||
3785 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow ) | |
3786 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) | |
3787 | EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel ) | |
3788 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) | |
3789 | EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown ) | |
3790 | EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp ) | |
3791 | EVT_CHAR( wxGridColLabelWindow::OnChar ) | |
3792 | END_EVENT_TABLE() | |
3793 | ||
3794 | wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent, | |
3795 | wxWindowID id, | |
3796 | const wxPoint &pos, const wxSize &size ) | |
3797 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE ) | |
3798 | { | |
3799 | m_owner = parent; | |
3800 | } | |
3801 | ||
3802 | void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
3803 | { | |
3804 | wxPaintDC dc(this); | |
3805 | ||
3806 | // NO - don't do this because it will set both the x and y origin | |
3807 | // coords to match the parent scrolled window and we just want to | |
3808 | // set the x coord - MB | |
3809 | // | |
3810 | // m_owner->PrepareDC( dc ); | |
3811 | ||
3812 | int x, y; | |
3813 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); | |
3814 | wxPoint pt = dc.GetDeviceOrigin(); | |
3815 | if (GetLayoutDirection() == wxLayout_RightToLeft) | |
3816 | dc.SetDeviceOrigin( pt.x+x, pt.y ); | |
3817 | else | |
3818 | dc.SetDeviceOrigin( pt.x-x, pt.y ); | |
3819 | ||
3820 | wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() ); | |
3821 | m_owner->DrawColLabels( dc, cols ); | |
3822 | } | |
3823 | ||
3824 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3825 | { | |
3826 | m_owner->ProcessColLabelMouseEvent( event ); | |
3827 | } | |
3828 | ||
3829 | void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event ) | |
3830 | { | |
3831 | m_owner->GetEventHandler()->ProcessEvent( event ); | |
3832 | } | |
3833 | ||
3834 | // This seems to be required for wxMotif otherwise the mouse | |
3835 | // cursor must be in the cell edit control to get key events | |
3836 | // | |
3837 | void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3838 | { | |
3839 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3840 | event.Skip(); | |
3841 | } | |
3842 | ||
3843 | void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event ) | |
3844 | { | |
3845 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3846 | event.Skip(); | |
3847 | } | |
3848 | ||
3849 | void wxGridColLabelWindow::OnChar( wxKeyEvent& event ) | |
3850 | { | |
3851 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3852 | event.Skip(); | |
3853 | } | |
3854 | ||
3855 | ////////////////////////////////////////////////////////////////////// | |
3856 | ||
3857 | IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow ) | |
3858 | ||
3859 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow ) | |
3860 | EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel ) | |
3861 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) | |
3862 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint ) | |
3863 | EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown ) | |
3864 | EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp ) | |
3865 | EVT_CHAR( wxGridCornerLabelWindow::OnChar ) | |
3866 | END_EVENT_TABLE() | |
3867 | ||
3868 | wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent, | |
3869 | wxWindowID id, | |
3870 | const wxPoint &pos, const wxSize &size ) | |
3871 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE ) | |
3872 | { | |
3873 | m_owner = parent; | |
3874 | } | |
3875 | ||
3876 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
3877 | { | |
3878 | wxPaintDC dc(this); | |
3879 | ||
3880 | int client_height = 0; | |
3881 | int client_width = 0; | |
3882 | GetClientSize( &client_width, &client_height ); | |
3883 | ||
3884 | // VZ: any reason for this ifdef? (FIXME) | |
3885 | #ifdef __WXGTK__ | |
3886 | wxRect rect; | |
3887 | rect.SetX( 1 ); | |
3888 | rect.SetY( 1 ); | |
3889 | rect.SetWidth( client_width - 2 ); | |
3890 | rect.SetHeight( client_height - 2 ); | |
3891 | ||
3892 | wxRendererNative::Get().DrawHeaderButton( this, dc, rect, 0 ); | |
3893 | #else // !__WXGTK__ | |
3894 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) ); | |
3895 | dc.DrawLine( client_width - 1, client_height - 1, client_width - 1, 0 ); | |
3896 | dc.DrawLine( client_width - 1, client_height - 1, 0, client_height - 1 ); | |
3897 | dc.DrawLine( 0, 0, client_width, 0 ); | |
3898 | dc.DrawLine( 0, 0, 0, client_height ); | |
3899 | ||
3900 | dc.SetPen( *wxWHITE_PEN ); | |
3901 | dc.DrawLine( 1, 1, client_width - 1, 1 ); | |
3902 | dc.DrawLine( 1, 1, 1, client_height - 1 ); | |
3903 | #endif | |
3904 | } | |
3905 | ||
3906 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3907 | { | |
3908 | m_owner->ProcessCornerLabelMouseEvent( event ); | |
3909 | } | |
3910 | ||
3911 | void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event ) | |
3912 | { | |
3913 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3914 | } | |
3915 | ||
3916 | // This seems to be required for wxMotif otherwise the mouse | |
3917 | // cursor must be in the cell edit control to get key events | |
3918 | // | |
3919 | void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3920 | { | |
3921 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3922 | event.Skip(); | |
3923 | } | |
3924 | ||
3925 | void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event ) | |
3926 | { | |
3927 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3928 | event.Skip(); | |
3929 | } | |
3930 | ||
3931 | void wxGridCornerLabelWindow::OnChar( wxKeyEvent& event ) | |
3932 | { | |
3933 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3934 | event.Skip(); | |
3935 | } | |
3936 | ||
3937 | ////////////////////////////////////////////////////////////////////// | |
3938 | ||
3939 | IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow ) | |
3940 | ||
3941 | BEGIN_EVENT_TABLE( wxGridWindow, wxWindow ) | |
3942 | EVT_PAINT( wxGridWindow::OnPaint ) | |
3943 | EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel ) | |
3944 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) | |
3945 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) | |
3946 | EVT_KEY_UP( wxGridWindow::OnKeyUp ) | |
3947 | EVT_CHAR( wxGridWindow::OnChar ) | |
3948 | EVT_SET_FOCUS( wxGridWindow::OnFocus ) | |
3949 | EVT_KILL_FOCUS( wxGridWindow::OnFocus ) | |
3950 | EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground ) | |
3951 | END_EVENT_TABLE() | |
3952 | ||
3953 | wxGridWindow::wxGridWindow( wxGrid *parent, | |
3954 | wxGridRowLabelWindow *rowLblWin, | |
3955 | wxGridColLabelWindow *colLblWin, | |
3956 | wxWindowID id, | |
3957 | const wxPoint &pos, | |
3958 | const wxSize &size ) | |
3959 | : wxWindow( | |
3960 | parent, id, pos, size, | |
3961 | wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN | wxFULL_REPAINT_ON_RESIZE, | |
3962 | wxT("grid window") ) | |
3963 | { | |
3964 | m_owner = parent; | |
3965 | m_rowLabelWin = rowLblWin; | |
3966 | m_colLabelWin = colLblWin; | |
3967 | } | |
3968 | ||
3969 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
3970 | { | |
3971 | wxPaintDC dc( this ); | |
3972 | m_owner->PrepareDC( dc ); | |
3973 | wxRegion reg = GetUpdateRegion(); | |
3974 | wxGridCellCoordsArray dirtyCells = m_owner->CalcCellsExposed( reg ); | |
3975 | m_owner->DrawGridCellArea( dc, dirtyCells ); | |
3976 | ||
3977 | #if WXGRID_DRAW_LINES | |
3978 | m_owner->DrawAllGridLines( dc, reg ); | |
3979 | #endif | |
3980 | ||
3981 | m_owner->DrawGridSpace( dc ); | |
3982 | m_owner->DrawHighlight( dc, dirtyCells ); | |
3983 | } | |
3984 | ||
3985 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
3986 | { | |
3987 | wxWindow::ScrollWindow( dx, dy, rect ); | |
3988 | m_rowLabelWin->ScrollWindow( 0, dy, rect ); | |
3989 | m_colLabelWin->ScrollWindow( dx, 0, rect ); | |
3990 | } | |
3991 | ||
3992 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) | |
3993 | { | |
3994 | if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this) | |
3995 | SetFocus(); | |
3996 | ||
3997 | m_owner->ProcessGridCellMouseEvent( event ); | |
3998 | } | |
3999 | ||
4000 | void wxGridWindow::OnMouseWheel( wxMouseEvent& event ) | |
4001 | { | |
4002 | m_owner->GetEventHandler()->ProcessEvent( event ); | |
4003 | } | |
4004 | ||
4005 | // This seems to be required for wxMotif/wxGTK otherwise the mouse | |
4006 | // cursor must be in the cell edit control to get key events | |
4007 | // | |
4008 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) | |
4009 | { | |
4010 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
4011 | event.Skip(); | |
4012 | } | |
4013 | ||
4014 | void wxGridWindow::OnKeyUp( wxKeyEvent& event ) | |
4015 | { | |
4016 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
4017 | event.Skip(); | |
4018 | } | |
4019 | ||
4020 | void wxGridWindow::OnChar( wxKeyEvent& event ) | |
4021 | { | |
4022 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
4023 | event.Skip(); | |
4024 | } | |
4025 | ||
4026 | void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) | |
4027 | { | |
4028 | } | |
4029 | ||
4030 | void wxGridWindow::OnFocus(wxFocusEvent& event) | |
4031 | { | |
4032 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
4033 | event.Skip(); | |
4034 | } | |
4035 | ||
4036 | ////////////////////////////////////////////////////////////////////// | |
4037 | ||
4038 | // Internal Helper function for computing row or column from some | |
4039 | // (unscrolled) coordinate value, using either | |
4040 | // m_defaultRowHeight/m_defaultColWidth or binary search on array | |
4041 | // of m_rowBottoms/m_ColRights to speed up the search! | |
4042 | ||
4043 | // Internal helper macros for simpler use of that function | |
4044 | ||
4045 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, | |
4046 | const wxArrayInt& BorderArray, int nMax, | |
4047 | bool clipToMinMax); | |
4048 | ||
4049 | #define internalXToCol(x) XToCol(x, true) | |
4050 | #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \ | |
4051 | m_minAcceptableRowHeight, \ | |
4052 | m_rowBottoms, m_numRows, true) | |
4053 | ||
4054 | ///////////////////////////////////////////////////////////////////// | |
4055 | ||
4056 | #if wxUSE_EXTENDED_RTTI | |
4057 | WX_DEFINE_FLAGS( wxGridStyle ) | |
4058 | ||
4059 | wxBEGIN_FLAGS( wxGridStyle ) | |
4060 | // new style border flags, we put them first to | |
4061 | // use them for streaming out | |
4062 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
4063 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
4064 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
4065 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
4066 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
4067 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
4068 | ||
4069 | // old style border flags | |
4070 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
4071 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
4072 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
4073 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
4074 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
4075 | wxFLAGS_MEMBER(wxBORDER) | |
4076 | ||
4077 | // standard window styles | |
4078 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
4079 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
4080 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
4081 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
4082 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
4083 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB) | |
4084 | wxFLAGS_MEMBER(wxVSCROLL) | |
4085 | wxFLAGS_MEMBER(wxHSCROLL) | |
4086 | ||
4087 | wxEND_FLAGS( wxGridStyle ) | |
4088 | ||
4089 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h") | |
4090 | ||
4091 | wxBEGIN_PROPERTIES_TABLE(wxGrid) | |
4092 | wxHIDE_PROPERTY( Children ) | |
4093 | wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
4094 | wxEND_PROPERTIES_TABLE() | |
4095 | ||
4096 | wxBEGIN_HANDLERS_TABLE(wxGrid) | |
4097 | wxEND_HANDLERS_TABLE() | |
4098 | ||
4099 | wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) | |
4100 | ||
4101 | /* | |
4102 | TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo) | |
4103 | */ | |
4104 | #else | |
4105 | IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow ) | |
4106 | #endif | |
4107 | ||
4108 | BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow ) | |
4109 | EVT_PAINT( wxGrid::OnPaint ) | |
4110 | EVT_SIZE( wxGrid::OnSize ) | |
4111 | EVT_KEY_DOWN( wxGrid::OnKeyDown ) | |
4112 | EVT_KEY_UP( wxGrid::OnKeyUp ) | |
4113 | EVT_CHAR ( wxGrid::OnChar ) | |
4114 | EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground ) | |
4115 | END_EVENT_TABLE() | |
4116 | ||
4117 | wxGrid::wxGrid() | |
4118 | { | |
4119 | // in order to make sure that a size event is not | |
4120 | // trigerred in a unfinished state | |
4121 | m_cornerLabelWin = NULL; | |
4122 | m_rowLabelWin = NULL; | |
4123 | m_colLabelWin = NULL; | |
4124 | m_gridWin = NULL; | |
4125 | } | |
4126 | ||
4127 | wxGrid::wxGrid( wxWindow *parent, | |
4128 | wxWindowID id, | |
4129 | const wxPoint& pos, | |
4130 | const wxSize& size, | |
4131 | long style, | |
4132 | const wxString& name ) | |
4133 | : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ), | |
4134 | m_colMinWidths(GRID_HASH_SIZE), | |
4135 | m_rowMinHeights(GRID_HASH_SIZE) | |
4136 | { | |
4137 | Create(); | |
4138 | SetBestFittingSize(size); | |
4139 | } | |
4140 | ||
4141 | bool wxGrid::Create(wxWindow *parent, wxWindowID id, | |
4142 | const wxPoint& pos, const wxSize& size, | |
4143 | long style, const wxString& name) | |
4144 | { | |
4145 | if (!wxScrolledWindow::Create(parent, id, pos, size, | |
4146 | style | wxWANTS_CHARS, name)) | |
4147 | return false; | |
4148 | ||
4149 | m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE); | |
4150 | m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE); | |
4151 | ||
4152 | Create(); | |
4153 | SetBestFittingSize(size); | |
4154 | ||
4155 | return true; | |
4156 | } | |
4157 | ||
4158 | wxGrid::~wxGrid() | |
4159 | { | |
4160 | // Must do this or ~wxScrollHelper will pop the wrong event handler | |
4161 | SetTargetWindow(this); | |
4162 | ClearAttrCache(); | |
4163 | wxSafeDecRef(m_defaultCellAttr); | |
4164 | ||
4165 | #ifdef DEBUG_ATTR_CACHE | |
4166 | size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses; | |
4167 | wxPrintf(_T("wxGrid attribute cache statistics: " | |
4168 | "total: %u, hits: %u (%u%%)\n"), | |
4169 | total, gs_nAttrCacheHits, | |
4170 | total ? (gs_nAttrCacheHits*100) / total : 0); | |
4171 | #endif | |
4172 | ||
4173 | if (m_ownTable) | |
4174 | delete m_table; | |
4175 | ||
4176 | delete m_typeRegistry; | |
4177 | delete m_selection; | |
4178 | } | |
4179 | ||
4180 | // | |
4181 | // ----- internal init and update functions | |
4182 | // | |
4183 | ||
4184 | // NOTE: If using the default visual attributes works everywhere then this can | |
4185 | // be removed as well as the #else cases below. | |
4186 | #define _USE_VISATTR 0 | |
4187 | ||
4188 | void wxGrid::Create() | |
4189 | { | |
4190 | // set to true by CreateGrid | |
4191 | m_created = false; | |
4192 | ||
4193 | // create the type registry | |
4194 | m_typeRegistry = new wxGridTypeRegistry; | |
4195 | m_selection = NULL; | |
4196 | ||
4197 | m_table = (wxGridTableBase *) NULL; | |
4198 | m_ownTable = false; | |
4199 | ||
4200 | m_cellEditCtrlEnabled = false; | |
4201 | ||
4202 | m_defaultCellAttr = new wxGridCellAttr(); | |
4203 | ||
4204 | // Set default cell attributes | |
4205 | m_defaultCellAttr->SetDefAttr(m_defaultCellAttr); | |
4206 | m_defaultCellAttr->SetKind(wxGridCellAttr::Default); | |
4207 | m_defaultCellAttr->SetFont(GetFont()); | |
4208 | m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP); | |
4209 | m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer); | |
4210 | m_defaultCellAttr->SetEditor(new wxGridCellTextEditor); | |
4211 | ||
4212 | #if _USE_VISATTR | |
4213 | wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes(); | |
4214 | wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes(); | |
4215 | ||
4216 | m_defaultCellAttr->SetTextColour(gva.colFg); | |
4217 | m_defaultCellAttr->SetBackgroundColour(gva.colBg); | |
4218 | ||
4219 | #else | |
4220 | m_defaultCellAttr->SetTextColour( | |
4221 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); | |
4222 | m_defaultCellAttr->SetBackgroundColour( | |
4223 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
4224 | #endif | |
4225 | ||
4226 | m_numRows = 0; | |
4227 | m_numCols = 0; | |
4228 | m_currentCellCoords = wxGridNoCellCoords; | |
4229 | ||
4230 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; | |
4231 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
4232 | ||
4233 | // subwindow components that make up the wxGrid | |
4234 | m_rowLabelWin = new wxGridRowLabelWindow( this, | |
4235 | wxID_ANY, | |
4236 | wxDefaultPosition, | |
4237 | wxDefaultSize ); | |
4238 | ||
4239 | m_colLabelWin = new wxGridColLabelWindow( this, | |
4240 | wxID_ANY, | |
4241 | wxDefaultPosition, | |
4242 | wxDefaultSize ); | |
4243 | ||
4244 | m_cornerLabelWin = new wxGridCornerLabelWindow( this, | |
4245 | wxID_ANY, | |
4246 | wxDefaultPosition, | |
4247 | wxDefaultSize ); | |
4248 | ||
4249 | m_gridWin = new wxGridWindow( this, | |
4250 | m_rowLabelWin, | |
4251 | m_colLabelWin, | |
4252 | wxID_ANY, | |
4253 | wxDefaultPosition, | |
4254 | wxDefaultSize ); | |
4255 | ||
4256 | SetTargetWindow( m_gridWin ); | |
4257 | ||
4258 | #if _USE_VISATTR | |
4259 | wxColour gfg = gva.colFg; | |
4260 | wxColour gbg = gva.colBg; | |
4261 | wxColour lfg = lva.colFg; | |
4262 | wxColour lbg = lva.colBg; | |
4263 | #else | |
4264 | wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
4265 | wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ); | |
4266 | wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
4267 | wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ); | |
4268 | #endif | |
4269 | ||
4270 | m_cornerLabelWin->SetOwnForegroundColour(lfg); | |
4271 | m_cornerLabelWin->SetOwnBackgroundColour(lbg); | |
4272 | m_rowLabelWin->SetOwnForegroundColour(lfg); | |
4273 | m_rowLabelWin->SetOwnBackgroundColour(lbg); | |
4274 | m_colLabelWin->SetOwnForegroundColour(lfg); | |
4275 | m_colLabelWin->SetOwnBackgroundColour(lbg); | |
4276 | ||
4277 | m_gridWin->SetOwnForegroundColour(gfg); | |
4278 | m_gridWin->SetOwnBackgroundColour(gbg); | |
4279 | ||
4280 | Init(); | |
4281 | } | |
4282 | ||
4283 | bool wxGrid::CreateGrid( int numRows, int numCols, | |
4284 | wxGrid::wxGridSelectionModes selmode ) | |
4285 | { | |
4286 | wxCHECK_MSG( !m_created, | |
4287 | false, | |
4288 | wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); | |
4289 | ||
4290 | m_numRows = numRows; | |
4291 | m_numCols = numCols; | |
4292 | ||
4293 | m_table = new wxGridStringTable( m_numRows, m_numCols ); | |
4294 | m_table->SetView( this ); | |
4295 | m_ownTable = true; | |
4296 | m_selection = new wxGridSelection( this, selmode ); | |
4297 | ||
4298 | CalcDimensions(); | |
4299 | ||
4300 | m_created = true; | |
4301 | ||
4302 | return m_created; | |
4303 | } | |
4304 | ||
4305 | void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode) | |
4306 | { | |
4307 | wxCHECK_RET( m_created, | |
4308 | wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") ); | |
4309 | ||
4310 | m_selection->SetSelectionMode( selmode ); | |
4311 | } | |
4312 | ||
4313 | wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const | |
4314 | { | |
4315 | wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells, | |
4316 | wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") ); | |
4317 | ||
4318 | return m_selection->GetSelectionMode(); | |
4319 | } | |
4320 | ||
4321 | bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership, | |
4322 | wxGrid::wxGridSelectionModes selmode ) | |
4323 | { | |
4324 | if ( m_created ) | |
4325 | { | |
4326 | // stop all processing | |
4327 | m_created = false; | |
4328 | ||
4329 | if (m_ownTable) | |
4330 | { | |
4331 | wxGridTableBase *t = m_table; | |
4332 | m_table = NULL; | |
4333 | delete t; | |
4334 | } | |
4335 | ||
4336 | delete m_selection; | |
4337 | ||
4338 | m_table = NULL; | |
4339 | m_selection = NULL; | |
4340 | m_numRows = 0; | |
4341 | m_numCols = 0; | |
4342 | } | |
4343 | ||
4344 | if (table) | |
4345 | { | |
4346 | m_numRows = table->GetNumberRows(); | |
4347 | m_numCols = table->GetNumberCols(); | |
4348 | ||
4349 | m_table = table; | |
4350 | m_table->SetView( this ); | |
4351 | m_ownTable = takeOwnership; | |
4352 | m_selection = new wxGridSelection( this, selmode ); | |
4353 | ||
4354 | CalcDimensions(); | |
4355 | ||
4356 | m_created = true; | |
4357 | } | |
4358 | ||
4359 | return m_created; | |
4360 | } | |
4361 | ||
4362 | void wxGrid::Init() | |
4363 | { | |
4364 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; | |
4365 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
4366 | ||
4367 | if ( m_rowLabelWin ) | |
4368 | { | |
4369 | m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour(); | |
4370 | } | |
4371 | else | |
4372 | { | |
4373 | m_labelBackgroundColour = *wxWHITE; | |
4374 | } | |
4375 | ||
4376 | m_labelTextColour = *wxBLACK; | |
4377 | ||
4378 | // init attr cache | |
4379 | m_attrCache.row = -1; | |
4380 | m_attrCache.col = -1; | |
4381 | m_attrCache.attr = NULL; | |
4382 | ||
4383 | // TODO: something better than this ? | |
4384 | // | |
4385 | m_labelFont = this->GetFont(); | |
4386 | m_labelFont.SetWeight( wxBOLD ); | |
4387 | ||
4388 | m_rowLabelHorizAlign = wxALIGN_CENTRE; | |
4389 | m_rowLabelVertAlign = wxALIGN_CENTRE; | |
4390 | ||
4391 | m_colLabelHorizAlign = wxALIGN_CENTRE; | |
4392 | m_colLabelVertAlign = wxALIGN_CENTRE; | |
4393 | m_colLabelTextOrientation = wxHORIZONTAL; | |
4394 | ||
4395 | m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH; | |
4396 | m_defaultRowHeight = m_gridWin->GetCharHeight(); | |
4397 | ||
4398 | m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH; | |
4399 | m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT; | |
4400 | ||
4401 | #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl() | |
4402 | m_defaultRowHeight += 8; | |
4403 | #else | |
4404 | m_defaultRowHeight += 4; | |
4405 | #endif | |
4406 | ||
4407 | m_gridLineColour = wxColour( 192,192,192 ); | |
4408 | m_gridLinesEnabled = true; | |
4409 | m_cellHighlightColour = *wxBLACK; | |
4410 | m_cellHighlightPenWidth = 2; | |
4411 | m_cellHighlightROPenWidth = 1; | |
4412 | ||
4413 | m_canDragColMove = false; | |
4414 | ||
4415 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; | |
4416 | m_winCapture = (wxWindow *)NULL; | |
4417 | m_canDragRowSize = true; | |
4418 | m_canDragColSize = true; | |
4419 | m_canDragGridSize = true; | |
4420 | m_canDragCell = false; | |
4421 | m_dragLastPos = -1; | |
4422 | m_dragRowOrCol = -1; | |
4423 | m_isDragging = false; | |
4424 | m_startDragPos = wxDefaultPosition; | |
4425 | ||
4426 | m_waitForSlowClick = false; | |
4427 | ||
4428 | m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS ); | |
4429 | m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE ); | |
4430 | ||
4431 | m_currentCellCoords = wxGridNoCellCoords; | |
4432 | ||
4433 | ClearSelection(); | |
4434 | ||
4435 | m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); | |
4436 | m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
4437 | ||
4438 | m_editable = true; // default for whole grid | |
4439 | ||
4440 | m_inOnKeyDown = false; | |
4441 | m_batchCount = 0; | |
4442 | ||
4443 | m_extraWidth = | |
4444 | m_extraHeight = 0; | |
4445 | ||
4446 | m_scrollLineX = GRID_SCROLL_LINE_X; | |
4447 | m_scrollLineY = GRID_SCROLL_LINE_Y; | |
4448 | } | |
4449 | ||
4450 | // ---------------------------------------------------------------------------- | |
4451 | // the idea is to call these functions only when necessary because they create | |
4452 | // quite big arrays which eat memory mostly unnecessary - in particular, if | |
4453 | // default widths/heights are used for all rows/columns, we may not use these | |
4454 | // arrays at all | |
4455 | // | |
4456 | // with some extra code, it should be possible to only store the | |
4457 | // widths/heights different from default ones but this will be done later... | |
4458 | // ---------------------------------------------------------------------------- | |
4459 | ||
4460 | void wxGrid::InitRowHeights() | |
4461 | { | |
4462 | m_rowHeights.Empty(); | |
4463 | m_rowBottoms.Empty(); | |
4464 | ||
4465 | m_rowHeights.Alloc( m_numRows ); | |
4466 | m_rowBottoms.Alloc( m_numRows ); | |
4467 | ||
4468 | int rowBottom = 0; | |
4469 | ||
4470 | m_rowHeights.Add( m_defaultRowHeight, m_numRows ); | |
4471 | ||
4472 | for ( int i = 0; i < m_numRows; i++ ) | |
4473 | { | |
4474 | rowBottom += m_defaultRowHeight; | |
4475 | m_rowBottoms.Add( rowBottom ); | |
4476 | } | |
4477 | } | |
4478 | ||
4479 | void wxGrid::InitColWidths() | |
4480 | { | |
4481 | m_colWidths.Empty(); | |
4482 | m_colRights.Empty(); | |
4483 | ||
4484 | m_colWidths.Alloc( m_numCols ); | |
4485 | m_colRights.Alloc( m_numCols ); | |
4486 | int colRight = 0; | |
4487 | ||
4488 | m_colWidths.Add( m_defaultColWidth, m_numCols ); | |
4489 | ||
4490 | for ( int i = 0; i < m_numCols; i++ ) | |
4491 | { | |
4492 | colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth; | |
4493 | m_colRights.Add( colRight ); | |
4494 | } | |
4495 | } | |
4496 | ||
4497 | int wxGrid::GetColWidth(int col) const | |
4498 | { | |
4499 | return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col]; | |
4500 | } | |
4501 | ||
4502 | int wxGrid::GetColLeft(int col) const | |
4503 | { | |
4504 | return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth | |
4505 | : m_colRights[col] - m_colWidths[col]; | |
4506 | } | |
4507 | ||
4508 | int wxGrid::GetColRight(int col) const | |
4509 | { | |
4510 | return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth | |
4511 | : m_colRights[col]; | |
4512 | } | |
4513 | ||
4514 | int wxGrid::GetRowHeight(int row) const | |
4515 | { | |
4516 | return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row]; | |
4517 | } | |
4518 | ||
4519 | int wxGrid::GetRowTop(int row) const | |
4520 | { | |
4521 | return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight | |
4522 | : m_rowBottoms[row] - m_rowHeights[row]; | |
4523 | } | |
4524 | ||
4525 | int wxGrid::GetRowBottom(int row) const | |
4526 | { | |
4527 | return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight | |
4528 | : m_rowBottoms[row]; | |
4529 | } | |
4530 | ||
4531 | void wxGrid::CalcDimensions() | |
4532 | { | |
4533 | int cw, ch; | |
4534 | GetClientSize( &cw, &ch ); | |
4535 | ||
4536 | if ( m_rowLabelWin->IsShown() ) | |
4537 | cw -= m_rowLabelWidth; | |
4538 | if ( m_colLabelWin->IsShown() ) | |
4539 | ch -= m_colLabelHeight; | |
4540 | ||
4541 | // grid total size | |
4542 | int w = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) + m_extraWidth + 1 : 0; | |
4543 | int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0; | |
4544 | ||
4545 | // take into account editor if shown | |
4546 | if ( IsCellEditControlShown() ) | |
4547 | { | |
4548 | int w2, h2; | |
4549 | int r = m_currentCellCoords.GetRow(); | |
4550 | int c = m_currentCellCoords.GetCol(); | |
4551 | int x = GetColLeft(c); | |
4552 | int y = GetRowTop(r); | |
4553 | ||
4554 | // how big is the editor | |
4555 | wxGridCellAttr* attr = GetCellAttr(r, c); | |
4556 | wxGridCellEditor* editor = attr->GetEditor(this, r, c); | |
4557 | editor->GetControl()->GetSize(&w2, &h2); | |
4558 | w2 += x; | |
4559 | h2 += y; | |
4560 | if ( w2 > w ) | |
4561 | w = w2; | |
4562 | if ( h2 > h ) | |
4563 | h = h2; | |
4564 | editor->DecRef(); | |
4565 | attr->DecRef(); | |
4566 | } | |
4567 | ||
4568 | // preserve (more or less) the previous position | |
4569 | int x, y; | |
4570 | GetViewStart( &x, &y ); | |
4571 | ||
4572 | // ensure the position is valid for the new scroll ranges | |
4573 | if ( x >= w ) | |
4574 | x = wxMax( w - 1, 0 ); | |
4575 | if ( y >= h ) | |
4576 | y = wxMax( h - 1, 0 ); | |
4577 | ||
4578 | // do set scrollbar parameters | |
4579 | SetScrollbars( m_scrollLineX, m_scrollLineY, | |
4580 | GetScrollX(w), GetScrollY(h), x, y, | |
4581 | GetBatchCount() != 0); | |
4582 | ||
4583 | // if our OnSize() hadn't been called (it would if we have scrollbars), we | |
4584 | // still must reposition the children | |
4585 | CalcWindowSizes(); | |
4586 | } | |
4587 | ||
4588 | void wxGrid::CalcWindowSizes() | |
4589 | { | |
4590 | // escape if the window is has not been fully created yet | |
4591 | ||
4592 | if ( m_cornerLabelWin == NULL ) | |
4593 | return; | |
4594 | ||
4595 | int cw, ch; | |
4596 | GetClientSize( &cw, &ch ); | |
4597 | ||
4598 | if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() ) | |
4599 | m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight ); | |
4600 | ||
4601 | if ( m_colLabelWin && m_colLabelWin->IsShown() ) | |
4602 | m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw - m_rowLabelWidth, m_colLabelHeight ); | |
4603 | ||
4604 | if ( m_rowLabelWin && m_rowLabelWin->IsShown() ) | |
4605 | m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch - m_colLabelHeight ); | |
4606 | ||
4607 | if ( m_gridWin && m_gridWin->IsShown() ) | |
4608 | m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw - m_rowLabelWidth, ch - m_colLabelHeight ); | |
4609 | } | |
4610 | ||
4611 | // this is called when the grid table sends a message | |
4612 | // to indicate that it has been redimensioned | |
4613 | // | |
4614 | bool wxGrid::Redimension( wxGridTableMessage& msg ) | |
4615 | { | |
4616 | int i; | |
4617 | bool result = false; | |
4618 | ||
4619 | // Clear the attribute cache as the attribute might refer to a different | |
4620 | // cell than stored in the cache after adding/removing rows/columns. | |
4621 | ClearAttrCache(); | |
4622 | ||
4623 | // By the same reasoning, the editor should be dismissed if columns are | |
4624 | // added or removed. And for consistency, it should IMHO always be | |
4625 | // removed, not only if the cell "underneath" it actually changes. | |
4626 | // For now, I intentionally do not save the editor's content as the | |
4627 | // cell it might want to save that stuff to might no longer exist. | |
4628 | HideCellEditControl(); | |
4629 | ||
4630 | #if 0 | |
4631 | // if we were using the default widths/heights so far, we must change them | |
4632 | // now | |
4633 | if ( m_colWidths.IsEmpty() ) | |
4634 | { | |
4635 | InitColWidths(); | |
4636 | } | |
4637 | ||
4638 | if ( m_rowHeights.IsEmpty() ) | |
4639 | { | |
4640 | InitRowHeights(); | |
4641 | } | |
4642 | #endif | |
4643 | ||
4644 | switch ( msg.GetId() ) | |
4645 | { | |
4646 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
4647 | { | |
4648 | size_t pos = msg.GetCommandInt(); | |
4649 | int numRows = msg.GetCommandInt2(); | |
4650 | ||
4651 | m_numRows += numRows; | |
4652 | ||
4653 | if ( !m_rowHeights.IsEmpty() ) | |
4654 | { | |
4655 | m_rowHeights.Insert( m_defaultRowHeight, pos, numRows ); | |
4656 | m_rowBottoms.Insert( 0, pos, numRows ); | |
4657 | ||
4658 | int bottom = 0; | |
4659 | if ( pos > 0 ) | |
4660 | bottom = m_rowBottoms[pos - 1]; | |
4661 | ||
4662 | for ( i = pos; i < m_numRows; i++ ) | |
4663 | { | |
4664 | bottom += m_rowHeights[i]; | |
4665 | m_rowBottoms[i] = bottom; | |
4666 | } | |
4667 | } | |
4668 | ||
4669 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4670 | { | |
4671 | // if we have just inserted cols into an empty grid the current | |
4672 | // cell will be undefined... | |
4673 | // | |
4674 | SetCurrentCell( 0, 0 ); | |
4675 | } | |
4676 | ||
4677 | if ( m_selection ) | |
4678 | m_selection->UpdateRows( pos, numRows ); | |
4679 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); | |
4680 | if (attrProvider) | |
4681 | attrProvider->UpdateAttrRows( pos, numRows ); | |
4682 | ||
4683 | if ( !GetBatchCount() ) | |
4684 | { | |
4685 | CalcDimensions(); | |
4686 | m_rowLabelWin->Refresh(); | |
4687 | } | |
4688 | } | |
4689 | result = true; | |
4690 | break; | |
4691 | ||
4692 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
4693 | { | |
4694 | int numRows = msg.GetCommandInt(); | |
4695 | int oldNumRows = m_numRows; | |
4696 | m_numRows += numRows; | |
4697 | ||
4698 | if ( !m_rowHeights.IsEmpty() ) | |
4699 | { | |
4700 | m_rowHeights.Add( m_defaultRowHeight, numRows ); | |
4701 | m_rowBottoms.Add( 0, numRows ); | |
4702 | ||
4703 | int bottom = 0; | |
4704 | if ( oldNumRows > 0 ) | |
4705 | bottom = m_rowBottoms[oldNumRows - 1]; | |
4706 | ||
4707 | for ( i = oldNumRows; i < m_numRows; i++ ) | |
4708 | { | |
4709 | bottom += m_rowHeights[i]; | |
4710 | m_rowBottoms[i] = bottom; | |
4711 | } | |
4712 | } | |
4713 | ||
4714 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4715 | { | |
4716 | // if we have just inserted cols into an empty grid the current | |
4717 | // cell will be undefined... | |
4718 | // | |
4719 | SetCurrentCell( 0, 0 ); | |
4720 | } | |
4721 | ||
4722 | if ( !GetBatchCount() ) | |
4723 | { | |
4724 | CalcDimensions(); | |
4725 | m_rowLabelWin->Refresh(); | |
4726 | } | |
4727 | } | |
4728 | result = true; | |
4729 | break; | |
4730 | ||
4731 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
4732 | { | |
4733 | size_t pos = msg.GetCommandInt(); | |
4734 | int numRows = msg.GetCommandInt2(); | |
4735 | m_numRows -= numRows; | |
4736 | ||
4737 | if ( !m_rowHeights.IsEmpty() ) | |
4738 | { | |
4739 | m_rowHeights.RemoveAt( pos, numRows ); | |
4740 | m_rowBottoms.RemoveAt( pos, numRows ); | |
4741 | ||
4742 | int h = 0; | |
4743 | for ( i = 0; i < m_numRows; i++ ) | |
4744 | { | |
4745 | h += m_rowHeights[i]; | |
4746 | m_rowBottoms[i] = h; | |
4747 | } | |
4748 | } | |
4749 | ||
4750 | if ( !m_numRows ) | |
4751 | { | |
4752 | m_currentCellCoords = wxGridNoCellCoords; | |
4753 | } | |
4754 | else | |
4755 | { | |
4756 | if ( m_currentCellCoords.GetRow() >= m_numRows ) | |
4757 | m_currentCellCoords.Set( 0, 0 ); | |
4758 | } | |
4759 | ||
4760 | if ( m_selection ) | |
4761 | m_selection->UpdateRows( pos, -((int)numRows) ); | |
4762 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); | |
4763 | if (attrProvider) | |
4764 | { | |
4765 | attrProvider->UpdateAttrRows( pos, -((int)numRows) ); | |
4766 | ||
4767 | // ifdef'd out following patch from Paul Gammans | |
4768 | #if 0 | |
4769 | // No need to touch column attributes, unless we | |
4770 | // removed _all_ rows, in this case, we remove | |
4771 | // all column attributes. | |
4772 | // I hate to do this here, but the | |
4773 | // needed data is not available inside UpdateAttrRows. | |
4774 | if ( !GetNumberRows() ) | |
4775 | attrProvider->UpdateAttrCols( 0, -GetNumberCols() ); | |
4776 | #endif | |
4777 | } | |
4778 | ||
4779 | if ( !GetBatchCount() ) | |
4780 | { | |
4781 | CalcDimensions(); | |
4782 | m_rowLabelWin->Refresh(); | |
4783 | } | |
4784 | } | |
4785 | result = true; | |
4786 | break; | |
4787 | ||
4788 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
4789 | { | |
4790 | size_t pos = msg.GetCommandInt(); | |
4791 | int numCols = msg.GetCommandInt2(); | |
4792 | m_numCols += numCols; | |
4793 | ||
4794 | if ( !m_colAt.IsEmpty() ) | |
4795 | { | |
4796 | //Shift the column IDs | |
4797 | int i; | |
4798 | for ( i = 0; i < m_numCols - numCols; i++ ) | |
4799 | { | |
4800 | if ( m_colAt[i] >= (int)pos ) | |
4801 | m_colAt[i] += numCols; | |
4802 | } | |
4803 | ||
4804 | m_colAt.Insert( pos, pos, numCols ); | |
4805 | ||
4806 | //Set the new columns' positions | |
4807 | for ( i = pos + 1; i < (int)pos + numCols; i++ ) | |
4808 | { | |
4809 | m_colAt[i] = i; | |
4810 | } | |
4811 | } | |
4812 | ||
4813 | if ( !m_colWidths.IsEmpty() ) | |
4814 | { | |
4815 | m_colWidths.Insert( m_defaultColWidth, pos, numCols ); | |
4816 | m_colRights.Insert( 0, pos, numCols ); | |
4817 | ||
4818 | int right = 0; | |
4819 | if ( pos > 0 ) | |
4820 | right = m_colRights[GetColAt( pos - 1 )]; | |
4821 | ||
4822 | int colPos; | |
4823 | for ( colPos = pos; colPos < m_numCols; colPos++ ) | |
4824 | { | |
4825 | i = GetColAt( colPos ); | |
4826 | ||
4827 | right += m_colWidths[i]; | |
4828 | m_colRights[i] = right; | |
4829 | } | |
4830 | } | |
4831 | ||
4832 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4833 | { | |
4834 | // if we have just inserted cols into an empty grid the current | |
4835 | // cell will be undefined... | |
4836 | // | |
4837 | SetCurrentCell( 0, 0 ); | |
4838 | } | |
4839 | ||
4840 | if ( m_selection ) | |
4841 | m_selection->UpdateCols( pos, numCols ); | |
4842 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); | |
4843 | if (attrProvider) | |
4844 | attrProvider->UpdateAttrCols( pos, numCols ); | |
4845 | if ( !GetBatchCount() ) | |
4846 | { | |
4847 | CalcDimensions(); | |
4848 | m_colLabelWin->Refresh(); | |
4849 | } | |
4850 | } | |
4851 | result = true; | |
4852 | break; | |
4853 | ||
4854 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
4855 | { | |
4856 | int numCols = msg.GetCommandInt(); | |
4857 | int oldNumCols = m_numCols; | |
4858 | m_numCols += numCols; | |
4859 | ||
4860 | if ( !m_colAt.IsEmpty() ) | |
4861 | { | |
4862 | m_colAt.Add( 0, numCols ); | |
4863 | ||
4864 | //Set the new columns' positions | |
4865 | int i; | |
4866 | for ( i = oldNumCols; i < m_numCols; i++ ) | |
4867 | { | |
4868 | m_colAt[i] = i; | |
4869 | } | |
4870 | } | |
4871 | ||
4872 | if ( !m_colWidths.IsEmpty() ) | |
4873 | { | |
4874 | m_colWidths.Add( m_defaultColWidth, numCols ); | |
4875 | m_colRights.Add( 0, numCols ); | |
4876 | ||
4877 | int right = 0; | |
4878 | if ( oldNumCols > 0 ) | |
4879 | right = m_colRights[GetColAt( oldNumCols - 1 )]; | |
4880 | ||
4881 | int colPos; | |
4882 | for ( colPos = oldNumCols; colPos < m_numCols; colPos++ ) | |
4883 | { | |
4884 | i = GetColAt( colPos ); | |
4885 | ||
4886 | right += m_colWidths[i]; | |
4887 | m_colRights[i] = right; | |
4888 | } | |
4889 | } | |
4890 | ||
4891 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4892 | { | |
4893 | // if we have just inserted cols into an empty grid the current | |
4894 | // cell will be undefined... | |
4895 | // | |
4896 | SetCurrentCell( 0, 0 ); | |
4897 | } | |
4898 | if ( !GetBatchCount() ) | |
4899 | { | |
4900 | CalcDimensions(); | |
4901 | m_colLabelWin->Refresh(); | |
4902 | } | |
4903 | } | |
4904 | result = true; | |
4905 | break; | |
4906 | ||
4907 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
4908 | { | |
4909 | size_t pos = msg.GetCommandInt(); | |
4910 | int numCols = msg.GetCommandInt2(); | |
4911 | m_numCols -= numCols; | |
4912 | ||
4913 | if ( !m_colAt.IsEmpty() ) | |
4914 | { | |
4915 | int colID = GetColAt( pos ); | |
4916 | ||
4917 | m_colAt.RemoveAt( pos, numCols ); | |
4918 | ||
4919 | //Shift the column IDs | |
4920 | int colPos; | |
4921 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
4922 | { | |
4923 | if ( m_colAt[colPos] > colID ) | |
4924 | m_colAt[colPos] -= numCols; | |
4925 | } | |
4926 | } | |
4927 | ||
4928 | if ( !m_colWidths.IsEmpty() ) | |
4929 | { | |
4930 | m_colWidths.RemoveAt( pos, numCols ); | |
4931 | m_colRights.RemoveAt( pos, numCols ); | |
4932 | ||
4933 | int w = 0; | |
4934 | int colPos; | |
4935 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
4936 | { | |
4937 | i = GetColAt( colPos ); | |
4938 | ||
4939 | w += m_colWidths[i]; | |
4940 | m_colRights[i] = w; | |
4941 | } | |
4942 | } | |
4943 | ||
4944 | if ( !m_numCols ) | |
4945 | { | |
4946 | m_currentCellCoords = wxGridNoCellCoords; | |
4947 | } | |
4948 | else | |
4949 | { | |
4950 | if ( m_currentCellCoords.GetCol() >= m_numCols ) | |
4951 | m_currentCellCoords.Set( 0, 0 ); | |
4952 | } | |
4953 | ||
4954 | if ( m_selection ) | |
4955 | m_selection->UpdateCols( pos, -((int)numCols) ); | |
4956 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); | |
4957 | if (attrProvider) | |
4958 | { | |
4959 | attrProvider->UpdateAttrCols( pos, -((int)numCols) ); | |
4960 | ||
4961 | // ifdef'd out following patch from Paul Gammans | |
4962 | #if 0 | |
4963 | // No need to touch row attributes, unless we | |
4964 | // removed _all_ columns, in this case, we remove | |
4965 | // all row attributes. | |
4966 | // I hate to do this here, but the | |
4967 | // needed data is not available inside UpdateAttrCols. | |
4968 | if ( !GetNumberCols() ) | |
4969 | attrProvider->UpdateAttrRows( 0, -GetNumberRows() ); | |
4970 | #endif | |
4971 | } | |
4972 | ||
4973 | if ( !GetBatchCount() ) | |
4974 | { | |
4975 | CalcDimensions(); | |
4976 | m_colLabelWin->Refresh(); | |
4977 | } | |
4978 | } | |
4979 | result = true; | |
4980 | break; | |
4981 | } | |
4982 | ||
4983 | if (result && !GetBatchCount() ) | |
4984 | m_gridWin->Refresh(); | |
4985 | ||
4986 | return result; | |
4987 | } | |
4988 | ||
4989 | wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) | |
4990 | { | |
4991 | wxRegionIterator iter( reg ); | |
4992 | wxRect r; | |
4993 | ||
4994 | wxArrayInt rowlabels; | |
4995 | ||
4996 | int top, bottom; | |
4997 | while ( iter ) | |
4998 | { | |
4999 | r = iter.GetRect(); | |
5000 | ||
5001 | // TODO: remove this when we can... | |
5002 | // There is a bug in wxMotif that gives garbage update | |
5003 | // rectangles if you jump-scroll a long way by clicking the | |
5004 | // scrollbar with middle button. This is a work-around | |
5005 | // | |
5006 | #if defined(__WXMOTIF__) | |
5007 | int cw, ch; | |
5008 | m_gridWin->GetClientSize( &cw, &ch ); | |
5009 | if ( r.GetTop() > ch ) | |
5010 | r.SetTop( 0 ); | |
5011 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
5012 | #endif | |
5013 | ||
5014 | // logical bounds of update region | |
5015 | // | |
5016 | int dummy; | |
5017 | CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top ); | |
5018 | CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom ); | |
5019 | ||
5020 | // find the row labels within these bounds | |
5021 | // | |
5022 | int row; | |
5023 | for ( row = internalYToRow(top); row < m_numRows; row++ ) | |
5024 | { | |
5025 | if ( GetRowBottom(row) < top ) | |
5026 | continue; | |
5027 | ||
5028 | if ( GetRowTop(row) > bottom ) | |
5029 | break; | |
5030 | ||
5031 | rowlabels.Add( row ); | |
5032 | } | |
5033 | ||
5034 | ++iter; | |
5035 | } | |
5036 | ||
5037 | return rowlabels; | |
5038 | } | |
5039 | ||
5040 | wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) | |
5041 | { | |
5042 | wxRegionIterator iter( reg ); | |
5043 | wxRect r; | |
5044 | ||
5045 | wxArrayInt colLabels; | |
5046 | ||
5047 | int left, right; | |
5048 | while ( iter ) | |
5049 | { | |
5050 | r = iter.GetRect(); | |
5051 | ||
5052 | // TODO: remove this when we can... | |
5053 | // There is a bug in wxMotif that gives garbage update | |
5054 | // rectangles if you jump-scroll a long way by clicking the | |
5055 | // scrollbar with middle button. This is a work-around | |
5056 | // | |
5057 | #if defined(__WXMOTIF__) | |
5058 | int cw, ch; | |
5059 | m_gridWin->GetClientSize( &cw, &ch ); | |
5060 | if ( r.GetLeft() > cw ) | |
5061 | r.SetLeft( 0 ); | |
5062 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
5063 | #endif | |
5064 | ||
5065 | // logical bounds of update region | |
5066 | // | |
5067 | int dummy; | |
5068 | CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy ); | |
5069 | CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy ); | |
5070 | ||
5071 | // find the cells within these bounds | |
5072 | // | |
5073 | int col; | |
5074 | int colPos; | |
5075 | for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ ) | |
5076 | { | |
5077 | col = GetColAt( colPos ); | |
5078 | ||
5079 | if ( GetColRight(col) < left ) | |
5080 | continue; | |
5081 | ||
5082 | if ( GetColLeft(col) > right ) | |
5083 | break; | |
5084 | ||
5085 | colLabels.Add( col ); | |
5086 | } | |
5087 | ||
5088 | ++iter; | |
5089 | } | |
5090 | ||
5091 | return colLabels; | |
5092 | } | |
5093 | ||
5094 | wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) | |
5095 | { | |
5096 | wxRegionIterator iter( reg ); | |
5097 | wxRect r; | |
5098 | ||
5099 | wxGridCellCoordsArray cellsExposed; | |
5100 | ||
5101 | int left, top, right, bottom; | |
5102 | while ( iter ) | |
5103 | { | |
5104 | r = iter.GetRect(); | |
5105 | ||
5106 | // TODO: remove this when we can... | |
5107 | // There is a bug in wxMotif that gives garbage update | |
5108 | // rectangles if you jump-scroll a long way by clicking the | |
5109 | // scrollbar with middle button. This is a work-around | |
5110 | // | |
5111 | #if defined(__WXMOTIF__) | |
5112 | int cw, ch; | |
5113 | m_gridWin->GetClientSize( &cw, &ch ); | |
5114 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
5115 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
5116 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
5117 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
5118 | #endif | |
5119 | ||
5120 | // logical bounds of update region | |
5121 | // | |
5122 | CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
5123 | CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
5124 | ||
5125 | // find the cells within these bounds | |
5126 | // | |
5127 | int row, col; | |
5128 | for ( row = internalYToRow(top); row < m_numRows; row++ ) | |
5129 | { | |
5130 | if ( GetRowBottom(row) <= top ) | |
5131 | continue; | |
5132 | ||
5133 | if ( GetRowTop(row) > bottom ) | |
5134 | break; | |
5135 | ||
5136 | int colPos; | |
5137 | for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ ) | |
5138 | { | |
5139 | col = GetColAt( colPos ); | |
5140 | ||
5141 | if ( GetColRight(col) <= left ) | |
5142 | continue; | |
5143 | ||
5144 | if ( GetColLeft(col) > right ) | |
5145 | break; | |
5146 | ||
5147 | cellsExposed.Add( wxGridCellCoords( row, col ) ); | |
5148 | } | |
5149 | } | |
5150 | ||
5151 | ++iter; | |
5152 | } | |
5153 | ||
5154 | return cellsExposed; | |
5155 | } | |
5156 | ||
5157 | ||
5158 | void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) | |
5159 | { | |
5160 | int x, y, row; | |
5161 | wxPoint pos( event.GetPosition() ); | |
5162 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
5163 | ||
5164 | if ( event.Dragging() ) | |
5165 | { | |
5166 | if (!m_isDragging) | |
5167 | { | |
5168 | m_isDragging = true; | |
5169 | m_rowLabelWin->CaptureMouse(); | |
5170 | } | |
5171 | ||
5172 | if ( event.LeftIsDown() ) | |
5173 | { | |
5174 | switch ( m_cursorMode ) | |
5175 | { | |
5176 | case WXGRID_CURSOR_RESIZE_ROW: | |
5177 | { | |
5178 | int cw, ch, left, dummy; | |
5179 | m_gridWin->GetClientSize( &cw, &ch ); | |
5180 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
5181 | ||
5182 | wxClientDC dc( m_gridWin ); | |
5183 | PrepareDC( dc ); | |
5184 | y = wxMax( y, | |
5185 | GetRowTop(m_dragRowOrCol) + | |
5186 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
5187 | dc.SetLogicalFunction(wxINVERT); | |
5188 | if ( m_dragLastPos >= 0 ) | |
5189 | { | |
5190 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
5191 | } | |
5192 | dc.DrawLine( left, y, left+cw, y ); | |
5193 | m_dragLastPos = y; | |
5194 | } | |
5195 | break; | |
5196 | ||
5197 | case WXGRID_CURSOR_SELECT_ROW: | |
5198 | { | |
5199 | if ( (row = YToRow( y )) >= 0 ) | |
5200 | { | |
5201 | if ( m_selection ) | |
5202 | { | |
5203 | m_selection->SelectRow( row, | |
5204 | event.ControlDown(), | |
5205 | event.ShiftDown(), | |
5206 | event.AltDown(), | |
5207 | event.MetaDown() ); | |
5208 | } | |
5209 | } | |
5210 | } | |
5211 | break; | |
5212 | ||
5213 | // default label to suppress warnings about "enumeration value | |
5214 | // 'xxx' not handled in switch | |
5215 | default: | |
5216 | break; | |
5217 | } | |
5218 | } | |
5219 | return; | |
5220 | } | |
5221 | ||
5222 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) | |
5223 | return; | |
5224 | ||
5225 | if (m_isDragging) | |
5226 | { | |
5227 | if (m_rowLabelWin->HasCapture()) | |
5228 | m_rowLabelWin->ReleaseMouse(); | |
5229 | m_isDragging = false; | |
5230 | } | |
5231 | ||
5232 | // ------------ Entering or leaving the window | |
5233 | // | |
5234 | if ( event.Entering() || event.Leaving() ) | |
5235 | { | |
5236 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); | |
5237 | } | |
5238 | ||
5239 | // ------------ Left button pressed | |
5240 | // | |
5241 | else if ( event.LeftDown() ) | |
5242 | { | |
5243 | // don't send a label click event for a hit on the | |
5244 | // edge of the row label - this is probably the user | |
5245 | // wanting to resize the row | |
5246 | // | |
5247 | if ( YToEdgeOfRow(y) < 0 ) | |
5248 | { | |
5249 | row = YToRow(y); | |
5250 | if ( row >= 0 && | |
5251 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) ) | |
5252 | { | |
5253 | if ( !event.ShiftDown() && !event.CmdDown() ) | |
5254 | ClearSelection(); | |
5255 | if ( m_selection ) | |
5256 | { | |
5257 | if ( event.ShiftDown() ) | |
5258 | { | |
5259 | m_selection->SelectBlock( m_currentCellCoords.GetRow(), | |
5260 | 0, | |
5261 | row, | |
5262 | GetNumberCols() - 1, | |
5263 | event.ControlDown(), | |
5264 | event.ShiftDown(), | |
5265 | event.AltDown(), | |
5266 | event.MetaDown() ); | |
5267 | } | |
5268 | else | |
5269 | { | |
5270 | m_selection->SelectRow( row, | |
5271 | event.ControlDown(), | |
5272 | event.ShiftDown(), | |
5273 | event.AltDown(), | |
5274 | event.MetaDown() ); | |
5275 | } | |
5276 | } | |
5277 | ||
5278 | ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin); | |
5279 | } | |
5280 | } | |
5281 | else | |
5282 | { | |
5283 | // starting to drag-resize a row | |
5284 | if ( CanDragRowSize() ) | |
5285 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin); | |
5286 | } | |
5287 | } | |
5288 | ||
5289 | // ------------ Left double click | |
5290 | // | |
5291 | else if (event.LeftDClick() ) | |
5292 | { | |
5293 | row = YToEdgeOfRow(y); | |
5294 | if ( row < 0 ) | |
5295 | { | |
5296 | row = YToRow(y); | |
5297 | if ( row >=0 && | |
5298 | !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) ) | |
5299 | { | |
5300 | // no default action at the moment | |
5301 | } | |
5302 | } | |
5303 | else | |
5304 | { | |
5305 | // adjust row height depending on label text | |
5306 | AutoSizeRowLabelSize( row ); | |
5307 | ||
5308 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
5309 | m_dragLastPos = -1; | |
5310 | } | |
5311 | } | |
5312 | ||
5313 | // ------------ Left button released | |
5314 | // | |
5315 | else if ( event.LeftUp() ) | |
5316 | { | |
5317 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
5318 | { | |
5319 | DoEndDragResizeRow(); | |
5320 | ||
5321 | // Note: we are ending the event *after* doing | |
5322 | // default processing in this case | |
5323 | // | |
5324 | SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); | |
5325 | } | |
5326 | ||
5327 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); | |
5328 | m_dragLastPos = -1; | |
5329 | } | |
5330 | ||
5331 | // ------------ Right button down | |
5332 | // | |
5333 | else if ( event.RightDown() ) | |
5334 | { | |
5335 | row = YToRow(y); | |
5336 | if ( row >=0 && | |
5337 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) | |
5338 | { | |
5339 | // no default action at the moment | |
5340 | } | |
5341 | } | |
5342 | ||
5343 | // ------------ Right double click | |
5344 | // | |
5345 | else if ( event.RightDClick() ) | |
5346 | { | |
5347 | row = YToRow(y); | |
5348 | if ( row >= 0 && | |
5349 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) | |
5350 | { | |
5351 | // no default action at the moment | |
5352 | } | |
5353 | } | |
5354 | ||
5355 | // ------------ No buttons down and mouse moving | |
5356 | // | |
5357 | else if ( event.Moving() ) | |
5358 | { | |
5359 | m_dragRowOrCol = YToEdgeOfRow( y ); | |
5360 | if ( m_dragRowOrCol >= 0 ) | |
5361 | { | |
5362 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
5363 | { | |
5364 | // don't capture the mouse yet | |
5365 | if ( CanDragRowSize() ) | |
5366 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false); | |
5367 | } | |
5368 | } | |
5369 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) | |
5370 | { | |
5371 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false); | |
5372 | } | |
5373 | } | |
5374 | } | |
5375 | ||
5376 | void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) | |
5377 | { | |
5378 | int x, y, col; | |
5379 | wxPoint pos( event.GetPosition() ); | |
5380 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
5381 | ||
5382 | if ( event.Dragging() ) | |
5383 | { | |
5384 | if (!m_isDragging) | |
5385 | { | |
5386 | m_isDragging = true; | |
5387 | m_colLabelWin->CaptureMouse(); | |
5388 | ||
5389 | if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL ) | |
5390 | m_dragRowOrCol = XToCol( x ); | |
5391 | } | |
5392 | ||
5393 | if ( event.LeftIsDown() ) | |
5394 | { | |
5395 | switch ( m_cursorMode ) | |
5396 | { | |
5397 | case WXGRID_CURSOR_RESIZE_COL: | |
5398 | { | |
5399 | int cw, ch, dummy, top; | |
5400 | m_gridWin->GetClientSize( &cw, &ch ); | |
5401 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
5402 | ||
5403 | wxClientDC dc( m_gridWin ); | |
5404 | PrepareDC( dc ); | |
5405 | ||
5406 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + | |
5407 | GetColMinimalWidth(m_dragRowOrCol)); | |
5408 | dc.SetLogicalFunction(wxINVERT); | |
5409 | if ( m_dragLastPos >= 0 ) | |
5410 | { | |
5411 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch ); | |
5412 | } | |
5413 | dc.DrawLine( x, top, x, top + ch ); | |
5414 | m_dragLastPos = x; | |
5415 | } | |
5416 | break; | |
5417 | ||
5418 | case WXGRID_CURSOR_SELECT_COL: | |
5419 | { | |
5420 | if ( (col = XToCol( x )) >= 0 ) | |
5421 | { | |
5422 | if ( m_selection ) | |
5423 | { | |
5424 | m_selection->SelectCol( col, | |
5425 | event.ControlDown(), | |
5426 | event.ShiftDown(), | |
5427 | event.AltDown(), | |
5428 | event.MetaDown() ); | |
5429 | } | |
5430 | } | |
5431 | } | |
5432 | break; | |
5433 | ||
5434 | case WXGRID_CURSOR_MOVE_COL: | |
5435 | { | |
5436 | if ( x < 0 ) | |
5437 | m_moveToCol = GetColAt( 0 ); | |
5438 | else | |
5439 | m_moveToCol = XToCol( x ); | |
5440 | ||
5441 | int markerX; | |
5442 | ||
5443 | if ( m_moveToCol < 0 ) | |
5444 | markerX = GetColRight( GetColAt( m_numCols - 1 ) ); | |
5445 | else | |
5446 | markerX = GetColLeft( m_moveToCol ); | |
5447 | ||
5448 | if ( markerX != m_dragLastPos ) | |
5449 | { | |
5450 | wxClientDC dc( m_colLabelWin ); | |
5451 | ||
5452 | int cw, ch; | |
5453 | m_colLabelWin->GetClientSize( &cw, &ch ); | |
5454 | ||
5455 | markerX++; | |
5456 | ||
5457 | //Clean up the last indicator | |
5458 | if ( m_dragLastPos >= 0 ) | |
5459 | { | |
5460 | wxPen pen( m_colLabelWin->GetBackgroundColour(), 2 ); | |
5461 | dc.SetPen(pen); | |
5462 | dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch ); | |
5463 | dc.SetPen(wxNullPen); | |
5464 | ||
5465 | if ( XToCol( m_dragLastPos ) != -1 ) | |
5466 | DrawColLabel( dc, XToCol( m_dragLastPos ) ); | |
5467 | } | |
5468 | ||
5469 | //Moving to the same place? Don't draw a marker | |
5470 | if ( (m_moveToCol == m_dragRowOrCol) | |
5471 | || (GetColPos( m_moveToCol ) == GetColPos( m_dragRowOrCol ) + 1) | |
5472 | || (m_moveToCol < 0 && m_dragRowOrCol == GetColAt( m_numCols - 1 ))) | |
5473 | { | |
5474 | m_dragLastPos = -1; | |
5475 | return; | |
5476 | } | |
5477 | ||
5478 | //Draw the marker | |
5479 | wxPen pen( *wxBLUE, 2 ); | |
5480 | dc.SetPen(pen); | |
5481 | ||
5482 | dc.DrawLine( markerX, 0, markerX, ch ); | |
5483 | ||
5484 | dc.SetPen(wxNullPen); | |
5485 | ||
5486 | m_dragLastPos = markerX - 1; | |
5487 | } | |
5488 | } | |
5489 | break; | |
5490 | ||
5491 | // default label to suppress warnings about "enumeration value | |
5492 | // 'xxx' not handled in switch | |
5493 | default: | |
5494 | break; | |
5495 | } | |
5496 | } | |
5497 | return; | |
5498 | } | |
5499 | ||
5500 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) | |
5501 | return; | |
5502 | ||
5503 | if (m_isDragging) | |
5504 | { | |
5505 | if (m_colLabelWin->HasCapture()) | |
5506 | m_colLabelWin->ReleaseMouse(); | |
5507 | m_isDragging = false; | |
5508 | } | |
5509 | ||
5510 | // ------------ Entering or leaving the window | |
5511 | // | |
5512 | if ( event.Entering() || event.Leaving() ) | |
5513 | { | |
5514 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
5515 | } | |
5516 | ||
5517 | // ------------ Left button pressed | |
5518 | // | |
5519 | else if ( event.LeftDown() ) | |
5520 | { | |
5521 | // don't send a label click event for a hit on the | |
5522 | // edge of the col label - this is probably the user | |
5523 | // wanting to resize the col | |
5524 | // | |
5525 | if ( XToEdgeOfCol(x) < 0 ) | |
5526 | { | |
5527 | col = XToCol(x); | |
5528 | if ( col >= 0 && | |
5529 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) ) | |
5530 | { | |
5531 | if ( m_canDragColMove ) | |
5532 | { | |
5533 | //Show button as pressed | |
5534 | wxClientDC dc( m_colLabelWin ); | |
5535 | int colLeft = GetColLeft( col ); | |
5536 | int colRight = GetColRight( col ) - 1; | |
5537 | dc.SetPen( wxPen( m_colLabelWin->GetBackgroundColour(), 1 ) ); | |
5538 | dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 ); | |
5539 | dc.DrawLine( colLeft, 1, colRight, 1 ); | |
5540 | ||
5541 | ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, m_colLabelWin); | |
5542 | } | |
5543 | else | |
5544 | { | |
5545 | if ( !event.ShiftDown() && !event.CmdDown() ) | |
5546 | ClearSelection(); | |
5547 | if ( m_selection ) | |
5548 | { | |
5549 | if ( event.ShiftDown() ) | |
5550 | { | |
5551 | m_selection->SelectBlock( 0, | |
5552 | m_currentCellCoords.GetCol(), | |
5553 | GetNumberRows() - 1, col, | |
5554 | event.ControlDown(), | |
5555 | event.ShiftDown(), | |
5556 | event.AltDown(), | |
5557 | event.MetaDown() ); | |
5558 | } | |
5559 | else | |
5560 | { | |
5561 | m_selection->SelectCol( col, | |
5562 | event.ControlDown(), | |
5563 | event.ShiftDown(), | |
5564 | event.AltDown(), | |
5565 | event.MetaDown() ); | |
5566 | } | |
5567 | } | |
5568 | ||
5569 | ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin); | |
5570 | } | |
5571 | } | |
5572 | } | |
5573 | else | |
5574 | { | |
5575 | // starting to drag-resize a col | |
5576 | // | |
5577 | if ( CanDragColSize() ) | |
5578 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin); | |
5579 | } | |
5580 | } | |
5581 | ||
5582 | // ------------ Left double click | |
5583 | // | |
5584 | if ( event.LeftDClick() ) | |
5585 | { | |
5586 | col = XToEdgeOfCol(x); | |
5587 | if ( col < 0 ) | |
5588 | { | |
5589 | col = XToCol(x); | |
5590 | if ( col >= 0 && | |
5591 | ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) ) | |
5592 | { | |
5593 | // no default action at the moment | |
5594 | } | |
5595 | } | |
5596 | else | |
5597 | { | |
5598 | // adjust column width depending on label text | |
5599 | AutoSizeColLabelSize( col ); | |
5600 | ||
5601 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
5602 | m_dragLastPos = -1; | |
5603 | } | |
5604 | } | |
5605 | ||
5606 | // ------------ Left button released | |
5607 | // | |
5608 | else if ( event.LeftUp() ) | |
5609 | { | |
5610 | switch ( m_cursorMode ) | |
5611 | { | |
5612 | case WXGRID_CURSOR_RESIZE_COL: | |
5613 | DoEndDragResizeCol(); | |
5614 | ||
5615 | // Note: we are ending the event *after* doing | |
5616 | // default processing in this case | |
5617 | // | |
5618 | SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); | |
5619 | break; | |
5620 | ||
5621 | case WXGRID_CURSOR_MOVE_COL: | |
5622 | DoEndDragMoveCol(); | |
5623 | ||
5624 | SendEvent( wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol, event ); | |
5625 | break; | |
5626 | ||
5627 | case WXGRID_CURSOR_SELECT_COL: | |
5628 | case WXGRID_CURSOR_SELECT_CELL: | |
5629 | case WXGRID_CURSOR_RESIZE_ROW: | |
5630 | case WXGRID_CURSOR_SELECT_ROW: | |
5631 | // nothing to do (?) | |
5632 | break; | |
5633 | } | |
5634 | ||
5635 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
5636 | m_dragLastPos = -1; | |
5637 | } | |
5638 | ||
5639 | // ------------ Right button down | |
5640 | // | |
5641 | else if ( event.RightDown() ) | |
5642 | { | |
5643 | col = XToCol(x); | |
5644 | if ( col >= 0 && | |
5645 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) | |
5646 | { | |
5647 | // no default action at the moment | |
5648 | } | |
5649 | } | |
5650 | ||
5651 | // ------------ Right double click | |
5652 | // | |
5653 | else if ( event.RightDClick() ) | |
5654 | { | |
5655 | col = XToCol(x); | |
5656 | if ( col >= 0 && | |
5657 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) | |
5658 | { | |
5659 | // no default action at the moment | |
5660 | } | |
5661 | } | |
5662 | ||
5663 | // ------------ No buttons down and mouse moving | |
5664 | // | |
5665 | else if ( event.Moving() ) | |
5666 | { | |
5667 | m_dragRowOrCol = XToEdgeOfCol( x ); | |
5668 | if ( m_dragRowOrCol >= 0 ) | |
5669 | { | |
5670 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
5671 | { | |
5672 | // don't capture the cursor yet | |
5673 | if ( CanDragColSize() ) | |
5674 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false); | |
5675 | } | |
5676 | } | |
5677 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) | |
5678 | { | |
5679 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false); | |
5680 | } | |
5681 | } | |
5682 | } | |
5683 | ||
5684 | void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event ) | |
5685 | { | |
5686 | if ( event.LeftDown() ) | |
5687 | { | |
5688 | // indicate corner label by having both row and | |
5689 | // col args == -1 | |
5690 | // | |
5691 | if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) ) | |
5692 | { | |
5693 | SelectAll(); | |
5694 | } | |
5695 | } | |
5696 | else if ( event.LeftDClick() ) | |
5697 | { | |
5698 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event ); | |
5699 | } | |
5700 | else if ( event.RightDown() ) | |
5701 | { | |
5702 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) ) | |
5703 | { | |
5704 | // no default action at the moment | |
5705 | } | |
5706 | } | |
5707 | else if ( event.RightDClick() ) | |
5708 | { | |
5709 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) ) | |
5710 | { | |
5711 | // no default action at the moment | |
5712 | } | |
5713 | } | |
5714 | } | |
5715 | ||
5716 | void wxGrid::ChangeCursorMode(CursorMode mode, | |
5717 | wxWindow *win, | |
5718 | bool captureMouse) | |
5719 | { | |
5720 | #ifdef __WXDEBUG__ | |
5721 | static const wxChar *cursorModes[] = | |
5722 | { | |
5723 | _T("SELECT_CELL"), | |
5724 | _T("RESIZE_ROW"), | |
5725 | _T("RESIZE_COL"), | |
5726 | _T("SELECT_ROW"), | |
5727 | _T("SELECT_COL"), | |
5728 | _T("MOVE_COL"), | |
5729 | }; | |
5730 | ||
5731 | wxLogTrace(_T("grid"), | |
5732 | _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"), | |
5733 | win == m_colLabelWin ? _T("colLabelWin") | |
5734 | : win ? _T("rowLabelWin") | |
5735 | : _T("gridWin"), | |
5736 | cursorModes[m_cursorMode], cursorModes[mode]); | |
5737 | #endif | |
5738 | ||
5739 | if ( mode == m_cursorMode && | |
5740 | win == m_winCapture && | |
5741 | captureMouse == (m_winCapture != NULL)) | |
5742 | return; | |
5743 | ||
5744 | if ( !win ) | |
5745 | { | |
5746 | // by default use the grid itself | |
5747 | win = m_gridWin; | |
5748 | } | |
5749 | ||
5750 | if ( m_winCapture ) | |
5751 | { | |
5752 | if (m_winCapture->HasCapture()) | |
5753 | m_winCapture->ReleaseMouse(); | |
5754 | m_winCapture = (wxWindow *)NULL; | |
5755 | } | |
5756 | ||
5757 | m_cursorMode = mode; | |
5758 | ||
5759 | switch ( m_cursorMode ) | |
5760 | { | |
5761 | case WXGRID_CURSOR_RESIZE_ROW: | |
5762 | win->SetCursor( m_rowResizeCursor ); | |
5763 | break; | |
5764 | ||
5765 | case WXGRID_CURSOR_RESIZE_COL: | |
5766 | win->SetCursor( m_colResizeCursor ); | |
5767 | break; | |
5768 | ||
5769 | case WXGRID_CURSOR_MOVE_COL: | |
5770 | win->SetCursor( wxCursor(wxCURSOR_HAND) ); | |
5771 | break; | |
5772 | ||
5773 | default: | |
5774 | win->SetCursor( *wxSTANDARD_CURSOR ); | |
5775 | break; | |
5776 | } | |
5777 | ||
5778 | // we need to capture mouse when resizing | |
5779 | bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW || | |
5780 | m_cursorMode == WXGRID_CURSOR_RESIZE_COL; | |
5781 | ||
5782 | if ( captureMouse && resize ) | |
5783 | { | |
5784 | win->CaptureMouse(); | |
5785 | m_winCapture = win; | |
5786 | } | |
5787 | } | |
5788 | ||
5789 | void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) | |
5790 | { | |
5791 | int x, y; | |
5792 | wxPoint pos( event.GetPosition() ); | |
5793 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
5794 | ||
5795 | wxGridCellCoords coords; | |
5796 | XYToCell( x, y, coords ); | |
5797 | ||
5798 | int cell_rows, cell_cols; | |
5799 | bool isFirstDrag = !m_isDragging; | |
5800 | GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols ); | |
5801 | if ((cell_rows < 0) || (cell_cols < 0)) | |
5802 | { | |
5803 | coords.SetRow(coords.GetRow() + cell_rows); | |
5804 | coords.SetCol(coords.GetCol() + cell_cols); | |
5805 | } | |
5806 | ||
5807 | if ( event.Dragging() ) | |
5808 | { | |
5809 | //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol()); | |
5810 | ||
5811 | // Don't start doing anything until the mouse has been dragged at | |
5812 | // least 3 pixels in any direction... | |
5813 | if (! m_isDragging) | |
5814 | { | |
5815 | if (m_startDragPos == wxDefaultPosition) | |
5816 | { | |
5817 | m_startDragPos = pos; | |
5818 | return; | |
5819 | } | |
5820 | if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4) | |
5821 | return; | |
5822 | } | |
5823 | ||
5824 | m_isDragging = true; | |
5825 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
5826 | { | |
5827 | // Hide the edit control, so it | |
5828 | // won't interfere with drag-shrinking. | |
5829 | if ( IsCellEditControlShown() ) | |
5830 | { | |
5831 | HideCellEditControl(); | |
5832 | SaveEditControlValue(); | |
5833 | } | |
5834 | ||
5835 | // Have we captured the mouse yet? | |
5836 | if (! m_winCapture) | |
5837 | { | |
5838 | m_winCapture = m_gridWin; | |
5839 | m_winCapture->CaptureMouse(); | |
5840 | } | |
5841 | ||
5842 | if ( coords != wxGridNoCellCoords ) | |
5843 | { | |
5844 | if ( event.CmdDown() ) | |
5845 | { | |
5846 | if ( m_selectingKeyboard == wxGridNoCellCoords) | |
5847 | m_selectingKeyboard = coords; | |
5848 | HighlightBlock( m_selectingKeyboard, coords ); | |
5849 | } | |
5850 | else if ( CanDragCell() ) | |
5851 | { | |
5852 | if ( isFirstDrag ) | |
5853 | { | |
5854 | if ( m_selectingKeyboard == wxGridNoCellCoords) | |
5855 | m_selectingKeyboard = coords; | |
5856 | ||
5857 | SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG, | |
5858 | coords.GetRow(), | |
5859 | coords.GetCol(), | |
5860 | event ); | |
5861 | } | |
5862 | } | |
5863 | else | |
5864 | { | |
5865 | if ( !IsSelection() ) | |
5866 | { | |
5867 | HighlightBlock( coords, coords ); | |
5868 | } | |
5869 | else | |
5870 | { | |
5871 | HighlightBlock( m_currentCellCoords, coords ); | |
5872 | } | |
5873 | } | |
5874 | ||
5875 | if (! IsVisible(coords)) | |
5876 | { | |
5877 | MakeCellVisible(coords); | |
5878 | // TODO: need to introduce a delay or something here. The | |
5879 | // scrolling is way to fast, at least on MSW - also on GTK. | |
5880 | } | |
5881 | } | |
5882 | } | |
5883 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
5884 | { | |
5885 | int cw, ch, left, dummy; | |
5886 | m_gridWin->GetClientSize( &cw, &ch ); | |
5887 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
5888 | ||
5889 | wxClientDC dc( m_gridWin ); | |
5890 | PrepareDC( dc ); | |
5891 | y = wxMax( y, GetRowTop(m_dragRowOrCol) + | |
5892 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
5893 | dc.SetLogicalFunction(wxINVERT); | |
5894 | if ( m_dragLastPos >= 0 ) | |
5895 | { | |
5896 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
5897 | } | |
5898 | dc.DrawLine( left, y, left+cw, y ); | |
5899 | m_dragLastPos = y; | |
5900 | } | |
5901 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
5902 | { | |
5903 | int cw, ch, dummy, top; | |
5904 | m_gridWin->GetClientSize( &cw, &ch ); | |
5905 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
5906 | ||
5907 | wxClientDC dc( m_gridWin ); | |
5908 | PrepareDC( dc ); | |
5909 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + | |
5910 | GetColMinimalWidth(m_dragRowOrCol) ); | |
5911 | dc.SetLogicalFunction(wxINVERT); | |
5912 | if ( m_dragLastPos >= 0 ) | |
5913 | { | |
5914 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch ); | |
5915 | } | |
5916 | dc.DrawLine( x, top, x, top + ch ); | |
5917 | m_dragLastPos = x; | |
5918 | } | |
5919 | ||
5920 | return; | |
5921 | } | |
5922 | ||
5923 | m_isDragging = false; | |
5924 | m_startDragPos = wxDefaultPosition; | |
5925 | ||
5926 | // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL | |
5927 | // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under | |
5928 | // wxGTK | |
5929 | #if 0 | |
5930 | if ( event.Entering() || event.Leaving() ) | |
5931 | { | |
5932 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5933 | m_gridWin->SetCursor( *wxSTANDARD_CURSOR ); | |
5934 | } | |
5935 | else | |
5936 | #endif // 0 | |
5937 | ||
5938 | // ------------ Left button pressed | |
5939 | // | |
5940 | if ( event.LeftDown() && coords != wxGridNoCellCoords ) | |
5941 | { | |
5942 | if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK, | |
5943 | coords.GetRow(), | |
5944 | coords.GetCol(), | |
5945 | event ) ) | |
5946 | { | |
5947 | if ( !event.CmdDown() ) | |
5948 | ClearSelection(); | |
5949 | if ( event.ShiftDown() ) | |
5950 | { | |
5951 | if ( m_selection ) | |
5952 | { | |
5953 | m_selection->SelectBlock( m_currentCellCoords.GetRow(), | |
5954 | m_currentCellCoords.GetCol(), | |
5955 | coords.GetRow(), | |
5956 | coords.GetCol(), | |
5957 | event.ControlDown(), | |
5958 | event.ShiftDown(), | |
5959 | event.AltDown(), | |
5960 | event.MetaDown() ); | |
5961 | } | |
5962 | } | |
5963 | else if ( XToEdgeOfCol(x) < 0 && | |
5964 | YToEdgeOfRow(y) < 0 ) | |
5965 | { | |
5966 | DisableCellEditControl(); | |
5967 | MakeCellVisible( coords ); | |
5968 | ||
5969 | if ( event.CmdDown() ) | |
5970 | { | |
5971 | if ( m_selection ) | |
5972 | { | |
5973 | m_selection->ToggleCellSelection( coords.GetRow(), | |
5974 | coords.GetCol(), | |
5975 | event.ControlDown(), | |
5976 | event.ShiftDown(), | |
5977 | event.AltDown(), | |
5978 | event.MetaDown() ); | |
5979 | } | |
5980 | m_selectingTopLeft = wxGridNoCellCoords; | |
5981 | m_selectingBottomRight = wxGridNoCellCoords; | |
5982 | m_selectingKeyboard = coords; | |
5983 | } | |
5984 | else | |
5985 | { | |
5986 | m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords; | |
5987 | SetCurrentCell( coords ); | |
5988 | if ( m_selection ) | |
5989 | { | |
5990 | if ( m_selection->GetSelectionMode() != | |
5991 | wxGrid::wxGridSelectCells ) | |
5992 | { | |
5993 | HighlightBlock( coords, coords ); | |
5994 | } | |
5995 | } | |
5996 | } | |
5997 | } | |
5998 | } | |
5999 | } | |
6000 | ||
6001 | // ------------ Left double click | |
6002 | // | |
6003 | else if ( event.LeftDClick() && coords != wxGridNoCellCoords ) | |
6004 | { | |
6005 | DisableCellEditControl(); | |
6006 | ||
6007 | if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 ) | |
6008 | { | |
6009 | if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK, | |
6010 | coords.GetRow(), | |
6011 | coords.GetCol(), | |
6012 | event ) ) | |
6013 | { | |
6014 | // we want double click to select a cell and start editing | |
6015 | // (i.e. to behave in same way as sequence of two slow clicks): | |
6016 | m_waitForSlowClick = true; | |
6017 | } | |
6018 | } | |
6019 | } | |
6020 | ||
6021 | // ------------ Left button released | |
6022 | // | |
6023 | else if ( event.LeftUp() ) | |
6024 | { | |
6025 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
6026 | { | |
6027 | if (m_winCapture) | |
6028 | { | |
6029 | if (m_winCapture->HasCapture()) | |
6030 | m_winCapture->ReleaseMouse(); | |
6031 | m_winCapture = NULL; | |
6032 | } | |
6033 | ||
6034 | if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() ) | |
6035 | { | |
6036 | ClearSelection(); | |
6037 | EnableCellEditControl(); | |
6038 | ||
6039 | wxGridCellAttr *attr = GetCellAttr(coords); | |
6040 | wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol()); | |
6041 | editor->StartingClick(); | |
6042 | editor->DecRef(); | |
6043 | attr->DecRef(); | |
6044 | ||
6045 | m_waitForSlowClick = false; | |
6046 | } | |
6047 | else if ( m_selectingTopLeft != wxGridNoCellCoords && | |
6048 | m_selectingBottomRight != wxGridNoCellCoords ) | |
6049 | { | |
6050 | if ( m_selection ) | |
6051 | { | |
6052 | m_selection->SelectBlock( m_selectingTopLeft.GetRow(), | |
6053 | m_selectingTopLeft.GetCol(), | |
6054 | m_selectingBottomRight.GetRow(), | |
6055 | m_selectingBottomRight.GetCol(), | |
6056 | event.ControlDown(), | |
6057 | event.ShiftDown(), | |
6058 | event.AltDown(), | |
6059 | event.MetaDown() ); | |
6060 | } | |
6061 | ||
6062 | m_selectingTopLeft = wxGridNoCellCoords; | |
6063 | m_selectingBottomRight = wxGridNoCellCoords; | |
6064 | ||
6065 | // Show the edit control, if it has been hidden for | |
6066 | // drag-shrinking. | |
6067 | ShowCellEditControl(); | |
6068 | } | |
6069 | } | |
6070 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
6071 | { | |
6072 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
6073 | DoEndDragResizeRow(); | |
6074 | ||
6075 | // Note: we are ending the event *after* doing | |
6076 | // default processing in this case | |
6077 | // | |
6078 | SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); | |
6079 | } | |
6080 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
6081 | { | |
6082 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
6083 | DoEndDragResizeCol(); | |
6084 | ||
6085 | // Note: we are ending the event *after* doing | |
6086 | // default processing in this case | |
6087 | // | |
6088 | SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); | |
6089 | } | |
6090 | ||
6091 | m_dragLastPos = -1; | |
6092 | } | |
6093 | ||
6094 | // ------------ Right button down | |
6095 | // | |
6096 | else if ( event.RightDown() && coords != wxGridNoCellCoords ) | |
6097 | { | |
6098 | DisableCellEditControl(); | |
6099 | if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK, | |
6100 | coords.GetRow(), | |
6101 | coords.GetCol(), | |
6102 | event ) ) | |
6103 | { | |
6104 | // no default action at the moment | |
6105 | } | |
6106 | } | |
6107 | ||
6108 | // ------------ Right double click | |
6109 | // | |
6110 | else if ( event.RightDClick() && coords != wxGridNoCellCoords ) | |
6111 | { | |
6112 | DisableCellEditControl(); | |
6113 | if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK, | |
6114 | coords.GetRow(), | |
6115 | coords.GetCol(), | |
6116 | event ) ) | |
6117 | { | |
6118 | // no default action at the moment | |
6119 | } | |
6120 | } | |
6121 | ||
6122 | // ------------ Moving and no button action | |
6123 | // | |
6124 | else if ( event.Moving() && !event.IsButton() ) | |
6125 | { | |
6126 | if ( coords.GetRow() < 0 || coords.GetCol() < 0 ) | |
6127 | { | |
6128 | // out of grid cell area | |
6129 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
6130 | return; | |
6131 | } | |
6132 | ||
6133 | int dragRow = YToEdgeOfRow( y ); | |
6134 | int dragCol = XToEdgeOfCol( x ); | |
6135 | ||
6136 | // Dragging on the corner of a cell to resize in both | |
6137 | // directions is not implemented yet... | |
6138 | // | |
6139 | if ( dragRow >= 0 && dragCol >= 0 ) | |
6140 | { | |
6141 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
6142 | return; | |
6143 | } | |
6144 | ||
6145 | if ( dragRow >= 0 ) | |
6146 | { | |
6147 | m_dragRowOrCol = dragRow; | |
6148 | ||
6149 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
6150 | { | |
6151 | if ( CanDragRowSize() && CanDragGridSize() ) | |
6152 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW); | |
6153 | } | |
6154 | } | |
6155 | else if ( dragCol >= 0 ) | |
6156 | { | |
6157 | m_dragRowOrCol = dragCol; | |
6158 | ||
6159 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
6160 | { | |
6161 | if ( CanDragColSize() && CanDragGridSize() ) | |
6162 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL); | |
6163 | } | |
6164 | } | |
6165 | else // Neither on a row or col edge | |
6166 | { | |
6167 | if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) | |
6168 | { | |
6169 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
6170 | } | |
6171 | } | |
6172 | } | |
6173 | } | |
6174 | ||
6175 | void wxGrid::DoEndDragResizeRow() | |
6176 | { | |
6177 | if ( m_dragLastPos >= 0 ) | |
6178 | { | |
6179 | // erase the last line and resize the row | |
6180 | // | |
6181 | int cw, ch, left, dummy; | |
6182 | m_gridWin->GetClientSize( &cw, &ch ); | |
6183 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
6184 | ||
6185 | wxClientDC dc( m_gridWin ); | |
6186 | PrepareDC( dc ); | |
6187 | dc.SetLogicalFunction( wxINVERT ); | |
6188 | dc.DrawLine( left, m_dragLastPos, left + cw, m_dragLastPos ); | |
6189 | HideCellEditControl(); | |
6190 | SaveEditControlValue(); | |
6191 | ||
6192 | int rowTop = GetRowTop(m_dragRowOrCol); | |
6193 | SetRowSize( m_dragRowOrCol, | |
6194 | wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) ); | |
6195 | ||
6196 | if ( !GetBatchCount() ) | |
6197 | { | |
6198 | // Only needed to get the correct rect.y: | |
6199 | wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) ); | |
6200 | rect.x = 0; | |
6201 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
6202 | rect.width = m_rowLabelWidth; | |
6203 | rect.height = ch - rect.y; | |
6204 | m_rowLabelWin->Refresh( true, &rect ); | |
6205 | rect.width = cw; | |
6206 | ||
6207 | // if there is a multicell block, paint all of it | |
6208 | if (m_table) | |
6209 | { | |
6210 | int i, cell_rows, cell_cols, subtract_rows = 0; | |
6211 | int leftCol = XToCol(left); | |
6212 | int rightCol = internalXToCol(left + cw); | |
6213 | if (leftCol >= 0) | |
6214 | { | |
6215 | for (i=leftCol; i<rightCol; i++) | |
6216 | { | |
6217 | GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols); | |
6218 | if (cell_rows < subtract_rows) | |
6219 | subtract_rows = cell_rows; | |
6220 | } | |
6221 | rect.y = GetRowTop(m_dragRowOrCol + subtract_rows); | |
6222 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
6223 | rect.height = ch - rect.y; | |
6224 | } | |
6225 | } | |
6226 | m_gridWin->Refresh( false, &rect ); | |
6227 | } | |
6228 | ||
6229 | ShowCellEditControl(); | |
6230 | } | |
6231 | } | |
6232 | ||
6233 | ||
6234 | void wxGrid::DoEndDragResizeCol() | |
6235 | { | |
6236 | if ( m_dragLastPos >= 0 ) | |
6237 | { | |
6238 | // erase the last line and resize the col | |
6239 | // | |
6240 | int cw, ch, dummy, top; | |
6241 | m_gridWin->GetClientSize( &cw, &ch ); | |
6242 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
6243 | ||
6244 | wxClientDC dc( m_gridWin ); | |
6245 | PrepareDC( dc ); | |
6246 | dc.SetLogicalFunction( wxINVERT ); | |
6247 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch ); | |
6248 | HideCellEditControl(); | |
6249 | SaveEditControlValue(); | |
6250 | ||
6251 | int colLeft = GetColLeft(m_dragRowOrCol); | |
6252 | SetColSize( m_dragRowOrCol, | |
6253 | wxMax( m_dragLastPos - colLeft, | |
6254 | GetColMinimalWidth(m_dragRowOrCol) ) ); | |
6255 | ||
6256 | if ( !GetBatchCount() ) | |
6257 | { | |
6258 | // Only needed to get the correct rect.x: | |
6259 | wxRect rect ( CellToRect( 0, m_dragRowOrCol ) ); | |
6260 | rect.y = 0; | |
6261 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
6262 | rect.width = cw - rect.x; | |
6263 | rect.height = m_colLabelHeight; | |
6264 | m_colLabelWin->Refresh( true, &rect ); | |
6265 | rect.height = ch; | |
6266 | ||
6267 | // if there is a multicell block, paint all of it | |
6268 | if (m_table) | |
6269 | { | |
6270 | int i, cell_rows, cell_cols, subtract_cols = 0; | |
6271 | int topRow = YToRow(top); | |
6272 | int bottomRow = internalYToRow(top + cw); | |
6273 | if (topRow >= 0) | |
6274 | { | |
6275 | for (i=topRow; i<bottomRow; i++) | |
6276 | { | |
6277 | GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols); | |
6278 | if (cell_cols < subtract_cols) | |
6279 | subtract_cols = cell_cols; | |
6280 | } | |
6281 | ||
6282 | rect.x = GetColLeft(m_dragRowOrCol + subtract_cols); | |
6283 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
6284 | rect.width = cw - rect.x; | |
6285 | } | |
6286 | } | |
6287 | ||
6288 | m_gridWin->Refresh( false, &rect ); | |
6289 | } | |
6290 | ||
6291 | ShowCellEditControl(); | |
6292 | } | |
6293 | } | |
6294 | ||
6295 | void wxGrid::DoEndDragMoveCol() | |
6296 | { | |
6297 | //The user clicked on the column but didn't actually drag | |
6298 | if ( m_dragLastPos < 0 ) | |
6299 | { | |
6300 | m_colLabelWin->Refresh(); //Do this to "unpress" the column | |
6301 | return; | |
6302 | } | |
6303 | ||
6304 | int newPos; | |
6305 | if ( m_moveToCol == -1 ) | |
6306 | newPos = m_numCols - 1; | |
6307 | else | |
6308 | { | |
6309 | newPos = GetColPos( m_moveToCol ); | |
6310 | if ( newPos > GetColPos( m_dragRowOrCol ) ) | |
6311 | newPos--; | |
6312 | } | |
6313 | ||
6314 | SetColPos( m_dragRowOrCol, newPos ); | |
6315 | } | |
6316 | ||
6317 | void wxGrid::SetColPos( int colID, int newPos ) | |
6318 | { | |
6319 | if ( m_colAt.IsEmpty() ) | |
6320 | { | |
6321 | m_colAt.Alloc( m_numCols ); | |
6322 | ||
6323 | int i; | |
6324 | for ( i = 0; i < m_numCols; i++ ) | |
6325 | { | |
6326 | m_colAt.Add( i ); | |
6327 | } | |
6328 | } | |
6329 | ||
6330 | int oldPos = GetColPos( colID ); | |
6331 | ||
6332 | //Reshuffle the m_colAt array | |
6333 | if ( newPos > oldPos ) | |
6334 | { | |
6335 | int i; | |
6336 | for ( i = oldPos; i < newPos; i++ ) | |
6337 | { | |
6338 | m_colAt[i] = m_colAt[i+1]; | |
6339 | } | |
6340 | } | |
6341 | else | |
6342 | { | |
6343 | int i; | |
6344 | for ( i = oldPos; i > newPos; i-- ) | |
6345 | { | |
6346 | m_colAt[i] = m_colAt[i-1]; | |
6347 | } | |
6348 | } | |
6349 | ||
6350 | m_colAt[newPos] = colID; | |
6351 | ||
6352 | //Recalculate the column rights | |
6353 | if ( !m_colWidths.IsEmpty() ) | |
6354 | { | |
6355 | int colRight = 0; | |
6356 | int colPos; | |
6357 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
6358 | { | |
6359 | int colID = GetColAt( colPos ); | |
6360 | ||
6361 | colRight += m_colWidths[colID]; | |
6362 | m_colRights[colID] = colRight; | |
6363 | } | |
6364 | } | |
6365 | ||
6366 | m_colLabelWin->Refresh(); | |
6367 | m_gridWin->Refresh(); | |
6368 | } | |
6369 | ||
6370 | ||
6371 | ||
6372 | void wxGrid::EnableDragColMove( bool enable ) | |
6373 | { | |
6374 | if ( m_canDragColMove == enable ) | |
6375 | return; | |
6376 | ||
6377 | m_canDragColMove = enable; | |
6378 | ||
6379 | if ( !m_canDragColMove ) | |
6380 | { | |
6381 | m_colAt.Clear(); | |
6382 | ||
6383 | //Recalculate the column rights | |
6384 | if ( !m_colWidths.IsEmpty() ) | |
6385 | { | |
6386 | int colRight = 0; | |
6387 | int colPos; | |
6388 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
6389 | { | |
6390 | colRight += m_colWidths[colPos]; | |
6391 | m_colRights[colPos] = colRight; | |
6392 | } | |
6393 | } | |
6394 | ||
6395 | m_colLabelWin->Refresh(); | |
6396 | m_gridWin->Refresh(); | |
6397 | } | |
6398 | } | |
6399 | ||
6400 | ||
6401 | // | |
6402 | // ------ interaction with data model | |
6403 | // | |
6404 | bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg ) | |
6405 | { | |
6406 | switch ( msg.GetId() ) | |
6407 | { | |
6408 | case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES: | |
6409 | return GetModelValues(); | |
6410 | ||
6411 | case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES: | |
6412 | return SetModelValues(); | |
6413 | ||
6414 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
6415 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
6416 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
6417 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
6418 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
6419 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
6420 | return Redimension( msg ); | |
6421 | ||
6422 | default: | |
6423 | return false; | |
6424 | } | |
6425 | } | |
6426 | ||
6427 | // The behaviour of this function depends on the grid table class | |
6428 | // Clear() function. For the default wxGridStringTable class the | |
6429 | // behavious is to replace all cell contents with wxEmptyString but | |
6430 | // not to change the number of rows or cols. | |
6431 | // | |
6432 | void wxGrid::ClearGrid() | |
6433 | { | |
6434 | if ( m_table ) | |
6435 | { | |
6436 | if (IsCellEditControlEnabled()) | |
6437 | DisableCellEditControl(); | |
6438 | ||
6439 | m_table->Clear(); | |
6440 | if (!GetBatchCount()) | |
6441 | m_gridWin->Refresh(); | |
6442 | } | |
6443 | } | |
6444 | ||
6445 | bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) | |
6446 | { | |
6447 | // TODO: something with updateLabels flag | |
6448 | ||
6449 | if ( !m_created ) | |
6450 | { | |
6451 | wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") ); | |
6452 | return false; | |
6453 | } | |
6454 | ||
6455 | if ( m_table ) | |
6456 | { | |
6457 | if (IsCellEditControlEnabled()) | |
6458 | DisableCellEditControl(); | |
6459 | ||
6460 | bool done = m_table->InsertRows( pos, numRows ); | |
6461 | return done; | |
6462 | ||
6463 | // the table will have sent the results of the insert row | |
6464 | // operation to this view object as a grid table message | |
6465 | } | |
6466 | ||
6467 | return false; | |
6468 | } | |
6469 | ||
6470 | bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) ) | |
6471 | { | |
6472 | // TODO: something with updateLabels flag | |
6473 | ||
6474 | if ( !m_created ) | |
6475 | { | |
6476 | wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") ); | |
6477 | return false; | |
6478 | } | |
6479 | ||
6480 | if ( m_table ) | |
6481 | { | |
6482 | bool done = m_table && m_table->AppendRows( numRows ); | |
6483 | return done; | |
6484 | ||
6485 | // the table will have sent the results of the append row | |
6486 | // operation to this view object as a grid table message | |
6487 | } | |
6488 | ||
6489 | return false; | |
6490 | } | |
6491 | ||
6492 | bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) | |
6493 | { | |
6494 | // TODO: something with updateLabels flag | |
6495 | ||
6496 | if ( !m_created ) | |
6497 | { | |
6498 | wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") ); | |
6499 | return false; | |
6500 | } | |
6501 | ||
6502 | if ( m_table ) | |
6503 | { | |
6504 | if (IsCellEditControlEnabled()) | |
6505 | DisableCellEditControl(); | |
6506 | ||
6507 | bool done = m_table->DeleteRows( pos, numRows ); | |
6508 | return done; | |
6509 | // the table will have sent the results of the delete row | |
6510 | // operation to this view object as a grid table message | |
6511 | } | |
6512 | ||
6513 | return false; | |
6514 | } | |
6515 | ||
6516 | bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) | |
6517 | { | |
6518 | // TODO: something with updateLabels flag | |
6519 | ||
6520 | if ( !m_created ) | |
6521 | { | |
6522 | wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") ); | |
6523 | return false; | |
6524 | } | |
6525 | ||
6526 | if ( m_table ) | |
6527 | { | |
6528 | if (IsCellEditControlEnabled()) | |
6529 | DisableCellEditControl(); | |
6530 | ||
6531 | bool done = m_table->InsertCols( pos, numCols ); | |
6532 | return done; | |
6533 | // the table will have sent the results of the insert col | |
6534 | // operation to this view object as a grid table message | |
6535 | } | |
6536 | ||
6537 | return false; | |
6538 | } | |
6539 | ||
6540 | bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) ) | |
6541 | { | |
6542 | // TODO: something with updateLabels flag | |
6543 | ||
6544 | if ( !m_created ) | |
6545 | { | |
6546 | wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") ); | |
6547 | return false; | |
6548 | } | |
6549 | ||
6550 | if ( m_table ) | |
6551 | { | |
6552 | bool done = m_table->AppendCols( numCols ); | |
6553 | return done; | |
6554 | // the table will have sent the results of the append col | |
6555 | // operation to this view object as a grid table message | |
6556 | } | |
6557 | ||
6558 | return false; | |
6559 | } | |
6560 | ||
6561 | bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) | |
6562 | { | |
6563 | // TODO: something with updateLabels flag | |
6564 | ||
6565 | if ( !m_created ) | |
6566 | { | |
6567 | wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") ); | |
6568 | return false; | |
6569 | } | |
6570 | ||
6571 | if ( m_table ) | |
6572 | { | |
6573 | if (IsCellEditControlEnabled()) | |
6574 | DisableCellEditControl(); | |
6575 | ||
6576 | bool done = m_table->DeleteCols( pos, numCols ); | |
6577 | return done; | |
6578 | // the table will have sent the results of the delete col | |
6579 | // operation to this view object as a grid table message | |
6580 | } | |
6581 | ||
6582 | return false; | |
6583 | } | |
6584 | ||
6585 | // | |
6586 | // ----- event handlers | |
6587 | // | |
6588 | ||
6589 | // Generate a grid event based on a mouse event and | |
6590 | // return the result of ProcessEvent() | |
6591 | // | |
6592 | int wxGrid::SendEvent( const wxEventType type, | |
6593 | int row, int col, | |
6594 | wxMouseEvent& mouseEv ) | |
6595 | { | |
6596 | bool claimed, vetoed; | |
6597 | ||
6598 | if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE ) | |
6599 | { | |
6600 | int rowOrCol = (row == -1 ? col : row); | |
6601 | ||
6602 | wxGridSizeEvent gridEvt( GetId(), | |
6603 | type, | |
6604 | this, | |
6605 | rowOrCol, | |
6606 | mouseEv.GetX() + GetRowLabelSize(), | |
6607 | mouseEv.GetY() + GetColLabelSize(), | |
6608 | mouseEv.ControlDown(), | |
6609 | mouseEv.ShiftDown(), | |
6610 | mouseEv.AltDown(), | |
6611 | mouseEv.MetaDown() ); | |
6612 | ||
6613 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6614 | vetoed = !gridEvt.IsAllowed(); | |
6615 | } | |
6616 | else if ( type == wxEVT_GRID_RANGE_SELECT ) | |
6617 | { | |
6618 | // Right now, it should _never_ end up here! | |
6619 | wxGridRangeSelectEvent gridEvt( GetId(), | |
6620 | type, | |
6621 | this, | |
6622 | m_selectingTopLeft, | |
6623 | m_selectingBottomRight, | |
6624 | true, | |
6625 | mouseEv.ControlDown(), | |
6626 | mouseEv.ShiftDown(), | |
6627 | mouseEv.AltDown(), | |
6628 | mouseEv.MetaDown() ); | |
6629 | ||
6630 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6631 | vetoed = !gridEvt.IsAllowed(); | |
6632 | } | |
6633 | else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK || | |
6634 | type == wxEVT_GRID_LABEL_LEFT_DCLICK || | |
6635 | type == wxEVT_GRID_LABEL_RIGHT_CLICK || | |
6636 | type == wxEVT_GRID_LABEL_RIGHT_DCLICK ) | |
6637 | { | |
6638 | wxPoint pos = mouseEv.GetPosition(); | |
6639 | ||
6640 | if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() ) | |
6641 | pos.y += GetColLabelSize(); | |
6642 | if ( mouseEv.GetEventObject() == GetGridColLabelWindow() ) | |
6643 | pos.x += GetRowLabelSize(); | |
6644 | ||
6645 | wxGridEvent gridEvt( GetId(), | |
6646 | type, | |
6647 | this, | |
6648 | row, col, | |
6649 | pos.x, | |
6650 | pos.y, | |
6651 | false, | |
6652 | mouseEv.ControlDown(), | |
6653 | mouseEv.ShiftDown(), | |
6654 | mouseEv.AltDown(), | |
6655 | mouseEv.MetaDown() ); | |
6656 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6657 | vetoed = !gridEvt.IsAllowed(); | |
6658 | } | |
6659 | else | |
6660 | { | |
6661 | wxGridEvent gridEvt( GetId(), | |
6662 | type, | |
6663 | this, | |
6664 | row, col, | |
6665 | mouseEv.GetX() + GetRowLabelSize(), | |
6666 | mouseEv.GetY() + GetColLabelSize(), | |
6667 | false, | |
6668 | mouseEv.ControlDown(), | |
6669 | mouseEv.ShiftDown(), | |
6670 | mouseEv.AltDown(), | |
6671 | mouseEv.MetaDown() ); | |
6672 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6673 | vetoed = !gridEvt.IsAllowed(); | |
6674 | } | |
6675 | ||
6676 | // A Veto'd event may not be `claimed' so test this first | |
6677 | if (vetoed) | |
6678 | return -1; | |
6679 | ||
6680 | return claimed ? 1 : 0; | |
6681 | } | |
6682 | ||
6683 | // Generate a grid event of specified type and return the result | |
6684 | // of ProcessEvent(). | |
6685 | // | |
6686 | int wxGrid::SendEvent( const wxEventType type, | |
6687 | int row, int col ) | |
6688 | { | |
6689 | bool claimed, vetoed; | |
6690 | ||
6691 | if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE ) | |
6692 | { | |
6693 | int rowOrCol = (row == -1 ? col : row); | |
6694 | ||
6695 | wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol ); | |
6696 | ||
6697 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6698 | vetoed = !gridEvt.IsAllowed(); | |
6699 | } | |
6700 | else | |
6701 | { | |
6702 | wxGridEvent gridEvt( GetId(), type, this, row, col ); | |
6703 | ||
6704 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6705 | vetoed = !gridEvt.IsAllowed(); | |
6706 | } | |
6707 | ||
6708 | // A Veto'd event may not be `claimed' so test this first | |
6709 | if (vetoed) | |
6710 | return -1; | |
6711 | ||
6712 | return claimed ? 1 : 0; | |
6713 | } | |
6714 | ||
6715 | void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
6716 | { | |
6717 | // needed to prevent zillions of paint events on MSW | |
6718 | wxPaintDC dc(this); | |
6719 | } | |
6720 | ||
6721 | void wxGrid::Refresh(bool eraseb, const wxRect* rect) | |
6722 | { | |
6723 | // Don't do anything if between Begin/EndBatch... | |
6724 | // EndBatch() will do all this on the last nested one anyway. | |
6725 | if (! GetBatchCount()) | |
6726 | { | |
6727 | // Refresh to get correct scrolled position: | |
6728 | wxScrolledWindow::Refresh(eraseb, rect); | |
6729 | ||
6730 | if (rect) | |
6731 | { | |
6732 | int rect_x, rect_y, rectWidth, rectHeight; | |
6733 | int width_label, width_cell, height_label, height_cell; | |
6734 | int x, y; | |
6735 | ||
6736 | // Copy rectangle can get scroll offsets.. | |
6737 | rect_x = rect->GetX(); | |
6738 | rect_y = rect->GetY(); | |
6739 | rectWidth = rect->GetWidth(); | |
6740 | rectHeight = rect->GetHeight(); | |
6741 | ||
6742 | width_label = m_rowLabelWidth - rect_x; | |
6743 | if (width_label > rectWidth) | |
6744 | width_label = rectWidth; | |
6745 | ||
6746 | height_label = m_colLabelHeight - rect_y; | |
6747 | if (height_label > rectHeight) | |
6748 | height_label = rectHeight; | |
6749 | ||
6750 | if (rect_x > m_rowLabelWidth) | |
6751 | { | |
6752 | x = rect_x - m_rowLabelWidth; | |
6753 | width_cell = rectWidth; | |
6754 | } | |
6755 | else | |
6756 | { | |
6757 | x = 0; | |
6758 | width_cell = rectWidth - (m_rowLabelWidth - rect_x); | |
6759 | } | |
6760 | ||
6761 | if (rect_y > m_colLabelHeight) | |
6762 | { | |
6763 | y = rect_y - m_colLabelHeight; | |
6764 | height_cell = rectHeight; | |
6765 | } | |
6766 | else | |
6767 | { | |
6768 | y = 0; | |
6769 | height_cell = rectHeight - (m_colLabelHeight - rect_y); | |
6770 | } | |
6771 | ||
6772 | // Paint corner label part intersecting rect. | |
6773 | if ( width_label > 0 && height_label > 0 ) | |
6774 | { | |
6775 | wxRect anotherrect(rect_x, rect_y, width_label, height_label); | |
6776 | m_cornerLabelWin->Refresh(eraseb, &anotherrect); | |
6777 | } | |
6778 | ||
6779 | // Paint col labels part intersecting rect. | |
6780 | if ( width_cell > 0 && height_label > 0 ) | |
6781 | { | |
6782 | wxRect anotherrect(x, rect_y, width_cell, height_label); | |
6783 | m_colLabelWin->Refresh(eraseb, &anotherrect); | |
6784 | } | |
6785 | ||
6786 | // Paint row labels part intersecting rect. | |
6787 | if ( width_label > 0 && height_cell > 0 ) | |
6788 | { | |
6789 | wxRect anotherrect(rect_x, y, width_label, height_cell); | |
6790 | m_rowLabelWin->Refresh(eraseb, &anotherrect); | |
6791 | } | |
6792 | ||
6793 | // Paint cell area part intersecting rect. | |
6794 | if ( width_cell > 0 && height_cell > 0 ) | |
6795 | { | |
6796 | wxRect anotherrect(x, y, width_cell, height_cell); | |
6797 | m_gridWin->Refresh(eraseb, &anotherrect); | |
6798 | } | |
6799 | } | |
6800 | else | |
6801 | { | |
6802 | m_cornerLabelWin->Refresh(eraseb, NULL); | |
6803 | m_colLabelWin->Refresh(eraseb, NULL); | |
6804 | m_rowLabelWin->Refresh(eraseb, NULL); | |
6805 | m_gridWin->Refresh(eraseb, NULL); | |
6806 | } | |
6807 | } | |
6808 | } | |
6809 | ||
6810 | void wxGrid::OnSize( wxSizeEvent& event ) | |
6811 | { | |
6812 | // position the child windows | |
6813 | CalcWindowSizes(); | |
6814 | ||
6815 | // don't call CalcDimensions() from here, the base class handles the size | |
6816 | // changes itself | |
6817 | event.Skip(); | |
6818 | } | |
6819 | ||
6820 | void wxGrid::OnKeyDown( wxKeyEvent& event ) | |
6821 | { | |
6822 | if ( m_inOnKeyDown ) | |
6823 | { | |
6824 | // shouldn't be here - we are going round in circles... | |
6825 | // | |
6826 | wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") ); | |
6827 | } | |
6828 | ||
6829 | m_inOnKeyDown = true; | |
6830 | ||
6831 | // propagate the event up and see if it gets processed | |
6832 | wxWindow *parent = GetParent(); | |
6833 | wxKeyEvent keyEvt( event ); | |
6834 | keyEvt.SetEventObject( parent ); | |
6835 | ||
6836 | if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) ) | |
6837 | { | |
6838 | if (GetLayoutDirection() == wxLayout_RightToLeft) | |
6839 | { | |
6840 | if (event.GetKeyCode() == WXK_RIGHT) | |
6841 | event.m_keyCode = WXK_LEFT; | |
6842 | else if (event.GetKeyCode() == WXK_LEFT) | |
6843 | event.m_keyCode = WXK_RIGHT; | |
6844 | } | |
6845 | ||
6846 | // try local handlers | |
6847 | switch ( event.GetKeyCode() ) | |
6848 | { | |
6849 | case WXK_UP: | |
6850 | if ( event.ControlDown() ) | |
6851 | MoveCursorUpBlock( event.ShiftDown() ); | |
6852 | else | |
6853 | MoveCursorUp( event.ShiftDown() ); | |
6854 | break; | |
6855 | ||
6856 | case WXK_DOWN: | |
6857 | if ( event.ControlDown() ) | |
6858 | MoveCursorDownBlock( event.ShiftDown() ); | |
6859 | else | |
6860 | MoveCursorDown( event.ShiftDown() ); | |
6861 | break; | |
6862 | ||
6863 | case WXK_LEFT: | |
6864 | if ( event.ControlDown() ) | |
6865 | MoveCursorLeftBlock( event.ShiftDown() ); | |
6866 | else | |
6867 | MoveCursorLeft( event.ShiftDown() ); | |
6868 | break; | |
6869 | ||
6870 | case WXK_RIGHT: | |
6871 | if ( event.ControlDown() ) | |
6872 | MoveCursorRightBlock( event.ShiftDown() ); | |
6873 | else | |
6874 | MoveCursorRight( event.ShiftDown() ); | |
6875 | break; | |
6876 | ||
6877 | case WXK_RETURN: | |
6878 | case WXK_NUMPAD_ENTER: | |
6879 | if ( event.ControlDown() ) | |
6880 | { | |
6881 | event.Skip(); // to let the edit control have the return | |
6882 | } | |
6883 | else | |
6884 | { | |
6885 | if ( GetGridCursorRow() < GetNumberRows()-1 ) | |
6886 | { | |
6887 | MoveCursorDown( event.ShiftDown() ); | |
6888 | } | |
6889 | else | |
6890 | { | |
6891 | // at the bottom of a column | |
6892 | DisableCellEditControl(); | |
6893 | } | |
6894 | } | |
6895 | break; | |
6896 | ||
6897 | case WXK_ESCAPE: | |
6898 | ClearSelection(); | |
6899 | break; | |
6900 | ||
6901 | case WXK_TAB: | |
6902 | if (event.ShiftDown()) | |
6903 | { | |
6904 | if ( GetGridCursorCol() > 0 ) | |
6905 | { | |
6906 | MoveCursorLeft( false ); | |
6907 | } | |
6908 | else | |
6909 | { | |
6910 | // at left of grid | |
6911 | DisableCellEditControl(); | |
6912 | } | |
6913 | } | |
6914 | else | |
6915 | { | |
6916 | if ( GetGridCursorCol() < GetNumberCols() - 1 ) | |
6917 | { | |
6918 | MoveCursorRight( false ); | |
6919 | } | |
6920 | else | |
6921 | { | |
6922 | // at right of grid | |
6923 | DisableCellEditControl(); | |
6924 | } | |
6925 | } | |
6926 | break; | |
6927 | ||
6928 | case WXK_HOME: | |
6929 | if ( event.ControlDown() ) | |
6930 | { | |
6931 | MakeCellVisible( 0, 0 ); | |
6932 | SetCurrentCell( 0, 0 ); | |
6933 | } | |
6934 | else | |
6935 | { | |
6936 | event.Skip(); | |
6937 | } | |
6938 | break; | |
6939 | ||
6940 | case WXK_END: | |
6941 | if ( event.ControlDown() ) | |
6942 | { | |
6943 | MakeCellVisible( m_numRows - 1, m_numCols - 1 ); | |
6944 | SetCurrentCell( m_numRows - 1, m_numCols - 1 ); | |
6945 | } | |
6946 | else | |
6947 | { | |
6948 | event.Skip(); | |
6949 | } | |
6950 | break; | |
6951 | ||
6952 | case WXK_PAGEUP: | |
6953 | MovePageUp(); | |
6954 | break; | |
6955 | ||
6956 | case WXK_PAGEDOWN: | |
6957 | MovePageDown(); | |
6958 | break; | |
6959 | ||
6960 | case WXK_SPACE: | |
6961 | if ( event.ControlDown() ) | |
6962 | { | |
6963 | if ( m_selection ) | |
6964 | { | |
6965 | m_selection->ToggleCellSelection( | |
6966 | m_currentCellCoords.GetRow(), | |
6967 | m_currentCellCoords.GetCol(), | |
6968 | event.ControlDown(), | |
6969 | event.ShiftDown(), | |
6970 | event.AltDown(), | |
6971 | event.MetaDown() ); | |
6972 | } | |
6973 | break; | |
6974 | } | |
6975 | ||
6976 | if ( !IsEditable() ) | |
6977 | MoveCursorRight( false ); | |
6978 | else | |
6979 | event.Skip(); | |
6980 | break; | |
6981 | ||
6982 | default: | |
6983 | event.Skip(); | |
6984 | break; | |
6985 | } | |
6986 | } | |
6987 | ||
6988 | m_inOnKeyDown = false; | |
6989 | } | |
6990 | ||
6991 | void wxGrid::OnKeyUp( wxKeyEvent& event ) | |
6992 | { | |
6993 | // try local handlers | |
6994 | // | |
6995 | if ( event.GetKeyCode() == WXK_SHIFT ) | |
6996 | { | |
6997 | if ( m_selectingTopLeft != wxGridNoCellCoords && | |
6998 | m_selectingBottomRight != wxGridNoCellCoords ) | |
6999 | { | |
7000 | if ( m_selection ) | |
7001 | { | |
7002 | m_selection->SelectBlock( | |
7003 | m_selectingTopLeft.GetRow(), | |
7004 | m_selectingTopLeft.GetCol(), | |
7005 | m_selectingBottomRight.GetRow(), | |
7006 | m_selectingBottomRight.GetCol(), | |
7007 | event.ControlDown(), | |
7008 | true, | |
7009 | event.AltDown(), | |
7010 | event.MetaDown() ); | |
7011 | } | |
7012 | } | |
7013 | ||
7014 | m_selectingTopLeft = wxGridNoCellCoords; | |
7015 | m_selectingBottomRight = wxGridNoCellCoords; | |
7016 | m_selectingKeyboard = wxGridNoCellCoords; | |
7017 | } | |
7018 | } | |
7019 | ||
7020 | void wxGrid::OnChar( wxKeyEvent& event ) | |
7021 | { | |
7022 | // is it possible to edit the current cell at all? | |
7023 | if ( !IsCellEditControlEnabled() && CanEnableCellControl() ) | |
7024 | { | |
7025 | // yes, now check whether the cells editor accepts the key | |
7026 | int row = m_currentCellCoords.GetRow(); | |
7027 | int col = m_currentCellCoords.GetCol(); | |
7028 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
7029 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); | |
7030 | ||
7031 | // <F2> is special and will always start editing, for | |
7032 | // other keys - ask the editor itself | |
7033 | if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers()) | |
7034 | || editor->IsAcceptedKey(event) ) | |
7035 | { | |
7036 | // ensure cell is visble | |
7037 | MakeCellVisible(row, col); | |
7038 | EnableCellEditControl(); | |
7039 | ||
7040 | // a problem can arise if the cell is not completely | |
7041 | // visible (even after calling MakeCellVisible the | |
7042 | // control is not created and calling StartingKey will | |
7043 | // crash the app | |
7044 | if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled ) | |
7045 | editor->StartingKey(event); | |
7046 | } | |
7047 | else | |
7048 | { | |
7049 | event.Skip(); | |
7050 | } | |
7051 | ||
7052 | editor->DecRef(); | |
7053 | attr->DecRef(); | |
7054 | } | |
7055 | else | |
7056 | { | |
7057 | event.Skip(); | |
7058 | } | |
7059 | } | |
7060 | ||
7061 | void wxGrid::OnEraseBackground(wxEraseEvent&) | |
7062 | { | |
7063 | } | |
7064 | ||
7065 | void wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) | |
7066 | { | |
7067 | if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) ) | |
7068 | { | |
7069 | // the event has been intercepted - do nothing | |
7070 | return; | |
7071 | } | |
7072 | ||
7073 | wxClientDC dc( m_gridWin ); | |
7074 | PrepareDC( dc ); | |
7075 | ||
7076 | if ( m_currentCellCoords != wxGridNoCellCoords ) | |
7077 | { | |
7078 | DisableCellEditControl(); | |
7079 | ||
7080 | if ( IsVisible( m_currentCellCoords, false ) ) | |
7081 | { | |
7082 | wxRect r; | |
7083 | r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords ); | |
7084 | if ( !m_gridLinesEnabled ) | |
7085 | { | |
7086 | r.x--; | |
7087 | r.y--; | |
7088 | r.width++; | |
7089 | r.height++; | |
7090 | } | |
7091 | ||
7092 | wxGridCellCoordsArray cells = CalcCellsExposed( r ); | |
7093 | ||
7094 | // Otherwise refresh redraws the highlight! | |
7095 | m_currentCellCoords = coords; | |
7096 | ||
7097 | DrawGridCellArea( dc, cells ); | |
7098 | DrawAllGridLines( dc, r ); | |
7099 | } | |
7100 | } | |
7101 | ||
7102 | m_currentCellCoords = coords; | |
7103 | ||
7104 | wxGridCellAttr *attr = GetCellAttr( coords ); | |
7105 | DrawCellHighlight( dc, attr ); | |
7106 | attr->DecRef(); | |
7107 | } | |
7108 | ||
7109 | void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol ) | |
7110 | { | |
7111 | int temp; | |
7112 | wxGridCellCoords updateTopLeft, updateBottomRight; | |
7113 | ||
7114 | if ( m_selection ) | |
7115 | { | |
7116 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows ) | |
7117 | { | |
7118 | leftCol = 0; | |
7119 | rightCol = GetNumberCols() - 1; | |
7120 | } | |
7121 | else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns ) | |
7122 | { | |
7123 | topRow = 0; | |
7124 | bottomRow = GetNumberRows() - 1; | |
7125 | } | |
7126 | } | |
7127 | ||
7128 | if ( topRow > bottomRow ) | |
7129 | { | |
7130 | temp = topRow; | |
7131 | topRow = bottomRow; | |
7132 | bottomRow = temp; | |
7133 | } | |
7134 | ||
7135 | if ( leftCol > rightCol ) | |
7136 | { | |
7137 | temp = leftCol; | |
7138 | leftCol = rightCol; | |
7139 | rightCol = temp; | |
7140 | } | |
7141 | ||
7142 | updateTopLeft = wxGridCellCoords( topRow, leftCol ); | |
7143 | updateBottomRight = wxGridCellCoords( bottomRow, rightCol ); | |
7144 | ||
7145 | // First the case that we selected a completely new area | |
7146 | if ( m_selectingTopLeft == wxGridNoCellCoords || | |
7147 | m_selectingBottomRight == wxGridNoCellCoords ) | |
7148 | { | |
7149 | wxRect rect; | |
7150 | rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ), | |
7151 | wxGridCellCoords ( bottomRow, rightCol ) ); | |
7152 | m_gridWin->Refresh( false, &rect ); | |
7153 | } | |
7154 | ||
7155 | // Now handle changing an existing selection area. | |
7156 | else if ( m_selectingTopLeft != updateTopLeft || | |
7157 | m_selectingBottomRight != updateBottomRight ) | |
7158 | { | |
7159 | // Compute two optimal update rectangles: | |
7160 | // Either one rectangle is a real subset of the | |
7161 | // other, or they are (almost) disjoint! | |
7162 | wxRect rect[4]; | |
7163 | bool need_refresh[4]; | |
7164 | need_refresh[0] = | |
7165 | need_refresh[1] = | |
7166 | need_refresh[2] = | |
7167 | need_refresh[3] = false; | |
7168 | int i; | |
7169 | ||
7170 | // Store intermediate values | |
7171 | wxCoord oldLeft = m_selectingTopLeft.GetCol(); | |
7172 | wxCoord oldTop = m_selectingTopLeft.GetRow(); | |
7173 | wxCoord oldRight = m_selectingBottomRight.GetCol(); | |
7174 | wxCoord oldBottom = m_selectingBottomRight.GetRow(); | |
7175 | ||
7176 | // Determine the outer/inner coordinates. | |
7177 | if (oldLeft > leftCol) | |
7178 | { | |
7179 | temp = oldLeft; | |
7180 | oldLeft = leftCol; | |
7181 | leftCol = temp; | |
7182 | } | |
7183 | if (oldTop > topRow ) | |
7184 | { | |
7185 | temp = oldTop; | |
7186 | oldTop = topRow; | |
7187 | topRow = temp; | |
7188 | } | |
7189 | if (oldRight < rightCol ) | |
7190 | { | |
7191 | temp = oldRight; | |
7192 | oldRight = rightCol; | |
7193 | rightCol = temp; | |
7194 | } | |
7195 | if (oldBottom < bottomRow) | |
7196 | { | |
7197 | temp = oldBottom; | |
7198 | oldBottom = bottomRow; | |
7199 | bottomRow = temp; | |
7200 | } | |
7201 | ||
7202 | // Now, either the stuff marked old is the outer | |
7203 | // rectangle or we don't have a situation where one | |
7204 | // is contained in the other. | |
7205 | ||
7206 | if ( oldLeft < leftCol ) | |
7207 | { | |
7208 | // Refresh the newly selected or deselected | |
7209 | // area to the left of the old or new selection. | |
7210 | need_refresh[0] = true; | |
7211 | rect[0] = BlockToDeviceRect( | |
7212 | wxGridCellCoords( oldTop, oldLeft ), | |
7213 | wxGridCellCoords( oldBottom, leftCol - 1 ) ); | |
7214 | } | |
7215 | ||
7216 | if ( oldTop < topRow ) | |
7217 | { | |
7218 | // Refresh the newly selected or deselected | |
7219 | // area above the old or new selection. | |
7220 | need_refresh[1] = true; | |
7221 | rect[1] = BlockToDeviceRect( | |
7222 | wxGridCellCoords( oldTop, leftCol ), | |
7223 | wxGridCellCoords( topRow - 1, rightCol ) ); | |
7224 | } | |
7225 | ||
7226 | if ( oldRight > rightCol ) | |
7227 | { | |
7228 | // Refresh the newly selected or deselected | |
7229 | // area to the right of the old or new selection. | |
7230 | need_refresh[2] = true; | |
7231 | rect[2] = BlockToDeviceRect( | |
7232 | wxGridCellCoords( oldTop, rightCol + 1 ), | |
7233 | wxGridCellCoords( oldBottom, oldRight ) ); | |
7234 | } | |
7235 | ||
7236 | if ( oldBottom > bottomRow ) | |
7237 | { | |
7238 | // Refresh the newly selected or deselected | |
7239 | // area below the old or new selection. | |
7240 | need_refresh[3] = true; | |
7241 | rect[3] = BlockToDeviceRect( | |
7242 | wxGridCellCoords( bottomRow + 1, leftCol ), | |
7243 | wxGridCellCoords( oldBottom, rightCol ) ); | |
7244 | } | |
7245 | ||
7246 | // various Refresh() calls | |
7247 | for (i = 0; i < 4; i++ ) | |
7248 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
7249 | m_gridWin->Refresh( false, &(rect[i]) ); | |
7250 | } | |
7251 | ||
7252 | // change selection | |
7253 | m_selectingTopLeft = updateTopLeft; | |
7254 | m_selectingBottomRight = updateBottomRight; | |
7255 | } | |
7256 | ||
7257 | // | |
7258 | // ------ functions to get/send data (see also public functions) | |
7259 | // | |
7260 | ||
7261 | bool wxGrid::GetModelValues() | |
7262 | { | |
7263 | // Hide the editor, so it won't hide a changed value. | |
7264 | HideCellEditControl(); | |
7265 | ||
7266 | if ( m_table ) | |
7267 | { | |
7268 | // all we need to do is repaint the grid | |
7269 | // | |
7270 | m_gridWin->Refresh(); | |
7271 | return true; | |
7272 | } | |
7273 | ||
7274 | return false; | |
7275 | } | |
7276 | ||
7277 | bool wxGrid::SetModelValues() | |
7278 | { | |
7279 | int row, col; | |
7280 | ||
7281 | // Disable the editor, so it won't hide a changed value. | |
7282 | // Do we also want to save the current value of the editor first? | |
7283 | // I think so ... | |
7284 | DisableCellEditControl(); | |
7285 | ||
7286 | if ( m_table ) | |
7287 | { | |
7288 | for ( row = 0; row < m_numRows; row++ ) | |
7289 | { | |
7290 | for ( col = 0; col < m_numCols; col++ ) | |
7291 | { | |
7292 | m_table->SetValue( row, col, GetCellValue(row, col) ); | |
7293 | } | |
7294 | } | |
7295 | ||
7296 | return true; | |
7297 | } | |
7298 | ||
7299 | return false; | |
7300 | } | |
7301 | ||
7302 | // Note - this function only draws cells that are in the list of | |
7303 | // exposed cells (usually set from the update region by | |
7304 | // CalcExposedCells) | |
7305 | // | |
7306 | void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells ) | |
7307 | { | |
7308 | if ( !m_numRows || !m_numCols ) | |
7309 | return; | |
7310 | ||
7311 | int i, numCells = cells.GetCount(); | |
7312 | int row, col, cell_rows, cell_cols; | |
7313 | wxGridCellCoordsArray redrawCells; | |
7314 | ||
7315 | for ( i = numCells - 1; i >= 0; i-- ) | |
7316 | { | |
7317 | row = cells[i].GetRow(); | |
7318 | col = cells[i].GetCol(); | |
7319 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
7320 | ||
7321 | // If this cell is part of a multicell block, find owner for repaint | |
7322 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
7323 | { | |
7324 | wxGridCellCoords cell( row + cell_rows, col + cell_cols ); | |
7325 | bool marked = false; | |
7326 | for ( int j = 0; j < numCells; j++ ) | |
7327 | { | |
7328 | if ( cell == cells[j] ) | |
7329 | { | |
7330 | marked = true; | |
7331 | break; | |
7332 | } | |
7333 | } | |
7334 | ||
7335 | if (!marked) | |
7336 | { | |
7337 | int count = redrawCells.GetCount(); | |
7338 | for (int j = 0; j < count; j++) | |
7339 | { | |
7340 | if ( cell == redrawCells[j] ) | |
7341 | { | |
7342 | marked = true; | |
7343 | break; | |
7344 | } | |
7345 | } | |
7346 | ||
7347 | if (!marked) | |
7348 | redrawCells.Add( cell ); | |
7349 | } | |
7350 | ||
7351 | // don't bother drawing this cell | |
7352 | continue; | |
7353 | } | |
7354 | ||
7355 | // If this cell is empty, find cell to left that might want to overflow | |
7356 | if (m_table && m_table->IsEmptyCell(row, col)) | |
7357 | { | |
7358 | for ( int l = 0; l < cell_rows; l++ ) | |
7359 | { | |
7360 | // find a cell in this row to leave already marked for repaint | |
7361 | int left = col; | |
7362 | for (int k = 0; k < int(redrawCells.GetCount()); k++) | |
7363 | if ((redrawCells[k].GetCol() < left) && | |
7364 | (redrawCells[k].GetRow() == row)) | |
7365 | { | |
7366 | left = redrawCells[k].GetCol(); | |
7367 | } | |
7368 | ||
7369 | if (left == col) | |
7370 | left = 0; // oh well | |
7371 | ||
7372 | for (int j = col - 1; j >= left; j--) | |
7373 | { | |
7374 | if (!m_table->IsEmptyCell(row + l, j)) | |
7375 | { | |
7376 | if (GetCellOverflow(row + l, j)) | |
7377 | { | |
7378 | wxGridCellCoords cell(row + l, j); | |
7379 | bool marked = false; | |
7380 | ||
7381 | for (int k = 0; k < numCells; k++) | |
7382 | { | |
7383 | if ( cell == cells[k] ) | |
7384 | { | |
7385 | marked = true; | |
7386 | break; | |
7387 | } | |
7388 | } | |
7389 | ||
7390 | if (!marked) | |
7391 | { | |
7392 | int count = redrawCells.GetCount(); | |
7393 | for (int k = 0; k < count; k++) | |
7394 | { | |
7395 | if ( cell == redrawCells[k] ) | |
7396 | { | |
7397 | marked = true; | |
7398 | break; | |
7399 | } | |
7400 | } | |
7401 | if (!marked) | |
7402 | redrawCells.Add( cell ); | |
7403 | } | |
7404 | } | |
7405 | break; | |
7406 | } | |
7407 | } | |
7408 | } | |
7409 | } | |
7410 | ||
7411 | DrawCell( dc, cells[i] ); | |
7412 | } | |
7413 | ||
7414 | numCells = redrawCells.GetCount(); | |
7415 | ||
7416 | for ( i = numCells - 1; i >= 0; i-- ) | |
7417 | { | |
7418 | DrawCell( dc, redrawCells[i] ); | |
7419 | } | |
7420 | } | |
7421 | ||
7422 | void wxGrid::DrawGridSpace( wxDC& dc ) | |
7423 | { | |
7424 | int cw, ch; | |
7425 | m_gridWin->GetClientSize( &cw, &ch ); | |
7426 | ||
7427 | int right, bottom; | |
7428 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7429 | ||
7430 | int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0; | |
7431 | int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0; | |
7432 | ||
7433 | if ( right > rightCol || bottom > bottomRow ) | |
7434 | { | |
7435 | int left, top; | |
7436 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7437 | ||
7438 | dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) ); | |
7439 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
7440 | ||
7441 | if ( right > rightCol ) | |
7442 | { | |
7443 | dc.DrawRectangle( rightCol, top, right - rightCol, ch ); | |
7444 | } | |
7445 | ||
7446 | if ( bottom > bottomRow ) | |
7447 | { | |
7448 | dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow ); | |
7449 | } | |
7450 | } | |
7451 | } | |
7452 | ||
7453 | void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) | |
7454 | { | |
7455 | int row = coords.GetRow(); | |
7456 | int col = coords.GetCol(); | |
7457 | ||
7458 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
7459 | return; | |
7460 | ||
7461 | // we draw the cell border ourselves | |
7462 | #if !WXGRID_DRAW_LINES | |
7463 | if ( m_gridLinesEnabled ) | |
7464 | DrawCellBorder( dc, coords ); | |
7465 | #endif | |
7466 | ||
7467 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
7468 | ||
7469 | bool isCurrent = coords == m_currentCellCoords; | |
7470 | ||
7471 | wxRect rect = CellToRect( row, col ); | |
7472 | ||
7473 | // if the editor is shown, we should use it and not the renderer | |
7474 | // Note: However, only if it is really _shown_, i.e. not hidden! | |
7475 | if ( isCurrent && IsCellEditControlShown() ) | |
7476 | { | |
7477 | // NB: this "#if..." is temporary and fixes a problem where the | |
7478 | // edit control is erased by this code after being rendered. | |
7479 | // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered | |
7480 | // implicitly, causing this out-of order render. | |
7481 | #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS | |
7482 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); | |
7483 | editor->PaintBackground(rect, attr); | |
7484 | editor->DecRef(); | |
7485 | #endif | |
7486 | } | |
7487 | else | |
7488 | { | |
7489 | // but all the rest is drawn by the cell renderer and hence may be customized | |
7490 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); | |
7491 | renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); | |
7492 | renderer->DecRef(); | |
7493 | } | |
7494 | ||
7495 | attr->DecRef(); | |
7496 | } | |
7497 | ||
7498 | void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ) | |
7499 | { | |
7500 | int row = m_currentCellCoords.GetRow(); | |
7501 | int col = m_currentCellCoords.GetCol(); | |
7502 | ||
7503 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
7504 | return; | |
7505 | ||
7506 | wxRect rect = CellToRect(row, col); | |
7507 | ||
7508 | // hmmm... what could we do here to show that the cell is disabled? | |
7509 | // for now, I just draw a thinner border than for the other ones, but | |
7510 | // it doesn't look really good | |
7511 | ||
7512 | int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth; | |
7513 | ||
7514 | if (penWidth > 0) | |
7515 | { | |
7516 | // The center of the drawn line is where the position/width/height of | |
7517 | // the rectangle is actually at (on wxMSW at least), so the | |
7518 | // size of the rectangle is reduced to compensate for the thickness of | |
7519 | // the line. If this is too strange on non-wxMSW platforms then | |
7520 | // please #ifdef this appropriately. | |
7521 | rect.x += penWidth / 2; | |
7522 | rect.y += penWidth / 2; | |
7523 | rect.width -= penWidth - 1; | |
7524 | rect.height -= penWidth - 1; | |
7525 | ||
7526 | // Now draw the rectangle | |
7527 | // use the cellHighlightColour if the cell is inside a selection, this | |
7528 | // will ensure the cell is always visible. | |
7529 | dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground : m_cellHighlightColour, penWidth, wxSOLID)); | |
7530 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
7531 | dc.DrawRectangle(rect); | |
7532 | } | |
7533 | ||
7534 | #if 0 | |
7535 | // VZ: my experiments with 3D borders... | |
7536 | ||
7537 | // how to properly set colours for arbitrary bg? | |
7538 | wxCoord x1 = rect.x, | |
7539 | y1 = rect.y, | |
7540 | x2 = rect.x + rect.width - 1, | |
7541 | y2 = rect.y + rect.height - 1; | |
7542 | ||
7543 | dc.SetPen(*wxWHITE_PEN); | |
7544 | dc.DrawLine(x1, y1, x2, y1); | |
7545 | dc.DrawLine(x1, y1, x1, y2); | |
7546 | ||
7547 | dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1); | |
7548 | dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2); | |
7549 | ||
7550 | dc.SetPen(*wxBLACK_PEN); | |
7551 | dc.DrawLine(x1, y2, x2, y2); | |
7552 | dc.DrawLine(x2, y1, x2, y2 + 1); | |
7553 | #endif | |
7554 | } | |
7555 | ||
7556 | wxPen wxGrid::GetDefaultGridLinePen() | |
7557 | { | |
7558 | return wxPen(GetGridLineColour(), 1, wxSOLID); | |
7559 | } | |
7560 | ||
7561 | wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row)) | |
7562 | { | |
7563 | return GetDefaultGridLinePen(); | |
7564 | } | |
7565 | ||
7566 | wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col)) | |
7567 | { | |
7568 | return GetDefaultGridLinePen(); | |
7569 | } | |
7570 | ||
7571 | void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) | |
7572 | { | |
7573 | int row = coords.GetRow(); | |
7574 | int col = coords.GetCol(); | |
7575 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
7576 | return; | |
7577 | ||
7578 | ||
7579 | wxRect rect = CellToRect( row, col ); | |
7580 | ||
7581 | // right hand border | |
7582 | dc.SetPen( GetColGridLinePen(col) ); | |
7583 | dc.DrawLine( rect.x + rect.width, rect.y, | |
7584 | rect.x + rect.width, rect.y + rect.height + 1 ); | |
7585 | ||
7586 | // bottom border | |
7587 | dc.SetPen( GetRowGridLinePen(row) ); | |
7588 | dc.DrawLine( rect.x, rect.y + rect.height, | |
7589 | rect.x + rect.width, rect.y + rect.height); | |
7590 | } | |
7591 | ||
7592 | void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells) | |
7593 | { | |
7594 | // This if block was previously in wxGrid::OnPaint but that doesn't | |
7595 | // seem to get called under wxGTK - MB | |
7596 | // | |
7597 | if ( m_currentCellCoords == wxGridNoCellCoords && | |
7598 | m_numRows && m_numCols ) | |
7599 | { | |
7600 | m_currentCellCoords.Set(0, 0); | |
7601 | } | |
7602 | ||
7603 | if ( IsCellEditControlShown() ) | |
7604 | { | |
7605 | // don't show highlight when the edit control is shown | |
7606 | return; | |
7607 | } | |
7608 | ||
7609 | // if the active cell was repainted, repaint its highlight too because it | |
7610 | // might have been damaged by the grid lines | |
7611 | size_t count = cells.GetCount(); | |
7612 | for ( size_t n = 0; n < count; n++ ) | |
7613 | { | |
7614 | if ( cells[n] == m_currentCellCoords ) | |
7615 | { | |
7616 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
7617 | DrawCellHighlight(dc, attr); | |
7618 | attr->DecRef(); | |
7619 | ||
7620 | break; | |
7621 | } | |
7622 | } | |
7623 | } | |
7624 | ||
7625 | // TODO: remove this ??? | |
7626 | // This is used to redraw all grid lines e.g. when the grid line colour | |
7627 | // has been changed | |
7628 | // | |
7629 | void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) ) | |
7630 | { | |
7631 | #if !WXGRID_DRAW_LINES | |
7632 | return; | |
7633 | #endif | |
7634 | ||
7635 | if ( !m_gridLinesEnabled || !m_numRows || !m_numCols ) | |
7636 | return; | |
7637 | ||
7638 | int top, bottom, left, right; | |
7639 | ||
7640 | #if 0 //#ifndef __WXGTK__ | |
7641 | if (reg.IsEmpty()) | |
7642 | { | |
7643 | int cw, ch; | |
7644 | m_gridWin->GetClientSize(&cw, &ch); | |
7645 | ||
7646 | // virtual coords of visible area | |
7647 | // | |
7648 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7649 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7650 | } | |
7651 | else | |
7652 | { | |
7653 | wxCoord x, y, w, h; | |
7654 | reg.GetBox(x, y, w, h); | |
7655 | CalcUnscrolledPosition( x, y, &left, &top ); | |
7656 | CalcUnscrolledPosition( x + w, y + h, &right, &bottom ); | |
7657 | } | |
7658 | #else | |
7659 | int cw, ch; | |
7660 | m_gridWin->GetClientSize(&cw, &ch); | |
7661 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7662 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7663 | #endif | |
7664 | ||
7665 | // avoid drawing grid lines past the last row and col | |
7666 | // | |
7667 | right = wxMin( right, GetColRight(GetColAt( m_numCols - 1 )) ); | |
7668 | bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) ); | |
7669 | ||
7670 | // no gridlines inside multicells, clip them out | |
7671 | int leftCol = GetColPos( internalXToCol(left) ); | |
7672 | int topRow = internalYToRow(top); | |
7673 | int rightCol = GetColPos( internalXToCol(right) ); | |
7674 | int bottomRow = internalYToRow(bottom); | |
7675 | ||
7676 | #ifndef __WXMAC__ | |
7677 | // CS: I don't know why suddenly unscrolled coordinates are used for clipping | |
7678 | wxRegion clippedcells(0, 0, cw, ch); | |
7679 | ||
7680 | int i, j, cell_rows, cell_cols; | |
7681 | wxRect rect; | |
7682 | ||
7683 | for (j=topRow; j<bottomRow; j++) | |
7684 | { | |
7685 | int colPos; | |
7686 | for (colPos=leftCol; colPos<rightCol; colPos++) | |
7687 | { | |
7688 | i = GetColAt( colPos ); | |
7689 | ||
7690 | GetCellSize( j, i, &cell_rows, &cell_cols ); | |
7691 | if ((cell_rows > 1) || (cell_cols > 1)) | |
7692 | { | |
7693 | rect = CellToRect(j,i); | |
7694 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
7695 | clippedcells.Subtract(rect); | |
7696 | } | |
7697 | else if ((cell_rows < 0) || (cell_cols < 0)) | |
7698 | { | |
7699 | rect = CellToRect(j + cell_rows, i + cell_cols); | |
7700 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
7701 | clippedcells.Subtract(rect); | |
7702 | } | |
7703 | } | |
7704 | } | |
7705 | #else | |
7706 | wxRegion clippedcells( left, top, right - left, bottom - top ); | |
7707 | ||
7708 | int i, j, cell_rows, cell_cols; | |
7709 | wxRect rect; | |
7710 | ||
7711 | for (j=topRow; j<bottomRow; j++) | |
7712 | { | |
7713 | for (i=leftCol; i<rightCol; i++) | |
7714 | { | |
7715 | GetCellSize( j, i, &cell_rows, &cell_cols ); | |
7716 | if ((cell_rows > 1) || (cell_cols > 1)) | |
7717 | { | |
7718 | rect = CellToRect(j, i); | |
7719 | clippedcells.Subtract(rect); | |
7720 | } | |
7721 | else if ((cell_rows < 0) || (cell_cols < 0)) | |
7722 | { | |
7723 | rect = CellToRect(j + cell_rows, i + cell_cols); | |
7724 | clippedcells.Subtract(rect); | |
7725 | } | |
7726 | } | |
7727 | } | |
7728 | #endif | |
7729 | ||
7730 | dc.SetClippingRegion( clippedcells ); | |
7731 | ||
7732 | ||
7733 | // horizontal grid lines | |
7734 | // | |
7735 | // already declared above - int i; | |
7736 | for ( i = internalYToRow(top); i < m_numRows; i++ ) | |
7737 | { | |
7738 | int bot = GetRowBottom(i) - 1; | |
7739 | ||
7740 | if ( bot > bottom ) | |
7741 | { | |
7742 | break; | |
7743 | } | |
7744 | ||
7745 | if ( bot >= top ) | |
7746 | { | |
7747 | dc.SetPen( GetRowGridLinePen(i) ); | |
7748 | dc.DrawLine( left, bot, right, bot ); | |
7749 | } | |
7750 | } | |
7751 | ||
7752 | // vertical grid lines | |
7753 | // | |
7754 | int colPos; | |
7755 | for ( colPos = leftCol; colPos < m_numCols; colPos++ ) | |
7756 | { | |
7757 | i = GetColAt( colPos ); | |
7758 | ||
7759 | int colRight = GetColRight(i); | |
7760 | #ifdef __WXGTK__ | |
7761 | if (GetLayoutDirection() != wxLayout_RightToLeft) | |
7762 | #endif | |
7763 | colRight--; | |
7764 | ||
7765 | if ( colRight > right ) | |
7766 | { | |
7767 | break; | |
7768 | } | |
7769 | ||
7770 | if ( colRight >= left ) | |
7771 | { | |
7772 | dc.SetPen( GetColGridLinePen(i) ); | |
7773 | dc.DrawLine( colRight, top, colRight, bottom ); | |
7774 | } | |
7775 | } | |
7776 | ||
7777 | dc.DestroyClippingRegion(); | |
7778 | } | |
7779 | ||
7780 | void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows) | |
7781 | { | |
7782 | if ( !m_numRows ) | |
7783 | return; | |
7784 | ||
7785 | size_t i; | |
7786 | size_t numLabels = rows.GetCount(); | |
7787 | ||
7788 | for ( i = 0; i < numLabels; i++ ) | |
7789 | { | |
7790 | DrawRowLabel( dc, rows[i] ); | |
7791 | } | |
7792 | } | |
7793 | ||
7794 | void wxGrid::DrawRowLabel( wxDC& dc, int row ) | |
7795 | { | |
7796 | if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 ) | |
7797 | return; | |
7798 | ||
7799 | wxRect rect; | |
7800 | ||
7801 | #ifdef __WXGTK20__ | |
7802 | rect.SetX( 1 ); | |
7803 | rect.SetY( GetRowTop(row) + 1 ); | |
7804 | rect.SetWidth( m_rowLabelWidth - 2 ); | |
7805 | rect.SetHeight( GetRowHeight(row) - 2 ); | |
7806 | ||
7807 | CalcScrolledPosition( 0, rect.y, NULL, &rect.y ); | |
7808 | ||
7809 | wxWindowDC *win_dc = (wxWindowDC*) &dc; | |
7810 | ||
7811 | wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 ); | |
7812 | #else | |
7813 | int rowTop = GetRowTop(row), | |
7814 | rowBottom = GetRowBottom(row) - 1; | |
7815 | ||
7816 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) ); | |
7817 | dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom ); | |
7818 | dc.DrawLine( 0, rowTop, 0, rowBottom ); | |
7819 | dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom ); | |
7820 | ||
7821 | dc.SetPen( *wxWHITE_PEN ); | |
7822 | dc.DrawLine( 1, rowTop, 1, rowBottom ); | |
7823 | dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop ); | |
7824 | #endif | |
7825 | ||
7826 | dc.SetBackgroundMode( wxTRANSPARENT ); | |
7827 | dc.SetTextForeground( GetLabelTextColour() ); | |
7828 | dc.SetFont( GetLabelFont() ); | |
7829 | ||
7830 | int hAlign, vAlign; | |
7831 | GetRowLabelAlignment( &hAlign, &vAlign ); | |
7832 | ||
7833 | rect.SetX( 2 ); | |
7834 | rect.SetY( GetRowTop(row) + 2 ); | |
7835 | rect.SetWidth( m_rowLabelWidth - 4 ); | |
7836 | rect.SetHeight( GetRowHeight(row) - 4 ); | |
7837 | DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign ); | |
7838 | } | |
7839 | ||
7840 | void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols ) | |
7841 | { | |
7842 | if ( !m_numCols ) | |
7843 | return; | |
7844 | ||
7845 | size_t i; | |
7846 | size_t numLabels = cols.GetCount(); | |
7847 | ||
7848 | for ( i = 0; i < numLabels; i++ ) | |
7849 | { | |
7850 | DrawColLabel( dc, cols[i] ); | |
7851 | } | |
7852 | } | |
7853 | ||
7854 | void wxGrid::DrawColLabel( wxDC& dc, int col ) | |
7855 | { | |
7856 | if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 ) | |
7857 | return; | |
7858 | ||
7859 | int colLeft = GetColLeft(col); | |
7860 | ||
7861 | wxRect rect; | |
7862 | ||
7863 | #ifdef __WXGTK20__ | |
7864 | rect.SetX( colLeft + 1 ); | |
7865 | rect.SetY( 1 ); | |
7866 | rect.SetWidth( GetColWidth(col) - 2 ); | |
7867 | rect.SetHeight( m_colLabelHeight - 2 ); | |
7868 | ||
7869 | wxWindowDC *win_dc = (wxWindowDC*) &dc; | |
7870 | ||
7871 | wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 ); | |
7872 | #else | |
7873 | int colRight = GetColRight(col) - 1; | |
7874 | ||
7875 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) ); | |
7876 | dc.DrawLine( colRight, 0, colRight, m_colLabelHeight - 1 ); | |
7877 | dc.DrawLine( colLeft, 0, colRight, 0 ); | |
7878 | dc.DrawLine( colLeft, m_colLabelHeight - 1, | |
7879 | colRight + 1, m_colLabelHeight - 1 ); | |
7880 | ||
7881 | dc.SetPen( *wxWHITE_PEN ); | |
7882 | dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 ); | |
7883 | dc.DrawLine( colLeft, 1, colRight, 1 ); | |
7884 | #endif | |
7885 | ||
7886 | dc.SetBackgroundMode( wxTRANSPARENT ); | |
7887 | dc.SetTextForeground( GetLabelTextColour() ); | |
7888 | dc.SetFont( GetLabelFont() ); | |
7889 | ||
7890 | int hAlign, vAlign, orient; | |
7891 | GetColLabelAlignment( &hAlign, &vAlign ); | |
7892 | orient = GetColLabelTextOrientation(); | |
7893 | ||
7894 | rect.SetX( colLeft + 2 ); | |
7895 | rect.SetY( 2 ); | |
7896 | rect.SetWidth( GetColWidth(col) - 4 ); | |
7897 | rect.SetHeight( m_colLabelHeight - 4 ); | |
7898 | DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient ); | |
7899 | } | |
7900 | ||
7901 | void wxGrid::DrawTextRectangle( wxDC& dc, | |
7902 | const wxString& value, | |
7903 | const wxRect& rect, | |
7904 | int horizAlign, | |
7905 | int vertAlign, | |
7906 | int textOrientation ) | |
7907 | { | |
7908 | wxArrayString lines; | |
7909 | ||
7910 | StringToLines( value, lines ); | |
7911 | ||
7912 | // Forward to new API. | |
7913 | DrawTextRectangle( dc, | |
7914 | lines, | |
7915 | rect, | |
7916 | horizAlign, | |
7917 | vertAlign, | |
7918 | textOrientation ); | |
7919 | } | |
7920 | ||
7921 | // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to | |
7922 | // add textOrientation support | |
7923 | void wxGrid::DrawTextRectangle(wxDC& dc, | |
7924 | const wxArrayString& lines, | |
7925 | const wxRect& rect, | |
7926 | int horizAlign, | |
7927 | int vertAlign, | |
7928 | int textOrientation) | |
7929 | { | |
7930 | if ( lines.empty() ) | |
7931 | return; | |
7932 | ||
7933 | wxDCClipper clip(dc, rect); | |
7934 | ||
7935 | long textWidth, | |
7936 | textHeight; | |
7937 | ||
7938 | if ( textOrientation == wxHORIZONTAL ) | |
7939 | GetTextBoxSize( dc, lines, &textWidth, &textHeight ); | |
7940 | else | |
7941 | GetTextBoxSize( dc, lines, &textHeight, &textWidth ); | |
7942 | ||
7943 | int x = 0, | |
7944 | y = 0; | |
7945 | switch ( vertAlign ) | |
7946 | { | |
7947 | case wxALIGN_BOTTOM: | |
7948 | if ( textOrientation == wxHORIZONTAL ) | |
7949 | y = rect.y + (rect.height - textHeight - 1); | |
7950 | else | |
7951 | x = rect.x + rect.width - textWidth; | |
7952 | break; | |
7953 | ||
7954 | case wxALIGN_CENTRE: | |
7955 | if ( textOrientation == wxHORIZONTAL ) | |
7956 | y = rect.y + ((rect.height - textHeight) / 2); | |
7957 | else | |
7958 | x = rect.x + ((rect.width - textWidth) / 2); | |
7959 | break; | |
7960 | ||
7961 | case wxALIGN_TOP: | |
7962 | default: | |
7963 | if ( textOrientation == wxHORIZONTAL ) | |
7964 | y = rect.y + 1; | |
7965 | else | |
7966 | x = rect.x + 1; | |
7967 | break; | |
7968 | } | |
7969 | ||
7970 | // Align each line of a multi-line label | |
7971 | size_t nLines = lines.GetCount(); | |
7972 | for ( size_t l = 0; l < nLines; l++ ) | |
7973 | { | |
7974 | const wxString& line = lines[l]; | |
7975 | ||
7976 | if ( line.empty() ) | |
7977 | { | |
7978 | *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight(); | |
7979 | continue; | |
7980 | } | |
7981 | ||
7982 | long lineWidth = 0, | |
7983 | lineHeight = 0; | |
7984 | dc.GetTextExtent(line, &lineWidth, &lineHeight); | |
7985 | ||
7986 | switch ( horizAlign ) | |
7987 | { | |
7988 | case wxALIGN_RIGHT: | |
7989 | if ( textOrientation == wxHORIZONTAL ) | |
7990 | x = rect.x + (rect.width - lineWidth - 1); | |
7991 | else | |
7992 | y = rect.y + lineWidth + 1; | |
7993 | break; | |
7994 | ||
7995 | case wxALIGN_CENTRE: | |
7996 | if ( textOrientation == wxHORIZONTAL ) | |
7997 | x = rect.x + ((rect.width - lineWidth) / 2); | |
7998 | else | |
7999 | y = rect.y + rect.height - ((rect.height - lineWidth) / 2); | |
8000 | break; | |
8001 | ||
8002 | case wxALIGN_LEFT: | |
8003 | default: | |
8004 | if ( textOrientation == wxHORIZONTAL ) | |
8005 | x = rect.x + 1; | |
8006 | else | |
8007 | y = rect.y + rect.height - 1; | |
8008 | break; | |
8009 | } | |
8010 | ||
8011 | if ( textOrientation == wxHORIZONTAL ) | |
8012 | { | |
8013 | dc.DrawText( line, x, y ); | |
8014 | y += lineHeight; | |
8015 | } | |
8016 | else | |
8017 | { | |
8018 | dc.DrawRotatedText( line, x, y, 90.0 ); | |
8019 | x += lineHeight; | |
8020 | } | |
8021 | } | |
8022 | } | |
8023 | ||
8024 | // Split multi-line text up into an array of strings. | |
8025 | // Any existing contents of the string array are preserved. | |
8026 | // | |
8027 | void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) | |
8028 | { | |
8029 | int startPos = 0; | |
8030 | int pos; | |
8031 | wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix ); | |
8032 | wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix ); | |
8033 | ||
8034 | while ( startPos < (int)tVal.length() ) | |
8035 | { | |
8036 | pos = tVal.Mid(startPos).Find( eol ); | |
8037 | if ( pos < 0 ) | |
8038 | { | |
8039 | break; | |
8040 | } | |
8041 | else if ( pos == 0 ) | |
8042 | { | |
8043 | lines.Add( wxEmptyString ); | |
8044 | } | |
8045 | else | |
8046 | { | |
8047 | lines.Add( value.Mid(startPos, pos) ); | |
8048 | } | |
8049 | ||
8050 | startPos += pos + 1; | |
8051 | } | |
8052 | ||
8053 | if ( startPos < (int)value.length() ) | |
8054 | { | |
8055 | lines.Add( value.Mid( startPos ) ); | |
8056 | } | |
8057 | } | |
8058 | ||
8059 | void wxGrid::GetTextBoxSize( const wxDC& dc, | |
8060 | const wxArrayString& lines, | |
8061 | long *width, long *height ) | |
8062 | { | |
8063 | long w = 0; | |
8064 | long h = 0; | |
8065 | long lineW = 0, lineH = 0; | |
8066 | ||
8067 | size_t i; | |
8068 | for ( i = 0; i < lines.GetCount(); i++ ) | |
8069 | { | |
8070 | dc.GetTextExtent( lines[i], &lineW, &lineH ); | |
8071 | w = wxMax( w, lineW ); | |
8072 | h += lineH; | |
8073 | } | |
8074 | ||
8075 | *width = w; | |
8076 | *height = h; | |
8077 | } | |
8078 | ||
8079 | // | |
8080 | // ------ Batch processing. | |
8081 | // | |
8082 | void wxGrid::EndBatch() | |
8083 | { | |
8084 | if ( m_batchCount > 0 ) | |
8085 | { | |
8086 | m_batchCount--; | |
8087 | if ( !m_batchCount ) | |
8088 | { | |
8089 | CalcDimensions(); | |
8090 | m_rowLabelWin->Refresh(); | |
8091 | m_colLabelWin->Refresh(); | |
8092 | m_cornerLabelWin->Refresh(); | |
8093 | m_gridWin->Refresh(); | |
8094 | } | |
8095 | } | |
8096 | } | |
8097 | ||
8098 | // Use this, rather than wxWindow::Refresh(), to force an immediate | |
8099 | // repainting of the grid. Has no effect if you are already inside a | |
8100 | // BeginBatch / EndBatch block. | |
8101 | // | |
8102 | void wxGrid::ForceRefresh() | |
8103 | { | |
8104 | BeginBatch(); | |
8105 | EndBatch(); | |
8106 | } | |
8107 | ||
8108 | bool wxGrid::Enable(bool enable) | |
8109 | { | |
8110 | if ( !wxScrolledWindow::Enable(enable) ) | |
8111 | return false; | |
8112 | ||
8113 | // redraw in the new state | |
8114 | m_gridWin->Refresh(); | |
8115 | ||
8116 | return true; | |
8117 | } | |
8118 | ||
8119 | // | |
8120 | // ------ Edit control functions | |
8121 | // | |
8122 | ||
8123 | void wxGrid::EnableEditing( bool edit ) | |
8124 | { | |
8125 | // TODO: improve this ? | |
8126 | // | |
8127 | if ( edit != m_editable ) | |
8128 | { | |
8129 | if (!edit) | |
8130 | EnableCellEditControl(edit); | |
8131 | m_editable = edit; | |
8132 | } | |
8133 | } | |
8134 | ||
8135 | void wxGrid::EnableCellEditControl( bool enable ) | |
8136 | { | |
8137 | if (! m_editable) | |
8138 | return; | |
8139 | ||
8140 | if ( enable != m_cellEditCtrlEnabled ) | |
8141 | { | |
8142 | if ( enable ) | |
8143 | { | |
8144 | if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0) | |
8145 | return; | |
8146 | ||
8147 | // this should be checked by the caller! | |
8148 | wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") ); | |
8149 | ||
8150 | // do it before ShowCellEditControl() | |
8151 | m_cellEditCtrlEnabled = enable; | |
8152 | ||
8153 | ShowCellEditControl(); | |
8154 | } | |
8155 | else | |
8156 | { | |
8157 | //FIXME:add veto support | |
8158 | SendEvent( wxEVT_GRID_EDITOR_HIDDEN ); | |
8159 | ||
8160 | HideCellEditControl(); | |
8161 | SaveEditControlValue(); | |
8162 | ||
8163 | // do it after HideCellEditControl() | |
8164 | m_cellEditCtrlEnabled = enable; | |
8165 | } | |
8166 | } | |
8167 | } | |
8168 | ||
8169 | bool wxGrid::IsCurrentCellReadOnly() const | |
8170 | { | |
8171 | // const_cast | |
8172 | wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords); | |
8173 | bool readonly = attr->IsReadOnly(); | |
8174 | attr->DecRef(); | |
8175 | ||
8176 | return readonly; | |
8177 | } | |
8178 | ||
8179 | bool wxGrid::CanEnableCellControl() const | |
8180 | { | |
8181 | return m_editable && (m_currentCellCoords != wxGridNoCellCoords) && | |
8182 | !IsCurrentCellReadOnly(); | |
8183 | } | |
8184 | ||
8185 | bool wxGrid::IsCellEditControlEnabled() const | |
8186 | { | |
8187 | // the cell edit control might be disable for all cells or just for the | |
8188 | // current one if it's read only | |
8189 | return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false; | |
8190 | } | |
8191 | ||
8192 | bool wxGrid::IsCellEditControlShown() const | |
8193 | { | |
8194 | bool isShown = false; | |
8195 | ||
8196 | if ( m_cellEditCtrlEnabled ) | |
8197 | { | |
8198 | int row = m_currentCellCoords.GetRow(); | |
8199 | int col = m_currentCellCoords.GetCol(); | |
8200 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
8201 | wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col); | |
8202 | attr->DecRef(); | |
8203 | ||
8204 | if ( editor ) | |
8205 | { | |
8206 | if ( editor->IsCreated() ) | |
8207 | { | |
8208 | isShown = editor->GetControl()->IsShown(); | |
8209 | } | |
8210 | ||
8211 | editor->DecRef(); | |
8212 | } | |
8213 | } | |
8214 | ||
8215 | return isShown; | |
8216 | } | |
8217 | ||
8218 | void wxGrid::ShowCellEditControl() | |
8219 | { | |
8220 | if ( IsCellEditControlEnabled() ) | |
8221 | { | |
8222 | if ( !IsVisible( m_currentCellCoords, false ) ) | |
8223 | { | |
8224 | m_cellEditCtrlEnabled = false; | |
8225 | return; | |
8226 | } | |
8227 | else | |
8228 | { | |
8229 | wxRect rect = CellToRect( m_currentCellCoords ); | |
8230 | int row = m_currentCellCoords.GetRow(); | |
8231 | int col = m_currentCellCoords.GetCol(); | |
8232 | ||
8233 | // if this is part of a multicell, find owner (topleft) | |
8234 | int cell_rows, cell_cols; | |
8235 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
8236 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
8237 | { | |
8238 | row += cell_rows; | |
8239 | col += cell_cols; | |
8240 | m_currentCellCoords.SetRow( row ); | |
8241 | m_currentCellCoords.SetCol( col ); | |
8242 | } | |
8243 | ||
8244 | // erase the highlight and the cell contents because the editor | |
8245 | // might not cover the entire cell | |
8246 | wxClientDC dc( m_gridWin ); | |
8247 | PrepareDC( dc ); | |
8248 | dc.SetBrush(wxBrush(GetCellAttr(row, col)->GetBackgroundColour(), wxSOLID)); | |
8249 | dc.SetPen(*wxTRANSPARENT_PEN); | |
8250 | dc.DrawRectangle(rect); | |
8251 | ||
8252 | // convert to scrolled coords | |
8253 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
8254 | ||
8255 | int nXMove = 0; | |
8256 | if (rect.x < 0) | |
8257 | nXMove = rect.x; | |
8258 | ||
8259 | // cell is shifted by one pixel | |
8260 | // However, don't allow x or y to become negative | |
8261 | // since the SetSize() method interprets that as | |
8262 | // "don't change." | |
8263 | if (rect.x > 0) | |
8264 | rect.x--; | |
8265 | if (rect.y > 0) | |
8266 | rect.y--; | |
8267 | ||
8268 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
8269 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); | |
8270 | if ( !editor->IsCreated() ) | |
8271 | { | |
8272 | editor->Create(m_gridWin, wxID_ANY, | |
8273 | new wxGridCellEditorEvtHandler(this, editor)); | |
8274 | ||
8275 | wxGridEditorCreatedEvent evt(GetId(), | |
8276 | wxEVT_GRID_EDITOR_CREATED, | |
8277 | this, | |
8278 | row, | |
8279 | col, | |
8280 | editor->GetControl()); | |
8281 | GetEventHandler()->ProcessEvent(evt); | |
8282 | } | |
8283 | ||
8284 | // resize editor to overflow into righthand cells if allowed | |
8285 | int maxWidth = rect.width; | |
8286 | wxString value = GetCellValue(row, col); | |
8287 | if ( (value != wxEmptyString) && (attr->GetOverflow()) ) | |
8288 | { | |
8289 | int y; | |
8290 | GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont()); | |
8291 | if (maxWidth < rect.width) | |
8292 | maxWidth = rect.width; | |
8293 | } | |
8294 | ||
8295 | int client_right = m_gridWin->GetClientSize().GetWidth(); | |
8296 | if (rect.x + maxWidth > client_right) | |
8297 | maxWidth = client_right - rect.x; | |
8298 | ||
8299 | if ((maxWidth > rect.width) && (col < m_numCols) && m_table) | |
8300 | { | |
8301 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
8302 | // may have changed earlier | |
8303 | for (int i = col + cell_cols; i < m_numCols; i++) | |
8304 | { | |
8305 | int c_rows, c_cols; | |
8306 | GetCellSize( row, i, &c_rows, &c_cols ); | |
8307 | ||
8308 | // looks weird going over a multicell | |
8309 | if (m_table->IsEmptyCell( row, i ) && | |
8310 | (rect.width < maxWidth) && (c_rows == 1)) | |
8311 | { | |
8312 | rect.width += GetColWidth( i ); | |
8313 | } | |
8314 | else | |
8315 | break; | |
8316 | } | |
8317 | ||
8318 | if (rect.GetRight() > client_right) | |
8319 | rect.SetRight( client_right - 1 ); | |
8320 | } | |
8321 | ||
8322 | editor->SetCellAttr( attr ); | |
8323 | editor->SetSize( rect ); | |
8324 | if (nXMove != 0) | |
8325 | editor->GetControl()->Move( | |
8326 | editor->GetControl()->GetPosition().x + nXMove, | |
8327 | editor->GetControl()->GetPosition().y ); | |
8328 | editor->Show( true, attr ); | |
8329 | ||
8330 | // recalc dimensions in case we need to | |
8331 | // expand the scrolled window to account for editor | |
8332 | CalcDimensions(); | |
8333 | ||
8334 | editor->BeginEdit(row, col, this); | |
8335 | editor->SetCellAttr(NULL); | |
8336 | ||
8337 | editor->DecRef(); | |
8338 | attr->DecRef(); | |
8339 | } | |
8340 | } | |
8341 | } | |
8342 | ||
8343 | void wxGrid::HideCellEditControl() | |
8344 | { | |
8345 | if ( IsCellEditControlEnabled() ) | |
8346 | { | |
8347 | int row = m_currentCellCoords.GetRow(); | |
8348 | int col = m_currentCellCoords.GetCol(); | |
8349 | ||
8350 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
8351 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); | |
8352 | editor->Show( false ); | |
8353 | editor->DecRef(); | |
8354 | attr->DecRef(); | |
8355 | ||
8356 | m_gridWin->SetFocus(); | |
8357 | ||
8358 | // refresh whole row to the right | |
8359 | wxRect rect( CellToRect(row, col) ); | |
8360 | CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y ); | |
8361 | rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x; | |
8362 | ||
8363 | #ifdef __WXMAC__ | |
8364 | // ensure that the pixels under the focus ring get refreshed as well | |
8365 | rect.Inflate(10, 10); | |
8366 | #endif | |
8367 | ||
8368 | m_gridWin->Refresh( false, &rect ); | |
8369 | } | |
8370 | } | |
8371 | ||
8372 | void wxGrid::SaveEditControlValue() | |
8373 | { | |
8374 | if ( IsCellEditControlEnabled() ) | |
8375 | { | |
8376 | int row = m_currentCellCoords.GetRow(); | |
8377 | int col = m_currentCellCoords.GetCol(); | |
8378 | ||
8379 | wxString oldval = GetCellValue(row, col); | |
8380 | ||
8381 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
8382 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); | |
8383 | bool changed = editor->EndEdit(row, col, this); | |
8384 | ||
8385 | editor->DecRef(); | |
8386 | attr->DecRef(); | |
8387 | ||
8388 | if (changed) | |
8389 | { | |
8390 | if ( SendEvent( wxEVT_GRID_CELL_CHANGE, | |
8391 | m_currentCellCoords.GetRow(), | |
8392 | m_currentCellCoords.GetCol() ) < 0 ) | |
8393 | { | |
8394 | // Event has been vetoed, set the data back. | |
8395 | SetCellValue(row, col, oldval); | |
8396 | } | |
8397 | } | |
8398 | } | |
8399 | } | |
8400 | ||
8401 | // | |
8402 | // ------ Grid location functions | |
8403 | // Note that all of these functions work with the logical coordinates of | |
8404 | // grid cells and labels so you will need to convert from device | |
8405 | // coordinates for mouse events etc. | |
8406 | // | |
8407 | ||
8408 | void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords ) | |
8409 | { | |
8410 | int row = YToRow(y); | |
8411 | int col = XToCol(x); | |
8412 | ||
8413 | if ( row == -1 || col == -1 ) | |
8414 | { | |
8415 | coords = wxGridNoCellCoords; | |
8416 | } | |
8417 | else | |
8418 | { | |
8419 | coords.Set( row, col ); | |
8420 | } | |
8421 | } | |
8422 | ||
8423 | // Internal Helper function for computing row or column from some | |
8424 | // (unscrolled) coordinate value, using either | |
8425 | // m_defaultRowHeight/m_defaultColWidth or binary search on array | |
8426 | // of m_rowBottoms/m_ColRights to speed up the search! | |
8427 | ||
8428 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, | |
8429 | const wxArrayInt& BorderArray, int nMax, | |
8430 | bool clipToMinMax) | |
8431 | { | |
8432 | if (coord < 0) | |
8433 | return clipToMinMax && (nMax > 0) ? 0 : -1; | |
8434 | ||
8435 | if (!defaultDist) | |
8436 | defaultDist = 1; | |
8437 | ||
8438 | size_t i_max = coord / defaultDist, | |
8439 | i_min = 0; | |
8440 | ||
8441 | if (BorderArray.IsEmpty()) | |
8442 | { | |
8443 | if ((int) i_max < nMax) | |
8444 | return i_max; | |
8445 | return clipToMinMax ? nMax - 1 : -1; | |
8446 | } | |
8447 | ||
8448 | if ( i_max >= BorderArray.GetCount()) | |
8449 | { | |
8450 | i_max = BorderArray.GetCount() - 1; | |
8451 | } | |
8452 | else | |
8453 | { | |
8454 | if ( coord >= BorderArray[i_max]) | |
8455 | { | |
8456 | i_min = i_max; | |
8457 | if (minDist) | |
8458 | i_max = coord / minDist; | |
8459 | else | |
8460 | i_max = BorderArray.GetCount() - 1; | |
8461 | } | |
8462 | ||
8463 | if ( i_max >= BorderArray.GetCount()) | |
8464 | i_max = BorderArray.GetCount() - 1; | |
8465 | } | |
8466 | ||
8467 | if ( coord >= BorderArray[i_max]) | |
8468 | return clipToMinMax ? (int)i_max : -1; | |
8469 | if ( coord < BorderArray[0] ) | |
8470 | return 0; | |
8471 | ||
8472 | while ( i_max - i_min > 0 ) | |
8473 | { | |
8474 | wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max], | |
8475 | 0, _T("wxGrid: internal error in CoordToRowOrCol")); | |
8476 | if (coord >= BorderArray[ i_max - 1]) | |
8477 | return i_max; | |
8478 | else | |
8479 | i_max--; | |
8480 | int median = i_min + (i_max - i_min + 1) / 2; | |
8481 | if (coord < BorderArray[median]) | |
8482 | i_max = median; | |
8483 | else | |
8484 | i_min = median; | |
8485 | } | |
8486 | ||
8487 | return i_max; | |
8488 | } | |
8489 | ||
8490 | int wxGrid::YToRow( int y ) | |
8491 | { | |
8492 | return CoordToRowOrCol(y, m_defaultRowHeight, | |
8493 | m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false); | |
8494 | } | |
8495 | ||
8496 | int wxGrid::XToCol( int x, bool clipToMinMax ) | |
8497 | { | |
8498 | if (x < 0) | |
8499 | return clipToMinMax && (m_numCols > 0) ? GetColAt( 0 ) : -1; | |
8500 | ||
8501 | if (!m_defaultColWidth) | |
8502 | m_defaultColWidth = 1; | |
8503 | ||
8504 | int maxPos = x / m_defaultColWidth; | |
8505 | int minPos = 0; | |
8506 | ||
8507 | if (m_colRights.IsEmpty()) | |
8508 | { | |
8509 | if(maxPos < m_numCols) | |
8510 | return GetColAt( maxPos ); | |
8511 | return clipToMinMax ? GetColAt( m_numCols - 1 ) : -1; | |
8512 | } | |
8513 | ||
8514 | if ( maxPos >= m_numCols) | |
8515 | maxPos = m_numCols - 1; | |
8516 | else | |
8517 | { | |
8518 | if ( x >= m_colRights[GetColAt( maxPos )]) | |
8519 | { | |
8520 | minPos = maxPos; | |
8521 | if (m_minAcceptableColWidth) | |
8522 | maxPos = x / m_minAcceptableColWidth; | |
8523 | else | |
8524 | maxPos = m_numCols - 1; | |
8525 | } | |
8526 | if ( maxPos >= m_numCols) | |
8527 | maxPos = m_numCols - 1; | |
8528 | } | |
8529 | ||
8530 | //X is beyond the last column | |
8531 | if ( x >= m_colRights[GetColAt( maxPos )]) | |
8532 | return clipToMinMax ? GetColAt( maxPos ) : -1; | |
8533 | ||
8534 | //X is before the first column | |
8535 | if ( x < m_colRights[GetColAt( 0 )] ) | |
8536 | return GetColAt( 0 ); | |
8537 | ||
8538 | //Perform a binary search | |
8539 | while ( maxPos - minPos > 0 ) | |
8540 | { | |
8541 | wxCHECK_MSG(m_colRights[GetColAt( minPos )] <= x && x < m_colRights[GetColAt( maxPos )], | |
8542 | 0, _T("wxGrid: internal error in XToCol")); | |
8543 | ||
8544 | if (x >= m_colRights[GetColAt( maxPos - 1 )]) | |
8545 | return GetColAt( maxPos ); | |
8546 | else | |
8547 | maxPos--; | |
8548 | int median = minPos + (maxPos - minPos + 1) / 2; | |
8549 | if (x < m_colRights[GetColAt( median )]) | |
8550 | maxPos = median; | |
8551 | else | |
8552 | minPos = median; | |
8553 | } | |
8554 | return GetColAt( maxPos ); | |
8555 | } | |
8556 | ||
8557 | // return the row number that that the y coord is near | |
8558 | // the edge of, or -1 if not near an edge. | |
8559 | // coords can only possibly be near an edge if | |
8560 | // (a) the row/column is large enough to still allow for an "inner" area | |
8561 | // that is _not_ nead the edge (i.e., if the height/width is smaller | |
8562 | // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be | |
8563 | // near the edge). | |
8564 | // and | |
8565 | // (b) resizing rows/columns (the thing for which edge detection is | |
8566 | // relevant at all) is enabled. | |
8567 | // | |
8568 | int wxGrid::YToEdgeOfRow( int y ) | |
8569 | { | |
8570 | int i; | |
8571 | i = internalYToRow(y); | |
8572 | ||
8573 | if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE && CanDragRowSize() ) | |
8574 | { | |
8575 | // We know that we are in row i, test whether we are | |
8576 | // close enough to lower or upper border, respectively. | |
8577 | if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE ) | |
8578 | return i; | |
8579 | else if ( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE ) | |
8580 | return i - 1; | |
8581 | } | |
8582 | ||
8583 | return -1; | |
8584 | } | |
8585 | ||
8586 | // return the col number that that the x coord is near the edge of, or | |
8587 | // -1 if not near an edge | |
8588 | // See comment at YToEdgeOfRow for conditions on edge detection. | |
8589 | // | |
8590 | int wxGrid::XToEdgeOfCol( int x ) | |
8591 | { | |
8592 | int i; | |
8593 | i = internalXToCol(x); | |
8594 | ||
8595 | if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE && CanDragColSize() ) | |
8596 | { | |
8597 | // We know that we are in column i; test whether we are | |
8598 | // close enough to right or left border, respectively. | |
8599 | if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE ) | |
8600 | return i; | |
8601 | else if ( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE ) | |
8602 | return i - 1; | |
8603 | } | |
8604 | ||
8605 | return -1; | |
8606 | } | |
8607 | ||
8608 | wxRect wxGrid::CellToRect( int row, int col ) | |
8609 | { | |
8610 | wxRect rect( -1, -1, -1, -1 ); | |
8611 | ||
8612 | if ( row >= 0 && row < m_numRows && | |
8613 | col >= 0 && col < m_numCols ) | |
8614 | { | |
8615 | int i, cell_rows, cell_cols; | |
8616 | rect.width = rect.height = 0; | |
8617 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
8618 | // if negative then find multicell owner | |
8619 | if (cell_rows < 0) | |
8620 | row += cell_rows; | |
8621 | if (cell_cols < 0) | |
8622 | col += cell_cols; | |
8623 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
8624 | ||
8625 | rect.x = GetColLeft(col); | |
8626 | rect.y = GetRowTop(row); | |
8627 | for (i=col; i < col + cell_cols; i++) | |
8628 | rect.width += GetColWidth(i); | |
8629 | for (i=row; i < row + cell_rows; i++) | |
8630 | rect.height += GetRowHeight(i); | |
8631 | } | |
8632 | ||
8633 | // if grid lines are enabled, then the area of the cell is a bit smaller | |
8634 | if (m_gridLinesEnabled) | |
8635 | { | |
8636 | rect.width -= 1; | |
8637 | rect.height -= 1; | |
8638 | } | |
8639 | ||
8640 | return rect; | |
8641 | } | |
8642 | ||
8643 | bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) | |
8644 | { | |
8645 | // get the cell rectangle in logical coords | |
8646 | // | |
8647 | wxRect r( CellToRect( row, col ) ); | |
8648 | ||
8649 | // convert to device coords | |
8650 | // | |
8651 | int left, top, right, bottom; | |
8652 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
8653 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
8654 | ||
8655 | // check against the client area of the grid window | |
8656 | int cw, ch; | |
8657 | m_gridWin->GetClientSize( &cw, &ch ); | |
8658 | ||
8659 | if ( wholeCellVisible ) | |
8660 | { | |
8661 | // is the cell wholly visible ? | |
8662 | return ( left >= 0 && right <= cw && | |
8663 | top >= 0 && bottom <= ch ); | |
8664 | } | |
8665 | else | |
8666 | { | |
8667 | // is the cell partly visible ? | |
8668 | // | |
8669 | return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) && | |
8670 | ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) ); | |
8671 | } | |
8672 | } | |
8673 | ||
8674 | // make the specified cell location visible by doing a minimal amount | |
8675 | // of scrolling | |
8676 | // | |
8677 | void wxGrid::MakeCellVisible( int row, int col ) | |
8678 | { | |
8679 | int i; | |
8680 | int xpos = -1, ypos = -1; | |
8681 | ||
8682 | if ( row >= 0 && row < m_numRows && | |
8683 | col >= 0 && col < m_numCols ) | |
8684 | { | |
8685 | // get the cell rectangle in logical coords | |
8686 | wxRect r( CellToRect( row, col ) ); | |
8687 | ||
8688 | // convert to device coords | |
8689 | int left, top, right, bottom; | |
8690 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
8691 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
8692 | ||
8693 | int cw, ch; | |
8694 | m_gridWin->GetClientSize( &cw, &ch ); | |
8695 | ||
8696 | if ( top < 0 ) | |
8697 | { | |
8698 | ypos = r.GetTop(); | |
8699 | } | |
8700 | else if ( bottom > ch ) | |
8701 | { | |
8702 | int h = r.GetHeight(); | |
8703 | ypos = r.GetTop(); | |
8704 | for ( i = row - 1; i >= 0; i-- ) | |
8705 | { | |
8706 | int rowHeight = GetRowHeight(i); | |
8707 | if ( h + rowHeight > ch ) | |
8708 | break; | |
8709 | ||
8710 | h += rowHeight; | |
8711 | ypos -= rowHeight; | |
8712 | } | |
8713 | ||
8714 | // we divide it later by GRID_SCROLL_LINE, make sure that we don't | |
8715 | // have rounding errors (this is important, because if we do, | |
8716 | // we might not scroll at all and some cells won't be redrawn) | |
8717 | // | |
8718 | // Sometimes GRID_SCROLL_LINE / 2 is not enough, | |
8719 | // so just add a full scroll unit... | |
8720 | ypos += m_scrollLineY; | |
8721 | } | |
8722 | ||
8723 | // special handling for wide cells - show always left part of the cell! | |
8724 | // Otherwise, e.g. when stepping from row to row, it would jump between | |
8725 | // left and right part of the cell on every step! | |
8726 | // if ( left < 0 ) | |
8727 | if ( left < 0 || (right - left) >= cw ) | |
8728 | { | |
8729 | xpos = r.GetLeft(); | |
8730 | } | |
8731 | else if ( right > cw ) | |
8732 | { | |
8733 | // position the view so that the cell is on the right | |
8734 | int x0, y0; | |
8735 | CalcUnscrolledPosition(0, 0, &x0, &y0); | |
8736 | xpos = x0 + (right - cw); | |
8737 | ||
8738 | // see comment for ypos above | |
8739 | xpos += m_scrollLineX; | |
8740 | } | |
8741 | ||
8742 | if ( xpos != -1 || ypos != -1 ) | |
8743 | { | |
8744 | if ( xpos != -1 ) | |
8745 | xpos /= m_scrollLineX; | |
8746 | if ( ypos != -1 ) | |
8747 | ypos /= m_scrollLineY; | |
8748 | Scroll( xpos, ypos ); | |
8749 | AdjustScrollbars(); | |
8750 | } | |
8751 | } | |
8752 | } | |
8753 | ||
8754 | // | |
8755 | // ------ Grid cursor movement functions | |
8756 | // | |
8757 | ||
8758 | bool wxGrid::MoveCursorUp( bool expandSelection ) | |
8759 | { | |
8760 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
8761 | m_currentCellCoords.GetRow() >= 0 ) | |
8762 | { | |
8763 | if ( expandSelection ) | |
8764 | { | |
8765 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
8766 | m_selectingKeyboard = m_currentCellCoords; | |
8767 | if ( m_selectingKeyboard.GetRow() > 0 ) | |
8768 | { | |
8769 | m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 ); | |
8770 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
8771 | m_selectingKeyboard.GetCol() ); | |
8772 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
8773 | } | |
8774 | } | |
8775 | else if ( m_currentCellCoords.GetRow() > 0 ) | |
8776 | { | |
8777 | int row = m_currentCellCoords.GetRow() - 1; | |
8778 | int col = m_currentCellCoords.GetCol(); | |
8779 | ClearSelection(); | |
8780 | MakeCellVisible( row, col ); | |
8781 | SetCurrentCell( row, col ); | |
8782 | } | |
8783 | else | |
8784 | return false; | |
8785 | ||
8786 | return true; | |
8787 | } | |
8788 | ||
8789 | return false; | |
8790 | } | |
8791 | ||
8792 | bool wxGrid::MoveCursorDown( bool expandSelection ) | |
8793 | { | |
8794 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
8795 | m_currentCellCoords.GetRow() < m_numRows ) | |
8796 | { | |
8797 | if ( expandSelection ) | |
8798 | { | |
8799 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
8800 | m_selectingKeyboard = m_currentCellCoords; | |
8801 | if ( m_selectingKeyboard.GetRow() < m_numRows - 1 ) | |
8802 | { | |
8803 | m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 ); | |
8804 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
8805 | m_selectingKeyboard.GetCol() ); | |
8806 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
8807 | } | |
8808 | } | |
8809 | else if ( m_currentCellCoords.GetRow() < m_numRows - 1 ) | |
8810 | { | |
8811 | int row = m_currentCellCoords.GetRow() + 1; | |
8812 | int col = m_currentCellCoords.GetCol(); | |
8813 | ClearSelection(); | |
8814 | MakeCellVisible( row, col ); | |
8815 | SetCurrentCell( row, col ); | |
8816 | } | |
8817 | else | |
8818 | return false; | |
8819 | ||
8820 | return true; | |
8821 | } | |
8822 | ||
8823 | return false; | |
8824 | } | |
8825 | ||
8826 | bool wxGrid::MoveCursorLeft( bool expandSelection ) | |
8827 | { | |
8828 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
8829 | m_currentCellCoords.GetCol() >= 0 ) | |
8830 | { | |
8831 | if ( expandSelection ) | |
8832 | { | |
8833 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
8834 | m_selectingKeyboard = m_currentCellCoords; | |
8835 | if ( m_selectingKeyboard.GetCol() > 0 ) | |
8836 | { | |
8837 | m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 ); | |
8838 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
8839 | m_selectingKeyboard.GetCol() ); | |
8840 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
8841 | } | |
8842 | } | |
8843 | else if ( GetColPos( m_currentCellCoords.GetCol() ) > 0 ) | |
8844 | { | |
8845 | int row = m_currentCellCoords.GetRow(); | |
8846 | int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) - 1 ); | |
8847 | ClearSelection(); | |
8848 | ||
8849 | MakeCellVisible( row, col ); | |
8850 | SetCurrentCell( row, col ); | |
8851 | } | |
8852 | else | |
8853 | return false; | |
8854 | ||
8855 | return true; | |
8856 | } | |
8857 | ||
8858 | return false; | |
8859 | } | |
8860 | ||
8861 | bool wxGrid::MoveCursorRight( bool expandSelection ) | |
8862 | { | |
8863 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
8864 | m_currentCellCoords.GetCol() < m_numCols ) | |
8865 | { | |
8866 | if ( expandSelection ) | |
8867 | { | |
8868 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
8869 | m_selectingKeyboard = m_currentCellCoords; | |
8870 | if ( m_selectingKeyboard.GetCol() < m_numCols - 1 ) | |
8871 | { | |
8872 | m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 ); | |
8873 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
8874 | m_selectingKeyboard.GetCol() ); | |
8875 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
8876 | } | |
8877 | } | |
8878 | else if ( GetColPos( m_currentCellCoords.GetCol() ) < m_numCols - 1 ) | |
8879 | { | |
8880 | int row = m_currentCellCoords.GetRow(); | |
8881 | int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) + 1 ); | |
8882 | ClearSelection(); | |
8883 | ||
8884 | MakeCellVisible( row, col ); | |
8885 | SetCurrentCell( row, col ); | |
8886 | } | |
8887 | else | |
8888 | return false; | |
8889 | ||
8890 | return true; | |
8891 | } | |
8892 | ||
8893 | return false; | |
8894 | } | |
8895 | ||
8896 | bool wxGrid::MovePageUp() | |
8897 | { | |
8898 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
8899 | return false; | |
8900 | ||
8901 | int row = m_currentCellCoords.GetRow(); | |
8902 | if ( row > 0 ) | |
8903 | { | |
8904 | int cw, ch; | |
8905 | m_gridWin->GetClientSize( &cw, &ch ); | |
8906 | ||
8907 | int y = GetRowTop(row); | |
8908 | int newRow = internalYToRow( y - ch + 1 ); | |
8909 | ||
8910 | if ( newRow == row ) | |
8911 | { | |
8912 | // row > 0, so newRow can never be less than 0 here. | |
8913 | newRow = row - 1; | |
8914 | } | |
8915 | ||
8916 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); | |
8917 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
8918 | ||
8919 | return true; | |
8920 | } | |
8921 | ||
8922 | return false; | |
8923 | } | |
8924 | ||
8925 | bool wxGrid::MovePageDown() | |
8926 | { | |
8927 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
8928 | return false; | |
8929 | ||
8930 | int row = m_currentCellCoords.GetRow(); | |
8931 | if ( (row + 1) < m_numRows ) | |
8932 | { | |
8933 | int cw, ch; | |
8934 | m_gridWin->GetClientSize( &cw, &ch ); | |
8935 | ||
8936 | int y = GetRowTop(row); | |
8937 | int newRow = internalYToRow( y + ch ); | |
8938 | if ( newRow == row ) | |
8939 | { | |
8940 | // row < m_numRows, so newRow can't overflow here. | |
8941 | newRow = row + 1; | |
8942 | } | |
8943 | ||
8944 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); | |
8945 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
8946 | ||
8947 | return true; | |
8948 | } | |
8949 | ||
8950 | return false; | |
8951 | } | |
8952 | ||
8953 | bool wxGrid::MoveCursorUpBlock( bool expandSelection ) | |
8954 | { | |
8955 | if ( m_table && | |
8956 | m_currentCellCoords != wxGridNoCellCoords && | |
8957 | m_currentCellCoords.GetRow() > 0 ) | |
8958 | { | |
8959 | int row = m_currentCellCoords.GetRow(); | |
8960 | int col = m_currentCellCoords.GetCol(); | |
8961 | ||
8962 | if ( m_table->IsEmptyCell(row, col) ) | |
8963 | { | |
8964 | // starting in an empty cell: find the next block of | |
8965 | // non-empty cells | |
8966 | // | |
8967 | while ( row > 0 ) | |
8968 | { | |
8969 | row--; | |
8970 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
8971 | break; | |
8972 | } | |
8973 | } | |
8974 | else if ( m_table->IsEmptyCell(row - 1, col) ) | |
8975 | { | |
8976 | // starting at the top of a block: find the next block | |
8977 | // | |
8978 | row--; | |
8979 | while ( row > 0 ) | |
8980 | { | |
8981 | row--; | |
8982 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
8983 | break; | |
8984 | } | |
8985 | } | |
8986 | else | |
8987 | { | |
8988 | // starting within a block: find the top of the block | |
8989 | // | |
8990 | while ( row > 0 ) | |
8991 | { | |
8992 | row--; | |
8993 | if ( m_table->IsEmptyCell(row, col) ) | |
8994 | { | |
8995 | row++; | |
8996 | break; | |
8997 | } | |
8998 | } | |
8999 | } | |
9000 | ||
9001 | MakeCellVisible( row, col ); | |
9002 | if ( expandSelection ) | |
9003 | { | |
9004 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
9005 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
9006 | } | |
9007 | else | |
9008 | { | |
9009 | ClearSelection(); | |
9010 | SetCurrentCell( row, col ); | |
9011 | } | |
9012 | ||
9013 | return true; | |
9014 | } | |
9015 | ||
9016 | return false; | |
9017 | } | |
9018 | ||
9019 | bool wxGrid::MoveCursorDownBlock( bool expandSelection ) | |
9020 | { | |
9021 | if ( m_table && | |
9022 | m_currentCellCoords != wxGridNoCellCoords && | |
9023 | m_currentCellCoords.GetRow() < m_numRows - 1 ) | |
9024 | { | |
9025 | int row = m_currentCellCoords.GetRow(); | |
9026 | int col = m_currentCellCoords.GetCol(); | |
9027 | ||
9028 | if ( m_table->IsEmptyCell(row, col) ) | |
9029 | { | |
9030 | // starting in an empty cell: find the next block of | |
9031 | // non-empty cells | |
9032 | // | |
9033 | while ( row < m_numRows - 1 ) | |
9034 | { | |
9035 | row++; | |
9036 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9037 | break; | |
9038 | } | |
9039 | } | |
9040 | else if ( m_table->IsEmptyCell(row + 1, col) ) | |
9041 | { | |
9042 | // starting at the bottom of a block: find the next block | |
9043 | // | |
9044 | row++; | |
9045 | while ( row < m_numRows - 1 ) | |
9046 | { | |
9047 | row++; | |
9048 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9049 | break; | |
9050 | } | |
9051 | } | |
9052 | else | |
9053 | { | |
9054 | // starting within a block: find the bottom of the block | |
9055 | // | |
9056 | while ( row < m_numRows - 1 ) | |
9057 | { | |
9058 | row++; | |
9059 | if ( m_table->IsEmptyCell(row, col) ) | |
9060 | { | |
9061 | row--; | |
9062 | break; | |
9063 | } | |
9064 | } | |
9065 | } | |
9066 | ||
9067 | MakeCellVisible( row, col ); | |
9068 | if ( expandSelection ) | |
9069 | { | |
9070 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
9071 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
9072 | } | |
9073 | else | |
9074 | { | |
9075 | ClearSelection(); | |
9076 | SetCurrentCell( row, col ); | |
9077 | } | |
9078 | ||
9079 | return true; | |
9080 | } | |
9081 | ||
9082 | return false; | |
9083 | } | |
9084 | ||
9085 | bool wxGrid::MoveCursorLeftBlock( bool expandSelection ) | |
9086 | { | |
9087 | if ( m_table && | |
9088 | m_currentCellCoords != wxGridNoCellCoords && | |
9089 | m_currentCellCoords.GetCol() > 0 ) | |
9090 | { | |
9091 | int row = m_currentCellCoords.GetRow(); | |
9092 | int col = m_currentCellCoords.GetCol(); | |
9093 | ||
9094 | if ( m_table->IsEmptyCell(row, col) ) | |
9095 | { | |
9096 | // starting in an empty cell: find the next block of | |
9097 | // non-empty cells | |
9098 | // | |
9099 | while ( col > 0 ) | |
9100 | { | |
9101 | col--; | |
9102 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9103 | break; | |
9104 | } | |
9105 | } | |
9106 | else if ( m_table->IsEmptyCell(row, col - 1) ) | |
9107 | { | |
9108 | // starting at the left of a block: find the next block | |
9109 | // | |
9110 | col--; | |
9111 | while ( col > 0 ) | |
9112 | { | |
9113 | col--; | |
9114 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9115 | break; | |
9116 | } | |
9117 | } | |
9118 | else | |
9119 | { | |
9120 | // starting within a block: find the left of the block | |
9121 | // | |
9122 | while ( col > 0 ) | |
9123 | { | |
9124 | col--; | |
9125 | if ( m_table->IsEmptyCell(row, col) ) | |
9126 | { | |
9127 | col++; | |
9128 | break; | |
9129 | } | |
9130 | } | |
9131 | } | |
9132 | ||
9133 | MakeCellVisible( row, col ); | |
9134 | if ( expandSelection ) | |
9135 | { | |
9136 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
9137 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
9138 | } | |
9139 | else | |
9140 | { | |
9141 | ClearSelection(); | |
9142 | SetCurrentCell( row, col ); | |
9143 | } | |
9144 | ||
9145 | return true; | |
9146 | } | |
9147 | ||
9148 | return false; | |
9149 | } | |
9150 | ||
9151 | bool wxGrid::MoveCursorRightBlock( bool expandSelection ) | |
9152 | { | |
9153 | if ( m_table && | |
9154 | m_currentCellCoords != wxGridNoCellCoords && | |
9155 | m_currentCellCoords.GetCol() < m_numCols - 1 ) | |
9156 | { | |
9157 | int row = m_currentCellCoords.GetRow(); | |
9158 | int col = m_currentCellCoords.GetCol(); | |
9159 | ||
9160 | if ( m_table->IsEmptyCell(row, col) ) | |
9161 | { | |
9162 | // starting in an empty cell: find the next block of | |
9163 | // non-empty cells | |
9164 | // | |
9165 | while ( col < m_numCols - 1 ) | |
9166 | { | |
9167 | col++; | |
9168 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9169 | break; | |
9170 | } | |
9171 | } | |
9172 | else if ( m_table->IsEmptyCell(row, col + 1) ) | |
9173 | { | |
9174 | // starting at the right of a block: find the next block | |
9175 | // | |
9176 | col++; | |
9177 | while ( col < m_numCols - 1 ) | |
9178 | { | |
9179 | col++; | |
9180 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9181 | break; | |
9182 | } | |
9183 | } | |
9184 | else | |
9185 | { | |
9186 | // starting within a block: find the right of the block | |
9187 | // | |
9188 | while ( col < m_numCols - 1 ) | |
9189 | { | |
9190 | col++; | |
9191 | if ( m_table->IsEmptyCell(row, col) ) | |
9192 | { | |
9193 | col--; | |
9194 | break; | |
9195 | } | |
9196 | } | |
9197 | } | |
9198 | ||
9199 | MakeCellVisible( row, col ); | |
9200 | if ( expandSelection ) | |
9201 | { | |
9202 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
9203 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
9204 | } | |
9205 | else | |
9206 | { | |
9207 | ClearSelection(); | |
9208 | SetCurrentCell( row, col ); | |
9209 | } | |
9210 | ||
9211 | return true; | |
9212 | } | |
9213 | ||
9214 | return false; | |
9215 | } | |
9216 | ||
9217 | // | |
9218 | // ------ Label values and formatting | |
9219 | // | |
9220 | ||
9221 | void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) | |
9222 | { | |
9223 | if ( horiz ) | |
9224 | *horiz = m_rowLabelHorizAlign; | |
9225 | if ( vert ) | |
9226 | *vert = m_rowLabelVertAlign; | |
9227 | } | |
9228 | ||
9229 | void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) | |
9230 | { | |
9231 | if ( horiz ) | |
9232 | *horiz = m_colLabelHorizAlign; | |
9233 | if ( vert ) | |
9234 | *vert = m_colLabelVertAlign; | |
9235 | } | |
9236 | ||
9237 | int wxGrid::GetColLabelTextOrientation() | |
9238 | { | |
9239 | return m_colLabelTextOrientation; | |
9240 | } | |
9241 | ||
9242 | wxString wxGrid::GetRowLabelValue( int row ) | |
9243 | { | |
9244 | if ( m_table ) | |
9245 | { | |
9246 | return m_table->GetRowLabelValue( row ); | |
9247 | } | |
9248 | else | |
9249 | { | |
9250 | wxString s; | |
9251 | s << row; | |
9252 | return s; | |
9253 | } | |
9254 | } | |
9255 | ||
9256 | wxString wxGrid::GetColLabelValue( int col ) | |
9257 | { | |
9258 | if ( m_table ) | |
9259 | { | |
9260 | return m_table->GetColLabelValue( col ); | |
9261 | } | |
9262 | else | |
9263 | { | |
9264 | wxString s; | |
9265 | s << col; | |
9266 | return s; | |
9267 | } | |
9268 | } | |
9269 | ||
9270 | void wxGrid::SetRowLabelSize( int width ) | |
9271 | { | |
9272 | width = wxMax( width, 0 ); | |
9273 | if ( width != m_rowLabelWidth ) | |
9274 | { | |
9275 | if ( width == 0 ) | |
9276 | { | |
9277 | m_rowLabelWin->Show( false ); | |
9278 | m_cornerLabelWin->Show( false ); | |
9279 | } | |
9280 | else if ( m_rowLabelWidth == 0 ) | |
9281 | { | |
9282 | m_rowLabelWin->Show( true ); | |
9283 | if ( m_colLabelHeight > 0 ) | |
9284 | m_cornerLabelWin->Show( true ); | |
9285 | } | |
9286 | ||
9287 | m_rowLabelWidth = width; | |
9288 | CalcWindowSizes(); | |
9289 | wxScrolledWindow::Refresh( true ); | |
9290 | } | |
9291 | } | |
9292 | ||
9293 | void wxGrid::SetColLabelSize( int height ) | |
9294 | { | |
9295 | height = wxMax( height, 0 ); | |
9296 | if ( height != m_colLabelHeight ) | |
9297 | { | |
9298 | if ( height == 0 ) | |
9299 | { | |
9300 | m_colLabelWin->Show( false ); | |
9301 | m_cornerLabelWin->Show( false ); | |
9302 | } | |
9303 | else if ( m_colLabelHeight == 0 ) | |
9304 | { | |
9305 | m_colLabelWin->Show( true ); | |
9306 | if ( m_rowLabelWidth > 0 ) | |
9307 | m_cornerLabelWin->Show( true ); | |
9308 | } | |
9309 | ||
9310 | m_colLabelHeight = height; | |
9311 | CalcWindowSizes(); | |
9312 | wxScrolledWindow::Refresh( true ); | |
9313 | } | |
9314 | } | |
9315 | ||
9316 | void wxGrid::SetLabelBackgroundColour( const wxColour& colour ) | |
9317 | { | |
9318 | if ( m_labelBackgroundColour != colour ) | |
9319 | { | |
9320 | m_labelBackgroundColour = colour; | |
9321 | m_rowLabelWin->SetBackgroundColour( colour ); | |
9322 | m_colLabelWin->SetBackgroundColour( colour ); | |
9323 | m_cornerLabelWin->SetBackgroundColour( colour ); | |
9324 | ||
9325 | if ( !GetBatchCount() ) | |
9326 | { | |
9327 | m_rowLabelWin->Refresh(); | |
9328 | m_colLabelWin->Refresh(); | |
9329 | m_cornerLabelWin->Refresh(); | |
9330 | } | |
9331 | } | |
9332 | } | |
9333 | ||
9334 | void wxGrid::SetLabelTextColour( const wxColour& colour ) | |
9335 | { | |
9336 | if ( m_labelTextColour != colour ) | |
9337 | { | |
9338 | m_labelTextColour = colour; | |
9339 | if ( !GetBatchCount() ) | |
9340 | { | |
9341 | m_rowLabelWin->Refresh(); | |
9342 | m_colLabelWin->Refresh(); | |
9343 | } | |
9344 | } | |
9345 | } | |
9346 | ||
9347 | void wxGrid::SetLabelFont( const wxFont& font ) | |
9348 | { | |
9349 | m_labelFont = font; | |
9350 | if ( !GetBatchCount() ) | |
9351 | { | |
9352 | m_rowLabelWin->Refresh(); | |
9353 | m_colLabelWin->Refresh(); | |
9354 | } | |
9355 | } | |
9356 | ||
9357 | void wxGrid::SetRowLabelAlignment( int horiz, int vert ) | |
9358 | { | |
9359 | // allow old (incorrect) defs to be used | |
9360 | switch ( horiz ) | |
9361 | { | |
9362 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
9363 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
9364 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
9365 | } | |
9366 | ||
9367 | switch ( vert ) | |
9368 | { | |
9369 | case wxTOP: vert = wxALIGN_TOP; break; | |
9370 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
9371 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
9372 | } | |
9373 | ||
9374 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) | |
9375 | { | |
9376 | m_rowLabelHorizAlign = horiz; | |
9377 | } | |
9378 | ||
9379 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) | |
9380 | { | |
9381 | m_rowLabelVertAlign = vert; | |
9382 | } | |
9383 | ||
9384 | if ( !GetBatchCount() ) | |
9385 | { | |
9386 | m_rowLabelWin->Refresh(); | |
9387 | } | |
9388 | } | |
9389 | ||
9390 | void wxGrid::SetColLabelAlignment( int horiz, int vert ) | |
9391 | { | |
9392 | // allow old (incorrect) defs to be used | |
9393 | switch ( horiz ) | |
9394 | { | |
9395 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
9396 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
9397 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
9398 | } | |
9399 | ||
9400 | switch ( vert ) | |
9401 | { | |
9402 | case wxTOP: vert = wxALIGN_TOP; break; | |
9403 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
9404 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
9405 | } | |
9406 | ||
9407 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) | |
9408 | { | |
9409 | m_colLabelHorizAlign = horiz; | |
9410 | } | |
9411 | ||
9412 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) | |
9413 | { | |
9414 | m_colLabelVertAlign = vert; | |
9415 | } | |
9416 | ||
9417 | if ( !GetBatchCount() ) | |
9418 | { | |
9419 | m_colLabelWin->Refresh(); | |
9420 | } | |
9421 | } | |
9422 | ||
9423 | // Note: under MSW, the default column label font must be changed because it | |
9424 | // does not support vertical printing | |
9425 | // | |
9426 | // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD); | |
9427 | // pGrid->SetLabelFont(font); | |
9428 | // pGrid->SetColLabelTextOrientation(wxVERTICAL); | |
9429 | // | |
9430 | void wxGrid::SetColLabelTextOrientation( int textOrientation ) | |
9431 | { | |
9432 | if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL ) | |
9433 | m_colLabelTextOrientation = textOrientation; | |
9434 | ||
9435 | if ( !GetBatchCount() ) | |
9436 | m_colLabelWin->Refresh(); | |
9437 | } | |
9438 | ||
9439 | void wxGrid::SetRowLabelValue( int row, const wxString& s ) | |
9440 | { | |
9441 | if ( m_table ) | |
9442 | { | |
9443 | m_table->SetRowLabelValue( row, s ); | |
9444 | if ( !GetBatchCount() ) | |
9445 | { | |
9446 | wxRect rect = CellToRect( row, 0 ); | |
9447 | if ( rect.height > 0 ) | |
9448 | { | |
9449 | CalcScrolledPosition(0, rect.y, &rect.x, &rect.y); | |
9450 | rect.x = 0; | |
9451 | rect.width = m_rowLabelWidth; | |
9452 | m_rowLabelWin->Refresh( true, &rect ); | |
9453 | } | |
9454 | } | |
9455 | } | |
9456 | } | |
9457 | ||
9458 | void wxGrid::SetColLabelValue( int col, const wxString& s ) | |
9459 | { | |
9460 | if ( m_table ) | |
9461 | { | |
9462 | m_table->SetColLabelValue( col, s ); | |
9463 | if ( !GetBatchCount() ) | |
9464 | { | |
9465 | wxRect rect = CellToRect( 0, col ); | |
9466 | if ( rect.width > 0 ) | |
9467 | { | |
9468 | CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y); | |
9469 | rect.y = 0; | |
9470 | rect.height = m_colLabelHeight; | |
9471 | m_colLabelWin->Refresh( true, &rect ); | |
9472 | } | |
9473 | } | |
9474 | } | |
9475 | } | |
9476 | ||
9477 | void wxGrid::SetGridLineColour( const wxColour& colour ) | |
9478 | { | |
9479 | if ( m_gridLineColour != colour ) | |
9480 | { | |
9481 | m_gridLineColour = colour; | |
9482 | ||
9483 | wxClientDC dc( m_gridWin ); | |
9484 | PrepareDC( dc ); | |
9485 | DrawAllGridLines( dc, wxRegion() ); | |
9486 | } | |
9487 | } | |
9488 | ||
9489 | void wxGrid::SetCellHighlightColour( const wxColour& colour ) | |
9490 | { | |
9491 | if ( m_cellHighlightColour != colour ) | |
9492 | { | |
9493 | m_cellHighlightColour = colour; | |
9494 | ||
9495 | wxClientDC dc( m_gridWin ); | |
9496 | PrepareDC( dc ); | |
9497 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
9498 | DrawCellHighlight(dc, attr); | |
9499 | attr->DecRef(); | |
9500 | } | |
9501 | } | |
9502 | ||
9503 | void wxGrid::SetCellHighlightPenWidth(int width) | |
9504 | { | |
9505 | if (m_cellHighlightPenWidth != width) | |
9506 | { | |
9507 | m_cellHighlightPenWidth = width; | |
9508 | ||
9509 | // Just redrawing the cell highlight is not enough since that won't | |
9510 | // make any visible change if the the thickness is getting smaller. | |
9511 | int row = m_currentCellCoords.GetRow(); | |
9512 | int col = m_currentCellCoords.GetCol(); | |
9513 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
9514 | return; | |
9515 | ||
9516 | wxRect rect = CellToRect(row, col); | |
9517 | m_gridWin->Refresh(true, &rect); | |
9518 | } | |
9519 | } | |
9520 | ||
9521 | void wxGrid::SetCellHighlightROPenWidth(int width) | |
9522 | { | |
9523 | if (m_cellHighlightROPenWidth != width) | |
9524 | { | |
9525 | m_cellHighlightROPenWidth = width; | |
9526 | ||
9527 | // Just redrawing the cell highlight is not enough since that won't | |
9528 | // make any visible change if the the thickness is getting smaller. | |
9529 | int row = m_currentCellCoords.GetRow(); | |
9530 | int col = m_currentCellCoords.GetCol(); | |
9531 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
9532 | return; | |
9533 | ||
9534 | wxRect rect = CellToRect(row, col); | |
9535 | m_gridWin->Refresh(true, &rect); | |
9536 | } | |
9537 | } | |
9538 | ||
9539 | void wxGrid::EnableGridLines( bool enable ) | |
9540 | { | |
9541 | if ( enable != m_gridLinesEnabled ) | |
9542 | { | |
9543 | m_gridLinesEnabled = enable; | |
9544 | ||
9545 | if ( !GetBatchCount() ) | |
9546 | { | |
9547 | if ( enable ) | |
9548 | { | |
9549 | wxClientDC dc( m_gridWin ); | |
9550 | PrepareDC( dc ); | |
9551 | DrawAllGridLines( dc, wxRegion() ); | |
9552 | } | |
9553 | else | |
9554 | { | |
9555 | m_gridWin->Refresh(); | |
9556 | } | |
9557 | } | |
9558 | } | |
9559 | } | |
9560 | ||
9561 | int wxGrid::GetDefaultRowSize() | |
9562 | { | |
9563 | return m_defaultRowHeight; | |
9564 | } | |
9565 | ||
9566 | int wxGrid::GetRowSize( int row ) | |
9567 | { | |
9568 | wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") ); | |
9569 | ||
9570 | return GetRowHeight(row); | |
9571 | } | |
9572 | ||
9573 | int wxGrid::GetDefaultColSize() | |
9574 | { | |
9575 | return m_defaultColWidth; | |
9576 | } | |
9577 | ||
9578 | int wxGrid::GetColSize( int col ) | |
9579 | { | |
9580 | wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") ); | |
9581 | ||
9582 | return GetColWidth(col); | |
9583 | } | |
9584 | ||
9585 | // ============================================================================ | |
9586 | // access to the grid attributes: each of them has a default value in the grid | |
9587 | // itself and may be overidden on a per-cell basis | |
9588 | // ============================================================================ | |
9589 | ||
9590 | // ---------------------------------------------------------------------------- | |
9591 | // setting default attributes | |
9592 | // ---------------------------------------------------------------------------- | |
9593 | ||
9594 | void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col ) | |
9595 | { | |
9596 | m_defaultCellAttr->SetBackgroundColour(col); | |
9597 | #ifdef __WXGTK__ | |
9598 | m_gridWin->SetBackgroundColour(col); | |
9599 | #endif | |
9600 | } | |
9601 | ||
9602 | void wxGrid::SetDefaultCellTextColour( const wxColour& col ) | |
9603 | { | |
9604 | m_defaultCellAttr->SetTextColour(col); | |
9605 | } | |
9606 | ||
9607 | void wxGrid::SetDefaultCellAlignment( int horiz, int vert ) | |
9608 | { | |
9609 | m_defaultCellAttr->SetAlignment(horiz, vert); | |
9610 | } | |
9611 | ||
9612 | void wxGrid::SetDefaultCellOverflow( bool allow ) | |
9613 | { | |
9614 | m_defaultCellAttr->SetOverflow(allow); | |
9615 | } | |
9616 | ||
9617 | void wxGrid::SetDefaultCellFont( const wxFont& font ) | |
9618 | { | |
9619 | m_defaultCellAttr->SetFont(font); | |
9620 | } | |
9621 | ||
9622 | // For editors and renderers the type registry takes precedence over the | |
9623 | // default attr, so we need to register the new editor/renderer for the string | |
9624 | // data type in order to make setting a default editor/renderer appear to | |
9625 | // work correctly. | |
9626 | ||
9627 | void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer) | |
9628 | { | |
9629 | RegisterDataType(wxGRID_VALUE_STRING, | |
9630 | renderer, | |
9631 | GetDefaultEditorForType(wxGRID_VALUE_STRING)); | |
9632 | } | |
9633 | ||
9634 | void wxGrid::SetDefaultEditor(wxGridCellEditor *editor) | |
9635 | { | |
9636 | RegisterDataType(wxGRID_VALUE_STRING, | |
9637 | GetDefaultRendererForType(wxGRID_VALUE_STRING), | |
9638 | editor); | |
9639 | } | |
9640 | ||
9641 | // ---------------------------------------------------------------------------- | |
9642 | // access to the default attrbiutes | |
9643 | // ---------------------------------------------------------------------------- | |
9644 | ||
9645 | wxColour wxGrid::GetDefaultCellBackgroundColour() | |
9646 | { | |
9647 | return m_defaultCellAttr->GetBackgroundColour(); | |
9648 | } | |
9649 | ||
9650 | wxColour wxGrid::GetDefaultCellTextColour() | |
9651 | { | |
9652 | return m_defaultCellAttr->GetTextColour(); | |
9653 | } | |
9654 | ||
9655 | wxFont wxGrid::GetDefaultCellFont() | |
9656 | { | |
9657 | return m_defaultCellAttr->GetFont(); | |
9658 | } | |
9659 | ||
9660 | void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) | |
9661 | { | |
9662 | m_defaultCellAttr->GetAlignment(horiz, vert); | |
9663 | } | |
9664 | ||
9665 | bool wxGrid::GetDefaultCellOverflow() | |
9666 | { | |
9667 | return m_defaultCellAttr->GetOverflow(); | |
9668 | } | |
9669 | ||
9670 | wxGridCellRenderer *wxGrid::GetDefaultRenderer() const | |
9671 | { | |
9672 | return m_defaultCellAttr->GetRenderer(NULL, 0, 0); | |
9673 | } | |
9674 | ||
9675 | wxGridCellEditor *wxGrid::GetDefaultEditor() const | |
9676 | { | |
9677 | return m_defaultCellAttr->GetEditor(NULL, 0, 0); | |
9678 | } | |
9679 | ||
9680 | // ---------------------------------------------------------------------------- | |
9681 | // access to cell attributes | |
9682 | // ---------------------------------------------------------------------------- | |
9683 | ||
9684 | wxColour wxGrid::GetCellBackgroundColour(int row, int col) | |
9685 | { | |
9686 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9687 | wxColour colour = attr->GetBackgroundColour(); | |
9688 | attr->DecRef(); | |
9689 | ||
9690 | return colour; | |
9691 | } | |
9692 | ||
9693 | wxColour wxGrid::GetCellTextColour( int row, int col ) | |
9694 | { | |
9695 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9696 | wxColour colour = attr->GetTextColour(); | |
9697 | attr->DecRef(); | |
9698 | ||
9699 | return colour; | |
9700 | } | |
9701 | ||
9702 | wxFont wxGrid::GetCellFont( int row, int col ) | |
9703 | { | |
9704 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9705 | wxFont font = attr->GetFont(); | |
9706 | attr->DecRef(); | |
9707 | ||
9708 | return font; | |
9709 | } | |
9710 | ||
9711 | void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) | |
9712 | { | |
9713 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9714 | attr->GetAlignment(horiz, vert); | |
9715 | attr->DecRef(); | |
9716 | } | |
9717 | ||
9718 | bool wxGrid::GetCellOverflow( int row, int col ) | |
9719 | { | |
9720 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9721 | bool allow = attr->GetOverflow(); | |
9722 | attr->DecRef(); | |
9723 | ||
9724 | return allow; | |
9725 | } | |
9726 | ||
9727 | void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) | |
9728 | { | |
9729 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9730 | attr->GetSize( num_rows, num_cols ); | |
9731 | attr->DecRef(); | |
9732 | } | |
9733 | ||
9734 | wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) | |
9735 | { | |
9736 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
9737 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); | |
9738 | attr->DecRef(); | |
9739 | ||
9740 | return renderer; | |
9741 | } | |
9742 | ||
9743 | wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) | |
9744 | { | |
9745 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
9746 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); | |
9747 | attr->DecRef(); | |
9748 | ||
9749 | return editor; | |
9750 | } | |
9751 | ||
9752 | bool wxGrid::IsReadOnly(int row, int col) const | |
9753 | { | |
9754 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
9755 | bool isReadOnly = attr->IsReadOnly(); | |
9756 | attr->DecRef(); | |
9757 | ||
9758 | return isReadOnly; | |
9759 | } | |
9760 | ||
9761 | // ---------------------------------------------------------------------------- | |
9762 | // attribute support: cache, automatic provider creation, ... | |
9763 | // ---------------------------------------------------------------------------- | |
9764 | ||
9765 | bool wxGrid::CanHaveAttributes() | |
9766 | { | |
9767 | if ( !m_table ) | |
9768 | { | |
9769 | return false; | |
9770 | } | |
9771 | ||
9772 | return m_table->CanHaveAttributes(); | |
9773 | } | |
9774 | ||
9775 | void wxGrid::ClearAttrCache() | |
9776 | { | |
9777 | if ( m_attrCache.row != -1 ) | |
9778 | { | |
9779 | wxSafeDecRef(m_attrCache.attr); | |
9780 | m_attrCache.attr = NULL; | |
9781 | m_attrCache.row = -1; | |
9782 | } | |
9783 | } | |
9784 | ||
9785 | void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const | |
9786 | { | |
9787 | if ( attr != NULL ) | |
9788 | { | |
9789 | wxGrid *self = (wxGrid *)this; // const_cast | |
9790 | ||
9791 | self->ClearAttrCache(); | |
9792 | self->m_attrCache.row = row; | |
9793 | self->m_attrCache.col = col; | |
9794 | self->m_attrCache.attr = attr; | |
9795 | wxSafeIncRef(attr); | |
9796 | } | |
9797 | } | |
9798 | ||
9799 | bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const | |
9800 | { | |
9801 | if ( row == m_attrCache.row && col == m_attrCache.col ) | |
9802 | { | |
9803 | *attr = m_attrCache.attr; | |
9804 | wxSafeIncRef(m_attrCache.attr); | |
9805 | ||
9806 | #ifdef DEBUG_ATTR_CACHE | |
9807 | gs_nAttrCacheHits++; | |
9808 | #endif | |
9809 | ||
9810 | return true; | |
9811 | } | |
9812 | else | |
9813 | { | |
9814 | #ifdef DEBUG_ATTR_CACHE | |
9815 | gs_nAttrCacheMisses++; | |
9816 | #endif | |
9817 | ||
9818 | return false; | |
9819 | } | |
9820 | } | |
9821 | ||
9822 | wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const | |
9823 | { | |
9824 | wxGridCellAttr *attr = NULL; | |
9825 | // Additional test to avoid looking at the cache e.g. for | |
9826 | // wxNoCellCoords, as this will confuse memory management. | |
9827 | if ( row >= 0 ) | |
9828 | { | |
9829 | if ( !LookupAttr(row, col, &attr) ) | |
9830 | { | |
9831 | attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any) | |
9832 | : (wxGridCellAttr *)NULL; | |
9833 | CacheAttr(row, col, attr); | |
9834 | } | |
9835 | } | |
9836 | ||
9837 | if (attr) | |
9838 | { | |
9839 | attr->SetDefAttr(m_defaultCellAttr); | |
9840 | } | |
9841 | else | |
9842 | { | |
9843 | attr = m_defaultCellAttr; | |
9844 | attr->IncRef(); | |
9845 | } | |
9846 | ||
9847 | return attr; | |
9848 | } | |
9849 | ||
9850 | wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const | |
9851 | { | |
9852 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
9853 | bool canHave = ((wxGrid*)this)->CanHaveAttributes(); | |
9854 | ||
9855 | wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed")); | |
9856 | wxCHECK_MSG( m_table, attr, _T("must have a table") ); | |
9857 | ||
9858 | attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell); | |
9859 | if ( !attr ) | |
9860 | { | |
9861 | attr = new wxGridCellAttr(m_defaultCellAttr); | |
9862 | ||
9863 | // artificially inc the ref count to match DecRef() in caller | |
9864 | attr->IncRef(); | |
9865 | m_table->SetAttr(attr, row, col); | |
9866 | } | |
9867 | ||
9868 | return attr; | |
9869 | } | |
9870 | ||
9871 | // ---------------------------------------------------------------------------- | |
9872 | // setting column attributes (wrappers around SetColAttr) | |
9873 | // ---------------------------------------------------------------------------- | |
9874 | ||
9875 | void wxGrid::SetColFormatBool(int col) | |
9876 | { | |
9877 | SetColFormatCustom(col, wxGRID_VALUE_BOOL); | |
9878 | } | |
9879 | ||
9880 | void wxGrid::SetColFormatNumber(int col) | |
9881 | { | |
9882 | SetColFormatCustom(col, wxGRID_VALUE_NUMBER); | |
9883 | } | |
9884 | ||
9885 | void wxGrid::SetColFormatFloat(int col, int width, int precision) | |
9886 | { | |
9887 | wxString typeName = wxGRID_VALUE_FLOAT; | |
9888 | if ( (width != -1) || (precision != -1) ) | |
9889 | { | |
9890 | typeName << _T(':') << width << _T(',') << precision; | |
9891 | } | |
9892 | ||
9893 | SetColFormatCustom(col, typeName); | |
9894 | } | |
9895 | ||
9896 | void wxGrid::SetColFormatCustom(int col, const wxString& typeName) | |
9897 | { | |
9898 | wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col ); | |
9899 | if (!attr) | |
9900 | attr = new wxGridCellAttr; | |
9901 | wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName); | |
9902 | attr->SetRenderer(renderer); | |
9903 | ||
9904 | SetColAttr(col, attr); | |
9905 | ||
9906 | } | |
9907 | ||
9908 | // ---------------------------------------------------------------------------- | |
9909 | // setting cell attributes: this is forwarded to the table | |
9910 | // ---------------------------------------------------------------------------- | |
9911 | ||
9912 | void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr) | |
9913 | { | |
9914 | if ( CanHaveAttributes() ) | |
9915 | { | |
9916 | m_table->SetAttr(attr, row, col); | |
9917 | ClearAttrCache(); | |
9918 | } | |
9919 | else | |
9920 | { | |
9921 | wxSafeDecRef(attr); | |
9922 | } | |
9923 | } | |
9924 | ||
9925 | void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr) | |
9926 | { | |
9927 | if ( CanHaveAttributes() ) | |
9928 | { | |
9929 | m_table->SetRowAttr(attr, row); | |
9930 | ClearAttrCache(); | |
9931 | } | |
9932 | else | |
9933 | { | |
9934 | wxSafeDecRef(attr); | |
9935 | } | |
9936 | } | |
9937 | ||
9938 | void wxGrid::SetColAttr(int col, wxGridCellAttr *attr) | |
9939 | { | |
9940 | if ( CanHaveAttributes() ) | |
9941 | { | |
9942 | m_table->SetColAttr(attr, col); | |
9943 | ClearAttrCache(); | |
9944 | } | |
9945 | else | |
9946 | { | |
9947 | wxSafeDecRef(attr); | |
9948 | } | |
9949 | } | |
9950 | ||
9951 | void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour ) | |
9952 | { | |
9953 | if ( CanHaveAttributes() ) | |
9954 | { | |
9955 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9956 | attr->SetBackgroundColour(colour); | |
9957 | attr->DecRef(); | |
9958 | } | |
9959 | } | |
9960 | ||
9961 | void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour ) | |
9962 | { | |
9963 | if ( CanHaveAttributes() ) | |
9964 | { | |
9965 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9966 | attr->SetTextColour(colour); | |
9967 | attr->DecRef(); | |
9968 | } | |
9969 | } | |
9970 | ||
9971 | void wxGrid::SetCellFont( int row, int col, const wxFont& font ) | |
9972 | { | |
9973 | if ( CanHaveAttributes() ) | |
9974 | { | |
9975 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9976 | attr->SetFont(font); | |
9977 | attr->DecRef(); | |
9978 | } | |
9979 | } | |
9980 | ||
9981 | void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert ) | |
9982 | { | |
9983 | if ( CanHaveAttributes() ) | |
9984 | { | |
9985 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9986 | attr->SetAlignment(horiz, vert); | |
9987 | attr->DecRef(); | |
9988 | } | |
9989 | } | |
9990 | ||
9991 | void wxGrid::SetCellOverflow( int row, int col, bool allow ) | |
9992 | { | |
9993 | if ( CanHaveAttributes() ) | |
9994 | { | |
9995 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9996 | attr->SetOverflow(allow); | |
9997 | attr->DecRef(); | |
9998 | } | |
9999 | } | |
10000 | ||
10001 | void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols ) | |
10002 | { | |
10003 | if ( CanHaveAttributes() ) | |
10004 | { | |
10005 | int cell_rows, cell_cols; | |
10006 | ||
10007 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
10008 | attr->GetSize(&cell_rows, &cell_cols); | |
10009 | attr->SetSize(num_rows, num_cols); | |
10010 | attr->DecRef(); | |
10011 | ||
10012 | // Cannot set the size of a cell to 0 or negative values | |
10013 | // While it is perfectly legal to do that, this function cannot | |
10014 | // handle all the possibilies, do it by hand by getting the CellAttr. | |
10015 | // You can only set the size of a cell to 1,1 or greater with this fn | |
10016 | wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)), | |
10017 | wxT("wxGrid::SetCellSize setting cell size that is already part of another cell")); | |
10018 | wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)), | |
10019 | wxT("wxGrid::SetCellSize setting cell size to < 1")); | |
10020 | ||
10021 | // if this was already a multicell then "turn off" the other cells first | |
10022 | if ((cell_rows > 1) || (cell_rows > 1)) | |
10023 | { | |
10024 | int i, j; | |
10025 | for (j=row; j < row + cell_rows; j++) | |
10026 | { | |
10027 | for (i=col; i < col + cell_cols; i++) | |
10028 | { | |
10029 | if ((i != col) || (j != row)) | |
10030 | { | |
10031 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
10032 | attr_stub->SetSize( 1, 1 ); | |
10033 | attr_stub->DecRef(); | |
10034 | } | |
10035 | } | |
10036 | } | |
10037 | } | |
10038 | ||
10039 | // mark the cells that will be covered by this cell to | |
10040 | // negative or zero values to point back at this cell | |
10041 | if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1)) | |
10042 | { | |
10043 | int i, j; | |
10044 | for (j=row; j < row + num_rows; j++) | |
10045 | { | |
10046 | for (i=col; i < col + num_cols; i++) | |
10047 | { | |
10048 | if ((i != col) || (j != row)) | |
10049 | { | |
10050 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
10051 | attr_stub->SetSize( row - j, col - i ); | |
10052 | attr_stub->DecRef(); | |
10053 | } | |
10054 | } | |
10055 | } | |
10056 | } | |
10057 | } | |
10058 | } | |
10059 | ||
10060 | void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer) | |
10061 | { | |
10062 | if ( CanHaveAttributes() ) | |
10063 | { | |
10064 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
10065 | attr->SetRenderer(renderer); | |
10066 | attr->DecRef(); | |
10067 | } | |
10068 | } | |
10069 | ||
10070 | void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor) | |
10071 | { | |
10072 | if ( CanHaveAttributes() ) | |
10073 | { | |
10074 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
10075 | attr->SetEditor(editor); | |
10076 | attr->DecRef(); | |
10077 | } | |
10078 | } | |
10079 | ||
10080 | void wxGrid::SetReadOnly(int row, int col, bool isReadOnly) | |
10081 | { | |
10082 | if ( CanHaveAttributes() ) | |
10083 | { | |
10084 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
10085 | attr->SetReadOnly(isReadOnly); | |
10086 | attr->DecRef(); | |
10087 | } | |
10088 | } | |
10089 | ||
10090 | // ---------------------------------------------------------------------------- | |
10091 | // Data type registration | |
10092 | // ---------------------------------------------------------------------------- | |
10093 | ||
10094 | void wxGrid::RegisterDataType(const wxString& typeName, | |
10095 | wxGridCellRenderer* renderer, | |
10096 | wxGridCellEditor* editor) | |
10097 | { | |
10098 | m_typeRegistry->RegisterDataType(typeName, renderer, editor); | |
10099 | } | |
10100 | ||
10101 | ||
10102 | wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const | |
10103 | { | |
10104 | wxString typeName = m_table->GetTypeName(row, col); | |
10105 | return GetDefaultEditorForType(typeName); | |
10106 | } | |
10107 | ||
10108 | wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const | |
10109 | { | |
10110 | wxString typeName = m_table->GetTypeName(row, col); | |
10111 | return GetDefaultRendererForType(typeName); | |
10112 | } | |
10113 | ||
10114 | wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const | |
10115 | { | |
10116 | int index = m_typeRegistry->FindOrCloneDataType(typeName); | |
10117 | if ( index == wxNOT_FOUND ) | |
10118 | { | |
10119 | wxString errStr; | |
10120 | ||
10121 | errStr.Printf(wxT("Unknown data type name [%s]"), typeName.c_str()); | |
10122 | wxFAIL_MSG(errStr.c_str()); | |
10123 | ||
10124 | return NULL; | |
10125 | } | |
10126 | ||
10127 | return m_typeRegistry->GetEditor(index); | |
10128 | } | |
10129 | ||
10130 | wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const | |
10131 | { | |
10132 | int index = m_typeRegistry->FindOrCloneDataType(typeName); | |
10133 | if ( index == wxNOT_FOUND ) | |
10134 | { | |
10135 | wxString errStr; | |
10136 | ||
10137 | errStr.Printf(wxT("Unknown data type name [%s]"), typeName.c_str()); | |
10138 | wxFAIL_MSG(errStr.c_str()); | |
10139 | ||
10140 | return NULL; | |
10141 | } | |
10142 | ||
10143 | return m_typeRegistry->GetRenderer(index); | |
10144 | } | |
10145 | ||
10146 | // ---------------------------------------------------------------------------- | |
10147 | // row/col size | |
10148 | // ---------------------------------------------------------------------------- | |
10149 | ||
10150 | void wxGrid::EnableDragRowSize( bool enable ) | |
10151 | { | |
10152 | m_canDragRowSize = enable; | |
10153 | } | |
10154 | ||
10155 | void wxGrid::EnableDragColSize( bool enable ) | |
10156 | { | |
10157 | m_canDragColSize = enable; | |
10158 | } | |
10159 | ||
10160 | void wxGrid::EnableDragGridSize( bool enable ) | |
10161 | { | |
10162 | m_canDragGridSize = enable; | |
10163 | } | |
10164 | ||
10165 | void wxGrid::EnableDragCell( bool enable ) | |
10166 | { | |
10167 | m_canDragCell = enable; | |
10168 | } | |
10169 | ||
10170 | void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) | |
10171 | { | |
10172 | m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight ); | |
10173 | ||
10174 | if ( resizeExistingRows ) | |
10175 | { | |
10176 | // since we are resizing all rows to the default row size, | |
10177 | // we can simply clear the row heights and row bottoms | |
10178 | // arrays (which also allows us to take advantage of | |
10179 | // some speed optimisations) | |
10180 | m_rowHeights.Empty(); | |
10181 | m_rowBottoms.Empty(); | |
10182 | if ( !GetBatchCount() ) | |
10183 | CalcDimensions(); | |
10184 | } | |
10185 | } | |
10186 | ||
10187 | void wxGrid::SetRowSize( int row, int height ) | |
10188 | { | |
10189 | wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") ); | |
10190 | ||
10191 | // See comment in SetColSize | |
10192 | if ( height < GetRowMinimalAcceptableHeight()) | |
10193 | return; | |
10194 | ||
10195 | if ( m_rowHeights.IsEmpty() ) | |
10196 | { | |
10197 | // need to really create the array | |
10198 | InitRowHeights(); | |
10199 | } | |
10200 | ||
10201 | int h = wxMax( 0, height ); | |
10202 | int diff = h - m_rowHeights[row]; | |
10203 | ||
10204 | m_rowHeights[row] = h; | |
10205 | int i; | |
10206 | for ( i = row; i < m_numRows; i++ ) | |
10207 | { | |
10208 | m_rowBottoms[i] += diff; | |
10209 | } | |
10210 | ||
10211 | if ( !GetBatchCount() ) | |
10212 | CalcDimensions(); | |
10213 | } | |
10214 | ||
10215 | void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) | |
10216 | { | |
10217 | m_defaultColWidth = wxMax( width, m_minAcceptableColWidth ); | |
10218 | ||
10219 | if ( resizeExistingCols ) | |
10220 | { | |
10221 | // since we are resizing all columns to the default column size, | |
10222 | // we can simply clear the col widths and col rights | |
10223 | // arrays (which also allows us to take advantage of | |
10224 | // some speed optimisations) | |
10225 | m_colWidths.Empty(); | |
10226 | m_colRights.Empty(); | |
10227 | if ( !GetBatchCount() ) | |
10228 | CalcDimensions(); | |
10229 | } | |
10230 | } | |
10231 | ||
10232 | void wxGrid::SetColSize( int col, int width ) | |
10233 | { | |
10234 | wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") ); | |
10235 | ||
10236 | // should we check that it's bigger than GetColMinimalWidth(col) here? | |
10237 | // (VZ) | |
10238 | // No, because it is reasonable to assume the library user know's | |
10239 | // what he is doing. However we should test against the weaker | |
10240 | // constraint of minimalAcceptableWidth, as this breaks rendering | |
10241 | // | |
10242 | // This test then fixes sf.net bug #645734 | |
10243 | ||
10244 | if ( width < GetColMinimalAcceptableWidth() ) | |
10245 | return; | |
10246 | ||
10247 | if ( m_colWidths.IsEmpty() ) | |
10248 | { | |
10249 | // need to really create the array | |
10250 | InitColWidths(); | |
10251 | } | |
10252 | ||
10253 | // if < 0 then calculate new width from label | |
10254 | if ( width < 0 ) | |
10255 | { | |
10256 | long w, h; | |
10257 | wxArrayString lines; | |
10258 | wxClientDC dc(m_colLabelWin); | |
10259 | dc.SetFont(GetLabelFont()); | |
10260 | StringToLines(GetColLabelValue(col), lines); | |
10261 | GetTextBoxSize(dc, lines, &w, &h); | |
10262 | width = w + 6; | |
10263 | } | |
10264 | ||
10265 | int w = wxMax( 0, width ); | |
10266 | int diff = w - m_colWidths[col]; | |
10267 | m_colWidths[col] = w; | |
10268 | ||
10269 | int i; | |
10270 | int colPos; | |
10271 | for ( colPos = GetColPos( col ); colPos < m_numCols; colPos++ ) | |
10272 | { | |
10273 | i = GetColAt( colPos ); | |
10274 | m_colRights[i] += diff; | |
10275 | } | |
10276 | ||
10277 | if ( !GetBatchCount() ) | |
10278 | CalcDimensions(); | |
10279 | } | |
10280 | ||
10281 | void wxGrid::SetColMinimalWidth( int col, int width ) | |
10282 | { | |
10283 | if (width > GetColMinimalAcceptableWidth()) | |
10284 | { | |
10285 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col; | |
10286 | m_colMinWidths[key] = width; | |
10287 | } | |
10288 | } | |
10289 | ||
10290 | void wxGrid::SetRowMinimalHeight( int row, int width ) | |
10291 | { | |
10292 | if (width > GetRowMinimalAcceptableHeight()) | |
10293 | { | |
10294 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row; | |
10295 | m_rowMinHeights[key] = width; | |
10296 | } | |
10297 | } | |
10298 | ||
10299 | int wxGrid::GetColMinimalWidth(int col) const | |
10300 | { | |
10301 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col; | |
10302 | wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key); | |
10303 | ||
10304 | return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth; | |
10305 | } | |
10306 | ||
10307 | int wxGrid::GetRowMinimalHeight(int row) const | |
10308 | { | |
10309 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row; | |
10310 | wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key); | |
10311 | ||
10312 | return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight; | |
10313 | } | |
10314 | ||
10315 | void wxGrid::SetColMinimalAcceptableWidth( int width ) | |
10316 | { | |
10317 | // We do allow a width of 0 since this gives us | |
10318 | // an easy way to temporarily hiding columns. | |
10319 | if ( width >= 0 ) | |
10320 | m_minAcceptableColWidth = width; | |
10321 | } | |
10322 | ||
10323 | void wxGrid::SetRowMinimalAcceptableHeight( int height ) | |
10324 | { | |
10325 | // We do allow a height of 0 since this gives us | |
10326 | // an easy way to temporarily hiding rows. | |
10327 | if ( height >= 0 ) | |
10328 | m_minAcceptableRowHeight = height; | |
10329 | } | |
10330 | ||
10331 | int wxGrid::GetColMinimalAcceptableWidth() const | |
10332 | { | |
10333 | return m_minAcceptableColWidth; | |
10334 | } | |
10335 | ||
10336 | int wxGrid::GetRowMinimalAcceptableHeight() const | |
10337 | { | |
10338 | return m_minAcceptableRowHeight; | |
10339 | } | |
10340 | ||
10341 | // ---------------------------------------------------------------------------- | |
10342 | // auto sizing | |
10343 | // ---------------------------------------------------------------------------- | |
10344 | ||
10345 | void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) | |
10346 | { | |
10347 | wxClientDC dc(m_gridWin); | |
10348 | ||
10349 | // cancel editing of cell | |
10350 | HideCellEditControl(); | |
10351 | SaveEditControlValue(); | |
10352 | ||
10353 | // init both of them to avoid compiler warnings, even if we only need one | |
10354 | int row = -1, | |
10355 | col = -1; | |
10356 | if ( column ) | |
10357 | col = colOrRow; | |
10358 | else | |
10359 | row = colOrRow; | |
10360 | ||
10361 | wxCoord extent, extentMax = 0; | |
10362 | int max = column ? m_numRows : m_numCols; | |
10363 | for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ ) | |
10364 | { | |
10365 | if ( column ) | |
10366 | row = rowOrCol; | |
10367 | else | |
10368 | col = rowOrCol; | |
10369 | ||
10370 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
10371 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); | |
10372 | if ( renderer ) | |
10373 | { | |
10374 | wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col); | |
10375 | extent = column ? size.x : size.y; | |
10376 | if ( extent > extentMax ) | |
10377 | extentMax = extent; | |
10378 | ||
10379 | renderer->DecRef(); | |
10380 | } | |
10381 | ||
10382 | attr->DecRef(); | |
10383 | } | |
10384 | ||
10385 | // now also compare with the column label extent | |
10386 | wxCoord w, h; | |
10387 | dc.SetFont( GetLabelFont() ); | |
10388 | ||
10389 | if ( column ) | |
10390 | { | |
10391 | dc.GetMultiLineTextExtent( GetColLabelValue(col), &w, &h ); | |
10392 | if ( GetColLabelTextOrientation() == wxVERTICAL ) | |
10393 | w = h; | |
10394 | } | |
10395 | else | |
10396 | dc.GetMultiLineTextExtent( GetRowLabelValue(row), &w, &h ); | |
10397 | ||
10398 | extent = column ? w : h; | |
10399 | if ( extent > extentMax ) | |
10400 | extentMax = extent; | |
10401 | ||
10402 | if ( !extentMax ) | |
10403 | { | |
10404 | // empty column - give default extent (notice that if extentMax is less | |
10405 | // than default extent but != 0, it's OK) | |
10406 | extentMax = column ? m_defaultColWidth : m_defaultRowHeight; | |
10407 | } | |
10408 | else | |
10409 | { | |
10410 | if ( column ) | |
10411 | // leave some space around text | |
10412 | extentMax += 10; | |
10413 | else | |
10414 | extentMax += 6; | |
10415 | } | |
10416 | ||
10417 | if ( column ) | |
10418 | { | |
10419 | SetColSize( col, extentMax ); | |
10420 | if ( !GetBatchCount() ) | |
10421 | { | |
10422 | int cw, ch, dummy; | |
10423 | m_gridWin->GetClientSize( &cw, &ch ); | |
10424 | wxRect rect ( CellToRect( 0, col ) ); | |
10425 | rect.y = 0; | |
10426 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
10427 | rect.width = cw - rect.x; | |
10428 | rect.height = m_colLabelHeight; | |
10429 | m_colLabelWin->Refresh( true, &rect ); | |
10430 | } | |
10431 | } | |
10432 | else | |
10433 | { | |
10434 | SetRowSize(row, extentMax); | |
10435 | if ( !GetBatchCount() ) | |
10436 | { | |
10437 | int cw, ch, dummy; | |
10438 | m_gridWin->GetClientSize( &cw, &ch ); | |
10439 | wxRect rect( CellToRect( row, 0 ) ); | |
10440 | rect.x = 0; | |
10441 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
10442 | rect.width = m_rowLabelWidth; | |
10443 | rect.height = ch - rect.y; | |
10444 | m_rowLabelWin->Refresh( true, &rect ); | |
10445 | } | |
10446 | } | |
10447 | ||
10448 | if ( setAsMin ) | |
10449 | { | |
10450 | if ( column ) | |
10451 | SetColMinimalWidth(col, extentMax); | |
10452 | else | |
10453 | SetRowMinimalHeight(row, extentMax); | |
10454 | } | |
10455 | } | |
10456 | ||
10457 | int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) | |
10458 | { | |
10459 | int width = m_rowLabelWidth; | |
10460 | ||
10461 | if ( !calcOnly ) | |
10462 | BeginBatch(); | |
10463 | ||
10464 | for ( int col = 0; col < m_numCols; col++ ) | |
10465 | { | |
10466 | if ( !calcOnly ) | |
10467 | AutoSizeColumn(col, setAsMin); | |
10468 | ||
10469 | width += GetColWidth(col); | |
10470 | } | |
10471 | ||
10472 | if ( !calcOnly ) | |
10473 | EndBatch(); | |
10474 | ||
10475 | return width; | |
10476 | } | |
10477 | ||
10478 | int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin) | |
10479 | { | |
10480 | int height = m_colLabelHeight; | |
10481 | ||
10482 | if ( !calcOnly ) | |
10483 | BeginBatch(); | |
10484 | ||
10485 | for ( int row = 0; row < m_numRows; row++ ) | |
10486 | { | |
10487 | if ( !calcOnly ) | |
10488 | AutoSizeRow(row, setAsMin); | |
10489 | ||
10490 | height += GetRowHeight(row); | |
10491 | } | |
10492 | ||
10493 | if ( !calcOnly ) | |
10494 | EndBatch(); | |
10495 | ||
10496 | return height; | |
10497 | } | |
10498 | ||
10499 | void wxGrid::AutoSize() | |
10500 | { | |
10501 | BeginBatch(); | |
10502 | ||
10503 | wxSize size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false)); | |
10504 | ||
10505 | // round up the size to a multiple of scroll step - this ensures that we | |
10506 | // won't get the scrollbars if we're sized exactly to this width | |
10507 | // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary | |
10508 | // scrollbar steps | |
10509 | wxSize sizeFit( | |
10510 | GetScrollX(size.x + m_extraWidth + 1) * m_scrollLineX, | |
10511 | GetScrollY(size.y + m_extraHeight + 1) * m_scrollLineY ); | |
10512 | ||
10513 | // distribute the extra space between the columns/rows to avoid having | |
10514 | // extra white space | |
10515 | ||
10516 | // Remove the extra m_extraWidth + 1 added above | |
10517 | wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1); | |
10518 | if ( diff && m_numCols ) | |
10519 | { | |
10520 | // try to resize the columns uniformly | |
10521 | wxCoord diffPerCol = diff / m_numCols; | |
10522 | if ( diffPerCol ) | |
10523 | { | |
10524 | for ( int col = 0; col < m_numCols; col++ ) | |
10525 | { | |
10526 | SetColSize(col, GetColWidth(col) + diffPerCol); | |
10527 | } | |
10528 | } | |
10529 | ||
10530 | // add remaining amount to the last columns | |
10531 | diff -= diffPerCol * m_numCols; | |
10532 | if ( diff ) | |
10533 | { | |
10534 | for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- ) | |
10535 | { | |
10536 | SetColSize(col, GetColWidth(col) + 1); | |
10537 | } | |
10538 | } | |
10539 | } | |
10540 | ||
10541 | // same for rows | |
10542 | diff = sizeFit.y - size.y - (m_extraHeight + 1); | |
10543 | if ( diff && m_numRows ) | |
10544 | { | |
10545 | // try to resize the columns uniformly | |
10546 | wxCoord diffPerRow = diff / m_numRows; | |
10547 | if ( diffPerRow ) | |
10548 | { | |
10549 | for ( int row = 0; row < m_numRows; row++ ) | |
10550 | { | |
10551 | SetRowSize(row, GetRowHeight(row) + diffPerRow); | |
10552 | } | |
10553 | } | |
10554 | ||
10555 | // add remaining amount to the last rows | |
10556 | diff -= diffPerRow * m_numRows; | |
10557 | if ( diff ) | |
10558 | { | |
10559 | for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- ) | |
10560 | { | |
10561 | SetRowSize(row, GetRowHeight(row) + 1); | |
10562 | } | |
10563 | } | |
10564 | } | |
10565 | ||
10566 | EndBatch(); | |
10567 | ||
10568 | SetClientSize(sizeFit); | |
10569 | } | |
10570 | ||
10571 | void wxGrid::AutoSizeRowLabelSize( int row ) | |
10572 | { | |
10573 | wxArrayString lines; | |
10574 | long w, h; | |
10575 | ||
10576 | // Hide the edit control, so it | |
10577 | // won't interfere with drag-shrinking. | |
10578 | if ( IsCellEditControlShown() ) | |
10579 | { | |
10580 | HideCellEditControl(); | |
10581 | SaveEditControlValue(); | |
10582 | } | |
10583 | ||
10584 | // autosize row height depending on label text | |
10585 | StringToLines( GetRowLabelValue( row ), lines ); | |
10586 | wxClientDC dc( m_rowLabelWin ); | |
10587 | GetTextBoxSize( dc, lines, &w, &h ); | |
10588 | if ( h < m_defaultRowHeight ) | |
10589 | h = m_defaultRowHeight; | |
10590 | SetRowSize(row, h); | |
10591 | ForceRefresh(); | |
10592 | } | |
10593 | ||
10594 | void wxGrid::AutoSizeColLabelSize( int col ) | |
10595 | { | |
10596 | wxArrayString lines; | |
10597 | long w, h; | |
10598 | ||
10599 | // Hide the edit control, so it | |
10600 | // won't interfere with drag-shrinking. | |
10601 | if ( IsCellEditControlShown() ) | |
10602 | { | |
10603 | HideCellEditControl(); | |
10604 | SaveEditControlValue(); | |
10605 | } | |
10606 | ||
10607 | // autosize column width depending on label text | |
10608 | StringToLines( GetColLabelValue( col ), lines ); | |
10609 | wxClientDC dc( m_colLabelWin ); | |
10610 | if ( GetColLabelTextOrientation() == wxHORIZONTAL ) | |
10611 | GetTextBoxSize( dc, lines, &w, &h ); | |
10612 | else | |
10613 | GetTextBoxSize( dc, lines, &h, &w ); | |
10614 | if ( w < m_defaultColWidth ) | |
10615 | w = m_defaultColWidth; | |
10616 | SetColSize(col, w); | |
10617 | ForceRefresh(); | |
10618 | } | |
10619 | ||
10620 | wxSize wxGrid::DoGetBestSize() const | |
10621 | { | |
10622 | // don't set sizes, only calculate them | |
10623 | wxGrid *self = (wxGrid *)this; // const_cast | |
10624 | ||
10625 | int width, height; | |
10626 | width = self->SetOrCalcColumnSizes(true); | |
10627 | height = self->SetOrCalcRowSizes(true); | |
10628 | ||
10629 | if (!width) | |
10630 | width = 100; | |
10631 | if (!height) | |
10632 | height = 80; | |
10633 | ||
10634 | // Round up to a multiple the scroll rate | |
10635 | // NOTE: this still doesn't get rid of the scrollbars; | |
10636 | // is there any magic incantation for that? | |
10637 | int xpu, ypu; | |
10638 | GetScrollPixelsPerUnit(&xpu, &ypu); | |
10639 | if (xpu) | |
10640 | width += 1 + xpu - (width % xpu); | |
10641 | if (ypu) | |
10642 | height += 1 + ypu - (height % ypu); | |
10643 | ||
10644 | // limit to 1/4 of the screen size | |
10645 | int maxwidth, maxheight; | |
10646 | wxDisplaySize( &maxwidth, &maxheight ); | |
10647 | maxwidth /= 2; | |
10648 | maxheight /= 2; | |
10649 | if ( width > maxwidth ) | |
10650 | width = maxwidth; | |
10651 | if ( height > maxheight ) | |
10652 | height = maxheight; | |
10653 | ||
10654 | wxSize best(width, height); | |
10655 | ||
10656 | // NOTE: This size should be cached, but first we need to add calls to | |
10657 | // InvalidateBestSize everywhere that could change the results of this | |
10658 | // calculation. | |
10659 | // CacheBestSize(size); | |
10660 | ||
10661 | return best; | |
10662 | } | |
10663 | ||
10664 | void wxGrid::Fit() | |
10665 | { | |
10666 | AutoSize(); | |
10667 | } | |
10668 | ||
10669 | wxPen& wxGrid::GetDividerPen() const | |
10670 | { | |
10671 | return wxNullPen; | |
10672 | } | |
10673 | ||
10674 | // ---------------------------------------------------------------------------- | |
10675 | // cell value accessor functions | |
10676 | // ---------------------------------------------------------------------------- | |
10677 | ||
10678 | void wxGrid::SetCellValue( int row, int col, const wxString& s ) | |
10679 | { | |
10680 | if ( m_table ) | |
10681 | { | |
10682 | m_table->SetValue( row, col, s ); | |
10683 | if ( !GetBatchCount() ) | |
10684 | { | |
10685 | int dummy; | |
10686 | wxRect rect( CellToRect( row, col ) ); | |
10687 | rect.x = 0; | |
10688 | rect.width = m_gridWin->GetClientSize().GetWidth(); | |
10689 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
10690 | m_gridWin->Refresh( false, &rect ); | |
10691 | } | |
10692 | ||
10693 | if ( m_currentCellCoords.GetRow() == row && | |
10694 | m_currentCellCoords.GetCol() == col && | |
10695 | IsCellEditControlShown()) | |
10696 | // Note: If we are using IsCellEditControlEnabled, | |
10697 | // this interacts badly with calling SetCellValue from | |
10698 | // an EVT_GRID_CELL_CHANGE handler. | |
10699 | { | |
10700 | HideCellEditControl(); | |
10701 | ShowCellEditControl(); // will reread data from table | |
10702 | } | |
10703 | } | |
10704 | } | |
10705 | ||
10706 | // ---------------------------------------------------------------------------- | |
10707 | // block, row and column selection | |
10708 | // ---------------------------------------------------------------------------- | |
10709 | ||
10710 | void wxGrid::SelectRow( int row, bool addToSelected ) | |
10711 | { | |
10712 | if ( IsSelection() && !addToSelected ) | |
10713 | ClearSelection(); | |
10714 | ||
10715 | if ( m_selection ) | |
10716 | m_selection->SelectRow( row, false, addToSelected ); | |
10717 | } | |
10718 | ||
10719 | void wxGrid::SelectCol( int col, bool addToSelected ) | |
10720 | { | |
10721 | if ( IsSelection() && !addToSelected ) | |
10722 | ClearSelection(); | |
10723 | ||
10724 | if ( m_selection ) | |
10725 | m_selection->SelectCol( col, false, addToSelected ); | |
10726 | } | |
10727 | ||
10728 | void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol, | |
10729 | bool addToSelected ) | |
10730 | { | |
10731 | if ( IsSelection() && !addToSelected ) | |
10732 | ClearSelection(); | |
10733 | ||
10734 | if ( m_selection ) | |
10735 | m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol, | |
10736 | false, addToSelected ); | |
10737 | } | |
10738 | ||
10739 | void wxGrid::SelectAll() | |
10740 | { | |
10741 | if ( m_numRows > 0 && m_numCols > 0 ) | |
10742 | { | |
10743 | if ( m_selection ) | |
10744 | m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 ); | |
10745 | } | |
10746 | } | |
10747 | ||
10748 | // ---------------------------------------------------------------------------- | |
10749 | // cell, row and col deselection | |
10750 | // ---------------------------------------------------------------------------- | |
10751 | ||
10752 | void wxGrid::DeselectRow( int row ) | |
10753 | { | |
10754 | if ( !m_selection ) | |
10755 | return; | |
10756 | ||
10757 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows ) | |
10758 | { | |
10759 | if ( m_selection->IsInSelection(row, 0 ) ) | |
10760 | m_selection->ToggleCellSelection(row, 0); | |
10761 | } | |
10762 | else | |
10763 | { | |
10764 | int nCols = GetNumberCols(); | |
10765 | for ( int i = 0; i < nCols; i++ ) | |
10766 | { | |
10767 | if ( m_selection->IsInSelection(row, i ) ) | |
10768 | m_selection->ToggleCellSelection(row, i); | |
10769 | } | |
10770 | } | |
10771 | } | |
10772 | ||
10773 | void wxGrid::DeselectCol( int col ) | |
10774 | { | |
10775 | if ( !m_selection ) | |
10776 | return; | |
10777 | ||
10778 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns ) | |
10779 | { | |
10780 | if ( m_selection->IsInSelection(0, col ) ) | |
10781 | m_selection->ToggleCellSelection(0, col); | |
10782 | } | |
10783 | else | |
10784 | { | |
10785 | int nRows = GetNumberRows(); | |
10786 | for ( int i = 0; i < nRows; i++ ) | |
10787 | { | |
10788 | if ( m_selection->IsInSelection(i, col ) ) | |
10789 | m_selection->ToggleCellSelection(i, col); | |
10790 | } | |
10791 | } | |
10792 | } | |
10793 | ||
10794 | void wxGrid::DeselectCell( int row, int col ) | |
10795 | { | |
10796 | if ( m_selection && m_selection->IsInSelection(row, col) ) | |
10797 | m_selection->ToggleCellSelection(row, col); | |
10798 | } | |
10799 | ||
10800 | bool wxGrid::IsSelection() | |
10801 | { | |
10802 | return ( m_selection && (m_selection->IsSelection() || | |
10803 | ( m_selectingTopLeft != wxGridNoCellCoords && | |
10804 | m_selectingBottomRight != wxGridNoCellCoords) ) ); | |
10805 | } | |
10806 | ||
10807 | bool wxGrid::IsInSelection( int row, int col ) const | |
10808 | { | |
10809 | return ( m_selection && (m_selection->IsInSelection( row, col ) || | |
10810 | ( row >= m_selectingTopLeft.GetRow() && | |
10811 | col >= m_selectingTopLeft.GetCol() && | |
10812 | row <= m_selectingBottomRight.GetRow() && | |
10813 | col <= m_selectingBottomRight.GetCol() )) ); | |
10814 | } | |
10815 | ||
10816 | wxGridCellCoordsArray wxGrid::GetSelectedCells() const | |
10817 | { | |
10818 | if (!m_selection) | |
10819 | { | |
10820 | wxGridCellCoordsArray a; | |
10821 | return a; | |
10822 | } | |
10823 | ||
10824 | return m_selection->m_cellSelection; | |
10825 | } | |
10826 | ||
10827 | wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const | |
10828 | { | |
10829 | if (!m_selection) | |
10830 | { | |
10831 | wxGridCellCoordsArray a; | |
10832 | return a; | |
10833 | } | |
10834 | ||
10835 | return m_selection->m_blockSelectionTopLeft; | |
10836 | } | |
10837 | ||
10838 | wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const | |
10839 | { | |
10840 | if (!m_selection) | |
10841 | { | |
10842 | wxGridCellCoordsArray a; | |
10843 | return a; | |
10844 | } | |
10845 | ||
10846 | return m_selection->m_blockSelectionBottomRight; | |
10847 | } | |
10848 | ||
10849 | wxArrayInt wxGrid::GetSelectedRows() const | |
10850 | { | |
10851 | if (!m_selection) | |
10852 | { | |
10853 | wxArrayInt a; | |
10854 | return a; | |
10855 | } | |
10856 | ||
10857 | return m_selection->m_rowSelection; | |
10858 | } | |
10859 | ||
10860 | wxArrayInt wxGrid::GetSelectedCols() const | |
10861 | { | |
10862 | if (!m_selection) | |
10863 | { | |
10864 | wxArrayInt a; | |
10865 | return a; | |
10866 | } | |
10867 | ||
10868 | return m_selection->m_colSelection; | |
10869 | } | |
10870 | ||
10871 | void wxGrid::ClearSelection() | |
10872 | { | |
10873 | m_selectingTopLeft = | |
10874 | m_selectingBottomRight = | |
10875 | m_selectingKeyboard = wxGridNoCellCoords; | |
10876 | if ( m_selection ) | |
10877 | m_selection->ClearSelection(); | |
10878 | } | |
10879 | ||
10880 | // This function returns the rectangle that encloses the given block | |
10881 | // in device coords clipped to the client size of the grid window. | |
10882 | // | |
10883 | wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft, | |
10884 | const wxGridCellCoords &bottomRight ) | |
10885 | { | |
10886 | wxRect rect( wxGridNoCellRect ); | |
10887 | wxRect cellRect; | |
10888 | ||
10889 | cellRect = CellToRect( topLeft ); | |
10890 | if ( cellRect != wxGridNoCellRect ) | |
10891 | { | |
10892 | rect = cellRect; | |
10893 | } | |
10894 | else | |
10895 | { | |
10896 | rect = wxRect(0, 0, 0, 0); | |
10897 | } | |
10898 | ||
10899 | cellRect = CellToRect( bottomRight ); | |
10900 | if ( cellRect != wxGridNoCellRect ) | |
10901 | { | |
10902 | rect += cellRect; | |
10903 | } | |
10904 | else | |
10905 | { | |
10906 | return wxGridNoCellRect; | |
10907 | } | |
10908 | ||
10909 | int i, j; | |
10910 | int left = rect.GetLeft(); | |
10911 | int top = rect.GetTop(); | |
10912 | int right = rect.GetRight(); | |
10913 | int bottom = rect.GetBottom(); | |
10914 | ||
10915 | int leftCol = topLeft.GetCol(); | |
10916 | int topRow = topLeft.GetRow(); | |
10917 | int rightCol = bottomRight.GetCol(); | |
10918 | int bottomRow = bottomRight.GetRow(); | |
10919 | ||
10920 | if (left > right) | |
10921 | { | |
10922 | i = left; | |
10923 | left = right; | |
10924 | right = i; | |
10925 | i = leftCol; | |
10926 | leftCol = rightCol; | |
10927 | rightCol = i; | |
10928 | } | |
10929 | ||
10930 | if (top > bottom) | |
10931 | { | |
10932 | i = top; | |
10933 | top = bottom; | |
10934 | bottom = i; | |
10935 | i = topRow; | |
10936 | topRow = bottomRow; | |
10937 | bottomRow = i; | |
10938 | } | |
10939 | ||
10940 | for ( j = topRow; j <= bottomRow; j++ ) | |
10941 | { | |
10942 | for ( i = leftCol; i <= rightCol; i++ ) | |
10943 | { | |
10944 | if ((j == topRow) || (j == bottomRow) || (i == leftCol) || (i == rightCol)) | |
10945 | { | |
10946 | cellRect = CellToRect( j, i ); | |
10947 | ||
10948 | if (cellRect.x < left) | |
10949 | left = cellRect.x; | |
10950 | if (cellRect.y < top) | |
10951 | top = cellRect.y; | |
10952 | if (cellRect.x + cellRect.width > right) | |
10953 | right = cellRect.x + cellRect.width; | |
10954 | if (cellRect.y + cellRect.height > bottom) | |
10955 | bottom = cellRect.y + cellRect.height; | |
10956 | } | |
10957 | else | |
10958 | { | |
10959 | i = rightCol; // jump over inner cells. | |
10960 | } | |
10961 | } | |
10962 | } | |
10963 | ||
10964 | // convert to scrolled coords | |
10965 | // | |
10966 | CalcScrolledPosition( left, top, &left, &top ); | |
10967 | CalcScrolledPosition( right, bottom, &right, &bottom ); | |
10968 | ||
10969 | int cw, ch; | |
10970 | m_gridWin->GetClientSize( &cw, &ch ); | |
10971 | ||
10972 | if (right < 0 || bottom < 0 || left > cw || top > ch) | |
10973 | return wxRect(0,0,0,0); | |
10974 | ||
10975 | rect.SetLeft( wxMax(0, left) ); | |
10976 | rect.SetTop( wxMax(0, top) ); | |
10977 | rect.SetRight( wxMin(cw, right) ); | |
10978 | rect.SetBottom( wxMin(ch, bottom) ); | |
10979 | ||
10980 | return rect; | |
10981 | } | |
10982 | ||
10983 | // ---------------------------------------------------------------------------- | |
10984 | // grid event classes | |
10985 | // ---------------------------------------------------------------------------- | |
10986 | ||
10987 | IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent ) | |
10988 | ||
10989 | wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, | |
10990 | int row, int col, int x, int y, bool sel, | |
10991 | bool control, bool shift, bool alt, bool meta ) | |
10992 | : wxNotifyEvent( type, id ) | |
10993 | { | |
10994 | m_row = row; | |
10995 | m_col = col; | |
10996 | m_x = x; | |
10997 | m_y = y; | |
10998 | m_selecting = sel; | |
10999 | m_control = control; | |
11000 | m_shift = shift; | |
11001 | m_alt = alt; | |
11002 | m_meta = meta; | |
11003 | ||
11004 | SetEventObject(obj); | |
11005 | } | |
11006 | ||
11007 | ||
11008 | IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent ) | |
11009 | ||
11010 | wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj, | |
11011 | int rowOrCol, int x, int y, | |
11012 | bool control, bool shift, bool alt, bool meta ) | |
11013 | : wxNotifyEvent( type, id ) | |
11014 | { | |
11015 | m_rowOrCol = rowOrCol; | |
11016 | m_x = x; | |
11017 | m_y = y; | |
11018 | m_control = control; | |
11019 | m_shift = shift; | |
11020 | m_alt = alt; | |
11021 | m_meta = meta; | |
11022 | ||
11023 | SetEventObject(obj); | |
11024 | } | |
11025 | ||
11026 | ||
11027 | IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent ) | |
11028 | ||
11029 | wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, | |
11030 | const wxGridCellCoords& topLeft, | |
11031 | const wxGridCellCoords& bottomRight, | |
11032 | bool sel, bool control, | |
11033 | bool shift, bool alt, bool meta ) | |
11034 | : wxNotifyEvent( type, id ) | |
11035 | { | |
11036 | m_topLeft = topLeft; | |
11037 | m_bottomRight = bottomRight; | |
11038 | m_selecting = sel; | |
11039 | m_control = control; | |
11040 | m_shift = shift; | |
11041 | m_alt = alt; | |
11042 | m_meta = meta; | |
11043 | ||
11044 | SetEventObject(obj); | |
11045 | } | |
11046 | ||
11047 | ||
11048 | IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent) | |
11049 | ||
11050 | wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type, | |
11051 | wxObject* obj, int row, | |
11052 | int col, wxControl* ctrl) | |
11053 | : wxCommandEvent(type, id) | |
11054 | { | |
11055 | SetEventObject(obj); | |
11056 | m_row = row; | |
11057 | m_col = col; | |
11058 | m_ctrl = ctrl; | |
11059 | } | |
11060 | ||
11061 | #endif // wxUSE_GRID |