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 extern bool g_blockEventsOnDrag
;
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
30 if (!choice
->HasVMT()) return;
31 if (g_blockEventsOnDrag
) return;
33 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
34 event
.SetInt( choice
->GetSelection() );
35 wxString
tmp( choice
->GetStringSelection() );
36 event
.SetString( WXSTRINGCAST(tmp
) );
37 event
.SetEventObject(choice
);
38 choice
->GetEventHandler()->ProcessEvent(event
);
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
47 wxChoice::wxChoice(void)
51 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
52 const wxPoint
&pos
, const wxSize
&size
,
53 int n
, const wxString choices
[],
54 long style
, const wxValidator
& validator
, const wxString
&name
)
58 PreCreation( parent
, id
, pos
, size
, style
, name
);
60 SetValidator( validator
);
62 m_widget
= gtk_option_menu_new();
64 wxSize newSize
= size
;
65 if (newSize
.x
== -1) newSize
.x
= 80;
66 if (newSize
.y
== -1) newSize
.y
= 26;
67 SetSize( newSize
.x
, newSize
.y
);
70 menu
= gtk_menu_new();
72 for (int i
= 0; i
< n
; i
++)
75 item
= gtk_menu_item_new_with_label( choices
[i
] );
76 gtk_signal_connect( GTK_OBJECT( item
), "activate",
77 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
78 gtk_menu_append( GTK_MENU(menu
), item
);
79 gtk_widget_show( item
);
81 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
90 void wxChoice::Append( const wxString
&item
)
92 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
94 menu_item
= gtk_menu_item_new_with_label( item
);
98 GtkBin
*bin
= GTK_BIN( menu_item
);
99 gtk_widget_set_style( bin
->child
,
101 gtk_widget_get_style( m_widget
) ) );
104 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
105 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
107 gtk_menu_append( GTK_MENU(menu
), menu_item
);
108 gtk_widget_show( menu_item
);
111 void wxChoice::Clear(void)
113 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
114 GtkWidget
*menu
= gtk_menu_new();
115 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
118 void wxChoice::Delete( int WXUNUSED(n
) )
120 wxFAIL_MSG( "wxChoice:Delete not implemented" );
123 int wxChoice::FindString( const wxString
&string
) const
125 // If you read this code once and you think you understand
126 // it, then you are very wrong. Robert Roebling.
128 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
130 GList
*child
= menu_shell
->children
;
133 GtkBin
*bin
= GTK_BIN( child
->data
);
134 GtkLabel
*label
= (GtkLabel
*) NULL
;
135 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
136 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
138 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
140 if (string
== label
->label
) return count
;
145 wxFAIL_MSG( "wxChoice: string not found" );
150 int wxChoice::GetColumns(void) const
155 int wxChoice::GetSelection(void)
157 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
159 GList
*child
= menu_shell
->children
;
162 GtkBin
*bin
= GTK_BIN( child
->data
);
163 if (!bin
->child
) return count
;
168 wxFAIL_MSG( "wxChoice: no selection" );
173 wxString
wxChoice::GetString( int n
) const
175 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
177 GList
*child
= menu_shell
->children
;
180 GtkBin
*bin
= GTK_BIN( child
->data
);
183 GtkLabel
*label
= (GtkLabel
*) NULL
;
184 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
185 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
187 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
195 wxFAIL_MSG( "wxChoice: string not found" );
200 wxString
wxChoice::GetStringSelection(void) const
202 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
204 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
209 int wxChoice::Number(void) const
211 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
213 GList
*child
= menu_shell
->children
;
222 void wxChoice::SetColumns( int WXUNUSED(n
) )
226 void wxChoice::SetSelection( int n
)
229 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
231 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
234 void wxChoice::SetStringSelection( const wxString
&string
)
236 int n
= FindString( string
);
237 if (n
!= -1) SetSelection( n
);
240 void wxChoice::SetFont( const wxFont
&font
)
242 wxWindow::SetFont( font
);
244 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
245 GList
*child
= menu_shell
->children
;
248 GtkBin
*bin
= GTK_BIN( child
->data
);
249 GtkWidget
*label
= (GtkWidget
*) NULL
;
250 if (bin
->child
) label
= bin
->child
;
251 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
253 gtk_widget_set_style( label
,
255 gtk_widget_get_style( m_widget
) ) );