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