1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/choice.cpp
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 // FIXME: We use GtkOptionMenu which has been deprecated since GTK+ 2.3.0 in
18 // favour of GtkComboBox.
19 // Later use GtkComboBox if GTK+ runtime version is new enough.
20 #include <gtk/gtkversion.h>
21 #if defined(GTK_DISABLE_DEPRECATED) && GTK_CHECK_VERSION(2,3,0)
22 #undef GTK_DISABLE_DEPRECATED
25 #include "wx/gtk/private.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
45 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
48 wxapp_install_idle_handler();
50 if (!choice
->m_hasVMT
) return;
52 if (g_blockEventsOnDrag
) return;
54 int selection
= wxNOT_FOUND
;
56 selection
= gtk_option_menu_get_history( GTK_OPTION_MENU(choice
->GetHandle()) );
58 choice
->m_selection_hack
= selection
;
60 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
61 int n
= choice
->GetSelection();
64 event
.SetString( choice
->GetStringSelection() );
65 event
.SetEventObject(choice
);
67 if ( choice
->HasClientObjectData() )
68 event
.SetClientObject( choice
->GetClientObject(n
) );
69 else if ( choice
->HasClientUntypedData() )
70 event
.SetClientData( choice
->GetClientData(n
) );
72 choice
->GetEventHandler()->ProcessEvent(event
);
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
80 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
84 m_strings
= (wxSortedArrayString
*)NULL
;
87 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
88 const wxPoint
&pos
, const wxSize
&size
,
89 const wxArrayString
& choices
,
90 long style
, const wxValidator
& validator
,
91 const wxString
&name
)
93 wxCArrayString
chs(choices
);
95 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
96 style
, validator
, name
);
99 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
100 const wxPoint
&pos
, const wxSize
&size
,
101 int n
, const wxString choices
[],
102 long style
, const wxValidator
& validator
, const wxString
&name
)
105 #if (GTK_MINOR_VERSION > 0)
106 m_acceptsFocus
= true;
109 if (!PreCreation( parent
, pos
, size
) ||
110 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
112 wxFAIL_MSG( wxT("wxChoice creation failed") );
116 m_widget
= gtk_option_menu_new();
118 if ( style
& wxCB_SORT
)
120 // if our m_strings != NULL, DoAppend() will check for it and insert
121 // items in the correct order
122 m_strings
= new wxSortedArrayString
;
125 // begin with no selection
126 m_selection_hack
= wxNOT_FOUND
;
128 GtkWidget
*menu
= gtk_menu_new();
130 for (int i
= 0; i
< n
; i
++)
132 GtkAddHelper(menu
, i
, choices
[i
]);
135 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
137 m_parent
->DoAddChild( this );
140 SetBestSize(size
); // need this too because this is a wxControlWithItems
145 wxChoice::~wxChoice()
152 int wxChoice::DoAppend( const wxString
&item
)
154 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
156 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
158 return GtkAddHelper(menu
, GetCount(), item
);
161 int wxChoice::DoInsert( const wxString
&item
, int pos
)
163 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
164 wxCHECK_MSG( (pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
166 if (pos
== GetCount())
167 return DoAppend(item
);
169 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
171 // if the item to insert is at or before the selection, and the selection is valid
172 if ((pos
<= m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
174 // move the selection forward one
178 return GtkAddHelper(menu
, pos
, item
);
181 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
183 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
185 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
186 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
188 node
->SetData( (wxObject
*) clientData
);
191 void* wxChoice::DoGetItemClientData( int n
) const
193 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
195 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
196 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
198 return node
->GetData();
201 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
203 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
205 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
206 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
208 // wxItemContainer already deletes data for us
210 node
->SetData( (wxObject
*) clientData
);
213 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
215 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
217 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
218 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
219 wxT("invalid index in wxChoice::DoGetItemClientObject") );
221 return (wxClientData
*) node
->GetData();
224 void wxChoice::Clear()
226 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
228 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
229 GtkWidget
*menu
= gtk_menu_new();
230 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
232 if ( HasClientObjectData() )
234 // destroy the data (due to Robert's idea of using wxList<wxObject>
235 // and not wxList<wxClientData> we can't just say
236 // m_clientList.DeleteContents(true) - this would crash!
237 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
240 delete (wxClientData
*)node
->GetData();
241 node
= node
->GetNext();
244 m_clientList
.Clear();
249 // begin with no selection
250 m_selection_hack
= wxNOT_FOUND
;
253 void wxChoice::Delete( int n
)
255 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
257 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
258 // in 2.0), hence this dumb implementation -- still better than nothing
262 wxCHECK_RET( n
>= 0 && n
< count
, _T("invalid index in wxChoice::Delete") );
264 // if the item to delete is before the selection, and the selection is valid
265 if ((n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
267 // move the selection back one
270 else if (n
== m_selection_hack
)
272 // invalidate the selection
273 m_selection_hack
= wxNOT_FOUND
;
276 const bool hasClientData
= m_clientDataItemsType
!= wxClientData_None
;
277 const bool hasObjectData
= m_clientDataItemsType
== wxClientData_Object
;
279 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
282 wxArrayPtrVoid itemsData
;
284 for ( i
= 0; i
< count
; i
++ )
288 items
.Add(GetString(i
));
291 // also save the client data
292 itemsData
.Add(node
->GetData());
295 else // need to delete the client object too
299 delete (wxClientData
*)node
->GetData();
305 node
= node
->GetNext();
311 // prevent Clear() from destroying all client data
312 m_clientDataItemsType
= wxClientData_None
;
317 for ( i
= 0; i
< count
- 1; i
++ )
322 SetClientObject(i
, (wxClientData
*)itemsData
[i
]);
323 else if ( hasClientData
)
324 SetClientData(i
, itemsData
[i
]);
328 int wxChoice::FindString( const wxString
&string
, bool bCase
) const
330 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
332 // If you read this code once and you think you understand
333 // it, then you are very wrong. Robert Roebling.
335 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
337 GList
*child
= menu_shell
->children
;
340 GtkBin
*bin
= GTK_BIN( child
->data
);
341 GtkLabel
*label
= (GtkLabel
*) NULL
;
343 label
= GTK_LABEL(bin
->child
);
345 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
347 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
349 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
350 if (string
.IsSameAs( tmp
, bCase
))
360 int wxChoice::GetSelection() const
362 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
364 return m_selection_hack
;
368 void wxChoice::SetString( int n
, const wxString
& str
)
370 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
372 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
374 GList
*child
= menu_shell
->children
;
377 GtkBin
*bin
= GTK_BIN( child
->data
);
380 GtkLabel
*label
= (GtkLabel
*) NULL
;
382 label
= GTK_LABEL(bin
->child
);
384 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
386 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
388 gtk_label_set_text( label
, wxGTK_CONV( str
) );
397 wxString
wxChoice::GetString( int n
) const
399 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
401 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
403 GList
*child
= menu_shell
->children
;
406 GtkBin
*bin
= GTK_BIN( child
->data
);
409 GtkLabel
*label
= (GtkLabel
*) NULL
;
411 label
= GTK_LABEL(bin
->child
);
413 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
415 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
417 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
423 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
425 return wxEmptyString
;
428 int wxChoice::GetCount() const
430 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
432 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
434 GList
*child
= menu_shell
->children
;
443 void wxChoice::SetSelection( int n
)
445 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
448 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
450 // set the local selection variable manually
451 if ((n
>= 0) && (n
< GetCount()))
453 // a valid selection has been made
454 m_selection_hack
= n
;
456 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
458 // invalidates the selection if there are no items
459 // or if it is specifically set to wxNOT_FOUND
460 m_selection_hack
= wxNOT_FOUND
;
464 // this selects the first item by default if the selection is out of bounds
465 m_selection_hack
= 0;
469 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
471 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
473 gtk_widget_modify_style( m_widget
, style
);
474 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
476 GList
*child
= menu_shell
->children
;
479 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
481 GtkBin
*bin
= GTK_BIN( child
->data
);
482 GtkWidget
*label
= (GtkWidget
*) NULL
;
486 label
= GTK_BIN(m_widget
)->child
;
488 gtk_widget_modify_style( label
, style
);
494 int wxChoice::GtkAddHelper(GtkWidget
*menu
, int pos
, const wxString
& item
)
496 wxCHECK_MSG((pos
>=0) && (pos
<=(int)m_clientList
.GetCount()), -1, wxT("invalid index"));
498 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
503 // sorted control, need to insert at the correct index
504 index
= m_strings
->Add(item
);
506 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, index
);
510 m_clientList
.Insert( m_clientList
.Item(index
- 1),
515 m_clientList
.Insert( (wxObject
*) NULL
);
520 // don't call wxChoice::GetCount() from here because it doesn't work
521 // if we're called from ctor (and GtkMenuShell is still NULL)
523 // normal control, just append
524 if (pos
== (int)m_clientList
.GetCount())
526 gtk_menu_shell_append( GTK_MENU_SHELL(menu
), menu_item
);
527 m_clientList
.Append( (wxObject
*) NULL
);
528 index
= m_clientList
.GetCount() - 1;
532 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, pos
);
533 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
538 if (GTK_WIDGET_REALIZED(m_widget
))
540 gtk_widget_realize( menu_item
);
541 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
546 // The best size of a wxChoice should probably
547 // be changed everytime the control has been
548 // changed, but at least after adding an item
549 // it has to change. Adapted from Matt Ownby.
550 InvalidateBestSize();
552 g_signal_connect_after (menu_item
, "activate",
553 G_CALLBACK (gtk_choice_clicked_callback
),
556 gtk_widget_show( menu_item
);
558 // return the index of the item in the control
562 wxSize
wxChoice::DoGetBestSize() const
564 wxSize
ret( wxControl::DoGetBestSize() );
566 // we know better our horizontal extent: it depends on the longest string
572 size_t count
= GetCount();
573 for ( size_t n
= 0; n
< count
; n
++ )
575 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
580 // add extra for the choice "=" button
582 // VZ: I don't know how to get the right value, it seems to be in
583 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
584 // to it - maybe we can use gtk_option_menu_size_request() for this
587 // This default value works only for the default GTK+ theme (i.e.
588 // no theme at all) (FIXME)
589 static const int widthChoiceIndicator
= 35;
590 ret
.x
+= widthChoiceIndicator
;
593 // but not less than the minimal width
597 // If this request_size is called with no entries then
598 // the returned height is wrong. Give it a reasonable
601 ret
.y
= 8 + GetCharHeight();
607 bool wxChoice::IsOwnGtkWindow( GdkWindow
*window
)
609 return GTK_BUTTON(m_widget
)->event_window
;
614 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
616 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
620 #endif // wxUSE_CHOICE