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