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
), wxChoice
*choice
)
24 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
25 event
.SetInt( choice
->GetSelection() );
26 wxString
tmp( choice
->GetStringSelection() );
27 event
.SetString( WXSTRINGCAST(tmp
) );
28 event
.SetEventObject(choice
);
29 choice
->GetEventHandler()->ProcessEvent(event
);
32 //-----------------------------------------------------------------------------
34 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
36 wxChoice::wxChoice(void)
40 wxChoice::wxChoice( wxWindow
*parent
, wxWindowID id
,
41 const wxPoint
&pos
, const wxSize
&size
,
42 int n
, const wxString choices
[],
43 long style
, const wxString
&name
)
45 Create( parent
, id
, pos
, size
, n
, choices
, style
, name
);
48 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
49 const wxPoint
&pos
, const wxSize
&size
,
50 int n
, const wxString choices
[],
51 long style
, const wxString
&name
)
55 PreCreation( parent
, id
, pos
, size
, style
, name
);
57 m_widget
= gtk_option_menu_new();
59 wxSize newSize
= size
;
60 if (newSize
.x
== -1) newSize
.x
= 80;
61 if (newSize
.y
== -1) newSize
.y
= 26;
62 SetSize( newSize
.x
, newSize
.y
);
65 menu
= gtk_menu_new();
67 for (int i
= 0; i
< n
; i
++)
70 item
= gtk_menu_item_new_with_label( choices
[i
] );
71 gtk_signal_connect( GTK_OBJECT( item
), "activate",
72 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
73 gtk_menu_append( GTK_MENU(menu
), item
);
74 gtk_widget_show( item
);
76 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
85 void wxChoice::Append( const wxString
&item
)
87 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
89 menu_item
= gtk_menu_item_new_with_label( item
);
90 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
91 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
92 gtk_menu_append( GTK_MENU(menu
), menu_item
);
93 gtk_widget_show( menu_item
);
96 void wxChoice::Clear(void)
98 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
99 GtkWidget
*menu
= gtk_menu_new();
100 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
103 int wxChoice::FindString( const wxString
&string
) const
105 // If you read this code once and you think you understand
106 // it, then you are very wrong. Robert Roebling.
108 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
110 GList
*child
= menu_shell
->children
;
113 GtkBin
*bin
= GTK_BIN( child
->data
);
114 GtkLabel
*label
= NULL
;
115 if (bin
->child
) 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( 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
= NULL
;
155 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
156 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
165 wxString
wxChoice::GetStringSelection(void) const
167 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
171 int wxChoice::Number(void) const
173 GtkMenu
*menu
= GTK_MENU( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
175 GList
*child
= menu
->children
;
184 void wxChoice::SetColumns( int WXUNUSED(n
) )
188 void wxChoice::SetSelection( int n
)
191 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
193 gtk_choice_clicked_callback( NULL
, this );
196 void wxChoice::SetStringSelection( const wxString
&string
)
198 int n
= FindString( string
);
199 if (n
!= -1) SetSelection( n
);