]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/choice.cpp
Ensure that the wxNativeFontInfo internal data is copied when the
[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 // wxItemContainer already deletes data for us
170
171 node->SetData( (wxObject*) clientData );
172 }
173
174 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
175 {
176 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
177
178 wxNode *node = m_clientList.Nth( n );
179 wxCHECK_MSG( node, (wxClientData *)NULL,
180 wxT("invalid index in wxChoice::DoGetItemClientObject") );
181
182 return (wxClientData*) node->Data();
183 }
184
185 void wxChoice::Clear()
186 {
187 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
188
189 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
190 GtkWidget *menu = gtk_menu_new();
191 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
192
193 if ( HasClientObjectData() )
194 {
195 // destroy the data (due to Robert's idea of using wxList<wxObject>
196 // and not wxList<wxClientData> we can't just say
197 // m_clientList.DeleteContents(TRUE) - this would crash!
198 wxNode *node = m_clientList.First();
199 while ( node )
200 {
201 delete (wxClientData *)node->Data();
202 node = node->Next();
203 }
204 }
205 m_clientList.Clear();
206
207 if ( m_strings )
208 m_strings->Clear();
209 }
210
211 void wxChoice::Delete( int n )
212 {
213 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
214
215 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
216 // in 2.0), hence this dump implementation - still better than nothing
217 int i,
218 count = GetCount();
219
220 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
221
222 wxArrayString items;
223 items.Alloc(count);
224 for ( i = 0; i < count; i++ )
225 {
226 if ( i != n )
227 items.Add(GetString(i));
228 }
229
230 Clear();
231
232 for ( i = 0; i < count - 1; i++ )
233 {
234 Append(items[i]);
235 }
236 }
237
238 int wxChoice::FindString( const wxString &string ) const
239 {
240 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
241
242 // If you read this code once and you think you understand
243 // it, then you are very wrong. Robert Roebling.
244
245 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
246 int count = 0;
247 GList *child = menu_shell->children;
248 while (child)
249 {
250 GtkBin *bin = GTK_BIN( child->data );
251 GtkLabel *label = (GtkLabel *) NULL;
252 if (bin->child)
253 label = GTK_LABEL(bin->child);
254 if (!label)
255 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
256
257 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
258
259 #ifdef __WXGTK20__
260 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
261 #else
262 wxString tmp( label->label );
263 #endif
264 if (string == tmp)
265 return count;
266
267 child = child->next;
268 count++;
269 }
270
271 return -1;
272 }
273
274 int wxChoice::GetSelection() const
275 {
276 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
277
278 #ifdef __WXGTK20__
279
280 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
281
282 #else
283 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
284 int count = 0;
285
286 GList *child = menu_shell->children;
287 while (child)
288 {
289 GtkBin *bin = GTK_BIN( child->data );
290 if (!bin->child) return count;
291 child = child->next;
292 count++;
293 }
294
295 return -1;
296 #endif
297 }
298
299 void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
300 {
301 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
302
303 wxFAIL_MSG(wxT("not implemented"));
304 }
305
306 wxString wxChoice::GetString( int n ) const
307 {
308 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
309
310 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
311 int count = 0;
312 GList *child = menu_shell->children;
313 while (child)
314 {
315 GtkBin *bin = GTK_BIN( child->data );
316 if (count == n)
317 {
318 GtkLabel *label = (GtkLabel *) NULL;
319 if (bin->child)
320 label = GTK_LABEL(bin->child);
321 if (!label)
322 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
323
324 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
325
326 #ifdef __WXGTK20__
327 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
328 #else
329 return wxString( label->label );
330 #endif
331 }
332 child = child->next;
333 count++;
334 }
335
336 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
337
338 return wxT("");
339 }
340
341 int wxChoice::GetCount() const
342 {
343 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
344
345 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
346 int count = 0;
347 GList *child = menu_shell->children;
348 while (child)
349 {
350 count++;
351 child = child->next;
352 }
353 return count;
354 }
355
356 void wxChoice::SetSelection( int n )
357 {
358 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
359
360 int tmp = n;
361 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
362 }
363
364 void wxChoice::ApplyWidgetStyle()
365 {
366 SetWidgetStyle();
367
368 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
369
370 gtk_widget_set_style( m_widget, m_widgetStyle );
371 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
372
373 GList *child = menu_shell->children;
374 while (child)
375 {
376 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
377
378 GtkBin *bin = GTK_BIN( child->data );
379 GtkWidget *label = (GtkWidget *) NULL;
380 if (bin->child)
381 label = bin->child;
382 if (!label)
383 label = BUTTON_CHILD(m_widget);
384
385 gtk_widget_set_style( label, m_widgetStyle );
386
387 child = child->next;
388 }
389 }
390
391 size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item)
392 {
393 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
394
395 size_t index;
396 if ( m_strings )
397 {
398 // sorted control, need to insert at the correct index
399 index = m_strings->Add(item);
400
401 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
402
403 if ( index )
404 {
405 m_clientList.Insert( m_clientList.Item(index - 1),
406 (wxObject*) NULL );
407 }
408 else
409 {
410 m_clientList.Insert( (wxObject*) NULL );
411 }
412 }
413 else
414 {
415 // normal control, just append
416 gtk_menu_append( GTK_MENU(menu), menu_item );
417
418 m_clientList.Append( (wxObject*) NULL );
419
420 // don't call wxChoice::GetCount() from here because it doesn't work
421 // if we're called from ctor (and GtkMenuShell is still NULL)
422 index = m_clientList.GetCount() - 1;
423 }
424
425 if (GTK_WIDGET_REALIZED(m_widget))
426 {
427 gtk_widget_realize( menu_item );
428 gtk_widget_realize( GTK_BIN(menu_item)->child );
429
430 if (m_widgetStyle) ApplyWidgetStyle();
431 }
432
433 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
434 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
435
436 gtk_widget_show( menu_item );
437
438 // return the index of the item in the control
439 return index;
440 }
441
442 wxSize wxChoice::DoGetBestSize() const
443 {
444 wxSize ret( wxControl::DoGetBestSize() );
445
446 // we know better our horizontal extent: it depends on the longest string
447 // we have
448 ret.x = 0;
449 if ( m_widget )
450 {
451 GdkFont *font = m_font.GetInternalFont();
452
453 wxCoord width;
454 size_t count = GetCount();
455 for ( size_t n = 0; n < count; n++ )
456 {
457 // FIXME GTK 2.0
458 width = (wxCoord)gdk_string_width(font, wxGTK_CONV( GetString(n) ) );
459 if ( width > ret.x )
460 ret.x = width;
461 }
462
463 // add extra for the choice "=" button
464
465 // VZ: I don't know how to get the right value, it seems to be in
466 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
467 // to it - maybe we can use gtk_option_menu_size_request() for this
468 // somehow?
469 //
470 // This default value works only for the default GTK+ theme (i.e.
471 // no theme at all) (FIXME)
472 static const int widthChoiceIndicator = 35;
473 ret.x += widthChoiceIndicator;
474 }
475
476 // but not less than the minimal width
477 if ( ret.x < 80 )
478 ret.x = 80;
479
480 ret.y = 16 + gdk_char_height(GET_STYLE_FONT( m_widget->style ), 'H' );
481
482 return ret;
483 }
484
485 #endif // wxUSE_CHOICE
486