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 // begin with no selection
115 m_selection_hack
= wxNOT_FOUND
;
117 GtkWidget
*menu
= gtk_menu_new();
119 for (int i
= 0; i
< n
; i
++)
121 GtkAddHelper(menu
, i
, choices
[i
]);
124 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
126 m_parent
->DoAddChild( this );
129 SetBestSize(size
); // need this too because this is a wxControlWithItems
134 wxChoice::~wxChoice()
141 int wxChoice::DoAppend( const wxString
&item
)
143 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
145 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
147 return GtkAddHelper(menu
, GetCount(), item
);
150 int wxChoice::DoInsert( const wxString
&item
, int pos
)
152 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
153 wxCHECK_MSG( (pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
155 if (pos
== GetCount())
156 return DoAppend(item
);
158 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
160 // if the item to insert is at or before the selection, and the selection is valid
161 if ((pos
<= m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
163 // move the selection forward one
167 return GtkAddHelper(menu
, pos
, item
);
170 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
172 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
174 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
175 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
177 node
->SetData( (wxObject
*) clientData
);
180 void* wxChoice::DoGetItemClientData( int n
) const
182 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
184 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
185 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
187 return node
->GetData();
190 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
192 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
194 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
195 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
197 // wxItemContainer already deletes data for us
199 node
->SetData( (wxObject
*) clientData
);
202 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
204 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
206 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
207 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
208 wxT("invalid index in wxChoice::DoGetItemClientObject") );
210 return (wxClientData
*) node
->GetData();
213 void wxChoice::Clear()
215 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
217 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
218 GtkWidget
*menu
= gtk_menu_new();
219 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
221 if ( HasClientObjectData() )
223 // destroy the data (due to Robert's idea of using wxList<wxObject>
224 // and not wxList<wxClientData> we can't just say
225 // m_clientList.DeleteContents(TRUE) - this would crash!
226 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
229 delete (wxClientData
*)node
->GetData();
230 node
= node
->GetNext();
233 m_clientList
.Clear();
238 // begin with no selection
239 m_selection_hack
= wxNOT_FOUND
;
242 void wxChoice::Delete( int n
)
244 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
246 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
247 // in 2.0), hence this dumb implementation -- still better than nothing
251 wxCHECK_RET( n
>= 0 && n
< count
, _T("invalid index in wxChoice::Delete") );
253 // if the item to delete is before the selection, and the selection is valid
254 if ((n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
256 // move the selection back one
259 else if (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
) const
319 wxCHECK_MSG( m_widget
!= NULL
, -1, 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( BUTTON_CHILD(m_widget
) );
336 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
339 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
341 wxString
tmp( label
->label
);
353 int wxChoice::GetSelection() const
355 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
357 // this has the same (if not better) behaviour as the following commented code
358 return m_selection_hack
;
363 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
366 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
369 GList *child = menu_shell->children;
372 GtkBin *bin = GTK_BIN( child->data );
373 if (!bin->child) return count;
383 void wxChoice::SetString( int n
, const wxString
& str
)
385 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
387 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
389 GList
*child
= menu_shell
->children
;
392 GtkBin
*bin
= GTK_BIN( child
->data
);
395 GtkLabel
*label
= (GtkLabel
*) NULL
;
397 label
= GTK_LABEL(bin
->child
);
399 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
401 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
403 gtk_label_set_text( label
, wxGTK_CONV( str
) );
412 wxString
wxChoice::GetString( int n
) const
414 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
416 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
418 GList
*child
= menu_shell
->children
;
421 GtkBin
*bin
= GTK_BIN( child
->data
);
424 GtkLabel
*label
= (GtkLabel
*) NULL
;
426 label
= GTK_LABEL(bin
->child
);
428 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
430 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
433 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
435 return wxString( label
->label
);
442 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
447 int wxChoice::GetCount() const
449 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
451 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
453 GList
*child
= menu_shell
->children
;
462 void wxChoice::SetSelection( int n
)
464 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
467 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
469 // set the local selection variable manually
470 if ((n
>= 0) && (n
< GetCount()))
472 // a valid selection has been made
473 m_selection_hack
= n
;
475 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
477 // invalidates the selection if there are no items
478 // or if it is specifically set to wxNOT_FOUND
479 m_selection_hack
= wxNOT_FOUND
;
483 // this selects the first item by default if the selection is out of bounds
484 m_selection_hack
= 0;
488 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
490 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
492 gtk_widget_modify_style( m_widget
, style
);
493 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
495 GList
*child
= menu_shell
->children
;
498 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
500 GtkBin
*bin
= GTK_BIN( child
->data
);
501 GtkWidget
*label
= (GtkWidget
*) NULL
;
505 label
= BUTTON_CHILD(m_widget
);
507 gtk_widget_modify_style( label
, style
);
513 int wxChoice::GtkAddHelper(GtkWidget
*menu
, int pos
, const wxString
& item
)
515 wxCHECK_MSG((pos
>=0) && (pos
<=(int)m_clientList
.GetCount()), -1, wxT("invalid index"));
517 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
522 // sorted control, need to insert at the correct index
523 index
= m_strings
->Add(item
);
525 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
529 m_clientList
.Insert( m_clientList
.Item(index
- 1),
534 m_clientList
.Insert( (wxObject
*) NULL
);
539 // don't call wxChoice::GetCount() from here because it doesn't work
540 // if we're called from ctor (and GtkMenuShell is still NULL)
542 // normal control, just append
543 if (pos
== (int)m_clientList
.GetCount())
545 gtk_menu_append( GTK_MENU(menu
), menu_item
);
546 m_clientList
.Append( (wxObject
*) NULL
);
547 index
= m_clientList
.GetCount() - 1;
551 gtk_menu_insert( GTK_MENU(menu
), menu_item
, pos
);
552 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
557 if (GTK_WIDGET_REALIZED(m_widget
))
559 gtk_widget_realize( menu_item
);
560 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
565 // The best size of a wxChoice should probably
566 // be changed everytime the control has been
567 // changed, but at least after adding an item
568 // it has to change. Adapted from Matt Ownby.
569 InvalidateBestSize();
571 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
572 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
574 gtk_widget_show( menu_item
);
576 // return the index of the item in the control
580 wxSize
wxChoice::DoGetBestSize() const
582 wxSize
ret( wxControl::DoGetBestSize() );
584 // we know better our horizontal extent: it depends on the longest string
590 size_t count
= GetCount();
591 for ( size_t n
= 0; n
< count
; n
++ )
593 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
598 // add extra for the choice "=" button
600 // VZ: I don't know how to get the right value, it seems to be in
601 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
602 // to it - maybe we can use gtk_option_menu_size_request() for this
605 // This default value works only for the default GTK+ theme (i.e.
606 // no theme at all) (FIXME)
607 static const int widthChoiceIndicator
= 35;
608 ret
.x
+= widthChoiceIndicator
;
611 // but not less than the minimal width
615 // If this request_size is called with no entries then
616 // the returned height is wrong. Give it a reasonable
619 ret
.y
= 8 + GetCharHeight();
625 bool wxChoice::IsOwnGtkWindow( GdkWindow
*window
)
628 return GTK_BUTTON(m_widget
)->event_window
;
630 return (window
== m_widget
->window
);
636 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
638 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
642 #endif // wxUSE_CHOICE