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
->HandleWindowEvent(event
);
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControlWithItems
)
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();
108 // if our m_strings != NULL, Append() will check for it and insert
109 // items in the correct order
110 m_strings
= new wxSortedArrayString
;
113 // If we have items, GTK will choose the first item by default
114 m_selection_hack
= n
> 0 ? 0 : 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
130 // workaround for bug in gtk_option_menu_set_history(), it causes
131 // gtk_widget_size_allocate() to be called with the current
132 // widget->allocation values, which will be zero if a proper
133 // size_allocate has not occured yet
134 m_widget
->allocation
.width
= m_width
;
135 m_widget
->allocation
.height
= m_height
;
140 wxChoice::~wxChoice()
147 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
149 void **clientData
, wxClientDataType type
)
151 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
153 const unsigned int count
= items
.GetCount();
155 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
157 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
159 int n
= GtkAddHelper(menu
, pos
, items
[i
]);
160 if ( n
== wxNOT_FOUND
)
163 AssignNewItemClientData(n
, clientData
, i
, type
);
166 // if the item to insert is at or before the selection, and the selection is valid
167 if (((int)pos
<= m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
169 // move the selection forward
170 m_selection_hack
+= count
;
173 // We must set the selection so that it can be read back even if
174 // the user has not modified it since GTK+ will then select the
175 // first item so well return 0.
176 if ((count
> 0) && (m_selection_hack
==wxNOT_FOUND
))
177 m_selection_hack
= 0;
182 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
184 m_clientData
[n
] = clientData
;
187 void* wxChoice::DoGetItemClientData(unsigned int n
) const
189 return m_clientData
[n
];
192 void wxChoice::DoClear()
194 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
196 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
197 GtkWidget
*menu
= gtk_menu_new();
198 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
200 m_clientData
.Clear();
205 // begin with no selection
206 m_selection_hack
= wxNOT_FOUND
;
209 void wxChoice::DoDeleteOneItem(unsigned int n
)
211 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
212 wxCHECK_RET( IsValid(n
), _T("invalid index in wxChoice::Delete") );
214 // if the item to delete is before the selection, and the selection is valid
215 if (((int)n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
217 // move the selection back one
220 else if ((int)n
== m_selection_hack
)
222 // invalidate the selection
223 m_selection_hack
= wxNOT_FOUND
;
226 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
227 // in 2.0), hence this dumb implementation -- still better than nothing
228 const unsigned int count
= GetCount();
231 wxArrayPtrVoid itemsData
;
232 items
.Alloc(count
- 1);
233 itemsData
.Alloc(count
- 1);
234 for ( unsigned i
= 0; i
< count
; i
++ )
238 items
.Add(GetString(i
));
239 itemsData
.Add(m_clientData
[i
]);
247 void ** const data
= &itemsData
[0];
248 if ( HasClientObjectData() )
249 Append(items
, wx_reinterpret_cast(wxClientData
**, data
));
253 //else: the control is now empty, nothing to append
256 int wxChoice::FindString( const wxString
&string
, bool bCase
) const
258 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
260 // If you read this code once and you think you understand
261 // it, then you are very wrong. Robert Roebling.
263 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
265 GList
*child
= menu_shell
->children
;
268 GtkBin
*bin
= GTK_BIN( child
->data
);
269 GtkLabel
*label
= (GtkLabel
*) NULL
;
271 label
= GTK_LABEL(bin
->child
);
273 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
275 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
277 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
278 if (string
.IsSameAs( tmp
, bCase
))
288 int wxChoice::GetSelection() const
290 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
292 return m_selection_hack
;
296 void wxChoice::SetString(unsigned int n
, const wxString
& str
)
298 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
300 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
301 unsigned int count
= 0;
302 GList
*child
= menu_shell
->children
;
305 GtkBin
*bin
= GTK_BIN( child
->data
);
308 GtkLabel
*label
= (GtkLabel
*) NULL
;
310 label
= GTK_LABEL(bin
->child
);
312 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
314 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
316 gtk_label_set_text( label
, wxGTK_CONV( str
) );
318 InvalidateBestSize();
327 wxString
wxChoice::GetString(unsigned int n
) const
329 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
331 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
332 unsigned int count
= 0;
333 GList
*child
= menu_shell
->children
;
336 GtkBin
*bin
= GTK_BIN( child
->data
);
339 GtkLabel
*label
= (GtkLabel
*) NULL
;
341 label
= GTK_LABEL(bin
->child
);
343 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
345 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
347 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
353 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
355 return wxEmptyString
;
358 unsigned int wxChoice::GetCount() const
360 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
362 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
363 unsigned int count
= 0;
364 GList
*child
= menu_shell
->children
;
373 void wxChoice::SetSelection( int n
)
375 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
378 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
380 // set the local selection variable manually
381 if ((n
>= 0) && ((unsigned int)n
< GetCount()))
383 // a valid selection has been made
384 m_selection_hack
= n
;
386 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
388 // invalidates the selection if there are no items
389 // or if it is specifically set to wxNOT_FOUND
390 m_selection_hack
= wxNOT_FOUND
;
394 // this selects the first item by default if the selection is out of bounds
395 m_selection_hack
= 0;
399 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
401 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
403 gtk_widget_modify_style( m_widget
, style
);
404 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
406 GList
*child
= menu_shell
->children
;
409 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
411 GtkBin
*bin
= GTK_BIN( child
->data
);
412 GtkWidget
*label
= (GtkWidget
*) NULL
;
416 label
= GTK_BIN(m_widget
)->child
;
418 gtk_widget_modify_style( label
, style
);
424 int wxChoice::GtkAddHelper(GtkWidget
*menu
, unsigned int pos
, const wxString
& item
)
426 wxCHECK_MSG(pos
<=m_clientData
.GetCount(), -1, wxT("invalid index"));
428 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
432 // sorted control, need to insert at the correct index
433 pos
= m_strings
->Add(item
);
436 // don't call wxChoice::GetCount() from here because it doesn't work
437 // if we're called from ctor (and GtkMenuShell is still NULL)
438 if (pos
== m_clientData
.GetCount())
439 gtk_menu_shell_append( GTK_MENU_SHELL(menu
), menu_item
);
441 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, pos
);
443 m_clientData
.Insert( NULL
, pos
);
445 if (GTK_WIDGET_REALIZED(m_widget
))
447 gtk_widget_realize( menu_item
);
448 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
453 // The best size of a wxChoice should probably
454 // be changed everytime the control has been
455 // changed, but at least after adding an item
456 // it has to change. Adapted from Matt Ownby.
457 InvalidateBestSize();
459 g_signal_connect_after (menu_item
, "activate",
460 G_CALLBACK (gtk_choice_clicked_callback
),
463 gtk_widget_show( menu_item
);
465 // return the index of the item in the control
469 wxSize
wxChoice::DoGetBestSize() const
471 wxSize
ret( wxControl::DoGetBestSize() );
473 // we know better our horizontal extent: it depends on the longest string
479 unsigned int count
= GetCount();
480 for ( unsigned int n
= 0; n
< count
; n
++ )
482 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
487 // add extra for the choice "=" button
489 // VZ: I don't know how to get the right value, it seems to be in
490 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
491 // to it - maybe we can use gtk_option_menu_size_request() for this
494 // This default value works only for the default GTK+ theme (i.e.
495 // no theme at all) (FIXME)
496 static const int widthChoiceIndicator
= 35;
497 ret
.x
+= widthChoiceIndicator
;
500 // but not less than the minimal width
504 // If this request_size is called with no entries then
505 // the returned height is wrong. Give it a reasonable
508 ret
.y
= 8 + GetCharHeight();
514 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
516 return GTK_BUTTON(m_widget
)->event_window
;
521 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
523 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
527 #endif // wxUSE_CHOICE