1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "listbox.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/listbox.h"
23 #include "wx/dynarray.h"
24 #include "wx/arrstr.h"
27 #include "wx/checklst.h"
28 #include "wx/settings.h"
29 #include "wx/gtk/private.h"
32 #include "wx/tooltip.h"
37 #include <gdk/gdkkeysyms.h>
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern void wxapp_install_idle_handler();
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 extern bool g_blockEventsOnDrag
;
51 extern bool g_blockEventsOnScroll
;
52 extern wxCursor g_globalCursor
;
53 extern wxWindowGTK
*g_delayedFocus
;
54 extern wxWindowGTK
*g_focusWindow
;
55 extern wxWindowGTK
*g_focusWindowLast
;
57 static bool g_hasDoubleClicked
= FALSE
;
59 //-----------------------------------------------------------------------------
60 // idle callback for SetFirstItem
61 //-----------------------------------------------------------------------------
63 struct wxlistbox_idle_struct
70 extern "C" gint
wxlistbox_idle_callback( gpointer gdata
)
72 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
75 gtk_idle_remove( data
->m_tag
);
77 // check that the items haven't been deleted from the listbox since we had
78 // installed this callback
79 wxListBox
*lbox
= data
->m_listbox
;
80 if ( data
->m_item
< lbox
->GetCount() )
82 lbox
->SetFirstItem( data
->m_item
);
92 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
96 static gint
gtk_listitem_focus_in_callback( GtkWidget
*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
->GetEventHandler()->ProcessEvent(eventChildFocus
);
115 wxFocusEvent
eventFocus(wxEVT_SET_FOCUS
, win
->GetId());
116 eventFocus
.SetEventObject(win
);
118 (void)win
->GetEventHandler()->ProcessEvent(eventFocus
);
124 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
128 static gint
gtk_listitem_focus_out_callback( GtkWidget
*widget
, GdkEventFocus
*gdk_event
, wxWindowGTK
*win
)
131 wxapp_install_idle_handler();
133 g_focusWindow
= (wxWindowGTK
*)NULL
;
135 // don't send the window a kill focus event if it thinks that it doesn't
136 // have focus already
137 if ( win
->m_hasFocus
)
139 win
->m_hasFocus
= FALSE
;
141 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
142 event
.SetEventObject( win
);
144 // even if we did process the event in wx code, still let GTK itself
145 // process it too as otherwise bad things happen, especially in GTK2
146 // where the text control simply aborts the program if it doesn't get
147 // the matching focus out event
148 (void)win
->GetEventHandler()->ProcessEvent( event
);
154 //-----------------------------------------------------------------------------
155 // "button_release_event"
156 //-----------------------------------------------------------------------------
158 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
159 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
160 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
161 this can lead to race conditions so that we emit the dclick event
162 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
);
204 //-----------------------------------------------------------------------------
205 // "button_press_event"
206 //-----------------------------------------------------------------------------
209 gtk_listbox_button_press_callback( GtkWidget
*widget
,
210 GdkEventButton
*gdk_event
,
213 if (g_isIdle
) wxapp_install_idle_handler();
215 if (g_blockEventsOnDrag
) return FALSE
;
216 if (g_blockEventsOnScroll
) return FALSE
;
218 if (!listbox
->m_hasVMT
) return FALSE
;
220 int sel
= listbox
->GtkGetIndex( widget
);
222 #if wxUSE_CHECKLISTBOX
223 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
225 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
227 clb
->Check( sel
, !clb
->IsChecked(sel
) );
229 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
230 event
.SetEventObject( listbox
);
232 listbox
->GetEventHandler()->ProcessEvent( event
);
234 #endif // wxUSE_CHECKLISTBOX
236 if ((gdk_event
->state
== 0) &&
237 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
238 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
240 if (listbox
->IsSelected(sel
))
242 gtk_list_unselect_item( GTK_LIST(listbox
->m_list
), sel
);
247 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
248 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
253 //-----------------------------------------------------------------------------
255 //-----------------------------------------------------------------------------
258 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
261 wxapp_install_idle_handler();
263 if (g_blockEventsOnDrag
)
268 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
270 wxNavigationKeyEvent new_event
;
271 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
272 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
273 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
274 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
275 new_event
.SetCurrentFocus( listbox
);
276 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
279 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
281 // eat return in all modes
285 #if wxUSE_CHECKLISTBOX
286 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
288 int sel
= listbox
->GtkGetIndex( widget
);
290 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
292 clb
->Check( sel
, !clb
->IsChecked(sel
) );
294 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
295 new_event
.SetEventObject( listbox
);
296 new_event
.SetInt( sel
);
297 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
299 #endif // wxUSE_CHECKLISTBOX
301 // Check or uncheck item with SPACE
302 if ((gdk_event
->keyval
== ' ') && (!ret
) &&
303 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
304 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
306 int sel
= listbox
->GtkGetIndex( widget
);
312 if (listbox
->IsSelected( sel
))
313 gtk_list_unselect_item( listbox
->m_list
, sel
);
315 gtk_list_select_item( listbox
->m_list
, sel
);
317 wxCommandEvent
new_event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
318 new_event
.SetEventObject( listbox
);
319 wxArrayInt aSelections
;
320 int n
, count
= listbox
->GetSelections(aSelections
);
324 if ( listbox
->HasClientObjectData() )
325 new_event
.SetClientObject( listbox
->GetClientObject(n
) );
326 else if ( listbox
->HasClientUntypedData() )
327 new_event
.SetClientData( listbox
->GetClientData(n
) );
328 new_event
.SetString( listbox
->GetString(n
) );
335 listbox
->GetEventHandler()->ProcessEvent( new_event
);
341 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
348 //-----------------------------------------------------------------------------
349 // "select" and "deselect"
350 //-----------------------------------------------------------------------------
352 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
);
354 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
356 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
359 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
361 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
364 static void gtk_listitem_select_cb( GtkWidget
*widget
,
368 if (g_isIdle
) wxapp_install_idle_handler();
370 if (!listbox
->m_hasVMT
) return;
371 if (g_blockEventsOnDrag
) return;
373 if (listbox
->m_blockEvent
) return;
375 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
376 event
.SetEventObject( listbox
);
378 // indicate whether this is a selection or a deselection
379 event
.SetExtraLong( is_selection
);
381 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
383 int sel
= listbox
->GtkGetIndex( widget
);
385 if (listbox
->m_prevSelection
!= sel
)
386 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
388 listbox
->m_prevSelection
= sel
;
391 wxArrayInt aSelections
;
392 int n
, count
= listbox
->GetSelections(aSelections
);
396 if ( listbox
->HasClientObjectData() )
397 event
.SetClientObject( listbox
->GetClientObject(n
) );
398 else if ( listbox
->HasClientUntypedData() )
399 event
.SetClientData( listbox
->GetClientData(n
) );
400 event
.SetString( listbox
->GetString(n
) );
409 // No longer required with new code in wxLB_SINGLE
410 // listbox->GetEventHandler()->AddPendingEvent( event );
411 listbox
->GetEventHandler()->ProcessEvent( event
);
414 //-----------------------------------------------------------------------------
416 //-----------------------------------------------------------------------------
419 gtk_listbox_realized_callback( GtkWidget
*m_widget
, wxListBox
*win
)
422 wxapp_install_idle_handler();
424 GList
*child
= win
->m_list
->children
;
425 for (child
= win
->m_list
->children
; child
!= NULL
; child
= child
->next
)
426 gtk_widget_show( GTK_WIDGET(child
->data
) );
431 //-----------------------------------------------------------------------------
433 //-----------------------------------------------------------------------------
435 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 wxListBox::wxListBox()
443 m_list
= (GtkList
*) NULL
;
444 #if wxUSE_CHECKLISTBOX
445 m_hasCheckBoxes
= FALSE
;
446 #endif // wxUSE_CHECKLISTBOX
449 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
450 const wxPoint
&pos
, const wxSize
&size
,
451 const wxArrayString
& choices
,
452 long style
, const wxValidator
& validator
,
453 const wxString
&name
)
455 wxCArrayString
chs(choices
);
457 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
458 style
, validator
, name
);
461 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
462 const wxPoint
&pos
, const wxSize
&size
,
463 int n
, const wxString choices
[],
464 long style
, const wxValidator
& validator
,
465 const wxString
&name
)
468 m_acceptsFocus
= TRUE
;
469 m_prevSelection
= 0; // or -1 ??
470 m_blockEvent
= FALSE
;
472 if (!PreCreation( parent
, pos
, size
) ||
473 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
475 wxFAIL_MSG( wxT("wxListBox creation failed") );
479 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
480 if (style
& wxLB_ALWAYS_SB
)
482 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
483 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
487 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
488 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
491 m_list
= GTK_LIST( gtk_list_new() );
493 GtkSelectionMode mode
;
494 if (style
& wxLB_MULTIPLE
)
496 mode
= GTK_SELECTION_MULTIPLE
;
498 else if (style
& wxLB_EXTENDED
)
500 mode
= GTK_SELECTION_EXTENDED
;
504 // if style was 0 set single mode
505 m_windowStyle
|= wxLB_SINGLE
;
506 mode
= GTK_SELECTION_SINGLE
;
509 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
511 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
513 /* make list scroll when moving the focus down using cursor keys */
514 gtk_container_set_focus_vadjustment(
515 GTK_CONTAINER(m_list
),
516 gtk_scrolled_window_get_vadjustment(
517 GTK_SCROLLED_WINDOW(m_widget
)));
519 gtk_widget_show( GTK_WIDGET(m_list
) );
521 gtk_signal_connect( GTK_OBJECT(m_list
), "realize",
522 GTK_SIGNAL_FUNC(gtk_listbox_realized_callback
), (gpointer
) this );
524 if ( style
& wxLB_SORT
)
526 // this will change DoAppend() behaviour
527 m_strings
= new wxSortedArrayString
;
531 m_strings
= (wxSortedArrayString
*)NULL
;
534 for (int i
= 0; i
< n
; i
++)
537 DoAppend(choices
[i
]);
540 m_parent
->DoAddChild( this );
543 SetBestSize(size
); // need this too because this is a wxControlWithItems
548 wxListBox::~wxListBox()
558 // ----------------------------------------------------------------------------
560 // ----------------------------------------------------------------------------
562 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
564 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
566 // VZ: notice that InsertItems knows nothing about sorting, so calling it
567 // from outside (and not from our own Append) is likely to break
570 // code elsewhere supposes we have as many items in m_clientList as items
572 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
573 wxT("bug in client data management") );
575 InvalidateBestSize();
577 GList
*children
= m_list
->children
;
578 int length
= g_list_length(children
);
580 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
582 size_t nItems
= items
.GetCount();
587 for (size_t n
= 0; n
< nItems
; n
++)
589 index
= m_strings
->Add( items
[n
] );
591 if (index
!= GetCount())
593 GtkAddItem( items
[n
], index
);
594 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
595 m_clientList
.Insert( node
, (wxObject
*) NULL
);
599 GtkAddItem( items
[n
] );
600 m_clientList
.Append( (wxObject
*) NULL
);
608 for ( size_t n
= 0; n
< nItems
; n
++ )
610 GtkAddItem( items
[n
] );
612 m_clientList
.Append((wxObject
*)NULL
);
617 wxList::compatibility_iterator node
= m_clientList
.Item( pos
);
618 for ( size_t n
= 0; n
< nItems
; n
++ )
620 GtkAddItem( items
[n
], pos
+n
);
622 m_clientList
.Insert( node
, (wxObject
*)NULL
);
627 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
628 wxT("bug in client data management") );
631 int wxListBox::DoAppend( const wxString
& item
)
633 InvalidateBestSize();
637 // need to determine the index
638 int index
= m_strings
->Add( item
);
640 // only if not at the end anyway
641 if (index
!= GetCount())
643 GtkAddItem( item
, index
);
645 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
646 m_clientList
.Insert( node
, (wxObject
*)NULL
);
654 m_clientList
.Append((wxObject
*)NULL
);
656 return GetCount() - 1;
659 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
661 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
663 GtkWidget
*list_item
;
665 wxString
label(item
);
666 #if wxUSE_CHECKLISTBOX
669 label
.Prepend(wxCHECKLBOX_STRING
);
671 #endif // wxUSE_CHECKLISTBOX
673 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
675 GList
*gitem_list
= g_list_alloc ();
676 gitem_list
->data
= list_item
;
679 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
681 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
683 gtk_signal_connect_after( GTK_OBJECT(list_item
), "select",
684 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
686 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
687 gtk_signal_connect_after( GTK_OBJECT(list_item
), "deselect",
688 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
690 gtk_signal_connect( GTK_OBJECT(list_item
),
691 "button_press_event",
692 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
695 gtk_signal_connect_after( GTK_OBJECT(list_item
),
696 "button_release_event",
697 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
700 gtk_signal_connect( GTK_OBJECT(list_item
),
702 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
706 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_in_event",
707 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback
), (gpointer
)this );
709 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_out_event",
710 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback
), (gpointer
)this );
712 ConnectWidget( list_item
);
714 if (GTK_WIDGET_REALIZED(m_widget
))
716 gtk_widget_show( list_item
);
718 gtk_widget_realize( list_item
);
719 gtk_widget_realize( GTK_BIN(list_item
)->child
);
722 if (m_tooltip
) m_tooltip
->Apply( this );
726 // Apply current widget style to the new list_item
727 GtkRcStyle
*style
= CreateWidgetStyle();
730 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
731 GtkBin
*bin
= GTK_BIN( list_item
);
732 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
733 gtk_widget_modify_style( label
, style
);
734 gtk_rc_style_unref( style
);
738 void wxListBox::DoSetItems( const wxArrayString
& items
,
743 DoInsertItems(items
, 0);
747 size_t count
= items
.GetCount();
748 for ( size_t n
= 0; n
< count
; n
++ )
750 SetClientData(n
, clientData
[n
]);
755 // ----------------------------------------------------------------------------
757 // ----------------------------------------------------------------------------
759 void wxListBox::Clear()
761 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
763 gtk_list_clear_items( m_list
, 0, GetCount() );
765 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
767 // This should be NULL, I think.
768 GTK_LIST(m_list
)->last_focus_child
= NULL
;
771 if ( HasClientObjectData() )
773 // destroy the data (due to Robert's idea of using wxList<wxObject>
774 // and not wxList<wxClientData> we can't just say
775 // m_clientList.DeleteContents(TRUE) - this would crash!
776 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
779 delete (wxClientData
*)node
->GetData();
780 node
= node
->GetNext();
783 m_clientList
.Clear();
789 void wxListBox::Delete( int n
)
791 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
793 GList
*child
= g_list_nth( m_list
->children
, n
);
795 wxCHECK_RET( child
, wxT("wrong listbox index") );
797 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
798 gtk_list_remove_items( m_list
, list
);
801 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
804 if ( m_clientDataItemsType
== wxClientData_Object
)
806 wxClientData
*cd
= (wxClientData
*)node
->GetData();
810 m_clientList
.Erase( node
);
814 m_strings
->RemoveAt(n
);
817 // ----------------------------------------------------------------------------
819 // ----------------------------------------------------------------------------
821 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
823 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
825 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
826 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
828 node
->SetData( (wxObject
*) clientData
);
831 void* wxListBox::DoGetItemClientData( int n
) const
833 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
835 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
836 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
838 return node
->GetData();
841 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
843 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
845 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
846 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
848 // wxItemContainer already deletes data for us
850 node
->SetData( (wxObject
*) clientData
);
853 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
855 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
857 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
858 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
859 wxT("invalid index in wxListBox::DoGetItemClientObject") );
861 return (wxClientData
*) node
->GetData();
864 // ----------------------------------------------------------------------------
865 // string list access
866 // ----------------------------------------------------------------------------
868 wxString
wxListBox::GetRealLabel(GList
*item
) const
870 GtkBin
*bin
= GTK_BIN( item
->data
);
871 GtkLabel
*label
= GTK_LABEL( bin
->child
);
876 str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
878 str
= wxString( label
->label
);
881 #if wxUSE_CHECKLISTBOX
882 // checklistboxes have "[±] " prepended to their lables, remove it
884 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
885 if ( m_hasCheckBoxes
)
887 #endif // wxUSE_CHECKLISTBOX
892 void wxListBox::SetString( int n
, const wxString
&string
)
894 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
896 GList
*child
= g_list_nth( m_list
->children
, n
);
899 GtkBin
*bin
= GTK_BIN( child
->data
);
900 GtkLabel
*label
= GTK_LABEL( bin
->child
);
903 #if wxUSE_CHECKLISTBOX
905 str
+= wxCHECKLBOX_STRING
;
906 #endif // wxUSE_CHECKLISTBOX
909 gtk_label_set( label
, wxGTK_CONV( str
) );
913 wxFAIL_MSG(wxT("wrong listbox index"));
917 wxString
wxListBox::GetString( int n
) const
919 wxCHECK_MSG( m_list
!= NULL
, wxT(""), wxT("invalid listbox") );
921 GList
*child
= g_list_nth( m_list
->children
, n
);
924 return GetRealLabel(child
);
927 wxFAIL_MSG(wxT("wrong listbox index"));
932 int wxListBox::GetCount() const
934 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
936 GList
*children
= m_list
->children
;
937 return g_list_length(children
);
940 int wxListBox::FindString( const wxString
&item
) const
942 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
944 GList
*child
= m_list
->children
;
948 if ( GetRealLabel(child
) == item
)
955 // it's not an error if the string is not found -> no wxCHECK
960 // ----------------------------------------------------------------------------
962 // ----------------------------------------------------------------------------
964 int wxListBox::GetSelection() const
966 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
968 GList
*child
= m_list
->children
;
972 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
979 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
981 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
983 // get the number of selected items first
984 GList
*child
= m_list
->children
;
986 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
988 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
997 aSelections
.Alloc(count
); // optimization attempt
999 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
1001 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
1009 bool wxListBox::IsSelected( int n
) const
1011 wxCHECK_MSG( m_list
!= NULL
, FALSE
, wxT("invalid listbox") );
1013 GList
*target
= g_list_nth( m_list
->children
, n
);
1015 wxCHECK_MSG( target
, FALSE
, wxT("invalid listbox index") );
1017 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
1020 void wxListBox::DoSetSelection( int n
, bool select
)
1022 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
1024 m_blockEvent
= TRUE
;
1028 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
1029 gtk_list_unselect_item( m_list
, m_prevSelection
);
1030 gtk_list_select_item( m_list
, n
);
1031 m_prevSelection
= n
;
1034 gtk_list_unselect_item( m_list
, n
);
1036 m_blockEvent
= FALSE
;
1039 void wxListBox::DoSetFirstItem( int n
)
1041 wxCHECK_RET( m_list
, wxT("invalid listbox") );
1043 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
1046 // terribly efficient
1047 const gchar
*vadjustment_key
= "gtk-vadjustment";
1048 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
1050 GtkAdjustment
*adjustment
=
1051 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
1052 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
1054 GList
*target
= g_list_nth( m_list
->children
, n
);
1055 wxCHECK_RET( target
, wxT("invalid listbox index") );
1057 GtkWidget
*item
= GTK_WIDGET(target
->data
);
1058 wxCHECK_RET( item
, wxT("invalid listbox code") );
1060 if (item
->allocation
.y
== -1)
1062 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
1063 data
->m_listbox
= this;
1065 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
1070 float y
= item
->allocation
.y
;
1071 if (y
> adjustment
->upper
- adjustment
->page_size
)
1072 y
= adjustment
->upper
- adjustment
->page_size
;
1073 gtk_adjustment_set_value( adjustment
, y
);
1076 // ----------------------------------------------------------------------------
1078 // ----------------------------------------------------------------------------
1080 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
1084 GList
*child
= m_list
->children
;
1088 if (GTK_WIDGET(child
->data
) == item
) return count
;
1090 child
= child
->next
;
1097 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
1099 GList
*child
= m_list
->children
;
1102 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
1103 child
= child
->next
;
1106 #endif // wxUSE_TOOLTIPS
1108 GtkWidget
*wxListBox::GetConnectWidget()
1110 // return GTK_WIDGET(m_list);
1114 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
1119 if (m_widget
->window
== window
) return TRUE
;
1121 if (GTK_WIDGET(m_list
)->window
== window
) return TRUE
;
1123 GList
*child
= m_list
->children
;
1126 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
1127 if (bin
->window
== window
) return TRUE
;
1128 child
= child
->next
;
1135 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1137 if (m_hasBgCol
&& m_backgroundColour
.Ok())
1139 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
1142 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1143 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1144 gdk_window_clear( window
);
1148 GList
*child
= m_list
->children
;
1151 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1153 GtkBin
*bin
= GTK_BIN( child
->data
);
1154 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1155 gtk_widget_modify_style( label
, style
);
1157 child
= child
->next
;
1161 void wxListBox::OnInternalIdle()
1163 wxCursor cursor
= m_cursor
;
1164 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1166 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1168 /* I now set the cursor the anew in every OnInternalIdle call
1169 as setting the cursor in a parent window also effects the
1170 windows above so that checking for the current cursor is
1173 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1175 GList
*child
= m_list
->children
;
1178 GtkBin
*bin
= GTK_BIN( child
->data
);
1179 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1184 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1186 child
= child
->next
;
1190 if (g_delayedFocus
== this)
1192 if (GTK_WIDGET_REALIZED(m_widget
))
1194 gtk_widget_grab_focus( m_widget
);
1195 g_delayedFocus
= NULL
;
1199 if (wxUpdateUIEvent::CanUpdate(this))
1200 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1203 wxSize
wxListBox::DoGetBestSize() const
1205 int lbWidth
= 100; // some defaults
1209 // Find the widest line
1210 for(int i
= 0; i
< GetCount(); i
++) {
1211 wxString
str(GetString(i
));
1212 GetTextExtent(str
, &wLine
, NULL
);
1213 lbWidth
= wxMax(lbWidth
, wLine
);
1216 // Add room for the scrollbar
1217 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1219 // And just a bit more
1221 GetTextExtent( wxT("X"), &cx
, &cy
);
1224 // don't make the listbox too tall (limit height to around 10 items) but don't
1225 // make it too small neither
1226 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1228 wxSize
best(lbWidth
, lbHeight
);
1229 CacheBestSize(best
);
1233 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1235 // the mouse event coords are relative to the listbox items, we need to
1236 // translate them to the normal client coords
1237 x
+= widget
->allocation
.x
;
1238 y
+= widget
->allocation
.y
;
1244 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1246 return GetDefaultAttributesFromGTKWidget(gtk_list_new
, true);
1249 #endif // wxUSE_LISTBOX