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 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
45 wxChoice::wxChoice(void)
49 wxChoice::wxChoice( wxWindow
*parent
, wxWindowID id
,
50 const wxPoint
&pos
, const wxSize
&size
,
51 int n
, const wxString choices
[],
52 long style
, const wxString
&name
)
54 Create( parent
, id
, pos
, size
, n
, choices
, style
, name
);
57 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
58 const wxPoint
&pos
, const wxSize
&size
,
59 int n
, const wxString choices
[],
60 long style
, const wxString
&name
)
64 PreCreation( parent
, id
, pos
, size
, style
, name
);
66 m_widget
= gtk_option_menu_new();
68 wxSize newSize
= size
;
69 if (newSize
.x
== -1) newSize
.x
= 80;
70 if (newSize
.y
== -1) newSize
.y
= 26;
71 SetSize( newSize
.x
, newSize
.y
);
74 menu
= gtk_menu_new();
76 for (int i
= 0; i
< n
; i
++)
79 item
= gtk_menu_item_new_with_label( choices
[i
] );
80 gtk_signal_connect( GTK_OBJECT( item
), "activate",
81 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
82 gtk_menu_append( GTK_MENU(menu
), item
);
83 gtk_widget_show( item
);
85 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
94 void wxChoice::Append( const wxString
&item
)
96 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
98 menu_item
= gtk_menu_item_new_with_label( item
);
99 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
100 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
101 gtk_menu_append( GTK_MENU(menu
), menu_item
);
102 gtk_widget_show( menu_item
);
105 void wxChoice::Clear(void)
107 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
108 GtkWidget
*menu
= gtk_menu_new();
109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
112 int wxChoice::FindString( const wxString
&string
) const
114 // If you read this code once and you think you understand
115 // it, then you are very wrong. Robert Roebling.
117 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
119 GList
*child
= menu_shell
->children
;
122 GtkBin
*bin
= GTK_BIN( child
->data
);
123 GtkLabel
*label
= NULL
;
124 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
126 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
128 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
129 if (string
== label
->label
) return count
;
134 wxFAIL_MSG( "wxChoice: string not found" );
139 int wxChoice::GetColumns(void) const
144 int wxChoice::GetSelection(void)
146 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
148 GList
*child
= menu_shell
->children
;
151 GtkBin
*bin
= GTK_BIN( child
->data
);
152 if (!bin
->child
) return count
;
157 wxFAIL_MSG( "wxChoice: no selection" );
162 wxString
wxChoice::GetString( int n
) const
164 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
166 GList
*child
= menu_shell
->children
;
169 GtkBin
*bin
= GTK_BIN( child
->data
);
172 GtkLabel
*label
= NULL
;
173 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
175 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
177 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
184 wxFAIL_MSG( "wxChoice: string not found" );
189 wxString
wxChoice::GetStringSelection(void) const
191 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
193 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
198 int wxChoice::Number(void) const
200 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
202 GList
*child
= menu_shell
->children
;
211 void wxChoice::SetColumns( int WXUNUSED(n
) )
215 void wxChoice::SetSelection( int n
)
218 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
220 gtk_choice_clicked_callback( NULL
, this );
223 void wxChoice::SetStringSelection( const wxString
&string
)
225 int n
= FindString( string
);
226 if (n
!= -1) SetSelection( n
);