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 #if wxUSE_CHECKLISTBOX
47 // checklistboxes have "[±] " prepended to their lables, this macro removes it
48 // (NB: 4 below is the length of wxCHECKLBOX_STRING above)
50 // the argument to it is a "const char *" pointer
51 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
53 #else // !wxUSE_CHECKLISTBOX
55 #define GET_REAL_LABEL(label) (label)
57 #endif // wxUSE_CHECKLISTBOX
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 extern bool g_blockEventsOnDrag
;
64 extern bool g_blockEventsOnScroll
;
65 extern wxCursor g_globalCursor
;
66 extern wxWindowGTK
*g_delayedFocus
;
68 static bool g_hasDoubleClicked
= FALSE
;
70 //-----------------------------------------------------------------------------
71 // idle callback for SetFirstItem
72 //-----------------------------------------------------------------------------
74 struct wxlistbox_idle_struct
81 extern "C" gint
wxlistbox_idle_callback( gpointer gdata
)
83 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
86 gtk_idle_remove( data
->m_tag
);
88 // check that the items haven't been deleted from the listbox since we had
89 // installed this callback
90 wxListBox
*lbox
= data
->m_listbox
;
91 if ( data
->m_item
< lbox
->GetCount() )
93 lbox
->SetFirstItem( data
->m_item
);
103 //-----------------------------------------------------------------------------
104 // "button_release_event"
105 //-----------------------------------------------------------------------------
107 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
108 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
109 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
110 this can lead to race conditions so that we emit the dclick event
111 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
114 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
115 GdkEventButton
* WXUNUSED(gdk_event
),
118 if (g_isIdle
) wxapp_install_idle_handler();
120 if (g_blockEventsOnDrag
) return FALSE
;
121 if (g_blockEventsOnScroll
) return FALSE
;
123 if (!listbox
->m_hasVMT
) return FALSE
;
125 if (!g_hasDoubleClicked
) return FALSE
;
127 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
128 event
.SetEventObject( listbox
);
130 wxArrayInt aSelections
;
131 int n
, count
= listbox
->GetSelections(aSelections
);
135 if ( listbox
->HasClientObjectData() )
136 event
.SetClientObject( listbox
->GetClientObject(n
) );
137 else if ( listbox
->HasClientUntypedData() )
138 event
.SetClientData( listbox
->GetClientData(n
) );
139 event
.SetString( listbox
->GetString(n
) );
146 event
.m_commandInt
= n
;
148 listbox
->GetEventHandler()->ProcessEvent( event
);
153 //-----------------------------------------------------------------------------
154 // "button_press_event"
155 //-----------------------------------------------------------------------------
158 gtk_listbox_button_press_callback( GtkWidget
*widget
,
159 GdkEventButton
*gdk_event
,
162 if (g_isIdle
) wxapp_install_idle_handler();
164 if (g_blockEventsOnDrag
) return FALSE
;
165 if (g_blockEventsOnScroll
) return FALSE
;
167 if (!listbox
->m_hasVMT
) return FALSE
;
169 int sel
= listbox
->GtkGetIndex( widget
);
171 #if wxUSE_CHECKLISTBOX
172 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
174 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
176 clb
->Check( sel
, !clb
->IsChecked(sel
) );
178 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
179 event
.SetEventObject( listbox
);
181 listbox
->GetEventHandler()->ProcessEvent( event
);
183 #endif // wxUSE_CHECKLISTBOX
185 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
186 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
191 //-----------------------------------------------------------------------------
193 //-----------------------------------------------------------------------------
196 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
199 wxapp_install_idle_handler();
201 if (g_blockEventsOnDrag
)
206 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
208 wxNavigationKeyEvent new_event
;
209 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
210 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
211 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
212 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
213 new_event
.SetCurrentFocus( listbox
);
214 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
217 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
219 // eat return in all modes
223 #if wxUSE_CHECKLISTBOX
224 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
226 int sel
= listbox
->GtkGetIndex( widget
);
228 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
230 clb
->Check( sel
, !clb
->IsChecked(sel
) );
232 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
233 new_event
.SetEventObject( listbox
);
234 new_event
.SetInt( sel
);
235 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
237 #endif // wxUSE_CHECKLISTBOX
241 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
248 //-----------------------------------------------------------------------------
249 // "select" and "deselect"
250 //-----------------------------------------------------------------------------
252 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
);
254 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
256 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
259 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
261 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
264 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
)
266 if (g_isIdle
) wxapp_install_idle_handler();
268 if (!listbox
->m_hasVMT
) return;
269 if (g_blockEventsOnDrag
) return;
271 if (listbox
->m_blockEvent
) return;
273 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
274 event
.SetEventObject( listbox
);
276 // MSW doesn't do that either
277 // event.SetExtraLong( (long) is_selection );
280 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
282 int sel
= listbox
->GtkGetIndex( widget
);
284 if (listbox
->m_prevSelection
!= sel
)
285 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
287 listbox
->m_prevSelection
= sel
;
290 wxArrayInt aSelections
;
291 int n
, count
= listbox
->GetSelections(aSelections
);
295 if ( listbox
->HasClientObjectData() )
296 event
.SetClientObject( listbox
->GetClientObject(n
) );
297 else if ( listbox
->HasClientUntypedData() )
298 event
.SetClientData( listbox
->GetClientData(n
) );
299 event
.SetString( listbox
->GetString(n
) );
306 event
.m_commandInt
= n
;
308 // No longer required with new code in wxLB_SINGLE
309 // listbox->GetEventHandler()->AddPendingEvent( event );
310 listbox
->GetEventHandler()->ProcessEvent( event
);
313 //-----------------------------------------------------------------------------
315 //-----------------------------------------------------------------------------
317 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
319 // ----------------------------------------------------------------------------
321 // ----------------------------------------------------------------------------
323 wxListBox::wxListBox()
325 m_list
= (GtkList
*) NULL
;
326 #if wxUSE_CHECKLISTBOX
327 m_hasCheckBoxes
= FALSE
;
328 #endif // wxUSE_CHECKLISTBOX
331 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
332 const wxPoint
&pos
, const wxSize
&size
,
333 int n
, const wxString choices
[],
334 long style
, const wxValidator
& validator
,
335 const wxString
&name
)
338 m_acceptsFocus
= TRUE
;
339 m_prevSelection
= 0; // or -1 ??
340 m_blockEvent
= FALSE
;
342 if (!PreCreation( parent
, pos
, size
) ||
343 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
345 wxFAIL_MSG( wxT("wxListBox creation failed") );
349 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
350 if (style
& wxLB_ALWAYS_SB
)
352 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
353 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
357 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
358 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
361 m_list
= GTK_LIST( gtk_list_new() );
363 GtkSelectionMode mode
;
364 if (style
& wxLB_MULTIPLE
)
366 mode
= GTK_SELECTION_MULTIPLE
;
368 else if (style
& wxLB_EXTENDED
)
370 mode
= GTK_SELECTION_EXTENDED
;
374 // if style was 0 set single mode
375 m_windowStyle
|= wxLB_SINGLE
;
376 mode
= GTK_SELECTION_MULTIPLE
;
379 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
381 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
383 /* make list scroll when moving the focus down using cursor keys */
384 gtk_container_set_focus_vadjustment(
385 GTK_CONTAINER(m_list
),
386 gtk_scrolled_window_get_vadjustment(
387 GTK_SCROLLED_WINDOW(m_widget
)));
389 gtk_widget_show( GTK_WIDGET(m_list
) );
391 if ( style
& wxLB_SORT
)
393 // this will change DoAppend() behaviour
394 m_strings
= new wxSortedArrayString
;
398 m_strings
= (wxSortedArrayString
*)NULL
;
401 for (int i
= 0; i
< n
; i
++)
404 DoAppend(choices
[i
]);
407 // call it after appending the strings to the listbox, otherwise it doesn't
411 m_parent
->DoAddChild( this );
415 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
416 SetForegroundColour( parent
->GetForegroundColour() );
417 SetFont( parent
->GetFont() );
424 wxListBox::~wxListBox()
434 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
436 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
438 // VZ: notice that InsertItems knows nothing about sorting, so calling it
439 // from outside (and not from our own Append) is likely to break
442 // code elsewhere supposes we have as many items in m_clientList as items
444 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
445 wxT("bug in client data management") );
447 GList
*children
= m_list
->children
;
448 int length
= g_list_length(children
);
450 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
452 size_t nItems
= items
.GetCount();
457 for (size_t n
= 0; n
< nItems
; n
++)
459 index
= m_strings
->Add( items
[n
] );
461 if (index
!= GetCount())
463 GtkAddItem( items
[n
], index
);
464 wxNode
*node
= m_clientList
.Nth( index
);
465 m_clientList
.Insert( node
, (wxObject
*) NULL
);
469 GtkAddItem( items
[n
] );
470 m_clientList
.Append( (wxObject
*) NULL
);
478 for ( size_t n
= 0; n
< nItems
; n
++ )
480 GtkAddItem( items
[n
] );
482 m_clientList
.Append((wxObject
*)NULL
);
487 wxNode
*node
= m_clientList
.Nth( pos
);
488 for ( size_t n
= 0; n
< nItems
; n
++ )
490 GtkAddItem( items
[n
], pos
+n
);
492 m_clientList
.Insert( node
, (wxObject
*)NULL
);
497 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
498 wxT("bug in client data management") );
501 int wxListBox::DoAppend( const wxString
& item
)
505 // need to determine the index
506 int index
= m_strings
->Add( item
);
508 // only if not at the end anyway
509 if (index
!= GetCount())
511 GtkAddItem( item
, index
);
513 wxNode
*node
= m_clientList
.Nth( index
);
514 m_clientList
.Insert( node
, (wxObject
*)NULL
);
522 m_clientList
.Append((wxObject
*)NULL
);
524 return GetCount() - 1;
527 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
529 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
531 GtkWidget
*list_item
;
533 wxString
label(item
);
534 #if wxUSE_CHECKLISTBOX
537 label
.Prepend(wxCHECKLBOX_STRING
);
539 #endif // wxUSE_CHECKLISTBOX
541 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
543 GList
*gitem_list
= g_list_alloc ();
544 gitem_list
->data
= list_item
;
547 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
549 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
551 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
552 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
554 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
555 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
556 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
558 gtk_signal_connect( GTK_OBJECT(list_item
),
559 "button_press_event",
560 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
563 gtk_signal_connect_after( GTK_OBJECT(list_item
),
564 "button_release_event",
565 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
568 gtk_signal_connect( GTK_OBJECT(list_item
),
570 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
573 ConnectWidget( list_item
);
575 gtk_widget_show( list_item
);
577 if (GTK_WIDGET_REALIZED(m_widget
))
579 gtk_widget_realize( list_item
);
580 gtk_widget_realize( GTK_BIN(list_item
)->child
);
582 // Apply current widget style to the new list_item
585 gtk_widget_set_style( GTK_WIDGET( list_item
), m_widgetStyle
);
586 GtkBin
*bin
= GTK_BIN( list_item
);
587 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
588 gtk_widget_set_style( label
, m_widgetStyle
);
592 if (m_tooltip
) m_tooltip
->Apply( this );
597 void wxListBox::DoSetItems( const wxArrayString
& items
,
602 DoInsertItems(items
, 0);
606 size_t count
= items
.GetCount();
607 for ( size_t n
= 0; n
< count
; n
++ )
609 SetClientData(n
, clientData
[n
]);
614 // ----------------------------------------------------------------------------
616 // ----------------------------------------------------------------------------
618 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
620 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
622 wxNode
*node
= m_clientList
.Nth( n
);
623 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
625 node
->SetData( (wxObject
*) clientData
);
628 void* wxListBox::DoGetItemClientData( int n
) const
630 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
632 wxNode
*node
= m_clientList
.Nth( n
);
633 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
638 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
640 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
642 wxNode
*node
= m_clientList
.Nth( n
);
643 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
645 wxClientData
*cd
= (wxClientData
*) node
->Data();
648 node
->SetData( (wxObject
*) clientData
);
651 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
653 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
655 wxNode
*node
= m_clientList
.Nth( n
);
656 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
657 wxT("invalid index in wxListBox::DoGetItemClientObject") );
659 return (wxClientData
*) node
->Data();
662 void wxListBox::Clear()
664 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
666 gtk_list_clear_items( m_list
, 0, GetCount() );
668 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
670 // This should be NULL, I think.
671 GTK_LIST(m_list
)->last_focus_child
= NULL
;
674 if ( HasClientObjectData() )
676 // destroy the data (due to Robert's idea of using wxList<wxObject>
677 // and not wxList<wxClientData> we can't just say
678 // m_clientList.DeleteContents(TRUE) - this would crash!
679 wxNode
*node
= m_clientList
.First();
682 delete (wxClientData
*)node
->Data();
686 m_clientList
.Clear();
692 void wxListBox::Delete( int n
)
694 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
696 GList
*child
= g_list_nth( m_list
->children
, n
);
698 wxCHECK_RET( child
, wxT("wrong listbox index") );
700 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
701 gtk_list_remove_items( m_list
, list
);
704 wxNode
*node
= m_clientList
.Nth( n
);
707 if ( m_clientDataItemsType
== wxClientData_Object
)
709 wxClientData
*cd
= (wxClientData
*)node
->Data();
713 m_clientList
.DeleteNode( node
);
717 m_strings
->Remove(n
);
720 // ----------------------------------------------------------------------------
721 // string list access
722 // ----------------------------------------------------------------------------
724 void wxListBox::SetString( int n
, const wxString
&string
)
726 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
728 GList
*child
= g_list_nth( m_list
->children
, n
);
731 GtkBin
*bin
= GTK_BIN( child
->data
);
732 GtkLabel
*label
= GTK_LABEL( bin
->child
);
735 #if wxUSE_CHECKLISTBOX
737 str
+= wxCHECKLBOX_STRING
;
738 #endif // wxUSE_CHECKLISTBOX
741 gtk_label_set( label
, wxGTK_CONV( str
) );
745 wxFAIL_MSG(wxT("wrong listbox index"));
749 wxString
wxListBox::GetString( int n
) const
751 wxCHECK_MSG( m_list
!= NULL
, wxT(""), wxT("invalid listbox") );
753 GList
*child
= g_list_nth( m_list
->children
, n
);
756 GtkBin
*bin
= GTK_BIN( child
->data
);
757 GtkLabel
*label
= GTK_LABEL( bin
->child
);
760 wxString str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
762 wxString str
= wxString( label
->label
);
768 wxFAIL_MSG(wxT("wrong listbox index"));
773 int wxListBox::GetCount() const
775 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
777 GList
*children
= m_list
->children
;
778 return g_list_length(children
);
781 int wxListBox::FindString( const wxString
&item
) const
783 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
785 GList
*child
= m_list
->children
;
789 GtkBin
*bin
= GTK_BIN( child
->data
);
790 GtkLabel
*label
= GTK_LABEL( bin
->child
);
793 wxString str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
795 wxString str
= wxString( label
->label
);
805 // it's not an error if the string is not found -> no wxCHECK
810 // ----------------------------------------------------------------------------
812 // ----------------------------------------------------------------------------
814 int wxListBox::GetSelection() const
816 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
818 GList
*child
= m_list
->children
;
822 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
829 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
831 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
833 // get the number of selected items first
834 GList
*child
= m_list
->children
;
836 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
838 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
847 aSelections
.Alloc(count
); // optimization attempt
849 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
851 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
859 bool wxListBox::IsSelected( int n
) const
861 wxCHECK_MSG( m_list
!= NULL
, FALSE
, wxT("invalid listbox") );
863 GList
*target
= g_list_nth( m_list
->children
, n
);
865 wxCHECK_MSG( target
, FALSE
, wxT("invalid listbox index") );
867 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
870 void wxListBox::SetSelection( int n
, bool select
)
872 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
878 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
879 gtk_list_unselect_item( m_list
, m_prevSelection
);
880 gtk_list_select_item( m_list
, n
);
884 gtk_list_unselect_item( m_list
, n
);
886 m_blockEvent
= FALSE
;
889 void wxListBox::DoSetFirstItem( int n
)
891 wxCHECK_RET( m_list
, wxT("invalid listbox") );
893 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
896 // terribly efficient
897 const gchar
*vadjustment_key
= "gtk-vadjustment";
898 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
900 GtkAdjustment
*adjustment
=
901 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
902 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
904 GList
*target
= g_list_nth( m_list
->children
, n
);
905 wxCHECK_RET( target
, wxT("invalid listbox index") );
907 GtkWidget
*item
= GTK_WIDGET(target
->data
);
908 wxCHECK_RET( item
, wxT("invalid listbox code") );
910 if (item
->allocation
.y
== -1)
912 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
913 data
->m_listbox
= this;
915 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
920 float y
= item
->allocation
.y
;
921 if (y
> adjustment
->upper
- adjustment
->page_size
)
922 y
= adjustment
->upper
- adjustment
->page_size
;
923 gtk_adjustment_set_value( adjustment
, y
);
926 // ----------------------------------------------------------------------------
928 // ----------------------------------------------------------------------------
930 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
934 GList
*child
= m_list
->children
;
938 if (GTK_WIDGET(child
->data
) == item
) return count
;
947 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
949 GList
*child
= m_list
->children
;
952 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
956 #endif // wxUSE_TOOLTIPS
958 GtkWidget
*wxListBox::GetConnectWidget()
960 return GTK_WIDGET(m_list
);
963 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
965 if (GTK_WIDGET(m_list
)->window
== window
) return TRUE
;
967 GList
*child
= m_list
->children
;
970 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
971 if (bin
->window
== window
) return TRUE
;
978 void wxListBox::ApplyWidgetStyle()
982 if (m_backgroundColour
.Ok())
984 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
987 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
988 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
989 gdk_window_clear( window
);
993 GList
*child
= m_list
->children
;
996 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
998 GtkBin
*bin
= GTK_BIN( child
->data
);
999 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1000 gtk_widget_set_style( label
, m_widgetStyle
);
1002 child
= child
->next
;
1006 void wxListBox::OnInternalIdle()
1008 wxCursor cursor
= m_cursor
;
1009 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1011 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1013 /* I now set the cursor the anew in every OnInternalIdle call
1014 as setting the cursor in a parent window also effects the
1015 windows above so that checking for the current cursor is
1018 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1020 GList
*child
= m_list
->children
;
1023 GtkBin
*bin
= GTK_BIN( child
->data
);
1024 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1029 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1031 child
= child
->next
;
1035 if (g_delayedFocus
== this)
1037 if (GTK_WIDGET_REALIZED(m_widget
))
1039 gtk_widget_grab_focus( m_widget
);
1040 g_delayedFocus
= NULL
;
1047 wxSize
wxListBox::DoGetBestSize() const
1049 int lbWidth
= 100; // some defaults
1053 // Find the widest line
1054 for(int i
= 0; i
< GetCount(); i
++) {
1055 wxString
str(GetString(i
));
1056 GetTextExtent(str
, &wLine
, NULL
);
1057 lbWidth
= wxMax(lbWidth
, wLine
);
1060 // Add room for the scrollbar
1061 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1063 // And just a bit more
1065 GetTextExtent("X", &cx
, &cy
);
1068 // don't make the listbox too tall (limit height to around 10 items) but don't
1069 // make it too small neither
1070 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1072 return wxSize(lbWidth
, lbHeight
);
1075 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1077 // the mouse event coords are relative to the listbox items, we need to
1078 // translate them to the normal client coords
1079 x
+= widget
->allocation
.x
;
1080 y
+= widget
->allocation
.y
;
1083 #endif // wxUSE_LISTBOX