]>
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; | |
83 | ||
d6d1892b RR |
84 | GtkRadioButton *m_radio = (GtkRadioButton*) NULL; |
85 | ||
c801d85f KB |
86 | // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0)) |
87 | if (n > 0) | |
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 | ||
110 | int width = m_width-10; | |
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 RR |
117 | } |
118 | } | |
d6d1892b | 119 | |
c801d85f KB |
120 | wxSize newSize = size; |
121 | if (newSize.x == -1) newSize.x = maxLen+10; | |
122 | if (newSize.y == -1) newSize.y = height; | |
123 | SetSize( newSize.x, newSize.y ); | |
124 | ||
125 | PostCreation(); | |
126 | ||
d6d1892b RR |
127 | SetLabel( title ); |
128 | ||
f96aa4d9 RR |
129 | SetBackgroundColour( parent->GetBackgroundColour() ); |
130 | ||
c801d85f KB |
131 | Show( TRUE ); |
132 | ||
133 | return TRUE; | |
6de97a3b | 134 | } |
c801d85f | 135 | |
d6d1892b RR |
136 | wxRadioBox::~wxRadioBox(void) |
137 | { | |
138 | wxNode *node = m_boxes.First(); | |
139 | while (node) | |
140 | { | |
141 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
142 | gtk_widget_destroy( button ); | |
143 | node = node->Next(); | |
144 | } | |
145 | } | |
146 | ||
b4071e91 | 147 | void wxRadioBox::OnSize( wxSizeEvent &event ) |
3f659fd6 | 148 | { |
b4071e91 RR |
149 | wxControl::OnSize( event ); |
150 | ||
3f659fd6 RR |
151 | int x = m_x+5; |
152 | int y = m_y+15; | |
153 | ||
d6d1892b RR |
154 | wxNode *node = m_boxes.First(); |
155 | while (node) | |
3f659fd6 | 156 | { |
d6d1892b | 157 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
3f659fd6 RR |
158 | |
159 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y ); | |
3f659fd6 RR |
160 | y += 20; |
161 | ||
e55ad60e RR |
162 | int w = m_width-10; |
163 | if (w < 15) w = 15; | |
164 | gtk_widget_set_usize( button, w, 20 ); | |
165 | ||
d6d1892b | 166 | node = node->Next(); |
3f659fd6 RR |
167 | } |
168 | } | |
169 | ||
debe6624 | 170 | bool wxRadioBox::Show( bool show ) |
c801d85f | 171 | { |
f96aa4d9 RR |
172 | wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" ); |
173 | ||
c801d85f KB |
174 | wxWindow::Show( show ); |
175 | ||
d6d1892b RR |
176 | wxNode *node = m_boxes.First(); |
177 | while (node) | |
c801d85f | 178 | { |
d6d1892b RR |
179 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
180 | ||
181 | if (show) gtk_widget_show( button ); else gtk_widget_hide( button ); | |
182 | ||
183 | node = node->Next(); | |
6de97a3b | 184 | } |
c801d85f KB |
185 | |
186 | return TRUE; | |
6de97a3b | 187 | } |
c801d85f | 188 | |
47908e25 | 189 | int wxRadioBox::FindString( const wxString &s ) const |
c801d85f | 190 | { |
f96aa4d9 RR |
191 | wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" ); |
192 | ||
d6d1892b | 193 | int count = 0; |
47908e25 | 194 | |
d6d1892b RR |
195 | wxNode *node = m_boxes.First(); |
196 | while (node) | |
47908e25 | 197 | { |
d6d1892b RR |
198 | GtkButton *button = GTK_BUTTON( node->Data() ); |
199 | ||
200 | GtkLabel *label = GTK_LABEL( button->child ); | |
201 | if (s == label->label) return count; | |
202 | count++; | |
203 | ||
204 | node = node->Next(); | |
6de97a3b | 205 | } |
d6d1892b | 206 | |
47908e25 | 207 | return -1; |
6de97a3b | 208 | } |
c801d85f | 209 | |
47908e25 | 210 | void wxRadioBox::SetSelection( int n ) |
c801d85f | 211 | { |
f96aa4d9 RR |
212 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); |
213 | ||
d6d1892b | 214 | wxNode *node = m_boxes.Nth( n ); |
d3904ceb | 215 | |
d6d1892b | 216 | if (!node) |
d3904ceb RR |
217 | { |
218 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
219 | return; | |
220 | } | |
47908e25 | 221 | |
d6d1892b | 222 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
47908e25 RR |
223 | |
224 | gtk_toggle_button_set_state( button, 1 ); | |
6de97a3b | 225 | } |
c801d85f KB |
226 | |
227 | int wxRadioBox::GetSelection(void) const | |
228 | { | |
f96aa4d9 RR |
229 | wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" ); |
230 | ||
c801d85f | 231 | int count = 0; |
d6d1892b RR |
232 | |
233 | wxNode *node = m_boxes.First(); | |
234 | while (node) | |
c801d85f | 235 | { |
d6d1892b RR |
236 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
237 | if (button->active) return count; | |
c801d85f | 238 | count++; |
d6d1892b | 239 | node = node->Next(); |
6de97a3b | 240 | } |
2b5bd7fd | 241 | |
d6d1892b RR |
242 | wxFAIL_MSG( "wxRadioBox none selected" ); |
243 | ||
244 | return -1; | |
6de97a3b | 245 | } |
c801d85f | 246 | |
47908e25 | 247 | wxString wxRadioBox::GetString( int n ) const |
c801d85f | 248 | { |
f96aa4d9 RR |
249 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); |
250 | ||
d6d1892b | 251 | wxNode *node = m_boxes.Nth( n ); |
47908e25 | 252 | |
d6d1892b | 253 | if (!node) |
d3904ceb RR |
254 | { |
255 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
256 | return ""; | |
257 | } | |
47908e25 | 258 | |
d6d1892b | 259 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb | 260 | GtkLabel *label = GTK_LABEL( button->child ); |
47908e25 RR |
261 | |
262 | return wxString( label->label ); | |
6de97a3b | 263 | } |
c801d85f | 264 | |
d3904ceb | 265 | wxString wxRadioBox::GetLabel( int item ) const |
c801d85f | 266 | { |
f96aa4d9 RR |
267 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); |
268 | ||
d3904ceb | 269 | return GetString( item ); |
6de97a3b | 270 | } |
c801d85f | 271 | |
d3904ceb | 272 | void wxRadioBox::SetLabel( const wxString& label ) |
c801d85f | 273 | { |
f96aa4d9 RR |
274 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); |
275 | ||
d3904ceb | 276 | wxControl::SetLabel( label ); |
f96aa4d9 RR |
277 | |
278 | gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel() ); | |
6de97a3b | 279 | } |
c801d85f | 280 | |
d3904ceb | 281 | void wxRadioBox::SetLabel( int item, const wxString& label ) |
c801d85f | 282 | { |
f96aa4d9 RR |
283 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); |
284 | ||
d6d1892b | 285 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 286 | |
d6d1892b | 287 | if (!node) |
d3904ceb RR |
288 | { |
289 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
290 | return; | |
291 | } | |
292 | ||
d6d1892b | 293 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
294 | GtkLabel *g_label = GTK_LABEL( button->child ); |
295 | ||
296 | gtk_label_set( g_label, label ); | |
6de97a3b | 297 | } |
c801d85f | 298 | |
debe6624 | 299 | void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) ) |
c801d85f | 300 | { |
a3622daa | 301 | wxFAIL_MSG("wxRadioBox::SetLabel not implemented."); |
6de97a3b | 302 | } |
c801d85f | 303 | |
d3904ceb | 304 | void wxRadioBox::Enable( bool enable ) |
c801d85f | 305 | { |
d3904ceb RR |
306 | wxControl::Enable( enable ); |
307 | ||
d6d1892b RR |
308 | wxNode *node = m_boxes.First(); |
309 | while (node) | |
d3904ceb | 310 | { |
d6d1892b | 311 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
312 | GtkWidget *label = button->child; |
313 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
314 | gtk_widget_set_sensitive( label, enable ); | |
d6d1892b | 315 | node = node->Next(); |
d3904ceb | 316 | } |
6de97a3b | 317 | } |
c801d85f | 318 | |
d3904ceb | 319 | void wxRadioBox::Enable( int item, bool enable ) |
c801d85f | 320 | { |
d6d1892b | 321 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 322 | |
d6d1892b | 323 | if (!node) |
d3904ceb RR |
324 | { |
325 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
326 | return; | |
327 | } | |
328 | ||
d6d1892b | 329 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
330 | GtkWidget *label = button->child; |
331 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
332 | gtk_widget_set_sensitive( label, enable ); | |
6de97a3b | 333 | } |
c801d85f | 334 | |
d3904ceb | 335 | void wxRadioBox::Show( int item, bool show ) |
c801d85f | 336 | { |
d6d1892b | 337 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 338 | |
d6d1892b | 339 | if (!node) |
d3904ceb RR |
340 | { |
341 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
342 | return; | |
343 | } | |
344 | ||
d6d1892b | 345 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
c801d85f | 346 | |
d3904ceb RR |
347 | if (show) |
348 | gtk_widget_show( button ); | |
349 | else | |
350 | gtk_widget_hide( button ); | |
6de97a3b | 351 | } |
c801d85f KB |
352 | |
353 | wxString wxRadioBox::GetStringSelection(void) const | |
354 | { | |
d6d1892b RR |
355 | wxNode *node = m_boxes.First(); |
356 | while (node) | |
c801d85f | 357 | { |
d6d1892b RR |
358 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
359 | if (button->active) | |
c801d85f | 360 | { |
d6d1892b | 361 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); |
c801d85f | 362 | return label->label; |
6de97a3b | 363 | } |
d6d1892b | 364 | node = node->Next(); |
6de97a3b | 365 | } |
d6d1892b RR |
366 | |
367 | wxFAIL_MSG( "wxRadioBox none selected" ); | |
c801d85f | 368 | return ""; |
6de97a3b | 369 | } |
c801d85f | 370 | |
47908e25 | 371 | bool wxRadioBox::SetStringSelection( const wxString&s ) |
c801d85f | 372 | { |
47908e25 RR |
373 | int res = FindString( s ); |
374 | if (res == -1) return FALSE; | |
375 | SetSelection( res ); | |
c801d85f | 376 | return TRUE; |
6de97a3b | 377 | } |
c801d85f KB |
378 | |
379 | int wxRadioBox::Number(void) const | |
380 | { | |
d6d1892b | 381 | return m_boxes.Number(); |
6de97a3b | 382 | } |
c801d85f KB |
383 | |
384 | int wxRadioBox::GetNumberOfRowsOrCols(void) const | |
385 | { | |
386 | return 1; | |
6de97a3b | 387 | } |
c801d85f | 388 | |
debe6624 | 389 | void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) ) |
c801d85f | 390 | { |
a3622daa | 391 | wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented."); |
6de97a3b | 392 | } |
c801d85f | 393 | |
868a2826 RR |
394 | void wxRadioBox::SetFont( const wxFont &font ) |
395 | { | |
f96aa4d9 RR |
396 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); |
397 | ||
398 | wxControl::SetFont( font ); | |
868a2826 | 399 | |
d6d1892b RR |
400 | wxNode *node = m_boxes.First(); |
401 | while (node) | |
868a2826 | 402 | { |
d6d1892b | 403 | GtkButton *button = GTK_BUTTON( node->Data() ); |
868a2826 RR |
404 | |
405 | gtk_widget_set_style( button->child, | |
406 | gtk_style_ref( | |
407 | gtk_widget_get_style( m_widget ) ) ); | |
408 | ||
d6d1892b | 409 | node = node->Next(); |
868a2826 RR |
410 | } |
411 | } | |
b4071e91 | 412 | |
f96aa4d9 RR |
413 | void wxRadioBox::SetBackgroundColour( const wxColour &colour ) |
414 | { | |
415 | return; | |
416 | ||
417 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
418 | ||
419 | wxControl::SetBackgroundColour( colour ); | |
420 | ||
421 | if (!m_backgroundColour.Ok()) return; | |
422 | ||
423 | wxNode *node = m_boxes.First(); | |
424 | while (node) | |
425 | { | |
426 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
427 | ||
428 | gtk_widget_set_style( button, | |
429 | gtk_style_ref( | |
430 | gtk_widget_get_style( m_widget ) ) ); | |
431 | ||
432 | node = node->Next(); | |
433 | } | |
434 | } | |
435 | ||
b4071e91 RR |
436 | bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window ) |
437 | { | |
438 | if (window == m_widget->window) return TRUE; | |
439 | ||
d6d1892b RR |
440 | wxNode *node = m_boxes.First(); |
441 | while (node) | |
b4071e91 | 442 | { |
d6d1892b RR |
443 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
444 | ||
b4071e91 | 445 | if (window == button->window) return TRUE; |
d6d1892b RR |
446 | |
447 | node = node->Next(); | |
b4071e91 RR |
448 | } |
449 | ||
450 | return FALSE; | |
451 | } |