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