1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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 #include "wx/gtk1/private.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
40 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
43 wxapp_install_idle_handler();
45 if (!choice
->m_hasVMT
) return;
47 if (g_blockEventsOnDrag
) return;
49 int selection
= wxNOT_FOUND
;
51 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(choice
->GetHandle()) ) );
54 GList
*child
= menu_shell
->children
;
57 GtkBin
*bin
= GTK_BIN( child
->data
);
67 choice
->m_selection_hack
= selection
;
69 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
70 int n
= choice
->GetSelection();
73 event
.SetString( choice
->GetStringSelection() );
74 event
.SetEventObject(choice
);
76 if ( choice
->HasClientObjectData() )
77 event
.SetClientObject( choice
->GetClientObject(n
) );
78 else if ( choice
->HasClientUntypedData() )
79 event
.SetClientData( choice
->GetClientData(n
) );
81 choice
->HandleWindowEvent(event
);
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControlWithItems
)
93 m_strings
= (wxSortedArrayString
*)NULL
;
96 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
97 const wxPoint
&pos
, const wxSize
&size
,
98 const wxArrayString
& choices
,
99 long style
, const wxValidator
& validator
,
100 const wxString
&name
)
102 wxCArrayString
chs(choices
);
104 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
105 style
, validator
, name
);
108 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
109 const wxPoint
&pos
, const wxSize
&size
,
110 int n
, const wxString choices
[],
111 long style
, const wxValidator
& validator
, const wxString
&name
)
114 #if (GTK_MINOR_VERSION > 0)
115 m_acceptsFocus
= true;
118 if (!PreCreation( parent
, pos
, size
) ||
119 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
121 wxFAIL_MSG( wxT("wxChoice creation failed") );
125 m_widget
= gtk_option_menu_new();
127 if ( style
& wxCB_SORT
)
129 // if our m_strings != NULL, Append() will check for it and insert
130 // items in the correct order
131 m_strings
= new wxSortedArrayString
;
134 // begin with no selection
135 m_selection_hack
= wxNOT_FOUND
;
137 GtkWidget
*menu
= gtk_menu_new();
139 for (unsigned int i
= 0; i
< (unsigned int)n
; i
++)
141 GtkAddHelper(menu
, i
, choices
[i
]);
144 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
146 m_parent
->DoAddChild( this );
149 SetInitialSize(size
); // need this too because this is a wxControlWithItems
154 wxChoice::~wxChoice()
161 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
163 void **clientData
, wxClientDataType type
)
165 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
167 const unsigned int count
= items
.GetCount();
169 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
171 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
173 int n
= GtkAddHelper(menu
, pos
, items
[i
]);
174 if ( n
== wxNOT_FOUND
)
177 AssignNewItemClientData(n
, clientData
, i
, type
);
180 // if the item to insert is at or before the selection, and the selection is valid
181 if (((int)pos
<= m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
183 // move the selection forward
184 m_selection_hack
+= count
;
190 void wxChoice::DoSetItemClientData(unsigned int n
, void* 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::DoSetItemClientData") );
197 node
->SetData( (wxObject
*) clientData
);
200 void* wxChoice::DoGetItemClientData(unsigned int n
) const
202 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
204 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
205 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
207 return node
->GetData();
210 void wxChoice::DoClear()
212 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
214 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
215 GtkWidget
*menu
= gtk_menu_new();
216 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
218 m_clientList
.Clear();
223 // begin with no selection
224 m_selection_hack
= wxNOT_FOUND
;
227 void wxChoice::DoDeleteOneItem(unsigned int n
)
229 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
231 wxCHECK_RET( IsValid(n
), _T("invalid index in wxChoice::Delete") );
233 // if the item to delete is before the selection, and the selection is valid
234 if (((int)n
< m_selection_hack
) && (m_selection_hack
!= wxNOT_FOUND
))
236 // move the selection back one
239 else if ((int)n
== m_selection_hack
)
241 // invalidate the selection
242 m_selection_hack
= wxNOT_FOUND
;
245 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
246 // in 2.0), hence this dumb implementation -- still better than nothing
247 const unsigned int count
= GetCount();
249 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
252 wxArrayPtrVoid itemsData
;
254 for ( unsigned i
= 0; i
< count
; i
++, node
= node
->GetNext() )
258 items
.Add(GetString(i
));
259 itemsData
.Add(node
->GetData());
265 void ** const data
= &itemsData
[0];
266 if ( HasClientObjectData() )
267 Append(items
, reinterpret_cast<wxClientData
**>(data
));
272 int wxChoice::FindString( const wxString
&string
, bool bCase
) const
274 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
276 // If you read this code once and you think you understand
277 // it, then you are very wrong. Robert Roebling.
279 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
281 GList
*child
= menu_shell
->children
;
284 GtkBin
*bin
= GTK_BIN( child
->data
);
285 GtkLabel
*label
= (GtkLabel
*) NULL
;
287 label
= GTK_LABEL(bin
->child
);
289 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
291 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
293 wxString
tmp( label
->label
);
295 if (string
.IsSameAs( tmp
, bCase
))
305 int wxChoice::GetSelection() const
307 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid choice") );
309 return m_selection_hack
;
313 void wxChoice::SetString(unsigned int n
, const wxString
& str
)
315 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
317 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
318 unsigned int count
= 0;
319 GList
*child
= menu_shell
->children
;
322 GtkBin
*bin
= GTK_BIN( child
->data
);
325 GtkLabel
*label
= (GtkLabel
*) NULL
;
327 label
= GTK_LABEL(bin
->child
);
329 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
331 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
333 gtk_label_set_text( label
, wxGTK_CONV( str
) );
342 wxString
wxChoice::GetString(unsigned int n
) const
344 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid choice") );
346 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
347 unsigned int count
= 0;
348 GList
*child
= menu_shell
->children
;
351 GtkBin
*bin
= GTK_BIN( child
->data
);
354 GtkLabel
*label
= (GtkLabel
*) NULL
;
356 label
= GTK_LABEL(bin
->child
);
358 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
360 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
362 return wxString( label
->label
);
368 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
370 return wxEmptyString
;
373 unsigned int wxChoice::GetCount() const
375 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
377 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
378 unsigned int count
= 0;
379 GList
*child
= menu_shell
->children
;
388 void wxChoice::SetSelection( int n
)
390 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
393 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
395 // set the local selection variable manually
396 if (IsValid((unsigned int)n
))
398 // a valid selection has been made
399 m_selection_hack
= n
;
401 else if ((n
== wxNOT_FOUND
) || (GetCount() == 0))
403 // invalidates the selection if there are no items
404 // or if it is specifically set to wxNOT_FOUND
405 m_selection_hack
= wxNOT_FOUND
;
409 // this selects the first item by default if the selection is out of bounds
410 m_selection_hack
= 0;
414 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
416 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
418 gtk_widget_modify_style( m_widget
, style
);
419 gtk_widget_modify_style( GTK_WIDGET( menu_shell
), style
);
421 GList
*child
= menu_shell
->children
;
424 gtk_widget_modify_style( GTK_WIDGET( child
->data
), style
);
426 GtkBin
*bin
= GTK_BIN( child
->data
);
427 GtkWidget
*label
= (GtkWidget
*) NULL
;
431 label
= BUTTON_CHILD(m_widget
);
433 gtk_widget_modify_style( label
, style
);
439 int wxChoice::GtkAddHelper(GtkWidget
*menu
, unsigned int pos
, const wxString
& item
)
441 wxCHECK_MSG(pos
<=m_clientList
.GetCount(), -1, wxT("invalid index"));
443 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
448 // sorted control, need to insert at the correct index
449 index
= m_strings
->Add(item
);
451 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
455 m_clientList
.Insert( m_clientList
.Item(index
- 1),
460 m_clientList
.Insert( (wxObject
*) NULL
);
465 // don't call wxChoice::GetCount() from here because it doesn't work
466 // if we're called from ctor (and GtkMenuShell is still NULL)
468 // normal control, just append
469 if (pos
== m_clientList
.GetCount())
471 gtk_menu_append( GTK_MENU(menu
), menu_item
);
472 m_clientList
.Append( (wxObject
*) NULL
);
473 index
= m_clientList
.GetCount() - 1;
477 gtk_menu_insert( GTK_MENU(menu
), menu_item
, pos
);
478 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
483 if (GTK_WIDGET_REALIZED(m_widget
))
485 gtk_widget_realize( menu_item
);
486 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
491 // The best size of a wxChoice should probably
492 // be changed everytime the control has been
493 // changed, but at least after adding an item
494 // it has to change. Adapted from Matt Ownby.
495 InvalidateBestSize();
497 gtk_signal_connect_after( GTK_OBJECT( menu_item
), "activate",
498 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
500 gtk_widget_show( menu_item
);
502 // return the index of the item in the control
506 wxSize
wxChoice::DoGetBestSize() const
508 wxSize
ret( wxControl::DoGetBestSize() );
510 // we know better our horizontal extent: it depends on the longest string
516 unsigned int count
= GetCount();
517 for ( unsigned int n
= 0; n
< count
; n
++ )
519 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
524 // add extra for the choice "=" button
526 // VZ: I don't know how to get the right value, it seems to be in
527 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
528 // to it - maybe we can use gtk_option_menu_size_request() for this
531 // This default value works only for the default GTK+ theme (i.e.
532 // no theme at all) (FIXME)
533 static const int widthChoiceIndicator
= 35;
534 ret
.x
+= widthChoiceIndicator
;
537 // but not less than the minimal width
541 // If this request_size is called with no entries then
542 // the returned height is wrong. Give it a reasonable
545 ret
.y
= 8 + GetCharHeight();
551 bool wxChoice::IsOwnGtkWindow( GdkWindow
*window
)
553 return (window
== m_widget
->window
);
558 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
560 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new
);
564 #endif // wxUSE_CHOICE