]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobox.cpp
SetFont() the second
[wxWidgets.git] / src / gtk / radiobox.cpp
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
21 //-----------------------------------------------------------------------------
22 // data
23 //-----------------------------------------------------------------------------
24
25 extern bool g_blockEventsOnDrag;
26
27 //-----------------------------------------------------------------------------
28 // wxRadioBox
29 //-----------------------------------------------------------------------------
30
31 static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb )
32 {
33 if (!rb->HasVMT()) return;
34 if (g_blockEventsOnDrag) return;
35
36 if (rb->m_alreadySent)
37 {
38 rb->m_alreadySent = FALSE;
39 return;
40 }
41
42 rb->m_alreadySent = TRUE;
43
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 );
49 rb->GetEventHandler()->ProcessEvent(event);
50 }
51
52 //-----------------------------------------------------------------------------
53
54 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
55
56 BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
57 EVT_SIZE(wxRadioBox::OnSize)
58 END_EVENT_TABLE()
59
60 wxRadioBox::wxRadioBox(void)
61 {
62 }
63
64 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
65 const wxPoint &pos, const wxSize &size,
66 int n, const wxString choices[], int WXUNUSED(majorDim),
67 long style, const wxValidator& validator, const wxString &name )
68 {
69 m_alreadySent = FALSE;
70 m_needParent = TRUE;
71
72 PreCreation( parent, id, pos, size, style, name );
73
74 SetValidator( validator );
75
76 m_widget = gtk_frame_new( title );
77
78 int x = m_x+5;
79 int y = m_y+15;
80 int maxLen = 0;
81 int height = 20;
82
83 // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0))
84 if (n > 0)
85 {
86 GSList *radio_button_group = (GSList *) NULL;
87 for (int i = 0; i < n; i++)
88 {
89 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
90
91 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) );
92
93 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
94
95 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
96 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
97
98 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y );
99
100 int tmp = 22+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] );
101 if (tmp > maxLen) maxLen = tmp;
102
103 int width = m_width-10;
104 if (size.x == -1) width = tmp;
105 gtk_widget_set_usize( GTK_WIDGET(m_radio), width, 20 );
106
107 y += 20;
108 height += 20;
109
110 }
111 }
112
113 wxSize newSize = size;
114 if (newSize.x == -1) newSize.x = maxLen+10;
115 if (newSize.y == -1) newSize.y = height;
116 SetSize( newSize.x, newSize.y );
117
118 PostCreation();
119
120 Show( TRUE );
121
122 return TRUE;
123 }
124
125 void wxRadioBox::OnSize( wxSizeEvent &WXUNUSED(event) )
126 {
127 int x = m_x+5;
128 int y = m_y+15;
129
130 GSList *item = gtk_radio_button_group( m_radio );
131 while (item)
132 {
133 GtkWidget *button = GTK_WIDGET( item->data );
134
135 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
136
137 y += 20;
138
139 item = item->next;
140 }
141 }
142
143 bool wxRadioBox::Show( bool show )
144 {
145 wxWindow::Show( show );
146
147 GSList *item = gtk_radio_button_group( m_radio );
148 while (item)
149 {
150 GtkWidget *w = GTK_WIDGET( item->data );
151 if (show) gtk_widget_show( w ); else gtk_widget_hide( w );
152 item = item->next;
153 }
154
155 return TRUE;
156 }
157
158 int wxRadioBox::FindString( const wxString &s ) const
159 {
160 GSList *item = gtk_radio_button_group( m_radio );
161
162 int count = g_slist_length(item)-1;
163
164 while (item)
165 {
166 GtkButton *b = GTK_BUTTON( item->data );
167 GtkLabel *l = GTK_LABEL( b->child );
168 if (s == l->label) return count;
169 count--;
170 item = item->next;
171 }
172
173 return -1;
174 }
175
176 void wxRadioBox::SetSelection( int n )
177 {
178 GSList *item = gtk_radio_button_group( m_radio );
179 item = g_slist_nth( item, g_slist_length(item)-n-1 );
180 if (!item) return;
181
182 GtkToggleButton *button = GTK_TOGGLE_BUTTON( item->data );
183
184 gtk_toggle_button_set_state( button, 1 );
185 }
186
187 int wxRadioBox::GetSelection(void) const
188 {
189 GSList *item = gtk_radio_button_group( m_radio );
190 int count = 0;
191 int found = -1;
192 while (item)
193 {
194 GtkButton *button = GTK_BUTTON( item->data );
195 if (GTK_TOGGLE_BUTTON(button)->active) found = count;
196 count++;
197 item = item->next;
198 }
199
200 return found != -1 ? count-found-1 : -1;
201 }
202
203 wxString wxRadioBox::GetString( int n ) const
204 {
205 GSList *item = gtk_radio_button_group( m_radio );
206
207 item = g_slist_nth( item, g_slist_length(item)-n-1 );
208 if (!item) return "";
209
210 GtkToggleButton *button = GTK_TOGGLE_BUTTON( item->data );
211
212 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
213
214 return wxString( label->label );
215 }
216
217 wxString wxRadioBox::GetLabel(void) const
218 {
219 return wxControl::GetLabel();
220 }
221
222 void wxRadioBox::SetLabel( const wxString& WXUNUSED(label) )
223 {
224 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
225 }
226
227 void wxRadioBox::SetLabel( int WXUNUSED(item), const wxString& WXUNUSED(label) )
228 {
229 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
230 }
231
232 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
233 {
234 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
235 }
236
237 wxString wxRadioBox::GetLabel( int WXUNUSED(item) ) const
238 {
239 wxFAIL_MSG("wxRadioBox::GetLabel not implemented.");
240 return "";
241 }
242
243 void wxRadioBox::Enable( bool WXUNUSED(enable) )
244 {
245 wxFAIL_MSG("wxRadioBox::Enable not implemented.");
246 }
247
248 void wxRadioBox::Enable( int WXUNUSED(item), bool WXUNUSED(enable) )
249 {
250 wxFAIL_MSG("wxRadioBox::Enable not implemented.");
251 }
252
253 void wxRadioBox::Show( int WXUNUSED(item), bool WXUNUSED(show) )
254 {
255 wxFAIL_MSG("wxRadioBox::Show not implemented.");
256 }
257
258 wxString wxRadioBox::GetStringSelection(void) const
259 {
260 GSList *item = gtk_radio_button_group( m_radio );
261 while (item)
262 {
263 GtkButton *button = GTK_BUTTON( item->data );
264 if (GTK_TOGGLE_BUTTON(button)->active)
265 {
266 GtkLabel *label = GTK_LABEL( button->child );
267 return label->label;
268 }
269 item = item->next;
270 }
271 return "";
272 }
273
274 bool wxRadioBox::SetStringSelection( const wxString&s )
275 {
276 int res = FindString( s );
277 if (res == -1) return FALSE;
278 SetSelection( res );
279 return TRUE;
280 }
281
282 int wxRadioBox::Number(void) const
283 {
284 int count = 0;
285 GSList *item = gtk_radio_button_group( m_radio );
286 while (item)
287 {
288 item = item->next;
289 count++;
290 }
291 return count;
292 }
293
294 int wxRadioBox::GetNumberOfRowsOrCols(void) const
295 {
296 return 1;
297 }
298
299 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
300 {
301 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
302 }
303
304 void wxRadioBox::SetFont( const wxFont &font )
305 {
306 wxWindow::SetFont( font );
307
308 GSList *item = gtk_radio_button_group( m_radio );
309 while (item)
310 {
311 GtkButton *button = GTK_BUTTON( item->data );
312
313 gtk_widget_set_style( button->child,
314 gtk_style_ref(
315 gtk_widget_get_style( m_widget ) ) );
316
317 item = item->next;
318 }
319 }