]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: radiobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
f96aa4d9 RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
c801d85f KB |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "radiobox.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/radiobox.h" | |
16 | #include "wx/dialog.h" | |
17 | #include "wx/frame.h" | |
18 | #include "wx/gtk/win_gtk.h" | |
19 | ||
66bd6b93 RR |
20 | //----------------------------------------------------------------------------- |
21 | // data | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | extern bool g_blockEventsOnDrag; | |
25 | ||
c801d85f | 26 | //----------------------------------------------------------------------------- |
b4071e91 | 27 | // "clicked" |
c801d85f KB |
28 | //----------------------------------------------------------------------------- |
29 | ||
66bd6b93 | 30 | static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb ) |
c801d85f | 31 | { |
66bd6b93 RR |
32 | if (!rb->HasVMT()) return; |
33 | if (g_blockEventsOnDrag) return; | |
34 | ||
47908e25 RR |
35 | if (rb->m_alreadySent) |
36 | { | |
37 | rb->m_alreadySent = FALSE; | |
38 | return; | |
39 | } | |
40 | ||
41 | rb->m_alreadySent = TRUE; | |
42 | ||
c801d85f KB |
43 | wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() ); |
44 | event.SetInt( rb->GetSelection() ); | |
45 | wxString tmp( rb->GetStringSelection() ); | |
46 | event.SetString( WXSTRINGCAST(tmp) ); | |
47 | event.SetEventObject( rb ); | |
47908e25 | 48 | rb->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 49 | } |
c801d85f | 50 | |
b4071e91 RR |
51 | //----------------------------------------------------------------------------- |
52 | // wxRadioBox | |
c801d85f KB |
53 | //----------------------------------------------------------------------------- |
54 | ||
55 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl) | |
56 | ||
3f659fd6 RR |
57 | BEGIN_EVENT_TABLE(wxRadioBox, wxControl) |
58 | EVT_SIZE(wxRadioBox::OnSize) | |
59 | END_EVENT_TABLE() | |
60 | ||
c801d85f KB |
61 | wxRadioBox::wxRadioBox(void) |
62 | { | |
6de97a3b | 63 | } |
c801d85f | 64 | |
debe6624 JS |
65 | bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title, |
66 | const wxPoint &pos, const wxSize &size, | |
6de97a3b RR |
67 | int n, const wxString choices[], int WXUNUSED(majorDim), |
68 | long style, const wxValidator& validator, const wxString &name ) | |
c801d85f | 69 | { |
47908e25 | 70 | m_alreadySent = FALSE; |
c801d85f KB |
71 | m_needParent = TRUE; |
72 | ||
73 | PreCreation( parent, id, pos, size, style, name ); | |
74 | ||
6de97a3b RR |
75 | SetValidator( validator ); |
76 | ||
c801d85f KB |
77 | m_widget = gtk_frame_new( title ); |
78 | ||
79 | int x = m_x+5; | |
80 | int y = m_y+15; | |
81 | int maxLen = 0; | |
82 | int height = 20; | |
e5403d7c | 83 | int width = 0; |
c801d85f | 84 | |
d6d1892b RR |
85 | GtkRadioButton *m_radio = (GtkRadioButton*) NULL; |
86 | ||
e5403d7c | 87 | if (m_windowStyle & wxRA_VERTICAL) |
c801d85f | 88 | { |
c67daf87 | 89 | GSList *radio_button_group = (GSList *) NULL; |
c801d85f KB |
90 | for (int i = 0; i < n; i++) |
91 | { | |
92 | if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) ); | |
47908e25 | 93 | |
c801d85f KB |
94 | m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) ); |
95 | ||
d6d1892b RR |
96 | m_boxes.Append( (wxObject*) m_radio ); |
97 | ||
b4071e91 RR |
98 | ConnectWidget( GTK_WIDGET(m_radio) ); |
99 | ||
c801d85f KB |
100 | if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE ); |
101 | ||
102 | gtk_signal_connect( GTK_OBJECT(m_radio), "clicked", | |
103 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); | |
104 | ||
105 | gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y ); | |
106 | ||
7bce6aec | 107 | int tmp = 25+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] ); |
c801d85f KB |
108 | if (tmp > maxLen) maxLen = tmp; |
109 | ||
e5403d7c | 110 | width = m_width-10; |
c801d85f KB |
111 | if (size.x == -1) width = tmp; |
112 | gtk_widget_set_usize( GTK_WIDGET(m_radio), width, 20 ); | |
113 | ||
114 | y += 20; | |
115 | height += 20; | |
116 | ||
6de97a3b | 117 | } |
e5403d7c RR |
118 | width = maxLen + 10; |
119 | } | |
120 | else | |
121 | { | |
122 | int max = 0; | |
123 | for (int i = 0; i < n; i++) | |
124 | { | |
125 | GdkFont *font = m_widget->style->font; | |
126 | int len = 27+gdk_string_measure( font, choices[i] ); | |
127 | if (len > max) max = len; | |
128 | } | |
129 | ||
130 | GSList *radio_button_group = (GSList *) NULL; | |
131 | for (int i = 0; i < n; i++) | |
132 | { | |
133 | if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) ); | |
134 | ||
135 | m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) ); | |
136 | ||
137 | m_boxes.Append( (wxObject*) m_radio ); | |
138 | ||
139 | ConnectWidget( GTK_WIDGET(m_radio) ); | |
140 | ||
141 | if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE ); | |
142 | ||
143 | gtk_signal_connect( GTK_OBJECT(m_radio), "clicked", | |
144 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); | |
145 | ||
146 | gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y ); | |
147 | ||
148 | gtk_widget_set_usize( GTK_WIDGET(m_radio), max, 20 ); | |
149 | ||
150 | x += max; | |
151 | } | |
152 | ||
153 | width = max*n + 10; | |
154 | height = 40; | |
6de97a3b | 155 | } |
d6d1892b | 156 | |
c801d85f | 157 | wxSize newSize = size; |
e5403d7c | 158 | if (newSize.x == -1) newSize.x = width; |
c801d85f KB |
159 | if (newSize.y == -1) newSize.y = height; |
160 | SetSize( newSize.x, newSize.y ); | |
161 | ||
e6b5bded RR |
162 | m_parent->AddChild( this ); |
163 | ||
164 | (m_parent->m_insertCallback)( m_parent, this ); | |
6ca41e57 | 165 | |
c801d85f KB |
166 | PostCreation(); |
167 | ||
d6d1892b RR |
168 | SetLabel( title ); |
169 | ||
f96aa4d9 | 170 | SetBackgroundColour( parent->GetBackgroundColour() ); |
58614078 | 171 | SetForegroundColour( parent->GetForegroundColour() ); |
f96aa4d9 | 172 | |
c801d85f KB |
173 | Show( TRUE ); |
174 | ||
175 | return TRUE; | |
6de97a3b | 176 | } |
c801d85f | 177 | |
d6d1892b RR |
178 | wxRadioBox::~wxRadioBox(void) |
179 | { | |
180 | wxNode *node = m_boxes.First(); | |
181 | while (node) | |
182 | { | |
183 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
184 | gtk_widget_destroy( button ); | |
185 | node = node->Next(); | |
186 | } | |
187 | } | |
188 | ||
b4071e91 | 189 | void wxRadioBox::OnSize( wxSizeEvent &event ) |
3f659fd6 | 190 | { |
b4071e91 RR |
191 | wxControl::OnSize( event ); |
192 | ||
3f659fd6 RR |
193 | int x = m_x+5; |
194 | int y = m_y+15; | |
195 | ||
e5403d7c | 196 | if (m_windowStyle & wxRA_VERTICAL) |
3f659fd6 | 197 | { |
e5403d7c RR |
198 | wxNode *node = m_boxes.First(); |
199 | while (node) | |
200 | { | |
201 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
3f659fd6 | 202 | |
e5403d7c RR |
203 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y ); |
204 | y += 20; | |
3f659fd6 | 205 | |
e5403d7c RR |
206 | int w = m_width-10; |
207 | if (w < 15) w = 15; | |
208 | gtk_widget_set_usize( button, w, 20 ); | |
e55ad60e | 209 | |
e5403d7c RR |
210 | node = node->Next(); |
211 | } | |
212 | } | |
213 | else | |
214 | { | |
215 | int max = 0; | |
216 | ||
217 | wxNode *node = m_boxes.First(); | |
218 | while (node) | |
219 | { | |
220 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
221 | GtkLabel *label = GTK_LABEL( button->child ); | |
222 | ||
223 | GdkFont *font = m_widget->style->font; | |
224 | int len = 27+gdk_string_measure( font, label->label ); | |
225 | if (len > max) max = len; | |
226 | ||
227 | node = node->Next(); | |
228 | } | |
229 | ||
230 | node = m_boxes.First(); | |
231 | while (node) | |
232 | { | |
233 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
234 | ||
235 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y ); | |
236 | x += max; | |
237 | gtk_widget_set_usize( button, max, 20 ); | |
238 | ||
239 | node = node->Next(); | |
240 | } | |
3f659fd6 RR |
241 | } |
242 | } | |
243 | ||
debe6624 | 244 | bool wxRadioBox::Show( bool show ) |
c801d85f | 245 | { |
f96aa4d9 RR |
246 | wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" ); |
247 | ||
c801d85f KB |
248 | wxWindow::Show( show ); |
249 | ||
d6d1892b RR |
250 | wxNode *node = m_boxes.First(); |
251 | while (node) | |
c801d85f | 252 | { |
d6d1892b RR |
253 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
254 | ||
255 | if (show) gtk_widget_show( button ); else gtk_widget_hide( button ); | |
256 | ||
257 | node = node->Next(); | |
6de97a3b | 258 | } |
c801d85f KB |
259 | |
260 | return TRUE; | |
6de97a3b | 261 | } |
c801d85f | 262 | |
47908e25 | 263 | int wxRadioBox::FindString( const wxString &s ) const |
c801d85f | 264 | { |
f96aa4d9 RR |
265 | wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" ); |
266 | ||
d6d1892b | 267 | int count = 0; |
47908e25 | 268 | |
d6d1892b RR |
269 | wxNode *node = m_boxes.First(); |
270 | while (node) | |
47908e25 | 271 | { |
d6d1892b RR |
272 | GtkButton *button = GTK_BUTTON( node->Data() ); |
273 | ||
274 | GtkLabel *label = GTK_LABEL( button->child ); | |
275 | if (s == label->label) return count; | |
276 | count++; | |
277 | ||
278 | node = node->Next(); | |
6de97a3b | 279 | } |
d6d1892b | 280 | |
47908e25 | 281 | return -1; |
6de97a3b | 282 | } |
c801d85f | 283 | |
47908e25 | 284 | void wxRadioBox::SetSelection( int n ) |
c801d85f | 285 | { |
f96aa4d9 RR |
286 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); |
287 | ||
d6d1892b | 288 | wxNode *node = m_boxes.Nth( n ); |
d3904ceb | 289 | |
d6d1892b | 290 | if (!node) |
d3904ceb RR |
291 | { |
292 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
293 | return; | |
294 | } | |
47908e25 | 295 | |
d6d1892b | 296 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
47908e25 RR |
297 | |
298 | gtk_toggle_button_set_state( button, 1 ); | |
6de97a3b | 299 | } |
c801d85f KB |
300 | |
301 | int wxRadioBox::GetSelection(void) const | |
302 | { | |
f96aa4d9 RR |
303 | wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" ); |
304 | ||
c801d85f | 305 | int count = 0; |
d6d1892b RR |
306 | |
307 | wxNode *node = m_boxes.First(); | |
308 | while (node) | |
c801d85f | 309 | { |
d6d1892b RR |
310 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
311 | if (button->active) return count; | |
c801d85f | 312 | count++; |
d6d1892b | 313 | node = node->Next(); |
6de97a3b | 314 | } |
2b5bd7fd | 315 | |
d6d1892b RR |
316 | wxFAIL_MSG( "wxRadioBox none selected" ); |
317 | ||
318 | return -1; | |
6de97a3b | 319 | } |
c801d85f | 320 | |
47908e25 | 321 | wxString wxRadioBox::GetString( int n ) const |
c801d85f | 322 | { |
f96aa4d9 RR |
323 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); |
324 | ||
d6d1892b | 325 | wxNode *node = m_boxes.Nth( n ); |
47908e25 | 326 | |
d6d1892b | 327 | if (!node) |
d3904ceb RR |
328 | { |
329 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
330 | return ""; | |
331 | } | |
47908e25 | 332 | |
d6d1892b | 333 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb | 334 | GtkLabel *label = GTK_LABEL( button->child ); |
47908e25 RR |
335 | |
336 | return wxString( label->label ); | |
6de97a3b | 337 | } |
c801d85f | 338 | |
d3904ceb | 339 | wxString wxRadioBox::GetLabel( int item ) const |
c801d85f | 340 | { |
f96aa4d9 RR |
341 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); |
342 | ||
d3904ceb | 343 | return GetString( item ); |
6de97a3b | 344 | } |
c801d85f | 345 | |
d3904ceb | 346 | void wxRadioBox::SetLabel( const wxString& label ) |
c801d85f | 347 | { |
f96aa4d9 RR |
348 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); |
349 | ||
d3904ceb | 350 | wxControl::SetLabel( label ); |
f96aa4d9 RR |
351 | |
352 | gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel() ); | |
6de97a3b | 353 | } |
c801d85f | 354 | |
d3904ceb | 355 | void wxRadioBox::SetLabel( int item, const wxString& label ) |
c801d85f | 356 | { |
f96aa4d9 RR |
357 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); |
358 | ||
d6d1892b | 359 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 360 | |
d6d1892b | 361 | if (!node) |
d3904ceb RR |
362 | { |
363 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
364 | return; | |
365 | } | |
366 | ||
d6d1892b | 367 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
368 | GtkLabel *g_label = GTK_LABEL( button->child ); |
369 | ||
370 | gtk_label_set( g_label, label ); | |
6de97a3b | 371 | } |
c801d85f | 372 | |
debe6624 | 373 | void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) ) |
c801d85f | 374 | { |
a3622daa | 375 | wxFAIL_MSG("wxRadioBox::SetLabel not implemented."); |
6de97a3b | 376 | } |
c801d85f | 377 | |
d3904ceb | 378 | void wxRadioBox::Enable( bool enable ) |
c801d85f | 379 | { |
d3904ceb RR |
380 | wxControl::Enable( enable ); |
381 | ||
d6d1892b RR |
382 | wxNode *node = m_boxes.First(); |
383 | while (node) | |
d3904ceb | 384 | { |
d6d1892b | 385 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
386 | GtkWidget *label = button->child; |
387 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
388 | gtk_widget_set_sensitive( label, enable ); | |
d6d1892b | 389 | node = node->Next(); |
d3904ceb | 390 | } |
6de97a3b | 391 | } |
c801d85f | 392 | |
d3904ceb | 393 | void wxRadioBox::Enable( int item, bool enable ) |
c801d85f | 394 | { |
d6d1892b | 395 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 396 | |
d6d1892b | 397 | if (!node) |
d3904ceb RR |
398 | { |
399 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
400 | return; | |
401 | } | |
402 | ||
d6d1892b | 403 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
404 | GtkWidget *label = button->child; |
405 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
406 | gtk_widget_set_sensitive( label, enable ); | |
6de97a3b | 407 | } |
c801d85f | 408 | |
d3904ceb | 409 | void wxRadioBox::Show( int item, bool show ) |
c801d85f | 410 | { |
d6d1892b | 411 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 412 | |
d6d1892b | 413 | if (!node) |
d3904ceb RR |
414 | { |
415 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
416 | return; | |
417 | } | |
418 | ||
d6d1892b | 419 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
c801d85f | 420 | |
d3904ceb RR |
421 | if (show) |
422 | gtk_widget_show( button ); | |
423 | else | |
424 | gtk_widget_hide( button ); | |
6de97a3b | 425 | } |
c801d85f KB |
426 | |
427 | wxString wxRadioBox::GetStringSelection(void) const | |
428 | { | |
d6d1892b RR |
429 | wxNode *node = m_boxes.First(); |
430 | while (node) | |
c801d85f | 431 | { |
d6d1892b RR |
432 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
433 | if (button->active) | |
c801d85f | 434 | { |
d6d1892b | 435 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); |
c801d85f | 436 | return label->label; |
6de97a3b | 437 | } |
d6d1892b | 438 | node = node->Next(); |
6de97a3b | 439 | } |
d6d1892b RR |
440 | |
441 | wxFAIL_MSG( "wxRadioBox none selected" ); | |
c801d85f | 442 | return ""; |
6de97a3b | 443 | } |
c801d85f | 444 | |
47908e25 | 445 | bool wxRadioBox::SetStringSelection( const wxString&s ) |
c801d85f | 446 | { |
47908e25 RR |
447 | int res = FindString( s ); |
448 | if (res == -1) return FALSE; | |
449 | SetSelection( res ); | |
c801d85f | 450 | return TRUE; |
6de97a3b | 451 | } |
c801d85f KB |
452 | |
453 | int wxRadioBox::Number(void) const | |
454 | { | |
d6d1892b | 455 | return m_boxes.Number(); |
6de97a3b | 456 | } |
c801d85f KB |
457 | |
458 | int wxRadioBox::GetNumberOfRowsOrCols(void) const | |
459 | { | |
460 | return 1; | |
6de97a3b | 461 | } |
c801d85f | 462 | |
debe6624 | 463 | void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) ) |
c801d85f | 464 | { |
a3622daa | 465 | wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented."); |
6de97a3b | 466 | } |
c801d85f | 467 | |
58614078 | 468 | void wxRadioBox::ApplyWidgetStyle() |
868a2826 | 469 | { |
58614078 | 470 | SetWidgetStyle(); |
f96aa4d9 | 471 | |
a81258be RR |
472 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
473 | ||
d6d1892b RR |
474 | wxNode *node = m_boxes.First(); |
475 | while (node) | |
868a2826 | 476 | { |
58614078 RR |
477 | GtkWidget *widget = GTK_WIDGET( node->Data() ); |
478 | gtk_widget_set_style( widget, m_widgetStyle ); | |
868a2826 | 479 | |
58614078 | 480 | GtkButton *button = GTK_BUTTON( node->Data() ); |
a81258be | 481 | gtk_widget_set_style( button->child, m_widgetStyle ); |
868a2826 | 482 | |
d6d1892b | 483 | node = node->Next(); |
868a2826 RR |
484 | } |
485 | } | |
b4071e91 RR |
486 | |
487 | bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window ) | |
488 | { | |
489 | if (window == m_widget->window) return TRUE; | |
490 | ||
d6d1892b RR |
491 | wxNode *node = m_boxes.First(); |
492 | while (node) | |
b4071e91 | 493 | { |
d6d1892b RR |
494 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
495 | ||
b4071e91 | 496 | if (window == button->window) return TRUE; |
d6d1892b RR |
497 | |
498 | node = node->Next(); | |
b4071e91 RR |
499 | } |
500 | ||
501 | return FALSE; | |
502 | } |