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
);
95 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
96 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
97 gtk_menu_append( GTK_MENU(menu
), menu_item
);
98 gtk_widget_show( menu_item
);
101 void wxChoice::Clear(void)
103 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
104 GtkWidget
*menu
= gtk_menu_new();
105 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
108 void wxChoice::Delete( int WXUNUSED(n
) )
110 wxFAIL_MSG( "wxChoice:Delete not implemented" );
113 int wxChoice::FindString( const wxString
&string
) const
115 // If you read this code once and you think you understand
116 // it, then you are very wrong. Robert Roebling.
118 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
120 GList
*child
= menu_shell
->children
;
123 GtkBin
*bin
= GTK_BIN( child
->data
);
124 GtkLabel
*label
= (GtkLabel
*) NULL
;
125 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
127 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
129 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
130 if (string
== label
->label
) return count
;
135 wxFAIL_MSG( "wxChoice: string not found" );
140 int wxChoice::GetColumns(void) const
145 int wxChoice::GetSelection(void)
147 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
149 GList
*child
= menu_shell
->children
;
152 GtkBin
*bin
= GTK_BIN( child
->data
);
153 if (!bin
->child
) return count
;
158 wxFAIL_MSG( "wxChoice: no selection" );
163 wxString
wxChoice::GetString( int n
) const
165 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
167 GList
*child
= menu_shell
->children
;
170 GtkBin
*bin
= GTK_BIN( child
->data
);
173 GtkLabel
*label
= (GtkLabel
*) NULL
;
174 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
176 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
178 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
185 wxFAIL_MSG( "wxChoice: string not found" );
190 wxString
wxChoice::GetStringSelection(void) const
192 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
194 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
199 int wxChoice::Number(void) const
201 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
203 GList
*child
= menu_shell
->children
;
212 void wxChoice::SetColumns( int WXUNUSED(n
) )
216 void wxChoice::SetSelection( int n
)
219 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
221 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
224 void wxChoice::SetStringSelection( const wxString
&string
)
226 int n
= FindString( string
);
227 if (n
!= -1) SetSelection( n
);