1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "listbox.h"
15 #include "wx/listbox.h"
19 #include "wx/dynarray.h"
22 #include "wx/checklst.h"
23 #include "wx/settings.h"
26 #include "wx/tooltip.h"
31 #include <gdk/gdkkeysyms.h>
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern void wxapp_install_idle_handler();
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 #if wxUSE_CHECKLISTBOX
46 // checklistboxes have "[±] " prepended to their lables, this macro removes it
47 // (NB: 4 below is the length of wxCHECKLBOX_STRING above)
49 // the argument to it is a "const char *" pointer
50 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
52 #else // !wxUSE_CHECKLISTBOX
54 #define GET_REAL_LABEL(label) (label)
56 #endif // wxUSE_CHECKLISTBOX
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 extern bool g_blockEventsOnDrag
;
63 extern bool g_blockEventsOnScroll
;
64 extern wxCursor g_globalCursor
;
66 static bool g_hasDoubleClicked
= FALSE
;
68 //-----------------------------------------------------------------------------
69 // idle callback for SetFirstItem
70 //-----------------------------------------------------------------------------
72 struct wxlistbox_idle_struct
79 extern "C" gint
wxlistbox_idle_callback( gpointer gdata
)
81 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
84 gtk_idle_remove( data
->m_tag
);
86 // check that the items haven't been deleted from the listbox since we had
87 // installed this callback
88 wxListBox
*lbox
= data
->m_listbox
;
89 if ( data
->m_item
< lbox
->GetCount() )
91 lbox
->SetFirstItem( data
->m_item
);
101 //-----------------------------------------------------------------------------
102 // "button_release_event"
103 //-----------------------------------------------------------------------------
105 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
106 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
107 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
108 this can lead to race conditions so that we emit the dclick event
109 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
112 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
113 GdkEventButton
* WXUNUSED(gdk_event
),
116 if (g_isIdle
) wxapp_install_idle_handler();
118 if (g_blockEventsOnDrag
) return FALSE
;
119 if (g_blockEventsOnScroll
) return FALSE
;
121 if (!listbox
->m_hasVMT
) return FALSE
;
123 if (!g_hasDoubleClicked
) return FALSE
;
125 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
126 event
.SetEventObject( listbox
);
128 wxArrayInt aSelections
;
129 int n
, count
= listbox
->GetSelections(aSelections
);
133 if ( listbox
->HasClientObjectData() )
134 event
.SetClientObject( listbox
->GetClientObject(n
) );
135 else if ( listbox
->HasClientUntypedData() )
136 event
.SetClientData( listbox
->GetClientData(n
) );
137 event
.SetString( listbox
->GetString(n
) );
144 event
.m_commandInt
= n
;
146 listbox
->GetEventHandler()->ProcessEvent( event
);
151 //-----------------------------------------------------------------------------
152 // "button_press_event"
153 //-----------------------------------------------------------------------------
156 gtk_listbox_button_press_callback( GtkWidget
*widget
,
157 GdkEventButton
*gdk_event
,
160 if (g_isIdle
) wxapp_install_idle_handler();
162 if (g_blockEventsOnDrag
) return FALSE
;
163 if (g_blockEventsOnScroll
) return FALSE
;
165 if (!listbox
->m_hasVMT
) return FALSE
;
167 int sel
= listbox
->GtkGetIndex( widget
);
169 #if wxUSE_CHECKLISTBOX
170 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
172 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
174 clb
->Check( sel
, !clb
->IsChecked(sel
) );
176 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
177 event
.SetEventObject( listbox
);
179 listbox
->GetEventHandler()->ProcessEvent( event
);
181 #endif // wxUSE_CHECKLISTBOX
183 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
184 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
189 //-----------------------------------------------------------------------------
191 //-----------------------------------------------------------------------------
194 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
197 wxapp_install_idle_handler();
199 if (g_blockEventsOnDrag
)
204 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
206 wxNavigationKeyEvent new_event
;
207 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
208 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
209 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
210 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
211 new_event
.SetCurrentFocus( listbox
);
212 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
215 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
217 // eat return in all modes
221 #if wxUSE_CHECKLISTBOX
222 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
224 int sel
= listbox
->GtkGetIndex( widget
);
226 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
228 clb
->Check( sel
, !clb
->IsChecked(sel
) );
230 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
231 new_event
.SetEventObject( listbox
);
232 new_event
.SetInt( sel
);
233 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
235 #endif // wxUSE_CHECKLISTBOX
239 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
246 //-----------------------------------------------------------------------------
247 // "select" and "deselect"
248 //-----------------------------------------------------------------------------
250 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
);
252 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
254 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
257 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
259 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
262 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
)
264 if (g_isIdle
) wxapp_install_idle_handler();
266 if (!listbox
->m_hasVMT
) return;
267 if (g_blockEventsOnDrag
) return;
269 if (listbox
->m_blockEvent
) return;
271 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
272 event
.SetEventObject( listbox
);
274 // MSW doesn't do that either
275 // event.SetExtraLong( (long) is_selection );
278 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
280 int sel
= listbox
->GtkGetIndex( widget
);
282 if (listbox
->m_prevSelection
!= sel
)
283 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
285 listbox
->m_prevSelection
= sel
;
288 wxArrayInt aSelections
;
289 int n
, count
= listbox
->GetSelections(aSelections
);
293 if ( listbox
->HasClientObjectData() )
294 event
.SetClientObject( listbox
->GetClientObject(n
) );
295 else if ( listbox
->HasClientUntypedData() )
296 event
.SetClientData( listbox
->GetClientData(n
) );
297 event
.SetString( listbox
->GetString(n
) );
304 event
.m_commandInt
= n
;
306 // No longer required with new code in wxLB_SINGLE
307 // listbox->GetEventHandler()->AddPendingEvent( event );
308 listbox
->GetEventHandler()->ProcessEvent( event
);
311 //-----------------------------------------------------------------------------
313 //-----------------------------------------------------------------------------
315 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
317 // ----------------------------------------------------------------------------
319 // ----------------------------------------------------------------------------
321 wxListBox::wxListBox()
323 m_list
= (GtkList
*) NULL
;
324 #if wxUSE_CHECKLISTBOX
325 m_hasCheckBoxes
= FALSE
;
326 #endif // wxUSE_CHECKLISTBOX
329 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
330 const wxPoint
&pos
, const wxSize
&size
,
331 int n
, const wxString choices
[],
332 long style
, const wxValidator
& validator
,
333 const wxString
&name
)
336 m_acceptsFocus
= TRUE
;
338 m_prevSelection
= 0; // or -1 ??
339 m_blockEvent
= FALSE
;
341 if (!PreCreation( parent
, pos
, size
) ||
342 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
344 wxFAIL_MSG( wxT("wxListBox creation failed") );
348 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
349 if (style
& wxLB_ALWAYS_SB
)
351 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
352 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
356 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
357 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
360 m_list
= GTK_LIST( gtk_list_new() );
362 GtkSelectionMode mode
;
363 if (style
& wxLB_MULTIPLE
)
365 mode
= GTK_SELECTION_MULTIPLE
;
367 else if (style
& wxLB_EXTENDED
)
369 mode
= GTK_SELECTION_EXTENDED
;
373 // if style was 0 set single mode
374 m_windowStyle
|= wxLB_SINGLE
;
375 mode
= GTK_SELECTION_MULTIPLE
;
378 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
380 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
382 /* make list scroll when moving the focus down using cursor keys */
383 gtk_container_set_focus_vadjustment(
384 GTK_CONTAINER(m_list
),
385 gtk_scrolled_window_get_vadjustment(
386 GTK_SCROLLED_WINDOW(m_widget
)));
388 gtk_widget_show( GTK_WIDGET(m_list
) );
390 if ( style
& wxLB_SORT
)
392 // this will change DoAppend() behaviour
393 m_strings
= new wxSortedArrayString
;
397 m_strings
= (wxSortedArrayString
*)NULL
;
400 for (int i
= 0; i
< n
; i
++)
403 DoAppend(choices
[i
]);
406 // call it after appending the strings to the listbox, otherwise it doesn't
410 m_parent
->DoAddChild( this );
414 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
415 SetForegroundColour( parent
->GetForegroundColour() );
416 SetFont( parent
->GetFont() );
423 wxListBox::~wxListBox()
433 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
435 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
437 // VZ: notice that InsertItems knows nothing about sorting, so calling it
438 // from outside (and not from our own Append) is likely to break
441 // code elsewhere supposes we have as many items in m_clientList as items
443 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
444 wxT("bug in client data management") );
446 GList
*children
= m_list
->children
;
447 int length
= g_list_length(children
);
449 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
451 size_t nItems
= items
.GetCount();
456 for (size_t n
= 0; n
< nItems
; n
++)
458 index
= m_strings
->Add( items
[n
] );
460 if (index
!= GetCount())
462 GtkAddItem( items
[n
], index
);
463 wxNode
*node
= m_clientList
.Nth( index
);
464 m_clientList
.Insert( node
, (wxObject
*) NULL
);
468 GtkAddItem( items
[n
] );
469 m_clientList
.Append( (wxObject
*) NULL
);
477 for ( size_t n
= 0; n
< nItems
; n
++ )
479 GtkAddItem( items
[n
] );
481 m_clientList
.Append((wxObject
*)NULL
);
486 wxNode
*node
= m_clientList
.Nth( pos
);
487 for ( size_t n
= 0; n
< nItems
; n
++ )
489 GtkAddItem( items
[n
], pos
+n
);
491 m_clientList
.Insert( node
, (wxObject
*)NULL
);
496 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
497 wxT("bug in client data management") );
500 int wxListBox::DoAppend( const wxString
& item
)
504 // need to determine the index
505 int index
= m_strings
->Add( item
);
507 // only if not at the end anyway
508 if (index
!= GetCount())
510 GtkAddItem( item
, index
);
512 wxNode
*node
= m_clientList
.Nth( index
);
513 m_clientList
.Insert( node
, (wxObject
*)NULL
);
521 m_clientList
.Append((wxObject
*)NULL
);
523 return GetCount() - 1;
526 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
528 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
530 GtkWidget
*list_item
;
532 wxString
label(item
);
533 #if wxUSE_CHECKLISTBOX
536 label
.Prepend(wxCHECKLBOX_STRING
);
538 #endif // wxUSE_CHECKLISTBOX
540 list_item
= gtk_list_item_new_with_label( label
.mbc_str() );
542 GList
*gitem_list
= g_list_alloc ();
543 gitem_list
->data
= list_item
;
546 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
548 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
550 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
551 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
553 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
554 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
555 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
557 gtk_signal_connect( GTK_OBJECT(list_item
),
558 "button_press_event",
559 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
562 gtk_signal_connect_after( GTK_OBJECT(list_item
),
563 "button_release_event",
564 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
567 gtk_signal_connect( GTK_OBJECT(list_item
),
569 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
572 ConnectWidget( list_item
);
574 gtk_widget_show( list_item
);
576 if (GTK_WIDGET_REALIZED(m_widget
))
578 gtk_widget_realize( list_item
);
579 gtk_widget_realize( GTK_BIN(list_item
)->child
);
581 // Apply current widget style to the new list_item
584 gtk_widget_set_style( GTK_WIDGET( list_item
), m_widgetStyle
);
585 GtkBin
*bin
= GTK_BIN( list_item
);
586 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
587 gtk_widget_set_style( label
, m_widgetStyle
);
591 if (m_tooltip
) m_tooltip
->Apply( this );
596 void wxListBox::DoSetItems( const wxArrayString
& items
,
601 DoInsertItems(items
, 0);
605 size_t count
= items
.GetCount();
606 for ( size_t n
= 0; n
< count
; n
++ )
608 SetClientData(n
, clientData
[n
]);
613 // ----------------------------------------------------------------------------
615 // ----------------------------------------------------------------------------
617 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
619 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
621 wxNode
*node
= m_clientList
.Nth( n
);
622 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
624 node
->SetData( (wxObject
*) clientData
);
627 void* wxListBox::DoGetItemClientData( int n
) const
629 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
631 wxNode
*node
= m_clientList
.Nth( n
);
632 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
637 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
639 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
641 wxNode
*node
= m_clientList
.Nth( n
);
642 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
644 wxClientData
*cd
= (wxClientData
*) node
->Data();
647 node
->SetData( (wxObject
*) clientData
);
650 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
652 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
654 wxNode
*node
= m_clientList
.Nth( n
);
655 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
656 wxT("invalid index in wxListBox::DoGetItemClientObject") );
658 return (wxClientData
*) node
->Data();
661 void wxListBox::Clear()
663 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
665 gtk_list_clear_items( m_list
, 0, GetCount() );
667 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
669 // This should be NULL, I think.
670 GTK_LIST(m_list
)->last_focus_child
= NULL
;
673 if ( HasClientObjectData() )
675 // destroy the data (due to Robert's idea of using wxList<wxObject>
676 // and not wxList<wxClientData> we can't just say
677 // m_clientList.DeleteContents(TRUE) - this would crash!
678 wxNode
*node
= m_clientList
.First();
681 delete (wxClientData
*)node
->Data();
685 m_clientList
.Clear();
691 void wxListBox::Delete( int n
)
693 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
695 GList
*child
= g_list_nth( m_list
->children
, n
);
697 wxCHECK_RET( child
, wxT("wrong listbox index") );
699 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
700 gtk_list_remove_items( m_list
, list
);
703 wxNode
*node
= m_clientList
.Nth( n
);
706 if ( m_clientDataItemsType
== wxClientData_Object
)
708 wxClientData
*cd
= (wxClientData
*)node
->Data();
712 m_clientList
.DeleteNode( node
);
716 m_strings
->Remove(n
);
719 // ----------------------------------------------------------------------------
720 // string list access
721 // ----------------------------------------------------------------------------
723 void wxListBox::SetString( int n
, const wxString
&string
)
725 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
727 GList
*child
= g_list_nth( m_list
->children
, n
);
730 GtkBin
*bin
= GTK_BIN( child
->data
);
731 GtkLabel
*label
= GTK_LABEL( bin
->child
);
734 #if wxUSE_CHECKLISTBOX
736 str
+= wxCHECKLBOX_STRING
;
737 #endif // wxUSE_CHECKLISTBOX
740 gtk_label_set( label
, str
.mbc_str() );
744 wxFAIL_MSG(wxT("wrong listbox index"));
748 wxString
wxListBox::GetString( int n
) const
750 wxCHECK_MSG( m_list
!= NULL
, wxT(""), wxT("invalid listbox") );
752 GList
*child
= g_list_nth( m_list
->children
, n
);
755 GtkBin
*bin
= GTK_BIN( child
->data
);
756 GtkLabel
*label
= GTK_LABEL( bin
->child
);
758 wxString str
= wxString(GET_REAL_LABEL(label
->label
),*wxConvCurrent
);
763 wxFAIL_MSG(wxT("wrong listbox index"));
768 int wxListBox::GetCount() const
770 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
772 GList
*children
= m_list
->children
;
773 return g_list_length(children
);
776 int wxListBox::FindString( const wxString
&item
) const
778 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
780 GList
*child
= m_list
->children
;
784 GtkBin
*bin
= GTK_BIN( child
->data
);
785 GtkLabel
*label
= GTK_LABEL( bin
->child
);
787 wxString str
= wxString(GET_REAL_LABEL(label
->label
),*wxConvCurrent
);
796 // it's not an error if the string is not found -> no wxCHECK
801 // ----------------------------------------------------------------------------
803 // ----------------------------------------------------------------------------
805 int wxListBox::GetSelection() const
807 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
809 GList
*child
= m_list
->children
;
813 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
820 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
822 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
824 // get the number of selected items first
825 GList
*child
= m_list
->children
;
827 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
829 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
838 aSelections
.Alloc(count
); // optimization attempt
840 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
842 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
850 bool wxListBox::IsSelected( int n
) const
852 wxCHECK_MSG( m_list
!= NULL
, FALSE
, wxT("invalid listbox") );
854 GList
*target
= g_list_nth( m_list
->children
, n
);
856 wxCHECK_MSG( target
, FALSE
, wxT("invalid listbox index") );
858 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
861 void wxListBox::SetSelection( int n
, bool select
)
863 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
869 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
870 gtk_list_unselect_item( m_list
, m_prevSelection
);
871 gtk_list_select_item( m_list
, n
);
875 gtk_list_unselect_item( m_list
, n
);
877 m_blockEvent
= FALSE
;
880 void wxListBox::DoSetFirstItem( int n
)
882 wxCHECK_RET( m_list
, wxT("invalid listbox") );
884 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
887 // terribly efficient
888 const gchar
*vadjustment_key
= "gtk-vadjustment";
889 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
891 GtkAdjustment
*adjustment
=
892 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
893 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
895 GList
*target
= g_list_nth( m_list
->children
, n
);
896 wxCHECK_RET( target
, wxT("invalid listbox index") );
898 GtkWidget
*item
= GTK_WIDGET(target
->data
);
899 wxCHECK_RET( item
, wxT("invalid listbox code") );
901 if (item
->allocation
.y
== -1)
903 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
904 data
->m_listbox
= this;
906 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
911 float y
= item
->allocation
.y
;
912 if (y
> adjustment
->upper
- adjustment
->page_size
)
913 y
= adjustment
->upper
- adjustment
->page_size
;
914 gtk_adjustment_set_value( adjustment
, y
);
917 // ----------------------------------------------------------------------------
919 // ----------------------------------------------------------------------------
921 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
925 GList
*child
= m_list
->children
;
929 if (GTK_WIDGET(child
->data
) == item
) return count
;
938 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
940 GList
*child
= m_list
->children
;
943 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
947 #endif // wxUSE_TOOLTIPS
949 GtkWidget
*wxListBox::GetConnectWidget()
951 return GTK_WIDGET(m_list
);
954 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
956 if (GTK_WIDGET(m_list
)->window
== window
) return TRUE
;
958 GList
*child
= m_list
->children
;
961 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
962 if (bin
->window
== window
) return TRUE
;
969 void wxListBox::ApplyWidgetStyle()
973 if (m_backgroundColour
.Ok())
975 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
978 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
979 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
980 gdk_window_clear( window
);
984 GList
*child
= m_list
->children
;
987 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
989 GtkBin
*bin
= GTK_BIN( child
->data
);
990 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
991 gtk_widget_set_style( label
, m_widgetStyle
);
997 void wxListBox::OnInternalIdle()
999 wxCursor cursor
= m_cursor
;
1000 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1002 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1004 /* I now set the cursor the anew in every OnInternalIdle call
1005 as setting the cursor in a parent window also effects the
1006 windows above so that checking for the current cursor is
1009 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1011 GList
*child
= m_list
->children
;
1014 GtkBin
*bin
= GTK_BIN( child
->data
);
1015 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1020 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1022 child
= child
->next
;
1029 wxSize
wxListBox::DoGetBestSize() const
1031 int lbWidth
= 100; // some defaults
1035 // Find the widest line
1036 for(int i
= 0; i
< GetCount(); i
++) {
1037 wxString
str(GetString(i
));
1038 GetTextExtent(str
, &wLine
, NULL
);
1039 lbWidth
= wxMax(lbWidth
, wLine
);
1042 // Add room for the scrollbar
1043 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1045 // And just a bit more
1047 GetTextExtent("X", &cx
, &cy
);
1050 // don't make the listbox too tall (limit height to around 10 items) but don't
1051 // make it too small neither
1052 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1054 return wxSize(lbWidth
, lbHeight
);