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"
17 #include "wx/arrstr.h"
20 // FIXME: We use GtkOptionMenu which has been deprecated since GTK+ 2.3.0 in
21 // favour of GtkComboBox.
22 // Later use GtkComboBox if GTK+ runtime version is new enough.
23 #include <gtk/gtkversion.h>
24 #if defined(GTK_DISABLE_DEPRECATED) && GTK_CHECK_VERSION(2,3,0)
25 #undef GTK_DISABLE_DEPRECATED
28 #include "wx/gtk/private.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern bool g_blockEventsOnDrag
;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
41 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
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
)
97 if (!PreCreation( parent
, pos
, size
) ||
98 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
100 wxFAIL_MSG( wxT("wxChoice creation failed") );
104 m_widget
= gtk_option_menu_new();
106 if ( style
& wxCB_SORT
)
108 // if our m_strings != NULL, DoAppend() will check for it and insert
109 // items in the correct order
110 m_strings
= new wxSortedArrayString
;
113 // begin with no selection
114 m_selection_hack
= wxNOT_FOUND
;
116 GtkWidget
*menu
= gtk_menu_new();
118 for (unsigned int i
= 0; i
< (unsigned int)n
; i
++)
120 GtkAddHelper(menu
, i
, choices
[i
]);
123 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
125 m_parent
->DoAddChild( this );
128 SetInitialSize(size
); // need this too because this is a wxControlWithItems
133 wxChoice::~wxChoice()
140 int wxChoice::DoAppend( const wxString
&item
)
142 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
144 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
146 return GtkAddHelper(menu
, GetCount(), item
);
149 int wxChoice::DoInsert(const wxString
&item
, unsigned int pos
)
151 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
152 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index"));
154 if (pos
== GetCount())
155 return DoAppend(item
);
157 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
159 // if the item to insert is at or before the selection, and the selection is valid
160 if (((int)pos
<= m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
162 // move the selection forward one
166 return GtkAddHelper(menu
, pos
, item
);
169 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
171 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
173 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
174 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
176 node
->SetData( (wxObject
*) clientData
);
179 void* wxChoice::DoGetItemClientData(unsigned int n
) const
181 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
183 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
184 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
186 return node
->GetData();
189 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
191 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
193 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
194 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
196 // wxItemContainer already deletes data for us
198 node
->SetData( (wxObject
*) clientData
);
201 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
203 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
205 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
206 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
207 wxT("invalid index in wxChoice::DoGetItemClientObject") );
209 return (wxClientData
*) node
->GetData();
212 void wxChoice::Clear()
214 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
216 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
217 GtkWidget
*menu
= gtk_menu_new();
218 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
220 if ( HasClientObjectData() )
222 // destroy the data (due to Robert's idea of using wxList<wxObject>
223 // and not wxList<wxClientData> we can't just say
224 // m_clientList.DeleteContents(true) - this would crash!
225 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
228 delete (wxClientData
*)node
->GetData();
229 node
= node
->GetNext();
232 m_clientList
.Clear();
237 // begin with no selection
238 m_selection_hack
= wxNOT_FOUND
;
241 void wxChoice::Delete(unsigned int n
)
243 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
244 wxCHECK_RET( IsValid(n
), _T("invalid index in wxChoice::Delete") );
246 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
247 // in 2.0), hence this dumb implementation -- still better than nothing
249 const unsigned int count
= GetCount();
251 // if the item to delete is before the selection, and the selection is valid
252 if (((int)n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
254 // move the selection back one
257 else if ((int)n
== m_selection_hack
)
259 // invalidate the selection
260 m_selection_hack
= wxNOT_FOUND
;
263 const bool hasClientData
= m_clientDataItemsType
!= wxClientData_None
;
264 const bool hasObjectData
= m_clientDataItemsType
== wxClientData_Object
;
266 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
269 wxArrayPtrVoid itemsData
;
271 for ( i
= 0; i
< count
; i
++ )
275 items
.Add(GetString(i
));
278 // also save the client data
279 itemsData
.Add(node
->GetData());
282 else // need to delete the client object too
286 delete (wxClientData
*)node
->GetData();
292 node
= node
->GetNext();
298 // prevent Clear() from destroying all client data
299 m_clientDataItemsType
= wxClientData_None
;
304 for ( i
= 0; i
< count
- 1; i
++ )
309 SetClientObject(i
, (wxClientData
*)itemsData
[i
]);
310 else if ( hasClientData
)
311 SetClientData(i
, itemsData
[i
]);
315 int wxChoice::FindString( const wxString
&string
, bool bCase
) const
317 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
319 // If you read this code once and you think you understand
320 // it, then you are very wrong. Robert Roebling.
322 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
324 GList
*child
= menu_shell
->children
;
327 GtkBin
*bin
= GTK_BIN( child
->data
);
328 GtkLabel
*label
= (GtkLabel
*) NULL
;
330 label
= GTK_LABEL(bin
->child
);
332 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
334 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
336 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
337 if (string
.IsSameAs( tmp
, bCase
))
347 int wxChoice::GetSelection() const
349 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
351 return m_selection_hack
;
355 void wxChoice::SetString(unsigned int n
, const wxString
& str
)
357 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
359 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
360 unsigned int count
= 0;
361 GList
*child
= menu_shell
->children
;
364 GtkBin
*bin
= GTK_BIN( child
->data
);
367 GtkLabel
*label
= (GtkLabel
*) NULL
;
369 label
= GTK_LABEL(bin
->child
);
371 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
373 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
375 gtk_label_set_text( label
, wxGTK_CONV( str
) );
384 wxString
wxChoice::GetString(unsigned int n
) const
386 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
388 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
389 unsigned int count
= 0;
390 GList
*child
= menu_shell
->children
;
393 GtkBin
*bin
= GTK_BIN( child
->data
);
396 GtkLabel
*label
= (GtkLabel
*) NULL
;
398 label
= GTK_LABEL(bin
->child
);
400 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
402 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
404 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
410 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
412 return wxEmptyString
;
415 unsigned int wxChoice::GetCount() const
417 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
419 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
420 unsigned int count
= 0;
421 GList
*child
= menu_shell
->children
;
430 void wxChoice::SetSelection( int n
)
432 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
435 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
437 // set the local selection variable manually
438 if ((n
>= 0) && ((unsigned int)n
< GetCount()))
440 // a valid selection has been made
441 m_selection_hack
= n
;
443 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
445 // invalidates the selection if there are no items
446 // or if it is specifically set to wxNOT_FOUND
447 m_selection_hack
= wxNOT_FOUND
;
451 // this selects the first item by default if the selection is out of bounds
452 m_selection_hack
= 0;
456 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
458 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
460 gtk_widget_modify_style( m_widget
, style
);
461 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
463 GList
*child
= menu_shell
->children
;
466 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
468 GtkBin
*bin
= GTK_BIN( child
->data
);
469 GtkWidget
*label
= (GtkWidget
*) NULL
;
473 label
= GTK_BIN(m_widget
)->child
;
475 gtk_widget_modify_style( label
, style
);
481 int wxChoice::GtkAddHelper(GtkWidget
*menu
, unsigned int pos
, const wxString
& item
)
483 wxCHECK_MSG(pos
<=m_clientList
.GetCount(), -1, wxT("invalid index"));
485 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
490 // sorted control, need to insert at the correct index
491 index
= m_strings
->Add(item
);
493 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, index
);
497 m_clientList
.Insert( m_clientList
.Item(index
- 1),
502 m_clientList
.Insert( (wxObject
*) NULL
);
507 // don't call wxChoice::GetCount() from here because it doesn't work
508 // if we're called from ctor (and GtkMenuShell is still NULL)
510 // normal control, just append
511 if (pos
== m_clientList
.GetCount())
513 gtk_menu_shell_append( GTK_MENU_SHELL(menu
), menu_item
);
514 m_clientList
.Append( (wxObject
*) NULL
);
515 index
= m_clientList
.GetCount() - 1;
519 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, pos
);
520 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
525 if (GTK_WIDGET_REALIZED(m_widget
))
527 gtk_widget_realize( menu_item
);
528 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
533 // The best size of a wxChoice should probably
534 // be changed everytime the control has been
535 // changed, but at least after adding an item
536 // it has to change. Adapted from Matt Ownby.
537 InvalidateBestSize();
539 g_signal_connect_after (menu_item
, "activate",
540 G_CALLBACK (gtk_choice_clicked_callback
),
543 gtk_widget_show( menu_item
);
545 // return the index of the item in the control
549 wxSize
wxChoice::DoGetBestSize() const
551 wxSize
ret( wxControl::DoGetBestSize() );
553 // we know better our horizontal extent: it depends on the longest string
559 unsigned int count
= GetCount();
560 for ( unsigned int n
= 0; n
< count
; n
++ )
562 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
567 // add extra for the choice "=" button
569 // VZ: I don't know how to get the right value, it seems to be in
570 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
571 // to it - maybe we can use gtk_option_menu_size_request() for this
574 // This default value works only for the default GTK+ theme (i.e.
575 // no theme at all) (FIXME)
576 static const int widthChoiceIndicator
= 35;
577 ret
.x
+= widthChoiceIndicator
;
580 // but not less than the minimal width
584 // If this request_size is called with no entries then
585 // the returned height is wrong. Give it a reasonable
588 ret
.y
= 8 + GetCharHeight();
594 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
596 return GTK_BUTTON(m_widget
)->event_window
;
601 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
603 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
607 #endif // wxUSE_CHOICE