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"
16 #include "wx/dynarray.h"
17 #include "wx/arrstr.h"
20 #include "wx/checklst.h"
21 #include "wx/settings.h"
22 #include "wx/gtk1/private.h"
25 #include "wx/tooltip.h"
30 #include <gdk/gdkkeysyms.h>
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern void wxapp_install_idle_handler();
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern bool g_blockEventsOnDrag
;
44 extern bool g_blockEventsOnScroll
;
45 extern wxCursor g_globalCursor
;
46 extern wxWindowGTK
*g_delayedFocus
;
47 extern wxWindowGTK
*g_focusWindow
;
48 extern wxWindowGTK
*g_focusWindowLast
;
50 static bool g_hasDoubleClicked
= false;
52 //-----------------------------------------------------------------------------
53 // idle callback for SetFirstItem
54 //-----------------------------------------------------------------------------
56 struct wxlistbox_idle_struct
64 static gint
wxlistbox_idle_callback( gpointer gdata
)
66 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
69 gtk_idle_remove( data
->m_tag
);
71 // check that the items haven't been deleted from the listbox since we had
72 // installed this callback
73 wxListBox
*lbox
= data
->m_listbox
;
74 if ( data
->m_item
< (int)lbox
->GetCount() )
76 lbox
->SetFirstItem( data
->m_item
);
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
92 static gint
gtk_listitem_focus_in_callback( GtkWidget
*widget
,
93 GdkEvent
*WXUNUSED(event
),
97 wxapp_install_idle_handler();
102 // does the window itself think that it has the focus?
103 if ( !win
->m_hasFocus
)
105 // not yet, notify it
106 win
->m_hasFocus
= true;
108 wxChildFocusEvent
eventChildFocus(win
);
109 (void)win
->GetEventHandler()->ProcessEvent(eventChildFocus
);
111 wxFocusEvent
eventFocus(wxEVT_SET_FOCUS
, win
->GetId());
112 eventFocus
.SetEventObject(win
);
114 (void)win
->GetEventHandler()->ProcessEvent(eventFocus
);
121 //-----------------------------------------------------------------------------
123 //-----------------------------------------------------------------------------
126 static gint
gtk_listitem_focus_out_callback( GtkWidget
*widget
, GdkEventFocus
*gdk_event
, wxWindowGTK
*win
)
129 wxapp_install_idle_handler();
131 g_focusWindow
= (wxWindowGTK
*)NULL
;
133 // don't send the window a kill focus event if it thinks that it doesn't
134 // have focus already
135 if ( win
->m_hasFocus
)
137 win
->m_hasFocus
= false;
139 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
140 event
.SetEventObject( win
);
142 // even if we did process the event in wx code, still let GTK itself
143 // process it too as otherwise bad things happen, especially in GTK2
144 // where the text control simply aborts the program if it doesn't get
145 // the matching focus out event
146 (void)win
->GetEventHandler()->ProcessEvent( event
);
153 //-----------------------------------------------------------------------------
154 // "button_release_event"
155 //-----------------------------------------------------------------------------
157 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
158 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
159 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
160 this can lead to race conditions so that we emit the dclick event
161 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
165 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
166 GdkEventButton
* WXUNUSED(gdk_event
),
169 if (g_isIdle
) wxapp_install_idle_handler();
171 if (g_blockEventsOnDrag
) return FALSE
;
172 if (g_blockEventsOnScroll
) return FALSE
;
174 if (!listbox
->m_hasVMT
) return FALSE
;
176 if (!g_hasDoubleClicked
) return FALSE
;
178 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
179 event
.SetEventObject( listbox
);
181 wxArrayInt aSelections
;
182 int n
, count
= listbox
->GetSelections(aSelections
);
186 if ( listbox
->HasClientObjectData() )
187 event
.SetClientObject( listbox
->GetClientObject(n
) );
188 else if ( listbox
->HasClientUntypedData() )
189 event
.SetClientData( listbox
->GetClientData(n
) );
190 event
.SetString( listbox
->GetString(n
) );
199 listbox
->GetEventHandler()->ProcessEvent( event
);
205 //-----------------------------------------------------------------------------
206 // "button_press_event"
207 //-----------------------------------------------------------------------------
211 gtk_listbox_button_press_callback( GtkWidget
*widget
,
212 GdkEventButton
*gdk_event
,
215 if (g_isIdle
) wxapp_install_idle_handler();
217 if (g_blockEventsOnDrag
) return FALSE
;
218 if (g_blockEventsOnScroll
) return FALSE
;
220 if (!listbox
->m_hasVMT
) return FALSE
;
222 int sel
= listbox
->GtkGetIndex( widget
);
224 #if wxUSE_CHECKLISTBOX
225 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
227 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
229 clb
->Check( sel
, !clb
->IsChecked(sel
) );
231 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
232 event
.SetEventObject( listbox
);
234 listbox
->GetEventHandler()->ProcessEvent( event
);
236 #endif // wxUSE_CHECKLISTBOX
238 if ((gdk_event
->state
== 0) &&
239 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
240 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
242 listbox
->m_blockEvent
= true;
245 for (i
= 0; i
< (int)listbox
->GetCount(); i
++)
247 gtk_list_unselect_item( GTK_LIST(listbox
->m_list
), i
);
249 listbox
->m_blockEvent
= false;
254 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
255 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
261 //-----------------------------------------------------------------------------
263 //-----------------------------------------------------------------------------
267 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
270 wxapp_install_idle_handler();
272 if (g_blockEventsOnDrag
)
277 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
279 wxNavigationKeyEvent new_event
;
280 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
281 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
282 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
283 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
284 new_event
.SetCurrentFocus( listbox
);
285 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
288 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
290 // eat return in all modes
294 #if wxUSE_CHECKLISTBOX
295 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
297 int sel
= listbox
->GtkGetIndex( widget
);
299 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
301 clb
->Check( sel
, !clb
->IsChecked(sel
) );
303 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
304 new_event
.SetEventObject( listbox
);
305 new_event
.SetInt( sel
);
306 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
308 #endif // wxUSE_CHECKLISTBOX
310 // Check or uncheck item with SPACE
311 if ((gdk_event
->keyval
== ' ') && (!ret
) &&
312 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
313 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
315 int sel
= listbox
->GtkGetIndex( widget
);
321 if (listbox
->IsSelected( sel
))
322 gtk_list_unselect_item( listbox
->m_list
, sel
);
324 gtk_list_select_item( listbox
->m_list
, sel
);
326 wxCommandEvent
new_event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
327 new_event
.SetEventObject( listbox
);
328 wxArrayInt aSelections
;
329 int n
, count
= listbox
->GetSelections(aSelections
);
333 if ( listbox
->HasClientObjectData() )
334 new_event
.SetClientObject( listbox
->GetClientObject(n
) );
335 else if ( listbox
->HasClientUntypedData() )
336 new_event
.SetClientData( listbox
->GetClientData(n
) );
337 new_event
.SetString( listbox
->GetString(n
) );
344 listbox
->GetEventHandler()->ProcessEvent( new_event
);
350 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
358 //-----------------------------------------------------------------------------
359 // "select" and "deselect"
360 //-----------------------------------------------------------------------------
362 static void gtk_listitem_select_cb( GtkWidget
*widget
,
366 if (g_isIdle
) wxapp_install_idle_handler();
368 if (!listbox
->m_hasVMT
) return;
369 if (g_blockEventsOnDrag
) return;
371 if (listbox
->m_blockEvent
) return;
373 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
374 event
.SetEventObject( listbox
);
376 // indicate whether this is a selection or a deselection
377 event
.SetExtraLong( is_selection
);
379 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
381 int sel
= listbox
->GtkGetIndex( widget
);
383 if (listbox
->m_prevSelection
!= sel
)
384 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
386 listbox
->m_prevSelection
= sel
;
389 wxArrayInt aSelections
;
390 int n
, count
= listbox
->GetSelections(aSelections
);
394 if ( listbox
->HasClientObjectData() )
395 event
.SetClientObject( listbox
->GetClientObject(n
) );
396 else if ( listbox
->HasClientUntypedData() )
397 event
.SetClientData( listbox
->GetClientData(n
) );
398 event
.SetString( listbox
->GetString(n
) );
407 // No longer required with new code in wxLB_SINGLE
408 // listbox->GetEventHandler()->AddPendingEvent( event );
409 listbox
->GetEventHandler()->ProcessEvent( event
);
413 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
415 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
420 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
422 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
426 //-----------------------------------------------------------------------------
428 //-----------------------------------------------------------------------------
432 gtk_listbox_realized_callback( GtkWidget
*m_widget
, wxListBox
*win
)
435 wxapp_install_idle_handler();
437 GList
*child
= win
->m_list
->children
;
438 for (child
= win
->m_list
->children
; child
!= NULL
; child
= child
->next
)
439 gtk_widget_show( GTK_WIDGET(child
->data
) );
445 //-----------------------------------------------------------------------------
447 //-----------------------------------------------------------------------------
449 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
455 wxListBox::wxListBox()
457 m_list
= (GtkList
*) NULL
;
458 #if wxUSE_CHECKLISTBOX
459 m_hasCheckBoxes
= false;
460 #endif // wxUSE_CHECKLISTBOX
463 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
464 const wxPoint
&pos
, const wxSize
&size
,
465 const wxArrayString
& choices
,
466 long style
, const wxValidator
& validator
,
467 const wxString
&name
)
469 wxCArrayString
chs(choices
);
471 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
472 style
, validator
, name
);
475 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
476 const wxPoint
&pos
, const wxSize
&size
,
477 int n
, const wxString choices
[],
478 long style
, const wxValidator
& validator
,
479 const wxString
&name
)
482 m_acceptsFocus
= true;
483 m_prevSelection
= 0; // or -1 ??
484 m_blockEvent
= false;
486 if (!PreCreation( parent
, pos
, size
) ||
487 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
489 wxFAIL_MSG( wxT("wxListBox creation failed") );
493 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
494 if (style
& wxLB_ALWAYS_SB
)
496 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
497 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
501 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
502 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
505 m_list
= GTK_LIST( gtk_list_new() );
507 GtkSelectionMode mode
;
508 if (style
& wxLB_MULTIPLE
)
510 mode
= GTK_SELECTION_MULTIPLE
;
512 else if (style
& wxLB_EXTENDED
)
514 mode
= GTK_SELECTION_EXTENDED
;
518 // if style was 0 set single mode
519 m_windowStyle
|= wxLB_SINGLE
;
520 mode
= GTK_SELECTION_SINGLE
;
523 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
525 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
527 /* make list scroll when moving the focus down using cursor keys */
528 gtk_container_set_focus_vadjustment(
529 GTK_CONTAINER(m_list
),
530 gtk_scrolled_window_get_vadjustment(
531 GTK_SCROLLED_WINDOW(m_widget
)));
533 gtk_widget_show( GTK_WIDGET(m_list
) );
535 gtk_signal_connect( GTK_OBJECT(m_list
), "realize",
536 GTK_SIGNAL_FUNC(gtk_listbox_realized_callback
), (gpointer
) this );
538 if ( style
& wxLB_SORT
)
540 // this will change DoAppend() behaviour
541 m_strings
= new wxSortedArrayString
;
545 m_strings
= (wxSortedArrayString
*)NULL
;
548 for (int i
= 0; i
< n
; i
++)
551 DoAppend(choices
[i
]);
554 m_parent
->DoAddChild( this );
557 SetBestSize(size
); // need this too because this is a wxControlWithItems
562 wxListBox::~wxListBox()
572 // ----------------------------------------------------------------------------
574 // ----------------------------------------------------------------------------
576 void wxListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
578 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
580 // VZ: notice that InsertItems knows nothing about sorting, so calling it
581 // from outside (and not from our own Append) is likely to break
584 // code elsewhere supposes we have as many items in m_clientList as items
586 wxASSERT_MSG( m_clientList
.GetCount() == GetCount(),
587 wxT("bug in client data management") );
589 InvalidateBestSize();
591 GList
*children
= m_list
->children
;
592 unsigned int length
= g_list_length(children
);
594 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
596 unsigned int nItems
= items
.GetCount();
601 for (unsigned int n
= 0; n
< nItems
; n
++)
603 index
= m_strings
->Add( items
[n
] );
605 if (index
!= (int)GetCount())
607 GtkAddItem( items
[n
], index
);
608 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
609 m_clientList
.Insert( node
, (wxObject
*) NULL
);
613 GtkAddItem( items
[n
] );
614 m_clientList
.Append( (wxObject
*) NULL
);
622 for ( unsigned int n
= 0; n
< nItems
; n
++ )
624 GtkAddItem( items
[n
] );
626 m_clientList
.Append((wxObject
*)NULL
);
631 wxList::compatibility_iterator node
= m_clientList
.Item( pos
);
632 for ( unsigned int n
= 0; n
< nItems
; n
++ )
634 GtkAddItem( items
[n
], pos
+n
);
636 m_clientList
.Insert( node
, (wxObject
*)NULL
);
641 wxASSERT_MSG( m_clientList
.GetCount() == GetCount(),
642 wxT("bug in client data management") );
645 int wxListBox::DoAppend( const wxString
& item
)
647 InvalidateBestSize();
651 // need to determine the index
652 int index
= m_strings
->Add( item
);
654 // only if not at the end anyway
655 if (index
!= (int)GetCount())
657 GtkAddItem( item
, index
);
659 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
660 m_clientList
.Insert( node
, (wxObject
*)NULL
);
668 m_clientList
.Append((wxObject
*)NULL
);
670 return GetCount() - 1;
673 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
675 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
677 GtkWidget
*list_item
;
679 wxString
label(item
);
680 #if wxUSE_CHECKLISTBOX
683 label
.Prepend(wxCHECKLBOX_STRING
);
685 #endif // wxUSE_CHECKLISTBOX
687 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
689 GList
*gitem_list
= g_list_alloc ();
690 gitem_list
->data
= list_item
;
693 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
695 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
697 gtk_signal_connect_after( GTK_OBJECT(list_item
), "select",
698 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
700 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
701 gtk_signal_connect_after( GTK_OBJECT(list_item
), "deselect",
702 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
704 gtk_signal_connect( GTK_OBJECT(list_item
),
705 "button_press_event",
706 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
709 gtk_signal_connect_after( GTK_OBJECT(list_item
),
710 "button_release_event",
711 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
714 gtk_signal_connect( GTK_OBJECT(list_item
),
716 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
720 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_in_event",
721 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback
), (gpointer
)this );
723 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_out_event",
724 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback
), (gpointer
)this );
726 ConnectWidget( list_item
);
728 if (GTK_WIDGET_REALIZED(m_widget
))
730 gtk_widget_show( list_item
);
732 gtk_widget_realize( list_item
);
733 gtk_widget_realize( GTK_BIN(list_item
)->child
);
736 if (m_tooltip
) m_tooltip
->Apply( this );
740 // Apply current widget style to the new list_item
741 GtkRcStyle
*style
= CreateWidgetStyle();
744 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
745 GtkBin
*bin
= GTK_BIN( list_item
);
746 gtk_widget_modify_style( GTK_WIDGET( bin
->child
), style
);
747 gtk_rc_style_unref( style
);
751 void wxListBox::DoSetItems( const wxArrayString
& items
,
756 DoInsertItems(items
, 0);
760 unsigned int count
= items
.GetCount();
761 for ( unsigned int n
= 0; n
< count
; n
++ )
763 SetClientData(n
, clientData
[n
]);
768 // ----------------------------------------------------------------------------
770 // ----------------------------------------------------------------------------
772 void wxListBox::Clear()
774 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
776 gtk_list_clear_items( m_list
, 0, (int)GetCount() );
778 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
780 // This should be NULL, I think.
781 GTK_LIST(m_list
)->last_focus_child
= NULL
;
784 if ( HasClientObjectData() )
786 // destroy the data (due to Robert's idea of using wxList<wxObject>
787 // and not wxList<wxClientData> we can't just say
788 // m_clientList.DeleteContents(true) - this would crash!
789 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
792 delete (wxClientData
*)node
->GetData();
793 node
= node
->GetNext();
796 m_clientList
.Clear();
802 void wxListBox::Delete(unsigned int n
)
804 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
806 GList
*child
= g_list_nth( m_list
->children
, n
);
808 wxCHECK_RET( child
, wxT("wrong listbox index") );
810 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
811 gtk_list_remove_items( m_list
, list
);
814 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
817 if ( m_clientDataItemsType
== wxClientData_Object
)
819 wxClientData
*cd
= (wxClientData
*)node
->GetData();
823 m_clientList
.Erase( node
);
827 m_strings
->RemoveAt(n
);
830 // ----------------------------------------------------------------------------
832 // ----------------------------------------------------------------------------
834 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
836 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
838 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
839 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
841 node
->SetData( (wxObject
*) clientData
);
844 void* wxListBox::DoGetItemClientData(unsigned int n
) const
846 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
848 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
849 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
851 return node
->GetData();
854 void wxListBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
856 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
858 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
859 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
861 // wxItemContainer already deletes data for us
863 node
->SetData( (wxObject
*) clientData
);
866 wxClientData
* wxListBox::DoGetItemClientObject(unsigned int n
) const
868 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
870 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
871 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
872 wxT("invalid index in wxListBox::DoGetItemClientObject") );
874 return (wxClientData
*) node
->GetData();
877 // ----------------------------------------------------------------------------
878 // string list access
879 // ----------------------------------------------------------------------------
881 wxString
wxListBox::GetRealLabel(GList
*item
) const
883 GtkBin
*bin
= GTK_BIN( item
->data
);
884 GtkLabel
*label
= GTK_LABEL( bin
->child
);
888 str
= wxString( label
->label
);
890 #if wxUSE_CHECKLISTBOX
891 // checklistboxes have "[±] " prepended to their lables, remove it
893 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk1/checklst.h
894 if ( m_hasCheckBoxes
)
896 #endif // wxUSE_CHECKLISTBOX
901 void wxListBox::SetString(unsigned int n
, const wxString
&string
)
903 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
905 GList
*child
= g_list_nth( m_list
->children
, n
);
908 GtkBin
*bin
= GTK_BIN( child
->data
);
909 GtkLabel
*label
= GTK_LABEL( bin
->child
);
912 #if wxUSE_CHECKLISTBOX
914 str
+= wxCHECKLBOX_STRING
;
915 #endif // wxUSE_CHECKLISTBOX
918 gtk_label_set( label
, wxGTK_CONV( str
) );
922 wxFAIL_MSG(wxT("wrong listbox index"));
926 wxString
wxListBox::GetString(unsigned int n
) const
928 wxCHECK_MSG( m_list
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
930 GList
*child
= g_list_nth( m_list
->children
, n
);
933 return GetRealLabel(child
);
936 wxFAIL_MSG(wxT("wrong listbox index"));
938 return wxEmptyString
;
941 unsigned int wxListBox::GetCount() const
943 wxCHECK_MSG( m_list
!= NULL
, 0, wxT("invalid listbox") );
945 GList
*children
= m_list
->children
;
946 return g_list_length(children
);
949 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
951 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
953 GList
*child
= m_list
->children
;
957 if ( item
.IsSameAs( GetRealLabel(child
), bCase
) )
964 // it's not an error if the string is not found -> no wxCHECK
969 // ----------------------------------------------------------------------------
971 // ----------------------------------------------------------------------------
973 int wxListBox::GetSelection() const
975 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
977 GList
*child
= m_list
->children
;
981 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
988 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
990 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
992 // get the number of selected items first
993 GList
*child
= m_list
->children
;
995 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
997 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
1001 aSelections
.Empty();
1005 // now fill the list
1006 aSelections
.Alloc(count
); // optimization attempt
1008 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
1010 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
1018 bool wxListBox::IsSelected( int n
) const
1020 wxCHECK_MSG( m_list
!= NULL
, false, wxT("invalid listbox") );
1022 GList
*target
= g_list_nth( m_list
->children
, n
);
1024 wxCHECK_MSG( target
, false, wxT("invalid listbox index") );
1026 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
1029 void wxListBox::DoSetSelection( int n
, bool select
)
1031 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
1033 m_blockEvent
= true;
1037 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
1038 gtk_list_unselect_item( m_list
, m_prevSelection
);
1039 gtk_list_select_item( m_list
, n
);
1040 m_prevSelection
= n
;
1043 gtk_list_unselect_item( m_list
, n
);
1045 m_blockEvent
= false;
1048 void wxListBox::DoSetFirstItem( int n
)
1050 wxCHECK_RET( m_list
, wxT("invalid listbox") );
1052 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
1055 // terribly efficient
1056 const gchar
*vadjustment_key
= "gtk-vadjustment";
1057 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
1059 GtkAdjustment
*adjustment
=
1060 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
1061 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
1063 GList
*target
= g_list_nth( m_list
->children
, n
);
1064 wxCHECK_RET( target
, wxT("invalid listbox index") );
1066 GtkWidget
*item
= GTK_WIDGET(target
->data
);
1067 wxCHECK_RET( item
, wxT("invalid listbox code") );
1069 if (item
->allocation
.y
== -1)
1071 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
1072 data
->m_listbox
= this;
1074 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
1079 float y
= item
->allocation
.y
;
1080 if (y
> adjustment
->upper
- adjustment
->page_size
)
1081 y
= adjustment
->upper
- adjustment
->page_size
;
1082 gtk_adjustment_set_value( adjustment
, y
);
1085 // ----------------------------------------------------------------------------
1087 // ----------------------------------------------------------------------------
1089 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
1093 GList
*child
= m_list
->children
;
1097 if (GTK_WIDGET(child
->data
) == item
) return count
;
1099 child
= child
->next
;
1106 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
1108 GList
*child
= m_list
->children
;
1111 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
1112 child
= child
->next
;
1115 #endif // wxUSE_TOOLTIPS
1117 GtkWidget
*wxListBox::GetConnectWidget()
1119 // return GTK_WIDGET(m_list);
1123 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
1128 if (m_widget
->window
== window
) return true;
1130 if (GTK_WIDGET(m_list
)->window
== window
) return true;
1132 GList
*child
= m_list
->children
;
1135 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
1136 if (bin
->window
== window
) return true;
1137 child
= child
->next
;
1144 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1146 if (m_hasBgCol
&& m_backgroundColour
.Ok())
1148 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
1151 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1152 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1153 gdk_window_clear( window
);
1157 GList
*child
= m_list
->children
;
1160 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1162 GtkBin
*bin
= GTK_BIN( child
->data
);
1163 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1164 gtk_widget_modify_style( label
, style
);
1166 child
= child
->next
;
1170 void wxListBox::OnInternalIdle()
1172 wxCursor cursor
= m_cursor
;
1173 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1175 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1177 /* I now set the cursor the anew in every OnInternalIdle call
1178 as setting the cursor in a parent window also effects the
1179 windows above so that checking for the current cursor is
1182 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1184 GList
*child
= m_list
->children
;
1187 GtkBin
*bin
= GTK_BIN( child
->data
);
1188 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1193 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1195 child
= child
->next
;
1199 if (g_delayedFocus
== this)
1201 if (GTK_WIDGET_REALIZED(m_widget
))
1203 gtk_widget_grab_focus( m_widget
);
1204 g_delayedFocus
= NULL
;
1208 if (wxUpdateUIEvent::CanUpdate(this))
1209 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1212 wxSize
wxListBox::DoGetBestSize() const
1214 int lbWidth
= 100; // some defaults
1218 // Find the widest line
1219 for(unsigned int i
= 0; i
< GetCount(); i
++) {
1220 wxString
str(GetString(i
));
1221 GetTextExtent(str
, &wLine
, NULL
);
1222 lbWidth
= wxMax(lbWidth
, wLine
);
1225 // Add room for the scrollbar
1226 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1228 // And just a bit more
1230 GetTextExtent( wxT("X"), &cx
, &cy
);
1233 // don't make the listbox too tall (limit height to around 10 items) but don't
1234 // make it too small neither
1235 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1237 wxSize
best(lbWidth
, lbHeight
);
1238 CacheBestSize(best
);
1242 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1244 // the mouse event coords are relative to the listbox items, we need to
1245 // translate them to the normal client coords
1246 x
+= widget
->allocation
.x
;
1247 y
+= widget
->allocation
.y
;
1253 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1255 return GetDefaultAttributesFromGTKWidget(gtk_list_new
, true);
1258 #endif // wxUSE_LISTBOX