Use InheritAttributes for wxGTK widgets so they will check
[wxWidgets.git] / src / gtk1 / 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "listbox.h"
13 #endif
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #include "wx/defs.h"
19
20 #if wxUSE_LISTBOX
21
22 #include "wx/listbox.h"
23 #include "wx/dynarray.h"
24 #include "wx/arrstr.h"
25 #include "wx/utils.h"
26 #include "wx/intl.h"
27 #include "wx/checklst.h"
28 #include "wx/settings.h"
29 #include "wx/gtk/private.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 // data
48 //-----------------------------------------------------------------------------
49
50 extern bool g_blockEventsOnDrag;
51 extern bool g_blockEventsOnScroll;
52 extern wxCursor g_globalCursor;
53 extern wxWindowGTK *g_delayedFocus;
54
55 static bool g_hasDoubleClicked = FALSE;
56
57 //-----------------------------------------------------------------------------
58 // idle callback for SetFirstItem
59 //-----------------------------------------------------------------------------
60
61 struct wxlistbox_idle_struct
62 {
63 wxListBox *m_listbox;
64 int m_item;
65 gint m_tag;
66 };
67
68 extern "C" gint wxlistbox_idle_callback( gpointer gdata )
69 {
70 wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata;
71 gdk_threads_enter();
72
73 gtk_idle_remove( data->m_tag );
74
75 // check that the items haven't been deleted from the listbox since we had
76 // installed this callback
77 wxListBox *lbox = data->m_listbox;
78 if ( data->m_item < lbox->GetCount() )
79 {
80 lbox->SetFirstItem( data->m_item );
81 }
82
83 delete data;
84
85 gdk_threads_leave();
86
87 return TRUE;
88 }
89
90 //-----------------------------------------------------------------------------
91 // "button_release_event"
92 //-----------------------------------------------------------------------------
93
94 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
95 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
96 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
97 this can lead to race conditions so that we emit the dclick event
98 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
99
100 static gint
101 gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget),
102 GdkEventButton * WXUNUSED(gdk_event),
103 wxListBox *listbox )
104 {
105 if (g_isIdle) wxapp_install_idle_handler();
106
107 if (g_blockEventsOnDrag) return FALSE;
108 if (g_blockEventsOnScroll) return FALSE;
109
110 if (!listbox->m_hasVMT) return FALSE;
111
112 if (!g_hasDoubleClicked) return FALSE;
113
114 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
115 event.SetEventObject( listbox );
116
117 wxArrayInt aSelections;
118 int n, count = listbox->GetSelections(aSelections);
119 if ( count > 0 )
120 {
121 n = aSelections[0];
122 if ( listbox->HasClientObjectData() )
123 event.SetClientObject( listbox->GetClientObject(n) );
124 else if ( listbox->HasClientUntypedData() )
125 event.SetClientData( listbox->GetClientData(n) );
126 event.SetString( listbox->GetString(n) );
127 }
128 else
129 {
130 n = -1;
131 }
132
133 event.m_commandInt = n;
134
135 listbox->GetEventHandler()->ProcessEvent( event );
136
137 return FALSE;
138 }
139
140 //-----------------------------------------------------------------------------
141 // "button_press_event"
142 //-----------------------------------------------------------------------------
143
144 static gint
145 gtk_listbox_button_press_callback( GtkWidget *widget,
146 GdkEventButton *gdk_event,
147 wxListBox *listbox )
148 {
149 if (g_isIdle) wxapp_install_idle_handler();
150
151 if (g_blockEventsOnDrag) return FALSE;
152 if (g_blockEventsOnScroll) return FALSE;
153
154 if (!listbox->m_hasVMT) return FALSE;
155
156 int sel = listbox->GtkGetIndex( widget );
157
158 #if wxUSE_CHECKLISTBOX
159 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
160 {
161 wxCheckListBox *clb = (wxCheckListBox *)listbox;
162
163 clb->Check( sel, !clb->IsChecked(sel) );
164
165 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
166 event.SetEventObject( listbox );
167 event.SetInt( sel );
168 listbox->GetEventHandler()->ProcessEvent( event );
169 }
170 #endif // wxUSE_CHECKLISTBOX
171
172 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
173 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
174
175 return FALSE;
176 }
177
178 //-----------------------------------------------------------------------------
179 // "key_press_event"
180 //-----------------------------------------------------------------------------
181
182 static gint
183 gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
184 {
185 if (g_isIdle)
186 wxapp_install_idle_handler();
187
188 if (g_blockEventsOnDrag)
189 return FALSE;
190
191 bool ret = FALSE;
192
193 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
194 {
195 wxNavigationKeyEvent new_event;
196 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
197 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
198 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
199 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
200 new_event.SetCurrentFocus( listbox );
201 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
202 }
203
204 if ((gdk_event->keyval == GDK_Return) && (!ret))
205 {
206 // eat return in all modes
207 ret = TRUE;
208 }
209
210 #if wxUSE_CHECKLISTBOX
211 if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret))
212 {
213 int sel = listbox->GtkGetIndex( widget );
214
215 wxCheckListBox *clb = (wxCheckListBox *)listbox;
216
217 clb->Check( sel, !clb->IsChecked(sel) );
218
219 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
220 new_event.SetEventObject( listbox );
221 new_event.SetInt( sel );
222 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
223 }
224 #endif // wxUSE_CHECKLISTBOX
225
226 if (ret)
227 {
228 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
229 return TRUE;
230 }
231
232 return FALSE;
233 }
234
235 //-----------------------------------------------------------------------------
236 // "select" and "deselect"
237 //-----------------------------------------------------------------------------
238
239 static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection );
240
241 static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox )
242 {
243 gtk_listitem_select_cb( widget, listbox, TRUE );
244 }
245
246 static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
247 {
248 gtk_listitem_select_cb( widget, listbox, FALSE );
249 }
250
251 static void gtk_listitem_select_cb( GtkWidget *widget,
252 wxListBox *listbox,
253 bool is_selection )
254 {
255 if (g_isIdle) wxapp_install_idle_handler();
256
257 if (!listbox->m_hasVMT) return;
258 if (g_blockEventsOnDrag) return;
259
260 if (listbox->m_blockEvent) return;
261
262 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
263 event.SetEventObject( listbox );
264
265 // indicate whether this is a selection or a deselection
266 event.SetExtraLong( is_selection );
267
268 if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0)
269 {
270 int sel = listbox->GtkGetIndex( widget );
271
272 if (listbox->m_prevSelection != sel)
273 gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection );
274
275 listbox->m_prevSelection = sel;
276 }
277
278 wxArrayInt aSelections;
279 int n, count = listbox->GetSelections(aSelections);
280 if ( count > 0 )
281 {
282 n = aSelections[0];
283 if ( listbox->HasClientObjectData() )
284 event.SetClientObject( listbox->GetClientObject(n) );
285 else if ( listbox->HasClientUntypedData() )
286 event.SetClientData( listbox->GetClientData(n) );
287 event.SetString( listbox->GetString(n) );
288 }
289 else
290 {
291 n = -1;
292 }
293
294 event.m_commandInt = n;
295
296 // No longer required with new code in wxLB_SINGLE
297 // listbox->GetEventHandler()->AddPendingEvent( event );
298 listbox->GetEventHandler()->ProcessEvent( event );
299 }
300
301 //-----------------------------------------------------------------------------
302 // wxListBox
303 //-----------------------------------------------------------------------------
304
305 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
306
307 // ----------------------------------------------------------------------------
308 // construction
309 // ----------------------------------------------------------------------------
310
311 wxListBox::wxListBox()
312 {
313 m_list = (GtkList *) NULL;
314 #if wxUSE_CHECKLISTBOX
315 m_hasCheckBoxes = FALSE;
316 #endif // wxUSE_CHECKLISTBOX
317 }
318
319 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
320 const wxPoint &pos, const wxSize &size,
321 int n, const wxString choices[],
322 long style, const wxValidator& validator,
323 const wxString &name )
324 {
325 m_needParent = TRUE;
326 m_acceptsFocus = TRUE;
327 m_prevSelection = 0; // or -1 ??
328 m_blockEvent = FALSE;
329
330 if (!PreCreation( parent, pos, size ) ||
331 !CreateBase( parent, id, pos, size, style, validator, name ))
332 {
333 wxFAIL_MSG( wxT("wxListBox creation failed") );
334 return FALSE;
335 }
336
337 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
338 if (style & wxLB_ALWAYS_SB)
339 {
340 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
341 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
342 }
343 else
344 {
345 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
346 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
347 }
348
349 m_list = GTK_LIST( gtk_list_new() );
350
351 GtkSelectionMode mode;
352 if (style & wxLB_MULTIPLE)
353 {
354 mode = GTK_SELECTION_MULTIPLE;
355 }
356 else if (style & wxLB_EXTENDED)
357 {
358 mode = GTK_SELECTION_EXTENDED;
359 }
360 else
361 {
362 // if style was 0 set single mode
363 m_windowStyle |= wxLB_SINGLE;
364 mode = GTK_SELECTION_MULTIPLE;
365 }
366
367 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
368
369 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
370
371 /* make list scroll when moving the focus down using cursor keys */
372 gtk_container_set_focus_vadjustment(
373 GTK_CONTAINER(m_list),
374 gtk_scrolled_window_get_vadjustment(
375 GTK_SCROLLED_WINDOW(m_widget)));
376
377 gtk_widget_show( GTK_WIDGET(m_list) );
378
379 if ( style & wxLB_SORT )
380 {
381 // this will change DoAppend() behaviour
382 m_strings = new wxSortedArrayString;
383 }
384 else
385 {
386 m_strings = (wxSortedArrayString *)NULL;
387 }
388
389 for (int i = 0; i < n; i++)
390 {
391 // add one by one
392 DoAppend(choices[i]);
393 }
394
395 // call it after appending the strings to the listbox, otherwise it doesn't
396 // work correctly
397 SetBestSize( size );
398
399 m_parent->DoAddChild( this );
400
401 PostCreation();
402 InheritAttributes();
403
404 Show( TRUE );
405
406 return TRUE;
407 }
408
409 wxListBox::~wxListBox()
410 {
411 m_hasVMT = FALSE;
412
413 Clear();
414
415 if (m_strings)
416 delete m_strings;
417 }
418
419 // ----------------------------------------------------------------------------
420 // adding items
421 // ----------------------------------------------------------------------------
422
423 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
424 {
425 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
426
427 // VZ: notice that InsertItems knows nothing about sorting, so calling it
428 // from outside (and not from our own Append) is likely to break
429 // everything
430
431 // code elsewhere supposes we have as many items in m_clientList as items
432 // in the listbox
433 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
434 wxT("bug in client data management") );
435
436 GList *children = m_list->children;
437 int length = g_list_length(children);
438
439 wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") );
440
441 size_t nItems = items.GetCount();
442 int index;
443
444 if (m_strings)
445 {
446 for (size_t n = 0; n < nItems; n++)
447 {
448 index = m_strings->Add( items[n] );
449
450 if (index != GetCount())
451 {
452 GtkAddItem( items[n], index );
453 wxList::compatibility_iterator node = m_clientList.Item( index );
454 m_clientList.Insert( node, (wxObject*) NULL );
455 }
456 else
457 {
458 GtkAddItem( items[n] );
459 m_clientList.Append( (wxObject*) NULL );
460 }
461 }
462 }
463 else
464 {
465 if (pos == length)
466 {
467 for ( size_t n = 0; n < nItems; n++ )
468 {
469 GtkAddItem( items[n] );
470
471 m_clientList.Append((wxObject *)NULL);
472 }
473 }
474 else
475 {
476 wxList::compatibility_iterator node = m_clientList.Item( pos );
477 for ( size_t n = 0; n < nItems; n++ )
478 {
479 GtkAddItem( items[n], pos+n );
480
481 m_clientList.Insert( node, (wxObject *)NULL );
482 }
483 }
484 }
485
486 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
487 wxT("bug in client data management") );
488 }
489
490 int wxListBox::DoAppend( const wxString& item )
491 {
492 if (m_strings)
493 {
494 // need to determine the index
495 int index = m_strings->Add( item );
496
497 // only if not at the end anyway
498 if (index != GetCount())
499 {
500 GtkAddItem( item, index );
501
502 wxList::compatibility_iterator node = m_clientList.Item( index );
503 m_clientList.Insert( node, (wxObject *)NULL );
504
505 return index;
506 }
507 }
508
509 GtkAddItem(item);
510
511 m_clientList.Append((wxObject *)NULL);
512
513 return GetCount() - 1;
514 }
515
516 void wxListBox::GtkAddItem( const wxString &item, int pos )
517 {
518 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
519
520 GtkWidget *list_item;
521
522 wxString label(item);
523 #if wxUSE_CHECKLISTBOX
524 if (m_hasCheckBoxes)
525 {
526 label.Prepend(wxCHECKLBOX_STRING);
527 }
528 #endif // wxUSE_CHECKLISTBOX
529
530 list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) );
531
532 GList *gitem_list = g_list_alloc ();
533 gitem_list->data = list_item;
534
535 if (pos == -1)
536 gtk_list_append_items( GTK_LIST (m_list), gitem_list );
537 else
538 gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos );
539
540 gtk_signal_connect( GTK_OBJECT(list_item), "select",
541 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
542
543 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
544 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
545 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
546
547 gtk_signal_connect( GTK_OBJECT(list_item),
548 "button_press_event",
549 (GtkSignalFunc)gtk_listbox_button_press_callback,
550 (gpointer) this );
551
552 gtk_signal_connect_after( GTK_OBJECT(list_item),
553 "button_release_event",
554 (GtkSignalFunc)gtk_listbox_button_release_callback,
555 (gpointer) this );
556
557 gtk_signal_connect( GTK_OBJECT(list_item),
558 "key_press_event",
559 (GtkSignalFunc)gtk_listbox_key_press_callback,
560 (gpointer)this );
561
562 ConnectWidget( list_item );
563
564 gtk_widget_show( list_item );
565
566 if (GTK_WIDGET_REALIZED(m_widget))
567 {
568 gtk_widget_realize( list_item );
569 gtk_widget_realize( GTK_BIN(list_item)->child );
570
571 // Apply current widget style to the new list_item
572 if (m_widgetStyle)
573 {
574 gtk_widget_set_style( GTK_WIDGET( list_item ), m_widgetStyle );
575 GtkBin *bin = GTK_BIN( list_item );
576 GtkWidget *label = GTK_WIDGET( bin->child );
577 gtk_widget_set_style( label, m_widgetStyle );
578 }
579
580 #if wxUSE_TOOLTIPS
581 if (m_tooltip) m_tooltip->Apply( this );
582 #endif
583 }
584 }
585
586 void wxListBox::DoSetItems( const wxArrayString& items,
587 void **clientData)
588 {
589 Clear();
590
591 DoInsertItems(items, 0);
592
593 if ( clientData )
594 {
595 size_t count = items.GetCount();
596 for ( size_t n = 0; n < count; n++ )
597 {
598 SetClientData(n, clientData[n]);
599 }
600 }
601 }
602
603 // ----------------------------------------------------------------------------
604 // deleting items
605 // ----------------------------------------------------------------------------
606
607 void wxListBox::Clear()
608 {
609 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
610
611 gtk_list_clear_items( m_list, 0, GetCount() );
612
613 if ( GTK_LIST(m_list)->last_focus_child != NULL )
614 {
615 // This should be NULL, I think.
616 GTK_LIST(m_list)->last_focus_child = NULL;
617 }
618
619 if ( HasClientObjectData() )
620 {
621 // destroy the data (due to Robert's idea of using wxList<wxObject>
622 // and not wxList<wxClientData> we can't just say
623 // m_clientList.DeleteContents(TRUE) - this would crash!
624 wxList::compatibility_iterator node = m_clientList.GetFirst();
625 while ( node )
626 {
627 delete (wxClientData *)node->GetData();
628 node = node->GetNext();
629 }
630 }
631 m_clientList.Clear();
632
633 if ( m_strings )
634 m_strings->Clear();
635 }
636
637 void wxListBox::Delete( int n )
638 {
639 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
640
641 GList *child = g_list_nth( m_list->children, n );
642
643 wxCHECK_RET( child, wxT("wrong listbox index") );
644
645 GList *list = g_list_append( (GList*) NULL, child->data );
646 gtk_list_remove_items( m_list, list );
647 g_list_free( list );
648
649 wxList::compatibility_iterator node = m_clientList.Item( n );
650 if ( node )
651 {
652 if ( m_clientDataItemsType == wxClientData_Object )
653 {
654 wxClientData *cd = (wxClientData*)node->GetData();
655 delete cd;
656 }
657
658 m_clientList.Erase( node );
659 }
660
661 if ( m_strings )
662 m_strings->RemoveAt(n);
663 }
664
665 // ----------------------------------------------------------------------------
666 // client data
667 // ----------------------------------------------------------------------------
668
669 void wxListBox::DoSetItemClientData( int n, void* clientData )
670 {
671 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
672
673 wxList::compatibility_iterator node = m_clientList.Item( n );
674 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
675
676 node->SetData( (wxObject*) clientData );
677 }
678
679 void* wxListBox::DoGetItemClientData( int n ) const
680 {
681 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
682
683 wxList::compatibility_iterator node = m_clientList.Item( n );
684 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
685
686 return node->GetData();
687 }
688
689 void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData )
690 {
691 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
692
693 wxList::compatibility_iterator node = m_clientList.Item( n );
694 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") );
695
696 // wxItemContainer already deletes data for us
697
698 node->SetData( (wxObject*) clientData );
699 }
700
701 wxClientData* wxListBox::DoGetItemClientObject( int n ) const
702 {
703 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") );
704
705 wxList::compatibility_iterator node = m_clientList.Item( n );
706 wxCHECK_MSG( node, (wxClientData *)NULL,
707 wxT("invalid index in wxListBox::DoGetItemClientObject") );
708
709 return (wxClientData*) node->GetData();
710 }
711
712 // ----------------------------------------------------------------------------
713 // string list access
714 // ----------------------------------------------------------------------------
715
716 wxString wxListBox::GetRealLabel(GList *item) const
717 {
718 GtkBin *bin = GTK_BIN( item->data );
719 GtkLabel *label = GTK_LABEL( bin->child );
720
721 wxString str;
722
723 #ifdef __WXGTK20__
724 str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
725 #else
726 str = wxString( label->label );
727 #endif
728
729 #if wxUSE_CHECKLISTBOX
730 // checklistboxes have "[±] " prepended to their lables, remove it
731 //
732 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
733 if ( m_hasCheckBoxes )
734 str.erase(0, 4);
735 #endif // wxUSE_CHECKLISTBOX
736
737 return str;
738 }
739
740 void wxListBox::SetString( int n, const wxString &string )
741 {
742 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
743
744 GList *child = g_list_nth( m_list->children, n );
745 if (child)
746 {
747 GtkBin *bin = GTK_BIN( child->data );
748 GtkLabel *label = GTK_LABEL( bin->child );
749
750 wxString str;
751 #if wxUSE_CHECKLISTBOX
752 if (m_hasCheckBoxes)
753 str += wxCHECKLBOX_STRING;
754 #endif // wxUSE_CHECKLISTBOX
755 str += string;
756
757 gtk_label_set( label, wxGTK_CONV( str ) );
758 }
759 else
760 {
761 wxFAIL_MSG(wxT("wrong listbox index"));
762 }
763 }
764
765 wxString wxListBox::GetString( int n ) const
766 {
767 wxCHECK_MSG( m_list != NULL, wxT(""), wxT("invalid listbox") );
768
769 GList *child = g_list_nth( m_list->children, n );
770 if (child)
771 {
772 return GetRealLabel(child);
773 }
774
775 wxFAIL_MSG(wxT("wrong listbox index"));
776
777 return wxT("");
778 }
779
780 int wxListBox::GetCount() const
781 {
782 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
783
784 GList *children = m_list->children;
785 return g_list_length(children);
786 }
787
788 int wxListBox::FindString( const wxString &item ) const
789 {
790 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
791
792 GList *child = m_list->children;
793 int count = 0;
794 while (child)
795 {
796 if ( GetRealLabel(child) == item )
797 return count;
798
799 count++;
800 child = child->next;
801 }
802
803 // it's not an error if the string is not found -> no wxCHECK
804
805 return wxNOT_FOUND;
806 }
807
808 // ----------------------------------------------------------------------------
809 // selection
810 // ----------------------------------------------------------------------------
811
812 int wxListBox::GetSelection() const
813 {
814 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
815
816 GList *child = m_list->children;
817 int count = 0;
818 while (child)
819 {
820 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
821 count++;
822 child = child->next;
823 }
824 return -1;
825 }
826
827 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
828 {
829 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
830
831 // get the number of selected items first
832 GList *child = m_list->children;
833 int count = 0;
834 for (child = m_list->children; child != NULL; child = child->next)
835 {
836 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
837 count++;
838 }
839
840 aSelections.Empty();
841
842 if (count > 0)
843 {
844 // now fill the list
845 aSelections.Alloc(count); // optimization attempt
846 int i = 0;
847 for (child = m_list->children; child != NULL; child = child->next, i++)
848 {
849 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
850 aSelections.Add(i);
851 }
852 }
853
854 return count;
855 }
856
857 bool wxListBox::IsSelected( int n ) const
858 {
859 wxCHECK_MSG( m_list != NULL, FALSE, wxT("invalid listbox") );
860
861 GList *target = g_list_nth( m_list->children, n );
862
863 wxCHECK_MSG( target, FALSE, wxT("invalid listbox index") );
864
865 return (GTK_WIDGET(target->data)->state == GTK_STATE_SELECTED) ;
866 }
867
868 void wxListBox::SetSelection( int n, bool select )
869 {
870 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
871
872 m_blockEvent = TRUE;
873
874 if (select)
875 {
876 if ((m_windowStyle & wxLB_SINGLE) != 0)
877 gtk_list_unselect_item( m_list, m_prevSelection );
878 gtk_list_select_item( m_list, n );
879 m_prevSelection = n;
880 }
881 else
882 gtk_list_unselect_item( m_list, n );
883
884 m_blockEvent = FALSE;
885 }
886
887 void wxListBox::DoSetFirstItem( int n )
888 {
889 wxCHECK_RET( m_list, wxT("invalid listbox") );
890
891 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list))
892 return;
893
894 // terribly efficient
895 const gchar *vadjustment_key = "gtk-vadjustment";
896 guint vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
897
898 GtkAdjustment *adjustment =
899 (GtkAdjustment*) gtk_object_get_data_by_id (GTK_OBJECT (m_list), vadjustment_key_id);
900 wxCHECK_RET( adjustment, wxT("invalid listbox code") );
901
902 GList *target = g_list_nth( m_list->children, n );
903 wxCHECK_RET( target, wxT("invalid listbox index") );
904
905 GtkWidget *item = GTK_WIDGET(target->data);
906 wxCHECK_RET( item, wxT("invalid listbox code") );
907
908 if (item->allocation.y == -1)
909 {
910 wxlistbox_idle_struct* data = new wxlistbox_idle_struct;
911 data->m_listbox = this;
912 data->m_item = n;
913 data->m_tag = gtk_idle_add_priority( 800, wxlistbox_idle_callback, (gpointer) data );
914
915 return;
916 }
917
918 float y = item->allocation.y;
919 if (y > adjustment->upper - adjustment->page_size)
920 y = adjustment->upper - adjustment->page_size;
921 gtk_adjustment_set_value( adjustment, y );
922 }
923
924 // ----------------------------------------------------------------------------
925 // helpers
926 // ----------------------------------------------------------------------------
927
928 int wxListBox::GtkGetIndex( GtkWidget *item ) const
929 {
930 if (item)
931 {
932 GList *child = m_list->children;
933 int count = 0;
934 while (child)
935 {
936 if (GTK_WIDGET(child->data) == item) return count;
937 count++;
938 child = child->next;
939 }
940 }
941 return -1;
942 }
943
944 #if wxUSE_TOOLTIPS
945 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
946 {
947 GList *child = m_list->children;
948 while (child)
949 {
950 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
951 child = child->next;
952 }
953 }
954 #endif // wxUSE_TOOLTIPS
955
956 GtkWidget *wxListBox::GetConnectWidget()
957 {
958 return GTK_WIDGET(m_list);
959 }
960
961 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
962 {
963 if (m_widget->window == window) return TRUE;
964
965 if (GTK_WIDGET(m_list)->window == window) return TRUE;
966
967 GList *child = m_list->children;
968 while (child)
969 {
970 GtkWidget *bin = GTK_WIDGET( child->data );
971 if (bin->window == window) return TRUE;
972 child = child->next;
973 }
974
975 return FALSE;
976 }
977
978 void wxListBox::ApplyWidgetStyle()
979 {
980 SetWidgetStyle();
981
982 if (m_backgroundColour.Ok())
983 {
984 GdkWindow *window = GTK_WIDGET(m_list)->window;
985 if ( window )
986 {
987 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
988 gdk_window_set_background( window, m_backgroundColour.GetColor() );
989 gdk_window_clear( window );
990 }
991 }
992
993 GList *child = m_list->children;
994 while (child)
995 {
996 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
997
998 GtkBin *bin = GTK_BIN( child->data );
999 GtkWidget *label = GTK_WIDGET( bin->child );
1000 gtk_widget_set_style( label, m_widgetStyle );
1001
1002 child = child->next;
1003 }
1004 }
1005
1006 void wxListBox::OnInternalIdle()
1007 {
1008 wxCursor cursor = m_cursor;
1009 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1010
1011 if (GTK_WIDGET(m_list)->window && cursor.Ok())
1012 {
1013 /* I now set the cursor the anew in every OnInternalIdle call
1014 as setting the cursor in a parent window also effects the
1015 windows above so that checking for the current cursor is
1016 not possible. */
1017
1018 gdk_window_set_cursor( GTK_WIDGET(m_list)->window, cursor.GetCursor() );
1019
1020 GList *child = m_list->children;
1021 while (child)
1022 {
1023 GtkBin *bin = GTK_BIN( child->data );
1024 GtkWidget *label = GTK_WIDGET( bin->child );
1025
1026 if (!label->window)
1027 break;
1028 else
1029 gdk_window_set_cursor( label->window, cursor.GetCursor() );
1030
1031 child = child->next;
1032 }
1033 }
1034
1035 if (g_delayedFocus == this)
1036 {
1037 if (GTK_WIDGET_REALIZED(m_widget))
1038 {
1039 gtk_widget_grab_focus( m_widget );
1040 g_delayedFocus = NULL;
1041 }
1042 }
1043
1044 if (wxUpdateUIEvent::CanUpdate(this))
1045 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1046 }
1047
1048 wxSize wxListBox::DoGetBestSize() const
1049 {
1050 int lbWidth = 100; // some defaults
1051 int lbHeight = 110;
1052 int wLine;
1053
1054 // Find the widest line
1055 for(int i = 0; i < GetCount(); i++) {
1056 wxString str(GetString(i));
1057 GetTextExtent(str, &wLine, NULL);
1058 lbWidth = wxMax(lbWidth, wLine);
1059 }
1060
1061 // Add room for the scrollbar
1062 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1063
1064 // And just a bit more
1065 int cx, cy;
1066 GetTextExtent( wxT("X"), &cx, &cy);
1067 lbWidth += 3 * cx;
1068
1069 // don't make the listbox too tall (limit height to around 10 items) but don't
1070 // make it too small neither
1071 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1072
1073 return wxSize(lbWidth, lbHeight);
1074 }
1075
1076 void wxListBox::FixUpMouseEvent(GtkWidget *widget, wxCoord& x, wxCoord& y)
1077 {
1078 // the mouse event coords are relative to the listbox items, we need to
1079 // translate them to the normal client coords
1080 x += widget->allocation.x;
1081 y += widget->allocation.y;
1082 }
1083
1084 #endif // wxUSE_LISTBOX
1085