Small distrib updates,
[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 wxFAIL_MSG( _T("wxChoice: no selection") );
291
292 return -1;
293 }
294
295 wxString wxChoice::GetString( int n ) const
296 {
297 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
298
299 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
300 int count = 0;
301 GList *child = menu_shell->children;
302 while (child)
303 {
304 GtkBin *bin = GTK_BIN( child->data );
305 if (count == n)
306 {
307 GtkLabel *label = (GtkLabel *) NULL;
308 if (bin->child) label = GTK_LABEL(bin->child);
309 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
310
311 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
312
313 return wxString(label->label,*wxConvCurrent);
314 }
315 child = child->next;
316 count++;
317 }
318
319 wxFAIL_MSG( _T("wxChoice: invalid index in GetString()") );
320
321 return _T("");
322 }
323
324 wxString wxChoice::GetStringSelection() const
325 {
326 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
327
328 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
329
330 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
331
332 return wxString(label->label,*wxConvCurrent);
333 }
334
335 int wxChoice::Number() const
336 {
337 wxCHECK_MSG( m_widget != NULL, 0, _T("invalid choice") );
338
339 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
340 int count = 0;
341 GList *child = menu_shell->children;
342 while (child)
343 {
344 count++;
345 child = child->next;
346 }
347 return count;
348 }
349
350 void wxChoice::SetColumns( int WXUNUSED(n) )
351 {
352 }
353
354 void wxChoice::SetSelection( int n )
355 {
356 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
357
358 int tmp = n;
359 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
360 }
361
362 void wxChoice::SetStringSelection( const wxString &string )
363 {
364 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
365
366 int n = FindString( string );
367 if (n != -1) SetSelection( n );
368 }
369
370 void wxChoice::DisableEvents()
371 {
372 /*
373 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
374 GList *child = menu_shell->children;
375 while (child)
376 {
377 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
378 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
379
380 child = child->next;
381 }
382 */
383 }
384
385 void wxChoice::EnableEvents()
386 {
387 /*
388 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
389 GList *child = menu_shell->children;
390 while (child)
391 {
392 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
393 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
394
395 child = child->next;
396 }
397 */
398 }
399
400 void wxChoice::ApplyWidgetStyle()
401 {
402 SetWidgetStyle();
403
404 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
405
406 gtk_widget_set_style( m_widget, m_widgetStyle );
407 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
408
409 GList *child = menu_shell->children;
410 while (child)
411 {
412 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
413
414 GtkBin *bin = GTK_BIN( child->data );
415 GtkWidget *label = (GtkWidget *) NULL;
416 if (bin->child) label = bin->child;
417 if (!label) label = GTK_BUTTON(m_widget)->child;
418
419 gtk_widget_set_style( label, m_widgetStyle );
420
421 child = child->next;
422 }
423 }
424
425 #endif