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