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 "choice.h"
19 #include "wx/choice.h"
20 #include "wx/arrstr.h"
22 #include "wx/gtk/private.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
44 wxapp_install_idle_handler();
46 if (!choice
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 int selection
= wxNOT_FOUND
;
53 selection
= gtk_option_menu_get_history( GTK_OPTION_MENU(choice
->GetHandle()) );
55 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(choice
->GetHandle()) ) );
58 GList
*child
= menu_shell
->children
;
61 GtkBin
*bin
= GTK_BIN( child
->data
);
71 choice
->m_selection_hack
= selection
;
73 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
74 int n
= choice
->GetSelection();
77 event
.SetString( choice
->GetStringSelection() );
78 event
.SetEventObject(choice
);
80 if ( choice
->HasClientObjectData() )
81 event
.SetClientObject( choice
->GetClientObject(n
) );
82 else if ( choice
->HasClientUntypedData() )
83 event
.SetClientData( choice
->GetClientData(n
) );
85 choice
->GetEventHandler()->ProcessEvent(event
);
88 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
92 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
96 m_strings
= (wxSortedArrayString
*)NULL
;
99 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
100 const wxPoint
&pos
, const wxSize
&size
,
101 const wxArrayString
& choices
,
102 long style
, const wxValidator
& validator
,
103 const wxString
&name
)
105 wxCArrayString
chs(choices
);
107 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
108 style
, validator
, name
);
111 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
112 const wxPoint
&pos
, const wxSize
&size
,
113 int n
, const wxString choices
[],
114 long style
, const wxValidator
& validator
, const wxString
&name
)
117 #if (GTK_MINOR_VERSION > 0)
118 m_acceptsFocus
= TRUE
;
121 if (!PreCreation( parent
, pos
, size
) ||
122 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
124 wxFAIL_MSG( wxT("wxChoice creation failed") );
128 m_widget
= gtk_option_menu_new();
130 if ( style
& wxCB_SORT
)
132 // if our m_strings != NULL, DoAppend() will check for it and insert
133 // items in the correct order
134 m_strings
= new wxSortedArrayString
;
137 // begin with no selection
138 m_selection_hack
= wxNOT_FOUND
;
140 GtkWidget
*menu
= gtk_menu_new();
142 for (int i
= 0; i
< n
; i
++)
144 GtkAddHelper(menu
, i
, choices
[i
]);
147 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
149 m_parent
->DoAddChild( this );
152 SetBestSize(size
); // need this too because this is a wxControlWithItems
157 wxChoice::~wxChoice()
164 int wxChoice::DoAppend( const wxString
&item
)
166 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
168 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
170 return GtkAddHelper(menu
, GetCount(), item
);
173 int wxChoice::DoInsert( const wxString
&item
, int pos
)
175 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
176 wxCHECK_MSG( (pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
178 if (pos
== GetCount())
179 return DoAppend(item
);
181 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
183 // if the item to insert is at or before the selection, and the selection is valid
184 if ((pos
<= m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
186 // move the selection forward one
190 return GtkAddHelper(menu
, pos
, item
);
193 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
195 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
197 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
198 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
200 node
->SetData( (wxObject
*) clientData
);
203 void* wxChoice::DoGetItemClientData( int n
) const
205 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
207 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
208 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
210 return node
->GetData();
213 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
215 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
217 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
218 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
220 // wxItemContainer already deletes data for us
222 node
->SetData( (wxObject
*) clientData
);
225 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
227 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
229 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
230 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
231 wxT("invalid index in wxChoice::DoGetItemClientObject") );
233 return (wxClientData
*) node
->GetData();
236 void wxChoice::Clear()
238 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
240 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
241 GtkWidget
*menu
= gtk_menu_new();
242 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
244 if ( HasClientObjectData() )
246 // destroy the data (due to Robert's idea of using wxList<wxObject>
247 // and not wxList<wxClientData> we can't just say
248 // m_clientList.DeleteContents(TRUE) - this would crash!
249 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
252 delete (wxClientData
*)node
->GetData();
253 node
= node
->GetNext();
256 m_clientList
.Clear();
261 // begin with no selection
262 m_selection_hack
= wxNOT_FOUND
;
265 void wxChoice::Delete( int n
)
267 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
269 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
270 // in 2.0), hence this dumb implementation -- still better than nothing
274 wxCHECK_RET( n
>= 0 && n
< count
, _T("invalid index in wxChoice::Delete") );
276 // if the item to delete is before the selection, and the selection is valid
277 if ((n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
279 // move the selection back one
282 else if (n
== m_selection_hack
)
284 // invalidate the selection
285 m_selection_hack
= wxNOT_FOUND
;
288 const bool hasClientData
= m_clientDataItemsType
!= wxClientData_None
;
289 const bool hasObjectData
= m_clientDataItemsType
== wxClientData_Object
;
291 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
294 wxArrayPtrVoid itemsData
;
296 for ( i
= 0; i
< count
; i
++ )
300 items
.Add(GetString(i
));
303 // also save the client data
304 itemsData
.Add(node
->GetData());
307 else // need to delete the client object too
311 delete (wxClientData
*)node
->GetData();
317 node
= node
->GetNext();
323 // prevent Clear() from destroying all client data
324 m_clientDataItemsType
= wxClientData_None
;
329 for ( i
= 0; i
< count
- 1; i
++ )
334 SetClientObject(i
, (wxClientData
*)itemsData
[i
]);
335 else if ( hasClientData
)
336 SetClientData(i
, itemsData
[i
]);
340 int wxChoice::FindString( const wxString
&string
) const
342 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
344 // If you read this code once and you think you understand
345 // it, then you are very wrong. Robert Roebling.
347 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
349 GList
*child
= menu_shell
->children
;
352 GtkBin
*bin
= GTK_BIN( child
->data
);
353 GtkLabel
*label
= (GtkLabel
*) NULL
;
355 label
= GTK_LABEL(bin
->child
);
357 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
359 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
362 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
364 wxString
tmp( label
->label
);
376 int wxChoice::GetSelection() const
378 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
380 return m_selection_hack
;
384 void wxChoice::SetString( int n
, const wxString
& str
)
386 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
388 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
390 GList
*child
= menu_shell
->children
;
393 GtkBin
*bin
= GTK_BIN( child
->data
);
396 GtkLabel
*label
= (GtkLabel
*) NULL
;
398 label
= GTK_LABEL(bin
->child
);
400 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
402 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
404 gtk_label_set_text( label
, wxGTK_CONV( str
) );
413 wxString
wxChoice::GetString( int n
) const
415 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
417 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
419 GList
*child
= menu_shell
->children
;
422 GtkBin
*bin
= GTK_BIN( child
->data
);
425 GtkLabel
*label
= (GtkLabel
*) NULL
;
427 label
= GTK_LABEL(bin
->child
);
429 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
431 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
434 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
436 return wxString( label
->label
);
443 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
448 int wxChoice::GetCount() const
450 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
452 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
454 GList
*child
= menu_shell
->children
;
463 void wxChoice::SetSelection( int n
)
465 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
468 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
470 // set the local selection variable manually
471 if ((n
>= 0) && (n
< GetCount()))
473 // a valid selection has been made
474 m_selection_hack
= n
;
476 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
478 // invalidates the selection if there are no items
479 // or if it is specifically set to wxNOT_FOUND
480 m_selection_hack
= wxNOT_FOUND
;
484 // this selects the first item by default if the selection is out of bounds
485 m_selection_hack
= 0;
489 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
491 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
493 gtk_widget_modify_style( m_widget
, style
);
494 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
496 GList
*child
= menu_shell
->children
;
499 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
501 GtkBin
*bin
= GTK_BIN( child
->data
);
502 GtkWidget
*label
= (GtkWidget
*) NULL
;
506 label
= BUTTON_CHILD(m_widget
);
508 gtk_widget_modify_style( label
, style
);
514 int wxChoice::GtkAddHelper(GtkWidget
*menu
, int pos
, const wxString
& item
)
516 wxCHECK_MSG((pos
>=0) && (pos
<=(int)m_clientList
.GetCount()), -1, wxT("invalid index"));
518 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
523 // sorted control, need to insert at the correct index
524 index
= m_strings
->Add(item
);
526 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
530 m_clientList
.Insert( m_clientList
.Item(index
- 1),
535 m_clientList
.Insert( (wxObject
*) NULL
);
540 // don't call wxChoice::GetCount() from here because it doesn't work
541 // if we're called from ctor (and GtkMenuShell is still NULL)
543 // normal control, just append
544 if (pos
== (int)m_clientList
.GetCount())
546 gtk_menu_append( GTK_MENU(menu
), menu_item
);
547 m_clientList
.Append( (wxObject
*) NULL
);
548 index
= m_clientList
.GetCount() - 1;
552 gtk_menu_insert( GTK_MENU(menu
), menu_item
, pos
);
553 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
558 if (GTK_WIDGET_REALIZED(m_widget
))
560 gtk_widget_realize( menu_item
);
561 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
566 // The best size of a wxChoice should probably
567 // be changed everytime the control has been
568 // changed, but at least after adding an item
569 // it has to change. Adapted from Matt Ownby.
570 InvalidateBestSize();
572 gtk_signal_connect_after( GTK_OBJECT( menu_item
), "activate",
573 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
575 gtk_widget_show( menu_item
);
577 // return the index of the item in the control
581 wxSize
wxChoice::DoGetBestSize() const
583 wxSize
ret( wxControl::DoGetBestSize() );
585 // we know better our horizontal extent: it depends on the longest string
591 size_t count
= GetCount();
592 for ( size_t n
= 0; n
< count
; n
++ )
594 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
599 // add extra for the choice "=" button
601 // VZ: I don't know how to get the right value, it seems to be in
602 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
603 // to it - maybe we can use gtk_option_menu_size_request() for this
606 // This default value works only for the default GTK+ theme (i.e.
607 // no theme at all) (FIXME)
608 static const int widthChoiceIndicator
= 35;
609 ret
.x
+= widthChoiceIndicator
;
612 // but not less than the minimal width
616 // If this request_size is called with no entries then
617 // the returned height is wrong. Give it a reasonable
620 ret
.y
= 8 + GetCharHeight();
626 bool wxChoice::IsOwnGtkWindow( GdkWindow
*window
)
629 return GTK_BUTTON(m_widget
)->event_window
;
631 return (window
== m_widget
->window
);
637 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
639 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
643 #endif // wxUSE_CHOICE