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
)
99 if (!PreCreation( parent
, pos
, size
) ||
100 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
102 wxFAIL_MSG( wxT("wxChoice creation failed") );
106 m_widget
= gtk_option_menu_new();
108 if ( style
& wxCB_SORT
)
110 // if our m_strings != NULL, DoAppend() will check for it and insert
111 // items in the correct order
112 m_strings
= new wxSortedArrayString
;
115 // begin with no selection
116 m_selection_hack
= wxNOT_FOUND
;
118 GtkWidget
*menu
= gtk_menu_new();
120 for (unsigned int i
= 0; i
< (unsigned int)n
; i
++)
122 GtkAddHelper(menu
, i
, choices
[i
]);
125 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
127 m_parent
->DoAddChild( this );
130 SetInitialSize(size
); // need this too because this is a wxControlWithItems
135 wxChoice::~wxChoice()
142 int wxChoice::DoAppend( const wxString
&item
)
144 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
146 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
148 return GtkAddHelper(menu
, GetCount(), item
);
151 int wxChoice::DoInsert(const wxString
&item
, unsigned int pos
)
153 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
154 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index"));
156 if (pos
== GetCount())
157 return DoAppend(item
);
159 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
161 // if the item to insert is at or before the selection, and the selection is valid
162 if (((int)pos
<= m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
164 // move the selection forward one
168 return GtkAddHelper(menu
, pos
, item
);
171 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
173 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
175 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
176 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
178 node
->SetData( (wxObject
*) clientData
);
181 void* wxChoice::DoGetItemClientData(unsigned int n
) const
183 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
185 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
186 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
188 return node
->GetData();
191 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
193 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
195 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
196 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
198 // wxItemContainer already deletes data for us
200 node
->SetData( (wxObject
*) clientData
);
203 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
205 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
207 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
208 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
209 wxT("invalid index in wxChoice::DoGetItemClientObject") );
211 return (wxClientData
*) node
->GetData();
214 void wxChoice::Clear()
216 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
218 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
219 GtkWidget
*menu
= gtk_menu_new();
220 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
222 if ( HasClientObjectData() )
224 // destroy the data (due to Robert's idea of using wxList<wxObject>
225 // and not wxList<wxClientData> we can't just say
226 // m_clientList.DeleteContents(true) - this would crash!
227 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
230 delete (wxClientData
*)node
->GetData();
231 node
= node
->GetNext();
234 m_clientList
.Clear();
239 // begin with no selection
240 m_selection_hack
= wxNOT_FOUND
;
243 void wxChoice::Delete(unsigned int n
)
245 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
246 wxCHECK_RET( IsValid(n
), _T("invalid index in wxChoice::Delete") );
248 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
249 // in 2.0), hence this dumb implementation -- still better than nothing
251 const unsigned int count
= GetCount();
253 // if the item to delete is before the selection, and the selection is valid
254 if (((int)n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
256 // move the selection back one
259 else if ((int)n
== m_selection_hack
)
261 // invalidate the selection
262 m_selection_hack
= wxNOT_FOUND
;
265 const bool hasClientData
= m_clientDataItemsType
!= wxClientData_None
;
266 const bool hasObjectData
= m_clientDataItemsType
== wxClientData_Object
;
268 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
271 wxArrayPtrVoid itemsData
;
273 for ( i
= 0; i
< count
; i
++ )
277 items
.Add(GetString(i
));
280 // also save the client data
281 itemsData
.Add(node
->GetData());
284 else // need to delete the client object too
288 delete (wxClientData
*)node
->GetData();
294 node
= node
->GetNext();
300 // prevent Clear() from destroying all client data
301 m_clientDataItemsType
= wxClientData_None
;
306 for ( i
= 0; i
< count
- 1; i
++ )
311 SetClientObject(i
, (wxClientData
*)itemsData
[i
]);
312 else if ( hasClientData
)
313 SetClientData(i
, itemsData
[i
]);
317 int wxChoice::FindString( const wxString
&string
, bool bCase
) const
319 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
321 // If you read this code once and you think you understand
322 // it, then you are very wrong. Robert Roebling.
324 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
326 GList
*child
= menu_shell
->children
;
329 GtkBin
*bin
= GTK_BIN( child
->data
);
330 GtkLabel
*label
= (GtkLabel
*) NULL
;
332 label
= GTK_LABEL(bin
->child
);
334 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
336 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
338 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
339 if (string
.IsSameAs( tmp
, bCase
))
349 int wxChoice::GetSelection() const
351 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
353 return m_selection_hack
;
357 void wxChoice::SetString(unsigned int n
, const wxString
& str
)
359 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
361 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
362 unsigned int count
= 0;
363 GList
*child
= menu_shell
->children
;
366 GtkBin
*bin
= GTK_BIN( child
->data
);
369 GtkLabel
*label
= (GtkLabel
*) NULL
;
371 label
= GTK_LABEL(bin
->child
);
373 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
375 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
377 gtk_label_set_text( label
, wxGTK_CONV( str
) );
386 wxString
wxChoice::GetString(unsigned int n
) const
388 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
390 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
391 unsigned int count
= 0;
392 GList
*child
= menu_shell
->children
;
395 GtkBin
*bin
= GTK_BIN( child
->data
);
398 GtkLabel
*label
= (GtkLabel
*) NULL
;
400 label
= GTK_LABEL(bin
->child
);
402 label
= GTK_LABEL(GTK_BIN(m_widget
)->child
);
404 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
406 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
412 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
414 return wxEmptyString
;
417 unsigned int wxChoice::GetCount() const
419 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
421 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
422 unsigned int count
= 0;
423 GList
*child
= menu_shell
->children
;
432 void wxChoice::SetSelection( int n
)
434 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
437 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
439 // set the local selection variable manually
440 if ((n
>= 0) && ((unsigned int)n
< GetCount()))
442 // a valid selection has been made
443 m_selection_hack
= n
;
445 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
447 // invalidates the selection if there are no items
448 // or if it is specifically set to wxNOT_FOUND
449 m_selection_hack
= wxNOT_FOUND
;
453 // this selects the first item by default if the selection is out of bounds
454 m_selection_hack
= 0;
458 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
460 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
462 gtk_widget_modify_style( m_widget
, style
);
463 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
465 GList
*child
= menu_shell
->children
;
468 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
470 GtkBin
*bin
= GTK_BIN( child
->data
);
471 GtkWidget
*label
= (GtkWidget
*) NULL
;
475 label
= GTK_BIN(m_widget
)->child
;
477 gtk_widget_modify_style( label
, style
);
483 int wxChoice::GtkAddHelper(GtkWidget
*menu
, unsigned int pos
, const wxString
& item
)
485 wxCHECK_MSG(pos
<=m_clientList
.GetCount(), -1, wxT("invalid index"));
487 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
492 // sorted control, need to insert at the correct index
493 index
= m_strings
->Add(item
);
495 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, index
);
499 m_clientList
.Insert( m_clientList
.Item(index
- 1),
504 m_clientList
.Insert( (wxObject
*) NULL
);
509 // don't call wxChoice::GetCount() from here because it doesn't work
510 // if we're called from ctor (and GtkMenuShell is still NULL)
512 // normal control, just append
513 if (pos
== m_clientList
.GetCount())
515 gtk_menu_shell_append( GTK_MENU_SHELL(menu
), menu_item
);
516 m_clientList
.Append( (wxObject
*) NULL
);
517 index
= m_clientList
.GetCount() - 1;
521 gtk_menu_shell_insert( GTK_MENU_SHELL(menu
), menu_item
, pos
);
522 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
527 if (GTK_WIDGET_REALIZED(m_widget
))
529 gtk_widget_realize( menu_item
);
530 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
535 // The best size of a wxChoice should probably
536 // be changed everytime the control has been
537 // changed, but at least after adding an item
538 // it has to change. Adapted from Matt Ownby.
539 InvalidateBestSize();
541 g_signal_connect_after (menu_item
, "activate",
542 G_CALLBACK (gtk_choice_clicked_callback
),
545 gtk_widget_show( menu_item
);
547 // return the index of the item in the control
551 wxSize
wxChoice::DoGetBestSize() const
553 wxSize
ret( wxControl::DoGetBestSize() );
555 // we know better our horizontal extent: it depends on the longest string
561 unsigned int count
= GetCount();
562 for ( unsigned int n
= 0; n
< count
; n
++ )
564 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
569 // add extra for the choice "=" button
571 // VZ: I don't know how to get the right value, it seems to be in
572 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
573 // to it - maybe we can use gtk_option_menu_size_request() for this
576 // This default value works only for the default GTK+ theme (i.e.
577 // no theme at all) (FIXME)
578 static const int widthChoiceIndicator
= 35;
579 ret
.x
+= widthChoiceIndicator
;
582 // but not less than the minimal width
586 // If this request_size is called with no entries then
587 // the returned height is wrong. Give it a reasonable
590 ret
.y
= 8 + GetCharHeight();
596 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
598 return GTK_BUTTON(m_widget
)->event_window
;
603 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
605 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
609 #endif // wxUSE_CHOICE