1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/listbox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/listbox.h"
18 #include "wx/dynarray.h"
21 #include "wx/settings.h"
22 #include "wx/checklst.h"
25 #include "wx/arrstr.h"
26 #include "wx/gtk1/private.h"
29 #include "wx/tooltip.h"
34 #include <gdk/gdkkeysyms.h>
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern void wxapp_install_idle_handler();
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 extern bool g_blockEventsOnDrag
;
48 extern bool g_blockEventsOnScroll
;
49 extern wxCursor g_globalCursor
;
50 extern wxWindowGTK
*g_delayedFocus
;
51 extern wxWindowGTK
*g_focusWindow
;
52 extern wxWindowGTK
*g_focusWindowLast
;
54 static bool g_hasDoubleClicked
= false;
56 //-----------------------------------------------------------------------------
57 // idle callback for SetFirstItem
58 //-----------------------------------------------------------------------------
60 struct wxlistbox_idle_struct
68 static gint
wxlistbox_idle_callback( gpointer gdata
)
70 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
73 gtk_idle_remove( data
->m_tag
);
75 // check that the items haven't been deleted from the listbox since we had
76 // installed this callback
77 wxListBox
*lbox
= data
->m_listbox
;
78 if ( data
->m_item
< (int)lbox
->GetCount() )
80 lbox
->SetFirstItem( data
->m_item
);
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
96 static gint
gtk_listitem_focus_in_callback( GtkWidget
*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
);
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
130 static gint
gtk_listitem_focus_out_callback( GtkWidget
*widget
, GdkEventFocus
*gdk_event
, wxWindowGTK
*win
)
133 wxapp_install_idle_handler();
135 g_focusWindow
= (wxWindowGTK
*)NULL
;
137 // don't send the window a kill focus event if it thinks that it doesn't
138 // have focus already
139 if ( win
->m_hasFocus
)
141 win
->m_hasFocus
= false;
143 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
144 event
.SetEventObject( win
);
146 // even if we did process the event in wx code, still let GTK itself
147 // process it too as otherwise bad things happen, especially in GTK2
148 // where the text control simply aborts the program if it doesn't get
149 // the matching focus out event
150 (void)win
->GetEventHandler()->ProcessEvent( event
);
157 //-----------------------------------------------------------------------------
158 // "button_release_event"
159 //-----------------------------------------------------------------------------
161 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
162 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
163 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
164 this can lead to race conditions so that we emit the dclick event
165 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
169 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
170 GdkEventButton
* WXUNUSED(gdk_event
),
173 if (g_isIdle
) wxapp_install_idle_handler();
175 if (g_blockEventsOnDrag
) return FALSE
;
176 if (g_blockEventsOnScroll
) return FALSE
;
178 if (!listbox
->m_hasVMT
) return FALSE
;
180 if (!g_hasDoubleClicked
) return FALSE
;
182 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
183 event
.SetEventObject( listbox
);
185 wxArrayInt aSelections
;
186 int n
, count
= listbox
->GetSelections(aSelections
);
190 if ( listbox
->HasClientObjectData() )
191 event
.SetClientObject( listbox
->GetClientObject(n
) );
192 else if ( listbox
->HasClientUntypedData() )
193 event
.SetClientData( listbox
->GetClientData(n
) );
194 event
.SetString( listbox
->GetString(n
) );
203 listbox
->GetEventHandler()->ProcessEvent( event
);
209 //-----------------------------------------------------------------------------
210 // "button_press_event"
211 //-----------------------------------------------------------------------------
215 gtk_listbox_button_press_callback( GtkWidget
*widget
,
216 GdkEventButton
*gdk_event
,
219 if (g_isIdle
) wxapp_install_idle_handler();
221 if (g_blockEventsOnDrag
) return FALSE
;
222 if (g_blockEventsOnScroll
) return FALSE
;
224 if (!listbox
->m_hasVMT
) return FALSE
;
226 int sel
= listbox
->GtkGetIndex( widget
);
228 #if wxUSE_CHECKLISTBOX
229 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
231 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
233 clb
->Check( sel
, !clb
->IsChecked(sel
) );
235 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
236 event
.SetEventObject( listbox
);
238 listbox
->GetEventHandler()->ProcessEvent( event
);
240 #endif // wxUSE_CHECKLISTBOX
242 if ((gdk_event
->state
== 0) &&
243 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
244 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
246 listbox
->m_blockEvent
= true;
249 for (i
= 0; i
< (int)listbox
->GetCount(); i
++)
251 gtk_list_unselect_item( GTK_LIST(listbox
->m_list
), i
);
253 listbox
->m_blockEvent
= false;
258 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
259 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
265 //-----------------------------------------------------------------------------
267 //-----------------------------------------------------------------------------
271 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
274 wxapp_install_idle_handler();
276 if (g_blockEventsOnDrag
)
281 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
283 wxNavigationKeyEvent new_event
;
284 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
285 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
286 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
287 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
288 new_event
.SetCurrentFocus( listbox
);
289 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
292 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
294 // eat return in all modes
298 #if wxUSE_CHECKLISTBOX
299 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
301 int sel
= listbox
->GtkGetIndex( widget
);
303 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
305 clb
->Check( sel
, !clb
->IsChecked(sel
) );
307 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
308 new_event
.SetEventObject( listbox
);
309 new_event
.SetInt( sel
);
310 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
312 #endif // wxUSE_CHECKLISTBOX
314 // Check or uncheck item with SPACE
315 if ((gdk_event
->keyval
== ' ') && (!ret
) &&
316 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
317 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
319 int sel
= listbox
->GtkGetIndex( widget
);
325 if (listbox
->IsSelected( sel
))
326 gtk_list_unselect_item( listbox
->m_list
, sel
);
328 gtk_list_select_item( listbox
->m_list
, sel
);
330 wxCommandEvent
new_event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
331 new_event
.SetEventObject( listbox
);
332 wxArrayInt aSelections
;
333 int n
, count
= listbox
->GetSelections(aSelections
);
337 if ( listbox
->HasClientObjectData() )
338 new_event
.SetClientObject( listbox
->GetClientObject(n
) );
339 else if ( listbox
->HasClientUntypedData() )
340 new_event
.SetClientData( listbox
->GetClientData(n
) );
341 new_event
.SetString( listbox
->GetString(n
) );
348 listbox
->GetEventHandler()->ProcessEvent( new_event
);
354 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
362 //-----------------------------------------------------------------------------
363 // "select" and "deselect"
364 //-----------------------------------------------------------------------------
366 static void gtk_listitem_select_cb( GtkWidget
*widget
,
370 if (g_isIdle
) wxapp_install_idle_handler();
372 if (!listbox
->m_hasVMT
) return;
373 if (g_blockEventsOnDrag
) return;
375 if (listbox
->m_blockEvent
) return;
377 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
378 event
.SetEventObject( listbox
);
380 // indicate whether this is a selection or a deselection
381 event
.SetExtraLong( is_selection
);
383 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
385 int sel
= listbox
->GtkGetIndex( widget
);
387 if (listbox
->m_prevSelection
!= sel
)
388 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
390 listbox
->m_prevSelection
= sel
;
393 wxArrayInt aSelections
;
394 int n
, count
= listbox
->GetSelections(aSelections
);
398 if ( listbox
->HasClientObjectData() )
399 event
.SetClientObject( listbox
->GetClientObject(n
) );
400 else if ( listbox
->HasClientUntypedData() )
401 event
.SetClientData( listbox
->GetClientData(n
) );
402 event
.SetString( listbox
->GetString(n
) );
411 // No longer required with new code in wxLB_SINGLE
412 // listbox->GetEventHandler()->AddPendingEvent( event );
413 listbox
->GetEventHandler()->ProcessEvent( event
);
417 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
419 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
424 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
426 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
430 //-----------------------------------------------------------------------------
432 //-----------------------------------------------------------------------------
436 gtk_listbox_realized_callback( GtkWidget
*m_widget
, wxListBox
*win
)
439 wxapp_install_idle_handler();
441 GList
*child
= win
->m_list
->children
;
442 for (child
= win
->m_list
->children
; child
!= NULL
; child
= child
->next
)
443 gtk_widget_show( GTK_WIDGET(child
->data
) );
449 //-----------------------------------------------------------------------------
451 //-----------------------------------------------------------------------------
453 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
459 wxListBox::wxListBox()
461 m_list
= (GtkList
*) NULL
;
462 #if wxUSE_CHECKLISTBOX
463 m_hasCheckBoxes
= false;
464 #endif // wxUSE_CHECKLISTBOX
467 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
468 const wxPoint
&pos
, const wxSize
&size
,
469 const wxArrayString
& choices
,
470 long style
, const wxValidator
& validator
,
471 const wxString
&name
)
473 wxCArrayString
chs(choices
);
475 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
476 style
, validator
, name
);
479 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
480 const wxPoint
&pos
, const wxSize
&size
,
481 int n
, const wxString choices
[],
482 long style
, const wxValidator
& validator
,
483 const wxString
&name
)
486 m_acceptsFocus
= true;
487 m_prevSelection
= 0; // or -1 ??
488 m_blockEvent
= false;
490 if (!PreCreation( parent
, pos
, size
) ||
491 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
493 wxFAIL_MSG( wxT("wxListBox creation failed") );
497 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
498 if (style
& wxLB_ALWAYS_SB
)
500 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
501 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
505 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
506 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
509 m_list
= GTK_LIST( gtk_list_new() );
511 GtkSelectionMode mode
;
512 if (style
& wxLB_MULTIPLE
)
514 mode
= GTK_SELECTION_MULTIPLE
;
516 else if (style
& wxLB_EXTENDED
)
518 mode
= GTK_SELECTION_EXTENDED
;
522 // if style was 0 set single mode
523 m_windowStyle
|= wxLB_SINGLE
;
524 mode
= GTK_SELECTION_SINGLE
;
527 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
529 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
531 /* make list scroll when moving the focus down using cursor keys */
532 gtk_container_set_focus_vadjustment(
533 GTK_CONTAINER(m_list
),
534 gtk_scrolled_window_get_vadjustment(
535 GTK_SCROLLED_WINDOW(m_widget
)));
537 gtk_widget_show( GTK_WIDGET(m_list
) );
539 gtk_signal_connect( GTK_OBJECT(m_list
), "realize",
540 GTK_SIGNAL_FUNC(gtk_listbox_realized_callback
), (gpointer
) this );
542 if ( style
& wxLB_SORT
)
544 // this will change DoAppend() behaviour
545 m_strings
= new wxSortedArrayString
;
549 m_strings
= (wxSortedArrayString
*)NULL
;
552 for (int i
= 0; i
< n
; i
++)
555 DoAppend(choices
[i
]);
558 m_parent
->DoAddChild( this );
561 SetBestSize(size
); // need this too because this is a wxControlWithItems
566 wxListBox::~wxListBox()
576 // ----------------------------------------------------------------------------
578 // ----------------------------------------------------------------------------
580 void wxListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
582 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
584 // VZ: notice that InsertItems knows nothing about sorting, so calling it
585 // from outside (and not from our own Append) is likely to break
588 // code elsewhere supposes we have as many items in m_clientList as items
590 wxASSERT_MSG( m_clientList
.GetCount() == GetCount(),
591 wxT("bug in client data management") );
593 InvalidateBestSize();
595 GList
*children
= m_list
->children
;
596 unsigned int length
= g_list_length(children
);
598 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
600 unsigned int nItems
= items
.GetCount();
605 for (unsigned int n
= 0; n
< nItems
; n
++)
607 index
= m_strings
->Add( items
[n
] );
609 if (index
!= (int)GetCount())
611 GtkAddItem( items
[n
], index
);
612 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
613 m_clientList
.Insert( node
, (wxObject
*) NULL
);
617 GtkAddItem( items
[n
] );
618 m_clientList
.Append( (wxObject
*) NULL
);
626 for ( unsigned int n
= 0; n
< nItems
; n
++ )
628 GtkAddItem( items
[n
] );
630 m_clientList
.Append((wxObject
*)NULL
);
635 wxList::compatibility_iterator node
= m_clientList
.Item( pos
);
636 for ( unsigned int n
= 0; n
< nItems
; n
++ )
638 GtkAddItem( items
[n
], pos
+n
);
640 m_clientList
.Insert( node
, (wxObject
*)NULL
);
645 wxASSERT_MSG( m_clientList
.GetCount() == GetCount(),
646 wxT("bug in client data management") );
649 int wxListBox::DoAppend( const wxString
& item
)
651 InvalidateBestSize();
655 // need to determine the index
656 int index
= m_strings
->Add( item
);
658 // only if not at the end anyway
659 if (index
!= (int)GetCount())
661 GtkAddItem( item
, index
);
663 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
664 m_clientList
.Insert( node
, (wxObject
*)NULL
);
672 m_clientList
.Append((wxObject
*)NULL
);
674 return GetCount() - 1;
677 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
679 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
681 GtkWidget
*list_item
;
683 wxString
label(item
);
684 #if wxUSE_CHECKLISTBOX
687 label
.Prepend(wxCHECKLBOX_STRING
);
689 #endif // wxUSE_CHECKLISTBOX
691 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
693 GList
*gitem_list
= g_list_alloc ();
694 gitem_list
->data
= list_item
;
697 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
699 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
701 gtk_signal_connect_after( GTK_OBJECT(list_item
), "select",
702 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
704 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
705 gtk_signal_connect_after( GTK_OBJECT(list_item
), "deselect",
706 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
708 gtk_signal_connect( GTK_OBJECT(list_item
),
709 "button_press_event",
710 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
713 gtk_signal_connect_after( GTK_OBJECT(list_item
),
714 "button_release_event",
715 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
718 gtk_signal_connect( GTK_OBJECT(list_item
),
720 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
724 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_in_event",
725 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback
), (gpointer
)this );
727 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_out_event",
728 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback
), (gpointer
)this );
730 ConnectWidget( list_item
);
732 if (GTK_WIDGET_REALIZED(m_widget
))
734 gtk_widget_show( list_item
);
736 gtk_widget_realize( list_item
);
737 gtk_widget_realize( GTK_BIN(list_item
)->child
);
740 if (m_tooltip
) m_tooltip
->Apply( this );
744 // Apply current widget style to the new list_item
745 GtkRcStyle
*style
= CreateWidgetStyle();
748 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
749 GtkBin
*bin
= GTK_BIN( list_item
);
750 gtk_widget_modify_style( GTK_WIDGET( bin
->child
), style
);
751 gtk_rc_style_unref( style
);
755 void wxListBox::DoSetItems( const wxArrayString
& items
,
760 DoInsertItems(items
, 0);
764 unsigned int count
= items
.GetCount();
765 for ( unsigned int n
= 0; n
< count
; n
++ )
767 SetClientData(n
, clientData
[n
]);
772 // ----------------------------------------------------------------------------
774 // ----------------------------------------------------------------------------
776 void wxListBox::Clear()
778 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
780 gtk_list_clear_items( m_list
, 0, (int)GetCount() );
782 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
784 // This should be NULL, I think.
785 GTK_LIST(m_list
)->last_focus_child
= NULL
;
788 if ( HasClientObjectData() )
790 // destroy the data (due to Robert's idea of using wxList<wxObject>
791 // and not wxList<wxClientData> we can't just say
792 // m_clientList.DeleteContents(true) - this would crash!
793 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
796 delete (wxClientData
*)node
->GetData();
797 node
= node
->GetNext();
800 m_clientList
.Clear();
806 void wxListBox::Delete(unsigned int n
)
808 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
810 GList
*child
= g_list_nth( m_list
->children
, n
);
812 wxCHECK_RET( child
, wxT("wrong listbox index") );
814 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
815 gtk_list_remove_items( m_list
, list
);
818 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
821 if ( m_clientDataItemsType
== wxClientData_Object
)
823 wxClientData
*cd
= (wxClientData
*)node
->GetData();
827 m_clientList
.Erase( node
);
831 m_strings
->RemoveAt(n
);
834 // ----------------------------------------------------------------------------
836 // ----------------------------------------------------------------------------
838 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
840 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
842 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
843 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
845 node
->SetData( (wxObject
*) clientData
);
848 void* wxListBox::DoGetItemClientData(unsigned int n
) const
850 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
852 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
853 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
855 return node
->GetData();
858 void wxListBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
860 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
862 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
863 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
865 // wxItemContainer already deletes data for us
867 node
->SetData( (wxObject
*) clientData
);
870 wxClientData
* wxListBox::DoGetItemClientObject(unsigned int n
) const
872 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
874 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
875 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
876 wxT("invalid index in wxListBox::DoGetItemClientObject") );
878 return (wxClientData
*) node
->GetData();
881 // ----------------------------------------------------------------------------
882 // string list access
883 // ----------------------------------------------------------------------------
885 wxString
wxListBox::GetRealLabel(GList
*item
) const
887 GtkBin
*bin
= GTK_BIN( item
->data
);
888 GtkLabel
*label
= GTK_LABEL( bin
->child
);
892 str
= wxString( label
->label
);
894 #if wxUSE_CHECKLISTBOX
895 // checklistboxes have "[±] " prepended to their lables, remove it
897 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk1/checklst.h
898 if ( m_hasCheckBoxes
)
900 #endif // wxUSE_CHECKLISTBOX
905 void wxListBox::SetString(unsigned int n
, const wxString
&string
)
907 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
909 GList
*child
= g_list_nth( m_list
->children
, n
);
912 GtkBin
*bin
= GTK_BIN( child
->data
);
913 GtkLabel
*label
= GTK_LABEL( bin
->child
);
916 #if wxUSE_CHECKLISTBOX
918 str
+= wxCHECKLBOX_STRING
;
919 #endif // wxUSE_CHECKLISTBOX
922 gtk_label_set( label
, wxGTK_CONV( str
) );
926 wxFAIL_MSG(wxT("wrong listbox index"));
930 wxString
wxListBox::GetString(unsigned int n
) const
932 wxCHECK_MSG( m_list
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
934 GList
*child
= g_list_nth( m_list
->children
, n
);
937 return GetRealLabel(child
);
940 wxFAIL_MSG(wxT("wrong listbox index"));
942 return wxEmptyString
;
945 unsigned int wxListBox::GetCount() const
947 wxCHECK_MSG( m_list
!= NULL
, 0, wxT("invalid listbox") );
949 GList
*children
= m_list
->children
;
950 return g_list_length(children
);
953 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
955 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
957 GList
*child
= m_list
->children
;
961 if ( item
.IsSameAs( GetRealLabel(child
), bCase
) )
968 // it's not an error if the string is not found -> no wxCHECK
973 // ----------------------------------------------------------------------------
975 // ----------------------------------------------------------------------------
977 int wxListBox::GetSelection() const
979 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
981 GList
*child
= m_list
->children
;
985 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
992 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
994 wxCHECK_MSG( m_list
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
996 // get the number of selected items first
997 GList
*child
= m_list
->children
;
999 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
1001 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
1005 aSelections
.Empty();
1009 // now fill the list
1010 aSelections
.Alloc(count
); // optimization attempt
1012 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
1014 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
1022 bool wxListBox::IsSelected( int n
) const
1024 wxCHECK_MSG( m_list
!= NULL
, false, wxT("invalid listbox") );
1026 GList
*target
= g_list_nth( m_list
->children
, n
);
1028 wxCHECK_MSG( target
, false, wxT("invalid listbox index") );
1030 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
1033 void wxListBox::DoSetSelection( int n
, bool select
)
1035 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
1037 m_blockEvent
= true;
1041 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
1042 gtk_list_unselect_item( m_list
, m_prevSelection
);
1043 gtk_list_select_item( m_list
, n
);
1044 m_prevSelection
= n
;
1047 gtk_list_unselect_item( m_list
, n
);
1049 m_blockEvent
= false;
1052 void wxListBox::DoSetFirstItem( int n
)
1054 wxCHECK_RET( m_list
, wxT("invalid listbox") );
1056 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
1059 // terribly efficient
1060 const gchar
*vadjustment_key
= "gtk-vadjustment";
1061 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
1063 GtkAdjustment
*adjustment
=
1064 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
1065 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
1067 GList
*target
= g_list_nth( m_list
->children
, n
);
1068 wxCHECK_RET( target
, wxT("invalid listbox index") );
1070 GtkWidget
*item
= GTK_WIDGET(target
->data
);
1071 wxCHECK_RET( item
, wxT("invalid listbox code") );
1073 if (item
->allocation
.y
== -1)
1075 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
1076 data
->m_listbox
= this;
1078 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
1083 float y
= item
->allocation
.y
;
1084 if (y
> adjustment
->upper
- adjustment
->page_size
)
1085 y
= adjustment
->upper
- adjustment
->page_size
;
1086 gtk_adjustment_set_value( adjustment
, y
);
1089 // ----------------------------------------------------------------------------
1091 // ----------------------------------------------------------------------------
1093 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
1097 GList
*child
= m_list
->children
;
1101 if (GTK_WIDGET(child
->data
) == item
) return count
;
1103 child
= child
->next
;
1110 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
1112 GList
*child
= m_list
->children
;
1115 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
1116 child
= child
->next
;
1119 #endif // wxUSE_TOOLTIPS
1121 GtkWidget
*wxListBox::GetConnectWidget()
1123 // return GTK_WIDGET(m_list);
1127 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
1132 if (m_widget
->window
== window
) return true;
1134 if (GTK_WIDGET(m_list
)->window
== window
) return true;
1136 GList
*child
= m_list
->children
;
1139 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
1140 if (bin
->window
== window
) return true;
1141 child
= child
->next
;
1148 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1150 if (m_hasBgCol
&& m_backgroundColour
.Ok())
1152 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
1155 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1156 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1157 gdk_window_clear( window
);
1161 GList
*child
= m_list
->children
;
1164 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1166 GtkBin
*bin
= GTK_BIN( child
->data
);
1167 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1168 gtk_widget_modify_style( label
, style
);
1170 child
= child
->next
;
1174 void wxListBox::OnInternalIdle()
1176 wxCursor cursor
= m_cursor
;
1177 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1179 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1181 /* I now set the cursor the anew in every OnInternalIdle call
1182 as setting the cursor in a parent window also effects the
1183 windows above so that checking for the current cursor is
1186 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1188 GList
*child
= m_list
->children
;
1191 GtkBin
*bin
= GTK_BIN( child
->data
);
1192 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1197 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1199 child
= child
->next
;
1203 if (g_delayedFocus
== this)
1205 if (GTK_WIDGET_REALIZED(m_widget
))
1207 gtk_widget_grab_focus( m_widget
);
1208 g_delayedFocus
= NULL
;
1212 if (wxUpdateUIEvent::CanUpdate(this))
1213 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1216 wxSize
wxListBox::DoGetBestSize() const
1218 int lbWidth
= 100; // some defaults
1222 // Find the widest line
1223 for(unsigned int i
= 0; i
< GetCount(); i
++) {
1224 wxString
str(GetString(i
));
1225 GetTextExtent(str
, &wLine
, NULL
);
1226 lbWidth
= wxMax(lbWidth
, wLine
);
1229 // Add room for the scrollbar
1230 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1232 // And just a bit more
1234 GetTextExtent( wxT("X"), &cx
, &cy
);
1237 // don't make the listbox too tall (limit height to around 10 items) but don't
1238 // make it too small neither
1239 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1241 wxSize
best(lbWidth
, lbHeight
);
1242 CacheBestSize(best
);
1246 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1248 // the mouse event coords are relative to the listbox items, we need to
1249 // translate them to the normal client coords
1250 x
+= widget
->allocation
.x
;
1251 y
+= widget
->allocation
.y
;
1257 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1259 return GetDefaultAttributesFromGTKWidget(gtk_list_new
, true);
1262 #endif // wxUSE_LISTBOX