]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobox.cpp
no more _ftime instead of ftime
[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
269 GtkRequisition req;
270 req.width = 2;
271 req.height = 2;
272 (* GTK_WIDGET_CLASS( GTK_OBJECT(button)->klass )->size_request )
273 (button, &req );
274
275 if (req.width > max_len) max_len = req.width;
276
277 gtk_pizza_move( GTK_PIZZA(m_parent->m_wxwindow), button, m_x+x, m_y+y );
278 y += req.height;
279
280 node = node->Next();
281 if (!node) break;
282 }
283
284 // we don't know the max_len before
285
286 node = m_boxes.Nth( j*num_of_rows );
287 for (int i2 = 0; i2< num_of_rows; i2++)
288 {
289 GtkWidget *button = GTK_WIDGET( node->Data() );
290
291 gtk_pizza_resize( GTK_PIZZA(m_parent->m_wxwindow), button, max_len, 20 );
292
293 node = node->Next();
294 if (!node) break;
295 }
296
297 if (y > res.y) res.y = y;
298
299 x += max_len + 2;
300 }
301
302 res.x = x+4;
303 res.y += 4;
304 }
305 else
306 {
307 int max = 0;
308
309 wxNode *node = m_boxes.First();
310 while (node)
311 {
312 GtkWidget *button = GTK_WIDGET( node->Data() );
313
314 GtkRequisition req;
315 req.width = 2;
316 req.height = 2;
317 (* GTK_WIDGET_CLASS( GTK_OBJECT(button)->klass )->size_request )
318 (button, &req );
319
320 if (req.width > max) max = req.width;
321
322 node = node->Next();
323 }
324
325 node = m_boxes.First();
326 while (node)
327 {
328 GtkWidget *button = GTK_WIDGET( node->Data() );
329
330 gtk_pizza_set_size( GTK_PIZZA(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
331 x += max;
332
333 node = node->Next();
334 }
335 res.x = x+4;
336 res.y = 40;
337 }
338
339 return res;
340 }
341
342 bool wxRadioBox::Show( bool show )
343 {
344 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") );
345
346 if (!wxControl::Show(show))
347 {
348 // nothing to do
349 return FALSE;
350 }
351
352 if ((m_windowStyle & wxNO_BORDER) != 0)
353 gtk_widget_hide( m_widget );
354
355 wxNode *node = m_boxes.First();
356 while (node)
357 {
358 GtkWidget *button = GTK_WIDGET( node->Data() );
359
360 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
361
362 node = node->Next();
363 }
364
365 return TRUE;
366 }
367
368 int wxRadioBox::FindString( const wxString &s ) const
369 {
370 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") );
371
372 int count = 0;
373
374 wxNode *node = m_boxes.First();
375 while (node)
376 {
377 GtkButton *button = GTK_BUTTON( node->Data() );
378
379 GtkLabel *label = GTK_LABEL( button->child );
380 if (s == label->label) return count;
381 count++;
382
383 node = node->Next();
384 }
385
386 return -1;
387 }
388
389 void wxRadioBox::SetFocus()
390 {
391 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
392
393 if (m_boxes.GetCount() == 0) return;
394
395 wxNode *node = m_boxes.First();
396 while (node)
397 {
398 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
399 if (button->active)
400 {
401 gtk_widget_grab_focus( GTK_WIDGET(button) );
402 return;
403 }
404 node = node->Next();
405 }
406
407 }
408
409 void wxRadioBox::SetSelection( int n )
410 {
411 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
412
413 wxNode *node = m_boxes.Nth( n );
414
415 wxCHECK_RET( node, wxT("radiobox wrong index") );
416
417 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
418
419 GtkDisableEvents();
420
421 gtk_toggle_button_set_state( button, 1 );
422
423 GtkEnableEvents();
424 }
425
426 int wxRadioBox::GetSelection(void) const
427 {
428 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") );
429
430 int count = 0;
431
432 wxNode *node = m_boxes.First();
433 while (node)
434 {
435 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
436 if (button->active) return count;
437 count++;
438 node = node->Next();
439 }
440
441 wxFAIL_MSG( wxT("wxRadioBox none selected") );
442
443 return -1;
444 }
445
446 wxString wxRadioBox::GetString( int n ) const
447 {
448 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
449
450 wxNode *node = m_boxes.Nth( n );
451
452 wxCHECK_MSG( node, wxT(""), wxT("radiobox wrong index") );
453
454 GtkButton *button = GTK_BUTTON( node->Data() );
455 GtkLabel *label = GTK_LABEL( button->child );
456
457 return wxString( label->label );
458 }
459
460 wxString wxRadioBox::GetLabel( int item ) const
461 {
462 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
463
464 return GetString( item );
465 }
466
467 void wxRadioBox::SetLabel( const wxString& label )
468 {
469 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
470
471 wxControl::SetLabel( label );
472
473 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
474 }
475
476 void wxRadioBox::SetLabel( int item, const wxString& label )
477 {
478 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
479
480 wxNode *node = m_boxes.Nth( item );
481
482 wxCHECK_RET( node, wxT("radiobox wrong index") );
483
484 GtkButton *button = GTK_BUTTON( node->Data() );
485 GtkLabel *g_label = GTK_LABEL( button->child );
486
487 gtk_label_set( g_label, label.mbc_str() );
488 }
489
490 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
491 {
492 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
493 }
494
495 bool wxRadioBox::Enable( bool enable )
496 {
497 if ( !wxControl::Enable( enable ) )
498 return FALSE;
499
500 wxNode *node = m_boxes.First();
501 while (node)
502 {
503 GtkButton *button = GTK_BUTTON( node->Data() );
504 GtkWidget *label = button->child;
505 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
506 gtk_widget_set_sensitive( label, enable );
507 node = node->Next();
508 }
509
510 return TRUE;
511 }
512
513 void wxRadioBox::Enable( int item, bool enable )
514 {
515 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
516
517 wxNode *node = m_boxes.Nth( item );
518
519 wxCHECK_RET( node, wxT("radiobox wrong index") );
520
521 GtkButton *button = GTK_BUTTON( node->Data() );
522 GtkWidget *label = button->child;
523 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
524 gtk_widget_set_sensitive( label, enable );
525 }
526
527 void wxRadioBox::Show( int item, bool show )
528 {
529 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
530
531 wxNode *node = m_boxes.Nth( item );
532
533 wxCHECK_RET( node, wxT("radiobox wrong index") );
534
535 GtkWidget *button = GTK_WIDGET( node->Data() );
536
537 if (show)
538 gtk_widget_show( button );
539 else
540 gtk_widget_hide( button );
541 }
542
543 wxString wxRadioBox::GetStringSelection() const
544 {
545 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
546
547 wxNode *node = m_boxes.First();
548 while (node)
549 {
550 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
551 if (button->active)
552 {
553 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
554 return label->label;
555 }
556 node = node->Next();
557 }
558
559 wxFAIL_MSG( wxT("wxRadioBox none selected") );
560 return wxT("");
561 }
562
563 bool wxRadioBox::SetStringSelection( const wxString &s )
564 {
565 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") );
566
567 int res = FindString( s );
568 if (res == -1) return FALSE;
569 SetSelection( res );
570
571 return TRUE;
572 }
573
574 int wxRadioBox::Number() const
575 {
576 return m_boxes.Number();
577 }
578
579 int wxRadioBox::GetNumberOfRowsOrCols() const
580 {
581 return 1;
582 }
583
584 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
585 {
586 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
587 }
588
589 void wxRadioBox::GtkDisableEvents()
590 {
591 wxNode *node = m_boxes.First();
592 while (node)
593 {
594 gtk_signal_disconnect_by_func( GTK_OBJECT(node->Data()),
595 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
596
597 node = node->Next();
598 }
599 }
600
601 void wxRadioBox::GtkEnableEvents()
602 {
603 wxNode *node = m_boxes.First();
604 while (node)
605 {
606 gtk_signal_connect( GTK_OBJECT(node->Data()), "clicked",
607 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
608
609 node = node->Next();
610 }
611 }
612
613 void wxRadioBox::ApplyWidgetStyle()
614 {
615 SetWidgetStyle();
616
617 gtk_widget_set_style( m_widget, m_widgetStyle );
618
619 wxNode *node = m_boxes.First();
620 while (node)
621 {
622 GtkWidget *widget = GTK_WIDGET( node->Data() );
623 gtk_widget_set_style( widget, m_widgetStyle );
624
625 GtkButton *button = GTK_BUTTON( node->Data() );
626 gtk_widget_set_style( button->child, m_widgetStyle );
627
628 node = node->Next();
629 }
630 }
631
632 #if wxUSE_TOOLTIPS
633 void wxRadioBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
634 {
635 wxNode *node = m_boxes.First();
636 while (node)
637 {
638 GtkWidget *widget = GTK_WIDGET( node->Data() );
639 gtk_tooltips_set_tip( tips, widget, wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
640 node = node->Next();
641 }
642 }
643 #endif // wxUSE_TOOLTIPS
644
645 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
646 {
647 if (window == m_widget->window) return TRUE;
648
649 wxNode *node = m_boxes.First();
650 while (node)
651 {
652 GtkWidget *button = GTK_WIDGET( node->Data() );
653
654 if (window == button->window) return TRUE;
655
656 node = node->Next();
657 }
658
659 return FALSE;
660 }
661
662 #endif