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 int n
= choice
->GetSelection();
52 event
.SetString( choice
->GetStringSelection() );
53 event
.SetEventObject(choice
);
55 if ( choice
->HasClientObjectData() )
56 event
.SetClientObject( choice
->GetClientObject(n
) );
57 else if ( choice
->HasClientUntypedData() )
58 event
.SetClientData( choice
->GetClientData(n
) );
60 choice
->GetEventHandler()->ProcessEvent(event
);
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
71 m_strings
= (wxSortedArrayString
*)NULL
;
74 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
75 const wxPoint
&pos
, const wxSize
&size
,
76 int n
, const wxString choices
[],
77 long style
, const wxValidator
& validator
, const wxString
&name
)
80 #if (GTK_MINOR_VERSION > 0)
81 m_acceptsFocus
= TRUE
;
84 if (!PreCreation( parent
, pos
, size
) ||
85 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
87 wxFAIL_MSG( wxT("wxChoice creation failed") );
91 m_widget
= gtk_option_menu_new();
93 SetSizeOrDefault( size
);
95 if ( style
& wxCB_SORT
)
97 // if our m_strings != NULL, DoAppend() will check for it and insert
98 // items in the correct order
99 m_strings
= new wxSortedArrayString
;
102 GtkWidget
*menu
= gtk_menu_new();
104 for (int i
= 0; i
< n
; i
++)
106 AppendHelper(menu
, choices
[i
]);
109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
111 m_parent
->DoAddChild( this );
115 SetBackgroundColour( parent
->GetBackgroundColour() );
116 SetForegroundColour( parent
->GetForegroundColour() );
117 SetFont( parent
->GetFont() );
124 wxChoice::~wxChoice()
131 int wxChoice::DoAppend( const wxString
&item
)
133 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
135 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
137 return AppendHelper(menu
, item
);
140 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
142 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
144 wxNode
*node
= m_clientList
.Nth( n
);
145 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
147 node
->SetData( (wxObject
*) clientData
);
150 void* wxChoice::DoGetItemClientData( int n
) const
152 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
154 wxNode
*node
= m_clientList
.Nth( n
);
155 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
160 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
162 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
164 wxNode
*node
= m_clientList
.Nth( n
);
165 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
167 wxClientData
*cd
= (wxClientData
*) node
->Data();
170 node
->SetData( (wxObject
*) clientData
);
173 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
175 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
177 wxNode
*node
= m_clientList
.Nth( n
);
178 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
179 wxT("invalid index in wxChoice::DoGetItemClientObject") );
181 return (wxClientData
*) node
->Data();
184 void wxChoice::Clear()
186 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
188 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
189 GtkWidget
*menu
= gtk_menu_new();
190 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
192 if ( HasClientObjectData() )
194 // destroy the data (due to Robert's idea of using wxList<wxObject>
195 // and not wxList<wxClientData> we can't just say
196 // m_clientList.DeleteContents(TRUE) - this would crash!
197 wxNode
*node
= m_clientList
.First();
200 delete (wxClientData
*)node
->Data();
204 m_clientList
.Clear();
210 void wxChoice::Delete( int WXUNUSED(n
) )
212 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
215 int wxChoice::FindString( const wxString
&string
) const
217 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
219 // If you read this code once and you think you understand
220 // it, then you are very wrong. Robert Roebling.
222 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
224 GList
*child
= menu_shell
->children
;
227 GtkBin
*bin
= GTK_BIN( child
->data
);
228 GtkLabel
*label
= (GtkLabel
*) NULL
;
229 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
230 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
232 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
234 if (string
== wxString(label
->label
,*wxConvCurrent
))
244 int wxChoice::GetSelection() const
246 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
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 if (!bin
->child
) return count
;
262 void wxChoice::SetString( int WXUNUSED(n
), const wxString
& WXUNUSED(string
) )
264 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
266 wxFAIL_MSG(wxT("not implemented"));
269 wxString
wxChoice::GetString( int n
) const
271 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
273 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
275 GList
*child
= menu_shell
->children
;
278 GtkBin
*bin
= GTK_BIN( child
->data
);
281 GtkLabel
*label
= (GtkLabel
*) NULL
;
282 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
283 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
285 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
287 return wxString(label
->label
,*wxConvCurrent
);
293 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
298 int wxChoice::GetCount() const
300 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
302 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
304 GList
*child
= menu_shell
->children
;
313 void wxChoice::SetSelection( int n
)
315 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
318 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
321 void wxChoice::DisableEvents()
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_disconnect_by_func( GTK_OBJECT( child->data ),
329 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
336 void wxChoice::EnableEvents()
339 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
340 GList *child = menu_shell->children;
343 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
344 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
351 void wxChoice::ApplyWidgetStyle()
355 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
357 gtk_widget_set_style( m_widget
, m_widgetStyle
);
358 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
360 GList
*child
= menu_shell
->children
;
363 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
365 GtkBin
*bin
= GTK_BIN( child
->data
);
366 GtkWidget
*label
= (GtkWidget
*) NULL
;
367 if (bin
->child
) label
= bin
->child
;
368 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
370 gtk_widget_set_style( label
, m_widgetStyle
);
376 size_t wxChoice::AppendHelper(GtkWidget
*menu
, const wxString
& item
)
378 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
383 // sorted control, need to insert at the correct index
384 index
= m_strings
->Add(item
);
386 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
390 m_clientList
.Insert( m_clientList
.Item(index
- 1),
395 m_clientList
.Insert( (wxObject
*) NULL
);
400 // normal control, just append
401 gtk_menu_append( GTK_MENU(menu
), menu_item
);
403 m_clientList
.Append( (wxObject
*) NULL
);
405 // don't call wxChoice::GetCount() from here because it doesn't work
406 // if we're called from ctor (and GtkMenuShell is still NULL)
407 index
= m_clientList
.GetCount() - 1;
410 if (GTK_WIDGET_REALIZED(m_widget
))
412 gtk_widget_realize( menu_item
);
413 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
415 if (m_widgetStyle
) ApplyWidgetStyle();
418 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
419 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
421 gtk_widget_show( menu_item
);
423 // return the index of the item in the control
427 wxSize
wxChoice::DoGetBestSize() const
429 return wxSize(80, 26);