]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listctrl.cpp
ADDED wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
[wxWidgets.git] / src / generic / listctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: listctrl.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "listctrl.h"
12 #endif
13
14 #include "wx/dcscreen.h"
15 #include "wx/app.h"
16 #include "wx/listctrl.h"
17
18 //-----------------------------------------------------------------------------
19 // wxListItemData
20 //-----------------------------------------------------------------------------
21
22 IMPLEMENT_DYNAMIC_CLASS(wxListItemData,wxObject);
23
24 wxListItemData::wxListItemData(void)
25 {
26 m_image = -1;
27 m_data = 0;
28 m_xpos = 0;
29 m_ypos = 0;
30 m_width = 0;
31 m_height = 0;
32 m_colour = wxBLACK;
33 }
34
35 wxListItemData::wxListItemData( const wxListItem &info )
36 {
37 m_image = -1;
38 m_data = 0;
39 m_colour = info.m_colour;
40 SetItem( info );
41 }
42
43 void wxListItemData::SetItem( const wxListItem &info )
44 {
45 if (info.m_mask & wxLIST_MASK_TEXT) m_text = info.m_text;
46 if (info.m_mask & wxLIST_MASK_IMAGE) m_image = info.m_image;
47 if (info.m_mask & wxLIST_MASK_DATA) m_data = info.m_data;
48 m_colour = info.m_colour;
49 m_xpos = 0;
50 m_ypos = 0;
51 m_width = info.m_width;
52 m_height = 0;
53 }
54
55 void wxListItemData::SetText( const wxString &s )
56 {
57 m_text = s;
58 }
59
60 void wxListItemData::SetImage( int image )
61 {
62 m_image = image;
63 }
64
65 void wxListItemData::SetData( long data )
66 {
67 m_data = data;
68 }
69
70 void wxListItemData::SetPosition( int x, int y )
71 {
72 m_xpos = x;
73 m_ypos = y;
74 }
75
76 void wxListItemData::SetSize( int const width, int height )
77 {
78 if (width != -1) m_width = width;
79 if (height != -1) m_height = height;
80 }
81
82 void wxListItemData::SetColour( wxColour *col )
83 {
84 m_colour = col;
85 }
86
87 bool wxListItemData::HasImage(void) const
88 {
89 return (m_image >= 0);
90 }
91
92 bool wxListItemData::HasText(void) const
93 {
94 return (!m_text.IsNull());
95 }
96
97 bool wxListItemData::IsHit( int x, int y ) const
98 {
99 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
100 }
101
102 void wxListItemData::GetText( wxString &s )
103 {
104 s = m_text;
105 }
106
107 int wxListItemData::GetX( void ) const
108 {
109 return m_xpos;
110 }
111
112 int wxListItemData::GetY( void ) const
113 {
114 return m_ypos;
115 }
116
117 int wxListItemData::GetWidth(void) const
118 {
119 return m_width;
120 }
121
122 int wxListItemData::GetHeight(void) const
123 {
124 return m_height;
125 }
126
127 int wxListItemData::GetImage(void) const
128 {
129 return m_image;
130 }
131
132 void wxListItemData::GetItem( wxListItem &info )
133 {
134 info.m_text = m_text;
135 info.m_image = m_image;
136 info.m_data = m_data;
137 }
138
139 wxColour *wxListItemData::GetColour(void)
140 {
141 return m_colour;
142 }
143
144 //-----------------------------------------------------------------------------
145 // wxListHeaderData
146 //-----------------------------------------------------------------------------
147
148 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderData,wxObject);
149
150 wxListHeaderData::wxListHeaderData(void)
151 {
152 m_mask = 0;
153 m_image = 0;
154 m_format = 0;
155 m_width = 0;
156 m_xpos = 0;
157 m_ypos = 0;
158 m_height = 0;
159 }
160
161 wxListHeaderData::wxListHeaderData( const wxListItem &item )
162 {
163 SetItem( item );
164 m_xpos = 0;
165 m_ypos = 0;
166 m_height = 0;
167 }
168
169 void wxListHeaderData::SetItem( const wxListItem &item )
170 {
171 m_mask = item.m_mask;
172 m_text = item.m_text;
173 m_image = item.m_image;
174 m_format = item.m_format;
175 m_width = item.m_width;
176 if (m_width < 0) m_width = 80;
177 if (m_width < 6) m_width = 6;
178 }
179
180 void wxListHeaderData::SetPosition( int x, int y )
181 {
182 m_xpos = x;
183 m_ypos = y;
184 }
185
186 void wxListHeaderData::SetHeight( int h )
187 {
188 m_height = h;
189 }
190
191 void wxListHeaderData::SetWidth( int w )
192 {
193 m_width = w;
194 if (m_width < 0) m_width = 80;
195 if (m_width < 6) m_width = 6;
196 }
197
198 void wxListHeaderData::SetFormat( int format )
199 {
200 m_format = format;
201 }
202
203 bool wxListHeaderData::HasImage(void) const
204 {
205 return (m_image != 0);
206 }
207
208 bool wxListHeaderData::HasText(void) const
209 {
210 return (m_text.Length() > 0);
211 }
212
213 bool wxListHeaderData::IsHit( int x, int y ) const
214 {
215 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
216 }
217
218 void wxListHeaderData::GetItem( wxListItem &item )
219 {
220 item.m_mask = m_mask;
221 item.m_text = m_text;
222 item.m_image = m_image;
223 item.m_format = m_format;
224 item.m_width = m_width;
225 }
226
227 void wxListHeaderData::GetText( wxString &s )
228 {
229 s = m_text;
230 }
231
232 int wxListHeaderData::GetImage(void) const
233 {
234 return m_image;
235 }
236
237 int wxListHeaderData::GetWidth(void) const
238 {
239 return m_width;
240 }
241
242 int wxListHeaderData::GetFormat(void) const
243 {
244 return m_format;
245 }
246
247 //-----------------------------------------------------------------------------
248 // wxListLineData
249 //-----------------------------------------------------------------------------
250
251 IMPLEMENT_DYNAMIC_CLASS(wxListLineData,wxObject);
252
253 wxListLineData::wxListLineData( wxListMainWindow *owner, int mode, wxBrush *hilightBrush )
254 {
255 m_mode = mode;
256 m_hilighted = FALSE;
257 m_owner = owner;
258 m_hilightBrush = hilightBrush;
259 m_items.DeleteContents( TRUE );
260 m_spacing = 0;
261 }
262
263 void wxListLineData::CalculateSize( wxPaintDC *dc, int spacing )
264 {
265 m_spacing = spacing;
266 switch (m_mode)
267 {
268 case wxLC_ICON:
269 {
270 m_bound_all.width = m_spacing;
271 m_bound_all.height = m_spacing+13;
272 wxNode *node = m_items.First();
273 if (node)
274 {
275 wxListItemData *item = (wxListItemData*)node->Data();
276 wxString s;
277 item->GetText( s );
278 long lw,lh;
279 dc->GetTextExtent( s, &lw, &lh );
280 if (lw > m_spacing) m_bound_all.width = lw;
281 }
282 break;
283 }
284 case wxLC_LIST:
285 {
286 wxNode *node = m_items.First();
287 if (node)
288 {
289 wxListItemData *item = (wxListItemData*)node->Data();
290 wxString s;
291 item->GetText( s );
292 long lw,lh;
293 dc->GetTextExtent( s, &lw, &lh );
294 m_bound_all.width = lw;
295 m_bound_all.height = lh;
296 }
297 break;
298 }
299 case wxLC_REPORT:
300 {
301 m_bound_all.width = 0;
302 m_bound_all.height = 0;
303 wxNode *node = m_items.First();
304 while (node)
305 {
306 wxListItemData *item = (wxListItemData*)node->Data();
307 wxString s;
308 item->GetText( s );
309 if (s.IsNull()) s = "H";
310 long lw,lh;
311 dc->GetTextExtent( s, &lw, &lh );
312 item->SetSize( item->GetWidth(), lh );
313 m_bound_all.width += lw;
314 m_bound_all.height = lh;
315 node = node->Next();
316 }
317 break;
318 }
319 }
320 }
321
322 void wxListLineData::SetPosition( wxPaintDC *dc, int x, int y, int window_width )
323 {
324 m_bound_all.x = x;
325 m_bound_all.y = y;
326 switch (m_mode)
327 {
328 case wxLC_ICON:
329 {
330 AssignRect( m_bound_icon, 0, 0, 0, 0 );
331 AssignRect( m_bound_label, 0, 0, 0, 0 );
332 AssignRect( m_bound_hilight, m_bound_all );
333 wxNode *node = m_items.First();
334 if (node)
335 {
336 wxListItemData *item = (wxListItemData*)node->Data();
337 if (item->HasImage())
338 {
339 wxListItemData *item = (wxListItemData*)node->Data();
340 int w = 0;
341 int h = 0;
342 m_owner->GetImageSize( item->GetImage(), w, h );
343 m_bound_icon.x = m_bound_all.x + (m_spacing/2) - (w/2);
344 m_bound_icon.y = m_bound_all.y + m_spacing - h - 5;
345 m_bound_icon.width = w;
346 m_bound_icon.height = h;
347 if (!item->HasText())
348 {
349 AssignRect( m_bound_hilight, m_bound_icon );
350 m_bound_hilight.x -= 5;
351 m_bound_hilight.y -= 5;
352 m_bound_hilight.width += 9;
353 m_bound_hilight.height += 9;
354 }
355 }
356 if (item->HasText())
357 {
358 wxString s;
359 item->GetText( s );
360 long lw,lh;
361 dc->GetTextExtent( s, &lw, &lh );
362 if (m_bound_all.width > m_spacing)
363 m_bound_label.x = m_bound_all.x;
364 else
365 m_bound_label.x = m_bound_all.x + (m_spacing/2) - lw/2;
366 m_bound_label.y = m_bound_all.y + m_bound_all.height - lh;
367 m_bound_label.width = lw;
368 m_bound_label.height = lh;
369 AssignRect( m_bound_hilight, m_bound_label );
370 m_bound_hilight.x -= 2;
371 m_bound_hilight.y -= 2;
372 m_bound_hilight.width += 4;
373 m_bound_hilight.height += 4;
374 }
375 }
376 break;
377 }
378 case wxLC_LIST:
379 {
380 AssignRect( m_bound_label, m_bound_all );
381 m_bound_all.x -= 2;
382 m_bound_all.y -= 2;
383 m_bound_all.width += 4;
384 m_bound_all.height += 3;
385 AssignRect( m_bound_hilight, m_bound_all );
386 AssignRect( m_bound_icon, 0, 0, 0, 0 );
387 break;
388 }
389 case wxLC_REPORT:
390 {
391 long lw,lh;
392 dc->GetTextExtent( "H", &lw, &lh );
393 m_bound_all.x = 0;
394 m_bound_all.y -= 0;
395 m_bound_all.height = lh+3;
396 m_bound_all.width = window_width;
397 AssignRect( m_bound_hilight, m_bound_all );
398 AssignRect( m_bound_label, 0, 0, 0 ,0 );
399 AssignRect( m_bound_icon, 0, 0, 0, 0 );
400 break;
401 }
402 }
403 }
404
405 void wxListLineData::SetColumnPosition( int index, int x )
406 {
407 int i = index;
408 wxNode *node = m_items.Nth( i );
409 if (node)
410 {
411 wxListItemData *item = (wxListItemData*)node->Data();
412 item->SetPosition( x, m_bound_all.y+1 );
413 }
414 }
415
416 void wxListLineData::GetSize( int &width, int &height )
417 {
418 width = m_bound_all.width;
419 height = m_bound_all.height;
420 }
421
422 void wxListLineData::GetExtent( int &x, int &y, int &width, int &height )
423 {
424 x = m_bound_all.x;
425 y = m_bound_all.y;
426 width = m_bound_all.width;
427 height = m_bound_all.height;
428 }
429
430 void wxListLineData::GetLabelExtent( int &x, int &y, int &width, int &height )
431 {
432 x = m_bound_label.x;
433 y = m_bound_label.y;
434 width = m_bound_label.width;
435 height = m_bound_label.height;
436 }
437
438 void wxListLineData::GetRect( wxRectangle &rect )
439 {
440 AssignRect( rect, m_bound_all );
441 }
442
443 long wxListLineData::IsHit( int x, int y )
444 {
445 wxNode *node = m_items.First();
446 if (node)
447 {
448 wxListItemData *item = (wxListItemData*)node->Data();
449 if (item->HasImage() && IsInRect( x, y, m_bound_icon )) return wxLIST_HITTEST_ONITEMICON;
450 if (item->HasText() && IsInRect( x, y, m_bound_label )) return wxLIST_HITTEST_ONITEMLABEL;
451 // if (!(item->HasImage() || item->HasText())) return 0;
452 }
453 // if there is no icon or text = empty
454 if (IsInRect( x, y, m_bound_all )) return wxLIST_HITTEST_ONITEMICON;
455 return 0;
456 }
457
458 void wxListLineData::InitItems( int num )
459 {
460 for (int i = 0; i < num; i++) m_items.Append( new wxListItemData() );
461 }
462
463 void wxListLineData::SetItem( int index, const wxListItem &info )
464 {
465 wxNode *node = m_items.Nth( index );
466 if (node)
467 {
468 wxListItemData *item = (wxListItemData*)node->Data();
469 item->SetItem( info );
470 }
471 }
472
473 void wxListLineData::GetItem( int const index, wxListItem &info )
474 {
475 int i = index;
476 wxNode *node = m_items.Nth( i );
477 if (node)
478 {
479 wxListItemData *item = (wxListItemData*)node->Data();
480 item->GetItem( info );
481 }
482 }
483
484 void wxListLineData::GetText( int index, wxString &s )
485 {
486 int i = index;
487 wxNode *node = m_items.Nth( i );
488 s = "";
489 if (node)
490 {
491 wxListItemData *item = (wxListItemData*)node->Data();
492 item->GetText( s );
493 }
494 }
495
496 void wxListLineData::SetText( int index, const wxString s )
497 {
498 int i = index;
499 wxNode *node = m_items.Nth( i );
500 if (node)
501 {
502 wxListItemData *item = (wxListItemData*)node->Data();
503 item->SetText( s );
504 }
505 }
506
507 int wxListLineData::GetImage( int index )
508 {
509 int i = index;
510 wxNode *node = m_items.Nth( i );
511 if (node)
512 {
513 wxListItemData *item = (wxListItemData*)node->Data();
514 return item->GetImage();
515 }
516 return -1;
517 }
518
519 void wxListLineData::DoDraw( wxPaintDC *dc, bool hilight, bool paintBG )
520 {
521 long dev_x = dc->LogicalToDeviceX( m_bound_all.x-2 );
522 long dev_y = dc->LogicalToDeviceY( m_bound_all.y-2 );
523 long dev_w = dc->LogicalToDeviceXRel( m_bound_all.width+4 );
524 long dev_h = dc->LogicalToDeviceYRel( m_bound_all.height+4 );
525 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::SendNotify( wxListLineData *line, wxEventType command )
1068 {
1069 wxListEvent le( command, GetParent()->GetId() );
1070 le.SetEventObject( GetParent() );
1071 le.m_itemIndex = GetIndexOfLine( line );
1072 line->GetItem( 0, le.m_item );
1073 GetParent()->GetEventHandler()->ProcessEvent( le );
1074 }
1075
1076 void wxListMainWindow::FocusLine( wxListLineData *WXUNUSED(line) )
1077 {
1078 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_FOCUSSED );
1079 }
1080
1081 void wxListMainWindow::UnfocusLine( wxListLineData *WXUNUSED(line) )
1082 {
1083 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_UNFOCUSSED );
1084 }
1085
1086 void wxListMainWindow::SelectLine( wxListLineData *line )
1087 {
1088 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_SELECTED );
1089 }
1090
1091 void wxListMainWindow::DeselectLine( wxListLineData *line )
1092 {
1093 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_DESELECTED );
1094 }
1095
1096 void wxListMainWindow::DeleteLine( wxListLineData *line )
1097 {
1098 SendNotify( line, wxEVT_COMMAND_LIST_DELETE_ITEM );
1099 }
1100
1101 void wxListMainWindow::StartLabelEdit( wxListLineData *line )
1102 {
1103 SendNotify( line, wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT );
1104 }
1105
1106 void wxListMainWindow::RenameLine( wxListLineData *line, const wxString &newName )
1107 {
1108 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
1109 le.SetEventObject( GetParent() );
1110 le.m_itemIndex = GetIndexOfLine( line );
1111 line->GetItem( 0, le.m_item );
1112 le.m_item.m_text = newName;
1113 GetParent()->GetEventHandler()->ProcessEvent( le );
1114 }
1115
1116 void wxListMainWindow::OnRenameTimer()
1117 {
1118 StartLabelEdit( m_current );
1119 wxString s;
1120 m_current->GetText( 0, s );
1121 int x = 0;
1122 int y = 0;
1123 int w = 0;
1124 int h = 0;
1125 m_current->GetLabelExtent( x, y, w, h );
1126
1127 wxClientDC dc(this);
1128 PrepareDC( dc );
1129 x = dc.LogicalToDeviceX( x );
1130 y = dc.LogicalToDeviceY( y );
1131
1132 wxListTextCtrl *text = new wxListTextCtrl(
1133 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
1134 text->SetFocus();
1135 }
1136
1137 void wxListMainWindow::OnRenameAccept()
1138 {
1139 RenameLine( m_current, m_renameRes );
1140 }
1141
1142 void wxListMainWindow::OnMouse( wxMouseEvent &event )
1143 {
1144 if (GetParent()->GetEventHandler()->ProcessEvent( event)) return;
1145
1146 if (!m_current) return;
1147 if (m_dirty) return;
1148
1149 wxClientDC dc(this);
1150 PrepareDC(dc);
1151 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1152 long y = dc.DeviceToLogicalY( (long)event.GetY() );
1153
1154 // Did we actually hit an item ?
1155 long hitResult = 0;
1156 wxNode *node = m_lines.First();
1157 wxListLineData *line = (wxListLineData *) NULL;
1158 while (node)
1159 {
1160 line = (wxListLineData*)node->Data();
1161 hitResult = line->IsHit( x, y );
1162 if (hitResult) break;
1163 line = (wxListLineData *) NULL;
1164 node = node->Next();
1165 }
1166
1167 if (!event.Dragging())
1168 m_dragCount = 0;
1169 else
1170 m_dragCount++;
1171
1172 if (event.Dragging() && (m_dragCount > 3))
1173 {
1174 m_dragCount = 0;
1175
1176 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_DRAG, GetParent()->GetId() );
1177 le.SetEventObject( GetParent() );
1178 le.m_pointDrag.x = x;
1179 le.m_pointDrag.y = y;
1180 GetParent()->GetEventHandler()->ProcessEvent( le );
1181
1182 return;
1183 }
1184
1185 if (!line) return;
1186
1187 if (event.ButtonDClick())
1188 {
1189 m_usedKeys = FALSE;
1190 m_lastOnSame = FALSE;
1191 m_renameTimer->Stop();
1192
1193 SendNotify( line, wxEVT_COMMAND_LIST_KEY_DOWN );
1194
1195 return;
1196 }
1197
1198 if (event.LeftUp() && m_lastOnSame)
1199 {
1200 m_usedKeys = FALSE;
1201 if ((line == m_current) &&
1202 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
1203 (m_mode & wxLC_EDIT_LABELS) )
1204 {
1205 m_renameTimer->Start( 100, TRUE );
1206 }
1207 m_lastOnSame = FALSE;
1208 return;
1209 }
1210
1211 if (event.RightDown())
1212 {
1213 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK );
1214 return;
1215 }
1216
1217 if (event.MiddleDown())
1218 {
1219 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
1220 return;
1221 }
1222
1223 if (event.LeftDown())
1224 {
1225 m_usedKeys = FALSE;
1226 wxListLineData *oldCurrent = m_current;
1227 if (m_mode & wxLC_SINGLE_SEL)
1228 {
1229 m_current = line;
1230 HilightAll( FALSE );
1231 m_current->ReverseHilight();
1232 RefreshLine( m_current );
1233 }
1234 else
1235 {
1236 if (event.ShiftDown())
1237 {
1238 m_current = line;
1239 m_current->ReverseHilight();
1240 RefreshLine( m_current );
1241 }
1242 else if (event.ControlDown())
1243 {
1244 m_current = line;
1245
1246 int numOfCurrent = -1;
1247 node = m_lines.First();
1248 while (node)
1249 {
1250 wxListLineData *test_line = (wxListLineData*)node->Data();
1251 numOfCurrent++;
1252 if (test_line == oldCurrent) break;
1253 node = node->Next();
1254 }
1255
1256 int numOfLine = -1;
1257 node = m_lines.First();
1258 while (node)
1259 {
1260 wxListLineData *test_line = (wxListLineData*)node->Data();
1261 numOfLine++;
1262 if (test_line == line) break;
1263 node = node->Next();
1264 }
1265
1266 if (numOfLine < numOfCurrent)
1267 {
1268 int i = numOfLine;
1269 numOfLine = numOfCurrent;
1270 numOfCurrent = i;
1271 }
1272
1273 wxNode *node = m_lines.Nth( numOfCurrent );
1274 for (int i = 0; i <= numOfLine-numOfCurrent; i++)
1275 {
1276 wxListLineData *test_line= (wxListLineData*)node->Data();
1277 test_line->Hilight(TRUE);
1278 RefreshLine( test_line );
1279 node = node->Next();
1280 }
1281 }
1282 else
1283 {
1284 m_current = line;
1285 HilightAll( FALSE );
1286 m_current->ReverseHilight();
1287 RefreshLine( m_current );
1288 }
1289 }
1290 if (m_current != oldCurrent)
1291 {
1292 RefreshLine( oldCurrent );
1293 UnfocusLine( oldCurrent );
1294 FocusLine( m_current );
1295 }
1296 m_lastOnSame = (m_current == oldCurrent);
1297 return;
1298 }
1299 }
1300
1301 void wxListMainWindow::MoveToFocus( void )
1302 {
1303 if (!m_current) return;
1304
1305 int x = 0;
1306 int y = 0;
1307 int w = 0;
1308 int h = 0;
1309 m_current->GetExtent( x, y, w, h );
1310 int w_p = 0;
1311 int h_p = 0;
1312 GetClientSize( &w_p, &h_p );
1313 if (m_mode & wxLC_REPORT)
1314 {
1315 if (GetScrollPos( wxHORIZONTAL ) != 0) SetScrollPos( wxHORIZONTAL, 0);
1316 int y_s = m_yScroll*GetScrollPos( wxVERTICAL );
1317 if ((y > y_s) && (y+h < y_s+h_p)) return;
1318 if (y-y_s < 5) SetScrollPos( wxVERTICAL, (y-5-h_p/2)/m_yScroll );
1319 if (y+h+5 > y_s+h_p) SetScrollPos( wxVERTICAL, (y+h-h_p/2+h+15)/m_yScroll );
1320 m_dirty = TRUE;
1321 }
1322 else
1323 {
1324 if (GetScrollPos( wxVERTICAL ) != 0) SetScrollPos( wxVERTICAL, 0);
1325 int x_s = m_xScroll*GetScrollPos( wxHORIZONTAL );
1326 if ((x > x_s) && (x+w < x_s+w_p)) return;
1327 if (x-x_s < 5) SetScrollPos( wxHORIZONTAL, (x-5)/m_xScroll );
1328 if (x+w-5 > x_s+w_p) SetScrollPos( wxHORIZONTAL, (x+w-w_p+15)/m_xScroll );
1329 m_dirty = TRUE;
1330 }
1331 }
1332
1333 void wxListMainWindow::OnArrowChar( wxListLineData *newCurrent, bool shiftDown )
1334 {
1335 if ((m_mode & wxLC_SINGLE_SEL) || (m_usedKeys == FALSE)) m_current->Hilight( FALSE );
1336 wxListLineData *oldCurrent = m_current;
1337 m_current = newCurrent;
1338 MoveToFocus();
1339 if (shiftDown || (m_mode & wxLC_SINGLE_SEL)) m_current->Hilight( TRUE );
1340 RefreshLine( m_current );
1341 RefreshLine( oldCurrent );
1342 FocusLine( m_current );
1343 UnfocusLine( oldCurrent );
1344 }
1345
1346 void wxListMainWindow::OnChar( wxKeyEvent &event )
1347 {
1348 /*
1349 if (event.KeyCode() == WXK_TAB)
1350 {
1351 if (event.ShiftDown())
1352 TravPrev( &event );
1353 else
1354 TravNext( &event );
1355 return;
1356 }
1357 */
1358 if (!m_current) return;
1359 switch (event.KeyCode())
1360 {
1361 case WXK_UP:
1362 {
1363 wxNode *node = m_lines.Member( m_current )->Previous();
1364 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1365 break;
1366 }
1367 case WXK_DOWN:
1368 {
1369 wxNode *node = m_lines.Member( m_current )->Next();
1370 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1371 break;
1372 }
1373 case WXK_END:
1374 {
1375 wxNode *node = m_lines.Last();
1376 OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1377 break;
1378 }
1379 case WXK_HOME:
1380 {
1381 wxNode *node = m_lines.First();
1382 OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1383 break;
1384 }
1385 case WXK_PRIOR:
1386 {
1387 int steps = 0;
1388 if (m_mode & wxLC_REPORT) { steps = m_visibleLines-1; }
1389 else
1390 {
1391 int pos = 0;
1392 wxNode *node = m_lines.First();
1393 for (;;) { if (m_current == (wxListLineData*)node->Data()) break; pos++; node = node->Next(); }
1394 steps = pos % m_visibleLines;
1395 }
1396 wxNode *node = m_lines.Member( m_current );
1397 for (int i = 0; i < steps; i++) if (node->Previous()) node = node->Previous();
1398 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1399 break;
1400 }
1401 case WXK_NEXT:
1402 {
1403 int steps = 0;
1404 if (m_mode & wxLC_REPORT) { steps = m_visibleLines-1; }
1405 else
1406 {
1407 int pos = 0; wxNode *node = m_lines.First();
1408 for (;;) { if (m_current == (wxListLineData*)node->Data()) break; pos++; node = node->Next(); }
1409 steps = m_visibleLines-(pos % m_visibleLines)-1;
1410 }
1411 wxNode *node = m_lines.Member( m_current );
1412 for (int i = 0; i < steps; i++) if (node->Next()) node = node->Next();
1413 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1414 break;
1415 }
1416 case WXK_LEFT:
1417 {
1418 if (!(m_mode & wxLC_REPORT))
1419 {
1420 wxNode *node = m_lines.Member( m_current );
1421 for (int i = 0; i <m_visibleLines; i++) if (node->Previous()) node = node->Previous();
1422 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1423 }
1424 break;
1425 }
1426 case WXK_RIGHT:
1427 {
1428 if (!(m_mode & wxLC_REPORT))
1429 {
1430 wxNode *node = m_lines.Member( m_current );
1431 for (int i = 0; i <m_visibleLines; i++) if (node->Next()) node = node->Next();
1432 if (node) OnArrowChar( (wxListLineData*)node->Data(), event.ShiftDown() );
1433 }
1434 break;
1435 }
1436 case WXK_SPACE:
1437 {
1438 m_current->ReverseHilight();
1439 RefreshLine( m_current );
1440 }
1441 break;
1442 case WXK_INSERT:
1443 {
1444 if (!(m_mode & wxLC_SINGLE_SEL))
1445 {
1446 wxListLineData *oldCurrent = m_current;
1447 m_current->ReverseHilight();
1448 wxNode *node = m_lines.Member( m_current )->Next();
1449 if (node) m_current = (wxListLineData*)node->Data();
1450 MoveToFocus();
1451 RefreshLine( oldCurrent );
1452 RefreshLine( m_current );
1453 UnfocusLine( oldCurrent );
1454 FocusLine( m_current );
1455 }
1456 }
1457 break;
1458 case WXK_RETURN:
1459 case WXK_EXECUTE:
1460 {
1461 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
1462 le.SetEventObject( GetParent() );
1463 le.m_itemIndex = GetIndexOfLine( m_current );
1464 m_current->GetItem( 0, le.m_item );
1465 GetParent()->GetEventHandler()->ProcessEvent( le );
1466 }
1467 break;
1468 default:
1469 {
1470 event.Skip();
1471 return;
1472 }
1473 }
1474 m_usedKeys = TRUE;
1475 }
1476
1477 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1478 {
1479 m_hasFocus = TRUE;
1480 RefreshLine( m_current );
1481
1482 if (!GetParent()) return;
1483
1484 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
1485 event.SetEventObject( GetParent() );
1486 GetParent()->GetEventHandler()->ProcessEvent( event );
1487 }
1488
1489 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1490 {
1491 m_hasFocus = FALSE;
1492 RefreshLine( m_current );
1493 }
1494
1495 void wxListMainWindow::OnSize( wxSizeEvent &WXUNUSED(event) )
1496 {
1497 /*
1498 We don't even allow the wxScrolledWindow::AdjustScrollbars() call
1499
1500 CalculatePositions();
1501 printf( "OnSize::Refresh.\n" );
1502 Refresh();
1503 event.Skip();
1504 */
1505 }
1506
1507 void wxListMainWindow::DrawImage( int index, wxPaintDC *dc, int x, int y )
1508 {
1509 if ((m_mode & wxLC_ICON) && (m_normal_image_list))
1510 {
1511 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1512 return;
1513 }
1514 if ((m_mode & wxLC_SMALL_ICON) && (m_small_image_list))
1515 {
1516 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1517 }
1518 if ((m_mode & wxLC_REPORT) && (m_small_image_list))
1519 {
1520 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
1521 return;
1522 }
1523 }
1524
1525 void wxListMainWindow::GetImageSize( int index, int &width, int &height )
1526 {
1527 if ((m_mode & wxLC_ICON) && (m_normal_image_list))
1528 {
1529 m_normal_image_list->GetSize( index, width, height );
1530 return;
1531 }
1532 if ((m_mode & wxLC_SMALL_ICON) && (m_small_image_list))
1533 {
1534 m_small_image_list->GetSize( index, width, height );
1535 return;
1536 }
1537 if ((m_mode & wxLC_REPORT) && (m_small_image_list))
1538 {
1539 m_small_image_list->GetSize( index, width, height );
1540 return;
1541 }
1542 width = 0;
1543 height = 0;
1544 }
1545
1546 int wxListMainWindow::GetTextLength( wxString &s )
1547 {
1548 wxPaintDC dc( this );
1549 long lw = 0;
1550 long lh = 0;
1551 dc.GetTextExtent( s, &lw, &lh );
1552 return lw + 6;
1553 }
1554
1555 int wxListMainWindow::GetIndexOfLine( const wxListLineData *line )
1556 {
1557 int i = 0;
1558 wxNode *node = m_lines.First();
1559 while (node)
1560 {
1561 if (line == (wxListLineData*)node->Data()) return i;
1562 i++;
1563 node = node->Next();
1564 }
1565 return -1;
1566 }
1567
1568 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
1569 {
1570 m_dirty = TRUE;
1571 if (which == wxIMAGE_LIST_NORMAL) m_normal_image_list = imageList;
1572 if (which == wxIMAGE_LIST_SMALL) m_small_image_list = imageList;
1573 }
1574
1575 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
1576 {
1577 m_dirty = TRUE;
1578 if (isSmall)
1579 {
1580 m_small_spacing = spacing;
1581 }
1582 else
1583 {
1584 m_normal_spacing = spacing;
1585 }
1586 }
1587
1588 int wxListMainWindow::GetItemSpacing( bool isSmall )
1589 {
1590 if (isSmall) return m_small_spacing; else return m_normal_spacing;
1591 }
1592
1593 void wxListMainWindow::SetColumn( int col, wxListItem &item )
1594 {
1595 m_dirty = TRUE;
1596 wxNode *node = m_columns.Nth( col );
1597 if (node)
1598 {
1599 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER) item.m_width = GetTextLength( item.m_text )+7;
1600 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1601 column->SetItem( item );
1602 }
1603 wxListCtrl *lc = (wxListCtrl*) GetParent();
1604 if (lc->m_headerWin) lc->m_headerWin->Refresh();
1605 }
1606
1607 void wxListMainWindow::SetColumnWidth( int col, int width )
1608 {
1609 if (!(m_mode & wxLC_REPORT)) return;
1610
1611 m_dirty = TRUE;
1612
1613 wxNode *node = m_columns.Nth( col );
1614 if (node)
1615 {
1616 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1617 column->SetWidth( width );
1618 }
1619
1620 node = m_lines.First();
1621 while (node)
1622 {
1623 wxListLineData *line = (wxListLineData*)node->Data();
1624 wxNode *n = line->m_items.Nth( col );
1625 if (n)
1626 {
1627 wxListItemData *item = (wxListItemData*)n->Data();
1628 item->SetSize( width, -1 );
1629 }
1630 node = node->Next();
1631 }
1632
1633 wxListCtrl *lc = (wxListCtrl*) GetParent();
1634 if (lc->m_headerWin) lc->m_headerWin->Refresh();
1635 }
1636
1637 void wxListMainWindow::GetColumn( int col, wxListItem &item )
1638 {
1639 wxNode *node = m_columns.Nth( col );
1640 if (node)
1641 {
1642 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1643 column->GetItem( item );
1644 }
1645 else
1646 {
1647 item.m_format = 0;
1648 item.m_width = 0;
1649 item.m_text = "";
1650 item.m_image = 0;
1651 item.m_data = 0;
1652 }
1653 }
1654
1655 int wxListMainWindow::GetColumnWidth( int col )
1656 {
1657 wxNode *node = m_columns.Nth( col );
1658 if (node)
1659 {
1660 wxListHeaderData *column = (wxListHeaderData*)node->Data();
1661 return column->GetWidth();
1662 }
1663 else
1664 {
1665 return 0;
1666 }
1667 }
1668
1669 int wxListMainWindow::GetColumnCount( void )
1670 {
1671 return m_columns.Number();
1672 }
1673
1674 int wxListMainWindow::GetCountPerPage( void )
1675 {
1676 return m_visibleLines;
1677 }
1678
1679 void wxListMainWindow::SetItem( wxListItem &item )
1680 {
1681 m_dirty = TRUE;
1682 wxNode *node = m_lines.Nth( item.m_itemId );
1683 if (node)
1684 {
1685 wxListLineData *line = (wxListLineData*)node->Data();
1686 if (m_mode & wxLC_REPORT) item.m_width = GetColumnWidth( item.m_col )-3;
1687 line->SetItem( item.m_col, item );
1688 }
1689 }
1690
1691 void wxListMainWindow::SetItemState( long item, long state, long stateMask )
1692 {
1693 // m_dirty = TRUE; no recalcs needed
1694
1695 wxListLineData *oldCurrent = m_current;
1696
1697 if (stateMask & wxLIST_STATE_FOCUSED)
1698 {
1699 wxNode *node = m_lines.Nth( item );
1700 if (node)
1701 {
1702 wxListLineData *line = (wxListLineData*)node->Data();
1703 UnfocusLine( m_current );
1704 m_current = line;
1705 FocusLine( m_current );
1706 RefreshLine( m_current );
1707 RefreshLine( oldCurrent );
1708 }
1709 }
1710
1711 if (stateMask & wxLIST_STATE_SELECTED)
1712 {
1713 bool on = state & wxLIST_STATE_SELECTED;
1714 if (!on && (m_mode & wxLC_SINGLE_SEL)) return;
1715
1716 wxNode *node = m_lines.Nth( item );
1717 if (node)
1718 {
1719 wxListLineData *line = (wxListLineData*)node->Data();
1720 if (m_mode & wxLC_SINGLE_SEL)
1721 {
1722 UnfocusLine( m_current );
1723 m_current = line;
1724 FocusLine( m_current );
1725 oldCurrent->Hilight( FALSE );
1726 RefreshLine( m_current );
1727 RefreshLine( oldCurrent );
1728 }
1729 bool on = state & wxLIST_STATE_SELECTED;
1730 line->Hilight( on );
1731 RefreshLine( line );
1732 }
1733 }
1734 }
1735
1736 int wxListMainWindow::GetItemState( long item, long stateMask )
1737 {
1738 int ret = wxLIST_STATE_DONTCARE;
1739 if (stateMask & wxLIST_STATE_FOCUSED)
1740 {
1741 wxNode *node = m_lines.Nth( item );
1742 if (node)
1743 {
1744 wxListLineData *line = (wxListLineData*)node->Data();
1745 if (line == m_current) ret |= wxLIST_STATE_FOCUSED;
1746 }
1747 }
1748 if (stateMask & wxLIST_STATE_SELECTED)
1749 {
1750 wxNode *node = m_lines.Nth( item );
1751 if (node)
1752 {
1753 wxListLineData *line = (wxListLineData*)node->Data();
1754 if (line->IsHilighted()) ret |= wxLIST_STATE_FOCUSED;
1755 }
1756 }
1757 return ret;
1758 }
1759
1760 void wxListMainWindow::GetItem( wxListItem &item )
1761 {
1762 wxNode *node = m_lines.Nth( item.m_itemId );
1763 if (node)
1764 {
1765 wxListLineData *line = (wxListLineData*)node->Data();
1766 line->GetItem( item.m_col, item );
1767 }
1768 else
1769 {
1770 item.m_mask = 0;
1771 item.m_text = "";
1772 item.m_image = 0;
1773 item.m_data = 0;
1774 }
1775 }
1776
1777 int wxListMainWindow::GetItemCount( void )
1778 {
1779 return m_lines.Number();
1780 }
1781
1782 void wxListMainWindow::GetItemRect( long index, wxRectangle &rect )
1783 {
1784 wxNode *node = m_lines.Nth( index );
1785 if (node)
1786 {
1787 wxListLineData *line = (wxListLineData*)node->Data();
1788 line->GetRect( rect );
1789 }
1790 else
1791 {
1792 rect.x = 0;
1793 rect.y = 0;
1794 rect.width = 0;
1795 rect.height = 0;
1796 }
1797 }
1798
1799 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos)
1800 {
1801 wxNode *node = m_lines.Nth( item );
1802 if (node)
1803 {
1804 wxRectangle rect;
1805 wxListLineData *line = (wxListLineData*)node->Data();
1806 line->GetRect( rect );
1807 pos.x = rect.x;
1808 pos.y = rect.y;
1809 }
1810 else
1811 {
1812 pos.x = 0;
1813 pos.y = 0;
1814 }
1815 return TRUE;
1816 }
1817
1818 int wxListMainWindow::GetSelectedItemCount( void )
1819 {
1820 int ret = 0;
1821 wxNode *node = m_lines.First();
1822 while (node)
1823 {
1824 wxListLineData *line = (wxListLineData*)node->Data();
1825 if (line->IsHilighted()) ret++;
1826 node = node->Next();
1827 }
1828 return ret;
1829 }
1830
1831 void wxListMainWindow::SetMode( long mode )
1832 {
1833 m_dirty = TRUE;
1834 m_mode = mode;
1835
1836 DeleteEverything();
1837
1838 if (m_mode & wxLC_REPORT)
1839 {
1840 m_xScroll = 0;
1841 m_yScroll = 15;
1842 }
1843 else
1844 {
1845 m_xScroll = 15;
1846 m_yScroll = 0;
1847 }
1848 }
1849
1850 long wxListMainWindow::GetMode( void ) const
1851 {
1852 return m_mode;
1853 }
1854
1855 void wxListMainWindow::CalculatePositions( void )
1856 {
1857 if (!m_lines.First()) return;
1858
1859 wxPaintDC dc( this );
1860 dc.SetFont( GetFont() );
1861
1862 int iconSpacing = 0;
1863 if (m_mode & wxLC_ICON) iconSpacing = m_normal_spacing;
1864 if (m_mode & wxLC_SMALL_ICON) iconSpacing = m_small_spacing;
1865
1866 // we take the first line (which also can be an icon or
1867 // an a text item in wxLC_ICON and wxLC_LIST modes) to
1868 // measure the size of the line
1869
1870 int lineWidth = 0;
1871 int lineHeight = 0;
1872 int lineSpacing = 0;
1873
1874 wxListLineData *line = (wxListLineData*)m_lines.First()->Data();
1875 line->CalculateSize( &dc, iconSpacing );
1876 int dummy = 0;
1877 line->GetSize( dummy, lineSpacing );
1878 lineSpacing += 4;
1879
1880 int clientWidth = 0;
1881 int clientHeight = 0;
1882
1883 if (m_mode & wxLC_REPORT)
1884 {
1885 int x = 4;
1886 int y = 1;
1887 int entireHeight = m_lines.Number() * lineSpacing + 2;
1888 int scroll_pos = GetScrollPos( wxVERTICAL );
1889 SetScrollbars( m_xScroll, m_yScroll, 0, (entireHeight+15-6) / m_yScroll, 0, scroll_pos, TRUE );
1890 GetClientSize( &clientWidth, &clientHeight );
1891
1892 wxNode* node = m_lines.First();
1893 while (node)
1894 {
1895 wxListLineData *line = (wxListLineData*)node->Data();
1896 line->CalculateSize( &dc, iconSpacing );
1897 line->SetPosition( &dc, x, y, clientWidth );
1898 int col_x = 2;
1899 for (int i = 0; i < GetColumnCount(); i++)
1900 {
1901 line->SetColumnPosition( i, col_x );
1902 col_x += GetColumnWidth( i );
1903 }
1904 y += lineSpacing; // one pixel blank line between items
1905 node = node->Next();
1906 }
1907 m_visibleLines = m_lines.GetCount();
1908 }
1909 else
1910 {
1911 // at first we try without any scrollbar. if the items don't
1912 // fit into the window, we recalculate after subtracting an
1913 // approximated 15 pt for the horizontal scrollbar
1914
1915 GetSize( &clientWidth, &clientHeight );
1916 clientHeight -= 4; // sunken frame
1917
1918 int entireWidth = 0;
1919
1920 for (int tries = 0; tries < 2; tries++)
1921 {
1922 entireWidth = 0;
1923 int x = 5; // painting is done at x-2
1924 int y = 5; // painting is done at y-2
1925 int maxWidth = 0;
1926 wxNode *node = m_lines.First();
1927 while (node)
1928 {
1929 wxListLineData *line = (wxListLineData*)node->Data();
1930 line->CalculateSize( &dc, iconSpacing );
1931 line->SetPosition( &dc, x, y, clientWidth );
1932 line->GetSize( lineWidth, lineHeight );
1933 if (lineWidth > maxWidth) maxWidth = lineWidth;
1934 y += lineSpacing;
1935 if (y+lineSpacing-7 >= clientHeight) // -7 for earlier "line breaking"
1936 {
1937 y = 5;
1938 x += maxWidth+5;
1939 entireWidth += maxWidth+5;
1940 maxWidth = 0;
1941 }
1942 node = node->Next();
1943 if (!node) entireWidth += maxWidth;
1944 if ((tries == 0) && (entireWidth > clientWidth))
1945 {
1946 clientHeight -= 15; // scrollbar height
1947 break;
1948 }
1949 if (!node) tries = 1; // everything fits, no second try required
1950 }
1951 }
1952 m_visibleLines = (clientHeight+7) / (lineSpacing); // +7 for earlier "line breaking"
1953
1954 int scroll_pos = GetScrollPos( wxHORIZONTAL );
1955 SetScrollbars( m_xScroll, m_yScroll, (entireWidth+15) / m_xScroll, 0, scroll_pos, 0, TRUE );
1956 }
1957 }
1958
1959 void wxListMainWindow::RealizeChanges( void )
1960 {
1961 if (!m_current)
1962 {
1963 wxNode *node = m_lines.First();
1964 if (node) m_current = (wxListLineData*)node->Data();
1965 }
1966 if (m_current)
1967 {
1968 FocusLine( m_current );
1969 if (m_mode & wxLC_SINGLE_SEL) m_current->Hilight( TRUE );
1970 }
1971 }
1972
1973 long wxListMainWindow::GetNextItem( long item, int WXUNUSED(geometry), int state )
1974 {
1975 long ret = 0;
1976 if (item > 0) ret = item;
1977 wxNode *node = m_lines.Nth( ret );
1978 while (node)
1979 {
1980 wxListLineData *line = (wxListLineData*)node->Data();
1981 if ((state & wxLIST_STATE_FOCUSED) && (line == m_current)) return ret;
1982 if ((state & wxLIST_STATE_SELECTED) && (line->IsHilighted())) return ret;
1983 if (!state) return ret;
1984 ret++;
1985 node = node->Next();
1986 }
1987 return -1;
1988 }
1989
1990 void wxListMainWindow::DeleteItem( long index )
1991 {
1992 m_dirty = TRUE;
1993 wxNode *node = m_lines.Nth( index );
1994 if (node)
1995 {
1996 wxListLineData *line = (wxListLineData*)node->Data();
1997 if (m_current == line) m_current = (wxListLineData *) NULL;
1998 DeleteLine( line );
1999 m_lines.DeleteNode( node );
2000 }
2001 }
2002
2003 void wxListMainWindow::DeleteColumn( int col )
2004 {
2005 wxCHECK_RET( col < (int)m_columns.GetCount(),
2006 "attempting to delete inexistent column in wxListView" );
2007
2008 m_dirty = TRUE;
2009 wxNode *node = m_columns.Nth( col );
2010 if (node) m_columns.DeleteNode( node );
2011 }
2012
2013 void wxListMainWindow::DeleteAllItems( void )
2014 {
2015 m_dirty = TRUE;
2016 m_current = (wxListLineData *) NULL;
2017 wxNode *node = m_lines.First();
2018 while (node)
2019 {
2020 wxListLineData *line = (wxListLineData*)node->Data();
2021 DeleteLine( line );
2022 node = node->Next();
2023 }
2024 m_lines.Clear();
2025 }
2026
2027 void wxListMainWindow::DeleteEverything( void )
2028 {
2029 m_dirty = TRUE;
2030 m_current = (wxListLineData *) NULL;
2031 wxNode *node = m_lines.First();
2032 while (node)
2033 {
2034 wxListLineData *line = (wxListLineData*)node->Data();
2035 DeleteLine( line );
2036 node = node->Next();
2037 }
2038 m_lines.Clear();
2039 m_current = (wxListLineData *) NULL;
2040 m_columns.Clear();
2041 }
2042
2043 void wxListMainWindow::EnsureVisible( long index )
2044 {
2045 wxListLineData *oldCurrent = m_current;
2046 m_current = (wxListLineData *) NULL;
2047 int i = index;
2048 wxNode *node = m_lines.Nth( i );
2049 if (node) m_current = (wxListLineData*)node->Data();
2050 if (m_current) MoveToFocus();
2051 m_current = oldCurrent;
2052 }
2053
2054 long wxListMainWindow::FindItem(long start, const wxString& str, bool WXUNUSED(partial) )
2055 {
2056 long pos = start;
2057 wxString tmp = str;
2058 if (pos < 0) pos = 0;
2059 wxNode *node = m_lines.Nth( pos );
2060 while (node)
2061 {
2062 wxListLineData *line = (wxListLineData*)node->Data();
2063 wxString s = "";
2064 line->GetText( 0, s );
2065 if (s == tmp) return pos;
2066 node = node->Next();
2067 pos++;
2068 }
2069 return -1;
2070 }
2071
2072 long wxListMainWindow::FindItem(long start, long data)
2073 {
2074 long pos = start;
2075 if (pos < 0) pos = 0;
2076 wxNode *node = m_lines.Nth( pos );
2077 while (node)
2078 {
2079 wxListLineData *line = (wxListLineData*)node->Data();
2080 wxListItem item;
2081 line->GetItem( 0, item );
2082 if (item.m_data == data) return pos;
2083 node = node->Next();
2084 pos++;
2085 }
2086 return -1;
2087 }
2088
2089 long wxListMainWindow::HitTest( int x, int y, int &flags )
2090 {
2091 wxNode *node = m_lines.First();
2092 int count = 0;
2093 while (node)
2094 {
2095 wxListLineData *line = (wxListLineData*)node->Data();
2096 long ret = line->IsHit( x, y );
2097 if (ret & flags)
2098 {
2099 flags = ret;
2100 return count;
2101 }
2102 node = node->Next();
2103 count++;
2104 }
2105 return -1;
2106 }
2107
2108 void wxListMainWindow::InsertItem( wxListItem &item )
2109 {
2110 m_dirty = TRUE;
2111 int mode = 0;
2112 if (m_mode & wxLC_REPORT) mode = wxLC_REPORT;
2113 else if (m_mode & wxLC_LIST) mode = wxLC_LIST;
2114 else if (m_mode & wxLC_ICON) mode = wxLC_ICON;
2115 else if (m_mode & wxLC_SMALL_ICON) mode = wxLC_ICON; // no typo
2116 wxListLineData *line = new wxListLineData( this, mode, m_hilightBrush );
2117 if (m_mode & wxLC_REPORT)
2118 {
2119 line->InitItems( GetColumnCount() );
2120 item.m_width = GetColumnWidth( 0 )-3;
2121 }
2122 else
2123 line->InitItems( 1 );
2124 line->SetItem( 0, item );
2125 if ((item.m_itemId >= 0) && (item.m_itemId < (int)m_lines.GetCount()))
2126 {
2127 wxNode *node = m_lines.Nth( item.m_itemId );
2128 if (node) m_lines.Insert( node, line );
2129 }
2130 else
2131 {
2132 m_lines.Append( line );
2133 }
2134 }
2135
2136 void wxListMainWindow::InsertColumn( long col, wxListItem &item )
2137 {
2138 m_dirty = TRUE;
2139 if (m_mode & wxLC_REPORT)
2140 {
2141 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER) item.m_width = GetTextLength( item.m_text );
2142 wxListHeaderData *column = new wxListHeaderData( item );
2143 if ((col >= 0) && (col < (int)m_columns.GetCount()))
2144 {
2145 wxNode *node = m_columns.Nth( col );
2146 if (node)
2147 m_columns.Insert( node, column );
2148 }
2149 else
2150 {
2151 m_columns.Append( column );
2152 }
2153 }
2154 }
2155
2156 wxListCtrlCompare list_ctrl_compare_func_2;
2157 long list_ctrl_compare_data;
2158
2159 int list_ctrl_compare_func_1( const void *arg1, const void *arg2 )
2160 {
2161 wxListLineData *line1 = *((wxListLineData**)arg1);
2162 wxListLineData *line2 = *((wxListLineData**)arg2);
2163 wxListItem item;
2164 line1->GetItem( 0, item );
2165 long data1 = item.m_data;
2166 line2->GetItem( 0, item );
2167 long data2 = item.m_data;
2168 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
2169 }
2170
2171 void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data )
2172 {
2173 list_ctrl_compare_func_2 = fn;
2174 list_ctrl_compare_data = data;
2175 m_lines.Sort( list_ctrl_compare_func_1 );
2176 }
2177
2178 // -------------------------------------------------------------------------------------
2179 // wxListItem
2180 // -------------------------------------------------------------------------------------
2181
2182 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
2183
2184 wxListItem::wxListItem(void)
2185 {
2186 m_mask = 0;
2187 m_itemId = 0;
2188 m_col = 0;
2189 m_state = 0;
2190 m_stateMask = 0;
2191 m_image = 0;
2192 m_data = 0;
2193 m_format = wxLIST_FORMAT_CENTRE;
2194 m_width = 0;
2195 m_colour = wxBLACK;
2196 }
2197
2198 // -------------------------------------------------------------------------------------
2199 // wxListEvent
2200 // -------------------------------------------------------------------------------------
2201
2202 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
2203
2204 wxListEvent::wxListEvent( wxEventType commandType, int id ):
2205 wxNotifyEvent( commandType, id )
2206 {
2207 m_code = 0;
2208 m_itemIndex = 0;
2209 m_oldItemIndex = 0;
2210 m_col = 0;
2211 m_cancelled = FALSE;
2212 m_pointDrag.x = 0;
2213 m_pointDrag.y = 0;
2214 }
2215
2216 // -------------------------------------------------------------------------------------
2217 // wxListCtrl
2218 // -------------------------------------------------------------------------------------
2219
2220 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
2221
2222 BEGIN_EVENT_TABLE(wxListCtrl,wxControl)
2223 EVT_SIZE (wxListCtrl::OnSize)
2224 EVT_IDLE (wxListCtrl::OnIdle)
2225 END_EVENT_TABLE()
2226
2227 wxListCtrl::wxListCtrl(void)
2228 {
2229 m_imageListNormal = (wxImageList *) NULL;
2230 m_imageListSmall = (wxImageList *) NULL;
2231 m_imageListState = (wxImageList *) NULL;
2232 m_mainWin = (wxListMainWindow*) NULL;
2233 m_headerWin = (wxListHeaderWindow*) NULL;
2234 }
2235
2236 wxListCtrl::~wxListCtrl(void)
2237 {
2238 }
2239
2240 bool wxListCtrl::Create( wxWindow *parent, wxWindowID id,
2241 const wxPoint &pos, const wxSize &size,
2242 long style, const wxValidator &validator,
2243 const wxString &name )
2244 {
2245 m_imageListNormal = (wxImageList *) NULL;
2246 m_imageListSmall = (wxImageList *) NULL;
2247 m_imageListState = (wxImageList *) NULL;
2248 m_mainWin = (wxListMainWindow*) NULL;
2249 m_headerWin = (wxListHeaderWindow*) NULL;
2250
2251 long s = style;
2252
2253 if ((s & wxLC_REPORT == 0) &&
2254 (s & wxLC_LIST == 0) &&
2255 (s & wxLC_ICON == 0))
2256 s = s | wxLC_LIST;
2257
2258 bool ret = wxControl::Create( parent, id, pos, size, s, name );
2259
2260 SetValidator( validator );
2261
2262 if (s & wxSUNKEN_BORDER) s -= wxSUNKEN_BORDER;
2263
2264 m_mainWin = new wxListMainWindow( this, -1, wxPoint(0,0), size, s );
2265
2266 if (GetWindowStyleFlag() & wxLC_REPORT)
2267 m_headerWin = new wxListHeaderWindow( this, -1, m_mainWin, wxPoint(0,0), wxSize(size.x,23) );
2268 else
2269 m_headerWin = (wxListHeaderWindow *) NULL;
2270
2271 return ret;
2272 }
2273
2274 void wxListCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
2275 {
2276 // handled in OnIdle
2277
2278 if (m_mainWin) m_mainWin->m_dirty = TRUE;
2279 }
2280
2281 void wxListCtrl::SetSingleStyle( long style, bool add )
2282 {
2283 long flag = GetWindowStyleFlag();
2284
2285 if (add)
2286 {
2287 if (style & wxLC_MASK_TYPE) flag = flag & ~wxLC_MASK_TYPE;
2288 if (style & wxLC_MASK_ALIGN) flag = flag & ~wxLC_MASK_ALIGN;
2289 if (style & wxLC_MASK_SORT) flag = flag & ~wxLC_MASK_SORT;
2290 }
2291
2292 if (add)
2293 {
2294 flag |= style;
2295 }
2296 else
2297 {
2298 if (flag & style) flag -= style;
2299 }
2300
2301 SetWindowStyleFlag( flag );
2302 }
2303
2304 void wxListCtrl::SetWindowStyleFlag( long flag )
2305 {
2306 m_mainWin->DeleteEverything();
2307
2308 int width = 0;
2309 int height = 0;
2310 GetClientSize( &width, &height );
2311
2312 m_mainWin->SetMode( flag );
2313
2314 if (flag & wxLC_REPORT)
2315 {
2316 if (!(GetWindowStyleFlag() & wxLC_REPORT))
2317 {
2318 // m_mainWin->SetSize( 0, 24, width, height-24 );
2319 if (!m_headerWin)
2320 {
2321 m_headerWin = new wxListHeaderWindow( this, -1, m_mainWin, wxPoint(0,0), wxSize(width,23) );
2322 }
2323 else
2324 {
2325 // m_headerWin->SetSize( 0, 0, width, 23 );
2326 m_headerWin->Show( TRUE );
2327 }
2328 }
2329 }
2330 else
2331 {
2332 if (GetWindowStyleFlag() & wxLC_REPORT)
2333 {
2334 // m_mainWin->SetSize( 0, 0, width, height );
2335 m_headerWin->Show( FALSE );
2336 }
2337 }
2338
2339 wxWindow::SetWindowStyleFlag( flag );
2340 }
2341
2342 bool wxListCtrl::GetColumn(int col, wxListItem &item) const
2343 {
2344 m_mainWin->GetColumn( col, item );
2345 return TRUE;
2346 }
2347
2348 bool wxListCtrl::SetColumn( int col, wxListItem& item )
2349 {
2350 m_mainWin->SetColumn( col, item );
2351 return TRUE;
2352 }
2353
2354 int wxListCtrl::GetColumnWidth( int col ) const
2355 {
2356 return m_mainWin->GetColumnWidth( col );
2357 }
2358
2359 bool wxListCtrl::SetColumnWidth( int col, int width )
2360 {
2361 m_mainWin->SetColumnWidth( col, width );
2362 return TRUE;
2363 }
2364
2365 int wxListCtrl::GetCountPerPage(void) const
2366 {
2367 return m_mainWin->GetCountPerPage(); // different from Windows ?
2368 }
2369
2370 /*
2371 wxText& wxListCtrl::GetEditControl(void) const
2372 {
2373 }
2374 */
2375
2376 bool wxListCtrl::GetItem( wxListItem &info ) const
2377 {
2378 m_mainWin->GetItem( info );
2379 return TRUE;
2380 }
2381
2382 bool wxListCtrl::SetItem( wxListItem &info )
2383 {
2384 m_mainWin->SetItem( info );
2385 return TRUE;
2386 }
2387
2388 long wxListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
2389 {
2390 wxListItem info;
2391 info.m_text = label;
2392 info.m_mask = wxLIST_MASK_TEXT;
2393 info.m_itemId = index;
2394 info.m_col = col;
2395 if ( imageId > -1 )
2396 {
2397 info.m_image = imageId;
2398 info.m_mask |= wxLIST_MASK_IMAGE;
2399 }
2400 ;
2401 m_mainWin->SetItem(info);
2402 return TRUE;
2403 }
2404
2405 int wxListCtrl::GetItemState( long item, long stateMask ) const
2406 {
2407 return m_mainWin->GetItemState( item, stateMask );
2408 }
2409
2410 bool wxListCtrl::SetItemState( long item, long state, long stateMask )
2411 {
2412 m_mainWin->SetItemState( item, state, stateMask );
2413 return TRUE;
2414 }
2415
2416 bool wxListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
2417 {
2418 wxListItem info;
2419 info.m_image = image;
2420 info.m_mask = wxLIST_MASK_IMAGE;
2421 info.m_itemId = item;
2422 m_mainWin->SetItem( info );
2423 return TRUE;
2424 }
2425
2426 wxString wxListCtrl::GetItemText( long item ) const
2427 {
2428 wxListItem info;
2429 info.m_itemId = item;
2430 m_mainWin->GetItem( info );
2431 return info.m_text;
2432 }
2433
2434 void wxListCtrl::SetItemText( long item, const wxString &str )
2435 {
2436 wxListItem info;
2437 info.m_mask = wxLIST_MASK_TEXT;
2438 info.m_itemId = item;
2439 info.m_text = str;
2440 m_mainWin->SetItem( info );
2441 }
2442
2443 long wxListCtrl::GetItemData( long item ) const
2444 {
2445 wxListItem info;
2446 info.m_itemId = item;
2447 m_mainWin->GetItem( info );
2448 return info.m_data;
2449 }
2450
2451 bool wxListCtrl::SetItemData( long item, long data )
2452 {
2453 wxListItem info;
2454 info.m_mask = wxLIST_MASK_DATA;
2455 info.m_itemId = item;
2456 info.m_data = data;
2457 m_mainWin->SetItem( info );
2458 return TRUE;
2459 }
2460
2461 bool wxListCtrl::GetItemRect( long item, wxRectangle &rect, int WXUNUSED(code) ) const
2462 {
2463 m_mainWin->GetItemRect( item, rect );
2464 return TRUE;
2465 }
2466
2467 bool wxListCtrl::GetItemPosition( long item, wxPoint& pos ) const
2468 {
2469 m_mainWin->GetItemPosition( item, pos );
2470 return TRUE;
2471 }
2472
2473 bool wxListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
2474 {
2475 return 0;
2476 }
2477
2478 int wxListCtrl::GetItemCount(void) const
2479 {
2480 return m_mainWin->GetItemCount();
2481 }
2482
2483 int wxListCtrl::GetColumnCount(void) const
2484 {
2485 return m_mainWin->GetColumnCount();
2486 }
2487
2488 void wxListCtrl::SetItemSpacing( int spacing, bool isSmall )
2489 {
2490 m_mainWin->SetItemSpacing( spacing, isSmall );
2491 }
2492
2493 int wxListCtrl::GetItemSpacing( bool isSmall ) const
2494 {
2495 return m_mainWin->GetItemSpacing( isSmall );
2496 }
2497
2498 int wxListCtrl::GetSelectedItemCount(void) const
2499 {
2500 return m_mainWin->GetSelectedItemCount();
2501 }
2502
2503 /*
2504 wxColour wxListCtrl::GetTextColour(void) const
2505 {
2506 }
2507
2508 void wxListCtrl::SetTextColour(const wxColour& WXUNUSED(col))
2509 {
2510 }
2511 */
2512
2513 long wxListCtrl::GetTopItem(void) const
2514 {
2515 return 0;
2516 }
2517
2518 long wxListCtrl::GetNextItem( long item, int geom, int state ) const
2519 {
2520 return m_mainWin->GetNextItem( item, geom, state );
2521 }
2522
2523 wxImageList *wxListCtrl::GetImageList(int which) const
2524 {
2525 if (which == wxIMAGE_LIST_NORMAL)
2526 {
2527 return m_imageListNormal;
2528 }
2529 else if (which == wxIMAGE_LIST_SMALL)
2530 {
2531 return m_imageListSmall;
2532 }
2533 else if (which == wxIMAGE_LIST_STATE)
2534 {
2535 return m_imageListState;
2536 }
2537 return (wxImageList *) NULL;
2538 }
2539
2540 void wxListCtrl::SetImageList( wxImageList *imageList, int which )
2541 {
2542 m_mainWin->SetImageList( imageList, which );
2543 }
2544
2545 bool wxListCtrl::Arrange( int WXUNUSED(flag) )
2546 {
2547 return 0;
2548 }
2549
2550 bool wxListCtrl::DeleteItem( long item )
2551 {
2552 m_mainWin->DeleteItem( item );
2553 return TRUE;
2554 }
2555
2556 bool wxListCtrl::DeleteAllItems(void)
2557 {
2558 m_mainWin->DeleteAllItems();
2559 return TRUE;
2560 }
2561
2562 void wxListCtrl::DeleteAllColumns()
2563 {
2564 for ( size_t n = 0; n < m_mainWin->m_columns.GetCount(); n++ )
2565 DeleteColumn(n);
2566 }
2567
2568 bool wxListCtrl::DeleteColumn( int col )
2569 {
2570 m_mainWin->DeleteColumn( col );
2571 return TRUE;
2572 }
2573
2574 /*
2575 wxText& wxListCtrl::Edit( long WXUNUSED(item ) )
2576 {
2577 }
2578 */
2579
2580 bool wxListCtrl::EnsureVisible( long item )
2581 {
2582 m_mainWin->EnsureVisible( item );
2583 return TRUE;
2584 }
2585
2586 long wxListCtrl::FindItem( long start, const wxString& str, bool partial )
2587 {
2588 return m_mainWin->FindItem( start, str, partial );
2589 }
2590
2591 long wxListCtrl::FindItem( long start, long data )
2592 {
2593 return m_mainWin->FindItem( start, data );
2594 }
2595
2596 long wxListCtrl::FindItem( long WXUNUSED(start), const wxPoint& WXUNUSED(pt),
2597 int WXUNUSED(direction))
2598 {
2599 return 0;
2600 }
2601
2602 long wxListCtrl::HitTest( const wxPoint &point, int &flags )
2603 {
2604 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
2605 }
2606
2607 long wxListCtrl::InsertItem( wxListItem& info )
2608 {
2609 m_mainWin->InsertItem( info );
2610 return 0;
2611 }
2612
2613 long wxListCtrl::InsertItem( long index, const wxString &label )
2614 {
2615 wxListItem info;
2616 info.m_text = label;
2617 info.m_mask = wxLIST_MASK_TEXT;
2618 info.m_itemId = index;
2619 return InsertItem( info );
2620 }
2621
2622 long wxListCtrl::InsertItem( long index, int imageIndex )
2623 {
2624 wxListItem info;
2625 info.m_mask = wxLIST_MASK_IMAGE;
2626 info.m_image = imageIndex;
2627 info.m_itemId = index;
2628 return InsertItem( info );
2629 }
2630
2631 long wxListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
2632 {
2633 wxListItem info;
2634 info.m_text = label;
2635 info.m_image = imageIndex;
2636 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
2637 info.m_itemId = index;
2638 return InsertItem( info );
2639 }
2640
2641 long wxListCtrl::InsertColumn( long col, wxListItem &item )
2642 {
2643 m_mainWin->InsertColumn( col, item );
2644 return 0;
2645 }
2646
2647 long wxListCtrl::InsertColumn( long col, const wxString &heading,
2648 int format, int width )
2649 {
2650 wxListItem item;
2651 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
2652 item.m_text = heading;
2653 if (width >= -2)
2654 {
2655 item.m_mask |= wxLIST_MASK_WIDTH;
2656 item.m_width = width;
2657 }
2658 ;
2659 item.m_format = format;
2660
2661 return InsertColumn( col, item );
2662 }
2663
2664 bool wxListCtrl::ScrollList( int WXUNUSED(dx), int WXUNUSED(dy) )
2665 {
2666 return 0;
2667 }
2668
2669 // Sort items.
2670 // fn is a function which takes 3 long arguments: item1, item2, data.
2671 // item1 is the long data associated with a first item (NOT the index).
2672 // item2 is the long data associated with a second item (NOT the index).
2673 // data is the same value as passed to SortItems.
2674 // The return value is a negative number if the first item should precede the second
2675 // item, a positive number of the second item should precede the first,
2676 // or zero if the two items are equivalent.
2677 // data is arbitrary data to be passed to the sort function.
2678
2679 bool wxListCtrl::SortItems( wxListCtrlCompare fn, long data )
2680 {
2681 m_mainWin->SortItems( fn, data );
2682 return TRUE;
2683 }
2684
2685 void wxListCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2686 {
2687 if (!m_mainWin->m_dirty) return;
2688
2689 int cw = 0;
2690 int ch = 0;
2691 GetClientSize( &cw, &ch );
2692
2693 int x = 0;
2694 int y = 0;
2695 int w = 0;
2696 int h = 0;
2697
2698 if (GetWindowStyleFlag() & wxLC_REPORT)
2699 {
2700 m_headerWin->GetPosition( &x, &y );
2701 m_headerWin->GetSize( &w, &h );
2702 if ((x != 0) || (y != 0) || (w != cw) || (h != 23))
2703 m_headerWin->SetSize( 0, 0, cw, 23 );
2704
2705 m_mainWin->GetPosition( &x, &y );
2706 m_mainWin->GetSize( &w, &h );
2707 if ((x != 0) || (y != 24) || (w != cw) || (h != ch-24))
2708 m_mainWin->SetSize( 0, 24, cw, ch-24 );
2709 }
2710 else
2711 {
2712 m_mainWin->GetPosition( &x, &y );
2713 m_mainWin->GetSize( &w, &h );
2714 if ((x != 0) || (y != 24) || (w != cw) || (h != ch))
2715 m_mainWin->SetSize( 0, 0, cw, ch );
2716 }
2717
2718 m_mainWin->CalculatePositions();
2719 m_mainWin->RealizeChanges();
2720 m_mainWin->m_dirty = FALSE;
2721 m_mainWin->Refresh();
2722 }
2723
2724 void wxListCtrl::SetBackgroundColour( const wxColour &colour )
2725 {
2726 if (m_mainWin)
2727 {
2728 m_mainWin->SetBackgroundColour( colour );
2729 m_mainWin->m_dirty = TRUE;
2730 }
2731 if (m_headerWin)
2732 m_headerWin->SetBackgroundColour( colour );
2733 }
2734
2735 void wxListCtrl::SetForegroundColour( const wxColour &colour )
2736 {
2737 if (m_mainWin)
2738 {
2739 m_mainWin->SetForegroundColour( colour );
2740 m_mainWin->m_dirty = TRUE;
2741 }
2742 if (m_headerWin)
2743 m_headerWin->SetForegroundColour( colour );
2744 }
2745
2746 void wxListCtrl::SetFont( const wxFont &font )
2747 {
2748 if (m_mainWin)
2749 {
2750 m_mainWin->SetFont( font );
2751 m_mainWin->m_dirty = TRUE;
2752 }
2753 if (m_headerWin)
2754 m_headerWin->SetFont( font );
2755 }
2756