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