]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: radiobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "radiobox.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/radiobox.h" | |
17 | #include "wx/dialog.h" | |
18 | #include "wx/frame.h" | |
19 | #include "wx/gtk/win_gtk.h" | |
20 | ||
66bd6b93 RR |
21 | //----------------------------------------------------------------------------- |
22 | // data | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | extern bool g_blockEventsOnDrag; | |
26 | ||
c801d85f | 27 | //----------------------------------------------------------------------------- |
b4071e91 | 28 | // "clicked" |
c801d85f KB |
29 | //----------------------------------------------------------------------------- |
30 | ||
66bd6b93 | 31 | static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb ) |
c801d85f | 32 | { |
66bd6b93 RR |
33 | if (!rb->HasVMT()) return; |
34 | if (g_blockEventsOnDrag) return; | |
35 | ||
47908e25 RR |
36 | if (rb->m_alreadySent) |
37 | { | |
38 | rb->m_alreadySent = FALSE; | |
39 | return; | |
40 | } | |
41 | ||
42 | rb->m_alreadySent = TRUE; | |
43 | ||
c801d85f KB |
44 | wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() ); |
45 | event.SetInt( rb->GetSelection() ); | |
46 | wxString tmp( rb->GetStringSelection() ); | |
47 | event.SetString( WXSTRINGCAST(tmp) ); | |
48 | event.SetEventObject( rb ); | |
47908e25 | 49 | rb->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 50 | } |
c801d85f | 51 | |
b4071e91 RR |
52 | //----------------------------------------------------------------------------- |
53 | // wxRadioBox | |
c801d85f KB |
54 | //----------------------------------------------------------------------------- |
55 | ||
56 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl) | |
57 | ||
3f659fd6 RR |
58 | BEGIN_EVENT_TABLE(wxRadioBox, wxControl) |
59 | EVT_SIZE(wxRadioBox::OnSize) | |
60 | END_EVENT_TABLE() | |
61 | ||
c801d85f KB |
62 | wxRadioBox::wxRadioBox(void) |
63 | { | |
6de97a3b | 64 | } |
c801d85f | 65 | |
debe6624 JS |
66 | bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title, |
67 | const wxPoint &pos, const wxSize &size, | |
6de97a3b RR |
68 | int n, const wxString choices[], int WXUNUSED(majorDim), |
69 | long style, const wxValidator& validator, const wxString &name ) | |
c801d85f | 70 | { |
47908e25 | 71 | m_alreadySent = FALSE; |
c801d85f KB |
72 | m_needParent = TRUE; |
73 | ||
74 | PreCreation( parent, id, pos, size, style, name ); | |
75 | ||
6de97a3b RR |
76 | SetValidator( validator ); |
77 | ||
c801d85f KB |
78 | m_widget = gtk_frame_new( title ); |
79 | ||
80 | int x = m_x+5; | |
81 | int y = m_y+15; | |
82 | int maxLen = 0; | |
83 | int height = 20; | |
84 | ||
d6d1892b RR |
85 | GtkRadioButton *m_radio = (GtkRadioButton*) NULL; |
86 | ||
c801d85f KB |
87 | // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0)) |
88 | if (n > 0) | |
89 | { | |
c67daf87 | 90 | GSList *radio_button_group = (GSList *) NULL; |
c801d85f KB |
91 | for (int i = 0; i < n; i++) |
92 | { | |
93 | if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) ); | |
47908e25 | 94 | |
c801d85f KB |
95 | m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) ); |
96 | ||
d6d1892b RR |
97 | m_boxes.Append( (wxObject*) m_radio ); |
98 | ||
b4071e91 RR |
99 | ConnectWidget( GTK_WIDGET(m_radio) ); |
100 | ||
c801d85f KB |
101 | if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE ); |
102 | ||
103 | gtk_signal_connect( GTK_OBJECT(m_radio), "clicked", | |
104 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); | |
105 | ||
106 | gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y ); | |
107 | ||
108 | int tmp = 22+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] ); | |
109 | if (tmp > maxLen) maxLen = tmp; | |
110 | ||
111 | int width = m_width-10; | |
112 | if (size.x == -1) width = tmp; | |
113 | gtk_widget_set_usize( GTK_WIDGET(m_radio), width, 20 ); | |
114 | ||
115 | y += 20; | |
116 | height += 20; | |
117 | ||
6de97a3b RR |
118 | } |
119 | } | |
d6d1892b | 120 | |
c801d85f KB |
121 | wxSize newSize = size; |
122 | if (newSize.x == -1) newSize.x = maxLen+10; | |
123 | if (newSize.y == -1) newSize.y = height; | |
124 | SetSize( newSize.x, newSize.y ); | |
125 | ||
126 | PostCreation(); | |
127 | ||
d6d1892b RR |
128 | SetLabel( title ); |
129 | ||
c801d85f KB |
130 | Show( TRUE ); |
131 | ||
132 | return TRUE; | |
6de97a3b | 133 | } |
c801d85f | 134 | |
d6d1892b RR |
135 | wxRadioBox::~wxRadioBox(void) |
136 | { | |
137 | wxNode *node = m_boxes.First(); | |
138 | while (node) | |
139 | { | |
140 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
141 | gtk_widget_destroy( button ); | |
142 | node = node->Next(); | |
143 | } | |
144 | } | |
145 | ||
b4071e91 | 146 | void wxRadioBox::OnSize( wxSizeEvent &event ) |
3f659fd6 | 147 | { |
b4071e91 RR |
148 | wxControl::OnSize( event ); |
149 | ||
3f659fd6 RR |
150 | int x = m_x+5; |
151 | int y = m_y+15; | |
152 | ||
d6d1892b RR |
153 | wxNode *node = m_boxes.First(); |
154 | while (node) | |
3f659fd6 | 155 | { |
d6d1892b | 156 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
3f659fd6 RR |
157 | |
158 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y ); | |
3f659fd6 RR |
159 | y += 20; |
160 | ||
d6d1892b | 161 | node = node->Next(); |
3f659fd6 RR |
162 | } |
163 | } | |
164 | ||
debe6624 | 165 | bool wxRadioBox::Show( bool show ) |
c801d85f KB |
166 | { |
167 | wxWindow::Show( show ); | |
168 | ||
d6d1892b RR |
169 | wxNode *node = m_boxes.First(); |
170 | while (node) | |
c801d85f | 171 | { |
d6d1892b RR |
172 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
173 | ||
174 | if (show) gtk_widget_show( button ); else gtk_widget_hide( button ); | |
175 | ||
176 | node = node->Next(); | |
6de97a3b | 177 | } |
c801d85f KB |
178 | |
179 | return TRUE; | |
6de97a3b | 180 | } |
c801d85f | 181 | |
47908e25 | 182 | int wxRadioBox::FindString( const wxString &s ) const |
c801d85f | 183 | { |
d6d1892b | 184 | int count = 0; |
47908e25 | 185 | |
d6d1892b RR |
186 | wxNode *node = m_boxes.First(); |
187 | while (node) | |
47908e25 | 188 | { |
d6d1892b RR |
189 | GtkButton *button = GTK_BUTTON( node->Data() ); |
190 | ||
191 | GtkLabel *label = GTK_LABEL( button->child ); | |
192 | if (s == label->label) return count; | |
193 | count++; | |
194 | ||
195 | node = node->Next(); | |
6de97a3b | 196 | } |
d6d1892b | 197 | |
47908e25 | 198 | return -1; |
6de97a3b | 199 | } |
c801d85f | 200 | |
47908e25 | 201 | void wxRadioBox::SetSelection( int n ) |
c801d85f | 202 | { |
d6d1892b | 203 | wxNode *node = m_boxes.Nth( n ); |
d3904ceb | 204 | |
d6d1892b | 205 | if (!node) |
d3904ceb RR |
206 | { |
207 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
208 | return; | |
209 | } | |
47908e25 | 210 | |
d6d1892b | 211 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
47908e25 RR |
212 | |
213 | gtk_toggle_button_set_state( button, 1 ); | |
6de97a3b | 214 | } |
c801d85f KB |
215 | |
216 | int wxRadioBox::GetSelection(void) const | |
217 | { | |
c801d85f | 218 | int count = 0; |
d6d1892b RR |
219 | |
220 | wxNode *node = m_boxes.First(); | |
221 | while (node) | |
c801d85f | 222 | { |
d6d1892b RR |
223 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
224 | if (button->active) return count; | |
c801d85f | 225 | count++; |
d6d1892b | 226 | node = node->Next(); |
6de97a3b | 227 | } |
2b5bd7fd | 228 | |
d6d1892b RR |
229 | wxFAIL_MSG( "wxRadioBox none selected" ); |
230 | ||
231 | return -1; | |
6de97a3b | 232 | } |
c801d85f | 233 | |
47908e25 | 234 | wxString wxRadioBox::GetString( int n ) const |
c801d85f | 235 | { |
d6d1892b | 236 | wxNode *node = m_boxes.Nth( n ); |
47908e25 | 237 | |
d6d1892b | 238 | if (!node) |
d3904ceb RR |
239 | { |
240 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
241 | return ""; | |
242 | } | |
47908e25 | 243 | |
d6d1892b | 244 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb | 245 | GtkLabel *label = GTK_LABEL( button->child ); |
47908e25 RR |
246 | |
247 | return wxString( label->label ); | |
6de97a3b | 248 | } |
c801d85f | 249 | |
d3904ceb | 250 | wxString wxRadioBox::GetLabel( int item ) const |
c801d85f | 251 | { |
d3904ceb | 252 | return GetString( item ); |
6de97a3b | 253 | } |
c801d85f | 254 | |
d3904ceb | 255 | void wxRadioBox::SetLabel( const wxString& label ) |
c801d85f | 256 | { |
d3904ceb RR |
257 | wxControl::SetLabel( label ); |
258 | GtkFrame *frame = GTK_FRAME( m_widget ); | |
259 | gtk_frame_set_label( frame, wxControl::GetLabel() ); | |
6de97a3b | 260 | } |
c801d85f | 261 | |
d3904ceb | 262 | void wxRadioBox::SetLabel( int item, const wxString& label ) |
c801d85f | 263 | { |
d6d1892b | 264 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 265 | |
d6d1892b | 266 | if (!node) |
d3904ceb RR |
267 | { |
268 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
269 | return; | |
270 | } | |
271 | ||
d6d1892b | 272 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
273 | GtkLabel *g_label = GTK_LABEL( button->child ); |
274 | ||
275 | gtk_label_set( g_label, label ); | |
6de97a3b | 276 | } |
c801d85f | 277 | |
debe6624 | 278 | void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) ) |
c801d85f | 279 | { |
a3622daa | 280 | wxFAIL_MSG("wxRadioBox::SetLabel not implemented."); |
6de97a3b | 281 | } |
c801d85f | 282 | |
d3904ceb | 283 | void wxRadioBox::Enable( bool enable ) |
c801d85f | 284 | { |
d3904ceb RR |
285 | wxControl::Enable( enable ); |
286 | ||
d6d1892b RR |
287 | wxNode *node = m_boxes.First(); |
288 | while (node) | |
d3904ceb | 289 | { |
d6d1892b | 290 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
291 | GtkWidget *label = button->child; |
292 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
293 | gtk_widget_set_sensitive( label, enable ); | |
d6d1892b | 294 | node = node->Next(); |
d3904ceb | 295 | } |
6de97a3b | 296 | } |
c801d85f | 297 | |
d3904ceb | 298 | void wxRadioBox::Enable( int item, bool enable ) |
c801d85f | 299 | { |
d6d1892b | 300 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 301 | |
d6d1892b | 302 | if (!node) |
d3904ceb RR |
303 | { |
304 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
305 | return; | |
306 | } | |
307 | ||
d6d1892b | 308 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
309 | GtkWidget *label = button->child; |
310 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
311 | gtk_widget_set_sensitive( label, enable ); | |
6de97a3b | 312 | } |
c801d85f | 313 | |
d3904ceb | 314 | void wxRadioBox::Show( int item, bool show ) |
c801d85f | 315 | { |
d6d1892b | 316 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 317 | |
d6d1892b | 318 | if (!node) |
d3904ceb RR |
319 | { |
320 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
321 | return; | |
322 | } | |
323 | ||
d6d1892b | 324 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
c801d85f | 325 | |
d3904ceb RR |
326 | if (show) |
327 | gtk_widget_show( button ); | |
328 | else | |
329 | gtk_widget_hide( button ); | |
6de97a3b | 330 | } |
c801d85f KB |
331 | |
332 | wxString wxRadioBox::GetStringSelection(void) const | |
333 | { | |
d6d1892b RR |
334 | wxNode *node = m_boxes.First(); |
335 | while (node) | |
c801d85f | 336 | { |
d6d1892b RR |
337 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
338 | if (button->active) | |
c801d85f | 339 | { |
d6d1892b | 340 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); |
c801d85f | 341 | return label->label; |
6de97a3b | 342 | } |
d6d1892b | 343 | node = node->Next(); |
6de97a3b | 344 | } |
d6d1892b RR |
345 | |
346 | wxFAIL_MSG( "wxRadioBox none selected" ); | |
c801d85f | 347 | return ""; |
6de97a3b | 348 | } |
c801d85f | 349 | |
47908e25 | 350 | bool wxRadioBox::SetStringSelection( const wxString&s ) |
c801d85f | 351 | { |
47908e25 RR |
352 | int res = FindString( s ); |
353 | if (res == -1) return FALSE; | |
354 | SetSelection( res ); | |
c801d85f | 355 | return TRUE; |
6de97a3b | 356 | } |
c801d85f KB |
357 | |
358 | int wxRadioBox::Number(void) const | |
359 | { | |
d6d1892b | 360 | return m_boxes.Number(); |
6de97a3b | 361 | } |
c801d85f KB |
362 | |
363 | int wxRadioBox::GetNumberOfRowsOrCols(void) const | |
364 | { | |
365 | return 1; | |
6de97a3b | 366 | } |
c801d85f | 367 | |
debe6624 | 368 | void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) ) |
c801d85f | 369 | { |
a3622daa | 370 | wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented."); |
6de97a3b | 371 | } |
c801d85f | 372 | |
868a2826 RR |
373 | void wxRadioBox::SetFont( const wxFont &font ) |
374 | { | |
375 | wxWindow::SetFont( font ); | |
376 | ||
d6d1892b RR |
377 | wxNode *node = m_boxes.First(); |
378 | while (node) | |
868a2826 | 379 | { |
d6d1892b | 380 | GtkButton *button = GTK_BUTTON( node->Data() ); |
868a2826 RR |
381 | |
382 | gtk_widget_set_style( button->child, | |
383 | gtk_style_ref( | |
384 | gtk_widget_get_style( m_widget ) ) ); | |
385 | ||
d6d1892b | 386 | node = node->Next(); |
868a2826 RR |
387 | } |
388 | } | |
b4071e91 RR |
389 | |
390 | bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window ) | |
391 | { | |
392 | if (window == m_widget->window) return TRUE; | |
393 | ||
d6d1892b RR |
394 | wxNode *node = m_boxes.First(); |
395 | while (node) | |
b4071e91 | 396 | { |
d6d1892b RR |
397 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
398 | ||
b4071e91 | 399 | if (window == button->window) return TRUE; |
d6d1892b RR |
400 | |
401 | node = node->Next(); | |
b4071e91 RR |
402 | } |
403 | ||
404 | return FALSE; | |
405 | } |