Added wxRadioButton (not tested)
[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 = 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 while (item)
170 {
171 GtkButton *button = GTK_BUTTON( item->data );
172 if (GTK_TOGGLE_BUTTON(button)->active) return count;
173 count++;
174 item = item->next;
175 }
176 return -1;
177 }
178
179 wxString wxRadioBox::GetString( int n ) const
180 {
181 GSList *item = gtk_radio_button_group( m_radio );
182
183 item = g_slist_nth( item, g_slist_length(item)-n-1 );
184 if (!item) return "";
185
186 GtkToggleButton *button = GTK_TOGGLE_BUTTON( item->data );
187
188 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
189
190 return wxString( label->label );
191 }
192
193 wxString wxRadioBox::GetLabel(void) const
194 {
195 return wxControl::GetLabel();
196 }
197
198 void wxRadioBox::SetLabel( const wxString& WXUNUSED(label) )
199 {
200 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
201 }
202
203 void wxRadioBox::SetLabel( int WXUNUSED(item), const wxString& WXUNUSED(label) )
204 {
205 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
206 }
207
208 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
209 {
210 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
211 }
212
213 wxString wxRadioBox::GetLabel( int WXUNUSED(item) ) const
214 {
215 wxFAIL_MSG("wxRadioBox::GetLabel not implemented.");
216 return "";
217 }
218
219 void wxRadioBox::Enable( bool WXUNUSED(enable) )
220 {
221 wxFAIL_MSG("wxRadioBox::Enable not implemented.");
222 }
223
224 void wxRadioBox::Enable( int WXUNUSED(item), bool WXUNUSED(enable) )
225 {
226 wxFAIL_MSG("wxRadioBox::Enable not implemented.");
227 }
228
229 void wxRadioBox::Show( int WXUNUSED(item), bool WXUNUSED(show) )
230 {
231 wxFAIL_MSG("wxRadioBox::Show not implemented.");
232 }
233
234 wxString wxRadioBox::GetStringSelection(void) const
235 {
236 GSList *item = gtk_radio_button_group( m_radio );
237 while (item)
238 {
239 GtkButton *button = GTK_BUTTON( item->data );
240 if (GTK_TOGGLE_BUTTON(button)->active)
241 {
242 GtkLabel *label = GTK_LABEL( button->child );
243 return label->label;
244 }
245 item = item->next;
246 }
247 return "";
248 }
249
250 bool wxRadioBox::SetStringSelection( const wxString&s )
251 {
252 int res = FindString( s );
253 if (res == -1) return FALSE;
254 SetSelection( res );
255 return TRUE;
256 }
257
258 int wxRadioBox::Number(void) const
259 {
260 int count = 0;
261 GSList *item = gtk_radio_button_group( m_radio );
262 while (item)
263 {
264 item = item->next;
265 count++;
266 }
267 return count;
268 }
269
270 int wxRadioBox::GetNumberOfRowsOrCols(void) const
271 {
272 return 1;
273 }
274
275 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
276 {
277 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
278 }
279