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 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
50 const wxPoint
&pos
, const wxSize
&size
,
51 int n
, const wxString choices
[],
52 long style
, const wxValidator
& validator
, const wxString
&name
)
56 PreCreation( parent
, id
, pos
, size
, style
, name
);
58 SetValidator( validator
);
60 m_widget
= gtk_option_menu_new();
62 wxSize newSize
= size
;
63 if (newSize
.x
== -1) newSize
.x
= 80;
64 if (newSize
.y
== -1) newSize
.y
= 26;
65 SetSize( newSize
.x
, newSize
.y
);
68 menu
= gtk_menu_new();
70 for (int i
= 0; i
< n
; i
++)
73 item
= gtk_menu_item_new_with_label( choices
[i
] );
74 gtk_signal_connect( GTK_OBJECT( item
), "activate",
75 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
76 gtk_menu_append( GTK_MENU(menu
), item
);
77 gtk_widget_show( item
);
79 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
88 void wxChoice::Append( const wxString
&item
)
90 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
92 menu_item
= gtk_menu_item_new_with_label( item
);
93 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
94 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
95 gtk_menu_append( GTK_MENU(menu
), menu_item
);
96 gtk_widget_show( menu_item
);
99 void wxChoice::Clear(void)
101 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
102 GtkWidget
*menu
= gtk_menu_new();
103 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
106 void wxChoice::Delete( int WXUNUSED(n
) )
108 wxFAIL_MSG( "wxChoice:Delete not implemented" );
111 int wxChoice::FindString( const wxString
&string
) const
113 // If you read this code once and you think you understand
114 // it, then you are very wrong. Robert Roebling.
116 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
118 GList
*child
= menu_shell
->children
;
121 GtkBin
*bin
= GTK_BIN( child
->data
);
122 GtkLabel
*label
= (GtkLabel
*) NULL
;
123 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
125 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
127 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
128 if (string
== label
->label
) return count
;
133 wxFAIL_MSG( "wxChoice: string not found" );
138 int wxChoice::GetColumns(void) const
143 int wxChoice::GetSelection(void)
145 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
147 GList
*child
= menu_shell
->children
;
150 GtkBin
*bin
= GTK_BIN( child
->data
);
151 if (!bin
->child
) return count
;
156 wxFAIL_MSG( "wxChoice: no selection" );
161 wxString
wxChoice::GetString( int n
) const
163 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
165 GList
*child
= menu_shell
->children
;
168 GtkBin
*bin
= GTK_BIN( child
->data
);
171 GtkLabel
*label
= (GtkLabel
*) NULL
;
172 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
174 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
176 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
183 wxFAIL_MSG( "wxChoice: string not found" );
188 wxString
wxChoice::GetStringSelection(void) const
190 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
192 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
197 int wxChoice::Number(void) const
199 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
201 GList
*child
= menu_shell
->children
;
210 void wxChoice::SetColumns( int WXUNUSED(n
) )
214 void wxChoice::SetSelection( int n
)
217 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
219 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
222 void wxChoice::SetStringSelection( const wxString
&string
)
224 int n
= FindString( string
);
225 if (n
!= -1) SetSelection( n
);