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