1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/listbox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/listbox.h"
18 #include "wx/dynarray.h"
21 #include "wx/settings.h"
22 #include "wx/checklst.h"
23 #include "wx/arrstr.h"
26 #include "wx/gtk1/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
;
51 extern wxWindowGTK
*g_focusWindow
;
52 extern wxWindowGTK
*g_focusWindowLast
;
54 static bool g_hasDoubleClicked
= false;
56 //-----------------------------------------------------------------------------
57 // idle callback for SetFirstItem
58 //-----------------------------------------------------------------------------
60 struct wxlistbox_idle_struct
68 static 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
< (int)lbox
->GetCount() )
80 lbox
->SetFirstItem( data
->m_item
);
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
96 static gint
gtk_listitem_focus_in_callback( GtkWidget
*WXUNUSED(widget
),
97 GdkEvent
*WXUNUSED(event
),
101 wxapp_install_idle_handler();
106 // does the window itself think that it has the focus?
107 if ( !win
->m_hasFocus
)
109 // not yet, notify it
110 win
->m_hasFocus
= true;
112 wxChildFocusEvent
eventChildFocus(win
);
113 (void)win
->HandleWindowEvent(eventChildFocus
);
115 wxFocusEvent
eventFocus(wxEVT_SET_FOCUS
, win
->GetId());
116 eventFocus
.SetEventObject(win
);
118 (void)win
->HandleWindowEvent(eventFocus
);
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
130 static gint
gtk_listitem_focus_out_callback( GtkWidget
*WXUNUSED(widget
),
131 GdkEventFocus
*WXUNUSED(gdk_event
),
135 wxapp_install_idle_handler();
137 g_focusWindow
= NULL
;
139 // don't send the window a kill focus event if it thinks that it doesn't
140 // have focus already
141 if ( win
->m_hasFocus
)
143 win
->m_hasFocus
= false;
145 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
146 event
.SetEventObject( win
);
148 // even if we did process the event in wx code, still let GTK itself
149 // process it too as otherwise bad things happen, especially in GTK2
150 // where the text control simply aborts the program if it doesn't get
151 // the matching focus out event
152 (void)win
->HandleWindowEvent( event
);
159 //-----------------------------------------------------------------------------
160 // "button_release_event"
161 //-----------------------------------------------------------------------------
163 /* we would normally emit a wxEVT_LISTBOX_DCLICK event once
164 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
165 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
166 this can lead to race conditions so that we emit the dclick event
167 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
171 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
172 GdkEventButton
* WXUNUSED(gdk_event
),
175 if (g_isIdle
) wxapp_install_idle_handler();
177 if (g_blockEventsOnDrag
) return FALSE
;
178 if (g_blockEventsOnScroll
) return FALSE
;
180 if (!listbox
->m_hasVMT
) return FALSE
;
182 if (!g_hasDoubleClicked
) return FALSE
;
184 wxCommandEvent
event( wxEVT_LISTBOX_DCLICK
, listbox
->GetId() );
185 event
.SetEventObject( listbox
);
187 wxArrayInt aSelections
;
188 int n
, count
= listbox
->GetSelections(aSelections
);
192 if ( listbox
->HasClientObjectData() )
193 event
.SetClientObject( listbox
->GetClientObject(n
) );
194 else if ( listbox
->HasClientUntypedData() )
195 event
.SetClientData( listbox
->GetClientData(n
) );
196 event
.SetString( listbox
->GetString(n
) );
205 listbox
->HandleWindowEvent( event
);
211 //-----------------------------------------------------------------------------
212 // "button_press_event"
213 //-----------------------------------------------------------------------------
217 gtk_listbox_button_press_callback( GtkWidget
*widget
,
218 GdkEventButton
*gdk_event
,
221 if (g_isIdle
) wxapp_install_idle_handler();
223 if (g_blockEventsOnDrag
) return FALSE
;
224 if (g_blockEventsOnScroll
) return FALSE
;
226 if (!listbox
->m_hasVMT
) return FALSE
;
228 int sel
= listbox
->GtkGetIndex( widget
);
230 #if wxUSE_CHECKLISTBOX
231 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
233 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
235 clb
->Check( sel
, !clb
->IsChecked(sel
) );
237 wxCommandEvent
event( wxEVT_CHECKLISTBOX
, listbox
->GetId() );
238 event
.SetEventObject( listbox
);
240 listbox
->HandleWindowEvent( event
);
242 #endif // wxUSE_CHECKLISTBOX
244 if ((gdk_event
->state
== 0) &&
245 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
246 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
248 listbox
->m_blockEvent
= true;
251 for (i
= 0; i
< (int)listbox
->GetCount(); i
++)
253 gtk_list_unselect_item( GTK_LIST(listbox
->m_list
), i
);
255 listbox
->m_blockEvent
= false;
260 /* emit wxEVT_LISTBOX_DCLICK later */
261 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
267 //-----------------------------------------------------------------------------
269 //-----------------------------------------------------------------------------
273 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
276 wxapp_install_idle_handler();
278 if (g_blockEventsOnDrag
)
283 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
285 wxNavigationKeyEvent new_event
;
286 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
287 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
288 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
289 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
290 new_event
.SetCurrentFocus( listbox
);
291 ret
= listbox
->HandleWindowEvent( new_event
);
294 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
296 // eat return in all modes
300 #if wxUSE_CHECKLISTBOX
301 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
303 int sel
= listbox
->GtkGetIndex( widget
);
305 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
307 clb
->Check( sel
, !clb
->IsChecked(sel
) );
309 wxCommandEvent
new_event( wxEVT_CHECKLISTBOX
, listbox
->GetId() );
310 new_event
.SetEventObject( listbox
);
311 new_event
.SetInt( sel
);
312 ret
= listbox
->HandleWindowEvent( new_event
);
314 #endif // wxUSE_CHECKLISTBOX
316 // Check or uncheck item with SPACE
317 if ((gdk_event
->keyval
== ' ') && (!ret
) &&
318 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
319 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
321 int sel
= listbox
->GtkGetIndex( widget
);
327 if (listbox
->IsSelected( sel
))
328 gtk_list_unselect_item( listbox
->m_list
, sel
);
330 gtk_list_select_item( listbox
->m_list
, sel
);
332 wxCommandEvent
new_event(wxEVT_LISTBOX
, listbox
->GetId() );
333 new_event
.SetEventObject( listbox
);
334 wxArrayInt aSelections
;
335 int n
, count
= listbox
->GetSelections(aSelections
);
339 if ( listbox
->HasClientObjectData() )
340 new_event
.SetClientObject( listbox
->GetClientObject(n
) );
341 else if ( listbox
->HasClientUntypedData() )
342 new_event
.SetClientData( listbox
->GetClientData(n
) );
343 new_event
.SetString( listbox
->GetString(n
) );
350 listbox
->HandleWindowEvent( new_event
);
356 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
364 //-----------------------------------------------------------------------------
365 // "select" and "deselect"
366 //-----------------------------------------------------------------------------
368 static void gtk_listitem_select_cb( GtkWidget
*widget
,
372 if (g_isIdle
) wxapp_install_idle_handler();
374 if (!listbox
->m_hasVMT
) return;
375 if (g_blockEventsOnDrag
) return;
377 if (listbox
->m_blockEvent
) return;
379 wxCommandEvent
event(wxEVT_LISTBOX
, listbox
->GetId() );
380 event
.SetEventObject( listbox
);
382 // indicate whether this is a selection or a deselection
383 event
.SetExtraLong( is_selection
);
385 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
387 int sel
= listbox
->GtkGetIndex( widget
);
389 if (listbox
->m_prevSelection
!= sel
)
390 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
392 listbox
->m_prevSelection
= sel
;
395 wxArrayInt aSelections
;
396 int n
, count
= listbox
->GetSelections(aSelections
);
400 if ( listbox
->HasClientObjectData() )
401 event
.SetClientObject( listbox
->GetClientObject(n
) );
402 else if ( listbox
->HasClientUntypedData() )
403 event
.SetClientData( listbox
->GetClientData(n
) );
404 event
.SetString( listbox
->GetString(n
) );
413 // No longer required with new code in wxLB_SINGLE
414 // listbox->GetEventHandler()->AddPendingEvent( event );
415 listbox
->HandleWindowEvent( event
);
419 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
421 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
426 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
428 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
432 //-----------------------------------------------------------------------------
434 //-----------------------------------------------------------------------------
438 gtk_listbox_realized_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*win
)
441 wxapp_install_idle_handler();
443 GList
*child
= win
->m_list
->children
;
444 for (child
= win
->m_list
->children
; child
!= NULL
; child
= child
->next
)
445 gtk_widget_show( GTK_WIDGET(child
->data
) );
451 //-----------------------------------------------------------------------------
453 //-----------------------------------------------------------------------------
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
459 wxListBox::wxListBox()
462 #if wxUSE_CHECKLISTBOX
463 m_hasCheckBoxes
= false;
464 #endif // wxUSE_CHECKLISTBOX
467 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
468 const wxPoint
&pos
, const wxSize
&size
,
469 const wxArrayString
& choices
,
470 long style
, const wxValidator
& validator
,
471 const wxString
&name
)
473 wxCArrayString
chs(choices
);
475 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
476 style
, validator
, name
);
479 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
480 const wxPoint
&pos
, const wxSize
&size
,
481 int n
, const wxString choices
[],
482 long style
, const wxValidator
& validator
,
483 const wxString
&name
)
486 m_acceptsFocus
= true;
487 m_prevSelection
= 0; // or -1 ??
488 m_blockEvent
= false;
490 if (!PreCreation( parent
, pos
, size
) ||
491 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
493 wxFAIL_MSG( wxT("wxListBox creation failed") );
497 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
498 if (style
& wxLB_ALWAYS_SB
)
500 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
501 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
505 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
506 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
509 m_list
= GTK_LIST( gtk_list_new() );
511 GtkSelectionMode mode
;
512 if (style
& wxLB_MULTIPLE
)
514 mode
= GTK_SELECTION_MULTIPLE
;
516 else if (style
& wxLB_EXTENDED
)
518 mode
= GTK_SELECTION_EXTENDED
;
522 // if style was 0 set single mode
523 m_windowStyle
|= wxLB_SINGLE
;
524 mode
= GTK_SELECTION_SINGLE
;
527 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
529 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
531 /* make list scroll when moving the focus down using cursor keys */
532 gtk_container_set_focus_vadjustment(
533 GTK_CONTAINER(m_list
),
534 gtk_scrolled_window_get_vadjustment(
535 GTK_SCROLLED_WINDOW(m_widget
)));
537 gtk_widget_show( GTK_WIDGET(m_list
) );
539 gtk_signal_connect( GTK_OBJECT(m_list
), "realize",
540 GTK_SIGNAL_FUNC(gtk_listbox_realized_callback
), (gpointer
) this );
542 if ( style
& wxLB_SORT
)
544 // this will change Append() behaviour
545 m_strings
= new wxSortedArrayString
;
554 m_parent
->DoAddChild( this );
557 SetInitialSize(size
); // need this too because this is a wxControlWithItems
562 wxListBox::~wxListBox()
571 // ----------------------------------------------------------------------------
573 // ----------------------------------------------------------------------------
575 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
578 wxClientDataType type
)
580 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
582 const unsigned count
= GetCount();
583 wxCHECK_MSG( pos
<= count
, wxNOT_FOUND
,
584 wxT("invalid index in wxListBox::InsertItems") );
586 // code elsewhere supposes we have as many items in m_clientList as items
588 wxASSERT_MSG( m_clientList
.GetCount() == count
,
589 wxT("bug in client data management") );
591 InvalidateBestSize();
593 const unsigned numItems
= items
.GetCount();
595 for ( unsigned int n
= 0; n
< numItems
; ++n
, ++pos
)
597 const wxString
& item
= items
[n
];
599 const unsigned idx
= m_strings
? m_strings
->Add(item
)
602 GtkAddItem(item
, idx
== GetCount() ? (unsigned) -1 : idx
);
604 m_clientList
.Insert(idx
, NULL
);
606 AssignNewItemClientData(idx
, clientData
, n
, type
);
609 wxASSERT_MSG( m_clientList
.GetCount() == GetCount(),
610 wxT("bug in client data management") );
615 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
617 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
619 GtkWidget
*list_item
;
621 wxString
label(item
);
622 #if wxUSE_CHECKLISTBOX
625 label
.Prepend(wxCHECKLBOX_STRING
);
627 #endif // wxUSE_CHECKLISTBOX
629 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
631 GList
*gitem_list
= g_list_alloc ();
632 gitem_list
->data
= list_item
;
635 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
637 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
639 gtk_signal_connect_after( GTK_OBJECT(list_item
), "select",
640 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
642 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
643 gtk_signal_connect_after( GTK_OBJECT(list_item
), "deselect",
644 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
646 gtk_signal_connect( GTK_OBJECT(list_item
),
647 "button_press_event",
648 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
651 gtk_signal_connect_after( GTK_OBJECT(list_item
),
652 "button_release_event",
653 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
656 gtk_signal_connect( GTK_OBJECT(list_item
),
658 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
662 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_in_event",
663 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback
), (gpointer
)this );
665 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_out_event",
666 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback
), (gpointer
)this );
668 ConnectWidget( list_item
);
670 if (GTK_WIDGET_REALIZED(m_widget
))
672 gtk_widget_show( list_item
);
674 gtk_widget_realize( list_item
);
675 gtk_widget_realize( GTK_BIN(list_item
)->child
);
678 if (m_tooltip
) m_tooltip
->Apply( this );
682 // Apply current widget style to the new list_item
683 GtkRcStyle
*style
= CreateWidgetStyle();
686 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
687 GtkBin
*bin
= GTK_BIN( list_item
);
688 gtk_widget_modify_style( GTK_WIDGET( bin
->child
), style
);
689 gtk_rc_style_unref( style
);
693 // ----------------------------------------------------------------------------
695 // ----------------------------------------------------------------------------
697 void wxListBox::DoClear()
699 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
701 gtk_list_clear_items( m_list
, 0, (int)GetCount() );
703 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
705 // This should be NULL, I think.
706 GTK_LIST(m_list
)->last_focus_child
= NULL
;
709 m_clientList
.Clear();
715 void wxListBox::DoDeleteOneItem(unsigned int n
)
717 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
719 GList
*child
= g_list_nth( m_list
->children
, n
);
721 wxCHECK_RET( child
, wxT("wrong listbox index") );
723 GList
*list
= g_list_append( NULL
, child
->data
);
724 gtk_list_remove_items( m_list
, list
);
727 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
730 m_clientList
.Erase( node
);
734 m_strings
->RemoveAt(n
);
737 // ----------------------------------------------------------------------------
739 // ----------------------------------------------------------------------------
741 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
743 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
745 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
746 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
748 node
->SetData( (wxObject
*) clientData
);
751 void* wxListBox::DoGetItemClientData(unsigned int n
) const
753 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
755 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
756 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
758 return node
->GetData();
761 // ----------------------------------------------------------------------------
762 // string list access
763 // ----------------------------------------------------------------------------
765 wxString
wxListBox::GetRealLabel(GList
*item
) const
767 GtkBin
*bin
= GTK_BIN( item
->data
);
768 GtkLabel
*label
= GTK_LABEL( bin
->child
);
772 str
= wxString( label
->label
);
774 #if wxUSE_CHECKLISTBOX
775 // checklistboxes have "[±] " prepended to their lables, remove it
777 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk1/checklst.h
778 if ( m_hasCheckBoxes
)
780 #endif // wxUSE_CHECKLISTBOX
785 void wxListBox::SetString(unsigned int n
, const wxString
&string
)
787 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
789 GList
*child
= g_list_nth( m_list
->children
, n
);
792 GtkBin
*bin
= GTK_BIN( child
->data
);
793 GtkLabel
*label
= GTK_LABEL( bin
->child
);
796 #if wxUSE_CHECKLISTBOX
798 str
+= wxCHECKLBOX_STRING
;
799 #endif // wxUSE_CHECKLISTBOX
802 gtk_label_set( label
, wxGTK_CONV( str
) );
806 wxFAIL_MSG(wxT("wrong listbox index"));
810 wxString
wxListBox::GetString(unsigned int n
) const
812 wxCHECK_MSG( m_list
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
814 GList
*child
= g_list_nth( m_list
->children
, n
);
817 return GetRealLabel(child
);
820 wxFAIL_MSG(wxT("wrong listbox index"));
822 return wxEmptyString
;
825 unsigned int wxListBox::GetCount() const
827 wxCHECK_MSG( m_list
!= NULL
, 0, wxT("invalid listbox") );
829 GList
*children
= m_list
->children
;
830 return g_list_length(children
);
833 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
835 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
837 GList
*child
= m_list
->children
;
841 if ( item
.IsSameAs( GetRealLabel(child
), bCase
) )
848 // it's not an error if the string is not found -> no wxCHECK
853 // ----------------------------------------------------------------------------
855 // ----------------------------------------------------------------------------
857 int wxListBox::GetSelection() const
859 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
861 GList
*child
= m_list
->children
;
865 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
872 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
874 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
876 // get the number of selected items first
877 GList
*child
= m_list
->children
;
879 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
881 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
890 aSelections
.Alloc(count
); // optimization attempt
892 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
894 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
902 bool wxListBox::IsSelected( int n
) const
904 wxCHECK_MSG( m_list
!= NULL
, false, wxT("invalid listbox") );
906 GList
*target
= g_list_nth( m_list
->children
, n
);
908 wxCHECK_MSG( target
, false, wxT("invalid listbox index") );
910 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
913 void wxListBox::DoSetSelection( int n
, bool select
)
915 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
921 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
922 gtk_list_unselect_item( m_list
, m_prevSelection
);
923 gtk_list_select_item( m_list
, n
);
927 gtk_list_unselect_item( m_list
, n
);
929 m_blockEvent
= false;
932 void wxListBox::DoSetFirstItem( int n
)
934 wxCHECK_RET( m_list
, wxT("invalid listbox") );
936 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
939 // terribly efficient
940 const gchar
*vadjustment_key
= "gtk-vadjustment";
941 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
943 GtkAdjustment
*adjustment
=
944 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
945 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
947 GList
*target
= g_list_nth( m_list
->children
, n
);
948 wxCHECK_RET( target
, wxT("invalid listbox index") );
950 GtkWidget
*item
= GTK_WIDGET(target
->data
);
951 wxCHECK_RET( item
, wxT("invalid listbox code") );
953 if (item
->allocation
.y
== -1)
955 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
956 data
->m_listbox
= this;
958 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
963 float y
= item
->allocation
.y
;
964 if (y
> adjustment
->upper
- adjustment
->page_size
)
965 y
= adjustment
->upper
- adjustment
->page_size
;
966 gtk_adjustment_set_value( adjustment
, y
);
969 // ----------------------------------------------------------------------------
971 // ----------------------------------------------------------------------------
973 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
977 GList
*child
= m_list
->children
;
981 if (GTK_WIDGET(child
->data
) == item
) return count
;
990 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
992 GList
*child
= m_list
->children
;
995 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), NULL
);
999 #endif // wxUSE_TOOLTIPS
1001 GtkWidget
*wxListBox::GetConnectWidget()
1003 // return GTK_WIDGET(m_list);
1007 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
1009 return m_widget
->window
== window
||
1010 GTK_WIDGET(m_list
)->window
== window
;
1013 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1015 if (m_hasBgCol
&& m_backgroundColour
.IsOk())
1017 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
1020 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1021 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1022 gdk_window_clear( window
);
1026 GList
*child
= m_list
->children
;
1029 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1031 GtkBin
*bin
= GTK_BIN( child
->data
);
1032 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1033 gtk_widget_modify_style( label
, style
);
1035 child
= child
->next
;
1039 void wxListBox::OnInternalIdle()
1041 wxCursor cursor
= m_cursor
;
1042 if (g_globalCursor
.IsOk()) cursor
= g_globalCursor
;
1044 if (GTK_WIDGET(m_list
)->window
&& cursor
.IsOk())
1046 /* I now set the cursor the anew in every OnInternalIdle call
1047 as setting the cursor in a parent window also effects the
1048 windows above so that checking for the current cursor is
1051 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1053 GList
*child
= m_list
->children
;
1056 GtkBin
*bin
= GTK_BIN( child
->data
);
1057 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1062 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1064 child
= child
->next
;
1068 if (g_delayedFocus
== this)
1070 if (GTK_WIDGET_REALIZED(m_widget
))
1072 gtk_widget_grab_focus( m_widget
);
1073 g_delayedFocus
= NULL
;
1077 if (wxUpdateUIEvent::CanUpdate(this))
1078 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1081 wxSize
wxListBox::DoGetBestSize() const
1083 int lbWidth
= 100; // some defaults
1087 // Find the widest line
1088 for(unsigned int i
= 0; i
< GetCount(); i
++) {
1089 wxString
str(GetString(i
));
1090 GetTextExtent(str
, &wLine
, NULL
);
1091 lbWidth
= wxMax(lbWidth
, wLine
);
1094 // Add room for the scrollbar
1095 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1097 // And just a bit more
1099 GetTextExtent( wxT("X"), &cx
, &cy
);
1102 // don't make the listbox too tall (limit height to around 10 items) but don't
1103 // make it too small neither
1104 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1106 wxSize
best(lbWidth
, lbHeight
);
1107 CacheBestSize(best
);
1111 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1113 // the mouse event coords are relative to the listbox items, we need to
1114 // translate them to the normal client coords
1115 x
+= widget
->allocation
.x
;
1116 y
+= widget
->allocation
.y
;
1122 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1124 return GetDefaultAttributesFromGTKWidget(gtk_list_new
, true);
1127 #endif // wxUSE_LISTBOX