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
);
85 m_parent
->AddChild( this );
87 (m_parent
->m_insertCallback
)( m_parent
, this );
91 SetBackgroundColour( parent
->GetBackgroundColour() );
92 SetForegroundColour( parent
->GetForegroundColour() );
99 void wxChoice::Append( const wxString
&item
)
101 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
103 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
104 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
);
106 gtk_menu_append( GTK_MENU(menu
), menu_item
);
108 gtk_widget_realize( menu_item
);
109 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
111 if (m_widgetStyle
) ApplyWidgetStyle();
113 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
114 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
116 gtk_widget_show( menu_item
);
119 void wxChoice::Clear(void)
121 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
123 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
124 GtkWidget
*menu
= gtk_menu_new();
125 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
128 void wxChoice::Delete( int WXUNUSED(n
) )
130 wxFAIL_MSG( "wxChoice:Delete not implemented" );
133 int wxChoice::FindString( const wxString
&string
) const
135 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
137 // If you read this code once and you think you understand
138 // it, then you are very wrong. Robert Roebling.
140 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
142 GList
*child
= menu_shell
->children
;
145 GtkBin
*bin
= GTK_BIN( child
->data
);
146 GtkLabel
*label
= (GtkLabel
*) NULL
;
147 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
148 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
150 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
152 if (string
== label
->label
) return count
;
157 wxFAIL_MSG( "wxChoice: string not found" );
162 int wxChoice::GetColumns(void) const
167 int wxChoice::GetSelection(void)
169 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
171 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
173 GList
*child
= menu_shell
->children
;
176 GtkBin
*bin
= GTK_BIN( child
->data
);
177 if (!bin
->child
) return count
;
182 wxFAIL_MSG( "wxChoice: no selection" );
187 wxString
wxChoice::GetString( int n
) const
189 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
191 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
193 GList
*child
= menu_shell
->children
;
196 GtkBin
*bin
= GTK_BIN( child
->data
);
199 GtkLabel
*label
= (GtkLabel
*) NULL
;
200 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
201 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
203 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
211 wxFAIL_MSG( "wxChoice: string not found" );
216 wxString
wxChoice::GetStringSelection(void) const
218 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
220 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
222 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
227 int wxChoice::Number(void) const
229 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid choice" );
231 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
233 GList
*child
= menu_shell
->children
;
242 void wxChoice::SetColumns( int WXUNUSED(n
) )
246 void wxChoice::SetSelection( int n
)
248 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
251 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
253 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
256 void wxChoice::SetStringSelection( const wxString
&string
)
258 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
260 int n
= FindString( string
);
261 if (n
!= -1) SetSelection( n
);
264 void wxChoice::ApplyWidgetStyle()
268 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
270 gtk_widget_set_style( m_widget
, m_widgetStyle
);
271 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
273 GList
*child
= menu_shell
->children
;
276 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
278 GtkBin
*bin
= GTK_BIN( child
->data
);
279 GtkWidget
*label
= (GtkWidget
*) NULL
;
280 if (bin
->child
) label
= bin
->child
;
281 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
283 gtk_widget_set_style( label
, m_widgetStyle
);