]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/listctrl.cpp | |
3 | // Purpose: generic implementation of wxListCtrl | |
4 | // Author: Robert Roebling | |
5 | // Vadim Zeitlin (virtual list control support) | |
6 | // Id: $Id$ | |
7 | // Copyright: (c) 1998 Robert Roebling | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // TODO | |
12 | // | |
13 | // 1. we need to implement searching/sorting for virtual controls somehow | |
14 | // 2. when changing selection the lines are refreshed twice | |
15 | ||
16 | ||
17 | // For compilers that support precompilation, includes "wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #if wxUSE_LISTCTRL | |
25 | ||
26 | #include "wx/listctrl.h" | |
27 | ||
28 | #if ((!defined(__WXMSW__) && !(defined(__WXMAC__) && wxOSX_USE_CARBON)) || defined(__WXUNIVERSAL__)) | |
29 | // if we have a native version, its implementation file does all this | |
30 | IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject) | |
31 | IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl) | |
32 | IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent) | |
33 | ||
34 | IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxGenericListCtrl) | |
35 | #endif | |
36 | ||
37 | #ifndef WX_PRECOMP | |
38 | #include "wx/scrolwin.h" | |
39 | #include "wx/timer.h" | |
40 | #include "wx/settings.h" | |
41 | #include "wx/dynarray.h" | |
42 | #include "wx/dcclient.h" | |
43 | #include "wx/dcscreen.h" | |
44 | #include "wx/math.h" | |
45 | #include "wx/settings.h" | |
46 | #include "wx/sizer.h" | |
47 | #endif | |
48 | ||
49 | #include "wx/imaglist.h" | |
50 | #include "wx/renderer.h" | |
51 | #include "wx/generic/private/listctrl.h" | |
52 | ||
53 | #ifdef __WXMAC__ | |
54 | #include "wx/osx/private.h" | |
55 | // for themeing support | |
56 | #include <Carbon/Carbon.h> | |
57 | #endif | |
58 | ||
59 | ||
60 | // NOTE: If using the wxListBox visual attributes works everywhere then this can | |
61 | // be removed, as well as the #else case below. | |
62 | #define _USE_VISATTR 0 | |
63 | ||
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // constants | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | // // the height of the header window (FIXME: should depend on its font!) | |
70 | // static const int HEADER_HEIGHT = 23; | |
71 | ||
72 | static const int SCROLL_UNIT_X = 15; | |
73 | ||
74 | // the spacing between the lines (in report mode) | |
75 | static const int LINE_SPACING = 0; | |
76 | ||
77 | // extra margins around the text label | |
78 | #ifdef __WXGTK__ | |
79 | static const int EXTRA_WIDTH = 6; | |
80 | #else | |
81 | static const int EXTRA_WIDTH = 4; | |
82 | #endif | |
83 | ||
84 | #ifdef __WXGTK__ | |
85 | static const int EXTRA_HEIGHT = 6; | |
86 | #else | |
87 | static const int EXTRA_HEIGHT = 4; | |
88 | #endif | |
89 | ||
90 | // margin between the window and the items | |
91 | static const int EXTRA_BORDER_X = 2; | |
92 | static const int EXTRA_BORDER_Y = 2; | |
93 | ||
94 | // offset for the header window | |
95 | static const int HEADER_OFFSET_X = 0; | |
96 | static const int HEADER_OFFSET_Y = 0; | |
97 | ||
98 | // margin between rows of icons in [small] icon view | |
99 | static const int MARGIN_BETWEEN_ROWS = 6; | |
100 | ||
101 | // when autosizing the columns, add some slack | |
102 | static const int AUTOSIZE_COL_MARGIN = 10; | |
103 | ||
104 | // default width for the header columns | |
105 | static const int WIDTH_COL_DEFAULT = 80; | |
106 | ||
107 | // the space between the image and the text in the report mode | |
108 | static const int IMAGE_MARGIN_IN_REPORT_MODE = 5; | |
109 | ||
110 | // the space between the image and the text in the report mode in header | |
111 | static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE = 2; | |
112 | ||
113 | ||
114 | ||
115 | // ---------------------------------------------------------------------------- | |
116 | // arrays/list implementations | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
119 | #include "wx/listimpl.cpp" | |
120 | WX_DEFINE_LIST(wxListItemDataList) | |
121 | ||
122 | #include "wx/arrimpl.cpp" | |
123 | WX_DEFINE_OBJARRAY(wxListLineDataArray) | |
124 | ||
125 | #include "wx/listimpl.cpp" | |
126 | WX_DEFINE_LIST(wxListHeaderDataList) | |
127 | ||
128 | ||
129 | // ---------------------------------------------------------------------------- | |
130 | // wxListItemData | |
131 | // ---------------------------------------------------------------------------- | |
132 | ||
133 | wxListItemData::~wxListItemData() | |
134 | { | |
135 | // in the virtual list control the attributes are managed by the main | |
136 | // program, so don't delete them | |
137 | if ( !m_owner->IsVirtual() ) | |
138 | delete m_attr; | |
139 | ||
140 | delete m_rect; | |
141 | } | |
142 | ||
143 | void wxListItemData::Init() | |
144 | { | |
145 | m_image = -1; | |
146 | m_data = 0; | |
147 | ||
148 | m_attr = NULL; | |
149 | } | |
150 | ||
151 | wxListItemData::wxListItemData(wxListMainWindow *owner) | |
152 | { | |
153 | Init(); | |
154 | ||
155 | m_owner = owner; | |
156 | ||
157 | if ( owner->InReportView() ) | |
158 | m_rect = NULL; | |
159 | else | |
160 | m_rect = new wxRect; | |
161 | } | |
162 | ||
163 | void wxListItemData::SetItem( const wxListItem &info ) | |
164 | { | |
165 | if ( info.m_mask & wxLIST_MASK_TEXT ) | |
166 | SetText(info.m_text); | |
167 | if ( info.m_mask & wxLIST_MASK_IMAGE ) | |
168 | m_image = info.m_image; | |
169 | if ( info.m_mask & wxLIST_MASK_DATA ) | |
170 | m_data = info.m_data; | |
171 | ||
172 | if ( info.HasAttributes() ) | |
173 | { | |
174 | if ( m_attr ) | |
175 | m_attr->AssignFrom(*info.GetAttributes()); | |
176 | else | |
177 | m_attr = new wxListItemAttr(*info.GetAttributes()); | |
178 | } | |
179 | ||
180 | if ( m_rect ) | |
181 | { | |
182 | m_rect->x = | |
183 | m_rect->y = | |
184 | m_rect->height = 0; | |
185 | m_rect->width = info.m_width; | |
186 | } | |
187 | } | |
188 | ||
189 | void wxListItemData::SetPosition( int x, int y ) | |
190 | { | |
191 | wxCHECK_RET( m_rect, _T("unexpected SetPosition() call") ); | |
192 | ||
193 | m_rect->x = x; | |
194 | m_rect->y = y; | |
195 | } | |
196 | ||
197 | void wxListItemData::SetSize( int width, int height ) | |
198 | { | |
199 | wxCHECK_RET( m_rect, _T("unexpected SetSize() call") ); | |
200 | ||
201 | if ( width != -1 ) | |
202 | m_rect->width = width; | |
203 | if ( height != -1 ) | |
204 | m_rect->height = height; | |
205 | } | |
206 | ||
207 | bool wxListItemData::IsHit( int x, int y ) const | |
208 | { | |
209 | wxCHECK_MSG( m_rect, false, _T("can't be called in this mode") ); | |
210 | ||
211 | return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x, y); | |
212 | } | |
213 | ||
214 | int wxListItemData::GetX() const | |
215 | { | |
216 | wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") ); | |
217 | ||
218 | return m_rect->x; | |
219 | } | |
220 | ||
221 | int wxListItemData::GetY() const | |
222 | { | |
223 | wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") ); | |
224 | ||
225 | return m_rect->y; | |
226 | } | |
227 | ||
228 | int wxListItemData::GetWidth() const | |
229 | { | |
230 | wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") ); | |
231 | ||
232 | return m_rect->width; | |
233 | } | |
234 | ||
235 | int wxListItemData::GetHeight() const | |
236 | { | |
237 | wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") ); | |
238 | ||
239 | return m_rect->height; | |
240 | } | |
241 | ||
242 | void wxListItemData::GetItem( wxListItem &info ) const | |
243 | { | |
244 | long mask = info.m_mask; | |
245 | if ( !mask ) | |
246 | // by default, get everything for backwards compatibility | |
247 | mask = -1; | |
248 | ||
249 | if ( mask & wxLIST_MASK_TEXT ) | |
250 | info.m_text = m_text; | |
251 | if ( mask & wxLIST_MASK_IMAGE ) | |
252 | info.m_image = m_image; | |
253 | if ( mask & wxLIST_MASK_DATA ) | |
254 | info.m_data = m_data; | |
255 | ||
256 | if ( m_attr ) | |
257 | { | |
258 | if ( m_attr->HasTextColour() ) | |
259 | info.SetTextColour(m_attr->GetTextColour()); | |
260 | if ( m_attr->HasBackgroundColour() ) | |
261 | info.SetBackgroundColour(m_attr->GetBackgroundColour()); | |
262 | if ( m_attr->HasFont() ) | |
263 | info.SetFont(m_attr->GetFont()); | |
264 | } | |
265 | } | |
266 | ||
267 | //----------------------------------------------------------------------------- | |
268 | // wxListHeaderData | |
269 | //----------------------------------------------------------------------------- | |
270 | ||
271 | void wxListHeaderData::Init() | |
272 | { | |
273 | m_mask = 0; | |
274 | m_image = -1; | |
275 | m_format = 0; | |
276 | m_width = 0; | |
277 | m_xpos = 0; | |
278 | m_ypos = 0; | |
279 | m_height = 0; | |
280 | m_state = 0; | |
281 | } | |
282 | ||
283 | wxListHeaderData::wxListHeaderData() | |
284 | { | |
285 | Init(); | |
286 | } | |
287 | ||
288 | wxListHeaderData::wxListHeaderData( const wxListItem &item ) | |
289 | { | |
290 | Init(); | |
291 | ||
292 | SetItem( item ); | |
293 | } | |
294 | ||
295 | void wxListHeaderData::SetItem( const wxListItem &item ) | |
296 | { | |
297 | m_mask = item.m_mask; | |
298 | ||
299 | if ( m_mask & wxLIST_MASK_TEXT ) | |
300 | m_text = item.m_text; | |
301 | ||
302 | if ( m_mask & wxLIST_MASK_IMAGE ) | |
303 | m_image = item.m_image; | |
304 | ||
305 | if ( m_mask & wxLIST_MASK_FORMAT ) | |
306 | m_format = item.m_format; | |
307 | ||
308 | if ( m_mask & wxLIST_MASK_WIDTH ) | |
309 | SetWidth(item.m_width); | |
310 | ||
311 | if ( m_mask & wxLIST_MASK_STATE ) | |
312 | SetState(item.m_state); | |
313 | } | |
314 | ||
315 | void wxListHeaderData::SetPosition( int x, int y ) | |
316 | { | |
317 | m_xpos = x; | |
318 | m_ypos = y; | |
319 | } | |
320 | ||
321 | void wxListHeaderData::SetHeight( int h ) | |
322 | { | |
323 | m_height = h; | |
324 | } | |
325 | ||
326 | void wxListHeaderData::SetWidth( int w ) | |
327 | { | |
328 | m_width = w < 0 ? WIDTH_COL_DEFAULT : w; | |
329 | } | |
330 | ||
331 | void wxListHeaderData::SetState( int flag ) | |
332 | { | |
333 | m_state = flag; | |
334 | } | |
335 | ||
336 | void wxListHeaderData::SetFormat( int format ) | |
337 | { | |
338 | m_format = format; | |
339 | } | |
340 | ||
341 | bool wxListHeaderData::HasImage() const | |
342 | { | |
343 | return m_image != -1; | |
344 | } | |
345 | ||
346 | bool wxListHeaderData::IsHit( int x, int y ) const | |
347 | { | |
348 | return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height)); | |
349 | } | |
350 | ||
351 | void wxListHeaderData::GetItem( wxListItem& item ) | |
352 | { | |
353 | item.m_mask = m_mask; | |
354 | item.m_text = m_text; | |
355 | item.m_image = m_image; | |
356 | item.m_format = m_format; | |
357 | item.m_width = m_width; | |
358 | item.m_state = m_state; | |
359 | } | |
360 | ||
361 | int wxListHeaderData::GetImage() const | |
362 | { | |
363 | return m_image; | |
364 | } | |
365 | ||
366 | int wxListHeaderData::GetWidth() const | |
367 | { | |
368 | return m_width; | |
369 | } | |
370 | ||
371 | int wxListHeaderData::GetFormat() const | |
372 | { | |
373 | return m_format; | |
374 | } | |
375 | ||
376 | int wxListHeaderData::GetState() const | |
377 | { | |
378 | return m_state; | |
379 | } | |
380 | ||
381 | //----------------------------------------------------------------------------- | |
382 | // wxListLineData | |
383 | //----------------------------------------------------------------------------- | |
384 | ||
385 | inline int wxListLineData::GetMode() const | |
386 | { | |
387 | return m_owner->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE; | |
388 | } | |
389 | ||
390 | inline bool wxListLineData::InReportView() const | |
391 | { | |
392 | return m_owner->HasFlag(wxLC_REPORT); | |
393 | } | |
394 | ||
395 | inline bool wxListLineData::IsVirtual() const | |
396 | { | |
397 | return m_owner->IsVirtual(); | |
398 | } | |
399 | ||
400 | wxListLineData::wxListLineData( wxListMainWindow *owner ) | |
401 | { | |
402 | m_owner = owner; | |
403 | ||
404 | if ( InReportView() ) | |
405 | m_gi = NULL; | |
406 | else // !report | |
407 | m_gi = new GeometryInfo; | |
408 | ||
409 | m_highlighted = false; | |
410 | ||
411 | InitItems( GetMode() == wxLC_REPORT ? m_owner->GetColumnCount() : 1 ); | |
412 | } | |
413 | ||
414 | void wxListLineData::CalculateSize( wxDC *dc, int spacing ) | |
415 | { | |
416 | wxListItemDataList::compatibility_iterator node = m_items.GetFirst(); | |
417 | wxCHECK_RET( node, _T("no subitems at all??") ); | |
418 | ||
419 | wxListItemData *item = node->GetData(); | |
420 | ||
421 | wxString s; | |
422 | wxCoord lw, lh; | |
423 | ||
424 | switch ( GetMode() ) | |
425 | { | |
426 | case wxLC_ICON: | |
427 | case wxLC_SMALL_ICON: | |
428 | m_gi->m_rectAll.width = spacing; | |
429 | ||
430 | s = item->GetText(); | |
431 | ||
432 | if ( s.empty() ) | |
433 | { | |
434 | lh = | |
435 | m_gi->m_rectLabel.width = | |
436 | m_gi->m_rectLabel.height = 0; | |
437 | } | |
438 | else // has label | |
439 | { | |
440 | dc->GetTextExtent( s, &lw, &lh ); | |
441 | lw += EXTRA_WIDTH; | |
442 | lh += EXTRA_HEIGHT; | |
443 | ||
444 | m_gi->m_rectAll.height = spacing + lh; | |
445 | if (lw > spacing) | |
446 | m_gi->m_rectAll.width = lw; | |
447 | ||
448 | m_gi->m_rectLabel.width = lw; | |
449 | m_gi->m_rectLabel.height = lh; | |
450 | } | |
451 | ||
452 | if (item->HasImage()) | |
453 | { | |
454 | int w, h; | |
455 | m_owner->GetImageSize( item->GetImage(), w, h ); | |
456 | m_gi->m_rectIcon.width = w + 8; | |
457 | m_gi->m_rectIcon.height = h + 8; | |
458 | ||
459 | if ( m_gi->m_rectIcon.width > m_gi->m_rectAll.width ) | |
460 | m_gi->m_rectAll.width = m_gi->m_rectIcon.width; | |
461 | if ( m_gi->m_rectIcon.height + lh > m_gi->m_rectAll.height - 4 ) | |
462 | m_gi->m_rectAll.height = m_gi->m_rectIcon.height + lh + 4; | |
463 | } | |
464 | ||
465 | if ( item->HasText() ) | |
466 | { | |
467 | m_gi->m_rectHighlight.width = m_gi->m_rectLabel.width; | |
468 | m_gi->m_rectHighlight.height = m_gi->m_rectLabel.height; | |
469 | } | |
470 | else // no text, highlight the icon | |
471 | { | |
472 | m_gi->m_rectHighlight.width = m_gi->m_rectIcon.width; | |
473 | m_gi->m_rectHighlight.height = m_gi->m_rectIcon.height; | |
474 | } | |
475 | break; | |
476 | ||
477 | case wxLC_LIST: | |
478 | s = item->GetTextForMeasuring(); | |
479 | ||
480 | dc->GetTextExtent( s, &lw, &lh ); | |
481 | lw += EXTRA_WIDTH; | |
482 | lh += EXTRA_HEIGHT; | |
483 | ||
484 | m_gi->m_rectLabel.width = lw; | |
485 | m_gi->m_rectLabel.height = lh; | |
486 | ||
487 | m_gi->m_rectAll.width = lw; | |
488 | m_gi->m_rectAll.height = lh; | |
489 | ||
490 | if (item->HasImage()) | |
491 | { | |
492 | int w, h; | |
493 | m_owner->GetImageSize( item->GetImage(), w, h ); | |
494 | m_gi->m_rectIcon.width = w; | |
495 | m_gi->m_rectIcon.height = h; | |
496 | ||
497 | m_gi->m_rectAll.width += 4 + w; | |
498 | if (h > m_gi->m_rectAll.height) | |
499 | m_gi->m_rectAll.height = h; | |
500 | } | |
501 | ||
502 | m_gi->m_rectHighlight.width = m_gi->m_rectAll.width; | |
503 | m_gi->m_rectHighlight.height = m_gi->m_rectAll.height; | |
504 | break; | |
505 | ||
506 | case wxLC_REPORT: | |
507 | wxFAIL_MSG( _T("unexpected call to SetSize") ); | |
508 | break; | |
509 | ||
510 | default: | |
511 | wxFAIL_MSG( _T("unknown mode") ); | |
512 | break; | |
513 | } | |
514 | } | |
515 | ||
516 | void wxListLineData::SetPosition( int x, int y, int spacing ) | |
517 | { | |
518 | wxListItemDataList::compatibility_iterator node = m_items.GetFirst(); | |
519 | wxCHECK_RET( node, _T("no subitems at all??") ); | |
520 | ||
521 | wxListItemData *item = node->GetData(); | |
522 | ||
523 | switch ( GetMode() ) | |
524 | { | |
525 | case wxLC_ICON: | |
526 | case wxLC_SMALL_ICON: | |
527 | m_gi->m_rectAll.x = x; | |
528 | m_gi->m_rectAll.y = y; | |
529 | ||
530 | if ( item->HasImage() ) | |
531 | { | |
532 | m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 4 + | |
533 | (m_gi->m_rectAll.width - m_gi->m_rectIcon.width) / 2; | |
534 | m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 4; | |
535 | } | |
536 | ||
537 | if ( item->HasText() ) | |
538 | { | |
539 | if (m_gi->m_rectAll.width > spacing) | |
540 | m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2); | |
541 | else | |
542 | m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2) + (spacing / 2) - (m_gi->m_rectLabel.width / 2); | |
543 | m_gi->m_rectLabel.y = m_gi->m_rectAll.y + m_gi->m_rectAll.height + 2 - m_gi->m_rectLabel.height; | |
544 | m_gi->m_rectHighlight.x = m_gi->m_rectLabel.x - 2; | |
545 | m_gi->m_rectHighlight.y = m_gi->m_rectLabel.y - 2; | |
546 | } | |
547 | else // no text, highlight the icon | |
548 | { | |
549 | m_gi->m_rectHighlight.x = m_gi->m_rectIcon.x - 4; | |
550 | m_gi->m_rectHighlight.y = m_gi->m_rectIcon.y - 4; | |
551 | } | |
552 | break; | |
553 | ||
554 | case wxLC_LIST: | |
555 | m_gi->m_rectAll.x = x; | |
556 | m_gi->m_rectAll.y = y; | |
557 | ||
558 | m_gi->m_rectHighlight.x = m_gi->m_rectAll.x; | |
559 | m_gi->m_rectHighlight.y = m_gi->m_rectAll.y; | |
560 | m_gi->m_rectLabel.y = m_gi->m_rectAll.y + 2; | |
561 | ||
562 | if (item->HasImage()) | |
563 | { | |
564 | m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 2; | |
565 | m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 2; | |
566 | m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 4 + (EXTRA_WIDTH/2) + m_gi->m_rectIcon.width; | |
567 | } | |
568 | else | |
569 | { | |
570 | m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2); | |
571 | } | |
572 | break; | |
573 | ||
574 | case wxLC_REPORT: | |
575 | wxFAIL_MSG( _T("unexpected call to SetPosition") ); | |
576 | break; | |
577 | ||
578 | default: | |
579 | wxFAIL_MSG( _T("unknown mode") ); | |
580 | break; | |
581 | } | |
582 | } | |
583 | ||
584 | void wxListLineData::InitItems( int num ) | |
585 | { | |
586 | for (int i = 0; i < num; i++) | |
587 | m_items.Append( new wxListItemData(m_owner) ); | |
588 | } | |
589 | ||
590 | void wxListLineData::SetItem( int index, const wxListItem &info ) | |
591 | { | |
592 | wxListItemDataList::compatibility_iterator node = m_items.Item( index ); | |
593 | wxCHECK_RET( node, _T("invalid column index in SetItem") ); | |
594 | ||
595 | wxListItemData *item = node->GetData(); | |
596 | item->SetItem( info ); | |
597 | } | |
598 | ||
599 | void wxListLineData::GetItem( int index, wxListItem &info ) | |
600 | { | |
601 | wxListItemDataList::compatibility_iterator node = m_items.Item( index ); | |
602 | if (node) | |
603 | { | |
604 | wxListItemData *item = node->GetData(); | |
605 | item->GetItem( info ); | |
606 | } | |
607 | } | |
608 | ||
609 | wxString wxListLineData::GetText(int index) const | |
610 | { | |
611 | wxString s; | |
612 | ||
613 | wxListItemDataList::compatibility_iterator node = m_items.Item( index ); | |
614 | if (node) | |
615 | { | |
616 | wxListItemData *item = node->GetData(); | |
617 | s = item->GetText(); | |
618 | } | |
619 | ||
620 | return s; | |
621 | } | |
622 | ||
623 | void wxListLineData::SetText( int index, const wxString& s ) | |
624 | { | |
625 | wxListItemDataList::compatibility_iterator node = m_items.Item( index ); | |
626 | if (node) | |
627 | { | |
628 | wxListItemData *item = node->GetData(); | |
629 | item->SetText( s ); | |
630 | } | |
631 | } | |
632 | ||
633 | void wxListLineData::SetImage( int index, int image ) | |
634 | { | |
635 | wxListItemDataList::compatibility_iterator node = m_items.Item( index ); | |
636 | wxCHECK_RET( node, _T("invalid column index in SetImage()") ); | |
637 | ||
638 | wxListItemData *item = node->GetData(); | |
639 | item->SetImage(image); | |
640 | } | |
641 | ||
642 | int wxListLineData::GetImage( int index ) const | |
643 | { | |
644 | wxListItemDataList::compatibility_iterator node = m_items.Item( index ); | |
645 | wxCHECK_MSG( node, -1, _T("invalid column index in GetImage()") ); | |
646 | ||
647 | wxListItemData *item = node->GetData(); | |
648 | return item->GetImage(); | |
649 | } | |
650 | ||
651 | wxListItemAttr *wxListLineData::GetAttr() const | |
652 | { | |
653 | wxListItemDataList::compatibility_iterator node = m_items.GetFirst(); | |
654 | wxCHECK_MSG( node, NULL, _T("invalid column index in GetAttr()") ); | |
655 | ||
656 | wxListItemData *item = node->GetData(); | |
657 | return item->GetAttr(); | |
658 | } | |
659 | ||
660 | void wxListLineData::SetAttr(wxListItemAttr *attr) | |
661 | { | |
662 | wxListItemDataList::compatibility_iterator node = m_items.GetFirst(); | |
663 | wxCHECK_RET( node, _T("invalid column index in SetAttr()") ); | |
664 | ||
665 | wxListItemData *item = node->GetData(); | |
666 | item->SetAttr(attr); | |
667 | } | |
668 | ||
669 | bool wxListLineData::SetAttributes(wxDC *dc, | |
670 | const wxListItemAttr *attr, | |
671 | bool highlighted) | |
672 | { | |
673 | wxWindow *listctrl = m_owner->GetParent(); | |
674 | ||
675 | // fg colour | |
676 | ||
677 | // don't use foreground colour for drawing highlighted items - this might | |
678 | // make them completely invisible (and there is no way to do bit | |
679 | // arithmetics on wxColour, unfortunately) | |
680 | wxColour colText; | |
681 | if ( highlighted ) | |
682 | #ifdef __WXMAC__ | |
683 | { | |
684 | if (m_owner->HasFocus() | |
685 | #if !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON | |
686 | && IsControlActive( (ControlRef)m_owner->GetHandle() ) | |
687 | #endif | |
688 | ) | |
689 | colText = *wxWHITE; | |
690 | else | |
691 | colText = *wxBLACK; | |
692 | } | |
693 | #else | |
694 | colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
695 | #endif | |
696 | else if ( attr && attr->HasTextColour() ) | |
697 | colText = attr->GetTextColour(); | |
698 | else | |
699 | colText = listctrl->GetForegroundColour(); | |
700 | ||
701 | dc->SetTextForeground(colText); | |
702 | ||
703 | // font | |
704 | wxFont font; | |
705 | if ( attr && attr->HasFont() ) | |
706 | font = attr->GetFont(); | |
707 | else | |
708 | font = listctrl->GetFont(); | |
709 | ||
710 | dc->SetFont(font); | |
711 | ||
712 | // bg colour | |
713 | bool hasBgCol = attr && attr->HasBackgroundColour(); | |
714 | if ( highlighted || hasBgCol ) | |
715 | { | |
716 | if ( highlighted ) | |
717 | dc->SetBrush( *m_owner->GetHighlightBrush() ); | |
718 | else | |
719 | dc->SetBrush(wxBrush(attr->GetBackgroundColour(), wxBRUSHSTYLE_SOLID)); | |
720 | ||
721 | dc->SetPen( *wxTRANSPARENT_PEN ); | |
722 | ||
723 | return true; | |
724 | } | |
725 | ||
726 | return false; | |
727 | } | |
728 | ||
729 | void wxListLineData::Draw( wxDC *dc ) | |
730 | { | |
731 | wxListItemDataList::compatibility_iterator node = m_items.GetFirst(); | |
732 | wxCHECK_RET( node, _T("no subitems at all??") ); | |
733 | ||
734 | bool highlighted = IsHighlighted(); | |
735 | ||
736 | wxListItemAttr *attr = GetAttr(); | |
737 | ||
738 | if ( SetAttributes(dc, attr, highlighted) ) | |
739 | #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) ) | |
740 | { | |
741 | dc->DrawRectangle( m_gi->m_rectHighlight ); | |
742 | } | |
743 | #else | |
744 | { | |
745 | if (highlighted) | |
746 | { | |
747 | int flags = wxCONTROL_SELECTED; | |
748 | if (m_owner->HasFocus() | |
749 | #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON | |
750 | && IsControlActive( (ControlRef)m_owner->GetHandle() ) | |
751 | #endif | |
752 | ) | |
753 | flags |= wxCONTROL_FOCUSED; | |
754 | wxRendererNative::Get().DrawItemSelectionRect( m_owner, *dc, m_gi->m_rectHighlight, flags ); | |
755 | ||
756 | } | |
757 | else | |
758 | { | |
759 | dc->DrawRectangle( m_gi->m_rectHighlight ); | |
760 | } | |
761 | } | |
762 | #endif | |
763 | ||
764 | // just for debugging to better see where the items are | |
765 | #if 0 | |
766 | dc->SetPen(*wxRED_PEN); | |
767 | dc->SetBrush(*wxTRANSPARENT_BRUSH); | |
768 | dc->DrawRectangle( m_gi->m_rectAll ); | |
769 | dc->SetPen(*wxGREEN_PEN); | |
770 | dc->DrawRectangle( m_gi->m_rectIcon ); | |
771 | #endif | |
772 | ||
773 | wxListItemData *item = node->GetData(); | |
774 | if (item->HasImage()) | |
775 | { | |
776 | // centre the image inside our rectangle, this looks nicer when items | |
777 | // ae aligned in a row | |
778 | const wxRect& rectIcon = m_gi->m_rectIcon; | |
779 | ||
780 | m_owner->DrawImage(item->GetImage(), dc, rectIcon.x, rectIcon.y); | |
781 | } | |
782 | ||
783 | if (item->HasText()) | |
784 | { | |
785 | const wxRect& rectLabel = m_gi->m_rectLabel; | |
786 | ||
787 | wxDCClipper clipper(*dc, rectLabel); | |
788 | dc->DrawText(item->GetText(), rectLabel.x, rectLabel.y); | |
789 | } | |
790 | } | |
791 | ||
792 | void wxListLineData::DrawInReportMode( wxDC *dc, | |
793 | const wxRect& rect, | |
794 | const wxRect& rectHL, | |
795 | bool highlighted, | |
796 | bool current ) | |
797 | { | |
798 | // TODO: later we should support setting different attributes for | |
799 | // different columns - to do it, just add "col" argument to | |
800 | // GetAttr() and move these lines into the loop below | |
801 | wxListItemAttr *attr = GetAttr(); | |
802 | if ( SetAttributes(dc, attr, highlighted) ) | |
803 | #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) ) | |
804 | { | |
805 | dc->DrawRectangle( rectHL ); | |
806 | } | |
807 | #else | |
808 | { | |
809 | if (highlighted) | |
810 | { | |
811 | int flags = wxCONTROL_SELECTED; | |
812 | if (m_owner->HasFocus()) | |
813 | flags |= wxCONTROL_FOCUSED; | |
814 | if (current) | |
815 | flags |= wxCONTROL_CURRENT; | |
816 | wxRendererNative::Get().DrawItemSelectionRect( m_owner, *dc, rectHL, flags ); | |
817 | } | |
818 | else | |
819 | { | |
820 | dc->DrawRectangle( rectHL ); | |
821 | } | |
822 | } | |
823 | #endif | |
824 | ||
825 | wxCoord x = rect.x + HEADER_OFFSET_X, | |
826 | yMid = rect.y + rect.height/2; | |
827 | #ifdef __WXGTK__ | |
828 | // This probably needs to be done | |
829 | // on all platforms as the icons | |
830 | // otherwise nearly touch the border | |
831 | x += 2; | |
832 | #endif | |
833 | ||
834 | size_t col = 0; | |
835 | for ( wxListItemDataList::compatibility_iterator node = m_items.GetFirst(); | |
836 | node; | |
837 | node = node->GetNext(), col++ ) | |
838 | { | |
839 | wxListItemData *item = node->GetData(); | |
840 | ||
841 | int width = m_owner->GetColumnWidth(col); | |
842 | int xOld = x; | |
843 | x += width; | |
844 | ||
845 | const int wText = width - 8; | |
846 | wxDCClipper clipper(*dc, xOld, rect.y, wText, rect.height); | |
847 | ||
848 | if ( item->HasImage() ) | |
849 | { | |
850 | int ix, iy; | |
851 | m_owner->GetImageSize( item->GetImage(), ix, iy ); | |
852 | m_owner->DrawImage( item->GetImage(), dc, xOld, yMid - iy/2 ); | |
853 | ||
854 | ix += IMAGE_MARGIN_IN_REPORT_MODE; | |
855 | ||
856 | xOld += ix; | |
857 | width -= ix; | |
858 | } | |
859 | ||
860 | if ( item->HasText() ) | |
861 | DrawTextFormatted(dc, item->GetText(), col, xOld, yMid, wText); | |
862 | } | |
863 | } | |
864 | ||
865 | void wxListLineData::DrawTextFormatted(wxDC *dc, | |
866 | const wxString& textOrig, | |
867 | int col, | |
868 | int x, | |
869 | int yMid, | |
870 | int width) | |
871 | { | |
872 | // we don't support displaying multiple lines currently (and neither does | |
873 | // wxMSW FWIW) so just merge all the lines | |
874 | wxString text(textOrig); | |
875 | text.Replace(_T("\n"), _T(" ")); | |
876 | ||
877 | wxCoord w, h; | |
878 | dc->GetTextExtent(text, &w, &h); | |
879 | ||
880 | const wxCoord y = yMid - (h + 1)/2; | |
881 | ||
882 | wxDCClipper clipper(*dc, x, y, width, h); | |
883 | ||
884 | // determine if the string can fit inside the current width | |
885 | if (w <= width) | |
886 | { | |
887 | // it can, draw it using the items alignment | |
888 | wxListItem item; | |
889 | m_owner->GetColumn(col, item); | |
890 | switch ( item.GetAlign() ) | |
891 | { | |
892 | case wxLIST_FORMAT_LEFT: | |
893 | // nothing to do | |
894 | break; | |
895 | ||
896 | case wxLIST_FORMAT_RIGHT: | |
897 | x += width - w; | |
898 | break; | |
899 | ||
900 | case wxLIST_FORMAT_CENTER: | |
901 | x += (width - w) / 2; | |
902 | break; | |
903 | ||
904 | default: | |
905 | wxFAIL_MSG( _T("unknown list item format") ); | |
906 | break; | |
907 | } | |
908 | ||
909 | dc->DrawText(text, x, y); | |
910 | } | |
911 | else // otherwise, truncate and add an ellipsis if possible | |
912 | { | |
913 | // determine the base width | |
914 | wxString ellipsis(wxT("...")); | |
915 | wxCoord base_w; | |
916 | dc->GetTextExtent(ellipsis, &base_w, &h); | |
917 | ||
918 | // continue until we have enough space or only one character left | |
919 | wxCoord w_c, h_c; | |
920 | size_t len = text.length(); | |
921 | wxString drawntext = text.Left(len); | |
922 | while (len > 1) | |
923 | { | |
924 | dc->GetTextExtent(drawntext.Last(), &w_c, &h_c); | |
925 | drawntext.RemoveLast(); | |
926 | len--; | |
927 | w -= w_c; | |
928 | if (w + base_w <= width) | |
929 | break; | |
930 | } | |
931 | ||
932 | // if still not enough space, remove ellipsis characters | |
933 | while (ellipsis.length() > 0 && w + base_w > width) | |
934 | { | |
935 | ellipsis = ellipsis.Left(ellipsis.length() - 1); | |
936 | dc->GetTextExtent(ellipsis, &base_w, &h); | |
937 | } | |
938 | ||
939 | // now draw the text | |
940 | dc->DrawText(drawntext, x, y); | |
941 | dc->DrawText(ellipsis, x + w, y); | |
942 | } | |
943 | } | |
944 | ||
945 | bool wxListLineData::Highlight( bool on ) | |
946 | { | |
947 | wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") ); | |
948 | ||
949 | if ( on == m_highlighted ) | |
950 | return false; | |
951 | ||
952 | m_highlighted = on; | |
953 | ||
954 | return true; | |
955 | } | |
956 | ||
957 | void wxListLineData::ReverseHighlight( void ) | |
958 | { | |
959 | Highlight(!IsHighlighted()); | |
960 | } | |
961 | ||
962 | //----------------------------------------------------------------------------- | |
963 | // wxListHeaderWindow | |
964 | //----------------------------------------------------------------------------- | |
965 | ||
966 | BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow) | |
967 | EVT_PAINT (wxListHeaderWindow::OnPaint) | |
968 | EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse) | |
969 | EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus) | |
970 | END_EVENT_TABLE() | |
971 | ||
972 | void wxListHeaderWindow::Init() | |
973 | { | |
974 | m_currentCursor = NULL; | |
975 | m_isDragging = false; | |
976 | m_dirty = false; | |
977 | m_sendSetColumnWidth = false; | |
978 | } | |
979 | ||
980 | wxListHeaderWindow::wxListHeaderWindow() | |
981 | { | |
982 | Init(); | |
983 | ||
984 | m_owner = NULL; | |
985 | m_resizeCursor = NULL; | |
986 | } | |
987 | ||
988 | wxListHeaderWindow::wxListHeaderWindow( wxWindow *win, | |
989 | wxWindowID id, | |
990 | wxListMainWindow *owner, | |
991 | const wxPoint& pos, | |
992 | const wxSize& size, | |
993 | long style, | |
994 | const wxString &name ) | |
995 | : wxWindow( win, id, pos, size, style, name ) | |
996 | { | |
997 | Init(); | |
998 | ||
999 | m_owner = owner; | |
1000 | m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE ); | |
1001 | ||
1002 | #if _USE_VISATTR | |
1003 | wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes(); | |
1004 | SetOwnForegroundColour( attr.colFg ); | |
1005 | SetOwnBackgroundColour( attr.colBg ); | |
1006 | if (!m_hasFont) | |
1007 | SetOwnFont( attr.font ); | |
1008 | #else | |
1009 | SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); | |
1010 | SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); | |
1011 | if (!m_hasFont) | |
1012 | SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT )); | |
1013 | #endif | |
1014 | } | |
1015 | ||
1016 | wxListHeaderWindow::~wxListHeaderWindow() | |
1017 | { | |
1018 | delete m_resizeCursor; | |
1019 | } | |
1020 | ||
1021 | #ifdef __WXUNIVERSAL__ | |
1022 | #include "wx/univ/renderer.h" | |
1023 | #include "wx/univ/theme.h" | |
1024 | #endif | |
1025 | ||
1026 | // shift the DC origin to match the position of the main window horz | |
1027 | // scrollbar: this allows us to always use logical coords | |
1028 | void wxListHeaderWindow::AdjustDC(wxDC& dc) | |
1029 | { | |
1030 | wxGenericListCtrl *parent = m_owner->GetListCtrl(); | |
1031 | ||
1032 | int xpix; | |
1033 | parent->GetScrollPixelsPerUnit( &xpix, NULL ); | |
1034 | ||
1035 | int view_start; | |
1036 | parent->GetViewStart( &view_start, NULL ); | |
1037 | ||
1038 | ||
1039 | int org_x = 0; | |
1040 | int org_y = 0; | |
1041 | dc.GetDeviceOrigin( &org_x, &org_y ); | |
1042 | ||
1043 | // account for the horz scrollbar offset | |
1044 | #ifdef __WXGTK__ | |
1045 | if (GetLayoutDirection() == wxLayout_RightToLeft) | |
1046 | { | |
1047 | // Maybe we just have to check for m_signX | |
1048 | // in the DC, but I leave the #ifdef __WXGTK__ | |
1049 | // for now | |
1050 | dc.SetDeviceOrigin( org_x + (view_start * xpix), org_y ); | |
1051 | } | |
1052 | else | |
1053 | #endif | |
1054 | dc.SetDeviceOrigin( org_x - (view_start * xpix), org_y ); | |
1055 | } | |
1056 | ||
1057 | void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
1058 | { | |
1059 | wxGenericListCtrl *parent = m_owner->GetListCtrl(); | |
1060 | ||
1061 | wxPaintDC dc( this ); | |
1062 | ||
1063 | AdjustDC( dc ); | |
1064 | ||
1065 | dc.SetFont( GetFont() ); | |
1066 | ||
1067 | // width and height of the entire header window | |
1068 | int w, h; | |
1069 | GetClientSize( &w, &h ); | |
1070 | parent->CalcUnscrolledPosition(w, 0, &w, NULL); | |
1071 | ||
1072 | dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); | |
1073 | dc.SetTextForeground(GetForegroundColour()); | |
1074 | ||
1075 | int x = HEADER_OFFSET_X; | |
1076 | int numColumns = m_owner->GetColumnCount(); | |
1077 | wxListItem item; | |
1078 | for ( int i = 0; i < numColumns && x < w; i++ ) | |
1079 | { | |
1080 | m_owner->GetColumn( i, item ); | |
1081 | int wCol = item.m_width; | |
1082 | ||
1083 | int cw = wCol; | |
1084 | int ch = h; | |
1085 | ||
1086 | int flags = 0; | |
1087 | if (!m_parent->IsEnabled()) | |
1088 | flags |= wxCONTROL_DISABLED; | |
1089 | ||
1090 | // NB: The code below is not really Mac-specific, but since we are close | |
1091 | // to 2.8 release and I don't have time to test on other platforms, I | |
1092 | // defined this only for wxMac. If this behavior is desired on | |
1093 | // other platforms, please go ahead and revise or remove the #ifdef. | |
1094 | #ifdef __WXMAC__ | |
1095 | if ( !m_owner->IsVirtual() && (item.m_mask & wxLIST_MASK_STATE) && | |
1096 | (item.m_state & wxLIST_STATE_SELECTED) ) | |
1097 | flags |= wxCONTROL_SELECTED; | |
1098 | #endif | |
1099 | ||
1100 | if (i == 0) | |
1101 | flags |= wxCONTROL_SPECIAL; // mark as first column | |
1102 | ||
1103 | wxRendererNative::Get().DrawHeaderButton | |
1104 | ( | |
1105 | this, | |
1106 | dc, | |
1107 | wxRect(x, HEADER_OFFSET_Y, cw, ch), | |
1108 | flags | |
1109 | ); | |
1110 | ||
1111 | // see if we have enough space for the column label | |
1112 | ||
1113 | // for this we need the width of the text | |
1114 | wxCoord wLabel; | |
1115 | wxCoord hLabel; | |
1116 | dc.GetTextExtent(item.GetText(), &wLabel, &hLabel); | |
1117 | wLabel += 2 * EXTRA_WIDTH; | |
1118 | ||
1119 | // and the width of the icon, if any | |
1120 | int ix = 0, iy = 0; // init them just to suppress the compiler warnings | |
1121 | const int image = item.m_image; | |
1122 | wxImageList *imageList; | |
1123 | if ( image != -1 ) | |
1124 | { | |
1125 | imageList = m_owner->GetSmallImageList(); | |
1126 | if ( imageList ) | |
1127 | { | |
1128 | imageList->GetSize(image, ix, iy); | |
1129 | wLabel += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE; | |
1130 | } | |
1131 | } | |
1132 | else | |
1133 | { | |
1134 | imageList = NULL; | |
1135 | } | |
1136 | ||
1137 | // ignore alignment if there is not enough space anyhow | |
1138 | int xAligned; | |
1139 | switch ( wLabel < cw ? item.GetAlign() : wxLIST_FORMAT_LEFT ) | |
1140 | { | |
1141 | default: | |
1142 | wxFAIL_MSG( _T("unknown list item format") ); | |
1143 | // fall through | |
1144 | ||
1145 | case wxLIST_FORMAT_LEFT: | |
1146 | xAligned = x; | |
1147 | break; | |
1148 | ||
1149 | case wxLIST_FORMAT_RIGHT: | |
1150 | xAligned = x + cw - wLabel; | |
1151 | break; | |
1152 | ||
1153 | case wxLIST_FORMAT_CENTER: | |
1154 | xAligned = x + (cw - wLabel) / 2; | |
1155 | break; | |
1156 | } | |
1157 | ||
1158 | // draw the text and image clipping them so that they | |
1159 | // don't overwrite the column boundary | |
1160 | wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h - 4 ); | |
1161 | ||
1162 | // if we have an image, draw it on the right of the label | |
1163 | if ( imageList ) | |
1164 | { | |
1165 | imageList->Draw | |
1166 | ( | |
1167 | image, | |
1168 | dc, | |
1169 | xAligned + wLabel - ix - HEADER_IMAGE_MARGIN_IN_REPORT_MODE, | |
1170 | HEADER_OFFSET_Y + (h - 4 - iy)/2, | |
1171 | wxIMAGELIST_DRAW_TRANSPARENT | |
1172 | ); | |
1173 | } | |
1174 | ||
1175 | dc.DrawText( item.GetText(), | |
1176 | xAligned + EXTRA_WIDTH, h / 2 - hLabel / 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT ); | |
1177 | ||
1178 | x += wCol; | |
1179 | } | |
1180 | ||
1181 | // Fill in what's missing to the right of the columns, otherwise we will | |
1182 | // leave an unpainted area when columns are removed (and it looks better) | |
1183 | if ( x < w ) | |
1184 | { | |
1185 | wxRendererNative::Get().DrawHeaderButton | |
1186 | ( | |
1187 | this, | |
1188 | dc, | |
1189 | wxRect(x, HEADER_OFFSET_Y, w - x, h), | |
1190 | wxCONTROL_DIRTY // mark as last column | |
1191 | ); | |
1192 | } | |
1193 | } | |
1194 | ||
1195 | void wxListHeaderWindow::OnInternalIdle() | |
1196 | { | |
1197 | wxWindow::OnInternalIdle(); | |
1198 | ||
1199 | if (m_sendSetColumnWidth) | |
1200 | { | |
1201 | m_owner->SetColumnWidth( m_colToSend, m_widthToSend ); | |
1202 | m_sendSetColumnWidth = false; | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | void wxListHeaderWindow::DrawCurrent() | |
1207 | { | |
1208 | #if 1 | |
1209 | // m_owner->SetColumnWidth( m_column, m_currentX - m_minX ); | |
1210 | m_sendSetColumnWidth = true; | |
1211 | m_colToSend = m_column; | |
1212 | m_widthToSend = m_currentX - m_minX; | |
1213 | #else | |
1214 | int x1 = m_currentX; | |
1215 | int y1 = 0; | |
1216 | m_owner->ClientToScreen( &x1, &y1 ); | |
1217 | ||
1218 | int x2 = m_currentX; | |
1219 | int y2 = 0; | |
1220 | m_owner->GetClientSize( NULL, &y2 ); | |
1221 | m_owner->ClientToScreen( &x2, &y2 ); | |
1222 | ||
1223 | wxScreenDC dc; | |
1224 | dc.SetLogicalFunction( wxINVERT ); | |
1225 | dc.SetPen( wxPen( *wxBLACK, 2, wxSOLID ) ); | |
1226 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); | |
1227 | ||
1228 | AdjustDC(dc); | |
1229 | ||
1230 | dc.DrawLine( x1, y1, x2, y2 ); | |
1231 | ||
1232 | dc.SetLogicalFunction( wxCOPY ); | |
1233 | ||
1234 | dc.SetPen( wxNullPen ); | |
1235 | dc.SetBrush( wxNullBrush ); | |
1236 | #endif | |
1237 | } | |
1238 | ||
1239 | void wxListHeaderWindow::OnMouse( wxMouseEvent &event ) | |
1240 | { | |
1241 | wxGenericListCtrl *parent = m_owner->GetListCtrl(); | |
1242 | ||
1243 | // we want to work with logical coords | |
1244 | int x; | |
1245 | parent->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL); | |
1246 | int y = event.GetY(); | |
1247 | ||
1248 | if (m_isDragging) | |
1249 | { | |
1250 | SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING, event.GetPosition()); | |
1251 | ||
1252 | // we don't draw the line beyond our window, but we allow dragging it | |
1253 | // there | |
1254 | int w = 0; | |
1255 | GetClientSize( &w, NULL ); | |
1256 | parent->CalcUnscrolledPosition(w, 0, &w, NULL); | |
1257 | w -= 6; | |
1258 | ||
1259 | // erase the line if it was drawn | |
1260 | if ( m_currentX < w ) | |
1261 | DrawCurrent(); | |
1262 | ||
1263 | if (event.ButtonUp()) | |
1264 | { | |
1265 | ReleaseMouse(); | |
1266 | m_isDragging = false; | |
1267 | m_dirty = true; | |
1268 | m_owner->SetColumnWidth( m_column, m_currentX - m_minX ); | |
1269 | SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG, event.GetPosition()); | |
1270 | } | |
1271 | else | |
1272 | { | |
1273 | if (x > m_minX + 7) | |
1274 | m_currentX = x; | |
1275 | else | |
1276 | m_currentX = m_minX + 7; | |
1277 | ||
1278 | // draw in the new location | |
1279 | if ( m_currentX < w ) | |
1280 | DrawCurrent(); | |
1281 | } | |
1282 | } | |
1283 | else // not dragging | |
1284 | { | |
1285 | m_minX = 0; | |
1286 | bool hit_border = false; | |
1287 | ||
1288 | // end of the current column | |
1289 | int xpos = 0; | |
1290 | ||
1291 | // find the column where this event occurred | |
1292 | int col, | |
1293 | countCol = m_owner->GetColumnCount(); | |
1294 | for (col = 0; col < countCol; col++) | |
1295 | { | |
1296 | xpos += m_owner->GetColumnWidth( col ); | |
1297 | m_column = col; | |
1298 | ||
1299 | if ( (abs(x-xpos) < 3) && (y < 22) ) | |
1300 | { | |
1301 | // near the column border | |
1302 | hit_border = true; | |
1303 | break; | |
1304 | } | |
1305 | ||
1306 | if ( x < xpos ) | |
1307 | { | |
1308 | // inside the column | |
1309 | break; | |
1310 | } | |
1311 | ||
1312 | m_minX = xpos; | |
1313 | } | |
1314 | ||
1315 | if ( col == countCol ) | |
1316 | m_column = -1; | |
1317 | ||
1318 | if (event.LeftDown() || event.RightUp()) | |
1319 | { | |
1320 | if (hit_border && event.LeftDown()) | |
1321 | { | |
1322 | if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG, | |
1323 | event.GetPosition()) ) | |
1324 | { | |
1325 | m_isDragging = true; | |
1326 | m_currentX = x; | |
1327 | CaptureMouse(); | |
1328 | DrawCurrent(); | |
1329 | } | |
1330 | //else: column resizing was vetoed by the user code | |
1331 | } | |
1332 | else // click on a column | |
1333 | { | |
1334 | // record the selected state of the columns | |
1335 | if (event.LeftDown()) | |
1336 | { | |
1337 | for (int i=0; i < m_owner->GetColumnCount(); i++) | |
1338 | { | |
1339 | wxListItem colItem; | |
1340 | m_owner->GetColumn(i, colItem); | |
1341 | long state = colItem.GetState(); | |
1342 | if (i == m_column) | |
1343 | colItem.SetState(state | wxLIST_STATE_SELECTED); | |
1344 | else | |
1345 | colItem.SetState(state & ~wxLIST_STATE_SELECTED); | |
1346 | m_owner->SetColumn(i, colItem); | |
1347 | } | |
1348 | } | |
1349 | ||
1350 | SendListEvent( event.LeftDown() | |
1351 | ? wxEVT_COMMAND_LIST_COL_CLICK | |
1352 | : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK, | |
1353 | event.GetPosition()); | |
1354 | } | |
1355 | } | |
1356 | else if (event.Moving()) | |
1357 | { | |
1358 | bool setCursor; | |
1359 | if (hit_border) | |
1360 | { | |
1361 | setCursor = m_currentCursor == wxSTANDARD_CURSOR; | |
1362 | m_currentCursor = m_resizeCursor; | |
1363 | } | |
1364 | else | |
1365 | { | |
1366 | setCursor = m_currentCursor != wxSTANDARD_CURSOR; | |
1367 | m_currentCursor = wxSTANDARD_CURSOR; | |
1368 | } | |
1369 | ||
1370 | if ( setCursor ) | |
1371 | SetCursor(*m_currentCursor); | |
1372 | } | |
1373 | } | |
1374 | } | |
1375 | ||
1376 | void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) ) | |
1377 | { | |
1378 | m_owner->SetFocus(); | |
1379 | m_owner->Update(); | |
1380 | } | |
1381 | ||
1382 | bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos) | |
1383 | { | |
1384 | wxWindow *parent = GetParent(); | |
1385 | wxListEvent le( type, parent->GetId() ); | |
1386 | le.SetEventObject( parent ); | |
1387 | le.m_pointDrag = pos; | |
1388 | ||
1389 | // the position should be relative to the parent window, not | |
1390 | // this one for compatibility with MSW and common sense: the | |
1391 | // user code doesn't know anything at all about this header | |
1392 | // window, so why should it get positions relative to it? | |
1393 | le.m_pointDrag.y -= GetSize().y; | |
1394 | ||
1395 | le.m_col = m_column; | |
1396 | return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed(); | |
1397 | } | |
1398 | ||
1399 | //----------------------------------------------------------------------------- | |
1400 | // wxListRenameTimer (internal) | |
1401 | //----------------------------------------------------------------------------- | |
1402 | ||
1403 | wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner ) | |
1404 | { | |
1405 | m_owner = owner; | |
1406 | } | |
1407 | ||
1408 | void wxListRenameTimer::Notify() | |
1409 | { | |
1410 | m_owner->OnRenameTimer(); | |
1411 | } | |
1412 | ||
1413 | //----------------------------------------------------------------------------- | |
1414 | // wxListTextCtrlWrapper (internal) | |
1415 | //----------------------------------------------------------------------------- | |
1416 | ||
1417 | BEGIN_EVENT_TABLE(wxListTextCtrlWrapper, wxEvtHandler) | |
1418 | EVT_CHAR (wxListTextCtrlWrapper::OnChar) | |
1419 | EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp) | |
1420 | EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus) | |
1421 | END_EVENT_TABLE() | |
1422 | ||
1423 | wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow *owner, | |
1424 | wxTextCtrl *text, | |
1425 | size_t itemEdit) | |
1426 | : m_startValue(owner->GetItemText(itemEdit)), | |
1427 | m_itemEdited(itemEdit) | |
1428 | { | |
1429 | m_owner = owner; | |
1430 | m_text = text; | |
1431 | m_aboutToFinish = false; | |
1432 | ||
1433 | wxGenericListCtrl *parent = m_owner->GetListCtrl(); | |
1434 | ||
1435 | wxRect rectLabel = owner->GetLineLabelRect(itemEdit); | |
1436 | ||
1437 | parent->CalcScrolledPosition(rectLabel.x, rectLabel.y, | |
1438 | &rectLabel.x, &rectLabel.y); | |
1439 | ||
1440 | m_text->Create(owner, wxID_ANY, m_startValue, | |
1441 | wxPoint(rectLabel.x-4,rectLabel.y-4), | |
1442 | wxSize(rectLabel.width+11,rectLabel.height+8)); | |
1443 | m_text->SetFocus(); | |
1444 | ||
1445 | m_text->PushEventHandler(this); | |
1446 | } | |
1447 | ||
1448 | void wxListTextCtrlWrapper::EndEdit(bool discardChanges) | |
1449 | { | |
1450 | m_aboutToFinish = true; | |
1451 | ||
1452 | if ( discardChanges ) | |
1453 | { | |
1454 | m_owner->OnRenameCancelled(m_itemEdited); | |
1455 | ||
1456 | Finish( true ); | |
1457 | } | |
1458 | else | |
1459 | { | |
1460 | // Notify the owner about the changes | |
1461 | AcceptChanges(); | |
1462 | ||
1463 | // Even if vetoed, close the control (consistent with MSW) | |
1464 | Finish( true ); | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | void wxListTextCtrlWrapper::Finish( bool setfocus ) | |
1469 | { | |
1470 | m_text->RemoveEventHandler(this); | |
1471 | m_owner->ResetTextControl( m_text ); | |
1472 | ||
1473 | wxPendingDelete.Append( this ); | |
1474 | ||
1475 | if (setfocus) | |
1476 | m_owner->SetFocus(); | |
1477 | } | |
1478 | ||
1479 | bool wxListTextCtrlWrapper::AcceptChanges() | |
1480 | { | |
1481 | const wxString value = m_text->GetValue(); | |
1482 | ||
1483 | // notice that we should always call OnRenameAccept() to generate the "end | |
1484 | // label editing" event, even if the user hasn't really changed anything | |
1485 | if ( !m_owner->OnRenameAccept(m_itemEdited, value) ) | |
1486 | { | |
1487 | // vetoed by the user | |
1488 | return false; | |
1489 | } | |
1490 | ||
1491 | // accepted, do rename the item (unless nothing changed) | |
1492 | if ( value != m_startValue ) | |
1493 | m_owner->SetItemText(m_itemEdited, value); | |
1494 | ||
1495 | return true; | |
1496 | } | |
1497 | ||
1498 | void wxListTextCtrlWrapper::OnChar( wxKeyEvent &event ) | |
1499 | { | |
1500 | switch ( event.m_keyCode ) | |
1501 | { | |
1502 | case WXK_RETURN: | |
1503 | EndEdit( false ); | |
1504 | break; | |
1505 | ||
1506 | case WXK_ESCAPE: | |
1507 | EndEdit( true ); | |
1508 | break; | |
1509 | ||
1510 | default: | |
1511 | event.Skip(); | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event ) | |
1516 | { | |
1517 | if (m_aboutToFinish) | |
1518 | { | |
1519 | // auto-grow the textctrl: | |
1520 | wxSize parentSize = m_owner->GetSize(); | |
1521 | wxPoint myPos = m_text->GetPosition(); | |
1522 | wxSize mySize = m_text->GetSize(); | |
1523 | int sx, sy; | |
1524 | m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy); | |
1525 | if (myPos.x + sx > parentSize.x) | |
1526 | sx = parentSize.x - myPos.x; | |
1527 | if (mySize.x > sx) | |
1528 | sx = mySize.x; | |
1529 | m_text->SetSize(sx, wxDefaultCoord); | |
1530 | } | |
1531 | ||
1532 | event.Skip(); | |
1533 | } | |
1534 | ||
1535 | void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event ) | |
1536 | { | |
1537 | if ( !m_aboutToFinish ) | |
1538 | { | |
1539 | if ( !AcceptChanges() ) | |
1540 | m_owner->OnRenameCancelled( m_itemEdited ); | |
1541 | ||
1542 | Finish( false ); | |
1543 | } | |
1544 | ||
1545 | // We must let the native text control handle focus | |
1546 | event.Skip(); | |
1547 | } | |
1548 | ||
1549 | //----------------------------------------------------------------------------- | |
1550 | // wxListMainWindow | |
1551 | //----------------------------------------------------------------------------- | |
1552 | ||
1553 | BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledCanvas) | |
1554 | EVT_PAINT (wxListMainWindow::OnPaint) | |
1555 | EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse) | |
1556 | EVT_CHAR (wxListMainWindow::OnChar) | |
1557 | EVT_KEY_DOWN (wxListMainWindow::OnKeyDown) | |
1558 | EVT_KEY_UP (wxListMainWindow::OnKeyUp) | |
1559 | EVT_SET_FOCUS (wxListMainWindow::OnSetFocus) | |
1560 | EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus) | |
1561 | EVT_SCROLLWIN (wxListMainWindow::OnScroll) | |
1562 | EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus) | |
1563 | END_EVENT_TABLE() | |
1564 | ||
1565 | void wxListMainWindow::Init() | |
1566 | { | |
1567 | m_dirty = true; | |
1568 | m_countVirt = 0; | |
1569 | m_lineFrom = | |
1570 | m_lineTo = (size_t)-1; | |
1571 | m_linesPerPage = 0; | |
1572 | ||
1573 | m_headerWidth = | |
1574 | m_lineHeight = 0; | |
1575 | ||
1576 | m_small_image_list = NULL; | |
1577 | m_normal_image_list = NULL; | |
1578 | ||
1579 | m_small_spacing = 30; | |
1580 | m_normal_spacing = 40; | |
1581 | ||
1582 | m_hasFocus = false; | |
1583 | m_dragCount = 0; | |
1584 | m_isCreated = false; | |
1585 | ||
1586 | m_lastOnSame = false; | |
1587 | m_renameTimer = new wxListRenameTimer( this ); | |
1588 | m_textctrlWrapper = NULL; | |
1589 | ||
1590 | m_current = | |
1591 | m_lineLastClicked = | |
1592 | m_lineSelectSingleOnUp = | |
1593 | m_lineBeforeLastClicked = (size_t)-1; | |
1594 | } | |
1595 | ||
1596 | wxListMainWindow::wxListMainWindow() | |
1597 | { | |
1598 | Init(); | |
1599 | ||
1600 | m_highlightBrush = | |
1601 | m_highlightUnfocusedBrush = NULL; | |
1602 | } | |
1603 | ||
1604 | wxListMainWindow::wxListMainWindow( wxWindow *parent, | |
1605 | wxWindowID id, | |
1606 | const wxPoint& pos, | |
1607 | const wxSize& size, | |
1608 | long style, | |
1609 | const wxString &name ) | |
1610 | : wxWindow( parent, id, pos, size, style, name ) | |
1611 | { | |
1612 | Init(); | |
1613 | ||
1614 | m_highlightBrush = new wxBrush | |
1615 | ( | |
1616 | wxSystemSettings::GetColour | |
1617 | ( | |
1618 | wxSYS_COLOUR_HIGHLIGHT | |
1619 | ), | |
1620 | wxBRUSHSTYLE_SOLID | |
1621 | ); | |
1622 | ||
1623 | m_highlightUnfocusedBrush = new wxBrush | |
1624 | ( | |
1625 | wxSystemSettings::GetColour | |
1626 | ( | |
1627 | wxSYS_COLOUR_BTNSHADOW | |
1628 | ), | |
1629 | wxBRUSHSTYLE_SOLID | |
1630 | ); | |
1631 | ||
1632 | wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes(); | |
1633 | SetOwnForegroundColour( attr.colFg ); | |
1634 | SetOwnBackgroundColour( attr.colBg ); | |
1635 | if (!m_hasFont) | |
1636 | SetOwnFont( attr.font ); | |
1637 | } | |
1638 | ||
1639 | wxListMainWindow::~wxListMainWindow() | |
1640 | { | |
1641 | DoDeleteAllItems(); | |
1642 | WX_CLEAR_LIST(wxListHeaderDataList, m_columns); | |
1643 | WX_CLEAR_ARRAY(m_aColWidths); | |
1644 | ||
1645 | delete m_highlightBrush; | |
1646 | delete m_highlightUnfocusedBrush; | |
1647 | delete m_renameTimer; | |
1648 | } | |
1649 | ||
1650 | void wxListMainWindow::CacheLineData(size_t line) | |
1651 | { | |
1652 | wxGenericListCtrl *listctrl = GetListCtrl(); | |
1653 | ||
1654 | wxListLineData *ld = GetDummyLine(); | |
1655 | ||
1656 | size_t countCol = GetColumnCount(); | |
1657 | for ( size_t col = 0; col < countCol; col++ ) | |
1658 | { | |
1659 | ld->SetText(col, listctrl->OnGetItemText(line, col)); | |
1660 | ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col)); | |
1661 | } | |
1662 | ||
1663 | ld->SetAttr(listctrl->OnGetItemAttr(line)); | |
1664 | } | |
1665 | ||
1666 | wxListLineData *wxListMainWindow::GetDummyLine() const | |
1667 | { | |
1668 | wxASSERT_MSG( !IsEmpty(), _T("invalid line index") ); | |
1669 | wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") ); | |
1670 | ||
1671 | wxListMainWindow *self = wxConstCast(this, wxListMainWindow); | |
1672 | ||
1673 | // we need to recreate the dummy line if the number of columns in the | |
1674 | // control changed as it would have the incorrect number of fields | |
1675 | // otherwise | |
1676 | if ( !m_lines.IsEmpty() && | |
1677 | m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() ) | |
1678 | { | |
1679 | self->m_lines.Clear(); | |
1680 | } | |
1681 | ||
1682 | if ( m_lines.IsEmpty() ) | |
1683 | { | |
1684 | wxListLineData *line = new wxListLineData(self); | |
1685 | self->m_lines.Add(line); | |
1686 | ||
1687 | // don't waste extra memory -- there never going to be anything | |
1688 | // else/more in this array | |
1689 | self->m_lines.Shrink(); | |
1690 | } | |
1691 | ||
1692 | return &m_lines[0]; | |
1693 | } | |
1694 | ||
1695 | // ---------------------------------------------------------------------------- | |
1696 | // line geometry (report mode only) | |
1697 | // ---------------------------------------------------------------------------- | |
1698 | ||
1699 | wxCoord wxListMainWindow::GetLineHeight() const | |
1700 | { | |
1701 | // we cache the line height as calling GetTextExtent() is slow | |
1702 | if ( !m_lineHeight ) | |
1703 | { | |
1704 | wxListMainWindow *self = wxConstCast(this, wxListMainWindow); | |
1705 | ||
1706 | wxClientDC dc( self ); | |
1707 | dc.SetFont( GetFont() ); | |
1708 | ||
1709 | wxCoord y; | |
1710 | dc.GetTextExtent(_T("H"), NULL, &y); | |
1711 | ||
1712 | if ( m_small_image_list && m_small_image_list->GetImageCount() ) | |
1713 | { | |
1714 | int iw = 0, ih = 0; | |
1715 | m_small_image_list->GetSize(0, iw, ih); | |
1716 | y = wxMax(y, ih); | |
1717 | } | |
1718 | ||
1719 | y += EXTRA_HEIGHT; | |
1720 | self->m_lineHeight = y + LINE_SPACING; | |
1721 | } | |
1722 | ||
1723 | return m_lineHeight; | |
1724 | } | |
1725 | ||
1726 | wxCoord wxListMainWindow::GetLineY(size_t line) const | |
1727 | { | |
1728 | wxASSERT_MSG( InReportView(), _T("only works in report mode") ); | |
1729 | ||
1730 | return LINE_SPACING + line * GetLineHeight(); | |
1731 | } | |
1732 | ||
1733 | wxRect wxListMainWindow::GetLineRect(size_t line) const | |
1734 | { | |
1735 | if ( !InReportView() ) | |
1736 | return GetLine(line)->m_gi->m_rectAll; | |
1737 | ||
1738 | wxRect rect; | |
1739 | rect.x = HEADER_OFFSET_X; | |
1740 | rect.y = GetLineY(line); | |
1741 | rect.width = GetHeaderWidth(); | |
1742 | rect.height = GetLineHeight(); | |
1743 | ||
1744 | return rect; | |
1745 | } | |
1746 | ||
1747 | wxRect wxListMainWindow::GetLineLabelRect(size_t line) const | |
1748 | { | |
1749 | if ( !InReportView() ) | |
1750 | return GetLine(line)->m_gi->m_rectLabel; | |
1751 | ||
1752 | int image_x = 0; | |
1753 | wxListLineData *data = GetLine(line); | |
1754 | wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst(); | |
1755 | if (node) | |
1756 | { | |
1757 | wxListItemData *item = node->GetData(); | |
1758 | if ( item->HasImage() ) | |
1759 | { | |
1760 | int ix, iy; | |
1761 | GetImageSize( item->GetImage(), ix, iy ); | |
1762 | image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE; | |
1763 | } | |
1764 | } | |
1765 | ||
1766 | wxRect rect; | |
1767 | rect.x = image_x + HEADER_OFFSET_X; | |
1768 | rect.y = GetLineY(line); | |
1769 | rect.width = GetColumnWidth(0) - image_x; | |
1770 | rect.height = GetLineHeight(); | |
1771 | ||
1772 | return rect; | |
1773 | } | |
1774 | ||
1775 | wxRect wxListMainWindow::GetLineIconRect(size_t line) const | |
1776 | { | |
1777 | if ( !InReportView() ) | |
1778 | return GetLine(line)->m_gi->m_rectIcon; | |
1779 | ||
1780 | wxListLineData *ld = GetLine(line); | |
1781 | wxASSERT_MSG( ld->HasImage(), _T("should have an image") ); | |
1782 | ||
1783 | wxRect rect; | |
1784 | rect.x = HEADER_OFFSET_X; | |
1785 | rect.y = GetLineY(line); | |
1786 | GetImageSize(ld->GetImage(), rect.width, rect.height); | |
1787 | ||
1788 | return rect; | |
1789 | } | |
1790 | ||
1791 | wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const | |
1792 | { | |
1793 | return InReportView() ? GetLineRect(line) | |
1794 | : GetLine(line)->m_gi->m_rectHighlight; | |
1795 | } | |
1796 | ||
1797 | long wxListMainWindow::HitTestLine(size_t line, int x, int y) const | |
1798 | { | |
1799 | wxASSERT_MSG( line < GetItemCount(), _T("invalid line in HitTestLine") ); | |
1800 | ||
1801 | wxListLineData *ld = GetLine(line); | |
1802 | ||
1803 | if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) ) | |
1804 | return wxLIST_HITTEST_ONITEMICON; | |
1805 | ||
1806 | // VS: Testing for "ld->HasText() || InReportView()" instead of | |
1807 | // "ld->HasText()" is needed to make empty lines in report view | |
1808 | // possible | |
1809 | if ( ld->HasText() || InReportView() ) | |
1810 | { | |
1811 | wxRect rect = InReportView() ? GetLineRect(line) | |
1812 | : GetLineLabelRect(line); | |
1813 | ||
1814 | if ( rect.Contains(x, y) ) | |
1815 | return wxLIST_HITTEST_ONITEMLABEL; | |
1816 | } | |
1817 | ||
1818 | return 0; | |
1819 | } | |
1820 | ||
1821 | // ---------------------------------------------------------------------------- | |
1822 | // highlight (selection) handling | |
1823 | // ---------------------------------------------------------------------------- | |
1824 | ||
1825 | bool wxListMainWindow::IsHighlighted(size_t line) const | |
1826 | { | |
1827 | if ( IsVirtual() ) | |
1828 | { | |
1829 | return m_selStore.IsSelected(line); | |
1830 | } | |
1831 | else // !virtual | |
1832 | { | |
1833 | wxListLineData *ld = GetLine(line); | |
1834 | wxCHECK_MSG( ld, false, _T("invalid index in IsHighlighted") ); | |
1835 | ||
1836 | return ld->IsHighlighted(); | |
1837 | } | |
1838 | } | |
1839 | ||
1840 | void wxListMainWindow::HighlightLines( size_t lineFrom, | |
1841 | size_t lineTo, | |
1842 | bool highlight ) | |
1843 | { | |
1844 | if ( IsVirtual() ) | |
1845 | { | |
1846 | wxArrayInt linesChanged; | |
1847 | if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight, | |
1848 | &linesChanged) ) | |
1849 | { | |
1850 | // meny items changed state, refresh everything | |
1851 | RefreshLines(lineFrom, lineTo); | |
1852 | } | |
1853 | else // only a few items changed state, refresh only them | |
1854 | { | |
1855 | size_t count = linesChanged.GetCount(); | |
1856 | for ( size_t n = 0; n < count; n++ ) | |
1857 | { | |
1858 | RefreshLine(linesChanged[n]); | |
1859 | } | |
1860 | } | |
1861 | } | |
1862 | else // iterate over all items in non report view | |
1863 | { | |
1864 | for ( size_t line = lineFrom; line <= lineTo; line++ ) | |
1865 | { | |
1866 | if ( HighlightLine(line, highlight) ) | |
1867 | RefreshLine(line); | |
1868 | } | |
1869 | } | |
1870 | } | |
1871 | ||
1872 | bool wxListMainWindow::HighlightLine( size_t line, bool highlight ) | |
1873 | { | |
1874 | bool changed; | |
1875 | ||
1876 | if ( IsVirtual() ) | |
1877 | { | |
1878 | changed = m_selStore.SelectItem(line, highlight); | |
1879 | } | |
1880 | else // !virtual | |
1881 | { | |
1882 | wxListLineData *ld = GetLine(line); | |
1883 | wxCHECK_MSG( ld, false, _T("invalid index in HighlightLine") ); | |
1884 | ||
1885 | changed = ld->Highlight(highlight); | |
1886 | } | |
1887 | ||
1888 | if ( changed ) | |
1889 | { | |
1890 | SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED | |
1891 | : wxEVT_COMMAND_LIST_ITEM_DESELECTED ); | |
1892 | } | |
1893 | ||
1894 | return changed; | |
1895 | } | |
1896 | ||
1897 | void wxListMainWindow::RefreshLine( size_t line ) | |
1898 | { | |
1899 | if ( InReportView() ) | |
1900 | { | |
1901 | size_t visibleFrom, visibleTo; | |
1902 | GetVisibleLinesRange(&visibleFrom, &visibleTo); | |
1903 | ||
1904 | if ( line < visibleFrom || line > visibleTo ) | |
1905 | return; | |
1906 | } | |
1907 | ||
1908 | wxRect rect = GetLineRect(line); | |
1909 | ||
1910 | GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
1911 | RefreshRect( rect ); | |
1912 | } | |
1913 | ||
1914 | void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo ) | |
1915 | { | |
1916 | // we suppose that they are ordered by caller | |
1917 | wxASSERT_MSG( lineFrom <= lineTo, _T("indices in disorder") ); | |
1918 | ||
1919 | wxASSERT_MSG( lineTo < GetItemCount(), _T("invalid line range") ); | |
1920 | ||
1921 | if ( InReportView() ) | |
1922 | { | |
1923 | size_t visibleFrom, visibleTo; | |
1924 | GetVisibleLinesRange(&visibleFrom, &visibleTo); | |
1925 | ||
1926 | if ( lineFrom < visibleFrom ) | |
1927 | lineFrom = visibleFrom; | |
1928 | if ( lineTo > visibleTo ) | |
1929 | lineTo = visibleTo; | |
1930 | ||
1931 | wxRect rect; | |
1932 | rect.x = 0; | |
1933 | rect.y = GetLineY(lineFrom); | |
1934 | rect.width = GetClientSize().x; | |
1935 | rect.height = GetLineY(lineTo) - rect.y + GetLineHeight(); | |
1936 | ||
1937 | GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
1938 | RefreshRect( rect ); | |
1939 | } | |
1940 | else // !report | |
1941 | { | |
1942 | // TODO: this should be optimized... | |
1943 | for ( size_t line = lineFrom; line <= lineTo; line++ ) | |
1944 | { | |
1945 | RefreshLine(line); | |
1946 | } | |
1947 | } | |
1948 | } | |
1949 | ||
1950 | void wxListMainWindow::RefreshAfter( size_t lineFrom ) | |
1951 | { | |
1952 | if ( InReportView() ) | |
1953 | { | |
1954 | size_t visibleFrom, visibleTo; | |
1955 | GetVisibleLinesRange(&visibleFrom, &visibleTo); | |
1956 | ||
1957 | if ( lineFrom < visibleFrom ) | |
1958 | lineFrom = visibleFrom; | |
1959 | else if ( lineFrom > visibleTo ) | |
1960 | return; | |
1961 | ||
1962 | wxRect rect; | |
1963 | rect.x = 0; | |
1964 | rect.y = GetLineY(lineFrom); | |
1965 | GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
1966 | ||
1967 | wxSize size = GetClientSize(); | |
1968 | rect.width = size.x; | |
1969 | ||
1970 | // refresh till the bottom of the window | |
1971 | rect.height = size.y - rect.y; | |
1972 | ||
1973 | RefreshRect( rect ); | |
1974 | } | |
1975 | else // !report | |
1976 | { | |
1977 | // TODO: how to do it more efficiently? | |
1978 | m_dirty = true; | |
1979 | } | |
1980 | } | |
1981 | ||
1982 | void wxListMainWindow::RefreshSelected() | |
1983 | { | |
1984 | if ( IsEmpty() ) | |
1985 | return; | |
1986 | ||
1987 | size_t from, to; | |
1988 | if ( InReportView() ) | |
1989 | { | |
1990 | GetVisibleLinesRange(&from, &to); | |
1991 | } | |
1992 | else // !virtual | |
1993 | { | |
1994 | from = 0; | |
1995 | to = GetItemCount() - 1; | |
1996 | } | |
1997 | ||
1998 | if ( HasCurrent() && m_current >= from && m_current <= to ) | |
1999 | RefreshLine(m_current); | |
2000 | ||
2001 | for ( size_t line = from; line <= to; line++ ) | |
2002 | { | |
2003 | // NB: the test works as expected even if m_current == -1 | |
2004 | if ( line != m_current && IsHighlighted(line) ) | |
2005 | RefreshLine(line); | |
2006 | } | |
2007 | } | |
2008 | ||
2009 | void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
2010 | { | |
2011 | // Note: a wxPaintDC must be constructed even if no drawing is | |
2012 | // done (a Windows requirement). | |
2013 | wxPaintDC dc( this ); | |
2014 | ||
2015 | if ( IsEmpty() ) | |
2016 | { | |
2017 | // nothing to draw or not the moment to draw it | |
2018 | return; | |
2019 | } | |
2020 | ||
2021 | if ( m_dirty ) | |
2022 | RecalculatePositions( false ); | |
2023 | ||
2024 | GetListCtrl()->PrepareDC( dc ); | |
2025 | ||
2026 | int dev_x, dev_y; | |
2027 | GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x, &dev_y ); | |
2028 | ||
2029 | dc.SetFont( GetFont() ); | |
2030 | ||
2031 | if ( InReportView() ) | |
2032 | { | |
2033 | int lineHeight = GetLineHeight(); | |
2034 | ||
2035 | size_t visibleFrom, visibleTo; | |
2036 | GetVisibleLinesRange(&visibleFrom, &visibleTo); | |
2037 | ||
2038 | wxRect rectLine; | |
2039 | int xOrig = dc.LogicalToDeviceX( 0 ); | |
2040 | int yOrig = dc.LogicalToDeviceY( 0 ); | |
2041 | ||
2042 | // tell the caller cache to cache the data | |
2043 | if ( IsVirtual() ) | |
2044 | { | |
2045 | wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT, | |
2046 | GetParent()->GetId()); | |
2047 | evCache.SetEventObject( GetParent() ); | |
2048 | evCache.m_oldItemIndex = visibleFrom; | |
2049 | evCache.m_itemIndex = visibleTo; | |
2050 | GetParent()->GetEventHandler()->ProcessEvent( evCache ); | |
2051 | } | |
2052 | ||
2053 | for ( size_t line = visibleFrom; line <= visibleTo; line++ ) | |
2054 | { | |
2055 | rectLine = GetLineRect(line); | |
2056 | ||
2057 | ||
2058 | if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig, | |
2059 | rectLine.width, rectLine.height) ) | |
2060 | { | |
2061 | // don't redraw unaffected lines to avoid flicker | |
2062 | continue; | |
2063 | } | |
2064 | ||
2065 | GetLine(line)->DrawInReportMode( &dc, | |
2066 | rectLine, | |
2067 | GetLineHighlightRect(line), | |
2068 | IsHighlighted(line), | |
2069 | line == m_current ); | |
2070 | } | |
2071 | ||
2072 | if ( HasFlag(wxLC_HRULES) ) | |
2073 | { | |
2074 | wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID); | |
2075 | wxSize clientSize = GetClientSize(); | |
2076 | ||
2077 | size_t i = visibleFrom; | |
2078 | if (i == 0) i = 1; // Don't draw the first one | |
2079 | for ( ; i <= visibleTo; i++ ) | |
2080 | { | |
2081 | dc.SetPen(pen); | |
2082 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); | |
2083 | dc.DrawLine(0 - dev_x, i * lineHeight, | |
2084 | clientSize.x - dev_x, i * lineHeight); | |
2085 | } | |
2086 | ||
2087 | // Draw last horizontal rule | |
2088 | if ( visibleTo == GetItemCount() - 1 ) | |
2089 | { | |
2090 | dc.SetPen( pen ); | |
2091 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); | |
2092 | dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight, | |
2093 | clientSize.x - dev_x , (m_lineTo + 1) * lineHeight ); | |
2094 | } | |
2095 | } | |
2096 | ||
2097 | // Draw vertical rules if required | |
2098 | if ( HasFlag(wxLC_VRULES) && !IsEmpty() ) | |
2099 | { | |
2100 | wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID); | |
2101 | wxRect firstItemRect, lastItemRect; | |
2102 | ||
2103 | GetItemRect(visibleFrom, firstItemRect); | |
2104 | GetItemRect(visibleTo, lastItemRect); | |
2105 | int x = firstItemRect.GetX(); | |
2106 | dc.SetPen(pen); | |
2107 | dc.SetBrush(* wxTRANSPARENT_BRUSH); | |
2108 | ||
2109 | for (int col = 0; col < GetColumnCount(); col++) | |
2110 | { | |
2111 | int colWidth = GetColumnWidth(col); | |
2112 | x += colWidth; | |
2113 | int x_pos = x - dev_x; | |
2114 | if (col < GetColumnCount()-1) x_pos -= 2; | |
2115 | dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y, | |
2116 | x_pos, lastItemRect.GetBottom() + 1 - dev_y); | |
2117 | } | |
2118 | } | |
2119 | } | |
2120 | else // !report | |
2121 | { | |
2122 | size_t count = GetItemCount(); | |
2123 | for ( size_t i = 0; i < count; i++ ) | |
2124 | { | |
2125 | GetLine(i)->Draw( &dc ); | |
2126 | } | |
2127 | } | |
2128 | ||
2129 | #if !defined( __WXMAC__) && !defined(__WXGTK20__) | |
2130 | // Don't draw rect outline under Mac at all. | |
2131 | // Draw it elsewhere under GTK. | |
2132 | if ( HasCurrent() ) | |
2133 | { | |
2134 | if ( m_hasFocus ) | |
2135 | { | |
2136 | wxRect rect( GetLineHighlightRect( m_current ) ); | |
2137 | dc.SetPen( *wxBLACK_PEN ); | |
2138 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); | |
2139 | dc.DrawRectangle( rect ); | |
2140 | } | |
2141 | } | |
2142 | #endif | |
2143 | } | |
2144 | ||
2145 | void wxListMainWindow::HighlightAll( bool on ) | |
2146 | { | |
2147 | if ( IsSingleSel() ) | |
2148 | { | |
2149 | wxASSERT_MSG( !on, _T("can't do this in a single selection control") ); | |
2150 | ||
2151 | // we just have one item to turn off | |
2152 | if ( HasCurrent() && IsHighlighted(m_current) ) | |
2153 | { | |
2154 | HighlightLine(m_current, false); | |
2155 | RefreshLine(m_current); | |
2156 | } | |
2157 | } | |
2158 | else // multi selection | |
2159 | { | |
2160 | if ( !IsEmpty() ) | |
2161 | HighlightLines(0, GetItemCount() - 1, on); | |
2162 | } | |
2163 | } | |
2164 | ||
2165 | void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event)) | |
2166 | { | |
2167 | // Do nothing here. This prevents the default handler in wxScrolledWindow | |
2168 | // from needlessly scrolling the window when the edit control is | |
2169 | // dismissed. See ticket #9563. | |
2170 | } | |
2171 | ||
2172 | void wxListMainWindow::SendNotify( size_t line, | |
2173 | wxEventType command, | |
2174 | const wxPoint& point ) | |
2175 | { | |
2176 | wxListEvent le( command, GetParent()->GetId() ); | |
2177 | le.SetEventObject( GetParent() ); | |
2178 | ||
2179 | le.m_itemIndex = line; | |
2180 | ||
2181 | // set only for events which have position | |
2182 | if ( point != wxDefaultPosition ) | |
2183 | le.m_pointDrag = point; | |
2184 | ||
2185 | // don't try to get the line info for virtual list controls: the main | |
2186 | // program has it anyhow and if we did it would result in accessing all | |
2187 | // the lines, even those which are not visible now and this is precisely | |
2188 | // what we're trying to avoid | |
2189 | if ( !IsVirtual() ) | |
2190 | { | |
2191 | if ( line != (size_t)-1 ) | |
2192 | { | |
2193 | GetLine(line)->GetItem( 0, le.m_item ); | |
2194 | } | |
2195 | //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event | |
2196 | } | |
2197 | //else: there may be no more such item | |
2198 | ||
2199 | GetParent()->GetEventHandler()->ProcessEvent( le ); | |
2200 | } | |
2201 | ||
2202 | void wxListMainWindow::ChangeCurrent(size_t current) | |
2203 | { | |
2204 | m_current = current; | |
2205 | ||
2206 | // as the current item changed, we shouldn't start editing it when the | |
2207 | // "slow click" timer expires as the click happened on another item | |
2208 | if ( m_renameTimer->IsRunning() ) | |
2209 | m_renameTimer->Stop(); | |
2210 | ||
2211 | SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED); | |
2212 | } | |
2213 | ||
2214 | wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass) | |
2215 | { | |
2216 | wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL, | |
2217 | wxT("wrong index in wxGenericListCtrl::EditLabel()") ); | |
2218 | ||
2219 | wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)), | |
2220 | wxT("EditLabel() needs a text control") ); | |
2221 | ||
2222 | size_t itemEdit = (size_t)item; | |
2223 | ||
2224 | wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() ); | |
2225 | le.SetEventObject( GetParent() ); | |
2226 | le.m_itemIndex = item; | |
2227 | wxListLineData *data = GetLine(itemEdit); | |
2228 | wxCHECK_MSG( data, NULL, _T("invalid index in EditLabel()") ); | |
2229 | data->GetItem( 0, le.m_item ); | |
2230 | ||
2231 | if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() ) | |
2232 | { | |
2233 | // vetoed by user code | |
2234 | return NULL; | |
2235 | } | |
2236 | ||
2237 | // We have to call this here because the label in question might just have | |
2238 | // been added and no screen update taken place. | |
2239 | if ( m_dirty ) | |
2240 | { | |
2241 | wxSafeYield(); | |
2242 | ||
2243 | // Pending events dispatched by wxSafeYield might have changed the item | |
2244 | // count | |
2245 | if ( (size_t)item >= GetItemCount() ) | |
2246 | return NULL; | |
2247 | } | |
2248 | ||
2249 | wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject(); | |
2250 | m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item); | |
2251 | return m_textctrlWrapper->GetText(); | |
2252 | } | |
2253 | ||
2254 | void wxListMainWindow::OnRenameTimer() | |
2255 | { | |
2256 | wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") ); | |
2257 | ||
2258 | EditLabel( m_current ); | |
2259 | } | |
2260 | ||
2261 | bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value) | |
2262 | { | |
2263 | wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() ); | |
2264 | le.SetEventObject( GetParent() ); | |
2265 | le.m_itemIndex = itemEdit; | |
2266 | ||
2267 | wxListLineData *data = GetLine(itemEdit); | |
2268 | ||
2269 | wxCHECK_MSG( data, false, _T("invalid index in OnRenameAccept()") ); | |
2270 | ||
2271 | data->GetItem( 0, le.m_item ); | |
2272 | le.m_item.m_text = value; | |
2273 | return !GetParent()->GetEventHandler()->ProcessEvent( le ) || | |
2274 | le.IsAllowed(); | |
2275 | } | |
2276 | ||
2277 | void wxListMainWindow::OnRenameCancelled(size_t itemEdit) | |
2278 | { | |
2279 | // let owner know that the edit was cancelled | |
2280 | wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() ); | |
2281 | ||
2282 | le.SetEditCanceled(true); | |
2283 | ||
2284 | le.SetEventObject( GetParent() ); | |
2285 | le.m_itemIndex = itemEdit; | |
2286 | ||
2287 | wxListLineData *data = GetLine(itemEdit); | |
2288 | wxCHECK_RET( data, _T("invalid index in OnRenameCancelled()") ); | |
2289 | ||
2290 | data->GetItem( 0, le.m_item ); | |
2291 | GetEventHandler()->ProcessEvent( le ); | |
2292 | } | |
2293 | ||
2294 | void wxListMainWindow::OnMouse( wxMouseEvent &event ) | |
2295 | { | |
2296 | ||
2297 | #ifdef __WXMAC__ | |
2298 | // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly | |
2299 | // shutdown the edit control when the mouse is clicked elsewhere on the | |
2300 | // listctrl because the order of events is different (or something like | |
2301 | // that), so explicitly end the edit if it is active. | |
2302 | if ( event.LeftDown() && m_textctrlWrapper ) | |
2303 | m_textctrlWrapper->EndEdit( false ); | |
2304 | #endif // __WXMAC__ | |
2305 | ||
2306 | if ( event.LeftDown() ) | |
2307 | SetFocus(); | |
2308 | ||
2309 | event.SetEventObject( GetParent() ); | |
2310 | if ( GetParent()->GetEventHandler()->ProcessEvent( event) ) | |
2311 | return; | |
2312 | ||
2313 | if (event.GetEventType() == wxEVT_MOUSEWHEEL) | |
2314 | { | |
2315 | // let the base handle mouse wheel events. | |
2316 | event.Skip(); | |
2317 | return; | |
2318 | } | |
2319 | ||
2320 | if ( !HasCurrent() || IsEmpty() ) | |
2321 | { | |
2322 | if (event.RightDown()) | |
2323 | { | |
2324 | SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() ); | |
2325 | ||
2326 | wxContextMenuEvent evtCtx( | |
2327 | wxEVT_CONTEXT_MENU, | |
2328 | GetParent()->GetId(), | |
2329 | ClientToScreen(event.GetPosition())); | |
2330 | evtCtx.SetEventObject(GetParent()); | |
2331 | GetParent()->GetEventHandler()->ProcessEvent(evtCtx); | |
2332 | } | |
2333 | return; | |
2334 | } | |
2335 | ||
2336 | if (m_dirty) | |
2337 | return; | |
2338 | ||
2339 | if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() || | |
2340 | event.ButtonDClick()) ) | |
2341 | return; | |
2342 | ||
2343 | int x = event.GetX(); | |
2344 | int y = event.GetY(); | |
2345 | GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y ); | |
2346 | ||
2347 | // where did we hit it (if we did)? | |
2348 | long hitResult = 0; | |
2349 | ||
2350 | size_t count = GetItemCount(), | |
2351 | current; | |
2352 | ||
2353 | if ( InReportView() ) | |
2354 | { | |
2355 | current = y / GetLineHeight(); | |
2356 | if ( current < count ) | |
2357 | hitResult = HitTestLine(current, x, y); | |
2358 | } | |
2359 | else // !report | |
2360 | { | |
2361 | // TODO: optimize it too! this is less simple than for report view but | |
2362 | // enumerating all items is still not a way to do it!! | |
2363 | for ( current = 0; current < count; current++ ) | |
2364 | { | |
2365 | hitResult = HitTestLine(current, x, y); | |
2366 | if ( hitResult ) | |
2367 | break; | |
2368 | } | |
2369 | } | |
2370 | ||
2371 | if (event.Dragging()) | |
2372 | { | |
2373 | if (m_dragCount == 0) | |
2374 | { | |
2375 | // we have to report the raw, physical coords as we want to be | |
2376 | // able to call HitTest(event.m_pointDrag) from the user code to | |
2377 | // get the item being dragged | |
2378 | m_dragStart = event.GetPosition(); | |
2379 | } | |
2380 | ||
2381 | m_dragCount++; | |
2382 | ||
2383 | if (m_dragCount != 3) | |
2384 | return; | |
2385 | ||
2386 | int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG | |
2387 | : wxEVT_COMMAND_LIST_BEGIN_DRAG; | |
2388 | ||
2389 | wxListEvent le( command, GetParent()->GetId() ); | |
2390 | le.SetEventObject( GetParent() ); | |
2391 | le.m_itemIndex = m_lineLastClicked; | |
2392 | le.m_pointDrag = m_dragStart; | |
2393 | GetParent()->GetEventHandler()->ProcessEvent( le ); | |
2394 | ||
2395 | return; | |
2396 | } | |
2397 | else | |
2398 | { | |
2399 | m_dragCount = 0; | |
2400 | } | |
2401 | ||
2402 | if ( !hitResult ) | |
2403 | { | |
2404 | // outside of any item | |
2405 | if (event.RightDown()) | |
2406 | { | |
2407 | SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() ); | |
2408 | ||
2409 | wxContextMenuEvent evtCtx( | |
2410 | wxEVT_CONTEXT_MENU, | |
2411 | GetParent()->GetId(), | |
2412 | ClientToScreen(event.GetPosition())); | |
2413 | evtCtx.SetEventObject(GetParent()); | |
2414 | GetParent()->GetEventHandler()->ProcessEvent(evtCtx); | |
2415 | } | |
2416 | else | |
2417 | { | |
2418 | // reset the selection and bail out | |
2419 | HighlightAll(false); | |
2420 | } | |
2421 | ||
2422 | return; | |
2423 | } | |
2424 | ||
2425 | bool forceClick = false; | |
2426 | if (event.ButtonDClick()) | |
2427 | { | |
2428 | if ( m_renameTimer->IsRunning() ) | |
2429 | m_renameTimer->Stop(); | |
2430 | ||
2431 | m_lastOnSame = false; | |
2432 | ||
2433 | if ( current == m_lineLastClicked ) | |
2434 | { | |
2435 | SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED ); | |
2436 | ||
2437 | return; | |
2438 | } | |
2439 | else | |
2440 | { | |
2441 | // The first click was on another item, so don't interpret this as | |
2442 | // a double click, but as a simple click instead | |
2443 | forceClick = true; | |
2444 | } | |
2445 | } | |
2446 | ||
2447 | if (event.LeftUp()) | |
2448 | { | |
2449 | if (m_lineSelectSingleOnUp != (size_t)-1) | |
2450 | { | |
2451 | // select single line | |
2452 | HighlightAll( false ); | |
2453 | ReverseHighlight(m_lineSelectSingleOnUp); | |
2454 | } | |
2455 | ||
2456 | if (m_lastOnSame) | |
2457 | { | |
2458 | if ((current == m_current) && | |
2459 | (hitResult == wxLIST_HITTEST_ONITEMLABEL) && | |
2460 | HasFlag(wxLC_EDIT_LABELS) ) | |
2461 | { | |
2462 | if ( !InReportView() || | |
2463 | GetLineLabelRect(current).Contains(x, y) ) | |
2464 | { | |
2465 | int dclick = wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC); | |
2466 | m_renameTimer->Start(dclick > 0 ? dclick : 250, true); | |
2467 | } | |
2468 | } | |
2469 | } | |
2470 | ||
2471 | m_lastOnSame = false; | |
2472 | m_lineSelectSingleOnUp = (size_t)-1; | |
2473 | } | |
2474 | else | |
2475 | { | |
2476 | // This is necessary, because after a DnD operation in | |
2477 | // from and to ourself, the up event is swallowed by the | |
2478 | // DnD code. So on next non-up event (which means here and | |
2479 | // now) m_lineSelectSingleOnUp should be reset. | |
2480 | m_lineSelectSingleOnUp = (size_t)-1; | |
2481 | } | |
2482 | if (event.RightDown()) | |
2483 | { | |
2484 | m_lineBeforeLastClicked = m_lineLastClicked; | |
2485 | m_lineLastClicked = current; | |
2486 | ||
2487 | // If the item is already selected, do not update the selection. | |
2488 | // Multi-selections should not be cleared if a selected item is clicked. | |
2489 | if (!IsHighlighted(current)) | |
2490 | { | |
2491 | HighlightAll(false); | |
2492 | ChangeCurrent(current); | |
2493 | ReverseHighlight(m_current); | |
2494 | } | |
2495 | ||
2496 | SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() ); | |
2497 | ||
2498 | // Allow generation of context menu event | |
2499 | event.Skip(); | |
2500 | } | |
2501 | else if (event.MiddleDown()) | |
2502 | { | |
2503 | SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK ); | |
2504 | } | |
2505 | else if ( event.LeftDown() || forceClick ) | |
2506 | { | |
2507 | m_lineBeforeLastClicked = m_lineLastClicked; | |
2508 | m_lineLastClicked = current; | |
2509 | ||
2510 | size_t oldCurrent = m_current; | |
2511 | bool oldWasSelected = IsHighlighted(m_current); | |
2512 | ||
2513 | bool cmdModifierDown = event.CmdDown(); | |
2514 | if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) ) | |
2515 | { | |
2516 | if ( IsSingleSel() || !IsHighlighted(current) ) | |
2517 | { | |
2518 | HighlightAll( false ); | |
2519 | ||
2520 | ChangeCurrent(current); | |
2521 | ||
2522 | ReverseHighlight(m_current); | |
2523 | } | |
2524 | else // multi sel & current is highlighted & no mod keys | |
2525 | { | |
2526 | m_lineSelectSingleOnUp = current; | |
2527 | ChangeCurrent(current); // change focus | |
2528 | } | |
2529 | } | |
2530 | else // multi sel & either ctrl or shift is down | |
2531 | { | |
2532 | if (cmdModifierDown) | |
2533 | { | |
2534 | ChangeCurrent(current); | |
2535 | ||
2536 | ReverseHighlight(m_current); | |
2537 | } | |
2538 | else if (event.ShiftDown()) | |
2539 | { | |
2540 | ChangeCurrent(current); | |
2541 | ||
2542 | size_t lineFrom = oldCurrent, | |
2543 | lineTo = current; | |
2544 | ||
2545 | if ( lineTo < lineFrom ) | |
2546 | { | |
2547 | lineTo = lineFrom; | |
2548 | lineFrom = m_current; | |
2549 | } | |
2550 | ||
2551 | HighlightLines(lineFrom, lineTo); | |
2552 | } | |
2553 | else // !ctrl, !shift | |
2554 | { | |
2555 | // test in the enclosing if should make it impossible | |
2556 | wxFAIL_MSG( _T("how did we get here?") ); | |
2557 | } | |
2558 | } | |
2559 | ||
2560 | if (m_current != oldCurrent) | |
2561 | RefreshLine( oldCurrent ); | |
2562 | ||
2563 | // forceClick is only set if the previous click was on another item | |
2564 | m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected; | |
2565 | } | |
2566 | } | |
2567 | ||
2568 | void wxListMainWindow::MoveToItem(size_t item) | |
2569 | { | |
2570 | if ( item == (size_t)-1 ) | |
2571 | return; | |
2572 | ||
2573 | wxRect rect = GetLineRect(item); | |
2574 | ||
2575 | int client_w, client_h; | |
2576 | GetClientSize( &client_w, &client_h ); | |
2577 | ||
2578 | const int hLine = GetLineHeight(); | |
2579 | ||
2580 | int view_x = SCROLL_UNIT_X * GetListCtrl()->GetScrollPos( wxHORIZONTAL ); | |
2581 | int view_y = hLine * GetListCtrl()->GetScrollPos( wxVERTICAL ); | |
2582 | ||
2583 | if ( InReportView() ) | |
2584 | { | |
2585 | // the next we need the range of lines shown it might be different, | |
2586 | // so recalculate it | |
2587 | ResetVisibleLinesRange(); | |
2588 | ||
2589 | if (rect.y < view_y) | |
2590 | GetListCtrl()->Scroll( -1, rect.y / hLine ); | |
2591 | if (rect.y + rect.height + 5 > view_y + client_h) | |
2592 | GetListCtrl()->Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine ); | |
2593 | ||
2594 | #ifdef __WXMAC__ | |
2595 | // At least on Mac the visible lines value will get reset inside of | |
2596 | // Scroll *before* it actually scrolls the window because of the | |
2597 | // Update() that happens there, so it will still have the wrong value. | |
2598 | // So let's reset it again and wait for it to be recalculated in the | |
2599 | // next paint event. I would expect this problem to show up in wxGTK | |
2600 | // too but couldn't duplicate it there. Perhaps the order of events | |
2601 | // is different... --Robin | |
2602 | ResetVisibleLinesRange(); | |
2603 | #endif | |
2604 | } | |
2605 | else // !report | |
2606 | { | |
2607 | int sx = -1, | |
2608 | sy = -1; | |
2609 | ||
2610 | if (rect.x-view_x < 5) | |
2611 | sx = (rect.x - 5) / SCROLL_UNIT_X; | |
2612 | if (rect.x + rect.width - 5 > view_x + client_w) | |
2613 | sx = (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X; | |
2614 | ||
2615 | if (rect.y-view_y < 5) | |
2616 | sy = (rect.y - 5) / hLine; | |
2617 | if (rect.y + rect.height - 5 > view_y + client_h) | |
2618 | sy = (rect.y + rect.height - client_h + hLine) / hLine; | |
2619 | ||
2620 | GetListCtrl()->Scroll(sx, sy); | |
2621 | } | |
2622 | } | |
2623 | ||
2624 | bool wxListMainWindow::ScrollList(int WXUNUSED(dx), int dy) | |
2625 | { | |
2626 | if ( !InReportView() ) | |
2627 | { | |
2628 | // TODO: this should work in all views but is not implemented now | |
2629 | return false; | |
2630 | } | |
2631 | ||
2632 | size_t top, bottom; | |
2633 | GetVisibleLinesRange(&top, &bottom); | |
2634 | ||
2635 | if ( bottom == (size_t)-1 ) | |
2636 | return 0; | |
2637 | ||
2638 | ResetVisibleLinesRange(); | |
2639 | ||
2640 | int hLine = GetLineHeight(); | |
2641 | ||
2642 | GetListCtrl()->Scroll(-1, top + dy / hLine); | |
2643 | ||
2644 | #ifdef __WXMAC__ | |
2645 | // see comment in MoveToItem() for why we do this | |
2646 | ResetVisibleLinesRange(); | |
2647 | #endif | |
2648 | ||
2649 | return true; | |
2650 | } | |
2651 | ||
2652 | // ---------------------------------------------------------------------------- | |
2653 | // keyboard handling | |
2654 | // ---------------------------------------------------------------------------- | |
2655 | ||
2656 | void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event) | |
2657 | { | |
2658 | wxCHECK_RET( newCurrent < (size_t)GetItemCount(), | |
2659 | _T("invalid item index in OnArrowChar()") ); | |
2660 | ||
2661 | size_t oldCurrent = m_current; | |
2662 | ||
2663 | // in single selection we just ignore Shift as we can't select several | |
2664 | // items anyhow | |
2665 | if ( event.ShiftDown() && !IsSingleSel() ) | |
2666 | { | |
2667 | ChangeCurrent(newCurrent); | |
2668 | ||
2669 | // refresh the old focus to remove it | |
2670 | RefreshLine( oldCurrent ); | |
2671 | ||
2672 | // select all the items between the old and the new one | |
2673 | if ( oldCurrent > newCurrent ) | |
2674 | { | |
2675 | newCurrent = oldCurrent; | |
2676 | oldCurrent = m_current; | |
2677 | } | |
2678 | ||
2679 | HighlightLines(oldCurrent, newCurrent); | |
2680 | } | |
2681 | else // !shift | |
2682 | { | |
2683 | // all previously selected items are unselected unless ctrl is held | |
2684 | // in a multiselection control | |
2685 | if ( !event.ControlDown() || IsSingleSel() ) | |
2686 | HighlightAll(false); | |
2687 | ||
2688 | ChangeCurrent(newCurrent); | |
2689 | ||
2690 | // refresh the old focus to remove it | |
2691 | RefreshLine( oldCurrent ); | |
2692 | ||
2693 | // in single selection mode we must always have a selected item | |
2694 | if ( !event.ControlDown() || IsSingleSel() ) | |
2695 | HighlightLine( m_current, true ); | |
2696 | } | |
2697 | ||
2698 | RefreshLine( m_current ); | |
2699 | ||
2700 | MoveToFocus(); | |
2701 | } | |
2702 | ||
2703 | void wxListMainWindow::OnKeyDown( wxKeyEvent &event ) | |
2704 | { | |
2705 | wxWindow *parent = GetParent(); | |
2706 | ||
2707 | // propagate the key event upwards | |
2708 | wxKeyEvent ke(event); | |
2709 | ke.SetEventObject( parent ); | |
2710 | if (parent->GetEventHandler()->ProcessEvent( ke )) | |
2711 | return; | |
2712 | ||
2713 | event.Skip(); | |
2714 | } | |
2715 | ||
2716 | void wxListMainWindow::OnKeyUp( wxKeyEvent &event ) | |
2717 | { | |
2718 | wxWindow *parent = GetParent(); | |
2719 | ||
2720 | // propagate the key event upwards | |
2721 | wxKeyEvent ke(event); | |
2722 | if (parent->GetEventHandler()->ProcessEvent( ke )) | |
2723 | return; | |
2724 | ||
2725 | event.Skip(); | |
2726 | } | |
2727 | ||
2728 | void wxListMainWindow::OnChar( wxKeyEvent &event ) | |
2729 | { | |
2730 | wxWindow *parent = GetParent(); | |
2731 | ||
2732 | // send a list_key event up | |
2733 | if ( HasCurrent() ) | |
2734 | { | |
2735 | wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() ); | |
2736 | le.m_itemIndex = m_current; | |
2737 | GetLine(m_current)->GetItem( 0, le.m_item ); | |
2738 | le.m_code = event.GetKeyCode(); | |
2739 | le.SetEventObject( parent ); | |
2740 | parent->GetEventHandler()->ProcessEvent( le ); | |
2741 | } | |
2742 | ||
2743 | if ( (event.GetKeyCode() != WXK_UP) && | |
2744 | (event.GetKeyCode() != WXK_DOWN) && | |
2745 | (event.GetKeyCode() != WXK_RIGHT) && | |
2746 | (event.GetKeyCode() != WXK_LEFT) && | |
2747 | (event.GetKeyCode() != WXK_PAGEUP) && | |
2748 | (event.GetKeyCode() != WXK_PAGEDOWN) && | |
2749 | (event.GetKeyCode() != WXK_END) && | |
2750 | (event.GetKeyCode() != WXK_HOME) ) | |
2751 | { | |
2752 | // propagate the char event upwards | |
2753 | wxKeyEvent ke(event); | |
2754 | ke.SetEventObject( parent ); | |
2755 | if (parent->GetEventHandler()->ProcessEvent( ke )) | |
2756 | return; | |
2757 | } | |
2758 | ||
2759 | if ( HandleAsNavigationKey(event) ) | |
2760 | return; | |
2761 | ||
2762 | // no item -> nothing to do | |
2763 | if (!HasCurrent()) | |
2764 | { | |
2765 | event.Skip(); | |
2766 | return; | |
2767 | } | |
2768 | ||
2769 | // don't use m_linesPerPage directly as it might not be computed yet | |
2770 | const int pageSize = GetCountPerPage(); | |
2771 | wxCHECK_RET( pageSize, _T("should have non zero page size") ); | |
2772 | ||
2773 | if (GetLayoutDirection() == wxLayout_RightToLeft) | |
2774 | { | |
2775 | if (event.GetKeyCode() == WXK_RIGHT) | |
2776 | event.m_keyCode = WXK_LEFT; | |
2777 | else if (event.GetKeyCode() == WXK_LEFT) | |
2778 | event.m_keyCode = WXK_RIGHT; | |
2779 | } | |
2780 | ||
2781 | switch ( event.GetKeyCode() ) | |
2782 | { | |
2783 | case WXK_UP: | |
2784 | if ( m_current > 0 ) | |
2785 | OnArrowChar( m_current - 1, event ); | |
2786 | break; | |
2787 | ||
2788 | case WXK_DOWN: | |
2789 | if ( m_current < (size_t)GetItemCount() - 1 ) | |
2790 | OnArrowChar( m_current + 1, event ); | |
2791 | break; | |
2792 | ||
2793 | case WXK_END: | |
2794 | if (!IsEmpty()) | |
2795 | OnArrowChar( GetItemCount() - 1, event ); | |
2796 | break; | |
2797 | ||
2798 | case WXK_HOME: | |
2799 | if (!IsEmpty()) | |
2800 | OnArrowChar( 0, event ); | |
2801 | break; | |
2802 | ||
2803 | case WXK_PAGEUP: | |
2804 | { | |
2805 | int steps = InReportView() ? pageSize - 1 | |
2806 | : m_current % pageSize; | |
2807 | ||
2808 | int index = m_current - steps; | |
2809 | if (index < 0) | |
2810 | index = 0; | |
2811 | ||
2812 | OnArrowChar( index, event ); | |
2813 | } | |
2814 | break; | |
2815 | ||
2816 | case WXK_PAGEDOWN: | |
2817 | { | |
2818 | int steps = InReportView() | |
2819 | ? pageSize - 1 | |
2820 | : pageSize - (m_current % pageSize) - 1; | |
2821 | ||
2822 | size_t index = m_current + steps; | |
2823 | size_t count = GetItemCount(); | |
2824 | if ( index >= count ) | |
2825 | index = count - 1; | |
2826 | ||
2827 | OnArrowChar( index, event ); | |
2828 | } | |
2829 | break; | |
2830 | ||
2831 | case WXK_LEFT: | |
2832 | if ( !InReportView() ) | |
2833 | { | |
2834 | int index = m_current - pageSize; | |
2835 | if (index < 0) | |
2836 | index = 0; | |
2837 | ||
2838 | OnArrowChar( index, event ); | |
2839 | } | |
2840 | break; | |
2841 | ||
2842 | case WXK_RIGHT: | |
2843 | if ( !InReportView() ) | |
2844 | { | |
2845 | size_t index = m_current + pageSize; | |
2846 | ||
2847 | size_t count = GetItemCount(); | |
2848 | if ( index >= count ) | |
2849 | index = count - 1; | |
2850 | ||
2851 | OnArrowChar( index, event ); | |
2852 | } | |
2853 | break; | |
2854 | ||
2855 | case WXK_SPACE: | |
2856 | if ( IsSingleSel() ) | |
2857 | { | |
2858 | if ( event.ControlDown() ) | |
2859 | { | |
2860 | ReverseHighlight(m_current); | |
2861 | } | |
2862 | else // normal space press | |
2863 | { | |
2864 | SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED ); | |
2865 | } | |
2866 | } | |
2867 | else // multiple selection | |
2868 | { | |
2869 | ReverseHighlight(m_current); | |
2870 | } | |
2871 | break; | |
2872 | ||
2873 | case WXK_RETURN: | |
2874 | case WXK_EXECUTE: | |
2875 | SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED ); | |
2876 | break; | |
2877 | ||
2878 | default: | |
2879 | event.Skip(); | |
2880 | } | |
2881 | } | |
2882 | ||
2883 | // ---------------------------------------------------------------------------- | |
2884 | // focus handling | |
2885 | // ---------------------------------------------------------------------------- | |
2886 | ||
2887 | void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) ) | |
2888 | { | |
2889 | if ( GetParent() ) | |
2890 | { | |
2891 | wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() ); | |
2892 | event.SetEventObject( GetParent() ); | |
2893 | if ( GetParent()->GetEventHandler()->ProcessEvent( event) ) | |
2894 | return; | |
2895 | } | |
2896 | ||
2897 | // wxGTK sends us EVT_SET_FOCUS events even if we had never got | |
2898 | // EVT_KILL_FOCUS before which means that we finish by redrawing the items | |
2899 | // which are already drawn correctly resulting in horrible flicker - avoid | |
2900 | // it | |
2901 | if ( !m_hasFocus ) | |
2902 | { | |
2903 | m_hasFocus = true; | |
2904 | ||
2905 | RefreshSelected(); | |
2906 | } | |
2907 | } | |
2908 | ||
2909 | void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) ) | |
2910 | { | |
2911 | if ( GetParent() ) | |
2912 | { | |
2913 | wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() ); | |
2914 | event.SetEventObject( GetParent() ); | |
2915 | if ( GetParent()->GetEventHandler()->ProcessEvent( event) ) | |
2916 | return; | |
2917 | } | |
2918 | ||
2919 | m_hasFocus = false; | |
2920 | RefreshSelected(); | |
2921 | } | |
2922 | ||
2923 | void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y ) | |
2924 | { | |
2925 | if ( HasFlag(wxLC_ICON) && (m_normal_image_list)) | |
2926 | { | |
2927 | m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT ); | |
2928 | } | |
2929 | else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list)) | |
2930 | { | |
2931 | m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT ); | |
2932 | } | |
2933 | else if ( HasFlag(wxLC_LIST) && (m_small_image_list)) | |
2934 | { | |
2935 | m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT ); | |
2936 | } | |
2937 | else if ( InReportView() && (m_small_image_list)) | |
2938 | { | |
2939 | m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT ); | |
2940 | } | |
2941 | } | |
2942 | ||
2943 | void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const | |
2944 | { | |
2945 | if ( HasFlag(wxLC_ICON) && m_normal_image_list ) | |
2946 | { | |
2947 | m_normal_image_list->GetSize( index, width, height ); | |
2948 | } | |
2949 | else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list ) | |
2950 | { | |
2951 | m_small_image_list->GetSize( index, width, height ); | |
2952 | } | |
2953 | else if ( HasFlag(wxLC_LIST) && m_small_image_list ) | |
2954 | { | |
2955 | m_small_image_list->GetSize( index, width, height ); | |
2956 | } | |
2957 | else if ( InReportView() && m_small_image_list ) | |
2958 | { | |
2959 | m_small_image_list->GetSize( index, width, height ); | |
2960 | } | |
2961 | else | |
2962 | { | |
2963 | width = | |
2964 | height = 0; | |
2965 | } | |
2966 | } | |
2967 | ||
2968 | int wxListMainWindow::GetTextLength( const wxString &s ) const | |
2969 | { | |
2970 | wxClientDC dc( wxConstCast(this, wxListMainWindow) ); | |
2971 | dc.SetFont( GetFont() ); | |
2972 | ||
2973 | wxCoord lw; | |
2974 | dc.GetTextExtent( s, &lw, NULL ); | |
2975 | ||
2976 | return lw + AUTOSIZE_COL_MARGIN; | |
2977 | } | |
2978 | ||
2979 | void wxListMainWindow::SetImageList( wxImageList *imageList, int which ) | |
2980 | { | |
2981 | m_dirty = true; | |
2982 | ||
2983 | // calc the spacing from the icon size | |
2984 | int width = 0, height = 0; | |
2985 | ||
2986 | if ((imageList) && (imageList->GetImageCount()) ) | |
2987 | imageList->GetSize(0, width, height); | |
2988 | ||
2989 | if (which == wxIMAGE_LIST_NORMAL) | |
2990 | { | |
2991 | m_normal_image_list = imageList; | |
2992 | m_normal_spacing = width + 8; | |
2993 | } | |
2994 | ||
2995 | if (which == wxIMAGE_LIST_SMALL) | |
2996 | { | |
2997 | m_small_image_list = imageList; | |
2998 | m_small_spacing = width + 14; | |
2999 | m_lineHeight = 0; // ensure that the line height will be recalc'd | |
3000 | } | |
3001 | } | |
3002 | ||
3003 | void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall ) | |
3004 | { | |
3005 | m_dirty = true; | |
3006 | if (isSmall) | |
3007 | m_small_spacing = spacing; | |
3008 | else | |
3009 | m_normal_spacing = spacing; | |
3010 | } | |
3011 | ||
3012 | int wxListMainWindow::GetItemSpacing( bool isSmall ) | |
3013 | { | |
3014 | return isSmall ? m_small_spacing : m_normal_spacing; | |
3015 | } | |
3016 | ||
3017 | // ---------------------------------------------------------------------------- | |
3018 | // columns | |
3019 | // ---------------------------------------------------------------------------- | |
3020 | ||
3021 | void wxListMainWindow::SetColumn( int col, wxListItem &item ) | |
3022 | { | |
3023 | wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col ); | |
3024 | ||
3025 | wxCHECK_RET( node, _T("invalid column index in SetColumn") ); | |
3026 | ||
3027 | if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER ) | |
3028 | item.m_width = GetTextLength( item.m_text ); | |
3029 | ||
3030 | wxListHeaderData *column = node->GetData(); | |
3031 | column->SetItem( item ); | |
3032 | ||
3033 | wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin; | |
3034 | if ( headerWin ) | |
3035 | headerWin->m_dirty = true; | |
3036 | ||
3037 | m_dirty = true; | |
3038 | ||
3039 | // invalidate it as it has to be recalculated | |
3040 | m_headerWidth = 0; | |
3041 | } | |
3042 | ||
3043 | void wxListMainWindow::SetColumnWidth( int col, int width ) | |
3044 | { | |
3045 | wxCHECK_RET( col >= 0 && col < GetColumnCount(), | |
3046 | _T("invalid column index") ); | |
3047 | ||
3048 | wxCHECK_RET( InReportView(), | |
3049 | _T("SetColumnWidth() can only be called in report mode.") ); | |
3050 | ||
3051 | m_dirty = true; | |
3052 | ||
3053 | wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin; | |
3054 | if ( headerWin ) | |
3055 | headerWin->m_dirty = true; | |
3056 | ||
3057 | wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col ); | |
3058 | wxCHECK_RET( node, _T("no column?") ); | |
3059 | ||
3060 | wxListHeaderData *column = node->GetData(); | |
3061 | ||
3062 | size_t count = GetItemCount(); | |
3063 | ||
3064 | if (width == wxLIST_AUTOSIZE_USEHEADER) | |
3065 | { | |
3066 | width = GetTextLength(column->GetText()); | |
3067 | width += 2*EXTRA_WIDTH; | |
3068 | ||
3069 | // check for column header's image availability | |
3070 | const int image = column->GetImage(); | |
3071 | if ( image != -1 ) | |
3072 | { | |
3073 | if ( m_small_image_list ) | |
3074 | { | |
3075 | int ix = 0, iy = 0; | |
3076 | m_small_image_list->GetSize(image, ix, iy); | |
3077 | width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE; | |
3078 | } | |
3079 | } | |
3080 | } | |
3081 | else if ( width == wxLIST_AUTOSIZE ) | |
3082 | { | |
3083 | if ( IsVirtual() ) | |
3084 | { | |
3085 | // TODO: determine the max width somehow... | |
3086 | width = WIDTH_COL_DEFAULT; | |
3087 | } | |
3088 | else // !virtual | |
3089 | { | |
3090 | wxClientDC dc(this); | |
3091 | dc.SetFont( GetFont() ); | |
3092 | ||
3093 | int max = AUTOSIZE_COL_MARGIN; | |
3094 | ||
3095 | // if the cached column width isn't valid then recalculate it | |
3096 | if (m_aColWidths.Item(col)->bNeedsUpdate) | |
3097 | { | |
3098 | for (size_t i = 0; i < count; i++) | |
3099 | { | |
3100 | wxListLineData *line = GetLine( i ); | |
3101 | wxListItemDataList::compatibility_iterator n = line->m_items.Item( col ); | |
3102 | ||
3103 | wxCHECK_RET( n, _T("no subitem?") ); | |
3104 | ||
3105 | wxListItemData *itemData = n->GetData(); | |
3106 | wxListItem item; | |
3107 | ||
3108 | itemData->GetItem(item); | |
3109 | int itemWidth = GetItemWidthWithImage(&item); | |
3110 | if (itemWidth > max) | |
3111 | max = itemWidth; | |
3112 | } | |
3113 | ||
3114 | m_aColWidths.Item(col)->bNeedsUpdate = false; | |
3115 | m_aColWidths.Item(col)->nMaxWidth = max; | |
3116 | } | |
3117 | ||
3118 | max = m_aColWidths.Item(col)->nMaxWidth; | |
3119 | width = max + AUTOSIZE_COL_MARGIN; | |
3120 | } | |
3121 | } | |
3122 | ||
3123 | column->SetWidth( width ); | |
3124 | ||
3125 | // invalidate it as it has to be recalculated | |
3126 | m_headerWidth = 0; | |
3127 | } | |
3128 | ||
3129 | int wxListMainWindow::GetHeaderWidth() const | |
3130 | { | |
3131 | if ( !m_headerWidth ) | |
3132 | { | |
3133 | wxListMainWindow *self = wxConstCast(this, wxListMainWindow); | |
3134 | ||
3135 | size_t count = GetColumnCount(); | |
3136 | for ( size_t col = 0; col < count; col++ ) | |
3137 | { | |
3138 | self->m_headerWidth += GetColumnWidth(col); | |
3139 | } | |
3140 | } | |
3141 | ||
3142 | return m_headerWidth; | |
3143 | } | |
3144 | ||
3145 | void wxListMainWindow::GetColumn( int col, wxListItem &item ) const | |
3146 | { | |
3147 | wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col ); | |
3148 | wxCHECK_RET( node, _T("invalid column index in GetColumn") ); | |
3149 | ||
3150 | wxListHeaderData *column = node->GetData(); | |
3151 | column->GetItem( item ); | |
3152 | } | |
3153 | ||
3154 | int wxListMainWindow::GetColumnWidth( int col ) const | |
3155 | { | |
3156 | wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col ); | |
3157 | wxCHECK_MSG( node, 0, _T("invalid column index") ); | |
3158 | ||
3159 | wxListHeaderData *column = node->GetData(); | |
3160 | return column->GetWidth(); | |
3161 | } | |
3162 | ||
3163 | // ---------------------------------------------------------------------------- | |
3164 | // item state | |
3165 | // ---------------------------------------------------------------------------- | |
3166 | ||
3167 | void wxListMainWindow::SetItem( wxListItem &item ) | |
3168 | { | |
3169 | long id = item.m_itemId; | |
3170 | wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(), | |
3171 | _T("invalid item index in SetItem") ); | |
3172 | ||
3173 | if ( !IsVirtual() ) | |
3174 | { | |
3175 | wxListLineData *line = GetLine((size_t)id); | |
3176 | line->SetItem( item.m_col, item ); | |
3177 | ||
3178 | // Set item state if user wants | |
3179 | if ( item.m_mask & wxLIST_MASK_STATE ) | |
3180 | SetItemState( item.m_itemId, item.m_state, item.m_state ); | |
3181 | ||
3182 | if (InReportView()) | |
3183 | { | |
3184 | // update the Max Width Cache if needed | |
3185 | int width = GetItemWidthWithImage(&item); | |
3186 | ||
3187 | if (width > m_aColWidths.Item(item.m_col)->nMaxWidth) | |
3188 | m_aColWidths.Item(item.m_col)->nMaxWidth = width; | |
3189 | } | |
3190 | } | |
3191 | ||
3192 | // update the item on screen | |
3193 | wxRect rectItem; | |
3194 | GetItemRect(id, rectItem); | |
3195 | RefreshRect(rectItem); | |
3196 | } | |
3197 | ||
3198 | void wxListMainWindow::SetItemStateAll(long state, long stateMask) | |
3199 | { | |
3200 | if ( IsEmpty() ) | |
3201 | return; | |
3202 | ||
3203 | // first deal with selection | |
3204 | if ( stateMask & wxLIST_STATE_SELECTED ) | |
3205 | { | |
3206 | // set/clear select state | |
3207 | if ( IsVirtual() ) | |
3208 | { | |
3209 | // optimized version for virtual listctrl. | |
3210 | m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED); | |
3211 | Refresh(); | |
3212 | } | |
3213 | else if ( state & wxLIST_STATE_SELECTED ) | |
3214 | { | |
3215 | const long count = GetItemCount(); | |
3216 | for( long i = 0; i < count; i++ ) | |
3217 | { | |
3218 | SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
3219 | } | |
3220 | ||
3221 | } | |
3222 | else | |
3223 | { | |
3224 | // clear for non virtual (somewhat optimized by using GetNextItem()) | |
3225 | long i = -1; | |
3226 | while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 ) | |
3227 | { | |
3228 | SetItemState( i, 0, wxLIST_STATE_SELECTED ); | |
3229 | } | |
3230 | } | |
3231 | } | |
3232 | ||
3233 | if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) ) | |
3234 | { | |
3235 | // unfocus all: only one item can be focussed, so clearing focus for | |
3236 | // all items is simply clearing focus of the focussed item. | |
3237 | SetItemState(m_current, state, stateMask); | |
3238 | } | |
3239 | //(setting focus to all items makes no sense, so it is not handled here.) | |
3240 | } | |
3241 | ||
3242 | void wxListMainWindow::SetItemState( long litem, long state, long stateMask ) | |
3243 | { | |
3244 | if ( litem == -1 ) | |
3245 | { | |
3246 | SetItemStateAll(state, stateMask); | |
3247 | return; | |
3248 | } | |
3249 | ||
3250 | wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(), | |
3251 | _T("invalid list ctrl item index in SetItem") ); | |
3252 | ||
3253 | size_t oldCurrent = m_current; | |
3254 | size_t item = (size_t)litem; // safe because of the check above | |
3255 | ||
3256 | // do we need to change the focus? | |
3257 | if ( stateMask & wxLIST_STATE_FOCUSED ) | |
3258 | { | |
3259 | if ( state & wxLIST_STATE_FOCUSED ) | |
3260 | { | |
3261 | // don't do anything if this item is already focused | |
3262 | if ( item != m_current ) | |
3263 | { | |
3264 | ChangeCurrent(item); | |
3265 | ||
3266 | if ( oldCurrent != (size_t)-1 ) | |
3267 | { | |
3268 | if ( IsSingleSel() ) | |
3269 | { | |
3270 | HighlightLine(oldCurrent, false); | |
3271 | } | |
3272 | ||
3273 | RefreshLine(oldCurrent); | |
3274 | } | |
3275 | ||
3276 | RefreshLine( m_current ); | |
3277 | } | |
3278 | } | |
3279 | else // unfocus | |
3280 | { | |
3281 | // don't do anything if this item is not focused | |
3282 | if ( item == m_current ) | |
3283 | { | |
3284 | ResetCurrent(); | |
3285 | ||
3286 | if ( IsSingleSel() ) | |
3287 | { | |
3288 | // we must unselect the old current item as well or we | |
3289 | // might end up with more than one selected item in a | |
3290 | // single selection control | |
3291 | HighlightLine(oldCurrent, false); | |
3292 | } | |
3293 | ||
3294 | RefreshLine( oldCurrent ); | |
3295 | } | |
3296 | } | |
3297 | } | |
3298 | ||
3299 | // do we need to change the selection state? | |
3300 | if ( stateMask & wxLIST_STATE_SELECTED ) | |
3301 | { | |
3302 | bool on = (state & wxLIST_STATE_SELECTED) != 0; | |
3303 | ||
3304 | if ( IsSingleSel() ) | |
3305 | { | |
3306 | if ( on ) | |
3307 | { | |
3308 | // selecting the item also makes it the focused one in the | |
3309 | // single sel mode | |
3310 | if ( m_current != item ) | |
3311 | { | |
3312 | ChangeCurrent(item); | |
3313 | ||
3314 | if ( oldCurrent != (size_t)-1 ) | |
3315 | { | |
3316 | HighlightLine( oldCurrent, false ); | |
3317 | RefreshLine( oldCurrent ); | |
3318 | } | |
3319 | } | |
3320 | } | |
3321 | else // off | |
3322 | { | |
3323 | // only the current item may be selected anyhow | |
3324 | if ( item != m_current ) | |
3325 | return; | |
3326 | } | |
3327 | } | |
3328 | ||
3329 | if ( HighlightLine(item, on) ) | |
3330 | { | |
3331 | RefreshLine(item); | |
3332 | } | |
3333 | } | |
3334 | } | |
3335 | ||
3336 | int wxListMainWindow::GetItemState( long item, long stateMask ) const | |
3337 | { | |
3338 | wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0, | |
3339 | _T("invalid list ctrl item index in GetItemState()") ); | |
3340 | ||
3341 | int ret = wxLIST_STATE_DONTCARE; | |
3342 | ||
3343 | if ( stateMask & wxLIST_STATE_FOCUSED ) | |
3344 | { | |
3345 | if ( (size_t)item == m_current ) | |
3346 | ret |= wxLIST_STATE_FOCUSED; | |
3347 | } | |
3348 | ||
3349 | if ( stateMask & wxLIST_STATE_SELECTED ) | |
3350 | { | |
3351 | if ( IsHighlighted(item) ) | |
3352 | ret |= wxLIST_STATE_SELECTED; | |
3353 | } | |
3354 | ||
3355 | return ret; | |
3356 | } | |
3357 | ||
3358 | void wxListMainWindow::GetItem( wxListItem &item ) const | |
3359 | { | |
3360 | wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(), | |
3361 | _T("invalid item index in GetItem") ); | |
3362 | ||
3363 | wxListLineData *line = GetLine((size_t)item.m_itemId); | |
3364 | line->GetItem( item.m_col, item ); | |
3365 | ||
3366 | // Get item state if user wants it | |
3367 | if ( item.m_mask & wxLIST_MASK_STATE ) | |
3368 | item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED | | |
3369 | wxLIST_STATE_FOCUSED ); | |
3370 | } | |
3371 | ||
3372 | // ---------------------------------------------------------------------------- | |
3373 | // item count | |
3374 | // ---------------------------------------------------------------------------- | |
3375 | ||
3376 | size_t wxListMainWindow::GetItemCount() const | |
3377 | { | |
3378 | return IsVirtual() ? m_countVirt : m_lines.GetCount(); | |
3379 | } | |
3380 | ||
3381 | void wxListMainWindow::SetItemCount(long count) | |
3382 | { | |
3383 | m_selStore.SetItemCount(count); | |
3384 | m_countVirt = count; | |
3385 | ||
3386 | ResetVisibleLinesRange(); | |
3387 | ||
3388 | // scrollbars must be reset | |
3389 | m_dirty = true; | |
3390 | } | |
3391 | ||
3392 | int wxListMainWindow::GetSelectedItemCount() const | |
3393 | { | |
3394 | // deal with the quick case first | |
3395 | if ( IsSingleSel() ) | |
3396 | return HasCurrent() ? IsHighlighted(m_current) : false; | |
3397 | ||
3398 | // virtual controls remmebers all its selections itself | |
3399 | if ( IsVirtual() ) | |
3400 | return m_selStore.GetSelectedCount(); | |
3401 | ||
3402 | // TODO: we probably should maintain the number of items selected even for | |
3403 | // non virtual controls as enumerating all lines is really slow... | |
3404 | size_t countSel = 0; | |
3405 | size_t count = GetItemCount(); | |
3406 | for ( size_t line = 0; line < count; line++ ) | |
3407 | { | |
3408 | if ( GetLine(line)->IsHighlighted() ) | |
3409 | countSel++; | |
3410 | } | |
3411 | ||
3412 | return countSel; | |
3413 | } | |
3414 | ||
3415 | // ---------------------------------------------------------------------------- | |
3416 | // item position/size | |
3417 | // ---------------------------------------------------------------------------- | |
3418 | ||
3419 | wxRect wxListMainWindow::GetViewRect() const | |
3420 | { | |
3421 | wxASSERT_MSG( !HasFlag(wxLC_LIST), "not implemented for list view" ); | |
3422 | ||
3423 | // we need to find the longest/tallest label | |
3424 | wxCoord xMax = 0, yMax = 0; | |
3425 | const int count = GetItemCount(); | |
3426 | if ( count ) | |
3427 | { | |
3428 | for ( int i = 0; i < count; i++ ) | |
3429 | { | |
3430 | // we need logical, not physical, coordinates here, so use | |
3431 | // GetLineRect() instead of GetItemRect() | |
3432 | wxRect r = GetLineRect(i); | |
3433 | ||
3434 | wxCoord x = r.GetRight(), | |
3435 | y = r.GetBottom(); | |
3436 | ||
3437 | if ( x > xMax ) | |
3438 | xMax = x; | |
3439 | if ( y > yMax ) | |
3440 | yMax = y; | |
3441 | } | |
3442 | } | |
3443 | ||
3444 | // some fudge needed to make it look prettier | |
3445 | xMax += 2 * EXTRA_BORDER_X; | |
3446 | yMax += 2 * EXTRA_BORDER_Y; | |
3447 | ||
3448 | // account for the scrollbars if necessary | |
3449 | const wxSize sizeAll = GetClientSize(); | |
3450 | if ( xMax > sizeAll.x ) | |
3451 | yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y); | |
3452 | if ( yMax > sizeAll.y ) | |
3453 | xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
3454 | ||
3455 | return wxRect(0, 0, xMax, yMax); | |
3456 | } | |
3457 | ||
3458 | bool | |
3459 | wxListMainWindow::GetSubItemRect(long item, long subItem, wxRect& rect) const | |
3460 | { | |
3461 | wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM || InReportView(), | |
3462 | false, | |
3463 | _T("GetSubItemRect only meaningful in report view") ); | |
3464 | wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), false, | |
3465 | _T("invalid item in GetSubItemRect") ); | |
3466 | ||
3467 | // ensure that we're laid out, otherwise we could return nonsense | |
3468 | if ( m_dirty ) | |
3469 | { | |
3470 | wxConstCast(this, wxListMainWindow)-> | |
3471 | RecalculatePositions(true /* no refresh */); | |
3472 | } | |
3473 | ||
3474 | rect = GetLineRect((size_t)item); | |
3475 | ||
3476 | // Adjust rect to specified column | |
3477 | if ( subItem != wxLIST_GETSUBITEMRECT_WHOLEITEM ) | |
3478 | { | |
3479 | wxCHECK_MSG( subItem >= 0 && subItem < GetColumnCount(), false, | |
3480 | _T("invalid subItem in GetSubItemRect") ); | |
3481 | ||
3482 | for (int i = 0; i < subItem; i++) | |
3483 | { | |
3484 | rect.x += GetColumnWidth(i); | |
3485 | } | |
3486 | rect.width = GetColumnWidth(subItem); | |
3487 | } | |
3488 | ||
3489 | GetListCtrl()->CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y); | |
3490 | ||
3491 | return true; | |
3492 | } | |
3493 | ||
3494 | bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const | |
3495 | { | |
3496 | wxRect rect; | |
3497 | GetItemRect(item, rect); | |
3498 | ||
3499 | pos.x = rect.x; | |
3500 | pos.y = rect.y; | |
3501 | ||
3502 | return true; | |
3503 | } | |
3504 | ||
3505 | // ---------------------------------------------------------------------------- | |
3506 | // geometry calculation | |
3507 | // ---------------------------------------------------------------------------- | |
3508 | ||
3509 | void wxListMainWindow::RecalculatePositions(bool noRefresh) | |
3510 | { | |
3511 | const int lineHeight = GetLineHeight(); | |
3512 | ||
3513 | wxClientDC dc( this ); | |
3514 | dc.SetFont( GetFont() ); | |
3515 | ||
3516 | const size_t count = GetItemCount(); | |
3517 | ||
3518 | int iconSpacing; | |
3519 | if ( HasFlag(wxLC_ICON) && m_normal_image_list ) | |
3520 | iconSpacing = m_normal_spacing; | |
3521 | else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list ) | |
3522 | iconSpacing = m_small_spacing; | |
3523 | else | |
3524 | iconSpacing = 0; | |
3525 | ||
3526 | // Note that we do not call GetClientSize() here but | |
3527 | // GetSize() and subtract the border size for sunken | |
3528 | // borders manually. This is technically incorrect, | |
3529 | // but we need to know the client area's size WITHOUT | |
3530 | // scrollbars here. Since we don't know if there are | |
3531 | // any scrollbars, we use GetSize() instead. Another | |
3532 | // solution would be to call SetScrollbars() here to | |
3533 | // remove the scrollbars and call GetClientSize() then, | |
3534 | // but this might result in flicker and - worse - will | |
3535 | // reset the scrollbars to 0 which is not good at all | |
3536 | // if you resize a dialog/window, but don't want to | |
3537 | // reset the window scrolling. RR. | |
3538 | // Furthermore, we actually do NOT subtract the border | |
3539 | // width as 2 pixels is just the extra space which we | |
3540 | // need around the actual content in the window. Other- | |
3541 | // wise the text would e.g. touch the upper border. RR. | |
3542 | int clientWidth, | |
3543 | clientHeight; | |
3544 | GetSize( &clientWidth, &clientHeight ); | |
3545 | ||
3546 | if ( InReportView() ) | |
3547 | { | |
3548 | // all lines have the same height and we scroll one line per step | |
3549 | int entireHeight = count * lineHeight + LINE_SPACING; | |
3550 | ||
3551 | m_linesPerPage = clientHeight / lineHeight; | |
3552 | ||
3553 | ResetVisibleLinesRange(); | |
3554 | ||
3555 | GetListCtrl()->SetScrollbars( SCROLL_UNIT_X, lineHeight, | |
3556 | GetHeaderWidth() / SCROLL_UNIT_X, | |
3557 | (entireHeight + lineHeight - 1) / lineHeight, | |
3558 | GetListCtrl()->GetScrollPos(wxHORIZONTAL), | |
3559 | GetListCtrl()->GetScrollPos(wxVERTICAL), | |
3560 | true ); | |
3561 | } | |
3562 | else // !report | |
3563 | { | |
3564 | // we have 3 different layout strategies: either layout all items | |
3565 | // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or | |
3566 | // to arrange them in top to bottom, left to right (don't ask me why | |
3567 | // not the other way round...) order | |
3568 | if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) ) | |
3569 | { | |
3570 | int x = EXTRA_BORDER_X; | |
3571 | int y = EXTRA_BORDER_Y; | |
3572 | ||
3573 | wxCoord widthMax = 0; | |
3574 | ||
3575 | size_t i; | |
3576 | for ( i = 0; i < count; i++ ) | |
3577 | { | |
3578 | wxListLineData *line = GetLine(i); | |
3579 | line->CalculateSize( &dc, iconSpacing ); | |
3580 | line->SetPosition( x, y, iconSpacing ); | |
3581 | ||
3582 | wxSize sizeLine = GetLineSize(i); | |
3583 | ||
3584 | if ( HasFlag(wxLC_ALIGN_TOP) ) | |
3585 | { | |
3586 | if ( sizeLine.x > widthMax ) | |
3587 | widthMax = sizeLine.x; | |
3588 | ||
3589 | y += sizeLine.y; | |
3590 | } | |
3591 | else // wxLC_ALIGN_LEFT | |
3592 | { | |
3593 | x += sizeLine.x + MARGIN_BETWEEN_ROWS; | |
3594 | } | |
3595 | } | |
3596 | ||
3597 | if ( HasFlag(wxLC_ALIGN_TOP) ) | |
3598 | { | |
3599 | // traverse the items again and tweak their sizes so that they are | |
3600 | // all the same in a row | |
3601 | for ( i = 0; i < count; i++ ) | |
3602 | { | |
3603 | wxListLineData *line = GetLine(i); | |
3604 | line->m_gi->ExtendWidth(widthMax); | |
3605 | } | |
3606 | } | |
3607 | ||
3608 | GetListCtrl()->SetScrollbars | |
3609 | ( | |
3610 | SCROLL_UNIT_X, | |
3611 | lineHeight, | |
3612 | (x + SCROLL_UNIT_X) / SCROLL_UNIT_X, | |
3613 | (y + lineHeight) / lineHeight, | |
3614 | GetListCtrl()->GetScrollPos( wxHORIZONTAL ), | |
3615 | GetListCtrl()->GetScrollPos( wxVERTICAL ), | |
3616 | true | |
3617 | ); | |
3618 | } | |
3619 | else // "flowed" arrangement, the most complicated case | |
3620 | { | |
3621 | // at first we try without any scrollbars, if the items don't fit into | |
3622 | // the window, we recalculate after subtracting the space taken by the | |
3623 | // scrollbar | |
3624 | ||
3625 | int entireWidth = 0; | |
3626 | ||
3627 | for (int tries = 0; tries < 2; tries++) | |
3628 | { | |
3629 | entireWidth = 2 * EXTRA_BORDER_X; | |
3630 | ||
3631 | if (tries == 1) | |
3632 | { | |
3633 | // Now we have decided that the items do not fit into the | |
3634 | // client area, so we need a scrollbar | |
3635 | entireWidth += SCROLL_UNIT_X; | |
3636 | } | |
3637 | ||
3638 | int x = EXTRA_BORDER_X; | |
3639 | int y = EXTRA_BORDER_Y; | |
3640 | int maxWidthInThisRow = 0; | |
3641 | ||
3642 | m_linesPerPage = 0; | |
3643 | int currentlyVisibleLines = 0; | |
3644 | ||
3645 | for (size_t i = 0; i < count; i++) | |
3646 | { | |
3647 | currentlyVisibleLines++; | |
3648 | wxListLineData *line = GetLine( i ); | |
3649 | line->CalculateSize( &dc, iconSpacing ); | |
3650 | line->SetPosition( x, y, iconSpacing ); | |
3651 | ||
3652 | wxSize sizeLine = GetLineSize( i ); | |
3653 | ||
3654 | if ( maxWidthInThisRow < sizeLine.x ) | |
3655 | maxWidthInThisRow = sizeLine.x; | |
3656 | ||
3657 | y += sizeLine.y; | |
3658 | if (currentlyVisibleLines > m_linesPerPage) | |
3659 | m_linesPerPage = currentlyVisibleLines; | |
3660 | ||
3661 | if ( y + sizeLine.y >= clientHeight ) | |
3662 | { | |
3663 | currentlyVisibleLines = 0; | |
3664 | y = EXTRA_BORDER_Y; | |
3665 | maxWidthInThisRow += MARGIN_BETWEEN_ROWS; | |
3666 | x += maxWidthInThisRow; | |
3667 | entireWidth += maxWidthInThisRow; | |
3668 | maxWidthInThisRow = 0; | |
3669 | } | |
3670 | ||
3671 | // We have reached the last item. | |
3672 | if ( i == count - 1 ) | |
3673 | entireWidth += maxWidthInThisRow; | |
3674 | ||
3675 | if ( (tries == 0) && | |
3676 | (entireWidth + SCROLL_UNIT_X > clientWidth) ) | |
3677 | { | |
3678 | clientHeight -= wxSystemSettings:: | |
3679 | GetMetric(wxSYS_HSCROLL_Y); | |
3680 | m_linesPerPage = 0; | |
3681 | break; | |
3682 | } | |
3683 | ||
3684 | if ( i == count - 1 ) | |
3685 | tries = 1; // Everything fits, no second try required. | |
3686 | } | |
3687 | } | |
3688 | ||
3689 | GetListCtrl()->SetScrollbars | |
3690 | ( | |
3691 | SCROLL_UNIT_X, | |
3692 | lineHeight, | |
3693 | (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X, | |
3694 | 0, | |
3695 | GetListCtrl()->GetScrollPos( wxHORIZONTAL ), | |
3696 | 0, | |
3697 | true | |
3698 | ); | |
3699 | } | |
3700 | } | |
3701 | ||
3702 | if ( !noRefresh ) | |
3703 | { | |
3704 | // FIXME: why should we call it from here? | |
3705 | UpdateCurrent(); | |
3706 | ||
3707 | RefreshAll(); | |
3708 | } | |
3709 | } | |
3710 | ||
3711 | void wxListMainWindow::RefreshAll() | |
3712 | { | |
3713 | m_dirty = false; | |
3714 | Refresh(); | |
3715 | ||
3716 | wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin; | |
3717 | if ( headerWin && headerWin->m_dirty ) | |
3718 | { | |
3719 | headerWin->m_dirty = false; | |
3720 | headerWin->Refresh(); | |
3721 | } | |
3722 | } | |
3723 | ||
3724 | void wxListMainWindow::UpdateCurrent() | |
3725 | { | |
3726 | if ( !HasCurrent() && !IsEmpty() ) | |
3727 | ChangeCurrent(0); | |
3728 | } | |
3729 | ||
3730 | long wxListMainWindow::GetNextItem( long item, | |
3731 | int WXUNUSED(geometry), | |
3732 | int state ) const | |
3733 | { | |
3734 | long ret = item, | |
3735 | max = GetItemCount(); | |
3736 | wxCHECK_MSG( (ret == -1) || (ret < max), -1, | |
3737 | _T("invalid listctrl index in GetNextItem()") ); | |
3738 | ||
3739 | // notice that we start with the next item (or the first one if item == -1) | |
3740 | // and this is intentional to allow writing a simple loop to iterate over | |
3741 | // all selected items | |
3742 | ret++; | |
3743 | if ( ret == max ) | |
3744 | // this is not an error because the index was OK initially, | |
3745 | // just no such item | |
3746 | return -1; | |
3747 | ||
3748 | if ( !state ) | |
3749 | // any will do | |
3750 | return (size_t)ret; | |
3751 | ||
3752 | size_t count = GetItemCount(); | |
3753 | for ( size_t line = (size_t)ret; line < count; line++ ) | |
3754 | { | |
3755 | if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) ) | |
3756 | return line; | |
3757 | ||
3758 | if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) ) | |
3759 | return line; | |
3760 | } | |
3761 | ||
3762 | return -1; | |
3763 | } | |
3764 | ||
3765 | // ---------------------------------------------------------------------------- | |
3766 | // deleting stuff | |
3767 | // ---------------------------------------------------------------------------- | |
3768 | ||
3769 | void wxListMainWindow::DeleteItem( long lindex ) | |
3770 | { | |
3771 | size_t count = GetItemCount(); | |
3772 | ||
3773 | wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count), | |
3774 | _T("invalid item index in DeleteItem") ); | |
3775 | ||
3776 | size_t index = (size_t)lindex; | |
3777 | ||
3778 | // we don't need to adjust the index for the previous items | |
3779 | if ( HasCurrent() && m_current >= index ) | |
3780 | { | |
3781 | // if the current item is being deleted, we want the next one to | |
3782 | // become selected - unless there is no next one - so don't adjust | |
3783 | // m_current in this case | |
3784 | if ( m_current != index || m_current == count - 1 ) | |
3785 | m_current--; | |
3786 | } | |
3787 | ||
3788 | if ( InReportView() ) | |
3789 | { | |
3790 | // mark the Column Max Width cache as dirty if the items in the line | |
3791 | // we're deleting contain the Max Column Width | |
3792 | wxListLineData * const line = GetLine(index); | |
3793 | wxListItemDataList::compatibility_iterator n; | |
3794 | wxListItemData *itemData; | |
3795 | wxListItem item; | |
3796 | int itemWidth; | |
3797 | ||
3798 | for (size_t i = 0; i < m_columns.GetCount(); i++) | |
3799 | { | |
3800 | n = line->m_items.Item( i ); | |
3801 | itemData = n->GetData(); | |
3802 | itemData->GetItem(item); | |
3803 | ||
3804 | itemWidth = GetItemWidthWithImage(&item); | |
3805 | ||
3806 | if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth) | |
3807 | m_aColWidths.Item(i)->bNeedsUpdate = true; | |
3808 | } | |
3809 | ||
3810 | ResetVisibleLinesRange(); | |
3811 | } | |
3812 | ||
3813 | SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM, wxDefaultPosition ); | |
3814 | ||
3815 | if ( IsVirtual() ) | |
3816 | { | |
3817 | m_countVirt--; | |
3818 | m_selStore.OnItemDelete(index); | |
3819 | } | |
3820 | else | |
3821 | { | |
3822 | m_lines.RemoveAt( index ); | |
3823 | } | |
3824 | ||
3825 | // we need to refresh the (vert) scrollbar as the number of items changed | |
3826 | m_dirty = true; | |
3827 | ||
3828 | RefreshAfter(index); | |
3829 | } | |
3830 | ||
3831 | void wxListMainWindow::DeleteColumn( int col ) | |
3832 | { | |
3833 | wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col ); | |
3834 | ||
3835 | wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") ); | |
3836 | ||
3837 | m_dirty = true; | |
3838 | delete node->GetData(); | |
3839 | m_columns.Erase( node ); | |
3840 | ||
3841 | if ( !IsVirtual() ) | |
3842 | { | |
3843 | // update all the items | |
3844 | for ( size_t i = 0; i < m_lines.GetCount(); i++ ) | |
3845 | { | |
3846 | wxListLineData * const line = GetLine(i); | |
3847 | wxListItemDataList::compatibility_iterator n = line->m_items.Item( col ); | |
3848 | delete n->GetData(); | |
3849 | line->m_items.Erase(n); | |
3850 | } | |
3851 | } | |
3852 | ||
3853 | if ( InReportView() ) // we only cache max widths when in Report View | |
3854 | { | |
3855 | delete m_aColWidths.Item(col); | |
3856 | m_aColWidths.RemoveAt(col); | |
3857 | } | |
3858 | ||
3859 | // invalidate it as it has to be recalculated | |
3860 | m_headerWidth = 0; | |
3861 | } | |
3862 | ||
3863 | void wxListMainWindow::DoDeleteAllItems() | |
3864 | { | |
3865 | if ( IsEmpty() ) | |
3866 | // nothing to do - in particular, don't send the event | |
3867 | return; | |
3868 | ||
3869 | ResetCurrent(); | |
3870 | ||
3871 | // to make the deletion of all items faster, we don't send the | |
3872 | // notifications for each item deletion in this case but only one event | |
3873 | // for all of them: this is compatible with wxMSW and documented in | |
3874 | // DeleteAllItems() description | |
3875 | ||
3876 | wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() ); | |
3877 | event.SetEventObject( GetParent() ); | |
3878 | GetParent()->GetEventHandler()->ProcessEvent( event ); | |
3879 | ||
3880 | if ( IsVirtual() ) | |
3881 | { | |
3882 | m_countVirt = 0; | |
3883 | m_selStore.Clear(); | |
3884 | } | |
3885 | ||
3886 | if ( InReportView() ) | |
3887 | { | |
3888 | ResetVisibleLinesRange(); | |
3889 | for (size_t i = 0; i < m_aColWidths.GetCount(); i++) | |
3890 | { | |
3891 | m_aColWidths.Item(i)->bNeedsUpdate = true; | |
3892 | } | |
3893 | } | |
3894 | ||
3895 | m_lines.Clear(); | |
3896 | } | |
3897 | ||
3898 | void wxListMainWindow::DeleteAllItems() | |
3899 | { | |
3900 | DoDeleteAllItems(); | |
3901 | ||
3902 | RecalculatePositions(); | |
3903 | } | |
3904 | ||
3905 | void wxListMainWindow::DeleteEverything() | |
3906 | { | |
3907 | WX_CLEAR_LIST(wxListHeaderDataList, m_columns); | |
3908 | WX_CLEAR_ARRAY(m_aColWidths); | |
3909 | ||
3910 | DeleteAllItems(); | |
3911 | } | |
3912 | ||
3913 | // ---------------------------------------------------------------------------- | |
3914 | // scanning for an item | |
3915 | // ---------------------------------------------------------------------------- | |
3916 | ||
3917 | void wxListMainWindow::EnsureVisible( long index ) | |
3918 | { | |
3919 | wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(), | |
3920 | _T("invalid index in EnsureVisible") ); | |
3921 | ||
3922 | // We have to call this here because the label in question might just have | |
3923 | // been added and its position is not known yet | |
3924 | if ( m_dirty ) | |
3925 | RecalculatePositions(true /* no refresh */); | |
3926 | ||
3927 | MoveToItem((size_t)index); | |
3928 | } | |
3929 | ||
3930 | long wxListMainWindow::FindItem(long start, const wxString& str, bool partial ) | |
3931 | { | |
3932 | if (str.empty()) | |
3933 | return wxNOT_FOUND; | |
3934 | ||
3935 | long pos = start; | |
3936 | wxString str_upper = str.Upper(); | |
3937 | if (pos < 0) | |
3938 | pos = 0; | |
3939 | ||
3940 | size_t count = GetItemCount(); | |
3941 | for ( size_t i = (size_t)pos; i < count; i++ ) | |
3942 | { | |
3943 | wxListLineData *line = GetLine(i); | |
3944 | wxString line_upper = line->GetText(0).Upper(); | |
3945 | if (!partial) | |
3946 | { | |
3947 | if (line_upper == str_upper ) | |
3948 | return i; | |
3949 | } | |
3950 | else | |
3951 | { | |
3952 | if (line_upper.find(str_upper) == 0) | |
3953 | return i; | |
3954 | } | |
3955 | } | |
3956 | ||
3957 | return wxNOT_FOUND; | |
3958 | } | |
3959 | ||
3960 | long wxListMainWindow::FindItem(long start, wxUIntPtr data) | |
3961 | { | |
3962 | long pos = start; | |
3963 | if (pos < 0) | |
3964 | pos = 0; | |
3965 | ||
3966 | size_t count = GetItemCount(); | |
3967 | for (size_t i = (size_t)pos; i < count; i++) | |
3968 | { | |
3969 | wxListLineData *line = GetLine(i); | |
3970 | wxListItem item; | |
3971 | line->GetItem( 0, item ); | |
3972 | if (item.m_data == data) | |
3973 | return i; | |
3974 | } | |
3975 | ||
3976 | return wxNOT_FOUND; | |
3977 | } | |
3978 | ||
3979 | long wxListMainWindow::FindItem( const wxPoint& pt ) | |
3980 | { | |
3981 | size_t topItem; | |
3982 | GetVisibleLinesRange( &topItem, NULL ); | |
3983 | ||
3984 | wxPoint p; | |
3985 | GetItemPosition( GetItemCount() - 1, p ); | |
3986 | if ( p.y == 0 ) | |
3987 | return topItem; | |
3988 | ||
3989 | long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem ); | |
3990 | if ( id >= 0 && id < (long)GetItemCount() ) | |
3991 | return id; | |
3992 | ||
3993 | return wxNOT_FOUND; | |
3994 | } | |
3995 | ||
3996 | long wxListMainWindow::HitTest( int x, int y, int &flags ) const | |
3997 | { | |
3998 | GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y ); | |
3999 | ||
4000 | size_t count = GetItemCount(); | |
4001 | ||
4002 | if ( InReportView() ) | |
4003 | { | |
4004 | size_t current = y / GetLineHeight(); | |
4005 | if ( current < count ) | |
4006 | { | |
4007 | flags = HitTestLine(current, x, y); | |
4008 | if ( flags ) | |
4009 | return current; | |
4010 | } | |
4011 | } | |
4012 | else // !report | |
4013 | { | |
4014 | // TODO: optimize it too! this is less simple than for report view but | |
4015 | // enumerating all items is still not a way to do it!! | |
4016 | for ( size_t current = 0; current < count; current++ ) | |
4017 | { | |
4018 | flags = HitTestLine(current, x, y); | |
4019 | if ( flags ) | |
4020 | return current; | |
4021 | } | |
4022 | } | |
4023 | ||
4024 | return wxNOT_FOUND; | |
4025 | } | |
4026 | ||
4027 | // ---------------------------------------------------------------------------- | |
4028 | // adding stuff | |
4029 | // ---------------------------------------------------------------------------- | |
4030 | ||
4031 | void wxListMainWindow::InsertItem( wxListItem &item ) | |
4032 | { | |
4033 | wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") ); | |
4034 | ||
4035 | int count = GetItemCount(); | |
4036 | wxCHECK_RET( item.m_itemId >= 0, _T("invalid item index") ); | |
4037 | ||
4038 | if (item.m_itemId > count) | |
4039 | item.m_itemId = count; | |
4040 | ||
4041 | size_t id = item.m_itemId; | |
4042 | ||
4043 | m_dirty = true; | |
4044 | ||
4045 | if ( InReportView() ) | |
4046 | { | |
4047 | ResetVisibleLinesRange(); | |
4048 | ||
4049 | // calculate the width of the item and adjust the max column width | |
4050 | wxColWidthInfo *pWidthInfo = m_aColWidths.Item(item.GetColumn()); | |
4051 | int width = GetItemWidthWithImage(&item); | |
4052 | item.SetWidth(width); | |
4053 | if (width > pWidthInfo->nMaxWidth) | |
4054 | pWidthInfo->nMaxWidth = width; | |
4055 | } | |
4056 | ||
4057 | wxListLineData *line = new wxListLineData(this); | |
4058 | ||
4059 | line->SetItem( item.m_col, item ); | |
4060 | ||
4061 | m_lines.Insert( line, id ); | |
4062 | ||
4063 | m_dirty = true; | |
4064 | ||
4065 | // If an item is selected at or below the point of insertion, we need to | |
4066 | // increment the member variables because the current row's index has gone | |
4067 | // up by one | |
4068 | if ( HasCurrent() && m_current >= id ) | |
4069 | m_current++; | |
4070 | ||
4071 | SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM); | |
4072 | ||
4073 | RefreshLines(id, GetItemCount() - 1); | |
4074 | } | |
4075 | ||
4076 | void wxListMainWindow::InsertColumn( long col, wxListItem &item ) | |
4077 | { | |
4078 | m_dirty = true; | |
4079 | if ( InReportView() ) | |
4080 | { | |
4081 | if (item.m_width == wxLIST_AUTOSIZE_USEHEADER) | |
4082 | item.m_width = GetTextLength( item.m_text ); | |
4083 | ||
4084 | wxListHeaderData *column = new wxListHeaderData( item ); | |
4085 | wxColWidthInfo *colWidthInfo = new wxColWidthInfo(); | |
4086 | ||
4087 | bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount()); | |
4088 | if ( insert ) | |
4089 | { | |
4090 | wxListHeaderDataList::compatibility_iterator | |
4091 | node = m_columns.Item( col ); | |
4092 | m_columns.Insert( node, column ); | |
4093 | m_aColWidths.Insert( colWidthInfo, col ); | |
4094 | } | |
4095 | else | |
4096 | { | |
4097 | m_columns.Append( column ); | |
4098 | m_aColWidths.Add( colWidthInfo ); | |
4099 | } | |
4100 | ||
4101 | if ( !IsVirtual() ) | |
4102 | { | |
4103 | // update all the items | |
4104 | for ( size_t i = 0; i < m_lines.GetCount(); i++ ) | |
4105 | { | |
4106 | wxListLineData * const line = GetLine(i); | |
4107 | wxListItemData * const data = new wxListItemData(this); | |
4108 | if ( insert ) | |
4109 | line->m_items.Insert(col, data); | |
4110 | else | |
4111 | line->m_items.Append(data); | |
4112 | } | |
4113 | } | |
4114 | ||
4115 | // invalidate it as it has to be recalculated | |
4116 | m_headerWidth = 0; | |
4117 | } | |
4118 | } | |
4119 | ||
4120 | int wxListMainWindow::GetItemWidthWithImage(wxListItem * item) | |
4121 | { | |
4122 | int width = 0; | |
4123 | wxClientDC dc(this); | |
4124 | ||
4125 | dc.SetFont( GetFont() ); | |
4126 | ||
4127 | if (item->GetImage() != -1) | |
4128 | { | |
4129 | int ix, iy; | |
4130 | GetImageSize( item->GetImage(), ix, iy ); | |
4131 | width += ix + 5; | |
4132 | } | |
4133 | ||
4134 | if (!item->GetText().empty()) | |
4135 | { | |
4136 | wxCoord w; | |
4137 | dc.GetTextExtent( item->GetText(), &w, NULL ); | |
4138 | width += w; | |
4139 | } | |
4140 | ||
4141 | return width; | |
4142 | } | |
4143 | ||
4144 | // ---------------------------------------------------------------------------- | |
4145 | // sorting | |
4146 | // ---------------------------------------------------------------------------- | |
4147 | ||
4148 | static wxListCtrlCompare list_ctrl_compare_func_2; | |
4149 | static long list_ctrl_compare_data; | |
4150 | ||
4151 | int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 ) | |
4152 | { | |
4153 | wxListLineData *line1 = *arg1; | |
4154 | wxListLineData *line2 = *arg2; | |
4155 | wxListItem item; | |
4156 | line1->GetItem( 0, item ); | |
4157 | wxUIntPtr data1 = item.m_data; | |
4158 | line2->GetItem( 0, item ); | |
4159 | wxUIntPtr data2 = item.m_data; | |
4160 | return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data ); | |
4161 | } | |
4162 | ||
4163 | void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data ) | |
4164 | { | |
4165 | // selections won't make sense any more after sorting the items so reset | |
4166 | // them | |
4167 | HighlightAll(false); | |
4168 | ResetCurrent(); | |
4169 | ||
4170 | list_ctrl_compare_func_2 = fn; | |
4171 | list_ctrl_compare_data = data; | |
4172 | m_lines.Sort( list_ctrl_compare_func_1 ); | |
4173 | m_dirty = true; | |
4174 | } | |
4175 | ||
4176 | // ---------------------------------------------------------------------------- | |
4177 | // scrolling | |
4178 | // ---------------------------------------------------------------------------- | |
4179 | ||
4180 | void wxListMainWindow::OnScroll(wxScrollWinEvent& event) | |
4181 | { | |
4182 | // update our idea of which lines are shown when we redraw the window the | |
4183 | // next time | |
4184 | ResetVisibleLinesRange(); | |
4185 | ||
4186 | if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() ) | |
4187 | { | |
4188 | wxGenericListCtrl* lc = GetListCtrl(); | |
4189 | wxCHECK_RET( lc, _T("no listctrl window?") ); | |
4190 | ||
4191 | if (lc->m_headerWin) // when we use wxLC_NO_HEADER, m_headerWin==NULL | |
4192 | { | |
4193 | lc->m_headerWin->Refresh(); | |
4194 | lc->m_headerWin->Update(); | |
4195 | } | |
4196 | } | |
4197 | } | |
4198 | ||
4199 | int wxListMainWindow::GetCountPerPage() const | |
4200 | { | |
4201 | if ( !m_linesPerPage ) | |
4202 | { | |
4203 | wxConstCast(this, wxListMainWindow)-> | |
4204 | m_linesPerPage = GetClientSize().y / GetLineHeight(); | |
4205 | } | |
4206 | ||
4207 | return m_linesPerPage; | |
4208 | } | |
4209 | ||
4210 | void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to) | |
4211 | { | |
4212 | wxASSERT_MSG( InReportView(), _T("this is for report mode only") ); | |
4213 | ||
4214 | if ( m_lineFrom == (size_t)-1 ) | |
4215 | { | |
4216 | size_t count = GetItemCount(); | |
4217 | if ( count ) | |
4218 | { | |
4219 | m_lineFrom = GetListCtrl()->GetScrollPos(wxVERTICAL); | |
4220 | ||
4221 | // this may happen if SetScrollbars() hadn't been called yet | |
4222 | if ( m_lineFrom >= count ) | |
4223 | m_lineFrom = count - 1; | |
4224 | ||
4225 | // we redraw one extra line but this is needed to make the redrawing | |
4226 | // logic work when there is a fractional number of lines on screen | |
4227 | m_lineTo = m_lineFrom + m_linesPerPage; | |
4228 | if ( m_lineTo >= count ) | |
4229 | m_lineTo = count - 1; | |
4230 | } | |
4231 | else // empty control | |
4232 | { | |
4233 | m_lineFrom = 0; | |
4234 | m_lineTo = (size_t)-1; | |
4235 | } | |
4236 | } | |
4237 | ||
4238 | wxASSERT_MSG( IsEmpty() || | |
4239 | (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()), | |
4240 | _T("GetVisibleLinesRange() returns incorrect result") ); | |
4241 | ||
4242 | if ( from ) | |
4243 | *from = m_lineFrom; | |
4244 | if ( to ) | |
4245 | *to = m_lineTo; | |
4246 | } | |
4247 | ||
4248 | // ------------------------------------------------------------------------------------- | |
4249 | // wxGenericListCtrl | |
4250 | // ------------------------------------------------------------------------------------- | |
4251 | ||
4252 | IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl) | |
4253 | ||
4254 | BEGIN_EVENT_TABLE(wxGenericListCtrl,wxControl) | |
4255 | EVT_SIZE(wxGenericListCtrl::OnSize) | |
4256 | EVT_SCROLLWIN(wxGenericListCtrl::OnScroll) | |
4257 | END_EVENT_TABLE() | |
4258 | ||
4259 | void wxGenericListCtrl::Init() | |
4260 | { | |
4261 | m_imageListNormal = NULL; | |
4262 | m_imageListSmall = NULL; | |
4263 | m_imageListState = NULL; | |
4264 | ||
4265 | m_ownsImageListNormal = | |
4266 | m_ownsImageListSmall = | |
4267 | m_ownsImageListState = false; | |
4268 | ||
4269 | m_mainWin = NULL; | |
4270 | m_headerWin = NULL; | |
4271 | m_headerHeight = wxRendererNative::Get().GetHeaderButtonHeight(this); | |
4272 | } | |
4273 | ||
4274 | wxGenericListCtrl::~wxGenericListCtrl() | |
4275 | { | |
4276 | if (m_ownsImageListNormal) | |
4277 | delete m_imageListNormal; | |
4278 | if (m_ownsImageListSmall) | |
4279 | delete m_imageListSmall; | |
4280 | if (m_ownsImageListState) | |
4281 | delete m_imageListState; | |
4282 | } | |
4283 | ||
4284 | void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded() | |
4285 | { | |
4286 | bool needs_header = HasHeader(); | |
4287 | bool has_header = (m_headerWin != NULL); | |
4288 | ||
4289 | if (needs_header == has_header) | |
4290 | return; | |
4291 | ||
4292 | if (needs_header) | |
4293 | { | |
4294 | m_headerWin = new wxListHeaderWindow | |
4295 | ( | |
4296 | this, wxID_ANY, m_mainWin, | |
4297 | wxPoint(0,0), | |
4298 | wxSize(GetClientSize().x, m_headerHeight), | |
4299 | wxTAB_TRAVERSAL | |
4300 | ); | |
4301 | ||
4302 | #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON | |
4303 | wxFont font; | |
4304 | #if wxOSX_USE_ATSU_TEXT | |
4305 | font.MacCreateFromThemeFont( kThemeSmallSystemFont ); | |
4306 | #else | |
4307 | font.MacCreateFromUIFont( kCTFontSystemFontType ); | |
4308 | #endif | |
4309 | m_headerWin->SetFont( font ); | |
4310 | #endif | |
4311 | ||
4312 | GetSizer()->Prepend( m_headerWin, 0, wxGROW ); | |
4313 | } | |
4314 | else | |
4315 | { | |
4316 | GetSizer()->Detach( m_headerWin ); | |
4317 | ||
4318 | delete m_headerWin; | |
4319 | ||
4320 | m_headerWin = NULL; | |
4321 | } | |
4322 | } | |
4323 | ||
4324 | bool wxGenericListCtrl::Create(wxWindow *parent, | |
4325 | wxWindowID id, | |
4326 | const wxPoint &pos, | |
4327 | const wxSize &size, | |
4328 | long style, | |
4329 | const wxValidator &validator, | |
4330 | const wxString &name) | |
4331 | { | |
4332 | Init(); | |
4333 | ||
4334 | // just like in other ports, an assert will fail if the user doesn't give any type style: | |
4335 | wxASSERT_MSG( (style & wxLC_MASK_TYPE), | |
4336 | _T("wxListCtrl style should have exactly one mode bit set") ); | |
4337 | ||
4338 | if ( !wxControl::Create( parent, id, pos, size, style|wxVSCROLL|wxHSCROLL, validator, name ) ) | |
4339 | return false; | |
4340 | ||
4341 | #ifdef __WXGTK__ | |
4342 | style &= ~wxBORDER_MASK; | |
4343 | style |= wxBORDER_THEME; | |
4344 | #endif | |
4345 | ||
4346 | m_mainWin = new wxListMainWindow( this, wxID_ANY, wxPoint(0, 0), size, style ); | |
4347 | ||
4348 | SetTargetWindow( m_mainWin ); | |
4349 | ||
4350 | wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL ); | |
4351 | sizer->Add( m_mainWin, 1, wxGROW ); | |
4352 | SetSizer( sizer ); | |
4353 | ||
4354 | CreateOrDestroyHeaderWindowAsNeeded(); | |
4355 | ||
4356 | SetInitialSize(size); | |
4357 | ||
4358 | return true; | |
4359 | } | |
4360 | ||
4361 | wxBorder wxGenericListCtrl::GetDefaultBorder() const | |
4362 | { | |
4363 | return wxBORDER_THEME; | |
4364 | } | |
4365 | ||
4366 | #ifdef __WXMSW__ | |
4367 | WXLRESULT wxGenericListCtrl::MSWWindowProc(WXUINT nMsg, | |
4368 | WXWPARAM wParam, | |
4369 | WXLPARAM lParam) | |
4370 | { | |
4371 | WXLRESULT rc = wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
4372 | ||
4373 | #ifndef __WXWINCE__ | |
4374 | // we need to process arrows ourselves for scrolling | |
4375 | if ( nMsg == WM_GETDLGCODE ) | |
4376 | { | |
4377 | rc |= DLGC_WANTARROWS; | |
4378 | } | |
4379 | #endif | |
4380 | ||
4381 | return rc; | |
4382 | } | |
4383 | #endif | |
4384 | ||
4385 | wxSize wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize& size) | |
4386 | { | |
4387 | wxSize newsize = size; | |
4388 | if (m_headerWin) | |
4389 | newsize.y -= m_headerWin->GetSize().y; | |
4390 | ||
4391 | return newsize; | |
4392 | } | |
4393 | ||
4394 | void wxGenericListCtrl::OnScroll(wxScrollWinEvent& event) | |
4395 | { | |
4396 | // update our idea of which lines are shown when we redraw | |
4397 | // the window the next time | |
4398 | m_mainWin->ResetVisibleLinesRange(); | |
4399 | ||
4400 | HandleOnScroll( event ); | |
4401 | ||
4402 | if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() ) | |
4403 | { | |
4404 | m_headerWin->Refresh(); | |
4405 | m_headerWin->Update(); | |
4406 | } | |
4407 | } | |
4408 | ||
4409 | void wxGenericListCtrl::SetSingleStyle( long style, bool add ) | |
4410 | { | |
4411 | wxASSERT_MSG( !(style & wxLC_VIRTUAL), | |
4412 | _T("wxLC_VIRTUAL can't be [un]set") ); | |
4413 | ||
4414 | long flag = GetWindowStyle(); | |
4415 | ||
4416 | if (add) | |
4417 | { | |
4418 | if (style & wxLC_MASK_TYPE) | |
4419 | flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL); | |
4420 | if (style & wxLC_MASK_ALIGN) | |
4421 | flag &= ~wxLC_MASK_ALIGN; | |
4422 | if (style & wxLC_MASK_SORT) | |
4423 | flag &= ~wxLC_MASK_SORT; | |
4424 | } | |
4425 | ||
4426 | if (add) | |
4427 | flag |= style; | |
4428 | else | |
4429 | flag &= ~style; | |
4430 | ||
4431 | // some styles can be set without recreating everything (as happens in | |
4432 | // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything()) | |
4433 | if ( !(style & ~(wxLC_HRULES | wxLC_VRULES)) ) | |
4434 | { | |
4435 | Refresh(); | |
4436 | wxWindow::SetWindowStyleFlag(flag); | |
4437 | } | |
4438 | else | |
4439 | { | |
4440 | SetWindowStyleFlag( flag ); | |
4441 | } | |
4442 | } | |
4443 | ||
4444 | void wxGenericListCtrl::SetWindowStyleFlag( long flag ) | |
4445 | { | |
4446 | if (m_mainWin) | |
4447 | { | |
4448 | // m_mainWin->DeleteEverything(); wxMSW doesn't do that | |
4449 | ||
4450 | CreateOrDestroyHeaderWindowAsNeeded(); | |
4451 | ||
4452 | GetSizer()->Layout(); | |
4453 | } | |
4454 | ||
4455 | wxWindow::SetWindowStyleFlag( flag ); | |
4456 | } | |
4457 | ||
4458 | bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const | |
4459 | { | |
4460 | m_mainWin->GetColumn( col, item ); | |
4461 | return true; | |
4462 | } | |
4463 | ||
4464 | bool wxGenericListCtrl::SetColumn( int col, wxListItem& item ) | |
4465 | { | |
4466 | m_mainWin->SetColumn( col, item ); | |
4467 | return true; | |
4468 | } | |
4469 | ||
4470 | int wxGenericListCtrl::GetColumnWidth( int col ) const | |
4471 | { | |
4472 | return m_mainWin->GetColumnWidth( col ); | |
4473 | } | |
4474 | ||
4475 | bool wxGenericListCtrl::SetColumnWidth( int col, int width ) | |
4476 | { | |
4477 | m_mainWin->SetColumnWidth( col, width ); | |
4478 | return true; | |
4479 | } | |
4480 | ||
4481 | int wxGenericListCtrl::GetCountPerPage() const | |
4482 | { | |
4483 | return m_mainWin->GetCountPerPage(); // different from Windows ? | |
4484 | } | |
4485 | ||
4486 | bool wxGenericListCtrl::GetItem( wxListItem &info ) const | |
4487 | { | |
4488 | m_mainWin->GetItem( info ); | |
4489 | return true; | |
4490 | } | |
4491 | ||
4492 | bool wxGenericListCtrl::SetItem( wxListItem &info ) | |
4493 | { | |
4494 | m_mainWin->SetItem( info ); | |
4495 | return true; | |
4496 | } | |
4497 | ||
4498 | long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId ) | |
4499 | { | |
4500 | wxListItem info; | |
4501 | info.m_text = label; | |
4502 | info.m_mask = wxLIST_MASK_TEXT; | |
4503 | info.m_itemId = index; | |
4504 | info.m_col = col; | |
4505 | if ( imageId > -1 ) | |
4506 | { | |
4507 | info.m_image = imageId; | |
4508 | info.m_mask |= wxLIST_MASK_IMAGE; | |
4509 | } | |
4510 | ||
4511 | m_mainWin->SetItem(info); | |
4512 | return true; | |
4513 | } | |
4514 | ||
4515 | int wxGenericListCtrl::GetItemState( long item, long stateMask ) const | |
4516 | { | |
4517 | return m_mainWin->GetItemState( item, stateMask ); | |
4518 | } | |
4519 | ||
4520 | bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask ) | |
4521 | { | |
4522 | m_mainWin->SetItemState( item, state, stateMask ); | |
4523 | return true; | |
4524 | } | |
4525 | ||
4526 | bool | |
4527 | wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) ) | |
4528 | { | |
4529 | return SetItemColumnImage(item, 0, image); | |
4530 | } | |
4531 | ||
4532 | bool | |
4533 | wxGenericListCtrl::SetItemColumnImage( long item, long column, int image ) | |
4534 | { | |
4535 | wxListItem info; | |
4536 | info.m_image = image; | |
4537 | info.m_mask = wxLIST_MASK_IMAGE; | |
4538 | info.m_itemId = item; | |
4539 | info.m_col = column; | |
4540 | m_mainWin->SetItem( info ); | |
4541 | return true; | |
4542 | } | |
4543 | ||
4544 | wxString wxGenericListCtrl::GetItemText( long item ) const | |
4545 | { | |
4546 | return m_mainWin->GetItemText(item); | |
4547 | } | |
4548 | ||
4549 | void wxGenericListCtrl::SetItemText( long item, const wxString& str ) | |
4550 | { | |
4551 | m_mainWin->SetItemText(item, str); | |
4552 | } | |
4553 | ||
4554 | wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const | |
4555 | { | |
4556 | wxListItem info; | |
4557 | info.m_mask = wxLIST_MASK_DATA; | |
4558 | info.m_itemId = item; | |
4559 | m_mainWin->GetItem( info ); | |
4560 | return info.m_data; | |
4561 | } | |
4562 | ||
4563 | bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data ) | |
4564 | { | |
4565 | wxListItem info; | |
4566 | info.m_mask = wxLIST_MASK_DATA; | |
4567 | info.m_itemId = item; | |
4568 | info.m_data = data; | |
4569 | m_mainWin->SetItem( info ); | |
4570 | return true; | |
4571 | } | |
4572 | ||
4573 | wxRect wxGenericListCtrl::GetViewRect() const | |
4574 | { | |
4575 | return m_mainWin->GetViewRect(); | |
4576 | } | |
4577 | ||
4578 | bool wxGenericListCtrl::GetItemRect(long item, wxRect& rect, int code) const | |
4579 | { | |
4580 | return GetSubItemRect(item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code); | |
4581 | } | |
4582 | ||
4583 | bool wxGenericListCtrl::GetSubItemRect(long item, | |
4584 | long subItem, | |
4585 | wxRect& rect, | |
4586 | int WXUNUSED(code)) const | |
4587 | { | |
4588 | if ( !m_mainWin->GetSubItemRect( item, subItem, rect ) ) | |
4589 | return false; | |
4590 | ||
4591 | if ( m_mainWin->HasHeader() ) | |
4592 | rect.y += m_headerHeight + 1; | |
4593 | ||
4594 | return true; | |
4595 | } | |
4596 | ||
4597 | bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const | |
4598 | { | |
4599 | m_mainWin->GetItemPosition( item, pos ); | |
4600 | return true; | |
4601 | } | |
4602 | ||
4603 | bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) ) | |
4604 | { | |
4605 | return false; | |
4606 | } | |
4607 | ||
4608 | int wxGenericListCtrl::GetItemCount() const | |
4609 | { | |
4610 | return m_mainWin->GetItemCount(); | |
4611 | } | |
4612 | ||
4613 | int wxGenericListCtrl::GetColumnCount() const | |
4614 | { | |
4615 | return m_mainWin->GetColumnCount(); | |
4616 | } | |
4617 | ||
4618 | void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall ) | |
4619 | { | |
4620 | m_mainWin->SetItemSpacing( spacing, isSmall ); | |
4621 | } | |
4622 | ||
4623 | wxSize wxGenericListCtrl::GetItemSpacing() const | |
4624 | { | |
4625 | const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON)); | |
4626 | ||
4627 | return wxSize(spacing, spacing); | |
4628 | } | |
4629 | ||
4630 | #if WXWIN_COMPATIBILITY_2_6 | |
4631 | int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const | |
4632 | { | |
4633 | return m_mainWin->GetItemSpacing( isSmall ); | |
4634 | } | |
4635 | #endif // WXWIN_COMPATIBILITY_2_6 | |
4636 | ||
4637 | void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col ) | |
4638 | { | |
4639 | wxListItem info; | |
4640 | info.m_itemId = item; | |
4641 | info.SetTextColour( col ); | |
4642 | m_mainWin->SetItem( info ); | |
4643 | } | |
4644 | ||
4645 | wxColour wxGenericListCtrl::GetItemTextColour( long item ) const | |
4646 | { | |
4647 | wxListItem info; | |
4648 | info.m_itemId = item; | |
4649 | m_mainWin->GetItem( info ); | |
4650 | return info.GetTextColour(); | |
4651 | } | |
4652 | ||
4653 | void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col ) | |
4654 | { | |
4655 | wxListItem info; | |
4656 | info.m_itemId = item; | |
4657 | info.SetBackgroundColour( col ); | |
4658 | m_mainWin->SetItem( info ); | |
4659 | } | |
4660 | ||
4661 | wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const | |
4662 | { | |
4663 | wxListItem info; | |
4664 | info.m_itemId = item; | |
4665 | m_mainWin->GetItem( info ); | |
4666 | return info.GetBackgroundColour(); | |
4667 | } | |
4668 | ||
4669 | void wxGenericListCtrl::SetItemFont( long item, const wxFont &f ) | |
4670 | { | |
4671 | wxListItem info; | |
4672 | info.m_itemId = item; | |
4673 | info.SetFont( f ); | |
4674 | m_mainWin->SetItem( info ); | |
4675 | } | |
4676 | ||
4677 | wxFont wxGenericListCtrl::GetItemFont( long item ) const | |
4678 | { | |
4679 | wxListItem info; | |
4680 | info.m_itemId = item; | |
4681 | m_mainWin->GetItem( info ); | |
4682 | return info.GetFont(); | |
4683 | } | |
4684 | ||
4685 | int wxGenericListCtrl::GetSelectedItemCount() const | |
4686 | { | |
4687 | return m_mainWin->GetSelectedItemCount(); | |
4688 | } | |
4689 | ||
4690 | wxColour wxGenericListCtrl::GetTextColour() const | |
4691 | { | |
4692 | return GetForegroundColour(); | |
4693 | } | |
4694 | ||
4695 | void wxGenericListCtrl::SetTextColour(const wxColour& col) | |
4696 | { | |
4697 | SetForegroundColour(col); | |
4698 | } | |
4699 | ||
4700 | long wxGenericListCtrl::GetTopItem() const | |
4701 | { | |
4702 | size_t top; | |
4703 | m_mainWin->GetVisibleLinesRange(&top, NULL); | |
4704 | return (long)top; | |
4705 | } | |
4706 | ||
4707 | long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const | |
4708 | { | |
4709 | return m_mainWin->GetNextItem( item, geom, state ); | |
4710 | } | |
4711 | ||
4712 | wxImageList *wxGenericListCtrl::GetImageList(int which) const | |
4713 | { | |
4714 | if (which == wxIMAGE_LIST_NORMAL) | |
4715 | return m_imageListNormal; | |
4716 | else if (which == wxIMAGE_LIST_SMALL) | |
4717 | return m_imageListSmall; | |
4718 | else if (which == wxIMAGE_LIST_STATE) | |
4719 | return m_imageListState; | |
4720 | ||
4721 | return NULL; | |
4722 | } | |
4723 | ||
4724 | void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which ) | |
4725 | { | |
4726 | if ( which == wxIMAGE_LIST_NORMAL ) | |
4727 | { | |
4728 | if (m_ownsImageListNormal) | |
4729 | delete m_imageListNormal; | |
4730 | m_imageListNormal = imageList; | |
4731 | m_ownsImageListNormal = false; | |
4732 | } | |
4733 | else if ( which == wxIMAGE_LIST_SMALL ) | |
4734 | { | |
4735 | if (m_ownsImageListSmall) | |
4736 | delete m_imageListSmall; | |
4737 | m_imageListSmall = imageList; | |
4738 | m_ownsImageListSmall = false; | |
4739 | } | |
4740 | else if ( which == wxIMAGE_LIST_STATE ) | |
4741 | { | |
4742 | if (m_ownsImageListState) | |
4743 | delete m_imageListState; | |
4744 | m_imageListState = imageList; | |
4745 | m_ownsImageListState = false; | |
4746 | } | |
4747 | ||
4748 | m_mainWin->SetImageList( imageList, which ); | |
4749 | } | |
4750 | ||
4751 | void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which) | |
4752 | { | |
4753 | SetImageList(imageList, which); | |
4754 | if ( which == wxIMAGE_LIST_NORMAL ) | |
4755 | m_ownsImageListNormal = true; | |
4756 | else if ( which == wxIMAGE_LIST_SMALL ) | |
4757 | m_ownsImageListSmall = true; | |
4758 | else if ( which == wxIMAGE_LIST_STATE ) | |
4759 | m_ownsImageListState = true; | |
4760 | } | |
4761 | ||
4762 | bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) ) | |
4763 | { | |
4764 | return 0; | |
4765 | } | |
4766 | ||
4767 | bool wxGenericListCtrl::DeleteItem( long item ) | |
4768 | { | |
4769 | m_mainWin->DeleteItem( item ); | |
4770 | return true; | |
4771 | } | |
4772 | ||
4773 | bool wxGenericListCtrl::DeleteAllItems() | |
4774 | { | |
4775 | m_mainWin->DeleteAllItems(); | |
4776 | return true; | |
4777 | } | |
4778 | ||
4779 | bool wxGenericListCtrl::DeleteAllColumns() | |
4780 | { | |
4781 | size_t count = m_mainWin->m_columns.GetCount(); | |
4782 | for ( size_t n = 0; n < count; n++ ) | |
4783 | DeleteColumn( 0 ); | |
4784 | return true; | |
4785 | } | |
4786 | ||
4787 | void wxGenericListCtrl::ClearAll() | |
4788 | { | |
4789 | m_mainWin->DeleteEverything(); | |
4790 | } | |
4791 | ||
4792 | bool wxGenericListCtrl::DeleteColumn( int col ) | |
4793 | { | |
4794 | m_mainWin->DeleteColumn( col ); | |
4795 | ||
4796 | // if we don't have the header any longer, we need to relayout the window | |
4797 | // if ( !GetColumnCount() ) | |
4798 | ||
4799 | return true; | |
4800 | } | |
4801 | ||
4802 | wxTextCtrl *wxGenericListCtrl::EditLabel(long item, | |
4803 | wxClassInfo* textControlClass) | |
4804 | { | |
4805 | return m_mainWin->EditLabel( item, textControlClass ); | |
4806 | } | |
4807 | ||
4808 | wxTextCtrl *wxGenericListCtrl::GetEditControl() const | |
4809 | { | |
4810 | return m_mainWin->GetEditControl(); | |
4811 | } | |
4812 | ||
4813 | bool wxGenericListCtrl::EnsureVisible( long item ) | |
4814 | { | |
4815 | m_mainWin->EnsureVisible( item ); | |
4816 | return true; | |
4817 | } | |
4818 | ||
4819 | long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial ) | |
4820 | { | |
4821 | return m_mainWin->FindItem( start, str, partial ); | |
4822 | } | |
4823 | ||
4824 | long wxGenericListCtrl::FindItem( long start, wxUIntPtr data ) | |
4825 | { | |
4826 | return m_mainWin->FindItem( start, data ); | |
4827 | } | |
4828 | ||
4829 | long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt, | |
4830 | int WXUNUSED(direction)) | |
4831 | { | |
4832 | return m_mainWin->FindItem( pt ); | |
4833 | } | |
4834 | ||
4835 | // TODO: sub item hit testing | |
4836 | long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const | |
4837 | { | |
4838 | return m_mainWin->HitTest( (int)point.x, (int)point.y, flags ); | |
4839 | } | |
4840 | ||
4841 | long wxGenericListCtrl::InsertItem( wxListItem& info ) | |
4842 | { | |
4843 | m_mainWin->InsertItem( info ); | |
4844 | return info.m_itemId; | |
4845 | } | |
4846 | ||
4847 | long wxGenericListCtrl::InsertItem( long index, const wxString &label ) | |
4848 | { | |
4849 | wxListItem info; | |
4850 | info.m_text = label; | |
4851 | info.m_mask = wxLIST_MASK_TEXT; | |
4852 | info.m_itemId = index; | |
4853 | return InsertItem( info ); | |
4854 | } | |
4855 | ||
4856 | long wxGenericListCtrl::InsertItem( long index, int imageIndex ) | |
4857 | { | |
4858 | wxListItem info; | |
4859 | info.m_mask = wxLIST_MASK_IMAGE; | |
4860 | info.m_image = imageIndex; | |
4861 | info.m_itemId = index; | |
4862 | return InsertItem( info ); | |
4863 | } | |
4864 | ||
4865 | long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex ) | |
4866 | { | |
4867 | wxListItem info; | |
4868 | info.m_text = label; | |
4869 | info.m_image = imageIndex; | |
4870 | info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE; | |
4871 | info.m_itemId = index; | |
4872 | return InsertItem( info ); | |
4873 | } | |
4874 | ||
4875 | long wxGenericListCtrl::InsertColumn( long col, wxListItem &item ) | |
4876 | { | |
4877 | wxCHECK_MSG( InReportView(), -1, _T("can't add column in non report mode") ); | |
4878 | ||
4879 | m_mainWin->InsertColumn( col, item ); | |
4880 | ||
4881 | // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but | |
4882 | // still have m_headerWin==NULL | |
4883 | if (m_headerWin) | |
4884 | m_headerWin->Refresh(); | |
4885 | ||
4886 | return 0; | |
4887 | } | |
4888 | ||
4889 | long wxGenericListCtrl::InsertColumn( long col, const wxString &heading, | |
4890 | int format, int width ) | |
4891 | { | |
4892 | wxListItem item; | |
4893 | item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT; | |
4894 | item.m_text = heading; | |
4895 | if (width >= -2) | |
4896 | { | |
4897 | item.m_mask |= wxLIST_MASK_WIDTH; | |
4898 | item.m_width = width; | |
4899 | } | |
4900 | ||
4901 | item.m_format = format; | |
4902 | ||
4903 | return InsertColumn( col, item ); | |
4904 | } | |
4905 | ||
4906 | bool wxGenericListCtrl::ScrollList( int dx, int dy ) | |
4907 | { | |
4908 | return m_mainWin->ScrollList(dx, dy); | |
4909 | } | |
4910 | ||
4911 | // Sort items. | |
4912 | // fn is a function which takes 3 long arguments: item1, item2, data. | |
4913 | // item1 is the long data associated with a first item (NOT the index). | |
4914 | // item2 is the long data associated with a second item (NOT the index). | |
4915 | // data is the same value as passed to SortItems. | |
4916 | // The return value is a negative number if the first item should precede the second | |
4917 | // item, a positive number of the second item should precede the first, | |
4918 | // or zero if the two items are equivalent. | |
4919 | // data is arbitrary data to be passed to the sort function. | |
4920 | ||
4921 | bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, long data ) | |
4922 | { | |
4923 | m_mainWin->SortItems( fn, data ); | |
4924 | return true; | |
4925 | } | |
4926 | ||
4927 | // ---------------------------------------------------------------------------- | |
4928 | // event handlers | |
4929 | // ---------------------------------------------------------------------------- | |
4930 | ||
4931 | void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event)) | |
4932 | { | |
4933 | if (!m_mainWin) return; | |
4934 | ||
4935 | // We need to override OnSize so that our scrolled | |
4936 | // window a) does call Layout() to use sizers for | |
4937 | // positioning the controls but b) does not query | |
4938 | // the sizer for their size and use that for setting | |
4939 | // the scrollable area as set that ourselves by | |
4940 | // calling SetScrollbar() further down. | |
4941 | ||
4942 | Layout(); | |
4943 | ||
4944 | m_mainWin->RecalculatePositions(); | |
4945 | ||
4946 | AdjustScrollbars(); | |
4947 | } | |
4948 | ||
4949 | void wxGenericListCtrl::OnInternalIdle() | |
4950 | { | |
4951 | wxWindow::OnInternalIdle(); | |
4952 | ||
4953 | if (m_mainWin->m_dirty) | |
4954 | m_mainWin->RecalculatePositions(); | |
4955 | } | |
4956 | ||
4957 | // ---------------------------------------------------------------------------- | |
4958 | // font/colours | |
4959 | // ---------------------------------------------------------------------------- | |
4960 | ||
4961 | bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour ) | |
4962 | { | |
4963 | if (m_mainWin) | |
4964 | { | |
4965 | m_mainWin->SetBackgroundColour( colour ); | |
4966 | m_mainWin->m_dirty = true; | |
4967 | } | |
4968 | ||
4969 | return true; | |
4970 | } | |
4971 | ||
4972 | bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour ) | |
4973 | { | |
4974 | if ( !wxWindow::SetForegroundColour( colour ) ) | |
4975 | return false; | |
4976 | ||
4977 | if (m_mainWin) | |
4978 | { | |
4979 | m_mainWin->SetForegroundColour( colour ); | |
4980 | m_mainWin->m_dirty = true; | |
4981 | } | |
4982 | ||
4983 | if (m_headerWin) | |
4984 | m_headerWin->SetForegroundColour( colour ); | |
4985 | ||
4986 | return true; | |
4987 | } | |
4988 | ||
4989 | bool wxGenericListCtrl::SetFont( const wxFont &font ) | |
4990 | { | |
4991 | if ( !wxWindow::SetFont( font ) ) | |
4992 | return false; | |
4993 | ||
4994 | if (m_mainWin) | |
4995 | { | |
4996 | m_mainWin->SetFont( font ); | |
4997 | m_mainWin->m_dirty = true; | |
4998 | } | |
4999 | ||
5000 | if (m_headerWin) | |
5001 | { | |
5002 | m_headerWin->SetFont( font ); | |
5003 | // CalculateAndSetHeaderHeight(); | |
5004 | } | |
5005 | ||
5006 | Refresh(); | |
5007 | ||
5008 | return true; | |
5009 | } | |
5010 | ||
5011 | // static | |
5012 | wxVisualAttributes | |
5013 | wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant) | |
5014 | { | |
5015 | #if _USE_VISATTR | |
5016 | // Use the same color scheme as wxListBox | |
5017 | return wxListBox::GetClassDefaultAttributes(variant); | |
5018 | #else | |
5019 | wxUnusedVar(variant); | |
5020 | wxVisualAttributes attr; | |
5021 | attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT); | |
5022 | attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX); | |
5023 | attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
5024 | return attr; | |
5025 | #endif | |
5026 | } | |
5027 | ||
5028 | // ---------------------------------------------------------------------------- | |
5029 | // methods forwarded to m_mainWin | |
5030 | // ---------------------------------------------------------------------------- | |
5031 | ||
5032 | #if wxUSE_DRAG_AND_DROP | |
5033 | ||
5034 | void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget ) | |
5035 | { | |
5036 | m_mainWin->SetDropTarget( dropTarget ); | |
5037 | } | |
5038 | ||
5039 | wxDropTarget *wxGenericListCtrl::GetDropTarget() const | |
5040 | { | |
5041 | return m_mainWin->GetDropTarget(); | |
5042 | } | |
5043 | ||
5044 | #endif | |
5045 | ||
5046 | bool wxGenericListCtrl::SetCursor( const wxCursor &cursor ) | |
5047 | { | |
5048 | return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false; | |
5049 | } | |
5050 | ||
5051 | wxColour wxGenericListCtrl::GetBackgroundColour() const | |
5052 | { | |
5053 | return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour(); | |
5054 | } | |
5055 | ||
5056 | wxColour wxGenericListCtrl::GetForegroundColour() const | |
5057 | { | |
5058 | return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour(); | |
5059 | } | |
5060 | ||
5061 | bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y ) | |
5062 | { | |
5063 | #if wxUSE_MENUS | |
5064 | return m_mainWin->PopupMenu( menu, x, y ); | |
5065 | #else | |
5066 | return false; | |
5067 | #endif | |
5068 | } | |
5069 | ||
5070 | void wxGenericListCtrl::DoClientToScreen( int *x, int *y ) const | |
5071 | { | |
5072 | m_mainWin->DoClientToScreen(x, y); | |
5073 | } | |
5074 | ||
5075 | void wxGenericListCtrl::DoScreenToClient( int *x, int *y ) const | |
5076 | { | |
5077 | m_mainWin->DoScreenToClient(x, y); | |
5078 | } | |
5079 | ||
5080 | void wxGenericListCtrl::SetFocus() | |
5081 | { | |
5082 | // The test in window.cpp fails as we are a composite | |
5083 | // window, so it checks against "this", but not m_mainWin. | |
5084 | if ( DoFindFocus() != this ) | |
5085 | m_mainWin->SetFocus(); | |
5086 | } | |
5087 | ||
5088 | wxSize wxGenericListCtrl::DoGetBestSize() const | |
5089 | { | |
5090 | // Something is better than nothing... | |
5091 | // 100x80 is what the MSW version will get from the default | |
5092 | // wxControl::DoGetBestSize | |
5093 | return wxSize(100, 80); | |
5094 | } | |
5095 | ||
5096 | // ---------------------------------------------------------------------------- | |
5097 | // virtual list control support | |
5098 | // ---------------------------------------------------------------------------- | |
5099 | ||
5100 | wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const | |
5101 | { | |
5102 | // this is a pure virtual function, in fact - which is not really pure | |
5103 | // because the controls which are not virtual don't need to implement it | |
5104 | wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") ); | |
5105 | ||
5106 | return wxEmptyString; | |
5107 | } | |
5108 | ||
5109 | int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const | |
5110 | { | |
5111 | wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL), | |
5112 | -1, | |
5113 | wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden.")); | |
5114 | return -1; | |
5115 | } | |
5116 | ||
5117 | int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const | |
5118 | { | |
5119 | if (!column) | |
5120 | return OnGetItemImage(item); | |
5121 | ||
5122 | return -1; | |
5123 | } | |
5124 | ||
5125 | wxListItemAttr * | |
5126 | wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const | |
5127 | { | |
5128 | wxASSERT_MSG( item >= 0 && item < GetItemCount(), | |
5129 | _T("invalid item index in OnGetItemAttr()") ); | |
5130 | ||
5131 | // no attributes by default | |
5132 | return NULL; | |
5133 | } | |
5134 | ||
5135 | void wxGenericListCtrl::SetItemCount(long count) | |
5136 | { | |
5137 | wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") ); | |
5138 | ||
5139 | m_mainWin->SetItemCount(count); | |
5140 | } | |
5141 | ||
5142 | void wxGenericListCtrl::RefreshItem(long item) | |
5143 | { | |
5144 | m_mainWin->RefreshLine(item); | |
5145 | } | |
5146 | ||
5147 | void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo) | |
5148 | { | |
5149 | m_mainWin->RefreshLines(itemFrom, itemTo); | |
5150 | } | |
5151 | ||
5152 | // Generic wxListCtrl is more or less a container for two other | |
5153 | // windows which drawings are done upon. These are namely | |
5154 | // 'm_headerWin' and 'm_mainWin'. | |
5155 | // Here we override 'virtual wxWindow::Refresh()' to mimic the | |
5156 | // behaviour wxListCtrl has under wxMSW. | |
5157 | // | |
5158 | void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect) | |
5159 | { | |
5160 | if (!rect) | |
5161 | { | |
5162 | // The easy case, no rectangle specified. | |
5163 | if (m_headerWin) | |
5164 | m_headerWin->Refresh(eraseBackground); | |
5165 | ||
5166 | if (m_mainWin) | |
5167 | m_mainWin->Refresh(eraseBackground); | |
5168 | } | |
5169 | else | |
5170 | { | |
5171 | // Refresh the header window | |
5172 | if (m_headerWin) | |
5173 | { | |
5174 | wxRect rectHeader = m_headerWin->GetRect(); | |
5175 | rectHeader.Intersect(*rect); | |
5176 | if (rectHeader.GetWidth() && rectHeader.GetHeight()) | |
5177 | { | |
5178 | int x, y; | |
5179 | m_headerWin->GetPosition(&x, &y); | |
5180 | rectHeader.Offset(-x, -y); | |
5181 | m_headerWin->Refresh(eraseBackground, &rectHeader); | |
5182 | } | |
5183 | } | |
5184 | ||
5185 | // Refresh the main window | |
5186 | if (m_mainWin) | |
5187 | { | |
5188 | wxRect rectMain = m_mainWin->GetRect(); | |
5189 | rectMain.Intersect(*rect); | |
5190 | if (rectMain.GetWidth() && rectMain.GetHeight()) | |
5191 | { | |
5192 | int x, y; | |
5193 | m_mainWin->GetPosition(&x, &y); | |
5194 | rectMain.Offset(-x, -y); | |
5195 | m_mainWin->Refresh(eraseBackground, &rectMain); | |
5196 | } | |
5197 | } | |
5198 | } | |
5199 | } | |
5200 | ||
5201 | #endif // wxUSE_LISTCTRL |