1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/listbox.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/listbox.h"
17 #include "wx/dynarray.h"
20 #include "wx/settings.h"
21 #include "wx/checklst.h"
22 #include "wx/arrstr.h"
25 #include "wx/gtk1/private.h"
28 #include "wx/tooltip.h"
33 #include <gdk/gdkkeysyms.h>
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern void wxapp_install_idle_handler();
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 extern bool g_blockEventsOnDrag
;
47 extern bool g_blockEventsOnScroll
;
48 extern wxCursor g_globalCursor
;
49 extern wxWindowGTK
*g_delayedFocus
;
50 extern wxWindowGTK
*g_focusWindow
;
51 extern wxWindowGTK
*g_focusWindowLast
;
53 static bool g_hasDoubleClicked
= false;
55 //-----------------------------------------------------------------------------
56 // idle callback for SetFirstItem
57 //-----------------------------------------------------------------------------
59 struct wxlistbox_idle_struct
67 static gint
wxlistbox_idle_callback( gpointer gdata
)
69 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
72 gtk_idle_remove( data
->m_tag
);
74 // check that the items haven't been deleted from the listbox since we had
75 // installed this callback
76 wxListBox
*lbox
= data
->m_listbox
;
77 if ( data
->m_item
< (int)lbox
->GetCount() )
79 lbox
->SetFirstItem( data
->m_item
);
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
95 static gint
gtk_listitem_focus_in_callback( GtkWidget
*WXUNUSED(widget
),
96 GdkEvent
*WXUNUSED(event
),
100 wxapp_install_idle_handler();
105 // does the window itself think that it has the focus?
106 if ( !win
->m_hasFocus
)
108 // not yet, notify it
109 win
->m_hasFocus
= true;
111 wxChildFocusEvent
eventChildFocus(win
);
112 (void)win
->HandleWindowEvent(eventChildFocus
);
114 wxFocusEvent
eventFocus(wxEVT_SET_FOCUS
, win
->GetId());
115 eventFocus
.SetEventObject(win
);
117 (void)win
->HandleWindowEvent(eventFocus
);
124 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
129 static gint
gtk_listitem_focus_out_callback( GtkWidget
*WXUNUSED(widget
),
130 GdkEventFocus
*WXUNUSED(gdk_event
),
134 wxapp_install_idle_handler();
136 g_focusWindow
= NULL
;
138 // don't send the window a kill focus event if it thinks that it doesn't
139 // have focus already
140 if ( win
->m_hasFocus
)
142 win
->m_hasFocus
= false;
144 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
145 event
.SetEventObject( win
);
147 // even if we did process the event in wx code, still let GTK itself
148 // process it too as otherwise bad things happen, especially in GTK2
149 // where the text control simply aborts the program if it doesn't get
150 // the matching focus out event
151 (void)win
->HandleWindowEvent( event
);
158 //-----------------------------------------------------------------------------
159 // "button_release_event"
160 //-----------------------------------------------------------------------------
162 /* we would normally emit a wxEVT_LISTBOX_DCLICK event once
163 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
164 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
165 this can lead to race conditions so that we emit the dclick event
166 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
170 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
171 GdkEventButton
* WXUNUSED(gdk_event
),
174 if (g_isIdle
) wxapp_install_idle_handler();
176 if (g_blockEventsOnDrag
) return FALSE
;
177 if (g_blockEventsOnScroll
) return FALSE
;
179 if (!listbox
->m_hasVMT
) return FALSE
;
181 if (!g_hasDoubleClicked
) return FALSE
;
183 wxCommandEvent
event( wxEVT_LISTBOX_DCLICK
, listbox
->GetId() );
184 event
.SetEventObject( listbox
);
186 wxArrayInt aSelections
;
187 int n
, count
= listbox
->GetSelections(aSelections
);
191 if ( listbox
->HasClientObjectData() )
192 event
.SetClientObject( listbox
->GetClientObject(n
) );
193 else if ( listbox
->HasClientUntypedData() )
194 event
.SetClientData( listbox
->GetClientData(n
) );
195 event
.SetString( listbox
->GetString(n
) );
204 listbox
->HandleWindowEvent( event
);
210 //-----------------------------------------------------------------------------
211 // "button_press_event"
212 //-----------------------------------------------------------------------------
216 gtk_listbox_button_press_callback( GtkWidget
*widget
,
217 GdkEventButton
*gdk_event
,
220 if (g_isIdle
) wxapp_install_idle_handler();
222 if (g_blockEventsOnDrag
) return FALSE
;
223 if (g_blockEventsOnScroll
) return FALSE
;
225 if (!listbox
->m_hasVMT
) return FALSE
;
227 int sel
= listbox
->GtkGetIndex( widget
);
229 #if wxUSE_CHECKLISTBOX
230 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
232 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
234 clb
->Check( sel
, !clb
->IsChecked(sel
) );
236 wxCommandEvent
event( wxEVT_CHECKLISTBOX
, listbox
->GetId() );
237 event
.SetEventObject( listbox
);
239 listbox
->HandleWindowEvent( event
);
241 #endif // wxUSE_CHECKLISTBOX
243 if ((gdk_event
->state
== 0) &&
244 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
245 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
247 listbox
->m_blockEvent
= true;
250 for (i
= 0; i
< (int)listbox
->GetCount(); i
++)
252 gtk_list_unselect_item( GTK_LIST(listbox
->m_list
), i
);
254 listbox
->m_blockEvent
= false;
259 /* emit wxEVT_LISTBOX_DCLICK later */
260 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
266 //-----------------------------------------------------------------------------
268 //-----------------------------------------------------------------------------
272 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
275 wxapp_install_idle_handler();
277 if (g_blockEventsOnDrag
)
282 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
284 wxNavigationKeyEvent new_event
;
285 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
286 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
287 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
288 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
289 new_event
.SetCurrentFocus( listbox
);
290 ret
= listbox
->HandleWindowEvent( new_event
);
293 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
295 // eat return in all modes
299 #if wxUSE_CHECKLISTBOX
300 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
302 int sel
= listbox
->GtkGetIndex( widget
);
304 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
306 clb
->Check( sel
, !clb
->IsChecked(sel
) );
308 wxCommandEvent
new_event( wxEVT_CHECKLISTBOX
, listbox
->GetId() );
309 new_event
.SetEventObject( listbox
);
310 new_event
.SetInt( sel
);
311 ret
= listbox
->HandleWindowEvent( new_event
);
313 #endif // wxUSE_CHECKLISTBOX
315 // Check or uncheck item with SPACE
316 if ((gdk_event
->keyval
== ' ') && (!ret
) &&
317 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
318 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
320 int sel
= listbox
->GtkGetIndex( widget
);
326 if (listbox
->IsSelected( sel
))
327 gtk_list_unselect_item( listbox
->m_list
, sel
);
329 gtk_list_select_item( listbox
->m_list
, sel
);
331 wxCommandEvent
new_event(wxEVT_LISTBOX
, listbox
->GetId() );
332 new_event
.SetEventObject( listbox
);
333 wxArrayInt aSelections
;
334 int n
, count
= listbox
->GetSelections(aSelections
);
338 if ( listbox
->HasClientObjectData() )
339 new_event
.SetClientObject( listbox
->GetClientObject(n
) );
340 else if ( listbox
->HasClientUntypedData() )
341 new_event
.SetClientData( listbox
->GetClientData(n
) );
342 new_event
.SetString( listbox
->GetString(n
) );
349 listbox
->HandleWindowEvent( new_event
);
355 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
363 //-----------------------------------------------------------------------------
364 // "select" and "deselect"
365 //-----------------------------------------------------------------------------
367 static void gtk_listitem_select_cb( GtkWidget
*widget
,
371 if (g_isIdle
) wxapp_install_idle_handler();
373 if (!listbox
->m_hasVMT
) return;
374 if (g_blockEventsOnDrag
) return;
376 if (listbox
->m_blockEvent
) return;
378 wxCommandEvent
event(wxEVT_LISTBOX
, listbox
->GetId() );
379 event
.SetEventObject( listbox
);
381 // indicate whether this is a selection or a deselection
382 event
.SetExtraLong( is_selection
);
384 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
386 int sel
= listbox
->GtkGetIndex( widget
);
388 if (listbox
->m_prevSelection
!= sel
)
389 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
391 listbox
->m_prevSelection
= sel
;
394 wxArrayInt aSelections
;
395 int n
, count
= listbox
->GetSelections(aSelections
);
399 if ( listbox
->HasClientObjectData() )
400 event
.SetClientObject( listbox
->GetClientObject(n
) );
401 else if ( listbox
->HasClientUntypedData() )
402 event
.SetClientData( listbox
->GetClientData(n
) );
403 event
.SetString( listbox
->GetString(n
) );
412 // No longer required with new code in wxLB_SINGLE
413 // listbox->GetEventHandler()->AddPendingEvent( event );
414 listbox
->HandleWindowEvent( event
);
418 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
420 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
425 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
427 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
431 //-----------------------------------------------------------------------------
433 //-----------------------------------------------------------------------------
437 gtk_listbox_realized_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*win
)
440 wxapp_install_idle_handler();
442 GList
*child
= win
->m_list
->children
;
443 for (child
= win
->m_list
->children
; child
!= NULL
; child
= child
->next
)
444 gtk_widget_show( GTK_WIDGET(child
->data
) );
450 //-----------------------------------------------------------------------------
452 //-----------------------------------------------------------------------------
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 wxListBox::wxListBox()
461 #if wxUSE_CHECKLISTBOX
462 m_hasCheckBoxes
= false;
463 #endif // wxUSE_CHECKLISTBOX
466 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
467 const wxPoint
&pos
, const wxSize
&size
,
468 const wxArrayString
& choices
,
469 long style
, const wxValidator
& validator
,
470 const wxString
&name
)
472 wxCArrayString
chs(choices
);
474 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
475 style
, validator
, name
);
478 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
479 const wxPoint
&pos
, const wxSize
&size
,
480 int n
, const wxString choices
[],
481 long style
, const wxValidator
& validator
,
482 const wxString
&name
)
485 m_acceptsFocus
= true;
486 m_prevSelection
= 0; // or -1 ??
487 m_blockEvent
= false;
489 if (!PreCreation( parent
, pos
, size
) ||
490 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
492 wxFAIL_MSG( wxT("wxListBox creation failed") );
496 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
497 if (style
& wxLB_ALWAYS_SB
)
499 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
500 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
504 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
505 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
508 m_list
= GTK_LIST( gtk_list_new() );
510 GtkSelectionMode mode
;
511 if (style
& wxLB_MULTIPLE
)
513 mode
= GTK_SELECTION_MULTIPLE
;
515 else if (style
& wxLB_EXTENDED
)
517 mode
= GTK_SELECTION_EXTENDED
;
521 // if style was 0 set single mode
522 m_windowStyle
|= wxLB_SINGLE
;
523 mode
= GTK_SELECTION_SINGLE
;
526 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
528 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
530 /* make list scroll when moving the focus down using cursor keys */
531 gtk_container_set_focus_vadjustment(
532 GTK_CONTAINER(m_list
),
533 gtk_scrolled_window_get_vadjustment(
534 GTK_SCROLLED_WINDOW(m_widget
)));
536 gtk_widget_show( GTK_WIDGET(m_list
) );
538 gtk_signal_connect( GTK_OBJECT(m_list
), "realize",
539 GTK_SIGNAL_FUNC(gtk_listbox_realized_callback
), (gpointer
) this );
541 if ( style
& wxLB_SORT
)
543 // this will change Append() behaviour
544 m_strings
= new wxSortedArrayString
;
553 m_parent
->DoAddChild( this );
556 SetInitialSize(size
); // need this too because this is a wxControlWithItems
561 wxListBox::~wxListBox()
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
574 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
577 wxClientDataType type
)
579 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
581 const unsigned count
= GetCount();
582 wxCHECK_MSG( pos
<= count
, wxNOT_FOUND
,
583 wxT("invalid index in wxListBox::InsertItems") );
585 // code elsewhere supposes we have as many items in m_clientList as items
587 wxASSERT_MSG( m_clientList
.GetCount() == count
,
588 wxT("bug in client data management") );
590 InvalidateBestSize();
592 const unsigned numItems
= items
.GetCount();
594 for ( unsigned int n
= 0; n
< numItems
; ++n
, ++pos
)
596 const wxString
& item
= items
[n
];
598 const unsigned idx
= m_strings
? m_strings
->Add(item
)
601 GtkAddItem(item
, idx
== GetCount() ? (unsigned) -1 : idx
);
603 m_clientList
.Insert(idx
, NULL
);
605 AssignNewItemClientData(idx
, clientData
, n
, type
);
608 wxASSERT_MSG( m_clientList
.GetCount() == GetCount(),
609 wxT("bug in client data management") );
614 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
616 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
618 GtkWidget
*list_item
;
620 wxString
label(item
);
621 #if wxUSE_CHECKLISTBOX
624 label
.Prepend(wxCHECKLBOX_STRING
);
626 #endif // wxUSE_CHECKLISTBOX
628 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
630 GList
*gitem_list
= g_list_alloc ();
631 gitem_list
->data
= list_item
;
634 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
636 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
638 gtk_signal_connect_after( GTK_OBJECT(list_item
), "select",
639 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
641 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
642 gtk_signal_connect_after( GTK_OBJECT(list_item
), "deselect",
643 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
645 gtk_signal_connect( GTK_OBJECT(list_item
),
646 "button_press_event",
647 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
650 gtk_signal_connect_after( GTK_OBJECT(list_item
),
651 "button_release_event",
652 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
655 gtk_signal_connect( GTK_OBJECT(list_item
),
657 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
661 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_in_event",
662 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback
), (gpointer
)this );
664 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_out_event",
665 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback
), (gpointer
)this );
667 ConnectWidget( list_item
);
669 if (GTK_WIDGET_REALIZED(m_widget
))
671 gtk_widget_show( list_item
);
673 gtk_widget_realize( list_item
);
674 gtk_widget_realize( GTK_BIN(list_item
)->child
);
677 if (m_tooltip
) m_tooltip
->Apply( this );
681 // Apply current widget style to the new list_item
682 GtkRcStyle
*style
= CreateWidgetStyle();
685 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
686 GtkBin
*bin
= GTK_BIN( list_item
);
687 gtk_widget_modify_style( GTK_WIDGET( bin
->child
), style
);
688 gtk_rc_style_unref( style
);
692 // ----------------------------------------------------------------------------
694 // ----------------------------------------------------------------------------
696 void wxListBox::DoClear()
698 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
700 gtk_list_clear_items( m_list
, 0, (int)GetCount() );
702 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
704 // This should be NULL, I think.
705 GTK_LIST(m_list
)->last_focus_child
= NULL
;
708 m_clientList
.Clear();
714 void wxListBox::DoDeleteOneItem(unsigned int n
)
716 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
718 GList
*child
= g_list_nth( m_list
->children
, n
);
720 wxCHECK_RET( child
, wxT("wrong listbox index") );
722 GList
*list
= g_list_append( NULL
, child
->data
);
723 gtk_list_remove_items( m_list
, list
);
726 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
729 m_clientList
.Erase( node
);
733 m_strings
->RemoveAt(n
);
736 // ----------------------------------------------------------------------------
738 // ----------------------------------------------------------------------------
740 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
742 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
744 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
745 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
747 node
->SetData( (wxObject
*) clientData
);
750 void* wxListBox::DoGetItemClientData(unsigned int n
) const
752 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
754 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
755 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
757 return node
->GetData();
760 // ----------------------------------------------------------------------------
761 // string list access
762 // ----------------------------------------------------------------------------
764 wxString
wxListBox::GetRealLabel(GList
*item
) const
766 GtkBin
*bin
= GTK_BIN( item
->data
);
767 GtkLabel
*label
= GTK_LABEL( bin
->child
);
771 str
= wxString( label
->label
);
773 #if wxUSE_CHECKLISTBOX
774 // checklistboxes have "[±] " prepended to their lables, remove it
776 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk1/checklst.h
777 if ( m_hasCheckBoxes
)
779 #endif // wxUSE_CHECKLISTBOX
784 void wxListBox::SetString(unsigned int n
, const wxString
&string
)
786 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
788 GList
*child
= g_list_nth( m_list
->children
, n
);
791 GtkBin
*bin
= GTK_BIN( child
->data
);
792 GtkLabel
*label
= GTK_LABEL( bin
->child
);
795 #if wxUSE_CHECKLISTBOX
797 str
+= wxCHECKLBOX_STRING
;
798 #endif // wxUSE_CHECKLISTBOX
801 gtk_label_set( label
, wxGTK_CONV( str
) );
805 wxFAIL_MSG(wxT("wrong listbox index"));
809 wxString
wxListBox::GetString(unsigned int n
) const
811 wxCHECK_MSG( m_list
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
813 GList
*child
= g_list_nth( m_list
->children
, n
);
816 return GetRealLabel(child
);
819 wxFAIL_MSG(wxT("wrong listbox index"));
821 return wxEmptyString
;
824 unsigned int wxListBox::GetCount() const
826 wxCHECK_MSG( m_list
!= NULL
, 0, wxT("invalid listbox") );
828 GList
*children
= m_list
->children
;
829 return g_list_length(children
);
832 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
834 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
836 GList
*child
= m_list
->children
;
840 if ( item
.IsSameAs( GetRealLabel(child
), bCase
) )
847 // it's not an error if the string is not found -> no wxCHECK
852 // ----------------------------------------------------------------------------
854 // ----------------------------------------------------------------------------
856 int wxListBox::GetSelection() const
858 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
860 GList
*child
= m_list
->children
;
864 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
871 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
873 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
875 // get the number of selected items first
876 GList
*child
= m_list
->children
;
878 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
880 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
889 aSelections
.Alloc(count
); // optimization attempt
891 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
893 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
901 bool wxListBox::IsSelected( int n
) const
903 wxCHECK_MSG( m_list
!= NULL
, false, wxT("invalid listbox") );
905 GList
*target
= g_list_nth( m_list
->children
, n
);
907 wxCHECK_MSG( target
, false, wxT("invalid listbox index") );
909 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
912 void wxListBox::DoSetSelection( int n
, bool select
)
914 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
920 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
921 gtk_list_unselect_item( m_list
, m_prevSelection
);
922 gtk_list_select_item( m_list
, n
);
926 gtk_list_unselect_item( m_list
, n
);
928 m_blockEvent
= false;
931 void wxListBox::DoSetFirstItem( int n
)
933 wxCHECK_RET( m_list
, wxT("invalid listbox") );
935 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
938 // terribly efficient
939 const gchar
*vadjustment_key
= "gtk-vadjustment";
940 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
942 GtkAdjustment
*adjustment
=
943 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
944 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
946 GList
*target
= g_list_nth( m_list
->children
, n
);
947 wxCHECK_RET( target
, wxT("invalid listbox index") );
949 GtkWidget
*item
= GTK_WIDGET(target
->data
);
950 wxCHECK_RET( item
, wxT("invalid listbox code") );
952 if (item
->allocation
.y
== -1)
954 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
955 data
->m_listbox
= this;
957 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
962 float y
= item
->allocation
.y
;
963 if (y
> adjustment
->upper
- adjustment
->page_size
)
964 y
= adjustment
->upper
- adjustment
->page_size
;
965 gtk_adjustment_set_value( adjustment
, y
);
968 // ----------------------------------------------------------------------------
970 // ----------------------------------------------------------------------------
972 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
976 GList
*child
= m_list
->children
;
980 if (GTK_WIDGET(child
->data
) == item
) return count
;
989 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
991 GList
*child
= m_list
->children
;
994 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), NULL
);
998 #endif // wxUSE_TOOLTIPS
1000 GtkWidget
*wxListBox::GetConnectWidget()
1002 // return GTK_WIDGET(m_list);
1006 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
1008 return m_widget
->window
== window
||
1009 GTK_WIDGET(m_list
)->window
== window
;
1012 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1014 if (m_hasBgCol
&& m_backgroundColour
.IsOk())
1016 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
1019 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1020 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1021 gdk_window_clear( window
);
1025 GList
*child
= m_list
->children
;
1028 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1030 GtkBin
*bin
= GTK_BIN( child
->data
);
1031 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1032 gtk_widget_modify_style( label
, style
);
1034 child
= child
->next
;
1038 void wxListBox::OnInternalIdle()
1040 wxCursor cursor
= m_cursor
;
1041 if (g_globalCursor
.IsOk()) cursor
= g_globalCursor
;
1043 if (GTK_WIDGET(m_list
)->window
&& cursor
.IsOk())
1045 /* I now set the cursor the anew in every OnInternalIdle call
1046 as setting the cursor in a parent window also effects the
1047 windows above so that checking for the current cursor is
1050 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1052 GList
*child
= m_list
->children
;
1055 GtkBin
*bin
= GTK_BIN( child
->data
);
1056 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1061 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1063 child
= child
->next
;
1067 if (g_delayedFocus
== this)
1069 if (GTK_WIDGET_REALIZED(m_widget
))
1071 gtk_widget_grab_focus( m_widget
);
1072 g_delayedFocus
= NULL
;
1076 if (wxUpdateUIEvent::CanUpdate(this))
1077 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1080 wxSize
wxListBox::DoGetBestSize() const
1082 int lbWidth
= 100; // some defaults
1086 // Find the widest line
1087 for(unsigned int i
= 0; i
< GetCount(); i
++) {
1088 wxString
str(GetString(i
));
1089 GetTextExtent(str
, &wLine
, NULL
);
1090 lbWidth
= wxMax(lbWidth
, wLine
);
1093 // Add room for the scrollbar
1094 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1096 // And just a bit more
1098 GetTextExtent( wxT("X"), &cx
, &cy
);
1101 // don't make the listbox too tall (limit height to around 10 items) but don't
1102 // make it too small neither
1103 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1105 wxSize
best(lbWidth
, lbHeight
);
1106 CacheBestSize(best
);
1110 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1112 // the mouse event coords are relative to the listbox items, we need to
1113 // translate them to the normal client coords
1114 x
+= widget
->allocation
.x
;
1115 y
+= widget
->allocation
.y
;
1121 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1123 return GetDefaultAttributesFromGTKWidget(gtk_list_new
, true);
1126 #endif // wxUSE_LISTBOX