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