1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/choice.h"
15 #include "wx/arrstr.h"
17 #include "wx/gtk/private.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern void wxapp_install_idle_handler();
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern bool g_blockEventsOnDrag
;
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
37 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
40 wxapp_install_idle_handler();
42 if (!choice
->m_hasVMT
) return;
44 if (g_blockEventsOnDrag
) return;
46 int selection
= wxNOT_FOUND
;
49 selection
= gtk_option_menu_get_history( GTK_OPTION_MENU(choice
->GetHandle()) );
51 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(choice
->GetHandle()) ) );
54 GList
*child
= menu_shell
->children
;
57 GtkBin
*bin
= GTK_BIN( child
->data
);
67 choice
->m_selection_hack
= selection
;
69 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
70 int n
= choice
->GetSelection();
73 event
.SetString( choice
->GetStringSelection() );
74 event
.SetEventObject(choice
);
76 if ( choice
->HasClientObjectData() )
77 event
.SetClientObject( choice
->GetClientObject(n
) );
78 else if ( choice
->HasClientUntypedData() )
79 event
.SetClientData( choice
->GetClientData(n
) );
81 choice
->GetEventHandler()->ProcessEvent(event
);
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
93 m_strings
= (wxSortedArrayString
*)NULL
;
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
)
102 wxCArrayString
chs(choices
);
104 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
105 style
, validator
, name
);
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
)
114 #if (GTK_MINOR_VERSION > 0)
115 m_acceptsFocus
= true;
118 if (!PreCreation( parent
, pos
, size
) ||
119 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
121 wxFAIL_MSG( wxT("wxChoice creation failed") );
125 m_widget
= gtk_option_menu_new();
127 if ( style
& wxCB_SORT
)
129 // if our m_strings != NULL, DoAppend() will check for it and insert
130 // items in the correct order
131 m_strings
= new wxSortedArrayString
;
134 // begin with no selection
135 m_selection_hack
= wxNOT_FOUND
;
137 GtkWidget
*menu
= gtk_menu_new();
139 for (int i
= 0; i
< n
; i
++)
141 GtkAddHelper(menu
, i
, choices
[i
]);
144 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
146 m_parent
->DoAddChild( this );
149 SetBestSize(size
); // need this too because this is a wxControlWithItems
154 wxChoice::~wxChoice()
161 int wxChoice::DoAppend( const wxString
&item
)
163 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
165 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
167 return GtkAddHelper(menu
, GetCount(), item
);
170 int wxChoice::DoInsert( const wxString
&item
, int pos
)
172 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
173 wxCHECK_MSG( (pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
175 if (pos
== GetCount())
176 return DoAppend(item
);
178 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
180 // if the item to insert is at or before the selection, and the selection is valid
181 if ((pos
<= m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
183 // move the selection forward one
187 return GtkAddHelper(menu
, pos
, item
);
190 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
192 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
194 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
195 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
197 node
->SetData( (wxObject
*) clientData
);
200 void* wxChoice::DoGetItemClientData( int n
) const
202 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
204 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
205 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
207 return node
->GetData();
210 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
212 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
214 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
215 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
217 // wxItemContainer already deletes data for us
219 node
->SetData( (wxObject
*) clientData
);
222 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
224 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
226 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
227 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
228 wxT("invalid index in wxChoice::DoGetItemClientObject") );
230 return (wxClientData
*) node
->GetData();
233 void wxChoice::Clear()
235 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
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
);
241 if ( HasClientObjectData() )
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();
249 delete (wxClientData
*)node
->GetData();
250 node
= node
->GetNext();
253 m_clientList
.Clear();
258 // begin with no selection
259 m_selection_hack
= wxNOT_FOUND
;
262 void wxChoice::Delete( int n
)
264 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
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
271 wxCHECK_RET( n
>= 0 && n
< count
, _T("invalid index in wxChoice::Delete") );
273 // if the item to delete is before the selection, and the selection is valid
274 if ((n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
276 // move the selection back one
279 else if (n
== m_selection_hack
)
281 // invalidate the selection
282 m_selection_hack
= wxNOT_FOUND
;
285 const bool hasClientData
= m_clientDataItemsType
!= wxClientData_None
;
286 const bool hasObjectData
= m_clientDataItemsType
== wxClientData_Object
;
288 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
291 wxArrayPtrVoid itemsData
;
293 for ( i
= 0; i
< count
; i
++ )
297 items
.Add(GetString(i
));
300 // also save the client data
301 itemsData
.Add(node
->GetData());
304 else // need to delete the client object too
308 delete (wxClientData
*)node
->GetData();
314 node
= node
->GetNext();
320 // prevent Clear() from destroying all client data
321 m_clientDataItemsType
= wxClientData_None
;
326 for ( i
= 0; i
< count
- 1; i
++ )
331 SetClientObject(i
, (wxClientData
*)itemsData
[i
]);
332 else if ( hasClientData
)
333 SetClientData(i
, itemsData
[i
]);
337 int wxChoice::FindString( const wxString
&string
) const
339 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
341 // If you read this code once and you think you understand
342 // it, then you are very wrong. Robert Roebling.
344 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
346 GList
*child
= menu_shell
->children
;
349 GtkBin
*bin
= GTK_BIN( child
->data
);
350 GtkLabel
*label
= (GtkLabel
*) NULL
;
352 label
= GTK_LABEL(bin
->child
);
354 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
356 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
359 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
361 wxString
tmp( label
->label
);
373 int wxChoice::GetSelection() const
375 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
377 return m_selection_hack
;
381 void wxChoice::SetString( int n
, const wxString
& str
)
383 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
385 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
387 GList
*child
= menu_shell
->children
;
390 GtkBin
*bin
= GTK_BIN( child
->data
);
393 GtkLabel
*label
= (GtkLabel
*) NULL
;
395 label
= GTK_LABEL(bin
->child
);
397 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
399 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
401 gtk_label_set_text( label
, wxGTK_CONV( str
) );
410 wxString
wxChoice::GetString( int n
) const
412 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
414 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
416 GList
*child
= menu_shell
->children
;
419 GtkBin
*bin
= GTK_BIN( child
->data
);
422 GtkLabel
*label
= (GtkLabel
*) NULL
;
424 label
= GTK_LABEL(bin
->child
);
426 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
428 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
431 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
433 return wxString( label
->label
);
440 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
442 return wxEmptyString
;
445 int wxChoice::GetCount() const
447 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
449 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
451 GList
*child
= menu_shell
->children
;
460 void wxChoice::SetSelection( int n
)
462 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
465 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
467 // set the local selection variable manually
468 if ((n
>= 0) && (n
< GetCount()))
470 // a valid selection has been made
471 m_selection_hack
= n
;
473 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
475 // invalidates the selection if there are no items
476 // or if it is specifically set to wxNOT_FOUND
477 m_selection_hack
= wxNOT_FOUND
;
481 // this selects the first item by default if the selection is out of bounds
482 m_selection_hack
= 0;
486 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
488 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
490 gtk_widget_modify_style( m_widget
, style
);
491 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
493 GList
*child
= menu_shell
->children
;
496 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
498 GtkBin
*bin
= GTK_BIN( child
->data
);
499 GtkWidget
*label
= (GtkWidget
*) NULL
;
503 label
= BUTTON_CHILD(m_widget
);
505 gtk_widget_modify_style( label
, style
);
511 int wxChoice::GtkAddHelper(GtkWidget
*menu
, int pos
, const wxString
& item
)
513 wxCHECK_MSG((pos
>=0) && (pos
<=(int)m_clientList
.GetCount()), -1, wxT("invalid index"));
515 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
520 // sorted control, need to insert at the correct index
521 index
= m_strings
->Add(item
);
523 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
527 m_clientList
.Insert( m_clientList
.Item(index
- 1),
532 m_clientList
.Insert( (wxObject
*) NULL
);
537 // don't call wxChoice::GetCount() from here because it doesn't work
538 // if we're called from ctor (and GtkMenuShell is still NULL)
540 // normal control, just append
541 if (pos
== (int)m_clientList
.GetCount())
543 gtk_menu_append( GTK_MENU(menu
), menu_item
);
544 m_clientList
.Append( (wxObject
*) NULL
);
545 index
= m_clientList
.GetCount() - 1;
549 gtk_menu_insert( GTK_MENU(menu
), menu_item
, pos
);
550 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
555 if (GTK_WIDGET_REALIZED(m_widget
))
557 gtk_widget_realize( menu_item
);
558 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
563 // The best size of a wxChoice should probably
564 // be changed everytime the control has been
565 // changed, but at least after adding an item
566 // it has to change. Adapted from Matt Ownby.
567 InvalidateBestSize();
569 gtk_signal_connect_after( GTK_OBJECT( menu_item
), "activate",
570 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
572 gtk_widget_show( menu_item
);
574 // return the index of the item in the control
578 wxSize
wxChoice::DoGetBestSize() const
580 wxSize
ret( wxControl::DoGetBestSize() );
582 // we know better our horizontal extent: it depends on the longest string
588 size_t count
= GetCount();
589 for ( size_t n
= 0; n
< count
; n
++ )
591 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
596 // add extra for the choice "=" button
598 // VZ: I don't know how to get the right value, it seems to be in
599 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
600 // to it - maybe we can use gtk_option_menu_size_request() for this
603 // This default value works only for the default GTK+ theme (i.e.
604 // no theme at all) (FIXME)
605 static const int widthChoiceIndicator
= 35;
606 ret
.x
+= widthChoiceIndicator
;
609 // but not less than the minimal width
613 // If this request_size is called with no entries then
614 // the returned height is wrong. Give it a reasonable
617 ret
.y
= 8 + GetCharHeight();
623 bool wxChoice::IsOwnGtkWindow( GdkWindow
*window
)
626 return GTK_BUTTON(m_widget
)->event_window
;
628 return (window
== m_widget
->window
);
634 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
636 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
640 #endif // wxUSE_CHOICE