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