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