Further UNicode fixes.
[wxWidgets.git] / src / gtk / 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/defs.h"
16
17 #if wxUSE_CHOICE
18
19 #include "wx/choice.h"
20
21 #include "wx/gtk/private.h"
22
23 //-----------------------------------------------------------------------------
24 // idle system
25 //-----------------------------------------------------------------------------
26
27 extern void wxapp_install_idle_handler();
28 extern bool g_isIdle;
29
30 //-----------------------------------------------------------------------------
31 // data
32 //-----------------------------------------------------------------------------
33
34 extern bool g_blockEventsOnDrag;
35
36 //-----------------------------------------------------------------------------
37 // "activate"
38 //-----------------------------------------------------------------------------
39
40 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
41 {
42 if (g_isIdle)
43 wxapp_install_idle_handler();
44
45 if (!choice->m_hasVMT) return;
46
47 if (g_blockEventsOnDrag) return;
48
49 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
50 int n = choice->GetSelection();
51
52 event.SetInt( n );
53 event.SetString( choice->GetStringSelection() );
54 event.SetEventObject(choice);
55
56 if ( choice->HasClientObjectData() )
57 event.SetClientObject( choice->GetClientObject(n) );
58 else if ( choice->HasClientUntypedData() )
59 event.SetClientData( choice->GetClientData(n) );
60
61 choice->GetEventHandler()->ProcessEvent(event);
62 }
63
64 //-----------------------------------------------------------------------------
65 // wxChoice
66 //-----------------------------------------------------------------------------
67
68 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
69
70 wxChoice::wxChoice()
71 {
72 m_strings = (wxSortedArrayString *)NULL;
73 }
74
75 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
76 const wxPoint &pos, const wxSize &size,
77 int n, const wxString choices[],
78 long style, const wxValidator& validator, const wxString &name )
79 {
80 m_needParent = TRUE;
81 #if (GTK_MINOR_VERSION > 0)
82 m_acceptsFocus = TRUE;
83 #endif
84
85 if (!PreCreation( parent, pos, size ) ||
86 !CreateBase( parent, id, pos, size, style, validator, name ))
87 {
88 wxFAIL_MSG( wxT("wxChoice creation failed") );
89 return FALSE;
90 }
91
92 m_widget = gtk_option_menu_new();
93
94 if ( style & wxCB_SORT )
95 {
96 // if our m_strings != NULL, DoAppend() will check for it and insert
97 // items in the correct order
98 m_strings = new wxSortedArrayString;
99 }
100
101 GtkWidget *menu = gtk_menu_new();
102
103 for (int i = 0; i < n; i++)
104 {
105 GtkAppendHelper(menu, choices[i]);
106 }
107
108 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
109
110 m_parent->DoAddChild( this );
111
112 PostCreation();
113
114 SetFont( parent->GetFont() );
115
116 SetBestSize(size);
117
118 SetBackgroundColour( parent->GetBackgroundColour() );
119 SetForegroundColour( parent->GetForegroundColour() );
120
121 Show( TRUE );
122
123 return TRUE;
124 }
125
126 wxChoice::~wxChoice()
127 {
128 Clear();
129
130 delete m_strings;
131 }
132
133 int wxChoice::DoAppend( const wxString &item )
134 {
135 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
136
137 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
138
139 return GtkAppendHelper(menu, item);
140 }
141
142 void wxChoice::DoSetItemClientData( int n, void* clientData )
143 {
144 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
145
146 wxNode *node = m_clientList.Nth( n );
147 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
148
149 node->SetData( (wxObject*) clientData );
150 }
151
152 void* wxChoice::DoGetItemClientData( int n ) const
153 {
154 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
155
156 wxNode *node = m_clientList.Nth( n );
157 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
158
159 return node->Data();
160 }
161
162 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
163 {
164 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
165
166 wxNode *node = m_clientList.Nth( n );
167 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
168
169 wxClientData *cd = (wxClientData*) node->Data();
170 delete cd;
171
172 node->SetData( (wxObject*) clientData );
173 }
174
175 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
176 {
177 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
178
179 wxNode *node = m_clientList.Nth( n );
180 wxCHECK_MSG( node, (wxClientData *)NULL,
181 wxT("invalid index in wxChoice::DoGetItemClientObject") );
182
183 return (wxClientData*) node->Data();
184 }
185
186 void wxChoice::Clear()
187 {
188 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
189
190 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
191 GtkWidget *menu = gtk_menu_new();
192 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
193
194 if ( HasClientObjectData() )
195 {
196 // destroy the data (due to Robert's idea of using wxList<wxObject>
197 // and not wxList<wxClientData> we can't just say
198 // m_clientList.DeleteContents(TRUE) - this would crash!
199 wxNode *node = m_clientList.First();
200 while ( node )
201 {
202 delete (wxClientData *)node->Data();
203 node = node->Next();
204 }
205 }
206 m_clientList.Clear();
207
208 if ( m_strings )
209 m_strings->Clear();
210 }
211
212 void wxChoice::Delete( int n )
213 {
214 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
215
216 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
217 // in 2.0), hence this dump implementation - still better than nothing
218 int i,
219 count = GetCount();
220
221 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
222
223 wxArrayString items;
224 items.Alloc(count);
225 for ( i = 0; i < count; i++ )
226 {
227 if ( i != n )
228 items.Add(GetString(i));
229 }
230
231 Clear();
232
233 for ( i = 0; i < count - 1; i++ )
234 {
235 Append(items[i]);
236 }
237 }
238
239 int wxChoice::FindString( const wxString &string ) const
240 {
241 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
242
243 // If you read this code once and you think you understand
244 // it, then you are very wrong. Robert Roebling.
245
246 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
247 int count = 0;
248 GList *child = menu_shell->children;
249 while (child)
250 {
251 GtkBin *bin = GTK_BIN( child->data );
252 GtkLabel *label = (GtkLabel *) NULL;
253 if (bin->child)
254 label = GTK_LABEL(bin->child);
255 if (!label)
256 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
257
258 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
259
260 #ifdef __WXGTK20__
261 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
262 #else
263 wxString tmp( label->label );
264 #endif
265 if (string == tmp)
266 return count;
267
268 child = child->next;
269 count++;
270 }
271
272 return -1;
273 }
274
275 int wxChoice::GetSelection() const
276 {
277 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
278
279 #ifdef __WXGTK20__
280
281 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
282
283 #else
284 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
285 int count = 0;
286
287 GList *child = menu_shell->children;
288 while (child)
289 {
290 GtkBin *bin = GTK_BIN( child->data );
291 if (!bin->child) return count;
292 child = child->next;
293 count++;
294 }
295
296 return -1;
297 #endif
298 }
299
300 void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
301 {
302 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
303
304 wxFAIL_MSG(wxT("not implemented"));
305 }
306
307 wxString wxChoice::GetString( int n ) const
308 {
309 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
310
311 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
312 int count = 0;
313 GList *child = menu_shell->children;
314 while (child)
315 {
316 GtkBin *bin = GTK_BIN( child->data );
317 if (count == n)
318 {
319 GtkLabel *label = (GtkLabel *) NULL;
320 if (bin->child)
321 label = GTK_LABEL(bin->child);
322 if (!label)
323 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
324
325 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
326
327 #ifdef __WXGTK20__
328 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
329 #else
330 return wxString( label->label );
331 #endif
332 }
333 child = child->next;
334 count++;
335 }
336
337 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
338
339 return wxT("");
340 }
341
342 int wxChoice::GetCount() const
343 {
344 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
345
346 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
347 int count = 0;
348 GList *child = menu_shell->children;
349 while (child)
350 {
351 count++;
352 child = child->next;
353 }
354 return count;
355 }
356
357 void wxChoice::SetSelection( int n )
358 {
359 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
360
361 int tmp = n;
362 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
363 }
364
365 void wxChoice::ApplyWidgetStyle()
366 {
367 SetWidgetStyle();
368
369 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
370
371 gtk_widget_set_style( m_widget, m_widgetStyle );
372 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
373
374 GList *child = menu_shell->children;
375 while (child)
376 {
377 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
378
379 GtkBin *bin = GTK_BIN( child->data );
380 GtkWidget *label = (GtkWidget *) NULL;
381 if (bin->child)
382 label = bin->child;
383 if (!label)
384 label = BUTTON_CHILD(m_widget);
385
386 gtk_widget_set_style( label, m_widgetStyle );
387
388 child = child->next;
389 }
390 }
391
392 size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item)
393 {
394 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
395
396 size_t index;
397 if ( m_strings )
398 {
399 // sorted control, need to insert at the correct index
400 index = m_strings->Add(item);
401
402 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
403
404 if ( index )
405 {
406 m_clientList.Insert( m_clientList.Item(index - 1),
407 (wxObject*) NULL );
408 }
409 else
410 {
411 m_clientList.Insert( (wxObject*) NULL );
412 }
413 }
414 else
415 {
416 // normal control, just append
417 gtk_menu_append( GTK_MENU(menu), menu_item );
418
419 m_clientList.Append( (wxObject*) NULL );
420
421 // don't call wxChoice::GetCount() from here because it doesn't work
422 // if we're called from ctor (and GtkMenuShell is still NULL)
423 index = m_clientList.GetCount() - 1;
424 }
425
426 if (GTK_WIDGET_REALIZED(m_widget))
427 {
428 gtk_widget_realize( menu_item );
429 gtk_widget_realize( GTK_BIN(menu_item)->child );
430
431 if (m_widgetStyle) ApplyWidgetStyle();
432 }
433
434 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
435 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
436
437 gtk_widget_show( menu_item );
438
439 // return the index of the item in the control
440 return index;
441 }
442
443 wxSize wxChoice::DoGetBestSize() const
444 {
445 wxSize ret( wxControl::DoGetBestSize() );
446
447 // we know better our horizontal extent: it depends on the longest string
448 // we have
449 ret.x = 0;
450 if ( m_widget )
451 {
452 GdkFont *font = m_font.GetInternalFont();
453
454 wxCoord width;
455 size_t count = GetCount();
456 for ( size_t n = 0; n < count; n++ )
457 {
458 // FIXME GTK 2.0
459 width = (wxCoord)gdk_string_width(font, wxGTK_CONV( GetString(n) ) );
460 if ( width > ret.x )
461 ret.x = width;
462 }
463
464 // add extra for the choice "=" button
465
466 // VZ: I don't know how to get the right value, it seems to be in
467 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
468 // to it - maybe we can use gtk_option_menu_size_request() for this
469 // somehow?
470 //
471 // This default value works only for the default GTK+ theme (i.e.
472 // no theme at all) (FIXME)
473 static const int widthChoiceIndicator = 35;
474 ret.x += widthChoiceIndicator;
475 }
476
477 // but not less than the minimal width
478 if ( ret.x < 80 )
479 ret.x = 80;
480
481 ret.y = 16 + gdk_char_height(GET_STYLE_FONT( m_widget->style ), 'H' );
482
483 return ret;
484 }
485
486 #endif // wxUSE_CHOICE
487