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
71 static gint
wxlistbox_idle_callback( gpointer gdata
)
73 wxlistbox_idle_struct
* data
= (wxlistbox_idle_struct
*) gdata
;
76 gtk_idle_remove( data
->m_tag
);
78 // check that the items haven't been deleted from the listbox since we had
79 // installed this callback
80 wxListBox
*lbox
= data
->m_listbox
;
81 if ( data
->m_item
< lbox
->GetCount() )
83 lbox
->SetFirstItem( data
->m_item
);
94 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
99 static gint
gtk_listitem_focus_in_callback( GtkWidget
*widget
,
100 GdkEvent
*WXUNUSED(event
),
104 wxapp_install_idle_handler();
109 // does the window itself think that it has the focus?
110 if ( !win
->m_hasFocus
)
112 // not yet, notify it
113 win
->m_hasFocus
= TRUE
;
115 wxChildFocusEvent
eventChildFocus(win
);
116 (void)win
->GetEventHandler()->ProcessEvent(eventChildFocus
);
118 wxFocusEvent
eventFocus(wxEVT_SET_FOCUS
, win
->GetId());
119 eventFocus
.SetEventObject(win
);
121 (void)win
->GetEventHandler()->ProcessEvent(eventFocus
);
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
133 static gint
gtk_listitem_focus_out_callback( GtkWidget
*widget
, GdkEventFocus
*gdk_event
, wxWindowGTK
*win
)
136 wxapp_install_idle_handler();
138 g_focusWindow
= (wxWindowGTK
*)NULL
;
140 // don't send the window a kill focus event if it thinks that it doesn't
141 // have focus already
142 if ( win
->m_hasFocus
)
144 win
->m_hasFocus
= FALSE
;
146 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
147 event
.SetEventObject( win
);
149 // even if we did process the event in wx code, still let GTK itself
150 // process it too as otherwise bad things happen, especially in GTK2
151 // where the text control simply aborts the program if it doesn't get
152 // the matching focus out event
153 (void)win
->GetEventHandler()->ProcessEvent( event
);
160 //-----------------------------------------------------------------------------
161 // "button_release_event"
162 //-----------------------------------------------------------------------------
164 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
165 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
166 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
167 this can lead to race conditions so that we emit the dclick event
168 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
172 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
173 GdkEventButton
* WXUNUSED(gdk_event
),
176 if (g_isIdle
) wxapp_install_idle_handler();
178 if (g_blockEventsOnDrag
) return FALSE
;
179 if (g_blockEventsOnScroll
) return FALSE
;
181 if (!listbox
->m_hasVMT
) return FALSE
;
183 if (!g_hasDoubleClicked
) return FALSE
;
185 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
186 event
.SetEventObject( listbox
);
188 wxArrayInt aSelections
;
189 int n
, count
= listbox
->GetSelections(aSelections
);
193 if ( listbox
->HasClientObjectData() )
194 event
.SetClientObject( listbox
->GetClientObject(n
) );
195 else if ( listbox
->HasClientUntypedData() )
196 event
.SetClientData( listbox
->GetClientData(n
) );
197 event
.SetString( listbox
->GetString(n
) );
206 listbox
->GetEventHandler()->ProcessEvent( event
);
212 //-----------------------------------------------------------------------------
213 // "button_press_event"
214 //-----------------------------------------------------------------------------
218 gtk_listbox_button_press_callback( GtkWidget
*widget
,
219 GdkEventButton
*gdk_event
,
222 if (g_isIdle
) wxapp_install_idle_handler();
224 if (g_blockEventsOnDrag
) return FALSE
;
225 if (g_blockEventsOnScroll
) return FALSE
;
227 if (!listbox
->m_hasVMT
) return FALSE
;
229 int sel
= listbox
->GtkGetIndex( widget
);
231 #if wxUSE_CHECKLISTBOX
232 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
234 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
236 clb
->Check( sel
, !clb
->IsChecked(sel
) );
238 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
239 event
.SetEventObject( listbox
);
241 listbox
->GetEventHandler()->ProcessEvent( event
);
243 #endif // wxUSE_CHECKLISTBOX
245 if ((gdk_event
->state
== 0) &&
246 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
247 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
249 listbox
->m_blockEvent
= TRUE
;
252 for (i
= 0; i
< (int)listbox
->GetCount(); i
++)
254 gtk_list_unselect_item( GTK_LIST(listbox
->m_list
), i
);
256 listbox
->m_blockEvent
= FALSE
;
261 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
262 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
268 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
274 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
277 wxapp_install_idle_handler();
279 if (g_blockEventsOnDrag
)
284 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
286 wxNavigationKeyEvent new_event
;
287 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
288 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
289 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
290 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
291 new_event
.SetCurrentFocus( listbox
);
292 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
295 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
297 // eat return in all modes
301 #if wxUSE_CHECKLISTBOX
302 if ((gdk_event
->keyval
== ' ') && (listbox
->m_hasCheckBoxes
) && (!ret
))
304 int sel
= listbox
->GtkGetIndex( widget
);
306 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
308 clb
->Check( sel
, !clb
->IsChecked(sel
) );
310 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
311 new_event
.SetEventObject( listbox
);
312 new_event
.SetInt( sel
);
313 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
315 #endif // wxUSE_CHECKLISTBOX
317 // Check or uncheck item with SPACE
318 if ((gdk_event
->keyval
== ' ') && (!ret
) &&
319 (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
320 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
322 int sel
= listbox
->GtkGetIndex( widget
);
328 if (listbox
->IsSelected( sel
))
329 gtk_list_unselect_item( listbox
->m_list
, sel
);
331 gtk_list_select_item( listbox
->m_list
, sel
);
333 wxCommandEvent
new_event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
334 new_event
.SetEventObject( listbox
);
335 wxArrayInt aSelections
;
336 int n
, count
= listbox
->GetSelections(aSelections
);
340 if ( listbox
->HasClientObjectData() )
341 new_event
.SetClientObject( listbox
->GetClientObject(n
) );
342 else if ( listbox
->HasClientUntypedData() )
343 new_event
.SetClientData( listbox
->GetClientData(n
) );
344 new_event
.SetString( listbox
->GetString(n
) );
351 listbox
->GetEventHandler()->ProcessEvent( new_event
);
357 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
365 //-----------------------------------------------------------------------------
366 // "select" and "deselect"
367 //-----------------------------------------------------------------------------
369 static void gtk_listitem_select_cb( GtkWidget
*widget
,
373 if (g_isIdle
) wxapp_install_idle_handler();
375 if (!listbox
->m_hasVMT
) return;
376 if (g_blockEventsOnDrag
) return;
378 if (listbox
->m_blockEvent
) return;
380 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
381 event
.SetEventObject( listbox
);
383 // indicate whether this is a selection or a deselection
384 event
.SetExtraLong( is_selection
);
386 if ((listbox
->GetWindowStyleFlag() & wxLB_SINGLE
) != 0)
388 int sel
= listbox
->GtkGetIndex( widget
);
390 if (listbox
->m_prevSelection
!= sel
)
391 gtk_list_unselect_item( listbox
->m_list
, listbox
->m_prevSelection
);
393 listbox
->m_prevSelection
= sel
;
396 wxArrayInt aSelections
;
397 int n
, count
= listbox
->GetSelections(aSelections
);
401 if ( listbox
->HasClientObjectData() )
402 event
.SetClientObject( listbox
->GetClientObject(n
) );
403 else if ( listbox
->HasClientUntypedData() )
404 event
.SetClientData( listbox
->GetClientData(n
) );
405 event
.SetString( listbox
->GetString(n
) );
414 // No longer required with new code in wxLB_SINGLE
415 // listbox->GetEventHandler()->AddPendingEvent( event );
416 listbox
->GetEventHandler()->ProcessEvent( event
);
420 static void gtk_listitem_select_callback( GtkWidget
*widget
, wxListBox
*listbox
)
422 gtk_listitem_select_cb( widget
, listbox
, TRUE
);
427 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
429 gtk_listitem_select_cb( widget
, listbox
, FALSE
);
433 //-----------------------------------------------------------------------------
435 //-----------------------------------------------------------------------------
439 gtk_listbox_realized_callback( GtkWidget
*m_widget
, wxListBox
*win
)
442 wxapp_install_idle_handler();
444 GList
*child
= win
->m_list
->children
;
445 for (child
= win
->m_list
->children
; child
!= NULL
; child
= child
->next
)
446 gtk_widget_show( GTK_WIDGET(child
->data
) );
452 //-----------------------------------------------------------------------------
454 //-----------------------------------------------------------------------------
456 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 wxListBox::wxListBox()
464 m_list
= (GtkList
*) NULL
;
465 #if wxUSE_CHECKLISTBOX
466 m_hasCheckBoxes
= FALSE
;
467 #endif // wxUSE_CHECKLISTBOX
470 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
471 const wxPoint
&pos
, const wxSize
&size
,
472 const wxArrayString
& choices
,
473 long style
, const wxValidator
& validator
,
474 const wxString
&name
)
476 wxCArrayString
chs(choices
);
478 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
479 style
, validator
, name
);
482 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
483 const wxPoint
&pos
, const wxSize
&size
,
484 int n
, const wxString choices
[],
485 long style
, const wxValidator
& validator
,
486 const wxString
&name
)
489 m_acceptsFocus
= TRUE
;
490 m_prevSelection
= 0; // or -1 ??
491 m_blockEvent
= FALSE
;
493 if (!PreCreation( parent
, pos
, size
) ||
494 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
496 wxFAIL_MSG( wxT("wxListBox creation failed") );
500 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
501 if (style
& wxLB_ALWAYS_SB
)
503 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
504 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
508 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
509 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
512 m_list
= GTK_LIST( gtk_list_new() );
514 GtkSelectionMode mode
;
515 if (style
& wxLB_MULTIPLE
)
517 mode
= GTK_SELECTION_MULTIPLE
;
519 else if (style
& wxLB_EXTENDED
)
521 mode
= GTK_SELECTION_EXTENDED
;
525 // if style was 0 set single mode
526 m_windowStyle
|= wxLB_SINGLE
;
527 mode
= GTK_SELECTION_SINGLE
;
530 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
532 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
534 /* make list scroll when moving the focus down using cursor keys */
535 gtk_container_set_focus_vadjustment(
536 GTK_CONTAINER(m_list
),
537 gtk_scrolled_window_get_vadjustment(
538 GTK_SCROLLED_WINDOW(m_widget
)));
540 gtk_widget_show( GTK_WIDGET(m_list
) );
542 gtk_signal_connect( GTK_OBJECT(m_list
), "realize",
543 GTK_SIGNAL_FUNC(gtk_listbox_realized_callback
), (gpointer
) this );
545 if ( style
& wxLB_SORT
)
547 // this will change DoAppend() behaviour
548 m_strings
= new wxSortedArrayString
;
552 m_strings
= (wxSortedArrayString
*)NULL
;
555 for (int i
= 0; i
< n
; i
++)
558 DoAppend(choices
[i
]);
561 m_parent
->DoAddChild( this );
564 SetBestSize(size
); // need this too because this is a wxControlWithItems
569 wxListBox::~wxListBox()
579 // ----------------------------------------------------------------------------
581 // ----------------------------------------------------------------------------
583 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
585 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
587 // VZ: notice that InsertItems knows nothing about sorting, so calling it
588 // from outside (and not from our own Append) is likely to break
591 // code elsewhere supposes we have as many items in m_clientList as items
593 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
594 wxT("bug in client data management") );
596 InvalidateBestSize();
598 GList
*children
= m_list
->children
;
599 int length
= g_list_length(children
);
601 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
603 size_t nItems
= items
.GetCount();
608 for (size_t n
= 0; n
< nItems
; n
++)
610 index
= m_strings
->Add( items
[n
] );
612 if (index
!= GetCount())
614 GtkAddItem( items
[n
], index
);
615 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
616 m_clientList
.Insert( node
, (wxObject
*) NULL
);
620 GtkAddItem( items
[n
] );
621 m_clientList
.Append( (wxObject
*) NULL
);
629 for ( size_t n
= 0; n
< nItems
; n
++ )
631 GtkAddItem( items
[n
] );
633 m_clientList
.Append((wxObject
*)NULL
);
638 wxList::compatibility_iterator node
= m_clientList
.Item( pos
);
639 for ( size_t n
= 0; n
< nItems
; n
++ )
641 GtkAddItem( items
[n
], pos
+n
);
643 m_clientList
.Insert( node
, (wxObject
*)NULL
);
648 wxASSERT_MSG( m_clientList
.GetCount() == (size_t)GetCount(),
649 wxT("bug in client data management") );
652 int wxListBox::DoAppend( const wxString
& item
)
654 InvalidateBestSize();
658 // need to determine the index
659 int index
= m_strings
->Add( item
);
661 // only if not at the end anyway
662 if (index
!= GetCount())
664 GtkAddItem( item
, index
);
666 wxList::compatibility_iterator node
= m_clientList
.Item( index
);
667 m_clientList
.Insert( node
, (wxObject
*)NULL
);
675 m_clientList
.Append((wxObject
*)NULL
);
677 return GetCount() - 1;
680 void wxListBox::GtkAddItem( const wxString
&item
, int pos
)
682 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
684 GtkWidget
*list_item
;
686 wxString
label(item
);
687 #if wxUSE_CHECKLISTBOX
690 label
.Prepend(wxCHECKLBOX_STRING
);
692 #endif // wxUSE_CHECKLISTBOX
694 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( label
) );
696 GList
*gitem_list
= g_list_alloc ();
697 gitem_list
->data
= list_item
;
700 gtk_list_append_items( GTK_LIST (m_list
), gitem_list
);
702 gtk_list_insert_items( GTK_LIST (m_list
), gitem_list
, pos
);
704 gtk_signal_connect_after( GTK_OBJECT(list_item
), "select",
705 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
707 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
708 gtk_signal_connect_after( GTK_OBJECT(list_item
), "deselect",
709 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
711 gtk_signal_connect( GTK_OBJECT(list_item
),
712 "button_press_event",
713 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
716 gtk_signal_connect_after( GTK_OBJECT(list_item
),
717 "button_release_event",
718 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
721 gtk_signal_connect( GTK_OBJECT(list_item
),
723 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
727 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_in_event",
728 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback
), (gpointer
)this );
730 gtk_signal_connect( GTK_OBJECT(list_item
), "focus_out_event",
731 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback
), (gpointer
)this );
733 ConnectWidget( list_item
);
735 if (GTK_WIDGET_REALIZED(m_widget
))
737 gtk_widget_show( list_item
);
739 gtk_widget_realize( list_item
);
740 gtk_widget_realize( GTK_BIN(list_item
)->child
);
743 if (m_tooltip
) m_tooltip
->Apply( this );
747 // Apply current widget style to the new list_item
748 GtkRcStyle
*style
= CreateWidgetStyle();
751 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
752 GtkBin
*bin
= GTK_BIN( list_item
);
753 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
754 gtk_widget_modify_style( label
, style
);
755 gtk_rc_style_unref( style
);
759 void wxListBox::DoSetItems( const wxArrayString
& items
,
764 DoInsertItems(items
, 0);
768 size_t count
= items
.GetCount();
769 for ( size_t n
= 0; n
< count
; n
++ )
771 SetClientData(n
, clientData
[n
]);
776 // ----------------------------------------------------------------------------
778 // ----------------------------------------------------------------------------
780 void wxListBox::Clear()
782 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
784 gtk_list_clear_items( m_list
, 0, GetCount() );
786 if ( GTK_LIST(m_list
)->last_focus_child
!= NULL
)
788 // This should be NULL, I think.
789 GTK_LIST(m_list
)->last_focus_child
= NULL
;
792 if ( HasClientObjectData() )
794 // destroy the data (due to Robert's idea of using wxList<wxObject>
795 // and not wxList<wxClientData> we can't just say
796 // m_clientList.DeleteContents(TRUE) - this would crash!
797 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
800 delete (wxClientData
*)node
->GetData();
801 node
= node
->GetNext();
804 m_clientList
.Clear();
810 void wxListBox::Delete( int n
)
812 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
814 GList
*child
= g_list_nth( m_list
->children
, n
);
816 wxCHECK_RET( child
, wxT("wrong listbox index") );
818 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
819 gtk_list_remove_items( m_list
, list
);
822 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
825 if ( m_clientDataItemsType
== wxClientData_Object
)
827 wxClientData
*cd
= (wxClientData
*)node
->GetData();
831 m_clientList
.Erase( node
);
835 m_strings
->RemoveAt(n
);
838 // ----------------------------------------------------------------------------
840 // ----------------------------------------------------------------------------
842 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
844 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
846 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
847 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
849 node
->SetData( (wxObject
*) clientData
);
852 void* wxListBox::DoGetItemClientData( int n
) const
854 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
856 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
857 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
859 return node
->GetData();
862 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
864 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
866 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
867 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
869 // wxItemContainer already deletes data for us
871 node
->SetData( (wxObject
*) clientData
);
874 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
876 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
878 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
879 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
880 wxT("invalid index in wxListBox::DoGetItemClientObject") );
882 return (wxClientData
*) node
->GetData();
885 // ----------------------------------------------------------------------------
886 // string list access
887 // ----------------------------------------------------------------------------
889 wxString
wxListBox::GetRealLabel(GList
*item
) const
891 GtkBin
*bin
= GTK_BIN( item
->data
);
892 GtkLabel
*label
= GTK_LABEL( bin
->child
);
897 str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
899 str
= wxString( label
->label
);
902 #if wxUSE_CHECKLISTBOX
903 // checklistboxes have "[±] " prepended to their lables, remove it
905 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
906 if ( m_hasCheckBoxes
)
908 #endif // wxUSE_CHECKLISTBOX
913 void wxListBox::SetString( int n
, const wxString
&string
)
915 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
917 GList
*child
= g_list_nth( m_list
->children
, n
);
920 GtkBin
*bin
= GTK_BIN( child
->data
);
921 GtkLabel
*label
= GTK_LABEL( bin
->child
);
924 #if wxUSE_CHECKLISTBOX
926 str
+= wxCHECKLBOX_STRING
;
927 #endif // wxUSE_CHECKLISTBOX
930 gtk_label_set( label
, wxGTK_CONV( str
) );
934 wxFAIL_MSG(wxT("wrong listbox index"));
938 wxString
wxListBox::GetString( int n
) const
940 wxCHECK_MSG( m_list
!= NULL
, wxT(""), wxT("invalid listbox") );
942 GList
*child
= g_list_nth( m_list
->children
, n
);
945 return GetRealLabel(child
);
948 wxFAIL_MSG(wxT("wrong listbox index"));
953 int wxListBox::GetCount() const
955 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
957 GList
*children
= m_list
->children
;
958 return g_list_length(children
);
961 int wxListBox::FindString( const wxString
&item
) const
963 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
965 GList
*child
= m_list
->children
;
969 if ( GetRealLabel(child
) == item
)
976 // it's not an error if the string is not found -> no wxCHECK
981 // ----------------------------------------------------------------------------
983 // ----------------------------------------------------------------------------
985 int wxListBox::GetSelection() const
987 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
989 GList
*child
= m_list
->children
;
993 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
1000 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
1002 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
1004 // get the number of selected items first
1005 GList
*child
= m_list
->children
;
1007 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
1009 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
1013 aSelections
.Empty();
1017 // now fill the list
1018 aSelections
.Alloc(count
); // optimization attempt
1020 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
1022 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
1030 bool wxListBox::IsSelected( int n
) const
1032 wxCHECK_MSG( m_list
!= NULL
, FALSE
, wxT("invalid listbox") );
1034 GList
*target
= g_list_nth( m_list
->children
, n
);
1036 wxCHECK_MSG( target
, FALSE
, wxT("invalid listbox index") );
1038 return (GTK_WIDGET(target
->data
)->state
== GTK_STATE_SELECTED
) ;
1041 void wxListBox::DoSetSelection( int n
, bool select
)
1043 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
1045 m_blockEvent
= TRUE
;
1049 if ((m_windowStyle
& wxLB_SINGLE
) != 0)
1050 gtk_list_unselect_item( m_list
, m_prevSelection
);
1051 gtk_list_select_item( m_list
, n
);
1052 m_prevSelection
= n
;
1055 gtk_list_unselect_item( m_list
, n
);
1057 m_blockEvent
= FALSE
;
1060 void wxListBox::DoSetFirstItem( int n
)
1062 wxCHECK_RET( m_list
, wxT("invalid listbox") );
1064 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
1067 // terribly efficient
1068 const gchar
*vadjustment_key
= "gtk-vadjustment";
1069 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
1071 GtkAdjustment
*adjustment
=
1072 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
1073 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
1075 GList
*target
= g_list_nth( m_list
->children
, n
);
1076 wxCHECK_RET( target
, wxT("invalid listbox index") );
1078 GtkWidget
*item
= GTK_WIDGET(target
->data
);
1079 wxCHECK_RET( item
, wxT("invalid listbox code") );
1081 if (item
->allocation
.y
== -1)
1083 wxlistbox_idle_struct
* data
= new wxlistbox_idle_struct
;
1084 data
->m_listbox
= this;
1086 data
->m_tag
= gtk_idle_add_priority( 800, wxlistbox_idle_callback
, (gpointer
) data
);
1091 float y
= item
->allocation
.y
;
1092 if (y
> adjustment
->upper
- adjustment
->page_size
)
1093 y
= adjustment
->upper
- adjustment
->page_size
;
1094 gtk_adjustment_set_value( adjustment
, y
);
1097 // ----------------------------------------------------------------------------
1099 // ----------------------------------------------------------------------------
1101 int wxListBox::GtkGetIndex( GtkWidget
*item
) const
1105 GList
*child
= m_list
->children
;
1109 if (GTK_WIDGET(child
->data
) == item
) return count
;
1111 child
= child
->next
;
1118 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
1120 GList
*child
= m_list
->children
;
1123 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
1124 child
= child
->next
;
1127 #endif // wxUSE_TOOLTIPS
1129 GtkWidget
*wxListBox::GetConnectWidget()
1131 // return GTK_WIDGET(m_list);
1135 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
1140 if (m_widget
->window
== window
) return TRUE
;
1142 if (GTK_WIDGET(m_list
)->window
== window
) return TRUE
;
1144 GList
*child
= m_list
->children
;
1147 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
1148 if (bin
->window
== window
) return TRUE
;
1149 child
= child
->next
;
1156 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1158 if (m_hasBgCol
&& m_backgroundColour
.Ok())
1160 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
1163 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1164 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1165 gdk_window_clear( window
);
1169 GList
*child
= m_list
->children
;
1172 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1174 GtkBin
*bin
= GTK_BIN( child
->data
);
1175 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1176 gtk_widget_modify_style( label
, style
);
1178 child
= child
->next
;
1182 void wxListBox::OnInternalIdle()
1184 wxCursor cursor
= m_cursor
;
1185 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1187 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1189 /* I now set the cursor the anew in every OnInternalIdle call
1190 as setting the cursor in a parent window also effects the
1191 windows above so that checking for the current cursor is
1194 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1196 GList
*child
= m_list
->children
;
1199 GtkBin
*bin
= GTK_BIN( child
->data
);
1200 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1205 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1207 child
= child
->next
;
1211 if (g_delayedFocus
== this)
1213 if (GTK_WIDGET_REALIZED(m_widget
))
1215 gtk_widget_grab_focus( m_widget
);
1216 g_delayedFocus
= NULL
;
1220 if (wxUpdateUIEvent::CanUpdate(this))
1221 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1224 wxSize
wxListBox::DoGetBestSize() const
1226 int lbWidth
= 100; // some defaults
1230 // Find the widest line
1231 for(int i
= 0; i
< GetCount(); i
++) {
1232 wxString
str(GetString(i
));
1233 GetTextExtent(str
, &wLine
, NULL
);
1234 lbWidth
= wxMax(lbWidth
, wLine
);
1237 // Add room for the scrollbar
1238 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1240 // And just a bit more
1242 GetTextExtent( wxT("X"), &cx
, &cy
);
1245 // don't make the listbox too tall (limit height to around 10 items) but don't
1246 // make it too small neither
1247 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1249 wxSize
best(lbWidth
, lbHeight
);
1250 CacheBestSize(best
);
1254 void wxListBox::FixUpMouseEvent(GtkWidget
*widget
, wxCoord
& x
, wxCoord
& y
)
1256 // the mouse event coords are relative to the listbox items, we need to
1257 // translate them to the normal client coords
1258 x
+= widget
->allocation
.x
;
1259 y
+= widget
->allocation
.y
;
1265 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1267 return GetDefaultAttributesFromGTKWidget(gtk_list_new
, true);
1270 #endif // wxUSE_LISTBOX