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