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