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
);
68 GtkWidget
*menu
= gtk_menu_new();
70 for (int i
= 0; i
< n
; i
++)
72 GtkWidget
*item
= gtk_menu_item_new_with_label( choices
[i
] );
73 gtk_signal_connect( GTK_OBJECT( item
), "activate",
74 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
76 gtk_menu_append( GTK_MENU(menu
), item
);
78 gtk_widget_show( item
);
79 gtk_widget_realize( item
);
80 gtk_widget_realize( GTK_BIN(item
)->child
);
82 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
86 SetBackgroundColour( parent
->GetBackgroundColour() );
93 void wxChoice::Append( const wxString
&item
)
95 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
97 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
98 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
);
100 gtk_menu_append( GTK_MENU(menu
), menu_item
);
102 gtk_widget_realize( menu_item
);
103 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
107 gtk_widget_set_style( menu_item
, m_widgetStyle
);
108 gtk_widget_set_style( GTK_BIN(menu_item
)->child
, m_widgetStyle
);
111 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
112 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
114 gtk_widget_show( menu_item
);
117 void wxChoice::Clear(void)
119 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
121 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
122 GtkWidget
*menu
= gtk_menu_new();
123 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
126 void wxChoice::Delete( int WXUNUSED(n
) )
128 wxFAIL_MSG( "wxChoice:Delete not implemented" );
131 int wxChoice::FindString( const wxString
&string
) const
133 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
135 // If you read this code once and you think you understand
136 // it, then you are very wrong. Robert Roebling.
138 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
140 GList
*child
= menu_shell
->children
;
143 GtkBin
*bin
= GTK_BIN( child
->data
);
144 GtkLabel
*label
= (GtkLabel
*) NULL
;
145 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
146 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
148 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
150 if (string
== label
->label
) return count
;
155 wxFAIL_MSG( "wxChoice: string not found" );
160 int wxChoice::GetColumns(void) const
165 int wxChoice::GetSelection(void)
167 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
169 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
171 GList
*child
= menu_shell
->children
;
174 GtkBin
*bin
= GTK_BIN( child
->data
);
175 if (!bin
->child
) return count
;
180 wxFAIL_MSG( "wxChoice: no selection" );
185 wxString
wxChoice::GetString( int n
) const
187 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
189 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
191 GList
*child
= menu_shell
->children
;
194 GtkBin
*bin
= GTK_BIN( child
->data
);
197 GtkLabel
*label
= (GtkLabel
*) NULL
;
198 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
199 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
201 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
209 wxFAIL_MSG( "wxChoice: string not found" );
214 wxString
wxChoice::GetStringSelection(void) const
216 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
218 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
220 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
225 int wxChoice::Number(void) const
227 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid choice" );
229 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
231 GList
*child
= menu_shell
->children
;
240 void wxChoice::SetColumns( int WXUNUSED(n
) )
244 void wxChoice::SetSelection( int n
)
246 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
249 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
251 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
254 void wxChoice::SetStringSelection( const wxString
&string
)
256 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
258 int n
= FindString( string
);
259 if (n
!= -1) SetSelection( n
);
262 void wxChoice::SetFont( const wxFont
&font
)
264 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
266 wxControl::SetFont( font
);
268 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
269 GList
*child
= menu_shell
->children
;
272 GtkBin
*bin
= GTK_BIN( child
->data
);
273 GtkWidget
*label
= (GtkWidget
*) NULL
;
274 if (bin
->child
) label
= bin
->child
;
275 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
277 gtk_widget_set_style( label
, GetWidgetStyle() );
283 void wxChoice::SetBackgroundColour( const wxColour
&colour
)
285 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
287 wxControl::SetBackgroundColour( colour
);
289 if (!m_backgroundColour
.Ok()) return;
291 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
293 gtk_widget_set_style( m_widget
, m_widgetStyle
);
294 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
296 GList
*child
= menu_shell
->children
;
299 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);