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") );
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 combobox") );
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 combobox") );
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 combobox") );
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 combobox") );
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
)
191 wxNode
*node
= m_clientList
.First();
194 wxClientData
*cd
= (wxClientData
*)node
->Data();
199 m_clientList
.Clear();
202 void wxChoice::Delete( int WXUNUSED(n
) )
204 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
207 int wxChoice::FindString( const wxString
&string
) const
209 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
211 // If you read this code once and you think you understand
212 // it, then you are very wrong. Robert Roebling.
214 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
216 GList
*child
= menu_shell
->children
;
219 GtkBin
*bin
= GTK_BIN( child
->data
);
220 GtkLabel
*label
= (GtkLabel
*) NULL
;
221 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
222 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
224 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
226 if (string
== wxString(label
->label
,*wxConvCurrent
))
236 int wxChoice::GetSelection() const
238 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
240 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
242 GList
*child
= menu_shell
->children
;
245 GtkBin
*bin
= GTK_BIN( child
->data
);
246 if (!bin
->child
) return count
;
254 wxString
wxChoice::GetString( int n
) const
256 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
258 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
260 GList
*child
= menu_shell
->children
;
263 GtkBin
*bin
= GTK_BIN( child
->data
);
266 GtkLabel
*label
= (GtkLabel
*) NULL
;
267 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
268 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
270 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
272 return wxString(label
->label
,*wxConvCurrent
);
278 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
283 int wxChoice::GetCount() const
285 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
287 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
289 GList
*child
= menu_shell
->children
;
298 void wxChoice::SetSelection( int n
)
300 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
303 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
306 void wxChoice::DisableEvents()
309 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
310 GList *child = menu_shell->children;
313 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
314 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
321 void wxChoice::EnableEvents()
324 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
325 GList *child = menu_shell->children;
328 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
329 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
336 void wxChoice::ApplyWidgetStyle()
340 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
342 gtk_widget_set_style( m_widget
, m_widgetStyle
);
343 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
345 GList
*child
= menu_shell
->children
;
348 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
350 GtkBin
*bin
= GTK_BIN( child
->data
);
351 GtkWidget
*label
= (GtkWidget
*) NULL
;
352 if (bin
->child
) label
= bin
->child
;
353 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
355 gtk_widget_set_style( label
, m_widgetStyle
);
361 size_t wxChoice::AppendHelper(GtkWidget
*menu
, const wxString
& item
)
363 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
368 // sorted control, need to insert at the correct index
369 index
= m_strings
->Add(item
);
371 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
375 m_clientList
.Insert( m_clientList
.Item(index
- 1),
380 // can't use Insert() :-(
381 m_clientList
.Append( (wxObject
*) NULL
);
386 // normal control, just append
387 gtk_menu_append( GTK_MENU(menu
), menu_item
);
389 m_clientList
.Append( (wxObject
*) NULL
);
391 // don't call wxChoice::GetCount() from here because it doesn't work
392 // if we're called from ctor (and GtkMenuShell is still NULL)
393 index
= m_clientList
.GetCount();
396 if (GTK_WIDGET_REALIZED(m_widget
))
398 gtk_widget_realize( menu_item
);
399 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
401 if (m_widgetStyle
) ApplyWidgetStyle();
404 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
405 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
407 gtk_widget_show( menu_item
);
409 // return the index of the item in the control