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