Improve default size for wxGauge and wxChoice.
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #include "wx/arrstr.h"
21
22 #include "wx/gtk/private.h"
23
24 //-----------------------------------------------------------------------------
25 // idle system
26 //-----------------------------------------------------------------------------
27
28 extern void wxapp_install_idle_handler();
29 extern bool g_isIdle;
30
31 //-----------------------------------------------------------------------------
32 // data
33 //-----------------------------------------------------------------------------
34
35 extern bool g_blockEventsOnDrag;
36
37 //-----------------------------------------------------------------------------
38 // "activate"
39 //-----------------------------------------------------------------------------
40
41 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
42 {
43 if (g_isIdle)
44 wxapp_install_idle_handler();
45
46 if (!choice->m_hasVMT) return;
47
48 if (g_blockEventsOnDrag) return;
49
50 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
51 int n = choice->GetSelection();
52
53 event.SetInt( n );
54 event.SetString( choice->GetStringSelection() );
55 event.SetEventObject(choice);
56
57 if ( choice->HasClientObjectData() )
58 event.SetClientObject( choice->GetClientObject(n) );
59 else if ( choice->HasClientUntypedData() )
60 event.SetClientData( choice->GetClientData(n) );
61
62 choice->GetEventHandler()->ProcessEvent(event);
63 }
64
65 //-----------------------------------------------------------------------------
66 // wxChoice
67 //-----------------------------------------------------------------------------
68
69 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
70
71 wxChoice::wxChoice()
72 {
73 m_strings = (wxSortedArrayString *)NULL;
74 }
75
76 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
77 const wxPoint &pos, const wxSize &size,
78 const wxArrayString& choices,
79 long style, const wxValidator& validator,
80 const wxString &name )
81 {
82 wxCArrayString chs(choices);
83
84 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
85 style, validator, name );
86 }
87
88 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
89 const wxPoint &pos, const wxSize &size,
90 int n, const wxString choices[],
91 long style, const wxValidator& validator, const wxString &name )
92 {
93 m_needParent = TRUE;
94 #if (GTK_MINOR_VERSION > 0)
95 m_acceptsFocus = TRUE;
96 #endif
97
98 if (!PreCreation( parent, pos, size ) ||
99 !CreateBase( parent, id, pos, size, style, validator, name ))
100 {
101 wxFAIL_MSG( wxT("wxChoice creation failed") );
102 return FALSE;
103 }
104
105 m_widget = gtk_option_menu_new();
106
107 if ( style & wxCB_SORT )
108 {
109 // if our m_strings != NULL, DoAppend() will check for it and insert
110 // items in the correct order
111 m_strings = new wxSortedArrayString;
112 }
113
114 GtkWidget *menu = gtk_menu_new();
115
116 for (int i = 0; i < n; i++)
117 {
118 GtkAddHelper(menu, i, choices[i]);
119 }
120
121 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
122
123 m_parent->DoAddChild( this );
124
125 PostCreation(size);
126 SetBestSize(size); // need this too because this is a wxControlWithItems
127
128 return TRUE;
129 }
130
131 wxChoice::~wxChoice()
132 {
133 Clear();
134
135 delete m_strings;
136 }
137
138 int wxChoice::DoAppend( const wxString &item )
139 {
140 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
141
142 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
143
144 return GtkAddHelper(menu, GetCount(), item);
145 }
146
147 int wxChoice::DoInsert( const wxString &item, int pos )
148 {
149 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
150 wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
151
152 if (pos == GetCount())
153 return DoAppend(item);
154
155 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
156
157 return GtkAddHelper(menu, pos, item);
158 }
159
160 void wxChoice::DoSetItemClientData( int n, void* clientData )
161 {
162 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
163
164 wxList::compatibility_iterator node = m_clientList.Item( n );
165 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
166
167 node->SetData( (wxObject*) clientData );
168 }
169
170 void* wxChoice::DoGetItemClientData( int n ) const
171 {
172 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
173
174 wxList::compatibility_iterator node = m_clientList.Item( n );
175 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
176
177 return node->GetData();
178 }
179
180 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
181 {
182 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
183
184 wxList::compatibility_iterator node = m_clientList.Item( n );
185 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
186
187 // wxItemContainer already deletes data for us
188
189 node->SetData( (wxObject*) clientData );
190 }
191
192 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
193 {
194 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
195
196 wxList::compatibility_iterator node = m_clientList.Item( n );
197 wxCHECK_MSG( node, (wxClientData *)NULL,
198 wxT("invalid index in wxChoice::DoGetItemClientObject") );
199
200 return (wxClientData*) node->GetData();
201 }
202
203 void wxChoice::Clear()
204 {
205 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
206
207 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
208 GtkWidget *menu = gtk_menu_new();
209 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
210
211 if ( HasClientObjectData() )
212 {
213 // destroy the data (due to Robert's idea of using wxList<wxObject>
214 // and not wxList<wxClientData> we can't just say
215 // m_clientList.DeleteContents(TRUE) - this would crash!
216 wxList::compatibility_iterator node = m_clientList.GetFirst();
217 while ( node )
218 {
219 delete (wxClientData *)node->GetData();
220 node = node->GetNext();
221 }
222 }
223 m_clientList.Clear();
224
225 if ( m_strings )
226 m_strings->Clear();
227 }
228
229 void wxChoice::Delete( int n )
230 {
231 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
232
233 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
234 // in 2.0), hence this dumb implementation -- still better than nothing
235 int i,
236 count = GetCount();
237
238 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
239
240 const bool hasClientData = m_clientDataItemsType != wxClientData_None;
241 const bool hasObjectData = m_clientDataItemsType == wxClientData_Object;
242
243 wxList::compatibility_iterator node = m_clientList.GetFirst();
244
245 wxArrayString items;
246 wxArrayPtrVoid itemsData;
247 items.Alloc(count);
248 for ( i = 0; i < count; i++ )
249 {
250 if ( i != n )
251 {
252 items.Add(GetString(i));
253 if ( hasClientData )
254 {
255 // also save the client data
256 itemsData.Add(node->GetData());
257 }
258 }
259 else // need to delete the client object too
260 {
261 if ( hasObjectData )
262 {
263 delete (wxClientData *)node->GetData();
264 }
265 }
266
267 if ( hasClientData )
268 {
269 node = node->GetNext();
270 }
271 }
272
273 if ( hasObjectData )
274 {
275 // prevent Clear() from destroying all client data
276 m_clientDataItemsType = wxClientData_None;
277 }
278
279 Clear();
280
281 for ( i = 0; i < count - 1; i++ )
282 {
283 Append(items[i]);
284
285 if ( hasObjectData )
286 SetClientObject(i, (wxClientData *)itemsData[i]);
287 else if ( hasClientData )
288 SetClientData(i, itemsData[i]);
289 }
290 }
291
292 int wxChoice::FindString( const wxString &string ) const
293 {
294 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
295
296 // If you read this code once and you think you understand
297 // it, then you are very wrong. Robert Roebling.
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 GtkLabel *label = (GtkLabel *) NULL;
306 if (bin->child)
307 label = GTK_LABEL(bin->child);
308 if (!label)
309 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
310
311 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
312
313 #ifdef __WXGTK20__
314 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
315 #else
316 wxString tmp( label->label );
317 #endif
318 if (string == tmp)
319 return count;
320
321 child = child->next;
322 count++;
323 }
324
325 return -1;
326 }
327
328 int wxChoice::GetSelection() const
329 {
330 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
331
332 #ifdef __WXGTK20__
333
334 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
335
336 #else
337 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
338 int count = 0;
339
340 GList *child = menu_shell->children;
341 while (child)
342 {
343 GtkBin *bin = GTK_BIN( child->data );
344 if (!bin->child) return count;
345 child = child->next;
346 count++;
347 }
348
349 return -1;
350 #endif
351 }
352
353 void wxChoice::SetString( int n, const wxString& str )
354 {
355 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
356
357 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
358 int count = 0;
359 GList *child = menu_shell->children;
360 while (child)
361 {
362 GtkBin *bin = GTK_BIN( child->data );
363 if (count == n)
364 {
365 GtkLabel *label = (GtkLabel *) NULL;
366 if (bin->child)
367 label = GTK_LABEL(bin->child);
368 if (!label)
369 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
370
371 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
372
373 gtk_label_set_text( label, wxGTK_CONV( str ) );
374
375 return;
376 }
377 child = child->next;
378 count++;
379 }
380 }
381
382 wxString wxChoice::GetString( int n ) const
383 {
384 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
385
386 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
387 int count = 0;
388 GList *child = menu_shell->children;
389 while (child)
390 {
391 GtkBin *bin = GTK_BIN( child->data );
392 if (count == n)
393 {
394 GtkLabel *label = (GtkLabel *) NULL;
395 if (bin->child)
396 label = GTK_LABEL(bin->child);
397 if (!label)
398 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
399
400 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
401
402 #ifdef __WXGTK20__
403 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
404 #else
405 return wxString( label->label );
406 #endif
407 }
408 child = child->next;
409 count++;
410 }
411
412 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
413
414 return wxT("");
415 }
416
417 int wxChoice::GetCount() const
418 {
419 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
420
421 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
422 int count = 0;
423 GList *child = menu_shell->children;
424 while (child)
425 {
426 count++;
427 child = child->next;
428 }
429 return count;
430 }
431
432 void wxChoice::SetSelection( int n )
433 {
434 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
435
436 int tmp = n;
437 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
438 }
439
440 void wxChoice::ApplyWidgetStyle()
441 {
442 SetWidgetStyle();
443
444 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
445
446 gtk_widget_set_style( m_widget, m_widgetStyle );
447 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
448
449 GList *child = menu_shell->children;
450 while (child)
451 {
452 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
453
454 GtkBin *bin = GTK_BIN( child->data );
455 GtkWidget *label = (GtkWidget *) NULL;
456 if (bin->child)
457 label = bin->child;
458 if (!label)
459 label = BUTTON_CHILD(m_widget);
460
461 gtk_widget_set_style( label, m_widgetStyle );
462
463 child = child->next;
464 }
465 }
466
467 int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
468 {
469 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
470
471 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
472
473 size_t index;
474 if ( m_strings )
475 {
476 // sorted control, need to insert at the correct index
477 index = m_strings->Add(item);
478
479 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
480
481 if ( index )
482 {
483 m_clientList.Insert( m_clientList.Item(index - 1),
484 (wxObject*) NULL );
485 }
486 else
487 {
488 m_clientList.Insert( (wxObject*) NULL );
489 }
490 }
491 else
492 {
493 // don't call wxChoice::GetCount() from here because it doesn't work
494 // if we're called from ctor (and GtkMenuShell is still NULL)
495
496 // normal control, just append
497 if (pos == (int)m_clientList.GetCount())
498 {
499 gtk_menu_append( GTK_MENU(menu), menu_item );
500 m_clientList.Append( (wxObject*) NULL );
501 index = m_clientList.GetCount() - 1;
502 }
503 else
504 {
505 gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
506 m_clientList.Insert( pos, (wxObject*) NULL );
507 index = pos;
508 }
509 }
510
511 if (GTK_WIDGET_REALIZED(m_widget))
512 {
513 gtk_widget_realize( menu_item );
514 gtk_widget_realize( GTK_BIN(menu_item)->child );
515
516 if (m_widgetStyle) ApplyWidgetStyle();
517 }
518
519 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
520 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
521
522 gtk_widget_show( menu_item );
523
524 // return the index of the item in the control
525 return index;
526 }
527
528 wxSize wxChoice::DoGetBestSize() const
529 {
530 wxSize ret( wxControl::DoGetBestSize() );
531
532 // we know better our horizontal extent: it depends on the longest string
533 // we have
534 ret.x = 0;
535 if ( m_widget )
536 {
537 int width;
538 size_t count = GetCount();
539 for ( size_t n = 0; n < count; n++ )
540 {
541 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
542 if ( width > ret.x )
543 ret.x = width;
544 }
545
546 // add extra for the choice "=" button
547
548 // VZ: I don't know how to get the right value, it seems to be in
549 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
550 // to it - maybe we can use gtk_option_menu_size_request() for this
551 // somehow?
552 //
553 // This default value works only for the default GTK+ theme (i.e.
554 // no theme at all) (FIXME)
555 static const int widthChoiceIndicator = 35;
556 ret.x += widthChoiceIndicator;
557 }
558
559 // but not less than the minimal width
560 if ( ret.x < 80 )
561 ret.x = 80;
562
563 // If this request_size is called with no entries then
564 // the returned height is wrong. Give it a reasonable
565 // default value.
566 if (ret.y <= 18)
567 ret.y = 8 + GetCharHeight();
568
569 return ret;
570 }
571
572 bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
573 {
574 #ifdef __WXGTK20__
575 return GTK_BUTTON(m_widget)->event_window;
576 #else
577 return (window == m_widget->window);
578 #endif
579 }
580
581 // static
582 wxVisualAttributes
583 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
584 {
585 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new);
586 }
587
588
589 #endif // wxUSE_CHOICE
590