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 //-------------------------------------------------------------------------
41 // conditional compilation
42 //-------------------------------------------------------------------------
44 #if (GTK_MINOR_VERSION > 0)
45 #define NEW_GTK_SCROLL_CODE
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 #if wxUSE_CHECKLISTBOX
54 // checklistboxes have "[±] " prepended to their lables, this macro removes it
55 // (NB: 4 below is the length of wxCHECKLBOX_STRING above)
57 // the argument to it is a "const char *" pointer
58 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
60 #else // !wxUSE_CHECKLISTBOX
62 #define GET_REAL_LABEL(label) (label)
64 #endif // wxUSE_CHECKLISTBOX
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 extern bool g_blockEventsOnDrag
;
71 extern bool g_blockEventsOnScroll
;
72 extern wxCursor g_globalCursor
;
74 static bool g_hasDoubleClicked
= FALSE
;
76 //-----------------------------------------------------------------------------
77 // idle callback for SetFirstItem
78 //-----------------------------------------------------------------------------
80 struct wxlistbox_idle_struct
87 static gint
wxlistbox_idle_callback( gpointer gdata
)
89 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
92 gtk_idle_remove( data
->m_tag
);
94 data
->m_listbox
->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 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
272 event
.SetEventObject( listbox
);
273 // MSW doesn't do that either
274 // event.SetExtraLong( (long) is_selection );
277 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
279 int sel
= listbox
->GtkGetIndex( widget
);
281 if (listbox
->m_prevSelection
!= sel
)
282 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
284 listbox
->m_prevSelection
= sel
;
287 wxArrayInt aSelections
;
288 int n
, count
= listbox
->GetSelections(aSelections
);
292 if ( listbox
->HasClientObjectData() )
293 event
.SetClientObject( listbox
->GetClientObject(n
) );
294 else if ( listbox
->HasClientUntypedData() )
295 event
.SetClientData( listbox
->GetClientData(n
) );
296 event
.SetString( listbox
->GetString(n
) );
303 event
.m_commandInt
= n
;
305 // No longer required with new code in wxLB_SINGLE
306 // listbox->GetEventHandler()->AddPendingEvent( event );
307 listbox
->GetEventHandler()->ProcessEvent( event
);
310 //-----------------------------------------------------------------------------
312 //-----------------------------------------------------------------------------
314 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 wxListBox::wxListBox()
322 m_list
= (GtkList
*) NULL
;
323 #if wxUSE_CHECKLISTBOX
324 m_hasCheckBoxes
= FALSE
;
325 #endif // wxUSE_CHECKLISTBOX
328 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
329 const wxPoint
&pos
, const wxSize
&size
,
330 int n
, const wxString choices
[],
331 long style
, const wxValidator
& validator
,
332 const wxString
&name
)
335 m_acceptsFocus
= TRUE
;
336 m_prevSelection
= 0; // or -1 ??
338 if (!PreCreation( parent
, pos
, size
) ||
339 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
341 wxFAIL_MSG( wxT("wxListBox creation failed") );
345 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
346 if (style
& wxLB_ALWAYS_SB
)
348 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
349 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
353 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
354 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
357 m_list
= GTK_LIST( gtk_list_new() );
359 GtkSelectionMode mode
;
360 if (style
& wxLB_MULTIPLE
)
362 mode
= GTK_SELECTION_MULTIPLE
;
364 else if (style
& wxLB_EXTENDED
)
366 mode
= GTK_SELECTION_EXTENDED
;
370 // if style was 0 set single mode
371 m_windowStyle
|= wxLB_SINGLE
;
372 mode
= GTK_SELECTION_MULTIPLE
;
375 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
377 #ifdef NEW_GTK_SCROLL_CODE
378 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
380 gtk_container_add( GTK_CONTAINER(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
) );
393 if ( style
& wxLB_SORT
)
395 // this will change DoAppend() behaviour
396 m_strings
= new wxSortedArrayString
;
400 m_strings
= (wxSortedArrayString
*)NULL
;
403 for (int i
= 0; i
< n
; i
++)
406 DoAppend(choices
[i
]);
409 m_parent
->DoAddChild( this );
413 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
414 SetForegroundColour( parent
->GetForegroundColour() );
415 SetFont( parent
->GetFont() );
422 wxListBox::~wxListBox()
432 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
434 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
436 // VZ: notice that InsertItems knows nothing about sorting, so calling it
437 // from outside (and not from our own Append) is likely to break
440 // code elsewhere supposes we have as many items in m_clientList as items
442 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
443 wxT("bug in client data management") );
445 GList
*children
= m_list
->children
;
446 int length
= g_list_length(children
);
448 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
450 size_t nItems
= items
.GetCount();
455 for (size_t n
= 0; n
< nItems
; n
++)
457 index
= m_strings
->Add( items
[n
] );
459 if (index
!= GetCount())
461 GtkAddItem( items
[n
], index
);
462 wxNode
*node
= m_clientList
.Nth( index
);
463 m_clientList
.Insert( node
, (wxObject
*) NULL
);
467 GtkAddItem( items
[n
] );
468 m_clientList
.Append( (wxObject
*) NULL
);
476 for ( size_t n
= 0; n
< nItems
; n
++ )
478 GtkAddItem( items
[n
] );
480 m_clientList
.Append((wxObject
*)NULL
);
485 wxNode
*node
= m_clientList
.Nth( pos
);
486 for ( size_t n
= 0; n
< nItems
; n
++ )
488 GtkAddItem( items
[n
], pos
+n
);
490 m_clientList
.Insert( node
, (wxObject
*)NULL
);
495 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
496 wxT("bug in client data management") );
499 int wxListBox::DoAppend( const wxString
& item
)
503 // need to determine the index
504 int index
= m_strings
->Add( item
);
506 // only if not at the end anyway
507 if (index
!= GetCount())
509 GtkAddItem( item
, index
);
511 wxNode
*node
= m_clientList
.Nth( index
);
512 m_clientList
.Insert( node
, (wxObject
*)NULL
);
520 m_clientList
.Append((wxObject
*)NULL
);
522 return GetCount() - 1;
525 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
527 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
529 GtkWidget
*list_item
;
531 wxString
label(item
);
532 #if wxUSE_CHECKLISTBOX
535 label
.Prepend(wxCHECKLBOX_STRING
);
537 #endif // wxUSE_CHECKLISTBOX
539 list_item
= gtk_list_item_new_with_label( label
.mbc_str() );
541 GList
*gitem_list
= g_list_alloc ();
542 gitem_list
->data
= list_item
;
545 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
547 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
549 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
550 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
552 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
553 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
554 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
556 gtk_signal_connect( GTK_OBJECT(list_item
),
557 "button_press_event",
558 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
561 gtk_signal_connect_after( GTK_OBJECT(list_item
),
562 "button_release_event",
563 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
566 gtk_signal_connect( GTK_OBJECT(list_item
),
568 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
571 ConnectWidget( list_item
);
573 gtk_widget_show( list_item
);
575 if (GTK_WIDGET_REALIZED(m_widget
))
577 gtk_widget_realize( list_item
);
578 gtk_widget_realize( GTK_BIN(list_item
)->child
);
580 // Apply current widget style to the new list_item
583 gtk_widget_set_style( GTK_WIDGET( list_item
), m_widgetStyle
);
584 GtkBin
*bin
= GTK_BIN( list_item
);
585 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
586 gtk_widget_set_style( label
, m_widgetStyle
);
590 if (m_tooltip
) m_tooltip
->Apply( this );
595 void wxListBox::DoSetItems( const wxArrayString
& items
,
600 DoInsertItems(items
, 0);
604 size_t count
= items
.GetCount();
605 for ( size_t n
= 0; n
< count
; n
++ )
607 SetClientData(n
, clientData
[n
]);
612 // ----------------------------------------------------------------------------
614 // ----------------------------------------------------------------------------
616 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
618 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
620 wxNode
*node
= m_clientList
.Nth( n
);
621 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
623 node
->SetData( (wxObject
*) clientData
);
626 void* wxListBox::DoGetItemClientData( int n
) const
628 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
630 wxNode
*node
= m_clientList
.Nth( n
);
631 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
636 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
638 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
640 wxNode
*node
= m_clientList
.Nth( n
);
641 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
643 wxClientData
*cd
= (wxClientData
*) node
->Data();
646 node
->SetData( (wxObject
*) clientData
);
649 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
651 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
653 wxNode
*node
= m_clientList
.Nth( n
);
654 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
655 wxT("invalid index in wxListBox::DoGetItemClientObject") );
657 return (wxClientData
*) node
->Data();
660 void wxListBox::Clear()
662 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
664 gtk_list_clear_items( m_list
, 0, GetCount() );
666 if ( HasClientObjectData() )
668 // destroy the data (due to Robert's idea of using wxList<wxObject>
669 // and not wxList<wxClientData> we can't just say
670 // m_clientList.DeleteContents(TRUE) - this would crash!
671 wxNode
*node
= m_clientList
.First();
674 delete (wxClientData
*)node
->Data();
678 m_clientList
.Clear();
684 void wxListBox::Delete( int n
)
686 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
688 GList
*child
= g_list_nth( m_list
->children
, n
);
690 wxCHECK_RET( child
, wxT("wrong listbox index") );
692 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
693 gtk_list_remove_items( m_list
, list
);
696 wxNode
*node
= m_clientList
.Nth( n
);
699 if ( m_clientDataItemsType
== wxClientData_Object
)
701 wxClientData
*cd
= (wxClientData
*)node
->Data();
705 m_clientList
.DeleteNode( node
);
709 m_strings
->Remove(n
);
712 // ----------------------------------------------------------------------------
713 // string list access
714 // ----------------------------------------------------------------------------
716 void wxListBox::SetString( int n
, const wxString
&string
)
718 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
720 GList
*child
= g_list_nth( m_list
->children
, n
);
723 GtkBin
*bin
= GTK_BIN( child
->data
);
724 GtkLabel
*label
= GTK_LABEL( bin
->child
);
727 #if wxUSE_CHECKLISTBOX
729 str
+= wxCHECKLBOX_STRING
;
730 #endif // wxUSE_CHECKLISTBOX
733 gtk_label_set( label
, str
.mbc_str() );
737 wxFAIL_MSG(wxT("wrong listbox index"));
741 wxString
wxListBox::GetString( int n
) const
743 wxCHECK_MSG( m_list
!= NULL
, wxT(""), wxT("invalid listbox") );
745 GList
*child
= g_list_nth( m_list
->children
, n
);
748 GtkBin
*bin
= GTK_BIN( child
->data
);
749 GtkLabel
*label
= GTK_LABEL( bin
->child
);
751 wxString str
= wxString(GET_REAL_LABEL(label
->label
),*wxConvCurrent
);
756 wxFAIL_MSG(wxT("wrong listbox index"));
761 int wxListBox::GetCount() const
763 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
765 GList
*children
= m_list
->children
;
766 return g_list_length(children
);
769 int wxListBox::FindString( const wxString
&item
) const
771 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
773 GList
*child
= m_list
->children
;
777 GtkBin
*bin
= GTK_BIN( child
->data
);
778 GtkLabel
*label
= GTK_LABEL( bin
->child
);
780 wxString str
= wxString(GET_REAL_LABEL(label
->label
),*wxConvCurrent
);
789 // it's not an error if the string is not found -> no wxCHECK
794 // ----------------------------------------------------------------------------
796 // ----------------------------------------------------------------------------
798 int wxListBox::GetSelection() const
800 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
802 GList
*child
= m_list
->children
;
806 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
813 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
815 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
817 // get the number of selected items first
818 GList
*child
= m_list
->children
;
820 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
822 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
831 aSelections
.Alloc(count
); // optimization attempt
833 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
835 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
843 bool wxListBox::IsSelected( int n
) const
845 wxCHECK_MSG( m_list
!= NULL
, FALSE
, wxT("invalid listbox") );
847 GList
*target
= g_list_nth( m_list
->children
, n
);
849 wxCHECK_MSG( target
, FALSE
, wxT("invalid listbox index") );
851 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
854 void wxListBox::SetSelection( int n
, bool select
)
856 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
862 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
863 gtk_list_unselect_item( m_list
, m_prevSelection
);
864 gtk_list_select_item( m_list
, n
);
868 gtk_list_unselect_item( m_list
, n
);
873 void wxListBox::DoSetFirstItem( int n
)
875 wxCHECK_RET( m_list
, wxT("invalid listbox") );
877 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
880 // terribly efficient
881 const gchar
*vadjustment_key
= "gtk-vadjustment";
882 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
884 GtkAdjustment
*adjustment
=
885 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
886 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
888 GList
*target
= g_list_nth( m_list
->children
, n
);
889 wxCHECK_RET( target
, wxT("invalid listbox index") );
891 GtkWidget
*item
= GTK_WIDGET(target
->data
);
892 wxCHECK_RET( item
, wxT("invalid listbox code") );
894 if (item
->allocation
.y
== -1)
896 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
897 data
->m_listbox
= this;
899 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
904 float y
= item
->allocation
.y
;
905 if (y
> adjustment
->upper
- adjustment
->page_size
)
906 y
= adjustment
->upper
- adjustment
->page_size
;
907 gtk_adjustment_set_value( adjustment
, y
);
910 // ----------------------------------------------------------------------------
912 // ----------------------------------------------------------------------------
914 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
918 GList
*child
= m_list
->children
;
922 if (GTK_WIDGET(child
->data
) == item
) return count
;
931 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
933 GList
*child
= m_list
->children
;
936 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
940 #endif // wxUSE_TOOLTIPS
942 void wxListBox::GtkDisableEvents()
944 GList
*child
= m_list
->children
;
947 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
948 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
950 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
951 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
952 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
958 void wxListBox::GtkEnableEvents()
960 GList
*child
= m_list
->children
;
963 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
964 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
966 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
967 gtk_signal_connect( GTK_OBJECT(child
->data
), "deselect",
968 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
974 GtkWidget
*wxListBox::GetConnectWidget()
976 return GTK_WIDGET(m_list
);
979 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
981 if (GTK_WIDGET(m_list
)->window
== window
) return TRUE
;
983 GList
*child
= m_list
->children
;
986 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
987 if (bin
->window
== window
) return TRUE
;
994 void wxListBox::ApplyWidgetStyle()
998 if (m_backgroundColour
.Ok())
1000 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
1003 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1004 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1005 gdk_window_clear( window
);
1009 GList
*child
= m_list
->children
;
1012 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
1014 GtkBin
*bin
= GTK_BIN( child
->data
);
1015 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1016 gtk_widget_set_style( label
, m_widgetStyle
);
1018 child
= child
->next
;
1022 void wxListBox::OnInternalIdle()
1024 wxCursor cursor
= m_cursor
;
1025 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1027 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1029 /* I now set the cursor the anew in every OnInternalIdle call
1030 as setting the cursor in a parent window also effects the
1031 windows above so that checking for the current cursor is
1034 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1036 GList
*child
= m_list
->children
;
1039 GtkBin
*bin
= GTK_BIN( child
->data
);
1040 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1045 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1047 child
= child
->next
;
1054 wxSize
wxListBox::DoGetBestSize() const
1056 int lbWidth
= 100; // some defaults
1060 // Find the widest line
1061 for(int i
= 0; i
< GetCount(); i
++) {
1062 wxString
str(GetString(i
));
1063 GetTextExtent(str
, &wLine
, NULL
);
1064 lbWidth
= wxMax(lbWidth
, wLine
);
1067 // Add room for the scrollbar
1068 lbWidth
+= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X
);
1070 // And just a bit more
1072 GetTextExtent("X", &cx
, &cy
);
1075 // don't make the listbox too tall (limit height to around 10 items) but don't
1076 // make it too small neither
1077 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1079 return wxSize(lbWidth
, lbHeight
);