]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobox.cpp
reSWIGged
[wxWidgets.git] / src / gtk / radiobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/radiobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_RADIOBOX
14
15 #include "wx/radiobox.h"
16
17 #include "wx/dialog.h"
18 #include "wx/frame.h"
19 #include "wx/log.h"
20
21 #include "wx/gtk/private.h"
22 #include <gdk/gdkkeysyms.h>
23
24 #include "wx/gtk/win_gtk.h"
25
26 //-----------------------------------------------------------------------------
27 // data
28 //-----------------------------------------------------------------------------
29
30 extern bool g_blockEventsOnDrag;
31 extern wxWindowGTK *g_delayedFocus;
32
33 //-----------------------------------------------------------------------------
34 // "clicked"
35 //-----------------------------------------------------------------------------
36
37 extern "C" {
38 static void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioBox *rb )
39 {
40 if (g_isIdle) wxapp_install_idle_handler();
41
42 if (!rb->m_hasVMT) return;
43 if (g_blockEventsOnDrag) return;
44
45 if (!button->active) return;
46
47 wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() );
48 event.SetInt( rb->GetSelection() );
49 event.SetString( rb->GetStringSelection() );
50 event.SetEventObject( rb );
51 rb->GetEventHandler()->ProcessEvent(event);
52 }
53 }
54
55 //-----------------------------------------------------------------------------
56 // "key_press_event"
57 //-----------------------------------------------------------------------------
58
59 extern "C" {
60 static gint gtk_radiobox_keypress_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxRadioBox *rb )
61 {
62 if (g_isIdle)
63 wxapp_install_idle_handler();
64
65 if (!rb->m_hasVMT) return FALSE;
66 if (g_blockEventsOnDrag) return FALSE;
67
68 if ( ((gdk_event->keyval == GDK_Tab) ||
69 (gdk_event->keyval == GDK_ISO_Left_Tab)) &&
70 rb->GetParent() && (rb->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
71 {
72 wxNavigationKeyEvent new_event;
73 new_event.SetEventObject( rb->GetParent() );
74 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
75 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
76 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
77 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
78 new_event.SetCurrentFocus( rb );
79 return rb->GetParent()->GetEventHandler()->ProcessEvent( new_event );
80 }
81
82 if ((gdk_event->keyval != GDK_Up) &&
83 (gdk_event->keyval != GDK_Down) &&
84 (gdk_event->keyval != GDK_Left) &&
85 (gdk_event->keyval != GDK_Right))
86 {
87 return FALSE;
88 }
89
90 wxList::compatibility_iterator node = rb->m_boxes.Find( (wxObject*) widget );
91 if (!node)
92 {
93 return FALSE;
94 }
95
96 g_signal_stop_emission_by_name (widget, "key_press_event");
97
98 if ((gdk_event->keyval == GDK_Up) ||
99 (gdk_event->keyval == GDK_Left))
100 {
101 if (node == rb->m_boxes.GetFirst())
102 node = rb->m_boxes.GetLast();
103 else
104 node = node->GetPrevious();
105 }
106 else
107 {
108 if (node == rb->m_boxes.GetLast())
109 node = rb->m_boxes.GetFirst();
110 else
111 node = node->GetNext();
112 }
113
114 GtkWidget *button = (GtkWidget*) node->GetData();
115
116 gtk_widget_grab_focus( button );
117
118 return TRUE;
119 }
120 }
121
122 extern "C" {
123 static gint gtk_radiobutton_focus_in( GtkWidget *widget,
124 GdkEvent *WXUNUSED(event),
125 wxRadioBox *win )
126 {
127 if ( win->m_lostFocus )
128 {
129 // no, we didn't really lose it
130 win->m_lostFocus = FALSE;
131 }
132 else if ( !win->m_hasFocus )
133 {
134 win->m_hasFocus = true;
135
136 wxFocusEvent event( wxEVT_SET_FOCUS, win->GetId() );
137 event.SetEventObject( win );
138
139 // never stop the signal emission, it seems to break the kbd handling
140 // inside the radiobox
141 (void)win->GetEventHandler()->ProcessEvent( event );
142 }
143
144 return FALSE;
145 }
146 }
147
148 extern "C" {
149 static gint gtk_radiobutton_focus_out( GtkWidget *widget,
150 GdkEvent *WXUNUSED(event),
151 wxRadioBox *win )
152 {
153 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
154 // Replace with a warning, else we dump core a lot!
155 // if (!win->m_hasFocus)
156 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
157
158 // we might have lost the focus, but may be not - it may have just gone to
159 // another button in the same radiobox, so we'll check for it in the next
160 // idle iteration (leave m_hasFocus == true for now)
161 win->m_lostFocus = true;
162
163 return FALSE;
164 }
165 }
166
167 //-----------------------------------------------------------------------------
168 // wxRadioBox
169 //-----------------------------------------------------------------------------
170
171 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
172
173 void wxRadioBox::Init()
174 {
175 m_needParent = true;
176 m_acceptsFocus = true;
177
178 m_hasFocus =
179 m_lostFocus = false;
180 }
181
182 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id,
183 const wxString& title,
184 const wxPoint &pos, const wxSize &size,
185 const wxArrayString& choices, int majorDim,
186 long style, const wxValidator& validator,
187 const wxString &name )
188 {
189 wxCArrayString chs(choices);
190
191 return Create( parent, id, title, pos, size, chs.GetCount(),
192 chs.GetStrings(), majorDim, style, validator, name );
193 }
194
195 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
196 const wxPoint &pos, const wxSize &size,
197 int n, const wxString choices[], int majorDim,
198 long style, const wxValidator& validator,
199 const wxString &name )
200 {
201 if (!PreCreation( parent, pos, size ) ||
202 !CreateBase( parent, id, pos, size, style, validator, name ))
203 {
204 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
205 return false;
206 }
207
208 m_widget = GTKCreateFrame(title);
209 wxControl::SetLabel(title);
210
211 // majorDim may be 0 if all trailing parameters were omitted, so don't
212 // assert here but just use the correct value for it
213 SetMajorDim(majorDim == 0 ? n : majorDim, style);
214
215
216 unsigned int num_of_cols = GetColumnCount();
217 unsigned int num_of_rows = GetRowCount();
218
219 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
220
221 GtkWidget *table = gtk_table_new( num_of_rows, num_of_cols, FALSE );
222 gtk_table_set_col_spacings( GTK_TABLE(table), 1 );
223 gtk_table_set_row_spacings( GTK_TABLE(table), 1 );
224 gtk_widget_show( table );
225 gtk_container_add( GTK_CONTAINER(m_widget), table );
226
227 wxString label;
228 GSList *radio_button_group = (GSList *) NULL;
229 for (unsigned int i = 0; i < n; i++)
230 {
231 if ( i != 0 )
232 radio_button_group = gtk_radio_button_get_group( GTK_RADIO_BUTTON(m_radio) );
233
234 label.Empty();
235 for ( const wxChar *pc = choices[i]; *pc; pc++ )
236 {
237 if ( *pc != wxT('&') )
238 label += *pc;
239 }
240
241 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, wxGTK_CONV( label ) ) );
242 gtk_widget_show( GTK_WIDGET(m_radio) );
243
244 g_signal_connect (m_radio, "key_press_event",
245 G_CALLBACK (gtk_radiobox_keypress_callback), this);
246
247 m_boxes.Append( (wxObject*) m_radio );
248
249 if (HasFlag(wxRA_SPECIFY_COLS))
250 {
251 int left = i%num_of_cols;
252 int right = (i%num_of_cols) + 1;
253 int top = i/num_of_cols;
254 int bottom = (i/num_of_cols)+1;
255 gtk_table_attach( GTK_TABLE(table), GTK_WIDGET(m_radio), left, right, top, bottom,
256 GTK_FILL, GTK_FILL, 1, 1 );
257 }
258 else
259 {
260 int left = i/num_of_rows;
261 int right = (i/num_of_rows) + 1;
262 int top = i%num_of_rows;
263 int bottom = (i%num_of_rows)+1;
264 gtk_table_attach( GTK_TABLE(table), GTK_WIDGET(m_radio), left, right, top, bottom,
265 GTK_FILL, GTK_FILL, 1, 1 );
266 }
267
268 ConnectWidget( GTK_WIDGET(m_radio) );
269
270 if (!i)
271 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_radio), TRUE );
272
273 g_signal_connect (m_radio, "clicked",
274 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
275 g_signal_connect (m_radio, "focus_in_event",
276 G_CALLBACK (gtk_radiobutton_focus_in), this);
277 g_signal_connect (m_radio, "focus_out_event",
278 G_CALLBACK (gtk_radiobutton_focus_out), this);
279 }
280
281 m_parent->DoAddChild( this );
282
283 PostCreation(size);
284
285 return true;
286 }
287
288 wxRadioBox::~wxRadioBox()
289 {
290 wxList::compatibility_iterator node = m_boxes.GetFirst();
291 while (node)
292 {
293 GtkWidget *button = GTK_WIDGET( node->GetData() );
294 gtk_widget_destroy( button );
295 node = node->GetNext();
296 }
297 }
298
299 bool wxRadioBox::Show( bool show )
300 {
301 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
302
303 if (!wxControl::Show(show))
304 {
305 // nothing to do
306 return false;
307 }
308
309 if ( HasFlag(wxNO_BORDER) )
310 gtk_widget_hide( m_widget );
311
312 wxList::compatibility_iterator node = m_boxes.GetFirst();
313 while (node)
314 {
315 GtkWidget *button = GTK_WIDGET( node->GetData() );
316
317 if (show)
318 gtk_widget_show( button );
319 else
320 gtk_widget_hide( button );
321
322 node = node->GetNext();
323 }
324
325 return true;
326 }
327
328 void wxRadioBox::SetFocus()
329 {
330 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
331
332 if (m_boxes.GetCount() == 0) return;
333
334 wxList::compatibility_iterator node = m_boxes.GetFirst();
335 while (node)
336 {
337 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
338 if (button->active)
339 {
340 gtk_widget_grab_focus( GTK_WIDGET(button) );
341 return;
342 }
343 node = node->GetNext();
344 }
345 }
346
347 void wxRadioBox::SetSelection( int n )
348 {
349 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
350
351 wxList::compatibility_iterator node = m_boxes.Item( n );
352
353 wxCHECK_RET( node, wxT("radiobox wrong index") );
354
355 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
356
357 GtkDisableEvents();
358
359 gtk_toggle_button_set_active( button, 1 );
360
361 GtkEnableEvents();
362 }
363
364 int wxRadioBox::GetSelection(void) const
365 {
366 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid radiobox") );
367
368 int count = 0;
369
370 wxList::compatibility_iterator node = m_boxes.GetFirst();
371 while (node)
372 {
373 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
374 if (button->active) return count;
375 count++;
376 node = node->GetNext();
377 }
378
379 wxFAIL_MSG( wxT("wxRadioBox none selected") );
380
381 return wxNOT_FOUND;
382 }
383
384 wxString wxRadioBox::GetString(unsigned int n) const
385 {
386 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid radiobox") );
387
388 wxList::compatibility_iterator node = m_boxes.Item( n );
389
390 wxCHECK_MSG( node, wxEmptyString, wxT("radiobox wrong index") );
391
392 GtkLabel *label = GTK_LABEL(GTK_BIN(node->GetData())->child);
393
394 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
395
396 return str;
397 }
398
399 void wxRadioBox::SetLabel( const wxString& label )
400 {
401 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
402
403 GTKSetLabelForFrame(GTK_FRAME(m_widget), label);
404 }
405
406 void wxRadioBox::SetString(unsigned int item, const wxString& label)
407 {
408 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
409
410 wxList::compatibility_iterator node = m_boxes.Item( item );
411
412 wxCHECK_RET( node, wxT("radiobox wrong index") );
413
414 GtkLabel *g_label = GTK_LABEL(GTK_BIN(node->GetData())->child);
415
416 gtk_label_set_text( g_label, wxGTK_CONV( label ) );
417 }
418
419 bool wxRadioBox::Enable( bool enable )
420 {
421 if ( !wxControl::Enable( enable ) )
422 return false;
423
424 wxList::compatibility_iterator node = m_boxes.GetFirst();
425 while (node)
426 {
427 GtkButton *button = GTK_BUTTON( node->GetData() );
428 GtkLabel *label = GTK_LABEL(GTK_BIN(button)->child);
429
430 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
431 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
432 node = node->GetNext();
433 }
434
435 return true;
436 }
437
438 bool wxRadioBox::Enable(unsigned int item, bool enable)
439 {
440 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
441
442 wxList::compatibility_iterator node = m_boxes.Item( item );
443
444 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
445
446 GtkButton *button = GTK_BUTTON( node->GetData() );
447 GtkLabel *label = GTK_LABEL(GTK_BIN(button)->child);
448
449 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
450 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
451
452 return true;
453 }
454
455 bool wxRadioBox::IsItemEnabled(unsigned int item) const
456 {
457 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
458
459 wxList::compatibility_iterator node = m_boxes.Item( item );
460
461 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
462
463 GtkButton *button = GTK_BUTTON( node->GetData() );
464
465 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
466 // the parent radiobox is disabled
467 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button));
468 }
469
470 bool wxRadioBox::Show(unsigned int item, bool show)
471 {
472 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
473
474 wxList::compatibility_iterator node = m_boxes.Item( item );
475
476 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
477
478 GtkWidget *button = GTK_WIDGET( node->GetData() );
479
480 if (show)
481 gtk_widget_show( button );
482 else
483 gtk_widget_hide( button );
484
485 return true;
486 }
487
488 bool wxRadioBox::IsItemShown(unsigned int item) const
489 {
490 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
491
492 wxList::compatibility_iterator node = m_boxes.Item( item );
493
494 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
495
496 GtkButton *button = GTK_BUTTON( node->GetData() );
497
498 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button));
499 }
500
501 unsigned int wxRadioBox::GetCount() const
502 {
503 return m_boxes.GetCount();
504 }
505
506 void wxRadioBox::GtkDisableEvents()
507 {
508 wxList::compatibility_iterator node = m_boxes.GetFirst();
509 while (node)
510 {
511 g_signal_handlers_disconnect_by_func (node->GetData(),
512 (gpointer) gtk_radiobutton_clicked_callback,
513 this);
514
515 node = node->GetNext();
516 }
517 }
518
519 void wxRadioBox::GtkEnableEvents()
520 {
521 wxList::compatibility_iterator node = m_boxes.GetFirst();
522 while (node)
523 {
524 g_signal_connect (node->GetData(), "clicked",
525 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
526
527 node = node->GetNext();
528 }
529 }
530
531 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle *style)
532 {
533 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget), style);
534
535 wxList::compatibility_iterator node = m_boxes.GetFirst();
536 while (node)
537 {
538 GtkWidget *widget = GTK_WIDGET( node->GetData() );
539
540 gtk_widget_modify_style( widget, style );
541 gtk_widget_modify_style(GTK_BIN(widget)->child, style);
542
543 node = node->GetNext();
544 }
545 }
546
547 bool wxRadioBox::GTKWidgetNeedsMnemonic() const
548 {
549 return true;
550 }
551
552 void wxRadioBox::GTKWidgetDoSetMnemonic(GtkWidget* w)
553 {
554 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget), w);
555 }
556
557 #if wxUSE_TOOLTIPS
558 void wxRadioBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
559 {
560 wxList::compatibility_iterator node = m_boxes.GetFirst();
561 while (node)
562 {
563 GtkWidget *widget = GTK_WIDGET( node->GetData() );
564 gtk_tooltips_set_tip( tips, widget, wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
565 node = node->GetNext();
566 }
567 }
568 #endif // wxUSE_TOOLTIPS
569
570 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
571 {
572 if (window == m_widget->window)
573 return true;
574
575 wxList::compatibility_iterator node = m_boxes.GetFirst();
576 while (node)
577 {
578 GtkWidget *button = GTK_WIDGET( node->GetData() );
579
580 if (window == button->window)
581 return true;
582
583 node = node->GetNext();
584 }
585
586 return false;
587 }
588
589 void wxRadioBox::OnInternalIdle()
590 {
591 if ( m_lostFocus )
592 {
593 m_hasFocus = false;
594 m_lostFocus = false;
595
596 wxFocusEvent event( wxEVT_KILL_FOCUS, GetId() );
597 event.SetEventObject( this );
598
599 (void)GetEventHandler()->ProcessEvent( event );
600 }
601
602 if (g_delayedFocus == this)
603 {
604 if (GTK_WIDGET_REALIZED(m_widget))
605 {
606 g_delayedFocus = NULL;
607 SetFocus();
608 }
609 }
610 }
611
612 // static
613 wxVisualAttributes
614 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
615 {
616 wxVisualAttributes attr;
617 // NB: we need toplevel window so that GTK+ can find the right style
618 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
619 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
620 gtk_container_add(GTK_CONTAINER(wnd), widget);
621 attr = GetDefaultAttributesFromGTKWidget(widget);
622 gtk_widget_destroy(wnd);
623 return attr;
624 }
625
626 #endif // wxUSE_RADIOBOX