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