]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/grid.cpp | |
3 | // Purpose: wxGrid and related classes | |
4 | // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn) | |
5 | // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios | |
6 | // Created: 1/08/1999 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx/wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if wxUSE_GRID | |
20 | ||
21 | #include "wx/grid.h" | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/utils.h" | |
25 | #include "wx/dcclient.h" | |
26 | #include "wx/settings.h" | |
27 | #include "wx/log.h" | |
28 | #include "wx/textctrl.h" | |
29 | #include "wx/checkbox.h" | |
30 | #include "wx/combobox.h" | |
31 | #include "wx/valtext.h" | |
32 | #include "wx/intl.h" | |
33 | #include "wx/math.h" | |
34 | #include "wx/listbox.h" | |
35 | #endif | |
36 | ||
37 | #include "wx/textfile.h" | |
38 | #include "wx/spinctrl.h" | |
39 | #include "wx/tokenzr.h" | |
40 | #include "wx/renderer.h" | |
41 | ||
42 | #include "wx/generic/gridsel.h" | |
43 | ||
44 | const wxChar wxGridNameStr[] = wxT("grid"); | |
45 | ||
46 | #if defined(__WXMOTIF__) | |
47 | #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier) | |
48 | #else | |
49 | #define WXUNUSED_MOTIF(identifier) identifier | |
50 | #endif | |
51 | ||
52 | #if defined(__WXGTK__) | |
53 | #define WXUNUSED_GTK(identifier) WXUNUSED(identifier) | |
54 | #else | |
55 | #define WXUNUSED_GTK(identifier) identifier | |
56 | #endif | |
57 | ||
58 | // Required for wxIs... functions | |
59 | #include <ctype.h> | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // array classes | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr *, wxArrayAttrs, | |
66 | class WXDLLIMPEXP_ADV); | |
67 | ||
68 | struct wxGridCellWithAttr | |
69 | { | |
70 | wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_) | |
71 | : coords(row, col), attr(attr_) | |
72 | { | |
73 | } | |
74 | ||
75 | ~wxGridCellWithAttr() | |
76 | { | |
77 | attr->DecRef(); | |
78 | } | |
79 | ||
80 | wxGridCellCoords coords; | |
81 | wxGridCellAttr *attr; | |
82 | ||
83 | // Cannot do this: | |
84 | // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr) | |
85 | // without rewriting the macros, which require a public copy constructor. | |
86 | }; | |
87 | ||
88 | WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr, wxGridCellWithAttrArray, | |
89 | class WXDLLIMPEXP_ADV); | |
90 | ||
91 | #include "wx/arrimpl.cpp" | |
92 | ||
93 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) | |
94 | WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray) | |
95 | ||
96 | // ---------------------------------------------------------------------------- | |
97 | // events | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
100 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK) | |
101 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK) | |
102 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK) | |
103 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK) | |
104 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG) | |
105 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK) | |
106 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK) | |
107 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK) | |
108 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK) | |
109 | DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE) | |
110 | DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE) | |
111 | DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE) | |
112 | DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT) | |
113 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE) | |
114 | DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL) | |
115 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN) | |
116 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN) | |
117 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED) | |
118 | ||
119 | // ---------------------------------------------------------------------------- | |
120 | // private classes | |
121 | // ---------------------------------------------------------------------------- | |
122 | ||
123 | class WXDLLIMPEXP_ADV wxGridRowLabelWindow : public wxWindow | |
124 | { | |
125 | public: | |
126 | wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; } | |
127 | wxGridRowLabelWindow( wxGrid *parent, wxWindowID id, | |
128 | const wxPoint &pos, const wxSize &size ); | |
129 | ||
130 | private: | |
131 | wxGrid *m_owner; | |
132 | ||
133 | void OnPaint( wxPaintEvent& event ); | |
134 | void OnMouseEvent( wxMouseEvent& event ); | |
135 | void OnMouseWheel( wxMouseEvent& event ); | |
136 | void OnKeyDown( wxKeyEvent& event ); | |
137 | void OnKeyUp( wxKeyEvent& ); | |
138 | void OnChar( wxKeyEvent& ); | |
139 | ||
140 | DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow) | |
141 | DECLARE_EVENT_TABLE() | |
142 | DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow) | |
143 | }; | |
144 | ||
145 | ||
146 | class WXDLLIMPEXP_ADV wxGridColLabelWindow : public wxWindow | |
147 | { | |
148 | public: | |
149 | wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; } | |
150 | wxGridColLabelWindow( wxGrid *parent, wxWindowID id, | |
151 | const wxPoint &pos, const wxSize &size ); | |
152 | ||
153 | private: | |
154 | wxGrid *m_owner; | |
155 | ||
156 | void OnPaint( wxPaintEvent& event ); | |
157 | void OnMouseEvent( wxMouseEvent& event ); | |
158 | void OnMouseWheel( wxMouseEvent& event ); | |
159 | void OnKeyDown( wxKeyEvent& event ); | |
160 | void OnKeyUp( wxKeyEvent& ); | |
161 | void OnChar( wxKeyEvent& ); | |
162 | ||
163 | DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow) | |
164 | DECLARE_EVENT_TABLE() | |
165 | DECLARE_NO_COPY_CLASS(wxGridColLabelWindow) | |
166 | }; | |
167 | ||
168 | ||
169 | class WXDLLIMPEXP_ADV wxGridCornerLabelWindow : public wxWindow | |
170 | { | |
171 | public: | |
172 | wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; } | |
173 | wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id, | |
174 | const wxPoint &pos, const wxSize &size ); | |
175 | ||
176 | private: | |
177 | wxGrid *m_owner; | |
178 | ||
179 | void OnMouseEvent( wxMouseEvent& event ); | |
180 | void OnMouseWheel( wxMouseEvent& event ); | |
181 | void OnKeyDown( wxKeyEvent& event ); | |
182 | void OnKeyUp( wxKeyEvent& ); | |
183 | void OnChar( wxKeyEvent& ); | |
184 | void OnPaint( wxPaintEvent& event ); | |
185 | ||
186 | DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow) | |
187 | DECLARE_EVENT_TABLE() | |
188 | DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow) | |
189 | }; | |
190 | ||
191 | class WXDLLIMPEXP_ADV wxGridWindow : public wxWindow | |
192 | { | |
193 | public: | |
194 | wxGridWindow() | |
195 | { | |
196 | m_owner = NULL; | |
197 | m_rowLabelWin = NULL; | |
198 | m_colLabelWin = NULL; | |
199 | } | |
200 | ||
201 | wxGridWindow( wxGrid *parent, | |
202 | wxGridRowLabelWindow *rowLblWin, | |
203 | wxGridColLabelWindow *colLblWin, | |
204 | wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
205 | virtual ~wxGridWindow() {} | |
206 | ||
207 | void ScrollWindow( int dx, int dy, const wxRect *rect ); | |
208 | ||
209 | wxGrid* GetOwner() { return m_owner; } | |
210 | ||
211 | private: | |
212 | wxGrid *m_owner; | |
213 | wxGridRowLabelWindow *m_rowLabelWin; | |
214 | wxGridColLabelWindow *m_colLabelWin; | |
215 | ||
216 | void OnPaint( wxPaintEvent &event ); | |
217 | void OnMouseWheel( wxMouseEvent& event ); | |
218 | void OnMouseEvent( wxMouseEvent& event ); | |
219 | void OnKeyDown( wxKeyEvent& ); | |
220 | void OnKeyUp( wxKeyEvent& ); | |
221 | void OnChar( wxKeyEvent& ); | |
222 | void OnEraseBackground( wxEraseEvent& ); | |
223 | void OnFocus( wxFocusEvent& ); | |
224 | ||
225 | DECLARE_DYNAMIC_CLASS(wxGridWindow) | |
226 | DECLARE_EVENT_TABLE() | |
227 | DECLARE_NO_COPY_CLASS(wxGridWindow) | |
228 | }; | |
229 | ||
230 | ||
231 | class wxGridCellEditorEvtHandler : public wxEvtHandler | |
232 | { | |
233 | public: | |
234 | wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor) | |
235 | : m_grid(grid), | |
236 | m_editor(editor), | |
237 | m_inSetFocus(false) | |
238 | { | |
239 | } | |
240 | ||
241 | void OnKillFocus(wxFocusEvent& event); | |
242 | void OnKeyDown(wxKeyEvent& event); | |
243 | void OnChar(wxKeyEvent& event); | |
244 | ||
245 | void SetInSetFocus(bool inSetFocus) { m_inSetFocus = inSetFocus; } | |
246 | ||
247 | private: | |
248 | wxGrid *m_grid; | |
249 | wxGridCellEditor *m_editor; | |
250 | ||
251 | // Work around the fact that a focus kill event can be sent to | |
252 | // a combobox within a set focus event. | |
253 | bool m_inSetFocus; | |
254 | ||
255 | DECLARE_EVENT_TABLE() | |
256 | DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler) | |
257 | DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler) | |
258 | }; | |
259 | ||
260 | ||
261 | IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler, wxEvtHandler) | |
262 | ||
263 | BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler ) | |
264 | EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus ) | |
265 | EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown ) | |
266 | EVT_CHAR( wxGridCellEditorEvtHandler::OnChar ) | |
267 | END_EVENT_TABLE() | |
268 | ||
269 | ||
270 | // ---------------------------------------------------------------------------- | |
271 | // the internal data representation used by wxGridCellAttrProvider | |
272 | // ---------------------------------------------------------------------------- | |
273 | ||
274 | // this class stores attributes set for cells | |
275 | class WXDLLIMPEXP_ADV wxGridCellAttrData | |
276 | { | |
277 | public: | |
278 | void SetAttr(wxGridCellAttr *attr, int row, int col); | |
279 | wxGridCellAttr *GetAttr(int row, int col) const; | |
280 | void UpdateAttrRows( size_t pos, int numRows ); | |
281 | void UpdateAttrCols( size_t pos, int numCols ); | |
282 | ||
283 | private: | |
284 | // searches for the attr for given cell, returns wxNOT_FOUND if not found | |
285 | int FindIndex(int row, int col) const; | |
286 | ||
287 | wxGridCellWithAttrArray m_attrs; | |
288 | }; | |
289 | ||
290 | // this class stores attributes set for rows or columns | |
291 | class WXDLLIMPEXP_ADV wxGridRowOrColAttrData | |
292 | { | |
293 | public: | |
294 | // empty ctor to suppress warnings | |
295 | wxGridRowOrColAttrData() {} | |
296 | ~wxGridRowOrColAttrData(); | |
297 | ||
298 | void SetAttr(wxGridCellAttr *attr, int rowOrCol); | |
299 | wxGridCellAttr *GetAttr(int rowOrCol) const; | |
300 | void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ); | |
301 | ||
302 | private: | |
303 | wxArrayInt m_rowsOrCols; | |
304 | wxArrayAttrs m_attrs; | |
305 | }; | |
306 | ||
307 | // NB: this is just a wrapper around 3 objects: one which stores cell | |
308 | // attributes, and 2 others for row/col ones | |
309 | class WXDLLIMPEXP_ADV wxGridCellAttrProviderData | |
310 | { | |
311 | public: | |
312 | wxGridCellAttrData m_cellAttrs; | |
313 | wxGridRowOrColAttrData m_rowAttrs, | |
314 | m_colAttrs; | |
315 | }; | |
316 | ||
317 | ||
318 | // ---------------------------------------------------------------------------- | |
319 | // data structures used for the data type registry | |
320 | // ---------------------------------------------------------------------------- | |
321 | ||
322 | struct wxGridDataTypeInfo | |
323 | { | |
324 | wxGridDataTypeInfo(const wxString& typeName, | |
325 | wxGridCellRenderer* renderer, | |
326 | wxGridCellEditor* editor) | |
327 | : m_typeName(typeName), m_renderer(renderer), m_editor(editor) | |
328 | {} | |
329 | ||
330 | ~wxGridDataTypeInfo() | |
331 | { | |
332 | wxSafeDecRef(m_renderer); | |
333 | wxSafeDecRef(m_editor); | |
334 | } | |
335 | ||
336 | wxString m_typeName; | |
337 | wxGridCellRenderer* m_renderer; | |
338 | wxGridCellEditor* m_editor; | |
339 | ||
340 | DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo) | |
341 | }; | |
342 | ||
343 | ||
344 | WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo*, wxGridDataTypeInfoArray, | |
345 | class WXDLLIMPEXP_ADV); | |
346 | ||
347 | ||
348 | class WXDLLIMPEXP_ADV wxGridTypeRegistry | |
349 | { | |
350 | public: | |
351 | wxGridTypeRegistry() {} | |
352 | ~wxGridTypeRegistry(); | |
353 | ||
354 | void RegisterDataType(const wxString& typeName, | |
355 | wxGridCellRenderer* renderer, | |
356 | wxGridCellEditor* editor); | |
357 | ||
358 | // find one of already registered data types | |
359 | int FindRegisteredDataType(const wxString& typeName); | |
360 | ||
361 | // try to FindRegisteredDataType(), if this fails and typeName is one of | |
362 | // standard typenames, register it and return its index | |
363 | int FindDataType(const wxString& typeName); | |
364 | ||
365 | // try to FindDataType(), if it fails see if it is not one of already | |
366 | // registered data types with some params in which case clone the | |
367 | // registered data type and set params for it | |
368 | int FindOrCloneDataType(const wxString& typeName); | |
369 | ||
370 | wxGridCellRenderer* GetRenderer(int index); | |
371 | wxGridCellEditor* GetEditor(int index); | |
372 | ||
373 | private: | |
374 | wxGridDataTypeInfoArray m_typeinfo; | |
375 | }; | |
376 | ||
377 | ||
378 | // ---------------------------------------------------------------------------- | |
379 | // conditional compilation | |
380 | // ---------------------------------------------------------------------------- | |
381 | ||
382 | #ifndef WXGRID_DRAW_LINES | |
383 | #define WXGRID_DRAW_LINES 1 | |
384 | #endif | |
385 | ||
386 | // ---------------------------------------------------------------------------- | |
387 | // globals | |
388 | // ---------------------------------------------------------------------------- | |
389 | ||
390 | //#define DEBUG_ATTR_CACHE | |
391 | #ifdef DEBUG_ATTR_CACHE | |
392 | static size_t gs_nAttrCacheHits = 0; | |
393 | static size_t gs_nAttrCacheMisses = 0; | |
394 | #endif | |
395 | ||
396 | // ---------------------------------------------------------------------------- | |
397 | // constants | |
398 | // ---------------------------------------------------------------------------- | |
399 | ||
400 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); | |
401 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); | |
402 | ||
403 | // scroll line size | |
404 | // TODO: this doesn't work at all, grid cells have different sizes and approx | |
405 | // calculations don't work as because of the size mismatch scrollbars | |
406 | // sometimes fail to be shown when they should be or vice versa | |
407 | // | |
408 | // The scroll bars may be a little flakey once in a while, but that is | |
409 | // surely much less horrible than having scroll lines of only 1!!! | |
410 | // -- Robin | |
411 | // | |
412 | // Well, it's still seriously broken so it might be better but needs | |
413 | // fixing anyhow | |
414 | // -- Vadim | |
415 | static const size_t GRID_SCROLL_LINE_X = 15; // 1; | |
416 | static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X; | |
417 | ||
418 | // the size of hash tables used a bit everywhere (the max number of elements | |
419 | // in these hash tables is the number of rows/columns) | |
420 | static const int GRID_HASH_SIZE = 100; | |
421 | ||
422 | #if 0 | |
423 | // ---------------------------------------------------------------------------- | |
424 | // private functions | |
425 | // ---------------------------------------------------------------------------- | |
426 | ||
427 | static inline int GetScrollX(int x) | |
428 | { | |
429 | return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X; | |
430 | } | |
431 | ||
432 | static inline int GetScrollY(int y) | |
433 | { | |
434 | return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y; | |
435 | } | |
436 | #endif | |
437 | ||
438 | // ============================================================================ | |
439 | // implementation | |
440 | // ============================================================================ | |
441 | ||
442 | // ---------------------------------------------------------------------------- | |
443 | // wxGridCellEditor | |
444 | // ---------------------------------------------------------------------------- | |
445 | ||
446 | wxGridCellEditor::wxGridCellEditor() | |
447 | { | |
448 | m_control = NULL; | |
449 | m_attr = NULL; | |
450 | } | |
451 | ||
452 | wxGridCellEditor::~wxGridCellEditor() | |
453 | { | |
454 | Destroy(); | |
455 | } | |
456 | ||
457 | void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent), | |
458 | wxWindowID WXUNUSED(id), | |
459 | wxEvtHandler* evtHandler) | |
460 | { | |
461 | if ( evtHandler ) | |
462 | m_control->PushEventHandler(evtHandler); | |
463 | } | |
464 | ||
465 | void wxGridCellEditor::PaintBackground(const wxRect& rectCell, | |
466 | wxGridCellAttr *attr) | |
467 | { | |
468 | // erase the background because we might not fill the cell | |
469 | wxClientDC dc(m_control->GetParent()); | |
470 | wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow); | |
471 | if (gridWindow) | |
472 | gridWindow->GetOwner()->PrepareDC(dc); | |
473 | ||
474 | dc.SetPen(*wxTRANSPARENT_PEN); | |
475 | dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID)); | |
476 | dc.DrawRectangle(rectCell); | |
477 | ||
478 | // redraw the control we just painted over | |
479 | m_control->Refresh(); | |
480 | } | |
481 | ||
482 | void wxGridCellEditor::Destroy() | |
483 | { | |
484 | if (m_control) | |
485 | { | |
486 | m_control->PopEventHandler( true /* delete it*/ ); | |
487 | ||
488 | m_control->Destroy(); | |
489 | m_control = NULL; | |
490 | } | |
491 | } | |
492 | ||
493 | void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr) | |
494 | { | |
495 | wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); | |
496 | ||
497 | m_control->Show(show); | |
498 | ||
499 | if ( show ) | |
500 | { | |
501 | // set the colours/fonts if we have any | |
502 | if ( attr ) | |
503 | { | |
504 | m_colFgOld = m_control->GetForegroundColour(); | |
505 | m_control->SetForegroundColour(attr->GetTextColour()); | |
506 | ||
507 | m_colBgOld = m_control->GetBackgroundColour(); | |
508 | m_control->SetBackgroundColour(attr->GetBackgroundColour()); | |
509 | ||
510 | // Workaround for GTK+1 font setting problem on some platforms | |
511 | #if !defined(__WXGTK__) || defined(__WXGTK20__) | |
512 | m_fontOld = m_control->GetFont(); | |
513 | m_control->SetFont(attr->GetFont()); | |
514 | #endif | |
515 | ||
516 | // can't do anything more in the base class version, the other | |
517 | // attributes may only be used by the derived classes | |
518 | } | |
519 | } | |
520 | else | |
521 | { | |
522 | // restore the standard colours fonts | |
523 | if ( m_colFgOld.Ok() ) | |
524 | { | |
525 | m_control->SetForegroundColour(m_colFgOld); | |
526 | m_colFgOld = wxNullColour; | |
527 | } | |
528 | ||
529 | if ( m_colBgOld.Ok() ) | |
530 | { | |
531 | m_control->SetBackgroundColour(m_colBgOld); | |
532 | m_colBgOld = wxNullColour; | |
533 | } | |
534 | ||
535 | // Workaround for GTK+1 font setting problem on some platforms | |
536 | #if !defined(__WXGTK__) || defined(__WXGTK20__) | |
537 | if ( m_fontOld.Ok() ) | |
538 | { | |
539 | m_control->SetFont(m_fontOld); | |
540 | m_fontOld = wxNullFont; | |
541 | } | |
542 | #endif | |
543 | } | |
544 | } | |
545 | ||
546 | void wxGridCellEditor::SetSize(const wxRect& rect) | |
547 | { | |
548 | wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); | |
549 | ||
550 | m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); | |
551 | } | |
552 | ||
553 | void wxGridCellEditor::HandleReturn(wxKeyEvent& event) | |
554 | { | |
555 | event.Skip(); | |
556 | } | |
557 | ||
558 | bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event) | |
559 | { | |
560 | bool ctrl = event.ControlDown(); | |
561 | bool alt = event.AltDown(); | |
562 | ||
563 | #ifdef __WXMAC__ | |
564 | // On the Mac the Alt key is more like shift and is used for entry of | |
565 | // valid characters, so check for Ctrl and Meta instead. | |
566 | alt = event.MetaDown(); | |
567 | #endif | |
568 | ||
569 | // Assume it's not a valid char if ctrl or alt is down, but if both are | |
570 | // down then it may be because of an AltGr key combination, so let them | |
571 | // through in that case. | |
572 | if ((ctrl || alt) && !(ctrl && alt)) | |
573 | return false; | |
574 | ||
575 | int key = 0; | |
576 | bool keyOk = true; | |
577 | ||
578 | #ifdef __WXGTK20__ | |
579 | // If it's a F-Key or other special key then it shouldn't start the | |
580 | // editor. | |
581 | if (event.GetKeyCode() >= WXK_START) | |
582 | return false; | |
583 | #endif | |
584 | #if wxUSE_UNICODE | |
585 | // if the unicode key code is not really a unicode character (it may | |
586 | // be a function key or etc., the platforms appear to always give us a | |
587 | // small value in this case) then fallback to the ASCII key code but | |
588 | // don't do anything for function keys or etc. | |
589 | key = event.GetUnicodeKey(); | |
590 | if (key <= 127) | |
591 | { | |
592 | key = event.GetKeyCode(); | |
593 | keyOk = (key <= 127); | |
594 | } | |
595 | #else | |
596 | key = event.GetKeyCode(); | |
597 | keyOk = (key <= 255); | |
598 | #endif | |
599 | ||
600 | return keyOk; | |
601 | } | |
602 | ||
603 | void wxGridCellEditor::StartingKey(wxKeyEvent& event) | |
604 | { | |
605 | event.Skip(); | |
606 | } | |
607 | ||
608 | void wxGridCellEditor::StartingClick() | |
609 | { | |
610 | } | |
611 | ||
612 | #if wxUSE_TEXTCTRL | |
613 | ||
614 | // ---------------------------------------------------------------------------- | |
615 | // wxGridCellTextEditor | |
616 | // ---------------------------------------------------------------------------- | |
617 | ||
618 | wxGridCellTextEditor::wxGridCellTextEditor() | |
619 | { | |
620 | m_maxChars = 0; | |
621 | } | |
622 | ||
623 | void wxGridCellTextEditor::Create(wxWindow* parent, | |
624 | wxWindowID id, | |
625 | wxEvtHandler* evtHandler) | |
626 | { | |
627 | m_control = new wxTextCtrl(parent, id, wxEmptyString, | |
628 | wxDefaultPosition, wxDefaultSize | |
629 | #if defined(__WXMSW__) | |
630 | , wxTE_PROCESS_TAB | wxTE_AUTO_SCROLL | wxNO_BORDER | |
631 | #endif | |
632 | ); | |
633 | ||
634 | // set max length allowed in the textctrl, if the parameter was set | |
635 | if (m_maxChars != 0) | |
636 | { | |
637 | ((wxTextCtrl*)m_control)->SetMaxLength(m_maxChars); | |
638 | } | |
639 | ||
640 | wxGridCellEditor::Create(parent, id, evtHandler); | |
641 | } | |
642 | ||
643 | void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell), | |
644 | wxGridCellAttr * WXUNUSED(attr)) | |
645 | { | |
646 | // as we fill the entire client area, | |
647 | // don't do anything here to minimize flicker | |
648 | } | |
649 | ||
650 | void wxGridCellTextEditor::SetSize(const wxRect& rectOrig) | |
651 | { | |
652 | wxRect rect(rectOrig); | |
653 | ||
654 | // Make the edit control large enough to allow for internal margins | |
655 | // | |
656 | // TODO: remove this if the text ctrl sizing is improved esp. for unix | |
657 | // | |
658 | #if defined(__WXGTK__) | |
659 | if (rect.x != 0) | |
660 | { | |
661 | rect.x += 1; | |
662 | rect.y += 1; | |
663 | rect.width -= 1; | |
664 | rect.height -= 1; | |
665 | } | |
666 | #elif defined(__WXMSW__) | |
667 | if ( rect.x == 0 ) | |
668 | rect.x += 2; | |
669 | else | |
670 | rect.x += 3; | |
671 | ||
672 | if ( rect.y == 0 ) | |
673 | rect.y += 2; | |
674 | else | |
675 | rect.y += 3; | |
676 | ||
677 | rect.width -= 2; | |
678 | rect.height -= 2; | |
679 | #else | |
680 | int extra_x = ( rect.x > 2 ) ? 2 : 1; | |
681 | int extra_y = ( rect.y > 2 ) ? 2 : 1; | |
682 | ||
683 | #if defined(__WXMOTIF__) | |
684 | extra_x *= 2; | |
685 | extra_y *= 2; | |
686 | #endif | |
687 | ||
688 | rect.SetLeft( wxMax(0, rect.x - extra_x) ); | |
689 | rect.SetTop( wxMax(0, rect.y - extra_y) ); | |
690 | rect.SetRight( rect.GetRight() + 2 * extra_x ); | |
691 | rect.SetBottom( rect.GetBottom() + 2 * extra_y ); | |
692 | #endif | |
693 | ||
694 | wxGridCellEditor::SetSize(rect); | |
695 | } | |
696 | ||
697 | void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid) | |
698 | { | |
699 | wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); | |
700 | ||
701 | m_startValue = grid->GetTable()->GetValue(row, col); | |
702 | ||
703 | DoBeginEdit(m_startValue); | |
704 | } | |
705 | ||
706 | void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue) | |
707 | { | |
708 | Text()->SetValue(startValue); | |
709 | Text()->SetInsertionPointEnd(); | |
710 | Text()->SetSelection(-1, -1); | |
711 | Text()->SetFocus(); | |
712 | } | |
713 | ||
714 | bool wxGridCellTextEditor::EndEdit(int row, int col, wxGrid* grid) | |
715 | { | |
716 | wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); | |
717 | ||
718 | bool changed = false; | |
719 | wxString value = Text()->GetValue(); | |
720 | if (value != m_startValue) | |
721 | changed = true; | |
722 | ||
723 | if (changed) | |
724 | grid->GetTable()->SetValue(row, col, value); | |
725 | ||
726 | m_startValue = wxEmptyString; | |
727 | ||
728 | // No point in setting the text of the hidden control | |
729 | //Text()->SetValue(m_startValue); | |
730 | ||
731 | return changed; | |
732 | } | |
733 | ||
734 | void wxGridCellTextEditor::Reset() | |
735 | { | |
736 | wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); | |
737 | ||
738 | DoReset(m_startValue); | |
739 | } | |
740 | ||
741 | void wxGridCellTextEditor::DoReset(const wxString& startValue) | |
742 | { | |
743 | Text()->SetValue(startValue); | |
744 | Text()->SetInsertionPointEnd(); | |
745 | } | |
746 | ||
747 | bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event) | |
748 | { | |
749 | return wxGridCellEditor::IsAcceptedKey(event); | |
750 | } | |
751 | ||
752 | void wxGridCellTextEditor::StartingKey(wxKeyEvent& event) | |
753 | { | |
754 | // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no | |
755 | // longer an appropriate way to get the character into the text control. | |
756 | // Do it ourselves instead. We know that if we get this far that we have | |
757 | // a valid character, so not a whole lot of testing needs to be done. | |
758 | ||
759 | wxTextCtrl* tc = Text(); | |
760 | wxChar ch; | |
761 | long pos; | |
762 | ||
763 | #if wxUSE_UNICODE | |
764 | ch = event.GetUnicodeKey(); | |
765 | if (ch <= 127) | |
766 | ch = (wxChar)event.GetKeyCode(); | |
767 | #else | |
768 | ch = (wxChar)event.GetKeyCode(); | |
769 | #endif | |
770 | ||
771 | switch (ch) | |
772 | { | |
773 | case WXK_DELETE: | |
774 | // delete the character at the cursor | |
775 | pos = tc->GetInsertionPoint(); | |
776 | if (pos < tc->GetLastPosition()) | |
777 | tc->Remove(pos, pos + 1); | |
778 | break; | |
779 | ||
780 | case WXK_BACK: | |
781 | // delete the character before the cursor | |
782 | pos = tc->GetInsertionPoint(); | |
783 | if (pos > 0) | |
784 | tc->Remove(pos - 1, pos); | |
785 | break; | |
786 | ||
787 | default: | |
788 | tc->WriteText(ch); | |
789 | break; | |
790 | } | |
791 | } | |
792 | ||
793 | void wxGridCellTextEditor::HandleReturn( wxKeyEvent& | |
794 | WXUNUSED_GTK(WXUNUSED_MOTIF(event)) ) | |
795 | { | |
796 | #if defined(__WXMOTIF__) || defined(__WXGTK__) | |
797 | // wxMotif needs a little extra help... | |
798 | size_t pos = (size_t)( Text()->GetInsertionPoint() ); | |
799 | wxString s( Text()->GetValue() ); | |
800 | s = s.Left(pos) + wxT("\n") + s.Mid(pos); | |
801 | Text()->SetValue(s); | |
802 | Text()->SetInsertionPoint( pos ); | |
803 | #else | |
804 | // the other ports can handle a Return key press | |
805 | // | |
806 | event.Skip(); | |
807 | #endif | |
808 | } | |
809 | ||
810 | void wxGridCellTextEditor::SetParameters(const wxString& params) | |
811 | { | |
812 | if ( !params ) | |
813 | { | |
814 | // reset to default | |
815 | m_maxChars = 0; | |
816 | } | |
817 | else | |
818 | { | |
819 | long tmp; | |
820 | if ( params.ToLong(&tmp) ) | |
821 | { | |
822 | m_maxChars = (size_t)tmp; | |
823 | } | |
824 | else | |
825 | { | |
826 | wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str() ); | |
827 | } | |
828 | } | |
829 | } | |
830 | ||
831 | // return the value in the text control | |
832 | wxString wxGridCellTextEditor::GetValue() const | |
833 | { | |
834 | return Text()->GetValue(); | |
835 | } | |
836 | ||
837 | // ---------------------------------------------------------------------------- | |
838 | // wxGridCellNumberEditor | |
839 | // ---------------------------------------------------------------------------- | |
840 | ||
841 | wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max) | |
842 | { | |
843 | m_min = min; | |
844 | m_max = max; | |
845 | } | |
846 | ||
847 | void wxGridCellNumberEditor::Create(wxWindow* parent, | |
848 | wxWindowID id, | |
849 | wxEvtHandler* evtHandler) | |
850 | { | |
851 | #if wxUSE_SPINCTRL | |
852 | if ( HasRange() ) | |
853 | { | |
854 | // create a spin ctrl | |
855 | m_control = new wxSpinCtrl(parent, wxID_ANY, wxEmptyString, | |
856 | wxDefaultPosition, wxDefaultSize, | |
857 | wxSP_ARROW_KEYS, | |
858 | m_min, m_max); | |
859 | ||
860 | wxGridCellEditor::Create(parent, id, evtHandler); | |
861 | } | |
862 | else | |
863 | #endif | |
864 | { | |
865 | // just a text control | |
866 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
867 | ||
868 | #if wxUSE_VALIDATORS | |
869 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); | |
870 | #endif | |
871 | } | |
872 | } | |
873 | ||
874 | void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid) | |
875 | { | |
876 | // first get the value | |
877 | wxGridTableBase *table = grid->GetTable(); | |
878 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
879 | { | |
880 | m_valueOld = table->GetValueAsLong(row, col); | |
881 | } | |
882 | else | |
883 | { | |
884 | m_valueOld = 0; | |
885 | wxString sValue = table->GetValue(row, col); | |
886 | if (! sValue.ToLong(&m_valueOld) && ! sValue.empty()) | |
887 | { | |
888 | wxFAIL_MSG( _T("this cell doesn't have numeric value") ); | |
889 | return; | |
890 | } | |
891 | } | |
892 | ||
893 | #if wxUSE_SPINCTRL | |
894 | if ( HasRange() ) | |
895 | { | |
896 | Spin()->SetValue((int)m_valueOld); | |
897 | Spin()->SetFocus(); | |
898 | } | |
899 | else | |
900 | #endif | |
901 | { | |
902 | DoBeginEdit(GetString()); | |
903 | } | |
904 | } | |
905 | ||
906 | bool wxGridCellNumberEditor::EndEdit(int row, int col, | |
907 | wxGrid* grid) | |
908 | { | |
909 | bool changed; | |
910 | long value = 0; | |
911 | wxString text; | |
912 | ||
913 | #if wxUSE_SPINCTRL | |
914 | if ( HasRange() ) | |
915 | { | |
916 | value = Spin()->GetValue(); | |
917 | changed = value != m_valueOld; | |
918 | if (changed) | |
919 | text = wxString::Format(wxT("%ld"), value); | |
920 | } | |
921 | else | |
922 | #endif | |
923 | { | |
924 | text = Text()->GetValue(); | |
925 | changed = (text.empty() || text.ToLong(&value)) && (value != m_valueOld); | |
926 | } | |
927 | ||
928 | if ( changed ) | |
929 | { | |
930 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER)) | |
931 | grid->GetTable()->SetValueAsLong(row, col, value); | |
932 | else | |
933 | grid->GetTable()->SetValue(row, col, text); | |
934 | } | |
935 | ||
936 | return changed; | |
937 | } | |
938 | ||
939 | void wxGridCellNumberEditor::Reset() | |
940 | { | |
941 | #if wxUSE_SPINCTRL | |
942 | if ( HasRange() ) | |
943 | { | |
944 | Spin()->SetValue((int)m_valueOld); | |
945 | } | |
946 | else | |
947 | #endif | |
948 | { | |
949 | DoReset(GetString()); | |
950 | } | |
951 | } | |
952 | ||
953 | bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event) | |
954 | { | |
955 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
956 | { | |
957 | int keycode = event.GetKeyCode(); | |
958 | if ( (keycode < 128) && | |
959 | (wxIsdigit(keycode) || keycode == '+' || keycode == '-')) | |
960 | { | |
961 | return true; | |
962 | } | |
963 | } | |
964 | ||
965 | return false; | |
966 | } | |
967 | ||
968 | void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event) | |
969 | { | |
970 | int keycode = event.GetKeyCode(); | |
971 | if ( !HasRange() ) | |
972 | { | |
973 | if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-') | |
974 | { | |
975 | wxGridCellTextEditor::StartingKey(event); | |
976 | ||
977 | // skip Skip() below | |
978 | return; | |
979 | } | |
980 | } | |
981 | #if wxUSE_SPINCTRL | |
982 | else | |
983 | { | |
984 | if ( wxIsdigit(keycode) ) | |
985 | { | |
986 | wxSpinCtrl* spin = (wxSpinCtrl*)m_control; | |
987 | spin->SetValue(keycode - '0'); | |
988 | spin->SetSelection(1,1); | |
989 | return; | |
990 | } | |
991 | } | |
992 | #endif | |
993 | ||
994 | event.Skip(); | |
995 | } | |
996 | ||
997 | void wxGridCellNumberEditor::SetParameters(const wxString& params) | |
998 | { | |
999 | if ( !params ) | |
1000 | { | |
1001 | // reset to default | |
1002 | m_min = | |
1003 | m_max = -1; | |
1004 | } | |
1005 | else | |
1006 | { | |
1007 | long tmp; | |
1008 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
1009 | { | |
1010 | m_min = (int)tmp; | |
1011 | ||
1012 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
1013 | { | |
1014 | m_max = (int)tmp; | |
1015 | ||
1016 | // skip the error message below | |
1017 | return; | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str()); | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | // return the value in the spin control if it is there (the text control otherwise) | |
1026 | wxString wxGridCellNumberEditor::GetValue() const | |
1027 | { | |
1028 | wxString s; | |
1029 | ||
1030 | #if wxUSE_SPINCTRL | |
1031 | if ( HasRange() ) | |
1032 | { | |
1033 | long value = Spin()->GetValue(); | |
1034 | s.Printf(wxT("%ld"), value); | |
1035 | } | |
1036 | else | |
1037 | #endif | |
1038 | { | |
1039 | s = Text()->GetValue(); | |
1040 | } | |
1041 | ||
1042 | return s; | |
1043 | } | |
1044 | ||
1045 | // ---------------------------------------------------------------------------- | |
1046 | // wxGridCellFloatEditor | |
1047 | // ---------------------------------------------------------------------------- | |
1048 | ||
1049 | wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision) | |
1050 | { | |
1051 | m_width = width; | |
1052 | m_precision = precision; | |
1053 | } | |
1054 | ||
1055 | void wxGridCellFloatEditor::Create(wxWindow* parent, | |
1056 | wxWindowID id, | |
1057 | wxEvtHandler* evtHandler) | |
1058 | { | |
1059 | wxGridCellTextEditor::Create(parent, id, evtHandler); | |
1060 | ||
1061 | #if wxUSE_VALIDATORS | |
1062 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); | |
1063 | #endif | |
1064 | } | |
1065 | ||
1066 | void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1067 | { | |
1068 | // first get the value | |
1069 | wxGridTableBase *table = grid->GetTable(); | |
1070 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
1071 | { | |
1072 | m_valueOld = table->GetValueAsDouble(row, col); | |
1073 | } | |
1074 | else | |
1075 | { | |
1076 | m_valueOld = 0.0; | |
1077 | wxString sValue = table->GetValue(row, col); | |
1078 | if (! sValue.ToDouble(&m_valueOld) && ! sValue.empty()) | |
1079 | { | |
1080 | wxFAIL_MSG( _T("this cell doesn't have float value") ); | |
1081 | return; | |
1082 | } | |
1083 | } | |
1084 | ||
1085 | DoBeginEdit(GetString()); | |
1086 | } | |
1087 | ||
1088 | bool wxGridCellFloatEditor::EndEdit(int row, int col, | |
1089 | wxGrid* grid) | |
1090 | { | |
1091 | double value = 0.0; | |
1092 | wxString text(Text()->GetValue()); | |
1093 | ||
1094 | if ( (text.empty() || text.ToDouble(&value)) && | |
1095 | !wxIsSameDouble(value, m_valueOld) ) | |
1096 | { | |
1097 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT)) | |
1098 | grid->GetTable()->SetValueAsDouble(row, col, value); | |
1099 | else | |
1100 | grid->GetTable()->SetValue(row, col, text); | |
1101 | ||
1102 | return true; | |
1103 | } | |
1104 | ||
1105 | return false; | |
1106 | } | |
1107 | ||
1108 | void wxGridCellFloatEditor::Reset() | |
1109 | { | |
1110 | DoReset(GetString()); | |
1111 | } | |
1112 | ||
1113 | void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event) | |
1114 | { | |
1115 | int keycode = event.GetKeyCode(); | |
1116 | char tmpbuf[2]; | |
1117 | tmpbuf[0] = (char) keycode; | |
1118 | tmpbuf[1] = '\0'; | |
1119 | wxString strbuf(tmpbuf, *wxConvCurrent); | |
1120 | ||
1121 | #if wxUSE_INTL | |
1122 | bool is_decimal_point = ( strbuf == | |
1123 | wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER) ); | |
1124 | #else | |
1125 | bool is_decimal_point = ( strbuf == _T(".") ); | |
1126 | #endif | |
1127 | ||
1128 | if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-' | |
1129 | || is_decimal_point ) | |
1130 | { | |
1131 | wxGridCellTextEditor::StartingKey(event); | |
1132 | ||
1133 | // skip Skip() below | |
1134 | return; | |
1135 | } | |
1136 | ||
1137 | event.Skip(); | |
1138 | } | |
1139 | ||
1140 | void wxGridCellFloatEditor::SetParameters(const wxString& params) | |
1141 | { | |
1142 | if ( !params ) | |
1143 | { | |
1144 | // reset to default | |
1145 | m_width = | |
1146 | m_precision = -1; | |
1147 | } | |
1148 | else | |
1149 | { | |
1150 | long tmp; | |
1151 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) | |
1152 | { | |
1153 | m_width = (int)tmp; | |
1154 | ||
1155 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) | |
1156 | { | |
1157 | m_precision = (int)tmp; | |
1158 | ||
1159 | // skip the error message below | |
1160 | return; | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str()); | |
1165 | } | |
1166 | } | |
1167 | ||
1168 | wxString wxGridCellFloatEditor::GetString() const | |
1169 | { | |
1170 | wxString fmt; | |
1171 | if ( m_precision == -1 && m_width != -1) | |
1172 | { | |
1173 | // default precision | |
1174 | fmt.Printf(_T("%%%d.f"), m_width); | |
1175 | } | |
1176 | else if ( m_precision != -1 && m_width == -1) | |
1177 | { | |
1178 | // default width | |
1179 | fmt.Printf(_T("%%.%df"), m_precision); | |
1180 | } | |
1181 | else if ( m_precision != -1 && m_width != -1 ) | |
1182 | { | |
1183 | fmt.Printf(_T("%%%d.%df"), m_width, m_precision); | |
1184 | } | |
1185 | else | |
1186 | { | |
1187 | // default width/precision | |
1188 | fmt = _T("%f"); | |
1189 | } | |
1190 | ||
1191 | return wxString::Format(fmt, m_valueOld); | |
1192 | } | |
1193 | ||
1194 | bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event) | |
1195 | { | |
1196 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
1197 | { | |
1198 | const int keycode = event.GetKeyCode(); | |
1199 | if ( isascii(keycode) ) | |
1200 | { | |
1201 | char tmpbuf[2]; | |
1202 | tmpbuf[0] = (char) keycode; | |
1203 | tmpbuf[1] = '\0'; | |
1204 | wxString strbuf(tmpbuf, *wxConvCurrent); | |
1205 | ||
1206 | #if wxUSE_INTL | |
1207 | const wxString decimalPoint = | |
1208 | wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER); | |
1209 | #else | |
1210 | const wxString decimalPoint(_T('.')); | |
1211 | #endif | |
1212 | ||
1213 | // accept digits, 'e' as in '1e+6', also '-', '+', and '.' | |
1214 | if ( wxIsdigit(keycode) || | |
1215 | tolower(keycode) == 'e' || | |
1216 | keycode == decimalPoint || | |
1217 | keycode == '+' || | |
1218 | keycode == '-' ) | |
1219 | { | |
1220 | return true; | |
1221 | } | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | return false; | |
1226 | } | |
1227 | ||
1228 | #endif // wxUSE_TEXTCTRL | |
1229 | ||
1230 | #if wxUSE_CHECKBOX | |
1231 | ||
1232 | // ---------------------------------------------------------------------------- | |
1233 | // wxGridCellBoolEditor | |
1234 | // ---------------------------------------------------------------------------- | |
1235 | ||
1236 | // the default values for GetValue() | |
1237 | wxString wxGridCellBoolEditor::ms_stringValues[2] = { _T("1"), _T("") }; | |
1238 | ||
1239 | void wxGridCellBoolEditor::Create(wxWindow* parent, | |
1240 | wxWindowID id, | |
1241 | wxEvtHandler* evtHandler) | |
1242 | { | |
1243 | m_control = new wxCheckBox(parent, id, wxEmptyString, | |
1244 | wxDefaultPosition, wxDefaultSize, | |
1245 | wxNO_BORDER); | |
1246 | ||
1247 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1248 | } | |
1249 | ||
1250 | void wxGridCellBoolEditor::SetSize(const wxRect& r) | |
1251 | { | |
1252 | bool resize = false; | |
1253 | wxSize size = m_control->GetSize(); | |
1254 | wxCoord minSize = wxMin(r.width, r.height); | |
1255 | ||
1256 | // check if the checkbox is not too big/small for this cell | |
1257 | wxSize sizeBest = m_control->GetBestSize(); | |
1258 | if ( !(size == sizeBest) ) | |
1259 | { | |
1260 | // reset to default size if it had been made smaller | |
1261 | size = sizeBest; | |
1262 | ||
1263 | resize = true; | |
1264 | } | |
1265 | ||
1266 | if ( size.x >= minSize || size.y >= minSize ) | |
1267 | { | |
1268 | // leave 1 pixel margin | |
1269 | size.x = size.y = minSize - 2; | |
1270 | ||
1271 | resize = true; | |
1272 | } | |
1273 | ||
1274 | if ( resize ) | |
1275 | { | |
1276 | m_control->SetSize(size); | |
1277 | } | |
1278 | ||
1279 | // position it in the centre of the rectangle (TODO: support alignment?) | |
1280 | ||
1281 | #if defined(__WXGTK__) || defined (__WXMOTIF__) | |
1282 | // the checkbox without label still has some space to the right in wxGTK, | |
1283 | // so shift it to the right | |
1284 | size.x -= 8; | |
1285 | #elif defined(__WXMSW__) | |
1286 | // here too, but in other way | |
1287 | size.x += 1; | |
1288 | size.y -= 2; | |
1289 | #endif | |
1290 | ||
1291 | int hAlign = wxALIGN_CENTRE; | |
1292 | int vAlign = wxALIGN_CENTRE; | |
1293 | if (GetCellAttr()) | |
1294 | GetCellAttr()->GetAlignment(& hAlign, & vAlign); | |
1295 | ||
1296 | int x = 0, y = 0; | |
1297 | if (hAlign == wxALIGN_LEFT) | |
1298 | { | |
1299 | x = r.x + 2; | |
1300 | ||
1301 | #ifdef __WXMSW__ | |
1302 | x += 2; | |
1303 | #endif | |
1304 | ||
1305 | y = r.y + r.height / 2 - size.y / 2; | |
1306 | } | |
1307 | else if (hAlign == wxALIGN_RIGHT) | |
1308 | { | |
1309 | x = r.x + r.width - size.x - 2; | |
1310 | y = r.y + r.height / 2 - size.y / 2; | |
1311 | } | |
1312 | else if (hAlign == wxALIGN_CENTRE) | |
1313 | { | |
1314 | x = r.x + r.width / 2 - size.x / 2; | |
1315 | y = r.y + r.height / 2 - size.y / 2; | |
1316 | } | |
1317 | ||
1318 | m_control->Move(x, y); | |
1319 | } | |
1320 | ||
1321 | void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr) | |
1322 | { | |
1323 | m_control->Show(show); | |
1324 | ||
1325 | if ( show ) | |
1326 | { | |
1327 | wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY; | |
1328 | CBox()->SetBackgroundColour(colBg); | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1333 | { | |
1334 | wxASSERT_MSG(m_control, | |
1335 | wxT("The wxGridCellEditor must be created first!")); | |
1336 | ||
1337 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) | |
1338 | { | |
1339 | m_startValue = grid->GetTable()->GetValueAsBool(row, col); | |
1340 | } | |
1341 | else | |
1342 | { | |
1343 | wxString cellval( grid->GetTable()->GetValue(row, col) ); | |
1344 | m_startValue = !( !cellval || (cellval == wxT("0")) ); | |
1345 | } | |
1346 | ||
1347 | CBox()->SetValue(m_startValue); | |
1348 | CBox()->SetFocus(); | |
1349 | } | |
1350 | ||
1351 | bool wxGridCellBoolEditor::EndEdit(int row, int col, | |
1352 | wxGrid* grid) | |
1353 | { | |
1354 | wxASSERT_MSG(m_control, | |
1355 | wxT("The wxGridCellEditor must be created first!")); | |
1356 | ||
1357 | bool changed = false; | |
1358 | bool value = CBox()->GetValue(); | |
1359 | if ( value != m_startValue ) | |
1360 | changed = true; | |
1361 | ||
1362 | if ( changed ) | |
1363 | { | |
1364 | wxGridTableBase * const table = grid->GetTable(); | |
1365 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) | |
1366 | table->SetValueAsBool(row, col, value); | |
1367 | else | |
1368 | table->SetValue(row, col, GetValue()); | |
1369 | } | |
1370 | ||
1371 | return changed; | |
1372 | } | |
1373 | ||
1374 | void wxGridCellBoolEditor::Reset() | |
1375 | { | |
1376 | wxASSERT_MSG(m_control, | |
1377 | wxT("The wxGridCellEditor must be created first!")); | |
1378 | ||
1379 | CBox()->SetValue(m_startValue); | |
1380 | } | |
1381 | ||
1382 | void wxGridCellBoolEditor::StartingClick() | |
1383 | { | |
1384 | CBox()->SetValue(!CBox()->GetValue()); | |
1385 | } | |
1386 | ||
1387 | bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event) | |
1388 | { | |
1389 | if ( wxGridCellEditor::IsAcceptedKey(event) ) | |
1390 | { | |
1391 | int keycode = event.GetKeyCode(); | |
1392 | switch ( keycode ) | |
1393 | { | |
1394 | case WXK_SPACE: | |
1395 | case '+': | |
1396 | case '-': | |
1397 | return true; | |
1398 | } | |
1399 | } | |
1400 | ||
1401 | return false; | |
1402 | } | |
1403 | ||
1404 | void wxGridCellBoolEditor::StartingKey(wxKeyEvent& event) | |
1405 | { | |
1406 | int keycode = event.GetKeyCode(); | |
1407 | switch ( keycode ) | |
1408 | { | |
1409 | case WXK_SPACE: | |
1410 | CBox()->SetValue(!CBox()->GetValue()); | |
1411 | break; | |
1412 | ||
1413 | case '+': | |
1414 | CBox()->SetValue(true); | |
1415 | break; | |
1416 | ||
1417 | case '-': | |
1418 | CBox()->SetValue(false); | |
1419 | break; | |
1420 | } | |
1421 | } | |
1422 | ||
1423 | wxString wxGridCellBoolEditor::GetValue() const | |
1424 | { | |
1425 | return ms_stringValues[CBox()->GetValue()]; | |
1426 | } | |
1427 | ||
1428 | /* static */ void | |
1429 | wxGridCellBoolEditor::UseStringValues(const wxString& valueTrue, | |
1430 | const wxString& valueFalse) | |
1431 | { | |
1432 | ms_stringValues[false] = valueFalse; | |
1433 | ms_stringValues[true] = valueTrue; | |
1434 | } | |
1435 | ||
1436 | /* static */ bool | |
1437 | wxGridCellBoolEditor::IsTrueValue(const wxString& value) | |
1438 | { | |
1439 | return value == ms_stringValues[true]; | |
1440 | } | |
1441 | ||
1442 | #endif // wxUSE_CHECKBOX | |
1443 | ||
1444 | #if wxUSE_COMBOBOX | |
1445 | ||
1446 | // ---------------------------------------------------------------------------- | |
1447 | // wxGridCellChoiceEditor | |
1448 | // ---------------------------------------------------------------------------- | |
1449 | ||
1450 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString& choices, | |
1451 | bool allowOthers) | |
1452 | : m_choices(choices), | |
1453 | m_allowOthers(allowOthers) { } | |
1454 | ||
1455 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count, | |
1456 | const wxString choices[], | |
1457 | bool allowOthers) | |
1458 | : m_allowOthers(allowOthers) | |
1459 | { | |
1460 | if ( count ) | |
1461 | { | |
1462 | m_choices.Alloc(count); | |
1463 | for ( size_t n = 0; n < count; n++ ) | |
1464 | { | |
1465 | m_choices.Add(choices[n]); | |
1466 | } | |
1467 | } | |
1468 | } | |
1469 | ||
1470 | wxGridCellEditor *wxGridCellChoiceEditor::Clone() const | |
1471 | { | |
1472 | wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor; | |
1473 | editor->m_allowOthers = m_allowOthers; | |
1474 | editor->m_choices = m_choices; | |
1475 | ||
1476 | return editor; | |
1477 | } | |
1478 | ||
1479 | void wxGridCellChoiceEditor::Create(wxWindow* parent, | |
1480 | wxWindowID id, | |
1481 | wxEvtHandler* evtHandler) | |
1482 | { | |
1483 | m_control = new wxComboBox(parent, id, wxEmptyString, | |
1484 | wxDefaultPosition, wxDefaultSize, | |
1485 | m_choices, | |
1486 | m_allowOthers ? 0 : wxCB_READONLY); | |
1487 | ||
1488 | wxGridCellEditor::Create(parent, id, evtHandler); | |
1489 | } | |
1490 | ||
1491 | void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell, | |
1492 | wxGridCellAttr * attr) | |
1493 | { | |
1494 | // as we fill the entire client area, don't do anything here to minimize | |
1495 | // flicker | |
1496 | ||
1497 | // TODO: It doesn't actually fill the client area since the height of a | |
1498 | // combo always defaults to the standard. Until someone has time to | |
1499 | // figure out the right rectangle to paint, just do it the normal way. | |
1500 | wxGridCellEditor::PaintBackground(rectCell, attr); | |
1501 | } | |
1502 | ||
1503 | void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid) | |
1504 | { | |
1505 | wxASSERT_MSG(m_control, | |
1506 | wxT("The wxGridCellEditor must be created first!")); | |
1507 | ||
1508 | wxGridCellEditorEvtHandler* evtHandler = NULL; | |
1509 | if (m_control) | |
1510 | evtHandler = wxDynamicCast(m_control->GetEventHandler(), wxGridCellEditorEvtHandler); | |
1511 | ||
1512 | // Don't immediately end if we get a kill focus event within BeginEdit | |
1513 | if (evtHandler) | |
1514 | evtHandler->SetInSetFocus(true); | |
1515 | ||
1516 | m_startValue = grid->GetTable()->GetValue(row, col); | |
1517 | ||
1518 | if (m_allowOthers) | |
1519 | { | |
1520 | Combo()->SetValue(m_startValue); | |
1521 | } | |
1522 | else | |
1523 | { | |
1524 | // find the right position, or default to the first if not found | |
1525 | int pos = Combo()->FindString(m_startValue); | |
1526 | if (pos == wxNOT_FOUND) | |
1527 | pos = 0; | |
1528 | Combo()->SetSelection(pos); | |
1529 | } | |
1530 | ||
1531 | Combo()->SetInsertionPointEnd(); | |
1532 | Combo()->SetFocus(); | |
1533 | ||
1534 | if (evtHandler) | |
1535 | { | |
1536 | // When dropping down the menu, a kill focus event | |
1537 | // happens after this point, so we can't reset the flag yet. | |
1538 | #if !defined(__WXGTK20__) | |
1539 | evtHandler->SetInSetFocus(false); | |
1540 | #endif | |
1541 | } | |
1542 | } | |
1543 | ||
1544 | bool wxGridCellChoiceEditor::EndEdit(int row, int col, | |
1545 | wxGrid* grid) | |
1546 | { | |
1547 | wxString value = Combo()->GetValue(); | |
1548 | if ( value == m_startValue ) | |
1549 | return false; | |
1550 | ||
1551 | grid->GetTable()->SetValue(row, col, value); | |
1552 | ||
1553 | return true; | |
1554 | } | |
1555 | ||
1556 | void wxGridCellChoiceEditor::Reset() | |
1557 | { | |
1558 | Combo()->SetValue(m_startValue); | |
1559 | Combo()->SetInsertionPointEnd(); | |
1560 | } | |
1561 | ||
1562 | void wxGridCellChoiceEditor::SetParameters(const wxString& params) | |
1563 | { | |
1564 | if ( !params ) | |
1565 | { | |
1566 | // what can we do? | |
1567 | return; | |
1568 | } | |
1569 | ||
1570 | m_choices.Empty(); | |
1571 | ||
1572 | wxStringTokenizer tk(params, _T(',')); | |
1573 | while ( tk.HasMoreTokens() ) | |
1574 | { | |
1575 | m_choices.Add(tk.GetNextToken()); | |
1576 | } | |
1577 | } | |
1578 | ||
1579 | // return the value in the text control | |
1580 | wxString wxGridCellChoiceEditor::GetValue() const | |
1581 | { | |
1582 | return Combo()->GetValue(); | |
1583 | } | |
1584 | ||
1585 | #endif // wxUSE_COMBOBOX | |
1586 | ||
1587 | // ---------------------------------------------------------------------------- | |
1588 | // wxGridCellEditorEvtHandler | |
1589 | // ---------------------------------------------------------------------------- | |
1590 | ||
1591 | void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent& event) | |
1592 | { | |
1593 | // Don't disable the cell if we're just starting to edit it | |
1594 | if (m_inSetFocus) | |
1595 | return; | |
1596 | ||
1597 | // accept changes | |
1598 | m_grid->DisableCellEditControl(); | |
1599 | ||
1600 | event.Skip(); | |
1601 | } | |
1602 | ||
1603 | void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event) | |
1604 | { | |
1605 | switch ( event.GetKeyCode() ) | |
1606 | { | |
1607 | case WXK_ESCAPE: | |
1608 | m_editor->Reset(); | |
1609 | m_grid->DisableCellEditControl(); | |
1610 | break; | |
1611 | ||
1612 | case WXK_TAB: | |
1613 | m_grid->GetEventHandler()->ProcessEvent( event ); | |
1614 | break; | |
1615 | ||
1616 | case WXK_RETURN: | |
1617 | case WXK_NUMPAD_ENTER: | |
1618 | if (!m_grid->GetEventHandler()->ProcessEvent(event)) | |
1619 | m_editor->HandleReturn(event); | |
1620 | break; | |
1621 | ||
1622 | default: | |
1623 | event.Skip(); | |
1624 | break; | |
1625 | } | |
1626 | } | |
1627 | ||
1628 | void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event) | |
1629 | { | |
1630 | int row = m_grid->GetGridCursorRow(); | |
1631 | int col = m_grid->GetGridCursorCol(); | |
1632 | wxRect rect = m_grid->CellToRect( row, col ); | |
1633 | int cw, ch; | |
1634 | m_grid->GetGridWindow()->GetClientSize( &cw, &ch ); | |
1635 | ||
1636 | // if cell width is smaller than grid client area, cell is wholly visible | |
1637 | bool wholeCellVisible = (rect.GetWidth() < cw); | |
1638 | ||
1639 | switch ( event.GetKeyCode() ) | |
1640 | { | |
1641 | case WXK_ESCAPE: | |
1642 | case WXK_TAB: | |
1643 | case WXK_RETURN: | |
1644 | case WXK_NUMPAD_ENTER: | |
1645 | break; | |
1646 | ||
1647 | case WXK_HOME: | |
1648 | { | |
1649 | if ( wholeCellVisible ) | |
1650 | { | |
1651 | // no special processing needed... | |
1652 | event.Skip(); | |
1653 | break; | |
1654 | } | |
1655 | ||
1656 | // do special processing for partly visible cell... | |
1657 | ||
1658 | // get the widths of all cells previous to this one | |
1659 | int colXPos = 0; | |
1660 | for ( int i = 0; i < col; i++ ) | |
1661 | { | |
1662 | colXPos += m_grid->GetColSize(i); | |
1663 | } | |
1664 | ||
1665 | int xUnit = 1, yUnit = 1; | |
1666 | m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit); | |
1667 | if (col != 0) | |
1668 | { | |
1669 | m_grid->Scroll(colXPos / xUnit - 1, m_grid->GetScrollPos(wxVERTICAL)); | |
1670 | } | |
1671 | else | |
1672 | { | |
1673 | m_grid->Scroll(colXPos / xUnit, m_grid->GetScrollPos(wxVERTICAL)); | |
1674 | } | |
1675 | event.Skip(); | |
1676 | break; | |
1677 | } | |
1678 | ||
1679 | case WXK_END: | |
1680 | { | |
1681 | if ( wholeCellVisible ) | |
1682 | { | |
1683 | // no special processing needed... | |
1684 | event.Skip(); | |
1685 | break; | |
1686 | } | |
1687 | ||
1688 | // do special processing for partly visible cell... | |
1689 | ||
1690 | int textWidth = 0; | |
1691 | wxString value = m_grid->GetCellValue(row, col); | |
1692 | if ( wxEmptyString != value ) | |
1693 | { | |
1694 | // get width of cell CONTENTS (text) | |
1695 | int y; | |
1696 | wxFont font = m_grid->GetCellFont(row, col); | |
1697 | m_grid->GetTextExtent(value, &textWidth, &y, NULL, NULL, &font); | |
1698 | ||
1699 | // try to RIGHT align the text by scrolling | |
1700 | int client_right = m_grid->GetGridWindow()->GetClientSize().GetWidth(); | |
1701 | ||
1702 | // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far, | |
1703 | // otherwise the last part of the cell content might be hidden below the scroll bar | |
1704 | // FIXME: maybe there is a more suitable correction? | |
1705 | textWidth -= (client_right - (m_grid->GetScrollLineX() * 2)); | |
1706 | if ( textWidth < 0 ) | |
1707 | { | |
1708 | textWidth = 0; | |
1709 | } | |
1710 | } | |
1711 | ||
1712 | // get the widths of all cells previous to this one | |
1713 | int colXPos = 0; | |
1714 | for ( int i = 0; i < col; i++ ) | |
1715 | { | |
1716 | colXPos += m_grid->GetColSize(i); | |
1717 | } | |
1718 | ||
1719 | // and add the (modified) text width of the cell contents | |
1720 | // as we'd like to see the last part of the cell contents | |
1721 | colXPos += textWidth; | |
1722 | ||
1723 | int xUnit = 1, yUnit = 1; | |
1724 | m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit); | |
1725 | m_grid->Scroll(colXPos / xUnit - 1, m_grid->GetScrollPos(wxVERTICAL)); | |
1726 | event.Skip(); | |
1727 | break; | |
1728 | } | |
1729 | ||
1730 | default: | |
1731 | event.Skip(); | |
1732 | break; | |
1733 | } | |
1734 | } | |
1735 | ||
1736 | // ---------------------------------------------------------------------------- | |
1737 | // wxGridCellWorker is an (almost) empty common base class for | |
1738 | // wxGridCellRenderer and wxGridCellEditor managing ref counting | |
1739 | // ---------------------------------------------------------------------------- | |
1740 | ||
1741 | void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params)) | |
1742 | { | |
1743 | // nothing to do | |
1744 | } | |
1745 | ||
1746 | wxGridCellWorker::~wxGridCellWorker() | |
1747 | { | |
1748 | } | |
1749 | ||
1750 | // ============================================================================ | |
1751 | // renderer classes | |
1752 | // ============================================================================ | |
1753 | ||
1754 | // ---------------------------------------------------------------------------- | |
1755 | // wxGridCellRenderer | |
1756 | // ---------------------------------------------------------------------------- | |
1757 | ||
1758 | void wxGridCellRenderer::Draw(wxGrid& grid, | |
1759 | wxGridCellAttr& attr, | |
1760 | wxDC& dc, | |
1761 | const wxRect& rect, | |
1762 | int WXUNUSED(row), int WXUNUSED(col), | |
1763 | bool isSelected) | |
1764 | { | |
1765 | dc.SetBackgroundMode( wxSOLID ); | |
1766 | ||
1767 | // grey out fields if the grid is disabled | |
1768 | if ( grid.IsEnabled() ) | |
1769 | { | |
1770 | if ( isSelected ) | |
1771 | { | |
1772 | dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) ); | |
1773 | } | |
1774 | else | |
1775 | { | |
1776 | dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) ); | |
1777 | } | |
1778 | } | |
1779 | else | |
1780 | { | |
1781 | dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), wxSOLID)); | |
1782 | } | |
1783 | ||
1784 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
1785 | dc.DrawRectangle(rect); | |
1786 | } | |
1787 | ||
1788 | // ---------------------------------------------------------------------------- | |
1789 | // wxGridCellStringRenderer | |
1790 | // ---------------------------------------------------------------------------- | |
1791 | ||
1792 | void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid& grid, | |
1793 | const wxGridCellAttr& attr, | |
1794 | wxDC& dc, | |
1795 | bool isSelected) | |
1796 | { | |
1797 | dc.SetBackgroundMode( wxTRANSPARENT ); | |
1798 | ||
1799 | // TODO some special colours for attr.IsReadOnly() case? | |
1800 | ||
1801 | // different coloured text when the grid is disabled | |
1802 | if ( grid.IsEnabled() ) | |
1803 | { | |
1804 | if ( isSelected ) | |
1805 | { | |
1806 | dc.SetTextBackground( grid.GetSelectionBackground() ); | |
1807 | dc.SetTextForeground( grid.GetSelectionForeground() ); | |
1808 | } | |
1809 | else | |
1810 | { | |
1811 | dc.SetTextBackground( attr.GetBackgroundColour() ); | |
1812 | dc.SetTextForeground( attr.GetTextColour() ); | |
1813 | } | |
1814 | } | |
1815 | else | |
1816 | { | |
1817 | dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); | |
1818 | dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT)); | |
1819 | } | |
1820 | ||
1821 | dc.SetFont( attr.GetFont() ); | |
1822 | } | |
1823 | ||
1824 | wxSize wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr& attr, | |
1825 | wxDC& dc, | |
1826 | const wxString& text) | |
1827 | { | |
1828 | wxCoord x = 0, y = 0, max_x = 0; | |
1829 | dc.SetFont(attr.GetFont()); | |
1830 | wxStringTokenizer tk(text, _T('\n')); | |
1831 | while ( tk.HasMoreTokens() ) | |
1832 | { | |
1833 | dc.GetTextExtent(tk.GetNextToken(), &x, &y); | |
1834 | max_x = wxMax(max_x, x); | |
1835 | } | |
1836 | ||
1837 | y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines. | |
1838 | ||
1839 | return wxSize(max_x, y); | |
1840 | } | |
1841 | ||
1842 | wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid, | |
1843 | wxGridCellAttr& attr, | |
1844 | wxDC& dc, | |
1845 | int row, int col) | |
1846 | { | |
1847 | return DoGetBestSize(attr, dc, grid.GetCellValue(row, col)); | |
1848 | } | |
1849 | ||
1850 | void wxGridCellStringRenderer::Draw(wxGrid& grid, | |
1851 | wxGridCellAttr& attr, | |
1852 | wxDC& dc, | |
1853 | const wxRect& rectCell, | |
1854 | int row, int col, | |
1855 | bool isSelected) | |
1856 | { | |
1857 | wxRect rect = rectCell; | |
1858 | rect.Inflate(-1); | |
1859 | ||
1860 | // erase only this cells background, overflow cells should have been erased | |
1861 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1862 | ||
1863 | int hAlign, vAlign; | |
1864 | attr.GetAlignment(&hAlign, &vAlign); | |
1865 | ||
1866 | int overflowCols = 0; | |
1867 | ||
1868 | if (attr.GetOverflow()) | |
1869 | { | |
1870 | int cols = grid.GetNumberCols(); | |
1871 | int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth(); | |
1872 | int cell_rows, cell_cols; | |
1873 | attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <= 0 | |
1874 | if ((best_width > rectCell.width) && (col < cols) && grid.GetTable()) | |
1875 | { | |
1876 | int i, c_cols, c_rows; | |
1877 | for (i = col+cell_cols; i < cols; i++) | |
1878 | { | |
1879 | bool is_empty = true; | |
1880 | for (int j=row; j < row + cell_rows; j++) | |
1881 | { | |
1882 | // check w/ anchor cell for multicell block | |
1883 | grid.GetCellSize(j, i, &c_rows, &c_cols); | |
1884 | if (c_rows > 0) | |
1885 | c_rows = 0; | |
1886 | if (!grid.GetTable()->IsEmptyCell(j + c_rows, i)) | |
1887 | { | |
1888 | is_empty = false; | |
1889 | break; | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | if (is_empty) | |
1894 | { | |
1895 | rect.width += grid.GetColSize(i); | |
1896 | } | |
1897 | else | |
1898 | { | |
1899 | i--; | |
1900 | break; | |
1901 | } | |
1902 | ||
1903 | if (rect.width >= best_width) | |
1904 | break; | |
1905 | } | |
1906 | ||
1907 | overflowCols = i - col - cell_cols + 1; | |
1908 | if (overflowCols >= cols) | |
1909 | overflowCols = cols - 1; | |
1910 | } | |
1911 | ||
1912 | if (overflowCols > 0) // redraw overflow cells w/ proper hilight | |
1913 | { | |
1914 | hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned | |
1915 | wxRect clip = rect; | |
1916 | clip.x += rectCell.width; | |
1917 | // draw each overflow cell individually | |
1918 | int col_end = col + cell_cols + overflowCols; | |
1919 | if (col_end >= grid.GetNumberCols()) | |
1920 | col_end = grid.GetNumberCols() - 1; | |
1921 | for (int i = col + cell_cols; i <= col_end; i++) | |
1922 | { | |
1923 | clip.width = grid.GetColSize(i) - 1; | |
1924 | dc.DestroyClippingRegion(); | |
1925 | dc.SetClippingRegion(clip); | |
1926 | ||
1927 | SetTextColoursAndFont(grid, attr, dc, | |
1928 | grid.IsInSelection(row,i)); | |
1929 | ||
1930 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), | |
1931 | rect, hAlign, vAlign); | |
1932 | clip.x += grid.GetColSize(i) - 1; | |
1933 | } | |
1934 | ||
1935 | rect = rectCell; | |
1936 | rect.Inflate(-1); | |
1937 | rect.width++; | |
1938 | dc.DestroyClippingRegion(); | |
1939 | } | |
1940 | } | |
1941 | ||
1942 | // now we only have to draw the text | |
1943 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1944 | ||
1945 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), | |
1946 | rect, hAlign, vAlign); | |
1947 | } | |
1948 | ||
1949 | // ---------------------------------------------------------------------------- | |
1950 | // wxGridCellNumberRenderer | |
1951 | // ---------------------------------------------------------------------------- | |
1952 | ||
1953 | wxString wxGridCellNumberRenderer::GetString(const wxGrid& grid, int row, int col) | |
1954 | { | |
1955 | wxGridTableBase *table = grid.GetTable(); | |
1956 | wxString text; | |
1957 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
1958 | { | |
1959 | text.Printf(_T("%ld"), table->GetValueAsLong(row, col)); | |
1960 | } | |
1961 | else | |
1962 | { | |
1963 | text = table->GetValue(row, col); | |
1964 | } | |
1965 | ||
1966 | return text; | |
1967 | } | |
1968 | ||
1969 | void wxGridCellNumberRenderer::Draw(wxGrid& grid, | |
1970 | wxGridCellAttr& attr, | |
1971 | wxDC& dc, | |
1972 | const wxRect& rectCell, | |
1973 | int row, int col, | |
1974 | bool isSelected) | |
1975 | { | |
1976 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
1977 | ||
1978 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
1979 | ||
1980 | // draw the text right aligned by default | |
1981 | int hAlign, vAlign; | |
1982 | attr.GetAlignment(&hAlign, &vAlign); | |
1983 | hAlign = wxALIGN_RIGHT; | |
1984 | ||
1985 | wxRect rect = rectCell; | |
1986 | rect.Inflate(-1); | |
1987 | ||
1988 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); | |
1989 | } | |
1990 | ||
1991 | wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid, | |
1992 | wxGridCellAttr& attr, | |
1993 | wxDC& dc, | |
1994 | int row, int col) | |
1995 | { | |
1996 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
1997 | } | |
1998 | ||
1999 | // ---------------------------------------------------------------------------- | |
2000 | // wxGridCellFloatRenderer | |
2001 | // ---------------------------------------------------------------------------- | |
2002 | ||
2003 | wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision) | |
2004 | { | |
2005 | SetWidth(width); | |
2006 | SetPrecision(precision); | |
2007 | } | |
2008 | ||
2009 | wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const | |
2010 | { | |
2011 | wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer; | |
2012 | renderer->m_width = m_width; | |
2013 | renderer->m_precision = m_precision; | |
2014 | renderer->m_format = m_format; | |
2015 | ||
2016 | return renderer; | |
2017 | } | |
2018 | ||
2019 | wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col) | |
2020 | { | |
2021 | wxGridTableBase *table = grid.GetTable(); | |
2022 | ||
2023 | bool hasDouble; | |
2024 | double val; | |
2025 | wxString text; | |
2026 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
2027 | { | |
2028 | val = table->GetValueAsDouble(row, col); | |
2029 | hasDouble = true; | |
2030 | } | |
2031 | else | |
2032 | { | |
2033 | text = table->GetValue(row, col); | |
2034 | hasDouble = text.ToDouble(&val); | |
2035 | } | |
2036 | ||
2037 | if ( hasDouble ) | |
2038 | { | |
2039 | if ( !m_format ) | |
2040 | { | |
2041 | if ( m_width == -1 ) | |
2042 | { | |
2043 | if ( m_precision == -1 ) | |
2044 | { | |
2045 | // default width/precision | |
2046 | m_format = _T("%f"); | |
2047 | } | |
2048 | else | |
2049 | { | |
2050 | m_format.Printf(_T("%%.%df"), m_precision); | |
2051 | } | |
2052 | } | |
2053 | else if ( m_precision == -1 ) | |
2054 | { | |
2055 | // default precision | |
2056 | m_format.Printf(_T("%%%d.f"), m_width); | |
2057 | } | |
2058 | else | |
2059 | { | |
2060 | m_format.Printf(_T("%%%d.%df"), m_width, m_precision); | |
2061 | } | |
2062 | } | |
2063 | ||
2064 | text.Printf(m_format, val); | |
2065 | ||
2066 | } | |
2067 | //else: text already contains the string | |
2068 | ||
2069 | return text; | |
2070 | } | |
2071 | ||
2072 | void wxGridCellFloatRenderer::Draw(wxGrid& grid, | |
2073 | wxGridCellAttr& attr, | |
2074 | wxDC& dc, | |
2075 | const wxRect& rectCell, | |
2076 | int row, int col, | |
2077 | bool isSelected) | |
2078 | { | |
2079 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
2080 | ||
2081 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
2082 | ||
2083 | // draw the text right aligned by default | |
2084 | int hAlign, vAlign; | |
2085 | attr.GetAlignment(&hAlign, &vAlign); | |
2086 | hAlign = wxALIGN_RIGHT; | |
2087 | ||
2088 | wxRect rect = rectCell; | |
2089 | rect.Inflate(-1); | |
2090 | ||
2091 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); | |
2092 | } | |
2093 | ||
2094 | wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid, | |
2095 | wxGridCellAttr& attr, | |
2096 | wxDC& dc, | |
2097 | int row, int col) | |
2098 | { | |
2099 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
2100 | } | |
2101 | ||
2102 | void wxGridCellFloatRenderer::SetParameters(const wxString& params) | |
2103 | { | |
2104 | if ( !params ) | |
2105 | { | |
2106 | // reset to defaults | |
2107 | SetWidth(-1); | |
2108 | SetPrecision(-1); | |
2109 | } | |
2110 | else | |
2111 | { | |
2112 | wxString tmp = params.BeforeFirst(_T(',')); | |
2113 | if ( !tmp.empty() ) | |
2114 | { | |
2115 | long width; | |
2116 | if ( tmp.ToLong(&width) ) | |
2117 | { | |
2118 | SetWidth((int)width); | |
2119 | } | |
2120 | else | |
2121 | { | |
2122 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str()); | |
2123 | } | |
2124 | } | |
2125 | ||
2126 | tmp = params.AfterFirst(_T(',')); | |
2127 | if ( !tmp.empty() ) | |
2128 | { | |
2129 | long precision; | |
2130 | if ( tmp.ToLong(&precision) ) | |
2131 | { | |
2132 | SetPrecision((int)precision); | |
2133 | } | |
2134 | else | |
2135 | { | |
2136 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str()); | |
2137 | } | |
2138 | } | |
2139 | } | |
2140 | } | |
2141 | ||
2142 | // ---------------------------------------------------------------------------- | |
2143 | // wxGridCellBoolRenderer | |
2144 | // ---------------------------------------------------------------------------- | |
2145 | ||
2146 | wxSize wxGridCellBoolRenderer::ms_sizeCheckMark; | |
2147 | ||
2148 | // FIXME these checkbox size calculations are really ugly... | |
2149 | ||
2150 | // between checkmark and box | |
2151 | static const wxCoord wxGRID_CHECKMARK_MARGIN = 2; | |
2152 | ||
2153 | wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid, | |
2154 | wxGridCellAttr& WXUNUSED(attr), | |
2155 | wxDC& WXUNUSED(dc), | |
2156 | int WXUNUSED(row), | |
2157 | int WXUNUSED(col)) | |
2158 | { | |
2159 | // compute it only once (no locks for MT safeness in GUI thread...) | |
2160 | if ( !ms_sizeCheckMark.x ) | |
2161 | { | |
2162 | // get checkbox size | |
2163 | wxCheckBox *checkbox = new wxCheckBox(&grid, wxID_ANY, wxEmptyString); | |
2164 | wxSize size = checkbox->GetBestSize(); | |
2165 | wxCoord checkSize = size.y + 2 * wxGRID_CHECKMARK_MARGIN; | |
2166 | ||
2167 | // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result | |
2168 | #if defined(__WXGTK__) || defined(__WXMOTIF__) | |
2169 | checkSize -= size.y / 2; | |
2170 | #endif | |
2171 | ||
2172 | delete checkbox; | |
2173 | ||
2174 | ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize; | |
2175 | } | |
2176 | ||
2177 | return ms_sizeCheckMark; | |
2178 | } | |
2179 | ||
2180 | void wxGridCellBoolRenderer::Draw(wxGrid& grid, | |
2181 | wxGridCellAttr& attr, | |
2182 | wxDC& dc, | |
2183 | const wxRect& rect, | |
2184 | int row, int col, | |
2185 | bool isSelected) | |
2186 | { | |
2187 | wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); | |
2188 | ||
2189 | // draw a check mark in the centre (ignoring alignment - TODO) | |
2190 | wxSize size = GetBestSize(grid, attr, dc, row, col); | |
2191 | ||
2192 | // don't draw outside the cell | |
2193 | wxCoord minSize = wxMin(rect.width, rect.height); | |
2194 | if ( size.x >= minSize || size.y >= minSize ) | |
2195 | { | |
2196 | // and even leave (at least) 1 pixel margin | |
2197 | size.x = size.y = minSize - 2; | |
2198 | } | |
2199 | ||
2200 | // draw a border around checkmark | |
2201 | int vAlign, hAlign; | |
2202 | attr.GetAlignment(&hAlign, &vAlign); | |
2203 | ||
2204 | wxRect rectBorder; | |
2205 | if (hAlign == wxALIGN_CENTRE) | |
2206 | { | |
2207 | rectBorder.x = rect.x + rect.width / 2 - size.x / 2; | |
2208 | rectBorder.y = rect.y + rect.height / 2 - size.y / 2; | |
2209 | rectBorder.width = size.x; | |
2210 | rectBorder.height = size.y; | |
2211 | } | |
2212 | else if (hAlign == wxALIGN_LEFT) | |
2213 | { | |
2214 | rectBorder.x = rect.x + 2; | |
2215 | rectBorder.y = rect.y + rect.height / 2 - size.y / 2; | |
2216 | rectBorder.width = size.x; | |
2217 | rectBorder.height = size.y; | |
2218 | } | |
2219 | else if (hAlign == wxALIGN_RIGHT) | |
2220 | { | |
2221 | rectBorder.x = rect.x + rect.width - size.x - 2; | |
2222 | rectBorder.y = rect.y + rect.height / 2 - size.y / 2; | |
2223 | rectBorder.width = size.x; | |
2224 | rectBorder.height = size.y; | |
2225 | } | |
2226 | ||
2227 | bool value; | |
2228 | if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) | |
2229 | { | |
2230 | value = grid.GetTable()->GetValueAsBool(row, col); | |
2231 | } | |
2232 | else | |
2233 | { | |
2234 | wxString cellval( grid.GetTable()->GetValue(row, col) ); | |
2235 | value = wxGridCellBoolEditor::IsTrueValue(cellval); | |
2236 | } | |
2237 | ||
2238 | if ( value ) | |
2239 | { | |
2240 | wxRect rectMark = rectBorder; | |
2241 | ||
2242 | #ifdef __WXMSW__ | |
2243 | // MSW DrawCheckMark() is weird (and should probably be changed...) | |
2244 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN / 2); | |
2245 | rectMark.x++; | |
2246 | rectMark.y++; | |
2247 | #else | |
2248 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN); | |
2249 | #endif | |
2250 | ||
2251 | dc.SetTextForeground(attr.GetTextColour()); | |
2252 | dc.DrawCheckMark(rectMark); | |
2253 | } | |
2254 | ||
2255 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
2256 | dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID)); | |
2257 | dc.DrawRectangle(rectBorder); | |
2258 | } | |
2259 | ||
2260 | // ---------------------------------------------------------------------------- | |
2261 | // wxGridCellAttr | |
2262 | // ---------------------------------------------------------------------------- | |
2263 | ||
2264 | void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) | |
2265 | { | |
2266 | m_nRef = 1; | |
2267 | ||
2268 | m_isReadOnly = Unset; | |
2269 | ||
2270 | m_renderer = NULL; | |
2271 | m_editor = NULL; | |
2272 | ||
2273 | m_attrkind = wxGridCellAttr::Cell; | |
2274 | ||
2275 | m_sizeRows = m_sizeCols = 1; | |
2276 | m_overflow = UnsetOverflow; | |
2277 | ||
2278 | SetDefAttr(attrDefault); | |
2279 | } | |
2280 | ||
2281 | wxGridCellAttr *wxGridCellAttr::Clone() const | |
2282 | { | |
2283 | wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr); | |
2284 | ||
2285 | if ( HasTextColour() ) | |
2286 | attr->SetTextColour(GetTextColour()); | |
2287 | if ( HasBackgroundColour() ) | |
2288 | attr->SetBackgroundColour(GetBackgroundColour()); | |
2289 | if ( HasFont() ) | |
2290 | attr->SetFont(GetFont()); | |
2291 | if ( HasAlignment() ) | |
2292 | attr->SetAlignment(m_hAlign, m_vAlign); | |
2293 | ||
2294 | attr->SetSize( m_sizeRows, m_sizeCols ); | |
2295 | ||
2296 | if ( m_renderer ) | |
2297 | { | |
2298 | attr->SetRenderer(m_renderer); | |
2299 | m_renderer->IncRef(); | |
2300 | } | |
2301 | if ( m_editor ) | |
2302 | { | |
2303 | attr->SetEditor(m_editor); | |
2304 | m_editor->IncRef(); | |
2305 | } | |
2306 | ||
2307 | if ( IsReadOnly() ) | |
2308 | attr->SetReadOnly(); | |
2309 | ||
2310 | attr->SetOverflow( m_overflow == Overflow ); | |
2311 | attr->SetKind( m_attrkind ); | |
2312 | ||
2313 | return attr; | |
2314 | } | |
2315 | ||
2316 | void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) | |
2317 | { | |
2318 | if ( !HasTextColour() && mergefrom->HasTextColour() ) | |
2319 | SetTextColour(mergefrom->GetTextColour()); | |
2320 | if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() ) | |
2321 | SetBackgroundColour(mergefrom->GetBackgroundColour()); | |
2322 | if ( !HasFont() && mergefrom->HasFont() ) | |
2323 | SetFont(mergefrom->GetFont()); | |
2324 | if ( !HasAlignment() && mergefrom->HasAlignment() ) | |
2325 | { | |
2326 | int hAlign, vAlign; | |
2327 | mergefrom->GetAlignment( &hAlign, &vAlign); | |
2328 | SetAlignment(hAlign, vAlign); | |
2329 | } | |
2330 | if ( !HasSize() && mergefrom->HasSize() ) | |
2331 | mergefrom->GetSize( &m_sizeRows, &m_sizeCols ); | |
2332 | ||
2333 | // Directly access member functions as GetRender/Editor don't just return | |
2334 | // m_renderer/m_editor | |
2335 | // | |
2336 | // Maybe add support for merge of Render and Editor? | |
2337 | if (!HasRenderer() && mergefrom->HasRenderer() ) | |
2338 | { | |
2339 | m_renderer = mergefrom->m_renderer; | |
2340 | m_renderer->IncRef(); | |
2341 | } | |
2342 | if ( !HasEditor() && mergefrom->HasEditor() ) | |
2343 | { | |
2344 | m_editor = mergefrom->m_editor; | |
2345 | m_editor->IncRef(); | |
2346 | } | |
2347 | if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) | |
2348 | SetReadOnly(mergefrom->IsReadOnly()); | |
2349 | ||
2350 | if (!HasOverflowMode() && mergefrom->HasOverflowMode() ) | |
2351 | SetOverflow(mergefrom->GetOverflow()); | |
2352 | ||
2353 | SetDefAttr(mergefrom->m_defGridAttr); | |
2354 | } | |
2355 | ||
2356 | void wxGridCellAttr::SetSize(int num_rows, int num_cols) | |
2357 | { | |
2358 | // The size of a cell is normally 1,1 | |
2359 | ||
2360 | // If this cell is larger (2,2) then this is the top left cell | |
2361 | // the other cells that will be covered (lower right cells) must be | |
2362 | // set to negative or zero values such that | |
2363 | // row + num_rows of the covered cell points to the larger cell (this cell) | |
2364 | // same goes for the col + num_cols. | |
2365 | ||
2366 | // Size of 0,0 is NOT valid, neither is <=0 and any positive value | |
2367 | ||
2368 | wxASSERT_MSG( (!((num_rows > 0) && (num_cols <= 0)) || | |
2369 | !((num_rows <= 0) && (num_cols > 0)) || | |
2370 | !((num_rows == 0) && (num_cols == 0))), | |
2371 | wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values")); | |
2372 | ||
2373 | m_sizeRows = num_rows; | |
2374 | m_sizeCols = num_cols; | |
2375 | } | |
2376 | ||
2377 | const wxColour& wxGridCellAttr::GetTextColour() const | |
2378 | { | |
2379 | if (HasTextColour()) | |
2380 | { | |
2381 | return m_colText; | |
2382 | } | |
2383 | else if (m_defGridAttr && m_defGridAttr != this) | |
2384 | { | |
2385 | return m_defGridAttr->GetTextColour(); | |
2386 | } | |
2387 | else | |
2388 | { | |
2389 | wxFAIL_MSG(wxT("Missing default cell attribute")); | |
2390 | return wxNullColour; | |
2391 | } | |
2392 | } | |
2393 | ||
2394 | const wxColour& wxGridCellAttr::GetBackgroundColour() const | |
2395 | { | |
2396 | if (HasBackgroundColour()) | |
2397 | { | |
2398 | return m_colBack; | |
2399 | } | |
2400 | else if (m_defGridAttr && m_defGridAttr != this) | |
2401 | { | |
2402 | return m_defGridAttr->GetBackgroundColour(); | |
2403 | } | |
2404 | else | |
2405 | { | |
2406 | wxFAIL_MSG(wxT("Missing default cell attribute")); | |
2407 | return wxNullColour; | |
2408 | } | |
2409 | } | |
2410 | ||
2411 | const wxFont& wxGridCellAttr::GetFont() const | |
2412 | { | |
2413 | if (HasFont()) | |
2414 | { | |
2415 | return m_font; | |
2416 | } | |
2417 | else if (m_defGridAttr && m_defGridAttr != this) | |
2418 | { | |
2419 | return m_defGridAttr->GetFont(); | |
2420 | } | |
2421 | else | |
2422 | { | |
2423 | wxFAIL_MSG(wxT("Missing default cell attribute")); | |
2424 | return wxNullFont; | |
2425 | } | |
2426 | } | |
2427 | ||
2428 | void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const | |
2429 | { | |
2430 | if (HasAlignment()) | |
2431 | { | |
2432 | if ( hAlign ) | |
2433 | *hAlign = m_hAlign; | |
2434 | if ( vAlign ) | |
2435 | *vAlign = m_vAlign; | |
2436 | } | |
2437 | else if (m_defGridAttr && m_defGridAttr != this) | |
2438 | { | |
2439 | m_defGridAttr->GetAlignment(hAlign, vAlign); | |
2440 | } | |
2441 | else | |
2442 | { | |
2443 | wxFAIL_MSG(wxT("Missing default cell attribute")); | |
2444 | } | |
2445 | } | |
2446 | ||
2447 | void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const | |
2448 | { | |
2449 | if ( num_rows ) | |
2450 | *num_rows = m_sizeRows; | |
2451 | if ( num_cols ) | |
2452 | *num_cols = m_sizeCols; | |
2453 | } | |
2454 | ||
2455 | // GetRenderer and GetEditor use a slightly different decision path about | |
2456 | // which attribute to use. If a non-default attr object has one then it is | |
2457 | // used, otherwise the default editor or renderer is fetched from the grid and | |
2458 | // used. It should be the default for the data type of the cell. If it is | |
2459 | // NULL (because the table has a type that the grid does not have in its | |
2460 | // registry), then the grid's default editor or renderer is used. | |
2461 | ||
2462 | wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const | |
2463 | { | |
2464 | wxGridCellRenderer *renderer = NULL; | |
2465 | ||
2466 | if ( m_renderer && this != m_defGridAttr ) | |
2467 | { | |
2468 | // use the cells renderer if it has one | |
2469 | renderer = m_renderer; | |
2470 | renderer->IncRef(); | |
2471 | } | |
2472 | else // no non-default cell renderer | |
2473 | { | |
2474 | // get default renderer for the data type | |
2475 | if ( grid ) | |
2476 | { | |
2477 | // GetDefaultRendererForCell() will do IncRef() for us | |
2478 | renderer = grid->GetDefaultRendererForCell(row, col); | |
2479 | } | |
2480 | ||
2481 | if ( renderer == NULL ) | |
2482 | { | |
2483 | if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) ) | |
2484 | { | |
2485 | // if we still don't have one then use the grid default | |
2486 | // (no need for IncRef() here neither) | |
2487 | renderer = m_defGridAttr->GetRenderer(NULL, 0, 0); | |
2488 | } | |
2489 | else // default grid attr | |
2490 | { | |
2491 | // use m_renderer which we had decided not to use initially | |
2492 | renderer = m_renderer; | |
2493 | if ( renderer ) | |
2494 | renderer->IncRef(); | |
2495 | } | |
2496 | } | |
2497 | } | |
2498 | ||
2499 | // we're supposed to always find something | |
2500 | wxASSERT_MSG(renderer, wxT("Missing default cell renderer")); | |
2501 | ||
2502 | return renderer; | |
2503 | } | |
2504 | ||
2505 | // same as above, except for s/renderer/editor/g | |
2506 | wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const | |
2507 | { | |
2508 | wxGridCellEditor *editor = NULL; | |
2509 | ||
2510 | if ( m_editor && this != m_defGridAttr ) | |
2511 | { | |
2512 | // use the cells editor if it has one | |
2513 | editor = m_editor; | |
2514 | editor->IncRef(); | |
2515 | } | |
2516 | else // no non default cell editor | |
2517 | { | |
2518 | // get default editor for the data type | |
2519 | if ( grid ) | |
2520 | { | |
2521 | // GetDefaultEditorForCell() will do IncRef() for us | |
2522 | editor = grid->GetDefaultEditorForCell(row, col); | |
2523 | } | |
2524 | ||
2525 | if ( editor == NULL ) | |
2526 | { | |
2527 | if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) ) | |
2528 | { | |
2529 | // if we still don't have one then use the grid default | |
2530 | // (no need for IncRef() here neither) | |
2531 | editor = m_defGridAttr->GetEditor(NULL, 0, 0); | |
2532 | } | |
2533 | else // default grid attr | |
2534 | { | |
2535 | // use m_editor which we had decided not to use initially | |
2536 | editor = m_editor; | |
2537 | if ( editor ) | |
2538 | editor->IncRef(); | |
2539 | } | |
2540 | } | |
2541 | } | |
2542 | ||
2543 | // we're supposed to always find something | |
2544 | wxASSERT_MSG(editor, wxT("Missing default cell editor")); | |
2545 | ||
2546 | return editor; | |
2547 | } | |
2548 | ||
2549 | // ---------------------------------------------------------------------------- | |
2550 | // wxGridCellAttrData | |
2551 | // ---------------------------------------------------------------------------- | |
2552 | ||
2553 | void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) | |
2554 | { | |
2555 | int n = FindIndex(row, col); | |
2556 | if ( n == wxNOT_FOUND ) | |
2557 | { | |
2558 | // add the attribute | |
2559 | m_attrs.Add(new wxGridCellWithAttr(row, col, attr)); | |
2560 | } | |
2561 | else | |
2562 | { | |
2563 | // free the old attribute | |
2564 | m_attrs[(size_t)n].attr->DecRef(); | |
2565 | ||
2566 | if ( attr ) | |
2567 | { | |
2568 | // change the attribute | |
2569 | m_attrs[(size_t)n].attr = attr; | |
2570 | } | |
2571 | else | |
2572 | { | |
2573 | // remove this attribute | |
2574 | m_attrs.RemoveAt((size_t)n); | |
2575 | } | |
2576 | } | |
2577 | } | |
2578 | ||
2579 | wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const | |
2580 | { | |
2581 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2582 | ||
2583 | int n = FindIndex(row, col); | |
2584 | if ( n != wxNOT_FOUND ) | |
2585 | { | |
2586 | attr = m_attrs[(size_t)n].attr; | |
2587 | attr->IncRef(); | |
2588 | } | |
2589 | ||
2590 | return attr; | |
2591 | } | |
2592 | ||
2593 | void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) | |
2594 | { | |
2595 | size_t count = m_attrs.GetCount(); | |
2596 | for ( size_t n = 0; n < count; n++ ) | |
2597 | { | |
2598 | wxGridCellCoords& coords = m_attrs[n].coords; | |
2599 | wxCoord row = coords.GetRow(); | |
2600 | if ((size_t)row >= pos) | |
2601 | { | |
2602 | if (numRows > 0) | |
2603 | { | |
2604 | // If rows inserted, include row counter where necessary | |
2605 | coords.SetRow(row + numRows); | |
2606 | } | |
2607 | else if (numRows < 0) | |
2608 | { | |
2609 | // If rows deleted ... | |
2610 | if ((size_t)row >= pos - numRows) | |
2611 | { | |
2612 | // ...either decrement row counter (if row still exists)... | |
2613 | coords.SetRow(row + numRows); | |
2614 | } | |
2615 | else | |
2616 | { | |
2617 | // ...or remove the attribute | |
2618 | // No need to DecRef the attribute itself since this is | |
2619 | // done be wxGridCellWithAttr's destructor! | |
2620 | m_attrs.RemoveAt(n); | |
2621 | n--; | |
2622 | count--; | |
2623 | } | |
2624 | } | |
2625 | } | |
2626 | } | |
2627 | } | |
2628 | ||
2629 | void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) | |
2630 | { | |
2631 | size_t count = m_attrs.GetCount(); | |
2632 | for ( size_t n = 0; n < count; n++ ) | |
2633 | { | |
2634 | wxGridCellCoords& coords = m_attrs[n].coords; | |
2635 | wxCoord col = coords.GetCol(); | |
2636 | if ( (size_t)col >= pos ) | |
2637 | { | |
2638 | if ( numCols > 0 ) | |
2639 | { | |
2640 | // If rows inserted, include row counter where necessary | |
2641 | coords.SetCol(col + numCols); | |
2642 | } | |
2643 | else if (numCols < 0) | |
2644 | { | |
2645 | // If rows deleted ... | |
2646 | if ((size_t)col >= pos - numCols) | |
2647 | { | |
2648 | // ...either decrement row counter (if row still exists)... | |
2649 | coords.SetCol(col + numCols); | |
2650 | } | |
2651 | else | |
2652 | { | |
2653 | // ...or remove the attribute | |
2654 | // No need to DecRef the attribute itself since this is | |
2655 | // done be wxGridCellWithAttr's destructor! | |
2656 | m_attrs.RemoveAt(n); | |
2657 | n--; | |
2658 | count--; | |
2659 | } | |
2660 | } | |
2661 | } | |
2662 | } | |
2663 | } | |
2664 | ||
2665 | int wxGridCellAttrData::FindIndex(int row, int col) const | |
2666 | { | |
2667 | size_t count = m_attrs.GetCount(); | |
2668 | for ( size_t n = 0; n < count; n++ ) | |
2669 | { | |
2670 | const wxGridCellCoords& coords = m_attrs[n].coords; | |
2671 | if ( (coords.GetRow() == row) && (coords.GetCol() == col) ) | |
2672 | { | |
2673 | return n; | |
2674 | } | |
2675 | } | |
2676 | ||
2677 | return wxNOT_FOUND; | |
2678 | } | |
2679 | ||
2680 | // ---------------------------------------------------------------------------- | |
2681 | // wxGridRowOrColAttrData | |
2682 | // ---------------------------------------------------------------------------- | |
2683 | ||
2684 | wxGridRowOrColAttrData::~wxGridRowOrColAttrData() | |
2685 | { | |
2686 | size_t count = m_attrs.Count(); | |
2687 | for ( size_t n = 0; n < count; n++ ) | |
2688 | { | |
2689 | m_attrs[n]->DecRef(); | |
2690 | } | |
2691 | } | |
2692 | ||
2693 | wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const | |
2694 | { | |
2695 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2696 | ||
2697 | int n = m_rowsOrCols.Index(rowOrCol); | |
2698 | if ( n != wxNOT_FOUND ) | |
2699 | { | |
2700 | attr = m_attrs[(size_t)n]; | |
2701 | attr->IncRef(); | |
2702 | } | |
2703 | ||
2704 | return attr; | |
2705 | } | |
2706 | ||
2707 | void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) | |
2708 | { | |
2709 | int i = m_rowsOrCols.Index(rowOrCol); | |
2710 | if ( i == wxNOT_FOUND ) | |
2711 | { | |
2712 | // add the attribute | |
2713 | m_rowsOrCols.Add(rowOrCol); | |
2714 | m_attrs.Add(attr); | |
2715 | } | |
2716 | else | |
2717 | { | |
2718 | size_t n = (size_t)i; | |
2719 | if ( attr ) | |
2720 | { | |
2721 | // change the attribute | |
2722 | m_attrs[n]->DecRef(); | |
2723 | m_attrs[n] = attr; | |
2724 | } | |
2725 | else | |
2726 | { | |
2727 | // remove this attribute | |
2728 | m_attrs[n]->DecRef(); | |
2729 | m_rowsOrCols.RemoveAt(n); | |
2730 | m_attrs.RemoveAt(n); | |
2731 | } | |
2732 | } | |
2733 | } | |
2734 | ||
2735 | void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) | |
2736 | { | |
2737 | size_t count = m_attrs.GetCount(); | |
2738 | for ( size_t n = 0; n < count; n++ ) | |
2739 | { | |
2740 | int & rowOrCol = m_rowsOrCols[n]; | |
2741 | if ( (size_t)rowOrCol >= pos ) | |
2742 | { | |
2743 | if ( numRowsOrCols > 0 ) | |
2744 | { | |
2745 | // If rows inserted, include row counter where necessary | |
2746 | rowOrCol += numRowsOrCols; | |
2747 | } | |
2748 | else if ( numRowsOrCols < 0) | |
2749 | { | |
2750 | // If rows deleted, either decrement row counter (if row still exists) | |
2751 | if ((size_t)rowOrCol >= pos - numRowsOrCols) | |
2752 | rowOrCol += numRowsOrCols; | |
2753 | else | |
2754 | { | |
2755 | m_rowsOrCols.RemoveAt(n); | |
2756 | m_attrs[n]->DecRef(); | |
2757 | m_attrs.RemoveAt(n); | |
2758 | n--; | |
2759 | count--; | |
2760 | } | |
2761 | } | |
2762 | } | |
2763 | } | |
2764 | } | |
2765 | ||
2766 | // ---------------------------------------------------------------------------- | |
2767 | // wxGridCellAttrProvider | |
2768 | // ---------------------------------------------------------------------------- | |
2769 | ||
2770 | wxGridCellAttrProvider::wxGridCellAttrProvider() | |
2771 | { | |
2772 | m_data = (wxGridCellAttrProviderData *)NULL; | |
2773 | } | |
2774 | ||
2775 | wxGridCellAttrProvider::~wxGridCellAttrProvider() | |
2776 | { | |
2777 | delete m_data; | |
2778 | } | |
2779 | ||
2780 | void wxGridCellAttrProvider::InitData() | |
2781 | { | |
2782 | m_data = new wxGridCellAttrProviderData; | |
2783 | } | |
2784 | ||
2785 | wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col, | |
2786 | wxGridCellAttr::wxAttrKind kind ) const | |
2787 | { | |
2788 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
2789 | if ( m_data ) | |
2790 | { | |
2791 | switch (kind) | |
2792 | { | |
2793 | case (wxGridCellAttr::Any): | |
2794 | // Get cached merge attributes. | |
2795 | // Currently not used as no cache implemented as not mutable | |
2796 | // attr = m_data->m_mergeAttr.GetAttr(row, col); | |
2797 | if (!attr) | |
2798 | { | |
2799 | // Basically implement old version. | |
2800 | // Also check merge cache, so we don't have to re-merge every time.. | |
2801 | wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col); | |
2802 | wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row); | |
2803 | wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col); | |
2804 | ||
2805 | if ((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol)) | |
2806 | { | |
2807 | // Two or more are non NULL | |
2808 | attr = new wxGridCellAttr; | |
2809 | attr->SetKind(wxGridCellAttr::Merged); | |
2810 | ||
2811 | // Order is important.. | |
2812 | if (attrcell) | |
2813 | { | |
2814 | attr->MergeWith(attrcell); | |
2815 | attrcell->DecRef(); | |
2816 | } | |
2817 | if (attrcol) | |
2818 | { | |
2819 | attr->MergeWith(attrcol); | |
2820 | attrcol->DecRef(); | |
2821 | } | |
2822 | if (attrrow) | |
2823 | { | |
2824 | attr->MergeWith(attrrow); | |
2825 | attrrow->DecRef(); | |
2826 | } | |
2827 | ||
2828 | // store merge attr if cache implemented | |
2829 | //attr->IncRef(); | |
2830 | //m_data->m_mergeAttr.SetAttr(attr, row, col); | |
2831 | } | |
2832 | else | |
2833 | { | |
2834 | // one or none is non null return it or null. | |
2835 | if (attrrow) | |
2836 | attr = attrrow; | |
2837 | if (attrcol) | |
2838 | { | |
2839 | if (attr) | |
2840 | attr->DecRef(); | |
2841 | attr = attrcol; | |
2842 | } | |
2843 | if (attrcell) | |
2844 | { | |
2845 | if (attr) | |
2846 | attr->DecRef(); | |
2847 | attr = attrcell; | |
2848 | } | |
2849 | } | |
2850 | } | |
2851 | break; | |
2852 | ||
2853 | case (wxGridCellAttr::Cell): | |
2854 | attr = m_data->m_cellAttrs.GetAttr(row, col); | |
2855 | break; | |
2856 | ||
2857 | case (wxGridCellAttr::Col): | |
2858 | attr = m_data->m_colAttrs.GetAttr(col); | |
2859 | break; | |
2860 | ||
2861 | case (wxGridCellAttr::Row): | |
2862 | attr = m_data->m_rowAttrs.GetAttr(row); | |
2863 | break; | |
2864 | ||
2865 | default: | |
2866 | // unused as yet... | |
2867 | // (wxGridCellAttr::Default): | |
2868 | // (wxGridCellAttr::Merged): | |
2869 | break; | |
2870 | } | |
2871 | } | |
2872 | ||
2873 | return attr; | |
2874 | } | |
2875 | ||
2876 | void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, | |
2877 | int row, int col) | |
2878 | { | |
2879 | if ( !m_data ) | |
2880 | InitData(); | |
2881 | ||
2882 | m_data->m_cellAttrs.SetAttr(attr, row, col); | |
2883 | } | |
2884 | ||
2885 | void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row) | |
2886 | { | |
2887 | if ( !m_data ) | |
2888 | InitData(); | |
2889 | ||
2890 | m_data->m_rowAttrs.SetAttr(attr, row); | |
2891 | } | |
2892 | ||
2893 | void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) | |
2894 | { | |
2895 | if ( !m_data ) | |
2896 | InitData(); | |
2897 | ||
2898 | m_data->m_colAttrs.SetAttr(attr, col); | |
2899 | } | |
2900 | ||
2901 | void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) | |
2902 | { | |
2903 | if ( m_data ) | |
2904 | { | |
2905 | m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); | |
2906 | ||
2907 | m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); | |
2908 | } | |
2909 | } | |
2910 | ||
2911 | void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) | |
2912 | { | |
2913 | if ( m_data ) | |
2914 | { | |
2915 | m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); | |
2916 | ||
2917 | m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); | |
2918 | } | |
2919 | } | |
2920 | ||
2921 | // ---------------------------------------------------------------------------- | |
2922 | // wxGridTypeRegistry | |
2923 | // ---------------------------------------------------------------------------- | |
2924 | ||
2925 | wxGridTypeRegistry::~wxGridTypeRegistry() | |
2926 | { | |
2927 | size_t count = m_typeinfo.Count(); | |
2928 | for ( size_t i = 0; i < count; i++ ) | |
2929 | delete m_typeinfo[i]; | |
2930 | } | |
2931 | ||
2932 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, | |
2933 | wxGridCellRenderer* renderer, | |
2934 | wxGridCellEditor* editor) | |
2935 | { | |
2936 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); | |
2937 | ||
2938 | // is it already registered? | |
2939 | int loc = FindRegisteredDataType(typeName); | |
2940 | if ( loc != wxNOT_FOUND ) | |
2941 | { | |
2942 | delete m_typeinfo[loc]; | |
2943 | m_typeinfo[loc] = info; | |
2944 | } | |
2945 | else | |
2946 | { | |
2947 | m_typeinfo.Add(info); | |
2948 | } | |
2949 | } | |
2950 | ||
2951 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) | |
2952 | { | |
2953 | size_t count = m_typeinfo.GetCount(); | |
2954 | for ( size_t i = 0; i < count; i++ ) | |
2955 | { | |
2956 | if ( typeName == m_typeinfo[i]->m_typeName ) | |
2957 | { | |
2958 | return i; | |
2959 | } | |
2960 | } | |
2961 | ||
2962 | return wxNOT_FOUND; | |
2963 | } | |
2964 | ||
2965 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) | |
2966 | { | |
2967 | int index = FindRegisteredDataType(typeName); | |
2968 | if ( index == wxNOT_FOUND ) | |
2969 | { | |
2970 | // check whether this is one of the standard ones, in which case | |
2971 | // register it "on the fly" | |
2972 | #if wxUSE_TEXTCTRL | |
2973 | if ( typeName == wxGRID_VALUE_STRING ) | |
2974 | { | |
2975 | RegisterDataType(wxGRID_VALUE_STRING, | |
2976 | new wxGridCellStringRenderer, | |
2977 | new wxGridCellTextEditor); | |
2978 | } | |
2979 | else | |
2980 | #endif // wxUSE_TEXTCTRL | |
2981 | #if wxUSE_CHECKBOX | |
2982 | if ( typeName == wxGRID_VALUE_BOOL ) | |
2983 | { | |
2984 | RegisterDataType(wxGRID_VALUE_BOOL, | |
2985 | new wxGridCellBoolRenderer, | |
2986 | new wxGridCellBoolEditor); | |
2987 | } | |
2988 | else | |
2989 | #endif // wxUSE_CHECKBOX | |
2990 | #if wxUSE_TEXTCTRL | |
2991 | if ( typeName == wxGRID_VALUE_NUMBER ) | |
2992 | { | |
2993 | RegisterDataType(wxGRID_VALUE_NUMBER, | |
2994 | new wxGridCellNumberRenderer, | |
2995 | new wxGridCellNumberEditor); | |
2996 | } | |
2997 | else if ( typeName == wxGRID_VALUE_FLOAT ) | |
2998 | { | |
2999 | RegisterDataType(wxGRID_VALUE_FLOAT, | |
3000 | new wxGridCellFloatRenderer, | |
3001 | new wxGridCellFloatEditor); | |
3002 | } | |
3003 | else | |
3004 | #endif // wxUSE_TEXTCTRL | |
3005 | #if wxUSE_COMBOBOX | |
3006 | if ( typeName == wxGRID_VALUE_CHOICE ) | |
3007 | { | |
3008 | RegisterDataType(wxGRID_VALUE_CHOICE, | |
3009 | new wxGridCellStringRenderer, | |
3010 | new wxGridCellChoiceEditor); | |
3011 | } | |
3012 | else | |
3013 | #endif // wxUSE_COMBOBOX | |
3014 | { | |
3015 | return wxNOT_FOUND; | |
3016 | } | |
3017 | ||
3018 | // we get here only if just added the entry for this type, so return | |
3019 | // the last index | |
3020 | index = m_typeinfo.GetCount() - 1; | |
3021 | } | |
3022 | ||
3023 | return index; | |
3024 | } | |
3025 | ||
3026 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) | |
3027 | { | |
3028 | int index = FindDataType(typeName); | |
3029 | if ( index == wxNOT_FOUND ) | |
3030 | { | |
3031 | // the first part of the typename is the "real" type, anything after ':' | |
3032 | // are the parameters for the renderer | |
3033 | index = FindDataType(typeName.BeforeFirst(_T(':'))); | |
3034 | if ( index == wxNOT_FOUND ) | |
3035 | { | |
3036 | return wxNOT_FOUND; | |
3037 | } | |
3038 | ||
3039 | wxGridCellRenderer *renderer = GetRenderer(index); | |
3040 | wxGridCellRenderer *rendererOld = renderer; | |
3041 | renderer = renderer->Clone(); | |
3042 | rendererOld->DecRef(); | |
3043 | ||
3044 | wxGridCellEditor *editor = GetEditor(index); | |
3045 | wxGridCellEditor *editorOld = editor; | |
3046 | editor = editor->Clone(); | |
3047 | editorOld->DecRef(); | |
3048 | ||
3049 | // do it even if there are no parameters to reset them to defaults | |
3050 | wxString params = typeName.AfterFirst(_T(':')); | |
3051 | renderer->SetParameters(params); | |
3052 | editor->SetParameters(params); | |
3053 | ||
3054 | // register the new typename | |
3055 | RegisterDataType(typeName, renderer, editor); | |
3056 | ||
3057 | // we just registered it, it's the last one | |
3058 | index = m_typeinfo.GetCount() - 1; | |
3059 | } | |
3060 | ||
3061 | return index; | |
3062 | } | |
3063 | ||
3064 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) | |
3065 | { | |
3066 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; | |
3067 | if (renderer) | |
3068 | renderer->IncRef(); | |
3069 | ||
3070 | return renderer; | |
3071 | } | |
3072 | ||
3073 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) | |
3074 | { | |
3075 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; | |
3076 | if (editor) | |
3077 | editor->IncRef(); | |
3078 | ||
3079 | return editor; | |
3080 | } | |
3081 | ||
3082 | // ---------------------------------------------------------------------------- | |
3083 | // wxGridTableBase | |
3084 | // ---------------------------------------------------------------------------- | |
3085 | ||
3086 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) | |
3087 | ||
3088 | wxGridTableBase::wxGridTableBase() | |
3089 | { | |
3090 | m_view = (wxGrid *) NULL; | |
3091 | m_attrProvider = (wxGridCellAttrProvider *) NULL; | |
3092 | } | |
3093 | ||
3094 | wxGridTableBase::~wxGridTableBase() | |
3095 | { | |
3096 | delete m_attrProvider; | |
3097 | } | |
3098 | ||
3099 | void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider) | |
3100 | { | |
3101 | delete m_attrProvider; | |
3102 | m_attrProvider = attrProvider; | |
3103 | } | |
3104 | ||
3105 | bool wxGridTableBase::CanHaveAttributes() | |
3106 | { | |
3107 | if ( ! GetAttrProvider() ) | |
3108 | { | |
3109 | // use the default attr provider by default | |
3110 | SetAttrProvider(new wxGridCellAttrProvider); | |
3111 | } | |
3112 | ||
3113 | return true; | |
3114 | } | |
3115 | ||
3116 | wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) | |
3117 | { | |
3118 | if ( m_attrProvider ) | |
3119 | return m_attrProvider->GetAttr(row, col, kind); | |
3120 | else | |
3121 | return (wxGridCellAttr *)NULL; | |
3122 | } | |
3123 | ||
3124 | void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) | |
3125 | { | |
3126 | if ( m_attrProvider ) | |
3127 | { | |
3128 | attr->SetKind(wxGridCellAttr::Cell); | |
3129 | m_attrProvider->SetAttr(attr, row, col); | |
3130 | } | |
3131 | else | |
3132 | { | |
3133 | // as we take ownership of the pointer and don't store it, we must | |
3134 | // free it now | |
3135 | wxSafeDecRef(attr); | |
3136 | } | |
3137 | } | |
3138 | ||
3139 | void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) | |
3140 | { | |
3141 | if ( m_attrProvider ) | |
3142 | { | |
3143 | attr->SetKind(wxGridCellAttr::Row); | |
3144 | m_attrProvider->SetRowAttr(attr, row); | |
3145 | } | |
3146 | else | |
3147 | { | |
3148 | // as we take ownership of the pointer and don't store it, we must | |
3149 | // free it now | |
3150 | wxSafeDecRef(attr); | |
3151 | } | |
3152 | } | |
3153 | ||
3154 | void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) | |
3155 | { | |
3156 | if ( m_attrProvider ) | |
3157 | { | |
3158 | attr->SetKind(wxGridCellAttr::Col); | |
3159 | m_attrProvider->SetColAttr(attr, col); | |
3160 | } | |
3161 | else | |
3162 | { | |
3163 | // as we take ownership of the pointer and don't store it, we must | |
3164 | // free it now | |
3165 | wxSafeDecRef(attr); | |
3166 | } | |
3167 | } | |
3168 | ||
3169 | bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos), | |
3170 | size_t WXUNUSED(numRows) ) | |
3171 | { | |
3172 | wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); | |
3173 | ||
3174 | return false; | |
3175 | } | |
3176 | ||
3177 | bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) ) | |
3178 | { | |
3179 | wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); | |
3180 | ||
3181 | return false; | |
3182 | } | |
3183 | ||
3184 | bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos), | |
3185 | size_t WXUNUSED(numRows) ) | |
3186 | { | |
3187 | wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); | |
3188 | ||
3189 | return false; | |
3190 | } | |
3191 | ||
3192 | bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos), | |
3193 | size_t WXUNUSED(numCols) ) | |
3194 | { | |
3195 | wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); | |
3196 | ||
3197 | return false; | |
3198 | } | |
3199 | ||
3200 | bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) ) | |
3201 | { | |
3202 | wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); | |
3203 | ||
3204 | return false; | |
3205 | } | |
3206 | ||
3207 | bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos), | |
3208 | size_t WXUNUSED(numCols) ) | |
3209 | { | |
3210 | wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); | |
3211 | ||
3212 | return false; | |
3213 | } | |
3214 | ||
3215 | wxString wxGridTableBase::GetRowLabelValue( int row ) | |
3216 | { | |
3217 | wxString s; | |
3218 | ||
3219 | // RD: Starting the rows at zero confuses users, | |
3220 | // no matter how much it makes sense to us geeks. | |
3221 | s << row + 1; | |
3222 | ||
3223 | return s; | |
3224 | } | |
3225 | ||
3226 | wxString wxGridTableBase::GetColLabelValue( int col ) | |
3227 | { | |
3228 | // default col labels are: | |
3229 | // cols 0 to 25 : A-Z | |
3230 | // cols 26 to 675 : AA-ZZ | |
3231 | // etc. | |
3232 | ||
3233 | wxString s; | |
3234 | unsigned int i, n; | |
3235 | for ( n = 1; ; n++ ) | |
3236 | { | |
3237 | s += (wxChar) (_T('A') + (wxChar)(col % 26)); | |
3238 | col = col / 26 - 1; | |
3239 | if ( col < 0 ) | |
3240 | break; | |
3241 | } | |
3242 | ||
3243 | // reverse the string... | |
3244 | wxString s2; | |
3245 | for ( i = 0; i < n; i++ ) | |
3246 | { | |
3247 | s2 += s[n - i - 1]; | |
3248 | } | |
3249 | ||
3250 | return s2; | |
3251 | } | |
3252 | ||
3253 | wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) ) | |
3254 | { | |
3255 | return wxGRID_VALUE_STRING; | |
3256 | } | |
3257 | ||
3258 | bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col), | |
3259 | const wxString& typeName ) | |
3260 | { | |
3261 | return typeName == wxGRID_VALUE_STRING; | |
3262 | } | |
3263 | ||
3264 | bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName ) | |
3265 | { | |
3266 | return CanGetValueAs(row, col, typeName); | |
3267 | } | |
3268 | ||
3269 | long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) ) | |
3270 | { | |
3271 | return 0; | |
3272 | } | |
3273 | ||
3274 | double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) ) | |
3275 | { | |
3276 | return 0.0; | |
3277 | } | |
3278 | ||
3279 | bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) ) | |
3280 | { | |
3281 | return false; | |
3282 | } | |
3283 | ||
3284 | void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col), | |
3285 | long WXUNUSED(value) ) | |
3286 | { | |
3287 | } | |
3288 | ||
3289 | void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col), | |
3290 | double WXUNUSED(value) ) | |
3291 | { | |
3292 | } | |
3293 | ||
3294 | void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col), | |
3295 | bool WXUNUSED(value) ) | |
3296 | { | |
3297 | } | |
3298 | ||
3299 | void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3300 | const wxString& WXUNUSED(typeName) ) | |
3301 | { | |
3302 | return NULL; | |
3303 | } | |
3304 | ||
3305 | void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
3306 | const wxString& WXUNUSED(typeName), | |
3307 | void* WXUNUSED(value) ) | |
3308 | { | |
3309 | } | |
3310 | ||
3311 | ////////////////////////////////////////////////////////////////////// | |
3312 | // | |
3313 | // Message class for the grid table to send requests and notifications | |
3314 | // to the grid view | |
3315 | // | |
3316 | ||
3317 | wxGridTableMessage::wxGridTableMessage() | |
3318 | { | |
3319 | m_table = (wxGridTableBase *) NULL; | |
3320 | m_id = -1; | |
3321 | m_comInt1 = -1; | |
3322 | m_comInt2 = -1; | |
3323 | } | |
3324 | ||
3325 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, | |
3326 | int commandInt1, int commandInt2 ) | |
3327 | { | |
3328 | m_table = table; | |
3329 | m_id = id; | |
3330 | m_comInt1 = commandInt1; | |
3331 | m_comInt2 = commandInt2; | |
3332 | } | |
3333 | ||
3334 | ////////////////////////////////////////////////////////////////////// | |
3335 | // | |
3336 | // A basic grid table for string data. An object of this class will | |
3337 | // created by wxGrid if you don't specify an alternative table class. | |
3338 | // | |
3339 | ||
3340 | WX_DEFINE_OBJARRAY(wxGridStringArray) | |
3341 | ||
3342 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) | |
3343 | ||
3344 | wxGridStringTable::wxGridStringTable() | |
3345 | : wxGridTableBase() | |
3346 | { | |
3347 | } | |
3348 | ||
3349 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) | |
3350 | : wxGridTableBase() | |
3351 | { | |
3352 | m_data.Alloc( numRows ); | |
3353 | ||
3354 | wxArrayString sa; | |
3355 | sa.Alloc( numCols ); | |
3356 | sa.Add( wxEmptyString, numCols ); | |
3357 | ||
3358 | m_data.Add( sa, numRows ); | |
3359 | } | |
3360 | ||
3361 | wxGridStringTable::~wxGridStringTable() | |
3362 | { | |
3363 | } | |
3364 | ||
3365 | int wxGridStringTable::GetNumberRows() | |
3366 | { | |
3367 | return m_data.GetCount(); | |
3368 | } | |
3369 | ||
3370 | int wxGridStringTable::GetNumberCols() | |
3371 | { | |
3372 | if ( m_data.GetCount() > 0 ) | |
3373 | return m_data[0].GetCount(); | |
3374 | else | |
3375 | return 0; | |
3376 | } | |
3377 | ||
3378 | wxString wxGridStringTable::GetValue( int row, int col ) | |
3379 | { | |
3380 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), | |
3381 | wxEmptyString, | |
3382 | _T("invalid row or column index in wxGridStringTable") ); | |
3383 | ||
3384 | return m_data[row][col]; | |
3385 | } | |
3386 | ||
3387 | void wxGridStringTable::SetValue( int row, int col, const wxString& value ) | |
3388 | { | |
3389 | wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()), | |
3390 | _T("invalid row or column index in wxGridStringTable") ); | |
3391 | ||
3392 | m_data[row][col] = value; | |
3393 | } | |
3394 | ||
3395 | bool wxGridStringTable::IsEmptyCell( int row, int col ) | |
3396 | { | |
3397 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), | |
3398 | true, | |
3399 | _T("invalid row or column index in wxGridStringTable") ); | |
3400 | ||
3401 | return (m_data[row][col] == wxEmptyString); | |
3402 | } | |
3403 | ||
3404 | void wxGridStringTable::Clear() | |
3405 | { | |
3406 | int row, col; | |
3407 | int numRows, numCols; | |
3408 | ||
3409 | numRows = m_data.GetCount(); | |
3410 | if ( numRows > 0 ) | |
3411 | { | |
3412 | numCols = m_data[0].GetCount(); | |
3413 | ||
3414 | for ( row = 0; row < numRows; row++ ) | |
3415 | { | |
3416 | for ( col = 0; col < numCols; col++ ) | |
3417 | { | |
3418 | m_data[row][col] = wxEmptyString; | |
3419 | } | |
3420 | } | |
3421 | } | |
3422 | } | |
3423 | ||
3424 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) | |
3425 | { | |
3426 | size_t curNumRows = m_data.GetCount(); | |
3427 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : | |
3428 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
3429 | ||
3430 | if ( pos >= curNumRows ) | |
3431 | { | |
3432 | return AppendRows( numRows ); | |
3433 | } | |
3434 | ||
3435 | wxArrayString sa; | |
3436 | sa.Alloc( curNumCols ); | |
3437 | sa.Add( wxEmptyString, curNumCols ); | |
3438 | m_data.Insert( sa, pos, numRows ); | |
3439 | ||
3440 | if ( GetView() ) | |
3441 | { | |
3442 | wxGridTableMessage msg( this, | |
3443 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, | |
3444 | pos, | |
3445 | numRows ); | |
3446 | ||
3447 | GetView()->ProcessTableMessage( msg ); | |
3448 | } | |
3449 | ||
3450 | return true; | |
3451 | } | |
3452 | ||
3453 | bool wxGridStringTable::AppendRows( size_t numRows ) | |
3454 | { | |
3455 | size_t curNumRows = m_data.GetCount(); | |
3456 | size_t curNumCols = ( curNumRows > 0 | |
3457 | ? m_data[0].GetCount() | |
3458 | : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
3459 | ||
3460 | wxArrayString sa; | |
3461 | if ( curNumCols > 0 ) | |
3462 | { | |
3463 | sa.Alloc( curNumCols ); | |
3464 | sa.Add( wxEmptyString, curNumCols ); | |
3465 | } | |
3466 | ||
3467 | m_data.Add( sa, numRows ); | |
3468 | ||
3469 | if ( GetView() ) | |
3470 | { | |
3471 | wxGridTableMessage msg( this, | |
3472 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, | |
3473 | numRows ); | |
3474 | ||
3475 | GetView()->ProcessTableMessage( msg ); | |
3476 | } | |
3477 | ||
3478 | return true; | |
3479 | } | |
3480 | ||
3481 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) | |
3482 | { | |
3483 | size_t curNumRows = m_data.GetCount(); | |
3484 | ||
3485 | if ( pos >= curNumRows ) | |
3486 | { | |
3487 | wxFAIL_MSG( wxString::Format | |
3488 | ( | |
3489 | wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), | |
3490 | (unsigned long)pos, | |
3491 | (unsigned long)numRows, | |
3492 | (unsigned long)curNumRows | |
3493 | ) ); | |
3494 | ||
3495 | return false; | |
3496 | } | |
3497 | ||
3498 | if ( numRows > curNumRows - pos ) | |
3499 | { | |
3500 | numRows = curNumRows - pos; | |
3501 | } | |
3502 | ||
3503 | if ( numRows >= curNumRows ) | |
3504 | { | |
3505 | m_data.Clear(); | |
3506 | } | |
3507 | else | |
3508 | { | |
3509 | m_data.RemoveAt( pos, numRows ); | |
3510 | } | |
3511 | ||
3512 | if ( GetView() ) | |
3513 | { | |
3514 | wxGridTableMessage msg( this, | |
3515 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, | |
3516 | pos, | |
3517 | numRows ); | |
3518 | ||
3519 | GetView()->ProcessTableMessage( msg ); | |
3520 | } | |
3521 | ||
3522 | return true; | |
3523 | } | |
3524 | ||
3525 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) | |
3526 | { | |
3527 | size_t row, col; | |
3528 | ||
3529 | size_t curNumRows = m_data.GetCount(); | |
3530 | size_t curNumCols = ( curNumRows > 0 | |
3531 | ? m_data[0].GetCount() | |
3532 | : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
3533 | ||
3534 | if ( pos >= curNumCols ) | |
3535 | { | |
3536 | return AppendCols( numCols ); | |
3537 | } | |
3538 | ||
3539 | if ( !m_colLabels.IsEmpty() ) | |
3540 | { | |
3541 | m_colLabels.Insert( wxEmptyString, pos, numCols ); | |
3542 | ||
3543 | size_t i; | |
3544 | for ( i = pos; i < pos + numCols; i++ ) | |
3545 | m_colLabels[i] = wxGridTableBase::GetColLabelValue( i ); | |
3546 | } | |
3547 | ||
3548 | for ( row = 0; row < curNumRows; row++ ) | |
3549 | { | |
3550 | for ( col = pos; col < pos + numCols; col++ ) | |
3551 | { | |
3552 | m_data[row].Insert( wxEmptyString, col ); | |
3553 | } | |
3554 | } | |
3555 | ||
3556 | if ( GetView() ) | |
3557 | { | |
3558 | wxGridTableMessage msg( this, | |
3559 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, | |
3560 | pos, | |
3561 | numCols ); | |
3562 | ||
3563 | GetView()->ProcessTableMessage( msg ); | |
3564 | } | |
3565 | ||
3566 | return true; | |
3567 | } | |
3568 | ||
3569 | bool wxGridStringTable::AppendCols( size_t numCols ) | |
3570 | { | |
3571 | size_t row; | |
3572 | ||
3573 | size_t curNumRows = m_data.GetCount(); | |
3574 | ||
3575 | #if 0 | |
3576 | if ( !curNumRows ) | |
3577 | { | |
3578 | // TODO: something better than this ? | |
3579 | // | |
3580 | wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") ); | |
3581 | return false; | |
3582 | } | |
3583 | #endif | |
3584 | ||
3585 | for ( row = 0; row < curNumRows; row++ ) | |
3586 | { | |
3587 | m_data[row].Add( wxEmptyString, numCols ); | |
3588 | } | |
3589 | ||
3590 | if ( GetView() ) | |
3591 | { | |
3592 | wxGridTableMessage msg( this, | |
3593 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, | |
3594 | numCols ); | |
3595 | ||
3596 | GetView()->ProcessTableMessage( msg ); | |
3597 | } | |
3598 | ||
3599 | return true; | |
3600 | } | |
3601 | ||
3602 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) | |
3603 | { | |
3604 | size_t row; | |
3605 | ||
3606 | size_t curNumRows = m_data.GetCount(); | |
3607 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : | |
3608 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
3609 | ||
3610 | if ( pos >= curNumCols ) | |
3611 | { | |
3612 | wxFAIL_MSG( wxString::Format | |
3613 | ( | |
3614 | wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), | |
3615 | (unsigned long)pos, | |
3616 | (unsigned long)numCols, | |
3617 | (unsigned long)curNumCols | |
3618 | ) ); | |
3619 | return false; | |
3620 | } | |
3621 | ||
3622 | int colID; | |
3623 | if ( GetView() ) | |
3624 | colID = GetView()->GetColAt( pos ); | |
3625 | else | |
3626 | colID = pos; | |
3627 | ||
3628 | if ( numCols > curNumCols - colID ) | |
3629 | { | |
3630 | numCols = curNumCols - colID; | |
3631 | } | |
3632 | ||
3633 | if ( !m_colLabels.IsEmpty() ) | |
3634 | { | |
3635 | m_colLabels.RemoveAt( colID, numCols ); | |
3636 | } | |
3637 | ||
3638 | for ( row = 0; row < curNumRows; row++ ) | |
3639 | { | |
3640 | if ( numCols >= curNumCols ) | |
3641 | { | |
3642 | m_data[row].Clear(); | |
3643 | } | |
3644 | else | |
3645 | { | |
3646 | m_data[row].RemoveAt( colID, numCols ); | |
3647 | } | |
3648 | } | |
3649 | ||
3650 | if ( GetView() ) | |
3651 | { | |
3652 | wxGridTableMessage msg( this, | |
3653 | wxGRIDTABLE_NOTIFY_COLS_DELETED, | |
3654 | pos, | |
3655 | numCols ); | |
3656 | ||
3657 | GetView()->ProcessTableMessage( msg ); | |
3658 | } | |
3659 | ||
3660 | return true; | |
3661 | } | |
3662 | ||
3663 | wxString wxGridStringTable::GetRowLabelValue( int row ) | |
3664 | { | |
3665 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3666 | { | |
3667 | // using default label | |
3668 | // | |
3669 | return wxGridTableBase::GetRowLabelValue( row ); | |
3670 | } | |
3671 | else | |
3672 | { | |
3673 | return m_rowLabels[row]; | |
3674 | } | |
3675 | } | |
3676 | ||
3677 | wxString wxGridStringTable::GetColLabelValue( int col ) | |
3678 | { | |
3679 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3680 | { | |
3681 | // using default label | |
3682 | // | |
3683 | return wxGridTableBase::GetColLabelValue( col ); | |
3684 | } | |
3685 | else | |
3686 | { | |
3687 | return m_colLabels[col]; | |
3688 | } | |
3689 | } | |
3690 | ||
3691 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) | |
3692 | { | |
3693 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
3694 | { | |
3695 | int n = m_rowLabels.GetCount(); | |
3696 | int i; | |
3697 | ||
3698 | for ( i = n; i <= row; i++ ) | |
3699 | { | |
3700 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); | |
3701 | } | |
3702 | } | |
3703 | ||
3704 | m_rowLabels[row] = value; | |
3705 | } | |
3706 | ||
3707 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) | |
3708 | { | |
3709 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
3710 | { | |
3711 | int n = m_colLabels.GetCount(); | |
3712 | int i; | |
3713 | ||
3714 | for ( i = n; i <= col; i++ ) | |
3715 | { | |
3716 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); | |
3717 | } | |
3718 | } | |
3719 | ||
3720 | m_colLabels[col] = value; | |
3721 | } | |
3722 | ||
3723 | ||
3724 | ////////////////////////////////////////////////////////////////////// | |
3725 | ////////////////////////////////////////////////////////////////////// | |
3726 | ||
3727 | IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow ) | |
3728 | ||
3729 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow ) | |
3730 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) | |
3731 | EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel ) | |
3732 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) | |
3733 | EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown ) | |
3734 | EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp ) | |
3735 | EVT_CHAR( wxGridRowLabelWindow::OnChar ) | |
3736 | END_EVENT_TABLE() | |
3737 | ||
3738 | wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent, | |
3739 | wxWindowID id, | |
3740 | const wxPoint &pos, const wxSize &size ) | |
3741 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE ) | |
3742 | { | |
3743 | m_owner = parent; | |
3744 | } | |
3745 | ||
3746 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
3747 | { | |
3748 | wxPaintDC dc(this); | |
3749 | ||
3750 | // NO - don't do this because it will set both the x and y origin | |
3751 | // coords to match the parent scrolled window and we just want to | |
3752 | // set the y coord - MB | |
3753 | // | |
3754 | // m_owner->PrepareDC( dc ); | |
3755 | ||
3756 | int x, y; | |
3757 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); | |
3758 | wxPoint pt = dc.GetDeviceOrigin(); | |
3759 | dc.SetDeviceOrigin( pt.x, pt.y-y ); | |
3760 | ||
3761 | wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); | |
3762 | m_owner->DrawRowLabels( dc, rows ); | |
3763 | } | |
3764 | ||
3765 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3766 | { | |
3767 | m_owner->ProcessRowLabelMouseEvent( event ); | |
3768 | } | |
3769 | ||
3770 | void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event ) | |
3771 | { | |
3772 | m_owner->GetEventHandler()->ProcessEvent( event ); | |
3773 | } | |
3774 | ||
3775 | // This seems to be required for wxMotif otherwise the mouse | |
3776 | // cursor must be in the cell edit control to get key events | |
3777 | // | |
3778 | void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3779 | { | |
3780 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3781 | event.Skip(); | |
3782 | } | |
3783 | ||
3784 | void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event ) | |
3785 | { | |
3786 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3787 | event.Skip(); | |
3788 | } | |
3789 | ||
3790 | void wxGridRowLabelWindow::OnChar( wxKeyEvent& event ) | |
3791 | { | |
3792 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3793 | event.Skip(); | |
3794 | } | |
3795 | ||
3796 | ////////////////////////////////////////////////////////////////////// | |
3797 | ||
3798 | IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow ) | |
3799 | ||
3800 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow ) | |
3801 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) | |
3802 | EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel ) | |
3803 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) | |
3804 | EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown ) | |
3805 | EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp ) | |
3806 | EVT_CHAR( wxGridColLabelWindow::OnChar ) | |
3807 | END_EVENT_TABLE() | |
3808 | ||
3809 | wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent, | |
3810 | wxWindowID id, | |
3811 | const wxPoint &pos, const wxSize &size ) | |
3812 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE ) | |
3813 | { | |
3814 | m_owner = parent; | |
3815 | } | |
3816 | ||
3817 | void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
3818 | { | |
3819 | wxPaintDC dc(this); | |
3820 | ||
3821 | // NO - don't do this because it will set both the x and y origin | |
3822 | // coords to match the parent scrolled window and we just want to | |
3823 | // set the x coord - MB | |
3824 | // | |
3825 | // m_owner->PrepareDC( dc ); | |
3826 | ||
3827 | int x, y; | |
3828 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); | |
3829 | wxPoint pt = dc.GetDeviceOrigin(); | |
3830 | if (GetLayoutDirection() == wxLayout_RightToLeft) | |
3831 | dc.SetDeviceOrigin( pt.x+x, pt.y ); | |
3832 | else | |
3833 | dc.SetDeviceOrigin( pt.x-x, pt.y ); | |
3834 | ||
3835 | wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() ); | |
3836 | m_owner->DrawColLabels( dc, cols ); | |
3837 | } | |
3838 | ||
3839 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3840 | { | |
3841 | m_owner->ProcessColLabelMouseEvent( event ); | |
3842 | } | |
3843 | ||
3844 | void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event ) | |
3845 | { | |
3846 | m_owner->GetEventHandler()->ProcessEvent( event ); | |
3847 | } | |
3848 | ||
3849 | // This seems to be required for wxMotif otherwise the mouse | |
3850 | // cursor must be in the cell edit control to get key events | |
3851 | // | |
3852 | void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3853 | { | |
3854 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3855 | event.Skip(); | |
3856 | } | |
3857 | ||
3858 | void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event ) | |
3859 | { | |
3860 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3861 | event.Skip(); | |
3862 | } | |
3863 | ||
3864 | void wxGridColLabelWindow::OnChar( wxKeyEvent& event ) | |
3865 | { | |
3866 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3867 | event.Skip(); | |
3868 | } | |
3869 | ||
3870 | ////////////////////////////////////////////////////////////////////// | |
3871 | ||
3872 | IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow ) | |
3873 | ||
3874 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow ) | |
3875 | EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel ) | |
3876 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) | |
3877 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint ) | |
3878 | EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown ) | |
3879 | EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp ) | |
3880 | EVT_CHAR( wxGridCornerLabelWindow::OnChar ) | |
3881 | END_EVENT_TABLE() | |
3882 | ||
3883 | wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent, | |
3884 | wxWindowID id, | |
3885 | const wxPoint &pos, const wxSize &size ) | |
3886 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE ) | |
3887 | { | |
3888 | m_owner = parent; | |
3889 | } | |
3890 | ||
3891 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
3892 | { | |
3893 | wxPaintDC dc(this); | |
3894 | ||
3895 | int client_height = 0; | |
3896 | int client_width = 0; | |
3897 | GetClientSize( &client_width, &client_height ); | |
3898 | ||
3899 | // VZ: any reason for this ifdef? (FIXME) | |
3900 | #if 0 | |
3901 | def __WXGTK__ | |
3902 | wxRect rect; | |
3903 | rect.SetX( 1 ); | |
3904 | rect.SetY( 1 ); | |
3905 | rect.SetWidth( client_width - 2 ); | |
3906 | rect.SetHeight( client_height - 2 ); | |
3907 | ||
3908 | wxRendererNative::Get().DrawHeaderButton( this, dc, rect, 0 ); | |
3909 | #else // !__WXGTK__ | |
3910 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) ); | |
3911 | dc.DrawLine( client_width - 1, client_height - 1, client_width - 1, 0 ); | |
3912 | dc.DrawLine( client_width - 1, client_height - 1, 0, client_height - 1 ); | |
3913 | dc.DrawLine( 0, 0, client_width, 0 ); | |
3914 | dc.DrawLine( 0, 0, 0, client_height ); | |
3915 | ||
3916 | dc.SetPen( *wxWHITE_PEN ); | |
3917 | dc.DrawLine( 1, 1, client_width - 1, 1 ); | |
3918 | dc.DrawLine( 1, 1, 1, client_height - 1 ); | |
3919 | #endif | |
3920 | } | |
3921 | ||
3922 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
3923 | { | |
3924 | m_owner->ProcessCornerLabelMouseEvent( event ); | |
3925 | } | |
3926 | ||
3927 | void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event ) | |
3928 | { | |
3929 | m_owner->GetEventHandler()->ProcessEvent(event); | |
3930 | } | |
3931 | ||
3932 | // This seems to be required for wxMotif otherwise the mouse | |
3933 | // cursor must be in the cell edit control to get key events | |
3934 | // | |
3935 | void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
3936 | { | |
3937 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3938 | event.Skip(); | |
3939 | } | |
3940 | ||
3941 | void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event ) | |
3942 | { | |
3943 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3944 | event.Skip(); | |
3945 | } | |
3946 | ||
3947 | void wxGridCornerLabelWindow::OnChar( wxKeyEvent& event ) | |
3948 | { | |
3949 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
3950 | event.Skip(); | |
3951 | } | |
3952 | ||
3953 | ////////////////////////////////////////////////////////////////////// | |
3954 | ||
3955 | IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow ) | |
3956 | ||
3957 | BEGIN_EVENT_TABLE( wxGridWindow, wxWindow ) | |
3958 | EVT_PAINT( wxGridWindow::OnPaint ) | |
3959 | EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel ) | |
3960 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) | |
3961 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) | |
3962 | EVT_KEY_UP( wxGridWindow::OnKeyUp ) | |
3963 | EVT_CHAR( wxGridWindow::OnChar ) | |
3964 | EVT_SET_FOCUS( wxGridWindow::OnFocus ) | |
3965 | EVT_KILL_FOCUS( wxGridWindow::OnFocus ) | |
3966 | EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground ) | |
3967 | END_EVENT_TABLE() | |
3968 | ||
3969 | wxGridWindow::wxGridWindow( wxGrid *parent, | |
3970 | wxGridRowLabelWindow *rowLblWin, | |
3971 | wxGridColLabelWindow *colLblWin, | |
3972 | wxWindowID id, | |
3973 | const wxPoint &pos, | |
3974 | const wxSize &size ) | |
3975 | : wxWindow( | |
3976 | parent, id, pos, size, | |
3977 | wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN | wxFULL_REPAINT_ON_RESIZE, | |
3978 | wxT("grid window") ) | |
3979 | { | |
3980 | m_owner = parent; | |
3981 | m_rowLabelWin = rowLblWin; | |
3982 | m_colLabelWin = colLblWin; | |
3983 | } | |
3984 | ||
3985 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
3986 | { | |
3987 | wxPaintDC dc( this ); | |
3988 | m_owner->PrepareDC( dc ); | |
3989 | wxRegion reg = GetUpdateRegion(); | |
3990 | wxGridCellCoordsArray dirtyCells = m_owner->CalcCellsExposed( reg ); | |
3991 | m_owner->DrawGridCellArea( dc, dirtyCells ); | |
3992 | ||
3993 | #if WXGRID_DRAW_LINES | |
3994 | m_owner->DrawAllGridLines( dc, reg ); | |
3995 | #endif | |
3996 | ||
3997 | m_owner->DrawGridSpace( dc ); | |
3998 | m_owner->DrawHighlight( dc, dirtyCells ); | |
3999 | } | |
4000 | ||
4001 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
4002 | { | |
4003 | wxWindow::ScrollWindow( dx, dy, rect ); | |
4004 | m_rowLabelWin->ScrollWindow( 0, dy, rect ); | |
4005 | m_colLabelWin->ScrollWindow( dx, 0, rect ); | |
4006 | } | |
4007 | ||
4008 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) | |
4009 | { | |
4010 | if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this) | |
4011 | SetFocus(); | |
4012 | ||
4013 | m_owner->ProcessGridCellMouseEvent( event ); | |
4014 | } | |
4015 | ||
4016 | void wxGridWindow::OnMouseWheel( wxMouseEvent& event ) | |
4017 | { | |
4018 | m_owner->GetEventHandler()->ProcessEvent( event ); | |
4019 | } | |
4020 | ||
4021 | // This seems to be required for wxMotif/wxGTK otherwise the mouse | |
4022 | // cursor must be in the cell edit control to get key events | |
4023 | // | |
4024 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) | |
4025 | { | |
4026 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
4027 | event.Skip(); | |
4028 | } | |
4029 | ||
4030 | void wxGridWindow::OnKeyUp( wxKeyEvent& event ) | |
4031 | { | |
4032 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
4033 | event.Skip(); | |
4034 | } | |
4035 | ||
4036 | void wxGridWindow::OnChar( wxKeyEvent& event ) | |
4037 | { | |
4038 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
4039 | event.Skip(); | |
4040 | } | |
4041 | ||
4042 | void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) | |
4043 | { | |
4044 | } | |
4045 | ||
4046 | void wxGridWindow::OnFocus(wxFocusEvent& event) | |
4047 | { | |
4048 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) | |
4049 | event.Skip(); | |
4050 | } | |
4051 | ||
4052 | ////////////////////////////////////////////////////////////////////// | |
4053 | ||
4054 | // Internal Helper function for computing row or column from some | |
4055 | // (unscrolled) coordinate value, using either | |
4056 | // m_defaultRowHeight/m_defaultColWidth or binary search on array | |
4057 | // of m_rowBottoms/m_ColRights to speed up the search! | |
4058 | ||
4059 | // Internal helper macros for simpler use of that function | |
4060 | ||
4061 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, | |
4062 | const wxArrayInt& BorderArray, int nMax, | |
4063 | bool clipToMinMax); | |
4064 | ||
4065 | #define internalXToCol(x) XToCol(x, true) | |
4066 | #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \ | |
4067 | m_minAcceptableRowHeight, \ | |
4068 | m_rowBottoms, m_numRows, true) | |
4069 | ||
4070 | ///////////////////////////////////////////////////////////////////// | |
4071 | ||
4072 | #if wxUSE_EXTENDED_RTTI | |
4073 | WX_DEFINE_FLAGS( wxGridStyle ) | |
4074 | ||
4075 | wxBEGIN_FLAGS( wxGridStyle ) | |
4076 | // new style border flags, we put them first to | |
4077 | // use them for streaming out | |
4078 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
4079 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
4080 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
4081 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
4082 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
4083 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
4084 | ||
4085 | // old style border flags | |
4086 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
4087 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
4088 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
4089 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
4090 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
4091 | wxFLAGS_MEMBER(wxBORDER) | |
4092 | ||
4093 | // standard window styles | |
4094 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
4095 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
4096 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
4097 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
4098 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
4099 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB) | |
4100 | wxFLAGS_MEMBER(wxVSCROLL) | |
4101 | wxFLAGS_MEMBER(wxHSCROLL) | |
4102 | ||
4103 | wxEND_FLAGS( wxGridStyle ) | |
4104 | ||
4105 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h") | |
4106 | ||
4107 | wxBEGIN_PROPERTIES_TABLE(wxGrid) | |
4108 | wxHIDE_PROPERTY( Children ) | |
4109 | wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
4110 | wxEND_PROPERTIES_TABLE() | |
4111 | ||
4112 | wxBEGIN_HANDLERS_TABLE(wxGrid) | |
4113 | wxEND_HANDLERS_TABLE() | |
4114 | ||
4115 | wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) | |
4116 | ||
4117 | /* | |
4118 | TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo) | |
4119 | */ | |
4120 | #else | |
4121 | IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow ) | |
4122 | #endif | |
4123 | ||
4124 | BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow ) | |
4125 | EVT_PAINT( wxGrid::OnPaint ) | |
4126 | EVT_SIZE( wxGrid::OnSize ) | |
4127 | EVT_KEY_DOWN( wxGrid::OnKeyDown ) | |
4128 | EVT_KEY_UP( wxGrid::OnKeyUp ) | |
4129 | EVT_CHAR ( wxGrid::OnChar ) | |
4130 | EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground ) | |
4131 | END_EVENT_TABLE() | |
4132 | ||
4133 | wxGrid::wxGrid() | |
4134 | { | |
4135 | // in order to make sure that a size event is not | |
4136 | // trigerred in a unfinished state | |
4137 | m_cornerLabelWin = NULL; | |
4138 | m_rowLabelWin = NULL; | |
4139 | m_colLabelWin = NULL; | |
4140 | m_gridWin = NULL; | |
4141 | } | |
4142 | ||
4143 | wxGrid::wxGrid( wxWindow *parent, | |
4144 | wxWindowID id, | |
4145 | const wxPoint& pos, | |
4146 | const wxSize& size, | |
4147 | long style, | |
4148 | const wxString& name ) | |
4149 | : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ), | |
4150 | m_colMinWidths(GRID_HASH_SIZE), | |
4151 | m_rowMinHeights(GRID_HASH_SIZE) | |
4152 | { | |
4153 | Create(); | |
4154 | SetBestFittingSize(size); | |
4155 | } | |
4156 | ||
4157 | bool wxGrid::Create(wxWindow *parent, wxWindowID id, | |
4158 | const wxPoint& pos, const wxSize& size, | |
4159 | long style, const wxString& name) | |
4160 | { | |
4161 | if (!wxScrolledWindow::Create(parent, id, pos, size, | |
4162 | style | wxWANTS_CHARS, name)) | |
4163 | return false; | |
4164 | ||
4165 | m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE); | |
4166 | m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE); | |
4167 | ||
4168 | Create(); | |
4169 | SetBestFittingSize(size); | |
4170 | ||
4171 | return true; | |
4172 | } | |
4173 | ||
4174 | wxGrid::~wxGrid() | |
4175 | { | |
4176 | // Must do this or ~wxScrollHelper will pop the wrong event handler | |
4177 | SetTargetWindow(this); | |
4178 | ClearAttrCache(); | |
4179 | wxSafeDecRef(m_defaultCellAttr); | |
4180 | ||
4181 | #ifdef DEBUG_ATTR_CACHE | |
4182 | size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses; | |
4183 | wxPrintf(_T("wxGrid attribute cache statistics: " | |
4184 | "total: %u, hits: %u (%u%%)\n"), | |
4185 | total, gs_nAttrCacheHits, | |
4186 | total ? (gs_nAttrCacheHits*100) / total : 0); | |
4187 | #endif | |
4188 | ||
4189 | if (m_ownTable) | |
4190 | delete m_table; | |
4191 | ||
4192 | delete m_typeRegistry; | |
4193 | delete m_selection; | |
4194 | } | |
4195 | ||
4196 | // | |
4197 | // ----- internal init and update functions | |
4198 | // | |
4199 | ||
4200 | // NOTE: If using the default visual attributes works everywhere then this can | |
4201 | // be removed as well as the #else cases below. | |
4202 | #define _USE_VISATTR 0 | |
4203 | ||
4204 | void wxGrid::Create() | |
4205 | { | |
4206 | // set to true by CreateGrid | |
4207 | m_created = false; | |
4208 | ||
4209 | // create the type registry | |
4210 | m_typeRegistry = new wxGridTypeRegistry; | |
4211 | m_selection = NULL; | |
4212 | ||
4213 | m_table = (wxGridTableBase *) NULL; | |
4214 | m_ownTable = false; | |
4215 | ||
4216 | m_cellEditCtrlEnabled = false; | |
4217 | ||
4218 | m_defaultCellAttr = new wxGridCellAttr(); | |
4219 | ||
4220 | // Set default cell attributes | |
4221 | m_defaultCellAttr->SetDefAttr(m_defaultCellAttr); | |
4222 | m_defaultCellAttr->SetKind(wxGridCellAttr::Default); | |
4223 | m_defaultCellAttr->SetFont(GetFont()); | |
4224 | m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP); | |
4225 | m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer); | |
4226 | m_defaultCellAttr->SetEditor(new wxGridCellTextEditor); | |
4227 | ||
4228 | #if _USE_VISATTR | |
4229 | wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes(); | |
4230 | wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes(); | |
4231 | ||
4232 | m_defaultCellAttr->SetTextColour(gva.colFg); | |
4233 | m_defaultCellAttr->SetBackgroundColour(gva.colBg); | |
4234 | ||
4235 | #else | |
4236 | m_defaultCellAttr->SetTextColour( | |
4237 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); | |
4238 | m_defaultCellAttr->SetBackgroundColour( | |
4239 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
4240 | #endif | |
4241 | ||
4242 | m_numRows = 0; | |
4243 | m_numCols = 0; | |
4244 | m_currentCellCoords = wxGridNoCellCoords; | |
4245 | ||
4246 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; | |
4247 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
4248 | ||
4249 | // subwindow components that make up the wxGrid | |
4250 | m_rowLabelWin = new wxGridRowLabelWindow( this, | |
4251 | wxID_ANY, | |
4252 | wxDefaultPosition, | |
4253 | wxDefaultSize ); | |
4254 | ||
4255 | m_colLabelWin = new wxGridColLabelWindow( this, | |
4256 | wxID_ANY, | |
4257 | wxDefaultPosition, | |
4258 | wxDefaultSize ); | |
4259 | ||
4260 | m_cornerLabelWin = new wxGridCornerLabelWindow( this, | |
4261 | wxID_ANY, | |
4262 | wxDefaultPosition, | |
4263 | wxDefaultSize ); | |
4264 | ||
4265 | m_gridWin = new wxGridWindow( this, | |
4266 | m_rowLabelWin, | |
4267 | m_colLabelWin, | |
4268 | wxID_ANY, | |
4269 | wxDefaultPosition, | |
4270 | wxDefaultSize ); | |
4271 | ||
4272 | SetTargetWindow( m_gridWin ); | |
4273 | ||
4274 | #if _USE_VISATTR | |
4275 | wxColour gfg = gva.colFg; | |
4276 | wxColour gbg = gva.colBg; | |
4277 | wxColour lfg = lva.colFg; | |
4278 | wxColour lbg = lva.colBg; | |
4279 | #else | |
4280 | wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
4281 | wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ); | |
4282 | wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
4283 | wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ); | |
4284 | #endif | |
4285 | ||
4286 | m_cornerLabelWin->SetOwnForegroundColour(lfg); | |
4287 | m_cornerLabelWin->SetOwnBackgroundColour(lbg); | |
4288 | m_rowLabelWin->SetOwnForegroundColour(lfg); | |
4289 | m_rowLabelWin->SetOwnBackgroundColour(lbg); | |
4290 | m_colLabelWin->SetOwnForegroundColour(lfg); | |
4291 | m_colLabelWin->SetOwnBackgroundColour(lbg); | |
4292 | ||
4293 | m_gridWin->SetOwnForegroundColour(gfg); | |
4294 | m_gridWin->SetOwnBackgroundColour(gbg); | |
4295 | ||
4296 | Init(); | |
4297 | } | |
4298 | ||
4299 | bool wxGrid::CreateGrid( int numRows, int numCols, | |
4300 | wxGrid::wxGridSelectionModes selmode ) | |
4301 | { | |
4302 | wxCHECK_MSG( !m_created, | |
4303 | false, | |
4304 | wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); | |
4305 | ||
4306 | m_numRows = numRows; | |
4307 | m_numCols = numCols; | |
4308 | ||
4309 | m_table = new wxGridStringTable( m_numRows, m_numCols ); | |
4310 | m_table->SetView( this ); | |
4311 | m_ownTable = true; | |
4312 | m_selection = new wxGridSelection( this, selmode ); | |
4313 | ||
4314 | CalcDimensions(); | |
4315 | ||
4316 | m_created = true; | |
4317 | ||
4318 | return m_created; | |
4319 | } | |
4320 | ||
4321 | void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode) | |
4322 | { | |
4323 | wxCHECK_RET( m_created, | |
4324 | wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") ); | |
4325 | ||
4326 | m_selection->SetSelectionMode( selmode ); | |
4327 | } | |
4328 | ||
4329 | wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const | |
4330 | { | |
4331 | wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells, | |
4332 | wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") ); | |
4333 | ||
4334 | return m_selection->GetSelectionMode(); | |
4335 | } | |
4336 | ||
4337 | bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership, | |
4338 | wxGrid::wxGridSelectionModes selmode ) | |
4339 | { | |
4340 | if ( m_created ) | |
4341 | { | |
4342 | // stop all processing | |
4343 | m_created = false; | |
4344 | ||
4345 | if (m_ownTable) | |
4346 | { | |
4347 | wxGridTableBase *t = m_table; | |
4348 | m_table = NULL; | |
4349 | delete t; | |
4350 | } | |
4351 | ||
4352 | delete m_selection; | |
4353 | ||
4354 | m_table = NULL; | |
4355 | m_selection = NULL; | |
4356 | m_numRows = 0; | |
4357 | m_numCols = 0; | |
4358 | } | |
4359 | ||
4360 | if (table) | |
4361 | { | |
4362 | m_numRows = table->GetNumberRows(); | |
4363 | m_numCols = table->GetNumberCols(); | |
4364 | ||
4365 | m_table = table; | |
4366 | m_table->SetView( this ); | |
4367 | m_ownTable = takeOwnership; | |
4368 | m_selection = new wxGridSelection( this, selmode ); | |
4369 | ||
4370 | CalcDimensions(); | |
4371 | ||
4372 | m_created = true; | |
4373 | } | |
4374 | ||
4375 | return m_created; | |
4376 | } | |
4377 | ||
4378 | void wxGrid::Init() | |
4379 | { | |
4380 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; | |
4381 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
4382 | ||
4383 | if ( m_rowLabelWin ) | |
4384 | { | |
4385 | m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour(); | |
4386 | } | |
4387 | else | |
4388 | { | |
4389 | m_labelBackgroundColour = *wxWHITE; | |
4390 | } | |
4391 | ||
4392 | m_labelTextColour = *wxBLACK; | |
4393 | ||
4394 | // init attr cache | |
4395 | m_attrCache.row = -1; | |
4396 | m_attrCache.col = -1; | |
4397 | m_attrCache.attr = NULL; | |
4398 | ||
4399 | // TODO: something better than this ? | |
4400 | // | |
4401 | m_labelFont = this->GetFont(); | |
4402 | m_labelFont.SetWeight( wxBOLD ); | |
4403 | ||
4404 | m_rowLabelHorizAlign = wxALIGN_CENTRE; | |
4405 | m_rowLabelVertAlign = wxALIGN_CENTRE; | |
4406 | ||
4407 | m_colLabelHorizAlign = wxALIGN_CENTRE; | |
4408 | m_colLabelVertAlign = wxALIGN_CENTRE; | |
4409 | m_colLabelTextOrientation = wxHORIZONTAL; | |
4410 | ||
4411 | m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH; | |
4412 | m_defaultRowHeight = m_gridWin->GetCharHeight(); | |
4413 | ||
4414 | m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH; | |
4415 | m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT; | |
4416 | ||
4417 | #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl() | |
4418 | m_defaultRowHeight += 8; | |
4419 | #else | |
4420 | m_defaultRowHeight += 4; | |
4421 | #endif | |
4422 | ||
4423 | m_gridLineColour = wxColour( 192,192,192 ); | |
4424 | m_gridLinesEnabled = true; | |
4425 | m_cellHighlightColour = *wxBLACK; | |
4426 | m_cellHighlightPenWidth = 2; | |
4427 | m_cellHighlightROPenWidth = 1; | |
4428 | ||
4429 | m_canDragColMove = false; | |
4430 | ||
4431 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; | |
4432 | m_winCapture = (wxWindow *)NULL; | |
4433 | m_canDragRowSize = true; | |
4434 | m_canDragColSize = true; | |
4435 | m_canDragGridSize = true; | |
4436 | m_canDragCell = false; | |
4437 | m_dragLastPos = -1; | |
4438 | m_dragRowOrCol = -1; | |
4439 | m_isDragging = false; | |
4440 | m_startDragPos = wxDefaultPosition; | |
4441 | ||
4442 | m_waitForSlowClick = false; | |
4443 | ||
4444 | m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS ); | |
4445 | m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE ); | |
4446 | ||
4447 | m_currentCellCoords = wxGridNoCellCoords; | |
4448 | ||
4449 | ClearSelection(); | |
4450 | ||
4451 | m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); | |
4452 | m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
4453 | ||
4454 | m_editable = true; // default for whole grid | |
4455 | ||
4456 | m_inOnKeyDown = false; | |
4457 | m_batchCount = 0; | |
4458 | ||
4459 | m_extraWidth = | |
4460 | m_extraHeight = 0; | |
4461 | ||
4462 | m_scrollLineX = GRID_SCROLL_LINE_X; | |
4463 | m_scrollLineY = GRID_SCROLL_LINE_Y; | |
4464 | } | |
4465 | ||
4466 | // ---------------------------------------------------------------------------- | |
4467 | // the idea is to call these functions only when necessary because they create | |
4468 | // quite big arrays which eat memory mostly unnecessary - in particular, if | |
4469 | // default widths/heights are used for all rows/columns, we may not use these | |
4470 | // arrays at all | |
4471 | // | |
4472 | // with some extra code, it should be possible to only store the | |
4473 | // widths/heights different from default ones but this will be done later... | |
4474 | // ---------------------------------------------------------------------------- | |
4475 | ||
4476 | void wxGrid::InitRowHeights() | |
4477 | { | |
4478 | m_rowHeights.Empty(); | |
4479 | m_rowBottoms.Empty(); | |
4480 | ||
4481 | m_rowHeights.Alloc( m_numRows ); | |
4482 | m_rowBottoms.Alloc( m_numRows ); | |
4483 | ||
4484 | int rowBottom = 0; | |
4485 | ||
4486 | m_rowHeights.Add( m_defaultRowHeight, m_numRows ); | |
4487 | ||
4488 | for ( int i = 0; i < m_numRows; i++ ) | |
4489 | { | |
4490 | rowBottom += m_defaultRowHeight; | |
4491 | m_rowBottoms.Add( rowBottom ); | |
4492 | } | |
4493 | } | |
4494 | ||
4495 | void wxGrid::InitColWidths() | |
4496 | { | |
4497 | m_colWidths.Empty(); | |
4498 | m_colRights.Empty(); | |
4499 | ||
4500 | m_colWidths.Alloc( m_numCols ); | |
4501 | m_colRights.Alloc( m_numCols ); | |
4502 | int colRight = 0; | |
4503 | ||
4504 | m_colWidths.Add( m_defaultColWidth, m_numCols ); | |
4505 | ||
4506 | for ( int i = 0; i < m_numCols; i++ ) | |
4507 | { | |
4508 | colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth; | |
4509 | m_colRights.Add( colRight ); | |
4510 | } | |
4511 | } | |
4512 | ||
4513 | int wxGrid::GetColWidth(int col) const | |
4514 | { | |
4515 | return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col]; | |
4516 | } | |
4517 | ||
4518 | int wxGrid::GetColLeft(int col) const | |
4519 | { | |
4520 | return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth | |
4521 | : m_colRights[col] - m_colWidths[col]; | |
4522 | } | |
4523 | ||
4524 | int wxGrid::GetColRight(int col) const | |
4525 | { | |
4526 | return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth | |
4527 | : m_colRights[col]; | |
4528 | } | |
4529 | ||
4530 | int wxGrid::GetRowHeight(int row) const | |
4531 | { | |
4532 | return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row]; | |
4533 | } | |
4534 | ||
4535 | int wxGrid::GetRowTop(int row) const | |
4536 | { | |
4537 | return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight | |
4538 | : m_rowBottoms[row] - m_rowHeights[row]; | |
4539 | } | |
4540 | ||
4541 | int wxGrid::GetRowBottom(int row) const | |
4542 | { | |
4543 | return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight | |
4544 | : m_rowBottoms[row]; | |
4545 | } | |
4546 | ||
4547 | void wxGrid::CalcDimensions() | |
4548 | { | |
4549 | int cw, ch; | |
4550 | GetClientSize( &cw, &ch ); | |
4551 | ||
4552 | if ( m_rowLabelWin->IsShown() ) | |
4553 | cw -= m_rowLabelWidth; | |
4554 | if ( m_colLabelWin->IsShown() ) | |
4555 | ch -= m_colLabelHeight; | |
4556 | ||
4557 | // grid total size | |
4558 | int w = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) + m_extraWidth + 1 : 0; | |
4559 | int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0; | |
4560 | ||
4561 | // take into account editor if shown | |
4562 | if ( IsCellEditControlShown() ) | |
4563 | { | |
4564 | int w2, h2; | |
4565 | int r = m_currentCellCoords.GetRow(); | |
4566 | int c = m_currentCellCoords.GetCol(); | |
4567 | int x = GetColLeft(c); | |
4568 | int y = GetRowTop(r); | |
4569 | ||
4570 | // how big is the editor | |
4571 | wxGridCellAttr* attr = GetCellAttr(r, c); | |
4572 | wxGridCellEditor* editor = attr->GetEditor(this, r, c); | |
4573 | editor->GetControl()->GetSize(&w2, &h2); | |
4574 | w2 += x; | |
4575 | h2 += y; | |
4576 | if ( w2 > w ) | |
4577 | w = w2; | |
4578 | if ( h2 > h ) | |
4579 | h = h2; | |
4580 | editor->DecRef(); | |
4581 | attr->DecRef(); | |
4582 | } | |
4583 | ||
4584 | // preserve (more or less) the previous position | |
4585 | int x, y; | |
4586 | GetViewStart( &x, &y ); | |
4587 | ||
4588 | // ensure the position is valid for the new scroll ranges | |
4589 | if ( x >= w ) | |
4590 | x = wxMax( w - 1, 0 ); | |
4591 | if ( y >= h ) | |
4592 | y = wxMax( h - 1, 0 ); | |
4593 | ||
4594 | // do set scrollbar parameters | |
4595 | SetScrollbars( m_scrollLineX, m_scrollLineY, | |
4596 | GetScrollX(w), GetScrollY(h), x, y, | |
4597 | GetBatchCount() != 0); | |
4598 | ||
4599 | // if our OnSize() hadn't been called (it would if we have scrollbars), we | |
4600 | // still must reposition the children | |
4601 | CalcWindowSizes(); | |
4602 | } | |
4603 | ||
4604 | void wxGrid::CalcWindowSizes() | |
4605 | { | |
4606 | // escape if the window is has not been fully created yet | |
4607 | ||
4608 | if ( m_cornerLabelWin == NULL ) | |
4609 | return; | |
4610 | ||
4611 | int cw, ch; | |
4612 | GetClientSize( &cw, &ch ); | |
4613 | ||
4614 | if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() ) | |
4615 | m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight ); | |
4616 | ||
4617 | if ( m_colLabelWin && m_colLabelWin->IsShown() ) | |
4618 | m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw - m_rowLabelWidth, m_colLabelHeight ); | |
4619 | ||
4620 | if ( m_rowLabelWin && m_rowLabelWin->IsShown() ) | |
4621 | m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch - m_colLabelHeight ); | |
4622 | ||
4623 | if ( m_gridWin && m_gridWin->IsShown() ) | |
4624 | m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw - m_rowLabelWidth, ch - m_colLabelHeight ); | |
4625 | } | |
4626 | ||
4627 | // this is called when the grid table sends a message | |
4628 | // to indicate that it has been redimensioned | |
4629 | // | |
4630 | bool wxGrid::Redimension( wxGridTableMessage& msg ) | |
4631 | { | |
4632 | int i; | |
4633 | bool result = false; | |
4634 | ||
4635 | // Clear the attribute cache as the attribute might refer to a different | |
4636 | // cell than stored in the cache after adding/removing rows/columns. | |
4637 | ClearAttrCache(); | |
4638 | ||
4639 | // By the same reasoning, the editor should be dismissed if columns are | |
4640 | // added or removed. And for consistency, it should IMHO always be | |
4641 | // removed, not only if the cell "underneath" it actually changes. | |
4642 | // For now, I intentionally do not save the editor's content as the | |
4643 | // cell it might want to save that stuff to might no longer exist. | |
4644 | HideCellEditControl(); | |
4645 | ||
4646 | #if 0 | |
4647 | // if we were using the default widths/heights so far, we must change them | |
4648 | // now | |
4649 | if ( m_colWidths.IsEmpty() ) | |
4650 | { | |
4651 | InitColWidths(); | |
4652 | } | |
4653 | ||
4654 | if ( m_rowHeights.IsEmpty() ) | |
4655 | { | |
4656 | InitRowHeights(); | |
4657 | } | |
4658 | #endif | |
4659 | ||
4660 | switch ( msg.GetId() ) | |
4661 | { | |
4662 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
4663 | { | |
4664 | size_t pos = msg.GetCommandInt(); | |
4665 | int numRows = msg.GetCommandInt2(); | |
4666 | ||
4667 | m_numRows += numRows; | |
4668 | ||
4669 | if ( !m_rowHeights.IsEmpty() ) | |
4670 | { | |
4671 | m_rowHeights.Insert( m_defaultRowHeight, pos, numRows ); | |
4672 | m_rowBottoms.Insert( 0, pos, numRows ); | |
4673 | ||
4674 | int bottom = 0; | |
4675 | if ( pos > 0 ) | |
4676 | bottom = m_rowBottoms[pos - 1]; | |
4677 | ||
4678 | for ( i = pos; i < m_numRows; i++ ) | |
4679 | { | |
4680 | bottom += m_rowHeights[i]; | |
4681 | m_rowBottoms[i] = bottom; | |
4682 | } | |
4683 | } | |
4684 | ||
4685 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4686 | { | |
4687 | // if we have just inserted cols into an empty grid the current | |
4688 | // cell will be undefined... | |
4689 | // | |
4690 | SetCurrentCell( 0, 0 ); | |
4691 | } | |
4692 | ||
4693 | if ( m_selection ) | |
4694 | m_selection->UpdateRows( pos, numRows ); | |
4695 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); | |
4696 | if (attrProvider) | |
4697 | attrProvider->UpdateAttrRows( pos, numRows ); | |
4698 | ||
4699 | if ( !GetBatchCount() ) | |
4700 | { | |
4701 | CalcDimensions(); | |
4702 | m_rowLabelWin->Refresh(); | |
4703 | } | |
4704 | } | |
4705 | result = true; | |
4706 | break; | |
4707 | ||
4708 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
4709 | { | |
4710 | int numRows = msg.GetCommandInt(); | |
4711 | int oldNumRows = m_numRows; | |
4712 | m_numRows += numRows; | |
4713 | ||
4714 | if ( !m_rowHeights.IsEmpty() ) | |
4715 | { | |
4716 | m_rowHeights.Add( m_defaultRowHeight, numRows ); | |
4717 | m_rowBottoms.Add( 0, numRows ); | |
4718 | ||
4719 | int bottom = 0; | |
4720 | if ( oldNumRows > 0 ) | |
4721 | bottom = m_rowBottoms[oldNumRows - 1]; | |
4722 | ||
4723 | for ( i = oldNumRows; i < m_numRows; i++ ) | |
4724 | { | |
4725 | bottom += m_rowHeights[i]; | |
4726 | m_rowBottoms[i] = bottom; | |
4727 | } | |
4728 | } | |
4729 | ||
4730 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4731 | { | |
4732 | // if we have just inserted cols into an empty grid the current | |
4733 | // cell will be undefined... | |
4734 | // | |
4735 | SetCurrentCell( 0, 0 ); | |
4736 | } | |
4737 | ||
4738 | if ( !GetBatchCount() ) | |
4739 | { | |
4740 | CalcDimensions(); | |
4741 | m_rowLabelWin->Refresh(); | |
4742 | } | |
4743 | } | |
4744 | result = true; | |
4745 | break; | |
4746 | ||
4747 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
4748 | { | |
4749 | size_t pos = msg.GetCommandInt(); | |
4750 | int numRows = msg.GetCommandInt2(); | |
4751 | m_numRows -= numRows; | |
4752 | ||
4753 | if ( !m_rowHeights.IsEmpty() ) | |
4754 | { | |
4755 | m_rowHeights.RemoveAt( pos, numRows ); | |
4756 | m_rowBottoms.RemoveAt( pos, numRows ); | |
4757 | ||
4758 | int h = 0; | |
4759 | for ( i = 0; i < m_numRows; i++ ) | |
4760 | { | |
4761 | h += m_rowHeights[i]; | |
4762 | m_rowBottoms[i] = h; | |
4763 | } | |
4764 | } | |
4765 | ||
4766 | if ( !m_numRows ) | |
4767 | { | |
4768 | m_currentCellCoords = wxGridNoCellCoords; | |
4769 | } | |
4770 | else | |
4771 | { | |
4772 | if ( m_currentCellCoords.GetRow() >= m_numRows ) | |
4773 | m_currentCellCoords.Set( 0, 0 ); | |
4774 | } | |
4775 | ||
4776 | if ( m_selection ) | |
4777 | m_selection->UpdateRows( pos, -((int)numRows) ); | |
4778 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); | |
4779 | if (attrProvider) | |
4780 | { | |
4781 | attrProvider->UpdateAttrRows( pos, -((int)numRows) ); | |
4782 | ||
4783 | // ifdef'd out following patch from Paul Gammans | |
4784 | #if 0 | |
4785 | // No need to touch column attributes, unless we | |
4786 | // removed _all_ rows, in this case, we remove | |
4787 | // all column attributes. | |
4788 | // I hate to do this here, but the | |
4789 | // needed data is not available inside UpdateAttrRows. | |
4790 | if ( !GetNumberRows() ) | |
4791 | attrProvider->UpdateAttrCols( 0, -GetNumberCols() ); | |
4792 | #endif | |
4793 | } | |
4794 | ||
4795 | if ( !GetBatchCount() ) | |
4796 | { | |
4797 | CalcDimensions(); | |
4798 | m_rowLabelWin->Refresh(); | |
4799 | } | |
4800 | } | |
4801 | result = true; | |
4802 | break; | |
4803 | ||
4804 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
4805 | { | |
4806 | size_t pos = msg.GetCommandInt(); | |
4807 | int numCols = msg.GetCommandInt2(); | |
4808 | m_numCols += numCols; | |
4809 | ||
4810 | if ( !m_colAt.IsEmpty() ) | |
4811 | { | |
4812 | //Shift the column IDs | |
4813 | int i; | |
4814 | for ( i = 0; i < m_numCols - numCols; i++ ) | |
4815 | { | |
4816 | if ( m_colAt[i] >= (int)pos ) | |
4817 | m_colAt[i] += numCols; | |
4818 | } | |
4819 | ||
4820 | m_colAt.Insert( pos, pos, numCols ); | |
4821 | ||
4822 | //Set the new columns' positions | |
4823 | for ( i = pos + 1; i < (int)pos + numCols; i++ ) | |
4824 | { | |
4825 | m_colAt[i] = i; | |
4826 | } | |
4827 | } | |
4828 | ||
4829 | if ( !m_colWidths.IsEmpty() ) | |
4830 | { | |
4831 | m_colWidths.Insert( m_defaultColWidth, pos, numCols ); | |
4832 | m_colRights.Insert( 0, pos, numCols ); | |
4833 | ||
4834 | int right = 0; | |
4835 | if ( pos > 0 ) | |
4836 | right = m_colRights[GetColAt( pos - 1 )]; | |
4837 | ||
4838 | int colPos; | |
4839 | for ( colPos = pos; colPos < m_numCols; colPos++ ) | |
4840 | { | |
4841 | i = GetColAt( colPos ); | |
4842 | ||
4843 | right += m_colWidths[i]; | |
4844 | m_colRights[i] = right; | |
4845 | } | |
4846 | } | |
4847 | ||
4848 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4849 | { | |
4850 | // if we have just inserted cols into an empty grid the current | |
4851 | // cell will be undefined... | |
4852 | // | |
4853 | SetCurrentCell( 0, 0 ); | |
4854 | } | |
4855 | ||
4856 | if ( m_selection ) | |
4857 | m_selection->UpdateCols( pos, numCols ); | |
4858 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); | |
4859 | if (attrProvider) | |
4860 | attrProvider->UpdateAttrCols( pos, numCols ); | |
4861 | if ( !GetBatchCount() ) | |
4862 | { | |
4863 | CalcDimensions(); | |
4864 | m_colLabelWin->Refresh(); | |
4865 | } | |
4866 | } | |
4867 | result = true; | |
4868 | break; | |
4869 | ||
4870 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
4871 | { | |
4872 | int numCols = msg.GetCommandInt(); | |
4873 | int oldNumCols = m_numCols; | |
4874 | m_numCols += numCols; | |
4875 | ||
4876 | if ( !m_colAt.IsEmpty() ) | |
4877 | { | |
4878 | m_colAt.Add( 0, numCols ); | |
4879 | ||
4880 | //Set the new columns' positions | |
4881 | int i; | |
4882 | for ( i = oldNumCols; i < m_numCols; i++ ) | |
4883 | { | |
4884 | m_colAt[i] = i; | |
4885 | } | |
4886 | } | |
4887 | ||
4888 | if ( !m_colWidths.IsEmpty() ) | |
4889 | { | |
4890 | m_colWidths.Add( m_defaultColWidth, numCols ); | |
4891 | m_colRights.Add( 0, numCols ); | |
4892 | ||
4893 | int right = 0; | |
4894 | if ( oldNumCols > 0 ) | |
4895 | right = m_colRights[GetColAt( oldNumCols - 1 )]; | |
4896 | ||
4897 | int colPos; | |
4898 | for ( colPos = oldNumCols; colPos < m_numCols; colPos++ ) | |
4899 | { | |
4900 | i = GetColAt( colPos ); | |
4901 | ||
4902 | right += m_colWidths[i]; | |
4903 | m_colRights[i] = right; | |
4904 | } | |
4905 | } | |
4906 | ||
4907 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
4908 | { | |
4909 | // if we have just inserted cols into an empty grid the current | |
4910 | // cell will be undefined... | |
4911 | // | |
4912 | SetCurrentCell( 0, 0 ); | |
4913 | } | |
4914 | if ( !GetBatchCount() ) | |
4915 | { | |
4916 | CalcDimensions(); | |
4917 | m_colLabelWin->Refresh(); | |
4918 | } | |
4919 | } | |
4920 | result = true; | |
4921 | break; | |
4922 | ||
4923 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
4924 | { | |
4925 | size_t pos = msg.GetCommandInt(); | |
4926 | int numCols = msg.GetCommandInt2(); | |
4927 | m_numCols -= numCols; | |
4928 | ||
4929 | if ( !m_colAt.IsEmpty() ) | |
4930 | { | |
4931 | int colID = GetColAt( pos ); | |
4932 | ||
4933 | m_colAt.RemoveAt( pos, numCols ); | |
4934 | ||
4935 | //Shift the column IDs | |
4936 | int colPos; | |
4937 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
4938 | { | |
4939 | if ( m_colAt[colPos] > colID ) | |
4940 | m_colAt[colPos] -= numCols; | |
4941 | } | |
4942 | } | |
4943 | ||
4944 | if ( !m_colWidths.IsEmpty() ) | |
4945 | { | |
4946 | m_colWidths.RemoveAt( pos, numCols ); | |
4947 | m_colRights.RemoveAt( pos, numCols ); | |
4948 | ||
4949 | int w = 0; | |
4950 | int colPos; | |
4951 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
4952 | { | |
4953 | i = GetColAt( colPos ); | |
4954 | ||
4955 | w += m_colWidths[i]; | |
4956 | m_colRights[i] = w; | |
4957 | } | |
4958 | } | |
4959 | ||
4960 | if ( !m_numCols ) | |
4961 | { | |
4962 | m_currentCellCoords = wxGridNoCellCoords; | |
4963 | } | |
4964 | else | |
4965 | { | |
4966 | if ( m_currentCellCoords.GetCol() >= m_numCols ) | |
4967 | m_currentCellCoords.Set( 0, 0 ); | |
4968 | } | |
4969 | ||
4970 | if ( m_selection ) | |
4971 | m_selection->UpdateCols( pos, -((int)numCols) ); | |
4972 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); | |
4973 | if (attrProvider) | |
4974 | { | |
4975 | attrProvider->UpdateAttrCols( pos, -((int)numCols) ); | |
4976 | ||
4977 | // ifdef'd out following patch from Paul Gammans | |
4978 | #if 0 | |
4979 | // No need to touch row attributes, unless we | |
4980 | // removed _all_ columns, in this case, we remove | |
4981 | // all row attributes. | |
4982 | // I hate to do this here, but the | |
4983 | // needed data is not available inside UpdateAttrCols. | |
4984 | if ( !GetNumberCols() ) | |
4985 | attrProvider->UpdateAttrRows( 0, -GetNumberRows() ); | |
4986 | #endif | |
4987 | } | |
4988 | ||
4989 | if ( !GetBatchCount() ) | |
4990 | { | |
4991 | CalcDimensions(); | |
4992 | m_colLabelWin->Refresh(); | |
4993 | } | |
4994 | } | |
4995 | result = true; | |
4996 | break; | |
4997 | } | |
4998 | ||
4999 | if (result && !GetBatchCount() ) | |
5000 | m_gridWin->Refresh(); | |
5001 | ||
5002 | return result; | |
5003 | } | |
5004 | ||
5005 | wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) | |
5006 | { | |
5007 | wxRegionIterator iter( reg ); | |
5008 | wxRect r; | |
5009 | ||
5010 | wxArrayInt rowlabels; | |
5011 | ||
5012 | int top, bottom; | |
5013 | while ( iter ) | |
5014 | { | |
5015 | r = iter.GetRect(); | |
5016 | ||
5017 | // TODO: remove this when we can... | |
5018 | // There is a bug in wxMotif that gives garbage update | |
5019 | // rectangles if you jump-scroll a long way by clicking the | |
5020 | // scrollbar with middle button. This is a work-around | |
5021 | // | |
5022 | #if defined(__WXMOTIF__) | |
5023 | int cw, ch; | |
5024 | m_gridWin->GetClientSize( &cw, &ch ); | |
5025 | if ( r.GetTop() > ch ) | |
5026 | r.SetTop( 0 ); | |
5027 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
5028 | #endif | |
5029 | ||
5030 | // logical bounds of update region | |
5031 | // | |
5032 | int dummy; | |
5033 | CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top ); | |
5034 | CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom ); | |
5035 | ||
5036 | // find the row labels within these bounds | |
5037 | // | |
5038 | int row; | |
5039 | for ( row = internalYToRow(top); row < m_numRows; row++ ) | |
5040 | { | |
5041 | if ( GetRowBottom(row) < top ) | |
5042 | continue; | |
5043 | ||
5044 | if ( GetRowTop(row) > bottom ) | |
5045 | break; | |
5046 | ||
5047 | rowlabels.Add( row ); | |
5048 | } | |
5049 | ||
5050 | ++iter; | |
5051 | } | |
5052 | ||
5053 | return rowlabels; | |
5054 | } | |
5055 | ||
5056 | wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) | |
5057 | { | |
5058 | wxRegionIterator iter( reg ); | |
5059 | wxRect r; | |
5060 | ||
5061 | wxArrayInt colLabels; | |
5062 | ||
5063 | int left, right; | |
5064 | while ( iter ) | |
5065 | { | |
5066 | r = iter.GetRect(); | |
5067 | ||
5068 | // TODO: remove this when we can... | |
5069 | // There is a bug in wxMotif that gives garbage update | |
5070 | // rectangles if you jump-scroll a long way by clicking the | |
5071 | // scrollbar with middle button. This is a work-around | |
5072 | // | |
5073 | #if defined(__WXMOTIF__) | |
5074 | int cw, ch; | |
5075 | m_gridWin->GetClientSize( &cw, &ch ); | |
5076 | if ( r.GetLeft() > cw ) | |
5077 | r.SetLeft( 0 ); | |
5078 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
5079 | #endif | |
5080 | ||
5081 | // logical bounds of update region | |
5082 | // | |
5083 | int dummy; | |
5084 | CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy ); | |
5085 | CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy ); | |
5086 | ||
5087 | // find the cells within these bounds | |
5088 | // | |
5089 | int col; | |
5090 | int colPos; | |
5091 | for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ ) | |
5092 | { | |
5093 | col = GetColAt( colPos ); | |
5094 | ||
5095 | if ( GetColRight(col) < left ) | |
5096 | continue; | |
5097 | ||
5098 | if ( GetColLeft(col) > right ) | |
5099 | break; | |
5100 | ||
5101 | colLabels.Add( col ); | |
5102 | } | |
5103 | ||
5104 | ++iter; | |
5105 | } | |
5106 | ||
5107 | return colLabels; | |
5108 | } | |
5109 | ||
5110 | wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) | |
5111 | { | |
5112 | wxRegionIterator iter( reg ); | |
5113 | wxRect r; | |
5114 | ||
5115 | wxGridCellCoordsArray cellsExposed; | |
5116 | ||
5117 | int left, top, right, bottom; | |
5118 | while ( iter ) | |
5119 | { | |
5120 | r = iter.GetRect(); | |
5121 | ||
5122 | // TODO: remove this when we can... | |
5123 | // There is a bug in wxMotif that gives garbage update | |
5124 | // rectangles if you jump-scroll a long way by clicking the | |
5125 | // scrollbar with middle button. This is a work-around | |
5126 | // | |
5127 | #if defined(__WXMOTIF__) | |
5128 | int cw, ch; | |
5129 | m_gridWin->GetClientSize( &cw, &ch ); | |
5130 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
5131 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
5132 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
5133 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
5134 | #endif | |
5135 | ||
5136 | // logical bounds of update region | |
5137 | // | |
5138 | CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
5139 | CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
5140 | ||
5141 | // find the cells within these bounds | |
5142 | // | |
5143 | int row, col; | |
5144 | for ( row = internalYToRow(top); row < m_numRows; row++ ) | |
5145 | { | |
5146 | if ( GetRowBottom(row) <= top ) | |
5147 | continue; | |
5148 | ||
5149 | if ( GetRowTop(row) > bottom ) | |
5150 | break; | |
5151 | ||
5152 | int colPos; | |
5153 | for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ ) | |
5154 | { | |
5155 | col = GetColAt( colPos ); | |
5156 | ||
5157 | if ( GetColRight(col) <= left ) | |
5158 | continue; | |
5159 | ||
5160 | if ( GetColLeft(col) > right ) | |
5161 | break; | |
5162 | ||
5163 | cellsExposed.Add( wxGridCellCoords( row, col ) ); | |
5164 | } | |
5165 | } | |
5166 | ||
5167 | ++iter; | |
5168 | } | |
5169 | ||
5170 | return cellsExposed; | |
5171 | } | |
5172 | ||
5173 | ||
5174 | void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) | |
5175 | { | |
5176 | int x, y, row; | |
5177 | wxPoint pos( event.GetPosition() ); | |
5178 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
5179 | ||
5180 | if ( event.Dragging() ) | |
5181 | { | |
5182 | if (!m_isDragging) | |
5183 | { | |
5184 | m_isDragging = true; | |
5185 | m_rowLabelWin->CaptureMouse(); | |
5186 | } | |
5187 | ||
5188 | if ( event.LeftIsDown() ) | |
5189 | { | |
5190 | switch ( m_cursorMode ) | |
5191 | { | |
5192 | case WXGRID_CURSOR_RESIZE_ROW: | |
5193 | { | |
5194 | int cw, ch, left, dummy; | |
5195 | m_gridWin->GetClientSize( &cw, &ch ); | |
5196 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
5197 | ||
5198 | wxClientDC dc( m_gridWin ); | |
5199 | PrepareDC( dc ); | |
5200 | y = wxMax( y, | |
5201 | GetRowTop(m_dragRowOrCol) + | |
5202 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
5203 | dc.SetLogicalFunction(wxINVERT); | |
5204 | if ( m_dragLastPos >= 0 ) | |
5205 | { | |
5206 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
5207 | } | |
5208 | dc.DrawLine( left, y, left+cw, y ); | |
5209 | m_dragLastPos = y; | |
5210 | } | |
5211 | break; | |
5212 | ||
5213 | case WXGRID_CURSOR_SELECT_ROW: | |
5214 | { | |
5215 | if ( (row = YToRow( y )) >= 0 ) | |
5216 | { | |
5217 | if ( m_selection ) | |
5218 | { | |
5219 | m_selection->SelectRow( row, | |
5220 | event.ControlDown(), | |
5221 | event.ShiftDown(), | |
5222 | event.AltDown(), | |
5223 | event.MetaDown() ); | |
5224 | } | |
5225 | } | |
5226 | } | |
5227 | break; | |
5228 | ||
5229 | // default label to suppress warnings about "enumeration value | |
5230 | // 'xxx' not handled in switch | |
5231 | default: | |
5232 | break; | |
5233 | } | |
5234 | } | |
5235 | return; | |
5236 | } | |
5237 | ||
5238 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) | |
5239 | return; | |
5240 | ||
5241 | if (m_isDragging) | |
5242 | { | |
5243 | if (m_rowLabelWin->HasCapture()) | |
5244 | m_rowLabelWin->ReleaseMouse(); | |
5245 | m_isDragging = false; | |
5246 | } | |
5247 | ||
5248 | // ------------ Entering or leaving the window | |
5249 | // | |
5250 | if ( event.Entering() || event.Leaving() ) | |
5251 | { | |
5252 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); | |
5253 | } | |
5254 | ||
5255 | // ------------ Left button pressed | |
5256 | // | |
5257 | else if ( event.LeftDown() ) | |
5258 | { | |
5259 | // don't send a label click event for a hit on the | |
5260 | // edge of the row label - this is probably the user | |
5261 | // wanting to resize the row | |
5262 | // | |
5263 | if ( YToEdgeOfRow(y) < 0 ) | |
5264 | { | |
5265 | row = YToRow(y); | |
5266 | if ( row >= 0 && | |
5267 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) ) | |
5268 | { | |
5269 | if ( !event.ShiftDown() && !event.CmdDown() ) | |
5270 | ClearSelection(); | |
5271 | if ( m_selection ) | |
5272 | { | |
5273 | if ( event.ShiftDown() ) | |
5274 | { | |
5275 | m_selection->SelectBlock( m_currentCellCoords.GetRow(), | |
5276 | 0, | |
5277 | row, | |
5278 | GetNumberCols() - 1, | |
5279 | event.ControlDown(), | |
5280 | event.ShiftDown(), | |
5281 | event.AltDown(), | |
5282 | event.MetaDown() ); | |
5283 | } | |
5284 | else | |
5285 | { | |
5286 | m_selection->SelectRow( row, | |
5287 | event.ControlDown(), | |
5288 | event.ShiftDown(), | |
5289 | event.AltDown(), | |
5290 | event.MetaDown() ); | |
5291 | } | |
5292 | } | |
5293 | ||
5294 | ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin); | |
5295 | } | |
5296 | } | |
5297 | else | |
5298 | { | |
5299 | // starting to drag-resize a row | |
5300 | if ( CanDragRowSize() ) | |
5301 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin); | |
5302 | } | |
5303 | } | |
5304 | ||
5305 | // ------------ Left double click | |
5306 | // | |
5307 | else if (event.LeftDClick() ) | |
5308 | { | |
5309 | row = YToEdgeOfRow(y); | |
5310 | if ( row < 0 ) | |
5311 | { | |
5312 | row = YToRow(y); | |
5313 | if ( row >=0 && | |
5314 | !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) ) | |
5315 | { | |
5316 | // no default action at the moment | |
5317 | } | |
5318 | } | |
5319 | else | |
5320 | { | |
5321 | // adjust row height depending on label text | |
5322 | AutoSizeRowLabelSize( row ); | |
5323 | ||
5324 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
5325 | m_dragLastPos = -1; | |
5326 | } | |
5327 | } | |
5328 | ||
5329 | // ------------ Left button released | |
5330 | // | |
5331 | else if ( event.LeftUp() ) | |
5332 | { | |
5333 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
5334 | { | |
5335 | DoEndDragResizeRow(); | |
5336 | ||
5337 | // Note: we are ending the event *after* doing | |
5338 | // default processing in this case | |
5339 | // | |
5340 | SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); | |
5341 | } | |
5342 | ||
5343 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); | |
5344 | m_dragLastPos = -1; | |
5345 | } | |
5346 | ||
5347 | // ------------ Right button down | |
5348 | // | |
5349 | else if ( event.RightDown() ) | |
5350 | { | |
5351 | row = YToRow(y); | |
5352 | if ( row >=0 && | |
5353 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) | |
5354 | { | |
5355 | // no default action at the moment | |
5356 | } | |
5357 | } | |
5358 | ||
5359 | // ------------ Right double click | |
5360 | // | |
5361 | else if ( event.RightDClick() ) | |
5362 | { | |
5363 | row = YToRow(y); | |
5364 | if ( row >= 0 && | |
5365 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) | |
5366 | { | |
5367 | // no default action at the moment | |
5368 | } | |
5369 | } | |
5370 | ||
5371 | // ------------ No buttons down and mouse moving | |
5372 | // | |
5373 | else if ( event.Moving() ) | |
5374 | { | |
5375 | m_dragRowOrCol = YToEdgeOfRow( y ); | |
5376 | if ( m_dragRowOrCol >= 0 ) | |
5377 | { | |
5378 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
5379 | { | |
5380 | // don't capture the mouse yet | |
5381 | if ( CanDragRowSize() ) | |
5382 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false); | |
5383 | } | |
5384 | } | |
5385 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) | |
5386 | { | |
5387 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false); | |
5388 | } | |
5389 | } | |
5390 | } | |
5391 | ||
5392 | void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) | |
5393 | { | |
5394 | int x, y, col; | |
5395 | wxPoint pos( event.GetPosition() ); | |
5396 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
5397 | ||
5398 | if ( event.Dragging() ) | |
5399 | { | |
5400 | if (!m_isDragging) | |
5401 | { | |
5402 | m_isDragging = true; | |
5403 | m_colLabelWin->CaptureMouse(); | |
5404 | ||
5405 | if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL ) | |
5406 | m_dragRowOrCol = XToCol( x ); | |
5407 | } | |
5408 | ||
5409 | if ( event.LeftIsDown() ) | |
5410 | { | |
5411 | switch ( m_cursorMode ) | |
5412 | { | |
5413 | case WXGRID_CURSOR_RESIZE_COL: | |
5414 | { | |
5415 | int cw, ch, dummy, top; | |
5416 | m_gridWin->GetClientSize( &cw, &ch ); | |
5417 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
5418 | ||
5419 | wxClientDC dc( m_gridWin ); | |
5420 | PrepareDC( dc ); | |
5421 | ||
5422 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + | |
5423 | GetColMinimalWidth(m_dragRowOrCol)); | |
5424 | dc.SetLogicalFunction(wxINVERT); | |
5425 | if ( m_dragLastPos >= 0 ) | |
5426 | { | |
5427 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch ); | |
5428 | } | |
5429 | dc.DrawLine( x, top, x, top + ch ); | |
5430 | m_dragLastPos = x; | |
5431 | } | |
5432 | break; | |
5433 | ||
5434 | case WXGRID_CURSOR_SELECT_COL: | |
5435 | { | |
5436 | if ( (col = XToCol( x )) >= 0 ) | |
5437 | { | |
5438 | if ( m_selection ) | |
5439 | { | |
5440 | m_selection->SelectCol( col, | |
5441 | event.ControlDown(), | |
5442 | event.ShiftDown(), | |
5443 | event.AltDown(), | |
5444 | event.MetaDown() ); | |
5445 | } | |
5446 | } | |
5447 | } | |
5448 | break; | |
5449 | ||
5450 | case WXGRID_CURSOR_MOVE_COL: | |
5451 | { | |
5452 | if ( x < 0 ) | |
5453 | m_moveToCol = GetColAt( 0 ); | |
5454 | else | |
5455 | m_moveToCol = XToCol( x ); | |
5456 | ||
5457 | int markerX; | |
5458 | ||
5459 | if ( m_moveToCol < 0 ) | |
5460 | markerX = GetColRight( GetColAt( m_numCols - 1 ) ); | |
5461 | else | |
5462 | markerX = GetColLeft( m_moveToCol ); | |
5463 | ||
5464 | if ( markerX != m_dragLastPos ) | |
5465 | { | |
5466 | wxClientDC dc( m_colLabelWin ); | |
5467 | ||
5468 | int cw, ch; | |
5469 | m_colLabelWin->GetClientSize( &cw, &ch ); | |
5470 | ||
5471 | markerX++; | |
5472 | ||
5473 | //Clean up the last indicator | |
5474 | if ( m_dragLastPos >= 0 ) | |
5475 | { | |
5476 | wxPen pen( m_colLabelWin->GetBackgroundColour(), 2 ); | |
5477 | dc.SetPen(pen); | |
5478 | dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch ); | |
5479 | dc.SetPen(wxNullPen); | |
5480 | ||
5481 | if ( XToCol( m_dragLastPos ) != -1 ) | |
5482 | DrawColLabel( dc, XToCol( m_dragLastPos ) ); | |
5483 | } | |
5484 | ||
5485 | //Moving to the same place? Don't draw a marker | |
5486 | if ( (m_moveToCol == m_dragRowOrCol) | |
5487 | || (GetColPos( m_moveToCol ) == GetColPos( m_dragRowOrCol ) + 1) | |
5488 | || (m_moveToCol < 0 && m_dragRowOrCol == GetColAt( m_numCols - 1 ))) | |
5489 | { | |
5490 | m_dragLastPos = -1; | |
5491 | return; | |
5492 | } | |
5493 | ||
5494 | //Draw the marker | |
5495 | wxPen pen( *wxBLUE, 2 ); | |
5496 | dc.SetPen(pen); | |
5497 | ||
5498 | dc.DrawLine( markerX, 0, markerX, ch ); | |
5499 | ||
5500 | dc.SetPen(wxNullPen); | |
5501 | ||
5502 | m_dragLastPos = markerX - 1; | |
5503 | } | |
5504 | } | |
5505 | break; | |
5506 | ||
5507 | // default label to suppress warnings about "enumeration value | |
5508 | // 'xxx' not handled in switch | |
5509 | default: | |
5510 | break; | |
5511 | } | |
5512 | } | |
5513 | return; | |
5514 | } | |
5515 | ||
5516 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) | |
5517 | return; | |
5518 | ||
5519 | if (m_isDragging) | |
5520 | { | |
5521 | if (m_colLabelWin->HasCapture()) | |
5522 | m_colLabelWin->ReleaseMouse(); | |
5523 | m_isDragging = false; | |
5524 | } | |
5525 | ||
5526 | // ------------ Entering or leaving the window | |
5527 | // | |
5528 | if ( event.Entering() || event.Leaving() ) | |
5529 | { | |
5530 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
5531 | } | |
5532 | ||
5533 | // ------------ Left button pressed | |
5534 | // | |
5535 | else if ( event.LeftDown() ) | |
5536 | { | |
5537 | // don't send a label click event for a hit on the | |
5538 | // edge of the col label - this is probably the user | |
5539 | // wanting to resize the col | |
5540 | // | |
5541 | if ( XToEdgeOfCol(x) < 0 ) | |
5542 | { | |
5543 | col = XToCol(x); | |
5544 | if ( col >= 0 && | |
5545 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) ) | |
5546 | { | |
5547 | if ( m_canDragColMove ) | |
5548 | { | |
5549 | //Show button as pressed | |
5550 | wxClientDC dc( m_colLabelWin ); | |
5551 | int colLeft = GetColLeft( col ); | |
5552 | int colRight = GetColRight( col ) - 1; | |
5553 | dc.SetPen( wxPen( m_colLabelWin->GetBackgroundColour(), 1 ) ); | |
5554 | dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 ); | |
5555 | dc.DrawLine( colLeft, 1, colRight, 1 ); | |
5556 | ||
5557 | ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, m_colLabelWin); | |
5558 | } | |
5559 | else | |
5560 | { | |
5561 | if ( !event.ShiftDown() && !event.CmdDown() ) | |
5562 | ClearSelection(); | |
5563 | if ( m_selection ) | |
5564 | { | |
5565 | if ( event.ShiftDown() ) | |
5566 | { | |
5567 | m_selection->SelectBlock( 0, | |
5568 | m_currentCellCoords.GetCol(), | |
5569 | GetNumberRows() - 1, col, | |
5570 | event.ControlDown(), | |
5571 | event.ShiftDown(), | |
5572 | event.AltDown(), | |
5573 | event.MetaDown() ); | |
5574 | } | |
5575 | else | |
5576 | { | |
5577 | m_selection->SelectCol( col, | |
5578 | event.ControlDown(), | |
5579 | event.ShiftDown(), | |
5580 | event.AltDown(), | |
5581 | event.MetaDown() ); | |
5582 | } | |
5583 | } | |
5584 | ||
5585 | ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin); | |
5586 | } | |
5587 | } | |
5588 | } | |
5589 | else | |
5590 | { | |
5591 | // starting to drag-resize a col | |
5592 | // | |
5593 | if ( CanDragColSize() ) | |
5594 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin); | |
5595 | } | |
5596 | } | |
5597 | ||
5598 | // ------------ Left double click | |
5599 | // | |
5600 | if ( event.LeftDClick() ) | |
5601 | { | |
5602 | col = XToEdgeOfCol(x); | |
5603 | if ( col < 0 ) | |
5604 | { | |
5605 | col = XToCol(x); | |
5606 | if ( col >= 0 && | |
5607 | ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) ) | |
5608 | { | |
5609 | // no default action at the moment | |
5610 | } | |
5611 | } | |
5612 | else | |
5613 | { | |
5614 | // adjust column width depending on label text | |
5615 | AutoSizeColLabelSize( col ); | |
5616 | ||
5617 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
5618 | m_dragLastPos = -1; | |
5619 | } | |
5620 | } | |
5621 | ||
5622 | // ------------ Left button released | |
5623 | // | |
5624 | else if ( event.LeftUp() ) | |
5625 | { | |
5626 | switch ( m_cursorMode ) | |
5627 | { | |
5628 | case WXGRID_CURSOR_RESIZE_COL: | |
5629 | DoEndDragResizeCol(); | |
5630 | ||
5631 | // Note: we are ending the event *after* doing | |
5632 | // default processing in this case | |
5633 | // | |
5634 | SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); | |
5635 | break; | |
5636 | ||
5637 | case WXGRID_CURSOR_MOVE_COL: | |
5638 | DoEndDragMoveCol(); | |
5639 | ||
5640 | SendEvent( wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol, event ); | |
5641 | break; | |
5642 | ||
5643 | case WXGRID_CURSOR_SELECT_COL: | |
5644 | case WXGRID_CURSOR_SELECT_CELL: | |
5645 | case WXGRID_CURSOR_RESIZE_ROW: | |
5646 | case WXGRID_CURSOR_SELECT_ROW: | |
5647 | // nothing to do (?) | |
5648 | break; | |
5649 | } | |
5650 | ||
5651 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); | |
5652 | m_dragLastPos = -1; | |
5653 | } | |
5654 | ||
5655 | // ------------ Right button down | |
5656 | // | |
5657 | else if ( event.RightDown() ) | |
5658 | { | |
5659 | col = XToCol(x); | |
5660 | if ( col >= 0 && | |
5661 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) | |
5662 | { | |
5663 | // no default action at the moment | |
5664 | } | |
5665 | } | |
5666 | ||
5667 | // ------------ Right double click | |
5668 | // | |
5669 | else if ( event.RightDClick() ) | |
5670 | { | |
5671 | col = XToCol(x); | |
5672 | if ( col >= 0 && | |
5673 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) | |
5674 | { | |
5675 | // no default action at the moment | |
5676 | } | |
5677 | } | |
5678 | ||
5679 | // ------------ No buttons down and mouse moving | |
5680 | // | |
5681 | else if ( event.Moving() ) | |
5682 | { | |
5683 | m_dragRowOrCol = XToEdgeOfCol( x ); | |
5684 | if ( m_dragRowOrCol >= 0 ) | |
5685 | { | |
5686 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
5687 | { | |
5688 | // don't capture the cursor yet | |
5689 | if ( CanDragColSize() ) | |
5690 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false); | |
5691 | } | |
5692 | } | |
5693 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) | |
5694 | { | |
5695 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false); | |
5696 | } | |
5697 | } | |
5698 | } | |
5699 | ||
5700 | void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event ) | |
5701 | { | |
5702 | if ( event.LeftDown() ) | |
5703 | { | |
5704 | // indicate corner label by having both row and | |
5705 | // col args == -1 | |
5706 | // | |
5707 | if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) ) | |
5708 | { | |
5709 | SelectAll(); | |
5710 | } | |
5711 | } | |
5712 | else if ( event.LeftDClick() ) | |
5713 | { | |
5714 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event ); | |
5715 | } | |
5716 | else if ( event.RightDown() ) | |
5717 | { | |
5718 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) ) | |
5719 | { | |
5720 | // no default action at the moment | |
5721 | } | |
5722 | } | |
5723 | else if ( event.RightDClick() ) | |
5724 | { | |
5725 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) ) | |
5726 | { | |
5727 | // no default action at the moment | |
5728 | } | |
5729 | } | |
5730 | } | |
5731 | ||
5732 | void wxGrid::ChangeCursorMode(CursorMode mode, | |
5733 | wxWindow *win, | |
5734 | bool captureMouse) | |
5735 | { | |
5736 | #ifdef __WXDEBUG__ | |
5737 | static const wxChar *cursorModes[] = | |
5738 | { | |
5739 | _T("SELECT_CELL"), | |
5740 | _T("RESIZE_ROW"), | |
5741 | _T("RESIZE_COL"), | |
5742 | _T("SELECT_ROW"), | |
5743 | _T("SELECT_COL"), | |
5744 | _T("MOVE_COL"), | |
5745 | }; | |
5746 | ||
5747 | wxLogTrace(_T("grid"), | |
5748 | _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"), | |
5749 | win == m_colLabelWin ? _T("colLabelWin") | |
5750 | : win ? _T("rowLabelWin") | |
5751 | : _T("gridWin"), | |
5752 | cursorModes[m_cursorMode], cursorModes[mode]); | |
5753 | #endif | |
5754 | ||
5755 | if ( mode == m_cursorMode && | |
5756 | win == m_winCapture && | |
5757 | captureMouse == (m_winCapture != NULL)) | |
5758 | return; | |
5759 | ||
5760 | if ( !win ) | |
5761 | { | |
5762 | // by default use the grid itself | |
5763 | win = m_gridWin; | |
5764 | } | |
5765 | ||
5766 | if ( m_winCapture ) | |
5767 | { | |
5768 | if (m_winCapture->HasCapture()) | |
5769 | m_winCapture->ReleaseMouse(); | |
5770 | m_winCapture = (wxWindow *)NULL; | |
5771 | } | |
5772 | ||
5773 | m_cursorMode = mode; | |
5774 | ||
5775 | switch ( m_cursorMode ) | |
5776 | { | |
5777 | case WXGRID_CURSOR_RESIZE_ROW: | |
5778 | win->SetCursor( m_rowResizeCursor ); | |
5779 | break; | |
5780 | ||
5781 | case WXGRID_CURSOR_RESIZE_COL: | |
5782 | win->SetCursor( m_colResizeCursor ); | |
5783 | break; | |
5784 | ||
5785 | case WXGRID_CURSOR_MOVE_COL: | |
5786 | win->SetCursor( wxCursor(wxCURSOR_HAND) ); | |
5787 | break; | |
5788 | ||
5789 | default: | |
5790 | win->SetCursor( *wxSTANDARD_CURSOR ); | |
5791 | break; | |
5792 | } | |
5793 | ||
5794 | // we need to capture mouse when resizing | |
5795 | bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW || | |
5796 | m_cursorMode == WXGRID_CURSOR_RESIZE_COL; | |
5797 | ||
5798 | if ( captureMouse && resize ) | |
5799 | { | |
5800 | win->CaptureMouse(); | |
5801 | m_winCapture = win; | |
5802 | } | |
5803 | } | |
5804 | ||
5805 | void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) | |
5806 | { | |
5807 | int x, y; | |
5808 | wxPoint pos( event.GetPosition() ); | |
5809 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
5810 | ||
5811 | wxGridCellCoords coords; | |
5812 | XYToCell( x, y, coords ); | |
5813 | ||
5814 | int cell_rows, cell_cols; | |
5815 | bool isFirstDrag = !m_isDragging; | |
5816 | GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols ); | |
5817 | if ((cell_rows < 0) || (cell_cols < 0)) | |
5818 | { | |
5819 | coords.SetRow(coords.GetRow() + cell_rows); | |
5820 | coords.SetCol(coords.GetCol() + cell_cols); | |
5821 | } | |
5822 | ||
5823 | if ( event.Dragging() ) | |
5824 | { | |
5825 | //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol()); | |
5826 | ||
5827 | // Don't start doing anything until the mouse has been dragged at | |
5828 | // least 3 pixels in any direction... | |
5829 | if (! m_isDragging) | |
5830 | { | |
5831 | if (m_startDragPos == wxDefaultPosition) | |
5832 | { | |
5833 | m_startDragPos = pos; | |
5834 | return; | |
5835 | } | |
5836 | if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4) | |
5837 | return; | |
5838 | } | |
5839 | ||
5840 | m_isDragging = true; | |
5841 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
5842 | { | |
5843 | // Hide the edit control, so it | |
5844 | // won't interfere with drag-shrinking. | |
5845 | if ( IsCellEditControlShown() ) | |
5846 | { | |
5847 | HideCellEditControl(); | |
5848 | SaveEditControlValue(); | |
5849 | } | |
5850 | ||
5851 | // Have we captured the mouse yet? | |
5852 | if (! m_winCapture) | |
5853 | { | |
5854 | m_winCapture = m_gridWin; | |
5855 | m_winCapture->CaptureMouse(); | |
5856 | } | |
5857 | ||
5858 | if ( coords != wxGridNoCellCoords ) | |
5859 | { | |
5860 | if ( event.CmdDown() ) | |
5861 | { | |
5862 | if ( m_selectingKeyboard == wxGridNoCellCoords) | |
5863 | m_selectingKeyboard = coords; | |
5864 | HighlightBlock( m_selectingKeyboard, coords ); | |
5865 | } | |
5866 | else if ( CanDragCell() ) | |
5867 | { | |
5868 | if ( isFirstDrag ) | |
5869 | { | |
5870 | if ( m_selectingKeyboard == wxGridNoCellCoords) | |
5871 | m_selectingKeyboard = coords; | |
5872 | ||
5873 | SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG, | |
5874 | coords.GetRow(), | |
5875 | coords.GetCol(), | |
5876 | event ); | |
5877 | } | |
5878 | } | |
5879 | else | |
5880 | { | |
5881 | if ( !IsSelection() ) | |
5882 | { | |
5883 | HighlightBlock( coords, coords ); | |
5884 | } | |
5885 | else | |
5886 | { | |
5887 | HighlightBlock( m_currentCellCoords, coords ); | |
5888 | } | |
5889 | } | |
5890 | ||
5891 | if (! IsVisible(coords)) | |
5892 | { | |
5893 | MakeCellVisible(coords); | |
5894 | // TODO: need to introduce a delay or something here. The | |
5895 | // scrolling is way to fast, at least on MSW - also on GTK. | |
5896 | } | |
5897 | } | |
5898 | } | |
5899 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
5900 | { | |
5901 | int cw, ch, left, dummy; | |
5902 | m_gridWin->GetClientSize( &cw, &ch ); | |
5903 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
5904 | ||
5905 | wxClientDC dc( m_gridWin ); | |
5906 | PrepareDC( dc ); | |
5907 | y = wxMax( y, GetRowTop(m_dragRowOrCol) + | |
5908 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
5909 | dc.SetLogicalFunction(wxINVERT); | |
5910 | if ( m_dragLastPos >= 0 ) | |
5911 | { | |
5912 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
5913 | } | |
5914 | dc.DrawLine( left, y, left+cw, y ); | |
5915 | m_dragLastPos = y; | |
5916 | } | |
5917 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
5918 | { | |
5919 | int cw, ch, dummy, top; | |
5920 | m_gridWin->GetClientSize( &cw, &ch ); | |
5921 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
5922 | ||
5923 | wxClientDC dc( m_gridWin ); | |
5924 | PrepareDC( dc ); | |
5925 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + | |
5926 | GetColMinimalWidth(m_dragRowOrCol) ); | |
5927 | dc.SetLogicalFunction(wxINVERT); | |
5928 | if ( m_dragLastPos >= 0 ) | |
5929 | { | |
5930 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch ); | |
5931 | } | |
5932 | dc.DrawLine( x, top, x, top + ch ); | |
5933 | m_dragLastPos = x; | |
5934 | } | |
5935 | ||
5936 | return; | |
5937 | } | |
5938 | ||
5939 | m_isDragging = false; | |
5940 | m_startDragPos = wxDefaultPosition; | |
5941 | ||
5942 | // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL | |
5943 | // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under | |
5944 | // wxGTK | |
5945 | #if 0 | |
5946 | if ( event.Entering() || event.Leaving() ) | |
5947 | { | |
5948 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
5949 | m_gridWin->SetCursor( *wxSTANDARD_CURSOR ); | |
5950 | } | |
5951 | else | |
5952 | #endif // 0 | |
5953 | ||
5954 | // ------------ Left button pressed | |
5955 | // | |
5956 | if ( event.LeftDown() && coords != wxGridNoCellCoords ) | |
5957 | { | |
5958 | if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK, | |
5959 | coords.GetRow(), | |
5960 | coords.GetCol(), | |
5961 | event ) ) | |
5962 | { | |
5963 | if ( !event.CmdDown() ) | |
5964 | ClearSelection(); | |
5965 | if ( event.ShiftDown() ) | |
5966 | { | |
5967 | if ( m_selection ) | |
5968 | { | |
5969 | m_selection->SelectBlock( m_currentCellCoords.GetRow(), | |
5970 | m_currentCellCoords.GetCol(), | |
5971 | coords.GetRow(), | |
5972 | coords.GetCol(), | |
5973 | event.ControlDown(), | |
5974 | event.ShiftDown(), | |
5975 | event.AltDown(), | |
5976 | event.MetaDown() ); | |
5977 | } | |
5978 | } | |
5979 | else if ( XToEdgeOfCol(x) < 0 && | |
5980 | YToEdgeOfRow(y) < 0 ) | |
5981 | { | |
5982 | DisableCellEditControl(); | |
5983 | MakeCellVisible( coords ); | |
5984 | ||
5985 | if ( event.CmdDown() ) | |
5986 | { | |
5987 | if ( m_selection ) | |
5988 | { | |
5989 | m_selection->ToggleCellSelection( coords.GetRow(), | |
5990 | coords.GetCol(), | |
5991 | event.ControlDown(), | |
5992 | event.ShiftDown(), | |
5993 | event.AltDown(), | |
5994 | event.MetaDown() ); | |
5995 | } | |
5996 | m_selectingTopLeft = wxGridNoCellCoords; | |
5997 | m_selectingBottomRight = wxGridNoCellCoords; | |
5998 | m_selectingKeyboard = coords; | |
5999 | } | |
6000 | else | |
6001 | { | |
6002 | m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords; | |
6003 | SetCurrentCell( coords ); | |
6004 | if ( m_selection ) | |
6005 | { | |
6006 | if ( m_selection->GetSelectionMode() != | |
6007 | wxGrid::wxGridSelectCells ) | |
6008 | { | |
6009 | HighlightBlock( coords, coords ); | |
6010 | } | |
6011 | } | |
6012 | } | |
6013 | } | |
6014 | } | |
6015 | } | |
6016 | ||
6017 | // ------------ Left double click | |
6018 | // | |
6019 | else if ( event.LeftDClick() && coords != wxGridNoCellCoords ) | |
6020 | { | |
6021 | DisableCellEditControl(); | |
6022 | ||
6023 | if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 ) | |
6024 | { | |
6025 | if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK, | |
6026 | coords.GetRow(), | |
6027 | coords.GetCol(), | |
6028 | event ) ) | |
6029 | { | |
6030 | // we want double click to select a cell and start editing | |
6031 | // (i.e. to behave in same way as sequence of two slow clicks): | |
6032 | m_waitForSlowClick = true; | |
6033 | } | |
6034 | } | |
6035 | } | |
6036 | ||
6037 | // ------------ Left button released | |
6038 | // | |
6039 | else if ( event.LeftUp() ) | |
6040 | { | |
6041 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
6042 | { | |
6043 | if (m_winCapture) | |
6044 | { | |
6045 | if (m_winCapture->HasCapture()) | |
6046 | m_winCapture->ReleaseMouse(); | |
6047 | m_winCapture = NULL; | |
6048 | } | |
6049 | ||
6050 | if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() ) | |
6051 | { | |
6052 | ClearSelection(); | |
6053 | EnableCellEditControl(); | |
6054 | ||
6055 | wxGridCellAttr *attr = GetCellAttr(coords); | |
6056 | wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol()); | |
6057 | editor->StartingClick(); | |
6058 | editor->DecRef(); | |
6059 | attr->DecRef(); | |
6060 | ||
6061 | m_waitForSlowClick = false; | |
6062 | } | |
6063 | else if ( m_selectingTopLeft != wxGridNoCellCoords && | |
6064 | m_selectingBottomRight != wxGridNoCellCoords ) | |
6065 | { | |
6066 | if ( m_selection ) | |
6067 | { | |
6068 | m_selection->SelectBlock( m_selectingTopLeft.GetRow(), | |
6069 | m_selectingTopLeft.GetCol(), | |
6070 | m_selectingBottomRight.GetRow(), | |
6071 | m_selectingBottomRight.GetCol(), | |
6072 | event.ControlDown(), | |
6073 | event.ShiftDown(), | |
6074 | event.AltDown(), | |
6075 | event.MetaDown() ); | |
6076 | } | |
6077 | ||
6078 | m_selectingTopLeft = wxGridNoCellCoords; | |
6079 | m_selectingBottomRight = wxGridNoCellCoords; | |
6080 | ||
6081 | // Show the edit control, if it has been hidden for | |
6082 | // drag-shrinking. | |
6083 | ShowCellEditControl(); | |
6084 | } | |
6085 | } | |
6086 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
6087 | { | |
6088 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
6089 | DoEndDragResizeRow(); | |
6090 | ||
6091 | // Note: we are ending the event *after* doing | |
6092 | // default processing in this case | |
6093 | // | |
6094 | SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); | |
6095 | } | |
6096 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
6097 | { | |
6098 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
6099 | DoEndDragResizeCol(); | |
6100 | ||
6101 | // Note: we are ending the event *after* doing | |
6102 | // default processing in this case | |
6103 | // | |
6104 | SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); | |
6105 | } | |
6106 | ||
6107 | m_dragLastPos = -1; | |
6108 | } | |
6109 | ||
6110 | // ------------ Right button down | |
6111 | // | |
6112 | else if ( event.RightDown() && coords != wxGridNoCellCoords ) | |
6113 | { | |
6114 | DisableCellEditControl(); | |
6115 | if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK, | |
6116 | coords.GetRow(), | |
6117 | coords.GetCol(), | |
6118 | event ) ) | |
6119 | { | |
6120 | // no default action at the moment | |
6121 | } | |
6122 | } | |
6123 | ||
6124 | // ------------ Right double click | |
6125 | // | |
6126 | else if ( event.RightDClick() && coords != wxGridNoCellCoords ) | |
6127 | { | |
6128 | DisableCellEditControl(); | |
6129 | if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK, | |
6130 | coords.GetRow(), | |
6131 | coords.GetCol(), | |
6132 | event ) ) | |
6133 | { | |
6134 | // no default action at the moment | |
6135 | } | |
6136 | } | |
6137 | ||
6138 | // ------------ Moving and no button action | |
6139 | // | |
6140 | else if ( event.Moving() && !event.IsButton() ) | |
6141 | { | |
6142 | if ( coords.GetRow() < 0 || coords.GetCol() < 0 ) | |
6143 | { | |
6144 | // out of grid cell area | |
6145 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
6146 | return; | |
6147 | } | |
6148 | ||
6149 | int dragRow = YToEdgeOfRow( y ); | |
6150 | int dragCol = XToEdgeOfCol( x ); | |
6151 | ||
6152 | // Dragging on the corner of a cell to resize in both | |
6153 | // directions is not implemented yet... | |
6154 | // | |
6155 | if ( dragRow >= 0 && dragCol >= 0 ) | |
6156 | { | |
6157 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
6158 | return; | |
6159 | } | |
6160 | ||
6161 | if ( dragRow >= 0 ) | |
6162 | { | |
6163 | m_dragRowOrCol = dragRow; | |
6164 | ||
6165 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
6166 | { | |
6167 | if ( CanDragRowSize() && CanDragGridSize() ) | |
6168 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW); | |
6169 | } | |
6170 | } | |
6171 | else if ( dragCol >= 0 ) | |
6172 | { | |
6173 | m_dragRowOrCol = dragCol; | |
6174 | ||
6175 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
6176 | { | |
6177 | if ( CanDragColSize() && CanDragGridSize() ) | |
6178 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL); | |
6179 | } | |
6180 | } | |
6181 | else // Neither on a row or col edge | |
6182 | { | |
6183 | if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) | |
6184 | { | |
6185 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
6186 | } | |
6187 | } | |
6188 | } | |
6189 | } | |
6190 | ||
6191 | void wxGrid::DoEndDragResizeRow() | |
6192 | { | |
6193 | if ( m_dragLastPos >= 0 ) | |
6194 | { | |
6195 | // erase the last line and resize the row | |
6196 | // | |
6197 | int cw, ch, left, dummy; | |
6198 | m_gridWin->GetClientSize( &cw, &ch ); | |
6199 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
6200 | ||
6201 | wxClientDC dc( m_gridWin ); | |
6202 | PrepareDC( dc ); | |
6203 | dc.SetLogicalFunction( wxINVERT ); | |
6204 | dc.DrawLine( left, m_dragLastPos, left + cw, m_dragLastPos ); | |
6205 | HideCellEditControl(); | |
6206 | SaveEditControlValue(); | |
6207 | ||
6208 | int rowTop = GetRowTop(m_dragRowOrCol); | |
6209 | SetRowSize( m_dragRowOrCol, | |
6210 | wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) ); | |
6211 | ||
6212 | if ( !GetBatchCount() ) | |
6213 | { | |
6214 | // Only needed to get the correct rect.y: | |
6215 | wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) ); | |
6216 | rect.x = 0; | |
6217 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
6218 | rect.width = m_rowLabelWidth; | |
6219 | rect.height = ch - rect.y; | |
6220 | m_rowLabelWin->Refresh( true, &rect ); | |
6221 | rect.width = cw; | |
6222 | ||
6223 | // if there is a multicell block, paint all of it | |
6224 | if (m_table) | |
6225 | { | |
6226 | int i, cell_rows, cell_cols, subtract_rows = 0; | |
6227 | int leftCol = XToCol(left); | |
6228 | int rightCol = internalXToCol(left + cw); | |
6229 | if (leftCol >= 0) | |
6230 | { | |
6231 | for (i=leftCol; i<rightCol; i++) | |
6232 | { | |
6233 | GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols); | |
6234 | if (cell_rows < subtract_rows) | |
6235 | subtract_rows = cell_rows; | |
6236 | } | |
6237 | rect.y = GetRowTop(m_dragRowOrCol + subtract_rows); | |
6238 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
6239 | rect.height = ch - rect.y; | |
6240 | } | |
6241 | } | |
6242 | m_gridWin->Refresh( false, &rect ); | |
6243 | } | |
6244 | ||
6245 | ShowCellEditControl(); | |
6246 | } | |
6247 | } | |
6248 | ||
6249 | ||
6250 | void wxGrid::DoEndDragResizeCol() | |
6251 | { | |
6252 | if ( m_dragLastPos >= 0 ) | |
6253 | { | |
6254 | // erase the last line and resize the col | |
6255 | // | |
6256 | int cw, ch, dummy, top; | |
6257 | m_gridWin->GetClientSize( &cw, &ch ); | |
6258 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
6259 | ||
6260 | wxClientDC dc( m_gridWin ); | |
6261 | PrepareDC( dc ); | |
6262 | dc.SetLogicalFunction( wxINVERT ); | |
6263 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch ); | |
6264 | HideCellEditControl(); | |
6265 | SaveEditControlValue(); | |
6266 | ||
6267 | int colLeft = GetColLeft(m_dragRowOrCol); | |
6268 | SetColSize( m_dragRowOrCol, | |
6269 | wxMax( m_dragLastPos - colLeft, | |
6270 | GetColMinimalWidth(m_dragRowOrCol) ) ); | |
6271 | ||
6272 | if ( !GetBatchCount() ) | |
6273 | { | |
6274 | // Only needed to get the correct rect.x: | |
6275 | wxRect rect ( CellToRect( 0, m_dragRowOrCol ) ); | |
6276 | rect.y = 0; | |
6277 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
6278 | rect.width = cw - rect.x; | |
6279 | rect.height = m_colLabelHeight; | |
6280 | m_colLabelWin->Refresh( true, &rect ); | |
6281 | rect.height = ch; | |
6282 | ||
6283 | // if there is a multicell block, paint all of it | |
6284 | if (m_table) | |
6285 | { | |
6286 | int i, cell_rows, cell_cols, subtract_cols = 0; | |
6287 | int topRow = YToRow(top); | |
6288 | int bottomRow = internalYToRow(top + cw); | |
6289 | if (topRow >= 0) | |
6290 | { | |
6291 | for (i=topRow; i<bottomRow; i++) | |
6292 | { | |
6293 | GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols); | |
6294 | if (cell_cols < subtract_cols) | |
6295 | subtract_cols = cell_cols; | |
6296 | } | |
6297 | ||
6298 | rect.x = GetColLeft(m_dragRowOrCol + subtract_cols); | |
6299 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
6300 | rect.width = cw - rect.x; | |
6301 | } | |
6302 | } | |
6303 | ||
6304 | m_gridWin->Refresh( false, &rect ); | |
6305 | } | |
6306 | ||
6307 | ShowCellEditControl(); | |
6308 | } | |
6309 | } | |
6310 | ||
6311 | void wxGrid::DoEndDragMoveCol() | |
6312 | { | |
6313 | //The user clicked on the column but didn't actually drag | |
6314 | if ( m_dragLastPos < 0 ) | |
6315 | { | |
6316 | m_colLabelWin->Refresh(); //Do this to "unpress" the column | |
6317 | return; | |
6318 | } | |
6319 | ||
6320 | int newPos; | |
6321 | if ( m_moveToCol == -1 ) | |
6322 | newPos = m_numCols - 1; | |
6323 | else | |
6324 | { | |
6325 | newPos = GetColPos( m_moveToCol ); | |
6326 | if ( newPos > GetColPos( m_dragRowOrCol ) ) | |
6327 | newPos--; | |
6328 | } | |
6329 | ||
6330 | SetColPos( m_dragRowOrCol, newPos ); | |
6331 | } | |
6332 | ||
6333 | void wxGrid::SetColPos( int colID, int newPos ) | |
6334 | { | |
6335 | if ( m_colAt.IsEmpty() ) | |
6336 | { | |
6337 | m_colAt.Alloc( m_numCols ); | |
6338 | ||
6339 | int i; | |
6340 | for ( i = 0; i < m_numCols; i++ ) | |
6341 | { | |
6342 | m_colAt.Add( i ); | |
6343 | } | |
6344 | } | |
6345 | ||
6346 | int oldPos = GetColPos( colID ); | |
6347 | ||
6348 | //Reshuffle the m_colAt array | |
6349 | if ( newPos > oldPos ) | |
6350 | { | |
6351 | int i; | |
6352 | for ( i = oldPos; i < newPos; i++ ) | |
6353 | { | |
6354 | m_colAt[i] = m_colAt[i+1]; | |
6355 | } | |
6356 | } | |
6357 | else | |
6358 | { | |
6359 | int i; | |
6360 | for ( i = oldPos; i > newPos; i-- ) | |
6361 | { | |
6362 | m_colAt[i] = m_colAt[i-1]; | |
6363 | } | |
6364 | } | |
6365 | ||
6366 | m_colAt[newPos] = colID; | |
6367 | ||
6368 | //Recalculate the column rights | |
6369 | if ( !m_colWidths.IsEmpty() ) | |
6370 | { | |
6371 | int colRight = 0; | |
6372 | int colPos; | |
6373 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
6374 | { | |
6375 | int colID = GetColAt( colPos ); | |
6376 | ||
6377 | colRight += m_colWidths[colID]; | |
6378 | m_colRights[colID] = colRight; | |
6379 | } | |
6380 | } | |
6381 | ||
6382 | m_colLabelWin->Refresh(); | |
6383 | m_gridWin->Refresh(); | |
6384 | } | |
6385 | ||
6386 | ||
6387 | ||
6388 | void wxGrid::EnableDragColMove( bool enable ) | |
6389 | { | |
6390 | if ( m_canDragColMove == enable ) | |
6391 | return; | |
6392 | ||
6393 | m_canDragColMove = enable; | |
6394 | ||
6395 | if ( !m_canDragColMove ) | |
6396 | { | |
6397 | m_colAt.Clear(); | |
6398 | ||
6399 | //Recalculate the column rights | |
6400 | if ( !m_colWidths.IsEmpty() ) | |
6401 | { | |
6402 | int colRight = 0; | |
6403 | int colPos; | |
6404 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
6405 | { | |
6406 | colRight += m_colWidths[colPos]; | |
6407 | m_colRights[colPos] = colRight; | |
6408 | } | |
6409 | } | |
6410 | ||
6411 | m_colLabelWin->Refresh(); | |
6412 | m_gridWin->Refresh(); | |
6413 | } | |
6414 | } | |
6415 | ||
6416 | ||
6417 | // | |
6418 | // ------ interaction with data model | |
6419 | // | |
6420 | bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg ) | |
6421 | { | |
6422 | switch ( msg.GetId() ) | |
6423 | { | |
6424 | case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES: | |
6425 | return GetModelValues(); | |
6426 | ||
6427 | case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES: | |
6428 | return SetModelValues(); | |
6429 | ||
6430 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
6431 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
6432 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
6433 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
6434 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
6435 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
6436 | return Redimension( msg ); | |
6437 | ||
6438 | default: | |
6439 | return false; | |
6440 | } | |
6441 | } | |
6442 | ||
6443 | // The behaviour of this function depends on the grid table class | |
6444 | // Clear() function. For the default wxGridStringTable class the | |
6445 | // behavious is to replace all cell contents with wxEmptyString but | |
6446 | // not to change the number of rows or cols. | |
6447 | // | |
6448 | void wxGrid::ClearGrid() | |
6449 | { | |
6450 | if ( m_table ) | |
6451 | { | |
6452 | if (IsCellEditControlEnabled()) | |
6453 | DisableCellEditControl(); | |
6454 | ||
6455 | m_table->Clear(); | |
6456 | if (!GetBatchCount()) | |
6457 | m_gridWin->Refresh(); | |
6458 | } | |
6459 | } | |
6460 | ||
6461 | bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) | |
6462 | { | |
6463 | // TODO: something with updateLabels flag | |
6464 | ||
6465 | if ( !m_created ) | |
6466 | { | |
6467 | wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") ); | |
6468 | return false; | |
6469 | } | |
6470 | ||
6471 | if ( m_table ) | |
6472 | { | |
6473 | if (IsCellEditControlEnabled()) | |
6474 | DisableCellEditControl(); | |
6475 | ||
6476 | bool done = m_table->InsertRows( pos, numRows ); | |
6477 | return done; | |
6478 | ||
6479 | // the table will have sent the results of the insert row | |
6480 | // operation to this view object as a grid table message | |
6481 | } | |
6482 | ||
6483 | return false; | |
6484 | } | |
6485 | ||
6486 | bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) ) | |
6487 | { | |
6488 | // TODO: something with updateLabels flag | |
6489 | ||
6490 | if ( !m_created ) | |
6491 | { | |
6492 | wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") ); | |
6493 | return false; | |
6494 | } | |
6495 | ||
6496 | if ( m_table ) | |
6497 | { | |
6498 | bool done = m_table && m_table->AppendRows( numRows ); | |
6499 | return done; | |
6500 | ||
6501 | // the table will have sent the results of the append row | |
6502 | // operation to this view object as a grid table message | |
6503 | } | |
6504 | ||
6505 | return false; | |
6506 | } | |
6507 | ||
6508 | bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) | |
6509 | { | |
6510 | // TODO: something with updateLabels flag | |
6511 | ||
6512 | if ( !m_created ) | |
6513 | { | |
6514 | wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") ); | |
6515 | return false; | |
6516 | } | |
6517 | ||
6518 | if ( m_table ) | |
6519 | { | |
6520 | if (IsCellEditControlEnabled()) | |
6521 | DisableCellEditControl(); | |
6522 | ||
6523 | bool done = m_table->DeleteRows( pos, numRows ); | |
6524 | return done; | |
6525 | // the table will have sent the results of the delete row | |
6526 | // operation to this view object as a grid table message | |
6527 | } | |
6528 | ||
6529 | return false; | |
6530 | } | |
6531 | ||
6532 | bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) | |
6533 | { | |
6534 | // TODO: something with updateLabels flag | |
6535 | ||
6536 | if ( !m_created ) | |
6537 | { | |
6538 | wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") ); | |
6539 | return false; | |
6540 | } | |
6541 | ||
6542 | if ( m_table ) | |
6543 | { | |
6544 | if (IsCellEditControlEnabled()) | |
6545 | DisableCellEditControl(); | |
6546 | ||
6547 | bool done = m_table->InsertCols( pos, numCols ); | |
6548 | return done; | |
6549 | // the table will have sent the results of the insert col | |
6550 | // operation to this view object as a grid table message | |
6551 | } | |
6552 | ||
6553 | return false; | |
6554 | } | |
6555 | ||
6556 | bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) ) | |
6557 | { | |
6558 | // TODO: something with updateLabels flag | |
6559 | ||
6560 | if ( !m_created ) | |
6561 | { | |
6562 | wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") ); | |
6563 | return false; | |
6564 | } | |
6565 | ||
6566 | if ( m_table ) | |
6567 | { | |
6568 | bool done = m_table->AppendCols( numCols ); | |
6569 | return done; | |
6570 | // the table will have sent the results of the append col | |
6571 | // operation to this view object as a grid table message | |
6572 | } | |
6573 | ||
6574 | return false; | |
6575 | } | |
6576 | ||
6577 | bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) | |
6578 | { | |
6579 | // TODO: something with updateLabels flag | |
6580 | ||
6581 | if ( !m_created ) | |
6582 | { | |
6583 | wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") ); | |
6584 | return false; | |
6585 | } | |
6586 | ||
6587 | if ( m_table ) | |
6588 | { | |
6589 | if (IsCellEditControlEnabled()) | |
6590 | DisableCellEditControl(); | |
6591 | ||
6592 | bool done = m_table->DeleteCols( pos, numCols ); | |
6593 | return done; | |
6594 | // the table will have sent the results of the delete col | |
6595 | // operation to this view object as a grid table message | |
6596 | } | |
6597 | ||
6598 | return false; | |
6599 | } | |
6600 | ||
6601 | // | |
6602 | // ----- event handlers | |
6603 | // | |
6604 | ||
6605 | // Generate a grid event based on a mouse event and | |
6606 | // return the result of ProcessEvent() | |
6607 | // | |
6608 | int wxGrid::SendEvent( const wxEventType type, | |
6609 | int row, int col, | |
6610 | wxMouseEvent& mouseEv ) | |
6611 | { | |
6612 | bool claimed, vetoed; | |
6613 | ||
6614 | if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE ) | |
6615 | { | |
6616 | int rowOrCol = (row == -1 ? col : row); | |
6617 | ||
6618 | wxGridSizeEvent gridEvt( GetId(), | |
6619 | type, | |
6620 | this, | |
6621 | rowOrCol, | |
6622 | mouseEv.GetX() + GetRowLabelSize(), | |
6623 | mouseEv.GetY() + GetColLabelSize(), | |
6624 | mouseEv.ControlDown(), | |
6625 | mouseEv.ShiftDown(), | |
6626 | mouseEv.AltDown(), | |
6627 | mouseEv.MetaDown() ); | |
6628 | ||
6629 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6630 | vetoed = !gridEvt.IsAllowed(); | |
6631 | } | |
6632 | else if ( type == wxEVT_GRID_RANGE_SELECT ) | |
6633 | { | |
6634 | // Right now, it should _never_ end up here! | |
6635 | wxGridRangeSelectEvent gridEvt( GetId(), | |
6636 | type, | |
6637 | this, | |
6638 | m_selectingTopLeft, | |
6639 | m_selectingBottomRight, | |
6640 | true, | |
6641 | mouseEv.ControlDown(), | |
6642 | mouseEv.ShiftDown(), | |
6643 | mouseEv.AltDown(), | |
6644 | mouseEv.MetaDown() ); | |
6645 | ||
6646 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6647 | vetoed = !gridEvt.IsAllowed(); | |
6648 | } | |
6649 | else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK || | |
6650 | type == wxEVT_GRID_LABEL_LEFT_DCLICK || | |
6651 | type == wxEVT_GRID_LABEL_RIGHT_CLICK || | |
6652 | type == wxEVT_GRID_LABEL_RIGHT_DCLICK ) | |
6653 | { | |
6654 | wxPoint pos = mouseEv.GetPosition(); | |
6655 | ||
6656 | if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() ) | |
6657 | pos.y += GetColLabelSize(); | |
6658 | if ( mouseEv.GetEventObject() == GetGridColLabelWindow() ) | |
6659 | pos.x += GetRowLabelSize(); | |
6660 | ||
6661 | wxGridEvent gridEvt( GetId(), | |
6662 | type, | |
6663 | this, | |
6664 | row, col, | |
6665 | pos.x, | |
6666 | pos.y, | |
6667 | false, | |
6668 | mouseEv.ControlDown(), | |
6669 | mouseEv.ShiftDown(), | |
6670 | mouseEv.AltDown(), | |
6671 | mouseEv.MetaDown() ); | |
6672 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6673 | vetoed = !gridEvt.IsAllowed(); | |
6674 | } | |
6675 | else | |
6676 | { | |
6677 | wxGridEvent gridEvt( GetId(), | |
6678 | type, | |
6679 | this, | |
6680 | row, col, | |
6681 | mouseEv.GetX() + GetRowLabelSize(), | |
6682 | mouseEv.GetY() + GetColLabelSize(), | |
6683 | false, | |
6684 | mouseEv.ControlDown(), | |
6685 | mouseEv.ShiftDown(), | |
6686 | mouseEv.AltDown(), | |
6687 | mouseEv.MetaDown() ); | |
6688 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6689 | vetoed = !gridEvt.IsAllowed(); | |
6690 | } | |
6691 | ||
6692 | // A Veto'd event may not be `claimed' so test this first | |
6693 | if (vetoed) | |
6694 | return -1; | |
6695 | ||
6696 | return claimed ? 1 : 0; | |
6697 | } | |
6698 | ||
6699 | // Generate a grid event of specified type and return the result | |
6700 | // of ProcessEvent(). | |
6701 | // | |
6702 | int wxGrid::SendEvent( const wxEventType type, | |
6703 | int row, int col ) | |
6704 | { | |
6705 | bool claimed, vetoed; | |
6706 | ||
6707 | if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE ) | |
6708 | { | |
6709 | int rowOrCol = (row == -1 ? col : row); | |
6710 | ||
6711 | wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol ); | |
6712 | ||
6713 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6714 | vetoed = !gridEvt.IsAllowed(); | |
6715 | } | |
6716 | else | |
6717 | { | |
6718 | wxGridEvent gridEvt( GetId(), type, this, row, col ); | |
6719 | ||
6720 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
6721 | vetoed = !gridEvt.IsAllowed(); | |
6722 | } | |
6723 | ||
6724 | // A Veto'd event may not be `claimed' so test this first | |
6725 | if (vetoed) | |
6726 | return -1; | |
6727 | ||
6728 | return claimed ? 1 : 0; | |
6729 | } | |
6730 | ||
6731 | void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
6732 | { | |
6733 | // needed to prevent zillions of paint events on MSW | |
6734 | wxPaintDC dc(this); | |
6735 | } | |
6736 | ||
6737 | void wxGrid::Refresh(bool eraseb, const wxRect* rect) | |
6738 | { | |
6739 | // Don't do anything if between Begin/EndBatch... | |
6740 | // EndBatch() will do all this on the last nested one anyway. | |
6741 | if (! GetBatchCount()) | |
6742 | { | |
6743 | // Refresh to get correct scrolled position: | |
6744 | wxScrolledWindow::Refresh(eraseb, rect); | |
6745 | ||
6746 | if (rect) | |
6747 | { | |
6748 | int rect_x, rect_y, rectWidth, rectHeight; | |
6749 | int width_label, width_cell, height_label, height_cell; | |
6750 | int x, y; | |
6751 | ||
6752 | // Copy rectangle can get scroll offsets.. | |
6753 | rect_x = rect->GetX(); | |
6754 | rect_y = rect->GetY(); | |
6755 | rectWidth = rect->GetWidth(); | |
6756 | rectHeight = rect->GetHeight(); | |
6757 | ||
6758 | width_label = m_rowLabelWidth - rect_x; | |
6759 | if (width_label > rectWidth) | |
6760 | width_label = rectWidth; | |
6761 | ||
6762 | height_label = m_colLabelHeight - rect_y; | |
6763 | if (height_label > rectHeight) | |
6764 | height_label = rectHeight; | |
6765 | ||
6766 | if (rect_x > m_rowLabelWidth) | |
6767 | { | |
6768 | x = rect_x - m_rowLabelWidth; | |
6769 | width_cell = rectWidth; | |
6770 | } | |
6771 | else | |
6772 | { | |
6773 | x = 0; | |
6774 | width_cell = rectWidth - (m_rowLabelWidth - rect_x); | |
6775 | } | |
6776 | ||
6777 | if (rect_y > m_colLabelHeight) | |
6778 | { | |
6779 | y = rect_y - m_colLabelHeight; | |
6780 | height_cell = rectHeight; | |
6781 | } | |
6782 | else | |
6783 | { | |
6784 | y = 0; | |
6785 | height_cell = rectHeight - (m_colLabelHeight - rect_y); | |
6786 | } | |
6787 | ||
6788 | // Paint corner label part intersecting rect. | |
6789 | if ( width_label > 0 && height_label > 0 ) | |
6790 | { | |
6791 | wxRect anotherrect(rect_x, rect_y, width_label, height_label); | |
6792 | m_cornerLabelWin->Refresh(eraseb, &anotherrect); | |
6793 | } | |
6794 | ||
6795 | // Paint col labels part intersecting rect. | |
6796 | if ( width_cell > 0 && height_label > 0 ) | |
6797 | { | |
6798 | wxRect anotherrect(x, rect_y, width_cell, height_label); | |
6799 | m_colLabelWin->Refresh(eraseb, &anotherrect); | |
6800 | } | |
6801 | ||
6802 | // Paint row labels part intersecting rect. | |
6803 | if ( width_label > 0 && height_cell > 0 ) | |
6804 | { | |
6805 | wxRect anotherrect(rect_x, y, width_label, height_cell); | |
6806 | m_rowLabelWin->Refresh(eraseb, &anotherrect); | |
6807 | } | |
6808 | ||
6809 | // Paint cell area part intersecting rect. | |
6810 | if ( width_cell > 0 && height_cell > 0 ) | |
6811 | { | |
6812 | wxRect anotherrect(x, y, width_cell, height_cell); | |
6813 | m_gridWin->Refresh(eraseb, &anotherrect); | |
6814 | } | |
6815 | } | |
6816 | else | |
6817 | { | |
6818 | m_cornerLabelWin->Refresh(eraseb, NULL); | |
6819 | m_colLabelWin->Refresh(eraseb, NULL); | |
6820 | m_rowLabelWin->Refresh(eraseb, NULL); | |
6821 | m_gridWin->Refresh(eraseb, NULL); | |
6822 | } | |
6823 | } | |
6824 | } | |
6825 | ||
6826 | void wxGrid::OnSize( wxSizeEvent& event ) | |
6827 | { | |
6828 | // position the child windows | |
6829 | CalcWindowSizes(); | |
6830 | ||
6831 | // don't call CalcDimensions() from here, the base class handles the size | |
6832 | // changes itself | |
6833 | event.Skip(); | |
6834 | } | |
6835 | ||
6836 | void wxGrid::OnKeyDown( wxKeyEvent& event ) | |
6837 | { | |
6838 | if ( m_inOnKeyDown ) | |
6839 | { | |
6840 | // shouldn't be here - we are going round in circles... | |
6841 | // | |
6842 | wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") ); | |
6843 | } | |
6844 | ||
6845 | m_inOnKeyDown = true; | |
6846 | ||
6847 | // propagate the event up and see if it gets processed | |
6848 | wxWindow *parent = GetParent(); | |
6849 | wxKeyEvent keyEvt( event ); | |
6850 | keyEvt.SetEventObject( parent ); | |
6851 | ||
6852 | if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) ) | |
6853 | { | |
6854 | if (GetLayoutDirection() == wxLayout_RightToLeft) | |
6855 | { | |
6856 | if (event.GetKeyCode() == WXK_RIGHT) | |
6857 | event.m_keyCode = WXK_LEFT; | |
6858 | else if (event.GetKeyCode() == WXK_LEFT) | |
6859 | event.m_keyCode = WXK_RIGHT; | |
6860 | } | |
6861 | ||
6862 | // try local handlers | |
6863 | switch ( event.GetKeyCode() ) | |
6864 | { | |
6865 | case WXK_UP: | |
6866 | if ( event.ControlDown() ) | |
6867 | MoveCursorUpBlock( event.ShiftDown() ); | |
6868 | else | |
6869 | MoveCursorUp( event.ShiftDown() ); | |
6870 | break; | |
6871 | ||
6872 | case WXK_DOWN: | |
6873 | if ( event.ControlDown() ) | |
6874 | MoveCursorDownBlock( event.ShiftDown() ); | |
6875 | else | |
6876 | MoveCursorDown( event.ShiftDown() ); | |
6877 | break; | |
6878 | ||
6879 | case WXK_LEFT: | |
6880 | if ( event.ControlDown() ) | |
6881 | MoveCursorLeftBlock( event.ShiftDown() ); | |
6882 | else | |
6883 | MoveCursorLeft( event.ShiftDown() ); | |
6884 | break; | |
6885 | ||
6886 | case WXK_RIGHT: | |
6887 | if ( event.ControlDown() ) | |
6888 | MoveCursorRightBlock( event.ShiftDown() ); | |
6889 | else | |
6890 | MoveCursorRight( event.ShiftDown() ); | |
6891 | break; | |
6892 | ||
6893 | case WXK_RETURN: | |
6894 | case WXK_NUMPAD_ENTER: | |
6895 | if ( event.ControlDown() ) | |
6896 | { | |
6897 | event.Skip(); // to let the edit control have the return | |
6898 | } | |
6899 | else | |
6900 | { | |
6901 | if ( GetGridCursorRow() < GetNumberRows()-1 ) | |
6902 | { | |
6903 | MoveCursorDown( event.ShiftDown() ); | |
6904 | } | |
6905 | else | |
6906 | { | |
6907 | // at the bottom of a column | |
6908 | DisableCellEditControl(); | |
6909 | } | |
6910 | } | |
6911 | break; | |
6912 | ||
6913 | case WXK_ESCAPE: | |
6914 | ClearSelection(); | |
6915 | break; | |
6916 | ||
6917 | case WXK_TAB: | |
6918 | if (event.ShiftDown()) | |
6919 | { | |
6920 | if ( GetGridCursorCol() > 0 ) | |
6921 | { | |
6922 | MoveCursorLeft( false ); | |
6923 | } | |
6924 | else | |
6925 | { | |
6926 | // at left of grid | |
6927 | DisableCellEditControl(); | |
6928 | } | |
6929 | } | |
6930 | else | |
6931 | { | |
6932 | if ( GetGridCursorCol() < GetNumberCols() - 1 ) | |
6933 | { | |
6934 | MoveCursorRight( false ); | |
6935 | } | |
6936 | else | |
6937 | { | |
6938 | // at right of grid | |
6939 | DisableCellEditControl(); | |
6940 | } | |
6941 | } | |
6942 | break; | |
6943 | ||
6944 | case WXK_HOME: | |
6945 | if ( event.ControlDown() ) | |
6946 | { | |
6947 | MakeCellVisible( 0, 0 ); | |
6948 | SetCurrentCell( 0, 0 ); | |
6949 | } | |
6950 | else | |
6951 | { | |
6952 | event.Skip(); | |
6953 | } | |
6954 | break; | |
6955 | ||
6956 | case WXK_END: | |
6957 | if ( event.ControlDown() ) | |
6958 | { | |
6959 | MakeCellVisible( m_numRows - 1, m_numCols - 1 ); | |
6960 | SetCurrentCell( m_numRows - 1, m_numCols - 1 ); | |
6961 | } | |
6962 | else | |
6963 | { | |
6964 | event.Skip(); | |
6965 | } | |
6966 | break; | |
6967 | ||
6968 | case WXK_PAGEUP: | |
6969 | MovePageUp(); | |
6970 | break; | |
6971 | ||
6972 | case WXK_PAGEDOWN: | |
6973 | MovePageDown(); | |
6974 | break; | |
6975 | ||
6976 | case WXK_SPACE: | |
6977 | if ( event.ControlDown() ) | |
6978 | { | |
6979 | if ( m_selection ) | |
6980 | { | |
6981 | m_selection->ToggleCellSelection( | |
6982 | m_currentCellCoords.GetRow(), | |
6983 | m_currentCellCoords.GetCol(), | |
6984 | event.ControlDown(), | |
6985 | event.ShiftDown(), | |
6986 | event.AltDown(), | |
6987 | event.MetaDown() ); | |
6988 | } | |
6989 | break; | |
6990 | } | |
6991 | ||
6992 | if ( !IsEditable() ) | |
6993 | MoveCursorRight( false ); | |
6994 | else | |
6995 | event.Skip(); | |
6996 | break; | |
6997 | ||
6998 | default: | |
6999 | event.Skip(); | |
7000 | break; | |
7001 | } | |
7002 | } | |
7003 | ||
7004 | m_inOnKeyDown = false; | |
7005 | } | |
7006 | ||
7007 | void wxGrid::OnKeyUp( wxKeyEvent& event ) | |
7008 | { | |
7009 | // try local handlers | |
7010 | // | |
7011 | if ( event.GetKeyCode() == WXK_SHIFT ) | |
7012 | { | |
7013 | if ( m_selectingTopLeft != wxGridNoCellCoords && | |
7014 | m_selectingBottomRight != wxGridNoCellCoords ) | |
7015 | { | |
7016 | if ( m_selection ) | |
7017 | { | |
7018 | m_selection->SelectBlock( | |
7019 | m_selectingTopLeft.GetRow(), | |
7020 | m_selectingTopLeft.GetCol(), | |
7021 | m_selectingBottomRight.GetRow(), | |
7022 | m_selectingBottomRight.GetCol(), | |
7023 | event.ControlDown(), | |
7024 | true, | |
7025 | event.AltDown(), | |
7026 | event.MetaDown() ); | |
7027 | } | |
7028 | } | |
7029 | ||
7030 | m_selectingTopLeft = wxGridNoCellCoords; | |
7031 | m_selectingBottomRight = wxGridNoCellCoords; | |
7032 | m_selectingKeyboard = wxGridNoCellCoords; | |
7033 | } | |
7034 | } | |
7035 | ||
7036 | void wxGrid::OnChar( wxKeyEvent& event ) | |
7037 | { | |
7038 | // is it possible to edit the current cell at all? | |
7039 | if ( !IsCellEditControlEnabled() && CanEnableCellControl() ) | |
7040 | { | |
7041 | // yes, now check whether the cells editor accepts the key | |
7042 | int row = m_currentCellCoords.GetRow(); | |
7043 | int col = m_currentCellCoords.GetCol(); | |
7044 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
7045 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); | |
7046 | ||
7047 | // <F2> is special and will always start editing, for | |
7048 | // other keys - ask the editor itself | |
7049 | if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers()) | |
7050 | || editor->IsAcceptedKey(event) ) | |
7051 | { | |
7052 | // ensure cell is visble | |
7053 | MakeCellVisible(row, col); | |
7054 | EnableCellEditControl(); | |
7055 | ||
7056 | // a problem can arise if the cell is not completely | |
7057 | // visible (even after calling MakeCellVisible the | |
7058 | // control is not created and calling StartingKey will | |
7059 | // crash the app | |
7060 | if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled ) | |
7061 | editor->StartingKey(event); | |
7062 | } | |
7063 | else | |
7064 | { | |
7065 | event.Skip(); | |
7066 | } | |
7067 | ||
7068 | editor->DecRef(); | |
7069 | attr->DecRef(); | |
7070 | } | |
7071 | else | |
7072 | { | |
7073 | event.Skip(); | |
7074 | } | |
7075 | } | |
7076 | ||
7077 | void wxGrid::OnEraseBackground(wxEraseEvent&) | |
7078 | { | |
7079 | } | |
7080 | ||
7081 | void wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) | |
7082 | { | |
7083 | if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) ) | |
7084 | { | |
7085 | // the event has been intercepted - do nothing | |
7086 | return; | |
7087 | } | |
7088 | ||
7089 | wxClientDC dc( m_gridWin ); | |
7090 | PrepareDC( dc ); | |
7091 | ||
7092 | if ( m_currentCellCoords != wxGridNoCellCoords ) | |
7093 | { | |
7094 | DisableCellEditControl(); | |
7095 | ||
7096 | if ( IsVisible( m_currentCellCoords, false ) ) | |
7097 | { | |
7098 | wxRect r; | |
7099 | r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords ); | |
7100 | if ( !m_gridLinesEnabled ) | |
7101 | { | |
7102 | r.x--; | |
7103 | r.y--; | |
7104 | r.width++; | |
7105 | r.height++; | |
7106 | } | |
7107 | ||
7108 | wxGridCellCoordsArray cells = CalcCellsExposed( r ); | |
7109 | ||
7110 | // Otherwise refresh redraws the highlight! | |
7111 | m_currentCellCoords = coords; | |
7112 | ||
7113 | DrawGridCellArea( dc, cells ); | |
7114 | DrawAllGridLines( dc, r ); | |
7115 | } | |
7116 | } | |
7117 | ||
7118 | m_currentCellCoords = coords; | |
7119 | ||
7120 | wxGridCellAttr *attr = GetCellAttr( coords ); | |
7121 | DrawCellHighlight( dc, attr ); | |
7122 | attr->DecRef(); | |
7123 | } | |
7124 | ||
7125 | void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol ) | |
7126 | { | |
7127 | int temp; | |
7128 | wxGridCellCoords updateTopLeft, updateBottomRight; | |
7129 | ||
7130 | if ( m_selection ) | |
7131 | { | |
7132 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows ) | |
7133 | { | |
7134 | leftCol = 0; | |
7135 | rightCol = GetNumberCols() - 1; | |
7136 | } | |
7137 | else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns ) | |
7138 | { | |
7139 | topRow = 0; | |
7140 | bottomRow = GetNumberRows() - 1; | |
7141 | } | |
7142 | } | |
7143 | ||
7144 | if ( topRow > bottomRow ) | |
7145 | { | |
7146 | temp = topRow; | |
7147 | topRow = bottomRow; | |
7148 | bottomRow = temp; | |
7149 | } | |
7150 | ||
7151 | if ( leftCol > rightCol ) | |
7152 | { | |
7153 | temp = leftCol; | |
7154 | leftCol = rightCol; | |
7155 | rightCol = temp; | |
7156 | } | |
7157 | ||
7158 | updateTopLeft = wxGridCellCoords( topRow, leftCol ); | |
7159 | updateBottomRight = wxGridCellCoords( bottomRow, rightCol ); | |
7160 | ||
7161 | // First the case that we selected a completely new area | |
7162 | if ( m_selectingTopLeft == wxGridNoCellCoords || | |
7163 | m_selectingBottomRight == wxGridNoCellCoords ) | |
7164 | { | |
7165 | wxRect rect; | |
7166 | rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ), | |
7167 | wxGridCellCoords ( bottomRow, rightCol ) ); | |
7168 | m_gridWin->Refresh( false, &rect ); | |
7169 | } | |
7170 | ||
7171 | // Now handle changing an existing selection area. | |
7172 | else if ( m_selectingTopLeft != updateTopLeft || | |
7173 | m_selectingBottomRight != updateBottomRight ) | |
7174 | { | |
7175 | // Compute two optimal update rectangles: | |
7176 | // Either one rectangle is a real subset of the | |
7177 | // other, or they are (almost) disjoint! | |
7178 | wxRect rect[4]; | |
7179 | bool need_refresh[4]; | |
7180 | need_refresh[0] = | |
7181 | need_refresh[1] = | |
7182 | need_refresh[2] = | |
7183 | need_refresh[3] = false; | |
7184 | int i; | |
7185 | ||
7186 | // Store intermediate values | |
7187 | wxCoord oldLeft = m_selectingTopLeft.GetCol(); | |
7188 | wxCoord oldTop = m_selectingTopLeft.GetRow(); | |
7189 | wxCoord oldRight = m_selectingBottomRight.GetCol(); | |
7190 | wxCoord oldBottom = m_selectingBottomRight.GetRow(); | |
7191 | ||
7192 | // Determine the outer/inner coordinates. | |
7193 | if (oldLeft > leftCol) | |
7194 | { | |
7195 | temp = oldLeft; | |
7196 | oldLeft = leftCol; | |
7197 | leftCol = temp; | |
7198 | } | |
7199 | if (oldTop > topRow ) | |
7200 | { | |
7201 | temp = oldTop; | |
7202 | oldTop = topRow; | |
7203 | topRow = temp; | |
7204 | } | |
7205 | if (oldRight < rightCol ) | |
7206 | { | |
7207 | temp = oldRight; | |
7208 | oldRight = rightCol; | |
7209 | rightCol = temp; | |
7210 | } | |
7211 | if (oldBottom < bottomRow) | |
7212 | { | |
7213 | temp = oldBottom; | |
7214 | oldBottom = bottomRow; | |
7215 | bottomRow = temp; | |
7216 | } | |
7217 | ||
7218 | // Now, either the stuff marked old is the outer | |
7219 | // rectangle or we don't have a situation where one | |
7220 | // is contained in the other. | |
7221 | ||
7222 | if ( oldLeft < leftCol ) | |
7223 | { | |
7224 | // Refresh the newly selected or deselected | |
7225 | // area to the left of the old or new selection. | |
7226 | need_refresh[0] = true; | |
7227 | rect[0] = BlockToDeviceRect( | |
7228 | wxGridCellCoords( oldTop, oldLeft ), | |
7229 | wxGridCellCoords( oldBottom, leftCol - 1 ) ); | |
7230 | } | |
7231 | ||
7232 | if ( oldTop < topRow ) | |
7233 | { | |
7234 | // Refresh the newly selected or deselected | |
7235 | // area above the old or new selection. | |
7236 | need_refresh[1] = true; | |
7237 | rect[1] = BlockToDeviceRect( | |
7238 | wxGridCellCoords( oldTop, leftCol ), | |
7239 | wxGridCellCoords( topRow - 1, rightCol ) ); | |
7240 | } | |
7241 | ||
7242 | if ( oldRight > rightCol ) | |
7243 | { | |
7244 | // Refresh the newly selected or deselected | |
7245 | // area to the right of the old or new selection. | |
7246 | need_refresh[2] = true; | |
7247 | rect[2] = BlockToDeviceRect( | |
7248 | wxGridCellCoords( oldTop, rightCol + 1 ), | |
7249 | wxGridCellCoords( oldBottom, oldRight ) ); | |
7250 | } | |
7251 | ||
7252 | if ( oldBottom > bottomRow ) | |
7253 | { | |
7254 | // Refresh the newly selected or deselected | |
7255 | // area below the old or new selection. | |
7256 | need_refresh[3] = true; | |
7257 | rect[3] = BlockToDeviceRect( | |
7258 | wxGridCellCoords( bottomRow + 1, leftCol ), | |
7259 | wxGridCellCoords( oldBottom, rightCol ) ); | |
7260 | } | |
7261 | ||
7262 | // various Refresh() calls | |
7263 | for (i = 0; i < 4; i++ ) | |
7264 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
7265 | m_gridWin->Refresh( false, &(rect[i]) ); | |
7266 | } | |
7267 | ||
7268 | // change selection | |
7269 | m_selectingTopLeft = updateTopLeft; | |
7270 | m_selectingBottomRight = updateBottomRight; | |
7271 | } | |
7272 | ||
7273 | // | |
7274 | // ------ functions to get/send data (see also public functions) | |
7275 | // | |
7276 | ||
7277 | bool wxGrid::GetModelValues() | |
7278 | { | |
7279 | // Hide the editor, so it won't hide a changed value. | |
7280 | HideCellEditControl(); | |
7281 | ||
7282 | if ( m_table ) | |
7283 | { | |
7284 | // all we need to do is repaint the grid | |
7285 | // | |
7286 | m_gridWin->Refresh(); | |
7287 | return true; | |
7288 | } | |
7289 | ||
7290 | return false; | |
7291 | } | |
7292 | ||
7293 | bool wxGrid::SetModelValues() | |
7294 | { | |
7295 | int row, col; | |
7296 | ||
7297 | // Disable the editor, so it won't hide a changed value. | |
7298 | // Do we also want to save the current value of the editor first? | |
7299 | // I think so ... | |
7300 | DisableCellEditControl(); | |
7301 | ||
7302 | if ( m_table ) | |
7303 | { | |
7304 | for ( row = 0; row < m_numRows; row++ ) | |
7305 | { | |
7306 | for ( col = 0; col < m_numCols; col++ ) | |
7307 | { | |
7308 | m_table->SetValue( row, col, GetCellValue(row, col) ); | |
7309 | } | |
7310 | } | |
7311 | ||
7312 | return true; | |
7313 | } | |
7314 | ||
7315 | return false; | |
7316 | } | |
7317 | ||
7318 | // Note - this function only draws cells that are in the list of | |
7319 | // exposed cells (usually set from the update region by | |
7320 | // CalcExposedCells) | |
7321 | // | |
7322 | void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells ) | |
7323 | { | |
7324 | if ( !m_numRows || !m_numCols ) | |
7325 | return; | |
7326 | ||
7327 | int i, numCells = cells.GetCount(); | |
7328 | int row, col, cell_rows, cell_cols; | |
7329 | wxGridCellCoordsArray redrawCells; | |
7330 | ||
7331 | for ( i = numCells - 1; i >= 0; i-- ) | |
7332 | { | |
7333 | row = cells[i].GetRow(); | |
7334 | col = cells[i].GetCol(); | |
7335 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
7336 | ||
7337 | // If this cell is part of a multicell block, find owner for repaint | |
7338 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
7339 | { | |
7340 | wxGridCellCoords cell( row + cell_rows, col + cell_cols ); | |
7341 | bool marked = false; | |
7342 | for ( int j = 0; j < numCells; j++ ) | |
7343 | { | |
7344 | if ( cell == cells[j] ) | |
7345 | { | |
7346 | marked = true; | |
7347 | break; | |
7348 | } | |
7349 | } | |
7350 | ||
7351 | if (!marked) | |
7352 | { | |
7353 | int count = redrawCells.GetCount(); | |
7354 | for (int j = 0; j < count; j++) | |
7355 | { | |
7356 | if ( cell == redrawCells[j] ) | |
7357 | { | |
7358 | marked = true; | |
7359 | break; | |
7360 | } | |
7361 | } | |
7362 | ||
7363 | if (!marked) | |
7364 | redrawCells.Add( cell ); | |
7365 | } | |
7366 | ||
7367 | // don't bother drawing this cell | |
7368 | continue; | |
7369 | } | |
7370 | ||
7371 | // If this cell is empty, find cell to left that might want to overflow | |
7372 | if (m_table && m_table->IsEmptyCell(row, col)) | |
7373 | { | |
7374 | for ( int l = 0; l < cell_rows; l++ ) | |
7375 | { | |
7376 | // find a cell in this row to leave already marked for repaint | |
7377 | int left = col; | |
7378 | for (int k = 0; k < int(redrawCells.GetCount()); k++) | |
7379 | if ((redrawCells[k].GetCol() < left) && | |
7380 | (redrawCells[k].GetRow() == row)) | |
7381 | { | |
7382 | left = redrawCells[k].GetCol(); | |
7383 | } | |
7384 | ||
7385 | if (left == col) | |
7386 | left = 0; // oh well | |
7387 | ||
7388 | for (int j = col - 1; j >= left; j--) | |
7389 | { | |
7390 | if (!m_table->IsEmptyCell(row + l, j)) | |
7391 | { | |
7392 | if (GetCellOverflow(row + l, j)) | |
7393 | { | |
7394 | wxGridCellCoords cell(row + l, j); | |
7395 | bool marked = false; | |
7396 | ||
7397 | for (int k = 0; k < numCells; k++) | |
7398 | { | |
7399 | if ( cell == cells[k] ) | |
7400 | { | |
7401 | marked = true; | |
7402 | break; | |
7403 | } | |
7404 | } | |
7405 | ||
7406 | if (!marked) | |
7407 | { | |
7408 | int count = redrawCells.GetCount(); | |
7409 | for (int k = 0; k < count; k++) | |
7410 | { | |
7411 | if ( cell == redrawCells[k] ) | |
7412 | { | |
7413 | marked = true; | |
7414 | break; | |
7415 | } | |
7416 | } | |
7417 | if (!marked) | |
7418 | redrawCells.Add( cell ); | |
7419 | } | |
7420 | } | |
7421 | break; | |
7422 | } | |
7423 | } | |
7424 | } | |
7425 | } | |
7426 | ||
7427 | DrawCell( dc, cells[i] ); | |
7428 | } | |
7429 | ||
7430 | numCells = redrawCells.GetCount(); | |
7431 | ||
7432 | for ( i = numCells - 1; i >= 0; i-- ) | |
7433 | { | |
7434 | DrawCell( dc, redrawCells[i] ); | |
7435 | } | |
7436 | } | |
7437 | ||
7438 | void wxGrid::DrawGridSpace( wxDC& dc ) | |
7439 | { | |
7440 | int cw, ch; | |
7441 | m_gridWin->GetClientSize( &cw, &ch ); | |
7442 | ||
7443 | int right, bottom; | |
7444 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7445 | ||
7446 | int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0; | |
7447 | int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0; | |
7448 | ||
7449 | if ( right > rightCol || bottom > bottomRow ) | |
7450 | { | |
7451 | int left, top; | |
7452 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7453 | ||
7454 | dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) ); | |
7455 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
7456 | ||
7457 | if ( right > rightCol ) | |
7458 | { | |
7459 | dc.DrawRectangle( rightCol, top, right - rightCol, ch ); | |
7460 | } | |
7461 | ||
7462 | if ( bottom > bottomRow ) | |
7463 | { | |
7464 | dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow ); | |
7465 | } | |
7466 | } | |
7467 | } | |
7468 | ||
7469 | void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) | |
7470 | { | |
7471 | int row = coords.GetRow(); | |
7472 | int col = coords.GetCol(); | |
7473 | ||
7474 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
7475 | return; | |
7476 | ||
7477 | // we draw the cell border ourselves | |
7478 | #if !WXGRID_DRAW_LINES | |
7479 | if ( m_gridLinesEnabled ) | |
7480 | DrawCellBorder( dc, coords ); | |
7481 | #endif | |
7482 | ||
7483 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
7484 | ||
7485 | bool isCurrent = coords == m_currentCellCoords; | |
7486 | ||
7487 | wxRect rect = CellToRect( row, col ); | |
7488 | ||
7489 | // if the editor is shown, we should use it and not the renderer | |
7490 | // Note: However, only if it is really _shown_, i.e. not hidden! | |
7491 | if ( isCurrent && IsCellEditControlShown() ) | |
7492 | { | |
7493 | // NB: this "#if..." is temporary and fixes a problem where the | |
7494 | // edit control is erased by this code after being rendered. | |
7495 | // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered | |
7496 | // implicitly, causing this out-of order render. | |
7497 | #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS | |
7498 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); | |
7499 | editor->PaintBackground(rect, attr); | |
7500 | editor->DecRef(); | |
7501 | #endif | |
7502 | } | |
7503 | else | |
7504 | { | |
7505 | // but all the rest is drawn by the cell renderer and hence may be customized | |
7506 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); | |
7507 | renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); | |
7508 | renderer->DecRef(); | |
7509 | } | |
7510 | ||
7511 | attr->DecRef(); | |
7512 | } | |
7513 | ||
7514 | void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ) | |
7515 | { | |
7516 | int row = m_currentCellCoords.GetRow(); | |
7517 | int col = m_currentCellCoords.GetCol(); | |
7518 | ||
7519 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
7520 | return; | |
7521 | ||
7522 | wxRect rect = CellToRect(row, col); | |
7523 | ||
7524 | // hmmm... what could we do here to show that the cell is disabled? | |
7525 | // for now, I just draw a thinner border than for the other ones, but | |
7526 | // it doesn't look really good | |
7527 | ||
7528 | int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth; | |
7529 | ||
7530 | if (penWidth > 0) | |
7531 | { | |
7532 | // The center of the drawn line is where the position/width/height of | |
7533 | // the rectangle is actually at (on wxMSW at least), so the | |
7534 | // size of the rectangle is reduced to compensate for the thickness of | |
7535 | // the line. If this is too strange on non-wxMSW platforms then | |
7536 | // please #ifdef this appropriately. | |
7537 | rect.x += penWidth / 2; | |
7538 | rect.y += penWidth / 2; | |
7539 | rect.width -= penWidth - 1; | |
7540 | rect.height -= penWidth - 1; | |
7541 | ||
7542 | // Now draw the rectangle | |
7543 | // use the cellHighlightColour if the cell is inside a selection, this | |
7544 | // will ensure the cell is always visible. | |
7545 | dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground : m_cellHighlightColour, penWidth, wxSOLID)); | |
7546 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
7547 | dc.DrawRectangle(rect); | |
7548 | } | |
7549 | ||
7550 | #if 0 | |
7551 | // VZ: my experiments with 3D borders... | |
7552 | ||
7553 | // how to properly set colours for arbitrary bg? | |
7554 | wxCoord x1 = rect.x, | |
7555 | y1 = rect.y, | |
7556 | x2 = rect.x + rect.width - 1, | |
7557 | y2 = rect.y + rect.height - 1; | |
7558 | ||
7559 | dc.SetPen(*wxWHITE_PEN); | |
7560 | dc.DrawLine(x1, y1, x2, y1); | |
7561 | dc.DrawLine(x1, y1, x1, y2); | |
7562 | ||
7563 | dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1); | |
7564 | dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2); | |
7565 | ||
7566 | dc.SetPen(*wxBLACK_PEN); | |
7567 | dc.DrawLine(x1, y2, x2, y2); | |
7568 | dc.DrawLine(x2, y1, x2, y2 + 1); | |
7569 | #endif | |
7570 | } | |
7571 | ||
7572 | wxPen wxGrid::GetDefaultGridLinePen() | |
7573 | { | |
7574 | return wxPen(GetGridLineColour(), 1, wxSOLID); | |
7575 | } | |
7576 | ||
7577 | wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row)) | |
7578 | { | |
7579 | return GetDefaultGridLinePen(); | |
7580 | } | |
7581 | ||
7582 | wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col)) | |
7583 | { | |
7584 | return GetDefaultGridLinePen(); | |
7585 | } | |
7586 | ||
7587 | void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) | |
7588 | { | |
7589 | int row = coords.GetRow(); | |
7590 | int col = coords.GetCol(); | |
7591 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
7592 | return; | |
7593 | ||
7594 | ||
7595 | wxRect rect = CellToRect( row, col ); | |
7596 | ||
7597 | // right hand border | |
7598 | dc.SetPen( GetColGridLinePen(col) ); | |
7599 | dc.DrawLine( rect.x + rect.width, rect.y, | |
7600 | rect.x + rect.width, rect.y + rect.height + 1 ); | |
7601 | ||
7602 | // bottom border | |
7603 | dc.SetPen( GetRowGridLinePen(row) ); | |
7604 | dc.DrawLine( rect.x, rect.y + rect.height, | |
7605 | rect.x + rect.width, rect.y + rect.height); | |
7606 | } | |
7607 | ||
7608 | void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells) | |
7609 | { | |
7610 | // This if block was previously in wxGrid::OnPaint but that doesn't | |
7611 | // seem to get called under wxGTK - MB | |
7612 | // | |
7613 | if ( m_currentCellCoords == wxGridNoCellCoords && | |
7614 | m_numRows && m_numCols ) | |
7615 | { | |
7616 | m_currentCellCoords.Set(0, 0); | |
7617 | } | |
7618 | ||
7619 | if ( IsCellEditControlShown() ) | |
7620 | { | |
7621 | // don't show highlight when the edit control is shown | |
7622 | return; | |
7623 | } | |
7624 | ||
7625 | // if the active cell was repainted, repaint its highlight too because it | |
7626 | // might have been damaged by the grid lines | |
7627 | size_t count = cells.GetCount(); | |
7628 | for ( size_t n = 0; n < count; n++ ) | |
7629 | { | |
7630 | if ( cells[n] == m_currentCellCoords ) | |
7631 | { | |
7632 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
7633 | DrawCellHighlight(dc, attr); | |
7634 | attr->DecRef(); | |
7635 | ||
7636 | break; | |
7637 | } | |
7638 | } | |
7639 | } | |
7640 | ||
7641 | // TODO: remove this ??? | |
7642 | // This is used to redraw all grid lines e.g. when the grid line colour | |
7643 | // has been changed | |
7644 | // | |
7645 | void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) ) | |
7646 | { | |
7647 | #if !WXGRID_DRAW_LINES | |
7648 | return; | |
7649 | #endif | |
7650 | ||
7651 | if ( !m_gridLinesEnabled || !m_numRows || !m_numCols ) | |
7652 | return; | |
7653 | ||
7654 | int top, bottom, left, right; | |
7655 | ||
7656 | #if 0 //#ifndef __WXGTK__ | |
7657 | if (reg.IsEmpty()) | |
7658 | { | |
7659 | int cw, ch; | |
7660 | m_gridWin->GetClientSize(&cw, &ch); | |
7661 | ||
7662 | // virtual coords of visible area | |
7663 | // | |
7664 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7665 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7666 | } | |
7667 | else | |
7668 | { | |
7669 | wxCoord x, y, w, h; | |
7670 | reg.GetBox(x, y, w, h); | |
7671 | CalcUnscrolledPosition( x, y, &left, &top ); | |
7672 | CalcUnscrolledPosition( x + w, y + h, &right, &bottom ); | |
7673 | } | |
7674 | #else | |
7675 | int cw, ch; | |
7676 | m_gridWin->GetClientSize(&cw, &ch); | |
7677 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7678 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7679 | #endif | |
7680 | ||
7681 | // avoid drawing grid lines past the last row and col | |
7682 | // | |
7683 | right = wxMin( right, GetColRight(GetColAt( m_numCols - 1 )) ); | |
7684 | bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) ); | |
7685 | ||
7686 | // no gridlines inside multicells, clip them out | |
7687 | int leftCol = GetColPos( internalXToCol(left) ); | |
7688 | int topRow = internalYToRow(top); | |
7689 | int rightCol = GetColPos( internalXToCol(right) ); | |
7690 | int bottomRow = internalYToRow(bottom); | |
7691 | ||
7692 | #ifndef __WXMAC__ | |
7693 | // CS: I don't know why suddenly unscrolled coordinates are used for clipping | |
7694 | wxRegion clippedcells(0, 0, cw, ch); | |
7695 | ||
7696 | int i, j, cell_rows, cell_cols; | |
7697 | wxRect rect; | |
7698 | ||
7699 | for (j=topRow; j<bottomRow; j++) | |
7700 | { | |
7701 | int colPos; | |
7702 | for (colPos=leftCol; colPos<rightCol; colPos++) | |
7703 | { | |
7704 | i = GetColAt( colPos ); | |
7705 | ||
7706 | GetCellSize( j, i, &cell_rows, &cell_cols ); | |
7707 | if ((cell_rows > 1) || (cell_cols > 1)) | |
7708 | { | |
7709 | rect = CellToRect(j,i); | |
7710 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
7711 | clippedcells.Subtract(rect); | |
7712 | } | |
7713 | else if ((cell_rows < 0) || (cell_cols < 0)) | |
7714 | { | |
7715 | rect = CellToRect(j + cell_rows, i + cell_cols); | |
7716 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
7717 | clippedcells.Subtract(rect); | |
7718 | } | |
7719 | } | |
7720 | } | |
7721 | #else | |
7722 | wxRegion clippedcells( left, top, right - left, bottom - top ); | |
7723 | ||
7724 | int i, j, cell_rows, cell_cols; | |
7725 | wxRect rect; | |
7726 | ||
7727 | for (j=topRow; j<bottomRow; j++) | |
7728 | { | |
7729 | for (i=leftCol; i<rightCol; i++) | |
7730 | { | |
7731 | GetCellSize( j, i, &cell_rows, &cell_cols ); | |
7732 | if ((cell_rows > 1) || (cell_cols > 1)) | |
7733 | { | |
7734 | rect = CellToRect(j, i); | |
7735 | clippedcells.Subtract(rect); | |
7736 | } | |
7737 | else if ((cell_rows < 0) || (cell_cols < 0)) | |
7738 | { | |
7739 | rect = CellToRect(j + cell_rows, i + cell_cols); | |
7740 | clippedcells.Subtract(rect); | |
7741 | } | |
7742 | } | |
7743 | } | |
7744 | #endif | |
7745 | ||
7746 | dc.SetClippingRegion( clippedcells ); | |
7747 | ||
7748 | ||
7749 | // horizontal grid lines | |
7750 | // | |
7751 | // already declared above - int i; | |
7752 | for ( i = internalYToRow(top); i < m_numRows; i++ ) | |
7753 | { | |
7754 | int bot = GetRowBottom(i) - 1; | |
7755 | ||
7756 | if ( bot > bottom ) | |
7757 | { | |
7758 | break; | |
7759 | } | |
7760 | ||
7761 | if ( bot >= top ) | |
7762 | { | |
7763 | dc.SetPen( GetRowGridLinePen(i) ); | |
7764 | dc.DrawLine( left, bot, right, bot ); | |
7765 | } | |
7766 | } | |
7767 | ||
7768 | // vertical grid lines | |
7769 | // | |
7770 | int colPos; | |
7771 | for ( colPos = leftCol; colPos < m_numCols; colPos++ ) | |
7772 | { | |
7773 | i = GetColAt( colPos ); | |
7774 | ||
7775 | int colRight = GetColRight(i); | |
7776 | #ifdef __WXGTK__ | |
7777 | if (GetLayoutDirection() != wxLayout_RightToLeft) | |
7778 | #endif | |
7779 | colRight--; | |
7780 | ||
7781 | if ( colRight > right ) | |
7782 | { | |
7783 | break; | |
7784 | } | |
7785 | ||
7786 | if ( colRight >= left ) | |
7787 | { | |
7788 | dc.SetPen( GetColGridLinePen(i) ); | |
7789 | dc.DrawLine( colRight, top, colRight, bottom ); | |
7790 | } | |
7791 | } | |
7792 | ||
7793 | dc.DestroyClippingRegion(); | |
7794 | } | |
7795 | ||
7796 | void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows) | |
7797 | { | |
7798 | if ( !m_numRows ) | |
7799 | return; | |
7800 | ||
7801 | size_t i; | |
7802 | size_t numLabels = rows.GetCount(); | |
7803 | ||
7804 | for ( i = 0; i < numLabels; i++ ) | |
7805 | { | |
7806 | DrawRowLabel( dc, rows[i] ); | |
7807 | } | |
7808 | } | |
7809 | ||
7810 | void wxGrid::DrawRowLabel( wxDC& dc, int row ) | |
7811 | { | |
7812 | if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 ) | |
7813 | return; | |
7814 | ||
7815 | wxRect rect; | |
7816 | ||
7817 | #if 0 | |
7818 | def __WXGTK20__ | |
7819 | rect.SetX( 1 ); | |
7820 | rect.SetY( GetRowTop(row) + 1 ); | |
7821 | rect.SetWidth( m_rowLabelWidth - 2 ); | |
7822 | rect.SetHeight( GetRowHeight(row) - 2 ); | |
7823 | ||
7824 | CalcScrolledPosition( 0, rect.y, NULL, &rect.y ); | |
7825 | ||
7826 | wxWindowDC *win_dc = (wxWindowDC*) &dc; | |
7827 | ||
7828 | wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 ); | |
7829 | #else | |
7830 | int rowTop = GetRowTop(row), | |
7831 | rowBottom = GetRowBottom(row) - 1; | |
7832 | ||
7833 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) ); | |
7834 | dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom ); | |
7835 | dc.DrawLine( 0, rowTop, 0, rowBottom ); | |
7836 | dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom ); | |
7837 | ||
7838 | dc.SetPen( *wxWHITE_PEN ); | |
7839 | dc.DrawLine( 1, rowTop, 1, rowBottom ); | |
7840 | dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop ); | |
7841 | #endif | |
7842 | ||
7843 | dc.SetBackgroundMode( wxTRANSPARENT ); | |
7844 | dc.SetTextForeground( GetLabelTextColour() ); | |
7845 | dc.SetFont( GetLabelFont() ); | |
7846 | ||
7847 | int hAlign, vAlign; | |
7848 | GetRowLabelAlignment( &hAlign, &vAlign ); | |
7849 | ||
7850 | rect.SetX( 2 ); | |
7851 | rect.SetY( GetRowTop(row) + 2 ); | |
7852 | rect.SetWidth( m_rowLabelWidth - 4 ); | |
7853 | rect.SetHeight( GetRowHeight(row) - 4 ); | |
7854 | DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign ); | |
7855 | } | |
7856 | ||
7857 | void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols ) | |
7858 | { | |
7859 | if ( !m_numCols ) | |
7860 | return; | |
7861 | ||
7862 | size_t i; | |
7863 | size_t numLabels = cols.GetCount(); | |
7864 | ||
7865 | for ( i = 0; i < numLabels; i++ ) | |
7866 | { | |
7867 | DrawColLabel( dc, cols[i] ); | |
7868 | } | |
7869 | } | |
7870 | ||
7871 | void wxGrid::DrawColLabel( wxDC& dc, int col ) | |
7872 | { | |
7873 | if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 ) | |
7874 | return; | |
7875 | ||
7876 | int colLeft = GetColLeft(col); | |
7877 | ||
7878 | wxRect rect; | |
7879 | ||
7880 | #if 0 | |
7881 | def __WXGTK20__ | |
7882 | rect.SetX( colLeft + 1 ); | |
7883 | rect.SetY( 1 ); | |
7884 | rect.SetWidth( GetColWidth(col) - 2 ); | |
7885 | rect.SetHeight( m_colLabelHeight - 2 ); | |
7886 | ||
7887 | wxWindowDC *win_dc = (wxWindowDC*) &dc; | |
7888 | ||
7889 | wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 ); | |
7890 | #else | |
7891 | int colRight = GetColRight(col) - 1; | |
7892 | ||
7893 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) ); | |
7894 | dc.DrawLine( colRight, 0, colRight, m_colLabelHeight - 1 ); | |
7895 | dc.DrawLine( colLeft, 0, colRight, 0 ); | |
7896 | dc.DrawLine( colLeft, m_colLabelHeight - 1, | |
7897 | colRight + 1, m_colLabelHeight - 1 ); | |
7898 | ||
7899 | dc.SetPen( *wxWHITE_PEN ); | |
7900 | dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 ); | |
7901 | dc.DrawLine( colLeft, 1, colRight, 1 ); | |
7902 | #endif | |
7903 | ||
7904 | dc.SetBackgroundMode( wxTRANSPARENT ); | |
7905 | dc.SetTextForeground( GetLabelTextColour() ); | |
7906 | dc.SetFont( GetLabelFont() ); | |
7907 | ||
7908 | int hAlign, vAlign, orient; | |
7909 | GetColLabelAlignment( &hAlign, &vAlign ); | |
7910 | orient = GetColLabelTextOrientation(); | |
7911 | ||
7912 | rect.SetX( colLeft + 2 ); | |
7913 | rect.SetY( 2 ); | |
7914 | rect.SetWidth( GetColWidth(col) - 4 ); | |
7915 | rect.SetHeight( m_colLabelHeight - 4 ); | |
7916 | DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient ); | |
7917 | } | |
7918 | ||
7919 | void wxGrid::DrawTextRectangle( wxDC& dc, | |
7920 | const wxString& value, | |
7921 | const wxRect& rect, | |
7922 | int horizAlign, | |
7923 | int vertAlign, | |
7924 | int textOrientation ) | |
7925 | { | |
7926 | wxArrayString lines; | |
7927 | ||
7928 | StringToLines( value, lines ); | |
7929 | ||
7930 | // Forward to new API. | |
7931 | DrawTextRectangle( dc, | |
7932 | lines, | |
7933 | rect, | |
7934 | horizAlign, | |
7935 | vertAlign, | |
7936 | textOrientation ); | |
7937 | } | |
7938 | ||
7939 | // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to | |
7940 | // add textOrientation support | |
7941 | void wxGrid::DrawTextRectangle(wxDC& dc, | |
7942 | const wxArrayString& lines, | |
7943 | const wxRect& rect, | |
7944 | int horizAlign, | |
7945 | int vertAlign, | |
7946 | int textOrientation) | |
7947 | { | |
7948 | if ( lines.empty() ) | |
7949 | return; | |
7950 | ||
7951 | wxDCClipper clip(dc, rect); | |
7952 | ||
7953 | long textWidth, | |
7954 | textHeight; | |
7955 | ||
7956 | if ( textOrientation == wxHORIZONTAL ) | |
7957 | GetTextBoxSize( dc, lines, &textWidth, &textHeight ); | |
7958 | else | |
7959 | GetTextBoxSize( dc, lines, &textHeight, &textWidth ); | |
7960 | ||
7961 | int x = 0, | |
7962 | y = 0; | |
7963 | switch ( vertAlign ) | |
7964 | { | |
7965 | case wxALIGN_BOTTOM: | |
7966 | if ( textOrientation == wxHORIZONTAL ) | |
7967 | y = rect.y + (rect.height - textHeight - 1); | |
7968 | else | |
7969 | x = rect.x + rect.width - textWidth; | |
7970 | break; | |
7971 | ||
7972 | case wxALIGN_CENTRE: | |
7973 | if ( textOrientation == wxHORIZONTAL ) | |
7974 | y = rect.y + ((rect.height - textHeight) / 2); | |
7975 | else | |
7976 | x = rect.x + ((rect.width - textWidth) / 2); | |
7977 | break; | |
7978 | ||
7979 | case wxALIGN_TOP: | |
7980 | default: | |
7981 | if ( textOrientation == wxHORIZONTAL ) | |
7982 | y = rect.y + 1; | |
7983 | else | |
7984 | x = rect.x + 1; | |
7985 | break; | |
7986 | } | |
7987 | ||
7988 | // Align each line of a multi-line label | |
7989 | size_t nLines = lines.GetCount(); | |
7990 | for ( size_t l = 0; l < nLines; l++ ) | |
7991 | { | |
7992 | const wxString& line = lines[l]; | |
7993 | ||
7994 | if ( line.empty() ) | |
7995 | { | |
7996 | *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight(); | |
7997 | continue; | |
7998 | } | |
7999 | ||
8000 | long lineWidth = 0, | |
8001 | lineHeight = 0; | |
8002 | dc.GetTextExtent(line, &lineWidth, &lineHeight); | |
8003 | ||
8004 | switch ( horizAlign ) | |
8005 | { | |
8006 | case wxALIGN_RIGHT: | |
8007 | if ( textOrientation == wxHORIZONTAL ) | |
8008 | x = rect.x + (rect.width - lineWidth - 1); | |
8009 | else | |
8010 | y = rect.y + lineWidth + 1; | |
8011 | break; | |
8012 | ||
8013 | case wxALIGN_CENTRE: | |
8014 | if ( textOrientation == wxHORIZONTAL ) | |
8015 | x = rect.x + ((rect.width - lineWidth) / 2); | |
8016 | else | |
8017 | y = rect.y + rect.height - ((rect.height - lineWidth) / 2); | |
8018 | break; | |
8019 | ||
8020 | case wxALIGN_LEFT: | |
8021 | default: | |
8022 | if ( textOrientation == wxHORIZONTAL ) | |
8023 | x = rect.x + 1; | |
8024 | else | |
8025 | y = rect.y + rect.height - 1; | |
8026 | break; | |
8027 | } | |
8028 | ||
8029 | if ( textOrientation == wxHORIZONTAL ) | |
8030 | { | |
8031 | dc.DrawText( line, x, y ); | |
8032 | y += lineHeight; | |
8033 | } | |
8034 | else | |
8035 | { | |
8036 | dc.DrawRotatedText( line, x, y, 90.0 ); | |
8037 | x += lineHeight; | |
8038 | } | |
8039 | } | |
8040 | } | |
8041 | ||
8042 | // Split multi-line text up into an array of strings. | |
8043 | // Any existing contents of the string array are preserved. | |
8044 | // | |
8045 | void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) | |
8046 | { | |
8047 | int startPos = 0; | |
8048 | int pos; | |
8049 | wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix ); | |
8050 | wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix ); | |
8051 | ||
8052 | while ( startPos < (int)tVal.length() ) | |
8053 | { | |
8054 | pos = tVal.Mid(startPos).Find( eol ); | |
8055 | if ( pos < 0 ) | |
8056 | { | |
8057 | break; | |
8058 | } | |
8059 | else if ( pos == 0 ) | |
8060 | { | |
8061 | lines.Add( wxEmptyString ); | |
8062 | } | |
8063 | else | |
8064 | { | |
8065 | lines.Add( value.Mid(startPos, pos) ); | |
8066 | } | |
8067 | ||
8068 | startPos += pos + 1; | |
8069 | } | |
8070 | ||
8071 | if ( startPos < (int)value.length() ) | |
8072 | { | |
8073 | lines.Add( value.Mid( startPos ) ); | |
8074 | } | |
8075 | } | |
8076 | ||
8077 | void wxGrid::GetTextBoxSize( const wxDC& dc, | |
8078 | const wxArrayString& lines, | |
8079 | long *width, long *height ) | |
8080 | { | |
8081 | long w = 0; | |
8082 | long h = 0; | |
8083 | long lineW = 0, lineH = 0; | |
8084 | ||
8085 | size_t i; | |
8086 | for ( i = 0; i < lines.GetCount(); i++ ) | |
8087 | { | |
8088 | dc.GetTextExtent( lines[i], &lineW, &lineH ); | |
8089 | w = wxMax( w, lineW ); | |
8090 | h += lineH; | |
8091 | } | |
8092 | ||
8093 | *width = w; | |
8094 | *height = h; | |
8095 | } | |
8096 | ||
8097 | // | |
8098 | // ------ Batch processing. | |
8099 | // | |
8100 | void wxGrid::EndBatch() | |
8101 | { | |
8102 | if ( m_batchCount > 0 ) | |
8103 | { | |
8104 | m_batchCount--; | |
8105 | if ( !m_batchCount ) | |
8106 | { | |
8107 | CalcDimensions(); | |
8108 | m_rowLabelWin->Refresh(); | |
8109 | m_colLabelWin->Refresh(); | |
8110 | m_cornerLabelWin->Refresh(); | |
8111 | m_gridWin->Refresh(); | |
8112 | } | |
8113 | } | |
8114 | } | |
8115 | ||
8116 | // Use this, rather than wxWindow::Refresh(), to force an immediate | |
8117 | // repainting of the grid. Has no effect if you are already inside a | |
8118 | // BeginBatch / EndBatch block. | |
8119 | // | |
8120 | void wxGrid::ForceRefresh() | |
8121 | { | |
8122 | BeginBatch(); | |
8123 | EndBatch(); | |
8124 | } | |
8125 | ||
8126 | bool wxGrid::Enable(bool enable) | |
8127 | { | |
8128 | if ( !wxScrolledWindow::Enable(enable) ) | |
8129 | return false; | |
8130 | ||
8131 | // redraw in the new state | |
8132 | m_gridWin->Refresh(); | |
8133 | ||
8134 | return true; | |
8135 | } | |
8136 | ||
8137 | // | |
8138 | // ------ Edit control functions | |
8139 | // | |
8140 | ||
8141 | void wxGrid::EnableEditing( bool edit ) | |
8142 | { | |
8143 | // TODO: improve this ? | |
8144 | // | |
8145 | if ( edit != m_editable ) | |
8146 | { | |
8147 | if (!edit) | |
8148 | EnableCellEditControl(edit); | |
8149 | m_editable = edit; | |
8150 | } | |
8151 | } | |
8152 | ||
8153 | void wxGrid::EnableCellEditControl( bool enable ) | |
8154 | { | |
8155 | if (! m_editable) | |
8156 | return; | |
8157 | ||
8158 | if ( enable != m_cellEditCtrlEnabled ) | |
8159 | { | |
8160 | if ( enable ) | |
8161 | { | |
8162 | if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0) | |
8163 | return; | |
8164 | ||
8165 | // this should be checked by the caller! | |
8166 | wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") ); | |
8167 | ||
8168 | // do it before ShowCellEditControl() | |
8169 | m_cellEditCtrlEnabled = enable; | |
8170 | ||
8171 | ShowCellEditControl(); | |
8172 | } | |
8173 | else | |
8174 | { | |
8175 | //FIXME:add veto support | |
8176 | SendEvent( wxEVT_GRID_EDITOR_HIDDEN ); | |
8177 | ||
8178 | HideCellEditControl(); | |
8179 | SaveEditControlValue(); | |
8180 | ||
8181 | // do it after HideCellEditControl() | |
8182 | m_cellEditCtrlEnabled = enable; | |
8183 | } | |
8184 | } | |
8185 | } | |
8186 | ||
8187 | bool wxGrid::IsCurrentCellReadOnly() const | |
8188 | { | |
8189 | // const_cast | |
8190 | wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords); | |
8191 | bool readonly = attr->IsReadOnly(); | |
8192 | attr->DecRef(); | |
8193 | ||
8194 | return readonly; | |
8195 | } | |
8196 | ||
8197 | bool wxGrid::CanEnableCellControl() const | |
8198 | { | |
8199 | return m_editable && (m_currentCellCoords != wxGridNoCellCoords) && | |
8200 | !IsCurrentCellReadOnly(); | |
8201 | } | |
8202 | ||
8203 | bool wxGrid::IsCellEditControlEnabled() const | |
8204 | { | |
8205 | // the cell edit control might be disable for all cells or just for the | |
8206 | // current one if it's read only | |
8207 | return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false; | |
8208 | } | |
8209 | ||
8210 | bool wxGrid::IsCellEditControlShown() const | |
8211 | { | |
8212 | bool isShown = false; | |
8213 | ||
8214 | if ( m_cellEditCtrlEnabled ) | |
8215 | { | |
8216 | int row = m_currentCellCoords.GetRow(); | |
8217 | int col = m_currentCellCoords.GetCol(); | |
8218 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
8219 | wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col); | |
8220 | attr->DecRef(); | |
8221 | ||
8222 | if ( editor ) | |
8223 | { | |
8224 | if ( editor->IsCreated() ) | |
8225 | { | |
8226 | isShown = editor->GetControl()->IsShown(); | |
8227 | } | |
8228 | ||
8229 | editor->DecRef(); | |
8230 | } | |
8231 | } | |
8232 | ||
8233 | return isShown; | |
8234 | } | |
8235 | ||
8236 | void wxGrid::ShowCellEditControl() | |
8237 | { | |
8238 | if ( IsCellEditControlEnabled() ) | |
8239 | { | |
8240 | if ( !IsVisible( m_currentCellCoords, false ) ) | |
8241 | { | |
8242 | m_cellEditCtrlEnabled = false; | |
8243 | return; | |
8244 | } | |
8245 | else | |
8246 | { | |
8247 | wxRect rect = CellToRect( m_currentCellCoords ); | |
8248 | int row = m_currentCellCoords.GetRow(); | |
8249 | int col = m_currentCellCoords.GetCol(); | |
8250 | ||
8251 | // if this is part of a multicell, find owner (topleft) | |
8252 | int cell_rows, cell_cols; | |
8253 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
8254 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
8255 | { | |
8256 | row += cell_rows; | |
8257 | col += cell_cols; | |
8258 | m_currentCellCoords.SetRow( row ); | |
8259 | m_currentCellCoords.SetCol( col ); | |
8260 | } | |
8261 | ||
8262 | // erase the highlight and the cell contents because the editor | |
8263 | // might not cover the entire cell | |
8264 | wxClientDC dc( m_gridWin ); | |
8265 | PrepareDC( dc ); | |
8266 | dc.SetBrush(wxBrush(GetCellAttr(row, col)->GetBackgroundColour(), wxSOLID)); | |
8267 | dc.SetPen(*wxTRANSPARENT_PEN); | |
8268 | dc.DrawRectangle(rect); | |
8269 | ||
8270 | // convert to scrolled coords | |
8271 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
8272 | ||
8273 | int nXMove = 0; | |
8274 | if (rect.x < 0) | |
8275 | nXMove = rect.x; | |
8276 | ||
8277 | // cell is shifted by one pixel | |
8278 | // However, don't allow x or y to become negative | |
8279 | // since the SetSize() method interprets that as | |
8280 | // "don't change." | |
8281 | if (rect.x > 0) | |
8282 | rect.x--; | |
8283 | if (rect.y > 0) | |
8284 | rect.y--; | |
8285 | ||
8286 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
8287 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); | |
8288 | if ( !editor->IsCreated() ) | |
8289 | { | |
8290 | editor->Create(m_gridWin, wxID_ANY, | |
8291 | new wxGridCellEditorEvtHandler(this, editor)); | |
8292 | ||
8293 | wxGridEditorCreatedEvent evt(GetId(), | |
8294 | wxEVT_GRID_EDITOR_CREATED, | |
8295 | this, | |
8296 | row, | |
8297 | col, | |
8298 | editor->GetControl()); | |
8299 | GetEventHandler()->ProcessEvent(evt); | |
8300 | } | |
8301 | ||
8302 | // resize editor to overflow into righthand cells if allowed | |
8303 | int maxWidth = rect.width; | |
8304 | wxString value = GetCellValue(row, col); | |
8305 | if ( (value != wxEmptyString) && (attr->GetOverflow()) ) | |
8306 | { | |
8307 | int y; | |
8308 | GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont()); | |
8309 | if (maxWidth < rect.width) | |
8310 | maxWidth = rect.width; | |
8311 | } | |
8312 | ||
8313 | int client_right = m_gridWin->GetClientSize().GetWidth(); | |
8314 | if (rect.x + maxWidth > client_right) | |
8315 | maxWidth = client_right - rect.x; | |
8316 | ||
8317 | if ((maxWidth > rect.width) && (col < m_numCols) && m_table) | |
8318 | { | |
8319 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
8320 | // may have changed earlier | |
8321 | for (int i = col + cell_cols; i < m_numCols; i++) | |
8322 | { | |
8323 | int c_rows, c_cols; | |
8324 | GetCellSize( row, i, &c_rows, &c_cols ); | |
8325 | ||
8326 | // looks weird going over a multicell | |
8327 | if (m_table->IsEmptyCell( row, i ) && | |
8328 | (rect.width < maxWidth) && (c_rows == 1)) | |
8329 | { | |
8330 | rect.width += GetColWidth( i ); | |
8331 | } | |
8332 | else | |
8333 | break; | |
8334 | } | |
8335 | ||
8336 | if (rect.GetRight() > client_right) | |
8337 | rect.SetRight( client_right - 1 ); | |
8338 | } | |
8339 | ||
8340 | editor->SetCellAttr( attr ); | |
8341 | editor->SetSize( rect ); | |
8342 | if (nXMove != 0) | |
8343 | editor->GetControl()->Move( | |
8344 | editor->GetControl()->GetPosition().x + nXMove, | |
8345 | editor->GetControl()->GetPosition().y ); | |
8346 | editor->Show( true, attr ); | |
8347 | ||
8348 | // recalc dimensions in case we need to | |
8349 | // expand the scrolled window to account for editor | |
8350 | CalcDimensions(); | |
8351 | ||
8352 | editor->BeginEdit(row, col, this); | |
8353 | editor->SetCellAttr(NULL); | |
8354 | ||
8355 | editor->DecRef(); | |
8356 | attr->DecRef(); | |
8357 | } | |
8358 | } | |
8359 | } | |
8360 | ||
8361 | void wxGrid::HideCellEditControl() | |
8362 | { | |
8363 | if ( IsCellEditControlEnabled() ) | |
8364 | { | |
8365 | int row = m_currentCellCoords.GetRow(); | |
8366 | int col = m_currentCellCoords.GetCol(); | |
8367 | ||
8368 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
8369 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); | |
8370 | editor->Show( false ); | |
8371 | editor->DecRef(); | |
8372 | attr->DecRef(); | |
8373 | ||
8374 | m_gridWin->SetFocus(); | |
8375 | ||
8376 | // refresh whole row to the right | |
8377 | wxRect rect( CellToRect(row, col) ); | |
8378 | CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y ); | |
8379 | rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x; | |
8380 | ||
8381 | #ifdef __WXMAC__ | |
8382 | // ensure that the pixels under the focus ring get refreshed as well | |
8383 | rect.Inflate(10, 10); | |
8384 | #endif | |
8385 | ||
8386 | m_gridWin->Refresh( false, &rect ); | |
8387 | } | |
8388 | } | |
8389 | ||
8390 | void wxGrid::SaveEditControlValue() | |
8391 | { | |
8392 | if ( IsCellEditControlEnabled() ) | |
8393 | { | |
8394 | int row = m_currentCellCoords.GetRow(); | |
8395 | int col = m_currentCellCoords.GetCol(); | |
8396 | ||
8397 | wxString oldval = GetCellValue(row, col); | |
8398 | ||
8399 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
8400 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); | |
8401 | bool changed = editor->EndEdit(row, col, this); | |
8402 | ||
8403 | editor->DecRef(); | |
8404 | attr->DecRef(); | |
8405 | ||
8406 | if (changed) | |
8407 | { | |
8408 | if ( SendEvent( wxEVT_GRID_CELL_CHANGE, | |
8409 | m_currentCellCoords.GetRow(), | |
8410 | m_currentCellCoords.GetCol() ) < 0 ) | |
8411 | { | |
8412 | // Event has been vetoed, set the data back. | |
8413 | SetCellValue(row, col, oldval); | |
8414 | } | |
8415 | } | |
8416 | } | |
8417 | } | |
8418 | ||
8419 | // | |
8420 | // ------ Grid location functions | |
8421 | // Note that all of these functions work with the logical coordinates of | |
8422 | // grid cells and labels so you will need to convert from device | |
8423 | // coordinates for mouse events etc. | |
8424 | // | |
8425 | ||
8426 | void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords ) | |
8427 | { | |
8428 | int row = YToRow(y); | |
8429 | int col = XToCol(x); | |
8430 | ||
8431 | if ( row == -1 || col == -1 ) | |
8432 | { | |
8433 | coords = wxGridNoCellCoords; | |
8434 | } | |
8435 | else | |
8436 | { | |
8437 | coords.Set( row, col ); | |
8438 | } | |
8439 | } | |
8440 | ||
8441 | // Internal Helper function for computing row or column from some | |
8442 | // (unscrolled) coordinate value, using either | |
8443 | // m_defaultRowHeight/m_defaultColWidth or binary search on array | |
8444 | // of m_rowBottoms/m_ColRights to speed up the search! | |
8445 | ||
8446 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, | |
8447 | const wxArrayInt& BorderArray, int nMax, | |
8448 | bool clipToMinMax) | |
8449 | { | |
8450 | if (coord < 0) | |
8451 | return clipToMinMax && (nMax > 0) ? 0 : -1; | |
8452 | ||
8453 | if (!defaultDist) | |
8454 | defaultDist = 1; | |
8455 | ||
8456 | size_t i_max = coord / defaultDist, | |
8457 | i_min = 0; | |
8458 | ||
8459 | if (BorderArray.IsEmpty()) | |
8460 | { | |
8461 | if ((int) i_max < nMax) | |
8462 | return i_max; | |
8463 | return clipToMinMax ? nMax - 1 : -1; | |
8464 | } | |
8465 | ||
8466 | if ( i_max >= BorderArray.GetCount()) | |
8467 | { | |
8468 | i_max = BorderArray.GetCount() - 1; | |
8469 | } | |
8470 | else | |
8471 | { | |
8472 | if ( coord >= BorderArray[i_max]) | |
8473 | { | |
8474 | i_min = i_max; | |
8475 | if (minDist) | |
8476 | i_max = coord / minDist; | |
8477 | else | |
8478 | i_max = BorderArray.GetCount() - 1; | |
8479 | } | |
8480 | ||
8481 | if ( i_max >= BorderArray.GetCount()) | |
8482 | i_max = BorderArray.GetCount() - 1; | |
8483 | } | |
8484 | ||
8485 | if ( coord >= BorderArray[i_max]) | |
8486 | return clipToMinMax ? (int)i_max : -1; | |
8487 | if ( coord < BorderArray[0] ) | |
8488 | return 0; | |
8489 | ||
8490 | while ( i_max - i_min > 0 ) | |
8491 | { | |
8492 | wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max], | |
8493 | 0, _T("wxGrid: internal error in CoordToRowOrCol")); | |
8494 | if (coord >= BorderArray[ i_max - 1]) | |
8495 | return i_max; | |
8496 | else | |
8497 | i_max--; | |
8498 | int median = i_min + (i_max - i_min + 1) / 2; | |
8499 | if (coord < BorderArray[median]) | |
8500 | i_max = median; | |
8501 | else | |
8502 | i_min = median; | |
8503 | } | |
8504 | ||
8505 | return i_max; | |
8506 | } | |
8507 | ||
8508 | int wxGrid::YToRow( int y ) | |
8509 | { | |
8510 | return CoordToRowOrCol(y, m_defaultRowHeight, | |
8511 | m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false); | |
8512 | } | |
8513 | ||
8514 | int wxGrid::XToCol( int x, bool clipToMinMax ) | |
8515 | { | |
8516 | if (x < 0) | |
8517 | return clipToMinMax && (m_numCols > 0) ? GetColAt( 0 ) : -1; | |
8518 | ||
8519 | if (!m_defaultColWidth) | |
8520 | m_defaultColWidth = 1; | |
8521 | ||
8522 | int maxPos = x / m_defaultColWidth; | |
8523 | int minPos = 0; | |
8524 | ||
8525 | if (m_colRights.IsEmpty()) | |
8526 | { | |
8527 | if(maxPos < m_numCols) | |
8528 | return GetColAt( maxPos ); | |
8529 | return clipToMinMax ? GetColAt( m_numCols - 1 ) : -1; | |
8530 | } | |
8531 | ||
8532 | if ( maxPos >= m_numCols) | |
8533 | maxPos = m_numCols - 1; | |
8534 | else | |
8535 | { | |
8536 | if ( x >= m_colRights[GetColAt( maxPos )]) | |
8537 | { | |
8538 | minPos = maxPos; | |
8539 | if (m_minAcceptableColWidth) | |
8540 | maxPos = x / m_minAcceptableColWidth; | |
8541 | else | |
8542 | maxPos = m_numCols - 1; | |
8543 | } | |
8544 | if ( maxPos >= m_numCols) | |
8545 | maxPos = m_numCols - 1; | |
8546 | } | |
8547 | ||
8548 | //X is beyond the last column | |
8549 | if ( x >= m_colRights[GetColAt( maxPos )]) | |
8550 | return clipToMinMax ? GetColAt( maxPos ) : -1; | |
8551 | ||
8552 | //X is before the first column | |
8553 | if ( x < m_colRights[GetColAt( 0 )] ) | |
8554 | return GetColAt( 0 ); | |
8555 | ||
8556 | //Perform a binary search | |
8557 | while ( maxPos - minPos > 0 ) | |
8558 | { | |
8559 | wxCHECK_MSG(m_colRights[GetColAt( minPos )] <= x && x < m_colRights[GetColAt( maxPos )], | |
8560 | 0, _T("wxGrid: internal error in XToCol")); | |
8561 | ||
8562 | if (x >= m_colRights[GetColAt( maxPos - 1 )]) | |
8563 | return GetColAt( maxPos ); | |
8564 | else | |
8565 | maxPos--; | |
8566 | int median = minPos + (maxPos - minPos + 1) / 2; | |
8567 | if (x < m_colRights[GetColAt( median )]) | |
8568 | maxPos = median; | |
8569 | else | |
8570 | minPos = median; | |
8571 | } | |
8572 | return GetColAt( maxPos ); | |
8573 | } | |
8574 | ||
8575 | // return the row number that that the y coord is near | |
8576 | // the edge of, or -1 if not near an edge. | |
8577 | // coords can only possibly be near an edge if | |
8578 | // (a) the row/column is large enough to still allow for an "inner" area | |
8579 | // that is _not_ nead the edge (i.e., if the height/width is smaller | |
8580 | // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be | |
8581 | // near the edge). | |
8582 | // and | |
8583 | // (b) resizing rows/columns (the thing for which edge detection is | |
8584 | // relevant at all) is enabled. | |
8585 | // | |
8586 | int wxGrid::YToEdgeOfRow( int y ) | |
8587 | { | |
8588 | int i; | |
8589 | i = internalYToRow(y); | |
8590 | ||
8591 | if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE && CanDragRowSize() ) | |
8592 | { | |
8593 | // We know that we are in row i, test whether we are | |
8594 | // close enough to lower or upper border, respectively. | |
8595 | if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE ) | |
8596 | return i; | |
8597 | else if ( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE ) | |
8598 | return i - 1; | |
8599 | } | |
8600 | ||
8601 | return -1; | |
8602 | } | |
8603 | ||
8604 | // return the col number that that the x coord is near the edge of, or | |
8605 | // -1 if not near an edge | |
8606 | // See comment at YToEdgeOfRow for conditions on edge detection. | |
8607 | // | |
8608 | int wxGrid::XToEdgeOfCol( int x ) | |
8609 | { | |
8610 | int i; | |
8611 | i = internalXToCol(x); | |
8612 | ||
8613 | if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE && CanDragColSize() ) | |
8614 | { | |
8615 | // We know that we are in column i; test whether we are | |
8616 | // close enough to right or left border, respectively. | |
8617 | if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE ) | |
8618 | return i; | |
8619 | else if ( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE ) | |
8620 | return i - 1; | |
8621 | } | |
8622 | ||
8623 | return -1; | |
8624 | } | |
8625 | ||
8626 | wxRect wxGrid::CellToRect( int row, int col ) | |
8627 | { | |
8628 | wxRect rect( -1, -1, -1, -1 ); | |
8629 | ||
8630 | if ( row >= 0 && row < m_numRows && | |
8631 | col >= 0 && col < m_numCols ) | |
8632 | { | |
8633 | int i, cell_rows, cell_cols; | |
8634 | rect.width = rect.height = 0; | |
8635 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
8636 | // if negative then find multicell owner | |
8637 | if (cell_rows < 0) | |
8638 | row += cell_rows; | |
8639 | if (cell_cols < 0) | |
8640 | col += cell_cols; | |
8641 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
8642 | ||
8643 | rect.x = GetColLeft(col); | |
8644 | rect.y = GetRowTop(row); | |
8645 | for (i=col; i < col + cell_cols; i++) | |
8646 | rect.width += GetColWidth(i); | |
8647 | for (i=row; i < row + cell_rows; i++) | |
8648 | rect.height += GetRowHeight(i); | |
8649 | } | |
8650 | ||
8651 | // if grid lines are enabled, then the area of the cell is a bit smaller | |
8652 | if (m_gridLinesEnabled) | |
8653 | { | |
8654 | rect.width -= 1; | |
8655 | rect.height -= 1; | |
8656 | } | |
8657 | ||
8658 | return rect; | |
8659 | } | |
8660 | ||
8661 | bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) | |
8662 | { | |
8663 | // get the cell rectangle in logical coords | |
8664 | // | |
8665 | wxRect r( CellToRect( row, col ) ); | |
8666 | ||
8667 | // convert to device coords | |
8668 | // | |
8669 | int left, top, right, bottom; | |
8670 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
8671 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
8672 | ||
8673 | // check against the client area of the grid window | |
8674 | int cw, ch; | |
8675 | m_gridWin->GetClientSize( &cw, &ch ); | |
8676 | ||
8677 | if ( wholeCellVisible ) | |
8678 | { | |
8679 | // is the cell wholly visible ? | |
8680 | return ( left >= 0 && right <= cw && | |
8681 | top >= 0 && bottom <= ch ); | |
8682 | } | |
8683 | else | |
8684 | { | |
8685 | // is the cell partly visible ? | |
8686 | // | |
8687 | return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) && | |
8688 | ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) ); | |
8689 | } | |
8690 | } | |
8691 | ||
8692 | // make the specified cell location visible by doing a minimal amount | |
8693 | // of scrolling | |
8694 | // | |
8695 | void wxGrid::MakeCellVisible( int row, int col ) | |
8696 | { | |
8697 | int i; | |
8698 | int xpos = -1, ypos = -1; | |
8699 | ||
8700 | if ( row >= 0 && row < m_numRows && | |
8701 | col >= 0 && col < m_numCols ) | |
8702 | { | |
8703 | // get the cell rectangle in logical coords | |
8704 | wxRect r( CellToRect( row, col ) ); | |
8705 | ||
8706 | // convert to device coords | |
8707 | int left, top, right, bottom; | |
8708 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
8709 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
8710 | ||
8711 | int cw, ch; | |
8712 | m_gridWin->GetClientSize( &cw, &ch ); | |
8713 | ||
8714 | if ( top < 0 ) | |
8715 | { | |
8716 | ypos = r.GetTop(); | |
8717 | } | |
8718 | else if ( bottom > ch ) | |
8719 | { | |
8720 | int h = r.GetHeight(); | |
8721 | ypos = r.GetTop(); | |
8722 | for ( i = row - 1; i >= 0; i-- ) | |
8723 | { | |
8724 | int rowHeight = GetRowHeight(i); | |
8725 | if ( h + rowHeight > ch ) | |
8726 | break; | |
8727 | ||
8728 | h += rowHeight; | |
8729 | ypos -= rowHeight; | |
8730 | } | |
8731 | ||
8732 | // we divide it later by GRID_SCROLL_LINE, make sure that we don't | |
8733 | // have rounding errors (this is important, because if we do, | |
8734 | // we might not scroll at all and some cells won't be redrawn) | |
8735 | // | |
8736 | // Sometimes GRID_SCROLL_LINE / 2 is not enough, | |
8737 | // so just add a full scroll unit... | |
8738 | ypos += m_scrollLineY; | |
8739 | } | |
8740 | ||
8741 | // special handling for wide cells - show always left part of the cell! | |
8742 | // Otherwise, e.g. when stepping from row to row, it would jump between | |
8743 | // left and right part of the cell on every step! | |
8744 | // if ( left < 0 ) | |
8745 | if ( left < 0 || (right - left) >= cw ) | |
8746 | { | |
8747 | xpos = r.GetLeft(); | |
8748 | } | |
8749 | else if ( right > cw ) | |
8750 | { | |
8751 | // position the view so that the cell is on the right | |
8752 | int x0, y0; | |
8753 | CalcUnscrolledPosition(0, 0, &x0, &y0); | |
8754 | xpos = x0 + (right - cw); | |
8755 | ||
8756 | // see comment for ypos above | |
8757 | xpos += m_scrollLineX; | |
8758 | } | |
8759 | ||
8760 | if ( xpos != -1 || ypos != -1 ) | |
8761 | { | |
8762 | if ( xpos != -1 ) | |
8763 | xpos /= m_scrollLineX; | |
8764 | if ( ypos != -1 ) | |
8765 | ypos /= m_scrollLineY; | |
8766 | Scroll( xpos, ypos ); | |
8767 | AdjustScrollbars(); | |
8768 | } | |
8769 | } | |
8770 | } | |
8771 | ||
8772 | // | |
8773 | // ------ Grid cursor movement functions | |
8774 | // | |
8775 | ||
8776 | bool wxGrid::MoveCursorUp( bool expandSelection ) | |
8777 | { | |
8778 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
8779 | m_currentCellCoords.GetRow() >= 0 ) | |
8780 | { | |
8781 | if ( expandSelection ) | |
8782 | { | |
8783 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
8784 | m_selectingKeyboard = m_currentCellCoords; | |
8785 | if ( m_selectingKeyboard.GetRow() > 0 ) | |
8786 | { | |
8787 | m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 ); | |
8788 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
8789 | m_selectingKeyboard.GetCol() ); | |
8790 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
8791 | } | |
8792 | } | |
8793 | else if ( m_currentCellCoords.GetRow() > 0 ) | |
8794 | { | |
8795 | int row = m_currentCellCoords.GetRow() - 1; | |
8796 | int col = m_currentCellCoords.GetCol(); | |
8797 | ClearSelection(); | |
8798 | MakeCellVisible( row, col ); | |
8799 | SetCurrentCell( row, col ); | |
8800 | } | |
8801 | else | |
8802 | return false; | |
8803 | ||
8804 | return true; | |
8805 | } | |
8806 | ||
8807 | return false; | |
8808 | } | |
8809 | ||
8810 | bool wxGrid::MoveCursorDown( bool expandSelection ) | |
8811 | { | |
8812 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
8813 | m_currentCellCoords.GetRow() < m_numRows ) | |
8814 | { | |
8815 | if ( expandSelection ) | |
8816 | { | |
8817 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
8818 | m_selectingKeyboard = m_currentCellCoords; | |
8819 | if ( m_selectingKeyboard.GetRow() < m_numRows - 1 ) | |
8820 | { | |
8821 | m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 ); | |
8822 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
8823 | m_selectingKeyboard.GetCol() ); | |
8824 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
8825 | } | |
8826 | } | |
8827 | else if ( m_currentCellCoords.GetRow() < m_numRows - 1 ) | |
8828 | { | |
8829 | int row = m_currentCellCoords.GetRow() + 1; | |
8830 | int col = m_currentCellCoords.GetCol(); | |
8831 | ClearSelection(); | |
8832 | MakeCellVisible( row, col ); | |
8833 | SetCurrentCell( row, col ); | |
8834 | } | |
8835 | else | |
8836 | return false; | |
8837 | ||
8838 | return true; | |
8839 | } | |
8840 | ||
8841 | return false; | |
8842 | } | |
8843 | ||
8844 | bool wxGrid::MoveCursorLeft( bool expandSelection ) | |
8845 | { | |
8846 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
8847 | m_currentCellCoords.GetCol() >= 0 ) | |
8848 | { | |
8849 | if ( expandSelection ) | |
8850 | { | |
8851 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
8852 | m_selectingKeyboard = m_currentCellCoords; | |
8853 | if ( m_selectingKeyboard.GetCol() > 0 ) | |
8854 | { | |
8855 | m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 ); | |
8856 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
8857 | m_selectingKeyboard.GetCol() ); | |
8858 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
8859 | } | |
8860 | } | |
8861 | else if ( GetColPos( m_currentCellCoords.GetCol() ) > 0 ) | |
8862 | { | |
8863 | int row = m_currentCellCoords.GetRow(); | |
8864 | int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) - 1 ); | |
8865 | ClearSelection(); | |
8866 | ||
8867 | MakeCellVisible( row, col ); | |
8868 | SetCurrentCell( row, col ); | |
8869 | } | |
8870 | else | |
8871 | return false; | |
8872 | ||
8873 | return true; | |
8874 | } | |
8875 | ||
8876 | return false; | |
8877 | } | |
8878 | ||
8879 | bool wxGrid::MoveCursorRight( bool expandSelection ) | |
8880 | { | |
8881 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
8882 | m_currentCellCoords.GetCol() < m_numCols ) | |
8883 | { | |
8884 | if ( expandSelection ) | |
8885 | { | |
8886 | if ( m_selectingKeyboard == wxGridNoCellCoords ) | |
8887 | m_selectingKeyboard = m_currentCellCoords; | |
8888 | if ( m_selectingKeyboard.GetCol() < m_numCols - 1 ) | |
8889 | { | |
8890 | m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 ); | |
8891 | MakeCellVisible( m_selectingKeyboard.GetRow(), | |
8892 | m_selectingKeyboard.GetCol() ); | |
8893 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
8894 | } | |
8895 | } | |
8896 | else if ( GetColPos( m_currentCellCoords.GetCol() ) < m_numCols - 1 ) | |
8897 | { | |
8898 | int row = m_currentCellCoords.GetRow(); | |
8899 | int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) + 1 ); | |
8900 | ClearSelection(); | |
8901 | ||
8902 | MakeCellVisible( row, col ); | |
8903 | SetCurrentCell( row, col ); | |
8904 | } | |
8905 | else | |
8906 | return false; | |
8907 | ||
8908 | return true; | |
8909 | } | |
8910 | ||
8911 | return false; | |
8912 | } | |
8913 | ||
8914 | bool wxGrid::MovePageUp() | |
8915 | { | |
8916 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
8917 | return false; | |
8918 | ||
8919 | int row = m_currentCellCoords.GetRow(); | |
8920 | if ( row > 0 ) | |
8921 | { | |
8922 | int cw, ch; | |
8923 | m_gridWin->GetClientSize( &cw, &ch ); | |
8924 | ||
8925 | int y = GetRowTop(row); | |
8926 | int newRow = internalYToRow( y - ch + 1 ); | |
8927 | ||
8928 | if ( newRow == row ) | |
8929 | { | |
8930 | // row > 0, so newRow can never be less than 0 here. | |
8931 | newRow = row - 1; | |
8932 | } | |
8933 | ||
8934 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); | |
8935 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
8936 | ||
8937 | return true; | |
8938 | } | |
8939 | ||
8940 | return false; | |
8941 | } | |
8942 | ||
8943 | bool wxGrid::MovePageDown() | |
8944 | { | |
8945 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
8946 | return false; | |
8947 | ||
8948 | int row = m_currentCellCoords.GetRow(); | |
8949 | if ( (row + 1) < m_numRows ) | |
8950 | { | |
8951 | int cw, ch; | |
8952 | m_gridWin->GetClientSize( &cw, &ch ); | |
8953 | ||
8954 | int y = GetRowTop(row); | |
8955 | int newRow = internalYToRow( y + ch ); | |
8956 | if ( newRow == row ) | |
8957 | { | |
8958 | // row < m_numRows, so newRow can't overflow here. | |
8959 | newRow = row + 1; | |
8960 | } | |
8961 | ||
8962 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); | |
8963 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
8964 | ||
8965 | return true; | |
8966 | } | |
8967 | ||
8968 | return false; | |
8969 | } | |
8970 | ||
8971 | bool wxGrid::MoveCursorUpBlock( bool expandSelection ) | |
8972 | { | |
8973 | if ( m_table && | |
8974 | m_currentCellCoords != wxGridNoCellCoords && | |
8975 | m_currentCellCoords.GetRow() > 0 ) | |
8976 | { | |
8977 | int row = m_currentCellCoords.GetRow(); | |
8978 | int col = m_currentCellCoords.GetCol(); | |
8979 | ||
8980 | if ( m_table->IsEmptyCell(row, col) ) | |
8981 | { | |
8982 | // starting in an empty cell: find the next block of | |
8983 | // non-empty cells | |
8984 | // | |
8985 | while ( row > 0 ) | |
8986 | { | |
8987 | row--; | |
8988 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
8989 | break; | |
8990 | } | |
8991 | } | |
8992 | else if ( m_table->IsEmptyCell(row - 1, col) ) | |
8993 | { | |
8994 | // starting at the top of a block: find the next block | |
8995 | // | |
8996 | row--; | |
8997 | while ( row > 0 ) | |
8998 | { | |
8999 | row--; | |
9000 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9001 | break; | |
9002 | } | |
9003 | } | |
9004 | else | |
9005 | { | |
9006 | // starting within a block: find the top of the block | |
9007 | // | |
9008 | while ( row > 0 ) | |
9009 | { | |
9010 | row--; | |
9011 | if ( m_table->IsEmptyCell(row, col) ) | |
9012 | { | |
9013 | row++; | |
9014 | break; | |
9015 | } | |
9016 | } | |
9017 | } | |
9018 | ||
9019 | MakeCellVisible( row, col ); | |
9020 | if ( expandSelection ) | |
9021 | { | |
9022 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
9023 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
9024 | } | |
9025 | else | |
9026 | { | |
9027 | ClearSelection(); | |
9028 | SetCurrentCell( row, col ); | |
9029 | } | |
9030 | ||
9031 | return true; | |
9032 | } | |
9033 | ||
9034 | return false; | |
9035 | } | |
9036 | ||
9037 | bool wxGrid::MoveCursorDownBlock( bool expandSelection ) | |
9038 | { | |
9039 | if ( m_table && | |
9040 | m_currentCellCoords != wxGridNoCellCoords && | |
9041 | m_currentCellCoords.GetRow() < m_numRows - 1 ) | |
9042 | { | |
9043 | int row = m_currentCellCoords.GetRow(); | |
9044 | int col = m_currentCellCoords.GetCol(); | |
9045 | ||
9046 | if ( m_table->IsEmptyCell(row, col) ) | |
9047 | { | |
9048 | // starting in an empty cell: find the next block of | |
9049 | // non-empty cells | |
9050 | // | |
9051 | while ( row < m_numRows - 1 ) | |
9052 | { | |
9053 | row++; | |
9054 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9055 | break; | |
9056 | } | |
9057 | } | |
9058 | else if ( m_table->IsEmptyCell(row + 1, col) ) | |
9059 | { | |
9060 | // starting at the bottom of a block: find the next block | |
9061 | // | |
9062 | row++; | |
9063 | while ( row < m_numRows - 1 ) | |
9064 | { | |
9065 | row++; | |
9066 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9067 | break; | |
9068 | } | |
9069 | } | |
9070 | else | |
9071 | { | |
9072 | // starting within a block: find the bottom of the block | |
9073 | // | |
9074 | while ( row < m_numRows - 1 ) | |
9075 | { | |
9076 | row++; | |
9077 | if ( m_table->IsEmptyCell(row, col) ) | |
9078 | { | |
9079 | row--; | |
9080 | break; | |
9081 | } | |
9082 | } | |
9083 | } | |
9084 | ||
9085 | MakeCellVisible( row, col ); | |
9086 | if ( expandSelection ) | |
9087 | { | |
9088 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
9089 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
9090 | } | |
9091 | else | |
9092 | { | |
9093 | ClearSelection(); | |
9094 | SetCurrentCell( row, col ); | |
9095 | } | |
9096 | ||
9097 | return true; | |
9098 | } | |
9099 | ||
9100 | return false; | |
9101 | } | |
9102 | ||
9103 | bool wxGrid::MoveCursorLeftBlock( bool expandSelection ) | |
9104 | { | |
9105 | if ( m_table && | |
9106 | m_currentCellCoords != wxGridNoCellCoords && | |
9107 | m_currentCellCoords.GetCol() > 0 ) | |
9108 | { | |
9109 | int row = m_currentCellCoords.GetRow(); | |
9110 | int col = m_currentCellCoords.GetCol(); | |
9111 | ||
9112 | if ( m_table->IsEmptyCell(row, col) ) | |
9113 | { | |
9114 | // starting in an empty cell: find the next block of | |
9115 | // non-empty cells | |
9116 | // | |
9117 | while ( col > 0 ) | |
9118 | { | |
9119 | col--; | |
9120 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9121 | break; | |
9122 | } | |
9123 | } | |
9124 | else if ( m_table->IsEmptyCell(row, col - 1) ) | |
9125 | { | |
9126 | // starting at the left of a block: find the next block | |
9127 | // | |
9128 | col--; | |
9129 | while ( col > 0 ) | |
9130 | { | |
9131 | col--; | |
9132 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9133 | break; | |
9134 | } | |
9135 | } | |
9136 | else | |
9137 | { | |
9138 | // starting within a block: find the left of the block | |
9139 | // | |
9140 | while ( col > 0 ) | |
9141 | { | |
9142 | col--; | |
9143 | if ( m_table->IsEmptyCell(row, col) ) | |
9144 | { | |
9145 | col++; | |
9146 | break; | |
9147 | } | |
9148 | } | |
9149 | } | |
9150 | ||
9151 | MakeCellVisible( row, col ); | |
9152 | if ( expandSelection ) | |
9153 | { | |
9154 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
9155 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
9156 | } | |
9157 | else | |
9158 | { | |
9159 | ClearSelection(); | |
9160 | SetCurrentCell( row, col ); | |
9161 | } | |
9162 | ||
9163 | return true; | |
9164 | } | |
9165 | ||
9166 | return false; | |
9167 | } | |
9168 | ||
9169 | bool wxGrid::MoveCursorRightBlock( bool expandSelection ) | |
9170 | { | |
9171 | if ( m_table && | |
9172 | m_currentCellCoords != wxGridNoCellCoords && | |
9173 | m_currentCellCoords.GetCol() < m_numCols - 1 ) | |
9174 | { | |
9175 | int row = m_currentCellCoords.GetRow(); | |
9176 | int col = m_currentCellCoords.GetCol(); | |
9177 | ||
9178 | if ( m_table->IsEmptyCell(row, col) ) | |
9179 | { | |
9180 | // starting in an empty cell: find the next block of | |
9181 | // non-empty cells | |
9182 | // | |
9183 | while ( col < m_numCols - 1 ) | |
9184 | { | |
9185 | col++; | |
9186 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9187 | break; | |
9188 | } | |
9189 | } | |
9190 | else if ( m_table->IsEmptyCell(row, col + 1) ) | |
9191 | { | |
9192 | // starting at the right of a block: find the next block | |
9193 | // | |
9194 | col++; | |
9195 | while ( col < m_numCols - 1 ) | |
9196 | { | |
9197 | col++; | |
9198 | if ( !(m_table->IsEmptyCell(row, col)) ) | |
9199 | break; | |
9200 | } | |
9201 | } | |
9202 | else | |
9203 | { | |
9204 | // starting within a block: find the right of the block | |
9205 | // | |
9206 | while ( col < m_numCols - 1 ) | |
9207 | { | |
9208 | col++; | |
9209 | if ( m_table->IsEmptyCell(row, col) ) | |
9210 | { | |
9211 | col--; | |
9212 | break; | |
9213 | } | |
9214 | } | |
9215 | } | |
9216 | ||
9217 | MakeCellVisible( row, col ); | |
9218 | if ( expandSelection ) | |
9219 | { | |
9220 | m_selectingKeyboard = wxGridCellCoords( row, col ); | |
9221 | HighlightBlock( m_currentCellCoords, m_selectingKeyboard ); | |
9222 | } | |
9223 | else | |
9224 | { | |
9225 | ClearSelection(); | |
9226 | SetCurrentCell( row, col ); | |
9227 | } | |
9228 | ||
9229 | return true; | |
9230 | } | |
9231 | ||
9232 | return false; | |
9233 | } | |
9234 | ||
9235 | // | |
9236 | // ------ Label values and formatting | |
9237 | // | |
9238 | ||
9239 | void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) | |
9240 | { | |
9241 | if ( horiz ) | |
9242 | *horiz = m_rowLabelHorizAlign; | |
9243 | if ( vert ) | |
9244 | *vert = m_rowLabelVertAlign; | |
9245 | } | |
9246 | ||
9247 | void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) | |
9248 | { | |
9249 | if ( horiz ) | |
9250 | *horiz = m_colLabelHorizAlign; | |
9251 | if ( vert ) | |
9252 | *vert = m_colLabelVertAlign; | |
9253 | } | |
9254 | ||
9255 | int wxGrid::GetColLabelTextOrientation() | |
9256 | { | |
9257 | return m_colLabelTextOrientation; | |
9258 | } | |
9259 | ||
9260 | wxString wxGrid::GetRowLabelValue( int row ) | |
9261 | { | |
9262 | if ( m_table ) | |
9263 | { | |
9264 | return m_table->GetRowLabelValue( row ); | |
9265 | } | |
9266 | else | |
9267 | { | |
9268 | wxString s; | |
9269 | s << row; | |
9270 | return s; | |
9271 | } | |
9272 | } | |
9273 | ||
9274 | wxString wxGrid::GetColLabelValue( int col ) | |
9275 | { | |
9276 | if ( m_table ) | |
9277 | { | |
9278 | return m_table->GetColLabelValue( col ); | |
9279 | } | |
9280 | else | |
9281 | { | |
9282 | wxString s; | |
9283 | s << col; | |
9284 | return s; | |
9285 | } | |
9286 | } | |
9287 | ||
9288 | void wxGrid::SetRowLabelSize( int width ) | |
9289 | { | |
9290 | width = wxMax( width, 0 ); | |
9291 | if ( width != m_rowLabelWidth ) | |
9292 | { | |
9293 | if ( width == 0 ) | |
9294 | { | |
9295 | m_rowLabelWin->Show( false ); | |
9296 | m_cornerLabelWin->Show( false ); | |
9297 | } | |
9298 | else if ( m_rowLabelWidth == 0 ) | |
9299 | { | |
9300 | m_rowLabelWin->Show( true ); | |
9301 | if ( m_colLabelHeight > 0 ) | |
9302 | m_cornerLabelWin->Show( true ); | |
9303 | } | |
9304 | ||
9305 | m_rowLabelWidth = width; | |
9306 | CalcWindowSizes(); | |
9307 | wxScrolledWindow::Refresh( true ); | |
9308 | } | |
9309 | } | |
9310 | ||
9311 | void wxGrid::SetColLabelSize( int height ) | |
9312 | { | |
9313 | height = wxMax( height, 0 ); | |
9314 | if ( height != m_colLabelHeight ) | |
9315 | { | |
9316 | if ( height == 0 ) | |
9317 | { | |
9318 | m_colLabelWin->Show( false ); | |
9319 | m_cornerLabelWin->Show( false ); | |
9320 | } | |
9321 | else if ( m_colLabelHeight == 0 ) | |
9322 | { | |
9323 | m_colLabelWin->Show( true ); | |
9324 | if ( m_rowLabelWidth > 0 ) | |
9325 | m_cornerLabelWin->Show( true ); | |
9326 | } | |
9327 | ||
9328 | m_colLabelHeight = height; | |
9329 | CalcWindowSizes(); | |
9330 | wxScrolledWindow::Refresh( true ); | |
9331 | } | |
9332 | } | |
9333 | ||
9334 | void wxGrid::SetLabelBackgroundColour( const wxColour& colour ) | |
9335 | { | |
9336 | if ( m_labelBackgroundColour != colour ) | |
9337 | { | |
9338 | m_labelBackgroundColour = colour; | |
9339 | m_rowLabelWin->SetBackgroundColour( colour ); | |
9340 | m_colLabelWin->SetBackgroundColour( colour ); | |
9341 | m_cornerLabelWin->SetBackgroundColour( colour ); | |
9342 | ||
9343 | if ( !GetBatchCount() ) | |
9344 | { | |
9345 | m_rowLabelWin->Refresh(); | |
9346 | m_colLabelWin->Refresh(); | |
9347 | m_cornerLabelWin->Refresh(); | |
9348 | } | |
9349 | } | |
9350 | } | |
9351 | ||
9352 | void wxGrid::SetLabelTextColour( const wxColour& colour ) | |
9353 | { | |
9354 | if ( m_labelTextColour != colour ) | |
9355 | { | |
9356 | m_labelTextColour = colour; | |
9357 | if ( !GetBatchCount() ) | |
9358 | { | |
9359 | m_rowLabelWin->Refresh(); | |
9360 | m_colLabelWin->Refresh(); | |
9361 | } | |
9362 | } | |
9363 | } | |
9364 | ||
9365 | void wxGrid::SetLabelFont( const wxFont& font ) | |
9366 | { | |
9367 | m_labelFont = font; | |
9368 | if ( !GetBatchCount() ) | |
9369 | { | |
9370 | m_rowLabelWin->Refresh(); | |
9371 | m_colLabelWin->Refresh(); | |
9372 | } | |
9373 | } | |
9374 | ||
9375 | void wxGrid::SetRowLabelAlignment( int horiz, int vert ) | |
9376 | { | |
9377 | // allow old (incorrect) defs to be used | |
9378 | switch ( horiz ) | |
9379 | { | |
9380 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
9381 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
9382 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
9383 | } | |
9384 | ||
9385 | switch ( vert ) | |
9386 | { | |
9387 | case wxTOP: vert = wxALIGN_TOP; break; | |
9388 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
9389 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
9390 | } | |
9391 | ||
9392 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) | |
9393 | { | |
9394 | m_rowLabelHorizAlign = horiz; | |
9395 | } | |
9396 | ||
9397 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) | |
9398 | { | |
9399 | m_rowLabelVertAlign = vert; | |
9400 | } | |
9401 | ||
9402 | if ( !GetBatchCount() ) | |
9403 | { | |
9404 | m_rowLabelWin->Refresh(); | |
9405 | } | |
9406 | } | |
9407 | ||
9408 | void wxGrid::SetColLabelAlignment( int horiz, int vert ) | |
9409 | { | |
9410 | // allow old (incorrect) defs to be used | |
9411 | switch ( horiz ) | |
9412 | { | |
9413 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
9414 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
9415 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
9416 | } | |
9417 | ||
9418 | switch ( vert ) | |
9419 | { | |
9420 | case wxTOP: vert = wxALIGN_TOP; break; | |
9421 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
9422 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
9423 | } | |
9424 | ||
9425 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) | |
9426 | { | |
9427 | m_colLabelHorizAlign = horiz; | |
9428 | } | |
9429 | ||
9430 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) | |
9431 | { | |
9432 | m_colLabelVertAlign = vert; | |
9433 | } | |
9434 | ||
9435 | if ( !GetBatchCount() ) | |
9436 | { | |
9437 | m_colLabelWin->Refresh(); | |
9438 | } | |
9439 | } | |
9440 | ||
9441 | // Note: under MSW, the default column label font must be changed because it | |
9442 | // does not support vertical printing | |
9443 | // | |
9444 | // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD); | |
9445 | // pGrid->SetLabelFont(font); | |
9446 | // pGrid->SetColLabelTextOrientation(wxVERTICAL); | |
9447 | // | |
9448 | void wxGrid::SetColLabelTextOrientation( int textOrientation ) | |
9449 | { | |
9450 | if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL ) | |
9451 | m_colLabelTextOrientation = textOrientation; | |
9452 | ||
9453 | if ( !GetBatchCount() ) | |
9454 | m_colLabelWin->Refresh(); | |
9455 | } | |
9456 | ||
9457 | void wxGrid::SetRowLabelValue( int row, const wxString& s ) | |
9458 | { | |
9459 | if ( m_table ) | |
9460 | { | |
9461 | m_table->SetRowLabelValue( row, s ); | |
9462 | if ( !GetBatchCount() ) | |
9463 | { | |
9464 | wxRect rect = CellToRect( row, 0 ); | |
9465 | if ( rect.height > 0 ) | |
9466 | { | |
9467 | CalcScrolledPosition(0, rect.y, &rect.x, &rect.y); | |
9468 | rect.x = 0; | |
9469 | rect.width = m_rowLabelWidth; | |
9470 | m_rowLabelWin->Refresh( true, &rect ); | |
9471 | } | |
9472 | } | |
9473 | } | |
9474 | } | |
9475 | ||
9476 | void wxGrid::SetColLabelValue( int col, const wxString& s ) | |
9477 | { | |
9478 | if ( m_table ) | |
9479 | { | |
9480 | m_table->SetColLabelValue( col, s ); | |
9481 | if ( !GetBatchCount() ) | |
9482 | { | |
9483 | wxRect rect = CellToRect( 0, col ); | |
9484 | if ( rect.width > 0 ) | |
9485 | { | |
9486 | CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y); | |
9487 | rect.y = 0; | |
9488 | rect.height = m_colLabelHeight; | |
9489 | m_colLabelWin->Refresh( true, &rect ); | |
9490 | } | |
9491 | } | |
9492 | } | |
9493 | } | |
9494 | ||
9495 | void wxGrid::SetGridLineColour( const wxColour& colour ) | |
9496 | { | |
9497 | if ( m_gridLineColour != colour ) | |
9498 | { | |
9499 | m_gridLineColour = colour; | |
9500 | ||
9501 | wxClientDC dc( m_gridWin ); | |
9502 | PrepareDC( dc ); | |
9503 | DrawAllGridLines( dc, wxRegion() ); | |
9504 | } | |
9505 | } | |
9506 | ||
9507 | void wxGrid::SetCellHighlightColour( const wxColour& colour ) | |
9508 | { | |
9509 | if ( m_cellHighlightColour != colour ) | |
9510 | { | |
9511 | m_cellHighlightColour = colour; | |
9512 | ||
9513 | wxClientDC dc( m_gridWin ); | |
9514 | PrepareDC( dc ); | |
9515 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
9516 | DrawCellHighlight(dc, attr); | |
9517 | attr->DecRef(); | |
9518 | } | |
9519 | } | |
9520 | ||
9521 | void wxGrid::SetCellHighlightPenWidth(int width) | |
9522 | { | |
9523 | if (m_cellHighlightPenWidth != width) | |
9524 | { | |
9525 | m_cellHighlightPenWidth = width; | |
9526 | ||
9527 | // Just redrawing the cell highlight is not enough since that won't | |
9528 | // make any visible change if the the thickness is getting smaller. | |
9529 | int row = m_currentCellCoords.GetRow(); | |
9530 | int col = m_currentCellCoords.GetCol(); | |
9531 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
9532 | return; | |
9533 | ||
9534 | wxRect rect = CellToRect(row, col); | |
9535 | m_gridWin->Refresh(true, &rect); | |
9536 | } | |
9537 | } | |
9538 | ||
9539 | void wxGrid::SetCellHighlightROPenWidth(int width) | |
9540 | { | |
9541 | if (m_cellHighlightROPenWidth != width) | |
9542 | { | |
9543 | m_cellHighlightROPenWidth = width; | |
9544 | ||
9545 | // Just redrawing the cell highlight is not enough since that won't | |
9546 | // make any visible change if the the thickness is getting smaller. | |
9547 | int row = m_currentCellCoords.GetRow(); | |
9548 | int col = m_currentCellCoords.GetCol(); | |
9549 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
9550 | return; | |
9551 | ||
9552 | wxRect rect = CellToRect(row, col); | |
9553 | m_gridWin->Refresh(true, &rect); | |
9554 | } | |
9555 | } | |
9556 | ||
9557 | void wxGrid::EnableGridLines( bool enable ) | |
9558 | { | |
9559 | if ( enable != m_gridLinesEnabled ) | |
9560 | { | |
9561 | m_gridLinesEnabled = enable; | |
9562 | ||
9563 | if ( !GetBatchCount() ) | |
9564 | { | |
9565 | if ( enable ) | |
9566 | { | |
9567 | wxClientDC dc( m_gridWin ); | |
9568 | PrepareDC( dc ); | |
9569 | DrawAllGridLines( dc, wxRegion() ); | |
9570 | } | |
9571 | else | |
9572 | { | |
9573 | m_gridWin->Refresh(); | |
9574 | } | |
9575 | } | |
9576 | } | |
9577 | } | |
9578 | ||
9579 | int wxGrid::GetDefaultRowSize() | |
9580 | { | |
9581 | return m_defaultRowHeight; | |
9582 | } | |
9583 | ||
9584 | int wxGrid::GetRowSize( int row ) | |
9585 | { | |
9586 | wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") ); | |
9587 | ||
9588 | return GetRowHeight(row); | |
9589 | } | |
9590 | ||
9591 | int wxGrid::GetDefaultColSize() | |
9592 | { | |
9593 | return m_defaultColWidth; | |
9594 | } | |
9595 | ||
9596 | int wxGrid::GetColSize( int col ) | |
9597 | { | |
9598 | wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") ); | |
9599 | ||
9600 | return GetColWidth(col); | |
9601 | } | |
9602 | ||
9603 | // ============================================================================ | |
9604 | // access to the grid attributes: each of them has a default value in the grid | |
9605 | // itself and may be overidden on a per-cell basis | |
9606 | // ============================================================================ | |
9607 | ||
9608 | // ---------------------------------------------------------------------------- | |
9609 | // setting default attributes | |
9610 | // ---------------------------------------------------------------------------- | |
9611 | ||
9612 | void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col ) | |
9613 | { | |
9614 | m_defaultCellAttr->SetBackgroundColour(col); | |
9615 | #ifdef __WXGTK__ | |
9616 | m_gridWin->SetBackgroundColour(col); | |
9617 | #endif | |
9618 | } | |
9619 | ||
9620 | void wxGrid::SetDefaultCellTextColour( const wxColour& col ) | |
9621 | { | |
9622 | m_defaultCellAttr->SetTextColour(col); | |
9623 | } | |
9624 | ||
9625 | void wxGrid::SetDefaultCellAlignment( int horiz, int vert ) | |
9626 | { | |
9627 | m_defaultCellAttr->SetAlignment(horiz, vert); | |
9628 | } | |
9629 | ||
9630 | void wxGrid::SetDefaultCellOverflow( bool allow ) | |
9631 | { | |
9632 | m_defaultCellAttr->SetOverflow(allow); | |
9633 | } | |
9634 | ||
9635 | void wxGrid::SetDefaultCellFont( const wxFont& font ) | |
9636 | { | |
9637 | m_defaultCellAttr->SetFont(font); | |
9638 | } | |
9639 | ||
9640 | // For editors and renderers the type registry takes precedence over the | |
9641 | // default attr, so we need to register the new editor/renderer for the string | |
9642 | // data type in order to make setting a default editor/renderer appear to | |
9643 | // work correctly. | |
9644 | ||
9645 | void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer) | |
9646 | { | |
9647 | RegisterDataType(wxGRID_VALUE_STRING, | |
9648 | renderer, | |
9649 | GetDefaultEditorForType(wxGRID_VALUE_STRING)); | |
9650 | } | |
9651 | ||
9652 | void wxGrid::SetDefaultEditor(wxGridCellEditor *editor) | |
9653 | { | |
9654 | RegisterDataType(wxGRID_VALUE_STRING, | |
9655 | GetDefaultRendererForType(wxGRID_VALUE_STRING), | |
9656 | editor); | |
9657 | } | |
9658 | ||
9659 | // ---------------------------------------------------------------------------- | |
9660 | // access to the default attrbiutes | |
9661 | // ---------------------------------------------------------------------------- | |
9662 | ||
9663 | wxColour wxGrid::GetDefaultCellBackgroundColour() | |
9664 | { | |
9665 | return m_defaultCellAttr->GetBackgroundColour(); | |
9666 | } | |
9667 | ||
9668 | wxColour wxGrid::GetDefaultCellTextColour() | |
9669 | { | |
9670 | return m_defaultCellAttr->GetTextColour(); | |
9671 | } | |
9672 | ||
9673 | wxFont wxGrid::GetDefaultCellFont() | |
9674 | { | |
9675 | return m_defaultCellAttr->GetFont(); | |
9676 | } | |
9677 | ||
9678 | void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) | |
9679 | { | |
9680 | m_defaultCellAttr->GetAlignment(horiz, vert); | |
9681 | } | |
9682 | ||
9683 | bool wxGrid::GetDefaultCellOverflow() | |
9684 | { | |
9685 | return m_defaultCellAttr->GetOverflow(); | |
9686 | } | |
9687 | ||
9688 | wxGridCellRenderer *wxGrid::GetDefaultRenderer() const | |
9689 | { | |
9690 | return m_defaultCellAttr->GetRenderer(NULL, 0, 0); | |
9691 | } | |
9692 | ||
9693 | wxGridCellEditor *wxGrid::GetDefaultEditor() const | |
9694 | { | |
9695 | return m_defaultCellAttr->GetEditor(NULL, 0, 0); | |
9696 | } | |
9697 | ||
9698 | // ---------------------------------------------------------------------------- | |
9699 | // access to cell attributes | |
9700 | // ---------------------------------------------------------------------------- | |
9701 | ||
9702 | wxColour wxGrid::GetCellBackgroundColour(int row, int col) | |
9703 | { | |
9704 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9705 | wxColour colour = attr->GetBackgroundColour(); | |
9706 | attr->DecRef(); | |
9707 | ||
9708 | return colour; | |
9709 | } | |
9710 | ||
9711 | wxColour wxGrid::GetCellTextColour( int row, int col ) | |
9712 | { | |
9713 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9714 | wxColour colour = attr->GetTextColour(); | |
9715 | attr->DecRef(); | |
9716 | ||
9717 | return colour; | |
9718 | } | |
9719 | ||
9720 | wxFont wxGrid::GetCellFont( int row, int col ) | |
9721 | { | |
9722 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9723 | wxFont font = attr->GetFont(); | |
9724 | attr->DecRef(); | |
9725 | ||
9726 | return font; | |
9727 | } | |
9728 | ||
9729 | void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) | |
9730 | { | |
9731 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9732 | attr->GetAlignment(horiz, vert); | |
9733 | attr->DecRef(); | |
9734 | } | |
9735 | ||
9736 | bool wxGrid::GetCellOverflow( int row, int col ) | |
9737 | { | |
9738 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9739 | bool allow = attr->GetOverflow(); | |
9740 | attr->DecRef(); | |
9741 | ||
9742 | return allow; | |
9743 | } | |
9744 | ||
9745 | void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) | |
9746 | { | |
9747 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
9748 | attr->GetSize( num_rows, num_cols ); | |
9749 | attr->DecRef(); | |
9750 | } | |
9751 | ||
9752 | wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) | |
9753 | { | |
9754 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
9755 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); | |
9756 | attr->DecRef(); | |
9757 | ||
9758 | return renderer; | |
9759 | } | |
9760 | ||
9761 | wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) | |
9762 | { | |
9763 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
9764 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); | |
9765 | attr->DecRef(); | |
9766 | ||
9767 | return editor; | |
9768 | } | |
9769 | ||
9770 | bool wxGrid::IsReadOnly(int row, int col) const | |
9771 | { | |
9772 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
9773 | bool isReadOnly = attr->IsReadOnly(); | |
9774 | attr->DecRef(); | |
9775 | ||
9776 | return isReadOnly; | |
9777 | } | |
9778 | ||
9779 | // ---------------------------------------------------------------------------- | |
9780 | // attribute support: cache, automatic provider creation, ... | |
9781 | // ---------------------------------------------------------------------------- | |
9782 | ||
9783 | bool wxGrid::CanHaveAttributes() | |
9784 | { | |
9785 | if ( !m_table ) | |
9786 | { | |
9787 | return false; | |
9788 | } | |
9789 | ||
9790 | return m_table->CanHaveAttributes(); | |
9791 | } | |
9792 | ||
9793 | void wxGrid::ClearAttrCache() | |
9794 | { | |
9795 | if ( m_attrCache.row != -1 ) | |
9796 | { | |
9797 | wxSafeDecRef(m_attrCache.attr); | |
9798 | m_attrCache.attr = NULL; | |
9799 | m_attrCache.row = -1; | |
9800 | } | |
9801 | } | |
9802 | ||
9803 | void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const | |
9804 | { | |
9805 | if ( attr != NULL ) | |
9806 | { | |
9807 | wxGrid *self = (wxGrid *)this; // const_cast | |
9808 | ||
9809 | self->ClearAttrCache(); | |
9810 | self->m_attrCache.row = row; | |
9811 | self->m_attrCache.col = col; | |
9812 | self->m_attrCache.attr = attr; | |
9813 | wxSafeIncRef(attr); | |
9814 | } | |
9815 | } | |
9816 | ||
9817 | bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const | |
9818 | { | |
9819 | if ( row == m_attrCache.row && col == m_attrCache.col ) | |
9820 | { | |
9821 | *attr = m_attrCache.attr; | |
9822 | wxSafeIncRef(m_attrCache.attr); | |
9823 | ||
9824 | #ifdef DEBUG_ATTR_CACHE | |
9825 | gs_nAttrCacheHits++; | |
9826 | #endif | |
9827 | ||
9828 | return true; | |
9829 | } | |
9830 | else | |
9831 | { | |
9832 | #ifdef DEBUG_ATTR_CACHE | |
9833 | gs_nAttrCacheMisses++; | |
9834 | #endif | |
9835 | ||
9836 | return false; | |
9837 | } | |
9838 | } | |
9839 | ||
9840 | wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const | |
9841 | { | |
9842 | wxGridCellAttr *attr = NULL; | |
9843 | // Additional test to avoid looking at the cache e.g. for | |
9844 | // wxNoCellCoords, as this will confuse memory management. | |
9845 | if ( row >= 0 ) | |
9846 | { | |
9847 | if ( !LookupAttr(row, col, &attr) ) | |
9848 | { | |
9849 | attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any) | |
9850 | : (wxGridCellAttr *)NULL; | |
9851 | CacheAttr(row, col, attr); | |
9852 | } | |
9853 | } | |
9854 | ||
9855 | if (attr) | |
9856 | { | |
9857 | attr->SetDefAttr(m_defaultCellAttr); | |
9858 | } | |
9859 | else | |
9860 | { | |
9861 | attr = m_defaultCellAttr; | |
9862 | attr->IncRef(); | |
9863 | } | |
9864 | ||
9865 | return attr; | |
9866 | } | |
9867 | ||
9868 | wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const | |
9869 | { | |
9870 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; | |
9871 | bool canHave = ((wxGrid*)this)->CanHaveAttributes(); | |
9872 | ||
9873 | wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed")); | |
9874 | wxCHECK_MSG( m_table, attr, _T("must have a table") ); | |
9875 | ||
9876 | attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell); | |
9877 | if ( !attr ) | |
9878 | { | |
9879 | attr = new wxGridCellAttr(m_defaultCellAttr); | |
9880 | ||
9881 | // artificially inc the ref count to match DecRef() in caller | |
9882 | attr->IncRef(); | |
9883 | m_table->SetAttr(attr, row, col); | |
9884 | } | |
9885 | ||
9886 | return attr; | |
9887 | } | |
9888 | ||
9889 | // ---------------------------------------------------------------------------- | |
9890 | // setting column attributes (wrappers around SetColAttr) | |
9891 | // ---------------------------------------------------------------------------- | |
9892 | ||
9893 | void wxGrid::SetColFormatBool(int col) | |
9894 | { | |
9895 | SetColFormatCustom(col, wxGRID_VALUE_BOOL); | |
9896 | } | |
9897 | ||
9898 | void wxGrid::SetColFormatNumber(int col) | |
9899 | { | |
9900 | SetColFormatCustom(col, wxGRID_VALUE_NUMBER); | |
9901 | } | |
9902 | ||
9903 | void wxGrid::SetColFormatFloat(int col, int width, int precision) | |
9904 | { | |
9905 | wxString typeName = wxGRID_VALUE_FLOAT; | |
9906 | if ( (width != -1) || (precision != -1) ) | |
9907 | { | |
9908 | typeName << _T(':') << width << _T(',') << precision; | |
9909 | } | |
9910 | ||
9911 | SetColFormatCustom(col, typeName); | |
9912 | } | |
9913 | ||
9914 | void wxGrid::SetColFormatCustom(int col, const wxString& typeName) | |
9915 | { | |
9916 | wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col ); | |
9917 | if (!attr) | |
9918 | attr = new wxGridCellAttr; | |
9919 | wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName); | |
9920 | attr->SetRenderer(renderer); | |
9921 | ||
9922 | SetColAttr(col, attr); | |
9923 | ||
9924 | } | |
9925 | ||
9926 | // ---------------------------------------------------------------------------- | |
9927 | // setting cell attributes: this is forwarded to the table | |
9928 | // ---------------------------------------------------------------------------- | |
9929 | ||
9930 | void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr) | |
9931 | { | |
9932 | if ( CanHaveAttributes() ) | |
9933 | { | |
9934 | m_table->SetAttr(attr, row, col); | |
9935 | ClearAttrCache(); | |
9936 | } | |
9937 | else | |
9938 | { | |
9939 | wxSafeDecRef(attr); | |
9940 | } | |
9941 | } | |
9942 | ||
9943 | void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr) | |
9944 | { | |
9945 | if ( CanHaveAttributes() ) | |
9946 | { | |
9947 | m_table->SetRowAttr(attr, row); | |
9948 | ClearAttrCache(); | |
9949 | } | |
9950 | else | |
9951 | { | |
9952 | wxSafeDecRef(attr); | |
9953 | } | |
9954 | } | |
9955 | ||
9956 | void wxGrid::SetColAttr(int col, wxGridCellAttr *attr) | |
9957 | { | |
9958 | if ( CanHaveAttributes() ) | |
9959 | { | |
9960 | m_table->SetColAttr(attr, col); | |
9961 | ClearAttrCache(); | |
9962 | } | |
9963 | else | |
9964 | { | |
9965 | wxSafeDecRef(attr); | |
9966 | } | |
9967 | } | |
9968 | ||
9969 | void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour ) | |
9970 | { | |
9971 | if ( CanHaveAttributes() ) | |
9972 | { | |
9973 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9974 | attr->SetBackgroundColour(colour); | |
9975 | attr->DecRef(); | |
9976 | } | |
9977 | } | |
9978 | ||
9979 | void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour ) | |
9980 | { | |
9981 | if ( CanHaveAttributes() ) | |
9982 | { | |
9983 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9984 | attr->SetTextColour(colour); | |
9985 | attr->DecRef(); | |
9986 | } | |
9987 | } | |
9988 | ||
9989 | void wxGrid::SetCellFont( int row, int col, const wxFont& font ) | |
9990 | { | |
9991 | if ( CanHaveAttributes() ) | |
9992 | { | |
9993 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
9994 | attr->SetFont(font); | |
9995 | attr->DecRef(); | |
9996 | } | |
9997 | } | |
9998 | ||
9999 | void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert ) | |
10000 | { | |
10001 | if ( CanHaveAttributes() ) | |
10002 | { | |
10003 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
10004 | attr->SetAlignment(horiz, vert); | |
10005 | attr->DecRef(); | |
10006 | } | |
10007 | } | |
10008 | ||
10009 | void wxGrid::SetCellOverflow( int row, int col, bool allow ) | |
10010 | { | |
10011 | if ( CanHaveAttributes() ) | |
10012 | { | |
10013 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
10014 | attr->SetOverflow(allow); | |
10015 | attr->DecRef(); | |
10016 | } | |
10017 | } | |
10018 | ||
10019 | void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols ) | |
10020 | { | |
10021 | if ( CanHaveAttributes() ) | |
10022 | { | |
10023 | int cell_rows, cell_cols; | |
10024 | ||
10025 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
10026 | attr->GetSize(&cell_rows, &cell_cols); | |
10027 | attr->SetSize(num_rows, num_cols); | |
10028 | attr->DecRef(); | |
10029 | ||
10030 | // Cannot set the size of a cell to 0 or negative values | |
10031 | // While it is perfectly legal to do that, this function cannot | |
10032 | // handle all the possibilies, do it by hand by getting the CellAttr. | |
10033 | // You can only set the size of a cell to 1,1 or greater with this fn | |
10034 | wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)), | |
10035 | wxT("wxGrid::SetCellSize setting cell size that is already part of another cell")); | |
10036 | wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)), | |
10037 | wxT("wxGrid::SetCellSize setting cell size to < 1")); | |
10038 | ||
10039 | // if this was already a multicell then "turn off" the other cells first | |
10040 | if ((cell_rows > 1) || (cell_rows > 1)) | |
10041 | { | |
10042 | int i, j; | |
10043 | for (j=row; j < row + cell_rows; j++) | |
10044 | { | |
10045 | for (i=col; i < col + cell_cols; i++) | |
10046 | { | |
10047 | if ((i != col) || (j != row)) | |
10048 | { | |
10049 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
10050 | attr_stub->SetSize( 1, 1 ); | |
10051 | attr_stub->DecRef(); | |
10052 | } | |
10053 | } | |
10054 | } | |
10055 | } | |
10056 | ||
10057 | // mark the cells that will be covered by this cell to | |
10058 | // negative or zero values to point back at this cell | |
10059 | if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1)) | |
10060 | { | |
10061 | int i, j; | |
10062 | for (j=row; j < row + num_rows; j++) | |
10063 | { | |
10064 | for (i=col; i < col + num_cols; i++) | |
10065 | { | |
10066 | if ((i != col) || (j != row)) | |
10067 | { | |
10068 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
10069 | attr_stub->SetSize( row - j, col - i ); | |
10070 | attr_stub->DecRef(); | |
10071 | } | |
10072 | } | |
10073 | } | |
10074 | } | |
10075 | } | |
10076 | } | |
10077 | ||
10078 | void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer) | |
10079 | { | |
10080 | if ( CanHaveAttributes() ) | |
10081 | { | |
10082 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
10083 | attr->SetRenderer(renderer); | |
10084 | attr->DecRef(); | |
10085 | } | |
10086 | } | |
10087 | ||
10088 | void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor) | |
10089 | { | |
10090 | if ( CanHaveAttributes() ) | |
10091 | { | |
10092 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
10093 | attr->SetEditor(editor); | |
10094 | attr->DecRef(); | |
10095 | } | |
10096 | } | |
10097 | ||
10098 | void wxGrid::SetReadOnly(int row, int col, bool isReadOnly) | |
10099 | { | |
10100 | if ( CanHaveAttributes() ) | |
10101 | { | |
10102 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
10103 | attr->SetReadOnly(isReadOnly); | |
10104 | attr->DecRef(); | |
10105 | } | |
10106 | } | |
10107 | ||
10108 | // ---------------------------------------------------------------------------- | |
10109 | // Data type registration | |
10110 | // ---------------------------------------------------------------------------- | |
10111 | ||
10112 | void wxGrid::RegisterDataType(const wxString& typeName, | |
10113 | wxGridCellRenderer* renderer, | |
10114 | wxGridCellEditor* editor) | |
10115 | { | |
10116 | m_typeRegistry->RegisterDataType(typeName, renderer, editor); | |
10117 | } | |
10118 | ||
10119 | ||
10120 | wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const | |
10121 | { | |
10122 | wxString typeName = m_table->GetTypeName(row, col); | |
10123 | return GetDefaultEditorForType(typeName); | |
10124 | } | |
10125 | ||
10126 | wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const | |
10127 | { | |
10128 | wxString typeName = m_table->GetTypeName(row, col); | |
10129 | return GetDefaultRendererForType(typeName); | |
10130 | } | |
10131 | ||
10132 | wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const | |
10133 | { | |
10134 | int index = m_typeRegistry->FindOrCloneDataType(typeName); | |
10135 | if ( index == wxNOT_FOUND ) | |
10136 | { | |
10137 | wxString errStr; | |
10138 | ||
10139 | errStr.Printf(wxT("Unknown data type name [%s]"), typeName.c_str()); | |
10140 | wxFAIL_MSG(errStr.c_str()); | |
10141 | ||
10142 | return NULL; | |
10143 | } | |
10144 | ||
10145 | return m_typeRegistry->GetEditor(index); | |
10146 | } | |
10147 | ||
10148 | wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const | |
10149 | { | |
10150 | int index = m_typeRegistry->FindOrCloneDataType(typeName); | |
10151 | if ( index == wxNOT_FOUND ) | |
10152 | { | |
10153 | wxString errStr; | |
10154 | ||
10155 | errStr.Printf(wxT("Unknown data type name [%s]"), typeName.c_str()); | |
10156 | wxFAIL_MSG(errStr.c_str()); | |
10157 | ||
10158 | return NULL; | |
10159 | } | |
10160 | ||
10161 | return m_typeRegistry->GetRenderer(index); | |
10162 | } | |
10163 | ||
10164 | // ---------------------------------------------------------------------------- | |
10165 | // row/col size | |
10166 | // ---------------------------------------------------------------------------- | |
10167 | ||
10168 | void wxGrid::EnableDragRowSize( bool enable ) | |
10169 | { | |
10170 | m_canDragRowSize = enable; | |
10171 | } | |
10172 | ||
10173 | void wxGrid::EnableDragColSize( bool enable ) | |
10174 | { | |
10175 | m_canDragColSize = enable; | |
10176 | } | |
10177 | ||
10178 | void wxGrid::EnableDragGridSize( bool enable ) | |
10179 | { | |
10180 | m_canDragGridSize = enable; | |
10181 | } | |
10182 | ||
10183 | void wxGrid::EnableDragCell( bool enable ) | |
10184 | { | |
10185 | m_canDragCell = enable; | |
10186 | } | |
10187 | ||
10188 | void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) | |
10189 | { | |
10190 | m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight ); | |
10191 | ||
10192 | if ( resizeExistingRows ) | |
10193 | { | |
10194 | // since we are resizing all rows to the default row size, | |
10195 | // we can simply clear the row heights and row bottoms | |
10196 | // arrays (which also allows us to take advantage of | |
10197 | // some speed optimisations) | |
10198 | m_rowHeights.Empty(); | |
10199 | m_rowBottoms.Empty(); | |
10200 | if ( !GetBatchCount() ) | |
10201 | CalcDimensions(); | |
10202 | } | |
10203 | } | |
10204 | ||
10205 | void wxGrid::SetRowSize( int row, int height ) | |
10206 | { | |
10207 | wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") ); | |
10208 | ||
10209 | // See comment in SetColSize | |
10210 | if ( height < GetRowMinimalAcceptableHeight()) | |
10211 | return; | |
10212 | ||
10213 | if ( m_rowHeights.IsEmpty() ) | |
10214 | { | |
10215 | // need to really create the array | |
10216 | InitRowHeights(); | |
10217 | } | |
10218 | ||
10219 | int h = wxMax( 0, height ); | |
10220 | int diff = h - m_rowHeights[row]; | |
10221 | ||
10222 | m_rowHeights[row] = h; | |
10223 | int i; | |
10224 | for ( i = row; i < m_numRows; i++ ) | |
10225 | { | |
10226 | m_rowBottoms[i] += diff; | |
10227 | } | |
10228 | ||
10229 | if ( !GetBatchCount() ) | |
10230 | CalcDimensions(); | |
10231 | } | |
10232 | ||
10233 | void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) | |
10234 | { | |
10235 | m_defaultColWidth = wxMax( width, m_minAcceptableColWidth ); | |
10236 | ||
10237 | if ( resizeExistingCols ) | |
10238 | { | |
10239 | // since we are resizing all columns to the default column size, | |
10240 | // we can simply clear the col widths and col rights | |
10241 | // arrays (which also allows us to take advantage of | |
10242 | // some speed optimisations) | |
10243 | m_colWidths.Empty(); | |
10244 | m_colRights.Empty(); | |
10245 | if ( !GetBatchCount() ) | |
10246 | CalcDimensions(); | |
10247 | } | |
10248 | } | |
10249 | ||
10250 | void wxGrid::SetColSize( int col, int width ) | |
10251 | { | |
10252 | wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") ); | |
10253 | ||
10254 | // should we check that it's bigger than GetColMinimalWidth(col) here? | |
10255 | // (VZ) | |
10256 | // No, because it is reasonable to assume the library user know's | |
10257 | // what he is doing. However we should test against the weaker | |
10258 | // constraint of minimalAcceptableWidth, as this breaks rendering | |
10259 | // | |
10260 | // This test then fixes sf.net bug #645734 | |
10261 | ||
10262 | if ( width < GetColMinimalAcceptableWidth() ) | |
10263 | return; | |
10264 | ||
10265 | if ( m_colWidths.IsEmpty() ) | |
10266 | { | |
10267 | // need to really create the array | |
10268 | InitColWidths(); | |
10269 | } | |
10270 | ||
10271 | // if < 0 then calculate new width from label | |
10272 | if ( width < 0 ) | |
10273 | { | |
10274 | long w, h; | |
10275 | wxArrayString lines; | |
10276 | wxClientDC dc(m_colLabelWin); | |
10277 | dc.SetFont(GetLabelFont()); | |
10278 | StringToLines(GetColLabelValue(col), lines); | |
10279 | GetTextBoxSize(dc, lines, &w, &h); | |
10280 | width = w + 6; | |
10281 | } | |
10282 | ||
10283 | int w = wxMax( 0, width ); | |
10284 | int diff = w - m_colWidths[col]; | |
10285 | m_colWidths[col] = w; | |
10286 | ||
10287 | int i; | |
10288 | int colPos; | |
10289 | for ( colPos = GetColPos( col ); colPos < m_numCols; colPos++ ) | |
10290 | { | |
10291 | i = GetColAt( colPos ); | |
10292 | m_colRights[i] += diff; | |
10293 | } | |
10294 | ||
10295 | if ( !GetBatchCount() ) | |
10296 | CalcDimensions(); | |
10297 | } | |
10298 | ||
10299 | void wxGrid::SetColMinimalWidth( int col, int width ) | |
10300 | { | |
10301 | if (width > GetColMinimalAcceptableWidth()) | |
10302 | { | |
10303 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col; | |
10304 | m_colMinWidths[key] = width; | |
10305 | } | |
10306 | } | |
10307 | ||
10308 | void wxGrid::SetRowMinimalHeight( int row, int width ) | |
10309 | { | |
10310 | if (width > GetRowMinimalAcceptableHeight()) | |
10311 | { | |
10312 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row; | |
10313 | m_rowMinHeights[key] = width; | |
10314 | } | |
10315 | } | |
10316 | ||
10317 | int wxGrid::GetColMinimalWidth(int col) const | |
10318 | { | |
10319 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col; | |
10320 | wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key); | |
10321 | ||
10322 | return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth; | |
10323 | } | |
10324 | ||
10325 | int wxGrid::GetRowMinimalHeight(int row) const | |
10326 | { | |
10327 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row; | |
10328 | wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key); | |
10329 | ||
10330 | return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight; | |
10331 | } | |
10332 | ||
10333 | void wxGrid::SetColMinimalAcceptableWidth( int width ) | |
10334 | { | |
10335 | // We do allow a width of 0 since this gives us | |
10336 | // an easy way to temporarily hiding columns. | |
10337 | if ( width >= 0 ) | |
10338 | m_minAcceptableColWidth = width; | |
10339 | } | |
10340 | ||
10341 | void wxGrid::SetRowMinimalAcceptableHeight( int height ) | |
10342 | { | |
10343 | // We do allow a height of 0 since this gives us | |
10344 | // an easy way to temporarily hiding rows. | |
10345 | if ( height >= 0 ) | |
10346 | m_minAcceptableRowHeight = height; | |
10347 | } | |
10348 | ||
10349 | int wxGrid::GetColMinimalAcceptableWidth() const | |
10350 | { | |
10351 | return m_minAcceptableColWidth; | |
10352 | } | |
10353 | ||
10354 | int wxGrid::GetRowMinimalAcceptableHeight() const | |
10355 | { | |
10356 | return m_minAcceptableRowHeight; | |
10357 | } | |
10358 | ||
10359 | // ---------------------------------------------------------------------------- | |
10360 | // auto sizing | |
10361 | // ---------------------------------------------------------------------------- | |
10362 | ||
10363 | void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) | |
10364 | { | |
10365 | wxClientDC dc(m_gridWin); | |
10366 | ||
10367 | // cancel editing of cell | |
10368 | HideCellEditControl(); | |
10369 | SaveEditControlValue(); | |
10370 | ||
10371 | // init both of them to avoid compiler warnings, even if we only need one | |
10372 | int row = -1, | |
10373 | col = -1; | |
10374 | if ( column ) | |
10375 | col = colOrRow; | |
10376 | else | |
10377 | row = colOrRow; | |
10378 | ||
10379 | wxCoord extent, extentMax = 0; | |
10380 | int max = column ? m_numRows : m_numCols; | |
10381 | for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ ) | |
10382 | { | |
10383 | if ( column ) | |
10384 | row = rowOrCol; | |
10385 | else | |
10386 | col = rowOrCol; | |
10387 | ||
10388 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
10389 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); | |
10390 | if ( renderer ) | |
10391 | { | |
10392 | wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col); | |
10393 | extent = column ? size.x : size.y; | |
10394 | if ( extent > extentMax ) | |
10395 | extentMax = extent; | |
10396 | ||
10397 | renderer->DecRef(); | |
10398 | } | |
10399 | ||
10400 | attr->DecRef(); | |
10401 | } | |
10402 | ||
10403 | // now also compare with the column label extent | |
10404 | wxCoord w, h; | |
10405 | dc.SetFont( GetLabelFont() ); | |
10406 | ||
10407 | if ( column ) | |
10408 | { | |
10409 | dc.GetMultiLineTextExtent( GetColLabelValue(col), &w, &h ); | |
10410 | if ( GetColLabelTextOrientation() == wxVERTICAL ) | |
10411 | w = h; | |
10412 | } | |
10413 | else | |
10414 | dc.GetMultiLineTextExtent( GetRowLabelValue(row), &w, &h ); | |
10415 | ||
10416 | extent = column ? w : h; | |
10417 | if ( extent > extentMax ) | |
10418 | extentMax = extent; | |
10419 | ||
10420 | if ( !extentMax ) | |
10421 | { | |
10422 | // empty column - give default extent (notice that if extentMax is less | |
10423 | // than default extent but != 0, it's OK) | |
10424 | extentMax = column ? m_defaultColWidth : m_defaultRowHeight; | |
10425 | } | |
10426 | else | |
10427 | { | |
10428 | if ( column ) | |
10429 | // leave some space around text | |
10430 | extentMax += 10; | |
10431 | else | |
10432 | extentMax += 6; | |
10433 | } | |
10434 | ||
10435 | if ( column ) | |
10436 | { | |
10437 | SetColSize( col, extentMax ); | |
10438 | if ( !GetBatchCount() ) | |
10439 | { | |
10440 | int cw, ch, dummy; | |
10441 | m_gridWin->GetClientSize( &cw, &ch ); | |
10442 | wxRect rect ( CellToRect( 0, col ) ); | |
10443 | rect.y = 0; | |
10444 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
10445 | rect.width = cw - rect.x; | |
10446 | rect.height = m_colLabelHeight; | |
10447 | m_colLabelWin->Refresh( true, &rect ); | |
10448 | } | |
10449 | } | |
10450 | else | |
10451 | { | |
10452 | SetRowSize(row, extentMax); | |
10453 | if ( !GetBatchCount() ) | |
10454 | { | |
10455 | int cw, ch, dummy; | |
10456 | m_gridWin->GetClientSize( &cw, &ch ); | |
10457 | wxRect rect( CellToRect( row, 0 ) ); | |
10458 | rect.x = 0; | |
10459 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
10460 | rect.width = m_rowLabelWidth; | |
10461 | rect.height = ch - rect.y; | |
10462 | m_rowLabelWin->Refresh( true, &rect ); | |
10463 | } | |
10464 | } | |
10465 | ||
10466 | if ( setAsMin ) | |
10467 | { | |
10468 | if ( column ) | |
10469 | SetColMinimalWidth(col, extentMax); | |
10470 | else | |
10471 | SetRowMinimalHeight(row, extentMax); | |
10472 | } | |
10473 | } | |
10474 | ||
10475 | int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) | |
10476 | { | |
10477 | int width = m_rowLabelWidth; | |
10478 | ||
10479 | if ( !calcOnly ) | |
10480 | BeginBatch(); | |
10481 | ||
10482 | for ( int col = 0; col < m_numCols; col++ ) | |
10483 | { | |
10484 | if ( !calcOnly ) | |
10485 | AutoSizeColumn(col, setAsMin); | |
10486 | ||
10487 | width += GetColWidth(col); | |
10488 | } | |
10489 | ||
10490 | if ( !calcOnly ) | |
10491 | EndBatch(); | |
10492 | ||
10493 | return width; | |
10494 | } | |
10495 | ||
10496 | int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin) | |
10497 | { | |
10498 | int height = m_colLabelHeight; | |
10499 | ||
10500 | if ( !calcOnly ) | |
10501 | BeginBatch(); | |
10502 | ||
10503 | for ( int row = 0; row < m_numRows; row++ ) | |
10504 | { | |
10505 | if ( !calcOnly ) | |
10506 | AutoSizeRow(row, setAsMin); | |
10507 | ||
10508 | height += GetRowHeight(row); | |
10509 | } | |
10510 | ||
10511 | if ( !calcOnly ) | |
10512 | EndBatch(); | |
10513 | ||
10514 | return height; | |
10515 | } | |
10516 | ||
10517 | void wxGrid::AutoSize() | |
10518 | { | |
10519 | BeginBatch(); | |
10520 | ||
10521 | wxSize size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false)); | |
10522 | ||
10523 | // round up the size to a multiple of scroll step - this ensures that we | |
10524 | // won't get the scrollbars if we're sized exactly to this width | |
10525 | // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary | |
10526 | // scrollbar steps | |
10527 | wxSize sizeFit( | |
10528 | GetScrollX(size.x + m_extraWidth + 1) * m_scrollLineX, | |
10529 | GetScrollY(size.y + m_extraHeight + 1) * m_scrollLineY ); | |
10530 | ||
10531 | // distribute the extra space between the columns/rows to avoid having | |
10532 | // extra white space | |
10533 | ||
10534 | // Remove the extra m_extraWidth + 1 added above | |
10535 | wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1); | |
10536 | if ( diff && m_numCols ) | |
10537 | { | |
10538 | // try to resize the columns uniformly | |
10539 | wxCoord diffPerCol = diff / m_numCols; | |
10540 | if ( diffPerCol ) | |
10541 | { | |
10542 | for ( int col = 0; col < m_numCols; col++ ) | |
10543 | { | |
10544 | SetColSize(col, GetColWidth(col) + diffPerCol); | |
10545 | } | |
10546 | } | |
10547 | ||
10548 | // add remaining amount to the last columns | |
10549 | diff -= diffPerCol * m_numCols; | |
10550 | if ( diff ) | |
10551 | { | |
10552 | for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- ) | |
10553 | { | |
10554 | SetColSize(col, GetColWidth(col) + 1); | |
10555 | } | |
10556 | } | |
10557 | } | |
10558 | ||
10559 | // same for rows | |
10560 | diff = sizeFit.y - size.y - (m_extraHeight + 1); | |
10561 | if ( diff && m_numRows ) | |
10562 | { | |
10563 | // try to resize the columns uniformly | |
10564 | wxCoord diffPerRow = diff / m_numRows; | |
10565 | if ( diffPerRow ) | |
10566 | { | |
10567 | for ( int row = 0; row < m_numRows; row++ ) | |
10568 | { | |
10569 | SetRowSize(row, GetRowHeight(row) + diffPerRow); | |
10570 | } | |
10571 | } | |
10572 | ||
10573 | // add remaining amount to the last rows | |
10574 | diff -= diffPerRow * m_numRows; | |
10575 | if ( diff ) | |
10576 | { | |
10577 | for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- ) | |
10578 | { | |
10579 | SetRowSize(row, GetRowHeight(row) + 1); | |
10580 | } | |
10581 | } | |
10582 | } | |
10583 | ||
10584 | EndBatch(); | |
10585 | ||
10586 | SetClientSize(sizeFit); | |
10587 | } | |
10588 | ||
10589 | void wxGrid::AutoSizeRowLabelSize( int row ) | |
10590 | { | |
10591 | wxArrayString lines; | |
10592 | long w, h; | |
10593 | ||
10594 | // Hide the edit control, so it | |
10595 | // won't interfere with drag-shrinking. | |
10596 | if ( IsCellEditControlShown() ) | |
10597 | { | |
10598 | HideCellEditControl(); | |
10599 | SaveEditControlValue(); | |
10600 | } | |
10601 | ||
10602 | // autosize row height depending on label text | |
10603 | StringToLines( GetRowLabelValue( row ), lines ); | |
10604 | wxClientDC dc( m_rowLabelWin ); | |
10605 | GetTextBoxSize( dc, lines, &w, &h ); | |
10606 | if ( h < m_defaultRowHeight ) | |
10607 | h = m_defaultRowHeight; | |
10608 | SetRowSize(row, h); | |
10609 | ForceRefresh(); | |
10610 | } | |
10611 | ||
10612 | void wxGrid::AutoSizeColLabelSize( int col ) | |
10613 | { | |
10614 | wxArrayString lines; | |
10615 | long w, h; | |
10616 | ||
10617 | // Hide the edit control, so it | |
10618 | // won't interfere with drag-shrinking. | |
10619 | if ( IsCellEditControlShown() ) | |
10620 | { | |
10621 | HideCellEditControl(); | |
10622 | SaveEditControlValue(); | |
10623 | } | |
10624 | ||
10625 | // autosize column width depending on label text | |
10626 | StringToLines( GetColLabelValue( col ), lines ); | |
10627 | wxClientDC dc( m_colLabelWin ); | |
10628 | if ( GetColLabelTextOrientation() == wxHORIZONTAL ) | |
10629 | GetTextBoxSize( dc, lines, &w, &h ); | |
10630 | else | |
10631 | GetTextBoxSize( dc, lines, &h, &w ); | |
10632 | if ( w < m_defaultColWidth ) | |
10633 | w = m_defaultColWidth; | |
10634 | SetColSize(col, w); | |
10635 | ForceRefresh(); | |
10636 | } | |
10637 | ||
10638 | wxSize wxGrid::DoGetBestSize() const | |
10639 | { | |
10640 | // don't set sizes, only calculate them | |
10641 | wxGrid *self = (wxGrid *)this; // const_cast | |
10642 | ||
10643 | int width, height; | |
10644 | width = self->SetOrCalcColumnSizes(true); | |
10645 | height = self->SetOrCalcRowSizes(true); | |
10646 | ||
10647 | if (!width) | |
10648 | width = 100; | |
10649 | if (!height) | |
10650 | height = 80; | |
10651 | ||
10652 | // Round up to a multiple the scroll rate | |
10653 | // NOTE: this still doesn't get rid of the scrollbars; | |
10654 | // is there any magic incantation for that? | |
10655 | int xpu, ypu; | |
10656 | GetScrollPixelsPerUnit(&xpu, &ypu); | |
10657 | if (xpu) | |
10658 | width += 1 + xpu - (width % xpu); | |
10659 | if (ypu) | |
10660 | height += 1 + ypu - (height % ypu); | |
10661 | ||
10662 | // limit to 1/4 of the screen size | |
10663 | int maxwidth, maxheight; | |
10664 | wxDisplaySize( &maxwidth, &maxheight ); | |
10665 | maxwidth /= 2; | |
10666 | maxheight /= 2; | |
10667 | if ( width > maxwidth ) | |
10668 | width = maxwidth; | |
10669 | if ( height > maxheight ) | |
10670 | height = maxheight; | |
10671 | ||
10672 | wxSize best(width, height); | |
10673 | ||
10674 | // NOTE: This size should be cached, but first we need to add calls to | |
10675 | // InvalidateBestSize everywhere that could change the results of this | |
10676 | // calculation. | |
10677 | // CacheBestSize(size); | |
10678 | ||
10679 | return best; | |
10680 | } | |
10681 | ||
10682 | void wxGrid::Fit() | |
10683 | { | |
10684 | AutoSize(); | |
10685 | } | |
10686 | ||
10687 | wxPen& wxGrid::GetDividerPen() const | |
10688 | { | |
10689 | return wxNullPen; | |
10690 | } | |
10691 | ||
10692 | // ---------------------------------------------------------------------------- | |
10693 | // cell value accessor functions | |
10694 | // ---------------------------------------------------------------------------- | |
10695 | ||
10696 | void wxGrid::SetCellValue( int row, int col, const wxString& s ) | |
10697 | { | |
10698 | if ( m_table ) | |
10699 | { | |
10700 | m_table->SetValue( row, col, s ); | |
10701 | if ( !GetBatchCount() ) | |
10702 | { | |
10703 | int dummy; | |
10704 | wxRect rect( CellToRect( row, col ) ); | |
10705 | rect.x = 0; | |
10706 | rect.width = m_gridWin->GetClientSize().GetWidth(); | |
10707 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
10708 | m_gridWin->Refresh( false, &rect ); | |
10709 | } | |
10710 | ||
10711 | if ( m_currentCellCoords.GetRow() == row && | |
10712 | m_currentCellCoords.GetCol() == col && | |
10713 | IsCellEditControlShown()) | |
10714 | // Note: If we are using IsCellEditControlEnabled, | |
10715 | // this interacts badly with calling SetCellValue from | |
10716 | // an EVT_GRID_CELL_CHANGE handler. | |
10717 | { | |
10718 | HideCellEditControl(); | |
10719 | ShowCellEditControl(); // will reread data from table | |
10720 | } | |
10721 | } | |
10722 | } | |
10723 | ||
10724 | // ---------------------------------------------------------------------------- | |
10725 | // block, row and column selection | |
10726 | // ---------------------------------------------------------------------------- | |
10727 | ||
10728 | void wxGrid::SelectRow( int row, bool addToSelected ) | |
10729 | { | |
10730 | if ( IsSelection() && !addToSelected ) | |
10731 | ClearSelection(); | |
10732 | ||
10733 | if ( m_selection ) | |
10734 | m_selection->SelectRow( row, false, addToSelected ); | |
10735 | } | |
10736 | ||
10737 | void wxGrid::SelectCol( int col, bool addToSelected ) | |
10738 | { | |
10739 | if ( IsSelection() && !addToSelected ) | |
10740 | ClearSelection(); | |
10741 | ||
10742 | if ( m_selection ) | |
10743 | m_selection->SelectCol( col, false, addToSelected ); | |
10744 | } | |
10745 | ||
10746 | void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol, | |
10747 | bool addToSelected ) | |
10748 | { | |
10749 | if ( IsSelection() && !addToSelected ) | |
10750 | ClearSelection(); | |
10751 | ||
10752 | if ( m_selection ) | |
10753 | m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol, | |
10754 | false, addToSelected ); | |
10755 | } | |
10756 | ||
10757 | void wxGrid::SelectAll() | |
10758 | { | |
10759 | if ( m_numRows > 0 && m_numCols > 0 ) | |
10760 | { | |
10761 | if ( m_selection ) | |
10762 | m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 ); | |
10763 | } | |
10764 | } | |
10765 | ||
10766 | // ---------------------------------------------------------------------------- | |
10767 | // cell, row and col deselection | |
10768 | // ---------------------------------------------------------------------------- | |
10769 | ||
10770 | void wxGrid::DeselectRow( int row ) | |
10771 | { | |
10772 | if ( !m_selection ) | |
10773 | return; | |
10774 | ||
10775 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows ) | |
10776 | { | |
10777 | if ( m_selection->IsInSelection(row, 0 ) ) | |
10778 | m_selection->ToggleCellSelection(row, 0); | |
10779 | } | |
10780 | else | |
10781 | { | |
10782 | int nCols = GetNumberCols(); | |
10783 | for ( int i = 0; i < nCols; i++ ) | |
10784 | { | |
10785 | if ( m_selection->IsInSelection(row, i ) ) | |
10786 | m_selection->ToggleCellSelection(row, i); | |
10787 | } | |
10788 | } | |
10789 | } | |
10790 | ||
10791 | void wxGrid::DeselectCol( int col ) | |
10792 | { | |
10793 | if ( !m_selection ) | |
10794 | return; | |
10795 | ||
10796 | if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns ) | |
10797 | { | |
10798 | if ( m_selection->IsInSelection(0, col ) ) | |
10799 | m_selection->ToggleCellSelection(0, col); | |
10800 | } | |
10801 | else | |
10802 | { | |
10803 | int nRows = GetNumberRows(); | |
10804 | for ( int i = 0; i < nRows; i++ ) | |
10805 | { | |
10806 | if ( m_selection->IsInSelection(i, col ) ) | |
10807 | m_selection->ToggleCellSelection(i, col); | |
10808 | } | |
10809 | } | |
10810 | } | |
10811 | ||
10812 | void wxGrid::DeselectCell( int row, int col ) | |
10813 | { | |
10814 | if ( m_selection && m_selection->IsInSelection(row, col) ) | |
10815 | m_selection->ToggleCellSelection(row, col); | |
10816 | } | |
10817 | ||
10818 | bool wxGrid::IsSelection() | |
10819 | { | |
10820 | return ( m_selection && (m_selection->IsSelection() || | |
10821 | ( m_selectingTopLeft != wxGridNoCellCoords && | |
10822 | m_selectingBottomRight != wxGridNoCellCoords) ) ); | |
10823 | } | |
10824 | ||
10825 | bool wxGrid::IsInSelection( int row, int col ) const | |
10826 | { | |
10827 | return ( m_selection && (m_selection->IsInSelection( row, col ) || | |
10828 | ( row >= m_selectingTopLeft.GetRow() && | |
10829 | col >= m_selectingTopLeft.GetCol() && | |
10830 | row <= m_selectingBottomRight.GetRow() && | |
10831 | col <= m_selectingBottomRight.GetCol() )) ); | |
10832 | } | |
10833 | ||
10834 | wxGridCellCoordsArray wxGrid::GetSelectedCells() const | |
10835 | { | |
10836 | if (!m_selection) | |
10837 | { | |
10838 | wxGridCellCoordsArray a; | |
10839 | return a; | |
10840 | } | |
10841 | ||
10842 | return m_selection->m_cellSelection; | |
10843 | } | |
10844 | ||
10845 | wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const | |
10846 | { | |
10847 | if (!m_selection) | |
10848 | { | |
10849 | wxGridCellCoordsArray a; | |
10850 | return a; | |
10851 | } | |
10852 | ||
10853 | return m_selection->m_blockSelectionTopLeft; | |
10854 | } | |
10855 | ||
10856 | wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const | |
10857 | { | |
10858 | if (!m_selection) | |
10859 | { | |
10860 | wxGridCellCoordsArray a; | |
10861 | return a; | |
10862 | } | |
10863 | ||
10864 | return m_selection->m_blockSelectionBottomRight; | |
10865 | } | |
10866 | ||
10867 | wxArrayInt wxGrid::GetSelectedRows() const | |
10868 | { | |
10869 | if (!m_selection) | |
10870 | { | |
10871 | wxArrayInt a; | |
10872 | return a; | |
10873 | } | |
10874 | ||
10875 | return m_selection->m_rowSelection; | |
10876 | } | |
10877 | ||
10878 | wxArrayInt wxGrid::GetSelectedCols() const | |
10879 | { | |
10880 | if (!m_selection) | |
10881 | { | |
10882 | wxArrayInt a; | |
10883 | return a; | |
10884 | } | |
10885 | ||
10886 | return m_selection->m_colSelection; | |
10887 | } | |
10888 | ||
10889 | void wxGrid::ClearSelection() | |
10890 | { | |
10891 | m_selectingTopLeft = | |
10892 | m_selectingBottomRight = | |
10893 | m_selectingKeyboard = wxGridNoCellCoords; | |
10894 | if ( m_selection ) | |
10895 | m_selection->ClearSelection(); | |
10896 | } | |
10897 | ||
10898 | // This function returns the rectangle that encloses the given block | |
10899 | // in device coords clipped to the client size of the grid window. | |
10900 | // | |
10901 | wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft, | |
10902 | const wxGridCellCoords &bottomRight ) | |
10903 | { | |
10904 | wxRect rect( wxGridNoCellRect ); | |
10905 | wxRect cellRect; | |
10906 | ||
10907 | cellRect = CellToRect( topLeft ); | |
10908 | if ( cellRect != wxGridNoCellRect ) | |
10909 | { | |
10910 | rect = cellRect; | |
10911 | } | |
10912 | else | |
10913 | { | |
10914 | rect = wxRect(0, 0, 0, 0); | |
10915 | } | |
10916 | ||
10917 | cellRect = CellToRect( bottomRight ); | |
10918 | if ( cellRect != wxGridNoCellRect ) | |
10919 | { | |
10920 | rect += cellRect; | |
10921 | } | |
10922 | else | |
10923 | { | |
10924 | return wxGridNoCellRect; | |
10925 | } | |
10926 | ||
10927 | int i, j; | |
10928 | int left = rect.GetLeft(); | |
10929 | int top = rect.GetTop(); | |
10930 | int right = rect.GetRight(); | |
10931 | int bottom = rect.GetBottom(); | |
10932 | ||
10933 | int leftCol = topLeft.GetCol(); | |
10934 | int topRow = topLeft.GetRow(); | |
10935 | int rightCol = bottomRight.GetCol(); | |
10936 | int bottomRow = bottomRight.GetRow(); | |
10937 | ||
10938 | if (left > right) | |
10939 | { | |
10940 | i = left; | |
10941 | left = right; | |
10942 | right = i; | |
10943 | i = leftCol; | |
10944 | leftCol = rightCol; | |
10945 | rightCol = i; | |
10946 | } | |
10947 | ||
10948 | if (top > bottom) | |
10949 | { | |
10950 | i = top; | |
10951 | top = bottom; | |
10952 | bottom = i; | |
10953 | i = topRow; | |
10954 | topRow = bottomRow; | |
10955 | bottomRow = i; | |
10956 | } | |
10957 | ||
10958 | for ( j = topRow; j <= bottomRow; j++ ) | |
10959 | { | |
10960 | for ( i = leftCol; i <= rightCol; i++ ) | |
10961 | { | |
10962 | if ((j == topRow) || (j == bottomRow) || (i == leftCol) || (i == rightCol)) | |
10963 | { | |
10964 | cellRect = CellToRect( j, i ); | |
10965 | ||
10966 | if (cellRect.x < left) | |
10967 | left = cellRect.x; | |
10968 | if (cellRect.y < top) | |
10969 | top = cellRect.y; | |
10970 | if (cellRect.x + cellRect.width > right) | |
10971 | right = cellRect.x + cellRect.width; | |
10972 | if (cellRect.y + cellRect.height > bottom) | |
10973 | bottom = cellRect.y + cellRect.height; | |
10974 | } | |
10975 | else | |
10976 | { | |
10977 | i = rightCol; // jump over inner cells. | |
10978 | } | |
10979 | } | |
10980 | } | |
10981 | ||
10982 | // convert to scrolled coords | |
10983 | // | |
10984 | CalcScrolledPosition( left, top, &left, &top ); | |
10985 | CalcScrolledPosition( right, bottom, &right, &bottom ); | |
10986 | ||
10987 | int cw, ch; | |
10988 | m_gridWin->GetClientSize( &cw, &ch ); | |
10989 | ||
10990 | if (right < 0 || bottom < 0 || left > cw || top > ch) | |
10991 | return wxRect(0,0,0,0); | |
10992 | ||
10993 | rect.SetLeft( wxMax(0, left) ); | |
10994 | rect.SetTop( wxMax(0, top) ); | |
10995 | rect.SetRight( wxMin(cw, right) ); | |
10996 | rect.SetBottom( wxMin(ch, bottom) ); | |
10997 | ||
10998 | return rect; | |
10999 | } | |
11000 | ||
11001 | // ---------------------------------------------------------------------------- | |
11002 | // grid event classes | |
11003 | // ---------------------------------------------------------------------------- | |
11004 | ||
11005 | IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent ) | |
11006 | ||
11007 | wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, | |
11008 | int row, int col, int x, int y, bool sel, | |
11009 | bool control, bool shift, bool alt, bool meta ) | |
11010 | : wxNotifyEvent( type, id ) | |
11011 | { | |
11012 | m_row = row; | |
11013 | m_col = col; | |
11014 | m_x = x; | |
11015 | m_y = y; | |
11016 | m_selecting = sel; | |
11017 | m_control = control; | |
11018 | m_shift = shift; | |
11019 | m_alt = alt; | |
11020 | m_meta = meta; | |
11021 | ||
11022 | SetEventObject(obj); | |
11023 | } | |
11024 | ||
11025 | ||
11026 | IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent ) | |
11027 | ||
11028 | wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj, | |
11029 | int rowOrCol, int x, int y, | |
11030 | bool control, bool shift, bool alt, bool meta ) | |
11031 | : wxNotifyEvent( type, id ) | |
11032 | { | |
11033 | m_rowOrCol = rowOrCol; | |
11034 | m_x = x; | |
11035 | m_y = y; | |
11036 | m_control = control; | |
11037 | m_shift = shift; | |
11038 | m_alt = alt; | |
11039 | m_meta = meta; | |
11040 | ||
11041 | SetEventObject(obj); | |
11042 | } | |
11043 | ||
11044 | ||
11045 | IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent ) | |
11046 | ||
11047 | wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, | |
11048 | const wxGridCellCoords& topLeft, | |
11049 | const wxGridCellCoords& bottomRight, | |
11050 | bool sel, bool control, | |
11051 | bool shift, bool alt, bool meta ) | |
11052 | : wxNotifyEvent( type, id ) | |
11053 | { | |
11054 | m_topLeft = topLeft; | |
11055 | m_bottomRight = bottomRight; | |
11056 | m_selecting = sel; | |
11057 | m_control = control; | |
11058 | m_shift = shift; | |
11059 | m_alt = alt; | |
11060 | m_meta = meta; | |
11061 | ||
11062 | SetEventObject(obj); | |
11063 | } | |
11064 | ||
11065 | ||
11066 | IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent) | |
11067 | ||
11068 | wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type, | |
11069 | wxObject* obj, int row, | |
11070 | int col, wxControl* ctrl) | |
11071 | : wxCommandEvent(type, id) | |
11072 | { | |
11073 | SetEventObject(obj); | |
11074 | m_row = row; | |
11075 | m_col = col; | |
11076 | m_ctrl = ctrl; | |
11077 | } | |
11078 | ||
11079 | #endif // wxUSE_GRID |