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