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 GtkAppendHelper(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 GtkAppendHelper(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::ApplyWidgetStyle()
325 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
327 gtk_widget_set_style( m_widget
, m_widgetStyle
);
328 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
330 GList
*child
= menu_shell
->children
;
333 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
335 GtkBin
*bin
= GTK_BIN( child
->data
);
336 GtkWidget
*label
= (GtkWidget
*) NULL
;
337 if (bin
->child
) label
= bin
->child
;
338 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
340 gtk_widget_set_style( label
, m_widgetStyle
);
346 size_t wxChoice::GtkAppendHelper(GtkWidget
*menu
, const wxString
& item
)
348 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
353 // sorted control, need to insert at the correct index
354 index
= m_strings
->Add(item
);
356 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
360 m_clientList
.Insert( m_clientList
.Item(index
- 1),
365 m_clientList
.Insert( (wxObject
*) NULL
);
370 // normal control, just append
371 gtk_menu_append( GTK_MENU(menu
), menu_item
);
373 m_clientList
.Append( (wxObject
*) NULL
);
375 // don't call wxChoice::GetCount() from here because it doesn't work
376 // if we're called from ctor (and GtkMenuShell is still NULL)
377 index
= m_clientList
.GetCount() - 1;
380 if (GTK_WIDGET_REALIZED(m_widget
))
382 gtk_widget_realize( menu_item
);
383 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
385 if (m_widgetStyle
) ApplyWidgetStyle();
388 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
389 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
391 gtk_widget_show( menu_item
);
393 // return the index of the item in the control
397 wxSize
wxChoice::DoGetBestSize() const
399 return wxSize(80, 26);