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