Correxted myfixed widget to prevent warnings from GTK
[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 if (m_hasCheckBoxes)
461 {
462 gtk_signal_connect( GTK_OBJECT(list_item),
463 "key_press_event",
464 (GtkSignalFunc)gtk_listbox_key_press_callback,
465 (gpointer)this );
466 }
467
468 gtk_widget_show( list_item );
469
470 ConnectWidget( list_item );
471
472 if (GTK_WIDGET_REALIZED(m_widget))
473 {
474 gtk_widget_realize( list_item );
475 gtk_widget_realize( GTK_BIN(list_item)->child );
476
477 if (m_widgetStyle) ApplyWidgetStyle();
478
479 #if wxUSE_DRAG_AND_DROP
480 #ifndef NEW_GTK_DND_CODE
481 if (m_dropTarget) m_dropTarget->RegisterWidget( list_item );
482 #endif
483 #endif
484
485 #if wxUSE_TOOLTIPS
486 if (m_tooltip) m_tooltip->Apply( this );
487 #endif
488 }
489 }
490
491 void wxListBox::Append( const wxString &item )
492 {
493 m_clientDataList.Append( (wxObject*) NULL );
494 m_clientObjectList.Append( (wxObject*) NULL );
495
496 AppendCommon( item );
497 }
498
499 void wxListBox::Append( const wxString &item, void *clientData )
500 {
501 m_clientDataList.Append( (wxObject*) clientData );
502 m_clientObjectList.Append( (wxObject*) NULL );
503
504 AppendCommon( item );
505 }
506
507 void wxListBox::Append( const wxString &item, wxClientData *clientData )
508 {
509 m_clientObjectList.Append( (wxObject*) clientData );
510 m_clientDataList.Append( (wxObject*) NULL );
511
512 AppendCommon( item );
513 }
514
515 void wxListBox::SetClientData( int n, void* clientData )
516 {
517 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
518
519 wxNode *node = m_clientDataList.Nth( n );
520 if (!node) return;
521
522 node->SetData( (wxObject*) clientData );
523 }
524
525 void* wxListBox::GetClientData( int n )
526 {
527 wxCHECK_MSG( m_widget != NULL, NULL, _T("invalid combobox") );
528
529 wxNode *node = m_clientDataList.Nth( n );
530 if (!node) return NULL;
531
532 return node->Data();
533 }
534
535 void wxListBox::SetClientObject( int n, wxClientData* clientData )
536 {
537 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
538
539 wxNode *node = m_clientObjectList.Nth( n );
540 if (!node) return;
541
542 wxClientData *cd = (wxClientData*) node->Data();
543 if (cd) delete cd;
544
545 node->SetData( (wxObject*) clientData );
546 }
547
548 wxClientData* wxListBox::GetClientObject( int n )
549 {
550 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, _T("invalid combobox") );
551
552 wxNode *node = m_clientObjectList.Nth( n );
553 if (!node) return (wxClientData*) NULL;
554
555 return (wxClientData*) node->Data();
556 }
557
558 void wxListBox::Clear()
559 {
560 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
561
562 gtk_list_clear_items( m_list, 0, Number() );
563
564 wxNode *node = m_clientObjectList.First();
565 while (node)
566 {
567 wxClientData *cd = (wxClientData*)node->Data();
568 if (cd) delete cd;
569 node = node->Next();
570 }
571 m_clientObjectList.Clear();
572
573 m_clientDataList.Clear();
574 }
575
576 void wxListBox::Delete( int n )
577 {
578 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
579
580 GList *child = g_list_nth( m_list->children, n );
581
582 wxCHECK_RET( child, _T("wrong listbox index") );
583
584 GList *list = g_list_append( (GList*) NULL, child->data );
585 gtk_list_remove_items( m_list, list );
586 g_list_free( list );
587
588 wxNode *node = m_clientObjectList.Nth( n );
589 if (node)
590 {
591 wxClientData *cd = (wxClientData*)node->Data();
592 if (cd) delete cd;
593 m_clientObjectList.DeleteNode( node );
594 }
595
596 node = m_clientDataList.Nth( n );
597 if (node)
598 {
599 m_clientDataList.DeleteNode( node );
600 }
601 }
602
603 void wxListBox::Deselect( int n )
604 {
605 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
606
607 gtk_list_unselect_item( m_list, n );
608 }
609
610 int wxListBox::FindString( const wxString &item ) const
611 {
612 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
613
614 GList *child = m_list->children;
615 int count = 0;
616 while (child)
617 {
618 GtkBin *bin = GTK_BIN( child->data );
619 GtkLabel *label = GTK_LABEL( bin->child );
620
621 wxString str = GET_REAL_LABEL(label->label);
622
623 if (str == item)
624 return count;
625
626 count++;
627 child = child->next;
628 }
629
630 // it's not an error if the string is not found -> no wxCHECK
631
632 return -1;
633 }
634
635 int wxListBox::GetSelection() const
636 {
637 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
638
639 GList *child = m_list->children;
640 int count = 0;
641 while (child)
642 {
643 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
644 count++;
645 child = child->next;
646 }
647 return -1;
648 }
649
650 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
651 {
652 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
653
654 // get the number of selected items first
655 GList *child = m_list->children;
656 int count = 0;
657 for (child = m_list->children; child != NULL; child = child->next)
658 {
659 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
660 count++;
661 }
662
663 aSelections.Empty();
664
665 if (count > 0)
666 {
667 // now fill the list
668 aSelections.Alloc(count); // optimization attempt
669 int i = 0;
670 for (child = m_list->children; child != NULL; child = child->next, i++)
671 {
672 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
673 aSelections.Add(i);
674 }
675 }
676
677 return count;
678 }
679
680 wxString wxListBox::GetString( int n ) const
681 {
682 wxCHECK_MSG( m_list != NULL, _T(""), _T("invalid listbox") );
683
684 GList *child = g_list_nth( m_list->children, n );
685 if (child)
686 {
687 GtkBin *bin = GTK_BIN( child->data );
688 GtkLabel *label = GTK_LABEL( bin->child );
689
690 wxString str = GET_REAL_LABEL(label->label);
691
692 return str;
693 }
694
695 wxFAIL_MSG(_T("wrong listbox index"));
696
697 return _T("");
698 }
699
700 wxString wxListBox::GetStringSelection() const
701 {
702 wxCHECK_MSG( m_list != NULL, _T(""), _T("invalid listbox") );
703
704 GList *selection = m_list->selection;
705 if (selection)
706 {
707 GtkBin *bin = GTK_BIN( selection->data );
708 GtkLabel *label = GTK_LABEL( bin->child );
709
710 wxString str = GET_REAL_LABEL(label->label);
711
712 return str;
713 }
714
715 wxFAIL_MSG(_T("no listbox selection available"));
716 return _T("");
717 }
718
719 int wxListBox::Number()
720 {
721 wxCHECK_MSG( m_list != NULL, -1, _T("invalid listbox") );
722
723 GList *child = m_list->children;
724 int count = 0;
725 while (child) { count++; child = child->next; }
726 return count;
727 }
728
729 bool wxListBox::Selected( int n )
730 {
731 wxCHECK_MSG( m_list != NULL, FALSE, _T("invalid listbox") );
732
733 GList *target = g_list_nth( m_list->children, n );
734 if (target)
735 {
736 GList *child = m_list->selection;
737 while (child)
738 {
739 if (child->data == target->data) return TRUE;
740 child = child->next;
741 }
742 }
743 wxFAIL_MSG(_T("wrong listbox index"));
744 return FALSE;
745 }
746
747 void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
748 {
749 wxFAIL_MSG(_T("wxListBox::Set not implemented"));
750 }
751
752 void wxListBox::SetFirstItem( int WXUNUSED(n) )
753 {
754 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
755 }
756
757 void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
758 {
759 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
760 }
761
762 void wxListBox::SetSelection( int n, bool select )
763 {
764 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
765
766 if (select)
767 gtk_list_select_item( m_list, n );
768 else
769 gtk_list_unselect_item( m_list, n );
770 }
771
772 void wxListBox::SetString( int n, const wxString &string )
773 {
774 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
775
776 GList *child = g_list_nth( m_list->children, n );
777 if (child)
778 {
779 GtkBin *bin = GTK_BIN( child->data );
780 GtkLabel *label = GTK_LABEL( bin->child );
781
782 wxString str;
783 if (m_hasCheckBoxes)
784 str += CHECKBOX_STRING;
785 str += string;
786
787 gtk_label_set( label, str.mbc_str() );
788 }
789 else
790 {
791 wxFAIL_MSG(_T("wrong listbox index"));
792 }
793 }
794
795 void wxListBox::SetStringSelection( const wxString &string, bool select )
796 {
797 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
798
799 SetSelection( FindString(string), select );
800 }
801
802 int wxListBox::GetIndex( GtkWidget *item ) const
803 {
804 if (item)
805 {
806 GList *child = m_list->children;
807 int count = 0;
808 while (child)
809 {
810 if (GTK_WIDGET(child->data) == item) return count;
811 count++;
812 child = child->next;
813 }
814 }
815 return -1;
816 }
817
818 #if wxUSE_TOOLTIPS
819 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
820 {
821 GList *child = m_list->children;
822 while (child)
823 {
824 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConv_local.cWX2MB(tip), (gchar*) NULL );
825 child = child->next;
826 }
827 }
828 #endif // wxUSE_TOOLTIPS
829
830 #if wxUSE_DRAG_AND_DROP
831 void wxListBox::SetDropTarget( wxDropTarget *dropTarget )
832 {
833 wxCHECK_RET( m_list != NULL, _T("invalid listbox") );
834
835 #ifndef NEW_GTK_DND_CODE
836 if (m_dropTarget)
837 {
838 GList *child = m_list->children;
839 while (child)
840 {
841 m_dropTarget->UnregisterWidget( GTK_WIDGET( child->data ) );
842 child = child->next;
843 }
844 }
845 #endif
846
847 wxWindow::SetDropTarget( dropTarget );
848
849 #ifndef NEW_GTK_DND_CODE
850 if (m_dropTarget)
851 {
852 GList *child = m_list->children;
853 while (child)
854 {
855 m_dropTarget->RegisterWidget( GTK_WIDGET( child->data ) );
856 child = child->next;
857 }
858 }
859 #endif
860 }
861 #endif
862
863 GtkWidget *wxListBox::GetConnectWidget()
864 {
865 return GTK_WIDGET(m_list);
866 }
867
868 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
869 {
870 if (wxWindow::IsOwnGtkWindow( window )) return TRUE;
871
872 GList *child = m_list->children;
873 while (child)
874 {
875 GtkWidget *bin = GTK_WIDGET( child->data );
876 if (bin->window == window) return TRUE;
877 child = child->next;
878 }
879
880 return FALSE;
881 }
882
883 void wxListBox::ApplyWidgetStyle()
884 {
885 SetWidgetStyle();
886
887 if (m_backgroundColour.Ok())
888 {
889 GdkWindow *window = GTK_WIDGET(m_list)->window;
890 if ( window )
891 {
892 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
893 gdk_window_set_background( window, m_backgroundColour.GetColor() );
894 gdk_window_clear( window );
895 }
896 }
897
898 GList *child = m_list->children;
899 while (child)
900 {
901 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
902
903 GtkBin *bin = GTK_BIN( child->data );
904 GtkWidget *label = GTK_WIDGET( bin->child );
905 gtk_widget_set_style( label, m_widgetStyle );
906
907 child = child->next;
908 }
909 }