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();
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 // 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::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
;
169 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
171 m_clientData
[n
] = clientData
;
174 void* wxChoice::DoGetItemClientData(unsigned int n
) const
176 return m_clientData
[n
];
179 void wxChoice::DoClear()
181 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
183 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
184 GtkWidget
*menu
= gtk_menu_new();
185 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
187 m_clientData
.Clear();
192 // begin with no selection
193 m_selection_hack
= wxNOT_FOUND
;
196 void wxChoice::DoDeleteOneItem(unsigned int n
)
198 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
199 wxCHECK_RET( IsValid(n
), _T("invalid index in wxChoice::Delete") );
201 // if the item to delete is before the selection, and the selection is valid
202 if (((int)n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
204 // move the selection back one
207 else if ((int)n
== m_selection_hack
)
209 // invalidate the selection
210 m_selection_hack
= wxNOT_FOUND
;
213 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
214 // in 2.0), hence this dumb implementation -- still better than nothing
215 const unsigned int count
= GetCount();
218 wxArrayPtrVoid itemsData
;
220 itemsData
.Alloc(count
);
221 for ( unsigned i
= 0; i
< count
; i
++ )
225 items
.Add(GetString(i
));
226 itemsData
.Add(m_clientData
[i
]);
232 void ** const data
= &itemsData
[0];
233 if ( HasClientObjectData() )
234 Append(items
, wx_reinterpret_cast(wxClientData
**, data
));
239 int wxChoice::FindString( const wxString
&string
, bool bCase
) const
241 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
243 // If you read this code once and you think you understand
244 // it, then you are very wrong. Robert Roebling.
246 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
248 GList
*child
= menu_shell
->children
;
251 GtkBin
*bin
= GTK_BIN( child
->data
);
252 GtkLabel
*label
= (GtkLabel
*) NULL
;
254 label
= GTK_LABEL(bin
->child
);
256 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
258 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
260 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
261 if (string
.IsSameAs( tmp
, bCase
))
271 int wxChoice::GetSelection() const
273 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
275 return m_selection_hack
;
279 void wxChoice::SetString(unsigned int n
, const wxString
& str
)
281 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
283 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
284 unsigned int count
= 0;
285 GList
*child
= menu_shell
->children
;
288 GtkBin
*bin
= GTK_BIN( child
->data
);
291 GtkLabel
*label
= (GtkLabel
*) NULL
;
293 label
= GTK_LABEL(bin
->child
);
295 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
297 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
299 gtk_label_set_text( label
, wxGTK_CONV( str
) );
301 InvalidateBestSize();
310 wxString
wxChoice::GetString(unsigned int n
) const
312 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
314 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
315 unsigned int count
= 0;
316 GList
*child
= menu_shell
->children
;
319 GtkBin
*bin
= GTK_BIN( child
->data
);
322 GtkLabel
*label
= (GtkLabel
*) NULL
;
324 label
= GTK_LABEL(bin
->child
);
326 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
328 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
330 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
336 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
338 return wxEmptyString
;
341 unsigned int wxChoice::GetCount() const
343 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
345 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
346 unsigned int count
= 0;
347 GList
*child
= menu_shell
->children
;
356 void wxChoice::SetSelection( int n
)
358 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
361 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
363 // set the local selection variable manually
364 if ((n
>= 0) && ((unsigned int)n
< GetCount()))
366 // a valid selection has been made
367 m_selection_hack
= n
;
369 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
371 // invalidates the selection if there are no items
372 // or if it is specifically set to wxNOT_FOUND
373 m_selection_hack
= wxNOT_FOUND
;
377 // this selects the first item by default if the selection is out of bounds
378 m_selection_hack
= 0;
382 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
384 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
386 gtk_widget_modify_style( m_widget
, style
);
387 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
389 GList
*child
= menu_shell
->children
;
392 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
394 GtkBin
*bin
= GTK_BIN( child
->data
);
395 GtkWidget
*label
= (GtkWidget
*) NULL
;
399 label
= GTK_BIN(m_widget
)->child
;
401 gtk_widget_modify_style( label
, style
);
407 int wxChoice::GtkAddHelper(GtkWidget
*menu
, unsigned int pos
, const wxString
& item
)
409 wxCHECK_MSG(pos
<=m_clientData
.GetCount(), -1, wxT("invalid index"));
411 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
415 // sorted control, need to insert at the correct index
416 pos
= m_strings
->Add(item
);
419 // don't call wxChoice::GetCount() from here because it doesn't work
420 // if we're called from ctor (and GtkMenuShell is still NULL)
421 if (pos
== m_clientData
.GetCount())
422 gtk_menu_shell_append( GTK_MENU_SHELL(menu
), menu_item
);
424 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, pos
);
426 m_clientData
.Insert( NULL
, pos
);
428 if (GTK_WIDGET_REALIZED(m_widget
))
430 gtk_widget_realize( menu_item
);
431 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
436 // The best size of a wxChoice should probably
437 // be changed everytime the control has been
438 // changed, but at least after adding an item
439 // it has to change. Adapted from Matt Ownby.
440 InvalidateBestSize();
442 g_signal_connect_after (menu_item
, "activate",
443 G_CALLBACK (gtk_choice_clicked_callback
),
446 gtk_widget_show( menu_item
);
448 // return the index of the item in the control
452 wxSize
wxChoice::DoGetBestSize() const
454 wxSize
ret( wxControl::DoGetBestSize() );
456 // we know better our horizontal extent: it depends on the longest string
462 unsigned int count
= GetCount();
463 for ( unsigned int n
= 0; n
< count
; n
++ )
465 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
470 // add extra for the choice "=" button
472 // VZ: I don't know how to get the right value, it seems to be in
473 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
474 // to it - maybe we can use gtk_option_menu_size_request() for this
477 // This default value works only for the default GTK+ theme (i.e.
478 // no theme at all) (FIXME)
479 static const int widthChoiceIndicator
= 35;
480 ret
.x
+= widthChoiceIndicator
;
483 // but not less than the minimal width
487 // If this request_size is called with no entries then
488 // the returned height is wrong. Give it a reasonable
491 ret
.y
= 8 + GetCharHeight();
497 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
499 return GTK_BUTTON(m_widget
)->event_window
;
504 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
506 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
510 #endif // wxUSE_CHOICE