]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listctrl.cpp
192ddb36a0bd06529e9e563e085f3c6b0cd9e8db
[wxWidgets.git] / src / generic / listctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: listctrl.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "listctrl.h"
12 #endif
13
14 #include "wx/dcscreen.h"
15 #include "wx/app.h"
16 #include "wx/listctrl.h"
17
18 //-----------------------------------------------------------------------------
19 // wxListItemData
20 //-----------------------------------------------------------------------------
21
22 IMPLEMENT_DYNAMIC_CLASS(wxListItemData,wxObject);
23
24 wxListItemData::wxListItemData(void)
25 {
26 m_image = -1;
27 m_data = 0;
28 m_xpos = 0;
29 m_ypos = 0;
30 m_width = 0;
31 m_height = 0;
32 m_colour = wxBLACK;
33 }
34
35 wxListItemData::wxListItemData( const wxListItem &info )
36 {
37 m_image = -1;
38 m_data = 0;
39 m_colour = info.m_colour;
40 SetItem( info );
41 }
42
43 void wxListItemData::SetItem( const wxListItem &info )
44 {
45 if (info.m_mask & wxLIST_MASK_TEXT) m_text = info.m_text;
46 if (info.m_mask & wxLIST_MASK_IMAGE) m_image = info.m_image;
47 if (info.m_mask & wxLIST_MASK_DATA) m_data = info.m_data;
48 m_colour = info.m_colour;
49 m_xpos = 0;
50 m_ypos = 0;
51 m_width = info.m_width;
52 m_height = 0;
53 }
54
55 void wxListItemData::SetText( const wxString &s )
56 {
57 m_text = s;
58 }
59
60 void wxListItemData::SetImage( int image )
61 {
62 m_image = image;
63 }
64
65 void wxListItemData::SetData( long data )
66 {
67 m_data = data;
68 }
69
70 void wxListItemData::SetPosition( int x, int y )
71 {
72 m_xpos = x;
73 m_ypos = y;
74 }
75
76 void wxListItemData::SetSize( int const width, int height )
77 {
78 if (width != -1) m_width = width;
79 if (height != -1) m_height = height;
80 }
81
82 void wxListItemData::SetColour( wxColour *col )
83 {
84 m_colour = col;
85 }
86
87 bool wxListItemData::HasImage(void) const
88 {
89 return (m_image >= 0);
90 }
91
92 bool wxListItemData::HasText(void) const
93 {
94 return (!m_text.IsNull());
95 }
96
97 bool wxListItemData::IsHit( int x, int y ) const
98 {
99 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
100 }
101
102 void wxListItemData::GetText( wxString &s )
103 {
104 s = m_text;
105 }
106
107 int wxListItemData::GetX( void ) const
108 {
109 return m_xpos;
110 }
111
112 int wxListItemData::GetY( void ) const
113 {
114 return m_ypos;
115 }
116
117 int wxListItemData::GetWidth(void) const
118 {
119 return m_width;
120 }
121
122 int wxListItemData::GetHeight(void) const
123 {
124 return m_height;
125 }
126
127 int wxListItemData::GetImage(void) const
128 {
129 return m_image;
130 }
131
132 void wxListItemData::GetItem( wxListItem &info )
133 {
134 info.m_text = m_text;
135 info.m_image = m_image;
136 info.m_data = m_data;
137 }
138
139 wxColour *wxListItemData::GetColour(void)
140 {
141 return m_colour;
142 }
143
144 //-----------------------------------------------------------------------------
145 // wxListHeaderData
146 //-----------------------------------------------------------------------------
147
148 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderData,wxObject);
149
150 wxListHeaderData::wxListHeaderData(void)
151 {
152 m_mask = 0;
153 m_image = 0;
154 m_format = 0;
155 m_width = 0;
156 m_xpos = 0;
157 m_ypos = 0;
158 m_height = 0;
159 }
160
161 wxListHeaderData::wxListHeaderData( const wxListItem &item )
162 {
163 SetItem( item );
164 m_xpos = 0;
165 m_ypos = 0;
166 m_height = 0;
167 }
168
169 void wxListHeaderData::SetItem( const wxListItem &item )
170 {
171 m_mask = item.m_mask;
172 m_text = item.m_text;
173 m_image = item.m_image;
174 m_format = item.m_format;
175 m_width = item.m_width;
176 if (m_width < 0) m_width = 80;
177 if (m_width < 6) m_width = 6;
178 }
179
180 void wxListHeaderData::SetPosition( int x, int y )
181 {
182 m_xpos = x;
183 m_ypos = y;
184 }
185
186 void wxListHeaderData::SetHeight( int h )
187 {
188 m_height = h;
189 }
190
191 void wxListHeaderData::SetWidth( int w )
192 {
193 m_width = w;
194 if (m_width < 0) m_width = 80;
195 if (m_width < 6) m_width = 6;
196 }
197
198 void wxListHeaderData::SetFormat( int format )
199 {
200 m_format = format;
201 }
202
203 bool wxListHeaderData::HasImage(void) const
204 {
205 return (m_image != 0);
206 }
207
208 bool wxListHeaderData::HasText(void) const
209 {
210 return (m_text.Length() > 0);
211 }
212
213 bool wxListHeaderData::IsHit( int x, int y ) const
214 {
215 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
216 }
217
218 void wxListHeaderData::GetItem( wxListItem &item )
219 {
220 item.m_mask = m_mask;
221 item.m_text = m_text;
222 item.m_image = m_image;
223 item.m_format = m_format;
224 item.m_width = m_width;
225 }
226
227 void wxListHeaderData::GetText( wxString &s )
228 {
229 s = m_text;
230 }
231
232 int wxListHeaderData::GetImage(void) const
233 {
234 return m_image;
235 }
236
237 int wxListHeaderData::GetWidth(void) const
238 {
239 return m_width;
240 }
241
242 int wxListHeaderData::GetFormat(void) const
243 {
244 return m_format;
245 }
246
247 //-----------------------------------------------------------------------------
248 // wxListLineData
249 //-----------------------------------------------------------------------------
250
251 IMPLEMENT_DYNAMIC_CLASS(wxListLineData,wxObject);
252
253 wxListLineData::wxListLineData( wxListMainWindow *owner, int mode, wxBrush *hilightBrush )
254 {
255 m_mode = mode;
256 m_hilighted = FALSE;
257 m_owner = owner;
258 m_hilightBrush = hilightBrush;
259 m_items.DeleteContents( TRUE );
260 m_spacing = 0;
261 }
262
263 void wxListLineData::CalculateSize( wxPaintDC *dc, int spacing )
264 {
265 m_spacing = spacing;
266 switch (m_mode)
267 {
268 case wxLC_ICON:
269 {
270 m_bound_all.width = m_spacing;
271 m_bound_all.height = m_spacing+13;
272 wxNode *node = m_items.First();
273 if (node)
274 {
275 wxListItemData *item = (wxListItemData*)node->Data();
276 wxString s;
277 item->GetText( s );
278 long lw,lh;
279 dc->GetTextExtent( s, &lw, &lh );
280 if (lw > m_spacing) m_bound_all.width = lw;
281 }
282 break;
283 }
284 case wxLC_LIST:
285 {
286 wxNode *node = m_items.First();
287 if (node)
288 {
289 wxListItemData *item = (wxListItemData*)node->Data();
290 wxString s;
291 item->GetText( s );
292 long lw,lh;
293 dc->GetTextExtent( s, &lw, &lh );
294 m_bound_all.width = lw;
295 m_bound_all.height = lh;
296 }
297 break;
298 }
299 case wxLC_REPORT:
300 {
301 m_bound_all.width = 0;
302 m_bound_all.height = 0;
303 wxNode *node = m_items.First();
304 while (node)
305 {
306 wxListItemData *item = (wxListItemData*)node->Data();
307 wxString s;
308 item->GetText( s );
309 if (s.IsNull()) s = "H";
310 long lw,lh;
311 dc->GetTextExtent( s, &lw, &lh );
312 item->SetSize( item->GetWidth(), lh );
313 m_bound_all.width += lw;
314 m_bound_all.height = lh;
315 node = node->Next();
316 }
317 break;
318 }
319 }
320 }
321
322 void wxListLineData::SetPosition( wxPaintDC *dc, int x, int y, int window_width )
323 {
324 m_bound_all.x = x;
325 m_bound_all.y = y;
326 switch (m_mode)
327 {
328 case wxLC_ICON:
329 {
330 AssignRect( m_bound_icon, 0, 0, 0, 0 );
331 AssignRect( m_bound_label, 0, 0, 0, 0 );
332 AssignRect( m_bound_hilight, m_bound_all );
333 wxNode *node = m_items.First();
334 if (node)
335 {
336 wxListItemData *item = (wxListItemData*)node->Data();
337 if (item->HasImage())
338 {
339 wxListItemData *item = (wxListItemData*)node->Data();
340 int w = 0;
341 int h = 0;
342 m_owner->GetImageSize( item->GetImage(), w, h );
343 m_bound_icon.x = m_bound_all.x + (m_spacing/2) - (w/2);
344 m_bound_icon.y = m_bound_all.y + m_spacing - h - 5;
345 m_bound_icon.width = w;
346 m_bound_icon.height = h;
347 if (!item->HasText())
348 {
349 AssignRect( m_bound_hilight, m_bound_icon );
350 m_bound_hilight.x -= 5;
351 m_bound_hilight.y -= 5;
352 m_bound_hilight.width += 9;
353 m_bound_hilight.height += 9;
354 }
355 }
356 if (item->HasText())
357 {
358 wxString s;
359 item->GetText( s );
360 long lw,lh;
361 dc->GetTextExtent( s, &lw, &lh );
362 if (m_bound_all.width > m_spacing)
363 m_bound_label.x = m_bound_all.x;
364 else
365 m_bound_label.x = m_bound_all.x + (m_spacing/2) - lw/2;
366 m_bound_label.y = m_bound_all.y + m_bound_all.height - lh;
367 m_bound_label.width = lw;
368 m_bound_label.height = lh;
369 AssignRect( m_bound_hilight, m_bound_label );
370 m_bound_hilight.x -= 2;
371 m_bound_hilight.y -= 2;
372 m_bound_hilight.width += 4;
373 m_bound_hilight.height += 4;
374 }
375 }
376 break;
377 }
378 case wxLC_LIST:
379 {
380 AssignRect( m_bound_label, m_bound_all );
381 m_bound_all.x -= 2;
382 m_bound_all.y -= 2;
383 m_bound_all.width += 4;
384 m_bound_all.height += 3;
385 AssignRect( m_bound_hilight, m_bound_all );
386 AssignRect( m_bound_icon, 0, 0, 0, 0 );
387 break;
388 }
389 case wxLC_REPORT:
390 {
391 long lw,lh;
392 dc->GetTextExtent( "H", &lw, &lh );
393 m_bound_all.x = 0;
394 m_bound_all.y -= 0;
395 m_bound_all.height = lh+3;
396 m_bound_all.width = window_width;
397 AssignRect( m_bound_hilight, m_bound_all );
398 AssignRect( m_bound_label, 0, 0, 0 ,0 );
399 AssignRect( m_bound_icon, 0, 0, 0, 0 );
400 break;
401 }
402 }
403 }
404
405 void wxListLineData::SetColumnPosition( int index, int x )
406 {
407 int i = index;
408 wxNode *node = m_items.Nth( i );
409 if (node)
410 {
411 wxListItemData *item = (wxListItemData*)node->Data();
412 item->SetPosition( x, m_bound_all.y+1 );
413 }
414 }
415
416 void wxListLineData::GetSize( int &width, int &height )
417 {
418 width = m_bound_all.width;
419 height = m_bound_all.height;
420 }
421
422 void wxListLineData::GetExtent( int &x, int &y, int &width, int &height )
423 {
424 x = m_bound_all.x;
425 y = m_bound_all.y;
426 width = m_bound_all.width;
427 height = m_bound_all.height;
428 }
429
430 void wxListLineData::GetLabelExtent( int &x, int &y, int &width, int &height )
431 {
432 x = m_bound_label.x;
433 y = m_bound_label.y;
434 width = m_bound_label.width;
435 height = m_bound_label.height;
436 }
437
438 void wxListLineData::GetRect( wxRectangle &rect )
439 {
440 AssignRect( rect, m_bound_all );
441 }
442
443 long wxListLineData::IsHit( int x, int y )
444 {
445 wxNode *node = m_items.First();
446 if (node)
447 {
448 wxListItemData *item = (wxListItemData*)node->Data();
449 if (item->HasImage() && IsInRect( x, y, m_bound_icon )) return wxLIST_HITTEST_ONITEMICON;
450 if (item->HasText() && IsInRect( x, y, m_bound_label )) return wxLIST_HITTEST_ONITEMLABEL;
451 // if (!(item->HasImage() || item->HasText())) return 0;
452 }
453 // if there is no icon or text = empty
454 if (IsInRect( x, y, m_bound_all )) return wxLIST_HITTEST_ONITEMICON;
455 return 0;
456 }
457
458 void wxListLineData::InitItems( int num )
459 {
460 for (int i = 0; i < num; i++) m_items.Append( new wxListItemData() );
461 }
462
463 void wxListLineData::SetItem( int index, const wxListItem &info )
464 {
465 wxNode *node = m_items.Nth( index );
466 if (node)
467 {
468 wxListItemData *item = (wxListItemData*)node->Data();
469 item->SetItem( info );
470 }
471 }
472
473 void wxListLineData::GetItem( int const index, wxListItem &info )
474 {
475 int i = index;
476 wxNode *node = m_items.Nth( i );
477 if (node)
478 {
479 wxListItemData *item = (wxListItemData*)node->Data();
480 item->GetItem( info );
481 }
482 }
483
484 void wxListLineData::GetText( int index, wxString &s )
485 {
486 int i = index;
487 wxNode *node = m_items.Nth( i );
488 s = "";
489 if (node)
490 {
491 wxListItemData *item = (wxListItemData*)node->Data();
492 item->GetText( s );
493 }
494 }
495
496 void wxListLineData::SetText( int index, const wxString s )
497 {
498 int i = index;
499 wxNode *node = m_items.Nth( i );
500 if (node)
501 {
502 wxListItemData *item = (wxListItemData*)node->Data();
503 item->SetText( s );
504 }
505 }
506
507 int wxListLineData::GetImage( int index )
508 {
509 int i = index;
510 wxNode *node = m_items.Nth( i );
511 if (node)
512 {
513 wxListItemData *item = (wxListItemData*)node->Data();
514 return item->GetImage();
515 }
516 return -1;
517 }
518
519 void wxListLineData::DoDraw( wxPaintDC *dc, bool hilight, bool paintBG )
520 {
521 long dev_x = dc->LogicalToDeviceX( m_bound_all.x-2 );
522 long dev_y = dc->LogicalToDeviceY( m_bound_all.y-2 );
523 long dev_w = dc->LogicalToDeviceXRel( m_bound_all.width+4 );
524 long dev_h = dc->LogicalToDeviceYRel( m_bound_all.height+4 );
525
526 if (!m_owner->IsExposed( dev_x, dev_y, dev_w, dev_h ))
527 {
528 return;
529 }
530
531 if (paintBG)
532 {
533 if (hilight)
534 {
535 dc->SetBrush( * m_hilightBrush );
536 dc->SetPen( * wxTRANSPARENT_PEN );
537 }
538 else
539 {
540 dc->SetBrush( * wxWHITE_BRUSH );
541 dc->SetPen( * wxTRANSPARENT_PEN );
542 }
543 dc->DrawRectangle( m_bound_hilight.x, m_bound_hilight.y,
544 m_bound_hilight.width, m_bound_hilight.height );
545 }
546
547 if (m_mode == wxLC_REPORT)
548 {
549 wxString s;
550 wxNode *node = m_items.First();
551 while (node)
552 {
553 wxListItemData *item = (wxListItemData*)node->Data();
554 dc->SetClippingRegion( item->GetX(), item->GetY(), item->GetWidth()-3, item->GetHeight() );
555 int x = item->GetX();
556 if (item->HasImage())
557 {
558 int y = 0;
559 m_owner->DrawImage( item->GetImage(), dc, x, item->GetY() );
560 m_owner->GetImageSize( item->GetImage(), x, y );
561 x += item->GetX() + 5;
562 }
563 if (item->HasText())
564 {
565 item->GetText( s );
566 if (hilight)
567 dc->SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
568 else
569 dc->SetTextForeground( *item->GetColour() );
570 dc->DrawText( s, x, item->GetY() );
571 }
572 dc->DestroyClippingRegion();
573 node = node->Next();
574 }
575 }
576 else
577 {
578 wxNode *node = m_items.First();
579 if (node)
580 {
581 wxListItemData *item = (wxListItemData*)node->Data();
582 if (item->HasImage())
583 {
584 m_owner->DrawImage( item->GetImage(), dc, m_bound_icon.x, m_bound_icon.y );
585 }
586 if (item->HasText())
587 {
588 wxString s;
589 item->GetText( s );
590 if (hilight)
591 dc->SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
592 else
593 dc->SetTextForeground( * item->GetColour() );
594 dc->DrawText( s, m_bound_label.x, m_bound_label.y );
595 }
596 }
597 }
598 }
599
600 void wxListLineData::Hilight( bool on )
601 {
602 if (on == m_hilighted) return;
603 if (on)
604 m_owner->SelectLine( this );
605 else
606 m_owner->DeselectLine( this );
607 m_hilighted = on;
608 }
609
610 void wxListLineData::ReverseHilight( void )
611 {
612 m_hilighted = !m_hilighted;
613 if (m_hilighted)
614 m_owner->SelectLine( this );
615 else
616 m_owner->DeselectLine( this );
617 }
618
619 void wxListLineData::DrawRubberBand( wxPaintDC *dc, bool on )
620 {
621 if (on)
622 {
623 dc->SetPen( * wxBLACK_PEN );
624 dc->SetBrush( * wxTRANSPARENT_BRUSH );
625 dc->DrawRectangle( m_bound_hilight.x, m_bound_hilight.y,
626 m_bound_hilight.width, m_bound_hilight.height );
627 }
628 }
629
630 void wxListLineData::Draw( wxPaintDC *dc )
631 {
632 DoDraw( dc, m_hilighted, m_hilighted );
633 }
634
635 bool wxListLineData::IsInRect( int x, int y, const wxRectangle &rect )
636 {
637 return ((x >= rect.x) && (x <= rect.x+rect.width) &&
638 (y >= rect.y) && (y <= rect.y+rect.height));
639 }
640
641 bool wxListLineData::IsHilighted( void )
642 {
643 return m_hilighted;
644 }
645
646 void wxListLineData::AssignRect( wxRectangle &dest, int x, int y, int width, int height )
647 {
648 dest.x = x;
649 dest.y = y;
650 dest.width = width;
651 dest.height = height;
652 }
653
654 void wxListLineData::AssignRect( wxRectangle &dest, const wxRectangle &source )
655 {
656 dest.x = source.x;
657 dest.y = source.y;
658 dest.width = source.width;
659 dest.height = source.height;
660 }
661
662 //-----------------------------------------------------------------------------
663 // wxListHeaderWindow
664 //-----------------------------------------------------------------------------
665
666 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow,wxWindow);
667
668 BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow)
669 EVT_PAINT (wxListHeaderWindow::OnPaint)
670 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse)
671 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus)
672 END_EVENT_TABLE()
673
674 wxListHeaderWindow::wxListHeaderWindow( void )
675 {
676 m_owner = (wxListMainWindow *) NULL;
677 m_currentCursor = (wxCursor *) NULL;
678 m_resizeCursor = (wxCursor *) NULL;
679 m_isDraging = FALSE;
680 }
681
682 wxListHeaderWindow::wxListHeaderWindow( wxWindow *win, wxWindowID id, wxListMainWindow *owner,
683 const wxPoint &pos, const wxSize &size,
684 long style, const wxString &name ) :
685 wxWindow( win, id, pos, size, style, name )
686 {
687 m_owner = owner;
688 // m_currentCursor = wxSTANDARD_CURSOR;
689 m_currentCursor = (wxCursor *) NULL;
690 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
691 m_isDraging = FALSE;
692 }
693
694 wxListHeaderWindow::~wxListHeaderWindow( void )
695 {
696 delete m_resizeCursor;
697 }
698
699 void wxListHeaderWindow::DoDrawRect( wxPaintDC *dc, int x, int y, int w, int h )
700 {
701 const int m_corner = 1;
702
703 dc->SetBrush( *wxTRANSPARENT_BRUSH );
704
705 dc->SetPen( *wxBLACK_PEN );
706 dc->DrawLine( x+w-m_corner+1, y, x+w, y+h ); // right (outer)
707 dc->DrawRectangle( x, y+h, w, 1 ); // bottom (outer)
708
709 wxPen pen( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNSHADOW ), 1, wxSOLID );
710
711 dc->SetPen( pen );
712 dc->DrawLine( x+w-m_corner, y, x+w-1, y+h ); // right (inner)
713 dc->DrawRectangle( x+1, y+h-1, w-2, 1 ); // bottom (inner)
714
715 dc->SetPen( *wxWHITE_PEN );
716 dc->DrawRectangle( x, y, w-m_corner+1, 1 ); // top (outer)
717 dc->DrawRectangle( x, y, 1, h ); // left (outer)
718 dc->DrawLine( x, y+h-1, x+1, y+h-1 );
719 dc->DrawLine( x+w-1, y, x+w-1, y+1 );
720 }
721
722 void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
723 {
724 wxPaintDC dc( this );
725 PrepareDC( dc );
726
727 dc.BeginDrawing();
728
729 dc.SetFont( GetFont() );
730
731 int w = 0;
732 int h = 0;
733 int x = 0;
734 int y = 0;
735 GetClientSize( &w, &h );
736
737 dc.SetTextForeground( *wxBLACK );
738 if (m_foregroundColour.Ok()) dc.SetTextForeground( m_foregroundColour );
739
740 x = 1;
741 y = 1;
742 int numColumns = m_owner->GetColumnCount();
743 wxListItem item;
744 for (int i = 0; i < numColumns; i++)
745 {
746 m_owner->GetColumn( i, item );
747 int cw = item.m_width-2;
748 if ((i+1 == numColumns) || (x+item.m_width > w-5)) cw = w-x-1;
749 dc.SetPen( *wxWHITE_PEN );
750
751 DoDrawRect( &dc, x, y, cw, h-2 );
752 dc.SetClippingRegion( x, y, cw-5, h-4 );
753 dc.DrawText( item.m_text, x+4, y+3 );
754 dc.DestroyClippingRegion();
755 x += item.m_width;
756 if (x > w+5) break;
757 }
758 dc.EndDrawing();
759 }
760
761 void wxListHeaderWindow::DrawCurrent()
762 {
763 int x1 = m_currentX;
764 int y1 = 0;
765 int x2 = m_currentX-1;
766 int y2 = 0;
767 int dummy;
768 m_owner->GetClientSize( &dummy, &y2 );
769 ClientToScreen( &x1, &y1 );
770 m_owner->ClientToScreen( &x2, &y2 );
771
772 wxScreenDC dc;
773 dc.SetLogicalFunction( wxXOR );
774 dc.SetPen( wxPen( *wxBLACK, 2, wxSOLID ) );
775 dc.SetBrush( *wxTRANSPARENT_BRUSH );
776
777 dc.DrawLine( x1, y1, x2, y2 );
778
779 dc.SetLogicalFunction( wxCOPY );
780
781 dc.SetPen( wxNullPen );
782 dc.SetBrush( wxNullBrush );
783 }
784
785 void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
786 {
787 int x = event.GetX();
788 int y = event.GetY();
789 if (m_isDraging)
790 {
791 DrawCurrent();
792 if (event.ButtonUp())
793 {
794 ReleaseMouse();
795 m_isDraging = FALSE;
796 m_owner->SetColumnWidth( m_column, m_currentX-m_minX );
797 }
798 else
799 {
800 int size_x = 0;
801 int dummy;
802 GetClientSize( &size_x, & dummy );
803 if (x > m_minX+7)
804 m_currentX = x;
805 else
806 m_currentX = m_minX+7;
807 if (m_currentX > size_x-7) m_currentX = size_x-7;
808 DrawCurrent();
809 }
810 return;
811 }
812
813 m_minX = 0;
814 bool hit_border = FALSE;
815 int xpos = 0;
816 for (int j = 0; j < m_owner->GetColumnCount(); j++)
817 {
818 xpos += m_owner->GetColumnWidth( j );
819 if ((abs(x-xpos) < 3) && (y < 22))
820 {
821 hit_border = TRUE;
822 m_column = j;
823 break;
824 }
825 m_minX = xpos;
826 }
827
828 if (event.LeftDown() && hit_border)
829 {
830 m_isDraging = TRUE;
831 m_currentX = x;
832 DrawCurrent();
833 CaptureMouse();
834 return;
835 }
836
837 if (event.Moving())
838 {
839 if (hit_border)
840 {
841 if (m_currentCursor == wxSTANDARD_CURSOR) SetCursor( * m_resizeCursor );
842 m_currentCursor = m_resizeCursor;
843 }
844 else
845 {
846 if (m_currentCursor != wxSTANDARD_CURSOR) SetCursor( * wxSTANDARD_CURSOR );
847 m_currentCursor = wxSTANDARD_CURSOR;
848 }
849 }
850 }
851
852 void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
853 {
854 m_owner->SetFocus();
855 }
856
857 //-----------------------------------------------------------------------------
858 // wxListRenameTimer (internal)
859 //-----------------------------------------------------------------------------
860
861 wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
862 {
863 m_owner = owner;
864 }
865
866 void wxListRenameTimer::Notify()
867 {
868 m_owner->OnRenameTimer();
869 }
870
871 //-----------------------------------------------------------------------------
872 // wxListTextCtrl (internal)
873 //-----------------------------------------------------------------------------
874
875 IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl,wxTextCtrl);
876
877 BEGIN_EVENT_TABLE(wxListTextCtrl,wxTextCtrl)
878 EVT_CHAR (wxListTextCtrl::OnChar)
879 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus)
880 END_EVENT_TABLE()
881
882 wxListTextCtrl::wxListTextCtrl( wxWindow *parent, const wxWindowID id,
883 bool *accept, wxString *res, wxListMainWindow *owner,
884 const wxString &value, const wxPoint &pos, const wxSize &size,
885 int style, const wxValidator& validator, const wxString &name ) :
886 wxTextCtrl( parent, id, value, pos, size, style, validator, name )
887 {
888 m_res = res;
889 m_accept = accept;
890 m_owner = owner;
891 }
892
893 void wxListTextCtrl::OnChar( wxKeyEvent &event )
894 {
895 if (event.m_keyCode == WXK_RETURN)
896 {
897 (*m_accept) = TRUE;
898 (*m_res) = GetValue();
899 m_owner->OnRenameAccept();
900 // Show( FALSE );
901 Destroy();
902 return;
903 }
904 if (event.m_keyCode == WXK_ESCAPE)
905 {
906 (*m_accept) = FALSE;
907 (*m_res) = "";
908 // Show( FALSE );
909 Destroy();
910 return;
911 }
912 event.Skip();
913 }
914
915 void wxListTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
916 {
917 (*m_accept) = FALSE;
918 (*m_res) = "";
919 // Show( FALSE );
920 Destroy();
921 return;
922 }
923
924 //-----------------------------------------------------------------------------
925 // wxListMainWindow
926 //-----------------------------------------------------------------------------
927
928 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow,wxScrolledWindow);
929
930 BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledWindow)
931 EVT_PAINT (wxListMainWindow::OnPaint)
932 EVT_SIZE (wxListMainWindow::OnSize)
933 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
934 EVT_CHAR (wxListMainWindow::OnChar)
935 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
936 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
937 END_EVENT_TABLE()
938
939 wxListMainWindow::wxListMainWindow( void )
940 {
941 m_mode = 0;
942 m_lines.DeleteContents( TRUE );
943 m_columns.DeleteContents( TRUE );
944 m_current = (wxListLineData *) NULL;
945 m_visibleLines = 0;
946 m_hilightBrush = (wxBrush *) NULL;
947 m_xScroll = 0;
948 m_yScroll = 0;
949 m_dirty = TRUE;
950 m_small_image_list = (wxImageList *) NULL;
951 m_normal_image_list = (wxImageList *) NULL;
952 m_small_spacing = 30;
953 m_normal_spacing = 40;
954 m_hasFocus = FALSE;
955 m_usedKeys = TRUE;
956 m_lastOnSame = FALSE;
957 m_renameTimer = new wxListRenameTimer( this );
958 m_isCreated = FALSE;
959 m_dragCount = 0;
960 }
961
962 wxListMainWindow::wxListMainWindow( wxWindow *parent, wxWindowID id,
963 const wxPoint &pos, const wxSize &size,
964 long style, const wxString &name ) :
965 wxScrolledWindow( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name )
966 {
967 m_mode = style;
968 m_lines.DeleteContents( TRUE );
969 m_columns.DeleteContents( TRUE );
970 m_current = (wxListLineData *) NULL;
971 m_dirty = TRUE;
972 m_visibleLines = 0;
973 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
974 m_small_image_list = (wxImageList *) NULL;
975 m_normal_image_list = (wxImageList *) NULL;
976 m_small_spacing = 30;
977 m_normal_spacing = 40;
978 m_hasFocus = FALSE;
979 m_dragCount = 0;
980 m_isCreated = FALSE;
981 wxSize sz = size;
982 sz.y = 25;
983
984 if (m_mode & wxLC_REPORT)
985 {
986 m_xScroll = 0;
987 m_yScroll = 15;
988 }
989 else
990 {
991 m_xScroll = 15;
992 m_yScroll = 0;
993 }
994 SetScrollbars( m_xScroll, m_yScroll, 0, 0, 0, 0 );
995
996 m_usedKeys = TRUE;
997 m_lastOnSame = FALSE;
998 m_renameTimer = new wxListRenameTimer( this );
999 m_renameAccept = FALSE;
1000
1001 SetBackgroundColour( *wxWHITE );
1002 }
1003
1004 wxListMainWindow::~wxListMainWindow( void )
1005 {
1006 if (m_hilightBrush) delete m_hilightBrush;
1007
1008 delete m_renameTimer;
1009 }
1010
1011 void wxListMainWindow::RefreshLine( wxListLineData *line )
1012 {
1013 int x = 0;
1014 int y = 0;
1015 int w = 0;
1016 int h = 0;
1017 if (line)
1018 {
1019 wxClientDC dc(this);
1020 PrepareDC( dc );
1021 line->GetExtent( x, y, w, h );
1022 wxRectangle rect(
1023 dc.LogicalToDeviceX(x-3),
1024 dc.LogicalToDeviceY(y-3),
1025 dc.LogicalToDeviceXRel(w+6),
1026 dc.LogicalToDeviceXRel(h+6) );
1027 Refresh( TRUE, &rect );
1028 }
1029 }
1030
1031 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1032 {
1033 if (m_dirty) return;
1034
1035 if (m_lines.GetCount() == 0) return;
1036
1037 wxPaintDC dc( this );
1038 PrepareDC( dc );
1039
1040 dc.BeginDrawing();
1041
1042 dc.SetFont( GetFont() );
1043
1044 if (m_mode & wxLC_REPORT)
1045 {
1046 int lineSpacing = 0;
1047 wxListLineData *line = (wxListLineData*)m_lines.First()->Data();
1048 int dummy = 0;
1049 line->GetSize( dummy, lineSpacing );
1050 lineSpacing += 1;
1051
1052 int y_s = m_yScroll*GetScrollPos( wxVERTICAL );
1053
1054 wxNode *node = m_lines.Nth( y_s / lineSpacing );
1055 for (int i = 0; i < m_visibleLines+2; i++)
1056 {
1057 if (!node) break;
1058
1059 line = (wxListLineData*)node->Data();
1060 line->Draw( &dc );
1061 node = node->Next();
1062 }
1063 }
1064 else
1065 {
1066 wxNode *node = m_lines.First();
1067 while (node)
1068 {
1069 wxListLineData *line = (wxListLineData*)node->Data();
1070 line->Draw( &dc );
1071 node = node->Next();
1072 }
1073 }
1074
1075 if (m_current) m_current->DrawRubberBand( &dc, m_hasFocus );
1076
1077 dc.EndDrawing();
1078 }
1079
1080 void wxListMainWindow::HilightAll( bool on )
1081 {
1082 wxNode *node = m_lines.First();
1083 while (node)
1084 {
1085 wxListLineData *line = (wxListLineData *)node->Data();
1086 if (line->IsHilighted() != on)
1087 {
1088 line->Hilight( on );
1089 RefreshLine( line );
1090 }
1091 node = node->Next();
1092 }
1093 }
1094
1095 void wxListMainWindow::SendNotify( wxListLineData *line, wxEventType command )
1096 {
1097 wxListEvent le( command, GetParent()->GetId() );
1098 le.SetEventObject( GetParent() );
1099 le.m_itemIndex = GetIndexOfLine( line );
1100 line->GetItem( 0, le.m_item );
1101 GetParent()->GetEventHandler()->ProcessEvent( le );
1102 }
1103
1104 void wxListMainWindow::FocusLine( wxListLineData *WXUNUSED(line) )
1105 {
1106 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_FOCUSSED );
1107 }
1108
1109 void wxListMainWindow::UnfocusLine( wxListLineData *WXUNUSED(line) )
1110 {
1111 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_UNFOCUSSED );
1112 }
1113
1114 void wxListMainWindow::SelectLine( wxListLineData *line )
1115 {
1116 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_SELECTED );
1117 }
1118
1119 void wxListMainWindow::DeselectLine( wxListLineData *line )
1120 {
1121 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_DESELECTED );
1122 }
1123
1124 void wxListMainWindow::DeleteLine( wxListLineData *line )
1125 {
1126 SendNotify( line, wxEVT_COMMAND_LIST_DELETE_ITEM );
1127 }
1128
1129 void wxListMainWindow::StartLabelEdit( wxListLineData *line )
1130 {
1131 SendNotify( line, wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT );
1132 }
1133
1134 void wxListMainWindow::RenameLine( wxListLineData *line, const wxString &newName )
1135 {
1136 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
1137 le.SetEventObject( GetParent() );
1138 le.m_itemIndex = GetIndexOfLine( line );
1139 line->GetItem( 0, le.m_item );
1140 le.m_item.m_text = newName;
1141 GetParent()->GetEventHandler()->ProcessEvent( le );
1142 }
1143
1144 void wxListMainWindow::OnRenameTimer()
1145 {
1146 StartLabelEdit( m_current );
1147 wxString s;
1148 m_current->GetText( 0, s );
1149 int x = 0;
1150 int y = 0;
1151 int w = 0;
1152 int h = 0;
1153 m_current->GetLabelExtent( x, y, w, h );
1154
1155 wxClientDC dc(this);
1156 PrepareDC( dc );
1157 x = dc.LogicalToDeviceX( x );
1158 y = dc.LogicalToDeviceY( y );
1159
1160 wxListTextCtrl *text = new wxListTextCtrl(
1161 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
1162 text->SetFocus();
1163 }
1164
1165 void wxListMainWindow::OnRenameAccept()
1166 {
1167 RenameLine( m_current, m_renameRes );
1168 }
1169
1170 void wxListMainWindow::OnMouse( wxMouseEvent &event )
1171 {
1172 if (GetParent()->GetEventHandler()->ProcessEvent( event)) return;
1173
1174 if (!m_current) return;
1175 if (m_dirty) return;
1176
1177 wxClientDC dc(this);
1178 PrepareDC(dc);
1179 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1180 long y = dc.DeviceToLogicalY( (long)event.GetY() );
1181
1182 // Did we actually hit an item ?
1183 long hitResult = 0;
1184 wxNode *node = m_lines.First();
1185 wxListLineData *line = (wxListLineData *) NULL;
1186 while (node)
1187 {
1188 line = (wxListLineData*)node->Data();
1189 hitResult = line->IsHit( x, y );
1190 if (hitResult) break;
1191 line = (wxListLineData *) NULL;
1192 node = node->Next();
1193 }
1194
1195 if (!event.Dragging())
1196 m_dragCount = 0;
1197 else
1198 m_dragCount++;
1199
1200 if (event.Dragging() && (m_dragCount > 3))
1201 {
1202 m_dragCount = 0;
1203
1204 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_DRAG, GetParent()->GetId() );
1205 le.SetEventObject( GetParent() );
1206 le.m_pointDrag.x = x;
1207 le.m_pointDrag.y = y;
1208 GetParent()->GetEventHandler()->ProcessEvent( le );
1209
1210 return;
1211 }
1212
1213 if (!line) return;
1214
1215 if (event.ButtonDClick())
1216 {
1217 m_usedKeys = FALSE;
1218 m_lastOnSame = FALSE;
1219 m_renameTimer->Stop();
1220
1221 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
1222
1223 return;
1224 }
1225
1226 if (event.LeftUp() && m_lastOnSame)
1227 {
1228 m_usedKeys = FALSE;
1229 if ((line == m_current) &&
1230 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
1231 (m_mode & wxLC_EDIT_LABELS) )
1232 {
1233 m_renameTimer->Start( 100, TRUE );
1234 }
1235 m_lastOnSame = FALSE;
1236 return;
1237 }
1238
1239 if (event.RightDown())
1240 {
1241 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK );
1242 return;
1243 }
1244
1245 if (event.MiddleDown())
1246 {
1247 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
1248 return;
1249 }
1250
1251 if (event.LeftDown())
1252 {
1253 m_usedKeys = FALSE;
1254 wxListLineData *oldCurrent = m_current;
1255 if (m_mode & wxLC_SINGLE_SEL)
1256 {
1257 m_current = line;
1258 HilightAll( FALSE );
1259 m_current->ReverseHilight();
1260 RefreshLine( m_current );
1261 }
1262 else
1263 {
1264 if (event.ShiftDown())
1265 {
1266 m_current = line;
1267 m_current->ReverseHilight();
1268 RefreshLine( m_current );
1269 }
1270 else if (event.ControlDown())
1271 {
1272 m_current = line;
1273
1274 int numOfCurrent = -1;
1275 node = m_lines.First();
1276 while (node)
1277 {
1278 wxListLineData *test_line = (wxListLineData*)node->Data();
1279 numOfCurrent++;
1280 if (test_line == oldCurrent) break;
1281 node = node->Next();
1282 }
1283
1284 int numOfLine = -1;
1285 node = m_lines.First();
1286 while (node)
1287 {
1288 wxListLineData *test_line = (wxListLineData*)node->Data();
1289 numOfLine++;
1290 if (test_line == line) break;
1291 node = node->Next();
1292 }
1293
1294 if (numOfLine < numOfCurrent)
1295 {
1296 int i = numOfLine;
1297 numOfLine = numOfCurrent;
1298 numOfCurrent = i;
1299 }
1300
1301 wxNode *node = m_lines.Nth( numOfCurrent );
1302 for (int i = 0; i <= numOfLine-numOfCurrent; i++)
1303 {
1304 wxListLineData *test_line= (wxListLineData*)node->Data();
1305 test_line->Hilight(TRUE);
1306 RefreshLine( test_line );
1307 node = node->Next();
1308 }
1309 }
1310 else
1311 {
1312 m_current = line;
1313 HilightAll( FALSE );
1314 m_current->ReverseHilight();
1315 RefreshLine( m_current );
1316 }
1317 }
1318 if (m_current != oldCurrent)
1319 {
1320 RefreshLine( oldCurrent );
1321 UnfocusLine( oldCurrent );
1322 FocusLine( m_current );
1323 }
1324 m_lastOnSame = (m_current == oldCurrent);
1325 return;
1326 }
1327 }
1328
1329 void wxListMainWindow::MoveToFocus( void )
1330 {
1331 if (!m_current) return;
1332
1333 int x = 0;
1334 int y = 0;
1335 int w = 0;
1336 int h = 0;
1337 m_current->GetExtent( x, y, w, h );
1338
1339 int w_p = 0;
1340 int h_p = 0;
1341 GetClientSize( &w_p, &h_p );
1342
1343 if (m_mode & wxLC_REPORT)
1344 {
1345 int y_s = m_yScroll*GetScrollPos( wxVERTICAL );
1346 if ((y > y_s) && (y+h < y_s+h_p)) return;
1347 if (y-y_s < 5) Scroll( -1, (y-5-h_p/2)/m_yScroll );
1348 if (y+h+5 > y_s+h_p) Scroll( -1, (y+h-h_p/2+h+15)/m_yScroll );
1349 }
1350 else
1351 {
1352 int x_s = m_xScroll*GetScrollPos( wxHORIZONTAL );
1353 if ((x > x_s) && (x+w < x_s+w_p)) return;
1354 if (x-x_s < 5) Scroll( (x-5)/m_xScroll, -1 );
1355 if (x+w-5 > x_s+w_p) Scroll( (x+w-w_p+15)/m_xScroll, -1 );
1356 }
1357 }
1358
1359 void wxListMainWindow::OnArrowChar( wxListLineData *newCurrent, bool shiftDown )
1360 {
1361 if ((m_mode & wxLC_SINGLE_SEL) || (m_usedKeys == FALSE)) m_current->Hilight( FALSE );
1362 wxListLineData *oldCurrent = m_current;
1363 m_current = newCurrent;
1364 MoveToFocus();
1365 if (shiftDown || (m_mode & wxLC_SINGLE_SEL)) m_current->Hilight( TRUE );
1366 RefreshLine( m_current );
1367 RefreshLine( oldCurrent );
1368 FocusLine( m_current );
1369 UnfocusLine( oldCurrent );
1370 }
1371
1372 void wxListMainWindow::OnChar( wxKeyEvent &event )
1373 {
1374 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
1375 le.m_code = event.KeyCode();
1376 le.SetEventObject( GetParent() );
1377 GetParent()->GetEventHandler()->ProcessEvent( le );
1378
1379 /*
1380 if (event.KeyCode() == WXK_TAB)
1381 {
1382 if (event.ShiftDown())
1383 TravPrev( &event );
1384 else
1385 TravNext( &event );
1386 return;
1387 }
1388 */
1389 if ( !m_current )
1390 {
1391 event.Skip();
1392 return;
1393 }
1394
1395 switch (event.KeyCode())
1396 {
1397 case WXK_UP:
1398 {
1399 wxNode *node = m_lines.Member( m_current )->Previous();
1400 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1401 break;
1402 }
1403 case WXK_DOWN:
1404 {
1405 wxNode *node = m_lines.Member( m_current )->Next();
1406 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1407 break;
1408 }
1409 case WXK_END:
1410 {
1411 wxNode *node = m_lines.Last();
1412 OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1413 break;
1414 }
1415 case WXK_HOME:
1416 {
1417 wxNode *node = m_lines.First();
1418 OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1419 break;
1420 }
1421 case WXK_PRIOR:
1422 {
1423 int steps = 0;
1424 if (m_mode & wxLC_REPORT) { steps = m_visibleLines-1; }
1425 else
1426 {
1427 int pos = 0;
1428 wxNode *node = m_lines.First();
1429 for (;;) { if (m_current == (wxListLineData*)node->Data()) break; pos++; node = node->Next(); }
1430 steps = pos % m_visibleLines;
1431 }
1432 wxNode *node = m_lines.Member( m_current );
1433 for (int i = 0; i < steps; i++) if (node->Previous()) node = node->Previous();
1434 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1435 break;
1436 }
1437 case WXK_NEXT:
1438 {
1439 int steps = 0;
1440 if (m_mode & wxLC_REPORT) { steps = m_visibleLines-1; }
1441 else
1442 {
1443 int pos = 0; wxNode *node = m_lines.First();
1444 for (;;) { if (m_current == (wxListLineData*)node->Data()) break; pos++; node = node->Next(); }
1445 steps = m_visibleLines-(pos % m_visibleLines)-1;
1446 }
1447 wxNode *node = m_lines.Member( m_current );
1448 for (int i = 0; i < steps; i++) if (node->Next()) node = node->Next();
1449 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1450 break;
1451 }
1452 case WXK_LEFT:
1453 {
1454 if (!(m_mode & wxLC_REPORT))
1455 {
1456 wxNode *node = m_lines.Member( m_current );
1457 for (int i = 0; i <m_visibleLines; i++) if (node->Previous()) node = node->Previous();
1458 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1459 }
1460 break;
1461 }
1462 case WXK_RIGHT:
1463 {
1464 if (!(m_mode & wxLC_REPORT))
1465 {
1466 wxNode *node = m_lines.Member( m_current );
1467 for (int i = 0; i <m_visibleLines; i++) if (node->Next()) node = node->Next();
1468 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1469 }
1470 break;
1471 }
1472 case WXK_SPACE:
1473 {
1474 m_current->ReverseHilight();
1475 RefreshLine( m_current );
1476 }
1477 break;
1478 case WXK_INSERT:
1479 {
1480 if (!(m_mode & wxLC_SINGLE_SEL))
1481 {
1482 wxListLineData *oldCurrent = m_current;
1483 m_current->ReverseHilight();
1484 wxNode *node = m_lines.Member( m_current )->Next();
1485 if (node) m_current = (wxListLineData*)node->Data();
1486 MoveToFocus();
1487 RefreshLine( oldCurrent );
1488 RefreshLine( m_current );
1489 UnfocusLine( oldCurrent );
1490 FocusLine( m_current );
1491 }
1492 }
1493 break;
1494 case WXK_RETURN:
1495 case WXK_EXECUTE:
1496 {
1497 wxListEvent le( wxEVT_COMMAND_LIST_ITEM_ACTIVATED, GetParent()->GetId() );
1498 le.SetEventObject( GetParent() );
1499 le.m_itemIndex = GetIndexOfLine( m_current );
1500 m_current->GetItem( 0, le.m_item );
1501 GetParent()->GetEventHandler()->ProcessEvent( le );
1502 }
1503 break;
1504 default:
1505 {
1506 event.Skip();
1507 return;
1508 }
1509 }
1510 m_usedKeys = TRUE;
1511 }
1512
1513 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1514 {
1515 m_hasFocus = TRUE;
1516 RefreshLine( m_current );
1517
1518 if (!GetParent()) return;
1519
1520 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
1521 event.SetEventObject( GetParent() );
1522 GetParent()->GetEventHandler()->ProcessEvent( event );
1523 }
1524
1525 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1526 {
1527 m_hasFocus = FALSE;
1528 RefreshLine( m_current );
1529 }
1530
1531 void wxListMainWindow::OnSize( wxSizeEvent &WXUNUSED(event) )
1532 {
1533 /*
1534 We don't even allow the wxScrolledWindow::AdjustScrollbars() call
1535
1536 */
1537 }
1538
1539 void wxListMainWindow::DrawImage( int index, wxPaintDC *dc, int x, int y )
1540 {
1541 if ((m_mode & wxLC_ICON) && (m_normal_image_list))
1542 {
1543 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1544 return;
1545 }
1546 if ((m_mode & wxLC_SMALL_ICON) && (m_small_image_list))
1547 {
1548 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1549 }
1550 if ((m_mode & wxLC_REPORT) && (m_small_image_list))
1551 {
1552 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1553 return;
1554 }
1555 }
1556
1557 void wxListMainWindow::GetImageSize( int index, int &width, int &height )
1558 {
1559 if ((m_mode & wxLC_ICON) && (m_normal_image_list))
1560 {
1561 m_normal_image_list->GetSize( index, width, height );
1562 return;
1563 }
1564 if ((m_mode & wxLC_SMALL_ICON) && (m_small_image_list))
1565 {
1566 m_small_image_list->GetSize( index, width, height );
1567 return;
1568 }
1569 if ((m_mode & wxLC_REPORT) && (m_small_image_list))
1570 {
1571 m_small_image_list->GetSize( index, width, height );
1572 return;
1573 }
1574 width = 0;
1575 height = 0;
1576 }
1577
1578 int wxListMainWindow::GetTextLength( wxString &s )
1579 {
1580 wxPaintDC dc( this );
1581 long lw = 0;
1582 long lh = 0;
1583 dc.GetTextExtent( s, &lw, &lh );
1584 return lw + 6;
1585 }
1586
1587 int wxListMainWindow::GetIndexOfLine( const wxListLineData *line )
1588 {
1589 int i = 0;
1590 wxNode *node = m_lines.First();
1591 while (node)
1592 {
1593 if (line == (wxListLineData*)node->Data()) return i;
1594 i++;
1595 node = node->Next();
1596 }
1597 return -1;
1598 }
1599
1600 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
1601 {
1602 m_dirty = TRUE;
1603 if (which == wxIMAGE_LIST_NORMAL) m_normal_image_list = imageList;
1604 if (which == wxIMAGE_LIST_SMALL) m_small_image_list = imageList;
1605 }
1606
1607 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
1608 {
1609 m_dirty = TRUE;
1610 if (isSmall)
1611 {
1612 m_small_spacing = spacing;
1613 }
1614 else
1615 {
1616 m_normal_spacing = spacing;
1617 }
1618 }
1619
1620 int wxListMainWindow::GetItemSpacing( bool isSmall )
1621 {
1622 if (isSmall) return m_small_spacing; else return m_normal_spacing;
1623 }
1624
1625 void wxListMainWindow::SetColumn( int col, wxListItem &item )
1626 {
1627 m_dirty = TRUE;
1628 wxNode *node = m_columns.Nth( col );
1629 if (node)
1630 {
1631 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER) item.m_width = GetTextLength( item.m_text )+7;
1632 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1633 column->SetItem( item );
1634 }
1635 wxListCtrl *lc = (wxListCtrl*) GetParent();
1636 if (lc->m_headerWin) lc->m_headerWin->Refresh();
1637 }
1638
1639 void wxListMainWindow::SetColumnWidth( int col, int width )
1640 {
1641 if (!(m_mode & wxLC_REPORT)) return;
1642
1643 m_dirty = TRUE;
1644
1645 wxNode *node = m_columns.Nth( col );
1646 if (node)
1647 {
1648 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1649 column->SetWidth( width );
1650 }
1651
1652 node = m_lines.First();
1653 while (node)
1654 {
1655 wxListLineData *line = (wxListLineData*)node->Data();
1656 wxNode *n = line->m_items.Nth( col );
1657 if (n)
1658 {
1659 wxListItemData *item = (wxListItemData*)n->Data();
1660 item->SetSize( width, -1 );
1661 }
1662 node = node->Next();
1663 }
1664
1665 wxListCtrl *lc = (wxListCtrl*) GetParent();
1666 if (lc->m_headerWin) lc->m_headerWin->Refresh();
1667 }
1668
1669 void wxListMainWindow::GetColumn( int col, wxListItem &item )
1670 {
1671 wxNode *node = m_columns.Nth( col );
1672 if (node)
1673 {
1674 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1675 column->GetItem( item );
1676 }
1677 else
1678 {
1679 item.m_format = 0;
1680 item.m_width = 0;
1681 item.m_text = "";
1682 item.m_image = 0;
1683 item.m_data = 0;
1684 }
1685 }
1686
1687 int wxListMainWindow::GetColumnWidth( int col )
1688 {
1689 wxNode *node = m_columns.Nth( col );
1690 if (node)
1691 {
1692 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1693 return column->GetWidth();
1694 }
1695 else
1696 {
1697 return 0;
1698 }
1699 }
1700
1701 int wxListMainWindow::GetColumnCount( void )
1702 {
1703 return m_columns.Number();
1704 }
1705
1706 int wxListMainWindow::GetCountPerPage( void )
1707 {
1708 return m_visibleLines;
1709 }
1710
1711 void wxListMainWindow::SetItem( wxListItem &item )
1712 {
1713 m_dirty = TRUE;
1714 wxNode *node = m_lines.Nth( item.m_itemId );
1715 if (node)
1716 {
1717 wxListLineData *line = (wxListLineData*)node->Data();
1718 if (m_mode & wxLC_REPORT) item.m_width = GetColumnWidth( item.m_col )-3;
1719 line->SetItem( item.m_col, item );
1720 }
1721 }
1722
1723 void wxListMainWindow::SetItemState( long item, long state, long stateMask )
1724 {
1725 // m_dirty = TRUE; no recalcs needed
1726
1727 wxListLineData *oldCurrent = m_current;
1728
1729 if (stateMask & wxLIST_STATE_FOCUSED)
1730 {
1731 wxNode *node = m_lines.Nth( item );
1732 if (node)
1733 {
1734 wxListLineData *line = (wxListLineData*)node->Data();
1735 UnfocusLine( m_current );
1736 m_current = line;
1737 FocusLine( m_current );
1738 RefreshLine( m_current );
1739 RefreshLine( oldCurrent );
1740 }
1741 }
1742
1743 if (stateMask & wxLIST_STATE_SELECTED)
1744 {
1745 bool on = state & wxLIST_STATE_SELECTED;
1746 if (!on && (m_mode & wxLC_SINGLE_SEL)) return;
1747
1748 wxNode *node = m_lines.Nth( item );
1749 if (node)
1750 {
1751 wxListLineData *line = (wxListLineData*)node->Data();
1752 if (m_mode & wxLC_SINGLE_SEL)
1753 {
1754 UnfocusLine( m_current );
1755 m_current = line;
1756 FocusLine( m_current );
1757 oldCurrent->Hilight( FALSE );
1758 RefreshLine( m_current );
1759 RefreshLine( oldCurrent );
1760 }
1761 bool on = state & wxLIST_STATE_SELECTED;
1762 if (on != line->IsHilighted())
1763 {
1764 line->Hilight( on );
1765 RefreshLine( line );
1766 }
1767 }
1768 }
1769 }
1770
1771 int wxListMainWindow::GetItemState( long item, long stateMask )
1772 {
1773 int ret = wxLIST_STATE_DONTCARE;
1774 if (stateMask & wxLIST_STATE_FOCUSED)
1775 {
1776 wxNode *node = m_lines.Nth( item );
1777 if (node)
1778 {
1779 wxListLineData *line = (wxListLineData*)node->Data();
1780 if (line == m_current) ret |= wxLIST_STATE_FOCUSED;
1781 }
1782 }
1783 if (stateMask & wxLIST_STATE_SELECTED)
1784 {
1785 wxNode *node = m_lines.Nth( item );
1786 if (node)
1787 {
1788 wxListLineData *line = (wxListLineData*)node->Data();
1789 if (line->IsHilighted()) ret |= wxLIST_STATE_FOCUSED;
1790 }
1791 }
1792 return ret;
1793 }
1794
1795 void wxListMainWindow::GetItem( wxListItem &item )
1796 {
1797 wxNode *node = m_lines.Nth( item.m_itemId );
1798 if (node)
1799 {
1800 wxListLineData *line = (wxListLineData*)node->Data();
1801 line->GetItem( item.m_col, item );
1802 }
1803 else
1804 {
1805 item.m_mask = 0;
1806 item.m_text = "";
1807 item.m_image = 0;
1808 item.m_data = 0;
1809 }
1810 }
1811
1812 int wxListMainWindow::GetItemCount( void )
1813 {
1814 return m_lines.Number();
1815 }
1816
1817 void wxListMainWindow::GetItemRect( long index, wxRectangle &rect )
1818 {
1819 wxNode *node = m_lines.Nth( index );
1820 if (node)
1821 {
1822 wxListLineData *line = (wxListLineData*)node->Data();
1823 line->GetRect( rect );
1824 }
1825 else
1826 {
1827 rect.x = 0;
1828 rect.y = 0;
1829 rect.width = 0;
1830 rect.height = 0;
1831 }
1832 }
1833
1834 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos)
1835 {
1836 wxNode *node = m_lines.Nth( item );
1837 if (node)
1838 {
1839 wxRectangle rect;
1840 wxListLineData *line = (wxListLineData*)node->Data();
1841 line->GetRect( rect );
1842 pos.x = rect.x;
1843 pos.y = rect.y;
1844 }
1845 else
1846 {
1847 pos.x = 0;
1848 pos.y = 0;
1849 }
1850 return TRUE;
1851 }
1852
1853 int wxListMainWindow::GetSelectedItemCount( void )
1854 {
1855 int ret = 0;
1856 wxNode *node = m_lines.First();
1857 while (node)
1858 {
1859 wxListLineData *line = (wxListLineData*)node->Data();
1860 if (line->IsHilighted()) ret++;
1861 node = node->Next();
1862 }
1863 return ret;
1864 }
1865
1866 void wxListMainWindow::SetMode( long mode )
1867 {
1868 m_dirty = TRUE;
1869 m_mode = mode;
1870
1871 DeleteEverything();
1872
1873 if (m_mode & wxLC_REPORT)
1874 {
1875 m_xScroll = 0;
1876 m_yScroll = 15;
1877 }
1878 else
1879 {
1880 m_xScroll = 15;
1881 m_yScroll = 0;
1882 }
1883 }
1884
1885 long wxListMainWindow::GetMode( void ) const
1886 {
1887 return m_mode;
1888 }
1889
1890 void wxListMainWindow::CalculatePositions( void )
1891 {
1892 if (!m_lines.First()) return;
1893
1894 wxPaintDC dc( this );
1895 dc.SetFont( GetFont() );
1896
1897 int iconSpacing = 0;
1898 if (m_mode & wxLC_ICON) iconSpacing = m_normal_spacing;
1899 if (m_mode & wxLC_SMALL_ICON) iconSpacing = m_small_spacing;
1900
1901 // we take the first line (which also can be an icon or
1902 // an a text item in wxLC_ICON and wxLC_LIST modes) to
1903 // measure the size of the line
1904
1905 int lineWidth = 0;
1906 int lineHeight = 0;
1907 int lineSpacing = 0;
1908
1909 wxListLineData *line = (wxListLineData*)m_lines.First()->Data();
1910 line->CalculateSize( &dc, iconSpacing );
1911 int dummy = 0;
1912 line->GetSize( dummy, lineSpacing );
1913 lineSpacing += 4;
1914
1915 int clientWidth = 0;
1916 int clientHeight = 0;
1917
1918 if (m_mode & wxLC_REPORT)
1919 {
1920 int x = 4;
1921 int y = 1;
1922 int entireHeight = m_lines.Number() * lineSpacing + 2;
1923 int scroll_pos = GetScrollPos( wxVERTICAL );
1924 SetScrollbars( m_xScroll, m_yScroll, 0, (entireHeight+15) / m_yScroll, 0, scroll_pos, TRUE );
1925 GetClientSize( &clientWidth, &clientHeight );
1926
1927 wxNode* node = m_lines.First();
1928 while (node)
1929 {
1930 wxListLineData *line = (wxListLineData*)node->Data();
1931 line->CalculateSize( &dc, iconSpacing );
1932 line->SetPosition( &dc, x, y, clientWidth );
1933 int col_x = 2;
1934 for (int i = 0; i < GetColumnCount(); i++)
1935 {
1936 line->SetColumnPosition( i, col_x );
1937 col_x += GetColumnWidth( i );
1938 }
1939 y += lineSpacing; // one pixel blank line between items
1940 node = node->Next();
1941 }
1942 m_visibleLines = clientHeight / lineSpacing;
1943 }
1944 else
1945 {
1946 // at first we try without any scrollbar. if the items don't
1947 // fit into the window, we recalculate after subtracting an
1948 // approximated 15 pt for the horizontal scrollbar
1949
1950 GetSize( &clientWidth, &clientHeight );
1951 clientHeight -= 4; // sunken frame
1952
1953 int entireWidth = 0;
1954
1955 for (int tries = 0; tries < 2; tries++)
1956 {
1957 entireWidth = 0;
1958 int x = 5; // painting is done at x-2
1959 int y = 5; // painting is done at y-2
1960 int maxWidth = 0;
1961 wxNode *node = m_lines.First();
1962 while (node)
1963 {
1964 wxListLineData *line = (wxListLineData*)node->Data();
1965 line->CalculateSize( &dc, iconSpacing );
1966 line->SetPosition( &dc, x, y, clientWidth );
1967 line->GetSize( lineWidth, lineHeight );
1968 if (lineWidth > maxWidth) maxWidth = lineWidth;
1969 y += lineSpacing;
1970 if (y+lineSpacing-6 >= clientHeight) // -6 for earlier "line breaking"
1971 {
1972 y = 5;
1973 x += maxWidth+6;
1974 entireWidth += maxWidth+6;
1975 maxWidth = 0;
1976 }
1977 node = node->Next();
1978 if (!node) entireWidth += maxWidth;
1979 if ((tries == 0) && (entireWidth > clientWidth))
1980 {
1981 clientHeight -= 15; // scrollbar height
1982 break;
1983 }
1984 if (!node) tries = 1; // everything fits, no second try required
1985 }
1986 }
1987 m_visibleLines = (clientHeight+6) / (lineSpacing); // +6 for earlier "line breaking"
1988
1989 int scroll_pos = GetScrollPos( wxHORIZONTAL );
1990 SetScrollbars( m_xScroll, m_yScroll, (entireWidth+15) / m_xScroll, 0, scroll_pos, 0, TRUE );
1991 }
1992 }
1993
1994 void wxListMainWindow::RealizeChanges( void )
1995 {
1996 if (!m_current)
1997 {
1998 wxNode *node = m_lines.First();
1999 if (node) m_current = (wxListLineData*)node->Data();
2000 }
2001 if (m_current)
2002 {
2003 FocusLine( m_current );
2004 if (m_mode & wxLC_SINGLE_SEL) m_current->Hilight( TRUE );
2005 }
2006 }
2007
2008 long wxListMainWindow::GetNextItem( long item, int WXUNUSED(geometry), int state )
2009 {
2010 long ret = 0;
2011 if (item > 0) ret = item;
2012 wxNode *node = m_lines.Nth( ret );
2013 while (node)
2014 {
2015 wxListLineData *line = (wxListLineData*)node->Data();
2016 if ((state & wxLIST_STATE_FOCUSED) && (line == m_current)) return ret;
2017 if ((state & wxLIST_STATE_SELECTED) && (line->IsHilighted())) return ret;
2018 if (!state) return ret;
2019 ret++;
2020 node = node->Next();
2021 }
2022 return -1;
2023 }
2024
2025 void wxListMainWindow::DeleteItem( long index )
2026 {
2027 m_dirty = TRUE;
2028 wxNode *node = m_lines.Nth( index );
2029 if (node)
2030 {
2031 wxListLineData *line = (wxListLineData*)node->Data();
2032 if (m_current == line) m_current = (wxListLineData *) NULL;
2033 DeleteLine( line );
2034 m_lines.DeleteNode( node );
2035 }
2036 }
2037
2038 void wxListMainWindow::DeleteColumn( int col )
2039 {
2040 wxCHECK_RET( col < (int)m_columns.GetCount(),
2041 "attempting to delete inexistent column in wxListView" );
2042
2043 m_dirty = TRUE;
2044 wxNode *node = m_columns.Nth( col );
2045 if (node) m_columns.DeleteNode( node );
2046 }
2047
2048 void wxListMainWindow::DeleteAllItems( void )
2049 {
2050 m_dirty = TRUE;
2051 m_current = (wxListLineData *) NULL;
2052 wxNode *node = m_lines.First();
2053 while (node)
2054 {
2055 wxListLineData *line = (wxListLineData*)node->Data();
2056 DeleteLine( line );
2057 node = node->Next();
2058 }
2059 m_lines.Clear();
2060 }
2061
2062 void wxListMainWindow::DeleteEverything( void )
2063 {
2064 m_dirty = TRUE;
2065 m_current = (wxListLineData *) NULL;
2066 wxNode *node = m_lines.First();
2067 while (node)
2068 {
2069 wxListLineData *line = (wxListLineData*)node->Data();
2070 DeleteLine( line );
2071 node = node->Next();
2072 }
2073 m_lines.Clear();
2074 m_current = (wxListLineData *) NULL;
2075 m_columns.Clear();
2076 }
2077
2078 void wxListMainWindow::EnsureVisible( long index )
2079 {
2080 wxListLineData *oldCurrent = m_current;
2081 m_current = (wxListLineData *) NULL;
2082 int i = index;
2083 wxNode *node = m_lines.Nth( i );
2084 if (node) m_current = (wxListLineData*)node->Data();
2085 if (m_current) MoveToFocus();
2086 m_current = oldCurrent;
2087 }
2088
2089 long wxListMainWindow::FindItem(long start, const wxString& str, bool WXUNUSED(partial) )
2090 {
2091 long pos = start;
2092 wxString tmp = str;
2093 if (pos < 0) pos = 0;
2094 wxNode *node = m_lines.Nth( pos );
2095 while (node)
2096 {
2097 wxListLineData *line = (wxListLineData*)node->Data();
2098 wxString s = "";
2099 line->GetText( 0, s );
2100 if (s == tmp) return pos;
2101 node = node->Next();
2102 pos++;
2103 }
2104 return -1;
2105 }
2106
2107 long wxListMainWindow::FindItem(long start, long data)
2108 {
2109 long pos = start;
2110 if (pos < 0) pos = 0;
2111 wxNode *node = m_lines.Nth( pos );
2112 while (node)
2113 {
2114 wxListLineData *line = (wxListLineData*)node->Data();
2115 wxListItem item;
2116 line->GetItem( 0, item );
2117 if (item.m_data == data) return pos;
2118 node = node->Next();
2119 pos++;
2120 }
2121 return -1;
2122 }
2123
2124 long wxListMainWindow::HitTest( int x, int y, int &flags )
2125 {
2126 wxNode *node = m_lines.First();
2127 int count = 0;
2128 while (node)
2129 {
2130 wxListLineData *line = (wxListLineData*)node->Data();
2131 long ret = line->IsHit( x, y );
2132 if (ret & flags)
2133 {
2134 flags = ret;
2135 return count;
2136 }
2137 node = node->Next();
2138 count++;
2139 }
2140 return -1;
2141 }
2142
2143 void wxListMainWindow::InsertItem( wxListItem &item )
2144 {
2145 m_dirty = TRUE;
2146 int mode = 0;
2147 if (m_mode & wxLC_REPORT) mode = wxLC_REPORT;
2148 else if (m_mode & wxLC_LIST) mode = wxLC_LIST;
2149 else if (m_mode & wxLC_ICON) mode = wxLC_ICON;
2150 else if (m_mode & wxLC_SMALL_ICON) mode = wxLC_ICON; // no typo
2151 wxListLineData *line = new wxListLineData( this, mode, m_hilightBrush );
2152 if (m_mode & wxLC_REPORT)
2153 {
2154 line->InitItems( GetColumnCount() );
2155 item.m_width = GetColumnWidth( 0 )-3;
2156 }
2157 else
2158 line->InitItems( 1 );
2159 line->SetItem( 0, item );
2160 if ((item.m_itemId >= 0) && (item.m_itemId < (int)m_lines.GetCount()))
2161 {
2162 wxNode *node = m_lines.Nth( item.m_itemId );
2163 if (node) m_lines.Insert( node, line );
2164 }
2165 else
2166 {
2167 m_lines.Append( line );
2168 }
2169 }
2170
2171 void wxListMainWindow::InsertColumn( long col, wxListItem &item )
2172 {
2173 m_dirty = TRUE;
2174 if (m_mode & wxLC_REPORT)
2175 {
2176 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER) item.m_width = GetTextLength( item.m_text );
2177 wxListHeaderData *column = new wxListHeaderData( item );
2178 if ((col >= 0) && (col < (int)m_columns.GetCount()))
2179 {
2180 wxNode *node = m_columns.Nth( col );
2181 if (node)
2182 m_columns.Insert( node, column );
2183 }
2184 else
2185 {
2186 m_columns.Append( column );
2187 }
2188 }
2189 }
2190
2191 wxListCtrlCompare list_ctrl_compare_func_2;
2192 long list_ctrl_compare_data;
2193
2194 int list_ctrl_compare_func_1( const void *arg1, const void *arg2 )
2195 {
2196 wxListLineData *line1 = *((wxListLineData**)arg1);
2197 wxListLineData *line2 = *((wxListLineData**)arg2);
2198 wxListItem item;
2199 line1->GetItem( 0, item );
2200 long data1 = item.m_data;
2201 line2->GetItem( 0, item );
2202 long data2 = item.m_data;
2203 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
2204 }
2205
2206 void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data )
2207 {
2208 list_ctrl_compare_func_2 = fn;
2209 list_ctrl_compare_data = data;
2210 m_lines.Sort( list_ctrl_compare_func_1 );
2211 }
2212
2213 // -------------------------------------------------------------------------------------
2214 // wxListItem
2215 // -------------------------------------------------------------------------------------
2216
2217 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
2218
2219 wxListItem::wxListItem(void)
2220 {
2221 m_mask = 0;
2222 m_itemId = 0;
2223 m_col = 0;
2224 m_state = 0;
2225 m_stateMask = 0;
2226 m_image = 0;
2227 m_data = 0;
2228 m_format = wxLIST_FORMAT_CENTRE;
2229 m_width = 0;
2230 m_colour = wxBLACK;
2231 }
2232
2233 // -------------------------------------------------------------------------------------
2234 // wxListEvent
2235 // -------------------------------------------------------------------------------------
2236
2237 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
2238
2239 wxListEvent::wxListEvent( wxEventType commandType, int id ):
2240 wxNotifyEvent( commandType, id )
2241 {
2242 m_code = 0;
2243 m_itemIndex = 0;
2244 m_oldItemIndex = 0;
2245 m_col = 0;
2246 m_cancelled = FALSE;
2247 m_pointDrag.x = 0;
2248 m_pointDrag.y = 0;
2249 }
2250
2251 // -------------------------------------------------------------------------------------
2252 // wxListCtrl
2253 // -------------------------------------------------------------------------------------
2254
2255 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
2256
2257 BEGIN_EVENT_TABLE(wxListCtrl,wxControl)
2258 EVT_SIZE (wxListCtrl::OnSize)
2259 EVT_IDLE (wxListCtrl::OnIdle)
2260 END_EVENT_TABLE()
2261
2262 wxListCtrl::wxListCtrl(void)
2263 {
2264 m_imageListNormal = (wxImageList *) NULL;
2265 m_imageListSmall = (wxImageList *) NULL;
2266 m_imageListState = (wxImageList *) NULL;
2267 m_mainWin = (wxListMainWindow*) NULL;
2268 m_headerWin = (wxListHeaderWindow*) NULL;
2269 }
2270
2271 wxListCtrl::~wxListCtrl(void)
2272 {
2273 }
2274
2275 bool wxListCtrl::Create( wxWindow *parent, wxWindowID id,
2276 const wxPoint &pos, const wxSize &size,
2277 long style, const wxValidator &validator,
2278 const wxString &name )
2279 {
2280 m_imageListNormal = (wxImageList *) NULL;
2281 m_imageListSmall = (wxImageList *) NULL;
2282 m_imageListState = (wxImageList *) NULL;
2283 m_mainWin = (wxListMainWindow*) NULL;
2284 m_headerWin = (wxListHeaderWindow*) NULL;
2285
2286 long s = style;
2287
2288 if ((s & wxLC_REPORT == 0) &&
2289 (s & wxLC_LIST == 0) &&
2290 (s & wxLC_ICON == 0))
2291 s = s | wxLC_LIST;
2292
2293 bool ret = wxControl::Create( parent, id, pos, size, s, name );
2294
2295 SetValidator( validator );
2296
2297 if (s & wxSUNKEN_BORDER) s -= wxSUNKEN_BORDER;
2298
2299 m_mainWin = new wxListMainWindow( this, -1, wxPoint(0,0), size, s );
2300
2301 if (GetWindowStyleFlag() & wxLC_REPORT)
2302 m_headerWin = new wxListHeaderWindow( this, -1, m_mainWin, wxPoint(0,0), wxSize(size.x,23), wxTAB_TRAVERSAL );
2303 else
2304 m_headerWin = (wxListHeaderWindow *) NULL;
2305
2306 return ret;
2307 }
2308
2309 void wxListCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
2310 {
2311 // handled in OnIdle
2312
2313 if (m_mainWin) m_mainWin->m_dirty = TRUE;
2314 }
2315
2316 void wxListCtrl::SetSingleStyle( long style, bool add )
2317 {
2318 long flag = GetWindowStyleFlag();
2319
2320 if (add)
2321 {
2322 if (style & wxLC_MASK_TYPE) flag = flag & ~wxLC_MASK_TYPE;
2323 if (style & wxLC_MASK_ALIGN) flag = flag & ~wxLC_MASK_ALIGN;
2324 if (style & wxLC_MASK_SORT) flag = flag & ~wxLC_MASK_SORT;
2325 }
2326
2327 if (add)
2328 {
2329 flag |= style;
2330 }
2331 else
2332 {
2333 if (flag & style) flag -= style;
2334 }
2335
2336 SetWindowStyleFlag( flag );
2337 }
2338
2339 void wxListCtrl::SetWindowStyleFlag( long flag )
2340 {
2341 m_mainWin->DeleteEverything();
2342
2343 int width = 0;
2344 int height = 0;
2345 GetClientSize( &width, &height );
2346
2347 m_mainWin->SetMode( flag );
2348
2349 if (flag & wxLC_REPORT)
2350 {
2351 if (!(GetWindowStyleFlag() & wxLC_REPORT))
2352 {
2353 // m_mainWin->SetSize( 0, 24, width, height-24 );
2354 if (!m_headerWin)
2355 {
2356 m_headerWin = new wxListHeaderWindow( this, -1, m_mainWin, wxPoint(0,0), wxSize(width,23), wxTAB_TRAVERSAL );
2357 }
2358 else
2359 {
2360 // m_headerWin->SetSize( 0, 0, width, 23 );
2361 m_headerWin->Show( TRUE );
2362 }
2363 }
2364 }
2365 else
2366 {
2367 if (GetWindowStyleFlag() & wxLC_REPORT)
2368 {
2369 // m_mainWin->SetSize( 0, 0, width, height );
2370 m_headerWin->Show( FALSE );
2371 }
2372 }
2373
2374 wxWindow::SetWindowStyleFlag( flag );
2375 }
2376
2377 bool wxListCtrl::GetColumn(int col, wxListItem &item) const
2378 {
2379 m_mainWin->GetColumn( col, item );
2380 return TRUE;
2381 }
2382
2383 bool wxListCtrl::SetColumn( int col, wxListItem& item )
2384 {
2385 m_mainWin->SetColumn( col, item );
2386 return TRUE;
2387 }
2388
2389 int wxListCtrl::GetColumnWidth( int col ) const
2390 {
2391 return m_mainWin->GetColumnWidth( col );
2392 }
2393
2394 bool wxListCtrl::SetColumnWidth( int col, int width )
2395 {
2396 m_mainWin->SetColumnWidth( col, width );
2397 return TRUE;
2398 }
2399
2400 int wxListCtrl::GetCountPerPage(void) const
2401 {
2402 return m_mainWin->GetCountPerPage(); // different from Windows ?
2403 }
2404
2405 /*
2406 wxText& wxListCtrl::GetEditControl(void) const
2407 {
2408 }
2409 */
2410
2411 bool wxListCtrl::GetItem( wxListItem &info ) const
2412 {
2413 m_mainWin->GetItem( info );
2414 return TRUE;
2415 }
2416
2417 bool wxListCtrl::SetItem( wxListItem &info )
2418 {
2419 m_mainWin->SetItem( info );
2420 return TRUE;
2421 }
2422
2423 long wxListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
2424 {
2425 wxListItem info;
2426 info.m_text = label;
2427 info.m_mask = wxLIST_MASK_TEXT;
2428 info.m_itemId = index;
2429 info.m_col = col;
2430 if ( imageId > -1 )
2431 {
2432 info.m_image = imageId;
2433 info.m_mask |= wxLIST_MASK_IMAGE;
2434 }
2435 ;
2436 m_mainWin->SetItem(info);
2437 return TRUE;
2438 }
2439
2440 int wxListCtrl::GetItemState( long item, long stateMask ) const
2441 {
2442 return m_mainWin->GetItemState( item, stateMask );
2443 }
2444
2445 bool wxListCtrl::SetItemState( long item, long state, long stateMask )
2446 {
2447 m_mainWin->SetItemState( item, state, stateMask );
2448 return TRUE;
2449 }
2450
2451 bool wxListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
2452 {
2453 wxListItem info;
2454 info.m_image = image;
2455 info.m_mask = wxLIST_MASK_IMAGE;
2456 info.m_itemId = item;
2457 m_mainWin->SetItem( info );
2458 return TRUE;
2459 }
2460
2461 wxString wxListCtrl::GetItemText( long item ) const
2462 {
2463 wxListItem info;
2464 info.m_itemId = item;
2465 m_mainWin->GetItem( info );
2466 return info.m_text;
2467 }
2468
2469 void wxListCtrl::SetItemText( long item, const wxString &str )
2470 {
2471 wxListItem info;
2472 info.m_mask = wxLIST_MASK_TEXT;
2473 info.m_itemId = item;
2474 info.m_text = str;
2475 m_mainWin->SetItem( info );
2476 }
2477
2478 long wxListCtrl::GetItemData( long item ) const
2479 {
2480 wxListItem info;
2481 info.m_itemId = item;
2482 m_mainWin->GetItem( info );
2483 return info.m_data;
2484 }
2485
2486 bool wxListCtrl::SetItemData( long item, long data )
2487 {
2488 wxListItem info;
2489 info.m_mask = wxLIST_MASK_DATA;
2490 info.m_itemId = item;
2491 info.m_data = data;
2492 m_mainWin->SetItem( info );
2493 return TRUE;
2494 }
2495
2496 bool wxListCtrl::GetItemRect( long item, wxRectangle &rect, int WXUNUSED(code) ) const
2497 {
2498 m_mainWin->GetItemRect( item, rect );
2499 return TRUE;
2500 }
2501
2502 bool wxListCtrl::GetItemPosition( long item, wxPoint& pos ) const
2503 {
2504 m_mainWin->GetItemPosition( item, pos );
2505 return TRUE;
2506 }
2507
2508 bool wxListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
2509 {
2510 return 0;
2511 }
2512
2513 int wxListCtrl::GetItemCount(void) const
2514 {
2515 return m_mainWin->GetItemCount();
2516 }
2517
2518 int wxListCtrl::GetColumnCount(void) const
2519 {
2520 return m_mainWin->GetColumnCount();
2521 }
2522
2523 void wxListCtrl::SetItemSpacing( int spacing, bool isSmall )
2524 {
2525 m_mainWin->SetItemSpacing( spacing, isSmall );
2526 }
2527
2528 int wxListCtrl::GetItemSpacing( bool isSmall ) const
2529 {
2530 return m_mainWin->GetItemSpacing( isSmall );
2531 }
2532
2533 int wxListCtrl::GetSelectedItemCount(void) const
2534 {
2535 return m_mainWin->GetSelectedItemCount();
2536 }
2537
2538 /*
2539 wxColour wxListCtrl::GetTextColour(void) const
2540 {
2541 }
2542
2543 void wxListCtrl::SetTextColour(const wxColour& WXUNUSED(col))
2544 {
2545 }
2546 */
2547
2548 long wxListCtrl::GetTopItem(void) const
2549 {
2550 return 0;
2551 }
2552
2553 long wxListCtrl::GetNextItem( long item, int geom, int state ) const
2554 {
2555 return m_mainWin->GetNextItem( item, geom, state );
2556 }
2557
2558 wxImageList *wxListCtrl::GetImageList(int which) const
2559 {
2560 if (which == wxIMAGE_LIST_NORMAL)
2561 {
2562 return m_imageListNormal;
2563 }
2564 else if (which == wxIMAGE_LIST_SMALL)
2565 {
2566 return m_imageListSmall;
2567 }
2568 else if (which == wxIMAGE_LIST_STATE)
2569 {
2570 return m_imageListState;
2571 }
2572 return (wxImageList *) NULL;
2573 }
2574
2575 void wxListCtrl::SetImageList( wxImageList *imageList, int which )
2576 {
2577 m_mainWin->SetImageList( imageList, which );
2578 }
2579
2580 bool wxListCtrl::Arrange( int WXUNUSED(flag) )
2581 {
2582 return 0;
2583 }
2584
2585 bool wxListCtrl::DeleteItem( long item )
2586 {
2587 m_mainWin->DeleteItem( item );
2588 return TRUE;
2589 }
2590
2591 bool wxListCtrl::DeleteAllItems(void)
2592 {
2593 m_mainWin->DeleteAllItems();
2594 return TRUE;
2595 }
2596
2597 bool wxListCtrl::DeleteAllColumns()
2598 {
2599 for ( size_t n = 0; n < m_mainWin->m_columns.GetCount(); n++ )
2600 DeleteColumn(n);
2601
2602 return TRUE;
2603 }
2604
2605 void wxListCtrl::ClearAll()
2606 {
2607 m_mainWin->DeleteEverything();
2608 }
2609
2610 bool wxListCtrl::DeleteColumn( int col )
2611 {
2612 m_mainWin->DeleteColumn( col );
2613 return TRUE;
2614 }
2615
2616 /*
2617 wxText& wxListCtrl::Edit( long WXUNUSED(item ) )
2618 {
2619 }
2620 */
2621
2622 bool wxListCtrl::EnsureVisible( long item )
2623 {
2624 m_mainWin->EnsureVisible( item );
2625 return TRUE;
2626 }
2627
2628 long wxListCtrl::FindItem( long start, const wxString& str, bool partial )
2629 {
2630 return m_mainWin->FindItem( start, str, partial );
2631 }
2632
2633 long wxListCtrl::FindItem( long start, long data )
2634 {
2635 return m_mainWin->FindItem( start, data );
2636 }
2637
2638 long wxListCtrl::FindItem( long WXUNUSED(start), const wxPoint& WXUNUSED(pt),
2639 int WXUNUSED(direction))
2640 {
2641 return 0;
2642 }
2643
2644 long wxListCtrl::HitTest( const wxPoint &point, int &flags )
2645 {
2646 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
2647 }
2648
2649 long wxListCtrl::InsertItem( wxListItem& info )
2650 {
2651 m_mainWin->InsertItem( info );
2652 return 0;
2653 }
2654
2655 long wxListCtrl::InsertItem( long index, const wxString &label )
2656 {
2657 wxListItem info;
2658 info.m_text = label;
2659 info.m_mask = wxLIST_MASK_TEXT;
2660 info.m_itemId = index;
2661 return InsertItem( info );
2662 }
2663
2664 long wxListCtrl::InsertItem( long index, int imageIndex )
2665 {
2666 wxListItem info;
2667 info.m_mask = wxLIST_MASK_IMAGE;
2668 info.m_image = imageIndex;
2669 info.m_itemId = index;
2670 return InsertItem( info );
2671 }
2672
2673 long wxListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
2674 {
2675 wxListItem info;
2676 info.m_text = label;
2677 info.m_image = imageIndex;
2678 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
2679 info.m_itemId = index;
2680 return InsertItem( info );
2681 }
2682
2683 long wxListCtrl::InsertColumn( long col, wxListItem &item )
2684 {
2685 m_mainWin->InsertColumn( col, item );
2686 return 0;
2687 }
2688
2689 long wxListCtrl::InsertColumn( long col, const wxString &heading,
2690 int format, int width )
2691 {
2692 wxListItem item;
2693 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
2694 item.m_text = heading;
2695 if (width >= -2)
2696 {
2697 item.m_mask |= wxLIST_MASK_WIDTH;
2698 item.m_width = width;
2699 }
2700 ;
2701 item.m_format = format;
2702
2703 return InsertColumn( col, item );
2704 }
2705
2706 bool wxListCtrl::ScrollList( int WXUNUSED(dx), int WXUNUSED(dy) )
2707 {
2708 return 0;
2709 }
2710
2711 // Sort items.
2712 // fn is a function which takes 3 long arguments: item1, item2, data.
2713 // item1 is the long data associated with a first item (NOT the index).
2714 // item2 is the long data associated with a second item (NOT the index).
2715 // data is the same value as passed to SortItems.
2716 // The return value is a negative number if the first item should precede the second
2717 // item, a positive number of the second item should precede the first,
2718 // or zero if the two items are equivalent.
2719 // data is arbitrary data to be passed to the sort function.
2720
2721 bool wxListCtrl::SortItems( wxListCtrlCompare fn, long data )
2722 {
2723 m_mainWin->SortItems( fn, data );
2724 return TRUE;
2725 }
2726
2727 void wxListCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2728 {
2729 if (!m_mainWin->m_dirty) return;
2730
2731 int cw = 0;
2732 int ch = 0;
2733 GetClientSize( &cw, &ch );
2734
2735 int x = 0;
2736 int y = 0;
2737 int w = 0;
2738 int h = 0;
2739
2740 if (GetWindowStyleFlag() & wxLC_REPORT)
2741 {
2742 m_headerWin->GetPosition( &x, &y );
2743 m_headerWin->GetSize( &w, &h );
2744 if ((x != 0) || (y != 0) || (w != cw) || (h != 23))
2745 m_headerWin->SetSize( 0, 0, cw, 23 );
2746
2747 m_mainWin->GetPosition( &x, &y );
2748 m_mainWin->GetSize( &w, &h );
2749 if ((x != 0) || (y != 24) || (w != cw) || (h != ch-24))
2750 m_mainWin->SetSize( 0, 24, cw, ch-24 );
2751 }
2752 else
2753 {
2754 m_mainWin->GetPosition( &x, &y );
2755 m_mainWin->GetSize( &w, &h );
2756 if ((x != 0) || (y != 24) || (w != cw) || (h != ch))
2757 m_mainWin->SetSize( 0, 0, cw, ch );
2758 }
2759
2760 m_mainWin->CalculatePositions();
2761 m_mainWin->RealizeChanges();
2762 m_mainWin->m_dirty = FALSE;
2763 m_mainWin->Refresh();
2764 }
2765
2766 void wxListCtrl::SetBackgroundColour( const wxColour &colour )
2767 {
2768 if (m_mainWin)
2769 {
2770 m_mainWin->SetBackgroundColour( colour );
2771 m_mainWin->m_dirty = TRUE;
2772 }
2773 if (m_headerWin)
2774 m_headerWin->SetBackgroundColour( colour );
2775 }
2776
2777 void wxListCtrl::SetForegroundColour( const wxColour &colour )
2778 {
2779 if (m_mainWin)
2780 {
2781 m_mainWin->SetForegroundColour( colour );
2782 m_mainWin->m_dirty = TRUE;
2783 }
2784 if (m_headerWin)
2785 m_headerWin->SetForegroundColour( colour );
2786 }
2787
2788 void wxListCtrl::SetFont( const wxFont &font )
2789 {
2790 if (m_mainWin)
2791 {
2792 m_mainWin->SetFont( font );
2793 m_mainWin->m_dirty = TRUE;
2794 }
2795 if (m_headerWin)
2796 m_headerWin->SetFont( font );
2797 }
2798