]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/radiobox.cpp
don't use -q option with egrep, Solaris doesn't have it (bug 517145)
[wxWidgets.git] / src / gtk1 / radiobox.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: radiobox.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "radiobox.h"
12#endif
13
14#include "wx/defs.h"
15
16#if wxUSE_RADIOBOX
17
18#include "wx/radiobox.h"
19
20#include "wx/dialog.h"
21#include "wx/frame.h"
22#include "wx/log.h"
23
24#include <gdk/gdk.h>
25#include <gtk/gtk.h>
26#include <gdk/gdkkeysyms.h>
27
28#include "wx/gtk/win_gtk.h"
29
30//-----------------------------------------------------------------------------
31// idle system
32//-----------------------------------------------------------------------------
33
34extern void wxapp_install_idle_handler();
35extern bool g_isIdle;
36
37//-----------------------------------------------------------------------------
38// data
39//-----------------------------------------------------------------------------
40
41extern bool g_blockEventsOnDrag;
42
43//-----------------------------------------------------------------------------
44// "clicked"
45//-----------------------------------------------------------------------------
46
47static void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioBox *rb )
48{
49 if (g_isIdle) wxapp_install_idle_handler();
50
51 if (!rb->m_hasVMT) return;
52 if (g_blockEventsOnDrag) return;
53
54 if (!button->active) return;
55
56 wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() );
57 event.SetInt( rb->GetSelection() );
58 event.SetString( rb->GetStringSelection() );
59 event.SetEventObject( rb );
60 rb->GetEventHandler()->ProcessEvent(event);
61}
62
63//-----------------------------------------------------------------------------
64// "key_press_event"
65//-----------------------------------------------------------------------------
66
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 wxNode *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.First())
95 node = rb->m_boxes.Last();
96 else
97 node = node->Previous();
98 }
99 else
100 {
101 if (node == rb->m_boxes.Last())
102 node = rb->m_boxes.First();
103 else
104 node = node->Next();
105 }
106
107 GtkWidget *button = (GtkWidget*) node->Data();
108
109 gtk_widget_grab_focus( button );
110
111 return TRUE;
112}
113
114static gint gtk_radiobutton_focus_in( GtkWidget *widget,
115 GdkEvent *WXUNUSED(event),
116 wxRadioBox *win )
117{
118 if ( win->m_lostFocus )
119 {
120 // no, we didn't really lose it
121 win->m_lostFocus = FALSE;
122 }
123 else if ( !win->m_hasFocus )
124 {
125 win->m_hasFocus = TRUE;
126
127 wxFocusEvent event( wxEVT_SET_FOCUS, win->GetId() );
128 event.SetEventObject( win );
129
130 // never stop the signal emission, it seems to break the kbd handling
131 // inside the radiobox
132 (void)win->GetEventHandler()->ProcessEvent( event );
133 }
134
135 return FALSE;
136}
137
138static gint gtk_radiobutton_focus_out( GtkWidget *widget,
139 GdkEvent *WXUNUSED(event),
140 wxRadioBox *win )
141{
142 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
143 // Replace with a warning, else we dump core a lot!
144 // if (!win->m_hasFocus)
145 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
146
147 // we might have lost the focus, but may be not - it may have just gone to
148 // another button in the same radiobox, so we'll check for it in the next
149 // idle iteration (leave m_hasFocus == TRUE for now)
150 win->m_lostFocus = TRUE;
151
152 return FALSE;
153}
154
155//-----------------------------------------------------------------------------
156// wxRadioBox
157//-----------------------------------------------------------------------------
158
159IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
160
161void wxRadioBox::Init()
162{
163 m_needParent = TRUE;
164 m_acceptsFocus = TRUE;
165
166 m_hasFocus =
167 m_lostFocus = FALSE;
168}
169
170bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
171 const wxPoint &pos, const wxSize &size,
172 int n, const wxString choices[], int majorDim,
173 long style, const wxValidator& validator,
174 const wxString &name )
175{
176 if (!PreCreation( parent, pos, size ) ||
177 !CreateBase( parent, id, pos, size, style, validator, name ))
178 {
179 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
180 return FALSE;
181 }
182
183 m_widget = gtk_frame_new( title.mbc_str() );
184
185 // majorDim may be 0 if all trailing parameters were omitted, so don't
186 // assert here but just use the correct value for it
187 m_majorDim = majorDim == 0 ? n : majorDim;
188
189 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
190
191 wxString label;
192 GSList *radio_button_group = (GSList *) NULL;
193 for (int i = 0; i < n; i++)
194 {
195 if ( i != 0 )
196 radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
197
198 label.Empty();
199 for ( const wxChar *pc = choices[i]; *pc; pc++ )
200 {
201 if ( *pc != wxT('&') )
202 label += *pc;
203 }
204
205 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, label.mbc_str() ) );
206
207 gtk_signal_connect( GTK_OBJECT(m_radio), "key_press_event",
208 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback), (gpointer)this );
209
210 m_boxes.Append( (wxObject*) m_radio );
211
212 ConnectWidget( GTK_WIDGET(m_radio) );
213
214 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
215
216 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
217 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
218
219 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_in_event",
220 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in), (gpointer)this );
221
222 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_out_event",
223 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out), (gpointer)this );
224
225 gtk_pizza_put( GTK_PIZZA(m_parent->m_wxwindow),
226 GTK_WIDGET(m_radio),
227 m_x+10, m_y+10+(i*24), 10, 10 );
228 }
229
230 m_parent->DoAddChild( this );
231
232 PostCreation();
233
234 ApplyWidgetStyle();
235
236 SetLabel( title );
237
238 SetFont( parent->GetFont() );
239
240 wxSize ls = LayoutItems();
241
242 GtkRequisition req;
243 req.width = 2;
244 req.height = 2;
245 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request ) (m_widget, &req );
246 if (req.width > ls.x) ls.x = req.width;
247
248 wxSize newSize = size;
249 if (newSize.x == -1) newSize.x = ls.x;
250 if (newSize.y == -1) newSize.y = ls.y;
251 SetSize( newSize.x, newSize.y );
252
253 SetBackgroundColour( parent->GetBackgroundColour() );
254 SetForegroundColour( parent->GetForegroundColour() );
255
256 Show( TRUE );
257
258 return TRUE;
259}
260
261wxRadioBox::~wxRadioBox()
262{
263 wxNode *node = m_boxes.First();
264 while (node)
265 {
266 GtkWidget *button = GTK_WIDGET( node->Data() );
267 gtk_widget_destroy( button );
268 node = node->Next();
269 }
270}
271
272void wxRadioBox::DoSetSize( int x, int y, int width, int height, int sizeFlags )
273{
274 wxWindow::DoSetSize( x, y, width, height, sizeFlags );
275
276 LayoutItems();
277}
278
279wxSize wxRadioBox::LayoutItems()
280{
281 int x = 7;
282 int y = 15;
283
284 if ( m_majorDim == 0 )
285 {
286 // avoid dividing by 0 below
287 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
288
289 m_majorDim = 1;
290 }
291
292 int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1;
293
294 wxSize res( 0, 0 );
295
296 int num_of_cols = 0;
297 int num_of_rows = 0;
298 if (HasFlag(wxRA_SPECIFY_COLS))
299 {
300 num_of_cols = m_majorDim;
301 num_of_rows = num_per_major;
302 }
303 else
304 {
305 num_of_cols = num_per_major;
306 num_of_rows = m_majorDim;
307 }
308
309 if ( HasFlag(wxRA_SPECIFY_COLS) ||
310 (HasFlag(wxRA_SPECIFY_ROWS) && (num_of_cols > 1)) )
311 {
312 for (int j = 0; j < num_of_cols; j++)
313 {
314 y = 15;
315
316 int max_len = 0;
317 wxNode *node = m_boxes.Nth( j*num_of_rows );
318 for (int i1 = 0; i1< num_of_rows; i1++)
319 {
320 GtkWidget *button = GTK_WIDGET( node->Data() );
321
322 GtkRequisition req;
323 req.width = 2;
324 req.height = 2;
325 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button) )->size_request )
326 (button, &req );
327
328 if (req.width > max_len) max_len = req.width;
329
330 gtk_pizza_move( GTK_PIZZA(m_parent->m_wxwindow), button, m_x+x, m_y+y );
331 y += req.height;
332
333 node = node->Next();
334 if (!node) break;
335 }
336
337 // we don't know the max_len before
338
339 node = m_boxes.Nth( j*num_of_rows );
340 for (int i2 = 0; i2< num_of_rows; i2++)
341 {
342 GtkWidget *button = GTK_WIDGET( node->Data() );
343
344 gtk_pizza_resize( GTK_PIZZA(m_parent->m_wxwindow), button, max_len, 20 );
345
346 node = node->Next();
347 if (!node) break;
348 }
349
350 if (y > res.y) res.y = y;
351
352 x += max_len + 2;
353 }
354
355 res.x = x+4;
356 res.y += 4;
357 }
358 else
359 {
360 int max = 0;
361
362 wxNode *node = m_boxes.First();
363 while (node)
364 {
365 GtkWidget *button = GTK_WIDGET( node->Data() );
366
367 GtkRequisition req;
368 req.width = 2;
369 req.height = 2;
370 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button) )->size_request )
371 (button, &req );
372
373 if (req.width > max) max = req.width;
374
375 node = node->Next();
376 }
377
378 node = m_boxes.First();
379 while (node)
380 {
381 GtkWidget *button = GTK_WIDGET( node->Data() );
382
383 gtk_pizza_set_size( GTK_PIZZA(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
384 x += max;
385
386 node = node->Next();
387 }
388 res.x = x+4;
389 res.y = 40;
390 }
391
392 return res;
393}
394
395bool wxRadioBox::Show( bool show )
396{
397 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") );
398
399 if (!wxControl::Show(show))
400 {
401 // nothing to do
402 return FALSE;
403 }
404
405 if ((m_windowStyle & wxNO_BORDER) != 0)
406 gtk_widget_hide( m_widget );
407
408 wxNode *node = m_boxes.First();
409 while (node)
410 {
411 GtkWidget *button = GTK_WIDGET( node->Data() );
412
413 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
414
415 node = node->Next();
416 }
417
418 return TRUE;
419}
420
421int wxRadioBox::FindString( const wxString &s ) const
422{
423 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") );
424
425 int count = 0;
426
427 wxNode *node = m_boxes.First();
428 while (node)
429 {
430 GtkButton *button = GTK_BUTTON( node->Data() );
431
432 GtkLabel *label = GTK_LABEL( button->child );
433 if (s == label->label) return count;
434 count++;
435
436 node = node->Next();
437 }
438
439 return -1;
440}
441
442void wxRadioBox::SetFocus()
443{
444 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
445
446 if (m_boxes.GetCount() == 0) return;
447
448 wxNode *node = m_boxes.First();
449 while (node)
450 {
451 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
452 if (button->active)
453 {
454 gtk_widget_grab_focus( GTK_WIDGET(button) );
455 return;
456 }
457 node = node->Next();
458 }
459
460}
461
462void wxRadioBox::SetSelection( int n )
463{
464 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
465
466 wxNode *node = m_boxes.Nth( n );
467
468 wxCHECK_RET( node, wxT("radiobox wrong index") );
469
470 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
471
472 GtkDisableEvents();
473
474 gtk_toggle_button_set_active( button, 1 );
475
476 GtkEnableEvents();
477}
478
479int wxRadioBox::GetSelection(void) const
480{
481 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") );
482
483 int count = 0;
484
485 wxNode *node = m_boxes.First();
486 while (node)
487 {
488 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
489 if (button->active) return count;
490 count++;
491 node = node->Next();
492 }
493
494 wxFAIL_MSG( wxT("wxRadioBox none selected") );
495
496 return -1;
497}
498
499wxString wxRadioBox::GetString( int n ) const
500{
501 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
502
503 wxNode *node = m_boxes.Nth( n );
504
505 wxCHECK_MSG( node, wxT(""), wxT("radiobox wrong index") );
506
507 GtkButton *button = GTK_BUTTON( node->Data() );
508 GtkLabel *label = GTK_LABEL( button->child );
509
510 return wxString( label->label );
511}
512
513wxString wxRadioBox::GetLabel( int item ) const
514{
515 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
516
517 return GetString( item );
518}
519
520void wxRadioBox::SetLabel( const wxString& label )
521{
522 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
523
524 wxControl::SetLabel( label );
525
526 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
527}
528
529void wxRadioBox::SetLabel( int item, const wxString& label )
530{
531 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
532
533 wxNode *node = m_boxes.Nth( item );
534
535 wxCHECK_RET( node, wxT("radiobox wrong index") );
536
537 GtkButton *button = GTK_BUTTON( node->Data() );
538 GtkLabel *g_label = GTK_LABEL( button->child );
539
540 gtk_label_set( g_label, label.mbc_str() );
541}
542
543void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
544{
545 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
546}
547
548bool wxRadioBox::Enable( bool enable )
549{
550 if ( !wxControl::Enable( enable ) )
551 return FALSE;
552
553 wxNode *node = m_boxes.First();
554 while (node)
555 {
556 GtkButton *button = GTK_BUTTON( node->Data() );
557 GtkWidget *label = button->child;
558 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
559 gtk_widget_set_sensitive( label, enable );
560 node = node->Next();
561 }
562
563 return TRUE;
564}
565
566void wxRadioBox::Enable( int item, bool enable )
567{
568 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
569
570 wxNode *node = m_boxes.Nth( item );
571
572 wxCHECK_RET( node, wxT("radiobox wrong index") );
573
574 GtkButton *button = GTK_BUTTON( node->Data() );
575 GtkWidget *label = button->child;
576 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
577 gtk_widget_set_sensitive( label, enable );
578}
579
580void wxRadioBox::Show( int item, bool show )
581{
582 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
583
584 wxNode *node = m_boxes.Nth( item );
585
586 wxCHECK_RET( node, wxT("radiobox wrong index") );
587
588 GtkWidget *button = GTK_WIDGET( node->Data() );
589
590 if (show)
591 gtk_widget_show( button );
592 else
593 gtk_widget_hide( button );
594}
595
596wxString wxRadioBox::GetStringSelection() const
597{
598 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
599
600 wxNode *node = m_boxes.First();
601 while (node)
602 {
603 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
604 if (button->active)
605 {
606 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
607 return label->label;
608 }
609 node = node->Next();
610 }
611
612 wxFAIL_MSG( wxT("wxRadioBox none selected") );
613 return wxT("");
614}
615
616bool wxRadioBox::SetStringSelection( const wxString &s )
617{
618 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") );
619
620 int res = FindString( s );
621 if (res == -1) return FALSE;
622 SetSelection( res );
623
624 return TRUE;
625}
626
627int wxRadioBox::Number() const
628{
629 return m_boxes.Number();
630}
631
632int wxRadioBox::GetNumberOfRowsOrCols() const
633{
634 return 1;
635}
636
637void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
638{
639 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
640}
641
642void wxRadioBox::GtkDisableEvents()
643{
644 wxNode *node = m_boxes.First();
645 while (node)
646 {
647 gtk_signal_disconnect_by_func( GTK_OBJECT(node->Data()),
648 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
649
650 node = node->Next();
651 }
652}
653
654void wxRadioBox::GtkEnableEvents()
655{
656 wxNode *node = m_boxes.First();
657 while (node)
658 {
659 gtk_signal_connect( GTK_OBJECT(node->Data()), "clicked",
660 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
661
662 node = node->Next();
663 }
664}
665
666void wxRadioBox::ApplyWidgetStyle()
667{
668 SetWidgetStyle();
669
670 gtk_widget_set_style( m_widget, m_widgetStyle );
671
672 wxNode *node = m_boxes.First();
673 while (node)
674 {
675 GtkWidget *widget = GTK_WIDGET( node->Data() );
676 gtk_widget_set_style( widget, m_widgetStyle );
677
678 GtkButton *button = GTK_BUTTON( node->Data() );
679 gtk_widget_set_style( button->child, m_widgetStyle );
680
681 node = node->Next();
682 }
683}
684
685#if wxUSE_TOOLTIPS
686void wxRadioBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
687{
688 wxNode *node = m_boxes.First();
689 while (node)
690 {
691 GtkWidget *widget = GTK_WIDGET( node->Data() );
692 gtk_tooltips_set_tip( tips, widget, wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
693 node = node->Next();
694 }
695}
696#endif // wxUSE_TOOLTIPS
697
698bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
699{
700 if (window == m_widget->window) return TRUE;
701
702 wxNode *node = m_boxes.First();
703 while (node)
704 {
705 GtkWidget *button = GTK_WIDGET( node->Data() );
706
707 if (window == button->window) return TRUE;
708
709 node = node->Next();
710 }
711
712 return FALSE;
713}
714
715void wxRadioBox::OnInternalIdle()
716{
717 if ( m_lostFocus )
718 {
719 m_hasFocus = FALSE;
720 m_lostFocus = FALSE;
721
722 wxFocusEvent event( wxEVT_KILL_FOCUS, GetId() );
723 event.SetEventObject( this );
724
725 (void)GetEventHandler()->ProcessEvent( event );
726 }
727}
728
729#endif // wxUSE_RADIOBOX
730