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