Minor resize probs solved.
[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
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 ApplyWidgetStyle();
188
189 SetLabel( title );
190
191 SetFont( parent->GetFont() );
192
193 wxSize ls = LayoutItems();
194
195 wxSize newSize = size;
196 if (newSize.x == -1) newSize.x = ls.x;
197 if (newSize.y == -1) newSize.y = ls.y;
198 SetSize( newSize.x, newSize.y );
199
200 SetBackgroundColour( parent->GetBackgroundColour() );
201 SetForegroundColour( parent->GetForegroundColour() );
202
203 Show( TRUE );
204
205 return TRUE;
206 }
207
208 wxRadioBox::~wxRadioBox()
209 {
210 wxNode *node = m_boxes.First();
211 while (node)
212 {
213 GtkWidget *button = GTK_WIDGET( node->Data() );
214 gtk_widget_destroy( button );
215 node = node->Next();
216 }
217 }
218
219 void wxRadioBox::DoSetSize( int x, int y, int width, int height, int sizeFlags )
220 {
221 wxWindow::DoSetSize( x, y, width, height, sizeFlags );
222
223 LayoutItems();
224 }
225
226 wxSize wxRadioBox::LayoutItems()
227 {
228 int x = 7;
229 int y = 15;
230
231 if ( m_majorDim == 0 )
232 {
233 // avoid dividing by 0 below
234 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
235
236 m_majorDim = 1;
237 }
238
239 int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1;
240
241 wxSize res( 0, 0 );
242
243 int num_of_cols = 0;
244 int num_of_rows = 0;
245 if (HasFlag(wxRA_SPECIFY_COLS))
246 {
247 num_of_cols = m_majorDim;
248 num_of_rows = num_per_major;
249 }
250 else
251 {
252 num_of_cols = num_per_major;
253 num_of_rows = m_majorDim;
254 }
255
256 if ( HasFlag(wxRA_SPECIFY_COLS) ||
257 (HasFlag(wxRA_SPECIFY_ROWS) && (num_of_cols > 1)) )
258 {
259 for (int j = 0; j < num_of_cols; j++)
260 {
261 y = 15;
262
263 int max_len = 0;
264 wxNode *node = m_boxes.Nth( j*num_of_rows );
265 for (int i1 = 0; i1< num_of_rows; i1++)
266 {
267 GtkWidget *button = GTK_WIDGET( node->Data() );
268 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
269 GdkFont *font = m_widget->style->font;
270 int len = 22+gdk_string_measure( font, label->label );
271 if (len > max_len) max_len = len;
272
273 gtk_pizza_move( GTK_PIZZA(m_parent->m_wxwindow), button, m_x+x, m_y+y );
274 y += 22;
275
276 node = node->Next();
277 if (!node) break;
278 }
279
280 // we don't know the max_len before
281
282 node = m_boxes.Nth( j*num_of_rows );
283 for (int i2 = 0; i2< num_of_rows; i2++)
284 {
285 GtkWidget *button = GTK_WIDGET( node->Data() );
286
287 gtk_pizza_resize( GTK_PIZZA(m_parent->m_wxwindow), button, max_len, 20 );
288
289 node = node->Next();
290 if (!node) break;
291 }
292
293 if (y > res.y) res.y = y;
294
295 x += max_len + 2;
296 }
297
298 res.x = x+4;
299 res.y += 9;
300 }
301 else
302 {
303 int max = 0;
304
305 wxNode *node = m_boxes.First();
306 while (node)
307 {
308 GtkButton *button = GTK_BUTTON( node->Data() );
309 GtkLabel *label = GTK_LABEL( button->child );
310
311 GdkFont *font = m_widget->style->font;
312 int len = 22+gdk_string_measure( font, label->label );
313 if (len > max) max = len;
314
315 node = node->Next();
316 }
317
318 node = m_boxes.First();
319 while (node)
320 {
321 GtkWidget *button = GTK_WIDGET( node->Data() );
322
323 gtk_pizza_set_size( GTK_PIZZA(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
324 x += max;
325
326 node = node->Next();
327 }
328 res.x = x+4;
329 res.y = 40;
330 }
331
332 return res;
333 }
334
335 bool wxRadioBox::Show( bool show )
336 {
337 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") );
338
339 if (!wxControl::Show(show))
340 {
341 // nothing to do
342 return FALSE;
343 }
344
345 if ((m_windowStyle & wxNO_BORDER) != 0)
346 gtk_widget_hide( m_widget );
347
348 wxNode *node = m_boxes.First();
349 while (node)
350 {
351 GtkWidget *button = GTK_WIDGET( node->Data() );
352
353 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
354
355 node = node->Next();
356 }
357
358 return TRUE;
359 }
360
361 int wxRadioBox::FindString( const wxString &s ) const
362 {
363 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") );
364
365 int count = 0;
366
367 wxNode *node = m_boxes.First();
368 while (node)
369 {
370 GtkButton *button = GTK_BUTTON( node->Data() );
371
372 GtkLabel *label = GTK_LABEL( button->child );
373 if (s == label->label) return count;
374 count++;
375
376 node = node->Next();
377 }
378
379 return -1;
380 }
381
382 void wxRadioBox::SetFocus()
383 {
384 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
385
386 if (m_boxes.GetCount() == 0) return;
387
388 wxNode *node = m_boxes.First();
389 while (node)
390 {
391 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
392 if (button->active)
393 {
394 gtk_widget_grab_focus( GTK_WIDGET(button) );
395 return;
396 }
397 node = node->Next();
398 }
399
400 }
401
402 void wxRadioBox::SetSelection( int n )
403 {
404 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
405
406 wxNode *node = m_boxes.Nth( n );
407
408 wxCHECK_RET( node, wxT("radiobox wrong index") );
409
410 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
411
412 GtkDisableEvents();
413
414 gtk_toggle_button_set_state( button, 1 );
415
416 GtkEnableEvents();
417 }
418
419 int wxRadioBox::GetSelection(void) const
420 {
421 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") );
422
423 int count = 0;
424
425 wxNode *node = m_boxes.First();
426 while (node)
427 {
428 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
429 if (button->active) return count;
430 count++;
431 node = node->Next();
432 }
433
434 wxFAIL_MSG( wxT("wxRadioBox none selected") );
435
436 return -1;
437 }
438
439 wxString wxRadioBox::GetString( int n ) const
440 {
441 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
442
443 wxNode *node = m_boxes.Nth( n );
444
445 wxCHECK_MSG( node, wxT(""), wxT("radiobox wrong index") );
446
447 GtkButton *button = GTK_BUTTON( node->Data() );
448 GtkLabel *label = GTK_LABEL( button->child );
449
450 return wxString( label->label );
451 }
452
453 wxString wxRadioBox::GetLabel( int item ) const
454 {
455 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
456
457 return GetString( item );
458 }
459
460 void wxRadioBox::SetLabel( const wxString& label )
461 {
462 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
463
464 wxControl::SetLabel( label );
465
466 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
467 }
468
469 void wxRadioBox::SetLabel( int item, const wxString& label )
470 {
471 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
472
473 wxNode *node = m_boxes.Nth( item );
474
475 wxCHECK_RET( node, wxT("radiobox wrong index") );
476
477 GtkButton *button = GTK_BUTTON( node->Data() );
478 GtkLabel *g_label = GTK_LABEL( button->child );
479
480 gtk_label_set( g_label, label.mbc_str() );
481 }
482
483 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
484 {
485 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
486 }
487
488 bool wxRadioBox::Enable( bool enable )
489 {
490 if ( !wxControl::Enable( enable ) )
491 return FALSE;
492
493 wxNode *node = m_boxes.First();
494 while (node)
495 {
496 GtkButton *button = GTK_BUTTON( node->Data() );
497 GtkWidget *label = button->child;
498 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
499 gtk_widget_set_sensitive( label, enable );
500 node = node->Next();
501 }
502
503 return TRUE;
504 }
505
506 void wxRadioBox::Enable( int item, bool enable )
507 {
508 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
509
510 wxNode *node = m_boxes.Nth( item );
511
512 wxCHECK_RET( node, wxT("radiobox wrong index") );
513
514 GtkButton *button = GTK_BUTTON( node->Data() );
515 GtkWidget *label = button->child;
516 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
517 gtk_widget_set_sensitive( label, enable );
518 }
519
520 void wxRadioBox::Show( int item, bool show )
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 GtkWidget *button = GTK_WIDGET( node->Data() );
529
530 if (show)
531 gtk_widget_show( button );
532 else
533 gtk_widget_hide( button );
534 }
535
536 wxString wxRadioBox::GetStringSelection() const
537 {
538 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
539
540 wxNode *node = m_boxes.First();
541 while (node)
542 {
543 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
544 if (button->active)
545 {
546 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
547 return label->label;
548 }
549 node = node->Next();
550 }
551
552 wxFAIL_MSG( wxT("wxRadioBox none selected") );
553 return wxT("");
554 }
555
556 bool wxRadioBox::SetStringSelection( const wxString &s )
557 {
558 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") );
559
560 int res = FindString( s );
561 if (res == -1) return FALSE;
562 SetSelection( res );
563
564 return TRUE;
565 }
566
567 int wxRadioBox::Number() const
568 {
569 return m_boxes.Number();
570 }
571
572 int wxRadioBox::GetNumberOfRowsOrCols() const
573 {
574 return 1;
575 }
576
577 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
578 {
579 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
580 }
581
582 void wxRadioBox::GtkDisableEvents()
583 {
584 wxNode *node = m_boxes.First();
585 while (node)
586 {
587 gtk_signal_disconnect_by_func( GTK_OBJECT(node->Data()),
588 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
589
590 node = node->Next();
591 }
592 }
593
594 void wxRadioBox::GtkEnableEvents()
595 {
596 wxNode *node = m_boxes.First();
597 while (node)
598 {
599 gtk_signal_connect( GTK_OBJECT(node->Data()), "clicked",
600 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
601
602 node = node->Next();
603 }
604 }
605
606 void wxRadioBox::ApplyWidgetStyle()
607 {
608 SetWidgetStyle();
609
610 gtk_widget_set_style( m_widget, m_widgetStyle );
611
612 wxNode *node = m_boxes.First();
613 while (node)
614 {
615 GtkWidget *widget = GTK_WIDGET( node->Data() );
616 gtk_widget_set_style( widget, m_widgetStyle );
617
618 GtkButton *button = GTK_BUTTON( node->Data() );
619 gtk_widget_set_style( button->child, m_widgetStyle );
620
621 node = node->Next();
622 }
623 }
624
625 #if wxUSE_TOOLTIPS
626 void wxRadioBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
627 {
628 wxNode *node = m_boxes.First();
629 while (node)
630 {
631 GtkWidget *widget = GTK_WIDGET( node->Data() );
632 gtk_tooltips_set_tip( tips, widget, wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
633 node = node->Next();
634 }
635 }
636 #endif // wxUSE_TOOLTIPS
637
638 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
639 {
640 if (window == m_widget->window) return TRUE;
641
642 wxNode *node = m_boxes.First();
643 while (node)
644 {
645 GtkWidget *button = GTK_WIDGET( node->Data() );
646
647 if (window == button->window) return TRUE;
648
649 node = node->Next();
650 }
651
652 return FALSE;
653 }
654
655 #endif