(1) Denis Pershin's patch for wxGTK (memory leaks corrections)
[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 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,
64 const wxString &name )
65 {
66 Create( parent, id, title, pos, size, n, choices, majorDim, style, name );
67 };
68
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,
73 const wxString &name )
74 {
75 m_alreadySent = FALSE;
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) );
94
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
129 bool wxRadioBox::Show( bool show )
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
144 int wxRadioBox::FindString( const wxString &s ) const
145 {
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;
160 };
161
162 void wxRadioBox::SetSelection( int n )
163 {
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 );
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
187 wxString wxRadioBox::GetString( int n ) const
188 {
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 );
199 };
200
201 wxString wxRadioBox::GetLabel(void) const
202 {
203 return wxControl::GetLabel();
204 };
205
206 void wxRadioBox::SetLabel( const wxString& WXUNUSED(label) )
207 {
208 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
209 };
210
211 void wxRadioBox::SetLabel( int WXUNUSED(item), const wxString& WXUNUSED(label) )
212 {
213 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
214 };
215
216 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
217 {
218 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
219 };
220
221 wxString wxRadioBox::GetLabel( int WXUNUSED(item) ) const
222 {
223 wxFAIL_MSG("wxRadioBox::GetLabel not implemented.");
224 return "";
225 };
226
227 void wxRadioBox::Enable( bool WXUNUSED(enable) )
228 {
229 wxFAIL_MSG("wxRadioBox::Enable not implemented.");
230 };
231
232 void wxRadioBox::Enable( int WXUNUSED(item), bool WXUNUSED(enable) )
233 {
234 wxFAIL_MSG("wxRadioBox::Enable not implemented.");
235 };
236
237 void wxRadioBox::Show( int WXUNUSED(item), bool WXUNUSED(show) )
238 {
239 wxFAIL_MSG("wxRadioBox::Show not implemented.");
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
258 bool wxRadioBox::SetStringSelection( const wxString&s )
259 {
260 int res = FindString( s );
261 if (res == -1) return FALSE;
262 SetSelection( res );
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
283 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
284 {
285 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
286 };
287