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"
24 #include "wx/gtk/private.h"
27 #include "wx/tooltip.h"
32 #include <gdk/gdkkeysyms.h>
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern void wxapp_install_idle_handler();
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 extern bool g_blockEventsOnDrag
;
46 extern bool g_blockEventsOnScroll
;
47 extern wxCursor g_globalCursor
;
48 extern wxWindowGTK
*g_delayedFocus
;
50 static bool g_hasDoubleClicked
= FALSE
;
52 //-----------------------------------------------------------------------------
53 // idle callback for SetFirstItem
54 //-----------------------------------------------------------------------------
56 struct wxlistbox_idle_struct
63 extern "C" gint
wxlistbox_idle_callback( gpointer gdata
)
65 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
68 gtk_idle_remove( data
->m_tag
);
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() )
75 lbox
->SetFirstItem( data
->m_item
);
85 //-----------------------------------------------------------------------------
86 // "button_release_event"
87 //-----------------------------------------------------------------------------
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 */
96 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
97 GdkEventButton
* WXUNUSED(gdk_event
),
100 if (g_isIdle
) wxapp_install_idle_handler();
102 if (g_blockEventsOnDrag
) return FALSE
;
103 if (g_blockEventsOnScroll
) return FALSE
;
105 if (!listbox
->m_hasVMT
) return FALSE
;
107 if (!g_hasDoubleClicked
) return FALSE
;
109 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
110 event
.SetEventObject( listbox
);
112 wxArrayInt aSelections
;
113 int n
, count
= listbox
->GetSelections(aSelections
);
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
) );
128 event
.m_commandInt
= n
;
130 listbox
->GetEventHandler()->ProcessEvent( event
);
135 //-----------------------------------------------------------------------------
136 // "button_press_event"
137 //-----------------------------------------------------------------------------
140 gtk_listbox_button_press_callback( GtkWidget
*widget
,
141 GdkEventButton
*gdk_event
,
144 if (g_isIdle
) wxapp_install_idle_handler();
146 if (g_blockEventsOnDrag
) return FALSE
;
147 if (g_blockEventsOnScroll
) return FALSE
;
149 if (!listbox
->m_hasVMT
) return FALSE
;
151 int sel
= listbox
->GtkGetIndex( widget
);
153 #if wxUSE_CHECKLISTBOX
154 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
156 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
158 clb
->Check( sel
, !clb
->IsChecked(sel
) );
160 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
161 event
.SetEventObject( listbox
);
163 listbox
->GetEventHandler()->ProcessEvent( event
);
165 #endif // wxUSE_CHECKLISTBOX
167 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
168 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
173 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
178 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
181 wxapp_install_idle_handler();
183 if (g_blockEventsOnDrag
)
188 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
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
);
199 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
201 // eat return in all modes
205 #if wxUSE_CHECKLISTBOX
206 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
208 int sel
= listbox
->GtkGetIndex( widget
);
210 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
212 clb
->Check( sel
, !clb
->IsChecked(sel
) );
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
);
219 #endif // wxUSE_CHECKLISTBOX
223 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
230 //-----------------------------------------------------------------------------
231 // "select" and "deselect"
232 //-----------------------------------------------------------------------------
234 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
);
236 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
238 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
241 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
243 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
246 static void gtk_listitem_select_cb( GtkWidget
*widget
,
250 if (g_isIdle
) wxapp_install_idle_handler();
252 if (!listbox
->m_hasVMT
) return;
253 if (g_blockEventsOnDrag
) return;
255 if (listbox
->m_blockEvent
) return;
257 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
258 event
.SetEventObject( listbox
);
260 // indicate whether this is a selection or a deselection
261 event
.SetExtraLong( is_selection
);
263 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
265 int sel
= listbox
->GtkGetIndex( widget
);
267 if (listbox
->m_prevSelection
!= sel
)
268 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
270 listbox
->m_prevSelection
= sel
;
273 wxArrayInt aSelections
;
274 int n
, count
= listbox
->GetSelections(aSelections
);
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
) );
289 event
.m_commandInt
= n
;
291 // No longer required with new code in wxLB_SINGLE
292 // listbox->GetEventHandler()->AddPendingEvent( event );
293 listbox
->GetEventHandler()->ProcessEvent( event
);
296 //-----------------------------------------------------------------------------
298 //-----------------------------------------------------------------------------
300 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 wxListBox::wxListBox()
308 m_list
= (GtkList
*) NULL
;
309 #if wxUSE_CHECKLISTBOX
310 m_hasCheckBoxes
= FALSE
;
311 #endif // wxUSE_CHECKLISTBOX
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
)
321 m_acceptsFocus
= TRUE
;
322 m_prevSelection
= 0; // or -1 ??
323 m_blockEvent
= FALSE
;
325 if (!PreCreation( parent
, pos
, size
) ||
326 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
328 wxFAIL_MSG( wxT("wxListBox creation failed") );
332 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
333 if (style
& wxLB_ALWAYS_SB
)
335 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
336 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
340 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
341 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
344 m_list
= GTK_LIST( gtk_list_new() );
346 GtkSelectionMode mode
;
347 if (style
& wxLB_MULTIPLE
)
349 mode
= GTK_SELECTION_MULTIPLE
;
351 else if (style
& wxLB_EXTENDED
)
353 mode
= GTK_SELECTION_EXTENDED
;
357 // if style was 0 set single mode
358 m_windowStyle
|= wxLB_SINGLE
;
359 mode
= GTK_SELECTION_MULTIPLE
;
362 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
364 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
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
)));
372 gtk_widget_show( GTK_WIDGET(m_list
) );
374 if ( style
& wxLB_SORT
)
376 // this will change DoAppend() behaviour
377 m_strings
= new wxSortedArrayString
;
381 m_strings
= (wxSortedArrayString
*)NULL
;
384 for (int i
= 0; i
< n
; i
++)
387 DoAppend(choices
[i
]);
390 // call it after appending the strings to the listbox, otherwise it doesn't
394 m_parent
->DoAddChild( this );
398 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
399 SetForegroundColour( parent
->GetForegroundColour() );
400 SetFont( parent
->GetFont() );
407 wxListBox::~wxListBox()
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
423 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
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
429 // code elsewhere supposes we have as many items in m_clientList as items
431 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
432 wxT("bug in client data management") );
434 GList
*children
= m_list
->children
;
435 int length
= g_list_length(children
);
437 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
439 size_t nItems
= items
.GetCount();
444 for (size_t n
= 0; n
< nItems
; n
++)
446 index
= m_strings
->Add( items
[n
] );
448 if (index
!= GetCount())
450 GtkAddItem( items
[n
], index
);
451 wxNode
*node
= m_clientList
.Item( index
);
452 m_clientList
.Insert( node
, (wxObject
*) NULL
);
456 GtkAddItem( items
[n
] );
457 m_clientList
.Append( (wxObject
*) NULL
);
465 for ( size_t n
= 0; n
< nItems
; n
++ )
467 GtkAddItem( items
[n
] );
469 m_clientList
.Append((wxObject
*)NULL
);
474 wxNode
*node
= m_clientList
.Item( pos
);
475 for ( size_t n
= 0; n
< nItems
; n
++ )
477 GtkAddItem( items
[n
], pos
+n
);
479 m_clientList
.Insert( node
, (wxObject
*)NULL
);
484 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
485 wxT("bug in client data management") );
488 int wxListBox::DoAppend( const wxString
& item
)
492 // need to determine the index
493 int index
= m_strings
->Add( item
);
495 // only if not at the end anyway
496 if (index
!= GetCount())
498 GtkAddItem( item
, index
);
500 wxNode
*node
= m_clientList
.Item( index
);
501 m_clientList
.Insert( node
, (wxObject
*)NULL
);
509 m_clientList
.Append((wxObject
*)NULL
);
511 return GetCount() - 1;
514 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
516 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
518 GtkWidget
*list_item
;
520 wxString
label(item
);
521 #if wxUSE_CHECKLISTBOX
524 label
.Prepend(wxCHECKLBOX_STRING
);
526 #endif // wxUSE_CHECKLISTBOX
528 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
530 GList
*gitem_list
= g_list_alloc ();
531 gitem_list
->data
= list_item
;
534 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
536 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
538 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
539 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
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 );
545 gtk_signal_connect( GTK_OBJECT(list_item
),
546 "button_press_event",
547 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
550 gtk_signal_connect_after( GTK_OBJECT(list_item
),
551 "button_release_event",
552 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
555 gtk_signal_connect( GTK_OBJECT(list_item
),
557 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
560 ConnectWidget( list_item
);
562 gtk_widget_show( list_item
);
564 if (GTK_WIDGET_REALIZED(m_widget
))
566 gtk_widget_realize( list_item
);
567 gtk_widget_realize( GTK_BIN(list_item
)->child
);
569 // Apply current widget style to the new list_item
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
);
579 if (m_tooltip
) m_tooltip
->Apply( this );
584 void wxListBox::DoSetItems( const wxArrayString
& items
,
589 DoInsertItems(items
, 0);
593 size_t count
= items
.GetCount();
594 for ( size_t n
= 0; n
< count
; n
++ )
596 SetClientData(n
, clientData
[n
]);
601 // ----------------------------------------------------------------------------
603 // ----------------------------------------------------------------------------
605 void wxListBox::Clear()
607 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
609 gtk_list_clear_items( m_list
, 0, GetCount() );
611 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
613 // This should be NULL, I think.
614 GTK_LIST(m_list
)->last_focus_child
= NULL
;
617 if ( HasClientObjectData() )
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();
625 delete (wxClientData
*)node
->GetData();
626 node
= node
->GetNext();
629 m_clientList
.Clear();
635 void wxListBox::Delete( int n
)
637 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
639 GList
*child
= g_list_nth( m_list
->children
, n
);
641 wxCHECK_RET( child
, wxT("wrong listbox index") );
643 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
644 gtk_list_remove_items( m_list
, list
);
647 wxNode
*node
= m_clientList
.Item( n
);
650 if ( m_clientDataItemsType
== wxClientData_Object
)
652 wxClientData
*cd
= (wxClientData
*)node
->GetData();
656 m_clientList
.DeleteNode( node
);
660 m_strings
->Remove(n
);
663 // ----------------------------------------------------------------------------
665 // ----------------------------------------------------------------------------
667 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
669 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
671 wxNode
*node
= m_clientList
.Item( n
);
672 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
674 node
->SetData( (wxObject
*) clientData
);
677 void* wxListBox::DoGetItemClientData( int n
) const
679 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
681 wxNode
*node
= m_clientList
.Item( n
);
682 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
684 return node
->GetData();
687 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
689 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
691 wxNode
*node
= m_clientList
.Item( n
);
692 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
694 // wxItemContainer already deletes data for us
696 node
->SetData( (wxObject
*) clientData
);
699 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
701 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
703 wxNode
*node
= m_clientList
.Item( n
);
704 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
705 wxT("invalid index in wxListBox::DoGetItemClientObject") );
707 return (wxClientData
*) node
->GetData();
710 // ----------------------------------------------------------------------------
711 // string list access
712 // ----------------------------------------------------------------------------
714 wxString
wxListBox::GetRealLabel(GList
*item
) const
716 GtkBin
*bin
= GTK_BIN( item
->data
);
717 GtkLabel
*label
= GTK_LABEL( bin
->child
);
722 str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
724 str
= wxString( label
->label
);
727 #if wxUSE_CHECKLISTBOX
728 // checklistboxes have "[±] " prepended to their lables, remove it
730 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
731 if ( m_hasCheckBoxes
)
733 #endif // wxUSE_CHECKLISTBOX
738 void wxListBox::SetString( int n
, const wxString
&string
)
740 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
742 GList
*child
= g_list_nth( m_list
->children
, n
);
745 GtkBin
*bin
= GTK_BIN( child
->data
);
746 GtkLabel
*label
= GTK_LABEL( bin
->child
);
749 #if wxUSE_CHECKLISTBOX
751 str
+= wxCHECKLBOX_STRING
;
752 #endif // wxUSE_CHECKLISTBOX
755 gtk_label_set( label
, wxGTK_CONV( str
) );
759 wxFAIL_MSG(wxT("wrong listbox index"));
763 wxString
wxListBox::GetString( int n
) const
765 wxCHECK_MSG( m_list
!= NULL
, wxT(""), wxT("invalid listbox") );
767 GList
*child
= g_list_nth( m_list
->children
, n
);
770 return GetRealLabel(child
);
773 wxFAIL_MSG(wxT("wrong listbox index"));
778 int wxListBox::GetCount() const
780 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
782 GList
*children
= m_list
->children
;
783 return g_list_length(children
);
786 int wxListBox::FindString( const wxString
&item
) const
788 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
790 GList
*child
= m_list
->children
;
794 if ( GetRealLabel(child
) == item
)
801 // it's not an error if the string is not found -> no wxCHECK
806 // ----------------------------------------------------------------------------
808 // ----------------------------------------------------------------------------
810 int wxListBox::GetSelection() const
812 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
814 GList
*child
= m_list
->children
;
818 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
825 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
827 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
829 // get the number of selected items first
830 GList
*child
= m_list
->children
;
832 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
834 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
843 aSelections
.Alloc(count
); // optimization attempt
845 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
847 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
855 bool wxListBox::IsSelected( int n
) const
857 wxCHECK_MSG( m_list
!= NULL
, FALSE
, wxT("invalid listbox") );
859 GList
*target
= g_list_nth( m_list
->children
, n
);
861 wxCHECK_MSG( target
, FALSE
, wxT("invalid listbox index") );
863 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
866 void wxListBox::SetSelection( int n
, bool select
)
868 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
874 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
875 gtk_list_unselect_item( m_list
, m_prevSelection
);
876 gtk_list_select_item( m_list
, n
);
880 gtk_list_unselect_item( m_list
, n
);
882 m_blockEvent
= FALSE
;
885 void wxListBox::DoSetFirstItem( int n
)
887 wxCHECK_RET( m_list
, wxT("invalid listbox") );
889 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
892 // terribly efficient
893 const gchar
*vadjustment_key
= "gtk-vadjustment";
894 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
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") );
900 GList
*target
= g_list_nth( m_list
->children
, n
);
901 wxCHECK_RET( target
, wxT("invalid listbox index") );
903 GtkWidget
*item
= GTK_WIDGET(target
->data
);
904 wxCHECK_RET( item
, wxT("invalid listbox code") );
906 if (item
->allocation
.y
== -1)
908 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
909 data
->m_listbox
= this;
911 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
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
);
922 // ----------------------------------------------------------------------------
924 // ----------------------------------------------------------------------------
926 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
930 GList
*child
= m_list
->children
;
934 if (GTK_WIDGET(child
->data
) == item
) return count
;
943 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
945 GList
*child
= m_list
->children
;
948 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
952 #endif // wxUSE_TOOLTIPS
954 GtkWidget
*wxListBox::GetConnectWidget()
956 return GTK_WIDGET(m_list
);
959 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
961 if (GTK_WIDGET(m_list
)->window
== window
) return TRUE
;
963 GList
*child
= m_list
->children
;
966 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
967 if (bin
->window
== window
) return TRUE
;
974 void wxListBox::ApplyWidgetStyle()
978 if (m_backgroundColour
.Ok())
980 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
983 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
984 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
985 gdk_window_clear( window
);
989 GList
*child
= m_list
->children
;
992 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
994 GtkBin
*bin
= GTK_BIN( child
->data
);
995 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
996 gtk_widget_set_style( label
, m_widgetStyle
);
1002 void wxListBox::OnInternalIdle()
1004 wxCursor cursor
= m_cursor
;
1005 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1007 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1009 /* I now set the cursor the anew in every OnInternalIdle call
1010 as setting the cursor in a parent window also effects the
1011 windows above so that checking for the current cursor is
1014 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1016 GList
*child
= m_list
->children
;
1019 GtkBin
*bin
= GTK_BIN( child
->data
);
1020 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1025 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1027 child
= child
->next
;
1031 if (g_delayedFocus
== this)
1033 if (GTK_WIDGET_REALIZED(m_widget
))
1035 gtk_widget_grab_focus( m_widget
);
1036 g_delayedFocus
= NULL
;
1043 wxSize
wxListBox::DoGetBestSize() const
1045 int lbWidth
= 100; // some defaults
1049 // Find the widest line
1050 for(int i
= 0; i
< GetCount(); i
++) {
1051 wxString
str(GetString(i
));
1052 GetTextExtent(str
, &wLine
, NULL
);
1053 lbWidth
= wxMax(lbWidth
, wLine
);
1056 // Add room for the scrollbar
1057 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1059 // And just a bit more
1061 GetTextExtent( wxT("X"), &cx
, &cy
);
1064 // don't make the listbox too tall (limit height to around 10 items) but don't
1065 // make it too small neither
1066 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1068 return wxSize(lbWidth
, lbHeight
);
1071 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1073 // the mouse event coords are relative to the listbox items, we need to
1074 // translate them to the normal client coords
1075 x
+= widget
->allocation
.x
;
1076 y
+= widget
->allocation
.y
;
1079 #endif // wxUSE_LISTBOX