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