]>
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 | 6 | // Created: 1/08/1999 |
f85afd4e | 7 | // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au) |
65571936 | 8 | // Licence: wxWindows licence |
f85afd4e MB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
bec70262 VZ |
11 | /* |
12 | TODO: | |
13 | ||
14 | - Replace use of wxINVERT with wxOverlay | |
15 | - Make Begin/EndBatch() the same as the generic Freeze/Thaw() | |
10a4531d VZ |
16 | - Review the column reordering code, it's a mess. |
17 | - Implement row reordering after dealing with the columns. | |
bec70262 VZ |
18 | */ |
19 | ||
95427194 | 20 | // For compilers that support precompilation, includes "wx/wx.h". |
4d85bcd1 JS |
21 | #include "wx/wxprec.h" |
22 | ||
f85afd4e MB |
23 | #ifdef __BORLANDC__ |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27b92ca4 VZ |
27 | #if wxUSE_GRID |
28 | ||
4c44eb66 PC |
29 | #include "wx/grid.h" |
30 | ||
f85afd4e MB |
31 | #ifndef WX_PRECOMP |
32 | #include "wx/utils.h" | |
33 | #include "wx/dcclient.h" | |
34 | #include "wx/settings.h" | |
35 | #include "wx/log.h" | |
508011ce VZ |
36 | #include "wx/textctrl.h" |
37 | #include "wx/checkbox.h" | |
4ee5fc9c | 38 | #include "wx/combobox.h" |
816be743 | 39 | #include "wx/valtext.h" |
60d876f3 | 40 | #include "wx/intl.h" |
c77a6796 | 41 | #include "wx/math.h" |
2a673eb1 | 42 | #include "wx/listbox.h" |
f85afd4e MB |
43 | #endif |
44 | ||
cb5df486 | 45 | #include "wx/textfile.h" |
816be743 | 46 | #include "wx/spinctrl.h" |
c4608a8a | 47 | #include "wx/tokenzr.h" |
4d1bc39c | 48 | #include "wx/renderer.h" |
ad805b9e | 49 | #include "wx/headerctrl.h" |
82edfbe7 | 50 | #include "wx/hashset.h" |
6d004f67 | 51 | |
b5808881 | 52 | #include "wx/generic/gridsel.h" |
29efc6e4 FM |
53 | #include "wx/generic/gridctrl.h" |
54 | #include "wx/generic/grideditors.h" | |
55 | #include "wx/generic/private/grid.h" | |
07296f0b | 56 | |
23318a53 | 57 | const char wxGridNameStr[] = "grid"; |
4c44eb66 | 58 | |
0b7e6e7d SN |
59 | #if defined(__WXMOTIF__) |
60 | #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier) | |
c78b3acd | 61 | #else |
0b7e6e7d | 62 | #define WXUNUSED_MOTIF(identifier) identifier |
c78b3acd SN |
63 | #endif |
64 | ||
65 | #if defined(__WXGTK__) | |
66 | #define WXUNUSED_GTK(identifier) WXUNUSED(identifier) | |
67 | #else | |
68 | #define WXUNUSED_GTK(identifier) identifier | |
69 | #endif | |
70 | ||
3f8e5072 JS |
71 | // Required for wxIs... functions |
72 | #include <ctype.h> | |
73 | ||
7a7fa93b | 74 | WX_DECLARE_HASH_SET_WITH_DECL_PTR(int, wxIntegerHash, wxIntegerEqual, |
e4c8592e | 75 | wxGridFixedIndicesSet, class WXDLLIMPEXP_ADV); |
82edfbe7 | 76 | |
29efc6e4 | 77 | |
b99be8fb | 78 | // ---------------------------------------------------------------------------- |
29efc6e4 | 79 | // globals |
b99be8fb VZ |
80 | // ---------------------------------------------------------------------------- |
81 | ||
670d0177 VZ |
82 | namespace |
83 | { | |
84 | ||
29efc6e4 FM |
85 | //#define DEBUG_ATTR_CACHE |
86 | #ifdef DEBUG_ATTR_CACHE | |
87 | static size_t gs_nAttrCacheHits = 0; | |
88 | static size_t gs_nAttrCacheMisses = 0; | |
89 | #endif | |
758cbedf | 90 | |
670d0177 VZ |
91 | // this struct simply combines together the default header renderers |
92 | // | |
93 | // as the renderers ctors are trivial, there is no problem with making them | |
94 | // globals | |
95 | struct DefaultHeaderRenderers | |
96 | { | |
97 | wxGridColumnHeaderRendererDefault colRenderer; | |
98 | wxGridRowHeaderRendererDefault rowRenderer; | |
99 | wxGridCornerHeaderRendererDefault cornerRenderer; | |
100 | } gs_defaultHeaderRenderers; | |
101 | ||
102 | } // anonymous namespace | |
103 | ||
29efc6e4 FM |
104 | // ---------------------------------------------------------------------------- |
105 | // constants | |
106 | // ---------------------------------------------------------------------------- | |
6f292345 | 107 | |
29efc6e4 FM |
108 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); |
109 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); | |
6f292345 | 110 | |
29efc6e4 FM |
111 | namespace |
112 | { | |
b99be8fb | 113 | |
29efc6e4 FM |
114 | // scroll line size |
115 | const size_t GRID_SCROLL_LINE_X = 15; | |
116 | const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X; | |
96ca74cd | 117 | |
29efc6e4 FM |
118 | // the size of hash tables used a bit everywhere (the max number of elements |
119 | // in these hash tables is the number of rows/columns) | |
120 | const int GRID_HASH_SIZE = 100; | |
2e9a6788 | 121 | |
29efc6e4 FM |
122 | // the minimal distance in pixels the mouse needs to move to start a drag |
123 | // operation | |
124 | const int DRAG_SENSITIVITY = 3; | |
b99be8fb | 125 | |
29efc6e4 | 126 | } // anonymous namespace |
b99be8fb VZ |
127 | |
128 | #include "wx/arrimpl.cpp" | |
129 | ||
130 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) | |
131 | WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray) | |
132 | ||
0f442030 RR |
133 | // ---------------------------------------------------------------------------- |
134 | // events | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
9b11752c VZ |
137 | wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK, wxGridEvent ); |
138 | wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK, wxGridEvent ); | |
139 | wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK, wxGridEvent ); | |
140 | wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK, wxGridEvent ); | |
141 | wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG, wxGridEvent ); | |
142 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK, wxGridEvent ); | |
143 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK, wxGridEvent ); | |
144 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK, wxGridEvent ); | |
145 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK, wxGridEvent ); | |
146 | wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE, wxGridSizeEvent ); | |
147 | wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE, wxGridSizeEvent ); | |
6f58f3d7 | 148 | wxDEFINE_EVENT( wxEVT_GRID_COL_AUTO_SIZE, wxGridSizeEvent ); |
9b11752c VZ |
149 | wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE, wxGridEvent ); |
150 | wxDEFINE_EVENT( wxEVT_GRID_COL_SORT, wxGridEvent ); | |
151 | wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT, wxGridRangeSelectEvent ); | |
152 | wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING, wxGridEvent ); | |
153 | wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED, wxGridEvent ); | |
154 | wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL, wxGridEvent ); | |
155 | wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN, wxGridEvent ); | |
156 | wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN, wxGridEvent ); | |
157 | wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED, wxGridEditorCreatedEvent ); | |
1dc17bca | 158 | wxDEFINE_EVENT( wxEVT_GRID_TABBING, wxGridEvent ); |
0b190b0f | 159 | |
db2c0468 SC |
160 | // ---------------------------------------------------------------------------- |
161 | // private helpers | |
162 | // ---------------------------------------------------------------------------- | |
163 | ||
164 | namespace | |
165 | { | |
166 | ||
167 | // ensure that first is less or equal to second, swapping the values if | |
168 | // necessary | |
169 | void EnsureFirstLessThanSecond(int& first, int& second) | |
170 | { | |
171 | if ( first > second ) | |
172 | wxSwap(first, second); | |
173 | } | |
174 | ||
175 | } // anonymous namespace | |
176 | ||
29efc6e4 FM |
177 | // ============================================================================ |
178 | // implementation | |
179 | // ============================================================================ | |
65e4e78e | 180 | |
29efc6e4 | 181 | IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler, wxEvtHandler) |
816be743 | 182 | |
29efc6e4 FM |
183 | BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler ) |
184 | EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus ) | |
185 | EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown ) | |
186 | EVT_CHAR( wxGridCellEditorEvtHandler::OnChar ) | |
187 | END_EVENT_TABLE() | |
816be743 | 188 | |
29efc6e4 FM |
189 | BEGIN_EVENT_TABLE(wxGridHeaderCtrl, wxHeaderCtrl) |
190 | EVT_HEADER_CLICK(wxID_ANY, wxGridHeaderCtrl::OnClick) | |
4a07d706 VZ |
191 | EVT_HEADER_DCLICK(wxID_ANY, wxGridHeaderCtrl::OnDoubleClick) |
192 | EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxGridHeaderCtrl::OnRightClick) | |
816be743 | 193 | |
29efc6e4 FM |
194 | EVT_HEADER_BEGIN_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnBeginResize) |
195 | EVT_HEADER_RESIZING(wxID_ANY, wxGridHeaderCtrl::OnResizing) | |
196 | EVT_HEADER_END_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnEndResize) | |
816be743 | 197 | |
29efc6e4 FM |
198 | EVT_HEADER_BEGIN_REORDER(wxID_ANY, wxGridHeaderCtrl::OnBeginReorder) |
199 | EVT_HEADER_END_REORDER(wxID_ANY, wxGridHeaderCtrl::OnEndReorder) | |
200 | END_EVENT_TABLE() | |
816be743 | 201 | |
29efc6e4 | 202 | wxGridOperations& wxGridRowOperations::Dual() const |
65e4e78e | 203 | { |
29efc6e4 FM |
204 | static wxGridColumnOperations s_colOper; |
205 | ||
206 | return s_colOper; | |
816be743 VZ |
207 | } |
208 | ||
29efc6e4 | 209 | wxGridOperations& wxGridColumnOperations::Dual() const |
0b190b0f | 210 | { |
29efc6e4 | 211 | static wxGridRowOperations s_rowOper; |
2f024384 | 212 | |
29efc6e4 | 213 | return s_rowOper; |
0b190b0f VZ |
214 | } |
215 | ||
508011ce | 216 | // ---------------------------------------------------------------------------- |
29efc6e4 FM |
217 | // wxGridCellWorker is an (almost) empty common base class for |
218 | // wxGridCellRenderer and wxGridCellEditor managing ref counting | |
508011ce VZ |
219 | // ---------------------------------------------------------------------------- |
220 | ||
29efc6e4 | 221 | void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params)) |
65e4e78e | 222 | { |
29efc6e4 | 223 | // nothing to do |
65e4e78e VZ |
224 | } |
225 | ||
29efc6e4 | 226 | wxGridCellWorker::~wxGridCellWorker() |
65e4e78e | 227 | { |
508011ce VZ |
228 | } |
229 | ||
ba9574c3 VZ |
230 | // ---------------------------------------------------------------------------- |
231 | // wxGridHeaderLabelsRenderer and related classes | |
232 | // ---------------------------------------------------------------------------- | |
233 | ||
234 | void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid& grid, | |
235 | wxDC& dc, | |
236 | const wxString& value, | |
237 | const wxRect& rect, | |
238 | int horizAlign, | |
239 | int vertAlign, | |
240 | int textOrientation) const | |
241 | { | |
242 | dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); | |
243 | dc.SetTextForeground(grid.GetLabelTextColour()); | |
244 | dc.SetFont(grid.GetLabelFont()); | |
245 | grid.DrawTextRectangle(dc, value, rect, horizAlign, vertAlign, textOrientation); | |
246 | } | |
247 | ||
248 | ||
249 | void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid), | |
250 | wxDC& dc, | |
251 | wxRect& rect) const | |
252 | { | |
253 | dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW))); | |
254 | dc.DrawLine(rect.GetRight(), rect.GetTop(), | |
255 | rect.GetRight(), rect.GetBottom()); | |
256 | dc.DrawLine(rect.GetLeft(), rect.GetTop(), | |
257 | rect.GetLeft(), rect.GetBottom()); | |
258 | dc.DrawLine(rect.GetLeft(), rect.GetBottom(), | |
259 | rect.GetRight() + 1, rect.GetBottom()); | |
260 | ||
261 | dc.SetPen(*wxWHITE_PEN); | |
262 | dc.DrawLine(rect.GetLeft() + 1, rect.GetTop(), | |
263 | rect.GetLeft() + 1, rect.GetBottom()); | |
264 | dc.DrawLine(rect.GetLeft() + 1, rect.GetTop(), | |
265 | rect.GetRight(), rect.GetTop()); | |
266 | ||
267 | rect.Deflate(2); | |
268 | } | |
269 | ||
270 | void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid), | |
271 | wxDC& dc, | |
272 | wxRect& rect) const | |
273 | { | |
274 | dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW))); | |
275 | dc.DrawLine(rect.GetRight(), rect.GetTop(), | |
276 | rect.GetRight(), rect.GetBottom()); | |
277 | dc.DrawLine(rect.GetLeft(), rect.GetTop(), | |
278 | rect.GetRight(), rect.GetTop()); | |
279 | dc.DrawLine(rect.GetLeft(), rect.GetBottom(), | |
280 | rect.GetRight() + 1, rect.GetBottom()); | |
281 | ||
282 | dc.SetPen(*wxWHITE_PEN); | |
283 | dc.DrawLine(rect.GetLeft(), rect.GetTop() + 1, | |
284 | rect.GetLeft(), rect.GetBottom()); | |
285 | dc.DrawLine(rect.GetLeft(), rect.GetTop() + 1, | |
286 | rect.GetRight(), rect.GetTop() + 1); | |
287 | ||
288 | rect.Deflate(2); | |
289 | } | |
290 | ||
291 | void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid), | |
292 | wxDC& dc, | |
293 | wxRect& rect) const | |
294 | { | |
295 | dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW))); | |
296 | dc.DrawLine(rect.GetRight() - 1, rect.GetBottom() - 1, | |
297 | rect.GetRight() - 1, rect.GetTop()); | |
298 | dc.DrawLine(rect.GetRight() - 1, rect.GetBottom() - 1, | |
299 | rect.GetLeft(), rect.GetBottom() - 1); | |
300 | dc.DrawLine(rect.GetLeft(), rect.GetTop(), | |
301 | rect.GetRight(), rect.GetTop()); | |
302 | dc.DrawLine(rect.GetLeft(), rect.GetTop(), | |
303 | rect.GetLeft(), rect.GetBottom()); | |
304 | ||
305 | dc.SetPen(*wxWHITE_PEN); | |
306 | dc.DrawLine(rect.GetLeft() + 1, rect.GetTop() + 1, | |
307 | rect.GetRight() - 1, rect.GetTop() + 1); | |
308 | dc.DrawLine(rect.GetLeft() + 1, rect.GetTop() + 1, | |
309 | rect.GetLeft() + 1, rect.GetBottom() - 1); | |
310 | ||
311 | rect.Deflate(2); | |
312 | } | |
313 | ||
2796cce3 RD |
314 | // ---------------------------------------------------------------------------- |
315 | // wxGridCellAttr | |
316 | // ---------------------------------------------------------------------------- | |
317 | ||
1df4050d VZ |
318 | void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) |
319 | { | |
1df4050d VZ |
320 | m_isReadOnly = Unset; |
321 | ||
322 | m_renderer = NULL; | |
323 | m_editor = NULL; | |
324 | ||
325 | m_attrkind = wxGridCellAttr::Cell; | |
326 | ||
27f35b66 | 327 | m_sizeRows = m_sizeCols = 1; |
b63fce94 | 328 | m_overflow = UnsetOverflow; |
27f35b66 | 329 | |
1df4050d VZ |
330 | SetDefAttr(attrDefault); |
331 | } | |
332 | ||
39bcce60 | 333 | wxGridCellAttr *wxGridCellAttr::Clone() const |
a68c1246 | 334 | { |
1df4050d VZ |
335 | wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr); |
336 | ||
a68c1246 VZ |
337 | if ( HasTextColour() ) |
338 | attr->SetTextColour(GetTextColour()); | |
339 | if ( HasBackgroundColour() ) | |
340 | attr->SetBackgroundColour(GetBackgroundColour()); | |
341 | if ( HasFont() ) | |
342 | attr->SetFont(GetFont()); | |
343 | if ( HasAlignment() ) | |
344 | attr->SetAlignment(m_hAlign, m_vAlign); | |
345 | ||
27f35b66 SN |
346 | attr->SetSize( m_sizeRows, m_sizeCols ); |
347 | ||
a68c1246 VZ |
348 | if ( m_renderer ) |
349 | { | |
350 | attr->SetRenderer(m_renderer); | |
39bcce60 | 351 | m_renderer->IncRef(); |
a68c1246 VZ |
352 | } |
353 | if ( m_editor ) | |
354 | { | |
355 | attr->SetEditor(m_editor); | |
39bcce60 | 356 | m_editor->IncRef(); |
a68c1246 VZ |
357 | } |
358 | ||
359 | if ( IsReadOnly() ) | |
360 | attr->SetReadOnly(); | |
361 | ||
dfd7c082 | 362 | attr->SetOverflow( m_overflow == Overflow ); |
19d7140e VZ |
363 | attr->SetKind( m_attrkind ); |
364 | ||
a68c1246 VZ |
365 | return attr; |
366 | } | |
367 | ||
19d7140e VZ |
368 | void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) |
369 | { | |
370 | if ( !HasTextColour() && mergefrom->HasTextColour() ) | |
371 | SetTextColour(mergefrom->GetTextColour()); | |
372 | if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() ) | |
373 | SetBackgroundColour(mergefrom->GetBackgroundColour()); | |
374 | if ( !HasFont() && mergefrom->HasFont() ) | |
375 | SetFont(mergefrom->GetFont()); | |
4db6714b KH |
376 | if ( !HasAlignment() && mergefrom->HasAlignment() ) |
377 | { | |
19d7140e VZ |
378 | int hAlign, vAlign; |
379 | mergefrom->GetAlignment( &hAlign, &vAlign); | |
380 | SetAlignment(hAlign, vAlign); | |
381 | } | |
3100c3db RD |
382 | if ( !HasSize() && mergefrom->HasSize() ) |
383 | mergefrom->GetSize( &m_sizeRows, &m_sizeCols ); | |
27f35b66 | 384 | |
19d7140e VZ |
385 | // Directly access member functions as GetRender/Editor don't just return |
386 | // m_renderer/m_editor | |
387 | // | |
388 | // Maybe add support for merge of Render and Editor? | |
389 | if (!HasRenderer() && mergefrom->HasRenderer() ) | |
bf7945ce | 390 | { |
19d7140e VZ |
391 | m_renderer = mergefrom->m_renderer; |
392 | m_renderer->IncRef(); | |
393 | } | |
394 | if ( !HasEditor() && mergefrom->HasEditor() ) | |
395 | { | |
396 | m_editor = mergefrom->m_editor; | |
397 | m_editor->IncRef(); | |
398 | } | |
2f024384 | 399 | if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) |
19d7140e VZ |
400 | SetReadOnly(mergefrom->IsReadOnly()); |
401 | ||
2f024384 | 402 | if (!HasOverflowMode() && mergefrom->HasOverflowMode() ) |
ff699386 | 403 | SetOverflow(mergefrom->GetOverflow()); |
ef5df12b | 404 | |
19d7140e VZ |
405 | SetDefAttr(mergefrom->m_defGridAttr); |
406 | } | |
407 | ||
27f35b66 SN |
408 | void wxGridCellAttr::SetSize(int num_rows, int num_cols) |
409 | { | |
410 | // The size of a cell is normally 1,1 | |
411 | ||
412 | // If this cell is larger (2,2) then this is the top left cell | |
413 | // the other cells that will be covered (lower right cells) must be | |
414 | // set to negative or zero values such that | |
415 | // row + num_rows of the covered cell points to the larger cell (this cell) | |
416 | // same goes for the col + num_cols. | |
417 | ||
418 | // Size of 0,0 is NOT valid, neither is <=0 and any positive value | |
419 | ||
2f024384 DS |
420 | wxASSERT_MSG( (!((num_rows > 0) && (num_cols <= 0)) || |
421 | !((num_rows <= 0) && (num_cols > 0)) || | |
422 | !((num_rows == 0) && (num_cols == 0))), | |
e75e8815 | 423 | wxT("wxGridCellAttr::SetSize only takes two positive values or negative/zero values")); |
27f35b66 SN |
424 | |
425 | m_sizeRows = num_rows; | |
426 | m_sizeCols = num_cols; | |
427 | } | |
428 | ||
2796cce3 RD |
429 | const wxColour& wxGridCellAttr::GetTextColour() const |
430 | { | |
431 | if (HasTextColour()) | |
508011ce | 432 | { |
2796cce3 | 433 | return m_colText; |
508011ce | 434 | } |
0926b2fc | 435 | else if (m_defGridAttr && m_defGridAttr != this) |
508011ce | 436 | { |
2796cce3 | 437 | return m_defGridAttr->GetTextColour(); |
508011ce VZ |
438 | } |
439 | else | |
440 | { | |
2796cce3 RD |
441 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
442 | return wxNullColour; | |
443 | } | |
444 | } | |
445 | ||
2796cce3 RD |
446 | const wxColour& wxGridCellAttr::GetBackgroundColour() const |
447 | { | |
448 | if (HasBackgroundColour()) | |
2f024384 | 449 | { |
2796cce3 | 450 | return m_colBack; |
2f024384 | 451 | } |
0926b2fc | 452 | else if (m_defGridAttr && m_defGridAttr != this) |
2f024384 | 453 | { |
2796cce3 | 454 | return m_defGridAttr->GetBackgroundColour(); |
2f024384 | 455 | } |
508011ce VZ |
456 | else |
457 | { | |
2796cce3 RD |
458 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
459 | return wxNullColour; | |
460 | } | |
461 | } | |
462 | ||
2796cce3 RD |
463 | const wxFont& wxGridCellAttr::GetFont() const |
464 | { | |
465 | if (HasFont()) | |
2f024384 | 466 | { |
2796cce3 | 467 | return m_font; |
2f024384 | 468 | } |
0926b2fc | 469 | else if (m_defGridAttr && m_defGridAttr != this) |
2f024384 | 470 | { |
2796cce3 | 471 | return m_defGridAttr->GetFont(); |
2f024384 | 472 | } |
508011ce VZ |
473 | else |
474 | { | |
2796cce3 RD |
475 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
476 | return wxNullFont; | |
477 | } | |
478 | } | |
479 | ||
2796cce3 RD |
480 | void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const |
481 | { | |
508011ce VZ |
482 | if (HasAlignment()) |
483 | { | |
4db6714b KH |
484 | if ( hAlign ) |
485 | *hAlign = m_hAlign; | |
486 | if ( vAlign ) | |
487 | *vAlign = m_vAlign; | |
2796cce3 | 488 | } |
0926b2fc | 489 | else if (m_defGridAttr && m_defGridAttr != this) |
c2f5b920 | 490 | { |
2796cce3 | 491 | m_defGridAttr->GetAlignment(hAlign, vAlign); |
c2f5b920 | 492 | } |
508011ce VZ |
493 | else |
494 | { | |
2796cce3 RD |
495 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
496 | } | |
497 | } | |
498 | ||
cfbc15ee VZ |
499 | void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign, int *vAlign) const |
500 | { | |
501 | if ( hAlign && m_hAlign != wxALIGN_INVALID ) | |
502 | *hAlign = m_hAlign; | |
503 | ||
504 | if ( vAlign && m_vAlign != wxALIGN_INVALID ) | |
505 | *vAlign = m_vAlign; | |
506 | } | |
507 | ||
27f35b66 SN |
508 | void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const |
509 | { | |
4db6714b KH |
510 | if ( num_rows ) |
511 | *num_rows = m_sizeRows; | |
512 | if ( num_cols ) | |
513 | *num_cols = m_sizeCols; | |
27f35b66 | 514 | } |
2796cce3 | 515 | |
f2d76237 | 516 | // GetRenderer and GetEditor use a slightly different decision path about |
28a77bc4 RD |
517 | // which attribute to use. If a non-default attr object has one then it is |
518 | // used, otherwise the default editor or renderer is fetched from the grid and | |
519 | // used. It should be the default for the data type of the cell. If it is | |
520 | // NULL (because the table has a type that the grid does not have in its | |
c2f5b920 | 521 | // registry), then the grid's default editor or renderer is used. |
28a77bc4 | 522 | |
ef316e23 | 523 | wxGridCellRenderer* wxGridCellAttr::GetRenderer(const wxGrid* grid, int row, int col) const |
28a77bc4 | 524 | { |
c2f5b920 | 525 | wxGridCellRenderer *renderer = NULL; |
28a77bc4 | 526 | |
3cf883a2 | 527 | if ( m_renderer && this != m_defGridAttr ) |
0b190b0f | 528 | { |
3cf883a2 VZ |
529 | // use the cells renderer if it has one |
530 | renderer = m_renderer; | |
531 | renderer->IncRef(); | |
0b190b0f | 532 | } |
2f024384 | 533 | else // no non-default cell renderer |
0b190b0f | 534 | { |
3cf883a2 VZ |
535 | // get default renderer for the data type |
536 | if ( grid ) | |
537 | { | |
538 | // GetDefaultRendererForCell() will do IncRef() for us | |
539 | renderer = grid->GetDefaultRendererForCell(row, col); | |
540 | } | |
0b190b0f | 541 | |
c2f5b920 | 542 | if ( renderer == NULL ) |
3cf883a2 | 543 | { |
c2f5b920 | 544 | if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) ) |
3cf883a2 VZ |
545 | { |
546 | // if we still don't have one then use the grid default | |
547 | // (no need for IncRef() here neither) | |
548 | renderer = m_defGridAttr->GetRenderer(NULL, 0, 0); | |
549 | } | |
550 | else // default grid attr | |
551 | { | |
552 | // use m_renderer which we had decided not to use initially | |
553 | renderer = m_renderer; | |
554 | if ( renderer ) | |
555 | renderer->IncRef(); | |
556 | } | |
557 | } | |
0b190b0f | 558 | } |
28a77bc4 | 559 | |
3cf883a2 VZ |
560 | // we're supposed to always find something |
561 | wxASSERT_MSG(renderer, wxT("Missing default cell renderer")); | |
28a77bc4 RD |
562 | |
563 | return renderer; | |
2796cce3 RD |
564 | } |
565 | ||
3cf883a2 | 566 | // same as above, except for s/renderer/editor/g |
ef316e23 | 567 | wxGridCellEditor* wxGridCellAttr::GetEditor(const wxGrid* grid, int row, int col) const |
07296f0b | 568 | { |
c2f5b920 | 569 | wxGridCellEditor *editor = NULL; |
0b190b0f | 570 | |
3cf883a2 | 571 | if ( m_editor && this != m_defGridAttr ) |
0b190b0f | 572 | { |
3cf883a2 VZ |
573 | // use the cells editor if it has one |
574 | editor = m_editor; | |
575 | editor->IncRef(); | |
0b190b0f | 576 | } |
3cf883a2 | 577 | else // no non default cell editor |
0b190b0f | 578 | { |
3cf883a2 VZ |
579 | // get default editor for the data type |
580 | if ( grid ) | |
581 | { | |
582 | // GetDefaultEditorForCell() will do IncRef() for us | |
583 | editor = grid->GetDefaultEditorForCell(row, col); | |
584 | } | |
3cf883a2 | 585 | |
c2f5b920 | 586 | if ( editor == NULL ) |
3cf883a2 | 587 | { |
c2f5b920 | 588 | if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) ) |
3cf883a2 VZ |
589 | { |
590 | // if we still don't have one then use the grid default | |
591 | // (no need for IncRef() here neither) | |
592 | editor = m_defGridAttr->GetEditor(NULL, 0, 0); | |
593 | } | |
594 | else // default grid attr | |
595 | { | |
596 | // use m_editor which we had decided not to use initially | |
597 | editor = m_editor; | |
598 | if ( editor ) | |
599 | editor->IncRef(); | |
600 | } | |
601 | } | |
0b190b0f | 602 | } |
28a77bc4 | 603 | |
3cf883a2 VZ |
604 | // we're supposed to always find something |
605 | wxASSERT_MSG(editor, wxT("Missing default cell editor")); | |
606 | ||
28a77bc4 | 607 | return editor; |
07296f0b RD |
608 | } |
609 | ||
b99be8fb | 610 | // ---------------------------------------------------------------------------- |
758cbedf | 611 | // wxGridCellAttrData |
b99be8fb VZ |
612 | // ---------------------------------------------------------------------------- |
613 | ||
758cbedf | 614 | void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) |
b99be8fb | 615 | { |
96ca74cd SN |
616 | // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not |
617 | // touch attribute's reference counting explicitly, since this | |
618 | // is managed by class wxGridCellWithAttr | |
b99be8fb VZ |
619 | int n = FindIndex(row, col); |
620 | if ( n == wxNOT_FOUND ) | |
621 | { | |
a70517e9 VZ |
622 | if ( attr ) |
623 | { | |
624 | // add the attribute | |
625 | m_attrs.Add(new wxGridCellWithAttr(row, col, attr)); | |
626 | } | |
627 | //else: nothing to do | |
b99be8fb | 628 | } |
6f292345 | 629 | else // we already have an attribute for this cell |
b99be8fb VZ |
630 | { |
631 | if ( attr ) | |
632 | { | |
633 | // change the attribute | |
96ca74cd | 634 | m_attrs[(size_t)n].ChangeAttr(attr); |
b99be8fb VZ |
635 | } |
636 | else | |
637 | { | |
638 | // remove this attribute | |
639 | m_attrs.RemoveAt((size_t)n); | |
640 | } | |
641 | } | |
b99be8fb VZ |
642 | } |
643 | ||
758cbedf | 644 | wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const |
b99be8fb | 645 | { |
10a4531d | 646 | wxGridCellAttr *attr = NULL; |
b99be8fb VZ |
647 | |
648 | int n = FindIndex(row, col); | |
649 | if ( n != wxNOT_FOUND ) | |
650 | { | |
2e9a6788 VZ |
651 | attr = m_attrs[(size_t)n].attr; |
652 | attr->IncRef(); | |
b99be8fb VZ |
653 | } |
654 | ||
655 | return attr; | |
656 | } | |
657 | ||
4d60017a SN |
658 | void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) |
659 | { | |
660 | size_t count = m_attrs.GetCount(); | |
661 | for ( size_t n = 0; n < count; n++ ) | |
662 | { | |
663 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
664 | wxCoord row = coords.GetRow(); |
665 | if ((size_t)row >= pos) | |
666 | { | |
667 | if (numRows > 0) | |
668 | { | |
669 | // If rows inserted, include row counter where necessary | |
670 | coords.SetRow(row + numRows); | |
671 | } | |
672 | else if (numRows < 0) | |
673 | { | |
674 | // If rows deleted ... | |
675 | if ((size_t)row >= pos - numRows) | |
676 | { | |
677 | // ...either decrement row counter (if row still exists)... | |
678 | coords.SetRow(row + numRows); | |
679 | } | |
680 | else | |
681 | { | |
682 | // ...or remove the attribute | |
01dd42b6 | 683 | m_attrs.RemoveAt(n); |
4db6714b KH |
684 | n--; |
685 | count--; | |
d1c0b4f9 VZ |
686 | } |
687 | } | |
4d60017a SN |
688 | } |
689 | } | |
690 | } | |
691 | ||
692 | void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) | |
693 | { | |
694 | size_t count = m_attrs.GetCount(); | |
695 | for ( size_t n = 0; n < count; n++ ) | |
696 | { | |
697 | wxGridCellCoords& coords = m_attrs[n].coords; | |
d1c0b4f9 VZ |
698 | wxCoord col = coords.GetCol(); |
699 | if ( (size_t)col >= pos ) | |
700 | { | |
701 | if ( numCols > 0 ) | |
702 | { | |
703 | // If rows inserted, include row counter where necessary | |
704 | coords.SetCol(col + numCols); | |
705 | } | |
706 | else if (numCols < 0) | |
707 | { | |
708 | // If rows deleted ... | |
709 | if ((size_t)col >= pos - numCols) | |
710 | { | |
711 | // ...either decrement row counter (if row still exists)... | |
712 | coords.SetCol(col + numCols); | |
713 | } | |
714 | else | |
715 | { | |
716 | // ...or remove the attribute | |
01dd42b6 | 717 | m_attrs.RemoveAt(n); |
2f024384 DS |
718 | n--; |
719 | count--; | |
d1c0b4f9 VZ |
720 | } |
721 | } | |
4d60017a SN |
722 | } |
723 | } | |
724 | } | |
725 | ||
758cbedf | 726 | int wxGridCellAttrData::FindIndex(int row, int col) const |
b99be8fb VZ |
727 | { |
728 | size_t count = m_attrs.GetCount(); | |
729 | for ( size_t n = 0; n < count; n++ ) | |
730 | { | |
731 | const wxGridCellCoords& coords = m_attrs[n].coords; | |
732 | if ( (coords.GetRow() == row) && (coords.GetCol() == col) ) | |
733 | { | |
734 | return n; | |
735 | } | |
736 | } | |
737 | ||
738 | return wxNOT_FOUND; | |
739 | } | |
740 | ||
758cbedf VZ |
741 | // ---------------------------------------------------------------------------- |
742 | // wxGridRowOrColAttrData | |
743 | // ---------------------------------------------------------------------------- | |
744 | ||
745 | wxGridRowOrColAttrData::~wxGridRowOrColAttrData() | |
746 | { | |
b4a980f4 | 747 | size_t count = m_attrs.GetCount(); |
758cbedf VZ |
748 | for ( size_t n = 0; n < count; n++ ) |
749 | { | |
750 | m_attrs[n]->DecRef(); | |
751 | } | |
752 | } | |
753 | ||
754 | wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const | |
755 | { | |
10a4531d | 756 | wxGridCellAttr *attr = NULL; |
758cbedf VZ |
757 | |
758 | int n = m_rowsOrCols.Index(rowOrCol); | |
759 | if ( n != wxNOT_FOUND ) | |
760 | { | |
761 | attr = m_attrs[(size_t)n]; | |
762 | attr->IncRef(); | |
763 | } | |
764 | ||
765 | return attr; | |
766 | } | |
767 | ||
768 | void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) | |
769 | { | |
a95e38c0 VZ |
770 | int i = m_rowsOrCols.Index(rowOrCol); |
771 | if ( i == wxNOT_FOUND ) | |
758cbedf | 772 | { |
62d249cb VZ |
773 | if ( attr ) |
774 | { | |
4a53f701 | 775 | // store the new attribute, taking its ownership |
62d249cb VZ |
776 | m_rowsOrCols.Add(rowOrCol); |
777 | m_attrs.Add(attr); | |
778 | } | |
779 | // nothing to remove | |
758cbedf | 780 | } |
4a53f701 | 781 | else // we have an attribute for this row or column |
758cbedf | 782 | { |
a95e38c0 | 783 | size_t n = (size_t)i; |
4a53f701 VZ |
784 | |
785 | // notice that this code works correctly even when the old attribute is | |
786 | // the same as the new one: as we own of it, we must call DecRef() on | |
787 | // it in any case and this won't result in destruction of the new | |
788 | // attribute if it's the same as old one because it must have ref count | |
789 | // of at least 2 to be passed to us while we keep a reference to it too | |
790 | m_attrs[n]->DecRef(); | |
791 | ||
758cbedf VZ |
792 | if ( attr ) |
793 | { | |
4a53f701 | 794 | // replace the attribute with the new one |
a95e38c0 | 795 | m_attrs[n] = attr; |
758cbedf | 796 | } |
4a53f701 | 797 | else // remove the attribute |
758cbedf | 798 | { |
a95e38c0 VZ |
799 | m_rowsOrCols.RemoveAt(n); |
800 | m_attrs.RemoveAt(n); | |
758cbedf VZ |
801 | } |
802 | } | |
803 | } | |
804 | ||
4d60017a SN |
805 | void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) |
806 | { | |
807 | size_t count = m_attrs.GetCount(); | |
808 | for ( size_t n = 0; n < count; n++ ) | |
809 | { | |
810 | int & rowOrCol = m_rowsOrCols[n]; | |
d1c0b4f9 VZ |
811 | if ( (size_t)rowOrCol >= pos ) |
812 | { | |
813 | if ( numRowsOrCols > 0 ) | |
814 | { | |
815 | // If rows inserted, include row counter where necessary | |
816 | rowOrCol += numRowsOrCols; | |
817 | } | |
818 | else if ( numRowsOrCols < 0) | |
819 | { | |
820 | // If rows deleted, either decrement row counter (if row still exists) | |
821 | if ((size_t)rowOrCol >= pos - numRowsOrCols) | |
822 | rowOrCol += numRowsOrCols; | |
823 | else | |
824 | { | |
01dd42b6 VZ |
825 | m_rowsOrCols.RemoveAt(n); |
826 | m_attrs[n]->DecRef(); | |
827 | m_attrs.RemoveAt(n); | |
4db6714b KH |
828 | n--; |
829 | count--; | |
d1c0b4f9 VZ |
830 | } |
831 | } | |
4d60017a SN |
832 | } |
833 | } | |
834 | } | |
835 | ||
b99be8fb VZ |
836 | // ---------------------------------------------------------------------------- |
837 | // wxGridCellAttrProvider | |
838 | // ---------------------------------------------------------------------------- | |
839 | ||
840 | wxGridCellAttrProvider::wxGridCellAttrProvider() | |
841 | { | |
10a4531d | 842 | m_data = NULL; |
b99be8fb VZ |
843 | } |
844 | ||
845 | wxGridCellAttrProvider::~wxGridCellAttrProvider() | |
846 | { | |
847 | delete m_data; | |
848 | } | |
849 | ||
850 | void wxGridCellAttrProvider::InitData() | |
851 | { | |
852 | m_data = new wxGridCellAttrProviderData; | |
853 | } | |
854 | ||
19d7140e VZ |
855 | wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col, |
856 | wxGridCellAttr::wxAttrKind kind ) const | |
b99be8fb | 857 | { |
10a4531d | 858 | wxGridCellAttr *attr = NULL; |
758cbedf VZ |
859 | if ( m_data ) |
860 | { | |
962a48f6 | 861 | switch (kind) |
758cbedf | 862 | { |
19d7140e | 863 | case (wxGridCellAttr::Any): |
962a48f6 DS |
864 | // Get cached merge attributes. |
865 | // Currently not used as no cache implemented as not mutable | |
19d7140e | 866 | // attr = m_data->m_mergeAttr.GetAttr(row, col); |
4db6714b | 867 | if (!attr) |
19d7140e | 868 | { |
962a48f6 DS |
869 | // Basically implement old version. |
870 | // Also check merge cache, so we don't have to re-merge every time.. | |
999836aa VZ |
871 | wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col); |
872 | wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row); | |
873 | wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col); | |
19d7140e | 874 | |
4db6714b KH |
875 | if ((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol)) |
876 | { | |
2d0c2e79 | 877 | // Two or more are non NULL |
19d7140e VZ |
878 | attr = new wxGridCellAttr; |
879 | attr->SetKind(wxGridCellAttr::Merged); | |
880 | ||
962a48f6 | 881 | // Order is important.. |
4db6714b KH |
882 | if (attrcell) |
883 | { | |
19d7140e VZ |
884 | attr->MergeWith(attrcell); |
885 | attrcell->DecRef(); | |
886 | } | |
4db6714b KH |
887 | if (attrcol) |
888 | { | |
19d7140e VZ |
889 | attr->MergeWith(attrcol); |
890 | attrcol->DecRef(); | |
891 | } | |
4db6714b KH |
892 | if (attrrow) |
893 | { | |
19d7140e VZ |
894 | attr->MergeWith(attrrow); |
895 | attrrow->DecRef(); | |
896 | } | |
962a48f6 DS |
897 | |
898 | // store merge attr if cache implemented | |
19d7140e VZ |
899 | //attr->IncRef(); |
900 | //m_data->m_mergeAttr.SetAttr(attr, row, col); | |
901 | } | |
902 | else | |
2d0c2e79 | 903 | { |
19d7140e | 904 | // one or none is non null return it or null. |
4db6714b KH |
905 | if (attrrow) |
906 | attr = attrrow; | |
907 | if (attrcol) | |
2d0c2e79 | 908 | { |
962a48f6 | 909 | if (attr) |
2d0c2e79 RD |
910 | attr->DecRef(); |
911 | attr = attrcol; | |
912 | } | |
4db6714b | 913 | if (attrcell) |
2d0c2e79 | 914 | { |
4db6714b | 915 | if (attr) |
2d0c2e79 RD |
916 | attr->DecRef(); |
917 | attr = attrcell; | |
918 | } | |
19d7140e | 919 | } |
29efc6e4 FM |
920 | } |
921 | break; | |
f2d76237 | 922 | |
29efc6e4 FM |
923 | case (wxGridCellAttr::Cell): |
924 | attr = m_data->m_cellAttrs.GetAttr(row, col); | |
925 | break; | |
926 | ||
927 | case (wxGridCellAttr::Col): | |
928 | attr = m_data->m_colAttrs.GetAttr(col); | |
929 | break; | |
930 | ||
931 | case (wxGridCellAttr::Row): | |
932 | attr = m_data->m_rowAttrs.GetAttr(row); | |
933 | break; | |
934 | ||
935 | default: | |
936 | // unused as yet... | |
937 | // (wxGridCellAttr::Default): | |
938 | // (wxGridCellAttr::Merged): | |
939 | break; | |
940 | } | |
c4608a8a VZ |
941 | } |
942 | ||
29efc6e4 | 943 | return attr; |
c4608a8a VZ |
944 | } |
945 | ||
29efc6e4 FM |
946 | void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, |
947 | int row, int col) | |
c4608a8a | 948 | { |
29efc6e4 FM |
949 | if ( !m_data ) |
950 | InitData(); | |
c4608a8a | 951 | |
29efc6e4 FM |
952 | m_data->m_cellAttrs.SetAttr(attr, row, col); |
953 | } | |
c4608a8a | 954 | |
29efc6e4 FM |
955 | void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row) |
956 | { | |
957 | if ( !m_data ) | |
958 | InitData(); | |
c4608a8a | 959 | |
29efc6e4 FM |
960 | m_data->m_rowAttrs.SetAttr(attr, row); |
961 | } | |
c4608a8a | 962 | |
29efc6e4 FM |
963 | void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) |
964 | { | |
965 | if ( !m_data ) | |
966 | InitData(); | |
f2d76237 | 967 | |
29efc6e4 | 968 | m_data->m_colAttrs.SetAttr(attr, col); |
f2d76237 RD |
969 | } |
970 | ||
29efc6e4 | 971 | void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) |
f2d76237 | 972 | { |
29efc6e4 FM |
973 | if ( m_data ) |
974 | { | |
975 | m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); | |
2f024384 | 976 | |
29efc6e4 FM |
977 | m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); |
978 | } | |
f2d76237 RD |
979 | } |
980 | ||
29efc6e4 | 981 | void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) |
f2d76237 | 982 | { |
29efc6e4 FM |
983 | if ( m_data ) |
984 | { | |
985 | m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); | |
2f024384 | 986 | |
29efc6e4 FM |
987 | m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); |
988 | } | |
f2d76237 RD |
989 | } |
990 | ||
670d0177 VZ |
991 | const wxGridColumnHeaderRenderer& |
992 | wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col)) | |
993 | { | |
994 | return gs_defaultHeaderRenderers.colRenderer; | |
995 | } | |
996 | ||
997 | const wxGridRowHeaderRenderer& | |
998 | wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row)) | |
999 | { | |
1000 | return gs_defaultHeaderRenderers.rowRenderer; | |
1001 | } | |
1002 | ||
1003 | const wxGridCornerHeaderRenderer& wxGridCellAttrProvider::GetCornerRenderer() | |
1004 | { | |
1005 | return gs_defaultHeaderRenderers.cornerRenderer; | |
1006 | } | |
1007 | ||
758cbedf VZ |
1008 | // ---------------------------------------------------------------------------- |
1009 | // wxGridTableBase | |
1010 | // ---------------------------------------------------------------------------- | |
1011 | ||
f85afd4e MB |
1012 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) |
1013 | ||
f85afd4e | 1014 | wxGridTableBase::wxGridTableBase() |
f85afd4e | 1015 | { |
10a4531d VZ |
1016 | m_view = NULL; |
1017 | m_attrProvider = NULL; | |
f85afd4e MB |
1018 | } |
1019 | ||
1020 | wxGridTableBase::~wxGridTableBase() | |
1021 | { | |
b99be8fb VZ |
1022 | delete m_attrProvider; |
1023 | } | |
1024 | ||
1025 | void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider) | |
1026 | { | |
1027 | delete m_attrProvider; | |
1028 | m_attrProvider = attrProvider; | |
f85afd4e MB |
1029 | } |
1030 | ||
f2d76237 RD |
1031 | bool wxGridTableBase::CanHaveAttributes() |
1032 | { | |
1033 | if ( ! GetAttrProvider() ) | |
1034 | { | |
1035 | // use the default attr provider by default | |
1036 | SetAttrProvider(new wxGridCellAttrProvider); | |
1037 | } | |
2f024384 | 1038 | |
ca65c044 | 1039 | return true; |
f2d76237 RD |
1040 | } |
1041 | ||
19d7140e | 1042 | wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) |
b99be8fb VZ |
1043 | { |
1044 | if ( m_attrProvider ) | |
19d7140e | 1045 | return m_attrProvider->GetAttr(row, col, kind); |
b99be8fb | 1046 | else |
10a4531d | 1047 | return NULL; |
b99be8fb VZ |
1048 | } |
1049 | ||
758cbedf | 1050 | void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) |
b99be8fb VZ |
1051 | { |
1052 | if ( m_attrProvider ) | |
1053 | { | |
6f292345 VZ |
1054 | if ( attr ) |
1055 | attr->SetKind(wxGridCellAttr::Cell); | |
b99be8fb VZ |
1056 | m_attrProvider->SetAttr(attr, row, col); |
1057 | } | |
1058 | else | |
1059 | { | |
1060 | // as we take ownership of the pointer and don't store it, we must | |
1061 | // free it now | |
39bcce60 | 1062 | wxSafeDecRef(attr); |
b99be8fb VZ |
1063 | } |
1064 | } | |
1065 | ||
758cbedf VZ |
1066 | void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) |
1067 | { | |
1068 | if ( m_attrProvider ) | |
1069 | { | |
19d7140e | 1070 | attr->SetKind(wxGridCellAttr::Row); |
758cbedf VZ |
1071 | m_attrProvider->SetRowAttr(attr, row); |
1072 | } | |
1073 | else | |
1074 | { | |
1075 | // as we take ownership of the pointer and don't store it, we must | |
1076 | // free it now | |
39bcce60 | 1077 | wxSafeDecRef(attr); |
758cbedf VZ |
1078 | } |
1079 | } | |
1080 | ||
1081 | void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) | |
1082 | { | |
1083 | if ( m_attrProvider ) | |
1084 | { | |
19d7140e | 1085 | attr->SetKind(wxGridCellAttr::Col); |
758cbedf VZ |
1086 | m_attrProvider->SetColAttr(attr, col); |
1087 | } | |
1088 | else | |
1089 | { | |
1090 | // as we take ownership of the pointer and don't store it, we must | |
1091 | // free it now | |
39bcce60 | 1092 | wxSafeDecRef(attr); |
758cbedf VZ |
1093 | } |
1094 | } | |
1095 | ||
aa5e1f75 SN |
1096 | bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos), |
1097 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 1098 | { |
f6bcfd97 | 1099 | wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); |
8f177c8e | 1100 | |
ca65c044 | 1101 | return false; |
f85afd4e MB |
1102 | } |
1103 | ||
aa5e1f75 | 1104 | bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) ) |
f85afd4e | 1105 | { |
f6bcfd97 | 1106 | wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); |
8f177c8e | 1107 | |
ca65c044 | 1108 | return false; |
f85afd4e MB |
1109 | } |
1110 | ||
aa5e1f75 SN |
1111 | bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos), |
1112 | size_t WXUNUSED(numRows) ) | |
f85afd4e | 1113 | { |
f6bcfd97 | 1114 | wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); |
8f177c8e | 1115 | |
ca65c044 | 1116 | return false; |
f85afd4e MB |
1117 | } |
1118 | ||
aa5e1f75 SN |
1119 | bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos), |
1120 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 1121 | { |
f6bcfd97 | 1122 | wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); |
8f177c8e | 1123 | |
ca65c044 | 1124 | return false; |
f85afd4e MB |
1125 | } |
1126 | ||
aa5e1f75 | 1127 | bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) ) |
f85afd4e | 1128 | { |
f6bcfd97 | 1129 | wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); |
8f177c8e | 1130 | |
ca65c044 | 1131 | return false; |
f85afd4e MB |
1132 | } |
1133 | ||
aa5e1f75 SN |
1134 | bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos), |
1135 | size_t WXUNUSED(numCols) ) | |
f85afd4e | 1136 | { |
f6bcfd97 | 1137 | wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); |
8f177c8e | 1138 | |
ca65c044 | 1139 | return false; |
f85afd4e MB |
1140 | } |
1141 | ||
f85afd4e MB |
1142 | wxString wxGridTableBase::GetRowLabelValue( int row ) |
1143 | { | |
1144 | wxString s; | |
93763ad5 | 1145 | |
2f024384 DS |
1146 | // RD: Starting the rows at zero confuses users, |
1147 | // no matter how much it makes sense to us geeks. | |
1148 | s << row + 1; | |
1149 | ||
f85afd4e MB |
1150 | return s; |
1151 | } | |
1152 | ||
1153 | wxString wxGridTableBase::GetColLabelValue( int col ) | |
1154 | { | |
1155 | // default col labels are: | |
1156 | // cols 0 to 25 : A-Z | |
1157 | // cols 26 to 675 : AA-ZZ | |
1158 | // etc. | |
1159 | ||
1160 | wxString s; | |
1161 | unsigned int i, n; | |
1162 | for ( n = 1; ; n++ ) | |
1163 | { | |
9a83f860 | 1164 | s += (wxChar) (wxT('A') + (wxChar)(col % 26)); |
2f024384 | 1165 | col = col / 26 - 1; |
4db6714b KH |
1166 | if ( col < 0 ) |
1167 | break; | |
f85afd4e MB |
1168 | } |
1169 | ||
1170 | // reverse the string... | |
1171 | wxString s2; | |
3d59537f | 1172 | for ( i = 0; i < n; i++ ) |
f85afd4e | 1173 | { |
2f024384 | 1174 | s2 += s[n - i - 1]; |
f85afd4e MB |
1175 | } |
1176 | ||
1177 | return s2; | |
1178 | } | |
1179 | ||
f2d76237 RD |
1180 | wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) ) |
1181 | { | |
816be743 | 1182 | return wxGRID_VALUE_STRING; |
f2d76237 RD |
1183 | } |
1184 | ||
1185 | bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col), | |
1186 | const wxString& typeName ) | |
1187 | { | |
816be743 | 1188 | return typeName == wxGRID_VALUE_STRING; |
f2d76237 RD |
1189 | } |
1190 | ||
1191 | bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName ) | |
1192 | { | |
1193 | return CanGetValueAs(row, col, typeName); | |
1194 | } | |
1195 | ||
1196 | long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) ) | |
1197 | { | |
1198 | return 0; | |
1199 | } | |
1200 | ||
1201 | double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) ) | |
1202 | { | |
1203 | return 0.0; | |
1204 | } | |
1205 | ||
1206 | bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) ) | |
1207 | { | |
ca65c044 | 1208 | return false; |
f2d76237 RD |
1209 | } |
1210 | ||
1211 | void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col), | |
1212 | long WXUNUSED(value) ) | |
1213 | { | |
1214 | } | |
1215 | ||
1216 | void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col), | |
1217 | double WXUNUSED(value) ) | |
1218 | { | |
1219 | } | |
1220 | ||
1221 | void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col), | |
1222 | bool WXUNUSED(value) ) | |
1223 | { | |
1224 | } | |
1225 | ||
f2d76237 RD |
1226 | void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), |
1227 | const wxString& WXUNUSED(typeName) ) | |
1228 | { | |
1229 | return NULL; | |
1230 | } | |
1231 | ||
1232 | void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), | |
1233 | const wxString& WXUNUSED(typeName), | |
1234 | void* WXUNUSED(value) ) | |
1235 | { | |
1236 | } | |
1237 | ||
f85afd4e MB |
1238 | ////////////////////////////////////////////////////////////////////// |
1239 | // | |
1240 | // Message class for the grid table to send requests and notifications | |
1241 | // to the grid view | |
1242 | // | |
1243 | ||
1244 | wxGridTableMessage::wxGridTableMessage() | |
1245 | { | |
10a4531d | 1246 | m_table = NULL; |
f85afd4e MB |
1247 | m_id = -1; |
1248 | m_comInt1 = -1; | |
1249 | m_comInt2 = -1; | |
1250 | } | |
1251 | ||
1252 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, | |
1253 | int commandInt1, int commandInt2 ) | |
1254 | { | |
1255 | m_table = table; | |
1256 | m_id = id; | |
1257 | m_comInt1 = commandInt1; | |
1258 | m_comInt2 = commandInt2; | |
1259 | } | |
1260 | ||
f85afd4e MB |
1261 | ////////////////////////////////////////////////////////////////////// |
1262 | // | |
1263 | // A basic grid table for string data. An object of this class will | |
1264 | // created by wxGrid if you don't specify an alternative table class. | |
1265 | // | |
1266 | ||
223d09f6 | 1267 | WX_DEFINE_OBJARRAY(wxGridStringArray) |
f85afd4e MB |
1268 | |
1269 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) | |
1270 | ||
1271 | wxGridStringTable::wxGridStringTable() | |
1272 | : wxGridTableBase() | |
1273 | { | |
5c3313a9 | 1274 | m_numCols = 0; |
f85afd4e MB |
1275 | } |
1276 | ||
1277 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) | |
1278 | : wxGridTableBase() | |
1279 | { | |
5c3313a9 VZ |
1280 | m_numCols = numCols; |
1281 | ||
f85afd4e MB |
1282 | m_data.Alloc( numRows ); |
1283 | ||
1284 | wxArrayString sa; | |
1285 | sa.Alloc( numCols ); | |
27f35b66 | 1286 | sa.Add( wxEmptyString, numCols ); |
8f177c8e | 1287 | |
27f35b66 | 1288 | m_data.Add( sa, numRows ); |
f85afd4e MB |
1289 | } |
1290 | ||
f85afd4e MB |
1291 | wxString wxGridStringTable::GetValue( int row, int col ) |
1292 | { | |
57e9abd6 VZ |
1293 | wxCHECK_MSG( (row >= 0 && row < GetNumberRows()) && |
1294 | (col >= 0 && col < GetNumberCols()), | |
3e13956a | 1295 | wxEmptyString, |
9a83f860 | 1296 | wxT("invalid row or column index in wxGridStringTable") ); |
af547d51 | 1297 | |
f85afd4e MB |
1298 | return m_data[row][col]; |
1299 | } | |
1300 | ||
f2d76237 | 1301 | void wxGridStringTable::SetValue( int row, int col, const wxString& value ) |
f85afd4e | 1302 | { |
57e9abd6 VZ |
1303 | wxCHECK_RET( (row >= 0 && row < GetNumberRows()) && |
1304 | (col >= 0 && col < GetNumberCols()), | |
9a83f860 | 1305 | wxT("invalid row or column index in wxGridStringTable") ); |
af547d51 | 1306 | |
f2d76237 | 1307 | m_data[row][col] = value; |
f85afd4e MB |
1308 | } |
1309 | ||
f85afd4e MB |
1310 | void wxGridStringTable::Clear() |
1311 | { | |
1312 | int row, col; | |
1313 | int numRows, numCols; | |
8f177c8e | 1314 | |
f85afd4e MB |
1315 | numRows = m_data.GetCount(); |
1316 | if ( numRows > 0 ) | |
1317 | { | |
1318 | numCols = m_data[0].GetCount(); | |
1319 | ||
3d59537f | 1320 | for ( row = 0; row < numRows; row++ ) |
f85afd4e | 1321 | { |
3d59537f | 1322 | for ( col = 0; col < numCols; col++ ) |
f85afd4e MB |
1323 | { |
1324 | m_data[row][col] = wxEmptyString; | |
1325 | } | |
1326 | } | |
1327 | } | |
1328 | } | |
1329 | ||
f85afd4e MB |
1330 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) |
1331 | { | |
f85afd4e | 1332 | size_t curNumRows = m_data.GetCount(); |
f6bcfd97 BP |
1333 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
1334 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 1335 | |
f85afd4e MB |
1336 | if ( pos >= curNumRows ) |
1337 | { | |
1338 | return AppendRows( numRows ); | |
1339 | } | |
8f177c8e | 1340 | |
f85afd4e MB |
1341 | wxArrayString sa; |
1342 | sa.Alloc( curNumCols ); | |
27f35b66 SN |
1343 | sa.Add( wxEmptyString, curNumCols ); |
1344 | m_data.Insert( sa, pos, numRows ); | |
2f024384 | 1345 | |
f85afd4e MB |
1346 | if ( GetView() ) |
1347 | { | |
1348 | wxGridTableMessage msg( this, | |
1349 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, | |
1350 | pos, | |
1351 | numRows ); | |
8f177c8e | 1352 | |
f85afd4e MB |
1353 | GetView()->ProcessTableMessage( msg ); |
1354 | } | |
1355 | ||
ca65c044 | 1356 | return true; |
f85afd4e MB |
1357 | } |
1358 | ||
1359 | bool wxGridStringTable::AppendRows( size_t numRows ) | |
1360 | { | |
f85afd4e | 1361 | size_t curNumRows = m_data.GetCount(); |
4db6714b KH |
1362 | size_t curNumCols = ( curNumRows > 0 |
1363 | ? m_data[0].GetCount() | |
1364 | : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 1365 | |
f85afd4e MB |
1366 | wxArrayString sa; |
1367 | if ( curNumCols > 0 ) | |
1368 | { | |
1369 | sa.Alloc( curNumCols ); | |
27f35b66 | 1370 | sa.Add( wxEmptyString, curNumCols ); |
f85afd4e | 1371 | } |
8f177c8e | 1372 | |
27f35b66 | 1373 | m_data.Add( sa, numRows ); |
f85afd4e MB |
1374 | |
1375 | if ( GetView() ) | |
1376 | { | |
1377 | wxGridTableMessage msg( this, | |
1378 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, | |
1379 | numRows ); | |
8f177c8e | 1380 | |
f85afd4e MB |
1381 | GetView()->ProcessTableMessage( msg ); |
1382 | } | |
1383 | ||
ca65c044 | 1384 | return true; |
f85afd4e MB |
1385 | } |
1386 | ||
1387 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) | |
1388 | { | |
f85afd4e | 1389 | size_t curNumRows = m_data.GetCount(); |
8f177c8e | 1390 | |
f85afd4e MB |
1391 | if ( pos >= curNumRows ) |
1392 | { | |
e91d2033 VZ |
1393 | wxFAIL_MSG( wxString::Format |
1394 | ( | |
1395 | wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), | |
1396 | (unsigned long)pos, | |
1397 | (unsigned long)numRows, | |
1398 | (unsigned long)curNumRows | |
1399 | ) ); | |
1400 | ||
ca65c044 | 1401 | return false; |
f85afd4e MB |
1402 | } |
1403 | ||
1404 | if ( numRows > curNumRows - pos ) | |
1405 | { | |
1406 | numRows = curNumRows - pos; | |
1407 | } | |
8f177c8e | 1408 | |
f85afd4e MB |
1409 | if ( numRows >= curNumRows ) |
1410 | { | |
d57ad377 | 1411 | m_data.Clear(); |
f85afd4e MB |
1412 | } |
1413 | else | |
1414 | { | |
27f35b66 | 1415 | m_data.RemoveAt( pos, numRows ); |
f85afd4e | 1416 | } |
4db6714b | 1417 | |
f85afd4e MB |
1418 | if ( GetView() ) |
1419 | { | |
1420 | wxGridTableMessage msg( this, | |
1421 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, | |
1422 | pos, | |
1423 | numRows ); | |
8f177c8e | 1424 | |
f85afd4e MB |
1425 | GetView()->ProcessTableMessage( msg ); |
1426 | } | |
1427 | ||
ca65c044 | 1428 | return true; |
f85afd4e MB |
1429 | } |
1430 | ||
1431 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) | |
1432 | { | |
1433 | size_t row, col; | |
1434 | ||
1435 | size_t curNumRows = m_data.GetCount(); | |
4db6714b KH |
1436 | size_t curNumCols = ( curNumRows > 0 |
1437 | ? m_data[0].GetCount() | |
1438 | : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 1439 | |
f85afd4e MB |
1440 | if ( pos >= curNumCols ) |
1441 | { | |
1442 | return AppendCols( numCols ); | |
1443 | } | |
1444 | ||
d4175745 VZ |
1445 | if ( !m_colLabels.IsEmpty() ) |
1446 | { | |
1447 | m_colLabels.Insert( wxEmptyString, pos, numCols ); | |
1448 | ||
1449 | size_t i; | |
1450 | for ( i = pos; i < pos + numCols; i++ ) | |
1451 | m_colLabels[i] = wxGridTableBase::GetColLabelValue( i ); | |
1452 | } | |
1453 | ||
3d59537f | 1454 | for ( row = 0; row < curNumRows; row++ ) |
f85afd4e | 1455 | { |
3d59537f | 1456 | for ( col = pos; col < pos + numCols; col++ ) |
f85afd4e MB |
1457 | { |
1458 | m_data[row].Insert( wxEmptyString, col ); | |
1459 | } | |
1460 | } | |
4db6714b | 1461 | |
5c3313a9 VZ |
1462 | m_numCols += numCols; |
1463 | ||
f85afd4e MB |
1464 | if ( GetView() ) |
1465 | { | |
1466 | wxGridTableMessage msg( this, | |
1467 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, | |
1468 | pos, | |
1469 | numCols ); | |
8f177c8e | 1470 | |
f85afd4e MB |
1471 | GetView()->ProcessTableMessage( msg ); |
1472 | } | |
1473 | ||
ca65c044 | 1474 | return true; |
f85afd4e MB |
1475 | } |
1476 | ||
1477 | bool wxGridStringTable::AppendCols( size_t numCols ) | |
1478 | { | |
27f35b66 | 1479 | size_t row; |
f85afd4e MB |
1480 | |
1481 | size_t curNumRows = m_data.GetCount(); | |
2f024384 | 1482 | |
3d59537f | 1483 | for ( row = 0; row < curNumRows; row++ ) |
f85afd4e | 1484 | { |
27f35b66 | 1485 | m_data[row].Add( wxEmptyString, numCols ); |
f85afd4e MB |
1486 | } |
1487 | ||
5c3313a9 VZ |
1488 | m_numCols += numCols; |
1489 | ||
f85afd4e MB |
1490 | if ( GetView() ) |
1491 | { | |
1492 | wxGridTableMessage msg( this, | |
1493 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, | |
1494 | numCols ); | |
8f177c8e | 1495 | |
f85afd4e MB |
1496 | GetView()->ProcessTableMessage( msg ); |
1497 | } | |
1498 | ||
ca65c044 | 1499 | return true; |
f85afd4e MB |
1500 | } |
1501 | ||
1502 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) | |
1503 | { | |
27f35b66 | 1504 | size_t row; |
f85afd4e MB |
1505 | |
1506 | size_t curNumRows = m_data.GetCount(); | |
f6bcfd97 BP |
1507 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
1508 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); | |
8f177c8e | 1509 | |
f85afd4e MB |
1510 | if ( pos >= curNumCols ) |
1511 | { | |
e91d2033 VZ |
1512 | wxFAIL_MSG( wxString::Format |
1513 | ( | |
1514 | wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), | |
1515 | (unsigned long)pos, | |
1516 | (unsigned long)numCols, | |
1517 | (unsigned long)curNumCols | |
1518 | ) ); | |
ca65c044 | 1519 | return false; |
f85afd4e MB |
1520 | } |
1521 | ||
d4175745 VZ |
1522 | int colID; |
1523 | if ( GetView() ) | |
1524 | colID = GetView()->GetColAt( pos ); | |
1525 | else | |
1526 | colID = pos; | |
1527 | ||
1528 | if ( numCols > curNumCols - colID ) | |
1529 | { | |
1530 | numCols = curNumCols - colID; | |
1531 | } | |
1532 | ||
1533 | if ( !m_colLabels.IsEmpty() ) | |
f85afd4e | 1534 | { |
b2df5ddf VZ |
1535 | // m_colLabels stores just as many elements as it needs, e.g. if only |
1536 | // the label of the first column had been set it would have only one | |
1537 | // element and not numCols, so account for it | |
d32fb5f9 | 1538 | int numRemaining = m_colLabels.size() - colID; |
84484a8d RD |
1539 | if (numRemaining > 0) |
1540 | m_colLabels.RemoveAt( colID, wxMin(numCols, numRemaining) ); | |
f85afd4e MB |
1541 | } |
1542 | ||
5c3313a9 | 1543 | if ( numCols >= curNumCols ) |
f85afd4e | 1544 | { |
5c3313a9 | 1545 | for ( row = 0; row < curNumRows; row++ ) |
f85afd4e | 1546 | { |
dcdce64e | 1547 | m_data[row].Clear(); |
f85afd4e | 1548 | } |
5c3313a9 VZ |
1549 | |
1550 | m_numCols = 0; | |
1551 | } | |
1552 | else // something will be left | |
1553 | { | |
1554 | for ( row = 0; row < curNumRows; row++ ) | |
f85afd4e | 1555 | { |
d4175745 | 1556 | m_data[row].RemoveAt( colID, numCols ); |
f85afd4e | 1557 | } |
5c3313a9 VZ |
1558 | |
1559 | m_numCols -= numCols; | |
f85afd4e | 1560 | } |
4db6714b | 1561 | |
f85afd4e MB |
1562 | if ( GetView() ) |
1563 | { | |
1564 | wxGridTableMessage msg( this, | |
1565 | wxGRIDTABLE_NOTIFY_COLS_DELETED, | |
1566 | pos, | |
1567 | numCols ); | |
8f177c8e | 1568 | |
f85afd4e MB |
1569 | GetView()->ProcessTableMessage( msg ); |
1570 | } | |
1571 | ||
ca65c044 | 1572 | return true; |
f85afd4e MB |
1573 | } |
1574 | ||
1575 | wxString wxGridStringTable::GetRowLabelValue( int row ) | |
1576 | { | |
1577 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
1578 | { | |
1579 | // using default label | |
1580 | // | |
1581 | return wxGridTableBase::GetRowLabelValue( row ); | |
1582 | } | |
1583 | else | |
1584 | { | |
2f024384 | 1585 | return m_rowLabels[row]; |
f85afd4e MB |
1586 | } |
1587 | } | |
1588 | ||
1589 | wxString wxGridStringTable::GetColLabelValue( int col ) | |
1590 | { | |
1591 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
1592 | { | |
1593 | // using default label | |
1594 | // | |
1595 | return wxGridTableBase::GetColLabelValue( col ); | |
1596 | } | |
1597 | else | |
1598 | { | |
2f024384 | 1599 | return m_colLabels[col]; |
f85afd4e MB |
1600 | } |
1601 | } | |
1602 | ||
1603 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) | |
1604 | { | |
1605 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
1606 | { | |
1607 | int n = m_rowLabels.GetCount(); | |
1608 | int i; | |
2f024384 | 1609 | |
3d59537f | 1610 | for ( i = n; i <= row; i++ ) |
f85afd4e MB |
1611 | { |
1612 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | m_rowLabels[row] = value; | |
1617 | } | |
1618 | ||
1619 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) | |
1620 | { | |
1621 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
1622 | { | |
1623 | int n = m_colLabels.GetCount(); | |
1624 | int i; | |
2f024384 | 1625 | |
3d59537f | 1626 | for ( i = n; i <= col; i++ ) |
f85afd4e MB |
1627 | { |
1628 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); | |
1629 | } | |
1630 | } | |
1631 | ||
1632 | m_colLabels[col] = value; | |
1633 | } | |
1634 | ||
1635 | ||
f85afd4e | 1636 | ////////////////////////////////////////////////////////////////////// |
2d66e025 MB |
1637 | ////////////////////////////////////////////////////////////////////// |
1638 | ||
86033c4b VZ |
1639 | BEGIN_EVENT_TABLE(wxGridSubwindow, wxWindow) |
1640 | EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost) | |
1641 | END_EVENT_TABLE() | |
1642 | ||
1643 | void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event)) | |
1644 | { | |
1645 | m_owner->CancelMouseCapture(); | |
1646 | } | |
1647 | ||
86033c4b | 1648 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxGridSubwindow ) |
2d66e025 | 1649 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) |
a9339fe2 | 1650 | EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel ) |
2d66e025 | 1651 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) |
2d66e025 MB |
1652 | END_EVENT_TABLE() |
1653 | ||
aa5e1f75 | 1654 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
1655 | { |
1656 | wxPaintDC dc(this); | |
1657 | ||
1658 | // NO - don't do this because it will set both the x and y origin | |
1659 | // coords to match the parent scrolled window and we just want to | |
1660 | // set the y coord - MB | |
1661 | // | |
1662 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 1663 | |
790ad94f | 1664 | int x, y; |
2d66e025 | 1665 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
615b7e6a RR |
1666 | wxPoint pt = dc.GetDeviceOrigin(); |
1667 | dc.SetDeviceOrigin( pt.x, pt.y-y ); | |
60ff3b99 | 1668 | |
d10f4bf9 | 1669 | wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); |
a9339fe2 | 1670 | m_owner->DrawRowLabels( dc, rows ); |
2d66e025 MB |
1671 | } |
1672 | ||
2d66e025 MB |
1673 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
1674 | { | |
1675 | m_owner->ProcessRowLabelMouseEvent( event ); | |
1676 | } | |
1677 | ||
b51c3f27 RD |
1678 | void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
1679 | { | |
b5e9cbb9 FM |
1680 | if (!m_owner->GetEventHandler()->ProcessEvent( event )) |
1681 | event.Skip(); | |
b51c3f27 RD |
1682 | } |
1683 | ||
2d66e025 MB |
1684 | ////////////////////////////////////////////////////////////////////// |
1685 | ||
86033c4b | 1686 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxGridSubwindow ) |
2d66e025 | 1687 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) |
a9339fe2 | 1688 | EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel ) |
2d66e025 | 1689 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) |
2d66e025 MB |
1690 | END_EVENT_TABLE() |
1691 | ||
aa5e1f75 | 1692 | void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
2d66e025 MB |
1693 | { |
1694 | wxPaintDC dc(this); | |
1695 | ||
1696 | // NO - don't do this because it will set both the x and y origin | |
1697 | // coords to match the parent scrolled window and we just want to | |
1698 | // set the x coord - MB | |
1699 | // | |
1700 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 1701 | |
790ad94f | 1702 | int x, y; |
2d66e025 | 1703 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
615b7e6a RR |
1704 | wxPoint pt = dc.GetDeviceOrigin(); |
1705 | if (GetLayoutDirection() == wxLayout_RightToLeft) | |
1706 | dc.SetDeviceOrigin( pt.x+x, pt.y ); | |
1707 | else | |
1708 | dc.SetDeviceOrigin( pt.x-x, pt.y ); | |
2d66e025 | 1709 | |
d10f4bf9 | 1710 | wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() ); |
a9339fe2 | 1711 | m_owner->DrawColLabels( dc, cols ); |
2d66e025 MB |
1712 | } |
1713 | ||
2d66e025 MB |
1714 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
1715 | { | |
1716 | m_owner->ProcessColLabelMouseEvent( event ); | |
1717 | } | |
1718 | ||
b51c3f27 RD |
1719 | void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
1720 | { | |
b5e9cbb9 FM |
1721 | if (!m_owner->GetEventHandler()->ProcessEvent( event )) |
1722 | event.Skip(); | |
b51c3f27 RD |
1723 | } |
1724 | ||
2d66e025 MB |
1725 | ////////////////////////////////////////////////////////////////////// |
1726 | ||
86033c4b | 1727 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxGridSubwindow ) |
a9339fe2 | 1728 | EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel ) |
2d66e025 | 1729 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) |
a9339fe2 | 1730 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint ) |
2d66e025 MB |
1731 | END_EVENT_TABLE() |
1732 | ||
d2fdd8d2 RR |
1733 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
1734 | { | |
1735 | wxPaintDC dc(this); | |
b99be8fb | 1736 | |
ff72f628 | 1737 | m_owner->DrawCornerLabel(dc); |
d2fdd8d2 RR |
1738 | } |
1739 | ||
2d66e025 MB |
1740 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
1741 | { | |
1742 | m_owner->ProcessCornerLabelMouseEvent( event ); | |
1743 | } | |
1744 | ||
b51c3f27 RD |
1745 | void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
1746 | { | |
b5e9cbb9 FM |
1747 | if (!m_owner->GetEventHandler()->ProcessEvent(event)) |
1748 | event.Skip(); | |
b51c3f27 RD |
1749 | } |
1750 | ||
f85afd4e MB |
1751 | ////////////////////////////////////////////////////////////////////// |
1752 | ||
86033c4b | 1753 | BEGIN_EVENT_TABLE( wxGridWindow, wxGridSubwindow ) |
2d66e025 | 1754 | EVT_PAINT( wxGridWindow::OnPaint ) |
a9339fe2 | 1755 | EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel ) |
2d66e025 MB |
1756 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) |
1757 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) | |
f6bcfd97 | 1758 | EVT_KEY_UP( wxGridWindow::OnKeyUp ) |
a9339fe2 | 1759 | EVT_CHAR( wxGridWindow::OnChar ) |
80acaf25 JS |
1760 | EVT_SET_FOCUS( wxGridWindow::OnFocus ) |
1761 | EVT_KILL_FOCUS( wxGridWindow::OnFocus ) | |
2796cce3 | 1762 | EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground ) |
2d66e025 MB |
1763 | END_EVENT_TABLE() |
1764 | ||
2d66e025 MB |
1765 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
1766 | { | |
1767 | wxPaintDC dc( this ); | |
1768 | m_owner->PrepareDC( dc ); | |
796df70a | 1769 | wxRegion reg = GetUpdateRegion(); |
ccdee36f DS |
1770 | wxGridCellCoordsArray dirtyCells = m_owner->CalcCellsExposed( reg ); |
1771 | m_owner->DrawGridCellArea( dc, dirtyCells ); | |
2f024384 | 1772 | |
9f7aee01 VZ |
1773 | m_owner->DrawGridSpace( dc ); |
1774 | ||
796df70a | 1775 | m_owner->DrawAllGridLines( dc, reg ); |
2f024384 | 1776 | |
ccdee36f | 1777 | m_owner->DrawHighlight( dc, dirtyCells ); |
2d66e025 MB |
1778 | } |
1779 | ||
779e28da VZ |
1780 | void wxGrid::Render( wxDC& dc, |
1781 | const wxPoint& position, | |
1782 | const wxSize& size, | |
1783 | const wxGridCellCoords& topLeft, | |
1784 | const wxGridCellCoords& bottomRight, | |
1785 | int style ) | |
1786 | { | |
1787 | wxCHECK_RET( bottomRight.GetCol() < GetNumberCols(), | |
1788 | "Invalid right column" ); | |
1789 | wxCHECK_RET( bottomRight.GetRow() < GetNumberRows(), | |
1790 | "Invalid bottom row" ); | |
1791 | ||
1792 | // store user settings and reset later | |
1793 | ||
1794 | // remove grid selection, don't paint selection colour | |
1795 | // unless we have wxGRID_DRAW_SELECTION | |
1796 | // block selections are the only ones catered for here | |
1797 | wxGridCellCoordsArray selectedCells; | |
1798 | bool hasSelection = IsSelection(); | |
1799 | if ( hasSelection && !( style & wxGRID_DRAW_SELECTION ) ) | |
1800 | { | |
1801 | selectedCells = GetSelectionBlockTopLeft(); | |
1802 | // non block selections may not have a bottom right | |
1803 | if ( GetSelectionBlockBottomRight().size() ) | |
1804 | selectedCells.Add( GetSelectionBlockBottomRight()[ 0 ] ); | |
1805 | ||
1806 | ClearSelection(); | |
1807 | } | |
1808 | ||
1809 | // store user device origin | |
1810 | wxCoord userOriginX, userOriginY; | |
1811 | dc.GetDeviceOrigin( &userOriginX, &userOriginY ); | |
1812 | ||
1813 | // store user scale | |
1814 | double scaleUserX, scaleUserY; | |
1815 | dc.GetUserScale( &scaleUserX, &scaleUserY ); | |
1816 | ||
1817 | // set defaults if necessary | |
f00acdb8 VZ |
1818 | wxGridCellCoords leftTop( topLeft ), rightBottom( bottomRight ); |
1819 | if ( leftTop.GetCol() < 0 ) | |
1820 | leftTop.SetCol(0); | |
1821 | if ( leftTop.GetRow() < 0 ) | |
1822 | leftTop.SetRow(0); | |
1823 | if ( rightBottom.GetCol() < 0 ) | |
1824 | rightBottom.SetCol(GetNumberCols() - 1); | |
1825 | if ( rightBottom.GetRow() < 0 ) | |
1826 | rightBottom.SetRow(GetNumberRows() - 1); | |
1827 | ||
1828 | // get grid offset, size and cell parameters | |
1829 | wxPoint pointOffSet; | |
1830 | wxSize sizeGrid; | |
779e28da VZ |
1831 | wxGridCellCoordsArray renderCells; |
1832 | wxArrayInt arrayCols; | |
1833 | wxArrayInt arrayRows; | |
779e28da | 1834 | |
f00acdb8 VZ |
1835 | GetRenderSizes( leftTop, rightBottom, |
1836 | pointOffSet, sizeGrid, | |
1837 | renderCells, | |
1838 | arrayCols, arrayRows ); | |
779e28da VZ |
1839 | |
1840 | // add headers/labels to dimensions | |
1841 | if ( style & wxGRID_DRAW_ROWS_HEADER ) | |
f00acdb8 | 1842 | sizeGrid.x += GetRowLabelSize(); |
779e28da | 1843 | if ( style & wxGRID_DRAW_COLS_HEADER ) |
f00acdb8 | 1844 | sizeGrid.y += GetColLabelSize(); |
779e28da | 1845 | |
f00acdb8 VZ |
1846 | // get render start position in logical units |
1847 | wxPoint positionRender = GetRenderPosition( dc, position ); | |
779e28da | 1848 | |
779e28da VZ |
1849 | wxCoord originX = dc.LogicalToDeviceX( positionRender.x ); |
1850 | wxCoord originY = dc.LogicalToDeviceY( positionRender.y ); | |
1851 | ||
1852 | dc.SetDeviceOrigin( originX, originY ); | |
1853 | ||
f00acdb8 | 1854 | SetRenderScale( dc, positionRender, size, sizeGrid ); |
779e28da VZ |
1855 | |
1856 | // draw row headers at specified origin | |
1857 | if ( GetRowLabelSize() > 0 && ( style & wxGRID_DRAW_ROWS_HEADER ) ) | |
1858 | { | |
1859 | if ( style & wxGRID_DRAW_COLS_HEADER ) | |
1860 | { | |
1861 | DrawCornerLabel( dc ); // do only if both col and row labels drawn | |
1862 | originY += dc.LogicalToDeviceYRel( GetColLabelSize() ); | |
1863 | } | |
1864 | ||
f00acdb8 | 1865 | originY -= dc.LogicalToDeviceYRel( pointOffSet.y ); |
779e28da VZ |
1866 | dc.SetDeviceOrigin( originX, originY ); |
1867 | ||
1868 | DrawRowLabels( dc, arrayRows ); | |
1869 | ||
1870 | // reset for columns | |
1871 | if ( style & wxGRID_DRAW_COLS_HEADER ) | |
1872 | originY -= dc.LogicalToDeviceYRel( GetColLabelSize() ); | |
1873 | ||
f00acdb8 | 1874 | originY += dc.LogicalToDeviceYRel( pointOffSet.y ); |
779e28da VZ |
1875 | // X offset so we don't overwrite row labels |
1876 | originX += dc.LogicalToDeviceXRel( GetRowLabelSize() ); | |
1877 | } | |
1878 | ||
1879 | // subtract col offset where startcol > 0 | |
f00acdb8 | 1880 | originX -= dc.LogicalToDeviceXRel( pointOffSet.x ); |
779e28da VZ |
1881 | // no y offset for col labels, they are at the Y origin |
1882 | ||
1883 | // draw column labels | |
1884 | if ( style & wxGRID_DRAW_COLS_HEADER ) | |
1885 | { | |
1886 | dc.SetDeviceOrigin( originX, originY ); | |
1887 | DrawColLabels( dc, arrayCols ); | |
1888 | // don't overwrite the labels, increment originY | |
1889 | originY += dc.LogicalToDeviceYRel( GetColLabelSize() ); | |
1890 | } | |
1891 | ||
1892 | // set device origin to draw grid cells and lines | |
f00acdb8 | 1893 | originY -= dc.LogicalToDeviceYRel( pointOffSet.y ); |
779e28da VZ |
1894 | dc.SetDeviceOrigin( originX, originY ); |
1895 | ||
1896 | // draw cell area background | |
1897 | dc.SetBrush( GetDefaultCellBackgroundColour() ); | |
1898 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
1899 | // subtract headers from grid area dimensions | |
f00acdb8 | 1900 | wxSize sizeCells( sizeGrid ); |
779e28da | 1901 | if ( style & wxGRID_DRAW_ROWS_HEADER ) |
f00acdb8 | 1902 | sizeCells.x -= GetRowLabelSize(); |
779e28da | 1903 | if ( style & wxGRID_DRAW_COLS_HEADER ) |
f00acdb8 | 1904 | sizeCells.y -= GetColLabelSize(); |
779e28da | 1905 | |
f00acdb8 | 1906 | dc.DrawRectangle( pointOffSet, sizeCells ); |
779e28da VZ |
1907 | |
1908 | // draw cells | |
1909 | DrawGridCellArea( dc, renderCells ); | |
1910 | ||
f00acdb8 | 1911 | // draw grid lines |
779e28da VZ |
1912 | if ( style & wxGRID_DRAW_CELL_LINES ) |
1913 | { | |
f00acdb8 VZ |
1914 | wxRegion regionClip( pointOffSet.x, pointOffSet.y, |
1915 | sizeCells.x, sizeCells.y ); | |
779e28da VZ |
1916 | |
1917 | DrawRangeGridLines(dc, regionClip, renderCells[0], renderCells.Last()); | |
1918 | } | |
1919 | ||
1920 | // draw render rectangle bounding lines | |
f00acdb8 VZ |
1921 | DoRenderBox( dc, style, |
1922 | pointOffSet, sizeCells, | |
1923 | leftTop, rightBottom ); | |
779e28da VZ |
1924 | |
1925 | // restore user setings | |
1926 | dc.SetDeviceOrigin( userOriginX, userOriginY ); | |
1927 | dc.SetUserScale( scaleUserX, scaleUserY ); | |
1928 | ||
1929 | if ( selectedCells.size() && !( style & wxGRID_DRAW_SELECTION ) ) | |
1930 | { | |
1931 | SelectBlock( selectedCells[ 0 ].GetRow(), | |
1932 | selectedCells[ 0 ].GetCol(), | |
1933 | selectedCells[ selectedCells.size() -1 ].GetRow(), | |
1934 | selectedCells[ selectedCells.size() -1 ].GetCol() ); | |
1935 | } | |
1936 | } | |
1937 | ||
1938 | void | |
1939 | wxGrid::SetRenderScale(wxDC& dc, | |
1940 | const wxPoint& pos, const wxSize& size, | |
f00acdb8 | 1941 | const wxSize& sizeGrid ) |
779e28da VZ |
1942 | { |
1943 | double scaleX, scaleY; | |
1944 | wxSize sizeTemp; | |
1945 | ||
8e2c6c3f VZ |
1946 | if ( size.GetWidth() != wxDefaultSize.GetWidth() ) // size.x was specified |
1947 | sizeTemp.SetWidth( size.GetWidth() ); | |
779e28da | 1948 | else |
8e2c6c3f | 1949 | sizeTemp.SetWidth( dc.DeviceToLogicalXRel( dc.GetSize().GetWidth() ) |
779e28da VZ |
1950 | - pos.x ); |
1951 | ||
8e2c6c3f VZ |
1952 | if ( size.GetHeight() != wxDefaultSize.GetHeight() ) // size.y was specified |
1953 | sizeTemp.SetHeight( size.GetHeight() ); | |
779e28da | 1954 | else |
8e2c6c3f | 1955 | sizeTemp.SetHeight( dc.DeviceToLogicalYRel( dc.GetSize().GetHeight() ) |
779e28da VZ |
1956 | - pos.y ); |
1957 | ||
8e2c6c3f VZ |
1958 | scaleX = (double)( (double) sizeTemp.GetWidth() / (double) sizeGrid.GetWidth() ); |
1959 | scaleY = (double)( (double) sizeTemp.GetHeight() / (double) sizeGrid.GetHeight() ); | |
779e28da VZ |
1960 | |
1961 | dc.SetUserScale( wxMin( scaleX, scaleY), wxMin( scaleX, scaleY ) ); | |
1962 | } | |
1963 | ||
f00acdb8 VZ |
1964 | // get grid rendered size, origin offset and fill cell arrays |
1965 | void wxGrid::GetRenderSizes( const wxGridCellCoords& topLeft, | |
1966 | const wxGridCellCoords& bottomRight, | |
1967 | wxPoint& pointOffSet, wxSize& sizeGrid, | |
1968 | wxGridCellCoordsArray& renderCells, | |
1969 | wxArrayInt& arrayCols, wxArrayInt& arrayRows ) | |
1970 | { | |
1971 | pointOffSet.x = 0; | |
1972 | pointOffSet.y = 0; | |
1973 | sizeGrid.SetWidth( 0 ); | |
1974 | sizeGrid.SetHeight( 0 ); | |
1975 | ||
1976 | int col, row; | |
1977 | ||
1978 | wxGridSizesInfo sizeinfo = GetColSizes(); | |
1979 | for ( col = 0; col <= bottomRight.GetCol(); col++ ) | |
1980 | { | |
1981 | if ( col < topLeft.GetCol() ) | |
1982 | { | |
1983 | pointOffSet.x += sizeinfo.GetSize( col ); | |
1984 | } | |
1985 | else | |
1986 | { | |
1987 | for ( row = topLeft.GetRow(); row <= bottomRight.GetRow(); row++ ) | |
1988 | { | |
1989 | renderCells.Add( wxGridCellCoords( row, col )); | |
1990 | arrayRows.Add( row ); // column labels rendered in DrawColLabels | |
1991 | } | |
1992 | arrayCols.Add( col ); // row labels rendered in DrawRowLabels | |
1993 | sizeGrid.x += sizeinfo.GetSize( col ); | |
1994 | } | |
1995 | } | |
1996 | ||
1997 | sizeinfo = GetRowSizes(); | |
1998 | for ( row = 0; row <= bottomRight.GetRow(); row++ ) | |
1999 | { | |
2000 | if ( row < topLeft.GetRow() ) | |
2001 | pointOffSet.y += sizeinfo.GetSize( row ); | |
2002 | else | |
2003 | sizeGrid.y += sizeinfo.GetSize( row ); | |
2004 | } | |
2005 | } | |
2006 | ||
2007 | // get render start position | |
2008 | // if position not specified use dc draw extents MaxX and MaxY | |
2009 | wxPoint wxGrid::GetRenderPosition( wxDC& dc, const wxPoint& position ) | |
2010 | { | |
2011 | wxPoint positionRender( position ); | |
2012 | ||
2013 | if ( !positionRender.IsFullySpecified() ) | |
2014 | { | |
2015 | if ( positionRender.x == wxDefaultPosition.x ) | |
2016 | positionRender.x = dc.MaxX(); | |
2017 | ||
2018 | if ( positionRender.y == wxDefaultPosition.y ) | |
2019 | positionRender.y = dc.MaxY(); | |
2020 | } | |
2021 | ||
2022 | return positionRender; | |
2023 | } | |
2024 | ||
2025 | // draw render rectangle bounding lines | |
2026 | // useful where there is multi cell row or col clipping and no cell border | |
2027 | void wxGrid::DoRenderBox( wxDC& dc, const int& style, | |
2028 | const wxPoint& pointOffSet, | |
2029 | const wxSize& sizeCells, | |
2030 | const wxGridCellCoords& topLeft, | |
2031 | const wxGridCellCoords& bottomRight ) | |
2032 | { | |
2033 | if ( !( style & wxGRID_DRAW_BOX_RECT ) ) | |
2034 | return; | |
2035 | ||
2036 | int bottom = pointOffSet.y + sizeCells.GetY(), | |
2037 | right = pointOffSet.x + sizeCells.GetX() - 1; | |
2038 | ||
2039 | // horiz top line if we are not drawing column header/labels | |
2040 | if ( !( style & wxGRID_DRAW_COLS_HEADER ) ) | |
2041 | { | |
2042 | int left = pointOffSet.x; | |
2043 | left += ( style & wxGRID_DRAW_COLS_HEADER ) | |
2044 | ? - GetRowLabelSize() : 0; | |
2045 | dc.SetPen( GetRowGridLinePen( topLeft.GetRow() ) ); | |
2046 | dc.DrawLine( left, | |
2047 | pointOffSet.y, | |
2048 | right, | |
2049 | pointOffSet.y ); | |
2050 | } | |
2051 | ||
2052 | // horiz bottom line | |
2053 | dc.SetPen( GetRowGridLinePen( bottomRight.GetRow() ) ); | |
2054 | dc.DrawLine( pointOffSet.x, bottom - 1, right, bottom - 1 ); | |
2055 | ||
2056 | // left vertical line if we are not drawing row header/labels | |
2057 | if ( !( style & wxGRID_DRAW_ROWS_HEADER ) ) | |
2058 | { | |
2059 | int top = pointOffSet.y; | |
2060 | top += ( style & wxGRID_DRAW_COLS_HEADER ) | |
2061 | ? - GetColLabelSize() : 0; | |
2062 | dc.SetPen( GetColGridLinePen( topLeft.GetCol() ) ); | |
2063 | dc.DrawLine( pointOffSet.x -1, | |
2064 | top, | |
2065 | pointOffSet.x - 1, | |
2066 | bottom - 1 ); | |
2067 | } | |
2068 | ||
2069 | // right vertical line | |
2070 | dc.SetPen( GetColGridLinePen( bottomRight.GetCol() ) ); | |
2071 | dc.DrawLine( right, pointOffSet.y, right, bottom - 1 ); | |
2072 | } | |
2073 | ||
2d66e025 MB |
2074 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) |
2075 | { | |
59ddac01 | 2076 | wxWindow::ScrollWindow( dx, dy, rect ); |
ad805b9e VZ |
2077 | m_owner->GetGridRowLabelWindow()->ScrollWindow( 0, dy, rect ); |
2078 | m_owner->GetGridColLabelWindow()->ScrollWindow( dx, 0, rect ); | |
2d66e025 MB |
2079 | } |
2080 | ||
2d66e025 MB |
2081 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) |
2082 | { | |
33e9fc54 RD |
2083 | if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this) |
2084 | SetFocus(); | |
902725ee | 2085 | |
2d66e025 MB |
2086 | m_owner->ProcessGridCellMouseEvent( event ); |
2087 | } | |
2088 | ||
b51c3f27 RD |
2089 | void wxGridWindow::OnMouseWheel( wxMouseEvent& event ) |
2090 | { | |
b5e9cbb9 FM |
2091 | if (!m_owner->GetEventHandler()->ProcessEvent( event )) |
2092 | event.Skip(); | |
b51c3f27 | 2093 | } |
2d66e025 | 2094 | |
f6bcfd97 | 2095 | // This seems to be required for wxMotif/wxGTK otherwise the mouse |
2d66e025 MB |
2096 | // cursor must be in the cell edit control to get key events |
2097 | // | |
2098 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) | |
2099 | { | |
4db6714b KH |
2100 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
2101 | event.Skip(); | |
2d66e025 | 2102 | } |
f85afd4e | 2103 | |
f6bcfd97 BP |
2104 | void wxGridWindow::OnKeyUp( wxKeyEvent& event ) |
2105 | { | |
4db6714b KH |
2106 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
2107 | event.Skip(); | |
f6bcfd97 | 2108 | } |
7c8a8ad5 | 2109 | |
63e2147c RD |
2110 | void wxGridWindow::OnChar( wxKeyEvent& event ) |
2111 | { | |
4db6714b KH |
2112 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
2113 | event.Skip(); | |
63e2147c RD |
2114 | } |
2115 | ||
aa5e1f75 | 2116 | void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) |
8dd4f536 | 2117 | { |
8dd4f536 | 2118 | } |
025562fe | 2119 | |
80acaf25 JS |
2120 | void wxGridWindow::OnFocus(wxFocusEvent& event) |
2121 | { | |
760be3f7 VS |
2122 | // and if we have any selection, it has to be repainted, because it |
2123 | // uses different colour when the grid is not focused: | |
2124 | if ( m_owner->IsSelection() ) | |
2125 | { | |
2126 | Refresh(); | |
2127 | } | |
2128 | else | |
2129 | { | |
2130 | // NB: Note that this code is in "else" branch only because the other | |
2131 | // branch refreshes everything and so there's no point in calling | |
2132 | // Refresh() again, *not* because it should only be done if | |
2133 | // !IsSelection(). If the above code is ever optimized to refresh | |
2134 | // only selected area, this needs to be moved out of the "else" | |
2135 | // branch so that it's always executed. | |
2136 | ||
2137 | // current cell cursor {dis,re}appears on focus change: | |
2138 | const wxGridCellCoords cursorCoords(m_owner->GetGridCursorRow(), | |
2139 | m_owner->GetGridCursorCol()); | |
2140 | const wxRect cursor = | |
2141 | m_owner->BlockToDeviceRect(cursorCoords, cursorCoords); | |
2142 | Refresh(true, &cursor); | |
2143 | } | |
2144 | ||
80acaf25 JS |
2145 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
2146 | event.Skip(); | |
2147 | } | |
2d66e025 | 2148 | |
d4175745 | 2149 | #define internalXToCol(x) XToCol(x, true) |
bec70262 | 2150 | #define internalYToRow(y) YToRow(y, true) |
ccdee36f | 2151 | |
33188aa4 | 2152 | ///////////////////////////////////////////////////////////////////// |
07296f0b | 2153 | |
2d66e025 | 2154 | BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow ) |
f85afd4e MB |
2155 | EVT_PAINT( wxGrid::OnPaint ) |
2156 | EVT_SIZE( wxGrid::OnSize ) | |
f85afd4e | 2157 | EVT_KEY_DOWN( wxGrid::OnKeyDown ) |
f6bcfd97 | 2158 | EVT_KEY_UP( wxGrid::OnKeyUp ) |
63e2147c | 2159 | EVT_CHAR ( wxGrid::OnChar ) |
2796cce3 | 2160 | EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground ) |
add6e919 | 2161 | EVT_COMMAND(wxID_ANY, wxEVT_GRID_HIDE_EDITOR, wxGrid::OnHideEditor ) |
f85afd4e | 2162 | END_EVENT_TABLE() |
8f177c8e | 2163 | |
b0a877ec SC |
2164 | bool wxGrid::Create(wxWindow *parent, wxWindowID id, |
2165 | const wxPoint& pos, const wxSize& size, | |
2166 | long style, const wxString& name) | |
2167 | { | |
2168 | if (!wxScrolledWindow::Create(parent, id, pos, size, | |
c2f5b920 | 2169 | style | wxWANTS_CHARS, name)) |
ca65c044 | 2170 | return false; |
b0a877ec | 2171 | |
2f024384 DS |
2172 | m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE); |
2173 | m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE); | |
b0a877ec | 2174 | |
2f024384 | 2175 | Create(); |
170acdc9 | 2176 | SetInitialSize(size); |
b93aafab | 2177 | CalcDimensions(); |
b0a877ec | 2178 | |
ca65c044 | 2179 | return true; |
b0a877ec SC |
2180 | } |
2181 | ||
58dd5b3b MB |
2182 | wxGrid::~wxGrid() |
2183 | { | |
e882d288 VZ |
2184 | if ( m_winCapture ) |
2185 | m_winCapture->ReleaseMouse(); | |
2186 | ||
b09b3048 VZ |
2187 | // Ensure that the editor control is destroyed before the grid is, |
2188 | // otherwise we crash later when the editor tries to do something with the | |
2189 | // half destroyed grid | |
2190 | HideCellEditControl(); | |
2191 | ||
606b005f JS |
2192 | // Must do this or ~wxScrollHelper will pop the wrong event handler |
2193 | SetTargetWindow(this); | |
0a976765 | 2194 | ClearAttrCache(); |
39bcce60 | 2195 | wxSafeDecRef(m_defaultCellAttr); |
0a976765 VZ |
2196 | |
2197 | #ifdef DEBUG_ATTR_CACHE | |
2198 | size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses; | |
9a83f860 | 2199 | wxPrintf(wxT("wxGrid attribute cache statistics: " |
0a976765 VZ |
2200 | "total: %u, hits: %u (%u%%)\n"), |
2201 | total, gs_nAttrCacheHits, | |
2202 | total ? (gs_nAttrCacheHits*100) / total : 0); | |
2203 | #endif | |
2204 | ||
86020f7e VZ |
2205 | // if we own the table, just delete it, otherwise at least don't leave it |
2206 | // with dangling view pointer | |
2207 | if ( m_ownTable ) | |
2796cce3 | 2208 | delete m_table; |
ecc7aa82 | 2209 | else if ( m_table && m_table->GetView() == this ) |
86020f7e | 2210 | m_table->SetView(NULL); |
f2d76237 RD |
2211 | |
2212 | delete m_typeRegistry; | |
b5808881 | 2213 | delete m_selection; |
82edfbe7 VZ |
2214 | |
2215 | delete m_setFixedRows; | |
2216 | delete m_setFixedCols; | |
58dd5b3b MB |
2217 | } |
2218 | ||
58dd5b3b MB |
2219 | // |
2220 | // ----- internal init and update functions | |
2221 | // | |
2222 | ||
9950649c RD |
2223 | // NOTE: If using the default visual attributes works everywhere then this can |
2224 | // be removed as well as the #else cases below. | |
2225 | #define _USE_VISATTR 0 | |
2226 | ||
58dd5b3b | 2227 | void wxGrid::Create() |
f0102d2a | 2228 | { |
3d59537f DS |
2229 | // create the type registry |
2230 | m_typeRegistry = new wxGridTypeRegistry; | |
2c9a89e0 | 2231 | |
ca65c044 | 2232 | m_cellEditCtrlEnabled = false; |
4634a5d6 | 2233 | |
ccd970b1 | 2234 | m_defaultCellAttr = new wxGridCellAttr(); |
f2d76237 RD |
2235 | |
2236 | // Set default cell attributes | |
ccd970b1 | 2237 | m_defaultCellAttr->SetDefAttr(m_defaultCellAttr); |
19d7140e | 2238 | m_defaultCellAttr->SetKind(wxGridCellAttr::Default); |
f2d76237 | 2239 | m_defaultCellAttr->SetFont(GetFont()); |
4c7277db | 2240 | m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP); |
9950649c RD |
2241 | m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer); |
2242 | m_defaultCellAttr->SetEditor(new wxGridCellTextEditor); | |
2243 | ||
2244 | #if _USE_VISATTR | |
2245 | wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes(); | |
2246 | wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes(); | |
2247 | ||
2248 | m_defaultCellAttr->SetTextColour(gva.colFg); | |
2249 | m_defaultCellAttr->SetBackgroundColour(gva.colBg); | |
ca65c044 | 2250 | |
9950649c | 2251 | #else |
f2d76237 | 2252 | m_defaultCellAttr->SetTextColour( |
a756f210 | 2253 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); |
f2d76237 | 2254 | m_defaultCellAttr->SetBackgroundColour( |
a756f210 | 2255 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
9950649c | 2256 | #endif |
2796cce3 | 2257 | |
4634a5d6 MB |
2258 | m_numRows = 0; |
2259 | m_numCols = 0; | |
2260 | m_currentCellCoords = wxGridNoCellCoords; | |
b99be8fb | 2261 | |
f2d76237 | 2262 | // subwindow components that make up the wxGrid |
ad805b9e VZ |
2263 | m_rowLabelWin = new wxGridRowLabelWindow(this); |
2264 | CreateColumnWindow(); | |
2265 | m_cornerLabelWin = new wxGridCornerLabelWindow(this); | |
2266 | m_gridWin = new wxGridWindow( this ); | |
2d66e025 MB |
2267 | |
2268 | SetTargetWindow( m_gridWin ); | |
6f36917b | 2269 | |
9950649c RD |
2270 | #if _USE_VISATTR |
2271 | wxColour gfg = gva.colFg; | |
2272 | wxColour gbg = gva.colBg; | |
2273 | wxColour lfg = lva.colFg; | |
2274 | wxColour lbg = lva.colBg; | |
2275 | #else | |
2276 | wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
2277 | wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ); | |
2278 | wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
2279 | wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ); | |
2280 | #endif | |
2f024384 | 2281 | |
fa47d7a7 VS |
2282 | m_cornerLabelWin->SetOwnForegroundColour(lfg); |
2283 | m_cornerLabelWin->SetOwnBackgroundColour(lbg); | |
2284 | m_rowLabelWin->SetOwnForegroundColour(lfg); | |
2285 | m_rowLabelWin->SetOwnBackgroundColour(lbg); | |
ad805b9e VZ |
2286 | m_colWindow->SetOwnForegroundColour(lfg); |
2287 | m_colWindow->SetOwnBackgroundColour(lbg); | |
fa47d7a7 VS |
2288 | |
2289 | m_gridWin->SetOwnForegroundColour(gfg); | |
2290 | m_gridWin->SetOwnBackgroundColour(gbg); | |
ca65c044 | 2291 | |
ad805b9e VZ |
2292 | m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour(); |
2293 | m_labelTextColour = m_rowLabelWin->GetForegroundColour(); | |
2294 | ||
2295 | // now that we have the grid window, use its font to compute the default | |
2296 | // row height | |
2297 | m_defaultRowHeight = m_gridWin->GetCharHeight(); | |
2298 | #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl() | |
2299 | m_defaultRowHeight += 8; | |
2300 | #else | |
2301 | m_defaultRowHeight += 4; | |
2302 | #endif | |
2303 | ||
2304 | } | |
2305 | ||
2306 | void wxGrid::CreateColumnWindow() | |
2307 | { | |
2308 | if ( m_useNativeHeader ) | |
2309 | { | |
2310 | m_colWindow = new wxGridHeaderCtrl(this); | |
2311 | m_colLabelHeight = m_colWindow->GetBestSize().y; | |
2312 | } | |
2313 | else // draw labels ourselves | |
2314 | { | |
2315 | m_colWindow = new wxGridColLabelWindow(this); | |
2316 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
2317 | } | |
2d66e025 | 2318 | } |
f85afd4e | 2319 | |
b5808881 | 2320 | bool wxGrid::CreateGrid( int numRows, int numCols, |
6b992f7d | 2321 | wxGridSelectionModes selmode ) |
2d66e025 | 2322 | { |
f6bcfd97 | 2323 | wxCHECK_MSG( !m_created, |
ca65c044 | 2324 | false, |
f6bcfd97 BP |
2325 | wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); |
2326 | ||
6b992f7d | 2327 | return SetTable(new wxGridStringTable(numRows, numCols), true, selmode); |
2796cce3 RD |
2328 | } |
2329 | ||
6b992f7d | 2330 | void wxGrid::SetSelectionMode(wxGridSelectionModes selmode) |
f1567cdd | 2331 | { |
6f36917b VZ |
2332 | wxCHECK_RET( m_created, |
2333 | wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") ); | |
2334 | ||
2335 | m_selection->SetSelectionMode( selmode ); | |
f1567cdd SN |
2336 | } |
2337 | ||
aa5b8857 SN |
2338 | wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const |
2339 | { | |
6b992f7d | 2340 | wxCHECK_MSG( m_created, wxGridSelectCells, |
aa5b8857 SN |
2341 | wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") ); |
2342 | ||
2343 | return m_selection->GetSelectionMode(); | |
2344 | } | |
2345 | ||
6b992f7d VZ |
2346 | bool |
2347 | wxGrid::SetTable(wxGridTableBase *table, | |
2348 | bool takeOwnership, | |
2349 | wxGrid::wxGridSelectionModes selmode ) | |
2796cce3 | 2350 | { |
a7dde52f | 2351 | bool checkSelection = false; |
2796cce3 RD |
2352 | if ( m_created ) |
2353 | { | |
3e13956a | 2354 | // stop all processing |
ca65c044 | 2355 | m_created = false; |
86c7378f | 2356 | |
c71b2126 | 2357 | if (m_table) |
86c7378f | 2358 | { |
a7dde52f SN |
2359 | m_table->SetView(0); |
2360 | if( m_ownTable ) | |
2361 | delete m_table; | |
5b061713 | 2362 | m_table = NULL; |
86c7378f | 2363 | } |
2f024384 | 2364 | |
5276b0a5 | 2365 | wxDELETE(m_selection); |
a7dde52f SN |
2366 | |
2367 | m_ownTable = false; | |
2f024384 DS |
2368 | m_numRows = 0; |
2369 | m_numCols = 0; | |
a7dde52f SN |
2370 | checkSelection = true; |
2371 | ||
2372 | // kill row and column size arrays | |
2373 | m_colWidths.Empty(); | |
2374 | m_colRights.Empty(); | |
2375 | m_rowHeights.Empty(); | |
2376 | m_rowBottoms.Empty(); | |
233a54f6 | 2377 | } |
2f024384 | 2378 | |
233a54f6 | 2379 | if (table) |
2796cce3 RD |
2380 | { |
2381 | m_numRows = table->GetNumberRows(); | |
2382 | m_numCols = table->GetNumberCols(); | |
2383 | ||
ad805b9e | 2384 | if ( m_useNativeHeader ) |
3039ade9 | 2385 | GetGridColHeader()->SetColumnCount(m_numCols); |
ad805b9e | 2386 | |
2796cce3 RD |
2387 | m_table = table; |
2388 | m_table->SetView( this ); | |
8fc856de | 2389 | m_ownTable = takeOwnership; |
043d16b2 | 2390 | m_selection = new wxGridSelection( this, selmode ); |
a7dde52f SN |
2391 | if (checkSelection) |
2392 | { | |
2393 | // If the newly set table is smaller than the | |
2394 | // original one current cell and selection regions | |
2395 | // might be invalid, | |
8a3e536c | 2396 | m_selectedBlockCorner = wxGridNoCellCoords; |
c71b2126 | 2397 | m_currentCellCoords = |
a7dde52f SN |
2398 | wxGridCellCoords(wxMin(m_numRows, m_currentCellCoords.GetRow()), |
2399 | wxMin(m_numCols, m_currentCellCoords.GetCol())); | |
8a3e536c VZ |
2400 | if (m_selectedBlockTopLeft.GetRow() >= m_numRows || |
2401 | m_selectedBlockTopLeft.GetCol() >= m_numCols) | |
a7dde52f | 2402 | { |
8a3e536c VZ |
2403 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
2404 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
a7dde52f SN |
2405 | } |
2406 | else | |
8a3e536c | 2407 | m_selectedBlockBottomRight = |
a7dde52f | 2408 | wxGridCellCoords(wxMin(m_numRows, |
8a3e536c | 2409 | m_selectedBlockBottomRight.GetRow()), |
a7dde52f | 2410 | wxMin(m_numCols, |
8a3e536c | 2411 | m_selectedBlockBottomRight.GetCol())); |
a7dde52f | 2412 | } |
6f36917b VZ |
2413 | CalcDimensions(); |
2414 | ||
ca65c044 | 2415 | m_created = true; |
2d66e025 | 2416 | } |
f85afd4e | 2417 | |
1f89d869 VZ |
2418 | InvalidateBestSize(); |
2419 | ||
2d66e025 | 2420 | return m_created; |
f85afd4e MB |
2421 | } |
2422 | ||
ad805b9e | 2423 | void wxGrid::Init() |
f9549841 VZ |
2424 | { |
2425 | m_created = false; | |
2426 | ||
2427 | m_cornerLabelWin = NULL; | |
2428 | m_rowLabelWin = NULL; | |
ad805b9e | 2429 | m_colWindow = NULL; |
f9549841 VZ |
2430 | m_gridWin = NULL; |
2431 | ||
2432 | m_table = NULL; | |
2433 | m_ownTable = false; | |
2434 | ||
2435 | m_selection = NULL; | |
2436 | m_defaultCellAttr = NULL; | |
2437 | m_typeRegistry = NULL; | |
2438 | m_winCapture = NULL; | |
f9549841 | 2439 | |
f85afd4e MB |
2440 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; |
2441 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
2442 | ||
82edfbe7 VZ |
2443 | m_setFixedRows = |
2444 | m_setFixedCols = NULL; | |
2445 | ||
0a976765 VZ |
2446 | // init attr cache |
2447 | m_attrCache.row = -1; | |
2b5f62a0 VZ |
2448 | m_attrCache.col = -1; |
2449 | m_attrCache.attr = NULL; | |
0a976765 | 2450 | |
ff72f628 | 2451 | m_labelFont = GetFont(); |
52d6f640 | 2452 | m_labelFont.SetWeight( wxBOLD ); |
8f177c8e | 2453 | |
73145b0e | 2454 | m_rowLabelHorizAlign = wxALIGN_CENTRE; |
4c7277db | 2455 | m_rowLabelVertAlign = wxALIGN_CENTRE; |
f85afd4e | 2456 | |
4c7277db | 2457 | m_colLabelHorizAlign = wxALIGN_CENTRE; |
73145b0e | 2458 | m_colLabelVertAlign = wxALIGN_CENTRE; |
d43851f7 | 2459 | m_colLabelTextOrientation = wxHORIZONTAL; |
f85afd4e | 2460 | |
f85afd4e | 2461 | m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH; |
ad805b9e | 2462 | m_defaultRowHeight = 0; // this will be initialized after creation |
1f1ce288 | 2463 | |
b8d24d4e RG |
2464 | m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH; |
2465 | m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT; | |
2466 | ||
73145b0e | 2467 | m_gridLineColour = wxColour( 192,192,192 ); |
ca65c044 | 2468 | m_gridLinesEnabled = true; |
9f7aee01 VZ |
2469 | m_gridLinesClipHorz = |
2470 | m_gridLinesClipVert = true; | |
73145b0e | 2471 | m_cellHighlightColour = *wxBLACK; |
bf7945ce | 2472 | m_cellHighlightPenWidth = 2; |
d2520c85 | 2473 | m_cellHighlightROPenWidth = 1; |
8f177c8e | 2474 | |
d4175745 VZ |
2475 | m_canDragColMove = false; |
2476 | ||
58dd5b3b | 2477 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
10a4531d | 2478 | m_winCapture = NULL; |
ca65c044 WS |
2479 | m_canDragRowSize = true; |
2480 | m_canDragColSize = true; | |
2481 | m_canDragGridSize = true; | |
79dbea21 | 2482 | m_canDragCell = false; |
f85afd4e MB |
2483 | m_dragLastPos = -1; |
2484 | m_dragRowOrCol = -1; | |
ca65c044 | 2485 | m_isDragging = false; |
07296f0b | 2486 | m_startDragPos = wxDefaultPosition; |
ad805b9e | 2487 | |
11393d29 VZ |
2488 | m_sortCol = wxNOT_FOUND; |
2489 | m_sortIsAscending = true; | |
2490 | ||
ad805b9e | 2491 | m_useNativeHeader = |
71cf399f | 2492 | m_nativeColumnLabels = false; |
f85afd4e | 2493 | |
ca65c044 | 2494 | m_waitForSlowClick = false; |
025562fe | 2495 | |
f85afd4e MB |
2496 | m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS ); |
2497 | m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE ); | |
2498 | ||
2499 | m_currentCellCoords = wxGridNoCellCoords; | |
f85afd4e | 2500 | |
ad805b9e VZ |
2501 | m_selectedBlockTopLeft = |
2502 | m_selectedBlockBottomRight = | |
2503 | m_selectedBlockCorner = wxGridNoCellCoords; | |
b524b5c6 | 2504 | |
d43851f7 JS |
2505 | m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); |
2506 | m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
8f177c8e | 2507 | |
ca65c044 | 2508 | m_editable = true; // default for whole grid |
f85afd4e | 2509 | |
ca65c044 | 2510 | m_inOnKeyDown = false; |
2d66e025 | 2511 | m_batchCount = 0; |
3c79cf49 | 2512 | |
266e8367 | 2513 | m_extraWidth = |
526dbb95 | 2514 | m_extraHeight = 0; |
608754c4 | 2515 | |
fd76c6a7 VZ |
2516 | // we can't call SetScrollRate() as the window isn't created yet but OTOH |
2517 | // we don't need to call it neither as the scroll position is (0, 0) right | |
2518 | // now anyhow, so just set the parameters directly | |
2519 | m_xScrollPixelsPerLine = GRID_SCROLL_LINE_X; | |
2520 | m_yScrollPixelsPerLine = GRID_SCROLL_LINE_Y; | |
1dc17bca VZ |
2521 | |
2522 | m_tabBehaviour = Tab_Stop; | |
7c1cb261 VZ |
2523 | } |
2524 | ||
2525 | // ---------------------------------------------------------------------------- | |
2526 | // the idea is to call these functions only when necessary because they create | |
2527 | // quite big arrays which eat memory mostly unnecessary - in particular, if | |
2528 | // default widths/heights are used for all rows/columns, we may not use these | |
2529 | // arrays at all | |
2530 | // | |
0ed3b812 VZ |
2531 | // with some extra code, it should be possible to only store the widths/heights |
2532 | // different from default ones (resulting in space savings for huge grids) but | |
2533 | // this is not done currently | |
7c1cb261 VZ |
2534 | // ---------------------------------------------------------------------------- |
2535 | ||
2536 | void wxGrid::InitRowHeights() | |
2537 | { | |
2538 | m_rowHeights.Empty(); | |
2539 | m_rowBottoms.Empty(); | |
2540 | ||
2541 | m_rowHeights.Alloc( m_numRows ); | |
2542 | m_rowBottoms.Alloc( m_numRows ); | |
2543 | ||
27f35b66 SN |
2544 | m_rowHeights.Add( m_defaultRowHeight, m_numRows ); |
2545 | ||
0ed3b812 | 2546 | int rowBottom = 0; |
3d59537f | 2547 | for ( int i = 0; i < m_numRows; i++ ) |
7c1cb261 | 2548 | { |
7c1cb261 VZ |
2549 | rowBottom += m_defaultRowHeight; |
2550 | m_rowBottoms.Add( rowBottom ); | |
2551 | } | |
2552 | } | |
2553 | ||
2554 | void wxGrid::InitColWidths() | |
2555 | { | |
2556 | m_colWidths.Empty(); | |
2557 | m_colRights.Empty(); | |
2558 | ||
2559 | m_colWidths.Alloc( m_numCols ); | |
2560 | m_colRights.Alloc( m_numCols ); | |
27f35b66 SN |
2561 | |
2562 | m_colWidths.Add( m_defaultColWidth, m_numCols ); | |
2563 | ||
3d59537f | 2564 | for ( int i = 0; i < m_numCols; i++ ) |
7c1cb261 | 2565 | { |
e822d1bd | 2566 | int colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth; |
7c1cb261 VZ |
2567 | m_colRights.Add( colRight ); |
2568 | } | |
2569 | } | |
2570 | ||
2571 | int wxGrid::GetColWidth(int col) const | |
2572 | { | |
30496905 VZ |
2573 | if ( m_colWidths.IsEmpty() ) |
2574 | return m_defaultColWidth; | |
2575 | ||
2576 | // a negative width indicates a hidden column | |
2577 | return m_colWidths[col] > 0 ? m_colWidths[col] : 0; | |
7c1cb261 VZ |
2578 | } |
2579 | ||
2580 | int wxGrid::GetColLeft(int col) const | |
2581 | { | |
30496905 VZ |
2582 | if ( m_colRights.IsEmpty() ) |
2583 | return GetColPos( col ) * m_defaultColWidth; | |
2584 | ||
2585 | return m_colRights[col] - GetColWidth(col); | |
7c1cb261 VZ |
2586 | } |
2587 | ||
2588 | int wxGrid::GetColRight(int col) const | |
2589 | { | |
d4175745 | 2590 | return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth |
7c1cb261 VZ |
2591 | : m_colRights[col]; |
2592 | } | |
2593 | ||
2594 | int wxGrid::GetRowHeight(int row) const | |
2595 | { | |
30496905 VZ |
2596 | // no custom heights / hidden rows |
2597 | if ( m_rowHeights.IsEmpty() ) | |
2598 | return m_defaultRowHeight; | |
2599 | ||
2600 | // a negative height indicates a hidden row | |
2601 | return m_rowHeights[row] > 0 ? m_rowHeights[row] : 0; | |
7c1cb261 | 2602 | } |
2d66e025 | 2603 | |
7c1cb261 VZ |
2604 | int wxGrid::GetRowTop(int row) const |
2605 | { | |
30496905 VZ |
2606 | if ( m_rowBottoms.IsEmpty() ) |
2607 | return row * m_defaultRowHeight; | |
2608 | ||
2609 | return m_rowBottoms[row] - GetRowHeight(row); | |
f85afd4e MB |
2610 | } |
2611 | ||
7c1cb261 VZ |
2612 | int wxGrid::GetRowBottom(int row) const |
2613 | { | |
2614 | return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight | |
2615 | : m_rowBottoms[row]; | |
2616 | } | |
f85afd4e MB |
2617 | |
2618 | void wxGrid::CalcDimensions() | |
2619 | { | |
0ed3b812 VZ |
2620 | // compute the size of the scrollable area |
2621 | int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0; | |
2622 | int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0; | |
60ff3b99 | 2623 | |
0ed3b812 VZ |
2624 | w += m_extraWidth; |
2625 | h += m_extraHeight; | |
faec5a43 | 2626 | |
73145b0e | 2627 | // take into account editor if shown |
4db6714b | 2628 | if ( IsCellEditControlShown() ) |
73145b0e | 2629 | { |
3d59537f DS |
2630 | int w2, h2; |
2631 | int r = m_currentCellCoords.GetRow(); | |
2632 | int c = m_currentCellCoords.GetCol(); | |
2633 | int x = GetColLeft(c); | |
2634 | int y = GetRowTop(r); | |
2635 | ||
2636 | // how big is the editor | |
2637 | wxGridCellAttr* attr = GetCellAttr(r, c); | |
2638 | wxGridCellEditor* editor = attr->GetEditor(this, r, c); | |
2639 | editor->GetControl()->GetSize(&w2, &h2); | |
2640 | w2 += x; | |
2641 | h2 += y; | |
2642 | if ( w2 > w ) | |
2643 | w = w2; | |
2644 | if ( h2 > h ) | |
2645 | h = h2; | |
2646 | editor->DecRef(); | |
2647 | attr->DecRef(); | |
73145b0e JS |
2648 | } |
2649 | ||
faec5a43 SN |
2650 | // preserve (more or less) the previous position |
2651 | int x, y; | |
2652 | GetViewStart( &x, &y ); | |
97a9929e | 2653 | |
c92ed9f7 | 2654 | // ensure the position is valid for the new scroll ranges |
7b519e5e | 2655 | if ( x >= w ) |
c92ed9f7 | 2656 | x = wxMax( w - 1, 0 ); |
7b519e5e | 2657 | if ( y >= h ) |
c92ed9f7 | 2658 | y = wxMax( h - 1, 0 ); |
faec5a43 | 2659 | |
ace8d849 | 2660 | // update the virtual size and refresh the scrollbars to reflect it |
f1ff7df0 VZ |
2661 | m_gridWin->SetVirtualSize(w, h); |
2662 | Scroll(x, y); | |
ace8d849 | 2663 | AdjustScrollbars(); |
12314291 VZ |
2664 | |
2665 | // if our OnSize() hadn't been called (it would if we have scrollbars), we | |
2666 | // still must reposition the children | |
2667 | CalcWindowSizes(); | |
f85afd4e MB |
2668 | } |
2669 | ||
69367c56 VZ |
2670 | wxSize wxGrid::GetSizeAvailableForScrollTarget(const wxSize& size) |
2671 | { | |
2672 | wxSize sizeGridWin(size); | |
2673 | sizeGridWin.x -= m_rowLabelWidth; | |
2674 | sizeGridWin.y -= m_colLabelHeight; | |
2675 | ||
2676 | return sizeGridWin; | |
2677 | } | |
2678 | ||
7807d81c MB |
2679 | void wxGrid::CalcWindowSizes() |
2680 | { | |
b0a877ec SC |
2681 | // escape if the window is has not been fully created yet |
2682 | ||
2683 | if ( m_cornerLabelWin == NULL ) | |
2f024384 | 2684 | return; |
b0a877ec | 2685 | |
7807d81c MB |
2686 | int cw, ch; |
2687 | GetClientSize( &cw, &ch ); | |
b99be8fb | 2688 | |
c1841ac2 VZ |
2689 | // the grid may be too small to have enough space for the labels yet, don't |
2690 | // size the windows to negative sizes in this case | |
2691 | int gw = cw - m_rowLabelWidth; | |
2692 | int gh = ch - m_colLabelHeight; | |
2693 | if (gw < 0) | |
2694 | gw = 0; | |
2695 | if (gh < 0) | |
2696 | gh = 0; | |
2697 | ||
6d308072 | 2698 | if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() ) |
7807d81c MB |
2699 | m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight ); |
2700 | ||
ad805b9e VZ |
2701 | if ( m_colWindow && m_colWindow->IsShown() ) |
2702 | m_colWindow->SetSize( m_rowLabelWidth, 0, gw, m_colLabelHeight ); | |
7807d81c | 2703 | |
6d308072 | 2704 | if ( m_rowLabelWin && m_rowLabelWin->IsShown() ) |
c1841ac2 | 2705 | m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, gh ); |
7807d81c | 2706 | |
6d308072 | 2707 | if ( m_gridWin && m_gridWin->IsShown() ) |
c1841ac2 | 2708 | m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, gw, gh ); |
7807d81c MB |
2709 | } |
2710 | ||
3d59537f DS |
2711 | // this is called when the grid table sends a message |
2712 | // to indicate that it has been redimensioned | |
f85afd4e MB |
2713 | // |
2714 | bool wxGrid::Redimension( wxGridTableMessage& msg ) | |
2715 | { | |
2716 | int i; | |
ca65c044 | 2717 | bool result = false; |
8f177c8e | 2718 | |
a6794685 SN |
2719 | // Clear the attribute cache as the attribute might refer to a different |
2720 | // cell than stored in the cache after adding/removing rows/columns. | |
2721 | ClearAttrCache(); | |
2f024384 | 2722 | |
7e48d7d9 SN |
2723 | // By the same reasoning, the editor should be dismissed if columns are |
2724 | // added or removed. And for consistency, it should IMHO always be | |
2725 | // removed, not only if the cell "underneath" it actually changes. | |
2726 | // For now, I intentionally do not save the editor's content as the | |
2727 | // cell it might want to save that stuff to might no longer exist. | |
bca7bfc8 | 2728 | HideCellEditControl(); |
2f024384 | 2729 | |
f85afd4e MB |
2730 | switch ( msg.GetId() ) |
2731 | { | |
2732 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
2733 | { | |
2734 | size_t pos = msg.GetCommandInt(); | |
2735 | int numRows = msg.GetCommandInt2(); | |
f6bcfd97 | 2736 | |
f85afd4e | 2737 | m_numRows += numRows; |
2d66e025 | 2738 | |
f6bcfd97 BP |
2739 | if ( !m_rowHeights.IsEmpty() ) |
2740 | { | |
27f35b66 SN |
2741 | m_rowHeights.Insert( m_defaultRowHeight, pos, numRows ); |
2742 | m_rowBottoms.Insert( 0, pos, numRows ); | |
f6bcfd97 BP |
2743 | |
2744 | int bottom = 0; | |
2f024384 DS |
2745 | if ( pos > 0 ) |
2746 | bottom = m_rowBottoms[pos - 1]; | |
60ff3b99 | 2747 | |
3d59537f | 2748 | for ( i = pos; i < m_numRows; i++ ) |
f6bcfd97 BP |
2749 | { |
2750 | bottom += m_rowHeights[i]; | |
2751 | m_rowBottoms[i] = bottom; | |
2752 | } | |
2753 | } | |
2f024384 | 2754 | |
f6bcfd97 BP |
2755 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2756 | { | |
2757 | // if we have just inserted cols into an empty grid the current | |
2758 | // cell will be undefined... | |
2759 | // | |
2760 | SetCurrentCell( 0, 0 ); | |
2761 | } | |
3f3dc2ef VZ |
2762 | |
2763 | if ( m_selection ) | |
2764 | m_selection->UpdateRows( pos, numRows ); | |
f6bcfd97 BP |
2765 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
2766 | if (attrProvider) | |
2767 | attrProvider->UpdateAttrRows( pos, numRows ); | |
2768 | ||
2769 | if ( !GetBatchCount() ) | |
2d66e025 | 2770 | { |
f6bcfd97 BP |
2771 | CalcDimensions(); |
2772 | m_rowLabelWin->Refresh(); | |
2d66e025 | 2773 | } |
f85afd4e | 2774 | } |
ca65c044 | 2775 | result = true; |
f6bcfd97 | 2776 | break; |
f85afd4e MB |
2777 | |
2778 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
2779 | { | |
2780 | int numRows = msg.GetCommandInt(); | |
2d66e025 | 2781 | int oldNumRows = m_numRows; |
f85afd4e | 2782 | m_numRows += numRows; |
2d66e025 | 2783 | |
f6bcfd97 BP |
2784 | if ( !m_rowHeights.IsEmpty() ) |
2785 | { | |
27f35b66 SN |
2786 | m_rowHeights.Add( m_defaultRowHeight, numRows ); |
2787 | m_rowBottoms.Add( 0, numRows ); | |
60ff3b99 | 2788 | |
f6bcfd97 | 2789 | int bottom = 0; |
2f024384 DS |
2790 | if ( oldNumRows > 0 ) |
2791 | bottom = m_rowBottoms[oldNumRows - 1]; | |
f6bcfd97 | 2792 | |
3d59537f | 2793 | for ( i = oldNumRows; i < m_numRows; i++ ) |
f6bcfd97 BP |
2794 | { |
2795 | bottom += m_rowHeights[i]; | |
2796 | m_rowBottoms[i] = bottom; | |
2797 | } | |
2798 | } | |
2f024384 | 2799 | |
f6bcfd97 BP |
2800 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2801 | { | |
2802 | // if we have just inserted cols into an empty grid the current | |
2803 | // cell will be undefined... | |
2804 | // | |
2805 | SetCurrentCell( 0, 0 ); | |
2806 | } | |
2f024384 | 2807 | |
f6bcfd97 | 2808 | if ( !GetBatchCount() ) |
2d66e025 | 2809 | { |
f6bcfd97 BP |
2810 | CalcDimensions(); |
2811 | m_rowLabelWin->Refresh(); | |
2d66e025 | 2812 | } |
f85afd4e | 2813 | } |
ca65c044 | 2814 | result = true; |
f6bcfd97 | 2815 | break; |
f85afd4e MB |
2816 | |
2817 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
2818 | { | |
2819 | size_t pos = msg.GetCommandInt(); | |
2820 | int numRows = msg.GetCommandInt2(); | |
f85afd4e MB |
2821 | m_numRows -= numRows; |
2822 | ||
f6bcfd97 | 2823 | if ( !m_rowHeights.IsEmpty() ) |
f85afd4e | 2824 | { |
27f35b66 SN |
2825 | m_rowHeights.RemoveAt( pos, numRows ); |
2826 | m_rowBottoms.RemoveAt( pos, numRows ); | |
2d66e025 MB |
2827 | |
2828 | int h = 0; | |
3d59537f | 2829 | for ( i = 0; i < m_numRows; i++ ) |
2d66e025 MB |
2830 | { |
2831 | h += m_rowHeights[i]; | |
2832 | m_rowBottoms[i] = h; | |
2833 | } | |
f85afd4e | 2834 | } |
3d59537f | 2835 | |
f6bcfd97 BP |
2836 | if ( !m_numRows ) |
2837 | { | |
2838 | m_currentCellCoords = wxGridNoCellCoords; | |
2839 | } | |
2840 | else | |
2841 | { | |
2842 | if ( m_currentCellCoords.GetRow() >= m_numRows ) | |
2843 | m_currentCellCoords.Set( 0, 0 ); | |
2844 | } | |
3f3dc2ef VZ |
2845 | |
2846 | if ( m_selection ) | |
2847 | m_selection->UpdateRows( pos, -((int)numRows) ); | |
f6bcfd97 | 2848 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4db6714b KH |
2849 | if (attrProvider) |
2850 | { | |
f6bcfd97 | 2851 | attrProvider->UpdateAttrRows( pos, -((int)numRows) ); |
3d59537f | 2852 | |
84912ef8 RD |
2853 | // ifdef'd out following patch from Paul Gammans |
2854 | #if 0 | |
3ca6a5f0 | 2855 | // No need to touch column attributes, unless we |
f6bcfd97 BP |
2856 | // removed _all_ rows, in this case, we remove |
2857 | // all column attributes. | |
2858 | // I hate to do this here, but the | |
2859 | // needed data is not available inside UpdateAttrRows. | |
2860 | if ( !GetNumberRows() ) | |
2861 | attrProvider->UpdateAttrCols( 0, -GetNumberCols() ); | |
84912ef8 | 2862 | #endif |
f6bcfd97 | 2863 | } |
ccdee36f | 2864 | |
f6bcfd97 BP |
2865 | if ( !GetBatchCount() ) |
2866 | { | |
2867 | CalcDimensions(); | |
2868 | m_rowLabelWin->Refresh(); | |
2869 | } | |
f85afd4e | 2870 | } |
ca65c044 | 2871 | result = true; |
f6bcfd97 | 2872 | break; |
f85afd4e MB |
2873 | |
2874 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
2875 | { | |
2876 | size_t pos = msg.GetCommandInt(); | |
2877 | int numCols = msg.GetCommandInt2(); | |
f85afd4e | 2878 | m_numCols += numCols; |
2d66e025 | 2879 | |
ad805b9e | 2880 | if ( m_useNativeHeader ) |
3039ade9 | 2881 | GetGridColHeader()->SetColumnCount(m_numCols); |
ad805b9e | 2882 | |
d4175745 VZ |
2883 | if ( !m_colAt.IsEmpty() ) |
2884 | { | |
2885 | //Shift the column IDs | |
d4175745 VZ |
2886 | for ( i = 0; i < m_numCols - numCols; i++ ) |
2887 | { | |
2888 | if ( m_colAt[i] >= (int)pos ) | |
2889 | m_colAt[i] += numCols; | |
2890 | } | |
2891 | ||
2892 | m_colAt.Insert( pos, pos, numCols ); | |
2893 | ||
2894 | //Set the new columns' positions | |
2895 | for ( i = pos + 1; i < (int)pos + numCols; i++ ) | |
2896 | { | |
2897 | m_colAt[i] = i; | |
2898 | } | |
2899 | } | |
2900 | ||
f6bcfd97 BP |
2901 | if ( !m_colWidths.IsEmpty() ) |
2902 | { | |
27f35b66 SN |
2903 | m_colWidths.Insert( m_defaultColWidth, pos, numCols ); |
2904 | m_colRights.Insert( 0, pos, numCols ); | |
f6bcfd97 BP |
2905 | |
2906 | int right = 0; | |
2f024384 | 2907 | if ( pos > 0 ) |
d4175745 | 2908 | right = m_colRights[GetColAt( pos - 1 )]; |
60ff3b99 | 2909 | |
d4175745 VZ |
2910 | int colPos; |
2911 | for ( colPos = pos; colPos < m_numCols; colPos++ ) | |
f6bcfd97 | 2912 | { |
d4175745 VZ |
2913 | i = GetColAt( colPos ); |
2914 | ||
f6bcfd97 BP |
2915 | right += m_colWidths[i]; |
2916 | m_colRights[i] = right; | |
2917 | } | |
2918 | } | |
2f024384 | 2919 | |
f6bcfd97 | 2920 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2d66e025 | 2921 | { |
f6bcfd97 BP |
2922 | // if we have just inserted cols into an empty grid the current |
2923 | // cell will be undefined... | |
2924 | // | |
2925 | SetCurrentCell( 0, 0 ); | |
2d66e025 | 2926 | } |
3f3dc2ef VZ |
2927 | |
2928 | if ( m_selection ) | |
2929 | m_selection->UpdateCols( pos, numCols ); | |
f6bcfd97 BP |
2930 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
2931 | if (attrProvider) | |
2932 | attrProvider->UpdateAttrCols( pos, numCols ); | |
2933 | if ( !GetBatchCount() ) | |
2934 | { | |
2935 | CalcDimensions(); | |
ad805b9e | 2936 | m_colWindow->Refresh(); |
f6bcfd97 | 2937 | } |
f85afd4e | 2938 | } |
ca65c044 | 2939 | result = true; |
f6bcfd97 | 2940 | break; |
f85afd4e MB |
2941 | |
2942 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
2943 | { | |
2944 | int numCols = msg.GetCommandInt(); | |
2d66e025 | 2945 | int oldNumCols = m_numCols; |
f85afd4e | 2946 | m_numCols += numCols; |
d4175745 VZ |
2947 | |
2948 | if ( !m_colAt.IsEmpty() ) | |
2949 | { | |
2950 | m_colAt.Add( 0, numCols ); | |
2951 | ||
2952 | //Set the new columns' positions | |
d4175745 VZ |
2953 | for ( i = oldNumCols; i < m_numCols; i++ ) |
2954 | { | |
2955 | m_colAt[i] = i; | |
2956 | } | |
2957 | } | |
2958 | ||
f6bcfd97 BP |
2959 | if ( !m_colWidths.IsEmpty() ) |
2960 | { | |
27f35b66 SN |
2961 | m_colWidths.Add( m_defaultColWidth, numCols ); |
2962 | m_colRights.Add( 0, numCols ); | |
2d66e025 | 2963 | |
f6bcfd97 | 2964 | int right = 0; |
2f024384 | 2965 | if ( oldNumCols > 0 ) |
d4175745 | 2966 | right = m_colRights[GetColAt( oldNumCols - 1 )]; |
60ff3b99 | 2967 | |
d4175745 VZ |
2968 | int colPos; |
2969 | for ( colPos = oldNumCols; colPos < m_numCols; colPos++ ) | |
f6bcfd97 | 2970 | { |
d4175745 VZ |
2971 | i = GetColAt( colPos ); |
2972 | ||
f6bcfd97 BP |
2973 | right += m_colWidths[i]; |
2974 | m_colRights[i] = right; | |
2975 | } | |
2976 | } | |
2f024384 | 2977 | |
a2d65663 VZ |
2978 | // Notice that this must be called after updating m_colWidths above |
2979 | // as the native grid control will check whether the new columns | |
2980 | // are shown which results in accessing m_colWidths array. | |
2981 | if ( m_useNativeHeader ) | |
2982 | GetGridColHeader()->SetColumnCount(m_numCols); | |
2983 | ||
f6bcfd97 | 2984 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2d66e025 | 2985 | { |
f6bcfd97 BP |
2986 | // if we have just inserted cols into an empty grid the current |
2987 | // cell will be undefined... | |
2988 | // | |
2989 | SetCurrentCell( 0, 0 ); | |
2990 | } | |
2991 | if ( !GetBatchCount() ) | |
2992 | { | |
2993 | CalcDimensions(); | |
ad805b9e | 2994 | m_colWindow->Refresh(); |
2d66e025 | 2995 | } |
f85afd4e | 2996 | } |
ca65c044 | 2997 | result = true; |
f6bcfd97 | 2998 | break; |
f85afd4e MB |
2999 | |
3000 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
3001 | { | |
3002 | size_t pos = msg.GetCommandInt(); | |
3003 | int numCols = msg.GetCommandInt2(); | |
f85afd4e | 3004 | m_numCols -= numCols; |
ad805b9e | 3005 | if ( m_useNativeHeader ) |
3039ade9 | 3006 | GetGridColHeader()->SetColumnCount(m_numCols); |
f85afd4e | 3007 | |
d4175745 VZ |
3008 | if ( !m_colAt.IsEmpty() ) |
3009 | { | |
3010 | int colID = GetColAt( pos ); | |
3011 | ||
3012 | m_colAt.RemoveAt( pos, numCols ); | |
3013 | ||
3014 | //Shift the column IDs | |
3015 | int colPos; | |
3016 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
3017 | { | |
3018 | if ( m_colAt[colPos] > colID ) | |
3019 | m_colAt[colPos] -= numCols; | |
3020 | } | |
3021 | } | |
3022 | ||
f6bcfd97 | 3023 | if ( !m_colWidths.IsEmpty() ) |
f85afd4e | 3024 | { |
27f35b66 SN |
3025 | m_colWidths.RemoveAt( pos, numCols ); |
3026 | m_colRights.RemoveAt( pos, numCols ); | |
2d66e025 MB |
3027 | |
3028 | int w = 0; | |
d4175745 VZ |
3029 | int colPos; |
3030 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
2d66e025 | 3031 | { |
d4175745 VZ |
3032 | i = GetColAt( colPos ); |
3033 | ||
2d66e025 MB |
3034 | w += m_colWidths[i]; |
3035 | m_colRights[i] = w; | |
3036 | } | |
f85afd4e | 3037 | } |
2f024384 | 3038 | |
f6bcfd97 BP |
3039 | if ( !m_numCols ) |
3040 | { | |
3041 | m_currentCellCoords = wxGridNoCellCoords; | |
3042 | } | |
3043 | else | |
3044 | { | |
3045 | if ( m_currentCellCoords.GetCol() >= m_numCols ) | |
3046 | m_currentCellCoords.Set( 0, 0 ); | |
3047 | } | |
3f3dc2ef VZ |
3048 | |
3049 | if ( m_selection ) | |
3050 | m_selection->UpdateCols( pos, -((int)numCols) ); | |
f6bcfd97 | 3051 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4db6714b KH |
3052 | if (attrProvider) |
3053 | { | |
f6bcfd97 | 3054 | attrProvider->UpdateAttrCols( pos, -((int)numCols) ); |
ccdee36f | 3055 | |
84912ef8 RD |
3056 | // ifdef'd out following patch from Paul Gammans |
3057 | #if 0 | |
f6bcfd97 BP |
3058 | // No need to touch row attributes, unless we |
3059 | // removed _all_ columns, in this case, we remove | |
3060 | // all row attributes. | |
3061 | // I hate to do this here, but the | |
3062 | // needed data is not available inside UpdateAttrCols. | |
3063 | if ( !GetNumberCols() ) | |
3064 | attrProvider->UpdateAttrRows( 0, -GetNumberRows() ); | |
84912ef8 | 3065 | #endif |
f6bcfd97 | 3066 | } |
ccdee36f | 3067 | |
f6bcfd97 BP |
3068 | if ( !GetBatchCount() ) |
3069 | { | |
3070 | CalcDimensions(); | |
ad805b9e | 3071 | m_colWindow->Refresh(); |
f6bcfd97 | 3072 | } |
f85afd4e | 3073 | } |
ca65c044 | 3074 | result = true; |
f6bcfd97 | 3075 | break; |
f85afd4e MB |
3076 | } |
3077 | ||
a6b40a9a VZ |
3078 | InvalidateBestSize(); |
3079 | ||
f6bcfd97 BP |
3080 | if (result && !GetBatchCount() ) |
3081 | m_gridWin->Refresh(); | |
2f024384 | 3082 | |
f6bcfd97 | 3083 | return result; |
f85afd4e MB |
3084 | } |
3085 | ||
ef316e23 | 3086 | wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const |
f85afd4e | 3087 | { |
2d66e025 MB |
3088 | wxRegionIterator iter( reg ); |
3089 | wxRect r; | |
f85afd4e | 3090 | |
275c4ae4 RD |
3091 | wxArrayInt rowlabels; |
3092 | ||
2d66e025 MB |
3093 | int top, bottom; |
3094 | while ( iter ) | |
f85afd4e | 3095 | { |
2d66e025 | 3096 | r = iter.GetRect(); |
f85afd4e | 3097 | |
2d66e025 MB |
3098 | // TODO: remove this when we can... |
3099 | // There is a bug in wxMotif that gives garbage update | |
3100 | // rectangles if you jump-scroll a long way by clicking the | |
3101 | // scrollbar with middle button. This is a work-around | |
3102 | // | |
3103 | #if defined(__WXMOTIF__) | |
3104 | int cw, ch; | |
3105 | m_gridWin->GetClientSize( &cw, &ch ); | |
56b6cf26 DS |
3106 | if ( r.GetTop() > ch ) |
3107 | r.SetTop( 0 ); | |
2d66e025 MB |
3108 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); |
3109 | #endif | |
f85afd4e | 3110 | |
2d66e025 MB |
3111 | // logical bounds of update region |
3112 | // | |
3113 | int dummy; | |
3114 | CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top ); | |
3115 | CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom ); | |
3116 | ||
3117 | // find the row labels within these bounds | |
3118 | // | |
3119 | int row; | |
3d59537f | 3120 | for ( row = internalYToRow(top); row < m_numRows; row++ ) |
2d66e025 | 3121 | { |
7c1cb261 VZ |
3122 | if ( GetRowBottom(row) < top ) |
3123 | continue; | |
2d66e025 | 3124 | |
6d55126d | 3125 | if ( GetRowTop(row) > bottom ) |
7c1cb261 | 3126 | break; |
60ff3b99 | 3127 | |
d10f4bf9 | 3128 | rowlabels.Add( row ); |
2d66e025 | 3129 | } |
60ff3b99 | 3130 | |
60d8e886 | 3131 | ++iter; |
f85afd4e | 3132 | } |
d10f4bf9 VZ |
3133 | |
3134 | return rowlabels; | |
f85afd4e MB |
3135 | } |
3136 | ||
ef316e23 | 3137 | wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const |
f85afd4e | 3138 | { |
2d66e025 MB |
3139 | wxRegionIterator iter( reg ); |
3140 | wxRect r; | |
f85afd4e | 3141 | |
d10f4bf9 | 3142 | wxArrayInt colLabels; |
f85afd4e | 3143 | |
2d66e025 MB |
3144 | int left, right; |
3145 | while ( iter ) | |
f85afd4e | 3146 | { |
2d66e025 | 3147 | r = iter.GetRect(); |
f85afd4e | 3148 | |
2d66e025 MB |
3149 | // TODO: remove this when we can... |
3150 | // There is a bug in wxMotif that gives garbage update | |
3151 | // rectangles if you jump-scroll a long way by clicking the | |
3152 | // scrollbar with middle button. This is a work-around | |
3153 | // | |
3154 | #if defined(__WXMOTIF__) | |
3155 | int cw, ch; | |
3156 | m_gridWin->GetClientSize( &cw, &ch ); | |
56b6cf26 DS |
3157 | if ( r.GetLeft() > cw ) |
3158 | r.SetLeft( 0 ); | |
2d66e025 MB |
3159 | r.SetRight( wxMin( r.GetRight(), cw ) ); |
3160 | #endif | |
3161 | ||
3162 | // logical bounds of update region | |
3163 | // | |
3164 | int dummy; | |
3165 | CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy ); | |
3166 | CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy ); | |
3167 | ||
3168 | // find the cells within these bounds | |
3169 | // | |
3170 | int col; | |
d4175745 VZ |
3171 | int colPos; |
3172 | for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ ) | |
2d66e025 | 3173 | { |
d4175745 VZ |
3174 | col = GetColAt( colPos ); |
3175 | ||
7c1cb261 VZ |
3176 | if ( GetColRight(col) < left ) |
3177 | continue; | |
60ff3b99 | 3178 | |
7c1cb261 VZ |
3179 | if ( GetColLeft(col) > right ) |
3180 | break; | |
2d66e025 | 3181 | |
d10f4bf9 | 3182 | colLabels.Add( col ); |
2d66e025 | 3183 | } |
60ff3b99 | 3184 | |
60d8e886 | 3185 | ++iter; |
f85afd4e | 3186 | } |
2f024384 | 3187 | |
d10f4bf9 | 3188 | return colLabels; |
f85afd4e MB |
3189 | } |
3190 | ||
ef316e23 | 3191 | wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const |
f85afd4e | 3192 | { |
2d66e025 | 3193 | wxRect r; |
f85afd4e | 3194 | |
d10f4bf9 | 3195 | wxGridCellCoordsArray cellsExposed; |
f85afd4e | 3196 | |
2d66e025 | 3197 | int left, top, right, bottom; |
9e57072a | 3198 | for ( wxRegionIterator iter(reg); iter; ++iter ) |
2d66e025 MB |
3199 | { |
3200 | r = iter.GetRect(); | |
f85afd4e | 3201 | |
9e57072a VZ |
3202 | // Skip 0-height cells, they're invisible anyhow, don't waste time |
3203 | // getting their rectangles and so on. | |
3204 | if ( !r.GetHeight() ) | |
3205 | continue; | |
3206 | ||
2d66e025 MB |
3207 | // TODO: remove this when we can... |
3208 | // There is a bug in wxMotif that gives garbage update | |
3209 | // rectangles if you jump-scroll a long way by clicking the | |
3210 | // scrollbar with middle button. This is a work-around | |
3211 | // | |
3212 | #if defined(__WXMOTIF__) | |
f85afd4e | 3213 | int cw, ch; |
2d66e025 MB |
3214 | m_gridWin->GetClientSize( &cw, &ch ); |
3215 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
3216 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
3217 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
3218 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
3219 | #endif | |
8f177c8e | 3220 | |
2d66e025 MB |
3221 | // logical bounds of update region |
3222 | // | |
3223 | CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
3224 | CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
f85afd4e | 3225 | |
2d66e025 | 3226 | // find the cells within these bounds |
cd4f6f5f VZ |
3227 | wxArrayInt cols; |
3228 | for ( int row = internalYToRow(top); row < m_numRows; row++ ) | |
f85afd4e | 3229 | { |
7c1cb261 VZ |
3230 | if ( GetRowBottom(row) <= top ) |
3231 | continue; | |
f85afd4e | 3232 | |
7c1cb261 VZ |
3233 | if ( GetRowTop(row) > bottom ) |
3234 | break; | |
60ff3b99 | 3235 | |
cd4f6f5f VZ |
3236 | // add all dirty cells in this row: notice that the columns which |
3237 | // are dirty don't depend on the row so we compute them only once | |
3238 | // for the first dirty row and then reuse for all the next ones | |
3239 | if ( cols.empty() ) | |
2d66e025 | 3240 | { |
cd4f6f5f VZ |
3241 | // do determine the dirty columns |
3242 | for ( int pos = XToPos(left); pos <= XToPos(right); pos++ ) | |
3243 | cols.push_back(GetColAt(pos)); | |
d4175745 | 3244 | |
cd4f6f5f VZ |
3245 | // if there are no dirty columns at all, nothing to do |
3246 | if ( cols.empty() ) | |
7c1cb261 | 3247 | break; |
2d66e025 | 3248 | } |
cd4f6f5f VZ |
3249 | |
3250 | const size_t count = cols.size(); | |
3251 | for ( size_t n = 0; n < count; n++ ) | |
3252 | cellsExposed.Add(wxGridCellCoords(row, cols[n])); | |
2d66e025 | 3253 | } |
f85afd4e | 3254 | } |
d10f4bf9 VZ |
3255 | |
3256 | return cellsExposed; | |
f85afd4e MB |
3257 | } |
3258 | ||
3259 | ||
2d66e025 | 3260 | void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 3261 | { |
2d66e025 MB |
3262 | int x, y, row; |
3263 | wxPoint pos( event.GetPosition() ); | |
3264 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 3265 | |
2d66e025 | 3266 | if ( event.Dragging() ) |
f85afd4e | 3267 | { |
426b2d87 JS |
3268 | if (!m_isDragging) |
3269 | { | |
ca65c044 | 3270 | m_isDragging = true; |
426b2d87 JS |
3271 | m_rowLabelWin->CaptureMouse(); |
3272 | } | |
8f177c8e | 3273 | |
2d66e025 | 3274 | if ( event.LeftIsDown() ) |
f85afd4e | 3275 | { |
962a48f6 | 3276 | switch ( m_cursorMode ) |
f85afd4e | 3277 | { |
f85afd4e MB |
3278 | case WXGRID_CURSOR_RESIZE_ROW: |
3279 | { | |
2d66e025 MB |
3280 | int cw, ch, left, dummy; |
3281 | m_gridWin->GetClientSize( &cw, &ch ); | |
3282 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
60ff3b99 | 3283 | |
2d66e025 MB |
3284 | wxClientDC dc( m_gridWin ); |
3285 | PrepareDC( dc ); | |
af547d51 VZ |
3286 | y = wxMax( y, |
3287 | GetRowTop(m_dragRowOrCol) + | |
3288 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
d2fdd8d2 | 3289 | dc.SetLogicalFunction(wxINVERT); |
f85afd4e MB |
3290 | if ( m_dragLastPos >= 0 ) |
3291 | { | |
2d66e025 | 3292 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); |
f85afd4e | 3293 | } |
2d66e025 MB |
3294 | dc.DrawLine( left, y, left+cw, y ); |
3295 | m_dragLastPos = y; | |
f85afd4e MB |
3296 | } |
3297 | break; | |
3298 | ||
3299 | case WXGRID_CURSOR_SELECT_ROW: | |
902725ee | 3300 | { |
e32352cf | 3301 | if ( (row = YToRow( y )) >= 0 ) |
aa5e1f75 | 3302 | { |
3f3dc2ef | 3303 | if ( m_selection ) |
8b5f6d9d | 3304 | m_selection->SelectRow(row, event); |
f85afd4e | 3305 | } |
902725ee WS |
3306 | } |
3307 | break; | |
e2b42eeb VZ |
3308 | |
3309 | // default label to suppress warnings about "enumeration value | |
3310 | // 'xxx' not handled in switch | |
3311 | default: | |
3312 | break; | |
f85afd4e MB |
3313 | } |
3314 | } | |
3315 | return; | |
3316 | } | |
3317 | ||
426b2d87 JS |
3318 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) |
3319 | return; | |
8f177c8e | 3320 | |
426b2d87 JS |
3321 | if (m_isDragging) |
3322 | { | |
ccdee36f DS |
3323 | if (m_rowLabelWin->HasCapture()) |
3324 | m_rowLabelWin->ReleaseMouse(); | |
ca65c044 | 3325 | m_isDragging = false; |
426b2d87 | 3326 | } |
60ff3b99 | 3327 | |
6d004f67 MB |
3328 | // ------------ Entering or leaving the window |
3329 | // | |
3330 | if ( event.Entering() || event.Leaving() ) | |
3331 | { | |
e2b42eeb | 3332 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
6d004f67 MB |
3333 | } |
3334 | ||
2d66e025 | 3335 | // ------------ Left button pressed |
f85afd4e | 3336 | // |
6d004f67 | 3337 | else if ( event.LeftDown() ) |
f85afd4e | 3338 | { |
82edfbe7 VZ |
3339 | row = YToEdgeOfRow(y); |
3340 | if ( row != wxNOT_FOUND && CanDragRowSize(row) ) | |
3341 | { | |
3342 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin); | |
3343 | } | |
3344 | else // not a request to start resizing | |
f85afd4e | 3345 | { |
2d66e025 | 3346 | row = YToRow(y); |
2f024384 | 3347 | if ( row >= 0 && |
b54ba671 | 3348 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) ) |
f85afd4e | 3349 | { |
2b01fd49 | 3350 | if ( !event.ShiftDown() && !event.CmdDown() ) |
aa5e1f75 | 3351 | ClearSelection(); |
64e15340 | 3352 | if ( m_selection ) |
3f3dc2ef VZ |
3353 | { |
3354 | if ( event.ShiftDown() ) | |
3355 | { | |
8b5f6d9d VZ |
3356 | m_selection->SelectBlock |
3357 | ( | |
3358 | m_currentCellCoords.GetRow(), 0, | |
3359 | row, GetNumberCols() - 1, | |
3360 | event | |
3361 | ); | |
3f3dc2ef VZ |
3362 | } |
3363 | else | |
3364 | { | |
8b5f6d9d | 3365 | m_selection->SelectRow(row, event); |
3f3dc2ef VZ |
3366 | } |
3367 | } | |
3368 | ||
e2b42eeb | 3369 | ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin); |
f85afd4e | 3370 | } |
2d66e025 | 3371 | } |
2d66e025 | 3372 | } |
f85afd4e | 3373 | |
2d66e025 MB |
3374 | // ------------ Left double click |
3375 | // | |
3376 | else if (event.LeftDClick() ) | |
3377 | { | |
4e115ed2 | 3378 | row = YToEdgeOfRow(y); |
82edfbe7 VZ |
3379 | if ( row != wxNOT_FOUND && CanDragRowSize(row) ) |
3380 | { | |
3381 | // adjust row height depending on label text | |
27bb2c8c VZ |
3382 | // |
3383 | // TODO: generate RESIZING event, see #10754 | |
82edfbe7 VZ |
3384 | AutoSizeRowLabelSize( row ); |
3385 | ||
c5c1ea96 | 3386 | SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, row, -1, event); |
27bb2c8c | 3387 | |
82edfbe7 VZ |
3388 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
3389 | m_dragLastPos = -1; | |
3390 | } | |
d13b34d3 | 3391 | else // not on row separator or it's not resizable |
2d66e025 MB |
3392 | { |
3393 | row = YToRow(y); | |
a967f048 | 3394 | if ( row >=0 && |
ca65c044 WS |
3395 | !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) ) |
3396 | { | |
a967f048 | 3397 | // no default action at the moment |
ca65c044 | 3398 | } |
f85afd4e MB |
3399 | } |
3400 | } | |
60ff3b99 | 3401 | |
2d66e025 | 3402 | // ------------ Left button released |
f85afd4e | 3403 | // |
2d66e025 | 3404 | else if ( event.LeftUp() ) |
f85afd4e | 3405 | { |
2d66e025 | 3406 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) |
27bb2c8c | 3407 | DoEndDragResizeRow(event); |
f85afd4e | 3408 | |
e2b42eeb VZ |
3409 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
3410 | m_dragLastPos = -1; | |
2d66e025 | 3411 | } |
f85afd4e | 3412 | |
2d66e025 MB |
3413 | // ------------ Right button down |
3414 | // | |
3415 | else if ( event.RightDown() ) | |
3416 | { | |
3417 | row = YToRow(y); | |
ef5df12b | 3418 | if ( row >=0 && |
ca65c044 | 3419 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) |
2d66e025 MB |
3420 | { |
3421 | // no default action at the moment | |
f85afd4e MB |
3422 | } |
3423 | } | |
60ff3b99 | 3424 | |
2d66e025 | 3425 | // ------------ Right double click |
f85afd4e | 3426 | // |
2d66e025 MB |
3427 | else if ( event.RightDClick() ) |
3428 | { | |
3429 | row = YToRow(y); | |
a967f048 | 3430 | if ( row >= 0 && |
ca65c044 | 3431 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) |
2d66e025 MB |
3432 | { |
3433 | // no default action at the moment | |
3434 | } | |
3435 | } | |
60ff3b99 | 3436 | |
2d66e025 | 3437 | // ------------ No buttons down and mouse moving |
f85afd4e | 3438 | // |
2d66e025 | 3439 | else if ( event.Moving() ) |
f85afd4e | 3440 | { |
2d66e025 | 3441 | m_dragRowOrCol = YToEdgeOfRow( y ); |
82edfbe7 | 3442 | if ( m_dragRowOrCol != wxNOT_FOUND ) |
8f177c8e | 3443 | { |
2d66e025 | 3444 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
8f177c8e | 3445 | { |
82edfbe7 | 3446 | if ( CanDragRowSize(m_dragRowOrCol) ) |
ca65c044 | 3447 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false); |
8f177c8e | 3448 | } |
2d66e025 | 3449 | } |
6d004f67 | 3450 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 3451 | { |
ca65c044 | 3452 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false); |
8f177c8e | 3453 | } |
f85afd4e | 3454 | } |
2d66e025 MB |
3455 | } |
3456 | ||
11393d29 VZ |
3457 | void wxGrid::UpdateColumnSortingIndicator(int col) |
3458 | { | |
3459 | wxCHECK_RET( col != wxNOT_FOUND, "invalid column index" ); | |
3460 | ||
3461 | if ( m_useNativeHeader ) | |
3039ade9 | 3462 | GetGridColHeader()->UpdateColumn(col); |
11393d29 VZ |
3463 | else if ( m_nativeColumnLabels ) |
3464 | m_colWindow->Refresh(); | |
3465 | //else: sorting indicator display not yet implemented in grid version | |
3466 | } | |
3467 | ||
3468 | void wxGrid::SetSortingColumn(int col, bool ascending) | |
3469 | { | |
3470 | if ( col == m_sortCol ) | |
3471 | { | |
3472 | // we are already using this column for sorting (or not sorting at all) | |
3473 | // but we might still change the sorting order, check for it | |
3474 | if ( m_sortCol != wxNOT_FOUND && ascending != m_sortIsAscending ) | |
3475 | { | |
3476 | m_sortIsAscending = ascending; | |
3477 | ||
3478 | UpdateColumnSortingIndicator(m_sortCol); | |
3479 | } | |
3480 | } | |
3481 | else // we're changing the column used for sorting | |
3482 | { | |
3483 | const int sortColOld = m_sortCol; | |
3484 | ||
3485 | // change it before updating the column as we want GetSortingColumn() | |
3486 | // to return the correct new value | |
3487 | m_sortCol = col; | |
3488 | ||
3489 | if ( sortColOld != wxNOT_FOUND ) | |
3490 | UpdateColumnSortingIndicator(sortColOld); | |
3491 | ||
3492 | if ( m_sortCol != wxNOT_FOUND ) | |
3493 | { | |
3494 | m_sortIsAscending = ascending; | |
3495 | UpdateColumnSortingIndicator(m_sortCol); | |
3496 | } | |
3497 | } | |
3498 | } | |
3499 | ||
3500 | void wxGrid::DoColHeaderClick(int col) | |
3501 | { | |
3502 | // we consider that the grid was resorted if this event is processed and | |
3503 | // not vetoed | |
3504 | if ( SendEvent(wxEVT_GRID_COL_SORT, -1, col) == 1 ) | |
3505 | { | |
3506 | SetSortingColumn(col, IsSortingBy(col) ? !m_sortIsAscending : true); | |
3507 | Refresh(); | |
3508 | } | |
3509 | } | |
3510 | ||
ad805b9e VZ |
3511 | void wxGrid::DoStartResizeCol(int col) |
3512 | { | |
3513 | m_dragRowOrCol = col; | |
3514 | m_dragLastPos = -1; | |
3515 | DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol)); | |
3516 | } | |
3517 | ||
3518 | void wxGrid::DoUpdateResizeCol(int x) | |
3519 | { | |
3520 | int cw, ch, dummy, top; | |
3521 | m_gridWin->GetClientSize( &cw, &ch ); | |
3522 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
3523 | ||
3524 | wxClientDC dc( m_gridWin ); | |
3525 | PrepareDC( dc ); | |
3526 | ||
3527 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + GetColMinimalWidth(m_dragRowOrCol)); | |
3528 | dc.SetLogicalFunction(wxINVERT); | |
3529 | if ( m_dragLastPos >= 0 ) | |
3530 | { | |
3531 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch ); | |
3532 | } | |
3533 | dc.DrawLine( x, top, x, top + ch ); | |
3534 | m_dragLastPos = x; | |
3535 | } | |
3536 | ||
3537 | void wxGrid::DoUpdateResizeColWidth(int w) | |
3538 | { | |
3539 | DoUpdateResizeCol(GetColLeft(m_dragRowOrCol) + w); | |
3540 | } | |
3541 | ||
2d66e025 MB |
3542 | void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) |
3543 | { | |
7d1214cd PC |
3544 | int x; |
3545 | CalcUnscrolledPosition( event.GetPosition().x, 0, &x, NULL ); | |
60ff3b99 | 3546 | |
11393d29 | 3547 | int col = XToCol(x); |
2d66e025 | 3548 | if ( event.Dragging() ) |
f85afd4e | 3549 | { |
fe77cf60 JS |
3550 | if (!m_isDragging) |
3551 | { | |
ca65c044 | 3552 | m_isDragging = true; |
ad805b9e | 3553 | GetColLabelWindow()->CaptureMouse(); |
d4175745 | 3554 | |
11393d29 VZ |
3555 | if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL && col != -1 ) |
3556 | DoStartMoveCol(col); | |
fe77cf60 | 3557 | } |
8f177c8e | 3558 | |
2d66e025 | 3559 | if ( event.LeftIsDown() ) |
8f177c8e | 3560 | { |
962a48f6 | 3561 | switch ( m_cursorMode ) |
8f177c8e | 3562 | { |
2d66e025 | 3563 | case WXGRID_CURSOR_RESIZE_COL: |
ad805b9e | 3564 | DoUpdateResizeCol(x); |
2d66e025 | 3565 | break; |
f85afd4e | 3566 | |
2d66e025 | 3567 | case WXGRID_CURSOR_SELECT_COL: |
902725ee | 3568 | { |
11393d29 | 3569 | if ( col != -1 ) |
aa5e1f75 | 3570 | { |
3f3dc2ef | 3571 | if ( m_selection ) |
8b5f6d9d | 3572 | m_selection->SelectCol(col, event); |
2d66e025 | 3573 | } |
902725ee WS |
3574 | } |
3575 | break; | |
e2b42eeb | 3576 | |
d4175745 VZ |
3577 | case WXGRID_CURSOR_MOVE_COL: |
3578 | { | |
cd68daf5 VZ |
3579 | int posNew = XToPos(x); |
3580 | int colNew = GetColAt(posNew); | |
d4175745 | 3581 | |
cd68daf5 | 3582 | // determine the position of the drop marker |
d4175745 | 3583 | int markerX; |
cd68daf5 VZ |
3584 | if ( x >= GetColLeft(colNew) + (GetColWidth(colNew) / 2) ) |
3585 | markerX = GetColRight(colNew); | |
d4175745 | 3586 | else |
cd68daf5 | 3587 | markerX = GetColLeft(colNew); |
d4175745 VZ |
3588 | |
3589 | if ( markerX != m_dragLastPos ) | |
3590 | { | |
ad805b9e | 3591 | wxClientDC dc( GetColLabelWindow() ); |
1eab9659 | 3592 | DoPrepareDC(dc); |
d4175745 VZ |
3593 | |
3594 | int cw, ch; | |
ad805b9e | 3595 | GetColLabelWindow()->GetClientSize( &cw, &ch ); |
d4175745 VZ |
3596 | |
3597 | markerX++; | |
3598 | ||
3599 | //Clean up the last indicator | |
3600 | if ( m_dragLastPos >= 0 ) | |
3601 | { | |
ad805b9e | 3602 | wxPen pen( GetColLabelWindow()->GetBackgroundColour(), 2 ); |
d4175745 VZ |
3603 | dc.SetPen(pen); |
3604 | dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch ); | |
3605 | dc.SetPen(wxNullPen); | |
3606 | ||
3607 | if ( XToCol( m_dragLastPos ) != -1 ) | |
3608 | DrawColLabel( dc, XToCol( m_dragLastPos ) ); | |
3609 | } | |
3610 | ||
87c819f9 | 3611 | const wxColour *color; |
d4175745 | 3612 | //Moving to the same place? Don't draw a marker |
cd68daf5 | 3613 | if ( colNew == m_dragRowOrCol ) |
87c819f9 VZ |
3614 | color = wxLIGHT_GREY; |
3615 | else | |
3616 | color = wxBLUE; | |
d4175745 VZ |
3617 | |
3618 | //Draw the marker | |
87c819f9 | 3619 | wxPen pen( *color, 2 ); |
d4175745 VZ |
3620 | dc.SetPen(pen); |
3621 | ||
3622 | dc.DrawLine( markerX, 0, markerX, ch ); | |
3623 | ||
3624 | dc.SetPen(wxNullPen); | |
3625 | ||
3626 | m_dragLastPos = markerX - 1; | |
3627 | } | |
3628 | } | |
3629 | break; | |
3630 | ||
e2b42eeb VZ |
3631 | // default label to suppress warnings about "enumeration value |
3632 | // 'xxx' not handled in switch | |
3633 | default: | |
3634 | break; | |
2d66e025 | 3635 | } |
f85afd4e | 3636 | } |
2d66e025 | 3637 | return; |
f85afd4e | 3638 | } |
2d66e025 | 3639 | |
fe77cf60 JS |
3640 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) |
3641 | return; | |
2d66e025 | 3642 | |
fe77cf60 JS |
3643 | if (m_isDragging) |
3644 | { | |
ad805b9e VZ |
3645 | if (GetColLabelWindow()->HasCapture()) |
3646 | GetColLabelWindow()->ReleaseMouse(); | |
ca65c044 | 3647 | m_isDragging = false; |
fe77cf60 | 3648 | } |
60ff3b99 | 3649 | |
6d004f67 MB |
3650 | // ------------ Entering or leaving the window |
3651 | // | |
3652 | if ( event.Entering() || event.Leaving() ) | |
3653 | { | |
ad805b9e | 3654 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
6d004f67 MB |
3655 | } |
3656 | ||
2d66e025 | 3657 | // ------------ Left button pressed |
f85afd4e | 3658 | // |
6d004f67 | 3659 | else if ( event.LeftDown() ) |
f85afd4e | 3660 | { |
7d1214cd PC |
3661 | int colEdge = XToEdgeOfCol(x); |
3662 | if ( colEdge != wxNOT_FOUND && CanDragColSize(colEdge) ) | |
8f177c8e | 3663 | { |
82edfbe7 VZ |
3664 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow()); |
3665 | } | |
3666 | else // not a request to start resizing | |
3667 | { | |
2f024384 | 3668 | if ( col >= 0 && |
b54ba671 | 3669 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) ) |
8f177c8e | 3670 | { |
d4175745 | 3671 | if ( m_canDragColMove ) |
3f3dc2ef | 3672 | { |
d4175745 | 3673 | //Show button as pressed |
ad805b9e | 3674 | wxClientDC dc( GetColLabelWindow() ); |
d4175745 VZ |
3675 | int colLeft = GetColLeft( col ); |
3676 | int colRight = GetColRight( col ) - 1; | |
ad805b9e | 3677 | dc.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) ); |
d4175745 VZ |
3678 | dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 ); |
3679 | dc.DrawLine( colLeft, 1, colRight, 1 ); | |
3680 | ||
ad805b9e | 3681 | ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, GetColLabelWindow()); |
d4175745 VZ |
3682 | } |
3683 | else | |
3684 | { | |
3685 | if ( !event.ShiftDown() && !event.CmdDown() ) | |
3686 | ClearSelection(); | |
3687 | if ( m_selection ) | |
3f3dc2ef | 3688 | { |
d4175745 VZ |
3689 | if ( event.ShiftDown() ) |
3690 | { | |
8b5f6d9d VZ |
3691 | m_selection->SelectBlock |
3692 | ( | |
3693 | 0, m_currentCellCoords.GetCol(), | |
3694 | GetNumberRows() - 1, col, | |
3695 | event | |
3696 | ); | |
d4175745 VZ |
3697 | } |
3698 | else | |
3699 | { | |
8b5f6d9d | 3700 | m_selection->SelectCol(col, event); |
d4175745 | 3701 | } |
3f3dc2ef | 3702 | } |
3f3dc2ef | 3703 | |
ad805b9e | 3704 | ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, GetColLabelWindow()); |
d4175745 | 3705 | } |
f85afd4e | 3706 | } |
2d66e025 | 3707 | } |
2d66e025 | 3708 | } |
f85afd4e | 3709 | |
2d66e025 MB |
3710 | // ------------ Left double click |
3711 | // | |
3712 | if ( event.LeftDClick() ) | |
3713 | { | |
11393d29 VZ |
3714 | const int colEdge = XToEdgeOfCol(x); |
3715 | if ( colEdge == -1 ) | |
2d66e025 | 3716 | { |
a967f048 RG |
3717 | if ( col >= 0 && |
3718 | ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) ) | |
3719 | { | |
ca65c044 | 3720 | // no default action at the moment |
a967f048 | 3721 | } |
2d66e025 | 3722 | } |
d43851f7 JS |
3723 | else |
3724 | { | |
3725 | // adjust column width depending on label text | |
27bb2c8c VZ |
3726 | // |
3727 | // TODO: generate RESIZING event, see #10754 | |
6f58f3d7 VZ |
3728 | if ( !SendGridSizeEvent(wxEVT_GRID_COL_AUTO_SIZE, -1, colEdge, event) ) |
3729 | AutoSizeColLabelSize( colEdge ); | |
d43851f7 | 3730 | |
c5c1ea96 | 3731 | SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, colEdge, event); |
27bb2c8c | 3732 | |
ad805b9e | 3733 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
ccdee36f | 3734 | m_dragLastPos = -1; |
d43851f7 | 3735 | } |
2d66e025 | 3736 | } |
60ff3b99 | 3737 | |
2d66e025 MB |
3738 | // ------------ Left button released |
3739 | // | |
3740 | else if ( event.LeftUp() ) | |
3741 | { | |
d4175745 | 3742 | switch ( m_cursorMode ) |
2d66e025 | 3743 | { |
d4175745 | 3744 | case WXGRID_CURSOR_RESIZE_COL: |
27bb2c8c | 3745 | DoEndDragResizeCol(event); |
852b6c3c | 3746 | break; |
d4175745 VZ |
3747 | |
3748 | case WXGRID_CURSOR_MOVE_COL: | |
11393d29 | 3749 | if ( m_dragLastPos == -1 || col == m_dragRowOrCol ) |
cd68daf5 | 3750 | { |
11393d29 VZ |
3751 | // the column didn't actually move anywhere |
3752 | if ( col != -1 ) | |
3753 | DoColHeaderClick(col); | |
cd68daf5 VZ |
3754 | m_colWindow->Refresh(); // "unpress" the column |
3755 | } | |
3756 | else | |
3757 | { | |
7c331059 VZ |
3758 | // get the position of the column we're over |
3759 | int pos = XToPos(x); | |
3760 | ||
3761 | // we may need to adjust the drop position but don't bother | |
3762 | // checking for it if we can't anyhow | |
3763 | if ( pos > 1 ) | |
3764 | { | |
3765 | // also find the index of the column we're over: notice | |
3766 | // that the existing "col" variable may be invalid but | |
3767 | // we need a valid one here | |
3768 | const int colValid = GetColAt(pos); | |
3769 | ||
3770 | // if we're on the "near" (usually left but right in | |
3771 | // RTL case) part of the column, the actual position we | |
3772 | // should be placed in is actually the one before it | |
18c0369b | 3773 | bool onNearPart; |
7c331059 VZ |
3774 | const int middle = GetColLeft(colValid) + |
3775 | GetColWidth(colValid)/2; | |
3776 | if ( GetLayoutDirection() == wxLayout_LeftToRight ) | |
18c0369b | 3777 | onNearPart = (x <= middle); |
7c331059 | 3778 | else // wxLayout_RightToLeft |
18c0369b | 3779 | onNearPart = (x > middle); |
7c331059 | 3780 | |
18c0369b | 3781 | if ( onNearPart ) |
7c331059 VZ |
3782 | pos--; |
3783 | } | |
3784 | ||
3785 | DoEndMoveCol(pos); | |
cd68daf5 | 3786 | } |
852b6c3c VZ |
3787 | break; |
3788 | ||
3789 | case WXGRID_CURSOR_SELECT_COL: | |
3790 | case WXGRID_CURSOR_SELECT_CELL: | |
3791 | case WXGRID_CURSOR_RESIZE_ROW: | |
3792 | case WXGRID_CURSOR_SELECT_ROW: | |
11393d29 VZ |
3793 | if ( col != -1 ) |
3794 | DoColHeaderClick(col); | |
852b6c3c | 3795 | break; |
2d66e025 | 3796 | } |
f85afd4e | 3797 | |
ad805b9e | 3798 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
ccdee36f | 3799 | m_dragLastPos = -1; |
60ff3b99 VZ |
3800 | } |
3801 | ||
2d66e025 MB |
3802 | // ------------ Right button down |
3803 | // | |
3804 | else if ( event.RightDown() ) | |
3805 | { | |
a967f048 | 3806 | if ( col >= 0 && |
ca65c044 | 3807 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) |
2d66e025 MB |
3808 | { |
3809 | // no default action at the moment | |
f85afd4e MB |
3810 | } |
3811 | } | |
60ff3b99 | 3812 | |
2d66e025 | 3813 | // ------------ Right double click |
f85afd4e | 3814 | // |
2d66e025 MB |
3815 | else if ( event.RightDClick() ) |
3816 | { | |
a967f048 | 3817 | if ( col >= 0 && |
ca65c044 | 3818 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) |
2d66e025 MB |
3819 | { |
3820 | // no default action at the moment | |
3821 | } | |
3822 | } | |
60ff3b99 | 3823 | |
2d66e025 | 3824 | // ------------ No buttons down and mouse moving |
f85afd4e | 3825 | // |
2d66e025 | 3826 | else if ( event.Moving() ) |
f85afd4e | 3827 | { |
2d66e025 MB |
3828 | m_dragRowOrCol = XToEdgeOfCol( x ); |
3829 | if ( m_dragRowOrCol >= 0 ) | |
f85afd4e | 3830 | { |
2d66e025 | 3831 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
f85afd4e | 3832 | { |
82edfbe7 | 3833 | if ( CanDragColSize(m_dragRowOrCol) ) |
ad805b9e | 3834 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow(), false); |
f85afd4e | 3835 | } |
2d66e025 | 3836 | } |
6d004f67 | 3837 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 3838 | { |
ad805b9e | 3839 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow(), false); |
8f177c8e | 3840 | } |
f85afd4e MB |
3841 | } |
3842 | } | |
3843 | ||
2d66e025 | 3844 | void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 3845 | { |
2d66e025 | 3846 | if ( event.LeftDown() ) |
f85afd4e | 3847 | { |
2d66e025 MB |
3848 | // indicate corner label by having both row and |
3849 | // col args == -1 | |
f85afd4e | 3850 | // |
b54ba671 | 3851 | if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) ) |
2d66e025 MB |
3852 | { |
3853 | SelectAll(); | |
3854 | } | |
f85afd4e | 3855 | } |
2d66e025 MB |
3856 | else if ( event.LeftDClick() ) |
3857 | { | |
b54ba671 | 3858 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event ); |
2d66e025 | 3859 | } |
2d66e025 | 3860 | else if ( event.RightDown() ) |
f85afd4e | 3861 | { |
b54ba671 | 3862 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) ) |
f85afd4e | 3863 | { |
2d66e025 MB |
3864 | // no default action at the moment |
3865 | } | |
3866 | } | |
2d66e025 MB |
3867 | else if ( event.RightDClick() ) |
3868 | { | |
b54ba671 | 3869 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) ) |
2d66e025 MB |
3870 | { |
3871 | // no default action at the moment | |
3872 | } | |
3873 | } | |
3874 | } | |
f85afd4e | 3875 | |
86033c4b VZ |
3876 | void wxGrid::CancelMouseCapture() |
3877 | { | |
3878 | // cancel operation currently in progress, whatever it is | |
3879 | if ( m_winCapture ) | |
3880 | { | |
3881 | m_isDragging = false; | |
8a3e536c VZ |
3882 | m_startDragPos = wxDefaultPosition; |
3883 | ||
86033c4b VZ |
3884 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
3885 | m_winCapture->SetCursor( *wxSTANDARD_CURSOR ); | |
3886 | m_winCapture = NULL; | |
3887 | ||
3888 | // remove traces of whatever we drew on screen | |
3889 | Refresh(); | |
3890 | } | |
3891 | } | |
3892 | ||
e2b42eeb VZ |
3893 | void wxGrid::ChangeCursorMode(CursorMode mode, |
3894 | wxWindow *win, | |
3895 | bool captureMouse) | |
3896 | { | |
711f12ef | 3897 | #if wxUSE_LOG_TRACE |
a243da29 | 3898 | static const wxChar *const cursorModes[] = |
e2b42eeb | 3899 | { |
9a83f860 VZ |
3900 | wxT("SELECT_CELL"), |
3901 | wxT("RESIZE_ROW"), | |
3902 | wxT("RESIZE_COL"), | |
3903 | wxT("SELECT_ROW"), | |
3904 | wxT("SELECT_COL"), | |
3905 | wxT("MOVE_COL"), | |
e2b42eeb VZ |
3906 | }; |
3907 | ||
9a83f860 VZ |
3908 | wxLogTrace(wxT("grid"), |
3909 | wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"), | |
3910 | win == m_colWindow ? wxT("colLabelWin") | |
3911 | : win ? wxT("rowLabelWin") | |
3912 | : wxT("gridWin"), | |
e2b42eeb | 3913 | cursorModes[m_cursorMode], cursorModes[mode]); |
711f12ef | 3914 | #endif // wxUSE_LOG_TRACE |
e2b42eeb | 3915 | |
faec5a43 SN |
3916 | if ( mode == m_cursorMode && |
3917 | win == m_winCapture && | |
3918 | captureMouse == (m_winCapture != NULL)) | |
e2b42eeb VZ |
3919 | return; |
3920 | ||
3921 | if ( !win ) | |
3922 | { | |
3923 | // by default use the grid itself | |
3924 | win = m_gridWin; | |
3925 | } | |
3926 | ||
3927 | if ( m_winCapture ) | |
3928 | { | |
e882d288 | 3929 | m_winCapture->ReleaseMouse(); |
10a4531d | 3930 | m_winCapture = NULL; |
e2b42eeb VZ |
3931 | } |
3932 | ||
3933 | m_cursorMode = mode; | |
3934 | ||
3935 | switch ( m_cursorMode ) | |
3936 | { | |
3937 | case WXGRID_CURSOR_RESIZE_ROW: | |
3938 | win->SetCursor( m_rowResizeCursor ); | |
3939 | break; | |
3940 | ||
3941 | case WXGRID_CURSOR_RESIZE_COL: | |
3942 | win->SetCursor( m_colResizeCursor ); | |
3943 | break; | |
3944 | ||
d4175745 VZ |
3945 | case WXGRID_CURSOR_MOVE_COL: |
3946 | win->SetCursor( wxCursor(wxCURSOR_HAND) ); | |
3947 | break; | |
3948 | ||
e2b42eeb VZ |
3949 | default: |
3950 | win->SetCursor( *wxSTANDARD_CURSOR ); | |
2f024384 | 3951 | break; |
e2b42eeb VZ |
3952 | } |
3953 | ||
3954 | // we need to capture mouse when resizing | |
3955 | bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW || | |
3956 | m_cursorMode == WXGRID_CURSOR_RESIZE_COL; | |
3957 | ||
3958 | if ( captureMouse && resize ) | |
3959 | { | |
3960 | win->CaptureMouse(); | |
3961 | m_winCapture = win; | |
3962 | } | |
3963 | } | |
8f177c8e | 3964 | |
8a3e536c VZ |
3965 | // ---------------------------------------------------------------------------- |
3966 | // grid mouse event processing | |
3967 | // ---------------------------------------------------------------------------- | |
60ff3b99 | 3968 | |
0cbcb12d | 3969 | bool |
8a3e536c VZ |
3970 | wxGrid::DoGridCellDrag(wxMouseEvent& event, |
3971 | const wxGridCellCoords& coords, | |
3972 | bool isFirstDrag) | |
3973 | { | |
0cbcb12d SC |
3974 | bool performDefault = true ; |
3975 | ||
8a3e536c | 3976 | if ( coords == wxGridNoCellCoords ) |
0cbcb12d | 3977 | return performDefault; // we're outside any valid cell |
60ff3b99 | 3978 | |
8a3e536c VZ |
3979 | // Hide the edit control, so it won't interfere with drag-shrinking. |
3980 | if ( IsCellEditControlShown() ) | |
27f35b66 | 3981 | { |
8a3e536c VZ |
3982 | HideCellEditControl(); |
3983 | SaveEditControlValue(); | |
27f35b66 SN |
3984 | } |
3985 | ||
8a3e536c | 3986 | switch ( event.GetModifiers() ) |
2d66e025 | 3987 | { |
3bc6d534 | 3988 | case wxMOD_CONTROL: |
8a3e536c VZ |
3989 | if ( m_selectedBlockCorner == wxGridNoCellCoords) |
3990 | m_selectedBlockCorner = coords; | |
3991 | UpdateBlockBeingSelected(m_selectedBlockCorner, coords); | |
3992 | break; | |
07296f0b | 3993 | |
8a3e536c VZ |
3994 | case wxMOD_NONE: |
3995 | if ( CanDragCell() ) | |
2d66e025 | 3996 | { |
8a3e536c | 3997 | if ( isFirstDrag ) |
79dbea21 | 3998 | { |
8a3e536c VZ |
3999 | if ( m_selectedBlockCorner == wxGridNoCellCoords) |
4000 | m_selectedBlockCorner = coords; | |
07296f0b | 4001 | |
0cbcb12d | 4002 | // if event is handled by user code, no further processing |
c118fa5f | 4003 | if ( SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event) != 0 ) |
0cbcb12d SC |
4004 | performDefault = false; |
4005 | ||
4006 | return performDefault; | |
07296f0b | 4007 | } |
2d66e025 | 4008 | } |
25107357 | 4009 | |
8a3e536c VZ |
4010 | UpdateBlockBeingSelected(m_currentCellCoords, coords); |
4011 | break; | |
c71b2126 | 4012 | |
8a3e536c VZ |
4013 | default: |
4014 | // we don't handle the other key modifiers | |
4015 | event.Skip(); | |
4016 | } | |
0cbcb12d SC |
4017 | |
4018 | return performDefault; | |
8a3e536c | 4019 | } |
8f177c8e | 4020 | |
8a3e536c VZ |
4021 | void wxGrid::DoGridLineDrag(wxMouseEvent& event, const wxGridOperations& oper) |
4022 | { | |
4023 | wxClientDC dc(m_gridWin); | |
4024 | PrepareDC(dc); | |
4025 | dc.SetLogicalFunction(wxINVERT); | |
e2b42eeb | 4026 | |
8a3e536c VZ |
4027 | const wxRect rectWin(CalcUnscrolledPosition(wxPoint(0, 0)), |
4028 | m_gridWin->GetClientSize()); | |
4029 | ||
4030 | // erase the previously drawn line, if any | |
4031 | if ( m_dragLastPos >= 0 ) | |
4032 | oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos); | |
4033 | ||
4034 | // we need the vertical position for rows and horizontal for columns here | |
4035 | m_dragLastPos = oper.Dual().Select(CalcUnscrolledPosition(event.GetPosition())); | |
4036 | ||
4037 | // don't allow resizing beneath the minimal size | |
4038 | const int posMin = oper.GetLineStartPos(this, m_dragRowOrCol) + | |
4039 | oper.GetMinimalLineSize(this, m_dragRowOrCol); | |
4040 | if ( m_dragLastPos < posMin ) | |
4041 | m_dragLastPos = posMin; | |
4042 | ||
4043 | // and draw it at the new position | |
4044 | oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos); | |
4045 | } | |
4046 | ||
4047 | void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords) | |
4048 | { | |
4049 | if ( !m_isDragging ) | |
4050 | { | |
4051 | // Don't start doing anything until the mouse has been dragged far | |
4052 | // enough | |
4053 | const wxPoint& pt = event.GetPosition(); | |
4054 | if ( m_startDragPos == wxDefaultPosition ) | |
4055 | { | |
4056 | m_startDragPos = pt; | |
4057 | return; | |
6d004f67 | 4058 | } |
e2b42eeb | 4059 | |
8a3e536c VZ |
4060 | if ( abs(m_startDragPos.x - pt.x) <= DRAG_SENSITIVITY && |
4061 | abs(m_startDragPos.y - pt.y) <= DRAG_SENSITIVITY ) | |
4062 | return; | |
2d66e025 | 4063 | } |
66242c80 | 4064 | |
8a3e536c VZ |
4065 | const bool isFirstDrag = !m_isDragging; |
4066 | m_isDragging = true; | |
07296f0b | 4067 | |
8a3e536c | 4068 | switch ( m_cursorMode ) |
a5777624 | 4069 | { |
8a3e536c | 4070 | case WXGRID_CURSOR_SELECT_CELL: |
0cbcb12d SC |
4071 | // no further handling if handled by user |
4072 | if ( DoGridCellDrag(event, coords, isFirstDrag) == false ) | |
4073 | return; | |
8a3e536c VZ |
4074 | break; |
4075 | ||
4076 | case WXGRID_CURSOR_RESIZE_ROW: | |
4077 | DoGridLineDrag(event, wxGridRowOperations()); | |
4078 | break; | |
4079 | ||
4080 | case WXGRID_CURSOR_RESIZE_COL: | |
4081 | DoGridLineDrag(event, wxGridColumnOperations()); | |
4082 | break; | |
4083 | ||
4084 | default: | |
4085 | event.Skip(); | |
a5777624 | 4086 | } |
e2b42eeb | 4087 | |
8a3e536c | 4088 | if ( isFirstDrag ) |
f6bcfd97 | 4089 | { |
98e95650 VZ |
4090 | wxASSERT_MSG( !m_winCapture, "shouldn't capture the mouse twice" ); |
4091 | ||
8a3e536c VZ |
4092 | m_winCapture = m_gridWin; |
4093 | m_winCapture->CaptureMouse(); | |
4094 | } | |
4095 | } | |
a5777624 | 4096 | |
8a3e536c VZ |
4097 | void |
4098 | wxGrid::DoGridCellLeftDown(wxMouseEvent& event, | |
4099 | const wxGridCellCoords& coords, | |
4100 | const wxPoint& pos) | |
4101 | { | |
4102 | if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK, coords, event) ) | |
4103 | { | |
4104 | // event handled by user code, no need to do anything here | |
4105 | return; | |
a5777624 | 4106 | } |
f85afd4e | 4107 | |
8a3e536c VZ |
4108 | if ( !event.CmdDown() ) |
4109 | ClearSelection(); | |
4110 | ||
4111 | if ( event.ShiftDown() ) | |
4112 | { | |
4113 | if ( m_selection ) | |
4114 | { | |
8b5f6d9d | 4115 | m_selection->SelectBlock(m_currentCellCoords, coords, event); |
8a3e536c VZ |
4116 | m_selectedBlockCorner = coords; |
4117 | } | |
4118 | } | |
4119 | else if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 ) | |
a5777624 RD |
4120 | { |
4121 | DisableCellEditControl(); | |
8a3e536c | 4122 | MakeCellVisible( coords ); |
a5777624 | 4123 | |
8a3e536c | 4124 | if ( event.CmdDown() ) |
58dd5b3b | 4125 | { |
8a3e536c | 4126 | if ( m_selection ) |
1ef49ab5 | 4127 | { |
8b5f6d9d | 4128 | m_selection->ToggleCellSelection(coords, event); |
1ef49ab5 | 4129 | } |
8b5f6d9d | 4130 | |
8a3e536c VZ |
4131 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
4132 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
4133 | m_selectedBlockCorner = coords; | |
4134 | } | |
4135 | else | |
4136 | { | |
e196db7b VZ |
4137 | if ( m_selection ) |
4138 | { | |
4139 | // In row or column selection mode just clicking on the cell | |
4140 | // should select the row or column containing it: this is more | |
4141 | // convenient for the kinds of controls that use such selection | |
4142 | // mode and is compatible with 2.8 behaviour (see #12062). | |
4143 | switch ( m_selection->GetSelectionMode() ) | |
4144 | { | |
4145 | case wxGridSelectCells: | |
4146 | case wxGridSelectRowsOrColumns: | |
4147 | // nothing to do in these cases | |
4148 | break; | |
4149 | ||
4150 | case wxGridSelectRows: | |
4151 | m_selection->SelectRow(coords.GetRow()); | |
4152 | break; | |
4153 | ||
4154 | case wxGridSelectColumns: | |
4155 | m_selection->SelectCol(coords.GetCol()); | |
4156 | break; | |
4157 | } | |
4158 | } | |
4159 | ||
8a3e536c VZ |
4160 | m_waitForSlowClick = m_currentCellCoords == coords && |
4161 | coords != wxGridNoCellCoords; | |
4162 | SetCurrentCell( coords ); | |
58dd5b3b | 4163 | } |
a5777624 | 4164 | } |
8a3e536c | 4165 | } |
f85afd4e | 4166 | |
8a3e536c VZ |
4167 | void |
4168 | wxGrid::DoGridCellLeftDClick(wxMouseEvent& event, | |
4169 | const wxGridCellCoords& coords, | |
4170 | const wxPoint& pos) | |
4171 | { | |
4172 | if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 ) | |
a5777624 | 4173 | { |
8a3e536c | 4174 | if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK, coords, event) ) |
f85afd4e | 4175 | { |
8a3e536c VZ |
4176 | // we want double click to select a cell and start editing |
4177 | // (i.e. to behave in same way as sequence of two slow clicks): | |
4178 | m_waitForSlowClick = true; | |
4179 | } | |
4180 | } | |
4181 | } | |
932b55d0 | 4182 | |
8a3e536c VZ |
4183 | void |
4184 | wxGrid::DoGridCellLeftUp(wxMouseEvent& event, const wxGridCellCoords& coords) | |
4185 | { | |
4186 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
4187 | { | |
4188 | if (m_winCapture) | |
4189 | { | |
e882d288 | 4190 | m_winCapture->ReleaseMouse(); |
8a3e536c VZ |
4191 | m_winCapture = NULL; |
4192 | } | |
932b55d0 | 4193 | |
8a3e536c VZ |
4194 | if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() ) |
4195 | { | |
4196 | ClearSelection(); | |
4197 | EnableCellEditControl(); | |
3f3dc2ef | 4198 | |
8a3e536c VZ |
4199 | wxGridCellAttr *attr = GetCellAttr(coords); |
4200 | wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol()); | |
4201 | editor->StartingClick(); | |
4202 | editor->DecRef(); | |
4203 | attr->DecRef(); | |
2d66e025 | 4204 | |
8a3e536c | 4205 | m_waitForSlowClick = false; |
a5777624 | 4206 | } |
8a3e536c VZ |
4207 | else if ( m_selectedBlockTopLeft != wxGridNoCellCoords && |
4208 | m_selectedBlockBottomRight != wxGridNoCellCoords ) | |
a5777624 | 4209 | { |
8a3e536c VZ |
4210 | if ( m_selection ) |
4211 | { | |
4212 | m_selection->SelectBlock( m_selectedBlockTopLeft, | |
4213 | m_selectedBlockBottomRight, | |
8b5f6d9d | 4214 | event ); |
8a3e536c | 4215 | } |
e2b42eeb | 4216 | |
8a3e536c VZ |
4217 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
4218 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
e2b42eeb | 4219 | |
8a3e536c VZ |
4220 | // Show the edit control, if it has been hidden for |
4221 | // drag-shrinking. | |
4222 | ShowCellEditControl(); | |
58dd5b3b | 4223 | } |
8a3e536c VZ |
4224 | } |
4225 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
4226 | { | |
4227 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
27bb2c8c | 4228 | DoEndDragResizeRow(event); |
a5777624 | 4229 | } |
8a3e536c VZ |
4230 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) |
4231 | { | |
4232 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
27bb2c8c | 4233 | DoEndDragResizeCol(event); |
8a3e536c VZ |
4234 | } |
4235 | ||
4236 | m_dragLastPos = -1; | |
4237 | } | |
4238 | ||
4239 | void | |
4240 | wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event), | |
4241 | const wxGridCellCoords& coords, | |
4242 | const wxPoint& pos) | |
4243 | { | |
4244 | if ( coords.GetRow() < 0 || coords.GetCol() < 0 ) | |
a5777624 | 4245 | { |
8a3e536c VZ |
4246 | // out of grid cell area |
4247 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
4248 | return; | |
a5777624 | 4249 | } |
2d66e025 | 4250 | |
8a3e536c VZ |
4251 | int dragRow = YToEdgeOfRow( pos.y ); |
4252 | int dragCol = XToEdgeOfCol( pos.x ); | |
4253 | ||
4254 | // Dragging on the corner of a cell to resize in both | |
4255 | // directions is not implemented yet... | |
a5777624 | 4256 | // |
8a3e536c | 4257 | if ( dragRow >= 0 && dragCol >= 0 ) |
a5777624 | 4258 | { |
8a3e536c VZ |
4259 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); |
4260 | return; | |
4261 | } | |
4262 | ||
82edfbe7 | 4263 | if ( dragRow >= 0 && CanDragGridSize() && CanDragRowSize(dragRow) ) |
8a3e536c | 4264 | { |
8a3e536c | 4265 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
f85afd4e | 4266 | { |
82edfbe7 VZ |
4267 | m_dragRowOrCol = dragRow; |
4268 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false); | |
60ff3b99 | 4269 | } |
a5777624 | 4270 | } |
ad805b9e VZ |
4271 | // When using the native header window we can only resize the columns by |
4272 | // dragging the dividers in it because we can't make it enter into the | |
4273 | // column resizing mode programmatically | |
82edfbe7 VZ |
4274 | else if ( dragCol >= 0 && !m_useNativeHeader && |
4275 | CanDragGridSize() && CanDragColSize(dragCol) ) | |
8a3e536c | 4276 | { |
8a3e536c VZ |
4277 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
4278 | { | |
82edfbe7 VZ |
4279 | m_dragRowOrCol = dragCol; |
4280 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false); | |
8a3e536c VZ |
4281 | } |
4282 | } | |
4283 | else // Neither on a row or col edge | |
a5777624 | 4284 | { |
8a3e536c | 4285 | if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
d57ad377 | 4286 | { |
d57ad377 | 4287 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); |
d57ad377 | 4288 | } |
8a3e536c VZ |
4289 | } |
4290 | } | |
d57ad377 | 4291 | |
8a3e536c VZ |
4292 | void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event) |
4293 | { | |
98e95650 VZ |
4294 | if ( event.Entering() || event.Leaving() ) |
4295 | { | |
4296 | // we don't care about these events but we must not reset m_isDragging | |
4297 | // if they happen so return before anything else is done | |
4298 | event.Skip(); | |
4299 | return; | |
4300 | } | |
4301 | ||
8a3e536c | 4302 | const wxPoint pos = CalcUnscrolledPosition(event.GetPosition()); |
790cc417 | 4303 | |
8a3e536c VZ |
4304 | // coordinates of the cell under mouse |
4305 | wxGridCellCoords coords = XYToCell(pos); | |
6d004f67 | 4306 | |
8a3e536c VZ |
4307 | int cell_rows, cell_cols; |
4308 | GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols ); | |
4309 | if ( (cell_rows < 0) || (cell_cols < 0) ) | |
4310 | { | |
4311 | coords.SetRow(coords.GetRow() + cell_rows); | |
4312 | coords.SetCol(coords.GetCol() + cell_cols); | |
4313 | } | |
6d004f67 | 4314 | |
8a3e536c VZ |
4315 | if ( event.Dragging() ) |
4316 | { | |
4317 | if ( event.LeftIsDown() ) | |
4318 | DoGridDragEvent(event, coords); | |
4319 | else | |
4320 | event.Skip(); | |
4321 | return; | |
4322 | } | |
4323 | ||
4324 | m_isDragging = false; | |
4325 | m_startDragPos = wxDefaultPosition; | |
4326 | ||
8a3e536c VZ |
4327 | // deal with various button presses |
4328 | if ( event.IsButton() ) | |
4329 | { | |
4330 | if ( coords != wxGridNoCellCoords ) | |
a5777624 | 4331 | { |
8a3e536c | 4332 | DisableCellEditControl(); |
e2b42eeb | 4333 | |
8a3e536c VZ |
4334 | if ( event.LeftDown() ) |
4335 | DoGridCellLeftDown(event, coords, pos); | |
4336 | else if ( event.LeftDClick() ) | |
4337 | DoGridCellLeftDClick(event, coords, pos); | |
4338 | else if ( event.RightDown() ) | |
4339 | SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK, coords, event); | |
4340 | else if ( event.RightDClick() ) | |
4341 | SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK, coords, event); | |
6d004f67 | 4342 | } |
8a3e536c VZ |
4343 | |
4344 | // this one should be called even if we're not over any cell | |
4345 | if ( event.LeftUp() ) | |
a5777624 | 4346 | { |
8a3e536c | 4347 | DoGridCellLeftUp(event, coords); |
a5777624 RD |
4348 | } |
4349 | } | |
8a3e536c VZ |
4350 | else if ( event.Moving() ) |
4351 | { | |
4352 | DoGridMouseMoveEvent(event, coords, pos); | |
4353 | } | |
4354 | else // unknown mouse event? | |
4355 | { | |
4356 | event.Skip(); | |
4357 | } | |
6d004f67 MB |
4358 | } |
4359 | ||
27bb2c8c VZ |
4360 | // this function returns true only if the size really changed |
4361 | bool wxGrid::DoEndDragResizeLine(const wxGridOperations& oper) | |
6d004f67 | 4362 | { |
bec70262 | 4363 | if ( m_dragLastPos == -1 ) |
27bb2c8c | 4364 | return false; |
6d004f67 | 4365 | |
bec70262 | 4366 | const wxGridOperations& doper = oper.Dual(); |
6d004f67 | 4367 | |
bec70262 | 4368 | const wxSize size = m_gridWin->GetClientSize(); |
e2b42eeb | 4369 | |
bec70262 | 4370 | const wxPoint ptOrigin = CalcUnscrolledPosition(wxPoint(0, 0)); |
ccdee36f | 4371 | |
bec70262 VZ |
4372 | // erase the last line we drew |
4373 | wxClientDC dc(m_gridWin); | |
4374 | PrepareDC(dc); | |
4375 | dc.SetLogicalFunction(wxINVERT); | |
6d004f67 | 4376 | |
bec70262 VZ |
4377 | const int posLineStart = oper.Select(ptOrigin); |
4378 | const int posLineEnd = oper.Select(ptOrigin) + oper.Select(size); | |
6d004f67 | 4379 | |
bec70262 | 4380 | oper.DrawParallelLine(dc, posLineStart, posLineEnd, m_dragLastPos); |
6d004f67 | 4381 | |
bec70262 VZ |
4382 | // temporarily hide the edit control before resizing |
4383 | HideCellEditControl(); | |
4384 | SaveEditControlValue(); | |
4385 | ||
4386 | // do resize the line | |
4387 | const int lineStart = oper.GetLineStartPos(this, m_dragRowOrCol); | |
27bb2c8c | 4388 | const int lineSizeOld = oper.GetLineSize(this, m_dragRowOrCol); |
bec70262 VZ |
4389 | oper.SetLineSize(this, m_dragRowOrCol, |
4390 | wxMax(m_dragLastPos - lineStart, | |
4391 | oper.GetMinimalLineSize(this, m_dragRowOrCol))); | |
27bb2c8c VZ |
4392 | const bool |
4393 | sizeChanged = oper.GetLineSize(this, m_dragRowOrCol) != lineSizeOld; | |
bec70262 | 4394 | |
ad805b9e VZ |
4395 | m_dragLastPos = -1; |
4396 | ||
bec70262 VZ |
4397 | // refresh now if we're not frozen |
4398 | if ( !GetBatchCount() ) | |
6d004f67 | 4399 | { |
bec70262 VZ |
4400 | // we need to refresh everything beyond the resized line in the header |
4401 | // window | |
6d004f67 | 4402 | |
bec70262 VZ |
4403 | // get the position from which to refresh in the other direction |
4404 | wxRect rect(CellToRect(oper.MakeCoords(m_dragRowOrCol, 0))); | |
4405 | rect.SetPosition(CalcScrolledPosition(rect.GetPosition())); | |
6d004f67 | 4406 | |
bec70262 VZ |
4407 | // we only need the ordinate (for rows) or abscissa (for columns) here, |
4408 | // and need to cover the entire window in the other direction | |
4409 | oper.Select(rect) = 0; | |
6d004f67 | 4410 | |
bec70262 VZ |
4411 | wxRect rectHeader(rect.GetPosition(), |
4412 | oper.MakeSize | |
4413 | ( | |
4414 | oper.GetHeaderWindowSize(this), | |
4415 | doper.Select(size) - doper.Select(rect) | |
4416 | )); | |
4417 | ||
4418 | oper.GetHeaderWindow(this)->Refresh(true, &rectHeader); | |
4419 | ||
4420 | ||
4421 | // also refresh the grid window: extend the rectangle | |
4422 | if ( m_table ) | |
6d004f67 | 4423 | { |
bec70262 | 4424 | oper.SelectSize(rect) = oper.Select(size); |
2f024384 | 4425 | |
bec70262 | 4426 | int subtractLines = 0; |
7d1214cd PC |
4427 | int line = doper.PosToLine(this, posLineStart); |
4428 | if ( line >= 0 ) | |
27f35b66 | 4429 | { |
bec70262 VZ |
4430 | // ensure that if we have a multi-cell block we redraw all of |
4431 | // it by increasing the refresh area to cover it entirely if a | |
4432 | // part of it is affected | |
13350621 | 4433 | const int lineEnd = doper.PosToLine(this, posLineEnd, true); |
7d1214cd | 4434 | for ( ; line < lineEnd; line++ ) |
27f35b66 | 4435 | { |
bec70262 VZ |
4436 | int cellLines = oper.Select( |
4437 | GetCellSize(oper.MakeCoords(m_dragRowOrCol, line))); | |
4438 | if ( cellLines < subtractLines ) | |
4439 | subtractLines = cellLines; | |
27f35b66 SN |
4440 | } |
4441 | } | |
2f024384 | 4442 | |
bec70262 VZ |
4443 | int startPos = |
4444 | oper.GetLineStartPos(this, m_dragRowOrCol + subtractLines); | |
4445 | startPos = doper.CalcScrolledPosition(this, startPos); | |
4446 | ||
4447 | doper.Select(rect) = startPos; | |
4448 | doper.SelectSize(rect) = doper.Select(size) - startPos; | |
e2b42eeb | 4449 | |
bec70262 VZ |
4450 | m_gridWin->Refresh(false, &rect); |
4451 | } | |
f85afd4e | 4452 | } |
bec70262 VZ |
4453 | |
4454 | // show the edit control back again | |
4455 | ShowCellEditControl(); | |
27bb2c8c VZ |
4456 | |
4457 | return sizeChanged; | |
bec70262 VZ |
4458 | } |
4459 | ||
27bb2c8c | 4460 | void wxGrid::DoEndDragResizeRow(const wxMouseEvent& event) |
bec70262 | 4461 | { |
27bb2c8c VZ |
4462 | // TODO: generate RESIZING event, see #10754 |
4463 | ||
4464 | if ( DoEndDragResizeLine(wxGridRowOperations()) ) | |
c5c1ea96 | 4465 | SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event); |
bec70262 VZ |
4466 | } |
4467 | ||
27bb2c8c | 4468 | void wxGrid::DoEndDragResizeCol(const wxMouseEvent& event) |
bec70262 | 4469 | { |
27bb2c8c | 4470 | // TODO: generate RESIZING event, see #10754 |
ad805b9e | 4471 | |
27bb2c8c | 4472 | if ( DoEndDragResizeLine(wxGridColumnOperations()) ) |
c5c1ea96 | 4473 | SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event); |
f85afd4e MB |
4474 | } |
4475 | ||
cd68daf5 | 4476 | void wxGrid::DoStartMoveCol(int col) |
d4175745 | 4477 | { |
cd68daf5 VZ |
4478 | m_dragRowOrCol = col; |
4479 | } | |
d4175745 | 4480 | |
cd68daf5 VZ |
4481 | void wxGrid::DoEndMoveCol(int pos) |
4482 | { | |
4483 | wxASSERT_MSG( m_dragRowOrCol != -1, "no matching DoStartMoveCol?" ); | |
4484 | ||
4485 | if ( SendEvent(wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol) != -1 ) | |
4486 | SetColPos(m_dragRowOrCol, pos); | |
4487 | //else: vetoed by user | |
d4175745 | 4488 | |
cd68daf5 | 4489 | m_dragRowOrCol = -1; |
d4175745 VZ |
4490 | } |
4491 | ||
4797b014 | 4492 | void wxGrid::RefreshAfterColPosChange() |
009c7216 | 4493 | { |
4797b014 VZ |
4494 | // recalculate the column rights as the column positions have changed, |
4495 | // unless we calculate them dynamically because all columns widths are the | |
4496 | // same and it's easy to do | |
4497 | if ( !m_colWidths.empty() ) | |
009c7216 | 4498 | { |
4797b014 VZ |
4499 | int colRight = 0; |
4500 | for ( int colPos = 0; colPos < m_numCols; colPos++ ) | |
4501 | { | |
4502 | int colID = GetColAt( colPos ); | |
4503 | ||
4504 | colRight += m_colWidths[colID]; | |
4505 | m_colRights[colID] = colRight; | |
4506 | } | |
4507 | } | |
009c7216 | 4508 | |
4797b014 VZ |
4509 | // and make the changes visible |
4510 | if ( m_useNativeHeader ) | |
4511 | { | |
4512 | if ( m_colAt.empty() ) | |
3039ade9 | 4513 | GetGridColHeader()->ResetColumnsOrder(); |
4797b014 | 4514 | else |
3039ade9 | 4515 | GetGridColHeader()->SetColumnsOrder(m_colAt); |
4797b014 VZ |
4516 | } |
4517 | else | |
4518 | { | |
4519 | m_colWindow->Refresh(); | |
009c7216 | 4520 | } |
4797b014 | 4521 | m_gridWin->Refresh(); |
009c7216 VZ |
4522 | } |
4523 | ||
31ec8b4e VZ |
4524 | void wxGrid::SetColumnsOrder(const wxArrayInt& order) |
4525 | { | |
4526 | m_colAt = order; | |
4527 | ||
4528 | RefreshAfterColPosChange(); | |
4529 | } | |
4530 | ||
3169a8e8 | 4531 | void wxGrid::SetColPos(int idx, int pos) |
d4175745 | 4532 | { |
1bb74626 | 4533 | // we're going to need m_colAt now, initialize it if needed |
3169a8e8 | 4534 | if ( m_colAt.empty() ) |
d4175745 | 4535 | { |
3169a8e8 VZ |
4536 | m_colAt.reserve(m_numCols); |
4537 | for ( int i = 0; i < m_numCols; i++ ) | |
4538 | m_colAt.push_back(i); | |
d4175745 VZ |
4539 | } |
4540 | ||
1bb74626 | 4541 | wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos); |
d4175745 | 4542 | |
4797b014 | 4543 | RefreshAfterColPosChange(); |
d4175745 VZ |
4544 | } |
4545 | ||
009c7216 VZ |
4546 | void wxGrid::ResetColPos() |
4547 | { | |
4548 | m_colAt.clear(); | |
da5a641f | 4549 | |
4797b014 | 4550 | RefreshAfterColPosChange(); |
009c7216 | 4551 | } |
d4175745 VZ |
4552 | |
4553 | void wxGrid::EnableDragColMove( bool enable ) | |
4554 | { | |
4555 | if ( m_canDragColMove == enable ) | |
4556 | return; | |
4557 | ||
009c7216 | 4558 | if ( m_useNativeHeader ) |
d4175745 | 4559 | { |
009c7216 | 4560 | // update all columns to make them [not] reorderable |
3039ade9 | 4561 | GetGridColHeader()->SetColumnCount(m_numCols); |
009c7216 | 4562 | } |
d4175745 | 4563 | |
009c7216 | 4564 | m_canDragColMove = enable; |
d4175745 | 4565 | |
009c7216 VZ |
4566 | // we use to call ResetColPos() from here if !enable but this doesn't seem |
4567 | // right as it would mean there would be no way to "freeze" the current | |
4568 | // columns order by disabling moving them after putting them in the desired | |
4569 | // order, whereas now you can always call ResetColPos() manually if needed | |
d4175745 VZ |
4570 | } |
4571 | ||
4572 | ||
2d66e025 MB |
4573 | // |
4574 | // ------ interaction with data model | |
4575 | // | |
4576 | bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg ) | |
f85afd4e | 4577 | { |
2d66e025 | 4578 | switch ( msg.GetId() ) |
17732cec | 4579 | { |
2d66e025 MB |
4580 | case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES: |
4581 | return GetModelValues(); | |
17732cec | 4582 | |
2d66e025 MB |
4583 | case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES: |
4584 | return SetModelValues(); | |
f85afd4e | 4585 | |
2d66e025 MB |
4586 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: |
4587 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
4588 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
4589 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
4590 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
4591 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
4592 | return Redimension( msg ); | |
4593 | ||
4594 | default: | |
ca65c044 | 4595 | return false; |
f85afd4e | 4596 | } |
2d66e025 | 4597 | } |
f85afd4e | 4598 | |
2d66e025 | 4599 | // The behaviour of this function depends on the grid table class |
5b061713 | 4600 | // Clear() function. For the default wxGridStringTable class the |
10a4531d | 4601 | // behaviour is to replace all cell contents with wxEmptyString but |
2d66e025 MB |
4602 | // not to change the number of rows or cols. |
4603 | // | |
4604 | void wxGrid::ClearGrid() | |
4605 | { | |
4606 | if ( m_table ) | |
f85afd4e | 4607 | { |
4cfa5de6 RD |
4608 | if (IsCellEditControlEnabled()) |
4609 | DisableCellEditControl(); | |
4610 | ||
2d66e025 | 4611 | m_table->Clear(); |
5b061713 | 4612 | if (!GetBatchCount()) |
a9339fe2 | 4613 | m_gridWin->Refresh(); |
f85afd4e MB |
4614 | } |
4615 | } | |
4616 | ||
10a4531d VZ |
4617 | bool |
4618 | wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify)(size_t, size_t), | |
4619 | int pos, int num, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 4620 | { |
10a4531d | 4621 | wxCHECK_MSG( m_created, false, "must finish creating the grid first" ); |
f85afd4e | 4622 | |
10a4531d | 4623 | if ( !m_table ) |
ca65c044 | 4624 | return false; |
f85afd4e | 4625 | |
10a4531d VZ |
4626 | if ( IsCellEditControlEnabled() ) |
4627 | DisableCellEditControl(); | |
b7fff980 | 4628 | |
10a4531d | 4629 | return (m_table->*funcModify)(pos, num); |
2f024384 | 4630 | |
10a4531d VZ |
4631 | // the table will have sent the results of the insert row |
4632 | // operation to this view object as a grid table message | |
f85afd4e MB |
4633 | } |
4634 | ||
10a4531d VZ |
4635 | bool |
4636 | wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend)(size_t), | |
4637 | int num, bool WXUNUSED(updateLabels)) | |
f85afd4e | 4638 | { |
10a4531d | 4639 | wxCHECK_MSG( m_created, false, "must finish creating the grid first" ); |
2f024384 | 4640 | |
10a4531d | 4641 | if ( !m_table ) |
ca65c044 | 4642 | return false; |
f85afd4e | 4643 | |
10a4531d | 4644 | return (m_table->*funcAppend)(num); |
f85afd4e MB |
4645 | } |
4646 | ||
27bb2c8c VZ |
4647 | // ---------------------------------------------------------------------------- |
4648 | // event generation helpers | |
4649 | // ---------------------------------------------------------------------------- | |
4650 | ||
6f58f3d7 | 4651 | bool |
c5c1ea96 | 4652 | wxGrid::SendGridSizeEvent(wxEventType type, |
27bb2c8c VZ |
4653 | int row, int col, |
4654 | const wxMouseEvent& mouseEv) | |
4655 | { | |
4656 | int rowOrCol = row == -1 ? col : row; | |
4657 | ||
4658 | wxGridSizeEvent gridEvt( GetId(), | |
4659 | type, | |
4660 | this, | |
4661 | rowOrCol, | |
4662 | mouseEv.GetX() + GetRowLabelSize(), | |
4663 | mouseEv.GetY() + GetColLabelSize(), | |
4664 | mouseEv); | |
4665 | ||
6f58f3d7 | 4666 | return GetEventHandler()->ProcessEvent(gridEvt); |
27bb2c8c | 4667 | } |
8f177c8e | 4668 | |
8a3e536c VZ |
4669 | // Generate a grid event based on a mouse event and return: |
4670 | // -1 if the event was vetoed | |
4671 | // +1 if the event was processed (but not vetoed) | |
4672 | // 0 if the event wasn't handled | |
4673 | int | |
aced1133 | 4674 | wxGrid::SendEvent(const wxEventType type, |
8a3e536c | 4675 | int row, int col, |
27bb2c8c | 4676 | const wxMouseEvent& mouseEv) |
2d66e025 | 4677 | { |
2f024384 | 4678 | bool claimed, vetoed; |
fe7b9ed6 | 4679 | |
27bb2c8c | 4680 | if ( type == wxEVT_GRID_RANGE_SELECT ) |
97a9929e VZ |
4681 | { |
4682 | // Right now, it should _never_ end up here! | |
4683 | wxGridRangeSelectEvent gridEvt( GetId(), | |
4684 | type, | |
4685 | this, | |
8a3e536c VZ |
4686 | m_selectedBlockTopLeft, |
4687 | m_selectedBlockBottomRight, | |
ca65c044 | 4688 | true, |
8b5f6d9d | 4689 | mouseEv); |
97a9929e VZ |
4690 | |
4691 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
4692 | vetoed = !gridEvt.IsAllowed(); | |
4693 | } | |
2b73a34e RD |
4694 | else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK || |
4695 | type == wxEVT_GRID_LABEL_LEFT_DCLICK || | |
4696 | type == wxEVT_GRID_LABEL_RIGHT_CLICK || | |
4697 | type == wxEVT_GRID_LABEL_RIGHT_DCLICK ) | |
4698 | { | |
4699 | wxPoint pos = mouseEv.GetPosition(); | |
4700 | ||
4701 | if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() ) | |
4702 | pos.y += GetColLabelSize(); | |
4703 | if ( mouseEv.GetEventObject() == GetGridColLabelWindow() ) | |
4704 | pos.x += GetRowLabelSize(); | |
c71b2126 | 4705 | |
2b73a34e RD |
4706 | wxGridEvent gridEvt( GetId(), |
4707 | type, | |
4708 | this, | |
4709 | row, col, | |
4710 | pos.x, | |
4711 | pos.y, | |
4712 | false, | |
8b5f6d9d | 4713 | mouseEv); |
2b73a34e RD |
4714 | claimed = GetEventHandler()->ProcessEvent(gridEvt); |
4715 | vetoed = !gridEvt.IsAllowed(); | |
4716 | } | |
fe7b9ed6 | 4717 | else |
97a9929e VZ |
4718 | { |
4719 | wxGridEvent gridEvt( GetId(), | |
4720 | type, | |
4721 | this, | |
4722 | row, col, | |
4723 | mouseEv.GetX() + GetRowLabelSize(), | |
4724 | mouseEv.GetY() + GetColLabelSize(), | |
ca65c044 | 4725 | false, |
8b5f6d9d | 4726 | mouseEv); |
0cbcb12d SC |
4727 | |
4728 | if ( type == wxEVT_GRID_CELL_BEGIN_DRAG ) | |
4729 | { | |
4730 | // by default the dragging is not supported, the user code must | |
4731 | // explicitly allow the event for it to take place | |
4732 | gridEvt.Veto(); | |
4733 | } | |
4734 | ||
97a9929e VZ |
4735 | claimed = GetEventHandler()->ProcessEvent(gridEvt); |
4736 | vetoed = !gridEvt.IsAllowed(); | |
4737 | } | |
4738 | ||
4739 | // A Veto'd event may not be `claimed' so test this first | |
4db6714b KH |
4740 | if (vetoed) |
4741 | return -1; | |
4742 | ||
97a9929e | 4743 | return claimed ? 1 : 0; |
f85afd4e MB |
4744 | } |
4745 | ||
8a3e536c | 4746 | // Generate a grid event of specified type, return value same as above |
f85afd4e | 4747 | // |
763163a8 VZ |
4748 | int |
4749 | wxGrid::SendEvent(const wxEventType type, int row, int col, const wxString& s) | |
f85afd4e | 4750 | { |
27bb2c8c VZ |
4751 | wxGridEvent gridEvt( GetId(), type, this, row, col ); |
4752 | gridEvt.SetString(s); | |
8f177c8e | 4753 | |
27bb2c8c | 4754 | const bool claimed = GetEventHandler()->ProcessEvent(gridEvt); |
fe7b9ed6 | 4755 | |
97a9929e | 4756 | // A Veto'd event may not be `claimed' so test this first |
27bb2c8c | 4757 | if ( !gridEvt.IsAllowed() ) |
4db6714b KH |
4758 | return -1; |
4759 | ||
97a9929e | 4760 | return claimed ? 1 : 0; |
f85afd4e MB |
4761 | } |
4762 | ||
2d66e025 | 4763 | void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
f85afd4e | 4764 | { |
3d59537f DS |
4765 | // needed to prevent zillions of paint events on MSW |
4766 | wxPaintDC dc(this); | |
8f177c8e | 4767 | } |
f85afd4e | 4768 | |
bca7bfc8 | 4769 | void wxGrid::Refresh(bool eraseb, const wxRect* rect) |
27f35b66 | 4770 | { |
ad603bf7 VZ |
4771 | // Don't do anything if between Begin/EndBatch... |
4772 | // EndBatch() will do all this on the last nested one anyway. | |
f9549841 | 4773 | if ( m_created && !GetBatchCount() ) |
27f35b66 | 4774 | { |
bca7bfc8 | 4775 | // Refresh to get correct scrolled position: |
4db6714b | 4776 | wxScrolledWindow::Refresh(eraseb, rect); |
27f35b66 | 4777 | |
27f35b66 SN |
4778 | if (rect) |
4779 | { | |
bca7bfc8 SN |
4780 | int rect_x, rect_y, rectWidth, rectHeight; |
4781 | int width_label, width_cell, height_label, height_cell; | |
4782 | int x, y; | |
4783 | ||
2f024384 | 4784 | // Copy rectangle can get scroll offsets.. |
bca7bfc8 SN |
4785 | rect_x = rect->GetX(); |
4786 | rect_y = rect->GetY(); | |
4787 | rectWidth = rect->GetWidth(); | |
4788 | rectHeight = rect->GetHeight(); | |
27f35b66 | 4789 | |
bca7bfc8 | 4790 | width_label = m_rowLabelWidth - rect_x; |
4db6714b KH |
4791 | if (width_label > rectWidth) |
4792 | width_label = rectWidth; | |
27f35b66 | 4793 | |
bca7bfc8 | 4794 | height_label = m_colLabelHeight - rect_y; |
a9339fe2 DS |
4795 | if (height_label > rectHeight) |
4796 | height_label = rectHeight; | |
27f35b66 | 4797 | |
bca7bfc8 SN |
4798 | if (rect_x > m_rowLabelWidth) |
4799 | { | |
4800 | x = rect_x - m_rowLabelWidth; | |
4801 | width_cell = rectWidth; | |
4802 | } | |
4803 | else | |
4804 | { | |
4805 | x = 0; | |
4806 | width_cell = rectWidth - (m_rowLabelWidth - rect_x); | |
4807 | } | |
4808 | ||
4809 | if (rect_y > m_colLabelHeight) | |
4810 | { | |
4811 | y = rect_y - m_colLabelHeight; | |
4812 | height_cell = rectHeight; | |
4813 | } | |
4814 | else | |
4815 | { | |
4816 | y = 0; | |
4817 | height_cell = rectHeight - (m_colLabelHeight - rect_y); | |
4818 | } | |
4819 | ||
4820 | // Paint corner label part intersecting rect. | |
4821 | if ( width_label > 0 && height_label > 0 ) | |
4822 | { | |
4823 | wxRect anotherrect(rect_x, rect_y, width_label, height_label); | |
4824 | m_cornerLabelWin->Refresh(eraseb, &anotherrect); | |
4825 | } | |
4826 | ||
4827 | // Paint col labels part intersecting rect. | |
4828 | if ( width_cell > 0 && height_label > 0 ) | |
4829 | { | |
4830 | wxRect anotherrect(x, rect_y, width_cell, height_label); | |
ad805b9e | 4831 | m_colWindow->Refresh(eraseb, &anotherrect); |
bca7bfc8 SN |
4832 | } |
4833 | ||
4834 | // Paint row labels part intersecting rect. | |
4835 | if ( width_label > 0 && height_cell > 0 ) | |
4836 | { | |
4837 | wxRect anotherrect(rect_x, y, width_label, height_cell); | |
4838 | m_rowLabelWin->Refresh(eraseb, &anotherrect); | |
4839 | } | |
4840 | ||
4841 | // Paint cell area part intersecting rect. | |
4842 | if ( width_cell > 0 && height_cell > 0 ) | |
4843 | { | |
4844 | wxRect anotherrect(x, y, width_cell, height_cell); | |
4845 | m_gridWin->Refresh(eraseb, &anotherrect); | |
4846 | } | |
4847 | } | |
4848 | else | |
4849 | { | |
4850 | m_cornerLabelWin->Refresh(eraseb, NULL); | |
ad805b9e | 4851 | m_colWindow->Refresh(eraseb, NULL); |
bca7bfc8 SN |
4852 | m_rowLabelWin->Refresh(eraseb, NULL); |
4853 | m_gridWin->Refresh(eraseb, NULL); | |
4854 | } | |
27f35b66 SN |
4855 | } |
4856 | } | |
f85afd4e | 4857 | |
c71b2126 | 4858 | void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event)) |
f85afd4e | 4859 | { |
b93aafab JS |
4860 | if (m_targetWindow != this) // check whether initialisation has been done |
4861 | { | |
f1ff7df0 VZ |
4862 | // reposition our children windows |
4863 | CalcWindowSizes(); | |
b93aafab | 4864 | } |
f85afd4e MB |
4865 | } |
4866 | ||
2d66e025 | 4867 | void wxGrid::OnKeyDown( wxKeyEvent& event ) |
f85afd4e | 4868 | { |
2d66e025 | 4869 | if ( m_inOnKeyDown ) |
f85afd4e | 4870 | { |
2d66e025 MB |
4871 | // shouldn't be here - we are going round in circles... |
4872 | // | |
07296f0b | 4873 | wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") ); |
f85afd4e MB |
4874 | } |
4875 | ||
ca65c044 | 4876 | m_inOnKeyDown = true; |
f85afd4e | 4877 | |
2d66e025 | 4878 | // propagate the event up and see if it gets processed |
2d66e025 MB |
4879 | wxWindow *parent = GetParent(); |
4880 | wxKeyEvent keyEvt( event ); | |
4881 | keyEvt.SetEventObject( parent ); | |
4882 | ||
4883 | if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) ) | |
f85afd4e | 4884 | { |
bcb614b3 RR |
4885 | if (GetLayoutDirection() == wxLayout_RightToLeft) |
4886 | { | |
4887 | if (event.GetKeyCode() == WXK_RIGHT) | |
4888 | event.m_keyCode = WXK_LEFT; | |
4889 | else if (event.GetKeyCode() == WXK_LEFT) | |
4890 | event.m_keyCode = WXK_RIGHT; | |
4891 | } | |
c71b2126 | 4892 | |
2d66e025 | 4893 | // try local handlers |
12a3f227 | 4894 | switch ( event.GetKeyCode() ) |
2d66e025 MB |
4895 | { |
4896 | case WXK_UP: | |
4897 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4898 | MoveCursorUpBlock( event.ShiftDown() ); |
2d66e025 | 4899 | else |
5c8fc7c1 | 4900 | MoveCursorUp( event.ShiftDown() ); |
2d66e025 | 4901 | break; |
f85afd4e | 4902 | |
2d66e025 MB |
4903 | case WXK_DOWN: |
4904 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4905 | MoveCursorDownBlock( event.ShiftDown() ); |
2d66e025 | 4906 | else |
5c8fc7c1 | 4907 | MoveCursorDown( event.ShiftDown() ); |
2d66e025 | 4908 | break; |
8f177c8e | 4909 | |
2d66e025 MB |
4910 | case WXK_LEFT: |
4911 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4912 | MoveCursorLeftBlock( event.ShiftDown() ); |
2d66e025 | 4913 | else |
5c8fc7c1 | 4914 | MoveCursorLeft( event.ShiftDown() ); |
2d66e025 MB |
4915 | break; |
4916 | ||
4917 | case WXK_RIGHT: | |
4918 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4919 | MoveCursorRightBlock( event.ShiftDown() ); |
2d66e025 | 4920 | else |
5c8fc7c1 | 4921 | MoveCursorRight( event.ShiftDown() ); |
2d66e025 | 4922 | break; |
b99be8fb | 4923 | |
2d66e025 | 4924 | case WXK_RETURN: |
a4f7bf58 | 4925 | case WXK_NUMPAD_ENTER: |
58dd5b3b MB |
4926 | if ( event.ControlDown() ) |
4927 | { | |
4928 | event.Skip(); // to let the edit control have the return | |
4929 | } | |
4930 | else | |
4931 | { | |
f6bcfd97 BP |
4932 | if ( GetGridCursorRow() < GetNumberRows()-1 ) |
4933 | { | |
4934 | MoveCursorDown( event.ShiftDown() ); | |
4935 | } | |
4936 | else | |
4937 | { | |
4938 | // at the bottom of a column | |
13f6e9e8 | 4939 | DisableCellEditControl(); |
f6bcfd97 | 4940 | } |
58dd5b3b | 4941 | } |
2d66e025 MB |
4942 | break; |
4943 | ||
5c8fc7c1 | 4944 | case WXK_ESCAPE: |
e32352cf | 4945 | ClearSelection(); |
5c8fc7c1 SN |
4946 | break; |
4947 | ||
2c9a89e0 | 4948 | case WXK_TAB: |
f6bcfd97 | 4949 | { |
1dc17bca VZ |
4950 | // send an event to the grid's parents for custom handling |
4951 | wxGridEvent gridEvt(GetId(), wxEVT_GRID_TABBING, this, | |
4952 | GetGridCursorRow(), GetGridCursorCol(), | |
4953 | -1, -1, false, event); | |
4954 | if ( ProcessWindowEvent(gridEvt) ) | |
f6bcfd97 | 4955 | { |
1dc17bca VZ |
4956 | // the event has been handled so no need for more processing |
4957 | break; | |
f6bcfd97 BP |
4958 | } |
4959 | } | |
1dc17bca | 4960 | DoGridProcessTab( event ); |
2c9a89e0 RD |
4961 | break; |
4962 | ||
2d66e025 | 4963 | case WXK_HOME: |
ad178a65 VZ |
4964 | GoToCell(event.ControlDown() ? 0 |
4965 | : m_currentCellCoords.GetRow(), | |
4966 | 0); | |
2d66e025 MB |
4967 | break; |
4968 | ||
4969 | case WXK_END: | |
ad178a65 VZ |
4970 | GoToCell(event.ControlDown() ? m_numRows - 1 |
4971 | : m_currentCellCoords.GetRow(), | |
4972 | m_numCols - 1); | |
2d66e025 MB |
4973 | break; |
4974 | ||
faa94f3e | 4975 | case WXK_PAGEUP: |
2d66e025 MB |
4976 | MovePageUp(); |
4977 | break; | |
4978 | ||
faa94f3e | 4979 | case WXK_PAGEDOWN: |
2d66e025 MB |
4980 | MovePageDown(); |
4981 | break; | |
4982 | ||
07296f0b | 4983 | case WXK_SPACE: |
32b4e9ec VZ |
4984 | // Ctrl-Space selects the current column, Shift-Space -- the |
4985 | // current row and Ctrl-Shift-Space -- everything | |
4986 | switch ( m_selection ? event.GetModifiers() : wxMOD_NONE ) | |
aa5e1f75 | 4987 | { |
32b4e9ec VZ |
4988 | case wxMOD_CONTROL: |
4989 | m_selection->SelectCol(m_currentCellCoords.GetCol()); | |
4990 | break; | |
ccdee36f | 4991 | |
32b4e9ec VZ |
4992 | case wxMOD_SHIFT: |
4993 | m_selection->SelectRow(m_currentCellCoords.GetRow()); | |
4994 | break; | |
4995 | ||
4996 | case wxMOD_CONTROL | wxMOD_SHIFT: | |
4997 | m_selection->SelectBlock(0, 0, | |
4998 | m_numRows - 1, m_numCols - 1); | |
4999 | break; | |
5000 | ||
5001 | case wxMOD_NONE: | |
5002 | if ( !IsEditable() ) | |
5003 | { | |
5004 | MoveCursorRight(false); | |
5005 | break; | |
5006 | } | |
5007 | //else: fall through | |
5008 | ||
5009 | default: | |
5010 | event.Skip(); | |
5011 | } | |
ccdee36f | 5012 | break; |
07296f0b | 5013 | |
2d66e025 | 5014 | default: |
63e2147c | 5015 | event.Skip(); |
025562fe | 5016 | break; |
2d66e025 | 5017 | } |
f85afd4e MB |
5018 | } |
5019 | ||
ca65c044 | 5020 | m_inOnKeyDown = false; |
f85afd4e MB |
5021 | } |
5022 | ||
f6bcfd97 BP |
5023 | void wxGrid::OnKeyUp( wxKeyEvent& event ) |
5024 | { | |
5025 | // try local handlers | |
5026 | // | |
12a3f227 | 5027 | if ( event.GetKeyCode() == WXK_SHIFT ) |
f6bcfd97 | 5028 | { |
8a3e536c VZ |
5029 | if ( m_selectedBlockTopLeft != wxGridNoCellCoords && |
5030 | m_selectedBlockBottomRight != wxGridNoCellCoords ) | |
3f3dc2ef VZ |
5031 | { |
5032 | if ( m_selection ) | |
5033 | { | |
a9339fe2 | 5034 | m_selection->SelectBlock( |
8a3e536c VZ |
5035 | m_selectedBlockTopLeft, |
5036 | m_selectedBlockBottomRight, | |
8b5f6d9d | 5037 | event); |
3f3dc2ef VZ |
5038 | } |
5039 | } | |
5040 | ||
8a3e536c VZ |
5041 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
5042 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
5043 | m_selectedBlockCorner = wxGridNoCellCoords; | |
f6bcfd97 BP |
5044 | } |
5045 | } | |
5046 | ||
63e2147c RD |
5047 | void wxGrid::OnChar( wxKeyEvent& event ) |
5048 | { | |
5049 | // is it possible to edit the current cell at all? | |
5050 | if ( !IsCellEditControlEnabled() && CanEnableCellControl() ) | |
5051 | { | |
5052 | // yes, now check whether the cells editor accepts the key | |
5053 | int row = m_currentCellCoords.GetRow(); | |
5054 | int col = m_currentCellCoords.GetCol(); | |
2f024384 | 5055 | wxGridCellAttr *attr = GetCellAttr(row, col); |
63e2147c RD |
5056 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
5057 | ||
5058 | // <F2> is special and will always start editing, for | |
5059 | // other keys - ask the editor itself | |
5060 | if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers()) | |
5061 | || editor->IsAcceptedKey(event) ) | |
5062 | { | |
5063 | // ensure cell is visble | |
5064 | MakeCellVisible(row, col); | |
5065 | EnableCellEditControl(); | |
5066 | ||
5067 | // a problem can arise if the cell is not completely | |
5068 | // visible (even after calling MakeCellVisible the | |
5069 | // control is not created and calling StartingKey will | |
5070 | // crash the app | |
046d682f | 5071 | if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled ) |
63e2147c RD |
5072 | editor->StartingKey(event); |
5073 | } | |
5074 | else | |
5075 | { | |
5076 | event.Skip(); | |
5077 | } | |
5078 | ||
5079 | editor->DecRef(); | |
5080 | attr->DecRef(); | |
5081 | } | |
5082 | else | |
5083 | { | |
5084 | event.Skip(); | |
5085 | } | |
5086 | } | |
5087 | ||
2796cce3 | 5088 | void wxGrid::OnEraseBackground(wxEraseEvent&) |
508011ce VZ |
5089 | { |
5090 | } | |
07296f0b | 5091 | |
1dc17bca VZ |
5092 | void wxGrid::DoGridProcessTab(wxKeyboardState& kbdState) |
5093 | { | |
5094 | const bool isForwardTab = !kbdState.ShiftDown(); | |
5095 | ||
5096 | // TAB processing only changes when we are at the borders of the grid, so | |
5097 | // let's first handle the common behaviour when we are not at the border. | |
5098 | if ( isForwardTab ) | |
5099 | { | |
5100 | if ( GetGridCursorCol() < GetNumberCols() - 1 ) | |
5101 | { | |
5102 | MoveCursorRight( false ); | |
5103 | return; | |
5104 | } | |
5105 | } | |
5106 | else // going back | |
5107 | { | |
5108 | if ( GetGridCursorCol() ) | |
5109 | { | |
5110 | MoveCursorLeft( false ); | |
5111 | return; | |
5112 | } | |
5113 | } | |
5114 | ||
5115 | ||
5116 | // We only get here if the cursor is at the border of the grid, apply the | |
5117 | // configured behaviour. | |
5118 | switch ( m_tabBehaviour ) | |
5119 | { | |
5120 | case Tab_Stop: | |
5121 | // Nothing special to do, we remain at the current cell. | |
5122 | break; | |
5123 | ||
5124 | case Tab_Wrap: | |
5125 | // Go to the beginning of the next or the end of the previous row. | |
5126 | if ( isForwardTab ) | |
5127 | { | |
5128 | if ( GetGridCursorRow() < GetNumberRows() - 1 ) | |
5129 | { | |
5130 | GoToCell( GetGridCursorRow() + 1, 0 ); | |
5131 | return; | |
5132 | } | |
5133 | } | |
5134 | else | |
5135 | { | |
5136 | if ( GetGridCursorRow() > 0 ) | |
5137 | { | |
5138 | GoToCell( GetGridCursorRow() - 1, GetNumberCols() - 1 ); | |
5139 | return; | |
5140 | } | |
5141 | } | |
5142 | break; | |
5143 | ||
5144 | case Tab_Leave: | |
5145 | if ( Navigate( isForwardTab ? wxNavigationKeyEvent::IsForward | |
5146 | : wxNavigationKeyEvent::IsBackward ) ) | |
5147 | return; | |
5148 | break; | |
5149 | } | |
5150 | ||
5151 | // If we remain in this cell, stop editing it if we were doing so. | |
5152 | DisableCellEditControl(); | |
5153 | } | |
5154 | ||
8a3e536c | 5155 | bool wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) |
66242c80 | 5156 | { |
8a3e536c | 5157 | if ( SendEvent(wxEVT_GRID_SELECT_CELL, coords) == -1 ) |
f6bcfd97 | 5158 | { |
8a3e536c VZ |
5159 | // the event has been vetoed - do nothing |
5160 | return false; | |
f6bcfd97 BP |
5161 | } |
5162 | ||
9553702e | 5163 | #if !defined(__WXMAC__) |
bee19958 DS |
5164 | wxClientDC dc( m_gridWin ); |
5165 | PrepareDC( dc ); | |
5d38a5f3 | 5166 | #endif |
f6bcfd97 | 5167 | |
2a7750d9 | 5168 | if ( m_currentCellCoords != wxGridNoCellCoords ) |
2d66e025 | 5169 | { |
b54ba671 | 5170 | DisableCellEditControl(); |
07296f0b | 5171 | |
ca65c044 | 5172 | if ( IsVisible( m_currentCellCoords, false ) ) |
f6bcfd97 BP |
5173 | { |
5174 | wxRect r; | |
bee19958 | 5175 | r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords ); |
f6bcfd97 BP |
5176 | if ( !m_gridLinesEnabled ) |
5177 | { | |
5178 | r.x--; | |
5179 | r.y--; | |
5180 | r.width++; | |
5181 | r.height++; | |
5182 | } | |
d1c0b4f9 | 5183 | |
3ed884a0 | 5184 | wxGridCellCoordsArray cells = CalcCellsExposed( r ); |
84912ef8 | 5185 | |
f6bcfd97 BP |
5186 | // Otherwise refresh redraws the highlight! |
5187 | m_currentCellCoords = coords; | |
275c4ae4 | 5188 | |
9553702e | 5189 | #if defined(__WXMAC__) |
5d38a5f3 JS |
5190 | m_gridWin->Refresh(true /*, & r */); |
5191 | #else | |
bee19958 | 5192 | DrawGridCellArea( dc, cells ); |
f6bcfd97 | 5193 | DrawAllGridLines( dc, r ); |
5d38a5f3 | 5194 | #endif |
f6bcfd97 | 5195 | } |
66242c80 | 5196 | } |
8f177c8e | 5197 | |
2d66e025 MB |
5198 | m_currentCellCoords = coords; |
5199 | ||
bee19958 | 5200 | wxGridCellAttr *attr = GetCellAttr( coords ); |
76c66f19 | 5201 | #if !defined(__WXMAC__) |
bee19958 | 5202 | DrawCellHighlight( dc, attr ); |
5d38a5f3 | 5203 | #endif |
2a7750d9 | 5204 | attr->DecRef(); |
8a3e536c VZ |
5205 | |
5206 | return true; | |
66242c80 MB |
5207 | } |
5208 | ||
8a3e536c VZ |
5209 | void |
5210 | wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol, | |
5211 | int bottomRow, int rightCol) | |
c9097836 | 5212 | { |
4445d035 VZ |
5213 | MakeCellVisible(m_selectedBlockCorner); |
5214 | m_selectedBlockCorner = wxGridCellCoords(bottomRow, rightCol); | |
5215 | ||
3f3dc2ef | 5216 | if ( m_selection ) |
c9097836 | 5217 | { |
8a3e536c | 5218 | switch ( m_selection->GetSelectionMode() ) |
3f3dc2ef | 5219 | { |
8a3e536c VZ |
5220 | default: |
5221 | wxFAIL_MSG( "unknown selection mode" ); | |
5222 | // fall through | |
5223 | ||
5224 | case wxGridSelectCells: | |
5225 | // arbitrary blocks selection allowed so just use the cell | |
5226 | // coordinates as is | |
5227 | break; | |
5228 | ||
5229 | case wxGridSelectRows: | |
5230 | // only full rows selection allowd, ensure that we do select | |
5231 | // full rows | |
5232 | leftCol = 0; | |
5233 | rightCol = GetNumberCols() - 1; | |
5234 | break; | |
5235 | ||
5236 | case wxGridSelectColumns: | |
5237 | // same as above but for columns | |
5238 | topRow = 0; | |
5239 | bottomRow = GetNumberRows() - 1; | |
5240 | break; | |
5241 | ||
5242 | case wxGridSelectRowsOrColumns: | |
5243 | // in this mode we can select only full rows or full columns so | |
5244 | // it doesn't make sense to select blocks at all (and we can't | |
5245 | // extend the block because there is no preferred direction, we | |
5246 | // could only extend it to cover the entire grid but this is | |
5247 | // not useful) | |
5248 | return; | |
3f3dc2ef | 5249 | } |
c9097836 | 5250 | } |
3f3dc2ef | 5251 | |
1372f8cc VZ |
5252 | EnsureFirstLessThanSecond(topRow, bottomRow); |
5253 | EnsureFirstLessThanSecond(leftCol, rightCol); | |
c9097836 | 5254 | |
8a3e536c VZ |
5255 | wxGridCellCoords updateTopLeft = wxGridCellCoords(topRow, leftCol), |
5256 | updateBottomRight = wxGridCellCoords(bottomRow, rightCol); | |
c9097836 | 5257 | |
3ed884a0 | 5258 | // First the case that we selected a completely new area |
8a3e536c VZ |
5259 | if ( m_selectedBlockTopLeft == wxGridNoCellCoords || |
5260 | m_selectedBlockBottomRight == wxGridNoCellCoords ) | |
3ed884a0 SN |
5261 | { |
5262 | wxRect rect; | |
5263 | rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ), | |
5264 | wxGridCellCoords ( bottomRow, rightCol ) ); | |
ca65c044 | 5265 | m_gridWin->Refresh( false, &rect ); |
3ed884a0 | 5266 | } |
2f024384 | 5267 | |
3ed884a0 | 5268 | // Now handle changing an existing selection area. |
8a3e536c VZ |
5269 | else if ( m_selectedBlockTopLeft != updateTopLeft || |
5270 | m_selectedBlockBottomRight != updateBottomRight ) | |
c9097836 MB |
5271 | { |
5272 | // Compute two optimal update rectangles: | |
5273 | // Either one rectangle is a real subset of the | |
5274 | // other, or they are (almost) disjoint! | |
5275 | wxRect rect[4]; | |
5276 | bool need_refresh[4]; | |
5277 | need_refresh[0] = | |
5278 | need_refresh[1] = | |
5279 | need_refresh[2] = | |
ca65c044 | 5280 | need_refresh[3] = false; |
c9097836 MB |
5281 | int i; |
5282 | ||
5283 | // Store intermediate values | |
8a3e536c VZ |
5284 | wxCoord oldLeft = m_selectedBlockTopLeft.GetCol(); |
5285 | wxCoord oldTop = m_selectedBlockTopLeft.GetRow(); | |
5286 | wxCoord oldRight = m_selectedBlockBottomRight.GetCol(); | |
5287 | wxCoord oldBottom = m_selectedBlockBottomRight.GetRow(); | |
c9097836 MB |
5288 | |
5289 | // Determine the outer/inner coordinates. | |
1372f8cc VZ |
5290 | EnsureFirstLessThanSecond(oldLeft, leftCol); |
5291 | EnsureFirstLessThanSecond(oldTop, topRow); | |
5292 | EnsureFirstLessThanSecond(rightCol, oldRight); | |
5293 | EnsureFirstLessThanSecond(bottomRow, oldBottom); | |
c9097836 MB |
5294 | |
5295 | // Now, either the stuff marked old is the outer | |
5296 | // rectangle or we don't have a situation where one | |
5297 | // is contained in the other. | |
5298 | ||
5299 | if ( oldLeft < leftCol ) | |
5300 | { | |
3ed884a0 SN |
5301 | // Refresh the newly selected or deselected |
5302 | // area to the left of the old or new selection. | |
ca65c044 | 5303 | need_refresh[0] = true; |
a9339fe2 DS |
5304 | rect[0] = BlockToDeviceRect( |
5305 | wxGridCellCoords( oldTop, oldLeft ), | |
5306 | wxGridCellCoords( oldBottom, leftCol - 1 ) ); | |
c9097836 MB |
5307 | } |
5308 | ||
2f024384 | 5309 | if ( oldTop < topRow ) |
c9097836 | 5310 | { |
3ed884a0 SN |
5311 | // Refresh the newly selected or deselected |
5312 | // area above the old or new selection. | |
ca65c044 | 5313 | need_refresh[1] = true; |
2f024384 DS |
5314 | rect[1] = BlockToDeviceRect( |
5315 | wxGridCellCoords( oldTop, leftCol ), | |
5316 | wxGridCellCoords( topRow - 1, rightCol ) ); | |
c9097836 MB |
5317 | } |
5318 | ||
5319 | if ( oldRight > rightCol ) | |
5320 | { | |
3ed884a0 SN |
5321 | // Refresh the newly selected or deselected |
5322 | // area to the right of the old or new selection. | |
ca65c044 | 5323 | need_refresh[2] = true; |
2f024384 | 5324 | rect[2] = BlockToDeviceRect( |
a9339fe2 DS |
5325 | wxGridCellCoords( oldTop, rightCol + 1 ), |
5326 | wxGridCellCoords( oldBottom, oldRight ) ); | |
c9097836 MB |
5327 | } |
5328 | ||
5329 | if ( oldBottom > bottomRow ) | |
5330 | { | |
3ed884a0 SN |
5331 | // Refresh the newly selected or deselected |
5332 | // area below the old or new selection. | |
ca65c044 | 5333 | need_refresh[3] = true; |
2f024384 | 5334 | rect[3] = BlockToDeviceRect( |
a9339fe2 DS |
5335 | wxGridCellCoords( bottomRow + 1, leftCol ), |
5336 | wxGridCellCoords( oldBottom, rightCol ) ); | |
c9097836 MB |
5337 | } |
5338 | ||
c9097836 MB |
5339 | // various Refresh() calls |
5340 | for (i = 0; i < 4; i++ ) | |
5341 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
ca65c044 | 5342 | m_gridWin->Refresh( false, &(rect[i]) ); |
c9097836 | 5343 | } |
2f024384 DS |
5344 | |
5345 | // change selection | |
8a3e536c VZ |
5346 | m_selectedBlockTopLeft = updateTopLeft; |
5347 | m_selectedBlockBottomRight = updateBottomRight; | |
c9097836 MB |
5348 | } |
5349 | ||
2d66e025 MB |
5350 | // |
5351 | // ------ functions to get/send data (see also public functions) | |
5352 | // | |
5353 | ||
5354 | bool wxGrid::GetModelValues() | |
66242c80 | 5355 | { |
bca7bfc8 SN |
5356 | // Hide the editor, so it won't hide a changed value. |
5357 | HideCellEditControl(); | |
c6707d16 | 5358 | |
2d66e025 | 5359 | if ( m_table ) |
66242c80 | 5360 | { |
2d66e025 | 5361 | // all we need to do is repaint the grid |
66242c80 | 5362 | // |
2d66e025 | 5363 | m_gridWin->Refresh(); |
ca65c044 | 5364 | return true; |
66242c80 | 5365 | } |
8f177c8e | 5366 | |
ca65c044 | 5367 | return false; |
66242c80 MB |
5368 | } |
5369 | ||
2d66e025 | 5370 | bool wxGrid::SetModelValues() |
f85afd4e | 5371 | { |
2d66e025 | 5372 | int row, col; |
8f177c8e | 5373 | |
c6707d16 SN |
5374 | // Disable the editor, so it won't hide a changed value. |
5375 | // Do we also want to save the current value of the editor first? | |
5376 | // I think so ... | |
c6707d16 SN |
5377 | DisableCellEditControl(); |
5378 | ||
2d66e025 MB |
5379 | if ( m_table ) |
5380 | { | |
56b6cf26 | 5381 | for ( row = 0; row < m_numRows; row++ ) |
f85afd4e | 5382 | { |
56b6cf26 | 5383 | for ( col = 0; col < m_numCols; col++ ) |
f85afd4e | 5384 | { |
2d66e025 | 5385 | m_table->SetValue( row, col, GetCellValue(row, col) ); |
f85afd4e MB |
5386 | } |
5387 | } | |
8f177c8e | 5388 | |
ca65c044 | 5389 | return true; |
f85afd4e MB |
5390 | } |
5391 | ||
ca65c044 | 5392 | return false; |
f85afd4e MB |
5393 | } |
5394 | ||
2d66e025 MB |
5395 | // Note - this function only draws cells that are in the list of |
5396 | // exposed cells (usually set from the update region by | |
5397 | // CalcExposedCells) | |
5398 | // | |
d10f4bf9 | 5399 | void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells ) |
f85afd4e | 5400 | { |
4db6714b KH |
5401 | if ( !m_numRows || !m_numCols ) |
5402 | return; | |
60ff3b99 | 5403 | |
dc1f566f | 5404 | int i, numCells = cells.GetCount(); |
27f35b66 SN |
5405 | int row, col, cell_rows, cell_cols; |
5406 | wxGridCellCoordsArray redrawCells; | |
60ff3b99 | 5407 | |
a9339fe2 | 5408 | for ( i = numCells - 1; i >= 0; i-- ) |
f85afd4e | 5409 | { |
27f35b66 SN |
5410 | row = cells[i].GetRow(); |
5411 | col = cells[i].GetCol(); | |
5412 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
5413 | ||
5414 | // If this cell is part of a multicell block, find owner for repaint | |
5415 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
5416 | { | |
a9339fe2 | 5417 | wxGridCellCoords cell( row + cell_rows, col + cell_cols ); |
ca65c044 | 5418 | bool marked = false; |
56b6cf26 | 5419 | for ( int j = 0; j < numCells; j++ ) |
27f35b66 SN |
5420 | { |
5421 | if ( cell == cells[j] ) | |
5422 | { | |
ca65c044 | 5423 | marked = true; |
3ed884a0 | 5424 | break; |
27f35b66 SN |
5425 | } |
5426 | } | |
2f024384 | 5427 | |
27f35b66 SN |
5428 | if (!marked) |
5429 | { | |
5430 | int count = redrawCells.GetCount(); | |
dc1f566f | 5431 | for (int j = 0; j < count; j++) |
27f35b66 SN |
5432 | { |
5433 | if ( cell == redrawCells[j] ) | |
5434 | { | |
ca65c044 | 5435 | marked = true; |
27f35b66 SN |
5436 | break; |
5437 | } | |
5438 | } | |
2f024384 | 5439 | |
4db6714b KH |
5440 | if (!marked) |
5441 | redrawCells.Add( cell ); | |
27f35b66 | 5442 | } |
2f024384 DS |
5443 | |
5444 | // don't bother drawing this cell | |
5445 | continue; | |
27f35b66 SN |
5446 | } |
5447 | ||
5448 | // If this cell is empty, find cell to left that might want to overflow | |
5449 | if (m_table && m_table->IsEmptyCell(row, col)) | |
5450 | { | |
dc1f566f | 5451 | for ( int l = 0; l < cell_rows; l++ ) |
27f35b66 | 5452 | { |
56b6cf26 | 5453 | // find a cell in this row to leave already marked for repaint |
dc1f566f SN |
5454 | int left = col; |
5455 | for (int k = 0; k < int(redrawCells.GetCount()); k++) | |
5456 | if ((redrawCells[k].GetCol() < left) && | |
5457 | (redrawCells[k].GetRow() == row)) | |
2f024384 | 5458 | { |
4db6714b | 5459 | left = redrawCells[k].GetCol(); |
2f024384 | 5460 | } |
dc1f566f | 5461 | |
4db6714b KH |
5462 | if (left == col) |
5463 | left = 0; // oh well | |
dc1f566f | 5464 | |
2f024384 | 5465 | for (int j = col - 1; j >= left; j--) |
27f35b66 | 5466 | { |
2f024384 | 5467 | if (!m_table->IsEmptyCell(row + l, j)) |
27f35b66 | 5468 | { |
2f024384 | 5469 | if (GetCellOverflow(row + l, j)) |
27f35b66 | 5470 | { |
2f024384 | 5471 | wxGridCellCoords cell(row + l, j); |
ca65c044 | 5472 | bool marked = false; |
27f35b66 | 5473 | |
dc1f566f | 5474 | for (int k = 0; k < numCells; k++) |
27f35b66 SN |
5475 | { |
5476 | if ( cell == cells[k] ) | |
5477 | { | |
ca65c044 | 5478 | marked = true; |
27f35b66 SN |
5479 | break; |
5480 | } | |
5481 | } | |
4db6714b | 5482 | |
27f35b66 SN |
5483 | if (!marked) |
5484 | { | |
5485 | int count = redrawCells.GetCount(); | |
dc1f566f | 5486 | for (int k = 0; k < count; k++) |
27f35b66 SN |
5487 | { |
5488 | if ( cell == redrawCells[k] ) | |
5489 | { | |
ca65c044 | 5490 | marked = true; |
27f35b66 SN |
5491 | break; |
5492 | } | |
5493 | } | |
4db6714b KH |
5494 | if (!marked) |
5495 | redrawCells.Add( cell ); | |
27f35b66 SN |
5496 | } |
5497 | } | |
5498 | break; | |
5499 | } | |
5500 | } | |
5501 | } | |
5502 | } | |
4db6714b | 5503 | |
d10f4bf9 | 5504 | DrawCell( dc, cells[i] ); |
2d66e025 | 5505 | } |
27f35b66 SN |
5506 | |
5507 | numCells = redrawCells.GetCount(); | |
5508 | ||
56b6cf26 | 5509 | for ( i = numCells - 1; i >= 0; i-- ) |
27f35b66 SN |
5510 | { |
5511 | DrawCell( dc, redrawCells[i] ); | |
5512 | } | |
2d66e025 | 5513 | } |
8f177c8e | 5514 | |
7c8a8ad5 MB |
5515 | void wxGrid::DrawGridSpace( wxDC& dc ) |
5516 | { | |
f6bcfd97 BP |
5517 | int cw, ch; |
5518 | m_gridWin->GetClientSize( &cw, &ch ); | |
7c8a8ad5 | 5519 | |
f6bcfd97 BP |
5520 | int right, bottom; |
5521 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7c8a8ad5 | 5522 | |
d4175745 | 5523 | int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0; |
2f024384 | 5524 | int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0; |
7c8a8ad5 | 5525 | |
f6bcfd97 BP |
5526 | if ( right > rightCol || bottom > bottomRow ) |
5527 | { | |
5528 | int left, top; | |
5529 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7c8a8ad5 | 5530 | |
ff72f628 | 5531 | dc.SetBrush(GetDefaultCellBackgroundColour()); |
f6bcfd97 | 5532 | dc.SetPen( *wxTRANSPARENT_PEN ); |
7c8a8ad5 | 5533 | |
f6bcfd97 BP |
5534 | if ( right > rightCol ) |
5535 | { | |
a9339fe2 | 5536 | dc.DrawRectangle( rightCol, top, right - rightCol, ch ); |
f6bcfd97 BP |
5537 | } |
5538 | ||
5539 | if ( bottom > bottomRow ) | |
5540 | { | |
a9339fe2 | 5541 | dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow ); |
f6bcfd97 BP |
5542 | } |
5543 | } | |
7c8a8ad5 MB |
5544 | } |
5545 | ||
2d66e025 MB |
5546 | void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) |
5547 | { | |
ab79958a VZ |
5548 | int row = coords.GetRow(); |
5549 | int col = coords.GetCol(); | |
60ff3b99 | 5550 | |
7c1cb261 | 5551 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
ab79958a VZ |
5552 | return; |
5553 | ||
5554 | // we draw the cell border ourselves | |
189d0213 VZ |
5555 | wxGridCellAttr* attr = GetCellAttr(row, col); |
5556 | ||
5557 | bool isCurrent = coords == m_currentCellCoords; | |
508011ce | 5558 | |
f6bcfd97 | 5559 | wxRect rect = CellToRect( row, col ); |
f85afd4e | 5560 | |
189d0213 | 5561 | // if the editor is shown, we should use it and not the renderer |
f6bcfd97 BP |
5562 | // Note: However, only if it is really _shown_, i.e. not hidden! |
5563 | if ( isCurrent && IsCellEditControlShown() ) | |
189d0213 | 5564 | { |
a9339fe2 | 5565 | // NB: this "#if..." is temporary and fixes a problem where the |
962a48f6 DS |
5566 | // edit control is erased by this code after being rendered. |
5567 | // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered | |
5568 | // implicitly, causing this out-of order render. | |
5d38a5f3 | 5569 | #if !defined(__WXMAC__) |
0b190b0f | 5570 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
ccc04025 | 5571 | editor->PaintBackground(dc, rect, *attr); |
0b190b0f | 5572 | editor->DecRef(); |
962a48f6 | 5573 | #endif |
189d0213 VZ |
5574 | } |
5575 | else | |
5576 | { | |
a9339fe2 | 5577 | // but all the rest is drawn by the cell renderer and hence may be customized |
0b190b0f VZ |
5578 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); |
5579 | renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); | |
5580 | renderer->DecRef(); | |
189d0213 | 5581 | } |
07296f0b | 5582 | |
283b7808 VZ |
5583 | attr->DecRef(); |
5584 | } | |
07296f0b | 5585 | |
283b7808 | 5586 | void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ) |
07296f0b | 5587 | { |
760be3f7 VS |
5588 | // don't show highlight when the grid doesn't have focus |
5589 | if ( !HasFocus() ) | |
5590 | return; | |
5591 | ||
07296f0b RD |
5592 | int row = m_currentCellCoords.GetRow(); |
5593 | int col = m_currentCellCoords.GetCol(); | |
5594 | ||
7c1cb261 | 5595 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
07296f0b RD |
5596 | return; |
5597 | ||
f6bcfd97 | 5598 | wxRect rect = CellToRect(row, col); |
07296f0b | 5599 | |
b3a7510d VZ |
5600 | // hmmm... what could we do here to show that the cell is disabled? |
5601 | // for now, I just draw a thinner border than for the other ones, but | |
5602 | // it doesn't look really good | |
b3a7510d | 5603 | |
d2520c85 RD |
5604 | int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth; |
5605 | ||
27f35b66 SN |
5606 | if (penWidth > 0) |
5607 | { | |
56b6cf26 DS |
5608 | // The center of the drawn line is where the position/width/height of |
5609 | // the rectangle is actually at (on wxMSW at least), so the | |
5610 | // size of the rectangle is reduced to compensate for the thickness of | |
5611 | // the line. If this is too strange on non-wxMSW platforms then | |
d2520c85 | 5612 | // please #ifdef this appropriately. |
4db6714b KH |
5613 | rect.x += penWidth / 2; |
5614 | rect.y += penWidth / 2; | |
5615 | rect.width -= penWidth - 1; | |
5616 | rect.height -= penWidth - 1; | |
d2520c85 | 5617 | |
d2520c85 | 5618 | // Now draw the rectangle |
73145b0e JS |
5619 | // use the cellHighlightColour if the cell is inside a selection, this |
5620 | // will ensure the cell is always visible. | |
ff72f628 VZ |
5621 | dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground |
5622 | : m_cellHighlightColour, | |
5623 | penWidth)); | |
d2520c85 RD |
5624 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
5625 | dc.DrawRectangle(rect); | |
5626 | } | |
2d66e025 | 5627 | } |
f85afd4e | 5628 | |
3d3f3e37 VZ |
5629 | wxPen wxGrid::GetDefaultGridLinePen() |
5630 | { | |
ff72f628 | 5631 | return wxPen(GetGridLineColour()); |
3d3f3e37 VZ |
5632 | } |
5633 | ||
5634 | wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row)) | |
5635 | { | |
5636 | return GetDefaultGridLinePen(); | |
5637 | } | |
5638 | ||
5639 | wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col)) | |
5640 | { | |
5641 | return GetDefaultGridLinePen(); | |
5642 | } | |
5643 | ||
2d66e025 MB |
5644 | void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) |
5645 | { | |
2d66e025 MB |
5646 | int row = coords.GetRow(); |
5647 | int col = coords.GetCol(); | |
7c1cb261 VZ |
5648 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
5649 | return; | |
5650 | ||
60ff3b99 | 5651 | |
27f35b66 SN |
5652 | wxRect rect = CellToRect( row, col ); |
5653 | ||
2d66e025 | 5654 | // right hand border |
3d3f3e37 | 5655 | dc.SetPen( GetColGridLinePen(col) ); |
27f35b66 SN |
5656 | dc.DrawLine( rect.x + rect.width, rect.y, |
5657 | rect.x + rect.width, rect.y + rect.height + 1 ); | |
2d66e025 MB |
5658 | |
5659 | // bottom border | |
3d3f3e37 | 5660 | dc.SetPen( GetRowGridLinePen(row) ); |
2f024384 | 5661 | dc.DrawLine( rect.x, rect.y + rect.height, |
27f35b66 | 5662 | rect.x + rect.width, rect.y + rect.height); |
f85afd4e MB |
5663 | } |
5664 | ||
2f024384 | 5665 | void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells) |
b3a7510d | 5666 | { |
2a7750d9 MB |
5667 | // This if block was previously in wxGrid::OnPaint but that doesn't |
5668 | // seem to get called under wxGTK - MB | |
5669 | // | |
2f024384 | 5670 | if ( m_currentCellCoords == wxGridNoCellCoords && |
2a7750d9 MB |
5671 | m_numRows && m_numCols ) |
5672 | { | |
5673 | m_currentCellCoords.Set(0, 0); | |
5674 | } | |
5675 | ||
f6bcfd97 | 5676 | if ( IsCellEditControlShown() ) |
99306db2 VZ |
5677 | { |
5678 | // don't show highlight when the edit control is shown | |
5679 | return; | |
5680 | } | |
5681 | ||
b3a7510d VZ |
5682 | // if the active cell was repainted, repaint its highlight too because it |
5683 | // might have been damaged by the grid lines | |
d10f4bf9 | 5684 | size_t count = cells.GetCount(); |
b3a7510d VZ |
5685 | for ( size_t n = 0; n < count; n++ ) |
5686 | { | |
54181a33 VZ |
5687 | wxGridCellCoords cell = cells[n]; |
5688 | ||
5689 | // If we are using attributes, then we may have just exposed another | |
5690 | // cell in a partially-visible merged cluster of cells. If the "anchor" | |
5691 | // (upper left) cell of this merged cluster is the cell indicated by | |
5692 | // m_currentCellCoords, then we need to refresh the cell highlight even | |
5693 | // though the "anchor" itself is not part of our update segment. | |
5694 | if ( CanHaveAttributes() ) | |
5695 | { | |
5696 | int rows = 0, | |
5697 | cols = 0; | |
5698 | GetCellSize(cell.GetRow(), cell.GetCol(), &rows, &cols); | |
5699 | ||
5700 | if ( rows < 0 ) | |
5701 | cell.SetRow(cell.GetRow() + rows); | |
5702 | ||
5703 | if ( cols < 0 ) | |
5704 | cell.SetCol(cell.GetCol() + cols); | |
5705 | } | |
5706 | ||
5707 | if ( cell == m_currentCellCoords ) | |
b3a7510d VZ |
5708 | { |
5709 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
5710 | DrawCellHighlight(dc, attr); | |
5711 | attr->DecRef(); | |
5712 | ||
5713 | break; | |
5714 | } | |
5715 | } | |
5716 | } | |
2d66e025 | 5717 | |
b55d57aa VZ |
5718 | // Used by wxGrid::Render() to draw the grid lines only for the cells in the |
5719 | // specified range. | |
5720 | void | |
5721 | wxGrid::DrawRangeGridLines(wxDC& dc, | |
5722 | const wxRegion& reg, | |
5723 | const wxGridCellCoords& topLeft, | |
5724 | const wxGridCellCoords& bottomRight) | |
5725 | { | |
5726 | if ( !m_gridLinesEnabled ) | |
5727 | return; | |
5728 | ||
5729 | int top, left, width, height; | |
5730 | reg.GetBox( left, top, width, height ); | |
5731 | ||
5732 | // create a clipping region | |
5733 | wxRegion clippedcells( dc.LogicalToDeviceX( left ), | |
5734 | dc.LogicalToDeviceY( top ), | |
5735 | dc.LogicalToDeviceXRel( width ), | |
5736 | dc.LogicalToDeviceYRel( height ) ); | |
5737 | ||
5738 | // subtract multi cell span area from clipping region for lines | |
5739 | wxRect rect; | |
5740 | for ( int row = topLeft.GetRow(); row <= bottomRight.GetRow(); row++ ) | |
5741 | { | |
5742 | for ( int col = topLeft.GetCol(); col <= bottomRight.GetCol(); col++ ) | |
5743 | { | |
5744 | int cell_rows, cell_cols; | |
5745 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
5746 | if ( cell_rows > 1 || cell_cols > 1 ) // multi cell | |
5747 | { | |
5748 | rect = CellToRect( row, col ); | |
5749 | // cater for scaling | |
5750 | // device origin already set in ::Render() for x, y | |
5751 | rect.x = dc.LogicalToDeviceX( rect.x ); | |
5752 | rect.y = dc.LogicalToDeviceY( rect.y ); | |
5753 | rect.width = dc.LogicalToDeviceXRel( rect.width ); | |
5754 | rect.height = dc.LogicalToDeviceYRel( rect.height ) - 1; | |
5755 | clippedcells.Subtract( rect ); | |
5756 | } | |
5757 | else if ( cell_rows < 0 || cell_cols < 0 ) // part of multicell | |
5758 | { | |
5759 | rect = CellToRect( row + cell_rows, col + cell_cols ); | |
5760 | rect.x = dc.LogicalToDeviceX( rect.x ); | |
5761 | rect.y = dc.LogicalToDeviceY( rect.y ); | |
5762 | rect.width = dc.LogicalToDeviceXRel( rect.width ); | |
5763 | rect.height = dc.LogicalToDeviceYRel( rect.height ) - 1; | |
5764 | clippedcells.Subtract( rect ); | |
5765 | } | |
5766 | } | |
5767 | } | |
5768 | ||
5769 | dc.SetDeviceClippingRegion( clippedcells ); | |
5770 | ||
5771 | DoDrawGridLines(dc, | |
5772 | top, left, top + height, left + width, | |
5773 | topLeft.GetRow(), topLeft.GetCol(), | |
5774 | bottomRight.GetRow(), bottomRight.GetCol()); | |
5775 | ||
5776 | dc.DestroyClippingRegion(); | |
5777 | } | |
5778 | ||
2d66e025 MB |
5779 | // This is used to redraw all grid lines e.g. when the grid line colour |
5780 | // has been changed | |
5781 | // | |
33ac7e6f | 5782 | void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) ) |
f85afd4e | 5783 | { |
9f7aee01 | 5784 | if ( !m_gridLinesEnabled ) |
4db6714b | 5785 | return; |
f85afd4e | 5786 | |
2d66e025 | 5787 | int top, bottom, left, right; |
796df70a | 5788 | |
ff72f628 VZ |
5789 | int cw, ch; |
5790 | m_gridWin->GetClientSize(&cw, &ch); | |
5791 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
5792 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
f85afd4e | 5793 | |
9496deb5 | 5794 | // avoid drawing grid lines past the last row and col |
9f7aee01 VZ |
5795 | if ( m_gridLinesClipHorz ) |
5796 | { | |
5797 | if ( !m_numCols ) | |
5798 | return; | |
5799 | ||
5800 | const int lastColRight = GetColRight(GetColAt(m_numCols - 1)); | |
5801 | if ( right > lastColRight ) | |
5802 | right = lastColRight; | |
5803 | } | |
5804 | ||
5805 | if ( m_gridLinesClipVert ) | |
5806 | { | |
5807 | if ( !m_numRows ) | |
5808 | return; | |
5809 | ||
5810 | const int lastRowBottom = GetRowBottom(m_numRows - 1); | |
5811 | if ( bottom > lastRowBottom ) | |
5812 | bottom = lastRowBottom; | |
5813 | } | |
9496deb5 | 5814 | |
27f35b66 | 5815 | // no gridlines inside multicells, clip them out |
d4175745 | 5816 | int leftCol = GetColPos( internalXToCol(left) ); |
a9339fe2 | 5817 | int topRow = internalYToRow(top); |
d4175745 | 5818 | int rightCol = GetColPos( internalXToCol(right) ); |
a967f048 | 5819 | int bottomRow = internalYToRow(bottom); |
27f35b66 | 5820 | |
c03bf0c7 | 5821 | wxRegion clippedcells(0, 0, cw, ch); |
27f35b66 | 5822 | |
ff72f628 | 5823 | int cell_rows, cell_cols; |
a967f048 | 5824 | wxRect rect; |
27f35b66 | 5825 | |
ff72f628 | 5826 | for ( int j = topRow; j <= bottomRow; j++ ) |
a967f048 | 5827 | { |
ff72f628 | 5828 | for ( int colPos = leftCol; colPos <= rightCol; colPos++ ) |
27f35b66 | 5829 | { |
ff72f628 | 5830 | int i = GetColAt( colPos ); |
d4175745 | 5831 | |
a967f048 RG |
5832 | GetCellSize( j, i, &cell_rows, &cell_cols ); |
5833 | if ((cell_rows > 1) || (cell_cols > 1)) | |
27f35b66 | 5834 | { |
a967f048 RG |
5835 | rect = CellToRect(j,i); |
5836 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
5837 | clippedcells.Subtract(rect); | |
5838 | } | |
5839 | else if ((cell_rows < 0) || (cell_cols < 0)) | |
5840 | { | |
a9339fe2 | 5841 | rect = CellToRect(j + cell_rows, i + cell_cols); |
a967f048 RG |
5842 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
5843 | clippedcells.Subtract(rect); | |
27f35b66 SN |
5844 | } |
5845 | } | |
5846 | } | |
a9339fe2 | 5847 | |
c1bc8d9f | 5848 | dc.SetDeviceClippingRegion( clippedcells ); |
27f35b66 | 5849 | |
f31d3faf VZ |
5850 | DoDrawGridLines(dc, |
5851 | top, left, bottom, right, | |
5852 | topRow, leftCol, m_numRows, m_numCols); | |
f85afd4e | 5853 | |
f31d3faf VZ |
5854 | dc.DestroyClippingRegion(); |
5855 | } | |
5856 | ||
5857 | void | |
5858 | wxGrid::DoDrawGridLines(wxDC& dc, | |
5859 | int top, int left, | |
5860 | int bottom, int right, | |
5861 | int topRow, int leftCol, | |
5862 | int bottomRow, int rightCol) | |
5863 | { | |
2d66e025 | 5864 | // horizontal grid lines |
f31d3faf | 5865 | for ( int i = topRow; i < bottomRow; i++ ) |
f85afd4e | 5866 | { |
6d55126d | 5867 | int bot = GetRowBottom(i) - 1; |
7c1cb261 | 5868 | |
6d55126d | 5869 | if ( bot > bottom ) |
2d66e025 | 5870 | break; |
7c1cb261 | 5871 | |
6d55126d | 5872 | if ( bot >= top ) |
2d66e025 | 5873 | { |
3d3f3e37 | 5874 | dc.SetPen( GetRowGridLinePen(i) ); |
6d55126d | 5875 | dc.DrawLine( left, bot, right, bot ); |
2d66e025 | 5876 | } |
f85afd4e MB |
5877 | } |
5878 | ||
2d66e025 | 5879 | // vertical grid lines |
f31d3faf | 5880 | for ( int colPos = leftCol; colPos < rightCol; colPos++ ) |
f85afd4e | 5881 | { |
ff72f628 | 5882 | int i = GetColAt( colPos ); |
d4175745 | 5883 | |
2121eb69 RR |
5884 | int colRight = GetColRight(i); |
5885 | #ifdef __WXGTK__ | |
5886 | if (GetLayoutDirection() != wxLayout_RightToLeft) | |
5887 | #endif | |
5888 | colRight--; | |
5889 | ||
7c1cb261 | 5890 | if ( colRight > right ) |
2d66e025 | 5891 | break; |
7c1cb261 VZ |
5892 | |
5893 | if ( colRight >= left ) | |
2d66e025 | 5894 | { |
3d3f3e37 | 5895 | dc.SetPen( GetColGridLinePen(i) ); |
7c1cb261 | 5896 | dc.DrawLine( colRight, top, colRight, bottom ); |
2d66e025 MB |
5897 | } |
5898 | } | |
5899 | } | |
f85afd4e | 5900 | |
c2f5b920 | 5901 | void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows) |
2d66e025 | 5902 | { |
4db6714b KH |
5903 | if ( !m_numRows ) |
5904 | return; | |
60ff3b99 | 5905 | |
ff72f628 VZ |
5906 | const size_t numLabels = rows.GetCount(); |
5907 | for ( size_t i = 0; i < numLabels; i++ ) | |
2d66e025 | 5908 | { |
d10f4bf9 | 5909 | DrawRowLabel( dc, rows[i] ); |
60ff3b99 | 5910 | } |
f85afd4e MB |
5911 | } |
5912 | ||
2d66e025 | 5913 | void wxGrid::DrawRowLabel( wxDC& dc, int row ) |
f85afd4e | 5914 | { |
659af826 | 5915 | if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 ) |
7c1cb261 | 5916 | return; |
60ff3b99 | 5917 | |
670d0177 VZ |
5918 | wxGridCellAttrProvider * const |
5919 | attrProvider = m_table ? m_table->GetAttrProvider() : NULL; | |
7a0a6cc8 VZ |
5920 | |
5921 | // notice that an explicit static_cast is needed to avoid a compilation | |
5922 | // error with VC7.1 which, for some reason, tries to instantiate (abstract) | |
5923 | // wxGridRowHeaderRenderer class without it | |
ba9574c3 | 5924 | const wxGridRowHeaderRenderer& |
670d0177 | 5925 | rend = attrProvider ? attrProvider->GetRowHeaderRenderer(row) |
7a0a6cc8 VZ |
5926 | : static_cast<const wxGridRowHeaderRenderer&> |
5927 | (gs_defaultHeaderRenderers.rowRenderer); | |
5928 | ||
ba9574c3 VZ |
5929 | wxRect rect(0, GetRowTop(row), m_rowLabelWidth, GetRowHeight(row)); |
5930 | rend.DrawBorder(*this, dc, rect); | |
8f177c8e | 5931 | |
f85afd4e | 5932 | int hAlign, vAlign; |
ba9574c3 | 5933 | GetRowLabelAlignment(&hAlign, &vAlign); |
60ff3b99 | 5934 | |
ba9574c3 VZ |
5935 | rend.DrawLabel(*this, dc, GetRowLabelValue(row), |
5936 | rect, hAlign, vAlign, wxHORIZONTAL); | |
f85afd4e MB |
5937 | } |
5938 | ||
ad805b9e VZ |
5939 | void wxGrid::UseNativeColHeader(bool native) |
5940 | { | |
5941 | if ( native == m_useNativeHeader ) | |
5942 | return; | |
5943 | ||
5944 | delete m_colWindow; | |
5945 | m_useNativeHeader = native; | |
5946 | ||
5947 | CreateColumnWindow(); | |
5948 | ||
5949 | if ( m_useNativeHeader ) | |
3039ade9 | 5950 | GetGridColHeader()->SetColumnCount(m_numCols); |
ad805b9e VZ |
5951 | CalcWindowSizes(); |
5952 | } | |
5953 | ||
71cf399f RR |
5954 | void wxGrid::SetUseNativeColLabels( bool native ) |
5955 | { | |
ad805b9e VZ |
5956 | wxASSERT_MSG( !m_useNativeHeader, |
5957 | "doesn't make sense when using native header" ); | |
5958 | ||
71cf399f RR |
5959 | m_nativeColumnLabels = native; |
5960 | if (native) | |
5961 | { | |
5962 | int height = wxRendererNative::Get().GetHeaderButtonHeight( this ); | |
5963 | SetColLabelSize( height ); | |
5964 | } | |
76c66f19 | 5965 | |
ad805b9e | 5966 | GetColLabelWindow()->Refresh(); |
ff72f628 | 5967 | m_cornerLabelWin->Refresh(); |
71cf399f RR |
5968 | } |
5969 | ||
d10f4bf9 | 5970 | void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols ) |
f85afd4e | 5971 | { |
4db6714b KH |
5972 | if ( !m_numCols ) |
5973 | return; | |
60ff3b99 | 5974 | |
ff72f628 VZ |
5975 | const size_t numLabels = cols.GetCount(); |
5976 | for ( size_t i = 0; i < numLabels; i++ ) | |
f85afd4e | 5977 | { |
d10f4bf9 | 5978 | DrawColLabel( dc, cols[i] ); |
60ff3b99 | 5979 | } |
f85afd4e MB |
5980 | } |
5981 | ||
ff72f628 VZ |
5982 | void wxGrid::DrawCornerLabel(wxDC& dc) |
5983 | { | |
ba9574c3 VZ |
5984 | wxRect rect(wxSize(m_rowLabelWidth, m_colLabelHeight)); |
5985 | ||
ff72f628 VZ |
5986 | if ( m_nativeColumnLabels ) |
5987 | { | |
ff72f628 VZ |
5988 | rect.Deflate(1); |
5989 | ||
5990 | wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin, dc, rect, 0); | |
5991 | } | |
5992 | else | |
5993 | { | |
ba9574c3 VZ |
5994 | rect.width++; |
5995 | rect.height++; | |
5996 | ||
670d0177 VZ |
5997 | wxGridCellAttrProvider * const |
5998 | attrProvider = m_table ? m_table->GetAttrProvider() : NULL; | |
ba9574c3 | 5999 | const wxGridCornerHeaderRenderer& |
670d0177 | 6000 | rend = attrProvider ? attrProvider->GetCornerRenderer() |
7a0a6cc8 VZ |
6001 | : static_cast<wxGridCornerHeaderRenderer&> |
6002 | (gs_defaultHeaderRenderers.cornerRenderer); | |
ff72f628 | 6003 | |
ba9574c3 | 6004 | rend.DrawBorder(*this, dc, rect); |
ff72f628 VZ |
6005 | } |
6006 | } | |
6007 | ||
6008 | void wxGrid::DrawColLabel(wxDC& dc, int col) | |
f85afd4e | 6009 | { |
659af826 | 6010 | if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 ) |
7c1cb261 | 6011 | return; |
60ff3b99 | 6012 | |
4d1bc39c | 6013 | int colLeft = GetColLeft(col); |
ec157c8f | 6014 | |
ff72f628 | 6015 | wxRect rect(colLeft, 0, GetColWidth(col), m_colLabelHeight); |
670d0177 VZ |
6016 | wxGridCellAttrProvider * const |
6017 | attrProvider = m_table ? m_table->GetAttrProvider() : NULL; | |
ba9574c3 | 6018 | const wxGridColumnHeaderRenderer& |
670d0177 | 6019 | rend = attrProvider ? attrProvider->GetColumnHeaderRenderer(col) |
7a0a6cc8 VZ |
6020 | : static_cast<wxGridColumnHeaderRenderer&> |
6021 | (gs_defaultHeaderRenderers.colRenderer); | |
2f024384 | 6022 | |
ff72f628 | 6023 | if ( m_nativeColumnLabels ) |
71cf399f | 6024 | { |
11393d29 VZ |
6025 | wxRendererNative::Get().DrawHeaderButton |
6026 | ( | |
6027 | GetColLabelWindow(), | |
6028 | dc, | |
6029 | rect, | |
6030 | 0, | |
6031 | IsSortingBy(col) | |
6032 | ? IsSortOrderAscending() | |
6033 | ? wxHDR_SORT_ICON_UP | |
6034 | : wxHDR_SORT_ICON_DOWN | |
6035 | : wxHDR_SORT_ICON_NONE | |
6036 | ); | |
ba9574c3 | 6037 | rect.Deflate(2); |
71cf399f RR |
6038 | } |
6039 | else | |
6040 | { | |
b5e97a17 VZ |
6041 | // It is reported that we need to erase the background to avoid display |
6042 | // artefacts, see #12055. | |
6043 | wxDCBrushChanger setBrush(dc, m_colWindow->GetBackgroundColour()); | |
6044 | dc.DrawRectangle(rect); | |
6045 | ||
ba9574c3 | 6046 | rend.DrawBorder(*this, dc, rect); |
71cf399f | 6047 | } |
2f024384 | 6048 | |
ff72f628 | 6049 | int hAlign, vAlign; |
ba9574c3 | 6050 | GetColLabelAlignment(&hAlign, &vAlign); |
ff72f628 | 6051 | const int orient = GetColLabelTextOrientation(); |
60ff3b99 | 6052 | |
ba9574c3 | 6053 | rend.DrawLabel(*this, dc, GetColLabelValue(col), rect, hAlign, vAlign, orient); |
f85afd4e MB |
6054 | } |
6055 | ||
ff72f628 VZ |
6056 | // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which |
6057 | // we just have to add textOrientation support | |
2d66e025 MB |
6058 | void wxGrid::DrawTextRectangle( wxDC& dc, |
6059 | const wxString& value, | |
6060 | const wxRect& rect, | |
6061 | int horizAlign, | |
d43851f7 | 6062 | int vertAlign, |
ba9574c3 | 6063 | int textOrientation ) const |
f85afd4e | 6064 | { |
2d66e025 | 6065 | wxArrayString lines; |
ef5df12b | 6066 | |
2d66e025 | 6067 | StringToLines( value, lines ); |
ef5df12b | 6068 | |
ff72f628 | 6069 | DrawTextRectangle(dc, lines, rect, horizAlign, vertAlign, textOrientation); |
d10f4bf9 VZ |
6070 | } |
6071 | ||
4330c974 | 6072 | void wxGrid::DrawTextRectangle(wxDC& dc, |
038a5591 JS |
6073 | const wxArrayString& lines, |
6074 | const wxRect& rect, | |
6075 | int horizAlign, | |
6076 | int vertAlign, | |
ba9574c3 | 6077 | int textOrientation) const |
d10f4bf9 | 6078 | { |
4330c974 VZ |
6079 | if ( lines.empty() ) |
6080 | return; | |
ef5df12b | 6081 | |
4330c974 | 6082 | wxDCClipper clip(dc, rect); |
ef5df12b | 6083 | |
4330c974 VZ |
6084 | long textWidth, |
6085 | textHeight; | |
ef5df12b | 6086 | |
4330c974 VZ |
6087 | if ( textOrientation == wxHORIZONTAL ) |
6088 | GetTextBoxSize( dc, lines, &textWidth, &textHeight ); | |
6089 | else | |
6090 | GetTextBoxSize( dc, lines, &textHeight, &textWidth ); | |
ef5df12b | 6091 | |
4330c974 VZ |
6092 | int x = 0, |
6093 | y = 0; | |
6094 | switch ( vertAlign ) | |
6095 | { | |
73145b0e | 6096 | case wxALIGN_BOTTOM: |
4db6714b | 6097 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 | 6098 | y = rect.y + (rect.height - textHeight - 1); |
d43851f7 | 6099 | else |
038a5591 JS |
6100 | x = rect.x + rect.width - textWidth; |
6101 | break; | |
ef5df12b | 6102 | |
73145b0e | 6103 | case wxALIGN_CENTRE: |
4db6714b | 6104 | if ( textOrientation == wxHORIZONTAL ) |
a9339fe2 | 6105 | y = rect.y + ((rect.height - textHeight) / 2); |
d43851f7 | 6106 | else |
a9339fe2 | 6107 | x = rect.x + ((rect.width - textWidth) / 2); |
038a5591 | 6108 | break; |
ef5df12b | 6109 | |
73145b0e JS |
6110 | case wxALIGN_TOP: |
6111 | default: | |
4db6714b | 6112 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 | 6113 | y = rect.y + 1; |
d43851f7 | 6114 | else |
038a5591 | 6115 | x = rect.x + 1; |
73145b0e | 6116 | break; |
4330c974 | 6117 | } |
ef5df12b | 6118 | |
4330c974 VZ |
6119 | // Align each line of a multi-line label |
6120 | size_t nLines = lines.GetCount(); | |
6121 | for ( size_t l = 0; l < nLines; l++ ) | |
6122 | { | |
6123 | const wxString& line = lines[l]; | |
ef5df12b | 6124 | |
9b7d3c09 VZ |
6125 | if ( line.empty() ) |
6126 | { | |
6127 | *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight(); | |
6128 | continue; | |
6129 | } | |
6130 | ||
ad2633bd | 6131 | wxCoord lineWidth = 0, |
c2b2c10e | 6132 | lineHeight = 0; |
4330c974 VZ |
6133 | dc.GetTextExtent(line, &lineWidth, &lineHeight); |
6134 | ||
6135 | switch ( horizAlign ) | |
6136 | { | |
038a5591 | 6137 | case wxALIGN_RIGHT: |
4db6714b | 6138 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 JS |
6139 | x = rect.x + (rect.width - lineWidth - 1); |
6140 | else | |
6141 | y = rect.y + lineWidth + 1; | |
6142 | break; | |
ef5df12b | 6143 | |
038a5591 | 6144 | case wxALIGN_CENTRE: |
4db6714b | 6145 | if ( textOrientation == wxHORIZONTAL ) |
2f024384 | 6146 | x = rect.x + ((rect.width - lineWidth) / 2); |
038a5591 | 6147 | else |
2f024384 | 6148 | y = rect.y + rect.height - ((rect.height - lineWidth) / 2); |
038a5591 | 6149 | break; |
ef5df12b | 6150 | |
038a5591 JS |
6151 | case wxALIGN_LEFT: |
6152 | default: | |
4db6714b | 6153 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 JS |
6154 | x = rect.x + 1; |
6155 | else | |
6156 | y = rect.y + rect.height - 1; | |
6157 | break; | |
4330c974 | 6158 | } |
ef5df12b | 6159 | |
4330c974 VZ |
6160 | if ( textOrientation == wxHORIZONTAL ) |
6161 | { | |
6162 | dc.DrawText( line, x, y ); | |
6163 | y += lineHeight; | |
6164 | } | |
6165 | else | |
6166 | { | |
6167 | dc.DrawRotatedText( line, x, y, 90.0 ); | |
6168 | x += lineHeight; | |
038a5591 | 6169 | } |
f85afd4e | 6170 | } |
f85afd4e MB |
6171 | } |
6172 | ||
2f024384 DS |
6173 | // Split multi-line text up into an array of strings. |
6174 | // Any existing contents of the string array are preserved. | |
f85afd4e | 6175 | // |
696f3e9d | 6176 | // TODO: refactor wxTextFile::Read() and reuse the same code from here |
ef316e23 | 6177 | void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) const |
f85afd4e | 6178 | { |
f85afd4e MB |
6179 | int startPos = 0; |
6180 | int pos; | |
6d004f67 | 6181 | wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix ); |
2433bb2e | 6182 | wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix ); |
e2b42eeb | 6183 | |
faa94f3e | 6184 | while ( startPos < (int)tVal.length() ) |
f85afd4e | 6185 | { |
2433bb2e | 6186 | pos = tVal.Mid(startPos).Find( eol ); |
f85afd4e MB |
6187 | if ( pos < 0 ) |
6188 | { | |
6189 | break; | |
6190 | } | |
6191 | else if ( pos == 0 ) | |
6192 | { | |
6193 | lines.Add( wxEmptyString ); | |
6194 | } | |
6195 | else | |
6196 | { | |
696f3e9d | 6197 | lines.Add( tVal.Mid(startPos, pos) ); |
f85afd4e | 6198 | } |
a9339fe2 | 6199 | |
4db6714b | 6200 | startPos += pos + 1; |
f85afd4e | 6201 | } |
4db6714b | 6202 | |
2eafd712 | 6203 | if ( startPos < (int)tVal.length() ) |
f85afd4e | 6204 | { |
696f3e9d | 6205 | lines.Add( tVal.Mid( startPos ) ); |
f85afd4e MB |
6206 | } |
6207 | } | |
6208 | ||
fbfb8bcc | 6209 | void wxGrid::GetTextBoxSize( const wxDC& dc, |
d10f4bf9 | 6210 | const wxArrayString& lines, |
ef316e23 | 6211 | long *width, long *height ) const |
f85afd4e | 6212 | { |
ad2633bd RR |
6213 | wxCoord w = 0; |
6214 | wxCoord h = 0; | |
6215 | wxCoord lineW = 0, lineH = 0; | |
f85afd4e | 6216 | |
b540eb2b | 6217 | size_t i; |
56b6cf26 | 6218 | for ( i = 0; i < lines.GetCount(); i++ ) |
f85afd4e MB |
6219 | { |
6220 | dc.GetTextExtent( lines[i], &lineW, &lineH ); | |
6221 | w = wxMax( w, lineW ); | |
6222 | h += lineH; | |
6223 | } | |
6224 | ||
6225 | *width = w; | |
6226 | *height = h; | |
6227 | } | |
6228 | ||
f6bcfd97 BP |
6229 | // |
6230 | // ------ Batch processing. | |
6231 | // | |
6232 | void wxGrid::EndBatch() | |
6233 | { | |
6234 | if ( m_batchCount > 0 ) | |
6235 | { | |
6236 | m_batchCount--; | |
6237 | if ( !m_batchCount ) | |
6238 | { | |
6239 | CalcDimensions(); | |
6240 | m_rowLabelWin->Refresh(); | |
ad805b9e | 6241 | m_colWindow->Refresh(); |
f6bcfd97 BP |
6242 | m_cornerLabelWin->Refresh(); |
6243 | m_gridWin->Refresh(); | |
6244 | } | |
6245 | } | |
6246 | } | |
f85afd4e | 6247 | |
d8232393 MB |
6248 | // Use this, rather than wxWindow::Refresh(), to force an immediate |
6249 | // repainting of the grid. Has no effect if you are already inside a | |
6250 | // BeginBatch / EndBatch block. | |
6251 | // | |
6252 | void wxGrid::ForceRefresh() | |
6253 | { | |
6254 | BeginBatch(); | |
6255 | EndBatch(); | |
6256 | } | |
6257 | ||
bf646121 VZ |
6258 | bool wxGrid::Enable(bool enable) |
6259 | { | |
6260 | if ( !wxScrolledWindow::Enable(enable) ) | |
6261 | return false; | |
6262 | ||
6263 | // redraw in the new state | |
6264 | m_gridWin->Refresh(); | |
6265 | ||
6266 | return true; | |
6267 | } | |
d8232393 | 6268 | |
f85afd4e | 6269 | // |
2d66e025 | 6270 | // ------ Edit control functions |
f85afd4e MB |
6271 | // |
6272 | ||
2d66e025 | 6273 | void wxGrid::EnableEditing( bool edit ) |
f85afd4e | 6274 | { |
2d66e025 | 6275 | if ( edit != m_editable ) |
f85afd4e | 6276 | { |
4db6714b KH |
6277 | if (!edit) |
6278 | EnableCellEditControl(edit); | |
2d66e025 | 6279 | m_editable = edit; |
f85afd4e | 6280 | } |
f85afd4e MB |
6281 | } |
6282 | ||
2d66e025 | 6283 | void wxGrid::EnableCellEditControl( bool enable ) |
f85afd4e | 6284 | { |
2c9a89e0 RD |
6285 | if (! m_editable) |
6286 | return; | |
6287 | ||
2c9a89e0 RD |
6288 | if ( enable != m_cellEditCtrlEnabled ) |
6289 | { | |
dcfe4c3d | 6290 | if ( enable ) |
f85afd4e | 6291 | { |
8a3e536c | 6292 | if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN) == -1 ) |
97a9929e | 6293 | return; |
fe7b9ed6 | 6294 | |
97a9929e | 6295 | // this should be checked by the caller! |
9a83f860 | 6296 | wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") ); |
b54ba671 VZ |
6297 | |
6298 | // do it before ShowCellEditControl() | |
dcfe4c3d | 6299 | m_cellEditCtrlEnabled = enable; |
b54ba671 | 6300 | |
2d66e025 | 6301 | ShowCellEditControl(); |
2d66e025 MB |
6302 | } |
6303 | else | |
6304 | { | |
8a3e536c | 6305 | SendEvent(wxEVT_GRID_EDITOR_HIDDEN); |
fe7b9ed6 | 6306 | |
97a9929e | 6307 | HideCellEditControl(); |
2d66e025 | 6308 | SaveEditControlValue(); |
b54ba671 VZ |
6309 | |
6310 | // do it after HideCellEditControl() | |
dcfe4c3d | 6311 | m_cellEditCtrlEnabled = enable; |
f85afd4e | 6312 | } |
f85afd4e | 6313 | } |
f85afd4e | 6314 | } |
f85afd4e | 6315 | |
b54ba671 | 6316 | bool wxGrid::IsCurrentCellReadOnly() const |
283b7808 | 6317 | { |
f48a1159 VZ |
6318 | wxGridCellAttr* |
6319 | attr = const_cast<wxGrid *>(this)->GetCellAttr(m_currentCellCoords); | |
b54ba671 VZ |
6320 | bool readonly = attr->IsReadOnly(); |
6321 | attr->DecRef(); | |
283b7808 | 6322 | |
b54ba671 VZ |
6323 | return readonly; |
6324 | } | |
283b7808 | 6325 | |
b54ba671 VZ |
6326 | bool wxGrid::CanEnableCellControl() const |
6327 | { | |
20c84410 SN |
6328 | return m_editable && (m_currentCellCoords != wxGridNoCellCoords) && |
6329 | !IsCurrentCellReadOnly(); | |
b54ba671 VZ |
6330 | } |
6331 | ||
6332 | bool wxGrid::IsCellEditControlEnabled() const | |
6333 | { | |
6334 | // the cell edit control might be disable for all cells or just for the | |
6335 | // current one if it's read only | |
ca65c044 | 6336 | return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false; |
283b7808 VZ |
6337 | } |
6338 | ||
f6bcfd97 BP |
6339 | bool wxGrid::IsCellEditControlShown() const |
6340 | { | |
ca65c044 | 6341 | bool isShown = false; |
f6bcfd97 BP |
6342 | |
6343 | if ( m_cellEditCtrlEnabled ) | |
6344 | { | |
6345 | int row = m_currentCellCoords.GetRow(); | |
6346 | int col = m_currentCellCoords.GetCol(); | |
6347 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
6348 | wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col); | |
6349 | attr->DecRef(); | |
6350 | ||
6351 | if ( editor ) | |
6352 | { | |
6353 | if ( editor->IsCreated() ) | |
6354 | { | |
6355 | isShown = editor->GetControl()->IsShown(); | |
6356 | } | |
6357 | ||
6358 | editor->DecRef(); | |
6359 | } | |
6360 | } | |
6361 | ||
6362 | return isShown; | |
6363 | } | |
6364 | ||
2d66e025 | 6365 | void wxGrid::ShowCellEditControl() |
f85afd4e | 6366 | { |
2d66e025 | 6367 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 6368 | { |
7db713ae | 6369 | if ( !IsVisible( m_currentCellCoords, false ) ) |
2d66e025 | 6370 | { |
ca65c044 | 6371 | m_cellEditCtrlEnabled = false; |
2d66e025 MB |
6372 | return; |
6373 | } | |
6374 | else | |
6375 | { | |
2c9a89e0 RD |
6376 | wxRect rect = CellToRect( m_currentCellCoords ); |
6377 | int row = m_currentCellCoords.GetRow(); | |
6378 | int col = m_currentCellCoords.GetCol(); | |
2d66e025 | 6379 | |
27f35b66 SN |
6380 | // if this is part of a multicell, find owner (topleft) |
6381 | int cell_rows, cell_cols; | |
6382 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
6383 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
6384 | { | |
6385 | row += cell_rows; | |
6386 | col += cell_cols; | |
6387 | m_currentCellCoords.SetRow( row ); | |
6388 | m_currentCellCoords.SetCol( col ); | |
6389 | } | |
6390 | ||
99306db2 VZ |
6391 | // erase the highlight and the cell contents because the editor |
6392 | // might not cover the entire cell | |
6393 | wxClientDC dc( m_gridWin ); | |
6394 | PrepareDC( dc ); | |
2c03d771 | 6395 | wxGridCellAttr* attr = GetCellAttr(row, col); |
ff72f628 | 6396 | dc.SetBrush(wxBrush(attr->GetBackgroundColour())); |
99306db2 VZ |
6397 | dc.SetPen(*wxTRANSPARENT_PEN); |
6398 | dc.DrawRectangle(rect); | |
d2fdd8d2 | 6399 | |
6f706ee0 VZ |
6400 | // convert to scrolled coords |
6401 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
6402 | ||
6403 | int nXMove = 0; | |
6404 | if (rect.x < 0) | |
6405 | nXMove = rect.x; | |
6406 | ||
99306db2 | 6407 | // cell is shifted by one pixel |
68c5a31c | 6408 | // However, don't allow x or y to become negative |
70e8d961 | 6409 | // since the SetSize() method interprets that as |
68c5a31c SN |
6410 | // "don't change." |
6411 | if (rect.x > 0) | |
6412 | rect.x--; | |
6413 | if (rect.y > 0) | |
6414 | rect.y--; | |
f0102d2a | 6415 | |
28a77bc4 | 6416 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
3da93aae VZ |
6417 | if ( !editor->IsCreated() ) |
6418 | { | |
ca65c044 | 6419 | editor->Create(m_gridWin, wxID_ANY, |
2c9a89e0 | 6420 | new wxGridCellEditorEvtHandler(this, editor)); |
bf7945ce RD |
6421 | |
6422 | wxGridEditorCreatedEvent evt(GetId(), | |
6423 | wxEVT_GRID_EDITOR_CREATED, | |
6424 | this, | |
6425 | row, | |
6426 | col, | |
6427 | editor->GetControl()); | |
6428 | GetEventHandler()->ProcessEvent(evt); | |
2d66e025 MB |
6429 | } |
6430 | ||
27f35b66 | 6431 | // resize editor to overflow into righthand cells if allowed |
39b80349 | 6432 | int maxWidth = rect.width; |
27f35b66 SN |
6433 | wxString value = GetCellValue(row, col); |
6434 | if ( (value != wxEmptyString) && (attr->GetOverflow()) ) | |
6435 | { | |
39b80349 | 6436 | int y; |
2f024384 DS |
6437 | GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont()); |
6438 | if (maxWidth < rect.width) | |
6439 | maxWidth = rect.width; | |
39b80349 | 6440 | } |
4db6714b | 6441 | |
39b80349 | 6442 | int client_right = m_gridWin->GetClientSize().GetWidth(); |
2f024384 | 6443 | if (rect.x + maxWidth > client_right) |
2b5f62a0 | 6444 | maxWidth = client_right - rect.x; |
39b80349 | 6445 | |
2b5f62a0 VZ |
6446 | if ((maxWidth > rect.width) && (col < m_numCols) && m_table) |
6447 | { | |
39b80349 | 6448 | GetCellSize( row, col, &cell_rows, &cell_cols ); |
2b5f62a0 | 6449 | // may have changed earlier |
2f024384 | 6450 | for (int i = col + cell_cols; i < m_numCols; i++) |
2b5f62a0 | 6451 | { |
39b80349 SN |
6452 | int c_rows, c_cols; |
6453 | GetCellSize( row, i, &c_rows, &c_cols ); | |
2f024384 | 6454 | |
2b5f62a0 | 6455 | // looks weird going over a multicell |
bee19958 | 6456 | if (m_table->IsEmptyCell( row, i ) && |
2b5f62a0 | 6457 | (rect.width < maxWidth) && (c_rows == 1)) |
2f024384 | 6458 | { |
bee19958 | 6459 | rect.width += GetColWidth( i ); |
2f024384 | 6460 | } |
2b5f62a0 VZ |
6461 | else |
6462 | break; | |
6463 | } | |
4db6714b | 6464 | |
39b80349 | 6465 | if (rect.GetRight() > client_right) |
bee19958 | 6466 | rect.SetRight( client_right - 1 ); |
27f35b66 | 6467 | } |
76c66f19 | 6468 | |
bee19958 | 6469 | editor->SetCellAttr( attr ); |
2c9a89e0 | 6470 | editor->SetSize( rect ); |
e1a66d9a RD |
6471 | if (nXMove != 0) |
6472 | editor->GetControl()->Move( | |
6473 | editor->GetControl()->GetPosition().x + nXMove, | |
6474 | editor->GetControl()->GetPosition().y ); | |
ca65c044 | 6475 | editor->Show( true, attr ); |
04418332 VZ |
6476 | |
6477 | // recalc dimensions in case we need to | |
6478 | // expand the scrolled window to account for editor | |
73145b0e | 6479 | CalcDimensions(); |
99306db2 | 6480 | |
3da93aae | 6481 | editor->BeginEdit(row, col, this); |
1bd71df9 | 6482 | editor->SetCellAttr(NULL); |
0b190b0f VZ |
6483 | |
6484 | editor->DecRef(); | |
2c9a89e0 | 6485 | attr->DecRef(); |
2b5f62a0 | 6486 | } |
f85afd4e MB |
6487 | } |
6488 | } | |
6489 | ||
2d66e025 | 6490 | void wxGrid::HideCellEditControl() |
f85afd4e | 6491 | { |
2d66e025 | 6492 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 6493 | { |
2c9a89e0 RD |
6494 | int row = m_currentCellCoords.GetRow(); |
6495 | int col = m_currentCellCoords.GetCol(); | |
6496 | ||
bee19958 | 6497 | wxGridCellAttr *attr = GetCellAttr(row, col); |
0b190b0f | 6498 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
7d277ac0 | 6499 | const bool editorHadFocus = editor->GetControl()->HasFocus(); |
ca65c044 | 6500 | editor->Show( false ); |
0b190b0f | 6501 | editor->DecRef(); |
2c9a89e0 | 6502 | attr->DecRef(); |
2fb3e528 | 6503 | |
7d277ac0 VZ |
6504 | // return the focus to the grid itself if the editor had it |
6505 | // | |
6506 | // note that we must not do this unconditionally to avoid stealing | |
6507 | // focus from the window which just received it if we are hiding the | |
6508 | // editor precisely because we lost focus | |
6509 | if ( editorHadFocus ) | |
6510 | m_gridWin->SetFocus(); | |
2fb3e528 | 6511 | |
27f35b66 SN |
6512 | // refresh whole row to the right |
6513 | wxRect rect( CellToRect(row, col) ); | |
6514 | CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y ); | |
6515 | rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x; | |
2f024384 | 6516 | |
7d75e6c6 RD |
6517 | #ifdef __WXMAC__ |
6518 | // ensure that the pixels under the focus ring get refreshed as well | |
4db6714b | 6519 | rect.Inflate(10, 10); |
7d75e6c6 | 6520 | #endif |
2f024384 | 6521 | |
ca65c044 | 6522 | m_gridWin->Refresh( false, &rect ); |
f85afd4e | 6523 | } |
2d66e025 | 6524 | } |
8f177c8e | 6525 | |
2d66e025 MB |
6526 | void wxGrid::SaveEditControlValue() |
6527 | { | |
3da93aae VZ |
6528 | if ( IsCellEditControlEnabled() ) |
6529 | { | |
2c9a89e0 RD |
6530 | int row = m_currentCellCoords.GetRow(); |
6531 | int col = m_currentCellCoords.GetCol(); | |
8f177c8e | 6532 | |
4db6714b | 6533 | wxString oldval = GetCellValue(row, col); |
fe7b9ed6 | 6534 | |
3da93aae | 6535 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 6536 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
2d66e025 | 6537 | |
763163a8 | 6538 | wxString newval; |
78e78812 | 6539 | bool changed = editor->EndEdit(row, col, this, oldval, &newval); |
2d66e025 | 6540 | |
763163a8 | 6541 | if ( changed && SendEvent(wxEVT_GRID_CELL_CHANGING, newval) != -1 ) |
3da93aae | 6542 | { |
763163a8 VZ |
6543 | editor->ApplyEdit(row, col, this); |
6544 | ||
6545 | // for compatibility reasons dating back to wx 2.8 when this event | |
6546 | // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING | |
6547 | // didn't exist we allow vetoing this one too | |
6548 | if ( SendEvent(wxEVT_GRID_CELL_CHANGED, oldval) == -1 ) | |
4db6714b | 6549 | { |
97a9929e | 6550 | // Event has been vetoed, set the data back. |
4db6714b | 6551 | SetCellValue(row, col, oldval); |
97a9929e | 6552 | } |
2d66e025 | 6553 | } |
763163a8 VZ |
6554 | |
6555 | editor->DecRef(); | |
6556 | attr->DecRef(); | |
f85afd4e MB |
6557 | } |
6558 | } | |
6559 | ||
add6e919 VZ |
6560 | void wxGrid::OnHideEditor(wxCommandEvent& WXUNUSED(event)) |
6561 | { | |
6562 | DisableCellEditControl(); | |
6563 | } | |
6564 | ||
2d66e025 | 6565 | // |
60ff3b99 VZ |
6566 | // ------ Grid location functions |
6567 | // Note that all of these functions work with the logical coordinates of | |
2d66e025 MB |
6568 | // grid cells and labels so you will need to convert from device |
6569 | // coordinates for mouse events etc. | |
6570 | // | |
6571 | ||
8a3e536c | 6572 | wxGridCellCoords wxGrid::XYToCell(int x, int y) const |
f85afd4e | 6573 | { |
58dd5b3b MB |
6574 | int row = YToRow(y); |
6575 | int col = XToCol(x); | |
6576 | ||
8a3e536c VZ |
6577 | return row == -1 || col == -1 ? wxGridNoCellCoords |
6578 | : wxGridCellCoords(row, col); | |
2d66e025 | 6579 | } |
f85afd4e | 6580 | |
bec70262 VZ |
6581 | // compute row or column from some (unscrolled) coordinate value, using either |
6582 | // m_defaultRowHeight/m_defaultColWidth or binary search on array of | |
5bce3d57 VZ |
6583 | // m_rowBottoms/m_colRights to do it quickly in O(log n) time. |
6584 | // NOTE: This may not work correctly for reordered columns. | |
cd4f6f5f VZ |
6585 | int wxGrid::PosToLinePos(int coord, |
6586 | bool clipToMinMax, | |
6587 | const wxGridOperations& oper) const | |
2d66e025 | 6588 | { |
bec70262 | 6589 | const int numLines = oper.GetNumberOfLines(this); |
a967f048 | 6590 | |
bec70262 | 6591 | if ( coord < 0 ) |
cd4f6f5f | 6592 | return clipToMinMax && numLines > 0 ? 0 : wxNOT_FOUND; |
a967f048 | 6593 | |
bec70262 VZ |
6594 | const int defaultLineSize = oper.GetDefaultLineSize(this); |
6595 | wxCHECK_MSG( defaultLineSize, -1, "can't have 0 default line size" ); | |
d57ad377 | 6596 | |
bec70262 VZ |
6597 | int maxPos = coord / defaultLineSize, |
6598 | minPos = 0; | |
8f177c8e | 6599 | |
bec70262 VZ |
6600 | // check for the simplest case: if we have no explicit line sizes |
6601 | // configured, then we already know the line this position falls in | |
6602 | const wxArrayInt& lineEnds = oper.GetLineEnds(this); | |
6603 | if ( lineEnds.empty() ) | |
f85afd4e | 6604 | { |
bec70262 VZ |
6605 | if ( maxPos < numLines ) |
6606 | return maxPos; | |
4db6714b | 6607 | |
bec70262 | 6608 | return clipToMinMax ? numLines - 1 : -1; |
f85afd4e | 6609 | } |
4db6714b | 6610 | |
2d66e025 | 6611 | |
5bce3d57 VZ |
6612 | // binary search is quite efficient and we can't really make any assumptions |
6613 | // on where to start here since row and columns could be of size 0 if they are | |
6614 | // hidden. While this could be made more efficient, some profiling will be | |
6615 | // necessary to determine if it really is a performance bottleneck | |
6616 | maxPos = numLines - 1; | |
d4175745 | 6617 | |
bec70262 VZ |
6618 | // check if the position is beyond the last column |
6619 | const int lineAtMaxPos = oper.GetLineAt(this, maxPos); | |
6620 | if ( coord >= lineEnds[lineAtMaxPos] ) | |
cd4f6f5f | 6621 | return clipToMinMax ? maxPos : -1; |
d4175745 | 6622 | |
bec70262 VZ |
6623 | // or before the first one |
6624 | const int lineAt0 = oper.GetLineAt(this, 0); | |
6625 | if ( coord < lineEnds[lineAt0] ) | |
cd4f6f5f | 6626 | return 0; |
d4175745 | 6627 | |
bec70262 VZ |
6628 | |
6629 | // finally do perform the binary search | |
6630 | while ( minPos < maxPos ) | |
d4175745 | 6631 | { |
bec70262 VZ |
6632 | wxCHECK_MSG( lineEnds[oper.GetLineAt(this, minPos)] <= coord && |
6633 | coord < lineEnds[oper.GetLineAt(this, maxPos)], | |
6634 | -1, | |
cd4f6f5f | 6635 | "wxGrid: internal error in PosToLinePos()" ); |
d4175745 | 6636 | |
bec70262 | 6637 | if ( coord >= lineEnds[oper.GetLineAt(this, maxPos - 1)] ) |
cd4f6f5f | 6638 | return maxPos; |
d4175745 VZ |
6639 | else |
6640 | maxPos--; | |
bec70262 VZ |
6641 | |
6642 | const int median = minPos + (maxPos - minPos + 1) / 2; | |
6643 | if ( coord < lineEnds[oper.GetLineAt(this, median)] ) | |
d4175745 VZ |
6644 | maxPos = median; |
6645 | else | |
6646 | minPos = median; | |
6647 | } | |
bec70262 | 6648 | |
cd4f6f5f VZ |
6649 | return maxPos; |
6650 | } | |
6651 | ||
6652 | int | |
6653 | wxGrid::PosToLine(int coord, | |
6654 | bool clipToMinMax, | |
6655 | const wxGridOperations& oper) const | |
6656 | { | |
6657 | int pos = PosToLinePos(coord, clipToMinMax, oper); | |
6658 | ||
6659 | return pos == wxNOT_FOUND ? wxNOT_FOUND : oper.GetLineAt(this, pos); | |
bec70262 VZ |
6660 | } |
6661 | ||
6662 | int wxGrid::YToRow(int y, bool clipToMinMax) const | |
6663 | { | |
6664 | return PosToLine(y, clipToMinMax, wxGridRowOperations()); | |
6665 | } | |
6666 | ||
6667 | int wxGrid::XToCol(int x, bool clipToMinMax) const | |
6668 | { | |
6669 | return PosToLine(x, clipToMinMax, wxGridColumnOperations()); | |
2d66e025 | 6670 | } |
8f177c8e | 6671 | |
cd4f6f5f VZ |
6672 | int wxGrid::XToPos(int x) const |
6673 | { | |
6674 | return PosToLinePos(x, true /* clip */, wxGridColumnOperations()); | |
6675 | } | |
6676 | ||
71b619d7 | 6677 | // return the row/col number such that the pos is near the edge of, or -1 if |
10a4531d VZ |
6678 | // not near an edge. |
6679 | // | |
82edfbe7 VZ |
6680 | // notice that position can only possibly be near an edge if the row/column is |
6681 | // large enough to still allow for an "inner" area that is _not_ near the edge | |
6682 | // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will | |
6683 | // _never_ be considered to be near the edge). | |
10a4531d | 6684 | int wxGrid::PosToEdgeOfLine(int pos, const wxGridOperations& oper) const |
2d66e025 | 6685 | { |
71b619d7 VZ |
6686 | // Get the bottom or rightmost line that could match. |
6687 | int line = oper.PosToLine(this, pos, true); | |
10a4531d | 6688 | |
6df84c5c VZ |
6689 | if ( oper.GetLineSize(this, line) > WXGRID_LABEL_EDGE_ZONE ) |
6690 | { | |
6691 | // We know that we are in this line, test whether we are close enough | |
6692 | // to start or end border, respectively. | |
6693 | if ( abs(oper.GetLineEndPos(this, line) - pos) < WXGRID_LABEL_EDGE_ZONE ) | |
6694 | return line; | |
6695 | else if ( line > 0 && | |
6696 | pos - oper.GetLineStartPos(this, | |
6697 | line) < WXGRID_LABEL_EDGE_ZONE ) | |
6698 | { | |
6699 | // We need to find the previous visible line, so skip all the | |
6700 | // hidden (of size 0) ones. | |
6701 | do | |
6702 | { | |
6703 | line = oper.GetLineBefore(this, line); | |
6704 | } | |
6705 | while ( line >= 0 && oper.GetLineSize(this, line) == 0 ); | |
2d66e025 | 6706 | |
6df84c5c VZ |
6707 | // It can possibly be -1 here. |
6708 | return line; | |
6709 | } | |
6710 | } | |
71b619d7 | 6711 | |
2d66e025 MB |
6712 | return -1; |
6713 | } | |
6714 | ||
10a4531d | 6715 | int wxGrid::YToEdgeOfRow(int y) const |
2d66e025 | 6716 | { |
10a4531d VZ |
6717 | return PosToEdgeOfLine(y, wxGridRowOperations()); |
6718 | } | |
2d66e025 | 6719 | |
10a4531d VZ |
6720 | int wxGrid::XToEdgeOfCol(int x) const |
6721 | { | |
6722 | return PosToEdgeOfLine(x, wxGridColumnOperations()); | |
f85afd4e MB |
6723 | } |
6724 | ||
ef316e23 | 6725 | wxRect wxGrid::CellToRect( int row, int col ) const |
f85afd4e | 6726 | { |
2d66e025 | 6727 | wxRect rect( -1, -1, -1, -1 ); |
f85afd4e | 6728 | |
2f024384 DS |
6729 | if ( row >= 0 && row < m_numRows && |
6730 | col >= 0 && col < m_numCols ) | |
f85afd4e | 6731 | { |
27f35b66 SN |
6732 | int i, cell_rows, cell_cols; |
6733 | rect.width = rect.height = 0; | |
6734 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
6735 | // if negative then find multicell owner | |
2f024384 DS |
6736 | if (cell_rows < 0) |
6737 | row += cell_rows; | |
6738 | if (cell_cols < 0) | |
6739 | col += cell_cols; | |
27f35b66 SN |
6740 | GetCellSize( row, col, &cell_rows, &cell_cols ); |
6741 | ||
7c1cb261 VZ |
6742 | rect.x = GetColLeft(col); |
6743 | rect.y = GetRowTop(row); | |
2f024384 DS |
6744 | for (i=col; i < col + cell_cols; i++) |
6745 | rect.width += GetColWidth(i); | |
6746 | for (i=row; i < row + cell_rows; i++) | |
27f35b66 | 6747 | rect.height += GetRowHeight(i); |
f85afd4e | 6748 | |
efc49a21 VZ |
6749 | // if grid lines are enabled, then the area of the cell is a bit smaller |
6750 | if (m_gridLinesEnabled) | |
6751 | { | |
6752 | rect.width -= 1; | |
6753 | rect.height -= 1; | |
6754 | } | |
f6bcfd97 | 6755 | } |
4db6714b | 6756 | |
2d66e025 MB |
6757 | return rect; |
6758 | } | |
6759 | ||
ef316e23 | 6760 | bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) const |
2d66e025 MB |
6761 | { |
6762 | // get the cell rectangle in logical coords | |
6763 | // | |
6764 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 6765 | |
2d66e025 MB |
6766 | // convert to device coords |
6767 | // | |
6768 | int left, top, right, bottom; | |
6769 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
6770 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 6771 | |
2d66e025 | 6772 | // check against the client area of the grid window |
2d66e025 MB |
6773 | int cw, ch; |
6774 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 6775 | |
2d66e025 | 6776 | if ( wholeCellVisible ) |
f85afd4e | 6777 | { |
2d66e025 | 6778 | // is the cell wholly visible ? |
2f024384 DS |
6779 | return ( left >= 0 && right <= cw && |
6780 | top >= 0 && bottom <= ch ); | |
2d66e025 MB |
6781 | } |
6782 | else | |
6783 | { | |
6784 | // is the cell partly visible ? | |
6785 | // | |
2f024384 DS |
6786 | return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) && |
6787 | ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) ); | |
2d66e025 MB |
6788 | } |
6789 | } | |
6790 | ||
2d66e025 MB |
6791 | // make the specified cell location visible by doing a minimal amount |
6792 | // of scrolling | |
6793 | // | |
6794 | void wxGrid::MakeCellVisible( int row, int col ) | |
6795 | { | |
6796 | int i; | |
60ff3b99 | 6797 | int xpos = -1, ypos = -1; |
2d66e025 | 6798 | |
2f024384 DS |
6799 | if ( row >= 0 && row < m_numRows && |
6800 | col >= 0 && col < m_numCols ) | |
2d66e025 MB |
6801 | { |
6802 | // get the cell rectangle in logical coords | |
2d66e025 | 6803 | wxRect r( CellToRect( row, col ) ); |
60ff3b99 | 6804 | |
2d66e025 | 6805 | // convert to device coords |
2d66e025 MB |
6806 | int left, top, right, bottom; |
6807 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
6808 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 6809 | |
2d66e025 MB |
6810 | int cw, ch; |
6811 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 6812 | |
2d66e025 | 6813 | if ( top < 0 ) |
3f296516 | 6814 | { |
2d66e025 | 6815 | ypos = r.GetTop(); |
3f296516 | 6816 | } |
2d66e025 MB |
6817 | else if ( bottom > ch ) |
6818 | { | |
6819 | int h = r.GetHeight(); | |
6820 | ypos = r.GetTop(); | |
56b6cf26 | 6821 | for ( i = row - 1; i >= 0; i-- ) |
2d66e025 | 6822 | { |
7c1cb261 VZ |
6823 | int rowHeight = GetRowHeight(i); |
6824 | if ( h + rowHeight > ch ) | |
6825 | break; | |
2d66e025 | 6826 | |
7c1cb261 VZ |
6827 | h += rowHeight; |
6828 | ypos -= rowHeight; | |
2d66e025 | 6829 | } |
f0102d2a VZ |
6830 | |
6831 | // we divide it later by GRID_SCROLL_LINE, make sure that we don't | |
56b6cf26 DS |
6832 | // have rounding errors (this is important, because if we do, |
6833 | // we might not scroll at all and some cells won't be redrawn) | |
275c4ae4 | 6834 | // |
56b6cf26 DS |
6835 | // Sometimes GRID_SCROLL_LINE / 2 is not enough, |
6836 | // so just add a full scroll unit... | |
fd76c6a7 | 6837 | ypos += m_yScrollPixelsPerLine; |
2d66e025 MB |
6838 | } |
6839 | ||
7db713ae | 6840 | // special handling for wide cells - show always left part of the cell! |
faa94f3e | 6841 | // Otherwise, e.g. when stepping from row to row, it would jump between |
7db713ae JS |
6842 | // left and right part of the cell on every step! |
6843 | // if ( left < 0 ) | |
ccdee36f | 6844 | if ( left < 0 || (right - left) >= cw ) |
2d66e025 MB |
6845 | { |
6846 | xpos = r.GetLeft(); | |
6847 | } | |
6848 | else if ( right > cw ) | |
6849 | { | |
73145b0e JS |
6850 | // position the view so that the cell is on the right |
6851 | int x0, y0; | |
6852 | CalcUnscrolledPosition(0, 0, &x0, &y0); | |
6853 | xpos = x0 + (right - cw); | |
f0102d2a VZ |
6854 | |
6855 | // see comment for ypos above | |
fd76c6a7 | 6856 | xpos += m_xScrollPixelsPerLine; |
2d66e025 MB |
6857 | } |
6858 | ||
ccdee36f | 6859 | if ( xpos != -1 || ypos != -1 ) |
2d66e025 | 6860 | { |
97a9929e | 6861 | if ( xpos != -1 ) |
fd76c6a7 | 6862 | xpos /= m_xScrollPixelsPerLine; |
97a9929e | 6863 | if ( ypos != -1 ) |
fd76c6a7 | 6864 | ypos /= m_yScrollPixelsPerLine; |
2d66e025 MB |
6865 | Scroll( xpos, ypos ); |
6866 | AdjustScrollbars(); | |
6867 | } | |
6868 | } | |
6869 | } | |
6870 | ||
2d66e025 MB |
6871 | // |
6872 | // ------ Grid cursor movement functions | |
6873 | // | |
6874 | ||
10a4531d VZ |
6875 | bool |
6876 | wxGrid::DoMoveCursor(bool expandSelection, | |
6877 | const wxGridDirectionOperations& diroper) | |
2d66e025 | 6878 | { |
10a4531d VZ |
6879 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
6880 | return false; | |
6881 | ||
6882 | if ( expandSelection ) | |
2d66e025 | 6883 | { |
8a3e536c VZ |
6884 | wxGridCellCoords coords = m_selectedBlockCorner; |
6885 | if ( coords == wxGridNoCellCoords ) | |
6886 | coords = m_currentCellCoords; | |
4db6714b | 6887 | |
8a3e536c | 6888 | if ( diroper.IsAtBoundary(coords) ) |
10a4531d | 6889 | return false; |
2d66e025 | 6890 | |
8a3e536c | 6891 | diroper.Advance(coords); |
2d66e025 | 6892 | |
8a3e536c | 6893 | UpdateBlockBeingSelected(m_currentCellCoords, coords); |
10a4531d | 6894 | } |
8a3e536c | 6895 | else // don't expand selection |
f85afd4e | 6896 | { |
8a3e536c VZ |
6897 | ClearSelection(); |
6898 | ||
10a4531d | 6899 | if ( diroper.IsAtBoundary(m_currentCellCoords) ) |
ca65c044 | 6900 | return false; |
4db6714b | 6901 | |
10a4531d VZ |
6902 | wxGridCellCoords coords = m_currentCellCoords; |
6903 | diroper.Advance(coords); | |
8a3e536c VZ |
6904 | |
6905 | GoToCell(coords); | |
f85afd4e | 6906 | } |
2d66e025 | 6907 | |
10a4531d | 6908 | return true; |
f85afd4e MB |
6909 | } |
6910 | ||
10a4531d | 6911 | bool wxGrid::MoveCursorUp(bool expandSelection) |
f85afd4e | 6912 | { |
10a4531d VZ |
6913 | return DoMoveCursor(expandSelection, |
6914 | wxGridBackwardOperations(this, wxGridRowOperations())); | |
2d66e025 MB |
6915 | } |
6916 | ||
10a4531d | 6917 | bool wxGrid::MoveCursorDown(bool expandSelection) |
2d66e025 | 6918 | { |
10a4531d VZ |
6919 | return DoMoveCursor(expandSelection, |
6920 | wxGridForwardOperations(this, wxGridRowOperations())); | |
6921 | } | |
4db6714b | 6922 | |
10a4531d VZ |
6923 | bool wxGrid::MoveCursorLeft(bool expandSelection) |
6924 | { | |
6925 | return DoMoveCursor(expandSelection, | |
6926 | wxGridBackwardOperations(this, wxGridColumnOperations())); | |
6927 | } | |
8f177c8e | 6928 | |
10a4531d VZ |
6929 | bool wxGrid::MoveCursorRight(bool expandSelection) |
6930 | { | |
6931 | return DoMoveCursor(expandSelection, | |
6932 | wxGridForwardOperations(this, wxGridColumnOperations())); | |
2d66e025 MB |
6933 | } |
6934 | ||
10a4531d | 6935 | bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations& diroper) |
2d66e025 | 6936 | { |
4db6714b KH |
6937 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
6938 | return false; | |
60ff3b99 | 6939 | |
10a4531d VZ |
6940 | if ( diroper.IsAtBoundary(m_currentCellCoords) ) |
6941 | return false; | |
ef5df12b | 6942 | |
10a4531d VZ |
6943 | const int oldRow = m_currentCellCoords.GetRow(); |
6944 | int newRow = diroper.MoveByPixelDistance(oldRow, m_gridWin->GetClientSize().y); | |
6945 | if ( newRow == oldRow ) | |
6946 | { | |
6947 | wxGridCellCoords coords(m_currentCellCoords); | |
6948 | diroper.Advance(coords); | |
6949 | newRow = coords.GetRow(); | |
6950 | } | |
8f177c8e | 6951 | |
8a3e536c | 6952 | GoToCell(newRow, m_currentCellCoords.GetCol()); |
60ff3b99 | 6953 | |
10a4531d VZ |
6954 | return true; |
6955 | } | |
2d66e025 | 6956 | |
10a4531d VZ |
6957 | bool wxGrid::MovePageUp() |
6958 | { | |
6959 | return DoMoveCursorByPage( | |
6960 | wxGridBackwardOperations(this, wxGridRowOperations())); | |
2d66e025 MB |
6961 | } |
6962 | ||
6963 | bool wxGrid::MovePageDown() | |
6964 | { | |
10a4531d | 6965 | return DoMoveCursorByPage( |
d188378e | 6966 | wxGridForwardOperations(this, wxGridRowOperations())); |
10a4531d | 6967 | } |
60ff3b99 | 6968 | |
10a4531d VZ |
6969 | // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper |
6970 | // until we find a non-empty cell or reach the grid end | |
6971 | void | |
6972 | wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords& coords, | |
6973 | const wxGridDirectionOperations& diroper) | |
6974 | { | |
6975 | while ( !diroper.IsAtBoundary(coords) ) | |
f85afd4e | 6976 | { |
10a4531d VZ |
6977 | diroper.Advance(coords); |
6978 | if ( !m_table->IsEmpty(coords) ) | |
6979 | break; | |
f85afd4e MB |
6980 | } |
6981 | } | |
8f177c8e | 6982 | |
10a4531d VZ |
6983 | bool |
6984 | wxGrid::DoMoveCursorByBlock(bool expandSelection, | |
6985 | const wxGridDirectionOperations& diroper) | |
2d66e025 | 6986 | { |
10a4531d VZ |
6987 | if ( !m_table || m_currentCellCoords == wxGridNoCellCoords ) |
6988 | return false; | |
f85afd4e | 6989 | |
10a4531d VZ |
6990 | if ( diroper.IsAtBoundary(m_currentCellCoords) ) |
6991 | return false; | |
4db6714b | 6992 | |
10a4531d VZ |
6993 | wxGridCellCoords coords(m_currentCellCoords); |
6994 | if ( m_table->IsEmpty(coords) ) | |
6995 | { | |
6996 | // we are in an empty cell: find the next block of non-empty cells | |
6997 | AdvanceToNextNonEmpty(coords, diroper); | |
2d66e025 | 6998 | } |
10a4531d | 6999 | else // current cell is not empty |
f85afd4e | 7000 | { |
10a4531d VZ |
7001 | diroper.Advance(coords); |
7002 | if ( m_table->IsEmpty(coords) ) | |
2d66e025 | 7003 | { |
10a4531d VZ |
7004 | // we started at the end of a block, find the next one |
7005 | AdvanceToNextNonEmpty(coords, diroper); | |
2d66e025 | 7006 | } |
10a4531d | 7007 | else // we're in a middle of a block |
2d66e025 | 7008 | { |
10a4531d VZ |
7009 | // go to the end of it, i.e. find the last cell before the next |
7010 | // empty one | |
7011 | while ( !diroper.IsAtBoundary(coords) ) | |
2d66e025 | 7012 | { |
10a4531d VZ |
7013 | wxGridCellCoords coordsNext(coords); |
7014 | diroper.Advance(coordsNext); | |
7015 | if ( m_table->IsEmpty(coordsNext) ) | |
2d66e025 | 7016 | break; |
2d66e025 | 7017 | |
10a4531d VZ |
7018 | coords = coordsNext; |
7019 | } | |
aa5e1f75 | 7020 | } |
10a4531d | 7021 | } |
2d66e025 | 7022 | |
10a4531d VZ |
7023 | if ( expandSelection ) |
7024 | { | |
8a3e536c | 7025 | UpdateBlockBeingSelected(m_currentCellCoords, coords); |
10a4531d VZ |
7026 | } |
7027 | else | |
7028 | { | |
7029 | ClearSelection(); | |
8a3e536c | 7030 | GoToCell(coords); |
f85afd4e | 7031 | } |
f85afd4e | 7032 | |
10a4531d | 7033 | return true; |
2d66e025 | 7034 | } |
f85afd4e | 7035 | |
10a4531d | 7036 | bool wxGrid::MoveCursorUpBlock(bool expandSelection) |
f85afd4e | 7037 | { |
10a4531d VZ |
7038 | return DoMoveCursorByBlock( |
7039 | expandSelection, | |
7040 | wxGridBackwardOperations(this, wxGridRowOperations()) | |
7041 | ); | |
7042 | } | |
8f177c8e | 7043 | |
10a4531d VZ |
7044 | bool wxGrid::MoveCursorDownBlock( bool expandSelection ) |
7045 | { | |
7046 | return DoMoveCursorByBlock( | |
7047 | expandSelection, | |
7048 | wxGridForwardOperations(this, wxGridRowOperations()) | |
7049 | ); | |
7050 | } | |
2d66e025 | 7051 | |
10a4531d VZ |
7052 | bool wxGrid::MoveCursorLeftBlock( bool expandSelection ) |
7053 | { | |
7054 | return DoMoveCursorByBlock( | |
7055 | expandSelection, | |
7056 | wxGridBackwardOperations(this, wxGridColumnOperations()) | |
7057 | ); | |
f85afd4e MB |
7058 | } |
7059 | ||
5c8fc7c1 | 7060 | bool wxGrid::MoveCursorRightBlock( bool expandSelection ) |
f85afd4e | 7061 | { |
10a4531d VZ |
7062 | return DoMoveCursorByBlock( |
7063 | expandSelection, | |
7064 | wxGridForwardOperations(this, wxGridColumnOperations()) | |
7065 | ); | |
f85afd4e MB |
7066 | } |
7067 | ||
f85afd4e | 7068 | // |
2d66e025 | 7069 | // ------ Label values and formatting |
f85afd4e MB |
7070 | // |
7071 | ||
ef316e23 | 7072 | void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) const |
f85afd4e | 7073 | { |
2f024384 DS |
7074 | if ( horiz ) |
7075 | *horiz = m_rowLabelHorizAlign; | |
7076 | if ( vert ) | |
7077 | *vert = m_rowLabelVertAlign; | |
f85afd4e MB |
7078 | } |
7079 | ||
ef316e23 | 7080 | void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) const |
f85afd4e | 7081 | { |
2f024384 DS |
7082 | if ( horiz ) |
7083 | *horiz = m_colLabelHorizAlign; | |
7084 | if ( vert ) | |
7085 | *vert = m_colLabelVertAlign; | |
f85afd4e MB |
7086 | } |
7087 | ||
ef316e23 | 7088 | int wxGrid::GetColLabelTextOrientation() const |
d43851f7 JS |
7089 | { |
7090 | return m_colLabelTextOrientation; | |
7091 | } | |
7092 | ||
ef316e23 | 7093 | wxString wxGrid::GetRowLabelValue( int row ) const |
f85afd4e MB |
7094 | { |
7095 | if ( m_table ) | |
7096 | { | |
7097 | return m_table->GetRowLabelValue( row ); | |
7098 | } | |
7099 | else | |
7100 | { | |
7101 | wxString s; | |
7102 | s << row; | |
7103 | return s; | |
7104 | } | |
7105 | } | |
7106 | ||
ef316e23 | 7107 | wxString wxGrid::GetColLabelValue( int col ) const |
f85afd4e MB |
7108 | { |
7109 | if ( m_table ) | |
7110 | { | |
7111 | return m_table->GetColLabelValue( col ); | |
7112 | } | |
7113 | else | |
7114 | { | |
7115 | wxString s; | |
7116 | s << col; | |
7117 | return s; | |
7118 | } | |
7119 | } | |
7120 | ||
7121 | void wxGrid::SetRowLabelSize( int width ) | |
7122 | { | |
733f486a VZ |
7123 | wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE ); |
7124 | ||
7125 | if ( width == wxGRID_AUTOSIZE ) | |
7126 | { | |
7127 | width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW); | |
7128 | } | |
7129 | ||
2e8cd977 MB |
7130 | if ( width != m_rowLabelWidth ) |
7131 | { | |
2e8cd977 MB |
7132 | if ( width == 0 ) |
7133 | { | |
ca65c044 WS |
7134 | m_rowLabelWin->Show( false ); |
7135 | m_cornerLabelWin->Show( false ); | |
2e8cd977 | 7136 | } |
7807d81c | 7137 | else if ( m_rowLabelWidth == 0 ) |
2e8cd977 | 7138 | { |
ca65c044 | 7139 | m_rowLabelWin->Show( true ); |
2f024384 DS |
7140 | if ( m_colLabelHeight > 0 ) |
7141 | m_cornerLabelWin->Show( true ); | |
2e8cd977 | 7142 | } |
b99be8fb | 7143 | |
2e8cd977 | 7144 | m_rowLabelWidth = width; |
a6b40a9a | 7145 | InvalidateBestSize(); |
7807d81c | 7146 | CalcWindowSizes(); |
ca65c044 | 7147 | wxScrolledWindow::Refresh( true ); |
2e8cd977 | 7148 | } |
f85afd4e MB |
7149 | } |
7150 | ||
7151 | void wxGrid::SetColLabelSize( int height ) | |
7152 | { | |
733f486a VZ |
7153 | wxASSERT( height >=0 || height == wxGRID_AUTOSIZE ); |
7154 | ||
7155 | if ( height == wxGRID_AUTOSIZE ) | |
7156 | { | |
7157 | height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN); | |
7158 | } | |
7159 | ||
2e8cd977 MB |
7160 | if ( height != m_colLabelHeight ) |
7161 | { | |
2e8cd977 MB |
7162 | if ( height == 0 ) |
7163 | { | |
ad805b9e | 7164 | m_colWindow->Show( false ); |
ca65c044 | 7165 | m_cornerLabelWin->Show( false ); |
2e8cd977 | 7166 | } |
7807d81c | 7167 | else if ( m_colLabelHeight == 0 ) |
2e8cd977 | 7168 | { |
ad805b9e | 7169 | m_colWindow->Show( true ); |
a9339fe2 DS |
7170 | if ( m_rowLabelWidth > 0 ) |
7171 | m_cornerLabelWin->Show( true ); | |
2e8cd977 | 7172 | } |
7807d81c | 7173 | |
2e8cd977 | 7174 | m_colLabelHeight = height; |
a6b40a9a | 7175 | InvalidateBestSize(); |
7807d81c | 7176 | CalcWindowSizes(); |
ca65c044 | 7177 | wxScrolledWindow::Refresh( true ); |
2e8cd977 | 7178 | } |
f85afd4e MB |
7179 | } |
7180 | ||
7181 | void wxGrid::SetLabelBackgroundColour( const wxColour& colour ) | |
7182 | { | |
2d66e025 MB |
7183 | if ( m_labelBackgroundColour != colour ) |
7184 | { | |
7185 | m_labelBackgroundColour = colour; | |
7186 | m_rowLabelWin->SetBackgroundColour( colour ); | |
ad805b9e | 7187 | m_colWindow->SetBackgroundColour( colour ); |
2d66e025 MB |
7188 | m_cornerLabelWin->SetBackgroundColour( colour ); |
7189 | ||
7190 | if ( !GetBatchCount() ) | |
7191 | { | |
7192 | m_rowLabelWin->Refresh(); | |
ad805b9e | 7193 | m_colWindow->Refresh(); |
2d66e025 MB |
7194 | m_cornerLabelWin->Refresh(); |
7195 | } | |
7196 | } | |
f85afd4e MB |
7197 | } |
7198 | ||
7199 | void wxGrid::SetLabelTextColour( const wxColour& colour ) | |
7200 | { | |
2d66e025 MB |
7201 | if ( m_labelTextColour != colour ) |
7202 | { | |
7203 | m_labelTextColour = colour; | |
7204 | if ( !GetBatchCount() ) | |
7205 | { | |
7206 | m_rowLabelWin->Refresh(); | |
ad805b9e | 7207 | m_colWindow->Refresh(); |
2d66e025 MB |
7208 | } |
7209 | } | |
f85afd4e MB |
7210 | } |
7211 | ||
7212 | void wxGrid::SetLabelFont( const wxFont& font ) | |
7213 | { | |
7214 | m_labelFont = font; | |
2d66e025 MB |
7215 | if ( !GetBatchCount() ) |
7216 | { | |
7217 | m_rowLabelWin->Refresh(); | |
ad805b9e | 7218 | m_colWindow->Refresh(); |
2d66e025 | 7219 | } |
f85afd4e MB |
7220 | } |
7221 | ||
7222 | void wxGrid::SetRowLabelAlignment( int horiz, int vert ) | |
7223 | { | |
4c7277db MB |
7224 | // allow old (incorrect) defs to be used |
7225 | switch ( horiz ) | |
7226 | { | |
7227 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
7228 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
7229 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
7230 | } | |
84912ef8 | 7231 | |
4c7277db MB |
7232 | switch ( vert ) |
7233 | { | |
7234 | case wxTOP: vert = wxALIGN_TOP; break; | |
7235 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
7236 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
7237 | } | |
84912ef8 | 7238 | |
4c7277db | 7239 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) |
f85afd4e MB |
7240 | { |
7241 | m_rowLabelHorizAlign = horiz; | |
7242 | } | |
8f177c8e | 7243 | |
4c7277db | 7244 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) |
f85afd4e MB |
7245 | { |
7246 | m_rowLabelVertAlign = vert; | |
7247 | } | |
7248 | ||
2d66e025 MB |
7249 | if ( !GetBatchCount() ) |
7250 | { | |
7251 | m_rowLabelWin->Refresh(); | |
60ff3b99 | 7252 | } |
f85afd4e MB |
7253 | } |
7254 | ||
7255 | void wxGrid::SetColLabelAlignment( int horiz, int vert ) | |
7256 | { | |
4c7277db MB |
7257 | // allow old (incorrect) defs to be used |
7258 | switch ( horiz ) | |
7259 | { | |
7260 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
7261 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
7262 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
7263 | } | |
84912ef8 | 7264 | |
4c7277db MB |
7265 | switch ( vert ) |
7266 | { | |
7267 | case wxTOP: vert = wxALIGN_TOP; break; | |
7268 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
7269 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
7270 | } | |
84912ef8 | 7271 | |
4c7277db | 7272 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) |
f85afd4e MB |
7273 | { |
7274 | m_colLabelHorizAlign = horiz; | |
7275 | } | |
8f177c8e | 7276 | |
4c7277db | 7277 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) |
f85afd4e MB |
7278 | { |
7279 | m_colLabelVertAlign = vert; | |
7280 | } | |
7281 | ||
2d66e025 MB |
7282 | if ( !GetBatchCount() ) |
7283 | { | |
ad805b9e | 7284 | m_colWindow->Refresh(); |
60ff3b99 | 7285 | } |
f85afd4e MB |
7286 | } |
7287 | ||
ca65c044 WS |
7288 | // Note: under MSW, the default column label font must be changed because it |
7289 | // does not support vertical printing | |
d43851f7 JS |
7290 | // |
7291 | // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD); | |
ca65c044 WS |
7292 | // pGrid->SetLabelFont(font); |
7293 | // pGrid->SetColLabelTextOrientation(wxVERTICAL); | |
d43851f7 JS |
7294 | // |
7295 | void wxGrid::SetColLabelTextOrientation( int textOrientation ) | |
7296 | { | |
4db6714b | 7297 | if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL ) |
d43851f7 | 7298 | m_colLabelTextOrientation = textOrientation; |
d43851f7 JS |
7299 | |
7300 | if ( !GetBatchCount() ) | |
ad805b9e | 7301 | m_colWindow->Refresh(); |
d43851f7 JS |
7302 | } |
7303 | ||
f85afd4e MB |
7304 | void wxGrid::SetRowLabelValue( int row, const wxString& s ) |
7305 | { | |
7306 | if ( m_table ) | |
7307 | { | |
7308 | m_table->SetRowLabelValue( row, s ); | |
2d66e025 MB |
7309 | if ( !GetBatchCount() ) |
7310 | { | |
ccdee36f | 7311 | wxRect rect = CellToRect( row, 0 ); |
70c7a608 SN |
7312 | if ( rect.height > 0 ) |
7313 | { | |
cb309039 | 7314 | CalcScrolledPosition(0, rect.y, &rect.x, &rect.y); |
b1944ebc | 7315 | rect.x = 0; |
70c7a608 | 7316 | rect.width = m_rowLabelWidth; |
ca65c044 | 7317 | m_rowLabelWin->Refresh( true, &rect ); |
70c7a608 | 7318 | } |
2d66e025 | 7319 | } |
f85afd4e MB |
7320 | } |
7321 | } | |
7322 | ||
7323 | void wxGrid::SetColLabelValue( int col, const wxString& s ) | |
7324 | { | |
7325 | if ( m_table ) | |
7326 | { | |
7327 | m_table->SetColLabelValue( col, s ); | |
2d66e025 MB |
7328 | if ( !GetBatchCount() ) |
7329 | { | |
ad805b9e | 7330 | if ( m_useNativeHeader ) |
70c7a608 | 7331 | { |
3039ade9 | 7332 | GetGridColHeader()->UpdateColumn(col); |
ad805b9e VZ |
7333 | } |
7334 | else | |
7335 | { | |
7336 | wxRect rect = CellToRect( 0, col ); | |
7337 | if ( rect.width > 0 ) | |
7338 | { | |
7339 | CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y); | |
7340 | rect.y = 0; | |
7341 | rect.height = m_colLabelHeight; | |
7342 | GetColLabelWindow()->Refresh( true, &rect ); | |
7343 | } | |
70c7a608 | 7344 | } |
2d66e025 | 7345 | } |
f85afd4e MB |
7346 | } |
7347 | } | |
7348 | ||
7349 | void wxGrid::SetGridLineColour( const wxColour& colour ) | |
7350 | { | |
2d66e025 MB |
7351 | if ( m_gridLineColour != colour ) |
7352 | { | |
7353 | m_gridLineColour = colour; | |
60ff3b99 | 7354 | |
9f7aee01 VZ |
7355 | if ( GridLinesEnabled() ) |
7356 | RedrawGridLines(); | |
2d66e025 | 7357 | } |
f85afd4e MB |
7358 | } |
7359 | ||
f6bcfd97 BP |
7360 | void wxGrid::SetCellHighlightColour( const wxColour& colour ) |
7361 | { | |
7362 | if ( m_cellHighlightColour != colour ) | |
7363 | { | |
7364 | m_cellHighlightColour = colour; | |
7365 | ||
7366 | wxClientDC dc( m_gridWin ); | |
7367 | PrepareDC( dc ); | |
7368 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
7369 | DrawCellHighlight(dc, attr); | |
7370 | attr->DecRef(); | |
7371 | } | |
7372 | } | |
7373 | ||
d2520c85 RD |
7374 | void wxGrid::SetCellHighlightPenWidth(int width) |
7375 | { | |
4db6714b KH |
7376 | if (m_cellHighlightPenWidth != width) |
7377 | { | |
d2520c85 RD |
7378 | m_cellHighlightPenWidth = width; |
7379 | ||
7380 | // Just redrawing the cell highlight is not enough since that won't | |
4c51a665 | 7381 | // make any visible change if the thickness is getting smaller. |
d2520c85 RD |
7382 | int row = m_currentCellCoords.GetRow(); |
7383 | int col = m_currentCellCoords.GetCol(); | |
1b3b96d8 | 7384 | if ( row == -1 || col == -1 || GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
d2520c85 | 7385 | return; |
2f024384 | 7386 | |
d2520c85 | 7387 | wxRect rect = CellToRect(row, col); |
ca65c044 | 7388 | m_gridWin->Refresh(true, &rect); |
d2520c85 RD |
7389 | } |
7390 | } | |
7391 | ||
7392 | void wxGrid::SetCellHighlightROPenWidth(int width) | |
7393 | { | |
4db6714b KH |
7394 | if (m_cellHighlightROPenWidth != width) |
7395 | { | |
d2520c85 RD |
7396 | m_cellHighlightROPenWidth = width; |
7397 | ||
7398 | // Just redrawing the cell highlight is not enough since that won't | |
4c51a665 | 7399 | // make any visible change if the thickness is getting smaller. |
d2520c85 RD |
7400 | int row = m_currentCellCoords.GetRow(); |
7401 | int col = m_currentCellCoords.GetCol(); | |
76c66f19 VZ |
7402 | if ( row == -1 || col == -1 || |
7403 | GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
d2520c85 | 7404 | return; |
ccdee36f | 7405 | |
d2520c85 | 7406 | wxRect rect = CellToRect(row, col); |
ca65c044 | 7407 | m_gridWin->Refresh(true, &rect); |
d2520c85 RD |
7408 | } |
7409 | } | |
7410 | ||
9f7aee01 VZ |
7411 | void wxGrid::RedrawGridLines() |
7412 | { | |
7413 | // the lines will be redrawn when the window is thawn | |
7414 | if ( GetBatchCount() ) | |
7415 | return; | |
7416 | ||
7417 | if ( GridLinesEnabled() ) | |
7418 | { | |
7419 | wxClientDC dc( m_gridWin ); | |
7420 | PrepareDC( dc ); | |
7421 | DrawAllGridLines( dc, wxRegion() ); | |
7422 | } | |
7423 | else // remove the grid lines | |
7424 | { | |
7425 | m_gridWin->Refresh(); | |
7426 | } | |
7427 | } | |
7428 | ||
f85afd4e MB |
7429 | void wxGrid::EnableGridLines( bool enable ) |
7430 | { | |
7431 | if ( enable != m_gridLinesEnabled ) | |
7432 | { | |
7433 | m_gridLinesEnabled = enable; | |
2d66e025 | 7434 | |
9f7aee01 VZ |
7435 | RedrawGridLines(); |
7436 | } | |
7437 | } | |
7438 | ||
7439 | void wxGrid::DoClipGridLines(bool& var, bool clip) | |
7440 | { | |
7441 | if ( clip != var ) | |
7442 | { | |
7443 | var = clip; | |
7444 | ||
7445 | if ( GridLinesEnabled() ) | |
7446 | RedrawGridLines(); | |
f85afd4e MB |
7447 | } |
7448 | } | |
7449 | ||
ef316e23 | 7450 | int wxGrid::GetDefaultRowSize() const |
f85afd4e MB |
7451 | { |
7452 | return m_defaultRowHeight; | |
7453 | } | |
7454 | ||
ef316e23 | 7455 | int wxGrid::GetRowSize( int row ) const |
f85afd4e | 7456 | { |
9a83f860 | 7457 | wxCHECK_MSG( row >= 0 && row < m_numRows, 0, wxT("invalid row index") ); |
b99be8fb | 7458 | |
7c1cb261 | 7459 | return GetRowHeight(row); |
f85afd4e MB |
7460 | } |
7461 | ||
ef316e23 | 7462 | int wxGrid::GetDefaultColSize() const |
f85afd4e MB |
7463 | { |
7464 | return m_defaultColWidth; | |
7465 | } | |
7466 | ||
ef316e23 | 7467 | int wxGrid::GetColSize( int col ) const |
f85afd4e | 7468 | { |
9a83f860 | 7469 | wxCHECK_MSG( col >= 0 && col < m_numCols, 0, wxT("invalid column index") ); |
b99be8fb | 7470 | |
7c1cb261 | 7471 | return GetColWidth(col); |
f85afd4e MB |
7472 | } |
7473 | ||
2e9a6788 VZ |
7474 | // ============================================================================ |
7475 | // access to the grid attributes: each of them has a default value in the grid | |
7476 | // itself and may be overidden on a per-cell basis | |
7477 | // ============================================================================ | |
7478 | ||
7479 | // ---------------------------------------------------------------------------- | |
7480 | // setting default attributes | |
7481 | // ---------------------------------------------------------------------------- | |
7482 | ||
7483 | void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col ) | |
7484 | { | |
2796cce3 | 7485 | m_defaultCellAttr->SetBackgroundColour(col); |
c916e13b RR |
7486 | #ifdef __WXGTK__ |
7487 | m_gridWin->SetBackgroundColour(col); | |
7488 | #endif | |
2e9a6788 VZ |
7489 | } |
7490 | ||
7491 | void wxGrid::SetDefaultCellTextColour( const wxColour& col ) | |
7492 | { | |
2796cce3 | 7493 | m_defaultCellAttr->SetTextColour(col); |
2e9a6788 VZ |
7494 | } |
7495 | ||
7496 | void wxGrid::SetDefaultCellAlignment( int horiz, int vert ) | |
7497 | { | |
2796cce3 | 7498 | m_defaultCellAttr->SetAlignment(horiz, vert); |
2e9a6788 VZ |
7499 | } |
7500 | ||
27f35b66 SN |
7501 | void wxGrid::SetDefaultCellOverflow( bool allow ) |
7502 | { | |
7503 | m_defaultCellAttr->SetOverflow(allow); | |
7504 | } | |
7505 | ||
2e9a6788 VZ |
7506 | void wxGrid::SetDefaultCellFont( const wxFont& font ) |
7507 | { | |
2796cce3 RD |
7508 | m_defaultCellAttr->SetFont(font); |
7509 | } | |
7510 | ||
ca63e8e9 RD |
7511 | // For editors and renderers the type registry takes precedence over the |
7512 | // default attr, so we need to register the new editor/renderer for the string | |
7513 | // data type in order to make setting a default editor/renderer appear to | |
7514 | // work correctly. | |
7515 | ||
0ba143c9 RD |
7516 | void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer) |
7517 | { | |
ca63e8e9 RD |
7518 | RegisterDataType(wxGRID_VALUE_STRING, |
7519 | renderer, | |
7520 | GetDefaultEditorForType(wxGRID_VALUE_STRING)); | |
0ba143c9 | 7521 | } |
2e9a6788 | 7522 | |
0ba143c9 RD |
7523 | void wxGrid::SetDefaultEditor(wxGridCellEditor *editor) |
7524 | { | |
ca63e8e9 RD |
7525 | RegisterDataType(wxGRID_VALUE_STRING, |
7526 | GetDefaultRendererForType(wxGRID_VALUE_STRING), | |
42841dfc | 7527 | editor); |
0ba143c9 | 7528 | } |
9b4aede2 | 7529 | |
2e9a6788 | 7530 | // ---------------------------------------------------------------------------- |
ef316e23 | 7531 | // access to the default attributes |
2e9a6788 VZ |
7532 | // ---------------------------------------------------------------------------- |
7533 | ||
ef316e23 | 7534 | wxColour wxGrid::GetDefaultCellBackgroundColour() const |
f85afd4e | 7535 | { |
2796cce3 | 7536 | return m_defaultCellAttr->GetBackgroundColour(); |
f85afd4e MB |
7537 | } |
7538 | ||
ef316e23 | 7539 | wxColour wxGrid::GetDefaultCellTextColour() const |
2e9a6788 | 7540 | { |
2796cce3 | 7541 | return m_defaultCellAttr->GetTextColour(); |
2e9a6788 VZ |
7542 | } |
7543 | ||
ef316e23 | 7544 | wxFont wxGrid::GetDefaultCellFont() const |
2e9a6788 | 7545 | { |
2796cce3 | 7546 | return m_defaultCellAttr->GetFont(); |
2e9a6788 VZ |
7547 | } |
7548 | ||
ef316e23 | 7549 | void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const |
2e9a6788 | 7550 | { |
2796cce3 | 7551 | m_defaultCellAttr->GetAlignment(horiz, vert); |
2e9a6788 VZ |
7552 | } |
7553 | ||
ef316e23 | 7554 | bool wxGrid::GetDefaultCellOverflow() const |
27f35b66 SN |
7555 | { |
7556 | return m_defaultCellAttr->GetOverflow(); | |
7557 | } | |
7558 | ||
0ba143c9 RD |
7559 | wxGridCellRenderer *wxGrid::GetDefaultRenderer() const |
7560 | { | |
0b190b0f | 7561 | return m_defaultCellAttr->GetRenderer(NULL, 0, 0); |
0ba143c9 | 7562 | } |
ab79958a | 7563 | |
0ba143c9 RD |
7564 | wxGridCellEditor *wxGrid::GetDefaultEditor() const |
7565 | { | |
4db6714b | 7566 | return m_defaultCellAttr->GetEditor(NULL, 0, 0); |
0ba143c9 | 7567 | } |
9b4aede2 | 7568 | |
2e9a6788 VZ |
7569 | // ---------------------------------------------------------------------------- |
7570 | // access to cell attributes | |
7571 | // ---------------------------------------------------------------------------- | |
7572 | ||
ef316e23 | 7573 | wxColour wxGrid::GetCellBackgroundColour(int row, int col) const |
f85afd4e | 7574 | { |
0a976765 | 7575 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7576 | wxColour colour = attr->GetBackgroundColour(); |
39bcce60 | 7577 | attr->DecRef(); |
2f024384 | 7578 | |
b99be8fb | 7579 | return colour; |
f85afd4e MB |
7580 | } |
7581 | ||
ef316e23 | 7582 | wxColour wxGrid::GetCellTextColour( int row, int col ) const |
f85afd4e | 7583 | { |
0a976765 | 7584 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7585 | wxColour colour = attr->GetTextColour(); |
39bcce60 | 7586 | attr->DecRef(); |
2f024384 | 7587 | |
b99be8fb | 7588 | return colour; |
f85afd4e MB |
7589 | } |
7590 | ||
ef316e23 | 7591 | wxFont wxGrid::GetCellFont( int row, int col ) const |
f85afd4e | 7592 | { |
0a976765 | 7593 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7594 | wxFont font = attr->GetFont(); |
39bcce60 | 7595 | attr->DecRef(); |
2f024384 | 7596 | |
b99be8fb | 7597 | return font; |
f85afd4e MB |
7598 | } |
7599 | ||
ef316e23 | 7600 | void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const |
f85afd4e | 7601 | { |
0a976765 | 7602 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7603 | attr->GetAlignment(horiz, vert); |
39bcce60 | 7604 | attr->DecRef(); |
2e9a6788 VZ |
7605 | } |
7606 | ||
ef316e23 | 7607 | bool wxGrid::GetCellOverflow( int row, int col ) const |
27f35b66 SN |
7608 | { |
7609 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
7610 | bool allow = attr->GetOverflow(); | |
7611 | attr->DecRef(); | |
4db6714b | 7612 | |
27f35b66 SN |
7613 | return allow; |
7614 | } | |
7615 | ||
ea99e8e3 VZ |
7616 | wxGrid::CellSpan |
7617 | wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const | |
27f35b66 SN |
7618 | { |
7619 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
7620 | attr->GetSize( num_rows, num_cols ); | |
7621 | attr->DecRef(); | |
ea99e8e3 VZ |
7622 | |
7623 | if ( *num_rows == 1 && *num_cols == 1 ) | |
7624 | return CellSpan_None; // just a normal cell | |
7625 | ||
7626 | if ( *num_rows < 0 || *num_cols < 0 ) | |
7627 | return CellSpan_Inside; // covered by a multi-span cell | |
7628 | ||
7629 | // this cell spans multiple cells to its right/bottom | |
7630 | return CellSpan_Main; | |
27f35b66 SN |
7631 | } |
7632 | ||
ef316e23 | 7633 | wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const |
2796cce3 RD |
7634 | { |
7635 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 7636 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); |
2796cce3 | 7637 | attr->DecRef(); |
0b190b0f | 7638 | |
2796cce3 RD |
7639 | return renderer; |
7640 | } | |
7641 | ||
ef316e23 | 7642 | wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const |
9b4aede2 RD |
7643 | { |
7644 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 7645 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
9b4aede2 | 7646 | attr->DecRef(); |
0b190b0f | 7647 | |
9b4aede2 RD |
7648 | return editor; |
7649 | } | |
7650 | ||
283b7808 VZ |
7651 | bool wxGrid::IsReadOnly(int row, int col) const |
7652 | { | |
7653 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
7654 | bool isReadOnly = attr->IsReadOnly(); | |
7655 | attr->DecRef(); | |
4db6714b | 7656 | |
283b7808 VZ |
7657 | return isReadOnly; |
7658 | } | |
7659 | ||
2e9a6788 | 7660 | // ---------------------------------------------------------------------------- |
758cbedf | 7661 | // attribute support: cache, automatic provider creation, ... |
2e9a6788 VZ |
7662 | // ---------------------------------------------------------------------------- |
7663 | ||
ef316e23 | 7664 | bool wxGrid::CanHaveAttributes() const |
2e9a6788 VZ |
7665 | { |
7666 | if ( !m_table ) | |
7667 | { | |
ca65c044 | 7668 | return false; |
2e9a6788 VZ |
7669 | } |
7670 | ||
f2d76237 | 7671 | return m_table->CanHaveAttributes(); |
2e9a6788 VZ |
7672 | } |
7673 | ||
0a976765 VZ |
7674 | void wxGrid::ClearAttrCache() |
7675 | { | |
7676 | if ( m_attrCache.row != -1 ) | |
7677 | { | |
54181a33 | 7678 | wxGridCellAttr *oldAttr = m_attrCache.attr; |
19d7140e | 7679 | m_attrCache.attr = NULL; |
0a976765 | 7680 | m_attrCache.row = -1; |
1506cc66 SN |
7681 | // wxSafeDecRec(...) might cause event processing that accesses |
7682 | // the cached attribute, if one exists (e.g. by deleting the | |
7683 | // editor stored within the attribute). Therefore it is important | |
ace8d849 | 7684 | // to invalidate the cache before calling wxSafeDecRef! |
1506cc66 | 7685 | wxSafeDecRef(oldAttr); |
0a976765 VZ |
7686 | } |
7687 | } | |
7688 | ||
b99450ee VZ |
7689 | void wxGrid::RefreshAttr(int row, int col) |
7690 | { | |
7691 | if ( m_attrCache.row == row && m_attrCache.col == col ) | |
7692 | ClearAttrCache(); | |
7693 | } | |
7694 | ||
7695 | ||
0a976765 VZ |
7696 | void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const |
7697 | { | |
2b5f62a0 VZ |
7698 | if ( attr != NULL ) |
7699 | { | |
f48a1159 | 7700 | wxGrid * const self = const_cast<wxGrid *>(this); |
0a976765 | 7701 | |
2b5f62a0 VZ |
7702 | self->ClearAttrCache(); |
7703 | self->m_attrCache.row = row; | |
7704 | self->m_attrCache.col = col; | |
7705 | self->m_attrCache.attr = attr; | |
7706 | wxSafeIncRef(attr); | |
7707 | } | |
0a976765 VZ |
7708 | } |
7709 | ||
7710 | bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const | |
7711 | { | |
7712 | if ( row == m_attrCache.row && col == m_attrCache.col ) | |
7713 | { | |
7714 | *attr = m_attrCache.attr; | |
39bcce60 | 7715 | wxSafeIncRef(m_attrCache.attr); |
0a976765 VZ |
7716 | |
7717 | #ifdef DEBUG_ATTR_CACHE | |
7718 | gs_nAttrCacheHits++; | |
7719 | #endif | |
7720 | ||
ca65c044 | 7721 | return true; |
0a976765 VZ |
7722 | } |
7723 | else | |
7724 | { | |
7725 | #ifdef DEBUG_ATTR_CACHE | |
7726 | gs_nAttrCacheMisses++; | |
7727 | #endif | |
4db6714b | 7728 | |
ca65c044 | 7729 | return false; |
0a976765 VZ |
7730 | } |
7731 | } | |
7732 | ||
2e9a6788 VZ |
7733 | wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const |
7734 | { | |
a373d23b SN |
7735 | wxGridCellAttr *attr = NULL; |
7736 | // Additional test to avoid looking at the cache e.g. for | |
7737 | // wxNoCellCoords, as this will confuse memory management. | |
7738 | if ( row >= 0 ) | |
7739 | { | |
3ed884a0 SN |
7740 | if ( !LookupAttr(row, col, &attr) ) |
7741 | { | |
c2f5b920 | 7742 | attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any) |
10a4531d | 7743 | : NULL; |
3ed884a0 SN |
7744 | CacheAttr(row, col, attr); |
7745 | } | |
0a976765 | 7746 | } |
4db6714b | 7747 | |
508011ce VZ |
7748 | if (attr) |
7749 | { | |
2796cce3 | 7750 | attr->SetDefAttr(m_defaultCellAttr); |
508011ce VZ |
7751 | } |
7752 | else | |
7753 | { | |
2796cce3 RD |
7754 | attr = m_defaultCellAttr; |
7755 | attr->IncRef(); | |
7756 | } | |
2e9a6788 | 7757 | |
0a976765 VZ |
7758 | return attr; |
7759 | } | |
7760 | ||
7761 | wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const | |
7762 | { | |
10a4531d | 7763 | wxGridCellAttr *attr = NULL; |
71e60f70 | 7764 | bool canHave = ((wxGrid*)this)->CanHaveAttributes(); |
0a976765 | 7765 | |
9a83f860 VZ |
7766 | wxCHECK_MSG( canHave, attr, wxT("Cell attributes not allowed")); |
7767 | wxCHECK_MSG( m_table, attr, wxT("must have a table") ); | |
1df4050d VZ |
7768 | |
7769 | attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell); | |
7770 | if ( !attr ) | |
7771 | { | |
7772 | attr = new wxGridCellAttr(m_defaultCellAttr); | |
7773 | ||
7774 | // artificially inc the ref count to match DecRef() in caller | |
7775 | attr->IncRef(); | |
7776 | m_table->SetAttr(attr, row, col); | |
7777 | } | |
0a976765 | 7778 | |
2e9a6788 VZ |
7779 | return attr; |
7780 | } | |
7781 | ||
0b190b0f VZ |
7782 | // ---------------------------------------------------------------------------- |
7783 | // setting column attributes (wrappers around SetColAttr) | |
7784 | // ---------------------------------------------------------------------------- | |
7785 | ||
7786 | void wxGrid::SetColFormatBool(int col) | |
7787 | { | |
7788 | SetColFormatCustom(col, wxGRID_VALUE_BOOL); | |
7789 | } | |
7790 | ||
7791 | void wxGrid::SetColFormatNumber(int col) | |
7792 | { | |
7793 | SetColFormatCustom(col, wxGRID_VALUE_NUMBER); | |
7794 | } | |
7795 | ||
7796 | void wxGrid::SetColFormatFloat(int col, int width, int precision) | |
7797 | { | |
7798 | wxString typeName = wxGRID_VALUE_FLOAT; | |
7799 | if ( (width != -1) || (precision != -1) ) | |
7800 | { | |
9a83f860 | 7801 | typeName << wxT(':') << width << wxT(',') << precision; |
0b190b0f VZ |
7802 | } |
7803 | ||
7804 | SetColFormatCustom(col, typeName); | |
7805 | } | |
7806 | ||
7807 | void wxGrid::SetColFormatCustom(int col, const wxString& typeName) | |
7808 | { | |
999836aa | 7809 | wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col ); |
4db6714b | 7810 | if (!attr) |
19d7140e | 7811 | attr = new wxGridCellAttr; |
0b190b0f VZ |
7812 | wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName); |
7813 | attr->SetRenderer(renderer); | |
54181a33 VZ |
7814 | wxGridCellEditor *editor = GetDefaultEditorForType(typeName); |
7815 | attr->SetEditor(editor); | |
0b190b0f VZ |
7816 | |
7817 | SetColAttr(col, attr); | |
19d7140e | 7818 | |
0b190b0f VZ |
7819 | } |
7820 | ||
758cbedf VZ |
7821 | // ---------------------------------------------------------------------------- |
7822 | // setting cell attributes: this is forwarded to the table | |
7823 | // ---------------------------------------------------------------------------- | |
7824 | ||
27f35b66 SN |
7825 | void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr) |
7826 | { | |
7827 | if ( CanHaveAttributes() ) | |
7828 | { | |
7829 | m_table->SetAttr(attr, row, col); | |
7830 | ClearAttrCache(); | |
7831 | } | |
7832 | else | |
7833 | { | |
7834 | wxSafeDecRef(attr); | |
7835 | } | |
7836 | } | |
7837 | ||
758cbedf VZ |
7838 | void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr) |
7839 | { | |
7840 | if ( CanHaveAttributes() ) | |
7841 | { | |
7842 | m_table->SetRowAttr(attr, row); | |
19d7140e | 7843 | ClearAttrCache(); |
758cbedf VZ |
7844 | } |
7845 | else | |
7846 | { | |
39bcce60 | 7847 | wxSafeDecRef(attr); |
758cbedf VZ |
7848 | } |
7849 | } | |
7850 | ||
7851 | void wxGrid::SetColAttr(int col, wxGridCellAttr *attr) | |
7852 | { | |
7853 | if ( CanHaveAttributes() ) | |
7854 | { | |
7855 | m_table->SetColAttr(attr, col); | |
19d7140e | 7856 | ClearAttrCache(); |
758cbedf VZ |
7857 | } |
7858 | else | |
7859 | { | |
39bcce60 | 7860 | wxSafeDecRef(attr); |
758cbedf VZ |
7861 | } |
7862 | } | |
7863 | ||
2e9a6788 VZ |
7864 | void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour ) |
7865 | { | |
7866 | if ( CanHaveAttributes() ) | |
7867 | { | |
0a976765 | 7868 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7869 | attr->SetBackgroundColour(colour); |
7870 | attr->DecRef(); | |
7871 | } | |
7872 | } | |
7873 | ||
7874 | void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour ) | |
7875 | { | |
7876 | if ( CanHaveAttributes() ) | |
7877 | { | |
0a976765 | 7878 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7879 | attr->SetTextColour(colour); |
7880 | attr->DecRef(); | |
7881 | } | |
7882 | } | |
7883 | ||
7884 | void wxGrid::SetCellFont( int row, int col, const wxFont& font ) | |
7885 | { | |
7886 | if ( CanHaveAttributes() ) | |
7887 | { | |
0a976765 | 7888 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7889 | attr->SetFont(font); |
7890 | attr->DecRef(); | |
7891 | } | |
7892 | } | |
7893 | ||
7894 | void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert ) | |
7895 | { | |
7896 | if ( CanHaveAttributes() ) | |
7897 | { | |
0a976765 | 7898 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7899 | attr->SetAlignment(horiz, vert); |
7900 | attr->DecRef(); | |
ab79958a VZ |
7901 | } |
7902 | } | |
7903 | ||
27f35b66 SN |
7904 | void wxGrid::SetCellOverflow( int row, int col, bool allow ) |
7905 | { | |
7906 | if ( CanHaveAttributes() ) | |
7907 | { | |
7908 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7909 | attr->SetOverflow(allow); | |
7910 | attr->DecRef(); | |
7911 | } | |
7912 | } | |
7913 | ||
7914 | void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols ) | |
7915 | { | |
7916 | if ( CanHaveAttributes() ) | |
7917 | { | |
7918 | int cell_rows, cell_cols; | |
7919 | ||
7920 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7921 | attr->GetSize(&cell_rows, &cell_cols); | |
7922 | attr->SetSize(num_rows, num_cols); | |
7923 | attr->DecRef(); | |
7924 | ||
7925 | // Cannot set the size of a cell to 0 or negative values | |
7926 | // While it is perfectly legal to do that, this function cannot | |
7927 | // handle all the possibilies, do it by hand by getting the CellAttr. | |
7928 | // You can only set the size of a cell to 1,1 or greater with this fn | |
7929 | wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)), | |
7930 | wxT("wxGrid::SetCellSize setting cell size that is already part of another cell")); | |
7931 | wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)), | |
7932 | wxT("wxGrid::SetCellSize setting cell size to < 1")); | |
7933 | ||
7934 | // if this was already a multicell then "turn off" the other cells first | |
a6b2078d | 7935 | if ((cell_rows > 1) || (cell_cols > 1)) |
27f35b66 SN |
7936 | { |
7937 | int i, j; | |
2f024384 | 7938 | for (j=row; j < row + cell_rows; j++) |
27f35b66 | 7939 | { |
2f024384 | 7940 | for (i=col; i < col + cell_cols; i++) |
27f35b66 SN |
7941 | { |
7942 | if ((i != col) || (j != row)) | |
7943 | { | |
7944 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
7945 | attr_stub->SetSize( 1, 1 ); | |
7946 | attr_stub->DecRef(); | |
7947 | } | |
7948 | } | |
7949 | } | |
7950 | } | |
7951 | ||
7952 | // mark the cells that will be covered by this cell to | |
7953 | // negative or zero values to point back at this cell | |
7954 | if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1)) | |
7955 | { | |
7956 | int i, j; | |
2f024384 | 7957 | for (j=row; j < row + num_rows; j++) |
27f35b66 | 7958 | { |
2f024384 | 7959 | for (i=col; i < col + num_cols; i++) |
27f35b66 SN |
7960 | { |
7961 | if ((i != col) || (j != row)) | |
7962 | { | |
7963 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
2f024384 | 7964 | attr_stub->SetSize( row - j, col - i ); |
27f35b66 SN |
7965 | attr_stub->DecRef(); |
7966 | } | |
7967 | } | |
7968 | } | |
7969 | } | |
7970 | } | |
7971 | } | |
7972 | ||
ab79958a VZ |
7973 | void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer) |
7974 | { | |
7975 | if ( CanHaveAttributes() ) | |
7976 | { | |
0a976765 | 7977 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
ab79958a VZ |
7978 | attr->SetRenderer(renderer); |
7979 | attr->DecRef(); | |
9b4aede2 RD |
7980 | } |
7981 | } | |
7982 | ||
7983 | void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor) | |
7984 | { | |
7985 | if ( CanHaveAttributes() ) | |
7986 | { | |
7987 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7988 | attr->SetEditor(editor); | |
7989 | attr->DecRef(); | |
283b7808 VZ |
7990 | } |
7991 | } | |
7992 | ||
7993 | void wxGrid::SetReadOnly(int row, int col, bool isReadOnly) | |
7994 | { | |
7995 | if ( CanHaveAttributes() ) | |
7996 | { | |
7997 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7998 | attr->SetReadOnly(isReadOnly); | |
7999 | attr->DecRef(); | |
2e9a6788 | 8000 | } |
f85afd4e MB |
8001 | } |
8002 | ||
f2d76237 RD |
8003 | // ---------------------------------------------------------------------------- |
8004 | // Data type registration | |
8005 | // ---------------------------------------------------------------------------- | |
8006 | ||
8007 | void wxGrid::RegisterDataType(const wxString& typeName, | |
8008 | wxGridCellRenderer* renderer, | |
8009 | wxGridCellEditor* editor) | |
8010 | { | |
8011 | m_typeRegistry->RegisterDataType(typeName, renderer, editor); | |
8012 | } | |
8013 | ||
8014 | ||
a9339fe2 | 8015 | wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const |
f2d76237 RD |
8016 | { |
8017 | wxString typeName = m_table->GetTypeName(row, col); | |
8018 | return GetDefaultEditorForType(typeName); | |
8019 | } | |
8020 | ||
a9339fe2 | 8021 | wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const |
f2d76237 RD |
8022 | { |
8023 | wxString typeName = m_table->GetTypeName(row, col); | |
8024 | return GetDefaultRendererForType(typeName); | |
8025 | } | |
8026 | ||
a9339fe2 | 8027 | wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const |
f2d76237 | 8028 | { |
c4608a8a | 8029 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
8030 | if ( index == wxNOT_FOUND ) |
8031 | { | |
767e0835 | 8032 | wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str())); |
0b190b0f | 8033 | |
f2d76237 RD |
8034 | return NULL; |
8035 | } | |
0b190b0f | 8036 | |
f2d76237 RD |
8037 | return m_typeRegistry->GetEditor(index); |
8038 | } | |
8039 | ||
a9339fe2 | 8040 | wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const |
f2d76237 | 8041 | { |
c4608a8a | 8042 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
8043 | if ( index == wxNOT_FOUND ) |
8044 | { | |
767e0835 | 8045 | wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str())); |
0b190b0f | 8046 | |
c4608a8a | 8047 | return NULL; |
e72b4213 | 8048 | } |
0b190b0f | 8049 | |
c4608a8a | 8050 | return m_typeRegistry->GetRenderer(index); |
f2d76237 RD |
8051 | } |
8052 | ||
2e9a6788 VZ |
8053 | // ---------------------------------------------------------------------------- |
8054 | // row/col size | |
8055 | // ---------------------------------------------------------------------------- | |
8056 | ||
82edfbe7 VZ |
8057 | void wxGrid::DoDisableLineResize(int line, wxGridFixedIndicesSet *& setFixed) |
8058 | { | |
8059 | if ( !setFixed ) | |
8060 | { | |
8061 | setFixed = new wxGridFixedIndicesSet; | |
8062 | } | |
8063 | ||
8064 | setFixed->insert(line); | |
8065 | } | |
8066 | ||
8067 | bool | |
8068 | wxGrid::DoCanResizeLine(int line, const wxGridFixedIndicesSet *setFixed) const | |
8069 | { | |
8070 | return !setFixed || !setFixed->count(line); | |
8071 | } | |
8072 | ||
6e8524b1 MB |
8073 | void wxGrid::EnableDragRowSize( bool enable ) |
8074 | { | |
8075 | m_canDragRowSize = enable; | |
8076 | } | |
8077 | ||
6e8524b1 MB |
8078 | void wxGrid::EnableDragColSize( bool enable ) |
8079 | { | |
8080 | m_canDragColSize = enable; | |
8081 | } | |
8082 | ||
4cfa5de6 RD |
8083 | void wxGrid::EnableDragGridSize( bool enable ) |
8084 | { | |
8085 | m_canDragGridSize = enable; | |
8086 | } | |
8087 | ||
79dbea21 RD |
8088 | void wxGrid::EnableDragCell( bool enable ) |
8089 | { | |
8090 | m_canDragCell = enable; | |
8091 | } | |
6e8524b1 | 8092 | |
f85afd4e MB |
8093 | void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) |
8094 | { | |
b8d24d4e | 8095 | m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight ); |
f85afd4e MB |
8096 | |
8097 | if ( resizeExistingRows ) | |
8098 | { | |
b1da8107 SN |
8099 | // since we are resizing all rows to the default row size, |
8100 | // we can simply clear the row heights and row bottoms | |
8101 | // arrays (which also allows us to take advantage of | |
8102 | // some speed optimisations) | |
8103 | m_rowHeights.Empty(); | |
8104 | m_rowBottoms.Empty(); | |
edb89f7e VZ |
8105 | if ( !GetBatchCount() ) |
8106 | CalcDimensions(); | |
f85afd4e MB |
8107 | } |
8108 | } | |
8109 | ||
30496905 VZ |
8110 | namespace |
8111 | { | |
8112 | ||
8113 | // This is a common part of SetRowSize() and SetColSize() which takes care of | |
8114 | // updating the height/width of a row/column depending on its current value and | |
8115 | // the new one. | |
8116 | // | |
8117 | // Returns the difference between the new and the old size. | |
8118 | int UpdateRowOrColSize(int& sizeCurrent, int sizeNew) | |
8119 | { | |
8120 | // On input here sizeCurrent can be negative if it's currently hidden (the | |
8121 | // real size is its absolute value then). And sizeNew can be 0 to indicate | |
8122 | // that the row/column should be hidden or -1 to indicate that it should be | |
8123 | // shown again. | |
8124 | ||
8125 | if ( sizeNew < 0 ) | |
8126 | { | |
8127 | // We're showing back a previously hidden row/column. | |
8128 | wxASSERT_MSG( sizeNew == -1, wxS("New size must be positive or -1.") ); | |
8129 | ||
2328f468 VZ |
8130 | // If it's already visible, simply do nothing. |
8131 | if ( sizeCurrent >= 0 ) | |
8132 | return 0; | |
30496905 | 8133 | |
2328f468 | 8134 | // Otherwise show it by restoring its old size. |
30496905 VZ |
8135 | sizeCurrent = -sizeCurrent; |
8136 | ||
8137 | // This is positive which is correct. | |
8138 | return sizeCurrent; | |
8139 | } | |
8140 | else if ( sizeNew == 0 ) | |
8141 | { | |
8142 | // We're hiding a row/column. | |
30496905 | 8143 | |
2328f468 VZ |
8144 | // If it's already hidden, simply do nothing. |
8145 | if ( sizeCurrent <= 0 ) | |
8146 | return 0; | |
8147 | ||
8148 | // Otherwise hide it and also remember the shown size to be able to | |
8149 | // restore it later. | |
30496905 VZ |
8150 | sizeCurrent = -sizeCurrent; |
8151 | ||
8152 | // This is negative which is correct. | |
8153 | return sizeCurrent; | |
8154 | } | |
8155 | else // We're just changing the row/column size. | |
8156 | { | |
8157 | // Here it could have been hidden or not previously. | |
8158 | const int sizeOld = sizeCurrent < 0 ? 0 : sizeCurrent; | |
8159 | ||
8160 | sizeCurrent = sizeNew; | |
8161 | ||
8162 | return sizeCurrent - sizeOld; | |
8163 | } | |
8164 | } | |
8165 | ||
8166 | } // anonymous namespace | |
8167 | ||
f85afd4e MB |
8168 | void wxGrid::SetRowSize( int row, int height ) |
8169 | { | |
30496905 VZ |
8170 | // See comment in SetColSize |
8171 | if ( height > 0 && height < GetRowMinimalAcceptableHeight()) | |
8172 | return; | |
60ff3b99 | 8173 | |
30496905 | 8174 | // The value of -1 is special and means to fit the height to the row label. |
d929eddf VZ |
8175 | // As with the columns, ignore attempts to auto-size the hidden rows. |
8176 | if ( height == -1 && GetRowHeight(row) != 0 ) | |
ad7502d8 SN |
8177 | { |
8178 | long w, h; | |
8179 | wxArrayString lines; | |
8180 | wxClientDC dc(m_rowLabelWin); | |
8181 | dc.SetFont(GetLabelFont()); | |
8182 | StringToLines(GetRowLabelValue( row ), lines); | |
ace8d849 VZ |
8183 | GetTextBoxSize( dc, lines, &w, &h ); |
8184 | //check that it is not less than the minimal height | |
8185 | height = wxMax(h, GetRowMinimalAcceptableHeight()); | |
ad7502d8 SN |
8186 | } |
8187 | ||
30496905 VZ |
8188 | DoSetRowSize(row, height); |
8189 | } | |
8190 | ||
8191 | void wxGrid::DoSetRowSize( int row, int height ) | |
8192 | { | |
8193 | wxCHECK_RET( row >= 0 && row < m_numRows, wxT("invalid row index") ); | |
b8d24d4e | 8194 | |
7c1cb261 VZ |
8195 | if ( m_rowHeights.IsEmpty() ) |
8196 | { | |
8197 | // need to really create the array | |
8198 | InitRowHeights(); | |
8199 | } | |
60ff3b99 | 8200 | |
30496905 VZ |
8201 | const int diff = UpdateRowOrColSize(m_rowHeights[row], height); |
8202 | if ( !diff ) | |
8203 | return; | |
60ff3b99 | 8204 | |
0ed3b812 | 8205 | for ( int i = row; i < m_numRows; i++ ) |
f85afd4e | 8206 | { |
b99be8fb | 8207 | m_rowBottoms[i] += diff; |
f85afd4e | 8208 | } |
2f024384 | 8209 | |
a6b40a9a VZ |
8210 | InvalidateBestSize(); |
8211 | ||
faec5a43 | 8212 | if ( !GetBatchCount() ) |
ce84ef6a | 8213 | { |
faec5a43 | 8214 | CalcDimensions(); |
ce84ef6a VZ |
8215 | Refresh(); |
8216 | } | |
f85afd4e MB |
8217 | } |
8218 | ||
8219 | void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) | |
8220 | { | |
ef316e23 VZ |
8221 | // we dont allow zero default column width |
8222 | m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 ); | |
f85afd4e MB |
8223 | |
8224 | if ( resizeExistingCols ) | |
8225 | { | |
b1da8107 SN |
8226 | // since we are resizing all columns to the default column size, |
8227 | // we can simply clear the col widths and col rights | |
8228 | // arrays (which also allows us to take advantage of | |
8229 | // some speed optimisations) | |
8230 | m_colWidths.Empty(); | |
8231 | m_colRights.Empty(); | |
edb89f7e VZ |
8232 | if ( !GetBatchCount() ) |
8233 | CalcDimensions(); | |
f85afd4e MB |
8234 | } |
8235 | } | |
8236 | ||
8237 | void wxGrid::SetColSize( int col, int width ) | |
8238 | { | |
30496905 VZ |
8239 | // we intentionally don't test whether the width is less than |
8240 | // GetColMinimalWidth() here but we do compare it with | |
8241 | // GetColMinimalAcceptableWidth() as otherwise things currently break (see | |
8242 | // #651) -- and we also always allow the width of 0 as it has the special | |
8243 | // sense of hiding the column | |
8244 | if ( width > 0 && width < GetColMinimalAcceptableWidth() ) | |
8245 | return; | |
60ff3b99 | 8246 | |
30496905 | 8247 | // The value of -1 is special and means to fit the width to the column label. |
d929eddf VZ |
8248 | // |
8249 | // Notice that we currently don't support auto-sizing hidden columns (we | |
8250 | // could, but it's not clear whether this is really needed and it would | |
8251 | // make the code more complex), and for them passing -1 simply means to | |
8252 | // show the column back using its old size. | |
8253 | if ( width == -1 && GetColWidth(col) != 0 ) | |
ad7502d8 SN |
8254 | { |
8255 | long w, h; | |
8256 | wxArrayString lines; | |
ad805b9e | 8257 | wxClientDC dc(m_colWindow); |
ad7502d8 SN |
8258 | dc.SetFont(GetLabelFont()); |
8259 | StringToLines(GetColLabelValue(col), lines); | |
8260 | if ( GetColLabelTextOrientation() == wxHORIZONTAL ) | |
8261 | GetTextBoxSize( dc, lines, &w, &h ); | |
8262 | else | |
8263 | GetTextBoxSize( dc, lines, &h, &w ); | |
8264 | width = w + 6; | |
ace8d849 | 8265 | //check that it is not less than the minimal width |
54181a33 | 8266 | width = wxMax(width, GetColMinimalAcceptableWidth()); |
ad7502d8 SN |
8267 | } |
8268 | ||
30496905 VZ |
8269 | DoSetColSize(col, width); |
8270 | } | |
8271 | ||
8272 | void wxGrid::DoSetColSize( int col, int width ) | |
8273 | { | |
8274 | wxCHECK_RET( col >= 0 && col < m_numCols, wxT("invalid column index") ); | |
3e13956a | 8275 | |
7c1cb261 VZ |
8276 | if ( m_colWidths.IsEmpty() ) |
8277 | { | |
8278 | // need to really create the array | |
8279 | InitColWidths(); | |
8280 | } | |
f85afd4e | 8281 | |
30496905 VZ |
8282 | const int diff = UpdateRowOrColSize(m_colWidths[col], width); |
8283 | if ( !diff ) | |
8284 | return; | |
8285 | ||
009c7216 | 8286 | if ( m_useNativeHeader ) |
3039ade9 | 8287 | GetGridColHeader()->UpdateColumn(col); |
009c7216 | 8288 | //else: will be refreshed when the header is redrawn |
60ff3b99 | 8289 | |
0ed3b812 | 8290 | for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ ) |
f85afd4e | 8291 | { |
0ed3b812 | 8292 | m_colRights[GetColAt(colPos)] += diff; |
f85afd4e | 8293 | } |
962a48f6 | 8294 | |
a6b40a9a VZ |
8295 | InvalidateBestSize(); |
8296 | ||
faec5a43 | 8297 | if ( !GetBatchCount() ) |
ad805b9e | 8298 | { |
faec5a43 | 8299 | CalcDimensions(); |
ad805b9e VZ |
8300 | Refresh(); |
8301 | } | |
f85afd4e MB |
8302 | } |
8303 | ||
43947979 VZ |
8304 | void wxGrid::SetColMinimalWidth( int col, int width ) |
8305 | { | |
4db6714b KH |
8306 | if (width > GetColMinimalAcceptableWidth()) |
8307 | { | |
c6fbe2f0 | 8308 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col; |
8253f2e0 | 8309 | m_colMinWidths[key] = width; |
b8d24d4e | 8310 | } |
af547d51 VZ |
8311 | } |
8312 | ||
8313 | void wxGrid::SetRowMinimalHeight( int row, int width ) | |
8314 | { | |
4db6714b KH |
8315 | if (width > GetRowMinimalAcceptableHeight()) |
8316 | { | |
c6fbe2f0 | 8317 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row; |
8253f2e0 | 8318 | m_rowMinHeights[key] = width; |
b8d24d4e | 8319 | } |
43947979 VZ |
8320 | } |
8321 | ||
8322 | int wxGrid::GetColMinimalWidth(int col) const | |
8323 | { | |
c6fbe2f0 | 8324 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col; |
8253f2e0 | 8325 | wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key); |
962a48f6 | 8326 | |
ba8c1601 | 8327 | return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth; |
af547d51 VZ |
8328 | } |
8329 | ||
8330 | int wxGrid::GetRowMinimalHeight(int row) const | |
8331 | { | |
c6fbe2f0 | 8332 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row; |
8253f2e0 | 8333 | wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key); |
962a48f6 | 8334 | |
ba8c1601 | 8335 | return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight; |
b8d24d4e RG |
8336 | } |
8337 | ||
8338 | void wxGrid::SetColMinimalAcceptableWidth( int width ) | |
8339 | { | |
20c84410 | 8340 | // We do allow a width of 0 since this gives us |
962a48f6 DS |
8341 | // an easy way to temporarily hiding columns. |
8342 | if ( width >= 0 ) | |
8343 | m_minAcceptableColWidth = width; | |
b8d24d4e RG |
8344 | } |
8345 | ||
8346 | void wxGrid::SetRowMinimalAcceptableHeight( int height ) | |
8347 | { | |
20c84410 | 8348 | // We do allow a height of 0 since this gives us |
962a48f6 DS |
8349 | // an easy way to temporarily hiding rows. |
8350 | if ( height >= 0 ) | |
8351 | m_minAcceptableRowHeight = height; | |
17a1ebd1 | 8352 | } |
b8d24d4e RG |
8353 | |
8354 | int wxGrid::GetColMinimalAcceptableWidth() const | |
8355 | { | |
8356 | return m_minAcceptableColWidth; | |
8357 | } | |
8358 | ||
8359 | int wxGrid::GetRowMinimalAcceptableHeight() const | |
8360 | { | |
8361 | return m_minAcceptableRowHeight; | |
43947979 VZ |
8362 | } |
8363 | ||
57c086ef VZ |
8364 | // ---------------------------------------------------------------------------- |
8365 | // auto sizing | |
8366 | // ---------------------------------------------------------------------------- | |
8367 | ||
733f486a VZ |
8368 | void |
8369 | wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction) | |
65e4e78e | 8370 | { |
733f486a VZ |
8371 | const bool column = direction == wxGRID_COLUMN; |
8372 | ||
6922252d VZ |
8373 | // We don't support auto-sizing hidden rows or columns, this doesn't seem |
8374 | // to make much sense. | |
8375 | if ( column ) | |
8376 | { | |
8377 | if ( GetColWidth(colOrRow) == 0 ) | |
8378 | return; | |
8379 | } | |
8380 | else | |
8381 | { | |
8382 | if ( GetRowHeight(colOrRow) == 0 ) | |
8383 | return; | |
8384 | } | |
8385 | ||
65e4e78e VZ |
8386 | wxClientDC dc(m_gridWin); |
8387 | ||
962a48f6 | 8388 | // cancel editing of cell |
13f6e9e8 RG |
8389 | HideCellEditControl(); |
8390 | SaveEditControlValue(); | |
8391 | ||
68abd97d VZ |
8392 | // initialize both of them just to avoid compiler warnings even if only |
8393 | // really needs to be initialized here | |
8394 | int row, | |
8395 | col; | |
8396 | if ( column ) | |
8397 | { | |
8398 | row = -1; | |
8399 | col = colOrRow; | |
8400 | } | |
8401 | else | |
8402 | { | |
8403 | row = colOrRow; | |
a95e38c0 | 8404 | col = -1; |
68abd97d | 8405 | } |
af547d51 VZ |
8406 | |
8407 | wxCoord extent, extentMax = 0; | |
8408 | int max = column ? m_numRows : m_numCols; | |
39bcce60 | 8409 | for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ ) |
65e4e78e | 8410 | { |
af547d51 | 8411 | if ( column ) |
bc7d2e9d | 8412 | { |
af547d51 | 8413 | row = rowOrCol; |
bc7d2e9d VZ |
8414 | col = colOrRow; |
8415 | } | |
6922252d | 8416 | else |
bc7d2e9d VZ |
8417 | { |
8418 | row = colOrRow; | |
af547d51 | 8419 | col = rowOrCol; |
bc7d2e9d | 8420 | } |
774f6b31 VZ |
8421 | |
8422 | // we need to account for the cells spanning multiple columns/rows: | |
8423 | // while they may need a lot of space, they don't need all of it in | |
8424 | // this column/row | |
8425 | int numRows, numCols; | |
8426 | const CellSpan span = GetCellSize(row, col, &numRows, &numCols); | |
8427 | if ( span == CellSpan_Inside ) | |
8428 | { | |
8429 | // we need to get the size of the main cell, not of a cell hidden | |
8430 | // by it | |
8431 | row += numRows; | |
8432 | col += numCols; | |
8433 | ||
8434 | // get the size of the main cell too | |
8435 | GetCellSize(row, col, &numRows, &numCols); | |
8436 | } | |
af547d51 | 8437 | |
bc7d2e9d | 8438 | // get cell ( main cell if CellSpan_Inside ) renderer best size |
2f024384 DS |
8439 | wxGridCellAttr *attr = GetCellAttr(row, col); |
8440 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); | |
65e4e78e VZ |
8441 | if ( renderer ) |
8442 | { | |
af547d51 VZ |
8443 | wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col); |
8444 | extent = column ? size.x : size.y; | |
774f6b31 VZ |
8445 | |
8446 | if ( span != CellSpan_None ) | |
8447 | { | |
8448 | // we spread the size of a spanning cell over all the cells it | |
8449 | // covers evenly -- this is probably not ideal but we can't | |
8450 | // really do much better here | |
8451 | // | |
8452 | // notice that numCols and numRows are never 0 as they | |
8453 | // correspond to the size of the main cell of the span and not | |
8454 | // of the cell inside it | |
8455 | extent /= column ? numCols : numRows; | |
8456 | } | |
8457 | ||
af547d51 | 8458 | if ( extent > extentMax ) |
af547d51 | 8459 | extentMax = extent; |
0b190b0f VZ |
8460 | |
8461 | renderer->DecRef(); | |
65e4e78e VZ |
8462 | } |
8463 | ||
8464 | attr->DecRef(); | |
8465 | } | |
8466 | ||
af547d51 VZ |
8467 | // now also compare with the column label extent |
8468 | wxCoord w, h; | |
65e4e78e | 8469 | dc.SetFont( GetLabelFont() ); |
294d195c MB |
8470 | |
8471 | if ( column ) | |
d43851f7 | 8472 | { |
bc7d2e9d | 8473 | dc.GetMultiLineTextExtent( GetColLabelValue(colOrRow), &w, &h ); |
4db6714b | 8474 | if ( GetColLabelTextOrientation() == wxVERTICAL ) |
d43851f7 JS |
8475 | w = h; |
8476 | } | |
294d195c | 8477 | else |
bc7d2e9d | 8478 | dc.GetMultiLineTextExtent( GetRowLabelValue(colOrRow), &w, &h ); |
294d195c | 8479 | |
af547d51 VZ |
8480 | extent = column ? w : h; |
8481 | if ( extent > extentMax ) | |
af547d51 | 8482 | extentMax = extent; |
65e4e78e | 8483 | |
af547d51 | 8484 | if ( !extentMax ) |
65e4e78e | 8485 | { |
af547d51 | 8486 | // empty column - give default extent (notice that if extentMax is less |
2f024384 | 8487 | // than default extent but != 0, it's OK) |
af547d51 | 8488 | extentMax = column ? m_defaultColWidth : m_defaultRowHeight; |
65e4e78e VZ |
8489 | } |
8490 | else | |
8491 | { | |
a95e38c0 | 8492 | if ( column ) |
a95e38c0 VZ |
8493 | // leave some space around text |
8494 | extentMax += 10; | |
f6bcfd97 | 8495 | else |
f6bcfd97 | 8496 | extentMax += 6; |
65e4e78e VZ |
8497 | } |
8498 | ||
edb89f7e VZ |
8499 | if ( column ) |
8500 | { | |
a01cfc08 VS |
8501 | // Ensure automatic width is not less than minimal width. See the |
8502 | // comment in SetColSize() for explanation of why this isn't done | |
8503 | // in SetColSize(). | |
8504 | if ( !setAsMin ) | |
bc7d2e9d | 8505 | extentMax = wxMax(extentMax, GetColMinimalWidth(colOrRow)); |
a01cfc08 | 8506 | |
bc7d2e9d | 8507 | SetColSize( colOrRow, extentMax ); |
faec5a43 SN |
8508 | if ( !GetBatchCount() ) |
8509 | { | |
ad805b9e VZ |
8510 | if ( m_useNativeHeader ) |
8511 | { | |
bc7d2e9d | 8512 | GetGridColHeader()->UpdateColumn(colOrRow); |
ad805b9e VZ |
8513 | } |
8514 | else | |
8515 | { | |
8516 | int cw, ch, dummy; | |
8517 | m_gridWin->GetClientSize( &cw, &ch ); | |
bc7d2e9d | 8518 | wxRect rect ( CellToRect( 0, colOrRow ) ); |
ad805b9e VZ |
8519 | rect.y = 0; |
8520 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
8521 | rect.width = cw - rect.x; | |
8522 | rect.height = m_colLabelHeight; | |
8523 | GetColLabelWindow()->Refresh( true, &rect ); | |
8524 | } | |
edb89f7e VZ |
8525 | } |
8526 | } | |
8527 | else | |
8528 | { | |
a01cfc08 VS |
8529 | // Ensure automatic width is not less than minimal height. See the |
8530 | // comment in SetColSize() for explanation of why this isn't done | |
8531 | // in SetRowSize(). | |
8532 | if ( !setAsMin ) | |
bc7d2e9d | 8533 | extentMax = wxMax(extentMax, GetRowMinimalHeight(colOrRow)); |
a01cfc08 | 8534 | |
bc7d2e9d | 8535 | SetRowSize(colOrRow, extentMax); |
faec5a43 SN |
8536 | if ( !GetBatchCount() ) |
8537 | { | |
edb89f7e VZ |
8538 | int cw, ch, dummy; |
8539 | m_gridWin->GetClientSize( &cw, &ch ); | |
bc7d2e9d | 8540 | wxRect rect( CellToRect( colOrRow, 0 ) ); |
edb89f7e VZ |
8541 | rect.x = 0; |
8542 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
8543 | rect.width = m_rowLabelWidth; | |
faec5a43 | 8544 | rect.height = ch - rect.y; |
ca65c044 | 8545 | m_rowLabelWin->Refresh( true, &rect ); |
edb89f7e | 8546 | } |
faec5a43 | 8547 | } |
2f024384 | 8548 | |
65e4e78e VZ |
8549 | if ( setAsMin ) |
8550 | { | |
af547d51 | 8551 | if ( column ) |
bc7d2e9d | 8552 | SetColMinimalWidth(colOrRow, extentMax); |
af547d51 | 8553 | else |
bc7d2e9d | 8554 | SetRowMinimalHeight(colOrRow, extentMax); |
65e4e78e VZ |
8555 | } |
8556 | } | |
8557 | ||
733f486a VZ |
8558 | wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction) |
8559 | { | |
8560 | // calculate size for the rows or columns? | |
8561 | const bool calcRows = direction == wxGRID_ROW; | |
8562 | ||
8563 | wxClientDC dc(calcRows ? GetGridRowLabelWindow() | |
8564 | : GetGridColLabelWindow()); | |
8565 | dc.SetFont(GetLabelFont()); | |
8566 | ||
8567 | // which dimension should we take into account for calculations? | |
8568 | // | |
8569 | // for columns, the text can be only horizontal so it's easy but for rows | |
8570 | // we also have to take into account the text orientation | |
8571 | const bool | |
8572 | useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL); | |
8573 | ||
8574 | wxArrayString lines; | |
8575 | wxCoord extentMax = 0; | |
8576 | ||
8577 | const int numRowsOrCols = calcRows ? m_numRows : m_numCols; | |
8578 | for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ ) | |
8579 | { | |
8580 | lines.Clear(); | |
add4bb40 VS |
8581 | |
8582 | wxString label = calcRows ? GetRowLabelValue(rowOrCol) | |
8583 | : GetColLabelValue(rowOrCol); | |
8584 | StringToLines(label, lines); | |
733f486a VZ |
8585 | |
8586 | long w, h; | |
8587 | GetTextBoxSize(dc, lines, &w, &h); | |
8588 | ||
8589 | const wxCoord extent = useWidth ? w : h; | |
8590 | if ( extent > extentMax ) | |
8591 | extentMax = extent; | |
8592 | } | |
8593 | ||
8594 | if ( !extentMax ) | |
8595 | { | |
8596 | // empty column - give default extent (notice that if extentMax is less | |
8597 | // than default extent but != 0, it's OK) | |
8598 | extentMax = calcRows ? GetDefaultRowLabelSize() | |
8599 | : GetDefaultColLabelSize(); | |
8600 | } | |
8601 | ||
8602 | // leave some space around text (taken from AutoSizeColOrRow) | |
8603 | if ( calcRows ) | |
8604 | extentMax += 10; | |
8605 | else | |
8606 | extentMax += 6; | |
8607 | ||
8608 | return extentMax; | |
8609 | } | |
8610 | ||
266e8367 | 8611 | int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) |
65e4e78e | 8612 | { |
57c086ef VZ |
8613 | int width = m_rowLabelWidth; |
8614 | ||
b62f94ff VZ |
8615 | wxGridUpdateLocker locker; |
8616 | if(!calcOnly) | |
8617 | locker.Create(this); | |
97a9929e | 8618 | |
65e4e78e VZ |
8619 | for ( int col = 0; col < m_numCols; col++ ) |
8620 | { | |
266e8367 | 8621 | if ( !calcOnly ) |
266e8367 | 8622 | AutoSizeColumn(col, setAsMin); |
57c086ef VZ |
8623 | |
8624 | width += GetColWidth(col); | |
65e4e78e | 8625 | } |
97a9929e | 8626 | |
266e8367 | 8627 | return width; |
65e4e78e VZ |
8628 | } |
8629 | ||
266e8367 | 8630 | int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin) |
57c086ef VZ |
8631 | { |
8632 | int height = m_colLabelHeight; | |
8633 | ||
b62f94ff VZ |
8634 | wxGridUpdateLocker locker; |
8635 | if(!calcOnly) | |
8636 | locker.Create(this); | |
97a9929e | 8637 | |
57c086ef VZ |
8638 | for ( int row = 0; row < m_numRows; row++ ) |
8639 | { | |
af547d51 | 8640 | if ( !calcOnly ) |
af547d51 | 8641 | AutoSizeRow(row, setAsMin); |
57c086ef VZ |
8642 | |
8643 | height += GetRowHeight(row); | |
8644 | } | |
97a9929e | 8645 | |
266e8367 | 8646 | return height; |
57c086ef VZ |
8647 | } |
8648 | ||
8649 | void wxGrid::AutoSize() | |
8650 | { | |
b62f94ff | 8651 | wxGridUpdateLocker locker(this); |
97a9929e | 8652 | |
0ed3b812 VZ |
8653 | wxSize size(SetOrCalcColumnSizes(false) - m_rowLabelWidth + m_extraWidth, |
8654 | SetOrCalcRowSizes(false) - m_colLabelHeight + m_extraHeight); | |
97a9929e | 8655 | |
0ed3b812 VZ |
8656 | // we know that we're not going to have scrollbars so disable them now to |
8657 | // avoid trouble in SetClientSize() which can otherwise set the correct | |
8658 | // client size but also leave space for (not needed any more) scrollbars | |
fd76c6a7 VZ |
8659 | SetScrollbars(m_xScrollPixelsPerLine, m_yScrollPixelsPerLine, |
8660 | 0, 0, 0, 0, true); | |
72b0a1de VZ |
8661 | |
8662 | SetClientSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight); | |
266e8367 VZ |
8663 | } |
8664 | ||
d43851f7 JS |
8665 | void wxGrid::AutoSizeRowLabelSize( int row ) |
8666 | { | |
d43851f7 | 8667 | // Hide the edit control, so it |
4db6714b KH |
8668 | // won't interfere with drag-shrinking. |
8669 | if ( IsCellEditControlShown() ) | |
d43851f7 JS |
8670 | { |
8671 | HideCellEditControl(); | |
8672 | SaveEditControlValue(); | |
8673 | } | |
8674 | ||
8675 | // autosize row height depending on label text | |
ad7502d8 | 8676 | SetRowSize(row, -1); |
30496905 | 8677 | |
d43851f7 JS |
8678 | ForceRefresh(); |
8679 | } | |
8680 | ||
8681 | void wxGrid::AutoSizeColLabelSize( int col ) | |
8682 | { | |
d43851f7 | 8683 | // Hide the edit control, so it |
c2f5b920 | 8684 | // won't interfere with drag-shrinking. |
4db6714b | 8685 | if ( IsCellEditControlShown() ) |
d43851f7 JS |
8686 | { |
8687 | HideCellEditControl(); | |
8688 | SaveEditControlValue(); | |
8689 | } | |
8690 | ||
8691 | // autosize column width depending on label text | |
ad7502d8 | 8692 | SetColSize(col, -1); |
30496905 | 8693 | |
d43851f7 JS |
8694 | ForceRefresh(); |
8695 | } | |
8696 | ||
266e8367 VZ |
8697 | wxSize wxGrid::DoGetBestSize() const |
8698 | { | |
f48a1159 | 8699 | wxGrid * const self = const_cast<wxGrid *>(this); |
266e8367 | 8700 | |
dc4689ef VZ |
8701 | // we do the same as in AutoSize() here with the exception that we don't |
8702 | // change the column/row sizes, only calculate them | |
8703 | wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth, | |
8704 | self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight); | |
962a48f6 | 8705 | |
72b0a1de | 8706 | return wxSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight) |
dc4689ef | 8707 | + GetWindowBorderSize(); |
266e8367 VZ |
8708 | } |
8709 | ||
8710 | void wxGrid::Fit() | |
8711 | { | |
8712 | AutoSize(); | |
57c086ef VZ |
8713 | } |
8714 | ||
7bc57fd9 | 8715 | #if WXWIN_COMPATIBILITY_2_8 |
6fc0f38f SN |
8716 | wxPen& wxGrid::GetDividerPen() const |
8717 | { | |
8718 | return wxNullPen; | |
8719 | } | |
7bc57fd9 | 8720 | #endif // WXWIN_COMPATIBILITY_2_8 |
6fc0f38f | 8721 | |
57c086ef VZ |
8722 | // ---------------------------------------------------------------------------- |
8723 | // cell value accessor functions | |
8724 | // ---------------------------------------------------------------------------- | |
f85afd4e MB |
8725 | |
8726 | void wxGrid::SetCellValue( int row, int col, const wxString& s ) | |
8727 | { | |
8728 | if ( m_table ) | |
8729 | { | |
f6bcfd97 | 8730 | m_table->SetValue( row, col, s ); |
2d66e025 MB |
8731 | if ( !GetBatchCount() ) |
8732 | { | |
27f35b66 SN |
8733 | int dummy; |
8734 | wxRect rect( CellToRect( row, col ) ); | |
8735 | rect.x = 0; | |
8736 | rect.width = m_gridWin->GetClientSize().GetWidth(); | |
8737 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
ca65c044 | 8738 | m_gridWin->Refresh( false, &rect ); |
2d66e025 | 8739 | } |
60ff3b99 | 8740 | |
f85afd4e | 8741 | if ( m_currentCellCoords.GetRow() == row && |
4cfa5de6 | 8742 | m_currentCellCoords.GetCol() == col && |
f6bcfd97 BP |
8743 | IsCellEditControlShown()) |
8744 | // Note: If we are using IsCellEditControlEnabled, | |
8745 | // this interacts badly with calling SetCellValue from | |
8746 | // an EVT_GRID_CELL_CHANGE handler. | |
f85afd4e | 8747 | { |
4cfa5de6 RD |
8748 | HideCellEditControl(); |
8749 | ShowCellEditControl(); // will reread data from table | |
f85afd4e | 8750 | } |
f85afd4e MB |
8751 | } |
8752 | } | |
8753 | ||
962a48f6 | 8754 | // ---------------------------------------------------------------------------- |
2f024384 | 8755 | // block, row and column selection |
962a48f6 | 8756 | // ---------------------------------------------------------------------------- |
f85afd4e MB |
8757 | |
8758 | void wxGrid::SelectRow( int row, bool addToSelected ) | |
8759 | { | |
8b5f6d9d VZ |
8760 | if ( !m_selection ) |
8761 | return; | |
8762 | ||
8763 | if ( !addToSelected ) | |
e32352cf | 8764 | ClearSelection(); |
70c7a608 | 8765 | |
8b5f6d9d | 8766 | m_selection->SelectRow(row); |
f85afd4e MB |
8767 | } |
8768 | ||
f85afd4e MB |
8769 | void wxGrid::SelectCol( int col, bool addToSelected ) |
8770 | { | |
8b5f6d9d VZ |
8771 | if ( !m_selection ) |
8772 | return; | |
8773 | ||
8774 | if ( !addToSelected ) | |
e32352cf | 8775 | ClearSelection(); |
f85afd4e | 8776 | |
8b5f6d9d | 8777 | m_selection->SelectCol(col); |
f85afd4e MB |
8778 | } |
8779 | ||
8b5f6d9d VZ |
8780 | void wxGrid::SelectBlock(int topRow, int leftCol, int bottomRow, int rightCol, |
8781 | bool addToSelected) | |
f85afd4e | 8782 | { |
8b5f6d9d VZ |
8783 | if ( !m_selection ) |
8784 | return; | |
8785 | ||
8786 | if ( !addToSelected ) | |
c9097836 | 8787 | ClearSelection(); |
f85afd4e | 8788 | |
8b5f6d9d | 8789 | m_selection->SelectBlock(topRow, leftCol, bottomRow, rightCol); |
f85afd4e MB |
8790 | } |
8791 | ||
8792 | void wxGrid::SelectAll() | |
8793 | { | |
f74d0b57 | 8794 | if ( m_numRows > 0 && m_numCols > 0 ) |
3f3dc2ef VZ |
8795 | { |
8796 | if ( m_selection ) | |
ccdee36f | 8797 | m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 ); |
3f3dc2ef | 8798 | } |
b5808881 | 8799 | } |
f85afd4e | 8800 | |
962a48f6 DS |
8801 | // ---------------------------------------------------------------------------- |
8802 | // cell, row and col deselection | |
8803 | // ---------------------------------------------------------------------------- | |
f7b4b343 | 8804 | |
bec70262 | 8805 | void wxGrid::DeselectLine(int line, const wxGridOperations& oper) |
f7b4b343 | 8806 | { |
3f3dc2ef VZ |
8807 | if ( !m_selection ) |
8808 | return; | |
8809 | ||
bec70262 | 8810 | const wxGridSelectionModes mode = m_selection->GetSelectionMode(); |
475be9ce VZ |
8811 | if ( mode == oper.GetSelectionMode() || |
8812 | mode == wxGrid::wxGridSelectRowsOrColumns ) | |
f7b4b343 | 8813 | { |
bec70262 VZ |
8814 | const wxGridCellCoords c(oper.MakeCoords(line, 0)); |
8815 | if ( m_selection->IsInSelection(c) ) | |
8816 | m_selection->ToggleCellSelection(c); | |
ffdd3c98 | 8817 | } |
bec70262 | 8818 | else if ( mode != oper.Dual().GetSelectionMode() ) |
f7b4b343 | 8819 | { |
bec70262 VZ |
8820 | const int nOther = oper.Dual().GetNumberOfLines(this); |
8821 | for ( int i = 0; i < nOther; i++ ) | |
f7b4b343 | 8822 | { |
bec70262 VZ |
8823 | const wxGridCellCoords c(oper.MakeCoords(line, i)); |
8824 | if ( m_selection->IsInSelection(c) ) | |
8825 | m_selection->ToggleCellSelection(c); | |
f7b4b343 VZ |
8826 | } |
8827 | } | |
bec70262 VZ |
8828 | //else: can only select orthogonal lines so no lines in this direction |
8829 | // could have been selected anyhow | |
f7b4b343 VZ |
8830 | } |
8831 | ||
bec70262 | 8832 | void wxGrid::DeselectRow(int row) |
f7b4b343 | 8833 | { |
bec70262 VZ |
8834 | DeselectLine(row, wxGridRowOperations()); |
8835 | } | |
3f3dc2ef | 8836 | |
bec70262 VZ |
8837 | void wxGrid::DeselectCol(int col) |
8838 | { | |
8839 | DeselectLine(col, wxGridColumnOperations()); | |
f7b4b343 VZ |
8840 | } |
8841 | ||
8842 | void wxGrid::DeselectCell( int row, int col ) | |
8843 | { | |
3f3dc2ef | 8844 | if ( m_selection && m_selection->IsInSelection(row, col) ) |
f7b4b343 VZ |
8845 | m_selection->ToggleCellSelection(row, col); |
8846 | } | |
8847 | ||
ef316e23 | 8848 | bool wxGrid::IsSelection() const |
b5808881 | 8849 | { |
3f3dc2ef | 8850 | return ( m_selection && (m_selection->IsSelection() || |
8a3e536c VZ |
8851 | ( m_selectedBlockTopLeft != wxGridNoCellCoords && |
8852 | m_selectedBlockBottomRight != wxGridNoCellCoords) ) ); | |
f85afd4e MB |
8853 | } |
8854 | ||
84035150 | 8855 | bool wxGrid::IsInSelection( int row, int col ) const |
b5808881 | 8856 | { |
3f3dc2ef | 8857 | return ( m_selection && (m_selection->IsInSelection( row, col ) || |
8a3e536c VZ |
8858 | ( row >= m_selectedBlockTopLeft.GetRow() && |
8859 | col >= m_selectedBlockTopLeft.GetCol() && | |
8860 | row <= m_selectedBlockBottomRight.GetRow() && | |
8861 | col <= m_selectedBlockBottomRight.GetCol() )) ); | |
b5808881 | 8862 | } |
f85afd4e | 8863 | |
aa5b8857 SN |
8864 | wxGridCellCoordsArray wxGrid::GetSelectedCells() const |
8865 | { | |
4db6714b KH |
8866 | if (!m_selection) |
8867 | { | |
8868 | wxGridCellCoordsArray a; | |
8869 | return a; | |
8870 | } | |
8871 | ||
aa5b8857 SN |
8872 | return m_selection->m_cellSelection; |
8873 | } | |
4db6714b | 8874 | |
aa5b8857 SN |
8875 | wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const |
8876 | { | |
4db6714b KH |
8877 | if (!m_selection) |
8878 | { | |
8879 | wxGridCellCoordsArray a; | |
8880 | return a; | |
8881 | } | |
8882 | ||
aa5b8857 SN |
8883 | return m_selection->m_blockSelectionTopLeft; |
8884 | } | |
4db6714b | 8885 | |
aa5b8857 SN |
8886 | wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const |
8887 | { | |
4db6714b KH |
8888 | if (!m_selection) |
8889 | { | |
8890 | wxGridCellCoordsArray a; | |
8891 | return a; | |
8892 | } | |
8893 | ||
a8de8190 | 8894 | return m_selection->m_blockSelectionBottomRight; |
aa5b8857 | 8895 | } |
4db6714b | 8896 | |
aa5b8857 SN |
8897 | wxArrayInt wxGrid::GetSelectedRows() const |
8898 | { | |
4db6714b KH |
8899 | if (!m_selection) |
8900 | { | |
8901 | wxArrayInt a; | |
8902 | return a; | |
8903 | } | |
8904 | ||
aa5b8857 SN |
8905 | return m_selection->m_rowSelection; |
8906 | } | |
4db6714b | 8907 | |
aa5b8857 SN |
8908 | wxArrayInt wxGrid::GetSelectedCols() const |
8909 | { | |
4db6714b KH |
8910 | if (!m_selection) |
8911 | { | |
8912 | wxArrayInt a; | |
8913 | return a; | |
8914 | } | |
8915 | ||
aa5b8857 SN |
8916 | return m_selection->m_colSelection; |
8917 | } | |
8918 | ||
f85afd4e MB |
8919 | void wxGrid::ClearSelection() |
8920 | { | |
8a3e536c VZ |
8921 | wxRect r1 = BlockToDeviceRect(m_selectedBlockTopLeft, |
8922 | m_selectedBlockBottomRight); | |
8923 | wxRect r2 = BlockToDeviceRect(m_currentCellCoords, | |
8924 | m_selectedBlockCorner); | |
8925 | ||
8926 | m_selectedBlockTopLeft = | |
8927 | m_selectedBlockBottomRight = | |
8928 | m_selectedBlockCorner = wxGridNoCellCoords; | |
8929 | ||
efc49a21 VZ |
8930 | if ( !r1.IsEmpty() ) |
8931 | RefreshRect(r1, false); | |
8932 | if ( !r2.IsEmpty() ) | |
8933 | RefreshRect(r2, false); | |
8a3e536c | 8934 | |
3f3dc2ef VZ |
8935 | if ( m_selection ) |
8936 | m_selection->ClearSelection(); | |
8f177c8e | 8937 | } |
f85afd4e | 8938 | |
da6af900 | 8939 | // This function returns the rectangle that encloses the given block |
2d66e025 MB |
8940 | // in device coords clipped to the client size of the grid window. |
8941 | // | |
731330ec VZ |
8942 | wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords& topLeft, |
8943 | const wxGridCellCoords& bottomRight ) const | |
f85afd4e | 8944 | { |
731330ec VZ |
8945 | wxRect resultRect; |
8946 | wxRect tempCellRect = CellToRect(topLeft); | |
8947 | if ( tempCellRect != wxGridNoCellRect ) | |
f85afd4e | 8948 | { |
731330ec | 8949 | resultRect = tempCellRect; |
58dd5b3b MB |
8950 | } |
8951 | else | |
8952 | { | |
731330ec | 8953 | resultRect = wxRect(0, 0, 0, 0); |
58dd5b3b | 8954 | } |
60ff3b99 | 8955 | |
731330ec VZ |
8956 | tempCellRect = CellToRect(bottomRight); |
8957 | if ( tempCellRect != wxGridNoCellRect ) | |
58dd5b3b | 8958 | { |
731330ec | 8959 | resultRect += tempCellRect; |
2d66e025 MB |
8960 | } |
8961 | else | |
8962 | { | |
731330ec | 8963 | // If both inputs were "wxGridNoCellRect," then there's nothing to do. |
2d66e025 | 8964 | return wxGridNoCellRect; |
f85afd4e MB |
8965 | } |
8966 | ||
731330ec VZ |
8967 | // Ensure that left/right and top/bottom pairs are in order. |
8968 | int left = resultRect.GetLeft(); | |
8969 | int top = resultRect.GetTop(); | |
8970 | int right = resultRect.GetRight(); | |
8971 | int bottom = resultRect.GetBottom(); | |
27f35b66 SN |
8972 | |
8973 | int leftCol = topLeft.GetCol(); | |
8974 | int topRow = topLeft.GetRow(); | |
8975 | int rightCol = bottomRight.GetCol(); | |
8976 | int bottomRow = bottomRight.GetRow(); | |
8977 | ||
3ed884a0 SN |
8978 | if (left > right) |
8979 | { | |
731330ec | 8980 | int tmp = left; |
3ed884a0 | 8981 | left = right; |
731330ec VZ |
8982 | right = tmp; |
8983 | ||
8984 | tmp = leftCol; | |
4db6714b | 8985 | leftCol = rightCol; |
731330ec | 8986 | rightCol = tmp; |
3ed884a0 SN |
8987 | } |
8988 | ||
8989 | if (top > bottom) | |
8990 | { | |
731330ec | 8991 | int tmp = top; |
3ed884a0 | 8992 | top = bottom; |
731330ec VZ |
8993 | bottom = tmp; |
8994 | ||
8995 | tmp = topRow; | |
3ed884a0 | 8996 | topRow = bottomRow; |
731330ec | 8997 | bottomRow = tmp; |
3ed884a0 SN |
8998 | } |
8999 | ||
731330ec VZ |
9000 | // The following loop is ONLY necessary to detect and handle merged cells. |
9001 | int cw, ch; | |
9002 | m_gridWin->GetClientSize( &cw, &ch ); | |
9003 | ||
9004 | // Get the origin coordinates: notice that they will be negative if the | |
9005 | // grid is scrolled downwards/to the right. | |
9006 | int gridOriginX = 0; | |
9007 | int gridOriginY = 0; | |
9008 | CalcScrolledPosition(gridOriginX, gridOriginY, &gridOriginX, &gridOriginY); | |
9009 | ||
9010 | int onScreenLeftmostCol = internalXToCol(-gridOriginX); | |
9011 | int onScreenUppermostRow = internalYToRow(-gridOriginY); | |
9012 | ||
9013 | int onScreenRightmostCol = internalXToCol(-gridOriginX + cw); | |
9014 | int onScreenBottommostRow = internalYToRow(-gridOriginY + ch); | |
9015 | ||
9016 | // Bound our loop so that we only examine the portion of the selected block | |
9017 | // that is shown on screen. Therefore, we compare the Top-Left block values | |
9018 | // to the Top-Left screen values, and the Bottom-Right block values to the | |
9019 | // Bottom-Right screen values, choosing appropriately. | |
9020 | const int visibleTopRow = wxMax(topRow, onScreenUppermostRow); | |
9021 | const int visibleBottomRow = wxMin(bottomRow, onScreenBottommostRow); | |
9022 | const int visibleLeftCol = wxMax(leftCol, onScreenLeftmostCol); | |
9023 | const int visibleRightCol = wxMin(rightCol, onScreenRightmostCol); | |
9024 | ||
9025 | for ( int j = visibleTopRow; j <= visibleBottomRow; j++ ) | |
27f35b66 | 9026 | { |
731330ec | 9027 | for ( int i = visibleLeftCol; i <= visibleRightCol; i++ ) |
27f35b66 | 9028 | { |
731330ec VZ |
9029 | if ( (j == visibleTopRow) || (j == visibleBottomRow) || |
9030 | (i == visibleLeftCol) || (i == visibleRightCol) ) | |
27f35b66 | 9031 | { |
731330ec | 9032 | tempCellRect = CellToRect( j, i ); |
27f35b66 | 9033 | |
731330ec VZ |
9034 | if (tempCellRect.x < left) |
9035 | left = tempCellRect.x; | |
9036 | if (tempCellRect.y < top) | |
9037 | top = tempCellRect.y; | |
9038 | if (tempCellRect.x + tempCellRect.width > right) | |
9039 | right = tempCellRect.x + tempCellRect.width; | |
9040 | if (tempCellRect.y + tempCellRect.height > bottom) | |
9041 | bottom = tempCellRect.y + tempCellRect.height; | |
27f35b66 | 9042 | } |
4db6714b KH |
9043 | else |
9044 | { | |
731330ec | 9045 | i = visibleRightCol; // jump over inner cells. |
4db6714b | 9046 | } |
27f35b66 SN |
9047 | } |
9048 | } | |
9049 | ||
731330ec | 9050 | // Convert to scrolled coords |
27f35b66 SN |
9051 | CalcScrolledPosition( left, top, &left, &top ); |
9052 | CalcScrolledPosition( right, bottom, &right, &bottom ); | |
58dd5b3b | 9053 | |
f6bcfd97 | 9054 | if (right < 0 || bottom < 0 || left > cw || top > ch) |
c47addef | 9055 | return wxRect(0,0,0,0); |
f6bcfd97 | 9056 | |
731330ec VZ |
9057 | resultRect.SetLeft( wxMax(0, left) ); |
9058 | resultRect.SetTop( wxMax(0, top) ); | |
9059 | resultRect.SetRight( wxMin(cw, right) ); | |
9060 | resultRect.SetBottom( wxMin(ch, bottom) ); | |
58dd5b3b | 9061 | |
731330ec | 9062 | return resultRect; |
f85afd4e MB |
9063 | } |
9064 | ||
574e1c5a VZ |
9065 | void wxGrid::DoSetSizes(const wxGridSizesInfo& sizeInfo, |
9066 | const wxGridOperations& oper) | |
9067 | { | |
9068 | BeginBatch(); | |
9069 | oper.SetDefaultLineSize(this, sizeInfo.m_sizeDefault, true); | |
9070 | const int numLines = oper.GetNumberOfLines(this); | |
9071 | for ( int i = 0; i < numLines; i++ ) | |
9072 | { | |
9073 | int size = sizeInfo.GetSize(i); | |
9074 | if ( size != sizeInfo.m_sizeDefault) | |
9075 | oper.SetLineSize(this, i, size); | |
9076 | } | |
9077 | EndBatch(); | |
9078 | } | |
9079 | ||
9080 | void wxGrid::SetColSizes(const wxGridSizesInfo& sizeInfo) | |
9081 | { | |
9082 | DoSetSizes(sizeInfo, wxGridColumnOperations()); | |
9083 | } | |
9084 | ||
9085 | void wxGrid::SetRowSizes(const wxGridSizesInfo& sizeInfo) | |
9086 | { | |
9087 | DoSetSizes(sizeInfo, wxGridRowOperations()); | |
9088 | } | |
9089 | ||
9090 | wxGridSizesInfo::wxGridSizesInfo(int defSize, const wxArrayInt& allSizes) | |
9091 | { | |
9092 | m_sizeDefault = defSize; | |
9093 | for ( size_t i = 0; i < allSizes.size(); i++ ) | |
9094 | { | |
9095 | if ( allSizes[i] != defSize ) | |
9096 | m_customSizes[i] = allSizes[i]; | |
9097 | } | |
9098 | } | |
9099 | ||
9100 | int wxGridSizesInfo::GetSize(unsigned pos) const | |
9101 | { | |
9102 | wxUnsignedToIntHashMap::const_iterator it = m_customSizes.find(pos); | |
9103 | ||
30496905 VZ |
9104 | // if it's not found return the default |
9105 | if ( it == m_customSizes.end() ) | |
9106 | return m_sizeDefault; | |
9107 | ||
9108 | // otherwise return 0 if it's hidden, currently there is no way to get | |
9109 | // its size before it had been hidden | |
9110 | if ( it->second < 0 ) | |
9111 | return 0; | |
9112 | ||
9113 | return it->second; | |
574e1c5a VZ |
9114 | } |
9115 | ||
1edce33f VZ |
9116 | // ---------------------------------------------------------------------------- |
9117 | // drop target | |
9118 | // ---------------------------------------------------------------------------- | |
9119 | ||
9120 | #if wxUSE_DRAG_AND_DROP | |
9121 | ||
9122 | // this allow setting drop target directly on wxGrid | |
9123 | void wxGrid::SetDropTarget(wxDropTarget *dropTarget) | |
9124 | { | |
9125 | GetGridWindow()->SetDropTarget(dropTarget); | |
9126 | } | |
9127 | ||
9128 | #endif // wxUSE_DRAG_AND_DROP | |
9129 | ||
962a48f6 DS |
9130 | // ---------------------------------------------------------------------------- |
9131 | // grid event classes | |
9132 | // ---------------------------------------------------------------------------- | |
f85afd4e | 9133 | |
bf7945ce | 9134 | IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent ) |
f85afd4e MB |
9135 | |
9136 | wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, | |
5c8fc7c1 | 9137 | int row, int col, int x, int y, bool sel, |
f85afd4e | 9138 | bool control, bool shift, bool alt, bool meta ) |
8b5f6d9d VZ |
9139 | : wxNotifyEvent( type, id ), |
9140 | wxKeyboardState(control, shift, alt, meta) | |
f85afd4e | 9141 | { |
8b5f6d9d | 9142 | Init(row, col, x, y, sel); |
8f177c8e | 9143 | |
f85afd4e MB |
9144 | SetEventObject(obj); |
9145 | } | |
9146 | ||
bf7945ce | 9147 | IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent ) |
f85afd4e MB |
9148 | |
9149 | wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj, | |
9150 | int rowOrCol, int x, int y, | |
9151 | bool control, bool shift, bool alt, bool meta ) | |
8b5f6d9d VZ |
9152 | : wxNotifyEvent( type, id ), |
9153 | wxKeyboardState(control, shift, alt, meta) | |
f85afd4e | 9154 | { |
8b5f6d9d | 9155 | Init(rowOrCol, x, y); |
8f177c8e | 9156 | |
f85afd4e MB |
9157 | SetEventObject(obj); |
9158 | } | |
9159 | ||
9160 | ||
bf7945ce | 9161 | IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent ) |
f85afd4e MB |
9162 | |
9163 | wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, | |
8f177c8e VZ |
9164 | const wxGridCellCoords& topLeft, |
9165 | const wxGridCellCoords& bottomRight, | |
5c8fc7c1 SN |
9166 | bool sel, bool control, |
9167 | bool shift, bool alt, bool meta ) | |
8b5f6d9d VZ |
9168 | : wxNotifyEvent( type, id ), |
9169 | wxKeyboardState(control, shift, alt, meta) | |
9170 | { | |
9171 | Init(topLeft, bottomRight, sel); | |
f85afd4e MB |
9172 | |
9173 | SetEventObject(obj); | |
9174 | } | |
9175 | ||
9176 | ||
bf7945ce RD |
9177 | IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent) |
9178 | ||
9179 | wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type, | |
9180 | wxObject* obj, int row, | |
9181 | int col, wxControl* ctrl) | |
9182 | : wxCommandEvent(type, id) | |
9183 | { | |
9184 | SetEventObject(obj); | |
9185 | m_row = row; | |
9186 | m_col = col; | |
9187 | m_ctrl = ctrl; | |
9188 | } | |
9189 | ||
29efc6e4 FM |
9190 | |
9191 | // ---------------------------------------------------------------------------- | |
9192 | // wxGridTypeRegistry | |
9193 | // ---------------------------------------------------------------------------- | |
9194 | ||
9195 | wxGridTypeRegistry::~wxGridTypeRegistry() | |
9196 | { | |
9197 | size_t count = m_typeinfo.GetCount(); | |
9198 | for ( size_t i = 0; i < count; i++ ) | |
9199 | delete m_typeinfo[i]; | |
9200 | } | |
9201 | ||
9202 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, | |
9203 | wxGridCellRenderer* renderer, | |
9204 | wxGridCellEditor* editor) | |
9205 | { | |
9206 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); | |
9207 | ||
9208 | // is it already registered? | |
9209 | int loc = FindRegisteredDataType(typeName); | |
9210 | if ( loc != wxNOT_FOUND ) | |
9211 | { | |
9212 | delete m_typeinfo[loc]; | |
9213 | m_typeinfo[loc] = info; | |
9214 | } | |
9215 | else | |
9216 | { | |
9217 | m_typeinfo.Add(info); | |
9218 | } | |
9219 | } | |
9220 | ||
9221 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) | |
9222 | { | |
9223 | size_t count = m_typeinfo.GetCount(); | |
9224 | for ( size_t i = 0; i < count; i++ ) | |
9225 | { | |
9226 | if ( typeName == m_typeinfo[i]->m_typeName ) | |
9227 | { | |
9228 | return i; | |
9229 | } | |
9230 | } | |
9231 | ||
9232 | return wxNOT_FOUND; | |
9233 | } | |
9234 | ||
9235 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) | |
9236 | { | |
9237 | int index = FindRegisteredDataType(typeName); | |
9238 | if ( index == wxNOT_FOUND ) | |
9239 | { | |
9240 | // check whether this is one of the standard ones, in which case | |
9241 | // register it "on the fly" | |
9242 | #if wxUSE_TEXTCTRL | |
9243 | if ( typeName == wxGRID_VALUE_STRING ) | |
9244 | { | |
9245 | RegisterDataType(wxGRID_VALUE_STRING, | |
9246 | new wxGridCellStringRenderer, | |
9247 | new wxGridCellTextEditor); | |
9248 | } | |
9249 | else | |
9250 | #endif // wxUSE_TEXTCTRL | |
9251 | #if wxUSE_CHECKBOX | |
9252 | if ( typeName == wxGRID_VALUE_BOOL ) | |
9253 | { | |
9254 | RegisterDataType(wxGRID_VALUE_BOOL, | |
9255 | new wxGridCellBoolRenderer, | |
9256 | new wxGridCellBoolEditor); | |
9257 | } | |
9258 | else | |
9259 | #endif // wxUSE_CHECKBOX | |
9260 | #if wxUSE_TEXTCTRL | |
9261 | if ( typeName == wxGRID_VALUE_NUMBER ) | |
9262 | { | |
9263 | RegisterDataType(wxGRID_VALUE_NUMBER, | |
9264 | new wxGridCellNumberRenderer, | |
9265 | new wxGridCellNumberEditor); | |
9266 | } | |
9267 | else if ( typeName == wxGRID_VALUE_FLOAT ) | |
9268 | { | |
9269 | RegisterDataType(wxGRID_VALUE_FLOAT, | |
9270 | new wxGridCellFloatRenderer, | |
9271 | new wxGridCellFloatEditor); | |
9272 | } | |
9273 | else | |
9274 | #endif // wxUSE_TEXTCTRL | |
9275 | #if wxUSE_COMBOBOX | |
9276 | if ( typeName == wxGRID_VALUE_CHOICE ) | |
9277 | { | |
9278 | RegisterDataType(wxGRID_VALUE_CHOICE, | |
9279 | new wxGridCellStringRenderer, | |
9280 | new wxGridCellChoiceEditor); | |
9281 | } | |
9282 | else | |
9283 | #endif // wxUSE_COMBOBOX | |
9284 | { | |
9285 | return wxNOT_FOUND; | |
9286 | } | |
9287 | ||
9288 | // we get here only if just added the entry for this type, so return | |
9289 | // the last index | |
9290 | index = m_typeinfo.GetCount() - 1; | |
9291 | } | |
9292 | ||
9293 | return index; | |
9294 | } | |
9295 | ||
9296 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) | |
9297 | { | |
9298 | int index = FindDataType(typeName); | |
9299 | if ( index == wxNOT_FOUND ) | |
9300 | { | |
9301 | // the first part of the typename is the "real" type, anything after ':' | |
9302 | // are the parameters for the renderer | |
9a83f860 | 9303 | index = FindDataType(typeName.BeforeFirst(wxT(':'))); |
29efc6e4 FM |
9304 | if ( index == wxNOT_FOUND ) |
9305 | { | |
9306 | return wxNOT_FOUND; | |
9307 | } | |
9308 | ||
9309 | wxGridCellRenderer *renderer = GetRenderer(index); | |
9310 | wxGridCellRenderer *rendererOld = renderer; | |
9311 | renderer = renderer->Clone(); | |
9312 | rendererOld->DecRef(); | |
9313 | ||
9314 | wxGridCellEditor *editor = GetEditor(index); | |
9315 | wxGridCellEditor *editorOld = editor; | |
9316 | editor = editor->Clone(); | |
9317 | editorOld->DecRef(); | |
9318 | ||
9319 | // do it even if there are no parameters to reset them to defaults | |
9a83f860 | 9320 | wxString params = typeName.AfterFirst(wxT(':')); |
29efc6e4 FM |
9321 | renderer->SetParameters(params); |
9322 | editor->SetParameters(params); | |
9323 | ||
9324 | // register the new typename | |
9325 | RegisterDataType(typeName, renderer, editor); | |
9326 | ||
9327 | // we just registered it, it's the last one | |
9328 | index = m_typeinfo.GetCount() - 1; | |
9329 | } | |
9330 | ||
9331 | return index; | |
9332 | } | |
9333 | ||
9334 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) | |
9335 | { | |
9336 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; | |
9337 | if (renderer) | |
9338 | renderer->IncRef(); | |
9339 | ||
9340 | return renderer; | |
9341 | } | |
9342 | ||
9343 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) | |
9344 | { | |
9345 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; | |
9346 | if (editor) | |
9347 | editor->IncRef(); | |
9348 | ||
9349 | return editor; | |
9350 | } | |
9351 | ||
27b92ca4 | 9352 | #endif // wxUSE_GRID |