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