1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "choice.h"
19 #include "wx/choice.h"
21 #include "wx/gtk/private.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 extern void wxapp_install_idle_handler();
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern bool g_blockEventsOnDrag
;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
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 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
50 int n
= choice
->GetSelection();
53 event
.SetString( choice
->GetStringSelection() );
54 event
.SetEventObject(choice
);
56 if ( choice
->HasClientObjectData() )
57 event
.SetClientObject( choice
->GetClientObject(n
) );
58 else if ( choice
->HasClientUntypedData() )
59 event
.SetClientData( choice
->GetClientData(n
) );
61 choice
->GetEventHandler()->ProcessEvent(event
);
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
72 m_strings
= (wxSortedArrayString
*)NULL
;
75 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
76 const wxPoint
&pos
, const wxSize
&size
,
77 int n
, const wxString choices
[],
78 long style
, const wxValidator
& validator
, const wxString
&name
)
81 #if (GTK_MINOR_VERSION > 0)
82 m_acceptsFocus
= TRUE
;
85 if (!PreCreation( parent
, pos
, size
) ||
86 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
88 wxFAIL_MSG( wxT("wxChoice creation failed") );
92 m_widget
= gtk_option_menu_new();
94 if ( style
& wxCB_SORT
)
96 // if our m_strings != NULL, DoAppend() will check for it and insert
97 // items in the correct order
98 m_strings
= new wxSortedArrayString
;
101 GtkWidget
*menu
= gtk_menu_new();
103 for (int i
= 0; i
< n
; i
++)
105 GtkAddHelper(menu
, i
, choices
[i
]);
108 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
110 m_parent
->DoAddChild( this );
114 SetFont( parent
->GetFont() );
118 SetBackgroundColour( parent
->GetBackgroundColour() );
119 SetForegroundColour( parent
->GetForegroundColour() );
126 wxChoice::~wxChoice()
133 int wxChoice::DoAppend( const wxString
&item
)
135 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
137 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
139 return GtkAddHelper(menu
, GetCount(), item
);
142 int wxChoice::DoInsert( const wxString
&item
, int pos
)
144 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice control") );
145 wxCHECK_MSG( (pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
147 if (pos
== GetCount())
148 return DoAppend(item
);
150 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
152 return GtkAddHelper(menu
, pos
, item
);
155 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
157 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
159 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
160 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
162 node
->SetData( (wxObject
*) clientData
);
165 void* wxChoice::DoGetItemClientData( int n
) const
167 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
169 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
170 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
172 return node
->GetData();
175 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
177 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
179 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
180 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
182 // wxItemContainer already deletes data for us
184 node
->SetData( (wxObject
*) clientData
);
187 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
189 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
191 wxList::compatibility_iterator node
= m_clientList
.Item( n
);
192 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
193 wxT("invalid index in wxChoice::DoGetItemClientObject") );
195 return (wxClientData
*) node
->GetData();
198 void wxChoice::Clear()
200 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
202 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
203 GtkWidget
*menu
= gtk_menu_new();
204 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
206 if ( HasClientObjectData() )
208 // destroy the data (due to Robert's idea of using wxList<wxObject>
209 // and not wxList<wxClientData> we can't just say
210 // m_clientList.DeleteContents(TRUE) - this would crash!
211 wxList::compatibility_iterator node
= m_clientList
.GetFirst();
214 delete (wxClientData
*)node
->GetData();
215 node
= node
->GetNext();
218 m_clientList
.Clear();
224 void wxChoice::Delete( int n
)
226 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
228 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
229 // in 2.0), hence this dump implementation - still better than nothing
233 wxCHECK_RET( n
>= 0 && n
< count
, _T("invalid index in wxChoice::Delete") );
237 for ( i
= 0; i
< count
; i
++ )
240 items
.Add(GetString(i
));
245 for ( i
= 0; i
< count
- 1; i
++ )
251 int wxChoice::FindString( const wxString
&string
) const
253 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
255 // If you read this code once and you think you understand
256 // it, then you are very wrong. Robert Roebling.
258 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
260 GList
*child
= menu_shell
->children
;
263 GtkBin
*bin
= GTK_BIN( child
->data
);
264 GtkLabel
*label
= (GtkLabel
*) NULL
;
266 label
= GTK_LABEL(bin
->child
);
268 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
270 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
273 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
275 wxString
tmp( label
->label
);
287 int wxChoice::GetSelection() const
289 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
293 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget
) );
296 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
299 GList
*child
= menu_shell
->children
;
302 GtkBin
*bin
= GTK_BIN( child
->data
);
303 if (!bin
->child
) return count
;
312 void wxChoice::SetString( int WXUNUSED(n
), const wxString
& WXUNUSED(string
) )
314 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
316 wxFAIL_MSG(wxT("not implemented"));
319 wxString
wxChoice::GetString( int n
) const
321 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
323 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
325 GList
*child
= menu_shell
->children
;
328 GtkBin
*bin
= GTK_BIN( child
->data
);
331 GtkLabel
*label
= (GtkLabel
*) NULL
;
333 label
= GTK_LABEL(bin
->child
);
335 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
337 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
340 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
342 return wxString( label
->label
);
349 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
354 int wxChoice::GetCount() const
356 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
358 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
360 GList
*child
= menu_shell
->children
;
369 void wxChoice::SetSelection( int n
)
371 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
374 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
377 void wxChoice::ApplyWidgetStyle()
381 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
383 gtk_widget_set_style( m_widget
, m_widgetStyle
);
384 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
386 GList
*child
= menu_shell
->children
;
389 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
391 GtkBin
*bin
= GTK_BIN( child
->data
);
392 GtkWidget
*label
= (GtkWidget
*) NULL
;
396 label
= BUTTON_CHILD(m_widget
);
398 gtk_widget_set_style( label
, m_widgetStyle
);
404 int wxChoice::GtkAddHelper(GtkWidget
*menu
, int pos
, const wxString
& item
)
406 wxCHECK_MSG((pos
>=0) && (pos
<=(int)m_clientList
.GetCount()), -1, wxT("invalid index"));
408 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
413 // sorted control, need to insert at the correct index
414 index
= m_strings
->Add(item
);
416 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
420 m_clientList
.Insert( m_clientList
.Item(index
- 1),
425 m_clientList
.Insert( (wxObject
*) NULL
);
430 // don't call wxChoice::GetCount() from here because it doesn't work
431 // if we're called from ctor (and GtkMenuShell is still NULL)
433 // normal control, just append
434 if (pos
== (int)m_clientList
.GetCount())
436 gtk_menu_append( GTK_MENU(menu
), menu_item
);
437 m_clientList
.Append( (wxObject
*) NULL
);
438 index
= m_clientList
.GetCount() - 1;
442 gtk_menu_insert( GTK_MENU(menu
), menu_item
, pos
);
443 m_clientList
.Insert( pos
, (wxObject
*) NULL
);
448 if (GTK_WIDGET_REALIZED(m_widget
))
450 gtk_widget_realize( menu_item
);
451 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
453 if (m_widgetStyle
) ApplyWidgetStyle();
456 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
457 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
459 gtk_widget_show( menu_item
);
461 // return the index of the item in the control
465 wxSize
wxChoice::DoGetBestSize() const
467 wxSize
ret( wxControl::DoGetBestSize() );
469 // we know better our horizontal extent: it depends on the longest string
475 size_t count
= GetCount();
476 for ( size_t n
= 0; n
< count
; n
++ )
478 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
, &m_font
);
483 // add extra for the choice "=" button
485 // VZ: I don't know how to get the right value, it seems to be in
486 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
487 // to it - maybe we can use gtk_option_menu_size_request() for this
490 // This default value works only for the default GTK+ theme (i.e.
491 // no theme at all) (FIXME)
492 static const int widthChoiceIndicator
= 35;
493 ret
.x
+= widthChoiceIndicator
;
496 // but not less than the minimal width
500 ret
.y
= 16 + GetCharHeight();
505 bool wxChoice::IsOwnGtkWindow( GdkWindow
*window
)
508 return GTK_BUTTON(m_widget
)->event_window
;
510 return (window
== m_widget
->window
);
515 #endif // wxUSE_CHOICE