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( _T("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 void wxChoice::AppendCommon( const wxString
&item
)
128 wxCHECK_RET( m_widget
!= NULL
, _T("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
);
149 void wxChoice::Append( const wxString
&item
)
151 m_clientDataList
.Append( (wxObject
*) NULL
);
152 m_clientObjectList
.Append( (wxObject
*) NULL
);
154 AppendCommon( item
);
157 void wxChoice::Append( const wxString
&item
, void *clientData
)
159 m_clientDataList
.Append( (wxObject
*) clientData
);
160 m_clientObjectList
.Append( (wxObject
*) NULL
);
162 AppendCommon( item
);
165 void wxChoice::Append( const wxString
&item
, wxClientData
*clientData
)
167 m_clientObjectList
.Append( (wxObject
*) clientData
);
168 m_clientDataList
.Append( (wxObject
*) NULL
);
170 AppendCommon( item
);
173 void wxChoice::SetClientData( int n
, void* clientData
)
175 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
177 wxNode
*node
= m_clientDataList
.Nth( n
);
180 node
->SetData( (wxObject
*) clientData
);
183 void* wxChoice::GetClientData( int n
)
185 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
187 wxNode
*node
= m_clientDataList
.Nth( n
);
188 if (!node
) return NULL
;
193 void wxChoice::SetClientObject( int n
, wxClientData
* clientData
)
195 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
197 wxNode
*node
= m_clientObjectList
.Nth( n
);
200 wxClientData
*cd
= (wxClientData
*) node
->Data();
203 node
->SetData( (wxObject
*) clientData
);
206 wxClientData
* wxChoice::GetClientObject( int n
)
208 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, _T("invalid combobox") );
210 wxNode
*node
= m_clientObjectList
.Nth( n
);
211 if (!node
) return (wxClientData
*) NULL
;
213 return (wxClientData
*) node
->Data();
216 void wxChoice::Clear()
218 wxCHECK_RET( m_widget
!= NULL
, _T("invalid choice") );
220 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
221 GtkWidget
*menu
= gtk_menu_new();
222 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
224 wxNode
*node
= m_clientObjectList
.First();
227 wxClientData
*cd
= (wxClientData
*)node
->Data();
231 m_clientObjectList
.Clear();
233 m_clientDataList
.Clear();
236 void wxChoice::Delete( int WXUNUSED(n
) )
238 wxFAIL_MSG( _T("wxChoice:Delete not implemented") );
241 int wxChoice::FindString( const wxString
&string
) const
243 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid choice") );
245 // If you read this code once and you think you understand
246 // it, then you are very wrong. Robert Roebling.
248 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
250 GList
*child
= menu_shell
->children
;
253 GtkBin
*bin
= GTK_BIN( child
->data
);
254 GtkLabel
*label
= (GtkLabel
*) NULL
;
255 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
256 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
258 wxASSERT_MSG( label
!= NULL
, _T("wxChoice: invalid label") );
260 if (string
== wxString(label
->label
,*wxConvCurrent
))
270 int wxChoice::GetColumns() const
275 int wxChoice::GetSelection()
277 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid choice") );
279 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
281 GList
*child
= menu_shell
->children
;
284 GtkBin
*bin
= GTK_BIN( child
->data
);
285 if (!bin
->child
) return count
;
293 wxString
wxChoice::GetString( int n
) const
295 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid choice") );
297 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
299 GList
*child
= menu_shell
->children
;
302 GtkBin
*bin
= GTK_BIN( child
->data
);
305 GtkLabel
*label
= (GtkLabel
*) NULL
;
306 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
307 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
309 wxASSERT_MSG( label
!= NULL
, _T("wxChoice: invalid label") );
311 return wxString(label
->label
,*wxConvCurrent
);
317 wxFAIL_MSG( _T("wxChoice: invalid index in GetString()") );
322 wxString
wxChoice::GetStringSelection() const
324 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid choice") );
326 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
328 wxASSERT_MSG( label
!= NULL
, _T("wxChoice: invalid label") );
330 return wxString(label
->label
,*wxConvCurrent
);
333 int wxChoice::Number() const
335 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("invalid choice") );
337 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
339 GList
*child
= menu_shell
->children
;
348 void wxChoice::SetColumns( int WXUNUSED(n
) )
352 void wxChoice::SetSelection( int n
)
354 wxCHECK_RET( m_widget
!= NULL
, _T("invalid choice") );
357 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
360 void wxChoice::SetStringSelection( const wxString
&string
)
362 wxCHECK_RET( m_widget
!= NULL
, _T("invalid choice") );
364 int n
= FindString( string
);
365 if (n
!= -1) SetSelection( n
);
368 void wxChoice::DisableEvents()
371 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
372 GList *child = menu_shell->children;
375 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
376 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
383 void wxChoice::EnableEvents()
386 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
387 GList *child = menu_shell->children;
390 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
391 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
398 void wxChoice::ApplyWidgetStyle()
402 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
404 gtk_widget_set_style( m_widget
, m_widgetStyle
);
405 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
407 GList
*child
= menu_shell
->children
;
410 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
412 GtkBin
*bin
= GTK_BIN( child
->data
);
413 GtkWidget
*label
= (GtkWidget
*) NULL
;
414 if (bin
->child
) label
= bin
->child
;
415 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
417 gtk_widget_set_style( label
, m_widgetStyle
);