]>
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 | ||
f85afd4e MB |
20 | #ifdef __GNUG__ |
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 | ||
8f177c8e | 35 | #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID) |
27b92ca4 VZ |
36 | #include "gridg.cpp" |
37 | #else // wxUSE_NEW_GRID | |
4d85bcd1 | 38 | |
f85afd4e MB |
39 | #ifndef WX_PRECOMP |
40 | #include "wx/utils.h" | |
41 | #include "wx/dcclient.h" | |
42 | #include "wx/settings.h" | |
43 | #include "wx/log.h" | |
508011ce VZ |
44 | #include "wx/textctrl.h" |
45 | #include "wx/checkbox.h" | |
4ee5fc9c | 46 | #include "wx/combobox.h" |
816be743 | 47 | #include "wx/valtext.h" |
f85afd4e MB |
48 | #endif |
49 | ||
cb5df486 | 50 | #include "wx/textfile.h" |
816be743 | 51 | #include "wx/spinctrl.h" |
c4608a8a | 52 | #include "wx/tokenzr.h" |
6d004f67 | 53 | |
07296f0b | 54 | #include "wx/grid.h" |
b5808881 | 55 | #include "wx/generic/gridsel.h" |
07296f0b | 56 | |
0b7e6e7d SN |
57 | #if defined(__WXMOTIF__) |
58 | #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier) | |
c78b3acd | 59 | #else |
0b7e6e7d | 60 | #define WXUNUSED_MOTIF(identifier) identifier |
c78b3acd SN |
61 | #endif |
62 | ||
63 | #if defined(__WXGTK__) | |
64 | #define WXUNUSED_GTK(identifier) WXUNUSED(identifier) | |
65 | #else | |
66 | #define WXUNUSED_GTK(identifier) identifier | |
67 | #endif | |
68 | ||
3f8e5072 JS |
69 | // Required for wxIs... functions |
70 | #include <ctype.h> | |
71 | ||
b99be8fb | 72 | // ---------------------------------------------------------------------------- |
758cbedf | 73 | // array classes |
b99be8fb VZ |
74 | // ---------------------------------------------------------------------------- |
75 | ||
f6bcfd97 | 76 | WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr *, wxArrayAttrs); |
758cbedf | 77 | |
b99be8fb VZ |
78 | struct wxGridCellWithAttr |
79 | { | |
2e9a6788 VZ |
80 | wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_) |
81 | : coords(row, col), attr(attr_) | |
b99be8fb VZ |
82 | { |
83 | } | |
84 | ||
2e9a6788 VZ |
85 | ~wxGridCellWithAttr() |
86 | { | |
87 | attr->DecRef(); | |
88 | } | |
89 | ||
b99be8fb | 90 | wxGridCellCoords coords; |
2e9a6788 | 91 | wxGridCellAttr *attr; |
22f3361e VZ |
92 | |
93 | // Cannot do this: | |
94 | // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr) | |
95 | // without rewriting the macros, which require a public copy constructor. | |
b99be8fb VZ |
96 | }; |
97 | ||
f6bcfd97 | 98 | WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray); |
b99be8fb VZ |
99 | |
100 | #include "wx/arrimpl.cpp" | |
101 | ||
102 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) | |
103 | WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray) | |
104 | ||
0f442030 RR |
105 | // ---------------------------------------------------------------------------- |
106 | // events | |
107 | // ---------------------------------------------------------------------------- | |
108 | ||
2e4df4bf VZ |
109 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK) |
110 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK) | |
111 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK) | |
112 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK) | |
113 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK) | |
114 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK) | |
115 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK) | |
116 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK) | |
117 | DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE) | |
118 | DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE) | |
119 | DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT) | |
120 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE) | |
121 | DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL) | |
122 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN) | |
123 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN) | |
bf7945ce | 124 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED) |
0f442030 | 125 | |
b99be8fb VZ |
126 | // ---------------------------------------------------------------------------- |
127 | // private classes | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
130 | class WXDLLEXPORT wxGridRowLabelWindow : public wxWindow | |
131 | { | |
132 | public: | |
133 | wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; } | |
134 | wxGridRowLabelWindow( wxGrid *parent, wxWindowID id, | |
135 | const wxPoint &pos, const wxSize &size ); | |
136 | ||
137 | private: | |
138 | wxGrid *m_owner; | |
139 | ||
140 | void OnPaint( wxPaintEvent& event ); | |
141 | void OnMouseEvent( wxMouseEvent& event ); | |
b51c3f27 | 142 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb | 143 | void OnKeyDown( wxKeyEvent& event ); |
f6bcfd97 | 144 | void OnKeyUp( wxKeyEvent& ); |
b99be8fb VZ |
145 | |
146 | DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow) | |
147 | DECLARE_EVENT_TABLE() | |
22f3361e | 148 | DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow) |
b99be8fb VZ |
149 | }; |
150 | ||
151 | ||
152 | class WXDLLEXPORT wxGridColLabelWindow : public wxWindow | |
153 | { | |
154 | public: | |
155 | wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; } | |
156 | wxGridColLabelWindow( wxGrid *parent, wxWindowID id, | |
157 | const wxPoint &pos, const wxSize &size ); | |
158 | ||
159 | private: | |
160 | wxGrid *m_owner; | |
161 | ||
162 | void OnPaint( wxPaintEvent &event ); | |
163 | void OnMouseEvent( wxMouseEvent& event ); | |
b51c3f27 | 164 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb | 165 | void OnKeyDown( wxKeyEvent& event ); |
f6bcfd97 | 166 | void OnKeyUp( wxKeyEvent& ); |
b99be8fb VZ |
167 | |
168 | DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow) | |
169 | DECLARE_EVENT_TABLE() | |
22f3361e | 170 | DECLARE_NO_COPY_CLASS(wxGridColLabelWindow) |
b99be8fb VZ |
171 | }; |
172 | ||
173 | ||
174 | class WXDLLEXPORT wxGridCornerLabelWindow : public wxWindow | |
175 | { | |
176 | public: | |
177 | wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; } | |
178 | wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id, | |
179 | const wxPoint &pos, const wxSize &size ); | |
180 | ||
181 | private: | |
182 | wxGrid *m_owner; | |
183 | ||
184 | void OnMouseEvent( wxMouseEvent& event ); | |
b51c3f27 | 185 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb | 186 | void OnKeyDown( wxKeyEvent& event ); |
f6bcfd97 | 187 | void OnKeyUp( wxKeyEvent& ); |
b99be8fb VZ |
188 | void OnPaint( wxPaintEvent& event ); |
189 | ||
190 | DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow) | |
191 | DECLARE_EVENT_TABLE() | |
22f3361e | 192 | DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow) |
b99be8fb VZ |
193 | }; |
194 | ||
59ddac01 | 195 | class WXDLLEXPORT wxGridWindow : public wxWindow |
b99be8fb VZ |
196 | { |
197 | public: | |
198 | wxGridWindow() | |
199 | { | |
200 | m_owner = (wxGrid *)NULL; | |
201 | m_rowLabelWin = (wxGridRowLabelWindow *)NULL; | |
202 | m_colLabelWin = (wxGridColLabelWindow *)NULL; | |
203 | } | |
204 | ||
205 | wxGridWindow( wxGrid *parent, | |
206 | wxGridRowLabelWindow *rowLblWin, | |
207 | wxGridColLabelWindow *colLblWin, | |
208 | wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
209 | ~wxGridWindow(); | |
210 | ||
211 | void ScrollWindow( int dx, int dy, const wxRect *rect ); | |
212 | ||
b819b854 JS |
213 | wxGrid* GetOwner() { return m_owner; } |
214 | ||
b99be8fb VZ |
215 | private: |
216 | wxGrid *m_owner; | |
217 | wxGridRowLabelWindow *m_rowLabelWin; | |
218 | wxGridColLabelWindow *m_colLabelWin; | |
219 | ||
220 | void OnPaint( wxPaintEvent &event ); | |
b51c3f27 | 221 | void OnMouseWheel( wxMouseEvent& event ); |
b99be8fb VZ |
222 | void OnMouseEvent( wxMouseEvent& event ); |
223 | void OnKeyDown( wxKeyEvent& ); | |
f6bcfd97 | 224 | void OnKeyUp( wxKeyEvent& ); |
2796cce3 RD |
225 | void OnEraseBackground( wxEraseEvent& ); |
226 | ||
b99be8fb VZ |
227 | |
228 | DECLARE_DYNAMIC_CLASS(wxGridWindow) | |
229 | DECLARE_EVENT_TABLE() | |
22f3361e | 230 | DECLARE_NO_COPY_CLASS(wxGridWindow) |
b99be8fb VZ |
231 | }; |
232 | ||
2796cce3 RD |
233 | |
234 | ||
235 | class wxGridCellEditorEvtHandler : public wxEvtHandler | |
236 | { | |
237 | public: | |
238 | wxGridCellEditorEvtHandler() | |
239 | : m_grid(0), m_editor(0) | |
240 | { } | |
241 | wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor) | |
242 | : m_grid(grid), m_editor(editor) | |
243 | { } | |
244 | ||
245 | void OnKeyDown(wxKeyEvent& event); | |
fb0de762 | 246 | void OnChar(wxKeyEvent& event); |
2796cce3 RD |
247 | |
248 | private: | |
249 | wxGrid* m_grid; | |
250 | wxGridCellEditor* m_editor; | |
251 | DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler) | |
252 | DECLARE_EVENT_TABLE() | |
22f3361e | 253 | DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler) |
2796cce3 RD |
254 | }; |
255 | ||
256 | ||
257 | IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler ) | |
258 | BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler ) | |
259 | EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown ) | |
fb0de762 | 260 | EVT_CHAR( wxGridCellEditorEvtHandler::OnChar ) |
2796cce3 RD |
261 | END_EVENT_TABLE() |
262 | ||
263 | ||
264 | ||
758cbedf | 265 | // ---------------------------------------------------------------------------- |
b99be8fb | 266 | // the internal data representation used by wxGridCellAttrProvider |
758cbedf VZ |
267 | // ---------------------------------------------------------------------------- |
268 | ||
269 | // this class stores attributes set for cells | |
270 | class WXDLLEXPORT wxGridCellAttrData | |
b99be8fb VZ |
271 | { |
272 | public: | |
2e9a6788 | 273 | void SetAttr(wxGridCellAttr *attr, int row, int col); |
b99be8fb | 274 | wxGridCellAttr *GetAttr(int row, int col) const; |
4d60017a SN |
275 | void UpdateAttrRows( size_t pos, int numRows ); |
276 | void UpdateAttrCols( size_t pos, int numCols ); | |
b99be8fb VZ |
277 | |
278 | private: | |
279 | // searches for the attr for given cell, returns wxNOT_FOUND if not found | |
280 | int FindIndex(int row, int col) const; | |
281 | ||
282 | wxGridCellWithAttrArray m_attrs; | |
283 | }; | |
284 | ||
758cbedf VZ |
285 | // this class stores attributes set for rows or columns |
286 | class WXDLLEXPORT wxGridRowOrColAttrData | |
287 | { | |
288 | public: | |
ee6694a7 VZ |
289 | // empty ctor to suppress warnings |
290 | wxGridRowOrColAttrData() { } | |
758cbedf VZ |
291 | ~wxGridRowOrColAttrData(); |
292 | ||
293 | void SetAttr(wxGridCellAttr *attr, int rowOrCol); | |
294 | wxGridCellAttr *GetAttr(int rowOrCol) const; | |
4d60017a | 295 | void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ); |
758cbedf VZ |
296 | |
297 | private: | |
298 | wxArrayInt m_rowsOrCols; | |
299 | wxArrayAttrs m_attrs; | |
300 | }; | |
301 | ||
302 | // NB: this is just a wrapper around 3 objects: one which stores cell | |
303 | // attributes, and 2 others for row/col ones | |
304 | class WXDLLEXPORT wxGridCellAttrProviderData | |
305 | { | |
306 | public: | |
307 | wxGridCellAttrData m_cellAttrs; | |
308 | wxGridRowOrColAttrData m_rowAttrs, | |
309 | m_colAttrs; | |
310 | }; | |
311 | ||
f2d76237 RD |
312 | |
313 | // ---------------------------------------------------------------------------- | |
314 | // data structures used for the data type registry | |
315 | // ---------------------------------------------------------------------------- | |
316 | ||
b94ae1ea VZ |
317 | struct wxGridDataTypeInfo |
318 | { | |
f2d76237 RD |
319 | wxGridDataTypeInfo(const wxString& typeName, |
320 | wxGridCellRenderer* renderer, | |
321 | wxGridCellEditor* editor) | |
322 | : m_typeName(typeName), m_renderer(renderer), m_editor(editor) | |
323 | { } | |
324 | ||
39bcce60 VZ |
325 | ~wxGridDataTypeInfo() |
326 | { | |
327 | wxSafeDecRef(m_renderer); | |
328 | wxSafeDecRef(m_editor); | |
329 | } | |
f2d76237 RD |
330 | |
331 | wxString m_typeName; | |
332 | wxGridCellRenderer* m_renderer; | |
333 | wxGridCellEditor* m_editor; | |
22f3361e VZ |
334 | |
335 | DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo) | |
f2d76237 RD |
336 | }; |
337 | ||
338 | ||
f6bcfd97 | 339 | WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo*, wxGridDataTypeInfoArray); |
f2d76237 RD |
340 | |
341 | ||
b94ae1ea VZ |
342 | class WXDLLEXPORT wxGridTypeRegistry |
343 | { | |
f2d76237 | 344 | public: |
c78b3acd | 345 | wxGridTypeRegistry() {} |
f2d76237 | 346 | ~wxGridTypeRegistry(); |
b94ae1ea | 347 | |
f2d76237 RD |
348 | void RegisterDataType(const wxString& typeName, |
349 | wxGridCellRenderer* renderer, | |
350 | wxGridCellEditor* editor); | |
c4608a8a VZ |
351 | |
352 | // find one of already registered data types | |
353 | int FindRegisteredDataType(const wxString& typeName); | |
354 | ||
355 | // try to FindRegisteredDataType(), if this fails and typeName is one of | |
356 | // standard typenames, register it and return its index | |
f2d76237 | 357 | int FindDataType(const wxString& typeName); |
c4608a8a VZ |
358 | |
359 | // try to FindDataType(), if it fails see if it is not one of already | |
360 | // registered data types with some params in which case clone the | |
361 | // registered data type and set params for it | |
362 | int FindOrCloneDataType(const wxString& typeName); | |
363 | ||
f2d76237 RD |
364 | wxGridCellRenderer* GetRenderer(int index); |
365 | wxGridCellEditor* GetEditor(int index); | |
366 | ||
367 | private: | |
368 | wxGridDataTypeInfoArray m_typeinfo; | |
369 | }; | |
370 | ||
b99be8fb VZ |
371 | // ---------------------------------------------------------------------------- |
372 | // conditional compilation | |
373 | // ---------------------------------------------------------------------------- | |
374 | ||
9496deb5 MB |
375 | #ifndef WXGRID_DRAW_LINES |
376 | #define WXGRID_DRAW_LINES 1 | |
796df70a SN |
377 | #endif |
378 | ||
0a976765 VZ |
379 | // ---------------------------------------------------------------------------- |
380 | // globals | |
381 | // ---------------------------------------------------------------------------- | |
382 | ||
383 | //#define DEBUG_ATTR_CACHE | |
384 | #ifdef DEBUG_ATTR_CACHE | |
385 | static size_t gs_nAttrCacheHits = 0; | |
386 | static size_t gs_nAttrCacheMisses = 0; | |
387 | #endif // DEBUG_ATTR_CACHE | |
f85afd4e | 388 | |
43947979 VZ |
389 | // ---------------------------------------------------------------------------- |
390 | // constants | |
391 | // ---------------------------------------------------------------------------- | |
392 | ||
f85afd4e MB |
393 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); |
394 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); | |
395 | ||
f0102d2a | 396 | // scroll line size |
faec5a43 SN |
397 | // TODO: this doesn't work at all, grid cells have different sizes and approx |
398 | // calculations don't work as because of the size mismatch scrollbars | |
399 | // sometimes fail to be shown when they should be or vice versa | |
b51c3f27 RD |
400 | // |
401 | // The scroll bars may be a little flakey once in a while, but that is | |
402 | // surely much less horrible than having scroll lines of only 1!!! | |
403 | // -- Robin | |
97a9929e VZ |
404 | // |
405 | // Well, it's still seriously broken so it might be better but needs | |
406 | // fixing anyhow | |
407 | // -- Vadim | |
408 | static const size_t GRID_SCROLL_LINE_X = 15; // 1; | |
409 | static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X; | |
f85afd4e | 410 | |
43947979 VZ |
411 | // the size of hash tables used a bit everywhere (the max number of elements |
412 | // in these hash tables is the number of rows/columns) | |
413 | static const int GRID_HASH_SIZE = 100; | |
414 | ||
97a9929e VZ |
415 | // ---------------------------------------------------------------------------- |
416 | // private functions | |
417 | // ---------------------------------------------------------------------------- | |
418 | ||
d0eb7e56 | 419 | static inline int GetScrollX(int x) |
97a9929e VZ |
420 | { |
421 | return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X; | |
422 | } | |
423 | ||
d0eb7e56 | 424 | static inline int GetScrollY(int y) |
97a9929e VZ |
425 | { |
426 | return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y; | |
427 | } | |
428 | ||
ab79958a VZ |
429 | // ============================================================================ |
430 | // implementation | |
431 | // ============================================================================ | |
432 | ||
2796cce3 RD |
433 | // ---------------------------------------------------------------------------- |
434 | // wxGridCellEditor | |
435 | // ---------------------------------------------------------------------------- | |
436 | ||
437 | wxGridCellEditor::wxGridCellEditor() | |
438 | { | |
439 | m_control = NULL; | |
1bd71df9 | 440 | m_attr = NULL; |
2796cce3 RD |
441 | } |
442 | ||
443 | ||
444 | wxGridCellEditor::~wxGridCellEditor() | |
445 | { | |
446 | Destroy(); | |
447 | } | |
448 | ||
508011ce VZ |
449 | void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent), |
450 | wxWindowID WXUNUSED(id), | |
451 | wxEvtHandler* evtHandler) | |
452 | { | |
189d0213 | 453 | if ( evtHandler ) |
508011ce VZ |
454 | m_control->PushEventHandler(evtHandler); |
455 | } | |
2796cce3 | 456 | |
189d0213 VZ |
457 | void wxGridCellEditor::PaintBackground(const wxRect& rectCell, |
458 | wxGridCellAttr *attr) | |
459 | { | |
460 | // erase the background because we might not fill the cell | |
461 | wxClientDC dc(m_control->GetParent()); | |
b819b854 JS |
462 | wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow); |
463 | if (gridWindow) | |
464 | gridWindow->GetOwner()->PrepareDC(dc); | |
465 | ||
189d0213 VZ |
466 | dc.SetPen(*wxTRANSPARENT_PEN); |
467 | dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID)); | |
468 | dc.DrawRectangle(rectCell); | |
469 | ||
470 | // redraw the control we just painted over | |
471 | m_control->Refresh(); | |
472 | } | |
473 | ||
2796cce3 RD |
474 | void wxGridCellEditor::Destroy() |
475 | { | |
508011ce VZ |
476 | if (m_control) |
477 | { | |
b94ae1ea VZ |
478 | m_control->PopEventHandler(TRUE /* delete it*/); |
479 | ||
2796cce3 RD |
480 | m_control->Destroy(); |
481 | m_control = NULL; | |
482 | } | |
483 | } | |
484 | ||
3da93aae | 485 | void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr) |
2796cce3 RD |
486 | { |
487 | wxASSERT_MSG(m_control, | |
488 | wxT("The wxGridCellEditor must be Created first!")); | |
489 | m_control->Show(show); | |
3da93aae VZ |
490 | |
491 | if ( show ) | |
492 | { | |
493 | // set the colours/fonts if we have any | |
494 | if ( attr ) | |
495 | { | |
f2d76237 RD |
496 | m_colFgOld = m_control->GetForegroundColour(); |
497 | m_control->SetForegroundColour(attr->GetTextColour()); | |
3da93aae | 498 | |
f2d76237 RD |
499 | m_colBgOld = m_control->GetBackgroundColour(); |
500 | m_control->SetBackgroundColour(attr->GetBackgroundColour()); | |
3da93aae | 501 | |
f2d76237 RD |
502 | m_fontOld = m_control->GetFont(); |
503 | m_control->SetFont(attr->GetFont()); | |
3da93aae VZ |
504 | |
505 | // can't do anything more in the base class version, the other | |
506 | // attributes may only be used by the derived classes | |
507 | } | |
508 | } | |
509 | else | |
510 | { | |
511 | // restore the standard colours fonts | |
512 | if ( m_colFgOld.Ok() ) | |
513 | { | |
514 | m_control->SetForegroundColour(m_colFgOld); | |
515 | m_colFgOld = wxNullColour; | |
516 | } | |
517 | ||
518 | if ( m_colBgOld.Ok() ) | |
519 | { | |
520 | m_control->SetBackgroundColour(m_colBgOld); | |
521 | m_colBgOld = wxNullColour; | |
522 | } | |
523 | ||
524 | if ( m_fontOld.Ok() ) | |
525 | { | |
526 | m_control->SetFont(m_fontOld); | |
527 | m_fontOld = wxNullFont; | |
528 | } | |
529 | } | |
2796cce3 RD |
530 | } |
531 | ||
532 | void wxGridCellEditor::SetSize(const wxRect& rect) | |
533 | { | |
534 | wxASSERT_MSG(m_control, | |
535 | wxT("The wxGridCellEditor must be Created first!")); | |
28a77bc4 | 536 | m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); |
2796cce3 RD |
537 | } |
538 | ||
539 | void wxGridCellEditor::HandleReturn(wxKeyEvent& event) | |
540 | { | |
541 | event.Skip(); | |
542 | } | |
543 | ||
f6bcfd97 BP |
544 | bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event) |
545 | { | |
546 | // accept the simple key presses, not anything with Ctrl/Alt/Meta | |
a4f7bf58 | 547 | return !(event.ControlDown() || event.AltDown()); |
f6bcfd97 | 548 | } |
2796cce3 | 549 | |
2c9a89e0 RD |
550 | void wxGridCellEditor::StartingKey(wxKeyEvent& event) |
551 | { | |
e195a54c VZ |
552 | event.Skip(); |
553 | } | |
2c9a89e0 | 554 | |
e195a54c VZ |
555 | void wxGridCellEditor::StartingClick() |
556 | { | |
b54ba671 | 557 | } |
2c9a89e0 | 558 | |
3a8c693a VZ |
559 | #if wxUSE_TEXTCTRL |
560 | ||
b54ba671 VZ |
561 | // ---------------------------------------------------------------------------- |
562 | // wxGridCellTextEditor | |
563 | // ---------------------------------------------------------------------------- | |
2c9a89e0 | 564 | |
2796cce3 RD |
565 | wxGridCellTextEditor::wxGridCellTextEditor() |
566 | { | |
c4608a8a | 567 | m_maxChars = 0; |
2796cce3 RD |
568 | } |
569 | ||
570 | void wxGridCellTextEditor::Create(wxWindow* parent, | |
571 | wxWindowID id, | |
2796cce3 RD |
572 | wxEvtHandler* evtHandler) |
573 | { | |
508011ce | 574 | m_control = new wxTextCtrl(parent, id, wxEmptyString, |
2c9a89e0 | 575 | wxDefaultPosition, wxDefaultSize |
2796cce3 | 576 | #if defined(__WXMSW__) |
84912ef8 RD |
577 | , wxTE_PROCESS_TAB | wxTE_MULTILINE | |
578 | wxTE_NO_VSCROLL | wxTE_AUTO_SCROLL | |
2796cce3 | 579 | #endif |
508011ce | 580 | ); |
2796cce3 | 581 | |
c4608a8a VZ |
582 | // TODO: use m_maxChars |
583 | ||
508011ce | 584 | wxGridCellEditor::Create(parent, id, evtHandler); |
2796cce3 RD |
585 | } |
586 | ||
189d0213 VZ |
587 | void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell), |
588 | wxGridCellAttr * WXUNUSED(attr)) | |
589 | { | |
590 | // as we fill the entire client area, don't do anything here to minimize | |
591 | // flicker | |
592 | } | |
2796cce3 | 593 | |
99306db2 VZ |
594 | void wxGridCellTextEditor::SetSize(const wxRect& rectOrig) |
595 | { | |
596 | wxRect rect(rectOrig); | |
597 | ||
598 | // Make the edit control large enough to allow for internal | |
599 | // margins | |
600 | // | |
601 | // TODO: remove this if the text ctrl sizing is improved esp. for | |
602 | // unix | |
603 | // | |
604 | #if defined(__WXGTK__) | |
b0e282b3 RR |
605 | if (rect.x != 0) |
606 | { | |
607 | rect.x += 1; | |
608 | rect.y += 1; | |
609 | rect.width -= 1; | |
610 | rect.height -= 1; | |
611 | } | |
99306db2 | 612 | #else // !GTK |
cb105ad4 | 613 | int extra_x = ( rect.x > 2 )? 2 : 1; |
84912ef8 RD |
614 | |
615 | // MB: treat MSW separately here otherwise the caret doesn't show | |
616 | // when the editor is in the first row. | |
a0948e27 MB |
617 | #if defined(__WXMSW__) |
618 | int extra_y = 2; | |
619 | #else | |
cb105ad4 | 620 | int extra_y = ( rect.y > 2 )? 2 : 1; |
a0948e27 MB |
621 | #endif // MSW |
622 | ||
99306db2 | 623 | #if defined(__WXMOTIF__) |
cb105ad4 SN |
624 | extra_x *= 2; |
625 | extra_y *= 2; | |
99306db2 | 626 | #endif |
cb105ad4 SN |
627 | rect.SetLeft( wxMax(0, rect.x - extra_x) ); |
628 | rect.SetTop( wxMax(0, rect.y - extra_y) ); | |
629 | rect.SetRight( rect.GetRight() + 2*extra_x ); | |
630 | rect.SetBottom( rect.GetBottom() + 2*extra_y ); | |
99306db2 VZ |
631 | #endif // GTK/!GTK |
632 | ||
633 | wxGridCellEditor::SetSize(rect); | |
634 | } | |
635 | ||
3da93aae | 636 | void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid) |
2796cce3 RD |
637 | { |
638 | wxASSERT_MSG(m_control, | |
639 | wxT("The wxGridCellEditor must be Created first!")); | |
640 | ||
641 | m_startValue = grid->GetTable()->GetValue(row, col); | |
816be743 VZ |
642 | |
643 | DoBeginEdit(m_startValue); | |
644 | } | |
645 | ||
646 | void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue) | |
647 | { | |
648 | Text()->SetValue(startValue); | |
b54ba671 | 649 | Text()->SetInsertionPointEnd(); |
505f70de | 650 | Text()->SetSelection(-1,-1); |
b54ba671 | 651 | Text()->SetFocus(); |
2796cce3 RD |
652 | } |
653 | ||
3324d5f5 | 654 | bool wxGridCellTextEditor::EndEdit(int row, int col, |
3da93aae | 655 | wxGrid* grid) |
2796cce3 RD |
656 | { |
657 | wxASSERT_MSG(m_control, | |
658 | wxT("The wxGridCellEditor must be Created first!")); | |
659 | ||
660 | bool changed = FALSE; | |
b54ba671 | 661 | wxString value = Text()->GetValue(); |
2796cce3 RD |
662 | if (value != m_startValue) |
663 | changed = TRUE; | |
664 | ||
665 | if (changed) | |
666 | grid->GetTable()->SetValue(row, col, value); | |
2c9a89e0 | 667 | |
3da93aae | 668 | m_startValue = wxEmptyString; |
b54ba671 | 669 | Text()->SetValue(m_startValue); |
2796cce3 RD |
670 | |
671 | return changed; | |
672 | } | |
673 | ||
674 | ||
675 | void wxGridCellTextEditor::Reset() | |
676 | { | |
677 | wxASSERT_MSG(m_control, | |
678 | wxT("The wxGridCellEditor must be Created first!")); | |
679 | ||
816be743 VZ |
680 | DoReset(m_startValue); |
681 | } | |
682 | ||
683 | void wxGridCellTextEditor::DoReset(const wxString& startValue) | |
684 | { | |
685 | Text()->SetValue(startValue); | |
b54ba671 | 686 | Text()->SetInsertionPointEnd(); |
2796cce3 RD |
687 | } |
688 | ||
f6bcfd97 BP |
689 | bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event) |
690 | { | |
691 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
692 | { | |
693 | int keycode = event.GetKeyCode(); | |
694 | switch ( keycode ) | |
695 | { | |
696 | case WXK_NUMPAD0: | |
697 | case WXK_NUMPAD1: | |
698 | case WXK_NUMPAD2: | |
699 | case WXK_NUMPAD3: | |
700 | case WXK_NUMPAD4: | |
701 | case WXK_NUMPAD5: | |
702 | case WXK_NUMPAD6: | |
703 | case WXK_NUMPAD7: | |
704 | case WXK_NUMPAD8: | |
705 | case WXK_NUMPAD9: | |
706 | case WXK_MULTIPLY: | |
707 | case WXK_NUMPAD_MULTIPLY: | |
708 | case WXK_ADD: | |
709 | case WXK_NUMPAD_ADD: | |
710 | case WXK_SUBTRACT: | |
711 | case WXK_NUMPAD_SUBTRACT: | |
712 | case WXK_DECIMAL: | |
713 | case WXK_NUMPAD_DECIMAL: | |
714 | case WXK_DIVIDE: | |
715 | case WXK_NUMPAD_DIVIDE: | |
716 | return TRUE; | |
717 | ||
718 | default: | |
719 | // accept 8 bit chars too if isprint() agrees | |
720 | if ( (keycode < 255) && (isprint(keycode)) ) | |
721 | return TRUE; | |
722 | } | |
723 | } | |
724 | ||
725 | return FALSE; | |
726 | } | |
727 | ||
2c9a89e0 RD |
728 | void wxGridCellTextEditor::StartingKey(wxKeyEvent& event) |
729 | { | |
94af7d45 | 730 | if ( !Text()->EmulateKeyPress(event) ) |
f6bcfd97 BP |
731 | { |
732 | event.Skip(); | |
733 | } | |
b54ba671 | 734 | } |
2c9a89e0 | 735 | |
c78b3acd | 736 | void wxGridCellTextEditor::HandleReturn( wxKeyEvent& |
0b7e6e7d | 737 | WXUNUSED_GTK(WXUNUSED_MOTIF(event)) ) |
2796cce3 RD |
738 | { |
739 | #if defined(__WXMOTIF__) || defined(__WXGTK__) | |
740 | // wxMotif needs a little extra help... | |
6fc0f38f | 741 | size_t pos = (size_t)( Text()->GetInsertionPoint() ); |
b54ba671 | 742 | wxString s( Text()->GetValue() ); |
8dd8f875 | 743 | s = s.Left(pos) + wxT("\n") + s.Mid(pos); |
b54ba671 VZ |
744 | Text()->SetValue(s); |
745 | Text()->SetInsertionPoint( pos ); | |
2796cce3 RD |
746 | #else |
747 | // the other ports can handle a Return key press | |
748 | // | |
749 | event.Skip(); | |
750 | #endif | |
751 | } | |
752 | ||
c4608a8a VZ |
753 | void wxGridCellTextEditor::SetParameters(const wxString& params) |
754 | { | |
755 | if ( !params ) | |
756 | { | |
757 | // reset to default | |
758 | m_maxChars = 0; | |
759 | } | |
760 | else | |
761 | { | |
762 | long tmp; | |
763 | if ( !params.ToLong(&tmp) ) | |
764 | { | |
f6bcfd97 | 765 | wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str()); |
c4608a8a VZ |
766 | } |
767 | else | |
768 | { | |
769 | m_maxChars = (size_t)tmp; | |
770 | } | |
771 | } | |
772 | } | |
773 | ||
73145b0e JS |
774 | // return the value in the text control |
775 | wxString wxGridCellTextEditor::GetValue() const | |
776 | { | |
777 | return Text()->GetValue(); | |
778 | } | |
779 | ||
816be743 VZ |
780 | // ---------------------------------------------------------------------------- |
781 | // wxGridCellNumberEditor | |
782 | // ---------------------------------------------------------------------------- | |
783 | ||
784 | wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max) | |
785 | { | |
786 | m_min = min; | |
787 | m_max = max; | |
788 | } | |
789 | ||
790 | void wxGridCellNumberEditor::Create(wxWindow* parent, | |
791 | wxWindowID id, | |
792 | wxEvtHandler* evtHandler) | |
793 | { | |
794 | if ( HasRange() ) | |
795 | { | |
796 | // create a spin ctrl | |
797 | m_control = new wxSpinCtrl(parent, -1, wxEmptyString, | |
798 | wxDefaultPosition, wxDefaultSize, | |
799 | wxSP_ARROW_KEYS, | |
800 | m_min, m_max); | |
801 | ||
802 | wxGridCellEditor::Create(parent, id, evtHandler); | |
803 | } | |
804 | else | |
805 | { | |
806 | // just a text control | |
807 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
808 | ||
809 | #if wxUSE_VALIDATORS | |
85bc0351 | 810 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); |
816be743 VZ |
811 | #endif // wxUSE_VALIDATORS |
812 | } | |
813 | } | |
814 | ||
815 | void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid) | |
816 | { | |
817 | // first get the value | |
818 | wxGridTableBase *table = grid->GetTable(); | |
819 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
820 | { | |
821 | m_valueOld = table->GetValueAsLong(row, col); | |
822 | } | |
823 | else | |
824 | { | |
8a60ff0a | 825 | m_valueOld = 0; |
a5777624 | 826 | wxString sValue = table->GetValue(row, col); |
8a60ff0a | 827 | if (! sValue.ToLong(&m_valueOld) && ! sValue.IsEmpty()) |
a5777624 RD |
828 | { |
829 | wxFAIL_MSG( _T("this cell doesn't have numeric value") ); | |
830 | return; | |
831 | } | |
816be743 VZ |
832 | } |
833 | ||
834 | if ( HasRange() ) | |
835 | { | |
4a64bee4 | 836 | Spin()->SetValue((int)m_valueOld); |
f6bcfd97 | 837 | Spin()->SetFocus(); |
816be743 VZ |
838 | } |
839 | else | |
840 | { | |
841 | DoBeginEdit(GetString()); | |
842 | } | |
843 | } | |
844 | ||
3324d5f5 | 845 | bool wxGridCellNumberEditor::EndEdit(int row, int col, |
816be743 VZ |
846 | wxGrid* grid) |
847 | { | |
848 | bool changed; | |
8a60ff0a RD |
849 | long value = 0; |
850 | wxString text; | |
816be743 VZ |
851 | |
852 | if ( HasRange() ) | |
853 | { | |
854 | value = Spin()->GetValue(); | |
855 | changed = value != m_valueOld; | |
8a60ff0a RD |
856 | if (changed) |
857 | text = wxString::Format(wxT("%ld"), value); | |
816be743 VZ |
858 | } |
859 | else | |
860 | { | |
8a60ff0a RD |
861 | text = Text()->GetValue(); |
862 | changed = (text.IsEmpty() || text.ToLong(&value)) && (value != m_valueOld); | |
816be743 VZ |
863 | } |
864 | ||
865 | if ( changed ) | |
866 | { | |
a5777624 RD |
867 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER)) |
868 | grid->GetTable()->SetValueAsLong(row, col, value); | |
869 | else | |
8a60ff0a | 870 | grid->GetTable()->SetValue(row, col, text); |
816be743 VZ |
871 | } |
872 | ||
873 | return changed; | |
874 | } | |
875 | ||
876 | void wxGridCellNumberEditor::Reset() | |
877 | { | |
878 | if ( HasRange() ) | |
879 | { | |
4a64bee4 | 880 | Spin()->SetValue((int)m_valueOld); |
816be743 VZ |
881 | } |
882 | else | |
883 | { | |
884 | DoReset(GetString()); | |
885 | } | |
886 | } | |
887 | ||
f6bcfd97 BP |
888 | bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event) |
889 | { | |
890 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
891 | { | |
892 | int keycode = event.GetKeyCode(); | |
893 | switch ( keycode ) | |
894 | { | |
895 | case WXK_NUMPAD0: | |
896 | case WXK_NUMPAD1: | |
897 | case WXK_NUMPAD2: | |
898 | case WXK_NUMPAD3: | |
899 | case WXK_NUMPAD4: | |
900 | case WXK_NUMPAD5: | |
901 | case WXK_NUMPAD6: | |
902 | case WXK_NUMPAD7: | |
903 | case WXK_NUMPAD8: | |
904 | case WXK_NUMPAD9: | |
905 | case WXK_ADD: | |
906 | case WXK_NUMPAD_ADD: | |
907 | case WXK_SUBTRACT: | |
908 | case WXK_NUMPAD_SUBTRACT: | |
909 | case WXK_UP: | |
910 | case WXK_DOWN: | |
911 | return TRUE; | |
912 | ||
913 | default: | |
914 | if ( (keycode < 128) && isdigit(keycode) ) | |
915 | return TRUE; | |
916 | } | |
917 | } | |
918 | ||
919 | return FALSE; | |
920 | } | |
921 | ||
816be743 VZ |
922 | void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event) |
923 | { | |
924 | if ( !HasRange() ) | |
925 | { | |
12a3f227 | 926 | int keycode = event.GetKeyCode(); |
85d8c319 JS |
927 | if ( isdigit(keycode) || keycode == '+' || keycode == '-' |
928 | || keycode == WXK_NUMPAD0 | |
929 | || keycode == WXK_NUMPAD1 | |
930 | || keycode == WXK_NUMPAD2 | |
931 | || keycode == WXK_NUMPAD3 | |
932 | || keycode == WXK_NUMPAD4 | |
933 | || keycode == WXK_NUMPAD5 | |
934 | || keycode == WXK_NUMPAD6 | |
935 | || keycode == WXK_NUMPAD7 | |
936 | || keycode == WXK_NUMPAD8 | |
937 | || keycode == WXK_NUMPAD9 | |
938 | || keycode == WXK_ADD | |
939 | || keycode == WXK_NUMPAD_ADD | |
940 | || keycode == WXK_SUBTRACT | |
941 | || keycode == WXK_NUMPAD_SUBTRACT) | |
816be743 VZ |
942 | { |
943 | wxGridCellTextEditor::StartingKey(event); | |
944 | ||
945 | // skip Skip() below | |
946 | return; | |
947 | } | |
948 | } | |
949 | ||
950 | event.Skip(); | |
951 | } | |
9c4ba614 | 952 | |
c4608a8a VZ |
953 | void wxGridCellNumberEditor::SetParameters(const wxString& params) |
954 | { | |
955 | if ( !params ) | |
956 | { | |
957 | // reset to default | |
958 | m_min = | |
959 | m_max = -1; | |
960 | } | |
961 | else | |
962 | { | |
963 | long tmp; | |
964 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
965 | { | |
966 | m_min = (int)tmp; | |
967 | ||
968 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
969 | { | |
970 | m_max = (int)tmp; | |
971 | ||
972 | // skip the error message below | |
973 | return; | |
974 | } | |
975 | } | |
976 | ||
f6bcfd97 | 977 | wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str()); |
c4608a8a VZ |
978 | } |
979 | } | |
980 | ||
73145b0e JS |
981 | // return the value in the spin control if it is there (the text control otherwise) |
982 | wxString wxGridCellNumberEditor::GetValue() const | |
983 | { | |
984 | wxString s; | |
985 | ||
986 | if( HasRange() ) | |
987 | { | |
14ac4f3c | 988 | long value = Spin()->GetValue(); |
73145b0e JS |
989 | s.Printf(wxT("%ld"), value); |
990 | } | |
991 | else | |
992 | { | |
993 | s = Text()->GetValue(); | |
994 | } | |
995 | return s; | |
996 | } | |
997 | ||
816be743 VZ |
998 | // ---------------------------------------------------------------------------- |
999 | // wxGridCellFloatEditor | |
1000 | // ---------------------------------------------------------------------------- | |
1001 | ||
f6bcfd97 BP |
1002 | wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision) |
1003 | { | |
1004 | m_width = width; | |
1005 | m_precision = precision; | |
1006 | } | |
1007 | ||
816be743 VZ |
1008 | void wxGridCellFloatEditor::Create(wxWindow* parent, |
1009 | wxWindowID id, | |
1010 | wxEvtHandler* evtHandler) | |
1011 | { | |
1012 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
1013 | ||
1014 | #if wxUSE_VALIDATORS | |
85bc0351 | 1015 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); |
816be743 VZ |
1016 | #endif // wxUSE_VALIDATORS |
1017 | } | |
1018 | ||
1019 | void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1020 | { | |
1021 | // first get the value | |
1022 | wxGridTableBase *table = grid->GetTable(); | |
1023 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
1024 | { | |
1025 | m_valueOld = table->GetValueAsDouble(row, col); | |
1026 | } | |
1027 | else | |
1028 | { | |
8a60ff0a | 1029 | m_valueOld = 0.0; |
a5777624 | 1030 | wxString sValue = table->GetValue(row, col); |
8a60ff0a | 1031 | if (! sValue.ToDouble(&m_valueOld) && ! sValue.IsEmpty()) |
a5777624 RD |
1032 | { |
1033 | wxFAIL_MSG( _T("this cell doesn't have float value") ); | |
1034 | return; | |
1035 | } | |
816be743 VZ |
1036 | } |
1037 | ||
1038 | DoBeginEdit(GetString()); | |
1039 | } | |
1040 | ||
3324d5f5 | 1041 | bool wxGridCellFloatEditor::EndEdit(int row, int col, |
816be743 VZ |
1042 | wxGrid* grid) |
1043 | { | |
8a60ff0a RD |
1044 | double value = 0.0; |
1045 | wxString text(Text()->GetValue()); | |
1046 | ||
1047 | if ( (text.IsEmpty() || text.ToDouble(&value)) && (value != m_valueOld) ) | |
816be743 | 1048 | { |
a5777624 RD |
1049 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT)) |
1050 | grid->GetTable()->SetValueAsDouble(row, col, value); | |
1051 | else | |
8a60ff0a | 1052 | grid->GetTable()->SetValue(row, col, text); |
816be743 VZ |
1053 | |
1054 | return TRUE; | |
1055 | } | |
ae31cc57 | 1056 | return FALSE; |
816be743 VZ |
1057 | } |
1058 | ||
1059 | void wxGridCellFloatEditor::Reset() | |
1060 | { | |
1061 | DoReset(GetString()); | |
1062 | } | |
1063 | ||
1064 | void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event) | |
1065 | { | |
12a3f227 | 1066 | int keycode = event.GetKeyCode(); |
85d8c319 JS |
1067 | if ( isdigit(keycode) || keycode == '+' || keycode == '-' || keycode == '.' |
1068 | || keycode == WXK_NUMPAD0 | |
1069 | || keycode == WXK_NUMPAD1 | |
1070 | || keycode == WXK_NUMPAD2 | |
1071 | || keycode == WXK_NUMPAD3 | |
1072 | || keycode == WXK_NUMPAD4 | |
1073 | || keycode == WXK_NUMPAD5 | |
1074 | || keycode == WXK_NUMPAD6 | |
1075 | || keycode == WXK_NUMPAD7 | |
1076 | || keycode == WXK_NUMPAD8 | |
1077 | || keycode == WXK_NUMPAD9 | |
1078 | || keycode == WXK_ADD | |
1079 | || keycode == WXK_NUMPAD_ADD | |
1080 | || keycode == WXK_SUBTRACT | |
1081 | || keycode == WXK_NUMPAD_SUBTRACT) | |
816be743 VZ |
1082 | { |
1083 | wxGridCellTextEditor::StartingKey(event); | |
1084 | ||
1085 | // skip Skip() below | |
1086 | return; | |
1087 | } | |
1088 | ||
1089 | event.Skip(); | |
1090 | } | |
1091 | ||
f6bcfd97 BP |
1092 | void wxGridCellFloatEditor::SetParameters(const wxString& params) |
1093 | { | |
1094 | if ( !params ) | |
1095 | { | |
1096 | // reset to default | |
1097 | m_width = | |
1098 | m_precision = -1; | |
1099 | } | |
1100 | else | |
1101 | { | |
1102 | long tmp; | |
1103 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
1104 | { | |
1105 | m_width = (int)tmp; | |
1106 | ||
1107 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
1108 | { | |
1109 | m_precision = (int)tmp; | |
1110 | ||
1111 | // skip the error message below | |
1112 | return; | |
1113 | } | |
1114 | } | |
1115 | ||
1116 | wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str()); | |
1117 | } | |
1118 | } | |
1119 | ||
1120 | wxString wxGridCellFloatEditor::GetString() const | |
1121 | { | |
1122 | wxString fmt; | |
1123 | if ( m_width == -1 ) | |
1124 | { | |
1125 | // default width/precision | |
ec53826c | 1126 | fmt = _T("%f"); |
f6bcfd97 BP |
1127 | } |
1128 | else if ( m_precision == -1 ) | |
1129 | { | |
1130 | // default precision | |
ec53826c | 1131 | fmt.Printf(_T("%%%d.f"), m_width); |
f6bcfd97 BP |
1132 | } |
1133 | else | |
1134 | { | |
ec53826c | 1135 | fmt.Printf(_T("%%%d.%df"), m_width, m_precision); |
f6bcfd97 BP |
1136 | } |
1137 | ||
1138 | return wxString::Format(fmt, m_valueOld); | |
1139 | } | |
1140 | ||
1141 | bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event) | |
1142 | { | |
1143 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
1144 | { | |
1145 | int keycode = event.GetKeyCode(); | |
1146 | switch ( keycode ) | |
1147 | { | |
1148 | case WXK_NUMPAD0: | |
1149 | case WXK_NUMPAD1: | |
1150 | case WXK_NUMPAD2: | |
1151 | case WXK_NUMPAD3: | |
1152 | case WXK_NUMPAD4: | |
1153 | case WXK_NUMPAD5: | |
1154 | case WXK_NUMPAD6: | |
1155 | case WXK_NUMPAD7: | |
1156 | case WXK_NUMPAD8: | |
1157 | case WXK_NUMPAD9: | |
1158 | case WXK_ADD: | |
1159 | case WXK_NUMPAD_ADD: | |
1160 | case WXK_SUBTRACT: | |
1161 | case WXK_NUMPAD_SUBTRACT: | |
1162 | case WXK_DECIMAL: | |
1163 | case WXK_NUMPAD_DECIMAL: | |
1164 | return TRUE; | |
1165 | ||
1166 | default: | |
1167 | // additionally accept 'e' as in '1e+6' | |
1168 | if ( (keycode < 128) && | |
c6707d16 | 1169 | (isdigit(keycode) || tolower(keycode) == 'e') ) |
f6bcfd97 BP |
1170 | return TRUE; |
1171 | } | |
1172 | } | |
1173 | ||
1174 | return FALSE; | |
1175 | } | |
1176 | ||
3a8c693a VZ |
1177 | #endif // wxUSE_TEXTCTRL |
1178 | ||
1179 | #if wxUSE_CHECKBOX | |
1180 | ||
508011ce VZ |
1181 | // ---------------------------------------------------------------------------- |
1182 | // wxGridCellBoolEditor | |
1183 | // ---------------------------------------------------------------------------- | |
1184 | ||
1185 | void wxGridCellBoolEditor::Create(wxWindow* parent, | |
1186 | wxWindowID id, | |
1187 | wxEvtHandler* evtHandler) | |
1188 | { | |
1189 | m_control = new wxCheckBox(parent, id, wxEmptyString, | |
1190 | wxDefaultPosition, wxDefaultSize, | |
1191 | wxNO_BORDER); | |
1192 | ||
1193 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1194 | } | |
1195 | ||
1196 | void wxGridCellBoolEditor::SetSize(const wxRect& r) | |
1197 | { | |
b94ae1ea VZ |
1198 | bool resize = FALSE; |
1199 | wxSize size = m_control->GetSize(); | |
1200 | wxCoord minSize = wxMin(r.width, r.height); | |
1201 | ||
1202 | // check if the checkbox is not too big/small for this cell | |
1203 | wxSize sizeBest = m_control->GetBestSize(); | |
1204 | if ( !(size == sizeBest) ) | |
1205 | { | |
1206 | // reset to default size if it had been made smaller | |
1207 | size = sizeBest; | |
1208 | ||
1209 | resize = TRUE; | |
1210 | } | |
1211 | ||
1212 | if ( size.x >= minSize || size.y >= minSize ) | |
1213 | { | |
1214 | // leave 1 pixel margin | |
1215 | size.x = size.y = minSize - 2; | |
1216 | ||
1217 | resize = TRUE; | |
1218 | } | |
1219 | ||
1220 | if ( resize ) | |
1221 | { | |
1222 | m_control->SetSize(size); | |
1223 | } | |
1224 | ||
508011ce | 1225 | // position it in the centre of the rectangle (TODO: support alignment?) |
508011ce | 1226 | |
b94ae1ea | 1227 | #if defined(__WXGTK__) || defined (__WXMOTIF__) |
508011ce VZ |
1228 | // the checkbox without label still has some space to the right in wxGTK, |
1229 | // so shift it to the right | |
b94ae1ea VZ |
1230 | size.x -= 8; |
1231 | #elif defined(__WXMSW__) | |
a95e38c0 VZ |
1232 | // here too, but in other way |
1233 | size.x += 1; | |
b94ae1ea VZ |
1234 | size.y -= 2; |
1235 | #endif | |
508011ce | 1236 | |
1bd71df9 JS |
1237 | int hAlign = wxALIGN_CENTRE; |
1238 | int vAlign = wxALIGN_CENTRE; | |
1239 | if (GetCellAttr()) | |
1240 | GetCellAttr()->GetAlignment(& hAlign, & vAlign); | |
52d6f640 | 1241 | |
1bd71df9 JS |
1242 | int x = 0, y = 0; |
1243 | if (hAlign == wxALIGN_LEFT) | |
1244 | { | |
1245 | x = r.x + 2; | |
1246 | #ifdef __WXMSW__ | |
1247 | x += 2; | |
52d6f640 | 1248 | #endif |
1bd71df9 JS |
1249 | y = r.y + r.height/2 - size.y/2; |
1250 | } | |
1251 | else if (hAlign == wxALIGN_RIGHT) | |
1252 | { | |
1253 | x = r.x + r.width - size.x - 2; | |
1254 | y = r.y + r.height/2 - size.y/2; | |
1255 | } | |
1256 | else if (hAlign == wxALIGN_CENTRE) | |
1257 | { | |
1258 | x = r.x + r.width/2 - size.x/2; | |
1259 | y = r.y + r.height/2 - size.y/2; | |
1260 | } | |
52d6f640 | 1261 | |
1bd71df9 | 1262 | m_control->Move(x, y); |
508011ce VZ |
1263 | } |
1264 | ||
1265 | void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr) | |
1266 | { | |
99306db2 VZ |
1267 | m_control->Show(show); |
1268 | ||
189d0213 | 1269 | if ( show ) |
508011ce | 1270 | { |
189d0213 VZ |
1271 | wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY; |
1272 | CBox()->SetBackgroundColour(colBg); | |
508011ce | 1273 | } |
508011ce VZ |
1274 | } |
1275 | ||
1276 | void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1277 | { | |
1278 | wxASSERT_MSG(m_control, | |
1279 | wxT("The wxGridCellEditor must be Created first!")); | |
1280 | ||
28a77bc4 | 1281 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) |
f2d76237 RD |
1282 | m_startValue = grid->GetTable()->GetValueAsBool(row, col); |
1283 | else | |
695a3263 MB |
1284 | { |
1285 | wxString cellval( grid->GetTable()->GetValue(row, col) ); | |
8dd8f875 | 1286 | m_startValue = !( !cellval || (cellval == wxT("0")) ); |
695a3263 | 1287 | } |
508011ce VZ |
1288 | CBox()->SetValue(m_startValue); |
1289 | CBox()->SetFocus(); | |
1290 | } | |
1291 | ||
1292 | bool wxGridCellBoolEditor::EndEdit(int row, int col, | |
508011ce VZ |
1293 | wxGrid* grid) |
1294 | { | |
1295 | wxASSERT_MSG(m_control, | |
1296 | wxT("The wxGridCellEditor must be Created first!")); | |
1297 | ||
1298 | bool changed = FALSE; | |
1299 | bool value = CBox()->GetValue(); | |
1300 | if ( value != m_startValue ) | |
1301 | changed = TRUE; | |
1302 | ||
1303 | if ( changed ) | |
1304 | { | |
28a77bc4 | 1305 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) |
f2d76237 RD |
1306 | grid->GetTable()->SetValueAsBool(row, col, value); |
1307 | else | |
1308 | grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString); | |
508011ce VZ |
1309 | } |
1310 | ||
1311 | return changed; | |
1312 | } | |
1313 | ||
1314 | void wxGridCellBoolEditor::Reset() | |
1315 | { | |
1316 | wxASSERT_MSG(m_control, | |
1317 | wxT("The wxGridCellEditor must be Created first!")); | |
1318 | ||
1319 | CBox()->SetValue(m_startValue); | |
1320 | } | |
1321 | ||
e195a54c | 1322 | void wxGridCellBoolEditor::StartingClick() |
508011ce | 1323 | { |
e195a54c | 1324 | CBox()->SetValue(!CBox()->GetValue()); |
508011ce VZ |
1325 | } |
1326 | ||
f6bcfd97 BP |
1327 | bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event) |
1328 | { | |
1329 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
1330 | { | |
1331 | int keycode = event.GetKeyCode(); | |
1332 | switch ( keycode ) | |
1333 | { | |
1334 | case WXK_MULTIPLY: | |
1335 | case WXK_NUMPAD_MULTIPLY: | |
1336 | case WXK_ADD: | |
1337 | case WXK_NUMPAD_ADD: | |
1338 | case WXK_SUBTRACT: | |
1339 | case WXK_NUMPAD_SUBTRACT: | |
1340 | case WXK_SPACE: | |
1341 | case '+': | |
1342 | case '-': | |
1343 | return TRUE; | |
1344 | } | |
1345 | } | |
1346 | ||
1347 | return FALSE; | |
1348 | } | |
04418332 | 1349 | |
73145b0e JS |
1350 | // return the value as "1" for true and the empty string for false |
1351 | wxString wxGridCellBoolEditor::GetValue() const | |
1352 | { | |
1353 | bool bSet = CBox()->GetValue(); | |
04418332 | 1354 | return bSet ? _T("1") : wxEmptyString; |
73145b0e | 1355 | } |
f6bcfd97 | 1356 | |
3a8c693a VZ |
1357 | #endif // wxUSE_CHECKBOX |
1358 | ||
1359 | #if wxUSE_COMBOBOX | |
1360 | ||
4ee5fc9c VZ |
1361 | // ---------------------------------------------------------------------------- |
1362 | // wxGridCellChoiceEditor | |
1363 | // ---------------------------------------------------------------------------- | |
1364 | ||
1365 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count, | |
f6bcfd97 | 1366 | const wxString choices[], |
4ee5fc9c VZ |
1367 | bool allowOthers) |
1368 | : m_allowOthers(allowOthers) | |
1369 | { | |
c4608a8a | 1370 | if ( count ) |
4ee5fc9c | 1371 | { |
c4608a8a VZ |
1372 | m_choices.Alloc(count); |
1373 | for ( size_t n = 0; n < count; n++ ) | |
1374 | { | |
1375 | m_choices.Add(choices[n]); | |
1376 | } | |
4ee5fc9c VZ |
1377 | } |
1378 | } | |
1379 | ||
c4608a8a VZ |
1380 | wxGridCellEditor *wxGridCellChoiceEditor::Clone() const |
1381 | { | |
1382 | wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor; | |
1383 | editor->m_allowOthers = m_allowOthers; | |
1384 | editor->m_choices = m_choices; | |
1385 | ||
1386 | return editor; | |
1387 | } | |
1388 | ||
4ee5fc9c VZ |
1389 | void wxGridCellChoiceEditor::Create(wxWindow* parent, |
1390 | wxWindowID id, | |
1391 | wxEvtHandler* evtHandler) | |
1392 | { | |
1393 | size_t count = m_choices.GetCount(); | |
1394 | wxString *choices = new wxString[count]; | |
1395 | for ( size_t n = 0; n < count; n++ ) | |
1396 | { | |
1397 | choices[n] = m_choices[n]; | |
1398 | } | |
1399 | ||
1400 | m_control = new wxComboBox(parent, id, wxEmptyString, | |
1401 | wxDefaultPosition, wxDefaultSize, | |
1402 | count, choices, | |
1403 | m_allowOthers ? 0 : wxCB_READONLY); | |
1404 | ||
1405 | delete [] choices; | |
1406 | ||
1407 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1408 | } | |
1409 | ||
a5777624 RD |
1410 | void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell, |
1411 | wxGridCellAttr * attr) | |
4ee5fc9c VZ |
1412 | { |
1413 | // as we fill the entire client area, don't do anything here to minimize | |
1414 | // flicker | |
a5777624 RD |
1415 | |
1416 | // TODO: It doesn't actually fill the client area since the height of a | |
1417 | // combo always defaults to the standard... Until someone has time to | |
1418 | // figure out the right rectangle to paint, just do it the normal way... | |
1419 | wxGridCellEditor::PaintBackground(rectCell, attr); | |
4ee5fc9c VZ |
1420 | } |
1421 | ||
1422 | void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1423 | { | |
1424 | wxASSERT_MSG(m_control, | |
1425 | wxT("The wxGridCellEditor must be Created first!")); | |
1426 | ||
1427 | m_startValue = grid->GetTable()->GetValue(row, col); | |
1428 | ||
2b5f62a0 VZ |
1429 | if (m_allowOthers) |
1430 | Combo()->SetValue(m_startValue); | |
1431 | else | |
28a77bc4 | 1432 | { |
2b5f62a0 VZ |
1433 | // find the right position, or default to the first if not found |
1434 | int pos = Combo()->FindString(m_startValue); | |
1435 | if (pos == -1) | |
1436 | pos = 0; | |
1437 | Combo()->SetSelection(pos); | |
28a77bc4 | 1438 | } |
4ee5fc9c VZ |
1439 | Combo()->SetInsertionPointEnd(); |
1440 | Combo()->SetFocus(); | |
1441 | } | |
1442 | ||
28a77bc4 | 1443 | bool wxGridCellChoiceEditor::EndEdit(int row, int col, |
4ee5fc9c VZ |
1444 | wxGrid* grid) |
1445 | { | |
1446 | wxString value = Combo()->GetValue(); | |
1447 | bool changed = value != m_startValue; | |
1448 | ||
1449 | if ( changed ) | |
1450 | grid->GetTable()->SetValue(row, col, value); | |
1451 | ||
1452 | m_startValue = wxEmptyString; | |
2b5f62a0 VZ |
1453 | if (m_allowOthers) |
1454 | Combo()->SetValue(m_startValue); | |
1455 | else | |
1456 | Combo()->SetSelection(0); | |
4ee5fc9c VZ |
1457 | |
1458 | return changed; | |
1459 | } | |
1460 | ||
1461 | void wxGridCellChoiceEditor::Reset() | |
1462 | { | |
1463 | Combo()->SetValue(m_startValue); | |
1464 | Combo()->SetInsertionPointEnd(); | |
1465 | } | |
1466 | ||
c4608a8a VZ |
1467 | void wxGridCellChoiceEditor::SetParameters(const wxString& params) |
1468 | { | |
1469 | if ( !params ) | |
1470 | { | |
1471 | // what can we do? | |
1472 | return; | |
1473 | } | |
1474 | ||
1475 | m_choices.Empty(); | |
1476 | ||
1477 | wxStringTokenizer tk(params, _T(',')); | |
1478 | while ( tk.HasMoreTokens() ) | |
1479 | { | |
1480 | m_choices.Add(tk.GetNextToken()); | |
1481 | } | |
1482 | } | |
1483 | ||
73145b0e JS |
1484 | // return the value in the text control |
1485 | wxString wxGridCellChoiceEditor::GetValue() const | |
1486 | { | |
1487 | return Combo()->GetValue(); | |
1488 | } | |
04418332 | 1489 | |
3a8c693a VZ |
1490 | #endif // wxUSE_COMBOBOX |
1491 | ||
508011ce VZ |
1492 | // ---------------------------------------------------------------------------- |
1493 | // wxGridCellEditorEvtHandler | |
1494 | // ---------------------------------------------------------------------------- | |
2796cce3 RD |
1495 | |
1496 | void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event) | |
1497 | { | |
12a3f227 | 1498 | switch ( event.GetKeyCode() ) |
2796cce3 RD |
1499 | { |
1500 | case WXK_ESCAPE: | |
1501 | m_editor->Reset(); | |
b54ba671 | 1502 | m_grid->DisableCellEditControl(); |
2796cce3 RD |
1503 | break; |
1504 | ||
2c9a89e0 | 1505 | case WXK_TAB: |
b51c3f27 | 1506 | m_grid->GetEventHandler()->ProcessEvent( event ); |
9b4aede2 RD |
1507 | break; |
1508 | ||
2796cce3 | 1509 | case WXK_RETURN: |
faec5a43 SN |
1510 | case WXK_NUMPAD_ENTER: |
1511 | if (!m_grid->GetEventHandler()->ProcessEvent(event)) | |
2796cce3 RD |
1512 | m_editor->HandleReturn(event); |
1513 | break; | |
1514 | ||
2796cce3 RD |
1515 | |
1516 | default: | |
1517 | event.Skip(); | |
1518 | } | |
1519 | } | |
1520 | ||
fb0de762 RD |
1521 | void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event) |
1522 | { | |
12a3f227 | 1523 | switch ( event.GetKeyCode() ) |
fb0de762 RD |
1524 | { |
1525 | case WXK_ESCAPE: | |
1526 | case WXK_TAB: | |
1527 | case WXK_RETURN: | |
a4f7bf58 | 1528 | case WXK_NUMPAD_ENTER: |
fb0de762 RD |
1529 | break; |
1530 | ||
1531 | default: | |
1532 | event.Skip(); | |
1533 | } | |
1534 | } | |
1535 | ||
c4608a8a VZ |
1536 | // ---------------------------------------------------------------------------- |
1537 | // wxGridCellWorker is an (almost) empty common base class for | |
1538 | // wxGridCellRenderer and wxGridCellEditor managing ref counting | |
1539 | // ---------------------------------------------------------------------------- | |
1540 | ||
1541 | void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params)) | |
1542 | { | |
1543 | // nothing to do | |
1544 | } | |
1545 | ||
1546 | wxGridCellWorker::~wxGridCellWorker() | |
1547 | { | |
1548 | } | |
1549 | ||
508011ce VZ |
1550 | // ============================================================================ |
1551 | // renderer classes | |
1552 | // ============================================================================ | |
1553 | ||
ab79958a VZ |
1554 | // ---------------------------------------------------------------------------- |
1555 | // wxGridCellRenderer | |
1556 | // ---------------------------------------------------------------------------- | |
1557 | ||
1558 | void wxGridCellRenderer::Draw(wxGrid& grid, | |
2796cce3 | 1559 | wxGridCellAttr& attr, |
ab79958a VZ |
1560 | wxDC& dc, |
1561 | const wxRect& rect, | |
c78b3acd | 1562 | int WXUNUSED(row), int WXUNUSED(col), |
ab79958a VZ |
1563 | bool isSelected) |
1564 | { | |
1565 | dc.SetBackgroundMode( wxSOLID ); | |
1566 | ||
04418332 | 1567 | // grey out fields if the grid is disabled |
73145b0e | 1568 | if( grid.IsEnabled() ) |
ab79958a | 1569 | { |
73145b0e JS |
1570 | if ( isSelected ) |
1571 | { | |
1572 | dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) ); | |
1573 | } | |
1574 | else | |
1575 | { | |
1576 | dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) ); | |
1577 | } | |
52d6f640 | 1578 | } |
ab79958a VZ |
1579 | else |
1580 | { | |
73145b0e | 1581 | dc.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE), wxSOLID)); |
ab79958a VZ |
1582 | } |
1583 | ||
1584 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
ab79958a VZ |
1585 | dc.DrawRectangle(rect); |
1586 | } | |
1587 | ||
508011ce VZ |
1588 | // ---------------------------------------------------------------------------- |
1589 | // wxGridCellStringRenderer | |
1590 | // ---------------------------------------------------------------------------- | |
1591 | ||
816be743 VZ |
1592 | void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid, |
1593 | wxGridCellAttr& attr, | |
1594 | wxDC& dc, | |
1595 | bool isSelected) | |
ab79958a | 1596 | { |
ab79958a VZ |
1597 | dc.SetBackgroundMode( wxTRANSPARENT ); |
1598 | ||
283b7808 VZ |
1599 | // TODO some special colours for attr.IsReadOnly() case? |
1600 | ||
04418332 | 1601 | // different coloured text when the grid is disabled |
73145b0e | 1602 | if( grid.IsEnabled() ) |
ab79958a | 1603 | { |
73145b0e JS |
1604 | if ( isSelected ) |
1605 | { | |
1606 | dc.SetTextBackground( grid.GetSelectionBackground() ); | |
1607 | dc.SetTextForeground( grid.GetSelectionForeground() ); | |
1608 | } | |
1609 | else | |
1610 | { | |
1611 | dc.SetTextBackground( attr.GetBackgroundColour() ); | |
1612 | dc.SetTextForeground( attr.GetTextColour() ); | |
1613 | } | |
ab79958a VZ |
1614 | } |
1615 | else | |
1616 | { | |
73145b0e JS |
1617 | dc.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE)); |
1618 | dc.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT)); | |
ab79958a | 1619 | } |
816be743 | 1620 | |
2796cce3 | 1621 | dc.SetFont( attr.GetFont() ); |
816be743 VZ |
1622 | } |
1623 | ||
65e4e78e VZ |
1624 | wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr, |
1625 | wxDC& dc, | |
1626 | const wxString& text) | |
1627 | { | |
f6bcfd97 | 1628 | wxCoord x = 0, y = 0, max_x = 0; |
65e4e78e | 1629 | dc.SetFont(attr.GetFont()); |
f6bcfd97 BP |
1630 | wxStringTokenizer tk(text, _T('\n')); |
1631 | while ( tk.HasMoreTokens() ) | |
1632 | { | |
1633 | dc.GetTextExtent(tk.GetNextToken(), &x, &y); | |
1634 | max_x = wxMax(max_x, x); | |
1635 | } | |
1636 | ||
1637 | y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines. | |
65e4e78e | 1638 | |
f6bcfd97 | 1639 | return wxSize(max_x, y); |
65e4e78e VZ |
1640 | } |
1641 | ||
1642 | wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid, | |
1643 | wxGridCellAttr& attr, | |
1644 | wxDC& dc, | |
1645 | int row, int col) | |
1646 | { | |
1647 | return DoGetBestSize(attr, dc, grid.GetCellValue(row, col)); | |
1648 | } | |
1649 | ||
816be743 VZ |
1650 | void wxGridCellStringRenderer::Draw(wxGrid& grid, |
1651 | wxGridCellAttr& attr, | |
1652 | wxDC& dc, | |
1653 | const wxRect& rectCell, | |
1654 | int row, int col, | |
1655 | bool isSelected) | |
1656 | { | |
27f35b66 | 1657 | wxRect rect = rectCell; |
dc1f566f SN |
1658 | rect.Inflate(-1); |
1659 | ||
1660 | // erase only this cells background, overflow cells should have been erased | |
1661 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1662 | ||
1663 | int hAlign, vAlign; | |
1664 | attr.GetAlignment(&hAlign, &vAlign); | |
27f35b66 | 1665 | |
39b80349 SN |
1666 | int overflowCols = 0; |
1667 | ||
27f35b66 SN |
1668 | if (attr.GetOverflow()) |
1669 | { | |
1670 | int cols = grid.GetNumberCols(); | |
1671 | int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth(); | |
1672 | int cell_rows, cell_cols; | |
1673 | attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <=0 | |
1674 | if ((best_width > rectCell.width) && (col < cols) && grid.GetTable()) | |
1675 | { | |
1676 | int i, c_cols, c_rows; | |
1677 | for (i = col+cell_cols; i < cols; i++) | |
1678 | { | |
2b5f62a0 | 1679 | bool is_empty = TRUE; |
39b80349 | 1680 | for (int j=row; j<row+cell_rows; j++) |
27f35b66 | 1681 | { |
39b80349 | 1682 | // check w/ anchor cell for multicell block |
ef4d6ce8 | 1683 | grid.GetCellSize(j, i, &c_rows, &c_cols); |
39b80349 | 1684 | if (c_rows > 0) c_rows = 0; |
ef4d6ce8 | 1685 | if (!grid.GetTable()->IsEmptyCell(j+c_rows, i)) |
39b80349 | 1686 | { |
2b5f62a0 | 1687 | is_empty = FALSE; |
ef4d6ce8 | 1688 | break; |
39b80349 | 1689 | } |
27f35b66 | 1690 | } |
39b80349 SN |
1691 | if (is_empty) |
1692 | rect.width += grid.GetColSize(i); | |
dc1f566f SN |
1693 | else |
1694 | { | |
1695 | i--; | |
1696 | break; | |
1697 | } | |
39b80349 | 1698 | if (rect.width >= best_width) break; |
2b5f62a0 | 1699 | } |
39b80349 | 1700 | overflowCols = i - col - cell_cols + 1; |
2b5f62a0 | 1701 | if (overflowCols >= cols) overflowCols = cols - 1; |
39b80349 | 1702 | } |
27f35b66 | 1703 | |
dc1f566f SN |
1704 | if (overflowCols > 0) // redraw overflow cells w/ proper hilight |
1705 | { | |
39b80349 | 1706 | hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned |
2b5f62a0 | 1707 | wxRect clip = rect; |
39b80349 | 1708 | clip.x += rectCell.width; |
dc1f566f SN |
1709 | // draw each overflow cell individually |
1710 | int col_end = col+cell_cols+overflowCols; | |
1711 | if (col_end >= grid.GetNumberCols()) | |
2b5f62a0 | 1712 | col_end = grid.GetNumberCols() - 1; |
dc1f566f SN |
1713 | for (int i = col+cell_cols; i <= col_end; i++) |
1714 | { | |
dc1f566f SN |
1715 | clip.width = grid.GetColSize(i) - 1; |
1716 | dc.DestroyClippingRegion(); | |
1717 | dc.SetClippingRegion(clip); | |
39b80349 SN |
1718 | |
1719 | SetTextColoursAndFont(grid, attr, dc, | |
2b5f62a0 | 1720 | grid.IsInSelection(row,i)); |
39b80349 | 1721 | |
dc1f566f | 1722 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), |
2b5f62a0 | 1723 | rect, hAlign, vAlign); |
39b80349 | 1724 | clip.x += grid.GetColSize(i) - 1; |
dc1f566f | 1725 | } |
ab79958a | 1726 | |
39b80349 | 1727 | rect = rectCell; |
2b5f62a0 | 1728 | rect.Inflate(-1); |
dc1f566f | 1729 | rect.width++; |
39b80349 | 1730 | dc.DestroyClippingRegion(); |
dc1f566f | 1731 | } |
39b80349 | 1732 | } |
ab79958a | 1733 | |
dc1f566f SN |
1734 | // now we only have to draw the text |
1735 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
39b80349 | 1736 | |
ab79958a VZ |
1737 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), |
1738 | rect, hAlign, vAlign); | |
1739 | } | |
1740 | ||
65e4e78e VZ |
1741 | // ---------------------------------------------------------------------------- |
1742 | // wxGridCellNumberRenderer | |
1743 | // ---------------------------------------------------------------------------- | |
1744 | ||
1745 | wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col) | |
1746 | { | |
1747 | wxGridTableBase *table = grid.GetTable(); | |
1748 | wxString text; | |
1749 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
1750 | { | |
1751 | text.Printf(_T("%ld"), table->GetValueAsLong(row, col)); | |
1752 | } | |
9c4ba614 VZ |
1753 | else |
1754 | { | |
1755 | text = table->GetValue(row, col); | |
1756 | } | |
65e4e78e VZ |
1757 | |
1758 | return text; | |
1759 | } | |
1760 | ||
816be743 VZ |
1761 | void wxGridCellNumberRenderer::Draw(wxGrid& grid, |
1762 | wxGridCellAttr& attr, | |
1763 | wxDC& dc, | |
1764 | const wxRect& rectCell, | |
1765 | int row, int col, | |
1766 | bool isSelected) | |
1767 | { | |
1768 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1769 | ||
1770 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1771 | ||
1772 | // draw the text right aligned by default | |
1773 | int hAlign, vAlign; | |
1774 | attr.GetAlignment(&hAlign, &vAlign); | |
4c7277db | 1775 | hAlign = wxALIGN_RIGHT; |
816be743 VZ |
1776 | |
1777 | wxRect rect = rectCell; | |
1778 | rect.Inflate(-1); | |
1779 | ||
65e4e78e VZ |
1780 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
1781 | } | |
816be743 | 1782 | |
65e4e78e VZ |
1783 | wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid, |
1784 | wxGridCellAttr& attr, | |
1785 | wxDC& dc, | |
1786 | int row, int col) | |
1787 | { | |
1788 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
816be743 VZ |
1789 | } |
1790 | ||
1791 | // ---------------------------------------------------------------------------- | |
1792 | // wxGridCellFloatRenderer | |
1793 | // ---------------------------------------------------------------------------- | |
1794 | ||
1795 | wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision) | |
1796 | { | |
1797 | SetWidth(width); | |
1798 | SetPrecision(precision); | |
1799 | } | |
1800 | ||
e72b4213 VZ |
1801 | wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const |
1802 | { | |
1803 | wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer; | |
1804 | renderer->m_width = m_width; | |
1805 | renderer->m_precision = m_precision; | |
1806 | renderer->m_format = m_format; | |
1807 | ||
1808 | return renderer; | |
1809 | } | |
1810 | ||
65e4e78e VZ |
1811 | wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col) |
1812 | { | |
1813 | wxGridTableBase *table = grid.GetTable(); | |
0b190b0f VZ |
1814 | |
1815 | bool hasDouble; | |
1816 | double val; | |
65e4e78e VZ |
1817 | wxString text; |
1818 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
1819 | { | |
0b190b0f VZ |
1820 | val = table->GetValueAsDouble(row, col); |
1821 | hasDouble = TRUE; | |
65e4e78e | 1822 | } |
9c4ba614 VZ |
1823 | else |
1824 | { | |
1825 | text = table->GetValue(row, col); | |
0b190b0f | 1826 | hasDouble = text.ToDouble(&val); |
9c4ba614 | 1827 | } |
65e4e78e | 1828 | |
0b190b0f VZ |
1829 | if ( hasDouble ) |
1830 | { | |
1831 | if ( !m_format ) | |
1832 | { | |
1833 | if ( m_width == -1 ) | |
1834 | { | |
19d7140e VZ |
1835 | if ( m_precision == -1 ) |
1836 | { | |
2b5f62a0 VZ |
1837 | // default width/precision |
1838 | m_format = _T("%f"); | |
1839 | } | |
19d7140e VZ |
1840 | else |
1841 | { | |
1842 | m_format.Printf(_T("%%.%df"), m_precision); | |
1843 | } | |
0b190b0f VZ |
1844 | } |
1845 | else if ( m_precision == -1 ) | |
1846 | { | |
1847 | // default precision | |
1848 | m_format.Printf(_T("%%%d.f"), m_width); | |
1849 | } | |
1850 | else | |
1851 | { | |
1852 | m_format.Printf(_T("%%%d.%df"), m_width, m_precision); | |
1853 | } | |
1854 | } | |
1855 | ||
1856 | text.Printf(m_format, val); | |
19d7140e | 1857 | |
0b190b0f VZ |
1858 | } |
1859 | //else: text already contains the string | |
1860 | ||
65e4e78e VZ |
1861 | return text; |
1862 | } | |
1863 | ||
816be743 VZ |
1864 | void wxGridCellFloatRenderer::Draw(wxGrid& grid, |
1865 | wxGridCellAttr& attr, | |
1866 | wxDC& dc, | |
1867 | const wxRect& rectCell, | |
1868 | int row, int col, | |
1869 | bool isSelected) | |
1870 | { | |
1871 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1872 | ||
1873 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1874 | ||
1875 | // draw the text right aligned by default | |
1876 | int hAlign, vAlign; | |
1877 | attr.GetAlignment(&hAlign, &vAlign); | |
4c7277db | 1878 | hAlign = wxALIGN_RIGHT; |
816be743 VZ |
1879 | |
1880 | wxRect rect = rectCell; | |
1881 | rect.Inflate(-1); | |
1882 | ||
65e4e78e VZ |
1883 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
1884 | } | |
816be743 | 1885 | |
65e4e78e VZ |
1886 | wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid, |
1887 | wxGridCellAttr& attr, | |
1888 | wxDC& dc, | |
1889 | int row, int col) | |
1890 | { | |
1891 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
816be743 VZ |
1892 | } |
1893 | ||
0b190b0f VZ |
1894 | void wxGridCellFloatRenderer::SetParameters(const wxString& params) |
1895 | { | |
0b190b0f VZ |
1896 | if ( !params ) |
1897 | { | |
1898 | // reset to defaults | |
1899 | SetWidth(-1); | |
1900 | SetPrecision(-1); | |
1901 | } | |
1902 | else | |
1903 | { | |
1904 | wxString tmp = params.BeforeFirst(_T(',')); | |
1905 | if ( !!tmp ) | |
1906 | { | |
1907 | long width; | |
19d7140e | 1908 | if ( tmp.ToLong(&width) ) |
0b190b0f | 1909 | { |
19d7140e | 1910 | SetWidth((int)width); |
0b190b0f VZ |
1911 | } |
1912 | else | |
1913 | { | |
19d7140e VZ |
1914 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str()); |
1915 | } | |
0b190b0f | 1916 | |
19d7140e | 1917 | } |
0b190b0f VZ |
1918 | tmp = params.AfterFirst(_T(',')); |
1919 | if ( !!tmp ) | |
1920 | { | |
1921 | long precision; | |
19d7140e | 1922 | if ( tmp.ToLong(&precision) ) |
0b190b0f | 1923 | { |
19d7140e | 1924 | SetPrecision((int)precision); |
0b190b0f VZ |
1925 | } |
1926 | else | |
1927 | { | |
19d7140e | 1928 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str()); |
0b190b0f VZ |
1929 | } |
1930 | ||
0b190b0f VZ |
1931 | } |
1932 | } | |
1933 | } | |
1934 | ||
19d7140e | 1935 | |
508011ce VZ |
1936 | // ---------------------------------------------------------------------------- |
1937 | // wxGridCellBoolRenderer | |
1938 | // ---------------------------------------------------------------------------- | |
1939 | ||
65e4e78e | 1940 | wxSize wxGridCellBoolRenderer::ms_sizeCheckMark; |
508011ce | 1941 | |
b94ae1ea VZ |
1942 | // FIXME these checkbox size calculations are really ugly... |
1943 | ||
65e4e78e | 1944 | // between checkmark and box |
a95e38c0 | 1945 | static const wxCoord wxGRID_CHECKMARK_MARGIN = 2; |
508011ce | 1946 | |
65e4e78e VZ |
1947 | wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid, |
1948 | wxGridCellAttr& WXUNUSED(attr), | |
1949 | wxDC& WXUNUSED(dc), | |
1950 | int WXUNUSED(row), | |
1951 | int WXUNUSED(col)) | |
1952 | { | |
1953 | // compute it only once (no locks for MT safeness in GUI thread...) | |
1954 | if ( !ms_sizeCheckMark.x ) | |
297da4ba | 1955 | { |
65e4e78e VZ |
1956 | // get checkbox size |
1957 | wxCoord checkSize = 0; | |
297da4ba VZ |
1958 | wxCheckBox *checkbox = new wxCheckBox(&grid, -1, wxEmptyString); |
1959 | wxSize size = checkbox->GetBestSize(); | |
a95e38c0 | 1960 | checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN; |
297da4ba | 1961 | |
65e4e78e | 1962 | // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result |
69d8f612 | 1963 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
65e4e78e | 1964 | checkSize -= size.y / 2; |
297da4ba VZ |
1965 | #endif |
1966 | ||
1967 | delete checkbox; | |
65e4e78e VZ |
1968 | |
1969 | ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize; | |
297da4ba VZ |
1970 | } |
1971 | ||
65e4e78e VZ |
1972 | return ms_sizeCheckMark; |
1973 | } | |
1974 | ||
1975 | void wxGridCellBoolRenderer::Draw(wxGrid& grid, | |
1976 | wxGridCellAttr& attr, | |
1977 | wxDC& dc, | |
1978 | const wxRect& rect, | |
1979 | int row, int col, | |
1980 | bool isSelected) | |
1981 | { | |
1982 | wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); | |
1983 | ||
297da4ba | 1984 | // draw a check mark in the centre (ignoring alignment - TODO) |
65e4e78e | 1985 | wxSize size = GetBestSize(grid, attr, dc, row, col); |
b94ae1ea VZ |
1986 | |
1987 | // don't draw outside the cell | |
1988 | wxCoord minSize = wxMin(rect.width, rect.height); | |
1989 | if ( size.x >= minSize || size.y >= minSize ) | |
1990 | { | |
1991 | // and even leave (at least) 1 pixel margin | |
1992 | size.x = size.y = minSize - 2; | |
1993 | } | |
1994 | ||
1995 | // draw a border around checkmark | |
1bd71df9 JS |
1996 | int vAlign, hAlign; |
1997 | attr.GetAlignment(& hAlign, &vAlign); | |
52d6f640 | 1998 | |
a95e38c0 | 1999 | wxRect rectBorder; |
1bd71df9 JS |
2000 | if (hAlign == wxALIGN_CENTRE) |
2001 | { | |
2002 | rectBorder.x = rect.x + rect.width/2 - size.x/2; | |
2003 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2004 | rectBorder.width = size.x; | |
2005 | rectBorder.height = size.y; | |
2006 | } | |
2007 | else if (hAlign == wxALIGN_LEFT) | |
2008 | { | |
2009 | rectBorder.x = rect.x + 2; | |
2010 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2011 | rectBorder.width = size.x; | |
52d6f640 | 2012 | rectBorder.height = size.y; |
1bd71df9 JS |
2013 | } |
2014 | else if (hAlign == wxALIGN_RIGHT) | |
2015 | { | |
2016 | rectBorder.x = rect.x + rect.width - size.x - 2; | |
2017 | rectBorder.y = rect.y + rect.height/2 - size.y/2; | |
2018 | rectBorder.width = size.x; | |
52d6f640 | 2019 | rectBorder.height = size.y; |
1bd71df9 | 2020 | } |
b94ae1ea | 2021 | |
f2d76237 | 2022 | bool value; |
b94ae1ea | 2023 | if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) |
f2d76237 RD |
2024 | value = grid.GetTable()->GetValueAsBool(row, col); |
2025 | else | |
695a3263 MB |
2026 | { |
2027 | wxString cellval( grid.GetTable()->GetValue(row, col) ); | |
8dd8f875 | 2028 | value = !( !cellval || (cellval == wxT("0")) ); |
695a3263 | 2029 | } |
f2d76237 RD |
2030 | |
2031 | if ( value ) | |
508011ce | 2032 | { |
a95e38c0 VZ |
2033 | wxRect rectMark = rectBorder; |
2034 | #ifdef __WXMSW__ | |
2035 | // MSW DrawCheckMark() is weird (and should probably be changed...) | |
2036 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2); | |
2037 | rectMark.x++; | |
2038 | rectMark.y++; | |
2039 | #else // !MSW | |
2040 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN); | |
2041 | #endif // MSW/!MSW | |
2042 | ||
508011ce VZ |
2043 | dc.SetTextForeground(attr.GetTextColour()); |
2044 | dc.DrawCheckMark(rectMark); | |
2045 | } | |
a95e38c0 VZ |
2046 | |
2047 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
2048 | dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID)); | |
2049 | dc.DrawRectangle(rectBorder); | |
508011ce VZ |
2050 | } |
2051 | ||
2796cce3 RD |
2052 | // ---------------------------------------------------------------------------- |
2053 | // wxGridCellAttr | |
2054 | // ---------------------------------------------------------------------------- | |
2055 | ||
1df4050d VZ |
2056 | void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) |
2057 | { | |
2058 | m_nRef = 1; | |
2059 | ||
2060 | m_isReadOnly = Unset; | |
2061 | ||
2062 | m_renderer = NULL; | |
2063 | m_editor = NULL; | |
2064 | ||
2065 | m_attrkind = wxGridCellAttr::Cell; | |
2066 | ||
27f35b66 SN |
2067 | m_sizeRows = m_sizeCols = 1; |
2068 | m_overflow = TRUE; | |
2069 | ||
1df4050d VZ |
2070 | SetDefAttr(attrDefault); |
2071 | } | |
2072 | ||
39bcce60 | 2073 | wxGridCellAttr *wxGridCellAttr::Clone() const |
a68c1246 | 2074 | { |
1df4050d VZ |
2075 | wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr); |
2076 | ||
a68c1246 VZ |
2077 | if ( HasTextColour() ) |
2078 | attr->SetTextColour(GetTextColour()); | |
2079 | if ( HasBackgroundColour() ) | |
2080 | attr->SetBackgroundColour(GetBackgroundColour()); | |
2081 | if ( HasFont() ) | |
2082 | attr->SetFont(GetFont()); | |
2083 | if ( HasAlignment() ) | |
2084 | attr->SetAlignment(m_hAlign, m_vAlign); | |
2085 | ||
27f35b66 SN |
2086 | attr->SetSize( m_sizeRows, m_sizeCols ); |
2087 | ||
a68c1246 VZ |
2088 | if ( m_renderer ) |
2089 | { | |
2090 | attr->SetRenderer(m_renderer); | |
39bcce60 | 2091 | m_renderer->IncRef(); |
a68c1246 VZ |
2092 | } |
2093 | if ( m_editor ) | |
2094 | { | |
2095 | attr->SetEditor(m_editor); | |
39bcce60 | 2096 | m_editor->IncRef(); |
a68c1246 VZ |
2097 | } |
2098 | ||
2099 | if ( IsReadOnly() ) | |
2100 | attr->SetReadOnly(); | |
2101 | ||
19d7140e VZ |
2102 | attr->SetKind( m_attrkind ); |
2103 | ||
a68c1246 VZ |
2104 | return attr; |
2105 | } | |
2106 | ||
19d7140e VZ |
2107 | void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) |
2108 | { | |
2109 | if ( !HasTextColour() && mergefrom->HasTextColour() ) | |
2110 | SetTextColour(mergefrom->GetTextColour()); | |
2111 | if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() ) | |
2112 | SetBackgroundColour(mergefrom->GetBackgroundColour()); | |
2113 | if ( !HasFont() && mergefrom->HasFont() ) | |
2114 | SetFont(mergefrom->GetFont()); | |
7e48d7d9 | 2115 | if ( !HasAlignment() && mergefrom->HasAlignment() ){ |
19d7140e VZ |
2116 | int hAlign, vAlign; |
2117 | mergefrom->GetAlignment( &hAlign, &vAlign); | |
2118 | SetAlignment(hAlign, vAlign); | |
2119 | } | |
2120 | ||
27f35b66 SN |
2121 | mergefrom->GetSize( &m_sizeRows, &m_sizeCols ); |
2122 | ||
19d7140e VZ |
2123 | // Directly access member functions as GetRender/Editor don't just return |
2124 | // m_renderer/m_editor | |
2125 | // | |
2126 | // Maybe add support for merge of Render and Editor? | |
2127 | if (!HasRenderer() && mergefrom->HasRenderer() ) | |
bf7945ce | 2128 | { |
19d7140e VZ |
2129 | m_renderer = mergefrom->m_renderer; |
2130 | m_renderer->IncRef(); | |
2131 | } | |
2132 | if ( !HasEditor() && mergefrom->HasEditor() ) | |
2133 | { | |
2134 | m_editor = mergefrom->m_editor; | |
2135 | m_editor->IncRef(); | |
2136 | } | |
2137 | if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) | |
2138 | SetReadOnly(mergefrom->IsReadOnly()); | |
2139 | ||
2140 | SetDefAttr(mergefrom->m_defGridAttr); | |
2141 | } | |
2142 | ||
27f35b66 SN |
2143 | void wxGridCellAttr::SetSize(int num_rows, int num_cols) |
2144 | { | |
2145 | // The size of a cell is normally 1,1 | |
2146 | ||
2147 | // If this cell is larger (2,2) then this is the top left cell | |
2148 | // the other cells that will be covered (lower right cells) must be | |
2149 | // set to negative or zero values such that | |
2150 | // row + num_rows of the covered cell points to the larger cell (this cell) | |
2151 | // same goes for the col + num_cols. | |
2152 | ||
2153 | // Size of 0,0 is NOT valid, neither is <=0 and any positive value | |
2154 | ||
2155 | wxASSERT_MSG( (!((num_rows>0)&&(num_cols<=0)) || | |
2156 | !((num_rows<=0)&&(num_cols>0)) || | |
2157 | !((num_rows==0)&&(num_cols==0))), | |
2158 | wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values")); | |
2159 | ||
2160 | m_sizeRows = num_rows; | |
2161 | m_sizeCols = num_cols; | |
2162 | } | |
2163 | ||
2796cce3 RD |
2164 | const wxColour& wxGridCellAttr::GetTextColour() const |
2165 | { | |
2166 | if (HasTextColour()) | |
508011ce | 2167 | { |
2796cce3 | 2168 | return m_colText; |
508011ce | 2169 | } |
0926b2fc | 2170 | else if (m_defGridAttr && m_defGridAttr != this) |
508011ce | 2171 | { |
2796cce3 | 2172 | return m_defGridAttr->GetTextColour(); |
508011ce VZ |
2173 | } |
2174 | else | |
2175 | { | |
2796cce3 RD |
2176 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2177 | return wxNullColour; | |
2178 | } | |
2179 | } | |
2180 | ||
2181 | ||
2182 | const wxColour& wxGridCellAttr::GetBackgroundColour() const | |
2183 | { | |
2184 | if (HasBackgroundColour()) | |
2185 | return m_colBack; | |
0926b2fc | 2186 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2187 | return m_defGridAttr->GetBackgroundColour(); |
508011ce VZ |
2188 | else |
2189 | { | |
2796cce3 RD |
2190 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2191 | return wxNullColour; | |
2192 | } | |
2193 | } | |
2194 | ||
2195 | ||
2196 | const wxFont& wxGridCellAttr::GetFont() const | |
2197 | { | |
2198 | if (HasFont()) | |
2199 | return m_font; | |
0926b2fc | 2200 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2201 | return m_defGridAttr->GetFont(); |
508011ce VZ |
2202 | else |
2203 | { | |
2796cce3 RD |
2204 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2205 | return wxNullFont; | |
2206 | } | |
2207 | } | |
2208 | ||
2209 | ||
2210 | void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const | |
2211 | { | |
508011ce VZ |
2212 | if (HasAlignment()) |
2213 | { | |
2796cce3 RD |
2214 | if ( hAlign ) *hAlign = m_hAlign; |
2215 | if ( vAlign ) *vAlign = m_vAlign; | |
2216 | } | |
0926b2fc | 2217 | else if (m_defGridAttr && m_defGridAttr != this) |
2796cce3 | 2218 | m_defGridAttr->GetAlignment(hAlign, vAlign); |
508011ce VZ |
2219 | else |
2220 | { | |
2796cce3 RD |
2221 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
2222 | } | |
2223 | } | |
2224 | ||
27f35b66 SN |
2225 | void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const |
2226 | { | |
2227 | if ( num_rows ) *num_rows = m_sizeRows; | |
2228 | if ( num_cols ) *num_cols = m_sizeCols; | |
2229 | } | |
2796cce3 | 2230 | |
f2d76237 | 2231 | // GetRenderer and GetEditor use a slightly different decision path about |
28a77bc4 RD |
2232 | // which attribute to use. If a non-default attr object has one then it is |
2233 | // used, otherwise the default editor or renderer is fetched from the grid and | |
2234 | // used. It should be the default for the data type of the cell. If it is | |
2235 | // NULL (because the table has a type that the grid does not have in its | |
2236 | // registry,) then the grid's default editor or renderer is used. | |
2237 | ||
2238 | wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const | |
2239 | { | |
3cf883a2 | 2240 | wxGridCellRenderer *renderer; |
28a77bc4 | 2241 | |
3cf883a2 | 2242 | if ( m_renderer && this != m_defGridAttr ) |
0b190b0f | 2243 | { |
3cf883a2 VZ |
2244 | // use the cells renderer if it has one |
2245 | renderer = m_renderer; | |
2246 | renderer->IncRef(); | |
0b190b0f | 2247 | } |
3cf883a2 | 2248 | else // no non default cell renderer |
0b190b0f | 2249 | { |
3cf883a2 VZ |
2250 | // get default renderer for the data type |
2251 | if ( grid ) | |
2252 | { | |
2253 | // GetDefaultRendererForCell() will do IncRef() for us | |
2254 | renderer = grid->GetDefaultRendererForCell(row, col); | |
2255 | } | |
2256 | else | |
2257 | { | |
2258 | renderer = NULL; | |
2259 | } | |
0b190b0f | 2260 | |
3cf883a2 VZ |
2261 | if ( !renderer ) |
2262 | { | |
0926b2fc | 2263 | if (m_defGridAttr && this != m_defGridAttr ) |
3cf883a2 VZ |
2264 | { |
2265 | // if we still don't have one then use the grid default | |
2266 | // (no need for IncRef() here neither) | |
2267 | renderer = m_defGridAttr->GetRenderer(NULL, 0, 0); | |
2268 | } | |
2269 | else // default grid attr | |
2270 | { | |
2271 | // use m_renderer which we had decided not to use initially | |
2272 | renderer = m_renderer; | |
2273 | if ( renderer ) | |
2274 | renderer->IncRef(); | |
2275 | } | |
2276 | } | |
0b190b0f | 2277 | } |
28a77bc4 | 2278 | |
3cf883a2 VZ |
2279 | // we're supposed to always find something |
2280 | wxASSERT_MSG(renderer, wxT("Missing default cell renderer")); | |
28a77bc4 RD |
2281 | |
2282 | return renderer; | |
2796cce3 RD |
2283 | } |
2284 | ||
3cf883a2 | 2285 | // same as above, except for s/renderer/editor/g |
28a77bc4 | 2286 | wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const |
07296f0b | 2287 | { |
3cf883a2 | 2288 | wxGridCellEditor *editor; |
0b190b0f | 2289 | |
3cf883a2 | 2290 | if ( m_editor && this != m_defGridAttr ) |
0b190b0f | 2291 | { |
3cf883a2 VZ |
2292 | // use the cells editor if it has one |
2293 | editor = m_editor; | |
2294 | editor->IncRef(); | |
0b190b0f | 2295 | } |
3cf883a2 | 2296 | else // no non default cell editor |
0b190b0f | 2297 | { |
3cf883a2 VZ |
2298 | // get default editor for the data type |
2299 | if ( grid ) | |
2300 | { | |
2301 | // GetDefaultEditorForCell() will do IncRef() for us | |
2302 | editor = grid->GetDefaultEditorForCell(row, col); | |
2303 | } | |
2304 | else | |
2305 | { | |
2306 | editor = NULL; | |
2307 | } | |
2308 | ||
2309 | if ( !editor ) | |
2310 | { | |
0926b2fc | 2311 | if ( m_defGridAttr && this != m_defGridAttr ) |
3cf883a2 VZ |
2312 | { |
2313 | // if we still don't have one then use the grid default | |
2314 | // (no need for IncRef() here neither) | |
2315 | editor = m_defGridAttr->GetEditor(NULL, 0, 0); | |
2316 | } | |
2317 | else // default grid attr | |
2318 | { | |
2319 | // use m_editor which we had decided not to use initially | |
2320 | editor = m_editor; | |
2321 | if ( editor ) | |
2322 | editor->IncRef(); | |
2323 | } | |
2324 | } | |
0b190b0f | 2325 | } |
28a77bc4 | 2326 | |
3cf883a2 VZ |
2327 | // we're supposed to always find something |
2328 | wxASSERT_MSG(editor, wxT("Missing default cell editor")); | |
2329 | ||
28a77bc4 | 2330 | return editor; |
07296f0b RD |
2331 | } |
2332 | ||
b99be8fb | 2333 | // ---------------------------------------------------------------------------- |
758cbedf | 2334 | // wxGridCellAttrData |
b99be8fb VZ |
2335 | // ---------------------------------------------------------------------------- |
2336 | ||
758cbedf | 2337 | void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) |
b99be8fb VZ |
2338 | { |
2339 | int n = FindIndex(row, col); | |
2340 | if ( n == wxNOT_FOUND ) | |
2341 | { | |
2342 | // add the attribute | |
2343 | m_attrs.Add(new wxGridCellWithAttr(row, col, attr)); | |
2344 | } | |
2345 | else | |
2346 | { | |
6f36917b VZ |
2347 | // free the old attribute |
2348 | m_attrs[(size_t)n].attr->DecRef(); | |
2349 | ||
b99be8fb VZ |
2350 | if ( attr ) |
2351 | { | |
2352 | // change the attribute | |
2e9a6788 | 2353 | m_attrs[(size_t)n].attr = attr; |
b99be8fb VZ |
2354 | } |
2355 | else | |
2356 | { | |
2357 | // remove this attribute | |
2358 | m_attrs.RemoveAt((size_t)n); | |
2359 | } | |
2360 | } | |
b99be8fb VZ |
2361 | } |
2362 | ||
758cbedf | 2363 | wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const |
b99be8fb VZ |
2364 | { |
2365 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2366 | ||
2367 | int n = FindIndex(row, col); | |
2368 | if ( n != wxNOT_FOUND ) | |
2369 | { | |
2e9a6788 VZ |
2370 | attr = m_attrs[(size_t)n].attr; |
2371 | attr->IncRef(); | |
b99be8fb VZ |
2372 | } |
2373 | ||
2374 | return attr; | |
2375 | } | |
2376 | ||
4d60017a SN |
2377 | void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) |
2378 | { | |
2379 | size_t count = m_attrs.GetCount(); | |
2380 | for ( size_t n = 0; n < count; n++ ) | |
2381 | { | |
2382 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
2383 | wxCoord row = coords.GetRow(); |
2384 | if ((size_t)row >= pos) | |
2385 | { | |
2386 | if (numRows > 0) | |
2387 | { | |
2388 | // If rows inserted, include row counter where necessary | |
2389 | coords.SetRow(row + numRows); | |
2390 | } | |
2391 | else if (numRows < 0) | |
2392 | { | |
2393 | // If rows deleted ... | |
2394 | if ((size_t)row >= pos - numRows) | |
2395 | { | |
2396 | // ...either decrement row counter (if row still exists)... | |
2397 | coords.SetRow(row + numRows); | |
2398 | } | |
2399 | else | |
2400 | { | |
2401 | // ...or remove the attribute | |
2402 | m_attrs.RemoveAt((size_t)n); | |
2403 | n--; count--; | |
2404 | } | |
2405 | } | |
4d60017a SN |
2406 | } |
2407 | } | |
2408 | } | |
2409 | ||
2410 | void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) | |
2411 | { | |
2412 | size_t count = m_attrs.GetCount(); | |
2413 | for ( size_t n = 0; n < count; n++ ) | |
2414 | { | |
2415 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
2416 | wxCoord col = coords.GetCol(); |
2417 | if ( (size_t)col >= pos ) | |
2418 | { | |
2419 | if ( numCols > 0 ) | |
2420 | { | |
2421 | // If rows inserted, include row counter where necessary | |
2422 | coords.SetCol(col + numCols); | |
2423 | } | |
2424 | else if (numCols < 0) | |
2425 | { | |
2426 | // If rows deleted ... | |
2427 | if ((size_t)col >= pos - numCols) | |
2428 | { | |
2429 | // ...either decrement row counter (if row still exists)... | |
2430 | coords.SetCol(col + numCols); | |
2431 | } | |
2432 | else | |
2433 | { | |
2434 | // ...or remove the attribute | |
2435 | m_attrs.RemoveAt((size_t)n); | |
2436 | n--; count--; | |
2437 | } | |
2438 | } | |
4d60017a SN |
2439 | } |
2440 | } | |
2441 | } | |
2442 | ||
758cbedf | 2443 | int wxGridCellAttrData::FindIndex(int row, int col) const |
b99be8fb VZ |
2444 | { |
2445 | size_t count = m_attrs.GetCount(); | |
2446 | for ( size_t n = 0; n < count; n++ ) | |
2447 | { | |
2448 | const wxGridCellCoords& coords = m_attrs[n].coords; | |
2449 | if ( (coords.GetRow() == row) && (coords.GetCol() == col) ) | |
2450 | { | |
2451 | return n; | |
2452 | } | |
2453 | } | |
2454 | ||
2455 | return wxNOT_FOUND; | |
2456 | } | |
2457 | ||
758cbedf VZ |
2458 | // ---------------------------------------------------------------------------- |
2459 | // wxGridRowOrColAttrData | |
2460 | // ---------------------------------------------------------------------------- | |
2461 | ||
2462 | wxGridRowOrColAttrData::~wxGridRowOrColAttrData() | |
2463 | { | |
2464 | size_t count = m_attrs.Count(); | |
2465 | for ( size_t n = 0; n < count; n++ ) | |
2466 | { | |
2467 | m_attrs[n]->DecRef(); | |
2468 | } | |
2469 | } | |
2470 | ||
2471 | wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const | |
2472 | { | |
2473 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2474 | ||
2475 | int n = m_rowsOrCols.Index(rowOrCol); | |
2476 | if ( n != wxNOT_FOUND ) | |
2477 | { | |
2478 | attr = m_attrs[(size_t)n]; | |
2479 | attr->IncRef(); | |
2480 | } | |
2481 | ||
2482 | return attr; | |
2483 | } | |
2484 | ||
2485 | void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) | |
2486 | { | |
a95e38c0 VZ |
2487 | int i = m_rowsOrCols.Index(rowOrCol); |
2488 | if ( i == wxNOT_FOUND ) | |
758cbedf VZ |
2489 | { |
2490 | // add the attribute | |
2491 | m_rowsOrCols.Add(rowOrCol); | |
2492 | m_attrs.Add(attr); | |
2493 | } | |
2494 | else | |
2495 | { | |
a95e38c0 | 2496 | size_t n = (size_t)i; |
758cbedf VZ |
2497 | if ( attr ) |
2498 | { | |
2499 | // change the attribute | |
a95e38c0 VZ |
2500 | m_attrs[n]->DecRef(); |
2501 | m_attrs[n] = attr; | |
758cbedf VZ |
2502 | } |
2503 | else | |
2504 | { | |
2505 | // remove this attribute | |
a95e38c0 VZ |
2506 | m_attrs[n]->DecRef(); |
2507 | m_rowsOrCols.RemoveAt(n); | |
2508 | m_attrs.RemoveAt(n); | |
758cbedf VZ |
2509 | } |
2510 | } | |
2511 | } | |
2512 | ||
4d60017a SN |
2513 | void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) |
2514 | { | |
2515 | size_t count = m_attrs.GetCount(); | |
2516 | for ( size_t n = 0; n < count; n++ ) | |
2517 | { | |
2518 | int & rowOrCol = m_rowsOrCols[n]; | |
d1c0b4f9 VZ |
2519 | if ( (size_t)rowOrCol >= pos ) |
2520 | { | |
2521 | if ( numRowsOrCols > 0 ) | |
2522 | { | |
2523 | // If rows inserted, include row counter where necessary | |
2524 | rowOrCol += numRowsOrCols; | |
2525 | } | |
2526 | else if ( numRowsOrCols < 0) | |
2527 | { | |
2528 | // If rows deleted, either decrement row counter (if row still exists) | |
2529 | if ((size_t)rowOrCol >= pos - numRowsOrCols) | |
2530 | rowOrCol += numRowsOrCols; | |
2531 | else | |
2532 | { | |
2533 | m_rowsOrCols.RemoveAt((size_t)n); | |
2534 | m_attrs.RemoveAt((size_t)n); | |
2535 | n--; count--; | |
2536 | } | |
2537 | } | |
4d60017a SN |
2538 | } |
2539 | } | |
2540 | } | |
2541 | ||
b99be8fb VZ |
2542 | // ---------------------------------------------------------------------------- |
2543 | // wxGridCellAttrProvider | |
2544 | // ---------------------------------------------------------------------------- | |
2545 | ||
2546 | wxGridCellAttrProvider::wxGridCellAttrProvider() | |
2547 | { | |
2548 | m_data = (wxGridCellAttrProviderData *)NULL; | |
2549 | } | |
2550 | ||
2551 | wxGridCellAttrProvider::~wxGridCellAttrProvider() | |
2552 | { | |
2553 | delete m_data; | |
2554 | } | |
2555 | ||
2556 | void wxGridCellAttrProvider::InitData() | |
2557 | { | |
2558 | m_data = new wxGridCellAttrProviderData; | |
2559 | } | |
2560 | ||
19d7140e VZ |
2561 | wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col, |
2562 | wxGridCellAttr::wxAttrKind kind ) const | |
b99be8fb | 2563 | { |
758cbedf VZ |
2564 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
2565 | if ( m_data ) | |
2566 | { | |
19d7140e | 2567 | switch(kind) |
758cbedf | 2568 | { |
19d7140e VZ |
2569 | case (wxGridCellAttr::Any): |
2570 | //Get cached merge attributes. | |
2571 | // Currenlty not used as no cache implemented as not mutiable | |
2572 | // attr = m_data->m_mergeAttr.GetAttr(row, col); | |
2573 | if(!attr) | |
2574 | { | |
2575 | //Basicaly implement old version. | |
2576 | //Also check merge cache, so we don't have to re-merge every time.. | |
2577 | wxGridCellAttr *attrcell = (wxGridCellAttr *)NULL, | |
2578 | *attrrow = (wxGridCellAttr *)NULL, | |
bf7945ce RD |
2579 | *attrcol = (wxGridCellAttr *)NULL; |
2580 | ||
19d7140e VZ |
2581 | attrcell = m_data->m_cellAttrs.GetAttr(row, col); |
2582 | attrcol = m_data->m_colAttrs.GetAttr(col); | |
2583 | attrrow = m_data->m_rowAttrs.GetAttr(row); | |
2584 | ||
bf7945ce | 2585 | if((attrcell != attrrow) && (attrrow !=attrcol) && (attrcell != attrcol)){ |
19d7140e VZ |
2586 | // Two or move are non NULL |
2587 | attr = new wxGridCellAttr; | |
2588 | attr->SetKind(wxGridCellAttr::Merged); | |
2589 | ||
bf7945ce | 2590 | //Order important.. |
19d7140e VZ |
2591 | if(attrcell){ |
2592 | attr->MergeWith(attrcell); | |
2593 | attrcell->DecRef(); | |
2594 | } | |
2595 | if(attrcol){ | |
2596 | attr->MergeWith(attrcol); | |
2597 | attrcol->DecRef(); | |
2598 | } | |
2599 | if(attrrow){ | |
2600 | attr->MergeWith(attrrow); | |
2601 | attrrow->DecRef(); | |
2602 | } | |
2603 | //store merge attr if cache implemented | |
2604 | //attr->IncRef(); | |
2605 | //m_data->m_mergeAttr.SetAttr(attr, row, col); | |
2606 | } | |
2607 | else | |
758cbedf | 2608 | { |
19d7140e VZ |
2609 | // one or none is non null return it or null. |
2610 | if(attrrow) attr = attrrow; | |
2611 | if(attrcol) attr = attrcol; | |
bf7945ce | 2612 | if(attrcell) attr = attrcell; |
19d7140e VZ |
2613 | } |
2614 | } | |
2615 | break; | |
2616 | case (wxGridCellAttr::Cell): | |
2617 | attr = m_data->m_cellAttrs.GetAttr(row, col); | |
2618 | break; | |
2619 | case (wxGridCellAttr::Col): | |
2620 | attr = m_data->m_colAttrs.GetAttr(col); | |
2621 | break; | |
2622 | case (wxGridCellAttr::Row): | |
758cbedf | 2623 | attr = m_data->m_rowAttrs.GetAttr(row); |
19d7140e VZ |
2624 | break; |
2625 | default: | |
2626 | // unused as yet... | |
2627 | // (wxGridCellAttr::Default): | |
2628 | // (wxGridCellAttr::Merged): | |
2629 | break; | |
758cbedf VZ |
2630 | } |
2631 | } | |
758cbedf | 2632 | return attr; |
b99be8fb VZ |
2633 | } |
2634 | ||
2e9a6788 | 2635 | void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, |
b99be8fb VZ |
2636 | int row, int col) |
2637 | { | |
2638 | if ( !m_data ) | |
2639 | InitData(); | |
2640 | ||
758cbedf VZ |
2641 | m_data->m_cellAttrs.SetAttr(attr, row, col); |
2642 | } | |
2643 | ||
2644 | void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row) | |
2645 | { | |
2646 | if ( !m_data ) | |
2647 | InitData(); | |
2648 | ||
2649 | m_data->m_rowAttrs.SetAttr(attr, row); | |
2650 | } | |
2651 | ||
2652 | void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) | |
2653 | { | |
2654 | if ( !m_data ) | |
2655 | InitData(); | |
2656 | ||
2657 | m_data->m_colAttrs.SetAttr(attr, col); | |
b99be8fb VZ |
2658 | } |
2659 | ||
4d60017a SN |
2660 | void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) |
2661 | { | |
2662 | if ( m_data ) | |
2663 | { | |
2664 | m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); | |
2665 | ||
d1c0b4f9 | 2666 | m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); |
4d60017a SN |
2667 | } |
2668 | } | |
2669 | ||
2670 | void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) | |
2671 | { | |
2672 | if ( m_data ) | |
2673 | { | |
2674 | m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); | |
2675 | ||
d1c0b4f9 | 2676 | m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); |
4d60017a SN |
2677 | } |
2678 | } | |
2679 | ||
f2d76237 RD |
2680 | // ---------------------------------------------------------------------------- |
2681 | // wxGridTypeRegistry | |
2682 | // ---------------------------------------------------------------------------- | |
2683 | ||
2684 | wxGridTypeRegistry::~wxGridTypeRegistry() | |
2685 | { | |
b94ae1ea VZ |
2686 | size_t count = m_typeinfo.Count(); |
2687 | for ( size_t i = 0; i < count; i++ ) | |
f2d76237 RD |
2688 | delete m_typeinfo[i]; |
2689 | } | |
2690 | ||
2691 | ||
2692 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, | |
2693 | wxGridCellRenderer* renderer, | |
2694 | wxGridCellEditor* editor) | |
2695 | { | |
f2d76237 RD |
2696 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); |
2697 | ||
2698 | // is it already registered? | |
c4608a8a | 2699 | int loc = FindRegisteredDataType(typeName); |
39bcce60 VZ |
2700 | if ( loc != wxNOT_FOUND ) |
2701 | { | |
f2d76237 RD |
2702 | delete m_typeinfo[loc]; |
2703 | m_typeinfo[loc] = info; | |
2704 | } | |
39bcce60 VZ |
2705 | else |
2706 | { | |
f2d76237 RD |
2707 | m_typeinfo.Add(info); |
2708 | } | |
2709 | } | |
2710 | ||
c4608a8a VZ |
2711 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) |
2712 | { | |
2713 | size_t count = m_typeinfo.GetCount(); | |
2714 | for ( size_t i = 0; i < count; i++ ) | |
2715 | { | |
2716 | if ( typeName == m_typeinfo[i]->m_typeName ) | |
2717 | { | |
2718 | return i; | |
2719 | } | |
2720 | } | |
2721 | ||
2722 | return wxNOT_FOUND; | |
2723 | } | |
2724 | ||
f2d76237 RD |
2725 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) |
2726 | { | |
c4608a8a VZ |
2727 | int index = FindRegisteredDataType(typeName); |
2728 | if ( index == wxNOT_FOUND ) | |
2729 | { | |
2730 | // check whether this is one of the standard ones, in which case | |
2731 | // register it "on the fly" | |
3a8c693a | 2732 | #if wxUSE_TEXTCTRL |
c4608a8a VZ |
2733 | if ( typeName == wxGRID_VALUE_STRING ) |
2734 | { | |
2735 | RegisterDataType(wxGRID_VALUE_STRING, | |
2736 | new wxGridCellStringRenderer, | |
2737 | new wxGridCellTextEditor); | |
3a8c693a VZ |
2738 | } else |
2739 | #endif // wxUSE_TEXTCTRL | |
2740 | #if wxUSE_CHECKBOX | |
2741 | if ( typeName == wxGRID_VALUE_BOOL ) | |
c4608a8a VZ |
2742 | { |
2743 | RegisterDataType(wxGRID_VALUE_BOOL, | |
2744 | new wxGridCellBoolRenderer, | |
2745 | new wxGridCellBoolEditor); | |
3a8c693a VZ |
2746 | } else |
2747 | #endif // wxUSE_CHECKBOX | |
2748 | #if wxUSE_TEXTCTRL | |
2749 | if ( typeName == wxGRID_VALUE_NUMBER ) | |
c4608a8a VZ |
2750 | { |
2751 | RegisterDataType(wxGRID_VALUE_NUMBER, | |
2752 | new wxGridCellNumberRenderer, | |
2753 | new wxGridCellNumberEditor); | |
2754 | } | |
2755 | else if ( typeName == wxGRID_VALUE_FLOAT ) | |
2756 | { | |
2757 | RegisterDataType(wxGRID_VALUE_FLOAT, | |
2758 | new wxGridCellFloatRenderer, | |
2759 | new wxGridCellFloatEditor); | |
3a8c693a VZ |
2760 | } else |
2761 | #endif // wxUSE_TEXTCTRL | |
2762 | #if wxUSE_COMBOBOX | |
2763 | if ( typeName == wxGRID_VALUE_CHOICE ) | |
c4608a8a VZ |
2764 | { |
2765 | RegisterDataType(wxGRID_VALUE_CHOICE, | |
2766 | new wxGridCellStringRenderer, | |
2767 | new wxGridCellChoiceEditor); | |
3a8c693a VZ |
2768 | } else |
2769 | #endif // wxUSE_COMBOBOX | |
c4608a8a VZ |
2770 | { |
2771 | return wxNOT_FOUND; | |
2772 | } | |
f2d76237 | 2773 | |
c4608a8a VZ |
2774 | // we get here only if just added the entry for this type, so return |
2775 | // the last index | |
2776 | index = m_typeinfo.GetCount() - 1; | |
2777 | } | |
2778 | ||
2779 | return index; | |
2780 | } | |
2781 | ||
2782 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) | |
2783 | { | |
2784 | int index = FindDataType(typeName); | |
2785 | if ( index == wxNOT_FOUND ) | |
2786 | { | |
2787 | // the first part of the typename is the "real" type, anything after ':' | |
2788 | // are the parameters for the renderer | |
2789 | index = FindDataType(typeName.BeforeFirst(_T(':'))); | |
2790 | if ( index == wxNOT_FOUND ) | |
2791 | { | |
2792 | return wxNOT_FOUND; | |
f2d76237 | 2793 | } |
c4608a8a VZ |
2794 | |
2795 | wxGridCellRenderer *renderer = GetRenderer(index); | |
2796 | wxGridCellRenderer *rendererOld = renderer; | |
2797 | renderer = renderer->Clone(); | |
2798 | rendererOld->DecRef(); | |
2799 | ||
2800 | wxGridCellEditor *editor = GetEditor(index); | |
2801 | wxGridCellEditor *editorOld = editor; | |
2802 | editor = editor->Clone(); | |
2803 | editorOld->DecRef(); | |
2804 | ||
2805 | // do it even if there are no parameters to reset them to defaults | |
2806 | wxString params = typeName.AfterFirst(_T(':')); | |
2807 | renderer->SetParameters(params); | |
2808 | editor->SetParameters(params); | |
2809 | ||
2810 | // register the new typename | |
c4608a8a VZ |
2811 | RegisterDataType(typeName, renderer, editor); |
2812 | ||
2813 | // we just registered it, it's the last one | |
2814 | index = m_typeinfo.GetCount() - 1; | |
f2d76237 RD |
2815 | } |
2816 | ||
c4608a8a | 2817 | return index; |
f2d76237 RD |
2818 | } |
2819 | ||
2820 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) | |
2821 | { | |
2822 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; | |
faec5a43 SN |
2823 | if (renderer) |
2824 | renderer->IncRef(); | |
f2d76237 RD |
2825 | return renderer; |
2826 | } | |
2827 | ||
0b190b0f | 2828 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) |
f2d76237 RD |
2829 | { |
2830 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; | |
faec5a43 SN |
2831 | if (editor) |
2832 | editor->IncRef(); | |
f2d76237 RD |
2833 | return editor; |
2834 | } | |
2835 | ||
758cbedf VZ |
2836 | // ---------------------------------------------------------------------------- |
2837 | // wxGridTableBase | |
2838 | // ---------------------------------------------------------------------------- | |
2839 | ||
f85afd4e MB |
2840 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) |
2841 | ||
2842 | ||
2843 | wxGridTableBase::wxGridTableBase() | |
f85afd4e MB |
2844 | { |
2845 | m_view = (wxGrid *) NULL; | |
b99be8fb | 2846 | m_attrProvider = (wxGridCellAttrProvider *) NULL; |
f85afd4e MB |
2847 | } |
2848 | ||
2849 | wxGridTableBase::~wxGridTableBase() | |
2850 | { | |
b99be8fb VZ |
2851 | delete m_attrProvider; |
2852 | } | |
2853 | ||
2854 | void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider) | |
2855 | { | |
2856 | delete m_attrProvider; | |
2857 | m_attrProvider = attrProvider; | |
f85afd4e MB |
2858 | } |
2859 | ||
f2d76237 RD |
2860 | bool wxGridTableBase::CanHaveAttributes() |
2861 | { | |
2862 | if ( ! GetAttrProvider() ) | |
2863 | { | |
2864 | // use the default attr provider by default | |
2865 | SetAttrProvider(new wxGridCellAttrProvider); | |
2866 | } | |
2867 | return TRUE; | |
2868 | } | |
2869 | ||
19d7140e | 2870 | wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) |
b99be8fb VZ |
2871 | { |
2872 | if ( m_attrProvider ) | |
19d7140e | 2873 | return m_attrProvider->GetAttr(row, col, kind); |
b99be8fb VZ |
2874 | else |
2875 | return (wxGridCellAttr *)NULL; | |
2876 | } | |
2877 | ||
758cbedf | 2878 | void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) |
b99be8fb VZ |
2879 | { |
2880 | if ( m_attrProvider ) | |
2881 | { | |
19d7140e | 2882 | attr->SetKind(wxGridCellAttr::Cell); |
b99be8fb VZ |
2883 | m_attrProvider->SetAttr(attr, row, col); |
2884 | } | |
2885 | else | |
2886 | { | |
2887 | // as we take ownership of the pointer and don't store it, we must | |
2888 | // free it now | |
39bcce60 | 2889 | wxSafeDecRef(attr); |
b99be8fb VZ |
2890 | } |
2891 | } | |
2892 | ||
758cbedf VZ |
2893 | void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) |
2894 | { | |
2895 | if ( m_attrProvider ) | |
2896 | { | |
19d7140e | 2897 | attr->SetKind(wxGridCellAttr::Row); |
758cbedf VZ |
2898 | m_attrProvider->SetRowAttr(attr, row); |
2899 | } | |
2900 | else | |
2901 | { | |
2902 | // as we take ownership of the pointer and don't store it, we must | |
2903 | // free it now | |
39bcce60 | 2904 | wxSafeDecRef(attr); |
758cbedf VZ |
2905 | } |
2906 | } | |
2907 | ||
2908 | void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) | |
2909 | { | |
2910 | if ( m_attrProvider ) | |
2911 | { | |
19d7140e | 2912 | attr->SetKind(wxGridCellAttr::Col); |
758cbedf VZ |
2913 | m_attrProvider->SetColAttr(attr, col); |
2914 | } | |
2915 | else | |
2916 | { | |
2917 | // as we take ownership of the pointer and don't store it, we must | |
2918 | // free it now | |
39bcce60 | 2919 | wxSafeDecRef(attr); |
758cbedf VZ |
2920 | } |
2921 | } | |
2922 | ||
aa5e1f75 SN |
2923 | bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos), |
2924 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 2925 | { |
f6bcfd97 | 2926 | wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); |
8f177c8e | 2927 | |
f85afd4e MB |
2928 | return FALSE; |
2929 | } | |
2930 | ||
aa5e1f75 | 2931 | bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) ) |
f85afd4e | 2932 | { |
f6bcfd97 | 2933 | wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); |
8f177c8e | 2934 | |
f85afd4e MB |
2935 | return FALSE; |
2936 | } | |
2937 | ||
aa5e1f75 SN |
2938 | bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos), |
2939 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 2940 | { |
f6bcfd97 | 2941 | wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); |
8f177c8e | 2942 | |
f85afd4e MB |
2943 | return FALSE; |
2944 | } | |
2945 | ||
aa5e1f75 SN |
2946 | bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos), |
2947 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 2948 | { |
f6bcfd97 | 2949 | wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); |
8f177c8e | 2950 | |
f85afd4e MB |
2951 | return FALSE; |
2952 | } | |
2953 | ||
aa5e1f75 | 2954 | bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) ) |
f85afd4e | 2955 | { |
f6bcfd97 | 2956 | wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); |
8f177c8e | 2957 | |
f85afd4e MB |
2958 | return FALSE; |
2959 | } | |
2960 | ||
aa5e1f75 SN |
2961 | bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos), |
2962 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 2963 | { |
f6bcfd97 | 2964 | wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); |
8f177c8e | 2965 | |
f85afd4e MB |
2966 | return FALSE; |
2967 | } | |
2968 | ||
2969 | ||
2970 | wxString wxGridTableBase::GetRowLabelValue( int row ) | |
2971 | { | |
2972 | wxString s; | |
f2d76237 RD |
2973 | s << row + 1; // RD: Starting the rows at zero confuses users, no matter |
2974 | // how much it makes sense to us geeks. | |
f85afd4e MB |
2975 | return s; |
2976 | } | |
2977 | ||
2978 | wxString wxGridTableBase::GetColLabelValue( int col ) | |
2979 | { | |
2980 | // default col labels are: | |
2981 | // cols 0 to 25 : A-Z | |
2982 | // cols 26 to 675 : AA-ZZ | |
2983 | // etc. | |
2984 | ||
2985 | wxString s; | |
2986 | unsigned int i, n; | |
2987 | for ( n = 1; ; n++ ) | |
2988 | { | |
f0102d2a | 2989 | s += (_T('A') + (wxChar)( col%26 )); |
f85afd4e MB |
2990 | col = col/26 - 1; |
2991 | if ( col < 0 ) break; | |
2992 | } | |
2993 | ||
2994 | // reverse the string... | |
2995 | wxString s2; | |
2996 | for ( i = 0; i < n; i++ ) | |
2997 | { | |
2998 | s2 += s[n-i-1]; | |
2999 | } | |
3000 | ||
3001 | return s2; | |
3002 | } | |
3003 | ||
3004 | ||
f2d76237 RD |
3005 | wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) ) |
3006 | { | |
816be743 | 3007 | return wxGRID_VALUE_STRING; |
f2d76237 RD |
3008 | } |
3009 | ||
3010 | bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col), | |
3011 | const wxString& typeName ) | |
3012 | { | |
816be743 | 3013 | return typeName == wxGRID_VALUE_STRING; |
f2d76237 RD |
3014 | } |
3015 | ||
3016 | bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName ) | |
3017 | { | |
3018 | return CanGetValueAs(row, col, typeName); | |
3019 | } | |
3020 | ||
3021 | long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) ) | |
3022 | { | |
3023 | return 0; | |
3024 | } | |
3025 | ||
3026 | double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) ) | |
3027 | { | |
3028 | return 0.0; | |
3029 | } | |
3030 | ||
3031 | bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) ) | |
3032 | { | |
3033 | return FALSE; | |
3034 | } | |
3035 | ||
3036 | void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col), | |
3037 | long WXUNUSED(value) ) | |
3038 | { | |
3039 | } | |
3040 | ||
3041 | void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col), | |
3042 | double WXUNUSED(value) ) | |
3043 | { | |
3044 | } | |
3045 | ||
3046 | void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col), | |
3047 | bool WXUNUSED(value) ) | |
3048 | { | |
3049 | } | |
3050 | ||
3051 | ||
3052 | void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3053 | const wxString& WXUNUSED(typeName) ) | |
3054 | { | |
3055 | return NULL; | |
3056 | } | |
3057 | ||
3058 | void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3059 | const wxString& WXUNUSED(typeName), | |
3060 | void* WXUNUSED(value) ) | |
3061 | { | |
3062 | } | |
3063 | ||
f85afd4e MB |
3064 | ////////////////////////////////////////////////////////////////////// |
3065 | // | |
3066 | // Message class for the grid table to send requests and notifications | |
3067 | // to the grid view | |
3068 | // | |
3069 | ||
3070 | wxGridTableMessage::wxGridTableMessage() | |
3071 | { | |
3072 | m_table = (wxGridTableBase *) NULL; | |
3073 | m_id = -1; | |
3074 | m_comInt1 = -1; | |
3075 | m_comInt2 = -1; | |
3076 | } | |
3077 | ||
3078 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, | |
3079 | int commandInt1, int commandInt2 ) | |
3080 | { | |
3081 | m_table = table; | |
3082 | m_id = id; | |
3083 | m_comInt1 = commandInt1; | |
3084 | m_comInt2 = commandInt2; | |
3085 | } | |
3086 | ||
3087 | ||
3088 | ||
3089 | ////////////////////////////////////////////////////////////////////// | |
3090 | // | |
3091 | // A basic grid table for string data. An object of this class will | |
3092 | // created by wxGrid if you don't specify an alternative table class. | |
3093 | // | |
3094 | ||
223d09f6 | 3095 | WX_DEFINE_OBJARRAY(wxGridStringArray) |
f85afd4e MB |
3096 | |
3097 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) | |
3098 | ||
3099 | wxGridStringTable::wxGridStringTable() | |
3100 | : wxGridTableBase() | |
3101 | { | |
3102 | } | |
3103 | ||
3104 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) | |
3105 | : wxGridTableBase() | |
3106 | { | |
f85afd4e MB |
3107 | m_data.Alloc( numRows ); |
3108 | ||
3109 | wxArrayString sa; | |
3110 | sa.Alloc( numCols ); | |
27f35b66 | 3111 | sa.Add( wxEmptyString, numCols ); |
8f177c8e | 3112 | |
27f35b66 | 3113 | m_data.Add( sa, numRows ); |
f85afd4e MB |
3114 | } |
3115 | ||
3116 | wxGridStringTable::~wxGridStringTable() | |
3117 | { | |
3118 | } | |
3119 | ||
e32352cf | 3120 | int wxGridStringTable::GetNumberRows() |
f85afd4e MB |
3121 | { |
3122 | return m_data.GetCount(); | |
3123 | } | |
3124 | ||
e32352cf | 3125 | int wxGridStringTable::GetNumberCols() |
f85afd4e MB |
3126 | { |
3127 | if ( m_data.GetCount() > 0 ) | |
3128 | return m_data[0].GetCount(); | |
3129 | else | |
3130 | return 0; | |
3131 | } | |
3132 | ||
3133 | wxString wxGridStringTable::GetValue( int row, int col ) | |
3134 | { | |
831243b1 | 3135 | wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
af547d51 VZ |
3136 | _T("invalid row or column index in wxGridStringTable") ); |
3137 | ||
f85afd4e MB |
3138 | return m_data[row][col]; |
3139 | } | |
3140 | ||
f2d76237 | 3141 | void wxGridStringTable::SetValue( int row, int col, const wxString& value ) |
f85afd4e | 3142 | { |
831243b1 | 3143 | wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
af547d51 VZ |
3144 | _T("invalid row or column index in wxGridStringTable") ); |
3145 | ||
f2d76237 | 3146 | m_data[row][col] = value; |
f85afd4e MB |
3147 | } |
3148 | ||
3149 | bool wxGridStringTable::IsEmptyCell( int row, int col ) | |
3150 | { | |
831243b1 | 3151 | wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
af547d51 VZ |
3152 | _T("invalid row or column index in wxGridStringTable") ); |
3153 | ||
f85afd4e MB |
3154 | return (m_data[row][col] == wxEmptyString); |
3155 | } | |
3156 | ||
f85afd4e MB |
3157 | void wxGridStringTable::Clear() |
3158 | { | |
3159 | int row, col; | |
3160 | int numRows, numCols; | |
8f177c8e | 3161 | |
f85afd4e MB |
3162 | numRows = m_data.GetCount(); |
3163 | if ( numRows > 0 ) | |
3164 | { | |
3165 | numCols = m_data[0].GetCount(); | |
3166 | ||
3167 | for ( row = 0; row < numRows; row++ ) | |
3168 | { | |
3169 | for ( col = 0; col < numCols; col++ ) | |
3170 | { | |
3171 | m_data[row][col] = wxEmptyString; | |
3172 | } | |
3173 | } | |
3174 | } | |
3175 | } | |
3176 | ||
3177 | ||
3178 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) | |
3179 | { | |
f85afd4e | 3180 | size_t curNumRows = m_data.GetCount(); |
f6bcfd97 BP |
3181 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3182 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3183 | |
f85afd4e MB |
3184 | if ( pos >= curNumRows ) |
3185 | { | |
3186 | return AppendRows( numRows ); | |
3187 | } | |
8f177c8e | 3188 | |
f85afd4e MB |
3189 | wxArrayString sa; |
3190 | sa.Alloc( curNumCols ); | |
27f35b66 SN |
3191 | sa.Add( wxEmptyString, curNumCols ); |
3192 | m_data.Insert( sa, pos, numRows ); | |
f85afd4e MB |
3193 | if ( GetView() ) |
3194 | { | |
3195 | wxGridTableMessage msg( this, | |
3196 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, | |
3197 | pos, | |
3198 | numRows ); | |
8f177c8e | 3199 | |
f85afd4e MB |
3200 | GetView()->ProcessTableMessage( msg ); |
3201 | } | |
3202 | ||
3203 | return TRUE; | |
3204 | } | |
3205 | ||
3206 | bool wxGridStringTable::AppendRows( size_t numRows ) | |
3207 | { | |
f85afd4e | 3208 | size_t curNumRows = m_data.GetCount(); |
f6bcfd97 BP |
3209 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3210 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3211 | |
f85afd4e MB |
3212 | wxArrayString sa; |
3213 | if ( curNumCols > 0 ) | |
3214 | { | |
3215 | sa.Alloc( curNumCols ); | |
27f35b66 | 3216 | sa.Add( wxEmptyString, curNumCols ); |
f85afd4e | 3217 | } |
8f177c8e | 3218 | |
27f35b66 | 3219 | m_data.Add( sa, numRows ); |
f85afd4e MB |
3220 | |
3221 | if ( GetView() ) | |
3222 | { | |
3223 | wxGridTableMessage msg( this, | |
3224 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, | |
3225 | numRows ); | |
8f177c8e | 3226 | |
f85afd4e MB |
3227 | GetView()->ProcessTableMessage( msg ); |
3228 | } | |
3229 | ||
8f177c8e | 3230 | return TRUE; |
f85afd4e MB |
3231 | } |
3232 | ||
3233 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) | |
3234 | { | |
f85afd4e | 3235 | size_t curNumRows = m_data.GetCount(); |
8f177c8e | 3236 | |
f85afd4e MB |
3237 | if ( pos >= curNumRows ) |
3238 | { | |
e91d2033 VZ |
3239 | wxFAIL_MSG( wxString::Format |
3240 | ( | |
3241 | wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), | |
3242 | (unsigned long)pos, | |
3243 | (unsigned long)numRows, | |
3244 | (unsigned long)curNumRows | |
3245 | ) ); | |
3246 | ||
f85afd4e MB |
3247 | return FALSE; |
3248 | } | |
3249 | ||
3250 | if ( numRows > curNumRows - pos ) | |
3251 | { | |
3252 | numRows = curNumRows - pos; | |
3253 | } | |
8f177c8e | 3254 | |
f85afd4e MB |
3255 | if ( numRows >= curNumRows ) |
3256 | { | |
d57ad377 | 3257 | m_data.Clear(); |
f85afd4e MB |
3258 | } |
3259 | else | |
3260 | { | |
27f35b66 | 3261 | m_data.RemoveAt( pos, numRows ); |
f85afd4e | 3262 | } |
f85afd4e MB |
3263 | if ( GetView() ) |
3264 | { | |
3265 | wxGridTableMessage msg( this, | |
3266 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, | |
3267 | pos, | |
3268 | numRows ); | |
8f177c8e | 3269 | |
f85afd4e MB |
3270 | GetView()->ProcessTableMessage( msg ); |
3271 | } | |
3272 | ||
8f177c8e | 3273 | return TRUE; |
f85afd4e MB |
3274 | } |
3275 | ||
3276 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) | |
3277 | { | |
3278 | size_t row, col; | |
3279 | ||
3280 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 BP |
3281 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3282 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3283 | |
f85afd4e MB |
3284 | if ( pos >= curNumCols ) |
3285 | { | |
3286 | return AppendCols( numCols ); | |
3287 | } | |
3288 | ||
3289 | for ( row = 0; row < curNumRows; row++ ) | |
3290 | { | |
3291 | for ( col = pos; col < pos + numCols; col++ ) | |
3292 | { | |
3293 | m_data[row].Insert( wxEmptyString, col ); | |
3294 | } | |
3295 | } | |
f85afd4e MB |
3296 | if ( GetView() ) |
3297 | { | |
3298 | wxGridTableMessage msg( this, | |
3299 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, | |
3300 | pos, | |
3301 | numCols ); | |
8f177c8e | 3302 | |
f85afd4e MB |
3303 | GetView()->ProcessTableMessage( msg ); |
3304 | } | |
3305 | ||
3306 | return TRUE; | |
3307 | } | |
3308 | ||
3309 | bool wxGridStringTable::AppendCols( size_t numCols ) | |
3310 | { | |
27f35b66 | 3311 | size_t row; |
f85afd4e MB |
3312 | |
3313 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 | 3314 | #if 0 |
f85afd4e MB |
3315 | if ( !curNumRows ) |
3316 | { | |
3317 | // TODO: something better than this ? | |
3318 | // | |
f6bcfd97 | 3319 | wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") ); |
f85afd4e MB |
3320 | return FALSE; |
3321 | } | |
f6bcfd97 | 3322 | #endif |
8f177c8e | 3323 | |
f85afd4e MB |
3324 | for ( row = 0; row < curNumRows; row++ ) |
3325 | { | |
27f35b66 | 3326 | m_data[row].Add( wxEmptyString, numCols ); |
f85afd4e MB |
3327 | } |
3328 | ||
3329 | if ( GetView() ) | |
3330 | { | |
3331 | wxGridTableMessage msg( this, | |
3332 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, | |
3333 | numCols ); | |
8f177c8e | 3334 | |
f85afd4e MB |
3335 | GetView()->ProcessTableMessage( msg ); |
3336 | } | |
3337 | ||
3338 | return TRUE; | |
3339 | } | |
3340 | ||
3341 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) | |
3342 | { | |
27f35b66 | 3343 | size_t row; |
f85afd4e MB |
3344 | |
3345 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 BP |
3346 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
3347 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 3348 | |
f85afd4e MB |
3349 | if ( pos >= curNumCols ) |
3350 | { | |
e91d2033 VZ |
3351 | wxFAIL_MSG( wxString::Format |
3352 | ( | |
3353 | wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), | |
3354 | (unsigned long)pos, | |
3355 | (unsigned long)numCols, | |
3356 | (unsigned long)curNumCols | |
3357 | ) ); | |
8f177c8e | 3358 | return FALSE; |
f85afd4e MB |
3359 | } |
3360 | ||
3361 | if ( numCols > curNumCols - pos ) | |
3362 | { | |
8f177c8e | 3363 | numCols = curNumCols - pos; |
f85afd4e MB |
3364 | } |
3365 | ||
3366 | for ( row = 0; row < curNumRows; row++ ) | |
3367 | { | |
3368 | if ( numCols >= curNumCols ) | |
3369 | { | |
dcdce64e | 3370 | m_data[row].Clear(); |
f85afd4e MB |
3371 | } |
3372 | else | |
3373 | { | |
27f35b66 | 3374 | m_data[row].RemoveAt( pos, numCols ); |
f85afd4e MB |
3375 | } |
3376 | } | |
f85afd4e MB |
3377 | if ( GetView() ) |
3378 | { | |
3379 | wxGridTableMessage msg( this, | |
3380 | wxGRIDTABLE_NOTIFY_COLS_DELETED, | |
3381 | pos, | |
3382 | numCols ); | |
8f177c8e | 3383 | |
f85afd4e MB |
3384 | GetView()->ProcessTableMessage( msg ); |
3385 | } | |
3386 | ||
8f177c8e | 3387 | return TRUE; |
f85afd4e MB |
3388 | } |
3389 | ||
3390 | wxString wxGridStringTable::GetRowLabelValue( int row ) | |
3391 | { | |
3392 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3393 | { | |
3394 | // using default label | |
3395 | // | |
3396 | return wxGridTableBase::GetRowLabelValue( row ); | |
3397 | } | |
3398 | else | |
3399 | { | |
3400 | return m_rowLabels[ row ]; | |
3401 | } | |
3402 | } | |
3403 | ||
3404 | wxString wxGridStringTable::GetColLabelValue( int col ) | |
3405 | { | |
3406 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3407 | { | |
3408 | // using default label | |
3409 | // | |
3410 | return wxGridTableBase::GetColLabelValue( col ); | |
3411 | } | |
3412 | else | |
3413 | { | |
3414 | return m_colLabels[ col ]; | |
3415 | } | |
3416 | } | |
3417 | ||
3418 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) | |
3419 | { | |
3420 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3421 | { | |
3422 | int n = m_rowLabels.GetCount(); | |
3423 | int i; | |
3424 | for ( i = n; i <= row; i++ ) | |
3425 | { | |
3426 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); | |
3427 | } | |
3428 | } | |
3429 | ||
3430 | m_rowLabels[row] = value; | |
3431 | } | |
3432 | ||
3433 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) | |
3434 | { | |
3435 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3436 | { | |
3437 | int n = m_colLabels.GetCount(); | |
3438 | int i; | |
3439 | for ( i = n; i <= col; i++ ) | |
3440 | { | |
3441 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); | |
3442 | } | |
3443 | } | |
3444 | ||
3445 | m_colLabels[col] = value; | |
3446 | } | |
3447 | ||
3448 | ||
3449 | ||
f85afd4e | 3450 | ////////////////////////////////////////////////////////////////////// |
2d66e025 MB |
3451 | ////////////////////////////////////////////////////////////////////// |
3452 | ||
3453 | IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow ) | |
3454 | ||
3455 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow ) | |
3456 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) | |
b51c3f27 | 3457 | EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel) |
2d66e025 MB |
3458 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) |
3459 | EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown ) | |
f6bcfd97 | 3460 | EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp ) |
2d66e025 MB |
3461 | END_EVENT_TABLE() |
3462 | ||
60ff3b99 VZ |
3463 | wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent, |
3464 | wxWindowID id, | |
2d66e025 | 3465 | const wxPoint &pos, const wxSize &size ) |
ebd773c6 | 3466 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS ) |
2d66e025 MB |
3467 | { |
3468 | m_owner = parent; | |
3469 | } | |
3470 | ||
aa5e1f75 | 3471 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
3472 | { |
3473 | wxPaintDC dc(this); | |
3474 | ||
3475 | // NO - don't do this because it will set both the x and y origin | |
3476 | // coords to match the parent scrolled window and we just want to | |
3477 | // set the y coord - MB | |
3478 | // | |
3479 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 3480 | |
790ad94f | 3481 | int x, y; |
2d66e025 MB |
3482 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
3483 | dc.SetDeviceOrigin( 0, -y ); | |
60ff3b99 | 3484 | |
d10f4bf9 VZ |
3485 | wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); |
3486 | m_owner->DrawRowLabels( dc , rows ); | |
2d66e025 MB |
3487 | } |
3488 | ||
3489 | ||
3490 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3491 | { | |
3492 | m_owner->ProcessRowLabelMouseEvent( event ); | |
3493 | } | |
3494 | ||
3495 | ||
b51c3f27 RD |
3496 | void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3497 | { | |
3498 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3499 | } | |
3500 | ||
3501 | ||
2d66e025 MB |
3502 | // This seems to be required for wxMotif otherwise the mouse |
3503 | // cursor must be in the cell edit control to get key events | |
3504 | // | |
3505 | void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3506 | { | |
ffdd3c98 | 3507 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3508 | } |
3509 | ||
f6bcfd97 BP |
3510 | void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3511 | { | |
ffdd3c98 | 3512 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3513 | } |
3514 | ||
2d66e025 MB |
3515 | |
3516 | ||
3517 | ////////////////////////////////////////////////////////////////////// | |
3518 | ||
3519 | IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow ) | |
3520 | ||
3521 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow ) | |
3522 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) | |
b51c3f27 | 3523 | EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel) |
2d66e025 MB |
3524 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) |
3525 | EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown ) | |
f6bcfd97 | 3526 | EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp ) |
2d66e025 MB |
3527 | END_EVENT_TABLE() |
3528 | ||
60ff3b99 VZ |
3529 | wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent, |
3530 | wxWindowID id, | |
2d66e025 | 3531 | const wxPoint &pos, const wxSize &size ) |
ebd773c6 | 3532 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS ) |
2d66e025 MB |
3533 | { |
3534 | m_owner = parent; | |
3535 | } | |
3536 | ||
aa5e1f75 | 3537 | void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
3538 | { |
3539 | wxPaintDC dc(this); | |
3540 | ||
3541 | // NO - don't do this because it will set both the x and y origin | |
3542 | // coords to match the parent scrolled window and we just want to | |
3543 | // set the x coord - MB | |
3544 | // | |
3545 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 3546 | |
790ad94f | 3547 | int x, y; |
2d66e025 MB |
3548 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
3549 | dc.SetDeviceOrigin( -x, 0 ); | |
3550 | ||
d10f4bf9 VZ |
3551 | wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() ); |
3552 | m_owner->DrawColLabels( dc , cols ); | |
2d66e025 MB |
3553 | } |
3554 | ||
3555 | ||
3556 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3557 | { | |
3558 | m_owner->ProcessColLabelMouseEvent( event ); | |
3559 | } | |
3560 | ||
b51c3f27 RD |
3561 | void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3562 | { | |
3563 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3564 | } | |
3565 | ||
2d66e025 MB |
3566 | |
3567 | // This seems to be required for wxMotif otherwise the mouse | |
3568 | // cursor must be in the cell edit control to get key events | |
3569 | // | |
3570 | void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3571 | { | |
ffdd3c98 | 3572 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3573 | } |
3574 | ||
f6bcfd97 BP |
3575 | void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3576 | { | |
ffdd3c98 | 3577 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3578 | } |
3579 | ||
2d66e025 MB |
3580 | |
3581 | ||
3582 | ////////////////////////////////////////////////////////////////////// | |
3583 | ||
3584 | IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow ) | |
3585 | ||
3586 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow ) | |
b51c3f27 | 3587 | EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel) |
2d66e025 | 3588 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) |
d2fdd8d2 | 3589 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint) |
2d66e025 | 3590 | EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown ) |
f6bcfd97 | 3591 | EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp ) |
2d66e025 MB |
3592 | END_EVENT_TABLE() |
3593 | ||
60ff3b99 VZ |
3594 | wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent, |
3595 | wxWindowID id, | |
2d66e025 | 3596 | const wxPoint &pos, const wxSize &size ) |
ebd773c6 | 3597 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS ) |
2d66e025 MB |
3598 | { |
3599 | m_owner = parent; | |
3600 | } | |
3601 | ||
d2fdd8d2 RR |
3602 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
3603 | { | |
3604 | wxPaintDC dc(this); | |
b99be8fb | 3605 | |
d2fdd8d2 RR |
3606 | int client_height = 0; |
3607 | int client_width = 0; | |
3608 | GetClientSize( &client_width, &client_height ); | |
b99be8fb | 3609 | |
73145b0e | 3610 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) ); |
d2fdd8d2 RR |
3611 | dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 ); |
3612 | dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 ); | |
d2fdd8d2 RR |
3613 | dc.DrawLine( 0, 0, client_width, 0 ); |
3614 | dc.DrawLine( 0, 0, 0, client_height ); | |
73145b0e JS |
3615 | |
3616 | dc.SetPen( *wxWHITE_PEN ); | |
3617 | dc.DrawLine( 1, 1, client_width-1, 1 ); | |
3618 | dc.DrawLine( 1, 1, 1, client_height-1 ); | |
d2fdd8d2 RR |
3619 | } |
3620 | ||
2d66e025 MB |
3621 | |
3622 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3623 | { | |
3624 | m_owner->ProcessCornerLabelMouseEvent( event ); | |
3625 | } | |
3626 | ||
3627 | ||
b51c3f27 RD |
3628 | void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
3629 | { | |
3630 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3631 | } | |
3632 | ||
2d66e025 MB |
3633 | // This seems to be required for wxMotif otherwise the mouse |
3634 | // cursor must be in the cell edit control to get key events | |
3635 | // | |
3636 | void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3637 | { | |
ffdd3c98 | 3638 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 MB |
3639 | } |
3640 | ||
f6bcfd97 BP |
3641 | void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event ) |
3642 | { | |
ffdd3c98 | 3643 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 BP |
3644 | } |
3645 | ||
2d66e025 MB |
3646 | |
3647 | ||
f85afd4e MB |
3648 | ////////////////////////////////////////////////////////////////////// |
3649 | ||
59ddac01 | 3650 | IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow ) |
2d66e025 | 3651 | |
59ddac01 | 3652 | BEGIN_EVENT_TABLE( wxGridWindow, wxWindow ) |
2d66e025 | 3653 | EVT_PAINT( wxGridWindow::OnPaint ) |
b51c3f27 | 3654 | EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel) |
2d66e025 MB |
3655 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) |
3656 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) | |
f6bcfd97 | 3657 | EVT_KEY_UP( wxGridWindow::OnKeyUp ) |
2796cce3 | 3658 | EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground ) |
2d66e025 MB |
3659 | END_EVENT_TABLE() |
3660 | ||
60ff3b99 VZ |
3661 | wxGridWindow::wxGridWindow( wxGrid *parent, |
3662 | wxGridRowLabelWindow *rowLblWin, | |
2d66e025 | 3663 | wxGridColLabelWindow *colLblWin, |
04418332 VZ |
3664 | wxWindowID id, |
3665 | const wxPoint &pos, | |
3666 | const wxSize &size ) | |
3667 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxCLIP_CHILDREN, | |
3668 | wxT("grid window") ) | |
73145b0e | 3669 | |
2d66e025 MB |
3670 | { |
3671 | m_owner = parent; | |
3672 | m_rowLabelWin = rowLblWin; | |
3673 | m_colLabelWin = colLblWin; | |
edb89f7e | 3674 | SetBackgroundColour(_T("WHITE")); |
2d66e025 MB |
3675 | } |
3676 | ||
3677 | ||
3678 | wxGridWindow::~wxGridWindow() | |
3679 | { | |
3680 | } | |
3681 | ||
3682 | ||
3683 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
3684 | { | |
3685 | wxPaintDC dc( this ); | |
3686 | m_owner->PrepareDC( dc ); | |
796df70a | 3687 | wxRegion reg = GetUpdateRegion(); |
d10f4bf9 VZ |
3688 | wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg ); |
3689 | m_owner->DrawGridCellArea( dc , DirtyCells); | |
9496deb5 | 3690 | #if WXGRID_DRAW_LINES |
796df70a SN |
3691 | m_owner->DrawAllGridLines( dc, reg ); |
3692 | #endif | |
a5777624 | 3693 | m_owner->DrawGridSpace( dc ); |
d10f4bf9 | 3694 | m_owner->DrawHighlight( dc , DirtyCells ); |
2d66e025 MB |
3695 | } |
3696 | ||
3697 | ||
3698 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
3699 | { | |
59ddac01 | 3700 | wxWindow::ScrollWindow( dx, dy, rect ); |
2d66e025 MB |
3701 | m_rowLabelWin->ScrollWindow( 0, dy, rect ); |
3702 | m_colLabelWin->ScrollWindow( dx, 0, rect ); | |
3703 | } | |
3704 | ||
3705 | ||
3706 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) | |
3707 | { | |
3708 | m_owner->ProcessGridCellMouseEvent( event ); | |
3709 | } | |
3710 | ||
b51c3f27 RD |
3711 | void wxGridWindow::OnMouseWheel( wxMouseEvent& event ) |
3712 | { | |
3713 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3714 | } | |
2d66e025 | 3715 | |
f6bcfd97 | 3716 | // This seems to be required for wxMotif/wxGTK otherwise the mouse |
2d66e025 MB |
3717 | // cursor must be in the cell edit control to get key events |
3718 | // | |
3719 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) | |
3720 | { | |
ffdd3c98 | 3721 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
2d66e025 | 3722 | } |
f85afd4e | 3723 | |
f6bcfd97 BP |
3724 | void wxGridWindow::OnKeyUp( wxKeyEvent& event ) |
3725 | { | |
ffdd3c98 | 3726 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
f6bcfd97 | 3727 | } |
7c8a8ad5 | 3728 | |
aa5e1f75 | 3729 | void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) |
8dd4f536 | 3730 | { |
8dd4f536 | 3731 | } |
025562fe | 3732 | |
2d66e025 MB |
3733 | |
3734 | ////////////////////////////////////////////////////////////////////// | |
3735 | ||
33188aa4 SN |
3736 | // Internal Helper function for computing row or column from some |
3737 | // (unscrolled) coordinate value, using either | |
70e8d961 | 3738 | // m_defaultRowHeight/m_defaultColWidth or binary search on array |
33188aa4 SN |
3739 | // of m_rowBottoms/m_ColRights to speed up the search! |
3740 | ||
3741 | // Internal helper macros for simpler use of that function | |
3742 | ||
3743 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, | |
64e15340 JS |
3744 | const wxArrayInt& BorderArray, int nMax, |
3745 | bool maxOnOverflow); | |
33188aa4 SN |
3746 | |
3747 | #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \ | |
3748 | WXGRID_MIN_COL_WIDTH, \ | |
64e15340 | 3749 | m_colRights, m_numCols, TRUE) |
33188aa4 SN |
3750 | #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \ |
3751 | WXGRID_MIN_ROW_HEIGHT, \ | |
64e15340 | 3752 | m_rowBottoms, m_numRows, TRUE) |
33188aa4 | 3753 | ///////////////////////////////////////////////////////////////////// |
07296f0b | 3754 | |
2d66e025 MB |
3755 | IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow ) |
3756 | ||
3757 | BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow ) | |
f85afd4e MB |
3758 | EVT_PAINT( wxGrid::OnPaint ) |
3759 | EVT_SIZE( wxGrid::OnSize ) | |
f85afd4e | 3760 | EVT_KEY_DOWN( wxGrid::OnKeyDown ) |
f6bcfd97 | 3761 | EVT_KEY_UP( wxGrid::OnKeyUp ) |
2796cce3 | 3762 | EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground ) |
f85afd4e | 3763 | END_EVENT_TABLE() |
8f177c8e | 3764 | |
2d66e025 MB |
3765 | wxGrid::wxGrid( wxWindow *parent, |
3766 | wxWindowID id, | |
3767 | const wxPoint& pos, | |
3768 | const wxSize& size, | |
3769 | long style, | |
3770 | const wxString& name ) | |
ebd773c6 | 3771 | : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ), |
af547d51 VZ |
3772 | m_colMinWidths(GRID_HASH_SIZE), |
3773 | m_rowMinHeights(GRID_HASH_SIZE) | |
2d66e025 MB |
3774 | { |
3775 | Create(); | |
58dd5b3b MB |
3776 | } |
3777 | ||
3778 | ||
3779 | wxGrid::~wxGrid() | |
3780 | { | |
606b005f JS |
3781 | // Must do this or ~wxScrollHelper will pop the wrong event handler |
3782 | SetTargetWindow(this); | |
0a976765 | 3783 | ClearAttrCache(); |
39bcce60 | 3784 | wxSafeDecRef(m_defaultCellAttr); |
0a976765 VZ |
3785 | |
3786 | #ifdef DEBUG_ATTR_CACHE | |
3787 | size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses; | |
3788 | wxPrintf(_T("wxGrid attribute cache statistics: " | |
3789 | "total: %u, hits: %u (%u%%)\n"), | |
3790 | total, gs_nAttrCacheHits, | |
3791 | total ? (gs_nAttrCacheHits*100) / total : 0); | |
3792 | #endif | |
3793 | ||
2796cce3 RD |
3794 | if (m_ownTable) |
3795 | delete m_table; | |
f2d76237 RD |
3796 | |
3797 | delete m_typeRegistry; | |
b5808881 | 3798 | delete m_selection; |
58dd5b3b MB |
3799 | } |
3800 | ||
2d66e025 | 3801 | |
58dd5b3b MB |
3802 | // |
3803 | // ----- internal init and update functions | |
3804 | // | |
3805 | ||
3806 | void wxGrid::Create() | |
f0102d2a | 3807 | { |
4634a5d6 | 3808 | m_created = FALSE; // set to TRUE by CreateGrid |
4634a5d6 MB |
3809 | |
3810 | m_table = (wxGridTableBase *) NULL; | |
2796cce3 | 3811 | m_ownTable = FALSE; |
2c9a89e0 RD |
3812 | |
3813 | m_cellEditCtrlEnabled = FALSE; | |
4634a5d6 | 3814 | |
ccd970b1 | 3815 | m_defaultCellAttr = new wxGridCellAttr(); |
f2d76237 RD |
3816 | |
3817 | // Set default cell attributes | |
ccd970b1 | 3818 | m_defaultCellAttr->SetDefAttr(m_defaultCellAttr); |
19d7140e | 3819 | m_defaultCellAttr->SetKind(wxGridCellAttr::Default); |
f2d76237 | 3820 | m_defaultCellAttr->SetFont(GetFont()); |
4c7277db | 3821 | m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP); |
f2d76237 | 3822 | m_defaultCellAttr->SetTextColour( |
a756f210 | 3823 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); |
f2d76237 | 3824 | m_defaultCellAttr->SetBackgroundColour( |
a756f210 | 3825 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f2d76237 RD |
3826 | m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer); |
3827 | m_defaultCellAttr->SetEditor(new wxGridCellTextEditor); | |
2796cce3 RD |
3828 | |
3829 | ||
4634a5d6 MB |
3830 | m_numRows = 0; |
3831 | m_numCols = 0; | |
3832 | m_currentCellCoords = wxGridNoCellCoords; | |
b99be8fb | 3833 | |
18f9565d MB |
3834 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; |
3835 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
2d66e025 | 3836 | |
c4608a8a | 3837 | // create the type registry |
f2d76237 | 3838 | m_typeRegistry = new wxGridTypeRegistry; |
3f3dc2ef VZ |
3839 | m_selection = NULL; |
3840 | ||
f2d76237 | 3841 | // subwindow components that make up the wxGrid |
7807d81c MB |
3842 | m_cornerLabelWin = new wxGridCornerLabelWindow( this, |
3843 | -1, | |
18f9565d MB |
3844 | wxDefaultPosition, |
3845 | wxDefaultSize ); | |
7807d81c | 3846 | |
60ff3b99 VZ |
3847 | m_rowLabelWin = new wxGridRowLabelWindow( this, |
3848 | -1, | |
18f9565d MB |
3849 | wxDefaultPosition, |
3850 | wxDefaultSize ); | |
2d66e025 MB |
3851 | |
3852 | m_colLabelWin = new wxGridColLabelWindow( this, | |
3853 | -1, | |
18f9565d MB |
3854 | wxDefaultPosition, |
3855 | wxDefaultSize ); | |
60ff3b99 | 3856 | |
60ff3b99 VZ |
3857 | m_gridWin = new wxGridWindow( this, |
3858 | m_rowLabelWin, | |
3859 | m_colLabelWin, | |
3860 | -1, | |
18f9565d | 3861 | wxDefaultPosition, |
2d66e025 MB |
3862 | wxDefaultSize ); |
3863 | ||
3864 | SetTargetWindow( m_gridWin ); | |
6f36917b VZ |
3865 | |
3866 | Init(); | |
2d66e025 | 3867 | } |
f85afd4e | 3868 | |
2d66e025 | 3869 | |
b5808881 SN |
3870 | bool wxGrid::CreateGrid( int numRows, int numCols, |
3871 | wxGrid::wxGridSelectionModes selmode ) | |
2d66e025 | 3872 | { |
f6bcfd97 BP |
3873 | wxCHECK_MSG( !m_created, |
3874 | FALSE, | |
3875 | wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); | |
3876 | ||
3877 | m_numRows = numRows; | |
3878 | m_numCols = numCols; | |
3879 | ||
3880 | m_table = new wxGridStringTable( m_numRows, m_numCols ); | |
3881 | m_table->SetView( this ); | |
3882 | m_ownTable = TRUE; | |
3883 | m_selection = new wxGridSelection( this, selmode ); | |
6f36917b VZ |
3884 | |
3885 | CalcDimensions(); | |
3886 | ||
f6bcfd97 | 3887 | m_created = TRUE; |
2d66e025 | 3888 | |
2796cce3 RD |
3889 | return m_created; |
3890 | } | |
3891 | ||
f1567cdd SN |
3892 | void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode) |
3893 | { | |
6f36917b VZ |
3894 | wxCHECK_RET( m_created, |
3895 | wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") ); | |
3896 | ||
3897 | m_selection->SetSelectionMode( selmode ); | |
f1567cdd SN |
3898 | } |
3899 | ||
aa5b8857 SN |
3900 | wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const |
3901 | { | |
3902 | wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells, | |
3903 | wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") ); | |
3904 | ||
3905 | return m_selection->GetSelectionMode(); | |
3906 | } | |
3907 | ||
043d16b2 SN |
3908 | bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership, |
3909 | wxGrid::wxGridSelectionModes selmode ) | |
2796cce3 RD |
3910 | { |
3911 | if ( m_created ) | |
3912 | { | |
fb295790 | 3913 | // RD: Actually, this should probably be allowed. I think it would be |
3f3dc2ef VZ |
3914 | // nice to be able to switch multiple Tables in and out of a single |
3915 | // View at runtime. Is there anything in the implementation that | |
3916 | // would prevent this? | |
fb295790 | 3917 | |
d95b0c2b | 3918 | // At least, you now have to cope with m_selection |
2796cce3 RD |
3919 | wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); |
3920 | return FALSE; | |
3921 | } | |
3922 | else | |
3923 | { | |
3924 | m_numRows = table->GetNumberRows(); | |
3925 | m_numCols = table->GetNumberCols(); | |
3926 | ||
3927 | m_table = table; | |
3928 | m_table->SetView( this ); | |
3929 | if (takeOwnership) | |
3930 | m_ownTable = TRUE; | |
043d16b2 | 3931 | m_selection = new wxGridSelection( this, selmode ); |
6f36917b VZ |
3932 | |
3933 | CalcDimensions(); | |
3934 | ||
2d66e025 MB |
3935 | m_created = TRUE; |
3936 | } | |
f85afd4e | 3937 | |
2d66e025 | 3938 | return m_created; |
f85afd4e MB |
3939 | } |
3940 | ||
2d66e025 | 3941 | |
f85afd4e MB |
3942 | void wxGrid::Init() |
3943 | { | |
f85afd4e MB |
3944 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; |
3945 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
3946 | ||
60ff3b99 VZ |
3947 | if ( m_rowLabelWin ) |
3948 | { | |
3949 | m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour(); | |
3950 | } | |
3951 | else | |
3952 | { | |
3953 | m_labelBackgroundColour = wxColour( _T("WHITE") ); | |
3954 | } | |
3955 | ||
3956 | m_labelTextColour = wxColour( _T("BLACK") ); | |
f85afd4e | 3957 | |
0a976765 VZ |
3958 | // init attr cache |
3959 | m_attrCache.row = -1; | |
2b5f62a0 VZ |
3960 | m_attrCache.col = -1; |
3961 | m_attrCache.attr = NULL; | |
0a976765 | 3962 | |
f85afd4e MB |
3963 | // TODO: something better than this ? |
3964 | // | |
3965 | m_labelFont = this->GetFont(); | |
52d6f640 | 3966 | m_labelFont.SetWeight( wxBOLD ); |
8f177c8e | 3967 | |
73145b0e | 3968 | m_rowLabelHorizAlign = wxALIGN_CENTRE; |
4c7277db | 3969 | m_rowLabelVertAlign = wxALIGN_CENTRE; |
f85afd4e | 3970 | |
4c7277db | 3971 | m_colLabelHorizAlign = wxALIGN_CENTRE; |
73145b0e | 3972 | m_colLabelVertAlign = wxALIGN_CENTRE; |
d43851f7 | 3973 | m_colLabelTextOrientation = wxHORIZONTAL; |
f85afd4e | 3974 | |
f85afd4e | 3975 | m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH; |
1f1ce288 MB |
3976 | m_defaultRowHeight = m_gridWin->GetCharHeight(); |
3977 | ||
d2fdd8d2 | 3978 | #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl() |
1f1ce288 MB |
3979 | m_defaultRowHeight += 8; |
3980 | #else | |
3981 | m_defaultRowHeight += 4; | |
3982 | #endif | |
3983 | ||
73145b0e | 3984 | m_gridLineColour = wxColour( 192,192,192 ); |
f85afd4e | 3985 | m_gridLinesEnabled = TRUE; |
73145b0e | 3986 | m_cellHighlightColour = *wxBLACK; |
bf7945ce | 3987 | m_cellHighlightPenWidth = 2; |
d2520c85 | 3988 | m_cellHighlightROPenWidth = 1; |
8f177c8e | 3989 | |
58dd5b3b | 3990 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
e2b42eeb | 3991 | m_winCapture = (wxWindow *)NULL; |
6e8524b1 MB |
3992 | m_canDragRowSize = TRUE; |
3993 | m_canDragColSize = TRUE; | |
4cfa5de6 | 3994 | m_canDragGridSize = TRUE; |
f85afd4e MB |
3995 | m_dragLastPos = -1; |
3996 | m_dragRowOrCol = -1; | |
3997 | m_isDragging = FALSE; | |
07296f0b | 3998 | m_startDragPos = wxDefaultPosition; |
f85afd4e | 3999 | |
07296f0b | 4000 | m_waitForSlowClick = FALSE; |
025562fe | 4001 | |
f85afd4e MB |
4002 | m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS ); |
4003 | m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE ); | |
4004 | ||
4005 | m_currentCellCoords = wxGridNoCellCoords; | |
f85afd4e | 4006 | |
b5808881 SN |
4007 | m_selectingTopLeft = wxGridNoCellCoords; |
4008 | m_selectingBottomRight = wxGridNoCellCoords; | |
d43851f7 JS |
4009 | m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); |
4010 | m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
8f177c8e | 4011 | |
f85afd4e MB |
4012 | m_editable = TRUE; // default for whole grid |
4013 | ||
2d66e025 MB |
4014 | m_inOnKeyDown = FALSE; |
4015 | m_batchCount = 0; | |
3c79cf49 | 4016 | |
266e8367 | 4017 | m_extraWidth = |
526dbb95 | 4018 | m_extraHeight = 0; |
7c1cb261 VZ |
4019 | } |
4020 | ||
4021 | // ---------------------------------------------------------------------------- | |
4022 | // the idea is to call these functions only when necessary because they create | |
4023 | // quite big arrays which eat memory mostly unnecessary - in particular, if | |
4024 | // default widths/heights are used for all rows/columns, we may not use these | |
4025 | // arrays at all | |
4026 | // | |
4027 | // with some extra code, it should be possible to only store the | |
4028 | // widths/heights different from default ones but this will be done later... | |
4029 | // ---------------------------------------------------------------------------- | |
4030 | ||
4031 | void wxGrid::InitRowHeights() | |
4032 | { | |
4033 | m_rowHeights.Empty(); | |
4034 | m_rowBottoms.Empty(); | |
4035 | ||
4036 | m_rowHeights.Alloc( m_numRows ); | |
4037 | m_rowBottoms.Alloc( m_numRows ); | |
4038 | ||
4039 | int rowBottom = 0; | |
2b5f62a0 | 4040 | |
27f35b66 SN |
4041 | m_rowHeights.Add( m_defaultRowHeight, m_numRows ); |
4042 | ||
7c1cb261 VZ |
4043 | for ( int i = 0; i < m_numRows; i++ ) |
4044 | { | |
7c1cb261 VZ |
4045 | rowBottom += m_defaultRowHeight; |
4046 | m_rowBottoms.Add( rowBottom ); | |
4047 | } | |
4048 | } | |
4049 | ||
4050 | void wxGrid::InitColWidths() | |
4051 | { | |
4052 | m_colWidths.Empty(); | |
4053 | m_colRights.Empty(); | |
4054 | ||
4055 | m_colWidths.Alloc( m_numCols ); | |
4056 | m_colRights.Alloc( m_numCols ); | |
4057 | int colRight = 0; | |
27f35b66 SN |
4058 | |
4059 | m_colWidths.Add( m_defaultColWidth, m_numCols ); | |
4060 | ||
7c1cb261 VZ |
4061 | for ( int i = 0; i < m_numCols; i++ ) |
4062 | { | |
7c1cb261 VZ |
4063 | colRight += m_defaultColWidth; |
4064 | m_colRights.Add( colRight ); | |
4065 | } | |
4066 | } | |
4067 | ||
4068 | int wxGrid::GetColWidth(int col) const | |
4069 | { | |
4070 | return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col]; | |
4071 | } | |
4072 | ||
4073 | int wxGrid::GetColLeft(int col) const | |
4074 | { | |
4075 | return m_colRights.IsEmpty() ? col * m_defaultColWidth | |
4076 | : m_colRights[col] - m_colWidths[col]; | |
4077 | } | |
4078 | ||
4079 | int wxGrid::GetColRight(int col) const | |
4080 | { | |
4081 | return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth | |
4082 | : m_colRights[col]; | |
4083 | } | |
4084 | ||
4085 | int wxGrid::GetRowHeight(int row) const | |
4086 | { | |
4087 | return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row]; | |
4088 | } | |
2d66e025 | 4089 | |
7c1cb261 VZ |
4090 | int wxGrid::GetRowTop(int row) const |
4091 | { | |
4092 | return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight | |
4093 | : m_rowBottoms[row] - m_rowHeights[row]; | |
f85afd4e MB |
4094 | } |
4095 | ||
7c1cb261 VZ |
4096 | int wxGrid::GetRowBottom(int row) const |
4097 | { | |
4098 | return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight | |
4099 | : m_rowBottoms[row]; | |
4100 | } | |
f85afd4e MB |
4101 | |
4102 | void wxGrid::CalcDimensions() | |
4103 | { | |
f85afd4e | 4104 | int cw, ch; |
2d66e025 | 4105 | GetClientSize( &cw, &ch ); |
f85afd4e | 4106 | |
faec5a43 | 4107 | if ( m_rowLabelWin->IsShown() ) |
ae1d0c6c VZ |
4108 | cw -= m_rowLabelWidth; |
4109 | if ( m_colLabelWin->IsShown() ) | |
faec5a43 | 4110 | ch -= m_colLabelHeight; |
60ff3b99 | 4111 | |
faec5a43 SN |
4112 | // grid total size |
4113 | int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0; | |
4114 | int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0; | |
4115 | ||
73145b0e JS |
4116 | // take into account editor if shown |
4117 | if( IsCellEditControlShown() ) | |
4118 | { | |
4119 | int w2, h2; | |
4120 | int r = m_currentCellCoords.GetRow(); | |
4121 | int c = m_currentCellCoords.GetCol(); | |
4122 | int x = GetColLeft(c); | |
4123 | int y = GetRowTop(r); | |
4124 | ||
4125 | // how big is the editor | |
4126 | wxGridCellAttr* attr = GetCellAttr(r, c); | |
4127 | wxGridCellEditor* editor = attr->GetEditor(this, r, c); | |
4128 | editor->GetControl()->GetSize(&w2, &h2); | |
4129 | w2 += x; | |
4130 | h2 += y; | |
4131 | if( w2 > w ) w = w2; | |
4132 | if( h2 > h ) h = h2; | |
4133 | editor->DecRef(); | |
4134 | attr->DecRef(); | |
4135 | } | |
4136 | ||
faec5a43 SN |
4137 | // preserve (more or less) the previous position |
4138 | int x, y; | |
4139 | GetViewStart( &x, &y ); | |
97a9929e VZ |
4140 | |
4141 | // maybe we don't need scrollbars at all? | |
4142 | // | |
4143 | // also adjust the position to be valid for the new scroll rangs | |
faec5a43 SN |
4144 | if ( w <= cw ) |
4145 | { | |
97a9929e | 4146 | w = x = 0; |
faec5a43 SN |
4147 | } |
4148 | else | |
4149 | { | |
edb89f7e VZ |
4150 | if ( x >= w ) |
4151 | x = w - 1; | |
f85afd4e | 4152 | } |
97a9929e | 4153 | |
faec5a43 SN |
4154 | if ( h <= ch ) |
4155 | { | |
97a9929e | 4156 | h = y = 0; |
faec5a43 SN |
4157 | } |
4158 | else | |
4159 | { | |
edb89f7e VZ |
4160 | if ( y >= h ) |
4161 | y = h - 1; | |
faec5a43 SN |
4162 | } |
4163 | ||
4164 | // do set scrollbar parameters | |
97a9929e VZ |
4165 | SetScrollbars( GRID_SCROLL_LINE_X, GRID_SCROLL_LINE_Y, |
4166 | GetScrollX(w), GetScrollY(h), x, y, | |
4167 | GetBatchCount() != 0); | |
12314291 VZ |
4168 | |
4169 | // if our OnSize() hadn't been called (it would if we have scrollbars), we | |
4170 | // still must reposition the children | |
4171 | CalcWindowSizes(); | |
f85afd4e MB |
4172 | } |
4173 | ||
4174 | ||
7807d81c MB |
4175 | void wxGrid::CalcWindowSizes() |
4176 | { | |
4177 | int cw, ch; | |
4178 | GetClientSize( &cw, &ch ); | |
b99be8fb | 4179 | |
7807d81c MB |
4180 | if ( m_cornerLabelWin->IsShown() ) |
4181 | m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight ); | |
4182 | ||
4183 | if ( m_colLabelWin->IsShown() ) | |
4184 | m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight); | |
4185 | ||
4186 | if ( m_rowLabelWin->IsShown() ) | |
4187 | m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight); | |
4188 | ||
4189 | if ( m_gridWin->IsShown() ) | |
4190 | m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight); | |
4191 | } | |
4192 | ||
4193 | ||
f85afd4e MB |
4194 | // this is called when the grid table sends a message to say that it |
4195 | // has been redimensioned | |
4196 | // | |
4197 | bool wxGrid::Redimension( wxGridTableMessage& msg ) | |
4198 | { | |
4199 | int i; | |
f6bcfd97 | 4200 | bool result = FALSE; |
8f177c8e | 4201 | |
a6794685 SN |
4202 | // Clear the attribute cache as the attribute might refer to a different |
4203 | // cell than stored in the cache after adding/removing rows/columns. | |
4204 | ClearAttrCache(); | |
7e48d7d9 SN |
4205 | // By the same reasoning, the editor should be dismissed if columns are |
4206 | // added or removed. And for consistency, it should IMHO always be | |
4207 | // removed, not only if the cell "underneath" it actually changes. | |
4208 | // For now, I intentionally do not save the editor's content as the | |
4209 | // cell it might want to save that stuff to might no longer exist. | |
bca7bfc8 | 4210 | HideCellEditControl(); |
f6bcfd97 | 4211 | #if 0 |
7c1cb261 VZ |
4212 | // if we were using the default widths/heights so far, we must change them |
4213 | // now | |
4214 | if ( m_colWidths.IsEmpty() ) | |
4215 | { | |
4216 | InitColWidths(); | |
4217 | } | |
4218 | ||
4219 | if ( m_rowHeights.IsEmpty() ) | |
4220 | { | |
4221 | InitRowHeights(); | |
4222 | } | |
f6bcfd97 | 4223 | #endif |
7c1cb261 | 4224 | |
f85afd4e MB |
4225 | switch ( msg.GetId() ) |
4226 | { | |
4227 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
4228 | { | |
4229 | size_t pos = msg.GetCommandInt(); | |
4230 | int numRows = msg.GetCommandInt2(); | |
f6bcfd97 | 4231 | |
f85afd4e | 4232 | m_numRows += numRows; |
2d66e025 | 4233 | |
f6bcfd97 BP |
4234 | if ( !m_rowHeights.IsEmpty() ) |
4235 | { | |
27f35b66 SN |
4236 | m_rowHeights.Insert( m_defaultRowHeight, pos, numRows ); |
4237 | m_rowBottoms.Insert( 0, pos, numRows ); | |
f6bcfd97 BP |
4238 | |
4239 | int bottom = 0; | |
4240 | if ( pos > 0 ) bottom = m_rowBottoms[pos-1]; | |
60ff3b99 | 4241 | |
f6bcfd97 BP |
4242 | for ( i = pos; i < m_numRows; i++ ) |
4243 | { | |
4244 | bottom += m_rowHeights[i]; | |
4245 | m_rowBottoms[i] = bottom; | |
4246 | } | |
4247 | } | |
4248 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4249 | { | |
4250 | // if we have just inserted cols into an empty grid the current | |
4251 | // cell will be undefined... | |
4252 | // | |
4253 | SetCurrentCell( 0, 0 ); | |
4254 | } | |
3f3dc2ef VZ |
4255 | |
4256 | if ( m_selection ) | |
4257 | m_selection->UpdateRows( pos, numRows ); | |
f6bcfd97 BP |
4258 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4259 | if (attrProvider) | |
4260 | attrProvider->UpdateAttrRows( pos, numRows ); | |
4261 | ||
4262 | if ( !GetBatchCount() ) | |
2d66e025 | 4263 | { |
f6bcfd97 BP |
4264 | CalcDimensions(); |
4265 | m_rowLabelWin->Refresh(); | |
2d66e025 | 4266 | } |
f85afd4e | 4267 | } |
f6bcfd97 BP |
4268 | result = TRUE; |
4269 | break; | |
f85afd4e MB |
4270 | |
4271 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
4272 | { | |
4273 | int numRows = msg.GetCommandInt(); | |
2d66e025 | 4274 | int oldNumRows = m_numRows; |
f85afd4e | 4275 | m_numRows += numRows; |
2d66e025 | 4276 | |
f6bcfd97 BP |
4277 | if ( !m_rowHeights.IsEmpty() ) |
4278 | { | |
27f35b66 SN |
4279 | m_rowHeights.Add( m_defaultRowHeight, numRows ); |
4280 | m_rowBottoms.Add( 0, numRows ); | |
60ff3b99 | 4281 | |
f6bcfd97 BP |
4282 | int bottom = 0; |
4283 | if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1]; | |
4284 | ||
4285 | for ( i = oldNumRows; i < m_numRows; i++ ) | |
4286 | { | |
4287 | bottom += m_rowHeights[i]; | |
4288 | m_rowBottoms[i] = bottom; | |
4289 | } | |
4290 | } | |
4291 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4292 | { | |
4293 | // if we have just inserted cols into an empty grid the current | |
4294 | // cell will be undefined... | |
4295 | // | |
4296 | SetCurrentCell( 0, 0 ); | |
4297 | } | |
4298 | if ( !GetBatchCount() ) | |
2d66e025 | 4299 | { |
f6bcfd97 BP |
4300 | CalcDimensions(); |
4301 | m_rowLabelWin->Refresh(); | |
2d66e025 | 4302 | } |
f85afd4e | 4303 | } |
f6bcfd97 BP |
4304 | result = TRUE; |
4305 | break; | |
f85afd4e MB |
4306 | |
4307 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
4308 | { | |
4309 | size_t pos = msg.GetCommandInt(); | |
4310 | int numRows = msg.GetCommandInt2(); | |
f85afd4e MB |
4311 | m_numRows -= numRows; |
4312 | ||
f6bcfd97 | 4313 | if ( !m_rowHeights.IsEmpty() ) |
f85afd4e | 4314 | { |
27f35b66 SN |
4315 | m_rowHeights.RemoveAt( pos, numRows ); |
4316 | m_rowBottoms.RemoveAt( pos, numRows ); | |
2d66e025 MB |
4317 | |
4318 | int h = 0; | |
4319 | for ( i = 0; i < m_numRows; i++ ) | |
4320 | { | |
4321 | h += m_rowHeights[i]; | |
4322 | m_rowBottoms[i] = h; | |
4323 | } | |
f85afd4e | 4324 | } |
f6bcfd97 BP |
4325 | if ( !m_numRows ) |
4326 | { | |
4327 | m_currentCellCoords = wxGridNoCellCoords; | |
4328 | } | |
4329 | else | |
4330 | { | |
4331 | if ( m_currentCellCoords.GetRow() >= m_numRows ) | |
4332 | m_currentCellCoords.Set( 0, 0 ); | |
4333 | } | |
3f3dc2ef VZ |
4334 | |
4335 | if ( m_selection ) | |
4336 | m_selection->UpdateRows( pos, -((int)numRows) ); | |
f6bcfd97 BP |
4337 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4338 | if (attrProvider) { | |
4339 | attrProvider->UpdateAttrRows( pos, -((int)numRows) ); | |
84912ef8 RD |
4340 | // ifdef'd out following patch from Paul Gammans |
4341 | #if 0 | |
3ca6a5f0 | 4342 | // No need to touch column attributes, unless we |
f6bcfd97 BP |
4343 | // removed _all_ rows, in this case, we remove |
4344 | // all column attributes. | |
4345 | // I hate to do this here, but the | |
4346 | // needed data is not available inside UpdateAttrRows. | |
4347 | if ( !GetNumberRows() ) | |
4348 | attrProvider->UpdateAttrCols( 0, -GetNumberCols() ); | |
84912ef8 | 4349 | #endif |
f6bcfd97 BP |
4350 | } |
4351 | if ( !GetBatchCount() ) | |
4352 | { | |
4353 | CalcDimensions(); | |
4354 | m_rowLabelWin->Refresh(); | |
4355 | } | |
f85afd4e | 4356 | } |
f6bcfd97 BP |
4357 | result = TRUE; |
4358 | break; | |
f85afd4e MB |
4359 | |
4360 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
4361 | { | |
4362 | size_t pos = msg.GetCommandInt(); | |
4363 | int numCols = msg.GetCommandInt2(); | |
f85afd4e | 4364 | m_numCols += numCols; |
2d66e025 | 4365 | |
f6bcfd97 BP |
4366 | if ( !m_colWidths.IsEmpty() ) |
4367 | { | |
27f35b66 SN |
4368 | m_colWidths.Insert( m_defaultColWidth, pos, numCols ); |
4369 | m_colRights.Insert( 0, pos, numCols ); | |
f6bcfd97 BP |
4370 | |
4371 | int right = 0; | |
4372 | if ( pos > 0 ) right = m_colRights[pos-1]; | |
60ff3b99 | 4373 | |
f6bcfd97 BP |
4374 | for ( i = pos; i < m_numCols; i++ ) |
4375 | { | |
4376 | right += m_colWidths[i]; | |
4377 | m_colRights[i] = right; | |
4378 | } | |
4379 | } | |
4380 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
2d66e025 | 4381 | { |
f6bcfd97 BP |
4382 | // if we have just inserted cols into an empty grid the current |
4383 | // cell will be undefined... | |
4384 | // | |
4385 | SetCurrentCell( 0, 0 ); | |
2d66e025 | 4386 | } |
3f3dc2ef VZ |
4387 | |
4388 | if ( m_selection ) | |
4389 | m_selection->UpdateCols( pos, numCols ); | |
f6bcfd97 BP |
4390 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4391 | if (attrProvider) | |
4392 | attrProvider->UpdateAttrCols( pos, numCols ); | |
4393 | if ( !GetBatchCount() ) | |
4394 | { | |
4395 | CalcDimensions(); | |
4396 | m_colLabelWin->Refresh(); | |
4397 | } | |
4398 | ||
f85afd4e | 4399 | } |
f6bcfd97 BP |
4400 | result = TRUE; |
4401 | break; | |
f85afd4e MB |
4402 | |
4403 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
4404 | { | |
4405 | int numCols = msg.GetCommandInt(); | |
2d66e025 | 4406 | int oldNumCols = m_numCols; |
f85afd4e | 4407 | m_numCols += numCols; |
f6bcfd97 BP |
4408 | if ( !m_colWidths.IsEmpty() ) |
4409 | { | |
27f35b66 SN |
4410 | m_colWidths.Add( m_defaultColWidth, numCols ); |
4411 | m_colRights.Add( 0, numCols ); | |
2d66e025 | 4412 | |
f6bcfd97 BP |
4413 | int right = 0; |
4414 | if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1]; | |
60ff3b99 | 4415 | |
f6bcfd97 BP |
4416 | for ( i = oldNumCols; i < m_numCols; i++ ) |
4417 | { | |
4418 | right += m_colWidths[i]; | |
4419 | m_colRights[i] = right; | |
4420 | } | |
4421 | } | |
4422 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
2d66e025 | 4423 | { |
f6bcfd97 BP |
4424 | // if we have just inserted cols into an empty grid the current |
4425 | // cell will be undefined... | |
4426 | // | |
4427 | SetCurrentCell( 0, 0 ); | |
4428 | } | |
4429 | if ( !GetBatchCount() ) | |
4430 | { | |
4431 | CalcDimensions(); | |
4432 | m_colLabelWin->Refresh(); | |
2d66e025 | 4433 | } |
f85afd4e | 4434 | } |
f6bcfd97 BP |
4435 | result = TRUE; |
4436 | break; | |
f85afd4e MB |
4437 | |
4438 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
4439 | { | |
4440 | size_t pos = msg.GetCommandInt(); | |
4441 | int numCols = msg.GetCommandInt2(); | |
f85afd4e | 4442 | m_numCols -= numCols; |
f85afd4e | 4443 | |
f6bcfd97 | 4444 | if ( !m_colWidths.IsEmpty() ) |
f85afd4e | 4445 | { |
27f35b66 SN |
4446 | m_colWidths.RemoveAt( pos, numCols ); |
4447 | m_colRights.RemoveAt( pos, numCols ); | |
2d66e025 MB |
4448 | |
4449 | int w = 0; | |
4450 | for ( i = 0; i < m_numCols; i++ ) | |
4451 | { | |
4452 | w += m_colWidths[i]; | |
4453 | m_colRights[i] = w; | |
4454 | } | |
f85afd4e | 4455 | } |
f6bcfd97 BP |
4456 | if ( !m_numCols ) |
4457 | { | |
4458 | m_currentCellCoords = wxGridNoCellCoords; | |
4459 | } | |
4460 | else | |
4461 | { | |
4462 | if ( m_currentCellCoords.GetCol() >= m_numCols ) | |
4463 | m_currentCellCoords.Set( 0, 0 ); | |
4464 | } | |
3f3dc2ef VZ |
4465 | |
4466 | if ( m_selection ) | |
4467 | m_selection->UpdateCols( pos, -((int)numCols) ); | |
f6bcfd97 BP |
4468 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4469 | if (attrProvider) { | |
4470 | attrProvider->UpdateAttrCols( pos, -((int)numCols) ); | |
84912ef8 RD |
4471 | // ifdef'd out following patch from Paul Gammans |
4472 | #if 0 | |
f6bcfd97 BP |
4473 | // No need to touch row attributes, unless we |
4474 | // removed _all_ columns, in this case, we remove | |
4475 | // all row attributes. | |
4476 | // I hate to do this here, but the | |
4477 | // needed data is not available inside UpdateAttrCols. | |
4478 | if ( !GetNumberCols() ) | |
4479 | attrProvider->UpdateAttrRows( 0, -GetNumberRows() ); | |
84912ef8 | 4480 | #endif |
f6bcfd97 BP |
4481 | } |
4482 | if ( !GetBatchCount() ) | |
4483 | { | |
4484 | CalcDimensions(); | |
4485 | m_colLabelWin->Refresh(); | |
4486 | } | |
f85afd4e | 4487 | } |
faec5a43 | 4488 | result = TRUE; |
f6bcfd97 | 4489 | break; |
f85afd4e MB |
4490 | } |
4491 | ||
f6bcfd97 BP |
4492 | if (result && !GetBatchCount() ) |
4493 | m_gridWin->Refresh(); | |
4494 | return result; | |
f85afd4e MB |
4495 | } |
4496 | ||
4497 | ||
d10f4bf9 | 4498 | wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) |
f85afd4e | 4499 | { |
2d66e025 MB |
4500 | wxRegionIterator iter( reg ); |
4501 | wxRect r; | |
f85afd4e | 4502 | |
275c4ae4 RD |
4503 | wxArrayInt rowlabels; |
4504 | ||
2d66e025 MB |
4505 | int top, bottom; |
4506 | while ( iter ) | |
f85afd4e | 4507 | { |
2d66e025 | 4508 | r = iter.GetRect(); |
f85afd4e | 4509 | |
2d66e025 MB |
4510 | // TODO: remove this when we can... |
4511 | // There is a bug in wxMotif that gives garbage update | |
4512 | // rectangles if you jump-scroll a long way by clicking the | |
4513 | // scrollbar with middle button. This is a work-around | |
4514 | // | |
4515 | #if defined(__WXMOTIF__) | |
4516 | int cw, ch; | |
4517 | m_gridWin->GetClientSize( &cw, &ch ); | |
4518 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
4519 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
4520 | #endif | |
f85afd4e | 4521 | |
2d66e025 MB |
4522 | // logical bounds of update region |
4523 | // | |
4524 | int dummy; | |
4525 | CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top ); | |
4526 | CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom ); | |
4527 | ||
4528 | // find the row labels within these bounds | |
4529 | // | |
4530 | int row; | |
33188aa4 | 4531 | for ( row = internalYToRow(top); row < m_numRows; row++ ) |
2d66e025 | 4532 | { |
7c1cb261 VZ |
4533 | if ( GetRowBottom(row) < top ) |
4534 | continue; | |
2d66e025 | 4535 | |
6d55126d | 4536 | if ( GetRowTop(row) > bottom ) |
7c1cb261 | 4537 | break; |
60ff3b99 | 4538 | |
d10f4bf9 | 4539 | rowlabels.Add( row ); |
2d66e025 | 4540 | } |
60ff3b99 | 4541 | |
2d66e025 | 4542 | iter++ ; |
f85afd4e | 4543 | } |
d10f4bf9 VZ |
4544 | |
4545 | return rowlabels; | |
f85afd4e MB |
4546 | } |
4547 | ||
4548 | ||
d10f4bf9 | 4549 | wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) |
f85afd4e | 4550 | { |
2d66e025 MB |
4551 | wxRegionIterator iter( reg ); |
4552 | wxRect r; | |
f85afd4e | 4553 | |
d10f4bf9 | 4554 | wxArrayInt colLabels; |
f85afd4e | 4555 | |
2d66e025 MB |
4556 | int left, right; |
4557 | while ( iter ) | |
f85afd4e | 4558 | { |
2d66e025 | 4559 | r = iter.GetRect(); |
f85afd4e | 4560 | |
2d66e025 MB |
4561 | // TODO: remove this when we can... |
4562 | // There is a bug in wxMotif that gives garbage update | |
4563 | // rectangles if you jump-scroll a long way by clicking the | |
4564 | // scrollbar with middle button. This is a work-around | |
4565 | // | |
4566 | #if defined(__WXMOTIF__) | |
4567 | int cw, ch; | |
4568 | m_gridWin->GetClientSize( &cw, &ch ); | |
4569 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
4570 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
4571 | #endif | |
4572 | ||
4573 | // logical bounds of update region | |
4574 | // | |
4575 | int dummy; | |
4576 | CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy ); | |
4577 | CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy ); | |
4578 | ||
4579 | // find the cells within these bounds | |
4580 | // | |
4581 | int col; | |
33188aa4 | 4582 | for ( col = internalXToCol(left); col < m_numCols; col++ ) |
2d66e025 | 4583 | { |
7c1cb261 VZ |
4584 | if ( GetColRight(col) < left ) |
4585 | continue; | |
60ff3b99 | 4586 | |
7c1cb261 VZ |
4587 | if ( GetColLeft(col) > right ) |
4588 | break; | |
2d66e025 | 4589 | |
d10f4bf9 | 4590 | colLabels.Add( col ); |
2d66e025 | 4591 | } |
60ff3b99 | 4592 | |
2d66e025 | 4593 | iter++ ; |
f85afd4e | 4594 | } |
d10f4bf9 | 4595 | return colLabels; |
f85afd4e MB |
4596 | } |
4597 | ||
4598 | ||
d10f4bf9 | 4599 | wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) |
f85afd4e | 4600 | { |
2d66e025 MB |
4601 | wxRegionIterator iter( reg ); |
4602 | wxRect r; | |
f85afd4e | 4603 | |
d10f4bf9 | 4604 | wxGridCellCoordsArray cellsExposed; |
f85afd4e | 4605 | |
2d66e025 MB |
4606 | int left, top, right, bottom; |
4607 | while ( iter ) | |
4608 | { | |
4609 | r = iter.GetRect(); | |
f85afd4e | 4610 | |
2d66e025 MB |
4611 | // TODO: remove this when we can... |
4612 | // There is a bug in wxMotif that gives garbage update | |
4613 | // rectangles if you jump-scroll a long way by clicking the | |
4614 | // scrollbar with middle button. This is a work-around | |
4615 | // | |
4616 | #if defined(__WXMOTIF__) | |
f85afd4e | 4617 | int cw, ch; |
2d66e025 MB |
4618 | m_gridWin->GetClientSize( &cw, &ch ); |
4619 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
4620 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
4621 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
4622 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
4623 | #endif | |
8f177c8e | 4624 | |
2d66e025 MB |
4625 | // logical bounds of update region |
4626 | // | |
4627 | CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
4628 | CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
f85afd4e | 4629 | |
2d66e025 | 4630 | // find the cells within these bounds |
f85afd4e | 4631 | // |
2d66e025 | 4632 | int row, col; |
33188aa4 | 4633 | for ( row = internalYToRow(top); row < m_numRows; row++ ) |
f85afd4e | 4634 | { |
7c1cb261 VZ |
4635 | if ( GetRowBottom(row) <= top ) |
4636 | continue; | |
f85afd4e | 4637 | |
7c1cb261 VZ |
4638 | if ( GetRowTop(row) > bottom ) |
4639 | break; | |
60ff3b99 | 4640 | |
33188aa4 | 4641 | for ( col = internalXToCol(left); col < m_numCols; col++ ) |
2d66e025 | 4642 | { |
7c1cb261 VZ |
4643 | if ( GetColRight(col) <= left ) |
4644 | continue; | |
60ff3b99 | 4645 | |
7c1cb261 VZ |
4646 | if ( GetColLeft(col) > right ) |
4647 | break; | |
60ff3b99 | 4648 | |
d10f4bf9 | 4649 | cellsExposed.Add( wxGridCellCoords( row, col ) ); |
2d66e025 MB |
4650 | } |
4651 | } | |
60ff3b99 | 4652 | |
7c1cb261 | 4653 | iter++; |
f85afd4e | 4654 | } |
d10f4bf9 VZ |
4655 | |
4656 | return cellsExposed; | |
f85afd4e MB |
4657 | } |
4658 | ||
4659 | ||
2d66e025 | 4660 | void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 4661 | { |
2d66e025 MB |
4662 | int x, y, row; |
4663 | wxPoint pos( event.GetPosition() ); | |
4664 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 4665 | |
2d66e025 | 4666 | if ( event.Dragging() ) |
f85afd4e | 4667 | { |
426b2d87 JS |
4668 | if (!m_isDragging) |
4669 | { | |
4670 | m_isDragging = TRUE; | |
4671 | m_rowLabelWin->CaptureMouse(); | |
4672 | } | |
8f177c8e | 4673 | |
2d66e025 | 4674 | if ( event.LeftIsDown() ) |
f85afd4e MB |
4675 | { |
4676 | switch( m_cursorMode ) | |
4677 | { | |
f85afd4e MB |
4678 | case WXGRID_CURSOR_RESIZE_ROW: |
4679 | { | |
2d66e025 MB |
4680 | int cw, ch, left, dummy; |
4681 | m_gridWin->GetClientSize( &cw, &ch ); | |
4682 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
60ff3b99 | 4683 | |
2d66e025 MB |
4684 | wxClientDC dc( m_gridWin ); |
4685 | PrepareDC( dc ); | |
af547d51 VZ |
4686 | y = wxMax( y, |
4687 | GetRowTop(m_dragRowOrCol) + | |
4688 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
d2fdd8d2 | 4689 | dc.SetLogicalFunction(wxINVERT); |
f85afd4e MB |
4690 | if ( m_dragLastPos >= 0 ) |
4691 | { | |
2d66e025 | 4692 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); |
f85afd4e | 4693 | } |
2d66e025 MB |
4694 | dc.DrawLine( left, y, left+cw, y ); |
4695 | m_dragLastPos = y; | |
f85afd4e MB |
4696 | } |
4697 | break; | |
4698 | ||
4699 | case WXGRID_CURSOR_SELECT_ROW: | |
e32352cf | 4700 | if ( (row = YToRow( y )) >= 0 ) |
aa5e1f75 | 4701 | { |
3f3dc2ef VZ |
4702 | if ( m_selection ) |
4703 | { | |
4704 | m_selection->SelectRow( row, | |
4705 | event.ControlDown(), | |
4706 | event.ShiftDown(), | |
4707 | event.AltDown(), | |
4708 | event.MetaDown() ); | |
4709 | } | |
f85afd4e | 4710 | } |
e2b42eeb VZ |
4711 | |
4712 | // default label to suppress warnings about "enumeration value | |
4713 | // 'xxx' not handled in switch | |
4714 | default: | |
4715 | break; | |
f85afd4e MB |
4716 | } |
4717 | } | |
4718 | return; | |
4719 | } | |
4720 | ||
426b2d87 JS |
4721 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) |
4722 | return; | |
8f177c8e | 4723 | |
426b2d87 JS |
4724 | if (m_isDragging) |
4725 | { | |
70e8d961 | 4726 | if (m_rowLabelWin->HasCapture()) m_rowLabelWin->ReleaseMouse(); |
426b2d87 JS |
4727 | m_isDragging = FALSE; |
4728 | } | |
60ff3b99 | 4729 | |
6d004f67 MB |
4730 | // ------------ Entering or leaving the window |
4731 | // | |
4732 | if ( event.Entering() || event.Leaving() ) | |
4733 | { | |
e2b42eeb | 4734 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
6d004f67 MB |
4735 | } |
4736 | ||
e2b42eeb | 4737 | |
2d66e025 | 4738 | // ------------ Left button pressed |
f85afd4e | 4739 | // |
6d004f67 | 4740 | else if ( event.LeftDown() ) |
f85afd4e | 4741 | { |
2d66e025 MB |
4742 | // don't send a label click event for a hit on the |
4743 | // edge of the row label - this is probably the user | |
4744 | // wanting to resize the row | |
4745 | // | |
4746 | if ( YToEdgeOfRow(y) < 0 ) | |
f85afd4e | 4747 | { |
2d66e025 | 4748 | row = YToRow(y); |
58dd5b3b | 4749 | if ( row >= 0 && |
b54ba671 | 4750 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) ) |
f85afd4e | 4751 | { |
aa5e1f75 SN |
4752 | if ( !event.ShiftDown() && !event.ControlDown() ) |
4753 | ClearSelection(); | |
64e15340 | 4754 | if ( m_selection ) |
3f3dc2ef VZ |
4755 | { |
4756 | if ( event.ShiftDown() ) | |
4757 | { | |
4758 | m_selection->SelectBlock( m_currentCellCoords.GetRow(), | |
4759 | 0, | |
4760 | row, | |
4761 | GetNumberCols() - 1, | |
4762 | event.ControlDown(), | |
4763 | event.ShiftDown(), | |
4764 | event.AltDown(), | |
4765 | event.MetaDown() ); | |
4766 | } | |
4767 | else | |
4768 | { | |
4769 | m_selection->SelectRow( row, | |
4770 | event.ControlDown(), | |
4771 | event.ShiftDown(), | |
4772 | event.AltDown(), | |
4773 | event.MetaDown() ); | |
4774 | } | |
4775 | } | |
4776 | ||
e2b42eeb | 4777 | ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin); |
f85afd4e | 4778 | } |
2d66e025 MB |
4779 | } |
4780 | else | |
4781 | { | |
4782 | // starting to drag-resize a row | |
4783 | // | |
6e8524b1 MB |
4784 | if ( CanDragRowSize() ) |
4785 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin); | |
2d66e025 MB |
4786 | } |
4787 | } | |
f85afd4e | 4788 | |
f85afd4e | 4789 | |
2d66e025 MB |
4790 | // ------------ Left double click |
4791 | // | |
4792 | else if (event.LeftDClick() ) | |
4793 | { | |
d43851f7 JS |
4794 | int row = YToEdgeOfRow(y); |
4795 | if ( row < 0 ) | |
2d66e025 MB |
4796 | { |
4797 | row = YToRow(y); | |
b54ba671 | 4798 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ); |
f85afd4e | 4799 | } |
d43851f7 JS |
4800 | else |
4801 | { | |
4802 | // adjust row height depending on label text | |
4803 | AutoSizeRowLabelSize( row ); | |
4804 | ||
4805 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
4806 | m_dragLastPos = -1; | |
4807 | } | |
f85afd4e | 4808 | } |
60ff3b99 VZ |
4809 | |
4810 | ||
2d66e025 | 4811 | // ------------ Left button released |
f85afd4e | 4812 | // |
2d66e025 | 4813 | else if ( event.LeftUp() ) |
f85afd4e | 4814 | { |
2d66e025 | 4815 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) |
f85afd4e | 4816 | { |
6d004f67 | 4817 | DoEndDragResizeRow(); |
60ff3b99 | 4818 | |
6d004f67 MB |
4819 | // Note: we are ending the event *after* doing |
4820 | // default processing in this case | |
4821 | // | |
b54ba671 | 4822 | SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); |
2d66e025 | 4823 | } |
f85afd4e | 4824 | |
e2b42eeb VZ |
4825 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
4826 | m_dragLastPos = -1; | |
2d66e025 | 4827 | } |
f85afd4e | 4828 | |
f85afd4e | 4829 | |
2d66e025 MB |
4830 | // ------------ Right button down |
4831 | // | |
4832 | else if ( event.RightDown() ) | |
4833 | { | |
4834 | row = YToRow(y); | |
b54ba671 | 4835 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) |
2d66e025 MB |
4836 | { |
4837 | // no default action at the moment | |
f85afd4e MB |
4838 | } |
4839 | } | |
60ff3b99 VZ |
4840 | |
4841 | ||
2d66e025 | 4842 | // ------------ Right double click |
f85afd4e | 4843 | // |
2d66e025 MB |
4844 | else if ( event.RightDClick() ) |
4845 | { | |
4846 | row = YToRow(y); | |
b54ba671 | 4847 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) |
2d66e025 MB |
4848 | { |
4849 | // no default action at the moment | |
4850 | } | |
4851 | } | |
60ff3b99 VZ |
4852 | |
4853 | ||
2d66e025 | 4854 | // ------------ No buttons down and mouse moving |
f85afd4e | 4855 | // |
2d66e025 | 4856 | else if ( event.Moving() ) |
f85afd4e | 4857 | { |
2d66e025 MB |
4858 | m_dragRowOrCol = YToEdgeOfRow( y ); |
4859 | if ( m_dragRowOrCol >= 0 ) | |
8f177c8e | 4860 | { |
2d66e025 | 4861 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
8f177c8e | 4862 | { |
e2b42eeb | 4863 | // don't capture the mouse yet |
6e8524b1 MB |
4864 | if ( CanDragRowSize() ) |
4865 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE); | |
8f177c8e | 4866 | } |
2d66e025 | 4867 | } |
6d004f67 | 4868 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 4869 | { |
e2b42eeb | 4870 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE); |
8f177c8e | 4871 | } |
f85afd4e | 4872 | } |
2d66e025 MB |
4873 | } |
4874 | ||
4875 | ||
4876 | void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) | |
4877 | { | |
4878 | int x, y, col; | |
4879 | wxPoint pos( event.GetPosition() ); | |
4880 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 4881 | |
2d66e025 | 4882 | if ( event.Dragging() ) |
f85afd4e | 4883 | { |
fe77cf60 JS |
4884 | if (!m_isDragging) |
4885 | { | |
4886 | m_isDragging = TRUE; | |
4887 | m_colLabelWin->CaptureMouse(); | |
4888 | } | |
8f177c8e | 4889 | |
2d66e025 | 4890 | if ( event.LeftIsDown() ) |
8f177c8e | 4891 | { |
2d66e025 | 4892 | switch( m_cursorMode ) |
8f177c8e | 4893 | { |
2d66e025 | 4894 | case WXGRID_CURSOR_RESIZE_COL: |
8f177c8e | 4895 | { |
2d66e025 MB |
4896 | int cw, ch, dummy, top; |
4897 | m_gridWin->GetClientSize( &cw, &ch ); | |
4898 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
60ff3b99 | 4899 | |
2d66e025 MB |
4900 | wxClientDC dc( m_gridWin ); |
4901 | PrepareDC( dc ); | |
43947979 VZ |
4902 | |
4903 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + | |
4904 | GetColMinimalWidth(m_dragRowOrCol)); | |
d2fdd8d2 | 4905 | dc.SetLogicalFunction(wxINVERT); |
2d66e025 MB |
4906 | if ( m_dragLastPos >= 0 ) |
4907 | { | |
4908 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
4909 | } | |
4910 | dc.DrawLine( x, top, x, top+ch ); | |
4911 | m_dragLastPos = x; | |
f85afd4e | 4912 | } |
2d66e025 | 4913 | break; |
f85afd4e | 4914 | |
2d66e025 | 4915 | case WXGRID_CURSOR_SELECT_COL: |
e32352cf | 4916 | if ( (col = XToCol( x )) >= 0 ) |
aa5e1f75 | 4917 | { |
3f3dc2ef VZ |
4918 | if ( m_selection ) |
4919 | { | |
4920 | m_selection->SelectCol( col, | |
4921 | event.ControlDown(), | |
4922 | event.ShiftDown(), | |
4923 | event.AltDown(), | |
4924 | event.MetaDown() ); | |
4925 | } | |
2d66e025 | 4926 | } |
e2b42eeb VZ |
4927 | |
4928 | // default label to suppress warnings about "enumeration value | |
4929 | // 'xxx' not handled in switch | |
4930 | default: | |
4931 | break; | |
2d66e025 | 4932 | } |
f85afd4e | 4933 | } |
2d66e025 | 4934 | return; |
f85afd4e | 4935 | } |
2d66e025 | 4936 | |
fe77cf60 JS |
4937 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) |
4938 | return; | |
2d66e025 | 4939 | |
fe77cf60 JS |
4940 | if (m_isDragging) |
4941 | { | |
70e8d961 | 4942 | if (m_colLabelWin->HasCapture()) m_colLabelWin->ReleaseMouse(); |
fe77cf60 JS |
4943 | m_isDragging = FALSE; |
4944 | } | |
60ff3b99 | 4945 | |
6d004f67 MB |
4946 | // ------------ Entering or leaving the window |
4947 | // | |
4948 | if ( event.Entering() || event.Leaving() ) | |
4949 | { | |
e2b42eeb | 4950 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); |
6d004f67 MB |
4951 | } |
4952 | ||
e2b42eeb | 4953 | |
2d66e025 | 4954 | // ------------ Left button pressed |
f85afd4e | 4955 | // |
6d004f67 | 4956 | else if ( event.LeftDown() ) |
f85afd4e | 4957 | { |
2d66e025 MB |
4958 | // don't send a label click event for a hit on the |
4959 | // edge of the col label - this is probably the user | |
4960 | // wanting to resize the col | |
4961 | // | |
4962 | if ( XToEdgeOfCol(x) < 0 ) | |
8f177c8e | 4963 | { |
2d66e025 | 4964 | col = XToCol(x); |
58dd5b3b | 4965 | if ( col >= 0 && |
b54ba671 | 4966 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) ) |
8f177c8e | 4967 | { |
aa5e1f75 SN |
4968 | if ( !event.ShiftDown() && !event.ControlDown() ) |
4969 | ClearSelection(); | |
3f3dc2ef VZ |
4970 | if ( m_selection ) |
4971 | { | |
4972 | if ( event.ShiftDown() ) | |
4973 | { | |
4974 | m_selection->SelectBlock( 0, | |
4975 | m_currentCellCoords.GetCol(), | |
4976 | GetNumberRows() - 1, col, | |
4977 | event.ControlDown(), | |
4978 | event.ShiftDown(), | |
4979 | event.AltDown(), | |
4980 | event.MetaDown() ); | |
4981 | } | |
4982 | else | |
4983 | { | |
4984 | m_selection->SelectCol( col, | |
4985 | event.ControlDown(), | |
4986 | event.ShiftDown(), | |
4987 | event.AltDown(), | |
4988 | event.MetaDown() ); | |
4989 | } | |
4990 | } | |
4991 | ||
e2b42eeb | 4992 | ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin); |
f85afd4e | 4993 | } |
2d66e025 MB |
4994 | } |
4995 | else | |
4996 | { | |
4997 | // starting to drag-resize a col | |
4998 | // | |
6e8524b1 MB |
4999 | if ( CanDragColSize() ) |
5000 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin); | |
2d66e025 MB |
5001 | } |
5002 | } | |
f85afd4e | 5003 | |
f85afd4e | 5004 | |
2d66e025 MB |
5005 | // ------------ Left double click |
5006 | // | |
5007 | if ( event.LeftDClick() ) | |
5008 | { | |
d43851f7 JS |
5009 | int col = XToEdgeOfCol(x); |
5010 | if ( col < 0 ) | |
2d66e025 MB |
5011 | { |
5012 | col = XToCol(x); | |
b54ba671 | 5013 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ); |
2d66e025 | 5014 | } |
d43851f7 JS |
5015 | else |
5016 | { | |
5017 | // adjust column width depending on label text | |
5018 | AutoSizeColLabelSize( col ); | |
5019 | ||
5020 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
5021 | m_dragLastPos = -1; | |
5022 | } | |
2d66e025 | 5023 | } |
60ff3b99 VZ |
5024 | |
5025 | ||
2d66e025 MB |
5026 | // ------------ Left button released |
5027 | // | |
5028 | else if ( event.LeftUp() ) | |
5029 | { | |
5030 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
5031 | { | |
6d004f67 | 5032 | DoEndDragResizeCol(); |
e2b42eeb | 5033 | |
6d004f67 MB |
5034 | // Note: we are ending the event *after* doing |
5035 | // default processing in this case | |
5036 | // | |
b54ba671 | 5037 | SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); |
2d66e025 | 5038 | } |
f85afd4e | 5039 | |
e2b42eeb | 5040 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); |
2d66e025 | 5041 | m_dragLastPos = -1; |
60ff3b99 VZ |
5042 | } |
5043 | ||
5044 | ||
2d66e025 MB |
5045 | // ------------ Right button down |
5046 | // | |
5047 | else if ( event.RightDown() ) | |
5048 | { | |
5049 | col = XToCol(x); | |
b54ba671 | 5050 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) |
2d66e025 MB |
5051 | { |
5052 | // no default action at the moment | |
f85afd4e MB |
5053 | } |
5054 | } | |
60ff3b99 VZ |
5055 | |
5056 | ||
2d66e025 | 5057 | // ------------ Right double click |
f85afd4e | 5058 | // |
2d66e025 MB |
5059 | else if ( event.RightDClick() ) |
5060 | { | |
5061 | col = XToCol(x); | |
b54ba671 | 5062 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) |
2d66e025 MB |
5063 | { |
5064 | // no default action at the moment | |
5065 | } | |
5066 | } | |
60ff3b99 VZ |
5067 | |
5068 | ||
2d66e025 | 5069 | // ------------ No buttons down and mouse moving |
f85afd4e | 5070 | // |
2d66e025 | 5071 | else if ( event.Moving() ) |
f85afd4e | 5072 | { |
2d66e025 MB |
5073 | m_dragRowOrCol = XToEdgeOfCol( x ); |
5074 | if ( m_dragRowOrCol >= 0 ) | |
f85afd4e | 5075 | { |
2d66e025 | 5076 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
f85afd4e | 5077 | { |
e2b42eeb | 5078 | // don't capture the cursor yet |
6e8524b1 MB |
5079 | if ( CanDragColSize() ) |
5080 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE); | |
f85afd4e | 5081 | } |
2d66e025 | 5082 | } |
6d004f67 | 5083 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 5084 | { |
e2b42eeb | 5085 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE); |
8f177c8e | 5086 | } |
f85afd4e MB |
5087 | } |
5088 | } | |
5089 | ||
5090 | ||
2d66e025 | 5091 | void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 5092 | { |
2d66e025 | 5093 | if ( event.LeftDown() ) |
f85afd4e | 5094 | { |
2d66e025 MB |
5095 | // indicate corner label by having both row and |
5096 | // col args == -1 | |
f85afd4e | 5097 | // |
b54ba671 | 5098 | if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) ) |
2d66e025 MB |
5099 | { |
5100 | SelectAll(); | |
5101 | } | |
f85afd4e MB |
5102 | } |
5103 | ||
2d66e025 MB |
5104 | else if ( event.LeftDClick() ) |
5105 | { | |
b54ba671 | 5106 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event ); |
2d66e025 | 5107 | } |
8f177c8e | 5108 | |
2d66e025 | 5109 | else if ( event.RightDown() ) |
f85afd4e | 5110 | { |
b54ba671 | 5111 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) ) |
f85afd4e | 5112 | { |
2d66e025 MB |
5113 | // no default action at the moment |
5114 | } | |
5115 | } | |
f85afd4e | 5116 | |
2d66e025 MB |
5117 | else if ( event.RightDClick() ) |
5118 | { | |
b54ba671 | 5119 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) ) |
2d66e025 MB |
5120 | { |
5121 | // no default action at the moment | |
5122 | } | |
5123 | } | |
5124 | } | |
f85afd4e | 5125 | |
e2b42eeb VZ |
5126 | void wxGrid::ChangeCursorMode(CursorMode mode, |
5127 | wxWindow *win, | |
5128 | bool captureMouse) | |
5129 | { | |
5130 | #ifdef __WXDEBUG__ | |
5131 | static const wxChar *cursorModes[] = | |
5132 | { | |
5133 | _T("SELECT_CELL"), | |
5134 | _T("RESIZE_ROW"), | |
5135 | _T("RESIZE_COL"), | |
5136 | _T("SELECT_ROW"), | |
5137 | _T("SELECT_COL") | |
5138 | }; | |
5139 | ||
181bfffd VZ |
5140 | wxLogTrace(_T("grid"), |
5141 | _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"), | |
e2b42eeb VZ |
5142 | win == m_colLabelWin ? _T("colLabelWin") |
5143 | : win ? _T("rowLabelWin") | |
5144 | : _T("gridWin"), | |
5145 | cursorModes[m_cursorMode], cursorModes[mode]); | |
5146 | #endif // __WXDEBUG__ | |
5147 | ||
faec5a43 SN |
5148 | if ( mode == m_cursorMode && |
5149 | win == m_winCapture && | |
5150 | captureMouse == (m_winCapture != NULL)) | |
e2b42eeb VZ |
5151 | return; |
5152 | ||
5153 | if ( !win ) | |
5154 | { | |
5155 | // by default use the grid itself | |
5156 | win = m_gridWin; | |
5157 | } | |
5158 | ||
5159 | if ( m_winCapture ) | |
5160 | { | |
70e8d961 | 5161 | if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse(); |
e2b42eeb VZ |
5162 | m_winCapture = (wxWindow *)NULL; |
5163 | } | |
5164 | ||
5165 | m_cursorMode = mode; | |
5166 | ||
5167 | switch ( m_cursorMode ) | |
5168 | { | |
5169 | case WXGRID_CURSOR_RESIZE_ROW: | |
5170 | win->SetCursor( m_rowResizeCursor ); | |
5171 | break; | |
5172 | ||
5173 | case WXGRID_CURSOR_RESIZE_COL: | |
5174 | win->SetCursor( m_colResizeCursor ); | |
5175 | break; | |
5176 | ||
5177 | default: | |
5178 | win->SetCursor( *wxSTANDARD_CURSOR ); | |
5179 | } | |
5180 | ||
5181 | // we need to capture mouse when resizing | |
5182 | bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW || | |
5183 | m_cursorMode == WXGRID_CURSOR_RESIZE_COL; | |
5184 | ||
5185 | if ( captureMouse && resize ) | |
5186 | { | |
5187 | win->CaptureMouse(); | |
5188 | m_winCapture = win; | |
5189 | } | |
5190 | } | |
8f177c8e | 5191 | |
2d66e025 MB |
5192 | void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) |
5193 | { | |
5194 | int x, y; | |
5195 | wxPoint pos( event.GetPosition() ); | |
5196 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 5197 | |
2d66e025 MB |
5198 | wxGridCellCoords coords; |
5199 | XYToCell( x, y, coords ); | |
60ff3b99 | 5200 | |
27f35b66 SN |
5201 | int cell_rows, cell_cols; |
5202 | GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols ); | |
5203 | if ((cell_rows < 0) || (cell_cols < 0)) | |
5204 | { | |
5205 | coords.SetRow(coords.GetRow() + cell_rows); | |
5206 | coords.SetCol(coords.GetCol() + cell_cols); | |
5207 | } | |
5208 | ||
2d66e025 MB |
5209 | if ( event.Dragging() ) |
5210 | { | |
07296f0b RD |
5211 | //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol()); |
5212 | ||
5213 | // Don't start doing anything until the mouse has been drug at | |
5214 | // least 3 pixels in any direction... | |
508011ce VZ |
5215 | if (! m_isDragging) |
5216 | { | |
5217 | if (m_startDragPos == wxDefaultPosition) | |
5218 | { | |
07296f0b RD |
5219 | m_startDragPos = pos; |
5220 | return; | |
5221 | } | |
5222 | if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4) | |
5223 | return; | |
5224 | } | |
5225 | ||
2d66e025 | 5226 | m_isDragging = TRUE; |
2d66e025 MB |
5227 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
5228 | { | |
f0102d2a VZ |
5229 | // Hide the edit control, so it |
5230 | // won't interfer with drag-shrinking. | |
f6bcfd97 | 5231 | if ( IsCellEditControlShown() ) |
a5777624 | 5232 | { |
f0102d2a | 5233 | HideCellEditControl(); |
a5777624 RD |
5234 | SaveEditControlValue(); |
5235 | } | |
07296f0b RD |
5236 | |
5237 | // Have we captured the mouse yet? | |
508011ce VZ |
5238 | if (! m_winCapture) |
5239 | { | |
07296f0b RD |
5240 | m_winCapture = m_gridWin; |
5241 | m_winCapture->CaptureMouse(); | |
5242 | } | |
5243 | ||
2d66e025 MB |
5244 | if ( coords != wxGridNoCellCoords ) |
5245 | { | |
aa5e1f75 SN |
5246 | if ( event.ControlDown() ) |
5247 | { | |
5248 | if ( m_selectingKeyboard == wxGridNoCellCoords) | |
5249 | m_selectingKeyboard = coords; | |
c9097836 | 5250 | HighlightBlock ( m_selectingKeyboard, coords ); |
aa5e1f75 SN |
5251 | } |
5252 | else | |
5253 | { | |
5254 | if ( !IsSelection() ) | |
5255 | { | |
c9097836 | 5256 | HighlightBlock( coords, coords ); |
aa5e1f75 SN |
5257 | } |
5258 | else | |
5259 | { | |
c9097836 | 5260 | HighlightBlock( m_currentCellCoords, coords ); |
aa5e1f75 | 5261 | } |
f85afd4e | 5262 | } |
07296f0b | 5263 | |
508011ce VZ |
5264 | if (! IsVisible(coords)) |
5265 | { | |
07296f0b RD |
5266 | MakeCellVisible(coords); |
5267 | // TODO: need to introduce a delay or something here. The | |
e32352cf | 5268 | // scrolling is way to fast, at least on MSW - also on GTK. |
07296f0b | 5269 | } |
2d66e025 MB |
5270 | } |
5271 | } | |
6d004f67 MB |
5272 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) |
5273 | { | |
5274 | int cw, ch, left, dummy; | |
5275 | m_gridWin->GetClientSize( &cw, &ch ); | |
5276 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
8f177c8e | 5277 | |
6d004f67 MB |
5278 | wxClientDC dc( m_gridWin ); |
5279 | PrepareDC( dc ); | |
a95e38c0 VZ |
5280 | y = wxMax( y, GetRowTop(m_dragRowOrCol) + |
5281 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
6d004f67 MB |
5282 | dc.SetLogicalFunction(wxINVERT); |
5283 | if ( m_dragLastPos >= 0 ) | |
5284 | { | |
5285 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
5286 | } | |
5287 | dc.DrawLine( left, y, left+cw, y ); | |
5288 | m_dragLastPos = y; | |
5289 | } | |
5290 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
5291 | { | |
5292 | int cw, ch, dummy, top; | |
5293 | m_gridWin->GetClientSize( &cw, &ch ); | |
5294 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
e2b42eeb | 5295 | |
6d004f67 MB |
5296 | wxClientDC dc( m_gridWin ); |
5297 | PrepareDC( dc ); | |
43947979 VZ |
5298 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + |
5299 | GetColMinimalWidth(m_dragRowOrCol) ); | |
6d004f67 MB |
5300 | dc.SetLogicalFunction(wxINVERT); |
5301 | if ( m_dragLastPos >= 0 ) | |
5302 | { | |
5303 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
5304 | } | |
5305 | dc.DrawLine( x, top, x, top+ch ); | |
5306 | m_dragLastPos = x; | |
5307 | } | |
e2b42eeb | 5308 | |
2d66e025 MB |
5309 | return; |
5310 | } | |
66242c80 | 5311 | |
2d66e025 | 5312 | m_isDragging = FALSE; |
07296f0b RD |
5313 | m_startDragPos = wxDefaultPosition; |
5314 | ||
a5777624 RD |
5315 | // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL |
5316 | // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under | |
5317 | // wxGTK | |
e2b42eeb | 5318 | #if 0 |
a5777624 RD |
5319 | if ( event.Entering() || event.Leaving() ) |
5320 | { | |
5321 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5322 | m_gridWin->SetCursor( *wxSTANDARD_CURSOR ); | |
5323 | } | |
5324 | else | |
e2b42eeb VZ |
5325 | #endif // 0 |
5326 | ||
a5777624 RD |
5327 | // ------------ Left button pressed |
5328 | // | |
5329 | if ( event.LeftDown() && coords != wxGridNoCellCoords ) | |
f6bcfd97 BP |
5330 | { |
5331 | if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK, | |
5332 | coords.GetRow(), | |
5333 | coords.GetCol(), | |
5334 | event ) ) | |
a5777624 | 5335 | { |
f6bcfd97 BP |
5336 | if ( !event.ControlDown() ) |
5337 | ClearSelection(); | |
5338 | if ( event.ShiftDown() ) | |
5339 | { | |
3f3dc2ef VZ |
5340 | if ( m_selection ) |
5341 | { | |
5342 | m_selection->SelectBlock( m_currentCellCoords.GetRow(), | |
5343 | m_currentCellCoords.GetCol(), | |
5344 | coords.GetRow(), | |
5345 | coords.GetCol(), | |
5346 | event.ControlDown(), | |
5347 | event.ShiftDown(), | |
5348 | event.AltDown(), | |
5349 | event.MetaDown() ); | |
5350 | } | |
f6bcfd97 BP |
5351 | } |
5352 | else if ( XToEdgeOfCol(x) < 0 && | |
5353 | YToEdgeOfRow(y) < 0 ) | |
58dd5b3b | 5354 | { |
a5777624 RD |
5355 | DisableCellEditControl(); |
5356 | MakeCellVisible( coords ); | |
5357 | ||
73145b0e | 5358 | if ( event.ControlDown() ) |
58dd5b3b | 5359 | { |
73145b0e JS |
5360 | if ( m_selection ) |
5361 | { | |
5362 | m_selection->ToggleCellSelection( coords.GetRow(), | |
5363 | coords.GetCol(), | |
5364 | event.ControlDown(), | |
5365 | event.ShiftDown(), | |
5366 | event.AltDown(), | |
5367 | event.MetaDown() ); | |
5368 | } | |
5369 | m_selectingTopLeft = wxGridNoCellCoords; | |
5370 | m_selectingBottomRight = wxGridNoCellCoords; | |
5371 | m_selectingKeyboard = coords; | |
a5777624 RD |
5372 | } |
5373 | else | |
5374 | { | |
73145b0e JS |
5375 | m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords; |
5376 | SetCurrentCell( coords ); | |
5377 | if ( m_selection ) | |
aa5e1f75 | 5378 | { |
73145b0e JS |
5379 | if ( m_selection->GetSelectionMode() != |
5380 | wxGrid::wxGridSelectCells ) | |
3f3dc2ef | 5381 | { |
73145b0e | 5382 | HighlightBlock( coords, coords ); |
3f3dc2ef | 5383 | } |
aa5e1f75 | 5384 | } |
58dd5b3b MB |
5385 | } |
5386 | } | |
f85afd4e | 5387 | } |
a5777624 | 5388 | } |
f85afd4e | 5389 | |
f85afd4e | 5390 | |
a5777624 RD |
5391 | // ------------ Left double click |
5392 | // | |
5393 | else if ( event.LeftDClick() && coords != wxGridNoCellCoords ) | |
5394 | { | |
5395 | DisableCellEditControl(); | |
5396 | ||
5397 | if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 ) | |
58dd5b3b | 5398 | { |
a5777624 RD |
5399 | SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK, |
5400 | coords.GetRow(), | |
5401 | coords.GetCol(), | |
5402 | event ); | |
58dd5b3b | 5403 | } |
a5777624 | 5404 | } |
f85afd4e | 5405 | |
8f177c8e | 5406 | |
a5777624 RD |
5407 | // ------------ Left button released |
5408 | // | |
5409 | else if ( event.LeftUp() ) | |
5410 | { | |
5411 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
f85afd4e | 5412 | { |
b5808881 SN |
5413 | if ( m_selectingTopLeft != wxGridNoCellCoords && |
5414 | m_selectingBottomRight != wxGridNoCellCoords ) | |
52068ea5 | 5415 | { |
a5777624 | 5416 | if (m_winCapture) |
58dd5b3b | 5417 | { |
70e8d961 | 5418 | if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse(); |
a5777624 | 5419 | m_winCapture = NULL; |
58dd5b3b | 5420 | } |
3f3dc2ef VZ |
5421 | |
5422 | if ( m_selection ) | |
5423 | { | |
5424 | m_selection->SelectBlock( m_selectingTopLeft.GetRow(), | |
5425 | m_selectingTopLeft.GetCol(), | |
5426 | m_selectingBottomRight.GetRow(), | |
5427 | m_selectingBottomRight.GetCol(), | |
5428 | event.ControlDown(), | |
5429 | event.ShiftDown(), | |
5430 | event.AltDown(), | |
5431 | event.MetaDown() ); | |
5432 | } | |
5433 | ||
5c8fc7c1 SN |
5434 | m_selectingTopLeft = wxGridNoCellCoords; |
5435 | m_selectingBottomRight = wxGridNoCellCoords; | |
2d66e025 | 5436 | |
73145b0e JS |
5437 | // Show the edit control, if it has been hidden for |
5438 | // drag-shrinking. | |
5439 | ShowCellEditControl(); | |
5440 | } | |
5441 | else | |
5442 | { | |
5443 | if( m_waitForSlowClick && CanEnableCellControl()) | |
5444 | { | |
5445 | EnableCellEditControl(); | |
5446 | ||
5447 | wxGridCellAttr* attr = GetCellAttr(coords); | |
5448 | wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol()); | |
5449 | editor->StartingClick(); | |
5450 | editor->DecRef(); | |
5451 | attr->DecRef(); | |
5452 | ||
5453 | m_waitForSlowClick = FALSE; | |
5454 | } | |
5455 | } | |
a5777624 RD |
5456 | } |
5457 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
5458 | { | |
5459 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5460 | DoEndDragResizeRow(); | |
e2b42eeb | 5461 | |
a5777624 RD |
5462 | // Note: we are ending the event *after* doing |
5463 | // default processing in this case | |
5464 | // | |
5465 | SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); | |
5466 | } | |
5467 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
5468 | { | |
5469 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5470 | DoEndDragResizeCol(); | |
e2b42eeb | 5471 | |
a5777624 RD |
5472 | // Note: we are ending the event *after* doing |
5473 | // default processing in this case | |
5474 | // | |
5475 | SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); | |
58dd5b3b | 5476 | } |
f85afd4e | 5477 | |
a5777624 RD |
5478 | m_dragLastPos = -1; |
5479 | } | |
5480 | ||
f85afd4e | 5481 | |
a5777624 RD |
5482 | // ------------ Right button down |
5483 | // | |
5484 | else if ( event.RightDown() && coords != wxGridNoCellCoords ) | |
5485 | { | |
5486 | DisableCellEditControl(); | |
5487 | if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK, | |
5488 | coords.GetRow(), | |
5489 | coords.GetCol(), | |
5490 | event ) ) | |
f85afd4e | 5491 | { |
a5777624 | 5492 | // no default action at the moment |
60ff3b99 | 5493 | } |
a5777624 | 5494 | } |
2d66e025 | 5495 | |
60ff3b99 | 5496 | |
a5777624 RD |
5497 | // ------------ Right double click |
5498 | // | |
5499 | else if ( event.RightDClick() && coords != wxGridNoCellCoords ) | |
5500 | { | |
5501 | DisableCellEditControl(); | |
5502 | if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK, | |
5503 | coords.GetRow(), | |
5504 | coords.GetCol(), | |
5505 | event ) ) | |
f85afd4e | 5506 | { |
a5777624 | 5507 | // no default action at the moment |
60ff3b99 | 5508 | } |
a5777624 RD |
5509 | } |
5510 | ||
5511 | // ------------ Moving and no button action | |
5512 | // | |
5513 | else if ( event.Moving() && !event.IsButton() ) | |
5514 | { | |
d57ad377 SN |
5515 | if( coords.GetRow() < 0 || coords.GetCol() < 0 ) |
5516 | { | |
5517 | // out of grid cell area | |
5518 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5519 | return; | |
5520 | } | |
5521 | ||
a5777624 RD |
5522 | int dragRow = YToEdgeOfRow( y ); |
5523 | int dragCol = XToEdgeOfCol( x ); | |
790cc417 | 5524 | |
a5777624 RD |
5525 | // Dragging on the corner of a cell to resize in both |
5526 | // directions is not implemented yet... | |
790cc417 | 5527 | // |
a5777624 | 5528 | if ( dragRow >= 0 && dragCol >= 0 ) |
790cc417 | 5529 | { |
a5777624 RD |
5530 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); |
5531 | return; | |
5532 | } | |
6d004f67 | 5533 | |
d57ad377 | 5534 | if ( dragRow >= 0 ) |
a5777624 RD |
5535 | { |
5536 | m_dragRowOrCol = dragRow; | |
6d004f67 | 5537 | |
a5777624 | 5538 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
6d004f67 | 5539 | { |
a5777624 RD |
5540 | if ( CanDragRowSize() && CanDragGridSize() ) |
5541 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW); | |
6d004f67 | 5542 | } |
e2b42eeb | 5543 | |
b94ae1ea VZ |
5544 | if ( dragCol >= 0 ) |
5545 | { | |
5546 | m_dragRowOrCol = dragCol; | |
122603f1 | 5547 | } |
b94ae1ea | 5548 | |
a5777624 RD |
5549 | return; |
5550 | } | |
e2b42eeb | 5551 | |
d57ad377 | 5552 | if ( dragCol >= 0 ) |
a5777624 RD |
5553 | { |
5554 | m_dragRowOrCol = dragCol; | |
e2b42eeb | 5555 | |
a5777624 | 5556 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
6d004f67 | 5557 | { |
a5777624 RD |
5558 | if ( CanDragColSize() && CanDragGridSize() ) |
5559 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL); | |
6d004f67 | 5560 | } |
a5777624 RD |
5561 | |
5562 | return; | |
6d004f67 | 5563 | } |
a5777624 RD |
5564 | |
5565 | // Neither on a row or col edge | |
5566 | // | |
5567 | if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) | |
5568 | { | |
5569 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5570 | } | |
5571 | } | |
6d004f67 MB |
5572 | } |
5573 | ||
5574 | ||
5575 | void wxGrid::DoEndDragResizeRow() | |
5576 | { | |
5577 | if ( m_dragLastPos >= 0 ) | |
5578 | { | |
5579 | // erase the last line and resize the row | |
5580 | // | |
5581 | int cw, ch, left, dummy; | |
5582 | m_gridWin->GetClientSize( &cw, &ch ); | |
5583 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
5584 | ||
5585 | wxClientDC dc( m_gridWin ); | |
5586 | PrepareDC( dc ); | |
5587 | dc.SetLogicalFunction( wxINVERT ); | |
5588 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
5589 | HideCellEditControl(); | |
a5777624 | 5590 | SaveEditControlValue(); |
6d004f67 | 5591 | |
7c1cb261 | 5592 | int rowTop = GetRowTop(m_dragRowOrCol); |
6d004f67 MB |
5593 | SetRowSize( m_dragRowOrCol, |
5594 | wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) ); | |
e2b42eeb | 5595 | |
6d004f67 MB |
5596 | if ( !GetBatchCount() ) |
5597 | { | |
5598 | // Only needed to get the correct rect.y: | |
5599 | wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) ); | |
5600 | rect.x = 0; | |
5601 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
5602 | rect.width = m_rowLabelWidth; | |
5603 | rect.height = ch - rect.y; | |
5604 | m_rowLabelWin->Refresh( TRUE, &rect ); | |
5605 | rect.width = cw; | |
27f35b66 SN |
5606 | // if there is a multicell block, paint all of it |
5607 | if (m_table) | |
5608 | { | |
5609 | int i, cell_rows, cell_cols, subtract_rows = 0; | |
5610 | int leftCol = XToCol(left); | |
5611 | int rightCol = XToCol(left+cw); | |
5612 | if (leftCol >= 0) | |
5613 | { | |
5614 | if (rightCol < 0) rightCol = m_numCols; | |
5615 | for (i=leftCol; i<rightCol; i++) | |
5616 | { | |
5617 | GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols); | |
5618 | if (cell_rows < subtract_rows) | |
5619 | subtract_rows = cell_rows; | |
5620 | } | |
5621 | rect.y = GetRowTop(m_dragRowOrCol + subtract_rows); | |
5622 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
5623 | rect.height = ch - rect.y; | |
5624 | } | |
5625 | } | |
6d004f67 MB |
5626 | m_gridWin->Refresh( FALSE, &rect ); |
5627 | } | |
5628 | ||
5629 | ShowCellEditControl(); | |
5630 | } | |
5631 | } | |
5632 | ||
5633 | ||
5634 | void wxGrid::DoEndDragResizeCol() | |
5635 | { | |
5636 | if ( m_dragLastPos >= 0 ) | |
5637 | { | |
5638 | // erase the last line and resize the col | |
5639 | // | |
5640 | int cw, ch, dummy, top; | |
5641 | m_gridWin->GetClientSize( &cw, &ch ); | |
5642 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
5643 | ||
5644 | wxClientDC dc( m_gridWin ); | |
5645 | PrepareDC( dc ); | |
5646 | dc.SetLogicalFunction( wxINVERT ); | |
5647 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
5648 | HideCellEditControl(); | |
a5777624 | 5649 | SaveEditControlValue(); |
6d004f67 | 5650 | |
7c1cb261 | 5651 | int colLeft = GetColLeft(m_dragRowOrCol); |
6d004f67 | 5652 | SetColSize( m_dragRowOrCol, |
43947979 VZ |
5653 | wxMax( m_dragLastPos - colLeft, |
5654 | GetColMinimalWidth(m_dragRowOrCol) ) ); | |
6d004f67 MB |
5655 | |
5656 | if ( !GetBatchCount() ) | |
5657 | { | |
5658 | // Only needed to get the correct rect.x: | |
5659 | wxRect rect ( CellToRect( 0, m_dragRowOrCol ) ); | |
5660 | rect.y = 0; | |
5661 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
5662 | rect.width = cw - rect.x; | |
5663 | rect.height = m_colLabelHeight; | |
5664 | m_colLabelWin->Refresh( TRUE, &rect ); | |
5665 | rect.height = ch; | |
27f35b66 SN |
5666 | // if there is a multicell block, paint all of it |
5667 | if (m_table) | |
5668 | { | |
5669 | int i, cell_rows, cell_cols, subtract_cols = 0; | |
5670 | int topRow = YToRow(top); | |
5671 | int bottomRow = YToRow(top+cw); | |
5672 | if (topRow >= 0) | |
5673 | { | |
5674 | if (bottomRow < 0) bottomRow = m_numRows; | |
5675 | for (i=topRow; i<bottomRow; i++) | |
5676 | { | |
5677 | GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols); | |
5678 | if (cell_cols < subtract_cols) | |
5679 | subtract_cols = cell_cols; | |
5680 | } | |
5681 | rect.x = GetColLeft(m_dragRowOrCol + subtract_cols); | |
5682 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
5683 | rect.width = cw - rect.x; | |
5684 | } | |
5685 | } | |
6d004f67 | 5686 | m_gridWin->Refresh( FALSE, &rect ); |
790cc417 | 5687 | } |
e2b42eeb | 5688 | |
6d004f67 | 5689 | ShowCellEditControl(); |
f85afd4e | 5690 | } |
f85afd4e MB |
5691 | } |
5692 | ||
5693 | ||
6d004f67 | 5694 | |
2d66e025 MB |
5695 | // |
5696 | // ------ interaction with data model | |
5697 | // | |
5698 | bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg ) | |
f85afd4e | 5699 | { |
2d66e025 | 5700 | switch ( msg.GetId() ) |
17732cec | 5701 | { |
2d66e025 MB |
5702 | case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES: |
5703 | return GetModelValues(); | |
17732cec | 5704 | |
2d66e025 MB |
5705 | case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES: |
5706 | return SetModelValues(); | |
f85afd4e | 5707 | |
2d66e025 MB |
5708 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: |
5709 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
5710 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
5711 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
5712 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
5713 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
5714 | return Redimension( msg ); | |
5715 | ||
5716 | default: | |
5717 | return FALSE; | |
f85afd4e | 5718 | } |
2d66e025 | 5719 | } |
f85afd4e | 5720 | |
f85afd4e | 5721 | |
f85afd4e | 5722 | |
2d66e025 MB |
5723 | // The behaviour of this function depends on the grid table class |
5724 | // Clear() function. For the default wxGridStringTable class the | |
5725 | // behavious is to replace all cell contents with wxEmptyString but | |
5726 | // not to change the number of rows or cols. | |
5727 | // | |
5728 | void wxGrid::ClearGrid() | |
5729 | { | |
5730 | if ( m_table ) | |
f85afd4e | 5731 | { |
4cfa5de6 RD |
5732 | if (IsCellEditControlEnabled()) |
5733 | DisableCellEditControl(); | |
5734 | ||
2d66e025 | 5735 | m_table->Clear(); |
1f1ce288 | 5736 | if ( !GetBatchCount() ) m_gridWin->Refresh(); |
f85afd4e MB |
5737 | } |
5738 | } | |
5739 | ||
5740 | ||
2d66e025 | 5741 | bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) |
f85afd4e | 5742 | { |
2d66e025 | 5743 | // TODO: something with updateLabels flag |
8f177c8e | 5744 | |
2d66e025 | 5745 | if ( !m_created ) |
f85afd4e | 5746 | { |
bd3c6758 | 5747 | wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") ); |
2d66e025 MB |
5748 | return FALSE; |
5749 | } | |
8f177c8e | 5750 | |
2d66e025 MB |
5751 | if ( m_table ) |
5752 | { | |
b7fff980 | 5753 | if (IsCellEditControlEnabled()) |
b54ba671 | 5754 | DisableCellEditControl(); |
b7fff980 | 5755 | |
f6bcfd97 | 5756 | return m_table->InsertRows( pos, numRows ); |
f85afd4e | 5757 | |
2d66e025 MB |
5758 | // the table will have sent the results of the insert row |
5759 | // operation to this view object as a grid table message | |
f85afd4e | 5760 | } |
f6bcfd97 | 5761 | return FALSE; |
f85afd4e MB |
5762 | } |
5763 | ||
5764 | ||
2d66e025 | 5765 | bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) ) |
f85afd4e | 5766 | { |
2d66e025 MB |
5767 | // TODO: something with updateLabels flag |
5768 | ||
5769 | if ( !m_created ) | |
f85afd4e | 5770 | { |
bd3c6758 | 5771 | wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") ); |
2d66e025 MB |
5772 | return FALSE; |
5773 | } | |
5774 | ||
f6bcfd97 BP |
5775 | return ( m_table && m_table->AppendRows( numRows ) ); |
5776 | // the table will have sent the results of the append row | |
5777 | // operation to this view object as a grid table message | |
f85afd4e MB |
5778 | } |
5779 | ||
2d66e025 MB |
5780 | |
5781 | bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 5782 | { |
2d66e025 MB |
5783 | // TODO: something with updateLabels flag |
5784 | ||
5785 | if ( !m_created ) | |
f85afd4e | 5786 | { |
bd3c6758 | 5787 | wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") ); |
2d66e025 MB |
5788 | return FALSE; |
5789 | } | |
5790 | ||
b7fff980 | 5791 | if ( m_table ) |
2d66e025 | 5792 | { |
b7fff980 | 5793 | if (IsCellEditControlEnabled()) |
b54ba671 | 5794 | DisableCellEditControl(); |
f85afd4e | 5795 | |
f6bcfd97 BP |
5796 | return (m_table->DeleteRows( pos, numRows )); |
5797 | // the table will have sent the results of the delete row | |
5798 | // operation to this view object as a grid table message | |
2d66e025 | 5799 | } |
b7fff980 | 5800 | return FALSE; |
2d66e025 | 5801 | } |
f85afd4e | 5802 | |
f85afd4e | 5803 | |
2d66e025 MB |
5804 | bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) |
5805 | { | |
5806 | // TODO: something with updateLabels flag | |
8f177c8e | 5807 | |
2d66e025 MB |
5808 | if ( !m_created ) |
5809 | { | |
bd3c6758 | 5810 | wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") ); |
2d66e025 MB |
5811 | return FALSE; |
5812 | } | |
f85afd4e | 5813 | |
2d66e025 MB |
5814 | if ( m_table ) |
5815 | { | |
b7fff980 | 5816 | if (IsCellEditControlEnabled()) |
b54ba671 | 5817 | DisableCellEditControl(); |
b7fff980 | 5818 | |
f6bcfd97 | 5819 | return m_table->InsertCols( pos, numCols ); |
2d66e025 MB |
5820 | // the table will have sent the results of the insert col |
5821 | // operation to this view object as a grid table message | |
f85afd4e | 5822 | } |
f6bcfd97 | 5823 | return FALSE; |
f85afd4e MB |
5824 | } |
5825 | ||
2d66e025 MB |
5826 | |
5827 | bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 5828 | { |
2d66e025 MB |
5829 | // TODO: something with updateLabels flag |
5830 | ||
5831 | if ( !m_created ) | |
f85afd4e | 5832 | { |
bd3c6758 | 5833 | wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") ); |
2d66e025 MB |
5834 | return FALSE; |
5835 | } | |
f85afd4e | 5836 | |
f6bcfd97 BP |
5837 | return ( m_table && m_table->AppendCols( numCols ) ); |
5838 | // the table will have sent the results of the append col | |
5839 | // operation to this view object as a grid table message | |
f85afd4e MB |
5840 | } |
5841 | ||
5842 | ||
2d66e025 | 5843 | bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) |
f85afd4e | 5844 | { |
2d66e025 | 5845 | // TODO: something with updateLabels flag |
8f177c8e | 5846 | |
2d66e025 | 5847 | if ( !m_created ) |
f85afd4e | 5848 | { |
bd3c6758 | 5849 | wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") ); |
2d66e025 | 5850 | return FALSE; |
f85afd4e MB |
5851 | } |
5852 | ||
b7fff980 | 5853 | if ( m_table ) |
2d66e025 | 5854 | { |
b7fff980 | 5855 | if (IsCellEditControlEnabled()) |
b54ba671 | 5856 | DisableCellEditControl(); |
8f177c8e | 5857 | |
f6bcfd97 BP |
5858 | return ( m_table->DeleteCols( pos, numCols ) ); |
5859 | // the table will have sent the results of the delete col | |
5860 | // operation to this view object as a grid table message | |
f85afd4e | 5861 | } |
b7fff980 | 5862 | return FALSE; |
f85afd4e MB |
5863 | } |
5864 | ||
5865 | ||
2d66e025 MB |
5866 | |
5867 | // | |
5868 | // ----- event handlers | |
f85afd4e | 5869 | // |
8f177c8e | 5870 | |
2d66e025 MB |
5871 | // Generate a grid event based on a mouse event and |
5872 | // return the result of ProcessEvent() | |
5873 | // | |
fe7b9ed6 | 5874 | int wxGrid::SendEvent( const wxEventType type, |
2d66e025 MB |
5875 | int row, int col, |
5876 | wxMouseEvent& mouseEv ) | |
5877 | { | |
fe7b9ed6 | 5878 | bool claimed; |
1bcf0c7d | 5879 | bool vetoed= FALSE; |
fe7b9ed6 | 5880 | |
97a9929e VZ |
5881 | if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE ) |
5882 | { | |
5883 | int rowOrCol = (row == -1 ? col : row); | |
5884 | ||
5885 | wxGridSizeEvent gridEvt( GetId(), | |
5886 | type, | |
5887 | this, | |
5888 | rowOrCol, | |
5889 | mouseEv.GetX() + GetRowLabelSize(), | |
5890 | mouseEv.GetY() + GetColLabelSize(), | |
5891 | mouseEv.ControlDown(), | |
5892 | mouseEv.ShiftDown(), | |
5893 | mouseEv.AltDown(), | |
5894 | mouseEv.MetaDown() ); | |
5895 | ||
5896 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
5897 | vetoed = !gridEvt.IsAllowed(); | |
5898 | } | |
fe7b9ed6 | 5899 | else if ( type == wxEVT_GRID_RANGE_SELECT ) |
97a9929e VZ |
5900 | { |
5901 | // Right now, it should _never_ end up here! | |
5902 | wxGridRangeSelectEvent gridEvt( GetId(), | |
5903 | type, | |
5904 | this, | |
5905 | m_selectingTopLeft, | |
5906 | m_selectingBottomRight, | |
5907 | TRUE, | |
5908 | mouseEv.ControlDown(), | |
5909 | mouseEv.ShiftDown(), | |
5910 | mouseEv.AltDown(), | |
5911 | mouseEv.MetaDown() ); | |
5912 | ||
5913 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
5914 | vetoed = !gridEvt.IsAllowed(); | |
5915 | } | |
fe7b9ed6 | 5916 | else |
97a9929e VZ |
5917 | { |
5918 | wxGridEvent gridEvt( GetId(), | |
5919 | type, | |
5920 | this, | |
5921 | row, col, | |
5922 | mouseEv.GetX() + GetRowLabelSize(), | |
5923 | mouseEv.GetY() + GetColLabelSize(), | |
5924 | FALSE, | |
5925 | mouseEv.ControlDown(), | |
5926 | mouseEv.ShiftDown(), | |
5927 | mouseEv.AltDown(), | |
5928 | mouseEv.MetaDown() ); | |
5929 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
5930 | vetoed = !gridEvt.IsAllowed(); | |
5931 | } | |
5932 | ||
5933 | // A Veto'd event may not be `claimed' so test this first | |
5934 | if (vetoed) return -1; | |
5935 | return claimed ? 1 : 0; | |
f85afd4e MB |
5936 | } |
5937 | ||
5938 | ||
2d66e025 MB |
5939 | // Generate a grid event of specified type and return the result |
5940 | // of ProcessEvent(). | |
f85afd4e | 5941 | // |
fe7b9ed6 | 5942 | int wxGrid::SendEvent( const wxEventType type, |
2d66e025 | 5943 | int row, int col ) |
f85afd4e | 5944 | { |
fe7b9ed6 | 5945 | bool claimed; |
1bcf0c7d | 5946 | bool vetoed= FALSE; |
fe7b9ed6 | 5947 | |
b54ba671 | 5948 | if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE ) |
f85afd4e | 5949 | { |
2d66e025 | 5950 | int rowOrCol = (row == -1 ? col : row); |
f85afd4e | 5951 | |
2d66e025 MB |
5952 | wxGridSizeEvent gridEvt( GetId(), |
5953 | type, | |
5954 | this, | |
5955 | rowOrCol ); | |
5956 | ||
fe7b9ed6 VZ |
5957 | claimed = GetEventHandler()->ProcessEvent(gridEvt); |
5958 | vetoed = !gridEvt.IsAllowed(); | |
2d66e025 MB |
5959 | } |
5960 | else | |
5961 | { | |
5962 | wxGridEvent gridEvt( GetId(), | |
5963 | type, | |
5964 | this, | |
5965 | row, col ); | |
8f177c8e | 5966 | |
fe7b9ed6 VZ |
5967 | claimed = GetEventHandler()->ProcessEvent(gridEvt); |
5968 | vetoed = !gridEvt.IsAllowed(); | |
5969 | } | |
5970 | ||
97a9929e VZ |
5971 | // A Veto'd event may not be `claimed' so test this first |
5972 | if (vetoed) return -1; | |
5973 | return claimed ? 1 : 0; | |
f85afd4e MB |
5974 | } |
5975 | ||
5976 | ||
2d66e025 | 5977 | void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
f85afd4e | 5978 | { |
f6bcfd97 | 5979 | wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW |
8f177c8e | 5980 | } |
f85afd4e | 5981 | |
bca7bfc8 | 5982 | void wxGrid::Refresh(bool eraseb, const wxRect* rect) |
27f35b66 | 5983 | { |
ad603bf7 VZ |
5984 | // Don't do anything if between Begin/EndBatch... |
5985 | // EndBatch() will do all this on the last nested one anyway. | |
27f35b66 SN |
5986 | if (! GetBatchCount()) |
5987 | { | |
bca7bfc8 | 5988 | // Refresh to get correct scrolled position: |
27f35b66 SN |
5989 | wxScrolledWindow::Refresh(eraseb,rect); |
5990 | ||
27f35b66 SN |
5991 | if (rect) |
5992 | { | |
bca7bfc8 SN |
5993 | int rect_x, rect_y, rectWidth, rectHeight; |
5994 | int width_label, width_cell, height_label, height_cell; | |
5995 | int x, y; | |
5996 | ||
27f35b66 | 5997 | //Copy rectangle can get scroll offsets.. |
bca7bfc8 SN |
5998 | rect_x = rect->GetX(); |
5999 | rect_y = rect->GetY(); | |
6000 | rectWidth = rect->GetWidth(); | |
6001 | rectHeight = rect->GetHeight(); | |
27f35b66 | 6002 | |
bca7bfc8 SN |
6003 | width_label = m_rowLabelWidth - rect_x; |
6004 | if (width_label > rectWidth) width_label = rectWidth; | |
27f35b66 | 6005 | |
bca7bfc8 SN |
6006 | height_label = m_colLabelHeight - rect_y; |
6007 | if (height_label > rectHeight) height_label = rectHeight; | |
27f35b66 | 6008 | |
bca7bfc8 SN |
6009 | if (rect_x > m_rowLabelWidth) |
6010 | { | |
6011 | x = rect_x - m_rowLabelWidth; | |
6012 | width_cell = rectWidth; | |
6013 | } | |
6014 | else | |
6015 | { | |
6016 | x = 0; | |
6017 | width_cell = rectWidth - (m_rowLabelWidth - rect_x); | |
6018 | } | |
6019 | ||
6020 | if (rect_y > m_colLabelHeight) | |
6021 | { | |
6022 | y = rect_y - m_colLabelHeight; | |
6023 | height_cell = rectHeight; | |
6024 | } | |
6025 | else | |
6026 | { | |
6027 | y = 0; | |
6028 | height_cell = rectHeight - (m_colLabelHeight - rect_y); | |
6029 | } | |
6030 | ||
6031 | // Paint corner label part intersecting rect. | |
6032 | if ( width_label > 0 && height_label > 0 ) | |
6033 | { | |
6034 | wxRect anotherrect(rect_x, rect_y, width_label, height_label); | |
6035 | m_cornerLabelWin->Refresh(eraseb, &anotherrect); | |
6036 | } | |
6037 | ||
6038 | // Paint col labels part intersecting rect. | |
6039 | if ( width_cell > 0 && height_label > 0 ) | |
6040 | { | |
6041 | wxRect anotherrect(x, rect_y, width_cell, height_label); | |
6042 | m_colLabelWin->Refresh(eraseb, &anotherrect); | |
6043 | } | |
6044 | ||
6045 | // Paint row labels part intersecting rect. | |
6046 | if ( width_label > 0 && height_cell > 0 ) | |
6047 | { | |
6048 | wxRect anotherrect(rect_x, y, width_label, height_cell); | |
6049 | m_rowLabelWin->Refresh(eraseb, &anotherrect); | |
6050 | } | |
6051 | ||
6052 | // Paint cell area part intersecting rect. | |
6053 | if ( width_cell > 0 && height_cell > 0 ) | |
6054 | { | |
6055 | wxRect anotherrect(x, y, width_cell, height_cell); | |
6056 | m_gridWin->Refresh(eraseb, &anotherrect); | |
6057 | } | |
6058 | } | |
6059 | else | |
6060 | { | |
6061 | m_cornerLabelWin->Refresh(eraseb, NULL); | |
2b5f62a0 | 6062 | m_colLabelWin->Refresh(eraseb, NULL); |
bca7bfc8 SN |
6063 | m_rowLabelWin->Refresh(eraseb, NULL); |
6064 | m_gridWin->Refresh(eraseb, NULL); | |
6065 | } | |
27f35b66 SN |
6066 | } |
6067 | } | |
f85afd4e | 6068 | |
97a9929e | 6069 | void wxGrid::OnSize( wxSizeEvent& event ) |
f85afd4e | 6070 | { |
97a9929e | 6071 | // position the child windows |
7807d81c | 6072 | CalcWindowSizes(); |
97a9929e VZ |
6073 | |
6074 | // don't call CalcDimensions() from here, the base class handles the size | |
6075 | // changes itself | |
6076 | event.Skip(); | |
f85afd4e MB |
6077 | } |
6078 | ||
f85afd4e | 6079 | |
2d66e025 | 6080 | void wxGrid::OnKeyDown( wxKeyEvent& event ) |
f85afd4e | 6081 | { |
2d66e025 | 6082 | if ( m_inOnKeyDown ) |
f85afd4e | 6083 | { |
2d66e025 MB |
6084 | // shouldn't be here - we are going round in circles... |
6085 | // | |
07296f0b | 6086 | wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") ); |
f85afd4e MB |
6087 | } |
6088 | ||
2d66e025 | 6089 | m_inOnKeyDown = TRUE; |
f85afd4e | 6090 | |
2d66e025 MB |
6091 | // propagate the event up and see if it gets processed |
6092 | // | |
6093 | wxWindow *parent = GetParent(); | |
6094 | wxKeyEvent keyEvt( event ); | |
6095 | keyEvt.SetEventObject( parent ); | |
6096 | ||
6097 | if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) ) | |
f85afd4e | 6098 | { |
9b4aede2 | 6099 | |
2d66e025 MB |
6100 | // try local handlers |
6101 | // | |
12a3f227 | 6102 | switch ( event.GetKeyCode() ) |
2d66e025 MB |
6103 | { |
6104 | case WXK_UP: | |
6105 | if ( event.ControlDown() ) | |
6106 | { | |
5c8fc7c1 | 6107 | MoveCursorUpBlock( event.ShiftDown() ); |
2d66e025 MB |
6108 | } |
6109 | else | |
6110 | { | |
5c8fc7c1 | 6111 | MoveCursorUp( event.ShiftDown() ); |
2d66e025 MB |
6112 | } |
6113 | break; | |
f85afd4e | 6114 | |
2d66e025 MB |
6115 | case WXK_DOWN: |
6116 | if ( event.ControlDown() ) | |
6117 | { | |
5c8fc7c1 | 6118 | MoveCursorDownBlock( event.ShiftDown() ); |
2d66e025 MB |
6119 | } |
6120 | else | |
6121 | { | |
5c8fc7c1 | 6122 | MoveCursorDown( event.ShiftDown() ); |
2d66e025 MB |
6123 | } |
6124 | break; | |
8f177c8e | 6125 | |
2d66e025 MB |
6126 | case WXK_LEFT: |
6127 | if ( event.ControlDown() ) | |
6128 | { | |
5c8fc7c1 | 6129 | MoveCursorLeftBlock( event.ShiftDown() ); |
2d66e025 MB |
6130 | } |
6131 | else | |
6132 | { | |
5c8fc7c1 | 6133 | MoveCursorLeft( event.ShiftDown() ); |
2d66e025 MB |
6134 | } |
6135 | break; | |
6136 | ||
6137 | case WXK_RIGHT: | |
6138 | if ( event.ControlDown() ) | |
6139 | { | |
5c8fc7c1 | 6140 | MoveCursorRightBlock( event.ShiftDown() ); |
2d66e025 MB |
6141 | } |
6142 | else | |
6143 | { | |
5c8fc7c1 | 6144 | MoveCursorRight( event.ShiftDown() ); |
2d66e025 MB |
6145 | } |
6146 | break; | |
b99be8fb | 6147 | |
2d66e025 | 6148 | case WXK_RETURN: |
a4f7bf58 | 6149 | case WXK_NUMPAD_ENTER: |
58dd5b3b MB |
6150 | if ( event.ControlDown() ) |
6151 | { | |
6152 | event.Skip(); // to let the edit control have the return | |
6153 | } | |
6154 | else | |
6155 | { | |
f6bcfd97 BP |
6156 | if ( GetGridCursorRow() < GetNumberRows()-1 ) |
6157 | { | |
6158 | MoveCursorDown( event.ShiftDown() ); | |
6159 | } | |
6160 | else | |
6161 | { | |
6162 | // at the bottom of a column | |
6163 | HideCellEditControl(); | |
6164 | SaveEditControlValue(); | |
6165 | } | |
58dd5b3b | 6166 | } |
2d66e025 MB |
6167 | break; |
6168 | ||
5c8fc7c1 | 6169 | case WXK_ESCAPE: |
e32352cf | 6170 | ClearSelection(); |
5c8fc7c1 SN |
6171 | break; |
6172 | ||
2c9a89e0 RD |
6173 | case WXK_TAB: |
6174 | if (event.ShiftDown()) | |
f6bcfd97 BP |
6175 | { |
6176 | if ( GetGridCursorCol() > 0 ) | |
6177 | { | |
6178 | MoveCursorLeft( FALSE ); | |
6179 | } | |
6180 | else | |
6181 | { | |
6182 | // at left of grid | |
6183 | HideCellEditControl(); | |
6184 | SaveEditControlValue(); | |
6185 | } | |
6186 | } | |
2c9a89e0 | 6187 | else |
f6bcfd97 BP |
6188 | { |
6189 | if ( GetGridCursorCol() < GetNumberCols()-1 ) | |
6190 | { | |
6191 | MoveCursorRight( FALSE ); | |
6192 | } | |
6193 | else | |
6194 | { | |
6195 | // at right of grid | |
6196 | HideCellEditControl(); | |
6197 | SaveEditControlValue(); | |
6198 | } | |
6199 | } | |
2c9a89e0 RD |
6200 | break; |
6201 | ||
2d66e025 MB |
6202 | case WXK_HOME: |
6203 | if ( event.ControlDown() ) | |
6204 | { | |
6205 | MakeCellVisible( 0, 0 ); | |
6206 | SetCurrentCell( 0, 0 ); | |
6207 | } | |
6208 | else | |
6209 | { | |
6210 | event.Skip(); | |
6211 | } | |
6212 | break; | |
6213 | ||
6214 | case WXK_END: | |
6215 | if ( event.ControlDown() ) | |
6216 | { | |
6217 | MakeCellVisible( m_numRows-1, m_numCols-1 ); | |
6218 | SetCurrentCell( m_numRows-1, m_numCols-1 ); | |
6219 | } | |
6220 | else | |
6221 | { | |
6222 | event.Skip(); | |
6223 | } | |
6224 | break; | |
6225 | ||
6226 | case WXK_PRIOR: | |
6227 | MovePageUp(); | |
6228 | break; | |
6229 | ||
6230 | case WXK_NEXT: | |
6231 | MovePageDown(); | |
6232 | break; | |
6233 | ||
07296f0b | 6234 | case WXK_SPACE: |
aa5e1f75 SN |
6235 | if ( event.ControlDown() ) |
6236 | { | |
3f3dc2ef VZ |
6237 | if ( m_selection ) |
6238 | { | |
6239 | m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(), | |
6240 | m_currentCellCoords.GetCol(), | |
6241 | event.ControlDown(), | |
6242 | event.ShiftDown(), | |
6243 | event.AltDown(), | |
6244 | event.MetaDown() ); | |
6245 | } | |
aa5e1f75 SN |
6246 | break; |
6247 | } | |
07296f0b RD |
6248 | if ( !IsEditable() ) |
6249 | { | |
5c8fc7c1 | 6250 | MoveCursorRight( FALSE ); |
07296f0b RD |
6251 | break; |
6252 | } | |
6253 | // Otherwise fall through to default | |
6254 | ||
2d66e025 | 6255 | default: |
f6bcfd97 BP |
6256 | // is it possible to edit the current cell at all? |
6257 | if ( !IsCellEditControlEnabled() && CanEnableCellControl() ) | |
b54ba671 | 6258 | { |
f6bcfd97 | 6259 | // yes, now check whether the cells editor accepts the key |
f2d76237 RD |
6260 | int row = m_currentCellCoords.GetRow(); |
6261 | int col = m_currentCellCoords.GetCol(); | |
6262 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
0b190b0f | 6263 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
f6bcfd97 BP |
6264 | |
6265 | // <F2> is special and will always start editing, for | |
6266 | // other keys - ask the editor itself | |
12a3f227 | 6267 | if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers()) |
f6bcfd97 BP |
6268 | || editor->IsAcceptedKey(event) ) |
6269 | { | |
04418332 | 6270 | // ensure cell is visble |
73145b0e | 6271 | MakeCellVisible(row, col); |
f6bcfd97 | 6272 | EnableCellEditControl(); |
04418332 VZ |
6273 | |
6274 | // a problem can arise if the cell is not completely | |
6275 | // visible (even after calling MakeCellVisible the | |
6276 | // control is not created and calling StartingKey will | |
73145b0e JS |
6277 | // crash the app |
6278 | if( editor->IsCreated() && m_cellEditCtrlEnabled ) editor->StartingKey(event); | |
f6bcfd97 BP |
6279 | } |
6280 | else | |
6281 | { | |
6282 | event.Skip(); | |
6283 | } | |
6284 | ||
0b190b0f | 6285 | editor->DecRef(); |
2c9a89e0 RD |
6286 | attr->DecRef(); |
6287 | } | |
b3a7510d VZ |
6288 | else |
6289 | { | |
b94ae1ea VZ |
6290 | // let others process char events with modifiers or all |
6291 | // char events for readonly cells | |
b3a7510d VZ |
6292 | event.Skip(); |
6293 | } | |
025562fe | 6294 | break; |
2d66e025 | 6295 | } |
f85afd4e MB |
6296 | } |
6297 | ||
2d66e025 | 6298 | m_inOnKeyDown = FALSE; |
f85afd4e MB |
6299 | } |
6300 | ||
f6bcfd97 BP |
6301 | void wxGrid::OnKeyUp( wxKeyEvent& event ) |
6302 | { | |
6303 | // try local handlers | |
6304 | // | |
12a3f227 | 6305 | if ( event.GetKeyCode() == WXK_SHIFT ) |
f6bcfd97 BP |
6306 | { |
6307 | if ( m_selectingTopLeft != wxGridNoCellCoords && | |
6308 | m_selectingBottomRight != wxGridNoCellCoords ) | |
3f3dc2ef VZ |
6309 | { |
6310 | if ( m_selection ) | |
6311 | { | |
6312 | m_selection->SelectBlock( m_selectingTopLeft.GetRow(), | |
6313 | m_selectingTopLeft.GetCol(), | |
6314 | m_selectingBottomRight.GetRow(), | |
6315 | m_selectingBottomRight.GetCol(), | |
6316 | event.ControlDown(), | |
6317 | TRUE, | |
6318 | event.AltDown(), | |
6319 | event.MetaDown() ); | |
6320 | } | |
6321 | } | |
6322 | ||
f6bcfd97 BP |
6323 | m_selectingTopLeft = wxGridNoCellCoords; |
6324 | m_selectingBottomRight = wxGridNoCellCoords; | |
6325 | m_selectingKeyboard = wxGridNoCellCoords; | |
6326 | } | |
6327 | } | |
6328 | ||
2796cce3 | 6329 | void wxGrid::OnEraseBackground(wxEraseEvent&) |
508011ce VZ |
6330 | { |
6331 | } | |
07296f0b | 6332 | |
2d66e025 | 6333 | void wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) |
66242c80 | 6334 | { |
f6bcfd97 BP |
6335 | if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) ) |
6336 | { | |
6337 | // the event has been intercepted - do nothing | |
6338 | return; | |
6339 | } | |
6340 | ||
6341 | wxClientDC dc(m_gridWin); | |
6342 | PrepareDC(dc); | |
6343 | ||
2a7750d9 | 6344 | if ( m_currentCellCoords != wxGridNoCellCoords ) |
2d66e025 | 6345 | { |
2d66e025 | 6346 | HideCellEditControl(); |
b54ba671 | 6347 | DisableCellEditControl(); |
07296f0b | 6348 | |
3ca6a5f0 | 6349 | if ( IsVisible( m_currentCellCoords, FALSE ) ) |
f6bcfd97 BP |
6350 | { |
6351 | wxRect r; | |
3ed884a0 | 6352 | r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords); |
f6bcfd97 BP |
6353 | if ( !m_gridLinesEnabled ) |
6354 | { | |
6355 | r.x--; | |
6356 | r.y--; | |
6357 | r.width++; | |
6358 | r.height++; | |
6359 | } | |
d1c0b4f9 | 6360 | |
3ed884a0 | 6361 | wxGridCellCoordsArray cells = CalcCellsExposed( r ); |
84912ef8 | 6362 | |
f6bcfd97 BP |
6363 | // Otherwise refresh redraws the highlight! |
6364 | m_currentCellCoords = coords; | |
275c4ae4 | 6365 | |
d10f4bf9 | 6366 | DrawGridCellArea(dc,cells); |
f6bcfd97 BP |
6367 | DrawAllGridLines( dc, r ); |
6368 | } | |
66242c80 | 6369 | } |
8f177c8e | 6370 | |
2d66e025 MB |
6371 | m_currentCellCoords = coords; |
6372 | ||
2a7750d9 MB |
6373 | wxGridCellAttr* attr = GetCellAttr(coords); |
6374 | DrawCellHighlight(dc, attr); | |
6375 | attr->DecRef(); | |
66242c80 MB |
6376 | } |
6377 | ||
2d66e025 | 6378 | |
c9097836 MB |
6379 | void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol ) |
6380 | { | |
6381 | int temp; | |
6382 | wxGridCellCoords updateTopLeft, updateBottomRight; | |
6383 | ||
3f3dc2ef | 6384 | if ( m_selection ) |
c9097836 | 6385 | { |
3f3dc2ef VZ |
6386 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows ) |
6387 | { | |
6388 | leftCol = 0; | |
6389 | rightCol = GetNumberCols() - 1; | |
6390 | } | |
6391 | else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns ) | |
6392 | { | |
6393 | topRow = 0; | |
6394 | bottomRow = GetNumberRows() - 1; | |
6395 | } | |
c9097836 | 6396 | } |
3f3dc2ef | 6397 | |
c9097836 MB |
6398 | if ( topRow > bottomRow ) |
6399 | { | |
6400 | temp = topRow; | |
6401 | topRow = bottomRow; | |
6402 | bottomRow = temp; | |
6403 | } | |
6404 | ||
6405 | if ( leftCol > rightCol ) | |
6406 | { | |
6407 | temp = leftCol; | |
6408 | leftCol = rightCol; | |
6409 | rightCol = temp; | |
6410 | } | |
6411 | ||
6412 | updateTopLeft = wxGridCellCoords( topRow, leftCol ); | |
6413 | updateBottomRight = wxGridCellCoords( bottomRow, rightCol ); | |
6414 | ||
3ed884a0 SN |
6415 | // First the case that we selected a completely new area |
6416 | if ( m_selectingTopLeft == wxGridNoCellCoords || | |
6417 | m_selectingBottomRight == wxGridNoCellCoords ) | |
6418 | { | |
6419 | wxRect rect; | |
6420 | rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ), | |
6421 | wxGridCellCoords ( bottomRow, rightCol ) ); | |
2b5f62a0 | 6422 | m_gridWin->Refresh( FALSE, &rect ); |
3ed884a0 SN |
6423 | } |
6424 | // Now handle changing an existing selection area. | |
6425 | else if ( m_selectingTopLeft != updateTopLeft || | |
6426 | m_selectingBottomRight != updateBottomRight ) | |
c9097836 MB |
6427 | { |
6428 | // Compute two optimal update rectangles: | |
6429 | // Either one rectangle is a real subset of the | |
6430 | // other, or they are (almost) disjoint! | |
6431 | wxRect rect[4]; | |
6432 | bool need_refresh[4]; | |
6433 | need_refresh[0] = | |
6434 | need_refresh[1] = | |
6435 | need_refresh[2] = | |
6436 | need_refresh[3] = FALSE; | |
6437 | int i; | |
6438 | ||
6439 | // Store intermediate values | |
6440 | wxCoord oldLeft = m_selectingTopLeft.GetCol(); | |
6441 | wxCoord oldTop = m_selectingTopLeft.GetRow(); | |
6442 | wxCoord oldRight = m_selectingBottomRight.GetCol(); | |
6443 | wxCoord oldBottom = m_selectingBottomRight.GetRow(); | |
6444 | ||
6445 | // Determine the outer/inner coordinates. | |
6446 | if (oldLeft > leftCol) | |
6447 | { | |
6448 | temp = oldLeft; | |
6449 | oldLeft = leftCol; | |
6450 | leftCol = temp; | |
6451 | } | |
6452 | if (oldTop > topRow ) | |
6453 | { | |
6454 | temp = oldTop; | |
6455 | oldTop = topRow; | |
6456 | topRow = temp; | |
6457 | } | |
6458 | if (oldRight < rightCol ) | |
6459 | { | |
6460 | temp = oldRight; | |
6461 | oldRight = rightCol; | |
6462 | rightCol = temp; | |
6463 | } | |
6464 | if (oldBottom < bottomRow) | |
6465 | { | |
6466 | temp = oldBottom; | |
6467 | oldBottom = bottomRow; | |
6468 | bottomRow = temp; | |
6469 | } | |
6470 | ||
6471 | // Now, either the stuff marked old is the outer | |
6472 | // rectangle or we don't have a situation where one | |
6473 | // is contained in the other. | |
6474 | ||
6475 | if ( oldLeft < leftCol ) | |
6476 | { | |
3ed884a0 SN |
6477 | // Refresh the newly selected or deselected |
6478 | // area to the left of the old or new selection. | |
c9097836 MB |
6479 | need_refresh[0] = TRUE; |
6480 | rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
6481 | oldLeft ), | |
6482 | wxGridCellCoords ( oldBottom, | |
6483 | leftCol - 1 ) ); | |
6484 | } | |
6485 | ||
6486 | if ( oldTop < topRow ) | |
6487 | { | |
3ed884a0 SN |
6488 | // Refresh the newly selected or deselected |
6489 | // area above the old or new selection. | |
c9097836 MB |
6490 | need_refresh[1] = TRUE; |
6491 | rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
6492 | leftCol ), | |
6493 | wxGridCellCoords ( topRow - 1, | |
6494 | rightCol ) ); | |
6495 | } | |
6496 | ||
6497 | if ( oldRight > rightCol ) | |
6498 | { | |
3ed884a0 SN |
6499 | // Refresh the newly selected or deselected |
6500 | // area to the right of the old or new selection. | |
c9097836 MB |
6501 | need_refresh[2] = TRUE; |
6502 | rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
6503 | rightCol + 1 ), | |
6504 | wxGridCellCoords ( oldBottom, | |
6505 | oldRight ) ); | |
6506 | } | |
6507 | ||
6508 | if ( oldBottom > bottomRow ) | |
6509 | { | |
3ed884a0 SN |
6510 | // Refresh the newly selected or deselected |
6511 | // area below the old or new selection. | |
c9097836 MB |
6512 | need_refresh[3] = TRUE; |
6513 | rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1, | |
6514 | leftCol ), | |
6515 | wxGridCellCoords ( oldBottom, | |
6516 | rightCol ) ); | |
6517 | } | |
6518 | ||
c9097836 MB |
6519 | // various Refresh() calls |
6520 | for (i = 0; i < 4; i++ ) | |
6521 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
6522 | m_gridWin->Refresh( FALSE, &(rect[i]) ); | |
6523 | } | |
3ed884a0 SN |
6524 | // Change Selection |
6525 | m_selectingTopLeft = updateTopLeft; | |
6526 | m_selectingBottomRight = updateBottomRight; | |
c9097836 MB |
6527 | } |
6528 | ||
2d66e025 MB |
6529 | // |
6530 | // ------ functions to get/send data (see also public functions) | |
6531 | // | |
6532 | ||
6533 | bool wxGrid::GetModelValues() | |
66242c80 | 6534 | { |
bca7bfc8 SN |
6535 | // Hide the editor, so it won't hide a changed value. |
6536 | HideCellEditControl(); | |
c6707d16 | 6537 | |
2d66e025 | 6538 | if ( m_table ) |
66242c80 | 6539 | { |
2d66e025 | 6540 | // all we need to do is repaint the grid |
66242c80 | 6541 | // |
2d66e025 | 6542 | m_gridWin->Refresh(); |
66242c80 MB |
6543 | return TRUE; |
6544 | } | |
8f177c8e | 6545 | |
66242c80 MB |
6546 | return FALSE; |
6547 | } | |
6548 | ||
2d66e025 MB |
6549 | |
6550 | bool wxGrid::SetModelValues() | |
f85afd4e | 6551 | { |
2d66e025 | 6552 | int row, col; |
8f177c8e | 6553 | |
c6707d16 SN |
6554 | // Disable the editor, so it won't hide a changed value. |
6555 | // Do we also want to save the current value of the editor first? | |
6556 | // I think so ... | |
c6707d16 SN |
6557 | DisableCellEditControl(); |
6558 | ||
2d66e025 MB |
6559 | if ( m_table ) |
6560 | { | |
6561 | for ( row = 0; row < m_numRows; row++ ) | |
f85afd4e | 6562 | { |
2d66e025 | 6563 | for ( col = 0; col < m_numCols; col++ ) |
f85afd4e | 6564 | { |
2d66e025 | 6565 | m_table->SetValue( row, col, GetCellValue(row, col) ); |
f85afd4e MB |
6566 | } |
6567 | } | |
8f177c8e | 6568 | |
f85afd4e MB |
6569 | return TRUE; |
6570 | } | |
6571 | ||
6572 | return FALSE; | |
6573 | } | |
6574 | ||
2d66e025 MB |
6575 | |
6576 | ||
6577 | // Note - this function only draws cells that are in the list of | |
6578 | // exposed cells (usually set from the update region by | |
6579 | // CalcExposedCells) | |
6580 | // | |
d10f4bf9 | 6581 | void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells ) |
f85afd4e | 6582 | { |
2d66e025 | 6583 | if ( !m_numRows || !m_numCols ) return; |
60ff3b99 | 6584 | |
dc1f566f | 6585 | int i, numCells = cells.GetCount(); |
27f35b66 SN |
6586 | int row, col, cell_rows, cell_cols; |
6587 | wxGridCellCoordsArray redrawCells; | |
60ff3b99 | 6588 | |
27f35b66 | 6589 | for ( i = numCells-1; i >= 0; i-- ) |
f85afd4e | 6590 | { |
27f35b66 SN |
6591 | row = cells[i].GetRow(); |
6592 | col = cells[i].GetCol(); | |
6593 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
6594 | ||
6595 | // If this cell is part of a multicell block, find owner for repaint | |
6596 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
6597 | { | |
6598 | wxGridCellCoords cell(row+cell_rows, col+cell_cols); | |
6599 | bool marked = FALSE; | |
dc1f566f | 6600 | for ( int j = 0; j < numCells; j++ ) |
27f35b66 SN |
6601 | { |
6602 | if ( cell == cells[j] ) | |
6603 | { | |
6604 | marked = TRUE; | |
3ed884a0 | 6605 | break; |
27f35b66 SN |
6606 | } |
6607 | } | |
6608 | if (!marked) | |
6609 | { | |
6610 | int count = redrawCells.GetCount(); | |
dc1f566f | 6611 | for (int j = 0; j < count; j++) |
27f35b66 SN |
6612 | { |
6613 | if ( cell == redrawCells[j] ) | |
6614 | { | |
6615 | marked = TRUE; | |
6616 | break; | |
6617 | } | |
6618 | } | |
6619 | if (!marked) redrawCells.Add( cell ); | |
6620 | } | |
6621 | continue; // don't bother drawing this cell | |
6622 | } | |
6623 | ||
6624 | // If this cell is empty, find cell to left that might want to overflow | |
6625 | if (m_table && m_table->IsEmptyCell(row, col)) | |
6626 | { | |
dc1f566f | 6627 | for ( int l = 0; l < cell_rows; l++ ) |
27f35b66 | 6628 | { |
dc1f566f SN |
6629 | // find a cell in this row to left alreay marked for repaint |
6630 | int left = col; | |
6631 | for (int k = 0; k < int(redrawCells.GetCount()); k++) | |
6632 | if ((redrawCells[k].GetCol() < left) && | |
6633 | (redrawCells[k].GetRow() == row)) | |
6634 | left=redrawCells[k].GetCol(); | |
6635 | ||
6636 | if (left == col) left = 0; // oh well | |
6637 | ||
6638 | for (int j = col-1; j >= left; j--) | |
27f35b66 SN |
6639 | { |
6640 | if (!m_table->IsEmptyCell(row+l, j)) | |
6641 | { | |
6642 | if (GetCellOverflow(row+l, j)) | |
6643 | { | |
6644 | wxGridCellCoords cell(row+l, j); | |
6645 | bool marked = FALSE; | |
6646 | ||
dc1f566f | 6647 | for (int k = 0; k < numCells; k++) |
27f35b66 SN |
6648 | { |
6649 | if ( cell == cells[k] ) | |
6650 | { | |
6651 | marked = TRUE; | |
6652 | break; | |
6653 | } | |
6654 | } | |
6655 | if (!marked) | |
6656 | { | |
6657 | int count = redrawCells.GetCount(); | |
dc1f566f | 6658 | for (int k = 0; k < count; k++) |
27f35b66 SN |
6659 | { |
6660 | if ( cell == redrawCells[k] ) | |
6661 | { | |
6662 | marked = TRUE; | |
6663 | break; | |
6664 | } | |
6665 | } | |
6666 | if (!marked) redrawCells.Add( cell ); | |
6667 | } | |
6668 | } | |
6669 | break; | |
6670 | } | |
6671 | } | |
6672 | } | |
6673 | } | |
d10f4bf9 | 6674 | DrawCell( dc, cells[i] ); |
2d66e025 | 6675 | } |
27f35b66 SN |
6676 | |
6677 | numCells = redrawCells.GetCount(); | |
6678 | ||
6679 | for ( i = numCells - 1; i >= 0; i-- ) | |
6680 | { | |
6681 | DrawCell( dc, redrawCells[i] ); | |
6682 | } | |
2d66e025 | 6683 | } |
8f177c8e | 6684 | |
8f177c8e | 6685 | |
7c8a8ad5 MB |
6686 | void wxGrid::DrawGridSpace( wxDC& dc ) |
6687 | { | |
f6bcfd97 BP |
6688 | int cw, ch; |
6689 | m_gridWin->GetClientSize( &cw, &ch ); | |
7c8a8ad5 | 6690 | |
f6bcfd97 BP |
6691 | int right, bottom; |
6692 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7c8a8ad5 | 6693 | |
f6bcfd97 BP |
6694 | int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0; |
6695 | int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0 ; | |
7c8a8ad5 | 6696 | |
f6bcfd97 BP |
6697 | if ( right > rightCol || bottom > bottomRow ) |
6698 | { | |
6699 | int left, top; | |
6700 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7c8a8ad5 | 6701 | |
f6bcfd97 BP |
6702 | dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) ); |
6703 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
7c8a8ad5 | 6704 | |
f6bcfd97 BP |
6705 | if ( right > rightCol ) |
6706 | { | |
6707 | dc.DrawRectangle( rightCol, top, right - rightCol, ch); | |
6708 | } | |
6709 | ||
6710 | if ( bottom > bottomRow ) | |
6711 | { | |
6712 | dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow); | |
6713 | } | |
6714 | } | |
7c8a8ad5 MB |
6715 | } |
6716 | ||
6717 | ||
2d66e025 MB |
6718 | void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) |
6719 | { | |
ab79958a VZ |
6720 | int row = coords.GetRow(); |
6721 | int col = coords.GetCol(); | |
60ff3b99 | 6722 | |
7c1cb261 | 6723 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
ab79958a VZ |
6724 | return; |
6725 | ||
6726 | // we draw the cell border ourselves | |
9496deb5 | 6727 | #if !WXGRID_DRAW_LINES |
2d66e025 MB |
6728 | if ( m_gridLinesEnabled ) |
6729 | DrawCellBorder( dc, coords ); | |
796df70a | 6730 | #endif |
f85afd4e | 6731 | |
189d0213 VZ |
6732 | wxGridCellAttr* attr = GetCellAttr(row, col); |
6733 | ||
6734 | bool isCurrent = coords == m_currentCellCoords; | |
508011ce | 6735 | |
f6bcfd97 | 6736 | wxRect rect = CellToRect( row, col ); |
f85afd4e | 6737 | |
189d0213 | 6738 | // if the editor is shown, we should use it and not the renderer |
f6bcfd97 BP |
6739 | // Note: However, only if it is really _shown_, i.e. not hidden! |
6740 | if ( isCurrent && IsCellEditControlShown() ) | |
189d0213 | 6741 | { |
0b190b0f VZ |
6742 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
6743 | editor->PaintBackground(rect, attr); | |
6744 | editor->DecRef(); | |
189d0213 VZ |
6745 | } |
6746 | else | |
6747 | { | |
6748 | // but all the rest is drawn by the cell renderer and hence may be | |
6749 | // customized | |
0b190b0f VZ |
6750 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); |
6751 | renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); | |
6752 | renderer->DecRef(); | |
189d0213 | 6753 | } |
07296f0b | 6754 | |
283b7808 VZ |
6755 | attr->DecRef(); |
6756 | } | |
07296f0b | 6757 | |
283b7808 | 6758 | void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ) |
07296f0b RD |
6759 | { |
6760 | int row = m_currentCellCoords.GetRow(); | |
6761 | int col = m_currentCellCoords.GetCol(); | |
6762 | ||
7c1cb261 | 6763 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
07296f0b RD |
6764 | return; |
6765 | ||
f6bcfd97 | 6766 | wxRect rect = CellToRect(row, col); |
07296f0b | 6767 | |
b3a7510d VZ |
6768 | // hmmm... what could we do here to show that the cell is disabled? |
6769 | // for now, I just draw a thinner border than for the other ones, but | |
6770 | // it doesn't look really good | |
b3a7510d | 6771 | |
d2520c85 RD |
6772 | int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth; |
6773 | ||
27f35b66 SN |
6774 | if (penWidth > 0) |
6775 | { | |
d2520c85 RD |
6776 | // The center of th drawn line is where the position/width/height of |
6777 | // the rectangle is actually at, (on wxMSW atr least,) so we will | |
6778 | // reduce the size of the rectangle to compensate for the thickness of | |
6779 | // the line. If this is too strange on non wxMSW platforms then | |
6780 | // please #ifdef this appropriately. | |
6781 | rect.x += penWidth/2; | |
6782 | rect.y += penWidth/2; | |
6783 | rect.width -= penWidth-1; | |
6784 | rect.height -= penWidth-1; | |
6785 | ||
6786 | ||
6787 | // Now draw the rectangle | |
73145b0e JS |
6788 | // use the cellHighlightColour if the cell is inside a selection, this |
6789 | // will ensure the cell is always visible. | |
6790 | dc.SetPen(wxPen(IsInSelection(row,col)?m_selectionForeground:m_cellHighlightColour, penWidth, wxSOLID)); | |
d2520c85 RD |
6791 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
6792 | dc.DrawRectangle(rect); | |
6793 | } | |
07296f0b | 6794 | |
283b7808 | 6795 | #if 0 |
b3a7510d | 6796 | // VZ: my experiments with 3d borders... |
283b7808 | 6797 | |
b3a7510d | 6798 | // how to properly set colours for arbitrary bg? |
283b7808 VZ |
6799 | wxCoord x1 = rect.x, |
6800 | y1 = rect.y, | |
00cf1208 RR |
6801 | x2 = rect.x + rect.width -1, |
6802 | y2 = rect.y + rect.height -1; | |
283b7808 VZ |
6803 | |
6804 | dc.SetPen(*wxWHITE_PEN); | |
00cf1208 RR |
6805 | dc.DrawLine(x1, y1, x2, y1); |
6806 | dc.DrawLine(x1, y1, x1, y2); | |
283b7808 | 6807 | |
283b7808 | 6808 | dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1); |
00cf1208 | 6809 | dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 ); |
283b7808 VZ |
6810 | |
6811 | dc.SetPen(*wxBLACK_PEN); | |
6812 | dc.DrawLine(x1, y2, x2, y2); | |
00cf1208 | 6813 | dc.DrawLine(x2, y1, x2, y2+1); |
b3a7510d | 6814 | #endif // 0 |
2d66e025 | 6815 | } |
f85afd4e | 6816 | |
f2d76237 | 6817 | |
2d66e025 MB |
6818 | void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) |
6819 | { | |
2d66e025 MB |
6820 | int row = coords.GetRow(); |
6821 | int col = coords.GetCol(); | |
7c1cb261 VZ |
6822 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
6823 | return; | |
6824 | ||
6825 | dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) ); | |
60ff3b99 | 6826 | |
27f35b66 SN |
6827 | wxRect rect = CellToRect( row, col ); |
6828 | ||
2d66e025 MB |
6829 | // right hand border |
6830 | // | |
27f35b66 SN |
6831 | dc.DrawLine( rect.x + rect.width, rect.y, |
6832 | rect.x + rect.width, rect.y + rect.height + 1 ); | |
2d66e025 MB |
6833 | |
6834 | // bottom border | |
6835 | // | |
27f35b66 SN |
6836 | dc.DrawLine( rect.x, rect.y + rect.height, |
6837 | rect.x + rect.width, rect.y + rect.height); | |
f85afd4e MB |
6838 | } |
6839 | ||
d10f4bf9 | 6840 | void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells) |
b3a7510d | 6841 | { |
2a7750d9 MB |
6842 | // This if block was previously in wxGrid::OnPaint but that doesn't |
6843 | // seem to get called under wxGTK - MB | |
6844 | // | |
6845 | if ( m_currentCellCoords == wxGridNoCellCoords && | |
6846 | m_numRows && m_numCols ) | |
6847 | { | |
6848 | m_currentCellCoords.Set(0, 0); | |
6849 | } | |
6850 | ||
f6bcfd97 | 6851 | if ( IsCellEditControlShown() ) |
99306db2 VZ |
6852 | { |
6853 | // don't show highlight when the edit control is shown | |
6854 | return; | |
6855 | } | |
6856 | ||
b3a7510d VZ |
6857 | // if the active cell was repainted, repaint its highlight too because it |
6858 | // might have been damaged by the grid lines | |
d10f4bf9 | 6859 | size_t count = cells.GetCount(); |
b3a7510d VZ |
6860 | for ( size_t n = 0; n < count; n++ ) |
6861 | { | |
d10f4bf9 | 6862 | if ( cells[n] == m_currentCellCoords ) |
b3a7510d VZ |
6863 | { |
6864 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
6865 | DrawCellHighlight(dc, attr); | |
6866 | attr->DecRef(); | |
6867 | ||
6868 | break; | |
6869 | } | |
6870 | } | |
6871 | } | |
2d66e025 | 6872 | |
2d66e025 MB |
6873 | // TODO: remove this ??? |
6874 | // This is used to redraw all grid lines e.g. when the grid line colour | |
6875 | // has been changed | |
6876 | // | |
33ac7e6f | 6877 | void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) ) |
f85afd4e | 6878 | { |
27f35b66 SN |
6879 | #if !WXGRID_DRAW_LINES |
6880 | return; | |
6881 | #endif | |
6882 | ||
60ff3b99 VZ |
6883 | if ( !m_gridLinesEnabled || |
6884 | !m_numRows || | |
2d66e025 | 6885 | !m_numCols ) return; |
f85afd4e | 6886 | |
2d66e025 | 6887 | int top, bottom, left, right; |
796df70a | 6888 | |
f6bcfd97 | 6889 | #if 0 //#ifndef __WXGTK__ |
508011ce VZ |
6890 | if (reg.IsEmpty()) |
6891 | { | |
796df70a SN |
6892 | int cw, ch; |
6893 | m_gridWin->GetClientSize(&cw, &ch); | |
6894 | ||
6895 | // virtual coords of visible area | |
6896 | // | |
6897 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
6898 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
6899 | } | |
508011ce VZ |
6900 | else |
6901 | { | |
796df70a SN |
6902 | wxCoord x, y, w, h; |
6903 | reg.GetBox(x, y, w, h); | |
6904 | CalcUnscrolledPosition( x, y, &left, &top ); | |
6905 | CalcUnscrolledPosition( x + w, y + h, &right, &bottom ); | |
6906 | } | |
8e217128 RR |
6907 | #else |
6908 | int cw, ch; | |
6909 | m_gridWin->GetClientSize(&cw, &ch); | |
6910 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
6911 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
6912 | #endif | |
f85afd4e | 6913 | |
9496deb5 MB |
6914 | // avoid drawing grid lines past the last row and col |
6915 | // | |
7c1cb261 VZ |
6916 | right = wxMin( right, GetColRight(m_numCols - 1) ); |
6917 | bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) ); | |
9496deb5 | 6918 | |
27f35b66 SN |
6919 | // no gridlines inside multicells, clip them out |
6920 | int leftCol = XToCol(left); | |
6921 | int topRow = YToRow(top); | |
6922 | int rightCol = XToCol(right); | |
6923 | int bottomRow = YToRow(bottom); | |
6924 | wxRegion clippedcells(0, 0, cw, ch); | |
6925 | ||
6926 | if ((leftCol >= 0) && (topRow >= 0)) | |
6927 | { | |
6928 | if (rightCol < 0) rightCol = m_numCols; | |
6929 | if (bottomRow < 0) bottomRow = m_numRows; | |
6930 | ||
6931 | int i, j, cell_rows, cell_cols; | |
6932 | wxRect rect; | |
6933 | ||
6934 | for (j=topRow; j<bottomRow; j++) | |
6935 | { | |
6936 | for (i=leftCol; i<rightCol; i++) | |
6937 | { | |
6938 | GetCellSize( j, i, &cell_rows, &cell_cols ); | |
6939 | if ((cell_rows > 1) || (cell_cols > 1)) | |
6940 | { | |
6941 | rect = CellToRect(j,i); | |
6942 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
6943 | clippedcells.Subtract(rect); | |
6944 | } | |
6945 | else if ((cell_rows < 0) || (cell_cols < 0)) | |
6946 | { | |
6947 | rect = CellToRect(j+cell_rows, i+cell_cols); | |
6948 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
6949 | clippedcells.Subtract(rect); | |
6950 | } | |
6951 | } | |
6952 | } | |
6953 | } | |
6954 | dc.SetClippingRegion( clippedcells ); | |
6955 | ||
2d66e025 | 6956 | dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) ); |
f85afd4e | 6957 | |
2d66e025 | 6958 | // horizontal grid lines |
f85afd4e | 6959 | // |
60ff3b99 | 6960 | int i; |
33188aa4 | 6961 | for ( i = internalYToRow(top); i < m_numRows; i++ ) |
f85afd4e | 6962 | { |
6d55126d | 6963 | int bot = GetRowBottom(i) - 1; |
7c1cb261 | 6964 | |
6d55126d | 6965 | if ( bot > bottom ) |
2d66e025 MB |
6966 | { |
6967 | break; | |
6968 | } | |
7c1cb261 | 6969 | |
6d55126d | 6970 | if ( bot >= top ) |
2d66e025 | 6971 | { |
6d55126d | 6972 | dc.DrawLine( left, bot, right, bot ); |
2d66e025 | 6973 | } |
f85afd4e MB |
6974 | } |
6975 | ||
f85afd4e | 6976 | |
2d66e025 MB |
6977 | // vertical grid lines |
6978 | // | |
33188aa4 | 6979 | for ( i = internalXToCol(left); i < m_numCols; i++ ) |
f85afd4e | 6980 | { |
7c1cb261 VZ |
6981 | int colRight = GetColRight(i) - 1; |
6982 | if ( colRight > right ) | |
2d66e025 MB |
6983 | { |
6984 | break; | |
6985 | } | |
7c1cb261 VZ |
6986 | |
6987 | if ( colRight >= left ) | |
2d66e025 | 6988 | { |
7c1cb261 | 6989 | dc.DrawLine( colRight, top, colRight, bottom ); |
2d66e025 MB |
6990 | } |
6991 | } | |
27f35b66 | 6992 | dc.DestroyClippingRegion(); |
2d66e025 | 6993 | } |
f85afd4e | 6994 | |
8f177c8e | 6995 | |
d10f4bf9 | 6996 | void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows) |
2d66e025 | 6997 | { |
f6bcfd97 | 6998 | if ( !m_numRows ) return; |
60ff3b99 | 6999 | |
2d66e025 | 7000 | size_t i; |
d10f4bf9 | 7001 | size_t numLabels = rows.GetCount(); |
60ff3b99 | 7002 | |
2d66e025 MB |
7003 | for ( i = 0; i < numLabels; i++ ) |
7004 | { | |
d10f4bf9 | 7005 | DrawRowLabel( dc, rows[i] ); |
60ff3b99 | 7006 | } |
f85afd4e MB |
7007 | } |
7008 | ||
7009 | ||
2d66e025 | 7010 | void wxGrid::DrawRowLabel( wxDC& dc, int row ) |
f85afd4e | 7011 | { |
7c1cb261 VZ |
7012 | if ( GetRowHeight(row) <= 0 ) |
7013 | return; | |
60ff3b99 | 7014 | |
7c1cb261 VZ |
7015 | int rowTop = GetRowTop(row), |
7016 | rowBottom = GetRowBottom(row) - 1; | |
b99be8fb | 7017 | |
73145b0e | 7018 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) ); |
b0d6ff2f | 7019 | dc.DrawLine( m_rowLabelWidth-1, rowTop, |
7c1cb261 | 7020 | m_rowLabelWidth-1, rowBottom ); |
b99be8fb | 7021 | |
73145b0e JS |
7022 | dc.DrawLine( 0, rowTop, 0, rowBottom ); |
7023 | ||
7024 | dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom ); | |
b99be8fb | 7025 | |
2d66e025 | 7026 | dc.SetPen( *wxWHITE_PEN ); |
73145b0e JS |
7027 | dc.DrawLine( 1, rowTop, 1, rowBottom ); |
7028 | dc.DrawLine( 1, rowTop, m_rowLabelWidth-1, rowTop ); | |
60ff3b99 | 7029 | |
f85afd4e | 7030 | dc.SetBackgroundMode( wxTRANSPARENT ); |
f85afd4e MB |
7031 | dc.SetTextForeground( GetLabelTextColour() ); |
7032 | dc.SetFont( GetLabelFont() ); | |
8f177c8e | 7033 | |
f85afd4e | 7034 | int hAlign, vAlign; |
2d66e025 | 7035 | GetRowLabelAlignment( &hAlign, &vAlign ); |
60ff3b99 | 7036 | |
2d66e025 MB |
7037 | wxRect rect; |
7038 | rect.SetX( 2 ); | |
7c1cb261 | 7039 | rect.SetY( GetRowTop(row) + 2 ); |
2d66e025 | 7040 | rect.SetWidth( m_rowLabelWidth - 4 ); |
7c1cb261 | 7041 | rect.SetHeight( GetRowHeight(row) - 4 ); |
60ff3b99 | 7042 | DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign ); |
f85afd4e MB |
7043 | } |
7044 | ||
7045 | ||
d10f4bf9 | 7046 | void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols ) |
f85afd4e | 7047 | { |
f6bcfd97 | 7048 | if ( !m_numCols ) return; |
60ff3b99 | 7049 | |
2d66e025 | 7050 | size_t i; |
d10f4bf9 | 7051 | size_t numLabels = cols.GetCount(); |
60ff3b99 | 7052 | |
2d66e025 | 7053 | for ( i = 0; i < numLabels; i++ ) |
f85afd4e | 7054 | { |
d10f4bf9 | 7055 | DrawColLabel( dc, cols[i] ); |
60ff3b99 | 7056 | } |
f85afd4e MB |
7057 | } |
7058 | ||
7059 | ||
2d66e025 | 7060 | void wxGrid::DrawColLabel( wxDC& dc, int col ) |
f85afd4e | 7061 | { |
7c1cb261 VZ |
7062 | if ( GetColWidth(col) <= 0 ) |
7063 | return; | |
60ff3b99 | 7064 | |
7c1cb261 VZ |
7065 | int colLeft = GetColLeft(col), |
7066 | colRight = GetColRight(col) - 1; | |
b99be8fb | 7067 | |
73145b0e | 7068 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) ); |
7c1cb261 VZ |
7069 | dc.DrawLine( colRight, 0, |
7070 | colRight, m_colLabelHeight-1 ); | |
b99be8fb | 7071 | |
73145b0e JS |
7072 | dc.DrawLine( colLeft, 0, colRight, 0 ); |
7073 | ||
b0d6ff2f | 7074 | dc.DrawLine( colLeft, m_colLabelHeight-1, |
73145b0e | 7075 | colRight+1, m_colLabelHeight-1 ); |
b99be8fb | 7076 | |
f85afd4e | 7077 | dc.SetPen( *wxWHITE_PEN ); |
73145b0e JS |
7078 | dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 ); |
7079 | dc.DrawLine( colLeft, 1, colRight, 1 ); | |
f85afd4e | 7080 | |
b0d6ff2f MB |
7081 | dc.SetBackgroundMode( wxTRANSPARENT ); |
7082 | dc.SetTextForeground( GetLabelTextColour() ); | |
7083 | dc.SetFont( GetLabelFont() ); | |
8f177c8e | 7084 | |
d43851f7 | 7085 | int hAlign, vAlign, orient; |
2d66e025 | 7086 | GetColLabelAlignment( &hAlign, &vAlign ); |
d43851f7 | 7087 | orient = GetColLabelTextOrientation(); |
60ff3b99 | 7088 | |
2d66e025 | 7089 | wxRect rect; |
7c1cb261 | 7090 | rect.SetX( colLeft + 2 ); |
2d66e025 | 7091 | rect.SetY( 2 ); |
7c1cb261 | 7092 | rect.SetWidth( GetColWidth(col) - 4 ); |
2d66e025 | 7093 | rect.SetHeight( m_colLabelHeight - 4 ); |
d43851f7 | 7094 | DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient ); |
f85afd4e MB |
7095 | } |
7096 | ||
2d66e025 MB |
7097 | void wxGrid::DrawTextRectangle( wxDC& dc, |
7098 | const wxString& value, | |
7099 | const wxRect& rect, | |
7100 | int horizAlign, | |
d43851f7 JS |
7101 | int vertAlign, |
7102 | int textOrientation ) | |
f85afd4e | 7103 | { |
2d66e025 | 7104 | wxArrayString lines; |
038a5591 | 7105 | |
2d66e025 | 7106 | StringToLines( value, lines ); |
038a5591 JS |
7107 | |
7108 | ||
d10f4bf9 VZ |
7109 | //Forward to new API. |
7110 | DrawTextRectangle( dc, | |
038a5591 JS |
7111 | lines, |
7112 | rect, | |
7113 | horizAlign, | |
7114 | vertAlign, | |
7115 | textOrientation ); | |
7116 | ||
d10f4bf9 VZ |
7117 | } |
7118 | ||
7119 | void wxGrid::DrawTextRectangle( wxDC& dc, | |
038a5591 JS |
7120 | const wxArrayString& lines, |
7121 | const wxRect& rect, | |
7122 | int horizAlign, | |
7123 | int vertAlign, | |
7124 | int textOrientation ) | |
d10f4bf9 VZ |
7125 | { |
7126 | long textWidth, textHeight; | |
7127 | long lineWidth, lineHeight; | |
73145b0e | 7128 | int nLines; |
038a5591 | 7129 | |
a373d23b | 7130 | dc.SetClippingRegion( rect ); |
038a5591 | 7131 | |
73145b0e JS |
7132 | nLines = lines.GetCount(); |
7133 | if( nLines > 0 ) | |
2d66e025 | 7134 | { |
038a5591 JS |
7135 | int l; |
7136 | float x = 0.0, y = 0.0; | |
7137 | ||
7138 | if( textOrientation == wxHORIZONTAL ) | |
7139 | GetTextBoxSize(dc, lines, &textWidth, &textHeight); | |
7140 | else | |
7141 | GetTextBoxSize( dc, lines, &textHeight, &textWidth ); | |
7142 | ||
7143 | switch( vertAlign ) | |
7144 | { | |
73145b0e | 7145 | case wxALIGN_BOTTOM: |
d43851f7 | 7146 | if( textOrientation == wxHORIZONTAL ) |
038a5591 | 7147 | y = rect.y + (rect.height - textHeight - 1); |
d43851f7 | 7148 | else |
038a5591 JS |
7149 | x = rect.x + rect.width - textWidth; |
7150 | break; | |
7151 | ||
73145b0e | 7152 | case wxALIGN_CENTRE: |
d43851f7 | 7153 | if( textOrientation == wxHORIZONTAL ) |
038a5591 | 7154 | y = rect.y + ((rect.height - textHeight)/2); |
d43851f7 | 7155 | else |
038a5591 JS |
7156 | x = rect.x + ((rect.width - textWidth)/2); |
7157 | break; | |
7158 | ||
73145b0e JS |
7159 | case wxALIGN_TOP: |
7160 | default: | |
d43851f7 | 7161 | if( textOrientation == wxHORIZONTAL ) |
038a5591 | 7162 | y = rect.y + 1; |
d43851f7 | 7163 | else |
038a5591 | 7164 | x = rect.x + 1; |
73145b0e | 7165 | break; |
f85afd4e | 7166 | } |
038a5591 JS |
7167 | |
7168 | // Align each line of a multi-line label | |
7169 | for( l = 0; l < nLines; l++ ) | |
7170 | { | |
7171 | dc.GetTextExtent(lines[l], &lineWidth, &lineHeight); | |
7172 | ||
7173 | switch( horizAlign ) | |
7174 | { | |
7175 | case wxALIGN_RIGHT: | |
7176 | if( textOrientation == wxHORIZONTAL ) | |
7177 | x = rect.x + (rect.width - lineWidth - 1); | |
7178 | else | |
7179 | y = rect.y + lineWidth + 1; | |
7180 | break; | |
7181 | ||
7182 | case wxALIGN_CENTRE: | |
7183 | if( textOrientation == wxHORIZONTAL ) | |
7184 | x = rect.x + ((rect.width - lineWidth)/2); | |
7185 | else | |
7186 | y = rect.y + rect.height - ((rect.height - lineWidth)/2); | |
7187 | break; | |
7188 | ||
7189 | case wxALIGN_LEFT: | |
7190 | default: | |
7191 | if( textOrientation == wxHORIZONTAL ) | |
7192 | x = rect.x + 1; | |
7193 | else | |
7194 | y = rect.y + rect.height - 1; | |
7195 | break; | |
7196 | } | |
7197 | ||
7198 | if( textOrientation == wxHORIZONTAL ) | |
7199 | { | |
7200 | dc.DrawText( lines[l], (int)x, (int)y ); | |
7201 | y += lineHeight; | |
7202 | } | |
7203 | else | |
7204 | { | |
7205 | dc.DrawRotatedText( lines[l], (int)x, (int)y, 90.0 ); | |
7206 | x += lineHeight; | |
7207 | } | |
7208 | } | |
f85afd4e | 7209 | } |
f85afd4e | 7210 | dc.DestroyClippingRegion(); |
f85afd4e MB |
7211 | } |
7212 | ||
7213 | ||
7214 | // Split multi line text up into an array of strings. Any existing | |
7215 | // contents of the string array are preserved. | |
7216 | // | |
7217 | void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) | |
7218 | { | |
f85afd4e MB |
7219 | int startPos = 0; |
7220 | int pos; | |
6d004f67 | 7221 | wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix ); |
2433bb2e | 7222 | wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix ); |
e2b42eeb | 7223 | |
2433bb2e | 7224 | while ( startPos < (int)tVal.Length() ) |
f85afd4e | 7225 | { |
2433bb2e | 7226 | pos = tVal.Mid(startPos).Find( eol ); |
f85afd4e MB |
7227 | if ( pos < 0 ) |
7228 | { | |
7229 | break; | |
7230 | } | |
7231 | else if ( pos == 0 ) | |
7232 | { | |
7233 | lines.Add( wxEmptyString ); | |
7234 | } | |
7235 | else | |
7236 | { | |
2433bb2e | 7237 | lines.Add( value.Mid(startPos, pos) ); |
f85afd4e MB |
7238 | } |
7239 | startPos += pos+1; | |
7240 | } | |
b540eb2b | 7241 | if ( startPos < (int)value.Length() ) |
f85afd4e MB |
7242 | { |
7243 | lines.Add( value.Mid( startPos ) ); | |
7244 | } | |
7245 | } | |
7246 | ||
7247 | ||
7248 | void wxGrid::GetTextBoxSize( wxDC& dc, | |
d10f4bf9 | 7249 | const wxArrayString& lines, |
f85afd4e MB |
7250 | long *width, long *height ) |
7251 | { | |
7252 | long w = 0; | |
7253 | long h = 0; | |
7254 | long lineW, lineH; | |
7255 | ||
b540eb2b | 7256 | size_t i; |
f85afd4e MB |
7257 | for ( i = 0; i < lines.GetCount(); i++ ) |
7258 | { | |
7259 | dc.GetTextExtent( lines[i], &lineW, &lineH ); | |
7260 | w = wxMax( w, lineW ); | |
7261 | h += lineH; | |
7262 | } | |
7263 | ||
7264 | *width = w; | |
7265 | *height = h; | |
7266 | } | |
7267 | ||
f6bcfd97 BP |
7268 | // |
7269 | // ------ Batch processing. | |
7270 | // | |
7271 | void wxGrid::EndBatch() | |
7272 | { | |
7273 | if ( m_batchCount > 0 ) | |
7274 | { | |
7275 | m_batchCount--; | |
7276 | if ( !m_batchCount ) | |
7277 | { | |
7278 | CalcDimensions(); | |
7279 | m_rowLabelWin->Refresh(); | |
7280 | m_colLabelWin->Refresh(); | |
7281 | m_cornerLabelWin->Refresh(); | |
7282 | m_gridWin->Refresh(); | |
7283 | } | |
7284 | } | |
7285 | } | |
f85afd4e | 7286 | |
d8232393 MB |
7287 | // Use this, rather than wxWindow::Refresh(), to force an immediate |
7288 | // repainting of the grid. Has no effect if you are already inside a | |
7289 | // BeginBatch / EndBatch block. | |
7290 | // | |
7291 | void wxGrid::ForceRefresh() | |
7292 | { | |
7293 | BeginBatch(); | |
7294 | EndBatch(); | |
7295 | } | |
7296 | ||
7297 | ||
f85afd4e | 7298 | // |
2d66e025 | 7299 | // ------ Edit control functions |
f85afd4e MB |
7300 | // |
7301 | ||
2d66e025 MB |
7302 | |
7303 | void wxGrid::EnableEditing( bool edit ) | |
f85afd4e | 7304 | { |
2d66e025 MB |
7305 | // TODO: improve this ? |
7306 | // | |
7307 | if ( edit != m_editable ) | |
f85afd4e | 7308 | { |
632efa47 | 7309 | if(!edit) EnableCellEditControl(edit); |
2d66e025 | 7310 | m_editable = edit; |
f85afd4e | 7311 | } |
f85afd4e MB |
7312 | } |
7313 | ||
7314 | ||
2d66e025 | 7315 | void wxGrid::EnableCellEditControl( bool enable ) |
f85afd4e | 7316 | { |
2c9a89e0 RD |
7317 | if (! m_editable) |
7318 | return; | |
7319 | ||
dcfe4c3d SN |
7320 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
7321 | SetCurrentCell( 0, 0 ); | |
60ff3b99 | 7322 | |
2c9a89e0 RD |
7323 | if ( enable != m_cellEditCtrlEnabled ) |
7324 | { | |
dcfe4c3d | 7325 | if ( enable ) |
f85afd4e | 7326 | { |
97a9929e VZ |
7327 | if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0) |
7328 | return; | |
fe7b9ed6 | 7329 | |
97a9929e | 7330 | // this should be checked by the caller! |
b54ba671 VZ |
7331 | wxASSERT_MSG( CanEnableCellControl(), |
7332 | _T("can't enable editing for this cell!") ); | |
7333 | ||
7334 | // do it before ShowCellEditControl() | |
dcfe4c3d | 7335 | m_cellEditCtrlEnabled = enable; |
b54ba671 | 7336 | |
2d66e025 | 7337 | ShowCellEditControl(); |
2d66e025 MB |
7338 | } |
7339 | else | |
7340 | { | |
97a9929e VZ |
7341 | //FIXME:add veto support |
7342 | SendEvent( wxEVT_GRID_EDITOR_HIDDEN); | |
fe7b9ed6 | 7343 | |
97a9929e | 7344 | HideCellEditControl(); |
2d66e025 | 7345 | SaveEditControlValue(); |
b54ba671 VZ |
7346 | |
7347 | // do it after HideCellEditControl() | |
dcfe4c3d | 7348 | m_cellEditCtrlEnabled = enable; |
f85afd4e | 7349 | } |
f85afd4e | 7350 | } |
f85afd4e | 7351 | } |
f85afd4e | 7352 | |
b54ba671 | 7353 | bool wxGrid::IsCurrentCellReadOnly() const |
283b7808 | 7354 | { |
b54ba671 VZ |
7355 | // const_cast |
7356 | wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords); | |
7357 | bool readonly = attr->IsReadOnly(); | |
7358 | attr->DecRef(); | |
283b7808 | 7359 | |
b54ba671 VZ |
7360 | return readonly; |
7361 | } | |
283b7808 | 7362 | |
b54ba671 VZ |
7363 | bool wxGrid::CanEnableCellControl() const |
7364 | { | |
7365 | return m_editable && !IsCurrentCellReadOnly(); | |
7366 | } | |
7367 | ||
7368 | bool wxGrid::IsCellEditControlEnabled() const | |
7369 | { | |
7370 | // the cell edit control might be disable for all cells or just for the | |
7371 | // current one if it's read only | |
7372 | return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE; | |
283b7808 VZ |
7373 | } |
7374 | ||
f6bcfd97 BP |
7375 | bool wxGrid::IsCellEditControlShown() const |
7376 | { | |
7377 | bool isShown = FALSE; | |
7378 | ||
7379 | if ( m_cellEditCtrlEnabled ) | |
7380 | { | |
7381 | int row = m_currentCellCoords.GetRow(); | |
7382 | int col = m_currentCellCoords.GetCol(); | |
7383 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
7384 | wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col); | |
7385 | attr->DecRef(); | |
7386 | ||
7387 | if ( editor ) | |
7388 | { | |
7389 | if ( editor->IsCreated() ) | |
7390 | { | |
7391 | isShown = editor->GetControl()->IsShown(); | |
7392 | } | |
7393 | ||
7394 | editor->DecRef(); | |
7395 | } | |
7396 | } | |
7397 | ||
7398 | return isShown; | |
7399 | } | |
7400 | ||
2d66e025 | 7401 | void wxGrid::ShowCellEditControl() |
f85afd4e | 7402 | { |
2d66e025 | 7403 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 7404 | { |
2d66e025 MB |
7405 | if ( !IsVisible( m_currentCellCoords ) ) |
7406 | { | |
3ca6a5f0 | 7407 | m_cellEditCtrlEnabled = FALSE; |
2d66e025 MB |
7408 | return; |
7409 | } | |
7410 | else | |
7411 | { | |
2c9a89e0 RD |
7412 | wxRect rect = CellToRect( m_currentCellCoords ); |
7413 | int row = m_currentCellCoords.GetRow(); | |
7414 | int col = m_currentCellCoords.GetCol(); | |
2d66e025 | 7415 | |
27f35b66 SN |
7416 | // if this is part of a multicell, find owner (topleft) |
7417 | int cell_rows, cell_cols; | |
7418 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
7419 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
7420 | { | |
7421 | row += cell_rows; | |
7422 | col += cell_cols; | |
7423 | m_currentCellCoords.SetRow( row ); | |
7424 | m_currentCellCoords.SetCol( col ); | |
7425 | } | |
7426 | ||
2d66e025 MB |
7427 | // convert to scrolled coords |
7428 | // | |
99306db2 | 7429 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
508011ce | 7430 | |
99306db2 VZ |
7431 | // done in PaintBackground() |
7432 | #if 0 | |
7433 | // erase the highlight and the cell contents because the editor | |
7434 | // might not cover the entire cell | |
7435 | wxClientDC dc( m_gridWin ); | |
7436 | PrepareDC( dc ); | |
7437 | dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID)); | |
7438 | dc.SetPen(*wxTRANSPARENT_PEN); | |
7439 | dc.DrawRectangle(rect); | |
7440 | #endif // 0 | |
d2fdd8d2 | 7441 | |
99306db2 | 7442 | // cell is shifted by one pixel |
68c5a31c | 7443 | // However, don't allow x or y to become negative |
70e8d961 | 7444 | // since the SetSize() method interprets that as |
68c5a31c SN |
7445 | // "don't change." |
7446 | if (rect.x > 0) | |
7447 | rect.x--; | |
7448 | if (rect.y > 0) | |
7449 | rect.y--; | |
f0102d2a | 7450 | |
508011ce | 7451 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 7452 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
3da93aae VZ |
7453 | if ( !editor->IsCreated() ) |
7454 | { | |
2c9a89e0 RD |
7455 | editor->Create(m_gridWin, -1, |
7456 | new wxGridCellEditorEvtHandler(this, editor)); | |
bf7945ce RD |
7457 | |
7458 | wxGridEditorCreatedEvent evt(GetId(), | |
7459 | wxEVT_GRID_EDITOR_CREATED, | |
7460 | this, | |
7461 | row, | |
7462 | col, | |
7463 | editor->GetControl()); | |
7464 | GetEventHandler()->ProcessEvent(evt); | |
2d66e025 MB |
7465 | } |
7466 | ||
0b190b0f | 7467 | |
27f35b66 | 7468 | // resize editor to overflow into righthand cells if allowed |
39b80349 | 7469 | int maxWidth = rect.width; |
27f35b66 SN |
7470 | wxString value = GetCellValue(row, col); |
7471 | if ( (value != wxEmptyString) && (attr->GetOverflow()) ) | |
7472 | { | |
39b80349 SN |
7473 | int y; |
7474 | GetTextExtent(value, &maxWidth, &y, | |
2b5f62a0 | 7475 | NULL, NULL, &attr->GetFont()); |
39b80349 SN |
7476 | if (maxWidth < rect.width) maxWidth = rect.width; |
7477 | } | |
7478 | int client_right = m_gridWin->GetClientSize().GetWidth(); | |
7479 | if (rect.x+maxWidth > client_right) | |
2b5f62a0 | 7480 | maxWidth = client_right - rect.x; |
39b80349 | 7481 | |
2b5f62a0 VZ |
7482 | if ((maxWidth > rect.width) && (col < m_numCols) && m_table) |
7483 | { | |
39b80349 | 7484 | GetCellSize( row, col, &cell_rows, &cell_cols ); |
2b5f62a0 VZ |
7485 | // may have changed earlier |
7486 | for (int i = col+cell_cols; i < m_numCols; i++) | |
7487 | { | |
39b80349 SN |
7488 | int c_rows, c_cols; |
7489 | GetCellSize( row, i, &c_rows, &c_cols ); | |
2b5f62a0 VZ |
7490 | // looks weird going over a multicell |
7491 | if (m_table->IsEmptyCell(row,i) && | |
7492 | (rect.width < maxWidth) && (c_rows == 1)) | |
7493 | rect.width += GetColWidth(i); | |
7494 | else | |
7495 | break; | |
7496 | } | |
39b80349 | 7497 | if (rect.GetRight() > client_right) |
2b5f62a0 | 7498 | rect.SetRight(client_right-1); |
27f35b66 | 7499 | } |
73145b0e | 7500 | |
1bd71df9 | 7501 | editor->SetCellAttr(attr); |
2c9a89e0 | 7502 | editor->SetSize( rect ); |
73145b0e | 7503 | editor->Show( TRUE, attr ); |
04418332 VZ |
7504 | |
7505 | // recalc dimensions in case we need to | |
7506 | // expand the scrolled window to account for editor | |
73145b0e | 7507 | CalcDimensions(); |
99306db2 | 7508 | |
3da93aae | 7509 | editor->BeginEdit(row, col, this); |
1bd71df9 | 7510 | editor->SetCellAttr(NULL); |
0b190b0f VZ |
7511 | |
7512 | editor->DecRef(); | |
2c9a89e0 | 7513 | attr->DecRef(); |
2b5f62a0 | 7514 | } |
f85afd4e MB |
7515 | } |
7516 | } | |
7517 | ||
7518 | ||
2d66e025 | 7519 | void wxGrid::HideCellEditControl() |
f85afd4e | 7520 | { |
2d66e025 | 7521 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 7522 | { |
2c9a89e0 RD |
7523 | int row = m_currentCellCoords.GetRow(); |
7524 | int col = m_currentCellCoords.GetCol(); | |
7525 | ||
3da93aae | 7526 | wxGridCellAttr* attr = GetCellAttr(row, col); |
0b190b0f VZ |
7527 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
7528 | editor->Show( FALSE ); | |
7529 | editor->DecRef(); | |
2c9a89e0 RD |
7530 | attr->DecRef(); |
7531 | m_gridWin->SetFocus(); | |
27f35b66 SN |
7532 | // refresh whole row to the right |
7533 | wxRect rect( CellToRect(row, col) ); | |
7534 | CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y ); | |
7535 | rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x; | |
f6bcfd97 | 7536 | m_gridWin->Refresh( FALSE, &rect ); |
f85afd4e | 7537 | } |
2d66e025 | 7538 | } |
8f177c8e | 7539 | |
2d66e025 | 7540 | |
2d66e025 MB |
7541 | void wxGrid::SaveEditControlValue() |
7542 | { | |
3da93aae VZ |
7543 | if ( IsCellEditControlEnabled() ) |
7544 | { | |
2c9a89e0 RD |
7545 | int row = m_currentCellCoords.GetRow(); |
7546 | int col = m_currentCellCoords.GetCol(); | |
8f177c8e | 7547 | |
fe7b9ed6 VZ |
7548 | wxString oldval = GetCellValue(row,col); |
7549 | ||
3da93aae | 7550 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 7551 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
3324d5f5 | 7552 | bool changed = editor->EndEdit(row, col, this); |
2d66e025 | 7553 | |
0b190b0f | 7554 | editor->DecRef(); |
2c9a89e0 | 7555 | attr->DecRef(); |
2d66e025 | 7556 | |
3da93aae VZ |
7557 | if (changed) |
7558 | { | |
fe7b9ed6 | 7559 | if ( SendEvent( wxEVT_GRID_CELL_CHANGE, |
2d66e025 | 7560 | m_currentCellCoords.GetRow(), |
fe7b9ed6 VZ |
7561 | m_currentCellCoords.GetCol() ) < 0 ) { |
7562 | ||
97a9929e VZ |
7563 | // Event has been vetoed, set the data back. |
7564 | SetCellValue(row,col,oldval); | |
7565 | } | |
2d66e025 | 7566 | } |
f85afd4e MB |
7567 | } |
7568 | } | |
7569 | ||
2d66e025 MB |
7570 | |
7571 | // | |
60ff3b99 VZ |
7572 | // ------ Grid location functions |
7573 | // Note that all of these functions work with the logical coordinates of | |
2d66e025 MB |
7574 | // grid cells and labels so you will need to convert from device |
7575 | // coordinates for mouse events etc. | |
7576 | // | |
7577 | ||
7578 | void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords ) | |
f85afd4e | 7579 | { |
58dd5b3b MB |
7580 | int row = YToRow(y); |
7581 | int col = XToCol(x); | |
7582 | ||
7583 | if ( row == -1 || col == -1 ) | |
7584 | { | |
7585 | coords = wxGridNoCellCoords; | |
7586 | } | |
7587 | else | |
7588 | { | |
7589 | coords.Set( row, col ); | |
7590 | } | |
2d66e025 | 7591 | } |
f85afd4e | 7592 | |
8f177c8e | 7593 | |
b1da8107 SN |
7594 | // Internal Helper function for computing row or column from some |
7595 | // (unscrolled) coordinate value, using either | |
70e8d961 | 7596 | // m_defaultRowHeight/m_defaultColWidth or binary search on array |
b1da8107 SN |
7597 | // of m_rowBottoms/m_ColRights to speed up the search! |
7598 | ||
7599 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, | |
64e15340 JS |
7600 | const wxArrayInt& BorderArray, int nMax, |
7601 | bool maxOnOverflow) | |
2d66e025 | 7602 | { |
b1da8107 SN |
7603 | if (!defaultDist) |
7604 | defaultDist = 1; | |
1af546bf VZ |
7605 | size_t i_max = coord / defaultDist, |
7606 | i_min = 0; | |
d57ad377 | 7607 | |
b1da8107 SN |
7608 | if (BorderArray.IsEmpty()) |
7609 | { | |
d57ad377 | 7610 | if((int) i_max < nMax) |
64e15340 | 7611 | return i_max; |
d57ad377 | 7612 | return maxOnOverflow ? nMax - 1 : -1; |
b1da8107 | 7613 | } |
8f177c8e | 7614 | |
b1da8107 SN |
7615 | if ( i_max >= BorderArray.GetCount()) |
7616 | i_max = BorderArray.GetCount() - 1; | |
70e8d961 | 7617 | else |
f85afd4e | 7618 | { |
b1da8107 SN |
7619 | if ( coord >= BorderArray[i_max]) |
7620 | { | |
7621 | i_min = i_max; | |
7622 | i_max = coord / minDist; | |
7623 | } | |
7624 | if ( i_max >= BorderArray.GetCount()) | |
7625 | i_max = BorderArray.GetCount() - 1; | |
f85afd4e | 7626 | } |
33188aa4 | 7627 | if ( coord >= BorderArray[i_max]) |
1af546bf | 7628 | return maxOnOverflow ? (int)i_max : -1; |
68c5a31c SN |
7629 | if ( coord < BorderArray[0] ) |
7630 | return 0; | |
2d66e025 | 7631 | |
68c5a31c | 7632 | while ( i_max - i_min > 0 ) |
b1da8107 SN |
7633 | { |
7634 | wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max], | |
33188aa4 | 7635 | 0, _T("wxGrid: internal error in CoordToRowOrCol")); |
b1da8107 | 7636 | if (coord >= BorderArray[ i_max - 1]) |
b1da8107 | 7637 | return i_max; |
b1da8107 SN |
7638 | else |
7639 | i_max--; | |
7640 | int median = i_min + (i_max - i_min + 1) / 2; | |
7641 | if (coord < BorderArray[median]) | |
7642 | i_max = median; | |
7643 | else | |
7644 | i_min = median; | |
7645 | } | |
7646 | return i_max; | |
f85afd4e MB |
7647 | } |
7648 | ||
b1da8107 | 7649 | int wxGrid::YToRow( int y ) |
f85afd4e | 7650 | { |
b1da8107 | 7651 | return CoordToRowOrCol(y, m_defaultRowHeight, |
64e15340 | 7652 | WXGRID_MIN_ROW_HEIGHT, m_rowBottoms, m_numRows, FALSE); |
b1da8107 | 7653 | } |
8f177c8e | 7654 | |
f85afd4e | 7655 | |
b1da8107 SN |
7656 | int wxGrid::XToCol( int x ) |
7657 | { | |
7658 | return CoordToRowOrCol(x, m_defaultColWidth, | |
64e15340 | 7659 | WXGRID_MIN_COL_WIDTH, m_colRights, m_numCols, FALSE); |
2d66e025 | 7660 | } |
8f177c8e | 7661 | |
2d66e025 MB |
7662 | |
7663 | // return the row number that that the y coord is near the edge of, or | |
7664 | // -1 if not near an edge | |
7665 | // | |
7666 | int wxGrid::YToEdgeOfRow( int y ) | |
7667 | { | |
33188aa4 SN |
7668 | int i; |
7669 | i = internalYToRow(y); | |
7670 | ||
7671 | if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE ) | |
2d66e025 | 7672 | { |
33188aa4 SN |
7673 | // We know that we are in row i, test whether we are |
7674 | // close enough to lower or upper border, respectively. | |
7675 | if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE ) | |
7676 | return i; | |
7677 | else if( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE ) | |
7678 | return i - 1; | |
f85afd4e | 7679 | } |
2d66e025 MB |
7680 | |
7681 | return -1; | |
7682 | } | |
7683 | ||
7684 | ||
7685 | // return the col number that that the x coord is near the edge of, or | |
7686 | // -1 if not near an edge | |
7687 | // | |
7688 | int wxGrid::XToEdgeOfCol( int x ) | |
7689 | { | |
33188aa4 SN |
7690 | int i; |
7691 | i = internalXToCol(x); | |
7692 | ||
7693 | if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE ) | |
f85afd4e | 7694 | { |
33188aa4 SN |
7695 | // We know that we are in column i, test whether we are |
7696 | // close enough to right or left border, respectively. | |
7697 | if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE ) | |
7698 | return i; | |
7699 | else if( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE ) | |
7700 | return i - 1; | |
f85afd4e | 7701 | } |
2d66e025 MB |
7702 | |
7703 | return -1; | |
f85afd4e MB |
7704 | } |
7705 | ||
2d66e025 MB |
7706 | |
7707 | wxRect wxGrid::CellToRect( int row, int col ) | |
f85afd4e | 7708 | { |
2d66e025 | 7709 | wxRect rect( -1, -1, -1, -1 ); |
f85afd4e | 7710 | |
2d66e025 MB |
7711 | if ( row >= 0 && row < m_numRows && |
7712 | col >= 0 && col < m_numCols ) | |
f85afd4e | 7713 | { |
27f35b66 SN |
7714 | int i, cell_rows, cell_cols; |
7715 | rect.width = rect.height = 0; | |
7716 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
7717 | // if negative then find multicell owner | |
7718 | if (cell_rows < 0) row += cell_rows; | |
7719 | if (cell_cols < 0) col += cell_cols; | |
7720 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
7721 | ||
7c1cb261 VZ |
7722 | rect.x = GetColLeft(col); |
7723 | rect.y = GetRowTop(row); | |
27f35b66 SN |
7724 | for (i=col; i<col+cell_cols; i++) |
7725 | rect.width += GetColWidth(i); | |
7726 | for (i=row; i<row+cell_rows; i++) | |
7727 | rect.height += GetRowHeight(i); | |
f85afd4e MB |
7728 | } |
7729 | ||
f6bcfd97 BP |
7730 | // if grid lines are enabled, then the area of the cell is a bit smaller |
7731 | if (m_gridLinesEnabled) { | |
7732 | rect.width -= 1; | |
7733 | rect.height -= 1; | |
7734 | } | |
2d66e025 MB |
7735 | return rect; |
7736 | } | |
7737 | ||
7738 | ||
7739 | bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) | |
7740 | { | |
7741 | // get the cell rectangle in logical coords | |
7742 | // | |
7743 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 7744 | |
2d66e025 MB |
7745 | // convert to device coords |
7746 | // | |
7747 | int left, top, right, bottom; | |
7748 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
7749 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 7750 | |
2d66e025 MB |
7751 | // check against the client area of the grid window |
7752 | // | |
7753 | int cw, ch; | |
7754 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 7755 | |
2d66e025 | 7756 | if ( wholeCellVisible ) |
f85afd4e | 7757 | { |
2d66e025 | 7758 | // is the cell wholly visible ? |
8f177c8e | 7759 | // |
2d66e025 MB |
7760 | return ( left >= 0 && right <= cw && |
7761 | top >= 0 && bottom <= ch ); | |
7762 | } | |
7763 | else | |
7764 | { | |
7765 | // is the cell partly visible ? | |
7766 | // | |
7767 | return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) && | |
7768 | ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) ); | |
7769 | } | |
7770 | } | |
7771 | ||
7772 | ||
7773 | // make the specified cell location visible by doing a minimal amount | |
7774 | // of scrolling | |
7775 | // | |
7776 | void wxGrid::MakeCellVisible( int row, int col ) | |
7777 | { | |
275c4ae4 | 7778 | |
2d66e025 | 7779 | int i; |
60ff3b99 | 7780 | int xpos = -1, ypos = -1; |
2d66e025 MB |
7781 | |
7782 | if ( row >= 0 && row < m_numRows && | |
7783 | col >= 0 && col < m_numCols ) | |
7784 | { | |
7785 | // get the cell rectangle in logical coords | |
7786 | // | |
7787 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 7788 | |
2d66e025 MB |
7789 | // convert to device coords |
7790 | // | |
7791 | int left, top, right, bottom; | |
7792 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
7793 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 7794 | |
2d66e025 MB |
7795 | int cw, ch; |
7796 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 7797 | |
2d66e025 | 7798 | if ( top < 0 ) |
3f296516 | 7799 | { |
2d66e025 | 7800 | ypos = r.GetTop(); |
3f296516 | 7801 | } |
2d66e025 MB |
7802 | else if ( bottom > ch ) |
7803 | { | |
7804 | int h = r.GetHeight(); | |
7805 | ypos = r.GetTop(); | |
7806 | for ( i = row-1; i >= 0; i-- ) | |
7807 | { | |
7c1cb261 VZ |
7808 | int rowHeight = GetRowHeight(i); |
7809 | if ( h + rowHeight > ch ) | |
7810 | break; | |
2d66e025 | 7811 | |
7c1cb261 VZ |
7812 | h += rowHeight; |
7813 | ypos -= rowHeight; | |
2d66e025 | 7814 | } |
f0102d2a VZ |
7815 | |
7816 | // we divide it later by GRID_SCROLL_LINE, make sure that we don't | |
7817 | // have rounding errors (this is important, because if we do, we | |
7818 | // might not scroll at all and some cells won't be redrawn) | |
275c4ae4 RD |
7819 | // |
7820 | // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full | |
7821 | // scroll unit... | |
97a9929e | 7822 | ypos += GRID_SCROLL_LINE_Y; |
2d66e025 MB |
7823 | } |
7824 | ||
7825 | if ( left < 0 ) | |
7826 | { | |
7827 | xpos = r.GetLeft(); | |
7828 | } | |
7829 | else if ( right > cw ) | |
7830 | { | |
73145b0e JS |
7831 | // position the view so that the cell is on the right |
7832 | int x0, y0; | |
7833 | CalcUnscrolledPosition(0, 0, &x0, &y0); | |
7834 | xpos = x0 + (right - cw); | |
f0102d2a VZ |
7835 | |
7836 | // see comment for ypos above | |
97a9929e | 7837 | xpos += GRID_SCROLL_LINE_X; |
2d66e025 MB |
7838 | } |
7839 | ||
7840 | if ( xpos != -1 || ypos != -1 ) | |
7841 | { | |
97a9929e VZ |
7842 | if ( xpos != -1 ) |
7843 | xpos /= GRID_SCROLL_LINE_X; | |
7844 | if ( ypos != -1 ) | |
7845 | ypos /= GRID_SCROLL_LINE_Y; | |
2d66e025 MB |
7846 | Scroll( xpos, ypos ); |
7847 | AdjustScrollbars(); | |
7848 | } | |
7849 | } | |
7850 | } | |
7851 | ||
7852 | ||
7853 | // | |
7854 | // ------ Grid cursor movement functions | |
7855 | // | |
7856 | ||
5c8fc7c1 | 7857 | bool wxGrid::MoveCursorUp( bool expandSelection ) |
2d66e025 MB |
7858 | { |
7859 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
f6bcfd97 | 7860 | m_currentCellCoords.GetRow() >= 0 ) |
2d66e025 | 7861 | { |
f6bcfd97 | 7862 | if ( expandSelection) |
d95b0c2b SN |
7863 | { |
7864 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
7865 | m_selectingKeyboard = m_currentCellCoords; | |
f6bcfd97 BP |
7866 | if ( m_selectingKeyboard.GetRow() > 0 ) |
7867 | { | |
7868 | m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 ); | |
7869 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
7870 | m_selectingKeyboard.GetCol() ); | |
c9097836 | 7871 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
f6bcfd97 | 7872 | } |
d95b0c2b | 7873 | } |
f6bcfd97 | 7874 | else if ( m_currentCellCoords.GetRow() > 0 ) |
aa5e1f75 SN |
7875 | { |
7876 | ClearSelection(); | |
7877 | MakeCellVisible( m_currentCellCoords.GetRow() - 1, | |
7878 | m_currentCellCoords.GetCol() ); | |
d95b0c2b SN |
7879 | SetCurrentCell( m_currentCellCoords.GetRow() - 1, |
7880 | m_currentCellCoords.GetCol() ); | |
aa5e1f75 | 7881 | } |
f6bcfd97 BP |
7882 | else |
7883 | return FALSE; | |
8f177c8e | 7884 | return TRUE; |
f85afd4e | 7885 | } |
2d66e025 MB |
7886 | |
7887 | return FALSE; | |
7888 | } | |
7889 | ||
7890 | ||
5c8fc7c1 | 7891 | bool wxGrid::MoveCursorDown( bool expandSelection ) |
2d66e025 | 7892 | { |
2d66e025 | 7893 | if ( m_currentCellCoords != wxGridNoCellCoords && |
f6bcfd97 | 7894 | m_currentCellCoords.GetRow() < m_numRows ) |
f85afd4e | 7895 | { |
5c8fc7c1 | 7896 | if ( expandSelection ) |
d95b0c2b SN |
7897 | { |
7898 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
7899 | m_selectingKeyboard = m_currentCellCoords; | |
f6bcfd97 BP |
7900 | if ( m_selectingKeyboard.GetRow() < m_numRows-1 ) |
7901 | { | |
7902 | m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 ); | |
7903 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
7904 | m_selectingKeyboard.GetCol() ); | |
c9097836 | 7905 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
f6bcfd97 | 7906 | } |
d95b0c2b | 7907 | } |
f6bcfd97 | 7908 | else if ( m_currentCellCoords.GetRow() < m_numRows - 1 ) |
aa5e1f75 SN |
7909 | { |
7910 | ClearSelection(); | |
7911 | MakeCellVisible( m_currentCellCoords.GetRow() + 1, | |
7912 | m_currentCellCoords.GetCol() ); | |
d95b0c2b SN |
7913 | SetCurrentCell( m_currentCellCoords.GetRow() + 1, |
7914 | m_currentCellCoords.GetCol() ); | |
aa5e1f75 | 7915 | } |
f6bcfd97 BP |
7916 | else |
7917 | return FALSE; | |
2d66e025 | 7918 | return TRUE; |
f85afd4e | 7919 | } |
2d66e025 MB |
7920 | |
7921 | return FALSE; | |
f85afd4e MB |
7922 | } |
7923 | ||
2d66e025 | 7924 | |
5c8fc7c1 | 7925 | bool wxGrid::MoveCursorLeft( bool expandSelection ) |
f85afd4e | 7926 | { |
2d66e025 | 7927 | if ( m_currentCellCoords != wxGridNoCellCoords && |
f6bcfd97 | 7928 | m_currentCellCoords.GetCol() >= 0 ) |
2d66e025 | 7929 | { |
5c8fc7c1 | 7930 | if ( expandSelection ) |
d95b0c2b SN |
7931 | { |
7932 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
7933 | m_selectingKeyboard = m_currentCellCoords; | |
f6bcfd97 BP |
7934 | if ( m_selectingKeyboard.GetCol() > 0 ) |
7935 | { | |
7936 | m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 ); | |
7937 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
7938 | m_selectingKeyboard.GetCol() ); | |
c9097836 | 7939 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
f6bcfd97 | 7940 | } |
d95b0c2b | 7941 | } |
f6bcfd97 | 7942 | else if ( m_currentCellCoords.GetCol() > 0 ) |
aa5e1f75 SN |
7943 | { |
7944 | ClearSelection(); | |
7945 | MakeCellVisible( m_currentCellCoords.GetRow(), | |
7946 | m_currentCellCoords.GetCol() - 1 ); | |
d95b0c2b SN |
7947 | SetCurrentCell( m_currentCellCoords.GetRow(), |
7948 | m_currentCellCoords.GetCol() - 1 ); | |
aa5e1f75 | 7949 | } |
f6bcfd97 BP |
7950 | else |
7951 | return FALSE; | |
2d66e025 MB |
7952 | return TRUE; |
7953 | } | |
7954 | ||
7955 | return FALSE; | |
7956 | } | |
7957 | ||
7958 | ||
5c8fc7c1 | 7959 | bool wxGrid::MoveCursorRight( bool expandSelection ) |
2d66e025 MB |
7960 | { |
7961 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
f6bcfd97 | 7962 | m_currentCellCoords.GetCol() < m_numCols ) |
f85afd4e | 7963 | { |
5c8fc7c1 | 7964 | if ( expandSelection ) |
d95b0c2b SN |
7965 | { |
7966 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
7967 | m_selectingKeyboard = m_currentCellCoords; | |
f6bcfd97 BP |
7968 | if ( m_selectingKeyboard.GetCol() < m_numCols - 1 ) |
7969 | { | |
7970 | m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 ); | |
7971 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
7972 | m_selectingKeyboard.GetCol() ); | |
c9097836 | 7973 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
f6bcfd97 | 7974 | } |
d95b0c2b | 7975 | } |
f6bcfd97 | 7976 | else if ( m_currentCellCoords.GetCol() < m_numCols - 1 ) |
aa5e1f75 SN |
7977 | { |
7978 | ClearSelection(); | |
7979 | MakeCellVisible( m_currentCellCoords.GetRow(), | |
7980 | m_currentCellCoords.GetCol() + 1 ); | |
7981 | SetCurrentCell( m_currentCellCoords.GetRow(), | |
d95b0c2b | 7982 | m_currentCellCoords.GetCol() + 1 ); |
aa5e1f75 | 7983 | } |
f6bcfd97 BP |
7984 | else |
7985 | return FALSE; | |
2d66e025 | 7986 | return TRUE; |
f85afd4e | 7987 | } |
8f177c8e | 7988 | |
2d66e025 MB |
7989 | return FALSE; |
7990 | } | |
7991 | ||
7992 | ||
7993 | bool wxGrid::MovePageUp() | |
7994 | { | |
7995 | if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE; | |
60ff3b99 | 7996 | |
2d66e025 MB |
7997 | int row = m_currentCellCoords.GetRow(); |
7998 | if ( row > 0 ) | |
f85afd4e | 7999 | { |
2d66e025 MB |
8000 | int cw, ch; |
8001 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 8002 | |
7c1cb261 | 8003 | int y = GetRowTop(row); |
2d66e025 MB |
8004 | int newRow = YToRow( y - ch + 1 ); |
8005 | if ( newRow == -1 ) | |
8006 | { | |
8007 | newRow = 0; | |
8008 | } | |
60ff3b99 | 8009 | else if ( newRow == row ) |
2d66e025 MB |
8010 | { |
8011 | newRow = row - 1; | |
8012 | } | |
8f177c8e | 8013 | |
2d66e025 MB |
8014 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); |
8015 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
60ff3b99 | 8016 | |
f85afd4e MB |
8017 | return TRUE; |
8018 | } | |
2d66e025 MB |
8019 | |
8020 | return FALSE; | |
8021 | } | |
8022 | ||
8023 | bool wxGrid::MovePageDown() | |
8024 | { | |
8025 | if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE; | |
60ff3b99 | 8026 | |
2d66e025 MB |
8027 | int row = m_currentCellCoords.GetRow(); |
8028 | if ( row < m_numRows ) | |
f85afd4e | 8029 | { |
2d66e025 MB |
8030 | int cw, ch; |
8031 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 8032 | |
7c1cb261 | 8033 | int y = GetRowTop(row); |
2d66e025 MB |
8034 | int newRow = YToRow( y + ch ); |
8035 | if ( newRow == -1 ) | |
8036 | { | |
8037 | newRow = m_numRows - 1; | |
8038 | } | |
60ff3b99 | 8039 | else if ( newRow == row ) |
2d66e025 MB |
8040 | { |
8041 | newRow = row + 1; | |
8042 | } | |
8043 | ||
8044 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); | |
8045 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
60ff3b99 | 8046 | |
2d66e025 | 8047 | return TRUE; |
f85afd4e | 8048 | } |
2d66e025 MB |
8049 | |
8050 | return FALSE; | |
f85afd4e | 8051 | } |
8f177c8e | 8052 | |
5c8fc7c1 | 8053 | bool wxGrid::MoveCursorUpBlock( bool expandSelection ) |
2d66e025 MB |
8054 | { |
8055 | if ( m_table && | |
8056 | m_currentCellCoords != wxGridNoCellCoords && | |
8057 | m_currentCellCoords.GetRow() > 0 ) | |
8058 | { | |
8059 | int row = m_currentCellCoords.GetRow(); | |
8060 | int col = m_currentCellCoords.GetCol(); | |
8061 | ||
8062 | if ( m_table->IsEmptyCell(row, col) ) | |
8063 | { | |
8064 | // starting in an empty cell: find the next block of | |
8065 | // non-empty cells | |
8066 | // | |
8067 | while ( row > 0 ) | |
8068 | { | |
8069 | row-- ; | |
8070 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8071 | } | |
8072 | } | |
8073 | else if ( m_table->IsEmptyCell(row-1, col) ) | |
8074 | { | |
8075 | // starting at the top of a block: find the next block | |
8076 | // | |
8077 | row--; | |
8078 | while ( row > 0 ) | |
8079 | { | |
8080 | row-- ; | |
8081 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8082 | } | |
8083 | } | |
8084 | else | |
8085 | { | |
8086 | // starting within a block: find the top of the block | |
8087 | // | |
8088 | while ( row > 0 ) | |
8089 | { | |
8090 | row-- ; | |
8091 | if ( m_table->IsEmptyCell(row, col) ) | |
8092 | { | |
8093 | row++ ; | |
8094 | break; | |
8095 | } | |
8096 | } | |
8097 | } | |
f85afd4e | 8098 | |
2d66e025 | 8099 | MakeCellVisible( row, col ); |
5c8fc7c1 | 8100 | if ( expandSelection ) |
d95b0c2b SN |
8101 | { |
8102 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
c9097836 | 8103 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
d95b0c2b SN |
8104 | } |
8105 | else | |
aa5e1f75 SN |
8106 | { |
8107 | ClearSelection(); | |
8108 | SetCurrentCell( row, col ); | |
8109 | } | |
2d66e025 MB |
8110 | return TRUE; |
8111 | } | |
f85afd4e | 8112 | |
2d66e025 MB |
8113 | return FALSE; |
8114 | } | |
f85afd4e | 8115 | |
5c8fc7c1 | 8116 | bool wxGrid::MoveCursorDownBlock( bool expandSelection ) |
f85afd4e | 8117 | { |
2d66e025 MB |
8118 | if ( m_table && |
8119 | m_currentCellCoords != wxGridNoCellCoords && | |
8120 | m_currentCellCoords.GetRow() < m_numRows-1 ) | |
f85afd4e | 8121 | { |
2d66e025 MB |
8122 | int row = m_currentCellCoords.GetRow(); |
8123 | int col = m_currentCellCoords.GetCol(); | |
8124 | ||
8125 | if ( m_table->IsEmptyCell(row, col) ) | |
8126 | { | |
8127 | // starting in an empty cell: find the next block of | |
8128 | // non-empty cells | |
8129 | // | |
8130 | while ( row < m_numRows-1 ) | |
8131 | { | |
8132 | row++ ; | |
8133 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8134 | } | |
8135 | } | |
8136 | else if ( m_table->IsEmptyCell(row+1, col) ) | |
8137 | { | |
8138 | // starting at the bottom of a block: find the next block | |
8139 | // | |
8140 | row++; | |
8141 | while ( row < m_numRows-1 ) | |
8142 | { | |
8143 | row++ ; | |
8144 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8145 | } | |
8146 | } | |
8147 | else | |
8148 | { | |
8149 | // starting within a block: find the bottom of the block | |
8150 | // | |
8151 | while ( row < m_numRows-1 ) | |
8152 | { | |
8153 | row++ ; | |
8154 | if ( m_table->IsEmptyCell(row, col) ) | |
8155 | { | |
8156 | row-- ; | |
8157 | break; | |
8158 | } | |
8159 | } | |
8160 | } | |
8161 | ||
8162 | MakeCellVisible( row, col ); | |
5c8fc7c1 | 8163 | if ( expandSelection ) |
d95b0c2b SN |
8164 | { |
8165 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
c9097836 | 8166 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
d95b0c2b SN |
8167 | } |
8168 | else | |
aa5e1f75 SN |
8169 | { |
8170 | ClearSelection(); | |
8171 | SetCurrentCell( row, col ); | |
8172 | } | |
2d66e025 MB |
8173 | |
8174 | return TRUE; | |
f85afd4e | 8175 | } |
f85afd4e | 8176 | |
2d66e025 MB |
8177 | return FALSE; |
8178 | } | |
f85afd4e | 8179 | |
5c8fc7c1 | 8180 | bool wxGrid::MoveCursorLeftBlock( bool expandSelection ) |
f85afd4e | 8181 | { |
2d66e025 MB |
8182 | if ( m_table && |
8183 | m_currentCellCoords != wxGridNoCellCoords && | |
8184 | m_currentCellCoords.GetCol() > 0 ) | |
f85afd4e | 8185 | { |
2d66e025 MB |
8186 | int row = m_currentCellCoords.GetRow(); |
8187 | int col = m_currentCellCoords.GetCol(); | |
8188 | ||
8189 | if ( m_table->IsEmptyCell(row, col) ) | |
8190 | { | |
8191 | // starting in an empty cell: find the next block of | |
8192 | // non-empty cells | |
8193 | // | |
8194 | while ( col > 0 ) | |
8195 | { | |
8196 | col-- ; | |
8197 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8198 | } | |
8199 | } | |
8200 | else if ( m_table->IsEmptyCell(row, col-1) ) | |
8201 | { | |
8202 | // starting at the left of a block: find the next block | |
8203 | // | |
8204 | col--; | |
8205 | while ( col > 0 ) | |
8206 | { | |
8207 | col-- ; | |
8208 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8209 | } | |
8210 | } | |
8211 | else | |
8212 | { | |
8213 | // starting within a block: find the left of the block | |
8214 | // | |
8215 | while ( col > 0 ) | |
8216 | { | |
8217 | col-- ; | |
8218 | if ( m_table->IsEmptyCell(row, col) ) | |
8219 | { | |
8220 | col++ ; | |
8221 | break; | |
8222 | } | |
8223 | } | |
8224 | } | |
f85afd4e | 8225 | |
2d66e025 | 8226 | MakeCellVisible( row, col ); |
5c8fc7c1 | 8227 | if ( expandSelection ) |
d95b0c2b SN |
8228 | { |
8229 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
c9097836 | 8230 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
d95b0c2b SN |
8231 | } |
8232 | else | |
aa5e1f75 SN |
8233 | { |
8234 | ClearSelection(); | |
8235 | SetCurrentCell( row, col ); | |
8236 | } | |
8f177c8e | 8237 | |
2d66e025 | 8238 | return TRUE; |
f85afd4e | 8239 | } |
2d66e025 MB |
8240 | |
8241 | return FALSE; | |
f85afd4e MB |
8242 | } |
8243 | ||
5c8fc7c1 | 8244 | bool wxGrid::MoveCursorRightBlock( bool expandSelection ) |
f85afd4e | 8245 | { |
2d66e025 MB |
8246 | if ( m_table && |
8247 | m_currentCellCoords != wxGridNoCellCoords && | |
8248 | m_currentCellCoords.GetCol() < m_numCols-1 ) | |
f85afd4e | 8249 | { |
2d66e025 MB |
8250 | int row = m_currentCellCoords.GetRow(); |
8251 | int col = m_currentCellCoords.GetCol(); | |
8f177c8e | 8252 | |
2d66e025 MB |
8253 | if ( m_table->IsEmptyCell(row, col) ) |
8254 | { | |
8255 | // starting in an empty cell: find the next block of | |
8256 | // non-empty cells | |
8257 | // | |
8258 | while ( col < m_numCols-1 ) | |
8259 | { | |
8260 | col++ ; | |
8261 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8262 | } | |
8263 | } | |
8264 | else if ( m_table->IsEmptyCell(row, col+1) ) | |
8265 | { | |
8266 | // starting at the right of a block: find the next block | |
8267 | // | |
8268 | col++; | |
8269 | while ( col < m_numCols-1 ) | |
8270 | { | |
8271 | col++ ; | |
8272 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
8273 | } | |
8274 | } | |
8275 | else | |
8276 | { | |
8277 | // starting within a block: find the right of the block | |
8278 | // | |
8279 | while ( col < m_numCols-1 ) | |
8280 | { | |
8281 | col++ ; | |
8282 | if ( m_table->IsEmptyCell(row, col) ) | |
8283 | { | |
8284 | col-- ; | |
8285 | break; | |
8286 | } | |
8287 | } | |
8288 | } | |
8f177c8e | 8289 | |
2d66e025 | 8290 | MakeCellVisible( row, col ); |
5c8fc7c1 | 8291 | if ( expandSelection ) |
d95b0c2b SN |
8292 | { |
8293 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
c9097836 | 8294 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); |
d95b0c2b SN |
8295 | } |
8296 | else | |
aa5e1f75 SN |
8297 | { |
8298 | ClearSelection(); | |
8299 | SetCurrentCell( row, col ); | |
8300 | } | |
f85afd4e | 8301 | |
2d66e025 | 8302 | return TRUE; |
f85afd4e | 8303 | } |
2d66e025 MB |
8304 | |
8305 | return FALSE; | |
f85afd4e MB |
8306 | } |
8307 | ||
8308 | ||
2d66e025 | 8309 | |
f85afd4e | 8310 | // |
2d66e025 | 8311 | // ------ Label values and formatting |
f85afd4e MB |
8312 | // |
8313 | ||
8314 | void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) | |
8315 | { | |
8316 | *horiz = m_rowLabelHorizAlign; | |
8317 | *vert = m_rowLabelVertAlign; | |
8318 | } | |
8319 | ||
8320 | void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) | |
8321 | { | |
8322 | *horiz = m_colLabelHorizAlign; | |
8323 | *vert = m_colLabelVertAlign; | |
8324 | } | |
8325 | ||
d43851f7 JS |
8326 | int wxGrid::GetColLabelTextOrientation() |
8327 | { | |
8328 | return m_colLabelTextOrientation; | |
8329 | } | |
8330 | ||
f85afd4e MB |
8331 | wxString wxGrid::GetRowLabelValue( int row ) |
8332 | { | |
8333 | if ( m_table ) | |
8334 | { | |
8335 | return m_table->GetRowLabelValue( row ); | |
8336 | } | |
8337 | else | |
8338 | { | |
8339 | wxString s; | |
8340 | s << row; | |
8341 | return s; | |
8342 | } | |
8343 | } | |
8344 | ||
8345 | wxString wxGrid::GetColLabelValue( int col ) | |
8346 | { | |
8347 | if ( m_table ) | |
8348 | { | |
8349 | return m_table->GetColLabelValue( col ); | |
8350 | } | |
8351 | else | |
8352 | { | |
8353 | wxString s; | |
8354 | s << col; | |
8355 | return s; | |
8356 | } | |
8357 | } | |
8358 | ||
2e8cd977 | 8359 | |
f85afd4e MB |
8360 | void wxGrid::SetRowLabelSize( int width ) |
8361 | { | |
2e8cd977 MB |
8362 | width = wxMax( width, 0 ); |
8363 | if ( width != m_rowLabelWidth ) | |
8364 | { | |
2e8cd977 MB |
8365 | if ( width == 0 ) |
8366 | { | |
8367 | m_rowLabelWin->Show( FALSE ); | |
7807d81c | 8368 | m_cornerLabelWin->Show( FALSE ); |
2e8cd977 | 8369 | } |
7807d81c | 8370 | else if ( m_rowLabelWidth == 0 ) |
2e8cd977 | 8371 | { |
7807d81c MB |
8372 | m_rowLabelWin->Show( TRUE ); |
8373 | if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE ); | |
2e8cd977 | 8374 | } |
b99be8fb | 8375 | |
2e8cd977 | 8376 | m_rowLabelWidth = width; |
7807d81c | 8377 | CalcWindowSizes(); |
27f35b66 | 8378 | wxScrolledWindow::Refresh( TRUE ); |
2e8cd977 | 8379 | } |
f85afd4e MB |
8380 | } |
8381 | ||
2e8cd977 | 8382 | |
f85afd4e MB |
8383 | void wxGrid::SetColLabelSize( int height ) |
8384 | { | |
7807d81c | 8385 | height = wxMax( height, 0 ); |
2e8cd977 MB |
8386 | if ( height != m_colLabelHeight ) |
8387 | { | |
2e8cd977 MB |
8388 | if ( height == 0 ) |
8389 | { | |
2e8cd977 | 8390 | m_colLabelWin->Show( FALSE ); |
7807d81c | 8391 | m_cornerLabelWin->Show( FALSE ); |
2e8cd977 | 8392 | } |
7807d81c | 8393 | else if ( m_colLabelHeight == 0 ) |
2e8cd977 | 8394 | { |
7807d81c MB |
8395 | m_colLabelWin->Show( TRUE ); |
8396 | if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE ); | |
2e8cd977 | 8397 | } |
7807d81c | 8398 | |
2e8cd977 | 8399 | m_colLabelHeight = height; |
7807d81c | 8400 | CalcWindowSizes(); |
27f35b66 | 8401 | wxScrolledWindow::Refresh( TRUE ); |
2e8cd977 | 8402 | } |
f85afd4e MB |
8403 | } |
8404 | ||
2e8cd977 | 8405 | |
f85afd4e MB |
8406 | void wxGrid::SetLabelBackgroundColour( const wxColour& colour ) |
8407 | { | |
2d66e025 MB |
8408 | if ( m_labelBackgroundColour != colour ) |
8409 | { | |
8410 | m_labelBackgroundColour = colour; | |
8411 | m_rowLabelWin->SetBackgroundColour( colour ); | |
8412 | m_colLabelWin->SetBackgroundColour( colour ); | |
8413 | m_cornerLabelWin->SetBackgroundColour( colour ); | |
8414 | ||
8415 | if ( !GetBatchCount() ) | |
8416 | { | |
8417 | m_rowLabelWin->Refresh(); | |
8418 | m_colLabelWin->Refresh(); | |
8419 | m_cornerLabelWin->Refresh(); | |
8420 | } | |
8421 | } | |
f85afd4e MB |
8422 | } |
8423 | ||
8424 | void wxGrid::SetLabelTextColour( const wxColour& colour ) | |
8425 | { | |
2d66e025 MB |
8426 | if ( m_labelTextColour != colour ) |
8427 | { | |
8428 | m_labelTextColour = colour; | |
8429 | if ( !GetBatchCount() ) | |
8430 | { | |
8431 | m_rowLabelWin->Refresh(); | |
8432 | m_colLabelWin->Refresh(); | |
8433 | } | |
8434 | } | |
f85afd4e MB |
8435 | } |
8436 | ||
8437 | void wxGrid::SetLabelFont( const wxFont& font ) | |
8438 | { | |
8439 | m_labelFont = font; | |
2d66e025 MB |
8440 | if ( !GetBatchCount() ) |
8441 | { | |
8442 | m_rowLabelWin->Refresh(); | |
8443 | m_colLabelWin->Refresh(); | |
8444 | } | |
f85afd4e MB |
8445 | } |
8446 | ||
8447 | void wxGrid::SetRowLabelAlignment( int horiz, int vert ) | |
8448 | { | |
4c7277db MB |
8449 | // allow old (incorrect) defs to be used |
8450 | switch ( horiz ) | |
8451 | { | |
8452 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
8453 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
8454 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
8455 | } | |
84912ef8 | 8456 | |
4c7277db MB |
8457 | switch ( vert ) |
8458 | { | |
8459 | case wxTOP: vert = wxALIGN_TOP; break; | |
8460 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
8461 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
8462 | } | |
84912ef8 | 8463 | |
4c7277db | 8464 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) |
f85afd4e MB |
8465 | { |
8466 | m_rowLabelHorizAlign = horiz; | |
8467 | } | |
8f177c8e | 8468 | |
4c7277db | 8469 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) |
f85afd4e MB |
8470 | { |
8471 | m_rowLabelVertAlign = vert; | |
8472 | } | |
8473 | ||
2d66e025 MB |
8474 | if ( !GetBatchCount() ) |
8475 | { | |
8476 | m_rowLabelWin->Refresh(); | |
60ff3b99 | 8477 | } |
f85afd4e MB |
8478 | } |
8479 | ||
8480 | void wxGrid::SetColLabelAlignment( int horiz, int vert ) | |
8481 | { | |
4c7277db MB |
8482 | // allow old (incorrect) defs to be used |
8483 | switch ( horiz ) | |
8484 | { | |
8485 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
8486 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
8487 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
8488 | } | |
84912ef8 | 8489 | |
4c7277db MB |
8490 | switch ( vert ) |
8491 | { | |
8492 | case wxTOP: vert = wxALIGN_TOP; break; | |
8493 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
8494 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
8495 | } | |
84912ef8 | 8496 | |
4c7277db | 8497 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) |
f85afd4e MB |
8498 | { |
8499 | m_colLabelHorizAlign = horiz; | |
8500 | } | |
8f177c8e | 8501 | |
4c7277db | 8502 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) |
f85afd4e MB |
8503 | { |
8504 | m_colLabelVertAlign = vert; | |
8505 | } | |
8506 | ||
2d66e025 MB |
8507 | if ( !GetBatchCount() ) |
8508 | { | |
2d66e025 | 8509 | m_colLabelWin->Refresh(); |
60ff3b99 | 8510 | } |
f85afd4e MB |
8511 | } |
8512 | ||
d43851f7 JS |
8513 | // Note: under MSW, the default column label font must be changed because it |
8514 | // does not support vertical printing | |
8515 | // | |
8516 | // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD); | |
8517 | // pGrid->SetLabelFont(font); | |
8518 | // pGrid->SetColLabelTextOrientation(wxVERTICAL); | |
8519 | // | |
8520 | void wxGrid::SetColLabelTextOrientation( int textOrientation ) | |
8521 | { | |
8522 | if( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL ) | |
8523 | { | |
8524 | m_colLabelTextOrientation = textOrientation; | |
8525 | } | |
8526 | ||
8527 | if ( !GetBatchCount() ) | |
8528 | { | |
8529 | m_colLabelWin->Refresh(); | |
8530 | } | |
8531 | } | |
8532 | ||
f85afd4e MB |
8533 | void wxGrid::SetRowLabelValue( int row, const wxString& s ) |
8534 | { | |
8535 | if ( m_table ) | |
8536 | { | |
8537 | m_table->SetRowLabelValue( row, s ); | |
2d66e025 MB |
8538 | if ( !GetBatchCount() ) |
8539 | { | |
70c7a608 SN |
8540 | wxRect rect = CellToRect( row, 0); |
8541 | if ( rect.height > 0 ) | |
8542 | { | |
cb309039 | 8543 | CalcScrolledPosition(0, rect.y, &rect.x, &rect.y); |
b1944ebc | 8544 | rect.x = 0; |
70c7a608 SN |
8545 | rect.width = m_rowLabelWidth; |
8546 | m_rowLabelWin->Refresh( TRUE, &rect ); | |
8547 | } | |
2d66e025 | 8548 | } |
f85afd4e MB |
8549 | } |
8550 | } | |
8551 | ||
8552 | void wxGrid::SetColLabelValue( int col, const wxString& s ) | |
8553 | { | |
8554 | if ( m_table ) | |
8555 | { | |
8556 | m_table->SetColLabelValue( col, s ); | |
2d66e025 MB |
8557 | if ( !GetBatchCount() ) |
8558 | { | |
70c7a608 SN |
8559 | wxRect rect = CellToRect( 0, col ); |
8560 | if ( rect.width > 0 ) | |
8561 | { | |
cb309039 | 8562 | CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y); |
b1944ebc | 8563 | rect.y = 0; |
70c7a608 SN |
8564 | rect.height = m_colLabelHeight; |
8565 | m_colLabelWin->Refresh( TRUE, &rect ); | |
8566 | } | |
2d66e025 | 8567 | } |
f85afd4e MB |
8568 | } |
8569 | } | |
8570 | ||
8571 | void wxGrid::SetGridLineColour( const wxColour& colour ) | |
8572 | { | |
2d66e025 MB |
8573 | if ( m_gridLineColour != colour ) |
8574 | { | |
8575 | m_gridLineColour = colour; | |
60ff3b99 | 8576 | |
2d66e025 MB |
8577 | wxClientDC dc( m_gridWin ); |
8578 | PrepareDC( dc ); | |
c6a51dcd | 8579 | DrawAllGridLines( dc, wxRegion() ); |
2d66e025 | 8580 | } |
f85afd4e MB |
8581 | } |
8582 | ||
f6bcfd97 BP |
8583 | |
8584 | void wxGrid::SetCellHighlightColour( const wxColour& colour ) | |
8585 | { | |
8586 | if ( m_cellHighlightColour != colour ) | |
8587 | { | |
8588 | m_cellHighlightColour = colour; | |
8589 | ||
8590 | wxClientDC dc( m_gridWin ); | |
8591 | PrepareDC( dc ); | |
8592 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
8593 | DrawCellHighlight(dc, attr); | |
8594 | attr->DecRef(); | |
8595 | } | |
8596 | } | |
8597 | ||
d2520c85 RD |
8598 | void wxGrid::SetCellHighlightPenWidth(int width) |
8599 | { | |
8600 | if (m_cellHighlightPenWidth != width) { | |
8601 | m_cellHighlightPenWidth = width; | |
8602 | ||
8603 | // Just redrawing the cell highlight is not enough since that won't | |
8604 | // make any visible change if the the thickness is getting smaller. | |
8605 | int row = m_currentCellCoords.GetRow(); | |
8606 | int col = m_currentCellCoords.GetCol(); | |
8607 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
8608 | return; | |
8609 | wxRect rect = CellToRect(row, col); | |
8610 | m_gridWin->Refresh(TRUE, &rect); | |
8611 | } | |
8612 | } | |
8613 | ||
8614 | void wxGrid::SetCellHighlightROPenWidth(int width) | |
8615 | { | |
8616 | if (m_cellHighlightROPenWidth != width) { | |
8617 | m_cellHighlightROPenWidth = width; | |
8618 | ||
8619 | // Just redrawing the cell highlight is not enough since that won't | |
8620 | // make any visible change if the the thickness is getting smaller. | |
8621 | int row = m_currentCellCoords.GetRow(); | |
8622 | int col = m_currentCellCoords.GetCol(); | |
8623 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
8624 | return; | |
8625 | wxRect rect = CellToRect(row, col); | |
8626 | m_gridWin->Refresh(TRUE, &rect); | |
8627 | } | |
8628 | } | |
8629 | ||
f85afd4e MB |
8630 | void wxGrid::EnableGridLines( bool enable ) |
8631 | { | |
8632 | if ( enable != m_gridLinesEnabled ) | |
8633 | { | |
8634 | m_gridLinesEnabled = enable; | |
2d66e025 MB |
8635 | |
8636 | if ( !GetBatchCount() ) | |
8637 | { | |
8638 | if ( enable ) | |
8639 | { | |
8640 | wxClientDC dc( m_gridWin ); | |
8641 | PrepareDC( dc ); | |
c6a51dcd | 8642 | DrawAllGridLines( dc, wxRegion() ); |
2d66e025 MB |
8643 | } |
8644 | else | |
8645 | { | |
8646 | m_gridWin->Refresh(); | |
8647 | } | |
8648 | } | |
f85afd4e MB |
8649 | } |
8650 | } | |
8651 | ||
8652 | ||
8653 | int wxGrid::GetDefaultRowSize() | |
8654 | { | |
8655 | return m_defaultRowHeight; | |
8656 | } | |
8657 | ||
8658 | int wxGrid::GetRowSize( int row ) | |
8659 | { | |
b99be8fb VZ |
8660 | wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") ); |
8661 | ||
7c1cb261 | 8662 | return GetRowHeight(row); |
f85afd4e MB |
8663 | } |
8664 | ||
8665 | int wxGrid::GetDefaultColSize() | |
8666 | { | |
8667 | return m_defaultColWidth; | |
8668 | } | |
8669 | ||
8670 | int wxGrid::GetColSize( int col ) | |
8671 | { | |
b99be8fb VZ |
8672 | wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") ); |
8673 | ||
7c1cb261 | 8674 | return GetColWidth(col); |
f85afd4e MB |
8675 | } |
8676 | ||
2e9a6788 VZ |
8677 | // ============================================================================ |
8678 | // access to the grid attributes: each of them has a default value in the grid | |
8679 | // itself and may be overidden on a per-cell basis | |
8680 | // ============================================================================ | |
8681 | ||
8682 | // ---------------------------------------------------------------------------- | |
8683 | // setting default attributes | |
8684 | // ---------------------------------------------------------------------------- | |
8685 | ||
8686 | void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col ) | |
8687 | { | |
2796cce3 | 8688 | m_defaultCellAttr->SetBackgroundColour(col); |
c916e13b RR |
8689 | #ifdef __WXGTK__ |
8690 | m_gridWin->SetBackgroundColour(col); | |
8691 | #endif | |
2e9a6788 VZ |
8692 | } |
8693 | ||
8694 | void wxGrid::SetDefaultCellTextColour( const wxColour& col ) | |
8695 | { | |
2796cce3 | 8696 | m_defaultCellAttr->SetTextColour(col); |
2e9a6788 VZ |
8697 | } |
8698 | ||
8699 | void wxGrid::SetDefaultCellAlignment( int horiz, int vert ) | |
8700 | { | |
2796cce3 | 8701 | m_defaultCellAttr->SetAlignment(horiz, vert); |
2e9a6788 VZ |
8702 | } |
8703 | ||
27f35b66 SN |
8704 | void wxGrid::SetDefaultCellOverflow( bool allow ) |
8705 | { | |
8706 | m_defaultCellAttr->SetOverflow(allow); | |
8707 | } | |
8708 | ||
2e9a6788 VZ |
8709 | void wxGrid::SetDefaultCellFont( const wxFont& font ) |
8710 | { | |
2796cce3 RD |
8711 | m_defaultCellAttr->SetFont(font); |
8712 | } | |
8713 | ||
0ba143c9 RD |
8714 | void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer) |
8715 | { | |
8716 | m_defaultCellAttr->SetRenderer(renderer); | |
8717 | } | |
2e9a6788 | 8718 | |
0ba143c9 RD |
8719 | void wxGrid::SetDefaultEditor(wxGridCellEditor *editor) |
8720 | { | |
8721 | m_defaultCellAttr->SetEditor(editor); | |
8722 | } | |
9b4aede2 | 8723 | |
2e9a6788 VZ |
8724 | // ---------------------------------------------------------------------------- |
8725 | // access to the default attrbiutes | |
8726 | // ---------------------------------------------------------------------------- | |
8727 | ||
f85afd4e MB |
8728 | wxColour wxGrid::GetDefaultCellBackgroundColour() |
8729 | { | |
2796cce3 | 8730 | return m_defaultCellAttr->GetBackgroundColour(); |
f85afd4e MB |
8731 | } |
8732 | ||
2e9a6788 VZ |
8733 | wxColour wxGrid::GetDefaultCellTextColour() |
8734 | { | |
2796cce3 | 8735 | return m_defaultCellAttr->GetTextColour(); |
2e9a6788 VZ |
8736 | } |
8737 | ||
8738 | wxFont wxGrid::GetDefaultCellFont() | |
8739 | { | |
2796cce3 | 8740 | return m_defaultCellAttr->GetFont(); |
2e9a6788 VZ |
8741 | } |
8742 | ||
8743 | void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) | |
8744 | { | |
2796cce3 | 8745 | m_defaultCellAttr->GetAlignment(horiz, vert); |
2e9a6788 VZ |
8746 | } |
8747 | ||
27f35b66 SN |
8748 | bool wxGrid::GetDefaultCellOverflow() |
8749 | { | |
8750 | return m_defaultCellAttr->GetOverflow(); | |
8751 | } | |
8752 | ||
0ba143c9 RD |
8753 | wxGridCellRenderer *wxGrid::GetDefaultRenderer() const |
8754 | { | |
0b190b0f | 8755 | return m_defaultCellAttr->GetRenderer(NULL, 0, 0); |
0ba143c9 | 8756 | } |
ab79958a | 8757 | |
0ba143c9 RD |
8758 | wxGridCellEditor *wxGrid::GetDefaultEditor() const |
8759 | { | |
28a77bc4 | 8760 | return m_defaultCellAttr->GetEditor(NULL,0,0); |
0ba143c9 | 8761 | } |
9b4aede2 | 8762 | |
2e9a6788 VZ |
8763 | // ---------------------------------------------------------------------------- |
8764 | // access to cell attributes | |
8765 | // ---------------------------------------------------------------------------- | |
8766 | ||
b99be8fb | 8767 | wxColour wxGrid::GetCellBackgroundColour(int row, int col) |
f85afd4e | 8768 | { |
0a976765 | 8769 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 8770 | wxColour colour = attr->GetBackgroundColour(); |
39bcce60 | 8771 | attr->DecRef(); |
b99be8fb | 8772 | return colour; |
f85afd4e MB |
8773 | } |
8774 | ||
b99be8fb | 8775 | wxColour wxGrid::GetCellTextColour( int row, int col ) |
f85afd4e | 8776 | { |
0a976765 | 8777 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 8778 | wxColour colour = attr->GetTextColour(); |
39bcce60 | 8779 | attr->DecRef(); |
b99be8fb | 8780 | return colour; |
f85afd4e MB |
8781 | } |
8782 | ||
b99be8fb | 8783 | wxFont wxGrid::GetCellFont( int row, int col ) |
f85afd4e | 8784 | { |
0a976765 | 8785 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 8786 | wxFont font = attr->GetFont(); |
39bcce60 | 8787 | attr->DecRef(); |
b99be8fb | 8788 | return font; |
f85afd4e MB |
8789 | } |
8790 | ||
b99be8fb | 8791 | void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) |
f85afd4e | 8792 | { |
0a976765 | 8793 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 8794 | attr->GetAlignment(horiz, vert); |
39bcce60 | 8795 | attr->DecRef(); |
2e9a6788 VZ |
8796 | } |
8797 | ||
27f35b66 SN |
8798 | bool wxGrid::GetCellOverflow( int row, int col ) |
8799 | { | |
8800 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
8801 | bool allow = attr->GetOverflow(); | |
8802 | attr->DecRef(); | |
8803 | return allow; | |
8804 | } | |
8805 | ||
8806 | void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) | |
8807 | { | |
8808 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
8809 | attr->GetSize( num_rows, num_cols ); | |
8810 | attr->DecRef(); | |
8811 | } | |
8812 | ||
2796cce3 RD |
8813 | wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) |
8814 | { | |
8815 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 8816 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); |
2796cce3 | 8817 | attr->DecRef(); |
0b190b0f | 8818 | |
2796cce3 RD |
8819 | return renderer; |
8820 | } | |
8821 | ||
9b4aede2 RD |
8822 | wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) |
8823 | { | |
8824 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 8825 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
9b4aede2 | 8826 | attr->DecRef(); |
0b190b0f | 8827 | |
9b4aede2 RD |
8828 | return editor; |
8829 | } | |
8830 | ||
283b7808 VZ |
8831 | bool wxGrid::IsReadOnly(int row, int col) const |
8832 | { | |
8833 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
8834 | bool isReadOnly = attr->IsReadOnly(); | |
8835 | attr->DecRef(); | |
8836 | return isReadOnly; | |
8837 | } | |
8838 | ||
2e9a6788 | 8839 | // ---------------------------------------------------------------------------- |
758cbedf | 8840 | // attribute support: cache, automatic provider creation, ... |
2e9a6788 VZ |
8841 | // ---------------------------------------------------------------------------- |
8842 | ||
8843 | bool wxGrid::CanHaveAttributes() | |
8844 | { | |
8845 | if ( !m_table ) | |
8846 | { | |
8847 | return FALSE; | |
8848 | } | |
8849 | ||
f2d76237 | 8850 | return m_table->CanHaveAttributes(); |
2e9a6788 VZ |
8851 | } |
8852 | ||
0a976765 VZ |
8853 | void wxGrid::ClearAttrCache() |
8854 | { | |
8855 | if ( m_attrCache.row != -1 ) | |
8856 | { | |
39bcce60 | 8857 | wxSafeDecRef(m_attrCache.attr); |
19d7140e | 8858 | m_attrCache.attr = NULL; |
0a976765 VZ |
8859 | m_attrCache.row = -1; |
8860 | } | |
8861 | } | |
8862 | ||
8863 | void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const | |
8864 | { | |
2b5f62a0 VZ |
8865 | if ( attr != NULL ) |
8866 | { | |
8867 | wxGrid *self = (wxGrid *)this; // const_cast | |
0a976765 | 8868 | |
2b5f62a0 VZ |
8869 | self->ClearAttrCache(); |
8870 | self->m_attrCache.row = row; | |
8871 | self->m_attrCache.col = col; | |
8872 | self->m_attrCache.attr = attr; | |
8873 | wxSafeIncRef(attr); | |
8874 | } | |
0a976765 VZ |
8875 | } |
8876 | ||
8877 | bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const | |
8878 | { | |
8879 | if ( row == m_attrCache.row && col == m_attrCache.col ) | |
8880 | { | |
8881 | *attr = m_attrCache.attr; | |
39bcce60 | 8882 | wxSafeIncRef(m_attrCache.attr); |
0a976765 VZ |
8883 | |
8884 | #ifdef DEBUG_ATTR_CACHE | |
8885 | gs_nAttrCacheHits++; | |
8886 | #endif | |
8887 | ||
8888 | return TRUE; | |
8889 | } | |
8890 | else | |
8891 | { | |
8892 | #ifdef DEBUG_ATTR_CACHE | |
8893 | gs_nAttrCacheMisses++; | |
8894 | #endif | |
8895 | return FALSE; | |
8896 | } | |
8897 | } | |
8898 | ||
2e9a6788 VZ |
8899 | wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const |
8900 | { | |
a373d23b SN |
8901 | wxGridCellAttr *attr = NULL; |
8902 | // Additional test to avoid looking at the cache e.g. for | |
8903 | // wxNoCellCoords, as this will confuse memory management. | |
8904 | if ( row >= 0 ) | |
8905 | { | |
3ed884a0 SN |
8906 | if ( !LookupAttr(row, col, &attr) ) |
8907 | { | |
8908 | attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any) | |
8909 | : (wxGridCellAttr *)NULL; | |
8910 | CacheAttr(row, col, attr); | |
8911 | } | |
0a976765 | 8912 | } |
508011ce VZ |
8913 | if (attr) |
8914 | { | |
2796cce3 | 8915 | attr->SetDefAttr(m_defaultCellAttr); |
508011ce VZ |
8916 | } |
8917 | else | |
8918 | { | |
2796cce3 RD |
8919 | attr = m_defaultCellAttr; |
8920 | attr->IncRef(); | |
8921 | } | |
2e9a6788 | 8922 | |
0a976765 VZ |
8923 | return attr; |
8924 | } | |
8925 | ||
8926 | wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const | |
8927 | { | |
19d7140e | 8928 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
0a976765 | 8929 | |
1df4050d VZ |
8930 | wxCHECK_MSG( m_table, attr, |
8931 | _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") ); | |
8932 | ||
8933 | attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell); | |
8934 | if ( !attr ) | |
8935 | { | |
8936 | attr = new wxGridCellAttr(m_defaultCellAttr); | |
8937 | ||
8938 | // artificially inc the ref count to match DecRef() in caller | |
8939 | attr->IncRef(); | |
8940 | m_table->SetAttr(attr, row, col); | |
8941 | } | |
0a976765 | 8942 | |
2e9a6788 VZ |
8943 | return attr; |
8944 | } | |
8945 | ||
0b190b0f VZ |
8946 | // ---------------------------------------------------------------------------- |
8947 | // setting column attributes (wrappers around SetColAttr) | |
8948 | // ---------------------------------------------------------------------------- | |
8949 | ||
8950 | void wxGrid::SetColFormatBool(int col) | |
8951 | { | |
8952 | SetColFormatCustom(col, wxGRID_VALUE_BOOL); | |
8953 | } | |
8954 | ||
8955 | void wxGrid::SetColFormatNumber(int col) | |
8956 | { | |
8957 | SetColFormatCustom(col, wxGRID_VALUE_NUMBER); | |
8958 | } | |
8959 | ||
8960 | void wxGrid::SetColFormatFloat(int col, int width, int precision) | |
8961 | { | |
8962 | wxString typeName = wxGRID_VALUE_FLOAT; | |
8963 | if ( (width != -1) || (precision != -1) ) | |
8964 | { | |
8965 | typeName << _T(':') << width << _T(',') << precision; | |
8966 | } | |
8967 | ||
8968 | SetColFormatCustom(col, typeName); | |
8969 | } | |
8970 | ||
8971 | void wxGrid::SetColFormatCustom(int col, const wxString& typeName) | |
8972 | { | |
19d7140e VZ |
8973 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
8974 | ||
8975 | attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col ); | |
8976 | if(!attr) | |
8977 | attr = new wxGridCellAttr; | |
0b190b0f VZ |
8978 | wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName); |
8979 | attr->SetRenderer(renderer); | |
8980 | ||
8981 | SetColAttr(col, attr); | |
19d7140e | 8982 | |
0b190b0f VZ |
8983 | } |
8984 | ||
758cbedf VZ |
8985 | // ---------------------------------------------------------------------------- |
8986 | // setting cell attributes: this is forwarded to the table | |
8987 | // ---------------------------------------------------------------------------- | |
8988 | ||
27f35b66 SN |
8989 | void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr) |
8990 | { | |
8991 | if ( CanHaveAttributes() ) | |
8992 | { | |
8993 | m_table->SetAttr(attr, row, col); | |
8994 | ClearAttrCache(); | |
8995 | } | |
8996 | else | |
8997 | { | |
8998 | wxSafeDecRef(attr); | |
8999 | } | |
9000 | } | |
9001 | ||
758cbedf VZ |
9002 | void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr) |
9003 | { | |
9004 | if ( CanHaveAttributes() ) | |
9005 | { | |
9006 | m_table->SetRowAttr(attr, row); | |
19d7140e | 9007 | ClearAttrCache(); |
758cbedf VZ |
9008 | } |
9009 | else | |
9010 | { | |
39bcce60 | 9011 | wxSafeDecRef(attr); |
758cbedf VZ |
9012 | } |
9013 | } | |
9014 | ||
9015 | void wxGrid::SetColAttr(int col, wxGridCellAttr *attr) | |
9016 | { | |
9017 | if ( CanHaveAttributes() ) | |
9018 | { | |
9019 | m_table->SetColAttr(attr, col); | |
19d7140e | 9020 | ClearAttrCache(); |
758cbedf VZ |
9021 | } |
9022 | else | |
9023 | { | |
39bcce60 | 9024 | wxSafeDecRef(attr); |
758cbedf VZ |
9025 | } |
9026 | } | |
9027 | ||
2e9a6788 VZ |
9028 | void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour ) |
9029 | { | |
9030 | if ( CanHaveAttributes() ) | |
9031 | { | |
0a976765 | 9032 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
9033 | attr->SetBackgroundColour(colour); |
9034 | attr->DecRef(); | |
9035 | } | |
9036 | } | |
9037 | ||
9038 | void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour ) | |
9039 | { | |
9040 | if ( CanHaveAttributes() ) | |
9041 | { | |
0a976765 | 9042 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
9043 | attr->SetTextColour(colour); |
9044 | attr->DecRef(); | |
9045 | } | |
9046 | } | |
9047 | ||
9048 | void wxGrid::SetCellFont( int row, int col, const wxFont& font ) | |
9049 | { | |
9050 | if ( CanHaveAttributes() ) | |
9051 | { | |
0a976765 | 9052 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
9053 | attr->SetFont(font); |
9054 | attr->DecRef(); | |
9055 | } | |
9056 | } | |
9057 | ||
9058 | void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert ) | |
9059 | { | |
9060 | if ( CanHaveAttributes() ) | |
9061 | { | |
0a976765 | 9062 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
9063 | attr->SetAlignment(horiz, vert); |
9064 | attr->DecRef(); | |
ab79958a VZ |
9065 | } |
9066 | } | |
9067 | ||
27f35b66 SN |
9068 | void wxGrid::SetCellOverflow( int row, int col, bool allow ) |
9069 | { | |
9070 | if ( CanHaveAttributes() ) | |
9071 | { | |
9072 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9073 | attr->SetOverflow(allow); | |
9074 | attr->DecRef(); | |
9075 | } | |
9076 | } | |
9077 | ||
9078 | void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols ) | |
9079 | { | |
9080 | if ( CanHaveAttributes() ) | |
9081 | { | |
9082 | int cell_rows, cell_cols; | |
9083 | ||
9084 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9085 | attr->GetSize(&cell_rows, &cell_cols); | |
9086 | attr->SetSize(num_rows, num_cols); | |
9087 | attr->DecRef(); | |
9088 | ||
9089 | // Cannot set the size of a cell to 0 or negative values | |
9090 | // While it is perfectly legal to do that, this function cannot | |
9091 | // handle all the possibilies, do it by hand by getting the CellAttr. | |
9092 | // You can only set the size of a cell to 1,1 or greater with this fn | |
9093 | wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)), | |
9094 | wxT("wxGrid::SetCellSize setting cell size that is already part of another cell")); | |
9095 | wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)), | |
9096 | wxT("wxGrid::SetCellSize setting cell size to < 1")); | |
9097 | ||
9098 | // if this was already a multicell then "turn off" the other cells first | |
9099 | if ((cell_rows > 1) || (cell_rows > 1)) | |
9100 | { | |
9101 | int i, j; | |
9102 | for (j=row; j<row+cell_rows; j++) | |
9103 | { | |
9104 | for (i=col; i<col+cell_cols; i++) | |
9105 | { | |
9106 | if ((i != col) || (j != row)) | |
9107 | { | |
9108 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
9109 | attr_stub->SetSize( 1, 1 ); | |
9110 | attr_stub->DecRef(); | |
9111 | } | |
9112 | } | |
9113 | } | |
9114 | } | |
9115 | ||
9116 | // mark the cells that will be covered by this cell to | |
9117 | // negative or zero values to point back at this cell | |
9118 | if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1)) | |
9119 | { | |
9120 | int i, j; | |
9121 | for (j=row; j<row+num_rows; j++) | |
9122 | { | |
9123 | for (i=col; i<col+num_cols; i++) | |
9124 | { | |
9125 | if ((i != col) || (j != row)) | |
9126 | { | |
9127 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
9128 | attr_stub->SetSize( row-j, col-i ); | |
9129 | attr_stub->DecRef(); | |
9130 | } | |
9131 | } | |
9132 | } | |
9133 | } | |
9134 | } | |
9135 | } | |
9136 | ||
ab79958a VZ |
9137 | void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer) |
9138 | { | |
9139 | if ( CanHaveAttributes() ) | |
9140 | { | |
0a976765 | 9141 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
ab79958a VZ |
9142 | attr->SetRenderer(renderer); |
9143 | attr->DecRef(); | |
9b4aede2 RD |
9144 | } |
9145 | } | |
9146 | ||
9147 | void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor) | |
9148 | { | |
9149 | if ( CanHaveAttributes() ) | |
9150 | { | |
9151 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9152 | attr->SetEditor(editor); | |
9153 | attr->DecRef(); | |
283b7808 VZ |
9154 | } |
9155 | } | |
9156 | ||
9157 | void wxGrid::SetReadOnly(int row, int col, bool isReadOnly) | |
9158 | { | |
9159 | if ( CanHaveAttributes() ) | |
9160 | { | |
9161 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9162 | attr->SetReadOnly(isReadOnly); | |
9163 | attr->DecRef(); | |
2e9a6788 | 9164 | } |
f85afd4e MB |
9165 | } |
9166 | ||
f2d76237 RD |
9167 | // ---------------------------------------------------------------------------- |
9168 | // Data type registration | |
9169 | // ---------------------------------------------------------------------------- | |
9170 | ||
9171 | void wxGrid::RegisterDataType(const wxString& typeName, | |
9172 | wxGridCellRenderer* renderer, | |
9173 | wxGridCellEditor* editor) | |
9174 | { | |
9175 | m_typeRegistry->RegisterDataType(typeName, renderer, editor); | |
9176 | } | |
9177 | ||
9178 | ||
99306db2 | 9179 | wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const |
f2d76237 RD |
9180 | { |
9181 | wxString typeName = m_table->GetTypeName(row, col); | |
9182 | return GetDefaultEditorForType(typeName); | |
9183 | } | |
9184 | ||
99306db2 | 9185 | wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const |
f2d76237 RD |
9186 | { |
9187 | wxString typeName = m_table->GetTypeName(row, col); | |
9188 | return GetDefaultRendererForType(typeName); | |
9189 | } | |
9190 | ||
99306db2 VZ |
9191 | wxGridCellEditor* |
9192 | wxGrid::GetDefaultEditorForType(const wxString& typeName) const | |
f2d76237 | 9193 | { |
c4608a8a | 9194 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
9195 | if ( index == wxNOT_FOUND ) |
9196 | { | |
9197 | wxFAIL_MSG(wxT("Unknown data type name")); | |
9198 | ||
f2d76237 RD |
9199 | return NULL; |
9200 | } | |
0b190b0f | 9201 | |
f2d76237 RD |
9202 | return m_typeRegistry->GetEditor(index); |
9203 | } | |
9204 | ||
99306db2 VZ |
9205 | wxGridCellRenderer* |
9206 | wxGrid::GetDefaultRendererForType(const wxString& typeName) const | |
f2d76237 | 9207 | { |
c4608a8a | 9208 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
9209 | if ( index == wxNOT_FOUND ) |
9210 | { | |
c4608a8a | 9211 | wxFAIL_MSG(wxT("Unknown data type name")); |
0b190b0f | 9212 | |
c4608a8a | 9213 | return NULL; |
e72b4213 | 9214 | } |
0b190b0f | 9215 | |
c4608a8a | 9216 | return m_typeRegistry->GetRenderer(index); |
f2d76237 RD |
9217 | } |
9218 | ||
9219 | ||
2e9a6788 VZ |
9220 | // ---------------------------------------------------------------------------- |
9221 | // row/col size | |
9222 | // ---------------------------------------------------------------------------- | |
9223 | ||
6e8524b1 MB |
9224 | void wxGrid::EnableDragRowSize( bool enable ) |
9225 | { | |
9226 | m_canDragRowSize = enable; | |
9227 | } | |
9228 | ||
9229 | ||
9230 | void wxGrid::EnableDragColSize( bool enable ) | |
9231 | { | |
9232 | m_canDragColSize = enable; | |
9233 | } | |
9234 | ||
4cfa5de6 RD |
9235 | void wxGrid::EnableDragGridSize( bool enable ) |
9236 | { | |
9237 | m_canDragGridSize = enable; | |
9238 | } | |
9239 | ||
6e8524b1 | 9240 | |
f85afd4e MB |
9241 | void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) |
9242 | { | |
9243 | m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT ); | |
9244 | ||
9245 | if ( resizeExistingRows ) | |
9246 | { | |
b1da8107 SN |
9247 | // since we are resizing all rows to the default row size, |
9248 | // we can simply clear the row heights and row bottoms | |
9249 | // arrays (which also allows us to take advantage of | |
9250 | // some speed optimisations) | |
9251 | m_rowHeights.Empty(); | |
9252 | m_rowBottoms.Empty(); | |
edb89f7e VZ |
9253 | if ( !GetBatchCount() ) |
9254 | CalcDimensions(); | |
f85afd4e MB |
9255 | } |
9256 | } | |
9257 | ||
9258 | void wxGrid::SetRowSize( int row, int height ) | |
9259 | { | |
b99be8fb | 9260 | wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") ); |
60ff3b99 | 9261 | |
7c1cb261 VZ |
9262 | if ( m_rowHeights.IsEmpty() ) |
9263 | { | |
9264 | // need to really create the array | |
9265 | InitRowHeights(); | |
9266 | } | |
60ff3b99 | 9267 | |
b99be8fb VZ |
9268 | int h = wxMax( 0, height ); |
9269 | int diff = h - m_rowHeights[row]; | |
60ff3b99 | 9270 | |
b99be8fb | 9271 | m_rowHeights[row] = h; |
7c1cb261 | 9272 | int i; |
b99be8fb | 9273 | for ( i = row; i < m_numRows; i++ ) |
f85afd4e | 9274 | { |
b99be8fb | 9275 | m_rowBottoms[i] += diff; |
f85afd4e | 9276 | } |
faec5a43 SN |
9277 | if ( !GetBatchCount() ) |
9278 | CalcDimensions(); | |
f85afd4e MB |
9279 | } |
9280 | ||
9281 | void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) | |
9282 | { | |
9283 | m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH ); | |
9284 | ||
9285 | if ( resizeExistingCols ) | |
9286 | { | |
b1da8107 SN |
9287 | // since we are resizing all columns to the default column size, |
9288 | // we can simply clear the col widths and col rights | |
9289 | // arrays (which also allows us to take advantage of | |
9290 | // some speed optimisations) | |
9291 | m_colWidths.Empty(); | |
9292 | m_colRights.Empty(); | |
edb89f7e VZ |
9293 | if ( !GetBatchCount() ) |
9294 | CalcDimensions(); | |
f85afd4e MB |
9295 | } |
9296 | } | |
9297 | ||
9298 | void wxGrid::SetColSize( int col, int width ) | |
9299 | { | |
b99be8fb | 9300 | wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") ); |
60ff3b99 | 9301 | |
43947979 VZ |
9302 | // should we check that it's bigger than GetColMinimalWidth(col) here? |
9303 | ||
7c1cb261 VZ |
9304 | if ( m_colWidths.IsEmpty() ) |
9305 | { | |
9306 | // need to really create the array | |
9307 | InitColWidths(); | |
9308 | } | |
f85afd4e | 9309 | |
04418332 | 9310 | // if < 0 calc new width from label |
73145b0e JS |
9311 | if( width < 0 ) |
9312 | { | |
9313 | long w, h; | |
9314 | wxArrayString lines; | |
9315 | wxClientDC dc(m_colLabelWin); | |
9316 | dc.SetFont(GetLabelFont()); | |
9317 | StringToLines(GetColLabelValue(col), lines); | |
9318 | GetTextBoxSize(dc, lines, &w, &h); | |
9319 | width = w + 6; | |
9320 | } | |
b99be8fb VZ |
9321 | int w = wxMax( 0, width ); |
9322 | int diff = w - m_colWidths[col]; | |
9323 | m_colWidths[col] = w; | |
60ff3b99 | 9324 | |
7c1cb261 | 9325 | int i; |
b99be8fb | 9326 | for ( i = col; i < m_numCols; i++ ) |
f85afd4e | 9327 | { |
b99be8fb | 9328 | m_colRights[i] += diff; |
f85afd4e | 9329 | } |
faec5a43 SN |
9330 | if ( !GetBatchCount() ) |
9331 | CalcDimensions(); | |
f85afd4e MB |
9332 | } |
9333 | ||
2d66e025 | 9334 | |
43947979 VZ |
9335 | void wxGrid::SetColMinimalWidth( int col, int width ) |
9336 | { | |
af547d51 VZ |
9337 | m_colMinWidths.Put(col, width); |
9338 | } | |
9339 | ||
9340 | void wxGrid::SetRowMinimalHeight( int row, int width ) | |
9341 | { | |
9342 | m_rowMinHeights.Put(row, width); | |
43947979 VZ |
9343 | } |
9344 | ||
9345 | int wxGrid::GetColMinimalWidth(int col) const | |
9346 | { | |
af547d51 VZ |
9347 | long value = m_colMinWidths.Get(col); |
9348 | return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH; | |
9349 | } | |
9350 | ||
9351 | int wxGrid::GetRowMinimalHeight(int row) const | |
9352 | { | |
9353 | long value = m_rowMinHeights.Get(row); | |
9354 | return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT; | |
43947979 VZ |
9355 | } |
9356 | ||
57c086ef VZ |
9357 | // ---------------------------------------------------------------------------- |
9358 | // auto sizing | |
9359 | // ---------------------------------------------------------------------------- | |
9360 | ||
af547d51 | 9361 | void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) |
65e4e78e VZ |
9362 | { |
9363 | wxClientDC dc(m_gridWin); | |
9364 | ||
a95e38c0 VZ |
9365 | // init both of them to avoid compiler warnings, even if weo nly need one |
9366 | int row = -1, | |
9367 | col = -1; | |
af547d51 VZ |
9368 | if ( column ) |
9369 | col = colOrRow; | |
9370 | else | |
9371 | row = colOrRow; | |
9372 | ||
9373 | wxCoord extent, extentMax = 0; | |
9374 | int max = column ? m_numRows : m_numCols; | |
39bcce60 | 9375 | for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ ) |
65e4e78e | 9376 | { |
af547d51 VZ |
9377 | if ( column ) |
9378 | row = rowOrCol; | |
9379 | else | |
9380 | col = rowOrCol; | |
9381 | ||
65e4e78e | 9382 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 9383 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); |
65e4e78e VZ |
9384 | if ( renderer ) |
9385 | { | |
af547d51 VZ |
9386 | wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col); |
9387 | extent = column ? size.x : size.y; | |
9388 | if ( extent > extentMax ) | |
65e4e78e | 9389 | { |
af547d51 | 9390 | extentMax = extent; |
65e4e78e | 9391 | } |
0b190b0f VZ |
9392 | |
9393 | renderer->DecRef(); | |
65e4e78e VZ |
9394 | } |
9395 | ||
9396 | attr->DecRef(); | |
9397 | } | |
9398 | ||
af547d51 VZ |
9399 | // now also compare with the column label extent |
9400 | wxCoord w, h; | |
65e4e78e | 9401 | dc.SetFont( GetLabelFont() ); |
294d195c MB |
9402 | |
9403 | if ( column ) | |
d43851f7 | 9404 | { |
294d195c | 9405 | dc.GetTextExtent( GetColLabelValue(col), &w, &h ); |
d43851f7 JS |
9406 | if( GetColLabelTextOrientation() == wxVERTICAL ) |
9407 | w = h; | |
9408 | } | |
294d195c | 9409 | else |
ee495e4c | 9410 | dc.GetTextExtent( GetRowLabelValue(row), &w, &h ); |
294d195c | 9411 | |
af547d51 VZ |
9412 | extent = column ? w : h; |
9413 | if ( extent > extentMax ) | |
65e4e78e | 9414 | { |
af547d51 | 9415 | extentMax = extent; |
65e4e78e VZ |
9416 | } |
9417 | ||
af547d51 | 9418 | if ( !extentMax ) |
65e4e78e | 9419 | { |
af547d51 VZ |
9420 | // empty column - give default extent (notice that if extentMax is less |
9421 | // than default extent but != 0, it's ok) | |
9422 | extentMax = column ? m_defaultColWidth : m_defaultRowHeight; | |
65e4e78e VZ |
9423 | } |
9424 | else | |
9425 | { | |
a95e38c0 VZ |
9426 | if ( column ) |
9427 | { | |
9428 | // leave some space around text | |
9429 | extentMax += 10; | |
9430 | } | |
f6bcfd97 BP |
9431 | else |
9432 | { | |
9433 | extentMax += 6; | |
9434 | } | |
65e4e78e VZ |
9435 | } |
9436 | ||
edb89f7e VZ |
9437 | if ( column ) |
9438 | { | |
af547d51 | 9439 | SetColSize(col, extentMax); |
faec5a43 SN |
9440 | if ( !GetBatchCount() ) |
9441 | { | |
edb89f7e VZ |
9442 | int cw, ch, dummy; |
9443 | m_gridWin->GetClientSize( &cw, &ch ); | |
9444 | wxRect rect ( CellToRect( 0, col ) ); | |
9445 | rect.y = 0; | |
9446 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
9447 | rect.width = cw - rect.x; | |
9448 | rect.height = m_colLabelHeight; | |
9449 | m_colLabelWin->Refresh( TRUE, &rect ); | |
9450 | } | |
9451 | } | |
9452 | else | |
9453 | { | |
39bcce60 | 9454 | SetRowSize(row, extentMax); |
faec5a43 SN |
9455 | if ( !GetBatchCount() ) |
9456 | { | |
edb89f7e VZ |
9457 | int cw, ch, dummy; |
9458 | m_gridWin->GetClientSize( &cw, &ch ); | |
9459 | wxRect rect ( CellToRect( row, 0 ) ); | |
9460 | rect.x = 0; | |
9461 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
9462 | rect.width = m_rowLabelWidth; | |
faec5a43 | 9463 | rect.height = ch - rect.y; |
edb89f7e VZ |
9464 | m_rowLabelWin->Refresh( TRUE, &rect ); |
9465 | } | |
faec5a43 | 9466 | } |
65e4e78e VZ |
9467 | if ( setAsMin ) |
9468 | { | |
af547d51 VZ |
9469 | if ( column ) |
9470 | SetColMinimalWidth(col, extentMax); | |
9471 | else | |
39bcce60 | 9472 | SetRowMinimalHeight(row, extentMax); |
65e4e78e VZ |
9473 | } |
9474 | } | |
9475 | ||
266e8367 | 9476 | int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) |
65e4e78e | 9477 | { |
57c086ef VZ |
9478 | int width = m_rowLabelWidth; |
9479 | ||
faec5a43 SN |
9480 | if ( !calcOnly ) |
9481 | BeginBatch(); | |
97a9929e | 9482 | |
65e4e78e VZ |
9483 | for ( int col = 0; col < m_numCols; col++ ) |
9484 | { | |
266e8367 VZ |
9485 | if ( !calcOnly ) |
9486 | { | |
9487 | AutoSizeColumn(col, setAsMin); | |
9488 | } | |
57c086ef VZ |
9489 | |
9490 | width += GetColWidth(col); | |
65e4e78e | 9491 | } |
97a9929e | 9492 | |
faec5a43 SN |
9493 | if ( !calcOnly ) |
9494 | EndBatch(); | |
97a9929e | 9495 | |
266e8367 | 9496 | return width; |
65e4e78e VZ |
9497 | } |
9498 | ||
266e8367 | 9499 | int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin) |
57c086ef VZ |
9500 | { |
9501 | int height = m_colLabelHeight; | |
9502 | ||
faec5a43 SN |
9503 | if ( !calcOnly ) |
9504 | BeginBatch(); | |
97a9929e | 9505 | |
57c086ef VZ |
9506 | for ( int row = 0; row < m_numRows; row++ ) |
9507 | { | |
af547d51 VZ |
9508 | if ( !calcOnly ) |
9509 | { | |
9510 | AutoSizeRow(row, setAsMin); | |
9511 | } | |
57c086ef VZ |
9512 | |
9513 | height += GetRowHeight(row); | |
9514 | } | |
97a9929e | 9515 | |
faec5a43 SN |
9516 | if ( !calcOnly ) |
9517 | EndBatch(); | |
97a9929e | 9518 | |
266e8367 | 9519 | return height; |
57c086ef VZ |
9520 | } |
9521 | ||
9522 | void wxGrid::AutoSize() | |
9523 | { | |
97a9929e VZ |
9524 | BeginBatch(); |
9525 | ||
9526 | wxSize size(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE)); | |
9527 | ||
9528 | // round up the size to a multiple of scroll step - this ensures that we | |
9529 | // won't get the scrollbars if we're sized exactly to this width | |
2b5f62a0 VZ |
9530 | // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary |
9531 | // scrollbar steps | |
9532 | wxSize sizeFit(GetScrollX(size.x + m_extraWidth + 1) * GRID_SCROLL_LINE_X, | |
9533 | GetScrollY(size.y + m_extraHeight + 1) * GRID_SCROLL_LINE_Y); | |
97a9929e | 9534 | |
2b5f62a0 | 9535 | // distribute the extra space between the columns/rows to avoid having |
97a9929e | 9536 | // extra white space |
2b5f62a0 VZ |
9537 | |
9538 | // Remove the extra m_extraWidth + 1 added above | |
9539 | wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1); | |
9540 | if ( diff && m_numCols ) | |
97a9929e VZ |
9541 | { |
9542 | // try to resize the columns uniformly | |
9543 | wxCoord diffPerCol = diff / m_numCols; | |
9544 | if ( diffPerCol ) | |
9545 | { | |
9546 | for ( int col = 0; col < m_numCols; col++ ) | |
9547 | { | |
9548 | SetColSize(col, GetColWidth(col) + diffPerCol); | |
9549 | } | |
9550 | } | |
9551 | ||
9552 | // add remaining amount to the last columns | |
9553 | diff -= diffPerCol * m_numCols; | |
9554 | if ( diff ) | |
9555 | { | |
9556 | for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- ) | |
9557 | { | |
9558 | SetColSize(col, GetColWidth(col) + 1); | |
9559 | } | |
9560 | } | |
9561 | } | |
9562 | ||
9563 | // same for rows | |
2b5f62a0 VZ |
9564 | diff = sizeFit.y - size.y - (m_extraHeight + 1); |
9565 | if ( diff && m_numRows ) | |
97a9929e VZ |
9566 | { |
9567 | // try to resize the columns uniformly | |
9568 | wxCoord diffPerRow = diff / m_numRows; | |
9569 | if ( diffPerRow ) | |
9570 | { | |
9571 | for ( int row = 0; row < m_numRows; row++ ) | |
9572 | { | |
9573 | SetRowSize(row, GetRowHeight(row) + diffPerRow); | |
9574 | } | |
9575 | } | |
9576 | ||
9577 | // add remaining amount to the last rows | |
9578 | diff -= diffPerRow * m_numRows; | |
9579 | if ( diff ) | |
9580 | { | |
9581 | for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- ) | |
9582 | { | |
9583 | SetRowSize(row, GetRowHeight(row) + 1); | |
9584 | } | |
9585 | } | |
9586 | } | |
9587 | ||
9588 | EndBatch(); | |
9589 | ||
9590 | SetClientSize(sizeFit); | |
266e8367 VZ |
9591 | } |
9592 | ||
d43851f7 JS |
9593 | void wxGrid::AutoSizeRowLabelSize( int row ) |
9594 | { | |
9595 | wxArrayString lines; | |
9596 | long w, h; | |
9597 | ||
9598 | // Hide the edit control, so it | |
9599 | // won't interfer with drag-shrinking. | |
9600 | if( IsCellEditControlShown() ) | |
9601 | { | |
9602 | HideCellEditControl(); | |
9603 | SaveEditControlValue(); | |
9604 | } | |
9605 | ||
9606 | // autosize row height depending on label text | |
9607 | StringToLines( GetRowLabelValue( row ), lines ); | |
9608 | wxClientDC dc( m_rowLabelWin ); | |
9609 | GetTextBoxSize( dc, lines, &w, &h); | |
9610 | if( h < m_defaultRowHeight ) | |
9611 | h = m_defaultRowHeight; | |
9612 | SetRowSize(row, h); | |
9613 | ForceRefresh(); | |
9614 | } | |
9615 | ||
9616 | void wxGrid::AutoSizeColLabelSize( int col ) | |
9617 | { | |
9618 | wxArrayString lines; | |
9619 | long w, h; | |
9620 | ||
9621 | // Hide the edit control, so it | |
9622 | // won't interfer with drag-shrinking. | |
9623 | if( IsCellEditControlShown() ) | |
9624 | { | |
9625 | HideCellEditControl(); | |
9626 | SaveEditControlValue(); | |
9627 | } | |
9628 | ||
9629 | // autosize column width depending on label text | |
9630 | StringToLines( GetColLabelValue( col ), lines ); | |
9631 | wxClientDC dc( m_colLabelWin ); | |
9632 | if( GetColLabelTextOrientation() == wxHORIZONTAL ) | |
9633 | GetTextBoxSize( dc, lines, &w, &h); | |
9634 | else | |
9635 | GetTextBoxSize( dc, lines, &h, &w); | |
9636 | if( w < m_defaultColWidth ) | |
9637 | w = m_defaultColWidth; | |
9638 | SetColSize(col, w); | |
9639 | ForceRefresh(); | |
9640 | } | |
9641 | ||
266e8367 VZ |
9642 | wxSize wxGrid::DoGetBestSize() const |
9643 | { | |
9644 | // don't set sizes, only calculate them | |
9645 | wxGrid *self = (wxGrid *)this; // const_cast | |
9646 | ||
84035150 SN |
9647 | int width, height; |
9648 | width = self->SetOrCalcColumnSizes(TRUE); | |
9649 | height = self->SetOrCalcRowSizes(TRUE); | |
9650 | ||
9651 | int maxwidth, maxheight; | |
9652 | wxDisplaySize( & maxwidth, & maxheight ); | |
9653 | ||
9654 | if ( width > maxwidth ) width = maxwidth; | |
9655 | if ( height > maxheight ) height = maxheight; | |
9656 | ||
9657 | return wxSize( width, height ); | |
266e8367 VZ |
9658 | } |
9659 | ||
9660 | void wxGrid::Fit() | |
9661 | { | |
9662 | AutoSize(); | |
57c086ef VZ |
9663 | } |
9664 | ||
6fc0f38f SN |
9665 | |
9666 | wxPen& wxGrid::GetDividerPen() const | |
9667 | { | |
9668 | return wxNullPen; | |
9669 | } | |
9670 | ||
57c086ef VZ |
9671 | // ---------------------------------------------------------------------------- |
9672 | // cell value accessor functions | |
9673 | // ---------------------------------------------------------------------------- | |
f85afd4e MB |
9674 | |
9675 | void wxGrid::SetCellValue( int row, int col, const wxString& s ) | |
9676 | { | |
9677 | if ( m_table ) | |
9678 | { | |
f6bcfd97 | 9679 | m_table->SetValue( row, col, s ); |
2d66e025 MB |
9680 | if ( !GetBatchCount() ) |
9681 | { | |
27f35b66 SN |
9682 | int dummy; |
9683 | wxRect rect( CellToRect( row, col ) ); | |
9684 | rect.x = 0; | |
9685 | rect.width = m_gridWin->GetClientSize().GetWidth(); | |
9686 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
9687 | m_gridWin->Refresh( FALSE, &rect ); | |
2d66e025 | 9688 | } |
60ff3b99 | 9689 | |
f85afd4e | 9690 | if ( m_currentCellCoords.GetRow() == row && |
4cfa5de6 | 9691 | m_currentCellCoords.GetCol() == col && |
f6bcfd97 BP |
9692 | IsCellEditControlShown()) |
9693 | // Note: If we are using IsCellEditControlEnabled, | |
9694 | // this interacts badly with calling SetCellValue from | |
9695 | // an EVT_GRID_CELL_CHANGE handler. | |
f85afd4e | 9696 | { |
4cfa5de6 RD |
9697 | HideCellEditControl(); |
9698 | ShowCellEditControl(); // will reread data from table | |
f85afd4e | 9699 | } |
f85afd4e MB |
9700 | } |
9701 | } | |
9702 | ||
9703 | ||
9704 | // | |
2d66e025 | 9705 | // ------ Block, row and col selection |
f85afd4e MB |
9706 | // |
9707 | ||
9708 | void wxGrid::SelectRow( int row, bool addToSelected ) | |
9709 | { | |
b5808881 | 9710 | if ( IsSelection() && !addToSelected ) |
e32352cf | 9711 | ClearSelection(); |
70c7a608 | 9712 | |
3f3dc2ef VZ |
9713 | if ( m_selection ) |
9714 | m_selection->SelectRow( row, FALSE, addToSelected ); | |
f85afd4e MB |
9715 | } |
9716 | ||
9717 | ||
9718 | void wxGrid::SelectCol( int col, bool addToSelected ) | |
9719 | { | |
b5808881 | 9720 | if ( IsSelection() && !addToSelected ) |
e32352cf | 9721 | ClearSelection(); |
f85afd4e | 9722 | |
3f3dc2ef VZ |
9723 | if ( m_selection ) |
9724 | m_selection->SelectCol( col, FALSE, addToSelected ); | |
f85afd4e MB |
9725 | } |
9726 | ||
9727 | ||
84912ef8 | 9728 | void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol, |
c9097836 | 9729 | bool addToSelected ) |
f85afd4e | 9730 | { |
c9097836 MB |
9731 | if ( IsSelection() && !addToSelected ) |
9732 | ClearSelection(); | |
f85afd4e | 9733 | |
3f3dc2ef VZ |
9734 | if ( m_selection ) |
9735 | m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol, | |
9736 | FALSE, addToSelected ); | |
f85afd4e MB |
9737 | } |
9738 | ||
c9097836 | 9739 | |
f85afd4e MB |
9740 | void wxGrid::SelectAll() |
9741 | { | |
f74d0b57 | 9742 | if ( m_numRows > 0 && m_numCols > 0 ) |
3f3dc2ef VZ |
9743 | { |
9744 | if ( m_selection ) | |
9745 | m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 ); | |
9746 | } | |
b5808881 | 9747 | } |
f85afd4e | 9748 | |
f7b4b343 VZ |
9749 | // |
9750 | // ------ Cell, row and col deselection | |
9751 | // | |
9752 | ||
9753 | void wxGrid::DeselectRow( int row ) | |
9754 | { | |
3f3dc2ef VZ |
9755 | if ( !m_selection ) |
9756 | return; | |
9757 | ||
f7b4b343 VZ |
9758 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows ) |
9759 | { | |
9760 | if ( m_selection->IsInSelection(row, 0 ) ) | |
9761 | m_selection->ToggleCellSelection( row, 0); | |
ffdd3c98 | 9762 | } |
f7b4b343 VZ |
9763 | else |
9764 | { | |
9765 | int nCols = GetNumberCols(); | |
9766 | for ( int i = 0; i < nCols ; i++ ) | |
9767 | { | |
9768 | if ( m_selection->IsInSelection(row, i ) ) | |
9769 | m_selection->ToggleCellSelection( row, i); | |
9770 | } | |
9771 | } | |
9772 | } | |
9773 | ||
9774 | void wxGrid::DeselectCol( int col ) | |
9775 | { | |
3f3dc2ef VZ |
9776 | if ( !m_selection ) |
9777 | return; | |
9778 | ||
f7b4b343 VZ |
9779 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns ) |
9780 | { | |
9781 | if ( m_selection->IsInSelection(0, col ) ) | |
9782 | m_selection->ToggleCellSelection( 0, col); | |
9783 | } | |
9784 | else | |
9785 | { | |
9786 | int nRows = GetNumberRows(); | |
9787 | for ( int i = 0; i < nRows ; i++ ) | |
9788 | { | |
9789 | if ( m_selection->IsInSelection(i, col ) ) | |
9790 | m_selection->ToggleCellSelection(i, col); | |
9791 | } | |
9792 | } | |
9793 | } | |
9794 | ||
9795 | void wxGrid::DeselectCell( int row, int col ) | |
9796 | { | |
3f3dc2ef | 9797 | if ( m_selection && m_selection->IsInSelection(row, col) ) |
f7b4b343 VZ |
9798 | m_selection->ToggleCellSelection(row, col); |
9799 | } | |
9800 | ||
b5808881 SN |
9801 | bool wxGrid::IsSelection() |
9802 | { | |
3f3dc2ef | 9803 | return ( m_selection && (m_selection->IsSelection() || |
b5808881 | 9804 | ( m_selectingTopLeft != wxGridNoCellCoords && |
3f3dc2ef | 9805 | m_selectingBottomRight != wxGridNoCellCoords) ) ); |
f85afd4e MB |
9806 | } |
9807 | ||
84035150 | 9808 | bool wxGrid::IsInSelection( int row, int col ) const |
b5808881 | 9809 | { |
3f3dc2ef | 9810 | return ( m_selection && (m_selection->IsInSelection( row, col ) || |
b5808881 SN |
9811 | ( row >= m_selectingTopLeft.GetRow() && |
9812 | col >= m_selectingTopLeft.GetCol() && | |
9813 | row <= m_selectingBottomRight.GetRow() && | |
3f3dc2ef | 9814 | col <= m_selectingBottomRight.GetCol() )) ); |
b5808881 | 9815 | } |
f85afd4e | 9816 | |
aa5b8857 SN |
9817 | wxGridCellCoordsArray wxGrid::GetSelectedCells() const |
9818 | { | |
9819 | if (!m_selection) { wxGridCellCoordsArray a; return a; } | |
9820 | return m_selection->m_cellSelection; | |
9821 | } | |
9822 | wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const | |
9823 | { | |
9824 | if (!m_selection) { wxGridCellCoordsArray a; return a; } | |
9825 | return m_selection->m_blockSelectionTopLeft; | |
9826 | } | |
9827 | wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const | |
9828 | { | |
9829 | if (!m_selection) { wxGridCellCoordsArray a; return a; } | |
a8de8190 | 9830 | return m_selection->m_blockSelectionBottomRight; |
aa5b8857 SN |
9831 | } |
9832 | wxArrayInt wxGrid::GetSelectedRows() const | |
9833 | { | |
9834 | if (!m_selection) { wxArrayInt a; return a; } | |
9835 | return m_selection->m_rowSelection; | |
9836 | } | |
9837 | wxArrayInt wxGrid::GetSelectedCols() const | |
9838 | { | |
9839 | if (!m_selection) { wxArrayInt a; return a; } | |
9840 | return m_selection->m_colSelection; | |
9841 | } | |
9842 | ||
9843 | ||
f85afd4e MB |
9844 | void wxGrid::ClearSelection() |
9845 | { | |
b5808881 SN |
9846 | m_selectingTopLeft = wxGridNoCellCoords; |
9847 | m_selectingBottomRight = wxGridNoCellCoords; | |
3f3dc2ef VZ |
9848 | if ( m_selection ) |
9849 | m_selection->ClearSelection(); | |
8f177c8e | 9850 | } |
f85afd4e MB |
9851 | |
9852 | ||
da6af900 | 9853 | // This function returns the rectangle that encloses the given block |
2d66e025 MB |
9854 | // in device coords clipped to the client size of the grid window. |
9855 | // | |
58dd5b3b MB |
9856 | wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft, |
9857 | const wxGridCellCoords &bottomRight ) | |
f85afd4e | 9858 | { |
58dd5b3b | 9859 | wxRect rect( wxGridNoCellRect ); |
f85afd4e MB |
9860 | wxRect cellRect; |
9861 | ||
58dd5b3b MB |
9862 | cellRect = CellToRect( topLeft ); |
9863 | if ( cellRect != wxGridNoCellRect ) | |
f85afd4e | 9864 | { |
58dd5b3b MB |
9865 | rect = cellRect; |
9866 | } | |
9867 | else | |
9868 | { | |
9869 | rect = wxRect( 0, 0, 0, 0 ); | |
9870 | } | |
60ff3b99 | 9871 | |
58dd5b3b MB |
9872 | cellRect = CellToRect( bottomRight ); |
9873 | if ( cellRect != wxGridNoCellRect ) | |
9874 | { | |
9875 | rect += cellRect; | |
2d66e025 MB |
9876 | } |
9877 | else | |
9878 | { | |
9879 | return wxGridNoCellRect; | |
f85afd4e MB |
9880 | } |
9881 | ||
27f35b66 SN |
9882 | int i, j; |
9883 | int left = rect.GetLeft(); | |
9884 | int top = rect.GetTop(); | |
9885 | int right = rect.GetRight(); | |
9886 | int bottom = rect.GetBottom(); | |
9887 | ||
9888 | int leftCol = topLeft.GetCol(); | |
9889 | int topRow = topLeft.GetRow(); | |
9890 | int rightCol = bottomRight.GetCol(); | |
9891 | int bottomRow = bottomRight.GetRow(); | |
9892 | ||
3ed884a0 SN |
9893 | if (left > right) |
9894 | { | |
9895 | i = left; | |
9896 | left = right; | |
9897 | right = i; | |
9898 | i = leftCol; | |
9899 | leftCol=rightCol; | |
9900 | rightCol = i; | |
9901 | } | |
9902 | ||
9903 | if (top > bottom) | |
9904 | { | |
9905 | i = top; | |
9906 | top = bottom; | |
9907 | bottom = i; | |
9908 | i = topRow; | |
9909 | topRow = bottomRow; | |
9910 | bottomRow = i; | |
9911 | } | |
9912 | ||
9913 | ||
27f35b66 SN |
9914 | for ( j = topRow; j <= bottomRow; j++ ) |
9915 | { | |
9916 | for ( i = leftCol; i <= rightCol; i++ ) | |
9917 | { | |
9918 | if ((j==topRow) || (j==bottomRow) || (i==leftCol) || (i==rightCol)) | |
9919 | { | |
9920 | cellRect = CellToRect( j, i ); | |
9921 | ||
9922 | if (cellRect.x < left) | |
9923 | left = cellRect.x; | |
9924 | if (cellRect.y < top) | |
9925 | top = cellRect.y; | |
9926 | if (cellRect.x + cellRect.width > right) | |
9927 | right = cellRect.x + cellRect.width; | |
9928 | if (cellRect.y + cellRect.height > bottom) | |
9929 | bottom = cellRect.y + cellRect.height; | |
9930 | } | |
3ed884a0 | 9931 | else i = rightCol; // jump over inner cells. |
27f35b66 SN |
9932 | } |
9933 | } | |
9934 | ||
58dd5b3b MB |
9935 | // convert to scrolled coords |
9936 | // | |
27f35b66 SN |
9937 | CalcScrolledPosition( left, top, &left, &top ); |
9938 | CalcScrolledPosition( right, bottom, &right, &bottom ); | |
58dd5b3b MB |
9939 | |
9940 | int cw, ch; | |
9941 | m_gridWin->GetClientSize( &cw, &ch ); | |
9942 | ||
f6bcfd97 BP |
9943 | if (right < 0 || bottom < 0 || left > cw || top > ch) |
9944 | return wxRect( 0, 0, 0, 0); | |
9945 | ||
58dd5b3b MB |
9946 | rect.SetLeft( wxMax(0, left) ); |
9947 | rect.SetTop( wxMax(0, top) ); | |
9948 | rect.SetRight( wxMin(cw, right) ); | |
9949 | rect.SetBottom( wxMin(ch, bottom) ); | |
9950 | ||
f85afd4e MB |
9951 | return rect; |
9952 | } | |
9953 | ||
9954 | ||
58dd5b3b | 9955 | |
f85afd4e MB |
9956 | // |
9957 | // ------ Grid event classes | |
9958 | // | |
9959 | ||
bf7945ce | 9960 | IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent ) |
f85afd4e MB |
9961 | |
9962 | wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, | |
5c8fc7c1 | 9963 | int row, int col, int x, int y, bool sel, |
f85afd4e MB |
9964 | bool control, bool shift, bool alt, bool meta ) |
9965 | : wxNotifyEvent( type, id ) | |
9966 | { | |
9967 | m_row = row; | |
9968 | m_col = col; | |
9969 | m_x = x; | |
9970 | m_y = y; | |
5c8fc7c1 | 9971 | m_selecting = sel; |
f85afd4e MB |
9972 | m_control = control; |
9973 | m_shift = shift; | |
9974 | m_alt = alt; | |
9975 | m_meta = meta; | |
8f177c8e | 9976 | |
f85afd4e MB |
9977 | SetEventObject(obj); |
9978 | } | |
9979 | ||
9980 | ||
bf7945ce | 9981 | IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent ) |
f85afd4e MB |
9982 | |
9983 | wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj, | |
9984 | int rowOrCol, int x, int y, | |
9985 | bool control, bool shift, bool alt, bool meta ) | |
9986 | : wxNotifyEvent( type, id ) | |
9987 | { | |
9988 | m_rowOrCol = rowOrCol; | |
9989 | m_x = x; | |
9990 | m_y = y; | |
9991 | m_control = control; | |
9992 | m_shift = shift; | |
9993 | m_alt = alt; | |
9994 | m_meta = meta; | |
8f177c8e | 9995 | |
f85afd4e MB |
9996 | SetEventObject(obj); |
9997 | } | |
9998 | ||
9999 | ||
bf7945ce | 10000 | IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent ) |
f85afd4e MB |
10001 | |
10002 | wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, | |
8f177c8e VZ |
10003 | const wxGridCellCoords& topLeft, |
10004 | const wxGridCellCoords& bottomRight, | |
5c8fc7c1 SN |
10005 | bool sel, bool control, |
10006 | bool shift, bool alt, bool meta ) | |
8f177c8e | 10007 | : wxNotifyEvent( type, id ) |
f85afd4e MB |
10008 | { |
10009 | m_topLeft = topLeft; | |
10010 | m_bottomRight = bottomRight; | |
5c8fc7c1 | 10011 | m_selecting = sel; |
f85afd4e MB |
10012 | m_control = control; |
10013 | m_shift = shift; | |
10014 | m_alt = alt; | |
10015 | m_meta = meta; | |
10016 | ||
10017 | SetEventObject(obj); | |
10018 | } | |
10019 | ||
10020 | ||
bf7945ce RD |
10021 | IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent) |
10022 | ||
10023 | wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type, | |
10024 | wxObject* obj, int row, | |
10025 | int col, wxControl* ctrl) | |
10026 | : wxCommandEvent(type, id) | |
10027 | { | |
10028 | SetEventObject(obj); | |
10029 | m_row = row; | |
10030 | m_col = col; | |
10031 | m_ctrl = ctrl; | |
10032 | } | |
10033 | ||
10034 | ||
27b92ca4 VZ |
10035 | #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID |
10036 | ||
10037 | #endif // wxUSE_GRID |