1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/choice.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
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 bool g_blockEventsOnDrag
;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
38 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
41 wxapp_install_idle_handler();
43 if (!choice
->m_hasVMT
) return;
45 if (g_blockEventsOnDrag
) return;
47 int selection
= wxNOT_FOUND
;
49 selection
= gtk_option_menu_get_history( GTK_OPTION_MENU(choice
->GetHandle()) );
51 choice
->m_selection_hack
= selection
;
53 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
54 int n
= choice
->GetSelection();
57 event
.SetString( choice
->GetStringSelection() );
58 event
.SetEventObject(choice
);
60 if ( choice
->HasClientObjectData() )
61 event
.SetClientObject( choice
->GetClientObject(n
) );
62 else if ( choice
->HasClientUntypedData() )
63 event
.SetClientData( choice
->GetClientData(n
) );
65 choice
->GetEventHandler()->ProcessEvent(event
);
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
77 m_strings
= (wxSortedArrayString
*)NULL
;
80 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
81 const wxPoint
&pos
, const wxSize
&size
,
82 const wxArrayString
& choices
,
83 long style
, const wxValidator
& validator
,
84 const wxString
&name
)
86 wxCArrayString
chs(choices
);
88 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
89 style
, validator
, name
);
92 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
93 const wxPoint
&pos
, const wxSize
&size
,
94 int n
, const wxString choices
[],
95 long style
, const wxValidator
& validator
, const wxString
&name
)
98 #if (GTK_MINOR_VERSION > 0)
99 m_acceptsFocus
= true;
102 if (!PreCreation( parent
, pos
, size
) ||
103 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
105 wxFAIL_MSG( wxT("wxChoice creation failed") );
109 m_widget
= gtk_option_menu_new();
111 if ( style
& wxCB_SORT
)
113 // if our m_strings != NULL, DoAppend() will check for it and insert
114 // items in the correct order
115 m_strings
= new wxSortedArrayString
;
118 // begin with no selection
119 m_selection_hack
= wxNOT_FOUND
;
121 GtkWidget
*menu
= gtk_menu_new();
123 for (size_t i
= 0; i
< (size_t)n
; i
++)
125 GtkAddHelper(menu
, i
, choices
[i
]);
128 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
130 m_parent
->DoAddChild( this );
133 SetBestSize(size
); // need this too because this is a wxControlWithItems
138 wxChoice::~wxChoice()
145 int wxChoice::DoAppend( const wxString
&item
)
147 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
149 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
151 return GtkAddHelper(menu
, GetCount(), item
);
154 int wxChoice::DoInsert( const wxString
&item
, int pos
)
156 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
157 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index"));
159 if ((size_t)pos
== GetCount())
160 return DoAppend(item
);
162 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
164 // if the item to insert is at or before the selection, and the selection is valid
165 if ((pos
<= m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
167 // move the selection forward one
171 return GtkAddHelper(menu
, (size_t)pos
, item
);
174 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
176 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
178 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
179 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
181 node
->SetData( (wxObject
*) clientData
);
184 void* wxChoice::DoGetItemClientData( int n
) const
186 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
188 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
189 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
191 return node
->GetData();
194 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
196 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
198 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
199 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
201 // wxItemContainer already deletes data for us
203 node
->SetData( (wxObject
*) clientData
);
206 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
208 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
210 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
211 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
212 wxT("invalid index in wxChoice::DoGetItemClientObject") );
214 return (wxClientData
*) node
->GetData();
217 void wxChoice::Clear()
219 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
221 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
222 GtkWidget
*menu
= gtk_menu_new();
223 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
225 if ( HasClientObjectData() )
227 // destroy the data (due to Robert's idea of using wxList<wxObject>
228 // and not wxList<wxClientData> we can't just say
229 // m_clientList.DeleteContents(true) - this would crash!
230 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
233 delete (wxClientData
*)node
->GetData();
234 node
= node
->GetNext();
237 m_clientList
.Clear();
242 // begin with no selection
243 m_selection_hack
= wxNOT_FOUND
;
246 void wxChoice::Delete( int n
)
248 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
249 wxCHECK_RET( IsValid(n
), _T("invalid index in wxChoice::Delete") );
251 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
252 // in 2.0), hence this dumb implementation -- still better than nothing
254 const size_t count
= GetCount();
256 // if the item to delete is before the selection, and the selection is valid
257 if ((n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
259 // move the selection back one
262 else if (n
== m_selection_hack
)
264 // invalidate the selection
265 m_selection_hack
= wxNOT_FOUND
;
268 const bool hasClientData
= m_clientDataItemsType
!= wxClientData_None
;
269 const bool hasObjectData
= m_clientDataItemsType
== wxClientData_Object
;
271 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
274 wxArrayPtrVoid itemsData
;
276 for ( i
= 0; i
< count
; i
++ )
278 if ( i
!= (size_t)n
)
280 items
.Add(GetString(i
));
283 // also save the client data
284 itemsData
.Add(node
->GetData());
287 else // need to delete the client object too
291 delete (wxClientData
*)node
->GetData();
297 node
= node
->GetNext();
303 // prevent Clear() from destroying all client data
304 m_clientDataItemsType
= wxClientData_None
;
309 for ( i
= 0; i
< count
- 1; i
++ )
314 SetClientObject(i
, (wxClientData
*)itemsData
[i
]);
315 else if ( hasClientData
)
316 SetClientData(i
, itemsData
[i
]);
320 int wxChoice::FindString( const wxString
&string
, bool bCase
) const
322 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
324 // If you read this code once and you think you understand
325 // it, then you are very wrong. Robert Roebling.
327 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
329 GList
*child
= menu_shell
->children
;
332 GtkBin
*bin
= GTK_BIN( child
->data
);
333 GtkLabel
*label
= (GtkLabel
*) NULL
;
335 label
= GTK_LABEL(bin
->child
);
337 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
339 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
341 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
342 if (string
.IsSameAs( tmp
, bCase
))
352 int wxChoice::GetSelection() const
354 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
356 return m_selection_hack
;
360 void wxChoice::SetString( int n
, const wxString
& str
)
362 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
364 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
366 GList
*child
= menu_shell
->children
;
369 GtkBin
*bin
= GTK_BIN( child
->data
);
372 GtkLabel
*label
= (GtkLabel
*) NULL
;
374 label
= GTK_LABEL(bin
->child
);
376 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
378 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
380 gtk_label_set_text( label
, wxGTK_CONV( str
) );
389 wxString
wxChoice::GetString( int n
) const
391 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
393 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
395 GList
*child
= menu_shell
->children
;
398 GtkBin
*bin
= GTK_BIN( child
->data
);
401 GtkLabel
*label
= (GtkLabel
*) NULL
;
403 label
= GTK_LABEL(bin
->child
);
405 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
407 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
409 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
415 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
417 return wxEmptyString
;
420 size_t wxChoice::GetCount() const
422 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
424 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
426 GList
*child
= menu_shell
->children
;
435 void wxChoice::SetSelection( int n
)
437 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
440 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
442 // set the local selection variable manually
443 if ((n
>= 0) && (n
< (int)GetCount()))
445 // a valid selection has been made
446 m_selection_hack
= n
;
448 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
450 // invalidates the selection if there are no items
451 // or if it is specifically set to wxNOT_FOUND
452 m_selection_hack
= wxNOT_FOUND
;
456 // this selects the first item by default if the selection is out of bounds
457 m_selection_hack
= 0;
461 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
463 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
465 gtk_widget_modify_style( m_widget
, style
);
466 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
468 GList
*child
= menu_shell
->children
;
471 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
473 GtkBin
*bin
= GTK_BIN( child
->data
);
474 GtkWidget
*label
= (GtkWidget
*) NULL
;
478 label
= GTK_BIN(m_widget
)->child
;
480 gtk_widget_modify_style( label
, style
);
487 #pragma message disable unscomzer
490 int wxChoice::GtkAddHelper(GtkWidget
*menu
, size_t pos
, const wxString
& item
)
492 wxCHECK_MSG((pos
>=0) && (pos
<=m_clientList
.GetCount()), -1, wxT("invalid index"));
494 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
499 // sorted control, need to insert at the correct index
500 index
= m_strings
->Add(item
);
502 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, index
);
506 m_clientList
.Insert( m_clientList
.Item(index
- 1),
511 m_clientList
.Insert( (wxObject
*) NULL
);
516 // don't call wxChoice::GetCount() from here because it doesn't work
517 // if we're called from ctor (and GtkMenuShell is still NULL)
519 // normal control, just append
520 if (pos
== m_clientList
.GetCount())
522 gtk_menu_shell_append( GTK_MENU_SHELL(menu
), menu_item
);
523 m_clientList
.Append( (wxObject
*) NULL
);
524 index
= m_clientList
.GetCount() - 1;
528 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, pos
);
529 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
534 if (GTK_WIDGET_REALIZED(m_widget
))
536 gtk_widget_realize( menu_item
);
537 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
542 // The best size of a wxChoice should probably
543 // be changed everytime the control has been
544 // changed, but at least after adding an item
545 // it has to change. Adapted from Matt Ownby.
546 InvalidateBestSize();
548 g_signal_connect_after (menu_item
, "activate",
549 G_CALLBACK (gtk_choice_clicked_callback
),
552 gtk_widget_show( menu_item
);
554 // return the index of the item in the control
559 #pragma message enable unscomzer
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