]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: radiobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "radiobox.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/radiobox.h" | |
15 | #include "wx/dialog.h" | |
16 | #include "wx/frame.h" | |
17 | ||
18 | #include "gdk/gdk.h" | |
19 | #include "gtk/gtk.h" | |
20 | #include "wx/gtk/win_gtk.h" | |
21 | ||
22 | //----------------------------------------------------------------------------- | |
23 | // data | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | extern bool g_blockEventsOnDrag; | |
27 | ||
28 | //----------------------------------------------------------------------------- | |
29 | // "clicked" | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
32 | static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb ) | |
33 | { | |
34 | if (!rb->HasVMT()) return; | |
35 | if (g_blockEventsOnDrag) return; | |
36 | ||
37 | if (rb->m_alreadySent) | |
38 | { | |
39 | rb->m_alreadySent = FALSE; | |
40 | return; | |
41 | } | |
42 | ||
43 | rb->m_alreadySent = TRUE; | |
44 | ||
45 | wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() ); | |
46 | event.SetInt( rb->GetSelection() ); | |
47 | wxString tmp( rb->GetStringSelection() ); | |
48 | event.SetString( WXSTRINGCAST(tmp) ); | |
49 | event.SetEventObject( rb ); | |
50 | rb->GetEventHandler()->ProcessEvent(event); | |
51 | } | |
52 | ||
53 | //----------------------------------------------------------------------------- | |
54 | // wxRadioBox | |
55 | //----------------------------------------------------------------------------- | |
56 | ||
57 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl) | |
58 | ||
59 | BEGIN_EVENT_TABLE(wxRadioBox, wxControl) | |
60 | EVT_SIZE(wxRadioBox::OnSize) | |
61 | END_EVENT_TABLE() | |
62 | ||
63 | wxRadioBox::wxRadioBox(void) | |
64 | { | |
65 | } | |
66 | ||
67 | bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title, | |
68 | const wxPoint &pos, const wxSize &size, | |
69 | int n, const wxString choices[], int majorDim, | |
70 | long style, const wxValidator& validator, | |
71 | const wxString &name ) | |
72 | { | |
73 | m_alreadySent = FALSE; | |
74 | m_needParent = TRUE; | |
75 | m_acceptsFocus = TRUE; | |
76 | ||
77 | PreCreation( parent, id, pos, size, style, name ); | |
78 | ||
79 | SetValidator( validator ); | |
80 | ||
81 | m_widget = gtk_frame_new( title ); | |
82 | ||
83 | m_majorDim = majorDim; | |
84 | ||
85 | GtkRadioButton *m_radio = (GtkRadioButton*) NULL; | |
86 | ||
87 | GSList *radio_button_group = (GSList *) NULL; | |
88 | for (int i = 0; i < n; i++) | |
89 | { | |
90 | if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) ); | |
91 | ||
92 | m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) ); | |
93 | ||
94 | m_boxes.Append( (wxObject*) m_radio ); | |
95 | ||
96 | ConnectWidget( GTK_WIDGET(m_radio) ); | |
97 | ||
98 | if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE ); | |
99 | ||
100 | gtk_signal_connect( GTK_OBJECT(m_radio), "clicked", | |
101 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); | |
102 | ||
103 | gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), m_x+10, m_y+10+(i*24) ); | |
104 | ||
105 | } | |
106 | ||
107 | wxSize ls = LayoutItems(); | |
108 | ||
109 | wxSize newSize = size; | |
110 | if (newSize.x == -1) newSize.x = ls.x; | |
111 | if (newSize.y == -1) newSize.y = ls.y; | |
112 | SetSize( newSize.x, newSize.y ); | |
113 | ||
114 | m_parent->AddChild( this ); | |
115 | ||
116 | (m_parent->m_insertCallback)( m_parent, this ); | |
117 | ||
118 | PostCreation(); | |
119 | ||
120 | SetLabel( title ); | |
121 | ||
122 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
123 | SetForegroundColour( parent->GetForegroundColour() ); | |
124 | ||
125 | Show( TRUE ); | |
126 | ||
127 | return TRUE; | |
128 | } | |
129 | ||
130 | wxRadioBox::~wxRadioBox(void) | |
131 | { | |
132 | wxNode *node = m_boxes.First(); | |
133 | while (node) | |
134 | { | |
135 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
136 | gtk_widget_destroy( button ); | |
137 | node = node->Next(); | |
138 | } | |
139 | } | |
140 | ||
141 | void wxRadioBox::OnSize( wxSizeEvent &event ) | |
142 | { | |
143 | wxControl::OnSize( event ); | |
144 | ||
145 | LayoutItems(); | |
146 | } | |
147 | ||
148 | wxSize wxRadioBox::LayoutItems() | |
149 | { | |
150 | int x = 7; | |
151 | int y = 15; | |
152 | ||
153 | int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1; | |
154 | ||
155 | wxSize res( 0, 0 ); | |
156 | ||
157 | if (m_windowStyle & wxRA_HORIZONTAL) | |
158 | { | |
159 | ||
160 | for (int j = 0; j < m_majorDim; j++) | |
161 | { | |
162 | y = 15; | |
163 | ||
164 | int max_len = 0; | |
165 | wxNode *node = m_boxes.Nth( j*num_per_major ); | |
166 | for (int i = 0; i< num_per_major; i++) | |
167 | { | |
168 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
169 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); | |
170 | GdkFont *font = m_widget->style->font; | |
171 | int len = 22+gdk_string_measure( font, label->label ); | |
172 | if (len > max_len) max_len = len; | |
173 | ||
174 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y ); | |
175 | y += 22; | |
176 | ||
177 | node = node->Next(); | |
178 | if (!node) break; | |
179 | } | |
180 | ||
181 | // we don't know the max_len before | |
182 | ||
183 | node = m_boxes.Nth( j*num_per_major ); | |
184 | for (int i = 0; i< num_per_major; i++) | |
185 | { | |
186 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
187 | ||
188 | gtk_widget_set_usize( button, max_len, 22 ); | |
189 | ||
190 | node = node->Next(); | |
191 | if (!node) break; | |
192 | } | |
193 | ||
194 | if (y > res.y) res.y = y; | |
195 | ||
196 | x += max_len + 2; | |
197 | } | |
198 | ||
199 | res.x = x+4; | |
200 | res.y += 9; | |
201 | } | |
202 | else | |
203 | { | |
204 | int max = 0; | |
205 | ||
206 | wxNode *node = m_boxes.First(); | |
207 | while (node) | |
208 | { | |
209 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
210 | GtkLabel *label = GTK_LABEL( button->child ); | |
211 | ||
212 | GdkFont *font = m_widget->style->font; | |
213 | int len = 22+gdk_string_measure( font, label->label ); | |
214 | if (len > max) max = len; | |
215 | ||
216 | node = node->Next(); | |
217 | } | |
218 | ||
219 | node = m_boxes.First(); | |
220 | while (node) | |
221 | { | |
222 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
223 | ||
224 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y ); | |
225 | x += max; | |
226 | gtk_widget_set_usize( button, max, 20 ); | |
227 | ||
228 | node = node->Next(); | |
229 | } | |
230 | res.x = x+4; | |
231 | res.y = 42; | |
232 | } | |
233 | ||
234 | return res; | |
235 | } | |
236 | ||
237 | bool wxRadioBox::Show( bool show ) | |
238 | { | |
239 | wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" ); | |
240 | ||
241 | wxWindow::Show( show ); | |
242 | ||
243 | wxNode *node = m_boxes.First(); | |
244 | while (node) | |
245 | { | |
246 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
247 | ||
248 | if (show) gtk_widget_show( button ); else gtk_widget_hide( button ); | |
249 | ||
250 | node = node->Next(); | |
251 | } | |
252 | ||
253 | return TRUE; | |
254 | } | |
255 | ||
256 | int wxRadioBox::FindString( const wxString &s ) const | |
257 | { | |
258 | wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" ); | |
259 | ||
260 | int count = 0; | |
261 | ||
262 | wxNode *node = m_boxes.First(); | |
263 | while (node) | |
264 | { | |
265 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
266 | ||
267 | GtkLabel *label = GTK_LABEL( button->child ); | |
268 | if (s == label->label) return count; | |
269 | count++; | |
270 | ||
271 | node = node->Next(); | |
272 | } | |
273 | ||
274 | return -1; | |
275 | } | |
276 | ||
277 | void wxRadioBox::SetFocus() | |
278 | { | |
279 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
280 | ||
281 | if (m_boxes.GetCount() == 0) return; | |
282 | ||
283 | wxNode *node = m_boxes.First(); | |
284 | while (node) | |
285 | { | |
286 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
287 | if (button->active) | |
288 | { | |
289 | gtk_widget_grab_focus( GTK_WIDGET(button) ); | |
290 | ||
291 | return; | |
292 | } | |
293 | node = node->Next(); | |
294 | } | |
295 | ||
296 | } | |
297 | ||
298 | void wxRadioBox::SetSelection( int n ) | |
299 | { | |
300 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
301 | ||
302 | wxNode *node = m_boxes.Nth( n ); | |
303 | ||
304 | wxCHECK_RET( node, "radiobox wrong index" ); | |
305 | ||
306 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
307 | ||
308 | gtk_toggle_button_set_state( button, 1 ); | |
309 | } | |
310 | ||
311 | int wxRadioBox::GetSelection(void) const | |
312 | { | |
313 | wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" ); | |
314 | ||
315 | int count = 0; | |
316 | ||
317 | wxNode *node = m_boxes.First(); | |
318 | while (node) | |
319 | { | |
320 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
321 | if (button->active) return count; | |
322 | count++; | |
323 | node = node->Next(); | |
324 | } | |
325 | ||
326 | wxFAIL_MSG( "wxRadioBox none selected" ); | |
327 | ||
328 | return -1; | |
329 | } | |
330 | ||
331 | wxString wxRadioBox::GetString( int n ) const | |
332 | { | |
333 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); | |
334 | ||
335 | wxNode *node = m_boxes.Nth( n ); | |
336 | ||
337 | wxCHECK_MSG( node, "", "radiobox wrong index" ); | |
338 | ||
339 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
340 | GtkLabel *label = GTK_LABEL( button->child ); | |
341 | ||
342 | return wxString( label->label ); | |
343 | } | |
344 | ||
345 | wxString wxRadioBox::GetLabel( int item ) const | |
346 | { | |
347 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); | |
348 | ||
349 | return GetString( item ); | |
350 | } | |
351 | ||
352 | void wxRadioBox::SetLabel( const wxString& label ) | |
353 | { | |
354 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
355 | ||
356 | wxControl::SetLabel( label ); | |
357 | ||
358 | gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel() ); | |
359 | } | |
360 | ||
361 | void wxRadioBox::SetLabel( int item, const wxString& label ) | |
362 | { | |
363 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
364 | ||
365 | wxNode *node = m_boxes.Nth( item ); | |
366 | ||
367 | wxCHECK_RET( node, "radiobox wrong index" ); | |
368 | ||
369 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
370 | GtkLabel *g_label = GTK_LABEL( button->child ); | |
371 | ||
372 | gtk_label_set( g_label, label ); | |
373 | } | |
374 | ||
375 | void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) ) | |
376 | { | |
377 | wxFAIL_MSG("wxRadioBox::SetLabel not implemented."); | |
378 | } | |
379 | ||
380 | void wxRadioBox::Enable( bool enable ) | |
381 | { | |
382 | wxControl::Enable( enable ); | |
383 | ||
384 | wxNode *node = m_boxes.First(); | |
385 | while (node) | |
386 | { | |
387 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
388 | GtkWidget *label = button->child; | |
389 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
390 | gtk_widget_set_sensitive( label, enable ); | |
391 | node = node->Next(); | |
392 | } | |
393 | } | |
394 | ||
395 | void wxRadioBox::Enable( int item, bool enable ) | |
396 | { | |
397 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
398 | ||
399 | wxNode *node = m_boxes.Nth( item ); | |
400 | ||
401 | wxCHECK_RET( node, "radiobox wrong index" ); | |
402 | ||
403 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
404 | GtkWidget *label = button->child; | |
405 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
406 | gtk_widget_set_sensitive( label, enable ); | |
407 | } | |
408 | ||
409 | void wxRadioBox::Show( int item, bool show ) | |
410 | { | |
411 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
412 | ||
413 | wxNode *node = m_boxes.Nth( item ); | |
414 | ||
415 | wxCHECK_RET( node, "radiobox wrong index" ); | |
416 | ||
417 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
418 | ||
419 | if (show) | |
420 | gtk_widget_show( button ); | |
421 | else | |
422 | gtk_widget_hide( button ); | |
423 | } | |
424 | ||
425 | wxString wxRadioBox::GetStringSelection(void) const | |
426 | { | |
427 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); | |
428 | ||
429 | wxNode *node = m_boxes.First(); | |
430 | while (node) | |
431 | { | |
432 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
433 | if (button->active) | |
434 | { | |
435 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); | |
436 | return label->label; | |
437 | } | |
438 | node = node->Next(); | |
439 | } | |
440 | ||
441 | wxFAIL_MSG( "wxRadioBox none selected" ); | |
442 | return ""; | |
443 | } | |
444 | ||
445 | bool wxRadioBox::SetStringSelection( const wxString &s ) | |
446 | { | |
447 | wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" ); | |
448 | ||
449 | int res = FindString( s ); | |
450 | if (res == -1) return FALSE; | |
451 | SetSelection( res ); | |
452 | ||
453 | return TRUE; | |
454 | } | |
455 | ||
456 | int wxRadioBox::Number(void) const | |
457 | { | |
458 | return m_boxes.Number(); | |
459 | } | |
460 | ||
461 | int wxRadioBox::GetNumberOfRowsOrCols(void) const | |
462 | { | |
463 | return 1; | |
464 | } | |
465 | ||
466 | void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) ) | |
467 | { | |
468 | wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented."); | |
469 | } | |
470 | ||
471 | void wxRadioBox::ApplyWidgetStyle() | |
472 | { | |
473 | SetWidgetStyle(); | |
474 | ||
475 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
476 | ||
477 | wxNode *node = m_boxes.First(); | |
478 | while (node) | |
479 | { | |
480 | GtkWidget *widget = GTK_WIDGET( node->Data() ); | |
481 | gtk_widget_set_style( widget, m_widgetStyle ); | |
482 | ||
483 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
484 | gtk_widget_set_style( button->child, m_widgetStyle ); | |
485 | ||
486 | node = node->Next(); | |
487 | } | |
488 | } | |
489 | ||
490 | bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window ) | |
491 | { | |
492 | if (window == m_widget->window) return TRUE; | |
493 | ||
494 | wxNode *node = m_boxes.First(); | |
495 | while (node) | |
496 | { | |
497 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
498 | ||
499 | if (window == button->window) return TRUE; | |
500 | ||
501 | node = node->Next(); | |
502 | } | |
503 | ||
504 | return FALSE; | |
505 | } |