]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: radiobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "radiobox.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/radiobox.h" | |
15 | #include "wx/dialog.h" | |
16 | #include "wx/frame.h" | |
17 | ||
18 | #include "gdk/gdk.h" | |
19 | #include "gtk/gtk.h" | |
20 | #include "wx/gtk/win_gtk.h" | |
21 | ||
22 | //----------------------------------------------------------------------------- | |
23 | // idle system | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | extern void wxapp_install_idle_handler(); | |
27 | extern bool g_isIdle; | |
28 | ||
29 | //----------------------------------------------------------------------------- | |
30 | // data | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | extern bool g_blockEventsOnDrag; | |
34 | ||
35 | //----------------------------------------------------------------------------- | |
36 | // "clicked" | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
39 | static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb ) | |
40 | { | |
41 | if (g_isIdle) wxapp_install_idle_handler(); | |
42 | ||
43 | if (!rb->HasVMT()) return; | |
44 | if (g_blockEventsOnDrag) return; | |
45 | ||
46 | if (rb->m_alreadySent) | |
47 | { | |
48 | rb->m_alreadySent = FALSE; | |
49 | return; | |
50 | } | |
51 | ||
52 | rb->m_alreadySent = TRUE; | |
53 | ||
54 | wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() ); | |
55 | event.SetInt( rb->GetSelection() ); | |
56 | event.SetString( rb->GetStringSelection() ); | |
57 | event.SetEventObject( rb ); | |
58 | rb->GetEventHandler()->ProcessEvent(event); | |
59 | } | |
60 | ||
61 | //----------------------------------------------------------------------------- | |
62 | // wxRadioBox | |
63 | //----------------------------------------------------------------------------- | |
64 | ||
65 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl) | |
66 | ||
67 | BEGIN_EVENT_TABLE(wxRadioBox, wxControl) | |
68 | EVT_SIZE(wxRadioBox::OnSize) | |
69 | END_EVENT_TABLE() | |
70 | ||
71 | wxRadioBox::wxRadioBox(void) | |
72 | { | |
73 | } | |
74 | ||
75 | bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title, | |
76 | const wxPoint &pos, const wxSize &size, | |
77 | int n, const wxString choices[], int majorDim, | |
78 | long style, const wxValidator& validator, | |
79 | const wxString &name ) | |
80 | { | |
81 | m_alreadySent = FALSE; | |
82 | m_needParent = TRUE; | |
83 | m_acceptsFocus = TRUE; | |
84 | ||
85 | PreCreation( parent, id, pos, size, style, name ); | |
86 | ||
87 | SetValidator( validator ); | |
88 | ||
89 | m_widget = gtk_frame_new( title.mbc_str() ); | |
90 | ||
91 | m_majorDim = majorDim; | |
92 | ||
93 | GtkRadioButton *m_radio = (GtkRadioButton*) NULL; | |
94 | ||
95 | GSList *radio_button_group = (GSList *) NULL; | |
96 | for (int i = 0; i < n; i++) | |
97 | { | |
98 | if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) ); | |
99 | ||
100 | m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i].mbc_str() ) ); | |
101 | ||
102 | m_boxes.Append( (wxObject*) m_radio ); | |
103 | ||
104 | ConnectWidget( GTK_WIDGET(m_radio) ); | |
105 | ||
106 | if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE ); | |
107 | ||
108 | gtk_signal_connect( GTK_OBJECT(m_radio), "clicked", | |
109 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); | |
110 | ||
111 | gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), m_x+10, m_y+10+(i*24) ); | |
112 | ||
113 | } | |
114 | ||
115 | wxSize ls = LayoutItems(); | |
116 | ||
117 | wxSize newSize = size; | |
118 | if (newSize.x == -1) newSize.x = ls.x; | |
119 | if (newSize.y == -1) newSize.y = ls.y; | |
120 | SetSize( newSize.x, newSize.y ); | |
121 | ||
122 | m_parent->AddChild( this ); | |
123 | ||
124 | (m_parent->m_insertCallback)( m_parent, this ); | |
125 | ||
126 | PostCreation(); | |
127 | ||
128 | SetLabel( title ); | |
129 | ||
130 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
131 | SetForegroundColour( parent->GetForegroundColour() ); | |
132 | SetFont( parent->GetFont() ); | |
133 | ||
134 | Show( TRUE ); | |
135 | ||
136 | return TRUE; | |
137 | } | |
138 | ||
139 | wxRadioBox::~wxRadioBox(void) | |
140 | { | |
141 | wxNode *node = m_boxes.First(); | |
142 | while (node) | |
143 | { | |
144 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
145 | gtk_widget_destroy( button ); | |
146 | node = node->Next(); | |
147 | } | |
148 | } | |
149 | ||
150 | void wxRadioBox::OnSize( wxSizeEvent &event ) | |
151 | { | |
152 | wxControl::OnSize( event ); | |
153 | ||
154 | LayoutItems(); | |
155 | } | |
156 | ||
157 | wxSize wxRadioBox::LayoutItems() | |
158 | { | |
159 | int x = 7; | |
160 | int y = 15; | |
161 | ||
162 | int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1; | |
163 | ||
164 | wxSize res( 0, 0 ); | |
165 | ||
166 | if (m_windowStyle & wxRA_HORIZONTAL) | |
167 | { | |
168 | ||
169 | for (int j = 0; j < m_majorDim; j++) | |
170 | { | |
171 | y = 15; | |
172 | ||
173 | int max_len = 0; | |
174 | wxNode *node = m_boxes.Nth( j*num_per_major ); | |
175 | for (int i1 = 0; i1< num_per_major; i1++) | |
176 | { | |
177 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
178 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); | |
179 | GdkFont *font = m_widget->style->font; | |
180 | int len = 22+gdk_string_measure( font, label->label ); | |
181 | if (len > max_len) max_len = len; | |
182 | ||
183 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y ); | |
184 | y += 20; | |
185 | ||
186 | node = node->Next(); | |
187 | if (!node) break; | |
188 | } | |
189 | ||
190 | // we don't know the max_len before | |
191 | ||
192 | node = m_boxes.Nth( j*num_per_major ); | |
193 | for (int i2 = 0; i2< num_per_major; i2++) | |
194 | { | |
195 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
196 | ||
197 | gtk_widget_set_usize( button, max_len, 20 ); | |
198 | ||
199 | node = node->Next(); | |
200 | if (!node) break; | |
201 | } | |
202 | ||
203 | if (y > res.y) res.y = y; | |
204 | ||
205 | x += max_len + 2; | |
206 | } | |
207 | ||
208 | res.x = x+4; | |
209 | res.y += 9; | |
210 | } | |
211 | else | |
212 | { | |
213 | int max = 0; | |
214 | ||
215 | wxNode *node = m_boxes.First(); | |
216 | while (node) | |
217 | { | |
218 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
219 | GtkLabel *label = GTK_LABEL( button->child ); | |
220 | ||
221 | GdkFont *font = m_widget->style->font; | |
222 | int len = 22+gdk_string_measure( font, label->label ); | |
223 | if (len > max) max = len; | |
224 | ||
225 | node = node->Next(); | |
226 | } | |
227 | ||
228 | node = m_boxes.First(); | |
229 | while (node) | |
230 | { | |
231 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
232 | ||
233 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y ); | |
234 | x += max; | |
235 | gtk_widget_set_usize( button, max, 20 ); | |
236 | ||
237 | node = node->Next(); | |
238 | } | |
239 | res.x = x+4; | |
240 | res.y = 42; | |
241 | } | |
242 | ||
243 | return res; | |
244 | } | |
245 | ||
246 | bool wxRadioBox::Show( bool show ) | |
247 | { | |
248 | wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") ); | |
249 | ||
250 | wxWindow::Show( show ); | |
251 | ||
252 | wxNode *node = m_boxes.First(); | |
253 | while (node) | |
254 | { | |
255 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
256 | ||
257 | if (show) gtk_widget_show( button ); else gtk_widget_hide( button ); | |
258 | ||
259 | node = node->Next(); | |
260 | } | |
261 | ||
262 | return TRUE; | |
263 | } | |
264 | ||
265 | int wxRadioBox::FindString( const wxString &s ) const | |
266 | { | |
267 | wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") ); | |
268 | ||
269 | int count = 0; | |
270 | ||
271 | wxNode *node = m_boxes.First(); | |
272 | while (node) | |
273 | { | |
274 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
275 | ||
276 | GtkLabel *label = GTK_LABEL( button->child ); | |
277 | if (s == label->label) return count; | |
278 | count++; | |
279 | ||
280 | node = node->Next(); | |
281 | } | |
282 | ||
283 | return -1; | |
284 | } | |
285 | ||
286 | void wxRadioBox::SetFocus() | |
287 | { | |
288 | wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") ); | |
289 | ||
290 | if (m_boxes.GetCount() == 0) return; | |
291 | ||
292 | wxNode *node = m_boxes.First(); | |
293 | while (node) | |
294 | { | |
295 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
296 | if (button->active) | |
297 | { | |
298 | gtk_widget_grab_focus( GTK_WIDGET(button) ); | |
299 | ||
300 | return; | |
301 | } | |
302 | node = node->Next(); | |
303 | } | |
304 | ||
305 | } | |
306 | ||
307 | void wxRadioBox::SetSelection( int n ) | |
308 | { | |
309 | wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") ); | |
310 | ||
311 | wxNode *node = m_boxes.Nth( n ); | |
312 | ||
313 | wxCHECK_RET( node, _T("radiobox wrong index") ); | |
314 | ||
315 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
316 | ||
317 | gtk_toggle_button_set_state( button, 1 ); | |
318 | } | |
319 | ||
320 | int wxRadioBox::GetSelection(void) const | |
321 | { | |
322 | wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") ); | |
323 | ||
324 | int count = 0; | |
325 | ||
326 | wxNode *node = m_boxes.First(); | |
327 | while (node) | |
328 | { | |
329 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
330 | if (button->active) return count; | |
331 | count++; | |
332 | node = node->Next(); | |
333 | } | |
334 | ||
335 | wxFAIL_MSG( _T("wxRadioBox none selected") ); | |
336 | ||
337 | return -1; | |
338 | } | |
339 | ||
340 | wxString wxRadioBox::GetString( int n ) const | |
341 | { | |
342 | wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") ); | |
343 | ||
344 | wxNode *node = m_boxes.Nth( n ); | |
345 | ||
346 | wxCHECK_MSG( node, _T(""), _T("radiobox wrong index") ); | |
347 | ||
348 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
349 | GtkLabel *label = GTK_LABEL( button->child ); | |
350 | ||
351 | return wxString( label->label ); | |
352 | } | |
353 | ||
354 | wxString wxRadioBox::GetLabel( int item ) const | |
355 | { | |
356 | wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") ); | |
357 | ||
358 | return GetString( item ); | |
359 | } | |
360 | ||
361 | void wxRadioBox::SetLabel( const wxString& label ) | |
362 | { | |
363 | wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") ); | |
364 | ||
365 | wxControl::SetLabel( label ); | |
366 | ||
367 | gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() ); | |
368 | } | |
369 | ||
370 | void wxRadioBox::SetLabel( int item, const wxString& label ) | |
371 | { | |
372 | wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") ); | |
373 | ||
374 | wxNode *node = m_boxes.Nth( item ); | |
375 | ||
376 | wxCHECK_RET( node, _T("radiobox wrong index") ); | |
377 | ||
378 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
379 | GtkLabel *g_label = GTK_LABEL( button->child ); | |
380 | ||
381 | gtk_label_set( g_label, label.mbc_str() ); | |
382 | } | |
383 | ||
384 | void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) ) | |
385 | { | |
386 | wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented.")); | |
387 | } | |
388 | ||
389 | void wxRadioBox::Enable( bool enable ) | |
390 | { | |
391 | wxControl::Enable( enable ); | |
392 | ||
393 | wxNode *node = m_boxes.First(); | |
394 | while (node) | |
395 | { | |
396 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
397 | GtkWidget *label = button->child; | |
398 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
399 | gtk_widget_set_sensitive( label, enable ); | |
400 | node = node->Next(); | |
401 | } | |
402 | } | |
403 | ||
404 | void wxRadioBox::Enable( int item, bool enable ) | |
405 | { | |
406 | wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") ); | |
407 | ||
408 | wxNode *node = m_boxes.Nth( item ); | |
409 | ||
410 | wxCHECK_RET( node, _T("radiobox wrong index") ); | |
411 | ||
412 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
413 | GtkWidget *label = button->child; | |
414 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
415 | gtk_widget_set_sensitive( label, enable ); | |
416 | } | |
417 | ||
418 | void wxRadioBox::Show( int item, bool show ) | |
419 | { | |
420 | wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") ); | |
421 | ||
422 | wxNode *node = m_boxes.Nth( item ); | |
423 | ||
424 | wxCHECK_RET( node, _T("radiobox wrong index") ); | |
425 | ||
426 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
427 | ||
428 | if (show) | |
429 | gtk_widget_show( button ); | |
430 | else | |
431 | gtk_widget_hide( button ); | |
432 | } | |
433 | ||
434 | wxString wxRadioBox::GetStringSelection(void) const | |
435 | { | |
436 | wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") ); | |
437 | ||
438 | wxNode *node = m_boxes.First(); | |
439 | while (node) | |
440 | { | |
441 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
442 | if (button->active) | |
443 | { | |
444 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); | |
445 | return label->label; | |
446 | } | |
447 | node = node->Next(); | |
448 | } | |
449 | ||
450 | wxFAIL_MSG( _T("wxRadioBox none selected") ); | |
451 | return _T(""); | |
452 | } | |
453 | ||
454 | bool wxRadioBox::SetStringSelection( const wxString &s ) | |
455 | { | |
456 | wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") ); | |
457 | ||
458 | int res = FindString( s ); | |
459 | if (res == -1) return FALSE; | |
460 | SetSelection( res ); | |
461 | ||
462 | return TRUE; | |
463 | } | |
464 | ||
465 | int wxRadioBox::Number(void) const | |
466 | { | |
467 | return m_boxes.Number(); | |
468 | } | |
469 | ||
470 | int wxRadioBox::GetNumberOfRowsOrCols(void) const | |
471 | { | |
472 | return 1; | |
473 | } | |
474 | ||
475 | void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) ) | |
476 | { | |
477 | wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented.")); | |
478 | } | |
479 | ||
480 | void wxRadioBox::ApplyWidgetStyle() | |
481 | { | |
482 | SetWidgetStyle(); | |
483 | ||
484 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
485 | ||
486 | wxNode *node = m_boxes.First(); | |
487 | while (node) | |
488 | { | |
489 | GtkWidget *widget = GTK_WIDGET( node->Data() ); | |
490 | gtk_widget_set_style( widget, m_widgetStyle ); | |
491 | ||
492 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
493 | gtk_widget_set_style( button->child, m_widgetStyle ); | |
494 | ||
495 | node = node->Next(); | |
496 | } | |
497 | } | |
498 | ||
499 | bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window ) | |
500 | { | |
501 | if (window == m_widget->window) return TRUE; | |
502 | ||
503 | wxNode *node = m_boxes.First(); | |
504 | while (node) | |
505 | { | |
506 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
507 | ||
508 | if (window == button->window) return TRUE; | |
509 | ||
510 | node = node->Next(); | |
511 | } | |
512 | ||
513 | return FALSE; | |
514 | } |