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
)
63 m_strings
= (wxSortedArrayString
*)NULL
;
66 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
67 const wxPoint
&pos
, const wxSize
&size
,
68 int n
, const wxString choices
[],
69 long style
, const wxValidator
& validator
, const wxString
&name
)
72 #if (GTK_MINOR_VERSION > 0)
73 m_acceptsFocus
= TRUE
;
76 if (!PreCreation( parent
, pos
, size
) ||
77 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
79 wxFAIL_MSG( wxT("wxChoice creation failed") );
83 m_widget
= gtk_option_menu_new();
90 SetSize( newSize
.x
, newSize
.y
);
92 if ( style
& wxCB_SORT
)
94 // if our m_strings != NULL, DoAppend() will check for it and insert
95 // items in the correct order
96 m_strings
= new wxSortedArrayString
;
99 GtkWidget
*menu
= gtk_menu_new();
101 for (int i
= 0; i
< n
; i
++)
103 AppendHelper(menu
, choices
[i
]);
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()
128 int wxChoice::DoAppend( const wxString
&item
)
130 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
132 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
134 return AppendHelper(menu
, item
);
137 void wxChoice::DoSetClientData( int n
, void* clientData
)
139 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
141 wxNode
*node
= m_clientList
.Nth( n
);
142 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetClientData") );
144 node
->SetData( (wxObject
*) clientData
);
147 void* wxChoice::DoGetClientData( int n
) const
149 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
151 wxNode
*node
= m_clientList
.Nth( n
);
152 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetClientData") );
157 void wxChoice::DoSetClientObject( int n
, wxClientData
* clientData
)
159 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
161 wxNode
*node
= m_clientList
.Nth( n
);
162 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetClientObject") );
164 wxClientData
*cd
= (wxClientData
*) node
->Data();
167 node
->SetData( (wxObject
*) clientData
);
170 wxClientData
* wxChoice::DoGetClientObject( int n
) const
172 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
174 wxNode
*node
= m_clientList
.Nth( n
);
175 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
176 wxT("invalid index in wxChoice::DoGetClientObject") );
178 return (wxClientData
*) node
->Data();
181 void wxChoice::Clear()
183 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
185 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
186 GtkWidget
*menu
= gtk_menu_new();
187 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
189 if ( m_clientDataItemsType
== ClientData_Object
)
190 m_clientList
.DeleteContents(TRUE
);
191 m_clientList
.Clear();
197 void wxChoice::Delete( int WXUNUSED(n
) )
199 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
202 int wxChoice::FindString( const wxString
&string
) const
204 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
206 // If you read this code once and you think you understand
207 // it, then you are very wrong. Robert Roebling.
209 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
211 GList
*child
= menu_shell
->children
;
214 GtkBin
*bin
= GTK_BIN( child
->data
);
215 GtkLabel
*label
= (GtkLabel
*) NULL
;
216 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
217 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
219 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
221 if (string
== wxString(label
->label
,*wxConvCurrent
))
231 int wxChoice::GetSelection() const
233 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
235 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
237 GList
*child
= menu_shell
->children
;
240 GtkBin
*bin
= GTK_BIN( child
->data
);
241 if (!bin
->child
) return count
;
249 wxString
wxChoice::GetString( int n
) const
251 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
253 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
255 GList
*child
= menu_shell
->children
;
258 GtkBin
*bin
= GTK_BIN( child
->data
);
261 GtkLabel
*label
= (GtkLabel
*) NULL
;
262 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
263 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
265 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
267 return wxString(label
->label
,*wxConvCurrent
);
273 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
278 int wxChoice::GetCount() const
280 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
282 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
284 GList
*child
= menu_shell
->children
;
293 void wxChoice::SetSelection( int n
)
295 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
298 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
301 void wxChoice::DisableEvents()
304 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
305 GList *child = menu_shell->children;
308 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
309 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
316 void wxChoice::EnableEvents()
319 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
320 GList *child = menu_shell->children;
323 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
324 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
331 void wxChoice::ApplyWidgetStyle()
335 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
337 gtk_widget_set_style( m_widget
, m_widgetStyle
);
338 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
340 GList
*child
= menu_shell
->children
;
343 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
345 GtkBin
*bin
= GTK_BIN( child
->data
);
346 GtkWidget
*label
= (GtkWidget
*) NULL
;
347 if (bin
->child
) label
= bin
->child
;
348 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
350 gtk_widget_set_style( label
, m_widgetStyle
);
356 size_t wxChoice::AppendHelper(GtkWidget
*menu
, const wxString
& item
)
358 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
363 // sorted control, need to insert at the correct index
364 index
= m_strings
->Add(item
);
366 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
370 m_clientList
.Insert( m_clientList
.Item(index
- 1),
375 // can't use Insert() :-(
376 m_clientList
.Append( (wxObject
*) NULL
);
381 // normal control, just append
382 gtk_menu_append( GTK_MENU(menu
), menu_item
);
384 m_clientList
.Append( (wxObject
*) NULL
);
386 // don't call wxChoice::GetCount() from here because it doesn't work
387 // if we're called from ctor (and GtkMenuShell is still NULL)
388 index
= m_clientList
.GetCount();
391 if (GTK_WIDGET_REALIZED(m_widget
))
393 gtk_widget_realize( menu_item
);
394 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
396 if (m_widgetStyle
) ApplyWidgetStyle();
399 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
400 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
402 gtk_widget_show( menu_item
);
404 // return the index of the item in the control