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
);
125 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
126 if (string
== label
->label
) return count
;
133 int wxChoice::GetColumns(void) const
138 int wxChoice::GetSelection(void)
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 if (!bin
->child
) return count
;
153 wxString
wxChoice::GetString( int n
) const
155 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
157 GList
*child
= menu_shell
->children
;
160 GtkBin
*bin
= GTK_BIN( child
->data
);
163 GtkLabel
*label
= NULL
;
164 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
165 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
174 wxString
wxChoice::GetStringSelection(void) const
176 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
180 int wxChoice::Number(void) const
182 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
184 GList
*child
= menu_shell
->children
;
193 void wxChoice::SetColumns( int WXUNUSED(n
) )
197 void wxChoice::SetSelection( int n
)
200 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
202 gtk_choice_clicked_callback( NULL
, this );
205 void wxChoice::SetStringSelection( const wxString
&string
)
207 int n
= FindString( string
);
208 if (n
!= -1) SetSelection( n
);