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