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
;
226 itemsData
.Alloc(count
);
227 for ( unsigned i
= 0; i
< count
; i
++ )
231 items
.Add(GetString(i
));
232 itemsData
.Add(m_clientData
[i
]);
238 void ** const data
= &itemsData
[0];
239 if ( HasClientObjectData() )
240 Append(items
, wx_reinterpret_cast(wxClientData
**, data
));
245 int wxChoice::FindString( const wxString
&string
, bool bCase
) const
247 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
249 // If you read this code once and you think you understand
250 // it, then you are very wrong. Robert Roebling.
252 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
254 GList
*child
= menu_shell
->children
;
257 GtkBin
*bin
= GTK_BIN( child
->data
);
258 GtkLabel
*label
= (GtkLabel
*) NULL
;
260 label
= GTK_LABEL(bin
->child
);
262 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
264 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
266 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
267 if (string
.IsSameAs( tmp
, bCase
))
277 int wxChoice::GetSelection() const
279 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
281 return m_selection_hack
;
285 void wxChoice::SetString(unsigned int n
, const wxString
& str
)
287 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
289 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
290 unsigned int count
= 0;
291 GList
*child
= menu_shell
->children
;
294 GtkBin
*bin
= GTK_BIN( child
->data
);
297 GtkLabel
*label
= (GtkLabel
*) NULL
;
299 label
= GTK_LABEL(bin
->child
);
301 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
303 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
305 gtk_label_set_text( label
, wxGTK_CONV( str
) );
307 InvalidateBestSize();
316 wxString
wxChoice::GetString(unsigned int n
) const
318 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
320 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
321 unsigned int count
= 0;
322 GList
*child
= menu_shell
->children
;
325 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 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
342 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
344 return wxEmptyString
;
347 unsigned int wxChoice::GetCount() const
349 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
351 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
352 unsigned int count
= 0;
353 GList
*child
= menu_shell
->children
;
362 void wxChoice::SetSelection( int n
)
364 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
367 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
369 // set the local selection variable manually
370 if ((n
>= 0) && ((unsigned int)n
< GetCount()))
372 // a valid selection has been made
373 m_selection_hack
= n
;
375 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
377 // invalidates the selection if there are no items
378 // or if it is specifically set to wxNOT_FOUND
379 m_selection_hack
= wxNOT_FOUND
;
383 // this selects the first item by default if the selection is out of bounds
384 m_selection_hack
= 0;
388 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
390 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
392 gtk_widget_modify_style( m_widget
, style
);
393 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
395 GList
*child
= menu_shell
->children
;
398 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
400 GtkBin
*bin
= GTK_BIN( child
->data
);
401 GtkWidget
*label
= (GtkWidget
*) NULL
;
405 label
= GTK_BIN(m_widget
)->child
;
407 gtk_widget_modify_style( label
, style
);
413 int wxChoice::GtkAddHelper(GtkWidget
*menu
, unsigned int pos
, const wxString
& item
)
415 wxCHECK_MSG(pos
<=m_clientData
.GetCount(), -1, wxT("invalid index"));
417 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
421 // sorted control, need to insert at the correct index
422 pos
= m_strings
->Add(item
);
425 // don't call wxChoice::GetCount() from here because it doesn't work
426 // if we're called from ctor (and GtkMenuShell is still NULL)
427 if (pos
== m_clientData
.GetCount())
428 gtk_menu_shell_append( GTK_MENU_SHELL(menu
), menu_item
);
430 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, pos
);
432 m_clientData
.Insert( NULL
, pos
);
434 if (GTK_WIDGET_REALIZED(m_widget
))
436 gtk_widget_realize( menu_item
);
437 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
442 // The best size of a wxChoice should probably
443 // be changed everytime the control has been
444 // changed, but at least after adding an item
445 // it has to change. Adapted from Matt Ownby.
446 InvalidateBestSize();
448 g_signal_connect_after (menu_item
, "activate",
449 G_CALLBACK (gtk_choice_clicked_callback
),
452 gtk_widget_show( menu_item
);
454 // return the index of the item in the control
458 wxSize
wxChoice::DoGetBestSize() const
460 wxSize
ret( wxControl::DoGetBestSize() );
462 // we know better our horizontal extent: it depends on the longest string
468 unsigned int count
= GetCount();
469 for ( unsigned int n
= 0; n
< count
; n
++ )
471 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
476 // add extra for the choice "=" button
478 // VZ: I don't know how to get the right value, it seems to be in
479 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
480 // to it - maybe we can use gtk_option_menu_size_request() for this
483 // This default value works only for the default GTK+ theme (i.e.
484 // no theme at all) (FIXME)
485 static const int widthChoiceIndicator
= 35;
486 ret
.x
+= widthChoiceIndicator
;
489 // but not less than the minimal width
493 // If this request_size is called with no entries then
494 // the returned height is wrong. Give it a reasonable
497 ret
.y
= 8 + GetCharHeight();
503 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
505 return GTK_BUTTON(m_widget
)->event_window
;
510 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
512 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
516 #endif // wxUSE_CHOICE