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