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