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"
17 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
21 extern bool g_blockEventsOnDrag
;
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
29 if (!choice
->HasVMT()) return;
30 if (g_blockEventsOnDrag
) return;
32 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
33 event
.SetInt( choice
->GetSelection() );
34 wxString
tmp( choice
->GetStringSelection() );
35 event
.SetString( WXSTRINGCAST(tmp
) );
36 event
.SetEventObject(choice
);
37 choice
->GetEventHandler()->ProcessEvent(event
);
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
46 wxChoice::wxChoice(void)
50 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
51 const wxPoint
&pos
, const wxSize
&size
,
52 int n
, const wxString choices
[],
53 long style
, const wxValidator
& validator
, const wxString
&name
)
57 PreCreation( parent
, id
, pos
, size
, style
, name
);
59 SetValidator( validator
);
61 m_widget
= gtk_option_menu_new();
63 wxSize newSize
= size
;
64 if (newSize
.x
== -1) newSize
.x
= 80;
65 if (newSize
.y
== -1) newSize
.y
= 26;
66 SetSize( newSize
.x
, newSize
.y
);
69 menu
= gtk_menu_new();
71 for (int i
= 0; i
< n
; i
++)
74 item
= gtk_menu_item_new_with_label( choices
[i
] );
75 gtk_signal_connect( GTK_OBJECT( item
), "activate",
76 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
77 gtk_menu_append( GTK_MENU(menu
), item
);
78 gtk_widget_show( item
);
80 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
89 void wxChoice::Append( const wxString
&item
)
91 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
93 menu_item
= gtk_menu_item_new_with_label( item
);
97 GtkBin
*bin
= GTK_BIN( menu_item
);
98 gtk_widget_set_style( bin
->child
,
100 gtk_widget_get_style( m_widget
) ) );
103 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
104 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
106 gtk_menu_append( GTK_MENU(menu
), menu_item
);
107 gtk_widget_show( menu_item
);
110 void wxChoice::Clear(void)
112 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
113 GtkWidget
*menu
= gtk_menu_new();
114 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
117 void wxChoice::Delete( int WXUNUSED(n
) )
119 wxFAIL_MSG( "wxChoice:Delete not implemented" );
122 int wxChoice::FindString( const wxString
&string
) const
124 // If you read this code once and you think you understand
125 // it, then you are very wrong. Robert Roebling.
127 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
129 GList
*child
= menu_shell
->children
;
132 GtkBin
*bin
= GTK_BIN( child
->data
);
133 GtkLabel
*label
= (GtkLabel
*) NULL
;
134 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
135 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
137 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
139 if (string
== label
->label
) return count
;
144 wxFAIL_MSG( "wxChoice: string not found" );
149 int wxChoice::GetColumns(void) const
154 int wxChoice::GetSelection(void)
156 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
158 GList
*child
= menu_shell
->children
;
161 GtkBin
*bin
= GTK_BIN( child
->data
);
162 if (!bin
->child
) return count
;
167 wxFAIL_MSG( "wxChoice: no selection" );
172 wxString
wxChoice::GetString( int n
) const
174 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
176 GList
*child
= menu_shell
->children
;
179 GtkBin
*bin
= GTK_BIN( child
->data
);
182 GtkLabel
*label
= (GtkLabel
*) NULL
;
183 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
184 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
186 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
194 wxFAIL_MSG( "wxChoice: string not found" );
199 wxString
wxChoice::GetStringSelection(void) const
201 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
203 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
208 int wxChoice::Number(void) const
210 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
212 GList
*child
= menu_shell
->children
;
221 void wxChoice::SetColumns( int WXUNUSED(n
) )
225 void wxChoice::SetSelection( int n
)
228 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
230 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
233 void wxChoice::SetStringSelection( const wxString
&string
)
235 int n
= FindString( string
);
236 if (n
!= -1) SetSelection( n
);
239 void wxChoice::SetFont( const wxFont
&font
)
241 wxWindow::SetFont( font
);
243 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
244 GList
*child
= menu_shell
->children
;
247 GtkBin
*bin
= GTK_BIN( child
->data
);
248 GtkWidget
*label
= (GtkWidget
*) NULL
;
249 if (bin
->child
) label
= bin
->child
;
250 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
252 gtk_widget_set_style( label
,
254 gtk_widget_get_style( m_widget
) ) );