1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "listbox.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/listbox.h"
23 #include "wx/dynarray.h"
24 #include "wx/arrstr.h"
27 #include "wx/checklst.h"
28 #include "wx/settings.h"
29 #include "wx/gtk/private.h"
32 #include "wx/tooltip.h"
37 #include <gdk/gdkkeysyms.h>
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern void wxapp_install_idle_handler();
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 extern bool g_blockEventsOnDrag
;
51 extern bool g_blockEventsOnScroll
;
52 extern wxCursor g_globalCursor
;
53 extern wxWindowGTK
*g_delayedFocus
;
55 static bool g_hasDoubleClicked
= FALSE
;
57 //-----------------------------------------------------------------------------
58 // idle callback for SetFirstItem
59 //-----------------------------------------------------------------------------
61 struct wxlistbox_idle_struct
68 extern "C" gint
wxlistbox_idle_callback( gpointer gdata
)
70 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
73 gtk_idle_remove( data
->m_tag
);
75 // check that the items haven't been deleted from the listbox since we had
76 // installed this callback
77 wxListBox
*lbox
= data
->m_listbox
;
78 if ( data
->m_item
< lbox
->GetCount() )
80 lbox
->SetFirstItem( data
->m_item
);
90 //-----------------------------------------------------------------------------
91 // "button_release_event"
92 //-----------------------------------------------------------------------------
94 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
95 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
96 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
97 this can lead to race conditions so that we emit the dclick event
98 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
101 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
102 GdkEventButton
* WXUNUSED(gdk_event
),
105 if (g_isIdle
) wxapp_install_idle_handler();
107 if (g_blockEventsOnDrag
) return FALSE
;
108 if (g_blockEventsOnScroll
) return FALSE
;
110 if (!listbox
->m_hasVMT
) return FALSE
;
112 if (!g_hasDoubleClicked
) return FALSE
;
114 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
115 event
.SetEventObject( listbox
);
117 wxArrayInt aSelections
;
118 int n
, count
= listbox
->GetSelections(aSelections
);
122 if ( listbox
->HasClientObjectData() )
123 event
.SetClientObject( listbox
->GetClientObject(n
) );
124 else if ( listbox
->HasClientUntypedData() )
125 event
.SetClientData( listbox
->GetClientData(n
) );
126 event
.SetString( listbox
->GetString(n
) );
133 event
.m_commandInt
= n
;
135 listbox
->GetEventHandler()->ProcessEvent( event
);
140 //-----------------------------------------------------------------------------
141 // "button_press_event"
142 //-----------------------------------------------------------------------------
145 gtk_listbox_button_press_callback( GtkWidget
*widget
,
146 GdkEventButton
*gdk_event
,
149 if (g_isIdle
) wxapp_install_idle_handler();
151 if (g_blockEventsOnDrag
) return FALSE
;
152 if (g_blockEventsOnScroll
) return FALSE
;
154 if (!listbox
->m_hasVMT
) return FALSE
;
156 int sel
= listbox
->GtkGetIndex( widget
);
158 #if wxUSE_CHECKLISTBOX
159 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
161 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
163 clb
->Check( sel
, !clb
->IsChecked(sel
) );
165 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
166 event
.SetEventObject( listbox
);
168 listbox
->GetEventHandler()->ProcessEvent( event
);
170 #endif // wxUSE_CHECKLISTBOX
172 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
173 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
178 //-----------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
183 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
186 wxapp_install_idle_handler();
188 if (g_blockEventsOnDrag
)
193 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
195 wxNavigationKeyEvent new_event
;
196 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
197 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
198 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
199 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
200 new_event
.SetCurrentFocus( listbox
);
201 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
204 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
206 // eat return in all modes
210 #if wxUSE_CHECKLISTBOX
211 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
213 int sel
= listbox
->GtkGetIndex( widget
);
215 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
217 clb
->Check( sel
, !clb
->IsChecked(sel
) );
219 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
220 new_event
.SetEventObject( listbox
);
221 new_event
.SetInt( sel
);
222 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
224 #endif // wxUSE_CHECKLISTBOX
228 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
235 //-----------------------------------------------------------------------------
236 // "select" and "deselect"
237 //-----------------------------------------------------------------------------
239 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
);
241 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
243 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
246 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
248 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
251 static void gtk_listitem_select_cb( GtkWidget
*widget
,
255 if (g_isIdle
) wxapp_install_idle_handler();
257 if (!listbox
->m_hasVMT
) return;
258 if (g_blockEventsOnDrag
) return;
260 if (listbox
->m_blockEvent
) return;
262 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
263 event
.SetEventObject( listbox
);
265 // indicate whether this is a selection or a deselection
266 event
.SetExtraLong( is_selection
);
268 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
270 int sel
= listbox
->GtkGetIndex( widget
);
272 if (listbox
->m_prevSelection
!= sel
)
273 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
275 listbox
->m_prevSelection
= sel
;
278 wxArrayInt aSelections
;
279 int n
, count
= listbox
->GetSelections(aSelections
);
283 if ( listbox
->HasClientObjectData() )
284 event
.SetClientObject( listbox
->GetClientObject(n
) );
285 else if ( listbox
->HasClientUntypedData() )
286 event
.SetClientData( listbox
->GetClientData(n
) );
287 event
.SetString( listbox
->GetString(n
) );
294 event
.m_commandInt
= n
;
296 // No longer required with new code in wxLB_SINGLE
297 // listbox->GetEventHandler()->AddPendingEvent( event );
298 listbox
->GetEventHandler()->ProcessEvent( event
);
301 //-----------------------------------------------------------------------------
303 //-----------------------------------------------------------------------------
305 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 wxListBox::wxListBox()
313 m_list
= (GtkList
*) NULL
;
314 #if wxUSE_CHECKLISTBOX
315 m_hasCheckBoxes
= FALSE
;
316 #endif // wxUSE_CHECKLISTBOX
319 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
320 const wxPoint
&pos
, const wxSize
&size
,
321 int n
, const wxString choices
[],
322 long style
, const wxValidator
& validator
,
323 const wxString
&name
)
326 m_acceptsFocus
= TRUE
;
327 m_prevSelection
= 0; // or -1 ??
328 m_blockEvent
= FALSE
;
330 if (!PreCreation( parent
, pos
, size
) ||
331 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
333 wxFAIL_MSG( wxT("wxListBox creation failed") );
337 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
338 if (style
& wxLB_ALWAYS_SB
)
340 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
341 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
345 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
346 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
349 m_list
= GTK_LIST( gtk_list_new() );
351 GtkSelectionMode mode
;
352 if (style
& wxLB_MULTIPLE
)
354 mode
= GTK_SELECTION_MULTIPLE
;
356 else if (style
& wxLB_EXTENDED
)
358 mode
= GTK_SELECTION_EXTENDED
;
362 // if style was 0 set single mode
363 m_windowStyle
|= wxLB_SINGLE
;
364 mode
= GTK_SELECTION_MULTIPLE
;
367 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
369 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
371 /* make list scroll when moving the focus down using cursor keys */
372 gtk_container_set_focus_vadjustment(
373 GTK_CONTAINER(m_list
),
374 gtk_scrolled_window_get_vadjustment(
375 GTK_SCROLLED_WINDOW(m_widget
)));
377 gtk_widget_show( GTK_WIDGET(m_list
) );
379 if ( style
& wxLB_SORT
)
381 // this will change DoAppend() behaviour
382 m_strings
= new wxSortedArrayString
;
386 m_strings
= (wxSortedArrayString
*)NULL
;
389 for (int i
= 0; i
< n
; i
++)
392 DoAppend(choices
[i
]);
395 // call it after appending the strings to the listbox, otherwise it doesn't
399 m_parent
->DoAddChild( this );
403 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
404 SetForegroundColour( parent
->GetForegroundColour() );
405 SetFont( parent
->GetFont() );
412 wxListBox::~wxListBox()
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
428 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
430 // VZ: notice that InsertItems knows nothing about sorting, so calling it
431 // from outside (and not from our own Append) is likely to break
434 // code elsewhere supposes we have as many items in m_clientList as items
436 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
437 wxT("bug in client data management") );
439 GList
*children
= m_list
->children
;
440 int length
= g_list_length(children
);
442 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
444 size_t nItems
= items
.GetCount();
449 for (size_t n
= 0; n
< nItems
; n
++)
451 index
= m_strings
->Add( items
[n
] );
453 if (index
!= GetCount())
455 GtkAddItem( items
[n
], index
);
456 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
457 m_clientList
.Insert( node
, (wxObject
*) NULL
);
461 GtkAddItem( items
[n
] );
462 m_clientList
.Append( (wxObject
*) NULL
);
470 for ( size_t n
= 0; n
< nItems
; n
++ )
472 GtkAddItem( items
[n
] );
474 m_clientList
.Append((wxObject
*)NULL
);
479 wxList::compatibility_iterator node
= m_clientList
.Item( pos
);
480 for ( size_t n
= 0; n
< nItems
; n
++ )
482 GtkAddItem( items
[n
], pos
+n
);
484 m_clientList
.Insert( node
, (wxObject
*)NULL
);
489 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
490 wxT("bug in client data management") );
493 int wxListBox::DoAppend( const wxString
& item
)
497 // need to determine the index
498 int index
= m_strings
->Add( item
);
500 // only if not at the end anyway
501 if (index
!= GetCount())
503 GtkAddItem( item
, index
);
505 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
506 m_clientList
.Insert( node
, (wxObject
*)NULL
);
514 m_clientList
.Append((wxObject
*)NULL
);
516 return GetCount() - 1;
519 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
521 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
523 GtkWidget
*list_item
;
525 wxString
label(item
);
526 #if wxUSE_CHECKLISTBOX
529 label
.Prepend(wxCHECKLBOX_STRING
);
531 #endif // wxUSE_CHECKLISTBOX
533 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
535 GList
*gitem_list
= g_list_alloc ();
536 gitem_list
->data
= list_item
;
539 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
541 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
543 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
544 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
546 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
547 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
548 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
550 gtk_signal_connect( GTK_OBJECT(list_item
),
551 "button_press_event",
552 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
555 gtk_signal_connect_after( GTK_OBJECT(list_item
),
556 "button_release_event",
557 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
560 gtk_signal_connect( GTK_OBJECT(list_item
),
562 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
565 ConnectWidget( list_item
);
567 gtk_widget_show( list_item
);
569 if (GTK_WIDGET_REALIZED(m_widget
))
571 gtk_widget_realize( list_item
);
572 gtk_widget_realize( GTK_BIN(list_item
)->child
);
574 // Apply current widget style to the new list_item
577 gtk_widget_set_style( GTK_WIDGET( list_item
), m_widgetStyle
);
578 GtkBin
*bin
= GTK_BIN( list_item
);
579 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
580 gtk_widget_set_style( label
, m_widgetStyle
);
584 if (m_tooltip
) m_tooltip
->Apply( this );
589 void wxListBox::DoSetItems( const wxArrayString
& items
,
594 DoInsertItems(items
, 0);
598 size_t count
= items
.GetCount();
599 for ( size_t n
= 0; n
< count
; n
++ )
601 SetClientData(n
, clientData
[n
]);
606 // ----------------------------------------------------------------------------
608 // ----------------------------------------------------------------------------
610 void wxListBox::Clear()
612 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
614 gtk_list_clear_items( m_list
, 0, GetCount() );
616 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
618 // This should be NULL, I think.
619 GTK_LIST(m_list
)->last_focus_child
= NULL
;
622 if ( HasClientObjectData() )
624 // destroy the data (due to Robert's idea of using wxList<wxObject>
625 // and not wxList<wxClientData> we can't just say
626 // m_clientList.DeleteContents(TRUE) - this would crash!
627 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
630 delete (wxClientData
*)node
->GetData();
631 node
= node
->GetNext();
634 m_clientList
.Clear();
640 void wxListBox::Delete( int n
)
642 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
644 GList
*child
= g_list_nth( m_list
->children
, n
);
646 wxCHECK_RET( child
, wxT("wrong listbox index") );
648 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
649 gtk_list_remove_items( m_list
, list
);
652 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
655 if ( m_clientDataItemsType
== wxClientData_Object
)
657 wxClientData
*cd
= (wxClientData
*)node
->GetData();
661 m_clientList
.Erase( node
);
665 m_strings
->RemoveAt(n
);
668 // ----------------------------------------------------------------------------
670 // ----------------------------------------------------------------------------
672 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
674 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
676 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
677 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
679 node
->SetData( (wxObject
*) clientData
);
682 void* wxListBox::DoGetItemClientData( int n
) const
684 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
686 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
687 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
689 return node
->GetData();
692 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
694 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
696 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
697 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
699 // wxItemContainer already deletes data for us
701 node
->SetData( (wxObject
*) clientData
);
704 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
706 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
708 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
709 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
710 wxT("invalid index in wxListBox::DoGetItemClientObject") );
712 return (wxClientData
*) node
->GetData();
715 // ----------------------------------------------------------------------------
716 // string list access
717 // ----------------------------------------------------------------------------
719 wxString
wxListBox::GetRealLabel(GList
*item
) const
721 GtkBin
*bin
= GTK_BIN( item
->data
);
722 GtkLabel
*label
= GTK_LABEL( bin
->child
);
727 str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
729 str
= wxString( label
->label
);
732 #if wxUSE_CHECKLISTBOX
733 // checklistboxes have "[±] " prepended to their lables, remove it
735 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
736 if ( m_hasCheckBoxes
)
738 #endif // wxUSE_CHECKLISTBOX
743 void wxListBox::SetString( int n
, const wxString
&string
)
745 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
747 GList
*child
= g_list_nth( m_list
->children
, n
);
750 GtkBin
*bin
= GTK_BIN( child
->data
);
751 GtkLabel
*label
= GTK_LABEL( bin
->child
);
754 #if wxUSE_CHECKLISTBOX
756 str
+= wxCHECKLBOX_STRING
;
757 #endif // wxUSE_CHECKLISTBOX
760 gtk_label_set( label
, wxGTK_CONV( str
) );
764 wxFAIL_MSG(wxT("wrong listbox index"));
768 wxString
wxListBox::GetString( int n
) const
770 wxCHECK_MSG( m_list
!= NULL
, wxT(""), wxT("invalid listbox") );
772 GList
*child
= g_list_nth( m_list
->children
, n
);
775 return GetRealLabel(child
);
778 wxFAIL_MSG(wxT("wrong listbox index"));
783 int wxListBox::GetCount() const
785 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
787 GList
*children
= m_list
->children
;
788 return g_list_length(children
);
791 int wxListBox::FindString( const wxString
&item
) const
793 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
795 GList
*child
= m_list
->children
;
799 if ( GetRealLabel(child
) == item
)
806 // it's not an error if the string is not found -> no wxCHECK
811 // ----------------------------------------------------------------------------
813 // ----------------------------------------------------------------------------
815 int wxListBox::GetSelection() const
817 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
819 GList
*child
= m_list
->children
;
823 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
830 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
832 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
834 // get the number of selected items first
835 GList
*child
= m_list
->children
;
837 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
839 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
848 aSelections
.Alloc(count
); // optimization attempt
850 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
852 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
860 bool wxListBox::IsSelected( int n
) const
862 wxCHECK_MSG( m_list
!= NULL
, FALSE
, wxT("invalid listbox") );
864 GList
*target
= g_list_nth( m_list
->children
, n
);
866 wxCHECK_MSG( target
, FALSE
, wxT("invalid listbox index") );
868 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
871 void wxListBox::SetSelection( int n
, bool select
)
873 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
879 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
880 gtk_list_unselect_item( m_list
, m_prevSelection
);
881 gtk_list_select_item( m_list
, n
);
885 gtk_list_unselect_item( m_list
, n
);
887 m_blockEvent
= FALSE
;
890 void wxListBox::DoSetFirstItem( int n
)
892 wxCHECK_RET( m_list
, wxT("invalid listbox") );
894 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
897 // terribly efficient
898 const gchar
*vadjustment_key
= "gtk-vadjustment";
899 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
901 GtkAdjustment
*adjustment
=
902 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
903 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
905 GList
*target
= g_list_nth( m_list
->children
, n
);
906 wxCHECK_RET( target
, wxT("invalid listbox index") );
908 GtkWidget
*item
= GTK_WIDGET(target
->data
);
909 wxCHECK_RET( item
, wxT("invalid listbox code") );
911 if (item
->allocation
.y
== -1)
913 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
914 data
->m_listbox
= this;
916 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
921 float y
= item
->allocation
.y
;
922 if (y
> adjustment
->upper
- adjustment
->page_size
)
923 y
= adjustment
->upper
- adjustment
->page_size
;
924 gtk_adjustment_set_value( adjustment
, y
);
927 // ----------------------------------------------------------------------------
929 // ----------------------------------------------------------------------------
931 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
935 GList
*child
= m_list
->children
;
939 if (GTK_WIDGET(child
->data
) == item
) return count
;
948 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
950 GList
*child
= m_list
->children
;
953 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
957 #endif // wxUSE_TOOLTIPS
959 GtkWidget
*wxListBox::GetConnectWidget()
961 return GTK_WIDGET(m_list
);
964 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
966 if (m_widget
->window
== window
) return TRUE
;
968 if (GTK_WIDGET(m_list
)->window
== window
) return TRUE
;
970 GList
*child
= m_list
->children
;
973 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
974 if (bin
->window
== window
) return TRUE
;
981 void wxListBox::ApplyWidgetStyle()
985 if (m_backgroundColour
.Ok())
987 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
990 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
991 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
992 gdk_window_clear( window
);
996 GList
*child
= m_list
->children
;
999 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
1001 GtkBin
*bin
= GTK_BIN( child
->data
);
1002 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1003 gtk_widget_set_style( label
, m_widgetStyle
);
1005 child
= child
->next
;
1009 void wxListBox::OnInternalIdle()
1011 wxCursor cursor
= m_cursor
;
1012 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1014 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1016 /* I now set the cursor the anew in every OnInternalIdle call
1017 as setting the cursor in a parent window also effects the
1018 windows above so that checking for the current cursor is
1021 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1023 GList
*child
= m_list
->children
;
1026 GtkBin
*bin
= GTK_BIN( child
->data
);
1027 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1032 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1034 child
= child
->next
;
1038 if (g_delayedFocus
== this)
1040 if (GTK_WIDGET_REALIZED(m_widget
))
1042 gtk_widget_grab_focus( m_widget
);
1043 g_delayedFocus
= NULL
;
1047 if (wxUpdateUIEvent::CanUpdate(this))
1048 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1051 wxSize
wxListBox::DoGetBestSize() const
1053 int lbWidth
= 100; // some defaults
1057 // Find the widest line
1058 for(int i
= 0; i
< GetCount(); i
++) {
1059 wxString
str(GetString(i
));
1060 GetTextExtent(str
, &wLine
, NULL
);
1061 lbWidth
= wxMax(lbWidth
, wLine
);
1064 // Add room for the scrollbar
1065 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1067 // And just a bit more
1069 GetTextExtent( wxT("X"), &cx
, &cy
);
1072 // don't make the listbox too tall (limit height to around 10 items) but don't
1073 // make it too small neither
1074 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1076 return wxSize(lbWidth
, lbHeight
);
1079 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1081 // the mouse event coords are relative to the listbox items, we need to
1082 // translate them to the normal client coords
1083 x
+= widget
->allocation
.x
;
1084 y
+= widget
->allocation
.y
;
1087 #endif // wxUSE_LISTBOX