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