1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "choice.h"
15 #include "wx/choice.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
42 wxapp_install_idle_handler();
44 if (!choice
->m_hasVMT
) return;
46 if (g_blockEventsOnDrag
) return;
48 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
49 event
.SetInt( choice
->GetSelection() );
50 event
.SetString( choice
->GetStringSelection() );
51 event
.SetEventObject(choice
);
52 choice
->GetEventHandler()->ProcessEvent(event
);
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
65 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
66 const wxPoint
&pos
, const wxSize
&size
,
67 int n
, const wxString choices
[],
68 long style
, const wxValidator
& validator
, const wxString
&name
)
71 #if (GTK_MINOR_VERSION > 0)
72 m_acceptsFocus
= TRUE
;
75 if (!PreCreation( parent
, pos
, size
) ||
76 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
78 wxFAIL_MSG( wxT("wxChoice creation failed") );
82 m_widget
= gtk_option_menu_new();
89 SetSize( newSize
.x
, newSize
.y
);
91 GtkWidget
*menu
= gtk_menu_new();
93 for (int i
= 0; i
< n
; i
++)
95 m_clientList
.Append( (wxObject
*) NULL
);
97 GtkWidget
*item
= gtk_menu_item_new_with_label( choices
[i
].mbc_str() );
98 gtk_menu_append( GTK_MENU(menu
), item
);
100 gtk_widget_show( item
);
102 gtk_signal_connect( GTK_OBJECT( item
), "activate",
103 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
105 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
107 m_parent
->DoAddChild( this );
111 SetBackgroundColour( parent
->GetBackgroundColour() );
112 SetForegroundColour( parent
->GetForegroundColour() );
113 SetFont( parent
->GetFont() );
120 wxChoice::~wxChoice()
125 int wxChoice::DoAppend( const wxString
&item
)
127 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
129 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
130 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
132 gtk_menu_append( GTK_MENU(menu
), menu_item
);
134 if (GTK_WIDGET_REALIZED(m_widget
))
136 gtk_widget_realize( menu_item
);
137 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
139 if (m_widgetStyle
) ApplyWidgetStyle();
142 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
143 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
145 gtk_widget_show( menu_item
);
147 m_clientList
.Append( (wxObject
*) NULL
);
149 // return the index of the item in the control
150 return GetCount() - 1;
153 void wxChoice::DoSetClientData( int n
, void* clientData
)
155 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
157 wxNode
*node
= m_clientList
.Nth( n
);
158 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetClientData") );
160 node
->SetData( (wxObject
*) clientData
);
163 void* wxChoice::DoGetClientData( int n
) const
165 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
167 wxNode
*node
= m_clientList
.Nth( n
);
168 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetClientData") );
173 void wxChoice::DoSetClientObject( int n
, wxClientData
* clientData
)
175 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
177 wxNode
*node
= m_clientList
.Nth( n
);
178 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetClientObject") );
180 wxClientData
*cd
= (wxClientData
*) node
->Data();
183 node
->SetData( (wxObject
*) clientData
);
186 wxClientData
* wxChoice::DoGetClientObject( int n
) const
188 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid combobox") );
190 wxNode
*node
= m_clientList
.Nth( n
);
191 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
192 wxT("invalid index in wxChoice::DoGetClientObject") );
194 return (wxClientData
*) node
->Data();
197 void wxChoice::Clear()
199 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
201 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
202 GtkWidget
*menu
= gtk_menu_new();
203 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
205 if (m_clientDataItemsType
== ClientData_Object
)
207 wxNode
*node
= m_clientList
.First();
210 wxClientData
*cd
= (wxClientData
*)node
->Data();
215 m_clientList
.Clear();
218 void wxChoice::Delete( int WXUNUSED(n
) )
220 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
223 int wxChoice::FindString( const wxString
&string
) const
225 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
227 // If you read this code once and you think you understand
228 // it, then you are very wrong. Robert Roebling.
230 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
232 GList
*child
= menu_shell
->children
;
235 GtkBin
*bin
= GTK_BIN( child
->data
);
236 GtkLabel
*label
= (GtkLabel
*) NULL
;
237 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
238 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
240 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
242 if (string
== wxString(label
->label
,*wxConvCurrent
))
252 int wxChoice::GetSelection() const
254 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
256 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
258 GList
*child
= menu_shell
->children
;
261 GtkBin
*bin
= GTK_BIN( child
->data
);
262 if (!bin
->child
) return count
;
270 wxString
wxChoice::GetString( int n
) const
272 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
274 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
276 GList
*child
= menu_shell
->children
;
279 GtkBin
*bin
= GTK_BIN( child
->data
);
282 GtkLabel
*label
= (GtkLabel
*) NULL
;
283 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
284 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
286 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
288 return wxString(label
->label
,*wxConvCurrent
);
294 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
299 int wxChoice::GetCount() const
301 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
303 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
305 GList
*child
= menu_shell
->children
;
314 void wxChoice::SetSelection( int n
)
316 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
319 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
322 void wxChoice::DisableEvents()
325 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
326 GList *child = menu_shell->children;
329 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
330 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
337 void wxChoice::EnableEvents()
340 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
341 GList *child = menu_shell->children;
344 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
345 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
352 void wxChoice::ApplyWidgetStyle()
356 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
358 gtk_widget_set_style( m_widget
, m_widgetStyle
);
359 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
361 GList
*child
= menu_shell
->children
;
364 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
366 GtkBin
*bin
= GTK_BIN( child
->data
);
367 GtkWidget
*label
= (GtkWidget
*) NULL
;
368 if (bin
->child
) label
= bin
->child
;
369 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
371 gtk_widget_set_style( label
, m_widgetStyle
);