]>
Commit | Line | Data |
---|---|---|
2796cce3 | 1 | /////////////////////////////////////////////////////////////////////////// |
faa94f3e | 2 | // Name: src/generic/grid.cpp |
f85afd4e MB |
3 | // Purpose: wxGrid and related classes |
4 | // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn) | |
d4175745 | 5 | // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios |
f85afd4e MB |
6 | // Created: 1/08/1999 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au) | |
65571936 | 9 | // Licence: wxWindows licence |
f85afd4e MB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
bec70262 VZ |
12 | /* |
13 | TODO: | |
14 | ||
15 | - Replace use of wxINVERT with wxOverlay | |
16 | - Make Begin/EndBatch() the same as the generic Freeze/Thaw() | |
10a4531d VZ |
17 | - Review the column reordering code, it's a mess. |
18 | - Implement row reordering after dealing with the columns. | |
bec70262 VZ |
19 | */ |
20 | ||
95427194 | 21 | // For compilers that support precompilation, includes "wx/wx.h". |
4d85bcd1 JS |
22 | #include "wx/wxprec.h" |
23 | ||
f85afd4e MB |
24 | #ifdef __BORLANDC__ |
25 | #pragma hdrstop | |
26 | #endif | |
27 | ||
27b92ca4 VZ |
28 | #if wxUSE_GRID |
29 | ||
4c44eb66 PC |
30 | #include "wx/grid.h" |
31 | ||
f85afd4e MB |
32 | #ifndef WX_PRECOMP |
33 | #include "wx/utils.h" | |
34 | #include "wx/dcclient.h" | |
35 | #include "wx/settings.h" | |
36 | #include "wx/log.h" | |
508011ce VZ |
37 | #include "wx/textctrl.h" |
38 | #include "wx/checkbox.h" | |
4ee5fc9c | 39 | #include "wx/combobox.h" |
816be743 | 40 | #include "wx/valtext.h" |
60d876f3 | 41 | #include "wx/intl.h" |
c77a6796 | 42 | #include "wx/math.h" |
2a673eb1 | 43 | #include "wx/listbox.h" |
f85afd4e MB |
44 | #endif |
45 | ||
cb5df486 | 46 | #include "wx/textfile.h" |
816be743 | 47 | #include "wx/spinctrl.h" |
c4608a8a | 48 | #include "wx/tokenzr.h" |
4d1bc39c | 49 | #include "wx/renderer.h" |
ad805b9e | 50 | #include "wx/headerctrl.h" |
82edfbe7 | 51 | #include "wx/hashset.h" |
6d004f67 | 52 | |
b5808881 | 53 | #include "wx/generic/gridsel.h" |
29efc6e4 FM |
54 | #include "wx/generic/gridctrl.h" |
55 | #include "wx/generic/grideditors.h" | |
56 | #include "wx/generic/private/grid.h" | |
07296f0b | 57 | |
23318a53 | 58 | const char wxGridNameStr[] = "grid"; |
4c44eb66 | 59 | |
0b7e6e7d SN |
60 | #if defined(__WXMOTIF__) |
61 | #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier) | |
c78b3acd | 62 | #else |
0b7e6e7d | 63 | #define WXUNUSED_MOTIF(identifier) identifier |
c78b3acd SN |
64 | #endif |
65 | ||
66 | #if defined(__WXGTK__) | |
67 | #define WXUNUSED_GTK(identifier) WXUNUSED(identifier) | |
68 | #else | |
69 | #define WXUNUSED_GTK(identifier) identifier | |
70 | #endif | |
71 | ||
3f8e5072 JS |
72 | // Required for wxIs... functions |
73 | #include <ctype.h> | |
74 | ||
7a7fa93b | 75 | WX_DECLARE_HASH_SET_WITH_DECL_PTR(int, wxIntegerHash, wxIntegerEqual, |
e4c8592e | 76 | wxGridFixedIndicesSet, class WXDLLIMPEXP_ADV); |
82edfbe7 | 77 | |
29efc6e4 | 78 | |
b99be8fb | 79 | // ---------------------------------------------------------------------------- |
29efc6e4 | 80 | // globals |
b99be8fb VZ |
81 | // ---------------------------------------------------------------------------- |
82 | ||
670d0177 VZ |
83 | namespace |
84 | { | |
85 | ||
29efc6e4 FM |
86 | //#define DEBUG_ATTR_CACHE |
87 | #ifdef DEBUG_ATTR_CACHE | |
88 | static size_t gs_nAttrCacheHits = 0; | |
89 | static size_t gs_nAttrCacheMisses = 0; | |
90 | #endif | |
758cbedf | 91 | |
670d0177 VZ |
92 | // this struct simply combines together the default header renderers |
93 | // | |
94 | // as the renderers ctors are trivial, there is no problem with making them | |
95 | // globals | |
96 | struct DefaultHeaderRenderers | |
97 | { | |
98 | wxGridColumnHeaderRendererDefault colRenderer; | |
99 | wxGridRowHeaderRendererDefault rowRenderer; | |
100 | wxGridCornerHeaderRendererDefault cornerRenderer; | |
101 | } gs_defaultHeaderRenderers; | |
102 | ||
103 | } // anonymous namespace | |
104 | ||
29efc6e4 FM |
105 | // ---------------------------------------------------------------------------- |
106 | // constants | |
107 | // ---------------------------------------------------------------------------- | |
6f292345 | 108 | |
29efc6e4 FM |
109 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); |
110 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); | |
6f292345 | 111 | |
29efc6e4 FM |
112 | namespace |
113 | { | |
b99be8fb | 114 | |
29efc6e4 FM |
115 | // scroll line size |
116 | const size_t GRID_SCROLL_LINE_X = 15; | |
117 | const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X; | |
96ca74cd | 118 | |
29efc6e4 FM |
119 | // the size of hash tables used a bit everywhere (the max number of elements |
120 | // in these hash tables is the number of rows/columns) | |
121 | const int GRID_HASH_SIZE = 100; | |
2e9a6788 | 122 | |
29efc6e4 FM |
123 | // the minimal distance in pixels the mouse needs to move to start a drag |
124 | // operation | |
125 | const int DRAG_SENSITIVITY = 3; | |
b99be8fb | 126 | |
29efc6e4 | 127 | } // anonymous namespace |
b99be8fb VZ |
128 | |
129 | #include "wx/arrimpl.cpp" | |
130 | ||
131 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) | |
132 | WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray) | |
133 | ||
0f442030 RR |
134 | // ---------------------------------------------------------------------------- |
135 | // events | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
9b11752c VZ |
138 | wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK, wxGridEvent ); |
139 | wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK, wxGridEvent ); | |
140 | wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK, wxGridEvent ); | |
141 | wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK, wxGridEvent ); | |
142 | wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG, wxGridEvent ); | |
143 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK, wxGridEvent ); | |
144 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK, wxGridEvent ); | |
145 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK, wxGridEvent ); | |
146 | wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK, wxGridEvent ); | |
147 | wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE, wxGridSizeEvent ); | |
148 | wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE, wxGridSizeEvent ); | |
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 ) |
f85afd4e | 2161 | END_EVENT_TABLE() |
8f177c8e | 2162 | |
b0a877ec SC |
2163 | bool wxGrid::Create(wxWindow *parent, wxWindowID id, |
2164 | const wxPoint& pos, const wxSize& size, | |
2165 | long style, const wxString& name) | |
2166 | { | |
2167 | if (!wxScrolledWindow::Create(parent, id, pos, size, | |
c2f5b920 | 2168 | style | wxWANTS_CHARS, name)) |
ca65c044 | 2169 | return false; |
b0a877ec | 2170 | |
2f024384 DS |
2171 | m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE); |
2172 | m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE); | |
b0a877ec | 2173 | |
2f024384 | 2174 | Create(); |
170acdc9 | 2175 | SetInitialSize(size); |
b93aafab | 2176 | CalcDimensions(); |
b0a877ec | 2177 | |
ca65c044 | 2178 | return true; |
b0a877ec SC |
2179 | } |
2180 | ||
58dd5b3b MB |
2181 | wxGrid::~wxGrid() |
2182 | { | |
e882d288 VZ |
2183 | if ( m_winCapture ) |
2184 | m_winCapture->ReleaseMouse(); | |
2185 | ||
b09b3048 VZ |
2186 | // Ensure that the editor control is destroyed before the grid is, |
2187 | // otherwise we crash later when the editor tries to do something with the | |
2188 | // half destroyed grid | |
2189 | HideCellEditControl(); | |
2190 | ||
606b005f JS |
2191 | // Must do this or ~wxScrollHelper will pop the wrong event handler |
2192 | SetTargetWindow(this); | |
0a976765 | 2193 | ClearAttrCache(); |
39bcce60 | 2194 | wxSafeDecRef(m_defaultCellAttr); |
0a976765 VZ |
2195 | |
2196 | #ifdef DEBUG_ATTR_CACHE | |
2197 | size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses; | |
9a83f860 | 2198 | wxPrintf(wxT("wxGrid attribute cache statistics: " |
0a976765 VZ |
2199 | "total: %u, hits: %u (%u%%)\n"), |
2200 | total, gs_nAttrCacheHits, | |
2201 | total ? (gs_nAttrCacheHits*100) / total : 0); | |
2202 | #endif | |
2203 | ||
86020f7e VZ |
2204 | // if we own the table, just delete it, otherwise at least don't leave it |
2205 | // with dangling view pointer | |
2206 | if ( m_ownTable ) | |
2796cce3 | 2207 | delete m_table; |
ecc7aa82 | 2208 | else if ( m_table && m_table->GetView() == this ) |
86020f7e | 2209 | m_table->SetView(NULL); |
f2d76237 RD |
2210 | |
2211 | delete m_typeRegistry; | |
b5808881 | 2212 | delete m_selection; |
82edfbe7 VZ |
2213 | |
2214 | delete m_setFixedRows; | |
2215 | delete m_setFixedCols; | |
58dd5b3b MB |
2216 | } |
2217 | ||
58dd5b3b MB |
2218 | // |
2219 | // ----- internal init and update functions | |
2220 | // | |
2221 | ||
9950649c RD |
2222 | // NOTE: If using the default visual attributes works everywhere then this can |
2223 | // be removed as well as the #else cases below. | |
2224 | #define _USE_VISATTR 0 | |
2225 | ||
58dd5b3b | 2226 | void wxGrid::Create() |
f0102d2a | 2227 | { |
3d59537f DS |
2228 | // create the type registry |
2229 | m_typeRegistry = new wxGridTypeRegistry; | |
2c9a89e0 | 2230 | |
ca65c044 | 2231 | m_cellEditCtrlEnabled = false; |
4634a5d6 | 2232 | |
ccd970b1 | 2233 | m_defaultCellAttr = new wxGridCellAttr(); |
f2d76237 RD |
2234 | |
2235 | // Set default cell attributes | |
ccd970b1 | 2236 | m_defaultCellAttr->SetDefAttr(m_defaultCellAttr); |
19d7140e | 2237 | m_defaultCellAttr->SetKind(wxGridCellAttr::Default); |
f2d76237 | 2238 | m_defaultCellAttr->SetFont(GetFont()); |
4c7277db | 2239 | m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP); |
9950649c RD |
2240 | m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer); |
2241 | m_defaultCellAttr->SetEditor(new wxGridCellTextEditor); | |
2242 | ||
2243 | #if _USE_VISATTR | |
2244 | wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes(); | |
2245 | wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes(); | |
2246 | ||
2247 | m_defaultCellAttr->SetTextColour(gva.colFg); | |
2248 | m_defaultCellAttr->SetBackgroundColour(gva.colBg); | |
ca65c044 | 2249 | |
9950649c | 2250 | #else |
f2d76237 | 2251 | m_defaultCellAttr->SetTextColour( |
a756f210 | 2252 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); |
f2d76237 | 2253 | m_defaultCellAttr->SetBackgroundColour( |
a756f210 | 2254 | wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
9950649c | 2255 | #endif |
2796cce3 | 2256 | |
4634a5d6 MB |
2257 | m_numRows = 0; |
2258 | m_numCols = 0; | |
2259 | m_currentCellCoords = wxGridNoCellCoords; | |
b99be8fb | 2260 | |
f2d76237 | 2261 | // subwindow components that make up the wxGrid |
ad805b9e VZ |
2262 | m_rowLabelWin = new wxGridRowLabelWindow(this); |
2263 | CreateColumnWindow(); | |
2264 | m_cornerLabelWin = new wxGridCornerLabelWindow(this); | |
2265 | m_gridWin = new wxGridWindow( this ); | |
2d66e025 MB |
2266 | |
2267 | SetTargetWindow( m_gridWin ); | |
6f36917b | 2268 | |
9950649c RD |
2269 | #if _USE_VISATTR |
2270 | wxColour gfg = gva.colFg; | |
2271 | wxColour gbg = gva.colBg; | |
2272 | wxColour lfg = lva.colFg; | |
2273 | wxColour lbg = lva.colBg; | |
2274 | #else | |
2275 | wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
2276 | wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ); | |
2277 | wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
2278 | wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ); | |
2279 | #endif | |
2f024384 | 2280 | |
fa47d7a7 VS |
2281 | m_cornerLabelWin->SetOwnForegroundColour(lfg); |
2282 | m_cornerLabelWin->SetOwnBackgroundColour(lbg); | |
2283 | m_rowLabelWin->SetOwnForegroundColour(lfg); | |
2284 | m_rowLabelWin->SetOwnBackgroundColour(lbg); | |
ad805b9e VZ |
2285 | m_colWindow->SetOwnForegroundColour(lfg); |
2286 | m_colWindow->SetOwnBackgroundColour(lbg); | |
fa47d7a7 VS |
2287 | |
2288 | m_gridWin->SetOwnForegroundColour(gfg); | |
2289 | m_gridWin->SetOwnBackgroundColour(gbg); | |
ca65c044 | 2290 | |
ad805b9e VZ |
2291 | m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour(); |
2292 | m_labelTextColour = m_rowLabelWin->GetForegroundColour(); | |
2293 | ||
2294 | // now that we have the grid window, use its font to compute the default | |
2295 | // row height | |
2296 | m_defaultRowHeight = m_gridWin->GetCharHeight(); | |
2297 | #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl() | |
2298 | m_defaultRowHeight += 8; | |
2299 | #else | |
2300 | m_defaultRowHeight += 4; | |
2301 | #endif | |
2302 | ||
2303 | } | |
2304 | ||
2305 | void wxGrid::CreateColumnWindow() | |
2306 | { | |
2307 | if ( m_useNativeHeader ) | |
2308 | { | |
2309 | m_colWindow = new wxGridHeaderCtrl(this); | |
2310 | m_colLabelHeight = m_colWindow->GetBestSize().y; | |
2311 | } | |
2312 | else // draw labels ourselves | |
2313 | { | |
2314 | m_colWindow = new wxGridColLabelWindow(this); | |
2315 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
2316 | } | |
2d66e025 | 2317 | } |
f85afd4e | 2318 | |
b5808881 | 2319 | bool wxGrid::CreateGrid( int numRows, int numCols, |
6b992f7d | 2320 | wxGridSelectionModes selmode ) |
2d66e025 | 2321 | { |
f6bcfd97 | 2322 | wxCHECK_MSG( !m_created, |
ca65c044 | 2323 | false, |
f6bcfd97 BP |
2324 | wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); |
2325 | ||
6b992f7d | 2326 | return SetTable(new wxGridStringTable(numRows, numCols), true, selmode); |
2796cce3 RD |
2327 | } |
2328 | ||
6b992f7d | 2329 | void wxGrid::SetSelectionMode(wxGridSelectionModes selmode) |
f1567cdd | 2330 | { |
6f36917b VZ |
2331 | wxCHECK_RET( m_created, |
2332 | wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") ); | |
2333 | ||
2334 | m_selection->SetSelectionMode( selmode ); | |
f1567cdd SN |
2335 | } |
2336 | ||
aa5b8857 SN |
2337 | wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const |
2338 | { | |
6b992f7d | 2339 | wxCHECK_MSG( m_created, wxGridSelectCells, |
aa5b8857 SN |
2340 | wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") ); |
2341 | ||
2342 | return m_selection->GetSelectionMode(); | |
2343 | } | |
2344 | ||
6b992f7d VZ |
2345 | bool |
2346 | wxGrid::SetTable(wxGridTableBase *table, | |
2347 | bool takeOwnership, | |
2348 | wxGrid::wxGridSelectionModes selmode ) | |
2796cce3 | 2349 | { |
a7dde52f | 2350 | bool checkSelection = false; |
2796cce3 RD |
2351 | if ( m_created ) |
2352 | { | |
3e13956a | 2353 | // stop all processing |
ca65c044 | 2354 | m_created = false; |
86c7378f | 2355 | |
c71b2126 | 2356 | if (m_table) |
86c7378f | 2357 | { |
a7dde52f SN |
2358 | m_table->SetView(0); |
2359 | if( m_ownTable ) | |
2360 | delete m_table; | |
5b061713 | 2361 | m_table = NULL; |
86c7378f | 2362 | } |
2f024384 | 2363 | |
5276b0a5 | 2364 | wxDELETE(m_selection); |
a7dde52f SN |
2365 | |
2366 | m_ownTable = false; | |
2f024384 DS |
2367 | m_numRows = 0; |
2368 | m_numCols = 0; | |
a7dde52f SN |
2369 | checkSelection = true; |
2370 | ||
2371 | // kill row and column size arrays | |
2372 | m_colWidths.Empty(); | |
2373 | m_colRights.Empty(); | |
2374 | m_rowHeights.Empty(); | |
2375 | m_rowBottoms.Empty(); | |
233a54f6 | 2376 | } |
2f024384 | 2377 | |
233a54f6 | 2378 | if (table) |
2796cce3 RD |
2379 | { |
2380 | m_numRows = table->GetNumberRows(); | |
2381 | m_numCols = table->GetNumberCols(); | |
2382 | ||
ad805b9e | 2383 | if ( m_useNativeHeader ) |
3039ade9 | 2384 | GetGridColHeader()->SetColumnCount(m_numCols); |
ad805b9e | 2385 | |
2796cce3 RD |
2386 | m_table = table; |
2387 | m_table->SetView( this ); | |
8fc856de | 2388 | m_ownTable = takeOwnership; |
043d16b2 | 2389 | m_selection = new wxGridSelection( this, selmode ); |
a7dde52f SN |
2390 | if (checkSelection) |
2391 | { | |
2392 | // If the newly set table is smaller than the | |
2393 | // original one current cell and selection regions | |
2394 | // might be invalid, | |
8a3e536c | 2395 | m_selectedBlockCorner = wxGridNoCellCoords; |
c71b2126 | 2396 | m_currentCellCoords = |
a7dde52f SN |
2397 | wxGridCellCoords(wxMin(m_numRows, m_currentCellCoords.GetRow()), |
2398 | wxMin(m_numCols, m_currentCellCoords.GetCol())); | |
8a3e536c VZ |
2399 | if (m_selectedBlockTopLeft.GetRow() >= m_numRows || |
2400 | m_selectedBlockTopLeft.GetCol() >= m_numCols) | |
a7dde52f | 2401 | { |
8a3e536c VZ |
2402 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
2403 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
a7dde52f SN |
2404 | } |
2405 | else | |
8a3e536c | 2406 | m_selectedBlockBottomRight = |
a7dde52f | 2407 | wxGridCellCoords(wxMin(m_numRows, |
8a3e536c | 2408 | m_selectedBlockBottomRight.GetRow()), |
a7dde52f | 2409 | wxMin(m_numCols, |
8a3e536c | 2410 | m_selectedBlockBottomRight.GetCol())); |
a7dde52f | 2411 | } |
6f36917b VZ |
2412 | CalcDimensions(); |
2413 | ||
ca65c044 | 2414 | m_created = true; |
2d66e025 | 2415 | } |
f85afd4e | 2416 | |
1f89d869 VZ |
2417 | InvalidateBestSize(); |
2418 | ||
2d66e025 | 2419 | return m_created; |
f85afd4e MB |
2420 | } |
2421 | ||
ad805b9e | 2422 | void wxGrid::Init() |
f9549841 VZ |
2423 | { |
2424 | m_created = false; | |
2425 | ||
2426 | m_cornerLabelWin = NULL; | |
2427 | m_rowLabelWin = NULL; | |
ad805b9e | 2428 | m_colWindow = NULL; |
f9549841 VZ |
2429 | m_gridWin = NULL; |
2430 | ||
2431 | m_table = NULL; | |
2432 | m_ownTable = false; | |
2433 | ||
2434 | m_selection = NULL; | |
2435 | m_defaultCellAttr = NULL; | |
2436 | m_typeRegistry = NULL; | |
2437 | m_winCapture = NULL; | |
f9549841 | 2438 | |
f85afd4e MB |
2439 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; |
2440 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
2441 | ||
82edfbe7 VZ |
2442 | m_setFixedRows = |
2443 | m_setFixedCols = NULL; | |
2444 | ||
0a976765 VZ |
2445 | // init attr cache |
2446 | m_attrCache.row = -1; | |
2b5f62a0 VZ |
2447 | m_attrCache.col = -1; |
2448 | m_attrCache.attr = NULL; | |
0a976765 | 2449 | |
ff72f628 | 2450 | m_labelFont = GetFont(); |
52d6f640 | 2451 | m_labelFont.SetWeight( wxBOLD ); |
8f177c8e | 2452 | |
73145b0e | 2453 | m_rowLabelHorizAlign = wxALIGN_CENTRE; |
4c7277db | 2454 | m_rowLabelVertAlign = wxALIGN_CENTRE; |
f85afd4e | 2455 | |
4c7277db | 2456 | m_colLabelHorizAlign = wxALIGN_CENTRE; |
73145b0e | 2457 | m_colLabelVertAlign = wxALIGN_CENTRE; |
d43851f7 | 2458 | m_colLabelTextOrientation = wxHORIZONTAL; |
f85afd4e | 2459 | |
f85afd4e | 2460 | m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH; |
ad805b9e | 2461 | m_defaultRowHeight = 0; // this will be initialized after creation |
1f1ce288 | 2462 | |
b8d24d4e RG |
2463 | m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH; |
2464 | m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT; | |
2465 | ||
73145b0e | 2466 | m_gridLineColour = wxColour( 192,192,192 ); |
ca65c044 | 2467 | m_gridLinesEnabled = true; |
9f7aee01 VZ |
2468 | m_gridLinesClipHorz = |
2469 | m_gridLinesClipVert = true; | |
73145b0e | 2470 | m_cellHighlightColour = *wxBLACK; |
bf7945ce | 2471 | m_cellHighlightPenWidth = 2; |
d2520c85 | 2472 | m_cellHighlightROPenWidth = 1; |
8f177c8e | 2473 | |
d4175745 VZ |
2474 | m_canDragColMove = false; |
2475 | ||
58dd5b3b | 2476 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
10a4531d | 2477 | m_winCapture = NULL; |
ca65c044 WS |
2478 | m_canDragRowSize = true; |
2479 | m_canDragColSize = true; | |
2480 | m_canDragGridSize = true; | |
79dbea21 | 2481 | m_canDragCell = false; |
f85afd4e MB |
2482 | m_dragLastPos = -1; |
2483 | m_dragRowOrCol = -1; | |
ca65c044 | 2484 | m_isDragging = false; |
07296f0b | 2485 | m_startDragPos = wxDefaultPosition; |
ad805b9e | 2486 | |
11393d29 VZ |
2487 | m_sortCol = wxNOT_FOUND; |
2488 | m_sortIsAscending = true; | |
2489 | ||
ad805b9e | 2490 | m_useNativeHeader = |
71cf399f | 2491 | m_nativeColumnLabels = false; |
f85afd4e | 2492 | |
ca65c044 | 2493 | m_waitForSlowClick = false; |
025562fe | 2494 | |
f85afd4e MB |
2495 | m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS ); |
2496 | m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE ); | |
2497 | ||
2498 | m_currentCellCoords = wxGridNoCellCoords; | |
f85afd4e | 2499 | |
ad805b9e VZ |
2500 | m_selectedBlockTopLeft = |
2501 | m_selectedBlockBottomRight = | |
2502 | m_selectedBlockCorner = wxGridNoCellCoords; | |
b524b5c6 | 2503 | |
d43851f7 JS |
2504 | m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); |
2505 | m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
8f177c8e | 2506 | |
ca65c044 | 2507 | m_editable = true; // default for whole grid |
f85afd4e | 2508 | |
ca65c044 | 2509 | m_inOnKeyDown = false; |
2d66e025 | 2510 | m_batchCount = 0; |
3c79cf49 | 2511 | |
266e8367 | 2512 | m_extraWidth = |
526dbb95 | 2513 | m_extraHeight = 0; |
608754c4 | 2514 | |
fd76c6a7 VZ |
2515 | // we can't call SetScrollRate() as the window isn't created yet but OTOH |
2516 | // we don't need to call it neither as the scroll position is (0, 0) right | |
2517 | // now anyhow, so just set the parameters directly | |
2518 | m_xScrollPixelsPerLine = GRID_SCROLL_LINE_X; | |
2519 | m_yScrollPixelsPerLine = GRID_SCROLL_LINE_Y; | |
1dc17bca VZ |
2520 | |
2521 | m_tabBehaviour = Tab_Stop; | |
7c1cb261 VZ |
2522 | } |
2523 | ||
2524 | // ---------------------------------------------------------------------------- | |
2525 | // the idea is to call these functions only when necessary because they create | |
2526 | // quite big arrays which eat memory mostly unnecessary - in particular, if | |
2527 | // default widths/heights are used for all rows/columns, we may not use these | |
2528 | // arrays at all | |
2529 | // | |
0ed3b812 VZ |
2530 | // with some extra code, it should be possible to only store the widths/heights |
2531 | // different from default ones (resulting in space savings for huge grids) but | |
2532 | // this is not done currently | |
7c1cb261 VZ |
2533 | // ---------------------------------------------------------------------------- |
2534 | ||
2535 | void wxGrid::InitRowHeights() | |
2536 | { | |
2537 | m_rowHeights.Empty(); | |
2538 | m_rowBottoms.Empty(); | |
2539 | ||
2540 | m_rowHeights.Alloc( m_numRows ); | |
2541 | m_rowBottoms.Alloc( m_numRows ); | |
2542 | ||
27f35b66 SN |
2543 | m_rowHeights.Add( m_defaultRowHeight, m_numRows ); |
2544 | ||
0ed3b812 | 2545 | int rowBottom = 0; |
3d59537f | 2546 | for ( int i = 0; i < m_numRows; i++ ) |
7c1cb261 | 2547 | { |
7c1cb261 VZ |
2548 | rowBottom += m_defaultRowHeight; |
2549 | m_rowBottoms.Add( rowBottom ); | |
2550 | } | |
2551 | } | |
2552 | ||
2553 | void wxGrid::InitColWidths() | |
2554 | { | |
2555 | m_colWidths.Empty(); | |
2556 | m_colRights.Empty(); | |
2557 | ||
2558 | m_colWidths.Alloc( m_numCols ); | |
2559 | m_colRights.Alloc( m_numCols ); | |
27f35b66 SN |
2560 | |
2561 | m_colWidths.Add( m_defaultColWidth, m_numCols ); | |
2562 | ||
3d59537f | 2563 | for ( int i = 0; i < m_numCols; i++ ) |
7c1cb261 | 2564 | { |
e822d1bd | 2565 | int colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth; |
7c1cb261 VZ |
2566 | m_colRights.Add( colRight ); |
2567 | } | |
2568 | } | |
2569 | ||
2570 | int wxGrid::GetColWidth(int col) const | |
2571 | { | |
30496905 VZ |
2572 | if ( m_colWidths.IsEmpty() ) |
2573 | return m_defaultColWidth; | |
2574 | ||
2575 | // a negative width indicates a hidden column | |
2576 | return m_colWidths[col] > 0 ? m_colWidths[col] : 0; | |
7c1cb261 VZ |
2577 | } |
2578 | ||
2579 | int wxGrid::GetColLeft(int col) const | |
2580 | { | |
30496905 VZ |
2581 | if ( m_colRights.IsEmpty() ) |
2582 | return GetColPos( col ) * m_defaultColWidth; | |
2583 | ||
2584 | return m_colRights[col] - GetColWidth(col); | |
7c1cb261 VZ |
2585 | } |
2586 | ||
2587 | int wxGrid::GetColRight(int col) const | |
2588 | { | |
d4175745 | 2589 | return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth |
7c1cb261 VZ |
2590 | : m_colRights[col]; |
2591 | } | |
2592 | ||
2593 | int wxGrid::GetRowHeight(int row) const | |
2594 | { | |
30496905 VZ |
2595 | // no custom heights / hidden rows |
2596 | if ( m_rowHeights.IsEmpty() ) | |
2597 | return m_defaultRowHeight; | |
2598 | ||
2599 | // a negative height indicates a hidden row | |
2600 | return m_rowHeights[row] > 0 ? m_rowHeights[row] : 0; | |
7c1cb261 | 2601 | } |
2d66e025 | 2602 | |
7c1cb261 VZ |
2603 | int wxGrid::GetRowTop(int row) const |
2604 | { | |
30496905 VZ |
2605 | if ( m_rowBottoms.IsEmpty() ) |
2606 | return row * m_defaultRowHeight; | |
2607 | ||
2608 | return m_rowBottoms[row] - GetRowHeight(row); | |
f85afd4e MB |
2609 | } |
2610 | ||
7c1cb261 VZ |
2611 | int wxGrid::GetRowBottom(int row) const |
2612 | { | |
2613 | return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight | |
2614 | : m_rowBottoms[row]; | |
2615 | } | |
f85afd4e MB |
2616 | |
2617 | void wxGrid::CalcDimensions() | |
2618 | { | |
0ed3b812 VZ |
2619 | // compute the size of the scrollable area |
2620 | int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0; | |
2621 | int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0; | |
60ff3b99 | 2622 | |
0ed3b812 VZ |
2623 | w += m_extraWidth; |
2624 | h += m_extraHeight; | |
faec5a43 | 2625 | |
73145b0e | 2626 | // take into account editor if shown |
4db6714b | 2627 | if ( IsCellEditControlShown() ) |
73145b0e | 2628 | { |
3d59537f DS |
2629 | int w2, h2; |
2630 | int r = m_currentCellCoords.GetRow(); | |
2631 | int c = m_currentCellCoords.GetCol(); | |
2632 | int x = GetColLeft(c); | |
2633 | int y = GetRowTop(r); | |
2634 | ||
2635 | // how big is the editor | |
2636 | wxGridCellAttr* attr = GetCellAttr(r, c); | |
2637 | wxGridCellEditor* editor = attr->GetEditor(this, r, c); | |
2638 | editor->GetControl()->GetSize(&w2, &h2); | |
2639 | w2 += x; | |
2640 | h2 += y; | |
2641 | if ( w2 > w ) | |
2642 | w = w2; | |
2643 | if ( h2 > h ) | |
2644 | h = h2; | |
2645 | editor->DecRef(); | |
2646 | attr->DecRef(); | |
73145b0e JS |
2647 | } |
2648 | ||
faec5a43 SN |
2649 | // preserve (more or less) the previous position |
2650 | int x, y; | |
2651 | GetViewStart( &x, &y ); | |
97a9929e | 2652 | |
c92ed9f7 | 2653 | // ensure the position is valid for the new scroll ranges |
7b519e5e | 2654 | if ( x >= w ) |
c92ed9f7 | 2655 | x = wxMax( w - 1, 0 ); |
7b519e5e | 2656 | if ( y >= h ) |
c92ed9f7 | 2657 | y = wxMax( h - 1, 0 ); |
faec5a43 | 2658 | |
ace8d849 | 2659 | // update the virtual size and refresh the scrollbars to reflect it |
f1ff7df0 VZ |
2660 | m_gridWin->SetVirtualSize(w, h); |
2661 | Scroll(x, y); | |
ace8d849 | 2662 | AdjustScrollbars(); |
12314291 VZ |
2663 | |
2664 | // if our OnSize() hadn't been called (it would if we have scrollbars), we | |
2665 | // still must reposition the children | |
2666 | CalcWindowSizes(); | |
f85afd4e MB |
2667 | } |
2668 | ||
69367c56 VZ |
2669 | wxSize wxGrid::GetSizeAvailableForScrollTarget(const wxSize& size) |
2670 | { | |
2671 | wxSize sizeGridWin(size); | |
2672 | sizeGridWin.x -= m_rowLabelWidth; | |
2673 | sizeGridWin.y -= m_colLabelHeight; | |
2674 | ||
2675 | return sizeGridWin; | |
2676 | } | |
2677 | ||
7807d81c MB |
2678 | void wxGrid::CalcWindowSizes() |
2679 | { | |
b0a877ec SC |
2680 | // escape if the window is has not been fully created yet |
2681 | ||
2682 | if ( m_cornerLabelWin == NULL ) | |
2f024384 | 2683 | return; |
b0a877ec | 2684 | |
7807d81c MB |
2685 | int cw, ch; |
2686 | GetClientSize( &cw, &ch ); | |
b99be8fb | 2687 | |
c1841ac2 VZ |
2688 | // the grid may be too small to have enough space for the labels yet, don't |
2689 | // size the windows to negative sizes in this case | |
2690 | int gw = cw - m_rowLabelWidth; | |
2691 | int gh = ch - m_colLabelHeight; | |
2692 | if (gw < 0) | |
2693 | gw = 0; | |
2694 | if (gh < 0) | |
2695 | gh = 0; | |
2696 | ||
6d308072 | 2697 | if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() ) |
7807d81c MB |
2698 | m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight ); |
2699 | ||
ad805b9e VZ |
2700 | if ( m_colWindow && m_colWindow->IsShown() ) |
2701 | m_colWindow->SetSize( m_rowLabelWidth, 0, gw, m_colLabelHeight ); | |
7807d81c | 2702 | |
6d308072 | 2703 | if ( m_rowLabelWin && m_rowLabelWin->IsShown() ) |
c1841ac2 | 2704 | m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, gh ); |
7807d81c | 2705 | |
6d308072 | 2706 | if ( m_gridWin && m_gridWin->IsShown() ) |
c1841ac2 | 2707 | m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, gw, gh ); |
7807d81c MB |
2708 | } |
2709 | ||
3d59537f DS |
2710 | // this is called when the grid table sends a message |
2711 | // to indicate that it has been redimensioned | |
f85afd4e MB |
2712 | // |
2713 | bool wxGrid::Redimension( wxGridTableMessage& msg ) | |
2714 | { | |
2715 | int i; | |
ca65c044 | 2716 | bool result = false; |
8f177c8e | 2717 | |
a6794685 SN |
2718 | // Clear the attribute cache as the attribute might refer to a different |
2719 | // cell than stored in the cache after adding/removing rows/columns. | |
2720 | ClearAttrCache(); | |
2f024384 | 2721 | |
7e48d7d9 SN |
2722 | // By the same reasoning, the editor should be dismissed if columns are |
2723 | // added or removed. And for consistency, it should IMHO always be | |
2724 | // removed, not only if the cell "underneath" it actually changes. | |
2725 | // For now, I intentionally do not save the editor's content as the | |
2726 | // cell it might want to save that stuff to might no longer exist. | |
bca7bfc8 | 2727 | HideCellEditControl(); |
2f024384 | 2728 | |
f85afd4e MB |
2729 | switch ( msg.GetId() ) |
2730 | { | |
2731 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
2732 | { | |
2733 | size_t pos = msg.GetCommandInt(); | |
2734 | int numRows = msg.GetCommandInt2(); | |
f6bcfd97 | 2735 | |
f85afd4e | 2736 | m_numRows += numRows; |
2d66e025 | 2737 | |
f6bcfd97 BP |
2738 | if ( !m_rowHeights.IsEmpty() ) |
2739 | { | |
27f35b66 SN |
2740 | m_rowHeights.Insert( m_defaultRowHeight, pos, numRows ); |
2741 | m_rowBottoms.Insert( 0, pos, numRows ); | |
f6bcfd97 BP |
2742 | |
2743 | int bottom = 0; | |
2f024384 DS |
2744 | if ( pos > 0 ) |
2745 | bottom = m_rowBottoms[pos - 1]; | |
60ff3b99 | 2746 | |
3d59537f | 2747 | for ( i = pos; i < m_numRows; i++ ) |
f6bcfd97 BP |
2748 | { |
2749 | bottom += m_rowHeights[i]; | |
2750 | m_rowBottoms[i] = bottom; | |
2751 | } | |
2752 | } | |
2f024384 | 2753 | |
f6bcfd97 BP |
2754 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2755 | { | |
2756 | // if we have just inserted cols into an empty grid the current | |
2757 | // cell will be undefined... | |
2758 | // | |
2759 | SetCurrentCell( 0, 0 ); | |
2760 | } | |
3f3dc2ef VZ |
2761 | |
2762 | if ( m_selection ) | |
2763 | m_selection->UpdateRows( pos, numRows ); | |
f6bcfd97 BP |
2764 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
2765 | if (attrProvider) | |
2766 | attrProvider->UpdateAttrRows( pos, numRows ); | |
2767 | ||
2768 | if ( !GetBatchCount() ) | |
2d66e025 | 2769 | { |
f6bcfd97 BP |
2770 | CalcDimensions(); |
2771 | m_rowLabelWin->Refresh(); | |
2d66e025 | 2772 | } |
f85afd4e | 2773 | } |
ca65c044 | 2774 | result = true; |
f6bcfd97 | 2775 | break; |
f85afd4e MB |
2776 | |
2777 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
2778 | { | |
2779 | int numRows = msg.GetCommandInt(); | |
2d66e025 | 2780 | int oldNumRows = m_numRows; |
f85afd4e | 2781 | m_numRows += numRows; |
2d66e025 | 2782 | |
f6bcfd97 BP |
2783 | if ( !m_rowHeights.IsEmpty() ) |
2784 | { | |
27f35b66 SN |
2785 | m_rowHeights.Add( m_defaultRowHeight, numRows ); |
2786 | m_rowBottoms.Add( 0, numRows ); | |
60ff3b99 | 2787 | |
f6bcfd97 | 2788 | int bottom = 0; |
2f024384 DS |
2789 | if ( oldNumRows > 0 ) |
2790 | bottom = m_rowBottoms[oldNumRows - 1]; | |
f6bcfd97 | 2791 | |
3d59537f | 2792 | for ( i = oldNumRows; i < m_numRows; i++ ) |
f6bcfd97 BP |
2793 | { |
2794 | bottom += m_rowHeights[i]; | |
2795 | m_rowBottoms[i] = bottom; | |
2796 | } | |
2797 | } | |
2f024384 | 2798 | |
f6bcfd97 BP |
2799 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2800 | { | |
2801 | // if we have just inserted cols into an empty grid the current | |
2802 | // cell will be undefined... | |
2803 | // | |
2804 | SetCurrentCell( 0, 0 ); | |
2805 | } | |
2f024384 | 2806 | |
f6bcfd97 | 2807 | if ( !GetBatchCount() ) |
2d66e025 | 2808 | { |
f6bcfd97 BP |
2809 | CalcDimensions(); |
2810 | m_rowLabelWin->Refresh(); | |
2d66e025 | 2811 | } |
f85afd4e | 2812 | } |
ca65c044 | 2813 | result = true; |
f6bcfd97 | 2814 | break; |
f85afd4e MB |
2815 | |
2816 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
2817 | { | |
2818 | size_t pos = msg.GetCommandInt(); | |
2819 | int numRows = msg.GetCommandInt2(); | |
f85afd4e MB |
2820 | m_numRows -= numRows; |
2821 | ||
f6bcfd97 | 2822 | if ( !m_rowHeights.IsEmpty() ) |
f85afd4e | 2823 | { |
27f35b66 SN |
2824 | m_rowHeights.RemoveAt( pos, numRows ); |
2825 | m_rowBottoms.RemoveAt( pos, numRows ); | |
2d66e025 MB |
2826 | |
2827 | int h = 0; | |
3d59537f | 2828 | for ( i = 0; i < m_numRows; i++ ) |
2d66e025 MB |
2829 | { |
2830 | h += m_rowHeights[i]; | |
2831 | m_rowBottoms[i] = h; | |
2832 | } | |
f85afd4e | 2833 | } |
3d59537f | 2834 | |
f6bcfd97 BP |
2835 | if ( !m_numRows ) |
2836 | { | |
2837 | m_currentCellCoords = wxGridNoCellCoords; | |
2838 | } | |
2839 | else | |
2840 | { | |
2841 | if ( m_currentCellCoords.GetRow() >= m_numRows ) | |
2842 | m_currentCellCoords.Set( 0, 0 ); | |
2843 | } | |
3f3dc2ef VZ |
2844 | |
2845 | if ( m_selection ) | |
2846 | m_selection->UpdateRows( pos, -((int)numRows) ); | |
f6bcfd97 | 2847 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4db6714b KH |
2848 | if (attrProvider) |
2849 | { | |
f6bcfd97 | 2850 | attrProvider->UpdateAttrRows( pos, -((int)numRows) ); |
3d59537f | 2851 | |
84912ef8 RD |
2852 | // ifdef'd out following patch from Paul Gammans |
2853 | #if 0 | |
3ca6a5f0 | 2854 | // No need to touch column attributes, unless we |
f6bcfd97 BP |
2855 | // removed _all_ rows, in this case, we remove |
2856 | // all column attributes. | |
2857 | // I hate to do this here, but the | |
2858 | // needed data is not available inside UpdateAttrRows. | |
2859 | if ( !GetNumberRows() ) | |
2860 | attrProvider->UpdateAttrCols( 0, -GetNumberCols() ); | |
84912ef8 | 2861 | #endif |
f6bcfd97 | 2862 | } |
ccdee36f | 2863 | |
f6bcfd97 BP |
2864 | if ( !GetBatchCount() ) |
2865 | { | |
2866 | CalcDimensions(); | |
2867 | m_rowLabelWin->Refresh(); | |
2868 | } | |
f85afd4e | 2869 | } |
ca65c044 | 2870 | result = true; |
f6bcfd97 | 2871 | break; |
f85afd4e MB |
2872 | |
2873 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
2874 | { | |
2875 | size_t pos = msg.GetCommandInt(); | |
2876 | int numCols = msg.GetCommandInt2(); | |
f85afd4e | 2877 | m_numCols += numCols; |
2d66e025 | 2878 | |
ad805b9e | 2879 | if ( m_useNativeHeader ) |
3039ade9 | 2880 | GetGridColHeader()->SetColumnCount(m_numCols); |
ad805b9e | 2881 | |
d4175745 VZ |
2882 | if ( !m_colAt.IsEmpty() ) |
2883 | { | |
2884 | //Shift the column IDs | |
d4175745 VZ |
2885 | for ( i = 0; i < m_numCols - numCols; i++ ) |
2886 | { | |
2887 | if ( m_colAt[i] >= (int)pos ) | |
2888 | m_colAt[i] += numCols; | |
2889 | } | |
2890 | ||
2891 | m_colAt.Insert( pos, pos, numCols ); | |
2892 | ||
2893 | //Set the new columns' positions | |
2894 | for ( i = pos + 1; i < (int)pos + numCols; i++ ) | |
2895 | { | |
2896 | m_colAt[i] = i; | |
2897 | } | |
2898 | } | |
2899 | ||
f6bcfd97 BP |
2900 | if ( !m_colWidths.IsEmpty() ) |
2901 | { | |
27f35b66 SN |
2902 | m_colWidths.Insert( m_defaultColWidth, pos, numCols ); |
2903 | m_colRights.Insert( 0, pos, numCols ); | |
f6bcfd97 BP |
2904 | |
2905 | int right = 0; | |
2f024384 | 2906 | if ( pos > 0 ) |
d4175745 | 2907 | right = m_colRights[GetColAt( pos - 1 )]; |
60ff3b99 | 2908 | |
d4175745 VZ |
2909 | int colPos; |
2910 | for ( colPos = pos; colPos < m_numCols; colPos++ ) | |
f6bcfd97 | 2911 | { |
d4175745 VZ |
2912 | i = GetColAt( colPos ); |
2913 | ||
f6bcfd97 BP |
2914 | right += m_colWidths[i]; |
2915 | m_colRights[i] = right; | |
2916 | } | |
2917 | } | |
2f024384 | 2918 | |
f6bcfd97 | 2919 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2d66e025 | 2920 | { |
f6bcfd97 BP |
2921 | // if we have just inserted cols into an empty grid the current |
2922 | // cell will be undefined... | |
2923 | // | |
2924 | SetCurrentCell( 0, 0 ); | |
2d66e025 | 2925 | } |
3f3dc2ef VZ |
2926 | |
2927 | if ( m_selection ) | |
2928 | m_selection->UpdateCols( pos, numCols ); | |
f6bcfd97 BP |
2929 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
2930 | if (attrProvider) | |
2931 | attrProvider->UpdateAttrCols( pos, numCols ); | |
2932 | if ( !GetBatchCount() ) | |
2933 | { | |
2934 | CalcDimensions(); | |
ad805b9e | 2935 | m_colWindow->Refresh(); |
f6bcfd97 | 2936 | } |
f85afd4e | 2937 | } |
ca65c044 | 2938 | result = true; |
f6bcfd97 | 2939 | break; |
f85afd4e MB |
2940 | |
2941 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
2942 | { | |
2943 | int numCols = msg.GetCommandInt(); | |
2d66e025 | 2944 | int oldNumCols = m_numCols; |
f85afd4e | 2945 | m_numCols += numCols; |
ad805b9e | 2946 | if ( m_useNativeHeader ) |
3039ade9 | 2947 | GetGridColHeader()->SetColumnCount(m_numCols); |
d4175745 VZ |
2948 | |
2949 | if ( !m_colAt.IsEmpty() ) | |
2950 | { | |
2951 | m_colAt.Add( 0, numCols ); | |
2952 | ||
2953 | //Set the new columns' positions | |
d4175745 VZ |
2954 | for ( i = oldNumCols; i < m_numCols; i++ ) |
2955 | { | |
2956 | m_colAt[i] = i; | |
2957 | } | |
2958 | } | |
2959 | ||
f6bcfd97 BP |
2960 | if ( !m_colWidths.IsEmpty() ) |
2961 | { | |
27f35b66 SN |
2962 | m_colWidths.Add( m_defaultColWidth, numCols ); |
2963 | m_colRights.Add( 0, numCols ); | |
2d66e025 | 2964 | |
f6bcfd97 | 2965 | int right = 0; |
2f024384 | 2966 | if ( oldNumCols > 0 ) |
d4175745 | 2967 | right = m_colRights[GetColAt( oldNumCols - 1 )]; |
60ff3b99 | 2968 | |
d4175745 VZ |
2969 | int colPos; |
2970 | for ( colPos = oldNumCols; colPos < m_numCols; colPos++ ) | |
f6bcfd97 | 2971 | { |
d4175745 VZ |
2972 | i = GetColAt( colPos ); |
2973 | ||
f6bcfd97 BP |
2974 | right += m_colWidths[i]; |
2975 | m_colRights[i] = right; | |
2976 | } | |
2977 | } | |
2f024384 | 2978 | |
f6bcfd97 | 2979 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2d66e025 | 2980 | { |
f6bcfd97 BP |
2981 | // if we have just inserted cols into an empty grid the current |
2982 | // cell will be undefined... | |
2983 | // | |
2984 | SetCurrentCell( 0, 0 ); | |
2985 | } | |
2986 | if ( !GetBatchCount() ) | |
2987 | { | |
2988 | CalcDimensions(); | |
ad805b9e | 2989 | m_colWindow->Refresh(); |
2d66e025 | 2990 | } |
f85afd4e | 2991 | } |
ca65c044 | 2992 | result = true; |
f6bcfd97 | 2993 | break; |
f85afd4e MB |
2994 | |
2995 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
2996 | { | |
2997 | size_t pos = msg.GetCommandInt(); | |
2998 | int numCols = msg.GetCommandInt2(); | |
f85afd4e | 2999 | m_numCols -= numCols; |
ad805b9e | 3000 | if ( m_useNativeHeader ) |
3039ade9 | 3001 | GetGridColHeader()->SetColumnCount(m_numCols); |
f85afd4e | 3002 | |
d4175745 VZ |
3003 | if ( !m_colAt.IsEmpty() ) |
3004 | { | |
3005 | int colID = GetColAt( pos ); | |
3006 | ||
3007 | m_colAt.RemoveAt( pos, numCols ); | |
3008 | ||
3009 | //Shift the column IDs | |
3010 | int colPos; | |
3011 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
3012 | { | |
3013 | if ( m_colAt[colPos] > colID ) | |
3014 | m_colAt[colPos] -= numCols; | |
3015 | } | |
3016 | } | |
3017 | ||
f6bcfd97 | 3018 | if ( !m_colWidths.IsEmpty() ) |
f85afd4e | 3019 | { |
27f35b66 SN |
3020 | m_colWidths.RemoveAt( pos, numCols ); |
3021 | m_colRights.RemoveAt( pos, numCols ); | |
2d66e025 MB |
3022 | |
3023 | int w = 0; | |
d4175745 VZ |
3024 | int colPos; |
3025 | for ( colPos = 0; colPos < m_numCols; colPos++ ) | |
2d66e025 | 3026 | { |
d4175745 VZ |
3027 | i = GetColAt( colPos ); |
3028 | ||
2d66e025 MB |
3029 | w += m_colWidths[i]; |
3030 | m_colRights[i] = w; | |
3031 | } | |
f85afd4e | 3032 | } |
2f024384 | 3033 | |
f6bcfd97 BP |
3034 | if ( !m_numCols ) |
3035 | { | |
3036 | m_currentCellCoords = wxGridNoCellCoords; | |
3037 | } | |
3038 | else | |
3039 | { | |
3040 | if ( m_currentCellCoords.GetCol() >= m_numCols ) | |
3041 | m_currentCellCoords.Set( 0, 0 ); | |
3042 | } | |
3f3dc2ef VZ |
3043 | |
3044 | if ( m_selection ) | |
3045 | m_selection->UpdateCols( pos, -((int)numCols) ); | |
f6bcfd97 | 3046 | wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); |
4db6714b KH |
3047 | if (attrProvider) |
3048 | { | |
f6bcfd97 | 3049 | attrProvider->UpdateAttrCols( pos, -((int)numCols) ); |
ccdee36f | 3050 | |
84912ef8 RD |
3051 | // ifdef'd out following patch from Paul Gammans |
3052 | #if 0 | |
f6bcfd97 BP |
3053 | // No need to touch row attributes, unless we |
3054 | // removed _all_ columns, in this case, we remove | |
3055 | // all row attributes. | |
3056 | // I hate to do this here, but the | |
3057 | // needed data is not available inside UpdateAttrCols. | |
3058 | if ( !GetNumberCols() ) | |
3059 | attrProvider->UpdateAttrRows( 0, -GetNumberRows() ); | |
84912ef8 | 3060 | #endif |
f6bcfd97 | 3061 | } |
ccdee36f | 3062 | |
f6bcfd97 BP |
3063 | if ( !GetBatchCount() ) |
3064 | { | |
3065 | CalcDimensions(); | |
ad805b9e | 3066 | m_colWindow->Refresh(); |
f6bcfd97 | 3067 | } |
f85afd4e | 3068 | } |
ca65c044 | 3069 | result = true; |
f6bcfd97 | 3070 | break; |
f85afd4e MB |
3071 | } |
3072 | ||
a6b40a9a VZ |
3073 | InvalidateBestSize(); |
3074 | ||
f6bcfd97 BP |
3075 | if (result && !GetBatchCount() ) |
3076 | m_gridWin->Refresh(); | |
2f024384 | 3077 | |
f6bcfd97 | 3078 | return result; |
f85afd4e MB |
3079 | } |
3080 | ||
ef316e23 | 3081 | wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const |
f85afd4e | 3082 | { |
2d66e025 MB |
3083 | wxRegionIterator iter( reg ); |
3084 | wxRect r; | |
f85afd4e | 3085 | |
275c4ae4 RD |
3086 | wxArrayInt rowlabels; |
3087 | ||
2d66e025 MB |
3088 | int top, bottom; |
3089 | while ( iter ) | |
f85afd4e | 3090 | { |
2d66e025 | 3091 | r = iter.GetRect(); |
f85afd4e | 3092 | |
2d66e025 MB |
3093 | // TODO: remove this when we can... |
3094 | // There is a bug in wxMotif that gives garbage update | |
3095 | // rectangles if you jump-scroll a long way by clicking the | |
3096 | // scrollbar with middle button. This is a work-around | |
3097 | // | |
3098 | #if defined(__WXMOTIF__) | |
3099 | int cw, ch; | |
3100 | m_gridWin->GetClientSize( &cw, &ch ); | |
56b6cf26 DS |
3101 | if ( r.GetTop() > ch ) |
3102 | r.SetTop( 0 ); | |
2d66e025 MB |
3103 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); |
3104 | #endif | |
f85afd4e | 3105 | |
2d66e025 MB |
3106 | // logical bounds of update region |
3107 | // | |
3108 | int dummy; | |
3109 | CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top ); | |
3110 | CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom ); | |
3111 | ||
3112 | // find the row labels within these bounds | |
3113 | // | |
3114 | int row; | |
3d59537f | 3115 | for ( row = internalYToRow(top); row < m_numRows; row++ ) |
2d66e025 | 3116 | { |
7c1cb261 VZ |
3117 | if ( GetRowBottom(row) < top ) |
3118 | continue; | |
2d66e025 | 3119 | |
6d55126d | 3120 | if ( GetRowTop(row) > bottom ) |
7c1cb261 | 3121 | break; |
60ff3b99 | 3122 | |
d10f4bf9 | 3123 | rowlabels.Add( row ); |
2d66e025 | 3124 | } |
60ff3b99 | 3125 | |
60d8e886 | 3126 | ++iter; |
f85afd4e | 3127 | } |
d10f4bf9 VZ |
3128 | |
3129 | return rowlabels; | |
f85afd4e MB |
3130 | } |
3131 | ||
ef316e23 | 3132 | wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const |
f85afd4e | 3133 | { |
2d66e025 MB |
3134 | wxRegionIterator iter( reg ); |
3135 | wxRect r; | |
f85afd4e | 3136 | |
d10f4bf9 | 3137 | wxArrayInt colLabels; |
f85afd4e | 3138 | |
2d66e025 MB |
3139 | int left, right; |
3140 | while ( iter ) | |
f85afd4e | 3141 | { |
2d66e025 | 3142 | r = iter.GetRect(); |
f85afd4e | 3143 | |
2d66e025 MB |
3144 | // TODO: remove this when we can... |
3145 | // There is a bug in wxMotif that gives garbage update | |
3146 | // rectangles if you jump-scroll a long way by clicking the | |
3147 | // scrollbar with middle button. This is a work-around | |
3148 | // | |
3149 | #if defined(__WXMOTIF__) | |
3150 | int cw, ch; | |
3151 | m_gridWin->GetClientSize( &cw, &ch ); | |
56b6cf26 DS |
3152 | if ( r.GetLeft() > cw ) |
3153 | r.SetLeft( 0 ); | |
2d66e025 MB |
3154 | r.SetRight( wxMin( r.GetRight(), cw ) ); |
3155 | #endif | |
3156 | ||
3157 | // logical bounds of update region | |
3158 | // | |
3159 | int dummy; | |
3160 | CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy ); | |
3161 | CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy ); | |
3162 | ||
3163 | // find the cells within these bounds | |
3164 | // | |
3165 | int col; | |
d4175745 VZ |
3166 | int colPos; |
3167 | for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ ) | |
2d66e025 | 3168 | { |
d4175745 VZ |
3169 | col = GetColAt( colPos ); |
3170 | ||
7c1cb261 VZ |
3171 | if ( GetColRight(col) < left ) |
3172 | continue; | |
60ff3b99 | 3173 | |
7c1cb261 VZ |
3174 | if ( GetColLeft(col) > right ) |
3175 | break; | |
2d66e025 | 3176 | |
d10f4bf9 | 3177 | colLabels.Add( col ); |
2d66e025 | 3178 | } |
60ff3b99 | 3179 | |
60d8e886 | 3180 | ++iter; |
f85afd4e | 3181 | } |
2f024384 | 3182 | |
d10f4bf9 | 3183 | return colLabels; |
f85afd4e MB |
3184 | } |
3185 | ||
ef316e23 | 3186 | wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const |
f85afd4e | 3187 | { |
2d66e025 | 3188 | wxRect r; |
f85afd4e | 3189 | |
d10f4bf9 | 3190 | wxGridCellCoordsArray cellsExposed; |
f85afd4e | 3191 | |
2d66e025 | 3192 | int left, top, right, bottom; |
9e57072a | 3193 | for ( wxRegionIterator iter(reg); iter; ++iter ) |
2d66e025 MB |
3194 | { |
3195 | r = iter.GetRect(); | |
f85afd4e | 3196 | |
9e57072a VZ |
3197 | // Skip 0-height cells, they're invisible anyhow, don't waste time |
3198 | // getting their rectangles and so on. | |
3199 | if ( !r.GetHeight() ) | |
3200 | continue; | |
3201 | ||
2d66e025 MB |
3202 | // TODO: remove this when we can... |
3203 | // There is a bug in wxMotif that gives garbage update | |
3204 | // rectangles if you jump-scroll a long way by clicking the | |
3205 | // scrollbar with middle button. This is a work-around | |
3206 | // | |
3207 | #if defined(__WXMOTIF__) | |
f85afd4e | 3208 | int cw, ch; |
2d66e025 MB |
3209 | m_gridWin->GetClientSize( &cw, &ch ); |
3210 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
3211 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
3212 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
3213 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
3214 | #endif | |
8f177c8e | 3215 | |
2d66e025 MB |
3216 | // logical bounds of update region |
3217 | // | |
3218 | CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
3219 | CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
f85afd4e | 3220 | |
2d66e025 | 3221 | // find the cells within these bounds |
cd4f6f5f VZ |
3222 | wxArrayInt cols; |
3223 | for ( int row = internalYToRow(top); row < m_numRows; row++ ) | |
f85afd4e | 3224 | { |
7c1cb261 VZ |
3225 | if ( GetRowBottom(row) <= top ) |
3226 | continue; | |
f85afd4e | 3227 | |
7c1cb261 VZ |
3228 | if ( GetRowTop(row) > bottom ) |
3229 | break; | |
60ff3b99 | 3230 | |
cd4f6f5f VZ |
3231 | // add all dirty cells in this row: notice that the columns which |
3232 | // are dirty don't depend on the row so we compute them only once | |
3233 | // for the first dirty row and then reuse for all the next ones | |
3234 | if ( cols.empty() ) | |
2d66e025 | 3235 | { |
cd4f6f5f VZ |
3236 | // do determine the dirty columns |
3237 | for ( int pos = XToPos(left); pos <= XToPos(right); pos++ ) | |
3238 | cols.push_back(GetColAt(pos)); | |
d4175745 | 3239 | |
cd4f6f5f VZ |
3240 | // if there are no dirty columns at all, nothing to do |
3241 | if ( cols.empty() ) | |
7c1cb261 | 3242 | break; |
2d66e025 | 3243 | } |
cd4f6f5f VZ |
3244 | |
3245 | const size_t count = cols.size(); | |
3246 | for ( size_t n = 0; n < count; n++ ) | |
3247 | cellsExposed.Add(wxGridCellCoords(row, cols[n])); | |
2d66e025 | 3248 | } |
f85afd4e | 3249 | } |
d10f4bf9 VZ |
3250 | |
3251 | return cellsExposed; | |
f85afd4e MB |
3252 | } |
3253 | ||
3254 | ||
2d66e025 | 3255 | void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 3256 | { |
2d66e025 MB |
3257 | int x, y, row; |
3258 | wxPoint pos( event.GetPosition() ); | |
3259 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 3260 | |
2d66e025 | 3261 | if ( event.Dragging() ) |
f85afd4e | 3262 | { |
426b2d87 JS |
3263 | if (!m_isDragging) |
3264 | { | |
ca65c044 | 3265 | m_isDragging = true; |
426b2d87 JS |
3266 | m_rowLabelWin->CaptureMouse(); |
3267 | } | |
8f177c8e | 3268 | |
2d66e025 | 3269 | if ( event.LeftIsDown() ) |
f85afd4e | 3270 | { |
962a48f6 | 3271 | switch ( m_cursorMode ) |
f85afd4e | 3272 | { |
f85afd4e MB |
3273 | case WXGRID_CURSOR_RESIZE_ROW: |
3274 | { | |
2d66e025 MB |
3275 | int cw, ch, left, dummy; |
3276 | m_gridWin->GetClientSize( &cw, &ch ); | |
3277 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
60ff3b99 | 3278 | |
2d66e025 MB |
3279 | wxClientDC dc( m_gridWin ); |
3280 | PrepareDC( dc ); | |
af547d51 VZ |
3281 | y = wxMax( y, |
3282 | GetRowTop(m_dragRowOrCol) + | |
3283 | GetRowMinimalHeight(m_dragRowOrCol) ); | |
d2fdd8d2 | 3284 | dc.SetLogicalFunction(wxINVERT); |
f85afd4e MB |
3285 | if ( m_dragLastPos >= 0 ) |
3286 | { | |
2d66e025 | 3287 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); |
f85afd4e | 3288 | } |
2d66e025 MB |
3289 | dc.DrawLine( left, y, left+cw, y ); |
3290 | m_dragLastPos = y; | |
f85afd4e MB |
3291 | } |
3292 | break; | |
3293 | ||
3294 | case WXGRID_CURSOR_SELECT_ROW: | |
902725ee | 3295 | { |
e32352cf | 3296 | if ( (row = YToRow( y )) >= 0 ) |
aa5e1f75 | 3297 | { |
3f3dc2ef | 3298 | if ( m_selection ) |
8b5f6d9d | 3299 | m_selection->SelectRow(row, event); |
f85afd4e | 3300 | } |
902725ee WS |
3301 | } |
3302 | break; | |
e2b42eeb VZ |
3303 | |
3304 | // default label to suppress warnings about "enumeration value | |
3305 | // 'xxx' not handled in switch | |
3306 | default: | |
3307 | break; | |
f85afd4e MB |
3308 | } |
3309 | } | |
3310 | return; | |
3311 | } | |
3312 | ||
426b2d87 JS |
3313 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) |
3314 | return; | |
8f177c8e | 3315 | |
426b2d87 JS |
3316 | if (m_isDragging) |
3317 | { | |
ccdee36f DS |
3318 | if (m_rowLabelWin->HasCapture()) |
3319 | m_rowLabelWin->ReleaseMouse(); | |
ca65c044 | 3320 | m_isDragging = false; |
426b2d87 | 3321 | } |
60ff3b99 | 3322 | |
6d004f67 MB |
3323 | // ------------ Entering or leaving the window |
3324 | // | |
3325 | if ( event.Entering() || event.Leaving() ) | |
3326 | { | |
e2b42eeb | 3327 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
6d004f67 MB |
3328 | } |
3329 | ||
2d66e025 | 3330 | // ------------ Left button pressed |
f85afd4e | 3331 | // |
6d004f67 | 3332 | else if ( event.LeftDown() ) |
f85afd4e | 3333 | { |
82edfbe7 VZ |
3334 | row = YToEdgeOfRow(y); |
3335 | if ( row != wxNOT_FOUND && CanDragRowSize(row) ) | |
3336 | { | |
3337 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin); | |
3338 | } | |
3339 | else // not a request to start resizing | |
f85afd4e | 3340 | { |
2d66e025 | 3341 | row = YToRow(y); |
2f024384 | 3342 | if ( row >= 0 && |
b54ba671 | 3343 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) ) |
f85afd4e | 3344 | { |
2b01fd49 | 3345 | if ( !event.ShiftDown() && !event.CmdDown() ) |
aa5e1f75 | 3346 | ClearSelection(); |
64e15340 | 3347 | if ( m_selection ) |
3f3dc2ef VZ |
3348 | { |
3349 | if ( event.ShiftDown() ) | |
3350 | { | |
8b5f6d9d VZ |
3351 | m_selection->SelectBlock |
3352 | ( | |
3353 | m_currentCellCoords.GetRow(), 0, | |
3354 | row, GetNumberCols() - 1, | |
3355 | event | |
3356 | ); | |
3f3dc2ef VZ |
3357 | } |
3358 | else | |
3359 | { | |
8b5f6d9d | 3360 | m_selection->SelectRow(row, event); |
3f3dc2ef VZ |
3361 | } |
3362 | } | |
3363 | ||
e2b42eeb | 3364 | ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin); |
f85afd4e | 3365 | } |
2d66e025 | 3366 | } |
2d66e025 | 3367 | } |
f85afd4e | 3368 | |
2d66e025 MB |
3369 | // ------------ Left double click |
3370 | // | |
3371 | else if (event.LeftDClick() ) | |
3372 | { | |
4e115ed2 | 3373 | row = YToEdgeOfRow(y); |
82edfbe7 VZ |
3374 | if ( row != wxNOT_FOUND && CanDragRowSize(row) ) |
3375 | { | |
3376 | // adjust row height depending on label text | |
27bb2c8c VZ |
3377 | // |
3378 | // TODO: generate RESIZING event, see #10754 | |
82edfbe7 VZ |
3379 | AutoSizeRowLabelSize( row ); |
3380 | ||
c5c1ea96 | 3381 | SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, row, -1, event); |
27bb2c8c | 3382 | |
82edfbe7 VZ |
3383 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
3384 | m_dragLastPos = -1; | |
3385 | } | |
d13b34d3 | 3386 | else // not on row separator or it's not resizable |
2d66e025 MB |
3387 | { |
3388 | row = YToRow(y); | |
a967f048 | 3389 | if ( row >=0 && |
ca65c044 WS |
3390 | !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) ) |
3391 | { | |
a967f048 | 3392 | // no default action at the moment |
ca65c044 | 3393 | } |
f85afd4e MB |
3394 | } |
3395 | } | |
60ff3b99 | 3396 | |
2d66e025 | 3397 | // ------------ Left button released |
f85afd4e | 3398 | // |
2d66e025 | 3399 | else if ( event.LeftUp() ) |
f85afd4e | 3400 | { |
2d66e025 | 3401 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) |
27bb2c8c | 3402 | DoEndDragResizeRow(event); |
f85afd4e | 3403 | |
e2b42eeb VZ |
3404 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin); |
3405 | m_dragLastPos = -1; | |
2d66e025 | 3406 | } |
f85afd4e | 3407 | |
2d66e025 MB |
3408 | // ------------ Right button down |
3409 | // | |
3410 | else if ( event.RightDown() ) | |
3411 | { | |
3412 | row = YToRow(y); | |
ef5df12b | 3413 | if ( row >=0 && |
ca65c044 | 3414 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) |
2d66e025 MB |
3415 | { |
3416 | // no default action at the moment | |
f85afd4e MB |
3417 | } |
3418 | } | |
60ff3b99 | 3419 | |
2d66e025 | 3420 | // ------------ Right double click |
f85afd4e | 3421 | // |
2d66e025 MB |
3422 | else if ( event.RightDClick() ) |
3423 | { | |
3424 | row = YToRow(y); | |
a967f048 | 3425 | if ( row >= 0 && |
ca65c044 | 3426 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) |
2d66e025 MB |
3427 | { |
3428 | // no default action at the moment | |
3429 | } | |
3430 | } | |
60ff3b99 | 3431 | |
2d66e025 | 3432 | // ------------ No buttons down and mouse moving |
f85afd4e | 3433 | // |
2d66e025 | 3434 | else if ( event.Moving() ) |
f85afd4e | 3435 | { |
2d66e025 | 3436 | m_dragRowOrCol = YToEdgeOfRow( y ); |
82edfbe7 | 3437 | if ( m_dragRowOrCol != wxNOT_FOUND ) |
8f177c8e | 3438 | { |
2d66e025 | 3439 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
8f177c8e | 3440 | { |
82edfbe7 | 3441 | if ( CanDragRowSize(m_dragRowOrCol) ) |
ca65c044 | 3442 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false); |
8f177c8e | 3443 | } |
2d66e025 | 3444 | } |
6d004f67 | 3445 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 3446 | { |
ca65c044 | 3447 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false); |
8f177c8e | 3448 | } |
f85afd4e | 3449 | } |
2d66e025 MB |
3450 | } |
3451 | ||
11393d29 VZ |
3452 | void wxGrid::UpdateColumnSortingIndicator(int col) |
3453 | { | |
3454 | wxCHECK_RET( col != wxNOT_FOUND, "invalid column index" ); | |
3455 | ||
3456 | if ( m_useNativeHeader ) | |
3039ade9 | 3457 | GetGridColHeader()->UpdateColumn(col); |
11393d29 VZ |
3458 | else if ( m_nativeColumnLabels ) |
3459 | m_colWindow->Refresh(); | |
3460 | //else: sorting indicator display not yet implemented in grid version | |
3461 | } | |
3462 | ||
3463 | void wxGrid::SetSortingColumn(int col, bool ascending) | |
3464 | { | |
3465 | if ( col == m_sortCol ) | |
3466 | { | |
3467 | // we are already using this column for sorting (or not sorting at all) | |
3468 | // but we might still change the sorting order, check for it | |
3469 | if ( m_sortCol != wxNOT_FOUND && ascending != m_sortIsAscending ) | |
3470 | { | |
3471 | m_sortIsAscending = ascending; | |
3472 | ||
3473 | UpdateColumnSortingIndicator(m_sortCol); | |
3474 | } | |
3475 | } | |
3476 | else // we're changing the column used for sorting | |
3477 | { | |
3478 | const int sortColOld = m_sortCol; | |
3479 | ||
3480 | // change it before updating the column as we want GetSortingColumn() | |
3481 | // to return the correct new value | |
3482 | m_sortCol = col; | |
3483 | ||
3484 | if ( sortColOld != wxNOT_FOUND ) | |
3485 | UpdateColumnSortingIndicator(sortColOld); | |
3486 | ||
3487 | if ( m_sortCol != wxNOT_FOUND ) | |
3488 | { | |
3489 | m_sortIsAscending = ascending; | |
3490 | UpdateColumnSortingIndicator(m_sortCol); | |
3491 | } | |
3492 | } | |
3493 | } | |
3494 | ||
3495 | void wxGrid::DoColHeaderClick(int col) | |
3496 | { | |
3497 | // we consider that the grid was resorted if this event is processed and | |
3498 | // not vetoed | |
3499 | if ( SendEvent(wxEVT_GRID_COL_SORT, -1, col) == 1 ) | |
3500 | { | |
3501 | SetSortingColumn(col, IsSortingBy(col) ? !m_sortIsAscending : true); | |
3502 | Refresh(); | |
3503 | } | |
3504 | } | |
3505 | ||
ad805b9e VZ |
3506 | void wxGrid::DoStartResizeCol(int col) |
3507 | { | |
3508 | m_dragRowOrCol = col; | |
3509 | m_dragLastPos = -1; | |
3510 | DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol)); | |
3511 | } | |
3512 | ||
3513 | void wxGrid::DoUpdateResizeCol(int x) | |
3514 | { | |
3515 | int cw, ch, dummy, top; | |
3516 | m_gridWin->GetClientSize( &cw, &ch ); | |
3517 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
3518 | ||
3519 | wxClientDC dc( m_gridWin ); | |
3520 | PrepareDC( dc ); | |
3521 | ||
3522 | x = wxMax( x, GetColLeft(m_dragRowOrCol) + GetColMinimalWidth(m_dragRowOrCol)); | |
3523 | dc.SetLogicalFunction(wxINVERT); | |
3524 | if ( m_dragLastPos >= 0 ) | |
3525 | { | |
3526 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch ); | |
3527 | } | |
3528 | dc.DrawLine( x, top, x, top + ch ); | |
3529 | m_dragLastPos = x; | |
3530 | } | |
3531 | ||
3532 | void wxGrid::DoUpdateResizeColWidth(int w) | |
3533 | { | |
3534 | DoUpdateResizeCol(GetColLeft(m_dragRowOrCol) + w); | |
3535 | } | |
3536 | ||
2d66e025 MB |
3537 | void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) |
3538 | { | |
7d1214cd PC |
3539 | int x; |
3540 | CalcUnscrolledPosition( event.GetPosition().x, 0, &x, NULL ); | |
60ff3b99 | 3541 | |
11393d29 | 3542 | int col = XToCol(x); |
2d66e025 | 3543 | if ( event.Dragging() ) |
f85afd4e | 3544 | { |
fe77cf60 JS |
3545 | if (!m_isDragging) |
3546 | { | |
ca65c044 | 3547 | m_isDragging = true; |
ad805b9e | 3548 | GetColLabelWindow()->CaptureMouse(); |
d4175745 | 3549 | |
11393d29 VZ |
3550 | if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL && col != -1 ) |
3551 | DoStartMoveCol(col); | |
fe77cf60 | 3552 | } |
8f177c8e | 3553 | |
2d66e025 | 3554 | if ( event.LeftIsDown() ) |
8f177c8e | 3555 | { |
962a48f6 | 3556 | switch ( m_cursorMode ) |
8f177c8e | 3557 | { |
2d66e025 | 3558 | case WXGRID_CURSOR_RESIZE_COL: |
ad805b9e | 3559 | DoUpdateResizeCol(x); |
2d66e025 | 3560 | break; |
f85afd4e | 3561 | |
2d66e025 | 3562 | case WXGRID_CURSOR_SELECT_COL: |
902725ee | 3563 | { |
11393d29 | 3564 | if ( col != -1 ) |
aa5e1f75 | 3565 | { |
3f3dc2ef | 3566 | if ( m_selection ) |
8b5f6d9d | 3567 | m_selection->SelectCol(col, event); |
2d66e025 | 3568 | } |
902725ee WS |
3569 | } |
3570 | break; | |
e2b42eeb | 3571 | |
d4175745 VZ |
3572 | case WXGRID_CURSOR_MOVE_COL: |
3573 | { | |
cd68daf5 VZ |
3574 | int posNew = XToPos(x); |
3575 | int colNew = GetColAt(posNew); | |
d4175745 | 3576 | |
cd68daf5 | 3577 | // determine the position of the drop marker |
d4175745 | 3578 | int markerX; |
cd68daf5 VZ |
3579 | if ( x >= GetColLeft(colNew) + (GetColWidth(colNew) / 2) ) |
3580 | markerX = GetColRight(colNew); | |
d4175745 | 3581 | else |
cd68daf5 | 3582 | markerX = GetColLeft(colNew); |
d4175745 VZ |
3583 | |
3584 | if ( markerX != m_dragLastPos ) | |
3585 | { | |
ad805b9e | 3586 | wxClientDC dc( GetColLabelWindow() ); |
1eab9659 | 3587 | DoPrepareDC(dc); |
d4175745 VZ |
3588 | |
3589 | int cw, ch; | |
ad805b9e | 3590 | GetColLabelWindow()->GetClientSize( &cw, &ch ); |
d4175745 VZ |
3591 | |
3592 | markerX++; | |
3593 | ||
3594 | //Clean up the last indicator | |
3595 | if ( m_dragLastPos >= 0 ) | |
3596 | { | |
ad805b9e | 3597 | wxPen pen( GetColLabelWindow()->GetBackgroundColour(), 2 ); |
d4175745 VZ |
3598 | dc.SetPen(pen); |
3599 | dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch ); | |
3600 | dc.SetPen(wxNullPen); | |
3601 | ||
3602 | if ( XToCol( m_dragLastPos ) != -1 ) | |
3603 | DrawColLabel( dc, XToCol( m_dragLastPos ) ); | |
3604 | } | |
3605 | ||
87c819f9 | 3606 | const wxColour *color; |
d4175745 | 3607 | //Moving to the same place? Don't draw a marker |
cd68daf5 | 3608 | if ( colNew == m_dragRowOrCol ) |
87c819f9 VZ |
3609 | color = wxLIGHT_GREY; |
3610 | else | |
3611 | color = wxBLUE; | |
d4175745 VZ |
3612 | |
3613 | //Draw the marker | |
87c819f9 | 3614 | wxPen pen( *color, 2 ); |
d4175745 VZ |
3615 | dc.SetPen(pen); |
3616 | ||
3617 | dc.DrawLine( markerX, 0, markerX, ch ); | |
3618 | ||
3619 | dc.SetPen(wxNullPen); | |
3620 | ||
3621 | m_dragLastPos = markerX - 1; | |
3622 | } | |
3623 | } | |
3624 | break; | |
3625 | ||
e2b42eeb VZ |
3626 | // default label to suppress warnings about "enumeration value |
3627 | // 'xxx' not handled in switch | |
3628 | default: | |
3629 | break; | |
2d66e025 | 3630 | } |
f85afd4e | 3631 | } |
2d66e025 | 3632 | return; |
f85afd4e | 3633 | } |
2d66e025 | 3634 | |
fe77cf60 JS |
3635 | if ( m_isDragging && (event.Entering() || event.Leaving()) ) |
3636 | return; | |
2d66e025 | 3637 | |
fe77cf60 JS |
3638 | if (m_isDragging) |
3639 | { | |
ad805b9e VZ |
3640 | if (GetColLabelWindow()->HasCapture()) |
3641 | GetColLabelWindow()->ReleaseMouse(); | |
ca65c044 | 3642 | m_isDragging = false; |
fe77cf60 | 3643 | } |
60ff3b99 | 3644 | |
6d004f67 MB |
3645 | // ------------ Entering or leaving the window |
3646 | // | |
3647 | if ( event.Entering() || event.Leaving() ) | |
3648 | { | |
ad805b9e | 3649 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
6d004f67 MB |
3650 | } |
3651 | ||
2d66e025 | 3652 | // ------------ Left button pressed |
f85afd4e | 3653 | // |
6d004f67 | 3654 | else if ( event.LeftDown() ) |
f85afd4e | 3655 | { |
7d1214cd PC |
3656 | int colEdge = XToEdgeOfCol(x); |
3657 | if ( colEdge != wxNOT_FOUND && CanDragColSize(colEdge) ) | |
8f177c8e | 3658 | { |
82edfbe7 VZ |
3659 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow()); |
3660 | } | |
3661 | else // not a request to start resizing | |
3662 | { | |
2f024384 | 3663 | if ( col >= 0 && |
b54ba671 | 3664 | !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) ) |
8f177c8e | 3665 | { |
d4175745 | 3666 | if ( m_canDragColMove ) |
3f3dc2ef | 3667 | { |
d4175745 | 3668 | //Show button as pressed |
ad805b9e | 3669 | wxClientDC dc( GetColLabelWindow() ); |
d4175745 VZ |
3670 | int colLeft = GetColLeft( col ); |
3671 | int colRight = GetColRight( col ) - 1; | |
ad805b9e | 3672 | dc.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) ); |
d4175745 VZ |
3673 | dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 ); |
3674 | dc.DrawLine( colLeft, 1, colRight, 1 ); | |
3675 | ||
ad805b9e | 3676 | ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, GetColLabelWindow()); |
d4175745 VZ |
3677 | } |
3678 | else | |
3679 | { | |
3680 | if ( !event.ShiftDown() && !event.CmdDown() ) | |
3681 | ClearSelection(); | |
3682 | if ( m_selection ) | |
3f3dc2ef | 3683 | { |
d4175745 VZ |
3684 | if ( event.ShiftDown() ) |
3685 | { | |
8b5f6d9d VZ |
3686 | m_selection->SelectBlock |
3687 | ( | |
3688 | 0, m_currentCellCoords.GetCol(), | |
3689 | GetNumberRows() - 1, col, | |
3690 | event | |
3691 | ); | |
d4175745 VZ |
3692 | } |
3693 | else | |
3694 | { | |
8b5f6d9d | 3695 | m_selection->SelectCol(col, event); |
d4175745 | 3696 | } |
3f3dc2ef | 3697 | } |
3f3dc2ef | 3698 | |
ad805b9e | 3699 | ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, GetColLabelWindow()); |
d4175745 | 3700 | } |
f85afd4e | 3701 | } |
2d66e025 | 3702 | } |
2d66e025 | 3703 | } |
f85afd4e | 3704 | |
2d66e025 MB |
3705 | // ------------ Left double click |
3706 | // | |
3707 | if ( event.LeftDClick() ) | |
3708 | { | |
11393d29 VZ |
3709 | const int colEdge = XToEdgeOfCol(x); |
3710 | if ( colEdge == -1 ) | |
2d66e025 | 3711 | { |
a967f048 RG |
3712 | if ( col >= 0 && |
3713 | ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) ) | |
3714 | { | |
ca65c044 | 3715 | // no default action at the moment |
a967f048 | 3716 | } |
2d66e025 | 3717 | } |
d43851f7 JS |
3718 | else |
3719 | { | |
3720 | // adjust column width depending on label text | |
27bb2c8c VZ |
3721 | // |
3722 | // TODO: generate RESIZING event, see #10754 | |
11393d29 | 3723 | AutoSizeColLabelSize( colEdge ); |
d43851f7 | 3724 | |
c5c1ea96 | 3725 | SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, colEdge, event); |
27bb2c8c | 3726 | |
ad805b9e | 3727 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
ccdee36f | 3728 | m_dragLastPos = -1; |
d43851f7 | 3729 | } |
2d66e025 | 3730 | } |
60ff3b99 | 3731 | |
2d66e025 MB |
3732 | // ------------ Left button released |
3733 | // | |
3734 | else if ( event.LeftUp() ) | |
3735 | { | |
d4175745 | 3736 | switch ( m_cursorMode ) |
2d66e025 | 3737 | { |
d4175745 | 3738 | case WXGRID_CURSOR_RESIZE_COL: |
27bb2c8c | 3739 | DoEndDragResizeCol(event); |
852b6c3c | 3740 | break; |
d4175745 VZ |
3741 | |
3742 | case WXGRID_CURSOR_MOVE_COL: | |
11393d29 | 3743 | if ( m_dragLastPos == -1 || col == m_dragRowOrCol ) |
cd68daf5 | 3744 | { |
11393d29 VZ |
3745 | // the column didn't actually move anywhere |
3746 | if ( col != -1 ) | |
3747 | DoColHeaderClick(col); | |
cd68daf5 VZ |
3748 | m_colWindow->Refresh(); // "unpress" the column |
3749 | } | |
3750 | else | |
3751 | { | |
7c331059 VZ |
3752 | // get the position of the column we're over |
3753 | int pos = XToPos(x); | |
3754 | ||
3755 | // we may need to adjust the drop position but don't bother | |
3756 | // checking for it if we can't anyhow | |
3757 | if ( pos > 1 ) | |
3758 | { | |
3759 | // also find the index of the column we're over: notice | |
3760 | // that the existing "col" variable may be invalid but | |
3761 | // we need a valid one here | |
3762 | const int colValid = GetColAt(pos); | |
3763 | ||
3764 | // if we're on the "near" (usually left but right in | |
3765 | // RTL case) part of the column, the actual position we | |
3766 | // should be placed in is actually the one before it | |
18c0369b | 3767 | bool onNearPart; |
7c331059 VZ |
3768 | const int middle = GetColLeft(colValid) + |
3769 | GetColWidth(colValid)/2; | |
3770 | if ( GetLayoutDirection() == wxLayout_LeftToRight ) | |
18c0369b | 3771 | onNearPart = (x <= middle); |
7c331059 | 3772 | else // wxLayout_RightToLeft |
18c0369b | 3773 | onNearPart = (x > middle); |
7c331059 | 3774 | |
18c0369b | 3775 | if ( onNearPart ) |
7c331059 VZ |
3776 | pos--; |
3777 | } | |
3778 | ||
3779 | DoEndMoveCol(pos); | |
cd68daf5 | 3780 | } |
852b6c3c VZ |
3781 | break; |
3782 | ||
3783 | case WXGRID_CURSOR_SELECT_COL: | |
3784 | case WXGRID_CURSOR_SELECT_CELL: | |
3785 | case WXGRID_CURSOR_RESIZE_ROW: | |
3786 | case WXGRID_CURSOR_SELECT_ROW: | |
11393d29 VZ |
3787 | if ( col != -1 ) |
3788 | DoColHeaderClick(col); | |
852b6c3c | 3789 | break; |
2d66e025 | 3790 | } |
f85afd4e | 3791 | |
ad805b9e | 3792 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); |
ccdee36f | 3793 | m_dragLastPos = -1; |
60ff3b99 VZ |
3794 | } |
3795 | ||
2d66e025 MB |
3796 | // ------------ Right button down |
3797 | // | |
3798 | else if ( event.RightDown() ) | |
3799 | { | |
a967f048 | 3800 | if ( col >= 0 && |
ca65c044 | 3801 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) |
2d66e025 MB |
3802 | { |
3803 | // no default action at the moment | |
f85afd4e MB |
3804 | } |
3805 | } | |
60ff3b99 | 3806 | |
2d66e025 | 3807 | // ------------ Right double click |
f85afd4e | 3808 | // |
2d66e025 MB |
3809 | else if ( event.RightDClick() ) |
3810 | { | |
a967f048 | 3811 | if ( col >= 0 && |
ca65c044 | 3812 | !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) |
2d66e025 MB |
3813 | { |
3814 | // no default action at the moment | |
3815 | } | |
3816 | } | |
60ff3b99 | 3817 | |
2d66e025 | 3818 | // ------------ No buttons down and mouse moving |
f85afd4e | 3819 | // |
2d66e025 | 3820 | else if ( event.Moving() ) |
f85afd4e | 3821 | { |
2d66e025 MB |
3822 | m_dragRowOrCol = XToEdgeOfCol( x ); |
3823 | if ( m_dragRowOrCol >= 0 ) | |
f85afd4e | 3824 | { |
2d66e025 | 3825 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
f85afd4e | 3826 | { |
82edfbe7 | 3827 | if ( CanDragColSize(m_dragRowOrCol) ) |
ad805b9e | 3828 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow(), false); |
f85afd4e | 3829 | } |
2d66e025 | 3830 | } |
6d004f67 | 3831 | else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
2d66e025 | 3832 | { |
ad805b9e | 3833 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow(), false); |
8f177c8e | 3834 | } |
f85afd4e MB |
3835 | } |
3836 | } | |
3837 | ||
2d66e025 | 3838 | void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 3839 | { |
2d66e025 | 3840 | if ( event.LeftDown() ) |
f85afd4e | 3841 | { |
2d66e025 MB |
3842 | // indicate corner label by having both row and |
3843 | // col args == -1 | |
f85afd4e | 3844 | // |
b54ba671 | 3845 | if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) ) |
2d66e025 MB |
3846 | { |
3847 | SelectAll(); | |
3848 | } | |
f85afd4e | 3849 | } |
2d66e025 MB |
3850 | else if ( event.LeftDClick() ) |
3851 | { | |
b54ba671 | 3852 | SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event ); |
2d66e025 | 3853 | } |
2d66e025 | 3854 | else if ( event.RightDown() ) |
f85afd4e | 3855 | { |
b54ba671 | 3856 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) ) |
f85afd4e | 3857 | { |
2d66e025 MB |
3858 | // no default action at the moment |
3859 | } | |
3860 | } | |
2d66e025 MB |
3861 | else if ( event.RightDClick() ) |
3862 | { | |
b54ba671 | 3863 | if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) ) |
2d66e025 MB |
3864 | { |
3865 | // no default action at the moment | |
3866 | } | |
3867 | } | |
3868 | } | |
f85afd4e | 3869 | |
86033c4b VZ |
3870 | void wxGrid::CancelMouseCapture() |
3871 | { | |
3872 | // cancel operation currently in progress, whatever it is | |
3873 | if ( m_winCapture ) | |
3874 | { | |
3875 | m_isDragging = false; | |
8a3e536c VZ |
3876 | m_startDragPos = wxDefaultPosition; |
3877 | ||
86033c4b VZ |
3878 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
3879 | m_winCapture->SetCursor( *wxSTANDARD_CURSOR ); | |
3880 | m_winCapture = NULL; | |
3881 | ||
3882 | // remove traces of whatever we drew on screen | |
3883 | Refresh(); | |
3884 | } | |
3885 | } | |
3886 | ||
e2b42eeb VZ |
3887 | void wxGrid::ChangeCursorMode(CursorMode mode, |
3888 | wxWindow *win, | |
3889 | bool captureMouse) | |
3890 | { | |
711f12ef | 3891 | #if wxUSE_LOG_TRACE |
a243da29 | 3892 | static const wxChar *const cursorModes[] = |
e2b42eeb | 3893 | { |
9a83f860 VZ |
3894 | wxT("SELECT_CELL"), |
3895 | wxT("RESIZE_ROW"), | |
3896 | wxT("RESIZE_COL"), | |
3897 | wxT("SELECT_ROW"), | |
3898 | wxT("SELECT_COL"), | |
3899 | wxT("MOVE_COL"), | |
e2b42eeb VZ |
3900 | }; |
3901 | ||
9a83f860 VZ |
3902 | wxLogTrace(wxT("grid"), |
3903 | wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"), | |
3904 | win == m_colWindow ? wxT("colLabelWin") | |
3905 | : win ? wxT("rowLabelWin") | |
3906 | : wxT("gridWin"), | |
e2b42eeb | 3907 | cursorModes[m_cursorMode], cursorModes[mode]); |
711f12ef | 3908 | #endif // wxUSE_LOG_TRACE |
e2b42eeb | 3909 | |
faec5a43 SN |
3910 | if ( mode == m_cursorMode && |
3911 | win == m_winCapture && | |
3912 | captureMouse == (m_winCapture != NULL)) | |
e2b42eeb VZ |
3913 | return; |
3914 | ||
3915 | if ( !win ) | |
3916 | { | |
3917 | // by default use the grid itself | |
3918 | win = m_gridWin; | |
3919 | } | |
3920 | ||
3921 | if ( m_winCapture ) | |
3922 | { | |
e882d288 | 3923 | m_winCapture->ReleaseMouse(); |
10a4531d | 3924 | m_winCapture = NULL; |
e2b42eeb VZ |
3925 | } |
3926 | ||
3927 | m_cursorMode = mode; | |
3928 | ||
3929 | switch ( m_cursorMode ) | |
3930 | { | |
3931 | case WXGRID_CURSOR_RESIZE_ROW: | |
3932 | win->SetCursor( m_rowResizeCursor ); | |
3933 | break; | |
3934 | ||
3935 | case WXGRID_CURSOR_RESIZE_COL: | |
3936 | win->SetCursor( m_colResizeCursor ); | |
3937 | break; | |
3938 | ||
d4175745 VZ |
3939 | case WXGRID_CURSOR_MOVE_COL: |
3940 | win->SetCursor( wxCursor(wxCURSOR_HAND) ); | |
3941 | break; | |
3942 | ||
e2b42eeb VZ |
3943 | default: |
3944 | win->SetCursor( *wxSTANDARD_CURSOR ); | |
2f024384 | 3945 | break; |
e2b42eeb VZ |
3946 | } |
3947 | ||
3948 | // we need to capture mouse when resizing | |
3949 | bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW || | |
3950 | m_cursorMode == WXGRID_CURSOR_RESIZE_COL; | |
3951 | ||
3952 | if ( captureMouse && resize ) | |
3953 | { | |
3954 | win->CaptureMouse(); | |
3955 | m_winCapture = win; | |
3956 | } | |
3957 | } | |
8f177c8e | 3958 | |
8a3e536c VZ |
3959 | // ---------------------------------------------------------------------------- |
3960 | // grid mouse event processing | |
3961 | // ---------------------------------------------------------------------------- | |
60ff3b99 | 3962 | |
0cbcb12d | 3963 | bool |
8a3e536c VZ |
3964 | wxGrid::DoGridCellDrag(wxMouseEvent& event, |
3965 | const wxGridCellCoords& coords, | |
3966 | bool isFirstDrag) | |
3967 | { | |
0cbcb12d SC |
3968 | bool performDefault = true ; |
3969 | ||
8a3e536c | 3970 | if ( coords == wxGridNoCellCoords ) |
0cbcb12d | 3971 | return performDefault; // we're outside any valid cell |
60ff3b99 | 3972 | |
8a3e536c VZ |
3973 | // Hide the edit control, so it won't interfere with drag-shrinking. |
3974 | if ( IsCellEditControlShown() ) | |
27f35b66 | 3975 | { |
8a3e536c VZ |
3976 | HideCellEditControl(); |
3977 | SaveEditControlValue(); | |
27f35b66 SN |
3978 | } |
3979 | ||
8a3e536c | 3980 | switch ( event.GetModifiers() ) |
2d66e025 | 3981 | { |
3bc6d534 | 3982 | case wxMOD_CONTROL: |
8a3e536c VZ |
3983 | if ( m_selectedBlockCorner == wxGridNoCellCoords) |
3984 | m_selectedBlockCorner = coords; | |
3985 | UpdateBlockBeingSelected(m_selectedBlockCorner, coords); | |
3986 | break; | |
07296f0b | 3987 | |
8a3e536c VZ |
3988 | case wxMOD_NONE: |
3989 | if ( CanDragCell() ) | |
2d66e025 | 3990 | { |
8a3e536c | 3991 | if ( isFirstDrag ) |
79dbea21 | 3992 | { |
8a3e536c VZ |
3993 | if ( m_selectedBlockCorner == wxGridNoCellCoords) |
3994 | m_selectedBlockCorner = coords; | |
07296f0b | 3995 | |
0cbcb12d | 3996 | // if event is handled by user code, no further processing |
c118fa5f | 3997 | if ( SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event) != 0 ) |
0cbcb12d SC |
3998 | performDefault = false; |
3999 | ||
4000 | return performDefault; | |
07296f0b | 4001 | } |
2d66e025 | 4002 | } |
25107357 | 4003 | |
8a3e536c VZ |
4004 | UpdateBlockBeingSelected(m_currentCellCoords, coords); |
4005 | break; | |
c71b2126 | 4006 | |
8a3e536c VZ |
4007 | default: |
4008 | // we don't handle the other key modifiers | |
4009 | event.Skip(); | |
4010 | } | |
0cbcb12d SC |
4011 | |
4012 | return performDefault; | |
8a3e536c | 4013 | } |
8f177c8e | 4014 | |
8a3e536c VZ |
4015 | void wxGrid::DoGridLineDrag(wxMouseEvent& event, const wxGridOperations& oper) |
4016 | { | |
4017 | wxClientDC dc(m_gridWin); | |
4018 | PrepareDC(dc); | |
4019 | dc.SetLogicalFunction(wxINVERT); | |
e2b42eeb | 4020 | |
8a3e536c VZ |
4021 | const wxRect rectWin(CalcUnscrolledPosition(wxPoint(0, 0)), |
4022 | m_gridWin->GetClientSize()); | |
4023 | ||
4024 | // erase the previously drawn line, if any | |
4025 | if ( m_dragLastPos >= 0 ) | |
4026 | oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos); | |
4027 | ||
4028 | // we need the vertical position for rows and horizontal for columns here | |
4029 | m_dragLastPos = oper.Dual().Select(CalcUnscrolledPosition(event.GetPosition())); | |
4030 | ||
4031 | // don't allow resizing beneath the minimal size | |
4032 | const int posMin = oper.GetLineStartPos(this, m_dragRowOrCol) + | |
4033 | oper.GetMinimalLineSize(this, m_dragRowOrCol); | |
4034 | if ( m_dragLastPos < posMin ) | |
4035 | m_dragLastPos = posMin; | |
4036 | ||
4037 | // and draw it at the new position | |
4038 | oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos); | |
4039 | } | |
4040 | ||
4041 | void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords) | |
4042 | { | |
4043 | if ( !m_isDragging ) | |
4044 | { | |
4045 | // Don't start doing anything until the mouse has been dragged far | |
4046 | // enough | |
4047 | const wxPoint& pt = event.GetPosition(); | |
4048 | if ( m_startDragPos == wxDefaultPosition ) | |
4049 | { | |
4050 | m_startDragPos = pt; | |
4051 | return; | |
6d004f67 | 4052 | } |
e2b42eeb | 4053 | |
8a3e536c VZ |
4054 | if ( abs(m_startDragPos.x - pt.x) <= DRAG_SENSITIVITY && |
4055 | abs(m_startDragPos.y - pt.y) <= DRAG_SENSITIVITY ) | |
4056 | return; | |
2d66e025 | 4057 | } |
66242c80 | 4058 | |
8a3e536c VZ |
4059 | const bool isFirstDrag = !m_isDragging; |
4060 | m_isDragging = true; | |
07296f0b | 4061 | |
8a3e536c | 4062 | switch ( m_cursorMode ) |
a5777624 | 4063 | { |
8a3e536c | 4064 | case WXGRID_CURSOR_SELECT_CELL: |
0cbcb12d SC |
4065 | // no further handling if handled by user |
4066 | if ( DoGridCellDrag(event, coords, isFirstDrag) == false ) | |
4067 | return; | |
8a3e536c VZ |
4068 | break; |
4069 | ||
4070 | case WXGRID_CURSOR_RESIZE_ROW: | |
4071 | DoGridLineDrag(event, wxGridRowOperations()); | |
4072 | break; | |
4073 | ||
4074 | case WXGRID_CURSOR_RESIZE_COL: | |
4075 | DoGridLineDrag(event, wxGridColumnOperations()); | |
4076 | break; | |
4077 | ||
4078 | default: | |
4079 | event.Skip(); | |
a5777624 | 4080 | } |
e2b42eeb | 4081 | |
8a3e536c | 4082 | if ( isFirstDrag ) |
f6bcfd97 | 4083 | { |
98e95650 VZ |
4084 | wxASSERT_MSG( !m_winCapture, "shouldn't capture the mouse twice" ); |
4085 | ||
8a3e536c VZ |
4086 | m_winCapture = m_gridWin; |
4087 | m_winCapture->CaptureMouse(); | |
4088 | } | |
4089 | } | |
a5777624 | 4090 | |
8a3e536c VZ |
4091 | void |
4092 | wxGrid::DoGridCellLeftDown(wxMouseEvent& event, | |
4093 | const wxGridCellCoords& coords, | |
4094 | const wxPoint& pos) | |
4095 | { | |
4096 | if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK, coords, event) ) | |
4097 | { | |
4098 | // event handled by user code, no need to do anything here | |
4099 | return; | |
a5777624 | 4100 | } |
f85afd4e | 4101 | |
8a3e536c VZ |
4102 | if ( !event.CmdDown() ) |
4103 | ClearSelection(); | |
4104 | ||
4105 | if ( event.ShiftDown() ) | |
4106 | { | |
4107 | if ( m_selection ) | |
4108 | { | |
8b5f6d9d | 4109 | m_selection->SelectBlock(m_currentCellCoords, coords, event); |
8a3e536c VZ |
4110 | m_selectedBlockCorner = coords; |
4111 | } | |
4112 | } | |
4113 | else if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 ) | |
a5777624 RD |
4114 | { |
4115 | DisableCellEditControl(); | |
8a3e536c | 4116 | MakeCellVisible( coords ); |
a5777624 | 4117 | |
8a3e536c | 4118 | if ( event.CmdDown() ) |
58dd5b3b | 4119 | { |
8a3e536c | 4120 | if ( m_selection ) |
1ef49ab5 | 4121 | { |
8b5f6d9d | 4122 | m_selection->ToggleCellSelection(coords, event); |
1ef49ab5 | 4123 | } |
8b5f6d9d | 4124 | |
8a3e536c VZ |
4125 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
4126 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
4127 | m_selectedBlockCorner = coords; | |
4128 | } | |
4129 | else | |
4130 | { | |
e196db7b VZ |
4131 | if ( m_selection ) |
4132 | { | |
4133 | // In row or column selection mode just clicking on the cell | |
4134 | // should select the row or column containing it: this is more | |
4135 | // convenient for the kinds of controls that use such selection | |
4136 | // mode and is compatible with 2.8 behaviour (see #12062). | |
4137 | switch ( m_selection->GetSelectionMode() ) | |
4138 | { | |
4139 | case wxGridSelectCells: | |
4140 | case wxGridSelectRowsOrColumns: | |
4141 | // nothing to do in these cases | |
4142 | break; | |
4143 | ||
4144 | case wxGridSelectRows: | |
4145 | m_selection->SelectRow(coords.GetRow()); | |
4146 | break; | |
4147 | ||
4148 | case wxGridSelectColumns: | |
4149 | m_selection->SelectCol(coords.GetCol()); | |
4150 | break; | |
4151 | } | |
4152 | } | |
4153 | ||
8a3e536c VZ |
4154 | m_waitForSlowClick = m_currentCellCoords == coords && |
4155 | coords != wxGridNoCellCoords; | |
4156 | SetCurrentCell( coords ); | |
58dd5b3b | 4157 | } |
a5777624 | 4158 | } |
8a3e536c | 4159 | } |
f85afd4e | 4160 | |
8a3e536c VZ |
4161 | void |
4162 | wxGrid::DoGridCellLeftDClick(wxMouseEvent& event, | |
4163 | const wxGridCellCoords& coords, | |
4164 | const wxPoint& pos) | |
4165 | { | |
4166 | if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 ) | |
a5777624 | 4167 | { |
8a3e536c | 4168 | if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK, coords, event) ) |
f85afd4e | 4169 | { |
8a3e536c VZ |
4170 | // we want double click to select a cell and start editing |
4171 | // (i.e. to behave in same way as sequence of two slow clicks): | |
4172 | m_waitForSlowClick = true; | |
4173 | } | |
4174 | } | |
4175 | } | |
932b55d0 | 4176 | |
8a3e536c VZ |
4177 | void |
4178 | wxGrid::DoGridCellLeftUp(wxMouseEvent& event, const wxGridCellCoords& coords) | |
4179 | { | |
4180 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) | |
4181 | { | |
4182 | if (m_winCapture) | |
4183 | { | |
e882d288 | 4184 | m_winCapture->ReleaseMouse(); |
8a3e536c VZ |
4185 | m_winCapture = NULL; |
4186 | } | |
932b55d0 | 4187 | |
8a3e536c VZ |
4188 | if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() ) |
4189 | { | |
4190 | ClearSelection(); | |
4191 | EnableCellEditControl(); | |
3f3dc2ef | 4192 | |
8a3e536c VZ |
4193 | wxGridCellAttr *attr = GetCellAttr(coords); |
4194 | wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol()); | |
4195 | editor->StartingClick(); | |
4196 | editor->DecRef(); | |
4197 | attr->DecRef(); | |
2d66e025 | 4198 | |
8a3e536c | 4199 | m_waitForSlowClick = false; |
a5777624 | 4200 | } |
8a3e536c VZ |
4201 | else if ( m_selectedBlockTopLeft != wxGridNoCellCoords && |
4202 | m_selectedBlockBottomRight != wxGridNoCellCoords ) | |
a5777624 | 4203 | { |
8a3e536c VZ |
4204 | if ( m_selection ) |
4205 | { | |
4206 | m_selection->SelectBlock( m_selectedBlockTopLeft, | |
4207 | m_selectedBlockBottomRight, | |
8b5f6d9d | 4208 | event ); |
8a3e536c | 4209 | } |
e2b42eeb | 4210 | |
8a3e536c VZ |
4211 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
4212 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
e2b42eeb | 4213 | |
8a3e536c VZ |
4214 | // Show the edit control, if it has been hidden for |
4215 | // drag-shrinking. | |
4216 | ShowCellEditControl(); | |
58dd5b3b | 4217 | } |
8a3e536c VZ |
4218 | } |
4219 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) | |
4220 | { | |
4221 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
27bb2c8c | 4222 | DoEndDragResizeRow(event); |
a5777624 | 4223 | } |
8a3e536c VZ |
4224 | else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) |
4225 | { | |
4226 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
27bb2c8c | 4227 | DoEndDragResizeCol(event); |
8a3e536c VZ |
4228 | } |
4229 | ||
4230 | m_dragLastPos = -1; | |
4231 | } | |
4232 | ||
4233 | void | |
4234 | wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event), | |
4235 | const wxGridCellCoords& coords, | |
4236 | const wxPoint& pos) | |
4237 | { | |
4238 | if ( coords.GetRow() < 0 || coords.GetCol() < 0 ) | |
a5777624 | 4239 | { |
8a3e536c VZ |
4240 | // out of grid cell area |
4241 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); | |
4242 | return; | |
a5777624 | 4243 | } |
2d66e025 | 4244 | |
8a3e536c VZ |
4245 | int dragRow = YToEdgeOfRow( pos.y ); |
4246 | int dragCol = XToEdgeOfCol( pos.x ); | |
4247 | ||
4248 | // Dragging on the corner of a cell to resize in both | |
4249 | // directions is not implemented yet... | |
a5777624 | 4250 | // |
8a3e536c | 4251 | if ( dragRow >= 0 && dragCol >= 0 ) |
a5777624 | 4252 | { |
8a3e536c VZ |
4253 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); |
4254 | return; | |
4255 | } | |
4256 | ||
82edfbe7 | 4257 | if ( dragRow >= 0 && CanDragGridSize() && CanDragRowSize(dragRow) ) |
8a3e536c | 4258 | { |
8a3e536c | 4259 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
f85afd4e | 4260 | { |
82edfbe7 VZ |
4261 | m_dragRowOrCol = dragRow; |
4262 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false); | |
60ff3b99 | 4263 | } |
a5777624 | 4264 | } |
ad805b9e VZ |
4265 | // When using the native header window we can only resize the columns by |
4266 | // dragging the dividers in it because we can't make it enter into the | |
4267 | // column resizing mode programmatically | |
82edfbe7 VZ |
4268 | else if ( dragCol >= 0 && !m_useNativeHeader && |
4269 | CanDragGridSize() && CanDragColSize(dragCol) ) | |
8a3e536c | 4270 | { |
8a3e536c VZ |
4271 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
4272 | { | |
82edfbe7 VZ |
4273 | m_dragRowOrCol = dragCol; |
4274 | ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false); | |
8a3e536c VZ |
4275 | } |
4276 | } | |
4277 | else // Neither on a row or col edge | |
a5777624 | 4278 | { |
8a3e536c | 4279 | if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) |
d57ad377 | 4280 | { |
d57ad377 | 4281 | ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); |
d57ad377 | 4282 | } |
8a3e536c VZ |
4283 | } |
4284 | } | |
d57ad377 | 4285 | |
8a3e536c VZ |
4286 | void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event) |
4287 | { | |
98e95650 VZ |
4288 | if ( event.Entering() || event.Leaving() ) |
4289 | { | |
4290 | // we don't care about these events but we must not reset m_isDragging | |
4291 | // if they happen so return before anything else is done | |
4292 | event.Skip(); | |
4293 | return; | |
4294 | } | |
4295 | ||
8a3e536c | 4296 | const wxPoint pos = CalcUnscrolledPosition(event.GetPosition()); |
790cc417 | 4297 | |
8a3e536c VZ |
4298 | // coordinates of the cell under mouse |
4299 | wxGridCellCoords coords = XYToCell(pos); | |
6d004f67 | 4300 | |
8a3e536c VZ |
4301 | int cell_rows, cell_cols; |
4302 | GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols ); | |
4303 | if ( (cell_rows < 0) || (cell_cols < 0) ) | |
4304 | { | |
4305 | coords.SetRow(coords.GetRow() + cell_rows); | |
4306 | coords.SetCol(coords.GetCol() + cell_cols); | |
4307 | } | |
6d004f67 | 4308 | |
8a3e536c VZ |
4309 | if ( event.Dragging() ) |
4310 | { | |
4311 | if ( event.LeftIsDown() ) | |
4312 | DoGridDragEvent(event, coords); | |
4313 | else | |
4314 | event.Skip(); | |
4315 | return; | |
4316 | } | |
4317 | ||
4318 | m_isDragging = false; | |
4319 | m_startDragPos = wxDefaultPosition; | |
4320 | ||
8a3e536c VZ |
4321 | // deal with various button presses |
4322 | if ( event.IsButton() ) | |
4323 | { | |
4324 | if ( coords != wxGridNoCellCoords ) | |
a5777624 | 4325 | { |
8a3e536c | 4326 | DisableCellEditControl(); |
e2b42eeb | 4327 | |
8a3e536c VZ |
4328 | if ( event.LeftDown() ) |
4329 | DoGridCellLeftDown(event, coords, pos); | |
4330 | else if ( event.LeftDClick() ) | |
4331 | DoGridCellLeftDClick(event, coords, pos); | |
4332 | else if ( event.RightDown() ) | |
4333 | SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK, coords, event); | |
4334 | else if ( event.RightDClick() ) | |
4335 | SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK, coords, event); | |
6d004f67 | 4336 | } |
8a3e536c VZ |
4337 | |
4338 | // this one should be called even if we're not over any cell | |
4339 | if ( event.LeftUp() ) | |
a5777624 | 4340 | { |
8a3e536c | 4341 | DoGridCellLeftUp(event, coords); |
a5777624 RD |
4342 | } |
4343 | } | |
8a3e536c VZ |
4344 | else if ( event.Moving() ) |
4345 | { | |
4346 | DoGridMouseMoveEvent(event, coords, pos); | |
4347 | } | |
4348 | else // unknown mouse event? | |
4349 | { | |
4350 | event.Skip(); | |
4351 | } | |
6d004f67 MB |
4352 | } |
4353 | ||
27bb2c8c VZ |
4354 | // this function returns true only if the size really changed |
4355 | bool wxGrid::DoEndDragResizeLine(const wxGridOperations& oper) | |
6d004f67 | 4356 | { |
bec70262 | 4357 | if ( m_dragLastPos == -1 ) |
27bb2c8c | 4358 | return false; |
6d004f67 | 4359 | |
bec70262 | 4360 | const wxGridOperations& doper = oper.Dual(); |
6d004f67 | 4361 | |
bec70262 | 4362 | const wxSize size = m_gridWin->GetClientSize(); |
e2b42eeb | 4363 | |
bec70262 | 4364 | const wxPoint ptOrigin = CalcUnscrolledPosition(wxPoint(0, 0)); |
ccdee36f | 4365 | |
bec70262 VZ |
4366 | // erase the last line we drew |
4367 | wxClientDC dc(m_gridWin); | |
4368 | PrepareDC(dc); | |
4369 | dc.SetLogicalFunction(wxINVERT); | |
6d004f67 | 4370 | |
bec70262 VZ |
4371 | const int posLineStart = oper.Select(ptOrigin); |
4372 | const int posLineEnd = oper.Select(ptOrigin) + oper.Select(size); | |
6d004f67 | 4373 | |
bec70262 | 4374 | oper.DrawParallelLine(dc, posLineStart, posLineEnd, m_dragLastPos); |
6d004f67 | 4375 | |
bec70262 VZ |
4376 | // temporarily hide the edit control before resizing |
4377 | HideCellEditControl(); | |
4378 | SaveEditControlValue(); | |
4379 | ||
4380 | // do resize the line | |
4381 | const int lineStart = oper.GetLineStartPos(this, m_dragRowOrCol); | |
27bb2c8c | 4382 | const int lineSizeOld = oper.GetLineSize(this, m_dragRowOrCol); |
bec70262 VZ |
4383 | oper.SetLineSize(this, m_dragRowOrCol, |
4384 | wxMax(m_dragLastPos - lineStart, | |
4385 | oper.GetMinimalLineSize(this, m_dragRowOrCol))); | |
27bb2c8c VZ |
4386 | const bool |
4387 | sizeChanged = oper.GetLineSize(this, m_dragRowOrCol) != lineSizeOld; | |
bec70262 | 4388 | |
ad805b9e VZ |
4389 | m_dragLastPos = -1; |
4390 | ||
bec70262 VZ |
4391 | // refresh now if we're not frozen |
4392 | if ( !GetBatchCount() ) | |
6d004f67 | 4393 | { |
bec70262 VZ |
4394 | // we need to refresh everything beyond the resized line in the header |
4395 | // window | |
6d004f67 | 4396 | |
bec70262 VZ |
4397 | // get the position from which to refresh in the other direction |
4398 | wxRect rect(CellToRect(oper.MakeCoords(m_dragRowOrCol, 0))); | |
4399 | rect.SetPosition(CalcScrolledPosition(rect.GetPosition())); | |
6d004f67 | 4400 | |
bec70262 VZ |
4401 | // we only need the ordinate (for rows) or abscissa (for columns) here, |
4402 | // and need to cover the entire window in the other direction | |
4403 | oper.Select(rect) = 0; | |
6d004f67 | 4404 | |
bec70262 VZ |
4405 | wxRect rectHeader(rect.GetPosition(), |
4406 | oper.MakeSize | |
4407 | ( | |
4408 | oper.GetHeaderWindowSize(this), | |
4409 | doper.Select(size) - doper.Select(rect) | |
4410 | )); | |
4411 | ||
4412 | oper.GetHeaderWindow(this)->Refresh(true, &rectHeader); | |
4413 | ||
4414 | ||
4415 | // also refresh the grid window: extend the rectangle | |
4416 | if ( m_table ) | |
6d004f67 | 4417 | { |
bec70262 | 4418 | oper.SelectSize(rect) = oper.Select(size); |
2f024384 | 4419 | |
bec70262 | 4420 | int subtractLines = 0; |
7d1214cd PC |
4421 | int line = doper.PosToLine(this, posLineStart); |
4422 | if ( line >= 0 ) | |
27f35b66 | 4423 | { |
bec70262 VZ |
4424 | // ensure that if we have a multi-cell block we redraw all of |
4425 | // it by increasing the refresh area to cover it entirely if a | |
4426 | // part of it is affected | |
13350621 | 4427 | const int lineEnd = doper.PosToLine(this, posLineEnd, true); |
7d1214cd | 4428 | for ( ; line < lineEnd; line++ ) |
27f35b66 | 4429 | { |
bec70262 VZ |
4430 | int cellLines = oper.Select( |
4431 | GetCellSize(oper.MakeCoords(m_dragRowOrCol, line))); | |
4432 | if ( cellLines < subtractLines ) | |
4433 | subtractLines = cellLines; | |
27f35b66 SN |
4434 | } |
4435 | } | |
2f024384 | 4436 | |
bec70262 VZ |
4437 | int startPos = |
4438 | oper.GetLineStartPos(this, m_dragRowOrCol + subtractLines); | |
4439 | startPos = doper.CalcScrolledPosition(this, startPos); | |
4440 | ||
4441 | doper.Select(rect) = startPos; | |
4442 | doper.SelectSize(rect) = doper.Select(size) - startPos; | |
e2b42eeb | 4443 | |
bec70262 VZ |
4444 | m_gridWin->Refresh(false, &rect); |
4445 | } | |
f85afd4e | 4446 | } |
bec70262 VZ |
4447 | |
4448 | // show the edit control back again | |
4449 | ShowCellEditControl(); | |
27bb2c8c VZ |
4450 | |
4451 | return sizeChanged; | |
bec70262 VZ |
4452 | } |
4453 | ||
27bb2c8c | 4454 | void wxGrid::DoEndDragResizeRow(const wxMouseEvent& event) |
bec70262 | 4455 | { |
27bb2c8c VZ |
4456 | // TODO: generate RESIZING event, see #10754 |
4457 | ||
4458 | if ( DoEndDragResizeLine(wxGridRowOperations()) ) | |
c5c1ea96 | 4459 | SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event); |
bec70262 VZ |
4460 | } |
4461 | ||
27bb2c8c | 4462 | void wxGrid::DoEndDragResizeCol(const wxMouseEvent& event) |
bec70262 | 4463 | { |
27bb2c8c | 4464 | // TODO: generate RESIZING event, see #10754 |
ad805b9e | 4465 | |
27bb2c8c | 4466 | if ( DoEndDragResizeLine(wxGridColumnOperations()) ) |
c5c1ea96 | 4467 | SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event); |
f85afd4e MB |
4468 | } |
4469 | ||
cd68daf5 | 4470 | void wxGrid::DoStartMoveCol(int col) |
d4175745 | 4471 | { |
cd68daf5 VZ |
4472 | m_dragRowOrCol = col; |
4473 | } | |
d4175745 | 4474 | |
cd68daf5 VZ |
4475 | void wxGrid::DoEndMoveCol(int pos) |
4476 | { | |
4477 | wxASSERT_MSG( m_dragRowOrCol != -1, "no matching DoStartMoveCol?" ); | |
4478 | ||
4479 | if ( SendEvent(wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol) != -1 ) | |
4480 | SetColPos(m_dragRowOrCol, pos); | |
4481 | //else: vetoed by user | |
d4175745 | 4482 | |
cd68daf5 | 4483 | m_dragRowOrCol = -1; |
d4175745 VZ |
4484 | } |
4485 | ||
4797b014 | 4486 | void wxGrid::RefreshAfterColPosChange() |
009c7216 | 4487 | { |
4797b014 VZ |
4488 | // recalculate the column rights as the column positions have changed, |
4489 | // unless we calculate them dynamically because all columns widths are the | |
4490 | // same and it's easy to do | |
4491 | if ( !m_colWidths.empty() ) | |
009c7216 | 4492 | { |
4797b014 VZ |
4493 | int colRight = 0; |
4494 | for ( int colPos = 0; colPos < m_numCols; colPos++ ) | |
4495 | { | |
4496 | int colID = GetColAt( colPos ); | |
4497 | ||
4498 | colRight += m_colWidths[colID]; | |
4499 | m_colRights[colID] = colRight; | |
4500 | } | |
4501 | } | |
009c7216 | 4502 | |
4797b014 VZ |
4503 | // and make the changes visible |
4504 | if ( m_useNativeHeader ) | |
4505 | { | |
4506 | if ( m_colAt.empty() ) | |
3039ade9 | 4507 | GetGridColHeader()->ResetColumnsOrder(); |
4797b014 | 4508 | else |
3039ade9 | 4509 | GetGridColHeader()->SetColumnsOrder(m_colAt); |
4797b014 VZ |
4510 | } |
4511 | else | |
4512 | { | |
4513 | m_colWindow->Refresh(); | |
009c7216 | 4514 | } |
4797b014 | 4515 | m_gridWin->Refresh(); |
009c7216 VZ |
4516 | } |
4517 | ||
31ec8b4e VZ |
4518 | void wxGrid::SetColumnsOrder(const wxArrayInt& order) |
4519 | { | |
4520 | m_colAt = order; | |
4521 | ||
4522 | RefreshAfterColPosChange(); | |
4523 | } | |
4524 | ||
3169a8e8 | 4525 | void wxGrid::SetColPos(int idx, int pos) |
d4175745 | 4526 | { |
1bb74626 | 4527 | // we're going to need m_colAt now, initialize it if needed |
3169a8e8 | 4528 | if ( m_colAt.empty() ) |
d4175745 | 4529 | { |
3169a8e8 VZ |
4530 | m_colAt.reserve(m_numCols); |
4531 | for ( int i = 0; i < m_numCols; i++ ) | |
4532 | m_colAt.push_back(i); | |
d4175745 VZ |
4533 | } |
4534 | ||
1bb74626 | 4535 | wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos); |
d4175745 | 4536 | |
4797b014 | 4537 | RefreshAfterColPosChange(); |
d4175745 VZ |
4538 | } |
4539 | ||
009c7216 VZ |
4540 | void wxGrid::ResetColPos() |
4541 | { | |
4542 | m_colAt.clear(); | |
da5a641f | 4543 | |
4797b014 | 4544 | RefreshAfterColPosChange(); |
009c7216 | 4545 | } |
d4175745 VZ |
4546 | |
4547 | void wxGrid::EnableDragColMove( bool enable ) | |
4548 | { | |
4549 | if ( m_canDragColMove == enable ) | |
4550 | return; | |
4551 | ||
009c7216 | 4552 | if ( m_useNativeHeader ) |
d4175745 | 4553 | { |
009c7216 | 4554 | // update all columns to make them [not] reorderable |
3039ade9 | 4555 | GetGridColHeader()->SetColumnCount(m_numCols); |
009c7216 | 4556 | } |
d4175745 | 4557 | |
009c7216 | 4558 | m_canDragColMove = enable; |
d4175745 | 4559 | |
009c7216 VZ |
4560 | // we use to call ResetColPos() from here if !enable but this doesn't seem |
4561 | // right as it would mean there would be no way to "freeze" the current | |
4562 | // columns order by disabling moving them after putting them in the desired | |
4563 | // order, whereas now you can always call ResetColPos() manually if needed | |
d4175745 VZ |
4564 | } |
4565 | ||
4566 | ||
2d66e025 MB |
4567 | // |
4568 | // ------ interaction with data model | |
4569 | // | |
4570 | bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg ) | |
f85afd4e | 4571 | { |
2d66e025 | 4572 | switch ( msg.GetId() ) |
17732cec | 4573 | { |
2d66e025 MB |
4574 | case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES: |
4575 | return GetModelValues(); | |
17732cec | 4576 | |
2d66e025 MB |
4577 | case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES: |
4578 | return SetModelValues(); | |
f85afd4e | 4579 | |
2d66e025 MB |
4580 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: |
4581 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
4582 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
4583 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
4584 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
4585 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
4586 | return Redimension( msg ); | |
4587 | ||
4588 | default: | |
ca65c044 | 4589 | return false; |
f85afd4e | 4590 | } |
2d66e025 | 4591 | } |
f85afd4e | 4592 | |
2d66e025 | 4593 | // The behaviour of this function depends on the grid table class |
5b061713 | 4594 | // Clear() function. For the default wxGridStringTable class the |
10a4531d | 4595 | // behaviour is to replace all cell contents with wxEmptyString but |
2d66e025 MB |
4596 | // not to change the number of rows or cols. |
4597 | // | |
4598 | void wxGrid::ClearGrid() | |
4599 | { | |
4600 | if ( m_table ) | |
f85afd4e | 4601 | { |
4cfa5de6 RD |
4602 | if (IsCellEditControlEnabled()) |
4603 | DisableCellEditControl(); | |
4604 | ||
2d66e025 | 4605 | m_table->Clear(); |
5b061713 | 4606 | if (!GetBatchCount()) |
a9339fe2 | 4607 | m_gridWin->Refresh(); |
f85afd4e MB |
4608 | } |
4609 | } | |
4610 | ||
10a4531d VZ |
4611 | bool |
4612 | wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify)(size_t, size_t), | |
4613 | int pos, int num, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 4614 | { |
10a4531d | 4615 | wxCHECK_MSG( m_created, false, "must finish creating the grid first" ); |
f85afd4e | 4616 | |
10a4531d | 4617 | if ( !m_table ) |
ca65c044 | 4618 | return false; |
f85afd4e | 4619 | |
10a4531d VZ |
4620 | if ( IsCellEditControlEnabled() ) |
4621 | DisableCellEditControl(); | |
b7fff980 | 4622 | |
10a4531d | 4623 | return (m_table->*funcModify)(pos, num); |
2f024384 | 4624 | |
10a4531d VZ |
4625 | // the table will have sent the results of the insert row |
4626 | // operation to this view object as a grid table message | |
f85afd4e MB |
4627 | } |
4628 | ||
10a4531d VZ |
4629 | bool |
4630 | wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend)(size_t), | |
4631 | int num, bool WXUNUSED(updateLabels)) | |
f85afd4e | 4632 | { |
10a4531d | 4633 | wxCHECK_MSG( m_created, false, "must finish creating the grid first" ); |
2f024384 | 4634 | |
10a4531d | 4635 | if ( !m_table ) |
ca65c044 | 4636 | return false; |
f85afd4e | 4637 | |
10a4531d | 4638 | return (m_table->*funcAppend)(num); |
f85afd4e MB |
4639 | } |
4640 | ||
27bb2c8c VZ |
4641 | // ---------------------------------------------------------------------------- |
4642 | // event generation helpers | |
4643 | // ---------------------------------------------------------------------------- | |
4644 | ||
4645 | void | |
c5c1ea96 | 4646 | wxGrid::SendGridSizeEvent(wxEventType type, |
27bb2c8c VZ |
4647 | int row, int col, |
4648 | const wxMouseEvent& mouseEv) | |
4649 | { | |
4650 | int rowOrCol = row == -1 ? col : row; | |
4651 | ||
4652 | wxGridSizeEvent gridEvt( GetId(), | |
4653 | type, | |
4654 | this, | |
4655 | rowOrCol, | |
4656 | mouseEv.GetX() + GetRowLabelSize(), | |
4657 | mouseEv.GetY() + GetColLabelSize(), | |
4658 | mouseEv); | |
4659 | ||
4660 | GetEventHandler()->ProcessEvent(gridEvt); | |
4661 | } | |
8f177c8e | 4662 | |
8a3e536c VZ |
4663 | // Generate a grid event based on a mouse event and return: |
4664 | // -1 if the event was vetoed | |
4665 | // +1 if the event was processed (but not vetoed) | |
4666 | // 0 if the event wasn't handled | |
4667 | int | |
aced1133 | 4668 | wxGrid::SendEvent(const wxEventType type, |
8a3e536c | 4669 | int row, int col, |
27bb2c8c | 4670 | const wxMouseEvent& mouseEv) |
2d66e025 | 4671 | { |
2f024384 | 4672 | bool claimed, vetoed; |
fe7b9ed6 | 4673 | |
27bb2c8c | 4674 | if ( type == wxEVT_GRID_RANGE_SELECT ) |
97a9929e VZ |
4675 | { |
4676 | // Right now, it should _never_ end up here! | |
4677 | wxGridRangeSelectEvent gridEvt( GetId(), | |
4678 | type, | |
4679 | this, | |
8a3e536c VZ |
4680 | m_selectedBlockTopLeft, |
4681 | m_selectedBlockBottomRight, | |
ca65c044 | 4682 | true, |
8b5f6d9d | 4683 | mouseEv); |
97a9929e VZ |
4684 | |
4685 | claimed = GetEventHandler()->ProcessEvent(gridEvt); | |
4686 | vetoed = !gridEvt.IsAllowed(); | |
4687 | } | |
2b73a34e RD |
4688 | else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK || |
4689 | type == wxEVT_GRID_LABEL_LEFT_DCLICK || | |
4690 | type == wxEVT_GRID_LABEL_RIGHT_CLICK || | |
4691 | type == wxEVT_GRID_LABEL_RIGHT_DCLICK ) | |
4692 | { | |
4693 | wxPoint pos = mouseEv.GetPosition(); | |
4694 | ||
4695 | if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() ) | |
4696 | pos.y += GetColLabelSize(); | |
4697 | if ( mouseEv.GetEventObject() == GetGridColLabelWindow() ) | |
4698 | pos.x += GetRowLabelSize(); | |
c71b2126 | 4699 | |
2b73a34e RD |
4700 | wxGridEvent gridEvt( GetId(), |
4701 | type, | |
4702 | this, | |
4703 | row, col, | |
4704 | pos.x, | |
4705 | pos.y, | |
4706 | false, | |
8b5f6d9d | 4707 | mouseEv); |
2b73a34e RD |
4708 | claimed = GetEventHandler()->ProcessEvent(gridEvt); |
4709 | vetoed = !gridEvt.IsAllowed(); | |
4710 | } | |
fe7b9ed6 | 4711 | else |
97a9929e VZ |
4712 | { |
4713 | wxGridEvent gridEvt( GetId(), | |
4714 | type, | |
4715 | this, | |
4716 | row, col, | |
4717 | mouseEv.GetX() + GetRowLabelSize(), | |
4718 | mouseEv.GetY() + GetColLabelSize(), | |
ca65c044 | 4719 | false, |
8b5f6d9d | 4720 | mouseEv); |
0cbcb12d SC |
4721 | |
4722 | if ( type == wxEVT_GRID_CELL_BEGIN_DRAG ) | |
4723 | { | |
4724 | // by default the dragging is not supported, the user code must | |
4725 | // explicitly allow the event for it to take place | |
4726 | gridEvt.Veto(); | |
4727 | } | |
4728 | ||
97a9929e VZ |
4729 | claimed = GetEventHandler()->ProcessEvent(gridEvt); |
4730 | vetoed = !gridEvt.IsAllowed(); | |
4731 | } | |
4732 | ||
4733 | // A Veto'd event may not be `claimed' so test this first | |
4db6714b KH |
4734 | if (vetoed) |
4735 | return -1; | |
4736 | ||
97a9929e | 4737 | return claimed ? 1 : 0; |
f85afd4e MB |
4738 | } |
4739 | ||
8a3e536c | 4740 | // Generate a grid event of specified type, return value same as above |
f85afd4e | 4741 | // |
763163a8 VZ |
4742 | int |
4743 | wxGrid::SendEvent(const wxEventType type, int row, int col, const wxString& s) | |
f85afd4e | 4744 | { |
27bb2c8c VZ |
4745 | wxGridEvent gridEvt( GetId(), type, this, row, col ); |
4746 | gridEvt.SetString(s); | |
8f177c8e | 4747 | |
27bb2c8c | 4748 | const bool claimed = GetEventHandler()->ProcessEvent(gridEvt); |
fe7b9ed6 | 4749 | |
97a9929e | 4750 | // A Veto'd event may not be `claimed' so test this first |
27bb2c8c | 4751 | if ( !gridEvt.IsAllowed() ) |
4db6714b KH |
4752 | return -1; |
4753 | ||
97a9929e | 4754 | return claimed ? 1 : 0; |
f85afd4e MB |
4755 | } |
4756 | ||
2d66e025 | 4757 | void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
f85afd4e | 4758 | { |
3d59537f DS |
4759 | // needed to prevent zillions of paint events on MSW |
4760 | wxPaintDC dc(this); | |
8f177c8e | 4761 | } |
f85afd4e | 4762 | |
bca7bfc8 | 4763 | void wxGrid::Refresh(bool eraseb, const wxRect* rect) |
27f35b66 | 4764 | { |
ad603bf7 VZ |
4765 | // Don't do anything if between Begin/EndBatch... |
4766 | // EndBatch() will do all this on the last nested one anyway. | |
f9549841 | 4767 | if ( m_created && !GetBatchCount() ) |
27f35b66 | 4768 | { |
bca7bfc8 | 4769 | // Refresh to get correct scrolled position: |
4db6714b | 4770 | wxScrolledWindow::Refresh(eraseb, rect); |
27f35b66 | 4771 | |
27f35b66 SN |
4772 | if (rect) |
4773 | { | |
bca7bfc8 SN |
4774 | int rect_x, rect_y, rectWidth, rectHeight; |
4775 | int width_label, width_cell, height_label, height_cell; | |
4776 | int x, y; | |
4777 | ||
2f024384 | 4778 | // Copy rectangle can get scroll offsets.. |
bca7bfc8 SN |
4779 | rect_x = rect->GetX(); |
4780 | rect_y = rect->GetY(); | |
4781 | rectWidth = rect->GetWidth(); | |
4782 | rectHeight = rect->GetHeight(); | |
27f35b66 | 4783 | |
bca7bfc8 | 4784 | width_label = m_rowLabelWidth - rect_x; |
4db6714b KH |
4785 | if (width_label > rectWidth) |
4786 | width_label = rectWidth; | |
27f35b66 | 4787 | |
bca7bfc8 | 4788 | height_label = m_colLabelHeight - rect_y; |
a9339fe2 DS |
4789 | if (height_label > rectHeight) |
4790 | height_label = rectHeight; | |
27f35b66 | 4791 | |
bca7bfc8 SN |
4792 | if (rect_x > m_rowLabelWidth) |
4793 | { | |
4794 | x = rect_x - m_rowLabelWidth; | |
4795 | width_cell = rectWidth; | |
4796 | } | |
4797 | else | |
4798 | { | |
4799 | x = 0; | |
4800 | width_cell = rectWidth - (m_rowLabelWidth - rect_x); | |
4801 | } | |
4802 | ||
4803 | if (rect_y > m_colLabelHeight) | |
4804 | { | |
4805 | y = rect_y - m_colLabelHeight; | |
4806 | height_cell = rectHeight; | |
4807 | } | |
4808 | else | |
4809 | { | |
4810 | y = 0; | |
4811 | height_cell = rectHeight - (m_colLabelHeight - rect_y); | |
4812 | } | |
4813 | ||
4814 | // Paint corner label part intersecting rect. | |
4815 | if ( width_label > 0 && height_label > 0 ) | |
4816 | { | |
4817 | wxRect anotherrect(rect_x, rect_y, width_label, height_label); | |
4818 | m_cornerLabelWin->Refresh(eraseb, &anotherrect); | |
4819 | } | |
4820 | ||
4821 | // Paint col labels part intersecting rect. | |
4822 | if ( width_cell > 0 && height_label > 0 ) | |
4823 | { | |
4824 | wxRect anotherrect(x, rect_y, width_cell, height_label); | |
ad805b9e | 4825 | m_colWindow->Refresh(eraseb, &anotherrect); |
bca7bfc8 SN |
4826 | } |
4827 | ||
4828 | // Paint row labels part intersecting rect. | |
4829 | if ( width_label > 0 && height_cell > 0 ) | |
4830 | { | |
4831 | wxRect anotherrect(rect_x, y, width_label, height_cell); | |
4832 | m_rowLabelWin->Refresh(eraseb, &anotherrect); | |
4833 | } | |
4834 | ||
4835 | // Paint cell area part intersecting rect. | |
4836 | if ( width_cell > 0 && height_cell > 0 ) | |
4837 | { | |
4838 | wxRect anotherrect(x, y, width_cell, height_cell); | |
4839 | m_gridWin->Refresh(eraseb, &anotherrect); | |
4840 | } | |
4841 | } | |
4842 | else | |
4843 | { | |
4844 | m_cornerLabelWin->Refresh(eraseb, NULL); | |
ad805b9e | 4845 | m_colWindow->Refresh(eraseb, NULL); |
bca7bfc8 SN |
4846 | m_rowLabelWin->Refresh(eraseb, NULL); |
4847 | m_gridWin->Refresh(eraseb, NULL); | |
4848 | } | |
27f35b66 SN |
4849 | } |
4850 | } | |
f85afd4e | 4851 | |
c71b2126 | 4852 | void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event)) |
f85afd4e | 4853 | { |
b93aafab JS |
4854 | if (m_targetWindow != this) // check whether initialisation has been done |
4855 | { | |
f1ff7df0 VZ |
4856 | // reposition our children windows |
4857 | CalcWindowSizes(); | |
b93aafab | 4858 | } |
f85afd4e MB |
4859 | } |
4860 | ||
2d66e025 | 4861 | void wxGrid::OnKeyDown( wxKeyEvent& event ) |
f85afd4e | 4862 | { |
2d66e025 | 4863 | if ( m_inOnKeyDown ) |
f85afd4e | 4864 | { |
2d66e025 MB |
4865 | // shouldn't be here - we are going round in circles... |
4866 | // | |
07296f0b | 4867 | wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") ); |
f85afd4e MB |
4868 | } |
4869 | ||
ca65c044 | 4870 | m_inOnKeyDown = true; |
f85afd4e | 4871 | |
2d66e025 | 4872 | // propagate the event up and see if it gets processed |
2d66e025 MB |
4873 | wxWindow *parent = GetParent(); |
4874 | wxKeyEvent keyEvt( event ); | |
4875 | keyEvt.SetEventObject( parent ); | |
4876 | ||
4877 | if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) ) | |
f85afd4e | 4878 | { |
bcb614b3 RR |
4879 | if (GetLayoutDirection() == wxLayout_RightToLeft) |
4880 | { | |
4881 | if (event.GetKeyCode() == WXK_RIGHT) | |
4882 | event.m_keyCode = WXK_LEFT; | |
4883 | else if (event.GetKeyCode() == WXK_LEFT) | |
4884 | event.m_keyCode = WXK_RIGHT; | |
4885 | } | |
c71b2126 | 4886 | |
2d66e025 | 4887 | // try local handlers |
12a3f227 | 4888 | switch ( event.GetKeyCode() ) |
2d66e025 MB |
4889 | { |
4890 | case WXK_UP: | |
4891 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4892 | MoveCursorUpBlock( event.ShiftDown() ); |
2d66e025 | 4893 | else |
5c8fc7c1 | 4894 | MoveCursorUp( event.ShiftDown() ); |
2d66e025 | 4895 | break; |
f85afd4e | 4896 | |
2d66e025 MB |
4897 | case WXK_DOWN: |
4898 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4899 | MoveCursorDownBlock( event.ShiftDown() ); |
2d66e025 | 4900 | else |
5c8fc7c1 | 4901 | MoveCursorDown( event.ShiftDown() ); |
2d66e025 | 4902 | break; |
8f177c8e | 4903 | |
2d66e025 MB |
4904 | case WXK_LEFT: |
4905 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4906 | MoveCursorLeftBlock( event.ShiftDown() ); |
2d66e025 | 4907 | else |
5c8fc7c1 | 4908 | MoveCursorLeft( event.ShiftDown() ); |
2d66e025 MB |
4909 | break; |
4910 | ||
4911 | case WXK_RIGHT: | |
4912 | if ( event.ControlDown() ) | |
5c8fc7c1 | 4913 | MoveCursorRightBlock( event.ShiftDown() ); |
2d66e025 | 4914 | else |
5c8fc7c1 | 4915 | MoveCursorRight( event.ShiftDown() ); |
2d66e025 | 4916 | break; |
b99be8fb | 4917 | |
2d66e025 | 4918 | case WXK_RETURN: |
a4f7bf58 | 4919 | case WXK_NUMPAD_ENTER: |
58dd5b3b MB |
4920 | if ( event.ControlDown() ) |
4921 | { | |
4922 | event.Skip(); // to let the edit control have the return | |
4923 | } | |
4924 | else | |
4925 | { | |
f6bcfd97 BP |
4926 | if ( GetGridCursorRow() < GetNumberRows()-1 ) |
4927 | { | |
4928 | MoveCursorDown( event.ShiftDown() ); | |
4929 | } | |
4930 | else | |
4931 | { | |
4932 | // at the bottom of a column | |
13f6e9e8 | 4933 | DisableCellEditControl(); |
f6bcfd97 | 4934 | } |
58dd5b3b | 4935 | } |
2d66e025 MB |
4936 | break; |
4937 | ||
5c8fc7c1 | 4938 | case WXK_ESCAPE: |
e32352cf | 4939 | ClearSelection(); |
5c8fc7c1 SN |
4940 | break; |
4941 | ||
2c9a89e0 | 4942 | case WXK_TAB: |
f6bcfd97 | 4943 | { |
1dc17bca VZ |
4944 | // send an event to the grid's parents for custom handling |
4945 | wxGridEvent gridEvt(GetId(), wxEVT_GRID_TABBING, this, | |
4946 | GetGridCursorRow(), GetGridCursorCol(), | |
4947 | -1, -1, false, event); | |
4948 | if ( ProcessWindowEvent(gridEvt) ) | |
f6bcfd97 | 4949 | { |
1dc17bca VZ |
4950 | // the event has been handled so no need for more processing |
4951 | break; | |
f6bcfd97 BP |
4952 | } |
4953 | } | |
1dc17bca | 4954 | DoGridProcessTab( event ); |
2c9a89e0 RD |
4955 | break; |
4956 | ||
2d66e025 | 4957 | case WXK_HOME: |
ad178a65 VZ |
4958 | GoToCell(event.ControlDown() ? 0 |
4959 | : m_currentCellCoords.GetRow(), | |
4960 | 0); | |
2d66e025 MB |
4961 | break; |
4962 | ||
4963 | case WXK_END: | |
ad178a65 VZ |
4964 | GoToCell(event.ControlDown() ? m_numRows - 1 |
4965 | : m_currentCellCoords.GetRow(), | |
4966 | m_numCols - 1); | |
2d66e025 MB |
4967 | break; |
4968 | ||
faa94f3e | 4969 | case WXK_PAGEUP: |
2d66e025 MB |
4970 | MovePageUp(); |
4971 | break; | |
4972 | ||
faa94f3e | 4973 | case WXK_PAGEDOWN: |
2d66e025 MB |
4974 | MovePageDown(); |
4975 | break; | |
4976 | ||
07296f0b | 4977 | case WXK_SPACE: |
32b4e9ec VZ |
4978 | // Ctrl-Space selects the current column, Shift-Space -- the |
4979 | // current row and Ctrl-Shift-Space -- everything | |
4980 | switch ( m_selection ? event.GetModifiers() : wxMOD_NONE ) | |
aa5e1f75 | 4981 | { |
32b4e9ec VZ |
4982 | case wxMOD_CONTROL: |
4983 | m_selection->SelectCol(m_currentCellCoords.GetCol()); | |
4984 | break; | |
ccdee36f | 4985 | |
32b4e9ec VZ |
4986 | case wxMOD_SHIFT: |
4987 | m_selection->SelectRow(m_currentCellCoords.GetRow()); | |
4988 | break; | |
4989 | ||
4990 | case wxMOD_CONTROL | wxMOD_SHIFT: | |
4991 | m_selection->SelectBlock(0, 0, | |
4992 | m_numRows - 1, m_numCols - 1); | |
4993 | break; | |
4994 | ||
4995 | case wxMOD_NONE: | |
4996 | if ( !IsEditable() ) | |
4997 | { | |
4998 | MoveCursorRight(false); | |
4999 | break; | |
5000 | } | |
5001 | //else: fall through | |
5002 | ||
5003 | default: | |
5004 | event.Skip(); | |
5005 | } | |
ccdee36f | 5006 | break; |
07296f0b | 5007 | |
2d66e025 | 5008 | default: |
63e2147c | 5009 | event.Skip(); |
025562fe | 5010 | break; |
2d66e025 | 5011 | } |
f85afd4e MB |
5012 | } |
5013 | ||
ca65c044 | 5014 | m_inOnKeyDown = false; |
f85afd4e MB |
5015 | } |
5016 | ||
f6bcfd97 BP |
5017 | void wxGrid::OnKeyUp( wxKeyEvent& event ) |
5018 | { | |
5019 | // try local handlers | |
5020 | // | |
12a3f227 | 5021 | if ( event.GetKeyCode() == WXK_SHIFT ) |
f6bcfd97 | 5022 | { |
8a3e536c VZ |
5023 | if ( m_selectedBlockTopLeft != wxGridNoCellCoords && |
5024 | m_selectedBlockBottomRight != wxGridNoCellCoords ) | |
3f3dc2ef VZ |
5025 | { |
5026 | if ( m_selection ) | |
5027 | { | |
a9339fe2 | 5028 | m_selection->SelectBlock( |
8a3e536c VZ |
5029 | m_selectedBlockTopLeft, |
5030 | m_selectedBlockBottomRight, | |
8b5f6d9d | 5031 | event); |
3f3dc2ef VZ |
5032 | } |
5033 | } | |
5034 | ||
8a3e536c VZ |
5035 | m_selectedBlockTopLeft = wxGridNoCellCoords; |
5036 | m_selectedBlockBottomRight = wxGridNoCellCoords; | |
5037 | m_selectedBlockCorner = wxGridNoCellCoords; | |
f6bcfd97 BP |
5038 | } |
5039 | } | |
5040 | ||
63e2147c RD |
5041 | void wxGrid::OnChar( wxKeyEvent& event ) |
5042 | { | |
5043 | // is it possible to edit the current cell at all? | |
5044 | if ( !IsCellEditControlEnabled() && CanEnableCellControl() ) | |
5045 | { | |
5046 | // yes, now check whether the cells editor accepts the key | |
5047 | int row = m_currentCellCoords.GetRow(); | |
5048 | int col = m_currentCellCoords.GetCol(); | |
2f024384 | 5049 | wxGridCellAttr *attr = GetCellAttr(row, col); |
63e2147c RD |
5050 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
5051 | ||
5052 | // <F2> is special and will always start editing, for | |
5053 | // other keys - ask the editor itself | |
5054 | if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers()) | |
5055 | || editor->IsAcceptedKey(event) ) | |
5056 | { | |
5057 | // ensure cell is visble | |
5058 | MakeCellVisible(row, col); | |
5059 | EnableCellEditControl(); | |
5060 | ||
5061 | // a problem can arise if the cell is not completely | |
5062 | // visible (even after calling MakeCellVisible the | |
5063 | // control is not created and calling StartingKey will | |
5064 | // crash the app | |
046d682f | 5065 | if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled ) |
63e2147c RD |
5066 | editor->StartingKey(event); |
5067 | } | |
5068 | else | |
5069 | { | |
5070 | event.Skip(); | |
5071 | } | |
5072 | ||
5073 | editor->DecRef(); | |
5074 | attr->DecRef(); | |
5075 | } | |
5076 | else | |
5077 | { | |
5078 | event.Skip(); | |
5079 | } | |
5080 | } | |
5081 | ||
2796cce3 | 5082 | void wxGrid::OnEraseBackground(wxEraseEvent&) |
508011ce VZ |
5083 | { |
5084 | } | |
07296f0b | 5085 | |
1dc17bca VZ |
5086 | void wxGrid::DoGridProcessTab(wxKeyboardState& kbdState) |
5087 | { | |
5088 | const bool isForwardTab = !kbdState.ShiftDown(); | |
5089 | ||
5090 | // TAB processing only changes when we are at the borders of the grid, so | |
5091 | // let's first handle the common behaviour when we are not at the border. | |
5092 | if ( isForwardTab ) | |
5093 | { | |
5094 | if ( GetGridCursorCol() < GetNumberCols() - 1 ) | |
5095 | { | |
5096 | MoveCursorRight( false ); | |
5097 | return; | |
5098 | } | |
5099 | } | |
5100 | else // going back | |
5101 | { | |
5102 | if ( GetGridCursorCol() ) | |
5103 | { | |
5104 | MoveCursorLeft( false ); | |
5105 | return; | |
5106 | } | |
5107 | } | |
5108 | ||
5109 | ||
5110 | // We only get here if the cursor is at the border of the grid, apply the | |
5111 | // configured behaviour. | |
5112 | switch ( m_tabBehaviour ) | |
5113 | { | |
5114 | case Tab_Stop: | |
5115 | // Nothing special to do, we remain at the current cell. | |
5116 | break; | |
5117 | ||
5118 | case Tab_Wrap: | |
5119 | // Go to the beginning of the next or the end of the previous row. | |
5120 | if ( isForwardTab ) | |
5121 | { | |
5122 | if ( GetGridCursorRow() < GetNumberRows() - 1 ) | |
5123 | { | |
5124 | GoToCell( GetGridCursorRow() + 1, 0 ); | |
5125 | return; | |
5126 | } | |
5127 | } | |
5128 | else | |
5129 | { | |
5130 | if ( GetGridCursorRow() > 0 ) | |
5131 | { | |
5132 | GoToCell( GetGridCursorRow() - 1, GetNumberCols() - 1 ); | |
5133 | return; | |
5134 | } | |
5135 | } | |
5136 | break; | |
5137 | ||
5138 | case Tab_Leave: | |
5139 | if ( Navigate( isForwardTab ? wxNavigationKeyEvent::IsForward | |
5140 | : wxNavigationKeyEvent::IsBackward ) ) | |
5141 | return; | |
5142 | break; | |
5143 | } | |
5144 | ||
5145 | // If we remain in this cell, stop editing it if we were doing so. | |
5146 | DisableCellEditControl(); | |
5147 | } | |
5148 | ||
8a3e536c | 5149 | bool wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) |
66242c80 | 5150 | { |
8a3e536c | 5151 | if ( SendEvent(wxEVT_GRID_SELECT_CELL, coords) == -1 ) |
f6bcfd97 | 5152 | { |
8a3e536c VZ |
5153 | // the event has been vetoed - do nothing |
5154 | return false; | |
f6bcfd97 BP |
5155 | } |
5156 | ||
9553702e | 5157 | #if !defined(__WXMAC__) |
bee19958 DS |
5158 | wxClientDC dc( m_gridWin ); |
5159 | PrepareDC( dc ); | |
5d38a5f3 | 5160 | #endif |
f6bcfd97 | 5161 | |
2a7750d9 | 5162 | if ( m_currentCellCoords != wxGridNoCellCoords ) |
2d66e025 | 5163 | { |
b54ba671 | 5164 | DisableCellEditControl(); |
07296f0b | 5165 | |
ca65c044 | 5166 | if ( IsVisible( m_currentCellCoords, false ) ) |
f6bcfd97 BP |
5167 | { |
5168 | wxRect r; | |
bee19958 | 5169 | r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords ); |
f6bcfd97 BP |
5170 | if ( !m_gridLinesEnabled ) |
5171 | { | |
5172 | r.x--; | |
5173 | r.y--; | |
5174 | r.width++; | |
5175 | r.height++; | |
5176 | } | |
d1c0b4f9 | 5177 | |
3ed884a0 | 5178 | wxGridCellCoordsArray cells = CalcCellsExposed( r ); |
84912ef8 | 5179 | |
f6bcfd97 BP |
5180 | // Otherwise refresh redraws the highlight! |
5181 | m_currentCellCoords = coords; | |
275c4ae4 | 5182 | |
9553702e | 5183 | #if defined(__WXMAC__) |
5d38a5f3 JS |
5184 | m_gridWin->Refresh(true /*, & r */); |
5185 | #else | |
bee19958 | 5186 | DrawGridCellArea( dc, cells ); |
f6bcfd97 | 5187 | DrawAllGridLines( dc, r ); |
5d38a5f3 | 5188 | #endif |
f6bcfd97 | 5189 | } |
66242c80 | 5190 | } |
8f177c8e | 5191 | |
2d66e025 MB |
5192 | m_currentCellCoords = coords; |
5193 | ||
bee19958 | 5194 | wxGridCellAttr *attr = GetCellAttr( coords ); |
76c66f19 | 5195 | #if !defined(__WXMAC__) |
bee19958 | 5196 | DrawCellHighlight( dc, attr ); |
5d38a5f3 | 5197 | #endif |
2a7750d9 | 5198 | attr->DecRef(); |
8a3e536c VZ |
5199 | |
5200 | return true; | |
66242c80 MB |
5201 | } |
5202 | ||
8a3e536c VZ |
5203 | void |
5204 | wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol, | |
5205 | int bottomRow, int rightCol) | |
c9097836 | 5206 | { |
4445d035 VZ |
5207 | MakeCellVisible(m_selectedBlockCorner); |
5208 | m_selectedBlockCorner = wxGridCellCoords(bottomRow, rightCol); | |
5209 | ||
3f3dc2ef | 5210 | if ( m_selection ) |
c9097836 | 5211 | { |
8a3e536c | 5212 | switch ( m_selection->GetSelectionMode() ) |
3f3dc2ef | 5213 | { |
8a3e536c VZ |
5214 | default: |
5215 | wxFAIL_MSG( "unknown selection mode" ); | |
5216 | // fall through | |
5217 | ||
5218 | case wxGridSelectCells: | |
5219 | // arbitrary blocks selection allowed so just use the cell | |
5220 | // coordinates as is | |
5221 | break; | |
5222 | ||
5223 | case wxGridSelectRows: | |
5224 | // only full rows selection allowd, ensure that we do select | |
5225 | // full rows | |
5226 | leftCol = 0; | |
5227 | rightCol = GetNumberCols() - 1; | |
5228 | break; | |
5229 | ||
5230 | case wxGridSelectColumns: | |
5231 | // same as above but for columns | |
5232 | topRow = 0; | |
5233 | bottomRow = GetNumberRows() - 1; | |
5234 | break; | |
5235 | ||
5236 | case wxGridSelectRowsOrColumns: | |
5237 | // in this mode we can select only full rows or full columns so | |
5238 | // it doesn't make sense to select blocks at all (and we can't | |
5239 | // extend the block because there is no preferred direction, we | |
5240 | // could only extend it to cover the entire grid but this is | |
5241 | // not useful) | |
5242 | return; | |
3f3dc2ef | 5243 | } |
c9097836 | 5244 | } |
3f3dc2ef | 5245 | |
1372f8cc VZ |
5246 | EnsureFirstLessThanSecond(topRow, bottomRow); |
5247 | EnsureFirstLessThanSecond(leftCol, rightCol); | |
c9097836 | 5248 | |
8a3e536c VZ |
5249 | wxGridCellCoords updateTopLeft = wxGridCellCoords(topRow, leftCol), |
5250 | updateBottomRight = wxGridCellCoords(bottomRow, rightCol); | |
c9097836 | 5251 | |
3ed884a0 | 5252 | // First the case that we selected a completely new area |
8a3e536c VZ |
5253 | if ( m_selectedBlockTopLeft == wxGridNoCellCoords || |
5254 | m_selectedBlockBottomRight == wxGridNoCellCoords ) | |
3ed884a0 SN |
5255 | { |
5256 | wxRect rect; | |
5257 | rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ), | |
5258 | wxGridCellCoords ( bottomRow, rightCol ) ); | |
ca65c044 | 5259 | m_gridWin->Refresh( false, &rect ); |
3ed884a0 | 5260 | } |
2f024384 | 5261 | |
3ed884a0 | 5262 | // Now handle changing an existing selection area. |
8a3e536c VZ |
5263 | else if ( m_selectedBlockTopLeft != updateTopLeft || |
5264 | m_selectedBlockBottomRight != updateBottomRight ) | |
c9097836 MB |
5265 | { |
5266 | // Compute two optimal update rectangles: | |
5267 | // Either one rectangle is a real subset of the | |
5268 | // other, or they are (almost) disjoint! | |
5269 | wxRect rect[4]; | |
5270 | bool need_refresh[4]; | |
5271 | need_refresh[0] = | |
5272 | need_refresh[1] = | |
5273 | need_refresh[2] = | |
ca65c044 | 5274 | need_refresh[3] = false; |
c9097836 MB |
5275 | int i; |
5276 | ||
5277 | // Store intermediate values | |
8a3e536c VZ |
5278 | wxCoord oldLeft = m_selectedBlockTopLeft.GetCol(); |
5279 | wxCoord oldTop = m_selectedBlockTopLeft.GetRow(); | |
5280 | wxCoord oldRight = m_selectedBlockBottomRight.GetCol(); | |
5281 | wxCoord oldBottom = m_selectedBlockBottomRight.GetRow(); | |
c9097836 MB |
5282 | |
5283 | // Determine the outer/inner coordinates. | |
1372f8cc VZ |
5284 | EnsureFirstLessThanSecond(oldLeft, leftCol); |
5285 | EnsureFirstLessThanSecond(oldTop, topRow); | |
5286 | EnsureFirstLessThanSecond(rightCol, oldRight); | |
5287 | EnsureFirstLessThanSecond(bottomRow, oldBottom); | |
c9097836 MB |
5288 | |
5289 | // Now, either the stuff marked old is the outer | |
5290 | // rectangle or we don't have a situation where one | |
5291 | // is contained in the other. | |
5292 | ||
5293 | if ( oldLeft < leftCol ) | |
5294 | { | |
3ed884a0 SN |
5295 | // Refresh the newly selected or deselected |
5296 | // area to the left of the old or new selection. | |
ca65c044 | 5297 | need_refresh[0] = true; |
a9339fe2 DS |
5298 | rect[0] = BlockToDeviceRect( |
5299 | wxGridCellCoords( oldTop, oldLeft ), | |
5300 | wxGridCellCoords( oldBottom, leftCol - 1 ) ); | |
c9097836 MB |
5301 | } |
5302 | ||
2f024384 | 5303 | if ( oldTop < topRow ) |
c9097836 | 5304 | { |
3ed884a0 SN |
5305 | // Refresh the newly selected or deselected |
5306 | // area above the old or new selection. | |
ca65c044 | 5307 | need_refresh[1] = true; |
2f024384 DS |
5308 | rect[1] = BlockToDeviceRect( |
5309 | wxGridCellCoords( oldTop, leftCol ), | |
5310 | wxGridCellCoords( topRow - 1, rightCol ) ); | |
c9097836 MB |
5311 | } |
5312 | ||
5313 | if ( oldRight > rightCol ) | |
5314 | { | |
3ed884a0 SN |
5315 | // Refresh the newly selected or deselected |
5316 | // area to the right of the old or new selection. | |
ca65c044 | 5317 | need_refresh[2] = true; |
2f024384 | 5318 | rect[2] = BlockToDeviceRect( |
a9339fe2 DS |
5319 | wxGridCellCoords( oldTop, rightCol + 1 ), |
5320 | wxGridCellCoords( oldBottom, oldRight ) ); | |
c9097836 MB |
5321 | } |
5322 | ||
5323 | if ( oldBottom > bottomRow ) | |
5324 | { | |
3ed884a0 SN |
5325 | // Refresh the newly selected or deselected |
5326 | // area below the old or new selection. | |
ca65c044 | 5327 | need_refresh[3] = true; |
2f024384 | 5328 | rect[3] = BlockToDeviceRect( |
a9339fe2 DS |
5329 | wxGridCellCoords( bottomRow + 1, leftCol ), |
5330 | wxGridCellCoords( oldBottom, rightCol ) ); | |
c9097836 MB |
5331 | } |
5332 | ||
c9097836 MB |
5333 | // various Refresh() calls |
5334 | for (i = 0; i < 4; i++ ) | |
5335 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
ca65c044 | 5336 | m_gridWin->Refresh( false, &(rect[i]) ); |
c9097836 | 5337 | } |
2f024384 DS |
5338 | |
5339 | // change selection | |
8a3e536c VZ |
5340 | m_selectedBlockTopLeft = updateTopLeft; |
5341 | m_selectedBlockBottomRight = updateBottomRight; | |
c9097836 MB |
5342 | } |
5343 | ||
2d66e025 MB |
5344 | // |
5345 | // ------ functions to get/send data (see also public functions) | |
5346 | // | |
5347 | ||
5348 | bool wxGrid::GetModelValues() | |
66242c80 | 5349 | { |
bca7bfc8 SN |
5350 | // Hide the editor, so it won't hide a changed value. |
5351 | HideCellEditControl(); | |
c6707d16 | 5352 | |
2d66e025 | 5353 | if ( m_table ) |
66242c80 | 5354 | { |
2d66e025 | 5355 | // all we need to do is repaint the grid |
66242c80 | 5356 | // |
2d66e025 | 5357 | m_gridWin->Refresh(); |
ca65c044 | 5358 | return true; |
66242c80 | 5359 | } |
8f177c8e | 5360 | |
ca65c044 | 5361 | return false; |
66242c80 MB |
5362 | } |
5363 | ||
2d66e025 | 5364 | bool wxGrid::SetModelValues() |
f85afd4e | 5365 | { |
2d66e025 | 5366 | int row, col; |
8f177c8e | 5367 | |
c6707d16 SN |
5368 | // Disable the editor, so it won't hide a changed value. |
5369 | // Do we also want to save the current value of the editor first? | |
5370 | // I think so ... | |
c6707d16 SN |
5371 | DisableCellEditControl(); |
5372 | ||
2d66e025 MB |
5373 | if ( m_table ) |
5374 | { | |
56b6cf26 | 5375 | for ( row = 0; row < m_numRows; row++ ) |
f85afd4e | 5376 | { |
56b6cf26 | 5377 | for ( col = 0; col < m_numCols; col++ ) |
f85afd4e | 5378 | { |
2d66e025 | 5379 | m_table->SetValue( row, col, GetCellValue(row, col) ); |
f85afd4e MB |
5380 | } |
5381 | } | |
8f177c8e | 5382 | |
ca65c044 | 5383 | return true; |
f85afd4e MB |
5384 | } |
5385 | ||
ca65c044 | 5386 | return false; |
f85afd4e MB |
5387 | } |
5388 | ||
2d66e025 MB |
5389 | // Note - this function only draws cells that are in the list of |
5390 | // exposed cells (usually set from the update region by | |
5391 | // CalcExposedCells) | |
5392 | // | |
d10f4bf9 | 5393 | void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells ) |
f85afd4e | 5394 | { |
4db6714b KH |
5395 | if ( !m_numRows || !m_numCols ) |
5396 | return; | |
60ff3b99 | 5397 | |
dc1f566f | 5398 | int i, numCells = cells.GetCount(); |
27f35b66 SN |
5399 | int row, col, cell_rows, cell_cols; |
5400 | wxGridCellCoordsArray redrawCells; | |
60ff3b99 | 5401 | |
a9339fe2 | 5402 | for ( i = numCells - 1; i >= 0; i-- ) |
f85afd4e | 5403 | { |
27f35b66 SN |
5404 | row = cells[i].GetRow(); |
5405 | col = cells[i].GetCol(); | |
5406 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
5407 | ||
5408 | // If this cell is part of a multicell block, find owner for repaint | |
5409 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
5410 | { | |
a9339fe2 | 5411 | wxGridCellCoords cell( row + cell_rows, col + cell_cols ); |
ca65c044 | 5412 | bool marked = false; |
56b6cf26 | 5413 | for ( int j = 0; j < numCells; j++ ) |
27f35b66 SN |
5414 | { |
5415 | if ( cell == cells[j] ) | |
5416 | { | |
ca65c044 | 5417 | marked = true; |
3ed884a0 | 5418 | break; |
27f35b66 SN |
5419 | } |
5420 | } | |
2f024384 | 5421 | |
27f35b66 SN |
5422 | if (!marked) |
5423 | { | |
5424 | int count = redrawCells.GetCount(); | |
dc1f566f | 5425 | for (int j = 0; j < count; j++) |
27f35b66 SN |
5426 | { |
5427 | if ( cell == redrawCells[j] ) | |
5428 | { | |
ca65c044 | 5429 | marked = true; |
27f35b66 SN |
5430 | break; |
5431 | } | |
5432 | } | |
2f024384 | 5433 | |
4db6714b KH |
5434 | if (!marked) |
5435 | redrawCells.Add( cell ); | |
27f35b66 | 5436 | } |
2f024384 DS |
5437 | |
5438 | // don't bother drawing this cell | |
5439 | continue; | |
27f35b66 SN |
5440 | } |
5441 | ||
5442 | // If this cell is empty, find cell to left that might want to overflow | |
5443 | if (m_table && m_table->IsEmptyCell(row, col)) | |
5444 | { | |
dc1f566f | 5445 | for ( int l = 0; l < cell_rows; l++ ) |
27f35b66 | 5446 | { |
56b6cf26 | 5447 | // find a cell in this row to leave already marked for repaint |
dc1f566f SN |
5448 | int left = col; |
5449 | for (int k = 0; k < int(redrawCells.GetCount()); k++) | |
5450 | if ((redrawCells[k].GetCol() < left) && | |
5451 | (redrawCells[k].GetRow() == row)) | |
2f024384 | 5452 | { |
4db6714b | 5453 | left = redrawCells[k].GetCol(); |
2f024384 | 5454 | } |
dc1f566f | 5455 | |
4db6714b KH |
5456 | if (left == col) |
5457 | left = 0; // oh well | |
dc1f566f | 5458 | |
2f024384 | 5459 | for (int j = col - 1; j >= left; j--) |
27f35b66 | 5460 | { |
2f024384 | 5461 | if (!m_table->IsEmptyCell(row + l, j)) |
27f35b66 | 5462 | { |
2f024384 | 5463 | if (GetCellOverflow(row + l, j)) |
27f35b66 | 5464 | { |
2f024384 | 5465 | wxGridCellCoords cell(row + l, j); |
ca65c044 | 5466 | bool marked = false; |
27f35b66 | 5467 | |
dc1f566f | 5468 | for (int k = 0; k < numCells; k++) |
27f35b66 SN |
5469 | { |
5470 | if ( cell == cells[k] ) | |
5471 | { | |
ca65c044 | 5472 | marked = true; |
27f35b66 SN |
5473 | break; |
5474 | } | |
5475 | } | |
4db6714b | 5476 | |
27f35b66 SN |
5477 | if (!marked) |
5478 | { | |
5479 | int count = redrawCells.GetCount(); | |
dc1f566f | 5480 | for (int k = 0; k < count; k++) |
27f35b66 SN |
5481 | { |
5482 | if ( cell == redrawCells[k] ) | |
5483 | { | |
ca65c044 | 5484 | marked = true; |
27f35b66 SN |
5485 | break; |
5486 | } | |
5487 | } | |
4db6714b KH |
5488 | if (!marked) |
5489 | redrawCells.Add( cell ); | |
27f35b66 SN |
5490 | } |
5491 | } | |
5492 | break; | |
5493 | } | |
5494 | } | |
5495 | } | |
5496 | } | |
4db6714b | 5497 | |
d10f4bf9 | 5498 | DrawCell( dc, cells[i] ); |
2d66e025 | 5499 | } |
27f35b66 SN |
5500 | |
5501 | numCells = redrawCells.GetCount(); | |
5502 | ||
56b6cf26 | 5503 | for ( i = numCells - 1; i >= 0; i-- ) |
27f35b66 SN |
5504 | { |
5505 | DrawCell( dc, redrawCells[i] ); | |
5506 | } | |
2d66e025 | 5507 | } |
8f177c8e | 5508 | |
7c8a8ad5 MB |
5509 | void wxGrid::DrawGridSpace( wxDC& dc ) |
5510 | { | |
f6bcfd97 BP |
5511 | int cw, ch; |
5512 | m_gridWin->GetClientSize( &cw, &ch ); | |
7c8a8ad5 | 5513 | |
f6bcfd97 BP |
5514 | int right, bottom; |
5515 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
7c8a8ad5 | 5516 | |
d4175745 | 5517 | int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0; |
2f024384 | 5518 | int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0; |
7c8a8ad5 | 5519 | |
f6bcfd97 BP |
5520 | if ( right > rightCol || bottom > bottomRow ) |
5521 | { | |
5522 | int left, top; | |
5523 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
7c8a8ad5 | 5524 | |
ff72f628 | 5525 | dc.SetBrush(GetDefaultCellBackgroundColour()); |
f6bcfd97 | 5526 | dc.SetPen( *wxTRANSPARENT_PEN ); |
7c8a8ad5 | 5527 | |
f6bcfd97 BP |
5528 | if ( right > rightCol ) |
5529 | { | |
a9339fe2 | 5530 | dc.DrawRectangle( rightCol, top, right - rightCol, ch ); |
f6bcfd97 BP |
5531 | } |
5532 | ||
5533 | if ( bottom > bottomRow ) | |
5534 | { | |
a9339fe2 | 5535 | dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow ); |
f6bcfd97 BP |
5536 | } |
5537 | } | |
7c8a8ad5 MB |
5538 | } |
5539 | ||
2d66e025 MB |
5540 | void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) |
5541 | { | |
ab79958a VZ |
5542 | int row = coords.GetRow(); |
5543 | int col = coords.GetCol(); | |
60ff3b99 | 5544 | |
7c1cb261 | 5545 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
ab79958a VZ |
5546 | return; |
5547 | ||
5548 | // we draw the cell border ourselves | |
189d0213 VZ |
5549 | wxGridCellAttr* attr = GetCellAttr(row, col); |
5550 | ||
5551 | bool isCurrent = coords == m_currentCellCoords; | |
508011ce | 5552 | |
f6bcfd97 | 5553 | wxRect rect = CellToRect( row, col ); |
f85afd4e | 5554 | |
189d0213 | 5555 | // if the editor is shown, we should use it and not the renderer |
f6bcfd97 BP |
5556 | // Note: However, only if it is really _shown_, i.e. not hidden! |
5557 | if ( isCurrent && IsCellEditControlShown() ) | |
189d0213 | 5558 | { |
a9339fe2 | 5559 | // NB: this "#if..." is temporary and fixes a problem where the |
962a48f6 DS |
5560 | // edit control is erased by this code after being rendered. |
5561 | // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered | |
5562 | // implicitly, causing this out-of order render. | |
5d38a5f3 | 5563 | #if !defined(__WXMAC__) |
0b190b0f | 5564 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
ccc04025 | 5565 | editor->PaintBackground(dc, rect, *attr); |
0b190b0f | 5566 | editor->DecRef(); |
962a48f6 | 5567 | #endif |
189d0213 VZ |
5568 | } |
5569 | else | |
5570 | { | |
a9339fe2 | 5571 | // but all the rest is drawn by the cell renderer and hence may be customized |
0b190b0f VZ |
5572 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); |
5573 | renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); | |
5574 | renderer->DecRef(); | |
189d0213 | 5575 | } |
07296f0b | 5576 | |
283b7808 VZ |
5577 | attr->DecRef(); |
5578 | } | |
07296f0b | 5579 | |
283b7808 | 5580 | void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ) |
07296f0b | 5581 | { |
760be3f7 VS |
5582 | // don't show highlight when the grid doesn't have focus |
5583 | if ( !HasFocus() ) | |
5584 | return; | |
5585 | ||
07296f0b RD |
5586 | int row = m_currentCellCoords.GetRow(); |
5587 | int col = m_currentCellCoords.GetCol(); | |
5588 | ||
7c1cb261 | 5589 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
07296f0b RD |
5590 | return; |
5591 | ||
f6bcfd97 | 5592 | wxRect rect = CellToRect(row, col); |
07296f0b | 5593 | |
b3a7510d VZ |
5594 | // hmmm... what could we do here to show that the cell is disabled? |
5595 | // for now, I just draw a thinner border than for the other ones, but | |
5596 | // it doesn't look really good | |
b3a7510d | 5597 | |
d2520c85 RD |
5598 | int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth; |
5599 | ||
27f35b66 SN |
5600 | if (penWidth > 0) |
5601 | { | |
56b6cf26 DS |
5602 | // The center of the drawn line is where the position/width/height of |
5603 | // the rectangle is actually at (on wxMSW at least), so the | |
5604 | // size of the rectangle is reduced to compensate for the thickness of | |
5605 | // the line. If this is too strange on non-wxMSW platforms then | |
d2520c85 | 5606 | // please #ifdef this appropriately. |
4db6714b KH |
5607 | rect.x += penWidth / 2; |
5608 | rect.y += penWidth / 2; | |
5609 | rect.width -= penWidth - 1; | |
5610 | rect.height -= penWidth - 1; | |
d2520c85 | 5611 | |
d2520c85 | 5612 | // Now draw the rectangle |
73145b0e JS |
5613 | // use the cellHighlightColour if the cell is inside a selection, this |
5614 | // will ensure the cell is always visible. | |
ff72f628 VZ |
5615 | dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground |
5616 | : m_cellHighlightColour, | |
5617 | penWidth)); | |
d2520c85 RD |
5618 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
5619 | dc.DrawRectangle(rect); | |
5620 | } | |
2d66e025 | 5621 | } |
f85afd4e | 5622 | |
3d3f3e37 VZ |
5623 | wxPen wxGrid::GetDefaultGridLinePen() |
5624 | { | |
ff72f628 | 5625 | return wxPen(GetGridLineColour()); |
3d3f3e37 VZ |
5626 | } |
5627 | ||
5628 | wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row)) | |
5629 | { | |
5630 | return GetDefaultGridLinePen(); | |
5631 | } | |
5632 | ||
5633 | wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col)) | |
5634 | { | |
5635 | return GetDefaultGridLinePen(); | |
5636 | } | |
5637 | ||
2d66e025 MB |
5638 | void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) |
5639 | { | |
2d66e025 MB |
5640 | int row = coords.GetRow(); |
5641 | int col = coords.GetCol(); | |
7c1cb261 VZ |
5642 | if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
5643 | return; | |
5644 | ||
60ff3b99 | 5645 | |
27f35b66 SN |
5646 | wxRect rect = CellToRect( row, col ); |
5647 | ||
2d66e025 | 5648 | // right hand border |
3d3f3e37 | 5649 | dc.SetPen( GetColGridLinePen(col) ); |
27f35b66 SN |
5650 | dc.DrawLine( rect.x + rect.width, rect.y, |
5651 | rect.x + rect.width, rect.y + rect.height + 1 ); | |
2d66e025 MB |
5652 | |
5653 | // bottom border | |
3d3f3e37 | 5654 | dc.SetPen( GetRowGridLinePen(row) ); |
2f024384 | 5655 | dc.DrawLine( rect.x, rect.y + rect.height, |
27f35b66 | 5656 | rect.x + rect.width, rect.y + rect.height); |
f85afd4e MB |
5657 | } |
5658 | ||
2f024384 | 5659 | void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells) |
b3a7510d | 5660 | { |
2a7750d9 MB |
5661 | // This if block was previously in wxGrid::OnPaint but that doesn't |
5662 | // seem to get called under wxGTK - MB | |
5663 | // | |
2f024384 | 5664 | if ( m_currentCellCoords == wxGridNoCellCoords && |
2a7750d9 MB |
5665 | m_numRows && m_numCols ) |
5666 | { | |
5667 | m_currentCellCoords.Set(0, 0); | |
5668 | } | |
5669 | ||
f6bcfd97 | 5670 | if ( IsCellEditControlShown() ) |
99306db2 VZ |
5671 | { |
5672 | // don't show highlight when the edit control is shown | |
5673 | return; | |
5674 | } | |
5675 | ||
b3a7510d VZ |
5676 | // if the active cell was repainted, repaint its highlight too because it |
5677 | // might have been damaged by the grid lines | |
d10f4bf9 | 5678 | size_t count = cells.GetCount(); |
b3a7510d VZ |
5679 | for ( size_t n = 0; n < count; n++ ) |
5680 | { | |
54181a33 VZ |
5681 | wxGridCellCoords cell = cells[n]; |
5682 | ||
5683 | // If we are using attributes, then we may have just exposed another | |
5684 | // cell in a partially-visible merged cluster of cells. If the "anchor" | |
5685 | // (upper left) cell of this merged cluster is the cell indicated by | |
5686 | // m_currentCellCoords, then we need to refresh the cell highlight even | |
5687 | // though the "anchor" itself is not part of our update segment. | |
5688 | if ( CanHaveAttributes() ) | |
5689 | { | |
5690 | int rows = 0, | |
5691 | cols = 0; | |
5692 | GetCellSize(cell.GetRow(), cell.GetCol(), &rows, &cols); | |
5693 | ||
5694 | if ( rows < 0 ) | |
5695 | cell.SetRow(cell.GetRow() + rows); | |
5696 | ||
5697 | if ( cols < 0 ) | |
5698 | cell.SetCol(cell.GetCol() + cols); | |
5699 | } | |
5700 | ||
5701 | if ( cell == m_currentCellCoords ) | |
b3a7510d VZ |
5702 | { |
5703 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
5704 | DrawCellHighlight(dc, attr); | |
5705 | attr->DecRef(); | |
5706 | ||
5707 | break; | |
5708 | } | |
5709 | } | |
5710 | } | |
2d66e025 | 5711 | |
b55d57aa VZ |
5712 | // Used by wxGrid::Render() to draw the grid lines only for the cells in the |
5713 | // specified range. | |
5714 | void | |
5715 | wxGrid::DrawRangeGridLines(wxDC& dc, | |
5716 | const wxRegion& reg, | |
5717 | const wxGridCellCoords& topLeft, | |
5718 | const wxGridCellCoords& bottomRight) | |
5719 | { | |
5720 | if ( !m_gridLinesEnabled ) | |
5721 | return; | |
5722 | ||
5723 | int top, left, width, height; | |
5724 | reg.GetBox( left, top, width, height ); | |
5725 | ||
5726 | // create a clipping region | |
5727 | wxRegion clippedcells( dc.LogicalToDeviceX( left ), | |
5728 | dc.LogicalToDeviceY( top ), | |
5729 | dc.LogicalToDeviceXRel( width ), | |
5730 | dc.LogicalToDeviceYRel( height ) ); | |
5731 | ||
5732 | // subtract multi cell span area from clipping region for lines | |
5733 | wxRect rect; | |
5734 | for ( int row = topLeft.GetRow(); row <= bottomRight.GetRow(); row++ ) | |
5735 | { | |
5736 | for ( int col = topLeft.GetCol(); col <= bottomRight.GetCol(); col++ ) | |
5737 | { | |
5738 | int cell_rows, cell_cols; | |
5739 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
5740 | if ( cell_rows > 1 || cell_cols > 1 ) // multi cell | |
5741 | { | |
5742 | rect = CellToRect( row, col ); | |
5743 | // cater for scaling | |
5744 | // device origin already set in ::Render() for x, y | |
5745 | rect.x = dc.LogicalToDeviceX( rect.x ); | |
5746 | rect.y = dc.LogicalToDeviceY( rect.y ); | |
5747 | rect.width = dc.LogicalToDeviceXRel( rect.width ); | |
5748 | rect.height = dc.LogicalToDeviceYRel( rect.height ) - 1; | |
5749 | clippedcells.Subtract( rect ); | |
5750 | } | |
5751 | else if ( cell_rows < 0 || cell_cols < 0 ) // part of multicell | |
5752 | { | |
5753 | rect = CellToRect( row + cell_rows, col + cell_cols ); | |
5754 | rect.x = dc.LogicalToDeviceX( rect.x ); | |
5755 | rect.y = dc.LogicalToDeviceY( rect.y ); | |
5756 | rect.width = dc.LogicalToDeviceXRel( rect.width ); | |
5757 | rect.height = dc.LogicalToDeviceYRel( rect.height ) - 1; | |
5758 | clippedcells.Subtract( rect ); | |
5759 | } | |
5760 | } | |
5761 | } | |
5762 | ||
5763 | dc.SetDeviceClippingRegion( clippedcells ); | |
5764 | ||
5765 | DoDrawGridLines(dc, | |
5766 | top, left, top + height, left + width, | |
5767 | topLeft.GetRow(), topLeft.GetCol(), | |
5768 | bottomRight.GetRow(), bottomRight.GetCol()); | |
5769 | ||
5770 | dc.DestroyClippingRegion(); | |
5771 | } | |
5772 | ||
2d66e025 MB |
5773 | // This is used to redraw all grid lines e.g. when the grid line colour |
5774 | // has been changed | |
5775 | // | |
33ac7e6f | 5776 | void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) ) |
f85afd4e | 5777 | { |
9f7aee01 | 5778 | if ( !m_gridLinesEnabled ) |
4db6714b | 5779 | return; |
f85afd4e | 5780 | |
2d66e025 | 5781 | int top, bottom, left, right; |
796df70a | 5782 | |
ff72f628 VZ |
5783 | int cw, ch; |
5784 | m_gridWin->GetClientSize(&cw, &ch); | |
5785 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
5786 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
f85afd4e | 5787 | |
9496deb5 | 5788 | // avoid drawing grid lines past the last row and col |
9f7aee01 VZ |
5789 | if ( m_gridLinesClipHorz ) |
5790 | { | |
5791 | if ( !m_numCols ) | |
5792 | return; | |
5793 | ||
5794 | const int lastColRight = GetColRight(GetColAt(m_numCols - 1)); | |
5795 | if ( right > lastColRight ) | |
5796 | right = lastColRight; | |
5797 | } | |
5798 | ||
5799 | if ( m_gridLinesClipVert ) | |
5800 | { | |
5801 | if ( !m_numRows ) | |
5802 | return; | |
5803 | ||
5804 | const int lastRowBottom = GetRowBottom(m_numRows - 1); | |
5805 | if ( bottom > lastRowBottom ) | |
5806 | bottom = lastRowBottom; | |
5807 | } | |
9496deb5 | 5808 | |
27f35b66 | 5809 | // no gridlines inside multicells, clip them out |
d4175745 | 5810 | int leftCol = GetColPos( internalXToCol(left) ); |
a9339fe2 | 5811 | int topRow = internalYToRow(top); |
d4175745 | 5812 | int rightCol = GetColPos( internalXToCol(right) ); |
a967f048 | 5813 | int bottomRow = internalYToRow(bottom); |
27f35b66 | 5814 | |
c03bf0c7 | 5815 | wxRegion clippedcells(0, 0, cw, ch); |
27f35b66 | 5816 | |
ff72f628 | 5817 | int cell_rows, cell_cols; |
a967f048 | 5818 | wxRect rect; |
27f35b66 | 5819 | |
ff72f628 | 5820 | for ( int j = topRow; j <= bottomRow; j++ ) |
a967f048 | 5821 | { |
ff72f628 | 5822 | for ( int colPos = leftCol; colPos <= rightCol; colPos++ ) |
27f35b66 | 5823 | { |
ff72f628 | 5824 | int i = GetColAt( colPos ); |
d4175745 | 5825 | |
a967f048 RG |
5826 | GetCellSize( j, i, &cell_rows, &cell_cols ); |
5827 | if ((cell_rows > 1) || (cell_cols > 1)) | |
27f35b66 | 5828 | { |
a967f048 RG |
5829 | rect = CellToRect(j,i); |
5830 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
5831 | clippedcells.Subtract(rect); | |
5832 | } | |
5833 | else if ((cell_rows < 0) || (cell_cols < 0)) | |
5834 | { | |
a9339fe2 | 5835 | rect = CellToRect(j + cell_rows, i + cell_cols); |
a967f048 RG |
5836 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
5837 | clippedcells.Subtract(rect); | |
27f35b66 SN |
5838 | } |
5839 | } | |
5840 | } | |
a9339fe2 | 5841 | |
c1bc8d9f | 5842 | dc.SetDeviceClippingRegion( clippedcells ); |
27f35b66 | 5843 | |
f31d3faf VZ |
5844 | DoDrawGridLines(dc, |
5845 | top, left, bottom, right, | |
5846 | topRow, leftCol, m_numRows, m_numCols); | |
f85afd4e | 5847 | |
f31d3faf VZ |
5848 | dc.DestroyClippingRegion(); |
5849 | } | |
5850 | ||
5851 | void | |
5852 | wxGrid::DoDrawGridLines(wxDC& dc, | |
5853 | int top, int left, | |
5854 | int bottom, int right, | |
5855 | int topRow, int leftCol, | |
5856 | int bottomRow, int rightCol) | |
5857 | { | |
2d66e025 | 5858 | // horizontal grid lines |
f31d3faf | 5859 | for ( int i = topRow; i < bottomRow; i++ ) |
f85afd4e | 5860 | { |
6d55126d | 5861 | int bot = GetRowBottom(i) - 1; |
7c1cb261 | 5862 | |
6d55126d | 5863 | if ( bot > bottom ) |
2d66e025 | 5864 | break; |
7c1cb261 | 5865 | |
6d55126d | 5866 | if ( bot >= top ) |
2d66e025 | 5867 | { |
3d3f3e37 | 5868 | dc.SetPen( GetRowGridLinePen(i) ); |
6d55126d | 5869 | dc.DrawLine( left, bot, right, bot ); |
2d66e025 | 5870 | } |
f85afd4e MB |
5871 | } |
5872 | ||
2d66e025 | 5873 | // vertical grid lines |
f31d3faf | 5874 | for ( int colPos = leftCol; colPos < rightCol; colPos++ ) |
f85afd4e | 5875 | { |
ff72f628 | 5876 | int i = GetColAt( colPos ); |
d4175745 | 5877 | |
2121eb69 RR |
5878 | int colRight = GetColRight(i); |
5879 | #ifdef __WXGTK__ | |
5880 | if (GetLayoutDirection() != wxLayout_RightToLeft) | |
5881 | #endif | |
5882 | colRight--; | |
5883 | ||
7c1cb261 | 5884 | if ( colRight > right ) |
2d66e025 | 5885 | break; |
7c1cb261 VZ |
5886 | |
5887 | if ( colRight >= left ) | |
2d66e025 | 5888 | { |
3d3f3e37 | 5889 | dc.SetPen( GetColGridLinePen(i) ); |
7c1cb261 | 5890 | dc.DrawLine( colRight, top, colRight, bottom ); |
2d66e025 MB |
5891 | } |
5892 | } | |
5893 | } | |
f85afd4e | 5894 | |
c2f5b920 | 5895 | void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows) |
2d66e025 | 5896 | { |
4db6714b KH |
5897 | if ( !m_numRows ) |
5898 | return; | |
60ff3b99 | 5899 | |
ff72f628 VZ |
5900 | const size_t numLabels = rows.GetCount(); |
5901 | for ( size_t i = 0; i < numLabels; i++ ) | |
2d66e025 | 5902 | { |
d10f4bf9 | 5903 | DrawRowLabel( dc, rows[i] ); |
60ff3b99 | 5904 | } |
f85afd4e MB |
5905 | } |
5906 | ||
2d66e025 | 5907 | void wxGrid::DrawRowLabel( wxDC& dc, int row ) |
f85afd4e | 5908 | { |
659af826 | 5909 | if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 ) |
7c1cb261 | 5910 | return; |
60ff3b99 | 5911 | |
670d0177 VZ |
5912 | wxGridCellAttrProvider * const |
5913 | attrProvider = m_table ? m_table->GetAttrProvider() : NULL; | |
7a0a6cc8 VZ |
5914 | |
5915 | // notice that an explicit static_cast is needed to avoid a compilation | |
5916 | // error with VC7.1 which, for some reason, tries to instantiate (abstract) | |
5917 | // wxGridRowHeaderRenderer class without it | |
ba9574c3 | 5918 | const wxGridRowHeaderRenderer& |
670d0177 | 5919 | rend = attrProvider ? attrProvider->GetRowHeaderRenderer(row) |
7a0a6cc8 VZ |
5920 | : static_cast<const wxGridRowHeaderRenderer&> |
5921 | (gs_defaultHeaderRenderers.rowRenderer); | |
5922 | ||
ba9574c3 VZ |
5923 | wxRect rect(0, GetRowTop(row), m_rowLabelWidth, GetRowHeight(row)); |
5924 | rend.DrawBorder(*this, dc, rect); | |
8f177c8e | 5925 | |
f85afd4e | 5926 | int hAlign, vAlign; |
ba9574c3 | 5927 | GetRowLabelAlignment(&hAlign, &vAlign); |
60ff3b99 | 5928 | |
ba9574c3 VZ |
5929 | rend.DrawLabel(*this, dc, GetRowLabelValue(row), |
5930 | rect, hAlign, vAlign, wxHORIZONTAL); | |
f85afd4e MB |
5931 | } |
5932 | ||
ad805b9e VZ |
5933 | void wxGrid::UseNativeColHeader(bool native) |
5934 | { | |
5935 | if ( native == m_useNativeHeader ) | |
5936 | return; | |
5937 | ||
5938 | delete m_colWindow; | |
5939 | m_useNativeHeader = native; | |
5940 | ||
5941 | CreateColumnWindow(); | |
5942 | ||
5943 | if ( m_useNativeHeader ) | |
3039ade9 | 5944 | GetGridColHeader()->SetColumnCount(m_numCols); |
ad805b9e VZ |
5945 | CalcWindowSizes(); |
5946 | } | |
5947 | ||
71cf399f RR |
5948 | void wxGrid::SetUseNativeColLabels( bool native ) |
5949 | { | |
ad805b9e VZ |
5950 | wxASSERT_MSG( !m_useNativeHeader, |
5951 | "doesn't make sense when using native header" ); | |
5952 | ||
71cf399f RR |
5953 | m_nativeColumnLabels = native; |
5954 | if (native) | |
5955 | { | |
5956 | int height = wxRendererNative::Get().GetHeaderButtonHeight( this ); | |
5957 | SetColLabelSize( height ); | |
5958 | } | |
76c66f19 | 5959 | |
ad805b9e | 5960 | GetColLabelWindow()->Refresh(); |
ff72f628 | 5961 | m_cornerLabelWin->Refresh(); |
71cf399f RR |
5962 | } |
5963 | ||
d10f4bf9 | 5964 | void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols ) |
f85afd4e | 5965 | { |
4db6714b KH |
5966 | if ( !m_numCols ) |
5967 | return; | |
60ff3b99 | 5968 | |
ff72f628 VZ |
5969 | const size_t numLabels = cols.GetCount(); |
5970 | for ( size_t i = 0; i < numLabels; i++ ) | |
f85afd4e | 5971 | { |
d10f4bf9 | 5972 | DrawColLabel( dc, cols[i] ); |
60ff3b99 | 5973 | } |
f85afd4e MB |
5974 | } |
5975 | ||
ff72f628 VZ |
5976 | void wxGrid::DrawCornerLabel(wxDC& dc) |
5977 | { | |
ba9574c3 VZ |
5978 | wxRect rect(wxSize(m_rowLabelWidth, m_colLabelHeight)); |
5979 | ||
ff72f628 VZ |
5980 | if ( m_nativeColumnLabels ) |
5981 | { | |
ff72f628 VZ |
5982 | rect.Deflate(1); |
5983 | ||
5984 | wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin, dc, rect, 0); | |
5985 | } | |
5986 | else | |
5987 | { | |
ba9574c3 VZ |
5988 | rect.width++; |
5989 | rect.height++; | |
5990 | ||
670d0177 VZ |
5991 | wxGridCellAttrProvider * const |
5992 | attrProvider = m_table ? m_table->GetAttrProvider() : NULL; | |
ba9574c3 | 5993 | const wxGridCornerHeaderRenderer& |
670d0177 | 5994 | rend = attrProvider ? attrProvider->GetCornerRenderer() |
7a0a6cc8 VZ |
5995 | : static_cast<wxGridCornerHeaderRenderer&> |
5996 | (gs_defaultHeaderRenderers.cornerRenderer); | |
ff72f628 | 5997 | |
ba9574c3 | 5998 | rend.DrawBorder(*this, dc, rect); |
ff72f628 VZ |
5999 | } |
6000 | } | |
6001 | ||
6002 | void wxGrid::DrawColLabel(wxDC& dc, int col) | |
f85afd4e | 6003 | { |
659af826 | 6004 | if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 ) |
7c1cb261 | 6005 | return; |
60ff3b99 | 6006 | |
4d1bc39c | 6007 | int colLeft = GetColLeft(col); |
ec157c8f | 6008 | |
ff72f628 | 6009 | wxRect rect(colLeft, 0, GetColWidth(col), m_colLabelHeight); |
670d0177 VZ |
6010 | wxGridCellAttrProvider * const |
6011 | attrProvider = m_table ? m_table->GetAttrProvider() : NULL; | |
ba9574c3 | 6012 | const wxGridColumnHeaderRenderer& |
670d0177 | 6013 | rend = attrProvider ? attrProvider->GetColumnHeaderRenderer(col) |
7a0a6cc8 VZ |
6014 | : static_cast<wxGridColumnHeaderRenderer&> |
6015 | (gs_defaultHeaderRenderers.colRenderer); | |
2f024384 | 6016 | |
ff72f628 | 6017 | if ( m_nativeColumnLabels ) |
71cf399f | 6018 | { |
11393d29 VZ |
6019 | wxRendererNative::Get().DrawHeaderButton |
6020 | ( | |
6021 | GetColLabelWindow(), | |
6022 | dc, | |
6023 | rect, | |
6024 | 0, | |
6025 | IsSortingBy(col) | |
6026 | ? IsSortOrderAscending() | |
6027 | ? wxHDR_SORT_ICON_UP | |
6028 | : wxHDR_SORT_ICON_DOWN | |
6029 | : wxHDR_SORT_ICON_NONE | |
6030 | ); | |
ba9574c3 | 6031 | rect.Deflate(2); |
71cf399f RR |
6032 | } |
6033 | else | |
6034 | { | |
b5e97a17 VZ |
6035 | // It is reported that we need to erase the background to avoid display |
6036 | // artefacts, see #12055. | |
6037 | wxDCBrushChanger setBrush(dc, m_colWindow->GetBackgroundColour()); | |
6038 | dc.DrawRectangle(rect); | |
6039 | ||
ba9574c3 | 6040 | rend.DrawBorder(*this, dc, rect); |
71cf399f | 6041 | } |
2f024384 | 6042 | |
ff72f628 | 6043 | int hAlign, vAlign; |
ba9574c3 | 6044 | GetColLabelAlignment(&hAlign, &vAlign); |
ff72f628 | 6045 | const int orient = GetColLabelTextOrientation(); |
60ff3b99 | 6046 | |
ba9574c3 | 6047 | rend.DrawLabel(*this, dc, GetColLabelValue(col), rect, hAlign, vAlign, orient); |
f85afd4e MB |
6048 | } |
6049 | ||
ff72f628 VZ |
6050 | // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which |
6051 | // we just have to add textOrientation support | |
2d66e025 MB |
6052 | void wxGrid::DrawTextRectangle( wxDC& dc, |
6053 | const wxString& value, | |
6054 | const wxRect& rect, | |
6055 | int horizAlign, | |
d43851f7 | 6056 | int vertAlign, |
ba9574c3 | 6057 | int textOrientation ) const |
f85afd4e | 6058 | { |
2d66e025 | 6059 | wxArrayString lines; |
ef5df12b | 6060 | |
2d66e025 | 6061 | StringToLines( value, lines ); |
ef5df12b | 6062 | |
ff72f628 | 6063 | DrawTextRectangle(dc, lines, rect, horizAlign, vertAlign, textOrientation); |
d10f4bf9 VZ |
6064 | } |
6065 | ||
4330c974 | 6066 | void wxGrid::DrawTextRectangle(wxDC& dc, |
038a5591 JS |
6067 | const wxArrayString& lines, |
6068 | const wxRect& rect, | |
6069 | int horizAlign, | |
6070 | int vertAlign, | |
ba9574c3 | 6071 | int textOrientation) const |
d10f4bf9 | 6072 | { |
4330c974 VZ |
6073 | if ( lines.empty() ) |
6074 | return; | |
ef5df12b | 6075 | |
4330c974 | 6076 | wxDCClipper clip(dc, rect); |
ef5df12b | 6077 | |
4330c974 VZ |
6078 | long textWidth, |
6079 | textHeight; | |
ef5df12b | 6080 | |
4330c974 VZ |
6081 | if ( textOrientation == wxHORIZONTAL ) |
6082 | GetTextBoxSize( dc, lines, &textWidth, &textHeight ); | |
6083 | else | |
6084 | GetTextBoxSize( dc, lines, &textHeight, &textWidth ); | |
ef5df12b | 6085 | |
4330c974 VZ |
6086 | int x = 0, |
6087 | y = 0; | |
6088 | switch ( vertAlign ) | |
6089 | { | |
73145b0e | 6090 | case wxALIGN_BOTTOM: |
4db6714b | 6091 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 | 6092 | y = rect.y + (rect.height - textHeight - 1); |
d43851f7 | 6093 | else |
038a5591 JS |
6094 | x = rect.x + rect.width - textWidth; |
6095 | break; | |
ef5df12b | 6096 | |
73145b0e | 6097 | case wxALIGN_CENTRE: |
4db6714b | 6098 | if ( textOrientation == wxHORIZONTAL ) |
a9339fe2 | 6099 | y = rect.y + ((rect.height - textHeight) / 2); |
d43851f7 | 6100 | else |
a9339fe2 | 6101 | x = rect.x + ((rect.width - textWidth) / 2); |
038a5591 | 6102 | break; |
ef5df12b | 6103 | |
73145b0e JS |
6104 | case wxALIGN_TOP: |
6105 | default: | |
4db6714b | 6106 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 | 6107 | y = rect.y + 1; |
d43851f7 | 6108 | else |
038a5591 | 6109 | x = rect.x + 1; |
73145b0e | 6110 | break; |
4330c974 | 6111 | } |
ef5df12b | 6112 | |
4330c974 VZ |
6113 | // Align each line of a multi-line label |
6114 | size_t nLines = lines.GetCount(); | |
6115 | for ( size_t l = 0; l < nLines; l++ ) | |
6116 | { | |
6117 | const wxString& line = lines[l]; | |
ef5df12b | 6118 | |
9b7d3c09 VZ |
6119 | if ( line.empty() ) |
6120 | { | |
6121 | *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight(); | |
6122 | continue; | |
6123 | } | |
6124 | ||
ad2633bd | 6125 | wxCoord lineWidth = 0, |
c2b2c10e | 6126 | lineHeight = 0; |
4330c974 VZ |
6127 | dc.GetTextExtent(line, &lineWidth, &lineHeight); |
6128 | ||
6129 | switch ( horizAlign ) | |
6130 | { | |
038a5591 | 6131 | case wxALIGN_RIGHT: |
4db6714b | 6132 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 JS |
6133 | x = rect.x + (rect.width - lineWidth - 1); |
6134 | else | |
6135 | y = rect.y + lineWidth + 1; | |
6136 | break; | |
ef5df12b | 6137 | |
038a5591 | 6138 | case wxALIGN_CENTRE: |
4db6714b | 6139 | if ( textOrientation == wxHORIZONTAL ) |
2f024384 | 6140 | x = rect.x + ((rect.width - lineWidth) / 2); |
038a5591 | 6141 | else |
2f024384 | 6142 | y = rect.y + rect.height - ((rect.height - lineWidth) / 2); |
038a5591 | 6143 | break; |
ef5df12b | 6144 | |
038a5591 JS |
6145 | case wxALIGN_LEFT: |
6146 | default: | |
4db6714b | 6147 | if ( textOrientation == wxHORIZONTAL ) |
038a5591 JS |
6148 | x = rect.x + 1; |
6149 | else | |
6150 | y = rect.y + rect.height - 1; | |
6151 | break; | |
4330c974 | 6152 | } |
ef5df12b | 6153 | |
4330c974 VZ |
6154 | if ( textOrientation == wxHORIZONTAL ) |
6155 | { | |
6156 | dc.DrawText( line, x, y ); | |
6157 | y += lineHeight; | |
6158 | } | |
6159 | else | |
6160 | { | |
6161 | dc.DrawRotatedText( line, x, y, 90.0 ); | |
6162 | x += lineHeight; | |
038a5591 | 6163 | } |
f85afd4e | 6164 | } |
f85afd4e MB |
6165 | } |
6166 | ||
2f024384 DS |
6167 | // Split multi-line text up into an array of strings. |
6168 | // Any existing contents of the string array are preserved. | |
f85afd4e | 6169 | // |
696f3e9d | 6170 | // TODO: refactor wxTextFile::Read() and reuse the same code from here |
ef316e23 | 6171 | void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) const |
f85afd4e | 6172 | { |
f85afd4e MB |
6173 | int startPos = 0; |
6174 | int pos; | |
6d004f67 | 6175 | wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix ); |
2433bb2e | 6176 | wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix ); |
e2b42eeb | 6177 | |
faa94f3e | 6178 | while ( startPos < (int)tVal.length() ) |
f85afd4e | 6179 | { |
2433bb2e | 6180 | pos = tVal.Mid(startPos).Find( eol ); |
f85afd4e MB |
6181 | if ( pos < 0 ) |
6182 | { | |
6183 | break; | |
6184 | } | |
6185 | else if ( pos == 0 ) | |
6186 | { | |
6187 | lines.Add( wxEmptyString ); | |
6188 | } | |
6189 | else | |
6190 | { | |
696f3e9d | 6191 | lines.Add( tVal.Mid(startPos, pos) ); |
f85afd4e | 6192 | } |
a9339fe2 | 6193 | |
4db6714b | 6194 | startPos += pos + 1; |
f85afd4e | 6195 | } |
4db6714b | 6196 | |
2eafd712 | 6197 | if ( startPos < (int)tVal.length() ) |
f85afd4e | 6198 | { |
696f3e9d | 6199 | lines.Add( tVal.Mid( startPos ) ); |
f85afd4e MB |
6200 | } |
6201 | } | |
6202 | ||
fbfb8bcc | 6203 | void wxGrid::GetTextBoxSize( const wxDC& dc, |
d10f4bf9 | 6204 | const wxArrayString& lines, |
ef316e23 | 6205 | long *width, long *height ) const |
f85afd4e | 6206 | { |
ad2633bd RR |
6207 | wxCoord w = 0; |
6208 | wxCoord h = 0; | |
6209 | wxCoord lineW = 0, lineH = 0; | |
f85afd4e | 6210 | |
b540eb2b | 6211 | size_t i; |
56b6cf26 | 6212 | for ( i = 0; i < lines.GetCount(); i++ ) |
f85afd4e MB |
6213 | { |
6214 | dc.GetTextExtent( lines[i], &lineW, &lineH ); | |
6215 | w = wxMax( w, lineW ); | |
6216 | h += lineH; | |
6217 | } | |
6218 | ||
6219 | *width = w; | |
6220 | *height = h; | |
6221 | } | |
6222 | ||
f6bcfd97 BP |
6223 | // |
6224 | // ------ Batch processing. | |
6225 | // | |
6226 | void wxGrid::EndBatch() | |
6227 | { | |
6228 | if ( m_batchCount > 0 ) | |
6229 | { | |
6230 | m_batchCount--; | |
6231 | if ( !m_batchCount ) | |
6232 | { | |
6233 | CalcDimensions(); | |
6234 | m_rowLabelWin->Refresh(); | |
ad805b9e | 6235 | m_colWindow->Refresh(); |
f6bcfd97 BP |
6236 | m_cornerLabelWin->Refresh(); |
6237 | m_gridWin->Refresh(); | |
6238 | } | |
6239 | } | |
6240 | } | |
f85afd4e | 6241 | |
d8232393 MB |
6242 | // Use this, rather than wxWindow::Refresh(), to force an immediate |
6243 | // repainting of the grid. Has no effect if you are already inside a | |
6244 | // BeginBatch / EndBatch block. | |
6245 | // | |
6246 | void wxGrid::ForceRefresh() | |
6247 | { | |
6248 | BeginBatch(); | |
6249 | EndBatch(); | |
6250 | } | |
6251 | ||
bf646121 VZ |
6252 | bool wxGrid::Enable(bool enable) |
6253 | { | |
6254 | if ( !wxScrolledWindow::Enable(enable) ) | |
6255 | return false; | |
6256 | ||
6257 | // redraw in the new state | |
6258 | m_gridWin->Refresh(); | |
6259 | ||
6260 | return true; | |
6261 | } | |
d8232393 | 6262 | |
f85afd4e | 6263 | // |
2d66e025 | 6264 | // ------ Edit control functions |
f85afd4e MB |
6265 | // |
6266 | ||
2d66e025 | 6267 | void wxGrid::EnableEditing( bool edit ) |
f85afd4e | 6268 | { |
2d66e025 | 6269 | if ( edit != m_editable ) |
f85afd4e | 6270 | { |
4db6714b KH |
6271 | if (!edit) |
6272 | EnableCellEditControl(edit); | |
2d66e025 | 6273 | m_editable = edit; |
f85afd4e | 6274 | } |
f85afd4e MB |
6275 | } |
6276 | ||
2d66e025 | 6277 | void wxGrid::EnableCellEditControl( bool enable ) |
f85afd4e | 6278 | { |
2c9a89e0 RD |
6279 | if (! m_editable) |
6280 | return; | |
6281 | ||
2c9a89e0 RD |
6282 | if ( enable != m_cellEditCtrlEnabled ) |
6283 | { | |
dcfe4c3d | 6284 | if ( enable ) |
f85afd4e | 6285 | { |
8a3e536c | 6286 | if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN) == -1 ) |
97a9929e | 6287 | return; |
fe7b9ed6 | 6288 | |
97a9929e | 6289 | // this should be checked by the caller! |
9a83f860 | 6290 | wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") ); |
b54ba671 VZ |
6291 | |
6292 | // do it before ShowCellEditControl() | |
dcfe4c3d | 6293 | m_cellEditCtrlEnabled = enable; |
b54ba671 | 6294 | |
2d66e025 | 6295 | ShowCellEditControl(); |
2d66e025 MB |
6296 | } |
6297 | else | |
6298 | { | |
8a3e536c | 6299 | SendEvent(wxEVT_GRID_EDITOR_HIDDEN); |
fe7b9ed6 | 6300 | |
97a9929e | 6301 | HideCellEditControl(); |
2d66e025 | 6302 | SaveEditControlValue(); |
b54ba671 VZ |
6303 | |
6304 | // do it after HideCellEditControl() | |
dcfe4c3d | 6305 | m_cellEditCtrlEnabled = enable; |
f85afd4e | 6306 | } |
f85afd4e | 6307 | } |
f85afd4e | 6308 | } |
f85afd4e | 6309 | |
b54ba671 | 6310 | bool wxGrid::IsCurrentCellReadOnly() const |
283b7808 | 6311 | { |
f48a1159 VZ |
6312 | wxGridCellAttr* |
6313 | attr = const_cast<wxGrid *>(this)->GetCellAttr(m_currentCellCoords); | |
b54ba671 VZ |
6314 | bool readonly = attr->IsReadOnly(); |
6315 | attr->DecRef(); | |
283b7808 | 6316 | |
b54ba671 VZ |
6317 | return readonly; |
6318 | } | |
283b7808 | 6319 | |
b54ba671 VZ |
6320 | bool wxGrid::CanEnableCellControl() const |
6321 | { | |
20c84410 SN |
6322 | return m_editable && (m_currentCellCoords != wxGridNoCellCoords) && |
6323 | !IsCurrentCellReadOnly(); | |
b54ba671 VZ |
6324 | } |
6325 | ||
6326 | bool wxGrid::IsCellEditControlEnabled() const | |
6327 | { | |
6328 | // the cell edit control might be disable for all cells or just for the | |
6329 | // current one if it's read only | |
ca65c044 | 6330 | return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false; |
283b7808 VZ |
6331 | } |
6332 | ||
f6bcfd97 BP |
6333 | bool wxGrid::IsCellEditControlShown() const |
6334 | { | |
ca65c044 | 6335 | bool isShown = false; |
f6bcfd97 BP |
6336 | |
6337 | if ( m_cellEditCtrlEnabled ) | |
6338 | { | |
6339 | int row = m_currentCellCoords.GetRow(); | |
6340 | int col = m_currentCellCoords.GetCol(); | |
6341 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
6342 | wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col); | |
6343 | attr->DecRef(); | |
6344 | ||
6345 | if ( editor ) | |
6346 | { | |
6347 | if ( editor->IsCreated() ) | |
6348 | { | |
6349 | isShown = editor->GetControl()->IsShown(); | |
6350 | } | |
6351 | ||
6352 | editor->DecRef(); | |
6353 | } | |
6354 | } | |
6355 | ||
6356 | return isShown; | |
6357 | } | |
6358 | ||
2d66e025 | 6359 | void wxGrid::ShowCellEditControl() |
f85afd4e | 6360 | { |
2d66e025 | 6361 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 6362 | { |
7db713ae | 6363 | if ( !IsVisible( m_currentCellCoords, false ) ) |
2d66e025 | 6364 | { |
ca65c044 | 6365 | m_cellEditCtrlEnabled = false; |
2d66e025 MB |
6366 | return; |
6367 | } | |
6368 | else | |
6369 | { | |
2c9a89e0 RD |
6370 | wxRect rect = CellToRect( m_currentCellCoords ); |
6371 | int row = m_currentCellCoords.GetRow(); | |
6372 | int col = m_currentCellCoords.GetCol(); | |
2d66e025 | 6373 | |
27f35b66 SN |
6374 | // if this is part of a multicell, find owner (topleft) |
6375 | int cell_rows, cell_cols; | |
6376 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
6377 | if ( cell_rows <= 0 || cell_cols <= 0 ) | |
6378 | { | |
6379 | row += cell_rows; | |
6380 | col += cell_cols; | |
6381 | m_currentCellCoords.SetRow( row ); | |
6382 | m_currentCellCoords.SetCol( col ); | |
6383 | } | |
6384 | ||
99306db2 VZ |
6385 | // erase the highlight and the cell contents because the editor |
6386 | // might not cover the entire cell | |
6387 | wxClientDC dc( m_gridWin ); | |
6388 | PrepareDC( dc ); | |
2c03d771 | 6389 | wxGridCellAttr* attr = GetCellAttr(row, col); |
ff72f628 | 6390 | dc.SetBrush(wxBrush(attr->GetBackgroundColour())); |
99306db2 VZ |
6391 | dc.SetPen(*wxTRANSPARENT_PEN); |
6392 | dc.DrawRectangle(rect); | |
d2fdd8d2 | 6393 | |
6f706ee0 VZ |
6394 | // convert to scrolled coords |
6395 | CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
6396 | ||
6397 | int nXMove = 0; | |
6398 | if (rect.x < 0) | |
6399 | nXMove = rect.x; | |
6400 | ||
99306db2 | 6401 | // cell is shifted by one pixel |
68c5a31c | 6402 | // However, don't allow x or y to become negative |
70e8d961 | 6403 | // since the SetSize() method interprets that as |
68c5a31c SN |
6404 | // "don't change." |
6405 | if (rect.x > 0) | |
6406 | rect.x--; | |
6407 | if (rect.y > 0) | |
6408 | rect.y--; | |
f0102d2a | 6409 | |
28a77bc4 | 6410 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
3da93aae VZ |
6411 | if ( !editor->IsCreated() ) |
6412 | { | |
ca65c044 | 6413 | editor->Create(m_gridWin, wxID_ANY, |
2c9a89e0 | 6414 | new wxGridCellEditorEvtHandler(this, editor)); |
bf7945ce RD |
6415 | |
6416 | wxGridEditorCreatedEvent evt(GetId(), | |
6417 | wxEVT_GRID_EDITOR_CREATED, | |
6418 | this, | |
6419 | row, | |
6420 | col, | |
6421 | editor->GetControl()); | |
6422 | GetEventHandler()->ProcessEvent(evt); | |
2d66e025 MB |
6423 | } |
6424 | ||
27f35b66 | 6425 | // resize editor to overflow into righthand cells if allowed |
39b80349 | 6426 | int maxWidth = rect.width; |
27f35b66 SN |
6427 | wxString value = GetCellValue(row, col); |
6428 | if ( (value != wxEmptyString) && (attr->GetOverflow()) ) | |
6429 | { | |
39b80349 | 6430 | int y; |
2f024384 DS |
6431 | GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont()); |
6432 | if (maxWidth < rect.width) | |
6433 | maxWidth = rect.width; | |
39b80349 | 6434 | } |
4db6714b | 6435 | |
39b80349 | 6436 | int client_right = m_gridWin->GetClientSize().GetWidth(); |
2f024384 | 6437 | if (rect.x + maxWidth > client_right) |
2b5f62a0 | 6438 | maxWidth = client_right - rect.x; |
39b80349 | 6439 | |
2b5f62a0 VZ |
6440 | if ((maxWidth > rect.width) && (col < m_numCols) && m_table) |
6441 | { | |
39b80349 | 6442 | GetCellSize( row, col, &cell_rows, &cell_cols ); |
2b5f62a0 | 6443 | // may have changed earlier |
2f024384 | 6444 | for (int i = col + cell_cols; i < m_numCols; i++) |
2b5f62a0 | 6445 | { |
39b80349 SN |
6446 | int c_rows, c_cols; |
6447 | GetCellSize( row, i, &c_rows, &c_cols ); | |
2f024384 | 6448 | |
2b5f62a0 | 6449 | // looks weird going over a multicell |
bee19958 | 6450 | if (m_table->IsEmptyCell( row, i ) && |
2b5f62a0 | 6451 | (rect.width < maxWidth) && (c_rows == 1)) |
2f024384 | 6452 | { |
bee19958 | 6453 | rect.width += GetColWidth( i ); |
2f024384 | 6454 | } |
2b5f62a0 VZ |
6455 | else |
6456 | break; | |
6457 | } | |
4db6714b | 6458 | |
39b80349 | 6459 | if (rect.GetRight() > client_right) |
bee19958 | 6460 | rect.SetRight( client_right - 1 ); |
27f35b66 | 6461 | } |
76c66f19 | 6462 | |
bee19958 | 6463 | editor->SetCellAttr( attr ); |
2c9a89e0 | 6464 | editor->SetSize( rect ); |
e1a66d9a RD |
6465 | if (nXMove != 0) |
6466 | editor->GetControl()->Move( | |
6467 | editor->GetControl()->GetPosition().x + nXMove, | |
6468 | editor->GetControl()->GetPosition().y ); | |
ca65c044 | 6469 | editor->Show( true, attr ); |
04418332 VZ |
6470 | |
6471 | // recalc dimensions in case we need to | |
6472 | // expand the scrolled window to account for editor | |
73145b0e | 6473 | CalcDimensions(); |
99306db2 | 6474 | |
3da93aae | 6475 | editor->BeginEdit(row, col, this); |
1bd71df9 | 6476 | editor->SetCellAttr(NULL); |
0b190b0f VZ |
6477 | |
6478 | editor->DecRef(); | |
2c9a89e0 | 6479 | attr->DecRef(); |
2b5f62a0 | 6480 | } |
f85afd4e MB |
6481 | } |
6482 | } | |
6483 | ||
2d66e025 | 6484 | void wxGrid::HideCellEditControl() |
f85afd4e | 6485 | { |
2d66e025 | 6486 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 6487 | { |
2c9a89e0 RD |
6488 | int row = m_currentCellCoords.GetRow(); |
6489 | int col = m_currentCellCoords.GetCol(); | |
6490 | ||
bee19958 | 6491 | wxGridCellAttr *attr = GetCellAttr(row, col); |
0b190b0f | 6492 | wxGridCellEditor *editor = attr->GetEditor(this, row, col); |
7d277ac0 | 6493 | const bool editorHadFocus = editor->GetControl()->HasFocus(); |
ca65c044 | 6494 | editor->Show( false ); |
0b190b0f | 6495 | editor->DecRef(); |
2c9a89e0 | 6496 | attr->DecRef(); |
2fb3e528 | 6497 | |
7d277ac0 VZ |
6498 | // return the focus to the grid itself if the editor had it |
6499 | // | |
6500 | // note that we must not do this unconditionally to avoid stealing | |
6501 | // focus from the window which just received it if we are hiding the | |
6502 | // editor precisely because we lost focus | |
6503 | if ( editorHadFocus ) | |
6504 | m_gridWin->SetFocus(); | |
2fb3e528 | 6505 | |
27f35b66 SN |
6506 | // refresh whole row to the right |
6507 | wxRect rect( CellToRect(row, col) ); | |
6508 | CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y ); | |
6509 | rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x; | |
2f024384 | 6510 | |
7d75e6c6 RD |
6511 | #ifdef __WXMAC__ |
6512 | // ensure that the pixels under the focus ring get refreshed as well | |
4db6714b | 6513 | rect.Inflate(10, 10); |
7d75e6c6 | 6514 | #endif |
2f024384 | 6515 | |
ca65c044 | 6516 | m_gridWin->Refresh( false, &rect ); |
f85afd4e | 6517 | } |
2d66e025 | 6518 | } |
8f177c8e | 6519 | |
2d66e025 MB |
6520 | void wxGrid::SaveEditControlValue() |
6521 | { | |
3da93aae VZ |
6522 | if ( IsCellEditControlEnabled() ) |
6523 | { | |
2c9a89e0 RD |
6524 | int row = m_currentCellCoords.GetRow(); |
6525 | int col = m_currentCellCoords.GetCol(); | |
8f177c8e | 6526 | |
4db6714b | 6527 | wxString oldval = GetCellValue(row, col); |
fe7b9ed6 | 6528 | |
3da93aae | 6529 | wxGridCellAttr* attr = GetCellAttr(row, col); |
28a77bc4 | 6530 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
2d66e025 | 6531 | |
763163a8 | 6532 | wxString newval; |
78e78812 | 6533 | bool changed = editor->EndEdit(row, col, this, oldval, &newval); |
2d66e025 | 6534 | |
763163a8 | 6535 | if ( changed && SendEvent(wxEVT_GRID_CELL_CHANGING, newval) != -1 ) |
3da93aae | 6536 | { |
763163a8 VZ |
6537 | editor->ApplyEdit(row, col, this); |
6538 | ||
6539 | // for compatibility reasons dating back to wx 2.8 when this event | |
6540 | // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING | |
6541 | // didn't exist we allow vetoing this one too | |
6542 | if ( SendEvent(wxEVT_GRID_CELL_CHANGED, oldval) == -1 ) | |
4db6714b | 6543 | { |
97a9929e | 6544 | // Event has been vetoed, set the data back. |
4db6714b | 6545 | SetCellValue(row, col, oldval); |
97a9929e | 6546 | } |
2d66e025 | 6547 | } |
763163a8 VZ |
6548 | |
6549 | editor->DecRef(); | |
6550 | attr->DecRef(); | |
f85afd4e MB |
6551 | } |
6552 | } | |
6553 | ||
2d66e025 | 6554 | // |
60ff3b99 VZ |
6555 | // ------ Grid location functions |
6556 | // Note that all of these functions work with the logical coordinates of | |
2d66e025 MB |
6557 | // grid cells and labels so you will need to convert from device |
6558 | // coordinates for mouse events etc. | |
6559 | // | |
6560 | ||
8a3e536c | 6561 | wxGridCellCoords wxGrid::XYToCell(int x, int y) const |
f85afd4e | 6562 | { |
58dd5b3b MB |
6563 | int row = YToRow(y); |
6564 | int col = XToCol(x); | |
6565 | ||
8a3e536c VZ |
6566 | return row == -1 || col == -1 ? wxGridNoCellCoords |
6567 | : wxGridCellCoords(row, col); | |
2d66e025 | 6568 | } |
f85afd4e | 6569 | |
bec70262 VZ |
6570 | // compute row or column from some (unscrolled) coordinate value, using either |
6571 | // m_defaultRowHeight/m_defaultColWidth or binary search on array of | |
5bce3d57 VZ |
6572 | // m_rowBottoms/m_colRights to do it quickly in O(log n) time. |
6573 | // NOTE: This may not work correctly for reordered columns. | |
cd4f6f5f VZ |
6574 | int wxGrid::PosToLinePos(int coord, |
6575 | bool clipToMinMax, | |
6576 | const wxGridOperations& oper) const | |
2d66e025 | 6577 | { |
bec70262 | 6578 | const int numLines = oper.GetNumberOfLines(this); |
a967f048 | 6579 | |
bec70262 | 6580 | if ( coord < 0 ) |
cd4f6f5f | 6581 | return clipToMinMax && numLines > 0 ? 0 : wxNOT_FOUND; |
a967f048 | 6582 | |
bec70262 VZ |
6583 | const int defaultLineSize = oper.GetDefaultLineSize(this); |
6584 | wxCHECK_MSG( defaultLineSize, -1, "can't have 0 default line size" ); | |
d57ad377 | 6585 | |
bec70262 VZ |
6586 | int maxPos = coord / defaultLineSize, |
6587 | minPos = 0; | |
8f177c8e | 6588 | |
bec70262 VZ |
6589 | // check for the simplest case: if we have no explicit line sizes |
6590 | // configured, then we already know the line this position falls in | |
6591 | const wxArrayInt& lineEnds = oper.GetLineEnds(this); | |
6592 | if ( lineEnds.empty() ) | |
f85afd4e | 6593 | { |
bec70262 VZ |
6594 | if ( maxPos < numLines ) |
6595 | return maxPos; | |
4db6714b | 6596 | |
bec70262 | 6597 | return clipToMinMax ? numLines - 1 : -1; |
f85afd4e | 6598 | } |
4db6714b | 6599 | |
2d66e025 | 6600 | |
5bce3d57 VZ |
6601 | // binary search is quite efficient and we can't really make any assumptions |
6602 | // on where to start here since row and columns could be of size 0 if they are | |
6603 | // hidden. While this could be made more efficient, some profiling will be | |
6604 | // necessary to determine if it really is a performance bottleneck | |
6605 | maxPos = numLines - 1; | |
d4175745 | 6606 | |
bec70262 VZ |
6607 | // check if the position is beyond the last column |
6608 | const int lineAtMaxPos = oper.GetLineAt(this, maxPos); | |
6609 | if ( coord >= lineEnds[lineAtMaxPos] ) | |
cd4f6f5f | 6610 | return clipToMinMax ? maxPos : -1; |
d4175745 | 6611 | |
bec70262 VZ |
6612 | // or before the first one |
6613 | const int lineAt0 = oper.GetLineAt(this, 0); | |
6614 | if ( coord < lineEnds[lineAt0] ) | |
cd4f6f5f | 6615 | return 0; |
d4175745 | 6616 | |
bec70262 VZ |
6617 | |
6618 | // finally do perform the binary search | |
6619 | while ( minPos < maxPos ) | |
d4175745 | 6620 | { |
bec70262 VZ |
6621 | wxCHECK_MSG( lineEnds[oper.GetLineAt(this, minPos)] <= coord && |
6622 | coord < lineEnds[oper.GetLineAt(this, maxPos)], | |
6623 | -1, | |
cd4f6f5f | 6624 | "wxGrid: internal error in PosToLinePos()" ); |
d4175745 | 6625 | |
bec70262 | 6626 | if ( coord >= lineEnds[oper.GetLineAt(this, maxPos - 1)] ) |
cd4f6f5f | 6627 | return maxPos; |
d4175745 VZ |
6628 | else |
6629 | maxPos--; | |
bec70262 VZ |
6630 | |
6631 | const int median = minPos + (maxPos - minPos + 1) / 2; | |
6632 | if ( coord < lineEnds[oper.GetLineAt(this, median)] ) | |
d4175745 VZ |
6633 | maxPos = median; |
6634 | else | |
6635 | minPos = median; | |
6636 | } | |
bec70262 | 6637 | |
cd4f6f5f VZ |
6638 | return maxPos; |
6639 | } | |
6640 | ||
6641 | int | |
6642 | wxGrid::PosToLine(int coord, | |
6643 | bool clipToMinMax, | |
6644 | const wxGridOperations& oper) const | |
6645 | { | |
6646 | int pos = PosToLinePos(coord, clipToMinMax, oper); | |
6647 | ||
6648 | return pos == wxNOT_FOUND ? wxNOT_FOUND : oper.GetLineAt(this, pos); | |
bec70262 VZ |
6649 | } |
6650 | ||
6651 | int wxGrid::YToRow(int y, bool clipToMinMax) const | |
6652 | { | |
6653 | return PosToLine(y, clipToMinMax, wxGridRowOperations()); | |
6654 | } | |
6655 | ||
6656 | int wxGrid::XToCol(int x, bool clipToMinMax) const | |
6657 | { | |
6658 | return PosToLine(x, clipToMinMax, wxGridColumnOperations()); | |
2d66e025 | 6659 | } |
8f177c8e | 6660 | |
cd4f6f5f VZ |
6661 | int wxGrid::XToPos(int x) const |
6662 | { | |
6663 | return PosToLinePos(x, true /* clip */, wxGridColumnOperations()); | |
6664 | } | |
6665 | ||
82edfbe7 | 6666 | // return the row number such that the y coord is near the edge of, or -1 if |
10a4531d VZ |
6667 | // not near an edge. |
6668 | // | |
82edfbe7 VZ |
6669 | // notice that position can only possibly be near an edge if the row/column is |
6670 | // large enough to still allow for an "inner" area that is _not_ near the edge | |
6671 | // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will | |
6672 | // _never_ be considered to be near the edge). | |
10a4531d | 6673 | int wxGrid::PosToEdgeOfLine(int pos, const wxGridOperations& oper) const |
2d66e025 | 6674 | { |
10a4531d VZ |
6675 | const int line = oper.PosToLine(this, pos, true); |
6676 | ||
6677 | if ( oper.GetLineSize(this, line) > WXGRID_LABEL_EDGE_ZONE ) | |
2d66e025 | 6678 | { |
10a4531d VZ |
6679 | // We know that we are in this line, test whether we are close enough |
6680 | // to start or end border, respectively. | |
6681 | if ( abs(oper.GetLineEndPos(this, line) - pos) < WXGRID_LABEL_EDGE_ZONE ) | |
6682 | return line; | |
6683 | else if ( line > 0 && | |
6684 | pos - oper.GetLineStartPos(this, | |
6685 | line) < WXGRID_LABEL_EDGE_ZONE ) | |
f71adb50 VZ |
6686 | { |
6687 | return oper.GetLineBefore(this, line); | |
6688 | } | |
f85afd4e | 6689 | } |
2d66e025 MB |
6690 | |
6691 | return -1; | |
6692 | } | |
6693 | ||
10a4531d | 6694 | int wxGrid::YToEdgeOfRow(int y) const |
2d66e025 | 6695 | { |
10a4531d VZ |
6696 | return PosToEdgeOfLine(y, wxGridRowOperations()); |
6697 | } | |
2d66e025 | 6698 | |
10a4531d VZ |
6699 | int wxGrid::XToEdgeOfCol(int x) const |
6700 | { | |
6701 | return PosToEdgeOfLine(x, wxGridColumnOperations()); | |
f85afd4e MB |
6702 | } |
6703 | ||
ef316e23 | 6704 | wxRect wxGrid::CellToRect( int row, int col ) const |
f85afd4e | 6705 | { |
2d66e025 | 6706 | wxRect rect( -1, -1, -1, -1 ); |
f85afd4e | 6707 | |
2f024384 DS |
6708 | if ( row >= 0 && row < m_numRows && |
6709 | col >= 0 && col < m_numCols ) | |
f85afd4e | 6710 | { |
27f35b66 SN |
6711 | int i, cell_rows, cell_cols; |
6712 | rect.width = rect.height = 0; | |
6713 | GetCellSize( row, col, &cell_rows, &cell_cols ); | |
6714 | // if negative then find multicell owner | |
2f024384 DS |
6715 | if (cell_rows < 0) |
6716 | row += cell_rows; | |
6717 | if (cell_cols < 0) | |
6718 | col += cell_cols; | |
27f35b66 SN |
6719 | GetCellSize( row, col, &cell_rows, &cell_cols ); |
6720 | ||
7c1cb261 VZ |
6721 | rect.x = GetColLeft(col); |
6722 | rect.y = GetRowTop(row); | |
2f024384 DS |
6723 | for (i=col; i < col + cell_cols; i++) |
6724 | rect.width += GetColWidth(i); | |
6725 | for (i=row; i < row + cell_rows; i++) | |
27f35b66 | 6726 | rect.height += GetRowHeight(i); |
f85afd4e | 6727 | |
efc49a21 VZ |
6728 | // if grid lines are enabled, then the area of the cell is a bit smaller |
6729 | if (m_gridLinesEnabled) | |
6730 | { | |
6731 | rect.width -= 1; | |
6732 | rect.height -= 1; | |
6733 | } | |
f6bcfd97 | 6734 | } |
4db6714b | 6735 | |
2d66e025 MB |
6736 | return rect; |
6737 | } | |
6738 | ||
ef316e23 | 6739 | bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) const |
2d66e025 MB |
6740 | { |
6741 | // get the cell rectangle in logical coords | |
6742 | // | |
6743 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 6744 | |
2d66e025 MB |
6745 | // convert to device coords |
6746 | // | |
6747 | int left, top, right, bottom; | |
6748 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
6749 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 6750 | |
2d66e025 | 6751 | // check against the client area of the grid window |
2d66e025 MB |
6752 | int cw, ch; |
6753 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 6754 | |
2d66e025 | 6755 | if ( wholeCellVisible ) |
f85afd4e | 6756 | { |
2d66e025 | 6757 | // is the cell wholly visible ? |
2f024384 DS |
6758 | return ( left >= 0 && right <= cw && |
6759 | top >= 0 && bottom <= ch ); | |
2d66e025 MB |
6760 | } |
6761 | else | |
6762 | { | |
6763 | // is the cell partly visible ? | |
6764 | // | |
2f024384 DS |
6765 | return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) && |
6766 | ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) ); | |
2d66e025 MB |
6767 | } |
6768 | } | |
6769 | ||
2d66e025 MB |
6770 | // make the specified cell location visible by doing a minimal amount |
6771 | // of scrolling | |
6772 | // | |
6773 | void wxGrid::MakeCellVisible( int row, int col ) | |
6774 | { | |
6775 | int i; | |
60ff3b99 | 6776 | int xpos = -1, ypos = -1; |
2d66e025 | 6777 | |
2f024384 DS |
6778 | if ( row >= 0 && row < m_numRows && |
6779 | col >= 0 && col < m_numCols ) | |
2d66e025 MB |
6780 | { |
6781 | // get the cell rectangle in logical coords | |
2d66e025 | 6782 | wxRect r( CellToRect( row, col ) ); |
60ff3b99 | 6783 | |
2d66e025 | 6784 | // convert to device coords |
2d66e025 MB |
6785 | int left, top, right, bottom; |
6786 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
6787 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 6788 | |
2d66e025 MB |
6789 | int cw, ch; |
6790 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 6791 | |
2d66e025 | 6792 | if ( top < 0 ) |
3f296516 | 6793 | { |
2d66e025 | 6794 | ypos = r.GetTop(); |
3f296516 | 6795 | } |
2d66e025 MB |
6796 | else if ( bottom > ch ) |
6797 | { | |
6798 | int h = r.GetHeight(); | |
6799 | ypos = r.GetTop(); | |
56b6cf26 | 6800 | for ( i = row - 1; i >= 0; i-- ) |
2d66e025 | 6801 | { |
7c1cb261 VZ |
6802 | int rowHeight = GetRowHeight(i); |
6803 | if ( h + rowHeight > ch ) | |
6804 | break; | |
2d66e025 | 6805 | |
7c1cb261 VZ |
6806 | h += rowHeight; |
6807 | ypos -= rowHeight; | |
2d66e025 | 6808 | } |
f0102d2a VZ |
6809 | |
6810 | // we divide it later by GRID_SCROLL_LINE, make sure that we don't | |
56b6cf26 DS |
6811 | // have rounding errors (this is important, because if we do, |
6812 | // we might not scroll at all and some cells won't be redrawn) | |
275c4ae4 | 6813 | // |
56b6cf26 DS |
6814 | // Sometimes GRID_SCROLL_LINE / 2 is not enough, |
6815 | // so just add a full scroll unit... | |
fd76c6a7 | 6816 | ypos += m_yScrollPixelsPerLine; |
2d66e025 MB |
6817 | } |
6818 | ||
7db713ae | 6819 | // special handling for wide cells - show always left part of the cell! |
faa94f3e | 6820 | // Otherwise, e.g. when stepping from row to row, it would jump between |
7db713ae JS |
6821 | // left and right part of the cell on every step! |
6822 | // if ( left < 0 ) | |
ccdee36f | 6823 | if ( left < 0 || (right - left) >= cw ) |
2d66e025 MB |
6824 | { |
6825 | xpos = r.GetLeft(); | |
6826 | } | |
6827 | else if ( right > cw ) | |
6828 | { | |
73145b0e JS |
6829 | // position the view so that the cell is on the right |
6830 | int x0, y0; | |
6831 | CalcUnscrolledPosition(0, 0, &x0, &y0); | |
6832 | xpos = x0 + (right - cw); | |
f0102d2a VZ |
6833 | |
6834 | // see comment for ypos above | |
fd76c6a7 | 6835 | xpos += m_xScrollPixelsPerLine; |
2d66e025 MB |
6836 | } |
6837 | ||
ccdee36f | 6838 | if ( xpos != -1 || ypos != -1 ) |
2d66e025 | 6839 | { |
97a9929e | 6840 | if ( xpos != -1 ) |
fd76c6a7 | 6841 | xpos /= m_xScrollPixelsPerLine; |
97a9929e | 6842 | if ( ypos != -1 ) |
fd76c6a7 | 6843 | ypos /= m_yScrollPixelsPerLine; |
2d66e025 MB |
6844 | Scroll( xpos, ypos ); |
6845 | AdjustScrollbars(); | |
6846 | } | |
6847 | } | |
6848 | } | |
6849 | ||
2d66e025 MB |
6850 | // |
6851 | // ------ Grid cursor movement functions | |
6852 | // | |
6853 | ||
10a4531d VZ |
6854 | bool |
6855 | wxGrid::DoMoveCursor(bool expandSelection, | |
6856 | const wxGridDirectionOperations& diroper) | |
2d66e025 | 6857 | { |
10a4531d VZ |
6858 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
6859 | return false; | |
6860 | ||
6861 | if ( expandSelection ) | |
2d66e025 | 6862 | { |
8a3e536c VZ |
6863 | wxGridCellCoords coords = m_selectedBlockCorner; |
6864 | if ( coords == wxGridNoCellCoords ) | |
6865 | coords = m_currentCellCoords; | |
4db6714b | 6866 | |
8a3e536c | 6867 | if ( diroper.IsAtBoundary(coords) ) |
10a4531d | 6868 | return false; |
2d66e025 | 6869 | |
8a3e536c | 6870 | diroper.Advance(coords); |
2d66e025 | 6871 | |
8a3e536c | 6872 | UpdateBlockBeingSelected(m_currentCellCoords, coords); |
10a4531d | 6873 | } |
8a3e536c | 6874 | else // don't expand selection |
f85afd4e | 6875 | { |
8a3e536c VZ |
6876 | ClearSelection(); |
6877 | ||
10a4531d | 6878 | if ( diroper.IsAtBoundary(m_currentCellCoords) ) |
ca65c044 | 6879 | return false; |
4db6714b | 6880 | |
10a4531d VZ |
6881 | wxGridCellCoords coords = m_currentCellCoords; |
6882 | diroper.Advance(coords); | |
8a3e536c VZ |
6883 | |
6884 | GoToCell(coords); | |
f85afd4e | 6885 | } |
2d66e025 | 6886 | |
10a4531d | 6887 | return true; |
f85afd4e MB |
6888 | } |
6889 | ||
10a4531d | 6890 | bool wxGrid::MoveCursorUp(bool expandSelection) |
f85afd4e | 6891 | { |
10a4531d VZ |
6892 | return DoMoveCursor(expandSelection, |
6893 | wxGridBackwardOperations(this, wxGridRowOperations())); | |
2d66e025 MB |
6894 | } |
6895 | ||
10a4531d | 6896 | bool wxGrid::MoveCursorDown(bool expandSelection) |
2d66e025 | 6897 | { |
10a4531d VZ |
6898 | return DoMoveCursor(expandSelection, |
6899 | wxGridForwardOperations(this, wxGridRowOperations())); | |
6900 | } | |
4db6714b | 6901 | |
10a4531d VZ |
6902 | bool wxGrid::MoveCursorLeft(bool expandSelection) |
6903 | { | |
6904 | return DoMoveCursor(expandSelection, | |
6905 | wxGridBackwardOperations(this, wxGridColumnOperations())); | |
6906 | } | |
8f177c8e | 6907 | |
10a4531d VZ |
6908 | bool wxGrid::MoveCursorRight(bool expandSelection) |
6909 | { | |
6910 | return DoMoveCursor(expandSelection, | |
6911 | wxGridForwardOperations(this, wxGridColumnOperations())); | |
2d66e025 MB |
6912 | } |
6913 | ||
10a4531d | 6914 | bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations& diroper) |
2d66e025 | 6915 | { |
4db6714b KH |
6916 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
6917 | return false; | |
60ff3b99 | 6918 | |
10a4531d VZ |
6919 | if ( diroper.IsAtBoundary(m_currentCellCoords) ) |
6920 | return false; | |
ef5df12b | 6921 | |
10a4531d VZ |
6922 | const int oldRow = m_currentCellCoords.GetRow(); |
6923 | int newRow = diroper.MoveByPixelDistance(oldRow, m_gridWin->GetClientSize().y); | |
6924 | if ( newRow == oldRow ) | |
6925 | { | |
6926 | wxGridCellCoords coords(m_currentCellCoords); | |
6927 | diroper.Advance(coords); | |
6928 | newRow = coords.GetRow(); | |
6929 | } | |
8f177c8e | 6930 | |
8a3e536c | 6931 | GoToCell(newRow, m_currentCellCoords.GetCol()); |
60ff3b99 | 6932 | |
10a4531d VZ |
6933 | return true; |
6934 | } | |
2d66e025 | 6935 | |
10a4531d VZ |
6936 | bool wxGrid::MovePageUp() |
6937 | { | |
6938 | return DoMoveCursorByPage( | |
6939 | wxGridBackwardOperations(this, wxGridRowOperations())); | |
2d66e025 MB |
6940 | } |
6941 | ||
6942 | bool wxGrid::MovePageDown() | |
6943 | { | |
10a4531d | 6944 | return DoMoveCursorByPage( |
d188378e | 6945 | wxGridForwardOperations(this, wxGridRowOperations())); |
10a4531d | 6946 | } |
60ff3b99 | 6947 | |
10a4531d VZ |
6948 | // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper |
6949 | // until we find a non-empty cell or reach the grid end | |
6950 | void | |
6951 | wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords& coords, | |
6952 | const wxGridDirectionOperations& diroper) | |
6953 | { | |
6954 | while ( !diroper.IsAtBoundary(coords) ) | |
f85afd4e | 6955 | { |
10a4531d VZ |
6956 | diroper.Advance(coords); |
6957 | if ( !m_table->IsEmpty(coords) ) | |
6958 | break; | |
f85afd4e MB |
6959 | } |
6960 | } | |
8f177c8e | 6961 | |
10a4531d VZ |
6962 | bool |
6963 | wxGrid::DoMoveCursorByBlock(bool expandSelection, | |
6964 | const wxGridDirectionOperations& diroper) | |
2d66e025 | 6965 | { |
10a4531d VZ |
6966 | if ( !m_table || m_currentCellCoords == wxGridNoCellCoords ) |
6967 | return false; | |
f85afd4e | 6968 | |
10a4531d VZ |
6969 | if ( diroper.IsAtBoundary(m_currentCellCoords) ) |
6970 | return false; | |
4db6714b | 6971 | |
10a4531d VZ |
6972 | wxGridCellCoords coords(m_currentCellCoords); |
6973 | if ( m_table->IsEmpty(coords) ) | |
6974 | { | |
6975 | // we are in an empty cell: find the next block of non-empty cells | |
6976 | AdvanceToNextNonEmpty(coords, diroper); | |
2d66e025 | 6977 | } |
10a4531d | 6978 | else // current cell is not empty |
f85afd4e | 6979 | { |
10a4531d VZ |
6980 | diroper.Advance(coords); |
6981 | if ( m_table->IsEmpty(coords) ) | |
2d66e025 | 6982 | { |
10a4531d VZ |
6983 | // we started at the end of a block, find the next one |
6984 | AdvanceToNextNonEmpty(coords, diroper); | |
2d66e025 | 6985 | } |
10a4531d | 6986 | else // we're in a middle of a block |
2d66e025 | 6987 | { |
10a4531d VZ |
6988 | // go to the end of it, i.e. find the last cell before the next |
6989 | // empty one | |
6990 | while ( !diroper.IsAtBoundary(coords) ) | |
2d66e025 | 6991 | { |
10a4531d VZ |
6992 | wxGridCellCoords coordsNext(coords); |
6993 | diroper.Advance(coordsNext); | |
6994 | if ( m_table->IsEmpty(coordsNext) ) | |
2d66e025 | 6995 | break; |
2d66e025 | 6996 | |
10a4531d VZ |
6997 | coords = coordsNext; |
6998 | } | |
aa5e1f75 | 6999 | } |
10a4531d | 7000 | } |
2d66e025 | 7001 | |
10a4531d VZ |
7002 | if ( expandSelection ) |
7003 | { | |
8a3e536c | 7004 | UpdateBlockBeingSelected(m_currentCellCoords, coords); |
10a4531d VZ |
7005 | } |
7006 | else | |
7007 | { | |
7008 | ClearSelection(); | |
8a3e536c | 7009 | GoToCell(coords); |
f85afd4e | 7010 | } |
f85afd4e | 7011 | |
10a4531d | 7012 | return true; |
2d66e025 | 7013 | } |
f85afd4e | 7014 | |
10a4531d | 7015 | bool wxGrid::MoveCursorUpBlock(bool expandSelection) |
f85afd4e | 7016 | { |
10a4531d VZ |
7017 | return DoMoveCursorByBlock( |
7018 | expandSelection, | |
7019 | wxGridBackwardOperations(this, wxGridRowOperations()) | |
7020 | ); | |
7021 | } | |
8f177c8e | 7022 | |
10a4531d VZ |
7023 | bool wxGrid::MoveCursorDownBlock( bool expandSelection ) |
7024 | { | |
7025 | return DoMoveCursorByBlock( | |
7026 | expandSelection, | |
7027 | wxGridForwardOperations(this, wxGridRowOperations()) | |
7028 | ); | |
7029 | } | |
2d66e025 | 7030 | |
10a4531d VZ |
7031 | bool wxGrid::MoveCursorLeftBlock( bool expandSelection ) |
7032 | { | |
7033 | return DoMoveCursorByBlock( | |
7034 | expandSelection, | |
7035 | wxGridBackwardOperations(this, wxGridColumnOperations()) | |
7036 | ); | |
f85afd4e MB |
7037 | } |
7038 | ||
5c8fc7c1 | 7039 | bool wxGrid::MoveCursorRightBlock( bool expandSelection ) |
f85afd4e | 7040 | { |
10a4531d VZ |
7041 | return DoMoveCursorByBlock( |
7042 | expandSelection, | |
7043 | wxGridForwardOperations(this, wxGridColumnOperations()) | |
7044 | ); | |
f85afd4e MB |
7045 | } |
7046 | ||
f85afd4e | 7047 | // |
2d66e025 | 7048 | // ------ Label values and formatting |
f85afd4e MB |
7049 | // |
7050 | ||
ef316e23 | 7051 | void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) const |
f85afd4e | 7052 | { |
2f024384 DS |
7053 | if ( horiz ) |
7054 | *horiz = m_rowLabelHorizAlign; | |
7055 | if ( vert ) | |
7056 | *vert = m_rowLabelVertAlign; | |
f85afd4e MB |
7057 | } |
7058 | ||
ef316e23 | 7059 | void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) const |
f85afd4e | 7060 | { |
2f024384 DS |
7061 | if ( horiz ) |
7062 | *horiz = m_colLabelHorizAlign; | |
7063 | if ( vert ) | |
7064 | *vert = m_colLabelVertAlign; | |
f85afd4e MB |
7065 | } |
7066 | ||
ef316e23 | 7067 | int wxGrid::GetColLabelTextOrientation() const |
d43851f7 JS |
7068 | { |
7069 | return m_colLabelTextOrientation; | |
7070 | } | |
7071 | ||
ef316e23 | 7072 | wxString wxGrid::GetRowLabelValue( int row ) const |
f85afd4e MB |
7073 | { |
7074 | if ( m_table ) | |
7075 | { | |
7076 | return m_table->GetRowLabelValue( row ); | |
7077 | } | |
7078 | else | |
7079 | { | |
7080 | wxString s; | |
7081 | s << row; | |
7082 | return s; | |
7083 | } | |
7084 | } | |
7085 | ||
ef316e23 | 7086 | wxString wxGrid::GetColLabelValue( int col ) const |
f85afd4e MB |
7087 | { |
7088 | if ( m_table ) | |
7089 | { | |
7090 | return m_table->GetColLabelValue( col ); | |
7091 | } | |
7092 | else | |
7093 | { | |
7094 | wxString s; | |
7095 | s << col; | |
7096 | return s; | |
7097 | } | |
7098 | } | |
7099 | ||
7100 | void wxGrid::SetRowLabelSize( int width ) | |
7101 | { | |
733f486a VZ |
7102 | wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE ); |
7103 | ||
7104 | if ( width == wxGRID_AUTOSIZE ) | |
7105 | { | |
7106 | width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW); | |
7107 | } | |
7108 | ||
2e8cd977 MB |
7109 | if ( width != m_rowLabelWidth ) |
7110 | { | |
2e8cd977 MB |
7111 | if ( width == 0 ) |
7112 | { | |
ca65c044 WS |
7113 | m_rowLabelWin->Show( false ); |
7114 | m_cornerLabelWin->Show( false ); | |
2e8cd977 | 7115 | } |
7807d81c | 7116 | else if ( m_rowLabelWidth == 0 ) |
2e8cd977 | 7117 | { |
ca65c044 | 7118 | m_rowLabelWin->Show( true ); |
2f024384 DS |
7119 | if ( m_colLabelHeight > 0 ) |
7120 | m_cornerLabelWin->Show( true ); | |
2e8cd977 | 7121 | } |
b99be8fb | 7122 | |
2e8cd977 | 7123 | m_rowLabelWidth = width; |
a6b40a9a | 7124 | InvalidateBestSize(); |
7807d81c | 7125 | CalcWindowSizes(); |
ca65c044 | 7126 | wxScrolledWindow::Refresh( true ); |
2e8cd977 | 7127 | } |
f85afd4e MB |
7128 | } |
7129 | ||
7130 | void wxGrid::SetColLabelSize( int height ) | |
7131 | { | |
733f486a VZ |
7132 | wxASSERT( height >=0 || height == wxGRID_AUTOSIZE ); |
7133 | ||
7134 | if ( height == wxGRID_AUTOSIZE ) | |
7135 | { | |
7136 | height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN); | |
7137 | } | |
7138 | ||
2e8cd977 MB |
7139 | if ( height != m_colLabelHeight ) |
7140 | { | |
2e8cd977 MB |
7141 | if ( height == 0 ) |
7142 | { | |
ad805b9e | 7143 | m_colWindow->Show( false ); |
ca65c044 | 7144 | m_cornerLabelWin->Show( false ); |
2e8cd977 | 7145 | } |
7807d81c | 7146 | else if ( m_colLabelHeight == 0 ) |
2e8cd977 | 7147 | { |
ad805b9e | 7148 | m_colWindow->Show( true ); |
a9339fe2 DS |
7149 | if ( m_rowLabelWidth > 0 ) |
7150 | m_cornerLabelWin->Show( true ); | |
2e8cd977 | 7151 | } |
7807d81c | 7152 | |
2e8cd977 | 7153 | m_colLabelHeight = height; |
a6b40a9a | 7154 | InvalidateBestSize(); |
7807d81c | 7155 | CalcWindowSizes(); |
ca65c044 | 7156 | wxScrolledWindow::Refresh( true ); |
2e8cd977 | 7157 | } |
f85afd4e MB |
7158 | } |
7159 | ||
7160 | void wxGrid::SetLabelBackgroundColour( const wxColour& colour ) | |
7161 | { | |
2d66e025 MB |
7162 | if ( m_labelBackgroundColour != colour ) |
7163 | { | |
7164 | m_labelBackgroundColour = colour; | |
7165 | m_rowLabelWin->SetBackgroundColour( colour ); | |
ad805b9e | 7166 | m_colWindow->SetBackgroundColour( colour ); |
2d66e025 MB |
7167 | m_cornerLabelWin->SetBackgroundColour( colour ); |
7168 | ||
7169 | if ( !GetBatchCount() ) | |
7170 | { | |
7171 | m_rowLabelWin->Refresh(); | |
ad805b9e | 7172 | m_colWindow->Refresh(); |
2d66e025 MB |
7173 | m_cornerLabelWin->Refresh(); |
7174 | } | |
7175 | } | |
f85afd4e MB |
7176 | } |
7177 | ||
7178 | void wxGrid::SetLabelTextColour( const wxColour& colour ) | |
7179 | { | |
2d66e025 MB |
7180 | if ( m_labelTextColour != colour ) |
7181 | { | |
7182 | m_labelTextColour = colour; | |
7183 | if ( !GetBatchCount() ) | |
7184 | { | |
7185 | m_rowLabelWin->Refresh(); | |
ad805b9e | 7186 | m_colWindow->Refresh(); |
2d66e025 MB |
7187 | } |
7188 | } | |
f85afd4e MB |
7189 | } |
7190 | ||
7191 | void wxGrid::SetLabelFont( const wxFont& font ) | |
7192 | { | |
7193 | m_labelFont = font; | |
2d66e025 MB |
7194 | if ( !GetBatchCount() ) |
7195 | { | |
7196 | m_rowLabelWin->Refresh(); | |
ad805b9e | 7197 | m_colWindow->Refresh(); |
2d66e025 | 7198 | } |
f85afd4e MB |
7199 | } |
7200 | ||
7201 | void wxGrid::SetRowLabelAlignment( int horiz, int vert ) | |
7202 | { | |
4c7277db MB |
7203 | // allow old (incorrect) defs to be used |
7204 | switch ( horiz ) | |
7205 | { | |
7206 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
7207 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
7208 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
7209 | } | |
84912ef8 | 7210 | |
4c7277db MB |
7211 | switch ( vert ) |
7212 | { | |
7213 | case wxTOP: vert = wxALIGN_TOP; break; | |
7214 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
7215 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
7216 | } | |
84912ef8 | 7217 | |
4c7277db | 7218 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) |
f85afd4e MB |
7219 | { |
7220 | m_rowLabelHorizAlign = horiz; | |
7221 | } | |
8f177c8e | 7222 | |
4c7277db | 7223 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) |
f85afd4e MB |
7224 | { |
7225 | m_rowLabelVertAlign = vert; | |
7226 | } | |
7227 | ||
2d66e025 MB |
7228 | if ( !GetBatchCount() ) |
7229 | { | |
7230 | m_rowLabelWin->Refresh(); | |
60ff3b99 | 7231 | } |
f85afd4e MB |
7232 | } |
7233 | ||
7234 | void wxGrid::SetColLabelAlignment( int horiz, int vert ) | |
7235 | { | |
4c7277db MB |
7236 | // allow old (incorrect) defs to be used |
7237 | switch ( horiz ) | |
7238 | { | |
7239 | case wxLEFT: horiz = wxALIGN_LEFT; break; | |
7240 | case wxRIGHT: horiz = wxALIGN_RIGHT; break; | |
7241 | case wxCENTRE: horiz = wxALIGN_CENTRE; break; | |
7242 | } | |
84912ef8 | 7243 | |
4c7277db MB |
7244 | switch ( vert ) |
7245 | { | |
7246 | case wxTOP: vert = wxALIGN_TOP; break; | |
7247 | case wxBOTTOM: vert = wxALIGN_BOTTOM; break; | |
7248 | case wxCENTRE: vert = wxALIGN_CENTRE; break; | |
7249 | } | |
84912ef8 | 7250 | |
4c7277db | 7251 | if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT ) |
f85afd4e MB |
7252 | { |
7253 | m_colLabelHorizAlign = horiz; | |
7254 | } | |
8f177c8e | 7255 | |
4c7277db | 7256 | if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM ) |
f85afd4e MB |
7257 | { |
7258 | m_colLabelVertAlign = vert; | |
7259 | } | |
7260 | ||
2d66e025 MB |
7261 | if ( !GetBatchCount() ) |
7262 | { | |
ad805b9e | 7263 | m_colWindow->Refresh(); |
60ff3b99 | 7264 | } |
f85afd4e MB |
7265 | } |
7266 | ||
ca65c044 WS |
7267 | // Note: under MSW, the default column label font must be changed because it |
7268 | // does not support vertical printing | |
d43851f7 JS |
7269 | // |
7270 | // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD); | |
ca65c044 WS |
7271 | // pGrid->SetLabelFont(font); |
7272 | // pGrid->SetColLabelTextOrientation(wxVERTICAL); | |
d43851f7 JS |
7273 | // |
7274 | void wxGrid::SetColLabelTextOrientation( int textOrientation ) | |
7275 | { | |
4db6714b | 7276 | if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL ) |
d43851f7 | 7277 | m_colLabelTextOrientation = textOrientation; |
d43851f7 JS |
7278 | |
7279 | if ( !GetBatchCount() ) | |
ad805b9e | 7280 | m_colWindow->Refresh(); |
d43851f7 JS |
7281 | } |
7282 | ||
f85afd4e MB |
7283 | void wxGrid::SetRowLabelValue( int row, const wxString& s ) |
7284 | { | |
7285 | if ( m_table ) | |
7286 | { | |
7287 | m_table->SetRowLabelValue( row, s ); | |
2d66e025 MB |
7288 | if ( !GetBatchCount() ) |
7289 | { | |
ccdee36f | 7290 | wxRect rect = CellToRect( row, 0 ); |
70c7a608 SN |
7291 | if ( rect.height > 0 ) |
7292 | { | |
cb309039 | 7293 | CalcScrolledPosition(0, rect.y, &rect.x, &rect.y); |
b1944ebc | 7294 | rect.x = 0; |
70c7a608 | 7295 | rect.width = m_rowLabelWidth; |
ca65c044 | 7296 | m_rowLabelWin->Refresh( true, &rect ); |
70c7a608 | 7297 | } |
2d66e025 | 7298 | } |
f85afd4e MB |
7299 | } |
7300 | } | |
7301 | ||
7302 | void wxGrid::SetColLabelValue( int col, const wxString& s ) | |
7303 | { | |
7304 | if ( m_table ) | |
7305 | { | |
7306 | m_table->SetColLabelValue( col, s ); | |
2d66e025 MB |
7307 | if ( !GetBatchCount() ) |
7308 | { | |
ad805b9e | 7309 | if ( m_useNativeHeader ) |
70c7a608 | 7310 | { |
3039ade9 | 7311 | GetGridColHeader()->UpdateColumn(col); |
ad805b9e VZ |
7312 | } |
7313 | else | |
7314 | { | |
7315 | wxRect rect = CellToRect( 0, col ); | |
7316 | if ( rect.width > 0 ) | |
7317 | { | |
7318 | CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y); | |
7319 | rect.y = 0; | |
7320 | rect.height = m_colLabelHeight; | |
7321 | GetColLabelWindow()->Refresh( true, &rect ); | |
7322 | } | |
70c7a608 | 7323 | } |
2d66e025 | 7324 | } |
f85afd4e MB |
7325 | } |
7326 | } | |
7327 | ||
7328 | void wxGrid::SetGridLineColour( const wxColour& colour ) | |
7329 | { | |
2d66e025 MB |
7330 | if ( m_gridLineColour != colour ) |
7331 | { | |
7332 | m_gridLineColour = colour; | |
60ff3b99 | 7333 | |
9f7aee01 VZ |
7334 | if ( GridLinesEnabled() ) |
7335 | RedrawGridLines(); | |
2d66e025 | 7336 | } |
f85afd4e MB |
7337 | } |
7338 | ||
f6bcfd97 BP |
7339 | void wxGrid::SetCellHighlightColour( const wxColour& colour ) |
7340 | { | |
7341 | if ( m_cellHighlightColour != colour ) | |
7342 | { | |
7343 | m_cellHighlightColour = colour; | |
7344 | ||
7345 | wxClientDC dc( m_gridWin ); | |
7346 | PrepareDC( dc ); | |
7347 | wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); | |
7348 | DrawCellHighlight(dc, attr); | |
7349 | attr->DecRef(); | |
7350 | } | |
7351 | } | |
7352 | ||
d2520c85 RD |
7353 | void wxGrid::SetCellHighlightPenWidth(int width) |
7354 | { | |
4db6714b KH |
7355 | if (m_cellHighlightPenWidth != width) |
7356 | { | |
d2520c85 RD |
7357 | m_cellHighlightPenWidth = width; |
7358 | ||
7359 | // Just redrawing the cell highlight is not enough since that won't | |
4c51a665 | 7360 | // make any visible change if the thickness is getting smaller. |
d2520c85 RD |
7361 | int row = m_currentCellCoords.GetRow(); |
7362 | int col = m_currentCellCoords.GetCol(); | |
1b3b96d8 | 7363 | if ( row == -1 || col == -1 || GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) |
d2520c85 | 7364 | return; |
2f024384 | 7365 | |
d2520c85 | 7366 | wxRect rect = CellToRect(row, col); |
ca65c044 | 7367 | m_gridWin->Refresh(true, &rect); |
d2520c85 RD |
7368 | } |
7369 | } | |
7370 | ||
7371 | void wxGrid::SetCellHighlightROPenWidth(int width) | |
7372 | { | |
4db6714b KH |
7373 | if (m_cellHighlightROPenWidth != width) |
7374 | { | |
d2520c85 RD |
7375 | m_cellHighlightROPenWidth = width; |
7376 | ||
7377 | // Just redrawing the cell highlight is not enough since that won't | |
4c51a665 | 7378 | // make any visible change if the thickness is getting smaller. |
d2520c85 RD |
7379 | int row = m_currentCellCoords.GetRow(); |
7380 | int col = m_currentCellCoords.GetCol(); | |
76c66f19 VZ |
7381 | if ( row == -1 || col == -1 || |
7382 | GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) | |
d2520c85 | 7383 | return; |
ccdee36f | 7384 | |
d2520c85 | 7385 | wxRect rect = CellToRect(row, col); |
ca65c044 | 7386 | m_gridWin->Refresh(true, &rect); |
d2520c85 RD |
7387 | } |
7388 | } | |
7389 | ||
9f7aee01 VZ |
7390 | void wxGrid::RedrawGridLines() |
7391 | { | |
7392 | // the lines will be redrawn when the window is thawn | |
7393 | if ( GetBatchCount() ) | |
7394 | return; | |
7395 | ||
7396 | if ( GridLinesEnabled() ) | |
7397 | { | |
7398 | wxClientDC dc( m_gridWin ); | |
7399 | PrepareDC( dc ); | |
7400 | DrawAllGridLines( dc, wxRegion() ); | |
7401 | } | |
7402 | else // remove the grid lines | |
7403 | { | |
7404 | m_gridWin->Refresh(); | |
7405 | } | |
7406 | } | |
7407 | ||
f85afd4e MB |
7408 | void wxGrid::EnableGridLines( bool enable ) |
7409 | { | |
7410 | if ( enable != m_gridLinesEnabled ) | |
7411 | { | |
7412 | m_gridLinesEnabled = enable; | |
2d66e025 | 7413 | |
9f7aee01 VZ |
7414 | RedrawGridLines(); |
7415 | } | |
7416 | } | |
7417 | ||
7418 | void wxGrid::DoClipGridLines(bool& var, bool clip) | |
7419 | { | |
7420 | if ( clip != var ) | |
7421 | { | |
7422 | var = clip; | |
7423 | ||
7424 | if ( GridLinesEnabled() ) | |
7425 | RedrawGridLines(); | |
f85afd4e MB |
7426 | } |
7427 | } | |
7428 | ||
ef316e23 | 7429 | int wxGrid::GetDefaultRowSize() const |
f85afd4e MB |
7430 | { |
7431 | return m_defaultRowHeight; | |
7432 | } | |
7433 | ||
ef316e23 | 7434 | int wxGrid::GetRowSize( int row ) const |
f85afd4e | 7435 | { |
9a83f860 | 7436 | wxCHECK_MSG( row >= 0 && row < m_numRows, 0, wxT("invalid row index") ); |
b99be8fb | 7437 | |
7c1cb261 | 7438 | return GetRowHeight(row); |
f85afd4e MB |
7439 | } |
7440 | ||
ef316e23 | 7441 | int wxGrid::GetDefaultColSize() const |
f85afd4e MB |
7442 | { |
7443 | return m_defaultColWidth; | |
7444 | } | |
7445 | ||
ef316e23 | 7446 | int wxGrid::GetColSize( int col ) const |
f85afd4e | 7447 | { |
9a83f860 | 7448 | wxCHECK_MSG( col >= 0 && col < m_numCols, 0, wxT("invalid column index") ); |
b99be8fb | 7449 | |
7c1cb261 | 7450 | return GetColWidth(col); |
f85afd4e MB |
7451 | } |
7452 | ||
2e9a6788 VZ |
7453 | // ============================================================================ |
7454 | // access to the grid attributes: each of them has a default value in the grid | |
7455 | // itself and may be overidden on a per-cell basis | |
7456 | // ============================================================================ | |
7457 | ||
7458 | // ---------------------------------------------------------------------------- | |
7459 | // setting default attributes | |
7460 | // ---------------------------------------------------------------------------- | |
7461 | ||
7462 | void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col ) | |
7463 | { | |
2796cce3 | 7464 | m_defaultCellAttr->SetBackgroundColour(col); |
c916e13b RR |
7465 | #ifdef __WXGTK__ |
7466 | m_gridWin->SetBackgroundColour(col); | |
7467 | #endif | |
2e9a6788 VZ |
7468 | } |
7469 | ||
7470 | void wxGrid::SetDefaultCellTextColour( const wxColour& col ) | |
7471 | { | |
2796cce3 | 7472 | m_defaultCellAttr->SetTextColour(col); |
2e9a6788 VZ |
7473 | } |
7474 | ||
7475 | void wxGrid::SetDefaultCellAlignment( int horiz, int vert ) | |
7476 | { | |
2796cce3 | 7477 | m_defaultCellAttr->SetAlignment(horiz, vert); |
2e9a6788 VZ |
7478 | } |
7479 | ||
27f35b66 SN |
7480 | void wxGrid::SetDefaultCellOverflow( bool allow ) |
7481 | { | |
7482 | m_defaultCellAttr->SetOverflow(allow); | |
7483 | } | |
7484 | ||
2e9a6788 VZ |
7485 | void wxGrid::SetDefaultCellFont( const wxFont& font ) |
7486 | { | |
2796cce3 RD |
7487 | m_defaultCellAttr->SetFont(font); |
7488 | } | |
7489 | ||
ca63e8e9 RD |
7490 | // For editors and renderers the type registry takes precedence over the |
7491 | // default attr, so we need to register the new editor/renderer for the string | |
7492 | // data type in order to make setting a default editor/renderer appear to | |
7493 | // work correctly. | |
7494 | ||
0ba143c9 RD |
7495 | void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer) |
7496 | { | |
ca63e8e9 RD |
7497 | RegisterDataType(wxGRID_VALUE_STRING, |
7498 | renderer, | |
7499 | GetDefaultEditorForType(wxGRID_VALUE_STRING)); | |
0ba143c9 | 7500 | } |
2e9a6788 | 7501 | |
0ba143c9 RD |
7502 | void wxGrid::SetDefaultEditor(wxGridCellEditor *editor) |
7503 | { | |
ca63e8e9 RD |
7504 | RegisterDataType(wxGRID_VALUE_STRING, |
7505 | GetDefaultRendererForType(wxGRID_VALUE_STRING), | |
42841dfc | 7506 | editor); |
0ba143c9 | 7507 | } |
9b4aede2 | 7508 | |
2e9a6788 | 7509 | // ---------------------------------------------------------------------------- |
ef316e23 | 7510 | // access to the default attributes |
2e9a6788 VZ |
7511 | // ---------------------------------------------------------------------------- |
7512 | ||
ef316e23 | 7513 | wxColour wxGrid::GetDefaultCellBackgroundColour() const |
f85afd4e | 7514 | { |
2796cce3 | 7515 | return m_defaultCellAttr->GetBackgroundColour(); |
f85afd4e MB |
7516 | } |
7517 | ||
ef316e23 | 7518 | wxColour wxGrid::GetDefaultCellTextColour() const |
2e9a6788 | 7519 | { |
2796cce3 | 7520 | return m_defaultCellAttr->GetTextColour(); |
2e9a6788 VZ |
7521 | } |
7522 | ||
ef316e23 | 7523 | wxFont wxGrid::GetDefaultCellFont() const |
2e9a6788 | 7524 | { |
2796cce3 | 7525 | return m_defaultCellAttr->GetFont(); |
2e9a6788 VZ |
7526 | } |
7527 | ||
ef316e23 | 7528 | void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const |
2e9a6788 | 7529 | { |
2796cce3 | 7530 | m_defaultCellAttr->GetAlignment(horiz, vert); |
2e9a6788 VZ |
7531 | } |
7532 | ||
ef316e23 | 7533 | bool wxGrid::GetDefaultCellOverflow() const |
27f35b66 SN |
7534 | { |
7535 | return m_defaultCellAttr->GetOverflow(); | |
7536 | } | |
7537 | ||
0ba143c9 RD |
7538 | wxGridCellRenderer *wxGrid::GetDefaultRenderer() const |
7539 | { | |
0b190b0f | 7540 | return m_defaultCellAttr->GetRenderer(NULL, 0, 0); |
0ba143c9 | 7541 | } |
ab79958a | 7542 | |
0ba143c9 RD |
7543 | wxGridCellEditor *wxGrid::GetDefaultEditor() const |
7544 | { | |
4db6714b | 7545 | return m_defaultCellAttr->GetEditor(NULL, 0, 0); |
0ba143c9 | 7546 | } |
9b4aede2 | 7547 | |
2e9a6788 VZ |
7548 | // ---------------------------------------------------------------------------- |
7549 | // access to cell attributes | |
7550 | // ---------------------------------------------------------------------------- | |
7551 | ||
ef316e23 | 7552 | wxColour wxGrid::GetCellBackgroundColour(int row, int col) const |
f85afd4e | 7553 | { |
0a976765 | 7554 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7555 | wxColour colour = attr->GetBackgroundColour(); |
39bcce60 | 7556 | attr->DecRef(); |
2f024384 | 7557 | |
b99be8fb | 7558 | return colour; |
f85afd4e MB |
7559 | } |
7560 | ||
ef316e23 | 7561 | wxColour wxGrid::GetCellTextColour( int row, int col ) const |
f85afd4e | 7562 | { |
0a976765 | 7563 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7564 | wxColour colour = attr->GetTextColour(); |
39bcce60 | 7565 | attr->DecRef(); |
2f024384 | 7566 | |
b99be8fb | 7567 | return colour; |
f85afd4e MB |
7568 | } |
7569 | ||
ef316e23 | 7570 | wxFont wxGrid::GetCellFont( int row, int col ) const |
f85afd4e | 7571 | { |
0a976765 | 7572 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7573 | wxFont font = attr->GetFont(); |
39bcce60 | 7574 | attr->DecRef(); |
2f024384 | 7575 | |
b99be8fb | 7576 | return font; |
f85afd4e MB |
7577 | } |
7578 | ||
ef316e23 | 7579 | void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const |
f85afd4e | 7580 | { |
0a976765 | 7581 | wxGridCellAttr *attr = GetCellAttr(row, col); |
2796cce3 | 7582 | attr->GetAlignment(horiz, vert); |
39bcce60 | 7583 | attr->DecRef(); |
2e9a6788 VZ |
7584 | } |
7585 | ||
ef316e23 | 7586 | bool wxGrid::GetCellOverflow( int row, int col ) const |
27f35b66 SN |
7587 | { |
7588 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
7589 | bool allow = attr->GetOverflow(); | |
7590 | attr->DecRef(); | |
4db6714b | 7591 | |
27f35b66 SN |
7592 | return allow; |
7593 | } | |
7594 | ||
ea99e8e3 VZ |
7595 | wxGrid::CellSpan |
7596 | wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const | |
27f35b66 SN |
7597 | { |
7598 | wxGridCellAttr *attr = GetCellAttr(row, col); | |
7599 | attr->GetSize( num_rows, num_cols ); | |
7600 | attr->DecRef(); | |
ea99e8e3 VZ |
7601 | |
7602 | if ( *num_rows == 1 && *num_cols == 1 ) | |
7603 | return CellSpan_None; // just a normal cell | |
7604 | ||
7605 | if ( *num_rows < 0 || *num_cols < 0 ) | |
7606 | return CellSpan_Inside; // covered by a multi-span cell | |
7607 | ||
7608 | // this cell spans multiple cells to its right/bottom | |
7609 | return CellSpan_Main; | |
27f35b66 SN |
7610 | } |
7611 | ||
ef316e23 | 7612 | wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const |
2796cce3 RD |
7613 | { |
7614 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 7615 | wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); |
2796cce3 | 7616 | attr->DecRef(); |
0b190b0f | 7617 | |
2796cce3 RD |
7618 | return renderer; |
7619 | } | |
7620 | ||
ef316e23 | 7621 | wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const |
9b4aede2 RD |
7622 | { |
7623 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
28a77bc4 | 7624 | wxGridCellEditor* editor = attr->GetEditor(this, row, col); |
9b4aede2 | 7625 | attr->DecRef(); |
0b190b0f | 7626 | |
9b4aede2 RD |
7627 | return editor; |
7628 | } | |
7629 | ||
283b7808 VZ |
7630 | bool wxGrid::IsReadOnly(int row, int col) const |
7631 | { | |
7632 | wxGridCellAttr* attr = GetCellAttr(row, col); | |
7633 | bool isReadOnly = attr->IsReadOnly(); | |
7634 | attr->DecRef(); | |
4db6714b | 7635 | |
283b7808 VZ |
7636 | return isReadOnly; |
7637 | } | |
7638 | ||
2e9a6788 | 7639 | // ---------------------------------------------------------------------------- |
758cbedf | 7640 | // attribute support: cache, automatic provider creation, ... |
2e9a6788 VZ |
7641 | // ---------------------------------------------------------------------------- |
7642 | ||
ef316e23 | 7643 | bool wxGrid::CanHaveAttributes() const |
2e9a6788 VZ |
7644 | { |
7645 | if ( !m_table ) | |
7646 | { | |
ca65c044 | 7647 | return false; |
2e9a6788 VZ |
7648 | } |
7649 | ||
f2d76237 | 7650 | return m_table->CanHaveAttributes(); |
2e9a6788 VZ |
7651 | } |
7652 | ||
0a976765 VZ |
7653 | void wxGrid::ClearAttrCache() |
7654 | { | |
7655 | if ( m_attrCache.row != -1 ) | |
7656 | { | |
54181a33 | 7657 | wxGridCellAttr *oldAttr = m_attrCache.attr; |
19d7140e | 7658 | m_attrCache.attr = NULL; |
0a976765 | 7659 | m_attrCache.row = -1; |
1506cc66 SN |
7660 | // wxSafeDecRec(...) might cause event processing that accesses |
7661 | // the cached attribute, if one exists (e.g. by deleting the | |
7662 | // editor stored within the attribute). Therefore it is important | |
ace8d849 | 7663 | // to invalidate the cache before calling wxSafeDecRef! |
1506cc66 | 7664 | wxSafeDecRef(oldAttr); |
0a976765 VZ |
7665 | } |
7666 | } | |
7667 | ||
b99450ee VZ |
7668 | void wxGrid::RefreshAttr(int row, int col) |
7669 | { | |
7670 | if ( m_attrCache.row == row && m_attrCache.col == col ) | |
7671 | ClearAttrCache(); | |
7672 | } | |
7673 | ||
7674 | ||
0a976765 VZ |
7675 | void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const |
7676 | { | |
2b5f62a0 VZ |
7677 | if ( attr != NULL ) |
7678 | { | |
f48a1159 | 7679 | wxGrid * const self = const_cast<wxGrid *>(this); |
0a976765 | 7680 | |
2b5f62a0 VZ |
7681 | self->ClearAttrCache(); |
7682 | self->m_attrCache.row = row; | |
7683 | self->m_attrCache.col = col; | |
7684 | self->m_attrCache.attr = attr; | |
7685 | wxSafeIncRef(attr); | |
7686 | } | |
0a976765 VZ |
7687 | } |
7688 | ||
7689 | bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const | |
7690 | { | |
7691 | if ( row == m_attrCache.row && col == m_attrCache.col ) | |
7692 | { | |
7693 | *attr = m_attrCache.attr; | |
39bcce60 | 7694 | wxSafeIncRef(m_attrCache.attr); |
0a976765 VZ |
7695 | |
7696 | #ifdef DEBUG_ATTR_CACHE | |
7697 | gs_nAttrCacheHits++; | |
7698 | #endif | |
7699 | ||
ca65c044 | 7700 | return true; |
0a976765 VZ |
7701 | } |
7702 | else | |
7703 | { | |
7704 | #ifdef DEBUG_ATTR_CACHE | |
7705 | gs_nAttrCacheMisses++; | |
7706 | #endif | |
4db6714b | 7707 | |
ca65c044 | 7708 | return false; |
0a976765 VZ |
7709 | } |
7710 | } | |
7711 | ||
2e9a6788 VZ |
7712 | wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const |
7713 | { | |
a373d23b SN |
7714 | wxGridCellAttr *attr = NULL; |
7715 | // Additional test to avoid looking at the cache e.g. for | |
7716 | // wxNoCellCoords, as this will confuse memory management. | |
7717 | if ( row >= 0 ) | |
7718 | { | |
3ed884a0 SN |
7719 | if ( !LookupAttr(row, col, &attr) ) |
7720 | { | |
c2f5b920 | 7721 | attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any) |
10a4531d | 7722 | : NULL; |
3ed884a0 SN |
7723 | CacheAttr(row, col, attr); |
7724 | } | |
0a976765 | 7725 | } |
4db6714b | 7726 | |
508011ce VZ |
7727 | if (attr) |
7728 | { | |
2796cce3 | 7729 | attr->SetDefAttr(m_defaultCellAttr); |
508011ce VZ |
7730 | } |
7731 | else | |
7732 | { | |
2796cce3 RD |
7733 | attr = m_defaultCellAttr; |
7734 | attr->IncRef(); | |
7735 | } | |
2e9a6788 | 7736 | |
0a976765 VZ |
7737 | return attr; |
7738 | } | |
7739 | ||
7740 | wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const | |
7741 | { | |
10a4531d | 7742 | wxGridCellAttr *attr = NULL; |
71e60f70 | 7743 | bool canHave = ((wxGrid*)this)->CanHaveAttributes(); |
0a976765 | 7744 | |
9a83f860 VZ |
7745 | wxCHECK_MSG( canHave, attr, wxT("Cell attributes not allowed")); |
7746 | wxCHECK_MSG( m_table, attr, wxT("must have a table") ); | |
1df4050d VZ |
7747 | |
7748 | attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell); | |
7749 | if ( !attr ) | |
7750 | { | |
7751 | attr = new wxGridCellAttr(m_defaultCellAttr); | |
7752 | ||
7753 | // artificially inc the ref count to match DecRef() in caller | |
7754 | attr->IncRef(); | |
7755 | m_table->SetAttr(attr, row, col); | |
7756 | } | |
0a976765 | 7757 | |
2e9a6788 VZ |
7758 | return attr; |
7759 | } | |
7760 | ||
0b190b0f VZ |
7761 | // ---------------------------------------------------------------------------- |
7762 | // setting column attributes (wrappers around SetColAttr) | |
7763 | // ---------------------------------------------------------------------------- | |
7764 | ||
7765 | void wxGrid::SetColFormatBool(int col) | |
7766 | { | |
7767 | SetColFormatCustom(col, wxGRID_VALUE_BOOL); | |
7768 | } | |
7769 | ||
7770 | void wxGrid::SetColFormatNumber(int col) | |
7771 | { | |
7772 | SetColFormatCustom(col, wxGRID_VALUE_NUMBER); | |
7773 | } | |
7774 | ||
7775 | void wxGrid::SetColFormatFloat(int col, int width, int precision) | |
7776 | { | |
7777 | wxString typeName = wxGRID_VALUE_FLOAT; | |
7778 | if ( (width != -1) || (precision != -1) ) | |
7779 | { | |
9a83f860 | 7780 | typeName << wxT(':') << width << wxT(',') << precision; |
0b190b0f VZ |
7781 | } |
7782 | ||
7783 | SetColFormatCustom(col, typeName); | |
7784 | } | |
7785 | ||
7786 | void wxGrid::SetColFormatCustom(int col, const wxString& typeName) | |
7787 | { | |
999836aa | 7788 | wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col ); |
4db6714b | 7789 | if (!attr) |
19d7140e | 7790 | attr = new wxGridCellAttr; |
0b190b0f VZ |
7791 | wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName); |
7792 | attr->SetRenderer(renderer); | |
54181a33 VZ |
7793 | wxGridCellEditor *editor = GetDefaultEditorForType(typeName); |
7794 | attr->SetEditor(editor); | |
0b190b0f VZ |
7795 | |
7796 | SetColAttr(col, attr); | |
19d7140e | 7797 | |
0b190b0f VZ |
7798 | } |
7799 | ||
758cbedf VZ |
7800 | // ---------------------------------------------------------------------------- |
7801 | // setting cell attributes: this is forwarded to the table | |
7802 | // ---------------------------------------------------------------------------- | |
7803 | ||
27f35b66 SN |
7804 | void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr) |
7805 | { | |
7806 | if ( CanHaveAttributes() ) | |
7807 | { | |
7808 | m_table->SetAttr(attr, row, col); | |
7809 | ClearAttrCache(); | |
7810 | } | |
7811 | else | |
7812 | { | |
7813 | wxSafeDecRef(attr); | |
7814 | } | |
7815 | } | |
7816 | ||
758cbedf VZ |
7817 | void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr) |
7818 | { | |
7819 | if ( CanHaveAttributes() ) | |
7820 | { | |
7821 | m_table->SetRowAttr(attr, row); | |
19d7140e | 7822 | ClearAttrCache(); |
758cbedf VZ |
7823 | } |
7824 | else | |
7825 | { | |
39bcce60 | 7826 | wxSafeDecRef(attr); |
758cbedf VZ |
7827 | } |
7828 | } | |
7829 | ||
7830 | void wxGrid::SetColAttr(int col, wxGridCellAttr *attr) | |
7831 | { | |
7832 | if ( CanHaveAttributes() ) | |
7833 | { | |
7834 | m_table->SetColAttr(attr, col); | |
19d7140e | 7835 | ClearAttrCache(); |
758cbedf VZ |
7836 | } |
7837 | else | |
7838 | { | |
39bcce60 | 7839 | wxSafeDecRef(attr); |
758cbedf VZ |
7840 | } |
7841 | } | |
7842 | ||
2e9a6788 VZ |
7843 | void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour ) |
7844 | { | |
7845 | if ( CanHaveAttributes() ) | |
7846 | { | |
0a976765 | 7847 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7848 | attr->SetBackgroundColour(colour); |
7849 | attr->DecRef(); | |
7850 | } | |
7851 | } | |
7852 | ||
7853 | void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour ) | |
7854 | { | |
7855 | if ( CanHaveAttributes() ) | |
7856 | { | |
0a976765 | 7857 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7858 | attr->SetTextColour(colour); |
7859 | attr->DecRef(); | |
7860 | } | |
7861 | } | |
7862 | ||
7863 | void wxGrid::SetCellFont( int row, int col, const wxFont& font ) | |
7864 | { | |
7865 | if ( CanHaveAttributes() ) | |
7866 | { | |
0a976765 | 7867 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7868 | attr->SetFont(font); |
7869 | attr->DecRef(); | |
7870 | } | |
7871 | } | |
7872 | ||
7873 | void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert ) | |
7874 | { | |
7875 | if ( CanHaveAttributes() ) | |
7876 | { | |
0a976765 | 7877 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
2e9a6788 VZ |
7878 | attr->SetAlignment(horiz, vert); |
7879 | attr->DecRef(); | |
ab79958a VZ |
7880 | } |
7881 | } | |
7882 | ||
27f35b66 SN |
7883 | void wxGrid::SetCellOverflow( int row, int col, bool allow ) |
7884 | { | |
7885 | if ( CanHaveAttributes() ) | |
7886 | { | |
7887 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7888 | attr->SetOverflow(allow); | |
7889 | attr->DecRef(); | |
7890 | } | |
7891 | } | |
7892 | ||
7893 | void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols ) | |
7894 | { | |
7895 | if ( CanHaveAttributes() ) | |
7896 | { | |
7897 | int cell_rows, cell_cols; | |
7898 | ||
7899 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7900 | attr->GetSize(&cell_rows, &cell_cols); | |
7901 | attr->SetSize(num_rows, num_cols); | |
7902 | attr->DecRef(); | |
7903 | ||
7904 | // Cannot set the size of a cell to 0 or negative values | |
7905 | // While it is perfectly legal to do that, this function cannot | |
7906 | // handle all the possibilies, do it by hand by getting the CellAttr. | |
7907 | // You can only set the size of a cell to 1,1 or greater with this fn | |
7908 | wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)), | |
7909 | wxT("wxGrid::SetCellSize setting cell size that is already part of another cell")); | |
7910 | wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)), | |
7911 | wxT("wxGrid::SetCellSize setting cell size to < 1")); | |
7912 | ||
7913 | // if this was already a multicell then "turn off" the other cells first | |
a6b2078d | 7914 | if ((cell_rows > 1) || (cell_cols > 1)) |
27f35b66 SN |
7915 | { |
7916 | int i, j; | |
2f024384 | 7917 | for (j=row; j < row + cell_rows; j++) |
27f35b66 | 7918 | { |
2f024384 | 7919 | for (i=col; i < col + cell_cols; i++) |
27f35b66 SN |
7920 | { |
7921 | if ((i != col) || (j != row)) | |
7922 | { | |
7923 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
7924 | attr_stub->SetSize( 1, 1 ); | |
7925 | attr_stub->DecRef(); | |
7926 | } | |
7927 | } | |
7928 | } | |
7929 | } | |
7930 | ||
7931 | // mark the cells that will be covered by this cell to | |
7932 | // negative or zero values to point back at this cell | |
7933 | if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1)) | |
7934 | { | |
7935 | int i, j; | |
2f024384 | 7936 | for (j=row; j < row + num_rows; j++) |
27f35b66 | 7937 | { |
2f024384 | 7938 | for (i=col; i < col + num_cols; i++) |
27f35b66 SN |
7939 | { |
7940 | if ((i != col) || (j != row)) | |
7941 | { | |
7942 | wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i); | |
2f024384 | 7943 | attr_stub->SetSize( row - j, col - i ); |
27f35b66 SN |
7944 | attr_stub->DecRef(); |
7945 | } | |
7946 | } | |
7947 | } | |
7948 | } | |
7949 | } | |
7950 | } | |
7951 | ||
ab79958a VZ |
7952 | void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer) |
7953 | { | |
7954 | if ( CanHaveAttributes() ) | |
7955 | { | |
0a976765 | 7956 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); |
ab79958a VZ |
7957 | attr->SetRenderer(renderer); |
7958 | attr->DecRef(); | |
9b4aede2 RD |
7959 | } |
7960 | } | |
7961 | ||
7962 | void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor) | |
7963 | { | |
7964 | if ( CanHaveAttributes() ) | |
7965 | { | |
7966 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7967 | attr->SetEditor(editor); | |
7968 | attr->DecRef(); | |
283b7808 VZ |
7969 | } |
7970 | } | |
7971 | ||
7972 | void wxGrid::SetReadOnly(int row, int col, bool isReadOnly) | |
7973 | { | |
7974 | if ( CanHaveAttributes() ) | |
7975 | { | |
7976 | wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); | |
7977 | attr->SetReadOnly(isReadOnly); | |
7978 | attr->DecRef(); | |
2e9a6788 | 7979 | } |
f85afd4e MB |
7980 | } |
7981 | ||
f2d76237 RD |
7982 | // ---------------------------------------------------------------------------- |
7983 | // Data type registration | |
7984 | // ---------------------------------------------------------------------------- | |
7985 | ||
7986 | void wxGrid::RegisterDataType(const wxString& typeName, | |
7987 | wxGridCellRenderer* renderer, | |
7988 | wxGridCellEditor* editor) | |
7989 | { | |
7990 | m_typeRegistry->RegisterDataType(typeName, renderer, editor); | |
7991 | } | |
7992 | ||
7993 | ||
a9339fe2 | 7994 | wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const |
f2d76237 RD |
7995 | { |
7996 | wxString typeName = m_table->GetTypeName(row, col); | |
7997 | return GetDefaultEditorForType(typeName); | |
7998 | } | |
7999 | ||
a9339fe2 | 8000 | wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const |
f2d76237 RD |
8001 | { |
8002 | wxString typeName = m_table->GetTypeName(row, col); | |
8003 | return GetDefaultRendererForType(typeName); | |
8004 | } | |
8005 | ||
a9339fe2 | 8006 | wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const |
f2d76237 | 8007 | { |
c4608a8a | 8008 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
8009 | if ( index == wxNOT_FOUND ) |
8010 | { | |
767e0835 | 8011 | wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str())); |
0b190b0f | 8012 | |
f2d76237 RD |
8013 | return NULL; |
8014 | } | |
0b190b0f | 8015 | |
f2d76237 RD |
8016 | return m_typeRegistry->GetEditor(index); |
8017 | } | |
8018 | ||
a9339fe2 | 8019 | wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const |
f2d76237 | 8020 | { |
c4608a8a | 8021 | int index = m_typeRegistry->FindOrCloneDataType(typeName); |
0b190b0f VZ |
8022 | if ( index == wxNOT_FOUND ) |
8023 | { | |
767e0835 | 8024 | wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str())); |
0b190b0f | 8025 | |
c4608a8a | 8026 | return NULL; |
e72b4213 | 8027 | } |
0b190b0f | 8028 | |
c4608a8a | 8029 | return m_typeRegistry->GetRenderer(index); |
f2d76237 RD |
8030 | } |
8031 | ||
2e9a6788 VZ |
8032 | // ---------------------------------------------------------------------------- |
8033 | // row/col size | |
8034 | // ---------------------------------------------------------------------------- | |
8035 | ||
82edfbe7 VZ |
8036 | void wxGrid::DoDisableLineResize(int line, wxGridFixedIndicesSet *& setFixed) |
8037 | { | |
8038 | if ( !setFixed ) | |
8039 | { | |
8040 | setFixed = new wxGridFixedIndicesSet; | |
8041 | } | |
8042 | ||
8043 | setFixed->insert(line); | |
8044 | } | |
8045 | ||
8046 | bool | |
8047 | wxGrid::DoCanResizeLine(int line, const wxGridFixedIndicesSet *setFixed) const | |
8048 | { | |
8049 | return !setFixed || !setFixed->count(line); | |
8050 | } | |
8051 | ||
6e8524b1 MB |
8052 | void wxGrid::EnableDragRowSize( bool enable ) |
8053 | { | |
8054 | m_canDragRowSize = enable; | |
8055 | } | |
8056 | ||
6e8524b1 MB |
8057 | void wxGrid::EnableDragColSize( bool enable ) |
8058 | { | |
8059 | m_canDragColSize = enable; | |
8060 | } | |
8061 | ||
4cfa5de6 RD |
8062 | void wxGrid::EnableDragGridSize( bool enable ) |
8063 | { | |
8064 | m_canDragGridSize = enable; | |
8065 | } | |
8066 | ||
79dbea21 RD |
8067 | void wxGrid::EnableDragCell( bool enable ) |
8068 | { | |
8069 | m_canDragCell = enable; | |
8070 | } | |
6e8524b1 | 8071 | |
f85afd4e MB |
8072 | void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) |
8073 | { | |
b8d24d4e | 8074 | m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight ); |
f85afd4e MB |
8075 | |
8076 | if ( resizeExistingRows ) | |
8077 | { | |
b1da8107 SN |
8078 | // since we are resizing all rows to the default row size, |
8079 | // we can simply clear the row heights and row bottoms | |
8080 | // arrays (which also allows us to take advantage of | |
8081 | // some speed optimisations) | |
8082 | m_rowHeights.Empty(); | |
8083 | m_rowBottoms.Empty(); | |
edb89f7e VZ |
8084 | if ( !GetBatchCount() ) |
8085 | CalcDimensions(); | |
f85afd4e MB |
8086 | } |
8087 | } | |
8088 | ||
30496905 VZ |
8089 | namespace |
8090 | { | |
8091 | ||
8092 | // This is a common part of SetRowSize() and SetColSize() which takes care of | |
8093 | // updating the height/width of a row/column depending on its current value and | |
8094 | // the new one. | |
8095 | // | |
8096 | // Returns the difference between the new and the old size. | |
8097 | int UpdateRowOrColSize(int& sizeCurrent, int sizeNew) | |
8098 | { | |
8099 | // On input here sizeCurrent can be negative if it's currently hidden (the | |
8100 | // real size is its absolute value then). And sizeNew can be 0 to indicate | |
8101 | // that the row/column should be hidden or -1 to indicate that it should be | |
8102 | // shown again. | |
8103 | ||
8104 | if ( sizeNew < 0 ) | |
8105 | { | |
8106 | // We're showing back a previously hidden row/column. | |
8107 | wxASSERT_MSG( sizeNew == -1, wxS("New size must be positive or -1.") ); | |
8108 | ||
2328f468 VZ |
8109 | // If it's already visible, simply do nothing. |
8110 | if ( sizeCurrent >= 0 ) | |
8111 | return 0; | |
30496905 | 8112 | |
2328f468 | 8113 | // Otherwise show it by restoring its old size. |
30496905 VZ |
8114 | sizeCurrent = -sizeCurrent; |
8115 | ||
8116 | // This is positive which is correct. | |
8117 | return sizeCurrent; | |
8118 | } | |
8119 | else if ( sizeNew == 0 ) | |
8120 | { | |
8121 | // We're hiding a row/column. | |
30496905 | 8122 | |
2328f468 VZ |
8123 | // If it's already hidden, simply do nothing. |
8124 | if ( sizeCurrent <= 0 ) | |
8125 | return 0; | |
8126 | ||
8127 | // Otherwise hide it and also remember the shown size to be able to | |
8128 | // restore it later. | |
30496905 VZ |
8129 | sizeCurrent = -sizeCurrent; |
8130 | ||
8131 | // This is negative which is correct. | |
8132 | return sizeCurrent; | |
8133 | } | |
8134 | else // We're just changing the row/column size. | |
8135 | { | |
8136 | // Here it could have been hidden or not previously. | |
8137 | const int sizeOld = sizeCurrent < 0 ? 0 : sizeCurrent; | |
8138 | ||
8139 | sizeCurrent = sizeNew; | |
8140 | ||
8141 | return sizeCurrent - sizeOld; | |
8142 | } | |
8143 | } | |
8144 | ||
8145 | } // anonymous namespace | |
8146 | ||
f85afd4e MB |
8147 | void wxGrid::SetRowSize( int row, int height ) |
8148 | { | |
30496905 VZ |
8149 | // See comment in SetColSize |
8150 | if ( height > 0 && height < GetRowMinimalAcceptableHeight()) | |
8151 | return; | |
60ff3b99 | 8152 | |
30496905 VZ |
8153 | // The value of -1 is special and means to fit the height to the row label. |
8154 | if ( height == -1 ) | |
ad7502d8 | 8155 | { |
6922252d VZ |
8156 | // As with the columns, ignore attempts to auto-size the hidden rows. |
8157 | if ( GetRowHeight(row) == 0 ) | |
8158 | return; | |
8159 | ||
ad7502d8 SN |
8160 | long w, h; |
8161 | wxArrayString lines; | |
8162 | wxClientDC dc(m_rowLabelWin); | |
8163 | dc.SetFont(GetLabelFont()); | |
8164 | StringToLines(GetRowLabelValue( row ), lines); | |
ace8d849 VZ |
8165 | GetTextBoxSize( dc, lines, &w, &h ); |
8166 | //check that it is not less than the minimal height | |
8167 | height = wxMax(h, GetRowMinimalAcceptableHeight()); | |
ad7502d8 SN |
8168 | } |
8169 | ||
30496905 VZ |
8170 | DoSetRowSize(row, height); |
8171 | } | |
8172 | ||
8173 | void wxGrid::DoSetRowSize( int row, int height ) | |
8174 | { | |
8175 | wxCHECK_RET( row >= 0 && row < m_numRows, wxT("invalid row index") ); | |
b8d24d4e | 8176 | |
7c1cb261 VZ |
8177 | if ( m_rowHeights.IsEmpty() ) |
8178 | { | |
8179 | // need to really create the array | |
8180 | InitRowHeights(); | |
8181 | } | |
60ff3b99 | 8182 | |
30496905 VZ |
8183 | const int diff = UpdateRowOrColSize(m_rowHeights[row], height); |
8184 | if ( !diff ) | |
8185 | return; | |
60ff3b99 | 8186 | |
0ed3b812 | 8187 | for ( int i = row; i < m_numRows; i++ ) |
f85afd4e | 8188 | { |
b99be8fb | 8189 | m_rowBottoms[i] += diff; |
f85afd4e | 8190 | } |
2f024384 | 8191 | |
a6b40a9a VZ |
8192 | InvalidateBestSize(); |
8193 | ||
faec5a43 | 8194 | if ( !GetBatchCount() ) |
ce84ef6a | 8195 | { |
faec5a43 | 8196 | CalcDimensions(); |
ce84ef6a VZ |
8197 | Refresh(); |
8198 | } | |
f85afd4e MB |
8199 | } |
8200 | ||
8201 | void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) | |
8202 | { | |
ef316e23 VZ |
8203 | // we dont allow zero default column width |
8204 | m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 ); | |
f85afd4e MB |
8205 | |
8206 | if ( resizeExistingCols ) | |
8207 | { | |
b1da8107 SN |
8208 | // since we are resizing all columns to the default column size, |
8209 | // we can simply clear the col widths and col rights | |
8210 | // arrays (which also allows us to take advantage of | |
8211 | // some speed optimisations) | |
8212 | m_colWidths.Empty(); | |
8213 | m_colRights.Empty(); | |
edb89f7e VZ |
8214 | if ( !GetBatchCount() ) |
8215 | CalcDimensions(); | |
f85afd4e MB |
8216 | } |
8217 | } | |
8218 | ||
8219 | void wxGrid::SetColSize( int col, int width ) | |
8220 | { | |
30496905 VZ |
8221 | // we intentionally don't test whether the width is less than |
8222 | // GetColMinimalWidth() here but we do compare it with | |
8223 | // GetColMinimalAcceptableWidth() as otherwise things currently break (see | |
8224 | // #651) -- and we also always allow the width of 0 as it has the special | |
8225 | // sense of hiding the column | |
8226 | if ( width > 0 && width < GetColMinimalAcceptableWidth() ) | |
8227 | return; | |
60ff3b99 | 8228 | |
30496905 VZ |
8229 | // The value of -1 is special and means to fit the width to the column label. |
8230 | if ( width == -1 ) | |
ad7502d8 | 8231 | { |
6922252d VZ |
8232 | // We currently don't support auto-sizing hidden columns. We could, but |
8233 | // it's not clear whether this is really needed and it would make the | |
8234 | // code more complex. | |
8235 | if ( GetColWidth(col) == 0 ) | |
8236 | return; | |
8237 | ||
ad7502d8 SN |
8238 | long w, h; |
8239 | wxArrayString lines; | |
ad805b9e | 8240 | wxClientDC dc(m_colWindow); |
ad7502d8 SN |
8241 | dc.SetFont(GetLabelFont()); |
8242 | StringToLines(GetColLabelValue(col), lines); | |
8243 | if ( GetColLabelTextOrientation() == wxHORIZONTAL ) | |
8244 | GetTextBoxSize( dc, lines, &w, &h ); | |
8245 | else | |
8246 | GetTextBoxSize( dc, lines, &h, &w ); | |
8247 | width = w + 6; | |
ace8d849 | 8248 | //check that it is not less than the minimal width |
54181a33 | 8249 | width = wxMax(width, GetColMinimalAcceptableWidth()); |
ad7502d8 SN |
8250 | } |
8251 | ||
30496905 VZ |
8252 | DoSetColSize(col, width); |
8253 | } | |
8254 | ||
8255 | void wxGrid::DoSetColSize( int col, int width ) | |
8256 | { | |
8257 | wxCHECK_RET( col >= 0 && col < m_numCols, wxT("invalid column index") ); | |
3e13956a | 8258 | |
7c1cb261 VZ |
8259 | if ( m_colWidths.IsEmpty() ) |
8260 | { | |
8261 | // need to really create the array | |
8262 | InitColWidths(); | |
8263 | } | |
f85afd4e | 8264 | |
30496905 VZ |
8265 | const int diff = UpdateRowOrColSize(m_colWidths[col], width); |
8266 | if ( !diff ) | |
8267 | return; | |
8268 | ||
009c7216 | 8269 | if ( m_useNativeHeader ) |
3039ade9 | 8270 | GetGridColHeader()->UpdateColumn(col); |
009c7216 | 8271 | //else: will be refreshed when the header is redrawn |
60ff3b99 | 8272 | |
0ed3b812 | 8273 | for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ ) |
f85afd4e | 8274 | { |
0ed3b812 | 8275 | m_colRights[GetColAt(colPos)] += diff; |
f85afd4e | 8276 | } |
962a48f6 | 8277 | |
a6b40a9a VZ |
8278 | InvalidateBestSize(); |
8279 | ||
faec5a43 | 8280 | if ( !GetBatchCount() ) |
ad805b9e | 8281 | { |
faec5a43 | 8282 | CalcDimensions(); |
ad805b9e VZ |
8283 | Refresh(); |
8284 | } | |
f85afd4e MB |
8285 | } |
8286 | ||
43947979 VZ |
8287 | void wxGrid::SetColMinimalWidth( int col, int width ) |
8288 | { | |
4db6714b KH |
8289 | if (width > GetColMinimalAcceptableWidth()) |
8290 | { | |
c6fbe2f0 | 8291 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col; |
8253f2e0 | 8292 | m_colMinWidths[key] = width; |
b8d24d4e | 8293 | } |
af547d51 VZ |
8294 | } |
8295 | ||
8296 | void wxGrid::SetRowMinimalHeight( int row, int width ) | |
8297 | { | |
4db6714b KH |
8298 | if (width > GetRowMinimalAcceptableHeight()) |
8299 | { | |
c6fbe2f0 | 8300 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row; |
8253f2e0 | 8301 | m_rowMinHeights[key] = width; |
b8d24d4e | 8302 | } |
43947979 VZ |
8303 | } |
8304 | ||
8305 | int wxGrid::GetColMinimalWidth(int col) const | |
8306 | { | |
c6fbe2f0 | 8307 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col; |
8253f2e0 | 8308 | wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key); |
962a48f6 | 8309 | |
ba8c1601 | 8310 | return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth; |
af547d51 VZ |
8311 | } |
8312 | ||
8313 | int wxGrid::GetRowMinimalHeight(int row) const | |
8314 | { | |
c6fbe2f0 | 8315 | wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row; |
8253f2e0 | 8316 | wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key); |
962a48f6 | 8317 | |
ba8c1601 | 8318 | return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight; |
b8d24d4e RG |
8319 | } |
8320 | ||
8321 | void wxGrid::SetColMinimalAcceptableWidth( int width ) | |
8322 | { | |
20c84410 | 8323 | // We do allow a width of 0 since this gives us |
962a48f6 DS |
8324 | // an easy way to temporarily hiding columns. |
8325 | if ( width >= 0 ) | |
8326 | m_minAcceptableColWidth = width; | |
b8d24d4e RG |
8327 | } |
8328 | ||
8329 | void wxGrid::SetRowMinimalAcceptableHeight( int height ) | |
8330 | { | |
20c84410 | 8331 | // We do allow a height of 0 since this gives us |
962a48f6 DS |
8332 | // an easy way to temporarily hiding rows. |
8333 | if ( height >= 0 ) | |
8334 | m_minAcceptableRowHeight = height; | |
17a1ebd1 | 8335 | } |
b8d24d4e RG |
8336 | |
8337 | int wxGrid::GetColMinimalAcceptableWidth() const | |
8338 | { | |
8339 | return m_minAcceptableColWidth; | |
8340 | } | |
8341 | ||
8342 | int wxGrid::GetRowMinimalAcceptableHeight() const | |
8343 | { | |
8344 | return m_minAcceptableRowHeight; | |
43947979 VZ |
8345 | } |
8346 | ||
57c086ef VZ |
8347 | // ---------------------------------------------------------------------------- |
8348 | // auto sizing | |
8349 | // ---------------------------------------------------------------------------- | |
8350 | ||
733f486a VZ |
8351 | void |
8352 | wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction) | |
65e4e78e | 8353 | { |
733f486a VZ |
8354 | const bool column = direction == wxGRID_COLUMN; |
8355 | ||
6922252d VZ |
8356 | // We don't support auto-sizing hidden rows or columns, this doesn't seem |
8357 | // to make much sense. | |
8358 | if ( column ) | |
8359 | { | |
8360 | if ( GetColWidth(colOrRow) == 0 ) | |
8361 | return; | |
8362 | } | |
8363 | else | |
8364 | { | |
8365 | if ( GetRowHeight(colOrRow) == 0 ) | |
8366 | return; | |
8367 | } | |
8368 | ||
65e4e78e VZ |
8369 | wxClientDC dc(m_gridWin); |
8370 | ||
962a48f6 | 8371 | // cancel editing of cell |
13f6e9e8 RG |
8372 | HideCellEditControl(); |
8373 | SaveEditControlValue(); | |
8374 | ||
68abd97d VZ |
8375 | // initialize both of them just to avoid compiler warnings even if only |
8376 | // really needs to be initialized here | |
8377 | int row, | |
8378 | col; | |
8379 | if ( column ) | |
8380 | { | |
8381 | row = -1; | |
8382 | col = colOrRow; | |
8383 | } | |
8384 | else | |
8385 | { | |
8386 | row = colOrRow; | |
a95e38c0 | 8387 | col = -1; |
68abd97d | 8388 | } |
af547d51 VZ |
8389 | |
8390 | wxCoord extent, extentMax = 0; | |
8391 | int max = column ? m_numRows : m_numCols; | |
39bcce60 | 8392 | for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ ) |
65e4e78e | 8393 | { |
af547d51 | 8394 | if ( column ) |
bc7d2e9d | 8395 | { |
af547d51 | 8396 | row = rowOrCol; |
bc7d2e9d VZ |
8397 | col = colOrRow; |
8398 | } | |
6922252d | 8399 | else |
bc7d2e9d VZ |
8400 | { |
8401 | row = colOrRow; | |
af547d51 | 8402 | col = rowOrCol; |
bc7d2e9d | 8403 | } |
774f6b31 VZ |
8404 | |
8405 | // we need to account for the cells spanning multiple columns/rows: | |
8406 | // while they may need a lot of space, they don't need all of it in | |
8407 | // this column/row | |
8408 | int numRows, numCols; | |
8409 | const CellSpan span = GetCellSize(row, col, &numRows, &numCols); | |
8410 | if ( span == CellSpan_Inside ) | |
8411 | { | |
8412 | // we need to get the size of the main cell, not of a cell hidden | |
8413 | // by it | |
8414 | row += numRows; | |
8415 | col += numCols; | |
8416 | ||
8417 | // get the size of the main cell too | |
8418 | GetCellSize(row, col, &numRows, &numCols); | |
8419 | } | |
af547d51 | 8420 | |
bc7d2e9d | 8421 | // get cell ( main cell if CellSpan_Inside ) renderer best size |
2f024384 DS |
8422 | wxGridCellAttr *attr = GetCellAttr(row, col); |
8423 | wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); | |
65e4e78e VZ |
8424 | if ( renderer ) |
8425 | { | |
af547d51 VZ |
8426 | wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col); |
8427 | extent = column ? size.x : size.y; | |
774f6b31 VZ |
8428 | |
8429 | if ( span != CellSpan_None ) | |
8430 | { | |
8431 | // we spread the size of a spanning cell over all the cells it | |
8432 | // covers evenly -- this is probably not ideal but we can't | |
8433 | // really do much better here | |
8434 | // | |
8435 | // notice that numCols and numRows are never 0 as they | |
8436 | // correspond to the size of the main cell of the span and not | |
8437 | // of the cell inside it | |
8438 | extent /= column ? numCols : numRows; | |
8439 | } | |
8440 | ||
af547d51 | 8441 | if ( extent > extentMax ) |
af547d51 | 8442 | extentMax = extent; |
0b190b0f VZ |
8443 | |
8444 | renderer->DecRef(); | |
65e4e78e VZ |
8445 | } |
8446 | ||
8447 | attr->DecRef(); | |
8448 | } | |
8449 | ||
af547d51 VZ |
8450 | // now also compare with the column label extent |
8451 | wxCoord w, h; | |
65e4e78e | 8452 | dc.SetFont( GetLabelFont() ); |
294d195c MB |
8453 | |
8454 | if ( column ) | |
d43851f7 | 8455 | { |
bc7d2e9d | 8456 | dc.GetMultiLineTextExtent( GetColLabelValue(colOrRow), &w, &h ); |
4db6714b | 8457 | if ( GetColLabelTextOrientation() == wxVERTICAL ) |
d43851f7 JS |
8458 | w = h; |
8459 | } | |
294d195c | 8460 | else |
bc7d2e9d | 8461 | dc.GetMultiLineTextExtent( GetRowLabelValue(colOrRow), &w, &h ); |
294d195c | 8462 | |
af547d51 VZ |
8463 | extent = column ? w : h; |
8464 | if ( extent > extentMax ) | |
af547d51 | 8465 | extentMax = extent; |
65e4e78e | 8466 | |
af547d51 | 8467 | if ( !extentMax ) |
65e4e78e | 8468 | { |
af547d51 | 8469 | // empty column - give default extent (notice that if extentMax is less |
2f024384 | 8470 | // than default extent but != 0, it's OK) |
af547d51 | 8471 | extentMax = column ? m_defaultColWidth : m_defaultRowHeight; |
65e4e78e VZ |
8472 | } |
8473 | else | |
8474 | { | |
a95e38c0 | 8475 | if ( column ) |
a95e38c0 VZ |
8476 | // leave some space around text |
8477 | extentMax += 10; | |
f6bcfd97 | 8478 | else |
f6bcfd97 | 8479 | extentMax += 6; |
65e4e78e VZ |
8480 | } |
8481 | ||
edb89f7e VZ |
8482 | if ( column ) |
8483 | { | |
a01cfc08 VS |
8484 | // Ensure automatic width is not less than minimal width. See the |
8485 | // comment in SetColSize() for explanation of why this isn't done | |
8486 | // in SetColSize(). | |
8487 | if ( !setAsMin ) | |
bc7d2e9d | 8488 | extentMax = wxMax(extentMax, GetColMinimalWidth(colOrRow)); |
a01cfc08 | 8489 | |
bc7d2e9d | 8490 | SetColSize( colOrRow, extentMax ); |
faec5a43 SN |
8491 | if ( !GetBatchCount() ) |
8492 | { | |
ad805b9e VZ |
8493 | if ( m_useNativeHeader ) |
8494 | { | |
bc7d2e9d | 8495 | GetGridColHeader()->UpdateColumn(colOrRow); |
ad805b9e VZ |
8496 | } |
8497 | else | |
8498 | { | |
8499 | int cw, ch, dummy; | |
8500 | m_gridWin->GetClientSize( &cw, &ch ); | |
bc7d2e9d | 8501 | wxRect rect ( CellToRect( 0, colOrRow ) ); |
ad805b9e VZ |
8502 | rect.y = 0; |
8503 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); | |
8504 | rect.width = cw - rect.x; | |
8505 | rect.height = m_colLabelHeight; | |
8506 | GetColLabelWindow()->Refresh( true, &rect ); | |
8507 | } | |
edb89f7e VZ |
8508 | } |
8509 | } | |
8510 | else | |
8511 | { | |
a01cfc08 VS |
8512 | // Ensure automatic width is not less than minimal height. See the |
8513 | // comment in SetColSize() for explanation of why this isn't done | |
8514 | // in SetRowSize(). | |
8515 | if ( !setAsMin ) | |
bc7d2e9d | 8516 | extentMax = wxMax(extentMax, GetRowMinimalHeight(colOrRow)); |
a01cfc08 | 8517 | |
bc7d2e9d | 8518 | SetRowSize(colOrRow, extentMax); |
faec5a43 SN |
8519 | if ( !GetBatchCount() ) |
8520 | { | |
edb89f7e VZ |
8521 | int cw, ch, dummy; |
8522 | m_gridWin->GetClientSize( &cw, &ch ); | |
bc7d2e9d | 8523 | wxRect rect( CellToRect( colOrRow, 0 ) ); |
edb89f7e VZ |
8524 | rect.x = 0; |
8525 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
8526 | rect.width = m_rowLabelWidth; | |
faec5a43 | 8527 | rect.height = ch - rect.y; |
ca65c044 | 8528 | m_rowLabelWin->Refresh( true, &rect ); |
edb89f7e | 8529 | } |
faec5a43 | 8530 | } |
2f024384 | 8531 | |
65e4e78e VZ |
8532 | if ( setAsMin ) |
8533 | { | |
af547d51 | 8534 | if ( column ) |
bc7d2e9d | 8535 | SetColMinimalWidth(colOrRow, extentMax); |
af547d51 | 8536 | else |
bc7d2e9d | 8537 | SetRowMinimalHeight(colOrRow, extentMax); |
65e4e78e VZ |
8538 | } |
8539 | } | |
8540 | ||
733f486a VZ |
8541 | wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction) |
8542 | { | |
8543 | // calculate size for the rows or columns? | |
8544 | const bool calcRows = direction == wxGRID_ROW; | |
8545 | ||
8546 | wxClientDC dc(calcRows ? GetGridRowLabelWindow() | |
8547 | : GetGridColLabelWindow()); | |
8548 | dc.SetFont(GetLabelFont()); | |
8549 | ||
8550 | // which dimension should we take into account for calculations? | |
8551 | // | |
8552 | // for columns, the text can be only horizontal so it's easy but for rows | |
8553 | // we also have to take into account the text orientation | |
8554 | const bool | |
8555 | useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL); | |
8556 | ||
8557 | wxArrayString lines; | |
8558 | wxCoord extentMax = 0; | |
8559 | ||
8560 | const int numRowsOrCols = calcRows ? m_numRows : m_numCols; | |
8561 | for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ ) | |
8562 | { | |
8563 | lines.Clear(); | |
add4bb40 VS |
8564 | |
8565 | wxString label = calcRows ? GetRowLabelValue(rowOrCol) | |
8566 | : GetColLabelValue(rowOrCol); | |
8567 | StringToLines(label, lines); | |
733f486a VZ |
8568 | |
8569 | long w, h; | |
8570 | GetTextBoxSize(dc, lines, &w, &h); | |
8571 | ||
8572 | const wxCoord extent = useWidth ? w : h; | |
8573 | if ( extent > extentMax ) | |
8574 | extentMax = extent; | |
8575 | } | |
8576 | ||
8577 | if ( !extentMax ) | |
8578 | { | |
8579 | // empty column - give default extent (notice that if extentMax is less | |
8580 | // than default extent but != 0, it's OK) | |
8581 | extentMax = calcRows ? GetDefaultRowLabelSize() | |
8582 | : GetDefaultColLabelSize(); | |
8583 | } | |
8584 | ||
8585 | // leave some space around text (taken from AutoSizeColOrRow) | |
8586 | if ( calcRows ) | |
8587 | extentMax += 10; | |
8588 | else | |
8589 | extentMax += 6; | |
8590 | ||
8591 | return extentMax; | |
8592 | } | |
8593 | ||
266e8367 | 8594 | int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) |
65e4e78e | 8595 | { |
57c086ef VZ |
8596 | int width = m_rowLabelWidth; |
8597 | ||
b62f94ff VZ |
8598 | wxGridUpdateLocker locker; |
8599 | if(!calcOnly) | |
8600 | locker.Create(this); | |
97a9929e | 8601 | |
65e4e78e VZ |
8602 | for ( int col = 0; col < m_numCols; col++ ) |
8603 | { | |
266e8367 | 8604 | if ( !calcOnly ) |
266e8367 | 8605 | AutoSizeColumn(col, setAsMin); |
57c086ef VZ |
8606 | |
8607 | width += GetColWidth(col); | |
65e4e78e | 8608 | } |
97a9929e | 8609 | |
266e8367 | 8610 | return width; |
65e4e78e VZ |
8611 | } |
8612 | ||
266e8367 | 8613 | int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin) |
57c086ef VZ |
8614 | { |
8615 | int height = m_colLabelHeight; | |
8616 | ||
b62f94ff VZ |
8617 | wxGridUpdateLocker locker; |
8618 | if(!calcOnly) | |
8619 | locker.Create(this); | |
97a9929e | 8620 | |
57c086ef VZ |
8621 | for ( int row = 0; row < m_numRows; row++ ) |
8622 | { | |
af547d51 | 8623 | if ( !calcOnly ) |
af547d51 | 8624 | AutoSizeRow(row, setAsMin); |
57c086ef VZ |
8625 | |
8626 | height += GetRowHeight(row); | |
8627 | } | |
97a9929e | 8628 | |
266e8367 | 8629 | return height; |
57c086ef VZ |
8630 | } |
8631 | ||
8632 | void wxGrid::AutoSize() | |
8633 | { | |
b62f94ff | 8634 | wxGridUpdateLocker locker(this); |
97a9929e | 8635 | |
0ed3b812 VZ |
8636 | wxSize size(SetOrCalcColumnSizes(false) - m_rowLabelWidth + m_extraWidth, |
8637 | SetOrCalcRowSizes(false) - m_colLabelHeight + m_extraHeight); | |
97a9929e | 8638 | |
0ed3b812 VZ |
8639 | // we know that we're not going to have scrollbars so disable them now to |
8640 | // avoid trouble in SetClientSize() which can otherwise set the correct | |
8641 | // client size but also leave space for (not needed any more) scrollbars | |
fd76c6a7 VZ |
8642 | SetScrollbars(m_xScrollPixelsPerLine, m_yScrollPixelsPerLine, |
8643 | 0, 0, 0, 0, true); | |
72b0a1de VZ |
8644 | |
8645 | SetClientSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight); | |
266e8367 VZ |
8646 | } |
8647 | ||
d43851f7 JS |
8648 | void wxGrid::AutoSizeRowLabelSize( int row ) |
8649 | { | |
d43851f7 | 8650 | // Hide the edit control, so it |
4db6714b KH |
8651 | // won't interfere with drag-shrinking. |
8652 | if ( IsCellEditControlShown() ) | |
d43851f7 JS |
8653 | { |
8654 | HideCellEditControl(); | |
8655 | SaveEditControlValue(); | |
8656 | } | |
8657 | ||
8658 | // autosize row height depending on label text | |
ad7502d8 | 8659 | SetRowSize(row, -1); |
30496905 | 8660 | |
d43851f7 JS |
8661 | ForceRefresh(); |
8662 | } | |
8663 | ||
8664 | void wxGrid::AutoSizeColLabelSize( int col ) | |
8665 | { | |
d43851f7 | 8666 | // Hide the edit control, so it |
c2f5b920 | 8667 | // won't interfere with drag-shrinking. |
4db6714b | 8668 | if ( IsCellEditControlShown() ) |
d43851f7 JS |
8669 | { |
8670 | HideCellEditControl(); | |
8671 | SaveEditControlValue(); | |
8672 | } | |
8673 | ||
8674 | // autosize column width depending on label text | |
ad7502d8 | 8675 | SetColSize(col, -1); |
30496905 | 8676 | |
d43851f7 JS |
8677 | ForceRefresh(); |
8678 | } | |
8679 | ||
266e8367 VZ |
8680 | wxSize wxGrid::DoGetBestSize() const |
8681 | { | |
f48a1159 | 8682 | wxGrid * const self = const_cast<wxGrid *>(this); |
266e8367 | 8683 | |
dc4689ef VZ |
8684 | // we do the same as in AutoSize() here with the exception that we don't |
8685 | // change the column/row sizes, only calculate them | |
8686 | wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth, | |
8687 | self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight); | |
962a48f6 | 8688 | |
72b0a1de | 8689 | return wxSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight) |
dc4689ef | 8690 | + GetWindowBorderSize(); |
266e8367 VZ |
8691 | } |
8692 | ||
8693 | void wxGrid::Fit() | |
8694 | { | |
8695 | AutoSize(); | |
57c086ef VZ |
8696 | } |
8697 | ||
7bc57fd9 | 8698 | #if WXWIN_COMPATIBILITY_2_8 |
6fc0f38f SN |
8699 | wxPen& wxGrid::GetDividerPen() const |
8700 | { | |
8701 | return wxNullPen; | |
8702 | } | |
7bc57fd9 | 8703 | #endif // WXWIN_COMPATIBILITY_2_8 |
6fc0f38f | 8704 | |
57c086ef VZ |
8705 | // ---------------------------------------------------------------------------- |
8706 | // cell value accessor functions | |
8707 | // ---------------------------------------------------------------------------- | |
f85afd4e MB |
8708 | |
8709 | void wxGrid::SetCellValue( int row, int col, const wxString& s ) | |
8710 | { | |
8711 | if ( m_table ) | |
8712 | { | |
f6bcfd97 | 8713 | m_table->SetValue( row, col, s ); |
2d66e025 MB |
8714 | if ( !GetBatchCount() ) |
8715 | { | |
27f35b66 SN |
8716 | int dummy; |
8717 | wxRect rect( CellToRect( row, col ) ); | |
8718 | rect.x = 0; | |
8719 | rect.width = m_gridWin->GetClientSize().GetWidth(); | |
8720 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); | |
ca65c044 | 8721 | m_gridWin->Refresh( false, &rect ); |
2d66e025 | 8722 | } |
60ff3b99 | 8723 | |
f85afd4e | 8724 | if ( m_currentCellCoords.GetRow() == row && |
4cfa5de6 | 8725 | m_currentCellCoords.GetCol() == col && |
f6bcfd97 BP |
8726 | IsCellEditControlShown()) |
8727 | // Note: If we are using IsCellEditControlEnabled, | |
8728 | // this interacts badly with calling SetCellValue from | |
8729 | // an EVT_GRID_CELL_CHANGE handler. | |
f85afd4e | 8730 | { |
4cfa5de6 RD |
8731 | HideCellEditControl(); |
8732 | ShowCellEditControl(); // will reread data from table | |
f85afd4e | 8733 | } |
f85afd4e MB |
8734 | } |
8735 | } | |
8736 | ||
962a48f6 | 8737 | // ---------------------------------------------------------------------------- |
2f024384 | 8738 | // block, row and column selection |
962a48f6 | 8739 | // ---------------------------------------------------------------------------- |
f85afd4e MB |
8740 | |
8741 | void wxGrid::SelectRow( int row, bool addToSelected ) | |
8742 | { | |
8b5f6d9d VZ |
8743 | if ( !m_selection ) |
8744 | return; | |
8745 | ||
8746 | if ( !addToSelected ) | |
e32352cf | 8747 | ClearSelection(); |
70c7a608 | 8748 | |
8b5f6d9d | 8749 | m_selection->SelectRow(row); |
f85afd4e MB |
8750 | } |
8751 | ||
f85afd4e MB |
8752 | void wxGrid::SelectCol( int col, bool addToSelected ) |
8753 | { | |
8b5f6d9d VZ |
8754 | if ( !m_selection ) |
8755 | return; | |
8756 | ||
8757 | if ( !addToSelected ) | |
e32352cf | 8758 | ClearSelection(); |
f85afd4e | 8759 | |
8b5f6d9d | 8760 | m_selection->SelectCol(col); |
f85afd4e MB |
8761 | } |
8762 | ||
8b5f6d9d VZ |
8763 | void wxGrid::SelectBlock(int topRow, int leftCol, int bottomRow, int rightCol, |
8764 | bool addToSelected) | |
f85afd4e | 8765 | { |
8b5f6d9d VZ |
8766 | if ( !m_selection ) |
8767 | return; | |
8768 | ||
8769 | if ( !addToSelected ) | |
c9097836 | 8770 | ClearSelection(); |
f85afd4e | 8771 | |
8b5f6d9d | 8772 | m_selection->SelectBlock(topRow, leftCol, bottomRow, rightCol); |
f85afd4e MB |
8773 | } |
8774 | ||
8775 | void wxGrid::SelectAll() | |
8776 | { | |
f74d0b57 | 8777 | if ( m_numRows > 0 && m_numCols > 0 ) |
3f3dc2ef VZ |
8778 | { |
8779 | if ( m_selection ) | |
ccdee36f | 8780 | m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 ); |
3f3dc2ef | 8781 | } |
b5808881 | 8782 | } |
f85afd4e | 8783 | |
962a48f6 DS |
8784 | // ---------------------------------------------------------------------------- |
8785 | // cell, row and col deselection | |
8786 | // ---------------------------------------------------------------------------- | |
f7b4b343 | 8787 | |
bec70262 | 8788 | void wxGrid::DeselectLine(int line, const wxGridOperations& oper) |
f7b4b343 | 8789 | { |
3f3dc2ef VZ |
8790 | if ( !m_selection ) |
8791 | return; | |
8792 | ||
bec70262 | 8793 | const wxGridSelectionModes mode = m_selection->GetSelectionMode(); |
475be9ce VZ |
8794 | if ( mode == oper.GetSelectionMode() || |
8795 | mode == wxGrid::wxGridSelectRowsOrColumns ) | |
f7b4b343 | 8796 | { |
bec70262 VZ |
8797 | const wxGridCellCoords c(oper.MakeCoords(line, 0)); |
8798 | if ( m_selection->IsInSelection(c) ) | |
8799 | m_selection->ToggleCellSelection(c); | |
ffdd3c98 | 8800 | } |
bec70262 | 8801 | else if ( mode != oper.Dual().GetSelectionMode() ) |
f7b4b343 | 8802 | { |
bec70262 VZ |
8803 | const int nOther = oper.Dual().GetNumberOfLines(this); |
8804 | for ( int i = 0; i < nOther; i++ ) | |
f7b4b343 | 8805 | { |
bec70262 VZ |
8806 | const wxGridCellCoords c(oper.MakeCoords(line, i)); |
8807 | if ( m_selection->IsInSelection(c) ) | |
8808 | m_selection->ToggleCellSelection(c); | |
f7b4b343 VZ |
8809 | } |
8810 | } | |
bec70262 VZ |
8811 | //else: can only select orthogonal lines so no lines in this direction |
8812 | // could have been selected anyhow | |
f7b4b343 VZ |
8813 | } |
8814 | ||
bec70262 | 8815 | void wxGrid::DeselectRow(int row) |
f7b4b343 | 8816 | { |
bec70262 VZ |
8817 | DeselectLine(row, wxGridRowOperations()); |
8818 | } | |
3f3dc2ef | 8819 | |
bec70262 VZ |
8820 | void wxGrid::DeselectCol(int col) |
8821 | { | |
8822 | DeselectLine(col, wxGridColumnOperations()); | |
f7b4b343 VZ |
8823 | } |
8824 | ||
8825 | void wxGrid::DeselectCell( int row, int col ) | |
8826 | { | |
3f3dc2ef | 8827 | if ( m_selection && m_selection->IsInSelection(row, col) ) |
f7b4b343 VZ |
8828 | m_selection->ToggleCellSelection(row, col); |
8829 | } | |
8830 | ||
ef316e23 | 8831 | bool wxGrid::IsSelection() const |
b5808881 | 8832 | { |
3f3dc2ef | 8833 | return ( m_selection && (m_selection->IsSelection() || |
8a3e536c VZ |
8834 | ( m_selectedBlockTopLeft != wxGridNoCellCoords && |
8835 | m_selectedBlockBottomRight != wxGridNoCellCoords) ) ); | |
f85afd4e MB |
8836 | } |
8837 | ||
84035150 | 8838 | bool wxGrid::IsInSelection( int row, int col ) const |
b5808881 | 8839 | { |
3f3dc2ef | 8840 | return ( m_selection && (m_selection->IsInSelection( row, col ) || |
8a3e536c VZ |
8841 | ( row >= m_selectedBlockTopLeft.GetRow() && |
8842 | col >= m_selectedBlockTopLeft.GetCol() && | |
8843 | row <= m_selectedBlockBottomRight.GetRow() && | |
8844 | col <= m_selectedBlockBottomRight.GetCol() )) ); | |
b5808881 | 8845 | } |
f85afd4e | 8846 | |
aa5b8857 SN |
8847 | wxGridCellCoordsArray wxGrid::GetSelectedCells() const |
8848 | { | |
4db6714b KH |
8849 | if (!m_selection) |
8850 | { | |
8851 | wxGridCellCoordsArray a; | |
8852 | return a; | |
8853 | } | |
8854 | ||
aa5b8857 SN |
8855 | return m_selection->m_cellSelection; |
8856 | } | |
4db6714b | 8857 | |
aa5b8857 SN |
8858 | wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const |
8859 | { | |
4db6714b KH |
8860 | if (!m_selection) |
8861 | { | |
8862 | wxGridCellCoordsArray a; | |
8863 | return a; | |
8864 | } | |
8865 | ||
aa5b8857 SN |
8866 | return m_selection->m_blockSelectionTopLeft; |
8867 | } | |
4db6714b | 8868 | |
aa5b8857 SN |
8869 | wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const |
8870 | { | |
4db6714b KH |
8871 | if (!m_selection) |
8872 | { | |
8873 | wxGridCellCoordsArray a; | |
8874 | return a; | |
8875 | } | |
8876 | ||
a8de8190 | 8877 | return m_selection->m_blockSelectionBottomRight; |
aa5b8857 | 8878 | } |
4db6714b | 8879 | |
aa5b8857 SN |
8880 | wxArrayInt wxGrid::GetSelectedRows() const |
8881 | { | |
4db6714b KH |
8882 | if (!m_selection) |
8883 | { | |
8884 | wxArrayInt a; | |
8885 | return a; | |
8886 | } | |
8887 | ||
aa5b8857 SN |
8888 | return m_selection->m_rowSelection; |
8889 | } | |
4db6714b | 8890 | |
aa5b8857 SN |
8891 | wxArrayInt wxGrid::GetSelectedCols() const |
8892 | { | |
4db6714b KH |
8893 | if (!m_selection) |
8894 | { | |
8895 | wxArrayInt a; | |
8896 | return a; | |
8897 | } | |
8898 | ||
aa5b8857 SN |
8899 | return m_selection->m_colSelection; |
8900 | } | |
8901 | ||
f85afd4e MB |
8902 | void wxGrid::ClearSelection() |
8903 | { | |
8a3e536c VZ |
8904 | wxRect r1 = BlockToDeviceRect(m_selectedBlockTopLeft, |
8905 | m_selectedBlockBottomRight); | |
8906 | wxRect r2 = BlockToDeviceRect(m_currentCellCoords, | |
8907 | m_selectedBlockCorner); | |
8908 | ||
8909 | m_selectedBlockTopLeft = | |
8910 | m_selectedBlockBottomRight = | |
8911 | m_selectedBlockCorner = wxGridNoCellCoords; | |
8912 | ||
efc49a21 VZ |
8913 | if ( !r1.IsEmpty() ) |
8914 | RefreshRect(r1, false); | |
8915 | if ( !r2.IsEmpty() ) | |
8916 | RefreshRect(r2, false); | |
8a3e536c | 8917 | |
3f3dc2ef VZ |
8918 | if ( m_selection ) |
8919 | m_selection->ClearSelection(); | |
8f177c8e | 8920 | } |
f85afd4e | 8921 | |
da6af900 | 8922 | // This function returns the rectangle that encloses the given block |
2d66e025 MB |
8923 | // in device coords clipped to the client size of the grid window. |
8924 | // | |
731330ec VZ |
8925 | wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords& topLeft, |
8926 | const wxGridCellCoords& bottomRight ) const | |
f85afd4e | 8927 | { |
731330ec VZ |
8928 | wxRect resultRect; |
8929 | wxRect tempCellRect = CellToRect(topLeft); | |
8930 | if ( tempCellRect != wxGridNoCellRect ) | |
f85afd4e | 8931 | { |
731330ec | 8932 | resultRect = tempCellRect; |
58dd5b3b MB |
8933 | } |
8934 | else | |
8935 | { | |
731330ec | 8936 | resultRect = wxRect(0, 0, 0, 0); |
58dd5b3b | 8937 | } |
60ff3b99 | 8938 | |
731330ec VZ |
8939 | tempCellRect = CellToRect(bottomRight); |
8940 | if ( tempCellRect != wxGridNoCellRect ) | |
58dd5b3b | 8941 | { |
731330ec | 8942 | resultRect += tempCellRect; |
2d66e025 MB |
8943 | } |
8944 | else | |
8945 | { | |
731330ec | 8946 | // If both inputs were "wxGridNoCellRect," then there's nothing to do. |
2d66e025 | 8947 | return wxGridNoCellRect; |
f85afd4e MB |
8948 | } |
8949 | ||
731330ec VZ |
8950 | // Ensure that left/right and top/bottom pairs are in order. |
8951 | int left = resultRect.GetLeft(); | |
8952 | int top = resultRect.GetTop(); | |
8953 | int right = resultRect.GetRight(); | |
8954 | int bottom = resultRect.GetBottom(); | |
27f35b66 SN |
8955 | |
8956 | int leftCol = topLeft.GetCol(); | |
8957 | int topRow = topLeft.GetRow(); | |
8958 | int rightCol = bottomRight.GetCol(); | |
8959 | int bottomRow = bottomRight.GetRow(); | |
8960 | ||
3ed884a0 SN |
8961 | if (left > right) |
8962 | { | |
731330ec | 8963 | int tmp = left; |
3ed884a0 | 8964 | left = right; |
731330ec VZ |
8965 | right = tmp; |
8966 | ||
8967 | tmp = leftCol; | |
4db6714b | 8968 | leftCol = rightCol; |
731330ec | 8969 | rightCol = tmp; |
3ed884a0 SN |
8970 | } |
8971 | ||
8972 | if (top > bottom) | |
8973 | { | |
731330ec | 8974 | int tmp = top; |
3ed884a0 | 8975 | top = bottom; |
731330ec VZ |
8976 | bottom = tmp; |
8977 | ||
8978 | tmp = topRow; | |
3ed884a0 | 8979 | topRow = bottomRow; |
731330ec | 8980 | bottomRow = tmp; |
3ed884a0 SN |
8981 | } |
8982 | ||
731330ec VZ |
8983 | // The following loop is ONLY necessary to detect and handle merged cells. |
8984 | int cw, ch; | |
8985 | m_gridWin->GetClientSize( &cw, &ch ); | |
8986 | ||
8987 | // Get the origin coordinates: notice that they will be negative if the | |
8988 | // grid is scrolled downwards/to the right. | |
8989 | int gridOriginX = 0; | |
8990 | int gridOriginY = 0; | |
8991 | CalcScrolledPosition(gridOriginX, gridOriginY, &gridOriginX, &gridOriginY); | |
8992 | ||
8993 | int onScreenLeftmostCol = internalXToCol(-gridOriginX); | |
8994 | int onScreenUppermostRow = internalYToRow(-gridOriginY); | |
8995 | ||
8996 | int onScreenRightmostCol = internalXToCol(-gridOriginX + cw); | |
8997 | int onScreenBottommostRow = internalYToRow(-gridOriginY + ch); | |
8998 | ||
8999 | // Bound our loop so that we only examine the portion of the selected block | |
9000 | // that is shown on screen. Therefore, we compare the Top-Left block values | |
9001 | // to the Top-Left screen values, and the Bottom-Right block values to the | |
9002 | // Bottom-Right screen values, choosing appropriately. | |
9003 | const int visibleTopRow = wxMax(topRow, onScreenUppermostRow); | |
9004 | const int visibleBottomRow = wxMin(bottomRow, onScreenBottommostRow); | |
9005 | const int visibleLeftCol = wxMax(leftCol, onScreenLeftmostCol); | |
9006 | const int visibleRightCol = wxMin(rightCol, onScreenRightmostCol); | |
9007 | ||
9008 | for ( int j = visibleTopRow; j <= visibleBottomRow; j++ ) | |
27f35b66 | 9009 | { |
731330ec | 9010 | for ( int i = visibleLeftCol; i <= visibleRightCol; i++ ) |
27f35b66 | 9011 | { |
731330ec VZ |
9012 | if ( (j == visibleTopRow) || (j == visibleBottomRow) || |
9013 | (i == visibleLeftCol) || (i == visibleRightCol) ) | |
27f35b66 | 9014 | { |
731330ec | 9015 | tempCellRect = CellToRect( j, i ); |
27f35b66 | 9016 | |
731330ec VZ |
9017 | if (tempCellRect.x < left) |
9018 | left = tempCellRect.x; | |
9019 | if (tempCellRect.y < top) | |
9020 | top = tempCellRect.y; | |
9021 | if (tempCellRect.x + tempCellRect.width > right) | |
9022 | right = tempCellRect.x + tempCellRect.width; | |
9023 | if (tempCellRect.y + tempCellRect.height > bottom) | |
9024 | bottom = tempCellRect.y + tempCellRect.height; | |
27f35b66 | 9025 | } |
4db6714b KH |
9026 | else |
9027 | { | |
731330ec | 9028 | i = visibleRightCol; // jump over inner cells. |
4db6714b | 9029 | } |
27f35b66 SN |
9030 | } |
9031 | } | |
9032 | ||
731330ec | 9033 | // Convert to scrolled coords |
27f35b66 SN |
9034 | CalcScrolledPosition( left, top, &left, &top ); |
9035 | CalcScrolledPosition( right, bottom, &right, &bottom ); | |
58dd5b3b | 9036 | |
f6bcfd97 | 9037 | if (right < 0 || bottom < 0 || left > cw || top > ch) |
c47addef | 9038 | return wxRect(0,0,0,0); |
f6bcfd97 | 9039 | |
731330ec VZ |
9040 | resultRect.SetLeft( wxMax(0, left) ); |
9041 | resultRect.SetTop( wxMax(0, top) ); | |
9042 | resultRect.SetRight( wxMin(cw, right) ); | |
9043 | resultRect.SetBottom( wxMin(ch, bottom) ); | |
58dd5b3b | 9044 | |
731330ec | 9045 | return resultRect; |
f85afd4e MB |
9046 | } |
9047 | ||
574e1c5a VZ |
9048 | void wxGrid::DoSetSizes(const wxGridSizesInfo& sizeInfo, |
9049 | const wxGridOperations& oper) | |
9050 | { | |
9051 | BeginBatch(); | |
9052 | oper.SetDefaultLineSize(this, sizeInfo.m_sizeDefault, true); | |
9053 | const int numLines = oper.GetNumberOfLines(this); | |
9054 | for ( int i = 0; i < numLines; i++ ) | |
9055 | { | |
9056 | int size = sizeInfo.GetSize(i); | |
9057 | if ( size != sizeInfo.m_sizeDefault) | |
9058 | oper.SetLineSize(this, i, size); | |
9059 | } | |
9060 | EndBatch(); | |
9061 | } | |
9062 | ||
9063 | void wxGrid::SetColSizes(const wxGridSizesInfo& sizeInfo) | |
9064 | { | |
9065 | DoSetSizes(sizeInfo, wxGridColumnOperations()); | |
9066 | } | |
9067 | ||
9068 | void wxGrid::SetRowSizes(const wxGridSizesInfo& sizeInfo) | |
9069 | { | |
9070 | DoSetSizes(sizeInfo, wxGridRowOperations()); | |
9071 | } | |
9072 | ||
9073 | wxGridSizesInfo::wxGridSizesInfo(int defSize, const wxArrayInt& allSizes) | |
9074 | { | |
9075 | m_sizeDefault = defSize; | |
9076 | for ( size_t i = 0; i < allSizes.size(); i++ ) | |
9077 | { | |
9078 | if ( allSizes[i] != defSize ) | |
9079 | m_customSizes[i] = allSizes[i]; | |
9080 | } | |
9081 | } | |
9082 | ||
9083 | int wxGridSizesInfo::GetSize(unsigned pos) const | |
9084 | { | |
9085 | wxUnsignedToIntHashMap::const_iterator it = m_customSizes.find(pos); | |
9086 | ||
30496905 VZ |
9087 | // if it's not found return the default |
9088 | if ( it == m_customSizes.end() ) | |
9089 | return m_sizeDefault; | |
9090 | ||
9091 | // otherwise return 0 if it's hidden, currently there is no way to get | |
9092 | // its size before it had been hidden | |
9093 | if ( it->second < 0 ) | |
9094 | return 0; | |
9095 | ||
9096 | return it->second; | |
574e1c5a VZ |
9097 | } |
9098 | ||
1edce33f VZ |
9099 | // ---------------------------------------------------------------------------- |
9100 | // drop target | |
9101 | // ---------------------------------------------------------------------------- | |
9102 | ||
9103 | #if wxUSE_DRAG_AND_DROP | |
9104 | ||
9105 | // this allow setting drop target directly on wxGrid | |
9106 | void wxGrid::SetDropTarget(wxDropTarget *dropTarget) | |
9107 | { | |
9108 | GetGridWindow()->SetDropTarget(dropTarget); | |
9109 | } | |
9110 | ||
9111 | #endif // wxUSE_DRAG_AND_DROP | |
9112 | ||
962a48f6 DS |
9113 | // ---------------------------------------------------------------------------- |
9114 | // grid event classes | |
9115 | // ---------------------------------------------------------------------------- | |
f85afd4e | 9116 | |
bf7945ce | 9117 | IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent ) |
f85afd4e MB |
9118 | |
9119 | wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, | |
5c8fc7c1 | 9120 | int row, int col, int x, int y, bool sel, |
f85afd4e | 9121 | bool control, bool shift, bool alt, bool meta ) |
8b5f6d9d VZ |
9122 | : wxNotifyEvent( type, id ), |
9123 | wxKeyboardState(control, shift, alt, meta) | |
f85afd4e | 9124 | { |
8b5f6d9d | 9125 | Init(row, col, x, y, sel); |
8f177c8e | 9126 | |
f85afd4e MB |
9127 | SetEventObject(obj); |
9128 | } | |
9129 | ||
bf7945ce | 9130 | IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent ) |
f85afd4e MB |
9131 | |
9132 | wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj, | |
9133 | int rowOrCol, int x, int y, | |
9134 | bool control, bool shift, bool alt, bool meta ) | |
8b5f6d9d VZ |
9135 | : wxNotifyEvent( type, id ), |
9136 | wxKeyboardState(control, shift, alt, meta) | |
f85afd4e | 9137 | { |
8b5f6d9d | 9138 | Init(rowOrCol, x, y); |
8f177c8e | 9139 | |
f85afd4e MB |
9140 | SetEventObject(obj); |
9141 | } | |
9142 | ||
9143 | ||
bf7945ce | 9144 | IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent ) |
f85afd4e MB |
9145 | |
9146 | wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, | |
8f177c8e VZ |
9147 | const wxGridCellCoords& topLeft, |
9148 | const wxGridCellCoords& bottomRight, | |
5c8fc7c1 SN |
9149 | bool sel, bool control, |
9150 | bool shift, bool alt, bool meta ) | |
8b5f6d9d VZ |
9151 | : wxNotifyEvent( type, id ), |
9152 | wxKeyboardState(control, shift, alt, meta) | |
9153 | { | |
9154 | Init(topLeft, bottomRight, sel); | |
f85afd4e MB |
9155 | |
9156 | SetEventObject(obj); | |
9157 | } | |
9158 | ||
9159 | ||
bf7945ce RD |
9160 | IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent) |
9161 | ||
9162 | wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type, | |
9163 | wxObject* obj, int row, | |
9164 | int col, wxControl* ctrl) | |
9165 | : wxCommandEvent(type, id) | |
9166 | { | |
9167 | SetEventObject(obj); | |
9168 | m_row = row; | |
9169 | m_col = col; | |
9170 | m_ctrl = ctrl; | |
9171 | } | |
9172 | ||
29efc6e4 FM |
9173 | |
9174 | // ---------------------------------------------------------------------------- | |
9175 | // wxGridTypeRegistry | |
9176 | // ---------------------------------------------------------------------------- | |
9177 | ||
9178 | wxGridTypeRegistry::~wxGridTypeRegistry() | |
9179 | { | |
9180 | size_t count = m_typeinfo.GetCount(); | |
9181 | for ( size_t i = 0; i < count; i++ ) | |
9182 | delete m_typeinfo[i]; | |
9183 | } | |
9184 | ||
9185 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, | |
9186 | wxGridCellRenderer* renderer, | |
9187 | wxGridCellEditor* editor) | |
9188 | { | |
9189 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); | |
9190 | ||
9191 | // is it already registered? | |
9192 | int loc = FindRegisteredDataType(typeName); | |
9193 | if ( loc != wxNOT_FOUND ) | |
9194 | { | |
9195 | delete m_typeinfo[loc]; | |
9196 | m_typeinfo[loc] = info; | |
9197 | } | |
9198 | else | |
9199 | { | |
9200 | m_typeinfo.Add(info); | |
9201 | } | |
9202 | } | |
9203 | ||
9204 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) | |
9205 | { | |
9206 | size_t count = m_typeinfo.GetCount(); | |
9207 | for ( size_t i = 0; i < count; i++ ) | |
9208 | { | |
9209 | if ( typeName == m_typeinfo[i]->m_typeName ) | |
9210 | { | |
9211 | return i; | |
9212 | } | |
9213 | } | |
9214 | ||
9215 | return wxNOT_FOUND; | |
9216 | } | |
9217 | ||
9218 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) | |
9219 | { | |
9220 | int index = FindRegisteredDataType(typeName); | |
9221 | if ( index == wxNOT_FOUND ) | |
9222 | { | |
9223 | // check whether this is one of the standard ones, in which case | |
9224 | // register it "on the fly" | |
9225 | #if wxUSE_TEXTCTRL | |
9226 | if ( typeName == wxGRID_VALUE_STRING ) | |
9227 | { | |
9228 | RegisterDataType(wxGRID_VALUE_STRING, | |
9229 | new wxGridCellStringRenderer, | |
9230 | new wxGridCellTextEditor); | |
9231 | } | |
9232 | else | |
9233 | #endif // wxUSE_TEXTCTRL | |
9234 | #if wxUSE_CHECKBOX | |
9235 | if ( typeName == wxGRID_VALUE_BOOL ) | |
9236 | { | |
9237 | RegisterDataType(wxGRID_VALUE_BOOL, | |
9238 | new wxGridCellBoolRenderer, | |
9239 | new wxGridCellBoolEditor); | |
9240 | } | |
9241 | else | |
9242 | #endif // wxUSE_CHECKBOX | |
9243 | #if wxUSE_TEXTCTRL | |
9244 | if ( typeName == wxGRID_VALUE_NUMBER ) | |
9245 | { | |
9246 | RegisterDataType(wxGRID_VALUE_NUMBER, | |
9247 | new wxGridCellNumberRenderer, | |
9248 | new wxGridCellNumberEditor); | |
9249 | } | |
9250 | else if ( typeName == wxGRID_VALUE_FLOAT ) | |
9251 | { | |
9252 | RegisterDataType(wxGRID_VALUE_FLOAT, | |
9253 | new wxGridCellFloatRenderer, | |
9254 | new wxGridCellFloatEditor); | |
9255 | } | |
9256 | else | |
9257 | #endif // wxUSE_TEXTCTRL | |
9258 | #if wxUSE_COMBOBOX | |
9259 | if ( typeName == wxGRID_VALUE_CHOICE ) | |
9260 | { | |
9261 | RegisterDataType(wxGRID_VALUE_CHOICE, | |
9262 | new wxGridCellStringRenderer, | |
9263 | new wxGridCellChoiceEditor); | |
9264 | } | |
9265 | else | |
9266 | #endif // wxUSE_COMBOBOX | |
9267 | { | |
9268 | return wxNOT_FOUND; | |
9269 | } | |
9270 | ||
9271 | // we get here only if just added the entry for this type, so return | |
9272 | // the last index | |
9273 | index = m_typeinfo.GetCount() - 1; | |
9274 | } | |
9275 | ||
9276 | return index; | |
9277 | } | |
9278 | ||
9279 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) | |
9280 | { | |
9281 | int index = FindDataType(typeName); | |
9282 | if ( index == wxNOT_FOUND ) | |
9283 | { | |
9284 | // the first part of the typename is the "real" type, anything after ':' | |
9285 | // are the parameters for the renderer | |
9a83f860 | 9286 | index = FindDataType(typeName.BeforeFirst(wxT(':'))); |
29efc6e4 FM |
9287 | if ( index == wxNOT_FOUND ) |
9288 | { | |
9289 | return wxNOT_FOUND; | |
9290 | } | |
9291 | ||
9292 | wxGridCellRenderer *renderer = GetRenderer(index); | |
9293 | wxGridCellRenderer *rendererOld = renderer; | |
9294 | renderer = renderer->Clone(); | |
9295 | rendererOld->DecRef(); | |
9296 | ||
9297 | wxGridCellEditor *editor = GetEditor(index); | |
9298 | wxGridCellEditor *editorOld = editor; | |
9299 | editor = editor->Clone(); | |
9300 | editorOld->DecRef(); | |
9301 | ||
9302 | // do it even if there are no parameters to reset them to defaults | |
9a83f860 | 9303 | wxString params = typeName.AfterFirst(wxT(':')); |
29efc6e4 FM |
9304 | renderer->SetParameters(params); |
9305 | editor->SetParameters(params); | |
9306 | ||
9307 | // register the new typename | |
9308 | RegisterDataType(typeName, renderer, editor); | |
9309 | ||
9310 | // we just registered it, it's the last one | |
9311 | index = m_typeinfo.GetCount() - 1; | |
9312 | } | |
9313 | ||
9314 | return index; | |
9315 | } | |
9316 | ||
9317 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) | |
9318 | { | |
9319 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; | |
9320 | if (renderer) | |
9321 | renderer->IncRef(); | |
9322 | ||
9323 | return renderer; | |
9324 | } | |
9325 | ||
9326 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) | |
9327 | { | |
9328 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; | |
9329 | if (editor) | |
9330 | editor->IncRef(); | |
9331 | ||
9332 | return editor; | |
9333 | } | |
9334 | ||
27b92ca4 | 9335 | #endif // wxUSE_GRID |