]>
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 | ||
e55ad60e RR |
161 | int w = m_width-10; |
162 | if (w < 15) w = 15; | |
163 | gtk_widget_set_usize( button, w, 20 ); | |
164 | ||
d6d1892b | 165 | node = node->Next(); |
3f659fd6 RR |
166 | } |
167 | } | |
168 | ||
debe6624 | 169 | bool wxRadioBox::Show( bool show ) |
c801d85f KB |
170 | { |
171 | wxWindow::Show( show ); | |
172 | ||
d6d1892b RR |
173 | wxNode *node = m_boxes.First(); |
174 | while (node) | |
c801d85f | 175 | { |
d6d1892b RR |
176 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
177 | ||
178 | if (show) gtk_widget_show( button ); else gtk_widget_hide( button ); | |
179 | ||
180 | node = node->Next(); | |
6de97a3b | 181 | } |
c801d85f KB |
182 | |
183 | return TRUE; | |
6de97a3b | 184 | } |
c801d85f | 185 | |
47908e25 | 186 | int wxRadioBox::FindString( const wxString &s ) const |
c801d85f | 187 | { |
d6d1892b | 188 | int count = 0; |
47908e25 | 189 | |
d6d1892b RR |
190 | wxNode *node = m_boxes.First(); |
191 | while (node) | |
47908e25 | 192 | { |
d6d1892b RR |
193 | GtkButton *button = GTK_BUTTON( node->Data() ); |
194 | ||
195 | GtkLabel *label = GTK_LABEL( button->child ); | |
196 | if (s == label->label) return count; | |
197 | count++; | |
198 | ||
199 | node = node->Next(); | |
6de97a3b | 200 | } |
d6d1892b | 201 | |
47908e25 | 202 | return -1; |
6de97a3b | 203 | } |
c801d85f | 204 | |
47908e25 | 205 | void wxRadioBox::SetSelection( int n ) |
c801d85f | 206 | { |
d6d1892b | 207 | wxNode *node = m_boxes.Nth( n ); |
d3904ceb | 208 | |
d6d1892b | 209 | if (!node) |
d3904ceb RR |
210 | { |
211 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
212 | return; | |
213 | } | |
47908e25 | 214 | |
d6d1892b | 215 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
47908e25 RR |
216 | |
217 | gtk_toggle_button_set_state( button, 1 ); | |
6de97a3b | 218 | } |
c801d85f KB |
219 | |
220 | int wxRadioBox::GetSelection(void) const | |
221 | { | |
c801d85f | 222 | int count = 0; |
d6d1892b RR |
223 | |
224 | wxNode *node = m_boxes.First(); | |
225 | while (node) | |
c801d85f | 226 | { |
d6d1892b RR |
227 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
228 | if (button->active) return count; | |
c801d85f | 229 | count++; |
d6d1892b | 230 | node = node->Next(); |
6de97a3b | 231 | } |
2b5bd7fd | 232 | |
d6d1892b RR |
233 | wxFAIL_MSG( "wxRadioBox none selected" ); |
234 | ||
235 | return -1; | |
6de97a3b | 236 | } |
c801d85f | 237 | |
47908e25 | 238 | wxString wxRadioBox::GetString( int n ) const |
c801d85f | 239 | { |
d6d1892b | 240 | wxNode *node = m_boxes.Nth( n ); |
47908e25 | 241 | |
d6d1892b | 242 | if (!node) |
d3904ceb RR |
243 | { |
244 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
245 | return ""; | |
246 | } | |
47908e25 | 247 | |
d6d1892b | 248 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb | 249 | GtkLabel *label = GTK_LABEL( button->child ); |
47908e25 RR |
250 | |
251 | return wxString( label->label ); | |
6de97a3b | 252 | } |
c801d85f | 253 | |
d3904ceb | 254 | wxString wxRadioBox::GetLabel( int item ) const |
c801d85f | 255 | { |
d3904ceb | 256 | return GetString( item ); |
6de97a3b | 257 | } |
c801d85f | 258 | |
d3904ceb | 259 | void wxRadioBox::SetLabel( const wxString& label ) |
c801d85f | 260 | { |
d3904ceb RR |
261 | wxControl::SetLabel( label ); |
262 | GtkFrame *frame = GTK_FRAME( m_widget ); | |
263 | gtk_frame_set_label( frame, wxControl::GetLabel() ); | |
6de97a3b | 264 | } |
c801d85f | 265 | |
d3904ceb | 266 | void wxRadioBox::SetLabel( int item, const wxString& label ) |
c801d85f | 267 | { |
d6d1892b | 268 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 269 | |
d6d1892b | 270 | if (!node) |
d3904ceb RR |
271 | { |
272 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
273 | return; | |
274 | } | |
275 | ||
d6d1892b | 276 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
277 | GtkLabel *g_label = GTK_LABEL( button->child ); |
278 | ||
279 | gtk_label_set( g_label, label ); | |
6de97a3b | 280 | } |
c801d85f | 281 | |
debe6624 | 282 | void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) ) |
c801d85f | 283 | { |
a3622daa | 284 | wxFAIL_MSG("wxRadioBox::SetLabel not implemented."); |
6de97a3b | 285 | } |
c801d85f | 286 | |
d3904ceb | 287 | void wxRadioBox::Enable( bool enable ) |
c801d85f | 288 | { |
d3904ceb RR |
289 | wxControl::Enable( enable ); |
290 | ||
d6d1892b RR |
291 | wxNode *node = m_boxes.First(); |
292 | while (node) | |
d3904ceb | 293 | { |
d6d1892b | 294 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
295 | GtkWidget *label = button->child; |
296 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
297 | gtk_widget_set_sensitive( label, enable ); | |
d6d1892b | 298 | node = node->Next(); |
d3904ceb | 299 | } |
6de97a3b | 300 | } |
c801d85f | 301 | |
d3904ceb | 302 | void wxRadioBox::Enable( int item, bool enable ) |
c801d85f | 303 | { |
d6d1892b | 304 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 305 | |
d6d1892b | 306 | if (!node) |
d3904ceb RR |
307 | { |
308 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
309 | return; | |
310 | } | |
311 | ||
d6d1892b | 312 | GtkButton *button = GTK_BUTTON( node->Data() ); |
d3904ceb RR |
313 | GtkWidget *label = button->child; |
314 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
315 | gtk_widget_set_sensitive( label, enable ); | |
6de97a3b | 316 | } |
c801d85f | 317 | |
d3904ceb | 318 | void wxRadioBox::Show( int item, bool show ) |
c801d85f | 319 | { |
d6d1892b | 320 | wxNode *node = m_boxes.Nth( item ); |
d3904ceb | 321 | |
d6d1892b | 322 | if (!node) |
d3904ceb RR |
323 | { |
324 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
325 | return; | |
326 | } | |
327 | ||
d6d1892b | 328 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
c801d85f | 329 | |
d3904ceb RR |
330 | if (show) |
331 | gtk_widget_show( button ); | |
332 | else | |
333 | gtk_widget_hide( button ); | |
6de97a3b | 334 | } |
c801d85f KB |
335 | |
336 | wxString wxRadioBox::GetStringSelection(void) const | |
337 | { | |
d6d1892b RR |
338 | wxNode *node = m_boxes.First(); |
339 | while (node) | |
c801d85f | 340 | { |
d6d1892b RR |
341 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
342 | if (button->active) | |
c801d85f | 343 | { |
d6d1892b | 344 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); |
c801d85f | 345 | return label->label; |
6de97a3b | 346 | } |
d6d1892b | 347 | node = node->Next(); |
6de97a3b | 348 | } |
d6d1892b RR |
349 | |
350 | wxFAIL_MSG( "wxRadioBox none selected" ); | |
c801d85f | 351 | return ""; |
6de97a3b | 352 | } |
c801d85f | 353 | |
47908e25 | 354 | bool wxRadioBox::SetStringSelection( const wxString&s ) |
c801d85f | 355 | { |
47908e25 RR |
356 | int res = FindString( s ); |
357 | if (res == -1) return FALSE; | |
358 | SetSelection( res ); | |
c801d85f | 359 | return TRUE; |
6de97a3b | 360 | } |
c801d85f KB |
361 | |
362 | int wxRadioBox::Number(void) const | |
363 | { | |
d6d1892b | 364 | return m_boxes.Number(); |
6de97a3b | 365 | } |
c801d85f KB |
366 | |
367 | int wxRadioBox::GetNumberOfRowsOrCols(void) const | |
368 | { | |
369 | return 1; | |
6de97a3b | 370 | } |
c801d85f | 371 | |
debe6624 | 372 | void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) ) |
c801d85f | 373 | { |
a3622daa | 374 | wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented."); |
6de97a3b | 375 | } |
c801d85f | 376 | |
868a2826 RR |
377 | void wxRadioBox::SetFont( const wxFont &font ) |
378 | { | |
379 | wxWindow::SetFont( font ); | |
380 | ||
d6d1892b RR |
381 | wxNode *node = m_boxes.First(); |
382 | while (node) | |
868a2826 | 383 | { |
d6d1892b | 384 | GtkButton *button = GTK_BUTTON( node->Data() ); |
868a2826 RR |
385 | |
386 | gtk_widget_set_style( button->child, | |
387 | gtk_style_ref( | |
388 | gtk_widget_get_style( m_widget ) ) ); | |
389 | ||
d6d1892b | 390 | node = node->Next(); |
868a2826 RR |
391 | } |
392 | } | |
b4071e91 RR |
393 | |
394 | bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window ) | |
395 | { | |
396 | if (window == m_widget->window) return TRUE; | |
397 | ||
d6d1892b RR |
398 | wxNode *node = m_boxes.First(); |
399 | while (node) | |
b4071e91 | 400 | { |
d6d1892b RR |
401 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
402 | ||
b4071e91 | 403 | if (window == button->window) return TRUE; |
d6d1892b RR |
404 | |
405 | node = node->Next(); | |
b4071e91 RR |
406 | } |
407 | ||
408 | return FALSE; | |
409 | } |