1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "choice.h"
19 #include "wx/choice.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 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
51 int n
= choice
->GetSelection();
54 event
.SetString( choice
->GetStringSelection() );
55 event
.SetEventObject(choice
);
57 if ( choice
->HasClientObjectData() )
58 event
.SetClientObject( choice
->GetClientObject(n
) );
59 else if ( choice
->HasClientUntypedData() )
60 event
.SetClientData( choice
->GetClientData(n
) );
62 choice
->GetEventHandler()->ProcessEvent(event
);
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
73 m_strings
= (wxSortedArrayString
*)NULL
;
76 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
77 const wxPoint
&pos
, const wxSize
&size
,
78 int n
, const wxString choices
[],
79 long style
, const wxValidator
& validator
, const wxString
&name
)
82 #if (GTK_MINOR_VERSION > 0)
83 m_acceptsFocus
= TRUE
;
86 if (!PreCreation( parent
, pos
, size
) ||
87 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
89 wxFAIL_MSG( wxT("wxChoice creation failed") );
93 m_widget
= gtk_option_menu_new();
95 if ( style
& wxCB_SORT
)
97 // if our m_strings != NULL, DoAppend() will check for it and insert
98 // items in the correct order
99 m_strings
= new wxSortedArrayString
;
102 GtkWidget
*menu
= gtk_menu_new();
104 for (int i
= 0; i
< n
; i
++)
106 GtkAppendHelper(menu
, choices
[i
]);
109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
111 m_parent
->DoAddChild( this );
115 SetFont( parent
->GetFont() );
119 SetBackgroundColour( parent
->GetBackgroundColour() );
120 SetForegroundColour( parent
->GetForegroundColour() );
127 wxChoice::~wxChoice()
134 int wxChoice::DoAppend( const wxString
&item
)
136 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
138 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
140 return GtkAppendHelper(menu
, item
);
143 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
145 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
147 wxNode
*node
= m_clientList
.Nth( n
);
148 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
150 node
->SetData( (wxObject
*) clientData
);
153 void* wxChoice::DoGetItemClientData( int n
) const
155 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
157 wxNode
*node
= m_clientList
.Nth( n
);
158 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
163 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
165 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
167 wxNode
*node
= m_clientList
.Nth( n
);
168 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
170 wxClientData
*cd
= (wxClientData
*) node
->Data();
173 node
->SetData( (wxObject
*) clientData
);
176 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
178 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
180 wxNode
*node
= m_clientList
.Nth( n
);
181 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
182 wxT("invalid index in wxChoice::DoGetItemClientObject") );
184 return (wxClientData
*) node
->Data();
187 void wxChoice::Clear()
189 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
191 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
192 GtkWidget
*menu
= gtk_menu_new();
193 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
195 if ( HasClientObjectData() )
197 // destroy the data (due to Robert's idea of using wxList<wxObject>
198 // and not wxList<wxClientData> we can't just say
199 // m_clientList.DeleteContents(TRUE) - this would crash!
200 wxNode
*node
= m_clientList
.First();
203 delete (wxClientData
*)node
->Data();
207 m_clientList
.Clear();
213 void wxChoice::Delete( int WXUNUSED(n
) )
215 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
218 int wxChoice::FindString( const wxString
&string
) const
220 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
222 // If you read this code once and you think you understand
223 // it, then you are very wrong. Robert Roebling.
225 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
227 GList
*child
= menu_shell
->children
;
230 GtkBin
*bin
= GTK_BIN( child
->data
);
231 GtkLabel
*label
= (GtkLabel
*) NULL
;
232 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
233 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
235 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
237 if (string
== wxString(label
->label
,*wxConvCurrent
))
247 int wxChoice::GetSelection() const
249 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
251 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
253 GList
*child
= menu_shell
->children
;
256 GtkBin
*bin
= GTK_BIN( child
->data
);
257 if (!bin
->child
) return count
;
265 void wxChoice::SetString( int WXUNUSED(n
), const wxString
& WXUNUSED(string
) )
267 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
269 wxFAIL_MSG(wxT("not implemented"));
272 wxString
wxChoice::GetString( int n
) const
274 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
276 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
278 GList
*child
= menu_shell
->children
;
281 GtkBin
*bin
= GTK_BIN( child
->data
);
284 GtkLabel
*label
= (GtkLabel
*) NULL
;
285 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
286 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
288 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
290 return wxString(label
->label
,*wxConvCurrent
);
296 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
301 int wxChoice::GetCount() const
303 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
305 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
307 GList
*child
= menu_shell
->children
;
316 void wxChoice::SetSelection( int n
)
318 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
321 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
324 void wxChoice::ApplyWidgetStyle()
328 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
330 gtk_widget_set_style( m_widget
, m_widgetStyle
);
331 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
333 GList
*child
= menu_shell
->children
;
336 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
338 GtkBin
*bin
= GTK_BIN( child
->data
);
339 GtkWidget
*label
= (GtkWidget
*) NULL
;
340 if (bin
->child
) label
= bin
->child
;
341 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
343 gtk_widget_set_style( label
, m_widgetStyle
);
349 size_t wxChoice::GtkAppendHelper(GtkWidget
*menu
, const wxString
& item
)
351 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
356 // sorted control, need to insert at the correct index
357 index
= m_strings
->Add(item
);
359 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
363 m_clientList
.Insert( m_clientList
.Item(index
- 1),
368 m_clientList
.Insert( (wxObject
*) NULL
);
373 // normal control, just append
374 gtk_menu_append( GTK_MENU(menu
), menu_item
);
376 m_clientList
.Append( (wxObject
*) NULL
);
378 // don't call wxChoice::GetCount() from here because it doesn't work
379 // if we're called from ctor (and GtkMenuShell is still NULL)
380 index
= m_clientList
.GetCount() - 1;
383 if (GTK_WIDGET_REALIZED(m_widget
))
385 gtk_widget_realize( menu_item
);
386 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
388 if (m_widgetStyle
) ApplyWidgetStyle();
391 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
392 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
394 gtk_widget_show( menu_item
);
396 // return the index of the item in the control
400 wxSize
wxChoice::DoGetBestSize() const
402 wxSize
ret( wxControl::DoGetBestSize() );
404 // we know better our horizontal extent: it depends on the longest string
409 GdkFont
*font
= m_font
.GetInternalFont();
412 size_t count
= GetCount();
413 for ( size_t n
= 0; n
< count
; n
++ )
415 width
= (wxCoord
)gdk_string_width(font
, GetString(n
).mbc_str());
420 // add extra for the choice "=" button
422 // VZ: I don't know how to get the right value, it seems to be in
423 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
424 // to it - maybe we can use gtk_option_menu_size_request() for this
427 // This default value works only for the default GTK+ theme (i.e.
428 // no theme at all) (FIXME)
429 static const int widthChoiceIndicator
= 35;
430 ret
.x
+= widthChoiceIndicator
;
433 // but not less than the minimal width
437 ret
.y
= 16 + gdk_char_height( m_widget
->style
->font
, 'H' );
441 #endif // wxUSE_CHOICE