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 int n
, const wxString choices
[],
79 long style
, const wxValidator
& validator
, const wxString
&name
)
82 #if (GTK_MINOR_VERSION > 0)
83 m_acceptsFocus
= TRUE
;
86 if (!PreCreation( parent
, pos
, size
) ||
87 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
89 wxFAIL_MSG( wxT("wxChoice creation failed") );
93 m_widget
= gtk_option_menu_new();
95 if ( style
& wxCB_SORT
)
97 // if our m_strings != NULL, DoAppend() will check for it and insert
98 // items in the correct order
99 m_strings
= new wxSortedArrayString
;
102 GtkWidget
*menu
= gtk_menu_new();
104 for (int i
= 0; i
< n
; i
++)
106 GtkAddHelper(menu
, i
, choices
[i
]);
109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
111 m_parent
->DoAddChild( this );
123 wxChoice::~wxChoice()
130 int wxChoice::DoAppend( const wxString
&item
)
132 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
134 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
136 return GtkAddHelper(menu
, GetCount(), item
);
139 int wxChoice::DoInsert( const wxString
&item
, int pos
)
141 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
142 wxCHECK_MSG( (pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
144 if (pos
== GetCount())
145 return DoAppend(item
);
147 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
149 return GtkAddHelper(menu
, pos
, item
);
152 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
154 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
156 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
157 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
159 node
->SetData( (wxObject
*) clientData
);
162 void* wxChoice::DoGetItemClientData( int n
) const
164 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
166 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
167 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
169 return node
->GetData();
172 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
174 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
176 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
177 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
179 // wxItemContainer already deletes data for us
181 node
->SetData( (wxObject
*) clientData
);
184 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
186 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
188 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
189 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
190 wxT("invalid index in wxChoice::DoGetItemClientObject") );
192 return (wxClientData
*) node
->GetData();
195 void wxChoice::Clear()
197 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
199 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
200 GtkWidget
*menu
= gtk_menu_new();
201 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
203 if ( HasClientObjectData() )
205 // destroy the data (due to Robert's idea of using wxList<wxObject>
206 // and not wxList<wxClientData> we can't just say
207 // m_clientList.DeleteContents(TRUE) - this would crash!
208 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
211 delete (wxClientData
*)node
->GetData();
212 node
= node
->GetNext();
215 m_clientList
.Clear();
221 void wxChoice::Delete( int n
)
223 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
225 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
226 // in 2.0), hence this dumb implementation -- still better than nothing
230 wxCHECK_RET( n
>= 0 && n
< count
, _T("invalid index in wxChoice::Delete") );
232 const bool hasClientData
= m_clientDataItemsType
!= wxClientData_None
;
233 const bool hasObjectData
= m_clientDataItemsType
== wxClientData_Object
;
235 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
238 wxArrayPtrVoid itemsData
;
240 for ( i
= 0; i
< count
; i
++ )
244 items
.Add(GetString(i
));
247 // also save the client data
248 itemsData
.Add(node
->GetData());
251 else // need to delete the client object too
255 delete (wxClientData
*)node
->GetData();
261 node
= node
->GetNext();
267 // prevent Clear() from destroying all client data
268 m_clientDataItemsType
= wxClientData_None
;
273 for ( i
= 0; i
< count
- 1; i
++ )
278 SetClientObject(i
, (wxClientData
*)itemsData
[i
]);
279 else if ( hasClientData
)
280 SetClientData(i
, itemsData
[i
]);
284 int wxChoice::FindString( const wxString
&string
) const
286 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
288 // If you read this code once and you think you understand
289 // it, then you are very wrong. Robert Roebling.
291 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
293 GList
*child
= menu_shell
->children
;
296 GtkBin
*bin
= GTK_BIN( child
->data
);
297 GtkLabel
*label
= (GtkLabel
*) NULL
;
299 label
= GTK_LABEL(bin
->child
);
301 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
303 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
306 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
308 wxString
tmp( label
->label
);
320 int wxChoice::GetSelection() const
322 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
326 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget
) );
329 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
332 GList
*child
= menu_shell
->children
;
335 GtkBin
*bin
= GTK_BIN( child
->data
);
336 if (!bin
->child
) return count
;
345 void wxChoice::SetString( int n
, const wxString
& str
)
347 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
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
);
357 GtkLabel
*label
= (GtkLabel
*) NULL
;
359 label
= GTK_LABEL(bin
->child
);
361 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
363 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
365 gtk_label_set_text( label
, wxGTK_CONV( str
) );
374 wxString
wxChoice::GetString( int n
) const
376 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
378 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
380 GList
*child
= menu_shell
->children
;
383 GtkBin
*bin
= GTK_BIN( child
->data
);
386 GtkLabel
*label
= (GtkLabel
*) NULL
;
388 label
= GTK_LABEL(bin
->child
);
390 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
392 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
395 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
397 return wxString( label
->label
);
404 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
409 int wxChoice::GetCount() const
411 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
413 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
415 GList
*child
= menu_shell
->children
;
424 void wxChoice::SetSelection( int n
)
426 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
429 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
432 void wxChoice::ApplyWidgetStyle()
436 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
438 gtk_widget_set_style( m_widget
, m_widgetStyle
);
439 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
441 GList
*child
= menu_shell
->children
;
444 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
446 GtkBin
*bin
= GTK_BIN( child
->data
);
447 GtkWidget
*label
= (GtkWidget
*) NULL
;
451 label
= BUTTON_CHILD(m_widget
);
453 gtk_widget_set_style( label
, m_widgetStyle
);
459 int wxChoice::GtkAddHelper(GtkWidget
*menu
, int pos
, const wxString
& item
)
461 wxCHECK_MSG((pos
>=0) && (pos
<=(int)m_clientList
.GetCount()), -1, wxT("invalid index"));
463 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
468 // sorted control, need to insert at the correct index
469 index
= m_strings
->Add(item
);
471 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
475 m_clientList
.Insert( m_clientList
.Item(index
- 1),
480 m_clientList
.Insert( (wxObject
*) NULL
);
485 // don't call wxChoice::GetCount() from here because it doesn't work
486 // if we're called from ctor (and GtkMenuShell is still NULL)
488 // normal control, just append
489 if (pos
== (int)m_clientList
.GetCount())
491 gtk_menu_append( GTK_MENU(menu
), menu_item
);
492 m_clientList
.Append( (wxObject
*) NULL
);
493 index
= m_clientList
.GetCount() - 1;
497 gtk_menu_insert( GTK_MENU(menu
), menu_item
, pos
);
498 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
503 if (GTK_WIDGET_REALIZED(m_widget
))
505 gtk_widget_realize( menu_item
);
506 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
508 if (m_widgetStyle
) ApplyWidgetStyle();
511 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
512 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
514 gtk_widget_show( menu_item
);
516 // return the index of the item in the control
520 wxSize
wxChoice::DoGetBestSize() const
522 wxSize
ret( wxControl::DoGetBestSize() );
524 // we know better our horizontal extent: it depends on the longest string
530 size_t count
= GetCount();
531 for ( size_t n
= 0; n
< count
; n
++ )
533 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
, &m_font
);
538 // add extra for the choice "=" button
540 // VZ: I don't know how to get the right value, it seems to be in
541 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
542 // to it - maybe we can use gtk_option_menu_size_request() for this
545 // This default value works only for the default GTK+ theme (i.e.
546 // no theme at all) (FIXME)
547 static const int widthChoiceIndicator
= 35;
548 ret
.x
+= widthChoiceIndicator
;
551 // but not less than the minimal width
555 ret
.y
= 16 + GetCharHeight();
560 bool wxChoice::IsOwnGtkWindow( GdkWindow
*window
)
563 return GTK_BUTTON(m_widget
)->event_window
;
565 return (window
== m_widget
->window
);
570 #endif // wxUSE_CHOICE