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