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