]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/radiobox.cpp
made wxHeaderCtrl::GetColumn() const to get rid of the const_cast<>s
[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->HandleWindowEvent(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()->HandleWindowEvent(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_out( GtkWidget * WXUNUSED(widget),
142 GdkEventFocus *WXUNUSED(event),
143 wxRadioBox *win )
144{
145 // NB: This control is composed of several GtkRadioButton widgets and
146 // when focus changes from one of them to another in the same
147 // wxRadioBox, we get a focus-out event followed by focus-in for
148 // another GtkRadioButton owned by the same control. We don't want
149 // to generate two spurious wxEVT_SET_FOCUS events in this case,
150 // so we defer sending wx events until idle time.
151 win->GTKHandleFocusOut();
152
153 // never stop the signal emission, it seems to break the kbd handling
154 // inside the radiobox
155 return FALSE;
156}
157}
158
159extern "C" {
160static gint gtk_radiobutton_focus_in( GtkWidget * WXUNUSED(widget),
161 GdkEventFocus *WXUNUSED(event),
162 wxRadioBox *win )
163{
164 win->GTKHandleFocusIn();
165
166 // never stop the signal emission, it seems to break the kbd handling
167 // inside the radiobox
168 return FALSE;
169}
170}
171
172extern "C" {
173static void gtk_radiobutton_size_allocate( GtkWidget *widget,
174 GtkAllocation * alloc,
175 wxRadioBox *win )
176{
177 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node = win->m_buttonsInfo.GetFirst();
178 node;
179 node = node->GetNext())
180 {
181 if (widget == GTK_WIDGET(node->GetData()->button))
182 {
183 const wxPoint origin = win->GetPosition();
184 wxRect rect = wxRect( alloc->x - origin.x, alloc->y - origin.y,
185 alloc->width, alloc->height );
186 node->GetData()->rect = rect;
187 break;
188 }
189 }
190}
191}
192
193
194//-----------------------------------------------------------------------------
195// wxRadioBox
196//-----------------------------------------------------------------------------
197
198IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
199
200bool wxRadioBox::Create( wxWindow *parent, wxWindowID id,
201 const wxString& title,
202 const wxPoint &pos, const wxSize &size,
203 const wxArrayString& choices, int majorDim,
204 long style, const wxValidator& validator,
205 const wxString &name )
206{
207 wxCArrayString chs(choices);
208
209 return Create( parent, id, title, pos, size, chs.GetCount(),
210 chs.GetStrings(), majorDim, style, validator, name );
211}
212
213bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
214 const wxPoint &pos, const wxSize &size,
215 int n, const wxString choices[], int majorDim,
216 long style, const wxValidator& validator,
217 const wxString &name )
218{
219 if (!PreCreation( parent, pos, size ) ||
220 !CreateBase( parent, id, pos, size, style, validator, name ))
221 {
222 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
223 return false;
224 }
225
226 m_widget = GTKCreateFrame(title);
227 g_object_ref(m_widget);
228 wxControl::SetLabel(title);
229 if ( HasFlag(wxNO_BORDER) )
230 {
231 // If we don't do this here, the wxNO_BORDER style is ignored in Show()
232 gtk_frame_set_shadow_type(GTK_FRAME(m_widget), GTK_SHADOW_NONE);
233 }
234
235
236 // majorDim may be 0 if all trailing parameters were omitted, so don't
237 // assert here but just use the correct value for it
238 SetMajorDim(majorDim == 0 ? n : majorDim, style);
239
240
241 unsigned int num_of_cols = GetColumnCount();
242 unsigned int num_of_rows = GetRowCount();
243
244 GtkRadioButton *rbtn = (GtkRadioButton*) NULL;
245
246 GtkWidget *table = gtk_table_new( num_of_rows, num_of_cols, FALSE );
247 gtk_table_set_col_spacings( GTK_TABLE(table), 1 );
248 gtk_table_set_row_spacings( GTK_TABLE(table), 1 );
249 gtk_widget_show( table );
250 gtk_container_add( GTK_CONTAINER(m_widget), table );
251
252 wxString label;
253 GSList *radio_button_group = (GSList *) NULL;
254 for (unsigned int i = 0; i < (unsigned int)n; i++)
255 {
256 if ( i != 0 )
257 radio_button_group = gtk_radio_button_get_group( GTK_RADIO_BUTTON(rbtn) );
258
259 label.Empty();
260 for ( wxString::const_iterator pc = choices[i].begin();
261 pc != choices[i].end(); ++pc )
262 {
263 if ( *pc != wxT('&') )
264 label += *pc;
265 }
266
267 rbtn = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, wxGTK_CONV( label ) ) );
268 gtk_widget_show( GTK_WIDGET(rbtn) );
269
270 g_signal_connect (rbtn, "key_press_event",
271 G_CALLBACK (gtk_radiobox_keypress_callback), this);
272
273 m_buttonsInfo.Append( new wxGTKRadioButtonInfo( rbtn, wxRect() ) );
274
275 if (HasFlag(wxRA_SPECIFY_COLS))
276 {
277 int left = i%num_of_cols;
278 int right = (i%num_of_cols) + 1;
279 int top = i/num_of_cols;
280 int bottom = (i/num_of_cols)+1;
281 gtk_table_attach( GTK_TABLE(table), GTK_WIDGET(rbtn), left, right, top, bottom,
282 GTK_FILL, GTK_FILL, 1, 1 );
283 }
284 else
285 {
286 int left = i/num_of_rows;
287 int right = (i/num_of_rows) + 1;
288 int top = i%num_of_rows;
289 int bottom = (i%num_of_rows)+1;
290 gtk_table_attach( GTK_TABLE(table), GTK_WIDGET(rbtn), left, right, top, bottom,
291 GTK_FILL, GTK_FILL, 1, 1 );
292 }
293
294 ConnectWidget( GTK_WIDGET(rbtn) );
295
296 if (!i)
297 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(rbtn), TRUE );
298
299 g_signal_connect (rbtn, "clicked",
300 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
301 g_signal_connect (rbtn, "focus_in_event",
302 G_CALLBACK (gtk_radiobutton_focus_in), this);
303 g_signal_connect (rbtn, "focus_out_event",
304 G_CALLBACK (gtk_radiobutton_focus_out), this);
305 g_signal_connect (rbtn, "size_allocate",
306 G_CALLBACK (gtk_radiobutton_size_allocate), this);
307 }
308
309 m_parent->DoAddChild( this );
310
311 PostCreation(size);
312
313 return true;
314}
315
316wxRadioBox::~wxRadioBox()
317{
318 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
319 while (node)
320 {
321 GtkWidget *button = GTK_WIDGET( node->GetData()->button );
322 gtk_widget_destroy( button );
323 node = node->GetNext();
324 }
325 WX_CLEAR_LIST( wxRadioBoxButtonsInfoList, m_buttonsInfo );
326}
327
328bool wxRadioBox::Show( bool show )
329{
330 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
331
332 if (!wxControl::Show(show))
333 {
334 // nothing to do
335 return false;
336 }
337
338 if ( HasFlag(wxNO_BORDER) )
339 gtk_widget_hide( m_widget );
340
341 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
342 while (node)
343 {
344 GtkWidget *button = GTK_WIDGET( node->GetData()->button );
345
346 if (show)
347 gtk_widget_show( button );
348 else
349 gtk_widget_hide( button );
350
351 node = node->GetNext();
352 }
353
354 return true;
355}
356
357void wxRadioBox::SetSelection( int n )
358{
359 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
360
361 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( n );
362
363 wxCHECK_RET( node, wxT("radiobox wrong index") );
364
365 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData()->button );
366
367 GtkDisableEvents();
368
369 gtk_toggle_button_set_active( button, 1 );
370
371 GtkEnableEvents();
372}
373
374int wxRadioBox::GetSelection(void) const
375{
376 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid radiobox") );
377
378 int count = 0;
379
380 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
381 while (node)
382 {
383 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData()->button );
384 if (button->active) return count;
385 count++;
386 node = node->GetNext();
387 }
388
389 wxFAIL_MSG( wxT("wxRadioBox none selected") );
390
391 return wxNOT_FOUND;
392}
393
394wxString wxRadioBox::GetString(unsigned int n) const
395{
396 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid radiobox") );
397
398 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( n );
399
400 wxCHECK_MSG( node, wxEmptyString, wxT("radiobox wrong index") );
401
402 GtkLabel *label = GTK_LABEL(GTK_BIN(node->GetData()->button)->child);
403
404 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
405
406 return str;
407}
408
409void wxRadioBox::SetLabel( const wxString& label )
410{
411 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
412
413 GTKSetLabelForFrame(GTK_FRAME(m_widget), label);
414}
415
416void wxRadioBox::SetString(unsigned int item, const wxString& label)
417{
418 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
419
420 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( item );
421
422 wxCHECK_RET( node, wxT("radiobox wrong index") );
423
424 GtkLabel *g_label = GTK_LABEL(GTK_BIN(node->GetData()->button)->child);
425
426 gtk_label_set_text( g_label, wxGTK_CONV( label ) );
427}
428
429bool wxRadioBox::Enable( bool enable )
430{
431 if ( !wxControl::Enable( enable ) )
432 return false;
433
434 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
435 while (node)
436 {
437 GtkButton *button = GTK_BUTTON( node->GetData()->button );
438 GtkLabel *label = GTK_LABEL(GTK_BIN(button)->child);
439
440 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
441 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
442 node = node->GetNext();
443 }
444
445 return true;
446}
447
448bool wxRadioBox::Enable(unsigned int item, bool enable)
449{
450 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
451
452 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( item );
453
454 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
455
456 GtkButton *button = GTK_BUTTON( node->GetData()->button );
457 GtkLabel *label = GTK_LABEL(GTK_BIN(button)->child);
458
459 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
460 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
461
462 return true;
463}
464
465bool wxRadioBox::IsItemEnabled(unsigned int item) const
466{
467 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
468
469 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( item );
470
471 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
472
473 GtkButton *button = GTK_BUTTON( node->GetData()->button );
474
475 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
476 // the parent radiobox is disabled
477 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button));
478}
479
480bool wxRadioBox::Show(unsigned int item, bool show)
481{
482 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
483
484 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( item );
485
486 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
487
488 GtkWidget *button = GTK_WIDGET( node->GetData()->button );
489
490 if (show)
491 gtk_widget_show( button );
492 else
493 gtk_widget_hide( button );
494
495 return true;
496}
497
498bool wxRadioBox::IsItemShown(unsigned int item) const
499{
500 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
501
502 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( item );
503
504 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
505
506 GtkButton *button = GTK_BUTTON( node->GetData()->button );
507
508 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button));
509}
510
511unsigned int wxRadioBox::GetCount() const
512{
513 return m_buttonsInfo.GetCount();
514}
515
516void wxRadioBox::GtkDisableEvents()
517{
518 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
519 while (node)
520 {
521 g_signal_handlers_block_by_func(node->GetData()->button,
522 (gpointer)gtk_radiobutton_clicked_callback, this);
523
524 node = node->GetNext();
525 }
526}
527
528void wxRadioBox::GtkEnableEvents()
529{
530 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
531 while (node)
532 {
533 g_signal_handlers_unblock_by_func(node->GetData()->button,
534 (gpointer)gtk_radiobutton_clicked_callback, this);
535
536 node = node->GetNext();
537 }
538}
539
540void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle *style)
541{
542 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget), style);
543
544 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
545 while (node)
546 {
547 GtkWidget *widget = GTK_WIDGET( node->GetData()->button );
548
549 gtk_widget_modify_style( widget, style );
550 gtk_widget_modify_style(GTK_BIN(widget)->child, style);
551
552 node = node->GetNext();
553 }
554}
555
556bool wxRadioBox::GTKWidgetNeedsMnemonic() const
557{
558 return true;
559}
560
561void wxRadioBox::GTKWidgetDoSetMnemonic(GtkWidget* w)
562{
563 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget), w);
564}
565
566#if wxUSE_TOOLTIPS
567void wxRadioBox::ApplyToolTip(GtkTooltips * WXUNUSED(tips), const gchar *tip)
568{
569 // set this tooltip for all radiobuttons which don't have their own tips
570 unsigned n = 0;
571 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
572 node;
573 node = node->GetNext(), n++ )
574 {
575 if ( !GetItemToolTip(n) )
576 {
577 wxToolTip::Apply(GTK_WIDGET(node->GetData()->button), tip);
578 }
579 }
580}
581
582void wxRadioBox::DoSetItemToolTip(unsigned int n, wxToolTip *tooltip)
583{
584 wxCharBuffer buf;
585 if ( !tooltip )
586 tooltip = GetToolTip();
587 if ( tooltip )
588 buf = wxGTK_CONV(tooltip->GetTip());
589
590 wxToolTip::Apply(GTK_WIDGET(m_buttonsInfo[n]->button), buf);
591}
592
593#endif // wxUSE_TOOLTIPS
594
595GdkWindow *wxRadioBox::GTKGetWindow(wxArrayGdkWindows& windows) const
596{
597 windows.push_back(m_widget->window);
598
599 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
600 while (node)
601 {
602 GtkWidget *button = GTK_WIDGET( node->GetData()->button );
603
604 windows.push_back(button->window);
605
606 node = node->GetNext();
607 }
608
609 return NULL;
610}
611
612// static
613wxVisualAttributes
614wxRadioBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
615{
616 wxVisualAttributes attr;
617 // NB: we need toplevel window so that GTK+ can find the right style
618 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
619 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
620 gtk_container_add(GTK_CONTAINER(wnd), widget);
621 attr = GetDefaultAttributesFromGTKWidget(widget);
622 gtk_widget_destroy(wnd);
623 return attr;
624}
625
626int wxRadioBox::GetItemFromPoint(const wxPoint& point) const
627{
628 const wxPoint pt = ScreenToClient(point);
629 unsigned n = 0;
630 for ( wxRadioBoxButtonsInfoList::compatibility_iterator
631 node = m_buttonsInfo.GetFirst(); node; node = node->GetNext(), n++ )
632 {
633 if ( m_buttonsInfo[n]->rect.Contains(pt) )
634 return n;
635 }
636
637 return wxNOT_FOUND;
638}
639
640#endif // wxUSE_RADIOBOX