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
) );
197 event
.m_commandInt
= 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 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
237 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
242 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
247 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
250 wxapp_install_idle_handler();
252 if (g_blockEventsOnDrag
)
257 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
259 wxNavigationKeyEvent new_event
;
260 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
261 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
262 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
263 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
264 new_event
.SetCurrentFocus( listbox
);
265 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
268 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
270 // eat return in all modes
274 #if wxUSE_CHECKLISTBOX
275 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
277 int sel
= listbox
->GtkGetIndex( widget
);
279 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
281 clb
->Check( sel
, !clb
->IsChecked(sel
) );
283 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
284 new_event
.SetEventObject( listbox
);
285 new_event
.SetInt( sel
);
286 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
288 #endif // wxUSE_CHECKLISTBOX
290 // Check or uncheck item with SPACE
291 if ((gdk_event
->keyval
== ' ') && (!ret
) &&
292 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
293 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
295 int sel
= listbox
->GtkGetIndex( widget
);
301 if (listbox
->IsSelected( sel
))
302 gtk_list_unselect_item( listbox
->m_list
, sel
);
304 gtk_list_select_item( listbox
->m_list
, sel
);
306 wxCommandEvent
new_event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
307 new_event
.SetEventObject( listbox
);
308 wxArrayInt aSelections
;
309 int n
, count
= listbox
->GetSelections(aSelections
);
313 if ( listbox
->HasClientObjectData() )
314 new_event
.SetClientObject( listbox
->GetClientObject(n
) );
315 else if ( listbox
->HasClientUntypedData() )
316 new_event
.SetClientData( listbox
->GetClientData(n
) );
317 new_event
.SetString( listbox
->GetString(n
) );
323 new_event
.m_commandInt
= n
;
324 listbox
->GetEventHandler()->ProcessEvent( new_event
);
330 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
337 //-----------------------------------------------------------------------------
338 // "select" and "deselect"
339 //-----------------------------------------------------------------------------
341 static void gtk_listitem_select_cb( GtkWidget
*widget
, wxListBox
*listbox
, bool is_selection
);
343 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
345 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
348 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
350 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
353 static void gtk_listitem_select_cb( GtkWidget
*widget
,
357 if (g_isIdle
) wxapp_install_idle_handler();
359 if (!listbox
->m_hasVMT
) return;
360 if (g_blockEventsOnDrag
) return;
362 if (listbox
->m_blockEvent
) return;
364 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
365 event
.SetEventObject( listbox
);
367 // indicate whether this is a selection or a deselection
368 event
.SetExtraLong( is_selection
);
370 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
372 int sel
= listbox
->GtkGetIndex( widget
);
374 if (listbox
->m_prevSelection
!= sel
)
375 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
377 listbox
->m_prevSelection
= sel
;
380 wxArrayInt aSelections
;
381 int n
, count
= listbox
->GetSelections(aSelections
);
385 if ( listbox
->HasClientObjectData() )
386 event
.SetClientObject( listbox
->GetClientObject(n
) );
387 else if ( listbox
->HasClientUntypedData() )
388 event
.SetClientData( listbox
->GetClientData(n
) );
389 event
.SetString( listbox
->GetString(n
) );
396 event
.m_commandInt
= n
;
398 // No longer required with new code in wxLB_SINGLE
399 // listbox->GetEventHandler()->AddPendingEvent( event );
400 listbox
->GetEventHandler()->ProcessEvent( event
);
403 //-----------------------------------------------------------------------------
405 //-----------------------------------------------------------------------------
407 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
413 wxListBox::wxListBox()
415 m_list
= (GtkList
*) NULL
;
416 #if wxUSE_CHECKLISTBOX
417 m_hasCheckBoxes
= FALSE
;
418 #endif // wxUSE_CHECKLISTBOX
421 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
422 const wxPoint
&pos
, const wxSize
&size
,
423 const wxArrayString
& choices
,
424 long style
, const wxValidator
& validator
,
425 const wxString
&name
)
427 wxCArrayString
chs(choices
);
429 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
430 style
, validator
, name
);
433 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
434 const wxPoint
&pos
, const wxSize
&size
,
435 int n
, const wxString choices
[],
436 long style
, const wxValidator
& validator
,
437 const wxString
&name
)
440 m_acceptsFocus
= TRUE
;
441 m_prevSelection
= 0; // or -1 ??
442 m_blockEvent
= FALSE
;
444 if (!PreCreation( parent
, pos
, size
) ||
445 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
447 wxFAIL_MSG( wxT("wxListBox creation failed") );
451 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
452 if (style
& wxLB_ALWAYS_SB
)
454 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
455 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
459 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
460 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
463 m_list
= GTK_LIST( gtk_list_new() );
465 GtkSelectionMode mode
;
466 if (style
& wxLB_MULTIPLE
)
468 mode
= GTK_SELECTION_MULTIPLE
;
470 else if (style
& wxLB_EXTENDED
)
472 mode
= GTK_SELECTION_EXTENDED
;
476 // if style was 0 set single mode
477 m_windowStyle
|= wxLB_SINGLE
;
478 mode
= GTK_SELECTION_MULTIPLE
;
481 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
483 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
485 /* make list scroll when moving the focus down using cursor keys */
486 gtk_container_set_focus_vadjustment(
487 GTK_CONTAINER(m_list
),
488 gtk_scrolled_window_get_vadjustment(
489 GTK_SCROLLED_WINDOW(m_widget
)));
491 gtk_widget_show( GTK_WIDGET(m_list
) );
493 if ( style
& wxLB_SORT
)
495 // this will change DoAppend() behaviour
496 m_strings
= new wxSortedArrayString
;
500 m_strings
= (wxSortedArrayString
*)NULL
;
503 for (int i
= 0; i
< n
; i
++)
506 DoAppend(choices
[i
]);
509 m_parent
->DoAddChild( this );
512 SetBestSize(size
); // need this too because this is a wxControlWithItems
517 wxListBox::~wxListBox()
527 // ----------------------------------------------------------------------------
529 // ----------------------------------------------------------------------------
531 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
533 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
535 // VZ: notice that InsertItems knows nothing about sorting, so calling it
536 // from outside (and not from our own Append) is likely to break
539 // code elsewhere supposes we have as many items in m_clientList as items
541 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
542 wxT("bug in client data management") );
544 InvalidateBestSize();
546 GList
*children
= m_list
->children
;
547 int length
= g_list_length(children
);
549 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
551 size_t nItems
= items
.GetCount();
556 for (size_t n
= 0; n
< nItems
; n
++)
558 index
= m_strings
->Add( items
[n
] );
560 if (index
!= GetCount())
562 GtkAddItem( items
[n
], index
);
563 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
564 m_clientList
.Insert( node
, (wxObject
*) NULL
);
568 GtkAddItem( items
[n
] );
569 m_clientList
.Append( (wxObject
*) NULL
);
577 for ( size_t n
= 0; n
< nItems
; n
++ )
579 GtkAddItem( items
[n
] );
581 m_clientList
.Append((wxObject
*)NULL
);
586 wxList::compatibility_iterator node
= m_clientList
.Item( pos
);
587 for ( size_t n
= 0; n
< nItems
; n
++ )
589 GtkAddItem( items
[n
], pos
+n
);
591 m_clientList
.Insert( node
, (wxObject
*)NULL
);
596 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
597 wxT("bug in client data management") );
600 int wxListBox::DoAppend( const wxString
& item
)
602 InvalidateBestSize();
606 // need to determine the index
607 int index
= m_strings
->Add( item
);
609 // only if not at the end anyway
610 if (index
!= GetCount())
612 GtkAddItem( item
, index
);
614 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
615 m_clientList
.Insert( node
, (wxObject
*)NULL
);
623 m_clientList
.Append((wxObject
*)NULL
);
625 return GetCount() - 1;
628 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
630 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
632 GtkWidget
*list_item
;
634 wxString
label(item
);
635 #if wxUSE_CHECKLISTBOX
638 label
.Prepend(wxCHECKLBOX_STRING
);
640 #endif // wxUSE_CHECKLISTBOX
642 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
644 GList
*gitem_list
= g_list_alloc ();
645 gitem_list
->data
= list_item
;
648 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
650 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
652 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
653 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
655 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
656 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
657 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
659 gtk_signal_connect( GTK_OBJECT(list_item
),
660 "button_press_event",
661 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
664 gtk_signal_connect_after( GTK_OBJECT(list_item
),
665 "button_release_event",
666 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
669 gtk_signal_connect( GTK_OBJECT(list_item
),
671 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
675 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_in_event",
676 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback
), (gpointer
)this );
678 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_out_event",
679 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback
), (gpointer
)this );
682 ConnectWidget( list_item
);
684 gtk_widget_show( list_item
);
686 if (GTK_WIDGET_REALIZED(m_widget
))
688 gtk_widget_realize( list_item
);
689 gtk_widget_realize( GTK_BIN(list_item
)->child
);
692 if (m_tooltip
) m_tooltip
->Apply( this );
696 // Apply current widget style to the new list_item
697 GtkRcStyle
*style
= CreateWidgetStyle();
700 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
701 GtkBin
*bin
= GTK_BIN( list_item
);
702 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
703 gtk_widget_modify_style( label
, style
);
704 gtk_rc_style_unref( style
);
708 void wxListBox::DoSetItems( const wxArrayString
& items
,
713 DoInsertItems(items
, 0);
717 size_t count
= items
.GetCount();
718 for ( size_t n
= 0; n
< count
; n
++ )
720 SetClientData(n
, clientData
[n
]);
725 // ----------------------------------------------------------------------------
727 // ----------------------------------------------------------------------------
729 void wxListBox::Clear()
731 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
733 gtk_list_clear_items( m_list
, 0, GetCount() );
735 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
737 // This should be NULL, I think.
738 GTK_LIST(m_list
)->last_focus_child
= NULL
;
741 if ( HasClientObjectData() )
743 // destroy the data (due to Robert's idea of using wxList<wxObject>
744 // and not wxList<wxClientData> we can't just say
745 // m_clientList.DeleteContents(TRUE) - this would crash!
746 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
749 delete (wxClientData
*)node
->GetData();
750 node
= node
->GetNext();
753 m_clientList
.Clear();
759 void wxListBox::Delete( int n
)
761 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
763 GList
*child
= g_list_nth( m_list
->children
, n
);
765 wxCHECK_RET( child
, wxT("wrong listbox index") );
767 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
768 gtk_list_remove_items( m_list
, list
);
771 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
774 if ( m_clientDataItemsType
== wxClientData_Object
)
776 wxClientData
*cd
= (wxClientData
*)node
->GetData();
780 m_clientList
.Erase( node
);
784 m_strings
->RemoveAt(n
);
787 // ----------------------------------------------------------------------------
789 // ----------------------------------------------------------------------------
791 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
793 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
795 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
796 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
798 node
->SetData( (wxObject
*) clientData
);
801 void* wxListBox::DoGetItemClientData( int n
) const
803 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
805 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
806 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
808 return node
->GetData();
811 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
813 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
815 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
816 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
818 // wxItemContainer already deletes data for us
820 node
->SetData( (wxObject
*) clientData
);
823 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
825 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
827 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
828 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
829 wxT("invalid index in wxListBox::DoGetItemClientObject") );
831 return (wxClientData
*) node
->GetData();
834 // ----------------------------------------------------------------------------
835 // string list access
836 // ----------------------------------------------------------------------------
838 wxString
wxListBox::GetRealLabel(GList
*item
) const
840 GtkBin
*bin
= GTK_BIN( item
->data
);
841 GtkLabel
*label
= GTK_LABEL( bin
->child
);
846 str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
848 str
= wxString( label
->label
);
851 #if wxUSE_CHECKLISTBOX
852 // checklistboxes have "[±] " prepended to their lables, remove it
854 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
855 if ( m_hasCheckBoxes
)
857 #endif // wxUSE_CHECKLISTBOX
862 void wxListBox::SetString( int n
, const wxString
&string
)
864 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
866 GList
*child
= g_list_nth( m_list
->children
, n
);
869 GtkBin
*bin
= GTK_BIN( child
->data
);
870 GtkLabel
*label
= GTK_LABEL( bin
->child
);
873 #if wxUSE_CHECKLISTBOX
875 str
+= wxCHECKLBOX_STRING
;
876 #endif // wxUSE_CHECKLISTBOX
879 gtk_label_set( label
, wxGTK_CONV( str
) );
883 wxFAIL_MSG(wxT("wrong listbox index"));
887 wxString
wxListBox::GetString( int n
) const
889 wxCHECK_MSG( m_list
!= NULL
, wxT(""), wxT("invalid listbox") );
891 GList
*child
= g_list_nth( m_list
->children
, n
);
894 return GetRealLabel(child
);
897 wxFAIL_MSG(wxT("wrong listbox index"));
902 int wxListBox::GetCount() const
904 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
906 GList
*children
= m_list
->children
;
907 return g_list_length(children
);
910 int wxListBox::FindString( const wxString
&item
) const
912 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
914 GList
*child
= m_list
->children
;
918 if ( GetRealLabel(child
) == item
)
925 // it's not an error if the string is not found -> no wxCHECK
930 // ----------------------------------------------------------------------------
932 // ----------------------------------------------------------------------------
934 int wxListBox::GetSelection() const
936 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
938 GList
*child
= m_list
->children
;
942 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
949 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
951 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
953 // get the number of selected items first
954 GList
*child
= m_list
->children
;
956 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
958 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
967 aSelections
.Alloc(count
); // optimization attempt
969 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
971 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
979 bool wxListBox::IsSelected( int n
) const
981 wxCHECK_MSG( m_list
!= NULL
, FALSE
, wxT("invalid listbox") );
983 GList
*target
= g_list_nth( m_list
->children
, n
);
985 wxCHECK_MSG( target
, FALSE
, wxT("invalid listbox index") );
987 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
990 void wxListBox::SetSelection( int n
, bool select
)
992 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
998 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
999 gtk_list_unselect_item( m_list
, m_prevSelection
);
1000 gtk_list_select_item( m_list
, n
);
1001 m_prevSelection
= n
;
1004 gtk_list_unselect_item( m_list
, n
);
1006 m_blockEvent
= FALSE
;
1009 void wxListBox::DoSetFirstItem( int n
)
1011 wxCHECK_RET( m_list
, wxT("invalid listbox") );
1013 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
1016 // terribly efficient
1017 const gchar
*vadjustment_key
= "gtk-vadjustment";
1018 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
1020 GtkAdjustment
*adjustment
=
1021 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
1022 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
1024 GList
*target
= g_list_nth( m_list
->children
, n
);
1025 wxCHECK_RET( target
, wxT("invalid listbox index") );
1027 GtkWidget
*item
= GTK_WIDGET(target
->data
);
1028 wxCHECK_RET( item
, wxT("invalid listbox code") );
1030 if (item
->allocation
.y
== -1)
1032 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
1033 data
->m_listbox
= this;
1035 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
1040 float y
= item
->allocation
.y
;
1041 if (y
> adjustment
->upper
- adjustment
->page_size
)
1042 y
= adjustment
->upper
- adjustment
->page_size
;
1043 gtk_adjustment_set_value( adjustment
, y
);
1046 // ----------------------------------------------------------------------------
1048 // ----------------------------------------------------------------------------
1050 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
1054 GList
*child
= m_list
->children
;
1058 if (GTK_WIDGET(child
->data
) == item
) return count
;
1060 child
= child
->next
;
1067 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
1069 GList
*child
= m_list
->children
;
1072 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
1073 child
= child
->next
;
1076 #endif // wxUSE_TOOLTIPS
1078 GtkWidget
*wxListBox::GetConnectWidget()
1080 // return GTK_WIDGET(m_list);
1084 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
1089 if (m_widget
->window
== window
) return TRUE
;
1091 if (GTK_WIDGET(m_list
)->window
== window
) return TRUE
;
1093 GList
*child
= m_list
->children
;
1096 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
1097 if (bin
->window
== window
) return TRUE
;
1098 child
= child
->next
;
1105 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1107 if (m_hasBgCol
&& m_backgroundColour
.Ok())
1109 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
1112 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1113 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1114 gdk_window_clear( window
);
1118 GList
*child
= m_list
->children
;
1121 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1123 GtkBin
*bin
= GTK_BIN( child
->data
);
1124 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1125 gtk_widget_modify_style( label
, style
);
1127 child
= child
->next
;
1131 void wxListBox::OnInternalIdle()
1133 wxCursor cursor
= m_cursor
;
1134 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1136 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1138 /* I now set the cursor the anew in every OnInternalIdle call
1139 as setting the cursor in a parent window also effects the
1140 windows above so that checking for the current cursor is
1143 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1145 GList
*child
= m_list
->children
;
1148 GtkBin
*bin
= GTK_BIN( child
->data
);
1149 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1154 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1156 child
= child
->next
;
1160 if (g_delayedFocus
== this)
1162 if (GTK_WIDGET_REALIZED(m_widget
))
1164 gtk_widget_grab_focus( m_widget
);
1165 g_delayedFocus
= NULL
;
1169 if (wxUpdateUIEvent::CanUpdate(this))
1170 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1173 wxSize
wxListBox::DoGetBestSize() const
1175 int lbWidth
= 100; // some defaults
1179 // Find the widest line
1180 for(int i
= 0; i
< GetCount(); i
++) {
1181 wxString
str(GetString(i
));
1182 GetTextExtent(str
, &wLine
, NULL
);
1183 lbWidth
= wxMax(lbWidth
, wLine
);
1186 // Add room for the scrollbar
1187 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1189 // And just a bit more
1191 GetTextExtent( wxT("X"), &cx
, &cy
);
1194 // don't make the listbox too tall (limit height to around 10 items) but don't
1195 // make it too small neither
1196 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1198 wxSize
best(lbWidth
, lbHeight
);
1199 CacheBestSize(best
);
1203 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1205 // the mouse event coords are relative to the listbox items, we need to
1206 // translate them to the normal client coords
1207 x
+= widget
->allocation
.x
;
1208 y
+= widget
->allocation
.y
;
1214 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1216 return GetDefaultAttributesFromGTKWidget(gtk_list_new
, true);
1219 #endif // wxUSE_LISTBOX