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