]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/listbox.cpp
Made wxNO_DEFAULT set the "no" button as the default, according to the docs
[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 static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
199 {
200 gtk_listitem_select_callback( widget, listbox );
201 }
202
203 static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox )
204 {
205 if (g_isIdle) wxapp_install_idle_handler();
206
207 if (!listbox->m_hasVMT) return;
208 if (g_blockEventsOnDrag) return;
209
210 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
211
212 wxArrayInt aSelections;
213 int count = listbox->GetSelections(aSelections);
214 if ( count > 0 )
215 {
216 event.m_commandInt = aSelections[0] ;
217 event.m_clientData = listbox->GetClientData( event.m_commandInt );
218 wxString str(listbox->GetString(event.m_commandInt));
219 if (!str.IsEmpty()) event.m_commandString = str;
220 }
221 else
222 {
223 event.m_commandInt = -1 ;
224 event.m_commandString.Empty();
225 }
226
227 event.SetEventObject( listbox );
228
229 listbox->GetEventHandler()->ProcessEvent( event );
230 }
231
232 //-----------------------------------------------------------------------------
233 // wxListBox
234 //-----------------------------------------------------------------------------
235
236 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
237
238 wxListBox::wxListBox()
239 {
240 m_list = (GtkList *) NULL;
241 #if wxUSE_CHECKLISTBOX
242 m_hasCheckBoxes = FALSE;
243 #endif // wxUSE_CHECKLISTBOX
244 }
245
246 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
247 const wxPoint &pos, const wxSize &size,
248 int n, const wxString choices[],
249 long style, const wxValidator& validator, const wxString &name )
250 {
251 m_needParent = TRUE;
252 m_acceptsFocus = TRUE;
253
254 if (!PreCreation( parent, pos, size ) ||
255 !CreateBase( parent, id, pos, size, style, validator, name ))
256 {
257 wxFAIL_MSG( _T("wxListBox creation failed") );
258 return FALSE;
259 }
260
261 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
262 if (style & wxLB_ALWAYS_SB)
263 {
264 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
265 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
266 }
267 else
268 {
269 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
270 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
271 }
272
273 m_list = GTK_LIST( gtk_list_new() );
274
275 GtkSelectionMode mode = GTK_SELECTION_BROWSE;
276 if (style & wxLB_MULTIPLE)
277 mode = GTK_SELECTION_MULTIPLE;
278 else if (style & wxLB_EXTENDED)
279 mode = GTK_SELECTION_EXTENDED;
280
281 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
282
283 #ifdef NEW_GTK_SCROLL_CODE
284 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
285 #else
286 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
287 #endif
288
289 gtk_widget_show( GTK_WIDGET(m_list) );
290
291 wxSize newSize = size;
292 if (newSize.x == -1) newSize.x = 100;
293 if (newSize.y == -1) newSize.y = 110;
294 SetSize( newSize.x, newSize.y );
295
296 for (int i = 0; i < n; i++)
297 {
298 m_clientDataList.Append( (wxObject*) NULL );
299 m_clientObjectList.Append( (wxObject*) NULL );
300
301 GtkWidget *list_item;
302
303 wxString str(choices[i]);
304 #if wxUSE_CHECKLISTBOX
305 if (m_hasCheckBoxes)
306 {
307 str.Prepend(CHECKBOX_STRING);
308 }
309 #endif // wxUSE_CHECKLISTBOX
310
311 list_item = gtk_list_item_new_with_label( str.mbc_str() );
312
313 gtk_container_add( GTK_CONTAINER(m_list), list_item );
314
315 gtk_signal_connect( GTK_OBJECT(list_item), "select",
316 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
317
318 if (style & wxLB_MULTIPLE)
319 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
320 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
321
322 gtk_signal_connect( GTK_OBJECT(list_item),
323 "button_press_event",
324 (GtkSignalFunc)gtk_listbox_button_press_callback,
325 (gpointer) this );
326
327 gtk_signal_connect_after( GTK_OBJECT(list_item),
328 "button_release_event",
329 (GtkSignalFunc)gtk_listbox_button_release_callback,
330 (gpointer) this );
331
332 #if wxUSE_CHECKLISTBOX
333 if (m_hasCheckBoxes)
334 {
335 gtk_signal_connect( GTK_OBJECT(list_item),
336 "key_press_event",
337 (GtkSignalFunc)gtk_listbox_key_press_callback,
338 (gpointer)this );
339 }
340 #endif // wxUSE_CHECKLISTBOX
341
342 ConnectWidget( list_item );
343
344 gtk_widget_show( list_item );
345 }
346
347 m_parent->DoAddChild( this );
348
349 PostCreation();
350
351 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW ) );
352 SetForegroundColour( parent->GetForegroundColour() );
353 SetFont( parent->GetFont() );
354
355 Show( TRUE );
356
357 return TRUE;
358 }
359
360 wxListBox::~wxListBox()
361 {
362 Clear();
363 }
364
365 void wxListBox::InsertItems(int nItems, const wxString items[], int pos)
366 {
367 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
368
369 GList *children = m_list->children;
370 int length = g_list_length(children);
371 wxCHECK_RET( pos <= length, _T("invalid index in wxListBox::InsertItems") );
372
373 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
374 // into a listbox at the given position, this is why we first delete
375 // all items after this position, then append these items and then
376 // reappend back the old ones.
377
378 // first detach the old items
379 int n; // loop var
380
381 if ( pos == length )
382 {
383 // no need to do anything complicated
384 for ( n = 0; n < nItems; n++ )
385 {
386 Append(items[n]);
387 }
388
389 return;
390 }
391
392 wxArrayString deletedLabels;
393 wxArrayPtrVoid deletedData;
394 wxArrayInt deletedChecks; // only for check list boxes
395
396 GList *child = g_list_nth( children, pos );
397 for ( n = 0; child != NULL; n++, child = child->next )
398 {
399 // save label
400 GtkBin *bin = GTK_BIN( child->data );
401 GtkLabel *label = GTK_LABEL( bin->child );
402
403 wxString str(GET_REAL_LABEL(label->label),*wxConvCurrent);
404 deletedLabels.Add(str);
405
406 // save data
407 void *clientData = NULL;
408 wxNode *node = NULL;
409
410 if ( n < (int)m_clientObjectList.GetCount() )
411 node = m_clientObjectList.Nth( n );
412
413 if ( node )
414 {
415 clientData = node->GetData();
416 m_clientObjectList.DeleteNode( node );
417 }
418
419 if ( !clientData )
420 {
421 if ( n < (int)m_clientDataList.GetCount() )
422 node = m_clientDataList.Nth( n );
423
424 if ( node )
425 {
426 clientData = node->GetData();
427 node = m_clientDataList.Nth( n );
428 }
429 }
430
431 deletedData.Add(clientData);
432
433 #if wxUSE_CHECKLISTBOX
434 // save check state
435 if ( m_hasCheckBoxes )
436 {
437 deletedChecks.Add(((wxCheckListBox *)this)->IsChecked(pos + n));
438 }
439 #endif // wxUSE_CHECKLISTBOX
440 }
441
442 int nDeletedCount = n;
443
444 gtk_list_clear_items( m_list, pos, length );
445
446 // now append the new items
447 for ( n = 0; n < nItems; n++ )
448 {
449 Append(items[n]);
450 }
451
452 // and append the old items too
453 pos += nItems; // now the indices are shifter
454 for ( n = 0; n < nDeletedCount; n++ )
455 {
456 Append(deletedLabels[n], deletedData[n]);
457
458 #if wxUSE_CHECKLISTBOX
459 if ( m_hasCheckBoxes )
460 {
461 ((wxCheckListBox *)this)->Check(pos + n, (bool)deletedChecks[n]);
462 }
463 #endif // wxUSE_CHECKLISTBOX
464 }
465 }
466
467 void wxListBox::AppendCommon( const wxString &item )
468 {
469 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
470
471 GtkWidget *list_item;
472
473 wxString label(item);
474 #if wxUSE_CHECKLISTBOX
475 if (m_hasCheckBoxes)
476 {
477 label.Prepend(CHECKBOX_STRING);
478 }
479 #endif // wxUSE_CHECKLISTBOX
480
481 list_item = gtk_list_item_new_with_label( label.mbc_str() );
482
483 gtk_container_add( GTK_CONTAINER(m_list), list_item );
484
485 gtk_signal_connect( GTK_OBJECT(list_item), "select",
486 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
487
488 if (HasFlag(wxLB_MULTIPLE))
489 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
490 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
491
492 gtk_signal_connect( GTK_OBJECT(list_item),
493 "button_press_event",
494 (GtkSignalFunc)gtk_listbox_button_press_callback,
495 (gpointer) this );
496
497 gtk_signal_connect_after( GTK_OBJECT(list_item),
498 "button_release_event",
499 (GtkSignalFunc)gtk_listbox_button_release_callback,
500 (gpointer) this );
501
502 #if wxUSE_CHECKLISTBOX
503 if (m_hasCheckBoxes)
504 {
505 gtk_signal_connect( GTK_OBJECT(list_item),
506 "key_press_event",
507 (GtkSignalFunc)gtk_listbox_key_press_callback,
508 (gpointer)this );
509 }
510 #endif // wxUSE_CHECKLISTBOX
511
512 gtk_widget_show( list_item );
513
514 ConnectWidget( list_item );
515
516 if (GTK_WIDGET_REALIZED(m_widget))
517 {
518 gtk_widget_realize( list_item );
519 gtk_widget_realize( GTK_BIN(list_item)->child );
520
521 if (m_widgetStyle) ApplyWidgetStyle();
522
523 #if wxUSE_DRAG_AND_DROP
524 #ifndef NEW_GTK_DND_CODE
525 if (m_dropTarget) m_dropTarget->RegisterWidget( list_item );
526 #endif
527 #endif
528
529 #if wxUSE_TOOLTIPS
530 if (m_tooltip) m_tooltip->Apply( this );
531 #endif
532 }
533 }
534
535 void wxListBox::Append( const wxString &item )
536 {
537 m_clientDataList.Append( (wxObject*) NULL );
538 m_clientObjectList.Append( (wxObject*) NULL );
539
540 AppendCommon( item );
541 }
542
543 void wxListBox::Append( const wxString &item, void *clientData )
544 {
545 m_clientDataList.Append( (wxObject*) clientData );
546 m_clientObjectList.Append( (wxObject*) NULL );
547
548 AppendCommon( item );
549 }
550
551 void wxListBox::Append( const wxString &item, wxClientData *clientData )
552 {
553 m_clientObjectList.Append( (wxObject*) clientData );
554 m_clientDataList.Append( (wxObject*) NULL );
555
556 AppendCommon( item );
557 }
558
559 void wxListBox::SetClientData( int n, void* clientData )
560 {
561 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
562
563 wxNode *node = m_clientDataList.Nth( n );
564 if (!node) return;
565
566 node->SetData( (wxObject*) clientData );
567 }
568
569 void* wxListBox::GetClientData( int n )
570 {
571 wxCHECK_MSG( m_widget != NULL, NULL, _T("invalid combobox") );
572
573 wxNode *node = m_clientDataList.Nth( n );
574 if (!node) return NULL;
575
576 return node->Data();
577 }
578
579 void wxListBox::SetClientObject( int n, wxClientData* clientData )
580 {
581 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
582
583 wxNode *node = m_clientObjectList.Nth( n );
584 if (!node) return;
585
586 wxClientData *cd = (wxClientData*) node->Data();
587 if (cd) delete cd;
588
589 node->SetData( (wxObject*) clientData );
590 }
591
592 wxClientData* wxListBox::GetClientObject( int n )
593 {
594 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, _T("invalid combobox") );
595
596 wxNode *node = m_clientObjectList.Nth( n );
597 if (!node) return (wxClientData*) NULL;
598
599 return (wxClientData*) node->Data();
600 }
601
602 void wxListBox::Clear()
603 {
604 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
605
606 gtk_list_clear_items( m_list, 0, Number() );
607
608 wxNode *node = m_clientObjectList.First();
609 while (node)
610 {
611 wxClientData *cd = (wxClientData*)node->Data();
612 if (cd) delete cd;
613 node = node->Next();
614 }
615 m_clientObjectList.Clear();
616
617 m_clientDataList.Clear();
618 }
619
620 void wxListBox::Delete( int n )
621 {
622 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
623
624 GList *child = g_list_nth( m_list->children, n );
625
626 wxCHECK_RET( child, _T("wrong listbox index") );
627
628 GList *list = g_list_append( (GList*) NULL, child->data );
629 gtk_list_remove_items( m_list, list );
630 g_list_free( list );
631
632 wxNode *node = m_clientObjectList.Nth( n );
633 if (node)
634 {
635 wxClientData *cd = (wxClientData*)node->Data();
636 if (cd) delete cd;
637 m_clientObjectList.DeleteNode( node );
638 }
639
640 node = m_clientDataList.Nth( n );
641 if (node)
642 {
643 m_clientDataList.DeleteNode( node );
644 }
645 }
646
647 void wxListBox::Deselect( int n )
648 {
649 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
650
651 DisableEvents();
652
653 gtk_list_unselect_item( m_list, n );
654
655 EnableEvents();
656 }
657
658 int wxListBox::FindString( const wxString &item ) const
659 {
660 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
661
662 GList *child = m_list->children;
663 int count = 0;
664 while (child)
665 {
666 GtkBin *bin = GTK_BIN( child->data );
667 GtkLabel *label = GTK_LABEL( bin->child );
668
669 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
670
671 if (str == item)
672 return count;
673
674 count++;
675 child = child->next;
676 }
677
678 // it's not an error if the string is not found -> no wxCHECK
679
680 return -1;
681 }
682
683 int wxListBox::GetSelection() const
684 {
685 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
686
687 GList *child = m_list->children;
688 int count = 0;
689 while (child)
690 {
691 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
692 count++;
693 child = child->next;
694 }
695 return -1;
696 }
697
698 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
699 {
700 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
701
702 // get the number of selected items first
703 GList *child = m_list->children;
704 int count = 0;
705 for (child = m_list->children; child != NULL; child = child->next)
706 {
707 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
708 count++;
709 }
710
711 aSelections.Empty();
712
713 if (count > 0)
714 {
715 // now fill the list
716 aSelections.Alloc(count); // optimization attempt
717 int i = 0;
718 for (child = m_list->children; child != NULL; child = child->next, i++)
719 {
720 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
721 aSelections.Add(i);
722 }
723 }
724
725 return count;
726 }
727
728 wxString wxListBox::GetString( int n ) const
729 {
730 wxCHECK_MSG( m_list != NULL, _T(""), _T("invalid listbox") );
731
732 GList *child = g_list_nth( m_list->children, n );
733 if (child)
734 {
735 GtkBin *bin = GTK_BIN( child->data );
736 GtkLabel *label = GTK_LABEL( bin->child );
737
738 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
739
740 return str;
741 }
742
743 wxFAIL_MSG(_T("wrong listbox index"));
744
745 return _T("");
746 }
747
748 wxString wxListBox::GetStringSelection() const
749 {
750 wxCHECK_MSG( m_list != NULL, _T(""), _T("invalid listbox") );
751
752 GList *selection = m_list->selection;
753 if (selection)
754 {
755 GtkBin *bin = GTK_BIN( selection->data );
756 GtkLabel *label = GTK_LABEL( bin->child );
757
758 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
759
760 return str;
761 }
762
763 wxFAIL_MSG(_T("no listbox selection available"));
764 return _T("");
765 }
766
767 int wxListBox::Number()
768 {
769 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
770
771 GList *child = m_list->children;
772 int count = 0;
773 while (child) { count++; child = child->next; }
774 return count;
775 }
776
777 bool wxListBox::Selected( int n )
778 {
779 wxCHECK_MSG( m_list != NULL, FALSE, _T("invalid listbox") );
780
781 GList *target = g_list_nth( m_list->children, n );
782 if (target)
783 {
784 GList *child = m_list->selection;
785 while (child)
786 {
787 if (child->data == target->data) return TRUE;
788 child = child->next;
789 }
790 }
791 wxFAIL_MSG(_T("wrong listbox index"));
792 return FALSE;
793 }
794
795 void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
796 {
797 wxFAIL_MSG(_T("wxListBox::Set not implemented"));
798 }
799
800 void wxListBox::SetFirstItem( int WXUNUSED(n) )
801 {
802 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
803 }
804
805 void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
806 {
807 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
808 }
809
810 void wxListBox::SetSelection( int n, bool select )
811 {
812 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
813
814 DisableEvents();
815
816 if (select)
817 gtk_list_select_item( m_list, n );
818 else
819 gtk_list_unselect_item( m_list, n );
820
821 EnableEvents();
822 }
823
824 void wxListBox::SetString( int n, const wxString &string )
825 {
826 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
827
828 GList *child = g_list_nth( m_list->children, n );
829 if (child)
830 {
831 GtkBin *bin = GTK_BIN( child->data );
832 GtkLabel *label = GTK_LABEL( bin->child );
833
834 wxString str;
835 #if wxUSE_CHECKLISTBOX
836 if (m_hasCheckBoxes)
837 str += CHECKBOX_STRING;
838 #endif // wxUSE_CHECKLISTBOX
839 str += string;
840
841 gtk_label_set( label, str.mbc_str() );
842 }
843 else
844 {
845 wxFAIL_MSG(_T("wrong listbox index"));
846 }
847 }
848
849 void wxListBox::SetStringSelection( const wxString &string, bool select )
850 {
851 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
852
853 SetSelection( FindString(string), select );
854 }
855
856 int wxListBox::GetIndex( GtkWidget *item ) const
857 {
858 if (item)
859 {
860 GList *child = m_list->children;
861 int count = 0;
862 while (child)
863 {
864 if (GTK_WIDGET(child->data) == item) return count;
865 count++;
866 child = child->next;
867 }
868 }
869 return -1;
870 }
871
872 #if wxUSE_TOOLTIPS
873 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
874 {
875 GList *child = m_list->children;
876 while (child)
877 {
878 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
879 child = child->next;
880 }
881 }
882 #endif // wxUSE_TOOLTIPS
883
884 #if wxUSE_DRAG_AND_DROP
885 void wxListBox::SetDropTarget( wxDropTarget *dropTarget )
886 {
887 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
888
889 #ifndef NEW_GTK_DND_CODE
890 if (m_dropTarget)
891 {
892 GList *child = m_list->children;
893 while (child)
894 {
895 m_dropTarget->UnregisterWidget( GTK_WIDGET( child->data ) );
896 child = child->next;
897 }
898 }
899 #endif
900
901 wxWindow::SetDropTarget( dropTarget );
902
903 #ifndef NEW_GTK_DND_CODE
904 if (m_dropTarget)
905 {
906 GList *child = m_list->children;
907 while (child)
908 {
909 m_dropTarget->RegisterWidget( GTK_WIDGET( child->data ) );
910 child = child->next;
911 }
912 }
913 #endif
914 }
915 #endif
916
917 void wxListBox::DisableEvents()
918 {
919 GList *child = m_list->children;
920 while (child)
921 {
922 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
923 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
924
925 if (HasFlag(wxLB_MULTIPLE))
926 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
927 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
928
929 child = child->next;
930 }
931 }
932
933 void wxListBox::EnableEvents()
934 {
935 GList *child = m_list->children;
936 while (child)
937 {
938 gtk_signal_connect( GTK_OBJECT(child->data), "select",
939 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
940
941 if (HasFlag(wxLB_MULTIPLE))
942 gtk_signal_connect( GTK_OBJECT(child->data), "deselect",
943 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
944
945 child = child->next;
946 }
947 }
948
949 GtkWidget *wxListBox::GetConnectWidget()
950 {
951 return GTK_WIDGET(m_list);
952 }
953
954 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
955 {
956 if (wxWindow::IsOwnGtkWindow( window )) return TRUE;
957
958 GList *child = m_list->children;
959 while (child)
960 {
961 GtkWidget *bin = GTK_WIDGET( child->data );
962 if (bin->window == window) return TRUE;
963 child = child->next;
964 }
965
966 return FALSE;
967 }
968
969 void wxListBox::ApplyWidgetStyle()
970 {
971 SetWidgetStyle();
972
973 if (m_backgroundColour.Ok())
974 {
975 GdkWindow *window = GTK_WIDGET(m_list)->window;
976 if ( window )
977 {
978 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
979 gdk_window_set_background( window, m_backgroundColour.GetColor() );
980 gdk_window_clear( window );
981 }
982 }
983
984 GList *child = m_list->children;
985 while (child)
986 {
987 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
988
989 GtkBin *bin = GTK_BIN( child->data );
990 GtkWidget *label = GTK_WIDGET( bin->child );
991 gtk_widget_set_style( label, m_widgetStyle );
992
993 child = child->next;
994 }
995 }
996
997 #endif