]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/choice.cpp
Fixed wxZlibOutputStream::Sync
[wxWidgets.git] / src / gtk1 / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choice.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "choice.h"
13 #endif
14
15 #include "wx/choice.h"
16
17 #if wxUSE_CHOICE
18
19 #include "gdk/gdk.h"
20 #include "gtk/gtk.h"
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34
35 //-----------------------------------------------------------------------------
36 // "activate"
37 //-----------------------------------------------------------------------------
38
39 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
40 {
41 if (g_isIdle)
42 wxapp_install_idle_handler();
43
44 if (!choice->m_hasVMT) return;
45
46 if (g_blockEventsOnDrag) return;
47
48 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
49 event.SetInt( choice->GetSelection() );
50 event.SetString( choice->GetStringSelection() );
51 event.SetEventObject(choice);
52 choice->GetEventHandler()->ProcessEvent(event);
53 }
54
55 //-----------------------------------------------------------------------------
56 // wxChoice
57 //-----------------------------------------------------------------------------
58
59 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
60
61 wxChoice::wxChoice()
62 {
63 }
64
65 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
66 const wxPoint &pos, const wxSize &size,
67 int n, const wxString choices[],
68 long style, const wxValidator& validator, const wxString &name )
69 {
70 m_needParent = TRUE;
71 #if (GTK_MINOR_VERSION > 0)
72 m_acceptsFocus = TRUE;
73 #endif
74
75 if (!PreCreation( parent, pos, size ) ||
76 !CreateBase( parent, id, pos, size, style, validator, name ))
77 {
78 wxFAIL_MSG( _T("wxChoice creation failed") );
79 return FALSE;
80 }
81
82 m_widget = gtk_option_menu_new();
83
84 wxSize newSize(size);
85 if (newSize.x == -1)
86 newSize.x = 80;
87 if (newSize.y == -1)
88 newSize.y = 26;
89 SetSize( newSize.x, newSize.y );
90
91 GtkWidget *menu = gtk_menu_new();
92
93 for (int i = 0; i < n; i++)
94 {
95 m_clientDataList.Append( (wxObject*) NULL );
96 m_clientObjectList.Append( (wxObject*) NULL );
97
98 GtkWidget *item = gtk_menu_item_new_with_label( choices[i].mbc_str() );
99 gtk_menu_append( GTK_MENU(menu), item );
100
101 gtk_widget_show( item );
102
103 gtk_signal_connect( GTK_OBJECT( item ), "activate",
104 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
105 }
106 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
107
108 m_parent->DoAddChild( this );
109
110 PostCreation();
111
112 SetBackgroundColour( parent->GetBackgroundColour() );
113 SetForegroundColour( parent->GetForegroundColour() );
114 SetFont( parent->GetFont() );
115
116 Show( TRUE );
117
118 return TRUE;
119 }
120
121 wxChoice::~wxChoice()
122 {
123 Clear();
124 }
125
126 void wxChoice::AppendCommon( const wxString &item )
127 {
128 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
129
130 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
131 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
132
133 gtk_menu_append( GTK_MENU(menu), menu_item );
134
135 if (GTK_WIDGET_REALIZED(m_widget))
136 {
137 gtk_widget_realize( menu_item );
138 gtk_widget_realize( GTK_BIN(menu_item)->child );
139
140 if (m_widgetStyle) ApplyWidgetStyle();
141 }
142
143 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
144 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
145
146 gtk_widget_show( menu_item );
147 }
148
149 void wxChoice::Append( const wxString &item )
150 {
151 m_clientDataList.Append( (wxObject*) NULL );
152 m_clientObjectList.Append( (wxObject*) NULL );
153
154 AppendCommon( item );
155 }
156
157 void wxChoice::Append( const wxString &item, void *clientData )
158 {
159 m_clientDataList.Append( (wxObject*) clientData );
160 m_clientObjectList.Append( (wxObject*) NULL );
161
162 AppendCommon( item );
163 }
164
165 void wxChoice::Append( const wxString &item, wxClientData *clientData )
166 {
167 m_clientObjectList.Append( (wxObject*) clientData );
168 m_clientDataList.Append( (wxObject*) NULL );
169
170 AppendCommon( item );
171 }
172
173 void wxChoice::SetClientData( int n, void* clientData )
174 {
175 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
176
177 wxNode *node = m_clientDataList.Nth( n );
178 if (!node) return;
179
180 node->SetData( (wxObject*) clientData );
181 }
182
183 void* wxChoice::GetClientData( int n )
184 {
185 wxCHECK_MSG( m_widget != NULL, NULL, _T("invalid combobox") );
186
187 wxNode *node = m_clientDataList.Nth( n );
188 if (!node) return NULL;
189
190 return node->Data();
191 }
192
193 void wxChoice::SetClientObject( int n, wxClientData* clientData )
194 {
195 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
196
197 wxNode *node = m_clientObjectList.Nth( n );
198 if (!node) return;
199
200 wxClientData *cd = (wxClientData*) node->Data();
201 if (cd) delete cd;
202
203 node->SetData( (wxObject*) clientData );
204 }
205
206 wxClientData* wxChoice::GetClientObject( int n )
207 {
208 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, _T("invalid combobox") );
209
210 wxNode *node = m_clientObjectList.Nth( n );
211 if (!node) return (wxClientData*) NULL;
212
213 return (wxClientData*) node->Data();
214 }
215
216 void wxChoice::Clear()
217 {
218 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
219
220 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
221 GtkWidget *menu = gtk_menu_new();
222 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
223
224 wxNode *node = m_clientObjectList.First();
225 while (node)
226 {
227 wxClientData *cd = (wxClientData*)node->Data();
228 if (cd) delete cd;
229 node = node->Next();
230 }
231 m_clientObjectList.Clear();
232
233 m_clientDataList.Clear();
234 }
235
236 void wxChoice::Delete( int WXUNUSED(n) )
237 {
238 wxFAIL_MSG( _T("wxChoice:Delete not implemented") );
239 }
240
241 int wxChoice::FindString( const wxString &string ) const
242 {
243 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid choice") );
244
245 // If you read this code once and you think you understand
246 // it, then you are very wrong. Robert Roebling.
247
248 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
249 int count = 0;
250 GList *child = menu_shell->children;
251 while (child)
252 {
253 GtkBin *bin = GTK_BIN( child->data );
254 GtkLabel *label = (GtkLabel *) NULL;
255 if (bin->child) label = GTK_LABEL(bin->child);
256 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
257
258 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
259
260 if (string == wxString(label->label,*wxConvCurrent))
261 return count;
262
263 child = child->next;
264 count++;
265 }
266
267 return -1;
268 }
269
270 int wxChoice::GetColumns() const
271 {
272 return 1;
273 }
274
275 int wxChoice::GetSelection()
276 {
277 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid choice") );
278
279 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
280 int count = 0;
281 GList *child = menu_shell->children;
282 while (child)
283 {
284 GtkBin *bin = GTK_BIN( child->data );
285 if (!bin->child) return count;
286 child = child->next;
287 count++;
288 }
289
290 return -1;
291 }
292
293 wxString wxChoice::GetString( int n ) const
294 {
295 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
296
297 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
298 int count = 0;
299 GList *child = menu_shell->children;
300 while (child)
301 {
302 GtkBin *bin = GTK_BIN( child->data );
303 if (count == n)
304 {
305 GtkLabel *label = (GtkLabel *) NULL;
306 if (bin->child) label = GTK_LABEL(bin->child);
307 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
308
309 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
310
311 return wxString(label->label,*wxConvCurrent);
312 }
313 child = child->next;
314 count++;
315 }
316
317 wxFAIL_MSG( _T("wxChoice: invalid index in GetString()") );
318
319 return _T("");
320 }
321
322 wxString wxChoice::GetStringSelection() const
323 {
324 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
325
326 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
327
328 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
329
330 return wxString(label->label,*wxConvCurrent);
331 }
332
333 int wxChoice::Number() const
334 {
335 wxCHECK_MSG( m_widget != NULL, 0, _T("invalid choice") );
336
337 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
338 int count = 0;
339 GList *child = menu_shell->children;
340 while (child)
341 {
342 count++;
343 child = child->next;
344 }
345 return count;
346 }
347
348 void wxChoice::SetColumns( int WXUNUSED(n) )
349 {
350 }
351
352 void wxChoice::SetSelection( int n )
353 {
354 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
355
356 int tmp = n;
357 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
358 }
359
360 void wxChoice::SetStringSelection( const wxString &string )
361 {
362 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
363
364 int n = FindString( string );
365 if (n != -1) SetSelection( n );
366 }
367
368 void wxChoice::DisableEvents()
369 {
370 /*
371 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
372 GList *child = menu_shell->children;
373 while (child)
374 {
375 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
376 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
377
378 child = child->next;
379 }
380 */
381 }
382
383 void wxChoice::EnableEvents()
384 {
385 /*
386 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
387 GList *child = menu_shell->children;
388 while (child)
389 {
390 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
391 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
392
393 child = child->next;
394 }
395 */
396 }
397
398 void wxChoice::ApplyWidgetStyle()
399 {
400 SetWidgetStyle();
401
402 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
403
404 gtk_widget_set_style( m_widget, m_widgetStyle );
405 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
406
407 GList *child = menu_shell->children;
408 while (child)
409 {
410 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
411
412 GtkBin *bin = GTK_BIN( child->data );
413 GtkWidget *label = (GtkWidget *) NULL;
414 if (bin->child) label = bin->child;
415 if (!label) label = GTK_BUTTON(m_widget)->child;
416
417 gtk_widget_set_style( label, m_widgetStyle );
418
419 child = child->next;
420 }
421 }
422
423 #endif