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
133 wxChoice::~wxChoice()
140 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
142 void **clientData
, wxClientDataType type
)
144 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
146 const unsigned int count
= items
.GetCount();
148 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
150 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
152 int n
= GtkAddHelper(menu
, pos
, items
[i
]);
153 if ( n
== wxNOT_FOUND
)
156 AssignNewItemClientData(n
, clientData
, i
, type
);
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
163 m_selection_hack
+= count
;
166 // We must set the selection so that it can be read back even if
167 // the user has not modified it since GTK+ will then select the
168 // first item so well return 0.
169 if ((count
> 0) && (m_selection_hack
==wxNOT_FOUND
))
170 m_selection_hack
= 0;
175 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
177 m_clientData
[n
] = clientData
;
180 void* wxChoice::DoGetItemClientData(unsigned int n
) const
182 return m_clientData
[n
];
185 void wxChoice::DoClear()
187 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
189 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
190 GtkWidget
*menu
= gtk_menu_new();
191 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
193 m_clientData
.Clear();
198 // begin with no selection
199 m_selection_hack
= wxNOT_FOUND
;
202 void wxChoice::DoDeleteOneItem(unsigned int n
)
204 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
205 wxCHECK_RET( IsValid(n
), _T("invalid index in wxChoice::Delete") );
207 // if the item to delete is before the selection, and the selection is valid
208 if (((int)n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
210 // move the selection back one
213 else if ((int)n
== m_selection_hack
)
215 // invalidate the selection
216 m_selection_hack
= wxNOT_FOUND
;
219 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
220 // in 2.0), hence this dumb implementation -- still better than nothing
221 const unsigned int count
= GetCount();
224 wxArrayPtrVoid itemsData
;
225 items
.Alloc(count
- 1);
226 itemsData
.Alloc(count
- 1);
227 for ( unsigned i
= 0; i
< count
; i
++ )
231 items
.Add(GetString(i
));
232 itemsData
.Add(m_clientData
[i
]);
240 void ** const data
= &itemsData
[0];
241 if ( HasClientObjectData() )
242 Append(items
, wx_reinterpret_cast(wxClientData
**, data
));
246 //else: the control is now empty, nothing to append
249 int wxChoice::FindString( const wxString
&string
, bool bCase
) const
251 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
253 // If you read this code once and you think you understand
254 // it, then you are very wrong. Robert Roebling.
256 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
258 GList
*child
= menu_shell
->children
;
261 GtkBin
*bin
= GTK_BIN( child
->data
);
262 GtkLabel
*label
= (GtkLabel
*) NULL
;
264 label
= GTK_LABEL(bin
->child
);
266 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
268 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
270 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
271 if (string
.IsSameAs( tmp
, bCase
))
281 int wxChoice::GetSelection() const
283 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
285 return m_selection_hack
;
289 void wxChoice::SetString(unsigned int n
, const wxString
& str
)
291 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
293 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
294 unsigned int count
= 0;
295 GList
*child
= menu_shell
->children
;
298 GtkBin
*bin
= GTK_BIN( child
->data
);
301 GtkLabel
*label
= (GtkLabel
*) NULL
;
303 label
= GTK_LABEL(bin
->child
);
305 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
307 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
309 gtk_label_set_text( label
, wxGTK_CONV( str
) );
311 InvalidateBestSize();
320 wxString
wxChoice::GetString(unsigned int n
) const
322 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
324 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
325 unsigned int count
= 0;
326 GList
*child
= menu_shell
->children
;
329 GtkBin
*bin
= GTK_BIN( child
->data
);
332 GtkLabel
*label
= (GtkLabel
*) NULL
;
334 label
= GTK_LABEL(bin
->child
);
336 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
338 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
340 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
346 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
348 return wxEmptyString
;
351 unsigned int wxChoice::GetCount() const
353 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
355 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
356 unsigned int count
= 0;
357 GList
*child
= menu_shell
->children
;
366 void wxChoice::SetSelection( int n
)
368 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
371 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
373 // set the local selection variable manually
374 if ((n
>= 0) && ((unsigned int)n
< GetCount()))
376 // a valid selection has been made
377 m_selection_hack
= n
;
379 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
381 // invalidates the selection if there are no items
382 // or if it is specifically set to wxNOT_FOUND
383 m_selection_hack
= wxNOT_FOUND
;
387 // this selects the first item by default if the selection is out of bounds
388 m_selection_hack
= 0;
392 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
394 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
396 gtk_widget_modify_style( m_widget
, style
);
397 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
399 GList
*child
= menu_shell
->children
;
402 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
404 GtkBin
*bin
= GTK_BIN( child
->data
);
405 GtkWidget
*label
= (GtkWidget
*) NULL
;
409 label
= GTK_BIN(m_widget
)->child
;
411 gtk_widget_modify_style( label
, style
);
417 int wxChoice::GtkAddHelper(GtkWidget
*menu
, unsigned int pos
, const wxString
& item
)
419 wxCHECK_MSG(pos
<=m_clientData
.GetCount(), -1, wxT("invalid index"));
421 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
425 // sorted control, need to insert at the correct index
426 pos
= m_strings
->Add(item
);
429 // don't call wxChoice::GetCount() from here because it doesn't work
430 // if we're called from ctor (and GtkMenuShell is still NULL)
431 if (pos
== m_clientData
.GetCount())
432 gtk_menu_shell_append( GTK_MENU_SHELL(menu
), menu_item
);
434 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, pos
);
436 m_clientData
.Insert( NULL
, pos
);
438 if (GTK_WIDGET_REALIZED(m_widget
))
440 gtk_widget_realize( menu_item
);
441 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
446 // The best size of a wxChoice should probably
447 // be changed everytime the control has been
448 // changed, but at least after adding an item
449 // it has to change. Adapted from Matt Ownby.
450 InvalidateBestSize();
452 g_signal_connect_after (menu_item
, "activate",
453 G_CALLBACK (gtk_choice_clicked_callback
),
456 gtk_widget_show( menu_item
);
458 // return the index of the item in the control
462 wxSize
wxChoice::DoGetBestSize() const
464 wxSize
ret( wxControl::DoGetBestSize() );
466 // we know better our horizontal extent: it depends on the longest string
472 unsigned int count
= GetCount();
473 for ( unsigned int n
= 0; n
< count
; n
++ )
475 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
480 // add extra for the choice "=" button
482 // VZ: I don't know how to get the right value, it seems to be in
483 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
484 // to it - maybe we can use gtk_option_menu_size_request() for this
487 // This default value works only for the default GTK+ theme (i.e.
488 // no theme at all) (FIXME)
489 static const int widthChoiceIndicator
= 35;
490 ret
.x
+= widthChoiceIndicator
;
493 // but not less than the minimal width
497 // If this request_size is called with no entries then
498 // the returned height is wrong. Give it a reasonable
501 ret
.y
= 8 + GetCharHeight();
507 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
509 return GTK_BUTTON(m_widget
)->event_window
;
514 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
516 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
520 #endif // wxUSE_CHOICE