Fixed Fontdialog
[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 };
209
210 void wxRadioBox::SetLabel( int WXUNUSED(item), const wxString& WXUNUSED(label) )
211 {
212 };
213
214 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
215 {
216 };
217
218 wxString wxRadioBox::GetLabel( int WXUNUSED(item) ) const
219 {
220 return "";
221 };
222
223 void wxRadioBox::Enable( bool WXUNUSED(enable) )
224 {
225 };
226
227 void wxRadioBox::Enable( int WXUNUSED(item), bool WXUNUSED(enable) )
228 {
229 };
230
231 void wxRadioBox::Show( int WXUNUSED(item), bool WXUNUSED(show) )
232 {
233 };
234
235 wxString wxRadioBox::GetStringSelection(void) const
236 {
237 GSList *item = gtk_radio_button_group( m_radio );
238 while (item)
239 {
240 GtkButton *button = GTK_BUTTON( item->data );
241 if (GTK_TOGGLE_BUTTON(button)->active)
242 {
243 GtkLabel *label = GTK_LABEL( button->child );
244 return label->label;
245 };
246 item = item->next;
247 };
248 return "";
249 };
250
251 bool wxRadioBox::SetStringSelection( const wxString&s )
252 {
253 int res = FindString( s );
254 if (res == -1) return FALSE;
255 SetSelection( res );
256 return TRUE;
257 };
258
259 int wxRadioBox::Number(void) const
260 {
261 int count = 0;
262 GSList *item = gtk_radio_button_group( m_radio );
263 while (item)
264 {
265 item = item->next;
266 count++;
267 };
268 return count;
269 };
270
271 int wxRadioBox::GetNumberOfRowsOrCols(void) const
272 {
273 return 1;
274 };
275
276 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
277 {
278 };
279