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_clientDataList
.Append( (wxObject
*) NULL
);
96 m_clientObjectList
.Append( (wxObject
*) NULL
);
98 GtkWidget
*item
= gtk_menu_item_new_with_label( choices
[i
].mbc_str() );
99 gtk_menu_append( GTK_MENU(menu
), item
);
101 gtk_widget_show( item
);
103 gtk_signal_connect( GTK_OBJECT( item
), "activate",
104 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
106 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
108 m_parent
->DoAddChild( this );
112 SetBackgroundColour( parent
->GetBackgroundColour() );
113 SetForegroundColour( parent
->GetForegroundColour() );
114 SetFont( parent
->GetFont() );
121 wxChoice::~wxChoice()
126 int wxChoice::DoAppend( const wxString
&item
)
128 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
130 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
131 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
133 gtk_menu_append( GTK_MENU(menu
), menu_item
);
135 if (GTK_WIDGET_REALIZED(m_widget
))
137 gtk_widget_realize( menu_item
);
138 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
140 if (m_widgetStyle
) ApplyWidgetStyle();
143 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
144 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
146 gtk_widget_show( menu_item
);
148 m_clientDataList
.Append( (wxObject
*) NULL
);
149 m_clientObjectList
.Append( (wxObject
*) NULL
);
151 // return the index of the item in the control
155 void wxChoice::DoSetClientData( int n
, void* clientData
)
157 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
159 wxNode
*node
= m_clientDataList
.Nth( n
);
160 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetClientData") );
162 node
->SetData( (wxObject
*) clientData
);
165 void* wxChoice::DoGetClientData( int n
) const
167 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
169 wxNode
*node
= m_clientDataList
.Nth( n
);
170 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetClientData") );
175 void wxChoice::DoSetClientObject( int n
, wxClientData
* clientData
)
177 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
179 wxNode
*node
= m_clientObjectList
.Nth( n
);
180 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetClientObject") );
182 wxClientData
*cd
= (wxClientData
*) node
->Data();
185 node
->SetData( (wxObject
*) clientData
);
188 wxClientData
* wxChoice::DoGetClientObject( int n
) const
190 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid combobox") );
192 wxNode
*node
= m_clientObjectList
.Nth( n
);
193 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
194 wxT("invalid index in wxChoice::DoGetClientObject") );
196 return (wxClientData
*) node
->Data();
199 void wxChoice::Clear()
201 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
203 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
204 GtkWidget
*menu
= gtk_menu_new();
205 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
207 wxNode
*node
= m_clientObjectList
.First();
210 wxClientData
*cd
= (wxClientData
*)node
->Data();
214 m_clientObjectList
.Clear();
216 m_clientDataList
.Clear();
219 void wxChoice::Delete( int WXUNUSED(n
) )
221 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
224 int wxChoice::FindString( const wxString
&string
) const
226 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
228 // If you read this code once and you think you understand
229 // it, then you are very wrong. Robert Roebling.
231 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
233 GList
*child
= menu_shell
->children
;
236 GtkBin
*bin
= GTK_BIN( child
->data
);
237 GtkLabel
*label
= (GtkLabel
*) NULL
;
238 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
239 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
241 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
243 if (string
== wxString(label
->label
,*wxConvCurrent
))
253 int wxChoice::GetSelection() const
255 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
257 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
259 GList
*child
= menu_shell
->children
;
262 GtkBin
*bin
= GTK_BIN( child
->data
);
263 if (!bin
->child
) return count
;
271 wxString
wxChoice::GetString( int n
) const
273 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
275 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
277 GList
*child
= menu_shell
->children
;
280 GtkBin
*bin
= GTK_BIN( child
->data
);
283 GtkLabel
*label
= (GtkLabel
*) NULL
;
284 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
285 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
287 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
289 return wxString(label
->label
,*wxConvCurrent
);
295 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
300 int wxChoice::GetCount() const
302 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
304 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
306 GList
*child
= menu_shell
->children
;
315 void wxChoice::SetSelection( int n
)
317 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
320 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
323 void wxChoice::DisableEvents()
326 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
327 GList *child = menu_shell->children;
330 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
331 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
338 void wxChoice::EnableEvents()
341 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
342 GList *child = menu_shell->children;
345 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
346 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
353 void wxChoice::ApplyWidgetStyle()
357 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
359 gtk_widget_set_style( m_widget
, m_widgetStyle
);
360 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
362 GList
*child
= menu_shell
->children
;
365 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
367 GtkBin
*bin
= GTK_BIN( child
->data
);
368 GtkWidget
*label
= (GtkWidget
*) NULL
;
369 if (bin
->child
) label
= bin
->child
;
370 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
372 gtk_widget_set_style( label
, m_widgetStyle
);