]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listctrl.cpp
3d271e0111f2a93856bdde059766674ff00c733f
[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/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_isDragging = 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_isDragging = FALSE;
701 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE ) );
702 }
703
704 wxListHeaderWindow::~wxListHeaderWindow( void )
705 {
706 delete m_resizeCursor;
707 }
708
709 void wxListHeaderWindow::DoDrawRect( wxDC *dc, int x, int y, int w, int h )
710 {
711 const int m_corner = 1;
712
713 dc->SetBrush( *wxTRANSPARENT_BRUSH );
714
715 dc->SetPen( *wxBLACK_PEN );
716 dc->DrawLine( x+w-m_corner+1, y, x+w, y+h ); // right (outer)
717 dc->DrawRectangle( x, y+h, w+1, 1 ); // bottom (outer)
718
719 wxPen pen( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNSHADOW ), 1, wxSOLID );
720
721 dc->SetPen( pen );
722 dc->DrawLine( x+w-m_corner, y, x+w-1, y+h ); // right (inner)
723 dc->DrawRectangle( x+1, y+h-1, w-2, 1 ); // bottom (inner)
724
725 dc->SetPen( *wxWHITE_PEN );
726 dc->DrawRectangle( x, y, w-m_corner+1, 1 ); // top (outer)
727 dc->DrawRectangle( x, y, 1, h ); // left (outer)
728 dc->DrawLine( x, y+h-1, x+1, y+h-1 );
729 dc->DrawLine( x+w-1, y, x+w-1, y+1 );
730 }
731
732 void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
733 {
734 wxPaintDC dc( this );
735 PrepareDC( dc );
736
737 dc.BeginDrawing();
738
739 dc.SetFont( GetFont() );
740
741 int w = 0;
742 int h = 0;
743 int x = 0;
744 int y = 0;
745 GetClientSize( &w, &h );
746
747 dc.SetBackgroundMode(wxTRANSPARENT);
748 dc.SetTextForeground( *wxBLACK );
749 if (m_foregroundColour.Ok()) dc.SetTextForeground( m_foregroundColour );
750
751 x = 1;
752 y = 1;
753 int numColumns = m_owner->GetColumnCount();
754 wxListItem item;
755 for (int i = 0; i < numColumns; i++)
756 {
757 m_owner->GetColumn( i, item );
758 int cw = item.m_width-2;
759 if ((i+1 == numColumns) || (x+item.m_width > w-5)) cw = w-x-1;
760 dc.SetPen( *wxWHITE_PEN );
761
762 DoDrawRect( &dc, x, y, cw, h-2 );
763 dc.SetClippingRegion( x, y, cw-5, h-4 );
764 dc.DrawText( item.m_text, x+4, y+3 );
765 dc.DestroyClippingRegion();
766 x += item.m_width;
767 if (x > w+5) break;
768 }
769 dc.EndDrawing();
770 }
771
772 void wxListHeaderWindow::DrawCurrent()
773 {
774 int x1 = m_currentX;
775 int y1 = 0;
776 int x2 = m_currentX-1;
777 int y2 = 0;
778 int dummy;
779 m_owner->GetClientSize( &dummy, &y2 );
780 ClientToScreen( &x1, &y1 );
781 m_owner->ClientToScreen( &x2, &y2 );
782
783 wxScreenDC dc;
784 dc.SetLogicalFunction( wxINVERT );
785 dc.SetPen( wxPen( *wxBLACK, 2, wxSOLID ) );
786 dc.SetBrush( *wxTRANSPARENT_BRUSH );
787
788 dc.DrawLine( x1, y1, x2, y2 );
789
790 dc.SetLogicalFunction( wxCOPY );
791
792 dc.SetPen( wxNullPen );
793 dc.SetBrush( wxNullBrush );
794 }
795
796 void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
797 {
798 int x = event.GetX();
799 int y = event.GetY();
800 if (m_isDragging)
801 {
802 DrawCurrent();
803 if (event.ButtonUp())
804 {
805 ReleaseMouse();
806 m_isDragging = FALSE;
807 m_owner->SetColumnWidth( m_column, m_currentX-m_minX );
808 }
809 else
810 {
811 int size_x = 0;
812 int dummy;
813 GetClientSize( &size_x, & dummy );
814 if (x > m_minX+7)
815 m_currentX = x;
816 else
817 m_currentX = m_minX+7;
818 if (m_currentX > size_x-7) m_currentX = size_x-7;
819 DrawCurrent();
820 }
821 return;
822 }
823
824 m_minX = 0;
825 bool hit_border = FALSE;
826 int xpos = 0;
827 for (int j = 0; j < m_owner->GetColumnCount(); j++)
828 {
829 xpos += m_owner->GetColumnWidth( j );
830 if ((abs(x-xpos) < 3) && (y < 22))
831 {
832 hit_border = TRUE;
833 m_column = j;
834 break;
835 }
836 m_minX = xpos;
837 }
838
839 if (event.LeftDown() && hit_border)
840 {
841 m_isDragging = TRUE;
842 m_currentX = x;
843 DrawCurrent();
844 CaptureMouse();
845 return;
846 }
847
848 if (event.Moving())
849 {
850 if (hit_border)
851 {
852 if (m_currentCursor == wxSTANDARD_CURSOR) SetCursor( * m_resizeCursor );
853 m_currentCursor = m_resizeCursor;
854 }
855 else
856 {
857 if (m_currentCursor != wxSTANDARD_CURSOR) SetCursor( * wxSTANDARD_CURSOR );
858 m_currentCursor = wxSTANDARD_CURSOR;
859 }
860 }
861 }
862
863 void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
864 {
865 m_owner->SetFocus();
866 }
867
868 //-----------------------------------------------------------------------------
869 // wxListRenameTimer (internal)
870 //-----------------------------------------------------------------------------
871
872 wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
873 {
874 m_owner = owner;
875 }
876
877 void wxListRenameTimer::Notify()
878 {
879 m_owner->OnRenameTimer();
880 }
881
882 //-----------------------------------------------------------------------------
883 // wxListTextCtrl (internal)
884 //-----------------------------------------------------------------------------
885
886 IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl,wxTextCtrl);
887
888 BEGIN_EVENT_TABLE(wxListTextCtrl,wxTextCtrl)
889 EVT_CHAR (wxListTextCtrl::OnChar)
890 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus)
891 END_EVENT_TABLE()
892
893 wxListTextCtrl::wxListTextCtrl( wxWindow *parent, const wxWindowID id,
894 bool *accept, wxString *res, wxListMainWindow *owner,
895 const wxString &value, const wxPoint &pos, const wxSize &size,
896 int style, const wxValidator& validator, const wxString &name ) :
897 wxTextCtrl( parent, id, value, pos, size, style, validator, name )
898 {
899 m_res = res;
900 m_accept = accept;
901 m_owner = owner;
902 (*m_accept) = FALSE;
903 (*m_res) = "";
904 m_startValue = value;
905 }
906
907 void wxListTextCtrl::OnChar( wxKeyEvent &event )
908 {
909 if (event.m_keyCode == WXK_RETURN)
910 {
911 (*m_accept) = TRUE;
912 (*m_res) = GetValue();
913 m_owner->SetFocus();
914 return;
915 }
916 if (event.m_keyCode == WXK_ESCAPE)
917 {
918 (*m_accept) = FALSE;
919 (*m_res) = "";
920 m_owner->SetFocus();
921 return;
922 }
923 event.Skip();
924 }
925
926 void wxListTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
927 {
928 if (wxPendingDelete.Member(this)) return;
929
930 wxPendingDelete.Append(this);
931
932 if ((*m_accept) && ((*m_res) != m_startValue))
933 m_owner->OnRenameAccept();
934 }
935
936 //-----------------------------------------------------------------------------
937 // wxListMainWindow
938 //-----------------------------------------------------------------------------
939
940 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow,wxScrolledWindow);
941
942 BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledWindow)
943 EVT_PAINT (wxListMainWindow::OnPaint)
944 EVT_SIZE (wxListMainWindow::OnSize)
945 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
946 EVT_CHAR (wxListMainWindow::OnChar)
947 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
948 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
949 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
950 END_EVENT_TABLE()
951
952 wxListMainWindow::wxListMainWindow( void )
953 {
954 m_mode = 0;
955 m_lines.DeleteContents( TRUE );
956 m_columns.DeleteContents( TRUE );
957 m_current = (wxListLineData *) NULL;
958 m_visibleLines = 0;
959 m_hilightBrush = (wxBrush *) NULL;
960 m_xScroll = 0;
961 m_yScroll = 0;
962 m_dirty = TRUE;
963 m_small_image_list = (wxImageList *) NULL;
964 m_normal_image_list = (wxImageList *) NULL;
965 m_small_spacing = 30;
966 m_normal_spacing = 40;
967 m_hasFocus = FALSE;
968 m_usedKeys = TRUE;
969 m_lastOnSame = FALSE;
970 m_renameTimer = new wxListRenameTimer( this );
971 m_isCreated = FALSE;
972 m_dragCount = 0;
973 }
974
975 wxListMainWindow::wxListMainWindow( wxWindow *parent, wxWindowID id,
976 const wxPoint &pos, const wxSize &size,
977 long style, const wxString &name ) :
978 wxScrolledWindow( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name )
979 {
980 m_mode = style;
981 m_lines.DeleteContents( TRUE );
982 m_columns.DeleteContents( TRUE );
983 m_current = (wxListLineData *) NULL;
984 m_dirty = TRUE;
985 m_visibleLines = 0;
986 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
987 m_small_image_list = (wxImageList *) NULL;
988 m_normal_image_list = (wxImageList *) NULL;
989 m_small_spacing = 30;
990 m_normal_spacing = 40;
991 m_hasFocus = FALSE;
992 m_dragCount = 0;
993 m_isCreated = FALSE;
994 wxSize sz = size;
995 sz.y = 25;
996
997 if (m_mode & wxLC_REPORT)
998 {
999 m_xScroll = 0;
1000 m_yScroll = 15;
1001 }
1002 else
1003 {
1004 m_xScroll = 15;
1005 m_yScroll = 0;
1006 }
1007 SetScrollbars( m_xScroll, m_yScroll, 0, 0, 0, 0 );
1008
1009 m_usedKeys = TRUE;
1010 m_lastOnSame = FALSE;
1011 m_renameTimer = new wxListRenameTimer( this );
1012 m_renameAccept = FALSE;
1013
1014 SetBackgroundColour( *wxWHITE );
1015 }
1016
1017 wxListMainWindow::~wxListMainWindow( void )
1018 {
1019 if (m_hilightBrush) delete m_hilightBrush;
1020
1021 delete m_renameTimer;
1022 }
1023
1024 void wxListMainWindow::RefreshLine( wxListLineData *line )
1025 {
1026 int x = 0;
1027 int y = 0;
1028 int w = 0;
1029 int h = 0;
1030 if (line)
1031 {
1032 wxClientDC dc(this);
1033 PrepareDC( dc );
1034 line->GetExtent( x, y, w, h );
1035 wxRect rect(
1036 dc.LogicalToDeviceX(x-3),
1037 dc.LogicalToDeviceY(y-3),
1038 dc.LogicalToDeviceXRel(w+6),
1039 dc.LogicalToDeviceXRel(h+6) );
1040 Refresh( TRUE, &rect );
1041 }
1042 }
1043
1044 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1045 {
1046 // Note: a wxPaintDC must be constructed even if no drawing is
1047 // done (a Windows requirement).
1048 wxPaintDC dc( this );
1049 PrepareDC( dc );
1050
1051 if (m_dirty) return;
1052
1053 if (m_lines.GetCount() == 0) return;
1054
1055 dc.BeginDrawing();
1056
1057 dc.SetFont( GetFont() );
1058
1059 if (m_mode & wxLC_REPORT)
1060 {
1061 int lineSpacing = 0;
1062 wxListLineData *line = (wxListLineData*)m_lines.First()->Data();
1063 int dummy = 0;
1064 line->GetSize( dummy, lineSpacing );
1065 lineSpacing += 1;
1066
1067 int y_s = m_yScroll*GetScrollPos( wxVERTICAL );
1068
1069 wxNode *node = m_lines.Nth( y_s / lineSpacing );
1070 for (int i = 0; i < m_visibleLines+2; i++)
1071 {
1072 if (!node) break;
1073
1074 line = (wxListLineData*)node->Data();
1075 line->Draw( &dc );
1076 node = node->Next();
1077 }
1078 }
1079 else
1080 {
1081 wxNode *node = m_lines.First();
1082 while (node)
1083 {
1084 wxListLineData *line = (wxListLineData*)node->Data();
1085 line->Draw( &dc );
1086 node = node->Next();
1087 }
1088 }
1089
1090 if (m_current) m_current->DrawRubberBand( &dc, m_hasFocus );
1091
1092 dc.EndDrawing();
1093 }
1094
1095 void wxListMainWindow::HilightAll( bool on )
1096 {
1097 wxNode *node = m_lines.First();
1098 while (node)
1099 {
1100 wxListLineData *line = (wxListLineData *)node->Data();
1101 if (line->IsHilighted() != on)
1102 {
1103 line->Hilight( on );
1104 RefreshLine( line );
1105 }
1106 node = node->Next();
1107 }
1108 }
1109
1110 void wxListMainWindow::SendNotify( wxListLineData *line, wxEventType command )
1111 {
1112 wxListEvent le( command, GetParent()->GetId() );
1113 le.SetEventObject( GetParent() );
1114 le.m_itemIndex = GetIndexOfLine( line );
1115 line->GetItem( 0, le.m_item );
1116 GetParent()->GetEventHandler()->ProcessEvent( le );
1117 }
1118
1119 void wxListMainWindow::FocusLine( wxListLineData *WXUNUSED(line) )
1120 {
1121 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_FOCUSSED );
1122 }
1123
1124 void wxListMainWindow::UnfocusLine( wxListLineData *WXUNUSED(line) )
1125 {
1126 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_UNFOCUSSED );
1127 }
1128
1129 void wxListMainWindow::SelectLine( wxListLineData *line )
1130 {
1131 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_SELECTED );
1132 }
1133
1134 void wxListMainWindow::DeselectLine( wxListLineData *line )
1135 {
1136 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_DESELECTED );
1137 }
1138
1139 void wxListMainWindow::DeleteLine( wxListLineData *line )
1140 {
1141 SendNotify( line, wxEVT_COMMAND_LIST_DELETE_ITEM );
1142 }
1143
1144 /* *** */
1145
1146 void wxListMainWindow::EditLabel( long item )
1147 {
1148 wxNode *node = m_lines.Nth( item );
1149 wxCHECK_RET( node, _T("wrong index in wxListCtrl::Edit()") );
1150
1151 m_currentEdit = (wxListLineData*) node->Data();
1152
1153 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
1154 le.SetEventObject( GetParent() );
1155 le.m_itemIndex = GetIndexOfLine( m_currentEdit );
1156 m_currentEdit->GetItem( 0, le.m_item );
1157 GetParent()->GetEventHandler()->ProcessEvent( le );
1158
1159 if (!le.IsAllowed())
1160 return;
1161
1162 // We have to call this here because the label in
1163 // question might just have been added and no screen
1164 // update taken place.
1165 if (m_dirty) wxYield();
1166
1167 wxString s;
1168 m_currentEdit->GetText( 0, s );
1169 int x = 0;
1170 int y = 0;
1171 int w = 0;
1172 int h = 0;
1173 m_currentEdit->GetLabelExtent( x, y, w, h );
1174
1175 wxClientDC dc(this);
1176 PrepareDC( dc );
1177 x = dc.LogicalToDeviceX( x );
1178 y = dc.LogicalToDeviceY( y );
1179
1180 wxListTextCtrl *text = new wxListTextCtrl(
1181 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
1182 text->SetFocus();
1183 }
1184
1185 void wxListMainWindow::OnRenameTimer()
1186 {
1187 wxCHECK_RET( m_current, _T("invalid m_current") );
1188
1189 Edit( m_lines.IndexOf( m_current ) );
1190 }
1191
1192 void wxListMainWindow::OnRenameAccept()
1193 {
1194 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
1195 le.SetEventObject( GetParent() );
1196 le.m_itemIndex = GetIndexOfLine( m_currentEdit );
1197 m_currentEdit->GetItem( 0, le.m_item );
1198 le.m_item.m_text = m_renameRes;
1199 GetParent()->GetEventHandler()->ProcessEvent( le );
1200
1201 if (!le.IsAllowed()) return;
1202
1203 wxListItem info;
1204 info.m_mask = wxLIST_MASK_TEXT;
1205 info.m_itemId = le.m_itemIndex;
1206 info.m_text = m_renameRes;
1207 SetItem( info );
1208 }
1209
1210 void wxListMainWindow::OnMouse( wxMouseEvent &event )
1211 {
1212 if (GetParent()->GetEventHandler()->ProcessEvent( event)) return;
1213
1214 if (!m_current) return;
1215 if (m_dirty) return;
1216
1217 wxClientDC dc(this);
1218 PrepareDC(dc);
1219 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1220 long y = dc.DeviceToLogicalY( (long)event.GetY() );
1221
1222 /* Did we actually hit an item ? */
1223 long hitResult = 0;
1224 wxNode *node = m_lines.First();
1225 wxListLineData *line = (wxListLineData *) NULL;
1226 while (node)
1227 {
1228 line = (wxListLineData*)node->Data();
1229 hitResult = line->IsHit( x, y );
1230 if (hitResult) break;
1231 line = (wxListLineData *) NULL;
1232 node = node->Next();
1233 }
1234
1235 if (!event.Dragging())
1236 m_dragCount = 0;
1237 else
1238 m_dragCount++;
1239
1240 if (event.Dragging() && (m_dragCount > 3))
1241 {
1242 m_dragCount = 0;
1243
1244 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_DRAG, GetParent()->GetId() );
1245 le.SetEventObject( GetParent() );
1246 le.m_pointDrag.x = x;
1247 le.m_pointDrag.y = y;
1248 GetParent()->GetEventHandler()->ProcessEvent( le );
1249
1250 return;
1251 }
1252
1253 if (!line) return;
1254
1255 if (event.ButtonDClick())
1256 {
1257 m_usedKeys = FALSE;
1258 m_lastOnSame = FALSE;
1259 m_renameTimer->Stop();
1260
1261 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
1262
1263 return;
1264 }
1265
1266 if (event.LeftUp() && m_lastOnSame)
1267 {
1268 m_usedKeys = FALSE;
1269 if ((line == m_current) &&
1270 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
1271 (m_mode & wxLC_EDIT_LABELS) )
1272 {
1273 m_renameTimer->Start( 100, TRUE );
1274 }
1275 m_lastOnSame = FALSE;
1276 return;
1277 }
1278
1279 if (event.RightDown())
1280 {
1281 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK );
1282 return;
1283 }
1284
1285 if (event.MiddleDown())
1286 {
1287 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
1288 return;
1289 }
1290
1291 if (event.LeftDown())
1292 {
1293 m_usedKeys = FALSE;
1294 wxListLineData *oldCurrent = m_current;
1295 if (m_mode & wxLC_SINGLE_SEL)
1296 {
1297 m_current = line;
1298 HilightAll( FALSE );
1299 m_current->ReverseHilight();
1300 RefreshLine( m_current );
1301 }
1302 else
1303 {
1304 if (event.ShiftDown())
1305 {
1306 m_current = line;
1307 m_current->ReverseHilight();
1308 RefreshLine( m_current );
1309 }
1310 else if (event.ControlDown())
1311 {
1312 m_current = line;
1313
1314 int numOfCurrent = -1;
1315 node = m_lines.First();
1316 while (node)
1317 {
1318 wxListLineData *test_line = (wxListLineData*)node->Data();
1319 numOfCurrent++;
1320 if (test_line == oldCurrent) break;
1321 node = node->Next();
1322 }
1323
1324 int numOfLine = -1;
1325 node = m_lines.First();
1326 while (node)
1327 {
1328 wxListLineData *test_line = (wxListLineData*)node->Data();
1329 numOfLine++;
1330 if (test_line == line) break;
1331 node = node->Next();
1332 }
1333
1334 if (numOfLine < numOfCurrent)
1335 {
1336 int i = numOfLine;
1337 numOfLine = numOfCurrent;
1338 numOfCurrent = i;
1339 }
1340
1341 wxNode *node = m_lines.Nth( numOfCurrent );
1342 for (int i = 0; i <= numOfLine-numOfCurrent; i++)
1343 {
1344 wxListLineData *test_line= (wxListLineData*)node->Data();
1345 test_line->Hilight(TRUE);
1346 RefreshLine( test_line );
1347 node = node->Next();
1348 }
1349 }
1350 else
1351 {
1352 m_current = line;
1353 HilightAll( FALSE );
1354 m_current->ReverseHilight();
1355 RefreshLine( m_current );
1356 }
1357 }
1358 if (m_current != oldCurrent)
1359 {
1360 RefreshLine( oldCurrent );
1361 UnfocusLine( oldCurrent );
1362 FocusLine( m_current );
1363 }
1364 m_lastOnSame = (m_current == oldCurrent);
1365 return;
1366 }
1367 }
1368
1369 void wxListMainWindow::MoveToFocus()
1370 {
1371 if (!m_current) return;
1372
1373 int x = 0;
1374 int y = 0;
1375 int w = 0;
1376 int h = 0;
1377 m_current->GetExtent( x, y, w, h );
1378
1379 int w_p = 0;
1380 int h_p = 0;
1381 GetClientSize( &w_p, &h_p );
1382
1383 if (m_mode & wxLC_REPORT)
1384 {
1385 int y_s = m_yScroll*GetScrollPos( wxVERTICAL );
1386 if ((y > y_s) && (y+h < y_s+h_p)) return;
1387 if (y-y_s < 5) { Scroll( -1, (y-5-h_p/2)/m_yScroll ); Refresh(); }
1388 if (y+h+5 > y_s+h_p) { Scroll( -1, (y+h-h_p/2+h+15)/m_yScroll); Refresh(); }
1389 }
1390 else
1391 {
1392 int x_s = m_xScroll*GetScrollPos( wxHORIZONTAL );
1393 if ((x > x_s) && (x+w < x_s+w_p)) return;
1394 if (x-x_s < 5) { Scroll( (x-5)/m_xScroll, -1 ); Refresh(); }
1395 if (x+w-5 > x_s+w_p) { Scroll( (x+w-w_p+15)/m_xScroll, -1 ); Refresh(); }
1396 }
1397 }
1398
1399 void wxListMainWindow::OnArrowChar( wxListLineData *newCurrent, bool shiftDown )
1400 {
1401 if ((m_mode & wxLC_SINGLE_SEL) || (m_usedKeys == FALSE)) m_current->Hilight( FALSE );
1402 wxListLineData *oldCurrent = m_current;
1403 m_current = newCurrent;
1404 MoveToFocus();
1405 if (shiftDown || (m_mode & wxLC_SINGLE_SEL)) m_current->Hilight( TRUE );
1406 RefreshLine( m_current );
1407 RefreshLine( oldCurrent );
1408 FocusLine( m_current );
1409 UnfocusLine( oldCurrent );
1410 }
1411
1412 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
1413 {
1414 wxWindow *parent = GetParent();
1415
1416 /* we propagate the key event up */
1417 wxKeyEvent ke( wxEVT_KEY_DOWN );
1418 ke.m_shiftDown = event.m_shiftDown;
1419 ke.m_controlDown = event.m_controlDown;
1420 ke.m_altDown = event.m_altDown;
1421 ke.m_metaDown = event.m_metaDown;
1422 ke.m_keyCode = event.m_keyCode;
1423 ke.m_x = event.m_x;
1424 ke.m_y = event.m_y;
1425 ke.SetEventObject( parent );
1426 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
1427
1428 event.Skip();
1429 }
1430
1431 void wxListMainWindow::OnChar( wxKeyEvent &event )
1432 {
1433 wxWindow *parent = GetParent();
1434
1435 /* we send a list_key event up */
1436 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
1437 le.m_code = event.KeyCode();
1438 le.SetEventObject( parent );
1439 parent->GetEventHandler()->ProcessEvent( le );
1440
1441 /* we propagate the char event up */
1442 wxKeyEvent ke( wxEVT_CHAR );
1443 ke.m_shiftDown = event.m_shiftDown;
1444 ke.m_controlDown = event.m_controlDown;
1445 ke.m_altDown = event.m_altDown;
1446 ke.m_metaDown = event.m_metaDown;
1447 ke.m_keyCode = event.m_keyCode;
1448 ke.m_x = event.m_x;
1449 ke.m_y = event.m_y;
1450 ke.SetEventObject( parent );
1451 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
1452
1453 if (event.KeyCode() == WXK_TAB)
1454 {
1455 wxNavigationKeyEvent nevent;
1456 nevent.SetDirection( !event.ShiftDown() );
1457 nevent.SetCurrentFocus( m_parent );
1458 if (m_parent->GetEventHandler()->ProcessEvent( nevent )) return;
1459 }
1460
1461 /* no item -> nothing to do */
1462 if (!m_current)
1463 {
1464 event.Skip();
1465 return;
1466 }
1467
1468 switch (event.KeyCode())
1469 {
1470 case WXK_UP:
1471 {
1472 wxNode *node = m_lines.Member( m_current )->Previous();
1473 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1474 break;
1475 }
1476 case WXK_DOWN:
1477 {
1478 wxNode *node = m_lines.Member( m_current )->Next();
1479 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1480 break;
1481 }
1482 case WXK_END:
1483 {
1484 wxNode *node = m_lines.Last();
1485 OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1486 break;
1487 }
1488 case WXK_HOME:
1489 {
1490 wxNode *node = m_lines.First();
1491 OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1492 break;
1493 }
1494 case WXK_PRIOR:
1495 {
1496 int steps = 0;
1497 if (m_mode & wxLC_REPORT)
1498 {
1499 steps = m_visibleLines-1;
1500 }
1501 else
1502 {
1503 int pos = 0;
1504 wxNode *node = m_lines.First();
1505 for (;;) { if (m_current == (wxListLineData*)node->Data()) break; pos++; node = node->Next(); }
1506 steps = pos % m_visibleLines;
1507 }
1508 wxNode *node = m_lines.Member( m_current );
1509 for (int i = 0; i < steps; i++) if (node->Previous()) node = node->Previous();
1510 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1511 break;
1512 }
1513 case WXK_NEXT:
1514 {
1515 int steps = 0;
1516 if (m_mode & wxLC_REPORT)
1517 {
1518 steps = m_visibleLines-1;
1519 }
1520 else
1521 {
1522 int pos = 0; wxNode *node = m_lines.First();
1523 for (;;) { if (m_current == (wxListLineData*)node->Data()) break; pos++; node = node->Next(); }
1524 steps = m_visibleLines-(pos % m_visibleLines)-1;
1525 }
1526 wxNode *node = m_lines.Member( m_current );
1527 for (int i = 0; i < steps; i++) if (node->Next()) node = node->Next();
1528 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1529 break;
1530 }
1531 case WXK_LEFT:
1532 {
1533 if (!(m_mode & wxLC_REPORT))
1534 {
1535 wxNode *node = m_lines.Member( m_current );
1536 for (int i = 0; i <m_visibleLines; i++) if (node->Previous()) node = node->Previous();
1537 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1538 }
1539 break;
1540 }
1541 case WXK_RIGHT:
1542 {
1543 if (!(m_mode & wxLC_REPORT))
1544 {
1545 wxNode *node = m_lines.Member( m_current );
1546 for (int i = 0; i <m_visibleLines; i++) if (node->Next()) node = node->Next();
1547 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1548 }
1549 break;
1550 }
1551 case WXK_SPACE:
1552 {
1553 m_current->ReverseHilight();
1554 RefreshLine( m_current );
1555 break;
1556 }
1557 case WXK_INSERT:
1558 {
1559 if (!(m_mode & wxLC_SINGLE_SEL))
1560 {
1561 wxListLineData *oldCurrent = m_current;
1562 m_current->ReverseHilight();
1563 wxNode *node = m_lines.Member( m_current )->Next();
1564 if (node) m_current = (wxListLineData*)node->Data();
1565 MoveToFocus();
1566 RefreshLine( oldCurrent );
1567 RefreshLine( m_current );
1568 UnfocusLine( oldCurrent );
1569 FocusLine( m_current );
1570 }
1571 break;
1572 }
1573 case WXK_RETURN:
1574 case WXK_EXECUTE:
1575 {
1576 wxListEvent le( wxEVT_COMMAND_LIST_ITEM_ACTIVATED, GetParent()->GetId() );
1577 le.SetEventObject( GetParent() );
1578 le.m_itemIndex = GetIndexOfLine( m_current );
1579 m_current->GetItem( 0, le.m_item );
1580 GetParent()->GetEventHandler()->ProcessEvent( le );
1581 break;
1582 }
1583 default:
1584 {
1585 event.Skip();
1586 return;
1587 }
1588 }
1589 m_usedKeys = TRUE;
1590 }
1591
1592 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1593 {
1594 m_hasFocus = TRUE;
1595 RefreshLine( m_current );
1596
1597 if (!GetParent()) return;
1598
1599 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
1600 event.SetEventObject( GetParent() );
1601 GetParent()->GetEventHandler()->ProcessEvent( event );
1602 }
1603
1604 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1605 {
1606 m_hasFocus = FALSE;
1607 RefreshLine( m_current );
1608 }
1609
1610 void wxListMainWindow::OnSize( wxSizeEvent &WXUNUSED(event) )
1611 {
1612 /*
1613 We don't even allow the wxScrolledWindow::AdjustScrollbars() call
1614
1615 */
1616 }
1617
1618 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
1619 {
1620 if ((m_mode & wxLC_ICON) && (m_normal_image_list))
1621 {
1622 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1623 return;
1624 }
1625 if ((m_mode & wxLC_SMALL_ICON) && (m_small_image_list))
1626 {
1627 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1628 }
1629 if ((m_mode & wxLC_REPORT) && (m_small_image_list))
1630 {
1631 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1632 return;
1633 }
1634 }
1635
1636 void wxListMainWindow::GetImageSize( int index, int &width, int &height )
1637 {
1638 if ((m_mode & wxLC_ICON) && (m_normal_image_list))
1639 {
1640 m_normal_image_list->GetSize( index, width, height );
1641 return;
1642 }
1643 if ((m_mode & wxLC_SMALL_ICON) && (m_small_image_list))
1644 {
1645 m_small_image_list->GetSize( index, width, height );
1646 return;
1647 }
1648 if ((m_mode & wxLC_REPORT) && (m_small_image_list))
1649 {
1650 m_small_image_list->GetSize( index, width, height );
1651 return;
1652 }
1653 width = 0;
1654 height = 0;
1655 }
1656
1657 int wxListMainWindow::GetTextLength( wxString &s )
1658 {
1659 wxClientDC dc( this );
1660 long lw = 0;
1661 long lh = 0;
1662 dc.GetTextExtent( s, &lw, &lh );
1663 return lw + 6;
1664 }
1665
1666 int wxListMainWindow::GetIndexOfLine( const wxListLineData *line )
1667 {
1668 int i = 0;
1669 wxNode *node = m_lines.First();
1670 while (node)
1671 {
1672 if (line == (wxListLineData*)node->Data()) return i;
1673 i++;
1674 node = node->Next();
1675 }
1676 return -1;
1677 }
1678
1679 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
1680 {
1681 m_dirty = TRUE;
1682 if (which == wxIMAGE_LIST_NORMAL) m_normal_image_list = imageList;
1683 if (which == wxIMAGE_LIST_SMALL) m_small_image_list = imageList;
1684 }
1685
1686 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
1687 {
1688 m_dirty = TRUE;
1689 if (isSmall)
1690 {
1691 m_small_spacing = spacing;
1692 }
1693 else
1694 {
1695 m_normal_spacing = spacing;
1696 }
1697 }
1698
1699 int wxListMainWindow::GetItemSpacing( bool isSmall )
1700 {
1701 if (isSmall) return m_small_spacing; else return m_normal_spacing;
1702 }
1703
1704 void wxListMainWindow::SetColumn( int col, wxListItem &item )
1705 {
1706 m_dirty = TRUE;
1707 wxNode *node = m_columns.Nth( col );
1708 if (node)
1709 {
1710 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER) item.m_width = GetTextLength( item.m_text )+7;
1711 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1712 column->SetItem( item );
1713 }
1714 wxListCtrl *lc = (wxListCtrl*) GetParent();
1715 if (lc->m_headerWin) lc->m_headerWin->Refresh();
1716 }
1717
1718 void wxListMainWindow::SetColumnWidth( int col, int width )
1719 {
1720 if (!(m_mode & wxLC_REPORT)) return;
1721
1722 m_dirty = TRUE;
1723
1724 wxNode *node = (wxNode*) NULL;
1725
1726 if (width == wxLIST_AUTOSIZE_USEHEADER) width = 80;
1727 if (width == wxLIST_AUTOSIZE)
1728 {
1729 wxClientDC dc(this);
1730 dc.SetFont( GetFont() );
1731 int max = 10;
1732 node = m_lines.First();
1733 while (node)
1734 {
1735 wxListLineData *line = (wxListLineData*)node->Data();
1736 wxNode *n = line->m_items.Nth( col );
1737 if (n)
1738 {
1739 wxListItemData *item = (wxListItemData*)n->Data();
1740 int current = 0, ix = 0, iy = 0;
1741 long lx = 0, ly = 0;
1742 if (item->HasImage())
1743 {
1744 GetImageSize( item->GetImage(), ix, iy );
1745 current = ix + 5;
1746 }
1747 if (item->HasText())
1748 {
1749 wxString str;
1750 item->GetText( str );
1751 dc.GetTextExtent( str, &lx, &ly );
1752 current += lx;
1753 }
1754 if (current > max) max = current;
1755 }
1756 node = node->Next();
1757 }
1758 width = max+10;
1759 }
1760
1761 node = m_columns.Nth( col );
1762 if (node)
1763 {
1764 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1765 column->SetWidth( width );
1766 }
1767
1768 node = m_lines.First();
1769 while (node)
1770 {
1771 wxListLineData *line = (wxListLineData*)node->Data();
1772 wxNode *n = line->m_items.Nth( col );
1773 if (n)
1774 {
1775 wxListItemData *item = (wxListItemData*)n->Data();
1776 item->SetSize( width, -1 );
1777 }
1778 node = node->Next();
1779 }
1780
1781 wxListCtrl *lc = (wxListCtrl*) GetParent();
1782 if (lc->m_headerWin) lc->m_headerWin->Refresh();
1783 }
1784
1785 void wxListMainWindow::GetColumn( int col, wxListItem &item )
1786 {
1787 wxNode *node = m_columns.Nth( col );
1788 if (node)
1789 {
1790 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1791 column->GetItem( item );
1792 }
1793 else
1794 {
1795 item.m_format = 0;
1796 item.m_width = 0;
1797 item.m_text = "";
1798 item.m_image = 0;
1799 item.m_data = 0;
1800 }
1801 }
1802
1803 int wxListMainWindow::GetColumnWidth( int col )
1804 {
1805 wxNode *node = m_columns.Nth( col );
1806 if (node)
1807 {
1808 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1809 return column->GetWidth();
1810 }
1811 else
1812 {
1813 return 0;
1814 }
1815 }
1816
1817 int wxListMainWindow::GetColumnCount()
1818 {
1819 return m_columns.Number();
1820 }
1821
1822 int wxListMainWindow::GetCountPerPage()
1823 {
1824 return m_visibleLines;
1825 }
1826
1827 void wxListMainWindow::SetItem( wxListItem &item )
1828 {
1829 m_dirty = TRUE;
1830 wxNode *node = m_lines.Nth( item.m_itemId );
1831 if (node)
1832 {
1833 wxListLineData *line = (wxListLineData*)node->Data();
1834 if (m_mode & wxLC_REPORT) item.m_width = GetColumnWidth( item.m_col )-3;
1835 line->SetItem( item.m_col, item );
1836 }
1837 }
1838
1839 void wxListMainWindow::SetItemState( long item, long state, long stateMask )
1840 {
1841 // m_dirty = TRUE; no recalcs needed
1842
1843 wxListLineData *oldCurrent = m_current;
1844
1845 if (stateMask & wxLIST_STATE_FOCUSED)
1846 {
1847 wxNode *node = m_lines.Nth( item );
1848 if (node)
1849 {
1850 wxListLineData *line = (wxListLineData*)node->Data();
1851 UnfocusLine( m_current );
1852 m_current = line;
1853 FocusLine( m_current );
1854 RefreshLine( m_current );
1855 if (oldCurrent) RefreshLine( oldCurrent );
1856 }
1857 }
1858
1859 if (stateMask & wxLIST_STATE_SELECTED)
1860 {
1861 bool on = state & wxLIST_STATE_SELECTED;
1862 if (!on && (m_mode & wxLC_SINGLE_SEL)) return;
1863
1864 wxNode *node = m_lines.Nth( item );
1865 if (node)
1866 {
1867 wxListLineData *line = (wxListLineData*)node->Data();
1868 if (m_mode & wxLC_SINGLE_SEL)
1869 {
1870 UnfocusLine( m_current );
1871 m_current = line;
1872 FocusLine( m_current );
1873 if (oldCurrent) oldCurrent->Hilight( FALSE );
1874 RefreshLine( m_current );
1875 if (oldCurrent) RefreshLine( oldCurrent );
1876 }
1877 bool on = state & wxLIST_STATE_SELECTED;
1878 if (on != line->IsHilighted())
1879 {
1880 line->Hilight( on );
1881 RefreshLine( line );
1882 }
1883 }
1884 }
1885 }
1886
1887 int wxListMainWindow::GetItemState( long item, long stateMask )
1888 {
1889 int ret = wxLIST_STATE_DONTCARE;
1890 if (stateMask & wxLIST_STATE_FOCUSED)
1891 {
1892 wxNode *node = m_lines.Nth( item );
1893 if (node)
1894 {
1895 wxListLineData *line = (wxListLineData*)node->Data();
1896 if (line == m_current) ret |= wxLIST_STATE_FOCUSED;
1897 }
1898 }
1899 if (stateMask & wxLIST_STATE_SELECTED)
1900 {
1901 wxNode *node = m_lines.Nth( item );
1902 if (node)
1903 {
1904 wxListLineData *line = (wxListLineData*)node->Data();
1905 if (line->IsHilighted()) ret |= wxLIST_STATE_FOCUSED;
1906 }
1907 }
1908 return ret;
1909 }
1910
1911 void wxListMainWindow::GetItem( wxListItem &item )
1912 {
1913 wxNode *node = m_lines.Nth( item.m_itemId );
1914 if (node)
1915 {
1916 wxListLineData *line = (wxListLineData*)node->Data();
1917 line->GetItem( item.m_col, item );
1918 }
1919 else
1920 {
1921 item.m_mask = 0;
1922 item.m_text = "";
1923 item.m_image = 0;
1924 item.m_data = 0;
1925 }
1926 }
1927
1928 int wxListMainWindow::GetItemCount()
1929 {
1930 return m_lines.Number();
1931 }
1932
1933 void wxListMainWindow::GetItemRect( long index, wxRect &rect )
1934 {
1935 wxNode *node = m_lines.Nth( index );
1936 if (node)
1937 {
1938 wxListLineData *line = (wxListLineData*)node->Data();
1939 line->GetRect( rect );
1940 }
1941 else
1942 {
1943 rect.x = 0;
1944 rect.y = 0;
1945 rect.width = 0;
1946 rect.height = 0;
1947 }
1948 }
1949
1950 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos)
1951 {
1952 wxNode *node = m_lines.Nth( item );
1953 if (node)
1954 {
1955 wxRect rect;
1956 wxListLineData *line = (wxListLineData*)node->Data();
1957 line->GetRect( rect );
1958 pos.x = rect.x;
1959 pos.y = rect.y;
1960 }
1961 else
1962 {
1963 pos.x = 0;
1964 pos.y = 0;
1965 }
1966 return TRUE;
1967 }
1968
1969 int wxListMainWindow::GetSelectedItemCount()
1970 {
1971 int ret = 0;
1972 wxNode *node = m_lines.First();
1973 while (node)
1974 {
1975 wxListLineData *line = (wxListLineData*)node->Data();
1976 if (line->IsHilighted()) ret++;
1977 node = node->Next();
1978 }
1979 return ret;
1980 }
1981
1982 void wxListMainWindow::SetMode( long mode )
1983 {
1984 m_dirty = TRUE;
1985 m_mode = mode;
1986
1987 DeleteEverything();
1988
1989 if (m_mode & wxLC_REPORT)
1990 {
1991 m_xScroll = 0;
1992 m_yScroll = 15;
1993 }
1994 else
1995 {
1996 m_xScroll = 15;
1997 m_yScroll = 0;
1998 }
1999 }
2000
2001 long wxListMainWindow::GetMode() const
2002 {
2003 return m_mode;
2004 }
2005
2006 void wxListMainWindow::CalculatePositions()
2007 {
2008 if (!m_lines.First()) return;
2009
2010 wxClientDC dc( this );
2011 dc.SetFont( GetFont() );
2012
2013 int iconSpacing = 0;
2014 if (m_mode & wxLC_ICON) iconSpacing = m_normal_spacing;
2015 if (m_mode & wxLC_SMALL_ICON) iconSpacing = m_small_spacing;
2016
2017 // we take the first line (which also can be an icon or
2018 // an a text item in wxLC_ICON and wxLC_LIST modes) to
2019 // measure the size of the line
2020
2021 int lineWidth = 0;
2022 int lineHeight = 0;
2023 int lineSpacing = 0;
2024
2025 wxListLineData *line = (wxListLineData*)m_lines.First()->Data();
2026 line->CalculateSize( &dc, iconSpacing );
2027 int dummy = 0;
2028 line->GetSize( dummy, lineSpacing );
2029 lineSpacing += 4;
2030
2031 int clientWidth = 0;
2032 int clientHeight = 0;
2033
2034 if (m_mode & wxLC_REPORT)
2035 {
2036 int x = 4;
2037 int y = 1;
2038 int entireHeight = m_lines.Number() * lineSpacing + 2;
2039 int scroll_pos = GetScrollPos( wxVERTICAL );
2040 SetScrollbars( m_xScroll, m_yScroll, 0, (entireHeight+15) / m_yScroll, 0, scroll_pos, TRUE );
2041 GetClientSize( &clientWidth, &clientHeight );
2042
2043 wxNode* node = m_lines.First();
2044 while (node)
2045 {
2046 wxListLineData *line = (wxListLineData*)node->Data();
2047 line->CalculateSize( &dc, iconSpacing );
2048 line->SetPosition( &dc, x, y, clientWidth );
2049 int col_x = 2;
2050 for (int i = 0; i < GetColumnCount(); i++)
2051 {
2052 line->SetColumnPosition( i, col_x );
2053 col_x += GetColumnWidth( i );
2054 }
2055 y += lineSpacing; // one pixel blank line between items
2056 node = node->Next();
2057 }
2058 m_visibleLines = clientHeight / lineSpacing;
2059 }
2060 else
2061 {
2062 // at first we try without any scrollbar. if the items don't
2063 // fit into the window, we recalculate after subtracting an
2064 // approximated 15 pt for the horizontal scrollbar
2065
2066 GetSize( &clientWidth, &clientHeight );
2067 clientHeight -= 4; // sunken frame
2068
2069 int entireWidth = 0;
2070
2071 for (int tries = 0; tries < 2; tries++)
2072 {
2073 entireWidth = 0;
2074 int x = 5; // painting is done at x-2
2075 int y = 5; // painting is done at y-2
2076 int maxWidth = 0;
2077 wxNode *node = m_lines.First();
2078 while (node)
2079 {
2080 wxListLineData *line = (wxListLineData*)node->Data();
2081 line->CalculateSize( &dc, iconSpacing );
2082 line->SetPosition( &dc, x, y, clientWidth );
2083 line->GetSize( lineWidth, lineHeight );
2084 if (lineWidth > maxWidth) maxWidth = lineWidth;
2085 y += lineSpacing;
2086 if (y+lineSpacing-6 >= clientHeight) // -6 for earlier "line breaking"
2087 {
2088 y = 5;
2089 x += maxWidth+6;
2090 entireWidth += maxWidth+6;
2091 maxWidth = 0;
2092 }
2093 node = node->Next();
2094 if (!node) entireWidth += maxWidth;
2095 if ((tries == 0) && (entireWidth > clientWidth))
2096 {
2097 clientHeight -= 15; // scrollbar height
2098 break;
2099 }
2100 if (!node) tries = 1; // everything fits, no second try required
2101 }
2102 }
2103 m_visibleLines = (clientHeight+6) / (lineSpacing); // +6 for earlier "line breaking"
2104
2105 int scroll_pos = GetScrollPos( wxHORIZONTAL );
2106 SetScrollbars( m_xScroll, m_yScroll, (entireWidth+15) / m_xScroll, 0, scroll_pos, 0, TRUE );
2107 }
2108 }
2109
2110 void wxListMainWindow::RealizeChanges( void )
2111 {
2112 if (!m_current)
2113 {
2114 wxNode *node = m_lines.First();
2115 if (node) m_current = (wxListLineData*)node->Data();
2116 }
2117 if (m_current)
2118 {
2119 FocusLine( m_current );
2120 if (m_mode & wxLC_SINGLE_SEL) m_current->Hilight( TRUE );
2121 }
2122 }
2123
2124 long wxListMainWindow::GetNextItem( long item, int WXUNUSED(geometry), int state )
2125 {
2126 long ret = 0;
2127 if (item > 0) ret = item;
2128 if(ret >= GetItemCount()) return -1;
2129 wxNode *node = m_lines.Nth( ret );
2130 while (node)
2131 {
2132 wxListLineData *line = (wxListLineData*)node->Data();
2133 if ((state & wxLIST_STATE_FOCUSED) && (line == m_current)) return ret;
2134 if ((state & wxLIST_STATE_SELECTED) && (line->IsHilighted())) return ret;
2135 if (!state) return ret;
2136 ret++;
2137 node = node->Next();
2138 }
2139 return -1;
2140 }
2141
2142 void wxListMainWindow::DeleteItem( long index )
2143 {
2144 m_dirty = TRUE;
2145 wxNode *node = m_lines.Nth( index );
2146 if (node)
2147 {
2148 wxListLineData *line = (wxListLineData*)node->Data();
2149 if (m_current == line) m_current = (wxListLineData *) NULL;
2150 DeleteLine( line );
2151 m_lines.DeleteNode( node );
2152 }
2153 }
2154
2155 void wxListMainWindow::DeleteColumn( int col )
2156 {
2157 wxCHECK_RET( col < (int)m_columns.GetCount(),
2158 _T("attempting to delete inexistent column in wxListView") );
2159
2160 m_dirty = TRUE;
2161 wxNode *node = m_columns.Nth( col );
2162 if (node) m_columns.DeleteNode( node );
2163 }
2164
2165 void wxListMainWindow::DeleteAllItems( void )
2166 {
2167 m_dirty = TRUE;
2168 m_current = (wxListLineData *) NULL;
2169 wxNode *node = m_lines.First();
2170 while (node)
2171 {
2172 wxListLineData *line = (wxListLineData*)node->Data();
2173 DeleteLine( line );
2174 node = node->Next();
2175 }
2176 m_lines.Clear();
2177 }
2178
2179 void wxListMainWindow::DeleteEverything( void )
2180 {
2181 m_dirty = TRUE;
2182 m_current = (wxListLineData *) NULL;
2183 wxNode *node = m_lines.First();
2184 while (node)
2185 {
2186 wxListLineData *line = (wxListLineData*)node->Data();
2187 DeleteLine( line );
2188 node = node->Next();
2189 }
2190 m_lines.Clear();
2191 m_current = (wxListLineData *) NULL;
2192 m_columns.Clear();
2193 }
2194
2195 void wxListMainWindow::EnsureVisible( long index )
2196 {
2197 // We have to call this here because the label in
2198 // question might just have been added and no screen
2199 // update taken place.
2200 if (m_dirty) wxYield();
2201
2202 wxListLineData *oldCurrent = m_current;
2203 m_current = (wxListLineData *) NULL;
2204 int i = index;
2205 wxNode *node = m_lines.Nth( i );
2206 if (node) m_current = (wxListLineData*)node->Data();
2207 if (m_current) MoveToFocus();
2208 m_current = oldCurrent;
2209 }
2210
2211 long wxListMainWindow::FindItem(long start, const wxString& str, bool WXUNUSED(partial) )
2212 {
2213 long pos = start;
2214 wxString tmp = str;
2215 if (pos < 0) pos = 0;
2216 wxNode *node = m_lines.Nth( pos );
2217 while (node)
2218 {
2219 wxListLineData *line = (wxListLineData*)node->Data();
2220 wxString s = "";
2221 line->GetText( 0, s );
2222 if (s == tmp) return pos;
2223 node = node->Next();
2224 pos++;
2225 }
2226 return -1;
2227 }
2228
2229 long wxListMainWindow::FindItem(long start, long data)
2230 {
2231 long pos = start;
2232 if (pos < 0) pos = 0;
2233 wxNode *node = m_lines.Nth( pos );
2234 while (node)
2235 {
2236 wxListLineData *line = (wxListLineData*)node->Data();
2237 wxListItem item;
2238 line->GetItem( 0, item );
2239 if (item.m_data == data) return pos;
2240 node = node->Next();
2241 pos++;
2242 }
2243 return -1;
2244 }
2245
2246 long wxListMainWindow::HitTest( int x, int y, int &flags )
2247 {
2248 wxNode *node = m_lines.First();
2249 int count = 0;
2250 while (node)
2251 {
2252 wxListLineData *line = (wxListLineData*)node->Data();
2253 long ret = line->IsHit( x, y );
2254 if (ret & flags)
2255 {
2256 flags = ret;
2257 return count;
2258 }
2259 node = node->Next();
2260 count++;
2261 }
2262 return -1;
2263 }
2264
2265 void wxListMainWindow::InsertItem( wxListItem &item )
2266 {
2267 m_dirty = TRUE;
2268 int mode = 0;
2269 if (m_mode & wxLC_REPORT) mode = wxLC_REPORT;
2270 else if (m_mode & wxLC_LIST) mode = wxLC_LIST;
2271 else if (m_mode & wxLC_ICON) mode = wxLC_ICON;
2272 else if (m_mode & wxLC_SMALL_ICON) mode = wxLC_ICON; // no typo
2273
2274 wxListLineData *line = new wxListLineData( this, mode, m_hilightBrush );
2275
2276 if (m_mode & wxLC_REPORT)
2277 {
2278 line->InitItems( GetColumnCount() );
2279 item.m_width = GetColumnWidth( 0 )-3;
2280 }
2281 else
2282 {
2283 line->InitItems( 1 );
2284 }
2285
2286 line->SetItem( 0, item );
2287 if ((item.m_itemId >= 0) && (item.m_itemId < (int)m_lines.GetCount()))
2288 {
2289 wxNode *node = m_lines.Nth( item.m_itemId );
2290 if (node) m_lines.Insert( node, line );
2291 }
2292 else
2293 {
2294 m_lines.Append( line );
2295 }
2296 }
2297
2298 void wxListMainWindow::InsertColumn( long col, wxListItem &item )
2299 {
2300 m_dirty = TRUE;
2301 if (m_mode & wxLC_REPORT)
2302 {
2303 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER) item.m_width = GetTextLength( item.m_text );
2304 wxListHeaderData *column = new wxListHeaderData( item );
2305 if ((col >= 0) && (col < (int)m_columns.GetCount()))
2306 {
2307 wxNode *node = m_columns.Nth( col );
2308 if (node)
2309 m_columns.Insert( node, column );
2310 }
2311 else
2312 {
2313 m_columns.Append( column );
2314 }
2315 }
2316 }
2317
2318 wxListCtrlCompare list_ctrl_compare_func_2;
2319 long list_ctrl_compare_data;
2320
2321 int list_ctrl_compare_func_1( const void *arg1, const void *arg2 )
2322 {
2323 wxListLineData *line1 = *((wxListLineData**)arg1);
2324 wxListLineData *line2 = *((wxListLineData**)arg2);
2325 wxListItem item;
2326 line1->GetItem( 0, item );
2327 long data1 = item.m_data;
2328 line2->GetItem( 0, item );
2329 long data2 = item.m_data;
2330 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
2331 }
2332
2333 void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data )
2334 {
2335 list_ctrl_compare_func_2 = fn;
2336 list_ctrl_compare_data = data;
2337 m_lines.Sort( list_ctrl_compare_func_1 );
2338 }
2339
2340 // -------------------------------------------------------------------------------------
2341 // wxListItem
2342 // -------------------------------------------------------------------------------------
2343
2344 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
2345
2346 wxListItem::wxListItem(void)
2347 {
2348 m_mask = 0;
2349 m_itemId = 0;
2350 m_col = 0;
2351 m_state = 0;
2352 m_stateMask = 0;
2353 m_image = 0;
2354 m_data = 0;
2355 m_format = wxLIST_FORMAT_CENTRE;
2356 m_width = 0;
2357 m_colour = wxBLACK;
2358 }
2359
2360 // -------------------------------------------------------------------------------------
2361 // wxListEvent
2362 // -------------------------------------------------------------------------------------
2363
2364 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
2365
2366 wxListEvent::wxListEvent( wxEventType commandType, int id ):
2367 wxNotifyEvent( commandType, id )
2368 {
2369 m_code = 0;
2370 m_itemIndex = 0;
2371 m_oldItemIndex = 0;
2372 m_col = 0;
2373 m_cancelled = FALSE;
2374 m_pointDrag.x = 0;
2375 m_pointDrag.y = 0;
2376 }
2377
2378 // -------------------------------------------------------------------------------------
2379 // wxListCtrl
2380 // -------------------------------------------------------------------------------------
2381
2382 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
2383
2384 BEGIN_EVENT_TABLE(wxListCtrl,wxControl)
2385 EVT_SIZE (wxListCtrl::OnSize)
2386 EVT_IDLE (wxListCtrl::OnIdle)
2387 END_EVENT_TABLE()
2388
2389 wxListCtrl::wxListCtrl(void)
2390 {
2391 m_imageListNormal = (wxImageList *) NULL;
2392 m_imageListSmall = (wxImageList *) NULL;
2393 m_imageListState = (wxImageList *) NULL;
2394 m_mainWin = (wxListMainWindow*) NULL;
2395 m_headerWin = (wxListHeaderWindow*) NULL;
2396 }
2397
2398 wxListCtrl::~wxListCtrl(void)
2399 {
2400 }
2401
2402 bool wxListCtrl::Create( wxWindow *parent, wxWindowID id,
2403 const wxPoint &pos, const wxSize &size,
2404 long style, const wxValidator &validator,
2405 const wxString &name )
2406 {
2407 m_imageListNormal = (wxImageList *) NULL;
2408 m_imageListSmall = (wxImageList *) NULL;
2409 m_imageListState = (wxImageList *) NULL;
2410 m_mainWin = (wxListMainWindow*) NULL;
2411 m_headerWin = (wxListHeaderWindow*) NULL;
2412
2413 long s = style;
2414
2415 if ((s & wxLC_REPORT == 0) &&
2416 (s & wxLC_LIST == 0) &&
2417 (s & wxLC_ICON == 0))
2418 {
2419 s = s | wxLC_LIST;
2420 }
2421
2422 bool ret = wxControl::Create( parent, id, pos, size, s, name );
2423
2424 #if wxUSE_VALIDATORS
2425 SetValidator( validator );
2426 #endif
2427
2428 if (s & wxSUNKEN_BORDER) s -= wxSUNKEN_BORDER;
2429
2430 m_mainWin = new wxListMainWindow( this, -1, wxPoint(0,0), size, s );
2431
2432 if (HasFlag(wxLC_REPORT))
2433 m_headerWin = new wxListHeaderWindow( this, -1, m_mainWin, wxPoint(0,0), wxSize(size.x,23), wxTAB_TRAVERSAL );
2434 else
2435 m_headerWin = (wxListHeaderWindow *) NULL;
2436
2437 SetBackgroundColour( *wxWHITE );
2438
2439 return ret;
2440 }
2441
2442 void wxListCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
2443 {
2444 /* handled in OnIdle */
2445
2446 if (m_mainWin) m_mainWin->m_dirty = TRUE;
2447 }
2448
2449 void wxListCtrl::SetSingleStyle( long style, bool add )
2450 {
2451 long flag = GetWindowStyle();
2452
2453 if (add)
2454 {
2455 if (style & wxLC_MASK_TYPE) flag = flag & ~wxLC_MASK_TYPE;
2456 if (style & wxLC_MASK_ALIGN) flag = flag & ~wxLC_MASK_ALIGN;
2457 if (style & wxLC_MASK_SORT) flag = flag & ~wxLC_MASK_SORT;
2458 }
2459
2460 if (add)
2461 {
2462 flag |= style;
2463 }
2464 else
2465 {
2466 if (flag & style) flag -= style;
2467 }
2468
2469 SetWindowStyleFlag( flag );
2470 }
2471
2472 void wxListCtrl::SetWindowStyleFlag( long flag )
2473 {
2474 if (m_mainWin)
2475 {
2476 m_mainWin->DeleteEverything();
2477
2478 int width = 0;
2479 int height = 0;
2480 GetClientSize( &width, &height );
2481
2482 m_mainWin->SetMode( flag );
2483
2484 if (flag & wxLC_REPORT)
2485 {
2486 if (!HasFlag(wxLC_REPORT))
2487 {
2488 if (!m_headerWin)
2489 {
2490 m_headerWin = new wxListHeaderWindow( this, -1, m_mainWin,
2491 wxPoint(0,0), wxSize(width,23), wxTAB_TRAVERSAL );
2492 }
2493 else
2494 {
2495 m_headerWin->Show( TRUE );
2496 }
2497 }
2498 }
2499 else
2500 {
2501 if (HasFlag(wxLC_REPORT))
2502 {
2503 m_headerWin->Show( FALSE );
2504 }
2505 }
2506 }
2507
2508 wxWindow::SetWindowStyleFlag( flag );
2509 }
2510
2511 bool wxListCtrl::GetColumn(int col, wxListItem &item) const
2512 {
2513 m_mainWin->GetColumn( col, item );
2514 return TRUE;
2515 }
2516
2517 bool wxListCtrl::SetColumn( int col, wxListItem& item )
2518 {
2519 m_mainWin->SetColumn( col, item );
2520 return TRUE;
2521 }
2522
2523 int wxListCtrl::GetColumnWidth( int col ) const
2524 {
2525 return m_mainWin->GetColumnWidth( col );
2526 }
2527
2528 bool wxListCtrl::SetColumnWidth( int col, int width )
2529 {
2530 m_mainWin->SetColumnWidth( col, width );
2531 return TRUE;
2532 }
2533
2534 int wxListCtrl::GetCountPerPage(void) const
2535 {
2536 return m_mainWin->GetCountPerPage(); // different from Windows ?
2537 }
2538
2539 bool wxListCtrl::GetItem( wxListItem &info ) const
2540 {
2541 m_mainWin->GetItem( info );
2542 return TRUE;
2543 }
2544
2545 bool wxListCtrl::SetItem( wxListItem &info )
2546 {
2547 m_mainWin->SetItem( info );
2548 return TRUE;
2549 }
2550
2551 long wxListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
2552 {
2553 wxListItem info;
2554 info.m_text = label;
2555 info.m_mask = wxLIST_MASK_TEXT;
2556 info.m_itemId = index;
2557 info.m_col = col;
2558 if ( imageId > -1 )
2559 {
2560 info.m_image = imageId;
2561 info.m_mask |= wxLIST_MASK_IMAGE;
2562 };
2563 m_mainWin->SetItem(info);
2564 return TRUE;
2565 }
2566
2567 int wxListCtrl::GetItemState( long item, long stateMask ) const
2568 {
2569 return m_mainWin->GetItemState( item, stateMask );
2570 }
2571
2572 bool wxListCtrl::SetItemState( long item, long state, long stateMask )
2573 {
2574 m_mainWin->SetItemState( item, state, stateMask );
2575 return TRUE;
2576 }
2577
2578 bool wxListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
2579 {
2580 wxListItem info;
2581 info.m_image = image;
2582 info.m_mask = wxLIST_MASK_IMAGE;
2583 info.m_itemId = item;
2584 m_mainWin->SetItem( info );
2585 return TRUE;
2586 }
2587
2588 wxString wxListCtrl::GetItemText( long item ) const
2589 {
2590 wxListItem info;
2591 info.m_itemId = item;
2592 m_mainWin->GetItem( info );
2593 return info.m_text;
2594 }
2595
2596 void wxListCtrl::SetItemText( long item, const wxString &str )
2597 {
2598 wxListItem info;
2599 info.m_mask = wxLIST_MASK_TEXT;
2600 info.m_itemId = item;
2601 info.m_text = str;
2602 m_mainWin->SetItem( info );
2603 }
2604
2605 long wxListCtrl::GetItemData( long item ) const
2606 {
2607 wxListItem info;
2608 info.m_itemId = item;
2609 m_mainWin->GetItem( info );
2610 return info.m_data;
2611 }
2612
2613 bool wxListCtrl::SetItemData( long item, long data )
2614 {
2615 wxListItem info;
2616 info.m_mask = wxLIST_MASK_DATA;
2617 info.m_itemId = item;
2618 info.m_data = data;
2619 m_mainWin->SetItem( info );
2620 return TRUE;
2621 }
2622
2623 bool wxListCtrl::GetItemRect( long item, wxRect &rect, int WXUNUSED(code) ) const
2624 {
2625 m_mainWin->GetItemRect( item, rect );
2626 return TRUE;
2627 }
2628
2629 bool wxListCtrl::GetItemPosition( long item, wxPoint& pos ) const
2630 {
2631 m_mainWin->GetItemPosition( item, pos );
2632 return TRUE;
2633 }
2634
2635 bool wxListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
2636 {
2637 return 0;
2638 }
2639
2640 int wxListCtrl::GetItemCount(void) const
2641 {
2642 return m_mainWin->GetItemCount();
2643 }
2644
2645 int wxListCtrl::GetColumnCount(void) const
2646 {
2647 return m_mainWin->GetColumnCount();
2648 }
2649
2650 void wxListCtrl::SetItemSpacing( int spacing, bool isSmall )
2651 {
2652 m_mainWin->SetItemSpacing( spacing, isSmall );
2653 }
2654
2655 int wxListCtrl::GetItemSpacing( bool isSmall ) const
2656 {
2657 return m_mainWin->GetItemSpacing( isSmall );
2658 }
2659
2660 int wxListCtrl::GetSelectedItemCount(void) const
2661 {
2662 return m_mainWin->GetSelectedItemCount();
2663 }
2664
2665 /*
2666 wxColour wxListCtrl::GetTextColour(void) const
2667 {
2668 }
2669
2670 void wxListCtrl::SetTextColour(const wxColour& WXUNUSED(col))
2671 {
2672 }
2673 */
2674
2675 long wxListCtrl::GetTopItem(void) const
2676 {
2677 return 0;
2678 }
2679
2680 long wxListCtrl::GetNextItem( long item, int geom, int state ) const
2681 {
2682 return m_mainWin->GetNextItem( item, geom, state );
2683 }
2684
2685 wxImageList *wxListCtrl::GetImageList(int which) const
2686 {
2687 if (which == wxIMAGE_LIST_NORMAL)
2688 {
2689 return m_imageListNormal;
2690 }
2691 else if (which == wxIMAGE_LIST_SMALL)
2692 {
2693 return m_imageListSmall;
2694 }
2695 else if (which == wxIMAGE_LIST_STATE)
2696 {
2697 return m_imageListState;
2698 }
2699 return (wxImageList *) NULL;
2700 }
2701
2702 void wxListCtrl::SetImageList( wxImageList *imageList, int which )
2703 {
2704 m_mainWin->SetImageList( imageList, which );
2705 }
2706
2707 bool wxListCtrl::Arrange( int WXUNUSED(flag) )
2708 {
2709 return 0;
2710 }
2711
2712 bool wxListCtrl::DeleteItem( long item )
2713 {
2714 m_mainWin->DeleteItem( item );
2715 return TRUE;
2716 }
2717
2718 bool wxListCtrl::DeleteAllItems(void)
2719 {
2720 m_mainWin->DeleteAllItems();
2721 return TRUE;
2722 }
2723
2724 bool wxListCtrl::DeleteAllColumns()
2725 {
2726 for ( size_t n = 0; n < m_mainWin->m_columns.GetCount(); n++ )
2727 DeleteColumn(n);
2728
2729 return TRUE;
2730 }
2731
2732 void wxListCtrl::ClearAll()
2733 {
2734 m_mainWin->DeleteEverything();
2735 }
2736
2737 bool wxListCtrl::DeleteColumn( int col )
2738 {
2739 m_mainWin->DeleteColumn( col );
2740 return TRUE;
2741 }
2742
2743 void wxListCtrl::Edit( long item )
2744 {
2745 m_mainWin->Edit( item );
2746 }
2747
2748 bool wxListCtrl::EnsureVisible( long item )
2749 {
2750 m_mainWin->EnsureVisible( item );
2751 return TRUE;
2752 }
2753
2754 long wxListCtrl::FindItem( long start, const wxString& str, bool partial )
2755 {
2756 return m_mainWin->FindItem( start, str, partial );
2757 }
2758
2759 long wxListCtrl::FindItem( long start, long data )
2760 {
2761 return m_mainWin->FindItem( start, data );
2762 }
2763
2764 long wxListCtrl::FindItem( long WXUNUSED(start), const wxPoint& WXUNUSED(pt),
2765 int WXUNUSED(direction))
2766 {
2767 return 0;
2768 }
2769
2770 long wxListCtrl::HitTest( const wxPoint &point, int &flags )
2771 {
2772 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
2773 }
2774
2775 long wxListCtrl::InsertItem( wxListItem& info )
2776 {
2777 m_mainWin->InsertItem( info );
2778 return 0;
2779 }
2780
2781 long wxListCtrl::InsertItem( long index, const wxString &label )
2782 {
2783 wxListItem info;
2784 info.m_text = label;
2785 info.m_mask = wxLIST_MASK_TEXT;
2786 info.m_itemId = index;
2787 return InsertItem( info );
2788 }
2789
2790 long wxListCtrl::InsertItem( long index, int imageIndex )
2791 {
2792 wxListItem info;
2793 info.m_mask = wxLIST_MASK_IMAGE;
2794 info.m_image = imageIndex;
2795 info.m_itemId = index;
2796 return InsertItem( info );
2797 }
2798
2799 long wxListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
2800 {
2801 wxListItem info;
2802 info.m_text = label;
2803 info.m_image = imageIndex;
2804 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
2805 info.m_itemId = index;
2806 return InsertItem( info );
2807 }
2808
2809 long wxListCtrl::InsertColumn( long col, wxListItem &item )
2810 {
2811 m_mainWin->InsertColumn( col, item );
2812 return 0;
2813 }
2814
2815 long wxListCtrl::InsertColumn( long col, const wxString &heading,
2816 int format, int width )
2817 {
2818 wxListItem item;
2819 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
2820 item.m_text = heading;
2821 if (width >= -2)
2822 {
2823 item.m_mask |= wxLIST_MASK_WIDTH;
2824 item.m_width = width;
2825 }
2826 item.m_format = format;
2827
2828 return InsertColumn( col, item );
2829 }
2830
2831 bool wxListCtrl::ScrollList( int WXUNUSED(dx), int WXUNUSED(dy) )
2832 {
2833 return 0;
2834 }
2835
2836 // Sort items.
2837 // fn is a function which takes 3 long arguments: item1, item2, data.
2838 // item1 is the long data associated with a first item (NOT the index).
2839 // item2 is the long data associated with a second item (NOT the index).
2840 // data is the same value as passed to SortItems.
2841 // The return value is a negative number if the first item should precede the second
2842 // item, a positive number of the second item should precede the first,
2843 // or zero if the two items are equivalent.
2844 // data is arbitrary data to be passed to the sort function.
2845
2846 bool wxListCtrl::SortItems( wxListCtrlCompare fn, long data )
2847 {
2848 m_mainWin->SortItems( fn, data );
2849 return TRUE;
2850 }
2851
2852 void wxListCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2853 {
2854 if (!m_mainWin->m_dirty) return;
2855
2856 int cw = 0;
2857 int ch = 0;
2858 GetClientSize( &cw, &ch );
2859
2860 int x = 0;
2861 int y = 0;
2862 int w = 0;
2863 int h = 0;
2864
2865 if (HasFlag(wxLC_REPORT))
2866 {
2867 m_headerWin->GetPosition( &x, &y );
2868 m_headerWin->GetSize( &w, &h );
2869 if ((x != 0) || (y != 0) || (w != cw) || (h != 23))
2870 m_headerWin->SetSize( 0, 0, cw, 23 );
2871
2872 m_mainWin->GetPosition( &x, &y );
2873 m_mainWin->GetSize( &w, &h );
2874 if ((x != 0) || (y != 24) || (w != cw) || (h != ch-24))
2875 m_mainWin->SetSize( 0, 24, cw, ch-24 );
2876 }
2877 else
2878 {
2879 m_mainWin->GetPosition( &x, &y );
2880 m_mainWin->GetSize( &w, &h );
2881 if ((x != 0) || (y != 24) || (w != cw) || (h != ch))
2882 m_mainWin->SetSize( 0, 0, cw, ch );
2883 }
2884
2885 m_mainWin->CalculatePositions();
2886 m_mainWin->RealizeChanges();
2887 m_mainWin->m_dirty = FALSE;
2888 m_mainWin->Refresh();
2889 }
2890
2891 bool wxListCtrl::SetBackgroundColour( const wxColour &colour )
2892 {
2893 if ( !wxWindow::SetBackgroundColour( colour ) )
2894 return FALSE;
2895
2896 if (m_mainWin)
2897 {
2898 m_mainWin->SetBackgroundColour( colour );
2899 m_mainWin->m_dirty = TRUE;
2900 }
2901
2902 if (m_headerWin)
2903 {
2904 // m_headerWin->SetBackgroundColour( colour );
2905 }
2906
2907 return TRUE;
2908 }
2909
2910 bool wxListCtrl::SetForegroundColour( const wxColour &colour )
2911 {
2912 if ( !wxWindow::SetForegroundColour( colour ) )
2913 return FALSE;
2914
2915 if (m_mainWin)
2916 {
2917 m_mainWin->SetForegroundColour( colour );
2918 m_mainWin->m_dirty = TRUE;
2919 }
2920
2921 if (m_headerWin)
2922 {
2923 m_headerWin->SetForegroundColour( colour );
2924 }
2925
2926 return TRUE;
2927 }
2928
2929 bool wxListCtrl::SetFont( const wxFont &font )
2930 {
2931 if ( !wxWindow::SetFont( font ) )
2932 return FALSE;
2933
2934 if (m_mainWin)
2935 {
2936 m_mainWin->SetFont( font );
2937 m_mainWin->m_dirty = TRUE;
2938 }
2939
2940 if (m_headerWin)
2941 {
2942 m_headerWin->SetFont( font );
2943 }
2944
2945 return TRUE;
2946 }
2947