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