]>
Commit | Line | Data |
---|---|---|
5612a518 | 1 | ///////////////////////////////////////////////////////////////////////////// |
f85afd4e MB |
2 | // Name: grid.cpp |
3 | // Purpose: wxGrid and related classes | |
4 | // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn) | |
5 | // Modified by: | |
6 | // Created: 1/08/1999 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
f85afd4e MB |
12 | #ifdef __GNUG__ |
13 | #pragma implementation "grid.h" | |
14 | #endif | |
15 | ||
4d85bcd1 JS |
16 | // For compilers that support precompilation, includes "wx/wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #include "wx/defs.h" | |
f85afd4e MB |
20 | |
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
8f177c8e | 25 | #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID) |
4d85bcd1 JS |
26 | #include "gridg.cpp" |
27 | #else | |
28 | ||
f85afd4e MB |
29 | #ifndef WX_PRECOMP |
30 | #include "wx/utils.h" | |
31 | #include "wx/dcclient.h" | |
32 | #include "wx/settings.h" | |
33 | #include "wx/log.h" | |
18f9565d | 34 | #include "wx/sizer.h" |
7807d81c | 35 | #include "wx/layout.h" |
f85afd4e MB |
36 | #endif |
37 | ||
38 | #include "wx/generic/grid.h" | |
39 | ||
9496deb5 MB |
40 | #ifndef WXGRID_DRAW_LINES |
41 | #define WXGRID_DRAW_LINES 1 | |
796df70a SN |
42 | #endif |
43 | ||
f85afd4e MB |
44 | ////////////////////////////////////////////////////////////////////// |
45 | ||
46 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); | |
47 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); | |
48 | ||
2d66e025 MB |
49 | // this is a magic incantation which must be done! |
50 | #include "wx/arrimpl.cpp" | |
51 | ||
52 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) | |
f85afd4e | 53 | |
f0102d2a VZ |
54 | // scroll line size |
55 | // TODO: fixed so far - make configurable later (and also different for x/y) | |
56 | static const size_t GRID_SCROLL_LINE = 10; | |
f85afd4e MB |
57 | |
58 | ////////////////////////////////////////////////////////////////////// | |
59 | // | |
60 | // Abstract base class for grid data (the model) | |
61 | // | |
62 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) | |
63 | ||
64 | ||
65 | wxGridTableBase::wxGridTableBase() | |
66 | : wxObject() | |
67 | { | |
68 | m_view = (wxGrid *) NULL; | |
69 | } | |
70 | ||
71 | wxGridTableBase::~wxGridTableBase() | |
72 | { | |
73 | } | |
74 | ||
75 | ||
76 | bool wxGridTableBase::InsertRows( size_t pos, size_t numRows ) | |
77 | { | |
6eec2bee OK |
78 | wxLogWarning( wxT("Called grid table class function InsertRows(pos=%d, N=%d)\n" |
79 | "but your derived table class does not override this function"), | |
f85afd4e | 80 | pos, numRows ); |
8f177c8e | 81 | |
f85afd4e MB |
82 | return FALSE; |
83 | } | |
84 | ||
85 | bool wxGridTableBase::AppendRows( size_t numRows ) | |
86 | { | |
6eec2bee OK |
87 | wxLogWarning( wxT("Called grid table class function AppendRows(N=%d)\n" |
88 | "but your derived table class does not override this function"), | |
f85afd4e | 89 | numRows ); |
8f177c8e | 90 | |
f85afd4e MB |
91 | return FALSE; |
92 | } | |
93 | ||
94 | bool wxGridTableBase::DeleteRows( size_t pos, size_t numRows ) | |
95 | { | |
6eec2bee OK |
96 | wxLogWarning( wxT("Called grid table class function DeleteRows(pos=%d, N=%d)\n" |
97 | "but your derived table class does not override this function"), | |
f85afd4e | 98 | pos, numRows ); |
8f177c8e | 99 | |
f85afd4e MB |
100 | return FALSE; |
101 | } | |
102 | ||
103 | bool wxGridTableBase::InsertCols( size_t pos, size_t numCols ) | |
104 | { | |
6eec2bee OK |
105 | wxLogWarning( wxT("Called grid table class function InsertCols(pos=%d, N=%d)\n" |
106 | "but your derived table class does not override this function"), | |
f85afd4e | 107 | pos, numCols ); |
8f177c8e | 108 | |
f85afd4e MB |
109 | return FALSE; |
110 | } | |
111 | ||
112 | bool wxGridTableBase::AppendCols( size_t numCols ) | |
113 | { | |
6eec2bee OK |
114 | wxLogWarning( wxT("Called grid table class function AppendCols(N=%d)\n" |
115 | "but your derived table class does not override this function"), | |
f85afd4e | 116 | numCols ); |
8f177c8e | 117 | |
f85afd4e MB |
118 | return FALSE; |
119 | } | |
120 | ||
121 | bool wxGridTableBase::DeleteCols( size_t pos, size_t numCols ) | |
122 | { | |
6eec2bee OK |
123 | wxLogWarning( wxT("Called grid table class function DeleteCols(pos=%d, N=%d)\n" |
124 | "but your derived table class does not override this function"), | |
f85afd4e | 125 | pos, numCols ); |
8f177c8e | 126 | |
f85afd4e MB |
127 | return FALSE; |
128 | } | |
129 | ||
130 | ||
131 | wxString wxGridTableBase::GetRowLabelValue( int row ) | |
132 | { | |
133 | wxString s; | |
134 | s << row; | |
135 | return s; | |
136 | } | |
137 | ||
138 | wxString wxGridTableBase::GetColLabelValue( int col ) | |
139 | { | |
140 | // default col labels are: | |
141 | // cols 0 to 25 : A-Z | |
142 | // cols 26 to 675 : AA-ZZ | |
143 | // etc. | |
144 | ||
145 | wxString s; | |
146 | unsigned int i, n; | |
147 | for ( n = 1; ; n++ ) | |
148 | { | |
f0102d2a | 149 | s += (_T('A') + (wxChar)( col%26 )); |
f85afd4e MB |
150 | col = col/26 - 1; |
151 | if ( col < 0 ) break; | |
152 | } | |
153 | ||
154 | // reverse the string... | |
155 | wxString s2; | |
156 | for ( i = 0; i < n; i++ ) | |
157 | { | |
158 | s2 += s[n-i-1]; | |
159 | } | |
160 | ||
161 | return s2; | |
162 | } | |
163 | ||
164 | ||
165 | ||
166 | ////////////////////////////////////////////////////////////////////// | |
167 | // | |
168 | // Message class for the grid table to send requests and notifications | |
169 | // to the grid view | |
170 | // | |
171 | ||
172 | wxGridTableMessage::wxGridTableMessage() | |
173 | { | |
174 | m_table = (wxGridTableBase *) NULL; | |
175 | m_id = -1; | |
176 | m_comInt1 = -1; | |
177 | m_comInt2 = -1; | |
178 | } | |
179 | ||
180 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, | |
181 | int commandInt1, int commandInt2 ) | |
182 | { | |
183 | m_table = table; | |
184 | m_id = id; | |
185 | m_comInt1 = commandInt1; | |
186 | m_comInt2 = commandInt2; | |
187 | } | |
188 | ||
189 | ||
190 | ||
191 | ////////////////////////////////////////////////////////////////////// | |
192 | // | |
193 | // A basic grid table for string data. An object of this class will | |
194 | // created by wxGrid if you don't specify an alternative table class. | |
195 | // | |
196 | ||
223d09f6 | 197 | WX_DEFINE_OBJARRAY(wxGridStringArray) |
f85afd4e MB |
198 | |
199 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) | |
200 | ||
201 | wxGridStringTable::wxGridStringTable() | |
202 | : wxGridTableBase() | |
203 | { | |
204 | } | |
205 | ||
206 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) | |
207 | : wxGridTableBase() | |
208 | { | |
209 | int row, col; | |
8f177c8e | 210 | |
f85afd4e MB |
211 | m_data.Alloc( numRows ); |
212 | ||
213 | wxArrayString sa; | |
214 | sa.Alloc( numCols ); | |
215 | for ( col = 0; col < numCols; col++ ) | |
216 | { | |
217 | sa.Add( wxEmptyString ); | |
218 | } | |
8f177c8e | 219 | |
f85afd4e MB |
220 | for ( row = 0; row < numRows; row++ ) |
221 | { | |
222 | m_data.Add( sa ); | |
223 | } | |
224 | } | |
225 | ||
226 | wxGridStringTable::~wxGridStringTable() | |
227 | { | |
228 | } | |
229 | ||
230 | long wxGridStringTable::GetNumberRows() | |
231 | { | |
232 | return m_data.GetCount(); | |
233 | } | |
234 | ||
235 | long wxGridStringTable::GetNumberCols() | |
236 | { | |
237 | if ( m_data.GetCount() > 0 ) | |
238 | return m_data[0].GetCount(); | |
239 | else | |
240 | return 0; | |
241 | } | |
242 | ||
243 | wxString wxGridStringTable::GetValue( int row, int col ) | |
244 | { | |
245 | // TODO: bounds checking | |
246 | // | |
247 | return m_data[row][col]; | |
248 | } | |
249 | ||
250 | void wxGridStringTable::SetValue( int row, int col, const wxString& s ) | |
251 | { | |
252 | // TODO: bounds checking | |
253 | // | |
254 | m_data[row][col] = s; | |
255 | } | |
256 | ||
257 | bool wxGridStringTable::IsEmptyCell( int row, int col ) | |
258 | { | |
259 | // TODO: bounds checking | |
260 | // | |
261 | return (m_data[row][col] == wxEmptyString); | |
262 | } | |
263 | ||
264 | ||
265 | void wxGridStringTable::Clear() | |
266 | { | |
267 | int row, col; | |
268 | int numRows, numCols; | |
8f177c8e | 269 | |
f85afd4e MB |
270 | numRows = m_data.GetCount(); |
271 | if ( numRows > 0 ) | |
272 | { | |
273 | numCols = m_data[0].GetCount(); | |
274 | ||
275 | for ( row = 0; row < numRows; row++ ) | |
276 | { | |
277 | for ( col = 0; col < numCols; col++ ) | |
278 | { | |
279 | m_data[row][col] = wxEmptyString; | |
280 | } | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | ||
286 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) | |
287 | { | |
288 | size_t row, col; | |
289 | ||
290 | size_t curNumRows = m_data.GetCount(); | |
291 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 ); | |
8f177c8e | 292 | |
f85afd4e MB |
293 | if ( pos >= curNumRows ) |
294 | { | |
295 | return AppendRows( numRows ); | |
296 | } | |
8f177c8e | 297 | |
f85afd4e MB |
298 | wxArrayString sa; |
299 | sa.Alloc( curNumCols ); | |
300 | for ( col = 0; col < curNumCols; col++ ) | |
301 | { | |
302 | sa.Add( wxEmptyString ); | |
303 | } | |
304 | ||
305 | for ( row = pos; row < pos + numRows; row++ ) | |
306 | { | |
307 | m_data.Insert( sa, row ); | |
308 | } | |
309 | ||
310 | if ( GetView() ) | |
311 | { | |
312 | wxGridTableMessage msg( this, | |
313 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, | |
314 | pos, | |
315 | numRows ); | |
8f177c8e | 316 | |
f85afd4e MB |
317 | GetView()->ProcessTableMessage( msg ); |
318 | } | |
319 | ||
320 | return TRUE; | |
321 | } | |
322 | ||
323 | bool wxGridStringTable::AppendRows( size_t numRows ) | |
324 | { | |
325 | size_t row, col; | |
326 | ||
327 | size_t curNumRows = m_data.GetCount(); | |
328 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 ); | |
8f177c8e | 329 | |
f85afd4e MB |
330 | wxArrayString sa; |
331 | if ( curNumCols > 0 ) | |
332 | { | |
333 | sa.Alloc( curNumCols ); | |
334 | for ( col = 0; col < curNumCols; col++ ) | |
335 | { | |
336 | sa.Add( wxEmptyString ); | |
337 | } | |
338 | } | |
8f177c8e | 339 | |
f85afd4e MB |
340 | for ( row = 0; row < numRows; row++ ) |
341 | { | |
342 | m_data.Add( sa ); | |
343 | } | |
344 | ||
345 | if ( GetView() ) | |
346 | { | |
347 | wxGridTableMessage msg( this, | |
348 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, | |
349 | numRows ); | |
8f177c8e | 350 | |
f85afd4e MB |
351 | GetView()->ProcessTableMessage( msg ); |
352 | } | |
353 | ||
8f177c8e | 354 | return TRUE; |
f85afd4e MB |
355 | } |
356 | ||
357 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) | |
358 | { | |
359 | size_t n; | |
360 | ||
361 | size_t curNumRows = m_data.GetCount(); | |
8f177c8e | 362 | |
f85afd4e MB |
363 | if ( pos >= curNumRows ) |
364 | { | |
6eec2bee OK |
365 | wxLogError( wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)...\n" |
366 | "Pos value is invalid for present table with %d rows"), | |
f85afd4e MB |
367 | pos, numRows, curNumRows ); |
368 | return FALSE; | |
369 | } | |
370 | ||
371 | if ( numRows > curNumRows - pos ) | |
372 | { | |
373 | numRows = curNumRows - pos; | |
374 | } | |
8f177c8e | 375 | |
f85afd4e MB |
376 | if ( numRows >= curNumRows ) |
377 | { | |
378 | m_data.Empty(); // don't release memory just yet | |
379 | } | |
380 | else | |
381 | { | |
382 | for ( n = 0; n < numRows; n++ ) | |
383 | { | |
384 | m_data.Remove( pos ); | |
385 | } | |
386 | } | |
387 | ||
388 | if ( GetView() ) | |
389 | { | |
390 | wxGridTableMessage msg( this, | |
391 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, | |
392 | pos, | |
393 | numRows ); | |
8f177c8e | 394 | |
f85afd4e MB |
395 | GetView()->ProcessTableMessage( msg ); |
396 | } | |
397 | ||
8f177c8e | 398 | return TRUE; |
f85afd4e MB |
399 | } |
400 | ||
401 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) | |
402 | { | |
403 | size_t row, col; | |
404 | ||
405 | size_t curNumRows = m_data.GetCount(); | |
406 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 ); | |
8f177c8e | 407 | |
f85afd4e MB |
408 | if ( pos >= curNumCols ) |
409 | { | |
410 | return AppendCols( numCols ); | |
411 | } | |
412 | ||
413 | for ( row = 0; row < curNumRows; row++ ) | |
414 | { | |
415 | for ( col = pos; col < pos + numCols; col++ ) | |
416 | { | |
417 | m_data[row].Insert( wxEmptyString, col ); | |
418 | } | |
419 | } | |
420 | ||
421 | if ( GetView() ) | |
422 | { | |
423 | wxGridTableMessage msg( this, | |
424 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, | |
425 | pos, | |
426 | numCols ); | |
8f177c8e | 427 | |
f85afd4e MB |
428 | GetView()->ProcessTableMessage( msg ); |
429 | } | |
430 | ||
431 | return TRUE; | |
432 | } | |
433 | ||
434 | bool wxGridStringTable::AppendCols( size_t numCols ) | |
435 | { | |
436 | size_t row, n; | |
437 | ||
438 | size_t curNumRows = m_data.GetCount(); | |
439 | if ( !curNumRows ) | |
440 | { | |
441 | // TODO: something better than this ? | |
442 | // | |
6eec2bee OK |
443 | wxLogError( wxT("Unable to append cols to a grid table with no rows.\n" |
444 | "Call AppendRows() first") ); | |
f85afd4e MB |
445 | return FALSE; |
446 | } | |
8f177c8e | 447 | |
f85afd4e MB |
448 | for ( row = 0; row < curNumRows; row++ ) |
449 | { | |
450 | for ( n = 0; n < numCols; n++ ) | |
451 | { | |
452 | m_data[row].Add( wxEmptyString ); | |
453 | } | |
454 | } | |
455 | ||
456 | if ( GetView() ) | |
457 | { | |
458 | wxGridTableMessage msg( this, | |
459 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, | |
460 | numCols ); | |
8f177c8e | 461 | |
f85afd4e MB |
462 | GetView()->ProcessTableMessage( msg ); |
463 | } | |
464 | ||
465 | return TRUE; | |
466 | } | |
467 | ||
468 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) | |
469 | { | |
470 | size_t row, n; | |
471 | ||
472 | size_t curNumRows = m_data.GetCount(); | |
473 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 ); | |
8f177c8e | 474 | |
f85afd4e MB |
475 | if ( pos >= curNumCols ) |
476 | { | |
8f177c8e VZ |
477 | wxLogError( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n" |
478 | "Pos value is invalid for present table with %d cols"), | |
479 | pos, numCols, curNumCols ); | |
480 | return FALSE; | |
f85afd4e MB |
481 | } |
482 | ||
483 | if ( numCols > curNumCols - pos ) | |
484 | { | |
8f177c8e | 485 | numCols = curNumCols - pos; |
f85afd4e MB |
486 | } |
487 | ||
488 | for ( row = 0; row < curNumRows; row++ ) | |
489 | { | |
490 | if ( numCols >= curNumCols ) | |
491 | { | |
492 | m_data[row].Clear(); | |
493 | } | |
494 | else | |
495 | { | |
496 | for ( n = 0; n < numCols; n++ ) | |
497 | { | |
498 | m_data[row].Remove( pos ); | |
499 | } | |
500 | } | |
501 | } | |
502 | ||
503 | if ( GetView() ) | |
504 | { | |
505 | wxGridTableMessage msg( this, | |
506 | wxGRIDTABLE_NOTIFY_COLS_DELETED, | |
507 | pos, | |
508 | numCols ); | |
8f177c8e | 509 | |
f85afd4e MB |
510 | GetView()->ProcessTableMessage( msg ); |
511 | } | |
512 | ||
8f177c8e | 513 | return TRUE; |
f85afd4e MB |
514 | } |
515 | ||
516 | wxString wxGridStringTable::GetRowLabelValue( int row ) | |
517 | { | |
518 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
519 | { | |
520 | // using default label | |
521 | // | |
522 | return wxGridTableBase::GetRowLabelValue( row ); | |
523 | } | |
524 | else | |
525 | { | |
526 | return m_rowLabels[ row ]; | |
527 | } | |
528 | } | |
529 | ||
530 | wxString wxGridStringTable::GetColLabelValue( int col ) | |
531 | { | |
532 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
533 | { | |
534 | // using default label | |
535 | // | |
536 | return wxGridTableBase::GetColLabelValue( col ); | |
537 | } | |
538 | else | |
539 | { | |
540 | return m_colLabels[ col ]; | |
541 | } | |
542 | } | |
543 | ||
544 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) | |
545 | { | |
546 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) | |
547 | { | |
548 | int n = m_rowLabels.GetCount(); | |
549 | int i; | |
550 | for ( i = n; i <= row; i++ ) | |
551 | { | |
552 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); | |
553 | } | |
554 | } | |
555 | ||
556 | m_rowLabels[row] = value; | |
557 | } | |
558 | ||
559 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) | |
560 | { | |
561 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) | |
562 | { | |
563 | int n = m_colLabels.GetCount(); | |
564 | int i; | |
565 | for ( i = n; i <= col; i++ ) | |
566 | { | |
567 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); | |
568 | } | |
569 | } | |
570 | ||
571 | m_colLabels[col] = value; | |
572 | } | |
573 | ||
574 | ||
575 | ||
576 | ||
577 | ////////////////////////////////////////////////////////////////////// | |
578 | ||
579 | IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl, wxTextCtrl ) | |
8f177c8e | 580 | |
f85afd4e MB |
581 | BEGIN_EVENT_TABLE( wxGridTextCtrl, wxTextCtrl ) |
582 | EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown ) | |
583 | END_EVENT_TABLE() | |
584 | ||
585 | ||
586 | wxGridTextCtrl::wxGridTextCtrl( wxWindow *par, | |
2d66e025 | 587 | wxGrid *grid, |
f85afd4e MB |
588 | bool isCellControl, |
589 | wxWindowID id, | |
590 | const wxString& value, | |
591 | const wxPoint& pos, | |
592 | const wxSize& size, | |
593 | long style ) | |
594 | : wxTextCtrl( par, id, value, pos, size, style ) | |
595 | { | |
2d66e025 | 596 | m_grid = grid; |
f85afd4e MB |
597 | m_isCellControl = isCellControl; |
598 | } | |
599 | ||
600 | ||
2d66e025 | 601 | void wxGridTextCtrl::OnKeyDown( wxKeyEvent& event ) |
f85afd4e | 602 | { |
2d66e025 | 603 | switch ( event.KeyCode() ) |
f85afd4e MB |
604 | { |
605 | case WXK_ESCAPE: | |
2d66e025 | 606 | m_grid->SetEditControlValue( startValue ); |
f85afd4e MB |
607 | SetInsertionPointEnd(); |
608 | break; | |
609 | ||
610 | case WXK_UP: | |
611 | case WXK_DOWN: | |
612 | case WXK_LEFT: | |
613 | case WXK_RIGHT: | |
edc2c290 MB |
614 | case WXK_PRIOR: |
615 | case WXK_NEXT: | |
f603012f | 616 | case WXK_SPACE: |
f85afd4e MB |
617 | if ( m_isCellControl ) |
618 | { | |
619 | // send the event to the parent grid, skipping the | |
620 | // event if nothing happens | |
621 | // | |
2d66e025 | 622 | event.Skip( m_grid->ProcessEvent( event ) ); |
f85afd4e MB |
623 | } |
624 | else | |
625 | { | |
626 | // default text control response within the top edit | |
627 | // control | |
628 | // | |
2d66e025 | 629 | event.Skip(); |
f85afd4e MB |
630 | } |
631 | break; | |
632 | ||
58dd5b3b MB |
633 | case WXK_RETURN: |
634 | if ( m_isCellControl ) | |
635 | { | |
636 | if ( !m_grid->ProcessEvent( event ) ) | |
637 | { | |
8a9a9d58 | 638 | #if defined(__WXMOTIF__) || defined(__WXGTK__) |
58dd5b3b MB |
639 | // wxMotif needs a little extra help... |
640 | // | |
641 | int pos = GetInsertionPoint(); | |
642 | wxString s( GetValue() ); | |
643 | s = s.Left(pos) + "\n" + s.Mid(pos); | |
644 | SetValue(s); | |
645 | SetInsertionPoint( pos ); | |
646 | #else | |
647 | // the other ports can handle a Return key press | |
648 | // | |
649 | event.Skip(); | |
f0102d2a | 650 | #endif |
58dd5b3b MB |
651 | } |
652 | } | |
653 | break; | |
654 | ||
f85afd4e MB |
655 | case WXK_HOME: |
656 | case WXK_END: | |
657 | if ( m_isCellControl ) | |
658 | { | |
659 | // send the event to the parent grid, skipping the | |
660 | // event if nothing happens | |
661 | // | |
2d66e025 | 662 | event.Skip( m_grid->ProcessEvent( event ) ); |
f85afd4e MB |
663 | } |
664 | else | |
665 | { | |
666 | // default text control response within the top edit | |
667 | // control | |
668 | // | |
2d66e025 | 669 | event.Skip(); |
f85afd4e MB |
670 | } |
671 | break; | |
8f177c8e | 672 | |
f85afd4e | 673 | default: |
2d66e025 | 674 | event.Skip(); |
f85afd4e MB |
675 | } |
676 | } | |
677 | ||
678 | void wxGridTextCtrl::SetStartValue( const wxString& s ) | |
679 | { | |
680 | startValue = s; | |
f0102d2a | 681 | wxTextCtrl::SetValue(s); |
f85afd4e MB |
682 | } |
683 | ||
684 | ||
2d66e025 MB |
685 | |
686 | ////////////////////////////////////////////////////////////////////// | |
687 | ||
688 | IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow ) | |
689 | ||
690 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow ) | |
691 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) | |
692 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) | |
693 | EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown ) | |
694 | END_EVENT_TABLE() | |
695 | ||
60ff3b99 VZ |
696 | wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent, |
697 | wxWindowID id, | |
2d66e025 MB |
698 | const wxPoint &pos, const wxSize &size ) |
699 | : wxWindow( parent, id, pos, size ) | |
700 | { | |
701 | m_owner = parent; | |
702 | } | |
703 | ||
704 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent &event ) | |
705 | { | |
706 | wxPaintDC dc(this); | |
707 | ||
708 | // NO - don't do this because it will set both the x and y origin | |
709 | // coords to match the parent scrolled window and we just want to | |
710 | // set the y coord - MB | |
711 | // | |
712 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 713 | |
790ad94f | 714 | int x, y; |
2d66e025 MB |
715 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
716 | dc.SetDeviceOrigin( 0, -y ); | |
60ff3b99 | 717 | |
2d66e025 MB |
718 | m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); |
719 | m_owner->DrawRowLabels( dc ); | |
720 | } | |
721 | ||
722 | ||
723 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
724 | { | |
725 | m_owner->ProcessRowLabelMouseEvent( event ); | |
726 | } | |
727 | ||
728 | ||
729 | // This seems to be required for wxMotif otherwise the mouse | |
730 | // cursor must be in the cell edit control to get key events | |
731 | // | |
732 | void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
733 | { | |
734 | if ( !m_owner->ProcessEvent( event ) ) event.Skip(); | |
735 | } | |
736 | ||
737 | ||
738 | ||
739 | ////////////////////////////////////////////////////////////////////// | |
740 | ||
741 | IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow ) | |
742 | ||
743 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow ) | |
744 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) | |
745 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) | |
746 | EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown ) | |
747 | END_EVENT_TABLE() | |
748 | ||
60ff3b99 VZ |
749 | wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent, |
750 | wxWindowID id, | |
2d66e025 MB |
751 | const wxPoint &pos, const wxSize &size ) |
752 | : wxWindow( parent, id, pos, size ) | |
753 | { | |
754 | m_owner = parent; | |
755 | } | |
756 | ||
757 | void wxGridColLabelWindow::OnPaint( wxPaintEvent &event ) | |
758 | { | |
759 | wxPaintDC dc(this); | |
760 | ||
761 | // NO - don't do this because it will set both the x and y origin | |
762 | // coords to match the parent scrolled window and we just want to | |
763 | // set the x coord - MB | |
764 | // | |
765 | // m_owner->PrepareDC( dc ); | |
60ff3b99 | 766 | |
790ad94f | 767 | int x, y; |
2d66e025 MB |
768 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
769 | dc.SetDeviceOrigin( -x, 0 ); | |
770 | ||
60ff3b99 VZ |
771 | m_owner->CalcColLabelsExposed( GetUpdateRegion() ); |
772 | m_owner->DrawColLabels( dc ); | |
2d66e025 MB |
773 | } |
774 | ||
775 | ||
776 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
777 | { | |
778 | m_owner->ProcessColLabelMouseEvent( event ); | |
779 | } | |
780 | ||
781 | ||
782 | // This seems to be required for wxMotif otherwise the mouse | |
783 | // cursor must be in the cell edit control to get key events | |
784 | // | |
785 | void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
786 | { | |
787 | if ( !m_owner->ProcessEvent( event ) ) event.Skip(); | |
788 | } | |
789 | ||
790 | ||
791 | ||
792 | ////////////////////////////////////////////////////////////////////// | |
793 | ||
794 | IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow ) | |
795 | ||
796 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow ) | |
797 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) | |
d2fdd8d2 | 798 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint) |
2d66e025 MB |
799 | EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown ) |
800 | END_EVENT_TABLE() | |
801 | ||
60ff3b99 VZ |
802 | wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent, |
803 | wxWindowID id, | |
2d66e025 | 804 | const wxPoint &pos, const wxSize &size ) |
d2fdd8d2 | 805 | : wxWindow( parent, id, pos, size ) |
2d66e025 MB |
806 | { |
807 | m_owner = parent; | |
808 | } | |
809 | ||
d2fdd8d2 RR |
810 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
811 | { | |
812 | wxPaintDC dc(this); | |
813 | ||
814 | int client_height = 0; | |
815 | int client_width = 0; | |
816 | GetClientSize( &client_width, &client_height ); | |
817 | ||
818 | dc.SetPen( *wxBLACK_PEN ); | |
819 | dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 ); | |
820 | dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 ); | |
821 | ||
822 | dc.SetPen( *wxWHITE_PEN ); | |
823 | dc.DrawLine( 0, 0, client_width, 0 ); | |
824 | dc.DrawLine( 0, 0, 0, client_height ); | |
825 | } | |
826 | ||
2d66e025 MB |
827 | |
828 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) | |
829 | { | |
830 | m_owner->ProcessCornerLabelMouseEvent( event ); | |
831 | } | |
832 | ||
833 | ||
834 | // This seems to be required for wxMotif otherwise the mouse | |
835 | // cursor must be in the cell edit control to get key events | |
836 | // | |
837 | void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event ) | |
838 | { | |
839 | if ( !m_owner->ProcessEvent( event ) ) event.Skip(); | |
840 | } | |
841 | ||
842 | ||
843 | ||
f85afd4e MB |
844 | ////////////////////////////////////////////////////////////////////// |
845 | ||
2d66e025 MB |
846 | IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxPanel ) |
847 | ||
848 | BEGIN_EVENT_TABLE( wxGridWindow, wxPanel ) | |
849 | EVT_PAINT( wxGridWindow::OnPaint ) | |
2d66e025 MB |
850 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) |
851 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) | |
852 | END_EVENT_TABLE() | |
853 | ||
60ff3b99 VZ |
854 | wxGridWindow::wxGridWindow( wxGrid *parent, |
855 | wxGridRowLabelWindow *rowLblWin, | |
2d66e025 MB |
856 | wxGridColLabelWindow *colLblWin, |
857 | wxWindowID id, const wxPoint &pos, const wxSize &size ) | |
b0d6ff2f | 858 | : wxPanel( parent, id, pos, size, 0, "grid window" ) |
2d66e025 MB |
859 | { |
860 | m_owner = parent; | |
861 | m_rowLabelWin = rowLblWin; | |
862 | m_colLabelWin = colLblWin; | |
60ff3b99 | 863 | |
2d66e025 MB |
864 | SetBackgroundColour( "WHITE" ); |
865 | } | |
866 | ||
867 | ||
868 | wxGridWindow::~wxGridWindow() | |
869 | { | |
870 | } | |
871 | ||
872 | ||
873 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
874 | { | |
875 | wxPaintDC dc( this ); | |
876 | m_owner->PrepareDC( dc ); | |
796df70a SN |
877 | wxRegion reg = GetUpdateRegion(); |
878 | m_owner->CalcCellsExposed( reg ); | |
2d66e025 | 879 | m_owner->DrawGridCellArea( dc ); |
9496deb5 | 880 | #if WXGRID_DRAW_LINES |
796df70a SN |
881 | m_owner->DrawAllGridLines( dc, reg ); |
882 | #endif | |
2d66e025 MB |
883 | } |
884 | ||
885 | ||
886 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
887 | { | |
888 | wxPanel::ScrollWindow( dx, dy, rect ); | |
889 | m_rowLabelWin->ScrollWindow( 0, dy, rect ); | |
890 | m_colLabelWin->ScrollWindow( dx, 0, rect ); | |
891 | } | |
892 | ||
893 | ||
894 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) | |
895 | { | |
896 | m_owner->ProcessGridCellMouseEvent( event ); | |
897 | } | |
898 | ||
899 | ||
900 | // This seems to be required for wxMotif otherwise the mouse | |
901 | // cursor must be in the cell edit control to get key events | |
902 | // | |
903 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) | |
904 | { | |
905 | if ( !m_owner->ProcessEvent( event ) ) event.Skip(); | |
906 | } | |
f85afd4e | 907 | |
8f177c8e | 908 | |
2d66e025 MB |
909 | |
910 | ////////////////////////////////////////////////////////////////////// | |
911 | ||
912 | IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow ) | |
913 | ||
914 | BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow ) | |
f85afd4e MB |
915 | EVT_PAINT( wxGrid::OnPaint ) |
916 | EVT_SIZE( wxGrid::OnSize ) | |
f85afd4e | 917 | EVT_KEY_DOWN( wxGrid::OnKeyDown ) |
f85afd4e | 918 | END_EVENT_TABLE() |
8f177c8e | 919 | |
2d66e025 MB |
920 | wxGrid::wxGrid( wxWindow *parent, |
921 | wxWindowID id, | |
922 | const wxPoint& pos, | |
923 | const wxSize& size, | |
924 | long style, | |
925 | const wxString& name ) | |
926 | : wxScrolledWindow( parent, id, pos, size, style, name ) | |
927 | { | |
928 | Create(); | |
58dd5b3b MB |
929 | } |
930 | ||
931 | ||
932 | wxGrid::~wxGrid() | |
933 | { | |
934 | delete m_table; | |
935 | } | |
936 | ||
2d66e025 | 937 | |
58dd5b3b MB |
938 | // |
939 | // ----- internal init and update functions | |
940 | // | |
941 | ||
942 | void wxGrid::Create() | |
f0102d2a | 943 | { |
4634a5d6 MB |
944 | m_created = FALSE; // set to TRUE by CreateGrid |
945 | m_displayed = FALSE; // set to TRUE by OnPaint | |
946 | ||
947 | m_table = (wxGridTableBase *) NULL; | |
948 | m_cellEditCtrl = (wxWindow *) NULL; | |
949 | ||
950 | m_numRows = 0; | |
951 | m_numCols = 0; | |
952 | m_currentCellCoords = wxGridNoCellCoords; | |
953 | ||
18f9565d MB |
954 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; |
955 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
2d66e025 | 956 | |
7807d81c MB |
957 | m_cornerLabelWin = new wxGridCornerLabelWindow( this, |
958 | -1, | |
18f9565d MB |
959 | wxDefaultPosition, |
960 | wxDefaultSize ); | |
7807d81c | 961 | |
60ff3b99 VZ |
962 | m_rowLabelWin = new wxGridRowLabelWindow( this, |
963 | -1, | |
18f9565d MB |
964 | wxDefaultPosition, |
965 | wxDefaultSize ); | |
2d66e025 MB |
966 | |
967 | m_colLabelWin = new wxGridColLabelWindow( this, | |
968 | -1, | |
18f9565d MB |
969 | wxDefaultPosition, |
970 | wxDefaultSize ); | |
60ff3b99 | 971 | |
60ff3b99 VZ |
972 | m_gridWin = new wxGridWindow( this, |
973 | m_rowLabelWin, | |
974 | m_colLabelWin, | |
975 | -1, | |
18f9565d | 976 | wxDefaultPosition, |
2d66e025 MB |
977 | wxDefaultSize ); |
978 | ||
979 | SetTargetWindow( m_gridWin ); | |
2d66e025 | 980 | } |
f85afd4e | 981 | |
2d66e025 MB |
982 | |
983 | bool wxGrid::CreateGrid( int numRows, int numCols ) | |
984 | { | |
985 | if ( m_created ) | |
986 | { | |
987 | wxLogError( wxT("wxGrid::CreateGrid(numRows, numCols) called more than once") ); | |
988 | return FALSE; | |
989 | } | |
990 | else | |
991 | { | |
992 | m_numRows = numRows; | |
993 | m_numCols = numCols; | |
994 | ||
995 | m_table = new wxGridStringTable( m_numRows, m_numCols ); | |
996 | m_table->SetView( this ); | |
997 | Init(); | |
998 | m_created = TRUE; | |
999 | } | |
f85afd4e | 1000 | |
2d66e025 | 1001 | return m_created; |
f85afd4e MB |
1002 | } |
1003 | ||
2d66e025 | 1004 | |
f85afd4e MB |
1005 | void wxGrid::Init() |
1006 | { | |
1007 | int i; | |
1008 | ||
f85afd4e MB |
1009 | if ( m_numRows <= 0 ) |
1010 | m_numRows = WXGRID_DEFAULT_NUMBER_ROWS; | |
1011 | ||
1012 | if ( m_numCols <= 0 ) | |
1013 | m_numCols = WXGRID_DEFAULT_NUMBER_COLS; | |
1014 | ||
1015 | m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH; | |
1016 | m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT; | |
1017 | ||
60ff3b99 VZ |
1018 | if ( m_rowLabelWin ) |
1019 | { | |
1020 | m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour(); | |
1021 | } | |
1022 | else | |
1023 | { | |
1024 | m_labelBackgroundColour = wxColour( _T("WHITE") ); | |
1025 | } | |
1026 | ||
1027 | m_labelTextColour = wxColour( _T("BLACK") ); | |
f85afd4e MB |
1028 | |
1029 | // TODO: something better than this ? | |
1030 | // | |
1031 | m_labelFont = this->GetFont(); | |
1032 | m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 ); | |
8f177c8e | 1033 | |
f85afd4e MB |
1034 | m_rowLabelHorizAlign = wxLEFT; |
1035 | m_rowLabelVertAlign = wxCENTRE; | |
1036 | ||
1037 | m_colLabelHorizAlign = wxCENTRE; | |
1038 | m_colLabelVertAlign = wxTOP; | |
1039 | ||
f85afd4e | 1040 | m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH; |
1f1ce288 MB |
1041 | m_defaultRowHeight = m_gridWin->GetCharHeight(); |
1042 | ||
d2fdd8d2 | 1043 | #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl() |
1f1ce288 MB |
1044 | m_defaultRowHeight += 8; |
1045 | #else | |
1046 | m_defaultRowHeight += 4; | |
1047 | #endif | |
1048 | ||
f85afd4e MB |
1049 | m_rowHeights.Alloc( m_numRows ); |
1050 | m_rowBottoms.Alloc( m_numRows ); | |
2d66e025 | 1051 | int rowBottom = 0; |
f85afd4e MB |
1052 | for ( i = 0; i < m_numRows; i++ ) |
1053 | { | |
1054 | m_rowHeights.Add( m_defaultRowHeight ); | |
2d66e025 MB |
1055 | rowBottom += m_defaultRowHeight; |
1056 | m_rowBottoms.Add( rowBottom ); | |
f85afd4e | 1057 | } |
8f177c8e | 1058 | |
f85afd4e | 1059 | m_colWidths.Alloc( m_numCols ); |
2d66e025 MB |
1060 | m_colRights.Alloc( m_numCols ); |
1061 | int colRight = 0; | |
f85afd4e MB |
1062 | for ( i = 0; i < m_numCols; i++ ) |
1063 | { | |
1064 | m_colWidths.Add( m_defaultColWidth ); | |
2d66e025 MB |
1065 | colRight += m_defaultColWidth; |
1066 | m_colRights.Add( colRight ); | |
f85afd4e | 1067 | } |
8f177c8e | 1068 | |
f85afd4e MB |
1069 | // TODO: improve this ? |
1070 | // | |
1071 | m_defaultCellFont = this->GetFont(); | |
8f177c8e | 1072 | |
2d66e025 | 1073 | m_gridLineColour = wxColour( 128, 128, 255 ); |
f85afd4e | 1074 | m_gridLinesEnabled = TRUE; |
8f177c8e | 1075 | |
58dd5b3b | 1076 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
f85afd4e MB |
1077 | m_dragLastPos = -1; |
1078 | m_dragRowOrCol = -1; | |
1079 | m_isDragging = FALSE; | |
1080 | ||
1081 | m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS ); | |
1082 | m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE ); | |
1083 | ||
1084 | m_currentCellCoords = wxGridNoCellCoords; | |
f85afd4e MB |
1085 | |
1086 | m_selectedTopLeft = wxGridNoCellCoords; | |
1087 | m_selectedBottomRight = wxGridNoCellCoords; | |
8f177c8e | 1088 | |
f85afd4e MB |
1089 | m_editable = TRUE; // default for whole grid |
1090 | ||
2d66e025 MB |
1091 | m_inOnKeyDown = FALSE; |
1092 | m_batchCount = 0; | |
1093 | ||
f85afd4e MB |
1094 | // TODO: extend this to other types of controls |
1095 | // | |
2d66e025 MB |
1096 | m_cellEditCtrl = new wxGridTextCtrl( m_gridWin, |
1097 | this, | |
f85afd4e MB |
1098 | TRUE, |
1099 | wxGRID_CELLCTRL, | |
1100 | "", | |
1101 | wxPoint(1,1), | |
edc2c290 | 1102 | wxSize(1,1) |
d2fdd8d2 | 1103 | #if defined(__WXMSW__) |
edc2c290 | 1104 | , wxTE_MULTILINE | wxTE_NO_VSCROLL |
f85afd4e MB |
1105 | #endif |
1106 | ); | |
8f177c8e VZ |
1107 | |
1108 | m_cellEditCtrl->Show( FALSE ); | |
f85afd4e | 1109 | m_cellEditCtrlEnabled = TRUE; |
8f177c8e | 1110 | m_editCtrlType = wxGRID_TEXTCTRL; |
f85afd4e MB |
1111 | } |
1112 | ||
1113 | ||
1114 | void wxGrid::CalcDimensions() | |
1115 | { | |
f85afd4e | 1116 | int cw, ch; |
2d66e025 | 1117 | GetClientSize( &cw, &ch ); |
f85afd4e | 1118 | |
2d66e025 | 1119 | if ( m_numRows > 0 && m_numCols > 0 ) |
f85afd4e | 1120 | { |
b0d6ff2f MB |
1121 | int right = m_colRights[ m_numCols-1 ] + 50; |
1122 | int bottom = m_rowBottoms[ m_numRows-1 ] + 50; | |
60ff3b99 | 1123 | |
2d66e025 MB |
1124 | // TODO: restore the scroll position that we had before sizing |
1125 | // | |
1126 | int x, y; | |
1127 | GetViewStart( &x, &y ); | |
f0102d2a VZ |
1128 | SetScrollbars( GRID_SCROLL_LINE, GRID_SCROLL_LINE, |
1129 | right/GRID_SCROLL_LINE, bottom/GRID_SCROLL_LINE, | |
be46e4a6 | 1130 | x, y ); |
f85afd4e | 1131 | } |
f85afd4e MB |
1132 | } |
1133 | ||
1134 | ||
7807d81c MB |
1135 | void wxGrid::CalcWindowSizes() |
1136 | { | |
1137 | int cw, ch; | |
1138 | GetClientSize( &cw, &ch ); | |
1139 | ||
1140 | if ( m_cornerLabelWin->IsShown() ) | |
1141 | m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight ); | |
1142 | ||
1143 | if ( m_colLabelWin->IsShown() ) | |
1144 | m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight); | |
1145 | ||
1146 | if ( m_rowLabelWin->IsShown() ) | |
1147 | m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight); | |
1148 | ||
1149 | if ( m_gridWin->IsShown() ) | |
1150 | m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight); | |
1151 | } | |
1152 | ||
1153 | ||
f85afd4e MB |
1154 | // this is called when the grid table sends a message to say that it |
1155 | // has been redimensioned | |
1156 | // | |
1157 | bool wxGrid::Redimension( wxGridTableMessage& msg ) | |
1158 | { | |
1159 | int i; | |
8f177c8e | 1160 | |
f85afd4e MB |
1161 | switch ( msg.GetId() ) |
1162 | { | |
1163 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: | |
1164 | { | |
1165 | size_t pos = msg.GetCommandInt(); | |
1166 | int numRows = msg.GetCommandInt2(); | |
1167 | for ( i = 0; i < numRows; i++ ) | |
1168 | { | |
1169 | m_rowHeights.Insert( m_defaultRowHeight, pos ); | |
1170 | m_rowBottoms.Insert( 0, pos ); | |
1171 | } | |
1172 | m_numRows += numRows; | |
2d66e025 MB |
1173 | |
1174 | int bottom = 0; | |
1175 | if ( pos > 0 ) bottom = m_rowBottoms[pos-1]; | |
60ff3b99 | 1176 | |
2d66e025 MB |
1177 | for ( i = pos; i < m_numRows; i++ ) |
1178 | { | |
1179 | bottom += m_rowHeights[i]; | |
1180 | m_rowBottoms[i] = bottom; | |
1181 | } | |
f85afd4e MB |
1182 | CalcDimensions(); |
1183 | } | |
1184 | return TRUE; | |
1185 | ||
1186 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
1187 | { | |
1188 | int numRows = msg.GetCommandInt(); | |
1189 | for ( i = 0; i < numRows; i++ ) | |
1190 | { | |
1191 | m_rowHeights.Add( m_defaultRowHeight ); | |
1192 | m_rowBottoms.Add( 0 ); | |
1193 | } | |
2d66e025 MB |
1194 | |
1195 | int oldNumRows = m_numRows; | |
f85afd4e | 1196 | m_numRows += numRows; |
2d66e025 MB |
1197 | |
1198 | int bottom = 0; | |
1199 | if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1]; | |
60ff3b99 | 1200 | |
2d66e025 MB |
1201 | for ( i = oldNumRows; i < m_numRows; i++ ) |
1202 | { | |
1203 | bottom += m_rowHeights[i]; | |
1204 | m_rowBottoms[i] = bottom; | |
1205 | } | |
f85afd4e MB |
1206 | CalcDimensions(); |
1207 | } | |
1208 | return TRUE; | |
1209 | ||
1210 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
1211 | { | |
1212 | size_t pos = msg.GetCommandInt(); | |
1213 | int numRows = msg.GetCommandInt2(); | |
1214 | for ( i = 0; i < numRows; i++ ) | |
1215 | { | |
1216 | m_rowHeights.Remove( pos ); | |
1217 | m_rowBottoms.Remove( pos ); | |
1218 | } | |
1219 | m_numRows -= numRows; | |
1220 | ||
f85afd4e MB |
1221 | if ( !m_numRows ) |
1222 | { | |
1223 | m_numCols = 0; | |
1224 | m_colWidths.Clear(); | |
1225 | m_colRights.Clear(); | |
1226 | m_currentCellCoords = wxGridNoCellCoords; | |
1227 | } | |
2d66e025 | 1228 | else |
f85afd4e | 1229 | { |
2d66e025 MB |
1230 | if ( m_currentCellCoords.GetRow() >= m_numRows ) |
1231 | m_currentCellCoords.Set( 0, 0 ); | |
1232 | ||
1233 | int h = 0; | |
1234 | for ( i = 0; i < m_numRows; i++ ) | |
1235 | { | |
1236 | h += m_rowHeights[i]; | |
1237 | m_rowBottoms[i] = h; | |
1238 | } | |
f85afd4e | 1239 | } |
60ff3b99 | 1240 | |
f85afd4e MB |
1241 | CalcDimensions(); |
1242 | } | |
1243 | return TRUE; | |
1244 | ||
1245 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
1246 | { | |
1247 | size_t pos = msg.GetCommandInt(); | |
1248 | int numCols = msg.GetCommandInt2(); | |
1249 | for ( i = 0; i < numCols; i++ ) | |
1250 | { | |
1251 | m_colWidths.Insert( m_defaultColWidth, pos ); | |
1252 | m_colRights.Insert( 0, pos ); | |
1253 | } | |
1254 | m_numCols += numCols; | |
2d66e025 MB |
1255 | |
1256 | int right = 0; | |
1257 | if ( pos > 0 ) right = m_colRights[pos-1]; | |
60ff3b99 | 1258 | |
2d66e025 MB |
1259 | for ( i = pos; i < m_numCols; i++ ) |
1260 | { | |
1261 | right += m_colWidths[i]; | |
1262 | m_colRights[i] = right; | |
1263 | } | |
f85afd4e MB |
1264 | CalcDimensions(); |
1265 | } | |
1266 | return TRUE; | |
1267 | ||
1268 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
1269 | { | |
1270 | int numCols = msg.GetCommandInt(); | |
1271 | for ( i = 0; i < numCols; i++ ) | |
1272 | { | |
1273 | m_colWidths.Add( m_defaultColWidth ); | |
1274 | m_colRights.Add( 0 ); | |
1275 | } | |
2d66e025 MB |
1276 | |
1277 | int oldNumCols = m_numCols; | |
f85afd4e | 1278 | m_numCols += numCols; |
2d66e025 MB |
1279 | |
1280 | int right = 0; | |
1281 | if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1]; | |
60ff3b99 | 1282 | |
2d66e025 MB |
1283 | for ( i = oldNumCols; i < m_numCols; i++ ) |
1284 | { | |
1285 | right += m_colWidths[i]; | |
1286 | m_colRights[i] = right; | |
1287 | } | |
f85afd4e MB |
1288 | CalcDimensions(); |
1289 | } | |
1290 | return TRUE; | |
1291 | ||
1292 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
1293 | { | |
1294 | size_t pos = msg.GetCommandInt(); | |
1295 | int numCols = msg.GetCommandInt2(); | |
1296 | for ( i = 0; i < numCols; i++ ) | |
1297 | { | |
1298 | m_colWidths.Remove( pos ); | |
1299 | m_colRights.Remove( pos ); | |
1300 | } | |
1301 | m_numCols -= numCols; | |
f85afd4e MB |
1302 | |
1303 | if ( !m_numCols ) | |
1304 | { | |
1305 | #if 0 // leave the row alone here so that AppendCols will work subsequently | |
1306 | m_numRows = 0; | |
1307 | m_rowHeights.Clear(); | |
1308 | m_rowBottoms.Clear(); | |
8f177c8e | 1309 | #endif |
f85afd4e MB |
1310 | m_currentCellCoords = wxGridNoCellCoords; |
1311 | } | |
60ff3b99 | 1312 | else |
f85afd4e | 1313 | { |
2d66e025 MB |
1314 | if ( m_currentCellCoords.GetCol() >= m_numCols ) |
1315 | m_currentCellCoords.Set( 0, 0 ); | |
1316 | ||
1317 | int w = 0; | |
1318 | for ( i = 0; i < m_numCols; i++ ) | |
1319 | { | |
1320 | w += m_colWidths[i]; | |
1321 | m_colRights[i] = w; | |
1322 | } | |
f85afd4e MB |
1323 | } |
1324 | CalcDimensions(); | |
1325 | } | |
1326 | return TRUE; | |
1327 | } | |
1328 | ||
1329 | return FALSE; | |
1330 | } | |
1331 | ||
1332 | ||
2d66e025 | 1333 | void wxGrid::CalcRowLabelsExposed( wxRegion& reg ) |
f85afd4e | 1334 | { |
2d66e025 MB |
1335 | wxRegionIterator iter( reg ); |
1336 | wxRect r; | |
f85afd4e | 1337 | |
2d66e025 | 1338 | m_rowLabelsExposed.Empty(); |
f85afd4e | 1339 | |
2d66e025 MB |
1340 | int top, bottom; |
1341 | while ( iter ) | |
f85afd4e | 1342 | { |
2d66e025 | 1343 | r = iter.GetRect(); |
f85afd4e | 1344 | |
2d66e025 MB |
1345 | // TODO: remove this when we can... |
1346 | // There is a bug in wxMotif that gives garbage update | |
1347 | // rectangles if you jump-scroll a long way by clicking the | |
1348 | // scrollbar with middle button. This is a work-around | |
1349 | // | |
1350 | #if defined(__WXMOTIF__) | |
1351 | int cw, ch; | |
1352 | m_gridWin->GetClientSize( &cw, &ch ); | |
1353 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
1354 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
1355 | #endif | |
f85afd4e | 1356 | |
2d66e025 MB |
1357 | // logical bounds of update region |
1358 | // | |
1359 | int dummy; | |
1360 | CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top ); | |
1361 | CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom ); | |
1362 | ||
1363 | // find the row labels within these bounds | |
1364 | // | |
1365 | int row; | |
1366 | int rowTop; | |
1367 | for ( row = 0; row < m_numRows; row++ ) | |
1368 | { | |
1369 | if ( m_rowBottoms[row] < top ) continue; | |
1370 | ||
1371 | rowTop = m_rowBottoms[row] - m_rowHeights[row]; | |
1372 | if ( rowTop > bottom ) break; | |
60ff3b99 | 1373 | |
2d66e025 MB |
1374 | m_rowLabelsExposed.Add( row ); |
1375 | } | |
60ff3b99 | 1376 | |
2d66e025 | 1377 | iter++ ; |
f85afd4e MB |
1378 | } |
1379 | } | |
1380 | ||
1381 | ||
2d66e025 | 1382 | void wxGrid::CalcColLabelsExposed( wxRegion& reg ) |
f85afd4e | 1383 | { |
2d66e025 MB |
1384 | wxRegionIterator iter( reg ); |
1385 | wxRect r; | |
f85afd4e | 1386 | |
2d66e025 | 1387 | m_colLabelsExposed.Empty(); |
f85afd4e | 1388 | |
2d66e025 MB |
1389 | int left, right; |
1390 | while ( iter ) | |
f85afd4e | 1391 | { |
2d66e025 | 1392 | r = iter.GetRect(); |
f85afd4e | 1393 | |
2d66e025 MB |
1394 | // TODO: remove this when we can... |
1395 | // There is a bug in wxMotif that gives garbage update | |
1396 | // rectangles if you jump-scroll a long way by clicking the | |
1397 | // scrollbar with middle button. This is a work-around | |
1398 | // | |
1399 | #if defined(__WXMOTIF__) | |
1400 | int cw, ch; | |
1401 | m_gridWin->GetClientSize( &cw, &ch ); | |
1402 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
1403 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
1404 | #endif | |
1405 | ||
1406 | // logical bounds of update region | |
1407 | // | |
1408 | int dummy; | |
1409 | CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy ); | |
1410 | CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy ); | |
1411 | ||
1412 | // find the cells within these bounds | |
1413 | // | |
1414 | int col; | |
1415 | int colLeft; | |
1416 | for ( col = 0; col < m_numCols; col++ ) | |
1417 | { | |
1418 | if ( m_colRights[col] < left ) continue; | |
60ff3b99 | 1419 | |
2d66e025 MB |
1420 | colLeft = m_colRights[col] - m_colWidths[col]; |
1421 | if ( colLeft > right ) break; | |
1422 | ||
1423 | m_colLabelsExposed.Add( col ); | |
1424 | } | |
60ff3b99 | 1425 | |
2d66e025 | 1426 | iter++ ; |
f85afd4e MB |
1427 | } |
1428 | } | |
1429 | ||
1430 | ||
2d66e025 | 1431 | void wxGrid::CalcCellsExposed( wxRegion& reg ) |
f85afd4e | 1432 | { |
2d66e025 MB |
1433 | wxRegionIterator iter( reg ); |
1434 | wxRect r; | |
f85afd4e | 1435 | |
2d66e025 MB |
1436 | m_cellsExposed.Empty(); |
1437 | m_rowsExposed.Empty(); | |
1438 | m_colsExposed.Empty(); | |
f85afd4e | 1439 | |
2d66e025 MB |
1440 | int left, top, right, bottom; |
1441 | while ( iter ) | |
1442 | { | |
1443 | r = iter.GetRect(); | |
f85afd4e | 1444 | |
2d66e025 MB |
1445 | // TODO: remove this when we can... |
1446 | // There is a bug in wxMotif that gives garbage update | |
1447 | // rectangles if you jump-scroll a long way by clicking the | |
1448 | // scrollbar with middle button. This is a work-around | |
1449 | // | |
1450 | #if defined(__WXMOTIF__) | |
f85afd4e | 1451 | int cw, ch; |
2d66e025 MB |
1452 | m_gridWin->GetClientSize( &cw, &ch ); |
1453 | if ( r.GetTop() > ch ) r.SetTop( 0 ); | |
1454 | if ( r.GetLeft() > cw ) r.SetLeft( 0 ); | |
1455 | r.SetRight( wxMin( r.GetRight(), cw ) ); | |
1456 | r.SetBottom( wxMin( r.GetBottom(), ch ) ); | |
1457 | #endif | |
8f177c8e | 1458 | |
2d66e025 MB |
1459 | // logical bounds of update region |
1460 | // | |
1461 | CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
1462 | CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
f85afd4e | 1463 | |
2d66e025 | 1464 | // find the cells within these bounds |
f85afd4e | 1465 | // |
2d66e025 MB |
1466 | int row, col; |
1467 | int colLeft, rowTop; | |
1468 | for ( row = 0; row < m_numRows; row++ ) | |
f85afd4e | 1469 | { |
2d66e025 | 1470 | if ( m_rowBottoms[row] < top ) continue; |
f85afd4e | 1471 | |
2d66e025 MB |
1472 | rowTop = m_rowBottoms[row] - m_rowHeights[row]; |
1473 | if ( rowTop > bottom ) break; | |
60ff3b99 | 1474 | |
2d66e025 | 1475 | m_rowsExposed.Add( row ); |
f85afd4e | 1476 | |
2d66e025 MB |
1477 | for ( col = 0; col < m_numCols; col++ ) |
1478 | { | |
1479 | if ( m_colRights[col] < left ) continue; | |
60ff3b99 | 1480 | |
2d66e025 MB |
1481 | colLeft = m_colRights[col] - m_colWidths[col]; |
1482 | if ( colLeft > right ) break; | |
60ff3b99 | 1483 | |
2d66e025 MB |
1484 | if ( m_colsExposed.Index( col ) == wxNOT_FOUND ) m_colsExposed.Add( col ); |
1485 | m_cellsExposed.Add( wxGridCellCoords( row, col ) ); | |
1486 | } | |
1487 | } | |
60ff3b99 | 1488 | |
2d66e025 | 1489 | iter++ ; |
f85afd4e MB |
1490 | } |
1491 | } | |
1492 | ||
1493 | ||
2d66e025 | 1494 | void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 1495 | { |
2d66e025 MB |
1496 | int x, y, row; |
1497 | wxPoint pos( event.GetPosition() ); | |
1498 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 1499 | |
2d66e025 | 1500 | if ( event.Dragging() ) |
f85afd4e MB |
1501 | { |
1502 | m_isDragging = TRUE; | |
8f177c8e | 1503 | |
2d66e025 | 1504 | if ( event.LeftIsDown() ) |
f85afd4e MB |
1505 | { |
1506 | switch( m_cursorMode ) | |
1507 | { | |
f85afd4e MB |
1508 | case WXGRID_CURSOR_RESIZE_ROW: |
1509 | { | |
2d66e025 MB |
1510 | int cw, ch, left, dummy; |
1511 | m_gridWin->GetClientSize( &cw, &ch ); | |
1512 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
60ff3b99 | 1513 | |
2d66e025 MB |
1514 | wxClientDC dc( m_gridWin ); |
1515 | PrepareDC( dc ); | |
d2fdd8d2 | 1516 | dc.SetLogicalFunction(wxINVERT); |
f85afd4e MB |
1517 | if ( m_dragLastPos >= 0 ) |
1518 | { | |
2d66e025 | 1519 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); |
f85afd4e | 1520 | } |
2d66e025 MB |
1521 | dc.DrawLine( left, y, left+cw, y ); |
1522 | m_dragLastPos = y; | |
f85afd4e MB |
1523 | } |
1524 | break; | |
1525 | ||
1526 | case WXGRID_CURSOR_SELECT_ROW: | |
1527 | { | |
1528 | if ( (row = YToRow( y )) >= 0 && | |
1529 | !IsInSelection( row, 0 ) ) | |
1530 | { | |
1531 | SelectRow( row, TRUE ); | |
1532 | } | |
1533 | } | |
1534 | break; | |
f85afd4e MB |
1535 | } |
1536 | } | |
1537 | return; | |
1538 | } | |
1539 | ||
1540 | m_isDragging = FALSE; | |
8f177c8e | 1541 | |
60ff3b99 | 1542 | |
2d66e025 | 1543 | // ------------ Left button pressed |
f85afd4e | 1544 | // |
2d66e025 | 1545 | if ( event.LeftDown() ) |
f85afd4e | 1546 | { |
2d66e025 MB |
1547 | // don't send a label click event for a hit on the |
1548 | // edge of the row label - this is probably the user | |
1549 | // wanting to resize the row | |
1550 | // | |
1551 | if ( YToEdgeOfRow(y) < 0 ) | |
f85afd4e | 1552 | { |
2d66e025 | 1553 | row = YToRow(y); |
58dd5b3b MB |
1554 | if ( row >= 0 && |
1555 | !SendEvent( EVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) ) | |
f85afd4e | 1556 | { |
2d66e025 MB |
1557 | SelectRow( row, event.ShiftDown() ); |
1558 | m_cursorMode = WXGRID_CURSOR_SELECT_ROW; | |
f85afd4e | 1559 | } |
2d66e025 MB |
1560 | } |
1561 | else | |
1562 | { | |
1563 | // starting to drag-resize a row | |
1564 | // | |
1565 | m_rowLabelWin->CaptureMouse(); | |
1566 | } | |
1567 | } | |
f85afd4e | 1568 | |
f85afd4e | 1569 | |
2d66e025 MB |
1570 | // ------------ Left double click |
1571 | // | |
1572 | else if (event.LeftDClick() ) | |
1573 | { | |
1574 | if ( YToEdgeOfRow(y) < 0 ) | |
1575 | { | |
1576 | row = YToRow(y); | |
1577 | SendEvent( EVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ); | |
f85afd4e MB |
1578 | } |
1579 | } | |
60ff3b99 VZ |
1580 | |
1581 | ||
2d66e025 | 1582 | // ------------ Left button released |
f85afd4e | 1583 | // |
2d66e025 | 1584 | else if ( event.LeftUp() ) |
f85afd4e | 1585 | { |
2d66e025 | 1586 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) |
f85afd4e | 1587 | { |
2d66e025 | 1588 | m_rowLabelWin->ReleaseMouse(); |
60ff3b99 | 1589 | |
2d66e025 | 1590 | if ( m_dragLastPos >= 0 ) |
f85afd4e | 1591 | { |
2d66e025 | 1592 | // erase the last line and resize the row |
f85afd4e | 1593 | // |
2d66e025 MB |
1594 | int cw, ch, left, dummy; |
1595 | m_gridWin->GetClientSize( &cw, &ch ); | |
1596 | CalcUnscrolledPosition( 0, 0, &left, &dummy ); | |
60ff3b99 | 1597 | |
2d66e025 MB |
1598 | wxClientDC dc( m_gridWin ); |
1599 | PrepareDC( dc ); | |
1600 | dc.SetLogicalFunction( wxINVERT ); | |
1601 | dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos ); | |
1602 | HideCellEditControl(); | |
1603 | ||
1604 | int rowTop = m_rowBottoms[m_dragRowOrCol] - m_rowHeights[m_dragRowOrCol]; | |
60ff3b99 | 1605 | SetRowSize( m_dragRowOrCol, wxMax( y - rowTop, WXGRID_MIN_ROW_HEIGHT ) ); |
2d66e025 | 1606 | if ( !GetBatchCount() ) |
f85afd4e | 1607 | { |
70c7a608 SN |
1608 | // Only needed to get the correct rect.y: |
1609 | wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) ); | |
1610 | rect.x = 0; | |
cb309039 | 1611 | CalcScrolledPosition(0, rect.y, &dummy, &rect.y); |
70c7a608 SN |
1612 | rect.width = m_rowLabelWidth; |
1613 | rect.height = ch - rect.y; | |
1614 | m_rowLabelWin->Refresh( TRUE, &rect ); | |
1615 | rect.width = cw; | |
c6a51dcd | 1616 | m_gridWin->Refresh( FALSE, &rect ); |
f85afd4e | 1617 | } |
60ff3b99 | 1618 | |
2d66e025 | 1619 | ShowCellEditControl(); |
f85afd4e | 1620 | |
2d66e025 MB |
1621 | // Note: we are ending the event *after* doing |
1622 | // default processing in this case | |
f85afd4e | 1623 | // |
2d66e025 | 1624 | SendEvent( EVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); |
f85afd4e | 1625 | } |
2d66e025 | 1626 | } |
f85afd4e | 1627 | |
790cc417 | 1628 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
2d66e025 MB |
1629 | m_dragLastPos = -1; |
1630 | } | |
f85afd4e | 1631 | |
f85afd4e | 1632 | |
2d66e025 MB |
1633 | // ------------ Right button down |
1634 | // | |
1635 | else if ( event.RightDown() ) | |
1636 | { | |
1637 | row = YToRow(y); | |
1638 | if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) | |
1639 | { | |
1640 | // no default action at the moment | |
f85afd4e MB |
1641 | } |
1642 | } | |
60ff3b99 VZ |
1643 | |
1644 | ||
2d66e025 | 1645 | // ------------ Right double click |
f85afd4e | 1646 | // |
2d66e025 MB |
1647 | else if ( event.RightDClick() ) |
1648 | { | |
1649 | row = YToRow(y); | |
1650 | if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) | |
1651 | { | |
1652 | // no default action at the moment | |
1653 | } | |
1654 | } | |
60ff3b99 VZ |
1655 | |
1656 | ||
2d66e025 | 1657 | // ------------ No buttons down and mouse moving |
f85afd4e | 1658 | // |
2d66e025 | 1659 | else if ( event.Moving() ) |
f85afd4e | 1660 | { |
2d66e025 MB |
1661 | m_dragRowOrCol = YToEdgeOfRow( y ); |
1662 | if ( m_dragRowOrCol >= 0 ) | |
8f177c8e | 1663 | { |
2d66e025 | 1664 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
8f177c8e | 1665 | { |
2d66e025 MB |
1666 | m_cursorMode = WXGRID_CURSOR_RESIZE_ROW; |
1667 | m_rowLabelWin->SetCursor( m_rowResizeCursor ); | |
8f177c8e | 1668 | } |
2d66e025 MB |
1669 | } |
1670 | else | |
1671 | { | |
b93a2fe6 MB |
1672 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
1673 | if ( m_rowLabelWin->GetCursor() == m_rowResizeCursor ) | |
2d66e025 | 1674 | m_rowLabelWin->SetCursor( *wxSTANDARD_CURSOR ); |
8f177c8e | 1675 | } |
f85afd4e | 1676 | } |
2d66e025 MB |
1677 | } |
1678 | ||
1679 | ||
1680 | void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) | |
1681 | { | |
1682 | int x, y, col; | |
1683 | wxPoint pos( event.GetPosition() ); | |
1684 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 1685 | |
2d66e025 | 1686 | if ( event.Dragging() ) |
f85afd4e | 1687 | { |
2d66e025 | 1688 | m_isDragging = TRUE; |
8f177c8e | 1689 | |
2d66e025 | 1690 | if ( event.LeftIsDown() ) |
8f177c8e | 1691 | { |
2d66e025 | 1692 | switch( m_cursorMode ) |
8f177c8e | 1693 | { |
2d66e025 | 1694 | case WXGRID_CURSOR_RESIZE_COL: |
8f177c8e | 1695 | { |
2d66e025 MB |
1696 | int cw, ch, dummy, top; |
1697 | m_gridWin->GetClientSize( &cw, &ch ); | |
1698 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
60ff3b99 | 1699 | |
2d66e025 MB |
1700 | wxClientDC dc( m_gridWin ); |
1701 | PrepareDC( dc ); | |
d2fdd8d2 | 1702 | dc.SetLogicalFunction(wxINVERT); |
2d66e025 MB |
1703 | if ( m_dragLastPos >= 0 ) |
1704 | { | |
1705 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
1706 | } | |
1707 | dc.DrawLine( x, top, x, top+ch ); | |
1708 | m_dragLastPos = x; | |
f85afd4e | 1709 | } |
2d66e025 | 1710 | break; |
f85afd4e | 1711 | |
2d66e025 | 1712 | case WXGRID_CURSOR_SELECT_COL: |
f85afd4e | 1713 | { |
2d66e025 MB |
1714 | if ( (col = XToCol( x )) >= 0 && |
1715 | !IsInSelection( 0, col ) ) | |
1716 | { | |
1717 | SelectCol( col, TRUE ); | |
1718 | } | |
f85afd4e | 1719 | } |
f85afd4e | 1720 | break; |
2d66e025 | 1721 | } |
f85afd4e | 1722 | } |
2d66e025 | 1723 | return; |
f85afd4e | 1724 | } |
2d66e025 MB |
1725 | |
1726 | m_isDragging = FALSE; | |
1727 | ||
60ff3b99 | 1728 | |
2d66e025 | 1729 | // ------------ Left button pressed |
f85afd4e | 1730 | // |
2d66e025 | 1731 | if ( event.LeftDown() ) |
f85afd4e | 1732 | { |
2d66e025 MB |
1733 | // don't send a label click event for a hit on the |
1734 | // edge of the col label - this is probably the user | |
1735 | // wanting to resize the col | |
1736 | // | |
1737 | if ( XToEdgeOfCol(x) < 0 ) | |
8f177c8e | 1738 | { |
2d66e025 | 1739 | col = XToCol(x); |
58dd5b3b MB |
1740 | if ( col >= 0 && |
1741 | !SendEvent( EVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) ) | |
8f177c8e | 1742 | { |
2d66e025 MB |
1743 | SelectCol( col, event.ShiftDown() ); |
1744 | m_cursorMode = WXGRID_CURSOR_SELECT_COL; | |
f85afd4e | 1745 | } |
2d66e025 MB |
1746 | } |
1747 | else | |
1748 | { | |
1749 | // starting to drag-resize a col | |
1750 | // | |
1751 | m_colLabelWin->CaptureMouse(); | |
1752 | } | |
1753 | } | |
f85afd4e | 1754 | |
f85afd4e | 1755 | |
2d66e025 MB |
1756 | // ------------ Left double click |
1757 | // | |
1758 | if ( event.LeftDClick() ) | |
1759 | { | |
1760 | if ( XToEdgeOfCol(x) < 0 ) | |
1761 | { | |
1762 | col = XToCol(x); | |
1763 | SendEvent( EVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ); | |
1764 | } | |
1765 | } | |
60ff3b99 VZ |
1766 | |
1767 | ||
2d66e025 MB |
1768 | // ------------ Left button released |
1769 | // | |
1770 | else if ( event.LeftUp() ) | |
1771 | { | |
1772 | if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) | |
1773 | { | |
1774 | m_colLabelWin->ReleaseMouse(); | |
60ff3b99 | 1775 | |
2d66e025 | 1776 | if ( m_dragLastPos >= 0 ) |
f85afd4e | 1777 | { |
2d66e025 | 1778 | // erase the last line and resize the col |
f85afd4e | 1779 | // |
2d66e025 MB |
1780 | int cw, ch, dummy, top; |
1781 | m_gridWin->GetClientSize( &cw, &ch ); | |
1782 | CalcUnscrolledPosition( 0, 0, &dummy, &top ); | |
60ff3b99 | 1783 | |
2d66e025 MB |
1784 | wxClientDC dc( m_gridWin ); |
1785 | PrepareDC( dc ); | |
1786 | dc.SetLogicalFunction( wxINVERT ); | |
1787 | dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch ); | |
1788 | HideCellEditControl(); | |
1789 | ||
1790 | int colLeft = m_colRights[m_dragRowOrCol] - m_colWidths[m_dragRowOrCol]; | |
60ff3b99 VZ |
1791 | SetColSize( m_dragRowOrCol, wxMax( x - colLeft, WXGRID_MIN_COL_WIDTH ) ); |
1792 | ||
2d66e025 MB |
1793 | if ( !GetBatchCount() ) |
1794 | { | |
70c7a608 SN |
1795 | // Only needed to get the correct rect.x: |
1796 | wxRect rect ( CellToRect( 0, m_dragRowOrCol ) ); | |
1797 | rect.y = 0; | |
cb309039 | 1798 | CalcScrolledPosition(rect.x, 0, &rect.x, &dummy); |
70c7a608 SN |
1799 | rect.width = cw - rect.x; |
1800 | rect.height = m_colLabelHeight; | |
1801 | m_colLabelWin->Refresh( TRUE, &rect ); | |
1802 | rect.height = ch; | |
c6a51dcd | 1803 | m_gridWin->Refresh( FALSE, &rect ); |
2d66e025 | 1804 | } |
60ff3b99 | 1805 | |
2d66e025 | 1806 | ShowCellEditControl(); |
f85afd4e | 1807 | |
2d66e025 MB |
1808 | // Note: we are ending the event *after* doing |
1809 | // default processing in this case | |
1810 | // | |
1811 | SendEvent( EVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); | |
f85afd4e | 1812 | } |
2d66e025 | 1813 | } |
f85afd4e | 1814 | |
790cc417 | 1815 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
2d66e025 | 1816 | m_dragLastPos = -1; |
60ff3b99 VZ |
1817 | } |
1818 | ||
1819 | ||
2d66e025 MB |
1820 | // ------------ Right button down |
1821 | // | |
1822 | else if ( event.RightDown() ) | |
1823 | { | |
1824 | col = XToCol(x); | |
1825 | if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) | |
1826 | { | |
1827 | // no default action at the moment | |
f85afd4e MB |
1828 | } |
1829 | } | |
60ff3b99 VZ |
1830 | |
1831 | ||
2d66e025 | 1832 | // ------------ Right double click |
f85afd4e | 1833 | // |
2d66e025 MB |
1834 | else if ( event.RightDClick() ) |
1835 | { | |
1836 | col = XToCol(x); | |
1837 | if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) | |
1838 | { | |
1839 | // no default action at the moment | |
1840 | } | |
1841 | } | |
60ff3b99 VZ |
1842 | |
1843 | ||
2d66e025 | 1844 | // ------------ No buttons down and mouse moving |
f85afd4e | 1845 | // |
2d66e025 | 1846 | else if ( event.Moving() ) |
f85afd4e | 1847 | { |
2d66e025 MB |
1848 | m_dragRowOrCol = XToEdgeOfCol( x ); |
1849 | if ( m_dragRowOrCol >= 0 ) | |
f85afd4e | 1850 | { |
2d66e025 | 1851 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
f85afd4e | 1852 | { |
2d66e025 MB |
1853 | m_cursorMode = WXGRID_CURSOR_RESIZE_COL; |
1854 | m_colLabelWin->SetCursor( m_colResizeCursor ); | |
f85afd4e | 1855 | } |
2d66e025 MB |
1856 | } |
1857 | else | |
1858 | { | |
b93a2fe6 MB |
1859 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; |
1860 | if ( m_colLabelWin->GetCursor() == m_colResizeCursor ) | |
2d66e025 | 1861 | m_colLabelWin->SetCursor( *wxSTANDARD_CURSOR ); |
8f177c8e | 1862 | } |
f85afd4e MB |
1863 | } |
1864 | } | |
1865 | ||
1866 | ||
2d66e025 | 1867 | void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event ) |
f85afd4e | 1868 | { |
2d66e025 | 1869 | if ( event.LeftDown() ) |
f85afd4e | 1870 | { |
2d66e025 MB |
1871 | // indicate corner label by having both row and |
1872 | // col args == -1 | |
f85afd4e | 1873 | // |
2d66e025 MB |
1874 | if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) ) |
1875 | { | |
1876 | SelectAll(); | |
1877 | } | |
f85afd4e MB |
1878 | } |
1879 | ||
2d66e025 MB |
1880 | else if ( event.LeftDClick() ) |
1881 | { | |
1882 | SendEvent( EVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event ); | |
1883 | } | |
8f177c8e | 1884 | |
2d66e025 | 1885 | else if ( event.RightDown() ) |
f85afd4e | 1886 | { |
2d66e025 | 1887 | if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) ) |
f85afd4e | 1888 | { |
2d66e025 MB |
1889 | // no default action at the moment |
1890 | } | |
1891 | } | |
f85afd4e | 1892 | |
2d66e025 MB |
1893 | else if ( event.RightDClick() ) |
1894 | { | |
1895 | if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) ) | |
1896 | { | |
1897 | // no default action at the moment | |
1898 | } | |
1899 | } | |
1900 | } | |
f85afd4e | 1901 | |
8f177c8e | 1902 | |
2d66e025 MB |
1903 | void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) |
1904 | { | |
1905 | int x, y; | |
1906 | wxPoint pos( event.GetPosition() ); | |
1907 | CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); | |
60ff3b99 | 1908 | |
2d66e025 MB |
1909 | wxGridCellCoords coords; |
1910 | XYToCell( x, y, coords ); | |
60ff3b99 | 1911 | |
2d66e025 MB |
1912 | if ( event.Dragging() ) |
1913 | { | |
1914 | m_isDragging = TRUE; | |
2d66e025 MB |
1915 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
1916 | { | |
f0102d2a VZ |
1917 | // Hide the edit control, so it |
1918 | // won't interfer with drag-shrinking. | |
1919 | if ( IsCellEditControlEnabled() ) | |
1920 | HideCellEditControl(); | |
2d66e025 MB |
1921 | if ( coords != wxGridNoCellCoords ) |
1922 | { | |
1923 | if ( !IsSelection() ) | |
f85afd4e | 1924 | { |
2d66e025 | 1925 | SelectBlock( coords, coords ); |
f85afd4e MB |
1926 | } |
1927 | else | |
1928 | { | |
f0102d2a | 1929 | SelectBlock( m_currentCellCoords, coords ); |
f85afd4e | 1930 | } |
2d66e025 MB |
1931 | } |
1932 | } | |
8f177c8e | 1933 | |
2d66e025 MB |
1934 | return; |
1935 | } | |
66242c80 | 1936 | |
2d66e025 | 1937 | m_isDragging = FALSE; |
8f177c8e | 1938 | |
58dd5b3b | 1939 | if ( coords != wxGridNoCellCoords ) |
2d66e025 | 1940 | { |
58dd5b3b | 1941 | if ( event.LeftDown() ) |
2d66e025 | 1942 | { |
58dd5b3b MB |
1943 | if ( event.ShiftDown() ) |
1944 | { | |
1945 | SelectBlock( m_currentCellCoords, coords ); | |
1946 | } | |
1947 | else | |
1948 | { | |
1949 | if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK, | |
1950 | coords.GetRow(), | |
1951 | coords.GetCol(), | |
1952 | event ) ) | |
1953 | { | |
1954 | MakeCellVisible( coords ); | |
1955 | SetCurrentCell( coords ); | |
1956 | } | |
1957 | } | |
f85afd4e | 1958 | } |
f85afd4e | 1959 | |
f85afd4e | 1960 | |
58dd5b3b MB |
1961 | // ------------ Left double click |
1962 | // | |
1963 | else if ( event.LeftDClick() ) | |
1964 | { | |
1965 | SendEvent( EVT_GRID_CELL_LEFT_DCLICK, | |
1966 | coords.GetRow(), | |
1967 | coords.GetCol(), | |
1968 | event ); | |
1969 | } | |
f85afd4e | 1970 | |
8f177c8e | 1971 | |
58dd5b3b MB |
1972 | // ------------ Left button released |
1973 | // | |
1974 | else if ( event.LeftUp() ) | |
f85afd4e | 1975 | { |
58dd5b3b | 1976 | if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) |
52068ea5 | 1977 | { |
58dd5b3b MB |
1978 | if ( IsSelection() ) |
1979 | { | |
1980 | SendEvent( EVT_GRID_RANGE_SELECT, -1, -1, event ); | |
1981 | } | |
f85afd4e | 1982 | } |
58dd5b3b | 1983 | |
f0102d2a VZ |
1984 | // Show the edit control, if it has |
1985 | // been hidden for drag-shrinking. | |
1986 | if ( IsCellEditControlEnabled() ) | |
1987 | ShowCellEditControl(); | |
2d66e025 | 1988 | |
58dd5b3b MB |
1989 | m_dragLastPos = -1; |
1990 | } | |
f85afd4e | 1991 | |
f85afd4e | 1992 | |
58dd5b3b MB |
1993 | // ------------ Right button down |
1994 | // | |
1995 | else if ( event.RightDown() ) | |
f85afd4e | 1996 | { |
58dd5b3b MB |
1997 | if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK, |
1998 | coords.GetRow(), | |
1999 | coords.GetCol(), | |
2000 | event ) ) | |
2001 | { | |
2002 | // no default action at the moment | |
2003 | } | |
60ff3b99 | 2004 | } |
2d66e025 | 2005 | |
60ff3b99 | 2006 | |
58dd5b3b MB |
2007 | // ------------ Right double click |
2008 | // | |
2009 | else if ( event.RightDClick() ) | |
f85afd4e | 2010 | { |
58dd5b3b MB |
2011 | if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK, |
2012 | coords.GetRow(), | |
2013 | coords.GetCol(), | |
2014 | event ) ) | |
2015 | { | |
2016 | // no default action at the moment | |
2017 | } | |
60ff3b99 | 2018 | } |
790cc417 MB |
2019 | |
2020 | // ------------ Moving and no button action | |
2021 | // | |
2022 | else if ( event.Moving() && !event.IsButton() ) | |
2023 | { | |
2024 | m_cursorMode = WXGRID_CURSOR_SELECT_CELL; | |
2025 | } | |
f85afd4e | 2026 | } |
f85afd4e MB |
2027 | } |
2028 | ||
2029 | ||
2d66e025 MB |
2030 | // |
2031 | // ------ interaction with data model | |
2032 | // | |
2033 | bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg ) | |
f85afd4e | 2034 | { |
2d66e025 | 2035 | switch ( msg.GetId() ) |
17732cec | 2036 | { |
2d66e025 MB |
2037 | case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES: |
2038 | return GetModelValues(); | |
17732cec | 2039 | |
2d66e025 MB |
2040 | case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES: |
2041 | return SetModelValues(); | |
f85afd4e | 2042 | |
2d66e025 MB |
2043 | case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: |
2044 | case wxGRIDTABLE_NOTIFY_ROWS_APPENDED: | |
2045 | case wxGRIDTABLE_NOTIFY_ROWS_DELETED: | |
2046 | case wxGRIDTABLE_NOTIFY_COLS_INSERTED: | |
2047 | case wxGRIDTABLE_NOTIFY_COLS_APPENDED: | |
2048 | case wxGRIDTABLE_NOTIFY_COLS_DELETED: | |
2049 | return Redimension( msg ); | |
2050 | ||
2051 | default: | |
2052 | return FALSE; | |
f85afd4e | 2053 | } |
2d66e025 | 2054 | } |
f85afd4e | 2055 | |
f85afd4e | 2056 | |
f85afd4e | 2057 | |
2d66e025 MB |
2058 | // The behaviour of this function depends on the grid table class |
2059 | // Clear() function. For the default wxGridStringTable class the | |
2060 | // behavious is to replace all cell contents with wxEmptyString but | |
2061 | // not to change the number of rows or cols. | |
2062 | // | |
2063 | void wxGrid::ClearGrid() | |
2064 | { | |
2065 | if ( m_table ) | |
f85afd4e | 2066 | { |
2d66e025 MB |
2067 | m_table->Clear(); |
2068 | SetEditControlValue(); | |
1f1ce288 | 2069 | if ( !GetBatchCount() ) m_gridWin->Refresh(); |
f85afd4e MB |
2070 | } |
2071 | } | |
2072 | ||
2073 | ||
2d66e025 | 2074 | bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) |
f85afd4e | 2075 | { |
2d66e025 | 2076 | // TODO: something with updateLabels flag |
8f177c8e | 2077 | |
2d66e025 | 2078 | if ( !m_created ) |
f85afd4e | 2079 | { |
2d66e025 MB |
2080 | wxLogError( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") ); |
2081 | return FALSE; | |
2082 | } | |
8f177c8e | 2083 | |
2d66e025 MB |
2084 | if ( m_table ) |
2085 | { | |
2086 | bool ok = m_table->InsertRows( pos, numRows ); | |
f85afd4e | 2087 | |
2d66e025 MB |
2088 | // the table will have sent the results of the insert row |
2089 | // operation to this view object as a grid table message | |
2090 | // | |
2091 | if ( ok ) | |
2092 | { | |
2093 | if ( m_numCols == 0 ) | |
f85afd4e | 2094 | { |
2d66e025 MB |
2095 | m_table->AppendCols( WXGRID_DEFAULT_NUMBER_COLS ); |
2096 | // | |
2097 | // TODO: perhaps instead of appending the default number of cols | |
2098 | // we should remember what the last non-zero number of cols was ? | |
2099 | // | |
2100 | } | |
8f177c8e | 2101 | |
2d66e025 MB |
2102 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
2103 | { | |
2104 | // if we have just inserted cols into an empty grid the current | |
2105 | // cell will be undefined... | |
2106 | // | |
2107 | SetCurrentCell( 0, 0 ); | |
f85afd4e MB |
2108 | } |
2109 | ||
2d66e025 MB |
2110 | ClearSelection(); |
2111 | if ( !GetBatchCount() ) Refresh(); | |
f85afd4e | 2112 | } |
2d66e025 MB |
2113 | |
2114 | SetEditControlValue(); | |
2115 | return ok; | |
2116 | } | |
2117 | else | |
2118 | { | |
2119 | return FALSE; | |
f85afd4e MB |
2120 | } |
2121 | } | |
2122 | ||
2123 | ||
2d66e025 | 2124 | bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) ) |
f85afd4e | 2125 | { |
2d66e025 MB |
2126 | // TODO: something with updateLabels flag |
2127 | ||
2128 | if ( !m_created ) | |
f85afd4e | 2129 | { |
2d66e025 MB |
2130 | wxLogError( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") ); |
2131 | return FALSE; | |
2132 | } | |
2133 | ||
2134 | if ( m_table && m_table->AppendRows( numRows ) ) | |
2135 | { | |
2136 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
2137 | { | |
2138 | // if we have just inserted cols into an empty grid the current | |
2139 | // cell will be undefined... | |
2140 | // | |
2141 | SetCurrentCell( 0, 0 ); | |
2142 | } | |
2143 | ||
2144 | // the table will have sent the results of the append row | |
2145 | // operation to this view object as a grid table message | |
2146 | // | |
2147 | ClearSelection(); | |
2148 | if ( !GetBatchCount() ) Refresh(); | |
2149 | return TRUE; | |
2150 | } | |
2151 | else | |
2152 | { | |
2153 | return FALSE; | |
f85afd4e MB |
2154 | } |
2155 | } | |
2156 | ||
2d66e025 MB |
2157 | |
2158 | bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 2159 | { |
2d66e025 MB |
2160 | // TODO: something with updateLabels flag |
2161 | ||
2162 | if ( !m_created ) | |
f85afd4e | 2163 | { |
2d66e025 MB |
2164 | wxLogError( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") ); |
2165 | return FALSE; | |
2166 | } | |
2167 | ||
2168 | if ( m_table && m_table->DeleteRows( pos, numRows ) ) | |
2169 | { | |
2170 | // the table will have sent the results of the delete row | |
2171 | // operation to this view object as a grid table message | |
2172 | // | |
2173 | if ( m_numRows > 0 ) | |
2174 | SetEditControlValue(); | |
2b37dc19 | 2175 | else |
2d66e025 | 2176 | HideCellEditControl(); |
f85afd4e | 2177 | |
2d66e025 MB |
2178 | ClearSelection(); |
2179 | if ( !GetBatchCount() ) Refresh(); | |
2180 | return TRUE; | |
2181 | } | |
2182 | else | |
2183 | { | |
2184 | return FALSE; | |
2185 | } | |
2186 | } | |
f85afd4e | 2187 | |
f85afd4e | 2188 | |
2d66e025 MB |
2189 | bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) |
2190 | { | |
2191 | // TODO: something with updateLabels flag | |
8f177c8e | 2192 | |
2d66e025 MB |
2193 | if ( !m_created ) |
2194 | { | |
2195 | wxLogError( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") ); | |
2196 | return FALSE; | |
2197 | } | |
f85afd4e | 2198 | |
2d66e025 MB |
2199 | if ( m_table ) |
2200 | { | |
2201 | HideCellEditControl(); | |
2202 | bool ok = m_table->InsertCols( pos, numCols ); | |
2203 | ||
2204 | // the table will have sent the results of the insert col | |
2205 | // operation to this view object as a grid table message | |
2206 | // | |
2207 | if ( ok ) | |
f85afd4e | 2208 | { |
2d66e025 | 2209 | if ( m_currentCellCoords == wxGridNoCellCoords ) |
f85afd4e | 2210 | { |
2d66e025 MB |
2211 | // if we have just inserted cols into an empty grid the current |
2212 | // cell will be undefined... | |
2213 | // | |
2214 | SetCurrentCell( 0, 0 ); | |
f85afd4e | 2215 | } |
2d66e025 MB |
2216 | |
2217 | ClearSelection(); | |
2218 | if ( !GetBatchCount() ) Refresh(); | |
f85afd4e | 2219 | } |
2d66e025 MB |
2220 | |
2221 | SetEditControlValue(); | |
2222 | return ok; | |
2223 | } | |
2224 | else | |
2225 | { | |
2226 | return FALSE; | |
f85afd4e MB |
2227 | } |
2228 | } | |
2229 | ||
2d66e025 MB |
2230 | |
2231 | bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) ) | |
f85afd4e | 2232 | { |
2d66e025 MB |
2233 | // TODO: something with updateLabels flag |
2234 | ||
2235 | if ( !m_created ) | |
f85afd4e | 2236 | { |
2d66e025 MB |
2237 | wxLogError( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") ); |
2238 | return FALSE; | |
2239 | } | |
f85afd4e | 2240 | |
2d66e025 MB |
2241 | if ( m_table && m_table->AppendCols( numCols ) ) |
2242 | { | |
2243 | // the table will have sent the results of the append col | |
2244 | // operation to this view object as a grid table message | |
2245 | // | |
2246 | if ( m_currentCellCoords == wxGridNoCellCoords ) | |
f85afd4e | 2247 | { |
2d66e025 MB |
2248 | // if we have just inserted cols into an empty grid the current |
2249 | // cell will be undefined... | |
2250 | // | |
2251 | SetCurrentCell( 0, 0 ); | |
f85afd4e | 2252 | } |
8f177c8e | 2253 | |
2d66e025 MB |
2254 | ClearSelection(); |
2255 | if ( !GetBatchCount() ) Refresh(); | |
2256 | return TRUE; | |
f85afd4e | 2257 | } |
2d66e025 | 2258 | else |
f85afd4e | 2259 | { |
2d66e025 | 2260 | return FALSE; |
f85afd4e | 2261 | } |
f85afd4e MB |
2262 | } |
2263 | ||
2264 | ||
2d66e025 | 2265 | bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) |
f85afd4e | 2266 | { |
2d66e025 | 2267 | // TODO: something with updateLabels flag |
8f177c8e | 2268 | |
2d66e025 | 2269 | if ( !m_created ) |
f85afd4e | 2270 | { |
2d66e025 MB |
2271 | wxLogError( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") ); |
2272 | return FALSE; | |
f85afd4e MB |
2273 | } |
2274 | ||
2d66e025 MB |
2275 | if ( m_table && m_table->DeleteCols( pos, numCols ) ) |
2276 | { | |
2277 | // the table will have sent the results of the delete col | |
2278 | // operation to this view object as a grid table message | |
2279 | // | |
2280 | if ( m_numCols > 0 ) | |
2281 | SetEditControlValue(); | |
2282 | else | |
2283 | HideCellEditControl(); | |
8f177c8e | 2284 | |
2d66e025 MB |
2285 | ClearSelection(); |
2286 | if ( !GetBatchCount() ) Refresh(); | |
2287 | return TRUE; | |
2288 | } | |
2289 | else | |
f85afd4e | 2290 | { |
2d66e025 | 2291 | return FALSE; |
f85afd4e | 2292 | } |
f85afd4e MB |
2293 | } |
2294 | ||
2295 | ||
2d66e025 MB |
2296 | |
2297 | // | |
2298 | // ----- event handlers | |
f85afd4e | 2299 | // |
8f177c8e | 2300 | |
2d66e025 MB |
2301 | // Generate a grid event based on a mouse event and |
2302 | // return the result of ProcessEvent() | |
2303 | // | |
2304 | bool wxGrid::SendEvent( const wxEventType type, | |
2305 | int row, int col, | |
2306 | wxMouseEvent& mouseEv ) | |
2307 | { | |
2308 | if ( type == EVT_GRID_ROW_SIZE || | |
2309 | type == EVT_GRID_COL_SIZE ) | |
f85afd4e | 2310 | { |
2d66e025 MB |
2311 | int rowOrCol = (row == -1 ? col : row); |
2312 | ||
2313 | wxGridSizeEvent gridEvt( GetId(), | |
2314 | type, | |
2315 | this, | |
2316 | rowOrCol, | |
2317 | mouseEv.GetX(), mouseEv.GetY(), | |
2318 | mouseEv.ControlDown(), | |
2319 | mouseEv.ShiftDown(), | |
2320 | mouseEv.AltDown(), | |
2321 | mouseEv.MetaDown() ); | |
2322 | ||
2323 | return GetEventHandler()->ProcessEvent(gridEvt); | |
f85afd4e | 2324 | } |
2d66e025 MB |
2325 | else if ( type == EVT_GRID_RANGE_SELECT ) |
2326 | { | |
2327 | wxGridRangeSelectEvent gridEvt( GetId(), | |
2328 | type, | |
2329 | this, | |
2330 | m_selectedTopLeft, | |
2331 | m_selectedBottomRight, | |
2332 | mouseEv.ControlDown(), | |
2333 | mouseEv.ShiftDown(), | |
2334 | mouseEv.AltDown(), | |
2335 | mouseEv.MetaDown() ); | |
f85afd4e | 2336 | |
2d66e025 MB |
2337 | return GetEventHandler()->ProcessEvent(gridEvt); |
2338 | } | |
2339 | else | |
2340 | { | |
2341 | wxGridEvent gridEvt( GetId(), | |
2342 | type, | |
2343 | this, | |
2344 | row, col, | |
2345 | mouseEv.GetX(), mouseEv.GetY(), | |
2346 | mouseEv.ControlDown(), | |
2347 | mouseEv.ShiftDown(), | |
2348 | mouseEv.AltDown(), | |
2349 | mouseEv.MetaDown() ); | |
8f177c8e | 2350 | |
2d66e025 MB |
2351 | return GetEventHandler()->ProcessEvent(gridEvt); |
2352 | } | |
f85afd4e MB |
2353 | } |
2354 | ||
2355 | ||
2d66e025 MB |
2356 | // Generate a grid event of specified type and return the result |
2357 | // of ProcessEvent(). | |
f85afd4e | 2358 | // |
2d66e025 MB |
2359 | bool wxGrid::SendEvent( const wxEventType type, |
2360 | int row, int col ) | |
f85afd4e | 2361 | { |
2d66e025 MB |
2362 | if ( type == EVT_GRID_ROW_SIZE || |
2363 | type == EVT_GRID_COL_SIZE ) | |
f85afd4e | 2364 | { |
2d66e025 | 2365 | int rowOrCol = (row == -1 ? col : row); |
f85afd4e | 2366 | |
2d66e025 MB |
2367 | wxGridSizeEvent gridEvt( GetId(), |
2368 | type, | |
2369 | this, | |
2370 | rowOrCol ); | |
2371 | ||
2372 | return GetEventHandler()->ProcessEvent(gridEvt); | |
2373 | } | |
2374 | else | |
2375 | { | |
2376 | wxGridEvent gridEvt( GetId(), | |
2377 | type, | |
2378 | this, | |
2379 | row, col ); | |
8f177c8e | 2380 | |
2d66e025 MB |
2381 | return GetEventHandler()->ProcessEvent(gridEvt); |
2382 | } | |
f85afd4e MB |
2383 | } |
2384 | ||
2385 | ||
2d66e025 | 2386 | void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
f85afd4e | 2387 | { |
2d66e025 | 2388 | wxPaintDC dc( this ); |
f85afd4e | 2389 | |
2d66e025 MB |
2390 | if ( m_currentCellCoords == wxGridNoCellCoords && |
2391 | m_numRows && m_numCols ) | |
f85afd4e | 2392 | { |
2d66e025 MB |
2393 | m_currentCellCoords.Set(0, 0); |
2394 | SetEditControlValue(); | |
2395 | ShowCellEditControl(); | |
f85afd4e | 2396 | } |
4634a5d6 MB |
2397 | |
2398 | m_displayed = TRUE; | |
8f177c8e | 2399 | } |
f85afd4e MB |
2400 | |
2401 | ||
18f9565d MB |
2402 | // This is just here to make sure that CalcDimensions gets called when |
2403 | // the grid view is resized... then the size event is skipped to allow | |
2404 | // the box sizers to handle everything | |
2405 | // | |
2d66e025 | 2406 | void wxGrid::OnSize( wxSizeEvent& event ) |
f85afd4e | 2407 | { |
7807d81c | 2408 | CalcWindowSizes(); |
2d66e025 | 2409 | CalcDimensions(); |
f85afd4e MB |
2410 | } |
2411 | ||
f85afd4e | 2412 | |
2d66e025 | 2413 | void wxGrid::OnKeyDown( wxKeyEvent& event ) |
f85afd4e | 2414 | { |
2d66e025 | 2415 | if ( m_inOnKeyDown ) |
f85afd4e | 2416 | { |
2d66e025 MB |
2417 | // shouldn't be here - we are going round in circles... |
2418 | // | |
2419 | wxLogFatalError( wxT("wxGrid::OnKeyDown called while alread active") ); | |
f85afd4e MB |
2420 | } |
2421 | ||
2d66e025 | 2422 | m_inOnKeyDown = TRUE; |
f85afd4e | 2423 | |
2d66e025 MB |
2424 | // propagate the event up and see if it gets processed |
2425 | // | |
2426 | wxWindow *parent = GetParent(); | |
2427 | wxKeyEvent keyEvt( event ); | |
2428 | keyEvt.SetEventObject( parent ); | |
2429 | ||
2430 | if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) ) | |
f85afd4e | 2431 | { |
2d66e025 MB |
2432 | // try local handlers |
2433 | // | |
2434 | switch ( event.KeyCode() ) | |
2435 | { | |
2436 | case WXK_UP: | |
2437 | if ( event.ControlDown() ) | |
2438 | { | |
2439 | MoveCursorUpBlock(); | |
2440 | } | |
2441 | else | |
2442 | { | |
2443 | MoveCursorUp(); | |
2444 | } | |
2445 | break; | |
f85afd4e | 2446 | |
2d66e025 MB |
2447 | case WXK_DOWN: |
2448 | if ( event.ControlDown() ) | |
2449 | { | |
2450 | MoveCursorDownBlock(); | |
2451 | } | |
2452 | else | |
2453 | { | |
2454 | MoveCursorDown(); | |
2455 | } | |
2456 | break; | |
8f177c8e | 2457 | |
2d66e025 MB |
2458 | case WXK_LEFT: |
2459 | if ( event.ControlDown() ) | |
2460 | { | |
2461 | MoveCursorLeftBlock(); | |
2462 | } | |
2463 | else | |
2464 | { | |
2465 | MoveCursorLeft(); | |
2466 | } | |
2467 | break; | |
2468 | ||
2469 | case WXK_RIGHT: | |
2470 | if ( event.ControlDown() ) | |
2471 | { | |
2472 | MoveCursorRightBlock(); | |
2473 | } | |
2474 | else | |
2475 | { | |
2476 | MoveCursorRight(); | |
2477 | } | |
2478 | break; | |
f603012f MB |
2479 | |
2480 | case WXK_SPACE: | |
2481 | if ( !IsEditable() ) | |
2482 | { | |
2483 | MoveCursorRight(); | |
2484 | } | |
2485 | else | |
2486 | { | |
8a9a9d58 | 2487 | event.Skip(); |
f603012f | 2488 | } |
8febdd39 | 2489 | break; |
2d66e025 MB |
2490 | |
2491 | case WXK_RETURN: | |
58dd5b3b MB |
2492 | if ( event.ControlDown() ) |
2493 | { | |
2494 | event.Skip(); // to let the edit control have the return | |
2495 | } | |
2496 | else | |
2497 | { | |
2498 | MoveCursorDown(); | |
2499 | } | |
2d66e025 MB |
2500 | break; |
2501 | ||
2502 | case WXK_HOME: | |
2503 | if ( event.ControlDown() ) | |
2504 | { | |
2505 | MakeCellVisible( 0, 0 ); | |
2506 | SetCurrentCell( 0, 0 ); | |
2507 | } | |
2508 | else | |
2509 | { | |
2510 | event.Skip(); | |
2511 | } | |
2512 | break; | |
2513 | ||
2514 | case WXK_END: | |
2515 | if ( event.ControlDown() ) | |
2516 | { | |
2517 | MakeCellVisible( m_numRows-1, m_numCols-1 ); | |
2518 | SetCurrentCell( m_numRows-1, m_numCols-1 ); | |
2519 | } | |
2520 | else | |
2521 | { | |
2522 | event.Skip(); | |
2523 | } | |
2524 | break; | |
2525 | ||
2526 | case WXK_PRIOR: | |
2527 | MovePageUp(); | |
2528 | break; | |
2529 | ||
2530 | case WXK_NEXT: | |
2531 | MovePageDown(); | |
2532 | break; | |
2533 | ||
2534 | default: | |
2535 | // now try the cell edit control | |
2536 | // | |
2537 | if ( IsCellEditControlEnabled() ) | |
2538 | { | |
2539 | event.SetEventObject( m_cellEditCtrl ); | |
2540 | m_cellEditCtrl->GetEventHandler()->ProcessEvent( event ); | |
2541 | } | |
2542 | break; | |
2543 | } | |
f85afd4e MB |
2544 | } |
2545 | ||
2d66e025 | 2546 | m_inOnKeyDown = FALSE; |
f85afd4e MB |
2547 | } |
2548 | ||
2d66e025 MB |
2549 | |
2550 | void wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) | |
66242c80 | 2551 | { |
2d66e025 | 2552 | if ( SendEvent( EVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) ) |
66242c80 | 2553 | { |
2d66e025 MB |
2554 | // the event has been intercepted - do nothing |
2555 | return; | |
2556 | } | |
66242c80 | 2557 | |
4634a5d6 MB |
2558 | if ( m_displayed && |
2559 | m_currentCellCoords != wxGridNoCellCoords ) | |
2d66e025 | 2560 | { |
2d66e025 MB |
2561 | HideCellEditControl(); |
2562 | SaveEditControlValue(); | |
66242c80 | 2563 | } |
8f177c8e | 2564 | |
2d66e025 MB |
2565 | m_currentCellCoords = coords; |
2566 | ||
2567 | SetEditControlValue(); | |
2d66e025 | 2568 | |
4634a5d6 | 2569 | if ( m_displayed ) |
2d66e025 | 2570 | { |
4634a5d6 MB |
2571 | ShowCellEditControl(); |
2572 | ||
2573 | if ( IsSelection() ) | |
2574 | { | |
2575 | wxRect r( SelectionToDeviceRect() ); | |
2576 | ClearSelection(); | |
2577 | if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r ); | |
2578 | } | |
2d66e025 | 2579 | } |
66242c80 MB |
2580 | } |
2581 | ||
2d66e025 MB |
2582 | |
2583 | // | |
2584 | // ------ functions to get/send data (see also public functions) | |
2585 | // | |
2586 | ||
2587 | bool wxGrid::GetModelValues() | |
66242c80 | 2588 | { |
2d66e025 | 2589 | if ( m_table ) |
66242c80 | 2590 | { |
2d66e025 | 2591 | // all we need to do is repaint the grid |
66242c80 | 2592 | // |
2d66e025 | 2593 | m_gridWin->Refresh(); |
66242c80 MB |
2594 | return TRUE; |
2595 | } | |
8f177c8e | 2596 | |
66242c80 MB |
2597 | return FALSE; |
2598 | } | |
2599 | ||
2d66e025 MB |
2600 | |
2601 | bool wxGrid::SetModelValues() | |
f85afd4e | 2602 | { |
2d66e025 | 2603 | int row, col; |
8f177c8e | 2604 | |
2d66e025 MB |
2605 | if ( m_table ) |
2606 | { | |
2607 | for ( row = 0; row < m_numRows; row++ ) | |
f85afd4e | 2608 | { |
2d66e025 | 2609 | for ( col = 0; col < m_numCols; col++ ) |
f85afd4e | 2610 | { |
2d66e025 | 2611 | m_table->SetValue( row, col, GetCellValue(row, col) ); |
f85afd4e MB |
2612 | } |
2613 | } | |
8f177c8e | 2614 | |
f85afd4e MB |
2615 | return TRUE; |
2616 | } | |
2617 | ||
2618 | return FALSE; | |
2619 | } | |
2620 | ||
2d66e025 MB |
2621 | |
2622 | ||
2623 | // Note - this function only draws cells that are in the list of | |
2624 | // exposed cells (usually set from the update region by | |
2625 | // CalcExposedCells) | |
2626 | // | |
2627 | void wxGrid::DrawGridCellArea( wxDC& dc ) | |
f85afd4e | 2628 | { |
2d66e025 | 2629 | if ( !m_numRows || !m_numCols ) return; |
60ff3b99 | 2630 | |
2d66e025 MB |
2631 | size_t i; |
2632 | size_t numCells = m_cellsExposed.GetCount(); | |
60ff3b99 | 2633 | |
2d66e025 | 2634 | for ( i = 0; i < numCells; i++ ) |
f85afd4e | 2635 | { |
2d66e025 MB |
2636 | DrawCell( dc, m_cellsExposed[i] ); |
2637 | } | |
2638 | } | |
8f177c8e | 2639 | |
8f177c8e | 2640 | |
2d66e025 MB |
2641 | void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) |
2642 | { | |
2643 | if ( m_colWidths[coords.GetCol()] <=0 || | |
2644 | m_rowHeights[coords.GetRow()] <= 0 ) return; | |
60ff3b99 | 2645 | |
9496deb5 | 2646 | #if !WXGRID_DRAW_LINES |
2d66e025 MB |
2647 | if ( m_gridLinesEnabled ) |
2648 | DrawCellBorder( dc, coords ); | |
796df70a | 2649 | #endif |
f85afd4e | 2650 | |
2d66e025 | 2651 | DrawCellBackground( dc, coords ); |
f85afd4e | 2652 | |
2d66e025 MB |
2653 | // TODO: separate functions here for different kinds of cells ? |
2654 | // e.g. text, image | |
2655 | // | |
2656 | DrawCellValue( dc, coords ); | |
2657 | } | |
f85afd4e | 2658 | |
2d66e025 MB |
2659 | |
2660 | void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) | |
2661 | { | |
2662 | if ( m_colWidths[coords.GetCol()] <=0 || | |
2663 | m_rowHeights[coords.GetRow()] <= 0 ) return; | |
60ff3b99 | 2664 | |
2d66e025 MB |
2665 | dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) ); |
2666 | int row = coords.GetRow(); | |
2667 | int col = coords.GetCol(); | |
60ff3b99 | 2668 | |
2d66e025 MB |
2669 | // right hand border |
2670 | // | |
2671 | dc.DrawLine( m_colRights[col], m_rowBottoms[row] - m_rowHeights[row], | |
2672 | m_colRights[col], m_rowBottoms[row] ); | |
2673 | ||
2674 | // bottom border | |
2675 | // | |
2676 | dc.DrawLine( m_colRights[col] - m_colWidths[col], m_rowBottoms[row], | |
2677 | m_colRights[col], m_rowBottoms[row] ); | |
f85afd4e MB |
2678 | } |
2679 | ||
2d66e025 MB |
2680 | |
2681 | void wxGrid::DrawCellBackground( wxDC& dc, const wxGridCellCoords& coords ) | |
f85afd4e | 2682 | { |
2d66e025 MB |
2683 | if ( m_colWidths[coords.GetCol()] <=0 || |
2684 | m_rowHeights[coords.GetRow()] <= 0 ) return; | |
60ff3b99 | 2685 | |
2d66e025 MB |
2686 | int row = coords.GetRow(); |
2687 | int col = coords.GetCol(); | |
8f177c8e | 2688 | |
2d66e025 | 2689 | dc.SetBackgroundMode( wxSOLID ); |
f85afd4e | 2690 | |
2d66e025 MB |
2691 | if ( IsInSelection( coords ) ) |
2692 | { | |
2693 | // TODO: improve this | |
2694 | // | |
2695 | dc.SetBrush( *wxBLACK_BRUSH ); | |
f85afd4e | 2696 | } |
2d66e025 | 2697 | else |
f85afd4e | 2698 | { |
2d66e025 | 2699 | dc.SetBrush( wxBrush(GetCellBackgroundColour(row, col), wxSOLID) ); |
f85afd4e MB |
2700 | } |
2701 | ||
2d66e025 | 2702 | dc.SetPen( *wxTRANSPARENT_PEN ); |
8f177c8e | 2703 | |
2d66e025 MB |
2704 | dc.DrawRectangle( m_colRights[col] - m_colWidths[col] + 1, |
2705 | m_rowBottoms[row] - m_rowHeights[row] + 1, | |
2706 | m_colWidths[col]-1, | |
2707 | m_rowHeights[row]-1 ); | |
f85afd4e MB |
2708 | } |
2709 | ||
2710 | ||
2d66e025 | 2711 | void wxGrid::DrawCellValue( wxDC& dc, const wxGridCellCoords& coords ) |
f85afd4e | 2712 | { |
2d66e025 MB |
2713 | if ( m_colWidths[coords.GetCol()] <=0 || |
2714 | m_rowHeights[coords.GetRow()] <= 0 ) return; | |
60ff3b99 | 2715 | |
2d66e025 MB |
2716 | int row = coords.GetRow(); |
2717 | int col = coords.GetCol(); | |
f85afd4e | 2718 | |
2d66e025 | 2719 | dc.SetBackgroundMode( wxTRANSPARENT ); |
8f177c8e | 2720 | |
2d66e025 MB |
2721 | if ( IsInSelection( row, col ) ) |
2722 | { | |
2723 | // TODO: improve this | |
2724 | // | |
2725 | dc.SetTextBackground( wxColour(0, 0, 0) ); | |
2726 | dc.SetTextForeground( wxColour(255, 255, 255) ); | |
2727 | } | |
2728 | else | |
f85afd4e | 2729 | { |
2d66e025 MB |
2730 | dc.SetTextBackground( GetCellBackgroundColour(row, col) ); |
2731 | dc.SetTextForeground( GetCellTextColour(row, col) ); | |
2732 | } | |
2733 | dc.SetFont( GetCellFont(row, col) ); | |
f85afd4e | 2734 | |
2d66e025 MB |
2735 | int hAlign, vAlign; |
2736 | GetCellAlignment( row, col, &hAlign, &vAlign ); | |
8f177c8e | 2737 | |
2d66e025 MB |
2738 | wxRect rect; |
2739 | rect.SetX( m_colRights[col] - m_colWidths[col] + 2 ); | |
2740 | rect.SetY( m_rowBottoms[row] - m_rowHeights[row] + 2 ); | |
2741 | rect.SetWidth( m_colWidths[col] - 4 ); | |
2742 | rect.SetHeight( m_rowHeights[row] - 4 ); | |
60ff3b99 | 2743 | |
2d66e025 | 2744 | DrawTextRectangle( dc, GetCellValue( row, col ), rect, hAlign, vAlign ); |
f85afd4e MB |
2745 | } |
2746 | ||
2747 | ||
2d66e025 MB |
2748 | |
2749 | // TODO: remove this ??? | |
2750 | // This is used to redraw all grid lines e.g. when the grid line colour | |
2751 | // has been changed | |
2752 | // | |
796df70a | 2753 | void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & reg ) |
f85afd4e | 2754 | { |
60ff3b99 VZ |
2755 | if ( !m_gridLinesEnabled || |
2756 | !m_numRows || | |
2d66e025 | 2757 | !m_numCols ) return; |
f85afd4e | 2758 | |
2d66e025 | 2759 | int top, bottom, left, right; |
796df70a SN |
2760 | |
2761 | if (reg.IsEmpty()){ | |
2762 | int cw, ch; | |
2763 | m_gridWin->GetClientSize(&cw, &ch); | |
2764 | ||
2765 | // virtual coords of visible area | |
2766 | // | |
2767 | CalcUnscrolledPosition( 0, 0, &left, &top ); | |
2768 | CalcUnscrolledPosition( cw, ch, &right, &bottom ); | |
2769 | } | |
2770 | else{ | |
2771 | wxCoord x, y, w, h; | |
2772 | reg.GetBox(x, y, w, h); | |
2773 | CalcUnscrolledPosition( x, y, &left, &top ); | |
2774 | CalcUnscrolledPosition( x + w, y + h, &right, &bottom ); | |
2775 | } | |
f85afd4e | 2776 | |
9496deb5 MB |
2777 | // avoid drawing grid lines past the last row and col |
2778 | // | |
2779 | right = wxMin( right, m_colRights[m_numCols-1] ); | |
2780 | bottom = wxMin( bottom, m_rowBottoms[m_numRows-1] ); | |
2781 | ||
2d66e025 | 2782 | dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) ); |
f85afd4e | 2783 | |
2d66e025 | 2784 | // horizontal grid lines |
f85afd4e | 2785 | // |
60ff3b99 | 2786 | int i; |
9496deb5 | 2787 | for ( i = 0; i < m_numRows; i++ ) |
f85afd4e | 2788 | { |
2d66e025 MB |
2789 | if ( m_rowBottoms[i] > bottom ) |
2790 | { | |
2791 | break; | |
2792 | } | |
2793 | else if ( m_rowBottoms[i] >= top ) | |
2794 | { | |
2795 | dc.DrawLine( left, m_rowBottoms[i], right, m_rowBottoms[i] ); | |
2796 | } | |
f85afd4e MB |
2797 | } |
2798 | ||
f85afd4e | 2799 | |
2d66e025 MB |
2800 | // vertical grid lines |
2801 | // | |
9496deb5 | 2802 | for ( i = 0; i < m_numCols; i++ ) |
f85afd4e | 2803 | { |
2d66e025 MB |
2804 | if ( m_colRights[i] > right ) |
2805 | { | |
2806 | break; | |
2807 | } | |
2808 | else if ( m_colRights[i] >= left ) | |
2809 | { | |
2810 | dc.DrawLine( m_colRights[i], top, m_colRights[i], bottom ); | |
2811 | } | |
2812 | } | |
2813 | } | |
f85afd4e | 2814 | |
8f177c8e | 2815 | |
2d66e025 MB |
2816 | void wxGrid::DrawRowLabels( wxDC& dc ) |
2817 | { | |
2818 | if ( !m_numRows || !m_numCols ) return; | |
60ff3b99 | 2819 | |
2d66e025 MB |
2820 | size_t i; |
2821 | size_t numLabels = m_rowLabelsExposed.GetCount(); | |
60ff3b99 | 2822 | |
2d66e025 MB |
2823 | for ( i = 0; i < numLabels; i++ ) |
2824 | { | |
2825 | DrawRowLabel( dc, m_rowLabelsExposed[i] ); | |
60ff3b99 | 2826 | } |
f85afd4e MB |
2827 | } |
2828 | ||
2829 | ||
2d66e025 | 2830 | void wxGrid::DrawRowLabel( wxDC& dc, int row ) |
f85afd4e | 2831 | { |
2d66e025 | 2832 | if ( m_rowHeights[row] <= 0 ) return; |
60ff3b99 | 2833 | |
b0d6ff2f MB |
2834 | int rowTop = m_rowBottoms[row] - m_rowHeights[row]; |
2835 | ||
2d66e025 | 2836 | dc.SetPen( *wxBLACK_PEN ); |
b0d6ff2f MB |
2837 | dc.DrawLine( m_rowLabelWidth-1, rowTop, |
2838 | m_rowLabelWidth-1, m_rowBottoms[row]-1 ); | |
2839 | ||
2840 | dc.DrawLine( 0, m_rowBottoms[row]-1, | |
2841 | m_rowLabelWidth-1, m_rowBottoms[row]-1 ); | |
2842 | ||
2d66e025 | 2843 | dc.SetPen( *wxWHITE_PEN ); |
b0d6ff2f MB |
2844 | dc.DrawLine( 0, rowTop, 0, m_rowBottoms[row]-1 ); |
2845 | dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop ); | |
60ff3b99 | 2846 | |
f85afd4e | 2847 | dc.SetBackgroundMode( wxTRANSPARENT ); |
f85afd4e MB |
2848 | dc.SetTextForeground( GetLabelTextColour() ); |
2849 | dc.SetFont( GetLabelFont() ); | |
8f177c8e | 2850 | |
f85afd4e | 2851 | int hAlign, vAlign; |
2d66e025 | 2852 | GetRowLabelAlignment( &hAlign, &vAlign ); |
60ff3b99 | 2853 | |
2d66e025 MB |
2854 | wxRect rect; |
2855 | rect.SetX( 2 ); | |
2856 | rect.SetY( m_rowBottoms[row] - m_rowHeights[row] + 2 ); | |
2857 | rect.SetWidth( m_rowLabelWidth - 4 ); | |
2858 | rect.SetHeight( m_rowHeights[row] - 4 ); | |
60ff3b99 | 2859 | DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign ); |
f85afd4e MB |
2860 | } |
2861 | ||
2862 | ||
2d66e025 | 2863 | void wxGrid::DrawColLabels( wxDC& dc ) |
f85afd4e | 2864 | { |
2d66e025 | 2865 | if ( !m_numRows || !m_numCols ) return; |
60ff3b99 | 2866 | |
2d66e025 MB |
2867 | size_t i; |
2868 | size_t numLabels = m_colLabelsExposed.GetCount(); | |
60ff3b99 | 2869 | |
2d66e025 | 2870 | for ( i = 0; i < numLabels; i++ ) |
f85afd4e | 2871 | { |
2d66e025 | 2872 | DrawColLabel( dc, m_colLabelsExposed[i] ); |
60ff3b99 | 2873 | } |
f85afd4e MB |
2874 | } |
2875 | ||
2876 | ||
2d66e025 | 2877 | void wxGrid::DrawColLabel( wxDC& dc, int col ) |
f85afd4e | 2878 | { |
2d66e025 | 2879 | if ( m_colWidths[col] <= 0 ) return; |
60ff3b99 | 2880 | |
b0d6ff2f MB |
2881 | int colLeft = m_colRights[col] - m_colWidths[col]; |
2882 | ||
2d66e025 | 2883 | dc.SetPen( *wxBLACK_PEN ); |
b0d6ff2f MB |
2884 | dc.DrawLine( m_colRights[col]-1, 0, |
2885 | m_colRights[col]-1, m_colLabelHeight-1 ); | |
2886 | ||
2887 | dc.DrawLine( colLeft, m_colLabelHeight-1, | |
2888 | m_colRights[col]-1, m_colLabelHeight-1 ); | |
2889 | ||
f85afd4e | 2890 | dc.SetPen( *wxWHITE_PEN ); |
b0d6ff2f MB |
2891 | dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 ); |
2892 | dc.DrawLine( colLeft, 0, m_colRights[col]-1, 0 ); | |
f85afd4e | 2893 | |
b0d6ff2f MB |
2894 | dc.SetBackgroundMode( wxTRANSPARENT ); |
2895 | dc.SetTextForeground( GetLabelTextColour() ); | |
2896 | dc.SetFont( GetLabelFont() ); | |
8f177c8e | 2897 | |
f85afd4e | 2898 | dc.SetBackgroundMode( wxTRANSPARENT ); |
f85afd4e MB |
2899 | dc.SetTextForeground( GetLabelTextColour() ); |
2900 | dc.SetFont( GetLabelFont() ); | |
8f177c8e | 2901 | |
f85afd4e | 2902 | int hAlign, vAlign; |
2d66e025 | 2903 | GetColLabelAlignment( &hAlign, &vAlign ); |
60ff3b99 | 2904 | |
2d66e025 MB |
2905 | wxRect rect; |
2906 | rect.SetX( m_colRights[col] - m_colWidths[col] + 2 ); | |
2907 | rect.SetY( 2 ); | |
2908 | rect.SetWidth( m_colWidths[col] - 4 ); | |
2909 | rect.SetHeight( m_colLabelHeight - 4 ); | |
60ff3b99 | 2910 | DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign ); |
f85afd4e MB |
2911 | } |
2912 | ||
2913 | ||
2d66e025 MB |
2914 | void wxGrid::DrawTextRectangle( wxDC& dc, |
2915 | const wxString& value, | |
2916 | const wxRect& rect, | |
2917 | int horizAlign, | |
2918 | int vertAlign ) | |
f85afd4e | 2919 | { |
2d66e025 MB |
2920 | long textWidth, textHeight; |
2921 | long lineWidth, lineHeight; | |
2922 | wxArrayString lines; | |
f85afd4e | 2923 | |
2d66e025 MB |
2924 | dc.SetClippingRegion( rect ); |
2925 | StringToLines( value, lines ); | |
2926 | if ( lines.GetCount() ) | |
2927 | { | |
2928 | GetTextBoxSize( dc, lines, &textWidth, &textHeight ); | |
2929 | dc.GetTextExtent( lines[0], &lineWidth, &lineHeight ); | |
f85afd4e | 2930 | |
2d66e025 MB |
2931 | float x, y; |
2932 | switch ( horizAlign ) | |
2933 | { | |
2934 | case wxRIGHT: | |
2935 | x = rect.x + (rect.width - textWidth - 1); | |
2936 | break; | |
f85afd4e | 2937 | |
2d66e025 MB |
2938 | case wxCENTRE: |
2939 | x = rect.x + ((rect.width - textWidth)/2); | |
2940 | break; | |
f85afd4e | 2941 | |
2d66e025 MB |
2942 | case wxLEFT: |
2943 | default: | |
2944 | x = rect.x + 1; | |
2945 | break; | |
2946 | } | |
f85afd4e | 2947 | |
2d66e025 MB |
2948 | switch ( vertAlign ) |
2949 | { | |
2950 | case wxBOTTOM: | |
2951 | y = rect.y + (rect.height - textHeight - 1); | |
2952 | break; | |
f85afd4e MB |
2953 | |
2954 | case wxCENTRE: | |
8f177c8e | 2955 | y = rect.y + ((rect.height - textHeight)/2); |
f85afd4e MB |
2956 | break; |
2957 | ||
2958 | case wxTOP: | |
2959 | default: | |
8f177c8e | 2960 | y = rect.y + 1; |
f85afd4e MB |
2961 | break; |
2962 | } | |
2963 | ||
b540eb2b | 2964 | for ( size_t i = 0; i < lines.GetCount(); i++ ) |
f85afd4e MB |
2965 | { |
2966 | dc.DrawText( lines[i], (long)x, (long)y ); | |
2967 | y += lineHeight; | |
2968 | } | |
2969 | } | |
8f177c8e | 2970 | |
f85afd4e | 2971 | dc.DestroyClippingRegion(); |
f85afd4e MB |
2972 | } |
2973 | ||
2974 | ||
2975 | // Split multi line text up into an array of strings. Any existing | |
2976 | // contents of the string array are preserved. | |
2977 | // | |
2978 | void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) | |
2979 | { | |
2980 | // TODO: this won't work for WXMAC ? (lines end with '\r') | |
b540eb2b | 2981 | // => use wxTextFile functions then (VZ) |
f85afd4e MB |
2982 | int startPos = 0; |
2983 | int pos; | |
b540eb2b | 2984 | while ( startPos < (int)value.Length() ) |
f85afd4e MB |
2985 | { |
2986 | pos = value.Mid(startPos).Find( '\n' ); | |
2987 | if ( pos < 0 ) | |
2988 | { | |
2989 | break; | |
2990 | } | |
2991 | else if ( pos == 0 ) | |
2992 | { | |
2993 | lines.Add( wxEmptyString ); | |
2994 | } | |
2995 | else | |
2996 | { | |
2997 | if ( value[startPos+pos-1] == '\r' ) | |
2998 | { | |
2999 | lines.Add( value.Mid(startPos, pos-1) ); | |
3000 | } | |
3001 | else | |
3002 | { | |
3003 | lines.Add( value.Mid(startPos, pos) ); | |
3004 | } | |
3005 | } | |
3006 | startPos += pos+1; | |
3007 | } | |
b540eb2b | 3008 | if ( startPos < (int)value.Length() ) |
f85afd4e MB |
3009 | { |
3010 | lines.Add( value.Mid( startPos ) ); | |
3011 | } | |
3012 | } | |
3013 | ||
3014 | ||
3015 | void wxGrid::GetTextBoxSize( wxDC& dc, | |
3016 | wxArrayString& lines, | |
3017 | long *width, long *height ) | |
3018 | { | |
3019 | long w = 0; | |
3020 | long h = 0; | |
3021 | long lineW, lineH; | |
3022 | ||
b540eb2b | 3023 | size_t i; |
f85afd4e MB |
3024 | for ( i = 0; i < lines.GetCount(); i++ ) |
3025 | { | |
3026 | dc.GetTextExtent( lines[i], &lineW, &lineH ); | |
3027 | w = wxMax( w, lineW ); | |
3028 | h += lineH; | |
3029 | } | |
3030 | ||
3031 | *width = w; | |
3032 | *height = h; | |
3033 | } | |
3034 | ||
3035 | ||
3036 | // | |
2d66e025 | 3037 | // ------ Edit control functions |
f85afd4e MB |
3038 | // |
3039 | ||
2d66e025 MB |
3040 | |
3041 | void wxGrid::EnableEditing( bool edit ) | |
f85afd4e | 3042 | { |
2d66e025 MB |
3043 | // TODO: improve this ? |
3044 | // | |
3045 | if ( edit != m_editable ) | |
f85afd4e | 3046 | { |
2d66e025 | 3047 | m_editable = edit; |
1f1ce288 MB |
3048 | |
3049 | // TODO: extend this for other edit control types | |
3050 | // | |
3051 | if ( m_editCtrlType == wxGRID_TEXTCTRL ) | |
3052 | { | |
3053 | ((wxTextCtrl *)m_cellEditCtrl)->SetEditable( m_editable ); | |
3054 | } | |
f85afd4e | 3055 | } |
f85afd4e MB |
3056 | } |
3057 | ||
3058 | ||
1f1ce288 | 3059 | #if 0 // disabled for the moment - the cell control is always active |
2d66e025 | 3060 | void wxGrid::EnableCellEditControl( bool enable ) |
f85afd4e | 3061 | { |
2d66e025 MB |
3062 | if ( m_cellEditCtrl && |
3063 | enable != m_cellEditCtrlEnabled ) | |
f85afd4e | 3064 | { |
2d66e025 | 3065 | m_cellEditCtrlEnabled = enable; |
60ff3b99 | 3066 | |
2d66e025 | 3067 | if ( m_cellEditCtrlEnabled ) |
f85afd4e | 3068 | { |
2d66e025 MB |
3069 | SetEditControlValue(); |
3070 | ShowCellEditControl(); | |
2d66e025 MB |
3071 | } |
3072 | else | |
3073 | { | |
2d66e025 MB |
3074 | HideCellEditControl(); |
3075 | SaveEditControlValue(); | |
f85afd4e | 3076 | } |
f85afd4e | 3077 | } |
f85afd4e | 3078 | } |
1f1ce288 | 3079 | #endif |
f85afd4e MB |
3080 | |
3081 | ||
2d66e025 | 3082 | void wxGrid::ShowCellEditControl() |
f85afd4e | 3083 | { |
2d66e025 MB |
3084 | wxRect rect; |
3085 | ||
3086 | if ( IsCellEditControlEnabled() ) | |
f85afd4e | 3087 | { |
2d66e025 MB |
3088 | if ( !IsVisible( m_currentCellCoords ) ) |
3089 | { | |
3090 | return; | |
3091 | } | |
3092 | else | |
3093 | { | |
3094 | rect = CellToRect( m_currentCellCoords ); | |
3095 | ||
3096 | // convert to scrolled coords | |
3097 | // | |
3098 | int left, top, right, bottom; | |
3099 | CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top ); | |
3100 | CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom ); | |
60ff3b99 | 3101 | |
2d66e025 MB |
3102 | int cw, ch; |
3103 | m_gridWin->GetClientSize( &cw, &ch ); | |
1f1ce288 MB |
3104 | |
3105 | // Make the edit control large enough to allow for internal margins | |
3106 | // TODO: remove this if the text ctrl sizing is improved esp. for unix | |
3107 | // | |
58dd5b3b | 3108 | int extra; |
d2fdd8d2 | 3109 | #if defined(__WXMOTIF__) |
58dd5b3b MB |
3110 | if ( m_currentCellCoords.GetRow() == 0 || |
3111 | m_currentCellCoords.GetCol() == 0 ) | |
3112 | { | |
3113 | extra = 2; | |
3114 | } | |
3115 | else | |
3116 | { | |
3117 | extra = 4; | |
3118 | } | |
1f1ce288 | 3119 | #else |
58dd5b3b MB |
3120 | if ( m_currentCellCoords.GetRow() == 0 || |
3121 | m_currentCellCoords.GetCol() == 0 ) | |
3122 | { | |
3123 | extra = 1; | |
3124 | } | |
3125 | else | |
3126 | { | |
3127 | extra = 2; | |
3128 | } | |
1f1ce288 | 3129 | #endif |
d2fdd8d2 RR |
3130 | |
3131 | #if defined(__WXGTK__) | |
3132 | int top_diff = 0; | |
3133 | int left_diff = 0; | |
3134 | if (left != 0) left_diff++; | |
3135 | if (top != 0) top_diff++; | |
3136 | rect.SetLeft( left + left_diff ); | |
3137 | rect.SetTop( top + top_diff ); | |
3138 | rect.SetRight( rect.GetRight() - left_diff ); | |
3139 | rect.SetBottom( rect.GetBottom() - top_diff ); | |
3140 | #else | |
58dd5b3b MB |
3141 | rect.SetLeft( wxMax(0, left - extra) ); |
3142 | rect.SetTop( wxMax(0, top - extra) ); | |
3143 | rect.SetRight( rect.GetRight() + 2*extra ); | |
3144 | rect.SetBottom( rect.GetBottom() + 2*extra ); | |
d2fdd8d2 | 3145 | #endif |
f0102d2a | 3146 | |
2d66e025 MB |
3147 | m_cellEditCtrl->SetSize( rect ); |
3148 | m_cellEditCtrl->Show( TRUE ); | |
3149 | ||
3150 | switch ( m_editCtrlType ) | |
3151 | { | |
3152 | case wxGRID_TEXTCTRL: | |
3153 | ((wxTextCtrl *) m_cellEditCtrl)->SetInsertionPointEnd(); | |
3154 | break; | |
3155 | ||
3156 | case wxGRID_CHECKBOX: | |
3157 | // TODO: anything ??? | |
3158 | // | |
3159 | break; | |
3160 | ||
3161 | case wxGRID_CHOICE: | |
3162 | // TODO: anything ??? | |
3163 | // | |
3164 | break; | |
3165 | ||
3166 | case wxGRID_COMBOBOX: | |
3167 | // TODO: anything ??? | |
3168 | // | |
3169 | break; | |
3170 | } | |
3171 | ||
3172 | m_cellEditCtrl->SetFocus(); | |
3173 | } | |
f85afd4e MB |
3174 | } |
3175 | } | |
3176 | ||
3177 | ||
2d66e025 | 3178 | void wxGrid::HideCellEditControl() |
f85afd4e | 3179 | { |
2d66e025 | 3180 | if ( IsCellEditControlEnabled() ) |
f85afd4e | 3181 | { |
2d66e025 | 3182 | m_cellEditCtrl->Show( FALSE ); |
f85afd4e | 3183 | } |
2d66e025 | 3184 | } |
8f177c8e | 3185 | |
2d66e025 MB |
3186 | |
3187 | void wxGrid::SetEditControlValue( const wxString& value ) | |
3188 | { | |
f85afd4e MB |
3189 | if ( m_table ) |
3190 | { | |
2d66e025 MB |
3191 | wxString s; |
3192 | if ( !value ) | |
3193 | s = GetCellValue(m_currentCellCoords); | |
3194 | else | |
3195 | s = value; | |
f85afd4e | 3196 | |
2d66e025 MB |
3197 | if ( IsCellEditControlEnabled() ) |
3198 | { | |
3199 | switch ( m_editCtrlType ) | |
3f296516 | 3200 | { |
2d66e025 MB |
3201 | case wxGRID_TEXTCTRL: |
3202 | ((wxGridTextCtrl *)m_cellEditCtrl)->SetStartValue(s); | |
3203 | break; | |
8f177c8e | 3204 | |
2d66e025 MB |
3205 | case wxGRID_CHECKBOX: |
3206 | // TODO: implement this | |
3207 | // | |
3208 | break; | |
3f296516 | 3209 | |
2d66e025 MB |
3210 | case wxGRID_CHOICE: |
3211 | // TODO: implement this | |
3212 | // | |
3213 | break; | |
3214 | ||
3215 | case wxGRID_COMBOBOX: | |
3216 | // TODO: implement this | |
3217 | // | |
3218 | break; | |
3219 | } | |
3220 | } | |
f85afd4e MB |
3221 | } |
3222 | } | |
3223 | ||
8f177c8e | 3224 | |
2d66e025 MB |
3225 | void wxGrid::SaveEditControlValue() |
3226 | { | |
3227 | if ( m_table ) | |
f85afd4e | 3228 | { |
2d66e025 | 3229 | wxWindow *ctrl = (wxWindow *)NULL; |
8f177c8e | 3230 | |
2d66e025 | 3231 | if ( IsCellEditControlEnabled() ) |
3f296516 | 3232 | { |
2d66e025 MB |
3233 | ctrl = m_cellEditCtrl; |
3234 | } | |
2d66e025 MB |
3235 | else |
3236 | { | |
3237 | return; | |
3f296516 | 3238 | } |
8f177c8e | 3239 | |
2d66e025 MB |
3240 | bool valueChanged = FALSE; |
3241 | ||
3242 | switch ( m_editCtrlType ) | |
3243 | { | |
3244 | case wxGRID_TEXTCTRL: | |
3245 | valueChanged = (((wxGridTextCtrl *)ctrl)->GetValue() != | |
3246 | ((wxGridTextCtrl *)ctrl)->GetStartValue()); | |
3247 | SetCellValue( m_currentCellCoords, | |
3248 | ((wxTextCtrl *) ctrl)->GetValue() ); | |
3249 | break; | |
3250 | ||
3251 | case wxGRID_CHECKBOX: | |
3252 | // TODO: implement this | |
3253 | // | |
3254 | break; | |
3255 | ||
3256 | case wxGRID_CHOICE: | |
3257 | // TODO: implement this | |
3258 | // | |
3259 | break; | |
3260 | ||
3261 | case wxGRID_COMBOBOX: | |
3262 | // TODO: implement this | |
3263 | // | |
3264 | break; | |
3265 | } | |
3266 | ||
3267 | if ( valueChanged ) | |
3268 | { | |
3269 | SendEvent( EVT_GRID_CELL_CHANGE, | |
3270 | m_currentCellCoords.GetRow(), | |
3271 | m_currentCellCoords.GetCol() ); | |
3272 | } | |
f85afd4e MB |
3273 | } |
3274 | } | |
3275 | ||
2d66e025 MB |
3276 | |
3277 | // | |
60ff3b99 VZ |
3278 | // ------ Grid location functions |
3279 | // Note that all of these functions work with the logical coordinates of | |
2d66e025 MB |
3280 | // grid cells and labels so you will need to convert from device |
3281 | // coordinates for mouse events etc. | |
3282 | // | |
3283 | ||
3284 | void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords ) | |
f85afd4e | 3285 | { |
58dd5b3b MB |
3286 | int row = YToRow(y); |
3287 | int col = XToCol(x); | |
3288 | ||
3289 | if ( row == -1 || col == -1 ) | |
3290 | { | |
3291 | coords = wxGridNoCellCoords; | |
3292 | } | |
3293 | else | |
3294 | { | |
3295 | coords.Set( row, col ); | |
3296 | } | |
2d66e025 | 3297 | } |
f85afd4e | 3298 | |
8f177c8e | 3299 | |
2d66e025 MB |
3300 | int wxGrid::YToRow( int y ) |
3301 | { | |
3302 | int i; | |
8f177c8e | 3303 | |
2d66e025 | 3304 | for ( i = 0; i < m_numRows; i++ ) |
f85afd4e | 3305 | { |
2d66e025 | 3306 | if ( y < m_rowBottoms[i] ) return i; |
f85afd4e | 3307 | } |
2d66e025 MB |
3308 | |
3309 | return -1; | |
f85afd4e MB |
3310 | } |
3311 | ||
2d66e025 MB |
3312 | |
3313 | int wxGrid::XToCol( int x ) | |
f85afd4e | 3314 | { |
2d66e025 | 3315 | int i; |
8f177c8e | 3316 | |
2d66e025 | 3317 | for ( i = 0; i < m_numCols; i++ ) |
f85afd4e | 3318 | { |
2d66e025 | 3319 | if ( x < m_colRights[i] ) return i; |
f85afd4e MB |
3320 | } |
3321 | ||
2d66e025 MB |
3322 | return -1; |
3323 | } | |
8f177c8e | 3324 | |
2d66e025 MB |
3325 | |
3326 | // return the row number that that the y coord is near the edge of, or | |
3327 | // -1 if not near an edge | |
3328 | // | |
3329 | int wxGrid::YToEdgeOfRow( int y ) | |
3330 | { | |
3331 | int i, d; | |
3332 | ||
3333 | for ( i = 0; i < m_numRows; i++ ) | |
3334 | { | |
3335 | if ( m_rowHeights[i] > WXGRID_LABEL_EDGE_ZONE ) | |
f85afd4e | 3336 | { |
2d66e025 | 3337 | d = abs( y - m_rowBottoms[i] ); |
3f296516 | 3338 | { |
2d66e025 | 3339 | if ( d < WXGRID_LABEL_EDGE_ZONE ) return i; |
3f296516 | 3340 | } |
f85afd4e | 3341 | } |
f85afd4e | 3342 | } |
2d66e025 MB |
3343 | |
3344 | return -1; | |
3345 | } | |
3346 | ||
3347 | ||
3348 | // return the col number that that the x coord is near the edge of, or | |
3349 | // -1 if not near an edge | |
3350 | // | |
3351 | int wxGrid::XToEdgeOfCol( int x ) | |
3352 | { | |
3353 | int i, d; | |
3354 | ||
3355 | for ( i = 0; i < m_numCols; i++ ) | |
f85afd4e | 3356 | { |
2d66e025 MB |
3357 | if ( m_colWidths[i] > WXGRID_LABEL_EDGE_ZONE ) |
3358 | { | |
3359 | d = abs( x - m_colRights[i] ); | |
3360 | { | |
3361 | if ( d < WXGRID_LABEL_EDGE_ZONE ) return i; | |
3362 | } | |
3363 | } | |
f85afd4e | 3364 | } |
2d66e025 MB |
3365 | |
3366 | return -1; | |
f85afd4e MB |
3367 | } |
3368 | ||
2d66e025 MB |
3369 | |
3370 | wxRect wxGrid::CellToRect( int row, int col ) | |
f85afd4e | 3371 | { |
2d66e025 | 3372 | wxRect rect( -1, -1, -1, -1 ); |
f85afd4e | 3373 | |
2d66e025 MB |
3374 | if ( row >= 0 && row < m_numRows && |
3375 | col >= 0 && col < m_numCols ) | |
f85afd4e | 3376 | { |
2d66e025 MB |
3377 | rect.x = m_colRights[col] - m_colWidths[col]; |
3378 | rect.y = m_rowBottoms[row] - m_rowHeights[row]; | |
3379 | rect.width = m_colWidths[col]; | |
3380 | rect.height = m_rowHeights[ row ]; | |
f85afd4e MB |
3381 | } |
3382 | ||
2d66e025 MB |
3383 | return rect; |
3384 | } | |
3385 | ||
3386 | ||
3387 | bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) | |
3388 | { | |
3389 | // get the cell rectangle in logical coords | |
3390 | // | |
3391 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 3392 | |
2d66e025 MB |
3393 | // convert to device coords |
3394 | // | |
3395 | int left, top, right, bottom; | |
3396 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
3397 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 3398 | |
2d66e025 MB |
3399 | // check against the client area of the grid window |
3400 | // | |
3401 | int cw, ch; | |
3402 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 3403 | |
2d66e025 | 3404 | if ( wholeCellVisible ) |
f85afd4e | 3405 | { |
2d66e025 | 3406 | // is the cell wholly visible ? |
8f177c8e | 3407 | // |
2d66e025 MB |
3408 | return ( left >= 0 && right <= cw && |
3409 | top >= 0 && bottom <= ch ); | |
3410 | } | |
3411 | else | |
3412 | { | |
3413 | // is the cell partly visible ? | |
3414 | // | |
3415 | return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) && | |
3416 | ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) ); | |
3417 | } | |
3418 | } | |
3419 | ||
3420 | ||
3421 | // make the specified cell location visible by doing a minimal amount | |
3422 | // of scrolling | |
3423 | // | |
3424 | void wxGrid::MakeCellVisible( int row, int col ) | |
3425 | { | |
3426 | int i; | |
60ff3b99 | 3427 | int xpos = -1, ypos = -1; |
2d66e025 MB |
3428 | |
3429 | if ( row >= 0 && row < m_numRows && | |
3430 | col >= 0 && col < m_numCols ) | |
3431 | { | |
3432 | // get the cell rectangle in logical coords | |
3433 | // | |
3434 | wxRect r( CellToRect( row, col ) ); | |
60ff3b99 | 3435 | |
2d66e025 MB |
3436 | // convert to device coords |
3437 | // | |
3438 | int left, top, right, bottom; | |
3439 | CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top ); | |
3440 | CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom ); | |
60ff3b99 | 3441 | |
2d66e025 MB |
3442 | int cw, ch; |
3443 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 3444 | |
2d66e025 | 3445 | if ( top < 0 ) |
3f296516 | 3446 | { |
2d66e025 | 3447 | ypos = r.GetTop(); |
3f296516 | 3448 | } |
2d66e025 MB |
3449 | else if ( bottom > ch ) |
3450 | { | |
3451 | int h = r.GetHeight(); | |
3452 | ypos = r.GetTop(); | |
3453 | for ( i = row-1; i >= 0; i-- ) | |
3454 | { | |
3455 | if ( h + m_rowHeights[i] > ch ) break; | |
3456 | ||
3457 | h += m_rowHeights[i]; | |
3458 | ypos -= m_rowHeights[i]; | |
3459 | } | |
f0102d2a VZ |
3460 | |
3461 | // we divide it later by GRID_SCROLL_LINE, make sure that we don't | |
3462 | // have rounding errors (this is important, because if we do, we | |
3463 | // might not scroll at all and some cells won't be redrawn) | |
3464 | ypos += GRID_SCROLL_LINE / 2; | |
2d66e025 MB |
3465 | } |
3466 | ||
3467 | if ( left < 0 ) | |
3468 | { | |
3469 | xpos = r.GetLeft(); | |
3470 | } | |
3471 | else if ( right > cw ) | |
3472 | { | |
3473 | int w = r.GetWidth(); | |
3474 | xpos = r.GetLeft(); | |
3475 | for ( i = col-1; i >= 0; i-- ) | |
3476 | { | |
3477 | if ( w + m_colWidths[i] > cw ) break; | |
3478 | ||
3479 | w += m_colWidths[i]; | |
3480 | xpos -= m_colWidths[i]; | |
3481 | } | |
f0102d2a VZ |
3482 | |
3483 | // see comment for ypos above | |
3484 | xpos += GRID_SCROLL_LINE / 2; | |
2d66e025 MB |
3485 | } |
3486 | ||
3487 | if ( xpos != -1 || ypos != -1 ) | |
3488 | { | |
f0102d2a VZ |
3489 | if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE; |
3490 | if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE; | |
2d66e025 MB |
3491 | Scroll( xpos, ypos ); |
3492 | AdjustScrollbars(); | |
3493 | } | |
3494 | } | |
3495 | } | |
3496 | ||
3497 | ||
3498 | // | |
3499 | // ------ Grid cursor movement functions | |
3500 | // | |
3501 | ||
3502 | bool wxGrid::MoveCursorUp() | |
3503 | { | |
3504 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
3505 | m_currentCellCoords.GetRow() > 0 ) | |
3506 | { | |
3507 | MakeCellVisible( m_currentCellCoords.GetRow() - 1, | |
3508 | m_currentCellCoords.GetCol() ); | |
60ff3b99 | 3509 | |
2d66e025 MB |
3510 | SetCurrentCell( m_currentCellCoords.GetRow() - 1, |
3511 | m_currentCellCoords.GetCol() ); | |
3512 | ||
8f177c8e | 3513 | return TRUE; |
f85afd4e | 3514 | } |
2d66e025 MB |
3515 | |
3516 | return FALSE; | |
3517 | } | |
3518 | ||
3519 | ||
3520 | bool wxGrid::MoveCursorDown() | |
3521 | { | |
3522 | // TODO: allow for scrolling | |
3523 | // | |
3524 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
3525 | m_currentCellCoords.GetRow() < m_numRows-1 ) | |
f85afd4e | 3526 | { |
2d66e025 MB |
3527 | MakeCellVisible( m_currentCellCoords.GetRow() + 1, |
3528 | m_currentCellCoords.GetCol() ); | |
60ff3b99 | 3529 | |
2d66e025 MB |
3530 | SetCurrentCell( m_currentCellCoords.GetRow() + 1, |
3531 | m_currentCellCoords.GetCol() ); | |
3532 | ||
3533 | return TRUE; | |
f85afd4e | 3534 | } |
2d66e025 MB |
3535 | |
3536 | return FALSE; | |
f85afd4e MB |
3537 | } |
3538 | ||
2d66e025 MB |
3539 | |
3540 | bool wxGrid::MoveCursorLeft() | |
f85afd4e | 3541 | { |
2d66e025 MB |
3542 | if ( m_currentCellCoords != wxGridNoCellCoords && |
3543 | m_currentCellCoords.GetCol() > 0 ) | |
3544 | { | |
3545 | MakeCellVisible( m_currentCellCoords.GetRow(), | |
3546 | m_currentCellCoords.GetCol() - 1 ); | |
60ff3b99 | 3547 | |
2d66e025 MB |
3548 | SetCurrentCell( m_currentCellCoords.GetRow(), |
3549 | m_currentCellCoords.GetCol() - 1 ); | |
8f177c8e | 3550 | |
2d66e025 MB |
3551 | return TRUE; |
3552 | } | |
3553 | ||
3554 | return FALSE; | |
3555 | } | |
3556 | ||
3557 | ||
3558 | bool wxGrid::MoveCursorRight() | |
3559 | { | |
3560 | if ( m_currentCellCoords != wxGridNoCellCoords && | |
3561 | m_currentCellCoords.GetCol() < m_numCols - 1 ) | |
f85afd4e | 3562 | { |
2d66e025 MB |
3563 | MakeCellVisible( m_currentCellCoords.GetRow(), |
3564 | m_currentCellCoords.GetCol() + 1 ); | |
60ff3b99 | 3565 | |
2d66e025 MB |
3566 | SetCurrentCell( m_currentCellCoords.GetRow(), |
3567 | m_currentCellCoords.GetCol() + 1 ); | |
3568 | ||
3569 | return TRUE; | |
f85afd4e | 3570 | } |
8f177c8e | 3571 | |
2d66e025 MB |
3572 | return FALSE; |
3573 | } | |
3574 | ||
3575 | ||
3576 | bool wxGrid::MovePageUp() | |
3577 | { | |
3578 | if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE; | |
60ff3b99 | 3579 | |
2d66e025 MB |
3580 | int row = m_currentCellCoords.GetRow(); |
3581 | if ( row > 0 ) | |
f85afd4e | 3582 | { |
2d66e025 MB |
3583 | int cw, ch; |
3584 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 3585 | |
2d66e025 MB |
3586 | int y = m_rowBottoms[ row ] - m_rowHeights[ row ]; |
3587 | int newRow = YToRow( y - ch + 1 ); | |
3588 | if ( newRow == -1 ) | |
3589 | { | |
3590 | newRow = 0; | |
3591 | } | |
60ff3b99 | 3592 | else if ( newRow == row ) |
2d66e025 MB |
3593 | { |
3594 | newRow = row - 1; | |
3595 | } | |
8f177c8e | 3596 | |
2d66e025 MB |
3597 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); |
3598 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
60ff3b99 | 3599 | |
f85afd4e MB |
3600 | return TRUE; |
3601 | } | |
2d66e025 MB |
3602 | |
3603 | return FALSE; | |
3604 | } | |
3605 | ||
3606 | bool wxGrid::MovePageDown() | |
3607 | { | |
3608 | if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE; | |
60ff3b99 | 3609 | |
2d66e025 MB |
3610 | int row = m_currentCellCoords.GetRow(); |
3611 | if ( row < m_numRows ) | |
f85afd4e | 3612 | { |
2d66e025 MB |
3613 | int cw, ch; |
3614 | m_gridWin->GetClientSize( &cw, &ch ); | |
60ff3b99 | 3615 | |
2d66e025 MB |
3616 | int y = m_rowBottoms[ row ] - m_rowHeights[ row ]; |
3617 | int newRow = YToRow( y + ch ); | |
3618 | if ( newRow == -1 ) | |
3619 | { | |
3620 | newRow = m_numRows - 1; | |
3621 | } | |
60ff3b99 | 3622 | else if ( newRow == row ) |
2d66e025 MB |
3623 | { |
3624 | newRow = row + 1; | |
3625 | } | |
3626 | ||
3627 | MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); | |
3628 | SetCurrentCell( newRow, m_currentCellCoords.GetCol() ); | |
60ff3b99 | 3629 | |
2d66e025 | 3630 | return TRUE; |
f85afd4e | 3631 | } |
2d66e025 MB |
3632 | |
3633 | return FALSE; | |
f85afd4e | 3634 | } |
8f177c8e | 3635 | |
2d66e025 MB |
3636 | bool wxGrid::MoveCursorUpBlock() |
3637 | { | |
3638 | if ( m_table && | |
3639 | m_currentCellCoords != wxGridNoCellCoords && | |
3640 | m_currentCellCoords.GetRow() > 0 ) | |
3641 | { | |
3642 | int row = m_currentCellCoords.GetRow(); | |
3643 | int col = m_currentCellCoords.GetCol(); | |
3644 | ||
3645 | if ( m_table->IsEmptyCell(row, col) ) | |
3646 | { | |
3647 | // starting in an empty cell: find the next block of | |
3648 | // non-empty cells | |
3649 | // | |
3650 | while ( row > 0 ) | |
3651 | { | |
3652 | row-- ; | |
3653 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
3654 | } | |
3655 | } | |
3656 | else if ( m_table->IsEmptyCell(row-1, col) ) | |
3657 | { | |
3658 | // starting at the top of a block: find the next block | |
3659 | // | |
3660 | row--; | |
3661 | while ( row > 0 ) | |
3662 | { | |
3663 | row-- ; | |
3664 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
3665 | } | |
3666 | } | |
3667 | else | |
3668 | { | |
3669 | // starting within a block: find the top of the block | |
3670 | // | |
3671 | while ( row > 0 ) | |
3672 | { | |
3673 | row-- ; | |
3674 | if ( m_table->IsEmptyCell(row, col) ) | |
3675 | { | |
3676 | row++ ; | |
3677 | break; | |
3678 | } | |
3679 | } | |
3680 | } | |
f85afd4e | 3681 | |
2d66e025 MB |
3682 | MakeCellVisible( row, col ); |
3683 | SetCurrentCell( row, col ); | |
f85afd4e | 3684 | |
2d66e025 MB |
3685 | return TRUE; |
3686 | } | |
f85afd4e | 3687 | |
2d66e025 MB |
3688 | return FALSE; |
3689 | } | |
f85afd4e | 3690 | |
2d66e025 | 3691 | bool wxGrid::MoveCursorDownBlock() |
f85afd4e | 3692 | { |
2d66e025 MB |
3693 | if ( m_table && |
3694 | m_currentCellCoords != wxGridNoCellCoords && | |
3695 | m_currentCellCoords.GetRow() < m_numRows-1 ) | |
f85afd4e | 3696 | { |
2d66e025 MB |
3697 | int row = m_currentCellCoords.GetRow(); |
3698 | int col = m_currentCellCoords.GetCol(); | |
3699 | ||
3700 | if ( m_table->IsEmptyCell(row, col) ) | |
3701 | { | |
3702 | // starting in an empty cell: find the next block of | |
3703 | // non-empty cells | |
3704 | // | |
3705 | while ( row < m_numRows-1 ) | |
3706 | { | |
3707 | row++ ; | |
3708 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
3709 | } | |
3710 | } | |
3711 | else if ( m_table->IsEmptyCell(row+1, col) ) | |
3712 | { | |
3713 | // starting at the bottom of a block: find the next block | |
3714 | // | |
3715 | row++; | |
3716 | while ( row < m_numRows-1 ) | |
3717 | { | |
3718 | row++ ; | |
3719 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
3720 | } | |
3721 | } | |
3722 | else | |
3723 | { | |
3724 | // starting within a block: find the bottom of the block | |
3725 | // | |
3726 | while ( row < m_numRows-1 ) | |
3727 | { | |
3728 | row++ ; | |
3729 | if ( m_table->IsEmptyCell(row, col) ) | |
3730 | { | |
3731 | row-- ; | |
3732 | break; | |
3733 | } | |
3734 | } | |
3735 | } | |
3736 | ||
3737 | MakeCellVisible( row, col ); | |
3738 | SetCurrentCell( row, col ); | |
3739 | ||
3740 | return TRUE; | |
f85afd4e | 3741 | } |
f85afd4e | 3742 | |
2d66e025 MB |
3743 | return FALSE; |
3744 | } | |
f85afd4e | 3745 | |
2d66e025 | 3746 | bool wxGrid::MoveCursorLeftBlock() |
f85afd4e | 3747 | { |
2d66e025 MB |
3748 | if ( m_table && |
3749 | m_currentCellCoords != wxGridNoCellCoords && | |
3750 | m_currentCellCoords.GetCol() > 0 ) | |
f85afd4e | 3751 | { |
2d66e025 MB |
3752 | int row = m_currentCellCoords.GetRow(); |
3753 | int col = m_currentCellCoords.GetCol(); | |
3754 | ||
3755 | if ( m_table->IsEmptyCell(row, col) ) | |
3756 | { | |
3757 | // starting in an empty cell: find the next block of | |
3758 | // non-empty cells | |
3759 | // | |
3760 | while ( col > 0 ) | |
3761 | { | |
3762 | col-- ; | |
3763 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
3764 | } | |
3765 | } | |
3766 | else if ( m_table->IsEmptyCell(row, col-1) ) | |
3767 | { | |
3768 | // starting at the left of a block: find the next block | |
3769 | // | |
3770 | col--; | |
3771 | while ( col > 0 ) | |
3772 | { | |
3773 | col-- ; | |
3774 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
3775 | } | |
3776 | } | |
3777 | else | |
3778 | { | |
3779 | // starting within a block: find the left of the block | |
3780 | // | |
3781 | while ( col > 0 ) | |
3782 | { | |
3783 | col-- ; | |
3784 | if ( m_table->IsEmptyCell(row, col) ) | |
3785 | { | |
3786 | col++ ; | |
3787 | break; | |
3788 | } | |
3789 | } | |
3790 | } | |
f85afd4e | 3791 | |
2d66e025 MB |
3792 | MakeCellVisible( row, col ); |
3793 | SetCurrentCell( row, col ); | |
8f177c8e | 3794 | |
2d66e025 | 3795 | return TRUE; |
f85afd4e | 3796 | } |
2d66e025 MB |
3797 | |
3798 | return FALSE; | |
f85afd4e MB |
3799 | } |
3800 | ||
2d66e025 | 3801 | bool wxGrid::MoveCursorRightBlock() |
f85afd4e | 3802 | { |
2d66e025 MB |
3803 | if ( m_table && |
3804 | m_currentCellCoords != wxGridNoCellCoords && | |
3805 | m_currentCellCoords.GetCol() < m_numCols-1 ) | |
f85afd4e | 3806 | { |
2d66e025 MB |
3807 | int row = m_currentCellCoords.GetRow(); |
3808 | int col = m_currentCellCoords.GetCol(); | |
8f177c8e | 3809 | |
2d66e025 MB |
3810 | if ( m_table->IsEmptyCell(row, col) ) |
3811 | { | |
3812 | // starting in an empty cell: find the next block of | |
3813 | // non-empty cells | |
3814 | // | |
3815 | while ( col < m_numCols-1 ) | |
3816 | { | |
3817 | col++ ; | |
3818 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
3819 | } | |
3820 | } | |
3821 | else if ( m_table->IsEmptyCell(row, col+1) ) | |
3822 | { | |
3823 | // starting at the right of a block: find the next block | |
3824 | // | |
3825 | col++; | |
3826 | while ( col < m_numCols-1 ) | |
3827 | { | |
3828 | col++ ; | |
3829 | if ( !(m_table->IsEmptyCell(row, col)) ) break; | |
3830 | } | |
3831 | } | |
3832 | else | |
3833 | { | |
3834 | // starting within a block: find the right of the block | |
3835 | // | |
3836 | while ( col < m_numCols-1 ) | |
3837 | { | |
3838 | col++ ; | |
3839 | if ( m_table->IsEmptyCell(row, col) ) | |
3840 | { | |
3841 | col-- ; | |
3842 | break; | |
3843 | } | |
3844 | } | |
3845 | } | |
8f177c8e | 3846 | |
2d66e025 MB |
3847 | MakeCellVisible( row, col ); |
3848 | SetCurrentCell( row, col ); | |
f85afd4e | 3849 | |
2d66e025 | 3850 | return TRUE; |
f85afd4e | 3851 | } |
2d66e025 MB |
3852 | |
3853 | return FALSE; | |
f85afd4e MB |
3854 | } |
3855 | ||
3856 | ||
2d66e025 | 3857 | |
f85afd4e | 3858 | // |
2d66e025 | 3859 | // ------ Label values and formatting |
f85afd4e MB |
3860 | // |
3861 | ||
3862 | void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) | |
3863 | { | |
3864 | *horiz = m_rowLabelHorizAlign; | |
3865 | *vert = m_rowLabelVertAlign; | |
3866 | } | |
3867 | ||
3868 | void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) | |
3869 | { | |
3870 | *horiz = m_colLabelHorizAlign; | |
3871 | *vert = m_colLabelVertAlign; | |
3872 | } | |
3873 | ||
3874 | wxString wxGrid::GetRowLabelValue( int row ) | |
3875 | { | |
3876 | if ( m_table ) | |
3877 | { | |
3878 | return m_table->GetRowLabelValue( row ); | |
3879 | } | |
3880 | else | |
3881 | { | |
3882 | wxString s; | |
3883 | s << row; | |
3884 | return s; | |
3885 | } | |
3886 | } | |
3887 | ||
3888 | wxString wxGrid::GetColLabelValue( int col ) | |
3889 | { | |
3890 | if ( m_table ) | |
3891 | { | |
3892 | return m_table->GetColLabelValue( col ); | |
3893 | } | |
3894 | else | |
3895 | { | |
3896 | wxString s; | |
3897 | s << col; | |
3898 | return s; | |
3899 | } | |
3900 | } | |
3901 | ||
2e8cd977 | 3902 | |
f85afd4e MB |
3903 | void wxGrid::SetRowLabelSize( int width ) |
3904 | { | |
2e8cd977 MB |
3905 | width = wxMax( width, 0 ); |
3906 | if ( width != m_rowLabelWidth ) | |
3907 | { | |
2e8cd977 MB |
3908 | if ( width == 0 ) |
3909 | { | |
3910 | m_rowLabelWin->Show( FALSE ); | |
7807d81c | 3911 | m_cornerLabelWin->Show( FALSE ); |
2e8cd977 | 3912 | } |
7807d81c | 3913 | else if ( m_rowLabelWidth == 0 ) |
2e8cd977 | 3914 | { |
7807d81c MB |
3915 | m_rowLabelWin->Show( TRUE ); |
3916 | if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE ); | |
2e8cd977 | 3917 | } |
7807d81c | 3918 | |
2e8cd977 | 3919 | m_rowLabelWidth = width; |
7807d81c MB |
3920 | CalcWindowSizes(); |
3921 | Refresh( TRUE ); | |
2e8cd977 | 3922 | } |
f85afd4e MB |
3923 | } |
3924 | ||
2e8cd977 | 3925 | |
f85afd4e MB |
3926 | void wxGrid::SetColLabelSize( int height ) |
3927 | { | |
7807d81c | 3928 | height = wxMax( height, 0 ); |
2e8cd977 MB |
3929 | if ( height != m_colLabelHeight ) |
3930 | { | |
2e8cd977 MB |
3931 | if ( height == 0 ) |
3932 | { | |
2e8cd977 | 3933 | m_colLabelWin->Show( FALSE ); |
7807d81c | 3934 | m_cornerLabelWin->Show( FALSE ); |
2e8cd977 | 3935 | } |
7807d81c | 3936 | else if ( m_colLabelHeight == 0 ) |
2e8cd977 | 3937 | { |
7807d81c MB |
3938 | m_colLabelWin->Show( TRUE ); |
3939 | if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE ); | |
2e8cd977 | 3940 | } |
7807d81c | 3941 | |
2e8cd977 | 3942 | m_colLabelHeight = height; |
7807d81c MB |
3943 | CalcWindowSizes(); |
3944 | Refresh( TRUE ); | |
2e8cd977 | 3945 | } |
f85afd4e MB |
3946 | } |
3947 | ||
2e8cd977 | 3948 | |
f85afd4e MB |
3949 | void wxGrid::SetLabelBackgroundColour( const wxColour& colour ) |
3950 | { | |
2d66e025 MB |
3951 | if ( m_labelBackgroundColour != colour ) |
3952 | { | |
3953 | m_labelBackgroundColour = colour; | |
3954 | m_rowLabelWin->SetBackgroundColour( colour ); | |
3955 | m_colLabelWin->SetBackgroundColour( colour ); | |
3956 | m_cornerLabelWin->SetBackgroundColour( colour ); | |
3957 | ||
3958 | if ( !GetBatchCount() ) | |
3959 | { | |
3960 | m_rowLabelWin->Refresh(); | |
3961 | m_colLabelWin->Refresh(); | |
3962 | m_cornerLabelWin->Refresh(); | |
3963 | } | |
3964 | } | |
f85afd4e MB |
3965 | } |
3966 | ||
3967 | void wxGrid::SetLabelTextColour( const wxColour& colour ) | |
3968 | { | |
2d66e025 MB |
3969 | if ( m_labelTextColour != colour ) |
3970 | { | |
3971 | m_labelTextColour = colour; | |
3972 | if ( !GetBatchCount() ) | |
3973 | { | |
3974 | m_rowLabelWin->Refresh(); | |
3975 | m_colLabelWin->Refresh(); | |
3976 | } | |
3977 | } | |
f85afd4e MB |
3978 | } |
3979 | ||
3980 | void wxGrid::SetLabelFont( const wxFont& font ) | |
3981 | { | |
3982 | m_labelFont = font; | |
2d66e025 MB |
3983 | if ( !GetBatchCount() ) |
3984 | { | |
3985 | m_rowLabelWin->Refresh(); | |
3986 | m_colLabelWin->Refresh(); | |
3987 | } | |
f85afd4e MB |
3988 | } |
3989 | ||
3990 | void wxGrid::SetRowLabelAlignment( int horiz, int vert ) | |
3991 | { | |
3992 | if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT ) | |
3993 | { | |
3994 | m_rowLabelHorizAlign = horiz; | |
3995 | } | |
8f177c8e | 3996 | |
f85afd4e MB |
3997 | if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM ) |
3998 | { | |
3999 | m_rowLabelVertAlign = vert; | |
4000 | } | |
4001 | ||
2d66e025 MB |
4002 | if ( !GetBatchCount() ) |
4003 | { | |
4004 | m_rowLabelWin->Refresh(); | |
60ff3b99 | 4005 | } |
f85afd4e MB |
4006 | } |
4007 | ||
4008 | void wxGrid::SetColLabelAlignment( int horiz, int vert ) | |
4009 | { | |
4010 | if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT ) | |
4011 | { | |
4012 | m_colLabelHorizAlign = horiz; | |
4013 | } | |
8f177c8e | 4014 | |
f85afd4e MB |
4015 | if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM ) |
4016 | { | |
4017 | m_colLabelVertAlign = vert; | |
4018 | } | |
4019 | ||
2d66e025 MB |
4020 | if ( !GetBatchCount() ) |
4021 | { | |
2d66e025 | 4022 | m_colLabelWin->Refresh(); |
60ff3b99 | 4023 | } |
f85afd4e MB |
4024 | } |
4025 | ||
4026 | void wxGrid::SetRowLabelValue( int row, const wxString& s ) | |
4027 | { | |
4028 | if ( m_table ) | |
4029 | { | |
4030 | m_table->SetRowLabelValue( row, s ); | |
2d66e025 MB |
4031 | if ( !GetBatchCount() ) |
4032 | { | |
70c7a608 SN |
4033 | wxRect rect = CellToRect( row, 0); |
4034 | if ( rect.height > 0 ) | |
4035 | { | |
cb309039 | 4036 | CalcScrolledPosition(0, rect.y, &rect.x, &rect.y); |
70c7a608 SN |
4037 | rect.x = m_left; |
4038 | rect.width = m_rowLabelWidth; | |
4039 | m_rowLabelWin->Refresh( TRUE, &rect ); | |
4040 | } | |
2d66e025 | 4041 | } |
f85afd4e MB |
4042 | } |
4043 | } | |
4044 | ||
4045 | void wxGrid::SetColLabelValue( int col, const wxString& s ) | |
4046 | { | |
4047 | if ( m_table ) | |
4048 | { | |
4049 | m_table->SetColLabelValue( col, s ); | |
2d66e025 MB |
4050 | if ( !GetBatchCount() ) |
4051 | { | |
70c7a608 SN |
4052 | wxRect rect = CellToRect( 0, col ); |
4053 | if ( rect.width > 0 ) | |
4054 | { | |
cb309039 | 4055 | CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y); |
70c7a608 SN |
4056 | rect.y = m_top; |
4057 | rect.height = m_colLabelHeight; | |
4058 | m_colLabelWin->Refresh( TRUE, &rect ); | |
4059 | } | |
2d66e025 | 4060 | } |
f85afd4e MB |
4061 | } |
4062 | } | |
4063 | ||
4064 | void wxGrid::SetGridLineColour( const wxColour& colour ) | |
4065 | { | |
2d66e025 MB |
4066 | if ( m_gridLineColour != colour ) |
4067 | { | |
4068 | m_gridLineColour = colour; | |
60ff3b99 | 4069 | |
2d66e025 MB |
4070 | wxClientDC dc( m_gridWin ); |
4071 | PrepareDC( dc ); | |
c6a51dcd | 4072 | DrawAllGridLines( dc, wxRegion() ); |
2d66e025 | 4073 | } |
f85afd4e MB |
4074 | } |
4075 | ||
4076 | void wxGrid::EnableGridLines( bool enable ) | |
4077 | { | |
4078 | if ( enable != m_gridLinesEnabled ) | |
4079 | { | |
4080 | m_gridLinesEnabled = enable; | |
2d66e025 MB |
4081 | |
4082 | if ( !GetBatchCount() ) | |
4083 | { | |
4084 | if ( enable ) | |
4085 | { | |
4086 | wxClientDC dc( m_gridWin ); | |
4087 | PrepareDC( dc ); | |
c6a51dcd | 4088 | DrawAllGridLines( dc, wxRegion() ); |
2d66e025 MB |
4089 | } |
4090 | else | |
4091 | { | |
4092 | m_gridWin->Refresh(); | |
4093 | } | |
4094 | } | |
f85afd4e MB |
4095 | } |
4096 | } | |
4097 | ||
4098 | ||
4099 | int wxGrid::GetDefaultRowSize() | |
4100 | { | |
4101 | return m_defaultRowHeight; | |
4102 | } | |
4103 | ||
4104 | int wxGrid::GetRowSize( int row ) | |
4105 | { | |
4106 | if ( row >= 0 && row < m_numRows ) | |
4107 | return m_rowHeights[row]; | |
4108 | else | |
4109 | return 0; // TODO: log an error here | |
4110 | } | |
4111 | ||
4112 | int wxGrid::GetDefaultColSize() | |
4113 | { | |
4114 | return m_defaultColWidth; | |
4115 | } | |
4116 | ||
4117 | int wxGrid::GetColSize( int col ) | |
4118 | { | |
4119 | if ( col >= 0 && col < m_numCols ) | |
4120 | return m_colWidths[col]; | |
4121 | else | |
4122 | return 0; // TODO: log an error here | |
4123 | } | |
4124 | ||
4125 | wxColour wxGrid::GetDefaultCellBackgroundColour() | |
4126 | { | |
4127 | // TODO: replace this temp test code | |
4128 | // | |
4129 | return wxColour( 255, 255, 255 ); | |
4130 | } | |
4131 | ||
af111fc3 | 4132 | wxColour wxGrid::GetCellBackgroundColour( int WXUNUSED(row), int WXUNUSED(col) ) |
f85afd4e MB |
4133 | { |
4134 | // TODO: replace this temp test code | |
4135 | // | |
8f177c8e | 4136 | return wxColour( 255, 255, 255 ); |
f85afd4e MB |
4137 | } |
4138 | ||
4139 | wxColour wxGrid::GetDefaultCellTextColour() | |
4140 | { | |
4141 | // TODO: replace this temp test code | |
4142 | // | |
8f177c8e | 4143 | return wxColour( 0, 0, 0 ); |
f85afd4e MB |
4144 | } |
4145 | ||
af111fc3 | 4146 | wxColour wxGrid::GetCellTextColour( int WXUNUSED(row), int WXUNUSED(col) ) |
f85afd4e MB |
4147 | { |
4148 | // TODO: replace this temp test code | |
4149 | // | |
8f177c8e | 4150 | return wxColour( 0, 0, 0 ); |
f85afd4e MB |
4151 | } |
4152 | ||
4153 | ||
f85afd4e MB |
4154 | wxFont wxGrid::GetDefaultCellFont() |
4155 | { | |
4156 | return m_defaultCellFont; | |
4157 | } | |
4158 | ||
af111fc3 | 4159 | wxFont wxGrid::GetCellFont( int WXUNUSED(row), int WXUNUSED(col) ) |
f85afd4e MB |
4160 | { |
4161 | // TODO: replace this temp test code | |
4162 | // | |
4163 | return m_defaultCellFont; | |
4164 | } | |
4165 | ||
4166 | void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) | |
4167 | { | |
4168 | // TODO: replace this temp test code | |
4169 | // | |
4170 | *horiz = wxLEFT; | |
4171 | *vert = wxTOP; | |
4172 | } | |
4173 | ||
af111fc3 | 4174 | void wxGrid::GetCellAlignment( int WXUNUSED(row), int WXUNUSED(col), int *horiz, int *vert ) |
f85afd4e MB |
4175 | { |
4176 | // TODO: replace this temp test code | |
4177 | // | |
4178 | *horiz = wxLEFT; | |
4179 | *vert = wxTOP; | |
4180 | } | |
4181 | ||
4182 | void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) | |
4183 | { | |
4184 | m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT ); | |
4185 | ||
4186 | if ( resizeExistingRows ) | |
4187 | { | |
f85afd4e | 4188 | int row; |
2d66e025 | 4189 | int bottom = 0; |
f85afd4e MB |
4190 | for ( row = 0; row < m_numRows; row++ ) |
4191 | { | |
4192 | m_rowHeights[row] = m_defaultRowHeight; | |
2d66e025 MB |
4193 | bottom += m_defaultRowHeight; |
4194 | m_rowBottoms[row] = bottom; | |
f85afd4e MB |
4195 | } |
4196 | CalcDimensions(); | |
f85afd4e MB |
4197 | } |
4198 | } | |
4199 | ||
4200 | void wxGrid::SetRowSize( int row, int height ) | |
4201 | { | |
2d66e025 | 4202 | int i; |
60ff3b99 | 4203 | |
f85afd4e MB |
4204 | if ( row >= 0 && row < m_numRows ) |
4205 | { | |
2d66e025 MB |
4206 | int h = wxMax( 0, height ); |
4207 | int diff = h - m_rowHeights[row]; | |
60ff3b99 | 4208 | |
2d66e025 MB |
4209 | m_rowHeights[row] = h; |
4210 | for ( i = row; i < m_numRows; i++ ) | |
4211 | { | |
4212 | m_rowBottoms[i] += diff; | |
4213 | } | |
f85afd4e | 4214 | CalcDimensions(); |
60ff3b99 | 4215 | |
f85afd4e MB |
4216 | // Note: we are ending the event *after* doing |
4217 | // default processing in this case | |
4218 | // | |
b5f788a5 | 4219 | SendEvent( EVT_GRID_ROW_SIZE, |
f85afd4e MB |
4220 | row, -1 ); |
4221 | } | |
4222 | else | |
4223 | { | |
4224 | // TODO: log an error here | |
4225 | } | |
4226 | } | |
4227 | ||
4228 | void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) | |
4229 | { | |
4230 | m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH ); | |
4231 | ||
4232 | if ( resizeExistingCols ) | |
4233 | { | |
f85afd4e | 4234 | int col; |
2d66e025 | 4235 | int right = 0; |
f85afd4e MB |
4236 | for ( col = 0; col < m_numCols; col++ ) |
4237 | { | |
4238 | m_colWidths[col] = m_defaultColWidth; | |
2d66e025 MB |
4239 | right += m_defaultColWidth; |
4240 | m_colRights[col] = right; | |
f85afd4e MB |
4241 | } |
4242 | CalcDimensions(); | |
f85afd4e MB |
4243 | } |
4244 | } | |
4245 | ||
4246 | void wxGrid::SetColSize( int col, int width ) | |
4247 | { | |
2d66e025 | 4248 | int i; |
60ff3b99 | 4249 | |
f85afd4e MB |
4250 | if ( col >= 0 && col < m_numCols ) |
4251 | { | |
2d66e025 MB |
4252 | int w = wxMax( 0, width ); |
4253 | int diff = w - m_colWidths[col]; | |
4254 | m_colWidths[col] = w; | |
f85afd4e | 4255 | |
2d66e025 MB |
4256 | for ( i = col; i < m_numCols; i++ ) |
4257 | { | |
4258 | m_colRights[i] += diff; | |
4259 | } | |
4260 | CalcDimensions(); | |
60ff3b99 | 4261 | |
f85afd4e MB |
4262 | // Note: we are ending the event *after* doing |
4263 | // default processing in this case | |
4264 | // | |
b5f788a5 | 4265 | SendEvent( EVT_GRID_COL_SIZE, |
f85afd4e MB |
4266 | -1, col ); |
4267 | } | |
4268 | else | |
4269 | { | |
4270 | // TODO: log an error here | |
4271 | } | |
4272 | } | |
4273 | ||
4274 | void wxGrid::SetDefaultCellBackgroundColour( const wxColour& ) | |
4275 | { | |
4276 | // TODO: everything !!! | |
4277 | // | |
4278 | } | |
4279 | ||
af111fc3 | 4280 | void wxGrid::SetCellBackgroundColour( int WXUNUSED(row), int WXUNUSED(col), const wxColour& ) |
f85afd4e MB |
4281 | { |
4282 | // TODO: everything !!! | |
4283 | // | |
4284 | } | |
4285 | ||
4286 | void wxGrid::SetDefaultCellTextColour( const wxColour& ) | |
4287 | { | |
4288 | // TODO: everything !!! | |
4289 | // | |
4290 | } | |
4291 | ||
af111fc3 | 4292 | void wxGrid::SetCellTextColour( int WXUNUSED(row), int WXUNUSED(col), const wxColour& ) |
f85afd4e MB |
4293 | { |
4294 | // TODO: everything !!! | |
4295 | // | |
4296 | } | |
4297 | ||
f85afd4e MB |
4298 | void wxGrid::SetDefaultCellFont( const wxFont& ) |
4299 | { | |
4300 | // TODO: everything !!! | |
4301 | // | |
4302 | } | |
4303 | ||
af111fc3 | 4304 | void wxGrid::SetCellFont( int WXUNUSED(row), int WXUNUSED(col), const wxFont& ) |
f85afd4e MB |
4305 | { |
4306 | // TODO: everything !!! | |
4307 | // | |
4308 | } | |
4309 | ||
af111fc3 | 4310 | void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz), int WXUNUSED(vert) ) |
f85afd4e MB |
4311 | { |
4312 | // TODO: everything !!! | |
4313 | // | |
4314 | } | |
4315 | ||
af111fc3 | 4316 | void wxGrid::SetCellAlignment( int WXUNUSED(row), int WXUNUSED(col), int WXUNUSED(horiz), int WXUNUSED(vert) ) |
f85afd4e MB |
4317 | { |
4318 | // TODO: everything !!! | |
4319 | // | |
4320 | } | |
4321 | ||
4322 | ||
2d66e025 | 4323 | |
f85afd4e MB |
4324 | // |
4325 | // ------ cell value accessor functions | |
4326 | // | |
4327 | ||
4328 | void wxGrid::SetCellValue( int row, int col, const wxString& s ) | |
4329 | { | |
4330 | if ( m_table ) | |
4331 | { | |
4332 | m_table->SetValue( row, col, s.c_str() ); | |
2d66e025 MB |
4333 | if ( !GetBatchCount() ) |
4334 | { | |
4335 | wxClientDC dc( m_gridWin ); | |
4336 | PrepareDC( dc ); | |
4337 | DrawCell( dc, wxGridCellCoords(row, col) ); | |
4338 | } | |
60ff3b99 | 4339 | |
2d66e025 | 4340 | #if 0 // TODO: edit in place |
60ff3b99 | 4341 | |
f85afd4e MB |
4342 | if ( m_currentCellCoords.GetRow() == row && |
4343 | m_currentCellCoords.GetCol() == col ) | |
4344 | { | |
4345 | SetEditControlValue( s ); | |
4346 | } | |
2d66e025 | 4347 | #endif |
f85afd4e | 4348 | |
f85afd4e MB |
4349 | } |
4350 | } | |
4351 | ||
4352 | ||
4353 | // | |
2d66e025 | 4354 | // ------ Block, row and col selection |
f85afd4e MB |
4355 | // |
4356 | ||
4357 | void wxGrid::SelectRow( int row, bool addToSelected ) | |
4358 | { | |
2d66e025 | 4359 | wxRect r; |
60ff3b99 | 4360 | |
f85afd4e MB |
4361 | if ( IsSelection() && addToSelected ) |
4362 | { | |
70c7a608 SN |
4363 | wxRect rect[4]; |
4364 | bool need_refresh[4] = { FALSE, FALSE, FALSE, FALSE }; | |
4365 | int i; | |
4366 | ||
4367 | wxCoord oldLeft = m_selectedTopLeft.GetCol(); | |
4368 | wxCoord oldTop = m_selectedTopLeft.GetRow(); | |
4369 | wxCoord oldRight = m_selectedBottomRight.GetCol(); | |
4370 | wxCoord oldBottom = m_selectedBottomRight.GetRow(); | |
4371 | ||
4372 | if ( oldTop > row ) | |
4373 | { | |
4374 | need_refresh[0] = TRUE; | |
4375 | rect[0] = BlockToDeviceRect( wxGridCellCoords ( row, 0 ), | |
4376 | wxGridCellCoords ( oldTop - 1, | |
4377 | m_numCols - 1 ) ); | |
f85afd4e | 4378 | m_selectedTopLeft.SetRow( row ); |
70c7a608 | 4379 | } |
8f177c8e | 4380 | |
70c7a608 SN |
4381 | if ( oldLeft > 0 ) |
4382 | { | |
4383 | need_refresh[1] = TRUE; | |
4384 | rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop, 0 ), | |
4385 | wxGridCellCoords ( oldBottom, | |
4386 | oldLeft - 1 ) ); | |
8f177c8e | 4387 | |
70c7a608 SN |
4388 | m_selectedTopLeft.SetCol( 0 ); |
4389 | } | |
4390 | ||
4391 | if ( oldBottom < row ) | |
4392 | { | |
4393 | need_refresh[2] = TRUE; | |
4394 | rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1, 0 ), | |
4395 | wxGridCellCoords ( row, | |
4396 | m_numCols - 1 ) ); | |
f85afd4e | 4397 | m_selectedBottomRight.SetRow( row ); |
70c7a608 | 4398 | } |
8f177c8e | 4399 | |
70c7a608 SN |
4400 | if ( oldRight < m_numCols - 1 ) |
4401 | { | |
4402 | need_refresh[3] = TRUE; | |
4403 | rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop , | |
4404 | oldRight + 1 ), | |
4405 | wxGridCellCoords ( oldBottom, | |
4406 | m_numCols - 1 ) ); | |
4407 | m_selectedBottomRight.SetCol( m_numCols - 1 ); | |
4408 | } | |
2d66e025 | 4409 | |
70c7a608 SN |
4410 | for (i = 0; i < 4; i++ ) |
4411 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
c6a51dcd | 4412 | m_gridWin->Refresh( FALSE, &(rect[i]) ); |
f85afd4e MB |
4413 | } |
4414 | else | |
4415 | { | |
2d66e025 | 4416 | r = SelectionToDeviceRect(); |
f85afd4e | 4417 | ClearSelection(); |
c6a51dcd | 4418 | if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r ); |
60ff3b99 | 4419 | |
f85afd4e MB |
4420 | m_selectedTopLeft.Set( row, 0 ); |
4421 | m_selectedBottomRight.Set( row, m_numCols-1 ); | |
2d66e025 | 4422 | r = SelectionToDeviceRect(); |
c6a51dcd | 4423 | m_gridWin->Refresh( FALSE, &r ); |
f85afd4e | 4424 | } |
8f177c8e | 4425 | |
f85afd4e | 4426 | wxGridRangeSelectEvent gridEvt( GetId(), |
b5f788a5 | 4427 | EVT_GRID_RANGE_SELECT, |
f85afd4e MB |
4428 | this, |
4429 | m_selectedTopLeft, | |
4430 | m_selectedBottomRight ); | |
4431 | ||
4432 | GetEventHandler()->ProcessEvent(gridEvt); | |
4433 | } | |
4434 | ||
4435 | ||
4436 | void wxGrid::SelectCol( int col, bool addToSelected ) | |
4437 | { | |
2d66e025 | 4438 | if ( IsSelection() && addToSelected ) |
f85afd4e | 4439 | { |
70c7a608 SN |
4440 | wxRect rect[4]; |
4441 | bool need_refresh[4] = { FALSE, FALSE, FALSE, FALSE }; | |
4442 | int i; | |
4443 | ||
4444 | wxCoord oldLeft = m_selectedTopLeft.GetCol(); | |
4445 | wxCoord oldTop = m_selectedTopLeft.GetRow(); | |
4446 | wxCoord oldRight = m_selectedBottomRight.GetCol(); | |
4447 | wxCoord oldBottom = m_selectedBottomRight.GetRow(); | |
4448 | ||
4449 | if ( oldLeft > col ) | |
4450 | { | |
4451 | need_refresh[0] = TRUE; | |
4452 | rect[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col ), | |
4453 | wxGridCellCoords ( m_numRows - 1, | |
4454 | oldLeft - 1 ) ); | |
f85afd4e | 4455 | m_selectedTopLeft.SetCol( col ); |
70c7a608 | 4456 | } |
f85afd4e | 4457 | |
70c7a608 SN |
4458 | if ( oldTop > 0 ) |
4459 | { | |
4460 | need_refresh[1] = TRUE; | |
4461 | rect[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft ), | |
4462 | wxGridCellCoords ( oldTop - 1, | |
4463 | oldRight ) ); | |
4464 | m_selectedTopLeft.SetRow( 0 ); | |
4465 | } | |
f85afd4e | 4466 | |
70c7a608 SN |
4467 | if ( oldRight < col ) |
4468 | { | |
4469 | need_refresh[2] = TRUE; | |
4470 | rect[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight + 1 ), | |
4471 | wxGridCellCoords ( m_numRows - 1, | |
4472 | col ) ); | |
f85afd4e | 4473 | m_selectedBottomRight.SetCol( col ); |
70c7a608 | 4474 | } |
f85afd4e | 4475 | |
70c7a608 SN |
4476 | if ( oldBottom < m_numRows - 1 ) |
4477 | { | |
4478 | need_refresh[3] = TRUE; | |
4479 | rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1, | |
4480 | oldLeft ), | |
4481 | wxGridCellCoords ( m_numRows - 1, | |
4482 | oldRight ) ); | |
4483 | m_selectedBottomRight.SetRow( m_numRows - 1 ); | |
4484 | } | |
2d66e025 | 4485 | |
70c7a608 SN |
4486 | for (i = 0; i < 4; i++ ) |
4487 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
c6a51dcd | 4488 | m_gridWin->Refresh( FALSE, &(rect[i]) ); |
f85afd4e MB |
4489 | } |
4490 | else | |
4491 | { | |
70c7a608 SN |
4492 | wxRect r; |
4493 | ||
2d66e025 | 4494 | r = SelectionToDeviceRect(); |
f85afd4e | 4495 | ClearSelection(); |
c6a51dcd | 4496 | if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r ); |
60ff3b99 | 4497 | |
f85afd4e MB |
4498 | m_selectedTopLeft.Set( 0, col ); |
4499 | m_selectedBottomRight.Set( m_numRows-1, col ); | |
2d66e025 | 4500 | r = SelectionToDeviceRect(); |
c6a51dcd | 4501 | m_gridWin->Refresh( FALSE, &r ); |
f85afd4e MB |
4502 | } |
4503 | ||
4504 | wxGridRangeSelectEvent gridEvt( GetId(), | |
b5f788a5 | 4505 | EVT_GRID_RANGE_SELECT, |
f85afd4e MB |
4506 | this, |
4507 | m_selectedTopLeft, | |
4508 | m_selectedBottomRight ); | |
4509 | ||
4510 | GetEventHandler()->ProcessEvent(gridEvt); | |
4511 | } | |
4512 | ||
4513 | ||
4514 | void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol ) | |
4515 | { | |
4516 | int temp; | |
da6af900 | 4517 | wxGridCellCoords updateTopLeft, updateBottomRight; |
8f177c8e | 4518 | |
f85afd4e MB |
4519 | if ( topRow > bottomRow ) |
4520 | { | |
4521 | temp = topRow; | |
4522 | topRow = bottomRow; | |
4523 | bottomRow = temp; | |
4524 | } | |
4525 | ||
4526 | if ( leftCol > rightCol ) | |
4527 | { | |
4528 | temp = leftCol; | |
4529 | leftCol = rightCol; | |
4530 | rightCol = temp; | |
4531 | } | |
f0102d2a | 4532 | |
70c7a608 SN |
4533 | updateTopLeft = wxGridCellCoords( topRow, leftCol ); |
4534 | updateBottomRight = wxGridCellCoords( bottomRow, rightCol ); | |
da6af900 | 4535 | |
70c7a608 SN |
4536 | if ( m_selectedTopLeft != updateTopLeft || |
4537 | m_selectedBottomRight != updateBottomRight ) | |
da6af900 | 4538 | { |
70c7a608 SN |
4539 | // Compute two optimal update rectangles: |
4540 | // Either one rectangle is a real subset of the | |
4541 | // other, or they are (almost) disjoint! | |
4542 | wxRect rect[4]; | |
4543 | bool need_refresh[4] = { FALSE, FALSE, FALSE, FALSE }; | |
4544 | int i; | |
4545 | ||
4546 | // Store intermediate values | |
4547 | wxCoord oldLeft = m_selectedTopLeft.GetCol(); | |
4548 | wxCoord oldTop = m_selectedTopLeft.GetRow(); | |
4549 | wxCoord oldRight = m_selectedBottomRight.GetCol(); | |
4550 | wxCoord oldBottom = m_selectedBottomRight.GetRow(); | |
4551 | ||
4552 | // Determine the outer/inner coordinates. | |
4553 | if (oldLeft > leftCol) | |
f0102d2a | 4554 | { |
70c7a608 SN |
4555 | temp = oldLeft; |
4556 | oldLeft = leftCol; | |
4557 | leftCol = temp; | |
cb309039 | 4558 | } |
70c7a608 | 4559 | if (oldTop > topRow ) |
f0102d2a | 4560 | { |
70c7a608 SN |
4561 | temp = oldTop; |
4562 | oldTop = topRow; | |
4563 | topRow = temp; | |
cb309039 SN |
4564 | } |
4565 | if (oldRight < rightCol ) | |
70c7a608 SN |
4566 | { |
4567 | temp = oldRight; | |
4568 | oldRight = rightCol; | |
4569 | rightCol = temp; | |
cb309039 SN |
4570 | } |
4571 | if (oldBottom < bottomRow) | |
4572 | { | |
70c7a608 SN |
4573 | temp = oldBottom; |
4574 | oldBottom = bottomRow; | |
4575 | bottomRow = temp; | |
cb309039 | 4576 | } |
70c7a608 SN |
4577 | |
4578 | // Now, either the stuff marked old is the outer | |
4579 | // rectangle or we don't have a situation where one | |
4580 | // is contained in the other. | |
4581 | ||
cb309039 SN |
4582 | if ( oldLeft < leftCol ) |
4583 | { | |
4584 | need_refresh[0] = TRUE; | |
4585 | rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
4586 | oldLeft ), | |
4587 | wxGridCellCoords ( oldBottom, | |
4588 | leftCol - 1 ) ); | |
4589 | } | |
4590 | ||
4591 | if ( oldTop < topRow ) | |
4592 | { | |
4593 | need_refresh[1] = TRUE; | |
4594 | rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
4595 | leftCol ), | |
4596 | wxGridCellCoords ( topRow - 1, | |
4597 | rightCol ) ); | |
4598 | } | |
4599 | ||
4600 | if ( oldRight > rightCol ) | |
4601 | { | |
4602 | need_refresh[2] = TRUE; | |
4603 | rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop, | |
4604 | rightCol + 1 ), | |
4605 | wxGridCellCoords ( oldBottom, | |
4606 | oldRight ) ); | |
4607 | } | |
4608 | ||
4609 | if ( oldBottom > bottomRow ) | |
4610 | { | |
4611 | need_refresh[3] = TRUE; | |
4612 | rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1, | |
4613 | leftCol ), | |
4614 | wxGridCellCoords ( oldBottom, | |
4615 | rightCol ) ); | |
4616 | } | |
70c7a608 SN |
4617 | |
4618 | ||
4619 | // Change Selection | |
4620 | m_selectedTopLeft = updateTopLeft; | |
4621 | m_selectedBottomRight = updateBottomRight; | |
4622 | ||
4623 | // various Refresh() calls | |
4624 | for (i = 0; i < 4; i++ ) | |
4625 | if ( need_refresh[i] && rect[i] != wxGridNoCellRect ) | |
c6a51dcd | 4626 | m_gridWin->Refresh( FALSE, &(rect[i]) ); |
da6af900 | 4627 | } |
f85afd4e MB |
4628 | |
4629 | // only generate an event if the block is not being selected by | |
4630 | // dragging the mouse (in which case the event will be generated in | |
2d66e025 | 4631 | // the mouse event handler) |
f85afd4e MB |
4632 | if ( !m_isDragging ) |
4633 | { | |
4634 | wxGridRangeSelectEvent gridEvt( GetId(), | |
b5f788a5 | 4635 | EVT_GRID_RANGE_SELECT, |
f85afd4e MB |
4636 | this, |
4637 | m_selectedTopLeft, | |
4638 | m_selectedBottomRight ); | |
8f177c8e | 4639 | |
f85afd4e MB |
4640 | GetEventHandler()->ProcessEvent(gridEvt); |
4641 | } | |
4642 | } | |
4643 | ||
4644 | void wxGrid::SelectAll() | |
4645 | { | |
4646 | m_selectedTopLeft.Set( 0, 0 ); | |
4647 | m_selectedBottomRight.Set( m_numRows-1, m_numCols-1 ); | |
4648 | ||
2d66e025 | 4649 | m_gridWin->Refresh(); |
f85afd4e MB |
4650 | } |
4651 | ||
4652 | ||
4653 | void wxGrid::ClearSelection() | |
4654 | { | |
2d66e025 MB |
4655 | m_selectedTopLeft = wxGridNoCellCoords; |
4656 | m_selectedBottomRight = wxGridNoCellCoords; | |
8f177c8e | 4657 | } |
f85afd4e MB |
4658 | |
4659 | ||
da6af900 | 4660 | // This function returns the rectangle that encloses the given block |
2d66e025 MB |
4661 | // in device coords clipped to the client size of the grid window. |
4662 | // | |
58dd5b3b MB |
4663 | wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft, |
4664 | const wxGridCellCoords &bottomRight ) | |
f85afd4e | 4665 | { |
58dd5b3b | 4666 | wxRect rect( wxGridNoCellRect ); |
f85afd4e MB |
4667 | wxRect cellRect; |
4668 | ||
58dd5b3b MB |
4669 | cellRect = CellToRect( topLeft ); |
4670 | if ( cellRect != wxGridNoCellRect ) | |
f85afd4e | 4671 | { |
58dd5b3b MB |
4672 | rect = cellRect; |
4673 | } | |
4674 | else | |
4675 | { | |
4676 | rect = wxRect( 0, 0, 0, 0 ); | |
4677 | } | |
60ff3b99 | 4678 | |
58dd5b3b MB |
4679 | cellRect = CellToRect( bottomRight ); |
4680 | if ( cellRect != wxGridNoCellRect ) | |
4681 | { | |
4682 | rect += cellRect; | |
2d66e025 MB |
4683 | } |
4684 | else | |
4685 | { | |
4686 | return wxGridNoCellRect; | |
f85afd4e MB |
4687 | } |
4688 | ||
58dd5b3b MB |
4689 | // convert to scrolled coords |
4690 | // | |
4691 | int left, top, right, bottom; | |
4692 | CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top ); | |
4693 | CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom ); | |
4694 | ||
4695 | int cw, ch; | |
4696 | m_gridWin->GetClientSize( &cw, &ch ); | |
4697 | ||
4698 | rect.SetLeft( wxMax(0, left) ); | |
4699 | rect.SetTop( wxMax(0, top) ); | |
4700 | rect.SetRight( wxMin(cw, right) ); | |
4701 | rect.SetBottom( wxMin(ch, bottom) ); | |
4702 | ||
f85afd4e MB |
4703 | return rect; |
4704 | } | |
4705 | ||
4706 | ||
58dd5b3b | 4707 | |
f85afd4e MB |
4708 | // |
4709 | // ------ Grid event classes | |
4710 | // | |
4711 | ||
4712 | IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxEvent ) | |
4713 | ||
4714 | wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, | |
4715 | int row, int col, int x, int y, | |
4716 | bool control, bool shift, bool alt, bool meta ) | |
4717 | : wxNotifyEvent( type, id ) | |
4718 | { | |
4719 | m_row = row; | |
4720 | m_col = col; | |
4721 | m_x = x; | |
4722 | m_y = y; | |
4723 | m_control = control; | |
4724 | m_shift = shift; | |
4725 | m_alt = alt; | |
4726 | m_meta = meta; | |
8f177c8e | 4727 | |
f85afd4e MB |
4728 | SetEventObject(obj); |
4729 | } | |
4730 | ||
4731 | ||
4732 | IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxEvent ) | |
4733 | ||
4734 | wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj, | |
4735 | int rowOrCol, int x, int y, | |
4736 | bool control, bool shift, bool alt, bool meta ) | |
4737 | : wxNotifyEvent( type, id ) | |
4738 | { | |
4739 | m_rowOrCol = rowOrCol; | |
4740 | m_x = x; | |
4741 | m_y = y; | |
4742 | m_control = control; | |
4743 | m_shift = shift; | |
4744 | m_alt = alt; | |
4745 | m_meta = meta; | |
8f177c8e | 4746 | |
f85afd4e MB |
4747 | SetEventObject(obj); |
4748 | } | |
4749 | ||
4750 | ||
4751 | IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxEvent ) | |
4752 | ||
4753 | wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, | |
8f177c8e VZ |
4754 | const wxGridCellCoords& topLeft, |
4755 | const wxGridCellCoords& bottomRight, | |
4756 | bool control, bool shift, bool alt, bool meta ) | |
4757 | : wxNotifyEvent( type, id ) | |
f85afd4e MB |
4758 | { |
4759 | m_topLeft = topLeft; | |
4760 | m_bottomRight = bottomRight; | |
4761 | m_control = control; | |
4762 | m_shift = shift; | |
4763 | m_alt = alt; | |
4764 | m_meta = meta; | |
4765 | ||
4766 | SetEventObject(obj); | |
4767 | } | |
4768 | ||
4769 | ||
4770 | #endif // ifndef wxUSE_NEW_GRID | |
4771 |