]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: choice.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
c801d85f KB |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "choice.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/choice.h" | |
16 | ||
66bd6b93 RR |
17 | //----------------------------------------------------------------------------- |
18 | // data | |
19 | //----------------------------------------------------------------------------- | |
20 | ||
21 | extern bool g_blockEventsOnDrag; | |
22 | ||
c801d85f | 23 | //----------------------------------------------------------------------------- |
e1e955e1 | 24 | // "activate" |
c801d85f KB |
25 | //----------------------------------------------------------------------------- |
26 | ||
66bd6b93 | 27 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) |
c801d85f | 28 | { |
66bd6b93 RR |
29 | if (!choice->HasVMT()) return; |
30 | if (g_blockEventsOnDrag) return; | |
31 | ||
47908e25 | 32 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); |
c801d85f KB |
33 | event.SetInt( choice->GetSelection() ); |
34 | wxString tmp( choice->GetStringSelection() ); | |
35 | event.SetString( WXSTRINGCAST(tmp) ); | |
36 | event.SetEventObject(choice); | |
47908e25 | 37 | choice->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 38 | } |
c801d85f | 39 | |
e1e955e1 RR |
40 | //----------------------------------------------------------------------------- |
41 | // wxChoice | |
c801d85f KB |
42 | //----------------------------------------------------------------------------- |
43 | ||
7f4dc78d | 44 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) |
c801d85f KB |
45 | |
46 | wxChoice::wxChoice(void) | |
47 | { | |
6de97a3b | 48 | } |
c801d85f | 49 | |
debe6624 JS |
50 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
51 | const wxPoint &pos, const wxSize &size, | |
52 | int n, const wxString choices[], | |
6de97a3b | 53 | long style, const wxValidator& validator, const wxString &name ) |
c801d85f KB |
54 | { |
55 | m_needParent = TRUE; | |
56 | ||
57 | PreCreation( parent, id, pos, size, style, name ); | |
58 | ||
6de97a3b RR |
59 | SetValidator( validator ); |
60 | ||
c801d85f KB |
61 | m_widget = gtk_option_menu_new(); |
62 | ||
63 | wxSize newSize = size; | |
64 | if (newSize.x == -1) newSize.x = 80; | |
65 | if (newSize.y == -1) newSize.y = 26; | |
66 | SetSize( newSize.x, newSize.y ); | |
67 | ||
f96aa4d9 | 68 | GtkWidget *menu = gtk_menu_new(); |
c801d85f KB |
69 | |
70 | for (int i = 0; i < n; i++) | |
71 | { | |
f96aa4d9 | 72 | GtkWidget *item = gtk_menu_item_new_with_label( choices[i] ); |
c801d85f KB |
73 | gtk_signal_connect( GTK_OBJECT( item ), "activate", |
74 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
f96aa4d9 | 75 | |
c801d85f | 76 | gtk_menu_append( GTK_MENU(menu), item ); |
f96aa4d9 | 77 | |
c801d85f | 78 | gtk_widget_show( item ); |
f96aa4d9 RR |
79 | gtk_widget_realize( item ); |
80 | gtk_widget_realize( GTK_BIN(item)->child ); | |
6de97a3b | 81 | } |
c801d85f KB |
82 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); |
83 | ||
84 | PostCreation(); | |
85 | ||
f96aa4d9 RR |
86 | SetBackgroundColour( parent->GetBackgroundColour() ); |
87 | ||
c801d85f KB |
88 | Show( TRUE ); |
89 | ||
90 | return TRUE; | |
6de97a3b | 91 | } |
c801d85f KB |
92 | |
93 | void wxChoice::Append( const wxString &item ) | |
94 | { | |
f96aa4d9 RR |
95 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
96 | ||
c801d85f | 97 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); |
f96aa4d9 RR |
98 | GtkWidget *menu_item = gtk_menu_item_new_with_label( item ); |
99 | ||
100 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
101 | ||
102 | gtk_widget_realize( menu_item ); | |
103 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
868a2826 RR |
104 | |
105 | if (m_hasOwnStyle) | |
106 | { | |
f96aa4d9 | 107 | |
868a2826 | 108 | GtkBin *bin = GTK_BIN( menu_item ); |
f96aa4d9 | 109 | |
868a2826 RR |
110 | gtk_widget_set_style( bin->child, |
111 | gtk_style_ref( | |
112 | gtk_widget_get_style( m_widget ) ) ); | |
f96aa4d9 RR |
113 | |
114 | gtk_widget_set_style( GTK_WIDGET( bin ), | |
115 | gtk_style_ref( | |
116 | gtk_widget_get_style( m_widget ) ) ); | |
868a2826 RR |
117 | } |
118 | ||
c801d85f KB |
119 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", |
120 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
868a2826 | 121 | |
c801d85f | 122 | gtk_widget_show( menu_item ); |
6de97a3b | 123 | } |
c801d85f KB |
124 | |
125 | void wxChoice::Clear(void) | |
126 | { | |
f96aa4d9 RR |
127 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
128 | ||
c801d85f KB |
129 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); |
130 | GtkWidget *menu = gtk_menu_new(); | |
131 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
6de97a3b | 132 | } |
c801d85f | 133 | |
2f6407b9 RR |
134 | void wxChoice::Delete( int WXUNUSED(n) ) |
135 | { | |
136 | wxFAIL_MSG( "wxChoice:Delete not implemented" ); | |
137 | } | |
138 | ||
c801d85f KB |
139 | int wxChoice::FindString( const wxString &string ) const |
140 | { | |
f96aa4d9 RR |
141 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); |
142 | ||
47908e25 RR |
143 | // If you read this code once and you think you understand |
144 | // it, then you are very wrong. Robert Roebling. | |
c801d85f KB |
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 ); | |
c67daf87 | 152 | GtkLabel *label = (GtkLabel *) NULL; |
47908e25 | 153 | if (bin->child) label = GTK_LABEL(bin->child); |
868a2826 | 154 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 RR |
155 | |
156 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
157 | ||
c801d85f KB |
158 | if (string == label->label) return count; |
159 | child = child->next; | |
160 | count++; | |
6de97a3b | 161 | } |
b6af8d80 RR |
162 | |
163 | wxFAIL_MSG( "wxChoice: string not found" ); | |
164 | ||
c801d85f | 165 | return -1; |
6de97a3b | 166 | } |
c801d85f KB |
167 | |
168 | int wxChoice::GetColumns(void) const | |
169 | { | |
170 | return 1; | |
6de97a3b | 171 | } |
c801d85f KB |
172 | |
173 | int wxChoice::GetSelection(void) | |
174 | { | |
f96aa4d9 RR |
175 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); |
176 | ||
c801d85f KB |
177 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
178 | int count = 0; | |
179 | GList *child = menu_shell->children; | |
180 | while (child) | |
181 | { | |
182 | GtkBin *bin = GTK_BIN( child->data ); | |
183 | if (!bin->child) return count; | |
184 | child = child->next; | |
185 | count++; | |
6de97a3b | 186 | } |
b6af8d80 RR |
187 | |
188 | wxFAIL_MSG( "wxChoice: no selection" ); | |
189 | ||
c801d85f | 190 | return -1; |
6de97a3b | 191 | } |
c801d85f | 192 | |
debe6624 | 193 | wxString wxChoice::GetString( int n ) const |
c801d85f | 194 | { |
f96aa4d9 RR |
195 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); |
196 | ||
c801d85f KB |
197 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
198 | int count = 0; | |
199 | GList *child = menu_shell->children; | |
200 | while (child) | |
201 | { | |
202 | GtkBin *bin = GTK_BIN( child->data ); | |
203 | if (count == n) | |
204 | { | |
c67daf87 | 205 | GtkLabel *label = (GtkLabel *) NULL; |
47908e25 | 206 | if (bin->child) label = GTK_LABEL(bin->child); |
868a2826 | 207 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 RR |
208 | |
209 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
210 | ||
c801d85f | 211 | return label->label; |
6de97a3b | 212 | } |
c801d85f KB |
213 | child = child->next; |
214 | count++; | |
6de97a3b | 215 | } |
b6af8d80 RR |
216 | |
217 | wxFAIL_MSG( "wxChoice: string not found" ); | |
218 | ||
c801d85f | 219 | return ""; |
6de97a3b | 220 | } |
c801d85f KB |
221 | |
222 | wxString wxChoice::GetStringSelection(void) const | |
223 | { | |
f96aa4d9 RR |
224 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); |
225 | ||
c801d85f | 226 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 RR |
227 | |
228 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
229 | ||
c801d85f | 230 | return label->label; |
6de97a3b | 231 | } |
c801d85f KB |
232 | |
233 | int wxChoice::Number(void) const | |
234 | { | |
f96aa4d9 RR |
235 | wxCHECK_MSG( m_widget != NULL, 0, "invalid choice" ); |
236 | ||
7a4b9130 | 237 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
c801d85f | 238 | int count = 0; |
7a4b9130 | 239 | GList *child = menu_shell->children; |
c801d85f KB |
240 | while (child) |
241 | { | |
242 | count++; | |
243 | child = child->next; | |
6de97a3b | 244 | } |
c801d85f | 245 | return count; |
6de97a3b | 246 | } |
c801d85f | 247 | |
debe6624 | 248 | void wxChoice::SetColumns( int WXUNUSED(n) ) |
c801d85f | 249 | { |
6de97a3b | 250 | } |
c801d85f | 251 | |
debe6624 | 252 | void wxChoice::SetSelection( int n ) |
c801d85f | 253 | { |
f96aa4d9 RR |
254 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
255 | ||
c801d85f KB |
256 | int tmp = n; |
257 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
47908e25 | 258 | |
c67daf87 | 259 | gtk_choice_clicked_callback( (GtkWidget *) NULL, this ); |
6de97a3b | 260 | } |
c801d85f KB |
261 | |
262 | void wxChoice::SetStringSelection( const wxString &string ) | |
263 | { | |
f96aa4d9 RR |
264 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
265 | ||
c801d85f KB |
266 | int n = FindString( string ); |
267 | if (n != -1) SetSelection( n ); | |
6de97a3b | 268 | } |
c801d85f | 269 | |
868a2826 RR |
270 | void wxChoice::SetFont( const wxFont &font ) |
271 | { | |
f96aa4d9 RR |
272 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
273 | ||
274 | wxControl::SetFont( font ); | |
868a2826 RR |
275 | |
276 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
277 | GList *child = menu_shell->children; | |
278 | while (child) | |
279 | { | |
280 | GtkBin *bin = GTK_BIN( child->data ); | |
281 | GtkWidget *label = (GtkWidget *) NULL; | |
282 | if (bin->child) label = bin->child; | |
283 | if (!label) label = GTK_BUTTON(m_widget)->child; | |
284 | ||
285 | gtk_widget_set_style( label, | |
286 | gtk_style_ref( | |
287 | gtk_widget_get_style( m_widget ) ) ); | |
288 | ||
289 | child = child->next; | |
290 | } | |
291 | } | |
f96aa4d9 RR |
292 | |
293 | void wxChoice::SetBackgroundColour( const wxColour &colour ) | |
294 | { | |
295 | return; | |
296 | ||
297 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); | |
298 | ||
299 | wxControl::SetBackgroundColour( colour ); | |
300 | ||
301 | if (!m_backgroundColour.Ok()) return; | |
302 | ||
303 | GtkStyle *style = gtk_widget_get_style( m_widget ); | |
304 | ||
305 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
306 | ||
307 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), gtk_style_ref( style ) ); | |
308 | ||
309 | GList *child = menu_shell->children; | |
310 | while (child) | |
311 | { | |
312 | gtk_widget_set_style( GTK_WIDGET( child->data ), gtk_style_ref( style ) ); | |
313 | child = child->next; | |
314 | } | |
315 | } | |
316 | ||
317 | ||
318 |