]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listctrl.cpp
Fixed bug that wrote incomplete wxExpr files (missing fclose)
[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()
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() const
96 {
97 return (m_image >= 0);
98 }
99
100 bool wxListItemData::HasText() 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() const
116 {
117 return m_xpos;
118 }
119
120 int wxListItemData::GetY() const
121 {
122 return m_ypos;
123 }
124
125 int wxListItemData::GetWidth() const
126 {
127 return m_width;
128 }
129
130 int wxListItemData::GetHeight() const
131 {
132 return m_height;
133 }
134
135 int wxListItemData::GetImage() 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()
148 {
149 return m_colour;
150 }
151
152 //-----------------------------------------------------------------------------
153 // wxListHeaderData
154 //-----------------------------------------------------------------------------
155
156 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderData,wxObject);
157
158 wxListHeaderData::wxListHeaderData()
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() const
212 {
213 return (m_image != 0);
214 }
215
216 bool wxListHeaderData::HasText() 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() const
241 {
242 return m_image;
243 }
244
245 int wxListHeaderData::GetWidth() const
246 {
247 return m_width;
248 }
249
250 int wxListHeaderData::GetFormat() 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()
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()
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_BEGIN_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 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp()) ) return;
1217
1218 wxClientDC dc(this);
1219 PrepareDC(dc);
1220 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1221 long y = dc.DeviceToLogicalY( (long)event.GetY() );
1222
1223 /* Did we actually hit an item ? */
1224 long hitResult = 0;
1225 wxNode *node = m_lines.First();
1226 wxListLineData *line = (wxListLineData *) NULL;
1227 while (node)
1228 {
1229 line = (wxListLineData*)node->Data();
1230 hitResult = line->IsHit( x, y );
1231 if (hitResult) break;
1232 line = (wxListLineData *) NULL;
1233 node = node->Next();
1234 }
1235
1236 if (event.Dragging())
1237 {
1238 if (m_dragCount == 0)
1239 m_dragStart = wxPoint(x,y);
1240
1241 m_dragCount++;
1242
1243 if (m_dragCount != 3) return;
1244
1245 int command = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1246 if (event.RightIsDown()) command = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1247
1248 wxListEvent le( command, GetParent()->GetId() );
1249 le.SetEventObject( GetParent() );
1250 le.m_pointDrag = m_dragStart;
1251 GetParent()->GetEventHandler()->ProcessEvent( le );
1252
1253 return;
1254 }
1255 else
1256 {
1257 m_dragCount = 0;
1258 }
1259
1260 if (!line) return;
1261
1262 if (event.ButtonDClick())
1263 {
1264 m_usedKeys = FALSE;
1265 m_lastOnSame = FALSE;
1266 m_renameTimer->Stop();
1267
1268 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
1269
1270 return;
1271 }
1272
1273 if (event.LeftUp() && m_lastOnSame)
1274 {
1275 m_usedKeys = FALSE;
1276 if ((line == m_current) &&
1277 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
1278 (m_mode & wxLC_EDIT_LABELS) )
1279 {
1280 m_renameTimer->Start( 100, TRUE );
1281 }
1282 m_lastOnSame = FALSE;
1283 return;
1284 }
1285
1286 if (event.RightDown())
1287 {
1288 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK );
1289 return;
1290 }
1291
1292 if (event.MiddleDown())
1293 {
1294 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
1295 return;
1296 }
1297
1298 if (event.LeftDown())
1299 {
1300 m_usedKeys = FALSE;
1301 wxListLineData *oldCurrent = m_current;
1302 if (m_mode & wxLC_SINGLE_SEL)
1303 {
1304 m_current = line;
1305 HilightAll( FALSE );
1306 m_current->ReverseHilight();
1307 RefreshLine( m_current );
1308 }
1309 else
1310 {
1311 if (event.ShiftDown())
1312 {
1313 m_current = line;
1314 m_current->ReverseHilight();
1315 RefreshLine( m_current );
1316 }
1317 else if (event.ControlDown())
1318 {
1319 m_current = line;
1320
1321 int numOfCurrent = -1;
1322 node = m_lines.First();
1323 while (node)
1324 {
1325 wxListLineData *test_line = (wxListLineData*)node->Data();
1326 numOfCurrent++;
1327 if (test_line == oldCurrent) break;
1328 node = node->Next();
1329 }
1330
1331 int numOfLine = -1;
1332 node = m_lines.First();
1333 while (node)
1334 {
1335 wxListLineData *test_line = (wxListLineData*)node->Data();
1336 numOfLine++;
1337 if (test_line == line) break;
1338 node = node->Next();
1339 }
1340
1341 if (numOfLine < numOfCurrent)
1342 {
1343 int i = numOfLine;
1344 numOfLine = numOfCurrent;
1345 numOfCurrent = i;
1346 }
1347
1348 wxNode *node = m_lines.Nth( numOfCurrent );
1349 for (int i = 0; i <= numOfLine-numOfCurrent; i++)
1350 {
1351 wxListLineData *test_line= (wxListLineData*)node->Data();
1352 test_line->Hilight(TRUE);
1353 RefreshLine( test_line );
1354 node = node->Next();
1355 }
1356 }
1357 else
1358 {
1359 m_current = line;
1360 HilightAll( FALSE );
1361 m_current->ReverseHilight();
1362 RefreshLine( m_current );
1363 }
1364 }
1365 if (m_current != oldCurrent)
1366 {
1367 RefreshLine( oldCurrent );
1368 UnfocusLine( oldCurrent );
1369 FocusLine( m_current );
1370 }
1371 m_lastOnSame = (m_current == oldCurrent);
1372 return;
1373 }
1374 }
1375
1376 void wxListMainWindow::MoveToFocus()
1377 {
1378 if (!m_current) return;
1379
1380 int x = 0;
1381 int y = 0;
1382 int w = 0;
1383 int h = 0;
1384 m_current->GetExtent( x, y, w, h );
1385
1386 int w_p = 0;
1387 int h_p = 0;
1388 GetClientSize( &w_p, &h_p );
1389
1390 if (m_mode & wxLC_REPORT)
1391 {
1392 int y_s = m_yScroll*GetScrollPos( wxVERTICAL );
1393 if ((y > y_s) && (y+h < y_s+h_p)) return;
1394 if (y-y_s < 5) { Scroll( -1, (y-5-h_p/2)/m_yScroll ); Refresh(); }
1395 if (y+h+5 > y_s+h_p) { Scroll( -1, (y+h-h_p/2+h+15)/m_yScroll); Refresh(); }
1396 }
1397 else
1398 {
1399 int x_s = m_xScroll*GetScrollPos( wxHORIZONTAL );
1400 if ((x > x_s) && (x+w < x_s+w_p)) return;
1401 if (x-x_s < 5) { Scroll( (x-5)/m_xScroll, -1 ); Refresh(); }
1402 if (x+w-5 > x_s+w_p) { Scroll( (x+w-w_p+15)/m_xScroll, -1 ); Refresh(); }
1403 }
1404 }
1405
1406 void wxListMainWindow::OnArrowChar( wxListLineData *newCurrent, bool shiftDown )
1407 {
1408 if ((m_mode & wxLC_SINGLE_SEL) || (m_usedKeys == FALSE)) m_current->Hilight( FALSE );
1409 wxListLineData *oldCurrent = m_current;
1410 m_current = newCurrent;
1411 MoveToFocus();
1412 if (shiftDown || (m_mode & wxLC_SINGLE_SEL)) m_current->Hilight( TRUE );
1413 RefreshLine( m_current );
1414 RefreshLine( oldCurrent );
1415 FocusLine( m_current );
1416 UnfocusLine( oldCurrent );
1417 }
1418
1419 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
1420 {
1421 wxWindow *parent = GetParent();
1422
1423 /* we propagate the key event up */
1424 wxKeyEvent ke( wxEVT_KEY_DOWN );
1425 ke.m_shiftDown = event.m_shiftDown;
1426 ke.m_controlDown = event.m_controlDown;
1427 ke.m_altDown = event.m_altDown;
1428 ke.m_metaDown = event.m_metaDown;
1429 ke.m_keyCode = event.m_keyCode;
1430 ke.m_x = event.m_x;
1431 ke.m_y = event.m_y;
1432 ke.SetEventObject( parent );
1433 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
1434
1435 event.Skip();
1436 }
1437
1438 void wxListMainWindow::OnChar( wxKeyEvent &event )
1439 {
1440 wxWindow *parent = GetParent();
1441
1442 /* we send a list_key event up */
1443 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
1444 le.m_code = event.KeyCode();
1445 le.SetEventObject( parent );
1446 parent->GetEventHandler()->ProcessEvent( le );
1447
1448 /* we propagate the char event up */
1449 wxKeyEvent ke( wxEVT_CHAR );
1450 ke.m_shiftDown = event.m_shiftDown;
1451 ke.m_controlDown = event.m_controlDown;
1452 ke.m_altDown = event.m_altDown;
1453 ke.m_metaDown = event.m_metaDown;
1454 ke.m_keyCode = event.m_keyCode;
1455 ke.m_x = event.m_x;
1456 ke.m_y = event.m_y;
1457 ke.SetEventObject( parent );
1458 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
1459
1460 if (event.KeyCode() == WXK_TAB)
1461 {
1462 wxNavigationKeyEvent nevent;
1463 nevent.SetDirection( !event.ShiftDown() );
1464 nevent.SetCurrentFocus( m_parent );
1465 if (m_parent->GetEventHandler()->ProcessEvent( nevent )) return;
1466 }
1467
1468 /* no item -> nothing to do */
1469 if (!m_current)
1470 {
1471 event.Skip();
1472 return;
1473 }
1474
1475 switch (event.KeyCode())
1476 {
1477 case WXK_UP:
1478 {
1479 wxNode *node = m_lines.Member( m_current )->Previous();
1480 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1481 break;
1482 }
1483 case WXK_DOWN:
1484 {
1485 wxNode *node = m_lines.Member( m_current )->Next();
1486 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1487 break;
1488 }
1489 case WXK_END:
1490 {
1491 wxNode *node = m_lines.Last();
1492 OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1493 break;
1494 }
1495 case WXK_HOME:
1496 {
1497 wxNode *node = m_lines.First();
1498 OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1499 break;
1500 }
1501 case WXK_PRIOR:
1502 {
1503 int steps = 0;
1504 if (m_mode & wxLC_REPORT)
1505 {
1506 steps = m_visibleLines-1;
1507 }
1508 else
1509 {
1510 int pos = 0;
1511 wxNode *node = m_lines.First();
1512 for (;;) { if (m_current == (wxListLineData*)node->Data()) break; pos++; node = node->Next(); }
1513 steps = pos % m_visibleLines;
1514 }
1515 wxNode *node = m_lines.Member( m_current );
1516 for (int i = 0; i < steps; i++) if (node->Previous()) node = node->Previous();
1517 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1518 break;
1519 }
1520 case WXK_NEXT:
1521 {
1522 int steps = 0;
1523 if (m_mode & wxLC_REPORT)
1524 {
1525 steps = m_visibleLines-1;
1526 }
1527 else
1528 {
1529 int pos = 0; wxNode *node = m_lines.First();
1530 for (;;) { if (m_current == (wxListLineData*)node->Data()) break; pos++; node = node->Next(); }
1531 steps = m_visibleLines-(pos % m_visibleLines)-1;
1532 }
1533 wxNode *node = m_lines.Member( m_current );
1534 for (int i = 0; i < steps; i++) if (node->Next()) node = node->Next();
1535 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1536 break;
1537 }
1538 case WXK_LEFT:
1539 {
1540 if (!(m_mode & wxLC_REPORT))
1541 {
1542 wxNode *node = m_lines.Member( m_current );
1543 for (int i = 0; i <m_visibleLines; i++) if (node->Previous()) node = node->Previous();
1544 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1545 }
1546 break;
1547 }
1548 case WXK_RIGHT:
1549 {
1550 if (!(m_mode & wxLC_REPORT))
1551 {
1552 wxNode *node = m_lines.Member( m_current );
1553 for (int i = 0; i <m_visibleLines; i++) if (node->Next()) node = node->Next();
1554 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1555 }
1556 break;
1557 }
1558 case WXK_SPACE:
1559 {
1560 m_current->ReverseHilight();
1561 RefreshLine( m_current );
1562 break;
1563 }
1564 case WXK_INSERT:
1565 {
1566 if (!(m_mode & wxLC_SINGLE_SEL))
1567 {
1568 wxListLineData *oldCurrent = m_current;
1569 m_current->ReverseHilight();
1570 wxNode *node = m_lines.Member( m_current )->Next();
1571 if (node) m_current = (wxListLineData*)node->Data();
1572 MoveToFocus();
1573 RefreshLine( oldCurrent );
1574 RefreshLine( m_current );
1575 UnfocusLine( oldCurrent );
1576 FocusLine( m_current );
1577 }
1578 break;
1579 }
1580 case WXK_RETURN:
1581 case WXK_EXECUTE:
1582 {
1583 wxListEvent le( wxEVT_COMMAND_LIST_ITEM_ACTIVATED, GetParent()->GetId() );
1584 le.SetEventObject( GetParent() );
1585 le.m_itemIndex = GetIndexOfLine( m_current );
1586 m_current->GetItem( 0, le.m_item );
1587 GetParent()->GetEventHandler()->ProcessEvent( le );
1588 break;
1589 }
1590 default:
1591 {
1592 event.Skip();
1593 return;
1594 }
1595 }
1596 m_usedKeys = TRUE;
1597 }
1598
1599 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1600 {
1601 m_hasFocus = TRUE;
1602 RefreshLine( m_current );
1603
1604 if (!GetParent()) return;
1605
1606 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
1607 event.SetEventObject( GetParent() );
1608 GetParent()->GetEventHandler()->ProcessEvent( event );
1609 }
1610
1611 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1612 {
1613 m_hasFocus = FALSE;
1614 RefreshLine( m_current );
1615 }
1616
1617 void wxListMainWindow::OnSize( wxSizeEvent &WXUNUSED(event) )
1618 {
1619 /*
1620 We don't even allow the wxScrolledWindow::AdjustScrollbars() call
1621
1622 */
1623 }
1624
1625 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
1626 {
1627 if ((m_mode & wxLC_ICON) && (m_normal_image_list))
1628 {
1629 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1630 return;
1631 }
1632 if ((m_mode & wxLC_SMALL_ICON) && (m_small_image_list))
1633 {
1634 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1635 }
1636 if ((m_mode & wxLC_REPORT) && (m_small_image_list))
1637 {
1638 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1639 return;
1640 }
1641 }
1642
1643 void wxListMainWindow::GetImageSize( int index, int &width, int &height )
1644 {
1645 if ((m_mode & wxLC_ICON) && (m_normal_image_list))
1646 {
1647 m_normal_image_list->GetSize( index, width, height );
1648 return;
1649 }
1650 if ((m_mode & wxLC_SMALL_ICON) && (m_small_image_list))
1651 {
1652 m_small_image_list->GetSize( index, width, height );
1653 return;
1654 }
1655 if ((m_mode & wxLC_REPORT) && (m_small_image_list))
1656 {
1657 m_small_image_list->GetSize( index, width, height );
1658 return;
1659 }
1660 width = 0;
1661 height = 0;
1662 }
1663
1664 int wxListMainWindow::GetTextLength( wxString &s )
1665 {
1666 wxClientDC dc( this );
1667 long lw = 0;
1668 long lh = 0;
1669 dc.GetTextExtent( s, &lw, &lh );
1670 return lw + 6;
1671 }
1672
1673 int wxListMainWindow::GetIndexOfLine( const wxListLineData *line )
1674 {
1675 int i = 0;
1676 wxNode *node = m_lines.First();
1677 while (node)
1678 {
1679 if (line == (wxListLineData*)node->Data()) return i;
1680 i++;
1681 node = node->Next();
1682 }
1683 return -1;
1684 }
1685
1686 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
1687 {
1688 m_dirty = TRUE;
1689 if (which == wxIMAGE_LIST_NORMAL) m_normal_image_list = imageList;
1690 if (which == wxIMAGE_LIST_SMALL) m_small_image_list = imageList;
1691 }
1692
1693 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
1694 {
1695 m_dirty = TRUE;
1696 if (isSmall)
1697 {
1698 m_small_spacing = spacing;
1699 }
1700 else
1701 {
1702 m_normal_spacing = spacing;
1703 }
1704 }
1705
1706 int wxListMainWindow::GetItemSpacing( bool isSmall )
1707 {
1708 if (isSmall) return m_small_spacing; else return m_normal_spacing;
1709 }
1710
1711 void wxListMainWindow::SetColumn( int col, wxListItem &item )
1712 {
1713 m_dirty = TRUE;
1714 wxNode *node = m_columns.Nth( col );
1715 if (node)
1716 {
1717 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER) item.m_width = GetTextLength( item.m_text )+7;
1718 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1719 column->SetItem( item );
1720 }
1721 wxListCtrl *lc = (wxListCtrl*) GetParent();
1722 if (lc->m_headerWin) lc->m_headerWin->Refresh();
1723 }
1724
1725 void wxListMainWindow::SetColumnWidth( int col, int width )
1726 {
1727 if (!(m_mode & wxLC_REPORT)) return;
1728
1729 m_dirty = TRUE;
1730
1731 wxNode *node = (wxNode*) NULL;
1732
1733 if (width == wxLIST_AUTOSIZE_USEHEADER) width = 80;
1734 if (width == wxLIST_AUTOSIZE)
1735 {
1736 wxClientDC dc(this);
1737 dc.SetFont( GetFont() );
1738 int max = 10;
1739 node = m_lines.First();
1740 while (node)
1741 {
1742 wxListLineData *line = (wxListLineData*)node->Data();
1743 wxNode *n = line->m_items.Nth( col );
1744 if (n)
1745 {
1746 wxListItemData *item = (wxListItemData*)n->Data();
1747 int current = 0, ix = 0, iy = 0;
1748 long lx = 0, ly = 0;
1749 if (item->HasImage())
1750 {
1751 GetImageSize( item->GetImage(), ix, iy );
1752 current = ix + 5;
1753 }
1754 if (item->HasText())
1755 {
1756 wxString str;
1757 item->GetText( str );
1758 dc.GetTextExtent( str, &lx, &ly );
1759 current += lx;
1760 }
1761 if (current > max) max = current;
1762 }
1763 node = node->Next();
1764 }
1765 width = max+10;
1766 }
1767
1768 node = m_columns.Nth( col );
1769 if (node)
1770 {
1771 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1772 column->SetWidth( width );
1773 }
1774
1775 node = m_lines.First();
1776 while (node)
1777 {
1778 wxListLineData *line = (wxListLineData*)node->Data();
1779 wxNode *n = line->m_items.Nth( col );
1780 if (n)
1781 {
1782 wxListItemData *item = (wxListItemData*)n->Data();
1783 item->SetSize( width, -1 );
1784 }
1785 node = node->Next();
1786 }
1787
1788 wxListCtrl *lc = (wxListCtrl*) GetParent();
1789 if (lc->m_headerWin) lc->m_headerWin->Refresh();
1790 }
1791
1792 void wxListMainWindow::GetColumn( int col, wxListItem &item )
1793 {
1794 wxNode *node = m_columns.Nth( col );
1795 if (node)
1796 {
1797 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1798 column->GetItem( item );
1799 }
1800 else
1801 {
1802 item.m_format = 0;
1803 item.m_width = 0;
1804 item.m_text = "";
1805 item.m_image = 0;
1806 item.m_data = 0;
1807 }
1808 }
1809
1810 int wxListMainWindow::GetColumnWidth( int col )
1811 {
1812 wxNode *node = m_columns.Nth( col );
1813 if (node)
1814 {
1815 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1816 return column->GetWidth();
1817 }
1818 else
1819 {
1820 return 0;
1821 }
1822 }
1823
1824 int wxListMainWindow::GetColumnCount()
1825 {
1826 return m_columns.Number();
1827 }
1828
1829 int wxListMainWindow::GetCountPerPage()
1830 {
1831 return m_visibleLines;
1832 }
1833
1834 void wxListMainWindow::SetItem( wxListItem &item )
1835 {
1836 m_dirty = TRUE;
1837 wxNode *node = m_lines.Nth( item.m_itemId );
1838 if (node)
1839 {
1840 wxListLineData *line = (wxListLineData*)node->Data();
1841 if (m_mode & wxLC_REPORT) item.m_width = GetColumnWidth( item.m_col )-3;
1842 line->SetItem( item.m_col, item );
1843 }
1844 }
1845
1846 void wxListMainWindow::SetItemState( long item, long state, long stateMask )
1847 {
1848 // m_dirty = TRUE; no recalcs needed
1849
1850 wxListLineData *oldCurrent = m_current;
1851
1852 if (stateMask & wxLIST_STATE_FOCUSED)
1853 {
1854 wxNode *node = m_lines.Nth( item );
1855 if (node)
1856 {
1857 wxListLineData *line = (wxListLineData*)node->Data();
1858 UnfocusLine( m_current );
1859 m_current = line;
1860 FocusLine( m_current );
1861 RefreshLine( m_current );
1862 if (oldCurrent) RefreshLine( oldCurrent );
1863 }
1864 }
1865
1866 if (stateMask & wxLIST_STATE_SELECTED)
1867 {
1868 bool on = state & wxLIST_STATE_SELECTED;
1869 if (!on && (m_mode & wxLC_SINGLE_SEL)) return;
1870
1871 wxNode *node = m_lines.Nth( item );
1872 if (node)
1873 {
1874 wxListLineData *line = (wxListLineData*)node->Data();
1875 if (m_mode & wxLC_SINGLE_SEL)
1876 {
1877 UnfocusLine( m_current );
1878 m_current = line;
1879 FocusLine( m_current );
1880 if (oldCurrent) oldCurrent->Hilight( FALSE );
1881 RefreshLine( m_current );
1882 if (oldCurrent) RefreshLine( oldCurrent );
1883 }
1884 bool on = state & wxLIST_STATE_SELECTED;
1885 if (on != line->IsHilighted())
1886 {
1887 line->Hilight( on );
1888 RefreshLine( line );
1889 }
1890 }
1891 }
1892 }
1893
1894 int wxListMainWindow::GetItemState( long item, long stateMask )
1895 {
1896 int ret = wxLIST_STATE_DONTCARE;
1897 if (stateMask & wxLIST_STATE_FOCUSED)
1898 {
1899 wxNode *node = m_lines.Nth( item );
1900 if (node)
1901 {
1902 wxListLineData *line = (wxListLineData*)node->Data();
1903 if (line == m_current) ret |= wxLIST_STATE_FOCUSED;
1904 }
1905 }
1906 if (stateMask & wxLIST_STATE_SELECTED)
1907 {
1908 wxNode *node = m_lines.Nth( item );
1909 if (node)
1910 {
1911 wxListLineData *line = (wxListLineData*)node->Data();
1912 if (line->IsHilighted()) ret |= wxLIST_STATE_FOCUSED;
1913 }
1914 }
1915 return ret;
1916 }
1917
1918 void wxListMainWindow::GetItem( wxListItem &item )
1919 {
1920 wxNode *node = m_lines.Nth( item.m_itemId );
1921 if (node)
1922 {
1923 wxListLineData *line = (wxListLineData*)node->Data();
1924 line->GetItem( item.m_col, item );
1925 }
1926 else
1927 {
1928 item.m_mask = 0;
1929 item.m_text = "";
1930 item.m_image = 0;
1931 item.m_data = 0;
1932 }
1933 }
1934
1935 int wxListMainWindow::GetItemCount()
1936 {
1937 return m_lines.Number();
1938 }
1939
1940 void wxListMainWindow::GetItemRect( long index, wxRect &rect )
1941 {
1942 wxNode *node = m_lines.Nth( index );
1943 if (node)
1944 {
1945 wxListLineData *line = (wxListLineData*)node->Data();
1946 line->GetRect( rect );
1947 }
1948 else
1949 {
1950 rect.x = 0;
1951 rect.y = 0;
1952 rect.width = 0;
1953 rect.height = 0;
1954 }
1955 }
1956
1957 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos)
1958 {
1959 wxNode *node = m_lines.Nth( item );
1960 if (node)
1961 {
1962 wxRect rect;
1963 wxListLineData *line = (wxListLineData*)node->Data();
1964 line->GetRect( rect );
1965 pos.x = rect.x;
1966 pos.y = rect.y;
1967 }
1968 else
1969 {
1970 pos.x = 0;
1971 pos.y = 0;
1972 }
1973 return TRUE;
1974 }
1975
1976 int wxListMainWindow::GetSelectedItemCount()
1977 {
1978 int ret = 0;
1979 wxNode *node = m_lines.First();
1980 while (node)
1981 {
1982 wxListLineData *line = (wxListLineData*)node->Data();
1983 if (line->IsHilighted()) ret++;
1984 node = node->Next();
1985 }
1986 return ret;
1987 }
1988
1989 void wxListMainWindow::SetMode( long mode )
1990 {
1991 m_dirty = TRUE;
1992 m_mode = mode;
1993
1994 DeleteEverything();
1995
1996 if (m_mode & wxLC_REPORT)
1997 {
1998 m_xScroll = 0;
1999 m_yScroll = 15;
2000 }
2001 else
2002 {
2003 m_xScroll = 15;
2004 m_yScroll = 0;
2005 }
2006 }
2007
2008 long wxListMainWindow::GetMode() const
2009 {
2010 return m_mode;
2011 }
2012
2013 void wxListMainWindow::CalculatePositions()
2014 {
2015 if (!m_lines.First()) return;
2016
2017 wxClientDC dc( this );
2018 dc.SetFont( GetFont() );
2019
2020 int iconSpacing = 0;
2021 if (m_mode & wxLC_ICON) iconSpacing = m_normal_spacing;
2022 if (m_mode & wxLC_SMALL_ICON) iconSpacing = m_small_spacing;
2023
2024 // we take the first line (which also can be an icon or
2025 // an a text item in wxLC_ICON and wxLC_LIST modes) to
2026 // measure the size of the line
2027
2028 int lineWidth = 0;
2029 int lineHeight = 0;
2030 int lineSpacing = 0;
2031
2032 wxListLineData *line = (wxListLineData*)m_lines.First()->Data();
2033 line->CalculateSize( &dc, iconSpacing );
2034 int dummy = 0;
2035 line->GetSize( dummy, lineSpacing );
2036 lineSpacing += 4;
2037
2038 int clientWidth = 0;
2039 int clientHeight = 0;
2040
2041 if (m_mode & wxLC_REPORT)
2042 {
2043 int x = 4;
2044 int y = 1;
2045 int entireHeight = m_lines.Number() * lineSpacing + 2;
2046 int scroll_pos = GetScrollPos( wxVERTICAL );
2047 SetScrollbars( m_xScroll, m_yScroll, 0, (entireHeight+15) / m_yScroll, 0, scroll_pos, TRUE );
2048 GetClientSize( &clientWidth, &clientHeight );
2049
2050 wxNode* node = m_lines.First();
2051 while (node)
2052 {
2053 wxListLineData *line = (wxListLineData*)node->Data();
2054 line->CalculateSize( &dc, iconSpacing );
2055 line->SetPosition( &dc, x, y, clientWidth );
2056 int col_x = 2;
2057 for (int i = 0; i < GetColumnCount(); i++)
2058 {
2059 line->SetColumnPosition( i, col_x );
2060 col_x += GetColumnWidth( i );
2061 }
2062 y += lineSpacing; // one pixel blank line between items
2063 node = node->Next();
2064 }
2065 m_visibleLines = clientHeight / lineSpacing;
2066 }
2067 else
2068 {
2069 // at first we try without any scrollbar. if the items don't
2070 // fit into the window, we recalculate after subtracting an
2071 // approximated 15 pt for the horizontal scrollbar
2072
2073 GetSize( &clientWidth, &clientHeight );
2074 clientHeight -= 4; // sunken frame
2075
2076 int entireWidth = 0;
2077
2078 for (int tries = 0; tries < 2; tries++)
2079 {
2080 entireWidth = 0;
2081 int x = 5; // painting is done at x-2
2082 int y = 5; // painting is done at y-2
2083 int maxWidth = 0;
2084 wxNode *node = m_lines.First();
2085 while (node)
2086 {
2087 wxListLineData *line = (wxListLineData*)node->Data();
2088 line->CalculateSize( &dc, iconSpacing );
2089 line->SetPosition( &dc, x, y, clientWidth );
2090 line->GetSize( lineWidth, lineHeight );
2091 if (lineWidth > maxWidth) maxWidth = lineWidth;
2092 y += lineSpacing;
2093 if (y+lineSpacing-6 >= clientHeight) // -6 for earlier "line breaking"
2094 {
2095 y = 5;
2096 x += maxWidth+6;
2097 entireWidth += maxWidth+6;
2098 maxWidth = 0;
2099 }
2100 node = node->Next();
2101 if (!node) entireWidth += maxWidth;
2102 if ((tries == 0) && (entireWidth > clientWidth))
2103 {
2104 clientHeight -= 15; // scrollbar height
2105 break;
2106 }
2107 if (!node) tries = 1; // everything fits, no second try required
2108 }
2109 }
2110 m_visibleLines = (clientHeight+6) / (lineSpacing); // +6 for earlier "line breaking"
2111
2112 int scroll_pos = GetScrollPos( wxHORIZONTAL );
2113 SetScrollbars( m_xScroll, m_yScroll, (entireWidth+15) / m_xScroll, 0, scroll_pos, 0, TRUE );
2114 }
2115 }
2116
2117 void wxListMainWindow::RealizeChanges( void )
2118 {
2119 if (!m_current)
2120 {
2121 wxNode *node = m_lines.First();
2122 if (node) m_current = (wxListLineData*)node->Data();
2123 }
2124 if (m_current)
2125 {
2126 FocusLine( m_current );
2127 if (m_mode & wxLC_SINGLE_SEL) m_current->Hilight( TRUE );
2128 }
2129 }
2130
2131 long wxListMainWindow::GetNextItem( long item, int WXUNUSED(geometry), int state )
2132 {
2133 long ret = 0;
2134 if (item > 0) ret = item;
2135 if(ret >= GetItemCount()) return -1;
2136 wxNode *node = m_lines.Nth( ret );
2137 while (node)
2138 {
2139 wxListLineData *line = (wxListLineData*)node->Data();
2140 if ((state & wxLIST_STATE_FOCUSED) && (line == m_current)) return ret;
2141 if ((state & wxLIST_STATE_SELECTED) && (line->IsHilighted())) return ret;
2142 if (!state) return ret;
2143 ret++;
2144 node = node->Next();
2145 }
2146 return -1;
2147 }
2148
2149 void wxListMainWindow::DeleteItem( long index )
2150 {
2151 m_dirty = TRUE;
2152 wxNode *node = m_lines.Nth( index );
2153 if (node)
2154 {
2155 wxListLineData *line = (wxListLineData*)node->Data();
2156 if (m_current == line) m_current = (wxListLineData *) NULL;
2157 DeleteLine( line );
2158 m_lines.DeleteNode( node );
2159 }
2160 }
2161
2162 void wxListMainWindow::DeleteColumn( int col )
2163 {
2164 wxCHECK_RET( col < (int)m_columns.GetCount(),
2165 _T("attempting to delete inexistent column in wxListView") );
2166
2167 m_dirty = TRUE;
2168 wxNode *node = m_columns.Nth( col );
2169 if (node) m_columns.DeleteNode( node );
2170 }
2171
2172 void wxListMainWindow::DeleteAllItems( void )
2173 {
2174 m_dirty = TRUE;
2175 m_current = (wxListLineData *) NULL;
2176 wxNode *node = m_lines.First();
2177 while (node)
2178 {
2179 wxListLineData *line = (wxListLineData*)node->Data();
2180 DeleteLine( line );
2181 node = node->Next();
2182 }
2183 m_lines.Clear();
2184 }
2185
2186 void wxListMainWindow::DeleteEverything( void )
2187 {
2188 m_dirty = TRUE;
2189 m_current = (wxListLineData *) NULL;
2190 wxNode *node = m_lines.First();
2191 while (node)
2192 {
2193 wxListLineData *line = (wxListLineData*)node->Data();
2194 DeleteLine( line );
2195 node = node->Next();
2196 }
2197 m_lines.Clear();
2198 m_current = (wxListLineData *) NULL;
2199 m_columns.Clear();
2200 }
2201
2202 void wxListMainWindow::EnsureVisible( long index )
2203 {
2204 // We have to call this here because the label in
2205 // question might just have been added and no screen
2206 // update taken place.
2207 if (m_dirty) wxYield();
2208
2209 wxListLineData *oldCurrent = m_current;
2210 m_current = (wxListLineData *) NULL;
2211 int i = index;
2212 wxNode *node = m_lines.Nth( i );
2213 if (node) m_current = (wxListLineData*)node->Data();
2214 if (m_current) MoveToFocus();
2215 m_current = oldCurrent;
2216 }
2217
2218 long wxListMainWindow::FindItem(long start, const wxString& str, bool WXUNUSED(partial) )
2219 {
2220 long pos = start;
2221 wxString tmp = str;
2222 if (pos < 0) pos = 0;
2223 wxNode *node = m_lines.Nth( pos );
2224 while (node)
2225 {
2226 wxListLineData *line = (wxListLineData*)node->Data();
2227 wxString s = "";
2228 line->GetText( 0, s );
2229 if (s == tmp) return pos;
2230 node = node->Next();
2231 pos++;
2232 }
2233 return -1;
2234 }
2235
2236 long wxListMainWindow::FindItem(long start, long data)
2237 {
2238 long pos = start;
2239 if (pos < 0) pos = 0;
2240 wxNode *node = m_lines.Nth( pos );
2241 while (node)
2242 {
2243 wxListLineData *line = (wxListLineData*)node->Data();
2244 wxListItem item;
2245 line->GetItem( 0, item );
2246 if (item.m_data == data) return pos;
2247 node = node->Next();
2248 pos++;
2249 }
2250 return -1;
2251 }
2252
2253 long wxListMainWindow::HitTest( int x, int y, int &flags )
2254 {
2255 wxNode *node = m_lines.First();
2256 int count = 0;
2257 while (node)
2258 {
2259 wxListLineData *line = (wxListLineData*)node->Data();
2260 long ret = line->IsHit( x, y );
2261 if (ret & flags)
2262 {
2263 flags = ret;
2264 return count;
2265 }
2266 node = node->Next();
2267 count++;
2268 }
2269 return -1;
2270 }
2271
2272 void wxListMainWindow::InsertItem( wxListItem &item )
2273 {
2274 m_dirty = TRUE;
2275 int mode = 0;
2276 if (m_mode & wxLC_REPORT) mode = wxLC_REPORT;
2277 else if (m_mode & wxLC_LIST) mode = wxLC_LIST;
2278 else if (m_mode & wxLC_ICON) mode = wxLC_ICON;
2279 else if (m_mode & wxLC_SMALL_ICON) mode = wxLC_ICON; // no typo
2280
2281 wxListLineData *line = new wxListLineData( this, mode, m_hilightBrush );
2282
2283 if (m_mode & wxLC_REPORT)
2284 {
2285 line->InitItems( GetColumnCount() );
2286 item.m_width = GetColumnWidth( 0 )-3;
2287 }
2288 else
2289 {
2290 line->InitItems( 1 );
2291 }
2292
2293 line->SetItem( 0, item );
2294 if ((item.m_itemId >= 0) && (item.m_itemId < (int)m_lines.GetCount()))
2295 {
2296 wxNode *node = m_lines.Nth( item.m_itemId );
2297 if (node) m_lines.Insert( node, line );
2298 }
2299 else
2300 {
2301 m_lines.Append( line );
2302 }
2303 }
2304
2305 void wxListMainWindow::InsertColumn( long col, wxListItem &item )
2306 {
2307 m_dirty = TRUE;
2308 if (m_mode & wxLC_REPORT)
2309 {
2310 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER) item.m_width = GetTextLength( item.m_text );
2311 wxListHeaderData *column = new wxListHeaderData( item );
2312 if ((col >= 0) && (col < (int)m_columns.GetCount()))
2313 {
2314 wxNode *node = m_columns.Nth( col );
2315 if (node)
2316 m_columns.Insert( node, column );
2317 }
2318 else
2319 {
2320 m_columns.Append( column );
2321 }
2322 }
2323 }
2324
2325 wxListCtrlCompare list_ctrl_compare_func_2;
2326 long list_ctrl_compare_data;
2327
2328 int list_ctrl_compare_func_1( const void *arg1, const void *arg2 )
2329 {
2330 wxListLineData *line1 = *((wxListLineData**)arg1);
2331 wxListLineData *line2 = *((wxListLineData**)arg2);
2332 wxListItem item;
2333 line1->GetItem( 0, item );
2334 long data1 = item.m_data;
2335 line2->GetItem( 0, item );
2336 long data2 = item.m_data;
2337 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
2338 }
2339
2340 void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data )
2341 {
2342 list_ctrl_compare_func_2 = fn;
2343 list_ctrl_compare_data = data;
2344 m_lines.Sort( list_ctrl_compare_func_1 );
2345 }
2346
2347 // -------------------------------------------------------------------------------------
2348 // wxListItem
2349 // -------------------------------------------------------------------------------------
2350
2351 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
2352
2353 wxListItem::wxListItem()
2354 {
2355 m_mask = 0;
2356 m_itemId = 0;
2357 m_col = 0;
2358 m_state = 0;
2359 m_stateMask = 0;
2360 m_image = 0;
2361 m_data = 0;
2362 m_format = wxLIST_FORMAT_CENTRE;
2363 m_width = 0;
2364 m_colour = wxBLACK;
2365 }
2366
2367 // -------------------------------------------------------------------------------------
2368 // wxListEvent
2369 // -------------------------------------------------------------------------------------
2370
2371 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
2372
2373 wxListEvent::wxListEvent( wxEventType commandType, int id ):
2374 wxNotifyEvent( commandType, id )
2375 {
2376 m_code = 0;
2377 m_itemIndex = 0;
2378 m_oldItemIndex = 0;
2379 m_col = 0;
2380 m_cancelled = FALSE;
2381 m_pointDrag.x = 0;
2382 m_pointDrag.y = 0;
2383 }
2384
2385 // -------------------------------------------------------------------------------------
2386 // wxListCtrl
2387 // -------------------------------------------------------------------------------------
2388
2389 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
2390
2391 BEGIN_EVENT_TABLE(wxListCtrl,wxControl)
2392 EVT_SIZE (wxListCtrl::OnSize)
2393 EVT_IDLE (wxListCtrl::OnIdle)
2394 END_EVENT_TABLE()
2395
2396 wxListCtrl::wxListCtrl()
2397 {
2398 m_imageListNormal = (wxImageList *) NULL;
2399 m_imageListSmall = (wxImageList *) NULL;
2400 m_imageListState = (wxImageList *) NULL;
2401 m_mainWin = (wxListMainWindow*) NULL;
2402 m_headerWin = (wxListHeaderWindow*) NULL;
2403 }
2404
2405 wxListCtrl::~wxListCtrl()
2406 {
2407 }
2408
2409 bool wxListCtrl::Create( wxWindow *parent, wxWindowID id,
2410 const wxPoint &pos, const wxSize &size,
2411 long style, const wxValidator &validator,
2412 const wxString &name )
2413 {
2414 m_imageListNormal = (wxImageList *) NULL;
2415 m_imageListSmall = (wxImageList *) NULL;
2416 m_imageListState = (wxImageList *) NULL;
2417 m_mainWin = (wxListMainWindow*) NULL;
2418 m_headerWin = (wxListHeaderWindow*) NULL;
2419
2420 long s = style;
2421
2422 if ((s & wxLC_REPORT == 0) &&
2423 (s & wxLC_LIST == 0) &&
2424 (s & wxLC_ICON == 0))
2425 {
2426 s = s | wxLC_LIST;
2427 }
2428
2429 bool ret = wxControl::Create( parent, id, pos, size, s, name );
2430
2431 #if wxUSE_VALIDATORS
2432 SetValidator( validator );
2433 #endif
2434
2435 if (s & wxSUNKEN_BORDER) s -= wxSUNKEN_BORDER;
2436
2437 m_mainWin = new wxListMainWindow( this, -1, wxPoint(0,0), size, s );
2438
2439 if (HasFlag(wxLC_REPORT))
2440 m_headerWin = new wxListHeaderWindow( this, -1, m_mainWin, wxPoint(0,0), wxSize(size.x,23), wxTAB_TRAVERSAL );
2441 else
2442 m_headerWin = (wxListHeaderWindow *) NULL;
2443
2444 SetBackgroundColour( *wxWHITE );
2445
2446 return ret;
2447 }
2448
2449 void wxListCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
2450 {
2451 /* handled in OnIdle */
2452
2453 if (m_mainWin) m_mainWin->m_dirty = TRUE;
2454 }
2455
2456 void wxListCtrl::SetSingleStyle( long style, bool add )
2457 {
2458 long flag = GetWindowStyle();
2459
2460 if (add)
2461 {
2462 if (style & wxLC_MASK_TYPE) flag = flag & ~wxLC_MASK_TYPE;
2463 if (style & wxLC_MASK_ALIGN) flag = flag & ~wxLC_MASK_ALIGN;
2464 if (style & wxLC_MASK_SORT) flag = flag & ~wxLC_MASK_SORT;
2465 }
2466
2467 if (add)
2468 {
2469 flag |= style;
2470 }
2471 else
2472 {
2473 if (flag & style) flag -= style;
2474 }
2475
2476 SetWindowStyleFlag( flag );
2477 }
2478
2479 void wxListCtrl::SetWindowStyleFlag( long flag )
2480 {
2481 if (m_mainWin)
2482 {
2483 m_mainWin->DeleteEverything();
2484
2485 int width = 0;
2486 int height = 0;
2487 GetClientSize( &width, &height );
2488
2489 m_mainWin->SetMode( flag );
2490
2491 if (flag & wxLC_REPORT)
2492 {
2493 if (!HasFlag(wxLC_REPORT))
2494 {
2495 if (!m_headerWin)
2496 {
2497 m_headerWin = new wxListHeaderWindow( this, -1, m_mainWin,
2498 wxPoint(0,0), wxSize(width,23), wxTAB_TRAVERSAL );
2499 }
2500 else
2501 {
2502 m_headerWin->Show( TRUE );
2503 }
2504 }
2505 }
2506 else
2507 {
2508 if (HasFlag(wxLC_REPORT))
2509 {
2510 m_headerWin->Show( FALSE );
2511 }
2512 }
2513 }
2514
2515 wxWindow::SetWindowStyleFlag( flag );
2516 }
2517
2518 bool wxListCtrl::GetColumn(int col, wxListItem &item) const
2519 {
2520 m_mainWin->GetColumn( col, item );
2521 return TRUE;
2522 }
2523
2524 bool wxListCtrl::SetColumn( int col, wxListItem& item )
2525 {
2526 m_mainWin->SetColumn( col, item );
2527 return TRUE;
2528 }
2529
2530 int wxListCtrl::GetColumnWidth( int col ) const
2531 {
2532 return m_mainWin->GetColumnWidth( col );
2533 }
2534
2535 bool wxListCtrl::SetColumnWidth( int col, int width )
2536 {
2537 m_mainWin->SetColumnWidth( col, width );
2538 return TRUE;
2539 }
2540
2541 int wxListCtrl::GetCountPerPage() const
2542 {
2543 return m_mainWin->GetCountPerPage(); // different from Windows ?
2544 }
2545
2546 bool wxListCtrl::GetItem( wxListItem &info ) const
2547 {
2548 m_mainWin->GetItem( info );
2549 return TRUE;
2550 }
2551
2552 bool wxListCtrl::SetItem( wxListItem &info )
2553 {
2554 m_mainWin->SetItem( info );
2555 return TRUE;
2556 }
2557
2558 long wxListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
2559 {
2560 wxListItem info;
2561 info.m_text = label;
2562 info.m_mask = wxLIST_MASK_TEXT;
2563 info.m_itemId = index;
2564 info.m_col = col;
2565 if ( imageId > -1 )
2566 {
2567 info.m_image = imageId;
2568 info.m_mask |= wxLIST_MASK_IMAGE;
2569 };
2570 m_mainWin->SetItem(info);
2571 return TRUE;
2572 }
2573
2574 int wxListCtrl::GetItemState( long item, long stateMask ) const
2575 {
2576 return m_mainWin->GetItemState( item, stateMask );
2577 }
2578
2579 bool wxListCtrl::SetItemState( long item, long state, long stateMask )
2580 {
2581 m_mainWin->SetItemState( item, state, stateMask );
2582 return TRUE;
2583 }
2584
2585 bool wxListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
2586 {
2587 wxListItem info;
2588 info.m_image = image;
2589 info.m_mask = wxLIST_MASK_IMAGE;
2590 info.m_itemId = item;
2591 m_mainWin->SetItem( info );
2592 return TRUE;
2593 }
2594
2595 wxString wxListCtrl::GetItemText( long item ) const
2596 {
2597 wxListItem info;
2598 info.m_itemId = item;
2599 m_mainWin->GetItem( info );
2600 return info.m_text;
2601 }
2602
2603 void wxListCtrl::SetItemText( long item, const wxString &str )
2604 {
2605 wxListItem info;
2606 info.m_mask = wxLIST_MASK_TEXT;
2607 info.m_itemId = item;
2608 info.m_text = str;
2609 m_mainWin->SetItem( info );
2610 }
2611
2612 long wxListCtrl::GetItemData( long item ) const
2613 {
2614 wxListItem info;
2615 info.m_itemId = item;
2616 m_mainWin->GetItem( info );
2617 return info.m_data;
2618 }
2619
2620 bool wxListCtrl::SetItemData( long item, long data )
2621 {
2622 wxListItem info;
2623 info.m_mask = wxLIST_MASK_DATA;
2624 info.m_itemId = item;
2625 info.m_data = data;
2626 m_mainWin->SetItem( info );
2627 return TRUE;
2628 }
2629
2630 bool wxListCtrl::GetItemRect( long item, wxRect &rect, int WXUNUSED(code) ) const
2631 {
2632 m_mainWin->GetItemRect( item, rect );
2633 return TRUE;
2634 }
2635
2636 bool wxListCtrl::GetItemPosition( long item, wxPoint& pos ) const
2637 {
2638 m_mainWin->GetItemPosition( item, pos );
2639 return TRUE;
2640 }
2641
2642 bool wxListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
2643 {
2644 return 0;
2645 }
2646
2647 int wxListCtrl::GetItemCount() const
2648 {
2649 return m_mainWin->GetItemCount();
2650 }
2651
2652 int wxListCtrl::GetColumnCount() const
2653 {
2654 return m_mainWin->GetColumnCount();
2655 }
2656
2657 void wxListCtrl::SetItemSpacing( int spacing, bool isSmall )
2658 {
2659 m_mainWin->SetItemSpacing( spacing, isSmall );
2660 }
2661
2662 int wxListCtrl::GetItemSpacing( bool isSmall ) const
2663 {
2664 return m_mainWin->GetItemSpacing( isSmall );
2665 }
2666
2667 int wxListCtrl::GetSelectedItemCount() const
2668 {
2669 return m_mainWin->GetSelectedItemCount();
2670 }
2671
2672 /*
2673 wxColour wxListCtrl::GetTextColour() const
2674 {
2675 }
2676
2677 void wxListCtrl::SetTextColour(const wxColour& WXUNUSED(col))
2678 {
2679 }
2680 */
2681
2682 long wxListCtrl::GetTopItem() const
2683 {
2684 return 0;
2685 }
2686
2687 long wxListCtrl::GetNextItem( long item, int geom, int state ) const
2688 {
2689 return m_mainWin->GetNextItem( item, geom, state );
2690 }
2691
2692 wxImageList *wxListCtrl::GetImageList(int which) const
2693 {
2694 if (which == wxIMAGE_LIST_NORMAL)
2695 {
2696 return m_imageListNormal;
2697 }
2698 else if (which == wxIMAGE_LIST_SMALL)
2699 {
2700 return m_imageListSmall;
2701 }
2702 else if (which == wxIMAGE_LIST_STATE)
2703 {
2704 return m_imageListState;
2705 }
2706 return (wxImageList *) NULL;
2707 }
2708
2709 void wxListCtrl::SetImageList( wxImageList *imageList, int which )
2710 {
2711 m_mainWin->SetImageList( imageList, which );
2712 }
2713
2714 bool wxListCtrl::Arrange( int WXUNUSED(flag) )
2715 {
2716 return 0;
2717 }
2718
2719 bool wxListCtrl::DeleteItem( long item )
2720 {
2721 m_mainWin->DeleteItem( item );
2722 return TRUE;
2723 }
2724
2725 bool wxListCtrl::DeleteAllItems()
2726 {
2727 m_mainWin->DeleteAllItems();
2728 return TRUE;
2729 }
2730
2731 bool wxListCtrl::DeleteAllColumns()
2732 {
2733 for ( size_t n = 0; n < m_mainWin->m_columns.GetCount(); n++ )
2734 DeleteColumn(n);
2735
2736 return TRUE;
2737 }
2738
2739 void wxListCtrl::ClearAll()
2740 {
2741 m_mainWin->DeleteEverything();
2742 }
2743
2744 bool wxListCtrl::DeleteColumn( int col )
2745 {
2746 m_mainWin->DeleteColumn( col );
2747 return TRUE;
2748 }
2749
2750 void wxListCtrl::Edit( long item )
2751 {
2752 m_mainWin->Edit( item );
2753 }
2754
2755 bool wxListCtrl::EnsureVisible( long item )
2756 {
2757 m_mainWin->EnsureVisible( item );
2758 return TRUE;
2759 }
2760
2761 long wxListCtrl::FindItem( long start, const wxString& str, bool partial )
2762 {
2763 return m_mainWin->FindItem( start, str, partial );
2764 }
2765
2766 long wxListCtrl::FindItem( long start, long data )
2767 {
2768 return m_mainWin->FindItem( start, data );
2769 }
2770
2771 long wxListCtrl::FindItem( long WXUNUSED(start), const wxPoint& WXUNUSED(pt),
2772 int WXUNUSED(direction))
2773 {
2774 return 0;
2775 }
2776
2777 long wxListCtrl::HitTest( const wxPoint &point, int &flags )
2778 {
2779 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
2780 }
2781
2782 long wxListCtrl::InsertItem( wxListItem& info )
2783 {
2784 m_mainWin->InsertItem( info );
2785 return 0;
2786 }
2787
2788 long wxListCtrl::InsertItem( long index, const wxString &label )
2789 {
2790 wxListItem info;
2791 info.m_text = label;
2792 info.m_mask = wxLIST_MASK_TEXT;
2793 info.m_itemId = index;
2794 return InsertItem( info );
2795 }
2796
2797 long wxListCtrl::InsertItem( long index, int imageIndex )
2798 {
2799 wxListItem info;
2800 info.m_mask = wxLIST_MASK_IMAGE;
2801 info.m_image = imageIndex;
2802 info.m_itemId = index;
2803 return InsertItem( info );
2804 }
2805
2806 long wxListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
2807 {
2808 wxListItem info;
2809 info.m_text = label;
2810 info.m_image = imageIndex;
2811 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
2812 info.m_itemId = index;
2813 return InsertItem( info );
2814 }
2815
2816 long wxListCtrl::InsertColumn( long col, wxListItem &item )
2817 {
2818 m_mainWin->InsertColumn( col, item );
2819 return 0;
2820 }
2821
2822 long wxListCtrl::InsertColumn( long col, const wxString &heading,
2823 int format, int width )
2824 {
2825 wxListItem item;
2826 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
2827 item.m_text = heading;
2828 if (width >= -2)
2829 {
2830 item.m_mask |= wxLIST_MASK_WIDTH;
2831 item.m_width = width;
2832 }
2833 item.m_format = format;
2834
2835 return InsertColumn( col, item );
2836 }
2837
2838 bool wxListCtrl::ScrollList( int WXUNUSED(dx), int WXUNUSED(dy) )
2839 {
2840 return 0;
2841 }
2842
2843 // Sort items.
2844 // fn is a function which takes 3 long arguments: item1, item2, data.
2845 // item1 is the long data associated with a first item (NOT the index).
2846 // item2 is the long data associated with a second item (NOT the index).
2847 // data is the same value as passed to SortItems.
2848 // The return value is a negative number if the first item should precede the second
2849 // item, a positive number of the second item should precede the first,
2850 // or zero if the two items are equivalent.
2851 // data is arbitrary data to be passed to the sort function.
2852
2853 bool wxListCtrl::SortItems( wxListCtrlCompare fn, long data )
2854 {
2855 m_mainWin->SortItems( fn, data );
2856 return TRUE;
2857 }
2858
2859 void wxListCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2860 {
2861 if (!m_mainWin->m_dirty) return;
2862
2863 int cw = 0;
2864 int ch = 0;
2865 GetClientSize( &cw, &ch );
2866
2867 int x = 0;
2868 int y = 0;
2869 int w = 0;
2870 int h = 0;
2871
2872 if (HasFlag(wxLC_REPORT))
2873 {
2874 m_headerWin->GetPosition( &x, &y );
2875 m_headerWin->GetSize( &w, &h );
2876 if ((x != 0) || (y != 0) || (w != cw) || (h != 23))
2877 m_headerWin->SetSize( 0, 0, cw, 23 );
2878
2879 m_mainWin->GetPosition( &x, &y );
2880 m_mainWin->GetSize( &w, &h );
2881 if ((x != 0) || (y != 24) || (w != cw) || (h != ch-24))
2882 m_mainWin->SetSize( 0, 24, cw, ch-24 );
2883 }
2884 else
2885 {
2886 m_mainWin->GetPosition( &x, &y );
2887 m_mainWin->GetSize( &w, &h );
2888 if ((x != 0) || (y != 24) || (w != cw) || (h != ch))
2889 m_mainWin->SetSize( 0, 0, cw, ch );
2890 }
2891
2892 m_mainWin->CalculatePositions();
2893 m_mainWin->RealizeChanges();
2894 m_mainWin->m_dirty = FALSE;
2895 m_mainWin->Refresh();
2896 }
2897
2898 bool wxListCtrl::SetBackgroundColour( const wxColour &colour )
2899 {
2900 if ( !wxWindow::SetBackgroundColour( colour ) )
2901 return FALSE;
2902
2903 if (m_mainWin)
2904 {
2905 m_mainWin->SetBackgroundColour( colour );
2906 m_mainWin->m_dirty = TRUE;
2907 }
2908
2909 if (m_headerWin)
2910 {
2911 // m_headerWin->SetBackgroundColour( colour );
2912 }
2913
2914 return TRUE;
2915 }
2916
2917 bool wxListCtrl::SetForegroundColour( const wxColour &colour )
2918 {
2919 if ( !wxWindow::SetForegroundColour( colour ) )
2920 return FALSE;
2921
2922 if (m_mainWin)
2923 {
2924 m_mainWin->SetForegroundColour( colour );
2925 m_mainWin->m_dirty = TRUE;
2926 }
2927
2928 if (m_headerWin)
2929 {
2930 m_headerWin->SetForegroundColour( colour );
2931 }
2932
2933 return TRUE;
2934 }
2935
2936 bool wxListCtrl::SetFont( const wxFont &font )
2937 {
2938 if ( !wxWindow::SetFont( font ) )
2939 return FALSE;
2940
2941 if (m_mainWin)
2942 {
2943 m_mainWin->SetFont( font );
2944 m_mainWin->m_dirty = TRUE;
2945 }
2946
2947 if (m_headerWin)
2948 {
2949 m_headerWin->SetFont( font );
2950 }
2951
2952 return TRUE;
2953 }
2954