]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/choice.cpp
made pre-compiling the python files optional
[wxWidgets.git] / src / gtk1 / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choice.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 #ifdef __GNUG__
13 #pragma implementation "choice.h"
14 #endif
15
16 #include "wx/choice.h"
17
18 //-----------------------------------------------------------------------------
19 // data
20 //-----------------------------------------------------------------------------
21
22 extern bool g_blockEventsOnDrag;
23
24 //-----------------------------------------------------------------------------
25 // wxChoice
26 //-----------------------------------------------------------------------------
27
28 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
29 {
30 if (!choice->HasVMT()) return;
31 if (g_blockEventsOnDrag) return;
32
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);
39 };
40
41 //-----------------------------------------------------------------------------
42
43 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
44
45 wxChoice::wxChoice(void)
46 {
47 };
48
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 )
53 {
54 Create( parent, id, pos, size, n, choices, style, name );
55 };
56
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 )
61 {
62 m_needParent = TRUE;
63
64 PreCreation( parent, id, pos, size, style, name );
65
66 m_widget = gtk_option_menu_new();
67
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 );
72
73 GtkWidget *menu;
74 menu = gtk_menu_new();
75
76 for (int i = 0; i < n; i++)
77 {
78 GtkWidget *item;
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 );
84 };
85 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
86
87 PostCreation();
88
89 Show( TRUE );
90
91 return TRUE;
92 };
93
94 void wxChoice::Append( const wxString &item )
95 {
96 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
97 GtkWidget *menu_item;
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 );
103 };
104
105 void wxChoice::Clear(void)
106 {
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 );
110 };
111
112 int wxChoice::FindString( const wxString &string ) const
113 {
114 // If you read this code once and you think you understand
115 // it, then you are very wrong. Robert Roebling.
116
117 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
118 int count = 0;
119 GList *child = menu_shell->children;
120 while (child)
121 {
122 GtkBin *bin = GTK_BIN( child->data );
123 GtkLabel *label = NULL;
124 if (bin->child) label = GTK_LABEL(bin->child);
125
126 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
127
128 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
129 if (string == label->label) return count;
130 child = child->next;
131 count++;
132 };
133
134 wxFAIL_MSG( "wxChoice: string not found" );
135
136 return -1;
137 };
138
139 int wxChoice::GetColumns(void) const
140 {
141 return 1;
142 };
143
144 int wxChoice::GetSelection(void)
145 {
146 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
147 int count = 0;
148 GList *child = menu_shell->children;
149 while (child)
150 {
151 GtkBin *bin = GTK_BIN( child->data );
152 if (!bin->child) return count;
153 child = child->next;
154 count++;
155 };
156
157 wxFAIL_MSG( "wxChoice: no selection" );
158
159 return -1;
160 };
161
162 wxString wxChoice::GetString( int n ) const
163 {
164 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
165 int count = 0;
166 GList *child = menu_shell->children;
167 while (child)
168 {
169 GtkBin *bin = GTK_BIN( child->data );
170 if (count == n)
171 {
172 GtkLabel *label = NULL;
173 if (bin->child) label = GTK_LABEL(bin->child);
174
175 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
176
177 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
178 return label->label;
179 };
180 child = child->next;
181 count++;
182 };
183
184 wxFAIL_MSG( "wxChoice: string not found" );
185
186 return "";
187 };
188
189 wxString wxChoice::GetStringSelection(void) const
190 {
191 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
192
193 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
194
195 return label->label;
196 };
197
198 int wxChoice::Number(void) const
199 {
200 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
201 int count = 0;
202 GList *child = menu_shell->children;
203 while (child)
204 {
205 count++;
206 child = child->next;
207 };
208 return count;
209 };
210
211 void wxChoice::SetColumns( int WXUNUSED(n) )
212 {
213 };
214
215 void wxChoice::SetSelection( int n )
216 {
217 int tmp = n;
218 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
219
220 gtk_choice_clicked_callback( NULL, this );
221 };
222
223 void wxChoice::SetStringSelection( const wxString &string )
224 {
225 int n = FindString( string );
226 if (n != -1) SetSelection( n );
227 };
228