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
;
65 extern wxWindowGTK
*g_delayedFocus
;
67 static bool g_hasDoubleClicked
= FALSE
;
69 //-----------------------------------------------------------------------------
70 // idle callback for SetFirstItem
71 //-----------------------------------------------------------------------------
73 struct wxlistbox_idle_struct
80 extern "C" gint
wxlistbox_idle_callback( gpointer gdata
)
82 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
85 gtk_idle_remove( data
->m_tag
);
87 // check that the items haven't been deleted from the listbox since we had
88 // installed this callback
89 wxListBox
*lbox
= data
->m_listbox
;
90 if ( data
->m_item
< lbox
->GetCount() )
92 lbox
->SetFirstItem( data
->m_item
);
102 //-----------------------------------------------------------------------------
103 // "button_release_event"
104 //-----------------------------------------------------------------------------
106 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
107 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
108 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
109 this can lead to race conditions so that we emit the dclick event
110 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
113 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
114 GdkEventButton
* WXUNUSED(gdk_event
),
117 if (g_isIdle
) wxapp_install_idle_handler();
119 if (g_blockEventsOnDrag
) return FALSE
;
120 if (g_blockEventsOnScroll
) return FALSE
;
122 if (!listbox
->m_hasVMT
) return FALSE
;
124 if (!g_hasDoubleClicked
) return FALSE
;
126 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
127 event
.SetEventObject( listbox
);
129 wxArrayInt aSelections
;
130 int n
, count
= listbox
->GetSelections(aSelections
);
134 if ( listbox
->HasClientObjectData() )
135 event
.SetClientObject( listbox
->GetClientObject(n
) );
136 else if ( listbox
->HasClientUntypedData() )
137 event
.SetClientData( listbox
->GetClientData(n
) );
138 event
.SetString( listbox
->GetString(n
) );
145 event
.m_commandInt
= n
;
147 listbox
->GetEventHandler()->ProcessEvent( event
);
152 //-----------------------------------------------------------------------------
153 // "button_press_event"
154 //-----------------------------------------------------------------------------
157 gtk_listbox_button_press_callback( GtkWidget
*widget
,
158 GdkEventButton
*gdk_event
,
161 if (g_isIdle
) wxapp_install_idle_handler();
163 if (g_blockEventsOnDrag
) return FALSE
;
164 if (g_blockEventsOnScroll
) return FALSE
;
166 if (!listbox
->m_hasVMT
) return FALSE
;
168 int sel
= listbox
->GtkGetIndex( widget
);
170 #if wxUSE_CHECKLISTBOX
171 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
173 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
175 clb
->Check( sel
, !clb
->IsChecked(sel
) );
177 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
178 event
.SetEventObject( listbox
);
180 listbox
->GetEventHandler()->ProcessEvent( event
);
182 #endif // wxUSE_CHECKLISTBOX
184 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
185 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
190 //-----------------------------------------------------------------------------
192 //-----------------------------------------------------------------------------
195 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
198 wxapp_install_idle_handler();
200 if (g_blockEventsOnDrag
)
205 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
207 wxNavigationKeyEvent new_event
;
208 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
209 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
210 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
211 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
212 new_event
.SetCurrentFocus( listbox
);
213 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
216 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
218 // eat return in all modes
222 #if wxUSE_CHECKLISTBOX
223 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
225 int sel
= listbox
->GtkGetIndex( widget
);
227 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
229 clb
->Check( sel
, !clb
->IsChecked(sel
) );
231 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
232 new_event
.SetEventObject( listbox
);
233 new_event
.SetInt( sel
);
234 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
236 #endif // wxUSE_CHECKLISTBOX
240 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
247 //-----------------------------------------------------------------------------
248 // "select" and "deselect"
249 //-----------------------------------------------------------------------------
251 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
);
253 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
255 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
258 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
260 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
263 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
)
265 if (g_isIdle
) wxapp_install_idle_handler();
267 if (!listbox
->m_hasVMT
) return;
268 if (g_blockEventsOnDrag
) return;
270 if (listbox
->m_blockEvent
) return;
272 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
273 event
.SetEventObject( listbox
);
275 // MSW doesn't do that either
276 // event.SetExtraLong( (long) is_selection );
279 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
281 int sel
= listbox
->GtkGetIndex( widget
);
283 if (listbox
->m_prevSelection
!= sel
)
284 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
286 listbox
->m_prevSelection
= sel
;
289 wxArrayInt aSelections
;
290 int n
, count
= listbox
->GetSelections(aSelections
);
294 if ( listbox
->HasClientObjectData() )
295 event
.SetClientObject( listbox
->GetClientObject(n
) );
296 else if ( listbox
->HasClientUntypedData() )
297 event
.SetClientData( listbox
->GetClientData(n
) );
298 event
.SetString( listbox
->GetString(n
) );
305 event
.m_commandInt
= n
;
307 // No longer required with new code in wxLB_SINGLE
308 // listbox->GetEventHandler()->AddPendingEvent( event );
309 listbox
->GetEventHandler()->ProcessEvent( event
);
312 //-----------------------------------------------------------------------------
314 //-----------------------------------------------------------------------------
316 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 wxListBox::wxListBox()
324 m_list
= (GtkList
*) NULL
;
325 #if wxUSE_CHECKLISTBOX
326 m_hasCheckBoxes
= FALSE
;
327 #endif // wxUSE_CHECKLISTBOX
330 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
331 const wxPoint
&pos
, const wxSize
&size
,
332 int n
, const wxString choices
[],
333 long style
, const wxValidator
& validator
,
334 const wxString
&name
)
337 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
;
1026 if (g_delayedFocus
== this)
1028 if (GTK_WIDGET_REALIZED(m_widget
))
1030 gtk_widget_grab_focus( m_widget
);
1031 g_delayedFocus
= NULL
;
1038 wxSize
wxListBox::DoGetBestSize() const
1040 int lbWidth
= 100; // some defaults
1044 // Find the widest line
1045 for(int i
= 0; i
< GetCount(); i
++) {
1046 wxString
str(GetString(i
));
1047 GetTextExtent(str
, &wLine
, NULL
);
1048 lbWidth
= wxMax(lbWidth
, wLine
);
1051 // Add room for the scrollbar
1052 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1054 // And just a bit more
1056 GetTextExtent("X", &cx
, &cy
);
1059 // don't make the listbox too tall (limit height to around 10 items) but don't
1060 // make it too small neither
1061 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1063 return wxSize(lbWidth
, lbHeight
);
1066 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1068 // the mouse event coords are relative to the listbox items, we need to
1069 // translate them to the normal client coords
1070 x
+= widget
->allocation
.x
;
1071 y
+= widget
->allocation
.y
;
1074 #endif // wxUSE_LISTBOX