]>
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 | ||
107 | int tmp = 22+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] ); | |
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 | ||
162 | PostCreation(); | |
163 | ||
d6d1892b RR |
164 | SetLabel( title ); |
165 | ||
f96aa4d9 | 166 | SetBackgroundColour( parent->GetBackgroundColour() ); |
58614078 | 167 | SetForegroundColour( parent->GetForegroundColour() ); |
f96aa4d9 | 168 | |
c801d85f KB |
169 | Show( TRUE ); |
170 | ||
171 | return TRUE; | |
6de97a3b | 172 | } |
c801d85f | 173 | |
d6d1892b RR |
174 | wxRadioBox::~wxRadioBox(void) |
175 | { | |
176 | wxNode *node = m_boxes.First(); | |
177 | while (node) | |
178 | { | |
179 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
180 | gtk_widget_destroy( button ); | |
181 | node = node->Next(); | |
182 | } | |
183 | } | |
184 | ||
b4071e91 | 185 | void wxRadioBox::OnSize( wxSizeEvent &event ) |
3f659fd6 | 186 | { |
b4071e91 RR |
187 | wxControl::OnSize( event ); |
188 | ||
3f659fd6 RR |
189 | int x = m_x+5; |
190 | int y = m_y+15; | |
191 | ||
e5403d7c | 192 | if (m_windowStyle & wxRA_VERTICAL) |
3f659fd6 | 193 | { |
e5403d7c RR |
194 | wxNode *node = m_boxes.First(); |
195 | while (node) | |
196 | { | |
197 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
3f659fd6 | 198 | |
e5403d7c RR |
199 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y ); |
200 | y += 20; | |
3f659fd6 | 201 | |
e5403d7c RR |
202 | int w = m_width-10; |
203 | if (w < 15) w = 15; | |
204 | gtk_widget_set_usize( button, w, 20 ); | |
e55ad60e | 205 | |
e5403d7c RR |
206 | node = node->Next(); |
207 | } | |
208 | } | |
209 | else | |
210 | { | |
211 | int max = 0; | |
212 | ||
213 | wxNode *node = m_boxes.First(); | |
214 | while (node) | |
215 | { | |
216 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
217 | GtkLabel *label = GTK_LABEL( button->child ); | |
218 | ||
219 | GdkFont *font = m_widget->style->font; | |
220 | int len = 27+gdk_string_measure( font, label->label ); | |
221 | if (len > max) max = len; | |
222 | ||
223 | node = node->Next(); | |
224 | } | |
225 | ||
226 | node = m_boxes.First(); | |
227 | while (node) | |
228 | { | |
229 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
230 | ||
231 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y ); | |
232 | x += max; | |
233 | gtk_widget_set_usize( button, max, 20 ); | |
234 | ||
235 | node = node->Next(); | |
236 | } | |
3f659fd6 RR |
237 | } |
238 | } | |
239 | ||
debe6624 | 240 | bool wxRadioBox::Show( bool show ) |
c801d85f | 241 | { |
f96aa4d9 RR |
242 | wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" ); |
243 | ||
c801d85f KB |
244 | wxWindow::Show( show ); |
245 | ||
d6d1892b RR |
246 | wxNode *node = m_boxes.First(); |
247 | while (node) | |
c801d85f | 248 | { |
d6d1892b RR |
249 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
250 | ||
251 | if (show) gtk_widget_show( button ); else gtk_widget_hide( button ); | |
252 | ||
253 | node = node->Next(); | |
6de97a3b | 254 | } |
c801d85f KB |
255 | |
256 | return TRUE; | |
6de97a3b | 257 | } |
c801d85f | 258 | |
47908e25 | 259 | int wxRadioBox::FindString( const wxString &s ) const |
c801d85f | 260 | { |
f96aa4d9 RR |
261 | wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" ); |
262 | ||
d6d1892b | 263 | int count = 0; |
47908e25 | 264 | |
d6d1892b RR |
265 | wxNode *node = m_boxes.First(); |
266 | while (node) | |
47908e25 | 267 | { |
d6d1892b RR |
268 | GtkButton *button = GTK_BUTTON( node->Data() ); |
269 | ||
270 | GtkLabel *label = GTK_LABEL( button->child ); | |
271 | if (s == label->label) return count; | |
272 | count++; | |
273 | ||
274 | node = node->Next(); | |
6de97a3b | 275 | } |
d6d1892b | 276 | |
47908e25 | 277 | return -1; |
6de97a3b | 278 | } |
c801d85f | 279 | |
47908e25 | 280 | void wxRadioBox::SetSelection( int n ) |
c801d85f | 281 | { |
f96aa4d9 RR |
282 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); |
283 | ||
d6d1892b | 284 | wxNode *node = m_boxes.Nth( n ); |
d3904ceb | 285 | |
d6d1892b | 286 | if (!node) |
d3904ceb RR |
287 | { |
288 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
289 | return; | |
290 | } | |
47908e25 | 291 | |
d6d1892b | 292 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
47908e25 RR |
293 | |
294 | gtk_toggle_button_set_state( button, 1 ); | |
6de97a3b | 295 | } |
c801d85f KB |
296 | |
297 | int wxRadioBox::GetSelection(void) const | |
298 | { | |
f96aa4d9 RR |
299 | wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" ); |
300 | ||
c801d85f | 301 | int count = 0; |
d6d1892b RR |
302 | |
303 | wxNode *node = m_boxes.First(); | |
304 | while (node) | |
c801d85f | 305 | { |
d6d1892b RR |
306 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
307 | if (button->active) return count; | |
c801d85f | 308 | count++; |
d6d1892b | 309 | node = node->Next(); |
6de97a3b | 310 | } |
2b5bd7fd | 311 | |
d6d1892b RR |
312 | wxFAIL_MSG( "wxRadioBox none selected" ); |
313 | ||
314 | return -1; | |
6de97a3b | 315 | } |
c801d85f | 316 | |
47908e25 | 317 | wxString wxRadioBox::GetString( int n ) const |
c801d85f | 318 | { |
f96aa4d9 RR |
319 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); |
320 | ||
d6d1892b | 321 | wxNode *node = m_boxes.Nth( n ); |
47908e25 | 322 | |
d6d1892b | 323 | if (!node) |
d3904ceb RR |
324 | { |
325 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
326 | return ""; | |
327 | } | |
47908e25 | 328 | |
d6d1892b | 329 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb | 330 | GtkLabel *label = GTK_LABEL( button->child ); |
47908e25 RR |
331 | |
332 | return wxString( label->label ); | |
6de97a3b | 333 | } |
c801d85f | 334 | |
d3904ceb | 335 | wxString wxRadioBox::GetLabel( int item ) const |
c801d85f | 336 | { |
f96aa4d9 RR |
337 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); |
338 | ||
d3904ceb | 339 | return GetString( item ); |
6de97a3b | 340 | } |
c801d85f | 341 | |
d3904ceb | 342 | void wxRadioBox::SetLabel( const wxString& label ) |
c801d85f | 343 | { |
f96aa4d9 RR |
344 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); |
345 | ||
d3904ceb | 346 | wxControl::SetLabel( label ); |
f96aa4d9 RR |
347 | |
348 | gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel() ); | |
6de97a3b | 349 | } |
c801d85f | 350 | |
d3904ceb | 351 | void wxRadioBox::SetLabel( int item, const wxString& label ) |
c801d85f | 352 | { |
f96aa4d9 RR |
353 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); |
354 | ||
d6d1892b | 355 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 356 | |
d6d1892b | 357 | if (!node) |
d3904ceb RR |
358 | { |
359 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
360 | return; | |
361 | } | |
362 | ||
d6d1892b | 363 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
364 | GtkLabel *g_label = GTK_LABEL( button->child ); |
365 | ||
366 | gtk_label_set( g_label, label ); | |
6de97a3b | 367 | } |
c801d85f | 368 | |
debe6624 | 369 | void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) ) |
c801d85f | 370 | { |
a3622daa | 371 | wxFAIL_MSG("wxRadioBox::SetLabel not implemented."); |
6de97a3b | 372 | } |
c801d85f | 373 | |
d3904ceb | 374 | void wxRadioBox::Enable( bool enable ) |
c801d85f | 375 | { |
d3904ceb RR |
376 | wxControl::Enable( enable ); |
377 | ||
d6d1892b RR |
378 | wxNode *node = m_boxes.First(); |
379 | while (node) | |
d3904ceb | 380 | { |
d6d1892b | 381 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
382 | GtkWidget *label = button->child; |
383 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
384 | gtk_widget_set_sensitive( label, enable ); | |
d6d1892b | 385 | node = node->Next(); |
d3904ceb | 386 | } |
6de97a3b | 387 | } |
c801d85f | 388 | |
d3904ceb | 389 | void wxRadioBox::Enable( int item, bool enable ) |
c801d85f | 390 | { |
d6d1892b | 391 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 392 | |
d6d1892b | 393 | if (!node) |
d3904ceb RR |
394 | { |
395 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
396 | return; | |
397 | } | |
398 | ||
d6d1892b | 399 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
400 | GtkWidget *label = button->child; |
401 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
402 | gtk_widget_set_sensitive( label, enable ); | |
6de97a3b | 403 | } |
c801d85f | 404 | |
d3904ceb | 405 | void wxRadioBox::Show( int item, bool show ) |
c801d85f | 406 | { |
d6d1892b | 407 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 408 | |
d6d1892b | 409 | if (!node) |
d3904ceb RR |
410 | { |
411 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
412 | return; | |
413 | } | |
414 | ||
d6d1892b | 415 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
c801d85f | 416 | |
d3904ceb RR |
417 | if (show) |
418 | gtk_widget_show( button ); | |
419 | else | |
420 | gtk_widget_hide( button ); | |
6de97a3b | 421 | } |
c801d85f KB |
422 | |
423 | wxString wxRadioBox::GetStringSelection(void) const | |
424 | { | |
d6d1892b RR |
425 | wxNode *node = m_boxes.First(); |
426 | while (node) | |
c801d85f | 427 | { |
d6d1892b RR |
428 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
429 | if (button->active) | |
c801d85f | 430 | { |
d6d1892b | 431 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); |
c801d85f | 432 | return label->label; |
6de97a3b | 433 | } |
d6d1892b | 434 | node = node->Next(); |
6de97a3b | 435 | } |
d6d1892b RR |
436 | |
437 | wxFAIL_MSG( "wxRadioBox none selected" ); | |
c801d85f | 438 | return ""; |
6de97a3b | 439 | } |
c801d85f | 440 | |
47908e25 | 441 | bool wxRadioBox::SetStringSelection( const wxString&s ) |
c801d85f | 442 | { |
47908e25 RR |
443 | int res = FindString( s ); |
444 | if (res == -1) return FALSE; | |
445 | SetSelection( res ); | |
c801d85f | 446 | return TRUE; |
6de97a3b | 447 | } |
c801d85f KB |
448 | |
449 | int wxRadioBox::Number(void) const | |
450 | { | |
d6d1892b | 451 | return m_boxes.Number(); |
6de97a3b | 452 | } |
c801d85f KB |
453 | |
454 | int wxRadioBox::GetNumberOfRowsOrCols(void) const | |
455 | { | |
456 | return 1; | |
6de97a3b | 457 | } |
c801d85f | 458 | |
debe6624 | 459 | void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) ) |
c801d85f | 460 | { |
a3622daa | 461 | wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented."); |
6de97a3b | 462 | } |
c801d85f | 463 | |
58614078 | 464 | void wxRadioBox::ApplyWidgetStyle() |
868a2826 | 465 | { |
58614078 | 466 | SetWidgetStyle(); |
f96aa4d9 | 467 | |
a81258be RR |
468 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
469 | ||
d6d1892b RR |
470 | wxNode *node = m_boxes.First(); |
471 | while (node) | |
868a2826 | 472 | { |
58614078 RR |
473 | GtkWidget *widget = GTK_WIDGET( node->Data() ); |
474 | gtk_widget_set_style( widget, m_widgetStyle ); | |
868a2826 | 475 | |
58614078 | 476 | GtkButton *button = GTK_BUTTON( node->Data() ); |
a81258be | 477 | gtk_widget_set_style( button->child, m_widgetStyle ); |
868a2826 | 478 | |
d6d1892b | 479 | node = node->Next(); |
868a2826 RR |
480 | } |
481 | } | |
b4071e91 RR |
482 | |
483 | bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window ) | |
484 | { | |
485 | if (window == m_widget->window) return TRUE; | |
486 | ||
d6d1892b RR |
487 | wxNode *node = m_boxes.First(); |
488 | while (node) | |
b4071e91 | 489 | { |
d6d1892b RR |
490 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
491 | ||
b4071e91 | 492 | if (window == button->window) return TRUE; |
d6d1892b RR |
493 | |
494 | node = node->Next(); | |
b4071e91 RR |
495 | } |
496 | ||
497 | return FALSE; | |
498 | } |