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 //-----------------------------------------------------------------------------
41 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
44 wxapp_install_idle_handler();
46 if (!choice
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
51 int n
= choice
->GetSelection();
54 event
.SetString( choice
->GetStringSelection() );
55 event
.SetEventObject(choice
);
57 if ( choice
->HasClientObjectData() )
58 event
.SetClientObject( choice
->GetClientObject(n
) );
59 else if ( choice
->HasClientUntypedData() )
60 event
.SetClientData( choice
->GetClientData(n
) );
62 choice
->GetEventHandler()->ProcessEvent(event
);
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
73 m_strings
= (wxSortedArrayString
*)NULL
;
76 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
77 const wxPoint
&pos
, const wxSize
&size
,
78 const wxArrayString
& choices
,
79 long style
, const wxValidator
& validator
,
80 const wxString
&name
)
82 wxCArrayString
chs(choices
);
84 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
85 style
, validator
, name
);
88 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
89 const wxPoint
&pos
, const wxSize
&size
,
90 int n
, const wxString choices
[],
91 long style
, const wxValidator
& validator
, const wxString
&name
)
94 #if (GTK_MINOR_VERSION > 0)
95 m_acceptsFocus
= TRUE
;
98 if (!PreCreation( parent
, pos
, size
) ||
99 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
101 wxFAIL_MSG( wxT("wxChoice creation failed") );
105 m_widget
= gtk_option_menu_new();
107 if ( style
& wxCB_SORT
)
109 // if our m_strings != NULL, DoAppend() will check for it and insert
110 // items in the correct order
111 m_strings
= new wxSortedArrayString
;
114 GtkWidget
*menu
= gtk_menu_new();
116 for (int i
= 0; i
< n
; i
++)
118 GtkAddHelper(menu
, i
, choices
[i
]);
121 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
123 m_parent
->DoAddChild( this );
126 SetBestSize(size
); // need this too because this is a wxControlWithItems
131 wxChoice::~wxChoice()
138 int wxChoice::DoAppend( const wxString
&item
)
140 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
142 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
144 return GtkAddHelper(menu
, GetCount(), item
);
147 int wxChoice::DoInsert( const wxString
&item
, int pos
)
149 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
150 wxCHECK_MSG( (pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
152 if (pos
== GetCount())
153 return DoAppend(item
);
155 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
157 return GtkAddHelper(menu
, pos
, item
);
160 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
162 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
164 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
165 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
167 node
->SetData( (wxObject
*) clientData
);
170 void* wxChoice::DoGetItemClientData( int n
) const
172 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
174 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
175 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
177 return node
->GetData();
180 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
182 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
184 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
185 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
187 // wxItemContainer already deletes data for us
189 node
->SetData( (wxObject
*) clientData
);
192 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
194 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
196 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
197 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
198 wxT("invalid index in wxChoice::DoGetItemClientObject") );
200 return (wxClientData
*) node
->GetData();
203 void wxChoice::Clear()
205 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
207 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
208 GtkWidget
*menu
= gtk_menu_new();
209 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
211 if ( HasClientObjectData() )
213 // destroy the data (due to Robert's idea of using wxList<wxObject>
214 // and not wxList<wxClientData> we can't just say
215 // m_clientList.DeleteContents(TRUE) - this would crash!
216 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
219 delete (wxClientData
*)node
->GetData();
220 node
= node
->GetNext();
223 m_clientList
.Clear();
229 void wxChoice::Delete( int n
)
231 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
233 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
234 // in 2.0), hence this dumb implementation -- still better than nothing
238 wxCHECK_RET( n
>= 0 && n
< count
, _T("invalid index in wxChoice::Delete") );
240 const bool hasClientData
= m_clientDataItemsType
!= wxClientData_None
;
241 const bool hasObjectData
= m_clientDataItemsType
== wxClientData_Object
;
243 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
246 wxArrayPtrVoid itemsData
;
248 for ( i
= 0; i
< count
; i
++ )
252 items
.Add(GetString(i
));
255 // also save the client data
256 itemsData
.Add(node
->GetData());
259 else // need to delete the client object too
263 delete (wxClientData
*)node
->GetData();
269 node
= node
->GetNext();
275 // prevent Clear() from destroying all client data
276 m_clientDataItemsType
= wxClientData_None
;
281 for ( i
= 0; i
< count
- 1; i
++ )
286 SetClientObject(i
, (wxClientData
*)itemsData
[i
]);
287 else if ( hasClientData
)
288 SetClientData(i
, itemsData
[i
]);
292 int wxChoice::FindString( const wxString
&string
) const
294 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
296 // If you read this code once and you think you understand
297 // it, then you are very wrong. Robert Roebling.
299 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
301 GList
*child
= menu_shell
->children
;
304 GtkBin
*bin
= GTK_BIN( child
->data
);
305 GtkLabel
*label
= (GtkLabel
*) NULL
;
307 label
= GTK_LABEL(bin
->child
);
309 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
311 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
314 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
316 wxString
tmp( label
->label
);
328 int wxChoice::GetSelection() const
330 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
334 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget
) );
337 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
340 GList
*child
= menu_shell
->children
;
343 GtkBin
*bin
= GTK_BIN( child
->data
);
344 if (!bin
->child
) return count
;
353 void wxChoice::SetString( int n
, const wxString
& str
)
355 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
357 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
359 GList
*child
= menu_shell
->children
;
362 GtkBin
*bin
= GTK_BIN( child
->data
);
365 GtkLabel
*label
= (GtkLabel
*) NULL
;
367 label
= GTK_LABEL(bin
->child
);
369 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
371 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
373 gtk_label_set_text( label
, wxGTK_CONV( str
) );
382 wxString
wxChoice::GetString( int n
) const
384 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
386 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
388 GList
*child
= menu_shell
->children
;
391 GtkBin
*bin
= GTK_BIN( child
->data
);
394 GtkLabel
*label
= (GtkLabel
*) NULL
;
396 label
= GTK_LABEL(bin
->child
);
398 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
400 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
403 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
405 return wxString( label
->label
);
412 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
417 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
) ) );
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
);
440 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
442 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
444 gtk_widget_modify_style( m_widget
, style
);
445 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
447 GList
*child
= menu_shell
->children
;
450 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
452 GtkBin
*bin
= GTK_BIN( child
->data
);
453 GtkWidget
*label
= (GtkWidget
*) NULL
;
457 label
= BUTTON_CHILD(m_widget
);
459 gtk_widget_modify_style( label
, style
);
465 int wxChoice::GtkAddHelper(GtkWidget
*menu
, int pos
, const wxString
& item
)
467 wxCHECK_MSG((pos
>=0) && (pos
<=(int)m_clientList
.GetCount()), -1, wxT("invalid index"));
469 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
474 // sorted control, need to insert at the correct index
475 index
= m_strings
->Add(item
);
477 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
481 m_clientList
.Insert( m_clientList
.Item(index
- 1),
486 m_clientList
.Insert( (wxObject
*) NULL
);
491 // don't call wxChoice::GetCount() from here because it doesn't work
492 // if we're called from ctor (and GtkMenuShell is still NULL)
494 // normal control, just append
495 if (pos
== (int)m_clientList
.GetCount())
497 gtk_menu_append( GTK_MENU(menu
), menu_item
);
498 m_clientList
.Append( (wxObject
*) NULL
);
499 index
= m_clientList
.GetCount() - 1;
503 gtk_menu_insert( GTK_MENU(menu
), menu_item
, pos
);
504 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
509 if (GTK_WIDGET_REALIZED(m_widget
))
511 gtk_widget_realize( menu_item
);
512 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
517 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
518 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
520 gtk_widget_show( menu_item
);
522 // return the index of the item in the control
526 wxSize
wxChoice::DoGetBestSize() const
528 wxSize
ret( wxControl::DoGetBestSize() );
530 // we know better our horizontal extent: it depends on the longest string
536 size_t count
= GetCount();
537 for ( size_t n
= 0; n
< count
; n
++ )
539 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
544 // add extra for the choice "=" button
546 // VZ: I don't know how to get the right value, it seems to be in
547 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
548 // to it - maybe we can use gtk_option_menu_size_request() for this
551 // This default value works only for the default GTK+ theme (i.e.
552 // no theme at all) (FIXME)
553 static const int widthChoiceIndicator
= 35;
554 ret
.x
+= widthChoiceIndicator
;
557 // but not less than the minimal width
561 // If this request_size is called with no entries then
562 // the returned height is wrong. Give it a reasonable
565 ret
.y
= 8 + GetCharHeight();
570 bool wxChoice::IsOwnGtkWindow( GdkWindow
*window
)
573 return GTK_BUTTON(m_widget
)->event_window
;
575 return (window
== m_widget
->window
);
581 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
583 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
587 #endif // wxUSE_CHOICE