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();
98 SetSize( newSize
.x
, newSize
.y
);
100 if ( style
& wxCB_SORT
)
102 // if our m_strings != NULL, DoAppend() will check for it and insert
103 // items in the correct order
104 m_strings
= new wxSortedArrayString
;
107 GtkWidget
*menu
= gtk_menu_new();
109 for (int i
= 0; i
< n
; i
++)
111 AppendHelper(menu
, choices
[i
]);
114 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
116 m_parent
->DoAddChild( this );
120 SetBackgroundColour( parent
->GetBackgroundColour() );
121 SetForegroundColour( parent
->GetForegroundColour() );
122 SetFont( parent
->GetFont() );
129 wxChoice::~wxChoice()
136 int wxChoice::DoAppend( const wxString
&item
)
138 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
140 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
142 return AppendHelper(menu
, item
);
145 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
147 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
149 wxNode
*node
= m_clientList
.Nth( n
);
150 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
152 node
->SetData( (wxObject
*) clientData
);
155 void* wxChoice::DoGetItemClientData( int n
) const
157 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
159 wxNode
*node
= m_clientList
.Nth( n
);
160 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
165 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
167 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
169 wxNode
*node
= m_clientList
.Nth( n
);
170 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
172 wxClientData
*cd
= (wxClientData
*) node
->Data();
175 node
->SetData( (wxObject
*) clientData
);
178 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
180 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
182 wxNode
*node
= m_clientList
.Nth( n
);
183 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
184 wxT("invalid index in wxChoice::DoGetItemClientObject") );
186 return (wxClientData
*) node
->Data();
189 void wxChoice::Clear()
191 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
193 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
194 GtkWidget
*menu
= gtk_menu_new();
195 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
197 if ( HasClientObjectData() )
199 // destroy the data (due to Robert's idea of using wxList<wxObject>
200 // and not wxList<wxClientData> we can't just say
201 // m_clientList.DeleteContents(TRUE) - this would crash!
202 wxNode
*node
= m_clientList
.First();
205 delete (wxClientData
*)node
->Data();
209 m_clientList
.Clear();
215 void wxChoice::Delete( int WXUNUSED(n
) )
217 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
220 int wxChoice::FindString( const wxString
&string
) const
222 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
224 // If you read this code once and you think you understand
225 // it, then you are very wrong. Robert Roebling.
227 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
229 GList
*child
= menu_shell
->children
;
232 GtkBin
*bin
= GTK_BIN( child
->data
);
233 GtkLabel
*label
= (GtkLabel
*) NULL
;
234 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
235 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
237 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
239 if (string
== wxString(label
->label
,*wxConvCurrent
))
249 int wxChoice::GetSelection() const
251 wxCHECK_MSG( m_widget
!= NULL
, -1, 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
);
259 if (!bin
->child
) return count
;
267 void wxChoice::SetString( int WXUNUSED(n
), const wxString
& WXUNUSED(string
) )
269 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
271 wxFAIL_MSG(wxT("not implemented"));
274 wxString
wxChoice::GetString( int n
) const
276 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
278 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
280 GList
*child
= menu_shell
->children
;
283 GtkBin
*bin
= GTK_BIN( child
->data
);
286 GtkLabel
*label
= (GtkLabel
*) NULL
;
287 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
288 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
290 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
292 return wxString(label
->label
,*wxConvCurrent
);
298 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
303 int wxChoice::GetCount() const
305 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
307 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
309 GList
*child
= menu_shell
->children
;
318 void wxChoice::SetSelection( int n
)
320 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
323 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
326 void wxChoice::DisableEvents()
329 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
330 GList *child = menu_shell->children;
333 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
334 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
341 void wxChoice::EnableEvents()
344 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
345 GList *child = menu_shell->children;
348 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
349 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
356 void wxChoice::ApplyWidgetStyle()
360 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
362 gtk_widget_set_style( m_widget
, m_widgetStyle
);
363 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
365 GList
*child
= menu_shell
->children
;
368 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
370 GtkBin
*bin
= GTK_BIN( child
->data
);
371 GtkWidget
*label
= (GtkWidget
*) NULL
;
372 if (bin
->child
) label
= bin
->child
;
373 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
375 gtk_widget_set_style( label
, m_widgetStyle
);
381 size_t wxChoice::AppendHelper(GtkWidget
*menu
, const wxString
& item
)
383 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
388 // sorted control, need to insert at the correct index
389 index
= m_strings
->Add(item
);
391 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
395 m_clientList
.Insert( m_clientList
.Item(index
- 1),
400 m_clientList
.Insert( (wxObject
*) NULL
);
405 // normal control, just append
406 gtk_menu_append( GTK_MENU(menu
), menu_item
);
408 m_clientList
.Append( (wxObject
*) NULL
);
410 // don't call wxChoice::GetCount() from here because it doesn't work
411 // if we're called from ctor (and GtkMenuShell is still NULL)
412 index
= m_clientList
.GetCount() - 1;
415 if (GTK_WIDGET_REALIZED(m_widget
))
417 gtk_widget_realize( menu_item
);
418 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
420 if (m_widgetStyle
) ApplyWidgetStyle();
423 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
424 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
426 gtk_widget_show( menu_item
);
428 // return the index of the item in the control