]>
Commit | Line | Data |
---|---|---|
2796cce3 | 1 | /////////////////////////////////////////////////////////////////////////// |
faa94f3e | 2 | // Name: src/generic/grid.cpp |
f85afd4e MB |
3 | // Purpose: wxGrid and related classes |
4 | // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn) | |
d4175745 | 5 | // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios |
f85afd4e MB |
6 | // Created: 1/08/1999 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au) | |
65571936 | 9 | // Licence: wxWindows licence |
f85afd4e MB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
bec70262 VZ |
12 | /* |
13 | TODO: | |
14 | ||
15 | - Replace use of wxINVERT with wxOverlay | |
16 | - Make Begin/EndBatch() the same as the generic Freeze/Thaw() | |
10a4531d VZ |
17 | - Review the column reordering code, it's a mess. |
18 | - Implement row reordering after dealing with the columns. | |
bec70262 VZ |
19 | */ |
20 | ||
95427194 | 21 | // For compilers that support precompilation, includes "wx/wx.h". |
4d85bcd1 JS |
22 | #include "wx/wxprec.h" |
23 | ||
f85afd4e MB |
24 | #ifdef __BORLANDC__ |
25 | #pragma hdrstop | |
26 | #endif | |
27 | ||
27b92ca4 VZ |
28 | #if wxUSE_GRID |
29 | ||
4c44eb66 PC |
30 | #include "wx/grid.h" |
31 | ||
f85afd4e MB |
32 | #ifndef WX_PRECOMP |
33 | #include "wx/utils.h" | |
34 | #include "wx/dcclient.h" | |
35 | #include "wx/settings.h" | |
36 | #include "wx/log.h" | |
508011ce VZ |
37 | #include "wx/textctrl.h" |
38 | #include "wx/checkbox.h" | |
4ee5fc9c | 39 | #include "wx/combobox.h" |
816be743 | 40 | #include "wx/valtext.h" |
60d876f3 | 41 | #include "wx/intl.h" |
c77a6796 | 42 | #include "wx/math.h" |
2a673eb1 | 43 | #include "wx/listbox.h" |
f85afd4e MB |
44 | #endif |
45 | ||
cb5df486 | 46 | #include "wx/textfile.h" |
816be743 | 47 | #include "wx/spinctrl.h" |
c4608a8a | 48 | #include "wx/tokenzr.h" |
4d1bc39c | 49 | #include "wx/renderer.h" |
ad805b9e | 50 | #include "wx/headerctrl.h" |
82edfbe7 | 51 | #include "wx/hashset.h" |
6d004f67 | 52 | |
b5808881 | 53 | #include "wx/generic/gridsel.h" |
29efc6e4 FM |
54 | #include "wx/generic/gridctrl.h" |
55 | #include "wx/generic/grideditors.h" | |
56 | #include "wx/generic/private/grid.h" | |
07296f0b | 57 | |
23318a53 | 58 | const char wxGridNameStr[] = "grid"; |
4c44eb66 | 59 | |
0b7e6e7d SN |
60 | #if defined(__WXMOTIF__) |
61 | #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier) | |
c78b3acd | 62 | #else |
0b7e6e7d | 63 | #define WXUNUSED_MOTIF(identifier) identifier |
c78b3acd SN |
64 | #endif |
65 | ||
66 | #if defined(__WXGTK__) | |
67 | #define WXUNUSED_GTK(identifier) WXUNUSED(identifier) | |
68 | #else | |
69 | #define WXUNUSED_GTK(identifier) identifier | |
70 | #endif | |
71 | ||
3f8e5072 JS |
72 | // Required for wxIs... functions |
73 | #include <ctype.h> | |
74 | ||
7a7fa93b | 75 | WX_DECLARE_HASH_SET_WITH_DECL_PTR(int, wxIntegerHash, wxIntegerEqual, |
e4c8592e | 76 | wxGridFixedIndicesSet, class WXDLLIMPEXP_ADV); |
82edfbe7 | 77 | |
29efc6e4 | 78 | |
b99be8fb | 79 | // ---------------------------------------------------------------------------- |
29efc6e4 | 80 | // globals |
b99be8fb VZ |
81 | // ---------------------------------------------------------------------------- |
82 | ||
670d0177 VZ |
83 | namespace |
84 | { | |
85 | ||
29efc6e4 FM |
86 | //#define DEBUG_ATTR_CACHE |
87 | #ifdef DEBUG_ATTR_CACHE | |
88 | static size_t gs_nAttrCacheHits = 0; | |
89 | static size_t gs_nAttrCacheMisses = 0; | |
90 | #endif | |
758cbedf | 91 | |
670d0177 VZ |
92 | // this struct simply combines together the default header renderers |
93 | // | |
94 | // as the renderers ctors are trivial, there is no problem with making them | |
95 | // globals | |
96 | struct DefaultHeaderRenderers | |
97 | { | |
98 | wxGridColumnHeaderRendererDefault colRenderer; | |
99 | wxGridRowHeaderRendererDefault rowRenderer; | |
100 | wxGridCornerHeaderRendererDefault cornerRenderer; | |
101 | } gs_defaultHeaderRenderers; | |
102 | ||
103 | } // anonymous namespace | |
104 | ||
29efc6e4 FM |
105 | // ---------------------------------------------------------------------------- |
106 | // constants | |
107 | // ---------------------------------------------------------------------------- | |
6f292345 | 108 | |
29efc6e4 FM |
109 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); |
110 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); | |
6f292345 | 111 | |
29efc6e4 FM |
112 | namespace |
113 | { | |
b99be8fb | 114 | |
29efc6e4 FM |
115 | // scroll line size |
116 | const size_t GRID_SCROLL_LINE_X = 15; | |
117 | const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X; | |
96ca74cd | 118 | |
29efc6e4 FM |
119 | // the size of hash tables used a bit everywhere (the max number of elements |
120 | // in these hash tables is the number of rows/columns) | |
121 | const int GRID_HASH_SIZE = 100; | |
2e9a6788 | 122 | |
29efc6e4 FM |
123 | // the minimal distance in pixels the mouse needs to move to start a drag |
124 | // operation | |
125 | const int DRAG_SENSITIVITY = 3; | |
b99be8fb | 126 | |
29efc6e4 | 127 | } // anonymous namespace |
b99be8fb VZ |
128 | |
129 | #include "wx/arrimpl.cpp" | |
130 | ||
131 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) | |
132 | WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray) | |
133 | ||
0f442030 RR |
134 | // ---------------------------------------------------------------------------- |
135 | // events | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
9b11752c VZ |
138 | wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK, wxGridEvent ); |
139 | wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK, wxGridEvent ); | |
140 | wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK, wxGridEvent ); | |
141 | wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK, wxGridEvent ); | |
142 | wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG, wxGridEvent ); | |
143 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK, wxGridEvent ); | |
144 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK, wxGridEvent ); | |
145 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK, wxGridEvent ); | |
146 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK, wxGridEvent ); | |
147 | wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE, wxGridSizeEvent ); | |
148 | wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE, wxGridSizeEvent ); | |
6f58f3d7 | 149 | wxDEFINE_EVENT( wxEVT_GRID_COL_AUTO_SIZE, wxGridSizeEvent ); |
9b11752c VZ |
150 | wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE, wxGridEvent ); |
151 | wxDEFINE_EVENT( wxEVT_GRID_COL_SORT, wxGridEvent ); | |
152 | wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT, wxGridRangeSelectEvent ); | |
153 | wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING, wxGridEvent ); | |
154 | wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED, wxGridEvent ); | |
155 | wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL, wxGridEvent ); | |
156 | wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN, wxGridEvent ); | |
157 | wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN, wxGridEvent ); | |
158 | wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED, wxGridEditorCreatedEvent ); | |
1dc17bca | 159 | wxDEFINE_EVENT( wxEVT_GRID_TABBING, wxGridEvent ); |
0b190b0f | 160 | |
db2c0468 SC |
161 | // ---------------------------------------------------------------------------- |
162 | // private helpers | |
163 | // ---------------------------------------------------------------------------- | |
164 | ||
165 | namespace | |
166 | { | |
167 | ||
168 | // ensure that first is less or equal to second, swapping the values if | |
169 | // necessary | |
170 | void EnsureFirstLessThanSecond(int& first, int& second) | |
171 | { | |
172 | if ( first > second ) | |
173 | wxSwap(first, second); | |
174 | } | |
175 | ||
176 | } // anonymous namespace | |
177 | ||
29efc6e4 FM |
178 | // ============================================================================ |
179 | // implementation | |
180 | // ============================================================================ | |
65e4e78e | 181 | |
29efc6e4 | 182 | IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler, wxEvtHandler) |
816be743 | 183 | |
29efc6e4 FM |
184 | BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler ) |
185 | EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus ) | |
186 | EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown ) | |
187 | EVT_CHAR( wxGridCellEditorEvtHandler::OnChar ) | |
188 | END_EVENT_TABLE() | |
816be743 | 189 | |
29efc6e4 FM |
190 | BEGIN_EVENT_TABLE(wxGridHeaderCtrl, wxHeaderCtrl) |
191 | EVT_HEADER_CLICK(wxID_ANY, wxGridHeaderCtrl::OnClick) | |
4a07d706 VZ |
192 | EVT_HEADER_DCLICK(wxID_ANY, wxGridHeaderCtrl::OnDoubleClick) |
193 | EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxGridHeaderCtrl::OnRightClick) | |
816be743 | 194 | |
29efc6e4 FM |
195 | EVT_HEADER_BEGIN_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnBeginResize) |
196 | EVT_HEADER_RESIZING(wxID_ANY, wxGridHeaderCtrl::OnResizing) | |
197 | EVT_HEADER_END_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnEndResize) | |
816be743 | 198 | |
29efc6e4 FM |
199 | EVT_HEADER_BEGIN_REORDER(wxID_ANY, wxGridHeaderCtrl::OnBeginReorder) |
200 | EVT_HEADER_END_REORDER(wxID_ANY, wxGridHeaderCtrl::OnEndReorder) | |
201 | END_EVENT_TABLE() | |
816be743 | 202 | |
29efc6e4 | 203 | wxGridOperations& wxGridRowOperations::Dual() const |
65e4e78e | 204 | { |
29efc6e4 FM |
205 | static wxGridColumnOperations s_colOper; |
206 | ||
207 | return s_colOper; | |
816be743 VZ |
208 | } |
209 | ||
29efc6e4 | 210 | wxGridOperations& wxGridColumnOperations::Dual() const |
0b190b0f | 211 | { |
29efc6e4 | 212 | static wxGridRowOperations s_rowOper; |
2f024384 | 213 | |
29efc6e4 | 214 | return s_rowOper; |
0b190b0f VZ |
215 | } |
216 | ||
508011ce | 217 | // ---------------------------------------------------------------------------- |
29efc6e4 FM |
218 | // wxGridCellWorker is an (almost) empty common base class for |
219 | // wxGridCellRenderer and wxGridCellEditor managing ref counting | |
508011ce VZ |
220 | // ---------------------------------------------------------------------------- |
221 | ||
29efc6e4 | 222 | void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params)) |
65e4e78e | 223 | { |
29efc6e4 | 224 | // nothing to do |
65e4e78e VZ |
225 | } |
226 | ||
29efc6e4 | 227 | wxGridCellWorker::~wxGridCellWorker() |
65e4e78e | 228 | { |
508011ce VZ |
229 | } |
230 | ||
ba9574c3 VZ |
231 | // ---------------------------------------------------------------------------- |
232 | // wxGridHeaderLabelsRenderer and related classes | |
233 | // ---------------------------------------------------------------------------- | |
234 | ||
235 | void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid& grid, | |
236 | wxDC& dc, | |
237 | const wxString& value, | |
238 | const wxRect& rect, | |
239 | int horizAlign, | |
240 | int vertAlign, | |
241 | int textOrientation) const | |
242 | { | |
243 | dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); | |
244 | dc.SetTextForeground(grid.GetLabelTextColour()); | |
245 | dc.SetFont(grid.GetLabelFont()); | |
246 | grid.DrawTextRectangle(dc, value, rect, horizAlign, vertAlign, textOrientation); | |
247 | } | |
248 | ||
249 | ||
250 | void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid), | |
251 | wxDC& dc, | |
252 | wxRect& rect) const | |
253 | { | |
254 | dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW))); | |
255 | dc.DrawLine(rect.GetRight(), rect.GetTop(), | |
256 | rect.GetRight(), rect.GetBottom()); | |
257 | dc.DrawLine(rect.GetLeft(), rect.GetTop(), | |
258 | rect.GetLeft(), rect.GetBottom()); | |
259 | dc.DrawLine(rect.GetLeft(), rect.GetBottom(), | |
260 | rect.GetRight() + 1, rect.GetBottom()); | |
261 | ||
262 | dc.SetPen(*wxWHITE_PEN); | |
263 | dc.DrawLine(rect.GetLeft() + 1, rect.GetTop(), | |
264 | rect.GetLeft() + 1, rect.GetBottom()); | |
265 | dc.DrawLine(rect.GetLeft() + 1, rect.GetTop(), | |
266 | rect.GetRight(), rect.GetTop()); | |
267 | ||
268 | rect.Deflate(2); | |
269 | } | |
270 | ||
271 | void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid), | |
272 | wxDC& dc, | |
273 | wxRect& rect) const | |
274 | { | |
275 | dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW))); | |
276 | dc.DrawLine(rect.GetRight(), rect.GetTop(), | |
277 | rect.GetRight(), rect.GetBottom()); | |
278 | dc.DrawLine(rect.GetLeft(), rect.GetTop(), | |
279 | rect.GetRight(), rect.GetTop()); | |
280 | dc.DrawLine(rect.GetLeft(), rect.GetBottom(), | |
281 | rect.GetRight() + 1, rect.GetBottom()); | |
282 | ||
283 | dc.SetPen(*wxWHITE_PEN); | |
284 | dc.DrawLine(rect.GetLeft(), rect.GetTop() + 1, | |
285 | rect.GetLeft(), rect.GetBottom()); | |
286 | dc.DrawLine(rect.GetLeft(), rect.GetTop() + 1, | |
287 | rect.GetRight(), rect.GetTop() + 1); | |
288 | ||
289 | rect.Deflate(2); | |
290 | } | |
291 | ||
292 | void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid), | |
293 | wxDC& dc, | |
294 | wxRect& rect) const | |
295 | { | |
296 | dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW))); | |
297 | dc.DrawLine(rect.GetRight() - 1, rect.GetBottom() - 1, | |
298 | rect.GetRight() - 1, rect.GetTop()); | |
299 | dc.DrawLine(rect.GetRight() - 1, rect.GetBottom() - 1, | |
300 | rect.GetLeft(), rect.GetBottom() - 1); | |
301 | dc.DrawLine(rect.GetLeft(), rect.GetTop(), | |
302 | rect.GetRight(), rect.GetTop()); | |
303 | dc.DrawLine(rect.GetLeft(), rect.GetTop(), | |
304 | rect.GetLeft(), rect.GetBottom()); | |
305 | ||
306 | dc.SetPen(*wxWHITE_PEN); | |
307 | dc.DrawLine(rect.GetLeft() + 1, rect.GetTop() + 1, | |
308 | rect.GetRight() - 1, rect.GetTop() + 1); | |
309 | dc.DrawLine(rect.GetLeft() + 1, rect.GetTop() + 1, | |
310 | rect.GetLeft() + 1, rect.GetBottom() - 1); | |
311 | ||
312 | rect.Deflate(2); | |
313 | } | |
314 | ||
2796cce3 RD |
315 | // ---------------------------------------------------------------------------- |
316 | // wxGridCellAttr | |
317 | // ---------------------------------------------------------------------------- | |
318 | ||
1df4050d VZ |
319 | void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) |
320 | { | |
1df4050d VZ |
321 | m_isReadOnly = Unset; |
322 | ||
323 | m_renderer = NULL; | |
324 | m_editor = NULL; | |
325 | ||
326 | m_attrkind = wxGridCellAttr::Cell; | |
327 | ||
27f35b66 | 328 | m_sizeRows = m_sizeCols = 1; |
b63fce94 | 329 | m_overflow = UnsetOverflow; |
27f35b66 | 330 | |
1df4050d VZ |
331 | SetDefAttr(attrDefault); |
332 | } | |
333 | ||
39bcce60 | 334 | wxGridCellAttr *wxGridCellAttr::Clone() const |
a68c1246 | 335 | { |
1df4050d VZ |
336 | wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr); |
337 | ||
a68c1246 VZ |
338 | if ( HasTextColour() ) |
339 | attr->SetTextColour(GetTextColour()); | |
340 | if ( HasBackgroundColour() ) | |
341 | attr->SetBackgroundColour(GetBackgroundColour()); | |
342 | if ( HasFont() ) | |
343 | attr->SetFont(GetFont()); | |
344 | if ( HasAlignment() ) | |
345 | attr->SetAlignment(m_hAlign, m_vAlign); | |
346 | ||
27f35b66 SN |
347 | attr->SetSize( m_sizeRows, m_sizeCols ); |
348 | ||
a68c1246 VZ |
349 | if ( m_renderer ) |
350 | { | |
351 | attr->SetRenderer(m_renderer); | |
39bcce60 | 352 | m_renderer->IncRef(); |
a68c1246 VZ |
353 | } |
354 | if ( m_editor ) | |
355 | { | |
356 | attr->SetEditor(m_editor); | |
39bcce60 | 357 | m_editor->IncRef(); |
a68c1246 VZ |
358 | } |
359 | ||
360 | if ( IsReadOnly() ) | |
361 | attr->SetReadOnly(); | |
362 | ||
dfd7c082 | 363 | attr->SetOverflow( m_overflow == Overflow ); |
19d7140e VZ |
364 | attr->SetKind( m_attrkind ); |
365 | ||
a68c1246 VZ |
366 | return attr; |
367 | } | |
368 | ||
19d7140e VZ |
369 | void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) |
370 | { | |
371 | if ( !HasTextColour() && mergefrom->HasTextColour() ) | |
372 | SetTextColour(mergefrom->GetTextColour()); | |
373 | if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() ) | |
374 | SetBackgroundColour(mergefrom->GetBackgroundColour()); | |
375 | if ( !HasFont() && mergefrom->HasFont() ) | |
376 | SetFont(mergefrom->GetFont()); | |
4db6714b KH |
377 | if ( !HasAlignment() && mergefrom->HasAlignment() ) |
378 | { | |
19d7140e VZ |
379 | int hAlign, vAlign; |
380 | mergefrom->GetAlignment( &hAlign, &vAlign); | |
381 | SetAlignment(hAlign, vAlign); | |
382 | } | |
3100c3db RD |
383 | if ( !HasSize() && mergefrom->HasSize() ) |
384 | mergefrom->GetSize( &m_sizeRows, &m_sizeCols ); | |
27f35b66 | 385 | |
19d7140e VZ |
386 | // Directly access member functions as GetRender/Editor don't just return |
387 | // m_renderer/m_editor | |
388 | // | |
389 | // Maybe add support for merge of Render and Editor? | |
390 | if (!HasRenderer() && mergefrom->HasRenderer() ) | |
bf7945ce | 391 | { |
19d7140e VZ |
392 | m_renderer = mergefrom->m_renderer; |
393 | m_renderer->IncRef(); | |
394 | } | |
395 | if ( !HasEditor() && mergefrom->HasEditor() ) | |
396 | { | |
397 | m_editor = mergefrom->m_editor; | |
398 | m_editor->IncRef(); | |
399 | } | |
2f024384 | 400 | if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) |
19d7140e VZ |
401 | SetReadOnly(mergefrom->IsReadOnly()); |
402 | ||
2f024384 | 403 | if (!HasOverflowMode() && mergefrom->HasOverflowMode() ) |
ff699386 | 404 | SetOverflow(mergefrom->GetOverflow()); |
ef5df12b | 405 | |
19d7140e VZ |
406 | SetDefAttr(mergefrom->m_defGridAttr); |
407 | } | |
408 | ||
27f35b66 SN |
409 | void wxGridCellAttr::SetSize(int num_rows, int num_cols) |
410 | { | |
411 | // The size of a cell is normally 1,1 | |
412 | ||
413 | // If this cell is larger (2,2) then this is the top left cell | |
414 | // the other cells that will be covered (lower right cells) must be | |
415 | // set to negative or zero values such that | |
416 | // row + num_rows of the covered cell points to the larger cell (this cell) | |
417 | // same goes for the col + num_cols. | |
418 | ||
419 | // Size of 0,0 is NOT valid, neither is <=0 and any positive value | |
420 | ||
2f024384 DS |
421 | wxASSERT_MSG( (!((num_rows > 0) && (num_cols <= 0)) || |
422 | !((num_rows <= 0) && (num_cols > 0)) || | |
423 | !((num_rows == 0) && (num_cols == 0))), | |
e75e8815 | 424 | wxT("wxGridCellAttr::SetSize only takes two positive values or negative/zero values")); |
27f35b66 SN |
425 | |
426 | m_sizeRows = num_rows; | |
427 | m_sizeCols = num_cols; | |
428 | } | |
429 | ||
2796cce3 RD |
430 | const wxColour& wxGridCellAttr::GetTextColour() const |
431 | { | |
432 | if (HasTextColour()) | |
508011ce | 433 | { |
2796cce3 | 434 | return m_colText; |
508011ce | 435 | } |
0926b2fc | 436 | else if (m_defGridAttr && m_defGridAttr != this) |
508011ce | 437 | { |
2796cce3 | 438 | return m_defGridAttr->GetTextColour(); |
508011ce VZ |
439 | } |
440 | else | |
441 | { | |
2796cce3 RD |
442 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
443 | return wxNullColour; | |
444 | } | |
445 | } | |
446 | ||
2796cce3 RD |
447 | const wxColour& wxGridCellAttr::GetBackgroundColour() const |
448 | { | |
449 | if (HasBackgroundColour()) | |
2f024384 | 450 | { |
2796cce3 | 451 | return m_colBack; |
2f024384 | 452 | } |
0926b2fc | 453 | else if (m_defGridAttr && m_defGridAttr != this) |
2f024384 | 454 | { |
2796cce3 | 455 | return m_defGridAttr->GetBackgroundColour(); |
2f024384 | 456 | } |
508011ce VZ |
457 | else |
458 | { | |
2796cce3 RD |
459 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
460 | return wxNullColour; | |
461 | } | |
462 | } | |
463 | ||
2796cce3 RD |
464 | const wxFont& wxGridCellAttr::GetFont() const |
465 | { | |
466 | if (HasFont()) | |
2f024384 | 467 | { |
2796cce3 | 468 | return m_font; |
2f024384 | 469 | } |
0926b2fc | 470 | else if (m_defGridAttr && m_defGridAttr != this) |
2f024384 | 471 | { |
2796cce3 | 472 | return m_defGridAttr->GetFont(); |
2f024384 | 473 | } |
508011ce VZ |
474 | else |
475 | { | |
2796cce3 RD |
476 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
477 | return wxNullFont; | |
478 | } | |
479 | } | |
480 | ||
2796cce3 RD |
481 | void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const |
482 | { | |
508011ce VZ |
483 | if (HasAlignment()) |
484 | { | |
4db6714b KH |
485 | if ( hAlign ) |
486 | *hAlign = m_hAlign; | |
487 | if ( vAlign ) | |
488 | *vAlign = m_vAlign; | |
2796cce3 | 489 | } |
0926b2fc | 490 | else if (m_defGridAttr && m_defGridAttr != this) |
c2f5b920 | 491 | { |
2796cce3 | 492 | m_defGridAttr->GetAlignment(hAlign, vAlign); |
c2f5b920 | 493 | } |
508011ce VZ |
494 | else |
495 | { | |
2796cce3 RD |
496 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
497 | } | |
498 | } | |
499 | ||
cfbc15ee VZ |
500 | void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign, int *vAlign) const |
501 | { | |
502 | if ( hAlign && m_hAlign != wxALIGN_INVALID ) | |
503 | *hAlign = m_hAlign; | |
504 | ||
505 | if ( vAlign && m_vAlign != wxALIGN_INVALID ) | |
506 | *vAlign = m_vAlign; | |
507 | } | |
508 | ||
27f35b66 SN |
509 | void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const |
510 | { | |
4db6714b KH |
511 | if ( num_rows ) |
512 | *num_rows = m_sizeRows; | |
513 | if ( num_cols ) | |
514 | *num_cols = m_sizeCols; | |
27f35b66 | 515 | } |
2796cce3 | 516 | |
f2d76237 | 517 | // GetRenderer and GetEditor use a slightly different decision path about |
28a77bc4 RD |
518 | // which attribute to use. If a non-default attr object has one then it is |
519 | // used, otherwise the default editor or renderer is fetched from the grid and | |
520 | // used. It should be the default for the data type of the cell. If it is | |
521 | // NULL (because the table has a type that the grid does not have in its | |
c2f5b920 | 522 | // registry), then the grid's default editor or renderer is used. |
28a77bc4 | 523 | |
ef316e23 | 524 | wxGridCellRenderer* wxGridCellAttr::GetRenderer(const wxGrid* grid, int row, int col) const |
28a77bc4 | 525 | { |
c2f5b920 | 526 | wxGridCellRenderer *renderer = NULL; |
28a77bc4 | 527 | |
3cf883a2 | 528 | if ( m_renderer && this != m_defGridAttr ) |
0b190b0f | 529 | { |
3cf883a2 VZ |
530 | // use the cells renderer if it has one |
531 | renderer = m_renderer; | |
532 | renderer->IncRef(); | |
0b190b0f | 533 | } |
2f024384 | 534 | else // no non-default cell renderer |
0b190b0f | 535 | { |
3cf883a2 VZ |
536 | // get default renderer for the data type |
537 | if ( grid ) | |
538 | { | |
539 | // GetDefaultRendererForCell() will do IncRef() for us | |
540 | renderer = grid->GetDefaultRendererForCell(row, col); | |
541 | } | |
0b190b0f | 542 | |
c2f5b920 | 543 | if ( renderer == NULL ) |
3cf883a2 | 544 | { |
c2f5b920 | 545 | if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) ) |
3cf883a2 VZ |
546 | { |
547 | // if we still don't have one then use the grid default | |
548 | // (no need for IncRef() here neither) | |
549 | renderer = m_defGridAttr->GetRenderer(NULL, 0, 0); | |
550 | } | |
551 | else // default grid attr | |
552 | { | |
553 | // use m_renderer which we had decided not to use initially | |
554 | renderer = m_renderer; | |
555 | if ( renderer ) | |
556 | renderer->IncRef(); | |
557 | } | |
558 | } | |
0b190b0f | 559 | } |
28a77bc4 | 560 | |
3cf883a2 VZ |
561 | // we're supposed to always find something |
562 | wxASSERT_MSG(renderer, wxT("Missing default cell renderer")); | |
28a77bc4 RD |
563 | |
564 | return renderer; | |
2796cce3 RD |
565 | } |
566 | ||
3cf883a2 | 567 | // same as above, except for s/renderer/editor/g |
ef316e23 | 568 | wxGridCellEditor* wxGridCellAttr::GetEditor(const wxGrid* grid, int row, int col) const |
07296f0b | 569 | { |
c2f5b920 | 570 | wxGridCellEditor *editor = NULL; |
0b190b0f | 571 | |
3cf883a2 | 572 | if ( m_editor && this != m_defGridAttr ) |
0b190b0f | 573 | { |
3cf883a2 VZ |
574 | // use the cells editor if it has one |
575 | editor = m_editor; | |
576 | editor->IncRef(); | |
0b190b0f | 577 | } |
3cf883a2 | 578 | else // no non default cell editor |
0b190b0f | 579 | { |
3cf883a2 VZ |
580 | // get default editor for the data type |
581 | if ( grid ) | |
582 | { | |
583 | // GetDefaultEditorForCell() will do IncRef() for us | |
584 | editor = grid->GetDefaultEditorForCell(row, col); | |
585 | } | |
3cf883a2 | 586 | |
c2f5b920 | 587 | if ( editor == NULL ) |
3cf883a2 | 588 | { |
c2f5b920 | 589 | if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) ) |
3cf883a2 VZ |
590 | { |
591 | // if we still don't have one then use the grid default | |
592 | // (no need for IncRef() here neither) | |
593 | editor = m_defGridAttr->GetEditor(NULL, 0, 0); | |
594 | } | |
595 | else // default grid attr | |
596 | { | |
597 | // use m_editor which we had decided not to use initially | |
598 | editor = m_editor; | |
599 | if ( editor ) | |
600 | editor->IncRef(); | |
601 | } | |
602 | } | |
0b190b0f | 603 | } |
28a77bc4 | 604 | |
3cf883a2 VZ |
605 | // we're supposed to always find something |
606 | wxASSERT_MSG(editor, wxT("Missing default cell editor")); | |
607 | ||
28a77bc4 | 608 | return editor; |
07296f0b RD |
609 | } |
610 | ||
b99be8fb | 611 | // ---------------------------------------------------------------------------- |
758cbedf | 612 | // wxGridCellAttrData |
b99be8fb VZ |
613 | // ---------------------------------------------------------------------------- |
614 | ||
758cbedf | 615 | void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) |
b99be8fb | 616 | { |
96ca74cd SN |
617 | // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not |
618 | // touch attribute's reference counting explicitly, since this | |
619 | // is managed by class wxGridCellWithAttr | |
b99be8fb VZ |
620 | int n = FindIndex(row, col); |
621 | if ( n == wxNOT_FOUND ) | |
622 | { | |
a70517e9 VZ |
623 | if ( attr ) |
624 | { | |
625 | // add the attribute | |
626 | m_attrs.Add(new wxGridCellWithAttr(row, col, attr)); | |
627 | } | |
628 | //else: nothing to do | |
b99be8fb | 629 | } |
6f292345 | 630 | else // we already have an attribute for this cell |
b99be8fb VZ |
631 | { |
632 | if ( attr ) | |
633 | { | |
634 | // change the attribute | |
96ca74cd | 635 | m_attrs[(size_t)n].ChangeAttr(attr); |
b99be8fb VZ |
636 | } |
637 | else | |
638 | { | |
639 | // remove this attribute | |
640 | m_attrs.RemoveAt((size_t)n); | |
641 | } | |
642 | } | |
b99be8fb VZ |
643 | } |
644 | ||
758cbedf | 645 | wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const |
b99be8fb | 646 | { |
10a4531d | 647 | wxGridCellAttr *attr = NULL; |
b99be8fb VZ |
648 | |
649 | int n = FindIndex(row, col); | |
650 | if ( n != wxNOT_FOUND ) | |
651 | { | |
2e9a6788 VZ |
652 | attr = m_attrs[(size_t)n].attr; |
653 | attr->IncRef(); | |
b99be8fb VZ |
654 | } |
655 | ||
656 | return attr; | |
657 | } | |
658 | ||
4d60017a SN |
659 | void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) |
660 | { | |
661 | size_t count = m_attrs.GetCount(); | |
662 | for ( size_t n = 0; n < count; n++ ) | |
663 | { | |
664 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
665 | wxCoord row = coords.GetRow(); |
666 | if ((size_t)row >= pos) | |
667 | { | |
668 | if (numRows > 0) | |
669 | { | |
670 | // If rows inserted, include row counter where necessary | |
671 | coords.SetRow(row + numRows); | |
672 | } | |
673 | else if (numRows < 0) | |
674 | { | |
675 | // If rows deleted ... | |
676 | if ((size_t)row >= pos - numRows) | |
677 | { | |
678 | // ...either decrement row counter (if row still exists)... | |
679 | coords.SetRow(row + numRows); | |
680 | } | |
681 | else | |
682 | { | |
683 | // ...or remove the attribute | |
01dd42b6 | 684 | m_attrs.RemoveAt(n); |
4db6714b KH |
685 | n--; |
686 | count--; | |
d1c0b4f9 VZ |
687 | } |
688 | } | |
4d60017a SN |
689 | } |
690 | } | |
691 | } | |
692 | ||
693 | void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) | |
694 | { | |
695 | size_t count = m_attrs.GetCount(); | |
696 | for ( size_t n = 0; n < count; n++ ) | |
697 | { | |
698 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
699 | wxCoord col = coords.GetCol(); |
700 | if ( (size_t)col >= pos ) | |
701 | { | |
702 | if ( numCols > 0 ) | |
703 | { | |
704 | // If rows inserted, include row counter where necessary | |
705 | coords.SetCol(col + numCols); | |
706 | } | |
707 | else if (numCols < 0) | |
708 | { | |
709 | // If rows deleted ... | |
710 | if ((size_t)col >= pos - numCols) | |
711 | { | |
712 | // ...either decrement row counter (if row still exists)... | |
713 | coords.SetCol(col + numCols); | |
714 | } | |
715 | else | |
716 | { | |
717 | // ...or remove the attribute | |
01dd42b6 | 718 | m_attrs.RemoveAt(n); |
2f024384 DS |
719 | n--; |
720 | count--; | |
d1c0b4f9 VZ |
721 | } |
722 | } | |
4d60017a SN |
723 | } |
724 | } | |
725 | } | |
726 | ||
758cbedf | 727 | int wxGridCellAttrData::FindIndex(int row, int col) const |
b99be8fb VZ |
728 | { |
729 | size_t count = m_attrs.GetCount(); | |
730 | for ( size_t n = 0; n < count; n++ ) | |
731 | { | |
732 | const wxGridCellCoords& coords = m_attrs[n].coords; | |
733 | if ( (coords.GetRow() == row) && (coords.GetCol() == col) ) | |
734 | { | |
735 | return n; | |
736 | } | |
737 | } | |
738 | ||
739 | return wxNOT_FOUND; | |
740 | } | |
741 | ||
758cbedf VZ |
742 | // ---------------------------------------------------------------------------- |
743 | // wxGridRowOrColAttrData | |
744 | // ---------------------------------------------------------------------------- | |
745 | ||
746 | wxGridRowOrColAttrData::~wxGridRowOrColAttrData() | |
747 | { | |
b4a980f4 | 748 | size_t count = m_attrs.GetCount(); |
758cbedf VZ |
749 | for ( size_t n = 0; n < count; n++ ) |
750 | { | |
751 | m_attrs[n]->DecRef(); | |
752 | } | |
753 | } | |
754 | ||
755 | wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const | |
756 | { | |
10a4531d | 757 | wxGridCellAttr *attr = NULL; |
758cbedf VZ |
758 | |
759 | int n = m_rowsOrCols.Index(rowOrCol); | |
760 | if ( n != wxNOT_FOUND ) | |
761 | { | |
762 | attr = m_attrs[(size_t)n]; | |
763 | attr->IncRef(); | |
764 | } | |
765 | ||
766 | return attr; | |
767 | } | |
768 | ||
769 | void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) | |
770 | { | |
a95e38c0 VZ |
771 | int i = m_rowsOrCols.Index(rowOrCol); |
772 | if ( i == wxNOT_FOUND ) | |
758cbedf | 773 | { |
62d249cb VZ |
774 | if ( attr ) |
775 | { | |
4a53f701 | 776 | // store the new attribute, taking its ownership |
62d249cb VZ |
777 | m_rowsOrCols.Add(rowOrCol); |
778 | m_attrs.Add(attr); | |
779 | } | |
780 | // nothing to remove | |
758cbedf | 781 | } |
4a53f701 | 782 | else // we have an attribute for this row or column |
758cbedf | 783 | { |
a95e38c0 | 784 | size_t n = (size_t)i; |
4a53f701 VZ |
785 | |
786 | // notice that this code works correctly even when the old attribute is | |
787 | // the same as the new one: as we own of it, we must call DecRef() on | |
788 | // it in any case and this won't result in destruction of the new | |
789 | // attribute if it's the same as old one because it must have ref count | |
790 | // of at least 2 to be passed to us while we keep a reference to it too | |
791 | m_attrs[n]->DecRef(); | |
792 | ||
758cbedf VZ |
793 | if ( attr ) |
794 | { | |
4a53f701 | 795 | // replace the attribute with the new one |
a95e38c0 | 796 | m_attrs[n] = attr; |
758cbedf | 797 | } |
4a53f701 | 798 | else // remove the attribute |
758cbedf | 799 | { |
a95e38c0 VZ |
800 | m_rowsOrCols.RemoveAt(n); |
801 | m_attrs.RemoveAt(n); | |
758cbedf VZ |
802 | } |
803 | } | |
804 | } | |
805 | ||
4d60017a SN |
806 | void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) |
807 | { | |
808 | size_t count = m_attrs.GetCount(); | |
809 | for ( size_t n = 0; n < count; n++ ) | |
810 | { | |
811 | int & rowOrCol = m_rowsOrCols[n]; | |
d1c0b4f9 VZ |
812 | if ( (size_t)rowOrCol >= pos ) |
813 | { | |
814 | if ( numRowsOrCols > 0 ) | |
815 | { | |
816 | // If rows inserted, include row counter where necessary | |
817 | rowOrCol += numRowsOrCols; | |
818 | } | |
819 | else if ( numRowsOrCols < 0) | |
820 | { | |
821 | // If rows deleted, either decrement row counter (if row still exists) | |
822 | if ((size_t)rowOrCol >= pos - numRowsOrCols) | |
823 | rowOrCol += numRowsOrCols; | |
824 | else | |
825 | { | |
01dd42b6 VZ |
826 | m_rowsOrCols.RemoveAt(n); |
827 | m_attrs[n]->DecRef(); | |
828 | m_attrs.RemoveAt(n); | |
4db6714b KH |
829 | n--; |
830 | count--; | |
d1c0b4f9 VZ |
831 | } |
832 | } | |
4d60017a SN |
833 | } |
834 | } | |
835 | } | |
836 | ||
b99be8fb VZ |
837 | // ---------------------------------------------------------------------------- |
838 | // wxGridCellAttrProvider | |
839 | // ---------------------------------------------------------------------------- | |
840 | ||
841 | wxGridCellAttrProvider::wxGridCellAttrProvider() | |
842 | { | |
10a4531d | 843 | m_data = NULL; |
b99be8fb VZ |
844 | } |
845 | ||
846 | wxGridCellAttrProvider::~wxGridCellAttrProvider() | |
847 | { | |
848 | delete m_data; | |
849 | } | |
850 | ||
851 | void wxGridCellAttrProvider::InitData() | |
852 | { | |
853 | m_data = new wxGridCellAttrProviderData; | |
854 | } | |
855 | ||
19d7140e VZ |
856 | wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col, |
857 | wxGridCellAttr::wxAttrKind kind ) const | |
b99be8fb | 858 | { |
10a4531d | 859 | wxGridCellAttr *attr = NULL; |
758cbedf VZ |
860 | if ( m_data ) |
861 | { | |
962a48f6 | 862 | switch (kind) |
758cbedf | 863 | { |
19d7140e | 864 | case (wxGridCellAttr::Any): |
962a48f6 DS |
865 | // Get cached merge attributes. |
866 | // Currently not used as no cache implemented as not mutable | |
19d7140e | 867 | // attr = m_data->m_mergeAttr.GetAttr(row, col); |
4db6714b | 868 | if (!attr) |
19d7140e | 869 | { |
962a48f6 DS |
870 | // Basically implement old version. |
871 | // Also check merge cache, so we don't have to re-merge every time.. | |
999836aa VZ |
872 | wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col); |
873 | wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row); | |
874 | wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col); | |
19d7140e | 875 | |
4db6714b KH |
876 | if ((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol)) |
877 | { | |
2d0c2e79 | 878 | // Two or more are non NULL |
19d7140e VZ |
879 | attr = new wxGridCellAttr; |
880 | attr->SetKind(wxGridCellAttr::Merged); | |
881 | ||
962a48f6 | 882 | // Order is important.. |
4db6714b KH |
883 | if (attrcell) |
884 | { | |
19d7140e VZ |
885 | attr->MergeWith(attrcell); |
886 | attrcell->DecRef(); | |
887 | } | |
4db6714b KH |
888 | if (attrcol) |
889 | { | |
19d7140e VZ |
890 | attr->MergeWith(attrcol); |
891 | attrcol->DecRef(); | |
892 | } | |
4db6714b KH |
893 | if (attrrow) |
894 | { | |
19d7140e VZ |
895 | attr->MergeWith(attrrow); |
896 | attrrow->DecRef(); | |
897 | } | |
962a48f6 DS |
898 | |
899 | // store merge attr if cache implemented | |
19d7140e VZ |
900 | //attr->IncRef(); |
901 | //m_data->m_mergeAttr.SetAttr(attr, row, col); | |
902 | } | |
903 | else | |
2d0c2e79 | 904 | { |
19d7140e | 905 | // one or none is non null return it or null. |
4db6714b KH |
906 | if (attrrow) |
907 | attr = attrrow; | |
908 | if (attrcol) | |
2d0c2e79 | 909 | { |
962a48f6 | 910 | if (attr) |
2d0c2e79 RD |
911 | attr->DecRef(); |
912 | attr = attrcol; | |
913 | } | |
4db6714b | 914 | if (attrcell) |
2d0c2e79 | 915 | { |
4db6714b | 916 | if (attr) |
2d0c2e79 RD |
917 | attr->DecRef(); |
918 | attr = attrcell; | |
919 | } | |
19d7140e | 920 | } |
29efc6e4 FM |
921 | } |
922 | break; | |
f2d76237 | 923 | |
29efc6e4 FM |
924 | case (wxGridCellAttr::Cell): |
925 | attr = m_data->m_cellAttrs.GetAttr(row, col); | |
926 | break; | |
927 | ||
928 | case (wxGridCellAttr::Col): | |
929 | attr = m_data->m_colAttrs.GetAttr(col); | |
930 | break; | |
931 | ||
932 | case (wxGridCellAttr::Row): | |
933 | attr = m_data->m_rowAttrs.GetAttr(row); | |
934 | break; | |
935 | ||
936 | default: | |
937 | // unused as yet... | |
938 | // (wxGridCellAttr::Default): | |
939 | // (wxGridCellAttr::Merged): | |
940 | break; | |
941 | } | |
c4608a8a VZ |
942 | } |
943 | ||
29efc6e4 | 944 | return attr; |
c4608a8a VZ |
945 | } |
946 | ||
29efc6e4 FM |
947 | void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, |
948 | int row, int col) | |
c4608a8a | 949 | { |
29efc6e4 FM |
950 | if ( !m_data ) |
951 | InitData(); | |
c4608a8a | 952 | |
29efc6e4 FM |
953 | m_data->m_cellAttrs.SetAttr(attr, row, col); |
954 | } | |
c4608a8a | 955 | |
29efc6e4 FM |
956 | void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row) |
957 | { | |
958 | if ( !m_data ) | |
959 | InitData(); | |
c4608a8a | 960 | |
29efc6e4 FM |
961 | m_data->m_rowAttrs.SetAttr(attr, row); |
962 | } | |
c4608a8a | 963 | |
29efc6e4 FM |
964 | void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) |
965 | { | |
966 | if ( !m_data ) | |
967 | InitData(); | |
f2d76237 | 968 | |
29efc6e4 | 969 | m_data->m_colAttrs.SetAttr(attr, col); |
f2d76237 RD |
970 | } |
971 | ||
29efc6e4 | 972 | void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) |
f2d76237 | 973 | { |
29efc6e4 FM |
974 | if ( m_data ) |
975 | { | |
976 | m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); | |
2f024384 | 977 | |
29efc6e4 FM |
978 | m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); |
979 | } | |
f2d76237 RD |
980 | } |
981 | ||
29efc6e4 | 982 | void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) |
f2d76237 | 983 | { |
29efc6e4 FM |
984 | if ( m_data ) |
985 | { | |
986 | m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); | |
2f024384 | 987 | |
29efc6e4 FM |
988 | m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); |
989 | } | |
f2d76237 RD |
990 | } |
991 | ||
670d0177 VZ |
992 | const wxGridColumnHeaderRenderer& |
993 | wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col)) | |
994 | { | |
995 | return gs_defaultHeaderRenderers.colRenderer; | |
996 | } | |
997 | ||
998 | const wxGridRowHeaderRenderer& | |
999 | wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row)) | |
1000 | { | |
1001 | return gs_defaultHeaderRenderers.rowRenderer; | |
1002 | } | |
1003 | ||
1004 | const wxGridCornerHeaderRenderer& wxGridCellAttrProvider::GetCornerRenderer() | |
1005 | { | |
1006 | return gs_defaultHeaderRenderers.cornerRenderer; | |
1007 | } | |
1008 | ||
758cbedf VZ |
1009 | // ---------------------------------------------------------------------------- |
1010 | // wxGridTableBase | |
1011 | // ---------------------------------------------------------------------------- | |
1012 | ||
f85afd4e MB |
1013 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) |
1014 | ||
f85afd4e | 1015 | wxGridTableBase::wxGridTableBase() |
f85afd4e | 1016 | { |
10a4531d VZ |
1017 | m_view = NULL; |
1018 | m_attrProvider = NULL; | |
f85afd4e MB |
1019 | } |
1020 | ||
1021 | wxGridTableBase::~wxGridTableBase() | |
1022 | { | |
b99be8fb VZ |
1023 | delete m_attrProvider; |
1024 | } | |
1025 | ||
1026 | void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider) | |
1027 | { | |
1028 | delete m_attrProvider; | |
1029 | m_attrProvider = attrProvider; | |
f85afd4e MB |
1030 | } |
1031 | ||
f2d76237 RD |
1032 | bool wxGridTableBase::CanHaveAttributes() |
1033 | { | |
1034 | if ( ! GetAttrProvider() ) | |
1035 | { | |
1036 | // use the default attr provider by default | |
1037 | SetAttrProvider(new wxGridCellAttrProvider); | |
1038 | } | |
2f024384 | 1039 | |
ca65c044 | 1040 | return true; |
f2d76237 RD |
1041 | } |
1042 | ||
19d7140e | 1043 | wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) |
b99be8fb VZ |
1044 | { |
1045 | if ( m_attrProvider ) | |
19d7140e | 1046 | return m_attrProvider->GetAttr(row, col, kind); |
b99be8fb | 1047 | else |
10a4531d | 1048 | return NULL; |
b99be8fb VZ |
1049 | } |
1050 | ||
758cbedf | 1051 | void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) |
b99be8fb VZ |
1052 | { |
1053 | if ( m_attrProvider ) | |
1054 | { | |
6f292345 VZ |
1055 | if ( attr ) |
1056 | attr->SetKind(wxGridCellAttr::Cell); | |
b99be8fb VZ |
1057 | m_attrProvider->SetAttr(attr, row, col); |
1058 | } | |
1059 | else | |
1060 | { | |
1061 | // as we take ownership of the pointer and don't store it, we must | |
1062 | // free it now | |
39bcce60 | 1063 | wxSafeDecRef(attr); |
b99be8fb VZ |
1064 | } |
1065 | } | |
1066 | ||
758cbedf VZ |
1067 | void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) |
1068 | { | |
1069 | if ( m_attrProvider ) | |
1070 | { | |
19d7140e | 1071 | attr->SetKind(wxGridCellAttr::Row); |
758cbedf VZ |
1072 | m_attrProvider->SetRowAttr(attr, row); |
1073 | } | |
1074 | else | |
1075 | { | |
1076 | // as we take ownership of the pointer and don't store it, we must | |
1077 | // free it now | |
39bcce60 | 1078 | wxSafeDecRef(attr); |
758cbedf VZ |
1079 | } |
1080 | } | |
1081 | ||
1082 | void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) | |
1083 | { | |
1084 | if ( m_attrProvider ) | |
1085 | { | |
19d7140e | 1086 | attr->SetKind(wxGridCellAttr::Col); |
758cbedf VZ |
1087 | m_attrProvider->SetColAttr(attr, col); |
1088 | } | |
1089 | else | |
1090 | { | |
1091 | // as we take ownership of the pointer and don't store it, we must | |
1092 | // free it now | |
39bcce60 | 1093 | wxSafeDecRef(attr); |
758cbedf VZ |
1094 | } |
1095 | } | |
1096 | ||
aa5e1f75 SN |
1097 | bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos), |
1098 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 1099 | { |
f6bcfd97 | 1100 | wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); |
8f177c8e | 1101 | |
ca65c044 | 1102 | return false; |
f85afd4e MB |
1103 | } |
1104 | ||
aa5e1f75 | 1105 | bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) ) |
f85afd4e | 1106 | { |
f6bcfd97 | 1107 | wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); |
8f177c8e | 1108 | |
ca65c044 | 1109 | return false; |
f85afd4e MB |
1110 | } |
1111 | ||
aa5e1f75 SN |
1112 | bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos), |
1113 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 1114 | { |
f6bcfd97 | 1115 | wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); |
8f177c8e | 1116 | |
ca65c044 | 1117 | return false; |
f85afd4e MB |
1118 | } |
1119 | ||
aa5e1f75 SN |
1120 | bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos), |
1121 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 1122 | { |
f6bcfd97 | 1123 | wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); |
8f177c8e | 1124 | |
ca65c044 | 1125 | return false; |
f85afd4e MB |
1126 | } |
1127 | ||
aa5e1f75 | 1128 | bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) ) |
f85afd4e | 1129 | { |
f6bcfd97 | 1130 | wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); |
8f177c8e | 1131 | |
ca65c044 | 1132 | return false; |
f85afd4e MB |
1133 | } |
1134 | ||
aa5e1f75 SN |
1135 | bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos), |
1136 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 1137 | { |
f6bcfd97 | 1138 | wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); |
8f177c8e | 1139 | |
ca65c044 | 1140 | return false; |
f85afd4e MB |
1141 | } |
1142 | ||
f85afd4e MB |
1143 | wxString wxGridTableBase::GetRowLabelValue( int row ) |
1144 | { | |
1145 | wxString s; | |
93763ad5 | 1146 | |
2f024384 DS |
1147 | // RD: Starting the rows at zero confuses users, |
1148 | // no matter how much it makes sense to us geeks. | |
1149 | s << row + 1; | |
1150 | ||
f85afd4e MB |
1151 | return s; |
1152 | } | |
1153 | ||
1154 | wxString wxGridTableBase::GetColLabelValue( int col ) | |
1155 | { | |
1156 | // default col labels are: | |
1157 | // cols 0 to 25 : A-Z | |
1158 | // cols 26 to 675 : AA-ZZ | |
1159 | // etc. | |
1160 | ||
1161 | wxString s; | |
1162 | unsigned int i, n; | |
1163 | for ( n = 1; ; n++ ) | |
1164 | { | |
9a83f860 | 1165 | s += (wxChar) (wxT('A') + (wxChar)(col % 26)); |
2f024384 | 1166 | col = col / 26 - 1; |
4db6714b KH |
1167 | if ( col < 0 ) |
1168 | break; | |
f85afd4e MB |
1169 | } |
1170 | ||
1171 | // reverse the string... | |
1172 | wxString s2; | |
3d59537f | 1173 | for ( i = 0; i < n; i++ ) |
f85afd4e | 1174 | { |
2f024384 | 1175 | s2 += s[n - i - 1]; |
f85afd4e MB |
1176 | } |
1177 | ||
1178 | return s2; | |
1179 | } | |
1180 | ||
f2d76237 RD |
1181 | wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) ) |
1182 | { | |
816be743 | 1183 | return wxGRID_VALUE_STRING; |
f2d76237 RD |
1184 | } |
1185 | ||
1186 | bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col), | |
1187 | const wxString& typeName ) | |
1188 | { | |
816be743 | 1189 | return typeName == wxGRID_VALUE_STRING; |
f2d76237 RD |
1190 | } |
1191 | ||
1192 | bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName ) | |
1193 | { | |
1194 | return CanGetValueAs(row, col, typeName); | |
1195 | } | |
1196 | ||
1197 | long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) ) | |
1198 | { | |
1199 | return 0; | |
1200 | } | |
1201 | ||
1202 | double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) ) | |
1203 | { | |
1204 | return 0.0; | |
1205 | } | |
1206 | ||
1207 | bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) ) | |
1208 | { | |
ca65c044 | 1209 | return false; |
f2d76237 RD |
1210 | } |
1211 | ||
1212 | void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col), | |
1213 | long WXUNUSED(value) ) | |
1214 | { | |
1215 | } | |
1216 | ||
1217 | void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col), | |
1218 | double WXUNUSED(value) ) | |
1219 | { | |
1220 | } | |
1221 | ||
1222 | void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col), | |
1223 | bool WXUNUSED(value) ) | |
1224 | { | |
1225 | } | |
1226 | ||
f2d76237 RD |
1227 | void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), |
1228 | const wxString& WXUNUSED(typeName) ) | |
1229 | { | |
1230 | return NULL; | |
1231 | } | |
1232 | ||
1233 | void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
1234 | const wxString& WXUNUSED(typeName), | |
1235 | void* WXUNUSED(value) ) | |
1236 | { | |
1237 | } | |
1238 | ||
f85afd4e MB |
1239 | ////////////////////////////////////////////////////////////////////// |
1240 | // | |
1241 | // Message class for the grid table to send requests and notifications | |
1242 | // to the grid view | |
1243 | // | |
1244 | ||
1245 | wxGridTableMessage::wxGridTableMessage() | |
1246 | { | |
10a4531d | 1247 | m_table = NULL; |
f85afd4e MB |
1248 | m_id = -1; |
1249 | m_comInt1 = -1; | |
1250 | m_comInt2 = -1; | |
1251 | } | |
1252 | ||
1253 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, | |
1254 | int commandInt1, int commandInt2 ) | |
1255 | { | |
1256 | m_table = table; | |
1257 | m_id = id; | |
1258 | m_comInt1 = commandInt1; | |
1259 | m_comInt2 = commandInt2; | |
1260 | } | |
1261 | ||
f85afd4e MB |
1262 | ////////////////////////////////////////////////////////////////////// |
1263 | // | |
1264 | // A basic grid table for string data. An object of this class will | |
1265 | // created by wxGrid if you don't specify an alternative table class. | |
1266 | // | |
1267 | ||
223d09f6 | 1268 | WX_DEFINE_OBJARRAY(wxGridStringArray) |
f85afd4e MB |
1269 | |
1270 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) | |
1271 | ||
1272 | wxGridStringTable::wxGridStringTable() | |
1273 | : wxGridTableBase() | |
1274 | { | |
5c3313a9 | 1275 | m_numCols = 0; |
f85afd4e MB |
1276 | } |
1277 | ||
1278 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) | |
1279 | : wxGridTableBase() | |
1280 | { | |
5c3313a9 VZ |
1281 | m_numCols = numCols; |
1282 | ||
f85afd4e MB |
1283 | m_data.Alloc( numRows ); |
1284 | ||
1285 | wxArrayString sa; | |
1286 | sa.Alloc( numCols ); | |
27f35b66 | 1287 | sa.Add( wxEmptyString, numCols ); |
8f177c8e | 1288 | |
27f35b66 | 1289 | m_data.Add( sa, numRows ); |
f85afd4e MB |
1290 | } |
1291 | ||
f85afd4e MB |
1292 | wxString wxGridStringTable::GetValue( int row, int col ) |
1293 | { | |
57e9abd6 VZ |
1294 | wxCHECK_MSG( (row >= 0 && row < GetNumberRows()) && |
1295 | (col >= 0 && col < GetNumberCols()), | |
3e13956a | 1296 | wxEmptyString, |
9a83f860 | 1297 | wxT("invalid row or column index in wxGridStringTable") ); |
af547d51 | 1298 | |
f85afd4e MB |
1299 | return m_data[row][col]; |
1300 | } | |
1301 | ||
f2d76237 | 1302 | void wxGridStringTable::SetValue( int row, int col, const wxString& value ) |
f85afd4e | 1303 | { |
57e9abd6 VZ |
1304 | wxCHECK_RET( (row >= 0 && row < GetNumberRows()) && |
1305 | (col >= 0 && col < GetNumberCols()), | |
9a83f860 | 1306 | wxT("invalid row or column index in wxGridStringTable") ); |
af547d51 | 1307 | |
f2d76237 | 1308 | m_data[row][col] = value; |
f85afd4e MB |
1309 | } |
1310 | ||
f85afd4e MB |
1311 | void wxGridStringTable::Clear() |
1312 | { | |
1313 | int row, col; | |
1314 | int numRows, numCols; | |
8f177c8e | 1315 | |
f85afd4e MB |
1316 | numRows = m_data.GetCount(); |
1317 | if ( numRows > 0 ) | |
1318 | { | |
1319 | numCols = m_data[0].GetCount(); | |
1320 | ||
3d59537f | 1321 | for ( row = 0; row < numRows; row++ ) |
f85afd4e | 1322 | { |
3d59537f | 1323 | for ( col = 0; col < numCols; col++ ) |
f85afd4e MB |
1324 | { |
1325 | m_data[row][col] = wxEmptyString; | |
1326 | } | |
1327 | } | |
1328 | } | |
1329 | } | |
1330 | ||
f85afd4e MB |
1331 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) |
1332 | { | |
f85afd4e | 1333 | size_t curNumRows = m_data.GetCount(); |
f6bcfd97 BP |
1334 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
1335 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 1336 | |
f85afd4e MB |
1337 | if ( pos >= curNumRows ) |
1338 | { | |
1339 | return AppendRows( numRows ); | |
1340 | } | |
8f177c8e | 1341 | |
f85afd4e MB |
1342 | wxArrayString sa; |
1343 | sa.Alloc( curNumCols ); | |
27f35b66 SN |
1344 | sa.Add( wxEmptyString, curNumCols ); |
1345 | m_data.Insert( sa, pos, numRows ); | |
2f024384 | 1346 | |
f85afd4e MB |
1347 | if ( GetView() ) |
1348 | { | |
1349 | wxGridTableMessage msg( this, | |
1350 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, | |
1351 | pos, | |
1352 | numRows ); | |
8f177c8e | 1353 | |
f85afd4e MB |
1354 | GetView()->ProcessTableMessage( msg ); |
1355 | } | |
1356 | ||
ca65c044 | 1357 | return true; |
f85afd4e MB |
1358 | } |
1359 | ||
1360 | bool wxGridStringTable::AppendRows( size_t numRows ) | |
1361 | { | |
f85afd4e | 1362 | size_t curNumRows = m_data.GetCount(); |
4db6714b KH |
1363 | size_t curNumCols = ( curNumRows > 0 |
1364 | ? m_data[0].GetCount() | |
1365 | : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 1366 | |
f85afd4e MB |
1367 | wxArrayString sa; |
1368 | if ( curNumCols > 0 ) | |
1369 | { | |
1370 | sa.Alloc( curNumCols ); | |
27f35b66 | 1371 | sa.Add( wxEmptyString, curNumCols ); |
f85afd4e | 1372 | } |
8f177c8e | 1373 | |
27f35b66 | 1374 | m_data.Add( sa, numRows ); |
f85afd4e MB |
1375 | |
1376 | if ( GetView() ) | |
1377 | { | |
1378 | wxGridTableMessage msg( this, | |
1379 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, | |
1380 | numRows ); | |
8f177c8e | 1381 | |
f85afd4e MB |
1382 | GetView()->ProcessTableMessage( msg ); |
1383 | } | |
1384 | ||
ca65c044 | 1385 | return true; |
f85afd4e MB |
1386 | } |
1387 | ||
1388 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) | |
1389 | { | |
f85afd4e | 1390 | size_t curNumRows = m_data.GetCount(); |
8f177c8e | 1391 | |
f85afd4e MB |
1392 | if ( pos >= curNumRows ) |
1393 | { | |
e91d2033 VZ |
1394 | wxFAIL_MSG( wxString::Format |
1395 | ( | |
1396 | wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), | |
1397 | (unsigned long)pos, | |
1398 | (unsigned long)numRows, | |
1399 | (unsigned long)curNumRows | |
1400 | ) ); | |
1401 | ||
ca65c044 | 1402 | return false; |
f85afd4e MB |
1403 | } |
1404 | ||
1405 | if ( numRows > curNumRows - pos ) | |
1406 | { | |
1407 | numRows = curNumRows - pos; | |
1408 | } | |
8f177c8e | 1409 | |
f85afd4e MB |
1410 | if ( numRows >= curNumRows ) |
1411 | { | |
d57ad377 | 1412 | m_data.Clear(); |
f85afd4e MB |
1413 | } |
1414 | else | |
1415 | { | |
27f35b66 | 1416 | m_data.RemoveAt( pos, numRows ); |
f85afd4e | 1417 | } |
4db6714b | 1418 | |
f85afd4e MB |
1419 | if ( GetView() ) |
1420 | { | |
1421 | wxGridTableMessage msg( this, | |
1422 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, | |
1423 | pos, | |
1424 | numRows ); | |
8f177c8e | 1425 | |
f85afd4e MB |
1426 | GetView()->ProcessTableMessage( msg ); |
1427 | } | |
1428 | ||
ca65c044 | 1429 | return true; |
f85afd4e MB |
1430 | } |
1431 | ||
1432 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) | |
1433 | { | |
1434 | size_t row, col; | |
1435 | ||
1436 | size_t curNumRows = m_data.GetCount(); | |
4db6714b KH |
1437 | size_t curNumCols = ( curNumRows > 0 |
1438 | ? m_data[0].GetCount() | |
1439 | : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 1440 | |
f85afd4e MB |
1441 | if ( pos >= curNumCols ) |
1442 | { | |
1443 | return AppendCols( numCols ); | |
1444 | } | |
1445 | ||
d4175745 VZ |
1446 | if ( !m_colLabels.IsEmpty() ) |
1447 | { | |
1448 | m_colLabels.Insert( wxEmptyString, pos, numCols ); | |
1449 | ||
1450 | size_t i; | |
1451 | for ( i = pos; i < pos + numCols; i++ ) | |
1452 | m_colLabels[i] = wxGridTableBase::GetColLabelValue( i ); | |
1453 | } | |
1454 | ||
3d59537f | 1455 | for ( row = 0; row < curNumRows; row++ ) |
f85afd4e | 1456 | { |
3d59537f | 1457 | for ( col = pos; col < pos + numCols; col++ ) |
f85afd4e MB |
1458 | { |
1459 | m_data[row].Insert( wxEmptyString, col ); | |
1460 | } | |
1461 | } | |
4db6714b | 1462 | |
5c3313a9 VZ |
1463 | m_numCols += numCols; |
1464 | ||
f85afd4e MB |
1465 | if ( GetView() ) |
1466 | { | |
1467 | wxGridTableMessage msg( this, | |
1468 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, | |
1469 | pos, | |
1470 | numCols ); | |
8f177c8e | 1471 | |
f85afd4e MB |
1472 | GetView()->ProcessTableMessage( msg ); |
1473 | } | |
1474 | ||
ca65c044 | 1475 | return true; |
f85afd4e MB |
1476 | } |
1477 | ||
1478 | bool wxGridStringTable::AppendCols( size_t numCols ) | |
1479 | { | |
27f35b66 | 1480 | size_t row; |
f85afd4e MB |
1481 | |
1482 | size_t curNumRows = m_data.GetCount(); | |
2f024384 | 1483 | |
3d59537f | 1484 | for ( row = 0; row < curNumRows; row++ ) |
f85afd4e | 1485 | { |
27f35b66 | 1486 | m_data[row].Add( wxEmptyString, numCols ); |
f85afd4e MB |
1487 | } |
1488 | ||
5c3313a9 VZ |
1489 | m_numCols += numCols; |
1490 | ||
f85afd4e MB |
1491 | if ( GetView() ) |
1492 | { | |
1493 | wxGridTableMessage msg( this, | |
1494 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, | |
1495 | numCols ); | |
8f177c8e | 1496 | |
f85afd4e MB |
1497 | GetView()->ProcessTableMessage( msg ); |
1498 | } | |
1499 | ||
ca65c044 | 1500 | return true; |
f85afd4e MB |
1501 | } |
1502 | ||
1503 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) | |
1504 | { | |
27f35b66 | 1505 | size_t row; |
f85afd4e MB |
1506 | |
1507 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 BP |
1508 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
1509 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 1510 | |
f85afd4e MB |
1511 | if ( pos >= curNumCols ) |
1512 | { | |
e91d2033 VZ |
1513 | wxFAIL_MSG( wxString::Format |
1514 | ( | |
1515 | wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), | |
1516 | (unsigned long)pos, | |
1517 | (unsigned long)numCols, | |
1518 | (unsigned long)curNumCols | |
1519 | ) ); | |
ca65c044 | 1520 | return false; |
f85afd4e MB |
1521 | } |
1522 | ||
d4175745 VZ |
1523 | int colID; |
1524 | if ( GetView() ) | |
1525 | colID = GetView()->GetColAt( pos ); | |
1526 | else | |
1527 | colID = pos; | |
1528 | ||
1529 | if ( numCols > curNumCols - colID ) | |
1530 | { | |
1531 | numCols = curNumCols - colID; | |
1532 | } | |
1533 | ||
1534 | if ( !m_colLabels.IsEmpty() ) | |
f85afd4e | 1535 | { |
b2df5ddf VZ |
1536 | // m_colLabels stores just as many elements as it needs, e.g. if only |
1537 | // the label of the first column had been set it would have only one | |
1538 | // element and not numCols, so account for it | |
d32fb5f9 | 1539 | int numRemaining = m_colLabels.size() - colID; |
84484a8d RD |
1540 | if (numRemaining > 0) |
1541 | m_colLabels.RemoveAt( colID, wxMin(numCols, numRemaining) ); | |
f85afd4e MB |
1542 | } |
1543 | ||
5c3313a9 | 1544 | if ( numCols >= curNumCols ) |
f85afd4e | 1545 | { |
5c3313a9 | 1546 | for ( row = 0; row < curNumRows; row++ ) |
f85afd4e | 1547 | { |
dcdce64e | 1548 | m_data[row].Clear(); |
f85afd4e | 1549 | } |
5c3313a9 VZ |
1550 | |
1551 | m_numCols = 0; | |
1552 | } | |
1553 | else // something will be left | |
1554 | { | |
1555 | for ( row = 0; row < curNumRows; row++ ) | |
f85afd4e | 1556 | { |
d4175745 | 1557 | m_data[row].RemoveAt( colID, numCols ); |
f85afd4e | 1558 | } |
5c3313a9 VZ |
1559 | |
1560 | m_numCols -= numCols; | |
f85afd4e | 1561 | } |
4db6714b | 1562 | |
f85afd4e MB |
1563 | if ( GetView() ) |
1564 | { | |
1565 | wxGridTableMessage msg( this, | |
1566 | wxGRIDTABLE_NOTIFY_COLS_DELETED, | |
1567 | pos, | |
1568 | numCols ); | |
8f177c8e | 1569 | |
f85afd4e MB |
1570 | GetView()->ProcessTableMessage( msg ); |
1571 | } | |
1572 | ||
ca65c044 | 1573 | return true; |
f85afd4e MB |
1574 | } |
1575 | ||
1576 | wxString wxGridStringTable::GetRowLabelValue( int row ) | |
1577 | { | |
1578 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
1579 | { | |
1580 | // using default label | |
1581 | // | |
1582 | return wxGridTableBase::GetRowLabelValue( row ); | |
1583 | } | |
1584 | else | |
1585 | { | |
2f024384 | 1586 | return m_rowLabels[row]; |
f85afd4e MB |
1587 | } |
1588 | } | |
1589 | ||
1590 | wxString wxGridStringTable::GetColLabelValue( int col ) | |
1591 | { | |
1592 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
1593 | { | |
1594 | // using default label | |
1595 | // | |
1596 | return wxGridTableBase::GetColLabelValue( col ); | |
1597 | } | |
1598 | else | |
1599 | { | |
2f024384 | 1600 | return m_colLabels[col]; |
f85afd4e MB |
1601 | } |
1602 | } | |
1603 | ||
1604 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) | |
1605 | { | |
1606 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
1607 | { | |
1608 | int n = m_rowLabels.GetCount(); | |
1609 | int i; | |
2f024384 | 1610 | |
3d59537f | 1611 | for ( i = n; i <= row; i++ ) |
f85afd4e MB |
1612 | { |
1613 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); | |
1614 | } | |
1615 | } | |
1616 | ||
1617 | m_rowLabels[row] = value; | |
1618 | } | |
1619 | ||
1620 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) | |
1621 | { | |
1622 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
1623 | { | |
1624 | int n = m_colLabels.GetCount(); | |
1625 | int i; | |
2f024384 | 1626 | |
3d59537f | 1627 | for ( i = n; i <= col; i++ ) |
f85afd4e MB |
1628 | { |
1629 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | m_colLabels[col] = value; | |
1634 | } | |
1635 | ||
1636 | ||
f85afd4e | 1637 | ////////////////////////////////////////////////////////////////////// |
2d66e025 MB |
1638 | ////////////////////////////////////////////////////////////////////// |
1639 | ||
86033c4b VZ |
1640 | BEGIN_EVENT_TABLE(wxGridSubwindow, wxWindow) |
1641 | EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost) | |
1642 | END_EVENT_TABLE() | |
1643 | ||
1644 | void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event)) | |
1645 | { | |
1646 | m_owner->CancelMouseCapture(); | |
1647 | } | |
1648 | ||
86033c4b | 1649 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxGridSubwindow ) |
2d66e025 | 1650 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) |
a9339fe2 | 1651 | EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel ) |
2d66e025 | 1652 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) |
2d66e025 MB |
1653 | END_EVENT_TABLE() |
1654 | ||
aa5e1f75 | 1655 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
1656 | { |
1657 | wxPaintDC dc(this); | |
1658 | ||
1659 | // NO - don't do this because it will set both the x and y origin | |
1660 | // coords to match the parent scrolled window and we just want to | |
1661 | // set the y coord - MB | |
1662 | // | |
1663 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 1664 | |
790ad94f | 1665 | int x, y; |
2d66e025 | 1666 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
615b7e6a RR |
1667 | wxPoint pt = dc.GetDeviceOrigin(); |
1668 | dc.SetDeviceOrigin( pt.x, pt.y-y ); | |
60ff3b99 | 1669 | |
d10f4bf9 | 1670 | wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); |
a9339fe2 | 1671 | m_owner->DrawRowLabels( dc, rows ); |
2d66e025 MB |
1672 | } |
1673 | ||
2d66e025 MB |
1674 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
1675 | { | |
1676 | m_owner->ProcessRowLabelMouseEvent( event ); | |
1677 | } | |
1678 | ||
b51c3f27 RD |
1679 | void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
1680 | { | |
b5e9cbb9 FM |
1681 | if (!m_owner->GetEventHandler()->ProcessEvent( event )) |
1682 | event.Skip(); | |
b51c3f27 RD |
1683 | } |
1684 | ||
2d66e025 MB |
1685 | ////////////////////////////////////////////////////////////////////// |
1686 | ||
86033c4b | 1687 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxGridSubwindow ) |
2d66e025 | 1688 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) |
a9339fe2 | 1689 | EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel ) |
2d66e025 | 1690 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) |
2d66e025 MB |
1691 | END_EVENT_TABLE() |
1692 | ||
aa5e1f75 | 1693 | void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
1694 | { |
1695 | wxPaintDC dc(this); | |
1696 | ||
1697 | // NO - don't do this because it will set both the x and y origin | |
1698 | // coords to match the parent scrolled window and we just want to | |
1699 | // set the x coord - MB | |
1700 | // | |
1701 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 1702 | |
790ad94f | 1703 | int x, y; |
2d66e025 | 1704 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
615b7e6a RR |
1705 | wxPoint pt = dc.GetDeviceOrigin(); |
1706 | if (GetLayoutDirection() == wxLayout_RightToLeft) | |
1707 | dc.SetDeviceOrigin( pt.x+x, pt.y ); | |
1708 | else | |
1709 | dc.SetDeviceOrigin( pt.x-x, pt.y ); | |
2d66e025 | 1710 | |
d10f4bf9 | 1711 | wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() ); |
a9339fe2 | 1712 | m_owner->DrawColLabels( dc, cols ); |
2d66e025 MB |
1713 | } |
1714 | ||
2d66e025 MB |
1715 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
1716 | { | |
1717 | m_owner->ProcessColLabelMouseEvent( event ); | |
1718 | } | |
1719 | ||
b51c3f27 RD |
1720 | void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
1721 | { | |
b5e9cbb9 FM |
1722 | if (!m_owner->GetEventHandler()->ProcessEvent( event )) |
1723 | event.Skip(); | |
b51c3f27 RD |
1724 | } |
1725 | ||
2d66e025 MB |
1726 | ////////////////////////////////////////////////////////////////////// |
1727 | ||
86033c4b | 1728 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxGridSubwindow ) |
a9339fe2 | 1729 | EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel ) |
2d66e025 | 1730 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) |
a9339fe2 | 1731 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint ) |
2d66e025 MB |
1732 | END_EVENT_TABLE() |
1733 | ||
d2fdd8d2 RR |
1734 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
1735 | { | |
1736 | wxPaintDC dc(this); | |
b99be8fb | 1737 | |
ff72f628 | 1738 | m_owner->DrawCornerLabel(dc); |
d2fdd8d2 RR |
1739 | } |
1740 | ||
2d66e025 MB |
1741 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
1742 | { | |
1743 | m_owner->ProcessCornerLabelMouseEvent( event ); | |
1744 | } | |
1745 | ||
b51c3f27 RD |
1746 | void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
1747 | { | |
b5e9cbb9 FM |
1748 | if (!m_owner->GetEventHandler()->ProcessEvent(event)) |
1749 | event.Skip(); | |
b51c3f27 RD |
1750 | } |
1751 | ||
f85afd4e MB |
1752 | ////////////////////////////////////////////////////////////////////// |
1753 | ||
86033c4b | 1754 | BEGIN_EVENT_TABLE( wxGridWindow, wxGridSubwindow ) |
2d66e025 | 1755 | EVT_PAINT( wxGridWindow::OnPaint ) |
a9339fe2 | 1756 | EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel ) |
2d66e025 MB |
1757 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) |
1758 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) | |
f6bcfd97 | 1759 | EVT_KEY_UP( wxGridWindow::OnKeyUp ) |
a9339fe2 | 1760 | EVT_CHAR( wxGridWindow::OnChar ) |
80acaf25 JS |
1761 | EVT_SET_FOCUS( wxGridWindow::OnFocus ) |
1762 | EVT_KILL_FOCUS( wxGridWindow::OnFocus ) | |
2796cce3 | 1763 | EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground ) |
2d66e025 MB |
1764 | END_EVENT_TABLE() |
1765 | ||
2d66e025 MB |
1766 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
1767 | { | |
1768 | wxPaintDC dc( this ); | |
1769 | m_owner->PrepareDC( dc ); | |
796df70a | 1770 | wxRegion reg = GetUpdateRegion(); |
ccdee36f DS |
1771 | wxGridCellCoordsArray dirtyCells = m_owner->CalcCellsExposed( reg ); |
1772 | m_owner->DrawGridCellArea( dc, dirtyCells ); | |
2f024384 | 1773 | |
9f7aee01 VZ |
1774 | m_owner->DrawGridSpace( dc ); |
1775 | ||
796df70a | 1776 | m_owner->DrawAllGridLines( dc, reg ); |
2f024384 | 1777 | |
ccdee36f | 1778 | m_owner->DrawHighlight( dc, dirtyCells ); |
2d66e025 MB |
1779 | } |
1780 | ||
779e28da VZ |
1781 | void wxGrid::Render( wxDC& dc, |
1782 | const wxPoint& position, | |
1783 | const wxSize& size, | |
1784 | const wxGridCellCoords& topLeft, | |
1785 | const wxGridCellCoords& bottomRight, | |
1786 | int style ) | |
1787 | { | |
1788 | wxCHECK_RET( bottomRight.GetCol() < GetNumberCols(), | |
1789 | "Invalid right column" ); | |
1790 | wxCHECK_RET( bottomRight.GetRow() < GetNumberRows(), | |
1791 | "Invalid bottom row" ); | |
1792 | ||
1793 | // store user settings and reset later | |
1794 | ||
1795 | // remove grid selection, don't paint selection colour | |
1796 | // unless we have wxGRID_DRAW_SELECTION | |
1797 | // block selections are the only ones catered for here | |
1798 | wxGridCellCoordsArray selectedCells; | |
1799 | bool hasSelection = IsSelection(); | |
1800 | if ( hasSelection && !( style & wxGRID_DRAW_SELECTION ) ) | |
1801 | { | |
1802 | selectedCells = GetSelectionBlockTopLeft(); | |
1803 | // non block selections may not have a bottom right | |
1804 | if ( GetSelectionBlockBottomRight().size() ) | |
1805 | selectedCells.Add( GetSelectionBlockBottomRight()[ 0 ] ); | |
1806 | ||
1807 | ClearSelection(); | |
1808 | } | |
1809 | ||
1810 | // store user device origin | |
1811 | wxCoord userOriginX, userOriginY; | |
1812 | dc.GetDeviceOrigin( &userOriginX, &userOriginY ); | |
1813 | ||
1814 | // store user scale | |
1815 | double scaleUserX, scaleUserY; | |
1816 | dc.GetUserScale( &scaleUserX, &scaleUserY ); | |
1817 | ||
1818 | // set defaults if necessary | |
f00acdb8 VZ |
1819 | wxGridCellCoords leftTop( topLeft ), rightBottom( bottomRight ); |
1820 | if ( leftTop.GetCol() < 0 ) | |
1821 | leftTop.SetCol(0); | |
1822 | if ( leftTop.GetRow() < 0 ) | |
1823 | leftTop.SetRow(0); | |
1824 | if ( rightBottom.GetCol() < 0 ) | |
1825 | rightBottom.SetCol(GetNumberCols() - 1); | |
1826 | if ( rightBottom.GetRow() < 0 ) | |
1827 | rightBottom.SetRow(GetNumberRows() - 1); | |
1828 | ||
1829 | // get grid offset, size and cell parameters | |
1830 | wxPoint pointOffSet; | |
1831 | wxSize sizeGrid; | |
779e28da VZ |
1832 | wxGridCellCoordsArray renderCells; |
1833 | wxArrayInt arrayCols; | |
1834 | wxArrayInt arrayRows; | |
779e28da | 1835 | |
f00acdb8 VZ |
1836 | GetRenderSizes( leftTop, rightBottom, |
1837 | pointOffSet, sizeGrid, | |
1838 | renderCells, | |
1839 | arrayCols, arrayRows ); | |
779e28da VZ |
1840 | |
1841 | // add headers/labels to dimensions | |
1842 | if ( style & wxGRID_DRAW_ROWS_HEADER ) | |
f00acdb8 | 1843 | sizeGrid.x += GetRowLabelSize(); |
779e28da | 1844 | if ( style & wxGRID_DRAW_COLS_HEADER ) |
f00acdb8 | 1845 | sizeGrid.y += GetColLabelSize(); |
779e28da | 1846 | |
f00acdb8 VZ |
1847 | // get render start position in logical units |
1848 | wxPoint positionRender = GetRenderPosition( dc, position ); | |
779e28da | 1849 | |
779e28da VZ |
1850 | wxCoord originX = dc.LogicalToDeviceX( positionRender.x ); |
1851 | wxCoord originY = dc.LogicalToDeviceY( positionRender.y ); | |
1852 | ||
1853 | dc.SetDeviceOrigin( originX, originY ); | |
1854 | ||
f00acdb8 | 1855 | SetRenderScale( dc, positionRender, size, sizeGrid ); |
779e28da VZ |
1856 | |
1857 | // draw row headers at specified origin | |
1858 | if ( GetRowLabelSize() > 0 && ( style & wxGRID_DRAW_ROWS_HEADER ) ) | |
1859 | { | |
1860 | if ( style & wxGRID_DRAW_COLS_HEADER ) | |
1861 | { | |
1862 | DrawCornerLabel( dc ); // do only if both col and row labels drawn | |
1863 | originY += dc.LogicalToDeviceYRel( GetColLabelSize() ); | |
1864 | } | |
1865 | ||
f00acdb8 | 1866 | originY -= dc.LogicalToDeviceYRel( pointOffSet.y ); |
779e28da VZ |
1867 | dc.SetDeviceOrigin( originX, originY ); |
1868 | ||
1869 | DrawRowLabels( dc, arrayRows ); | |
1870 | ||
1871 | // reset for columns | |
1872 | if ( style & wxGRID_DRAW_COLS_HEADER ) | |
1873 | originY -= dc.LogicalToDeviceYRel( GetColLabelSize() ); | |
1874 | ||
f00acdb8 | 1875 | originY += dc.LogicalToDeviceYRel( pointOffSet.y ); |
779e28da VZ |
1876 | // X offset so we don't overwrite row labels |
1877 | originX += dc.LogicalToDeviceXRel( GetRowLabelSize() ); | |
1878 | } | |
1879 | ||
1880 | // subtract col offset where startcol > 0 | |
f00acdb8 | 1881 | originX -= dc.LogicalToDeviceXRel( pointOffSet.x ); |
779e28da VZ |
1882 | // no y offset for col labels, they are at the Y origin |
1883 | ||
1884 | // draw column labels | |
1885 | if ( style & wxGRID_DRAW_COLS_HEADER ) | |
1886 | { | |
1887 | dc.SetDeviceOrigin( originX, originY ); | |
1888 | DrawColLabels( dc, arrayCols ); | |
1889 | // don't overwrite the labels, increment originY | |
1890 | originY += dc.LogicalToDeviceYRel( GetColLabelSize() ); | |
1891 | } | |
1892 | ||
1893 | // set device origin to draw grid cells and lines | |
f00acdb8 | 1894 | originY -= dc.LogicalToDeviceYRel( pointOffSet.y ); |
779e28da VZ |
1895 | dc.SetDeviceOrigin( originX, originY ); |
1896 | ||
1897 | // draw cell area background | |
1898 | dc.SetBrush( GetDefaultCellBackgroundColour() ); | |
1899 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
1900 | // subtract headers from grid area dimensions | |
f00acdb8 | 1901 | wxSize sizeCells( sizeGrid ); |
779e28da | 1902 | if ( style & wxGRID_DRAW_ROWS_HEADER ) |
f00acdb8 | 1903 | sizeCells.x -= GetRowLabelSize(); |
779e28da | 1904 | if ( style & wxGRID_DRAW_COLS_HEADER ) |
f00acdb8 | 1905 | sizeCells.y -= GetColLabelSize(); |
779e28da | 1906 | |
f00acdb8 | 1907 | dc.DrawRectangle( pointOffSet, sizeCells ); |
779e28da VZ |
1908 | |
1909 | // draw cells | |
1910 | DrawGridCellArea( dc, renderCells ); | |
1911 | ||
f00acdb8 | 1912 | // draw grid lines |
779e28da VZ |
1913 | if ( style & wxGRID_DRAW_CELL_LINES ) |
1914 | { | |
f00acdb8 VZ |
1915 | wxRegion regionClip( pointOffSet.x, pointOffSet.y, |
1916 | sizeCells.x, sizeCells.y ); | |
779e28da VZ |
1917 | |
1918 | DrawRangeGridLines(dc, regionClip, renderCells[0], renderCells.Last()); | |
1919 | } | |
1920 | ||
1921 | // draw render rectangle bounding lines | |
f00acdb8 VZ |
1922 | DoRenderBox( dc, style, |
1923 | pointOffSet, sizeCells, | |
1924 | leftTop, rightBottom ); | |
779e28da VZ |
1925 | |
1926 | // restore user setings | |
1927 | dc.SetDeviceOrigin( userOriginX, userOriginY ); | |
1928 | dc.SetUserScale( scaleUserX, scaleUserY ); | |
1929 | ||
1930 | if ( selectedCells.size() && !( style & wxGRID_DRAW_SELECTION ) ) | |
1931 | { | |
1932 | SelectBlock( selectedCells[ 0 ].GetRow(), | |
1933 | selectedCells[ 0 ].GetCol(), | |
1934 | selectedCells[ selectedCells.size() -1 ].GetRow(), | |
1935 | selectedCells[ selectedCells.size() -1 ].GetCol() ); | |
1936 | } | |
1937 | } | |
1938 | ||
1939 | void | |
1940 | wxGrid::SetRenderScale(wxDC& dc, | |
1941 | const wxPoint& pos, const wxSize& size, | |
f00acdb8 | 1942 | const wxSize& sizeGrid ) |
779e28da VZ |
1943 | { |
1944 | double scaleX, scaleY; | |
1945 | wxSize sizeTemp; | |
1946 | ||
8e2c6c3f VZ |
1947 | if ( size.GetWidth() != wxDefaultSize.GetWidth() ) // size.x was specified |
1948 | sizeTemp.SetWidth( size.GetWidth() ); | |
779e28da | 1949 | else |
8e2c6c3f | 1950 | sizeTemp.SetWidth( dc.DeviceToLogicalXRel( dc.GetSize().GetWidth() ) |
779e28da VZ |
1951 | - pos.x ); |
1952 | ||
8e2c6c3f VZ |
1953 | if ( size.GetHeight() != wxDefaultSize.GetHeight() ) // size.y was specified |
1954 | sizeTemp.SetHeight( size.GetHeight() ); | |
779e28da | 1955 | else |
8e2c6c3f | 1956 | sizeTemp.SetHeight( dc.DeviceToLogicalYRel( dc.GetSize().GetHeight() ) |
779e28da VZ |
1957 | - pos.y ); |
1958 | ||
8e2c6c3f VZ |
1959 | scaleX = (double)( (double) sizeTemp.GetWidth() / (double) sizeGrid.GetWidth() ); |
1960 | scaleY = (double)( (double) sizeTemp.GetHeight() / (double) sizeGrid.GetHeight() ); | |
779e28da VZ |
1961 | |
1962 | dc.SetUserScale( wxMin( scaleX, scaleY), wxMin( scaleX, scaleY ) ); | |
1963 | } | |
1964 | ||
f00acdb8 VZ |
1965 | // get grid rendered size, origin offset and fill cell arrays |
1966 | void wxGrid::GetRenderSizes( const wxGridCellCoords& topLeft, | |
1967 | const wxGridCellCoords& bottomRight, | |
1968 | wxPoint& pointOffSet, wxSize& sizeGrid, | |
1969 | wxGridCellCoordsArray& renderCells, | |
1970 | wxArrayInt& arrayCols, wxArrayInt& arrayRows ) | |
1971 | { | |
1972 | pointOffSet.x = 0; | |
1973 | pointOffSet.y = 0; | |
1974 | sizeGrid.SetWidth( 0 ); | |
1975 | sizeGrid.SetHeight( 0 ); | |
1976 | ||
1977 | int col, row; | |
1978 | ||
1979 | wxGridSizesInfo sizeinfo = GetColSizes(); | |
1980 | for ( col = 0; col <= bottomRight.GetCol(); col++ ) | |
1981 | { | |
1982 | if ( col < topLeft.GetCol() ) | |
1983 | { | |
1984 | pointOffSet.x += sizeinfo.GetSize( col ); | |
1985 | } | |
1986 | else | |
1987 | { | |
1988 | for ( row = topLeft.GetRow(); row <= bottomRight.GetRow(); row++ ) | |
1989 | { | |
1990 | renderCells.Add( wxGridCellCoords( row, col )); | |
1991 | arrayRows.Add( row ); // column labels rendered in DrawColLabels | |
1992 | } | |
1993 | arrayCols.Add( col ); // row labels rendered in DrawRowLabels | |
1994 | sizeGrid.x += sizeinfo.GetSize( col ); | |
1995 | } | |
1996 | } | |
1997 | ||
1998 | sizeinfo = GetRowSizes(); | |
1999 | for ( row = 0; row <= bottomRight.GetRow(); row++ ) | |
2000 | { | |
2001 | if ( row < topLeft.GetRow() ) | |
2002 | pointOffSet.y += sizeinfo.GetSize( row ); | |
2003 | else | |
2004 | sizeGrid.y += sizeinfo.GetSize( row ); | |
2005 | } | |
2006 | } | |
2007 | ||
2008 | // get render start position | |
2009 | // if position not specified use dc draw extents MaxX and MaxY | |
2010 | wxPoint wxGrid::GetRenderPosition( wxDC& dc, const wxPoint& position ) | |
2011 | { | |
2012 | wxPoint positionRender( position ); | |
2013 | ||
2014 | if ( !positionRender.IsFullySpecified() ) | |
2015 | { | |
2016 | if ( positionRender.x == wxDefaultPosition.x ) | |
2017 | positionRender.x = dc.MaxX(); | |
2018 | ||
2019 | if ( positionRender.y == wxDefaultPosition.y ) | |
2020 | positionRender.y = dc.MaxY(); | |
2021 | } | |
2022 | ||
2023 | return positionRender; | |
2024 | } | |
2025 | ||
2026 | // draw render rectangle bounding lines | |
2027 | // useful where there is multi cell row or col clipping and no cell border | |
2028 | void wxGrid::DoRenderBox( wxDC& dc, const int& style, | |
2029 | const wxPoint& pointOffSet, | |
2030 | const wxSize& sizeCells, | |
2031 | const wxGridCellCoords& topLeft, | |
2032 | const wxGridCellCoords& bottomRight ) | |
2033 | { | |
2034 | if ( !( style & wxGRID_DRAW_BOX_RECT ) ) | |
2035 | return; | |
2036 | ||
2037 | int bottom = pointOffSet.y + sizeCells.GetY(), | |
2038 | right = pointOffSet.x + sizeCells.GetX() - 1; | |
2039 | ||
2040 | // horiz top line if we are not drawing column header/labels | |
2041 | if ( !( style & wxGRID_DRAW_COLS_HEADER ) ) | |
2042 | { | |
2043 | int left = pointOffSet.x; | |
2044 | left += ( style & wxGRID_DRAW_COLS_HEADER ) | |
2045 | ? - GetRowLabelSize() : 0; | |
2046 | dc.SetPen( GetRowGridLinePen( topLeft.GetRow() ) ); | |
2047 | dc.DrawLine( left, | |
2048 | pointOffSet.y, | |
2049 | right, | |
2050 | pointOffSet.y ); | |
2051 | } | |
2052 | ||
2053 | // horiz bottom line | |
2054 | dc.SetPen( GetRowGridLinePen( bottomRight.GetRow() ) ); | |
2055 | dc.DrawLine( pointOffSet.x, bottom - 1, right, bottom - 1 ); | |
2056 | ||
2057 | // left vertical line if we are not drawing row header/labels | |
2058 | if ( !( style & wxGRID_DRAW_ROWS_HEADER ) ) | |
2059 | { | |
2060 | int top = pointOffSet.y; | |
2061 | top += ( style & wxGRID_DRAW_COLS_HEADER ) | |
2062 | ? - GetColLabelSize() : 0; | |
2063 | dc.SetPen( GetColGridLinePen( topLeft.GetCol() ) ); | |
2064 | dc.DrawLine( pointOffSet.x -1, | |
2065 | top, | |
2066 | pointOffSet.x - 1, | |
2067 | bottom - 1 ); | |
2068 | } | |
2069 | ||
2070 | // right vertical line | |
2071 | dc.SetPen( GetColGridLinePen( bottomRight.GetCol() ) ); | |
2072 | dc.DrawLine( right, pointOffSet.y, right, bottom - 1 ); | |
2073 | } | |
2074 | ||
2d66e025 MB |
2075 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) |
2076 | { | |
59ddac01 | 2077 | wxWindow::ScrollWindow( dx, dy, rect ); |
ad805b9e VZ |
2078 | m_owner->GetGridRowLabelWindow()->ScrollWindow( 0, dy, rect ); |
2079 | m_owner->GetGridColLabelWindow()->ScrollWindow( dx, 0, rect ); | |
2d66e025 MB |
2080 | } |
2081 | ||
2d66e025 MB |
2082 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) |
2083 | { | |
33e9fc54 RD |
2084 | if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this) |
2085 | SetFocus(); | |
902725ee | 2086 | |
2d66e025 MB |
2087 | m_owner->ProcessGridCellMouseEvent( event ); |
2088 | } | |
2089 | ||
b51c3f27 RD |
2090 | void wxGridWindow::OnMouseWheel( wxMouseEvent& event ) |
2091 | { | |
b5e9cbb9 FM |
2092 | if (!m_owner->GetEventHandler()->ProcessEvent( event )) |
2093 | event.Skip(); | |
b51c3f27 | 2094 | } |
2d66e025 | 2095 | |
f6bcfd97 | 2096 | // This seems to be required for wxMotif/wxGTK otherwise the mouse |
2d66e025 MB |
2097 | // cursor must be in the cell edit control to get key events |
2098 | // | |
2099 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) | |
2100 | { | |
4db6714b KH |
2101 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
2102 | event.Skip(); | |
2d66e025 | 2103 | } |
f85afd4e | 2104 | |
f6bcfd97 BP |
2105 | void wxGridWindow::OnKeyUp( wxKeyEvent& event ) |
2106 | { | |
4db6714b KH |
2107 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
2108 | event.Skip(); | |
f6bcfd97 | 2109 | } |
7c8a8ad5 | 2110 | |
63e2147c RD |
2111 | void wxGridWindow::OnChar( wxKeyEvent& event ) |
2112 | { | |
4db6714b KH |
2113 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
2114 | event.Skip(); | |
63e2147c RD |
2115 | } |
2116 | ||
aa5e1f75 | 2117 | void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) |
8dd4f536 | 2118 | { |
8dd4f536 | 2119 | } |
025562fe | 2120 | |
80acaf25 JS |
2121 | void wxGridWindow::OnFocus(wxFocusEvent& event) |
2122 | { | |
760be3f7 VS |
2123 | // and if we have any selection, it has to be repainted, because it |
2124 | // uses different colour when the grid is not focused: | |
2125 | if ( m_owner->IsSelection() ) | |
2126 | { | |
2127 | Refresh(); | |
2128 | } | |
2129 | else | |
2130 | { | |
2131 | // NB: Note that this code is in "else" branch only because the other | |
2132 | // branch refreshes everything and so there's no point in calling | |
2133 | // Refresh() again, *not* because it should only be done if | |
2134 | // !IsSelection(). If the above code is ever optimized to refresh | |
2135 | // only selected area, this needs to be moved out of the "else" | |
2136 | // branch so that it's always executed. | |
2137 | ||
2138 | // current cell cursor {dis,re}appears on focus change: | |
2139 | const wxGridCellCoords cursorCoords(m_owner->GetGridCursorRow(), | |
2140 | m_owner->GetGridCursorCol()); | |
2141 | const wxRect cursor = | |
2142 | m_owner->BlockToDeviceRect(cursorCoords, cursorCoords); | |
2143 | Refresh(true, &cursor); | |
2144 | } | |
2145 | ||
80acaf25 JS |
2146 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
2147 | event.Skip(); | |
2148 | } | |
2d66e025 | 2149 | |
d4175745 | 2150 | #define internalXToCol(x) XToCol(x, true) |
bec70262 | 2151 | #define internalYToRow(y) YToRow(y, true) |
ccdee36f | 2152 | |
33188aa4 | 2153 | ///////////////////////////////////////////////////////////////////// |
07296f0b | 2154 | |
2d66e025 | 2155 | BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow ) |
f85afd4e MB |
2156 | EVT_PAINT( wxGrid::OnPaint ) |
2157 | EVT_SIZE( wxGrid::OnSize ) | |
f85afd4e | 2158 | EVT_KEY_DOWN( wxGrid::OnKeyDown ) |
f6bcfd97 | 2159 | EVT_KEY_UP( wxGrid::OnKeyUp ) |
63e2147c | 2160 | EVT_CHAR ( wxGrid::OnChar ) |
2796cce3 | 2161 | EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground ) |
add6e919 | 2162 | EVT_COMMAND(wxID_ANY, wxEVT_GRID_HIDE_EDITOR, wxGrid::OnHideEditor ) |
f85afd4e | 2163 | END_EVENT_TABLE() |
8f177c8e | 2164 | |
b0a877ec SC |
2165 | bool wxGrid::Create(wxWindow *parent, wxWindowID id, |
2166 | const wxPoint& pos, const wxSize& size, | |
2167 | long style, const wxString& name) | |
2168 | { | |
2169 | if (!wxScrolledWindow::Create(parent, id, pos, size, | |
c2f5b920 | 2170 | style | wxWANTS_CHARS, name)) |
ca65c044 | 2171 | return false; |
b0a877ec | 2172 | |
2f024384 DS |
2173 | m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE); |
2174 | m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE); | |
b0a877ec | 2175 | |
2f024384 | 2176 | Create(); |
170acdc9 | 2177 | SetInitialSize(size); |
b93aafab | 2178 | CalcDimensions(); |
b0a877ec | 2179 | |
ca65c044 | 2180 | return true; |
b0a877ec SC |
2181 | } |
2182 | ||
58dd5b3b MB |
2183 | wxGrid::~wxGrid() |
2184 | { | |
e882d288 VZ |
2185 | if ( m_winCapture ) |
2186 | m_winCapture->ReleaseMouse(); | |
2187 | ||
b09b3048 VZ |
2188 | // Ensure that the editor control is destroyed before the grid is, |
2189 | // otherwise we crash later when the editor tries to do something with the | |
2190 | // half destroyed grid | |
2191 | HideCellEditControl(); | |
2192 | ||
606b005f JS |
2193 | // Must do this or ~wxScrollHelper will pop the wrong event handler |
2194 | SetTargetWindow(this); | |
0a976765 | 2195 | ClearAttrCache(); |
39bcce60 | 2196 | wxSafeDecRef(m_defaultCellAttr); |
0a976765 VZ |
2197 | |
2198 | #ifdef DEBUG_ATTR_CACHE | |
2199 | size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses; | |
9a83f860 | 2200 | wxPrintf(wxT("wxGrid attribute cache statistics: " |
0a976765 VZ |
2201 | "total: %u, hits: %u (%u%%)\n"), |
2202 | total, gs_nAttrCacheHits, | |
2203 | total ? (gs_nAttrCacheHits*100) / total : 0); | |
2204 | #endif | |
2205 | ||
86020f7e VZ |
2206 | // if we own the table, just delete it, otherwise at least don't leave it |
2207 | // with dangling view pointer | |
2208 | if ( m_ownTable ) | |
2796cce3 | 2209 | delete m_table; |
ecc7aa82 | 2210 | else if ( m_table && m_table->GetView() == this ) |
86020f7e | 2211 | m_table->SetView(NULL); |
f2d76237 RD |
2212 | |
2213 | delete m_typeRegistry; | |
b5808881 | 2214 | delete m_selection; |
82edfbe7 VZ |
2215 | |
2216 | delete m_setFixedRows; | |
2217 | delete m_setFixedCols; | |
58dd5b3b MB |
2218 | } |
2219 | ||
58dd5b3b MB |
2220 | // |
2221 | // ----- internal init and update functions | |
2222 | // | |
2223 | ||
9950649c RD |
2224 | // NOTE: If using the default visual attributes works everywhere then this can |
2225 | // be removed as well as the #else cases below. | |
2226 | #define _USE_VISATTR 0 | |
2227 | ||
58dd5b3b | 2228 | void wxGrid::Create() |
f0102d2a | 2229 | { |
3d59537f DS |
2230 | // create the type registry |
2231 | m_typeRegistry = new wxGridTypeRegistry; | |
2c9a89e0 | 2232 | |
ca65c044 | 2233 | m_cellEditCtrlEnabled = false; |
4634a5d6 | 2234 | |
ccd970b1 | 2235 | m_defaultCellAttr = new wxGridCellAttr(); |
f2d76237 RD |
2236 | |
2237 | // Set default cell attributes | |
ccd970b1 | 2238 | m_defaultCellAttr->SetDefAttr(m_defaultCellAttr); |
19d7140e | 2239 | m_defaultCellAttr->SetKind(wxGridCellAttr::Default); |
f2d76237 | 2240 | m_defaultCellAttr->SetFont(GetFont()); |
4c7277db | 2241 | m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP); |
9950649c RD |
2242 | m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer); |
2243 | m_defaultCellAttr->SetEditor(new wxGridCellTextEditor); | |
2244 | ||
2245 | #if _USE_VISATTR | |
2246 | wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes(); | |
2247 | wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes(); | |
2248 | ||
2249 | m_defaultCellAttr->SetTextColour(gva.colFg); | |
2250 | m_defaultCellAttr->SetBackgroundColour(gva.colBg); | |
ca65c044 | 2251 | |
9950649c | 2252 | #else |
f2d76237 | 2253 | m_defaultCellAttr->SetTextColour( |
a756f210 | 2254 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); |
f2d76237 | 2255 | m_defaultCellAttr->SetBackgroundColour( |
a756f210 | 2256 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
9950649c | 2257 | #endif |
2796cce3 | 2258 | |
4634a5d6 MB |
2259 | m_numRows = 0; |
2260 | m_numCols = 0; | |
2261 | m_currentCellCoords = wxGridNoCellCoords; | |
b99be8fb | 2262 | |
f2d76237 | 2263 | // subwindow components that make up the wxGrid |
ad805b9e VZ |
2264 | m_rowLabelWin = new wxGridRowLabelWindow(this); |
2265 | CreateColumnWindow(); | |
2266 | m_cornerLabelWin = new wxGridCornerLabelWindow(this); | |
2267 | m_gridWin = new wxGridWindow( this ); | |
2d66e025 MB |
2268 | |
2269 | SetTargetWindow( m_gridWin ); | |
6f36917b | 2270 | |
9950649c RD |
2271 | #if _USE_VISATTR |
2272 | wxColour gfg = gva.colFg; | |
2273 | wxColour gbg = gva.colBg; | |
2274 | wxColour lfg = lva.colFg; | |
2275 | wxColour lbg = lva.colBg; | |
2276 | #else | |
2277 | wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
2278 | wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ); | |
2279 | wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
2280 | wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ); | |
2281 | #endif | |
2f024384 | 2282 | |
fa47d7a7 VS |
2283 | m_cornerLabelWin->SetOwnForegroundColour(lfg); |
2284 | m_cornerLabelWin->SetOwnBackgroundColour(lbg); | |
2285 | m_rowLabelWin->SetOwnForegroundColour(lfg); | |
2286 | m_rowLabelWin->SetOwnBackgroundColour(lbg); | |
ad805b9e VZ |
2287 | m_colWindow->SetOwnForegroundColour(lfg); |
2288 | m_colWindow->SetOwnBackgroundColour(lbg); | |
fa47d7a7 VS |
2289 | |
2290 | m_gridWin->SetOwnForegroundColour(gfg); | |
2291 | m_gridWin->SetOwnBackgroundColour(gbg); | |
ca65c044 | 2292 | |
ad805b9e VZ |
2293 | m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour(); |
2294 | m_labelTextColour = m_rowLabelWin->GetForegroundColour(); | |
2295 | ||
2296 | // now that we have the grid window, use its font to compute the default | |
2297 | // row height | |
2298 | m_defaultRowHeight = m_gridWin->GetCharHeight(); | |
2299 | #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl() | |
2300 | m_defaultRowHeight += 8; | |
2301 | #else | |
2302 | m_defaultRowHeight += 4; | |
2303 | #endif | |
2304 | ||
2305 | } | |
2306 | ||
2307 | void wxGrid::CreateColumnWindow() | |
2308 | { | |
2309 | if ( m_useNativeHeader ) | |
2310 | { | |
2311 | m_colWindow = new wxGridHeaderCtrl(this); | |
2312 | m_colLabelHeight = m_colWindow->GetBestSize().y; | |
2313 | } | |
2314 | else // draw labels ourselves | |
2315 | { | |
2316 | m_colWindow = new wxGridColLabelWindow(this); | |
2317 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
2318 | } | |
2d66e025 | 2319 | } |
f85afd4e | 2320 | |
b5808881 | 2321 | bool wxGrid::CreateGrid( int numRows, int numCols, |
6b992f7d | 2322 | wxGridSelectionModes selmode ) |
2d66e025 | 2323 | { |
f6bcfd97 | 2324 | wxCHECK_MSG( !m_created, |
ca65c044 | 2325 | false, |
f6bcfd97 BP |
2326 | wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); |
2327 | ||
6b992f7d | 2328 | return SetTable(new wxGridStringTable(numRows, numCols), true, selmode); |
2796cce3 RD |
2329 | } |
2330 | ||
6b992f7d | 2331 | void wxGrid::SetSelectionMode(wxGridSelectionModes selmode) |
f1567cdd | 2332 | { |
6f36917b VZ |
2333 | wxCHECK_RET( m_created, |
2334 | wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") ); | |
2335 | ||
2336 | m_selection->SetSelectionMode( selmode ); | |
f1567cdd SN |
2337 | } |
2338 | ||
aa5b8857 SN |
2339 | wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const |
2340 | { | |
6b992f7d | 2341 | wxCHECK_MSG( m_created, wxGridSelectCells, |
aa5b8857 SN |
2342 | wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") ); |
2343 | ||
2344 | return m_selection->GetSelectionMode(); | |
2345 | } | |
2346 | ||
6b992f7d VZ |
2347 | bool |
2348 | wxGrid::SetTable(wxGridTableBase *table, | |
2349 | bool takeOwnership, | |
2350 | wxGrid::wxGridSelectionModes selmode ) | |
2796cce3 | 2351 | { |
a7dde52f | 2352 | bool checkSelection = false; |
2796cce3 RD |
2353 | if ( m_created ) |
2354 | { | |
3e13956a | 2355 | // stop all processing |
ca65c044 | 2356 | m_created = false; |
86c7378f | 2357 | |
c71b2126 | 2358 | if (m_table) |
86c7378f | 2359 | { |
a7dde52f SN |
2360 | m_table->SetView(0); |
2361 | if( m_ownTable ) | |
2362 | delete m_table; | |
5b061713 | 2363 | m_table = NULL; |
86c7378f | 2364 | } |
2f024384 | 2365 | |
5276b0a5 | 2366 | wxDELETE(m_selection); |
a7dde52f SN |
2367 | |
2368 | m_ownTable = false; | |
2f024384 DS |
2369 | m_numRows = 0; |
2370 | m_numCols = 0; | |
a7dde52f SN |
2371 | checkSelection = true; |
2372 | ||
2373 | // kill row and column size arrays | |
2374 | m_colWidths.Empty(); | |
2375 | m_colRights.Empty(); | |
2376 | m_rowHeights.Empty(); | |
2377 | m_rowBottoms.Empty(); | |
233a54f6 | 2378 | } |
2f024384 | 2379 | |
233a54f6 | 2380 | if (table) |
2796cce3 RD |
2381 | { |
2382 | m_numRows = table->GetNumberRows(); | |
2383 | m_numCols = table->GetNumberCols(); | |
2384 | ||
ad805b9e | 2385 | if ( m_useNativeHeader ) |
3039ade9 | 2386 | GetGridColHeader()->SetColumnCount(m_numCols); |
ad805b9e | 2387 | |
2796cce3 RD |
2388 | m_table = table; |
2389 | m_table->SetView( this ); | |
8fc856de | 2390 | m_ownTable = takeOwnership; |
043d16b2 | 2391 | m_selection = new wxGridSelection( this, selmode ); |
a7dde52f SN |
2392 | if (checkSelection) |
2393 | { | |
2394 | // If the newly set table is smaller than the | |
2395 | // original one current cell and selection regions | |
2396 | // might be invalid, | |
8a3e536c | 2397 | m_selectedBlockCorner = wxGridNoCellCoords; |
c71b2126 | 2398 | m_currentCellCoords = |
a7dde52f SN |
2399 | wxGridCellCoords(wxMin(m_numRows, m_currentCellCoords.GetRow()), |
2400 | wxMin(m_numCols, m_currentCellCoords.GetCol())); | |
8a3e536c VZ |
2401 | if (m_selectedBlockTopLeft.GetRow() >= m_numRows || |
2402 | m_selectedBlockTopLeft.GetCol() >= m_numCols) | |
a7dde52f | 2403 | { |
8a3e536c VZ |
2404 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
2405 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
a7dde52f SN |
2406 | } |
2407 | else | |
8a3e536c | 2408 | m_selectedBlockBottomRight = |
a7dde52f | 2409 | wxGridCellCoords(wxMin(m_numRows, |
8a3e536c | 2410 | m_selectedBlockBottomRight.GetRow()), |
a7dde52f | 2411 | wxMin(m_numCols, |
8a3e536c | 2412 | m_selectedBlockBottomRight.GetCol())); |
a7dde52f | 2413 | } |
6f36917b VZ |
2414 | CalcDimensions(); |
2415 | ||
ca65c044 | 2416 | m_created = true; |
2d66e025 | 2417 | } |
f85afd4e | 2418 | |
1f89d869 VZ |
2419 | InvalidateBestSize(); |
2420 | ||
2d66e025 | 2421 | return m_created; |
f85afd4e MB |
2422 | } |
2423 | ||
ad805b9e | 2424 | void wxGrid::Init() |
f9549841 VZ |
2425 | { |
2426 | m_created = false; | |
2427 | ||
2428 | m_cornerLabelWin = NULL; | |
2429 | m_rowLabelWin = NULL; | |
ad805b9e | 2430 | m_colWindow = NULL; |
f9549841 VZ |
2431 | m_gridWin = NULL; |
2432 | ||
2433 | m_table = NULL; | |
2434 | m_ownTable = false; | |
2435 | ||
2436 | m_selection = NULL; | |
2437 | m_defaultCellAttr = NULL; | |
2438 | m_typeRegistry = NULL; | |
2439 | m_winCapture = NULL; | |
f9549841 | 2440 | |
f85afd4e MB |
2441 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; |
2442 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
2443 | ||
82edfbe7 VZ |
2444 | m_setFixedRows = |
2445 | m_setFixedCols = NULL; | |
2446 | ||
0a976765 VZ |
2447 | // init attr cache |
2448 | m_attrCache.row = -1; | |
2b5f62a0 VZ |
2449 | m_attrCache.col = -1; |
2450 | m_attrCache.attr = NULL; | |
0a976765 | 2451 | |
ff72f628 | 2452 | m_labelFont = GetFont(); |
52d6f640 | 2453 | m_labelFont.SetWeight( wxBOLD ); |
8f177c8e | 2454 | |
73145b0e | 2455 | m_rowLabelHorizAlign = wxALIGN_CENTRE; |
4c7277db | 2456 | m_rowLabelVertAlign = wxALIGN_CENTRE; |
f85afd4e | 2457 | |
4c7277db | 2458 | m_colLabelHorizAlign = wxALIGN_CENTRE; |
73145b0e | 2459 | m_colLabelVertAlign = wxALIGN_CENTRE; |
d43851f7 | 2460 | m_colLabelTextOrientation = wxHORIZONTAL; |
f85afd4e | 2461 | |
f85afd4e | 2462 | m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH; |
ad805b9e | 2463 | m_defaultRowHeight = 0; // this will be initialized after creation |
1f1ce288 | 2464 | |
b8d24d4e RG |
2465 | m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH; |
2466 | m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT; | |
2467 | ||
73145b0e | 2468 | m_gridLineColour = wxColour( 192,192,192 ); |
ca65c044 | 2469 | m_gridLinesEnabled = true; |
9f7aee01 VZ |
2470 | m_gridLinesClipHorz = |
2471 | m_gridLinesClipVert = true; | |
73145b0e | 2472 | m_cellHighlightColour = *wxBLACK; |
bf7945ce | 2473 | m_cellHighlightPenWidth = 2; |
d2520c85 | 2474 | m_cellHighlightROPenWidth = 1; |
8f177c8e | 2475 | |
d4175745 VZ |
2476 | m_canDragColMove = false; |
2477 | ||
58dd5b3b | 2478 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
10a4531d | 2479 | m_winCapture = NULL; |
ca65c044 WS |
2480 | m_canDragRowSize = true; |
2481 | m_canDragColSize = true; | |
2482 | m_canDragGridSize = true; | |
79dbea21 | 2483 | m_canDragCell = false; |
f85afd4e MB |
2484 | m_dragLastPos = -1; |
2485 | m_dragRowOrCol = -1; | |
ca65c044 | 2486 | m_isDragging = false; |
07296f0b | 2487 | m_startDragPos = wxDefaultPosition; |
ad805b9e | 2488 | |
11393d29 VZ |
2489 | m_sortCol = wxNOT_FOUND; |
2490 | m_sortIsAscending = true; | |
2491 | ||
ad805b9e | 2492 | m_useNativeHeader = |
71cf399f | 2493 | m_nativeColumnLabels = false; |
f85afd4e | 2494 | |
ca65c044 | 2495 | m_waitForSlowClick = false; |
025562fe | 2496 | |
f85afd4e MB |
2497 | m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS ); |
2498 | m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE ); | |
2499 | ||
2500 | m_currentCellCoords = wxGridNoCellCoords; | |
f85afd4e | 2501 | |
ad805b9e VZ |
2502 | m_selectedBlockTopLeft = |
2503 | m_selectedBlockBottomRight = | |
2504 | m_selectedBlockCorner = wxGridNoCellCoords; | |
b524b5c6 | 2505 | |
d43851f7 JS |
2506 | m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); |
2507 | m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
8f177c8e | 2508 | |
ca65c044 | 2509 | m_editable = true; // default for whole grid |
f85afd4e | 2510 | |
ca65c044 | 2511 | m_inOnKeyDown = false; |
2d66e025 | 2512 | m_batchCount = 0; |
3c79cf49 | 2513 | |
266e8367 | 2514 | m_extraWidth = |
526dbb95 | 2515 | m_extraHeight = 0; |
608754c4 | 2516 | |
fd76c6a7 VZ |
2517 | // we can't call SetScrollRate() as the window isn't created yet but OTOH |
2518 | // we don't need to call it neither as the scroll position is (0, 0) right | |
2519 | // now anyhow, so just set the parameters directly | |
2520 | m_xScrollPixelsPerLine = GRID_SCROLL_LINE_X; | |
2521 | m_yScrollPixelsPerLine = GRID_SCROLL_LINE_Y; | |
1dc17bca VZ |
2522 | |
2523 | m_tabBehaviour = Tab_Stop; | |
7c1cb261 VZ |
2524 | } |
2525 | ||
2526 | // ---------------------------------------------------------------------------- | |
2527 | // the idea is to call these functions only when necessary because they create | |
2528 | // quite big arrays which eat memory mostly unnecessary - in particular, if | |
2529 | // default widths/heights are used for all rows/columns, we may not use these | |
2530 | // arrays at all | |
2531 | // | |
0ed3b812 VZ |
2532 | // with some extra code, it should be possible to only store the widths/heights |
2533 | // different from default ones (resulting in space savings for huge grids) but | |
2534 | // this is not done currently | |
7c1cb261 VZ |
2535 | // ---------------------------------------------------------------------------- |
2536 | ||
2537 | void wxGrid::InitRowHeights() | |
2538 | { | |
2539 | m_rowHeights.Empty(); | |
2540 | m_rowBottoms.Empty(); | |
2541 | ||
2542 | m_rowHeights.Alloc( m_numRows ); | |
2543 | m_rowBottoms.Alloc( m_numRows ); | |
2544 | ||
27f35b66 SN |
2545 | m_rowHeights.Add( m_defaultRowHeight, m_numRows ); |
2546 | ||
0ed3b812 | 2547 | int rowBottom = 0; |
3d59537f | 2548 | for ( int i = 0; i < m_numRows; i++ ) |
7c1cb261 | 2549 | { |
7c1cb261 VZ |
2550 | rowBottom += m_defaultRowHeight; |
2551 | m_rowBottoms.Add( rowBottom ); | |
2552 | } | |
2553 | } | |
2554 | ||
2555 | void wxGrid::InitColWidths() | |
2556 | { | |
2557 | m_colWidths.Empty(); | |
2558 | m_colRights.Empty(); | |
2559 | ||
2560 | m_colWidths.Alloc( m_numCols ); | |
2561 | m_colRights.Alloc( m_numCols ); | |
27f35b66 SN |
2562 | |
2563 | m_colWidths.Add( m_defaultColWidth, m_numCols ); | |
2564 | ||
3d59537f | 2565 | for ( int i = 0; i < m_numCols; i++ ) |
7c1cb261 | 2566 | { |
e822d1bd | 2567 | int colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth; |
7c1cb261 VZ |
2568 | m_colRights.Add( colRight ); |
2569 | } | |
2570 | } | |
2571 | ||
2572 | int wxGrid::GetColWidth(int col) const | |
2573 | { | |
30496905 VZ |
2574 | if ( m_colWidths.IsEmpty() ) |
2575 | return m_defaultColWidth; | |
2576 | ||
2577 | // a negative width indicates a hidden column | |
2578 | return m_colWidths[col] > 0 ? m_colWidths[col] : 0; | |
7c1cb261 VZ |
2579 | } |
2580 | ||
2581 | int wxGrid::GetColLeft(int col) const | |
2582 | { | |
30496905 VZ |
2583 | if ( m_colRights.IsEmpty() ) |
2584 | return GetColPos( col ) * m_defaultColWidth; | |
2585 | ||
2586 | return m_colRights[col] - GetColWidth(col); | |
7c1cb261 VZ |
2587 | } |
2588 | ||
2589 | int wxGrid::GetColRight(int col) const | |
2590 | { | |
d4175745 | 2591 | return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth |
7c1cb261 VZ |
2592 | : m_colRights[col]; |
2593 | } | |
2594 | ||
2595 | int wxGrid::GetRowHeight(int row) const | |
2596 | { | |
30496905 VZ |
2597 | // no custom heights / hidden rows |
2598 | if ( m_rowHeights.IsEmpty() ) | |
2599 | return m_defaultRowHeight; | |
2600 | ||
2601 | // a negative height indicates a hidden row | |
2602 | return m_rowHeights[row] > 0 ? m_rowHeights[row] : 0; | |
7c1cb261 | 2603 | } |
2d66e025 | 2604 | |
7c1cb261 VZ |
2605 | int wxGrid::GetRowTop(int row) const |
2606 | { | |
30496905 VZ |
2607 | if ( m_rowBottoms.IsEmpty() ) |
2608 | return row * m_defaultRowHeight; | |
2609 | ||
2610 | return m_rowBottoms[row] - GetRowHeight(row); | |
f85afd4e MB |
2611 | } |
2612 | ||
7c1cb261 VZ |
2613 | int wxGrid::GetRowBottom(int row) const |
2614 | { | |
2615 | return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight | |
2616 | : m_rowBottoms[row]; | |
2617 | } | |
f85afd4e MB |
2618 | |
2619 | void wxGrid::CalcDimensions() | |
2620 | { | |
0ed3b812 VZ |
2621 | // compute the size of the scrollable area |
2622 | int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0; | |
2623 | int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0; | |
60ff3b99 | 2624 | |
0ed3b812 VZ |
2625 | w += m_extraWidth; |
2626 | h += m_extraHeight; | |
faec5a43 | 2627 | |
73145b0e | 2628 | // take into account editor if shown |
4db6714b | 2629 | if ( IsCellEditControlShown() ) |
73145b0e | 2630 | { |
3d59537f DS |
2631 | int w2, h2; |
2632 | int r = m_currentCellCoords.GetRow(); | |
2633 | int c = m_currentCellCoords.GetCol(); | |
2634 | int x = GetColLeft(c); | |
2635 | int y = GetRowTop(r); | |
2636 | ||
2637 | // how big is the editor | |
2638 | wxGridCellAttr* attr = GetCellAttr(r, c); | |
2639 | wxGridCellEditor* editor = attr->GetEditor(this, r, c); | |
2640 | editor->GetControl()->GetSize(&w2, &h2); | |
2641 | w2 += x; | |
2642 | h2 += y; | |
2643 | if ( w2 > w ) | |
2644 | w = w2; | |
2645 | if ( h2 > h ) | |
2646 | h = h2; | |
2647 | editor->DecRef(); | |
2648 | attr->DecRef(); | |
73145b0e JS |
2649 | } |
2650 | ||
faec5a43 SN |
2651 | // preserve (more or less) the previous position |
2652 | int x, y; | |
2653 | GetViewStart( &x, &y ); | |
97a9929e | 2654 | |
c92ed9f7 | 2655 | // ensure the position is valid for the new scroll ranges |
7b519e5e | 2656 | if ( x >= w ) |
c92ed9f7 | 2657 | x = wxMax( w - 1, 0 ); |
7b519e5e | 2658 | if ( y >= h ) |
c92ed9f7 | 2659 | y = wxMax( h - 1, 0 ); |
faec5a43 | 2660 | |
ace8d849 | 2661 | // update the virtual size and refresh the scrollbars to reflect it |
f1ff7df0 VZ |
2662 | m_gridWin->SetVirtualSize(w, h); |
2663 | Scroll(x, y); | |
ace8d849 | 2664 | AdjustScrollbars(); |
12314291 VZ |
2665 | |
2666 | // if our OnSize() hadn't been called (it would if we have scrollbars), we | |
2667 | // still must reposition the children | |
2668 | CalcWindowSizes(); | |
f85afd4e MB |
2669 | } |
2670 | ||
69367c56 VZ |
2671 | wxSize wxGrid::GetSizeAvailableForScrollTarget(const wxSize& size) |
2672 | { | |
2673 | wxSize sizeGridWin(size); | |
2674 | sizeGridWin.x -= m_rowLabelWidth; | |
2675 | sizeGridWin.y -= m_colLabelHeight; | |
2676 | ||
2677 | return sizeGridWin; | |
2678 | } | |
2679 | ||
7807d81c MB |
2680 | void wxGrid::CalcWindowSizes() |
2681 | { | |
b0a877ec SC |
2682 | // escape if the window is has not been fully created yet |
2683 | ||
2684 | if ( m_cornerLabelWin == NULL ) | |
2f024384 | 2685 | return; |
b0a877ec | 2686 | |
7807d81c MB |
2687 | int cw, ch; |
2688 | GetClientSize( &cw, &ch ); | |
b99be8fb | 2689 | |
c1841ac2 VZ |
2690 | // the grid may be too small to have enough space for the labels yet, don't |
2691 | // size the windows to negative sizes in this case | |
2692 | int gw = cw - m_rowLabelWidth; | |
2693 | int gh = ch - m_colLabelHeight; | |
2694 | if (gw < 0) | |
2695 | gw = 0; | |
2696 | if (gh < 0) | |
2697 | gh = 0; | |
2698 | ||
6d308072 | 2699 | if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() ) |
7807d81c MB |
2700 | m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight ); |
2701 | ||
ad805b9e VZ |
2702 | if ( m_colWindow && m_colWindow->IsShown() ) |
2703 | m_colWindow->SetSize( m_rowLabelWidth, 0, gw, m_colLabelHeight ); | |
7807d81c | 2704 | |
6d308072 | 2705 | if ( m_rowLabelWin && m_rowLabelWin->IsShown() ) |
c1841ac2 | 2706 | m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, gh ); |
7807d81c | 2707 | |
6d308072 | 2708 | if ( m_gridWin && m_gridWin->IsShown() ) |
c1841ac2 | 2709 | m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, gw, gh ); |
7807d81c MB |
2710 | } |
2711 | ||
3d59537f DS |
2712 | // this is called when the grid table sends a message |
2713 | // to indicate that it has been redimensioned | |
f85afd4e MB |
2714 | // |
2715 | bool wxGrid::Redimension( wxGridTableMessage& msg ) | |
2716 | { | |
2717 | int i; | |
ca65c044 | 2718 | bool result = false; |
8f177c8e | 2719 | |
a6794685 SN |
2720 | // Clear the attribute cache as the attribute might refer to a different |
2721 | // cell than stored in the cache after adding/removing rows/columns. | |
2722 | ClearAttrCache(); | |
2f024384 | 2723 | |
7e48d7d9 SN |
2724 | // By the same reasoning, the editor should be dismissed if columns are |
2725 | // added or removed. And for consistency, it should IMHO always be | |
2726 | // removed, not only if the cell "underneath" it actually changes. | |
2727 | // For now, I intentionally do not save the editor's content as the | |
2728 | // cell it might want to save that stuff to might no longer exist. | |
bca7bfc8 | 2729 | HideCellEditControl(); |
2f024384 | 2730 | |
f85afd4e MB |
2731 | switch ( msg.GetId() ) |
2732 | { | |
2733 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
2734 | { | |
2735 | size_t pos = msg.GetCommandInt(); | |
2736 | int numRows = msg.GetCommandInt2(); | |
f6bcfd97 | 2737 | |
f85afd4e | 2738 | m_numRows += numRows; |
2d66e025 | 2739 | |
f6bcfd97 BP |
2740 | if ( !m_rowHeights.IsEmpty() ) |
2741 | { | |
27f35b66 SN |
2742 | m_rowHeights.Insert( m_defaultRowHeight, pos, numRows ); |
2743 | m_rowBottoms.Insert( 0, pos, numRows ); | |
f6bcfd97 BP |
2744 | |
2745 | int bottom = 0; | |
2f024384 DS |
2746 | if ( pos > 0 ) |
2747 | bottom = m_rowBottoms[pos - 1]; | |
60ff3b99 | 2748 | |
3d59537f | 2749 | for ( i = pos; i < m_numRows; i++ ) |
f6bcfd97 BP |
2750 | { |
2751 | bottom += m_rowHeights[i]; | |
2752 | m_rowBottoms[i] = bottom; | |
2753 | } | |
2754 | } | |
2f024384 | 2755 | |
f6bcfd97 BP |
2756 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2757 | { | |
2758 | // if we have just inserted cols into an empty grid the current | |
2759 | // cell will be undefined... | |
2760 | // | |
2761 | SetCurrentCell( 0, 0 ); | |
2762 | } | |
3f3dc2ef VZ |
2763 | |
2764 | if ( m_selection ) | |
2765 | m_selection->UpdateRows( pos, numRows ); | |
f6bcfd97 BP |
2766 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
2767 | if (attrProvider) | |
2768 | attrProvider->UpdateAttrRows( pos, numRows ); | |
2769 | ||
2770 | if ( !GetBatchCount() ) | |
2d66e025 | 2771 | { |
f6bcfd97 BP |
2772 | CalcDimensions(); |
2773 | m_rowLabelWin->Refresh(); | |
2d66e025 | 2774 | } |
f85afd4e | 2775 | } |
ca65c044 | 2776 | result = true; |
f6bcfd97 | 2777 | break; |
f85afd4e MB |
2778 | |
2779 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
2780 | { | |
2781 | int numRows = msg.GetCommandInt(); | |
2d66e025 | 2782 | int oldNumRows = m_numRows; |
f85afd4e | 2783 | m_numRows += numRows; |
2d66e025 | 2784 | |
f6bcfd97 BP |
2785 | if ( !m_rowHeights.IsEmpty() ) |
2786 | { | |
27f35b66 SN |
2787 | m_rowHeights.Add( m_defaultRowHeight, numRows ); |
2788 | m_rowBottoms.Add( 0, numRows ); | |
60ff3b99 | 2789 | |
f6bcfd97 | 2790 | int bottom = 0; |
2f024384 DS |
2791 | if ( oldNumRows > 0 ) |
2792 | bottom = m_rowBottoms[oldNumRows - 1]; | |
f6bcfd97 | 2793 | |
3d59537f | 2794 | for ( i = oldNumRows; i < m_numRows; i++ ) |
f6bcfd97 BP |
2795 | { |
2796 | bottom += m_rowHeights[i]; | |
2797 | m_rowBottoms[i] = bottom; | |
2798 | } | |
2799 | } | |
2f024384 | 2800 | |
f6bcfd97 BP |
2801 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2802 | { | |
2803 | // if we have just inserted cols into an empty grid the current | |
2804 | // cell will be undefined... | |
2805 | // | |
2806 | SetCurrentCell( 0, 0 ); | |
2807 | } | |
2f024384 | 2808 | |
f6bcfd97 | 2809 | if ( !GetBatchCount() ) |
2d66e025 | 2810 | { |
f6bcfd97 BP |
2811 | CalcDimensions(); |
2812 | m_rowLabelWin->Refresh(); | |
2d66e025 | 2813 | } |
f85afd4e | 2814 | } |
ca65c044 | 2815 | result = true; |
f6bcfd97 | 2816 | break; |
f85afd4e MB |
2817 | |
2818 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
2819 | { | |
2820 | size_t pos = msg.GetCommandInt(); | |
2821 | int numRows = msg.GetCommandInt2(); | |
f85afd4e MB |
2822 | m_numRows -= numRows; |
2823 | ||
f6bcfd97 | 2824 | if ( !m_rowHeights.IsEmpty() ) |
f85afd4e | 2825 | { |
27f35b66 SN |
2826 | m_rowHeights.RemoveAt( pos, numRows ); |
2827 | m_rowBottoms.RemoveAt( pos, numRows ); | |
2d66e025 MB |
2828 | |
2829 | int h = 0; | |
3d59537f | 2830 | for ( i = 0; i < m_numRows; i++ ) |
2d66e025 MB |
2831 | { |
2832 | h += m_rowHeights[i]; | |
2833 | m_rowBottoms[i] = h; | |
2834 | } | |
f85afd4e | 2835 | } |
3d59537f | 2836 | |
f6bcfd97 BP |
2837 | if ( !m_numRows ) |
2838 | { | |
2839 | m_currentCellCoords = wxGridNoCellCoords; | |
2840 | } | |
2841 | else | |
2842 | { | |
2843 | if ( m_currentCellCoords.GetRow() >= m_numRows ) | |
2844 | m_currentCellCoords.Set( 0, 0 ); | |
2845 | } | |
3f3dc2ef VZ |
2846 | |
2847 | if ( m_selection ) | |
2848 | m_selection->UpdateRows( pos, -((int)numRows) ); | |
f6bcfd97 | 2849 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4db6714b KH |
2850 | if (attrProvider) |
2851 | { | |
f6bcfd97 | 2852 | attrProvider->UpdateAttrRows( pos, -((int)numRows) ); |
3d59537f | 2853 | |
84912ef8 RD |
2854 | // ifdef'd out following patch from Paul Gammans |
2855 | #if 0 | |
3ca6a5f0 | 2856 | // No need to touch column attributes, unless we |
f6bcfd97 BP |
2857 | // removed _all_ rows, in this case, we remove |
2858 | // all column attributes. | |
2859 | // I hate to do this here, but the | |
2860 | // needed data is not available inside UpdateAttrRows. | |
2861 | if ( !GetNumberRows() ) | |
2862 | attrProvider->UpdateAttrCols( 0, -GetNumberCols() ); | |
84912ef8 | 2863 | #endif |
f6bcfd97 | 2864 | } |
ccdee36f | 2865 | |
f6bcfd97 BP |
2866 | if ( !GetBatchCount() ) |
2867 | { | |
2868 | CalcDimensions(); | |
2869 | m_rowLabelWin->Refresh(); | |
2870 | } | |
f85afd4e | 2871 | } |
ca65c044 | 2872 | result = true; |
f6bcfd97 | 2873 | break; |
f85afd4e MB |
2874 | |
2875 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
2876 | { | |
2877 | size_t pos = msg.GetCommandInt(); | |
2878 | int numCols = msg.GetCommandInt2(); | |
f85afd4e | 2879 | m_numCols += numCols; |
2d66e025 | 2880 | |
ad805b9e | 2881 | if ( m_useNativeHeader ) |
3039ade9 | 2882 | GetGridColHeader()->SetColumnCount(m_numCols); |
ad805b9e | 2883 | |
d4175745 VZ |
2884 | if ( !m_colAt.IsEmpty() ) |
2885 | { | |
2886 | //Shift the column IDs | |
d4175745 VZ |
2887 | for ( i = 0; i < m_numCols - numCols; i++ ) |
2888 | { | |
2889 | if ( m_colAt[i] >= (int)pos ) | |
2890 | m_colAt[i] += numCols; | |
2891 | } | |
2892 | ||
2893 | m_colAt.Insert( pos, pos, numCols ); | |
2894 | ||
2895 | //Set the new columns' positions | |
2896 | for ( i = pos + 1; i < (int)pos + numCols; i++ ) | |
2897 | { | |
2898 | m_colAt[i] = i; | |
2899 | } | |
2900 | } | |
2901 | ||
f6bcfd97 BP |
2902 | if ( !m_colWidths.IsEmpty() ) |
2903 | { | |
27f35b66 SN |
2904 | m_colWidths.Insert( m_defaultColWidth, pos, numCols ); |
2905 | m_colRights.Insert( 0, pos, numCols ); | |
f6bcfd97 BP |
2906 | |
2907 | int right = 0; | |
2f024384 | 2908 | if ( pos > 0 ) |
d4175745 | 2909 | right = m_colRights[GetColAt( pos - 1 )]; |
60ff3b99 | 2910 | |
d4175745 VZ |
2911 | int colPos; |
2912 | for ( colPos = pos; colPos < m_numCols; colPos++ ) | |
f6bcfd97 | 2913 | { |
d4175745 VZ |
2914 | i = GetColAt( colPos ); |
2915 | ||
f6bcfd97 BP |
2916 | right += m_colWidths[i]; |
2917 | m_colRights[i] = right; | |
2918 | } | |
2919 | } | |
2f024384 | 2920 | |
f6bcfd97 | 2921 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2d66e025 | 2922 | { |
f6bcfd97 BP |
2923 | // if we have just inserted cols into an empty grid the current |
2924 | // cell will be undefined... | |
2925 | // | |
2926 | SetCurrentCell( 0, 0 ); | |
2d66e025 | 2927 | } |
3f3dc2ef VZ |
2928 | |
2929 | if ( m_selection ) | |
2930 | m_selection->UpdateCols( pos, numCols ); | |
f6bcfd97 BP |
2931 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
2932 | if (attrProvider) | |
2933 | attrProvider->UpdateAttrCols( pos, numCols ); | |
2934 | if ( !GetBatchCount() ) | |
2935 | { | |
2936 | CalcDimensions(); | |
ad805b9e | 2937 | m_colWindow->Refresh(); |
f6bcfd97 | 2938 | } |
f85afd4e | 2939 | } |
ca65c044 | 2940 | result = true; |
f6bcfd97 | 2941 | break; |
f85afd4e MB |
2942 | |
2943 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
2944 | { | |
2945 | int numCols = msg.GetCommandInt(); | |
2d66e025 | 2946 | int oldNumCols = m_numCols; |
f85afd4e | 2947 | m_numCols += numCols; |
ad805b9e | 2948 | if ( m_useNativeHeader ) |
3039ade9 | 2949 | GetGridColHeader()->SetColumnCount(m_numCols); |
d4175745 VZ |
2950 | |
2951 | if ( !m_colAt.IsEmpty() ) | |
2952 | { | |
2953 | m_colAt.Add( 0, numCols ); | |
2954 | ||
2955 | //Set the new columns' positions | |
d4175745 VZ |
2956 | for ( i = oldNumCols; i < m_numCols; i++ ) |
2957 | { | |
2958 | m_colAt[i] = i; | |
2959 | } | |
2960 | } | |
2961 | ||
f6bcfd97 BP |
2962 | if ( !m_colWidths.IsEmpty() ) |
2963 | { | |
27f35b66 SN |
2964 | m_colWidths.Add( m_defaultColWidth, numCols ); |
2965 | m_colRights.Add( 0, numCols ); | |
2d66e025 | 2966 | |
f6bcfd97 | 2967 | int right = 0; |
2f024384 | 2968 | if ( oldNumCols > 0 ) |
d4175745 | 2969 | right = m_colRights[GetColAt( oldNumCols - 1 )]; |
60ff3b99 | 2970 | |
d4175745 VZ |
2971 | int colPos; |
2972 | for ( colPos = oldNumCols; colPos < m_numCols; colPos++ ) | |
f6bcfd97 | 2973 | { |
d4175745 VZ |
2974 | i = GetColAt( colPos ); |
2975 | ||
f6bcfd97 BP |
2976 | right += m_colWidths[i]; |
2977 | m_colRights[i] = right; | |
2978 | } | |
2979 | } | |
2f024384 | 2980 | |
f6bcfd97 | 2981 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2d66e025 | 2982 | { |
f6bcfd97 BP |
2983 | // if we have just inserted cols into an empty grid the current |
2984 | // cell will be undefined... | |
2985 | // | |
2986 | SetCurrentCell( 0, 0 ); | |
2987 | } | |
2988 | if ( !GetBatchCount() ) | |
2989 | { | |
2990 | CalcDimensions(); | |
ad805b9e | 2991 | m_colWindow->Refresh(); |
2d66e025 | 2992 | } |
f85afd4e | 2993 | } |
ca65c044 | 2994 | result = true; |
f6bcfd97 | 2995 | break; |
f85afd4e MB |
2996 | |
2997 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
2998 | { | |
2999 | size_t pos = msg.GetCommandInt(); | |
3000 | int numCols = msg.GetCommandInt2(); | |
f85afd4e | 3001 | m_numCols -= numCols; |
ad805b9e | 3002 | if ( m_useNativeHeader ) |
3039ade9 | 3003 | GetGridColHeader()->SetColumnCount(m_numCols); |
f85afd4e | 3004 | |
d4175745 VZ |
3005 | if ( !m_colAt.IsEmpty() ) |
3006 | { | |
3007 | int colID = GetColAt( pos ); | |
3008 | ||
3009 | m_colAt.RemoveAt( pos, numCols ); | |
3010 | ||
3011 | //Shift the column IDs | |
3012 | int colPos; | |
3013 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
3014 | { | |
3015 | if ( m_colAt[colPos] > colID ) | |
3016 | m_colAt[colPos] -= numCols; | |
3017 | } | |
3018 | } | |
3019 | ||
f6bcfd97 | 3020 | if ( !m_colWidths.IsEmpty() ) |
f85afd4e | 3021 | { |
27f35b66 SN |
3022 | m_colWidths.RemoveAt( pos, numCols ); |
3023 | m_colRights.RemoveAt( pos, numCols ); | |
2d66e025 MB |
3024 | |
3025 | int w = 0; | |
d4175745 VZ |
3026 | int colPos; |
3027 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
2d66e025 | 3028 | { |
d4175745 VZ |
3029 | i = GetColAt( colPos ); |
3030 | ||
2d66e025 MB |
3031 | w += m_colWidths[i]; |
3032 | m_colRights[i] = w; | |
3033 | } | |
f85afd4e | 3034 | } |
2f024384 | 3035 | |
f6bcfd97 BP |
3036 | if ( !m_numCols ) |
3037 | { | |
3038 | m_currentCellCoords = wxGridNoCellCoords; | |
3039 | } | |
3040 | else | |
3041 | { | |
3042 | if ( m_currentCellCoords.GetCol() >= m_numCols ) | |
3043 | m_currentCellCoords.Set( 0, 0 ); | |
3044 | } | |
3f3dc2ef VZ |
3045 | |
3046 | if ( m_selection ) | |
3047 | m_selection->UpdateCols( pos, -((int)numCols) ); | |
f6bcfd97 | 3048 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4db6714b KH |
3049 | if (attrProvider) |
3050 | { | |
f6bcfd97 | 3051 | attrProvider->UpdateAttrCols( pos, -((int)numCols) ); |
ccdee36f | 3052 | |
84912ef8 RD |
3053 | // ifdef'd out following patch from Paul Gammans |
3054 | #if 0 | |
f6bcfd97 BP |
3055 | // No need to touch row attributes, unless we |
3056 | // removed _all_ columns, in this case, we remove | |
3057 | // all row attributes. | |
3058 | // I hate to do this here, but the | |
3059 | // needed data is not available inside UpdateAttrCols. | |
3060 | if ( !GetNumberCols() ) | |
3061 | attrProvider->UpdateAttrRows( 0, -GetNumberRows() ); | |
84912ef8 | 3062 | #endif |
f6bcfd97 | 3063 | } |
ccdee36f | 3064 | |
f6bcfd97 BP |
3065 | if ( !GetBatchCount() ) |
3066 | { | |
3067 | CalcDimensions(); | |
ad805b9e | 3068 | m_colWindow->Refresh(); |
f6bcfd97 | 3069 | } |
f85afd4e | 3070 | } |
ca65c044 | 3071 | result = true; |
f6bcfd97 | 3072 | break; |
f85afd4e MB |
3073 | } |
3074 | ||
a6b40a9a VZ |
3075 | InvalidateBestSize(); |
3076 | ||
f6bcfd97 BP |
3077 | if (result && !GetBatchCount() ) |
3078 | m_gridWin->Refresh(); | |
2f024384 | 3079 | |
f6bcfd97 | 3080 | return result; |
f85afd4e MB |
3081 | } |
3082 | ||
ef316e23 | 3083 | wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const |
f85afd4e | 3084 | { |
2d66e025 MB |
3085 | wxRegionIterator iter( reg ); |
3086 | wxRect r; | |
f85afd4e | 3087 | |
275c4ae4 RD |
3088 | wxArrayInt rowlabels; |
3089 | ||
2d66e025 MB |
3090 | int top, bottom; |
3091 | while ( iter ) | |
f85afd4e | 3092 | { |
2d66e025 | 3093 | r = iter.GetRect(); |
f85afd4e | 3094 | |
2d66e025 MB |
3095 | // TODO: remove this when we can... |
3096 | // There is a bug in wxMotif that gives garbage update | |
3097 | // rectangles if you jump-scroll a long way by clicking the | |
3098 | // scrollbar with middle button. This is a work-around | |
3099 | // | |
3100 | #if defined(__WXMOTIF__) | |
3101 | int cw, ch; | |
3102 | m_gridWin->GetClientSize( &cw, &ch ); | |
56b6cf26 DS |
3103 | if ( r.GetTop() > ch ) |
3104 | r.SetTop( 0 ); | |
2d66e025 MB |
3105 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); |
3106 | #endif | |
f85afd4e | 3107 | |
2d66e025 MB |
3108 | // logical bounds of update region |
3109 | // | |
3110 | int dummy; | |
3111 | CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top ); | |
3112 | CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom ); | |
3113 | ||
3114 | // find the row labels within these bounds | |
3115 | // | |
3116 | int row; | |
3d59537f | 3117 | for ( row = internalYToRow(top); row < m_numRows; row++ ) |
2d66e025 | 3118 | { |
7c1cb261 VZ |
3119 | if ( GetRowBottom(row) < top ) |
3120 | continue; | |
2d66e025 | 3121 | |
6d55126d | 3122 | if ( GetRowTop(row) > bottom ) |
7c1cb261 | 3123 | break; |
60ff3b99 | 3124 | |
d10f4bf9 | 3125 | rowlabels.Add( row ); |
2d66e025 | 3126 | } |
60ff3b99 | 3127 | |
60d8e886 | 3128 | ++iter; |
f85afd4e | 3129 | } |
d10f4bf9 VZ |
3130 | |
3131 | return rowlabels; | |
f85afd4e MB |
3132 | } |
3133 | ||
ef316e23 | 3134 | wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const |
f85afd4e | 3135 | { |
2d66e025 MB |
3136 | wxRegionIterator iter( reg ); |
3137 | wxRect r; | |
f85afd4e | 3138 | |
d10f4bf9 | 3139 | wxArrayInt colLabels; |
f85afd4e | 3140 | |
2d66e025 MB |
3141 | int left, right; |
3142 | while ( iter ) | |
f85afd4e | 3143 | { |
2d66e025 | 3144 | r = iter.GetRect(); |
f85afd4e | 3145 | |
2d66e025 MB |
3146 | // TODO: remove this when we can... |
3147 | // There is a bug in wxMotif that gives garbage update | |
3148 | // rectangles if you jump-scroll a long way by clicking the | |
3149 | // scrollbar with middle button. This is a work-around | |
3150 | // | |
3151 | #if defined(__WXMOTIF__) | |
3152 | int cw, ch; | |
3153 | m_gridWin->GetClientSize( &cw, &ch ); | |
56b6cf26 DS |
3154 | if ( r.GetLeft() > cw ) |
3155 | r.SetLeft( 0 ); | |
2d66e025 MB |
3156 | r.SetRight( wxMin( r.GetRight(), cw ) ); |
3157 | #endif | |
3158 | ||
3159 | // logical bounds of update region | |
3160 | // | |
3161 | int dummy; | |
3162 | CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy ); | |
3163 | CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy ); | |
3164 | ||
3165 | // find the cells within these bounds | |
3166 | // | |
3167 | int col; | |
d4175745 VZ |
3168 | int colPos; |
3169 | for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ ) | |
2d66e025 | 3170 | { |
d4175745 VZ |
3171 | col = GetColAt( colPos ); |
3172 | ||
7c1cb261 VZ |
3173 | if ( GetColRight(col) < left ) |
3174 | continue; | |
60ff3b99 | 3175 | |
7c1cb261 VZ |
3176 | if ( GetColLeft(col) > right ) |
3177 | break; | |
2d66e025 | 3178 | |
d10f4bf9 | 3179 | colLabels.Add( col ); |
2d66e025 | 3180 | } |
60ff3b99 | 3181 | |
60d8e886 | 3182 | ++iter; |
f85afd4e | 3183 | } |
2f024384 | 3184 | |
d10f4bf9 | 3185 | return colLabels; |
f85afd4e MB |
3186 | } |
3187 | ||
ef316e23 | 3188 | wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const |
f85afd4e | 3189 | { |
2d66e025 | 3190 | wxRect r; |
f85afd4e | 3191 | |
d10f4bf9 | 3192 | wxGridCellCoordsArray cellsExposed; |
f85afd4e | 3193 | |
2d66e025 | 3194 | int left, top, right, bottom; |
9e57072a | 3195 | for ( wxRegionIterator iter(reg); iter; ++iter ) |
2d66e025 MB |
3196 | { |
3197 | r = iter.GetRect(); | |
f85afd4e | 3198 | |
9e57072a VZ |
3199 | // Skip 0-height cells, they're invisible anyhow, don't waste time |
3200 | // getting their rectangles and so on. | |
3201 | if ( !r.GetHeight() ) | |
3202 | continue; | |
3203 | ||
2d66e025 MB |
3204 | // TODO: remove this when we can... |
3205 | // There is a bug in wxMotif that gives garbage update | |
3206 | // rectangles if you jump-scroll a long way by clicking the | |
3207 | // scrollbar with middle button. This is a work-around | |
3208 | // | |
3209 | #if defined(__WXMOTIF__) | |
f85afd4e | 3210 | int cw, ch; |
2d66e025 MB |
3211 | m_gridWin->GetClientSize( &cw, &ch ); |
3212 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
3213 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
3214 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
3215 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
3216 | #endif | |
8f177c8e | 3217 | |
2d66e025 MB |
3218 | // logical bounds of update region |
3219 | // | |
3220 | CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
3221 | CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
f85afd4e | 3222 | |
2d66e025 | 3223 | // find the cells within these bounds |
cd4f6f5f VZ |
3224 | wxArrayInt cols; |
3225 | for ( int row = internalYToRow(top); row < m_numRows; row++ ) | |
f85afd4e | 3226 | { |
7c1cb261 VZ |
3227 | if ( GetRowBottom(row) <= top ) |
3228 | continue; | |
f85afd4e | 3229 | |
7c1cb261 VZ |
3230 | if ( GetRowTop(row) > bottom ) |
3231 | break; | |
60ff3b99 | 3232 | |
cd4f6f5f VZ |
3233 | // add all dirty cells in this row: notice that the columns which |
3234 | // are dirty don't depend on the row so we compute them only once | |
3235 | // for the first dirty row and then reuse for all the next ones | |
3236 | if ( cols.empty() ) | |
2d66e025 | 3237 | { |
cd4f6f5f VZ |
3238 | // do determine the dirty columns |
3239 | for ( int pos = XToPos(left); pos <= XToPos(right); pos++ ) | |
3240 | cols.push_back(GetColAt(pos)); | |
d4175745 | 3241 | |
cd4f6f5f VZ |
3242 | // if there are no dirty columns at all, nothing to do |
3243 | if ( cols.empty() ) | |
7c1cb261 | 3244 | break; |
2d66e025 | 3245 | } |
cd4f6f5f VZ |
3246 | |
3247 | const size_t count = cols.size(); | |
3248 | for ( size_t n = 0; n < count; n++ ) | |
3249 | cellsExposed.Add(wxGridCellCoords(row, cols[n])); | |
2d66e025 | 3250 | } |
f85afd4e | 3251 | } |
d10f4bf9 VZ |
3252 | |
3253 | return cellsExposed; | |
f85afd4e MB |
3254 | } |
3255 | ||
3256 | ||
2d66e025 | 3257 | void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 3258 | { |
2d66e025 MB |
3259 | int x, y, row; |
3260 | wxPoint pos( event.GetPosition() ); | |
3261 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 3262 | |
2d66e025 | 3263 | if ( event.Dragging() ) |
f85afd4e | 3264 | { |
426b2d87 JS |
3265 | if (!m_isDragging) |
3266 | { | |
ca65c044 | 3267 | m_isDragging = true; |
426b2d87 JS |
3268 | m_rowLabelWin->CaptureMouse(); |
3269 | } | |
8f177c8e | 3270 | |
2d66e025 | 3271 | if ( event.LeftIsDown() ) |
f85afd4e | 3272 | { |
962a48f6 | 3273 | switch ( m_cursorMode ) |
f85afd4e | 3274 | { |
f85afd4e MB |
3275 | case WXGRID_CURSOR_RESIZE_ROW: |
3276 | { | |
2d66e025 MB |
3277 | int cw, ch, left, dummy; |
3278 | m_gridWin->GetClientSize( &cw, &ch ); | |
3279 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
60ff3b99 | 3280 | |
2d66e025 MB |
3281 | wxClientDC dc( m_gridWin ); |
3282 | PrepareDC( dc ); | |
af547d51 VZ |
3283 | y = wxMax( y, |
3284 | GetRowTop(m_dragRowOrCol) + | |
3285 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
d2fdd8d2 | 3286 | dc.SetLogicalFunction(wxINVERT); |
f85afd4e MB |
3287 | if ( m_dragLastPos >= 0 ) |
3288 | { | |
2d66e025 | 3289 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); |
f85afd4e | 3290 | } |
2d66e025 MB |
3291 | dc.DrawLine( left, y, left+cw, y ); |
3292 | m_dragLastPos = y; | |
f85afd4e MB |
3293 | } |
3294 | break; | |
3295 | ||
3296 | case WXGRID_CURSOR_SELECT_ROW: | |
902725ee | 3297 | { |
e32352cf | 3298 | if ( (row = YToRow( y )) >= 0 ) |
aa5e1f75 | 3299 | { |
3f3dc2ef | 3300 | if ( m_selection ) |
8b5f6d9d | 3301 | m_selection->SelectRow(row, event); |
f85afd4e | 3302 | } |
902725ee WS |
3303 | } |
3304 | break; | |
e2b42eeb VZ |
3305 | |
3306 | // default label to suppress warnings about "enumeration value | |
3307 | // 'xxx' not handled in switch | |
3308 | default: | |
3309 | break; | |
f85afd4e MB |
3310 | } |
3311 | } | |
3312 | return; | |
3313 | } | |
3314 | ||
426b2d87 JS |
3315 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) |
3316 | return; | |
8f177c8e | 3317 | |
426b2d87 JS |
3318 | if (m_isDragging) |
3319 | { | |
ccdee36f DS |
3320 | if (m_rowLabelWin->HasCapture()) |
3321 | m_rowLabelWin->ReleaseMouse(); | |
ca65c044 | 3322 | m_isDragging = false; |
426b2d87 | 3323 | } |
60ff3b99 | 3324 | |
6d004f67 MB |
3325 | // ------------ Entering or leaving the window |
3326 | // | |
3327 | if ( event.Entering() || event.Leaving() ) | |
3328 | { | |
e2b42eeb | 3329 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
6d004f67 MB |
3330 | } |
3331 | ||
2d66e025 | 3332 | // ------------ Left button pressed |
f85afd4e | 3333 | // |
6d004f67 | 3334 | else if ( event.LeftDown() ) |
f85afd4e | 3335 | { |
82edfbe7 VZ |
3336 | row = YToEdgeOfRow(y); |
3337 | if ( row != wxNOT_FOUND && CanDragRowSize(row) ) | |
3338 | { | |
3339 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin); | |
3340 | } | |
3341 | else // not a request to start resizing | |
f85afd4e | 3342 | { |
2d66e025 | 3343 | row = YToRow(y); |
2f024384 | 3344 | if ( row >= 0 && |
b54ba671 | 3345 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) ) |
f85afd4e | 3346 | { |
2b01fd49 | 3347 | if ( !event.ShiftDown() && !event.CmdDown() ) |
aa5e1f75 | 3348 | ClearSelection(); |
64e15340 | 3349 | if ( m_selection ) |
3f3dc2ef VZ |
3350 | { |
3351 | if ( event.ShiftDown() ) | |
3352 | { | |
8b5f6d9d VZ |
3353 | m_selection->SelectBlock |
3354 | ( | |
3355 | m_currentCellCoords.GetRow(), 0, | |
3356 | row, GetNumberCols() - 1, | |
3357 | event | |
3358 | ); | |
3f3dc2ef VZ |
3359 | } |
3360 | else | |
3361 | { | |
8b5f6d9d | 3362 | m_selection->SelectRow(row, event); |
3f3dc2ef VZ |
3363 | } |
3364 | } | |
3365 | ||
e2b42eeb | 3366 | ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin); |
f85afd4e | 3367 | } |
2d66e025 | 3368 | } |
2d66e025 | 3369 | } |
f85afd4e | 3370 | |
2d66e025 MB |
3371 | // ------------ Left double click |
3372 | // | |
3373 | else if (event.LeftDClick() ) | |
3374 | { | |
4e115ed2 | 3375 | row = YToEdgeOfRow(y); |
82edfbe7 VZ |
3376 | if ( row != wxNOT_FOUND && CanDragRowSize(row) ) |
3377 | { | |
3378 | // adjust row height depending on label text | |
27bb2c8c VZ |
3379 | // |
3380 | // TODO: generate RESIZING event, see #10754 | |
82edfbe7 VZ |
3381 | AutoSizeRowLabelSize( row ); |
3382 | ||
c5c1ea96 | 3383 | SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, row, -1, event); |
27bb2c8c | 3384 | |
82edfbe7 VZ |
3385 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
3386 | m_dragLastPos = -1; | |
3387 | } | |
d13b34d3 | 3388 | else // not on row separator or it's not resizable |
2d66e025 MB |
3389 | { |
3390 | row = YToRow(y); | |
a967f048 | 3391 | if ( row >=0 && |
ca65c044 WS |
3392 | !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) ) |
3393 | { | |
a967f048 | 3394 | // no default action at the moment |
ca65c044 | 3395 | } |
f85afd4e MB |
3396 | } |
3397 | } | |
60ff3b99 | 3398 | |
2d66e025 | 3399 | // ------------ Left button released |
f85afd4e | 3400 | // |
2d66e025 | 3401 | else if ( event.LeftUp() ) |
f85afd4e | 3402 | { |
2d66e025 | 3403 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) |
27bb2c8c | 3404 | DoEndDragResizeRow(event); |
f85afd4e | 3405 | |
e2b42eeb VZ |
3406 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
3407 | m_dragLastPos = -1; | |
2d66e025 | 3408 | } |
f85afd4e | 3409 | |
2d66e025 MB |
3410 | // ------------ Right button down |
3411 | // | |
3412 | else if ( event.RightDown() ) | |
3413 | { | |
3414 | row = YToRow(y); | |
ef5df12b | 3415 | if ( row >=0 && |
ca65c044 | 3416 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) |
2d66e025 MB |
3417 | { |
3418 | // no default action at the moment | |
f85afd4e MB |
3419 | } |
3420 | } | |
60ff3b99 | 3421 | |
2d66e025 | 3422 | // ------------ Right double click |
f85afd4e | 3423 | // |
2d66e025 MB |
3424 | else if ( event.RightDClick() ) |
3425 | { | |
3426 | row = YToRow(y); | |
a967f048 | 3427 | if ( row >= 0 && |
ca65c044 | 3428 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) |
2d66e025 MB |
3429 | { |
3430 | // no default action at the moment | |
3431 | } | |
3432 | } | |
60ff3b99 | 3433 | |
2d66e025 | 3434 | // ------------ No buttons down and mouse moving |
f85afd4e | 3435 | // |
2d66e025 | 3436 | else if ( event.Moving() ) |
f85afd4e | 3437 | { |
2d66e025 | 3438 | m_dragRowOrCol = YToEdgeOfRow( y ); |
82edfbe7 | 3439 | if ( m_dragRowOrCol != wxNOT_FOUND ) |
8f177c8e | 3440 | { |
2d66e025 | 3441 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
8f177c8e | 3442 | { |
82edfbe7 | 3443 | if ( CanDragRowSize(m_dragRowOrCol) ) |
ca65c044 | 3444 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false); |
8f177c8e | 3445 | } |
2d66e025 | 3446 | } |
6d004f67 | 3447 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 3448 | { |
ca65c044 | 3449 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false); |
8f177c8e | 3450 | } |
f85afd4e | 3451 | } |
2d66e025 MB |
3452 | } |
3453 | ||
11393d29 VZ |
3454 | void wxGrid::UpdateColumnSortingIndicator(int col) |
3455 | { | |
3456 | wxCHECK_RET( col != wxNOT_FOUND, "invalid column index" ); | |
3457 | ||
3458 | if ( m_useNativeHeader ) | |
3039ade9 | 3459 | GetGridColHeader()->UpdateColumn(col); |
11393d29 VZ |
3460 | else if ( m_nativeColumnLabels ) |
3461 | m_colWindow->Refresh(); | |
3462 | //else: sorting indicator display not yet implemented in grid version | |
3463 | } | |
3464 | ||
3465 | void wxGrid::SetSortingColumn(int col, bool ascending) | |
3466 | { | |
3467 | if ( col == m_sortCol ) | |
3468 | { | |
3469 | // we are already using this column for sorting (or not sorting at all) | |
3470 | // but we might still change the sorting order, check for it | |
3471 | if ( m_sortCol != wxNOT_FOUND && ascending != m_sortIsAscending ) | |
3472 | { | |
3473 | m_sortIsAscending = ascending; | |
3474 | ||
3475 | UpdateColumnSortingIndicator(m_sortCol); | |
3476 | } | |
3477 | } | |
3478 | else // we're changing the column used for sorting | |
3479 | { | |
3480 | const int sortColOld = m_sortCol; | |
3481 | ||
3482 | // change it before updating the column as we want GetSortingColumn() | |
3483 | // to return the correct new value | |
3484 | m_sortCol = col; | |
3485 | ||
3486 | if ( sortColOld != wxNOT_FOUND ) | |
3487 | UpdateColumnSortingIndicator(sortColOld); | |
3488 | ||
3489 | if ( m_sortCol != wxNOT_FOUND ) | |
3490 | { | |
3491 | m_sortIsAscending = ascending; | |
3492 | UpdateColumnSortingIndicator(m_sortCol); | |
3493 | } | |
3494 | } | |
3495 | } | |
3496 | ||
3497 | void wxGrid::DoColHeaderClick(int col) | |
3498 | { | |
3499 | // we consider that the grid was resorted if this event is processed and | |
3500 | // not vetoed | |
3501 | if ( SendEvent(wxEVT_GRID_COL_SORT, -1, col) == 1 ) | |
3502 | { | |
3503 | SetSortingColumn(col, IsSortingBy(col) ? !m_sortIsAscending : true); | |
3504 | Refresh(); | |
3505 | } | |
3506 | } | |
3507 | ||
ad805b9e VZ |
3508 | void wxGrid::DoStartResizeCol(int col) |
3509 | { | |
3510 | m_dragRowOrCol = col; | |
3511 | m_dragLastPos = -1; | |
3512 | DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol)); | |
3513 | } | |
3514 | ||
3515 | void wxGrid::DoUpdateResizeCol(int x) | |
3516 | { | |
3517 | int cw, ch, dummy, top; | |
3518 | m_gridWin->GetClientSize( &cw, &ch ); | |
3519 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
3520 | ||
3521 | wxClientDC dc( m_gridWin ); | |
3522 | PrepareDC( dc ); | |
3523 | ||
3524 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + GetColMinimalWidth(m_dragRowOrCol)); | |
3525 | dc.SetLogicalFunction(wxINVERT); | |
3526 | if ( m_dragLastPos >= 0 ) | |
3527 | { | |
3528 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch ); | |
3529 | } | |
3530 | dc.DrawLine( x, top, x, top + ch ); | |
3531 | m_dragLastPos = x; | |
3532 | } | |
3533 | ||
3534 | void wxGrid::DoUpdateResizeColWidth(int w) | |
3535 | { | |
3536 | DoUpdateResizeCol(GetColLeft(m_dragRowOrCol) + w); | |
3537 | } | |
3538 | ||
2d66e025 MB |
3539 | void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) |
3540 | { | |
7d1214cd PC |
3541 | int x; |
3542 | CalcUnscrolledPosition( event.GetPosition().x, 0, &x, NULL ); | |
60ff3b99 | 3543 | |
11393d29 | 3544 | int col = XToCol(x); |
2d66e025 | 3545 | if ( event.Dragging() ) |
f85afd4e | 3546 | { |
fe77cf60 JS |
3547 | if (!m_isDragging) |
3548 | { | |
ca65c044 | 3549 | m_isDragging = true; |
ad805b9e | 3550 | GetColLabelWindow()->CaptureMouse(); |
d4175745 | 3551 | |
11393d29 VZ |
3552 | if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL && col != -1 ) |
3553 | DoStartMoveCol(col); | |
fe77cf60 | 3554 | } |
8f177c8e | 3555 | |
2d66e025 | 3556 | if ( event.LeftIsDown() ) |
8f177c8e | 3557 | { |
962a48f6 | 3558 | switch ( m_cursorMode ) |
8f177c8e | 3559 | { |
2d66e025 | 3560 | case WXGRID_CURSOR_RESIZE_COL: |
ad805b9e | 3561 | DoUpdateResizeCol(x); |
2d66e025 | 3562 | break; |
f85afd4e | 3563 | |
2d66e025 | 3564 | case WXGRID_CURSOR_SELECT_COL: |
902725ee | 3565 | { |
11393d29 | 3566 | if ( col != -1 ) |
aa5e1f75 | 3567 | { |
3f3dc2ef | 3568 | if ( m_selection ) |
8b5f6d9d | 3569 | m_selection->SelectCol(col, event); |
2d66e025 | 3570 | } |
902725ee WS |
3571 | } |
3572 | break; | |
e2b42eeb | 3573 | |
d4175745 VZ |
3574 | case WXGRID_CURSOR_MOVE_COL: |
3575 | { | |
cd68daf5 VZ |
3576 | int posNew = XToPos(x); |
3577 | int colNew = GetColAt(posNew); | |
d4175745 | 3578 | |
cd68daf5 | 3579 | // determine the position of the drop marker |
d4175745 | 3580 | int markerX; |
cd68daf5 VZ |
3581 | if ( x >= GetColLeft(colNew) + (GetColWidth(colNew) / 2) ) |
3582 | markerX = GetColRight(colNew); | |
d4175745 | 3583 | else |
cd68daf5 | 3584 | markerX = GetColLeft(colNew); |
d4175745 VZ |
3585 | |
3586 | if ( markerX != m_dragLastPos ) | |
3587 | { | |
ad805b9e | 3588 | wxClientDC dc( GetColLabelWindow() ); |
1eab9659 | 3589 | DoPrepareDC(dc); |
d4175745 VZ |
3590 | |
3591 | int cw, ch; | |
ad805b9e | 3592 | GetColLabelWindow()->GetClientSize( &cw, &ch ); |
d4175745 VZ |
3593 | |
3594 | markerX++; | |
3595 | ||
3596 | //Clean up the last indicator | |
3597 | if ( m_dragLastPos >= 0 ) | |
3598 | { | |
ad805b9e | 3599 | wxPen pen( GetColLabelWindow()->GetBackgroundColour(), 2 ); |
d4175745 VZ |
3600 | dc.SetPen(pen); |
3601 | dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch ); | |
3602 | dc.SetPen(wxNullPen); | |
3603 | ||
3604 | if ( XToCol( m_dragLastPos ) != -1 ) | |
3605 | DrawColLabel( dc, XToCol( m_dragLastPos ) ); | |
3606 | } | |
3607 | ||
87c819f9 | 3608 | const wxColour *color; |
d4175745 | 3609 | //Moving to the same place? Don't draw a marker |
cd68daf5 | 3610 | if ( colNew == m_dragRowOrCol ) |
87c819f9 VZ |
3611 | color = wxLIGHT_GREY; |
3612 | else | |
3613 | color = wxBLUE; | |
d4175745 VZ |
3614 | |
3615 | //Draw the marker | |
87c819f9 | 3616 | wxPen pen( *color, 2 ); |
d4175745 VZ |
3617 | dc.SetPen(pen); |
3618 | ||
3619 | dc.DrawLine( markerX, 0, markerX, ch ); | |
3620 | ||
3621 | dc.SetPen(wxNullPen); | |
3622 | ||
3623 | m_dragLastPos = markerX - 1; | |
3624 | } | |
3625 | } | |
3626 | break; | |
3627 | ||
e2b42eeb VZ |
3628 | // default label to suppress warnings about "enumeration value |
3629 | // 'xxx' not handled in switch | |
3630 | default: | |
3631 | break; | |
2d66e025 | 3632 | } |
f85afd4e | 3633 | } |
2d66e025 | 3634 | return; |
f85afd4e | 3635 | } |
2d66e025 | 3636 | |
fe77cf60 JS |
3637 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) |
3638 | return; | |
2d66e025 | 3639 | |
fe77cf60 JS |
3640 | if (m_isDragging) |
3641 | { | |
ad805b9e VZ |
3642 | if (GetColLabelWindow()->HasCapture()) |
3643 | GetColLabelWindow()->ReleaseMouse(); | |
ca65c044 | 3644 | m_isDragging = false; |
fe77cf60 | 3645 | } |
60ff3b99 | 3646 | |
6d004f67 MB |
3647 | // ------------ Entering or leaving the window |
3648 | // | |
3649 | if ( event.Entering() || event.Leaving() ) | |
3650 | { | |
ad805b9e | 3651 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
6d004f67 MB |
3652 | } |
3653 | ||
2d66e025 | 3654 | // ------------ Left button pressed |
f85afd4e | 3655 | // |
6d004f67 | 3656 | else if ( event.LeftDown() ) |
f85afd4e | 3657 | { |
7d1214cd PC |
3658 | int colEdge = XToEdgeOfCol(x); |
3659 | if ( colEdge != wxNOT_FOUND && CanDragColSize(colEdge) ) | |
8f177c8e | 3660 | { |
82edfbe7 VZ |
3661 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow()); |
3662 | } | |
3663 | else // not a request to start resizing | |
3664 | { | |
2f024384 | 3665 | if ( col >= 0 && |
b54ba671 | 3666 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) ) |
8f177c8e | 3667 | { |
d4175745 | 3668 | if ( m_canDragColMove ) |
3f3dc2ef | 3669 | { |
d4175745 | 3670 | //Show button as pressed |
ad805b9e | 3671 | wxClientDC dc( GetColLabelWindow() ); |
d4175745 VZ |
3672 | int colLeft = GetColLeft( col ); |
3673 | int colRight = GetColRight( col ) - 1; | |
ad805b9e | 3674 | dc.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) ); |
d4175745 VZ |
3675 | dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 ); |
3676 | dc.DrawLine( colLeft, 1, colRight, 1 ); | |
3677 | ||
ad805b9e | 3678 | ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, GetColLabelWindow()); |
d4175745 VZ |
3679 | } |
3680 | else | |
3681 | { | |
3682 | if ( !event.ShiftDown() && !event.CmdDown() ) | |
3683 | ClearSelection(); | |
3684 | if ( m_selection ) | |
3f3dc2ef | 3685 | { |
d4175745 VZ |
3686 | if ( event.ShiftDown() ) |
3687 | { | |
8b5f6d9d VZ |
3688 | m_selection->SelectBlock |
3689 | ( | |
3690 | 0, m_currentCellCoords.GetCol(), | |
3691 | GetNumberRows() - 1, col, | |
3692 | event | |
3693 | ); | |
d4175745 VZ |
3694 | } |
3695 | else | |
3696 | { | |
8b5f6d9d | 3697 | m_selection->SelectCol(col, event); |
d4175745 | 3698 | } |
3f3dc2ef | 3699 | } |
3f3dc2ef | 3700 | |
ad805b9e | 3701 | ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, GetColLabelWindow()); |
d4175745 | 3702 | } |
f85afd4e | 3703 | } |
2d66e025 | 3704 | } |
2d66e025 | 3705 | } |
f85afd4e | 3706 | |
2d66e025 MB |
3707 | // ------------ Left double click |
3708 | // | |
3709 | if ( event.LeftDClick() ) | |
3710 | { | |
11393d29 VZ |
3711 | const int colEdge = XToEdgeOfCol(x); |
3712 | if ( colEdge == -1 ) | |
2d66e025 | 3713 | { |
a967f048 RG |
3714 | if ( col >= 0 && |
3715 | ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) ) | |
3716 | { | |
ca65c044 | 3717 | // no default action at the moment |
a967f048 | 3718 | } |
2d66e025 | 3719 | } |
d43851f7 JS |
3720 | else |
3721 | { | |
3722 | // adjust column width depending on label text | |
27bb2c8c VZ |
3723 | // |
3724 | // TODO: generate RESIZING event, see #10754 | |
6f58f3d7 VZ |
3725 | if ( !SendGridSizeEvent(wxEVT_GRID_COL_AUTO_SIZE, -1, colEdge, event) ) |
3726 | AutoSizeColLabelSize( colEdge ); | |
d43851f7 | 3727 | |
c5c1ea96 | 3728 | SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, colEdge, event); |
27bb2c8c | 3729 | |
ad805b9e | 3730 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
ccdee36f | 3731 | m_dragLastPos = -1; |
d43851f7 | 3732 | } |
2d66e025 | 3733 | } |
60ff3b99 | 3734 | |
2d66e025 MB |
3735 | // ------------ Left button released |
3736 | // | |
3737 | else if ( event.LeftUp() ) | |
3738 | { | |
d4175745 | 3739 | switch ( m_cursorMode ) |
2d66e025 | 3740 | { |
d4175745 | 3741 | case WXGRID_CURSOR_RESIZE_COL: |
27bb2c8c | 3742 | DoEndDragResizeCol(event); |
852b6c3c | 3743 | break; |
d4175745 VZ |
3744 | |
3745 | case WXGRID_CURSOR_MOVE_COL: | |
11393d29 | 3746 | if ( m_dragLastPos == -1 || col == m_dragRowOrCol ) |
cd68daf5 | 3747 | { |
11393d29 VZ |
3748 | // the column didn't actually move anywhere |
3749 | if ( col != -1 ) | |
3750 | DoColHeaderClick(col); | |
cd68daf5 VZ |
3751 | m_colWindow->Refresh(); // "unpress" the column |
3752 | } | |
3753 | else | |
3754 | { | |
7c331059 VZ |
3755 | // get the position of the column we're over |
3756 | int pos = XToPos(x); | |
3757 | ||
3758 | // we may need to adjust the drop position but don't bother | |
3759 | // checking for it if we can't anyhow | |
3760 | if ( pos > 1 ) | |
3761 | { | |
3762 | // also find the index of the column we're over: notice | |
3763 | // that the existing "col" variable may be invalid but | |
3764 | // we need a valid one here | |
3765 | const int colValid = GetColAt(pos); | |
3766 | ||
3767 | // if we're on the "near" (usually left but right in | |
3768 | // RTL case) part of the column, the actual position we | |
3769 | // should be placed in is actually the one before it | |
18c0369b | 3770 | bool onNearPart; |
7c331059 VZ |
3771 | const int middle = GetColLeft(colValid) + |
3772 | GetColWidth(colValid)/2; | |
3773 | if ( GetLayoutDirection() == wxLayout_LeftToRight ) | |
18c0369b | 3774 | onNearPart = (x <= middle); |
7c331059 | 3775 | else // wxLayout_RightToLeft |
18c0369b | 3776 | onNearPart = (x > middle); |
7c331059 | 3777 | |
18c0369b | 3778 | if ( onNearPart ) |
7c331059 VZ |
3779 | pos--; |
3780 | } | |
3781 | ||
3782 | DoEndMoveCol(pos); | |
cd68daf5 | 3783 | } |
852b6c3c VZ |
3784 | break; |
3785 | ||
3786 | case WXGRID_CURSOR_SELECT_COL: | |
3787 | case WXGRID_CURSOR_SELECT_CELL: | |
3788 | case WXGRID_CURSOR_RESIZE_ROW: | |
3789 | case WXGRID_CURSOR_SELECT_ROW: | |
11393d29 VZ |
3790 | if ( col != -1 ) |
3791 | DoColHeaderClick(col); | |
852b6c3c | 3792 | break; |
2d66e025 | 3793 | } |
f85afd4e | 3794 | |
ad805b9e | 3795 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
ccdee36f | 3796 | m_dragLastPos = -1; |
60ff3b99 VZ |
3797 | } |
3798 | ||
2d66e025 MB |
3799 | // ------------ Right button down |
3800 | // | |
3801 | else if ( event.RightDown() ) | |
3802 | { | |
a967f048 | 3803 | if ( col >= 0 && |
ca65c044 | 3804 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) |
2d66e025 MB |
3805 | { |
3806 | // no default action at the moment | |
f85afd4e MB |
3807 | } |
3808 | } | |
60ff3b99 | 3809 | |
2d66e025 | 3810 | // ------------ Right double click |
f85afd4e | 3811 | // |
2d66e025 MB |
3812 | else if ( event.RightDClick() ) |
3813 | { | |
a967f048 | 3814 | if ( col >= 0 && |
ca65c044 | 3815 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) |
2d66e025 MB |
3816 | { |
3817 | // no default action at the moment | |
3818 | } | |
3819 | } | |
60ff3b99 | 3820 | |
2d66e025 | 3821 | // ------------ No buttons down and mouse moving |
f85afd4e | 3822 | // |
2d66e025 | 3823 | else if ( event.Moving() ) |
f85afd4e | 3824 | { |
2d66e025 MB |
3825 | m_dragRowOrCol = XToEdgeOfCol( x ); |
3826 | if ( m_dragRowOrCol >= 0 ) | |
f85afd4e | 3827 | { |
2d66e025 | 3828 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
f85afd4e | 3829 | { |
82edfbe7 | 3830 | if ( CanDragColSize(m_dragRowOrCol) ) |
ad805b9e | 3831 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow(), false); |
f85afd4e | 3832 | } |
2d66e025 | 3833 | } |
6d004f67 | 3834 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 3835 | { |
ad805b9e | 3836 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow(), false); |
8f177c8e | 3837 | } |
f85afd4e MB |
3838 | } |
3839 | } | |
3840 | ||
2d66e025 | 3841 | void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 3842 | { |
2d66e025 | 3843 | if ( event.LeftDown() ) |
f85afd4e | 3844 | { |
2d66e025 MB |
3845 | // indicate corner label by having both row and |
3846 | // col args == -1 | |
f85afd4e | 3847 | // |
b54ba671 | 3848 | if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) ) |
2d66e025 MB |
3849 | { |
3850 | SelectAll(); | |
3851 | } | |
f85afd4e | 3852 | } |
2d66e025 MB |
3853 | else if ( event.LeftDClick() ) |
3854 | { | |
b54ba671 | 3855 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event ); |
2d66e025 | 3856 | } |
2d66e025 | 3857 | else if ( event.RightDown() ) |
f85afd4e | 3858 | { |
b54ba671 | 3859 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) ) |
f85afd4e | 3860 | { |
2d66e025 MB |
3861 | // no default action at the moment |
3862 | } | |
3863 | } | |
2d66e025 MB |
3864 | else if ( event.RightDClick() ) |
3865 | { | |
b54ba671 | 3866 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) ) |
2d66e025 MB |
3867 | { |
3868 | // no default action at the moment | |
3869 | } | |
3870 | } | |
3871 | } | |
f85afd4e | 3872 | |
86033c4b VZ |
3873 | void wxGrid::CancelMouseCapture() |
3874 | { | |
3875 | // cancel operation currently in progress, whatever it is | |
3876 | if ( m_winCapture ) | |
3877 | { | |
3878 | m_isDragging = false; | |
8a3e536c VZ |
3879 | m_startDragPos = wxDefaultPosition; |
3880 | ||
86033c4b VZ |
3881 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
3882 | m_winCapture->SetCursor( *wxSTANDARD_CURSOR ); | |
3883 | m_winCapture = NULL; | |
3884 | ||
3885 | // remove traces of whatever we drew on screen | |
3886 | Refresh(); | |
3887 | } | |
3888 | } | |
3889 | ||
e2b42eeb VZ |
3890 | void wxGrid::ChangeCursorMode(CursorMode mode, |
3891 | wxWindow *win, | |
3892 | bool captureMouse) | |
3893 | { | |
711f12ef | 3894 | #if wxUSE_LOG_TRACE |
a243da29 | 3895 | static const wxChar *const cursorModes[] = |
e2b42eeb | 3896 | { |
9a83f860 VZ |
3897 | wxT("SELECT_CELL"), |
3898 | wxT("RESIZE_ROW"), | |
3899 | wxT("RESIZE_COL"), | |
3900 | wxT("SELECT_ROW"), | |
3901 | wxT("SELECT_COL"), | |
3902 | wxT("MOVE_COL"), | |
e2b42eeb VZ |
3903 | }; |
3904 | ||
9a83f860 VZ |
3905 | wxLogTrace(wxT("grid"), |
3906 | wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"), | |
3907 | win == m_colWindow ? wxT("colLabelWin") | |
3908 | : win ? wxT("rowLabelWin") | |
3909 | : wxT("gridWin"), | |
e2b42eeb | 3910 | cursorModes[m_cursorMode], cursorModes[mode]); |
711f12ef | 3911 | #endif // wxUSE_LOG_TRACE |
e2b42eeb | 3912 | |
faec5a43 SN |
3913 | if ( mode == m_cursorMode && |
3914 | win == m_winCapture && | |
3915 | captureMouse == (m_winCapture != NULL)) | |
e2b42eeb VZ |
3916 | return; |
3917 | ||
3918 | if ( !win ) | |
3919 | { | |
3920 | // by default use the grid itself | |
3921 | win = m_gridWin; | |
3922 | } | |
3923 | ||
3924 | if ( m_winCapture ) | |
3925 | { | |
e882d288 | 3926 | m_winCapture->ReleaseMouse(); |
10a4531d | 3927 | m_winCapture = NULL; |
e2b42eeb VZ |
3928 | } |
3929 | ||
3930 | m_cursorMode = mode; | |
3931 | ||
3932 | switch ( m_cursorMode ) | |
3933 | { | |
3934 | case WXGRID_CURSOR_RESIZE_ROW: | |
3935 | win->SetCursor( m_rowResizeCursor ); | |
3936 | break; | |
3937 | ||
3938 | case WXGRID_CURSOR_RESIZE_COL: | |
3939 | win->SetCursor( m_colResizeCursor ); | |
3940 | break; | |
3941 | ||
d4175745 VZ |
3942 | case WXGRID_CURSOR_MOVE_COL: |
3943 | win->SetCursor( wxCursor(wxCURSOR_HAND) ); | |
3944 | break; | |
3945 | ||
e2b42eeb VZ |
3946 | default: |
3947 | win->SetCursor( *wxSTANDARD_CURSOR ); | |
2f024384 | 3948 | break; |
e2b42eeb VZ |
3949 | } |
3950 | ||
3951 | // we need to capture mouse when resizing | |
3952 | bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW || | |
3953 | m_cursorMode == WXGRID_CURSOR_RESIZE_COL; | |
3954 | ||
3955 | if ( captureMouse && resize ) | |
3956 | { | |
3957 | win->CaptureMouse(); | |
3958 | m_winCapture = win; | |
3959 | } | |
3960 | } | |
8f177c8e | 3961 | |
8a3e536c VZ |
3962 | // ---------------------------------------------------------------------------- |
3963 | // grid mouse event processing | |
3964 | // ---------------------------------------------------------------------------- | |
60ff3b99 | 3965 | |
0cbcb12d | 3966 | bool |
8a3e536c VZ |
3967 | wxGrid::DoGridCellDrag(wxMouseEvent& event, |
3968 | const wxGridCellCoords& coords, | |
3969 | bool isFirstDrag) | |
3970 | { | |
0cbcb12d SC |
3971 | bool performDefault = true ; |
3972 | ||
8a3e536c | 3973 | if ( coords == wxGridNoCellCoords ) |
0cbcb12d | 3974 | return performDefault; // we're outside any valid cell |
60ff3b99 | 3975 | |
8a3e536c VZ |
3976 | // Hide the edit control, so it won't interfere with drag-shrinking. |
3977 | if ( IsCellEditControlShown() ) | |
27f35b66 | 3978 | { |
8a3e536c VZ |
3979 | HideCellEditControl(); |
3980 | SaveEditControlValue(); | |
27f35b66 SN |
3981 | } |
3982 | ||
8a3e536c | 3983 | switch ( event.GetModifiers() ) |
2d66e025 | 3984 | { |
3bc6d534 | 3985 | case wxMOD_CONTROL: |
8a3e536c VZ |
3986 | if ( m_selectedBlockCorner == wxGridNoCellCoords) |
3987 | m_selectedBlockCorner = coords; | |
3988 | UpdateBlockBeingSelected(m_selectedBlockCorner, coords); | |
3989 | break; | |
07296f0b | 3990 | |
8a3e536c VZ |
3991 | case wxMOD_NONE: |
3992 | if ( CanDragCell() ) | |
2d66e025 | 3993 | { |
8a3e536c | 3994 | if ( isFirstDrag ) |
79dbea21 | 3995 | { |
8a3e536c VZ |
3996 | if ( m_selectedBlockCorner == wxGridNoCellCoords) |
3997 | m_selectedBlockCorner = coords; | |
07296f0b | 3998 | |
0cbcb12d | 3999 | // if event is handled by user code, no further processing |
c118fa5f | 4000 | if ( SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event) != 0 ) |
0cbcb12d SC |
4001 | performDefault = false; |
4002 | ||
4003 | return performDefault; | |
07296f0b | 4004 | } |
2d66e025 | 4005 | } |
25107357 | 4006 | |
8a3e536c VZ |
4007 | UpdateBlockBeingSelected(m_currentCellCoords, coords); |
4008 | break; | |
c71b2126 | 4009 | |
8a3e536c VZ |
4010 | default: |
4011 | // we don't handle the other key modifiers | |
4012 | event.Skip(); | |
4013 | } | |
0cbcb12d SC |
4014 | |
4015 | return performDefault; | |
8a3e536c | 4016 | } |
8f177c8e | 4017 | |
8a3e536c VZ |
4018 | void wxGrid::DoGridLineDrag(wxMouseEvent& event, const wxGridOperations& oper) |
4019 | { | |
4020 | wxClientDC dc(m_gridWin); | |
4021 | PrepareDC(dc); | |
4022 | dc.SetLogicalFunction(wxINVERT); | |
e2b42eeb | 4023 | |
8a3e536c VZ |
4024 | const wxRect rectWin(CalcUnscrolledPosition(wxPoint(0, 0)), |
4025 | m_gridWin->GetClientSize()); | |
4026 | ||
4027 | // erase the previously drawn line, if any | |
4028 | if ( m_dragLastPos >= 0 ) | |
4029 | oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos); | |
4030 | ||
4031 | // we need the vertical position for rows and horizontal for columns here | |
4032 | m_dragLastPos = oper.Dual().Select(CalcUnscrolledPosition(event.GetPosition())); | |
4033 | ||
4034 | // don't allow resizing beneath the minimal size | |
4035 | const int posMin = oper.GetLineStartPos(this, m_dragRowOrCol) + | |
4036 | oper.GetMinimalLineSize(this, m_dragRowOrCol); | |
4037 | if ( m_dragLastPos < posMin ) | |
4038 | m_dragLastPos = posMin; | |
4039 | ||
4040 | // and draw it at the new position | |
4041 | oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos); | |
4042 | } | |
4043 | ||
4044 | void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords) | |
4045 | { | |
4046 | if ( !m_isDragging ) | |
4047 | { | |
4048 | // Don't start doing anything until the mouse has been dragged far | |
4049 | // enough | |
4050 | const wxPoint& pt = event.GetPosition(); | |
4051 | if ( m_startDragPos == wxDefaultPosition ) | |
4052 | { | |
4053 | m_startDragPos = pt; | |
4054 | return; | |
6d004f67 | 4055 | } |
e2b42eeb | 4056 | |
8a3e536c VZ |
4057 | if ( abs(m_startDragPos.x - pt.x) <= DRAG_SENSITIVITY && |
4058 | abs(m_startDragPos.y - pt.y) <= DRAG_SENSITIVITY ) | |
4059 | return; | |
2d66e025 | 4060 | } |
66242c80 | 4061 | |
8a3e536c VZ |
4062 | const bool isFirstDrag = !m_isDragging; |
4063 | m_isDragging = true; | |
07296f0b | 4064 | |
8a3e536c | 4065 | switch ( m_cursorMode ) |
a5777624 | 4066 | { |
8a3e536c | 4067 | case WXGRID_CURSOR_SELECT_CELL: |
0cbcb12d SC |
4068 | // no further handling if handled by user |
4069 | if ( DoGridCellDrag(event, coords, isFirstDrag) == false ) | |
4070 | return; | |
8a3e536c VZ |
4071 | break; |
4072 | ||
4073 | case WXGRID_CURSOR_RESIZE_ROW: | |
4074 | DoGridLineDrag(event, wxGridRowOperations()); | |
4075 | break; | |
4076 | ||
4077 | case WXGRID_CURSOR_RESIZE_COL: | |
4078 | DoGridLineDrag(event, wxGridColumnOperations()); | |
4079 | break; | |
4080 | ||
4081 | default: | |
4082 | event.Skip(); | |
a5777624 | 4083 | } |
e2b42eeb | 4084 | |
8a3e536c | 4085 | if ( isFirstDrag ) |
f6bcfd97 | 4086 | { |
98e95650 VZ |
4087 | wxASSERT_MSG( !m_winCapture, "shouldn't capture the mouse twice" ); |
4088 | ||
8a3e536c VZ |
4089 | m_winCapture = m_gridWin; |
4090 | m_winCapture->CaptureMouse(); | |
4091 | } | |
4092 | } | |
a5777624 | 4093 | |
8a3e536c VZ |
4094 | void |
4095 | wxGrid::DoGridCellLeftDown(wxMouseEvent& event, | |
4096 | const wxGridCellCoords& coords, | |
4097 | const wxPoint& pos) | |
4098 | { | |
4099 | if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK, coords, event) ) | |
4100 | { | |
4101 | // event handled by user code, no need to do anything here | |
4102 | return; | |
a5777624 | 4103 | } |
f85afd4e | 4104 | |
8a3e536c VZ |
4105 | if ( !event.CmdDown() ) |
4106 | ClearSelection(); | |
4107 | ||
4108 | if ( event.ShiftDown() ) | |
4109 | { | |
4110 | if ( m_selection ) | |
4111 | { | |
8b5f6d9d | 4112 | m_selection->SelectBlock(m_currentCellCoords, coords, event); |
8a3e536c VZ |
4113 | m_selectedBlockCorner = coords; |
4114 | } | |
4115 | } | |
4116 | else if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 ) | |
a5777624 RD |
4117 | { |
4118 | DisableCellEditControl(); | |
8a3e536c | 4119 | MakeCellVisible( coords ); |
a5777624 | 4120 | |
8a3e536c | 4121 | if ( event.CmdDown() ) |
58dd5b3b | 4122 | { |
8a3e536c | 4123 | if ( m_selection ) |
1ef49ab5 | 4124 | { |
8b5f6d9d | 4125 | m_selection->ToggleCellSelection(coords, event); |
1ef49ab5 | 4126 | } |
8b5f6d9d | 4127 | |
8a3e536c VZ |
4128 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
4129 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
4130 | m_selectedBlockCorner = coords; | |
4131 | } | |
4132 | else | |
4133 | { | |
e196db7b VZ |
4134 | if ( m_selection ) |
4135 | { | |
4136 | // In row or column selection mode just clicking on the cell | |
4137 | // should select the row or column containing it: this is more | |
4138 | // convenient for the kinds of controls that use such selection | |
4139 | // mode and is compatible with 2.8 behaviour (see #12062). | |
4140 | switch ( m_selection->GetSelectionMode() ) | |
4141 | { | |
4142 | case wxGridSelectCells: | |
4143 | case wxGridSelectRowsOrColumns: | |
4144 | // nothing to do in these cases | |
4145 | break; | |
4146 | ||
4147 | case wxGridSelectRows: | |
4148 | m_selection->SelectRow(coords.GetRow()); | |
4149 | break; | |
4150 | ||
4151 | case wxGridSelectColumns: | |
4152 | m_selection->SelectCol(coords.GetCol()); | |
4153 | break; | |
4154 | } | |
4155 | } | |
4156 | ||
8a3e536c VZ |
4157 | m_waitForSlowClick = m_currentCellCoords == coords && |
4158 | coords != wxGridNoCellCoords; | |
4159 | SetCurrentCell( coords ); | |
58dd5b3b | 4160 | } |
a5777624 | 4161 | } |
8a3e536c | 4162 | } |
f85afd4e | 4163 | |
8a3e536c VZ |
4164 | void |
4165 | wxGrid::DoGridCellLeftDClick(wxMouseEvent& event, | |
4166 | const wxGridCellCoords& coords, | |
4167 | const wxPoint& pos) | |
4168 | { | |
4169 | if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 ) | |
a5777624 | 4170 | { |
8a3e536c | 4171 | if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK, coords, event) ) |
f85afd4e | 4172 | { |
8a3e536c VZ |
4173 | // we want double click to select a cell and start editing |
4174 | // (i.e. to behave in same way as sequence of two slow clicks): | |
4175 | m_waitForSlowClick = true; | |
4176 | } | |
4177 | } | |
4178 | } | |
932b55d0 | 4179 | |
8a3e536c VZ |
4180 | void |
4181 | wxGrid::DoGridCellLeftUp(wxMouseEvent& event, const wxGridCellCoords& coords) | |
4182 | { | |
4183 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
4184 | { | |
4185 | if (m_winCapture) | |
4186 | { | |
e882d288 | 4187 | m_winCapture->ReleaseMouse(); |
8a3e536c VZ |
4188 | m_winCapture = NULL; |
4189 | } | |
932b55d0 | 4190 | |
8a3e536c VZ |
4191 | if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() ) |
4192 | { | |
4193 | ClearSelection(); | |
4194 | EnableCellEditControl(); | |
3f3dc2ef | 4195 | |
8a3e536c VZ |
4196 | wxGridCellAttr *attr = GetCellAttr(coords); |
4197 | wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol()); | |
4198 | editor->StartingClick(); | |
4199 | editor->DecRef(); | |
4200 | attr->DecRef(); | |
2d66e025 | 4201 | |
8a3e536c | 4202 | m_waitForSlowClick = false; |
a5777624 | 4203 | } |
8a3e536c VZ |
4204 | else if ( m_selectedBlockTopLeft != wxGridNoCellCoords && |
4205 | m_selectedBlockBottomRight != wxGridNoCellCoords ) | |
a5777624 | 4206 | { |
8a3e536c VZ |
4207 | if ( m_selection ) |
4208 | { | |
4209 | m_selection->SelectBlock( m_selectedBlockTopLeft, | |
4210 | m_selectedBlockBottomRight, | |
8b5f6d9d | 4211 | event ); |
8a3e536c | 4212 | } |
e2b42eeb | 4213 | |
8a3e536c VZ |
4214 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
4215 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
e2b42eeb | 4216 | |
8a3e536c VZ |
4217 | // Show the edit control, if it has been hidden for |
4218 | // drag-shrinking. | |
4219 | ShowCellEditControl(); | |
58dd5b3b | 4220 | } |
8a3e536c VZ |
4221 | } |
4222 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
4223 | { | |
4224 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
27bb2c8c | 4225 | DoEndDragResizeRow(event); |
a5777624 | 4226 | } |
8a3e536c VZ |
4227 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) |
4228 | { | |
4229 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
27bb2c8c | 4230 | DoEndDragResizeCol(event); |
8a3e536c VZ |
4231 | } |
4232 | ||
4233 | m_dragLastPos = -1; | |
4234 | } | |
4235 | ||
4236 | void | |
4237 | wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event), | |
4238 | const wxGridCellCoords& coords, | |
4239 | const wxPoint& pos) | |
4240 | { | |
4241 | if ( coords.GetRow() < 0 || coords.GetCol() < 0 ) | |
a5777624 | 4242 | { |
8a3e536c VZ |
4243 | // out of grid cell area |
4244 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
4245 | return; | |
a5777624 | 4246 | } |
2d66e025 | 4247 | |
8a3e536c VZ |
4248 | int dragRow = YToEdgeOfRow( pos.y ); |
4249 | int dragCol = XToEdgeOfCol( pos.x ); | |
4250 | ||
4251 | // Dragging on the corner of a cell to resize in both | |
4252 | // directions is not implemented yet... | |
a5777624 | 4253 | // |
8a3e536c | 4254 | if ( dragRow >= 0 && dragCol >= 0 ) |
a5777624 | 4255 | { |
8a3e536c VZ |
4256 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); |
4257 | return; | |
4258 | } | |
4259 | ||
82edfbe7 | 4260 | if ( dragRow >= 0 && CanDragGridSize() && CanDragRowSize(dragRow) ) |
8a3e536c | 4261 | { |
8a3e536c | 4262 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
f85afd4e | 4263 | { |
82edfbe7 VZ |
4264 | m_dragRowOrCol = dragRow; |
4265 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false); | |
60ff3b99 | 4266 | } |
a5777624 | 4267 | } |
ad805b9e VZ |
4268 | // When using the native header window we can only resize the columns by |
4269 | // dragging the dividers in it because we can't make it enter into the | |
4270 | // column resizing mode programmatically | |
82edfbe7 VZ |
4271 | else if ( dragCol >= 0 && !m_useNativeHeader && |
4272 | CanDragGridSize() && CanDragColSize(dragCol) ) | |
8a3e536c | 4273 | { |
8a3e536c VZ |
4274 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
4275 | { | |
82edfbe7 VZ |
4276 | m_dragRowOrCol = dragCol; |
4277 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false); | |
8a3e536c VZ |
4278 | } |
4279 | } | |
4280 | else // Neither on a row or col edge | |
a5777624 | 4281 | { |
8a3e536c | 4282 | if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
d57ad377 | 4283 | { |
d57ad377 | 4284 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); |
d57ad377 | 4285 | } |
8a3e536c VZ |
4286 | } |
4287 | } | |
d57ad377 | 4288 | |
8a3e536c VZ |
4289 | void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event) |
4290 | { | |
98e95650 VZ |
4291 | if ( event.Entering() || event.Leaving() ) |
4292 | { | |
4293 | // we don't care about these events but we must not reset m_isDragging | |
4294 | // if they happen so return before anything else is done | |
4295 | event.Skip(); | |
4296 | return; | |
4297 | } | |
4298 | ||
8a3e536c | 4299 | const wxPoint pos = CalcUnscrolledPosition(event.GetPosition()); |
790cc417 | 4300 | |
8a3e536c VZ |
4301 | // coordinates of the cell under mouse |
4302 | wxGridCellCoords coords = XYToCell(pos); | |
6d004f67 | 4303 | |
8a3e536c VZ |
4304 | int cell_rows, cell_cols; |
4305 | GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols ); | |
4306 | if ( (cell_rows < 0) || (cell_cols < 0) ) | |
4307 | { | |
4308 | coords.SetRow(coords.GetRow() + cell_rows); | |
4309 | coords.SetCol(coords.GetCol() + cell_cols); | |
4310 | } | |
6d004f67 | 4311 | |
8a3e536c VZ |
4312 | if ( event.Dragging() ) |
4313 | { | |
4314 | if ( event.LeftIsDown() ) | |
4315 | DoGridDragEvent(event, coords); | |
4316 | else | |
4317 | event.Skip(); | |
4318 | return; | |
4319 | } | |
4320 | ||
4321 | m_isDragging = false; | |
4322 | m_startDragPos = wxDefaultPosition; | |
4323 | ||
8a3e536c VZ |
4324 | // deal with various button presses |
4325 | if ( event.IsButton() ) | |
4326 | { | |
4327 | if ( coords != wxGridNoCellCoords ) | |
a5777624 | 4328 | { |
8a3e536c | 4329 | DisableCellEditControl(); |
e2b42eeb | 4330 | |
8a3e536c VZ |
4331 | if ( event.LeftDown() ) |
4332 | DoGridCellLeftDown(event, coords, pos); | |
4333 | else if ( event.LeftDClick() ) | |
4334 | DoGridCellLeftDClick(event, coords, pos); | |
4335 | else if ( event.RightDown() ) | |
4336 | SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK, coords, event); | |
4337 | else if ( event.RightDClick() ) | |
4338 | SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK, coords, event); | |
6d004f67 | 4339 | } |
8a3e536c VZ |
4340 | |
4341 | // this one should be called even if we're not over any cell | |
4342 | if ( event.LeftUp() ) | |
a5777624 | 4343 | { |
8a3e536c | 4344 | DoGridCellLeftUp(event, coords); |
a5777624 RD |
4345 | } |
4346 | } | |
8a3e536c VZ |
4347 | else if ( event.Moving() ) |
4348 | { | |
4349 | DoGridMouseMoveEvent(event, coords, pos); | |
4350 | } | |
4351 | else // unknown mouse event? | |
4352 | { | |
4353 | event.Skip(); | |
4354 | } | |
6d004f67 MB |
4355 | } |
4356 | ||
27bb2c8c VZ |
4357 | // this function returns true only if the size really changed |
4358 | bool wxGrid::DoEndDragResizeLine(const wxGridOperations& oper) | |
6d004f67 | 4359 | { |
bec70262 | 4360 | if ( m_dragLastPos == -1 ) |
27bb2c8c | 4361 | return false; |
6d004f67 | 4362 | |
bec70262 | 4363 | const wxGridOperations& doper = oper.Dual(); |
6d004f67 | 4364 | |
bec70262 | 4365 | const wxSize size = m_gridWin->GetClientSize(); |
e2b42eeb | 4366 | |
bec70262 | 4367 | const wxPoint ptOrigin = CalcUnscrolledPosition(wxPoint(0, 0)); |
ccdee36f | 4368 | |
bec70262 VZ |
4369 | // erase the last line we drew |
4370 | wxClientDC dc(m_gridWin); | |
4371 | PrepareDC(dc); | |
4372 | dc.SetLogicalFunction(wxINVERT); | |
6d004f67 | 4373 | |
bec70262 VZ |
4374 | const int posLineStart = oper.Select(ptOrigin); |
4375 | const int posLineEnd = oper.Select(ptOrigin) + oper.Select(size); | |
6d004f67 | 4376 | |
bec70262 | 4377 | oper.DrawParallelLine(dc, posLineStart, posLineEnd, m_dragLastPos); |
6d004f67 | 4378 | |
bec70262 VZ |
4379 | // temporarily hide the edit control before resizing |
4380 | HideCellEditControl(); | |
4381 | SaveEditControlValue(); | |
4382 | ||
4383 | // do resize the line | |
4384 | const int lineStart = oper.GetLineStartPos(this, m_dragRowOrCol); | |
27bb2c8c | 4385 | const int lineSizeOld = oper.GetLineSize(this, m_dragRowOrCol); |
bec70262 VZ |
4386 | oper.SetLineSize(this, m_dragRowOrCol, |
4387 | wxMax(m_dragLastPos - lineStart, | |
4388 | oper.GetMinimalLineSize(this, m_dragRowOrCol))); | |
27bb2c8c VZ |
4389 | const bool |
4390 | sizeChanged = oper.GetLineSize(this, m_dragRowOrCol) != lineSizeOld; | |
bec70262 | 4391 | |
ad805b9e VZ |
4392 | m_dragLastPos = -1; |
4393 | ||
bec70262 VZ |
4394 | // refresh now if we're not frozen |
4395 | if ( !GetBatchCount() ) | |
6d004f67 | 4396 | { |
bec70262 VZ |
4397 | // we need to refresh everything beyond the resized line in the header |
4398 | // window | |
6d004f67 | 4399 | |
bec70262 VZ |
4400 | // get the position from which to refresh in the other direction |
4401 | wxRect rect(CellToRect(oper.MakeCoords(m_dragRowOrCol, 0))); | |
4402 | rect.SetPosition(CalcScrolledPosition(rect.GetPosition())); | |
6d004f67 | 4403 | |
bec70262 VZ |
4404 | // we only need the ordinate (for rows) or abscissa (for columns) here, |
4405 | // and need to cover the entire window in the other direction | |
4406 | oper.Select(rect) = 0; | |
6d004f67 | 4407 | |
bec70262 VZ |
4408 | wxRect rectHeader(rect.GetPosition(), |
4409 | oper.MakeSize | |
4410 | ( | |
4411 | oper.GetHeaderWindowSize(this), | |
4412 | doper.Select(size) - doper.Select(rect) | |
4413 | )); | |
4414 | ||
4415 | oper.GetHeaderWindow(this)->Refresh(true, &rectHeader); | |
4416 | ||
4417 | ||
4418 | // also refresh the grid window: extend the rectangle | |
4419 | if ( m_table ) | |
6d004f67 | 4420 | { |
bec70262 | 4421 | oper.SelectSize(rect) = oper.Select(size); |
2f024384 | 4422 | |
bec70262 | 4423 | int subtractLines = 0; |
7d1214cd PC |
4424 | int line = doper.PosToLine(this, posLineStart); |
4425 | if ( line >= 0 ) | |
27f35b66 | 4426 | { |
bec70262 VZ |
4427 | // ensure that if we have a multi-cell block we redraw all of |
4428 | // it by increasing the refresh area to cover it entirely if a | |
4429 | // part of it is affected | |
13350621 | 4430 | const int lineEnd = doper.PosToLine(this, posLineEnd, true); |
7d1214cd | 4431 | for ( ; line < lineEnd; line++ ) |
27f35b66 | 4432 | { |
bec70262 VZ |
4433 | int cellLines = oper.Select( |
4434 | GetCellSize(oper.MakeCoords(m_dragRowOrCol, line))); | |
4435 | if ( cellLines < subtractLines ) | |
4436 | subtractLines = cellLines; | |
27f35b66 SN |
4437 | } |
4438 | } | |
2f024384 | 4439 | |
bec70262 VZ |
4440 | int startPos = |
4441 | oper.GetLineStartPos(this, m_dragRowOrCol + subtractLines); | |
4442 | startPos = doper.CalcScrolledPosition(this, startPos); | |
4443 | ||
4444 | doper.Select(rect) = startPos; | |
4445 | doper.SelectSize(rect) = doper.Select(size) - startPos; | |
e2b42eeb | 4446 | |
bec70262 VZ |
4447 | m_gridWin->Refresh(false, &rect); |
4448 | } | |
f85afd4e | 4449 | } |
bec70262 VZ |
4450 | |
4451 | // show the edit control back again | |
4452 | ShowCellEditControl(); | |
27bb2c8c VZ |
4453 | |
4454 | return sizeChanged; | |
bec70262 VZ |
4455 | } |
4456 | ||
27bb2c8c | 4457 | void wxGrid::DoEndDragResizeRow(const wxMouseEvent& event) |
bec70262 | 4458 | { |
27bb2c8c VZ |
4459 | // TODO: generate RESIZING event, see #10754 |
4460 | ||
4461 | if ( DoEndDragResizeLine(wxGridRowOperations()) ) | |
c5c1ea96 | 4462 | SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event); |
bec70262 VZ |
4463 | } |
4464 | ||
27bb2c8c | 4465 | void wxGrid::DoEndDragResizeCol(const wxMouseEvent& event) |
bec70262 | 4466 | { |
27bb2c8c | 4467 | // TODO: generate RESIZING event, see #10754 |
ad805b9e | 4468 | |
27bb2c8c | 4469 | if ( DoEndDragResizeLine(wxGridColumnOperations()) ) |
c5c1ea96 | 4470 | SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event); |
f85afd4e MB |
4471 | } |
4472 | ||
cd68daf5 | 4473 | void wxGrid::DoStartMoveCol(int col) |
d4175745 | 4474 | { |
cd68daf5 VZ |
4475 | m_dragRowOrCol = col; |
4476 | } | |
d4175745 | 4477 | |
cd68daf5 VZ |
4478 | void wxGrid::DoEndMoveCol(int pos) |
4479 | { | |
4480 | wxASSERT_MSG( m_dragRowOrCol != -1, "no matching DoStartMoveCol?" ); | |
4481 | ||
4482 | if ( SendEvent(wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol) != -1 ) | |
4483 | SetColPos(m_dragRowOrCol, pos); | |
4484 | //else: vetoed by user | |
d4175745 | 4485 | |
cd68daf5 | 4486 | m_dragRowOrCol = -1; |
d4175745 VZ |
4487 | } |
4488 | ||
4797b014 | 4489 | void wxGrid::RefreshAfterColPosChange() |
009c7216 | 4490 | { |
4797b014 VZ |
4491 | // recalculate the column rights as the column positions have changed, |
4492 | // unless we calculate them dynamically because all columns widths are the | |
4493 | // same and it's easy to do | |
4494 | if ( !m_colWidths.empty() ) | |
009c7216 | 4495 | { |
4797b014 VZ |
4496 | int colRight = 0; |
4497 | for ( int colPos = 0; colPos < m_numCols; colPos++ ) | |
4498 | { | |
4499 | int colID = GetColAt( colPos ); | |
4500 | ||
4501 | colRight += m_colWidths[colID]; | |
4502 | m_colRights[colID] = colRight; | |
4503 | } | |
4504 | } | |
009c7216 | 4505 | |
4797b014 VZ |
4506 | // and make the changes visible |
4507 | if ( m_useNativeHeader ) | |
4508 | { | |
4509 | if ( m_colAt.empty() ) | |
3039ade9 | 4510 | GetGridColHeader()->ResetColumnsOrder(); |
4797b014 | 4511 | else |
3039ade9 | 4512 | GetGridColHeader()->SetColumnsOrder(m_colAt); |
4797b014 VZ |
4513 | } |
4514 | else | |
4515 | { | |
4516 | m_colWindow->Refresh(); | |
009c7216 | 4517 | } |
4797b014 | 4518 | m_gridWin->Refresh(); |
009c7216 VZ |
4519 | } |
4520 | ||
31ec8b4e VZ |
4521 | void wxGrid::SetColumnsOrder(const wxArrayInt& order) |
4522 | { | |
4523 | m_colAt = order; | |
4524 | ||
4525 | RefreshAfterColPosChange(); | |
4526 | } | |
4527 | ||
3169a8e8 | 4528 | void wxGrid::SetColPos(int idx, int pos) |
d4175745 | 4529 | { |
1bb74626 | 4530 | // we're going to need m_colAt now, initialize it if needed |
3169a8e8 | 4531 | if ( m_colAt.empty() ) |
d4175745 | 4532 | { |
3169a8e8 VZ |
4533 | m_colAt.reserve(m_numCols); |
4534 | for ( int i = 0; i < m_numCols; i++ ) | |
4535 | m_colAt.push_back(i); | |
d4175745 VZ |
4536 | } |
4537 | ||
1bb74626 | 4538 | wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos); |
d4175745 | 4539 | |
4797b014 | 4540 | RefreshAfterColPosChange(); |
d4175745 VZ |
4541 | } |
4542 | ||
009c7216 VZ |
4543 | void wxGrid::ResetColPos() |
4544 | { | |
4545 | m_colAt.clear(); | |
da5a641f | 4546 | |
4797b014 | 4547 | RefreshAfterColPosChange(); |
009c7216 | 4548 | } |
d4175745 VZ |
4549 | |
4550 | void wxGrid::EnableDragColMove( bool enable ) | |
4551 | { | |
4552 | if ( m_canDragColMove == enable ) | |
4553 | return; | |
4554 | ||
009c7216 | 4555 | if ( m_useNativeHeader ) |
d4175745 | 4556 | { |
009c7216 | 4557 | // update all columns to make them [not] reorderable |
3039ade9 | 4558 | GetGridColHeader()->SetColumnCount(m_numCols); |
009c7216 | 4559 | } |
d4175745 | 4560 | |
009c7216 | 4561 | m_canDragColMove = enable; |
d4175745 | 4562 | |
009c7216 VZ |
4563 | // we use to call ResetColPos() from here if !enable but this doesn't seem |
4564 | // right as it would mean there would be no way to "freeze" the current | |
4565 | // columns order by disabling moving them after putting them in the desired | |
4566 | // order, whereas now you can always call ResetColPos() manually if needed | |
d4175745 VZ |
4567 | } |
4568 | ||
4569 | ||
2d66e025 MB |
4570 | // |
4571 | // ------ interaction with data model | |
4572 | // | |
4573 | bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg ) | |
f85afd4e | 4574 | { |
2d66e025 | 4575 | switch ( msg.GetId() ) |
17732cec | 4576 | { |
2d66e025 MB |
4577 | case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES: |
4578 | return GetModelValues(); | |
17732cec | 4579 | |
2d66e025 MB |
4580 | case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES: |
4581 | return SetModelValues(); | |
f85afd4e | 4582 | |
2d66e025 MB |
4583 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: |
4584 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
4585 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
4586 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
4587 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
4588 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
4589 | return Redimension( msg ); | |
4590 | ||
4591 | default: | |
ca65c044 | 4592 | return false; |
f85afd4e | 4593 | } |
2d66e025 | 4594 | } |
f85afd4e | 4595 | |
2d66e025 | 4596 | // The behaviour of this function depends on the grid table class |
5b061713 | 4597 | // Clear() function. For the default wxGridStringTable class the |
10a4531d | 4598 | // behaviour is to replace all cell contents with wxEmptyString but |
2d66e025 MB |
4599 | // not to change the number of rows or cols. |
4600 | // | |
4601 | void wxGrid::ClearGrid() | |
4602 | { | |
4603 | if ( m_table ) | |
f85afd4e | 4604 | { |
4cfa5de6 RD |
4605 | if (IsCellEditControlEnabled()) |
4606 | DisableCellEditControl(); | |
4607 | ||
2d66e025 | 4608 | m_table->Clear(); |
5b061713 | 4609 | if (!GetBatchCount()) |
a9339fe2 | 4610 | m_gridWin->Refresh(); |
f85afd4e MB |
4611 | } |
4612 | } | |
4613 | ||
10a4531d VZ |
4614 | bool |
4615 | wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify)(size_t, size_t), | |
4616 | int pos, int num, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 4617 | { |
10a4531d | 4618 | wxCHECK_MSG( m_created, false, "must finish creating the grid first" ); |
f85afd4e | 4619 | |
10a4531d | 4620 | if ( !m_table ) |
ca65c044 | 4621 | return false; |
f85afd4e | 4622 | |
10a4531d VZ |
4623 | if ( IsCellEditControlEnabled() ) |
4624 | DisableCellEditControl(); | |
b7fff980 | 4625 | |
10a4531d | 4626 | return (m_table->*funcModify)(pos, num); |
2f024384 | 4627 | |
10a4531d VZ |
4628 | // the table will have sent the results of the insert row |
4629 | // operation to this view object as a grid table message | |
f85afd4e MB |
4630 | } |
4631 | ||
10a4531d VZ |
4632 | bool |
4633 | wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend)(size_t), | |
4634 | int num, bool WXUNUSED(updateLabels)) | |
f85afd4e | 4635 | { |
10a4531d | 4636 | wxCHECK_MSG( m_created, false, "must finish creating the grid first" ); |
2f024384 | 4637 | |
10a4531d | 4638 | if ( !m_table ) |
ca65c044 | 4639 | return false; |
f85afd4e | 4640 | |
10a4531d | 4641 | return (m_table->*funcAppend)(num); |
f85afd4e MB |
4642 | } |
4643 | ||
27bb2c8c VZ |
4644 | // ---------------------------------------------------------------------------- |
4645 | // event generation helpers | |
4646 | // ---------------------------------------------------------------------------- | |
4647 | ||
6f58f3d7 | 4648 | bool |
c5c1ea96 | 4649 | wxGrid::SendGridSizeEvent(wxEventType type, |
27bb2c8c VZ |
4650 | int row, int col, |
4651 | const wxMouseEvent& mouseEv) | |
4652 | { | |
4653 | int rowOrCol = row == -1 ? col : row; | |
4654 | ||
4655 | wxGridSizeEvent gridEvt( GetId(), | |
4656 | type, | |
4657 | this, | |
4658 | rowOrCol, | |
4659 | mouseEv.GetX() + GetRowLabelSize(), | |
4660 | mouseEv.GetY() + GetColLabelSize(), | |
4661 | mouseEv); | |
4662 | ||
6f58f3d7 | 4663 | return GetEventHandler()->ProcessEvent(gridEvt); |
27bb2c8c | 4664 | } |
8f177c8e | 4665 | |
8a3e536c VZ |
4666 | // Generate a grid event based on a mouse event and return: |
4667 | // -1 if the event was vetoed | |
4668 | // +1 if the event was processed (but not vetoed) | |
4669 | // 0 if the event wasn't handled | |
4670 | int | |
aced1133 | 4671 | wxGrid::SendEvent(const wxEventType type, |
8a3e536c | 4672 | int row, int col, |
27bb2c8c | 4673 | const wxMouseEvent& mouseEv) |
2d66e025 | 4674 | { |
2f024384 | 4675 | bool claimed, vetoed; |
fe7b9ed6 | 4676 | |
27bb2c8c | 4677 | if ( type == wxEVT_GRID_RANGE_SELECT ) |
97a9929e VZ |
4678 | { |
4679 | // Right now, it should _never_ end up here! | |
4680 | wxGridRangeSelectEvent gridEvt( GetId(), | |
4681 | type, | |
4682 | this, | |
8a3e536c VZ |
4683 | m_selectedBlockTopLeft, |
4684 | m_selectedBlockBottomRight, | |
ca65c044 | 4685 | true, |
8b5f6d9d | 4686 | mouseEv); |
97a9929e VZ |
4687 | |
4688 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
4689 | vetoed = !gridEvt.IsAllowed(); | |
4690 | } | |
2b73a34e RD |
4691 | else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK || |
4692 | type == wxEVT_GRID_LABEL_LEFT_DCLICK || | |
4693 | type == wxEVT_GRID_LABEL_RIGHT_CLICK || | |
4694 | type == wxEVT_GRID_LABEL_RIGHT_DCLICK ) | |
4695 | { | |
4696 | wxPoint pos = mouseEv.GetPosition(); | |
4697 | ||
4698 | if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() ) | |
4699 | pos.y += GetColLabelSize(); | |
4700 | if ( mouseEv.GetEventObject() == GetGridColLabelWindow() ) | |
4701 | pos.x += GetRowLabelSize(); | |
c71b2126 | 4702 | |
2b73a34e RD |
4703 | wxGridEvent gridEvt( GetId(), |
4704 | type, | |
4705 | this, | |
4706 | row, col, | |
4707 | pos.x, | |
4708 | pos.y, | |
4709 | false, | |
8b5f6d9d | 4710 | mouseEv); |
2b73a34e RD |
4711 | claimed = GetEventHandler()->ProcessEvent(gridEvt); |
4712 | vetoed = !gridEvt.IsAllowed(); | |
4713 | } | |
fe7b9ed6 | 4714 | else |
97a9929e VZ |
4715 | { |
4716 | wxGridEvent gridEvt( GetId(), | |
4717 | type, | |
4718 | this, | |
4719 | row, col, | |
4720 | mouseEv.GetX() + GetRowLabelSize(), | |
4721 | mouseEv.GetY() + GetColLabelSize(), | |
ca65c044 | 4722 | false, |
8b5f6d9d | 4723 | mouseEv); |
0cbcb12d SC |
4724 | |
4725 | if ( type == wxEVT_GRID_CELL_BEGIN_DRAG ) | |
4726 | { | |
4727 | // by default the dragging is not supported, the user code must | |
4728 | // explicitly allow the event for it to take place | |
4729 | gridEvt.Veto(); | |
4730 | } | |
4731 | ||
97a9929e VZ |
4732 | claimed = GetEventHandler()->ProcessEvent(gridEvt); |
4733 | vetoed = !gridEvt.IsAllowed(); | |
4734 | } | |
4735 | ||
4736 | // A Veto'd event may not be `claimed' so test this first | |
4db6714b KH |
4737 | if (vetoed) |
4738 | return -1; | |
4739 | ||
97a9929e | 4740 | return claimed ? 1 : 0; |
f85afd4e MB |
4741 | } |
4742 | ||
8a3e536c | 4743 | // Generate a grid event of specified type, return value same as above |
f85afd4e | 4744 | // |
763163a8 VZ |
4745 | int |
4746 | wxGrid::SendEvent(const wxEventType type, int row, int col, const wxString& s) | |
f85afd4e | 4747 | { |
27bb2c8c VZ |
4748 | wxGridEvent gridEvt( GetId(), type, this, row, col ); |
4749 | gridEvt.SetString(s); | |
8f177c8e | 4750 | |
27bb2c8c | 4751 | const bool claimed = GetEventHandler()->ProcessEvent(gridEvt); |
fe7b9ed6 | 4752 | |
97a9929e | 4753 | // A Veto'd event may not be `claimed' so test this first |
27bb2c8c | 4754 | if ( !gridEvt.IsAllowed() ) |
4db6714b KH |
4755 | return -1; |
4756 | ||
97a9929e | 4757 | return claimed ? 1 : 0; |
f85afd4e MB |
4758 | } |
4759 | ||
2d66e025 | 4760 | void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
f85afd4e | 4761 | { |
3d59537f DS |
4762 | // needed to prevent zillions of paint events on MSW |
4763 | wxPaintDC dc(this); | |
8f177c8e | 4764 | } |
f85afd4e | 4765 | |
bca7bfc8 | 4766 | void wxGrid::Refresh(bool eraseb, const wxRect* rect) |
27f35b66 | 4767 | { |
ad603bf7 VZ |
4768 | // Don't do anything if between Begin/EndBatch... |
4769 | // EndBatch() will do all this on the last nested one anyway. | |
f9549841 | 4770 | if ( m_created && !GetBatchCount() ) |
27f35b66 | 4771 | { |
bca7bfc8 | 4772 | // Refresh to get correct scrolled position: |
4db6714b | 4773 | wxScrolledWindow::Refresh(eraseb, rect); |
27f35b66 | 4774 | |
27f35b66 SN |
4775 | if (rect) |
4776 | { | |
bca7bfc8 SN |
4777 | int rect_x, rect_y, rectWidth, rectHeight; |
4778 | int width_label, width_cell, height_label, height_cell; | |
4779 | int x, y; | |
4780 | ||
2f024384 | 4781 | // Copy rectangle can get scroll offsets.. |
bca7bfc8 SN |
4782 | rect_x = rect->GetX(); |
4783 | rect_y = rect->GetY(); | |
4784 | rectWidth = rect->GetWidth(); | |
4785 | rectHeight = rect->GetHeight(); | |
27f35b66 | 4786 | |
bca7bfc8 | 4787 | width_label = m_rowLabelWidth - rect_x; |
4db6714b KH |
4788 | if (width_label > rectWidth) |
4789 | width_label = rectWidth; | |
27f35b66 | 4790 | |
bca7bfc8 | 4791 | height_label = m_colLabelHeight - rect_y; |
a9339fe2 DS |
4792 | if (height_label > rectHeight) |
4793 | height_label = rectHeight; | |
27f35b66 | 4794 | |
bca7bfc8 SN |
4795 | if (rect_x > m_rowLabelWidth) |
4796 | { | |
4797 | x = rect_x - m_rowLabelWidth; | |
4798 | width_cell = rectWidth; | |
4799 | } | |
4800 | else | |
4801 | { | |
4802 | x = 0; | |
4803 | width_cell = rectWidth - (m_rowLabelWidth - rect_x); | |
4804 | } | |
4805 | ||
4806 | if (rect_y > m_colLabelHeight) | |
4807 | { | |
4808 | y = rect_y - m_colLabelHeight; | |
4809 | height_cell = rectHeight; | |
4810 | } | |
4811 | else | |
4812 | { | |
4813 | y = 0; | |
4814 | height_cell = rectHeight - (m_colLabelHeight - rect_y); | |
4815 | } | |
4816 | ||
4817 | // Paint corner label part intersecting rect. | |
4818 | if ( width_label > 0 && height_label > 0 ) | |
4819 | { | |
4820 | wxRect anotherrect(rect_x, rect_y, width_label, height_label); | |
4821 | m_cornerLabelWin->Refresh(eraseb, &anotherrect); | |
4822 | } | |
4823 | ||
4824 | // Paint col labels part intersecting rect. | |
4825 | if ( width_cell > 0 && height_label > 0 ) | |
4826 | { | |
4827 | wxRect anotherrect(x, rect_y, width_cell, height_label); | |
ad805b9e | 4828 | m_colWindow->Refresh(eraseb, &anotherrect); |
bca7bfc8 SN |
4829 | } |
4830 | ||
4831 | // Paint row labels part intersecting rect. | |
4832 | if ( width_label > 0 && height_cell > 0 ) | |
4833 | { | |
4834 | wxRect anotherrect(rect_x, y, width_label, height_cell); | |
4835 | m_rowLabelWin->Refresh(eraseb, &anotherrect); | |
4836 | } | |
4837 | ||
4838 | // Paint cell area part intersecting rect. | |
4839 | if ( width_cell > 0 && height_cell > 0 ) | |
4840 | { | |
4841 | wxRect anotherrect(x, y, width_cell, height_cell); | |
4842 | m_gridWin->Refresh(eraseb, &anotherrect); | |
4843 | } | |
4844 | } | |
4845 | else | |
4846 | { | |
4847 | m_cornerLabelWin->Refresh(eraseb, NULL); | |
ad805b9e | 4848 | m_colWindow->Refresh(eraseb, NULL); |
bca7bfc8 SN |
4849 | m_rowLabelWin->Refresh(eraseb, NULL); |
4850 | m_gridWin->Refresh(eraseb, NULL); | |
4851 | } | |
27f35b66 SN |
4852 | } |
4853 | } | |
f85afd4e | 4854 | |
c71b2126 | 4855 | void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event)) |
f85afd4e | 4856 | { |
b93aafab JS |
4857 | if (m_targetWindow != this) // check whether initialisation has been done |
4858 | { | |
f1ff7df0 VZ |
4859 | // reposition our children windows |
4860 | CalcWindowSizes(); | |
b93aafab | 4861 | } |
f85afd4e MB |
4862 | } |
4863 | ||
2d66e025 | 4864 | void wxGrid::OnKeyDown( wxKeyEvent& event ) |
f85afd4e | 4865 | { |
2d66e025 | 4866 | if ( m_inOnKeyDown ) |
f85afd4e | 4867 | { |
2d66e025 MB |
4868 | // shouldn't be here - we are going round in circles... |
4869 | // | |
07296f0b | 4870 | wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") ); |
f85afd4e MB |
4871 | } |
4872 | ||
ca65c044 | 4873 | m_inOnKeyDown = true; |
f85afd4e | 4874 | |
2d66e025 | 4875 | // propagate the event up and see if it gets processed |
2d66e025 MB |
4876 | wxWindow *parent = GetParent(); |
4877 | wxKeyEvent keyEvt( event ); | |
4878 | keyEvt.SetEventObject( parent ); | |
4879 | ||
4880 | if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) ) | |
f85afd4e | 4881 | { |
bcb614b3 RR |
4882 | if (GetLayoutDirection() == wxLayout_RightToLeft) |
4883 | { | |
4884 | if (event.GetKeyCode() == WXK_RIGHT) | |
4885 | event.m_keyCode = WXK_LEFT; | |
4886 | else if (event.GetKeyCode() == WXK_LEFT) | |
4887 | event.m_keyCode = WXK_RIGHT; | |
4888 | } | |
c71b2126 | 4889 | |
2d66e025 | 4890 | // try local handlers |
12a3f227 | 4891 | switch ( event.GetKeyCode() ) |
2d66e025 MB |
4892 | { |
4893 | case WXK_UP: | |
4894 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4895 | MoveCursorUpBlock( event.ShiftDown() ); |
2d66e025 | 4896 | else |
5c8fc7c1 | 4897 | MoveCursorUp( event.ShiftDown() ); |
2d66e025 | 4898 | break; |
f85afd4e | 4899 | |
2d66e025 MB |
4900 | case WXK_DOWN: |
4901 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4902 | MoveCursorDownBlock( event.ShiftDown() ); |
2d66e025 | 4903 | else |
5c8fc7c1 | 4904 | MoveCursorDown( event.ShiftDown() ); |
2d66e025 | 4905 | break; |
8f177c8e | 4906 | |
2d66e025 MB |
4907 | case WXK_LEFT: |
4908 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4909 | MoveCursorLeftBlock( event.ShiftDown() ); |
2d66e025 | 4910 | else |
5c8fc7c1 | 4911 | MoveCursorLeft( event.ShiftDown() ); |
2d66e025 MB |
4912 | break; |
4913 | ||
4914 | case WXK_RIGHT: | |
4915 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4916 | MoveCursorRightBlock( event.ShiftDown() ); |
2d66e025 | 4917 | else |
5c8fc7c1 | 4918 | MoveCursorRight( event.ShiftDown() ); |
2d66e025 | 4919 | break; |
b99be8fb | 4920 | |
2d66e025 | 4921 | case WXK_RETURN: |
a4f7bf58 | 4922 | case WXK_NUMPAD_ENTER: |
58dd5b3b MB |
4923 | if ( event.ControlDown() ) |
4924 | { | |
4925 | event.Skip(); // to let the edit control have the return | |
4926 | } | |
4927 | else | |
4928 | { | |
f6bcfd97 BP |
4929 | if ( GetGridCursorRow() < GetNumberRows()-1 ) |
4930 | { | |
4931 | MoveCursorDown( event.ShiftDown() ); | |
4932 | } | |
4933 | else | |
4934 | { | |
4935 | // at the bottom of a column | |
13f6e9e8 | 4936 | DisableCellEditControl(); |
f6bcfd97 | 4937 | } |
58dd5b3b | 4938 | } |
2d66e025 MB |
4939 | break; |
4940 | ||
5c8fc7c1 | 4941 | case WXK_ESCAPE: |
e32352cf | 4942 | ClearSelection(); |
5c8fc7c1 SN |
4943 | break; |
4944 | ||
2c9a89e0 | 4945 | case WXK_TAB: |
f6bcfd97 | 4946 | { |
1dc17bca VZ |
4947 | // send an event to the grid's parents for custom handling |
4948 | wxGridEvent gridEvt(GetId(), wxEVT_GRID_TABBING, this, | |
4949 | GetGridCursorRow(), GetGridCursorCol(), | |
4950 | -1, -1, false, event); | |
4951 | if ( ProcessWindowEvent(gridEvt) ) | |
f6bcfd97 | 4952 | { |
1dc17bca VZ |
4953 | // the event has been handled so no need for more processing |
4954 | break; | |
f6bcfd97 BP |
4955 | } |
4956 | } | |
1dc17bca | 4957 | DoGridProcessTab( event ); |
2c9a89e0 RD |
4958 | break; |
4959 | ||
2d66e025 | 4960 | case WXK_HOME: |
ad178a65 VZ |
4961 | GoToCell(event.ControlDown() ? 0 |
4962 | : m_currentCellCoords.GetRow(), | |
4963 | 0); | |
2d66e025 MB |
4964 | break; |
4965 | ||
4966 | case WXK_END: | |
ad178a65 VZ |
4967 | GoToCell(event.ControlDown() ? m_numRows - 1 |
4968 | : m_currentCellCoords.GetRow(), | |
4969 | m_numCols - 1); | |
2d66e025 MB |
4970 | break; |
4971 | ||
faa94f3e | 4972 | case WXK_PAGEUP: |
2d66e025 MB |
4973 | MovePageUp(); |
4974 | break; | |
4975 | ||
faa94f3e | 4976 | case WXK_PAGEDOWN: |
2d66e025 MB |
4977 | MovePageDown(); |
4978 | break; | |
4979 | ||
07296f0b | 4980 | case WXK_SPACE: |
32b4e9ec VZ |
4981 | // Ctrl-Space selects the current column, Shift-Space -- the |
4982 | // current row and Ctrl-Shift-Space -- everything | |
4983 | switch ( m_selection ? event.GetModifiers() : wxMOD_NONE ) | |
aa5e1f75 | 4984 | { |
32b4e9ec VZ |
4985 | case wxMOD_CONTROL: |
4986 | m_selection->SelectCol(m_currentCellCoords.GetCol()); | |
4987 | break; | |
ccdee36f | 4988 | |
32b4e9ec VZ |
4989 | case wxMOD_SHIFT: |
4990 | m_selection->SelectRow(m_currentCellCoords.GetRow()); | |
4991 | break; | |
4992 | ||
4993 | case wxMOD_CONTROL | wxMOD_SHIFT: | |
4994 | m_selection->SelectBlock(0, 0, | |
4995 | m_numRows - 1, m_numCols - 1); | |
4996 | break; | |
4997 | ||
4998 | case wxMOD_NONE: | |
4999 | if ( !IsEditable() ) | |
5000 | { | |
5001 | MoveCursorRight(false); | |
5002 | break; | |
5003 | } | |
5004 | //else: fall through | |
5005 | ||
5006 | default: | |
5007 | event.Skip(); | |
5008 | } | |
ccdee36f | 5009 | break; |
07296f0b | 5010 | |
2d66e025 | 5011 | default: |
63e2147c | 5012 | event.Skip(); |
025562fe | 5013 | break; |
2d66e025 | 5014 | } |
f85afd4e MB |
5015 | } |
5016 | ||
ca65c044 | 5017 | m_inOnKeyDown = false; |
f85afd4e MB |
5018 | } |
5019 | ||
f6bcfd97 BP |
5020 | void wxGrid::OnKeyUp( wxKeyEvent& event ) |
5021 | { | |
5022 | // try local handlers | |
5023 | // | |
12a3f227 | 5024 | if ( event.GetKeyCode() == WXK_SHIFT ) |
f6bcfd97 | 5025 | { |
8a3e536c VZ |
5026 | if ( m_selectedBlockTopLeft != wxGridNoCellCoords && |
5027 | m_selectedBlockBottomRight != wxGridNoCellCoords ) | |
3f3dc2ef VZ |
5028 | { |
5029 | if ( m_selection ) | |
5030 | { | |
a9339fe2 | 5031 | m_selection->SelectBlock( |
8a3e536c VZ |
5032 | m_selectedBlockTopLeft, |
5033 | m_selectedBlockBottomRight, | |
8b5f6d9d | 5034 | event); |
3f3dc2ef VZ |
5035 | } |
5036 | } | |
5037 | ||
8a3e536c VZ |
5038 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
5039 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
5040 | m_selectedBlockCorner = wxGridNoCellCoords; | |
f6bcfd97 BP |
5041 | } |
5042 | } | |
5043 | ||
63e2147c RD |
5044 | void wxGrid::OnChar( wxKeyEvent& event ) |
5045 | { | |
5046 | // is it possible to edit the current cell at all? | |
5047 | if ( !IsCellEditControlEnabled() && CanEnableCellControl() ) | |
5048 | { | |
5049 | // yes, now check whether the cells editor accepts the key | |
5050 | int row = m_currentCellCoords.GetRow(); | |
5051 | int col = m_currentCellCoords.GetCol(); | |
2f024384 | 5052 | wxGridCellAttr *attr = GetCellAttr(row, col); |
63e2147c RD |
5053 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
5054 | ||
5055 | // <F2> is special and will always start editing, for | |
5056 | // other keys - ask the editor itself | |
5057 | if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers()) | |
5058 | || editor->IsAcceptedKey(event) ) | |
5059 | { | |
5060 | // ensure cell is visble | |
5061 | MakeCellVisible(row, col); | |
5062 | EnableCellEditControl(); | |
5063 | ||
5064 | // a problem can arise if the cell is not completely | |
5065 | // visible (even after calling MakeCellVisible the | |
5066 | // control is not created and calling StartingKey will | |
5067 | // crash the app | |
046d682f | 5068 | if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled ) |
63e2147c RD |
5069 | editor->StartingKey(event); |
5070 | } | |
5071 | else | |
5072 | { | |
5073 | event.Skip(); | |
5074 | } | |
5075 | ||
5076 | editor->DecRef(); | |
5077 | attr->DecRef(); | |
5078 | } | |
5079 | else | |
5080 | { | |
5081 | event.Skip(); | |
5082 | } | |
5083 | } | |
5084 | ||
2796cce3 | 5085 | void wxGrid::OnEraseBackground(wxEraseEvent&) |
508011ce VZ |
5086 | { |
5087 | } | |
07296f0b | 5088 | |
1dc17bca VZ |
5089 | void wxGrid::DoGridProcessTab(wxKeyboardState& kbdState) |
5090 | { | |
5091 | const bool isForwardTab = !kbdState.ShiftDown(); | |
5092 | ||
5093 | // TAB processing only changes when we are at the borders of the grid, so | |
5094 | // let's first handle the common behaviour when we are not at the border. | |
5095 | if ( isForwardTab ) | |
5096 | { | |
5097 | if ( GetGridCursorCol() < GetNumberCols() - 1 ) | |
5098 | { | |
5099 | MoveCursorRight( false ); | |
5100 | return; | |
5101 | } | |
5102 | } | |
5103 | else // going back | |
5104 | { | |
5105 | if ( GetGridCursorCol() ) | |
5106 | { | |
5107 | MoveCursorLeft( false ); | |
5108 | return; | |
5109 | } | |
5110 | } | |
5111 | ||
5112 | ||
5113 | // We only get here if the cursor is at the border of the grid, apply the | |
5114 | // configured behaviour. | |
5115 | switch ( m_tabBehaviour ) | |
5116 | { | |
5117 | case Tab_Stop: | |
5118 | // Nothing special to do, we remain at the current cell. | |
5119 | break; | |
5120 | ||
5121 | case Tab_Wrap: | |
5122 | // Go to the beginning of the next or the end of the previous row. | |
5123 | if ( isForwardTab ) | |
5124 | { | |
5125 | if ( GetGridCursorRow() < GetNumberRows() - 1 ) | |
5126 | { | |
5127 | GoToCell( GetGridCursorRow() + 1, 0 ); | |
5128 | return; | |
5129 | } | |
5130 | } | |
5131 | else | |
5132 | { | |
5133 | if ( GetGridCursorRow() > 0 ) | |
5134 | { | |
5135 | GoToCell( GetGridCursorRow() - 1, GetNumberCols() - 1 ); | |
5136 | return; | |
5137 | } | |
5138 | } | |
5139 | break; | |
5140 | ||
5141 | case Tab_Leave: | |
5142 | if ( Navigate( isForwardTab ? wxNavigationKeyEvent::IsForward | |
5143 | : wxNavigationKeyEvent::IsBackward ) ) | |
5144 | return; | |
5145 | break; | |
5146 | } | |
5147 | ||
5148 | // If we remain in this cell, stop editing it if we were doing so. | |
5149 | DisableCellEditControl(); | |
5150 | } | |
5151 | ||
8a3e536c | 5152 | bool wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) |
66242c80 | 5153 | { |
8a3e536c | 5154 | if ( SendEvent(wxEVT_GRID_SELECT_CELL, coords) == -1 ) |
f6bcfd97 | 5155 | { |
8a3e536c VZ |
5156 | // the event has been vetoed - do nothing |
5157 | return false; | |
f6bcfd97 BP |
5158 | } |
5159 | ||
9553702e | 5160 | #if !defined(__WXMAC__) |
bee19958 DS |
5161 | wxClientDC dc( m_gridWin ); |
5162 | PrepareDC( dc ); | |
5d38a5f3 | 5163 | #endif |
f6bcfd97 | 5164 | |
2a7750d9 | 5165 | if ( m_currentCellCoords != wxGridNoCellCoords ) |
2d66e025 | 5166 | { |
b54ba671 | 5167 | DisableCellEditControl(); |
07296f0b | 5168 | |
ca65c044 | 5169 | if ( IsVisible( m_currentCellCoords, false ) ) |
f6bcfd97 BP |
5170 | { |
5171 | wxRect r; | |
bee19958 | 5172 | r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords ); |
f6bcfd97 BP |
5173 | if ( !m_gridLinesEnabled ) |
5174 | { | |
5175 | r.x--; | |
5176 | r.y--; | |
5177 | r.width++; | |
5178 | r.height++; | |
5179 | } | |
d1c0b4f9 | 5180 | |
3ed884a0 | 5181 | wxGridCellCoordsArray cells = CalcCellsExposed( r ); |
84912ef8 | 5182 | |
f6bcfd97 BP |
5183 | // Otherwise refresh redraws the highlight! |
5184 | m_currentCellCoords = coords; | |
275c4ae4 | 5185 | |
9553702e | 5186 | #if defined(__WXMAC__) |
5d38a5f3 JS |
5187 | m_gridWin->Refresh(true /*, & r */); |
5188 | #else | |
bee19958 | 5189 | DrawGridCellArea( dc, cells ); |
f6bcfd97 | 5190 | DrawAllGridLines( dc, r ); |
5d38a5f3 | 5191 | #endif |
f6bcfd97 | 5192 | } |
66242c80 | 5193 | } |
8f177c8e | 5194 | |
2d66e025 MB |
5195 | m_currentCellCoords = coords; |
5196 | ||
bee19958 | 5197 | wxGridCellAttr *attr = GetCellAttr( coords ); |
76c66f19 | 5198 | #if !defined(__WXMAC__) |
bee19958 | 5199 | DrawCellHighlight( dc, attr ); |
5d38a5f3 | 5200 | #endif |
2a7750d9 | 5201 | attr->DecRef(); |
8a3e536c VZ |
5202 | |
5203 | return true; | |
66242c80 MB |
5204 | } |
5205 | ||
8a3e536c VZ |
5206 | void |
5207 | wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol, | |
5208 | int bottomRow, int rightCol) | |
c9097836 | 5209 | { |
4445d035 VZ |
5210 | MakeCellVisible(m_selectedBlockCorner); |
5211 | m_selectedBlockCorner = wxGridCellCoords(bottomRow, rightCol); | |
5212 | ||
3f3dc2ef | 5213 | if ( m_selection ) |
c9097836 | 5214 | { |
8a3e536c | 5215 | switch ( m_selection->GetSelectionMode() ) |
3f3dc2ef | 5216 | { |
8a3e536c VZ |
5217 | default: |
5218 | wxFAIL_MSG( "unknown selection mode" ); | |
5219 | // fall through | |
5220 | ||
5221 | case wxGridSelectCells: | |
5222 | // arbitrary blocks selection allowed so just use the cell | |
5223 | // coordinates as is | |
5224 | break; | |
5225 | ||
5226 | case wxGridSelectRows: | |
5227 | // only full rows selection allowd, ensure that we do select | |
5228 | // full rows | |
5229 | leftCol = 0; | |
5230 | rightCol = GetNumberCols() - 1; | |
5231 | break; | |
5232 | ||
5233 | case wxGridSelectColumns: | |
5234 | // same as above but for columns | |
5235 | topRow = 0; | |
5236 | bottomRow = GetNumberRows() - 1; | |
5237 | break; | |
5238 | ||
5239 | case wxGridSelectRowsOrColumns: | |
5240 | // in this mode we can select only full rows or full columns so | |
5241 | // it doesn't make sense to select blocks at all (and we can't | |
5242 | // extend the block because there is no preferred direction, we | |
5243 | // could only extend it to cover the entire grid but this is | |
5244 | // not useful) | |
5245 | return; | |
3f3dc2ef | 5246 | } |
c9097836 | 5247 | } |
3f3dc2ef | 5248 | |
1372f8cc VZ |
5249 | EnsureFirstLessThanSecond(topRow, bottomRow); |
5250 | EnsureFirstLessThanSecond(leftCol, rightCol); | |
c9097836 | 5251 | |
8a3e536c VZ |
5252 | wxGridCellCoords updateTopLeft = wxGridCellCoords(topRow, leftCol), |
5253 | updateBottomRight = wxGridCellCoords(bottomRow, rightCol); | |
c9097836 | 5254 | |
3ed884a0 | 5255 | // First the case that we selected a completely new area |
8a3e536c VZ |
5256 | if ( m_selectedBlockTopLeft == wxGridNoCellCoords || |
5257 | m_selectedBlockBottomRight == wxGridNoCellCoords ) | |
3ed884a0 SN |
5258 | { |
5259 | wxRect rect; | |
5260 | rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ), | |
5261 | wxGridCellCoords ( bottomRow, rightCol ) ); | |
ca65c044 | 5262 | m_gridWin->Refresh( false, &rect ); |
3ed884a0 | 5263 | } |
2f024384 | 5264 | |
3ed884a0 | 5265 | // Now handle changing an existing selection area. |
8a3e536c VZ |
5266 | else if ( m_selectedBlockTopLeft != updateTopLeft || |
5267 | m_selectedBlockBottomRight != updateBottomRight ) | |
c9097836 MB |
5268 | { |
5269 | // Compute two optimal update rectangles: | |
5270 | // Either one rectangle is a real subset of the | |
5271 | // other, or they are (almost) disjoint! | |
5272 | wxRect rect[4]; | |
5273 | bool need_refresh[4]; | |
5274 | need_refresh[0] = | |
5275 | need_refresh[1] = | |
5276 | need_refresh[2] = | |
ca65c044 | 5277 | need_refresh[3] = false; |
c9097836 MB |
5278 | int i; |
5279 | ||
5280 | // Store intermediate values | |
8a3e536c VZ |
5281 | wxCoord oldLeft = m_selectedBlockTopLeft.GetCol(); |
5282 | wxCoord oldTop = m_selectedBlockTopLeft.GetRow(); | |
5283 | wxCoord oldRight = m_selectedBlockBottomRight.GetCol(); | |
5284 | wxCoord oldBottom = m_selectedBlockBottomRight.GetRow(); | |
c9097836 MB |
5285 | |
5286 | // Determine the outer/inner coordinates. | |
1372f8cc VZ |
5287 | EnsureFirstLessThanSecond(oldLeft, leftCol); |
5288 | EnsureFirstLessThanSecond(oldTop, topRow); | |
5289 | EnsureFirstLessThanSecond(rightCol, oldRight); | |
5290 | EnsureFirstLessThanSecond(bottomRow, oldBottom); | |
c9097836 MB |
5291 | |
5292 | // Now, either the stuff marked old is the outer | |
5293 | // rectangle or we don't have a situation where one | |
5294 | // is contained in the other. | |
5295 | ||
5296 | if ( oldLeft < leftCol ) | |
5297 | { | |
3ed884a0 SN |
5298 | // Refresh the newly selected or deselected |
5299 | // area to the left of the old or new selection. | |
ca65c044 | 5300 | need_refresh[0] = true; |
a9339fe2 DS |
5301 | rect[0] = BlockToDeviceRect( |
5302 | wxGridCellCoords( oldTop, oldLeft ), | |
5303 | wxGridCellCoords( oldBottom, leftCol - 1 ) ); | |
c9097836 MB |
5304 | } |
5305 | ||
2f024384 | 5306 | if ( oldTop < topRow ) |
c9097836 | 5307 | { |
3ed884a0 SN |
5308 | // Refresh the newly selected or deselected |
5309 | // area above the old or new selection. | |
ca65c044 | 5310 | need_refresh[1] = true; |
2f024384 DS |
5311 | rect[1] = BlockToDeviceRect( |
5312 | wxGridCellCoords( oldTop, leftCol ), | |
5313 | wxGridCellCoords( topRow - 1, rightCol ) ); | |
c9097836 MB |
5314 | } |
5315 | ||
5316 | if ( oldRight > rightCol ) | |
5317 | { | |
3ed884a0 SN |
5318 | // Refresh the newly selected or deselected |
5319 | // area to the right of the old or new selection. | |
ca65c044 | 5320 | need_refresh[2] = true; |
2f024384 | 5321 | rect[2] = BlockToDeviceRect( |
a9339fe2 DS |
5322 | wxGridCellCoords( oldTop, rightCol + 1 ), |
5323 | wxGridCellCoords( oldBottom, oldRight ) ); | |
c9097836 MB |
5324 | } |
5325 | ||
5326 | if ( oldBottom > bottomRow ) | |
5327 | { | |
3ed884a0 SN |
5328 | // Refresh the newly selected or deselected |
5329 | // area below the old or new selection. | |
ca65c044 | 5330 | need_refresh[3] = true; |
2f024384 | 5331 | rect[3] = BlockToDeviceRect( |
a9339fe2 DS |
5332 | wxGridCellCoords( bottomRow + 1, leftCol ), |
5333 | wxGridCellCoords( oldBottom, rightCol ) ); | |
c9097836 MB |
5334 | } |
5335 | ||
c9097836 MB |
5336 | // various Refresh() calls |
5337 | for (i = 0; i < 4; i++ ) | |
5338 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
ca65c044 | 5339 | m_gridWin->Refresh( false, &(rect[i]) ); |
c9097836 | 5340 | } |
2f024384 DS |
5341 | |
5342 | // change selection | |
8a3e536c VZ |
5343 | m_selectedBlockTopLeft = updateTopLeft; |
5344 | m_selectedBlockBottomRight = updateBottomRight; | |
c9097836 MB |
5345 | } |
5346 | ||
2d66e025 MB |
5347 | // |
5348 | // ------ functions to get/send data (see also public functions) | |
5349 | // | |
5350 | ||
5351 | bool wxGrid::GetModelValues() | |
66242c80 | 5352 | { |
bca7bfc8 SN |
5353 | // Hide the editor, so it won't hide a changed value. |
5354 | HideCellEditControl(); | |
c6707d16 | 5355 | |
2d66e025 | 5356 | if ( m_table ) |
66242c80 | 5357 | { |
2d66e025 | 5358 | // all we need to do is repaint the grid |
66242c80 | 5359 | // |
2d66e025 | 5360 | m_gridWin->Refresh(); |
ca65c044 | 5361 | return true; |
66242c80 | 5362 | } |
8f177c8e | 5363 | |
ca65c044 | 5364 | return false; |
66242c80 MB |
5365 | } |
5366 | ||
2d66e025 | 5367 | bool wxGrid::SetModelValues() |
f85afd4e | 5368 | { |
2d66e025 | 5369 | int row, col; |
8f177c8e | 5370 | |
c6707d16 SN |
5371 | // Disable the editor, so it won't hide a changed value. |
5372 | // Do we also want to save the current value of the editor first? | |
5373 | // I think so ... | |
c6707d16 SN |
5374 | DisableCellEditControl(); |
5375 | ||
2d66e025 MB |
5376 | if ( m_table ) |
5377 | { | |
56b6cf26 | 5378 | for ( row = 0; row < m_numRows; row++ ) |
f85afd4e | 5379 | { |
56b6cf26 | 5380 | for ( col = 0; col < m_numCols; col++ ) |
f85afd4e | 5381 | { |
2d66e025 | 5382 | m_table->SetValue( row, col, GetCellValue(row, col) ); |
f85afd4e MB |
5383 | } |
5384 | } | |
8f177c8e | 5385 | |
ca65c044 | 5386 | return true; |
f85afd4e MB |
5387 | } |
5388 | ||
ca65c044 | 5389 | return false; |
f85afd4e MB |
5390 | } |
5391 | ||
2d66e025 MB |
5392 | // Note - this function only draws cells that are in the list of |
5393 | // exposed cells (usually set from the update region by | |
5394 | // CalcExposedCells) | |
5395 | // | |
d10f4bf9 | 5396 | void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells ) |
f85afd4e | 5397 | { |
4db6714b KH |
5398 | if ( !m_numRows || !m_numCols ) |
5399 | return; | |
60ff3b99 | 5400 | |
dc1f566f | 5401 | int i, numCells = cells.GetCount(); |
27f35b66 SN |
5402 | int row, col, cell_rows, cell_cols; |
5403 | wxGridCellCoordsArray redrawCells; | |
60ff3b99 | 5404 | |
a9339fe2 | 5405 | for ( i = numCells - 1; i >= 0; i-- ) |
f85afd4e | 5406 | { |
27f35b66 SN |
5407 | row = cells[i].GetRow(); |
5408 | col = cells[i].GetCol(); | |
5409 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
5410 | ||
5411 | // If this cell is part of a multicell block, find owner for repaint | |
5412 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
5413 | { | |
a9339fe2 | 5414 | wxGridCellCoords cell( row + cell_rows, col + cell_cols ); |
ca65c044 | 5415 | bool marked = false; |
56b6cf26 | 5416 | for ( int j = 0; j < numCells; j++ ) |
27f35b66 SN |
5417 | { |
5418 | if ( cell == cells[j] ) | |
5419 | { | |
ca65c044 | 5420 | marked = true; |
3ed884a0 | 5421 | break; |
27f35b66 SN |
5422 | } |
5423 | } | |
2f024384 | 5424 | |
27f35b66 SN |
5425 | if (!marked) |
5426 | { | |
5427 | int count = redrawCells.GetCount(); | |
dc1f566f | 5428 | for (int j = 0; j < count; j++) |
27f35b66 SN |
5429 | { |
5430 | if ( cell == redrawCells[j] ) | |
5431 | { | |
ca65c044 | 5432 | marked = true; |
27f35b66 SN |
5433 | break; |
5434 | } | |
5435 | } | |
2f024384 | 5436 | |
4db6714b KH |
5437 | if (!marked) |
5438 | redrawCells.Add( cell ); | |
27f35b66 | 5439 | } |
2f024384 DS |
5440 | |
5441 | // don't bother drawing this cell | |
5442 | continue; | |
27f35b66 SN |
5443 | } |
5444 | ||
5445 | // If this cell is empty, find cell to left that might want to overflow | |
5446 | if (m_table && m_table->IsEmptyCell(row, col)) | |
5447 | { | |
dc1f566f | 5448 | for ( int l = 0; l < cell_rows; l++ ) |
27f35b66 | 5449 | { |
56b6cf26 | 5450 | // find a cell in this row to leave already marked for repaint |
dc1f566f SN |
5451 | int left = col; |
5452 | for (int k = 0; k < int(redrawCells.GetCount()); k++) | |
5453 | if ((redrawCells[k].GetCol() < left) && | |
5454 | (redrawCells[k].GetRow() == row)) | |
2f024384 | 5455 | { |
4db6714b | 5456 | left = redrawCells[k].GetCol(); |
2f024384 | 5457 | } |
dc1f566f | 5458 | |
4db6714b KH |
5459 | if (left == col) |
5460 | left = 0; // oh well | |
dc1f566f | 5461 | |
2f024384 | 5462 | for (int j = col - 1; j >= left; j--) |
27f35b66 | 5463 | { |
2f024384 | 5464 | if (!m_table->IsEmptyCell(row + l, j)) |
27f35b66 | 5465 | { |
2f024384 | 5466 | if (GetCellOverflow(row + l, j)) |
27f35b66 | 5467 | { |
2f024384 | 5468 | wxGridCellCoords cell(row + l, j); |
ca65c044 | 5469 | bool marked = false; |
27f35b66 | 5470 | |
dc1f566f | 5471 | for (int k = 0; k < numCells; k++) |
27f35b66 SN |
5472 | { |
5473 | if ( cell == cells[k] ) | |
5474 | { | |
ca65c044 | 5475 | marked = true; |
27f35b66 SN |
5476 | break; |
5477 | } | |
5478 | } | |
4db6714b | 5479 | |
27f35b66 SN |
5480 | if (!marked) |
5481 | { | |
5482 | int count = redrawCells.GetCount(); | |
dc1f566f | 5483 | for (int k = 0; k < count; k++) |
27f35b66 SN |
5484 | { |
5485 | if ( cell == redrawCells[k] ) | |
5486 | { | |
ca65c044 | 5487 | marked = true; |
27f35b66 SN |
5488 | break; |
5489 | } | |
5490 | } | |
4db6714b KH |
5491 | if (!marked) |
5492 | redrawCells.Add( cell ); | |
27f35b66 SN |
5493 | } |
5494 | } | |
5495 | break; | |
5496 | } | |
5497 | } | |
5498 | } | |
5499 | } | |
4db6714b | 5500 | |
d10f4bf9 | 5501 | DrawCell( dc, cells[i] ); |
2d66e025 | 5502 | } |
27f35b66 SN |
5503 | |
5504 | numCells = redrawCells.GetCount(); | |
5505 | ||
56b6cf26 | 5506 | for ( i = numCells - 1; i >= 0; i-- ) |
27f35b66 SN |
5507 | { |
5508 | DrawCell( dc, redrawCells[i] ); | |
5509 | } | |
2d66e025 | 5510 | } |
8f177c8e | 5511 | |
7c8a8ad5 MB |
5512 | void wxGrid::DrawGridSpace( wxDC& dc ) |
5513 | { | |
f6bcfd97 BP |
5514 | int cw, ch; |
5515 | m_gridWin->GetClientSize( &cw, &ch ); | |
7c8a8ad5 | 5516 | |
f6bcfd97 BP |
5517 | int right, bottom; |
5518 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7c8a8ad5 | 5519 | |
d4175745 | 5520 | int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0; |
2f024384 | 5521 | int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0; |
7c8a8ad5 | 5522 | |
f6bcfd97 BP |
5523 | if ( right > rightCol || bottom > bottomRow ) |
5524 | { | |
5525 | int left, top; | |
5526 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7c8a8ad5 | 5527 | |
ff72f628 | 5528 | dc.SetBrush(GetDefaultCellBackgroundColour()); |
f6bcfd97 | 5529 | dc.SetPen( *wxTRANSPARENT_PEN ); |
7c8a8ad5 | 5530 | |
f6bcfd97 BP |
5531 | if ( right > rightCol ) |
5532 | { | |
a9339fe2 | 5533 | dc.DrawRectangle( rightCol, top, right - rightCol, ch ); |
f6bcfd97 BP |
5534 | } |
5535 | ||
5536 | if ( bottom > bottomRow ) | |
5537 | { | |
a9339fe2 | 5538 | dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow ); |
f6bcfd97 BP |
5539 | } |
5540 | } | |
7c8a8ad5 MB |
5541 | } |
5542 | ||
2d66e025 MB |
5543 | void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) |
5544 | { | |
ab79958a VZ |
5545 | int row = coords.GetRow(); |
5546 | int col = coords.GetCol(); | |
60ff3b99 | 5547 | |
7c1cb261 | 5548 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
ab79958a VZ |
5549 | return; |
5550 | ||
5551 | // we draw the cell border ourselves | |
189d0213 VZ |
5552 | wxGridCellAttr* attr = GetCellAttr(row, col); |
5553 | ||
5554 | bool isCurrent = coords == m_currentCellCoords; | |
508011ce | 5555 | |
f6bcfd97 | 5556 | wxRect rect = CellToRect( row, col ); |
f85afd4e | 5557 | |
189d0213 | 5558 | // if the editor is shown, we should use it and not the renderer |
f6bcfd97 BP |
5559 | // Note: However, only if it is really _shown_, i.e. not hidden! |
5560 | if ( isCurrent && IsCellEditControlShown() ) | |
189d0213 | 5561 | { |
a9339fe2 | 5562 | // NB: this "#if..." is temporary and fixes a problem where the |
962a48f6 DS |
5563 | // edit control is erased by this code after being rendered. |
5564 | // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered | |
5565 | // implicitly, causing this out-of order render. | |
5d38a5f3 | 5566 | #if !defined(__WXMAC__) |
0b190b0f | 5567 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
ccc04025 | 5568 | editor->PaintBackground(dc, rect, *attr); |
0b190b0f | 5569 | editor->DecRef(); |
962a48f6 | 5570 | #endif |
189d0213 VZ |
5571 | } |
5572 | else | |
5573 | { | |
a9339fe2 | 5574 | // but all the rest is drawn by the cell renderer and hence may be customized |
0b190b0f VZ |
5575 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); |
5576 | renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); | |
5577 | renderer->DecRef(); | |
189d0213 | 5578 | } |
07296f0b | 5579 | |
283b7808 VZ |
5580 | attr->DecRef(); |
5581 | } | |
07296f0b | 5582 | |
283b7808 | 5583 | void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ) |
07296f0b | 5584 | { |
760be3f7 VS |
5585 | // don't show highlight when the grid doesn't have focus |
5586 | if ( !HasFocus() ) | |
5587 | return; | |
5588 | ||
07296f0b RD |
5589 | int row = m_currentCellCoords.GetRow(); |
5590 | int col = m_currentCellCoords.GetCol(); | |
5591 | ||
7c1cb261 | 5592 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
07296f0b RD |
5593 | return; |
5594 | ||
f6bcfd97 | 5595 | wxRect rect = CellToRect(row, col); |
07296f0b | 5596 | |
b3a7510d VZ |
5597 | // hmmm... what could we do here to show that the cell is disabled? |
5598 | // for now, I just draw a thinner border than for the other ones, but | |
5599 | // it doesn't look really good | |
b3a7510d | 5600 | |
d2520c85 RD |
5601 | int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth; |
5602 | ||
27f35b66 SN |
5603 | if (penWidth > 0) |
5604 | { | |
56b6cf26 DS |
5605 | // The center of the drawn line is where the position/width/height of |
5606 | // the rectangle is actually at (on wxMSW at least), so the | |
5607 | // size of the rectangle is reduced to compensate for the thickness of | |
5608 | // the line. If this is too strange on non-wxMSW platforms then | |
d2520c85 | 5609 | // please #ifdef this appropriately. |
4db6714b KH |
5610 | rect.x += penWidth / 2; |
5611 | rect.y += penWidth / 2; | |
5612 | rect.width -= penWidth - 1; | |
5613 | rect.height -= penWidth - 1; | |
d2520c85 | 5614 | |
d2520c85 | 5615 | // Now draw the rectangle |
73145b0e JS |
5616 | // use the cellHighlightColour if the cell is inside a selection, this |
5617 | // will ensure the cell is always visible. | |
ff72f628 VZ |
5618 | dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground |
5619 | : m_cellHighlightColour, | |
5620 | penWidth)); | |
d2520c85 RD |
5621 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
5622 | dc.DrawRectangle(rect); | |
5623 | } | |
2d66e025 | 5624 | } |
f85afd4e | 5625 | |
3d3f3e37 VZ |
5626 | wxPen wxGrid::GetDefaultGridLinePen() |
5627 | { | |
ff72f628 | 5628 | return wxPen(GetGridLineColour()); |
3d3f3e37 VZ |
5629 | } |
5630 | ||
5631 | wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row)) | |
5632 | { | |
5633 | return GetDefaultGridLinePen(); | |
5634 | } | |
5635 | ||
5636 | wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col)) | |
5637 | { | |
5638 | return GetDefaultGridLinePen(); | |
5639 | } | |
5640 | ||
2d66e025 MB |
5641 | void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) |
5642 | { | |
2d66e025 MB |
5643 | int row = coords.GetRow(); |
5644 | int col = coords.GetCol(); | |
7c1cb261 VZ |
5645 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
5646 | return; | |
5647 | ||
60ff3b99 | 5648 | |
27f35b66 SN |
5649 | wxRect rect = CellToRect( row, col ); |
5650 | ||
2d66e025 | 5651 | // right hand border |
3d3f3e37 | 5652 | dc.SetPen( GetColGridLinePen(col) ); |
27f35b66 SN |
5653 | dc.DrawLine( rect.x + rect.width, rect.y, |
5654 | rect.x + rect.width, rect.y + rect.height + 1 ); | |
2d66e025 MB |
5655 | |
5656 | // bottom border | |
3d3f3e37 | 5657 | dc.SetPen( GetRowGridLinePen(row) ); |
2f024384 | 5658 | dc.DrawLine( rect.x, rect.y + rect.height, |
27f35b66 | 5659 | rect.x + rect.width, rect.y + rect.height); |
f85afd4e MB |
5660 | } |
5661 | ||
2f024384 | 5662 | void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells) |
b3a7510d | 5663 | { |
2a7750d9 MB |
5664 | // This if block was previously in wxGrid::OnPaint but that doesn't |
5665 | // seem to get called under wxGTK - MB | |
5666 | // | |
2f024384 | 5667 | if ( m_currentCellCoords == wxGridNoCellCoords && |
2a7750d9 MB |
5668 | m_numRows && m_numCols ) |
5669 | { | |
5670 | m_currentCellCoords.Set(0, 0); | |
5671 | } | |
5672 | ||
f6bcfd97 | 5673 | if ( IsCellEditControlShown() ) |
99306db2 VZ |
5674 | { |
5675 | // don't show highlight when the edit control is shown | |
5676 | return; | |
5677 | } | |
5678 | ||
b3a7510d VZ |
5679 | // if the active cell was repainted, repaint its highlight too because it |
5680 | // might have been damaged by the grid lines | |
d10f4bf9 | 5681 | size_t count = cells.GetCount(); |
b3a7510d VZ |
5682 | for ( size_t n = 0; n < count; n++ ) |
5683 | { | |
54181a33 VZ |
5684 | wxGridCellCoords cell = cells[n]; |
5685 | ||
5686 | // If we are using attributes, then we may have just exposed another | |
5687 | // cell in a partially-visible merged cluster of cells. If the "anchor" | |
5688 | // (upper left) cell of this merged cluster is the cell indicated by | |
5689 | // m_currentCellCoords, then we need to refresh the cell highlight even | |
5690 | // though the "anchor" itself is not part of our update segment. | |
5691 | if ( CanHaveAttributes() ) | |
5692 | { | |
5693 | int rows = 0, | |
5694 | cols = 0; | |
5695 | GetCellSize(cell.GetRow(), cell.GetCol(), &rows, &cols); | |
5696 | ||
5697 | if ( rows < 0 ) | |
5698 | cell.SetRow(cell.GetRow() + rows); | |
5699 | ||
5700 | if ( cols < 0 ) | |
5701 | cell.SetCol(cell.GetCol() + cols); | |
5702 | } | |
5703 | ||
5704 | if ( cell == m_currentCellCoords ) | |
b3a7510d VZ |
5705 | { |
5706 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
5707 | DrawCellHighlight(dc, attr); | |
5708 | attr->DecRef(); | |
5709 | ||
5710 | break; | |
5711 | } | |
5712 | } | |
5713 | } | |
2d66e025 | 5714 | |
b55d57aa VZ |
5715 | // Used by wxGrid::Render() to draw the grid lines only for the cells in the |
5716 | // specified range. | |
5717 | void | |
5718 | wxGrid::DrawRangeGridLines(wxDC& dc, | |
5719 | const wxRegion& reg, | |
5720 | const wxGridCellCoords& topLeft, | |
5721 | const wxGridCellCoords& bottomRight) | |
5722 | { | |
5723 | if ( !m_gridLinesEnabled ) | |
5724 | return; | |
5725 | ||
5726 | int top, left, width, height; | |
5727 | reg.GetBox( left, top, width, height ); | |
5728 | ||
5729 | // create a clipping region | |
5730 | wxRegion clippedcells( dc.LogicalToDeviceX( left ), | |
5731 | dc.LogicalToDeviceY( top ), | |
5732 | dc.LogicalToDeviceXRel( width ), | |
5733 | dc.LogicalToDeviceYRel( height ) ); | |
5734 | ||
5735 | // subtract multi cell span area from clipping region for lines | |
5736 | wxRect rect; | |
5737 | for ( int row = topLeft.GetRow(); row <= bottomRight.GetRow(); row++ ) | |
5738 | { | |
5739 | for ( int col = topLeft.GetCol(); col <= bottomRight.GetCol(); col++ ) | |
5740 | { | |
5741 | int cell_rows, cell_cols; | |
5742 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
5743 | if ( cell_rows > 1 || cell_cols > 1 ) // multi cell | |
5744 | { | |
5745 | rect = CellToRect( row, col ); | |
5746 | // cater for scaling | |
5747 | // device origin already set in ::Render() for x, y | |
5748 | rect.x = dc.LogicalToDeviceX( rect.x ); | |
5749 | rect.y = dc.LogicalToDeviceY( rect.y ); | |
5750 | rect.width = dc.LogicalToDeviceXRel( rect.width ); | |
5751 | rect.height = dc.LogicalToDeviceYRel( rect.height ) - 1; | |
5752 | clippedcells.Subtract( rect ); | |
5753 | } | |
5754 | else if ( cell_rows < 0 || cell_cols < 0 ) // part of multicell | |
5755 | { | |
5756 | rect = CellToRect( row + cell_rows, col + cell_cols ); | |
5757 | rect.x = dc.LogicalToDeviceX( rect.x ); | |
5758 | rect.y = dc.LogicalToDeviceY( rect.y ); | |
5759 | rect.width = dc.LogicalToDeviceXRel( rect.width ); | |
5760 | rect.height = dc.LogicalToDeviceYRel( rect.height ) - 1; | |
5761 | clippedcells.Subtract( rect ); | |
5762 | } | |
5763 | } | |
5764 | } | |
5765 | ||
5766 | dc.SetDeviceClippingRegion( clippedcells ); | |
5767 | ||
5768 | DoDrawGridLines(dc, | |
5769 | top, left, top + height, left + width, | |
5770 | topLeft.GetRow(), topLeft.GetCol(), | |
5771 | bottomRight.GetRow(), bottomRight.GetCol()); | |
5772 | ||
5773 | dc.DestroyClippingRegion(); | |
5774 | } | |
5775 | ||
2d66e025 MB |
5776 | // This is used to redraw all grid lines e.g. when the grid line colour |
5777 | // has been changed | |
5778 | // | |
33ac7e6f | 5779 | void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) ) |
f85afd4e | 5780 | { |
9f7aee01 | 5781 | if ( !m_gridLinesEnabled ) |
4db6714b | 5782 | return; |
f85afd4e | 5783 | |
2d66e025 | 5784 | int top, bottom, left, right; |
796df70a | 5785 | |
ff72f628 VZ |
5786 | int cw, ch; |
5787 | m_gridWin->GetClientSize(&cw, &ch); | |
5788 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
5789 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
f85afd4e | 5790 | |
9496deb5 | 5791 | // avoid drawing grid lines past the last row and col |
9f7aee01 VZ |
5792 | if ( m_gridLinesClipHorz ) |
5793 | { | |
5794 | if ( !m_numCols ) | |
5795 | return; | |
5796 | ||
5797 | const int lastColRight = GetColRight(GetColAt(m_numCols - 1)); | |
5798 | if ( right > lastColRight ) | |
5799 | right = lastColRight; | |
5800 | } | |
5801 | ||
5802 | if ( m_gridLinesClipVert ) | |
5803 | { | |
5804 | if ( !m_numRows ) | |
5805 | return; | |
5806 | ||
5807 | const int lastRowBottom = GetRowBottom(m_numRows - 1); | |
5808 | if ( bottom > lastRowBottom ) | |
5809 | bottom = lastRowBottom; | |
5810 | } | |
9496deb5 | 5811 | |
27f35b66 | 5812 | // no gridlines inside multicells, clip them out |
d4175745 | 5813 | int leftCol = GetColPos( internalXToCol(left) ); |
a9339fe2 | 5814 | int topRow = internalYToRow(top); |
d4175745 | 5815 | int rightCol = GetColPos( internalXToCol(right) ); |
a967f048 | 5816 | int bottomRow = internalYToRow(bottom); |
27f35b66 | 5817 | |
c03bf0c7 | 5818 | wxRegion clippedcells(0, 0, cw, ch); |
27f35b66 | 5819 | |
ff72f628 | 5820 | int cell_rows, cell_cols; |
a967f048 | 5821 | wxRect rect; |
27f35b66 | 5822 | |
ff72f628 | 5823 | for ( int j = topRow; j <= bottomRow; j++ ) |
a967f048 | 5824 | { |
ff72f628 | 5825 | for ( int colPos = leftCol; colPos <= rightCol; colPos++ ) |
27f35b66 | 5826 | { |
ff72f628 | 5827 | int i = GetColAt( colPos ); |
d4175745 | 5828 | |
a967f048 RG |
5829 | GetCellSize( j, i, &cell_rows, &cell_cols ); |
5830 | if ((cell_rows > 1) || (cell_cols > 1)) | |
27f35b66 | 5831 | { |
a967f048 RG |
5832 | rect = CellToRect(j,i); |
5833 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
5834 | clippedcells.Subtract(rect); | |
5835 | } | |
5836 | else if ((cell_rows < 0) || (cell_cols < 0)) | |
5837 | { | |
a9339fe2 | 5838 | rect = CellToRect(j + cell_rows, i + cell_cols); |
a967f048 RG |
5839 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
5840 | clippedcells.Subtract(rect); | |
27f35b66 SN |
5841 | } |
5842 | } | |
5843 | } | |
a9339fe2 | 5844 | |
c1bc8d9f | 5845 | dc.SetDeviceClippingRegion( clippedcells ); |
27f35b66 | 5846 | |
f31d3faf VZ |
5847 | DoDrawGridLines(dc, |
5848 | top, left, bottom, right, | |
5849 | topRow, leftCol, m_numRows, m_numCols); | |
f85afd4e | 5850 | |
f31d3faf VZ |
5851 | dc.DestroyClippingRegion(); |
5852 | } | |
5853 | ||
5854 | void | |
5855 | wxGrid::DoDrawGridLines(wxDC& dc, | |
5856 | int top, int left, | |
5857 | int bottom, int right, | |
5858 | int topRow, int leftCol, | |
5859 | int bottomRow, int rightCol) | |
5860 | { | |
2d66e025 | 5861 | // horizontal grid lines |
f31d3faf | 5862 | for ( int i = topRow; i < bottomRow; i++ ) |
f85afd4e | 5863 | { |
6d55126d | 5864 | int bot = GetRowBottom(i) - 1; |
7c1cb261 | 5865 | |
6d55126d | 5866 | if ( bot > bottom ) |
2d66e025 | 5867 | break; |
7c1cb261 | 5868 | |
6d55126d | 5869 | if ( bot >= top ) |
2d66e025 | 5870 | { |
3d3f3e37 | 5871 | dc.SetPen( GetRowGridLinePen(i) ); |
6d55126d | 5872 | dc.DrawLine( left, bot, right, bot ); |
2d66e025 | 5873 | } |
f85afd4e MB |
5874 | } |
5875 | ||
2d66e025 | 5876 | // vertical grid lines |
f31d3faf | 5877 | for ( int colPos = leftCol; colPos < rightCol; colPos++ ) |
f85afd4e | 5878 | { |
ff72f628 | 5879 | int i = GetColAt( colPos ); |
d4175745 | 5880 | |
2121eb69 RR |
5881 | int colRight = GetColRight(i); |
5882 | #ifdef __WXGTK__ | |
5883 | if (GetLayoutDirection() != wxLayout_RightToLeft) | |
5884 | #endif | |
5885 | colRight--; | |
5886 | ||
7c1cb261 | 5887 | if ( colRight > right ) |
2d66e025 | 5888 | break; |
7c1cb261 VZ |
5889 | |
5890 | if ( colRight >= left ) | |
2d66e025 | 5891 | { |
3d3f3e37 | 5892 | dc.SetPen( GetColGridLinePen(i) ); |
7c1cb261 | 5893 | dc.DrawLine( colRight, top, colRight, bottom ); |
2d66e025 MB |
5894 | } |
5895 | } | |
5896 | } | |
f85afd4e | 5897 | |
c2f5b920 | 5898 | void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows) |
2d66e025 | 5899 | { |
4db6714b KH |
5900 | if ( !m_numRows ) |
5901 | return; | |
60ff3b99 | 5902 | |
ff72f628 VZ |
5903 | const size_t numLabels = rows.GetCount(); |
5904 | for ( size_t i = 0; i < numLabels; i++ ) | |
2d66e025 | 5905 | { |
d10f4bf9 | 5906 | DrawRowLabel( dc, rows[i] ); |
60ff3b99 | 5907 | } |
f85afd4e MB |
5908 | } |
5909 | ||
2d66e025 | 5910 | void wxGrid::DrawRowLabel( wxDC& dc, int row ) |
f85afd4e | 5911 | { |
659af826 | 5912 | if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 ) |
7c1cb261 | 5913 | return; |
60ff3b99 | 5914 | |
670d0177 VZ |
5915 | wxGridCellAttrProvider * const |
5916 | attrProvider = m_table ? m_table->GetAttrProvider() : NULL; | |
7a0a6cc8 VZ |
5917 | |
5918 | // notice that an explicit static_cast is needed to avoid a compilation | |
5919 | // error with VC7.1 which, for some reason, tries to instantiate (abstract) | |
5920 | // wxGridRowHeaderRenderer class without it | |
ba9574c3 | 5921 | const wxGridRowHeaderRenderer& |
670d0177 | 5922 | rend = attrProvider ? attrProvider->GetRowHeaderRenderer(row) |
7a0a6cc8 VZ |
5923 | : static_cast<const wxGridRowHeaderRenderer&> |
5924 | (gs_defaultHeaderRenderers.rowRenderer); | |
5925 | ||
ba9574c3 VZ |
5926 | wxRect rect(0, GetRowTop(row), m_rowLabelWidth, GetRowHeight(row)); |
5927 | rend.DrawBorder(*this, dc, rect); | |
8f177c8e | 5928 | |
f85afd4e | 5929 | int hAlign, vAlign; |
ba9574c3 | 5930 | GetRowLabelAlignment(&hAlign, &vAlign); |
60ff3b99 | 5931 | |
ba9574c3 VZ |
5932 | rend.DrawLabel(*this, dc, GetRowLabelValue(row), |
5933 | rect, hAlign, vAlign, wxHORIZONTAL); | |
f85afd4e MB |
5934 | } |
5935 | ||
ad805b9e VZ |
5936 | void wxGrid::UseNativeColHeader(bool native) |
5937 | { | |
5938 | if ( native == m_useNativeHeader ) | |
5939 | return; | |
5940 | ||
5941 | delete m_colWindow; | |
5942 | m_useNativeHeader = native; | |
5943 | ||
5944 | CreateColumnWindow(); | |
5945 | ||
5946 | if ( m_useNativeHeader ) | |
3039ade9 | 5947 | GetGridColHeader()->SetColumnCount(m_numCols); |
ad805b9e VZ |
5948 | CalcWindowSizes(); |
5949 | } | |
5950 | ||
71cf399f RR |
5951 | void wxGrid::SetUseNativeColLabels( bool native ) |
5952 | { | |
ad805b9e VZ |
5953 | wxASSERT_MSG( !m_useNativeHeader, |
5954 | "doesn't make sense when using native header" ); | |
5955 | ||
71cf399f RR |
5956 | m_nativeColumnLabels = native; |
5957 | if (native) | |
5958 | { | |
5959 | int height = wxRendererNative::Get().GetHeaderButtonHeight( this ); | |
5960 | SetColLabelSize( height ); | |
5961 | } | |
76c66f19 | 5962 | |
ad805b9e | 5963 | GetColLabelWindow()->Refresh(); |
ff72f628 | 5964 | m_cornerLabelWin->Refresh(); |
71cf399f RR |
5965 | } |
5966 | ||
d10f4bf9 | 5967 | void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols ) |
f85afd4e | 5968 | { |
4db6714b KH |
5969 | if ( !m_numCols ) |
5970 | return; | |
60ff3b99 | 5971 | |
ff72f628 VZ |
5972 | const size_t numLabels = cols.GetCount(); |
5973 | for ( size_t i = 0; i < numLabels; i++ ) | |
f85afd4e | 5974 | { |
d10f4bf9 | 5975 | DrawColLabel( dc, cols[i] ); |
60ff3b99 | 5976 | } |
f85afd4e MB |
5977 | } |
5978 | ||
ff72f628 VZ |
5979 | void wxGrid::DrawCornerLabel(wxDC& dc) |
5980 | { | |
ba9574c3 VZ |
5981 | wxRect rect(wxSize(m_rowLabelWidth, m_colLabelHeight)); |
5982 | ||
ff72f628 VZ |
5983 | if ( m_nativeColumnLabels ) |
5984 | { | |
ff72f628 VZ |
5985 | rect.Deflate(1); |
5986 | ||
5987 | wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin, dc, rect, 0); | |
5988 | } | |
5989 | else | |
5990 | { | |
ba9574c3 VZ |
5991 | rect.width++; |
5992 | rect.height++; | |
5993 | ||
670d0177 VZ |
5994 | wxGridCellAttrProvider * const |
5995 | attrProvider = m_table ? m_table->GetAttrProvider() : NULL; | |
ba9574c3 | 5996 | const wxGridCornerHeaderRenderer& |
670d0177 | 5997 | rend = attrProvider ? attrProvider->GetCornerRenderer() |
7a0a6cc8 VZ |
5998 | : static_cast<wxGridCornerHeaderRenderer&> |
5999 | (gs_defaultHeaderRenderers.cornerRenderer); | |
ff72f628 | 6000 | |
ba9574c3 | 6001 | rend.DrawBorder(*this, dc, rect); |
ff72f628 VZ |
6002 | } |
6003 | } | |
6004 | ||
6005 | void wxGrid::DrawColLabel(wxDC& dc, int col) | |
f85afd4e | 6006 | { |
659af826 | 6007 | if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 ) |
7c1cb261 | 6008 | return; |
60ff3b99 | 6009 | |
4d1bc39c | 6010 | int colLeft = GetColLeft(col); |
ec157c8f | 6011 | |
ff72f628 | 6012 | wxRect rect(colLeft, 0, GetColWidth(col), m_colLabelHeight); |
670d0177 VZ |
6013 | wxGridCellAttrProvider * const |
6014 | attrProvider = m_table ? m_table->GetAttrProvider() : NULL; | |
ba9574c3 | 6015 | const wxGridColumnHeaderRenderer& |
670d0177 | 6016 | rend = attrProvider ? attrProvider->GetColumnHeaderRenderer(col) |
7a0a6cc8 VZ |
6017 | : static_cast<wxGridColumnHeaderRenderer&> |
6018 | (gs_defaultHeaderRenderers.colRenderer); | |
2f024384 | 6019 | |
ff72f628 | 6020 | if ( m_nativeColumnLabels ) |
71cf399f | 6021 | { |
11393d29 VZ |
6022 | wxRendererNative::Get().DrawHeaderButton |
6023 | ( | |
6024 | GetColLabelWindow(), | |
6025 | dc, | |
6026 | rect, | |
6027 | 0, | |
6028 | IsSortingBy(col) | |
6029 | ? IsSortOrderAscending() | |
6030 | ? wxHDR_SORT_ICON_UP | |
6031 | : wxHDR_SORT_ICON_DOWN | |
6032 | : wxHDR_SORT_ICON_NONE | |
6033 | ); | |
ba9574c3 | 6034 | rect.Deflate(2); |
71cf399f RR |
6035 | } |
6036 | else | |
6037 | { | |
b5e97a17 VZ |
6038 | // It is reported that we need to erase the background to avoid display |
6039 | // artefacts, see #12055. | |
6040 | wxDCBrushChanger setBrush(dc, m_colWindow->GetBackgroundColour()); | |
6041 | dc.DrawRectangle(rect); | |
6042 | ||
ba9574c3 | 6043 | rend.DrawBorder(*this, dc, rect); |
71cf399f | 6044 | } |
2f024384 | 6045 | |
ff72f628 | 6046 | int hAlign, vAlign; |
ba9574c3 | 6047 | GetColLabelAlignment(&hAlign, &vAlign); |
ff72f628 | 6048 | const int orient = GetColLabelTextOrientation(); |
60ff3b99 | 6049 | |
ba9574c3 | 6050 | rend.DrawLabel(*this, dc, GetColLabelValue(col), rect, hAlign, vAlign, orient); |
f85afd4e MB |
6051 | } |
6052 | ||
ff72f628 VZ |
6053 | // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which |
6054 | // we just have to add textOrientation support | |
2d66e025 MB |
6055 | void wxGrid::DrawTextRectangle( wxDC& dc, |
6056 | const wxString& value, | |
6057 | const wxRect& rect, | |
6058 | int horizAlign, | |
d43851f7 | 6059 | int vertAlign, |
ba9574c3 | 6060 | int textOrientation ) const |
f85afd4e | 6061 | { |
2d66e025 | 6062 | wxArrayString lines; |
ef5df12b | 6063 | |
2d66e025 | 6064 | StringToLines( value, lines ); |
ef5df12b | 6065 | |
ff72f628 | 6066 | DrawTextRectangle(dc, lines, rect, horizAlign, vertAlign, textOrientation); |
d10f4bf9 VZ |
6067 | } |
6068 | ||
4330c974 | 6069 | void wxGrid::DrawTextRectangle(wxDC& dc, |
038a5591 JS |
6070 | const wxArrayString& lines, |
6071 | const wxRect& rect, | |
6072 | int horizAlign, | |
6073 | int vertAlign, | |
ba9574c3 | 6074 | int textOrientation) const |
d10f4bf9 | 6075 | { |
4330c974 VZ |
6076 | if ( lines.empty() ) |
6077 | return; | |
ef5df12b | 6078 | |
4330c974 | 6079 | wxDCClipper clip(dc, rect); |
ef5df12b | 6080 | |
4330c974 VZ |
6081 | long textWidth, |
6082 | textHeight; | |
ef5df12b | 6083 | |
4330c974 VZ |
6084 | if ( textOrientation == wxHORIZONTAL ) |
6085 | GetTextBoxSize( dc, lines, &textWidth, &textHeight ); | |
6086 | else | |
6087 | GetTextBoxSize( dc, lines, &textHeight, &textWidth ); | |
ef5df12b | 6088 | |
4330c974 VZ |
6089 | int x = 0, |
6090 | y = 0; | |
6091 | switch ( vertAlign ) | |
6092 | { | |
73145b0e | 6093 | case wxALIGN_BOTTOM: |
4db6714b | 6094 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 | 6095 | y = rect.y + (rect.height - textHeight - 1); |
d43851f7 | 6096 | else |
038a5591 JS |
6097 | x = rect.x + rect.width - textWidth; |
6098 | break; | |
ef5df12b | 6099 | |
73145b0e | 6100 | case wxALIGN_CENTRE: |
4db6714b | 6101 | if ( textOrientation == wxHORIZONTAL ) |
a9339fe2 | 6102 | y = rect.y + ((rect.height - textHeight) / 2); |
d43851f7 | 6103 | else |
a9339fe2 | 6104 | x = rect.x + ((rect.width - textWidth) / 2); |
038a5591 | 6105 | break; |
ef5df12b | 6106 | |
73145b0e JS |
6107 | case wxALIGN_TOP: |
6108 | default: | |
4db6714b | 6109 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 | 6110 | y = rect.y + 1; |
d43851f7 | 6111 | else |
038a5591 | 6112 | x = rect.x + 1; |
73145b0e | 6113 | break; |
4330c974 | 6114 | } |
ef5df12b | 6115 | |
4330c974 VZ |
6116 | // Align each line of a multi-line label |
6117 | size_t nLines = lines.GetCount(); | |
6118 | for ( size_t l = 0; l < nLines; l++ ) | |
6119 | { | |
6120 | const wxString& line = lines[l]; | |
ef5df12b | 6121 | |
9b7d3c09 VZ |
6122 | if ( line.empty() ) |
6123 | { | |
6124 | *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight(); | |
6125 | continue; | |
6126 | } | |
6127 | ||
ad2633bd | 6128 | wxCoord lineWidth = 0, |
c2b2c10e | 6129 | lineHeight = 0; |
4330c974 VZ |
6130 | dc.GetTextExtent(line, &lineWidth, &lineHeight); |
6131 | ||
6132 | switch ( horizAlign ) | |
6133 | { | |
038a5591 | 6134 | case wxALIGN_RIGHT: |
4db6714b | 6135 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 JS |
6136 | x = rect.x + (rect.width - lineWidth - 1); |
6137 | else | |
6138 | y = rect.y + lineWidth + 1; | |
6139 | break; | |
ef5df12b | 6140 | |
038a5591 | 6141 | case wxALIGN_CENTRE: |
4db6714b | 6142 | if ( textOrientation == wxHORIZONTAL ) |
2f024384 | 6143 | x = rect.x + ((rect.width - lineWidth) / 2); |
038a5591 | 6144 | else |
2f024384 | 6145 | y = rect.y + rect.height - ((rect.height - lineWidth) / 2); |
038a5591 | 6146 | break; |
ef5df12b | 6147 | |
038a5591 JS |
6148 | case wxALIGN_LEFT: |
6149 | default: | |
4db6714b | 6150 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 JS |
6151 | x = rect.x + 1; |
6152 | else | |
6153 | y = rect.y + rect.height - 1; | |
6154 | break; | |
4330c974 | 6155 | } |
ef5df12b | 6156 | |
4330c974 VZ |
6157 | if ( textOrientation == wxHORIZONTAL ) |
6158 | { | |
6159 | dc.DrawText( line, x, y ); | |
6160 | y += lineHeight; | |
6161 | } | |
6162 | else | |
6163 | { | |
6164 | dc.DrawRotatedText( line, x, y, 90.0 ); | |
6165 | x += lineHeight; | |
038a5591 | 6166 | } |
f85afd4e | 6167 | } |
f85afd4e MB |
6168 | } |
6169 | ||
2f024384 DS |
6170 | // Split multi-line text up into an array of strings. |
6171 | // Any existing contents of the string array are preserved. | |
f85afd4e | 6172 | // |
696f3e9d | 6173 | // TODO: refactor wxTextFile::Read() and reuse the same code from here |
ef316e23 | 6174 | void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) const |
f85afd4e | 6175 | { |
f85afd4e MB |
6176 | int startPos = 0; |
6177 | int pos; | |
6d004f67 | 6178 | wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix ); |
2433bb2e | 6179 | wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix ); |
e2b42eeb | 6180 | |
faa94f3e | 6181 | while ( startPos < (int)tVal.length() ) |
f85afd4e | 6182 | { |
2433bb2e | 6183 | pos = tVal.Mid(startPos).Find( eol ); |
f85afd4e MB |
6184 | if ( pos < 0 ) |
6185 | { | |
6186 | break; | |
6187 | } | |
6188 | else if ( pos == 0 ) | |
6189 | { | |
6190 | lines.Add( wxEmptyString ); | |
6191 | } | |
6192 | else | |
6193 | { | |
696f3e9d | 6194 | lines.Add( tVal.Mid(startPos, pos) ); |
f85afd4e | 6195 | } |
a9339fe2 | 6196 | |
4db6714b | 6197 | startPos += pos + 1; |
f85afd4e | 6198 | } |
4db6714b | 6199 | |
2eafd712 | 6200 | if ( startPos < (int)tVal.length() ) |
f85afd4e | 6201 | { |
696f3e9d | 6202 | lines.Add( tVal.Mid( startPos ) ); |
f85afd4e MB |
6203 | } |
6204 | } | |
6205 | ||
fbfb8bcc | 6206 | void wxGrid::GetTextBoxSize( const wxDC& dc, |
d10f4bf9 | 6207 | const wxArrayString& lines, |
ef316e23 | 6208 | long *width, long *height ) const |
f85afd4e | 6209 | { |
ad2633bd RR |
6210 | wxCoord w = 0; |
6211 | wxCoord h = 0; | |
6212 | wxCoord lineW = 0, lineH = 0; | |
f85afd4e | 6213 | |
b540eb2b | 6214 | size_t i; |
56b6cf26 | 6215 | for ( i = 0; i < lines.GetCount(); i++ ) |
f85afd4e MB |
6216 | { |
6217 | dc.GetTextExtent( lines[i], &lineW, &lineH ); | |
6218 | w = wxMax( w, lineW ); | |
6219 | h += lineH; | |
6220 | } | |
6221 | ||
6222 | *width = w; | |
6223 | *height = h; | |
6224 | } | |
6225 | ||
f6bcfd97 BP |
6226 | // |
6227 | // ------ Batch processing. | |
6228 | // | |
6229 | void wxGrid::EndBatch() | |
6230 | { | |
6231 | if ( m_batchCount > 0 ) | |
6232 | { | |
6233 | m_batchCount--; | |
6234 | if ( !m_batchCount ) | |
6235 | { | |
6236 | CalcDimensions(); | |
6237 | m_rowLabelWin->Refresh(); | |
ad805b9e | 6238 | m_colWindow->Refresh(); |
f6bcfd97 BP |
6239 | m_cornerLabelWin->Refresh(); |
6240 | m_gridWin->Refresh(); | |
6241 | } | |
6242 | } | |
6243 | } | |
f85afd4e | 6244 | |
d8232393 MB |
6245 | // Use this, rather than wxWindow::Refresh(), to force an immediate |
6246 | // repainting of the grid. Has no effect if you are already inside a | |
6247 | // BeginBatch / EndBatch block. | |
6248 | // | |
6249 | void wxGrid::ForceRefresh() | |
6250 | { | |
6251 | BeginBatch(); | |
6252 | EndBatch(); | |
6253 | } | |
6254 | ||
bf646121 VZ |
6255 | bool wxGrid::Enable(bool enable) |
6256 | { | |
6257 | if ( !wxScrolledWindow::Enable(enable) ) | |
6258 | return false; | |
6259 | ||
6260 | // redraw in the new state | |
6261 | m_gridWin->Refresh(); | |
6262 | ||
6263 | return true; | |
6264 | } | |
d8232393 | 6265 | |
f85afd4e | 6266 | // |
2d66e025 | 6267 | // ------ Edit control functions |
f85afd4e MB |
6268 | // |
6269 | ||
2d66e025 | 6270 | void wxGrid::EnableEditing( bool edit ) |
f85afd4e | 6271 | { |
2d66e025 | 6272 | if ( edit != m_editable ) |
f85afd4e | 6273 | { |
4db6714b KH |
6274 | if (!edit) |
6275 | EnableCellEditControl(edit); | |
2d66e025 | 6276 | m_editable = edit; |
f85afd4e | 6277 | } |
f85afd4e MB |
6278 | } |
6279 | ||
2d66e025 | 6280 | void wxGrid::EnableCellEditControl( bool enable ) |
f85afd4e | 6281 | { |
2c9a89e0 RD |
6282 | if (! m_editable) |
6283 | return; | |
6284 | ||
2c9a89e0 RD |
6285 | if ( enable != m_cellEditCtrlEnabled ) |
6286 | { | |
dcfe4c3d | 6287 | if ( enable ) |
f85afd4e | 6288 | { |
8a3e536c | 6289 | if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN) == -1 ) |
97a9929e | 6290 | return; |
fe7b9ed6 | 6291 | |
97a9929e | 6292 | // this should be checked by the caller! |
9a83f860 | 6293 | wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") ); |
b54ba671 VZ |
6294 | |
6295 | // do it before ShowCellEditControl() | |
dcfe4c3d | 6296 | m_cellEditCtrlEnabled = enable; |
b54ba671 | 6297 | |
2d66e025 | 6298 | ShowCellEditControl(); |
2d66e025 MB |
6299 | } |
6300 | else | |
6301 | { | |
8a3e536c | 6302 | SendEvent(wxEVT_GRID_EDITOR_HIDDEN); |
fe7b9ed6 | 6303 | |
97a9929e | 6304 | HideCellEditControl(); |
2d66e025 | 6305 | SaveEditControlValue(); |
b54ba671 VZ |
6306 | |
6307 | // do it after HideCellEditControl() | |
dcfe4c3d | 6308 | m_cellEditCtrlEnabled = enable; |
f85afd4e | 6309 | } |
f85afd4e | 6310 | } |
f85afd4e | 6311 | } |
f85afd4e | 6312 | |
b54ba671 | 6313 | bool wxGrid::IsCurrentCellReadOnly() const |
283b7808 | 6314 | { |
f48a1159 VZ |
6315 | wxGridCellAttr* |
6316 | attr = const_cast<wxGrid *>(this)->GetCellAttr(m_currentCellCoords); | |
b54ba671 VZ |
6317 | bool readonly = attr->IsReadOnly(); |
6318 | attr->DecRef(); | |
283b7808 | 6319 | |
b54ba671 VZ |
6320 | return readonly; |
6321 | } | |
283b7808 | 6322 | |
b54ba671 VZ |
6323 | bool wxGrid::CanEnableCellControl() const |
6324 | { | |
20c84410 SN |
6325 | return m_editable && (m_currentCellCoords != wxGridNoCellCoords) && |
6326 | !IsCurrentCellReadOnly(); | |
b54ba671 VZ |
6327 | } |
6328 | ||
6329 | bool wxGrid::IsCellEditControlEnabled() const | |
6330 | { | |
6331 | // the cell edit control might be disable for all cells or just for the | |
6332 | // current one if it's read only | |
ca65c044 | 6333 | return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false; |
283b7808 VZ |
6334 | } |
6335 | ||
f6bcfd97 BP |
6336 | bool wxGrid::IsCellEditControlShown() const |
6337 | { | |
ca65c044 | 6338 | bool isShown = false; |
f6bcfd97 BP |
6339 | |
6340 | if ( m_cellEditCtrlEnabled ) | |
6341 | { | |
6342 | int row = m_currentCellCoords.GetRow(); | |
6343 | int col = m_currentCellCoords.GetCol(); | |
6344 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
6345 | wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col); | |
6346 | attr->DecRef(); | |
6347 | ||
6348 | if ( editor ) | |
6349 | { | |
6350 | if ( editor->IsCreated() ) | |
6351 | { | |
6352 | isShown = editor->GetControl()->IsShown(); | |
6353 | } | |
6354 | ||
6355 | editor->DecRef(); | |
6356 | } | |
6357 | } | |
6358 | ||
6359 | return isShown; | |
6360 | } | |
6361 | ||
2d66e025 | 6362 | void wxGrid::ShowCellEditControl() |
f85afd4e | 6363 | { |
2d66e025 | 6364 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 6365 | { |
7db713ae | 6366 | if ( !IsVisible( m_currentCellCoords, false ) ) |
2d66e025 | 6367 | { |
ca65c044 | 6368 | m_cellEditCtrlEnabled = false; |
2d66e025 MB |
6369 | return; |
6370 | } | |
6371 | else | |
6372 | { | |
2c9a89e0 RD |
6373 | wxRect rect = CellToRect( m_currentCellCoords ); |
6374 | int row = m_currentCellCoords.GetRow(); | |
6375 | int col = m_currentCellCoords.GetCol(); | |
2d66e025 | 6376 | |
27f35b66 SN |
6377 | // if this is part of a multicell, find owner (topleft) |
6378 | int cell_rows, cell_cols; | |
6379 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
6380 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
6381 | { | |
6382 | row += cell_rows; | |
6383 | col += cell_cols; | |
6384 | m_currentCellCoords.SetRow( row ); | |
6385 | m_currentCellCoords.SetCol( col ); | |
6386 | } | |
6387 | ||
99306db2 VZ |
6388 | // erase the highlight and the cell contents because the editor |
6389 | // might not cover the entire cell | |
6390 | wxClientDC dc( m_gridWin ); | |
6391 | PrepareDC( dc ); | |
2c03d771 | 6392 | wxGridCellAttr* attr = GetCellAttr(row, col); |
ff72f628 | 6393 | dc.SetBrush(wxBrush(attr->GetBackgroundColour())); |
99306db2 VZ |
6394 | dc.SetPen(*wxTRANSPARENT_PEN); |
6395 | dc.DrawRectangle(rect); | |
d2fdd8d2 | 6396 | |
6f706ee0 VZ |
6397 | // convert to scrolled coords |
6398 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
6399 | ||
6400 | int nXMove = 0; | |
6401 | if (rect.x < 0) | |
6402 | nXMove = rect.x; | |
6403 | ||
99306db2 | 6404 | // cell is shifted by one pixel |
68c5a31c | 6405 | // However, don't allow x or y to become negative |
70e8d961 | 6406 | // since the SetSize() method interprets that as |
68c5a31c SN |
6407 | // "don't change." |
6408 | if (rect.x > 0) | |
6409 | rect.x--; | |
6410 | if (rect.y > 0) | |
6411 | rect.y--; | |
f0102d2a | 6412 | |
28a77bc4 | 6413 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
3da93aae VZ |
6414 | if ( !editor->IsCreated() ) |
6415 | { | |
ca65c044 | 6416 | editor->Create(m_gridWin, wxID_ANY, |
2c9a89e0 | 6417 | new wxGridCellEditorEvtHandler(this, editor)); |
bf7945ce RD |
6418 | |
6419 | wxGridEditorCreatedEvent evt(GetId(), | |
6420 | wxEVT_GRID_EDITOR_CREATED, | |
6421 | this, | |
6422 | row, | |
6423 | col, | |
6424 | editor->GetControl()); | |
6425 | GetEventHandler()->ProcessEvent(evt); | |
2d66e025 MB |
6426 | } |
6427 | ||
27f35b66 | 6428 | // resize editor to overflow into righthand cells if allowed |
39b80349 | 6429 | int maxWidth = rect.width; |
27f35b66 SN |
6430 | wxString value = GetCellValue(row, col); |
6431 | if ( (value != wxEmptyString) && (attr->GetOverflow()) ) | |
6432 | { | |
39b80349 | 6433 | int y; |
2f024384 DS |
6434 | GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont()); |
6435 | if (maxWidth < rect.width) | |
6436 | maxWidth = rect.width; | |
39b80349 | 6437 | } |
4db6714b | 6438 | |
39b80349 | 6439 | int client_right = m_gridWin->GetClientSize().GetWidth(); |
2f024384 | 6440 | if (rect.x + maxWidth > client_right) |
2b5f62a0 | 6441 | maxWidth = client_right - rect.x; |
39b80349 | 6442 | |
2b5f62a0 VZ |
6443 | if ((maxWidth > rect.width) && (col < m_numCols) && m_table) |
6444 | { | |
39b80349 | 6445 | GetCellSize( row, col, &cell_rows, &cell_cols ); |
2b5f62a0 | 6446 | // may have changed earlier |
2f024384 | 6447 | for (int i = col + cell_cols; i < m_numCols; i++) |
2b5f62a0 | 6448 | { |
39b80349 SN |
6449 | int c_rows, c_cols; |
6450 | GetCellSize( row, i, &c_rows, &c_cols ); | |
2f024384 | 6451 | |
2b5f62a0 | 6452 | // looks weird going over a multicell |
bee19958 | 6453 | if (m_table->IsEmptyCell( row, i ) && |
2b5f62a0 | 6454 | (rect.width < maxWidth) && (c_rows == 1)) |
2f024384 | 6455 | { |
bee19958 | 6456 | rect.width += GetColWidth( i ); |
2f024384 | 6457 | } |
2b5f62a0 VZ |
6458 | else |
6459 | break; | |
6460 | } | |
4db6714b | 6461 | |
39b80349 | 6462 | if (rect.GetRight() > client_right) |
bee19958 | 6463 | rect.SetRight( client_right - 1 ); |
27f35b66 | 6464 | } |
76c66f19 | 6465 | |
bee19958 | 6466 | editor->SetCellAttr( attr ); |
2c9a89e0 | 6467 | editor->SetSize( rect ); |
e1a66d9a RD |
6468 | if (nXMove != 0) |
6469 | editor->GetControl()->Move( | |
6470 | editor->GetControl()->GetPosition().x + nXMove, | |
6471 | editor->GetControl()->GetPosition().y ); | |
ca65c044 | 6472 | editor->Show( true, attr ); |
04418332 VZ |
6473 | |
6474 | // recalc dimensions in case we need to | |
6475 | // expand the scrolled window to account for editor | |
73145b0e | 6476 | CalcDimensions(); |
99306db2 | 6477 | |
3da93aae | 6478 | editor->BeginEdit(row, col, this); |
1bd71df9 | 6479 | editor->SetCellAttr(NULL); |
0b190b0f VZ |
6480 | |
6481 | editor->DecRef(); | |
2c9a89e0 | 6482 | attr->DecRef(); |
2b5f62a0 | 6483 | } |
f85afd4e MB |
6484 | } |
6485 | } | |
6486 | ||
2d66e025 | 6487 | void wxGrid::HideCellEditControl() |
f85afd4e | 6488 | { |
2d66e025 | 6489 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 6490 | { |
2c9a89e0 RD |
6491 | int row = m_currentCellCoords.GetRow(); |
6492 | int col = m_currentCellCoords.GetCol(); | |
6493 | ||
bee19958 | 6494 | wxGridCellAttr *attr = GetCellAttr(row, col); |
0b190b0f | 6495 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
7d277ac0 | 6496 | const bool editorHadFocus = editor->GetControl()->HasFocus(); |
ca65c044 | 6497 | editor->Show( false ); |
0b190b0f | 6498 | editor->DecRef(); |
2c9a89e0 | 6499 | attr->DecRef(); |
2fb3e528 | 6500 | |
7d277ac0 VZ |
6501 | // return the focus to the grid itself if the editor had it |
6502 | // | |
6503 | // note that we must not do this unconditionally to avoid stealing | |
6504 | // focus from the window which just received it if we are hiding the | |
6505 | // editor precisely because we lost focus | |
6506 | if ( editorHadFocus ) | |
6507 | m_gridWin->SetFocus(); | |
2fb3e528 | 6508 | |
27f35b66 SN |
6509 | // refresh whole row to the right |
6510 | wxRect rect( CellToRect(row, col) ); | |
6511 | CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y ); | |
6512 | rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x; | |
2f024384 | 6513 | |
7d75e6c6 RD |
6514 | #ifdef __WXMAC__ |
6515 | // ensure that the pixels under the focus ring get refreshed as well | |
4db6714b | 6516 | rect.Inflate(10, 10); |
7d75e6c6 | 6517 | #endif |
2f024384 | 6518 | |
ca65c044 | 6519 | m_gridWin->Refresh( false, &rect ); |
f85afd4e | 6520 | } |
2d66e025 | 6521 | } |
8f177c8e | 6522 | |
2d66e025 MB |
6523 | void wxGrid::SaveEditControlValue() |
6524 | { | |
3da93aae VZ |
6525 | if ( IsCellEditControlEnabled() ) |
6526 | { | |
2c9a89e0 RD |
6527 | int row = m_currentCellCoords.GetRow(); |
6528 | int col = m_currentCellCoords.GetCol(); | |
8f177c8e | 6529 | |
4db6714b | 6530 | wxString oldval = GetCellValue(row, col); |
fe7b9ed6 | 6531 | |
3da93aae | 6532 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 6533 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
2d66e025 | 6534 | |
763163a8 | 6535 | wxString newval; |
78e78812 | 6536 | bool changed = editor->EndEdit(row, col, this, oldval, &newval); |
2d66e025 | 6537 | |
763163a8 | 6538 | if ( changed && SendEvent(wxEVT_GRID_CELL_CHANGING, newval) != -1 ) |
3da93aae | 6539 | { |
763163a8 VZ |
6540 | editor->ApplyEdit(row, col, this); |
6541 | ||
6542 | // for compatibility reasons dating back to wx 2.8 when this event | |
6543 | // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING | |
6544 | // didn't exist we allow vetoing this one too | |
6545 | if ( SendEvent(wxEVT_GRID_CELL_CHANGED, oldval) == -1 ) | |
4db6714b | 6546 | { |
97a9929e | 6547 | // Event has been vetoed, set the data back. |
4db6714b | 6548 | SetCellValue(row, col, oldval); |
97a9929e | 6549 | } |
2d66e025 | 6550 | } |
763163a8 VZ |
6551 | |
6552 | editor->DecRef(); | |
6553 | attr->DecRef(); | |
f85afd4e MB |
6554 | } |
6555 | } | |
6556 | ||
add6e919 VZ |
6557 | void wxGrid::OnHideEditor(wxCommandEvent& WXUNUSED(event)) |
6558 | { | |
6559 | DisableCellEditControl(); | |
6560 | } | |
6561 | ||
2d66e025 | 6562 | // |
60ff3b99 VZ |
6563 | // ------ Grid location functions |
6564 | // Note that all of these functions work with the logical coordinates of | |
2d66e025 MB |
6565 | // grid cells and labels so you will need to convert from device |
6566 | // coordinates for mouse events etc. | |
6567 | // | |
6568 | ||
8a3e536c | 6569 | wxGridCellCoords wxGrid::XYToCell(int x, int y) const |
f85afd4e | 6570 | { |
58dd5b3b MB |
6571 | int row = YToRow(y); |
6572 | int col = XToCol(x); | |
6573 | ||
8a3e536c VZ |
6574 | return row == -1 || col == -1 ? wxGridNoCellCoords |
6575 | : wxGridCellCoords(row, col); | |
2d66e025 | 6576 | } |
f85afd4e | 6577 | |
bec70262 VZ |
6578 | // compute row or column from some (unscrolled) coordinate value, using either |
6579 | // m_defaultRowHeight/m_defaultColWidth or binary search on array of | |
5bce3d57 VZ |
6580 | // m_rowBottoms/m_colRights to do it quickly in O(log n) time. |
6581 | // NOTE: This may not work correctly for reordered columns. | |
cd4f6f5f VZ |
6582 | int wxGrid::PosToLinePos(int coord, |
6583 | bool clipToMinMax, | |
6584 | const wxGridOperations& oper) const | |
2d66e025 | 6585 | { |
bec70262 | 6586 | const int numLines = oper.GetNumberOfLines(this); |
a967f048 | 6587 | |
bec70262 | 6588 | if ( coord < 0 ) |
cd4f6f5f | 6589 | return clipToMinMax && numLines > 0 ? 0 : wxNOT_FOUND; |
a967f048 | 6590 | |
bec70262 VZ |
6591 | const int defaultLineSize = oper.GetDefaultLineSize(this); |
6592 | wxCHECK_MSG( defaultLineSize, -1, "can't have 0 default line size" ); | |
d57ad377 | 6593 | |
bec70262 VZ |
6594 | int maxPos = coord / defaultLineSize, |
6595 | minPos = 0; | |
8f177c8e | 6596 | |
bec70262 VZ |
6597 | // check for the simplest case: if we have no explicit line sizes |
6598 | // configured, then we already know the line this position falls in | |
6599 | const wxArrayInt& lineEnds = oper.GetLineEnds(this); | |
6600 | if ( lineEnds.empty() ) | |
f85afd4e | 6601 | { |
bec70262 VZ |
6602 | if ( maxPos < numLines ) |
6603 | return maxPos; | |
4db6714b | 6604 | |
bec70262 | 6605 | return clipToMinMax ? numLines - 1 : -1; |
f85afd4e | 6606 | } |
4db6714b | 6607 | |
2d66e025 | 6608 | |
5bce3d57 VZ |
6609 | // binary search is quite efficient and we can't really make any assumptions |
6610 | // on where to start here since row and columns could be of size 0 if they are | |
6611 | // hidden. While this could be made more efficient, some profiling will be | |
6612 | // necessary to determine if it really is a performance bottleneck | |
6613 | maxPos = numLines - 1; | |
d4175745 | 6614 | |
bec70262 VZ |
6615 | // check if the position is beyond the last column |
6616 | const int lineAtMaxPos = oper.GetLineAt(this, maxPos); | |
6617 | if ( coord >= lineEnds[lineAtMaxPos] ) | |
cd4f6f5f | 6618 | return clipToMinMax ? maxPos : -1; |
d4175745 | 6619 | |
bec70262 VZ |
6620 | // or before the first one |
6621 | const int lineAt0 = oper.GetLineAt(this, 0); | |
6622 | if ( coord < lineEnds[lineAt0] ) | |
cd4f6f5f | 6623 | return 0; |
d4175745 | 6624 | |
bec70262 VZ |
6625 | |
6626 | // finally do perform the binary search | |
6627 | while ( minPos < maxPos ) | |
d4175745 | 6628 | { |
bec70262 VZ |
6629 | wxCHECK_MSG( lineEnds[oper.GetLineAt(this, minPos)] <= coord && |
6630 | coord < lineEnds[oper.GetLineAt(this, maxPos)], | |
6631 | -1, | |
cd4f6f5f | 6632 | "wxGrid: internal error in PosToLinePos()" ); |
d4175745 | 6633 | |
bec70262 | 6634 | if ( coord >= lineEnds[oper.GetLineAt(this, maxPos - 1)] ) |
cd4f6f5f | 6635 | return maxPos; |
d4175745 VZ |
6636 | else |
6637 | maxPos--; | |
bec70262 VZ |
6638 | |
6639 | const int median = minPos + (maxPos - minPos + 1) / 2; | |
6640 | if ( coord < lineEnds[oper.GetLineAt(this, median)] ) | |
d4175745 VZ |
6641 | maxPos = median; |
6642 | else | |
6643 | minPos = median; | |
6644 | } | |
bec70262 | 6645 | |
cd4f6f5f VZ |
6646 | return maxPos; |
6647 | } | |
6648 | ||
6649 | int | |
6650 | wxGrid::PosToLine(int coord, | |
6651 | bool clipToMinMax, | |
6652 | const wxGridOperations& oper) const | |
6653 | { | |
6654 | int pos = PosToLinePos(coord, clipToMinMax, oper); | |
6655 | ||
6656 | return pos == wxNOT_FOUND ? wxNOT_FOUND : oper.GetLineAt(this, pos); | |
bec70262 VZ |
6657 | } |
6658 | ||
6659 | int wxGrid::YToRow(int y, bool clipToMinMax) const | |
6660 | { | |
6661 | return PosToLine(y, clipToMinMax, wxGridRowOperations()); | |
6662 | } | |
6663 | ||
6664 | int wxGrid::XToCol(int x, bool clipToMinMax) const | |
6665 | { | |
6666 | return PosToLine(x, clipToMinMax, wxGridColumnOperations()); | |
2d66e025 | 6667 | } |
8f177c8e | 6668 | |
cd4f6f5f VZ |
6669 | int wxGrid::XToPos(int x) const |
6670 | { | |
6671 | return PosToLinePos(x, true /* clip */, wxGridColumnOperations()); | |
6672 | } | |
6673 | ||
71b619d7 | 6674 | // return the row/col number such that the pos is near the edge of, or -1 if |
10a4531d VZ |
6675 | // not near an edge. |
6676 | // | |
82edfbe7 VZ |
6677 | // notice that position can only possibly be near an edge if the row/column is |
6678 | // large enough to still allow for an "inner" area that is _not_ near the edge | |
6679 | // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will | |
6680 | // _never_ be considered to be near the edge). | |
10a4531d | 6681 | int wxGrid::PosToEdgeOfLine(int pos, const wxGridOperations& oper) const |
2d66e025 | 6682 | { |
71b619d7 VZ |
6683 | // Get the bottom or rightmost line that could match. |
6684 | int line = oper.PosToLine(this, pos, true); | |
10a4531d | 6685 | |
6df84c5c VZ |
6686 | if ( oper.GetLineSize(this, line) > WXGRID_LABEL_EDGE_ZONE ) |
6687 | { | |
6688 | // We know that we are in this line, test whether we are close enough | |
6689 | // to start or end border, respectively. | |
6690 | if ( abs(oper.GetLineEndPos(this, line) - pos) < WXGRID_LABEL_EDGE_ZONE ) | |
6691 | return line; | |
6692 | else if ( line > 0 && | |
6693 | pos - oper.GetLineStartPos(this, | |
6694 | line) < WXGRID_LABEL_EDGE_ZONE ) | |
6695 | { | |
6696 | // We need to find the previous visible line, so skip all the | |
6697 | // hidden (of size 0) ones. | |
6698 | do | |
6699 | { | |
6700 | line = oper.GetLineBefore(this, line); | |
6701 | } | |
6702 | while ( line >= 0 && oper.GetLineSize(this, line) == 0 ); | |
2d66e025 | 6703 | |
6df84c5c VZ |
6704 | // It can possibly be -1 here. |
6705 | return line; | |
6706 | } | |
6707 | } | |
71b619d7 | 6708 | |
2d66e025 MB |
6709 | return -1; |
6710 | } | |
6711 | ||
10a4531d | 6712 | int wxGrid::YToEdgeOfRow(int y) const |
2d66e025 | 6713 | { |
10a4531d VZ |
6714 | return PosToEdgeOfLine(y, wxGridRowOperations()); |
6715 | } | |
2d66e025 | 6716 | |
10a4531d VZ |
6717 | int wxGrid::XToEdgeOfCol(int x) const |
6718 | { | |
6719 | return PosToEdgeOfLine(x, wxGridColumnOperations()); | |
f85afd4e MB |
6720 | } |
6721 | ||
ef316e23 | 6722 | wxRect wxGrid::CellToRect( int row, int col ) const |
f85afd4e | 6723 | { |
2d66e025 | 6724 | wxRect rect( -1, -1, -1, -1 ); |
f85afd4e | 6725 | |
2f024384 DS |
6726 | if ( row >= 0 && row < m_numRows && |
6727 | col >= 0 && col < m_numCols ) | |
f85afd4e | 6728 | { |
27f35b66 SN |
6729 | int i, cell_rows, cell_cols; |
6730 | rect.width = rect.height = 0; | |
6731 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
6732 | // if negative then find multicell owner | |
2f024384 DS |
6733 | if (cell_rows < 0) |
6734 | row += cell_rows; | |
6735 | if (cell_cols < 0) | |
6736 | col += cell_cols; | |
27f35b66 SN |
6737 | GetCellSize( row, col, &cell_rows, &cell_cols ); |
6738 | ||
7c1cb261 VZ |
6739 | rect.x = GetColLeft(col); |
6740 | rect.y = GetRowTop(row); | |
2f024384 DS |
6741 | for (i=col; i < col + cell_cols; i++) |
6742 | rect.width += GetColWidth(i); | |
6743 | for (i=row; i < row + cell_rows; i++) | |
27f35b66 | 6744 | rect.height += GetRowHeight(i); |
f85afd4e | 6745 | |
efc49a21 VZ |
6746 | // if grid lines are enabled, then the area of the cell is a bit smaller |
6747 | if (m_gridLinesEnabled) | |
6748 | { | |
6749 | rect.width -= 1; | |
6750 | rect.height -= 1; | |
6751 | } | |
f6bcfd97 | 6752 | } |
4db6714b | 6753 | |
2d66e025 MB |
6754 | return rect; |
6755 | } | |
6756 | ||
ef316e23 | 6757 | bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) const |
2d66e025 MB |
6758 | { |
6759 | // get the cell rectangle in logical coords | |
6760 | // | |
6761 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 6762 | |
2d66e025 MB |
6763 | // convert to device coords |
6764 | // | |
6765 | int left, top, right, bottom; | |
6766 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
6767 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 6768 | |
2d66e025 | 6769 | // check against the client area of the grid window |
2d66e025 MB |
6770 | int cw, ch; |
6771 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 6772 | |
2d66e025 | 6773 | if ( wholeCellVisible ) |
f85afd4e | 6774 | { |
2d66e025 | 6775 | // is the cell wholly visible ? |
2f024384 DS |
6776 | return ( left >= 0 && right <= cw && |
6777 | top >= 0 && bottom <= ch ); | |
2d66e025 MB |
6778 | } |
6779 | else | |
6780 | { | |
6781 | // is the cell partly visible ? | |
6782 | // | |
2f024384 DS |
6783 | return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) && |
6784 | ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) ); | |
2d66e025 MB |
6785 | } |
6786 | } | |
6787 | ||
2d66e025 MB |
6788 | // make the specified cell location visible by doing a minimal amount |
6789 | // of scrolling | |
6790 | // | |
6791 | void wxGrid::MakeCellVisible( int row, int col ) | |
6792 | { | |
6793 | int i; | |
60ff3b99 | 6794 | int xpos = -1, ypos = -1; |
2d66e025 | 6795 | |
2f024384 DS |
6796 | if ( row >= 0 && row < m_numRows && |
6797 | col >= 0 && col < m_numCols ) | |
2d66e025 MB |
6798 | { |
6799 | // get the cell rectangle in logical coords | |
2d66e025 | 6800 | wxRect r( CellToRect( row, col ) ); |
60ff3b99 | 6801 | |
2d66e025 | 6802 | // convert to device coords |
2d66e025 MB |
6803 | int left, top, right, bottom; |
6804 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
6805 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 6806 | |
2d66e025 MB |
6807 | int cw, ch; |
6808 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 6809 | |
2d66e025 | 6810 | if ( top < 0 ) |
3f296516 | 6811 | { |
2d66e025 | 6812 | ypos = r.GetTop(); |
3f296516 | 6813 | } |
2d66e025 MB |
6814 | else if ( bottom > ch ) |
6815 | { | |
6816 | int h = r.GetHeight(); | |
6817 | ypos = r.GetTop(); | |
56b6cf26 | 6818 | for ( i = row - 1; i >= 0; i-- ) |
2d66e025 | 6819 | { |
7c1cb261 VZ |
6820 | int rowHeight = GetRowHeight(i); |
6821 | if ( h + rowHeight > ch ) | |
6822 | break; | |
2d66e025 | 6823 | |
7c1cb261 VZ |
6824 | h += rowHeight; |
6825 | ypos -= rowHeight; | |
2d66e025 | 6826 | } |
f0102d2a VZ |
6827 | |
6828 | // we divide it later by GRID_SCROLL_LINE, make sure that we don't | |
56b6cf26 DS |
6829 | // have rounding errors (this is important, because if we do, |
6830 | // we might not scroll at all and some cells won't be redrawn) | |
275c4ae4 | 6831 | // |
56b6cf26 DS |
6832 | // Sometimes GRID_SCROLL_LINE / 2 is not enough, |
6833 | // so just add a full scroll unit... | |
fd76c6a7 | 6834 | ypos += m_yScrollPixelsPerLine; |
2d66e025 MB |
6835 | } |
6836 | ||
7db713ae | 6837 | // special handling for wide cells - show always left part of the cell! |
faa94f3e | 6838 | // Otherwise, e.g. when stepping from row to row, it would jump between |
7db713ae JS |
6839 | // left and right part of the cell on every step! |
6840 | // if ( left < 0 ) | |
ccdee36f | 6841 | if ( left < 0 || (right - left) >= cw ) |
2d66e025 MB |
6842 | { |
6843 | xpos = r.GetLeft(); | |
6844 | } | |
6845 | else if ( right > cw ) | |
6846 | { | |
73145b0e JS |
6847 | // position the view so that the cell is on the right |
6848 | int x0, y0; | |
6849 | CalcUnscrolledPosition(0, 0, &x0, &y0); | |
6850 | xpos = x0 + (right - cw); | |
f0102d2a VZ |
6851 | |
6852 | // see comment for ypos above | |
fd76c6a7 | 6853 | xpos += m_xScrollPixelsPerLine; |
2d66e025 MB |
6854 | } |
6855 | ||
ccdee36f | 6856 | if ( xpos != -1 || ypos != -1 ) |
2d66e025 | 6857 | { |
97a9929e | 6858 | if ( xpos != -1 ) |
fd76c6a7 | 6859 | xpos /= m_xScrollPixelsPerLine; |
97a9929e | 6860 | if ( ypos != -1 ) |
fd76c6a7 | 6861 | ypos /= m_yScrollPixelsPerLine; |
2d66e025 MB |
6862 | Scroll( xpos, ypos ); |
6863 | AdjustScrollbars(); | |
6864 | } | |
6865 | } | |
6866 | } | |
6867 | ||
2d66e025 MB |
6868 | // |
6869 | // ------ Grid cursor movement functions | |
6870 | // | |
6871 | ||
10a4531d VZ |
6872 | bool |
6873 | wxGrid::DoMoveCursor(bool expandSelection, | |
6874 | const wxGridDirectionOperations& diroper) | |
2d66e025 | 6875 | { |
10a4531d VZ |
6876 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
6877 | return false; | |
6878 | ||
6879 | if ( expandSelection ) | |
2d66e025 | 6880 | { |
8a3e536c VZ |
6881 | wxGridCellCoords coords = m_selectedBlockCorner; |
6882 | if ( coords == wxGridNoCellCoords ) | |
6883 | coords = m_currentCellCoords; | |
4db6714b | 6884 | |
8a3e536c | 6885 | if ( diroper.IsAtBoundary(coords) ) |
10a4531d | 6886 | return false; |
2d66e025 | 6887 | |
8a3e536c | 6888 | diroper.Advance(coords); |
2d66e025 | 6889 | |
8a3e536c | 6890 | UpdateBlockBeingSelected(m_currentCellCoords, coords); |
10a4531d | 6891 | } |
8a3e536c | 6892 | else // don't expand selection |
f85afd4e | 6893 | { |
8a3e536c VZ |
6894 | ClearSelection(); |
6895 | ||
10a4531d | 6896 | if ( diroper.IsAtBoundary(m_currentCellCoords) ) |
ca65c044 | 6897 | return false; |
4db6714b | 6898 | |
10a4531d VZ |
6899 | wxGridCellCoords coords = m_currentCellCoords; |
6900 | diroper.Advance(coords); | |
8a3e536c VZ |
6901 | |
6902 | GoToCell(coords); | |
f85afd4e | 6903 | } |
2d66e025 | 6904 | |
10a4531d | 6905 | return true; |
f85afd4e MB |
6906 | } |
6907 | ||
10a4531d | 6908 | bool wxGrid::MoveCursorUp(bool expandSelection) |
f85afd4e | 6909 | { |
10a4531d VZ |
6910 | return DoMoveCursor(expandSelection, |
6911 | wxGridBackwardOperations(this, wxGridRowOperations())); | |
2d66e025 MB |
6912 | } |
6913 | ||
10a4531d | 6914 | bool wxGrid::MoveCursorDown(bool expandSelection) |
2d66e025 | 6915 | { |
10a4531d VZ |
6916 | return DoMoveCursor(expandSelection, |
6917 | wxGridForwardOperations(this, wxGridRowOperations())); | |
6918 | } | |
4db6714b | 6919 | |
10a4531d VZ |
6920 | bool wxGrid::MoveCursorLeft(bool expandSelection) |
6921 | { | |
6922 | return DoMoveCursor(expandSelection, | |
6923 | wxGridBackwardOperations(this, wxGridColumnOperations())); | |
6924 | } | |
8f177c8e | 6925 | |
10a4531d VZ |
6926 | bool wxGrid::MoveCursorRight(bool expandSelection) |
6927 | { | |
6928 | return DoMoveCursor(expandSelection, | |
6929 | wxGridForwardOperations(this, wxGridColumnOperations())); | |
2d66e025 MB |
6930 | } |
6931 | ||
10a4531d | 6932 | bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations& diroper) |
2d66e025 | 6933 | { |
4db6714b KH |
6934 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
6935 | return false; | |
60ff3b99 | 6936 | |
10a4531d VZ |
6937 | if ( diroper.IsAtBoundary(m_currentCellCoords) ) |
6938 | return false; | |
ef5df12b | 6939 | |
10a4531d VZ |
6940 | const int oldRow = m_currentCellCoords.GetRow(); |
6941 | int newRow = diroper.MoveByPixelDistance(oldRow, m_gridWin->GetClientSize().y); | |
6942 | if ( newRow == oldRow ) | |
6943 | { | |
6944 | wxGridCellCoords coords(m_currentCellCoords); | |
6945 | diroper.Advance(coords); | |
6946 | newRow = coords.GetRow(); | |
6947 | } | |
8f177c8e | 6948 | |
8a3e536c | 6949 | GoToCell(newRow, m_currentCellCoords.GetCol()); |
60ff3b99 | 6950 | |
10a4531d VZ |
6951 | return true; |
6952 | } | |
2d66e025 | 6953 | |
10a4531d VZ |
6954 | bool wxGrid::MovePageUp() |
6955 | { | |
6956 | return DoMoveCursorByPage( | |
6957 | wxGridBackwardOperations(this, wxGridRowOperations())); | |
2d66e025 MB |
6958 | } |
6959 | ||
6960 | bool wxGrid::MovePageDown() | |
6961 | { | |
10a4531d | 6962 | return DoMoveCursorByPage( |
d188378e | 6963 | wxGridForwardOperations(this, wxGridRowOperations())); |
10a4531d | 6964 | } |
60ff3b99 | 6965 | |
10a4531d VZ |
6966 | // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper |
6967 | // until we find a non-empty cell or reach the grid end | |
6968 | void | |
6969 | wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords& coords, | |
6970 | const wxGridDirectionOperations& diroper) | |
6971 | { | |
6972 | while ( !diroper.IsAtBoundary(coords) ) | |
f85afd4e | 6973 | { |
10a4531d VZ |
6974 | diroper.Advance(coords); |
6975 | if ( !m_table->IsEmpty(coords) ) | |
6976 | break; | |
f85afd4e MB |
6977 | } |
6978 | } | |
8f177c8e | 6979 | |
10a4531d VZ |
6980 | bool |
6981 | wxGrid::DoMoveCursorByBlock(bool expandSelection, | |
6982 | const wxGridDirectionOperations& diroper) | |
2d66e025 | 6983 | { |
10a4531d VZ |
6984 | if ( !m_table || m_currentCellCoords == wxGridNoCellCoords ) |
6985 | return false; | |
f85afd4e | 6986 | |
10a4531d VZ |
6987 | if ( diroper.IsAtBoundary(m_currentCellCoords) ) |
6988 | return false; | |
4db6714b | 6989 | |
10a4531d VZ |
6990 | wxGridCellCoords coords(m_currentCellCoords); |
6991 | if ( m_table->IsEmpty(coords) ) | |
6992 | { | |
6993 | // we are in an empty cell: find the next block of non-empty cells | |
6994 | AdvanceToNextNonEmpty(coords, diroper); | |
2d66e025 | 6995 | } |
10a4531d | 6996 | else // current cell is not empty |
f85afd4e | 6997 | { |
10a4531d VZ |
6998 | diroper.Advance(coords); |
6999 | if ( m_table->IsEmpty(coords) ) | |
2d66e025 | 7000 | { |
10a4531d VZ |
7001 | // we started at the end of a block, find the next one |
7002 | AdvanceToNextNonEmpty(coords, diroper); | |
2d66e025 | 7003 | } |
10a4531d | 7004 | else // we're in a middle of a block |
2d66e025 | 7005 | { |
10a4531d VZ |
7006 | // go to the end of it, i.e. find the last cell before the next |
7007 | // empty one | |
7008 | while ( !diroper.IsAtBoundary(coords) ) | |
2d66e025 | 7009 | { |
10a4531d VZ |
7010 | wxGridCellCoords coordsNext(coords); |
7011 | diroper.Advance(coordsNext); | |
7012 | if ( m_table->IsEmpty(coordsNext) ) | |
2d66e025 | 7013 | break; |
2d66e025 | 7014 | |
10a4531d VZ |
7015 | coords = coordsNext; |
7016 | } | |
aa5e1f75 | 7017 | } |
10a4531d | 7018 | } |
2d66e025 | 7019 | |
10a4531d VZ |
7020 | if ( expandSelection ) |
7021 | { | |
8a3e536c | 7022 | UpdateBlockBeingSelected(m_currentCellCoords, coords); |
10a4531d VZ |
7023 | } |
7024 | else | |
7025 | { | |
7026 | ClearSelection(); | |
8a3e536c | 7027 | GoToCell(coords); |
f85afd4e | 7028 | } |
f85afd4e | 7029 | |
10a4531d | 7030 | return true; |
2d66e025 | 7031 | } |
f85afd4e | 7032 | |
10a4531d | 7033 | bool wxGrid::MoveCursorUpBlock(bool expandSelection) |
f85afd4e | 7034 | { |
10a4531d VZ |
7035 | return DoMoveCursorByBlock( |
7036 | expandSelection, | |
7037 | wxGridBackwardOperations(this, wxGridRowOperations()) | |
7038 | ); | |
7039 | } | |
8f177c8e | 7040 | |
10a4531d VZ |
7041 | bool wxGrid::MoveCursorDownBlock( bool expandSelection ) |
7042 | { | |
7043 | return DoMoveCursorByBlock( | |
7044 | expandSelection, | |
7045 | wxGridForwardOperations(this, wxGridRowOperations()) | |
7046 | ); | |
7047 | } | |
2d66e025 | 7048 | |
10a4531d VZ |
7049 | bool wxGrid::MoveCursorLeftBlock( bool expandSelection ) |
7050 | { | |
7051 | return DoMoveCursorByBlock( | |
7052 | expandSelection, | |
7053 | wxGridBackwardOperations(this, wxGridColumnOperations()) | |
7054 | ); | |
f85afd4e MB |
7055 | } |
7056 | ||
5c8fc7c1 | 7057 | bool wxGrid::MoveCursorRightBlock( bool expandSelection ) |
f85afd4e | 7058 | { |
10a4531d VZ |
7059 | return DoMoveCursorByBlock( |
7060 | expandSelection, | |
7061 | wxGridForwardOperations(this, wxGridColumnOperations()) | |
7062 | ); | |
f85afd4e MB |
7063 | } |
7064 | ||
f85afd4e | 7065 | // |
2d66e025 | 7066 | // ------ Label values and formatting |
f85afd4e MB |
7067 | // |
7068 | ||
ef316e23 | 7069 | void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) const |
f85afd4e | 7070 | { |
2f024384 DS |
7071 | if ( horiz ) |
7072 | *horiz = m_rowLabelHorizAlign; | |
7073 | if ( vert ) | |
7074 | *vert = m_rowLabelVertAlign; | |
f85afd4e MB |
7075 | } |
7076 | ||
ef316e23 | 7077 | void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) const |
f85afd4e | 7078 | { |
2f024384 DS |
7079 | if ( horiz ) |
7080 | *horiz = m_colLabelHorizAlign; | |
7081 | if ( vert ) | |
7082 | *vert = m_colLabelVertAlign; | |
f85afd4e MB |
7083 | } |
7084 | ||
ef316e23 | 7085 | int wxGrid::GetColLabelTextOrientation() const |
d43851f7 JS |
7086 | { |
7087 | return m_colLabelTextOrientation; | |
7088 | } | |
7089 | ||
ef316e23 | 7090 | wxString wxGrid::GetRowLabelValue( int row ) const |
f85afd4e MB |
7091 | { |
7092 | if ( m_table ) | |
7093 | { | |
7094 | return m_table->GetRowLabelValue( row ); | |
7095 | } | |
7096 | else | |
7097 | { | |
7098 | wxString s; | |
7099 | s << row; | |
7100 | return s; | |
7101 | } | |
7102 | } | |
7103 | ||
ef316e23 | 7104 | wxString wxGrid::GetColLabelValue( int col ) const |
f85afd4e MB |
7105 | { |
7106 | if ( m_table ) | |
7107 | { | |
7108 | return m_table->GetColLabelValue( col ); | |
7109 | } | |
7110 | else | |
7111 | { | |
7112 | wxString s; | |
7113 | s << col; | |
7114 | return s; | |
7115 | } | |
7116 | } | |
7117 | ||
7118 | void wxGrid::SetRowLabelSize( int width ) | |
7119 | { | |
733f486a VZ |
7120 | wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE ); |
7121 | ||
7122 | if ( width == wxGRID_AUTOSIZE ) | |
7123 | { | |
7124 | width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW); | |
7125 | } | |
7126 | ||
2e8cd977 MB |
7127 | if ( width != m_rowLabelWidth ) |
7128 | { | |
2e8cd977 MB |
7129 | if ( width == 0 ) |
7130 | { | |
ca65c044 WS |
7131 | m_rowLabelWin->Show( false ); |
7132 | m_cornerLabelWin->Show( false ); | |
2e8cd977 | 7133 | } |
7807d81c | 7134 | else if ( m_rowLabelWidth == 0 ) |
2e8cd977 | 7135 | { |
ca65c044 | 7136 | m_rowLabelWin->Show( true ); |
2f024384 DS |
7137 | if ( m_colLabelHeight > 0 ) |
7138 | m_cornerLabelWin->Show( true ); | |
2e8cd977 | 7139 | } |
b99be8fb | 7140 | |
2e8cd977 | 7141 | m_rowLabelWidth = width; |
a6b40a9a | 7142 | InvalidateBestSize(); |
7807d81c | 7143 | CalcWindowSizes(); |
ca65c044 | 7144 | wxScrolledWindow::Refresh( true ); |
2e8cd977 | 7145 | } |
f85afd4e MB |
7146 | } |
7147 | ||
7148 | void wxGrid::SetColLabelSize( int height ) | |
7149 | { | |
733f486a VZ |
7150 | wxASSERT( height >=0 || height == wxGRID_AUTOSIZE ); |
7151 | ||
7152 | if ( height == wxGRID_AUTOSIZE ) | |
7153 | { | |
7154 | height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN); | |
7155 | } | |
7156 | ||
2e8cd977 MB |
7157 | if ( height != m_colLabelHeight ) |
7158 | { | |
2e8cd977 MB |
7159 | if ( height == 0 ) |
7160 | { | |
ad805b9e | 7161 | m_colWindow->Show( false ); |
ca65c044 | 7162 | m_cornerLabelWin->Show( false ); |
2e8cd977 | 7163 | } |
7807d81c | 7164 | else if ( m_colLabelHeight == 0 ) |
2e8cd977 | 7165 | { |
ad805b9e | 7166 | m_colWindow->Show( true ); |
a9339fe2 DS |
7167 | if ( m_rowLabelWidth > 0 ) |
7168 | m_cornerLabelWin->Show( true ); | |
2e8cd977 | 7169 | } |
7807d81c | 7170 | |
2e8cd977 | 7171 | m_colLabelHeight = height; |
a6b40a9a | 7172 | InvalidateBestSize(); |
7807d81c | 7173 | CalcWindowSizes(); |
ca65c044 | 7174 | wxScrolledWindow::Refresh( true ); |
2e8cd977 | 7175 | } |
f85afd4e MB |
7176 | } |
7177 | ||
7178 | void wxGrid::SetLabelBackgroundColour( const wxColour& colour ) | |
7179 | { | |
2d66e025 MB |
7180 | if ( m_labelBackgroundColour != colour ) |
7181 | { | |
7182 | m_labelBackgroundColour = colour; | |
7183 | m_rowLabelWin->SetBackgroundColour( colour ); | |
ad805b9e | 7184 | m_colWindow->SetBackgroundColour( colour ); |
2d66e025 MB |
7185 | m_cornerLabelWin->SetBackgroundColour( colour ); |
7186 | ||
7187 | if ( !GetBatchCount() ) | |
7188 | { | |
7189 | m_rowLabelWin->Refresh(); | |
ad805b9e | 7190 | m_colWindow->Refresh(); |
2d66e025 MB |
7191 | m_cornerLabelWin->Refresh(); |
7192 | } | |
7193 | } | |
f85afd4e MB |
7194 | } |
7195 | ||
7196 | void wxGrid::SetLabelTextColour( const wxColour& colour ) | |
7197 | { | |
2d66e025 MB |
7198 | if ( m_labelTextColour != colour ) |
7199 | { | |
7200 | m_labelTextColour = colour; | |
7201 | if ( !GetBatchCount() ) | |
7202 | { | |
7203 | m_rowLabelWin->Refresh(); | |
ad805b9e | 7204 | m_colWindow->Refresh(); |
2d66e025 MB |
7205 | } |
7206 | } | |
f85afd4e MB |
7207 | } |
7208 | ||
7209 | void wxGrid::SetLabelFont( const wxFont& font ) | |
7210 | { | |
7211 | m_labelFont = font; | |
2d66e025 MB |
7212 | if ( !GetBatchCount() ) |
7213 | { | |
7214 | m_rowLabelWin->Refresh(); | |
ad805b9e | 7215 | m_colWindow->Refresh(); |
2d66e025 | 7216 | } |
f85afd4e MB |
7217 | } |
7218 | ||
7219 | void wxGrid::SetRowLabelAlignment( int horiz, int vert ) | |
7220 | { | |
4c7277db MB |
7221 | // allow old (incorrect) defs to be used |
7222 | switch ( horiz ) | |
7223 | { | |
7224 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
7225 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
7226 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
7227 | } | |
84912ef8 | 7228 | |
4c7277db MB |
7229 | switch ( vert ) |
7230 | { | |
7231 | case wxTOP: vert = wxALIGN_TOP; break; | |
7232 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
7233 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
7234 | } | |
84912ef8 | 7235 | |
4c7277db | 7236 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) |
f85afd4e MB |
7237 | { |
7238 | m_rowLabelHorizAlign = horiz; | |
7239 | } | |
8f177c8e | 7240 | |
4c7277db | 7241 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) |
f85afd4e MB |
7242 | { |
7243 | m_rowLabelVertAlign = vert; | |
7244 | } | |
7245 | ||
2d66e025 MB |
7246 | if ( !GetBatchCount() ) |
7247 | { | |
7248 | m_rowLabelWin->Refresh(); | |
60ff3b99 | 7249 | } |
f85afd4e MB |
7250 | } |
7251 | ||
7252 | void wxGrid::SetColLabelAlignment( int horiz, int vert ) | |
7253 | { | |
4c7277db MB |
7254 | // allow old (incorrect) defs to be used |
7255 | switch ( horiz ) | |
7256 | { | |
7257 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
7258 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
7259 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
7260 | } | |
84912ef8 | 7261 | |
4c7277db MB |
7262 | switch ( vert ) |
7263 | { | |
7264 | case wxTOP: vert = wxALIGN_TOP; break; | |
7265 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
7266 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
7267 | } | |
84912ef8 | 7268 | |
4c7277db | 7269 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) |
f85afd4e MB |
7270 | { |
7271 | m_colLabelHorizAlign = horiz; | |
7272 | } | |
8f177c8e | 7273 | |
4c7277db | 7274 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) |
f85afd4e MB |
7275 | { |
7276 | m_colLabelVertAlign = vert; | |
7277 | } | |
7278 | ||
2d66e025 MB |
7279 | if ( !GetBatchCount() ) |
7280 | { | |
ad805b9e | 7281 | m_colWindow->Refresh(); |
60ff3b99 | 7282 | } |
f85afd4e MB |
7283 | } |
7284 | ||
ca65c044 WS |
7285 | // Note: under MSW, the default column label font must be changed because it |
7286 | // does not support vertical printing | |
d43851f7 JS |
7287 | // |
7288 | // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD); | |
ca65c044 WS |
7289 | // pGrid->SetLabelFont(font); |
7290 | // pGrid->SetColLabelTextOrientation(wxVERTICAL); | |
d43851f7 JS |
7291 | // |
7292 | void wxGrid::SetColLabelTextOrientation( int textOrientation ) | |
7293 | { | |
4db6714b | 7294 | if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL ) |
d43851f7 | 7295 | m_colLabelTextOrientation = textOrientation; |
d43851f7 JS |
7296 | |
7297 | if ( !GetBatchCount() ) | |
ad805b9e | 7298 | m_colWindow->Refresh(); |
d43851f7 JS |
7299 | } |
7300 | ||
f85afd4e MB |
7301 | void wxGrid::SetRowLabelValue( int row, const wxString& s ) |
7302 | { | |
7303 | if ( m_table ) | |
7304 | { | |
7305 | m_table->SetRowLabelValue( row, s ); | |
2d66e025 MB |
7306 | if ( !GetBatchCount() ) |
7307 | { | |
ccdee36f | 7308 | wxRect rect = CellToRect( row, 0 ); |
70c7a608 SN |
7309 | if ( rect.height > 0 ) |
7310 | { | |
cb309039 | 7311 | CalcScrolledPosition(0, rect.y, &rect.x, &rect.y); |
b1944ebc | 7312 | rect.x = 0; |
70c7a608 | 7313 | rect.width = m_rowLabelWidth; |
ca65c044 | 7314 | m_rowLabelWin->Refresh( true, &rect ); |
70c7a608 | 7315 | } |
2d66e025 | 7316 | } |
f85afd4e MB |
7317 | } |
7318 | } | |
7319 | ||
7320 | void wxGrid::SetColLabelValue( int col, const wxString& s ) | |
7321 | { | |
7322 | if ( m_table ) | |
7323 | { | |
7324 | m_table->SetColLabelValue( col, s ); | |
2d66e025 MB |
7325 | if ( !GetBatchCount() ) |
7326 | { | |
ad805b9e | 7327 | if ( m_useNativeHeader ) |
70c7a608 | 7328 | { |
3039ade9 | 7329 | GetGridColHeader()->UpdateColumn(col); |
ad805b9e VZ |
7330 | } |
7331 | else | |
7332 | { | |
7333 | wxRect rect = CellToRect( 0, col ); | |
7334 | if ( rect.width > 0 ) | |
7335 | { | |
7336 | CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y); | |
7337 | rect.y = 0; | |
7338 | rect.height = m_colLabelHeight; | |
7339 | GetColLabelWindow()->Refresh( true, &rect ); | |
7340 | } | |
70c7a608 | 7341 | } |
2d66e025 | 7342 | } |
f85afd4e MB |
7343 | } |
7344 | } | |
7345 | ||
7346 | void wxGrid::SetGridLineColour( const wxColour& colour ) | |
7347 | { | |
2d66e025 MB |
7348 | if ( m_gridLineColour != colour ) |
7349 | { | |
7350 | m_gridLineColour = colour; | |
60ff3b99 | 7351 | |
9f7aee01 VZ |
7352 | if ( GridLinesEnabled() ) |
7353 | RedrawGridLines(); | |
2d66e025 | 7354 | } |
f85afd4e MB |
7355 | } |
7356 | ||
f6bcfd97 BP |
7357 | void wxGrid::SetCellHighlightColour( const wxColour& colour ) |
7358 | { | |
7359 | if ( m_cellHighlightColour != colour ) | |
7360 | { | |
7361 | m_cellHighlightColour = colour; | |
7362 | ||
7363 | wxClientDC dc( m_gridWin ); | |
7364 | PrepareDC( dc ); | |
7365 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
7366 | DrawCellHighlight(dc, attr); | |
7367 | attr->DecRef(); | |
7368 | } | |
7369 | } | |
7370 | ||
d2520c85 RD |
7371 | void wxGrid::SetCellHighlightPenWidth(int width) |
7372 | { | |
4db6714b KH |
7373 | if (m_cellHighlightPenWidth != width) |
7374 | { | |
d2520c85 RD |
7375 | m_cellHighlightPenWidth = width; |
7376 | ||
7377 | // Just redrawing the cell highlight is not enough since that won't | |
4c51a665 | 7378 | // make any visible change if the thickness is getting smaller. |
d2520c85 RD |
7379 | int row = m_currentCellCoords.GetRow(); |
7380 | int col = m_currentCellCoords.GetCol(); | |
1b3b96d8 | 7381 | if ( row == -1 || col == -1 || GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
d2520c85 | 7382 | return; |
2f024384 | 7383 | |
d2520c85 | 7384 | wxRect rect = CellToRect(row, col); |
ca65c044 | 7385 | m_gridWin->Refresh(true, &rect); |
d2520c85 RD |
7386 | } |
7387 | } | |
7388 | ||
7389 | void wxGrid::SetCellHighlightROPenWidth(int width) | |
7390 | { | |
4db6714b KH |
7391 | if (m_cellHighlightROPenWidth != width) |
7392 | { | |
d2520c85 RD |
7393 | m_cellHighlightROPenWidth = width; |
7394 | ||
7395 | // Just redrawing the cell highlight is not enough since that won't | |
4c51a665 | 7396 | // make any visible change if the thickness is getting smaller. |
d2520c85 RD |
7397 | int row = m_currentCellCoords.GetRow(); |
7398 | int col = m_currentCellCoords.GetCol(); | |
76c66f19 VZ |
7399 | if ( row == -1 || col == -1 || |
7400 | GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
d2520c85 | 7401 | return; |
ccdee36f | 7402 | |
d2520c85 | 7403 | wxRect rect = CellToRect(row, col); |
ca65c044 | 7404 | m_gridWin->Refresh(true, &rect); |
d2520c85 RD |
7405 | } |
7406 | } | |
7407 | ||
9f7aee01 VZ |
7408 | void wxGrid::RedrawGridLines() |
7409 | { | |
7410 | // the lines will be redrawn when the window is thawn | |
7411 | if ( GetBatchCount() ) | |
7412 | return; | |
7413 | ||
7414 | if ( GridLinesEnabled() ) | |
7415 | { | |
7416 | wxClientDC dc( m_gridWin ); | |
7417 | PrepareDC( dc ); | |
7418 | DrawAllGridLines( dc, wxRegion() ); | |
7419 | } | |
7420 | else // remove the grid lines | |
7421 | { | |
7422 | m_gridWin->Refresh(); | |
7423 | } | |
7424 | } | |
7425 | ||
f85afd4e MB |
7426 | void wxGrid::EnableGridLines( bool enable ) |
7427 | { | |
7428 | if ( enable != m_gridLinesEnabled ) | |
7429 | { | |
7430 | m_gridLinesEnabled = enable; | |
2d66e025 | 7431 | |
9f7aee01 VZ |
7432 | RedrawGridLines(); |
7433 | } | |
7434 | } | |
7435 | ||
7436 | void wxGrid::DoClipGridLines(bool& var, bool clip) | |
7437 | { | |
7438 | if ( clip != var ) | |
7439 | { | |
7440 | var = clip; | |
7441 | ||
7442 | if ( GridLinesEnabled() ) | |
7443 | RedrawGridLines(); | |
f85afd4e MB |
7444 | } |
7445 | } | |
7446 | ||
ef316e23 | 7447 | int wxGrid::GetDefaultRowSize() const |
f85afd4e MB |
7448 | { |
7449 | return m_defaultRowHeight; | |
7450 | } | |
7451 | ||
ef316e23 | 7452 | int wxGrid::GetRowSize( int row ) const |
f85afd4e | 7453 | { |
9a83f860 | 7454 | wxCHECK_MSG( row >= 0 && row < m_numRows, 0, wxT("invalid row index") ); |
b99be8fb | 7455 | |
7c1cb261 | 7456 | return GetRowHeight(row); |
f85afd4e MB |
7457 | } |
7458 | ||
ef316e23 | 7459 | int wxGrid::GetDefaultColSize() const |
f85afd4e MB |
7460 | { |
7461 | return m_defaultColWidth; | |
7462 | } | |
7463 | ||
ef316e23 | 7464 | int wxGrid::GetColSize( int col ) const |
f85afd4e | 7465 | { |
9a83f860 | 7466 | wxCHECK_MSG( col >= 0 && col < m_numCols, 0, wxT("invalid column index") ); |
b99be8fb | 7467 | |
7c1cb261 | 7468 | return GetColWidth(col); |
f85afd4e MB |
7469 | } |
7470 | ||
2e9a6788 VZ |
7471 | // ============================================================================ |
7472 | // access to the grid attributes: each of them has a default value in the grid | |
7473 | // itself and may be overidden on a per-cell basis | |
7474 | // ============================================================================ | |
7475 | ||
7476 | // ---------------------------------------------------------------------------- | |
7477 | // setting default attributes | |
7478 | // ---------------------------------------------------------------------------- | |
7479 | ||
7480 | void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col ) | |
7481 | { | |
2796cce3 | 7482 | m_defaultCellAttr->SetBackgroundColour(col); |
c916e13b RR |
7483 | #ifdef __WXGTK__ |
7484 | m_gridWin->SetBackgroundColour(col); | |
7485 | #endif | |
2e9a6788 VZ |
7486 | } |
7487 | ||
7488 | void wxGrid::SetDefaultCellTextColour( const wxColour& col ) | |
7489 | { | |
2796cce3 | 7490 | m_defaultCellAttr->SetTextColour(col); |
2e9a6788 VZ |
7491 | } |
7492 | ||
7493 | void wxGrid::SetDefaultCellAlignment( int horiz, int vert ) | |
7494 | { | |
2796cce3 | 7495 | m_defaultCellAttr->SetAlignment(horiz, vert); |
2e9a6788 VZ |
7496 | } |
7497 | ||
27f35b66 SN |
7498 | void wxGrid::SetDefaultCellOverflow( bool allow ) |
7499 | { | |
7500 | m_defaultCellAttr->SetOverflow(allow); | |
7501 | } | |
7502 | ||
2e9a6788 VZ |
7503 | void wxGrid::SetDefaultCellFont( const wxFont& font ) |
7504 | { | |
2796cce3 RD |
7505 | m_defaultCellAttr->SetFont(font); |
7506 | } | |
7507 | ||
ca63e8e9 RD |
7508 | // For editors and renderers the type registry takes precedence over the |
7509 | // default attr, so we need to register the new editor/renderer for the string | |
7510 | // data type in order to make setting a default editor/renderer appear to | |
7511 | // work correctly. | |
7512 | ||
0ba143c9 RD |
7513 | void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer) |
7514 | { | |
ca63e8e9 RD |
7515 | RegisterDataType(wxGRID_VALUE_STRING, |
7516 | renderer, | |
7517 | GetDefaultEditorForType(wxGRID_VALUE_STRING)); | |
0ba143c9 | 7518 | } |
2e9a6788 | 7519 | |
0ba143c9 RD |
7520 | void wxGrid::SetDefaultEditor(wxGridCellEditor *editor) |
7521 | { | |
ca63e8e9 RD |
7522 | RegisterDataType(wxGRID_VALUE_STRING, |
7523 | GetDefaultRendererForType(wxGRID_VALUE_STRING), | |
42841dfc | 7524 | editor); |
0ba143c9 | 7525 | } |
9b4aede2 | 7526 | |
2e9a6788 | 7527 | // ---------------------------------------------------------------------------- |
ef316e23 | 7528 | // access to the default attributes |
2e9a6788 VZ |
7529 | // ---------------------------------------------------------------------------- |
7530 | ||
ef316e23 | 7531 | wxColour wxGrid::GetDefaultCellBackgroundColour() const |
f85afd4e | 7532 | { |
2796cce3 | 7533 | return m_defaultCellAttr->GetBackgroundColour(); |
f85afd4e MB |
7534 | } |
7535 | ||
ef316e23 | 7536 | wxColour wxGrid::GetDefaultCellTextColour() const |
2e9a6788 | 7537 | { |
2796cce3 | 7538 | return m_defaultCellAttr->GetTextColour(); |
2e9a6788 VZ |
7539 | } |
7540 | ||
ef316e23 | 7541 | wxFont wxGrid::GetDefaultCellFont() const |
2e9a6788 | 7542 | { |
2796cce3 | 7543 | return m_defaultCellAttr->GetFont(); |
2e9a6788 VZ |
7544 | } |
7545 | ||
ef316e23 | 7546 | void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const |
2e9a6788 | 7547 | { |
2796cce3 | 7548 | m_defaultCellAttr->GetAlignment(horiz, vert); |
2e9a6788 VZ |
7549 | } |
7550 | ||
ef316e23 | 7551 | bool wxGrid::GetDefaultCellOverflow() const |
27f35b66 SN |
7552 | { |
7553 | return m_defaultCellAttr->GetOverflow(); | |
7554 | } | |
7555 | ||
0ba143c9 RD |
7556 | wxGridCellRenderer *wxGrid::GetDefaultRenderer() const |
7557 | { | |
0b190b0f | 7558 | return m_defaultCellAttr->GetRenderer(NULL, 0, 0); |
0ba143c9 | 7559 | } |
ab79958a | 7560 | |
0ba143c9 RD |
7561 | wxGridCellEditor *wxGrid::GetDefaultEditor() const |
7562 | { | |
4db6714b | 7563 | return m_defaultCellAttr->GetEditor(NULL, 0, 0); |
0ba143c9 | 7564 | } |
9b4aede2 | 7565 | |
2e9a6788 VZ |
7566 | // ---------------------------------------------------------------------------- |
7567 | // access to cell attributes | |
7568 | // ---------------------------------------------------------------------------- | |
7569 | ||
ef316e23 | 7570 | wxColour wxGrid::GetCellBackgroundColour(int row, int col) const |
f85afd4e | 7571 | { |
0a976765 | 7572 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7573 | wxColour colour = attr->GetBackgroundColour(); |
39bcce60 | 7574 | attr->DecRef(); |
2f024384 | 7575 | |
b99be8fb | 7576 | return colour; |
f85afd4e MB |
7577 | } |
7578 | ||
ef316e23 | 7579 | wxColour wxGrid::GetCellTextColour( int row, int col ) const |
f85afd4e | 7580 | { |
0a976765 | 7581 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7582 | wxColour colour = attr->GetTextColour(); |
39bcce60 | 7583 | attr->DecRef(); |
2f024384 | 7584 | |
b99be8fb | 7585 | return colour; |
f85afd4e MB |
7586 | } |
7587 | ||
ef316e23 | 7588 | wxFont wxGrid::GetCellFont( int row, int col ) const |
f85afd4e | 7589 | { |
0a976765 | 7590 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7591 | wxFont font = attr->GetFont(); |
39bcce60 | 7592 | attr->DecRef(); |
2f024384 | 7593 | |
b99be8fb | 7594 | return font; |
f85afd4e MB |
7595 | } |
7596 | ||
ef316e23 | 7597 | void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const |
f85afd4e | 7598 | { |
0a976765 | 7599 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7600 | attr->GetAlignment(horiz, vert); |
39bcce60 | 7601 | attr->DecRef(); |
2e9a6788 VZ |
7602 | } |
7603 | ||
ef316e23 | 7604 | bool wxGrid::GetCellOverflow( int row, int col ) const |
27f35b66 SN |
7605 | { |
7606 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
7607 | bool allow = attr->GetOverflow(); | |
7608 | attr->DecRef(); | |
4db6714b | 7609 | |
27f35b66 SN |
7610 | return allow; |
7611 | } | |
7612 | ||
ea99e8e3 VZ |
7613 | wxGrid::CellSpan |
7614 | wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const | |
27f35b66 SN |
7615 | { |
7616 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
7617 | attr->GetSize( num_rows, num_cols ); | |
7618 | attr->DecRef(); | |
ea99e8e3 VZ |
7619 | |
7620 | if ( *num_rows == 1 && *num_cols == 1 ) | |
7621 | return CellSpan_None; // just a normal cell | |
7622 | ||
7623 | if ( *num_rows < 0 || *num_cols < 0 ) | |
7624 | return CellSpan_Inside; // covered by a multi-span cell | |
7625 | ||
7626 | // this cell spans multiple cells to its right/bottom | |
7627 | return CellSpan_Main; | |
27f35b66 SN |
7628 | } |
7629 | ||
ef316e23 | 7630 | wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const |
2796cce3 RD |
7631 | { |
7632 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 7633 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); |
2796cce3 | 7634 | attr->DecRef(); |
0b190b0f | 7635 | |
2796cce3 RD |
7636 | return renderer; |
7637 | } | |
7638 | ||
ef316e23 | 7639 | wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const |
9b4aede2 RD |
7640 | { |
7641 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 7642 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
9b4aede2 | 7643 | attr->DecRef(); |
0b190b0f | 7644 | |
9b4aede2 RD |
7645 | return editor; |
7646 | } | |
7647 | ||
283b7808 VZ |
7648 | bool wxGrid::IsReadOnly(int row, int col) const |
7649 | { | |
7650 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
7651 | bool isReadOnly = attr->IsReadOnly(); | |
7652 | attr->DecRef(); | |
4db6714b | 7653 | |
283b7808 VZ |
7654 | return isReadOnly; |
7655 | } | |
7656 | ||
2e9a6788 | 7657 | // ---------------------------------------------------------------------------- |
758cbedf | 7658 | // attribute support: cache, automatic provider creation, ... |
2e9a6788 VZ |
7659 | // ---------------------------------------------------------------------------- |
7660 | ||
ef316e23 | 7661 | bool wxGrid::CanHaveAttributes() const |
2e9a6788 VZ |
7662 | { |
7663 | if ( !m_table ) | |
7664 | { | |
ca65c044 | 7665 | return false; |
2e9a6788 VZ |
7666 | } |
7667 | ||
f2d76237 | 7668 | return m_table->CanHaveAttributes(); |
2e9a6788 VZ |
7669 | } |
7670 | ||
0a976765 VZ |
7671 | void wxGrid::ClearAttrCache() |
7672 | { | |
7673 | if ( m_attrCache.row != -1 ) | |
7674 | { | |
54181a33 | 7675 | wxGridCellAttr *oldAttr = m_attrCache.attr; |
19d7140e | 7676 | m_attrCache.attr = NULL; |
0a976765 | 7677 | m_attrCache.row = -1; |
1506cc66 SN |
7678 | // wxSafeDecRec(...) might cause event processing that accesses |
7679 | // the cached attribute, if one exists (e.g. by deleting the | |
7680 | // editor stored within the attribute). Therefore it is important | |
ace8d849 | 7681 | // to invalidate the cache before calling wxSafeDecRef! |
1506cc66 | 7682 | wxSafeDecRef(oldAttr); |
0a976765 VZ |
7683 | } |
7684 | } | |
7685 | ||
b99450ee VZ |
7686 | void wxGrid::RefreshAttr(int row, int col) |
7687 | { | |
7688 | if ( m_attrCache.row == row && m_attrCache.col == col ) | |
7689 | ClearAttrCache(); | |
7690 | } | |
7691 | ||
7692 | ||
0a976765 VZ |
7693 | void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const |
7694 | { | |
2b5f62a0 VZ |
7695 | if ( attr != NULL ) |
7696 | { | |
f48a1159 | 7697 | wxGrid * const self = const_cast<wxGrid *>(this); |
0a976765 | 7698 | |
2b5f62a0 VZ |
7699 | self->ClearAttrCache(); |
7700 | self->m_attrCache.row = row; | |
7701 | self->m_attrCache.col = col; | |
7702 | self->m_attrCache.attr = attr; | |
7703 | wxSafeIncRef(attr); | |
7704 | } | |
0a976765 VZ |
7705 | } |
7706 | ||
7707 | bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const | |
7708 | { | |
7709 | if ( row == m_attrCache.row && col == m_attrCache.col ) | |
7710 | { | |
7711 | *attr = m_attrCache.attr; | |
39bcce60 | 7712 | wxSafeIncRef(m_attrCache.attr); |
0a976765 VZ |
7713 | |
7714 | #ifdef DEBUG_ATTR_CACHE | |
7715 | gs_nAttrCacheHits++; | |
7716 | #endif | |
7717 | ||
ca65c044 | 7718 | return true; |
0a976765 VZ |
7719 | } |
7720 | else | |
7721 | { | |
7722 | #ifdef DEBUG_ATTR_CACHE | |
7723 | gs_nAttrCacheMisses++; | |
7724 | #endif | |
4db6714b | 7725 | |
ca65c044 | 7726 | return false; |
0a976765 VZ |
7727 | } |
7728 | } | |
7729 | ||
2e9a6788 VZ |
7730 | wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const |
7731 | { | |
a373d23b SN |
7732 | wxGridCellAttr *attr = NULL; |
7733 | // Additional test to avoid looking at the cache e.g. for | |
7734 | // wxNoCellCoords, as this will confuse memory management. | |
7735 | if ( row >= 0 ) | |
7736 | { | |
3ed884a0 SN |
7737 | if ( !LookupAttr(row, col, &attr) ) |
7738 | { | |
c2f5b920 | 7739 | attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any) |
10a4531d | 7740 | : NULL; |
3ed884a0 SN |
7741 | CacheAttr(row, col, attr); |
7742 | } | |
0a976765 | 7743 | } |
4db6714b | 7744 | |
508011ce VZ |
7745 | if (attr) |
7746 | { | |
2796cce3 | 7747 | attr->SetDefAttr(m_defaultCellAttr); |
508011ce VZ |
7748 | } |
7749 | else | |
7750 | { | |
2796cce3 RD |
7751 | attr = m_defaultCellAttr; |
7752 | attr->IncRef(); | |
7753 | } | |
2e9a6788 | 7754 | |
0a976765 VZ |
7755 | return attr; |
7756 | } | |
7757 | ||
7758 | wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const | |
7759 | { | |
10a4531d | 7760 | wxGridCellAttr *attr = NULL; |
71e60f70 | 7761 | bool canHave = ((wxGrid*)this)->CanHaveAttributes(); |
0a976765 | 7762 | |
9a83f860 VZ |
7763 | wxCHECK_MSG( canHave, attr, wxT("Cell attributes not allowed")); |
7764 | wxCHECK_MSG( m_table, attr, wxT("must have a table") ); | |
1df4050d VZ |
7765 | |
7766 | attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell); | |
7767 | if ( !attr ) | |
7768 | { | |
7769 | attr = new wxGridCellAttr(m_defaultCellAttr); | |
7770 | ||
7771 | // artificially inc the ref count to match DecRef() in caller | |
7772 | attr->IncRef(); | |
7773 | m_table->SetAttr(attr, row, col); | |
7774 | } | |
0a976765 | 7775 | |
2e9a6788 VZ |
7776 | return attr; |
7777 | } | |
7778 | ||
0b190b0f VZ |
7779 | // ---------------------------------------------------------------------------- |
7780 | // setting column attributes (wrappers around SetColAttr) | |
7781 | // ---------------------------------------------------------------------------- | |
7782 | ||
7783 | void wxGrid::SetColFormatBool(int col) | |
7784 | { | |
7785 | SetColFormatCustom(col, wxGRID_VALUE_BOOL); | |
7786 | } | |
7787 | ||
7788 | void wxGrid::SetColFormatNumber(int col) | |
7789 | { | |
7790 | SetColFormatCustom(col, wxGRID_VALUE_NUMBER); | |
7791 | } | |
7792 | ||
7793 | void wxGrid::SetColFormatFloat(int col, int width, int precision) | |
7794 | { | |
7795 | wxString typeName = wxGRID_VALUE_FLOAT; | |
7796 | if ( (width != -1) || (precision != -1) ) | |
7797 | { | |
9a83f860 | 7798 | typeName << wxT(':') << width << wxT(',') << precision; |
0b190b0f VZ |
7799 | } |
7800 | ||
7801 | SetColFormatCustom(col, typeName); | |
7802 | } | |
7803 | ||
7804 | void wxGrid::SetColFormatCustom(int col, const wxString& typeName) | |
7805 | { | |
999836aa | 7806 | wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col ); |
4db6714b | 7807 | if (!attr) |
19d7140e | 7808 | attr = new wxGridCellAttr; |
0b190b0f VZ |
7809 | wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName); |
7810 | attr->SetRenderer(renderer); | |
54181a33 VZ |
7811 | wxGridCellEditor *editor = GetDefaultEditorForType(typeName); |
7812 | attr->SetEditor(editor); | |
0b190b0f VZ |
7813 | |
7814 | SetColAttr(col, attr); | |
19d7140e | 7815 | |
0b190b0f VZ |
7816 | } |
7817 | ||
758cbedf VZ |
7818 | // ---------------------------------------------------------------------------- |
7819 | // setting cell attributes: this is forwarded to the table | |
7820 | // ---------------------------------------------------------------------------- | |
7821 | ||
27f35b66 SN |
7822 | void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr) |
7823 | { | |
7824 | if ( CanHaveAttributes() ) | |
7825 | { | |
7826 | m_table->SetAttr(attr, row, col); | |
7827 | ClearAttrCache(); | |
7828 | } | |
7829 | else | |
7830 | { | |
7831 | wxSafeDecRef(attr); | |
7832 | } | |
7833 | } | |
7834 | ||
758cbedf VZ |
7835 | void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr) |
7836 | { | |
7837 | if ( CanHaveAttributes() ) | |
7838 | { | |
7839 | m_table->SetRowAttr(attr, row); | |
19d7140e | 7840 | ClearAttrCache(); |
758cbedf VZ |
7841 | } |
7842 | else | |
7843 | { | |
39bcce60 | 7844 | wxSafeDecRef(attr); |
758cbedf VZ |
7845 | } |
7846 | } | |
7847 | ||
7848 | void wxGrid::SetColAttr(int col, wxGridCellAttr *attr) | |
7849 | { | |
7850 | if ( CanHaveAttributes() ) | |
7851 | { | |
7852 | m_table->SetColAttr(attr, col); | |
19d7140e | 7853 | ClearAttrCache(); |
758cbedf VZ |
7854 | } |
7855 | else | |
7856 | { | |
39bcce60 | 7857 | wxSafeDecRef(attr); |
758cbedf VZ |
7858 | } |
7859 | } | |
7860 | ||
2e9a6788 VZ |
7861 | void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour ) |
7862 | { | |
7863 | if ( CanHaveAttributes() ) | |
7864 | { | |
0a976765 | 7865 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7866 | attr->SetBackgroundColour(colour); |
7867 | attr->DecRef(); | |
7868 | } | |
7869 | } | |
7870 | ||
7871 | void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour ) | |
7872 | { | |
7873 | if ( CanHaveAttributes() ) | |
7874 | { | |
0a976765 | 7875 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7876 | attr->SetTextColour(colour); |
7877 | attr->DecRef(); | |
7878 | } | |
7879 | } | |
7880 | ||
7881 | void wxGrid::SetCellFont( int row, int col, const wxFont& font ) | |
7882 | { | |
7883 | if ( CanHaveAttributes() ) | |
7884 | { | |
0a976765 | 7885 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7886 | attr->SetFont(font); |
7887 | attr->DecRef(); | |
7888 | } | |
7889 | } | |
7890 | ||
7891 | void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert ) | |
7892 | { | |
7893 | if ( CanHaveAttributes() ) | |
7894 | { | |
0a976765 | 7895 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7896 | attr->SetAlignment(horiz, vert); |
7897 | attr->DecRef(); | |
ab79958a VZ |
7898 | } |
7899 | } | |
7900 | ||
27f35b66 SN |
7901 | void wxGrid::SetCellOverflow( int row, int col, bool allow ) |
7902 | { | |
7903 | if ( CanHaveAttributes() ) | |
7904 | { | |
7905 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7906 | attr->SetOverflow(allow); | |
7907 | attr->DecRef(); | |
7908 | } | |
7909 | } | |
7910 | ||
7911 | void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols ) | |
7912 | { | |
7913 | if ( CanHaveAttributes() ) | |
7914 | { | |
7915 | int cell_rows, cell_cols; | |
7916 | ||
7917 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7918 | attr->GetSize(&cell_rows, &cell_cols); | |
7919 | attr->SetSize(num_rows, num_cols); | |
7920 | attr->DecRef(); | |
7921 | ||
7922 | // Cannot set the size of a cell to 0 or negative values | |
7923 | // While it is perfectly legal to do that, this function cannot | |
7924 | // handle all the possibilies, do it by hand by getting the CellAttr. | |
7925 | // You can only set the size of a cell to 1,1 or greater with this fn | |
7926 | wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)), | |
7927 | wxT("wxGrid::SetCellSize setting cell size that is already part of another cell")); | |
7928 | wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)), | |
7929 | wxT("wxGrid::SetCellSize setting cell size to < 1")); | |
7930 | ||
7931 | // if this was already a multicell then "turn off" the other cells first | |
a6b2078d | 7932 | if ((cell_rows > 1) || (cell_cols > 1)) |
27f35b66 SN |
7933 | { |
7934 | int i, j; | |
2f024384 | 7935 | for (j=row; j < row + cell_rows; j++) |
27f35b66 | 7936 | { |
2f024384 | 7937 | for (i=col; i < col + cell_cols; i++) |
27f35b66 SN |
7938 | { |
7939 | if ((i != col) || (j != row)) | |
7940 | { | |
7941 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
7942 | attr_stub->SetSize( 1, 1 ); | |
7943 | attr_stub->DecRef(); | |
7944 | } | |
7945 | } | |
7946 | } | |
7947 | } | |
7948 | ||
7949 | // mark the cells that will be covered by this cell to | |
7950 | // negative or zero values to point back at this cell | |
7951 | if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1)) | |
7952 | { | |
7953 | int i, j; | |
2f024384 | 7954 | for (j=row; j < row + num_rows; j++) |
27f35b66 | 7955 | { |
2f024384 | 7956 | for (i=col; i < col + num_cols; i++) |
27f35b66 SN |
7957 | { |
7958 | if ((i != col) || (j != row)) | |
7959 | { | |
7960 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
2f024384 | 7961 | attr_stub->SetSize( row - j, col - i ); |
27f35b66 SN |
7962 | attr_stub->DecRef(); |
7963 | } | |
7964 | } | |
7965 | } | |
7966 | } | |
7967 | } | |
7968 | } | |
7969 | ||
ab79958a VZ |
7970 | void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer) |
7971 | { | |
7972 | if ( CanHaveAttributes() ) | |
7973 | { | |
0a976765 | 7974 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
ab79958a VZ |
7975 | attr->SetRenderer(renderer); |
7976 | attr->DecRef(); | |
9b4aede2 RD |
7977 | } |
7978 | } | |
7979 | ||
7980 | void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor) | |
7981 | { | |
7982 | if ( CanHaveAttributes() ) | |
7983 | { | |
7984 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7985 | attr->SetEditor(editor); | |
7986 | attr->DecRef(); | |
283b7808 VZ |
7987 | } |
7988 | } | |
7989 | ||
7990 | void wxGrid::SetReadOnly(int row, int col, bool isReadOnly) | |
7991 | { | |
7992 | if ( CanHaveAttributes() ) | |
7993 | { | |
7994 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7995 | attr->SetReadOnly(isReadOnly); | |
7996 | attr->DecRef(); | |
2e9a6788 | 7997 | } |
f85afd4e MB |
7998 | } |
7999 | ||
f2d76237 RD |
8000 | // ---------------------------------------------------------------------------- |
8001 | // Data type registration | |
8002 | // ---------------------------------------------------------------------------- | |
8003 | ||
8004 | void wxGrid::RegisterDataType(const wxString& typeName, | |
8005 | wxGridCellRenderer* renderer, | |
8006 | wxGridCellEditor* editor) | |
8007 | { | |
8008 | m_typeRegistry->RegisterDataType(typeName, renderer, editor); | |
8009 | } | |
8010 | ||
8011 | ||
a9339fe2 | 8012 | wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const |
f2d76237 RD |
8013 | { |
8014 | wxString typeName = m_table->GetTypeName(row, col); | |
8015 | return GetDefaultEditorForType(typeName); | |
8016 | } | |
8017 | ||
a9339fe2 | 8018 | wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const |
f2d76237 RD |
8019 | { |
8020 | wxString typeName = m_table->GetTypeName(row, col); | |
8021 | return GetDefaultRendererForType(typeName); | |
8022 | } | |
8023 | ||
a9339fe2 | 8024 | wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const |
f2d76237 | 8025 | { |
c4608a8a | 8026 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
8027 | if ( index == wxNOT_FOUND ) |
8028 | { | |
767e0835 | 8029 | wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str())); |
0b190b0f | 8030 | |
f2d76237 RD |
8031 | return NULL; |
8032 | } | |
0b190b0f | 8033 | |
f2d76237 RD |
8034 | return m_typeRegistry->GetEditor(index); |
8035 | } | |
8036 | ||
a9339fe2 | 8037 | wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const |
f2d76237 | 8038 | { |
c4608a8a | 8039 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
8040 | if ( index == wxNOT_FOUND ) |
8041 | { | |
767e0835 | 8042 | wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str())); |
0b190b0f | 8043 | |
c4608a8a | 8044 | return NULL; |
e72b4213 | 8045 | } |
0b190b0f | 8046 | |
c4608a8a | 8047 | return m_typeRegistry->GetRenderer(index); |
f2d76237 RD |
8048 | } |
8049 | ||
2e9a6788 VZ |
8050 | // ---------------------------------------------------------------------------- |
8051 | // row/col size | |
8052 | // ---------------------------------------------------------------------------- | |
8053 | ||
82edfbe7 VZ |
8054 | void wxGrid::DoDisableLineResize(int line, wxGridFixedIndicesSet *& setFixed) |
8055 | { | |
8056 | if ( !setFixed ) | |
8057 | { | |
8058 | setFixed = new wxGridFixedIndicesSet; | |
8059 | } | |
8060 | ||
8061 | setFixed->insert(line); | |
8062 | } | |
8063 | ||
8064 | bool | |
8065 | wxGrid::DoCanResizeLine(int line, const wxGridFixedIndicesSet *setFixed) const | |
8066 | { | |
8067 | return !setFixed || !setFixed->count(line); | |
8068 | } | |
8069 | ||
6e8524b1 MB |
8070 | void wxGrid::EnableDragRowSize( bool enable ) |
8071 | { | |
8072 | m_canDragRowSize = enable; | |
8073 | } | |
8074 | ||
6e8524b1 MB |
8075 | void wxGrid::EnableDragColSize( bool enable ) |
8076 | { | |
8077 | m_canDragColSize = enable; | |
8078 | } | |
8079 | ||
4cfa5de6 RD |
8080 | void wxGrid::EnableDragGridSize( bool enable ) |
8081 | { | |
8082 | m_canDragGridSize = enable; | |
8083 | } | |
8084 | ||
79dbea21 RD |
8085 | void wxGrid::EnableDragCell( bool enable ) |
8086 | { | |
8087 | m_canDragCell = enable; | |
8088 | } | |
6e8524b1 | 8089 | |
f85afd4e MB |
8090 | void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) |
8091 | { | |
b8d24d4e | 8092 | m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight ); |
f85afd4e MB |
8093 | |
8094 | if ( resizeExistingRows ) | |
8095 | { | |
b1da8107 SN |
8096 | // since we are resizing all rows to the default row size, |
8097 | // we can simply clear the row heights and row bottoms | |
8098 | // arrays (which also allows us to take advantage of | |
8099 | // some speed optimisations) | |
8100 | m_rowHeights.Empty(); | |
8101 | m_rowBottoms.Empty(); | |
edb89f7e VZ |
8102 | if ( !GetBatchCount() ) |
8103 | CalcDimensions(); | |
f85afd4e MB |
8104 | } |
8105 | } | |
8106 | ||
30496905 VZ |
8107 | namespace |
8108 | { | |
8109 | ||
8110 | // This is a common part of SetRowSize() and SetColSize() which takes care of | |
8111 | // updating the height/width of a row/column depending on its current value and | |
8112 | // the new one. | |
8113 | // | |
8114 | // Returns the difference between the new and the old size. | |
8115 | int UpdateRowOrColSize(int& sizeCurrent, int sizeNew) | |
8116 | { | |
8117 | // On input here sizeCurrent can be negative if it's currently hidden (the | |
8118 | // real size is its absolute value then). And sizeNew can be 0 to indicate | |
8119 | // that the row/column should be hidden or -1 to indicate that it should be | |
8120 | // shown again. | |
8121 | ||
8122 | if ( sizeNew < 0 ) | |
8123 | { | |
8124 | // We're showing back a previously hidden row/column. | |
8125 | wxASSERT_MSG( sizeNew == -1, wxS("New size must be positive or -1.") ); | |
8126 | ||
2328f468 VZ |
8127 | // If it's already visible, simply do nothing. |
8128 | if ( sizeCurrent >= 0 ) | |
8129 | return 0; | |
30496905 | 8130 | |
2328f468 | 8131 | // Otherwise show it by restoring its old size. |
30496905 VZ |
8132 | sizeCurrent = -sizeCurrent; |
8133 | ||
8134 | // This is positive which is correct. | |
8135 | return sizeCurrent; | |
8136 | } | |
8137 | else if ( sizeNew == 0 ) | |
8138 | { | |
8139 | // We're hiding a row/column. | |
30496905 | 8140 | |
2328f468 VZ |
8141 | // If it's already hidden, simply do nothing. |
8142 | if ( sizeCurrent <= 0 ) | |
8143 | return 0; | |
8144 | ||
8145 | // Otherwise hide it and also remember the shown size to be able to | |
8146 | // restore it later. | |
30496905 VZ |
8147 | sizeCurrent = -sizeCurrent; |
8148 | ||
8149 | // This is negative which is correct. | |
8150 | return sizeCurrent; | |
8151 | } | |
8152 | else // We're just changing the row/column size. | |
8153 | { | |
8154 | // Here it could have been hidden or not previously. | |
8155 | const int sizeOld = sizeCurrent < 0 ? 0 : sizeCurrent; | |
8156 | ||
8157 | sizeCurrent = sizeNew; | |
8158 | ||
8159 | return sizeCurrent - sizeOld; | |
8160 | } | |
8161 | } | |
8162 | ||
8163 | } // anonymous namespace | |
8164 | ||
f85afd4e MB |
8165 | void wxGrid::SetRowSize( int row, int height ) |
8166 | { | |
30496905 VZ |
8167 | // See comment in SetColSize |
8168 | if ( height > 0 && height < GetRowMinimalAcceptableHeight()) | |
8169 | return; | |
60ff3b99 | 8170 | |
30496905 | 8171 | // The value of -1 is special and means to fit the height to the row label. |
d929eddf VZ |
8172 | // As with the columns, ignore attempts to auto-size the hidden rows. |
8173 | if ( height == -1 && GetRowHeight(row) != 0 ) | |
ad7502d8 SN |
8174 | { |
8175 | long w, h; | |
8176 | wxArrayString lines; | |
8177 | wxClientDC dc(m_rowLabelWin); | |
8178 | dc.SetFont(GetLabelFont()); | |
8179 | StringToLines(GetRowLabelValue( row ), lines); | |
ace8d849 VZ |
8180 | GetTextBoxSize( dc, lines, &w, &h ); |
8181 | //check that it is not less than the minimal height | |
8182 | height = wxMax(h, GetRowMinimalAcceptableHeight()); | |
ad7502d8 SN |
8183 | } |
8184 | ||
30496905 VZ |
8185 | DoSetRowSize(row, height); |
8186 | } | |
8187 | ||
8188 | void wxGrid::DoSetRowSize( int row, int height ) | |
8189 | { | |
8190 | wxCHECK_RET( row >= 0 && row < m_numRows, wxT("invalid row index") ); | |
b8d24d4e | 8191 | |
7c1cb261 VZ |
8192 | if ( m_rowHeights.IsEmpty() ) |
8193 | { | |
8194 | // need to really create the array | |
8195 | InitRowHeights(); | |
8196 | } | |
60ff3b99 | 8197 | |
30496905 VZ |
8198 | const int diff = UpdateRowOrColSize(m_rowHeights[row], height); |
8199 | if ( !diff ) | |
8200 | return; | |
60ff3b99 | 8201 | |
0ed3b812 | 8202 | for ( int i = row; i < m_numRows; i++ ) |
f85afd4e | 8203 | { |
b99be8fb | 8204 | m_rowBottoms[i] += diff; |
f85afd4e | 8205 | } |
2f024384 | 8206 | |
a6b40a9a VZ |
8207 | InvalidateBestSize(); |
8208 | ||
faec5a43 | 8209 | if ( !GetBatchCount() ) |
ce84ef6a | 8210 | { |
faec5a43 | 8211 | CalcDimensions(); |
ce84ef6a VZ |
8212 | Refresh(); |
8213 | } | |
f85afd4e MB |
8214 | } |
8215 | ||
8216 | void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) | |
8217 | { | |
ef316e23 VZ |
8218 | // we dont allow zero default column width |
8219 | m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 ); | |
f85afd4e MB |
8220 | |
8221 | if ( resizeExistingCols ) | |
8222 | { | |
b1da8107 SN |
8223 | // since we are resizing all columns to the default column size, |
8224 | // we can simply clear the col widths and col rights | |
8225 | // arrays (which also allows us to take advantage of | |
8226 | // some speed optimisations) | |
8227 | m_colWidths.Empty(); | |
8228 | m_colRights.Empty(); | |
edb89f7e VZ |
8229 | if ( !GetBatchCount() ) |
8230 | CalcDimensions(); | |
f85afd4e MB |
8231 | } |
8232 | } | |
8233 | ||
8234 | void wxGrid::SetColSize( int col, int width ) | |
8235 | { | |
30496905 VZ |
8236 | // we intentionally don't test whether the width is less than |
8237 | // GetColMinimalWidth() here but we do compare it with | |
8238 | // GetColMinimalAcceptableWidth() as otherwise things currently break (see | |
8239 | // #651) -- and we also always allow the width of 0 as it has the special | |
8240 | // sense of hiding the column | |
8241 | if ( width > 0 && width < GetColMinimalAcceptableWidth() ) | |
8242 | return; | |
60ff3b99 | 8243 | |
30496905 | 8244 | // The value of -1 is special and means to fit the width to the column label. |
d929eddf VZ |
8245 | // |
8246 | // Notice that we currently don't support auto-sizing hidden columns (we | |
8247 | // could, but it's not clear whether this is really needed and it would | |
8248 | // make the code more complex), and for them passing -1 simply means to | |
8249 | // show the column back using its old size. | |
8250 | if ( width == -1 && GetColWidth(col) != 0 ) | |
ad7502d8 SN |
8251 | { |
8252 | long w, h; | |
8253 | wxArrayString lines; | |
ad805b9e | 8254 | wxClientDC dc(m_colWindow); |
ad7502d8 SN |
8255 | dc.SetFont(GetLabelFont()); |
8256 | StringToLines(GetColLabelValue(col), lines); | |
8257 | if ( GetColLabelTextOrientation() == wxHORIZONTAL ) | |
8258 | GetTextBoxSize( dc, lines, &w, &h ); | |
8259 | else | |
8260 | GetTextBoxSize( dc, lines, &h, &w ); | |
8261 | width = w + 6; | |
ace8d849 | 8262 | //check that it is not less than the minimal width |
54181a33 | 8263 | width = wxMax(width, GetColMinimalAcceptableWidth()); |
ad7502d8 SN |
8264 | } |
8265 | ||
30496905 VZ |
8266 | DoSetColSize(col, width); |
8267 | } | |
8268 | ||
8269 | void wxGrid::DoSetColSize( int col, int width ) | |
8270 | { | |
8271 | wxCHECK_RET( col >= 0 && col < m_numCols, wxT("invalid column index") ); | |
3e13956a | 8272 | |
7c1cb261 VZ |
8273 | if ( m_colWidths.IsEmpty() ) |
8274 | { | |
8275 | // need to really create the array | |
8276 | InitColWidths(); | |
8277 | } | |
f85afd4e | 8278 | |
30496905 VZ |
8279 | const int diff = UpdateRowOrColSize(m_colWidths[col], width); |
8280 | if ( !diff ) | |
8281 | return; | |
8282 | ||
009c7216 | 8283 | if ( m_useNativeHeader ) |
3039ade9 | 8284 | GetGridColHeader()->UpdateColumn(col); |
009c7216 | 8285 | //else: will be refreshed when the header is redrawn |
60ff3b99 | 8286 | |
0ed3b812 | 8287 | for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ ) |
f85afd4e | 8288 | { |
0ed3b812 | 8289 | m_colRights[GetColAt(colPos)] += diff; |
f85afd4e | 8290 | } |
962a48f6 | 8291 | |
a6b40a9a VZ |
8292 | InvalidateBestSize(); |
8293 | ||
faec5a43 | 8294 | if ( !GetBatchCount() ) |
ad805b9e | 8295 | { |
faec5a43 | 8296 | CalcDimensions(); |
ad805b9e VZ |
8297 | Refresh(); |
8298 | } | |
f85afd4e MB |
8299 | } |
8300 | ||
43947979 VZ |
8301 | void wxGrid::SetColMinimalWidth( int col, int width ) |
8302 | { | |
4db6714b KH |
8303 | if (width > GetColMinimalAcceptableWidth()) |
8304 | { | |
c6fbe2f0 | 8305 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col; |
8253f2e0 | 8306 | m_colMinWidths[key] = width; |
b8d24d4e | 8307 | } |
af547d51 VZ |
8308 | } |
8309 | ||
8310 | void wxGrid::SetRowMinimalHeight( int row, int width ) | |
8311 | { | |
4db6714b KH |
8312 | if (width > GetRowMinimalAcceptableHeight()) |
8313 | { | |
c6fbe2f0 | 8314 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row; |
8253f2e0 | 8315 | m_rowMinHeights[key] = width; |
b8d24d4e | 8316 | } |
43947979 VZ |
8317 | } |
8318 | ||
8319 | int wxGrid::GetColMinimalWidth(int col) const | |
8320 | { | |
c6fbe2f0 | 8321 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col; |
8253f2e0 | 8322 | wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key); |
962a48f6 | 8323 | |
ba8c1601 | 8324 | return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth; |
af547d51 VZ |
8325 | } |
8326 | ||
8327 | int wxGrid::GetRowMinimalHeight(int row) const | |
8328 | { | |
c6fbe2f0 | 8329 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row; |
8253f2e0 | 8330 | wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key); |
962a48f6 | 8331 | |
ba8c1601 | 8332 | return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight; |
b8d24d4e RG |
8333 | } |
8334 | ||
8335 | void wxGrid::SetColMinimalAcceptableWidth( int width ) | |
8336 | { | |
20c84410 | 8337 | // We do allow a width of 0 since this gives us |
962a48f6 DS |
8338 | // an easy way to temporarily hiding columns. |
8339 | if ( width >= 0 ) | |
8340 | m_minAcceptableColWidth = width; | |
b8d24d4e RG |
8341 | } |
8342 | ||
8343 | void wxGrid::SetRowMinimalAcceptableHeight( int height ) | |
8344 | { | |
20c84410 | 8345 | // We do allow a height of 0 since this gives us |
962a48f6 DS |
8346 | // an easy way to temporarily hiding rows. |
8347 | if ( height >= 0 ) | |
8348 | m_minAcceptableRowHeight = height; | |
17a1ebd1 | 8349 | } |
b8d24d4e RG |
8350 | |
8351 | int wxGrid::GetColMinimalAcceptableWidth() const | |
8352 | { | |
8353 | return m_minAcceptableColWidth; | |
8354 | } | |
8355 | ||
8356 | int wxGrid::GetRowMinimalAcceptableHeight() const | |
8357 | { | |
8358 | return m_minAcceptableRowHeight; | |
43947979 VZ |
8359 | } |
8360 | ||
57c086ef VZ |
8361 | // ---------------------------------------------------------------------------- |
8362 | // auto sizing | |
8363 | // ---------------------------------------------------------------------------- | |
8364 | ||
733f486a VZ |
8365 | void |
8366 | wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction) | |
65e4e78e | 8367 | { |
733f486a VZ |
8368 | const bool column = direction == wxGRID_COLUMN; |
8369 | ||
6922252d VZ |
8370 | // We don't support auto-sizing hidden rows or columns, this doesn't seem |
8371 | // to make much sense. | |
8372 | if ( column ) | |
8373 | { | |
8374 | if ( GetColWidth(colOrRow) == 0 ) | |
8375 | return; | |
8376 | } | |
8377 | else | |
8378 | { | |
8379 | if ( GetRowHeight(colOrRow) == 0 ) | |
8380 | return; | |
8381 | } | |
8382 | ||
65e4e78e VZ |
8383 | wxClientDC dc(m_gridWin); |
8384 | ||
962a48f6 | 8385 | // cancel editing of cell |
13f6e9e8 RG |
8386 | HideCellEditControl(); |
8387 | SaveEditControlValue(); | |
8388 | ||
68abd97d VZ |
8389 | // initialize both of them just to avoid compiler warnings even if only |
8390 | // really needs to be initialized here | |
8391 | int row, | |
8392 | col; | |
8393 | if ( column ) | |
8394 | { | |
8395 | row = -1; | |
8396 | col = colOrRow; | |
8397 | } | |
8398 | else | |
8399 | { | |
8400 | row = colOrRow; | |
a95e38c0 | 8401 | col = -1; |
68abd97d | 8402 | } |
af547d51 VZ |
8403 | |
8404 | wxCoord extent, extentMax = 0; | |
8405 | int max = column ? m_numRows : m_numCols; | |
39bcce60 | 8406 | for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ ) |
65e4e78e | 8407 | { |
af547d51 | 8408 | if ( column ) |
bc7d2e9d | 8409 | { |
af547d51 | 8410 | row = rowOrCol; |
bc7d2e9d VZ |
8411 | col = colOrRow; |
8412 | } | |
6922252d | 8413 | else |
bc7d2e9d VZ |
8414 | { |
8415 | row = colOrRow; | |
af547d51 | 8416 | col = rowOrCol; |
bc7d2e9d | 8417 | } |
774f6b31 VZ |
8418 | |
8419 | // we need to account for the cells spanning multiple columns/rows: | |
8420 | // while they may need a lot of space, they don't need all of it in | |
8421 | // this column/row | |
8422 | int numRows, numCols; | |
8423 | const CellSpan span = GetCellSize(row, col, &numRows, &numCols); | |
8424 | if ( span == CellSpan_Inside ) | |
8425 | { | |
8426 | // we need to get the size of the main cell, not of a cell hidden | |
8427 | // by it | |
8428 | row += numRows; | |
8429 | col += numCols; | |
8430 | ||
8431 | // get the size of the main cell too | |
8432 | GetCellSize(row, col, &numRows, &numCols); | |
8433 | } | |
af547d51 | 8434 | |
bc7d2e9d | 8435 | // get cell ( main cell if CellSpan_Inside ) renderer best size |
2f024384 DS |
8436 | wxGridCellAttr *attr = GetCellAttr(row, col); |
8437 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); | |
65e4e78e VZ |
8438 | if ( renderer ) |
8439 | { | |
af547d51 VZ |
8440 | wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col); |
8441 | extent = column ? size.x : size.y; | |
774f6b31 VZ |
8442 | |
8443 | if ( span != CellSpan_None ) | |
8444 | { | |
8445 | // we spread the size of a spanning cell over all the cells it | |
8446 | // covers evenly -- this is probably not ideal but we can't | |
8447 | // really do much better here | |
8448 | // | |
8449 | // notice that numCols and numRows are never 0 as they | |
8450 | // correspond to the size of the main cell of the span and not | |
8451 | // of the cell inside it | |
8452 | extent /= column ? numCols : numRows; | |
8453 | } | |
8454 | ||
af547d51 | 8455 | if ( extent > extentMax ) |
af547d51 | 8456 | extentMax = extent; |
0b190b0f VZ |
8457 | |
8458 | renderer->DecRef(); | |
65e4e78e VZ |
8459 | } |
8460 | ||
8461 | attr->DecRef(); | |
8462 | } | |
8463 | ||
af547d51 VZ |
8464 | // now also compare with the column label extent |
8465 | wxCoord w, h; | |
65e4e78e | 8466 | dc.SetFont( GetLabelFont() ); |
294d195c MB |
8467 | |
8468 | if ( column ) | |
d43851f7 | 8469 | { |
bc7d2e9d | 8470 | dc.GetMultiLineTextExtent( GetColLabelValue(colOrRow), &w, &h ); |
4db6714b | 8471 | if ( GetColLabelTextOrientation() == wxVERTICAL ) |
d43851f7 JS |
8472 | w = h; |
8473 | } | |
294d195c | 8474 | else |
bc7d2e9d | 8475 | dc.GetMultiLineTextExtent( GetRowLabelValue(colOrRow), &w, &h ); |
294d195c | 8476 | |
af547d51 VZ |
8477 | extent = column ? w : h; |
8478 | if ( extent > extentMax ) | |
af547d51 | 8479 | extentMax = extent; |
65e4e78e | 8480 | |
af547d51 | 8481 | if ( !extentMax ) |
65e4e78e | 8482 | { |
af547d51 | 8483 | // empty column - give default extent (notice that if extentMax is less |
2f024384 | 8484 | // than default extent but != 0, it's OK) |
af547d51 | 8485 | extentMax = column ? m_defaultColWidth : m_defaultRowHeight; |
65e4e78e VZ |
8486 | } |
8487 | else | |
8488 | { | |
a95e38c0 | 8489 | if ( column ) |
a95e38c0 VZ |
8490 | // leave some space around text |
8491 | extentMax += 10; | |
f6bcfd97 | 8492 | else |
f6bcfd97 | 8493 | extentMax += 6; |
65e4e78e VZ |
8494 | } |
8495 | ||
edb89f7e VZ |
8496 | if ( column ) |
8497 | { | |
a01cfc08 VS |
8498 | // Ensure automatic width is not less than minimal width. See the |
8499 | // comment in SetColSize() for explanation of why this isn't done | |
8500 | // in SetColSize(). | |
8501 | if ( !setAsMin ) | |
bc7d2e9d | 8502 | extentMax = wxMax(extentMax, GetColMinimalWidth(colOrRow)); |
a01cfc08 | 8503 | |
bc7d2e9d | 8504 | SetColSize( colOrRow, extentMax ); |
faec5a43 SN |
8505 | if ( !GetBatchCount() ) |
8506 | { | |
ad805b9e VZ |
8507 | if ( m_useNativeHeader ) |
8508 | { | |
bc7d2e9d | 8509 | GetGridColHeader()->UpdateColumn(colOrRow); |
ad805b9e VZ |
8510 | } |
8511 | else | |
8512 | { | |
8513 | int cw, ch, dummy; | |
8514 | m_gridWin->GetClientSize( &cw, &ch ); | |
bc7d2e9d | 8515 | wxRect rect ( CellToRect( 0, colOrRow ) ); |
ad805b9e VZ |
8516 | rect.y = 0; |
8517 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
8518 | rect.width = cw - rect.x; | |
8519 | rect.height = m_colLabelHeight; | |
8520 | GetColLabelWindow()->Refresh( true, &rect ); | |
8521 | } | |
edb89f7e VZ |
8522 | } |
8523 | } | |
8524 | else | |
8525 | { | |
a01cfc08 VS |
8526 | // Ensure automatic width is not less than minimal height. See the |
8527 | // comment in SetColSize() for explanation of why this isn't done | |
8528 | // in SetRowSize(). | |
8529 | if ( !setAsMin ) | |
bc7d2e9d | 8530 | extentMax = wxMax(extentMax, GetRowMinimalHeight(colOrRow)); |
a01cfc08 | 8531 | |
bc7d2e9d | 8532 | SetRowSize(colOrRow, extentMax); |
faec5a43 SN |
8533 | if ( !GetBatchCount() ) |
8534 | { | |
edb89f7e VZ |
8535 | int cw, ch, dummy; |
8536 | m_gridWin->GetClientSize( &cw, &ch ); | |
bc7d2e9d | 8537 | wxRect rect( CellToRect( colOrRow, 0 ) ); |
edb89f7e VZ |
8538 | rect.x = 0; |
8539 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
8540 | rect.width = m_rowLabelWidth; | |
faec5a43 | 8541 | rect.height = ch - rect.y; |
ca65c044 | 8542 | m_rowLabelWin->Refresh( true, &rect ); |
edb89f7e | 8543 | } |
faec5a43 | 8544 | } |
2f024384 | 8545 | |
65e4e78e VZ |
8546 | if ( setAsMin ) |
8547 | { | |
af547d51 | 8548 | if ( column ) |
bc7d2e9d | 8549 | SetColMinimalWidth(colOrRow, extentMax); |
af547d51 | 8550 | else |
bc7d2e9d | 8551 | SetRowMinimalHeight(colOrRow, extentMax); |
65e4e78e VZ |
8552 | } |
8553 | } | |
8554 | ||
733f486a VZ |
8555 | wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction) |
8556 | { | |
8557 | // calculate size for the rows or columns? | |
8558 | const bool calcRows = direction == wxGRID_ROW; | |
8559 | ||
8560 | wxClientDC dc(calcRows ? GetGridRowLabelWindow() | |
8561 | : GetGridColLabelWindow()); | |
8562 | dc.SetFont(GetLabelFont()); | |
8563 | ||
8564 | // which dimension should we take into account for calculations? | |
8565 | // | |
8566 | // for columns, the text can be only horizontal so it's easy but for rows | |
8567 | // we also have to take into account the text orientation | |
8568 | const bool | |
8569 | useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL); | |
8570 | ||
8571 | wxArrayString lines; | |
8572 | wxCoord extentMax = 0; | |
8573 | ||
8574 | const int numRowsOrCols = calcRows ? m_numRows : m_numCols; | |
8575 | for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ ) | |
8576 | { | |
8577 | lines.Clear(); | |
add4bb40 VS |
8578 | |
8579 | wxString label = calcRows ? GetRowLabelValue(rowOrCol) | |
8580 | : GetColLabelValue(rowOrCol); | |
8581 | StringToLines(label, lines); | |
733f486a VZ |
8582 | |
8583 | long w, h; | |
8584 | GetTextBoxSize(dc, lines, &w, &h); | |
8585 | ||
8586 | const wxCoord extent = useWidth ? w : h; | |
8587 | if ( extent > extentMax ) | |
8588 | extentMax = extent; | |
8589 | } | |
8590 | ||
8591 | if ( !extentMax ) | |
8592 | { | |
8593 | // empty column - give default extent (notice that if extentMax is less | |
8594 | // than default extent but != 0, it's OK) | |
8595 | extentMax = calcRows ? GetDefaultRowLabelSize() | |
8596 | : GetDefaultColLabelSize(); | |
8597 | } | |
8598 | ||
8599 | // leave some space around text (taken from AutoSizeColOrRow) | |
8600 | if ( calcRows ) | |
8601 | extentMax += 10; | |
8602 | else | |
8603 | extentMax += 6; | |
8604 | ||
8605 | return extentMax; | |
8606 | } | |
8607 | ||
266e8367 | 8608 | int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) |
65e4e78e | 8609 | { |
57c086ef VZ |
8610 | int width = m_rowLabelWidth; |
8611 | ||
b62f94ff VZ |
8612 | wxGridUpdateLocker locker; |
8613 | if(!calcOnly) | |
8614 | locker.Create(this); | |
97a9929e | 8615 | |
65e4e78e VZ |
8616 | for ( int col = 0; col < m_numCols; col++ ) |
8617 | { | |
266e8367 | 8618 | if ( !calcOnly ) |
266e8367 | 8619 | AutoSizeColumn(col, setAsMin); |
57c086ef VZ |
8620 | |
8621 | width += GetColWidth(col); | |
65e4e78e | 8622 | } |
97a9929e | 8623 | |
266e8367 | 8624 | return width; |
65e4e78e VZ |
8625 | } |
8626 | ||
266e8367 | 8627 | int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin) |
57c086ef VZ |
8628 | { |
8629 | int height = m_colLabelHeight; | |
8630 | ||
b62f94ff VZ |
8631 | wxGridUpdateLocker locker; |
8632 | if(!calcOnly) | |
8633 | locker.Create(this); | |
97a9929e | 8634 | |
57c086ef VZ |
8635 | for ( int row = 0; row < m_numRows; row++ ) |
8636 | { | |
af547d51 | 8637 | if ( !calcOnly ) |
af547d51 | 8638 | AutoSizeRow(row, setAsMin); |
57c086ef VZ |
8639 | |
8640 | height += GetRowHeight(row); | |
8641 | } | |
97a9929e | 8642 | |
266e8367 | 8643 | return height; |
57c086ef VZ |
8644 | } |
8645 | ||
8646 | void wxGrid::AutoSize() | |
8647 | { | |
b62f94ff | 8648 | wxGridUpdateLocker locker(this); |
97a9929e | 8649 | |
0ed3b812 VZ |
8650 | wxSize size(SetOrCalcColumnSizes(false) - m_rowLabelWidth + m_extraWidth, |
8651 | SetOrCalcRowSizes(false) - m_colLabelHeight + m_extraHeight); | |
97a9929e | 8652 | |
0ed3b812 VZ |
8653 | // we know that we're not going to have scrollbars so disable them now to |
8654 | // avoid trouble in SetClientSize() which can otherwise set the correct | |
8655 | // client size but also leave space for (not needed any more) scrollbars | |
fd76c6a7 VZ |
8656 | SetScrollbars(m_xScrollPixelsPerLine, m_yScrollPixelsPerLine, |
8657 | 0, 0, 0, 0, true); | |
72b0a1de VZ |
8658 | |
8659 | SetClientSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight); | |
266e8367 VZ |
8660 | } |
8661 | ||
d43851f7 JS |
8662 | void wxGrid::AutoSizeRowLabelSize( int row ) |
8663 | { | |
d43851f7 | 8664 | // Hide the edit control, so it |
4db6714b KH |
8665 | // won't interfere with drag-shrinking. |
8666 | if ( IsCellEditControlShown() ) | |
d43851f7 JS |
8667 | { |
8668 | HideCellEditControl(); | |
8669 | SaveEditControlValue(); | |
8670 | } | |
8671 | ||
8672 | // autosize row height depending on label text | |
ad7502d8 | 8673 | SetRowSize(row, -1); |
30496905 | 8674 | |
d43851f7 JS |
8675 | ForceRefresh(); |
8676 | } | |
8677 | ||
8678 | void wxGrid::AutoSizeColLabelSize( int col ) | |
8679 | { | |
d43851f7 | 8680 | // Hide the edit control, so it |
c2f5b920 | 8681 | // won't interfere with drag-shrinking. |
4db6714b | 8682 | if ( IsCellEditControlShown() ) |
d43851f7 JS |
8683 | { |
8684 | HideCellEditControl(); | |
8685 | SaveEditControlValue(); | |
8686 | } | |
8687 | ||
8688 | // autosize column width depending on label text | |
ad7502d8 | 8689 | SetColSize(col, -1); |
30496905 | 8690 | |
d43851f7 JS |
8691 | ForceRefresh(); |
8692 | } | |
8693 | ||
266e8367 VZ |
8694 | wxSize wxGrid::DoGetBestSize() const |
8695 | { | |
f48a1159 | 8696 | wxGrid * const self = const_cast<wxGrid *>(this); |
266e8367 | 8697 | |
dc4689ef VZ |
8698 | // we do the same as in AutoSize() here with the exception that we don't |
8699 | // change the column/row sizes, only calculate them | |
8700 | wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth, | |
8701 | self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight); | |
962a48f6 | 8702 | |
72b0a1de | 8703 | return wxSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight) |
dc4689ef | 8704 | + GetWindowBorderSize(); |
266e8367 VZ |
8705 | } |
8706 | ||
8707 | void wxGrid::Fit() | |
8708 | { | |
8709 | AutoSize(); | |
57c086ef VZ |
8710 | } |
8711 | ||
7bc57fd9 | 8712 | #if WXWIN_COMPATIBILITY_2_8 |
6fc0f38f SN |
8713 | wxPen& wxGrid::GetDividerPen() const |
8714 | { | |
8715 | return wxNullPen; | |
8716 | } | |
7bc57fd9 | 8717 | #endif // WXWIN_COMPATIBILITY_2_8 |
6fc0f38f | 8718 | |
57c086ef VZ |
8719 | // ---------------------------------------------------------------------------- |
8720 | // cell value accessor functions | |
8721 | // ---------------------------------------------------------------------------- | |
f85afd4e MB |
8722 | |
8723 | void wxGrid::SetCellValue( int row, int col, const wxString& s ) | |
8724 | { | |
8725 | if ( m_table ) | |
8726 | { | |
f6bcfd97 | 8727 | m_table->SetValue( row, col, s ); |
2d66e025 MB |
8728 | if ( !GetBatchCount() ) |
8729 | { | |
27f35b66 SN |
8730 | int dummy; |
8731 | wxRect rect( CellToRect( row, col ) ); | |
8732 | rect.x = 0; | |
8733 | rect.width = m_gridWin->GetClientSize().GetWidth(); | |
8734 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
ca65c044 | 8735 | m_gridWin->Refresh( false, &rect ); |
2d66e025 | 8736 | } |
60ff3b99 | 8737 | |
f85afd4e | 8738 | if ( m_currentCellCoords.GetRow() == row && |
4cfa5de6 | 8739 | m_currentCellCoords.GetCol() == col && |
f6bcfd97 BP |
8740 | IsCellEditControlShown()) |
8741 | // Note: If we are using IsCellEditControlEnabled, | |
8742 | // this interacts badly with calling SetCellValue from | |
8743 | // an EVT_GRID_CELL_CHANGE handler. | |
f85afd4e | 8744 | { |
4cfa5de6 RD |
8745 | HideCellEditControl(); |
8746 | ShowCellEditControl(); // will reread data from table | |
f85afd4e | 8747 | } |
f85afd4e MB |
8748 | } |
8749 | } | |
8750 | ||
962a48f6 | 8751 | // ---------------------------------------------------------------------------- |
2f024384 | 8752 | // block, row and column selection |
962a48f6 | 8753 | // ---------------------------------------------------------------------------- |
f85afd4e MB |
8754 | |
8755 | void wxGrid::SelectRow( int row, bool addToSelected ) | |
8756 | { | |
8b5f6d9d VZ |
8757 | if ( !m_selection ) |
8758 | return; | |
8759 | ||
8760 | if ( !addToSelected ) | |
e32352cf | 8761 | ClearSelection(); |
70c7a608 | 8762 | |
8b5f6d9d | 8763 | m_selection->SelectRow(row); |
f85afd4e MB |
8764 | } |
8765 | ||
f85afd4e MB |
8766 | void wxGrid::SelectCol( int col, bool addToSelected ) |
8767 | { | |
8b5f6d9d VZ |
8768 | if ( !m_selection ) |
8769 | return; | |
8770 | ||
8771 | if ( !addToSelected ) | |
e32352cf | 8772 | ClearSelection(); |
f85afd4e | 8773 | |
8b5f6d9d | 8774 | m_selection->SelectCol(col); |
f85afd4e MB |
8775 | } |
8776 | ||
8b5f6d9d VZ |
8777 | void wxGrid::SelectBlock(int topRow, int leftCol, int bottomRow, int rightCol, |
8778 | bool addToSelected) | |
f85afd4e | 8779 | { |
8b5f6d9d VZ |
8780 | if ( !m_selection ) |
8781 | return; | |
8782 | ||
8783 | if ( !addToSelected ) | |
c9097836 | 8784 | ClearSelection(); |
f85afd4e | 8785 | |
8b5f6d9d | 8786 | m_selection->SelectBlock(topRow, leftCol, bottomRow, rightCol); |
f85afd4e MB |
8787 | } |
8788 | ||
8789 | void wxGrid::SelectAll() | |
8790 | { | |
f74d0b57 | 8791 | if ( m_numRows > 0 && m_numCols > 0 ) |
3f3dc2ef VZ |
8792 | { |
8793 | if ( m_selection ) | |
ccdee36f | 8794 | m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 ); |
3f3dc2ef | 8795 | } |
b5808881 | 8796 | } |
f85afd4e | 8797 | |
962a48f6 DS |
8798 | // ---------------------------------------------------------------------------- |
8799 | // cell, row and col deselection | |
8800 | // ---------------------------------------------------------------------------- | |
f7b4b343 | 8801 | |
bec70262 | 8802 | void wxGrid::DeselectLine(int line, const wxGridOperations& oper) |
f7b4b343 | 8803 | { |
3f3dc2ef VZ |
8804 | if ( !m_selection ) |
8805 | return; | |
8806 | ||
bec70262 | 8807 | const wxGridSelectionModes mode = m_selection->GetSelectionMode(); |
475be9ce VZ |
8808 | if ( mode == oper.GetSelectionMode() || |
8809 | mode == wxGrid::wxGridSelectRowsOrColumns ) | |
f7b4b343 | 8810 | { |
bec70262 VZ |
8811 | const wxGridCellCoords c(oper.MakeCoords(line, 0)); |
8812 | if ( m_selection->IsInSelection(c) ) | |
8813 | m_selection->ToggleCellSelection(c); | |
ffdd3c98 | 8814 | } |
bec70262 | 8815 | else if ( mode != oper.Dual().GetSelectionMode() ) |
f7b4b343 | 8816 | { |
bec70262 VZ |
8817 | const int nOther = oper.Dual().GetNumberOfLines(this); |
8818 | for ( int i = 0; i < nOther; i++ ) | |
f7b4b343 | 8819 | { |
bec70262 VZ |
8820 | const wxGridCellCoords c(oper.MakeCoords(line, i)); |
8821 | if ( m_selection->IsInSelection(c) ) | |
8822 | m_selection->ToggleCellSelection(c); | |
f7b4b343 VZ |
8823 | } |
8824 | } | |
bec70262 VZ |
8825 | //else: can only select orthogonal lines so no lines in this direction |
8826 | // could have been selected anyhow | |
f7b4b343 VZ |
8827 | } |
8828 | ||
bec70262 | 8829 | void wxGrid::DeselectRow(int row) |
f7b4b343 | 8830 | { |
bec70262 VZ |
8831 | DeselectLine(row, wxGridRowOperations()); |
8832 | } | |
3f3dc2ef | 8833 | |
bec70262 VZ |
8834 | void wxGrid::DeselectCol(int col) |
8835 | { | |
8836 | DeselectLine(col, wxGridColumnOperations()); | |
f7b4b343 VZ |
8837 | } |
8838 | ||
8839 | void wxGrid::DeselectCell( int row, int col ) | |
8840 | { | |
3f3dc2ef | 8841 | if ( m_selection && m_selection->IsInSelection(row, col) ) |
f7b4b343 VZ |
8842 | m_selection->ToggleCellSelection(row, col); |
8843 | } | |
8844 | ||
ef316e23 | 8845 | bool wxGrid::IsSelection() const |
b5808881 | 8846 | { |
3f3dc2ef | 8847 | return ( m_selection && (m_selection->IsSelection() || |
8a3e536c VZ |
8848 | ( m_selectedBlockTopLeft != wxGridNoCellCoords && |
8849 | m_selectedBlockBottomRight != wxGridNoCellCoords) ) ); | |
f85afd4e MB |
8850 | } |
8851 | ||
84035150 | 8852 | bool wxGrid::IsInSelection( int row, int col ) const |
b5808881 | 8853 | { |
3f3dc2ef | 8854 | return ( m_selection && (m_selection->IsInSelection( row, col ) || |
8a3e536c VZ |
8855 | ( row >= m_selectedBlockTopLeft.GetRow() && |
8856 | col >= m_selectedBlockTopLeft.GetCol() && | |
8857 | row <= m_selectedBlockBottomRight.GetRow() && | |
8858 | col <= m_selectedBlockBottomRight.GetCol() )) ); | |
b5808881 | 8859 | } |
f85afd4e | 8860 | |
aa5b8857 SN |
8861 | wxGridCellCoordsArray wxGrid::GetSelectedCells() const |
8862 | { | |
4db6714b KH |
8863 | if (!m_selection) |
8864 | { | |
8865 | wxGridCellCoordsArray a; | |
8866 | return a; | |
8867 | } | |
8868 | ||
aa5b8857 SN |
8869 | return m_selection->m_cellSelection; |
8870 | } | |
4db6714b | 8871 | |
aa5b8857 SN |
8872 | wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const |
8873 | { | |
4db6714b KH |
8874 | if (!m_selection) |
8875 | { | |
8876 | wxGridCellCoordsArray a; | |
8877 | return a; | |
8878 | } | |
8879 | ||
aa5b8857 SN |
8880 | return m_selection->m_blockSelectionTopLeft; |
8881 | } | |
4db6714b | 8882 | |
aa5b8857 SN |
8883 | wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const |
8884 | { | |
4db6714b KH |
8885 | if (!m_selection) |
8886 | { | |
8887 | wxGridCellCoordsArray a; | |
8888 | return a; | |
8889 | } | |
8890 | ||
a8de8190 | 8891 | return m_selection->m_blockSelectionBottomRight; |
aa5b8857 | 8892 | } |
4db6714b | 8893 | |
aa5b8857 SN |
8894 | wxArrayInt wxGrid::GetSelectedRows() const |
8895 | { | |
4db6714b KH |
8896 | if (!m_selection) |
8897 | { | |
8898 | wxArrayInt a; | |
8899 | return a; | |
8900 | } | |
8901 | ||
aa5b8857 SN |
8902 | return m_selection->m_rowSelection; |
8903 | } | |
4db6714b | 8904 | |
aa5b8857 SN |
8905 | wxArrayInt wxGrid::GetSelectedCols() const |
8906 | { | |
4db6714b KH |
8907 | if (!m_selection) |
8908 | { | |
8909 | wxArrayInt a; | |
8910 | return a; | |
8911 | } | |
8912 | ||
aa5b8857 SN |
8913 | return m_selection->m_colSelection; |
8914 | } | |
8915 | ||
f85afd4e MB |
8916 | void wxGrid::ClearSelection() |
8917 | { | |
8a3e536c VZ |
8918 | wxRect r1 = BlockToDeviceRect(m_selectedBlockTopLeft, |
8919 | m_selectedBlockBottomRight); | |
8920 | wxRect r2 = BlockToDeviceRect(m_currentCellCoords, | |
8921 | m_selectedBlockCorner); | |
8922 | ||
8923 | m_selectedBlockTopLeft = | |
8924 | m_selectedBlockBottomRight = | |
8925 | m_selectedBlockCorner = wxGridNoCellCoords; | |
8926 | ||
efc49a21 VZ |
8927 | if ( !r1.IsEmpty() ) |
8928 | RefreshRect(r1, false); | |
8929 | if ( !r2.IsEmpty() ) | |
8930 | RefreshRect(r2, false); | |
8a3e536c | 8931 | |
3f3dc2ef VZ |
8932 | if ( m_selection ) |
8933 | m_selection->ClearSelection(); | |
8f177c8e | 8934 | } |
f85afd4e | 8935 | |
da6af900 | 8936 | // This function returns the rectangle that encloses the given block |
2d66e025 MB |
8937 | // in device coords clipped to the client size of the grid window. |
8938 | // | |
731330ec VZ |
8939 | wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords& topLeft, |
8940 | const wxGridCellCoords& bottomRight ) const | |
f85afd4e | 8941 | { |
731330ec VZ |
8942 | wxRect resultRect; |
8943 | wxRect tempCellRect = CellToRect(topLeft); | |
8944 | if ( tempCellRect != wxGridNoCellRect ) | |
f85afd4e | 8945 | { |
731330ec | 8946 | resultRect = tempCellRect; |
58dd5b3b MB |
8947 | } |
8948 | else | |
8949 | { | |
731330ec | 8950 | resultRect = wxRect(0, 0, 0, 0); |
58dd5b3b | 8951 | } |
60ff3b99 | 8952 | |
731330ec VZ |
8953 | tempCellRect = CellToRect(bottomRight); |
8954 | if ( tempCellRect != wxGridNoCellRect ) | |
58dd5b3b | 8955 | { |
731330ec | 8956 | resultRect += tempCellRect; |
2d66e025 MB |
8957 | } |
8958 | else | |
8959 | { | |
731330ec | 8960 | // If both inputs were "wxGridNoCellRect," then there's nothing to do. |
2d66e025 | 8961 | return wxGridNoCellRect; |
f85afd4e MB |
8962 | } |
8963 | ||
731330ec VZ |
8964 | // Ensure that left/right and top/bottom pairs are in order. |
8965 | int left = resultRect.GetLeft(); | |
8966 | int top = resultRect.GetTop(); | |
8967 | int right = resultRect.GetRight(); | |
8968 | int bottom = resultRect.GetBottom(); | |
27f35b66 SN |
8969 | |
8970 | int leftCol = topLeft.GetCol(); | |
8971 | int topRow = topLeft.GetRow(); | |
8972 | int rightCol = bottomRight.GetCol(); | |
8973 | int bottomRow = bottomRight.GetRow(); | |
8974 | ||
3ed884a0 SN |
8975 | if (left > right) |
8976 | { | |
731330ec | 8977 | int tmp = left; |
3ed884a0 | 8978 | left = right; |
731330ec VZ |
8979 | right = tmp; |
8980 | ||
8981 | tmp = leftCol; | |
4db6714b | 8982 | leftCol = rightCol; |
731330ec | 8983 | rightCol = tmp; |
3ed884a0 SN |
8984 | } |
8985 | ||
8986 | if (top > bottom) | |
8987 | { | |
731330ec | 8988 | int tmp = top; |
3ed884a0 | 8989 | top = bottom; |
731330ec VZ |
8990 | bottom = tmp; |
8991 | ||
8992 | tmp = topRow; | |
3ed884a0 | 8993 | topRow = bottomRow; |
731330ec | 8994 | bottomRow = tmp; |
3ed884a0 SN |
8995 | } |
8996 | ||
731330ec VZ |
8997 | // The following loop is ONLY necessary to detect and handle merged cells. |
8998 | int cw, ch; | |
8999 | m_gridWin->GetClientSize( &cw, &ch ); | |
9000 | ||
9001 | // Get the origin coordinates: notice that they will be negative if the | |
9002 | // grid is scrolled downwards/to the right. | |
9003 | int gridOriginX = 0; | |
9004 | int gridOriginY = 0; | |
9005 | CalcScrolledPosition(gridOriginX, gridOriginY, &gridOriginX, &gridOriginY); | |
9006 | ||
9007 | int onScreenLeftmostCol = internalXToCol(-gridOriginX); | |
9008 | int onScreenUppermostRow = internalYToRow(-gridOriginY); | |
9009 | ||
9010 | int onScreenRightmostCol = internalXToCol(-gridOriginX + cw); | |
9011 | int onScreenBottommostRow = internalYToRow(-gridOriginY + ch); | |
9012 | ||
9013 | // Bound our loop so that we only examine the portion of the selected block | |
9014 | // that is shown on screen. Therefore, we compare the Top-Left block values | |
9015 | // to the Top-Left screen values, and the Bottom-Right block values to the | |
9016 | // Bottom-Right screen values, choosing appropriately. | |
9017 | const int visibleTopRow = wxMax(topRow, onScreenUppermostRow); | |
9018 | const int visibleBottomRow = wxMin(bottomRow, onScreenBottommostRow); | |
9019 | const int visibleLeftCol = wxMax(leftCol, onScreenLeftmostCol); | |
9020 | const int visibleRightCol = wxMin(rightCol, onScreenRightmostCol); | |
9021 | ||
9022 | for ( int j = visibleTopRow; j <= visibleBottomRow; j++ ) | |
27f35b66 | 9023 | { |
731330ec | 9024 | for ( int i = visibleLeftCol; i <= visibleRightCol; i++ ) |
27f35b66 | 9025 | { |
731330ec VZ |
9026 | if ( (j == visibleTopRow) || (j == visibleBottomRow) || |
9027 | (i == visibleLeftCol) || (i == visibleRightCol) ) | |
27f35b66 | 9028 | { |
731330ec | 9029 | tempCellRect = CellToRect( j, i ); |
27f35b66 | 9030 | |
731330ec VZ |
9031 | if (tempCellRect.x < left) |
9032 | left = tempCellRect.x; | |
9033 | if (tempCellRect.y < top) | |
9034 | top = tempCellRect.y; | |
9035 | if (tempCellRect.x + tempCellRect.width > right) | |
9036 | right = tempCellRect.x + tempCellRect.width; | |
9037 | if (tempCellRect.y + tempCellRect.height > bottom) | |
9038 | bottom = tempCellRect.y + tempCellRect.height; | |
27f35b66 | 9039 | } |
4db6714b KH |
9040 | else |
9041 | { | |
731330ec | 9042 | i = visibleRightCol; // jump over inner cells. |
4db6714b | 9043 | } |
27f35b66 SN |
9044 | } |
9045 | } | |
9046 | ||
731330ec | 9047 | // Convert to scrolled coords |
27f35b66 SN |
9048 | CalcScrolledPosition( left, top, &left, &top ); |
9049 | CalcScrolledPosition( right, bottom, &right, &bottom ); | |
58dd5b3b | 9050 | |
f6bcfd97 | 9051 | if (right < 0 || bottom < 0 || left > cw || top > ch) |
c47addef | 9052 | return wxRect(0,0,0,0); |
f6bcfd97 | 9053 | |
731330ec VZ |
9054 | resultRect.SetLeft( wxMax(0, left) ); |
9055 | resultRect.SetTop( wxMax(0, top) ); | |
9056 | resultRect.SetRight( wxMin(cw, right) ); | |
9057 | resultRect.SetBottom( wxMin(ch, bottom) ); | |
58dd5b3b | 9058 | |
731330ec | 9059 | return resultRect; |
f85afd4e MB |
9060 | } |
9061 | ||
574e1c5a VZ |
9062 | void wxGrid::DoSetSizes(const wxGridSizesInfo& sizeInfo, |
9063 | const wxGridOperations& oper) | |
9064 | { | |
9065 | BeginBatch(); | |
9066 | oper.SetDefaultLineSize(this, sizeInfo.m_sizeDefault, true); | |
9067 | const int numLines = oper.GetNumberOfLines(this); | |
9068 | for ( int i = 0; i < numLines; i++ ) | |
9069 | { | |
9070 | int size = sizeInfo.GetSize(i); | |
9071 | if ( size != sizeInfo.m_sizeDefault) | |
9072 | oper.SetLineSize(this, i, size); | |
9073 | } | |
9074 | EndBatch(); | |
9075 | } | |
9076 | ||
9077 | void wxGrid::SetColSizes(const wxGridSizesInfo& sizeInfo) | |
9078 | { | |
9079 | DoSetSizes(sizeInfo, wxGridColumnOperations()); | |
9080 | } | |
9081 | ||
9082 | void wxGrid::SetRowSizes(const wxGridSizesInfo& sizeInfo) | |
9083 | { | |
9084 | DoSetSizes(sizeInfo, wxGridRowOperations()); | |
9085 | } | |
9086 | ||
9087 | wxGridSizesInfo::wxGridSizesInfo(int defSize, const wxArrayInt& allSizes) | |
9088 | { | |
9089 | m_sizeDefault = defSize; | |
9090 | for ( size_t i = 0; i < allSizes.size(); i++ ) | |
9091 | { | |
9092 | if ( allSizes[i] != defSize ) | |
9093 | m_customSizes[i] = allSizes[i]; | |
9094 | } | |
9095 | } | |
9096 | ||
9097 | int wxGridSizesInfo::GetSize(unsigned pos) const | |
9098 | { | |
9099 | wxUnsignedToIntHashMap::const_iterator it = m_customSizes.find(pos); | |
9100 | ||
30496905 VZ |
9101 | // if it's not found return the default |
9102 | if ( it == m_customSizes.end() ) | |
9103 | return m_sizeDefault; | |
9104 | ||
9105 | // otherwise return 0 if it's hidden, currently there is no way to get | |
9106 | // its size before it had been hidden | |
9107 | if ( it->second < 0 ) | |
9108 | return 0; | |
9109 | ||
9110 | return it->second; | |
574e1c5a VZ |
9111 | } |
9112 | ||
1edce33f VZ |
9113 | // ---------------------------------------------------------------------------- |
9114 | // drop target | |
9115 | // ---------------------------------------------------------------------------- | |
9116 | ||
9117 | #if wxUSE_DRAG_AND_DROP | |
9118 | ||
9119 | // this allow setting drop target directly on wxGrid | |
9120 | void wxGrid::SetDropTarget(wxDropTarget *dropTarget) | |
9121 | { | |
9122 | GetGridWindow()->SetDropTarget(dropTarget); | |
9123 | } | |
9124 | ||
9125 | #endif // wxUSE_DRAG_AND_DROP | |
9126 | ||
962a48f6 DS |
9127 | // ---------------------------------------------------------------------------- |
9128 | // grid event classes | |
9129 | // ---------------------------------------------------------------------------- | |
f85afd4e | 9130 | |
bf7945ce | 9131 | IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent ) |
f85afd4e MB |
9132 | |
9133 | wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, | |
5c8fc7c1 | 9134 | int row, int col, int x, int y, bool sel, |
f85afd4e | 9135 | bool control, bool shift, bool alt, bool meta ) |
8b5f6d9d VZ |
9136 | : wxNotifyEvent( type, id ), |
9137 | wxKeyboardState(control, shift, alt, meta) | |
f85afd4e | 9138 | { |
8b5f6d9d | 9139 | Init(row, col, x, y, sel); |
8f177c8e | 9140 | |
f85afd4e MB |
9141 | SetEventObject(obj); |
9142 | } | |
9143 | ||
bf7945ce | 9144 | IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent ) |
f85afd4e MB |
9145 | |
9146 | wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj, | |
9147 | int rowOrCol, int x, int y, | |
9148 | bool control, bool shift, bool alt, bool meta ) | |
8b5f6d9d VZ |
9149 | : wxNotifyEvent( type, id ), |
9150 | wxKeyboardState(control, shift, alt, meta) | |
f85afd4e | 9151 | { |
8b5f6d9d | 9152 | Init(rowOrCol, x, y); |
8f177c8e | 9153 | |
f85afd4e MB |
9154 | SetEventObject(obj); |
9155 | } | |
9156 | ||
9157 | ||
bf7945ce | 9158 | IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent ) |
f85afd4e MB |
9159 | |
9160 | wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, | |
8f177c8e VZ |
9161 | const wxGridCellCoords& topLeft, |
9162 | const wxGridCellCoords& bottomRight, | |
5c8fc7c1 SN |
9163 | bool sel, bool control, |
9164 | bool shift, bool alt, bool meta ) | |
8b5f6d9d VZ |
9165 | : wxNotifyEvent( type, id ), |
9166 | wxKeyboardState(control, shift, alt, meta) | |
9167 | { | |
9168 | Init(topLeft, bottomRight, sel); | |
f85afd4e MB |
9169 | |
9170 | SetEventObject(obj); | |
9171 | } | |
9172 | ||
9173 | ||
bf7945ce RD |
9174 | IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent) |
9175 | ||
9176 | wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type, | |
9177 | wxObject* obj, int row, | |
9178 | int col, wxControl* ctrl) | |
9179 | : wxCommandEvent(type, id) | |
9180 | { | |
9181 | SetEventObject(obj); | |
9182 | m_row = row; | |
9183 | m_col = col; | |
9184 | m_ctrl = ctrl; | |
9185 | } | |
9186 | ||
29efc6e4 FM |
9187 | |
9188 | // ---------------------------------------------------------------------------- | |
9189 | // wxGridTypeRegistry | |
9190 | // ---------------------------------------------------------------------------- | |
9191 | ||
9192 | wxGridTypeRegistry::~wxGridTypeRegistry() | |
9193 | { | |
9194 | size_t count = m_typeinfo.GetCount(); | |
9195 | for ( size_t i = 0; i < count; i++ ) | |
9196 | delete m_typeinfo[i]; | |
9197 | } | |
9198 | ||
9199 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, | |
9200 | wxGridCellRenderer* renderer, | |
9201 | wxGridCellEditor* editor) | |
9202 | { | |
9203 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); | |
9204 | ||
9205 | // is it already registered? | |
9206 | int loc = FindRegisteredDataType(typeName); | |
9207 | if ( loc != wxNOT_FOUND ) | |
9208 | { | |
9209 | delete m_typeinfo[loc]; | |
9210 | m_typeinfo[loc] = info; | |
9211 | } | |
9212 | else | |
9213 | { | |
9214 | m_typeinfo.Add(info); | |
9215 | } | |
9216 | } | |
9217 | ||
9218 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) | |
9219 | { | |
9220 | size_t count = m_typeinfo.GetCount(); | |
9221 | for ( size_t i = 0; i < count; i++ ) | |
9222 | { | |
9223 | if ( typeName == m_typeinfo[i]->m_typeName ) | |
9224 | { | |
9225 | return i; | |
9226 | } | |
9227 | } | |
9228 | ||
9229 | return wxNOT_FOUND; | |
9230 | } | |
9231 | ||
9232 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) | |
9233 | { | |
9234 | int index = FindRegisteredDataType(typeName); | |
9235 | if ( index == wxNOT_FOUND ) | |
9236 | { | |
9237 | // check whether this is one of the standard ones, in which case | |
9238 | // register it "on the fly" | |
9239 | #if wxUSE_TEXTCTRL | |
9240 | if ( typeName == wxGRID_VALUE_STRING ) | |
9241 | { | |
9242 | RegisterDataType(wxGRID_VALUE_STRING, | |
9243 | new wxGridCellStringRenderer, | |
9244 | new wxGridCellTextEditor); | |
9245 | } | |
9246 | else | |
9247 | #endif // wxUSE_TEXTCTRL | |
9248 | #if wxUSE_CHECKBOX | |
9249 | if ( typeName == wxGRID_VALUE_BOOL ) | |
9250 | { | |
9251 | RegisterDataType(wxGRID_VALUE_BOOL, | |
9252 | new wxGridCellBoolRenderer, | |
9253 | new wxGridCellBoolEditor); | |
9254 | } | |
9255 | else | |
9256 | #endif // wxUSE_CHECKBOX | |
9257 | #if wxUSE_TEXTCTRL | |
9258 | if ( typeName == wxGRID_VALUE_NUMBER ) | |
9259 | { | |
9260 | RegisterDataType(wxGRID_VALUE_NUMBER, | |
9261 | new wxGridCellNumberRenderer, | |
9262 | new wxGridCellNumberEditor); | |
9263 | } | |
9264 | else if ( typeName == wxGRID_VALUE_FLOAT ) | |
9265 | { | |
9266 | RegisterDataType(wxGRID_VALUE_FLOAT, | |
9267 | new wxGridCellFloatRenderer, | |
9268 | new wxGridCellFloatEditor); | |
9269 | } | |
9270 | else | |
9271 | #endif // wxUSE_TEXTCTRL | |
9272 | #if wxUSE_COMBOBOX | |
9273 | if ( typeName == wxGRID_VALUE_CHOICE ) | |
9274 | { | |
9275 | RegisterDataType(wxGRID_VALUE_CHOICE, | |
9276 | new wxGridCellStringRenderer, | |
9277 | new wxGridCellChoiceEditor); | |
9278 | } | |
9279 | else | |
9280 | #endif // wxUSE_COMBOBOX | |
9281 | { | |
9282 | return wxNOT_FOUND; | |
9283 | } | |
9284 | ||
9285 | // we get here only if just added the entry for this type, so return | |
9286 | // the last index | |
9287 | index = m_typeinfo.GetCount() - 1; | |
9288 | } | |
9289 | ||
9290 | return index; | |
9291 | } | |
9292 | ||
9293 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) | |
9294 | { | |
9295 | int index = FindDataType(typeName); | |
9296 | if ( index == wxNOT_FOUND ) | |
9297 | { | |
9298 | // the first part of the typename is the "real" type, anything after ':' | |
9299 | // are the parameters for the renderer | |
9a83f860 | 9300 | index = FindDataType(typeName.BeforeFirst(wxT(':'))); |
29efc6e4 FM |
9301 | if ( index == wxNOT_FOUND ) |
9302 | { | |
9303 | return wxNOT_FOUND; | |
9304 | } | |
9305 | ||
9306 | wxGridCellRenderer *renderer = GetRenderer(index); | |
9307 | wxGridCellRenderer *rendererOld = renderer; | |
9308 | renderer = renderer->Clone(); | |
9309 | rendererOld->DecRef(); | |
9310 | ||
9311 | wxGridCellEditor *editor = GetEditor(index); | |
9312 | wxGridCellEditor *editorOld = editor; | |
9313 | editor = editor->Clone(); | |
9314 | editorOld->DecRef(); | |
9315 | ||
9316 | // do it even if there are no parameters to reset them to defaults | |
9a83f860 | 9317 | wxString params = typeName.AfterFirst(wxT(':')); |
29efc6e4 FM |
9318 | renderer->SetParameters(params); |
9319 | editor->SetParameters(params); | |
9320 | ||
9321 | // register the new typename | |
9322 | RegisterDataType(typeName, renderer, editor); | |
9323 | ||
9324 | // we just registered it, it's the last one | |
9325 | index = m_typeinfo.GetCount() - 1; | |
9326 | } | |
9327 | ||
9328 | return index; | |
9329 | } | |
9330 | ||
9331 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) | |
9332 | { | |
9333 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; | |
9334 | if (renderer) | |
9335 | renderer->IncRef(); | |
9336 | ||
9337 | return renderer; | |
9338 | } | |
9339 | ||
9340 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) | |
9341 | { | |
9342 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; | |
9343 | if (editor) | |
9344 | editor->IncRef(); | |
9345 | ||
9346 | return editor; | |
9347 | } | |
9348 | ||
27b92ca4 | 9349 | #endif // wxUSE_GRID |