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