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_menu_append( GTK_MENU(menu
), item
);
75 gtk_widget_realize( item
);
76 gtk_widget_realize( GTK_BIN(item
)->child
);
78 gtk_widget_show( item
);
80 gtk_signal_connect( GTK_OBJECT( item
), "activate",
81 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
83 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
87 SetBackgroundColour( parent
->GetBackgroundColour() );
88 SetForegroundColour( parent
->GetForegroundColour() );
95 void wxChoice::Append( const wxString
&item
)
97 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
99 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
100 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
);
102 gtk_menu_append( GTK_MENU(menu
), menu_item
);
104 gtk_widget_realize( menu_item
);
105 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
107 if (m_widgetStyle
) ApplyWidgetStyle();
109 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
110 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
112 gtk_widget_show( menu_item
);
115 void wxChoice::Clear(void)
117 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
119 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
120 GtkWidget
*menu
= gtk_menu_new();
121 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
124 void wxChoice::Delete( int WXUNUSED(n
) )
126 wxFAIL_MSG( "wxChoice:Delete not implemented" );
129 int wxChoice::FindString( const wxString
&string
) const
131 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
133 // If you read this code once and you think you understand
134 // it, then you are very wrong. Robert Roebling.
136 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
138 GList
*child
= menu_shell
->children
;
141 GtkBin
*bin
= GTK_BIN( child
->data
);
142 GtkLabel
*label
= (GtkLabel
*) NULL
;
143 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
144 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
146 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
148 if (string
== label
->label
) return count
;
153 wxFAIL_MSG( "wxChoice: string not found" );
158 int wxChoice::GetColumns(void) const
163 int wxChoice::GetSelection(void)
165 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
167 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
169 GList
*child
= menu_shell
->children
;
172 GtkBin
*bin
= GTK_BIN( child
->data
);
173 if (!bin
->child
) return count
;
178 wxFAIL_MSG( "wxChoice: no selection" );
183 wxString
wxChoice::GetString( int n
) const
185 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
187 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
189 GList
*child
= menu_shell
->children
;
192 GtkBin
*bin
= GTK_BIN( child
->data
);
195 GtkLabel
*label
= (GtkLabel
*) NULL
;
196 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
197 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
199 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
207 wxFAIL_MSG( "wxChoice: string not found" );
212 wxString
wxChoice::GetStringSelection(void) const
214 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
216 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
218 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
223 int wxChoice::Number(void) const
225 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid choice" );
227 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
229 GList
*child
= menu_shell
->children
;
238 void wxChoice::SetColumns( int WXUNUSED(n
) )
242 void wxChoice::SetSelection( int n
)
244 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
247 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
249 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
252 void wxChoice::SetStringSelection( const wxString
&string
)
254 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
256 int n
= FindString( string
);
257 if (n
!= -1) SetSelection( n
);
260 void wxChoice::ApplyWidgetStyle()
264 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
266 gtk_widget_set_style( m_widget
, m_widgetStyle
);
267 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
269 GList
*child
= menu_shell
->children
;
272 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
274 GtkBin
*bin
= GTK_BIN( child
->data
);
275 GtkWidget
*label
= (GtkWidget
*) NULL
;
276 if (bin
->child
) label
= bin
->child
;
277 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
279 gtk_widget_set_style( label
, m_widgetStyle
);