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