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