]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/listbox.cpp
Corrected bug in DoSetSize() (wxALLOW_MINUS_ONE)
[wxWidgets.git] / src / gtk / listbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: listbox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "listbox.h"
13 #endif
14
15 #include "wx/dynarray.h"
16 #include "wx/listbox.h"
17 #include "wx/utils.h"
18 #include "wx/intl.h"
19 #include "wx/checklst.h"
20 #include "wx/settings.h"
21
22 #if wxUSE_TOOLTIPS
23 #include "wx/tooltip.h"
24 #endif
25
26 #if wxUSE_DRAG_AND_DROP
27 #include "wx/dnd.h"
28 #endif
29
30 #include "gdk/gdk.h"
31 #include "gtk/gtk.h"
32
33 //-----------------------------------------------------------------------------
34 // idle system
35 //-----------------------------------------------------------------------------
36
37 extern void wxapp_install_idle_handler();
38 extern bool g_isIdle;
39
40 //-------------------------------------------------------------------------
41 // conditional compilation
42 //-------------------------------------------------------------------------
43
44 #if (GTK_MINOR_VERSION > 0)
45 #define NEW_GTK_SCROLL_CODE
46 #endif
47
48 //-----------------------------------------------------------------------------
49 // private functions
50 //-----------------------------------------------------------------------------
51
52 #if wxUSE_CHECKLISTBOX
53
54 #define CHECKBOX_STRING "[-] "
55
56 // checklistboxes have "[±] " prepended to their lables, this macro removes it
57 // (NB: 4 below is the length of CHECKBOX_STRING above)
58 //
59 // the argument to it is a "const char *" pointer
60 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
61
62 #else // !wxUSE_CHECKLISTBOX
63
64 #define GET_REAL_LABEL(label) (label)
65
66 #endif // wxUSE_CHECKLISTBOX
67
68 //-----------------------------------------------------------------------------
69 // data
70 //-----------------------------------------------------------------------------
71
72 extern bool g_blockEventsOnDrag;
73 extern bool g_blockEventsOnScroll;
74
75 static bool g_hasDoubleClicked = FALSE;
76
77 //-----------------------------------------------------------------------------
78 // "button_release_event"
79 //-----------------------------------------------------------------------------
80
81 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
82 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
83 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
84 this can lead to race conditions so that we emit the dclick event
85 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
86
87 static gint
88 gtk_listbox_button_release_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxListBox *listbox )
89 {
90 if (g_isIdle) wxapp_install_idle_handler();
91
92 if (g_blockEventsOnDrag) return FALSE;
93 if (g_blockEventsOnScroll) return FALSE;
94
95 if (!listbox->m_hasVMT) return FALSE;
96
97 if (!g_hasDoubleClicked) return FALSE;
98
99 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
100 event.SetEventObject( listbox );
101
102 wxArrayInt aSelections;
103 int count = listbox->GetSelections(aSelections);
104 if ( count > 0 )
105 {
106 event.m_commandInt = aSelections[0] ;
107 event.m_clientData = listbox->GetClientData( event.m_commandInt );
108 wxString str(listbox->GetString(event.m_commandInt));
109 if (!str.IsEmpty()) event.m_commandString = str;
110 }
111 else
112 {
113 event.m_commandInt = -1 ;
114 event.m_commandString.Empty();
115 }
116
117 listbox->GetEventHandler()->ProcessEvent( event );
118
119 return FALSE;
120 }
121
122 //-----------------------------------------------------------------------------
123 // "button_press_event"
124 //-----------------------------------------------------------------------------
125
126 static gint
127 gtk_listbox_button_press_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxListBox *listbox )
128 {
129 if (g_isIdle) wxapp_install_idle_handler();
130
131 if (g_blockEventsOnDrag) return FALSE;
132 if (g_blockEventsOnScroll) return FALSE;
133
134 if (!listbox->m_hasVMT) return FALSE;
135
136 int sel = listbox->GetIndex( widget );
137
138 #if wxUSE_CHECKLISTBOX
139 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
140 {
141 wxCheckListBox *clb = (wxCheckListBox *)listbox;
142
143 clb->Check( sel, !clb->IsChecked(sel) );
144
145 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
146 event.SetEventObject( listbox );
147 event.SetInt( sel );
148 listbox->GetEventHandler()->ProcessEvent( event );
149 }
150 #endif // wxUSE_CHECKLISTBOX
151
152 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
153 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
154
155 return FALSE;
156 }
157
158 //-----------------------------------------------------------------------------
159 // "key_press_event"
160 //-----------------------------------------------------------------------------
161
162 static gint
163 gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
164 {
165 if (g_isIdle) wxapp_install_idle_handler();
166
167 if (g_blockEventsOnDrag) return FALSE;
168
169 if (!listbox->m_hasVMT) return FALSE;
170
171 if (gdk_event->keyval != ' ') return FALSE;
172
173 #if wxUSE_CHECKLISTBOX
174 int sel = listbox->GetIndex( widget );
175
176 wxCheckListBox *clb = (wxCheckListBox *)listbox;
177
178 clb->Check( sel, !clb->IsChecked(sel) );
179
180 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
181 event.SetEventObject( listbox );
182 event.SetInt( sel );
183 listbox->GetEventHandler()->ProcessEvent( event );
184 #endif // wxUSE_CHECKLISTBOX
185
186 return FALSE;
187 }
188
189 //-----------------------------------------------------------------------------
190 // "select" and "deselect"
191 //-----------------------------------------------------------------------------
192
193 static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox )
194 {
195 if (g_isIdle) wxapp_install_idle_handler();
196
197 if (!listbox->m_hasVMT) return;
198 if (g_blockEventsOnDrag) return;
199
200 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
201
202 wxArrayInt aSelections;
203 int count = listbox->GetSelections(aSelections);
204 if ( count > 0 )
205 {
206 event.m_commandInt = aSelections[0] ;
207 event.m_clientData = listbox->GetClientData( event.m_commandInt );
208 wxString str(listbox->GetString(event.m_commandInt));
209 if (!str.IsEmpty()) event.m_commandString = str;
210 }
211 else
212 {
213 event.m_commandInt = -1 ;
214 event.m_commandString.Empty();
215 }
216
217 event.SetEventObject( listbox );
218
219 listbox->GetEventHandler()->ProcessEvent( event );
220 }
221
222 //-----------------------------------------------------------------------------
223 // wxListBox
224 //-----------------------------------------------------------------------------
225
226 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
227
228 wxListBox::wxListBox()
229 {
230 m_list = (GtkList *) NULL;
231 #if wxUSE_CHECKLISTBOX
232 m_hasCheckBoxes = FALSE;
233 #endif // wxUSE_CHECKLISTBOX
234 }
235
236 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
237 const wxPoint &pos, const wxSize &size,
238 int n, const wxString choices[],
239 long style, const wxValidator& validator, const wxString &name )
240 {
241 m_needParent = TRUE;
242 m_acceptsFocus = TRUE;
243
244 PreCreation( parent, id, pos, size, style, name );
245
246 SetValidator( validator );
247
248 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
249 if (style & wxLB_ALWAYS_SB)
250 {
251 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
252 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
253 }
254 else
255 {
256 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
257 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
258 }
259
260 m_list = GTK_LIST( gtk_list_new() );
261
262 GtkSelectionMode mode = GTK_SELECTION_BROWSE;
263 if (style & wxLB_MULTIPLE)
264 mode = GTK_SELECTION_MULTIPLE;
265 else if (style & wxLB_EXTENDED)
266 mode = GTK_SELECTION_EXTENDED;
267
268 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
269
270 #ifdef NEW_GTK_SCROLL_CODE
271 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
272 #else
273 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
274 #endif
275
276 gtk_widget_show( GTK_WIDGET(m_list) );
277
278 wxSize newSize = size;
279 if (newSize.x == -1) newSize.x = 100;
280 if (newSize.y == -1) newSize.y = 110;
281 SetSize( newSize.x, newSize.y );
282
283 for (int i = 0; i < n; i++)
284 {
285 m_clientDataList.Append( (wxObject*) NULL );
286 m_clientObjectList.Append( (wxObject*) NULL );
287
288 GtkWidget *list_item;
289
290 wxString str(choices[i]);
291 #if wxUSE_CHECKLISTBOX
292 if (m_hasCheckBoxes)
293 {
294 str.Prepend(CHECKBOX_STRING);
295 }
296 #endif // wxUSE_CHECKLISTBOX
297
298 list_item = gtk_list_item_new_with_label( str.mbc_str() );
299
300 gtk_container_add( GTK_CONTAINER(m_list), list_item );
301
302 gtk_signal_connect( GTK_OBJECT(list_item), "select",
303 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
304
305 if (style & wxLB_MULTIPLE)
306 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
307 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
308
309 gtk_signal_connect( GTK_OBJECT(list_item),
310 "button_press_event",
311 (GtkSignalFunc)gtk_listbox_button_press_callback,
312 (gpointer) this );
313
314 gtk_signal_connect_after( GTK_OBJECT(list_item),
315 "button_release_event",
316 (GtkSignalFunc)gtk_listbox_button_release_callback,
317 (gpointer) this );
318
319 #if wxUSE_CHECKLISTBOX
320 if (m_hasCheckBoxes)
321 {
322 gtk_signal_connect( GTK_OBJECT(list_item),
323 "key_press_event",
324 (GtkSignalFunc)gtk_listbox_key_press_callback,
325 (gpointer)this );
326 }
327 #endif // wxUSE_CHECKLISTBOX
328
329 ConnectWidget( list_item );
330
331 gtk_widget_show( list_item );
332 }
333
334 m_parent->DoAddChild( this );
335
336 PostCreation();
337
338 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW ) );
339 SetForegroundColour( parent->GetForegroundColour() );
340 SetFont( parent->GetFont() );
341
342 Show( TRUE );
343
344 return TRUE;
345 }
346
347 wxListBox::~wxListBox()
348 {
349 Clear();
350 }
351
352 void wxListBox::InsertItems(int nItems, const wxString items[], int pos)
353 {
354 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
355
356 GList *children = m_list->children;
357 int length = g_list_length(children);
358 wxCHECK_RET( pos <= length, _T("invalid index in wxListBox::InsertItems") );
359
360 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
361 // into a listbox at the given position, this is why we first delete
362 // all items after this position, then append these items and then
363 // reappend back the old ones.
364
365 // first detach the old items
366 int n; // loop var
367
368 if ( pos == length )
369 {
370 // no need to do anything complicated
371 for ( n = 0; n < nItems; n++ )
372 {
373 Append(items[n]);
374 }
375
376 return;
377 }
378
379 wxArrayString deletedLabels;
380 wxArrayPtrVoid deletedData;
381 wxArrayInt deletedChecks; // only for check list boxes
382
383 GList *child = g_list_nth( children, pos );
384 for ( n = 0; child != NULL; n++, child = child->next )
385 {
386 // save label
387 GtkBin *bin = GTK_BIN( child->data );
388 GtkLabel *label = GTK_LABEL( bin->child );
389
390 wxString str(GET_REAL_LABEL(label->label));
391 deletedLabels.Add(str);
392
393 // save data
394 void *clientData = NULL;
395 wxNode *node = NULL;
396
397 if ( n < (int)m_clientObjectList.GetCount() )
398 node = m_clientObjectList.Nth( n );
399
400 if ( node )
401 {
402 clientData = node->GetData();
403 m_clientObjectList.DeleteNode( node );
404 }
405
406 if ( !clientData )
407 {
408 if ( n < (int)m_clientDataList.GetCount() )
409 node = m_clientDataList.Nth( n );
410
411 if ( node )
412 {
413 clientData = node->GetData();
414 node = m_clientDataList.Nth( n );
415 }
416 }
417
418 deletedData.Add(clientData);
419
420 #if wxUSE_CHECKLISTBOX
421 // save check state
422 if ( m_hasCheckBoxes )
423 {
424 deletedChecks.Add(((wxCheckListBox *)this)->IsChecked(pos + n));
425 }
426 #endif // wxUSE_CHECKLISTBOX
427 }
428
429 int nDeletedCount = n;
430
431 gtk_list_clear_items( m_list, pos, length );
432
433 // now append the new items
434 for ( n = 0; n < nItems; n++ )
435 {
436 Append(items[n]);
437 }
438
439 // and append the old items too
440 pos += nItems; // now the indices are shifter
441 for ( n = 0; n < nDeletedCount; n++ )
442 {
443 Append(deletedLabels[n], deletedData[n]);
444
445 #if wxUSE_CHECKLISTBOX
446 if ( m_hasCheckBoxes )
447 {
448 ((wxCheckListBox *)this)->Check(pos + n, (bool)deletedChecks[n]);
449 }
450 #endif // wxUSE_CHECKLISTBOX
451 }
452 }
453
454 void wxListBox::AppendCommon( const wxString &item )
455 {
456 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
457
458 GtkWidget *list_item;
459
460 wxString label(item);
461 #if wxUSE_CHECKLISTBOX
462 if (m_hasCheckBoxes)
463 {
464 label.Prepend(CHECKBOX_STRING);
465 }
466 #endif // wxUSE_CHECKLISTBOX
467
468 list_item = gtk_list_item_new_with_label( label.mbc_str() );
469
470 gtk_container_add( GTK_CONTAINER(m_list), list_item );
471
472 gtk_signal_connect( GTK_OBJECT(list_item), "select",
473 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
474
475 if (HasFlag(wxLB_MULTIPLE))
476 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
477 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
478
479 gtk_signal_connect( GTK_OBJECT(list_item),
480 "button_press_event",
481 (GtkSignalFunc)gtk_listbox_button_press_callback,
482 (gpointer) this );
483
484 gtk_signal_connect_after( GTK_OBJECT(list_item),
485 "button_release_event",
486 (GtkSignalFunc)gtk_listbox_button_release_callback,
487 (gpointer) this );
488
489 #if wxUSE_CHECKLISTBOX
490 if (m_hasCheckBoxes)
491 {
492 gtk_signal_connect( GTK_OBJECT(list_item),
493 "key_press_event",
494 (GtkSignalFunc)gtk_listbox_key_press_callback,
495 (gpointer)this );
496 }
497 #endif // wxUSE_CHECKLISTBOX
498
499 gtk_widget_show( list_item );
500
501 ConnectWidget( list_item );
502
503 if (GTK_WIDGET_REALIZED(m_widget))
504 {
505 gtk_widget_realize( list_item );
506 gtk_widget_realize( GTK_BIN(list_item)->child );
507
508 if (m_widgetStyle) ApplyWidgetStyle();
509
510 #if wxUSE_DRAG_AND_DROP
511 #ifndef NEW_GTK_DND_CODE
512 if (m_dropTarget) m_dropTarget->RegisterWidget( list_item );
513 #endif
514 #endif
515
516 #if wxUSE_TOOLTIPS
517 if (m_tooltip) m_tooltip->Apply( this );
518 #endif
519 }
520 }
521
522 void wxListBox::Append( const wxString &item )
523 {
524 m_clientDataList.Append( (wxObject*) NULL );
525 m_clientObjectList.Append( (wxObject*) NULL );
526
527 AppendCommon( item );
528 }
529
530 void wxListBox::Append( const wxString &item, void *clientData )
531 {
532 m_clientDataList.Append( (wxObject*) clientData );
533 m_clientObjectList.Append( (wxObject*) NULL );
534
535 AppendCommon( item );
536 }
537
538 void wxListBox::Append( const wxString &item, wxClientData *clientData )
539 {
540 m_clientObjectList.Append( (wxObject*) clientData );
541 m_clientDataList.Append( (wxObject*) NULL );
542
543 AppendCommon( item );
544 }
545
546 void wxListBox::SetClientData( int n, void* clientData )
547 {
548 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
549
550 wxNode *node = m_clientDataList.Nth( n );
551 if (!node) return;
552
553 node->SetData( (wxObject*) clientData );
554 }
555
556 void* wxListBox::GetClientData( int n )
557 {
558 wxCHECK_MSG( m_widget != NULL, NULL, _T("invalid combobox") );
559
560 wxNode *node = m_clientDataList.Nth( n );
561 if (!node) return NULL;
562
563 return node->Data();
564 }
565
566 void wxListBox::SetClientObject( int n, wxClientData* clientData )
567 {
568 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
569
570 wxNode *node = m_clientObjectList.Nth( n );
571 if (!node) return;
572
573 wxClientData *cd = (wxClientData*) node->Data();
574 if (cd) delete cd;
575
576 node->SetData( (wxObject*) clientData );
577 }
578
579 wxClientData* wxListBox::GetClientObject( int n )
580 {
581 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, _T("invalid combobox") );
582
583 wxNode *node = m_clientObjectList.Nth( n );
584 if (!node) return (wxClientData*) NULL;
585
586 return (wxClientData*) node->Data();
587 }
588
589 void wxListBox::Clear()
590 {
591 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
592
593 gtk_list_clear_items( m_list, 0, Number() );
594
595 wxNode *node = m_clientObjectList.First();
596 while (node)
597 {
598 wxClientData *cd = (wxClientData*)node->Data();
599 if (cd) delete cd;
600 node = node->Next();
601 }
602 m_clientObjectList.Clear();
603
604 m_clientDataList.Clear();
605 }
606
607 void wxListBox::Delete( int n )
608 {
609 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
610
611 GList *child = g_list_nth( m_list->children, n );
612
613 wxCHECK_RET( child, _T("wrong listbox index") );
614
615 GList *list = g_list_append( (GList*) NULL, child->data );
616 gtk_list_remove_items( m_list, list );
617 g_list_free( list );
618
619 wxNode *node = m_clientObjectList.Nth( n );
620 if (node)
621 {
622 wxClientData *cd = (wxClientData*)node->Data();
623 if (cd) delete cd;
624 m_clientObjectList.DeleteNode( node );
625 }
626
627 node = m_clientDataList.Nth( n );
628 if (node)
629 {
630 m_clientDataList.DeleteNode( node );
631 }
632 }
633
634 void wxListBox::Deselect( int n )
635 {
636 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
637
638 gtk_list_unselect_item( m_list, n );
639 }
640
641 int wxListBox::FindString( const wxString &item ) const
642 {
643 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
644
645 GList *child = m_list->children;
646 int count = 0;
647 while (child)
648 {
649 GtkBin *bin = GTK_BIN( child->data );
650 GtkLabel *label = GTK_LABEL( bin->child );
651
652 wxString str = GET_REAL_LABEL(label->label);
653
654 if (str == item)
655 return count;
656
657 count++;
658 child = child->next;
659 }
660
661 // it's not an error if the string is not found -> no wxCHECK
662
663 return -1;
664 }
665
666 int wxListBox::GetSelection() const
667 {
668 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
669
670 GList *child = m_list->children;
671 int count = 0;
672 while (child)
673 {
674 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
675 count++;
676 child = child->next;
677 }
678 return -1;
679 }
680
681 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
682 {
683 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
684
685 // get the number of selected items first
686 GList *child = m_list->children;
687 int count = 0;
688 for (child = m_list->children; child != NULL; child = child->next)
689 {
690 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
691 count++;
692 }
693
694 aSelections.Empty();
695
696 if (count > 0)
697 {
698 // now fill the list
699 aSelections.Alloc(count); // optimization attempt
700 int i = 0;
701 for (child = m_list->children; child != NULL; child = child->next, i++)
702 {
703 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
704 aSelections.Add(i);
705 }
706 }
707
708 return count;
709 }
710
711 wxString wxListBox::GetString( int n ) const
712 {
713 wxCHECK_MSG( m_list != NULL, _T(""), _T("invalid listbox") );
714
715 GList *child = g_list_nth( m_list->children, n );
716 if (child)
717 {
718 GtkBin *bin = GTK_BIN( child->data );
719 GtkLabel *label = GTK_LABEL( bin->child );
720
721 wxString str = GET_REAL_LABEL(label->label);
722
723 return str;
724 }
725
726 wxFAIL_MSG(_T("wrong listbox index"));
727
728 return _T("");
729 }
730
731 wxString wxListBox::GetStringSelection() const
732 {
733 wxCHECK_MSG( m_list != NULL, _T(""), _T("invalid listbox") );
734
735 GList *selection = m_list->selection;
736 if (selection)
737 {
738 GtkBin *bin = GTK_BIN( selection->data );
739 GtkLabel *label = GTK_LABEL( bin->child );
740
741 wxString str = GET_REAL_LABEL(label->label);
742
743 return str;
744 }
745
746 wxFAIL_MSG(_T("no listbox selection available"));
747 return _T("");
748 }
749
750 int wxListBox::Number()
751 {
752 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
753
754 GList *child = m_list->children;
755 int count = 0;
756 while (child) { count++; child = child->next; }
757 return count;
758 }
759
760 bool wxListBox::Selected( int n )
761 {
762 wxCHECK_MSG( m_list != NULL, FALSE, _T("invalid listbox") );
763
764 GList *target = g_list_nth( m_list->children, n );
765 if (target)
766 {
767 GList *child = m_list->selection;
768 while (child)
769 {
770 if (child->data == target->data) return TRUE;
771 child = child->next;
772 }
773 }
774 wxFAIL_MSG(_T("wrong listbox index"));
775 return FALSE;
776 }
777
778 void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
779 {
780 wxFAIL_MSG(_T("wxListBox::Set not implemented"));
781 }
782
783 void wxListBox::SetFirstItem( int WXUNUSED(n) )
784 {
785 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
786 }
787
788 void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
789 {
790 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
791 }
792
793 void wxListBox::SetSelection( int n, bool select )
794 {
795 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
796
797 if (select)
798 gtk_list_select_item( m_list, n );
799 else
800 gtk_list_unselect_item( m_list, n );
801 }
802
803 void wxListBox::SetString( int n, const wxString &string )
804 {
805 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
806
807 GList *child = g_list_nth( m_list->children, n );
808 if (child)
809 {
810 GtkBin *bin = GTK_BIN( child->data );
811 GtkLabel *label = GTK_LABEL( bin->child );
812
813 wxString str;
814 #if wxUSE_CHECKLISTBOX
815 if (m_hasCheckBoxes)
816 str += CHECKBOX_STRING;
817 #endif // wxUSE_CHECKLISTBOX
818 str += string;
819
820 gtk_label_set( label, str.mbc_str() );
821 }
822 else
823 {
824 wxFAIL_MSG(_T("wrong listbox index"));
825 }
826 }
827
828 void wxListBox::SetStringSelection( const wxString &string, bool select )
829 {
830 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
831
832 SetSelection( FindString(string), select );
833 }
834
835 int wxListBox::GetIndex( GtkWidget *item ) const
836 {
837 if (item)
838 {
839 GList *child = m_list->children;
840 int count = 0;
841 while (child)
842 {
843 if (GTK_WIDGET(child->data) == item) return count;
844 count++;
845 child = child->next;
846 }
847 }
848 return -1;
849 }
850
851 #if wxUSE_TOOLTIPS
852 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
853 {
854 GList *child = m_list->children;
855 while (child)
856 {
857 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConv_local.cWX2MB(tip), (gchar*) NULL );
858 child = child->next;
859 }
860 }
861 #endif // wxUSE_TOOLTIPS
862
863 #if wxUSE_DRAG_AND_DROP
864 void wxListBox::SetDropTarget( wxDropTarget *dropTarget )
865 {
866 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
867
868 #ifndef NEW_GTK_DND_CODE
869 if (m_dropTarget)
870 {
871 GList *child = m_list->children;
872 while (child)
873 {
874 m_dropTarget->UnregisterWidget( GTK_WIDGET( child->data ) );
875 child = child->next;
876 }
877 }
878 #endif
879
880 wxWindow::SetDropTarget( dropTarget );
881
882 #ifndef NEW_GTK_DND_CODE
883 if (m_dropTarget)
884 {
885 GList *child = m_list->children;
886 while (child)
887 {
888 m_dropTarget->RegisterWidget( GTK_WIDGET( child->data ) );
889 child = child->next;
890 }
891 }
892 #endif
893 }
894 #endif
895
896 GtkWidget *wxListBox::GetConnectWidget()
897 {
898 return GTK_WIDGET(m_list);
899 }
900
901 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
902 {
903 if (wxWindow::IsOwnGtkWindow( window )) return TRUE;
904
905 GList *child = m_list->children;
906 while (child)
907 {
908 GtkWidget *bin = GTK_WIDGET( child->data );
909 if (bin->window == window) return TRUE;
910 child = child->next;
911 }
912
913 return FALSE;
914 }
915
916 void wxListBox::ApplyWidgetStyle()
917 {
918 SetWidgetStyle();
919
920 if (m_backgroundColour.Ok())
921 {
922 GdkWindow *window = GTK_WIDGET(m_list)->window;
923 if ( window )
924 {
925 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
926 gdk_window_set_background( window, m_backgroundColour.GetColor() );
927 gdk_window_clear( window );
928 }
929 }
930
931 GList *child = m_list->children;
932 while (child)
933 {
934 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
935
936 GtkBin *bin = GTK_BIN( child->data );
937 GtkWidget *label = GTK_WIDGET( bin->child );
938 gtk_widget_set_style( label, m_widgetStyle );
939
940 child = child->next;
941 }
942 }