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
)
41 if (g_isIdle
) wxapp_install_idle_handler();
43 if (!choice
->m_hasVMT
) return;
45 if (g_blockEventsOnDrag
) return;
47 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
48 event
.SetInt( choice
->GetSelection() );
49 event
.SetString( choice
->GetStringSelection() );
50 event
.SetEventObject(choice
);
51 choice
->GetEventHandler()->ProcessEvent(event
);
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
64 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
65 const wxPoint
&pos
, const wxSize
&size
,
66 int n
, const wxString choices
[],
67 long style
, const wxValidator
& validator
, const wxString
&name
)
70 #if (GTK_MINOR_VERSION > 0)
71 m_acceptsFocus
= TRUE
;
74 PreCreation( parent
, id
, pos
, size
, style
, name
);
77 SetValidator( validator
);
80 m_widget
= gtk_option_menu_new();
87 SetSize( newSize
.x
, newSize
.y
);
89 GtkWidget
*menu
= gtk_menu_new();
91 for (int i
= 0; i
< n
; i
++)
93 m_clientDataList
.Append( (wxObject
*) NULL
);
94 m_clientObjectList
.Append( (wxObject
*) NULL
);
96 GtkWidget
*item
= gtk_menu_item_new_with_label( choices
[i
].mbc_str() );
97 gtk_menu_append( GTK_MENU(menu
), item
);
99 gtk_widget_show( item
);
101 gtk_signal_connect( GTK_OBJECT( item
), "activate",
102 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
104 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
106 m_parent
->DoAddChild( this );
110 SetBackgroundColour( parent
->GetBackgroundColour() );
111 SetForegroundColour( parent
->GetForegroundColour() );
112 SetFont( parent
->GetFont() );
119 wxChoice::~wxChoice()
124 void wxChoice::AppendCommon( const wxString
&item
)
126 wxCHECK_RET( m_widget
!= NULL
, _T("invalid choice") );
128 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
129 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
131 gtk_menu_append( GTK_MENU(menu
), menu_item
);
133 if (GTK_WIDGET_REALIZED(m_widget
))
135 gtk_widget_realize( menu_item
);
136 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
138 if (m_widgetStyle
) ApplyWidgetStyle();
141 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
142 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
144 gtk_widget_show( menu_item
);
147 void wxChoice::Append( const wxString
&item
)
149 m_clientDataList
.Append( (wxObject
*) NULL
);
150 m_clientObjectList
.Append( (wxObject
*) NULL
);
152 AppendCommon( item
);
155 void wxChoice::Append( const wxString
&item
, void *clientData
)
157 m_clientDataList
.Append( (wxObject
*) clientData
);
158 m_clientObjectList
.Append( (wxObject
*) NULL
);
160 AppendCommon( item
);
163 void wxChoice::Append( const wxString
&item
, wxClientData
*clientData
)
165 m_clientObjectList
.Append( (wxObject
*) clientData
);
166 m_clientDataList
.Append( (wxObject
*) NULL
);
168 AppendCommon( item
);
171 void wxChoice::SetClientData( int n
, void* clientData
)
173 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
175 wxNode
*node
= m_clientDataList
.Nth( n
);
178 node
->SetData( (wxObject
*) clientData
);
181 void* wxChoice::GetClientData( int n
)
183 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
185 wxNode
*node
= m_clientDataList
.Nth( n
);
186 if (!node
) return NULL
;
191 void wxChoice::SetClientObject( int n
, wxClientData
* clientData
)
193 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
195 wxNode
*node
= m_clientObjectList
.Nth( n
);
198 wxClientData
*cd
= (wxClientData
*) node
->Data();
201 node
->SetData( (wxObject
*) clientData
);
204 wxClientData
* wxChoice::GetClientObject( int n
)
206 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, _T("invalid combobox") );
208 wxNode
*node
= m_clientObjectList
.Nth( n
);
209 if (!node
) return (wxClientData
*) NULL
;
211 return (wxClientData
*) node
->Data();
214 void wxChoice::Clear()
216 wxCHECK_RET( m_widget
!= NULL
, _T("invalid choice") );
218 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
219 GtkWidget
*menu
= gtk_menu_new();
220 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
222 wxNode
*node
= m_clientObjectList
.First();
225 wxClientData
*cd
= (wxClientData
*)node
->Data();
229 m_clientObjectList
.Clear();
231 m_clientDataList
.Clear();
234 void wxChoice::Delete( int WXUNUSED(n
) )
236 wxFAIL_MSG( _T("wxChoice:Delete not implemented") );
239 int wxChoice::FindString( const wxString
&string
) const
241 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid choice") );
243 // If you read this code once and you think you understand
244 // it, then you are very wrong. Robert Roebling.
246 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
248 GList
*child
= menu_shell
->children
;
251 GtkBin
*bin
= GTK_BIN( child
->data
);
252 GtkLabel
*label
= (GtkLabel
*) NULL
;
253 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
254 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
256 wxASSERT_MSG( label
!= NULL
, _T("wxChoice: invalid label") );
258 if (string
== wxString(label
->label
,*wxConvCurrent
))
268 int wxChoice::GetColumns() const
273 int wxChoice::GetSelection()
275 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid choice") );
277 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
279 GList
*child
= menu_shell
->children
;
282 GtkBin
*bin
= GTK_BIN( child
->data
);
283 if (!bin
->child
) return count
;
288 wxFAIL_MSG( _T("wxChoice: no selection") );
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
);
359 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
362 void wxChoice::SetStringSelection( const wxString
&string
)
364 wxCHECK_RET( m_widget
!= NULL
, _T("invalid choice") );
366 int n
= FindString( string
);
367 if (n
!= -1) SetSelection( n
);
370 void wxChoice::ApplyWidgetStyle()
374 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
376 gtk_widget_set_style( m_widget
, m_widgetStyle
);
377 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
379 GList
*child
= menu_shell
->children
;
382 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
384 GtkBin
*bin
= GTK_BIN( child
->data
);
385 GtkWidget
*label
= (GtkWidget
*) NULL
;
386 if (bin
->child
) label
= bin
->child
;
387 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
389 gtk_widget_set_style( label
, m_widgetStyle
);