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"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern void wxapp_install_idle_handler();
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern bool g_blockEventsOnDrag
;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
39 if (g_isIdle
) wxapp_install_idle_handler();
41 if (!choice
->HasVMT()) return;
43 if (g_blockEventsOnDrag
) return;
45 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
46 event
.SetInt( choice
->GetSelection() );
47 event
.SetString( choice
->GetStringSelection() );
48 event
.SetEventObject(choice
);
49 choice
->GetEventHandler()->ProcessEvent(event
);
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
62 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
63 const wxPoint
&pos
, const wxSize
&size
,
64 int n
, const wxString choices
[],
65 long style
, const wxValidator
& validator
, const wxString
&name
)
68 #if (GTK_MINOR_VERSION > 0)
69 m_acceptsFocus
= TRUE
;
72 PreCreation( parent
, id
, pos
, size
, style
, name
);
74 SetValidator( validator
);
76 m_widget
= gtk_option_menu_new();
83 SetSize( newSize
.x
, newSize
.y
);
85 GtkWidget
*menu
= gtk_menu_new();
87 for (int i
= 0; i
< n
; i
++)
89 m_clientDataList
.Append( (wxObject
*) NULL
);
90 m_clientObjectList
.Append( (wxObject
*) NULL
);
92 GtkWidget
*item
= gtk_menu_item_new_with_label( choices
[i
].mbc_str() );
93 gtk_menu_append( GTK_MENU(menu
), item
);
95 gtk_widget_show( item
);
97 gtk_signal_connect( GTK_OBJECT( item
), "activate",
98 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
100 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
102 m_parent
->AddChild( this );
104 (m_parent
->m_insertCallback
)( m_parent
, this );
108 SetBackgroundColour( parent
->GetBackgroundColour() );
109 SetForegroundColour( parent
->GetForegroundColour() );
110 SetFont( parent
->GetFont() );
117 wxChoice::~wxChoice()
122 void wxChoice::AppendCommon( const wxString
&item
)
124 wxCHECK_RET( m_widget
!= NULL
, _T("invalid choice") );
126 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
127 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
129 gtk_menu_append( GTK_MENU(menu
), menu_item
);
131 if (GTK_WIDGET_REALIZED(m_widget
))
133 gtk_widget_realize( menu_item
);
134 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
136 if (m_widgetStyle
) ApplyWidgetStyle();
139 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
140 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
142 gtk_widget_show( menu_item
);
145 void wxChoice::Append( const wxString
&item
)
147 m_clientDataList
.Append( (wxObject
*) NULL
);
148 m_clientObjectList
.Append( (wxObject
*) NULL
);
150 AppendCommon( item
);
153 void wxChoice::Append( const wxString
&item
, void *clientData
)
155 m_clientDataList
.Append( (wxObject
*) clientData
);
156 m_clientObjectList
.Append( (wxObject
*) NULL
);
158 AppendCommon( item
);
161 void wxChoice::Append( const wxString
&item
, wxClientData
*clientData
)
163 m_clientObjectList
.Append( (wxObject
*) clientData
);
164 m_clientDataList
.Append( (wxObject
*) NULL
);
166 AppendCommon( item
);
169 void wxChoice::SetClientData( int n
, void* clientData
)
171 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
173 wxNode
*node
= m_clientDataList
.Nth( n
);
176 node
->SetData( (wxObject
*) clientData
);
179 void* wxChoice::GetClientData( int n
)
181 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
183 wxNode
*node
= m_clientDataList
.Nth( n
);
184 if (!node
) return NULL
;
189 void wxChoice::SetClientObject( int n
, wxClientData
* clientData
)
191 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
193 wxNode
*node
= m_clientObjectList
.Nth( n
);
196 wxClientData
*cd
= (wxClientData
*) node
->Data();
199 node
->SetData( (wxObject
*) clientData
);
202 wxClientData
* wxChoice::GetClientObject( int n
)
204 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, _T("invalid combobox") );
206 wxNode
*node
= m_clientObjectList
.Nth( n
);
207 if (!node
) return (wxClientData
*) NULL
;
209 return (wxClientData
*) node
->Data();
212 void wxChoice::Clear()
214 wxCHECK_RET( m_widget
!= NULL
, _T("invalid choice") );
216 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
217 GtkWidget
*menu
= gtk_menu_new();
218 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
220 wxNode
*node
= m_clientObjectList
.First();
223 wxClientData
*cd
= (wxClientData
*)node
->Data();
227 m_clientObjectList
.Clear();
229 m_clientDataList
.Clear();
232 void wxChoice::Delete( int WXUNUSED(n
) )
234 wxFAIL_MSG( _T("wxChoice:Delete not implemented") );
237 int wxChoice::FindString( const wxString
&string
) const
239 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid choice") );
241 // If you read this code once and you think you understand
242 // it, then you are very wrong. Robert Roebling.
244 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
246 GList
*child
= menu_shell
->children
;
249 GtkBin
*bin
= GTK_BIN( child
->data
);
250 GtkLabel
*label
= (GtkLabel
*) NULL
;
251 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
252 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
254 wxASSERT_MSG( label
!= NULL
, _T("wxChoice: invalid label") );
256 if (string
== label
->label
)
266 int wxChoice::GetColumns() const
271 int wxChoice::GetSelection()
273 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("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
);
281 if (!bin
->child
) return count
;
286 wxFAIL_MSG( _T("wxChoice: no selection") );
291 wxString
wxChoice::GetString( int n
) const
293 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid choice") );
295 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
297 GList
*child
= menu_shell
->children
;
300 GtkBin
*bin
= GTK_BIN( child
->data
);
303 GtkLabel
*label
= (GtkLabel
*) NULL
;
304 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
305 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
307 wxASSERT_MSG( label
!= NULL
, _T("wxChoice: invalid label") );
315 wxFAIL_MSG( _T("wxChoice: invalid index in GetString()") );
320 wxString
wxChoice::GetStringSelection() const
322 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid choice") );
324 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
326 wxASSERT_MSG( label
!= NULL
, _T("wxChoice: invalid label") );
331 int wxChoice::Number() const
333 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("invalid choice") );
335 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
337 GList
*child
= menu_shell
->children
;
346 void wxChoice::SetColumns( int WXUNUSED(n
) )
350 void wxChoice::SetSelection( int n
)
352 wxCHECK_RET( m_widget
!= NULL
, _T("invalid choice") );
355 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
357 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
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::ApplyWidgetStyle()
372 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
374 gtk_widget_set_style( m_widget
, m_widgetStyle
);
375 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
377 GList
*child
= menu_shell
->children
;
380 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
382 GtkBin
*bin
= GTK_BIN( child
->data
);
383 GtkWidget
*label
= (GtkWidget
*) NULL
;
384 if (bin
->child
) label
= bin
->child
;
385 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
387 gtk_widget_set_style( label
, m_widgetStyle
);