]>
Commit | Line | Data |
---|---|---|
2796cce3 | 1 | /////////////////////////////////////////////////////////////////////////// |
d16c04bb | 2 | // Name: generic/grid.cpp |
f85afd4e MB |
3 | // Purpose: wxGrid and related classes |
4 | // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn) | |
97a9929e | 5 | // Modified by: Robin Dunn, Vadim Zeitlin |
f85afd4e MB |
6 | // Created: 1/08/1999 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
758cbedf VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
f85afd4e MB |
21 | #pragma implementation "grid.h" |
22 | #endif | |
23 | ||
f6bcfd97 | 24 | // For compilers that support precompilatixon, includes "wx/wx.h". |
4d85bcd1 JS |
25 | #include "wx/wxprec.h" |
26 | ||
27 | #include "wx/defs.h" | |
f85afd4e MB |
28 | |
29 | #ifdef __BORLANDC__ | |
30 | #pragma hdrstop | |
31 | #endif | |
32 | ||
27b92ca4 VZ |
33 | #if wxUSE_GRID |
34 | ||
f85afd4e MB |
35 | #ifndef WX_PRECOMP |
36 | #include "wx/utils.h" | |
37 | #include "wx/dcclient.h" | |
38 | #include "wx/settings.h" | |
39 | #include "wx/log.h" | |
508011ce VZ |
40 | #include "wx/textctrl.h" |
41 | #include "wx/checkbox.h" | |
4ee5fc9c | 42 | #include "wx/combobox.h" |
816be743 | 43 | #include "wx/valtext.h" |
f85afd4e MB |
44 | #endif |
45 | ||
cb5df486 | 46 | #include "wx/textfile.h" |
816be743 | 47 | #include "wx/spinctrl.h" |
c4608a8a | 48 | #include "wx/tokenzr.h" |
6d004f67 | 49 | |
07296f0b | 50 | #include "wx/grid.h" |
b5808881 | 51 | #include "wx/generic/gridsel.h" |
07296f0b | 52 | |
0b7e6e7d SN |
53 | #if defined(__WXMOTIF__) |
54 | #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier) | |
c78b3acd | 55 | #else |
0b7e6e7d | 56 | #define WXUNUSED_MOTIF(identifier) identifier |
c78b3acd SN |
57 | #endif |
58 | ||
59 | #if defined(__WXGTK__) | |
60 | #define WXUNUSED_GTK(identifier) WXUNUSED(identifier) | |
61 | #else | |
62 | #define WXUNUSED_GTK(identifier) identifier | |
63 | #endif | |
64 | ||
3f8e5072 JS |
65 | // Required for wxIs... functions |
66 | #include <ctype.h> | |
67 | ||
b99be8fb | 68 | // ---------------------------------------------------------------------------- |
758cbedf | 69 | // array classes |
b99be8fb VZ |
70 | // ---------------------------------------------------------------------------- |
71 | ||
160ba750 VS |
72 | WX_DEFINE_ARRAY_WITH_DECL_NO_PTR(wxGridCellAttr *, wxArrayAttrs, |
73 | class WXDLLIMPEXP_ADV); | |
758cbedf | 74 | |
b99be8fb VZ |
75 | struct wxGridCellWithAttr |
76 | { | |
2e9a6788 VZ |
77 | wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_) |
78 | : coords(row, col), attr(attr_) | |
b99be8fb VZ |
79 | { |
80 | } | |
81 | ||
2e9a6788 VZ |
82 | ~wxGridCellWithAttr() |
83 | { | |
84 | attr->DecRef(); | |
85 | } | |
86 | ||
b99be8fb | 87 | wxGridCellCoords coords; |
2e9a6788 | 88 | wxGridCellAttr *attr; |
22f3361e VZ |
89 | |
90 | // Cannot do this: | |
91 | // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr) | |
92 | // without rewriting the macros, which require a public copy constructor. | |
b99be8fb VZ |
93 | }; |
94 | ||
160ba750 VS |
95 | WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr, wxGridCellWithAttrArray, |
96 | class WXDLLIMPEXP_ADV); | |
b99be8fb VZ |
97 | |
98 | #include "wx/arrimpl.cpp" | |
99 | ||
100 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) | |
101 | WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray) | |
102 | ||
0f442030 RR |
103 | // ---------------------------------------------------------------------------- |
104 | // events | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
2e4df4bf VZ |
107 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK) |
108 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK) | |
109 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK) | |
110 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK) | |
111 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK) | |
112 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK) | |
113 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK) | |
114 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK) | |
115 | DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE) | |
116 | DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE) | |
117 | DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT) | |
118 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE) | |
119 | DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL) | |
120 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN) | |
121 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN) | |
bf7945ce | 122 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED) |
0f442030 | 123 | |
b99be8fb VZ |
124 | // ---------------------------------------------------------------------------- |
125 | // private classes | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
12f190b0 | 128 | class WXDLLIMPEXP_ADV wxGridRowLabelWindow : public wxWindow |
b99be8fb VZ |
129 | { |
130 | public: | |
131 | wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; } | |
132 | wxGridRowLabelWindow( wxGrid *parent, wxWindowID id, | |
133 | const wxPoint &pos, const wxSize &size ); | |
134 | ||
135 | private: | |
136 | wxGrid *m_owner; | |
137 | ||
138 | void OnPaint( wxPaintEvent& event ); | |
139 | void OnMouseEvent( wxMouseEvent& event ); | |
b51c3f27 | 140 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb | 141 | void OnKeyDown( wxKeyEvent& event ); |
f6bcfd97 | 142 | void OnKeyUp( wxKeyEvent& ); |
b99be8fb VZ |
143 | |
144 | DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow) | |
145 | DECLARE_EVENT_TABLE() | |
22f3361e | 146 | DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow) |
b99be8fb VZ |
147 | }; |
148 | ||
149 | ||
12f190b0 | 150 | class WXDLLIMPEXP_ADV wxGridColLabelWindow : public wxWindow |
b99be8fb VZ |
151 | { |
152 | public: | |
153 | wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; } | |
154 | wxGridColLabelWindow( wxGrid *parent, wxWindowID id, | |
155 | const wxPoint &pos, const wxSize &size ); | |
156 | ||
157 | private: | |
158 | wxGrid *m_owner; | |
159 | ||
160 | void OnPaint( wxPaintEvent &event ); | |
161 | void OnMouseEvent( wxMouseEvent& event ); | |
b51c3f27 | 162 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb | 163 | void OnKeyDown( wxKeyEvent& event ); |
f6bcfd97 | 164 | void OnKeyUp( wxKeyEvent& ); |
b99be8fb VZ |
165 | |
166 | DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow) | |
167 | DECLARE_EVENT_TABLE() | |
22f3361e | 168 | DECLARE_NO_COPY_CLASS(wxGridColLabelWindow) |
b99be8fb VZ |
169 | }; |
170 | ||
171 | ||
12f190b0 | 172 | class WXDLLIMPEXP_ADV wxGridCornerLabelWindow : public wxWindow |
b99be8fb VZ |
173 | { |
174 | public: | |
175 | wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; } | |
176 | wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id, | |
177 | const wxPoint &pos, const wxSize &size ); | |
178 | ||
179 | private: | |
180 | wxGrid *m_owner; | |
181 | ||
182 | void OnMouseEvent( wxMouseEvent& event ); | |
b51c3f27 | 183 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb | 184 | void OnKeyDown( wxKeyEvent& event ); |
f6bcfd97 | 185 | void OnKeyUp( wxKeyEvent& ); |
b99be8fb VZ |
186 | void OnPaint( wxPaintEvent& event ); |
187 | ||
188 | DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow) | |
189 | DECLARE_EVENT_TABLE() | |
22f3361e | 190 | DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow) |
b99be8fb VZ |
191 | }; |
192 | ||
12f190b0 | 193 | class WXDLLIMPEXP_ADV wxGridWindow : public wxWindow |
b99be8fb VZ |
194 | { |
195 | public: | |
196 | wxGridWindow() | |
197 | { | |
198 | m_owner = (wxGrid *)NULL; | |
199 | m_rowLabelWin = (wxGridRowLabelWindow *)NULL; | |
200 | m_colLabelWin = (wxGridColLabelWindow *)NULL; | |
201 | } | |
202 | ||
203 | wxGridWindow( wxGrid *parent, | |
204 | wxGridRowLabelWindow *rowLblWin, | |
205 | wxGridColLabelWindow *colLblWin, | |
206 | wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
207 | ~wxGridWindow(); | |
208 | ||
209 | void ScrollWindow( int dx, int dy, const wxRect *rect ); | |
210 | ||
b819b854 JS |
211 | wxGrid* GetOwner() { return m_owner; } |
212 | ||
b99be8fb VZ |
213 | private: |
214 | wxGrid *m_owner; | |
215 | wxGridRowLabelWindow *m_rowLabelWin; | |
216 | wxGridColLabelWindow *m_colLabelWin; | |
217 | ||
218 | void OnPaint( wxPaintEvent &event ); | |
b51c3f27 | 219 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb VZ |
220 | void OnMouseEvent( wxMouseEvent& event ); |
221 | void OnKeyDown( wxKeyEvent& ); | |
f6bcfd97 | 222 | void OnKeyUp( wxKeyEvent& ); |
2796cce3 RD |
223 | void OnEraseBackground( wxEraseEvent& ); |
224 | ||
b99be8fb VZ |
225 | |
226 | DECLARE_DYNAMIC_CLASS(wxGridWindow) | |
227 | DECLARE_EVENT_TABLE() | |
22f3361e | 228 | DECLARE_NO_COPY_CLASS(wxGridWindow) |
b99be8fb VZ |
229 | }; |
230 | ||
2796cce3 RD |
231 | |
232 | ||
233 | class wxGridCellEditorEvtHandler : public wxEvtHandler | |
234 | { | |
235 | public: | |
236 | wxGridCellEditorEvtHandler() | |
237 | : m_grid(0), m_editor(0) | |
238 | { } | |
239 | wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor) | |
240 | : m_grid(grid), m_editor(editor) | |
241 | { } | |
242 | ||
243 | void OnKeyDown(wxKeyEvent& event); | |
fb0de762 | 244 | void OnChar(wxKeyEvent& event); |
2796cce3 RD |
245 | |
246 | private: | |
247 | wxGrid* m_grid; | |
248 | wxGridCellEditor* m_editor; | |
249 | DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler) | |
250 | DECLARE_EVENT_TABLE() | |
22f3361e | 251 | DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler) |
2796cce3 RD |
252 | }; |
253 | ||
254 | ||
255 | IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler ) | |
256 | BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler ) | |
257 | EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown ) | |
fb0de762 | 258 | EVT_CHAR( wxGridCellEditorEvtHandler::OnChar ) |
2796cce3 RD |
259 | END_EVENT_TABLE() |
260 | ||
261 | ||
262 | ||
758cbedf | 263 | // ---------------------------------------------------------------------------- |
b99be8fb | 264 | // the internal data representation used by wxGridCellAttrProvider |
758cbedf VZ |
265 | // ---------------------------------------------------------------------------- |
266 | ||
267 | // this class stores attributes set for cells | |
12f190b0 | 268 | class WXDLLIMPEXP_ADV wxGridCellAttrData |
b99be8fb VZ |
269 | { |
270 | public: | |
2e9a6788 | 271 | void SetAttr(wxGridCellAttr *attr, int row, int col); |
b99be8fb | 272 | wxGridCellAttr *GetAttr(int row, int col) const; |
4d60017a SN |
273 | void UpdateAttrRows( size_t pos, int numRows ); |
274 | void UpdateAttrCols( size_t pos, int numCols ); | |
b99be8fb VZ |
275 | |
276 | private: | |
277 | // searches for the attr for given cell, returns wxNOT_FOUND if not found | |
278 | int FindIndex(int row, int col) const; | |
279 | ||
280 | wxGridCellWithAttrArray m_attrs; | |
281 | }; | |
282 | ||
758cbedf | 283 | // this class stores attributes set for rows or columns |
12f190b0 | 284 | class WXDLLIMPEXP_ADV wxGridRowOrColAttrData |
758cbedf VZ |
285 | { |
286 | public: | |
ee6694a7 VZ |
287 | // empty ctor to suppress warnings |
288 | wxGridRowOrColAttrData() { } | |
758cbedf VZ |
289 | ~wxGridRowOrColAttrData(); |
290 | ||
291 | void SetAttr(wxGridCellAttr *attr, int rowOrCol); | |
292 | wxGridCellAttr *GetAttr(int rowOrCol) const; | |
4d60017a | 293 | void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ); |
758cbedf VZ |
294 | |
295 | private: | |
296 | wxArrayInt m_rowsOrCols; | |
297 | wxArrayAttrs m_attrs; | |
298 | }; | |
299 | ||
300 | // NB: this is just a wrapper around 3 objects: one which stores cell | |
301 | // attributes, and 2 others for row/col ones | |
12f190b0 | 302 | class WXDLLIMPEXP_ADV wxGridCellAttrProviderData |
758cbedf VZ |
303 | { |
304 | public: | |
305 | wxGridCellAttrData m_cellAttrs; | |
306 | wxGridRowOrColAttrData m_rowAttrs, | |
307 | m_colAttrs; | |
308 | }; | |
309 | ||
f2d76237 RD |
310 | |
311 | // ---------------------------------------------------------------------------- | |
312 | // data structures used for the data type registry | |
313 | // ---------------------------------------------------------------------------- | |
314 | ||
b94ae1ea VZ |
315 | struct wxGridDataTypeInfo |
316 | { | |
f2d76237 RD |
317 | wxGridDataTypeInfo(const wxString& typeName, |
318 | wxGridCellRenderer* renderer, | |
319 | wxGridCellEditor* editor) | |
320 | : m_typeName(typeName), m_renderer(renderer), m_editor(editor) | |
321 | { } | |
322 | ||
39bcce60 VZ |
323 | ~wxGridDataTypeInfo() |
324 | { | |
325 | wxSafeDecRef(m_renderer); | |
326 | wxSafeDecRef(m_editor); | |
327 | } | |
f2d76237 RD |
328 | |
329 | wxString m_typeName; | |
330 | wxGridCellRenderer* m_renderer; | |
331 | wxGridCellEditor* m_editor; | |
22f3361e VZ |
332 | |
333 | DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo) | |
f2d76237 RD |
334 | }; |
335 | ||
336 | ||
160ba750 VS |
337 | WX_DEFINE_ARRAY_WITH_DECL_NO_PTR(wxGridDataTypeInfo*, wxGridDataTypeInfoArray, |
338 | class WXDLLIMPEXP_ADV); | |
f2d76237 RD |
339 | |
340 | ||
12f190b0 | 341 | class WXDLLIMPEXP_ADV wxGridTypeRegistry |
b94ae1ea | 342 | { |
f2d76237 | 343 | public: |
c78b3acd | 344 | wxGridTypeRegistry() {} |
f2d76237 | 345 | ~wxGridTypeRegistry(); |
b94ae1ea | 346 | |
f2d76237 RD |
347 | void RegisterDataType(const wxString& typeName, |
348 | wxGridCellRenderer* renderer, | |
349 | wxGridCellEditor* editor); | |
c4608a8a VZ |
350 | |
351 | // find one of already registered data types | |
352 | int FindRegisteredDataType(const wxString& typeName); | |
353 | ||
354 | // try to FindRegisteredDataType(), if this fails and typeName is one of | |
355 | // standard typenames, register it and return its index | |
f2d76237 | 356 | int FindDataType(const wxString& typeName); |
c4608a8a VZ |
357 | |
358 | // try to FindDataType(), if it fails see if it is not one of already | |
359 | // registered data types with some params in which case clone the | |
360 | // registered data type and set params for it | |
361 | int FindOrCloneDataType(const wxString& typeName); | |
362 | ||
f2d76237 RD |
363 | wxGridCellRenderer* GetRenderer(int index); |
364 | wxGridCellEditor* GetEditor(int index); | |
365 | ||
366 | private: | |
367 | wxGridDataTypeInfoArray m_typeinfo; | |
368 | }; | |
369 | ||
b99be8fb VZ |
370 | // ---------------------------------------------------------------------------- |
371 | // conditional compilation | |
372 | // ---------------------------------------------------------------------------- | |
373 | ||
9496deb5 MB |
374 | #ifndef WXGRID_DRAW_LINES |
375 | #define WXGRID_DRAW_LINES 1 | |
796df70a SN |
376 | #endif |
377 | ||
0a976765 VZ |
378 | // ---------------------------------------------------------------------------- |
379 | // globals | |
380 | // ---------------------------------------------------------------------------- | |
381 | ||
382 | //#define DEBUG_ATTR_CACHE | |
383 | #ifdef DEBUG_ATTR_CACHE | |
384 | static size_t gs_nAttrCacheHits = 0; | |
385 | static size_t gs_nAttrCacheMisses = 0; | |
386 | #endif // DEBUG_ATTR_CACHE | |
f85afd4e | 387 | |
43947979 VZ |
388 | // ---------------------------------------------------------------------------- |
389 | // constants | |
390 | // ---------------------------------------------------------------------------- | |
391 | ||
f85afd4e MB |
392 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); |
393 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); | |
394 | ||
f0102d2a | 395 | // scroll line size |
faec5a43 SN |
396 | // TODO: this doesn't work at all, grid cells have different sizes and approx |
397 | // calculations don't work as because of the size mismatch scrollbars | |
398 | // sometimes fail to be shown when they should be or vice versa | |
b51c3f27 RD |
399 | // |
400 | // The scroll bars may be a little flakey once in a while, but that is | |
401 | // surely much less horrible than having scroll lines of only 1!!! | |
402 | // -- Robin | |
97a9929e VZ |
403 | // |
404 | // Well, it's still seriously broken so it might be better but needs | |
405 | // fixing anyhow | |
406 | // -- Vadim | |
407 | static const size_t GRID_SCROLL_LINE_X = 15; // 1; | |
408 | static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X; | |
f85afd4e | 409 | |
43947979 VZ |
410 | // the size of hash tables used a bit everywhere (the max number of elements |
411 | // in these hash tables is the number of rows/columns) | |
412 | static const int GRID_HASH_SIZE = 100; | |
413 | ||
97a9929e VZ |
414 | // ---------------------------------------------------------------------------- |
415 | // private functions | |
416 | // ---------------------------------------------------------------------------- | |
417 | ||
d0eb7e56 | 418 | static inline int GetScrollX(int x) |
97a9929e VZ |
419 | { |
420 | return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X; | |
421 | } | |
422 | ||
d0eb7e56 | 423 | static inline int GetScrollY(int y) |
97a9929e VZ |
424 | { |
425 | return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y; | |
426 | } | |
427 | ||
ab79958a VZ |
428 | // ============================================================================ |
429 | // implementation | |
430 | // ============================================================================ | |
431 | ||
2796cce3 RD |
432 | // ---------------------------------------------------------------------------- |
433 | // wxGridCellEditor | |
434 | // ---------------------------------------------------------------------------- | |
435 | ||
436 | wxGridCellEditor::wxGridCellEditor() | |
437 | { | |
438 | m_control = NULL; | |
1bd71df9 | 439 | m_attr = NULL; |
2796cce3 RD |
440 | } |
441 | ||
442 | ||
443 | wxGridCellEditor::~wxGridCellEditor() | |
444 | { | |
445 | Destroy(); | |
446 | } | |
447 | ||
508011ce VZ |
448 | void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent), |
449 | wxWindowID WXUNUSED(id), | |
450 | wxEvtHandler* evtHandler) | |
451 | { | |
189d0213 | 452 | if ( evtHandler ) |
508011ce VZ |
453 | m_control->PushEventHandler(evtHandler); |
454 | } | |
2796cce3 | 455 | |
189d0213 VZ |
456 | void wxGridCellEditor::PaintBackground(const wxRect& rectCell, |
457 | wxGridCellAttr *attr) | |
458 | { | |
459 | // erase the background because we might not fill the cell | |
460 | wxClientDC dc(m_control->GetParent()); | |
b819b854 JS |
461 | wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow); |
462 | if (gridWindow) | |
463 | gridWindow->GetOwner()->PrepareDC(dc); | |
ef5df12b | 464 | |
189d0213 VZ |
465 | dc.SetPen(*wxTRANSPARENT_PEN); |
466 | dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID)); | |
467 | dc.DrawRectangle(rectCell); | |
468 | ||
469 | // redraw the control we just painted over | |
470 | m_control->Refresh(); | |
471 | } | |
472 | ||
2796cce3 RD |
473 | void wxGridCellEditor::Destroy() |
474 | { | |
508011ce VZ |
475 | if (m_control) |
476 | { | |
b94ae1ea VZ |
477 | m_control->PopEventHandler(TRUE /* delete it*/); |
478 | ||
2796cce3 RD |
479 | m_control->Destroy(); |
480 | m_control = NULL; | |
481 | } | |
482 | } | |
483 | ||
3da93aae | 484 | void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr) |
2796cce3 RD |
485 | { |
486 | wxASSERT_MSG(m_control, | |
487 | wxT("The wxGridCellEditor must be Created first!")); | |
488 | m_control->Show(show); | |
3da93aae VZ |
489 | |
490 | if ( show ) | |
491 | { | |
492 | // set the colours/fonts if we have any | |
493 | if ( attr ) | |
494 | { | |
f2d76237 RD |
495 | m_colFgOld = m_control->GetForegroundColour(); |
496 | m_control->SetForegroundColour(attr->GetTextColour()); | |
3da93aae | 497 | |
f2d76237 RD |
498 | m_colBgOld = m_control->GetBackgroundColour(); |
499 | m_control->SetBackgroundColour(attr->GetBackgroundColour()); | |
3da93aae | 500 | |
f2d76237 RD |
501 | m_fontOld = m_control->GetFont(); |
502 | m_control->SetFont(attr->GetFont()); | |
3da93aae VZ |
503 | |
504 | // can't do anything more in the base class version, the other | |
505 | // attributes may only be used by the derived classes | |
506 | } | |
507 | } | |
508 | else | |
509 | { | |
510 | // restore the standard colours fonts | |
511 | if ( m_colFgOld.Ok() ) | |
512 | { | |
513 | m_control->SetForegroundColour(m_colFgOld); | |
514 | m_colFgOld = wxNullColour; | |
515 | } | |
516 | ||
517 | if ( m_colBgOld.Ok() ) | |
518 | { | |
519 | m_control->SetBackgroundColour(m_colBgOld); | |
520 | m_colBgOld = wxNullColour; | |
521 | } | |
522 | ||
523 | if ( m_fontOld.Ok() ) | |
524 | { | |
525 | m_control->SetFont(m_fontOld); | |
526 | m_fontOld = wxNullFont; | |
527 | } | |
528 | } | |
2796cce3 RD |
529 | } |
530 | ||
531 | void wxGridCellEditor::SetSize(const wxRect& rect) | |
532 | { | |
533 | wxASSERT_MSG(m_control, | |
534 | wxT("The wxGridCellEditor must be Created first!")); | |
28a77bc4 | 535 | m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); |
2796cce3 RD |
536 | } |
537 | ||
538 | void wxGridCellEditor::HandleReturn(wxKeyEvent& event) | |
539 | { | |
540 | event.Skip(); | |
541 | } | |
542 | ||
f6bcfd97 BP |
543 | bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event) |
544 | { | |
545 | // accept the simple key presses, not anything with Ctrl/Alt/Meta | |
a4f7bf58 | 546 | return !(event.ControlDown() || event.AltDown()); |
f6bcfd97 | 547 | } |
2796cce3 | 548 | |
2c9a89e0 RD |
549 | void wxGridCellEditor::StartingKey(wxKeyEvent& event) |
550 | { | |
e195a54c VZ |
551 | event.Skip(); |
552 | } | |
2c9a89e0 | 553 | |
e195a54c VZ |
554 | void wxGridCellEditor::StartingClick() |
555 | { | |
b54ba671 | 556 | } |
2c9a89e0 | 557 | |
3a8c693a VZ |
558 | #if wxUSE_TEXTCTRL |
559 | ||
b54ba671 VZ |
560 | // ---------------------------------------------------------------------------- |
561 | // wxGridCellTextEditor | |
562 | // ---------------------------------------------------------------------------- | |
2c9a89e0 | 563 | |
2796cce3 RD |
564 | wxGridCellTextEditor::wxGridCellTextEditor() |
565 | { | |
c4608a8a | 566 | m_maxChars = 0; |
2796cce3 RD |
567 | } |
568 | ||
569 | void wxGridCellTextEditor::Create(wxWindow* parent, | |
570 | wxWindowID id, | |
2796cce3 RD |
571 | wxEvtHandler* evtHandler) |
572 | { | |
508011ce | 573 | m_control = new wxTextCtrl(parent, id, wxEmptyString, |
2c9a89e0 | 574 | wxDefaultPosition, wxDefaultSize |
2796cce3 | 575 | #if defined(__WXMSW__) |
aaae069f | 576 | , wxTE_PROCESS_TAB | wxTE_AUTO_SCROLL |
2796cce3 | 577 | #endif |
508011ce | 578 | ); |
2796cce3 | 579 | |
46a5010a RD |
580 | // set max length allowed in the textctrl, if the parameter was set |
581 | if (m_maxChars != 0) | |
582 | { | |
583 | ((wxTextCtrl*)m_control)->SetMaxLength(m_maxChars); | |
584 | } | |
c4608a8a | 585 | |
508011ce | 586 | wxGridCellEditor::Create(parent, id, evtHandler); |
2796cce3 RD |
587 | } |
588 | ||
189d0213 VZ |
589 | void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell), |
590 | wxGridCellAttr * WXUNUSED(attr)) | |
591 | { | |
592 | // as we fill the entire client area, don't do anything here to minimize | |
593 | // flicker | |
594 | } | |
2796cce3 | 595 | |
99306db2 VZ |
596 | void wxGridCellTextEditor::SetSize(const wxRect& rectOrig) |
597 | { | |
598 | wxRect rect(rectOrig); | |
599 | ||
600 | // Make the edit control large enough to allow for internal | |
601 | // margins | |
602 | // | |
603 | // TODO: remove this if the text ctrl sizing is improved esp. for | |
604 | // unix | |
605 | // | |
606 | #if defined(__WXGTK__) | |
b0e282b3 RR |
607 | if (rect.x != 0) |
608 | { | |
609 | rect.x += 1; | |
610 | rect.y += 1; | |
611 | rect.width -= 1; | |
612 | rect.height -= 1; | |
613 | } | |
99306db2 | 614 | #else // !GTK |
cb105ad4 | 615 | int extra_x = ( rect.x > 2 )? 2 : 1; |
84912ef8 RD |
616 | |
617 | // MB: treat MSW separately here otherwise the caret doesn't show | |
618 | // when the editor is in the first row. | |
a0948e27 MB |
619 | #if defined(__WXMSW__) |
620 | int extra_y = 2; | |
621 | #else | |
cb105ad4 | 622 | int extra_y = ( rect.y > 2 )? 2 : 1; |
a0948e27 MB |
623 | #endif // MSW |
624 | ||
99306db2 | 625 | #if defined(__WXMOTIF__) |
cb105ad4 SN |
626 | extra_x *= 2; |
627 | extra_y *= 2; | |
99306db2 | 628 | #endif |
cb105ad4 SN |
629 | rect.SetLeft( wxMax(0, rect.x - extra_x) ); |
630 | rect.SetTop( wxMax(0, rect.y - extra_y) ); | |
631 | rect.SetRight( rect.GetRight() + 2*extra_x ); | |
632 | rect.SetBottom( rect.GetBottom() + 2*extra_y ); | |
99306db2 VZ |
633 | #endif // GTK/!GTK |
634 | ||
635 | wxGridCellEditor::SetSize(rect); | |
636 | } | |
637 | ||
3da93aae | 638 | void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid) |
2796cce3 RD |
639 | { |
640 | wxASSERT_MSG(m_control, | |
641 | wxT("The wxGridCellEditor must be Created first!")); | |
642 | ||
643 | m_startValue = grid->GetTable()->GetValue(row, col); | |
816be743 VZ |
644 | |
645 | DoBeginEdit(m_startValue); | |
646 | } | |
647 | ||
648 | void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue) | |
649 | { | |
650 | Text()->SetValue(startValue); | |
b54ba671 | 651 | Text()->SetInsertionPointEnd(); |
505f70de | 652 | Text()->SetSelection(-1,-1); |
b54ba671 | 653 | Text()->SetFocus(); |
2796cce3 RD |
654 | } |
655 | ||
3324d5f5 | 656 | bool wxGridCellTextEditor::EndEdit(int row, int col, |
3da93aae | 657 | wxGrid* grid) |
2796cce3 RD |
658 | { |
659 | wxASSERT_MSG(m_control, | |
660 | wxT("The wxGridCellEditor must be Created first!")); | |
661 | ||
662 | bool changed = FALSE; | |
b54ba671 | 663 | wxString value = Text()->GetValue(); |
2796cce3 RD |
664 | if (value != m_startValue) |
665 | changed = TRUE; | |
666 | ||
667 | if (changed) | |
668 | grid->GetTable()->SetValue(row, col, value); | |
2c9a89e0 | 669 | |
3da93aae | 670 | m_startValue = wxEmptyString; |
b54ba671 | 671 | Text()->SetValue(m_startValue); |
2796cce3 RD |
672 | |
673 | return changed; | |
674 | } | |
675 | ||
676 | ||
677 | void wxGridCellTextEditor::Reset() | |
678 | { | |
679 | wxASSERT_MSG(m_control, | |
680 | wxT("The wxGridCellEditor must be Created first!")); | |
681 | ||
816be743 VZ |
682 | DoReset(m_startValue); |
683 | } | |
684 | ||
685 | void wxGridCellTextEditor::DoReset(const wxString& startValue) | |
686 | { | |
687 | Text()->SetValue(startValue); | |
b54ba671 | 688 | Text()->SetInsertionPointEnd(); |
2796cce3 RD |
689 | } |
690 | ||
f6bcfd97 BP |
691 | bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event) |
692 | { | |
693 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
694 | { | |
695 | int keycode = event.GetKeyCode(); | |
696 | switch ( keycode ) | |
697 | { | |
698 | case WXK_NUMPAD0: | |
699 | case WXK_NUMPAD1: | |
700 | case WXK_NUMPAD2: | |
701 | case WXK_NUMPAD3: | |
702 | case WXK_NUMPAD4: | |
703 | case WXK_NUMPAD5: | |
704 | case WXK_NUMPAD6: | |
705 | case WXK_NUMPAD7: | |
706 | case WXK_NUMPAD8: | |
707 | case WXK_NUMPAD9: | |
708 | case WXK_MULTIPLY: | |
709 | case WXK_NUMPAD_MULTIPLY: | |
710 | case WXK_ADD: | |
711 | case WXK_NUMPAD_ADD: | |
712 | case WXK_SUBTRACT: | |
713 | case WXK_NUMPAD_SUBTRACT: | |
714 | case WXK_DECIMAL: | |
715 | case WXK_NUMPAD_DECIMAL: | |
716 | case WXK_DIVIDE: | |
717 | case WXK_NUMPAD_DIVIDE: | |
718 | return TRUE; | |
719 | ||
720 | default: | |
721 | // accept 8 bit chars too if isprint() agrees | |
1c193821 | 722 | if ( (keycode < 255) && (wxIsprint(keycode)) ) |
f6bcfd97 BP |
723 | return TRUE; |
724 | } | |
725 | } | |
726 | ||
727 | return FALSE; | |
728 | } | |
729 | ||
2c9a89e0 RD |
730 | void wxGridCellTextEditor::StartingKey(wxKeyEvent& event) |
731 | { | |
94af7d45 | 732 | if ( !Text()->EmulateKeyPress(event) ) |
f6bcfd97 BP |
733 | { |
734 | event.Skip(); | |
735 | } | |
b54ba671 | 736 | } |
2c9a89e0 | 737 | |
c78b3acd | 738 | void wxGridCellTextEditor::HandleReturn( wxKeyEvent& |
0b7e6e7d | 739 | WXUNUSED_GTK(WXUNUSED_MOTIF(event)) ) |
2796cce3 RD |
740 | { |
741 | #if defined(__WXMOTIF__) || defined(__WXGTK__) | |
742 | // wxMotif needs a little extra help... | |
6fc0f38f | 743 | size_t pos = (size_t)( Text()->GetInsertionPoint() ); |
b54ba671 | 744 | wxString s( Text()->GetValue() ); |
8dd8f875 | 745 | s = s.Left(pos) + wxT("\n") + s.Mid(pos); |
b54ba671 VZ |
746 | Text()->SetValue(s); |
747 | Text()->SetInsertionPoint( pos ); | |
2796cce3 RD |
748 | #else |
749 | // the other ports can handle a Return key press | |
750 | // | |
751 | event.Skip(); | |
752 | #endif | |
753 | } | |
754 | ||
c4608a8a VZ |
755 | void wxGridCellTextEditor::SetParameters(const wxString& params) |
756 | { | |
757 | if ( !params ) | |
758 | { | |
759 | // reset to default | |
760 | m_maxChars = 0; | |
761 | } | |
762 | else | |
763 | { | |
764 | long tmp; | |
765 | if ( !params.ToLong(&tmp) ) | |
766 | { | |
f6bcfd97 | 767 | wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str()); |
c4608a8a VZ |
768 | } |
769 | else | |
770 | { | |
771 | m_maxChars = (size_t)tmp; | |
772 | } | |
773 | } | |
774 | } | |
775 | ||
73145b0e JS |
776 | // return the value in the text control |
777 | wxString wxGridCellTextEditor::GetValue() const | |
778 | { | |
779 | return Text()->GetValue(); | |
780 | } | |
781 | ||
816be743 VZ |
782 | // ---------------------------------------------------------------------------- |
783 | // wxGridCellNumberEditor | |
784 | // ---------------------------------------------------------------------------- | |
785 | ||
786 | wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max) | |
787 | { | |
788 | m_min = min; | |
789 | m_max = max; | |
790 | } | |
791 | ||
792 | void wxGridCellNumberEditor::Create(wxWindow* parent, | |
793 | wxWindowID id, | |
794 | wxEvtHandler* evtHandler) | |
795 | { | |
796 | if ( HasRange() ) | |
797 | { | |
798 | // create a spin ctrl | |
799 | m_control = new wxSpinCtrl(parent, -1, wxEmptyString, | |
800 | wxDefaultPosition, wxDefaultSize, | |
801 | wxSP_ARROW_KEYS, | |
802 | m_min, m_max); | |
803 | ||
804 | wxGridCellEditor::Create(parent, id, evtHandler); | |
805 | } | |
806 | else | |
807 | { | |
808 | // just a text control | |
809 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
810 | ||
811 | #if wxUSE_VALIDATORS | |
85bc0351 | 812 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); |
816be743 VZ |
813 | #endif // wxUSE_VALIDATORS |
814 | } | |
815 | } | |
816 | ||
817 | void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid) | |
818 | { | |
819 | // first get the value | |
820 | wxGridTableBase *table = grid->GetTable(); | |
821 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
822 | { | |
823 | m_valueOld = table->GetValueAsLong(row, col); | |
824 | } | |
825 | else | |
826 | { | |
8a60ff0a | 827 | m_valueOld = 0; |
a5777624 | 828 | wxString sValue = table->GetValue(row, col); |
8a60ff0a | 829 | if (! sValue.ToLong(&m_valueOld) && ! sValue.IsEmpty()) |
a5777624 RD |
830 | { |
831 | wxFAIL_MSG( _T("this cell doesn't have numeric value") ); | |
832 | return; | |
833 | } | |
816be743 VZ |
834 | } |
835 | ||
836 | if ( HasRange() ) | |
837 | { | |
4a64bee4 | 838 | Spin()->SetValue((int)m_valueOld); |
f6bcfd97 | 839 | Spin()->SetFocus(); |
816be743 VZ |
840 | } |
841 | else | |
842 | { | |
843 | DoBeginEdit(GetString()); | |
844 | } | |
845 | } | |
846 | ||
3324d5f5 | 847 | bool wxGridCellNumberEditor::EndEdit(int row, int col, |
816be743 VZ |
848 | wxGrid* grid) |
849 | { | |
850 | bool changed; | |
8a60ff0a RD |
851 | long value = 0; |
852 | wxString text; | |
816be743 VZ |
853 | |
854 | if ( HasRange() ) | |
855 | { | |
856 | value = Spin()->GetValue(); | |
857 | changed = value != m_valueOld; | |
8a60ff0a RD |
858 | if (changed) |
859 | text = wxString::Format(wxT("%ld"), value); | |
816be743 VZ |
860 | } |
861 | else | |
862 | { | |
8a60ff0a RD |
863 | text = Text()->GetValue(); |
864 | changed = (text.IsEmpty() || text.ToLong(&value)) && (value != m_valueOld); | |
816be743 VZ |
865 | } |
866 | ||
867 | if ( changed ) | |
868 | { | |
a5777624 RD |
869 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER)) |
870 | grid->GetTable()->SetValueAsLong(row, col, value); | |
871 | else | |
8a60ff0a | 872 | grid->GetTable()->SetValue(row, col, text); |
816be743 VZ |
873 | } |
874 | ||
875 | return changed; | |
876 | } | |
877 | ||
878 | void wxGridCellNumberEditor::Reset() | |
879 | { | |
880 | if ( HasRange() ) | |
881 | { | |
4a64bee4 | 882 | Spin()->SetValue((int)m_valueOld); |
816be743 VZ |
883 | } |
884 | else | |
885 | { | |
886 | DoReset(GetString()); | |
887 | } | |
888 | } | |
889 | ||
f6bcfd97 BP |
890 | bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event) |
891 | { | |
892 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
893 | { | |
894 | int keycode = event.GetKeyCode(); | |
895 | switch ( keycode ) | |
896 | { | |
897 | case WXK_NUMPAD0: | |
898 | case WXK_NUMPAD1: | |
899 | case WXK_NUMPAD2: | |
900 | case WXK_NUMPAD3: | |
901 | case WXK_NUMPAD4: | |
902 | case WXK_NUMPAD5: | |
903 | case WXK_NUMPAD6: | |
904 | case WXK_NUMPAD7: | |
905 | case WXK_NUMPAD8: | |
906 | case WXK_NUMPAD9: | |
907 | case WXK_ADD: | |
908 | case WXK_NUMPAD_ADD: | |
909 | case WXK_SUBTRACT: | |
910 | case WXK_NUMPAD_SUBTRACT: | |
911 | case WXK_UP: | |
912 | case WXK_DOWN: | |
913 | return TRUE; | |
914 | ||
915 | default: | |
4676948b | 916 | if ( (keycode < 128) && wxIsdigit(keycode) ) |
f6bcfd97 BP |
917 | return TRUE; |
918 | } | |
919 | } | |
920 | ||
921 | return FALSE; | |
922 | } | |
923 | ||
816be743 VZ |
924 | void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event) |
925 | { | |
926 | if ( !HasRange() ) | |
927 | { | |
12a3f227 | 928 | int keycode = event.GetKeyCode(); |
4676948b | 929 | if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-' |
85d8c319 JS |
930 | || keycode == WXK_NUMPAD0 |
931 | || keycode == WXK_NUMPAD1 | |
932 | || keycode == WXK_NUMPAD2 | |
933 | || keycode == WXK_NUMPAD3 | |
934 | || keycode == WXK_NUMPAD4 | |
935 | || keycode == WXK_NUMPAD5 | |
936 | || keycode == WXK_NUMPAD6 | |
937 | || keycode == WXK_NUMPAD7 | |
938 | || keycode == WXK_NUMPAD8 | |
939 | || keycode == WXK_NUMPAD9 | |
940 | || keycode == WXK_ADD | |
941 | || keycode == WXK_NUMPAD_ADD | |
942 | || keycode == WXK_SUBTRACT | |
943 | || keycode == WXK_NUMPAD_SUBTRACT) | |
816be743 VZ |
944 | { |
945 | wxGridCellTextEditor::StartingKey(event); | |
946 | ||
947 | // skip Skip() below | |
948 | return; | |
949 | } | |
950 | } | |
951 | ||
952 | event.Skip(); | |
953 | } | |
9c4ba614 | 954 | |
c4608a8a VZ |
955 | void wxGridCellNumberEditor::SetParameters(const wxString& params) |
956 | { | |
957 | if ( !params ) | |
958 | { | |
959 | // reset to default | |
960 | m_min = | |
961 | m_max = -1; | |
962 | } | |
963 | else | |
964 | { | |
965 | long tmp; | |
966 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
967 | { | |
968 | m_min = (int)tmp; | |
969 | ||
970 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
971 | { | |
972 | m_max = (int)tmp; | |
973 | ||
974 | // skip the error message below | |
975 | return; | |
976 | } | |
977 | } | |
978 | ||
f6bcfd97 | 979 | wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str()); |
c4608a8a VZ |
980 | } |
981 | } | |
982 | ||
73145b0e JS |
983 | // return the value in the spin control if it is there (the text control otherwise) |
984 | wxString wxGridCellNumberEditor::GetValue() const | |
985 | { | |
986 | wxString s; | |
987 | ||
988 | if( HasRange() ) | |
989 | { | |
14ac4f3c | 990 | long value = Spin()->GetValue(); |
73145b0e JS |
991 | s.Printf(wxT("%ld"), value); |
992 | } | |
993 | else | |
994 | { | |
995 | s = Text()->GetValue(); | |
996 | } | |
997 | return s; | |
998 | } | |
999 | ||
816be743 VZ |
1000 | // ---------------------------------------------------------------------------- |
1001 | // wxGridCellFloatEditor | |
1002 | // ---------------------------------------------------------------------------- | |
1003 | ||
f6bcfd97 BP |
1004 | wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision) |
1005 | { | |
1006 | m_width = width; | |
1007 | m_precision = precision; | |
1008 | } | |
1009 | ||
816be743 VZ |
1010 | void wxGridCellFloatEditor::Create(wxWindow* parent, |
1011 | wxWindowID id, | |
1012 | wxEvtHandler* evtHandler) | |
1013 | { | |
1014 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
1015 | ||
1016 | #if wxUSE_VALIDATORS | |
85bc0351 | 1017 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); |
816be743 VZ |
1018 | #endif // wxUSE_VALIDATORS |
1019 | } | |
1020 | ||
1021 | void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1022 | { | |
1023 | // first get the value | |
1024 | wxGridTableBase *table = grid->GetTable(); | |
1025 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
1026 | { | |
1027 | m_valueOld = table->GetValueAsDouble(row, col); | |
1028 | } | |
1029 | else | |
1030 | { | |
8a60ff0a | 1031 | m_valueOld = 0.0; |
a5777624 | 1032 | wxString sValue = table->GetValue(row, col); |
8a60ff0a | 1033 | if (! sValue.ToDouble(&m_valueOld) && ! sValue.IsEmpty()) |
a5777624 RD |
1034 | { |
1035 | wxFAIL_MSG( _T("this cell doesn't have float value") ); | |
1036 | return; | |
1037 | } | |
816be743 VZ |
1038 | } |
1039 | ||
1040 | DoBeginEdit(GetString()); | |
1041 | } | |
1042 | ||
3324d5f5 | 1043 | bool wxGridCellFloatEditor::EndEdit(int row, int col, |
816be743 VZ |
1044 | wxGrid* grid) |
1045 | { | |
8a60ff0a RD |
1046 | double value = 0.0; |
1047 | wxString text(Text()->GetValue()); | |
1048 | ||
1049 | if ( (text.IsEmpty() || text.ToDouble(&value)) && (value != m_valueOld) ) | |
816be743 | 1050 | { |
a5777624 RD |
1051 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT)) |
1052 | grid->GetTable()->SetValueAsDouble(row, col, value); | |
1053 | else | |
8a60ff0a | 1054 | grid->GetTable()->SetValue(row, col, text); |
816be743 VZ |
1055 | |
1056 | return TRUE; | |
1057 | } | |
ae31cc57 | 1058 | return FALSE; |
816be743 VZ |
1059 | } |
1060 | ||
1061 | void wxGridCellFloatEditor::Reset() | |
1062 | { | |
1063 | DoReset(GetString()); | |
1064 | } | |
1065 | ||
1066 | void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event) | |
1067 | { | |
12a3f227 | 1068 | int keycode = event.GetKeyCode(); |
4676948b | 1069 | if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-' || keycode == '.' |
85d8c319 JS |
1070 | || keycode == WXK_NUMPAD0 |
1071 | || keycode == WXK_NUMPAD1 | |
1072 | || keycode == WXK_NUMPAD2 | |
1073 | || keycode == WXK_NUMPAD3 | |
1074 | || keycode == WXK_NUMPAD4 | |
1075 | || keycode == WXK_NUMPAD5 | |
1076 | || keycode == WXK_NUMPAD6 | |
1077 | || keycode == WXK_NUMPAD7 | |
1078 | || keycode == WXK_NUMPAD8 | |
1079 | || keycode == WXK_NUMPAD9 | |
1080 | || keycode == WXK_ADD | |
1081 | || keycode == WXK_NUMPAD_ADD | |
1082 | || keycode == WXK_SUBTRACT | |
1083 | || keycode == WXK_NUMPAD_SUBTRACT) | |
816be743 VZ |
1084 | { |
1085 | wxGridCellTextEditor::StartingKey(event); | |
1086 | ||
1087 | // skip Skip() below | |
1088 | return; | |
1089 | } | |
1090 | ||
1091 | event.Skip(); | |
1092 | } | |
1093 | ||
f6bcfd97 BP |
1094 | void wxGridCellFloatEditor::SetParameters(const wxString& params) |
1095 | { | |
1096 | if ( !params ) | |
1097 | { | |
1098 | // reset to default | |
1099 | m_width = | |
1100 | m_precision = -1; | |
1101 | } | |
1102 | else | |
1103 | { | |
1104 | long tmp; | |
1105 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
1106 | { | |
1107 | m_width = (int)tmp; | |
1108 | ||
1109 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
1110 | { | |
1111 | m_precision = (int)tmp; | |
1112 | ||
1113 | // skip the error message below | |
1114 | return; | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str()); | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | wxString wxGridCellFloatEditor::GetString() const | |
1123 | { | |
1124 | wxString fmt; | |
1125 | if ( m_width == -1 ) | |
1126 | { | |
1127 | // default width/precision | |
ec53826c | 1128 | fmt = _T("%f"); |
f6bcfd97 BP |
1129 | } |
1130 | else if ( m_precision == -1 ) | |
1131 | { | |
1132 | // default precision | |
ec53826c | 1133 | fmt.Printf(_T("%%%d.f"), m_width); |
f6bcfd97 BP |
1134 | } |
1135 | else | |
1136 | { | |
ec53826c | 1137 | fmt.Printf(_T("%%%d.%df"), m_width, m_precision); |
f6bcfd97 BP |
1138 | } |
1139 | ||
1140 | return wxString::Format(fmt, m_valueOld); | |
1141 | } | |
1142 | ||
1143 | bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event) | |
1144 | { | |
1145 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
1146 | { | |
1147 | int keycode = event.GetKeyCode(); | |
1148 | switch ( keycode ) | |
1149 | { | |
1150 | case WXK_NUMPAD0: | |
1151 | case WXK_NUMPAD1: | |
1152 | case WXK_NUMPAD2: | |
1153 | case WXK_NUMPAD3: | |
1154 | case WXK_NUMPAD4: | |
1155 | case WXK_NUMPAD5: | |
1156 | case WXK_NUMPAD6: | |
1157 | case WXK_NUMPAD7: | |
1158 | case WXK_NUMPAD8: | |
1159 | case WXK_NUMPAD9: | |
1160 | case WXK_ADD: | |
1161 | case WXK_NUMPAD_ADD: | |
1162 | case WXK_SUBTRACT: | |
1163 | case WXK_NUMPAD_SUBTRACT: | |
1164 | case WXK_DECIMAL: | |
1165 | case WXK_NUMPAD_DECIMAL: | |
1166 | return TRUE; | |
1167 | ||
1168 | default: | |
1169 | // additionally accept 'e' as in '1e+6' | |
1170 | if ( (keycode < 128) && | |
4676948b | 1171 | (wxIsdigit(keycode) || tolower(keycode) == 'e') ) |
f6bcfd97 BP |
1172 | return TRUE; |
1173 | } | |
1174 | } | |
1175 | ||
1176 | return FALSE; | |
1177 | } | |
1178 | ||
3a8c693a VZ |
1179 | #endif // wxUSE_TEXTCTRL |
1180 | ||
1181 | #if wxUSE_CHECKBOX | |
1182 | ||
508011ce VZ |
1183 | // ---------------------------------------------------------------------------- |
1184 | // wxGridCellBoolEditor | |
1185 | // ---------------------------------------------------------------------------- | |
1186 | ||
1187 | void wxGridCellBoolEditor::Create(wxWindow* parent, | |
1188 | wxWindowID id, | |
1189 | wxEvtHandler* evtHandler) | |
1190 | { | |
1191 | m_control = new wxCheckBox(parent, id, wxEmptyString, | |
1192 | wxDefaultPosition, wxDefaultSize, | |
1193 | wxNO_BORDER); | |
1194 | ||
1195 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1196 | } | |
1197 | ||
1198 | void wxGridCellBoolEditor::SetSize(const wxRect& r) | |
1199 | { | |
b94ae1ea VZ |
1200 | bool resize = FALSE; |
1201 | wxSize size = m_control->GetSize(); | |
1202 | wxCoord minSize = wxMin(r.width, r.height); | |
1203 | ||
1204 | // check if the checkbox is not too big/small for this cell | |
1205 | wxSize sizeBest = m_control->GetBestSize(); | |
1206 | if ( !(size == sizeBest) ) | |
1207 | { | |
1208 | // reset to default size if it had been made smaller | |
1209 | size = sizeBest; | |
1210 | ||
1211 | resize = TRUE; | |
1212 | } | |
1213 | ||
1214 | if ( size.x >= minSize || size.y >= minSize ) | |
1215 | { | |
1216 | // leave 1 pixel margin | |
1217 | size.x = size.y = minSize - 2; | |
1218 | ||
1219 | resize = TRUE; | |
1220 | } | |
1221 | ||
1222 | if ( resize ) | |
1223 | { | |
1224 | m_control->SetSize(size); | |
1225 | } | |
1226 | ||
508011ce | 1227 | // position it in the centre of the rectangle (TODO: support alignment?) |
508011ce | 1228 | |
b94ae1ea | 1229 | #if defined(__WXGTK__) || defined (__WXMOTIF__) |
508011ce VZ |
1230 | // the checkbox without label still has some space to the right in wxGTK, |
1231 | // so shift it to the right | |
b94ae1ea VZ |
1232 | size.x -= 8; |
1233 | #elif defined(__WXMSW__) | |
a95e38c0 VZ |
1234 | // here too, but in other way |
1235 | size.x += 1; | |
b94ae1ea VZ |
1236 | size.y -= 2; |
1237 | #endif | |
508011ce | 1238 | |
1bd71df9 JS |
1239 | int hAlign = wxALIGN_CENTRE; |
1240 | int vAlign = wxALIGN_CENTRE; | |
1241 | if (GetCellAttr()) | |
1242 | GetCellAttr()->GetAlignment(& hAlign, & vAlign); | |
52d6f640 | 1243 | |
1bd71df9 JS |
1244 | int x = 0, y = 0; |
1245 | if (hAlign == wxALIGN_LEFT) | |
1246 | { | |
1247 | x = r.x + 2; | |
1248 | #ifdef __WXMSW__ | |
1249 | x += 2; | |
52d6f640 | 1250 | #endif |
1bd71df9 JS |
1251 | y = r.y + r.height/2 - size.y/2; |
1252 | } | |
1253 | else if (hAlign == wxALIGN_RIGHT) | |
1254 | { | |
1255 | x = r.x + r.width - size.x - 2; | |
1256 | y = r.y + r.height/2 - size.y/2; | |
1257 | } | |
1258 | else if (hAlign == wxALIGN_CENTRE) | |
1259 | { | |
1260 | x = r.x + r.width/2 - size.x/2; | |
1261 | y = r.y + r.height/2 - size.y/2; | |
1262 | } | |
52d6f640 | 1263 | |
1bd71df9 | 1264 | m_control->Move(x, y); |
508011ce VZ |
1265 | } |
1266 | ||
1267 | void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr) | |
1268 | { | |
99306db2 VZ |
1269 | m_control->Show(show); |
1270 | ||
189d0213 | 1271 | if ( show ) |
508011ce | 1272 | { |
189d0213 VZ |
1273 | wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY; |
1274 | CBox()->SetBackgroundColour(colBg); | |
508011ce | 1275 | } |
508011ce VZ |
1276 | } |
1277 | ||
1278 | void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1279 | { | |
1280 | wxASSERT_MSG(m_control, | |
1281 | wxT("The wxGridCellEditor must be Created first!")); | |
1282 | ||
28a77bc4 | 1283 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) |
f2d76237 RD |
1284 | m_startValue = grid->GetTable()->GetValueAsBool(row, col); |
1285 | else | |
695a3263 MB |
1286 | { |
1287 | wxString cellval( grid->GetTable()->GetValue(row, col) ); | |
8dd8f875 | 1288 | m_startValue = !( !cellval || (cellval == wxT("0")) ); |
695a3263 | 1289 | } |
508011ce VZ |
1290 | CBox()->SetValue(m_startValue); |
1291 | CBox()->SetFocus(); | |
1292 | } | |
1293 | ||
1294 | bool wxGridCellBoolEditor::EndEdit(int row, int col, | |
508011ce VZ |
1295 | wxGrid* grid) |
1296 | { | |
1297 | wxASSERT_MSG(m_control, | |
1298 | wxT("The wxGridCellEditor must be Created first!")); | |
1299 | ||
1300 | bool changed = FALSE; | |
1301 | bool value = CBox()->GetValue(); | |
1302 | if ( value != m_startValue ) | |
1303 | changed = TRUE; | |
1304 | ||
1305 | if ( changed ) | |
1306 | { | |
28a77bc4 | 1307 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) |
f2d76237 RD |
1308 | grid->GetTable()->SetValueAsBool(row, col, value); |
1309 | else | |
1310 | grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString); | |
508011ce VZ |
1311 | } |
1312 | ||
1313 | return changed; | |
1314 | } | |
1315 | ||
1316 | void wxGridCellBoolEditor::Reset() | |
1317 | { | |
1318 | wxASSERT_MSG(m_control, | |
1319 | wxT("The wxGridCellEditor must be Created first!")); | |
1320 | ||
1321 | CBox()->SetValue(m_startValue); | |
1322 | } | |
1323 | ||
e195a54c | 1324 | void wxGridCellBoolEditor::StartingClick() |
508011ce | 1325 | { |
e195a54c | 1326 | CBox()->SetValue(!CBox()->GetValue()); |
508011ce VZ |
1327 | } |
1328 | ||
f6bcfd97 BP |
1329 | bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event) |
1330 | { | |
1331 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
1332 | { | |
1333 | int keycode = event.GetKeyCode(); | |
1334 | switch ( keycode ) | |
1335 | { | |
1336 | case WXK_MULTIPLY: | |
1337 | case WXK_NUMPAD_MULTIPLY: | |
1338 | case WXK_ADD: | |
1339 | case WXK_NUMPAD_ADD: | |
1340 | case WXK_SUBTRACT: | |
1341 | case WXK_NUMPAD_SUBTRACT: | |
1342 | case WXK_SPACE: | |
1343 | case '+': | |
1344 | case '-': | |
1345 | return TRUE; | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | return FALSE; | |
1350 | } | |
04418332 | 1351 | |
73145b0e JS |
1352 | // return the value as "1" for true and the empty string for false |
1353 | wxString wxGridCellBoolEditor::GetValue() const | |
1354 | { | |
1355 | bool bSet = CBox()->GetValue(); | |
04418332 | 1356 | return bSet ? _T("1") : wxEmptyString; |
73145b0e | 1357 | } |
f6bcfd97 | 1358 | |
3a8c693a VZ |
1359 | #endif // wxUSE_CHECKBOX |
1360 | ||
1361 | #if wxUSE_COMBOBOX | |
1362 | ||
4ee5fc9c VZ |
1363 | // ---------------------------------------------------------------------------- |
1364 | // wxGridCellChoiceEditor | |
1365 | // ---------------------------------------------------------------------------- | |
1366 | ||
1367 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count, | |
f6bcfd97 | 1368 | const wxString choices[], |
4ee5fc9c VZ |
1369 | bool allowOthers) |
1370 | : m_allowOthers(allowOthers) | |
1371 | { | |
c4608a8a | 1372 | if ( count ) |
4ee5fc9c | 1373 | { |
c4608a8a VZ |
1374 | m_choices.Alloc(count); |
1375 | for ( size_t n = 0; n < count; n++ ) | |
1376 | { | |
1377 | m_choices.Add(choices[n]); | |
1378 | } | |
4ee5fc9c VZ |
1379 | } |
1380 | } | |
1381 | ||
c4608a8a VZ |
1382 | wxGridCellEditor *wxGridCellChoiceEditor::Clone() const |
1383 | { | |
1384 | wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor; | |
1385 | editor->m_allowOthers = m_allowOthers; | |
1386 | editor->m_choices = m_choices; | |
1387 | ||
1388 | return editor; | |
1389 | } | |
1390 | ||
4ee5fc9c VZ |
1391 | void wxGridCellChoiceEditor::Create(wxWindow* parent, |
1392 | wxWindowID id, | |
1393 | wxEvtHandler* evtHandler) | |
1394 | { | |
1395 | size_t count = m_choices.GetCount(); | |
1396 | wxString *choices = new wxString[count]; | |
1397 | for ( size_t n = 0; n < count; n++ ) | |
1398 | { | |
1399 | choices[n] = m_choices[n]; | |
1400 | } | |
1401 | ||
1402 | m_control = new wxComboBox(parent, id, wxEmptyString, | |
1403 | wxDefaultPosition, wxDefaultSize, | |
1404 | count, choices, | |
1405 | m_allowOthers ? 0 : wxCB_READONLY); | |
1406 | ||
1407 | delete [] choices; | |
1408 | ||
1409 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1410 | } | |
1411 | ||
a5777624 RD |
1412 | void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell, |
1413 | wxGridCellAttr * attr) | |
4ee5fc9c VZ |
1414 | { |
1415 | // as we fill the entire client area, don't do anything here to minimize | |
1416 | // flicker | |
a5777624 RD |
1417 | |
1418 | // TODO: It doesn't actually fill the client area since the height of a | |
1419 | // combo always defaults to the standard... Until someone has time to | |
1420 | // figure out the right rectangle to paint, just do it the normal way... | |
1421 | wxGridCellEditor::PaintBackground(rectCell, attr); | |
4ee5fc9c VZ |
1422 | } |
1423 | ||
1424 | void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1425 | { | |
1426 | wxASSERT_MSG(m_control, | |
1427 | wxT("The wxGridCellEditor must be Created first!")); | |
1428 | ||
1429 | m_startValue = grid->GetTable()->GetValue(row, col); | |
1430 | ||
2b5f62a0 VZ |
1431 | if (m_allowOthers) |
1432 | Combo()->SetValue(m_startValue); | |
1433 | else | |
28a77bc4 | 1434 | { |
2b5f62a0 VZ |
1435 | // find the right position, or default to the first if not found |
1436 | int pos = Combo()->FindString(m_startValue); | |
1437 | if (pos == -1) | |
1438 | pos = 0; | |
1439 | Combo()->SetSelection(pos); | |
28a77bc4 | 1440 | } |
4ee5fc9c VZ |
1441 | Combo()->SetInsertionPointEnd(); |
1442 | Combo()->SetFocus(); | |
1443 | } | |
1444 | ||
28a77bc4 | 1445 | bool wxGridCellChoiceEditor::EndEdit(int row, int col, |
4ee5fc9c VZ |
1446 | wxGrid* grid) |
1447 | { | |
1448 | wxString value = Combo()->GetValue(); | |
1449 | bool changed = value != m_startValue; | |
1450 | ||
1451 | if ( changed ) | |
1452 | grid->GetTable()->SetValue(row, col, value); | |
1453 | ||
1454 | m_startValue = wxEmptyString; | |
2b5f62a0 VZ |
1455 | if (m_allowOthers) |
1456 | Combo()->SetValue(m_startValue); | |
1457 | else | |
1458 | Combo()->SetSelection(0); | |
4ee5fc9c VZ |
1459 | |
1460 | return changed; | |
1461 | } | |
1462 | ||
1463 | void wxGridCellChoiceEditor::Reset() | |
1464 | { | |
1465 | Combo()->SetValue(m_startValue); | |
1466 | Combo()->SetInsertionPointEnd(); | |
1467 | } | |
1468 | ||
c4608a8a VZ |
1469 | void wxGridCellChoiceEditor::SetParameters(const wxString& params) |
1470 | { | |
1471 | if ( !params ) | |
1472 | { | |
1473 | // what can we do? | |
1474 | return; | |
1475 | } | |
1476 | ||
1477 | m_choices.Empty(); | |
1478 | ||
1479 | wxStringTokenizer tk(params, _T(',')); | |
1480 | while ( tk.HasMoreTokens() ) | |
1481 | { | |
1482 | m_choices.Add(tk.GetNextToken()); | |
1483 | } | |
1484 | } | |
1485 | ||
73145b0e JS |
1486 | // return the value in the text control |
1487 | wxString wxGridCellChoiceEditor::GetValue() const | |
1488 | { | |
1489 | return Combo()->GetValue(); | |
1490 | } | |
04418332 | 1491 | |
3a8c693a VZ |
1492 | #endif // wxUSE_COMBOBOX |
1493 | ||
508011ce VZ |
1494 | // ---------------------------------------------------------------------------- |
1495 | // wxGridCellEditorEvtHandler | |
1496 | // ---------------------------------------------------------------------------- | |
2796cce3 RD |
1497 | |
1498 | void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event) | |
1499 | { | |
12a3f227 | 1500 | switch ( event.GetKeyCode() ) |
2796cce3 RD |
1501 | { |
1502 | case WXK_ESCAPE: | |
1503 | m_editor->Reset(); | |
b54ba671 | 1504 | m_grid->DisableCellEditControl(); |
2796cce3 RD |
1505 | break; |
1506 | ||
2c9a89e0 | 1507 | case WXK_TAB: |
b51c3f27 | 1508 | m_grid->GetEventHandler()->ProcessEvent( event ); |
9b4aede2 RD |
1509 | break; |
1510 | ||
2796cce3 | 1511 | case WXK_RETURN: |
faec5a43 SN |
1512 | case WXK_NUMPAD_ENTER: |
1513 | if (!m_grid->GetEventHandler()->ProcessEvent(event)) | |
2796cce3 RD |
1514 | m_editor->HandleReturn(event); |
1515 | break; | |
1516 | ||
2796cce3 RD |
1517 | |
1518 | default: | |
1519 | event.Skip(); | |
1520 | } | |
1521 | } | |
1522 | ||
fb0de762 RD |
1523 | void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event) |
1524 | { | |
12a3f227 | 1525 | switch ( event.GetKeyCode() ) |
fb0de762 RD |
1526 | { |
1527 | case WXK_ESCAPE: | |
1528 | case WXK_TAB: | |
1529 | case WXK_RETURN: | |
a4f7bf58 | 1530 | case WXK_NUMPAD_ENTER: |
fb0de762 RD |
1531 | break; |
1532 | ||
1533 | default: | |
1534 | event.Skip(); | |
1535 | } | |
1536 | } | |
1537 | ||
c4608a8a VZ |
1538 | // ---------------------------------------------------------------------------- |
1539 | // wxGridCellWorker is an (almost) empty common base class for | |
1540 | // wxGridCellRenderer and wxGridCellEditor managing ref counting | |
1541 | // ---------------------------------------------------------------------------- | |
1542 | ||
1543 | void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params)) | |
1544 | { | |
1545 | // nothing to do | |
1546 | } | |
1547 | ||
1548 | wxGridCellWorker::~wxGridCellWorker() | |
1549 | { | |
1550 | } | |
1551 | ||
508011ce VZ |
1552 | // ============================================================================ |
1553 | // renderer classes | |
1554 | // ============================================================================ | |
1555 | ||
ab79958a VZ |
1556 | // ---------------------------------------------------------------------------- |
1557 | // wxGridCellRenderer | |
1558 | // ---------------------------------------------------------------------------- | |
1559 | ||
1560 | void wxGridCellRenderer::Draw(wxGrid& grid, | |
2796cce3 | 1561 | wxGridCellAttr& attr, |
ab79958a VZ |
1562 | wxDC& dc, |
1563 | const wxRect& rect, | |
c78b3acd | 1564 | int WXUNUSED(row), int WXUNUSED(col), |
ab79958a VZ |
1565 | bool isSelected) |
1566 | { | |
1567 | dc.SetBackgroundMode( wxSOLID ); | |
1568 | ||
04418332 | 1569 | // grey out fields if the grid is disabled |
73145b0e | 1570 | if( grid.IsEnabled() ) |
ab79958a | 1571 | { |
73145b0e JS |
1572 | if ( isSelected ) |
1573 | { | |
1574 | dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) ); | |
1575 | } | |
1576 | else | |
1577 | { | |
1578 | dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) ); | |
1579 | } | |
52d6f640 | 1580 | } |
ab79958a VZ |
1581 | else |
1582 | { | |
73145b0e | 1583 | dc.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE), wxSOLID)); |
ab79958a VZ |
1584 | } |
1585 | ||
1586 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
ab79958a VZ |
1587 | dc.DrawRectangle(rect); |
1588 | } | |
1589 | ||
508011ce VZ |
1590 | // ---------------------------------------------------------------------------- |
1591 | // wxGridCellStringRenderer | |
1592 | // ---------------------------------------------------------------------------- | |
1593 | ||
816be743 VZ |
1594 | void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid, |
1595 | wxGridCellAttr& attr, | |
1596 | wxDC& dc, | |
1597 | bool isSelected) | |
ab79958a | 1598 | { |
ab79958a VZ |
1599 | dc.SetBackgroundMode( wxTRANSPARENT ); |
1600 | ||
283b7808 VZ |
1601 | // TODO some special colours for attr.IsReadOnly() case? |
1602 | ||
04418332 | 1603 | // different coloured text when the grid is disabled |
73145b0e | 1604 | if( grid.IsEnabled() ) |
ab79958a | 1605 | { |
73145b0e JS |
1606 | if ( isSelected ) |
1607 | { | |
1608 | dc.SetTextBackground( grid.GetSelectionBackground() ); | |
1609 | dc.SetTextForeground( grid.GetSelectionForeground() ); | |
1610 | } | |
1611 | else | |
1612 | { | |
1613 | dc.SetTextBackground( attr.GetBackgroundColour() ); | |
1614 | dc.SetTextForeground( attr.GetTextColour() ); | |
1615 | } | |
ab79958a VZ |
1616 | } |
1617 | else | |
1618 | { | |
73145b0e JS |
1619 | dc.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE)); |
1620 | dc.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT)); | |
ab79958a | 1621 | } |
816be743 | 1622 | |
2796cce3 | 1623 | dc.SetFont( attr.GetFont() ); |
816be743 VZ |
1624 | } |
1625 | ||
65e4e78e VZ |
1626 | wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr, |
1627 | wxDC& dc, | |
1628 | const wxString& text) | |
1629 | { | |
f6bcfd97 | 1630 | wxCoord x = 0, y = 0, max_x = 0; |
65e4e78e | 1631 | dc.SetFont(attr.GetFont()); |
f6bcfd97 BP |
1632 | wxStringTokenizer tk(text, _T('\n')); |
1633 | while ( tk.HasMoreTokens() ) | |
1634 | { | |
1635 | dc.GetTextExtent(tk.GetNextToken(), &x, &y); | |
1636 | max_x = wxMax(max_x, x); | |
1637 | } | |
1638 | ||
1639 | y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines. | |
65e4e78e | 1640 | |
f6bcfd97 | 1641 | return wxSize(max_x, y); |
65e4e78e VZ |
1642 | } |
1643 | ||
1644 | wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid, | |
1645 | wxGridCellAttr& attr, | |
1646 | wxDC& dc, | |
1647 | int row, int col) | |
1648 | { | |
1649 | return DoGetBestSize(attr, dc, grid.GetCellValue(row, col)); | |
1650 | } | |
1651 | ||
816be743 VZ |
1652 | void wxGridCellStringRenderer::Draw(wxGrid& grid, |
1653 | wxGridCellAttr& attr, | |
1654 | wxDC& dc, | |
1655 | const wxRect& rectCell, | |
1656 | int row, int col, | |
1657 | bool isSelected) | |
1658 | { | |
27f35b66 | 1659 | wxRect rect = rectCell; |
dc1f566f SN |
1660 | rect.Inflate(-1); |
1661 | ||
1662 | // erase only this cells background, overflow cells should have been erased | |
1663 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1664 | ||
1665 | int hAlign, vAlign; | |
1666 | attr.GetAlignment(&hAlign, &vAlign); | |
27f35b66 | 1667 | |
39b80349 SN |
1668 | int overflowCols = 0; |
1669 | ||
27f35b66 SN |
1670 | if (attr.GetOverflow()) |
1671 | { | |
1672 | int cols = grid.GetNumberCols(); | |
1673 | int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth(); | |
1674 | int cell_rows, cell_cols; | |
1675 | attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <=0 | |
1676 | if ((best_width > rectCell.width) && (col < cols) && grid.GetTable()) | |
1677 | { | |
1678 | int i, c_cols, c_rows; | |
1679 | for (i = col+cell_cols; i < cols; i++) | |
1680 | { | |
2b5f62a0 | 1681 | bool is_empty = TRUE; |
39b80349 | 1682 | for (int j=row; j<row+cell_rows; j++) |
27f35b66 | 1683 | { |
39b80349 | 1684 | // check w/ anchor cell for multicell block |
ef4d6ce8 | 1685 | grid.GetCellSize(j, i, &c_rows, &c_cols); |
39b80349 | 1686 | if (c_rows > 0) c_rows = 0; |
ef4d6ce8 | 1687 | if (!grid.GetTable()->IsEmptyCell(j+c_rows, i)) |
39b80349 | 1688 | { |
2b5f62a0 | 1689 | is_empty = FALSE; |
ef4d6ce8 | 1690 | break; |
39b80349 | 1691 | } |
27f35b66 | 1692 | } |
39b80349 SN |
1693 | if (is_empty) |
1694 | rect.width += grid.GetColSize(i); | |
dc1f566f SN |
1695 | else |
1696 | { | |
1697 | i--; | |
1698 | break; | |
1699 | } | |
39b80349 | 1700 | if (rect.width >= best_width) break; |
2b5f62a0 | 1701 | } |
39b80349 | 1702 | overflowCols = i - col - cell_cols + 1; |
2b5f62a0 | 1703 | if (overflowCols >= cols) overflowCols = cols - 1; |
39b80349 | 1704 | } |
27f35b66 | 1705 | |
dc1f566f SN |
1706 | if (overflowCols > 0) // redraw overflow cells w/ proper hilight |
1707 | { | |
39b80349 | 1708 | hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned |
2b5f62a0 | 1709 | wxRect clip = rect; |
39b80349 | 1710 | clip.x += rectCell.width; |
dc1f566f SN |
1711 | // draw each overflow cell individually |
1712 | int col_end = col+cell_cols+overflowCols; | |
1713 | if (col_end >= grid.GetNumberCols()) | |
2b5f62a0 | 1714 | col_end = grid.GetNumberCols() - 1; |
dc1f566f SN |
1715 | for (int i = col+cell_cols; i <= col_end; i++) |
1716 | { | |
dc1f566f SN |
1717 | clip.width = grid.GetColSize(i) - 1; |
1718 | dc.DestroyClippingRegion(); | |
1719 | dc.SetClippingRegion(clip); | |
39b80349 SN |
1720 | |
1721 | SetTextColoursAndFont(grid, attr, dc, | |
2b5f62a0 | 1722 | grid.IsInSelection(row,i)); |
39b80349 | 1723 | |
dc1f566f | 1724 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), |
2b5f62a0 | 1725 | rect, hAlign, vAlign); |
39b80349 | 1726 | clip.x += grid.GetColSize(i) - 1; |
dc1f566f | 1727 | } |
ab79958a | 1728 | |
39b80349 | 1729 | rect = rectCell; |
2b5f62a0 | 1730 | rect.Inflate(-1); |
dc1f566f | 1731 | rect.width++; |
39b80349 | 1732 | dc.DestroyClippingRegion(); |
dc1f566f | 1733 | } |
39b80349 | 1734 | } |
ab79958a | 1735 | |
dc1f566f SN |
1736 | // now we only have to draw the text |
1737 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
39b80349 | 1738 | |
ab79958a VZ |
1739 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), |
1740 | rect, hAlign, vAlign); | |
1741 | } | |
1742 | ||
65e4e78e VZ |
1743 | // ---------------------------------------------------------------------------- |
1744 | // wxGridCellNumberRenderer | |
1745 | // ---------------------------------------------------------------------------- | |
1746 | ||
1747 | wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col) | |
1748 | { | |
1749 | wxGridTableBase *table = grid.GetTable(); | |
1750 | wxString text; | |
1751 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
1752 | { | |
1753 | text.Printf(_T("%ld"), table->GetValueAsLong(row, col)); | |
1754 | } | |
9c4ba614 VZ |
1755 | else |
1756 | { | |
1757 | text = table->GetValue(row, col); | |
1758 | } | |
65e4e78e VZ |
1759 | |
1760 | return text; | |
1761 | } | |
1762 | ||
816be743 VZ |
1763 | void wxGridCellNumberRenderer::Draw(wxGrid& grid, |
1764 | wxGridCellAttr& attr, | |
1765 | wxDC& dc, | |
1766 | const wxRect& rectCell, | |
1767 | int row, int col, | |
1768 | bool isSelected) | |
1769 | { | |
1770 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1771 | ||
1772 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1773 | ||
1774 | // draw the text right aligned by default | |
1775 | int hAlign, vAlign; | |
1776 | attr.GetAlignment(&hAlign, &vAlign); | |
4c7277db | 1777 | hAlign = wxALIGN_RIGHT; |
816be743 VZ |
1778 | |
1779 | wxRect rect = rectCell; | |
1780 | rect.Inflate(-1); | |
1781 | ||
65e4e78e VZ |
1782 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
1783 | } | |
816be743 | 1784 | |
65e4e78e VZ |
1785 | wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid, |
1786 | wxGridCellAttr& attr, | |
1787 | wxDC& dc, | |
1788 | int row, int col) | |
1789 | { | |
1790 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
816be743 VZ |
1791 | } |
1792 | ||
1793 | // ---------------------------------------------------------------------------- | |
1794 | // wxGridCellFloatRenderer | |
1795 | // ---------------------------------------------------------------------------- | |
1796 | ||
1797 | wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision) | |
1798 | { | |
1799 | SetWidth(width); | |
1800 | SetPrecision(precision); | |
1801 | } | |
1802 | ||
e72b4213 VZ |
1803 | wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const |
1804 | { | |
1805 | wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer; | |
1806 | renderer->m_width = m_width; | |
1807 | renderer->m_precision = m_precision; | |
1808 | renderer->m_format = m_format; | |
1809 | ||
1810 | return renderer; | |
1811 | } | |
1812 | ||
65e4e78e VZ |
1813 | wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col) |
1814 | { | |
1815 | wxGridTableBase *table = grid.GetTable(); | |
0b190b0f VZ |
1816 | |
1817 | bool hasDouble; | |
1818 | double val; | |
65e4e78e VZ |
1819 | wxString text; |
1820 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
1821 | { | |
0b190b0f VZ |
1822 | val = table->GetValueAsDouble(row, col); |
1823 | hasDouble = TRUE; | |
65e4e78e | 1824 | } |
9c4ba614 VZ |
1825 | else |
1826 | { | |
1827 | text = table->GetValue(row, col); | |
0b190b0f | 1828 | hasDouble = text.ToDouble(&val); |
9c4ba614 | 1829 | } |
65e4e78e | 1830 | |
0b190b0f VZ |
1831 | if ( hasDouble ) |
1832 | { | |
1833 | if ( !m_format ) | |
1834 | { | |
1835 | if ( m_width == -1 ) | |
1836 | { | |
19d7140e VZ |
1837 | if ( m_precision == -1 ) |
1838 | { | |
2b5f62a0 VZ |
1839 | // default width/precision |
1840 | m_format = _T("%f"); | |
1841 | } | |
19d7140e VZ |
1842 | else |
1843 | { | |
1844 | m_format.Printf(_T("%%.%df"), m_precision); | |
1845 | } | |
0b190b0f VZ |
1846 | } |
1847 | else if ( m_precision == -1 ) | |
1848 | { | |
1849 | // default precision | |
1850 | m_format.Printf(_T("%%%d.f"), m_width); | |
1851 | } | |
1852 | else | |
1853 | { | |
1854 | m_format.Printf(_T("%%%d.%df"), m_width, m_precision); | |
1855 | } | |
1856 | } | |
1857 | ||
1858 | text.Printf(m_format, val); | |
19d7140e | 1859 | |
0b190b0f VZ |
1860 | } |
1861 | //else: text already contains the string | |
1862 | ||
65e4e78e VZ |
1863 | return text; |
1864 | } | |
1865 | ||
816be743 VZ |
1866 | void wxGridCellFloatRenderer::Draw(wxGrid& grid, |
1867 | wxGridCellAttr& attr, | |
1868 | wxDC& dc, | |
1869 | const wxRect& rectCell, | |
1870 | int row, int col, | |
1871 | bool isSelected) | |
1872 | { | |
1873 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1874 | ||
1875 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1876 | ||
1877 | // draw the text right aligned by default | |
1878 | int hAlign, vAlign; | |
1879 | attr.GetAlignment(&hAlign, &vAlign); | |
4c7277db | 1880 | hAlign = wxALIGN_RIGHT; |
816be743 VZ |
1881 | |
1882 | wxRect rect = rectCell; | |
1883 | rect.Inflate(-1); | |
1884 | ||
65e4e78e VZ |
1885 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
1886 | } | |
816be743 | 1887 | |
65e4e78e VZ |
1888 | wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid, |
1889 | wxGridCellAttr& attr, | |
1890 | wxDC& dc, | |
1891 | int row, int col) | |
1892 | { | |
1893 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
816be743 VZ |
1894 | } |
1895 | ||
0b190b0f VZ |
1896 | void wxGridCellFloatRenderer::SetParameters(const wxString& params) |
1897 | { | |
0b190b0f VZ |
1898 | if ( !params ) |
1899 | { | |
1900 | // reset to defaults | |
1901 | SetWidth(-1); | |
1902 | SetPrecision(-1); | |
1903 | } | |
1904 | else | |
1905 | { | |
1906 | wxString tmp = params.BeforeFirst(_T(',')); | |
1907 | if ( !!tmp ) | |
1908 | { | |
1909 | long width; | |
19d7140e | 1910 | if ( tmp.ToLong(&width) ) |
0b190b0f | 1911 | { |
19d7140e | 1912 | SetWidth((int)width); |
0b190b0f VZ |
1913 | } |
1914 | else | |
1915 | { | |
19d7140e VZ |
1916 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str()); |
1917 | } | |
0b190b0f | 1918 | |
19d7140e | 1919 | } |
0b190b0f VZ |
1920 | tmp = params.AfterFirst(_T(',')); |
1921 | if ( !!tmp ) | |
1922 | { | |
1923 | long precision; | |
19d7140e | 1924 | if ( tmp.ToLong(&precision) ) |
0b190b0f | 1925 | { |
19d7140e | 1926 | SetPrecision((int)precision); |
0b190b0f VZ |
1927 | } |
1928 | else | |
1929 | { | |
19d7140e | 1930 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str()); |
0b190b0f VZ |
1931 | } |
1932 | ||
0b190b0f VZ |
1933 | } |
1934 | } | |
1935 | } | |
1936 | ||
19d7140e | 1937 | |
508011ce VZ |
1938 | // ---------------------------------------------------------------------------- |
1939 | // wxGridCellBoolRenderer | |
1940 | // ---------------------------------------------------------------------------- | |
1941 | ||
65e4e78e | 1942 | wxSize wxGridCellBoolRenderer::ms_sizeCheckMark; |
508011ce | 1943 | |
b94ae1ea VZ |
1944 | // FIXME these checkbox size calculations are really ugly... |
1945 | ||
65e4e78e | 1946 | // between checkmark and box |
a95e38c0 | 1947 | static const wxCoord wxGRID_CHECKMARK_MARGIN = 2; |
508011ce | 1948 | |
65e4e78e VZ |
1949 | wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid, |
1950 | wxGridCellAttr& WXUNUSED(attr), | |
1951 | wxDC& WXUNUSED(dc), | |
1952 | int WXUNUSED(row), | |
1953 | int WXUNUSED(col)) | |
1954 | { | |
1955 | // compute it only once (no locks for MT safeness in GUI thread...) | |
1956 | if ( !ms_sizeCheckMark.x ) | |
297da4ba | 1957 | { |
65e4e78e VZ |
1958 | // get checkbox size |
1959 | wxCoord checkSize = 0; | |
297da4ba VZ |
1960 | wxCheckBox *checkbox = new wxCheckBox(&grid, -1, wxEmptyString); |
1961 | wxSize size = checkbox->GetBestSize(); | |
a95e38c0 | 1962 | checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN; |
297da4ba | 1963 | |
65e4e78e | 1964 | // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result |
69d8f612 | 1965 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
65e4e78e | 1966 | checkSize -= size.y / 2; |
297da4ba VZ |
1967 | #endif |
1968 | ||
1969 | delete checkbox; | |
65e4e78e VZ |
1970 | |
1971 | ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize; | |
297da4ba VZ |
1972 | } |
1973 | ||
65e4e78e VZ |
1974 | return ms_sizeCheckMark; |
1975 | } | |
1976 | ||
1977 | void wxGridCellBoolRenderer::Draw(wxGrid& grid, | |
1978 | wxGridCellAttr& attr, | |
1979 | wxDC& dc, | |
1980 | const wxRect& rect, | |
1981 | int row, int col, | |
1982 | bool isSelected) | |
1983 | { | |
1984 | wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); | |
1985 | ||
297da4ba | 1986 | // draw a check mark in the centre (ignoring alignment - TODO) |
65e4e78e | 1987 | wxSize size = GetBestSize(grid, attr, dc, row, col); |
b94ae1ea VZ |
1988 | |
1989 | // don't draw outside the cell | |
1990 | wxCoord minSize = wxMin(rect.width, rect.height); | |
1991 | if ( size.x >= minSize || size.y >= minSize ) | |
1992 | { | |
1993 | // and even leave (at least) 1 pixel margin | |
1994 | size.x = size.y = minSize - 2; | |
1995 | } | |
1996 | ||
1997 | // draw a border around checkmark | |
1bd71df9 JS |
1998 | int vAlign, hAlign; |
1999 | attr.GetAlignment(& hAlign, &vAlign); | |
52d6f640 | 2000 | |
a95e38c0 | 2001 | wxRect rectBorder; |
1bd71df9 JS |
2002 | if (hAlign == wxALIGN_CENTRE) |
2003 | { | |
2004 | rectBorder.x = rect.x + rect.width/2 - size.x/2; | |
2005 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2006 | rectBorder.width = size.x; | |
2007 | rectBorder.height = size.y; | |
2008 | } | |
2009 | else if (hAlign == wxALIGN_LEFT) | |
2010 | { | |
2011 | rectBorder.x = rect.x + 2; | |
2012 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2013 | rectBorder.width = size.x; | |
52d6f640 | 2014 | rectBorder.height = size.y; |
1bd71df9 JS |
2015 | } |
2016 | else if (hAlign == wxALIGN_RIGHT) | |
2017 | { | |
2018 | rectBorder.x = rect.x + rect.width - size.x - 2; | |
2019 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2020 | rectBorder.width = size.x; | |
52d6f640 | 2021 | rectBorder.height = size.y; |
1bd71df9 | 2022 | } |
b94ae1ea | 2023 | |
f2d76237 | 2024 | bool value; |
b94ae1ea | 2025 | if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) |
f2d76237 RD |
2026 | value = grid.GetTable()->GetValueAsBool(row, col); |
2027 | else | |
695a3263 MB |
2028 | { |
2029 | wxString cellval( grid.GetTable()->GetValue(row, col) ); | |
8dd8f875 | 2030 | value = !( !cellval || (cellval == wxT("0")) ); |
695a3263 | 2031 | } |
f2d76237 RD |
2032 | |
2033 | if ( value ) | |
508011ce | 2034 | { |
a95e38c0 VZ |
2035 | wxRect rectMark = rectBorder; |
2036 | #ifdef __WXMSW__ | |
2037 | // MSW DrawCheckMark() is weird (and should probably be changed...) | |
2038 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2); | |
2039 | rectMark.x++; | |
2040 | rectMark.y++; | |
2041 | #else // !MSW | |
2042 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN); | |
2043 | #endif // MSW/!MSW | |
2044 | ||
508011ce VZ |
2045 | dc.SetTextForeground(attr.GetTextColour()); |
2046 | dc.DrawCheckMark(rectMark); | |
2047 | } | |
a95e38c0 VZ |
2048 | |
2049 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
2050 | dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID)); | |
2051 | dc.DrawRectangle(rectBorder); | |
508011ce VZ |
2052 | } |
2053 | ||
2796cce3 RD |
2054 | // ---------------------------------------------------------------------------- |
2055 | // wxGridCellAttr | |
2056 | // ---------------------------------------------------------------------------- | |
2057 | ||
1df4050d VZ |
2058 | void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) |
2059 | { | |
2060 | m_nRef = 1; | |
2061 | ||
2062 | m_isReadOnly = Unset; | |
2063 | ||
2064 | m_renderer = NULL; | |
2065 | m_editor = NULL; | |
2066 | ||
2067 | m_attrkind = wxGridCellAttr::Cell; | |
2068 | ||
27f35b66 | 2069 | m_sizeRows = m_sizeCols = 1; |
b63fce94 | 2070 | m_overflow = UnsetOverflow; |
27f35b66 | 2071 | |
1df4050d VZ |
2072 | SetDefAttr(attrDefault); |
2073 | } | |
2074 | ||
39bcce60 | 2075 | wxGridCellAttr *wxGridCellAttr::Clone() const |
a68c1246 | 2076 | { |
1df4050d VZ |
2077 | wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr); |
2078 | ||
a68c1246 VZ |
2079 | if ( HasTextColour() ) |
2080 | attr->SetTextColour(GetTextColour()); | |
2081 | if ( HasBackgroundColour() ) | |
2082 | attr->SetBackgroundColour(GetBackgroundColour()); | |
2083 | if ( HasFont() ) | |
2084 | attr->SetFont(GetFont()); | |
2085 | if ( HasAlignment() ) | |
2086 | attr->SetAlignment(m_hAlign, m_vAlign); | |
2087 | ||
27f35b66 SN |
2088 | attr->SetSize( m_sizeRows, m_sizeCols ); |
2089 | ||
a68c1246 VZ |
2090 | if ( m_renderer ) |
2091 | { | |
2092 | attr->SetRenderer(m_renderer); | |
39bcce60 | 2093 | m_renderer->IncRef(); |
a68c1246 VZ |
2094 | } |
2095 | if ( m_editor ) | |
2096 | { | |
2097 | attr->SetEditor(m_editor); | |
39bcce60 | 2098 | m_editor->IncRef(); |
a68c1246 VZ |
2099 | } |
2100 | ||
2101 | if ( IsReadOnly() ) | |
2102 | attr->SetReadOnly(); | |
2103 | ||
19d7140e VZ |
2104 | attr->SetKind( m_attrkind ); |
2105 | ||
a68c1246 VZ |
2106 | return attr; |
2107 | } | |
2108 | ||
19d7140e VZ |
2109 | void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) |
2110 | { | |
2111 | if ( !HasTextColour() && mergefrom->HasTextColour() ) | |
2112 | SetTextColour(mergefrom->GetTextColour()); | |
2113 | if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() ) | |
2114 | SetBackgroundColour(mergefrom->GetBackgroundColour()); | |
2115 | if ( !HasFont() && mergefrom->HasFont() ) | |
2116 | SetFont(mergefrom->GetFont()); | |
7e48d7d9 | 2117 | if ( !HasAlignment() && mergefrom->HasAlignment() ){ |
19d7140e VZ |
2118 | int hAlign, vAlign; |
2119 | mergefrom->GetAlignment( &hAlign, &vAlign); | |
2120 | SetAlignment(hAlign, vAlign); | |
2121 | } | |
2122 | ||
27f35b66 SN |
2123 | mergefrom->GetSize( &m_sizeRows, &m_sizeCols ); |
2124 | ||
19d7140e VZ |
2125 | // Directly access member functions as GetRender/Editor don't just return |
2126 | // m_renderer/m_editor | |
2127 | // | |
2128 | // Maybe add support for merge of Render and Editor? | |
2129 | if (!HasRenderer() && mergefrom->HasRenderer() ) | |
bf7945ce | 2130 | { |
19d7140e VZ |
2131 | m_renderer = mergefrom->m_renderer; |
2132 | m_renderer->IncRef(); | |
2133 | } | |
2134 | if ( !HasEditor() && mergefrom->HasEditor() ) | |
2135 | { | |
2136 | m_editor = mergefrom->m_editor; | |
2137 | m_editor->IncRef(); | |
2138 | } | |
2139 | if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) | |
2140 | SetReadOnly(mergefrom->IsReadOnly()); | |
2141 | ||
ef5df12b | 2142 | if (!HasOverflowMode() && mergefrom->HasOverflowMode() ) |
ff699386 | 2143 | SetOverflow(mergefrom->GetOverflow()); |
ef5df12b | 2144 | |
19d7140e VZ |
2145 | SetDefAttr(mergefrom->m_defGridAttr); |
2146 | } | |
2147 | ||
27f35b66 SN |
2148 | void wxGridCellAttr::SetSize(int num_rows, int num_cols) |
2149 | { | |
2150 | // The size of a cell is normally 1,1 | |
2151 | ||
2152 | // If this cell is larger (2,2) then this is the top left cell | |
2153 | // the other cells that will be covered (lower right cells) must be | |
2154 | // set to negative or zero values such that | |
2155 | // row + num_rows of the covered cell points to the larger cell (this cell) | |
2156 | // same goes for the col + num_cols. | |
2157 | ||
2158 | // Size of 0,0 is NOT valid, neither is <=0 and any positive value | |
2159 | ||
2160 | wxASSERT_MSG( (!((num_rows>0)&&(num_cols<=0)) || | |
2161 | !((num_rows<=0)&&(num_cols>0)) || | |
2162 | !((num_rows==0)&&(num_cols==0))), | |
2163 | wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values")); | |
2164 | ||
2165 | m_sizeRows = num_rows; | |
2166 | m_sizeCols = num_cols; | |
2167 | } | |
2168 | ||
2796cce3 RD |
2169 | const wxColour& wxGridCellAttr::GetTextColour() const |
2170 | { | |
2171 | if (HasTextColour()) | |
508011ce | 2172 | { |
2796cce3 | 2173 | return m_colText; |
508011ce | 2174 | } |
0926b2fc | 2175 | else if (m_defGridAttr && m_defGridAttr != this) |
508011ce | 2176 | { |
2796cce3 | 2177 | return m_defGridAttr->GetTextColour(); |
508011ce VZ |
2178 | } |
2179 | else | |
2180 | { | |
2796cce3 RD |
2181 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2182 | return wxNullColour; | |
2183 | } | |
2184 | } | |
2185 | ||
2186 | ||
2187 | const wxColour& wxGridCellAttr::GetBackgroundColour() const | |
2188 | { | |
2189 | if (HasBackgroundColour()) | |
2190 | return m_colBack; | |
0926b2fc | 2191 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2192 | return m_defGridAttr->GetBackgroundColour(); |
508011ce VZ |
2193 | else |
2194 | { | |
2796cce3 RD |
2195 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2196 | return wxNullColour; | |
2197 | } | |
2198 | } | |
2199 | ||
2200 | ||
2201 | const wxFont& wxGridCellAttr::GetFont() const | |
2202 | { | |
2203 | if (HasFont()) | |
2204 | return m_font; | |
0926b2fc | 2205 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2206 | return m_defGridAttr->GetFont(); |
508011ce VZ |
2207 | else |
2208 | { | |
2796cce3 RD |
2209 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2210 | return wxNullFont; | |
2211 | } | |
2212 | } | |
2213 | ||
2214 | ||
2215 | void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const | |
2216 | { | |
508011ce VZ |
2217 | if (HasAlignment()) |
2218 | { | |
2796cce3 RD |
2219 | if ( hAlign ) *hAlign = m_hAlign; |
2220 | if ( vAlign ) *vAlign = m_vAlign; | |
2221 | } | |
0926b2fc | 2222 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2223 | m_defGridAttr->GetAlignment(hAlign, vAlign); |
508011ce VZ |
2224 | else |
2225 | { | |
2796cce3 RD |
2226 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2227 | } | |
2228 | } | |
2229 | ||
27f35b66 SN |
2230 | void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const |
2231 | { | |
2232 | if ( num_rows ) *num_rows = m_sizeRows; | |
2233 | if ( num_cols ) *num_cols = m_sizeCols; | |
2234 | } | |
2796cce3 | 2235 | |
f2d76237 | 2236 | // GetRenderer and GetEditor use a slightly different decision path about |
28a77bc4 RD |
2237 | // which attribute to use. If a non-default attr object has one then it is |
2238 | // used, otherwise the default editor or renderer is fetched from the grid and | |
2239 | // used. It should be the default for the data type of the cell. If it is | |
2240 | // NULL (because the table has a type that the grid does not have in its | |
2241 | // registry,) then the grid's default editor or renderer is used. | |
2242 | ||
2243 | wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const | |
2244 | { | |
3cf883a2 | 2245 | wxGridCellRenderer *renderer; |
28a77bc4 | 2246 | |
3cf883a2 | 2247 | if ( m_renderer && this != m_defGridAttr ) |
0b190b0f | 2248 | { |
3cf883a2 VZ |
2249 | // use the cells renderer if it has one |
2250 | renderer = m_renderer; | |
2251 | renderer->IncRef(); | |
0b190b0f | 2252 | } |
3cf883a2 | 2253 | else // no non default cell renderer |
0b190b0f | 2254 | { |
3cf883a2 VZ |
2255 | // get default renderer for the data type |
2256 | if ( grid ) | |
2257 | { | |
2258 | // GetDefaultRendererForCell() will do IncRef() for us | |
2259 | renderer = grid->GetDefaultRendererForCell(row, col); | |
2260 | } | |
2261 | else | |
2262 | { | |
2263 | renderer = NULL; | |
2264 | } | |
0b190b0f | 2265 | |
3cf883a2 VZ |
2266 | if ( !renderer ) |
2267 | { | |
0926b2fc | 2268 | if (m_defGridAttr && this != m_defGridAttr ) |
3cf883a2 VZ |
2269 | { |
2270 | // if we still don't have one then use the grid default | |
2271 | // (no need for IncRef() here neither) | |
2272 | renderer = m_defGridAttr->GetRenderer(NULL, 0, 0); | |
2273 | } | |
2274 | else // default grid attr | |
2275 | { | |
2276 | // use m_renderer which we had decided not to use initially | |
2277 | renderer = m_renderer; | |
2278 | if ( renderer ) | |
2279 | renderer->IncRef(); | |
2280 | } | |
2281 | } | |
0b190b0f | 2282 | } |
28a77bc4 | 2283 | |
3cf883a2 VZ |
2284 | // we're supposed to always find something |
2285 | wxASSERT_MSG(renderer, wxT("Missing default cell renderer")); | |
28a77bc4 RD |
2286 | |
2287 | return renderer; | |
2796cce3 RD |
2288 | } |
2289 | ||
3cf883a2 | 2290 | // same as above, except for s/renderer/editor/g |
28a77bc4 | 2291 | wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const |
07296f0b | 2292 | { |
3cf883a2 | 2293 | wxGridCellEditor *editor; |
0b190b0f | 2294 | |
3cf883a2 | 2295 | if ( m_editor && this != m_defGridAttr ) |
0b190b0f | 2296 | { |
3cf883a2 VZ |
2297 | // use the cells editor if it has one |
2298 | editor = m_editor; | |
2299 | editor->IncRef(); | |
0b190b0f | 2300 | } |
3cf883a2 | 2301 | else // no non default cell editor |
0b190b0f | 2302 | { |
3cf883a2 VZ |
2303 | // get default editor for the data type |
2304 | if ( grid ) | |
2305 | { | |
2306 | // GetDefaultEditorForCell() will do IncRef() for us | |
2307 | editor = grid->GetDefaultEditorForCell(row, col); | |
2308 | } | |
2309 | else | |
2310 | { | |
2311 | editor = NULL; | |
2312 | } | |
2313 | ||
2314 | if ( !editor ) | |
2315 | { | |
0926b2fc | 2316 | if ( m_defGridAttr && this != m_defGridAttr ) |
3cf883a2 VZ |
2317 | { |
2318 | // if we still don't have one then use the grid default | |
2319 | // (no need for IncRef() here neither) | |
2320 | editor = m_defGridAttr->GetEditor(NULL, 0, 0); | |
2321 | } | |
2322 | else // default grid attr | |
2323 | { | |
2324 | // use m_editor which we had decided not to use initially | |
2325 | editor = m_editor; | |
2326 | if ( editor ) | |
2327 | editor->IncRef(); | |
2328 | } | |
2329 | } | |
0b190b0f | 2330 | } |
28a77bc4 | 2331 | |
3cf883a2 VZ |
2332 | // we're supposed to always find something |
2333 | wxASSERT_MSG(editor, wxT("Missing default cell editor")); | |
2334 | ||
28a77bc4 | 2335 | return editor; |
07296f0b RD |
2336 | } |
2337 | ||
b99be8fb | 2338 | // ---------------------------------------------------------------------------- |
758cbedf | 2339 | // wxGridCellAttrData |
b99be8fb VZ |
2340 | // ---------------------------------------------------------------------------- |
2341 | ||
758cbedf | 2342 | void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) |
b99be8fb VZ |
2343 | { |
2344 | int n = FindIndex(row, col); | |
2345 | if ( n == wxNOT_FOUND ) | |
2346 | { | |
2347 | // add the attribute | |
2348 | m_attrs.Add(new wxGridCellWithAttr(row, col, attr)); | |
2349 | } | |
2350 | else | |
2351 | { | |
6f36917b VZ |
2352 | // free the old attribute |
2353 | m_attrs[(size_t)n].attr->DecRef(); | |
2354 | ||
b99be8fb VZ |
2355 | if ( attr ) |
2356 | { | |
2357 | // change the attribute | |
2e9a6788 | 2358 | m_attrs[(size_t)n].attr = attr; |
b99be8fb VZ |
2359 | } |
2360 | else | |
2361 | { | |
2362 | // remove this attribute | |
2363 | m_attrs.RemoveAt((size_t)n); | |
2364 | } | |
2365 | } | |
b99be8fb VZ |
2366 | } |
2367 | ||
758cbedf | 2368 | wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const |
b99be8fb VZ |
2369 | { |
2370 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2371 | ||
2372 | int n = FindIndex(row, col); | |
2373 | if ( n != wxNOT_FOUND ) | |
2374 | { | |
2e9a6788 VZ |
2375 | attr = m_attrs[(size_t)n].attr; |
2376 | attr->IncRef(); | |
b99be8fb VZ |
2377 | } |
2378 | ||
2379 | return attr; | |
2380 | } | |
2381 | ||
4d60017a SN |
2382 | void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) |
2383 | { | |
2384 | size_t count = m_attrs.GetCount(); | |
2385 | for ( size_t n = 0; n < count; n++ ) | |
2386 | { | |
2387 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
2388 | wxCoord row = coords.GetRow(); |
2389 | if ((size_t)row >= pos) | |
2390 | { | |
2391 | if (numRows > 0) | |
2392 | { | |
2393 | // If rows inserted, include row counter where necessary | |
2394 | coords.SetRow(row + numRows); | |
2395 | } | |
2396 | else if (numRows < 0) | |
2397 | { | |
2398 | // If rows deleted ... | |
2399 | if ((size_t)row >= pos - numRows) | |
2400 | { | |
2401 | // ...either decrement row counter (if row still exists)... | |
2402 | coords.SetRow(row + numRows); | |
2403 | } | |
2404 | else | |
2405 | { | |
2406 | // ...or remove the attribute | |
2407 | m_attrs.RemoveAt((size_t)n); | |
2408 | n--; count--; | |
2409 | } | |
2410 | } | |
4d60017a SN |
2411 | } |
2412 | } | |
2413 | } | |
2414 | ||
2415 | void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) | |
2416 | { | |
2417 | size_t count = m_attrs.GetCount(); | |
2418 | for ( size_t n = 0; n < count; n++ ) | |
2419 | { | |
2420 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
2421 | wxCoord col = coords.GetCol(); |
2422 | if ( (size_t)col >= pos ) | |
2423 | { | |
2424 | if ( numCols > 0 ) | |
2425 | { | |
2426 | // If rows inserted, include row counter where necessary | |
2427 | coords.SetCol(col + numCols); | |
2428 | } | |
2429 | else if (numCols < 0) | |
2430 | { | |
2431 | // If rows deleted ... | |
2432 | if ((size_t)col >= pos - numCols) | |
2433 | { | |
2434 | // ...either decrement row counter (if row still exists)... | |
2435 | coords.SetCol(col + numCols); | |
2436 | } | |
2437 | else | |
2438 | { | |
2439 | // ...or remove the attribute | |
2440 | m_attrs.RemoveAt((size_t)n); | |
2441 | n--; count--; | |
2442 | } | |
2443 | } | |
4d60017a SN |
2444 | } |
2445 | } | |
2446 | } | |
2447 | ||
758cbedf | 2448 | int wxGridCellAttrData::FindIndex(int row, int col) const |
b99be8fb VZ |
2449 | { |
2450 | size_t count = m_attrs.GetCount(); | |
2451 | for ( size_t n = 0; n < count; n++ ) | |
2452 | { | |
2453 | const wxGridCellCoords& coords = m_attrs[n].coords; | |
2454 | if ( (coords.GetRow() == row) && (coords.GetCol() == col) ) | |
2455 | { | |
2456 | return n; | |
2457 | } | |
2458 | } | |
2459 | ||
2460 | return wxNOT_FOUND; | |
2461 | } | |
2462 | ||
758cbedf VZ |
2463 | // ---------------------------------------------------------------------------- |
2464 | // wxGridRowOrColAttrData | |
2465 | // ---------------------------------------------------------------------------- | |
2466 | ||
2467 | wxGridRowOrColAttrData::~wxGridRowOrColAttrData() | |
2468 | { | |
2469 | size_t count = m_attrs.Count(); | |
2470 | for ( size_t n = 0; n < count; n++ ) | |
2471 | { | |
2472 | m_attrs[n]->DecRef(); | |
2473 | } | |
2474 | } | |
2475 | ||
2476 | wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const | |
2477 | { | |
2478 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2479 | ||
2480 | int n = m_rowsOrCols.Index(rowOrCol); | |
2481 | if ( n != wxNOT_FOUND ) | |
2482 | { | |
2483 | attr = m_attrs[(size_t)n]; | |
2484 | attr->IncRef(); | |
2485 | } | |
2486 | ||
2487 | return attr; | |
2488 | } | |
2489 | ||
2490 | void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) | |
2491 | { | |
a95e38c0 VZ |
2492 | int i = m_rowsOrCols.Index(rowOrCol); |
2493 | if ( i == wxNOT_FOUND ) | |
758cbedf VZ |
2494 | { |
2495 | // add the attribute | |
2496 | m_rowsOrCols.Add(rowOrCol); | |
2497 | m_attrs.Add(attr); | |
2498 | } | |
2499 | else | |
2500 | { | |
a95e38c0 | 2501 | size_t n = (size_t)i; |
758cbedf VZ |
2502 | if ( attr ) |
2503 | { | |
2504 | // change the attribute | |
a95e38c0 VZ |
2505 | m_attrs[n]->DecRef(); |
2506 | m_attrs[n] = attr; | |
758cbedf VZ |
2507 | } |
2508 | else | |
2509 | { | |
2510 | // remove this attribute | |
a95e38c0 VZ |
2511 | m_attrs[n]->DecRef(); |
2512 | m_rowsOrCols.RemoveAt(n); | |
2513 | m_attrs.RemoveAt(n); | |
758cbedf VZ |
2514 | } |
2515 | } | |
2516 | } | |
2517 | ||
4d60017a SN |
2518 | void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) |
2519 | { | |
2520 | size_t count = m_attrs.GetCount(); | |
2521 | for ( size_t n = 0; n < count; n++ ) | |
2522 | { | |
2523 | int & rowOrCol = m_rowsOrCols[n]; | |
d1c0b4f9 VZ |
2524 | if ( (size_t)rowOrCol >= pos ) |
2525 | { | |
2526 | if ( numRowsOrCols > 0 ) | |
2527 | { | |
2528 | // If rows inserted, include row counter where necessary | |
2529 | rowOrCol += numRowsOrCols; | |
2530 | } | |
2531 | else if ( numRowsOrCols < 0) | |
2532 | { | |
2533 | // If rows deleted, either decrement row counter (if row still exists) | |
2534 | if ((size_t)rowOrCol >= pos - numRowsOrCols) | |
2535 | rowOrCol += numRowsOrCols; | |
2536 | else | |
2537 | { | |
2538 | m_rowsOrCols.RemoveAt((size_t)n); | |
2539 | m_attrs.RemoveAt((size_t)n); | |
2540 | n--; count--; | |
2541 | } | |
2542 | } | |
4d60017a SN |
2543 | } |
2544 | } | |
2545 | } | |
2546 | ||
b99be8fb VZ |
2547 | // ---------------------------------------------------------------------------- |
2548 | // wxGridCellAttrProvider | |
2549 | // ---------------------------------------------------------------------------- | |
2550 | ||
2551 | wxGridCellAttrProvider::wxGridCellAttrProvider() | |
2552 | { | |
2553 | m_data = (wxGridCellAttrProviderData *)NULL; | |
2554 | } | |
2555 | ||
2556 | wxGridCellAttrProvider::~wxGridCellAttrProvider() | |
2557 | { | |
2558 | delete m_data; | |
2559 | } | |
2560 | ||
2561 | void wxGridCellAttrProvider::InitData() | |
2562 | { | |
2563 | m_data = new wxGridCellAttrProviderData; | |
2564 | } | |
2565 | ||
19d7140e VZ |
2566 | wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col, |
2567 | wxGridCellAttr::wxAttrKind kind ) const | |
b99be8fb | 2568 | { |
758cbedf VZ |
2569 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
2570 | if ( m_data ) | |
2571 | { | |
19d7140e | 2572 | switch(kind) |
758cbedf | 2573 | { |
19d7140e VZ |
2574 | case (wxGridCellAttr::Any): |
2575 | //Get cached merge attributes. | |
2576 | // Currenlty not used as no cache implemented as not mutiable | |
2577 | // attr = m_data->m_mergeAttr.GetAttr(row, col); | |
2578 | if(!attr) | |
2579 | { | |
2580 | //Basicaly implement old version. | |
2581 | //Also check merge cache, so we don't have to re-merge every time.. | |
2582 | wxGridCellAttr *attrcell = (wxGridCellAttr *)NULL, | |
2583 | *attrrow = (wxGridCellAttr *)NULL, | |
bf7945ce RD |
2584 | *attrcol = (wxGridCellAttr *)NULL; |
2585 | ||
19d7140e VZ |
2586 | attrcell = m_data->m_cellAttrs.GetAttr(row, col); |
2587 | attrcol = m_data->m_colAttrs.GetAttr(col); | |
2588 | attrrow = m_data->m_rowAttrs.GetAttr(row); | |
2589 | ||
bf7945ce | 2590 | if((attrcell != attrrow) && (attrrow !=attrcol) && (attrcell != attrcol)){ |
19d7140e VZ |
2591 | // Two or move are non NULL |
2592 | attr = new wxGridCellAttr; | |
2593 | attr->SetKind(wxGridCellAttr::Merged); | |
2594 | ||
bf7945ce | 2595 | //Order important.. |
19d7140e VZ |
2596 | if(attrcell){ |
2597 | attr->MergeWith(attrcell); | |
2598 | attrcell->DecRef(); | |
2599 | } | |
2600 | if(attrcol){ | |
2601 | attr->MergeWith(attrcol); | |
2602 | attrcol->DecRef(); | |
2603 | } | |
2604 | if(attrrow){ | |
2605 | attr->MergeWith(attrrow); | |
2606 | attrrow->DecRef(); | |
2607 | } | |
2608 | //store merge attr if cache implemented | |
2609 | //attr->IncRef(); | |
2610 | //m_data->m_mergeAttr.SetAttr(attr, row, col); | |
2611 | } | |
2612 | else | |
758cbedf | 2613 | { |
19d7140e VZ |
2614 | // one or none is non null return it or null. |
2615 | if(attrrow) attr = attrrow; | |
2616 | if(attrcol) attr = attrcol; | |
bf7945ce | 2617 | if(attrcell) attr = attrcell; |
19d7140e VZ |
2618 | } |
2619 | } | |
2620 | break; | |
2621 | case (wxGridCellAttr::Cell): | |
2622 | attr = m_data->m_cellAttrs.GetAttr(row, col); | |
2623 | break; | |
2624 | case (wxGridCellAttr::Col): | |
2625 | attr = m_data->m_colAttrs.GetAttr(col); | |
2626 | break; | |
2627 | case (wxGridCellAttr::Row): | |
758cbedf | 2628 | attr = m_data->m_rowAttrs.GetAttr(row); |
19d7140e VZ |
2629 | break; |
2630 | default: | |
2631 | // unused as yet... | |
2632 | // (wxGridCellAttr::Default): | |
2633 | // (wxGridCellAttr::Merged): | |
2634 | break; | |
758cbedf VZ |
2635 | } |
2636 | } | |
758cbedf | 2637 | return attr; |
b99be8fb VZ |
2638 | } |
2639 | ||
2e9a6788 | 2640 | void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, |
b99be8fb VZ |
2641 | int row, int col) |
2642 | { | |
2643 | if ( !m_data ) | |
2644 | InitData(); | |
2645 | ||
758cbedf VZ |
2646 | m_data->m_cellAttrs.SetAttr(attr, row, col); |
2647 | } | |
2648 | ||
2649 | void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row) | |
2650 | { | |
2651 | if ( !m_data ) | |
2652 | InitData(); | |
2653 | ||
2654 | m_data->m_rowAttrs.SetAttr(attr, row); | |
2655 | } | |
2656 | ||
2657 | void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) | |
2658 | { | |
2659 | if ( !m_data ) | |
2660 | InitData(); | |
2661 | ||
2662 | m_data->m_colAttrs.SetAttr(attr, col); | |
b99be8fb VZ |
2663 | } |
2664 | ||
4d60017a SN |
2665 | void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) |
2666 | { | |
2667 | if ( m_data ) | |
2668 | { | |
2669 | m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); | |
2670 | ||
d1c0b4f9 | 2671 | m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); |
4d60017a SN |
2672 | } |
2673 | } | |
2674 | ||
2675 | void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) | |
2676 | { | |
2677 | if ( m_data ) | |
2678 | { | |
2679 | m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); | |
2680 | ||
d1c0b4f9 | 2681 | m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); |
4d60017a SN |
2682 | } |
2683 | } | |
2684 | ||
f2d76237 RD |
2685 | // ---------------------------------------------------------------------------- |
2686 | // wxGridTypeRegistry | |
2687 | // ---------------------------------------------------------------------------- | |
2688 | ||
2689 | wxGridTypeRegistry::~wxGridTypeRegistry() | |
2690 | { | |
b94ae1ea VZ |
2691 | size_t count = m_typeinfo.Count(); |
2692 | for ( size_t i = 0; i < count; i++ ) | |
f2d76237 RD |
2693 | delete m_typeinfo[i]; |
2694 | } | |
2695 | ||
2696 | ||
2697 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, | |
2698 | wxGridCellRenderer* renderer, | |
2699 | wxGridCellEditor* editor) | |
2700 | { | |
f2d76237 RD |
2701 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); |
2702 | ||
2703 | // is it already registered? | |
c4608a8a | 2704 | int loc = FindRegisteredDataType(typeName); |
39bcce60 VZ |
2705 | if ( loc != wxNOT_FOUND ) |
2706 | { | |
f2d76237 RD |
2707 | delete m_typeinfo[loc]; |
2708 | m_typeinfo[loc] = info; | |
2709 | } | |
39bcce60 VZ |
2710 | else |
2711 | { | |
f2d76237 RD |
2712 | m_typeinfo.Add(info); |
2713 | } | |
2714 | } | |
2715 | ||
c4608a8a VZ |
2716 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) |
2717 | { | |
2718 | size_t count = m_typeinfo.GetCount(); | |
2719 | for ( size_t i = 0; i < count; i++ ) | |
2720 | { | |
2721 | if ( typeName == m_typeinfo[i]->m_typeName ) | |
2722 | { | |
2723 | return i; | |
2724 | } | |
2725 | } | |
2726 | ||
2727 | return wxNOT_FOUND; | |
2728 | } | |
2729 | ||
f2d76237 RD |
2730 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) |
2731 | { | |
c4608a8a VZ |
2732 | int index = FindRegisteredDataType(typeName); |
2733 | if ( index == wxNOT_FOUND ) | |
2734 | { | |
2735 | // check whether this is one of the standard ones, in which case | |
2736 | // register it "on the fly" | |
3a8c693a | 2737 | #if wxUSE_TEXTCTRL |
c4608a8a VZ |
2738 | if ( typeName == wxGRID_VALUE_STRING ) |
2739 | { | |
2740 | RegisterDataType(wxGRID_VALUE_STRING, | |
2741 | new wxGridCellStringRenderer, | |
2742 | new wxGridCellTextEditor); | |
3a8c693a VZ |
2743 | } else |
2744 | #endif // wxUSE_TEXTCTRL | |
2745 | #if wxUSE_CHECKBOX | |
2746 | if ( typeName == wxGRID_VALUE_BOOL ) | |
c4608a8a VZ |
2747 | { |
2748 | RegisterDataType(wxGRID_VALUE_BOOL, | |
2749 | new wxGridCellBoolRenderer, | |
2750 | new wxGridCellBoolEditor); | |
3a8c693a VZ |
2751 | } else |
2752 | #endif // wxUSE_CHECKBOX | |
2753 | #if wxUSE_TEXTCTRL | |
2754 | if ( typeName == wxGRID_VALUE_NUMBER ) | |
c4608a8a VZ |
2755 | { |
2756 | RegisterDataType(wxGRID_VALUE_NUMBER, | |
2757 | new wxGridCellNumberRenderer, | |
2758 | new wxGridCellNumberEditor); | |
2759 | } | |
2760 | else if ( typeName == wxGRID_VALUE_FLOAT ) | |
2761 | { | |
2762 | RegisterDataType(wxGRID_VALUE_FLOAT, | |
2763 | new wxGridCellFloatRenderer, | |
2764 | new wxGridCellFloatEditor); | |
3a8c693a VZ |
2765 | } else |
2766 | #endif // wxUSE_TEXTCTRL | |
2767 | #if wxUSE_COMBOBOX | |
2768 | if ( typeName == wxGRID_VALUE_CHOICE ) | |
c4608a8a VZ |
2769 | { |
2770 | RegisterDataType(wxGRID_VALUE_CHOICE, | |
2771 | new wxGridCellStringRenderer, | |
2772 | new wxGridCellChoiceEditor); | |
3a8c693a VZ |
2773 | } else |
2774 | #endif // wxUSE_COMBOBOX | |
c4608a8a VZ |
2775 | { |
2776 | return wxNOT_FOUND; | |
2777 | } | |
f2d76237 | 2778 | |
c4608a8a VZ |
2779 | // we get here only if just added the entry for this type, so return |
2780 | // the last index | |
2781 | index = m_typeinfo.GetCount() - 1; | |
2782 | } | |
2783 | ||
2784 | return index; | |
2785 | } | |
2786 | ||
2787 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) | |
2788 | { | |
2789 | int index = FindDataType(typeName); | |
2790 | if ( index == wxNOT_FOUND ) | |
2791 | { | |
2792 | // the first part of the typename is the "real" type, anything after ':' | |
2793 | // are the parameters for the renderer | |
2794 | index = FindDataType(typeName.BeforeFirst(_T(':'))); | |
2795 | if ( index == wxNOT_FOUND ) | |
2796 | { | |
2797 | return wxNOT_FOUND; | |
f2d76237 | 2798 | } |
c4608a8a VZ |
2799 | |
2800 | wxGridCellRenderer *renderer = GetRenderer(index); | |
2801 | wxGridCellRenderer *rendererOld = renderer; | |
2802 | renderer = renderer->Clone(); | |
2803 | rendererOld->DecRef(); | |
2804 | ||
2805 | wxGridCellEditor *editor = GetEditor(index); | |
2806 | wxGridCellEditor *editorOld = editor; | |
2807 | editor = editor->Clone(); | |
2808 | editorOld->DecRef(); | |
2809 | ||
2810 | // do it even if there are no parameters to reset them to defaults | |
2811 | wxString params = typeName.AfterFirst(_T(':')); | |
2812 | renderer->SetParameters(params); | |
2813 | editor->SetParameters(params); | |
2814 | ||
2815 | // register the new typename | |
c4608a8a VZ |
2816 | RegisterDataType(typeName, renderer, editor); |
2817 | ||
2818 | // we just registered it, it's the last one | |
2819 | index = m_typeinfo.GetCount() - 1; | |
f2d76237 RD |
2820 | } |
2821 | ||
c4608a8a | 2822 | return index; |
f2d76237 RD |
2823 | } |
2824 | ||
2825 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) | |
2826 | { | |
2827 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; | |
faec5a43 SN |
2828 | if (renderer) |
2829 | renderer->IncRef(); | |
f2d76237 RD |
2830 | return renderer; |
2831 | } | |
2832 | ||
0b190b0f | 2833 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) |
f2d76237 RD |
2834 | { |
2835 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; | |
faec5a43 SN |
2836 | if (editor) |
2837 | editor->IncRef(); | |
f2d76237 RD |
2838 | return editor; |
2839 | } | |
2840 | ||
758cbedf VZ |
2841 | // ---------------------------------------------------------------------------- |
2842 | // wxGridTableBase | |
2843 | // ---------------------------------------------------------------------------- | |
2844 | ||
f85afd4e MB |
2845 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) |
2846 | ||
2847 | ||
2848 | wxGridTableBase::wxGridTableBase() | |
f85afd4e MB |
2849 | { |
2850 | m_view = (wxGrid *) NULL; | |
b99be8fb | 2851 | m_attrProvider = (wxGridCellAttrProvider *) NULL; |
f85afd4e MB |
2852 | } |
2853 | ||
2854 | wxGridTableBase::~wxGridTableBase() | |
2855 | { | |
b99be8fb VZ |
2856 | delete m_attrProvider; |
2857 | } | |
2858 | ||
2859 | void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider) | |
2860 | { | |
2861 | delete m_attrProvider; | |
2862 | m_attrProvider = attrProvider; | |
f85afd4e MB |
2863 | } |
2864 | ||
f2d76237 RD |
2865 | bool wxGridTableBase::CanHaveAttributes() |
2866 | { | |
2867 | if ( ! GetAttrProvider() ) | |
2868 | { | |
2869 | // use the default attr provider by default | |
2870 | SetAttrProvider(new wxGridCellAttrProvider); | |
2871 | } | |
2872 | return TRUE; | |
2873 | } | |
2874 | ||
19d7140e | 2875 | wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) |
b99be8fb VZ |
2876 | { |
2877 | if ( m_attrProvider ) | |
19d7140e | 2878 | return m_attrProvider->GetAttr(row, col, kind); |
b99be8fb VZ |
2879 | else |
2880 | return (wxGridCellAttr *)NULL; | |
2881 | } | |
2882 | ||
758cbedf | 2883 | void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) |
b99be8fb VZ |
2884 | { |
2885 | if ( m_attrProvider ) | |
2886 | { | |
19d7140e | 2887 | attr->SetKind(wxGridCellAttr::Cell); |
b99be8fb VZ |
2888 | m_attrProvider->SetAttr(attr, row, col); |
2889 | } | |
2890 | else | |
2891 | { | |
2892 | // as we take ownership of the pointer and don't store it, we must | |
2893 | // free it now | |
39bcce60 | 2894 | wxSafeDecRef(attr); |
b99be8fb VZ |
2895 | } |
2896 | } | |
2897 | ||
758cbedf VZ |
2898 | void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) |
2899 | { | |
2900 | if ( m_attrProvider ) | |
2901 | { | |
19d7140e | 2902 | attr->SetKind(wxGridCellAttr::Row); |
758cbedf VZ |
2903 | m_attrProvider->SetRowAttr(attr, row); |
2904 | } | |
2905 | else | |
2906 | { | |
2907 | // as we take ownership of the pointer and don't store it, we must | |
2908 | // free it now | |
39bcce60 | 2909 | wxSafeDecRef(attr); |
758cbedf VZ |
2910 | } |
2911 | } | |
2912 | ||
2913 | void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) | |
2914 | { | |
2915 | if ( m_attrProvider ) | |
2916 | { | |
19d7140e | 2917 | attr->SetKind(wxGridCellAttr::Col); |
758cbedf VZ |
2918 | m_attrProvider->SetColAttr(attr, col); |
2919 | } | |
2920 | else | |
2921 | { | |
2922 | // as we take ownership of the pointer and don't store it, we must | |
2923 | // free it now | |
39bcce60 | 2924 | wxSafeDecRef(attr); |
758cbedf VZ |
2925 | } |
2926 | } | |
2927 | ||
aa5e1f75 SN |
2928 | bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos), |
2929 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 2930 | { |
f6bcfd97 | 2931 | wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); |
8f177c8e | 2932 | |
f85afd4e MB |
2933 | return FALSE; |
2934 | } | |
2935 | ||
aa5e1f75 | 2936 | bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) ) |
f85afd4e | 2937 | { |
f6bcfd97 | 2938 | wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); |
8f177c8e | 2939 | |
f85afd4e MB |
2940 | return FALSE; |
2941 | } | |
2942 | ||
aa5e1f75 SN |
2943 | bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos), |
2944 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 2945 | { |
f6bcfd97 | 2946 | wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); |
8f177c8e | 2947 | |
f85afd4e MB |
2948 | return FALSE; |
2949 | } | |
2950 | ||
aa5e1f75 SN |
2951 | bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos), |
2952 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 2953 | { |
f6bcfd97 | 2954 | wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); |
8f177c8e | 2955 | |
f85afd4e MB |
2956 | return FALSE; |
2957 | } | |
2958 | ||
aa5e1f75 | 2959 | bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) ) |
f85afd4e | 2960 | { |
f6bcfd97 | 2961 | wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); |
8f177c8e | 2962 | |
f85afd4e MB |
2963 | return FALSE; |
2964 | } | |
2965 | ||
aa5e1f75 SN |
2966 | bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos), |
2967 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 2968 | { |
f6bcfd97 | 2969 | wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); |
8f177c8e | 2970 | |
f85afd4e MB |
2971 | return FALSE; |
2972 | } | |
2973 | ||
2974 | ||
2975 | wxString wxGridTableBase::GetRowLabelValue( int row ) | |
2976 | { | |
2977 | wxString s; | |
f2d76237 RD |
2978 | s << row + 1; // RD: Starting the rows at zero confuses users, no matter |
2979 | // how much it makes sense to us geeks. | |
f85afd4e MB |
2980 | return s; |
2981 | } | |
2982 | ||
2983 | wxString wxGridTableBase::GetColLabelValue( int col ) | |
2984 | { | |
2985 | // default col labels are: | |
2986 | // cols 0 to 25 : A-Z | |
2987 | // cols 26 to 675 : AA-ZZ | |
2988 | // etc. | |
2989 | ||
2990 | wxString s; | |
2991 | unsigned int i, n; | |
2992 | for ( n = 1; ; n++ ) | |
2993 | { | |
f0102d2a | 2994 | s += (_T('A') + (wxChar)( col%26 )); |
f85afd4e MB |
2995 | col = col/26 - 1; |
2996 | if ( col < 0 ) break; | |
2997 | } | |
2998 | ||
2999 | // reverse the string... | |
3000 | wxString s2; | |
3001 | for ( i = 0; i < n; i++ ) | |
3002 | { | |
3003 | s2 += s[n-i-1]; | |
3004 | } | |
3005 | ||
3006 | return s2; | |
3007 | } | |
3008 | ||
3009 | ||
f2d76237 RD |
3010 | wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) ) |
3011 | { | |
816be743 | 3012 | return wxGRID_VALUE_STRING; |
f2d76237 RD |
3013 | } |
3014 | ||
3015 | bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col), | |
3016 | const wxString& typeName ) | |
3017 | { | |
816be743 | 3018 | return typeName == wxGRID_VALUE_STRING; |
f2d76237 RD |
3019 | } |
3020 | ||
3021 | bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName ) | |
3022 | { | |
3023 | return CanGetValueAs(row, col, typeName); | |
3024 | } | |
3025 | ||
3026 | long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) ) | |
3027 | { | |
3028 | return 0; | |
3029 | } | |
3030 | ||
3031 | double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) ) | |
3032 | { | |
3033 | return 0.0; | |
3034 | } | |
3035 | ||
3036 | bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) ) | |
3037 | { | |
3038 | return FALSE; | |
3039 | } | |
3040 | ||
3041 | void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col), | |
3042 | long WXUNUSED(value) ) | |
3043 | { | |
3044 | } | |
3045 | ||
3046 | void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col), | |
3047 | double WXUNUSED(value) ) | |
3048 | { | |
3049 | } | |
3050 | ||
3051 | void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col), | |
3052 | bool WXUNUSED(value) ) | |
3053 | { | |
3054 | } | |
3055 | ||
3056 | ||
3057 | void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3058 | const wxString& WXUNUSED(typeName) ) | |
3059 | { | |
3060 | return NULL; | |
3061 | } | |
3062 | ||
3063 | void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3064 | const wxString& WXUNUSED(typeName), | |
3065 | void* WXUNUSED(value) ) | |
3066 | { | |
3067 | } | |
3068 | ||
f85afd4e MB |
3069 | ////////////////////////////////////////////////////////////////////// |
3070 | // | |
3071 | // Message class for the grid table to send requests and notifications | |
3072 | // to the grid view | |
3073 | // | |
3074 | ||
3075 | wxGridTableMessage::wxGridTableMessage() | |
3076 | { | |
3077 | m_table = (wxGridTableBase *) NULL; | |
3078 | m_id = -1; | |
3079 | m_comInt1 = -1; | |
3080 | m_comInt2 = -1; | |
3081 | } | |
3082 | ||
3083 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, | |
3084 | int commandInt1, int commandInt2 ) | |
3085 | { | |
3086 | m_table = table; | |
3087 | m_id = id; | |
3088 | m_comInt1 = commandInt1; | |
3089 | m_comInt2 = commandInt2; | |
3090 | } | |
3091 | ||
3092 | ||
3093 | ||
3094 | ////////////////////////////////////////////////////////////////////// | |
3095 | // | |
3096 | // A basic grid table for string data. An object of this class will | |
3097 | // created by wxGrid if you don't specify an alternative table class. | |
3098 | // | |
3099 | ||
223d09f6 | 3100 | WX_DEFINE_OBJARRAY(wxGridStringArray) |
f85afd4e MB |
3101 | |
3102 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) | |
3103 | ||
3104 | wxGridStringTable::wxGridStringTable() | |
3105 | : wxGridTableBase() | |
3106 | { | |
3107 | } | |
3108 | ||
3109 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) | |
3110 | : wxGridTableBase() | |
3111 | { | |
f85afd4e MB |
3112 | m_data.Alloc( numRows ); |
3113 | ||
3114 | wxArrayString sa; | |
3115 | sa.Alloc( numCols ); | |
27f35b66 | 3116 | sa.Add( wxEmptyString, numCols ); |
8f177c8e | 3117 | |
27f35b66 | 3118 | m_data.Add( sa, numRows ); |
f85afd4e MB |
3119 | } |
3120 | ||
3121 | wxGridStringTable::~wxGridStringTable() | |
3122 | { | |
3123 | } | |
3124 | ||
e32352cf | 3125 | int wxGridStringTable::GetNumberRows() |
f85afd4e MB |
3126 | { |
3127 | return m_data.GetCount(); | |
3128 | } | |
3129 | ||
e32352cf | 3130 | int wxGridStringTable::GetNumberCols() |
f85afd4e MB |
3131 | { |
3132 | if ( m_data.GetCount() > 0 ) | |
3133 | return m_data[0].GetCount(); | |
3134 | else | |
3135 | return 0; | |
3136 | } | |
3137 | ||
3138 | wxString wxGridStringTable::GetValue( int row, int col ) | |
3139 | { | |
3e13956a RD |
3140 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
3141 | wxEmptyString, | |
3142 | _T("invalid row or column index in wxGridStringTable") ); | |
af547d51 | 3143 | |
f85afd4e MB |
3144 | return m_data[row][col]; |
3145 | } | |
3146 | ||
f2d76237 | 3147 | void wxGridStringTable::SetValue( int row, int col, const wxString& value ) |
f85afd4e | 3148 | { |
3e13956a RD |
3149 | wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()), |
3150 | _T("invalid row or column index in wxGridStringTable") ); | |
af547d51 | 3151 | |
f2d76237 | 3152 | m_data[row][col] = value; |
f85afd4e MB |
3153 | } |
3154 | ||
3155 | bool wxGridStringTable::IsEmptyCell( int row, int col ) | |
3156 | { | |
3e13956a RD |
3157 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
3158 | true, | |
af547d51 VZ |
3159 | _T("invalid row or column index in wxGridStringTable") ); |
3160 | ||
f85afd4e MB |
3161 | return (m_data[row][col] == wxEmptyString); |
3162 | } | |
3163 | ||
f85afd4e MB |
3164 | void wxGridStringTable::Clear() |
3165 | { | |
3166 | int row, col; | |
3167 | int numRows, numCols; | |
8f177c8e | 3168 | |
f85afd4e MB |
3169 | numRows = m_data.GetCount(); |
3170 | if ( numRows > 0 ) | |
3171 | { | |
3172 | numCols = m_data[0].GetCount(); | |
3173 | ||
3174 | for ( row = 0; row < numRows; row++ ) | |
3175 | { | |
3176 | for ( col = 0; col < numCols; col++ ) | |
3177 | { | |
3178 | m_data[row][col] = wxEmptyString; | |
3179 | } | |
3180 | } | |
3181 | } | |
3182 | } | |
3183 | ||
3184 | ||
3185 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) | |
3186 | { | |
f85afd4e | 3187 | size_t curNumRows = m_data.GetCount(); |
f6bcfd97 BP |
3188 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3189 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3190 | |
f85afd4e MB |
3191 | if ( pos >= curNumRows ) |
3192 | { | |
3193 | return AppendRows( numRows ); | |
3194 | } | |
8f177c8e | 3195 | |
f85afd4e MB |
3196 | wxArrayString sa; |
3197 | sa.Alloc( curNumCols ); | |
27f35b66 SN |
3198 | sa.Add( wxEmptyString, curNumCols ); |
3199 | m_data.Insert( sa, pos, numRows ); | |
f85afd4e MB |
3200 | if ( GetView() ) |
3201 | { | |
3202 | wxGridTableMessage msg( this, | |
3203 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, | |
3204 | pos, | |
3205 | numRows ); | |
8f177c8e | 3206 | |
f85afd4e MB |
3207 | GetView()->ProcessTableMessage( msg ); |
3208 | } | |
3209 | ||
3210 | return TRUE; | |
3211 | } | |
3212 | ||
3213 | bool wxGridStringTable::AppendRows( size_t numRows ) | |
3214 | { | |
f85afd4e | 3215 | size_t curNumRows = m_data.GetCount(); |
f6bcfd97 BP |
3216 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3217 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3218 | |
f85afd4e MB |
3219 | wxArrayString sa; |
3220 | if ( curNumCols > 0 ) | |
3221 | { | |
3222 | sa.Alloc( curNumCols ); | |
27f35b66 | 3223 | sa.Add( wxEmptyString, curNumCols ); |
f85afd4e | 3224 | } |
8f177c8e | 3225 | |
27f35b66 | 3226 | m_data.Add( sa, numRows ); |
f85afd4e MB |
3227 | |
3228 | if ( GetView() ) | |
3229 | { | |
3230 | wxGridTableMessage msg( this, | |
3231 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, | |
3232 | numRows ); | |
8f177c8e | 3233 | |
f85afd4e MB |
3234 | GetView()->ProcessTableMessage( msg ); |
3235 | } | |
3236 | ||
8f177c8e | 3237 | return TRUE; |
f85afd4e MB |
3238 | } |
3239 | ||
3240 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) | |
3241 | { | |
f85afd4e | 3242 | size_t curNumRows = m_data.GetCount(); |
8f177c8e | 3243 | |
f85afd4e MB |
3244 | if ( pos >= curNumRows ) |
3245 | { | |
e91d2033 VZ |
3246 | wxFAIL_MSG( wxString::Format |
3247 | ( | |
3248 | wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), | |
3249 | (unsigned long)pos, | |
3250 | (unsigned long)numRows, | |
3251 | (unsigned long)curNumRows | |
3252 | ) ); | |
3253 | ||
f85afd4e MB |
3254 | return FALSE; |
3255 | } | |
3256 | ||
3257 | if ( numRows > curNumRows - pos ) | |
3258 | { | |
3259 | numRows = curNumRows - pos; | |
3260 | } | |
8f177c8e | 3261 | |
f85afd4e MB |
3262 | if ( numRows >= curNumRows ) |
3263 | { | |
d57ad377 | 3264 | m_data.Clear(); |
f85afd4e MB |
3265 | } |
3266 | else | |
3267 | { | |
27f35b66 | 3268 | m_data.RemoveAt( pos, numRows ); |
f85afd4e | 3269 | } |
f85afd4e MB |
3270 | if ( GetView() ) |
3271 | { | |
3272 | wxGridTableMessage msg( this, | |
3273 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, | |
3274 | pos, | |
3275 | numRows ); | |
8f177c8e | 3276 | |
f85afd4e MB |
3277 | GetView()->ProcessTableMessage( msg ); |
3278 | } | |
3279 | ||
8f177c8e | 3280 | return TRUE; |
f85afd4e MB |
3281 | } |
3282 | ||
3283 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) | |
3284 | { | |
3285 | size_t row, col; | |
3286 | ||
3287 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 BP |
3288 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3289 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3290 | |
f85afd4e MB |
3291 | if ( pos >= curNumCols ) |
3292 | { | |
3293 | return AppendCols( numCols ); | |
3294 | } | |
3295 | ||
3296 | for ( row = 0; row < curNumRows; row++ ) | |
3297 | { | |
3298 | for ( col = pos; col < pos + numCols; col++ ) | |
3299 | { | |
3300 | m_data[row].Insert( wxEmptyString, col ); | |
3301 | } | |
3302 | } | |
f85afd4e MB |
3303 | if ( GetView() ) |
3304 | { | |
3305 | wxGridTableMessage msg( this, | |
3306 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, | |
3307 | pos, | |
3308 | numCols ); | |
8f177c8e | 3309 | |
f85afd4e MB |
3310 | GetView()->ProcessTableMessage( msg ); |
3311 | } | |
3312 | ||
3313 | return TRUE; | |
3314 | } | |
3315 | ||
3316 | bool wxGridStringTable::AppendCols( size_t numCols ) | |
3317 | { | |
27f35b66 | 3318 | size_t row; |
f85afd4e MB |
3319 | |
3320 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 | 3321 | #if 0 |
f85afd4e MB |
3322 | if ( !curNumRows ) |
3323 | { | |
3324 | // TODO: something better than this ? | |
3325 | // | |
f6bcfd97 | 3326 | wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") ); |
f85afd4e MB |
3327 | return FALSE; |
3328 | } | |
f6bcfd97 | 3329 | #endif |
8f177c8e | 3330 | |
f85afd4e MB |
3331 | for ( row = 0; row < curNumRows; row++ ) |
3332 | { | |
27f35b66 | 3333 | m_data[row].Add( wxEmptyString, numCols ); |
f85afd4e MB |
3334 | } |
3335 | ||
3336 | if ( GetView() ) | |
3337 | { | |
3338 | wxGridTableMessage msg( this, | |
3339 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, | |
3340 | numCols ); | |
8f177c8e | 3341 | |
f85afd4e MB |
3342 | GetView()->ProcessTableMessage( msg ); |
3343 | } | |
3344 | ||
3345 | return TRUE; | |
3346 | } | |
3347 | ||
3348 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) | |
3349 | { | |
27f35b66 | 3350 | size_t row; |
f85afd4e MB |
3351 | |
3352 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 BP |
3353 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3354 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3355 | |
f85afd4e MB |
3356 | if ( pos >= curNumCols ) |
3357 | { | |
e91d2033 VZ |
3358 | wxFAIL_MSG( wxString::Format |
3359 | ( | |
3360 | wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), | |
3361 | (unsigned long)pos, | |
3362 | (unsigned long)numCols, | |
3363 | (unsigned long)curNumCols | |
3364 | ) ); | |
8f177c8e | 3365 | return FALSE; |
f85afd4e MB |
3366 | } |
3367 | ||
3368 | if ( numCols > curNumCols - pos ) | |
3369 | { | |
8f177c8e | 3370 | numCols = curNumCols - pos; |
f85afd4e MB |
3371 | } |
3372 | ||
3373 | for ( row = 0; row < curNumRows; row++ ) | |
3374 | { | |
3375 | if ( numCols >= curNumCols ) | |
3376 | { | |
dcdce64e | 3377 | m_data[row].Clear(); |
f85afd4e MB |
3378 | } |
3379 | else | |
3380 | { | |
27f35b66 | 3381 | m_data[row].RemoveAt( pos, numCols ); |
f85afd4e MB |
3382 | } |
3383 | } | |
f85afd4e MB |
3384 | if ( GetView() ) |
3385 | { | |
3386 | wxGridTableMessage msg( this, | |
3387 | wxGRIDTABLE_NOTIFY_COLS_DELETED, | |
3388 | pos, | |
3389 | numCols ); | |
8f177c8e | 3390 | |
f85afd4e MB |
3391 | GetView()->ProcessTableMessage( msg ); |
3392 | } | |
3393 | ||
8f177c8e | 3394 | return TRUE; |
f85afd4e MB |
3395 | } |
3396 | ||
3397 | wxString wxGridStringTable::GetRowLabelValue( int row ) | |
3398 | { | |
3399 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3400 | { | |
3401 | // using default label | |
3402 | // | |
3403 | return wxGridTableBase::GetRowLabelValue( row ); | |
3404 | } | |
3405 | else | |
3406 | { | |
3407 | return m_rowLabels[ row ]; | |
3408 | } | |
3409 | } | |
3410 | ||
3411 | wxString wxGridStringTable::GetColLabelValue( int col ) | |
3412 | { | |
3413 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3414 | { | |
3415 | // using default label | |
3416 | // | |
3417 | return wxGridTableBase::GetColLabelValue( col ); | |
3418 | } | |
3419 | else | |
3420 | { | |
3421 | return m_colLabels[ col ]; | |
3422 | } | |
3423 | } | |
3424 | ||
3425 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) | |
3426 | { | |
3427 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3428 | { | |
3429 | int n = m_rowLabels.GetCount(); | |
3430 | int i; | |
3431 | for ( i = n; i <= row; i++ ) | |
3432 | { | |
3433 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); | |
3434 | } | |
3435 | } | |
3436 | ||
3437 | m_rowLabels[row] = value; | |
3438 | } | |
3439 | ||
3440 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) | |
3441 | { | |
3442 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3443 | { | |
3444 | int n = m_colLabels.GetCount(); | |
3445 | int i; | |
3446 | for ( i = n; i <= col; i++ ) | |
3447 | { | |
3448 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); | |
3449 | } | |
3450 | } | |
3451 | ||
3452 | m_colLabels[col] = value; | |
3453 | } | |
3454 | ||
3455 | ||
3456 | ||
f85afd4e | 3457 | ////////////////////////////////////////////////////////////////////// |
2d66e025 MB |
3458 | ////////////////////////////////////////////////////////////////////// |
3459 | ||
3460 | IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow ) | |
3461 | ||
3462 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow ) | |
3463 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) | |
b51c3f27 | 3464 | EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel) |
2d66e025 MB |
3465 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) |
3466 | EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown ) | |
f6bcfd97 | 3467 | EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp ) |
2d66e025 MB |
3468 | END_EVENT_TABLE() |
3469 | ||
60ff3b99 VZ |
3470 | wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent, |
3471 | wxWindowID id, | |
2d66e025 | 3472 | const wxPoint &pos, const wxSize &size ) |
0bbeb42d | 3473 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE ) |
2d66e025 MB |
3474 | { |
3475 | m_owner = parent; | |
3476 | } | |
3477 | ||
aa5e1f75 | 3478 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
3479 | { |
3480 | wxPaintDC dc(this); | |
3481 | ||
3482 | // NO - don't do this because it will set both the x and y origin | |
3483 | // coords to match the parent scrolled window and we just want to | |
3484 | // set the y coord - MB | |
3485 | // | |
3486 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 3487 | |
790ad94f | 3488 | int x, y; |
2d66e025 MB |
3489 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
3490 | dc.SetDeviceOrigin( 0, -y ); | |
60ff3b99 | 3491 | |
d10f4bf9 VZ |
3492 | wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); |
3493 | m_owner->DrawRowLabels( dc , rows ); | |
2d66e025 MB |
3494 | } |
3495 | ||
3496 | ||
3497 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3498 | { | |
3499 | m_owner->ProcessRowLabelMouseEvent( event ); | |
3500 | } | |
3501 | ||
3502 | ||
b51c3f27 RD |
3503 | void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3504 | { | |
3505 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3506 | } | |
3507 | ||
3508 | ||
2d66e025 MB |
3509 | // This seems to be required for wxMotif otherwise the mouse |
3510 | // cursor must be in the cell edit control to get key events | |
3511 | // | |
3512 | void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3513 | { | |
ffdd3c98 | 3514 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3515 | } |
3516 | ||
f6bcfd97 BP |
3517 | void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3518 | { | |
ffdd3c98 | 3519 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3520 | } |
3521 | ||
2d66e025 MB |
3522 | |
3523 | ||
3524 | ////////////////////////////////////////////////////////////////////// | |
3525 | ||
3526 | IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow ) | |
3527 | ||
3528 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow ) | |
3529 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) | |
b51c3f27 | 3530 | EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel) |
2d66e025 MB |
3531 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) |
3532 | EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown ) | |
f6bcfd97 | 3533 | EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp ) |
2d66e025 MB |
3534 | END_EVENT_TABLE() |
3535 | ||
60ff3b99 VZ |
3536 | wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent, |
3537 | wxWindowID id, | |
2d66e025 | 3538 | const wxPoint &pos, const wxSize &size ) |
0bbeb42d | 3539 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE ) |
2d66e025 MB |
3540 | { |
3541 | m_owner = parent; | |
3542 | } | |
3543 | ||
aa5e1f75 | 3544 | void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
3545 | { |
3546 | wxPaintDC dc(this); | |
3547 | ||
3548 | // NO - don't do this because it will set both the x and y origin | |
3549 | // coords to match the parent scrolled window and we just want to | |
3550 | // set the x coord - MB | |
3551 | // | |
3552 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 3553 | |
790ad94f | 3554 | int x, y; |
2d66e025 MB |
3555 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
3556 | dc.SetDeviceOrigin( -x, 0 ); | |
3557 | ||
d10f4bf9 VZ |
3558 | wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() ); |
3559 | m_owner->DrawColLabels( dc , cols ); | |
2d66e025 MB |
3560 | } |
3561 | ||
3562 | ||
3563 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3564 | { | |
3565 | m_owner->ProcessColLabelMouseEvent( event ); | |
3566 | } | |
3567 | ||
b51c3f27 RD |
3568 | void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3569 | { | |
3570 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3571 | } | |
3572 | ||
2d66e025 MB |
3573 | |
3574 | // This seems to be required for wxMotif otherwise the mouse | |
3575 | // cursor must be in the cell edit control to get key events | |
3576 | // | |
3577 | void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3578 | { | |
ffdd3c98 | 3579 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3580 | } |
3581 | ||
f6bcfd97 BP |
3582 | void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3583 | { | |
ffdd3c98 | 3584 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3585 | } |
3586 | ||
2d66e025 MB |
3587 | |
3588 | ||
3589 | ////////////////////////////////////////////////////////////////////// | |
3590 | ||
3591 | IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow ) | |
3592 | ||
3593 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow ) | |
b51c3f27 | 3594 | EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel) |
2d66e025 | 3595 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) |
d2fdd8d2 | 3596 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint) |
2d66e025 | 3597 | EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown ) |
f6bcfd97 | 3598 | EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp ) |
2d66e025 MB |
3599 | END_EVENT_TABLE() |
3600 | ||
60ff3b99 VZ |
3601 | wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent, |
3602 | wxWindowID id, | |
2d66e025 | 3603 | const wxPoint &pos, const wxSize &size ) |
0bbeb42d | 3604 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE ) |
2d66e025 MB |
3605 | { |
3606 | m_owner = parent; | |
3607 | } | |
3608 | ||
d2fdd8d2 RR |
3609 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
3610 | { | |
3611 | wxPaintDC dc(this); | |
b99be8fb | 3612 | |
d2fdd8d2 RR |
3613 | int client_height = 0; |
3614 | int client_width = 0; | |
3615 | GetClientSize( &client_width, &client_height ); | |
b99be8fb | 3616 | |
73145b0e | 3617 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) ); |
d2fdd8d2 RR |
3618 | dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 ); |
3619 | dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 ); | |
d2fdd8d2 RR |
3620 | dc.DrawLine( 0, 0, client_width, 0 ); |
3621 | dc.DrawLine( 0, 0, 0, client_height ); | |
73145b0e JS |
3622 | |
3623 | dc.SetPen( *wxWHITE_PEN ); | |
3624 | dc.DrawLine( 1, 1, client_width-1, 1 ); | |
3625 | dc.DrawLine( 1, 1, 1, client_height-1 ); | |
d2fdd8d2 RR |
3626 | } |
3627 | ||
2d66e025 MB |
3628 | |
3629 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3630 | { | |
3631 | m_owner->ProcessCornerLabelMouseEvent( event ); | |
3632 | } | |
3633 | ||
3634 | ||
b51c3f27 RD |
3635 | void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3636 | { | |
3637 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3638 | } | |
3639 | ||
2d66e025 MB |
3640 | // This seems to be required for wxMotif otherwise the mouse |
3641 | // cursor must be in the cell edit control to get key events | |
3642 | // | |
3643 | void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3644 | { | |
ffdd3c98 | 3645 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3646 | } |
3647 | ||
f6bcfd97 BP |
3648 | void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3649 | { | |
ffdd3c98 | 3650 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3651 | } |
3652 | ||
2d66e025 MB |
3653 | |
3654 | ||
f85afd4e MB |
3655 | ////////////////////////////////////////////////////////////////////// |
3656 | ||
59ddac01 | 3657 | IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow ) |
2d66e025 | 3658 | |
59ddac01 | 3659 | BEGIN_EVENT_TABLE( wxGridWindow, wxWindow ) |
2d66e025 | 3660 | EVT_PAINT( wxGridWindow::OnPaint ) |
b51c3f27 | 3661 | EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel) |
2d66e025 MB |
3662 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) |
3663 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) | |
f6bcfd97 | 3664 | EVT_KEY_UP( wxGridWindow::OnKeyUp ) |
2796cce3 | 3665 | EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground ) |
2d66e025 MB |
3666 | END_EVENT_TABLE() |
3667 | ||
60ff3b99 VZ |
3668 | wxGridWindow::wxGridWindow( wxGrid *parent, |
3669 | wxGridRowLabelWindow *rowLblWin, | |
2d66e025 | 3670 | wxGridColLabelWindow *colLblWin, |
04418332 VZ |
3671 | wxWindowID id, |
3672 | const wxPoint &pos, | |
3673 | const wxSize &size ) | |
0bbeb42d | 3674 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN, |
04418332 | 3675 | wxT("grid window") ) |
73145b0e | 3676 | |
2d66e025 MB |
3677 | { |
3678 | m_owner = parent; | |
3679 | m_rowLabelWin = rowLblWin; | |
3680 | m_colLabelWin = colLblWin; | |
edb89f7e | 3681 | SetBackgroundColour(_T("WHITE")); |
2d66e025 MB |
3682 | } |
3683 | ||
3684 | ||
3685 | wxGridWindow::~wxGridWindow() | |
3686 | { | |
3687 | } | |
3688 | ||
3689 | ||
3690 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
3691 | { | |
3692 | wxPaintDC dc( this ); | |
3693 | m_owner->PrepareDC( dc ); | |
796df70a | 3694 | wxRegion reg = GetUpdateRegion(); |
d10f4bf9 VZ |
3695 | wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg ); |
3696 | m_owner->DrawGridCellArea( dc , DirtyCells); | |
9496deb5 | 3697 | #if WXGRID_DRAW_LINES |
796df70a SN |
3698 | m_owner->DrawAllGridLines( dc, reg ); |
3699 | #endif | |
a5777624 | 3700 | m_owner->DrawGridSpace( dc ); |
d10f4bf9 | 3701 | m_owner->DrawHighlight( dc , DirtyCells ); |
2d66e025 MB |
3702 | } |
3703 | ||
3704 | ||
3705 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
3706 | { | |
59ddac01 | 3707 | wxWindow::ScrollWindow( dx, dy, rect ); |
2d66e025 MB |
3708 | m_rowLabelWin->ScrollWindow( 0, dy, rect ); |
3709 | m_colLabelWin->ScrollWindow( dx, 0, rect ); | |
3710 | } | |
3711 | ||
3712 | ||
3713 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) | |
3714 | { | |
3715 | m_owner->ProcessGridCellMouseEvent( event ); | |
3716 | } | |
3717 | ||
b51c3f27 RD |
3718 | void wxGridWindow::OnMouseWheel( wxMouseEvent& event ) |
3719 | { | |
3720 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3721 | } | |
2d66e025 | 3722 | |
f6bcfd97 | 3723 | // This seems to be required for wxMotif/wxGTK otherwise the mouse |
2d66e025 MB |
3724 | // cursor must be in the cell edit control to get key events |
3725 | // | |
3726 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) | |
3727 | { | |
ffdd3c98 | 3728 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 | 3729 | } |
f85afd4e | 3730 | |
f6bcfd97 BP |
3731 | void wxGridWindow::OnKeyUp( wxKeyEvent& event ) |
3732 | { | |
ffdd3c98 | 3733 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 | 3734 | } |
7c8a8ad5 | 3735 | |
aa5e1f75 | 3736 | void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) |
8dd4f536 | 3737 | { |
8dd4f536 | 3738 | } |
025562fe | 3739 | |
2d66e025 MB |
3740 | |
3741 | ////////////////////////////////////////////////////////////////////// | |
3742 | ||
33188aa4 SN |
3743 | // Internal Helper function for computing row or column from some |
3744 | // (unscrolled) coordinate value, using either | |
70e8d961 | 3745 | // m_defaultRowHeight/m_defaultColWidth or binary search on array |
33188aa4 SN |
3746 | // of m_rowBottoms/m_ColRights to speed up the search! |
3747 | ||
3748 | // Internal helper macros for simpler use of that function | |
3749 | ||
3750 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, | |
64e15340 | 3751 | const wxArrayInt& BorderArray, int nMax, |
a967f048 | 3752 | bool clipToMinMax); |
33188aa4 SN |
3753 | |
3754 | #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \ | |
b8d24d4e | 3755 | m_minAcceptableColWidth, \ |
64e15340 | 3756 | m_colRights, m_numCols, TRUE) |
33188aa4 | 3757 | #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \ |
b8d24d4e | 3758 | m_minAcceptableRowHeight, \ |
64e15340 | 3759 | m_rowBottoms, m_numRows, TRUE) |
33188aa4 | 3760 | ///////////////////////////////////////////////////////////////////// |
07296f0b | 3761 | |
2d66e025 MB |
3762 | IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow ) |
3763 | ||
3764 | BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow ) | |
f85afd4e MB |
3765 | EVT_PAINT( wxGrid::OnPaint ) |
3766 | EVT_SIZE( wxGrid::OnSize ) | |
f85afd4e | 3767 | EVT_KEY_DOWN( wxGrid::OnKeyDown ) |
f6bcfd97 | 3768 | EVT_KEY_UP( wxGrid::OnKeyUp ) |
2796cce3 | 3769 | EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground ) |
f85afd4e | 3770 | END_EVENT_TABLE() |
8f177c8e | 3771 | |
2d66e025 MB |
3772 | wxGrid::wxGrid( wxWindow *parent, |
3773 | wxWindowID id, | |
3774 | const wxPoint& pos, | |
3775 | const wxSize& size, | |
3776 | long style, | |
3777 | const wxString& name ) | |
ebd773c6 | 3778 | : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ), |
af547d51 VZ |
3779 | m_colMinWidths(GRID_HASH_SIZE), |
3780 | m_rowMinHeights(GRID_HASH_SIZE) | |
2d66e025 MB |
3781 | { |
3782 | Create(); | |
58dd5b3b MB |
3783 | } |
3784 | ||
3785 | ||
3786 | wxGrid::~wxGrid() | |
3787 | { | |
606b005f JS |
3788 | // Must do this or ~wxScrollHelper will pop the wrong event handler |
3789 | SetTargetWindow(this); | |
0a976765 | 3790 | ClearAttrCache(); |
39bcce60 | 3791 | wxSafeDecRef(m_defaultCellAttr); |
0a976765 VZ |
3792 | |
3793 | #ifdef DEBUG_ATTR_CACHE | |
3794 | size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses; | |
3795 | wxPrintf(_T("wxGrid attribute cache statistics: " | |
3796 | "total: %u, hits: %u (%u%%)\n"), | |
3797 | total, gs_nAttrCacheHits, | |
3798 | total ? (gs_nAttrCacheHits*100) / total : 0); | |
3799 | #endif | |
3800 | ||
2796cce3 RD |
3801 | if (m_ownTable) |
3802 | delete m_table; | |
f2d76237 RD |
3803 | |
3804 | delete m_typeRegistry; | |
b5808881 | 3805 | delete m_selection; |
58dd5b3b MB |
3806 | } |
3807 | ||
2d66e025 | 3808 | |
58dd5b3b MB |
3809 | // |
3810 | // ----- internal init and update functions | |
3811 | // | |
3812 | ||
3813 | void wxGrid::Create() | |
f0102d2a | 3814 | { |
4634a5d6 | 3815 | m_created = FALSE; // set to TRUE by CreateGrid |
4634a5d6 MB |
3816 | |
3817 | m_table = (wxGridTableBase *) NULL; | |
2796cce3 | 3818 | m_ownTable = FALSE; |
2c9a89e0 RD |
3819 | |
3820 | m_cellEditCtrlEnabled = FALSE; | |
4634a5d6 | 3821 | |
ccd970b1 | 3822 | m_defaultCellAttr = new wxGridCellAttr(); |
f2d76237 RD |
3823 | |
3824 | // Set default cell attributes | |
ccd970b1 | 3825 | m_defaultCellAttr->SetDefAttr(m_defaultCellAttr); |
19d7140e | 3826 | m_defaultCellAttr->SetKind(wxGridCellAttr::Default); |
f2d76237 | 3827 | m_defaultCellAttr->SetFont(GetFont()); |
4c7277db | 3828 | m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP); |
f2d76237 | 3829 | m_defaultCellAttr->SetTextColour( |
a756f210 | 3830 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); |
f2d76237 | 3831 | m_defaultCellAttr->SetBackgroundColour( |
a756f210 | 3832 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f2d76237 RD |
3833 | m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer); |
3834 | m_defaultCellAttr->SetEditor(new wxGridCellTextEditor); | |
2796cce3 RD |
3835 | |
3836 | ||
4634a5d6 MB |
3837 | m_numRows = 0; |
3838 | m_numCols = 0; | |
3839 | m_currentCellCoords = wxGridNoCellCoords; | |
b99be8fb | 3840 | |
18f9565d MB |
3841 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; |
3842 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
2d66e025 | 3843 | |
c4608a8a | 3844 | // create the type registry |
f2d76237 | 3845 | m_typeRegistry = new wxGridTypeRegistry; |
3f3dc2ef VZ |
3846 | m_selection = NULL; |
3847 | ||
f2d76237 | 3848 | // subwindow components that make up the wxGrid |
7807d81c MB |
3849 | m_cornerLabelWin = new wxGridCornerLabelWindow( this, |
3850 | -1, | |
18f9565d MB |
3851 | wxDefaultPosition, |
3852 | wxDefaultSize ); | |
7807d81c | 3853 | |
60ff3b99 VZ |
3854 | m_rowLabelWin = new wxGridRowLabelWindow( this, |
3855 | -1, | |
18f9565d MB |
3856 | wxDefaultPosition, |
3857 | wxDefaultSize ); | |
2d66e025 MB |
3858 | |
3859 | m_colLabelWin = new wxGridColLabelWindow( this, | |
3860 | -1, | |
18f9565d MB |
3861 | wxDefaultPosition, |
3862 | wxDefaultSize ); | |
60ff3b99 | 3863 | |
60ff3b99 VZ |
3864 | m_gridWin = new wxGridWindow( this, |
3865 | m_rowLabelWin, | |
3866 | m_colLabelWin, | |
3867 | -1, | |
18f9565d | 3868 | wxDefaultPosition, |
2d66e025 MB |
3869 | wxDefaultSize ); |
3870 | ||
3871 | SetTargetWindow( m_gridWin ); | |
6f36917b VZ |
3872 | |
3873 | Init(); | |
2d66e025 | 3874 | } |
f85afd4e | 3875 | |
2d66e025 | 3876 | |
b5808881 SN |
3877 | bool wxGrid::CreateGrid( int numRows, int numCols, |
3878 | wxGrid::wxGridSelectionModes selmode ) | |
2d66e025 | 3879 | { |
f6bcfd97 BP |
3880 | wxCHECK_MSG( !m_created, |
3881 | FALSE, | |
3882 | wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); | |
3883 | ||
3884 | m_numRows = numRows; | |
3885 | m_numCols = numCols; | |
3886 | ||
3887 | m_table = new wxGridStringTable( m_numRows, m_numCols ); | |
3888 | m_table->SetView( this ); | |
3889 | m_ownTable = TRUE; | |
3890 | m_selection = new wxGridSelection( this, selmode ); | |
6f36917b VZ |
3891 | |
3892 | CalcDimensions(); | |
3893 | ||
f6bcfd97 | 3894 | m_created = TRUE; |
2d66e025 | 3895 | |
2796cce3 RD |
3896 | return m_created; |
3897 | } | |
3898 | ||
f1567cdd SN |
3899 | void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode) |
3900 | { | |
6f36917b VZ |
3901 | wxCHECK_RET( m_created, |
3902 | wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") ); | |
3903 | ||
3904 | m_selection->SetSelectionMode( selmode ); | |
f1567cdd SN |
3905 | } |
3906 | ||
aa5b8857 SN |
3907 | wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const |
3908 | { | |
3909 | wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells, | |
3910 | wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") ); | |
3911 | ||
3912 | return m_selection->GetSelectionMode(); | |
3913 | } | |
3914 | ||
043d16b2 SN |
3915 | bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership, |
3916 | wxGrid::wxGridSelectionModes selmode ) | |
2796cce3 RD |
3917 | { |
3918 | if ( m_created ) | |
3919 | { | |
3e13956a RD |
3920 | // stop all processing |
3921 | m_created = FALSE; | |
86c7378f SN |
3922 | |
3923 | if (m_ownTable) | |
3924 | { | |
3925 | wxGridTableBase *t=m_table; | |
3926 | m_table=0; | |
3e13956a | 3927 | delete t; |
86c7378f | 3928 | } |
3e13956a RD |
3929 | delete m_selection; |
3930 | ||
3931 | m_table=0; | |
3932 | m_selection=0; | |
3933 | m_numRows=0; | |
3934 | m_numCols=0; | |
233a54f6 JS |
3935 | } |
3936 | if (table) | |
2796cce3 RD |
3937 | { |
3938 | m_numRows = table->GetNumberRows(); | |
3939 | m_numCols = table->GetNumberCols(); | |
3940 | ||
3941 | m_table = table; | |
3942 | m_table->SetView( this ); | |
3943 | if (takeOwnership) | |
3944 | m_ownTable = TRUE; | |
043d16b2 | 3945 | m_selection = new wxGridSelection( this, selmode ); |
6f36917b VZ |
3946 | |
3947 | CalcDimensions(); | |
3948 | ||
2d66e025 MB |
3949 | m_created = TRUE; |
3950 | } | |
f85afd4e | 3951 | |
2d66e025 | 3952 | return m_created; |
f85afd4e MB |
3953 | } |
3954 | ||
2d66e025 | 3955 | |
f85afd4e MB |
3956 | void wxGrid::Init() |
3957 | { | |
f85afd4e MB |
3958 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; |
3959 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
3960 | ||
60ff3b99 VZ |
3961 | if ( m_rowLabelWin ) |
3962 | { | |
3963 | m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour(); | |
3964 | } | |
3965 | else | |
3966 | { | |
3967 | m_labelBackgroundColour = wxColour( _T("WHITE") ); | |
3968 | } | |
3969 | ||
3970 | m_labelTextColour = wxColour( _T("BLACK") ); | |
f85afd4e | 3971 | |
0a976765 VZ |
3972 | // init attr cache |
3973 | m_attrCache.row = -1; | |
2b5f62a0 VZ |
3974 | m_attrCache.col = -1; |
3975 | m_attrCache.attr = NULL; | |
0a976765 | 3976 | |
f85afd4e MB |
3977 | // TODO: something better than this ? |
3978 | // | |
3979 | m_labelFont = this->GetFont(); | |
52d6f640 | 3980 | m_labelFont.SetWeight( wxBOLD ); |
8f177c8e | 3981 | |
73145b0e | 3982 | m_rowLabelHorizAlign = wxALIGN_CENTRE; |
4c7277db | 3983 | m_rowLabelVertAlign = wxALIGN_CENTRE; |
f85afd4e | 3984 | |
4c7277db | 3985 | m_colLabelHorizAlign = wxALIGN_CENTRE; |
73145b0e | 3986 | m_colLabelVertAlign = wxALIGN_CENTRE; |
d43851f7 | 3987 | m_colLabelTextOrientation = wxHORIZONTAL; |
f85afd4e | 3988 | |
f85afd4e | 3989 | m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH; |
1f1ce288 MB |
3990 | m_defaultRowHeight = m_gridWin->GetCharHeight(); |
3991 | ||
b8d24d4e RG |
3992 | m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH; |
3993 | m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT; | |
3994 | ||
d2fdd8d2 | 3995 | #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl() |
1f1ce288 MB |
3996 | m_defaultRowHeight += 8; |
3997 | #else | |
3998 | m_defaultRowHeight += 4; | |
3999 | #endif | |
4000 | ||
73145b0e | 4001 | m_gridLineColour = wxColour( 192,192,192 ); |
f85afd4e | 4002 | m_gridLinesEnabled = TRUE; |
73145b0e | 4003 | m_cellHighlightColour = *wxBLACK; |
bf7945ce | 4004 | m_cellHighlightPenWidth = 2; |
d2520c85 | 4005 | m_cellHighlightROPenWidth = 1; |
8f177c8e | 4006 | |
58dd5b3b | 4007 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
e2b42eeb | 4008 | m_winCapture = (wxWindow *)NULL; |
6e8524b1 MB |
4009 | m_canDragRowSize = TRUE; |
4010 | m_canDragColSize = TRUE; | |
4cfa5de6 | 4011 | m_canDragGridSize = TRUE; |
f85afd4e MB |
4012 | m_dragLastPos = -1; |
4013 | m_dragRowOrCol = -1; | |
4014 | m_isDragging = FALSE; | |
07296f0b | 4015 | m_startDragPos = wxDefaultPosition; |
f85afd4e | 4016 | |
07296f0b | 4017 | m_waitForSlowClick = FALSE; |
025562fe | 4018 | |
f85afd4e MB |
4019 | m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS ); |
4020 | m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE ); | |
4021 | ||
4022 | m_currentCellCoords = wxGridNoCellCoords; | |
f85afd4e | 4023 | |
b5808881 SN |
4024 | m_selectingTopLeft = wxGridNoCellCoords; |
4025 | m_selectingBottomRight = wxGridNoCellCoords; | |
d43851f7 JS |
4026 | m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); |
4027 | m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
8f177c8e | 4028 | |
f85afd4e MB |
4029 | m_editable = TRUE; // default for whole grid |
4030 | ||
2d66e025 MB |
4031 | m_inOnKeyDown = FALSE; |
4032 | m_batchCount = 0; | |
3c79cf49 | 4033 | |
266e8367 | 4034 | m_extraWidth = |
526dbb95 | 4035 | m_extraHeight = 0; |
7c1cb261 VZ |
4036 | } |
4037 | ||
4038 | // ---------------------------------------------------------------------------- | |
4039 | // the idea is to call these functions only when necessary because they create | |
4040 | // quite big arrays which eat memory mostly unnecessary - in particular, if | |
4041 | // default widths/heights are used for all rows/columns, we may not use these | |
4042 | // arrays at all | |
4043 | // | |
4044 | // with some extra code, it should be possible to only store the | |
4045 | // widths/heights different from default ones but this will be done later... | |
4046 | // ---------------------------------------------------------------------------- | |
4047 | ||
4048 | void wxGrid::InitRowHeights() | |
4049 | { | |
4050 | m_rowHeights.Empty(); | |
4051 | m_rowBottoms.Empty(); | |
4052 | ||
4053 | m_rowHeights.Alloc( m_numRows ); | |
4054 | m_rowBottoms.Alloc( m_numRows ); | |
4055 | ||
4056 | int rowBottom = 0; | |
2b5f62a0 | 4057 | |
27f35b66 SN |
4058 | m_rowHeights.Add( m_defaultRowHeight, m_numRows ); |
4059 | ||
7c1cb261 VZ |
4060 | for ( int i = 0; i < m_numRows; i++ ) |
4061 | { | |
7c1cb261 VZ |
4062 | rowBottom += m_defaultRowHeight; |
4063 | m_rowBottoms.Add( rowBottom ); | |
4064 | } | |
4065 | } | |
4066 | ||
4067 | void wxGrid::InitColWidths() | |
4068 | { | |
4069 | m_colWidths.Empty(); | |
4070 | m_colRights.Empty(); | |
4071 | ||
4072 | m_colWidths.Alloc( m_numCols ); | |
4073 | m_colRights.Alloc( m_numCols ); | |
4074 | int colRight = 0; | |
27f35b66 SN |
4075 | |
4076 | m_colWidths.Add( m_defaultColWidth, m_numCols ); | |
4077 | ||
7c1cb261 VZ |
4078 | for ( int i = 0; i < m_numCols; i++ ) |
4079 | { | |
7c1cb261 VZ |
4080 | colRight += m_defaultColWidth; |
4081 | m_colRights.Add( colRight ); | |
4082 | } | |
4083 | } | |
4084 | ||
4085 | int wxGrid::GetColWidth(int col) const | |
4086 | { | |
4087 | return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col]; | |
4088 | } | |
4089 | ||
4090 | int wxGrid::GetColLeft(int col) const | |
4091 | { | |
4092 | return m_colRights.IsEmpty() ? col * m_defaultColWidth | |
4093 | : m_colRights[col] - m_colWidths[col]; | |
4094 | } | |
4095 | ||
4096 | int wxGrid::GetColRight(int col) const | |
4097 | { | |
4098 | return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth | |
4099 | : m_colRights[col]; | |
4100 | } | |
4101 | ||
4102 | int wxGrid::GetRowHeight(int row) const | |
4103 | { | |
4104 | return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row]; | |
4105 | } | |
2d66e025 | 4106 | |
7c1cb261 VZ |
4107 | int wxGrid::GetRowTop(int row) const |
4108 | { | |
4109 | return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight | |
4110 | : m_rowBottoms[row] - m_rowHeights[row]; | |
f85afd4e MB |
4111 | } |
4112 | ||
7c1cb261 VZ |
4113 | int wxGrid::GetRowBottom(int row) const |
4114 | { | |
4115 | return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight | |
4116 | : m_rowBottoms[row]; | |
4117 | } | |
f85afd4e MB |
4118 | |
4119 | void wxGrid::CalcDimensions() | |
4120 | { | |
f85afd4e | 4121 | int cw, ch; |
2d66e025 | 4122 | GetClientSize( &cw, &ch ); |
f85afd4e | 4123 | |
faec5a43 | 4124 | if ( m_rowLabelWin->IsShown() ) |
ae1d0c6c VZ |
4125 | cw -= m_rowLabelWidth; |
4126 | if ( m_colLabelWin->IsShown() ) | |
faec5a43 | 4127 | ch -= m_colLabelHeight; |
60ff3b99 | 4128 | |
faec5a43 SN |
4129 | // grid total size |
4130 | int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0; | |
4131 | int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0; | |
4132 | ||
73145b0e JS |
4133 | // take into account editor if shown |
4134 | if( IsCellEditControlShown() ) | |
4135 | { | |
4136 | int w2, h2; | |
4137 | int r = m_currentCellCoords.GetRow(); | |
4138 | int c = m_currentCellCoords.GetCol(); | |
4139 | int x = GetColLeft(c); | |
4140 | int y = GetRowTop(r); | |
4141 | ||
4142 | // how big is the editor | |
4143 | wxGridCellAttr* attr = GetCellAttr(r, c); | |
4144 | wxGridCellEditor* editor = attr->GetEditor(this, r, c); | |
4145 | editor->GetControl()->GetSize(&w2, &h2); | |
4146 | w2 += x; | |
4147 | h2 += y; | |
4148 | if( w2 > w ) w = w2; | |
4149 | if( h2 > h ) h = h2; | |
4150 | editor->DecRef(); | |
4151 | attr->DecRef(); | |
4152 | } | |
4153 | ||
faec5a43 SN |
4154 | // preserve (more or less) the previous position |
4155 | int x, y; | |
4156 | GetViewStart( &x, &y ); | |
97a9929e VZ |
4157 | |
4158 | // maybe we don't need scrollbars at all? | |
4159 | // | |
4160 | // also adjust the position to be valid for the new scroll rangs | |
faec5a43 SN |
4161 | if ( w <= cw ) |
4162 | { | |
97a9929e | 4163 | w = x = 0; |
faec5a43 SN |
4164 | } |
4165 | else | |
4166 | { | |
edb89f7e VZ |
4167 | if ( x >= w ) |
4168 | x = w - 1; | |
f85afd4e | 4169 | } |
97a9929e | 4170 | |
faec5a43 SN |
4171 | if ( h <= ch ) |
4172 | { | |
97a9929e | 4173 | h = y = 0; |
faec5a43 SN |
4174 | } |
4175 | else | |
4176 | { | |
edb89f7e VZ |
4177 | if ( y >= h ) |
4178 | y = h - 1; | |
faec5a43 SN |
4179 | } |
4180 | ||
4181 | // do set scrollbar parameters | |
97a9929e VZ |
4182 | SetScrollbars( GRID_SCROLL_LINE_X, GRID_SCROLL_LINE_Y, |
4183 | GetScrollX(w), GetScrollY(h), x, y, | |
4184 | GetBatchCount() != 0); | |
12314291 VZ |
4185 | |
4186 | // if our OnSize() hadn't been called (it would if we have scrollbars), we | |
4187 | // still must reposition the children | |
4188 | CalcWindowSizes(); | |
f85afd4e MB |
4189 | } |
4190 | ||
4191 | ||
7807d81c MB |
4192 | void wxGrid::CalcWindowSizes() |
4193 | { | |
4194 | int cw, ch; | |
4195 | GetClientSize( &cw, &ch ); | |
b99be8fb | 4196 | |
7807d81c MB |
4197 | if ( m_cornerLabelWin->IsShown() ) |
4198 | m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight ); | |
4199 | ||
4200 | if ( m_colLabelWin->IsShown() ) | |
4201 | m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight); | |
4202 | ||
4203 | if ( m_rowLabelWin->IsShown() ) | |
4204 | m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight); | |
4205 | ||
4206 | if ( m_gridWin->IsShown() ) | |
4207 | m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight); | |
4208 | } | |
4209 | ||
4210 | ||
f85afd4e MB |
4211 | // this is called when the grid table sends a message to say that it |
4212 | // has been redimensioned | |
4213 | // | |
4214 | bool wxGrid::Redimension( wxGridTableMessage& msg ) | |
4215 | { | |
4216 | int i; | |
f6bcfd97 | 4217 | bool result = FALSE; |
8f177c8e | 4218 | |
a6794685 SN |
4219 | // Clear the attribute cache as the attribute might refer to a different |
4220 | // cell than stored in the cache after adding/removing rows/columns. | |
4221 | ClearAttrCache(); | |
7e48d7d9 SN |
4222 | // By the same reasoning, the editor should be dismissed if columns are |
4223 | // added or removed. And for consistency, it should IMHO always be | |
4224 | // removed, not only if the cell "underneath" it actually changes. | |
4225 | // For now, I intentionally do not save the editor's content as the | |
4226 | // cell it might want to save that stuff to might no longer exist. | |
bca7bfc8 | 4227 | HideCellEditControl(); |
f6bcfd97 | 4228 | #if 0 |
7c1cb261 VZ |
4229 | // if we were using the default widths/heights so far, we must change them |
4230 | // now | |
4231 | if ( m_colWidths.IsEmpty() ) | |
4232 | { | |
4233 | InitColWidths(); | |
4234 | } | |
4235 | ||
4236 | if ( m_rowHeights.IsEmpty() ) | |
4237 | { | |
4238 | InitRowHeights(); | |
4239 | } | |
f6bcfd97 | 4240 | #endif |
7c1cb261 | 4241 | |
f85afd4e MB |
4242 | switch ( msg.GetId() ) |
4243 | { | |
4244 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
4245 | { | |
4246 | size_t pos = msg.GetCommandInt(); | |
4247 | int numRows = msg.GetCommandInt2(); | |
f6bcfd97 | 4248 | |
f85afd4e | 4249 | m_numRows += numRows; |
2d66e025 | 4250 | |
f6bcfd97 BP |
4251 | if ( !m_rowHeights.IsEmpty() ) |
4252 | { | |
27f35b66 SN |
4253 | m_rowHeights.Insert( m_defaultRowHeight, pos, numRows ); |
4254 | m_rowBottoms.Insert( 0, pos, numRows ); | |
f6bcfd97 BP |
4255 | |
4256 | int bottom = 0; | |
4257 | if ( pos > 0 ) bottom = m_rowBottoms[pos-1]; | |
60ff3b99 | 4258 | |
f6bcfd97 BP |
4259 | for ( i = pos; i < m_numRows; i++ ) |
4260 | { | |
4261 | bottom += m_rowHeights[i]; | |
4262 | m_rowBottoms[i] = bottom; | |
4263 | } | |
4264 | } | |
4265 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4266 | { | |
4267 | // if we have just inserted cols into an empty grid the current | |
4268 | // cell will be undefined... | |
4269 | // | |
4270 | SetCurrentCell( 0, 0 ); | |
4271 | } | |
3f3dc2ef VZ |
4272 | |
4273 | if ( m_selection ) | |
4274 | m_selection->UpdateRows( pos, numRows ); | |
f6bcfd97 BP |
4275 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4276 | if (attrProvider) | |
4277 | attrProvider->UpdateAttrRows( pos, numRows ); | |
4278 | ||
4279 | if ( !GetBatchCount() ) | |
2d66e025 | 4280 | { |
f6bcfd97 BP |
4281 | CalcDimensions(); |
4282 | m_rowLabelWin->Refresh(); | |
2d66e025 | 4283 | } |
f85afd4e | 4284 | } |
f6bcfd97 BP |
4285 | result = TRUE; |
4286 | break; | |
f85afd4e MB |
4287 | |
4288 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
4289 | { | |
4290 | int numRows = msg.GetCommandInt(); | |
2d66e025 | 4291 | int oldNumRows = m_numRows; |
f85afd4e | 4292 | m_numRows += numRows; |
2d66e025 | 4293 | |
f6bcfd97 BP |
4294 | if ( !m_rowHeights.IsEmpty() ) |
4295 | { | |
27f35b66 SN |
4296 | m_rowHeights.Add( m_defaultRowHeight, numRows ); |
4297 | m_rowBottoms.Add( 0, numRows ); | |
60ff3b99 | 4298 | |
f6bcfd97 BP |
4299 | int bottom = 0; |
4300 | if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1]; | |
4301 | ||
4302 | for ( i = oldNumRows; i < m_numRows; i++ ) | |
4303 | { | |
4304 | bottom += m_rowHeights[i]; | |
4305 | m_rowBottoms[i] = bottom; | |
4306 | } | |
4307 | } | |
4308 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4309 | { | |
4310 | // if we have just inserted cols into an empty grid the current | |
4311 | // cell will be undefined... | |
4312 | // | |
4313 | SetCurrentCell( 0, 0 ); | |
4314 | } | |
4315 | if ( !GetBatchCount() ) | |
2d66e025 | 4316 | { |
f6bcfd97 BP |
4317 | CalcDimensions(); |
4318 | m_rowLabelWin->Refresh(); | |
2d66e025 | 4319 | } |
f85afd4e | 4320 | } |
f6bcfd97 BP |
4321 | result = TRUE; |
4322 | break; | |
f85afd4e MB |
4323 | |
4324 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
4325 | { | |
4326 | size_t pos = msg.GetCommandInt(); | |
4327 | int numRows = msg.GetCommandInt2(); | |
f85afd4e MB |
4328 | m_numRows -= numRows; |
4329 | ||
f6bcfd97 | 4330 | if ( !m_rowHeights.IsEmpty() ) |
f85afd4e | 4331 | { |
27f35b66 SN |
4332 | m_rowHeights.RemoveAt( pos, numRows ); |
4333 | m_rowBottoms.RemoveAt( pos, numRows ); | |
2d66e025 MB |
4334 | |
4335 | int h = 0; | |
4336 | for ( i = 0; i < m_numRows; i++ ) | |
4337 | { | |
4338 | h += m_rowHeights[i]; | |
4339 | m_rowBottoms[i] = h; | |
4340 | } | |
f85afd4e | 4341 | } |
f6bcfd97 BP |
4342 | if ( !m_numRows ) |
4343 | { | |
4344 | m_currentCellCoords = wxGridNoCellCoords; | |
4345 | } | |
4346 | else | |
4347 | { | |
4348 | if ( m_currentCellCoords.GetRow() >= m_numRows ) | |
4349 | m_currentCellCoords.Set( 0, 0 ); | |
4350 | } | |
3f3dc2ef VZ |
4351 | |
4352 | if ( m_selection ) | |
4353 | m_selection->UpdateRows( pos, -((int)numRows) ); | |
f6bcfd97 BP |
4354 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4355 | if (attrProvider) { | |
4356 | attrProvider->UpdateAttrRows( pos, -((int)numRows) ); | |
84912ef8 RD |
4357 | // ifdef'd out following patch from Paul Gammans |
4358 | #if 0 | |
3ca6a5f0 | 4359 | // No need to touch column attributes, unless we |
f6bcfd97 BP |
4360 | // removed _all_ rows, in this case, we remove |
4361 | // all column attributes. | |
4362 | // I hate to do this here, but the | |
4363 | // needed data is not available inside UpdateAttrRows. | |
4364 | if ( !GetNumberRows() ) | |
4365 | attrProvider->UpdateAttrCols( 0, -GetNumberCols() ); | |
84912ef8 | 4366 | #endif |
f6bcfd97 BP |
4367 | } |
4368 | if ( !GetBatchCount() ) | |
4369 | { | |
4370 | CalcDimensions(); | |
4371 | m_rowLabelWin->Refresh(); | |
4372 | } | |
f85afd4e | 4373 | } |
f6bcfd97 BP |
4374 | result = TRUE; |
4375 | break; | |
f85afd4e MB |
4376 | |
4377 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
4378 | { | |
4379 | size_t pos = msg.GetCommandInt(); | |
4380 | int numCols = msg.GetCommandInt2(); | |
f85afd4e | 4381 | m_numCols += numCols; |
2d66e025 | 4382 | |
f6bcfd97 BP |
4383 | if ( !m_colWidths.IsEmpty() ) |
4384 | { | |
27f35b66 SN |
4385 | m_colWidths.Insert( m_defaultColWidth, pos, numCols ); |
4386 | m_colRights.Insert( 0, pos, numCols ); | |
f6bcfd97 BP |
4387 | |
4388 | int right = 0; | |
4389 | if ( pos > 0 ) right = m_colRights[pos-1]; | |
60ff3b99 | 4390 | |
f6bcfd97 BP |
4391 | for ( i = pos; i < m_numCols; i++ ) |
4392 | { | |
4393 | right += m_colWidths[i]; | |
4394 | m_colRights[i] = right; | |
4395 | } | |
4396 | } | |
4397 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
2d66e025 | 4398 | { |
f6bcfd97 BP |
4399 | // if we have just inserted cols into an empty grid the current |
4400 | // cell will be undefined... | |
4401 | // | |
4402 | SetCurrentCell( 0, 0 ); | |
2d66e025 | 4403 | } |
3f3dc2ef VZ |
4404 | |
4405 | if ( m_selection ) | |
4406 | m_selection->UpdateCols( pos, numCols ); | |
f6bcfd97 BP |
4407 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4408 | if (attrProvider) | |
4409 | attrProvider->UpdateAttrCols( pos, numCols ); | |
4410 | if ( !GetBatchCount() ) | |
4411 | { | |
4412 | CalcDimensions(); | |
4413 | m_colLabelWin->Refresh(); | |
4414 | } | |
4415 | ||
f85afd4e | 4416 | } |
f6bcfd97 BP |
4417 | result = TRUE; |
4418 | break; | |
f85afd4e MB |
4419 | |
4420 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
4421 | { | |
4422 | int numCols = msg.GetCommandInt(); | |
2d66e025 | 4423 | int oldNumCols = m_numCols; |
f85afd4e | 4424 | m_numCols += numCols; |
f6bcfd97 BP |
4425 | if ( !m_colWidths.IsEmpty() ) |
4426 | { | |
27f35b66 SN |
4427 | m_colWidths.Add( m_defaultColWidth, numCols ); |
4428 | m_colRights.Add( 0, numCols ); | |
2d66e025 | 4429 | |
f6bcfd97 BP |
4430 | int right = 0; |
4431 | if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1]; | |
60ff3b99 | 4432 | |
f6bcfd97 BP |
4433 | for ( i = oldNumCols; i < m_numCols; i++ ) |
4434 | { | |
4435 | right += m_colWidths[i]; | |
4436 | m_colRights[i] = right; | |
4437 | } | |
4438 | } | |
4439 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
2d66e025 | 4440 | { |
f6bcfd97 BP |
4441 | // if we have just inserted cols into an empty grid the current |
4442 | // cell will be undefined... | |
4443 | // | |
4444 | SetCurrentCell( 0, 0 ); | |
4445 | } | |
4446 | if ( !GetBatchCount() ) | |
4447 | { | |
4448 | CalcDimensions(); | |
4449 | m_colLabelWin->Refresh(); | |
2d66e025 | 4450 | } |
f85afd4e | 4451 | } |
f6bcfd97 BP |
4452 | result = TRUE; |
4453 | break; | |
f85afd4e MB |
4454 | |
4455 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
4456 | { | |
4457 | size_t pos = msg.GetCommandInt(); | |
4458 | int numCols = msg.GetCommandInt2(); | |
f85afd4e | 4459 | m_numCols -= numCols; |
f85afd4e | 4460 | |
f6bcfd97 | 4461 | if ( !m_colWidths.IsEmpty() ) |
f85afd4e | 4462 | { |
27f35b66 SN |
4463 | m_colWidths.RemoveAt( pos, numCols ); |
4464 | m_colRights.RemoveAt( pos, numCols ); | |
2d66e025 MB |
4465 | |
4466 | int w = 0; | |
4467 | for ( i = 0; i < m_numCols; i++ ) | |
4468 | { | |
4469 | w += m_colWidths[i]; | |
4470 | m_colRights[i] = w; | |
4471 | } | |
f85afd4e | 4472 | } |
f6bcfd97 BP |
4473 | if ( !m_numCols ) |
4474 | { | |
4475 | m_currentCellCoords = wxGridNoCellCoords; | |
4476 | } | |
4477 | else | |
4478 | { | |
4479 | if ( m_currentCellCoords.GetCol() >= m_numCols ) | |
4480 | m_currentCellCoords.Set( 0, 0 ); | |
4481 | } | |
3f3dc2ef VZ |
4482 | |
4483 | if ( m_selection ) | |
4484 | m_selection->UpdateCols( pos, -((int)numCols) ); | |
f6bcfd97 BP |
4485 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4486 | if (attrProvider) { | |
4487 | attrProvider->UpdateAttrCols( pos, -((int)numCols) ); | |
84912ef8 RD |
4488 | // ifdef'd out following patch from Paul Gammans |
4489 | #if 0 | |
f6bcfd97 BP |
4490 | // No need to touch row attributes, unless we |
4491 | // removed _all_ columns, in this case, we remove | |
4492 | // all row attributes. | |
4493 | // I hate to do this here, but the | |
4494 | // needed data is not available inside UpdateAttrCols. | |
4495 | if ( !GetNumberCols() ) | |
4496 | attrProvider->UpdateAttrRows( 0, -GetNumberRows() ); | |
84912ef8 | 4497 | #endif |
f6bcfd97 BP |
4498 | } |
4499 | if ( !GetBatchCount() ) | |
4500 | { | |
4501 | CalcDimensions(); | |
4502 | m_colLabelWin->Refresh(); | |
4503 | } | |
f85afd4e | 4504 | } |
faec5a43 | 4505 | result = TRUE; |
f6bcfd97 | 4506 | break; |
f85afd4e MB |
4507 | } |
4508 | ||
f6bcfd97 BP |
4509 | if (result && !GetBatchCount() ) |
4510 | m_gridWin->Refresh(); | |
4511 | return result; | |
f85afd4e MB |
4512 | } |
4513 | ||
4514 | ||
d10f4bf9 | 4515 | wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) |
f85afd4e | 4516 | { |
2d66e025 MB |
4517 | wxRegionIterator iter( reg ); |
4518 | wxRect r; | |
f85afd4e | 4519 | |
275c4ae4 RD |
4520 | wxArrayInt rowlabels; |
4521 | ||
2d66e025 MB |
4522 | int top, bottom; |
4523 | while ( iter ) | |
f85afd4e | 4524 | { |
2d66e025 | 4525 | r = iter.GetRect(); |
f85afd4e | 4526 | |
2d66e025 MB |
4527 | // TODO: remove this when we can... |
4528 | // There is a bug in wxMotif that gives garbage update | |
4529 | // rectangles if you jump-scroll a long way by clicking the | |
4530 | // scrollbar with middle button. This is a work-around | |
4531 | // | |
4532 | #if defined(__WXMOTIF__) | |
4533 | int cw, ch; | |
4534 | m_gridWin->GetClientSize( &cw, &ch ); | |
4535 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
4536 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
4537 | #endif | |
f85afd4e | 4538 | |
2d66e025 MB |
4539 | // logical bounds of update region |
4540 | // | |
4541 | int dummy; | |
4542 | CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top ); | |
4543 | CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom ); | |
4544 | ||
4545 | // find the row labels within these bounds | |
4546 | // | |
4547 | int row; | |
33188aa4 | 4548 | for ( row = internalYToRow(top); row < m_numRows; row++ ) |
2d66e025 | 4549 | { |
7c1cb261 VZ |
4550 | if ( GetRowBottom(row) < top ) |
4551 | continue; | |
2d66e025 | 4552 | |
6d55126d | 4553 | if ( GetRowTop(row) > bottom ) |
7c1cb261 | 4554 | break; |
60ff3b99 | 4555 | |
d10f4bf9 | 4556 | rowlabels.Add( row ); |
2d66e025 | 4557 | } |
60ff3b99 | 4558 | |
2d66e025 | 4559 | iter++ ; |
f85afd4e | 4560 | } |
d10f4bf9 VZ |
4561 | |
4562 | return rowlabels; | |
f85afd4e MB |
4563 | } |
4564 | ||
4565 | ||
d10f4bf9 | 4566 | wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) |
f85afd4e | 4567 | { |
2d66e025 MB |
4568 | wxRegionIterator iter( reg ); |
4569 | wxRect r; | |
f85afd4e | 4570 | |
d10f4bf9 | 4571 | wxArrayInt colLabels; |
f85afd4e | 4572 | |
2d66e025 MB |
4573 | int left, right; |
4574 | while ( iter ) | |
f85afd4e | 4575 | { |
2d66e025 | 4576 | r = iter.GetRect(); |
f85afd4e | 4577 | |
2d66e025 MB |
4578 | // TODO: remove this when we can... |
4579 | // There is a bug in wxMotif that gives garbage update | |
4580 | // rectangles if you jump-scroll a long way by clicking the | |
4581 | // scrollbar with middle button. This is a work-around | |
4582 | // | |
4583 | #if defined(__WXMOTIF__) | |
4584 | int cw, ch; | |
4585 | m_gridWin->GetClientSize( &cw, &ch ); | |
4586 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
4587 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
4588 | #endif | |
4589 | ||
4590 | // logical bounds of update region | |
4591 | // | |
4592 | int dummy; | |
4593 | CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy ); | |
4594 | CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy ); | |
4595 | ||
4596 | // find the cells within these bounds | |
4597 | // | |
4598 | int col; | |
33188aa4 | 4599 | for ( col = internalXToCol(left); col < m_numCols; col++ ) |
2d66e025 | 4600 | { |
7c1cb261 VZ |
4601 | if ( GetColRight(col) < left ) |
4602 | continue; | |
60ff3b99 | 4603 | |
7c1cb261 VZ |
4604 | if ( GetColLeft(col) > right ) |
4605 | break; | |
2d66e025 | 4606 | |
d10f4bf9 | 4607 | colLabels.Add( col ); |
2d66e025 | 4608 | } |
60ff3b99 | 4609 | |
2d66e025 | 4610 | iter++ ; |
f85afd4e | 4611 | } |
d10f4bf9 | 4612 | return colLabels; |
f85afd4e MB |
4613 | } |
4614 | ||
4615 | ||
d10f4bf9 | 4616 | wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) |
f85afd4e | 4617 | { |
2d66e025 MB |
4618 | wxRegionIterator iter( reg ); |
4619 | wxRect r; | |
f85afd4e | 4620 | |
d10f4bf9 | 4621 | wxGridCellCoordsArray cellsExposed; |
f85afd4e | 4622 | |
2d66e025 MB |
4623 | int left, top, right, bottom; |
4624 | while ( iter ) | |
4625 | { | |
4626 | r = iter.GetRect(); | |
f85afd4e | 4627 | |
2d66e025 MB |
4628 | // TODO: remove this when we can... |
4629 | // There is a bug in wxMotif that gives garbage update | |
4630 | // rectangles if you jump-scroll a long way by clicking the | |
4631 | // scrollbar with middle button. This is a work-around | |
4632 | // | |
4633 | #if defined(__WXMOTIF__) | |
f85afd4e | 4634 | int cw, ch; |
2d66e025 MB |
4635 | m_gridWin->GetClientSize( &cw, &ch ); |
4636 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
4637 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
4638 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
4639 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
4640 | #endif | |
8f177c8e | 4641 | |
2d66e025 MB |
4642 | // logical bounds of update region |
4643 | // | |
4644 | CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
4645 | CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
f85afd4e | 4646 | |
2d66e025 | 4647 | // find the cells within these bounds |
f85afd4e | 4648 | // |
2d66e025 | 4649 | int row, col; |
33188aa4 | 4650 | for ( row = internalYToRow(top); row < m_numRows; row++ ) |
f85afd4e | 4651 | { |
7c1cb261 VZ |
4652 | if ( GetRowBottom(row) <= top ) |
4653 | continue; | |
f85afd4e | 4654 | |
7c1cb261 VZ |
4655 | if ( GetRowTop(row) > bottom ) |
4656 | break; | |
60ff3b99 | 4657 | |
33188aa4 | 4658 | for ( col = internalXToCol(left); col < m_numCols; col++ ) |
2d66e025 | 4659 | { |
7c1cb261 VZ |
4660 | if ( GetColRight(col) <= left ) |
4661 | continue; | |
60ff3b99 | 4662 | |
7c1cb261 VZ |
4663 | if ( GetColLeft(col) > right ) |
4664 | break; | |
60ff3b99 | 4665 | |
d10f4bf9 | 4666 | cellsExposed.Add( wxGridCellCoords( row, col ) ); |
2d66e025 MB |
4667 | } |
4668 | } | |
60ff3b99 | 4669 | |
7c1cb261 | 4670 | iter++; |
f85afd4e | 4671 | } |
d10f4bf9 VZ |
4672 | |
4673 | return cellsExposed; | |
f85afd4e MB |
4674 | } |
4675 | ||
4676 | ||
2d66e025 | 4677 | void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 4678 | { |
2d66e025 MB |
4679 | int x, y, row; |
4680 | wxPoint pos( event.GetPosition() ); | |
4681 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 4682 | |
2d66e025 | 4683 | if ( event.Dragging() ) |
f85afd4e | 4684 | { |
426b2d87 JS |
4685 | if (!m_isDragging) |
4686 | { | |
4687 | m_isDragging = TRUE; | |
4688 | m_rowLabelWin->CaptureMouse(); | |
4689 | } | |
8f177c8e | 4690 | |
2d66e025 | 4691 | if ( event.LeftIsDown() ) |
f85afd4e MB |
4692 | { |
4693 | switch( m_cursorMode ) | |
4694 | { | |
f85afd4e MB |
4695 | case WXGRID_CURSOR_RESIZE_ROW: |
4696 | { | |
2d66e025 MB |
4697 | int cw, ch, left, dummy; |
4698 | m_gridWin->GetClientSize( &cw, &ch ); | |
4699 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
60ff3b99 | 4700 | |
2d66e025 MB |
4701 | wxClientDC dc( m_gridWin ); |
4702 | PrepareDC( dc ); | |
af547d51 VZ |
4703 | y = wxMax( y, |
4704 | GetRowTop(m_dragRowOrCol) + | |
4705 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
d2fdd8d2 | 4706 | dc.SetLogicalFunction(wxINVERT); |
f85afd4e MB |
4707 | if ( m_dragLastPos >= 0 ) |
4708 | { | |
2d66e025 | 4709 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); |
f85afd4e | 4710 | } |
2d66e025 MB |
4711 | dc.DrawLine( left, y, left+cw, y ); |
4712 | m_dragLastPos = y; | |
f85afd4e MB |
4713 | } |
4714 | break; | |
4715 | ||
4716 | case WXGRID_CURSOR_SELECT_ROW: | |
e32352cf | 4717 | if ( (row = YToRow( y )) >= 0 ) |
aa5e1f75 | 4718 | { |
3f3dc2ef VZ |
4719 | if ( m_selection ) |
4720 | { | |
4721 | m_selection->SelectRow( row, | |
4722 | event.ControlDown(), | |
4723 | event.ShiftDown(), | |
4724 | event.AltDown(), | |
4725 | event.MetaDown() ); | |
4726 | } | |
f85afd4e | 4727 | } |
e2b42eeb VZ |
4728 | |
4729 | // default label to suppress warnings about "enumeration value | |
4730 | // 'xxx' not handled in switch | |
4731 | default: | |
4732 | break; | |
f85afd4e MB |
4733 | } |
4734 | } | |
4735 | return; | |
4736 | } | |
4737 | ||
426b2d87 JS |
4738 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) |
4739 | return; | |
8f177c8e | 4740 | |
426b2d87 JS |
4741 | if (m_isDragging) |
4742 | { | |
70e8d961 | 4743 | if (m_rowLabelWin->HasCapture()) m_rowLabelWin->ReleaseMouse(); |
426b2d87 JS |
4744 | m_isDragging = FALSE; |
4745 | } | |
60ff3b99 | 4746 | |
6d004f67 MB |
4747 | // ------------ Entering or leaving the window |
4748 | // | |
4749 | if ( event.Entering() || event.Leaving() ) | |
4750 | { | |
e2b42eeb | 4751 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
6d004f67 MB |
4752 | } |
4753 | ||
e2b42eeb | 4754 | |
2d66e025 | 4755 | // ------------ Left button pressed |
f85afd4e | 4756 | // |
6d004f67 | 4757 | else if ( event.LeftDown() ) |
f85afd4e | 4758 | { |
2d66e025 MB |
4759 | // don't send a label click event for a hit on the |
4760 | // edge of the row label - this is probably the user | |
4761 | // wanting to resize the row | |
4762 | // | |
4763 | if ( YToEdgeOfRow(y) < 0 ) | |
f85afd4e | 4764 | { |
2d66e025 | 4765 | row = YToRow(y); |
58dd5b3b | 4766 | if ( row >= 0 && |
b54ba671 | 4767 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) ) |
f85afd4e | 4768 | { |
aa5e1f75 SN |
4769 | if ( !event.ShiftDown() && !event.ControlDown() ) |
4770 | ClearSelection(); | |
64e15340 | 4771 | if ( m_selection ) |
3f3dc2ef VZ |
4772 | { |
4773 | if ( event.ShiftDown() ) | |
4774 | { | |
4775 | m_selection->SelectBlock( m_currentCellCoords.GetRow(), | |
4776 | 0, | |
4777 | row, | |
4778 | GetNumberCols() - 1, | |
4779 | event.ControlDown(), | |
4780 | event.ShiftDown(), | |
4781 | event.AltDown(), | |
4782 | event.MetaDown() ); | |
4783 | } | |
4784 | else | |
4785 | { | |
4786 | m_selection->SelectRow( row, | |
4787 | event.ControlDown(), | |
4788 | event.ShiftDown(), | |
4789 | event.AltDown(), | |
4790 | event.MetaDown() ); | |
4791 | } | |
4792 | } | |
4793 | ||
e2b42eeb | 4794 | ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin); |
f85afd4e | 4795 | } |
2d66e025 MB |
4796 | } |
4797 | else | |
4798 | { | |
4799 | // starting to drag-resize a row | |
4800 | // | |
6e8524b1 MB |
4801 | if ( CanDragRowSize() ) |
4802 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin); | |
2d66e025 MB |
4803 | } |
4804 | } | |
f85afd4e | 4805 | |
f85afd4e | 4806 | |
2d66e025 MB |
4807 | // ------------ Left double click |
4808 | // | |
4809 | else if (event.LeftDClick() ) | |
4810 | { | |
d43851f7 JS |
4811 | int row = YToEdgeOfRow(y); |
4812 | if ( row < 0 ) | |
2d66e025 MB |
4813 | { |
4814 | row = YToRow(y); | |
a967f048 RG |
4815 | if ( row >=0 && |
4816 | !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) ) | |
4817 | { | |
4818 | // no default action at the moment | |
4819 | } | |
f85afd4e | 4820 | } |
d43851f7 JS |
4821 | else |
4822 | { | |
4823 | // adjust row height depending on label text | |
4824 | AutoSizeRowLabelSize( row ); | |
4825 | ||
4826 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
4827 | m_dragLastPos = -1; | |
4828 | } | |
f85afd4e | 4829 | } |
60ff3b99 VZ |
4830 | |
4831 | ||
2d66e025 | 4832 | // ------------ Left button released |
f85afd4e | 4833 | // |
2d66e025 | 4834 | else if ( event.LeftUp() ) |
f85afd4e | 4835 | { |
2d66e025 | 4836 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) |
f85afd4e | 4837 | { |
6d004f67 | 4838 | DoEndDragResizeRow(); |
60ff3b99 | 4839 | |
6d004f67 MB |
4840 | // Note: we are ending the event *after* doing |
4841 | // default processing in this case | |
4842 | // | |
b54ba671 | 4843 | SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); |
2d66e025 | 4844 | } |
f85afd4e | 4845 | |
e2b42eeb VZ |
4846 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
4847 | m_dragLastPos = -1; | |
2d66e025 | 4848 | } |
f85afd4e | 4849 | |
f85afd4e | 4850 | |
2d66e025 MB |
4851 | // ------------ Right button down |
4852 | // | |
4853 | else if ( event.RightDown() ) | |
4854 | { | |
4855 | row = YToRow(y); | |
ef5df12b | 4856 | if ( row >=0 && |
a967f048 | 4857 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) |
2d66e025 MB |
4858 | { |
4859 | // no default action at the moment | |
f85afd4e MB |
4860 | } |
4861 | } | |
60ff3b99 VZ |
4862 | |
4863 | ||
2d66e025 | 4864 | // ------------ Right double click |
f85afd4e | 4865 | // |
2d66e025 MB |
4866 | else if ( event.RightDClick() ) |
4867 | { | |
4868 | row = YToRow(y); | |
a967f048 RG |
4869 | if ( row >= 0 && |
4870 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) | |
2d66e025 MB |
4871 | { |
4872 | // no default action at the moment | |
4873 | } | |
4874 | } | |
60ff3b99 VZ |
4875 | |
4876 | ||
2d66e025 | 4877 | // ------------ No buttons down and mouse moving |
f85afd4e | 4878 | // |
2d66e025 | 4879 | else if ( event.Moving() ) |
f85afd4e | 4880 | { |
2d66e025 MB |
4881 | m_dragRowOrCol = YToEdgeOfRow( y ); |
4882 | if ( m_dragRowOrCol >= 0 ) | |
8f177c8e | 4883 | { |
2d66e025 | 4884 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
8f177c8e | 4885 | { |
e2b42eeb | 4886 | // don't capture the mouse yet |
6e8524b1 MB |
4887 | if ( CanDragRowSize() ) |
4888 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE); | |
8f177c8e | 4889 | } |
2d66e025 | 4890 | } |
6d004f67 | 4891 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 4892 | { |
e2b42eeb | 4893 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE); |
8f177c8e | 4894 | } |
f85afd4e | 4895 | } |
2d66e025 MB |
4896 | } |
4897 | ||
4898 | ||
4899 | void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) | |
4900 | { | |
4901 | int x, y, col; | |
4902 | wxPoint pos( event.GetPosition() ); | |
4903 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 4904 | |
2d66e025 | 4905 | if ( event.Dragging() ) |
f85afd4e | 4906 | { |
fe77cf60 JS |
4907 | if (!m_isDragging) |
4908 | { | |
4909 | m_isDragging = TRUE; | |
4910 | m_colLabelWin->CaptureMouse(); | |
4911 | } | |
8f177c8e | 4912 | |
2d66e025 | 4913 | if ( event.LeftIsDown() ) |
8f177c8e | 4914 | { |
2d66e025 | 4915 | switch( m_cursorMode ) |
8f177c8e | 4916 | { |
2d66e025 | 4917 | case WXGRID_CURSOR_RESIZE_COL: |
8f177c8e | 4918 | { |
2d66e025 MB |
4919 | int cw, ch, dummy, top; |
4920 | m_gridWin->GetClientSize( &cw, &ch ); | |
4921 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
60ff3b99 | 4922 | |
2d66e025 MB |
4923 | wxClientDC dc( m_gridWin ); |
4924 | PrepareDC( dc ); | |
43947979 VZ |
4925 | |
4926 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + | |
4927 | GetColMinimalWidth(m_dragRowOrCol)); | |
d2fdd8d2 | 4928 | dc.SetLogicalFunction(wxINVERT); |
2d66e025 MB |
4929 | if ( m_dragLastPos >= 0 ) |
4930 | { | |
4931 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
4932 | } | |
4933 | dc.DrawLine( x, top, x, top+ch ); | |
4934 | m_dragLastPos = x; | |
f85afd4e | 4935 | } |
2d66e025 | 4936 | break; |
f85afd4e | 4937 | |
2d66e025 | 4938 | case WXGRID_CURSOR_SELECT_COL: |
e32352cf | 4939 | if ( (col = XToCol( x )) >= 0 ) |
aa5e1f75 | 4940 | { |
3f3dc2ef VZ |
4941 | if ( m_selection ) |
4942 | { | |
4943 | m_selection->SelectCol( col, | |
4944 | event.ControlDown(), | |
4945 | event.ShiftDown(), | |
4946 | event.AltDown(), | |
4947 | event.MetaDown() ); | |
4948 | } | |
2d66e025 | 4949 | } |
e2b42eeb VZ |
4950 | |
4951 | // default label to suppress warnings about "enumeration value | |
4952 | // 'xxx' not handled in switch | |
4953 | default: | |
4954 | break; | |
2d66e025 | 4955 | } |
f85afd4e | 4956 | } |
2d66e025 | 4957 | return; |
f85afd4e | 4958 | } |
2d66e025 | 4959 | |
fe77cf60 JS |
4960 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) |
4961 | return; | |
2d66e025 | 4962 | |
fe77cf60 JS |
4963 | if (m_isDragging) |
4964 | { | |
70e8d961 | 4965 | if (m_colLabelWin->HasCapture()) m_colLabelWin->ReleaseMouse(); |
fe77cf60 JS |
4966 | m_isDragging = FALSE; |
4967 | } | |
60ff3b99 | 4968 | |
6d004f67 MB |
4969 | // ------------ Entering or leaving the window |
4970 | // | |
4971 | if ( event.Entering() || event.Leaving() ) | |
4972 | { | |
e2b42eeb | 4973 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); |
6d004f67 MB |
4974 | } |
4975 | ||
e2b42eeb | 4976 | |
2d66e025 | 4977 | // ------------ Left button pressed |
f85afd4e | 4978 | // |
6d004f67 | 4979 | else if ( event.LeftDown() ) |
f85afd4e | 4980 | { |
2d66e025 MB |
4981 | // don't send a label click event for a hit on the |
4982 | // edge of the col label - this is probably the user | |
4983 | // wanting to resize the col | |
4984 | // | |
4985 | if ( XToEdgeOfCol(x) < 0 ) | |
8f177c8e | 4986 | { |
2d66e025 | 4987 | col = XToCol(x); |
58dd5b3b | 4988 | if ( col >= 0 && |
b54ba671 | 4989 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) ) |
8f177c8e | 4990 | { |
aa5e1f75 SN |
4991 | if ( !event.ShiftDown() && !event.ControlDown() ) |
4992 | ClearSelection(); | |
3f3dc2ef VZ |
4993 | if ( m_selection ) |
4994 | { | |
4995 | if ( event.ShiftDown() ) | |
4996 | { | |
4997 | m_selection->SelectBlock( 0, | |
4998 | m_currentCellCoords.GetCol(), | |
4999 | GetNumberRows() - 1, col, | |
5000 | event.ControlDown(), | |
5001 | event.ShiftDown(), | |
5002 | event.AltDown(), | |
5003 | event.MetaDown() ); | |
5004 | } | |
5005 | else | |
5006 | { | |
5007 | m_selection->SelectCol( col, | |
5008 | event.ControlDown(), | |
5009 | event.ShiftDown(), | |
5010 | event.AltDown(), | |
5011 | event.MetaDown() ); | |
5012 | } | |
5013 | } | |
5014 | ||
e2b42eeb | 5015 | ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin); |
f85afd4e | 5016 | } |
2d66e025 MB |
5017 | } |
5018 | else | |
5019 | { | |
5020 | // starting to drag-resize a col | |
5021 | // | |
6e8524b1 MB |
5022 | if ( CanDragColSize() ) |
5023 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin); | |
2d66e025 MB |
5024 | } |
5025 | } | |
f85afd4e | 5026 | |
f85afd4e | 5027 | |
2d66e025 MB |
5028 | // ------------ Left double click |
5029 | // | |
5030 | if ( event.LeftDClick() ) | |
5031 | { | |
d43851f7 JS |
5032 | int col = XToEdgeOfCol(x); |
5033 | if ( col < 0 ) | |
2d66e025 MB |
5034 | { |
5035 | col = XToCol(x); | |
a967f048 RG |
5036 | if ( col >= 0 && |
5037 | ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) ) | |
5038 | { | |
5039 | // no default action at the moment | |
5040 | } | |
2d66e025 | 5041 | } |
d43851f7 JS |
5042 | else |
5043 | { | |
5044 | // adjust column width depending on label text | |
5045 | AutoSizeColLabelSize( col ); | |
5046 | ||
5047 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
5048 | m_dragLastPos = -1; | |
5049 | } | |
2d66e025 | 5050 | } |
60ff3b99 VZ |
5051 | |
5052 | ||
2d66e025 MB |
5053 | // ------------ Left button released |
5054 | // | |
5055 | else if ( event.LeftUp() ) | |
5056 | { | |
5057 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
5058 | { | |
6d004f67 | 5059 | DoEndDragResizeCol(); |
e2b42eeb | 5060 | |
6d004f67 MB |
5061 | // Note: we are ending the event *after* doing |
5062 | // default processing in this case | |
5063 | // | |
b54ba671 | 5064 | SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); |
2d66e025 | 5065 | } |
f85afd4e | 5066 | |
e2b42eeb | 5067 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); |
2d66e025 | 5068 | m_dragLastPos = -1; |
60ff3b99 VZ |
5069 | } |
5070 | ||
5071 | ||
2d66e025 MB |
5072 | // ------------ Right button down |
5073 | // | |
5074 | else if ( event.RightDown() ) | |
5075 | { | |
5076 | col = XToCol(x); | |
a967f048 RG |
5077 | if ( col >= 0 && |
5078 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) | |
2d66e025 MB |
5079 | { |
5080 | // no default action at the moment | |
f85afd4e MB |
5081 | } |
5082 | } | |
60ff3b99 VZ |
5083 | |
5084 | ||
2d66e025 | 5085 | // ------------ Right double click |
f85afd4e | 5086 | // |
2d66e025 MB |
5087 | else if ( event.RightDClick() ) |
5088 | { | |
5089 | col = XToCol(x); | |
a967f048 RG |
5090 | if ( col >= 0 && |
5091 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) | |
2d66e025 MB |
5092 | { |
5093 | // no default action at the moment | |
5094 | } | |
5095 | } | |
60ff3b99 VZ |
5096 | |
5097 | ||
2d66e025 | 5098 | // ------------ No buttons down and mouse moving |
f85afd4e | 5099 | // |
2d66e025 | 5100 | else if ( event.Moving() ) |
f85afd4e | 5101 | { |
2d66e025 MB |
5102 | m_dragRowOrCol = XToEdgeOfCol( x ); |
5103 | if ( m_dragRowOrCol >= 0 ) | |
f85afd4e | 5104 | { |
2d66e025 | 5105 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
f85afd4e | 5106 | { |
e2b42eeb | 5107 | // don't capture the cursor yet |
6e8524b1 MB |
5108 | if ( CanDragColSize() ) |
5109 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE); | |
f85afd4e | 5110 | } |
2d66e025 | 5111 | } |
6d004f67 | 5112 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 5113 | { |
e2b42eeb | 5114 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE); |
8f177c8e | 5115 | } |
f85afd4e MB |
5116 | } |
5117 | } | |
5118 | ||
5119 | ||
2d66e025 | 5120 | void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 5121 | { |
2d66e025 | 5122 | if ( event.LeftDown() ) |
f85afd4e | 5123 | { |
2d66e025 MB |
5124 | // indicate corner label by having both row and |
5125 | // col args == -1 | |
f85afd4e | 5126 | // |
b54ba671 | 5127 | if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) ) |
2d66e025 MB |
5128 | { |
5129 | SelectAll(); | |
5130 | } | |
f85afd4e MB |
5131 | } |
5132 | ||
2d66e025 MB |
5133 | else if ( event.LeftDClick() ) |
5134 | { | |
b54ba671 | 5135 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event ); |
2d66e025 | 5136 | } |
8f177c8e | 5137 | |
2d66e025 | 5138 | else if ( event.RightDown() ) |
f85afd4e | 5139 | { |
b54ba671 | 5140 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) ) |
f85afd4e | 5141 | { |
2d66e025 MB |
5142 | // no default action at the moment |
5143 | } | |
5144 | } | |
f85afd4e | 5145 | |
2d66e025 MB |
5146 | else if ( event.RightDClick() ) |
5147 | { | |
b54ba671 | 5148 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) ) |
2d66e025 MB |
5149 | { |
5150 | // no default action at the moment | |
5151 | } | |
5152 | } | |
5153 | } | |
f85afd4e | 5154 | |
e2b42eeb VZ |
5155 | void wxGrid::ChangeCursorMode(CursorMode mode, |
5156 | wxWindow *win, | |
5157 | bool captureMouse) | |
5158 | { | |
5159 | #ifdef __WXDEBUG__ | |
5160 | static const wxChar *cursorModes[] = | |
5161 | { | |
5162 | _T("SELECT_CELL"), | |
5163 | _T("RESIZE_ROW"), | |
5164 | _T("RESIZE_COL"), | |
5165 | _T("SELECT_ROW"), | |
5166 | _T("SELECT_COL") | |
5167 | }; | |
5168 | ||
181bfffd VZ |
5169 | wxLogTrace(_T("grid"), |
5170 | _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"), | |
e2b42eeb VZ |
5171 | win == m_colLabelWin ? _T("colLabelWin") |
5172 | : win ? _T("rowLabelWin") | |
5173 | : _T("gridWin"), | |
5174 | cursorModes[m_cursorMode], cursorModes[mode]); | |
5175 | #endif // __WXDEBUG__ | |
5176 | ||
faec5a43 SN |
5177 | if ( mode == m_cursorMode && |
5178 | win == m_winCapture && | |
5179 | captureMouse == (m_winCapture != NULL)) | |
e2b42eeb VZ |
5180 | return; |
5181 | ||
5182 | if ( !win ) | |
5183 | { | |
5184 | // by default use the grid itself | |
5185 | win = m_gridWin; | |
5186 | } | |
5187 | ||
5188 | if ( m_winCapture ) | |
5189 | { | |
70e8d961 | 5190 | if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse(); |
e2b42eeb VZ |
5191 | m_winCapture = (wxWindow *)NULL; |
5192 | } | |
5193 | ||
5194 | m_cursorMode = mode; | |
5195 | ||
5196 | switch ( m_cursorMode ) | |
5197 | { | |
5198 | case WXGRID_CURSOR_RESIZE_ROW: | |
5199 | win->SetCursor( m_rowResizeCursor ); | |
5200 | break; | |
5201 | ||
5202 | case WXGRID_CURSOR_RESIZE_COL: | |
5203 | win->SetCursor( m_colResizeCursor ); | |
5204 | break; | |
5205 | ||
5206 | default: | |
5207 | win->SetCursor( *wxSTANDARD_CURSOR ); | |
5208 | } | |
5209 | ||
5210 | // we need to capture mouse when resizing | |
5211 | bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW || | |
5212 | m_cursorMode == WXGRID_CURSOR_RESIZE_COL; | |
5213 | ||
5214 | if ( captureMouse && resize ) | |
5215 | { | |
5216 | win->CaptureMouse(); | |
5217 | m_winCapture = win; | |
5218 | } | |
5219 | } | |
8f177c8e | 5220 | |
2d66e025 MB |
5221 | void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) |
5222 | { | |
5223 | int x, y; | |
5224 | wxPoint pos( event.GetPosition() ); | |
5225 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 5226 | |
2d66e025 MB |
5227 | wxGridCellCoords coords; |
5228 | XYToCell( x, y, coords ); | |
60ff3b99 | 5229 | |
27f35b66 SN |
5230 | int cell_rows, cell_cols; |
5231 | GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols ); | |
5232 | if ((cell_rows < 0) || (cell_cols < 0)) | |
5233 | { | |
5234 | coords.SetRow(coords.GetRow() + cell_rows); | |
5235 | coords.SetCol(coords.GetCol() + cell_cols); | |
5236 | } | |
5237 | ||
2d66e025 MB |
5238 | if ( event.Dragging() ) |
5239 | { | |
07296f0b RD |
5240 | //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol()); |
5241 | ||
5242 | // Don't start doing anything until the mouse has been drug at | |
5243 | // least 3 pixels in any direction... | |
508011ce VZ |
5244 | if (! m_isDragging) |
5245 | { | |
5246 | if (m_startDragPos == wxDefaultPosition) | |
5247 | { | |
07296f0b RD |
5248 | m_startDragPos = pos; |
5249 | return; | |
5250 | } | |
5251 | if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4) | |
5252 | return; | |
5253 | } | |
5254 | ||
2d66e025 | 5255 | m_isDragging = TRUE; |
2d66e025 MB |
5256 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
5257 | { | |
f0102d2a VZ |
5258 | // Hide the edit control, so it |
5259 | // won't interfer with drag-shrinking. | |
f6bcfd97 | 5260 | if ( IsCellEditControlShown() ) |
a5777624 | 5261 | { |
f0102d2a | 5262 | HideCellEditControl(); |
a5777624 RD |
5263 | SaveEditControlValue(); |
5264 | } | |
07296f0b RD |
5265 | |
5266 | // Have we captured the mouse yet? | |
508011ce VZ |
5267 | if (! m_winCapture) |
5268 | { | |
07296f0b RD |
5269 | m_winCapture = m_gridWin; |
5270 | m_winCapture->CaptureMouse(); | |
5271 | } | |
5272 | ||
2d66e025 MB |
5273 | if ( coords != wxGridNoCellCoords ) |
5274 | { | |
aa5e1f75 SN |
5275 | if ( event.ControlDown() ) |
5276 | { | |
5277 | if ( m_selectingKeyboard == wxGridNoCellCoords) | |
5278 | m_selectingKeyboard = coords; | |
c9097836 | 5279 | HighlightBlock ( m_selectingKeyboard, coords ); |
aa5e1f75 SN |
5280 | } |
5281 | else | |
5282 | { | |
5283 | if ( !IsSelection() ) | |
5284 | { | |
c9097836 | 5285 | HighlightBlock( coords, coords ); |
aa5e1f75 SN |
5286 | } |
5287 | else | |
5288 | { | |
c9097836 | 5289 | HighlightBlock( m_currentCellCoords, coords ); |
aa5e1f75 | 5290 | } |
f85afd4e | 5291 | } |
07296f0b | 5292 | |
508011ce VZ |
5293 | if (! IsVisible(coords)) |
5294 | { | |
07296f0b RD |
5295 | MakeCellVisible(coords); |
5296 | // TODO: need to introduce a delay or something here. The | |
e32352cf | 5297 | // scrolling is way to fast, at least on MSW - also on GTK. |
07296f0b | 5298 | } |
2d66e025 MB |
5299 | } |
5300 | } | |
6d004f67 MB |
5301 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) |
5302 | { | |
5303 | int cw, ch, left, dummy; | |
5304 | m_gridWin->GetClientSize( &cw, &ch ); | |
5305 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
8f177c8e | 5306 | |
6d004f67 MB |
5307 | wxClientDC dc( m_gridWin ); |
5308 | PrepareDC( dc ); | |
a95e38c0 VZ |
5309 | y = wxMax( y, GetRowTop(m_dragRowOrCol) + |
5310 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
6d004f67 MB |
5311 | dc.SetLogicalFunction(wxINVERT); |
5312 | if ( m_dragLastPos >= 0 ) | |
5313 | { | |
5314 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
5315 | } | |
5316 | dc.DrawLine( left, y, left+cw, y ); | |
5317 | m_dragLastPos = y; | |
5318 | } | |
5319 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
5320 | { | |
5321 | int cw, ch, dummy, top; | |
5322 | m_gridWin->GetClientSize( &cw, &ch ); | |
5323 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
e2b42eeb | 5324 | |
6d004f67 MB |
5325 | wxClientDC dc( m_gridWin ); |
5326 | PrepareDC( dc ); | |
43947979 VZ |
5327 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + |
5328 | GetColMinimalWidth(m_dragRowOrCol) ); | |
6d004f67 MB |
5329 | dc.SetLogicalFunction(wxINVERT); |
5330 | if ( m_dragLastPos >= 0 ) | |
5331 | { | |
5332 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
5333 | } | |
5334 | dc.DrawLine( x, top, x, top+ch ); | |
5335 | m_dragLastPos = x; | |
5336 | } | |
e2b42eeb | 5337 | |
2d66e025 MB |
5338 | return; |
5339 | } | |
66242c80 | 5340 | |
2d66e025 | 5341 | m_isDragging = FALSE; |
07296f0b RD |
5342 | m_startDragPos = wxDefaultPosition; |
5343 | ||
a5777624 RD |
5344 | // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL |
5345 | // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under | |
5346 | // wxGTK | |
e2b42eeb | 5347 | #if 0 |
a5777624 RD |
5348 | if ( event.Entering() || event.Leaving() ) |
5349 | { | |
5350 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5351 | m_gridWin->SetCursor( *wxSTANDARD_CURSOR ); | |
5352 | } | |
5353 | else | |
e2b42eeb VZ |
5354 | #endif // 0 |
5355 | ||
a5777624 RD |
5356 | // ------------ Left button pressed |
5357 | // | |
5358 | if ( event.LeftDown() && coords != wxGridNoCellCoords ) | |
f6bcfd97 BP |
5359 | { |
5360 | if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK, | |
5361 | coords.GetRow(), | |
5362 | coords.GetCol(), | |
5363 | event ) ) | |
a5777624 | 5364 | { |
f6bcfd97 BP |
5365 | if ( !event.ControlDown() ) |
5366 | ClearSelection(); | |
5367 | if ( event.ShiftDown() ) | |
5368 | { | |
3f3dc2ef VZ |
5369 | if ( m_selection ) |
5370 | { | |
5371 | m_selection->SelectBlock( m_currentCellCoords.GetRow(), | |
5372 | m_currentCellCoords.GetCol(), | |
5373 | coords.GetRow(), | |
5374 | coords.GetCol(), | |
5375 | event.ControlDown(), | |
5376 | event.ShiftDown(), | |
5377 | event.AltDown(), | |
5378 | event.MetaDown() ); | |
5379 | } | |
f6bcfd97 BP |
5380 | } |
5381 | else if ( XToEdgeOfCol(x) < 0 && | |
5382 | YToEdgeOfRow(y) < 0 ) | |
58dd5b3b | 5383 | { |
a5777624 RD |
5384 | DisableCellEditControl(); |
5385 | MakeCellVisible( coords ); | |
5386 | ||
73145b0e | 5387 | if ( event.ControlDown() ) |
58dd5b3b | 5388 | { |
73145b0e JS |
5389 | if ( m_selection ) |
5390 | { | |
5391 | m_selection->ToggleCellSelection( coords.GetRow(), | |
5392 | coords.GetCol(), | |
5393 | event.ControlDown(), | |
5394 | event.ShiftDown(), | |
5395 | event.AltDown(), | |
5396 | event.MetaDown() ); | |
5397 | } | |
5398 | m_selectingTopLeft = wxGridNoCellCoords; | |
5399 | m_selectingBottomRight = wxGridNoCellCoords; | |
5400 | m_selectingKeyboard = coords; | |
a5777624 RD |
5401 | } |
5402 | else | |
5403 | { | |
73145b0e JS |
5404 | m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords; |
5405 | SetCurrentCell( coords ); | |
5406 | if ( m_selection ) | |
aa5e1f75 | 5407 | { |
73145b0e JS |
5408 | if ( m_selection->GetSelectionMode() != |
5409 | wxGrid::wxGridSelectCells ) | |
3f3dc2ef | 5410 | { |
73145b0e | 5411 | HighlightBlock( coords, coords ); |
3f3dc2ef | 5412 | } |
aa5e1f75 | 5413 | } |
58dd5b3b MB |
5414 | } |
5415 | } | |
f85afd4e | 5416 | } |
a5777624 | 5417 | } |
f85afd4e | 5418 | |
f85afd4e | 5419 | |
a5777624 RD |
5420 | // ------------ Left double click |
5421 | // | |
5422 | else if ( event.LeftDClick() && coords != wxGridNoCellCoords ) | |
5423 | { | |
5424 | DisableCellEditControl(); | |
5425 | ||
5426 | if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 ) | |
58dd5b3b | 5427 | { |
a5777624 RD |
5428 | SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK, |
5429 | coords.GetRow(), | |
5430 | coords.GetCol(), | |
5431 | event ); | |
58dd5b3b | 5432 | } |
a5777624 | 5433 | } |
f85afd4e | 5434 | |
8f177c8e | 5435 | |
a5777624 RD |
5436 | // ------------ Left button released |
5437 | // | |
5438 | else if ( event.LeftUp() ) | |
5439 | { | |
5440 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
f85afd4e | 5441 | { |
f40f9976 JS |
5442 | if (m_winCapture) |
5443 | { | |
5444 | if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse(); | |
5445 | m_winCapture = NULL; | |
5446 | } | |
5447 | ||
932b55d0 JS |
5448 | if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl()) |
5449 | { | |
5450 | ClearSelection(); | |
5451 | EnableCellEditControl(); | |
5452 | ||
5453 | wxGridCellAttr* attr = GetCellAttr(coords); | |
5454 | wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol()); | |
5455 | editor->StartingClick(); | |
5456 | editor->DecRef(); | |
5457 | attr->DecRef(); | |
5458 | ||
5459 | m_waitForSlowClick = FALSE; | |
5460 | } | |
5461 | else if ( m_selectingTopLeft != wxGridNoCellCoords && | |
b5808881 | 5462 | m_selectingBottomRight != wxGridNoCellCoords ) |
52068ea5 | 5463 | { |
3f3dc2ef VZ |
5464 | if ( m_selection ) |
5465 | { | |
5466 | m_selection->SelectBlock( m_selectingTopLeft.GetRow(), | |
5467 | m_selectingTopLeft.GetCol(), | |
5468 | m_selectingBottomRight.GetRow(), | |
5469 | m_selectingBottomRight.GetCol(), | |
5470 | event.ControlDown(), | |
5471 | event.ShiftDown(), | |
5472 | event.AltDown(), | |
5473 | event.MetaDown() ); | |
5474 | } | |
5475 | ||
5c8fc7c1 SN |
5476 | m_selectingTopLeft = wxGridNoCellCoords; |
5477 | m_selectingBottomRight = wxGridNoCellCoords; | |
2d66e025 | 5478 | |
73145b0e JS |
5479 | // Show the edit control, if it has been hidden for |
5480 | // drag-shrinking. | |
5481 | ShowCellEditControl(); | |
5482 | } | |
a5777624 RD |
5483 | } |
5484 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
5485 | { | |
5486 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5487 | DoEndDragResizeRow(); | |
e2b42eeb | 5488 | |
a5777624 RD |
5489 | // Note: we are ending the event *after* doing |
5490 | // default processing in this case | |
5491 | // | |
5492 | SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); | |
5493 | } | |
5494 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
5495 | { | |
5496 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5497 | DoEndDragResizeCol(); | |
e2b42eeb | 5498 | |
a5777624 RD |
5499 | // Note: we are ending the event *after* doing |
5500 | // default processing in this case | |
5501 | // | |
5502 | SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); | |
58dd5b3b | 5503 | } |
f85afd4e | 5504 | |
a5777624 RD |
5505 | m_dragLastPos = -1; |
5506 | } | |
5507 | ||
f85afd4e | 5508 | |
a5777624 RD |
5509 | // ------------ Right button down |
5510 | // | |
5511 | else if ( event.RightDown() && coords != wxGridNoCellCoords ) | |
5512 | { | |
5513 | DisableCellEditControl(); | |
5514 | if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK, | |
5515 | coords.GetRow(), | |
5516 | coords.GetCol(), | |
5517 | event ) ) | |
f85afd4e | 5518 | { |
a5777624 | 5519 | // no default action at the moment |
60ff3b99 | 5520 | } |
a5777624 | 5521 | } |
2d66e025 | 5522 | |
60ff3b99 | 5523 | |
a5777624 RD |
5524 | // ------------ Right double click |
5525 | // | |
5526 | else if ( event.RightDClick() && coords != wxGridNoCellCoords ) | |
5527 | { | |
5528 | DisableCellEditControl(); | |
5529 | if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK, | |
5530 | coords.GetRow(), | |
5531 | coords.GetCol(), | |
5532 | event ) ) | |
f85afd4e | 5533 | { |
a5777624 | 5534 | // no default action at the moment |
60ff3b99 | 5535 | } |
a5777624 RD |
5536 | } |
5537 | ||
5538 | // ------------ Moving and no button action | |
5539 | // | |
5540 | else if ( event.Moving() && !event.IsButton() ) | |
5541 | { | |
d57ad377 SN |
5542 | if( coords.GetRow() < 0 || coords.GetCol() < 0 ) |
5543 | { | |
5544 | // out of grid cell area | |
5545 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5546 | return; | |
5547 | } | |
5548 | ||
a5777624 RD |
5549 | int dragRow = YToEdgeOfRow( y ); |
5550 | int dragCol = XToEdgeOfCol( x ); | |
790cc417 | 5551 | |
a5777624 RD |
5552 | // Dragging on the corner of a cell to resize in both |
5553 | // directions is not implemented yet... | |
790cc417 | 5554 | // |
a5777624 | 5555 | if ( dragRow >= 0 && dragCol >= 0 ) |
790cc417 | 5556 | { |
a5777624 RD |
5557 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); |
5558 | return; | |
5559 | } | |
6d004f67 | 5560 | |
d57ad377 | 5561 | if ( dragRow >= 0 ) |
a5777624 RD |
5562 | { |
5563 | m_dragRowOrCol = dragRow; | |
6d004f67 | 5564 | |
a5777624 | 5565 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
6d004f67 | 5566 | { |
a5777624 RD |
5567 | if ( CanDragRowSize() && CanDragGridSize() ) |
5568 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW); | |
6d004f67 | 5569 | } |
e2b42eeb | 5570 | |
b94ae1ea VZ |
5571 | if ( dragCol >= 0 ) |
5572 | { | |
5573 | m_dragRowOrCol = dragCol; | |
122603f1 | 5574 | } |
b94ae1ea | 5575 | |
a5777624 RD |
5576 | return; |
5577 | } | |
e2b42eeb | 5578 | |
d57ad377 | 5579 | if ( dragCol >= 0 ) |
a5777624 RD |
5580 | { |
5581 | m_dragRowOrCol = dragCol; | |
e2b42eeb | 5582 | |
a5777624 | 5583 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
6d004f67 | 5584 | { |
a5777624 RD |
5585 | if ( CanDragColSize() && CanDragGridSize() ) |
5586 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL); | |
6d004f67 | 5587 | } |
a5777624 RD |
5588 | |
5589 | return; | |
6d004f67 | 5590 | } |
a5777624 RD |
5591 | |
5592 | // Neither on a row or col edge | |
5593 | // | |
5594 | if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) | |
5595 | { | |
5596 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5597 | } | |
5598 | } | |
6d004f67 MB |
5599 | } |
5600 | ||
5601 | ||
5602 | void wxGrid::DoEndDragResizeRow() | |
5603 | { | |
5604 | if ( m_dragLastPos >= 0 ) | |
5605 | { | |
5606 | // erase the last line and resize the row | |
5607 | // | |
5608 | int cw, ch, left, dummy; | |
5609 | m_gridWin->GetClientSize( &cw, &ch ); | |
5610 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
5611 | ||
5612 | wxClientDC dc( m_gridWin ); | |
5613 | PrepareDC( dc ); | |
5614 | dc.SetLogicalFunction( wxINVERT ); | |
5615 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
5616 | HideCellEditControl(); | |
a5777624 | 5617 | SaveEditControlValue(); |
6d004f67 | 5618 | |
7c1cb261 | 5619 | int rowTop = GetRowTop(m_dragRowOrCol); |
6d004f67 | 5620 | SetRowSize( m_dragRowOrCol, |
b8d24d4e | 5621 | wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) ); |
e2b42eeb | 5622 | |
6d004f67 MB |
5623 | if ( !GetBatchCount() ) |
5624 | { | |
5625 | // Only needed to get the correct rect.y: | |
5626 | wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) ); | |
5627 | rect.x = 0; | |
5628 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
5629 | rect.width = m_rowLabelWidth; | |
5630 | rect.height = ch - rect.y; | |
5631 | m_rowLabelWin->Refresh( TRUE, &rect ); | |
5632 | rect.width = cw; | |
27f35b66 SN |
5633 | // if there is a multicell block, paint all of it |
5634 | if (m_table) | |
5635 | { | |
5636 | int i, cell_rows, cell_cols, subtract_rows = 0; | |
5637 | int leftCol = XToCol(left); | |
a967f048 | 5638 | int rightCol = internalXToCol(left+cw); |
27f35b66 SN |
5639 | if (leftCol >= 0) |
5640 | { | |
27f35b66 SN |
5641 | for (i=leftCol; i<rightCol; i++) |
5642 | { | |
5643 | GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols); | |
5644 | if (cell_rows < subtract_rows) | |
5645 | subtract_rows = cell_rows; | |
5646 | } | |
5647 | rect.y = GetRowTop(m_dragRowOrCol + subtract_rows); | |
5648 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
5649 | rect.height = ch - rect.y; | |
5650 | } | |
5651 | } | |
6d004f67 MB |
5652 | m_gridWin->Refresh( FALSE, &rect ); |
5653 | } | |
5654 | ||
5655 | ShowCellEditControl(); | |
5656 | } | |
5657 | } | |
5658 | ||
5659 | ||
5660 | void wxGrid::DoEndDragResizeCol() | |
5661 | { | |
5662 | if ( m_dragLastPos >= 0 ) | |
5663 | { | |
5664 | // erase the last line and resize the col | |
5665 | // | |
5666 | int cw, ch, dummy, top; | |
5667 | m_gridWin->GetClientSize( &cw, &ch ); | |
5668 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
5669 | ||
5670 | wxClientDC dc( m_gridWin ); | |
5671 | PrepareDC( dc ); | |
5672 | dc.SetLogicalFunction( wxINVERT ); | |
5673 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
5674 | HideCellEditControl(); | |
a5777624 | 5675 | SaveEditControlValue(); |
6d004f67 | 5676 | |
7c1cb261 | 5677 | int colLeft = GetColLeft(m_dragRowOrCol); |
6d004f67 | 5678 | SetColSize( m_dragRowOrCol, |
43947979 VZ |
5679 | wxMax( m_dragLastPos - colLeft, |
5680 | GetColMinimalWidth(m_dragRowOrCol) ) ); | |
6d004f67 MB |
5681 | |
5682 | if ( !GetBatchCount() ) | |
5683 | { | |
5684 | // Only needed to get the correct rect.x: | |
5685 | wxRect rect ( CellToRect( 0, m_dragRowOrCol ) ); | |
5686 | rect.y = 0; | |
5687 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
5688 | rect.width = cw - rect.x; | |
5689 | rect.height = m_colLabelHeight; | |
5690 | m_colLabelWin->Refresh( TRUE, &rect ); | |
5691 | rect.height = ch; | |
27f35b66 SN |
5692 | // if there is a multicell block, paint all of it |
5693 | if (m_table) | |
5694 | { | |
5695 | int i, cell_rows, cell_cols, subtract_cols = 0; | |
5696 | int topRow = YToRow(top); | |
a967f048 | 5697 | int bottomRow = internalYToRow(top+cw); |
27f35b66 SN |
5698 | if (topRow >= 0) |
5699 | { | |
27f35b66 SN |
5700 | for (i=topRow; i<bottomRow; i++) |
5701 | { | |
5702 | GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols); | |
5703 | if (cell_cols < subtract_cols) | |
5704 | subtract_cols = cell_cols; | |
5705 | } | |
5706 | rect.x = GetColLeft(m_dragRowOrCol + subtract_cols); | |
5707 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
5708 | rect.width = cw - rect.x; | |
5709 | } | |
5710 | } | |
6d004f67 | 5711 | m_gridWin->Refresh( FALSE, &rect ); |
790cc417 | 5712 | } |
e2b42eeb | 5713 | |
6d004f67 | 5714 | ShowCellEditControl(); |
f85afd4e | 5715 | } |
f85afd4e MB |
5716 | } |
5717 | ||
5718 | ||
6d004f67 | 5719 | |
2d66e025 MB |
5720 | // |
5721 | // ------ interaction with data model | |
5722 | // | |
5723 | bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg ) | |
f85afd4e | 5724 | { |
2d66e025 | 5725 | switch ( msg.GetId() ) |
17732cec | 5726 | { |
2d66e025 MB |
5727 | case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES: |
5728 | return GetModelValues(); | |
17732cec | 5729 | |
2d66e025 MB |
5730 | case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES: |
5731 | return SetModelValues(); | |
f85afd4e | 5732 | |
2d66e025 MB |
5733 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: |
5734 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
5735 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
5736 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
5737 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
5738 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
5739 | return Redimension( msg ); | |
5740 | ||
5741 | default: | |
5742 | return FALSE; | |
f85afd4e | 5743 | } |
2d66e025 | 5744 | } |
f85afd4e | 5745 | |
f85afd4e | 5746 | |
f85afd4e | 5747 | |
2d66e025 MB |
5748 | // The behaviour of this function depends on the grid table class |
5749 | // Clear() function. For the default wxGridStringTable class the | |
5750 | // behavious is to replace all cell contents with wxEmptyString but | |
5751 | // not to change the number of rows or cols. | |
5752 | // | |
5753 | void wxGrid::ClearGrid() | |
5754 | { | |
5755 | if ( m_table ) | |
f85afd4e | 5756 | { |
4cfa5de6 RD |
5757 | if (IsCellEditControlEnabled()) |
5758 | DisableCellEditControl(); | |
5759 | ||
2d66e025 | 5760 | m_table->Clear(); |
1f1ce288 | 5761 | if ( !GetBatchCount() ) m_gridWin->Refresh(); |
f85afd4e MB |
5762 | } |
5763 | } | |
5764 | ||
5765 | ||
2d66e025 | 5766 | bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) |
f85afd4e | 5767 | { |
2d66e025 | 5768 | // TODO: something with updateLabels flag |
8f177c8e | 5769 | |
2d66e025 | 5770 | if ( !m_created ) |
f85afd4e | 5771 | { |
bd3c6758 | 5772 | wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") ); |
2d66e025 MB |
5773 | return FALSE; |
5774 | } | |
8f177c8e | 5775 | |
2d66e025 MB |
5776 | if ( m_table ) |
5777 | { | |
b7fff980 | 5778 | if (IsCellEditControlEnabled()) |
b54ba671 | 5779 | DisableCellEditControl(); |
b7fff980 | 5780 | |
4ea3479c | 5781 | bool done = m_table->InsertRows( pos, numRows ); |
4ea3479c | 5782 | return done; |
f85afd4e | 5783 | |
2d66e025 MB |
5784 | // the table will have sent the results of the insert row |
5785 | // operation to this view object as a grid table message | |
f85afd4e | 5786 | } |
f6bcfd97 | 5787 | return FALSE; |
f85afd4e MB |
5788 | } |
5789 | ||
5790 | ||
2d66e025 | 5791 | bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) ) |
f85afd4e | 5792 | { |
2d66e025 MB |
5793 | // TODO: something with updateLabels flag |
5794 | ||
5795 | if ( !m_created ) | |
f85afd4e | 5796 | { |
bd3c6758 | 5797 | wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") ); |
2d66e025 MB |
5798 | return FALSE; |
5799 | } | |
5800 | ||
4ea3479c JS |
5801 | if ( m_table ) |
5802 | { | |
5803 | bool done = m_table && m_table->AppendRows( numRows ); | |
4ea3479c JS |
5804 | return done; |
5805 | // the table will have sent the results of the append row | |
5806 | // operation to this view object as a grid table message | |
5807 | } | |
5808 | return FALSE; | |
f85afd4e MB |
5809 | } |
5810 | ||
2d66e025 MB |
5811 | |
5812 | bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 5813 | { |
2d66e025 MB |
5814 | // TODO: something with updateLabels flag |
5815 | ||
5816 | if ( !m_created ) | |
f85afd4e | 5817 | { |
bd3c6758 | 5818 | wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") ); |
2d66e025 MB |
5819 | return FALSE; |
5820 | } | |
5821 | ||
b7fff980 | 5822 | if ( m_table ) |
2d66e025 | 5823 | { |
b7fff980 | 5824 | if (IsCellEditControlEnabled()) |
b54ba671 | 5825 | DisableCellEditControl(); |
f85afd4e | 5826 | |
4ea3479c | 5827 | bool done = m_table->DeleteRows( pos, numRows ); |
4ea3479c | 5828 | return done; |
f6bcfd97 BP |
5829 | // the table will have sent the results of the delete row |
5830 | // operation to this view object as a grid table message | |
2d66e025 | 5831 | } |
b7fff980 | 5832 | return FALSE; |
2d66e025 | 5833 | } |
f85afd4e | 5834 | |
f85afd4e | 5835 | |
2d66e025 MB |
5836 | bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) |
5837 | { | |
5838 | // TODO: something with updateLabels flag | |
8f177c8e | 5839 | |
2d66e025 MB |
5840 | if ( !m_created ) |
5841 | { | |
bd3c6758 | 5842 | wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") ); |
2d66e025 MB |
5843 | return FALSE; |
5844 | } | |
f85afd4e | 5845 | |
2d66e025 MB |
5846 | if ( m_table ) |
5847 | { | |
b7fff980 | 5848 | if (IsCellEditControlEnabled()) |
b54ba671 | 5849 | DisableCellEditControl(); |
b7fff980 | 5850 | |
4ea3479c | 5851 | bool done = m_table->InsertCols( pos, numCols ); |
4ea3479c | 5852 | return done; |
2d66e025 MB |
5853 | // the table will have sent the results of the insert col |
5854 | // operation to this view object as a grid table message | |
f85afd4e | 5855 | } |
f6bcfd97 | 5856 | return FALSE; |
f85afd4e MB |
5857 | } |
5858 | ||
2d66e025 MB |
5859 | |
5860 | bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 5861 | { |
2d66e025 MB |
5862 | // TODO: something with updateLabels flag |
5863 | ||
5864 | if ( !m_created ) | |
f85afd4e | 5865 | { |
bd3c6758 | 5866 | wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") ); |
2d66e025 MB |
5867 | return FALSE; |
5868 | } | |
f85afd4e | 5869 | |
4ea3479c JS |
5870 | if ( m_table ) |
5871 | { | |
5872 | bool done = m_table->AppendCols( numCols ); | |
4ea3479c JS |
5873 | return done; |
5874 | // the table will have sent the results of the append col | |
5875 | // operation to this view object as a grid table message | |
5876 | } | |
5877 | return FALSE; | |
f85afd4e MB |
5878 | } |
5879 | ||
5880 | ||
2d66e025 | 5881 | bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) |
f85afd4e | 5882 | { |
2d66e025 | 5883 | // TODO: something with updateLabels flag |
8f177c8e | 5884 | |
2d66e025 | 5885 | if ( !m_created ) |
f85afd4e | 5886 | { |
bd3c6758 | 5887 | wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") ); |
2d66e025 | 5888 | return FALSE; |
f85afd4e MB |
5889 | } |
5890 | ||
b7fff980 | 5891 | if ( m_table ) |
2d66e025 | 5892 | { |
b7fff980 | 5893 | if (IsCellEditControlEnabled()) |
b54ba671 | 5894 | DisableCellEditControl(); |
8f177c8e | 5895 | |
4ea3479c | 5896 | bool done = m_table->DeleteCols( pos, numCols ); |
4ea3479c | 5897 | return done; |
f6bcfd97 BP |
5898 | // the table will have sent the results of the delete col |
5899 | // operation to this view object as a grid table message | |
f85afd4e | 5900 | } |
b7fff980 | 5901 | return FALSE; |
f85afd4e MB |
5902 | } |
5903 | ||
5904 | ||
2d66e025 MB |
5905 | |
5906 | // | |
5907 | // ----- event handlers | |
f85afd4e | 5908 | // |
8f177c8e | 5909 | |
2d66e025 MB |
5910 | // Generate a grid event based on a mouse event and |
5911 | // return the result of ProcessEvent() | |
5912 | // | |
fe7b9ed6 | 5913 | int wxGrid::SendEvent( const wxEventType type, |
2d66e025 MB |
5914 | int row, int col, |
5915 | wxMouseEvent& mouseEv ) | |
5916 | { | |
fe7b9ed6 | 5917 | bool claimed; |
1bcf0c7d | 5918 | bool vetoed= FALSE; |
fe7b9ed6 | 5919 | |
97a9929e VZ |
5920 | if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE ) |
5921 | { | |
5922 | int rowOrCol = (row == -1 ? col : row); | |
5923 | ||
5924 | wxGridSizeEvent gridEvt( GetId(), | |
5925 | type, | |
5926 | this, | |
5927 | rowOrCol, | |
5928 | mouseEv.GetX() + GetRowLabelSize(), | |
5929 | mouseEv.GetY() + GetColLabelSize(), | |
5930 | mouseEv.ControlDown(), | |
5931 | mouseEv.ShiftDown(), | |
5932 | mouseEv.AltDown(), | |
5933 | mouseEv.MetaDown() ); | |
5934 | ||
5935 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
5936 | vetoed = !gridEvt.IsAllowed(); | |
5937 | } | |
fe7b9ed6 | 5938 | else if ( type == wxEVT_GRID_RANGE_SELECT ) |
97a9929e VZ |
5939 | { |
5940 | // Right now, it should _never_ end up here! | |
5941 | wxGridRangeSelectEvent gridEvt( GetId(), | |
5942 | type, | |
5943 | this, | |
5944 | m_selectingTopLeft, | |
5945 | m_selectingBottomRight, | |
5946 | TRUE, | |
5947 | mouseEv.ControlDown(), | |
5948 | mouseEv.ShiftDown(), | |
5949 | mouseEv.AltDown(), | |
5950 | mouseEv.MetaDown() ); | |
5951 | ||
5952 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
5953 | vetoed = !gridEvt.IsAllowed(); | |
5954 | } | |
fe7b9ed6 | 5955 | else |
97a9929e VZ |
5956 | { |
5957 | wxGridEvent gridEvt( GetId(), | |
5958 | type, | |
5959 | this, | |
5960 | row, col, | |
5961 | mouseEv.GetX() + GetRowLabelSize(), | |
5962 | mouseEv.GetY() + GetColLabelSize(), | |
5963 | FALSE, | |
5964 | mouseEv.ControlDown(), | |
5965 | mouseEv.ShiftDown(), | |
5966 | mouseEv.AltDown(), | |
5967 | mouseEv.MetaDown() ); | |
5968 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
5969 | vetoed = !gridEvt.IsAllowed(); | |
5970 | } | |
5971 | ||
5972 | // A Veto'd event may not be `claimed' so test this first | |
5973 | if (vetoed) return -1; | |
5974 | return claimed ? 1 : 0; | |
f85afd4e MB |
5975 | } |
5976 | ||
5977 | ||
2d66e025 MB |
5978 | // Generate a grid event of specified type and return the result |
5979 | // of ProcessEvent(). | |
f85afd4e | 5980 | // |
fe7b9ed6 | 5981 | int wxGrid::SendEvent( const wxEventType type, |
2d66e025 | 5982 | int row, int col ) |
f85afd4e | 5983 | { |
fe7b9ed6 | 5984 | bool claimed; |
1bcf0c7d | 5985 | bool vetoed= FALSE; |
fe7b9ed6 | 5986 | |
b54ba671 | 5987 | if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE ) |
f85afd4e | 5988 | { |
2d66e025 | 5989 | int rowOrCol = (row == -1 ? col : row); |
f85afd4e | 5990 | |
2d66e025 MB |
5991 | wxGridSizeEvent gridEvt( GetId(), |
5992 | type, | |
5993 | this, | |
5994 | rowOrCol ); | |
5995 | ||
fe7b9ed6 VZ |
5996 | claimed = GetEventHandler()->ProcessEvent(gridEvt); |
5997 | vetoed = !gridEvt.IsAllowed(); | |
2d66e025 MB |
5998 | } |
5999 | else | |
6000 | { | |
6001 | wxGridEvent gridEvt( GetId(), | |
6002 | type, | |
6003 | this, | |
6004 | row, col ); | |
8f177c8e | 6005 | |
fe7b9ed6 VZ |
6006 | claimed = GetEventHandler()->ProcessEvent(gridEvt); |
6007 | vetoed = !gridEvt.IsAllowed(); | |
6008 | } | |
6009 | ||
97a9929e VZ |
6010 | // A Veto'd event may not be `claimed' so test this first |
6011 | if (vetoed) return -1; | |
6012 | return claimed ? 1 : 0; | |
f85afd4e MB |
6013 | } |
6014 | ||
6015 | ||
2d66e025 | 6016 | void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
f85afd4e | 6017 | { |
f6bcfd97 | 6018 | wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW |
8f177c8e | 6019 | } |
f85afd4e | 6020 | |
bca7bfc8 | 6021 | void wxGrid::Refresh(bool eraseb, const wxRect* rect) |
27f35b66 | 6022 | { |
ad603bf7 VZ |
6023 | // Don't do anything if between Begin/EndBatch... |
6024 | // EndBatch() will do all this on the last nested one anyway. | |
27f35b66 SN |
6025 | if (! GetBatchCount()) |
6026 | { | |
bca7bfc8 | 6027 | // Refresh to get correct scrolled position: |
27f35b66 SN |
6028 | wxScrolledWindow::Refresh(eraseb,rect); |
6029 | ||
27f35b66 SN |
6030 | if (rect) |
6031 | { | |
bca7bfc8 SN |
6032 | int rect_x, rect_y, rectWidth, rectHeight; |
6033 | int width_label, width_cell, height_label, height_cell; | |
6034 | int x, y; | |
6035 | ||
27f35b66 | 6036 | //Copy rectangle can get scroll offsets.. |
bca7bfc8 SN |
6037 | rect_x = rect->GetX(); |
6038 | rect_y = rect->GetY(); | |
6039 | rectWidth = rect->GetWidth(); | |
6040 | rectHeight = rect->GetHeight(); | |
27f35b66 | 6041 | |
bca7bfc8 SN |
6042 | width_label = m_rowLabelWidth - rect_x; |
6043 | if (width_label > rectWidth) width_label = rectWidth; | |
27f35b66 | 6044 | |
bca7bfc8 SN |
6045 | height_label = m_colLabelHeight - rect_y; |
6046 | if (height_label > rectHeight) height_label = rectHeight; | |
27f35b66 | 6047 | |
bca7bfc8 SN |
6048 | if (rect_x > m_rowLabelWidth) |
6049 | { | |
6050 | x = rect_x - m_rowLabelWidth; | |
6051 | width_cell = rectWidth; | |
6052 | } | |
6053 | else | |
6054 | { | |
6055 | x = 0; | |
6056 | width_cell = rectWidth - (m_rowLabelWidth - rect_x); | |
6057 | } | |
6058 | ||
6059 | if (rect_y > m_colLabelHeight) | |
6060 | { | |
6061 | y = rect_y - m_colLabelHeight; | |
6062 | height_cell = rectHeight; | |
6063 | } | |
6064 | else | |
6065 | { | |
6066 | y = 0; | |
6067 | height_cell = rectHeight - (m_colLabelHeight - rect_y); | |
6068 | } | |
6069 | ||
6070 | // Paint corner label part intersecting rect. | |
6071 | if ( width_label > 0 && height_label > 0 ) | |
6072 | { | |
6073 | wxRect anotherrect(rect_x, rect_y, width_label, height_label); | |
6074 | m_cornerLabelWin->Refresh(eraseb, &anotherrect); | |
6075 | } | |
6076 | ||
6077 | // Paint col labels part intersecting rect. | |
6078 | if ( width_cell > 0 && height_label > 0 ) | |
6079 | { | |
6080 | wxRect anotherrect(x, rect_y, width_cell, height_label); | |
6081 | m_colLabelWin->Refresh(eraseb, &anotherrect); | |
6082 | } | |
6083 | ||
6084 | // Paint row labels part intersecting rect. | |
6085 | if ( width_label > 0 && height_cell > 0 ) | |
6086 | { | |
6087 | wxRect anotherrect(rect_x, y, width_label, height_cell); | |
6088 | m_rowLabelWin->Refresh(eraseb, &anotherrect); | |
6089 | } | |
6090 | ||
6091 | // Paint cell area part intersecting rect. | |
6092 | if ( width_cell > 0 && height_cell > 0 ) | |
6093 | { | |
6094 | wxRect anotherrect(x, y, width_cell, height_cell); | |
6095 | m_gridWin->Refresh(eraseb, &anotherrect); | |
6096 | } | |
6097 | } | |
6098 | else | |
6099 | { | |
6100 | m_cornerLabelWin->Refresh(eraseb, NULL); | |
2b5f62a0 | 6101 | m_colLabelWin->Refresh(eraseb, NULL); |
bca7bfc8 SN |
6102 | m_rowLabelWin->Refresh(eraseb, NULL); |
6103 | m_gridWin->Refresh(eraseb, NULL); | |
6104 | } | |
27f35b66 SN |
6105 | } |
6106 | } | |
f85afd4e | 6107 | |
97a9929e | 6108 | void wxGrid::OnSize( wxSizeEvent& event ) |
f85afd4e | 6109 | { |
97a9929e | 6110 | // position the child windows |
7807d81c | 6111 | CalcWindowSizes(); |
97a9929e VZ |
6112 | |
6113 | // don't call CalcDimensions() from here, the base class handles the size | |
6114 | // changes itself | |
6115 | event.Skip(); | |
f85afd4e MB |
6116 | } |
6117 | ||
f85afd4e | 6118 | |
2d66e025 | 6119 | void wxGrid::OnKeyDown( wxKeyEvent& event ) |
f85afd4e | 6120 | { |
2d66e025 | 6121 | if ( m_inOnKeyDown ) |
f85afd4e | 6122 | { |
2d66e025 MB |
6123 | // shouldn't be here - we are going round in circles... |
6124 | // | |
07296f0b | 6125 | wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") ); |
f85afd4e MB |
6126 | } |
6127 | ||
2d66e025 | 6128 | m_inOnKeyDown = TRUE; |
f85afd4e | 6129 | |
2d66e025 MB |
6130 | // propagate the event up and see if it gets processed |
6131 | // | |
6132 | wxWindow *parent = GetParent(); | |
6133 | wxKeyEvent keyEvt( event ); | |
6134 | keyEvt.SetEventObject( parent ); | |
6135 | ||
6136 | if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) ) | |
f85afd4e | 6137 | { |
9b4aede2 | 6138 | |
2d66e025 MB |
6139 | // try local handlers |
6140 | // | |
12a3f227 | 6141 | switch ( event.GetKeyCode() ) |
2d66e025 MB |
6142 | { |
6143 | case WXK_UP: | |
6144 | if ( event.ControlDown() ) | |
6145 | { | |
5c8fc7c1 | 6146 | MoveCursorUpBlock( event.ShiftDown() ); |
2d66e025 MB |
6147 | } |
6148 | else | |
6149 | { | |
5c8fc7c1 | 6150 | MoveCursorUp( event.ShiftDown() ); |
2d66e025 MB |
6151 | } |
6152 | break; | |
f85afd4e | 6153 | |
2d66e025 MB |
6154 | case WXK_DOWN: |
6155 | if ( event.ControlDown() ) | |
6156 | { | |
5c8fc7c1 | 6157 | MoveCursorDownBlock( event.ShiftDown() ); |
2d66e025 MB |
6158 | } |
6159 | else | |
6160 | { | |
5c8fc7c1 | 6161 | MoveCursorDown( event.ShiftDown() ); |
2d66e025 MB |
6162 | } |
6163 | break; | |
8f177c8e | 6164 | |
2d66e025 MB |
6165 | case WXK_LEFT: |
6166 | if ( event.ControlDown() ) | |
6167 | { | |
5c8fc7c1 | 6168 | MoveCursorLeftBlock( event.ShiftDown() ); |
2d66e025 MB |
6169 | } |
6170 | else | |
6171 | { | |
5c8fc7c1 | 6172 | MoveCursorLeft( event.ShiftDown() ); |
2d66e025 MB |
6173 | } |
6174 | break; | |
6175 | ||
6176 | case WXK_RIGHT: | |
6177 | if ( event.ControlDown() ) | |
6178 | { | |
5c8fc7c1 | 6179 | MoveCursorRightBlock( event.ShiftDown() ); |
2d66e025 MB |
6180 | } |
6181 | else | |
6182 | { | |
5c8fc7c1 | 6183 | MoveCursorRight( event.ShiftDown() ); |
2d66e025 MB |
6184 | } |
6185 | break; | |
b99be8fb | 6186 | |
2d66e025 | 6187 | case WXK_RETURN: |
a4f7bf58 | 6188 | case WXK_NUMPAD_ENTER: |
58dd5b3b MB |
6189 | if ( event.ControlDown() ) |
6190 | { | |
6191 | event.Skip(); // to let the edit control have the return | |
6192 | } | |
6193 | else | |
6194 | { | |
f6bcfd97 BP |
6195 | if ( GetGridCursorRow() < GetNumberRows()-1 ) |
6196 | { | |
6197 | MoveCursorDown( event.ShiftDown() ); | |
6198 | } | |
6199 | else | |
6200 | { | |
6201 | // at the bottom of a column | |
6202 | HideCellEditControl(); | |
6203 | SaveEditControlValue(); | |
6204 | } | |
58dd5b3b | 6205 | } |
2d66e025 MB |
6206 | break; |
6207 | ||
5c8fc7c1 | 6208 | case WXK_ESCAPE: |
e32352cf | 6209 | ClearSelection(); |
5c8fc7c1 SN |
6210 | break; |
6211 | ||
2c9a89e0 RD |
6212 | case WXK_TAB: |
6213 | if (event.ShiftDown()) | |
f6bcfd97 BP |
6214 | { |
6215 | if ( GetGridCursorCol() > 0 ) | |
6216 | { | |
6217 | MoveCursorLeft( FALSE ); | |
6218 | } | |
6219 | else | |
6220 | { | |
6221 | // at left of grid | |
6222 | HideCellEditControl(); | |
6223 | SaveEditControlValue(); | |
6224 | } | |
6225 | } | |
2c9a89e0 | 6226 | else |
f6bcfd97 BP |
6227 | { |
6228 | if ( GetGridCursorCol() < GetNumberCols()-1 ) | |
6229 | { | |
6230 | MoveCursorRight( FALSE ); | |
6231 | } | |
6232 | else | |
6233 | { | |
6234 | // at right of grid | |
6235 | HideCellEditControl(); | |
6236 | SaveEditControlValue(); | |
6237 | } | |
6238 | } | |
2c9a89e0 RD |
6239 | break; |
6240 | ||
2d66e025 MB |
6241 | case WXK_HOME: |
6242 | if ( event.ControlDown() ) | |
6243 | { | |
6244 | MakeCellVisible( 0, 0 ); | |
6245 | SetCurrentCell( 0, 0 ); | |
6246 | } | |
6247 | else | |
6248 | { | |
6249 | event.Skip(); | |
6250 | } | |
6251 | break; | |
6252 | ||
6253 | case WXK_END: | |
6254 | if ( event.ControlDown() ) | |
6255 | { | |
6256 | MakeCellVisible( m_numRows-1, m_numCols-1 ); | |
6257 | SetCurrentCell( m_numRows-1, m_numCols-1 ); | |
6258 | } | |
6259 | else | |
6260 | { | |
6261 | event.Skip(); | |
6262 | } | |
6263 | break; | |
6264 | ||
6265 | case WXK_PRIOR: | |
6266 | MovePageUp(); | |
6267 | break; | |
6268 | ||
6269 | case WXK_NEXT: | |
6270 | MovePageDown(); | |
6271 | break; | |
6272 | ||
07296f0b | 6273 | case WXK_SPACE: |
aa5e1f75 SN |
6274 | if ( event.ControlDown() ) |
6275 | { | |
3f3dc2ef VZ |
6276 | if ( m_selection ) |
6277 | { | |
6278 | m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(), | |
6279 | m_currentCellCoords.GetCol(), | |
6280 | event.ControlDown(), | |
6281 | event.ShiftDown(), | |
6282 | event.AltDown(), | |
6283 | event.MetaDown() ); | |
6284 | } | |
aa5e1f75 SN |
6285 | break; |
6286 | } | |
07296f0b RD |
6287 | if ( !IsEditable() ) |
6288 | { | |
5c8fc7c1 | 6289 | MoveCursorRight( FALSE ); |
07296f0b RD |
6290 | break; |
6291 | } | |
6292 | // Otherwise fall through to default | |
6293 | ||
2d66e025 | 6294 | default: |
f6bcfd97 BP |
6295 | // is it possible to edit the current cell at all? |
6296 | if ( !IsCellEditControlEnabled() && CanEnableCellControl() ) | |
b54ba671 | 6297 | { |
f6bcfd97 | 6298 | // yes, now check whether the cells editor accepts the key |
f2d76237 RD |
6299 | int row = m_currentCellCoords.GetRow(); |
6300 | int col = m_currentCellCoords.GetCol(); | |
6301 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
0b190b0f | 6302 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
f6bcfd97 BP |
6303 | |
6304 | // <F2> is special and will always start editing, for | |
6305 | // other keys - ask the editor itself | |
12a3f227 | 6306 | if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers()) |
f6bcfd97 BP |
6307 | || editor->IsAcceptedKey(event) ) |
6308 | { | |
04418332 | 6309 | // ensure cell is visble |
73145b0e | 6310 | MakeCellVisible(row, col); |
f6bcfd97 | 6311 | EnableCellEditControl(); |
04418332 VZ |
6312 | |
6313 | // a problem can arise if the cell is not completely | |
6314 | // visible (even after calling MakeCellVisible the | |
6315 | // control is not created and calling StartingKey will | |
73145b0e JS |
6316 | // crash the app |
6317 | if( editor->IsCreated() && m_cellEditCtrlEnabled ) editor->StartingKey(event); | |
f6bcfd97 BP |
6318 | } |
6319 | else | |
6320 | { | |
6321 | event.Skip(); | |
6322 | } | |
6323 | ||
0b190b0f | 6324 | editor->DecRef(); |
2c9a89e0 RD |
6325 | attr->DecRef(); |
6326 | } | |
b3a7510d VZ |
6327 | else |
6328 | { | |
b94ae1ea VZ |
6329 | // let others process char events with modifiers or all |
6330 | // char events for readonly cells | |
b3a7510d VZ |
6331 | event.Skip(); |
6332 | } | |
025562fe | 6333 | break; |
2d66e025 | 6334 | } |
f85afd4e MB |
6335 | } |
6336 | ||
2d66e025 | 6337 | m_inOnKeyDown = FALSE; |
f85afd4e MB |
6338 | } |
6339 | ||
f6bcfd97 BP |
6340 | void wxGrid::OnKeyUp( wxKeyEvent& event ) |
6341 | { | |
6342 | // try local handlers | |
6343 | // | |
12a3f227 | 6344 | if ( event.GetKeyCode() == WXK_SHIFT ) |
f6bcfd97 BP |
6345 | { |
6346 | if ( m_selectingTopLeft != wxGridNoCellCoords && | |
6347 | m_selectingBottomRight != wxGridNoCellCoords ) | |
3f3dc2ef VZ |
6348 | { |
6349 | if ( m_selection ) | |
6350 | { | |
6351 | m_selection->SelectBlock( m_selectingTopLeft.GetRow(), | |
6352 | m_selectingTopLeft.GetCol(), | |
6353 | m_selectingBottomRight.GetRow(), | |
6354 | m_selectingBottomRight.GetCol(), | |
6355 | event.ControlDown(), | |
6356 | TRUE, | |
6357 | event.AltDown(), | |
6358 | event.MetaDown() ); | |
6359 | } | |
6360 | } | |
6361 | ||
f6bcfd97 BP |
6362 | m_selectingTopLeft = wxGridNoCellCoords; |
6363 | m_selectingBottomRight = wxGridNoCellCoords; | |
6364 | m_selectingKeyboard = wxGridNoCellCoords; | |
6365 | } | |
6366 | } | |
6367 | ||
2796cce3 | 6368 | void wxGrid::OnEraseBackground(wxEraseEvent&) |
508011ce VZ |
6369 | { |
6370 | } | |
07296f0b | 6371 | |
2d66e025 | 6372 | void wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) |
66242c80 | 6373 | { |
f6bcfd97 BP |
6374 | if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) ) |
6375 | { | |
6376 | // the event has been intercepted - do nothing | |
6377 | return; | |
6378 | } | |
6379 | ||
6380 | wxClientDC dc(m_gridWin); | |
6381 | PrepareDC(dc); | |
6382 | ||
2a7750d9 | 6383 | if ( m_currentCellCoords != wxGridNoCellCoords ) |
2d66e025 | 6384 | { |
2d66e025 | 6385 | HideCellEditControl(); |
b54ba671 | 6386 | DisableCellEditControl(); |
07296f0b | 6387 | |
3ca6a5f0 | 6388 | if ( IsVisible( m_currentCellCoords, FALSE ) ) |
f6bcfd97 BP |
6389 | { |
6390 | wxRect r; | |
3ed884a0 | 6391 | r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords); |
f6bcfd97 BP |
6392 | if ( !m_gridLinesEnabled ) |
6393 | { | |
6394 | r.x--; | |
6395 | r.y--; | |
6396 | r.width++; | |
6397 | r.height++; | |
6398 | } | |
d1c0b4f9 | 6399 | |
3ed884a0 | 6400 | wxGridCellCoordsArray cells = CalcCellsExposed( r ); |
84912ef8 | 6401 | |
f6bcfd97 BP |
6402 | // Otherwise refresh redraws the highlight! |
6403 | m_currentCellCoords = coords; | |
275c4ae4 | 6404 | |
d10f4bf9 | 6405 | DrawGridCellArea(dc,cells); |
f6bcfd97 BP |
6406 | DrawAllGridLines( dc, r ); |
6407 | } | |
66242c80 | 6408 | } |
8f177c8e | 6409 | |
2d66e025 MB |
6410 | m_currentCellCoords = coords; |
6411 | ||
2a7750d9 MB |
6412 | wxGridCellAttr* attr = GetCellAttr(coords); |
6413 | DrawCellHighlight(dc, attr); | |
6414 | attr->DecRef(); | |
66242c80 MB |
6415 | } |
6416 | ||
2d66e025 | 6417 | |
c9097836 MB |
6418 | void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol ) |
6419 | { | |
6420 | int temp; | |
6421 | wxGridCellCoords updateTopLeft, updateBottomRight; | |
6422 | ||
3f3dc2ef | 6423 | if ( m_selection ) |
c9097836 | 6424 | { |
3f3dc2ef VZ |
6425 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows ) |
6426 | { | |
6427 | leftCol = 0; | |
6428 | rightCol = GetNumberCols() - 1; | |
6429 | } | |
6430 | else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns ) | |
6431 | { | |
6432 | topRow = 0; | |
6433 | bottomRow = GetNumberRows() - 1; | |
6434 | } | |
c9097836 | 6435 | } |
3f3dc2ef | 6436 | |
c9097836 MB |
6437 | if ( topRow > bottomRow ) |
6438 | { | |
6439 | temp = topRow; | |
6440 | topRow = bottomRow; | |
6441 | bottomRow = temp; | |
6442 | } | |
6443 | ||
6444 | if ( leftCol > rightCol ) | |
6445 | { | |
6446 | temp = leftCol; | |
6447 | leftCol = rightCol; | |
6448 | rightCol = temp; | |
6449 | } | |
6450 | ||
6451 | updateTopLeft = wxGridCellCoords( topRow, leftCol ); | |
6452 | updateBottomRight = wxGridCellCoords( bottomRow, rightCol ); | |
6453 | ||
3ed884a0 SN |
6454 | // First the case that we selected a completely new area |
6455 | if ( m_selectingTopLeft == wxGridNoCellCoords || | |
6456 | m_selectingBottomRight == wxGridNoCellCoords ) | |
6457 | { | |
6458 | wxRect rect; | |
6459 | rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ), | |
6460 | wxGridCellCoords ( bottomRow, rightCol ) ); | |
2b5f62a0 | 6461 | m_gridWin->Refresh( FALSE, &rect ); |
3ed884a0 SN |
6462 | } |
6463 | // Now handle changing an existing selection area. | |
6464 | else if ( m_selectingTopLeft != updateTopLeft || | |
6465 | m_selectingBottomRight != updateBottomRight ) | |
c9097836 MB |
6466 | { |
6467 | // Compute two optimal update rectangles: | |
6468 | // Either one rectangle is a real subset of the | |
6469 | // other, or they are (almost) disjoint! | |
6470 | wxRect rect[4]; | |
6471 | bool need_refresh[4]; | |
6472 | need_refresh[0] = | |
6473 | need_refresh[1] = | |
6474 | need_refresh[2] = | |
6475 | need_refresh[3] = FALSE; | |
6476 | int i; | |
6477 | ||
6478 | // Store intermediate values | |
6479 | wxCoord oldLeft = m_selectingTopLeft.GetCol(); | |
6480 | wxCoord oldTop = m_selectingTopLeft.GetRow(); | |
6481 | wxCoord oldRight = m_selectingBottomRight.GetCol(); | |
6482 | wxCoord oldBottom = m_selectingBottomRight.GetRow(); | |
6483 | ||
6484 | // Determine the outer/inner coordinates. | |
6485 | if (oldLeft > leftCol) | |
6486 | { | |
6487 | temp = oldLeft; | |
6488 | oldLeft = leftCol; | |
6489 | leftCol = temp; | |
6490 | } | |
6491 | if (oldTop > topRow ) | |
6492 | { | |
6493 | temp = oldTop; | |
6494 | oldTop = topRow; | |
6495 | topRow = temp; | |
6496 | } | |
6497 | if (oldRight < rightCol ) | |
6498 | { | |
6499 | temp = oldRight; | |
6500 | oldRight = rightCol; | |
6501 | rightCol = temp; | |
6502 | } | |
6503 | if (oldBottom < bottomRow) | |
6504 | { | |
6505 | temp = oldBottom; | |
6506 | oldBottom = bottomRow; | |
6507 | bottomRow = temp; | |
6508 | } | |
6509 | ||
6510 | // Now, either the stuff marked old is the outer | |
6511 | // rectangle or we don't have a situation where one | |
6512 | // is contained in the other. | |
6513 | ||
6514 | if ( oldLeft < leftCol ) | |
6515 | { | |
3ed884a0 SN |
6516 | // Refresh the newly selected or deselected |
6517 | // area to the left of the old or new selection. | |
c9097836 MB |
6518 | need_refresh[0] = TRUE; |
6519 | rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
6520 | oldLeft ), | |
6521 | wxGridCellCoords ( oldBottom, | |
6522 | leftCol - 1 ) ); | |
6523 | } | |
6524 | ||
6525 | if ( oldTop < topRow ) | |
6526 | { | |
3ed884a0 SN |
6527 | // Refresh the newly selected or deselected |
6528 | // area above the old or new selection. | |
c9097836 MB |
6529 | need_refresh[1] = TRUE; |
6530 | rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
6531 | leftCol ), | |
6532 | wxGridCellCoords ( topRow - 1, | |
6533 | rightCol ) ); | |
6534 | } | |
6535 | ||
6536 | if ( oldRight > rightCol ) | |
6537 | { | |
3ed884a0 SN |
6538 | // Refresh the newly selected or deselected |
6539 | // area to the right of the old or new selection. | |
c9097836 MB |
6540 | need_refresh[2] = TRUE; |
6541 | rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
6542 | rightCol + 1 ), | |
6543 | wxGridCellCoords ( oldBottom, | |
6544 | oldRight ) ); | |
6545 | } | |
6546 | ||
6547 | if ( oldBottom > bottomRow ) | |
6548 | { | |
3ed884a0 SN |
6549 | // Refresh the newly selected or deselected |
6550 | // area below the old or new selection. | |
c9097836 MB |
6551 | need_refresh[3] = TRUE; |
6552 | rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1, | |
6553 | leftCol ), | |
6554 | wxGridCellCoords ( oldBottom, | |
6555 | rightCol ) ); | |
6556 | } | |
6557 | ||
c9097836 MB |
6558 | // various Refresh() calls |
6559 | for (i = 0; i < 4; i++ ) | |
6560 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
6561 | m_gridWin->Refresh( FALSE, &(rect[i]) ); | |
6562 | } | |
3ed884a0 SN |
6563 | // Change Selection |
6564 | m_selectingTopLeft = updateTopLeft; | |
6565 | m_selectingBottomRight = updateBottomRight; | |
c9097836 MB |
6566 | } |
6567 | ||
2d66e025 MB |
6568 | // |
6569 | // ------ functions to get/send data (see also public functions) | |
6570 | // | |
6571 | ||
6572 | bool wxGrid::GetModelValues() | |
66242c80 | 6573 | { |
bca7bfc8 SN |
6574 | // Hide the editor, so it won't hide a changed value. |
6575 | HideCellEditControl(); | |
c6707d16 | 6576 | |
2d66e025 | 6577 | if ( m_table ) |
66242c80 | 6578 | { |
2d66e025 | 6579 | // all we need to do is repaint the grid |
66242c80 | 6580 | // |
2d66e025 | 6581 | m_gridWin->Refresh(); |
66242c80 MB |
6582 | return TRUE; |
6583 | } | |
8f177c8e | 6584 | |
66242c80 MB |
6585 | return FALSE; |
6586 | } | |
6587 | ||
2d66e025 MB |
6588 | |
6589 | bool wxGrid::SetModelValues() | |
f85afd4e | 6590 | { |
2d66e025 | 6591 | int row, col; |
8f177c8e | 6592 | |
c6707d16 SN |
6593 | // Disable the editor, so it won't hide a changed value. |
6594 | // Do we also want to save the current value of the editor first? | |
6595 | // I think so ... | |
c6707d16 SN |
6596 | DisableCellEditControl(); |
6597 | ||
2d66e025 MB |
6598 | if ( m_table ) |
6599 | { | |
6600 | for ( row = 0; row < m_numRows; row++ ) | |
f85afd4e | 6601 | { |
2d66e025 | 6602 | for ( col = 0; col < m_numCols; col++ ) |
f85afd4e | 6603 | { |
2d66e025 | 6604 | m_table->SetValue( row, col, GetCellValue(row, col) ); |
f85afd4e MB |
6605 | } |
6606 | } | |
8f177c8e | 6607 | |
f85afd4e MB |
6608 | return TRUE; |
6609 | } | |
6610 | ||
6611 | return FALSE; | |
6612 | } | |
6613 | ||
2d66e025 MB |
6614 | |
6615 | ||
6616 | // Note - this function only draws cells that are in the list of | |
6617 | // exposed cells (usually set from the update region by | |
6618 | // CalcExposedCells) | |
6619 | // | |
d10f4bf9 | 6620 | void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells ) |
f85afd4e | 6621 | { |
2d66e025 | 6622 | if ( !m_numRows || !m_numCols ) return; |
60ff3b99 | 6623 | |
dc1f566f | 6624 | int i, numCells = cells.GetCount(); |
27f35b66 SN |
6625 | int row, col, cell_rows, cell_cols; |
6626 | wxGridCellCoordsArray redrawCells; | |
60ff3b99 | 6627 | |
27f35b66 | 6628 | for ( i = numCells-1; i >= 0; i-- ) |
f85afd4e | 6629 | { |
27f35b66 SN |
6630 | row = cells[i].GetRow(); |
6631 | col = cells[i].GetCol(); | |
6632 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
6633 | ||
6634 | // If this cell is part of a multicell block, find owner for repaint | |
6635 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
6636 | { | |
6637 | wxGridCellCoords cell(row+cell_rows, col+cell_cols); | |
6638 | bool marked = FALSE; | |
dc1f566f | 6639 | for ( int j = 0; j < numCells; j++ ) |
27f35b66 SN |
6640 | { |
6641 | if ( cell == cells[j] ) | |
6642 | { | |
6643 | marked = TRUE; | |
3ed884a0 | 6644 | break; |
27f35b66 SN |
6645 | } |
6646 | } | |
6647 | if (!marked) | |
6648 | { | |
6649 | int count = redrawCells.GetCount(); | |
dc1f566f | 6650 | for (int j = 0; j < count; j++) |
27f35b66 SN |
6651 | { |
6652 | if ( cell == redrawCells[j] ) | |
6653 | { | |
6654 | marked = TRUE; | |
6655 | break; | |
6656 | } | |
6657 | } | |
6658 | if (!marked) redrawCells.Add( cell ); | |
6659 | } | |
6660 | continue; // don't bother drawing this cell | |
6661 | } | |
6662 | ||
6663 | // If this cell is empty, find cell to left that might want to overflow | |
6664 | if (m_table && m_table->IsEmptyCell(row, col)) | |
6665 | { | |
dc1f566f | 6666 | for ( int l = 0; l < cell_rows; l++ ) |
27f35b66 | 6667 | { |
dc1f566f SN |
6668 | // find a cell in this row to left alreay marked for repaint |
6669 | int left = col; | |
6670 | for (int k = 0; k < int(redrawCells.GetCount()); k++) | |
6671 | if ((redrawCells[k].GetCol() < left) && | |
6672 | (redrawCells[k].GetRow() == row)) | |
6673 | left=redrawCells[k].GetCol(); | |
6674 | ||
6675 | if (left == col) left = 0; // oh well | |
6676 | ||
6677 | for (int j = col-1; j >= left; j--) | |
27f35b66 SN |
6678 | { |
6679 | if (!m_table->IsEmptyCell(row+l, j)) | |
6680 | { | |
6681 | if (GetCellOverflow(row+l, j)) | |
6682 | { | |
6683 | wxGridCellCoords cell(row+l, j); | |
6684 | bool marked = FALSE; | |
6685 | ||
dc1f566f | 6686 | for (int k = 0; k < numCells; k++) |
27f35b66 SN |
6687 | { |
6688 | if ( cell == cells[k] ) | |
6689 | { | |
6690 | marked = TRUE; | |
6691 | break; | |
6692 | } | |
6693 | } | |
6694 | if (!marked) | |
6695 | { | |
6696 | int count = redrawCells.GetCount(); | |
dc1f566f | 6697 | for (int k = 0; k < count; k++) |
27f35b66 SN |
6698 | { |
6699 | if ( cell == redrawCells[k] ) | |
6700 | { | |
6701 | marked = TRUE; | |
6702 | break; | |
6703 | } | |
6704 | } | |
6705 | if (!marked) redrawCells.Add( cell ); | |
6706 | } | |
6707 | } | |
6708 | break; | |
6709 | } | |
6710 | } | |
6711 | } | |
6712 | } | |
d10f4bf9 | 6713 | DrawCell( dc, cells[i] ); |
2d66e025 | 6714 | } |
27f35b66 SN |
6715 | |
6716 | numCells = redrawCells.GetCount(); | |
6717 | ||
6718 | for ( i = numCells - 1; i >= 0; i-- ) | |
6719 | { | |
6720 | DrawCell( dc, redrawCells[i] ); | |
6721 | } | |
2d66e025 | 6722 | } |
8f177c8e | 6723 | |
8f177c8e | 6724 | |
7c8a8ad5 MB |
6725 | void wxGrid::DrawGridSpace( wxDC& dc ) |
6726 | { | |
f6bcfd97 BP |
6727 | int cw, ch; |
6728 | m_gridWin->GetClientSize( &cw, &ch ); | |
7c8a8ad5 | 6729 | |
f6bcfd97 BP |
6730 | int right, bottom; |
6731 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7c8a8ad5 | 6732 | |
f6bcfd97 BP |
6733 | int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0; |
6734 | int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0 ; | |
7c8a8ad5 | 6735 | |
f6bcfd97 BP |
6736 | if ( right > rightCol || bottom > bottomRow ) |
6737 | { | |
6738 | int left, top; | |
6739 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7c8a8ad5 | 6740 | |
f6bcfd97 BP |
6741 | dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) ); |
6742 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
7c8a8ad5 | 6743 | |
f6bcfd97 BP |
6744 | if ( right > rightCol ) |
6745 | { | |
6746 | dc.DrawRectangle( rightCol, top, right - rightCol, ch); | |
6747 | } | |
6748 | ||
6749 | if ( bottom > bottomRow ) | |
6750 | { | |
6751 | dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow); | |
6752 | } | |
6753 | } | |
7c8a8ad5 MB |
6754 | } |
6755 | ||
6756 | ||
2d66e025 MB |
6757 | void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) |
6758 | { | |
ab79958a VZ |
6759 | int row = coords.GetRow(); |
6760 | int col = coords.GetCol(); | |
60ff3b99 | 6761 | |
7c1cb261 | 6762 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
ab79958a VZ |
6763 | return; |
6764 | ||
6765 | // we draw the cell border ourselves | |
9496deb5 | 6766 | #if !WXGRID_DRAW_LINES |
2d66e025 MB |
6767 | if ( m_gridLinesEnabled ) |
6768 | DrawCellBorder( dc, coords ); | |
796df70a | 6769 | #endif |
f85afd4e | 6770 | |
189d0213 VZ |
6771 | wxGridCellAttr* attr = GetCellAttr(row, col); |
6772 | ||
6773 | bool isCurrent = coords == m_currentCellCoords; | |
508011ce | 6774 | |
f6bcfd97 | 6775 | wxRect rect = CellToRect( row, col ); |
f85afd4e | 6776 | |
189d0213 | 6777 | // if the editor is shown, we should use it and not the renderer |
f6bcfd97 BP |
6778 | // Note: However, only if it is really _shown_, i.e. not hidden! |
6779 | if ( isCurrent && IsCellEditControlShown() ) | |
189d0213 | 6780 | { |
0b190b0f VZ |
6781 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
6782 | editor->PaintBackground(rect, attr); | |
6783 | editor->DecRef(); | |
189d0213 VZ |
6784 | } |
6785 | else | |
6786 | { | |
6787 | // but all the rest is drawn by the cell renderer and hence may be | |
6788 | // customized | |
0b190b0f VZ |
6789 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); |
6790 | renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); | |
6791 | renderer->DecRef(); | |
189d0213 | 6792 | } |
07296f0b | 6793 | |
283b7808 VZ |
6794 | attr->DecRef(); |
6795 | } | |
07296f0b | 6796 | |
283b7808 | 6797 | void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ) |
07296f0b RD |
6798 | { |
6799 | int row = m_currentCellCoords.GetRow(); | |
6800 | int col = m_currentCellCoords.GetCol(); | |
6801 | ||
7c1cb261 | 6802 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
07296f0b RD |
6803 | return; |
6804 | ||
f6bcfd97 | 6805 | wxRect rect = CellToRect(row, col); |
07296f0b | 6806 | |
b3a7510d VZ |
6807 | // hmmm... what could we do here to show that the cell is disabled? |
6808 | // for now, I just draw a thinner border than for the other ones, but | |
6809 | // it doesn't look really good | |
b3a7510d | 6810 | |
d2520c85 RD |
6811 | int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth; |
6812 | ||
27f35b66 SN |
6813 | if (penWidth > 0) |
6814 | { | |
d2520c85 RD |
6815 | // The center of th drawn line is where the position/width/height of |
6816 | // the rectangle is actually at, (on wxMSW atr least,) so we will | |
6817 | // reduce the size of the rectangle to compensate for the thickness of | |
6818 | // the line. If this is too strange on non wxMSW platforms then | |
6819 | // please #ifdef this appropriately. | |
6820 | rect.x += penWidth/2; | |
6821 | rect.y += penWidth/2; | |
6822 | rect.width -= penWidth-1; | |
6823 | rect.height -= penWidth-1; | |
6824 | ||
6825 | ||
6826 | // Now draw the rectangle | |
73145b0e JS |
6827 | // use the cellHighlightColour if the cell is inside a selection, this |
6828 | // will ensure the cell is always visible. | |
6829 | dc.SetPen(wxPen(IsInSelection(row,col)?m_selectionForeground:m_cellHighlightColour, penWidth, wxSOLID)); | |
d2520c85 RD |
6830 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
6831 | dc.DrawRectangle(rect); | |
6832 | } | |
07296f0b | 6833 | |
283b7808 | 6834 | #if 0 |
b3a7510d | 6835 | // VZ: my experiments with 3d borders... |
283b7808 | 6836 | |
b3a7510d | 6837 | // how to properly set colours for arbitrary bg? |
283b7808 VZ |
6838 | wxCoord x1 = rect.x, |
6839 | y1 = rect.y, | |
00cf1208 RR |
6840 | x2 = rect.x + rect.width -1, |
6841 | y2 = rect.y + rect.height -1; | |
283b7808 VZ |
6842 | |
6843 | dc.SetPen(*wxWHITE_PEN); | |
00cf1208 RR |
6844 | dc.DrawLine(x1, y1, x2, y1); |
6845 | dc.DrawLine(x1, y1, x1, y2); | |
283b7808 | 6846 | |
283b7808 | 6847 | dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1); |
00cf1208 | 6848 | dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 ); |
283b7808 VZ |
6849 | |
6850 | dc.SetPen(*wxBLACK_PEN); | |
6851 | dc.DrawLine(x1, y2, x2, y2); | |
00cf1208 | 6852 | dc.DrawLine(x2, y1, x2, y2+1); |
b3a7510d | 6853 | #endif // 0 |
2d66e025 | 6854 | } |
f85afd4e | 6855 | |
f2d76237 | 6856 | |
2d66e025 MB |
6857 | void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) |
6858 | { | |
2d66e025 MB |
6859 | int row = coords.GetRow(); |
6860 | int col = coords.GetCol(); | |
7c1cb261 VZ |
6861 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
6862 | return; | |
6863 | ||
6864 | dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) ); | |
60ff3b99 | 6865 | |
27f35b66 SN |
6866 | wxRect rect = CellToRect( row, col ); |
6867 | ||
2d66e025 MB |
6868 | // right hand border |
6869 | // | |
27f35b66 SN |
6870 | dc.DrawLine( rect.x + rect.width, rect.y, |
6871 | rect.x + rect.width, rect.y + rect.height + 1 ); | |
2d66e025 MB |
6872 | |
6873 | // bottom border | |
6874 | // | |
27f35b66 SN |
6875 | dc.DrawLine( rect.x, rect.y + rect.height, |
6876 | rect.x + rect.width, rect.y + rect.height); | |
f85afd4e MB |
6877 | } |
6878 | ||
d10f4bf9 | 6879 | void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells) |
b3a7510d | 6880 | { |
2a7750d9 MB |
6881 | // This if block was previously in wxGrid::OnPaint but that doesn't |
6882 | // seem to get called under wxGTK - MB | |
6883 | // | |
6884 | if ( m_currentCellCoords == wxGridNoCellCoords && | |
6885 | m_numRows && m_numCols ) | |
6886 | { | |
6887 | m_currentCellCoords.Set(0, 0); | |
6888 | } | |
6889 | ||
f6bcfd97 | 6890 | if ( IsCellEditControlShown() ) |
99306db2 VZ |
6891 | { |
6892 | // don't show highlight when the edit control is shown | |
6893 | return; | |
6894 | } | |
6895 | ||
b3a7510d VZ |
6896 | // if the active cell was repainted, repaint its highlight too because it |
6897 | // might have been damaged by the grid lines | |
d10f4bf9 | 6898 | size_t count = cells.GetCount(); |
b3a7510d VZ |
6899 | for ( size_t n = 0; n < count; n++ ) |
6900 | { | |
d10f4bf9 | 6901 | if ( cells[n] == m_currentCellCoords ) |
b3a7510d VZ |
6902 | { |
6903 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
6904 | DrawCellHighlight(dc, attr); | |
6905 | attr->DecRef(); | |
6906 | ||
6907 | break; | |
6908 | } | |
6909 | } | |
6910 | } | |
2d66e025 | 6911 | |
2d66e025 MB |
6912 | // TODO: remove this ??? |
6913 | // This is used to redraw all grid lines e.g. when the grid line colour | |
6914 | // has been changed | |
6915 | // | |
33ac7e6f | 6916 | void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) ) |
f85afd4e | 6917 | { |
27f35b66 SN |
6918 | #if !WXGRID_DRAW_LINES |
6919 | return; | |
6920 | #endif | |
6921 | ||
60ff3b99 VZ |
6922 | if ( !m_gridLinesEnabled || |
6923 | !m_numRows || | |
2d66e025 | 6924 | !m_numCols ) return; |
f85afd4e | 6925 | |
2d66e025 | 6926 | int top, bottom, left, right; |
796df70a | 6927 | |
f6bcfd97 | 6928 | #if 0 //#ifndef __WXGTK__ |
508011ce VZ |
6929 | if (reg.IsEmpty()) |
6930 | { | |
796df70a SN |
6931 | int cw, ch; |
6932 | m_gridWin->GetClientSize(&cw, &ch); | |
6933 | ||
6934 | // virtual coords of visible area | |
6935 | // | |
6936 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
6937 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
6938 | } | |
508011ce VZ |
6939 | else |
6940 | { | |
796df70a SN |
6941 | wxCoord x, y, w, h; |
6942 | reg.GetBox(x, y, w, h); | |
6943 | CalcUnscrolledPosition( x, y, &left, &top ); | |
6944 | CalcUnscrolledPosition( x + w, y + h, &right, &bottom ); | |
6945 | } | |
8e217128 RR |
6946 | #else |
6947 | int cw, ch; | |
6948 | m_gridWin->GetClientSize(&cw, &ch); | |
6949 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
6950 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
6951 | #endif | |
f85afd4e | 6952 | |
9496deb5 MB |
6953 | // avoid drawing grid lines past the last row and col |
6954 | // | |
7c1cb261 VZ |
6955 | right = wxMin( right, GetColRight(m_numCols - 1) ); |
6956 | bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) ); | |
9496deb5 | 6957 | |
27f35b66 | 6958 | // no gridlines inside multicells, clip them out |
a967f048 RG |
6959 | int leftCol = internalXToCol(left); |
6960 | int topRow = internalYToRow(top); | |
6961 | int rightCol = internalXToCol(right); | |
6962 | int bottomRow = internalYToRow(bottom); | |
27f35b66 SN |
6963 | wxRegion clippedcells(0, 0, cw, ch); |
6964 | ||
27f35b66 | 6965 | |
a967f048 RG |
6966 | int i, j, cell_rows, cell_cols; |
6967 | wxRect rect; | |
27f35b66 | 6968 | |
a967f048 RG |
6969 | for (j=topRow; j<bottomRow; j++) |
6970 | { | |
6971 | for (i=leftCol; i<rightCol; i++) | |
27f35b66 | 6972 | { |
a967f048 RG |
6973 | GetCellSize( j, i, &cell_rows, &cell_cols ); |
6974 | if ((cell_rows > 1) || (cell_cols > 1)) | |
27f35b66 | 6975 | { |
a967f048 RG |
6976 | rect = CellToRect(j,i); |
6977 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
6978 | clippedcells.Subtract(rect); | |
6979 | } | |
6980 | else if ((cell_rows < 0) || (cell_cols < 0)) | |
6981 | { | |
6982 | rect = CellToRect(j+cell_rows, i+cell_cols); | |
6983 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
6984 | clippedcells.Subtract(rect); | |
27f35b66 SN |
6985 | } |
6986 | } | |
6987 | } | |
6988 | dc.SetClippingRegion( clippedcells ); | |
6989 | ||
2d66e025 | 6990 | dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) ); |
f85afd4e | 6991 | |
2d66e025 | 6992 | // horizontal grid lines |
f85afd4e | 6993 | // |
a967f048 | 6994 | // already declared above - int i; |
33188aa4 | 6995 | for ( i = internalYToRow(top); i < m_numRows; i++ ) |
f85afd4e | 6996 | { |
6d55126d | 6997 | int bot = GetRowBottom(i) - 1; |
7c1cb261 | 6998 | |
6d55126d | 6999 | if ( bot > bottom ) |
2d66e025 MB |
7000 | { |
7001 | break; | |
7002 | } | |
7c1cb261 | 7003 | |
6d55126d | 7004 | if ( bot >= top ) |
2d66e025 | 7005 | { |
6d55126d | 7006 | dc.DrawLine( left, bot, right, bot ); |
2d66e025 | 7007 | } |
f85afd4e MB |
7008 | } |
7009 | ||
f85afd4e | 7010 | |
2d66e025 MB |
7011 | // vertical grid lines |
7012 | // | |
33188aa4 | 7013 | for ( i = internalXToCol(left); i < m_numCols; i++ ) |
f85afd4e | 7014 | { |
7c1cb261 VZ |
7015 | int colRight = GetColRight(i) - 1; |
7016 | if ( colRight > right ) | |
2d66e025 MB |
7017 | { |
7018 | break; | |
7019 | } | |
7c1cb261 VZ |
7020 | |
7021 | if ( colRight >= left ) | |
2d66e025 | 7022 | { |
7c1cb261 | 7023 | dc.DrawLine( colRight, top, colRight, bottom ); |
2d66e025 MB |
7024 | } |
7025 | } | |
27f35b66 | 7026 | dc.DestroyClippingRegion(); |
2d66e025 | 7027 | } |
f85afd4e | 7028 | |
8f177c8e | 7029 | |
d10f4bf9 | 7030 | void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows) |
2d66e025 | 7031 | { |
f6bcfd97 | 7032 | if ( !m_numRows ) return; |
60ff3b99 | 7033 | |
2d66e025 | 7034 | size_t i; |
d10f4bf9 | 7035 | size_t numLabels = rows.GetCount(); |
60ff3b99 | 7036 | |
2d66e025 MB |
7037 | for ( i = 0; i < numLabels; i++ ) |
7038 | { | |
d10f4bf9 | 7039 | DrawRowLabel( dc, rows[i] ); |
60ff3b99 | 7040 | } |
f85afd4e MB |
7041 | } |
7042 | ||
7043 | ||
2d66e025 | 7044 | void wxGrid::DrawRowLabel( wxDC& dc, int row ) |
f85afd4e | 7045 | { |
7c1cb261 VZ |
7046 | if ( GetRowHeight(row) <= 0 ) |
7047 | return; | |
60ff3b99 | 7048 | |
7c1cb261 VZ |
7049 | int rowTop = GetRowTop(row), |
7050 | rowBottom = GetRowBottom(row) - 1; | |
b99be8fb | 7051 | |
73145b0e | 7052 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) ); |
b0d6ff2f | 7053 | dc.DrawLine( m_rowLabelWidth-1, rowTop, |
7c1cb261 | 7054 | m_rowLabelWidth-1, rowBottom ); |
b99be8fb | 7055 | |
73145b0e JS |
7056 | dc.DrawLine( 0, rowTop, 0, rowBottom ); |
7057 | ||
7058 | dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom ); | |
b99be8fb | 7059 | |
2d66e025 | 7060 | dc.SetPen( *wxWHITE_PEN ); |
73145b0e JS |
7061 | dc.DrawLine( 1, rowTop, 1, rowBottom ); |
7062 | dc.DrawLine( 1, rowTop, m_rowLabelWidth-1, rowTop ); | |
60ff3b99 | 7063 | |
f85afd4e | 7064 | dc.SetBackgroundMode( wxTRANSPARENT ); |
f85afd4e MB |
7065 | dc.SetTextForeground( GetLabelTextColour() ); |
7066 | dc.SetFont( GetLabelFont() ); | |
8f177c8e | 7067 | |
f85afd4e | 7068 | int hAlign, vAlign; |
2d66e025 | 7069 | GetRowLabelAlignment( &hAlign, &vAlign ); |
60ff3b99 | 7070 | |
2d66e025 MB |
7071 | wxRect rect; |
7072 | rect.SetX( 2 ); | |
7c1cb261 | 7073 | rect.SetY( GetRowTop(row) + 2 ); |
2d66e025 | 7074 | rect.SetWidth( m_rowLabelWidth - 4 ); |
7c1cb261 | 7075 | rect.SetHeight( GetRowHeight(row) - 4 ); |
60ff3b99 | 7076 | DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign ); |
f85afd4e MB |
7077 | } |
7078 | ||
7079 | ||
d10f4bf9 | 7080 | void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols ) |
f85afd4e | 7081 | { |
f6bcfd97 | 7082 | if ( !m_numCols ) return; |
60ff3b99 | 7083 | |
2d66e025 | 7084 | size_t i; |
d10f4bf9 | 7085 | size_t numLabels = cols.GetCount(); |
60ff3b99 | 7086 | |
2d66e025 | 7087 | for ( i = 0; i < numLabels; i++ ) |
f85afd4e | 7088 | { |
d10f4bf9 | 7089 | DrawColLabel( dc, cols[i] ); |
60ff3b99 | 7090 | } |
f85afd4e MB |
7091 | } |
7092 | ||
7093 | ||
2d66e025 | 7094 | void wxGrid::DrawColLabel( wxDC& dc, int col ) |
f85afd4e | 7095 | { |
7c1cb261 VZ |
7096 | if ( GetColWidth(col) <= 0 ) |
7097 | return; | |
60ff3b99 | 7098 | |
7c1cb261 VZ |
7099 | int colLeft = GetColLeft(col), |
7100 | colRight = GetColRight(col) - 1; | |
b99be8fb | 7101 | |
73145b0e | 7102 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) ); |
7c1cb261 VZ |
7103 | dc.DrawLine( colRight, 0, |
7104 | colRight, m_colLabelHeight-1 ); | |
b99be8fb | 7105 | |
73145b0e JS |
7106 | dc.DrawLine( colLeft, 0, colRight, 0 ); |
7107 | ||
b0d6ff2f | 7108 | dc.DrawLine( colLeft, m_colLabelHeight-1, |
73145b0e | 7109 | colRight+1, m_colLabelHeight-1 ); |
b99be8fb | 7110 | |
f85afd4e | 7111 | dc.SetPen( *wxWHITE_PEN ); |
73145b0e JS |
7112 | dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 ); |
7113 | dc.DrawLine( colLeft, 1, colRight, 1 ); | |
f85afd4e | 7114 | |
b0d6ff2f MB |
7115 | dc.SetBackgroundMode( wxTRANSPARENT ); |
7116 | dc.SetTextForeground( GetLabelTextColour() ); | |
7117 | dc.SetFont( GetLabelFont() ); | |
8f177c8e | 7118 | |
d43851f7 | 7119 | int hAlign, vAlign, orient; |
2d66e025 | 7120 | GetColLabelAlignment( &hAlign, &vAlign ); |
d43851f7 | 7121 | orient = GetColLabelTextOrientation(); |
60ff3b99 | 7122 | |
2d66e025 | 7123 | wxRect rect; |
7c1cb261 | 7124 | rect.SetX( colLeft + 2 ); |
2d66e025 | 7125 | rect.SetY( 2 ); |
7c1cb261 | 7126 | rect.SetWidth( GetColWidth(col) - 4 ); |
2d66e025 | 7127 | rect.SetHeight( m_colLabelHeight - 4 ); |
d43851f7 | 7128 | DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient ); |
f85afd4e MB |
7129 | } |
7130 | ||
2d66e025 MB |
7131 | void wxGrid::DrawTextRectangle( wxDC& dc, |
7132 | const wxString& value, | |
7133 | const wxRect& rect, | |
7134 | int horizAlign, | |
d43851f7 JS |
7135 | int vertAlign, |
7136 | int textOrientation ) | |
f85afd4e | 7137 | { |
2d66e025 | 7138 | wxArrayString lines; |
ef5df12b | 7139 | |
2d66e025 | 7140 | StringToLines( value, lines ); |
ef5df12b RD |
7141 | |
7142 | ||
d10f4bf9 VZ |
7143 | //Forward to new API. |
7144 | DrawTextRectangle( dc, | |
038a5591 JS |
7145 | lines, |
7146 | rect, | |
7147 | horizAlign, | |
7148 | vertAlign, | |
7149 | textOrientation ); | |
ef5df12b | 7150 | |
d10f4bf9 VZ |
7151 | } |
7152 | ||
7153 | void wxGrid::DrawTextRectangle( wxDC& dc, | |
038a5591 JS |
7154 | const wxArrayString& lines, |
7155 | const wxRect& rect, | |
7156 | int horizAlign, | |
7157 | int vertAlign, | |
7158 | int textOrientation ) | |
d10f4bf9 VZ |
7159 | { |
7160 | long textWidth, textHeight; | |
7161 | long lineWidth, lineHeight; | |
73145b0e | 7162 | int nLines; |
ef5df12b | 7163 | |
a373d23b | 7164 | dc.SetClippingRegion( rect ); |
ef5df12b | 7165 | |
73145b0e JS |
7166 | nLines = lines.GetCount(); |
7167 | if( nLines > 0 ) | |
2d66e025 | 7168 | { |
038a5591 JS |
7169 | int l; |
7170 | float x = 0.0, y = 0.0; | |
ef5df12b | 7171 | |
038a5591 JS |
7172 | if( textOrientation == wxHORIZONTAL ) |
7173 | GetTextBoxSize(dc, lines, &textWidth, &textHeight); | |
7174 | else | |
7175 | GetTextBoxSize( dc, lines, &textHeight, &textWidth ); | |
ef5df12b | 7176 | |
038a5591 JS |
7177 | switch( vertAlign ) |
7178 | { | |
73145b0e | 7179 | case wxALIGN_BOTTOM: |
d43851f7 | 7180 | if( textOrientation == wxHORIZONTAL ) |
038a5591 | 7181 | y = rect.y + (rect.height - textHeight - 1); |
d43851f7 | 7182 | else |
038a5591 JS |
7183 | x = rect.x + rect.width - textWidth; |
7184 | break; | |
ef5df12b | 7185 | |
73145b0e | 7186 | case wxALIGN_CENTRE: |
d43851f7 | 7187 | if( textOrientation == wxHORIZONTAL ) |
038a5591 | 7188 | y = rect.y + ((rect.height - textHeight)/2); |
d43851f7 | 7189 | else |
038a5591 JS |
7190 | x = rect.x + ((rect.width - textWidth)/2); |
7191 | break; | |
ef5df12b | 7192 | |
73145b0e JS |
7193 | case wxALIGN_TOP: |
7194 | default: | |
d43851f7 | 7195 | if( textOrientation == wxHORIZONTAL ) |
038a5591 | 7196 | y = rect.y + 1; |
d43851f7 | 7197 | else |
038a5591 | 7198 | x = rect.x + 1; |
73145b0e | 7199 | break; |
f85afd4e | 7200 | } |
ef5df12b | 7201 | |
038a5591 JS |
7202 | // Align each line of a multi-line label |
7203 | for( l = 0; l < nLines; l++ ) | |
7204 | { | |
7205 | dc.GetTextExtent(lines[l], &lineWidth, &lineHeight); | |
ef5df12b | 7206 | |
038a5591 JS |
7207 | switch( horizAlign ) |
7208 | { | |
7209 | case wxALIGN_RIGHT: | |
7210 | if( textOrientation == wxHORIZONTAL ) | |
7211 | x = rect.x + (rect.width - lineWidth - 1); | |
7212 | else | |
7213 | y = rect.y + lineWidth + 1; | |
7214 | break; | |
ef5df12b | 7215 | |
038a5591 JS |
7216 | case wxALIGN_CENTRE: |
7217 | if( textOrientation == wxHORIZONTAL ) | |
7218 | x = rect.x + ((rect.width - lineWidth)/2); | |
7219 | else | |
7220 | y = rect.y + rect.height - ((rect.height - lineWidth)/2); | |
7221 | break; | |
ef5df12b | 7222 | |
038a5591 JS |
7223 | case wxALIGN_LEFT: |
7224 | default: | |
7225 | if( textOrientation == wxHORIZONTAL ) | |
7226 | x = rect.x + 1; | |
7227 | else | |
7228 | y = rect.y + rect.height - 1; | |
7229 | break; | |
7230 | } | |
ef5df12b | 7231 | |
038a5591 JS |
7232 | if( textOrientation == wxHORIZONTAL ) |
7233 | { | |
7234 | dc.DrawText( lines[l], (int)x, (int)y ); | |
7235 | y += lineHeight; | |
7236 | } | |
7237 | else | |
7238 | { | |
7239 | dc.DrawRotatedText( lines[l], (int)x, (int)y, 90.0 ); | |
7240 | x += lineHeight; | |
7241 | } | |
7242 | } | |
f85afd4e | 7243 | } |
f85afd4e | 7244 | dc.DestroyClippingRegion(); |
f85afd4e MB |
7245 | } |
7246 | ||
7247 | ||
7248 | // Split multi line text up into an array of strings. Any existing | |
7249 | // contents of the string array are preserved. | |
7250 | // | |
7251 | void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) | |
7252 | { | |
f85afd4e MB |
7253 | int startPos = 0; |
7254 | int pos; | |
6d004f67 | 7255 | wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix ); |
2433bb2e | 7256 | wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix ); |
e2b42eeb | 7257 | |
2433bb2e | 7258 | while ( startPos < (int)tVal.Length() ) |
f85afd4e | 7259 | { |
2433bb2e | 7260 | pos = tVal.Mid(startPos).Find( eol ); |
f85afd4e MB |
7261 | if ( pos < 0 ) |
7262 | { | |
7263 | break; | |
7264 | } | |
7265 | else if ( pos == 0 ) | |
7266 | { | |
7267 | lines.Add( wxEmptyString ); | |
7268 | } | |
7269 | else | |
7270 | { | |
2433bb2e | 7271 | lines.Add( value.Mid(startPos, pos) ); |
f85afd4e MB |
7272 | } |
7273 | startPos += pos+1; | |
7274 | } | |
b540eb2b | 7275 | if ( startPos < (int)value.Length() ) |
f85afd4e MB |
7276 | { |
7277 | lines.Add( value.Mid( startPos ) ); | |
7278 | } | |
7279 | } | |
7280 | ||
7281 | ||
7282 | void wxGrid::GetTextBoxSize( wxDC& dc, | |
d10f4bf9 | 7283 | const wxArrayString& lines, |
f85afd4e MB |
7284 | long *width, long *height ) |
7285 | { | |
7286 | long w = 0; | |
7287 | long h = 0; | |
7288 | long lineW, lineH; | |
7289 | ||
b540eb2b | 7290 | size_t i; |
f85afd4e MB |
7291 | for ( i = 0; i < lines.GetCount(); i++ ) |
7292 | { | |
7293 | dc.GetTextExtent( lines[i], &lineW, &lineH ); | |
7294 | w = wxMax( w, lineW ); | |
7295 | h += lineH; | |
7296 | } | |
7297 | ||
7298 | *width = w; | |
7299 | *height = h; | |
7300 | } | |
7301 | ||
f6bcfd97 BP |
7302 | // |
7303 | // ------ Batch processing. | |
7304 | // | |
7305 | void wxGrid::EndBatch() | |
7306 | { | |
7307 | if ( m_batchCount > 0 ) | |
7308 | { | |
7309 | m_batchCount--; | |
7310 | if ( !m_batchCount ) | |
7311 | { | |
7312 | CalcDimensions(); | |
7313 | m_rowLabelWin->Refresh(); | |
7314 | m_colLabelWin->Refresh(); | |
7315 | m_cornerLabelWin->Refresh(); | |
7316 | m_gridWin->Refresh(); | |
7317 | } | |
7318 | } | |
7319 | } | |
f85afd4e | 7320 | |
d8232393 MB |
7321 | // Use this, rather than wxWindow::Refresh(), to force an immediate |
7322 | // repainting of the grid. Has no effect if you are already inside a | |
7323 | // BeginBatch / EndBatch block. | |
7324 | // | |
7325 | void wxGrid::ForceRefresh() | |
7326 | { | |
7327 | BeginBatch(); | |
7328 | EndBatch(); | |
7329 | } | |
7330 | ||
7331 | ||
f85afd4e | 7332 | // |
2d66e025 | 7333 | // ------ Edit control functions |
f85afd4e MB |
7334 | // |
7335 | ||
2d66e025 MB |
7336 | |
7337 | void wxGrid::EnableEditing( bool edit ) | |
f85afd4e | 7338 | { |
2d66e025 MB |
7339 | // TODO: improve this ? |
7340 | // | |
7341 | if ( edit != m_editable ) | |
f85afd4e | 7342 | { |
632efa47 | 7343 | if(!edit) EnableCellEditControl(edit); |
2d66e025 | 7344 | m_editable = edit; |
f85afd4e | 7345 | } |
f85afd4e MB |
7346 | } |
7347 | ||
7348 | ||
2d66e025 | 7349 | void wxGrid::EnableCellEditControl( bool enable ) |
f85afd4e | 7350 | { |
2c9a89e0 RD |
7351 | if (! m_editable) |
7352 | return; | |
7353 | ||
dcfe4c3d SN |
7354 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
7355 | SetCurrentCell( 0, 0 ); | |
60ff3b99 | 7356 | |
2c9a89e0 RD |
7357 | if ( enable != m_cellEditCtrlEnabled ) |
7358 | { | |
dcfe4c3d | 7359 | if ( enable ) |
f85afd4e | 7360 | { |
97a9929e VZ |
7361 | if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0) |
7362 | return; | |
fe7b9ed6 | 7363 | |
97a9929e | 7364 | // this should be checked by the caller! |
b54ba671 VZ |
7365 | wxASSERT_MSG( CanEnableCellControl(), |
7366 | _T("can't enable editing for this cell!") ); | |
7367 | ||
7368 | // do it before ShowCellEditControl() | |
dcfe4c3d | 7369 | m_cellEditCtrlEnabled = enable; |
b54ba671 | 7370 | |
2d66e025 | 7371 | ShowCellEditControl(); |
2d66e025 MB |
7372 | } |
7373 | else | |
7374 | { | |
97a9929e VZ |
7375 | //FIXME:add veto support |
7376 | SendEvent( wxEVT_GRID_EDITOR_HIDDEN); | |
fe7b9ed6 | 7377 | |
97a9929e | 7378 | HideCellEditControl(); |
2d66e025 | 7379 | SaveEditControlValue(); |
b54ba671 VZ |
7380 | |
7381 | // do it after HideCellEditControl() | |
dcfe4c3d | 7382 | m_cellEditCtrlEnabled = enable; |
f85afd4e | 7383 | } |
f85afd4e | 7384 | } |
f85afd4e | 7385 | } |
f85afd4e | 7386 | |
b54ba671 | 7387 | bool wxGrid::IsCurrentCellReadOnly() const |
283b7808 | 7388 | { |
b54ba671 VZ |
7389 | // const_cast |
7390 | wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords); | |
7391 | bool readonly = attr->IsReadOnly(); | |
7392 | attr->DecRef(); | |
283b7808 | 7393 | |
b54ba671 VZ |
7394 | return readonly; |
7395 | } | |
283b7808 | 7396 | |
b54ba671 VZ |
7397 | bool wxGrid::CanEnableCellControl() const |
7398 | { | |
7399 | return m_editable && !IsCurrentCellReadOnly(); | |
7400 | } | |
7401 | ||
7402 | bool wxGrid::IsCellEditControlEnabled() const | |
7403 | { | |
7404 | // the cell edit control might be disable for all cells or just for the | |
7405 | // current one if it's read only | |
7406 | return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE; | |
283b7808 VZ |
7407 | } |
7408 | ||
f6bcfd97 BP |
7409 | bool wxGrid::IsCellEditControlShown() const |
7410 | { | |
7411 | bool isShown = FALSE; | |
7412 | ||
7413 | if ( m_cellEditCtrlEnabled ) | |
7414 | { | |
7415 | int row = m_currentCellCoords.GetRow(); | |
7416 | int col = m_currentCellCoords.GetCol(); | |
7417 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
7418 | wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col); | |
7419 | attr->DecRef(); | |
7420 | ||
7421 | if ( editor ) | |
7422 | { | |
7423 | if ( editor->IsCreated() ) | |
7424 | { | |
7425 | isShown = editor->GetControl()->IsShown(); | |
7426 | } | |
7427 | ||
7428 | editor->DecRef(); | |
7429 | } | |
7430 | } | |
7431 | ||
7432 | return isShown; | |
7433 | } | |
7434 | ||
2d66e025 | 7435 | void wxGrid::ShowCellEditControl() |
f85afd4e | 7436 | { |
2d66e025 | 7437 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 7438 | { |
2d66e025 MB |
7439 | if ( !IsVisible( m_currentCellCoords ) ) |
7440 | { | |
3ca6a5f0 | 7441 | m_cellEditCtrlEnabled = FALSE; |
2d66e025 MB |
7442 | return; |
7443 | } | |
7444 | else | |
7445 | { | |
2c9a89e0 RD |
7446 | wxRect rect = CellToRect( m_currentCellCoords ); |
7447 | int row = m_currentCellCoords.GetRow(); | |
7448 | int col = m_currentCellCoords.GetCol(); | |
2d66e025 | 7449 | |
27f35b66 SN |
7450 | // if this is part of a multicell, find owner (topleft) |
7451 | int cell_rows, cell_cols; | |
7452 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
7453 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
7454 | { | |
7455 | row += cell_rows; | |
7456 | col += cell_cols; | |
7457 | m_currentCellCoords.SetRow( row ); | |
7458 | m_currentCellCoords.SetCol( col ); | |
7459 | } | |
7460 | ||
2d66e025 MB |
7461 | // convert to scrolled coords |
7462 | // | |
99306db2 | 7463 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
508011ce | 7464 | |
99306db2 VZ |
7465 | // done in PaintBackground() |
7466 | #if 0 | |
7467 | // erase the highlight and the cell contents because the editor | |
7468 | // might not cover the entire cell | |
7469 | wxClientDC dc( m_gridWin ); | |
7470 | PrepareDC( dc ); | |
7471 | dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID)); | |
7472 | dc.SetPen(*wxTRANSPARENT_PEN); | |
7473 | dc.DrawRectangle(rect); | |
7474 | #endif // 0 | |
d2fdd8d2 | 7475 | |
99306db2 | 7476 | // cell is shifted by one pixel |
68c5a31c | 7477 | // However, don't allow x or y to become negative |
70e8d961 | 7478 | // since the SetSize() method interprets that as |
68c5a31c SN |
7479 | // "don't change." |
7480 | if (rect.x > 0) | |
7481 | rect.x--; | |
7482 | if (rect.y > 0) | |
7483 | rect.y--; | |
f0102d2a | 7484 | |
508011ce | 7485 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 7486 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
3da93aae VZ |
7487 | if ( !editor->IsCreated() ) |
7488 | { | |
2c9a89e0 RD |
7489 | editor->Create(m_gridWin, -1, |
7490 | new wxGridCellEditorEvtHandler(this, editor)); | |
bf7945ce RD |
7491 | |
7492 | wxGridEditorCreatedEvent evt(GetId(), | |
7493 | wxEVT_GRID_EDITOR_CREATED, | |
7494 | this, | |
7495 | row, | |
7496 | col, | |
7497 | editor->GetControl()); | |
7498 | GetEventHandler()->ProcessEvent(evt); | |
2d66e025 MB |
7499 | } |
7500 | ||
0b190b0f | 7501 | |
27f35b66 | 7502 | // resize editor to overflow into righthand cells if allowed |
39b80349 | 7503 | int maxWidth = rect.width; |
27f35b66 SN |
7504 | wxString value = GetCellValue(row, col); |
7505 | if ( (value != wxEmptyString) && (attr->GetOverflow()) ) | |
7506 | { | |
39b80349 SN |
7507 | int y; |
7508 | GetTextExtent(value, &maxWidth, &y, | |
2b5f62a0 | 7509 | NULL, NULL, &attr->GetFont()); |
39b80349 SN |
7510 | if (maxWidth < rect.width) maxWidth = rect.width; |
7511 | } | |
7512 | int client_right = m_gridWin->GetClientSize().GetWidth(); | |
7513 | if (rect.x+maxWidth > client_right) | |
2b5f62a0 | 7514 | maxWidth = client_right - rect.x; |
39b80349 | 7515 | |
2b5f62a0 VZ |
7516 | if ((maxWidth > rect.width) && (col < m_numCols) && m_table) |
7517 | { | |
39b80349 | 7518 | GetCellSize( row, col, &cell_rows, &cell_cols ); |
2b5f62a0 VZ |
7519 | // may have changed earlier |
7520 | for (int i = col+cell_cols; i < m_numCols; i++) | |
7521 | { | |
39b80349 SN |
7522 | int c_rows, c_cols; |
7523 | GetCellSize( row, i, &c_rows, &c_cols ); | |
2b5f62a0 VZ |
7524 | // looks weird going over a multicell |
7525 | if (m_table->IsEmptyCell(row,i) && | |
7526 | (rect.width < maxWidth) && (c_rows == 1)) | |
7527 | rect.width += GetColWidth(i); | |
7528 | else | |
7529 | break; | |
7530 | } | |
39b80349 | 7531 | if (rect.GetRight() > client_right) |
2b5f62a0 | 7532 | rect.SetRight(client_right-1); |
27f35b66 | 7533 | } |
73145b0e | 7534 | |
1bd71df9 | 7535 | editor->SetCellAttr(attr); |
2c9a89e0 | 7536 | editor->SetSize( rect ); |
73145b0e | 7537 | editor->Show( TRUE, attr ); |
04418332 VZ |
7538 | |
7539 | // recalc dimensions in case we need to | |
7540 | // expand the scrolled window to account for editor | |
73145b0e | 7541 | CalcDimensions(); |
99306db2 | 7542 | |
3da93aae | 7543 | editor->BeginEdit(row, col, this); |
1bd71df9 | 7544 | editor->SetCellAttr(NULL); |
0b190b0f VZ |
7545 | |
7546 | editor->DecRef(); | |
2c9a89e0 | 7547 | attr->DecRef(); |
2b5f62a0 | 7548 | } |
f85afd4e MB |
7549 | } |
7550 | } | |
7551 | ||
7552 | ||
2d66e025 | 7553 | void wxGrid::HideCellEditControl() |
f85afd4e | 7554 | { |
2d66e025 | 7555 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 7556 | { |
2c9a89e0 RD |
7557 | int row = m_currentCellCoords.GetRow(); |
7558 | int col = m_currentCellCoords.GetCol(); | |
7559 | ||
3da93aae | 7560 | wxGridCellAttr* attr = GetCellAttr(row, col); |
0b190b0f VZ |
7561 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
7562 | editor->Show( FALSE ); | |
7563 | editor->DecRef(); | |
2c9a89e0 RD |
7564 | attr->DecRef(); |
7565 | m_gridWin->SetFocus(); | |
27f35b66 SN |
7566 | // refresh whole row to the right |
7567 | wxRect rect( CellToRect(row, col) ); | |
7568 | CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y ); | |
7569 | rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x; | |
f6bcfd97 | 7570 | m_gridWin->Refresh( FALSE, &rect ); |
f85afd4e | 7571 | } |
2d66e025 | 7572 | } |
8f177c8e | 7573 | |
2d66e025 | 7574 | |
2d66e025 MB |
7575 | void wxGrid::SaveEditControlValue() |
7576 | { | |
3da93aae VZ |
7577 | if ( IsCellEditControlEnabled() ) |
7578 | { | |
2c9a89e0 RD |
7579 | int row = m_currentCellCoords.GetRow(); |
7580 | int col = m_currentCellCoords.GetCol(); | |
8f177c8e | 7581 | |
fe7b9ed6 VZ |
7582 | wxString oldval = GetCellValue(row,col); |
7583 | ||
3da93aae | 7584 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 7585 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
3324d5f5 | 7586 | bool changed = editor->EndEdit(row, col, this); |
2d66e025 | 7587 | |
0b190b0f | 7588 | editor->DecRef(); |
2c9a89e0 | 7589 | attr->DecRef(); |
2d66e025 | 7590 | |
3da93aae VZ |
7591 | if (changed) |
7592 | { | |
fe7b9ed6 | 7593 | if ( SendEvent( wxEVT_GRID_CELL_CHANGE, |
2d66e025 | 7594 | m_currentCellCoords.GetRow(), |
fe7b9ed6 VZ |
7595 | m_currentCellCoords.GetCol() ) < 0 ) { |
7596 | ||
97a9929e VZ |
7597 | // Event has been vetoed, set the data back. |
7598 | SetCellValue(row,col,oldval); | |
7599 | } | |
2d66e025 | 7600 | } |
f85afd4e MB |
7601 | } |
7602 | } | |
7603 | ||
2d66e025 MB |
7604 | |
7605 | // | |
60ff3b99 VZ |
7606 | // ------ Grid location functions |
7607 | // Note that all of these functions work with the logical coordinates of | |
2d66e025 MB |
7608 | // grid cells and labels so you will need to convert from device |
7609 | // coordinates for mouse events etc. | |
7610 | // | |
7611 | ||
7612 | void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords ) | |
f85afd4e | 7613 | { |
58dd5b3b MB |
7614 | int row = YToRow(y); |
7615 | int col = XToCol(x); | |
7616 | ||
7617 | if ( row == -1 || col == -1 ) | |
7618 | { | |
7619 | coords = wxGridNoCellCoords; | |
7620 | } | |
7621 | else | |
7622 | { | |
7623 | coords.Set( row, col ); | |
7624 | } | |
2d66e025 | 7625 | } |
f85afd4e | 7626 | |
8f177c8e | 7627 | |
b1da8107 SN |
7628 | // Internal Helper function for computing row or column from some |
7629 | // (unscrolled) coordinate value, using either | |
70e8d961 | 7630 | // m_defaultRowHeight/m_defaultColWidth or binary search on array |
b1da8107 SN |
7631 | // of m_rowBottoms/m_ColRights to speed up the search! |
7632 | ||
7633 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, | |
64e15340 | 7634 | const wxArrayInt& BorderArray, int nMax, |
a967f048 | 7635 | bool clipToMinMax) |
2d66e025 | 7636 | { |
a967f048 RG |
7637 | |
7638 | if (coord < 0) | |
7639 | return clipToMinMax && (nMax > 0) ? 0 : -1; | |
7640 | ||
ef5df12b | 7641 | |
b1da8107 SN |
7642 | if (!defaultDist) |
7643 | defaultDist = 1; | |
a967f048 | 7644 | |
1af546bf VZ |
7645 | size_t i_max = coord / defaultDist, |
7646 | i_min = 0; | |
d57ad377 | 7647 | |
b1da8107 SN |
7648 | if (BorderArray.IsEmpty()) |
7649 | { | |
d57ad377 | 7650 | if((int) i_max < nMax) |
64e15340 | 7651 | return i_max; |
a967f048 | 7652 | return clipToMinMax ? nMax - 1 : -1; |
b1da8107 | 7653 | } |
8f177c8e | 7654 | |
b1da8107 SN |
7655 | if ( i_max >= BorderArray.GetCount()) |
7656 | i_max = BorderArray.GetCount() - 1; | |
70e8d961 | 7657 | else |
f85afd4e | 7658 | { |
b1da8107 SN |
7659 | if ( coord >= BorderArray[i_max]) |
7660 | { | |
7661 | i_min = i_max; | |
7662 | i_max = coord / minDist; | |
7663 | } | |
7664 | if ( i_max >= BorderArray.GetCount()) | |
7665 | i_max = BorderArray.GetCount() - 1; | |
f85afd4e | 7666 | } |
33188aa4 | 7667 | if ( coord >= BorderArray[i_max]) |
a967f048 | 7668 | return clipToMinMax ? (int)i_max : -1; |
68c5a31c SN |
7669 | if ( coord < BorderArray[0] ) |
7670 | return 0; | |
2d66e025 | 7671 | |
68c5a31c | 7672 | while ( i_max - i_min > 0 ) |
b1da8107 SN |
7673 | { |
7674 | wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max], | |
33188aa4 | 7675 | 0, _T("wxGrid: internal error in CoordToRowOrCol")); |
b1da8107 | 7676 | if (coord >= BorderArray[ i_max - 1]) |
b1da8107 | 7677 | return i_max; |
b1da8107 SN |
7678 | else |
7679 | i_max--; | |
7680 | int median = i_min + (i_max - i_min + 1) / 2; | |
7681 | if (coord < BorderArray[median]) | |
7682 | i_max = median; | |
7683 | else | |
7684 | i_min = median; | |
7685 | } | |
7686 | return i_max; | |
f85afd4e MB |
7687 | } |
7688 | ||
b1da8107 | 7689 | int wxGrid::YToRow( int y ) |
f85afd4e | 7690 | { |
b1da8107 | 7691 | return CoordToRowOrCol(y, m_defaultRowHeight, |
b8d24d4e | 7692 | m_minAcceptableRowHeight, m_rowBottoms, m_numRows, FALSE); |
b1da8107 | 7693 | } |
8f177c8e | 7694 | |
f85afd4e | 7695 | |
b1da8107 SN |
7696 | int wxGrid::XToCol( int x ) |
7697 | { | |
7698 | return CoordToRowOrCol(x, m_defaultColWidth, | |
b8d24d4e | 7699 | m_minAcceptableColWidth, m_colRights, m_numCols, FALSE); |
2d66e025 | 7700 | } |
8f177c8e | 7701 | |
2d66e025 MB |
7702 | |
7703 | // return the row number that that the y coord is near the edge of, or | |
7704 | // -1 if not near an edge | |
7705 | // | |
7706 | int wxGrid::YToEdgeOfRow( int y ) | |
7707 | { | |
33188aa4 SN |
7708 | int i; |
7709 | i = internalYToRow(y); | |
7710 | ||
7711 | if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE ) | |
2d66e025 | 7712 | { |
33188aa4 SN |
7713 | // We know that we are in row i, test whether we are |
7714 | // close enough to lower or upper border, respectively. | |
7715 | if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE ) | |
7716 | return i; | |
7717 | else if( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE ) | |
7718 | return i - 1; | |
f85afd4e | 7719 | } |
2d66e025 MB |
7720 | |
7721 | return -1; | |
7722 | } | |
7723 | ||
7724 | ||
7725 | // return the col number that that the x coord is near the edge of, or | |
7726 | // -1 if not near an edge | |
7727 | // | |
7728 | int wxGrid::XToEdgeOfCol( int x ) | |
7729 | { | |
33188aa4 SN |
7730 | int i; |
7731 | i = internalXToCol(x); | |
7732 | ||
7733 | if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE ) | |
f85afd4e | 7734 | { |
33188aa4 SN |
7735 | // We know that we are in column i, test whether we are |
7736 | // close enough to right or left border, respectively. | |
7737 | if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE ) | |
7738 | return i; | |
7739 | else if( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE ) | |
7740 | return i - 1; | |
f85afd4e | 7741 | } |
2d66e025 MB |
7742 | |
7743 | return -1; | |
f85afd4e MB |
7744 | } |
7745 | ||
2d66e025 MB |
7746 | |
7747 | wxRect wxGrid::CellToRect( int row, int col ) | |
f85afd4e | 7748 | { |
2d66e025 | 7749 | wxRect rect( -1, -1, -1, -1 ); |
f85afd4e | 7750 | |
2d66e025 MB |
7751 | if ( row >= 0 && row < m_numRows && |
7752 | col >= 0 && col < m_numCols ) | |
f85afd4e | 7753 | { |
27f35b66 SN |
7754 | int i, cell_rows, cell_cols; |
7755 | rect.width = rect.height = 0; | |
7756 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
7757 | // if negative then find multicell owner | |
7758 | if (cell_rows < 0) row += cell_rows; | |
7759 | if (cell_cols < 0) col += cell_cols; | |
7760 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
7761 | ||
7c1cb261 VZ |
7762 | rect.x = GetColLeft(col); |
7763 | rect.y = GetRowTop(row); | |
27f35b66 SN |
7764 | for (i=col; i<col+cell_cols; i++) |
7765 | rect.width += GetColWidth(i); | |
7766 | for (i=row; i<row+cell_rows; i++) | |
7767 | rect.height += GetRowHeight(i); | |
f85afd4e MB |
7768 | } |
7769 | ||
f6bcfd97 BP |
7770 | // if grid lines are enabled, then the area of the cell is a bit smaller |
7771 | if (m_gridLinesEnabled) { | |
7772 | rect.width -= 1; | |
7773 | rect.height -= 1; | |
7774 | } | |
2d66e025 MB |
7775 | return rect; |
7776 | } | |
7777 | ||
7778 | ||
7779 | bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) | |
7780 | { | |
7781 | // get the cell rectangle in logical coords | |
7782 | // | |
7783 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 7784 | |
2d66e025 MB |
7785 | // convert to device coords |
7786 | // | |
7787 | int left, top, right, bottom; | |
7788 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
7789 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 7790 | |
2d66e025 MB |
7791 | // check against the client area of the grid window |
7792 | // | |
7793 | int cw, ch; | |
7794 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 7795 | |
2d66e025 | 7796 | if ( wholeCellVisible ) |
f85afd4e | 7797 | { |
2d66e025 | 7798 | // is the cell wholly visible ? |
8f177c8e | 7799 | // |
2d66e025 MB |
7800 | return ( left >= 0 && right <= cw && |
7801 | top >= 0 && bottom <= ch ); | |
7802 | } | |
7803 | else | |
7804 | { | |
7805 | // is the cell partly visible ? | |
7806 | // | |
7807 | return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) && | |
7808 | ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) ); | |
7809 | } | |
7810 | } | |
7811 | ||
7812 | ||
7813 | // make the specified cell location visible by doing a minimal amount | |
7814 | // of scrolling | |
7815 | // | |
7816 | void wxGrid::MakeCellVisible( int row, int col ) | |
7817 | { | |
275c4ae4 | 7818 | |
2d66e025 | 7819 | int i; |
60ff3b99 | 7820 | int xpos = -1, ypos = -1; |
2d66e025 MB |
7821 | |
7822 | if ( row >= 0 && row < m_numRows && | |
7823 | col >= 0 && col < m_numCols ) | |
7824 | { | |
7825 | // get the cell rectangle in logical coords | |
7826 | // | |
7827 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 7828 | |
2d66e025 MB |
7829 | // convert to device coords |
7830 | // | |
7831 | int left, top, right, bottom; | |
7832 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
7833 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 7834 | |
2d66e025 MB |
7835 | int cw, ch; |
7836 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 7837 | |
2d66e025 | 7838 | if ( top < 0 ) |
3f296516 | 7839 | { |
2d66e025 | 7840 | ypos = r.GetTop(); |
3f296516 | 7841 | } |
2d66e025 MB |
7842 | else if ( bottom > ch ) |
7843 | { | |
7844 | int h = r.GetHeight(); | |
7845 | ypos = r.GetTop(); | |
7846 | for ( i = row-1; i >= 0; i-- ) | |
7847 | { | |
7c1cb261 VZ |
7848 | int rowHeight = GetRowHeight(i); |
7849 | if ( h + rowHeight > ch ) | |
7850 | break; | |
2d66e025 | 7851 | |
7c1cb261 VZ |
7852 | h += rowHeight; |
7853 | ypos -= rowHeight; | |
2d66e025 | 7854 | } |
f0102d2a VZ |
7855 | |
7856 | // we divide it later by GRID_SCROLL_LINE, make sure that we don't | |
7857 | // have rounding errors (this is important, because if we do, we | |
7858 | // might not scroll at all and some cells won't be redrawn) | |
275c4ae4 RD |
7859 | // |
7860 | // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full | |
7861 | // scroll unit... | |
97a9929e | 7862 | ypos += GRID_SCROLL_LINE_Y; |
2d66e025 MB |
7863 | } |
7864 | ||
7865 | if ( left < 0 ) | |
7866 | { | |
7867 | xpos = r.GetLeft(); | |
7868 | } | |
7869 | else if ( right > cw ) | |
7870 | { | |
73145b0e JS |
7871 | // position the view so that the cell is on the right |
7872 | int x0, y0; | |
7873 | CalcUnscrolledPosition(0, 0, &x0, &y0); | |
7874 | xpos = x0 + (right - cw); | |
f0102d2a VZ |
7875 | |
7876 | // see comment for ypos above | |
97a9929e | 7877 | xpos += GRID_SCROLL_LINE_X; |
2d66e025 MB |
7878 | } |
7879 | ||
7880 | if ( xpos != -1 || ypos != -1 ) | |
7881 | { | |
97a9929e VZ |
7882 | if ( xpos != -1 ) |
7883 | xpos /= GRID_SCROLL_LINE_X; | |
7884 | if ( ypos != -1 ) | |
7885 | ypos /= GRID_SCROLL_LINE_Y; | |
2d66e025 MB |
7886 | Scroll( xpos, ypos ); |
7887 | AdjustScrollbars(); | |
7888 | } | |
7889 | } | |
7890 | } | |
7891 | ||
7892 | ||
7893 | // | |
7894 | // ------ Grid cursor movement functions | |
7895 | // | |
7896 | ||
5c8fc7c1 | 7897 | bool wxGrid::MoveCursorUp( bool expandSelection ) |
2d66e025 MB |
7898 | { |
7899 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
f6bcfd97 | 7900 | m_currentCellCoords.GetRow() >= 0 ) |
2d66e025 | 7901 | { |
f6bcfd97 | 7902 | if ( expandSelection) |
d95b0c2b SN |
7903 | { |
7904 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
7905 | m_selectingKeyboard = m_currentCellCoords; | |
f6bcfd97 BP |
7906 | if ( m_selectingKeyboard.GetRow() > 0 ) |
7907 | { | |
7908 | m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 ); | |
7909 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
7910 | m_selectingKeyboard.GetCol() ); | |
c9097836 | 7911 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
f6bcfd97 | 7912 | } |
d95b0c2b | 7913 | } |
f6bcfd97 | 7914 | else if ( m_currentCellCoords.GetRow() > 0 ) |
aa5e1f75 SN |
7915 | { |
7916 | ClearSelection(); | |
7917 | MakeCellVisible( m_currentCellCoords.GetRow() - 1, | |
7918 | m_currentCellCoords.GetCol() ); | |
d95b0c2b SN |
7919 | SetCurrentCell( m_currentCellCoords.GetRow() - 1, |
7920 | m_currentCellCoords.GetCol() ); | |
aa5e1f75 | 7921 | } |
f6bcfd97 BP |
7922 | else |
7923 | return FALSE; | |
8f177c8e | 7924 | return TRUE; |
f85afd4e | 7925 | } |
2d66e025 MB |
7926 | |
7927 | return FALSE; | |
7928 | } | |
7929 | ||
7930 | ||
5c8fc7c1 | 7931 | bool wxGrid::MoveCursorDown( bool expandSelection ) |
2d66e025 | 7932 | { |
2d66e025 | 7933 | if ( m_currentCellCoords != wxGridNoCellCoords && |
f6bcfd97 | 7934 | m_currentCellCoords.GetRow() < m_numRows ) |
f85afd4e | 7935 | { |
5c8fc7c1 | 7936 | if ( expandSelection ) |
d95b0c2b SN |
7937 | { |
7938 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
7939 | m_selectingKeyboard = m_currentCellCoords; | |
f6bcfd97 BP |
7940 | if ( m_selectingKeyboard.GetRow() < m_numRows-1 ) |
7941 | { | |
7942 | m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 ); | |
7943 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
7944 | m_selectingKeyboard.GetCol() ); | |
c9097836 | 7945 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
f6bcfd97 | 7946 | } |
d95b0c2b | 7947 | } |
f6bcfd97 | 7948 | else if ( m_currentCellCoords.GetRow() < m_numRows - 1 ) |
aa5e1f75 SN |
7949 | { |
7950 | ClearSelection(); | |
7951 | MakeCellVisible( m_currentCellCoords.GetRow() + 1, | |
7952 | m_currentCellCoords.GetCol() ); | |
d95b0c2b SN |
7953 | SetCurrentCell( m_currentCellCoords.GetRow() + 1, |
7954 | m_currentCellCoords.GetCol() ); | |
aa5e1f75 | 7955 | } |
f6bcfd97 BP |
7956 | else |
7957 | return FALSE; | |
2d66e025 | 7958 | return TRUE; |
f85afd4e | 7959 | } |
2d66e025 MB |
7960 | |
7961 | return FALSE; | |
f85afd4e MB |
7962 | } |
7963 | ||
2d66e025 | 7964 | |
5c8fc7c1 | 7965 | bool wxGrid::MoveCursorLeft( bool expandSelection ) |
f85afd4e | 7966 | { |
2d66e025 | 7967 | if ( m_currentCellCoords != wxGridNoCellCoords && |
f6bcfd97 | 7968 | m_currentCellCoords.GetCol() >= 0 ) |
2d66e025 | 7969 | { |
5c8fc7c1 | 7970 | if ( expandSelection ) |
d95b0c2b SN |
7971 | { |
7972 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
7973 | m_selectingKeyboard = m_currentCellCoords; | |
f6bcfd97 BP |
7974 | if ( m_selectingKeyboard.GetCol() > 0 ) |
7975 | { | |
7976 | m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 ); | |
7977 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
7978 | m_selectingKeyboard.GetCol() ); | |
c9097836 | 7979 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
f6bcfd97 | 7980 | } |
d95b0c2b | 7981 | } |
f6bcfd97 | 7982 | else if ( m_currentCellCoords.GetCol() > 0 ) |
aa5e1f75 SN |
7983 | { |
7984 | ClearSelection(); | |
7985 | MakeCellVisible( m_currentCellCoords.GetRow(), | |
7986 | m_currentCellCoords.GetCol() - 1 ); | |
d95b0c2b SN |
7987 | SetCurrentCell( m_currentCellCoords.GetRow(), |
7988 | m_currentCellCoords.GetCol() - 1 ); | |
aa5e1f75 | 7989 | } |
f6bcfd97 BP |
7990 | else |
7991 | return FALSE; | |
2d66e025 MB |
7992 | return TRUE; |
7993 | } | |
7994 | ||
7995 | return FALSE; | |
7996 | } | |
7997 | ||
7998 | ||
5c8fc7c1 | 7999 | bool wxGrid::MoveCursorRight( bool expandSelection ) |
2d66e025 MB |
8000 | { |
8001 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
f6bcfd97 | 8002 | m_currentCellCoords.GetCol() < m_numCols ) |
f85afd4e | 8003 | { |
5c8fc7c1 | 8004 | if ( expandSelection ) |
d95b0c2b SN |
8005 | { |
8006 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
8007 | m_selectingKeyboard = m_currentCellCoords; | |
f6bcfd97 BP |
8008 | if ( m_selectingKeyboard.GetCol() < m_numCols - 1 ) |
8009 | { | |
8010 | m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 ); | |
8011 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
8012 | m_selectingKeyboard.GetCol() ); | |
c9097836 | 8013 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
f6bcfd97 | 8014 | } |
d95b0c2b | 8015 | } |
f6bcfd97 | 8016 | else if ( m_currentCellCoords.GetCol() < m_numCols - 1 ) |
aa5e1f75 SN |
8017 | { |
8018 | ClearSelection(); | |
8019 | MakeCellVisible( m_currentCellCoords.GetRow(), | |
8020 | m_currentCellCoords.GetCol() + 1 ); | |
8021 | SetCurrentCell( m_currentCellCoords.GetRow(), | |
d95b0c2b | 8022 | m_currentCellCoords.GetCol() + 1 ); |
aa5e1f75 | 8023 | } |
f6bcfd97 BP |
8024 | else |
8025 | return FALSE; | |
2d66e025 | 8026 | return TRUE; |
f85afd4e | 8027 | } |
8f177c8e | 8028 | |
2d66e025 MB |
8029 | return FALSE; |
8030 | } | |
8031 | ||
8032 | ||
8033 | bool wxGrid::MovePageUp() | |
8034 | { | |
8035 | if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE; | |
60ff3b99 | 8036 | |
2d66e025 MB |
8037 | int row = m_currentCellCoords.GetRow(); |
8038 | if ( row > 0 ) | |
f85afd4e | 8039 | { |
2d66e025 MB |
8040 | int cw, ch; |
8041 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 8042 | |
7c1cb261 | 8043 | int y = GetRowTop(row); |
a967f048 | 8044 | int newRow = internalYToRow( y - ch + 1 ); |
ef5df12b | 8045 | |
a967f048 | 8046 | if ( newRow == row ) |
2d66e025 | 8047 | { |
ef5df12b | 8048 | //row > 0 , so newrow can never be less than 0 here. |
2d66e025 MB |
8049 | newRow = row - 1; |
8050 | } | |
8f177c8e | 8051 | |
2d66e025 MB |
8052 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); |
8053 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
60ff3b99 | 8054 | |
f85afd4e MB |
8055 | return TRUE; |
8056 | } | |
2d66e025 MB |
8057 | |
8058 | return FALSE; | |
8059 | } | |
8060 | ||
8061 | bool wxGrid::MovePageDown() | |
8062 | { | |
8063 | if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE; | |
60ff3b99 | 8064 | |
2d66e025 | 8065 | int row = m_currentCellCoords.GetRow(); |
4b8f4912 | 8066 | if ( (row+1) < m_numRows ) |
f85afd4e | 8067 | { |
2d66e025 MB |
8068 | int cw, ch; |
8069 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 8070 | |
7c1cb261 | 8071 | int y = GetRowTop(row); |
a967f048 RG |
8072 | int newRow = internalYToRow( y + ch ); |
8073 | if ( newRow == row ) | |
2d66e025 | 8074 | { |
ef5df12b | 8075 | // row < m_numRows , so newrow can't overflow here. |
a967f048 | 8076 | newRow = row + 1; |
2d66e025 MB |
8077 | } |
8078 | ||
8079 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); | |
8080 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
60ff3b99 | 8081 | |
2d66e025 | 8082 | return TRUE; |
f85afd4e | 8083 | } |
2d66e025 MB |
8084 | |
8085 | return FALSE; | |
f85afd4e | 8086 | } |
8f177c8e | 8087 | |
5c8fc7c1 | 8088 | bool wxGrid::MoveCursorUpBlock( bool expandSelection ) |
2d66e025 MB |
8089 | { |
8090 | if ( m_table && | |
8091 | m_currentCellCoords != wxGridNoCellCoords && | |
8092 | m_currentCellCoords.GetRow() > 0 ) | |
8093 | { | |
8094 | int row = m_currentCellCoords.GetRow(); | |
8095 | int col = m_currentCellCoords.GetCol(); | |
8096 | ||
8097 | if ( m_table->IsEmptyCell(row, col) ) | |
8098 | { | |
8099 | // starting in an empty cell: find the next block of | |
8100 | // non-empty cells | |
8101 | // | |
8102 | while ( row > 0 ) | |
8103 | { | |
8104 | row-- ; | |
8105 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8106 | } | |
8107 | } | |
8108 | else if ( m_table->IsEmptyCell(row-1, col) ) | |
8109 | { | |
8110 | // starting at the top of a block: find the next block | |
8111 | // | |
8112 | row--; | |
8113 | while ( row > 0 ) | |
8114 | { | |
8115 | row-- ; | |
8116 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8117 | } | |
8118 | } | |
8119 | else | |
8120 | { | |
8121 | // starting within a block: find the top of the block | |
8122 | // | |
8123 | while ( row > 0 ) | |
8124 | { | |
8125 | row-- ; | |
8126 | if ( m_table->IsEmptyCell(row, col) ) | |
8127 | { | |
8128 | row++ ; | |
8129 | break; | |
8130 | } | |
8131 | } | |
8132 | } | |
f85afd4e | 8133 | |
2d66e025 | 8134 | MakeCellVisible( row, col ); |
5c8fc7c1 | 8135 | if ( expandSelection ) |
d95b0c2b SN |
8136 | { |
8137 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
c9097836 | 8138 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
d95b0c2b SN |
8139 | } |
8140 | else | |
aa5e1f75 SN |
8141 | { |
8142 | ClearSelection(); | |
8143 | SetCurrentCell( row, col ); | |
8144 | } | |
2d66e025 MB |
8145 | return TRUE; |
8146 | } | |
f85afd4e | 8147 | |
2d66e025 MB |
8148 | return FALSE; |
8149 | } | |
f85afd4e | 8150 | |
5c8fc7c1 | 8151 | bool wxGrid::MoveCursorDownBlock( bool expandSelection ) |
f85afd4e | 8152 | { |
2d66e025 MB |
8153 | if ( m_table && |
8154 | m_currentCellCoords != wxGridNoCellCoords && | |
8155 | m_currentCellCoords.GetRow() < m_numRows-1 ) | |
f85afd4e | 8156 | { |
2d66e025 MB |
8157 | int row = m_currentCellCoords.GetRow(); |
8158 | int col = m_currentCellCoords.GetCol(); | |
8159 | ||
8160 | if ( m_table->IsEmptyCell(row, col) ) | |
8161 | { | |
8162 | // starting in an empty cell: find the next block of | |
8163 | // non-empty cells | |
8164 | // | |
8165 | while ( row < m_numRows-1 ) | |
8166 | { | |
8167 | row++ ; | |
8168 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8169 | } | |
8170 | } | |
8171 | else if ( m_table->IsEmptyCell(row+1, col) ) | |
8172 | { | |
8173 | // starting at the bottom of a block: find the next block | |
8174 | // | |
8175 | row++; | |
8176 | while ( row < m_numRows-1 ) | |
8177 | { | |
8178 | row++ ; | |
8179 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8180 | } | |
8181 | } | |
8182 | else | |
8183 | { | |
8184 | // starting within a block: find the bottom of the block | |
8185 | // | |
8186 | while ( row < m_numRows-1 ) | |
8187 | { | |
8188 | row++ ; | |
8189 | if ( m_table->IsEmptyCell(row, col) ) | |
8190 | { | |
8191 | row-- ; | |
8192 | break; | |
8193 | } | |
8194 | } | |
8195 | } | |
8196 | ||
8197 | MakeCellVisible( row, col ); | |
5c8fc7c1 | 8198 | if ( expandSelection ) |
d95b0c2b SN |
8199 | { |
8200 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
c9097836 | 8201 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
d95b0c2b SN |
8202 | } |
8203 | else | |
aa5e1f75 SN |
8204 | { |
8205 | ClearSelection(); | |
8206 | SetCurrentCell( row, col ); | |
8207 | } | |
2d66e025 MB |
8208 | |
8209 | return TRUE; | |
f85afd4e | 8210 | } |
f85afd4e | 8211 | |
2d66e025 MB |
8212 | return FALSE; |
8213 | } | |
f85afd4e | 8214 | |
5c8fc7c1 | 8215 | bool wxGrid::MoveCursorLeftBlock( bool expandSelection ) |
f85afd4e | 8216 | { |
2d66e025 MB |
8217 | if ( m_table && |
8218 | m_currentCellCoords != wxGridNoCellCoords && | |
8219 | m_currentCellCoords.GetCol() > 0 ) | |
f85afd4e | 8220 | { |
2d66e025 MB |
8221 | int row = m_currentCellCoords.GetRow(); |
8222 | int col = m_currentCellCoords.GetCol(); | |
8223 | ||
8224 | if ( m_table->IsEmptyCell(row, col) ) | |
8225 | { | |
8226 | // starting in an empty cell: find the next block of | |
8227 | // non-empty cells | |
8228 | // | |
8229 | while ( col > 0 ) | |
8230 | { | |
8231 | col-- ; | |
8232 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8233 | } | |
8234 | } | |
8235 | else if ( m_table->IsEmptyCell(row, col-1) ) | |
8236 | { | |
8237 | // starting at the left of a block: find the next block | |
8238 | // | |
8239 | col--; | |
8240 | while ( col > 0 ) | |
8241 | { | |
8242 | col-- ; | |
8243 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8244 | } | |
8245 | } | |
8246 | else | |
8247 | { | |
8248 | // starting within a block: find the left of the block | |
8249 | // | |
8250 | while ( col > 0 ) | |
8251 | { | |
8252 | col-- ; | |
8253 | if ( m_table->IsEmptyCell(row, col) ) | |
8254 | { | |
8255 | col++ ; | |
8256 | break; | |
8257 | } | |
8258 | } | |
8259 | } | |
f85afd4e | 8260 | |
2d66e025 | 8261 | MakeCellVisible( row, col ); |
5c8fc7c1 | 8262 | if ( expandSelection ) |
d95b0c2b SN |
8263 | { |
8264 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
c9097836 | 8265 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
d95b0c2b SN |
8266 | } |
8267 | else | |
aa5e1f75 SN |
8268 | { |
8269 | ClearSelection(); | |
8270 | SetCurrentCell( row, col ); | |
8271 | } | |
8f177c8e | 8272 | |
2d66e025 | 8273 | return TRUE; |
f85afd4e | 8274 | } |
2d66e025 MB |
8275 | |
8276 | return FALSE; | |
f85afd4e MB |
8277 | } |
8278 | ||
5c8fc7c1 | 8279 | bool wxGrid::MoveCursorRightBlock( bool expandSelection ) |
f85afd4e | 8280 | { |
2d66e025 MB |
8281 | if ( m_table && |
8282 | m_currentCellCoords != wxGridNoCellCoords && | |
8283 | m_currentCellCoords.GetCol() < m_numCols-1 ) | |
f85afd4e | 8284 | { |
2d66e025 MB |
8285 | int row = m_currentCellCoords.GetRow(); |
8286 | int col = m_currentCellCoords.GetCol(); | |
8f177c8e | 8287 | |
2d66e025 MB |
8288 | if ( m_table->IsEmptyCell(row, col) ) |
8289 | { | |
8290 | // starting in an empty cell: find the next block of | |
8291 | // non-empty cells | |
8292 | // | |
8293 | while ( col < m_numCols-1 ) | |
8294 | { | |
8295 | col++ ; | |
8296 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8297 | } | |
8298 | } | |
8299 | else if ( m_table->IsEmptyCell(row, col+1) ) | |
8300 | { | |
8301 | // starting at the right of a block: find the next block | |
8302 | // | |
8303 | col++; | |
8304 | while ( col < m_numCols-1 ) | |
8305 | { | |
8306 | col++ ; | |
8307 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8308 | } | |
8309 | } | |
8310 | else | |
8311 | { | |
8312 | // starting within a block: find the right of the block | |
8313 | // | |
8314 | while ( col < m_numCols-1 ) | |
8315 | { | |
8316 | col++ ; | |
8317 | if ( m_table->IsEmptyCell(row, col) ) | |
8318 | { | |
8319 | col-- ; | |
8320 | break; | |
8321 | } | |
8322 | } | |
8323 | } | |
8f177c8e | 8324 | |
2d66e025 | 8325 | MakeCellVisible( row, col ); |
5c8fc7c1 | 8326 | if ( expandSelection ) |
d95b0c2b SN |
8327 | { |
8328 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
c9097836 | 8329 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
d95b0c2b SN |
8330 | } |
8331 | else | |
aa5e1f75 SN |
8332 | { |
8333 | ClearSelection(); | |
8334 | SetCurrentCell( row, col ); | |
8335 | } | |
f85afd4e | 8336 | |
2d66e025 | 8337 | return TRUE; |
f85afd4e | 8338 | } |
2d66e025 MB |
8339 | |
8340 | return FALSE; | |
f85afd4e MB |
8341 | } |
8342 | ||
8343 | ||
2d66e025 | 8344 | |
f85afd4e | 8345 | // |
2d66e025 | 8346 | // ------ Label values and formatting |
f85afd4e MB |
8347 | // |
8348 | ||
8349 | void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) | |
8350 | { | |
8351 | *horiz = m_rowLabelHorizAlign; | |
8352 | *vert = m_rowLabelVertAlign; | |
8353 | } | |
8354 | ||
8355 | void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) | |
8356 | { | |
8357 | *horiz = m_colLabelHorizAlign; | |
8358 | *vert = m_colLabelVertAlign; | |
8359 | } | |
8360 | ||
d43851f7 JS |
8361 | int wxGrid::GetColLabelTextOrientation() |
8362 | { | |
8363 | return m_colLabelTextOrientation; | |
8364 | } | |
8365 | ||
f85afd4e MB |
8366 | wxString wxGrid::GetRowLabelValue( int row ) |
8367 | { | |
8368 | if ( m_table ) | |
8369 | { | |
8370 | return m_table->GetRowLabelValue( row ); | |
8371 | } | |
8372 | else | |
8373 | { | |
8374 | wxString s; | |
8375 | s << row; | |
8376 | return s; | |
8377 | } | |
8378 | } | |
8379 | ||
8380 | wxString wxGrid::GetColLabelValue( int col ) | |
8381 | { | |
8382 | if ( m_table ) | |
8383 | { | |
8384 | return m_table->GetColLabelValue( col ); | |
8385 | } | |
8386 | else | |
8387 | { | |
8388 | wxString s; | |
8389 | s << col; | |
8390 | return s; | |
8391 | } | |
8392 | } | |
8393 | ||
2e8cd977 | 8394 | |
f85afd4e MB |
8395 | void wxGrid::SetRowLabelSize( int width ) |
8396 | { | |
2e8cd977 MB |
8397 | width = wxMax( width, 0 ); |
8398 | if ( width != m_rowLabelWidth ) | |
8399 | { | |
2e8cd977 MB |
8400 | if ( width == 0 ) |
8401 | { | |
8402 | m_rowLabelWin->Show( FALSE ); | |
7807d81c | 8403 | m_cornerLabelWin->Show( FALSE ); |
2e8cd977 | 8404 | } |
7807d81c | 8405 | else if ( m_rowLabelWidth == 0 ) |
2e8cd977 | 8406 | { |
7807d81c MB |
8407 | m_rowLabelWin->Show( TRUE ); |
8408 | if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE ); | |
2e8cd977 | 8409 | } |
b99be8fb | 8410 | |
2e8cd977 | 8411 | m_rowLabelWidth = width; |
7807d81c | 8412 | CalcWindowSizes(); |
27f35b66 | 8413 | wxScrolledWindow::Refresh( TRUE ); |
2e8cd977 | 8414 | } |
f85afd4e MB |
8415 | } |
8416 | ||
2e8cd977 | 8417 | |
f85afd4e MB |
8418 | void wxGrid::SetColLabelSize( int height ) |
8419 | { | |
7807d81c | 8420 | height = wxMax( height, 0 ); |
2e8cd977 MB |
8421 | if ( height != m_colLabelHeight ) |
8422 | { | |
2e8cd977 MB |
8423 | if ( height == 0 ) |
8424 | { | |
2e8cd977 | 8425 | m_colLabelWin->Show( FALSE ); |
7807d81c | 8426 | m_cornerLabelWin->Show( FALSE ); |
2e8cd977 | 8427 | } |
7807d81c | 8428 | else if ( m_colLabelHeight == 0 ) |
2e8cd977 | 8429 | { |
7807d81c MB |
8430 | m_colLabelWin->Show( TRUE ); |
8431 | if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE ); | |
2e8cd977 | 8432 | } |
7807d81c | 8433 | |
2e8cd977 | 8434 | m_colLabelHeight = height; |
7807d81c | 8435 | CalcWindowSizes(); |
27f35b66 | 8436 | wxScrolledWindow::Refresh( TRUE ); |
2e8cd977 | 8437 | } |
f85afd4e MB |
8438 | } |
8439 | ||
2e8cd977 | 8440 | |
f85afd4e MB |
8441 | void wxGrid::SetLabelBackgroundColour( const wxColour& colour ) |
8442 | { | |
2d66e025 MB |
8443 | if ( m_labelBackgroundColour != colour ) |
8444 | { | |
8445 | m_labelBackgroundColour = colour; | |
8446 | m_rowLabelWin->SetBackgroundColour( colour ); | |
8447 | m_colLabelWin->SetBackgroundColour( colour ); | |
8448 | m_cornerLabelWin->SetBackgroundColour( colour ); | |
8449 | ||
8450 | if ( !GetBatchCount() ) | |
8451 | { | |
8452 | m_rowLabelWin->Refresh(); | |
8453 | m_colLabelWin->Refresh(); | |
8454 | m_cornerLabelWin->Refresh(); | |
8455 | } | |
8456 | } | |
f85afd4e MB |
8457 | } |
8458 | ||
8459 | void wxGrid::SetLabelTextColour( const wxColour& colour ) | |
8460 | { | |
2d66e025 MB |
8461 | if ( m_labelTextColour != colour ) |
8462 | { | |
8463 | m_labelTextColour = colour; | |
8464 | if ( !GetBatchCount() ) | |
8465 | { | |
8466 | m_rowLabelWin->Refresh(); | |
8467 | m_colLabelWin->Refresh(); | |
8468 | } | |
8469 | } | |
f85afd4e MB |
8470 | } |
8471 | ||
8472 | void wxGrid::SetLabelFont( const wxFont& font ) | |
8473 | { | |
8474 | m_labelFont = font; | |
2d66e025 MB |
8475 | if ( !GetBatchCount() ) |
8476 | { | |
8477 | m_rowLabelWin->Refresh(); | |
8478 | m_colLabelWin->Refresh(); | |
8479 | } | |
f85afd4e MB |
8480 | } |
8481 | ||
8482 | void wxGrid::SetRowLabelAlignment( int horiz, int vert ) | |
8483 | { | |
4c7277db MB |
8484 | // allow old (incorrect) defs to be used |
8485 | switch ( horiz ) | |
8486 | { | |
8487 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
8488 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
8489 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
8490 | } | |
84912ef8 | 8491 | |
4c7277db MB |
8492 | switch ( vert ) |
8493 | { | |
8494 | case wxTOP: vert = wxALIGN_TOP; break; | |
8495 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
8496 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
8497 | } | |
84912ef8 | 8498 | |
4c7277db | 8499 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) |
f85afd4e MB |
8500 | { |
8501 | m_rowLabelHorizAlign = horiz; | |
8502 | } | |
8f177c8e | 8503 | |
4c7277db | 8504 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) |
f85afd4e MB |
8505 | { |
8506 | m_rowLabelVertAlign = vert; | |
8507 | } | |
8508 | ||
2d66e025 MB |
8509 | if ( !GetBatchCount() ) |
8510 | { | |
8511 | m_rowLabelWin->Refresh(); | |
60ff3b99 | 8512 | } |
f85afd4e MB |
8513 | } |
8514 | ||
8515 | void wxGrid::SetColLabelAlignment( int horiz, int vert ) | |
8516 | { | |
4c7277db MB |
8517 | // allow old (incorrect) defs to be used |
8518 | switch ( horiz ) | |
8519 | { | |
8520 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
8521 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
8522 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
8523 | } | |
84912ef8 | 8524 | |
4c7277db MB |
8525 | switch ( vert ) |
8526 | { | |
8527 | case wxTOP: vert = wxALIGN_TOP; break; | |
8528 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
8529 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
8530 | } | |
84912ef8 | 8531 | |
4c7277db | 8532 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) |
f85afd4e MB |
8533 | { |
8534 | m_colLabelHorizAlign = horiz; | |
8535 | } | |
8f177c8e | 8536 | |
4c7277db | 8537 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) |
f85afd4e MB |
8538 | { |
8539 | m_colLabelVertAlign = vert; | |
8540 | } | |
8541 | ||
2d66e025 MB |
8542 | if ( !GetBatchCount() ) |
8543 | { | |
2d66e025 | 8544 | m_colLabelWin->Refresh(); |
60ff3b99 | 8545 | } |
f85afd4e MB |
8546 | } |
8547 | ||
d43851f7 JS |
8548 | // Note: under MSW, the default column label font must be changed because it |
8549 | // does not support vertical printing | |
8550 | // | |
8551 | // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD); | |
8552 | // pGrid->SetLabelFont(font); | |
8553 | // pGrid->SetColLabelTextOrientation(wxVERTICAL); | |
8554 | // | |
8555 | void wxGrid::SetColLabelTextOrientation( int textOrientation ) | |
8556 | { | |
8557 | if( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL ) | |
8558 | { | |
8559 | m_colLabelTextOrientation = textOrientation; | |
8560 | } | |
8561 | ||
8562 | if ( !GetBatchCount() ) | |
8563 | { | |
8564 | m_colLabelWin->Refresh(); | |
8565 | } | |
8566 | } | |
8567 | ||
f85afd4e MB |
8568 | void wxGrid::SetRowLabelValue( int row, const wxString& s ) |
8569 | { | |
8570 | if ( m_table ) | |
8571 | { | |
8572 | m_table->SetRowLabelValue( row, s ); | |
2d66e025 MB |
8573 | if ( !GetBatchCount() ) |
8574 | { | |
70c7a608 SN |
8575 | wxRect rect = CellToRect( row, 0); |
8576 | if ( rect.height > 0 ) | |
8577 | { | |
cb309039 | 8578 | CalcScrolledPosition(0, rect.y, &rect.x, &rect.y); |
b1944ebc | 8579 | rect.x = 0; |
70c7a608 SN |
8580 | rect.width = m_rowLabelWidth; |
8581 | m_rowLabelWin->Refresh( TRUE, &rect ); | |
8582 | } | |
2d66e025 | 8583 | } |
f85afd4e MB |
8584 | } |
8585 | } | |
8586 | ||
8587 | void wxGrid::SetColLabelValue( int col, const wxString& s ) | |
8588 | { | |
8589 | if ( m_table ) | |
8590 | { | |
8591 | m_table->SetColLabelValue( col, s ); | |
2d66e025 MB |
8592 | if ( !GetBatchCount() ) |
8593 | { | |
70c7a608 SN |
8594 | wxRect rect = CellToRect( 0, col ); |
8595 | if ( rect.width > 0 ) | |
8596 | { | |
cb309039 | 8597 | CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y); |
b1944ebc | 8598 | rect.y = 0; |
70c7a608 SN |
8599 | rect.height = m_colLabelHeight; |
8600 | m_colLabelWin->Refresh( TRUE, &rect ); | |
8601 | } | |
2d66e025 | 8602 | } |
f85afd4e MB |
8603 | } |
8604 | } | |
8605 | ||
8606 | void wxGrid::SetGridLineColour( const wxColour& colour ) | |
8607 | { | |
2d66e025 MB |
8608 | if ( m_gridLineColour != colour ) |
8609 | { | |
8610 | m_gridLineColour = colour; | |
60ff3b99 | 8611 | |
2d66e025 MB |
8612 | wxClientDC dc( m_gridWin ); |
8613 | PrepareDC( dc ); | |
c6a51dcd | 8614 | DrawAllGridLines( dc, wxRegion() ); |
2d66e025 | 8615 | } |
f85afd4e MB |
8616 | } |
8617 | ||
f6bcfd97 BP |
8618 | |
8619 | void wxGrid::SetCellHighlightColour( const wxColour& colour ) | |
8620 | { | |
8621 | if ( m_cellHighlightColour != colour ) | |
8622 | { | |
8623 | m_cellHighlightColour = colour; | |
8624 | ||
8625 | wxClientDC dc( m_gridWin ); | |
8626 | PrepareDC( dc ); | |
8627 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
8628 | DrawCellHighlight(dc, attr); | |
8629 | attr->DecRef(); | |
8630 | } | |
8631 | } | |
8632 | ||
d2520c85 RD |
8633 | void wxGrid::SetCellHighlightPenWidth(int width) |
8634 | { | |
8635 | if (m_cellHighlightPenWidth != width) { | |
8636 | m_cellHighlightPenWidth = width; | |
8637 | ||
8638 | // Just redrawing the cell highlight is not enough since that won't | |
8639 | // make any visible change if the the thickness is getting smaller. | |
8640 | int row = m_currentCellCoords.GetRow(); | |
8641 | int col = m_currentCellCoords.GetCol(); | |
8642 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
8643 | return; | |
8644 | wxRect rect = CellToRect(row, col); | |
8645 | m_gridWin->Refresh(TRUE, &rect); | |
8646 | } | |
8647 | } | |
8648 | ||
8649 | void wxGrid::SetCellHighlightROPenWidth(int width) | |
8650 | { | |
8651 | if (m_cellHighlightROPenWidth != width) { | |
8652 | m_cellHighlightROPenWidth = width; | |
8653 | ||
8654 | // Just redrawing the cell highlight is not enough since that won't | |
8655 | // make any visible change if the the thickness is getting smaller. | |
8656 | int row = m_currentCellCoords.GetRow(); | |
8657 | int col = m_currentCellCoords.GetCol(); | |
8658 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
8659 | return; | |
8660 | wxRect rect = CellToRect(row, col); | |
8661 | m_gridWin->Refresh(TRUE, &rect); | |
8662 | } | |
8663 | } | |
8664 | ||
f85afd4e MB |
8665 | void wxGrid::EnableGridLines( bool enable ) |
8666 | { | |
8667 | if ( enable != m_gridLinesEnabled ) | |
8668 | { | |
8669 | m_gridLinesEnabled = enable; | |
2d66e025 MB |
8670 | |
8671 | if ( !GetBatchCount() ) | |
8672 | { | |
8673 | if ( enable ) | |
8674 | { | |
8675 | wxClientDC dc( m_gridWin ); | |
8676 | PrepareDC( dc ); | |
c6a51dcd | 8677 | DrawAllGridLines( dc, wxRegion() ); |
2d66e025 MB |
8678 | } |
8679 | else | |
8680 | { | |
8681 | m_gridWin->Refresh(); | |
8682 | } | |
8683 | } | |
f85afd4e MB |
8684 | } |
8685 | } | |
8686 | ||
8687 | ||
8688 | int wxGrid::GetDefaultRowSize() | |
8689 | { | |
8690 | return m_defaultRowHeight; | |
8691 | } | |
8692 | ||
8693 | int wxGrid::GetRowSize( int row ) | |
8694 | { | |
b99be8fb VZ |
8695 | wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") ); |
8696 | ||
7c1cb261 | 8697 | return GetRowHeight(row); |
f85afd4e MB |
8698 | } |
8699 | ||
8700 | int wxGrid::GetDefaultColSize() | |
8701 | { | |
8702 | return m_defaultColWidth; | |
8703 | } | |
8704 | ||
8705 | int wxGrid::GetColSize( int col ) | |
8706 | { | |
b99be8fb VZ |
8707 | wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") ); |
8708 | ||
7c1cb261 | 8709 | return GetColWidth(col); |
f85afd4e MB |
8710 | } |
8711 | ||
2e9a6788 VZ |
8712 | // ============================================================================ |
8713 | // access to the grid attributes: each of them has a default value in the grid | |
8714 | // itself and may be overidden on a per-cell basis | |
8715 | // ============================================================================ | |
8716 | ||
8717 | // ---------------------------------------------------------------------------- | |
8718 | // setting default attributes | |
8719 | // ---------------------------------------------------------------------------- | |
8720 | ||
8721 | void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col ) | |
8722 | { | |
2796cce3 | 8723 | m_defaultCellAttr->SetBackgroundColour(col); |
c916e13b RR |
8724 | #ifdef __WXGTK__ |
8725 | m_gridWin->SetBackgroundColour(col); | |
8726 | #endif | |
2e9a6788 VZ |
8727 | } |
8728 | ||
8729 | void wxGrid::SetDefaultCellTextColour( const wxColour& col ) | |
8730 | { | |
2796cce3 | 8731 | m_defaultCellAttr->SetTextColour(col); |
2e9a6788 VZ |
8732 | } |
8733 | ||
8734 | void wxGrid::SetDefaultCellAlignment( int horiz, int vert ) | |
8735 | { | |
2796cce3 | 8736 | m_defaultCellAttr->SetAlignment(horiz, vert); |
2e9a6788 VZ |
8737 | } |
8738 | ||
27f35b66 SN |
8739 | void wxGrid::SetDefaultCellOverflow( bool allow ) |
8740 | { | |
8741 | m_defaultCellAttr->SetOverflow(allow); | |
8742 | } | |
8743 | ||
2e9a6788 VZ |
8744 | void wxGrid::SetDefaultCellFont( const wxFont& font ) |
8745 | { | |
2796cce3 RD |
8746 | m_defaultCellAttr->SetFont(font); |
8747 | } | |
8748 | ||
0ba143c9 RD |
8749 | void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer) |
8750 | { | |
8751 | m_defaultCellAttr->SetRenderer(renderer); | |
8752 | } | |
2e9a6788 | 8753 | |
0ba143c9 RD |
8754 | void wxGrid::SetDefaultEditor(wxGridCellEditor *editor) |
8755 | { | |
8756 | m_defaultCellAttr->SetEditor(editor); | |
8757 | } | |
9b4aede2 | 8758 | |
2e9a6788 VZ |
8759 | // ---------------------------------------------------------------------------- |
8760 | // access to the default attrbiutes | |
8761 | // ---------------------------------------------------------------------------- | |
8762 | ||
f85afd4e MB |
8763 | wxColour wxGrid::GetDefaultCellBackgroundColour() |
8764 | { | |
2796cce3 | 8765 | return m_defaultCellAttr->GetBackgroundColour(); |
f85afd4e MB |
8766 | } |
8767 | ||
2e9a6788 VZ |
8768 | wxColour wxGrid::GetDefaultCellTextColour() |
8769 | { | |
2796cce3 | 8770 | return m_defaultCellAttr->GetTextColour(); |
2e9a6788 VZ |
8771 | } |
8772 | ||
8773 | wxFont wxGrid::GetDefaultCellFont() | |
8774 | { | |
2796cce3 | 8775 | return m_defaultCellAttr->GetFont(); |
2e9a6788 VZ |
8776 | } |
8777 | ||
8778 | void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) | |
8779 | { | |
2796cce3 | 8780 | m_defaultCellAttr->GetAlignment(horiz, vert); |
2e9a6788 VZ |
8781 | } |
8782 | ||
27f35b66 SN |
8783 | bool wxGrid::GetDefaultCellOverflow() |
8784 | { | |
8785 | return m_defaultCellAttr->GetOverflow(); | |
8786 | } | |
8787 | ||
0ba143c9 RD |
8788 | wxGridCellRenderer *wxGrid::GetDefaultRenderer() const |
8789 | { | |
0b190b0f | 8790 | return m_defaultCellAttr->GetRenderer(NULL, 0, 0); |
0ba143c9 | 8791 | } |
ab79958a | 8792 | |
0ba143c9 RD |
8793 | wxGridCellEditor *wxGrid::GetDefaultEditor() const |
8794 | { | |
28a77bc4 | 8795 | return m_defaultCellAttr->GetEditor(NULL,0,0); |
0ba143c9 | 8796 | } |
9b4aede2 | 8797 | |
2e9a6788 VZ |
8798 | // ---------------------------------------------------------------------------- |
8799 | // access to cell attributes | |
8800 | // ---------------------------------------------------------------------------- | |
8801 | ||
b99be8fb | 8802 | wxColour wxGrid::GetCellBackgroundColour(int row, int col) |
f85afd4e | 8803 | { |
0a976765 | 8804 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 8805 | wxColour colour = attr->GetBackgroundColour(); |
39bcce60 | 8806 | attr->DecRef(); |
b99be8fb | 8807 | return colour; |
f85afd4e MB |
8808 | } |
8809 | ||
b99be8fb | 8810 | wxColour wxGrid::GetCellTextColour( int row, int col ) |
f85afd4e | 8811 | { |
0a976765 | 8812 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 8813 | wxColour colour = attr->GetTextColour(); |
39bcce60 | 8814 | attr->DecRef(); |
b99be8fb | 8815 | return colour; |
f85afd4e MB |
8816 | } |
8817 | ||
b99be8fb | 8818 | wxFont wxGrid::GetCellFont( int row, int col ) |
f85afd4e | 8819 | { |
0a976765 | 8820 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 8821 | wxFont font = attr->GetFont(); |
39bcce60 | 8822 | attr->DecRef(); |
b99be8fb | 8823 | return font; |
f85afd4e MB |
8824 | } |
8825 | ||
b99be8fb | 8826 | void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) |
f85afd4e | 8827 | { |
0a976765 | 8828 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 8829 | attr->GetAlignment(horiz, vert); |
39bcce60 | 8830 | attr->DecRef(); |
2e9a6788 VZ |
8831 | } |
8832 | ||
27f35b66 SN |
8833 | bool wxGrid::GetCellOverflow( int row, int col ) |
8834 | { | |
8835 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
8836 | bool allow = attr->GetOverflow(); | |
8837 | attr->DecRef(); | |
8838 | return allow; | |
8839 | } | |
8840 | ||
8841 | void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) | |
8842 | { | |
8843 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
8844 | attr->GetSize( num_rows, num_cols ); | |
8845 | attr->DecRef(); | |
8846 | } | |
8847 | ||
2796cce3 RD |
8848 | wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) |
8849 | { | |
8850 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 8851 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); |
2796cce3 | 8852 | attr->DecRef(); |
0b190b0f | 8853 | |
2796cce3 RD |
8854 | return renderer; |
8855 | } | |
8856 | ||
9b4aede2 RD |
8857 | wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) |
8858 | { | |
8859 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 8860 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
9b4aede2 | 8861 | attr->DecRef(); |
0b190b0f | 8862 | |
9b4aede2 RD |
8863 | return editor; |
8864 | } | |
8865 | ||
283b7808 VZ |
8866 | bool wxGrid::IsReadOnly(int row, int col) const |
8867 | { | |
8868 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
8869 | bool isReadOnly = attr->IsReadOnly(); | |
8870 | attr->DecRef(); | |
8871 | return isReadOnly; | |
8872 | } | |
8873 | ||
2e9a6788 | 8874 | // ---------------------------------------------------------------------------- |
758cbedf | 8875 | // attribute support: cache, automatic provider creation, ... |
2e9a6788 VZ |
8876 | // ---------------------------------------------------------------------------- |
8877 | ||
8878 | bool wxGrid::CanHaveAttributes() | |
8879 | { | |
8880 | if ( !m_table ) | |
8881 | { | |
8882 | return FALSE; | |
8883 | } | |
8884 | ||
f2d76237 | 8885 | return m_table->CanHaveAttributes(); |
2e9a6788 VZ |
8886 | } |
8887 | ||
0a976765 VZ |
8888 | void wxGrid::ClearAttrCache() |
8889 | { | |
8890 | if ( m_attrCache.row != -1 ) | |
8891 | { | |
39bcce60 | 8892 | wxSafeDecRef(m_attrCache.attr); |
19d7140e | 8893 | m_attrCache.attr = NULL; |
0a976765 VZ |
8894 | m_attrCache.row = -1; |
8895 | } | |
8896 | } | |
8897 | ||
8898 | void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const | |
8899 | { | |
2b5f62a0 VZ |
8900 | if ( attr != NULL ) |
8901 | { | |
8902 | wxGrid *self = (wxGrid *)this; // const_cast | |
0a976765 | 8903 | |
2b5f62a0 VZ |
8904 | self->ClearAttrCache(); |
8905 | self->m_attrCache.row = row; | |
8906 | self->m_attrCache.col = col; | |
8907 | self->m_attrCache.attr = attr; | |
8908 | wxSafeIncRef(attr); | |
8909 | } | |
0a976765 VZ |
8910 | } |
8911 | ||
8912 | bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const | |
8913 | { | |
8914 | if ( row == m_attrCache.row && col == m_attrCache.col ) | |
8915 | { | |
8916 | *attr = m_attrCache.attr; | |
39bcce60 | 8917 | wxSafeIncRef(m_attrCache.attr); |
0a976765 VZ |
8918 | |
8919 | #ifdef DEBUG_ATTR_CACHE | |
8920 | gs_nAttrCacheHits++; | |
8921 | #endif | |
8922 | ||
8923 | return TRUE; | |
8924 | } | |
8925 | else | |
8926 | { | |
8927 | #ifdef DEBUG_ATTR_CACHE | |
8928 | gs_nAttrCacheMisses++; | |
8929 | #endif | |
8930 | return FALSE; | |
8931 | } | |
8932 | } | |
8933 | ||
2e9a6788 VZ |
8934 | wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const |
8935 | { | |
a373d23b SN |
8936 | wxGridCellAttr *attr = NULL; |
8937 | // Additional test to avoid looking at the cache e.g. for | |
8938 | // wxNoCellCoords, as this will confuse memory management. | |
8939 | if ( row >= 0 ) | |
8940 | { | |
3ed884a0 SN |
8941 | if ( !LookupAttr(row, col, &attr) ) |
8942 | { | |
8943 | attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any) | |
8944 | : (wxGridCellAttr *)NULL; | |
8945 | CacheAttr(row, col, attr); | |
8946 | } | |
0a976765 | 8947 | } |
508011ce VZ |
8948 | if (attr) |
8949 | { | |
2796cce3 | 8950 | attr->SetDefAttr(m_defaultCellAttr); |
508011ce VZ |
8951 | } |
8952 | else | |
8953 | { | |
2796cce3 RD |
8954 | attr = m_defaultCellAttr; |
8955 | attr->IncRef(); | |
8956 | } | |
2e9a6788 | 8957 | |
0a976765 VZ |
8958 | return attr; |
8959 | } | |
8960 | ||
8961 | wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const | |
8962 | { | |
19d7140e | 8963 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
0a976765 | 8964 | |
1df4050d VZ |
8965 | wxCHECK_MSG( m_table, attr, |
8966 | _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") ); | |
8967 | ||
8968 | attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell); | |
8969 | if ( !attr ) | |
8970 | { | |
8971 | attr = new wxGridCellAttr(m_defaultCellAttr); | |
8972 | ||
8973 | // artificially inc the ref count to match DecRef() in caller | |
8974 | attr->IncRef(); | |
8975 | m_table->SetAttr(attr, row, col); | |
8976 | } | |
0a976765 | 8977 | |
2e9a6788 VZ |
8978 | return attr; |
8979 | } | |
8980 | ||
0b190b0f VZ |
8981 | // ---------------------------------------------------------------------------- |
8982 | // setting column attributes (wrappers around SetColAttr) | |
8983 | // ---------------------------------------------------------------------------- | |
8984 | ||
8985 | void wxGrid::SetColFormatBool(int col) | |
8986 | { | |
8987 | SetColFormatCustom(col, wxGRID_VALUE_BOOL); | |
8988 | } | |
8989 | ||
8990 | void wxGrid::SetColFormatNumber(int col) | |
8991 | { | |
8992 | SetColFormatCustom(col, wxGRID_VALUE_NUMBER); | |
8993 | } | |
8994 | ||
8995 | void wxGrid::SetColFormatFloat(int col, int width, int precision) | |
8996 | { | |
8997 | wxString typeName = wxGRID_VALUE_FLOAT; | |
8998 | if ( (width != -1) || (precision != -1) ) | |
8999 | { | |
9000 | typeName << _T(':') << width << _T(',') << precision; | |
9001 | } | |
9002 | ||
9003 | SetColFormatCustom(col, typeName); | |
9004 | } | |
9005 | ||
9006 | void wxGrid::SetColFormatCustom(int col, const wxString& typeName) | |
9007 | { | |
19d7140e VZ |
9008 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
9009 | ||
9010 | attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col ); | |
9011 | if(!attr) | |
9012 | attr = new wxGridCellAttr; | |
0b190b0f VZ |
9013 | wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName); |
9014 | attr->SetRenderer(renderer); | |
9015 | ||
9016 | SetColAttr(col, attr); | |
19d7140e | 9017 | |
0b190b0f VZ |
9018 | } |
9019 | ||
758cbedf VZ |
9020 | // ---------------------------------------------------------------------------- |
9021 | // setting cell attributes: this is forwarded to the table | |
9022 | // ---------------------------------------------------------------------------- | |
9023 | ||
27f35b66 SN |
9024 | void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr) |
9025 | { | |
9026 | if ( CanHaveAttributes() ) | |
9027 | { | |
9028 | m_table->SetAttr(attr, row, col); | |
9029 | ClearAttrCache(); | |
9030 | } | |
9031 | else | |
9032 | { | |
9033 | wxSafeDecRef(attr); | |
9034 | } | |
9035 | } | |
9036 | ||
758cbedf VZ |
9037 | void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr) |
9038 | { | |
9039 | if ( CanHaveAttributes() ) | |
9040 | { | |
9041 | m_table->SetRowAttr(attr, row); | |
19d7140e | 9042 | ClearAttrCache(); |
758cbedf VZ |
9043 | } |
9044 | else | |
9045 | { | |
39bcce60 | 9046 | wxSafeDecRef(attr); |
758cbedf VZ |
9047 | } |
9048 | } | |
9049 | ||
9050 | void wxGrid::SetColAttr(int col, wxGridCellAttr *attr) | |
9051 | { | |
9052 | if ( CanHaveAttributes() ) | |
9053 | { | |
9054 | m_table->SetColAttr(attr, col); | |
19d7140e | 9055 | ClearAttrCache(); |
758cbedf VZ |
9056 | } |
9057 | else | |
9058 | { | |
39bcce60 | 9059 | wxSafeDecRef(attr); |
758cbedf VZ |
9060 | } |
9061 | } | |
9062 | ||
2e9a6788 VZ |
9063 | void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour ) |
9064 | { | |
9065 | if ( CanHaveAttributes() ) | |
9066 | { | |
0a976765 | 9067 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
9068 | attr->SetBackgroundColour(colour); |
9069 | attr->DecRef(); | |
9070 | } | |
9071 | } | |
9072 | ||
9073 | void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour ) | |
9074 | { | |
9075 | if ( CanHaveAttributes() ) | |
9076 | { | |
0a976765 | 9077 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
9078 | attr->SetTextColour(colour); |
9079 | attr->DecRef(); | |
9080 | } | |
9081 | } | |
9082 | ||
9083 | void wxGrid::SetCellFont( int row, int col, const wxFont& font ) | |
9084 | { | |
9085 | if ( CanHaveAttributes() ) | |
9086 | { | |
0a976765 | 9087 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
9088 | attr->SetFont(font); |
9089 | attr->DecRef(); | |
9090 | } | |
9091 | } | |
9092 | ||
9093 | void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert ) | |
9094 | { | |
9095 | if ( CanHaveAttributes() ) | |
9096 | { | |
0a976765 | 9097 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
9098 | attr->SetAlignment(horiz, vert); |
9099 | attr->DecRef(); | |
ab79958a VZ |
9100 | } |
9101 | } | |
9102 | ||
27f35b66 SN |
9103 | void wxGrid::SetCellOverflow( int row, int col, bool allow ) |
9104 | { | |
9105 | if ( CanHaveAttributes() ) | |
9106 | { | |
9107 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9108 | attr->SetOverflow(allow); | |
9109 | attr->DecRef(); | |
9110 | } | |
9111 | } | |
9112 | ||
9113 | void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols ) | |
9114 | { | |
9115 | if ( CanHaveAttributes() ) | |
9116 | { | |
9117 | int cell_rows, cell_cols; | |
9118 | ||
9119 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9120 | attr->GetSize(&cell_rows, &cell_cols); | |
9121 | attr->SetSize(num_rows, num_cols); | |
9122 | attr->DecRef(); | |
9123 | ||
9124 | // Cannot set the size of a cell to 0 or negative values | |
9125 | // While it is perfectly legal to do that, this function cannot | |
9126 | // handle all the possibilies, do it by hand by getting the CellAttr. | |
9127 | // You can only set the size of a cell to 1,1 or greater with this fn | |
9128 | wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)), | |
9129 | wxT("wxGrid::SetCellSize setting cell size that is already part of another cell")); | |
9130 | wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)), | |
9131 | wxT("wxGrid::SetCellSize setting cell size to < 1")); | |
9132 | ||
9133 | // if this was already a multicell then "turn off" the other cells first | |
9134 | if ((cell_rows > 1) || (cell_rows > 1)) | |
9135 | { | |
9136 | int i, j; | |
9137 | for (j=row; j<row+cell_rows; j++) | |
9138 | { | |
9139 | for (i=col; i<col+cell_cols; i++) | |
9140 | { | |
9141 | if ((i != col) || (j != row)) | |
9142 | { | |
9143 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
9144 | attr_stub->SetSize( 1, 1 ); | |
9145 | attr_stub->DecRef(); | |
9146 | } | |
9147 | } | |
9148 | } | |
9149 | } | |
9150 | ||
9151 | // mark the cells that will be covered by this cell to | |
9152 | // negative or zero values to point back at this cell | |
9153 | if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1)) | |
9154 | { | |
9155 | int i, j; | |
9156 | for (j=row; j<row+num_rows; j++) | |
9157 | { | |
9158 | for (i=col; i<col+num_cols; i++) | |
9159 | { | |
9160 | if ((i != col) || (j != row)) | |
9161 | { | |
9162 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
9163 | attr_stub->SetSize( row-j, col-i ); | |
9164 | attr_stub->DecRef(); | |
9165 | } | |
9166 | } | |
9167 | } | |
9168 | } | |
9169 | } | |
9170 | } | |
9171 | ||
ab79958a VZ |
9172 | void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer) |
9173 | { | |
9174 | if ( CanHaveAttributes() ) | |
9175 | { | |
0a976765 | 9176 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
ab79958a VZ |
9177 | attr->SetRenderer(renderer); |
9178 | attr->DecRef(); | |
9b4aede2 RD |
9179 | } |
9180 | } | |
9181 | ||
9182 | void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor) | |
9183 | { | |
9184 | if ( CanHaveAttributes() ) | |
9185 | { | |
9186 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9187 | attr->SetEditor(editor); | |
9188 | attr->DecRef(); | |
283b7808 VZ |
9189 | } |
9190 | } | |
9191 | ||
9192 | void wxGrid::SetReadOnly(int row, int col, bool isReadOnly) | |
9193 | { | |
9194 | if ( CanHaveAttributes() ) | |
9195 | { | |
9196 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9197 | attr->SetReadOnly(isReadOnly); | |
9198 | attr->DecRef(); | |
2e9a6788 | 9199 | } |
f85afd4e MB |
9200 | } |
9201 | ||
f2d76237 RD |
9202 | // ---------------------------------------------------------------------------- |
9203 | // Data type registration | |
9204 | // ---------------------------------------------------------------------------- | |
9205 | ||
9206 | void wxGrid::RegisterDataType(const wxString& typeName, | |
9207 | wxGridCellRenderer* renderer, | |
9208 | wxGridCellEditor* editor) | |
9209 | { | |
9210 | m_typeRegistry->RegisterDataType(typeName, renderer, editor); | |
9211 | } | |
9212 | ||
9213 | ||
99306db2 | 9214 | wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const |
f2d76237 RD |
9215 | { |
9216 | wxString typeName = m_table->GetTypeName(row, col); | |
9217 | return GetDefaultEditorForType(typeName); | |
9218 | } | |
9219 | ||
99306db2 | 9220 | wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const |
f2d76237 RD |
9221 | { |
9222 | wxString typeName = m_table->GetTypeName(row, col); | |
9223 | return GetDefaultRendererForType(typeName); | |
9224 | } | |
9225 | ||
99306db2 VZ |
9226 | wxGridCellEditor* |
9227 | wxGrid::GetDefaultEditorForType(const wxString& typeName) const | |
f2d76237 | 9228 | { |
c4608a8a | 9229 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
9230 | if ( index == wxNOT_FOUND ) |
9231 | { | |
9232 | wxFAIL_MSG(wxT("Unknown data type name")); | |
9233 | ||
f2d76237 RD |
9234 | return NULL; |
9235 | } | |
0b190b0f | 9236 | |
f2d76237 RD |
9237 | return m_typeRegistry->GetEditor(index); |
9238 | } | |
9239 | ||
99306db2 VZ |
9240 | wxGridCellRenderer* |
9241 | wxGrid::GetDefaultRendererForType(const wxString& typeName) const | |
f2d76237 | 9242 | { |
c4608a8a | 9243 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
9244 | if ( index == wxNOT_FOUND ) |
9245 | { | |
c4608a8a | 9246 | wxFAIL_MSG(wxT("Unknown data type name")); |
0b190b0f | 9247 | |
c4608a8a | 9248 | return NULL; |
e72b4213 | 9249 | } |
0b190b0f | 9250 | |
c4608a8a | 9251 | return m_typeRegistry->GetRenderer(index); |
f2d76237 RD |
9252 | } |
9253 | ||
9254 | ||
2e9a6788 VZ |
9255 | // ---------------------------------------------------------------------------- |
9256 | // row/col size | |
9257 | // ---------------------------------------------------------------------------- | |
9258 | ||
6e8524b1 MB |
9259 | void wxGrid::EnableDragRowSize( bool enable ) |
9260 | { | |
9261 | m_canDragRowSize = enable; | |
9262 | } | |
9263 | ||
9264 | ||
9265 | void wxGrid::EnableDragColSize( bool enable ) | |
9266 | { | |
9267 | m_canDragColSize = enable; | |
9268 | } | |
9269 | ||
4cfa5de6 RD |
9270 | void wxGrid::EnableDragGridSize( bool enable ) |
9271 | { | |
9272 | m_canDragGridSize = enable; | |
9273 | } | |
9274 | ||
6e8524b1 | 9275 | |
f85afd4e MB |
9276 | void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) |
9277 | { | |
b8d24d4e | 9278 | m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight ); |
f85afd4e MB |
9279 | |
9280 | if ( resizeExistingRows ) | |
9281 | { | |
b1da8107 SN |
9282 | // since we are resizing all rows to the default row size, |
9283 | // we can simply clear the row heights and row bottoms | |
9284 | // arrays (which also allows us to take advantage of | |
9285 | // some speed optimisations) | |
9286 | m_rowHeights.Empty(); | |
9287 | m_rowBottoms.Empty(); | |
edb89f7e VZ |
9288 | if ( !GetBatchCount() ) |
9289 | CalcDimensions(); | |
f85afd4e MB |
9290 | } |
9291 | } | |
9292 | ||
9293 | void wxGrid::SetRowSize( int row, int height ) | |
9294 | { | |
b99be8fb | 9295 | wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") ); |
60ff3b99 | 9296 | |
b4bfd0fa RG |
9297 | // See comment in SetColSize |
9298 | if ( height < GetRowMinimalAcceptableHeight()) { return; } | |
b8d24d4e | 9299 | |
7c1cb261 VZ |
9300 | if ( m_rowHeights.IsEmpty() ) |
9301 | { | |
9302 | // need to really create the array | |
9303 | InitRowHeights(); | |
9304 | } | |
60ff3b99 | 9305 | |
b99be8fb VZ |
9306 | int h = wxMax( 0, height ); |
9307 | int diff = h - m_rowHeights[row]; | |
60ff3b99 | 9308 | |
b99be8fb | 9309 | m_rowHeights[row] = h; |
7c1cb261 | 9310 | int i; |
b99be8fb | 9311 | for ( i = row; i < m_numRows; i++ ) |
f85afd4e | 9312 | { |
b99be8fb | 9313 | m_rowBottoms[i] += diff; |
f85afd4e | 9314 | } |
faec5a43 SN |
9315 | if ( !GetBatchCount() ) |
9316 | CalcDimensions(); | |
f85afd4e MB |
9317 | } |
9318 | ||
9319 | void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) | |
9320 | { | |
b8d24d4e | 9321 | m_defaultColWidth = wxMax( width, m_minAcceptableColWidth ); |
f85afd4e MB |
9322 | |
9323 | if ( resizeExistingCols ) | |
9324 | { | |
b1da8107 SN |
9325 | // since we are resizing all columns to the default column size, |
9326 | // we can simply clear the col widths and col rights | |
9327 | // arrays (which also allows us to take advantage of | |
9328 | // some speed optimisations) | |
9329 | m_colWidths.Empty(); | |
9330 | m_colRights.Empty(); | |
edb89f7e VZ |
9331 | if ( !GetBatchCount() ) |
9332 | CalcDimensions(); | |
f85afd4e MB |
9333 | } |
9334 | } | |
9335 | ||
9336 | void wxGrid::SetColSize( int col, int width ) | |
9337 | { | |
b99be8fb | 9338 | wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") ); |
60ff3b99 | 9339 | |
43947979 | 9340 | // should we check that it's bigger than GetColMinimalWidth(col) here? |
b4bfd0fa RG |
9341 | // (VZ) |
9342 | // No, because it is reasonable to assume the library user know's | |
3e13956a | 9343 | // what he is doing. However whe should test against the weaker |
b4bfd0fa | 9344 | // constariant of minimalAcceptableWidth, as this breaks rendering |
3e13956a | 9345 | // |
b4bfd0fa | 9346 | // This test then fixes sf.net bug #645734 |
3e13956a | 9347 | |
b4bfd0fa | 9348 | if ( width < GetColMinimalAcceptableWidth()) { return; } |
3e13956a | 9349 | |
7c1cb261 VZ |
9350 | if ( m_colWidths.IsEmpty() ) |
9351 | { | |
9352 | // need to really create the array | |
9353 | InitColWidths(); | |
9354 | } | |
f85afd4e | 9355 | |
04418332 | 9356 | // if < 0 calc new width from label |
73145b0e JS |
9357 | if( width < 0 ) |
9358 | { | |
9359 | long w, h; | |
9360 | wxArrayString lines; | |
9361 | wxClientDC dc(m_colLabelWin); | |
9362 | dc.SetFont(GetLabelFont()); | |
9363 | StringToLines(GetColLabelValue(col), lines); | |
9364 | GetTextBoxSize(dc, lines, &w, &h); | |
9365 | width = w + 6; | |
9366 | } | |
b99be8fb VZ |
9367 | int w = wxMax( 0, width ); |
9368 | int diff = w - m_colWidths[col]; | |
9369 | m_colWidths[col] = w; | |
60ff3b99 | 9370 | |
7c1cb261 | 9371 | int i; |
b99be8fb | 9372 | for ( i = col; i < m_numCols; i++ ) |
f85afd4e | 9373 | { |
b99be8fb | 9374 | m_colRights[i] += diff; |
f85afd4e | 9375 | } |
faec5a43 SN |
9376 | if ( !GetBatchCount() ) |
9377 | CalcDimensions(); | |
f85afd4e MB |
9378 | } |
9379 | ||
2d66e025 | 9380 | |
43947979 VZ |
9381 | void wxGrid::SetColMinimalWidth( int col, int width ) |
9382 | { | |
b8d24d4e | 9383 | if (width > GetColMinimalAcceptableWidth()) { |
ba8c1601 | 9384 | m_colMinWidths[col] = width; |
b8d24d4e | 9385 | } |
af547d51 VZ |
9386 | } |
9387 | ||
9388 | void wxGrid::SetRowMinimalHeight( int row, int width ) | |
9389 | { | |
b8d24d4e | 9390 | if (width > GetRowMinimalAcceptableHeight()) { |
ba8c1601 | 9391 | m_rowMinHeights[row] = width; |
b8d24d4e | 9392 | } |
43947979 VZ |
9393 | } |
9394 | ||
9395 | int wxGrid::GetColMinimalWidth(int col) const | |
9396 | { | |
ba8c1601 MB |
9397 | wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(col); |
9398 | return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth; | |
af547d51 VZ |
9399 | } |
9400 | ||
9401 | int wxGrid::GetRowMinimalHeight(int row) const | |
9402 | { | |
ba8c1601 MB |
9403 | wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(row); |
9404 | return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight; | |
b8d24d4e RG |
9405 | } |
9406 | ||
9407 | void wxGrid::SetColMinimalAcceptableWidth( int width ) | |
9408 | { | |
9409 | if ( width<1 ) | |
9410 | return; | |
9411 | m_minAcceptableColWidth = width; | |
9412 | } | |
9413 | ||
9414 | void wxGrid::SetRowMinimalAcceptableHeight( int height ) | |
9415 | { | |
9416 | if ( height<1 ) | |
9417 | return; | |
9418 | m_minAcceptableRowHeight = height; | |
9419 | }; | |
9420 | ||
9421 | int wxGrid::GetColMinimalAcceptableWidth() const | |
9422 | { | |
9423 | return m_minAcceptableColWidth; | |
9424 | } | |
9425 | ||
9426 | int wxGrid::GetRowMinimalAcceptableHeight() const | |
9427 | { | |
9428 | return m_minAcceptableRowHeight; | |
43947979 VZ |
9429 | } |
9430 | ||
57c086ef VZ |
9431 | // ---------------------------------------------------------------------------- |
9432 | // auto sizing | |
9433 | // ---------------------------------------------------------------------------- | |
9434 | ||
af547d51 | 9435 | void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) |
65e4e78e VZ |
9436 | { |
9437 | wxClientDC dc(m_gridWin); | |
9438 | ||
a95e38c0 VZ |
9439 | // init both of them to avoid compiler warnings, even if weo nly need one |
9440 | int row = -1, | |
9441 | col = -1; | |
af547d51 VZ |
9442 | if ( column ) |
9443 | col = colOrRow; | |
9444 | else | |
9445 | row = colOrRow; | |
9446 | ||
9447 | wxCoord extent, extentMax = 0; | |
9448 | int max = column ? m_numRows : m_numCols; | |
39bcce60 | 9449 | for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ ) |
65e4e78e | 9450 | { |
af547d51 VZ |
9451 | if ( column ) |
9452 | row = rowOrCol; | |
9453 | else | |
9454 | col = rowOrCol; | |
9455 | ||
65e4e78e | 9456 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 9457 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); |
65e4e78e VZ |
9458 | if ( renderer ) |
9459 | { | |
af547d51 VZ |
9460 | wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col); |
9461 | extent = column ? size.x : size.y; | |
9462 | if ( extent > extentMax ) | |
65e4e78e | 9463 | { |
af547d51 | 9464 | extentMax = extent; |
65e4e78e | 9465 | } |
0b190b0f VZ |
9466 | |
9467 | renderer->DecRef(); | |
65e4e78e VZ |
9468 | } |
9469 | ||
9470 | attr->DecRef(); | |
9471 | } | |
9472 | ||
af547d51 VZ |
9473 | // now also compare with the column label extent |
9474 | wxCoord w, h; | |
65e4e78e | 9475 | dc.SetFont( GetLabelFont() ); |
294d195c MB |
9476 | |
9477 | if ( column ) | |
d43851f7 | 9478 | { |
294d195c | 9479 | dc.GetTextExtent( GetColLabelValue(col), &w, &h ); |
d43851f7 JS |
9480 | if( GetColLabelTextOrientation() == wxVERTICAL ) |
9481 | w = h; | |
9482 | } | |
294d195c | 9483 | else |
ee495e4c | 9484 | dc.GetTextExtent( GetRowLabelValue(row), &w, &h ); |
294d195c | 9485 | |
af547d51 VZ |
9486 | extent = column ? w : h; |
9487 | if ( extent > extentMax ) | |
65e4e78e | 9488 | { |
af547d51 | 9489 | extentMax = extent; |
65e4e78e VZ |
9490 | } |
9491 | ||
af547d51 | 9492 | if ( !extentMax ) |
65e4e78e | 9493 | { |
af547d51 VZ |
9494 | // empty column - give default extent (notice that if extentMax is less |
9495 | // than default extent but != 0, it's ok) | |
9496 | extentMax = column ? m_defaultColWidth : m_defaultRowHeight; | |
65e4e78e VZ |
9497 | } |
9498 | else | |
9499 | { | |
a95e38c0 VZ |
9500 | if ( column ) |
9501 | { | |
9502 | // leave some space around text | |
9503 | extentMax += 10; | |
9504 | } | |
f6bcfd97 BP |
9505 | else |
9506 | { | |
9507 | extentMax += 6; | |
9508 | } | |
65e4e78e VZ |
9509 | } |
9510 | ||
edb89f7e VZ |
9511 | if ( column ) |
9512 | { | |
af547d51 | 9513 | SetColSize(col, extentMax); |
faec5a43 SN |
9514 | if ( !GetBatchCount() ) |
9515 | { | |
edb89f7e VZ |
9516 | int cw, ch, dummy; |
9517 | m_gridWin->GetClientSize( &cw, &ch ); | |
9518 | wxRect rect ( CellToRect( 0, col ) ); | |
9519 | rect.y = 0; | |
9520 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
9521 | rect.width = cw - rect.x; | |
9522 | rect.height = m_colLabelHeight; | |
9523 | m_colLabelWin->Refresh( TRUE, &rect ); | |
9524 | } | |
9525 | } | |
9526 | else | |
9527 | { | |
39bcce60 | 9528 | SetRowSize(row, extentMax); |
faec5a43 SN |
9529 | if ( !GetBatchCount() ) |
9530 | { | |
edb89f7e VZ |
9531 | int cw, ch, dummy; |
9532 | m_gridWin->GetClientSize( &cw, &ch ); | |
9533 | wxRect rect ( CellToRect( row, 0 ) ); | |
9534 | rect.x = 0; | |
9535 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
9536 | rect.width = m_rowLabelWidth; | |
faec5a43 | 9537 | rect.height = ch - rect.y; |
edb89f7e VZ |
9538 | m_rowLabelWin->Refresh( TRUE, &rect ); |
9539 | } | |
faec5a43 | 9540 | } |
65e4e78e VZ |
9541 | if ( setAsMin ) |
9542 | { | |
af547d51 VZ |
9543 | if ( column ) |
9544 | SetColMinimalWidth(col, extentMax); | |
9545 | else | |
39bcce60 | 9546 | SetRowMinimalHeight(row, extentMax); |
65e4e78e VZ |
9547 | } |
9548 | } | |
9549 | ||
266e8367 | 9550 | int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) |
65e4e78e | 9551 | { |
57c086ef VZ |
9552 | int width = m_rowLabelWidth; |
9553 | ||
faec5a43 SN |
9554 | if ( !calcOnly ) |
9555 | BeginBatch(); | |
97a9929e | 9556 | |
65e4e78e VZ |
9557 | for ( int col = 0; col < m_numCols; col++ ) |
9558 | { | |
266e8367 VZ |
9559 | if ( !calcOnly ) |
9560 | { | |
9561 | AutoSizeColumn(col, setAsMin); | |
9562 | } | |
57c086ef VZ |
9563 | |
9564 | width += GetColWidth(col); | |
65e4e78e | 9565 | } |
97a9929e | 9566 | |
faec5a43 SN |
9567 | if ( !calcOnly ) |
9568 | EndBatch(); | |
97a9929e | 9569 | |
266e8367 | 9570 | return width; |
65e4e78e VZ |
9571 | } |
9572 | ||
266e8367 | 9573 | int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin) |
57c086ef VZ |
9574 | { |
9575 | int height = m_colLabelHeight; | |
9576 | ||
faec5a43 SN |
9577 | if ( !calcOnly ) |
9578 | BeginBatch(); | |
97a9929e | 9579 | |
57c086ef VZ |
9580 | for ( int row = 0; row < m_numRows; row++ ) |
9581 | { | |
af547d51 VZ |
9582 | if ( !calcOnly ) |
9583 | { | |
9584 | AutoSizeRow(row, setAsMin); | |
9585 | } | |
57c086ef VZ |
9586 | |
9587 | height += GetRowHeight(row); | |
9588 | } | |
97a9929e | 9589 | |
faec5a43 SN |
9590 | if ( !calcOnly ) |
9591 | EndBatch(); | |
97a9929e | 9592 | |
266e8367 | 9593 | return height; |
57c086ef VZ |
9594 | } |
9595 | ||
9596 | void wxGrid::AutoSize() | |
9597 | { | |
97a9929e VZ |
9598 | BeginBatch(); |
9599 | ||
9600 | wxSize size(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE)); | |
9601 | ||
9602 | // round up the size to a multiple of scroll step - this ensures that we | |
9603 | // won't get the scrollbars if we're sized exactly to this width | |
2b5f62a0 VZ |
9604 | // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary |
9605 | // scrollbar steps | |
9606 | wxSize sizeFit(GetScrollX(size.x + m_extraWidth + 1) * GRID_SCROLL_LINE_X, | |
9607 | GetScrollY(size.y + m_extraHeight + 1) * GRID_SCROLL_LINE_Y); | |
97a9929e | 9608 | |
2b5f62a0 | 9609 | // distribute the extra space between the columns/rows to avoid having |
97a9929e | 9610 | // extra white space |
2b5f62a0 VZ |
9611 | |
9612 | // Remove the extra m_extraWidth + 1 added above | |
9613 | wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1); | |
9614 | if ( diff && m_numCols ) | |
97a9929e VZ |
9615 | { |
9616 | // try to resize the columns uniformly | |
9617 | wxCoord diffPerCol = diff / m_numCols; | |
9618 | if ( diffPerCol ) | |
9619 | { | |
9620 | for ( int col = 0; col < m_numCols; col++ ) | |
9621 | { | |
9622 | SetColSize(col, GetColWidth(col) + diffPerCol); | |
9623 | } | |
9624 | } | |
9625 | ||
9626 | // add remaining amount to the last columns | |
9627 | diff -= diffPerCol * m_numCols; | |
9628 | if ( diff ) | |
9629 | { | |
9630 | for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- ) | |
9631 | { | |
9632 | SetColSize(col, GetColWidth(col) + 1); | |
9633 | } | |
9634 | } | |
9635 | } | |
9636 | ||
9637 | // same for rows | |
2b5f62a0 VZ |
9638 | diff = sizeFit.y - size.y - (m_extraHeight + 1); |
9639 | if ( diff && m_numRows ) | |
97a9929e VZ |
9640 | { |
9641 | // try to resize the columns uniformly | |
9642 | wxCoord diffPerRow = diff / m_numRows; | |
9643 | if ( diffPerRow ) | |
9644 | { | |
9645 | for ( int row = 0; row < m_numRows; row++ ) | |
9646 | { | |
9647 | SetRowSize(row, GetRowHeight(row) + diffPerRow); | |
9648 | } | |
9649 | } | |
9650 | ||
9651 | // add remaining amount to the last rows | |
9652 | diff -= diffPerRow * m_numRows; | |
9653 | if ( diff ) | |
9654 | { | |
9655 | for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- ) | |
9656 | { | |
9657 | SetRowSize(row, GetRowHeight(row) + 1); | |
9658 | } | |
9659 | } | |
9660 | } | |
9661 | ||
9662 | EndBatch(); | |
9663 | ||
9664 | SetClientSize(sizeFit); | |
266e8367 VZ |
9665 | } |
9666 | ||
d43851f7 JS |
9667 | void wxGrid::AutoSizeRowLabelSize( int row ) |
9668 | { | |
9669 | wxArrayString lines; | |
9670 | long w, h; | |
9671 | ||
9672 | // Hide the edit control, so it | |
9673 | // won't interfer with drag-shrinking. | |
9674 | if( IsCellEditControlShown() ) | |
9675 | { | |
9676 | HideCellEditControl(); | |
9677 | SaveEditControlValue(); | |
9678 | } | |
9679 | ||
9680 | // autosize row height depending on label text | |
9681 | StringToLines( GetRowLabelValue( row ), lines ); | |
9682 | wxClientDC dc( m_rowLabelWin ); | |
9683 | GetTextBoxSize( dc, lines, &w, &h); | |
9684 | if( h < m_defaultRowHeight ) | |
9685 | h = m_defaultRowHeight; | |
9686 | SetRowSize(row, h); | |
9687 | ForceRefresh(); | |
9688 | } | |
9689 | ||
9690 | void wxGrid::AutoSizeColLabelSize( int col ) | |
9691 | { | |
9692 | wxArrayString lines; | |
9693 | long w, h; | |
9694 | ||
9695 | // Hide the edit control, so it | |
9696 | // won't interfer with drag-shrinking. | |
9697 | if( IsCellEditControlShown() ) | |
9698 | { | |
9699 | HideCellEditControl(); | |
9700 | SaveEditControlValue(); | |
9701 | } | |
9702 | ||
9703 | // autosize column width depending on label text | |
9704 | StringToLines( GetColLabelValue( col ), lines ); | |
9705 | wxClientDC dc( m_colLabelWin ); | |
9706 | if( GetColLabelTextOrientation() == wxHORIZONTAL ) | |
9707 | GetTextBoxSize( dc, lines, &w, &h); | |
9708 | else | |
9709 | GetTextBoxSize( dc, lines, &h, &w); | |
9710 | if( w < m_defaultColWidth ) | |
9711 | w = m_defaultColWidth; | |
9712 | SetColSize(col, w); | |
9713 | ForceRefresh(); | |
9714 | } | |
9715 | ||
266e8367 VZ |
9716 | wxSize wxGrid::DoGetBestSize() const |
9717 | { | |
9718 | // don't set sizes, only calculate them | |
9719 | wxGrid *self = (wxGrid *)this; // const_cast | |
9720 | ||
84035150 SN |
9721 | int width, height; |
9722 | width = self->SetOrCalcColumnSizes(TRUE); | |
9723 | height = self->SetOrCalcRowSizes(TRUE); | |
9724 | ||
9725 | int maxwidth, maxheight; | |
9726 | wxDisplaySize( & maxwidth, & maxheight ); | |
9727 | ||
9728 | if ( width > maxwidth ) width = maxwidth; | |
9729 | if ( height > maxheight ) height = maxheight; | |
9730 | ||
9731 | return wxSize( width, height ); | |
266e8367 VZ |
9732 | } |
9733 | ||
9734 | void wxGrid::Fit() | |
9735 | { | |
9736 | AutoSize(); | |
57c086ef VZ |
9737 | } |
9738 | ||
6fc0f38f SN |
9739 | |
9740 | wxPen& wxGrid::GetDividerPen() const | |
9741 | { | |
9742 | return wxNullPen; | |
9743 | } | |
9744 | ||
57c086ef VZ |
9745 | // ---------------------------------------------------------------------------- |
9746 | // cell value accessor functions | |
9747 | // ---------------------------------------------------------------------------- | |
f85afd4e MB |
9748 | |
9749 | void wxGrid::SetCellValue( int row, int col, const wxString& s ) | |
9750 | { | |
9751 | if ( m_table ) | |
9752 | { | |
f6bcfd97 | 9753 | m_table->SetValue( row, col, s ); |
2d66e025 MB |
9754 | if ( !GetBatchCount() ) |
9755 | { | |
27f35b66 SN |
9756 | int dummy; |
9757 | wxRect rect( CellToRect( row, col ) ); | |
9758 | rect.x = 0; | |
9759 | rect.width = m_gridWin->GetClientSize().GetWidth(); | |
9760 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
9761 | m_gridWin->Refresh( FALSE, &rect ); | |
2d66e025 | 9762 | } |
60ff3b99 | 9763 | |
f85afd4e | 9764 | if ( m_currentCellCoords.GetRow() == row && |
4cfa5de6 | 9765 | m_currentCellCoords.GetCol() == col && |
f6bcfd97 BP |
9766 | IsCellEditControlShown()) |
9767 | // Note: If we are using IsCellEditControlEnabled, | |
9768 | // this interacts badly with calling SetCellValue from | |
9769 | // an EVT_GRID_CELL_CHANGE handler. | |
f85afd4e | 9770 | { |
4cfa5de6 RD |
9771 | HideCellEditControl(); |
9772 | ShowCellEditControl(); // will reread data from table | |
f85afd4e | 9773 | } |
f85afd4e MB |
9774 | } |
9775 | } | |
9776 | ||
9777 | ||
9778 | // | |
2d66e025 | 9779 | // ------ Block, row and col selection |
f85afd4e MB |
9780 | // |
9781 | ||
9782 | void wxGrid::SelectRow( int row, bool addToSelected ) | |
9783 | { | |
b5808881 | 9784 | if ( IsSelection() && !addToSelected ) |
e32352cf | 9785 | ClearSelection(); |
70c7a608 | 9786 | |
3f3dc2ef VZ |
9787 | if ( m_selection ) |
9788 | m_selection->SelectRow( row, FALSE, addToSelected ); | |
f85afd4e MB |
9789 | } |
9790 | ||
9791 | ||
9792 | void wxGrid::SelectCol( int col, bool addToSelected ) | |
9793 | { | |
b5808881 | 9794 | if ( IsSelection() && !addToSelected ) |
e32352cf | 9795 | ClearSelection(); |
f85afd4e | 9796 | |
3f3dc2ef VZ |
9797 | if ( m_selection ) |
9798 | m_selection->SelectCol( col, FALSE, addToSelected ); | |
f85afd4e MB |
9799 | } |
9800 | ||
9801 | ||
84912ef8 | 9802 | void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol, |
c9097836 | 9803 | bool addToSelected ) |
f85afd4e | 9804 | { |
c9097836 MB |
9805 | if ( IsSelection() && !addToSelected ) |
9806 | ClearSelection(); | |
f85afd4e | 9807 | |
3f3dc2ef VZ |
9808 | if ( m_selection ) |
9809 | m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol, | |
9810 | FALSE, addToSelected ); | |
f85afd4e MB |
9811 | } |
9812 | ||
c9097836 | 9813 | |
f85afd4e MB |
9814 | void wxGrid::SelectAll() |
9815 | { | |
f74d0b57 | 9816 | if ( m_numRows > 0 && m_numCols > 0 ) |
3f3dc2ef VZ |
9817 | { |
9818 | if ( m_selection ) | |
9819 | m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 ); | |
9820 | } | |
b5808881 | 9821 | } |
f85afd4e | 9822 | |
f7b4b343 VZ |
9823 | // |
9824 | // ------ Cell, row and col deselection | |
9825 | // | |
9826 | ||
9827 | void wxGrid::DeselectRow( int row ) | |
9828 | { | |
3f3dc2ef VZ |
9829 | if ( !m_selection ) |
9830 | return; | |
9831 | ||
f7b4b343 VZ |
9832 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows ) |
9833 | { | |
9834 | if ( m_selection->IsInSelection(row, 0 ) ) | |
9835 | m_selection->ToggleCellSelection( row, 0); | |
ffdd3c98 | 9836 | } |
f7b4b343 VZ |
9837 | else |
9838 | { | |
9839 | int nCols = GetNumberCols(); | |
9840 | for ( int i = 0; i < nCols ; i++ ) | |
9841 | { | |
9842 | if ( m_selection->IsInSelection(row, i ) ) | |
9843 | m_selection->ToggleCellSelection( row, i); | |
9844 | } | |
9845 | } | |
9846 | } | |
9847 | ||
9848 | void wxGrid::DeselectCol( int col ) | |
9849 | { | |
3f3dc2ef VZ |
9850 | if ( !m_selection ) |
9851 | return; | |
9852 | ||
f7b4b343 VZ |
9853 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns ) |
9854 | { | |
9855 | if ( m_selection->IsInSelection(0, col ) ) | |
9856 | m_selection->ToggleCellSelection( 0, col); | |
9857 | } | |
9858 | else | |
9859 | { | |
9860 | int nRows = GetNumberRows(); | |
9861 | for ( int i = 0; i < nRows ; i++ ) | |
9862 | { | |
9863 | if ( m_selection->IsInSelection(i, col ) ) | |
9864 | m_selection->ToggleCellSelection(i, col); | |
9865 | } | |
9866 | } | |
9867 | } | |
9868 | ||
9869 | void wxGrid::DeselectCell( int row, int col ) | |
9870 | { | |
3f3dc2ef | 9871 | if ( m_selection && m_selection->IsInSelection(row, col) ) |
f7b4b343 VZ |
9872 | m_selection->ToggleCellSelection(row, col); |
9873 | } | |
9874 | ||
b5808881 SN |
9875 | bool wxGrid::IsSelection() |
9876 | { | |
3f3dc2ef | 9877 | return ( m_selection && (m_selection->IsSelection() || |
b5808881 | 9878 | ( m_selectingTopLeft != wxGridNoCellCoords && |
3f3dc2ef | 9879 | m_selectingBottomRight != wxGridNoCellCoords) ) ); |
f85afd4e MB |
9880 | } |
9881 | ||
84035150 | 9882 | bool wxGrid::IsInSelection( int row, int col ) const |
b5808881 | 9883 | { |
3f3dc2ef | 9884 | return ( m_selection && (m_selection->IsInSelection( row, col ) || |
b5808881 SN |
9885 | ( row >= m_selectingTopLeft.GetRow() && |
9886 | col >= m_selectingTopLeft.GetCol() && | |
9887 | row <= m_selectingBottomRight.GetRow() && | |
3f3dc2ef | 9888 | col <= m_selectingBottomRight.GetCol() )) ); |
b5808881 | 9889 | } |
f85afd4e | 9890 | |
aa5b8857 SN |
9891 | wxGridCellCoordsArray wxGrid::GetSelectedCells() const |
9892 | { | |
9893 | if (!m_selection) { wxGridCellCoordsArray a; return a; } | |
9894 | return m_selection->m_cellSelection; | |
9895 | } | |
9896 | wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const | |
9897 | { | |
9898 | if (!m_selection) { wxGridCellCoordsArray a; return a; } | |
9899 | return m_selection->m_blockSelectionTopLeft; | |
9900 | } | |
9901 | wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const | |
9902 | { | |
9903 | if (!m_selection) { wxGridCellCoordsArray a; return a; } | |
a8de8190 | 9904 | return m_selection->m_blockSelectionBottomRight; |
aa5b8857 SN |
9905 | } |
9906 | wxArrayInt wxGrid::GetSelectedRows() const | |
9907 | { | |
9908 | if (!m_selection) { wxArrayInt a; return a; } | |
9909 | return m_selection->m_rowSelection; | |
9910 | } | |
9911 | wxArrayInt wxGrid::GetSelectedCols() const | |
9912 | { | |
9913 | if (!m_selection) { wxArrayInt a; return a; } | |
9914 | return m_selection->m_colSelection; | |
9915 | } | |
9916 | ||
9917 | ||
f85afd4e MB |
9918 | void wxGrid::ClearSelection() |
9919 | { | |
b5808881 SN |
9920 | m_selectingTopLeft = wxGridNoCellCoords; |
9921 | m_selectingBottomRight = wxGridNoCellCoords; | |
3f3dc2ef VZ |
9922 | if ( m_selection ) |
9923 | m_selection->ClearSelection(); | |
8f177c8e | 9924 | } |
f85afd4e MB |
9925 | |
9926 | ||
da6af900 | 9927 | // This function returns the rectangle that encloses the given block |
2d66e025 MB |
9928 | // in device coords clipped to the client size of the grid window. |
9929 | // | |
58dd5b3b MB |
9930 | wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft, |
9931 | const wxGridCellCoords &bottomRight ) | |
f85afd4e | 9932 | { |
58dd5b3b | 9933 | wxRect rect( wxGridNoCellRect ); |
f85afd4e MB |
9934 | wxRect cellRect; |
9935 | ||
58dd5b3b MB |
9936 | cellRect = CellToRect( topLeft ); |
9937 | if ( cellRect != wxGridNoCellRect ) | |
f85afd4e | 9938 | { |
58dd5b3b MB |
9939 | rect = cellRect; |
9940 | } | |
9941 | else | |
9942 | { | |
9943 | rect = wxRect( 0, 0, 0, 0 ); | |
9944 | } | |
60ff3b99 | 9945 | |
58dd5b3b MB |
9946 | cellRect = CellToRect( bottomRight ); |
9947 | if ( cellRect != wxGridNoCellRect ) | |
9948 | { | |
9949 | rect += cellRect; | |
2d66e025 MB |
9950 | } |
9951 | else | |
9952 | { | |
9953 | return wxGridNoCellRect; | |
f85afd4e MB |
9954 | } |
9955 | ||
27f35b66 SN |
9956 | int i, j; |
9957 | int left = rect.GetLeft(); | |
9958 | int top = rect.GetTop(); | |
9959 | int right = rect.GetRight(); | |
9960 | int bottom = rect.GetBottom(); | |
9961 | ||
9962 | int leftCol = topLeft.GetCol(); | |
9963 | int topRow = topLeft.GetRow(); | |
9964 | int rightCol = bottomRight.GetCol(); | |
9965 | int bottomRow = bottomRight.GetRow(); | |
9966 | ||
3ed884a0 SN |
9967 | if (left > right) |
9968 | { | |
9969 | i = left; | |
9970 | left = right; | |
9971 | right = i; | |
9972 | i = leftCol; | |
9973 | leftCol=rightCol; | |
9974 | rightCol = i; | |
9975 | } | |
9976 | ||
9977 | if (top > bottom) | |
9978 | { | |
9979 | i = top; | |
9980 | top = bottom; | |
9981 | bottom = i; | |
9982 | i = topRow; | |
9983 | topRow = bottomRow; | |
9984 | bottomRow = i; | |
9985 | } | |
9986 | ||
9987 | ||
27f35b66 SN |
9988 | for ( j = topRow; j <= bottomRow; j++ ) |
9989 | { | |
9990 | for ( i = leftCol; i <= rightCol; i++ ) | |
9991 | { | |
9992 | if ((j==topRow) || (j==bottomRow) || (i==leftCol) || (i==rightCol)) | |
9993 | { | |
9994 | cellRect = CellToRect( j, i ); | |
9995 | ||
9996 | if (cellRect.x < left) | |
9997 | left = cellRect.x; | |
9998 | if (cellRect.y < top) | |
9999 | top = cellRect.y; | |
10000 | if (cellRect.x + cellRect.width > right) | |
10001 | right = cellRect.x + cellRect.width; | |
10002 | if (cellRect.y + cellRect.height > bottom) | |
10003 | bottom = cellRect.y + cellRect.height; | |
10004 | } | |
3ed884a0 | 10005 | else i = rightCol; // jump over inner cells. |
27f35b66 SN |
10006 | } |
10007 | } | |
10008 | ||
58dd5b3b MB |
10009 | // convert to scrolled coords |
10010 | // | |
27f35b66 SN |
10011 | CalcScrolledPosition( left, top, &left, &top ); |
10012 | CalcScrolledPosition( right, bottom, &right, &bottom ); | |
58dd5b3b MB |
10013 | |
10014 | int cw, ch; | |
10015 | m_gridWin->GetClientSize( &cw, &ch ); | |
10016 | ||
f6bcfd97 BP |
10017 | if (right < 0 || bottom < 0 || left > cw || top > ch) |
10018 | return wxRect( 0, 0, 0, 0); | |
10019 | ||
58dd5b3b MB |
10020 | rect.SetLeft( wxMax(0, left) ); |
10021 | rect.SetTop( wxMax(0, top) ); | |
10022 | rect.SetRight( wxMin(cw, right) ); | |
10023 | rect.SetBottom( wxMin(ch, bottom) ); | |
10024 | ||
f85afd4e MB |
10025 | return rect; |
10026 | } | |
10027 | ||
f85afd4e MB |
10028 | // |
10029 | // ------ Grid event classes | |
10030 | // | |
10031 | ||
bf7945ce | 10032 | IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent ) |
f85afd4e MB |
10033 | |
10034 | wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, | |
5c8fc7c1 | 10035 | int row, int col, int x, int y, bool sel, |
f85afd4e MB |
10036 | bool control, bool shift, bool alt, bool meta ) |
10037 | : wxNotifyEvent( type, id ) | |
10038 | { | |
10039 | m_row = row; | |
10040 | m_col = col; | |
10041 | m_x = x; | |
10042 | m_y = y; | |
5c8fc7c1 | 10043 | m_selecting = sel; |
f85afd4e MB |
10044 | m_control = control; |
10045 | m_shift = shift; | |
10046 | m_alt = alt; | |
10047 | m_meta = meta; | |
8f177c8e | 10048 | |
f85afd4e MB |
10049 | SetEventObject(obj); |
10050 | } | |
10051 | ||
10052 | ||
bf7945ce | 10053 | IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent ) |
f85afd4e MB |
10054 | |
10055 | wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj, | |
10056 | int rowOrCol, int x, int y, | |
10057 | bool control, bool shift, bool alt, bool meta ) | |
10058 | : wxNotifyEvent( type, id ) | |
10059 | { | |
10060 | m_rowOrCol = rowOrCol; | |
10061 | m_x = x; | |
10062 | m_y = y; | |
10063 | m_control = control; | |
10064 | m_shift = shift; | |
10065 | m_alt = alt; | |
10066 | m_meta = meta; | |
8f177c8e | 10067 | |
f85afd4e MB |
10068 | SetEventObject(obj); |
10069 | } | |
10070 | ||
10071 | ||
bf7945ce | 10072 | IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent ) |
f85afd4e MB |
10073 | |
10074 | wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, | |
8f177c8e VZ |
10075 | const wxGridCellCoords& topLeft, |
10076 | const wxGridCellCoords& bottomRight, | |
5c8fc7c1 SN |
10077 | bool sel, bool control, |
10078 | bool shift, bool alt, bool meta ) | |
8f177c8e | 10079 | : wxNotifyEvent( type, id ) |
f85afd4e MB |
10080 | { |
10081 | m_topLeft = topLeft; | |
10082 | m_bottomRight = bottomRight; | |
5c8fc7c1 | 10083 | m_selecting = sel; |
f85afd4e MB |
10084 | m_control = control; |
10085 | m_shift = shift; | |
10086 | m_alt = alt; | |
10087 | m_meta = meta; | |
10088 | ||
10089 | SetEventObject(obj); | |
10090 | } | |
10091 | ||
10092 | ||
bf7945ce RD |
10093 | IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent) |
10094 | ||
10095 | wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type, | |
10096 | wxObject* obj, int row, | |
10097 | int col, wxControl* ctrl) | |
10098 | : wxCommandEvent(type, id) | |
10099 | { | |
10100 | SetEventObject(obj); | |
10101 | m_row = row; | |
10102 | m_col = col; | |
10103 | m_ctrl = ctrl; | |
10104 | } | |
10105 | ||
27b92ca4 | 10106 | #endif // wxUSE_GRID |
f7556ff0 | 10107 |