1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "choice.h"
16 #include "wx/choice.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), gpointer data
)
24 wxChoice
*choice
= (wxChoice
*)data
;
25 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId());
26 event
.SetInt( choice
->GetSelection() );
27 wxString
tmp( choice
->GetStringSelection() );
28 event
.SetString( WXSTRINGCAST(tmp
) );
29 event
.SetEventObject(choice
);
30 choice
->ProcessEvent(event
);
33 //-----------------------------------------------------------------------------
35 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
37 wxChoice::wxChoice(void)
41 wxChoice::wxChoice( wxWindow
*parent
, const wxWindowID id
,
42 const wxPoint
&pos
, const wxSize
&size
,
43 const int n
, const wxString choices
[],
44 const long style
, const wxString
&name
)
46 Create( parent
, id
, pos
, size
, n
, choices
, style
, name
);
49 bool wxChoice::Create( wxWindow
*parent
, const wxWindowID id
,
50 const wxPoint
&pos
, const wxSize
&size
,
51 const int n
, const wxString choices
[],
52 const long style
, const wxString
&name
)
56 PreCreation( parent
, id
, pos
, size
, style
, name
);
58 m_widget
= gtk_option_menu_new();
60 wxSize newSize
= size
;
61 if (newSize
.x
== -1) newSize
.x
= 80;
62 if (newSize
.y
== -1) newSize
.y
= 26;
63 SetSize( newSize
.x
, newSize
.y
);
66 menu
= gtk_menu_new();
68 for (int i
= 0; i
< n
; i
++)
71 item
= gtk_menu_item_new_with_label( choices
[i
] );
72 gtk_signal_connect( GTK_OBJECT( item
), "activate",
73 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
74 gtk_menu_append( GTK_MENU(menu
), item
);
75 gtk_widget_show( item
);
77 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
86 void wxChoice::Append( const wxString
&item
)
88 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
90 menu_item
= gtk_menu_item_new_with_label( item
);
91 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
92 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
93 gtk_menu_append( GTK_MENU(menu
), menu_item
);
94 gtk_widget_show( menu_item
);
97 void wxChoice::Clear(void)
99 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
100 GtkWidget
*menu
= gtk_menu_new();
101 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
104 int wxChoice::FindString( const wxString
&string
) const
106 // If you read this code once and you think you undestand
107 // it, then you are very wrong. RR
109 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
111 GList
*child
= menu_shell
->children
;
114 GtkBin
*bin
= GTK_BIN( child
->data
);
115 GtkLabel
*label
= GTK_LABEL(bin
->child
);
116 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
117 if (string
== label
->label
) return count
;
124 int wxChoice::GetColumns(void) const
129 int wxChoice::GetSelection(void)
131 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
133 GList
*child
= menu_shell
->children
;
136 GtkBin
*bin
= GTK_BIN( child
->data
);
137 if (!bin
->child
) return count
;
144 wxString
wxChoice::GetString( const int n
) const
146 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
148 GList
*child
= menu_shell
->children
;
151 GtkBin
*bin
= GTK_BIN( child
->data
);
154 GtkLabel
*label
= GTK_LABEL(bin
->child
);
155 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
164 wxString
wxChoice::GetStringSelection(void) const
166 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
170 int wxChoice::Number(void) const
172 GtkMenu
*menu
= GTK_MENU( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
174 GList
*child
= menu
->children
;
183 void wxChoice::SetColumns( const int WXUNUSED(n
) )
187 void wxChoice::SetSelection( const int n
)
190 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
193 void wxChoice::SetStringSelection( const wxString
&string
)
195 int n
= FindString( string
);
196 if (n
!= -1) SetSelection( n
);