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