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