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