1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "choice.h"
19 #include "wx/choice.h"
20 #include "wx/arrstr.h"
22 #include "wx/gtk/private.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
42 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
45 wxapp_install_idle_handler();
47 if (!choice
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 int selection
= wxNOT_FOUND
;
54 selection
= gtk_option_menu_get_history( GTK_OPTION_MENU(choice
->GetHandle()) );
56 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(choice
->GetHandle()) ) );
59 GList
*child
= menu_shell
->children
;
62 GtkBin
*bin
= GTK_BIN( child
->data
);
72 choice
->m_selection_hack
= selection
;
74 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
75 int n
= choice
->GetSelection();
78 event
.SetString( choice
->GetStringSelection() );
79 event
.SetEventObject(choice
);
81 if ( choice
->HasClientObjectData() )
82 event
.SetClientObject( choice
->GetClientObject(n
) );
83 else if ( choice
->HasClientUntypedData() )
84 event
.SetClientData( choice
->GetClientData(n
) );
86 choice
->GetEventHandler()->ProcessEvent(event
);
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
98 m_strings
= (wxSortedArrayString
*)NULL
;
101 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
102 const wxPoint
&pos
, const wxSize
&size
,
103 const wxArrayString
& choices
,
104 long style
, const wxValidator
& validator
,
105 const wxString
&name
)
107 wxCArrayString
chs(choices
);
109 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
110 style
, validator
, name
);
113 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
114 const wxPoint
&pos
, const wxSize
&size
,
115 int n
, const wxString choices
[],
116 long style
, const wxValidator
& validator
, const wxString
&name
)
119 #if (GTK_MINOR_VERSION > 0)
120 m_acceptsFocus
= true;
123 if (!PreCreation( parent
, pos
, size
) ||
124 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
126 wxFAIL_MSG( wxT("wxChoice creation failed") );
130 m_widget
= gtk_option_menu_new();
132 if ( style
& wxCB_SORT
)
134 // if our m_strings != NULL, DoAppend() will check for it and insert
135 // items in the correct order
136 m_strings
= new wxSortedArrayString
;
139 // begin with no selection
140 m_selection_hack
= wxNOT_FOUND
;
142 GtkWidget
*menu
= gtk_menu_new();
144 for (int i
= 0; i
< n
; i
++)
146 GtkAddHelper(menu
, i
, choices
[i
]);
149 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
151 m_parent
->DoAddChild( this );
154 SetBestSize(size
); // need this too because this is a wxControlWithItems
159 wxChoice::~wxChoice()
166 int wxChoice::DoAppend( const wxString
&item
)
168 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
170 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
172 return GtkAddHelper(menu
, GetCount(), item
);
175 int wxChoice::DoInsert( const wxString
&item
, int pos
)
177 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
178 wxCHECK_MSG( (pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
180 if (pos
== GetCount())
181 return DoAppend(item
);
183 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
185 // if the item to insert is at or before the selection, and the selection is valid
186 if ((pos
<= m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
188 // move the selection forward one
192 return GtkAddHelper(menu
, pos
, item
);
195 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
197 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
199 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
200 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
202 node
->SetData( (wxObject
*) clientData
);
205 void* wxChoice::DoGetItemClientData( int n
) const
207 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
209 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
210 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
212 return node
->GetData();
215 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
217 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
219 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
220 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
222 // wxItemContainer already deletes data for us
224 node
->SetData( (wxObject
*) clientData
);
227 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
229 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
231 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
232 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
233 wxT("invalid index in wxChoice::DoGetItemClientObject") );
235 return (wxClientData
*) node
->GetData();
238 void wxChoice::Clear()
240 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
242 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
243 GtkWidget
*menu
= gtk_menu_new();
244 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
246 if ( HasClientObjectData() )
248 // destroy the data (due to Robert's idea of using wxList<wxObject>
249 // and not wxList<wxClientData> we can't just say
250 // m_clientList.DeleteContents(true) - this would crash!
251 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
254 delete (wxClientData
*)node
->GetData();
255 node
= node
->GetNext();
258 m_clientList
.Clear();
263 // begin with no selection
264 m_selection_hack
= wxNOT_FOUND
;
267 void wxChoice::Delete( int n
)
269 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
271 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
272 // in 2.0), hence this dumb implementation -- still better than nothing
276 wxCHECK_RET( n
>= 0 && n
< count
, _T("invalid index in wxChoice::Delete") );
278 // if the item to delete is before the selection, and the selection is valid
279 if ((n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
281 // move the selection back one
284 else if (n
== m_selection_hack
)
286 // invalidate the selection
287 m_selection_hack
= wxNOT_FOUND
;
290 const bool hasClientData
= m_clientDataItemsType
!= wxClientData_None
;
291 const bool hasObjectData
= m_clientDataItemsType
== wxClientData_Object
;
293 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
296 wxArrayPtrVoid itemsData
;
298 for ( i
= 0; i
< count
; i
++ )
302 items
.Add(GetString(i
));
305 // also save the client data
306 itemsData
.Add(node
->GetData());
309 else // need to delete the client object too
313 delete (wxClientData
*)node
->GetData();
319 node
= node
->GetNext();
325 // prevent Clear() from destroying all client data
326 m_clientDataItemsType
= wxClientData_None
;
331 for ( i
= 0; i
< count
- 1; i
++ )
336 SetClientObject(i
, (wxClientData
*)itemsData
[i
]);
337 else if ( hasClientData
)
338 SetClientData(i
, itemsData
[i
]);
342 int wxChoice::FindString( const wxString
&string
) const
344 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
346 // If you read this code once and you think you understand
347 // it, then you are very wrong. Robert Roebling.
349 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
351 GList
*child
= menu_shell
->children
;
354 GtkBin
*bin
= GTK_BIN( child
->data
);
355 GtkLabel
*label
= (GtkLabel
*) NULL
;
357 label
= GTK_LABEL(bin
->child
);
359 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
361 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
364 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
366 wxString
tmp( label
->label
);
378 int wxChoice::GetSelection() const
380 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
382 return m_selection_hack
;
386 void wxChoice::SetString( int n
, const wxString
& str
)
388 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
390 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
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( BUTTON_CHILD(m_widget
) );
404 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
406 gtk_label_set_text( label
, wxGTK_CONV( str
) );
415 wxString
wxChoice::GetString( int n
) const
417 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
419 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
421 GList
*child
= menu_shell
->children
;
424 GtkBin
*bin
= GTK_BIN( child
->data
);
427 GtkLabel
*label
= (GtkLabel
*) NULL
;
429 label
= GTK_LABEL(bin
->child
);
431 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
433 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
436 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
438 return wxString( label
->label
);
445 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
447 return wxEmptyString
;
450 int wxChoice::GetCount() const
452 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
454 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
456 GList
*child
= menu_shell
->children
;
465 void wxChoice::SetSelection( int n
)
467 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
470 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
472 // set the local selection variable manually
473 if ((n
>= 0) && (n
< GetCount()))
475 // a valid selection has been made
476 m_selection_hack
= n
;
478 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
480 // invalidates the selection if there are no items
481 // or if it is specifically set to wxNOT_FOUND
482 m_selection_hack
= wxNOT_FOUND
;
486 // this selects the first item by default if the selection is out of bounds
487 m_selection_hack
= 0;
491 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
493 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
495 gtk_widget_modify_style( m_widget
, style
);
496 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
498 GList
*child
= menu_shell
->children
;
501 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
503 GtkBin
*bin
= GTK_BIN( child
->data
);
504 GtkWidget
*label
= (GtkWidget
*) NULL
;
508 label
= BUTTON_CHILD(m_widget
);
510 gtk_widget_modify_style( label
, style
);
516 int wxChoice::GtkAddHelper(GtkWidget
*menu
, int pos
, const wxString
& item
)
518 wxCHECK_MSG((pos
>=0) && (pos
<=(int)m_clientList
.GetCount()), -1, wxT("invalid index"));
520 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
525 // sorted control, need to insert at the correct index
526 index
= m_strings
->Add(item
);
528 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
532 m_clientList
.Insert( m_clientList
.Item(index
- 1),
537 m_clientList
.Insert( (wxObject
*) NULL
);
542 // don't call wxChoice::GetCount() from here because it doesn't work
543 // if we're called from ctor (and GtkMenuShell is still NULL)
545 // normal control, just append
546 if (pos
== (int)m_clientList
.GetCount())
548 gtk_menu_append( GTK_MENU(menu
), menu_item
);
549 m_clientList
.Append( (wxObject
*) NULL
);
550 index
= m_clientList
.GetCount() - 1;
554 gtk_menu_insert( GTK_MENU(menu
), menu_item
, pos
);
555 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
560 if (GTK_WIDGET_REALIZED(m_widget
))
562 gtk_widget_realize( menu_item
);
563 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
568 // The best size of a wxChoice should probably
569 // be changed everytime the control has been
570 // changed, but at least after adding an item
571 // it has to change. Adapted from Matt Ownby.
572 InvalidateBestSize();
574 gtk_signal_connect_after( GTK_OBJECT( menu_item
), "activate",
575 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
577 gtk_widget_show( menu_item
);
579 // return the index of the item in the control
583 wxSize
wxChoice::DoGetBestSize() const
585 wxSize
ret( wxControl::DoGetBestSize() );
587 // we know better our horizontal extent: it depends on the longest string
593 size_t count
= GetCount();
594 for ( size_t n
= 0; n
< count
; n
++ )
596 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
601 // add extra for the choice "=" button
603 // VZ: I don't know how to get the right value, it seems to be in
604 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
605 // to it - maybe we can use gtk_option_menu_size_request() for this
608 // This default value works only for the default GTK+ theme (i.e.
609 // no theme at all) (FIXME)
610 static const int widthChoiceIndicator
= 35;
611 ret
.x
+= widthChoiceIndicator
;
614 // but not less than the minimal width
618 // If this request_size is called with no entries then
619 // the returned height is wrong. Give it a reasonable
622 ret
.y
= 8 + GetCharHeight();
628 bool wxChoice::IsOwnGtkWindow( GdkWindow
*window
)
631 return GTK_BUTTON(m_widget
)->event_window
;
633 return (window
== m_widget
->window
);
639 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
641 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
645 #endif // wxUSE_CHOICE