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