1. derive wxGTK wxRadioBox from wxRadioBoxBase now, as in all other ports
[wxWidgets.git] / src / gtk1 / 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 gtk_signal_emit_stop_by_name( GTK_OBJECT(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( wxGTK_CONV( title ) );
202
203 // majorDim may be 0 if all trailing parameters were omitted, so don't
204 // assert here but just use the correct value for it
205 SetMajorDim(majorDim == 0 ? n : majorDim, style);
206
207
208 int num_of_cols = GetColumnCount();
209 int num_of_rows = GetRowCount();
210
211 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
212
213 GtkWidget *table = gtk_table_new( num_of_rows, num_of_cols, FALSE );
214 gtk_table_set_col_spacings( GTK_TABLE(table), 1 );
215 gtk_table_set_row_spacings( GTK_TABLE(table), 1 );
216 gtk_widget_show( table );
217 gtk_container_add( GTK_CONTAINER(m_widget), table );
218
219 wxString label;
220 GSList *radio_button_group = (GSList *) NULL;
221 for (int i = 0; i < n; i++)
222 {
223 if ( i != 0 )
224 radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
225
226 label.Empty();
227 for ( const wxChar *pc = choices[i]; *pc; pc++ )
228 {
229 if ( *pc != wxT('&') )
230 label += *pc;
231 }
232
233 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, wxGTK_CONV( label ) ) );
234 gtk_widget_show( GTK_WIDGET(m_radio) );
235
236 gtk_signal_connect( GTK_OBJECT(m_radio), "key_press_event",
237 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback), (gpointer)this );
238
239 m_boxes.Append( (wxObject*) m_radio );
240
241 if (HasFlag(wxRA_SPECIFY_COLS))
242 {
243 int left = i%num_of_cols;
244 int right = (i%num_of_cols) + 1;
245 int top = i/num_of_cols;
246 int bottom = (i/num_of_cols)+1;
247 gtk_table_attach( GTK_TABLE(table), GTK_WIDGET(m_radio), left, right, top, bottom,
248 GTK_FILL, GTK_FILL, 1, 1 );
249 }
250 else
251 {
252 int left = i/num_of_rows;
253 int right = (i/num_of_rows) + 1;
254 int top = i%num_of_rows;
255 int bottom = (i%num_of_rows)+1;
256 gtk_table_attach( GTK_TABLE(table), GTK_WIDGET(m_radio), left, right, top, bottom,
257 GTK_FILL, GTK_FILL, 1, 1 );
258 }
259
260 ConnectWidget( GTK_WIDGET(m_radio) );
261
262 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
263
264 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
265 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
266
267 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_in_event",
268 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in), (gpointer)this );
269
270 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_out_event",
271 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out), (gpointer)this );
272 }
273
274 m_parent->DoAddChild( this );
275
276 SetLabel( title );
277
278 PostCreation(size);
279
280 return true;
281 }
282
283 wxRadioBox::~wxRadioBox()
284 {
285 wxList::compatibility_iterator node = m_boxes.GetFirst();
286 while (node)
287 {
288 GtkWidget *button = GTK_WIDGET( node->GetData() );
289 gtk_widget_destroy( button );
290 node = node->GetNext();
291 }
292 }
293
294 bool wxRadioBox::Show( bool show )
295 {
296 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
297
298 if (!wxControl::Show(show))
299 {
300 // nothing to do
301 return false;
302 }
303
304 if ( HasFlag(wxNO_BORDER) )
305 gtk_widget_hide( m_widget );
306
307 wxList::compatibility_iterator node = m_boxes.GetFirst();
308 while (node)
309 {
310 GtkWidget *button = GTK_WIDGET( node->GetData() );
311
312 if (show)
313 gtk_widget_show( button );
314 else
315 gtk_widget_hide( button );
316
317 node = node->GetNext();
318 }
319
320 return true;
321 }
322
323 void wxRadioBox::SetFocus()
324 {
325 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
326
327 if (m_boxes.GetCount() == 0) return;
328
329 wxList::compatibility_iterator node = m_boxes.GetFirst();
330 while (node)
331 {
332 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
333 if (button->active)
334 {
335 gtk_widget_grab_focus( GTK_WIDGET(button) );
336 return;
337 }
338 node = node->GetNext();
339 }
340 }
341
342 void wxRadioBox::SetSelection( int n )
343 {
344 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
345
346 wxList::compatibility_iterator node = m_boxes.Item( n );
347
348 wxCHECK_RET( node, wxT("radiobox wrong index") );
349
350 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
351
352 GtkDisableEvents();
353
354 gtk_toggle_button_set_active( button, 1 );
355
356 GtkEnableEvents();
357 }
358
359 int wxRadioBox::GetSelection(void) const
360 {
361 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid radiobox") );
362
363 int count = 0;
364
365 wxList::compatibility_iterator node = m_boxes.GetFirst();
366 while (node)
367 {
368 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
369 if (button->active) return count;
370 count++;
371 node = node->GetNext();
372 }
373
374 wxFAIL_MSG( wxT("wxRadioBox none selected") );
375
376 return wxNOT_FOUND;
377 }
378
379 wxString wxRadioBox::GetString( int n ) const
380 {
381 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid radiobox") );
382
383 wxList::compatibility_iterator node = m_boxes.Item( n );
384
385 wxCHECK_MSG( node, wxEmptyString, wxT("radiobox wrong index") );
386
387 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
388
389 #ifdef __WXGTK20__
390 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
391 #else
392 wxString str( label->label );
393 #endif
394
395 return str;
396 }
397
398 void wxRadioBox::SetLabel( const wxString& label )
399 {
400 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
401
402 wxControl::SetLabel( label );
403
404 gtk_frame_set_label( GTK_FRAME(m_widget), wxGTK_CONV( wxControl::GetLabel() ) );
405 }
406
407 void wxRadioBox::SetString( int item, const wxString& label )
408 {
409 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
410
411 wxList::compatibility_iterator node = m_boxes.Item( item );
412
413 wxCHECK_RET( node, wxT("radiobox wrong index") );
414
415 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
416
417 gtk_label_set( g_label, wxGTK_CONV( label ) );
418 }
419
420 bool wxRadioBox::Enable( bool enable )
421 {
422 if ( !wxControl::Enable( enable ) )
423 return false;
424
425 wxList::compatibility_iterator node = m_boxes.GetFirst();
426 while (node)
427 {
428 GtkButton *button = GTK_BUTTON( node->GetData() );
429 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(button) );
430
431 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
432 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
433 node = node->GetNext();
434 }
435
436 return true;
437 }
438
439 bool wxRadioBox::Enable( int item, bool enable )
440 {
441 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
442
443 wxList::compatibility_iterator node = m_boxes.Item( item );
444
445 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
446
447 GtkButton *button = GTK_BUTTON( node->GetData() );
448 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(button) );
449
450 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
451 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
452
453 return true;
454 }
455
456 bool wxRadioBox::IsItemEnabled(int item) const
457 {
458 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
459
460 wxList::compatibility_iterator node = m_boxes.Item( item );
461
462 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
463
464 GtkButton *button = GTK_BUTTON( node->GetData() );
465
466 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
467 // the parent radiobox is disabled
468 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button));
469 }
470
471 bool wxRadioBox::Show( int item, bool show )
472 {
473 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
474
475 wxList::compatibility_iterator node = m_boxes.Item( item );
476
477 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
478
479 GtkWidget *button = GTK_WIDGET( node->GetData() );
480
481 if (show)
482 gtk_widget_show( button );
483 else
484 gtk_widget_hide( button );
485
486 return true;
487 }
488
489 bool wxRadioBox::IsItemShown(int item) const
490 {
491 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
492
493 wxList::compatibility_iterator node = m_boxes.Item( item );
494
495 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
496
497 GtkButton *button = GTK_BUTTON( node->GetData() );
498
499 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button));
500 }
501
502 int wxRadioBox::GetCount() const
503 {
504 return m_boxes.GetCount();
505 }
506
507 void wxRadioBox::GtkDisableEvents()
508 {
509 wxList::compatibility_iterator node = m_boxes.GetFirst();
510 while (node)
511 {
512 gtk_signal_disconnect_by_func( GTK_OBJECT(node->GetData()),
513 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)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 gtk_signal_connect( GTK_OBJECT(node->GetData()), "clicked",
525 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
526
527 node = node->GetNext();
528 }
529 }
530
531 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle *style)
532 {
533 gtk_widget_modify_style( m_widget, style );
534
535 #ifdef __WXGTK20__
536 gtk_widget_modify_style(GTK_FRAME(m_widget)->label_widget, style);
537 #endif
538
539 wxList::compatibility_iterator node = m_boxes.GetFirst();
540 while (node)
541 {
542 GtkWidget *widget = GTK_WIDGET( node->GetData() );
543
544 gtk_widget_modify_style( widget, style );
545 gtk_widget_modify_style( BUTTON_CHILD(node->GetData()), style );
546
547 node = node->GetNext();
548 }
549 }
550
551 #if wxUSE_TOOLTIPS
552 void wxRadioBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
553 {
554 wxList::compatibility_iterator node = m_boxes.GetFirst();
555 while (node)
556 {
557 GtkWidget *widget = GTK_WIDGET( node->GetData() );
558 gtk_tooltips_set_tip( tips, widget, wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
559 node = node->GetNext();
560 }
561 }
562 #endif // wxUSE_TOOLTIPS
563
564 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
565 {
566 if (window == m_widget->window)
567 return true;
568
569 wxList::compatibility_iterator node = m_boxes.GetFirst();
570 while (node)
571 {
572 GtkWidget *button = GTK_WIDGET( node->GetData() );
573
574 if (window == button->window)
575 return true;
576
577 node = node->GetNext();
578 }
579
580 return false;
581 }
582
583 void wxRadioBox::OnInternalIdle()
584 {
585 if ( m_lostFocus )
586 {
587 m_hasFocus = false;
588 m_lostFocus = false;
589
590 wxFocusEvent event( wxEVT_KILL_FOCUS, GetId() );
591 event.SetEventObject( this );
592
593 (void)GetEventHandler()->ProcessEvent( event );
594 }
595
596 if (g_delayedFocus == this)
597 {
598 if (GTK_WIDGET_REALIZED(m_widget))
599 {
600 g_delayedFocus = NULL;
601 SetFocus();
602 }
603 }
604 }
605
606 // static
607 wxVisualAttributes
608 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
609 {
610 wxVisualAttributes attr;
611 // NB: we need toplevel window so that GTK+ can find the right style
612 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
613 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
614 gtk_container_add(GTK_CONTAINER(wnd), widget);
615 attr = GetDefaultAttributesFromGTKWidget(widget);
616 gtk_widget_destroy(wnd);
617 return attr;
618 }
619
620 #endif // wxUSE_RADIOBOX