1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "listbox.h"
19 #include "wx/listbox.h"
20 #include "wx/dynarray.h"
21 #include "wx/arrstr.h"
24 #include "wx/checklst.h"
25 #include "wx/settings.h"
26 #include "wx/gtk/private.h"
29 #include "wx/tooltip.h"
34 #include <gdk/gdkkeysyms.h>
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern void wxapp_install_idle_handler();
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 extern bool g_blockEventsOnDrag
;
48 extern bool g_blockEventsOnScroll
;
49 extern wxCursor g_globalCursor
;
50 extern wxWindowGTK
*g_delayedFocus
;
52 static bool g_hasDoubleClicked
= FALSE
;
54 //-----------------------------------------------------------------------------
55 // idle callback for SetFirstItem
56 //-----------------------------------------------------------------------------
58 struct wxlistbox_idle_struct
65 extern "C" gint
wxlistbox_idle_callback( gpointer gdata
)
67 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
70 gtk_idle_remove( data
->m_tag
);
72 // check that the items haven't been deleted from the listbox since we had
73 // installed this callback
74 wxListBox
*lbox
= data
->m_listbox
;
75 if ( data
->m_item
< lbox
->GetCount() )
77 lbox
->SetFirstItem( data
->m_item
);
87 //-----------------------------------------------------------------------------
88 // "button_release_event"
89 //-----------------------------------------------------------------------------
91 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
92 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
93 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
94 this can lead to race conditions so that we emit the dclick event
95 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
98 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
99 GdkEventButton
* WXUNUSED(gdk_event
),
102 if (g_isIdle
) wxapp_install_idle_handler();
104 if (g_blockEventsOnDrag
) return FALSE
;
105 if (g_blockEventsOnScroll
) return FALSE
;
107 if (!listbox
->m_hasVMT
) return FALSE
;
109 if (!g_hasDoubleClicked
) return FALSE
;
111 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
112 event
.SetEventObject( listbox
);
114 wxArrayInt aSelections
;
115 int n
, count
= listbox
->GetSelections(aSelections
);
119 if ( listbox
->HasClientObjectData() )
120 event
.SetClientObject( listbox
->GetClientObject(n
) );
121 else if ( listbox
->HasClientUntypedData() )
122 event
.SetClientData( listbox
->GetClientData(n
) );
123 event
.SetString( listbox
->GetString(n
) );
130 event
.m_commandInt
= n
;
132 listbox
->GetEventHandler()->ProcessEvent( event
);
137 //-----------------------------------------------------------------------------
138 // "button_press_event"
139 //-----------------------------------------------------------------------------
142 gtk_listbox_button_press_callback( GtkWidget
*widget
,
143 GdkEventButton
*gdk_event
,
146 if (g_isIdle
) wxapp_install_idle_handler();
148 if (g_blockEventsOnDrag
) return FALSE
;
149 if (g_blockEventsOnScroll
) return FALSE
;
151 if (!listbox
->m_hasVMT
) return FALSE
;
153 int sel
= listbox
->GtkGetIndex( widget
);
155 #if wxUSE_CHECKLISTBOX
156 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
158 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
160 clb
->Check( sel
, !clb
->IsChecked(sel
) );
162 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
163 event
.SetEventObject( listbox
);
165 listbox
->GetEventHandler()->ProcessEvent( event
);
167 #endif // wxUSE_CHECKLISTBOX
169 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
170 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
175 //-----------------------------------------------------------------------------
177 //-----------------------------------------------------------------------------
180 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
183 wxapp_install_idle_handler();
185 if (g_blockEventsOnDrag
)
190 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
192 wxNavigationKeyEvent new_event
;
193 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
194 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
195 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
196 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
197 new_event
.SetCurrentFocus( listbox
);
198 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
201 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
203 // eat return in all modes
207 #if wxUSE_CHECKLISTBOX
208 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
210 int sel
= listbox
->GtkGetIndex( widget
);
212 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
214 clb
->Check( sel
, !clb
->IsChecked(sel
) );
216 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
217 new_event
.SetEventObject( listbox
);
218 new_event
.SetInt( sel
);
219 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
221 #endif // wxUSE_CHECKLISTBOX
225 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
232 //-----------------------------------------------------------------------------
233 // "select" and "deselect"
234 //-----------------------------------------------------------------------------
236 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
);
238 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
240 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
243 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
245 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
248 static void gtk_listitem_select_cb( GtkWidget
*widget
,
252 if (g_isIdle
) wxapp_install_idle_handler();
254 if (!listbox
->m_hasVMT
) return;
255 if (g_blockEventsOnDrag
) return;
257 if (listbox
->m_blockEvent
) return;
259 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
260 event
.SetEventObject( listbox
);
262 // indicate whether this is a selection or a deselection
263 event
.SetExtraLong( is_selection
);
265 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
267 int sel
= listbox
->GtkGetIndex( widget
);
269 if (listbox
->m_prevSelection
!= sel
)
270 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
272 listbox
->m_prevSelection
= sel
;
275 wxArrayInt aSelections
;
276 int n
, count
= listbox
->GetSelections(aSelections
);
280 if ( listbox
->HasClientObjectData() )
281 event
.SetClientObject( listbox
->GetClientObject(n
) );
282 else if ( listbox
->HasClientUntypedData() )
283 event
.SetClientData( listbox
->GetClientData(n
) );
284 event
.SetString( listbox
->GetString(n
) );
291 event
.m_commandInt
= n
;
293 // No longer required with new code in wxLB_SINGLE
294 // listbox->GetEventHandler()->AddPendingEvent( event );
295 listbox
->GetEventHandler()->ProcessEvent( event
);
298 //-----------------------------------------------------------------------------
300 //-----------------------------------------------------------------------------
302 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
304 // ----------------------------------------------------------------------------
306 // ----------------------------------------------------------------------------
308 wxListBox::wxListBox()
310 m_list
= (GtkList
*) NULL
;
311 #if wxUSE_CHECKLISTBOX
312 m_hasCheckBoxes
= FALSE
;
313 #endif // wxUSE_CHECKLISTBOX
316 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
317 const wxPoint
&pos
, const wxSize
&size
,
318 int n
, const wxString choices
[],
319 long style
, const wxValidator
& validator
,
320 const wxString
&name
)
323 m_acceptsFocus
= TRUE
;
324 m_prevSelection
= 0; // or -1 ??
325 m_blockEvent
= FALSE
;
327 if (!PreCreation( parent
, pos
, size
) ||
328 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
330 wxFAIL_MSG( wxT("wxListBox creation failed") );
334 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
335 if (style
& wxLB_ALWAYS_SB
)
337 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
338 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
342 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
343 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
346 m_list
= GTK_LIST( gtk_list_new() );
348 GtkSelectionMode mode
;
349 if (style
& wxLB_MULTIPLE
)
351 mode
= GTK_SELECTION_MULTIPLE
;
353 else if (style
& wxLB_EXTENDED
)
355 mode
= GTK_SELECTION_EXTENDED
;
359 // if style was 0 set single mode
360 m_windowStyle
|= wxLB_SINGLE
;
361 mode
= GTK_SELECTION_MULTIPLE
;
364 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
366 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
368 /* make list scroll when moving the focus down using cursor keys */
369 gtk_container_set_focus_vadjustment(
370 GTK_CONTAINER(m_list
),
371 gtk_scrolled_window_get_vadjustment(
372 GTK_SCROLLED_WINDOW(m_widget
)));
374 gtk_widget_show( GTK_WIDGET(m_list
) );
376 if ( style
& wxLB_SORT
)
378 // this will change DoAppend() behaviour
379 m_strings
= new wxSortedArrayString
;
383 m_strings
= (wxSortedArrayString
*)NULL
;
386 for (int i
= 0; i
< n
; i
++)
389 DoAppend(choices
[i
]);
392 // call it after appending the strings to the listbox, otherwise it doesn't
396 m_parent
->DoAddChild( this );
400 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
401 SetForegroundColour( parent
->GetForegroundColour() );
402 SetFont( parent
->GetFont() );
409 wxListBox::~wxListBox()
419 // ----------------------------------------------------------------------------
421 // ----------------------------------------------------------------------------
423 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
425 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
427 // VZ: notice that InsertItems knows nothing about sorting, so calling it
428 // from outside (and not from our own Append) is likely to break
431 // code elsewhere supposes we have as many items in m_clientList as items
433 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
434 wxT("bug in client data management") );
436 GList
*children
= m_list
->children
;
437 int length
= g_list_length(children
);
439 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
441 size_t nItems
= items
.GetCount();
446 for (size_t n
= 0; n
< nItems
; n
++)
448 index
= m_strings
->Add( items
[n
] );
450 if (index
!= GetCount())
452 GtkAddItem( items
[n
], index
);
453 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
454 m_clientList
.Insert( node
, (wxObject
*) NULL
);
458 GtkAddItem( items
[n
] );
459 m_clientList
.Append( (wxObject
*) NULL
);
467 for ( size_t n
= 0; n
< nItems
; n
++ )
469 GtkAddItem( items
[n
] );
471 m_clientList
.Append((wxObject
*)NULL
);
476 wxList::compatibility_iterator node
= m_clientList
.Item( pos
);
477 for ( size_t n
= 0; n
< nItems
; n
++ )
479 GtkAddItem( items
[n
], pos
+n
);
481 m_clientList
.Insert( node
, (wxObject
*)NULL
);
486 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
487 wxT("bug in client data management") );
490 int wxListBox::DoAppend( const wxString
& item
)
494 // need to determine the index
495 int index
= m_strings
->Add( item
);
497 // only if not at the end anyway
498 if (index
!= GetCount())
500 GtkAddItem( item
, index
);
502 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
503 m_clientList
.Insert( node
, (wxObject
*)NULL
);
511 m_clientList
.Append((wxObject
*)NULL
);
513 return GetCount() - 1;
516 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
518 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
520 GtkWidget
*list_item
;
522 wxString
label(item
);
523 #if wxUSE_CHECKLISTBOX
526 label
.Prepend(wxCHECKLBOX_STRING
);
528 #endif // wxUSE_CHECKLISTBOX
530 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
532 GList
*gitem_list
= g_list_alloc ();
533 gitem_list
->data
= list_item
;
536 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
538 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
540 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
541 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
543 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
544 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
545 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
547 gtk_signal_connect( GTK_OBJECT(list_item
),
548 "button_press_event",
549 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
552 gtk_signal_connect_after( GTK_OBJECT(list_item
),
553 "button_release_event",
554 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
557 gtk_signal_connect( GTK_OBJECT(list_item
),
559 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
562 ConnectWidget( list_item
);
564 gtk_widget_show( list_item
);
566 if (GTK_WIDGET_REALIZED(m_widget
))
568 gtk_widget_realize( list_item
);
569 gtk_widget_realize( GTK_BIN(list_item
)->child
);
571 // Apply current widget style to the new list_item
574 gtk_widget_set_style( GTK_WIDGET( list_item
), m_widgetStyle
);
575 GtkBin
*bin
= GTK_BIN( list_item
);
576 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
577 gtk_widget_set_style( label
, m_widgetStyle
);
581 if (m_tooltip
) m_tooltip
->Apply( this );
586 void wxListBox::DoSetItems( const wxArrayString
& items
,
591 DoInsertItems(items
, 0);
595 size_t count
= items
.GetCount();
596 for ( size_t n
= 0; n
< count
; n
++ )
598 SetClientData(n
, clientData
[n
]);
603 // ----------------------------------------------------------------------------
605 // ----------------------------------------------------------------------------
607 void wxListBox::Clear()
609 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
611 gtk_list_clear_items( m_list
, 0, GetCount() );
613 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
615 // This should be NULL, I think.
616 GTK_LIST(m_list
)->last_focus_child
= NULL
;
619 if ( HasClientObjectData() )
621 // destroy the data (due to Robert's idea of using wxList<wxObject>
622 // and not wxList<wxClientData> we can't just say
623 // m_clientList.DeleteContents(TRUE) - this would crash!
624 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
627 delete (wxClientData
*)node
->GetData();
628 node
= node
->GetNext();
631 m_clientList
.Clear();
637 void wxListBox::Delete( int n
)
639 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
641 GList
*child
= g_list_nth( m_list
->children
, n
);
643 wxCHECK_RET( child
, wxT("wrong listbox index") );
645 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
646 gtk_list_remove_items( m_list
, list
);
649 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
652 if ( m_clientDataItemsType
== wxClientData_Object
)
654 wxClientData
*cd
= (wxClientData
*)node
->GetData();
658 m_clientList
.Erase( node
);
662 m_strings
->RemoveAt(n
);
665 // ----------------------------------------------------------------------------
667 // ----------------------------------------------------------------------------
669 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
671 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
673 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
674 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
676 node
->SetData( (wxObject
*) clientData
);
679 void* wxListBox::DoGetItemClientData( int n
) const
681 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
683 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
684 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
686 return node
->GetData();
689 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
691 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
693 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
694 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
696 // wxItemContainer already deletes data for us
698 node
->SetData( (wxObject
*) clientData
);
701 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
703 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
705 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
706 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
707 wxT("invalid index in wxListBox::DoGetItemClientObject") );
709 return (wxClientData
*) node
->GetData();
712 // ----------------------------------------------------------------------------
713 // string list access
714 // ----------------------------------------------------------------------------
716 wxString
wxListBox::GetRealLabel(GList
*item
) const
718 GtkBin
*bin
= GTK_BIN( item
->data
);
719 GtkLabel
*label
= GTK_LABEL( bin
->child
);
724 str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
726 str
= wxString( label
->label
);
729 #if wxUSE_CHECKLISTBOX
730 // checklistboxes have "[±] " prepended to their lables, remove it
732 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
733 if ( m_hasCheckBoxes
)
735 #endif // wxUSE_CHECKLISTBOX
740 void wxListBox::SetString( int n
, const wxString
&string
)
742 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
744 GList
*child
= g_list_nth( m_list
->children
, n
);
747 GtkBin
*bin
= GTK_BIN( child
->data
);
748 GtkLabel
*label
= GTK_LABEL( bin
->child
);
751 #if wxUSE_CHECKLISTBOX
753 str
+= wxCHECKLBOX_STRING
;
754 #endif // wxUSE_CHECKLISTBOX
757 gtk_label_set( label
, wxGTK_CONV( str
) );
761 wxFAIL_MSG(wxT("wrong listbox index"));
765 wxString
wxListBox::GetString( int n
) const
767 wxCHECK_MSG( m_list
!= NULL
, wxT(""), wxT("invalid listbox") );
769 GList
*child
= g_list_nth( m_list
->children
, n
);
772 return GetRealLabel(child
);
775 wxFAIL_MSG(wxT("wrong listbox index"));
780 int wxListBox::GetCount() const
782 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
784 GList
*children
= m_list
->children
;
785 return g_list_length(children
);
788 int wxListBox::FindString( const wxString
&item
) const
790 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
792 GList
*child
= m_list
->children
;
796 if ( GetRealLabel(child
) == item
)
803 // it's not an error if the string is not found -> no wxCHECK
808 // ----------------------------------------------------------------------------
810 // ----------------------------------------------------------------------------
812 int wxListBox::GetSelection() const
814 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
816 GList
*child
= m_list
->children
;
820 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
827 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
829 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
831 // get the number of selected items first
832 GList
*child
= m_list
->children
;
834 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
836 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
845 aSelections
.Alloc(count
); // optimization attempt
847 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
849 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
857 bool wxListBox::IsSelected( int n
) const
859 wxCHECK_MSG( m_list
!= NULL
, FALSE
, wxT("invalid listbox") );
861 GList
*target
= g_list_nth( m_list
->children
, n
);
863 wxCHECK_MSG( target
, FALSE
, wxT("invalid listbox index") );
865 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
868 void wxListBox::SetSelection( int n
, bool select
)
870 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
876 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
877 gtk_list_unselect_item( m_list
, m_prevSelection
);
878 gtk_list_select_item( m_list
, n
);
882 gtk_list_unselect_item( m_list
, n
);
884 m_blockEvent
= FALSE
;
887 void wxListBox::DoSetFirstItem( int n
)
889 wxCHECK_RET( m_list
, wxT("invalid listbox") );
891 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
894 // terribly efficient
895 const gchar
*vadjustment_key
= "gtk-vadjustment";
896 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
898 GtkAdjustment
*adjustment
=
899 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
900 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
902 GList
*target
= g_list_nth( m_list
->children
, n
);
903 wxCHECK_RET( target
, wxT("invalid listbox index") );
905 GtkWidget
*item
= GTK_WIDGET(target
->data
);
906 wxCHECK_RET( item
, wxT("invalid listbox code") );
908 if (item
->allocation
.y
== -1)
910 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
911 data
->m_listbox
= this;
913 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
918 float y
= item
->allocation
.y
;
919 if (y
> adjustment
->upper
- adjustment
->page_size
)
920 y
= adjustment
->upper
- adjustment
->page_size
;
921 gtk_adjustment_set_value( adjustment
, y
);
924 // ----------------------------------------------------------------------------
926 // ----------------------------------------------------------------------------
928 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
932 GList
*child
= m_list
->children
;
936 if (GTK_WIDGET(child
->data
) == item
) return count
;
945 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
947 GList
*child
= m_list
->children
;
950 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
954 #endif // wxUSE_TOOLTIPS
956 GtkWidget
*wxListBox::GetConnectWidget()
958 return GTK_WIDGET(m_list
);
961 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
963 if (m_widget
->window
== window
) return TRUE
;
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
;
1044 if (wxUpdateUIEvent::CanUpdate(this))
1045 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1048 wxSize
wxListBox::DoGetBestSize() const
1050 int lbWidth
= 100; // some defaults
1054 // Find the widest line
1055 for(int i
= 0; i
< GetCount(); i
++) {
1056 wxString
str(GetString(i
));
1057 GetTextExtent(str
, &wLine
, NULL
);
1058 lbWidth
= wxMax(lbWidth
, wLine
);
1061 // Add room for the scrollbar
1062 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1064 // And just a bit more
1066 GetTextExtent( wxT("X"), &cx
, &cy
);
1069 // don't make the listbox too tall (limit height to around 10 items) but don't
1070 // make it too small neither
1071 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1073 return wxSize(lbWidth
, lbHeight
);
1076 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1078 // the mouse event coords are relative to the listbox items, we need to
1079 // translate them to the normal client coords
1080 x
+= widget
->allocation
.x
;
1081 y
+= widget
->allocation
.y
;
1084 #endif // wxUSE_LISTBOX