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 GtkAppendHelper(menu
, 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 GtkAppendHelper(menu
, item
);
142 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
144 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
146 wxNode
*node
= m_clientList
.Nth( n
);
147 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
149 node
->SetData( (wxObject
*) clientData
);
152 void* wxChoice::DoGetItemClientData( int n
) const
154 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
156 wxNode
*node
= m_clientList
.Nth( n
);
157 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
162 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
164 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
166 wxNode
*node
= m_clientList
.Nth( n
);
167 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
169 // wxItemContainer already deletes data for us
171 node
->SetData( (wxObject
*) clientData
);
174 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
176 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
178 wxNode
*node
= m_clientList
.Nth( n
);
179 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
180 wxT("invalid index in wxChoice::DoGetItemClientObject") );
182 return (wxClientData
*) node
->Data();
185 void wxChoice::Clear()
187 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
189 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
190 GtkWidget
*menu
= gtk_menu_new();
191 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
193 if ( HasClientObjectData() )
195 // destroy the data (due to Robert's idea of using wxList<wxObject>
196 // and not wxList<wxClientData> we can't just say
197 // m_clientList.DeleteContents(TRUE) - this would crash!
198 wxNode
*node
= m_clientList
.First();
201 delete (wxClientData
*)node
->Data();
205 m_clientList
.Clear();
211 void wxChoice::Delete( int n
)
213 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
215 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
216 // in 2.0), hence this dump implementation - still better than nothing
220 wxCHECK_RET( n
>= 0 && n
< count
, _T("invalid index in wxChoice::Delete") );
224 for ( i
= 0; i
< count
; i
++ )
227 items
.Add(GetString(i
));
232 for ( i
= 0; i
< count
- 1; i
++ )
238 int wxChoice::FindString( const wxString
&string
) const
240 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
242 // If you read this code once and you think you understand
243 // it, then you are very wrong. Robert Roebling.
245 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
247 GList
*child
= menu_shell
->children
;
250 GtkBin
*bin
= GTK_BIN( child
->data
);
251 GtkLabel
*label
= (GtkLabel
*) NULL
;
253 label
= GTK_LABEL(bin
->child
);
255 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
257 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
260 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
262 wxString
tmp( label
->label
);
274 int wxChoice::GetSelection() const
276 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
280 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget
) );
283 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
286 GList
*child
= menu_shell
->children
;
289 GtkBin
*bin
= GTK_BIN( child
->data
);
290 if (!bin
->child
) return count
;
299 void wxChoice::SetString( int WXUNUSED(n
), const wxString
& WXUNUSED(string
) )
301 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
303 wxFAIL_MSG(wxT("not implemented"));
306 wxString
wxChoice::GetString( int n
) const
308 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
310 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
312 GList
*child
= menu_shell
->children
;
315 GtkBin
*bin
= GTK_BIN( child
->data
);
318 GtkLabel
*label
= (GtkLabel
*) NULL
;
320 label
= GTK_LABEL(bin
->child
);
322 label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
324 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
327 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) );
329 return wxString( label
->label
);
336 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
341 int wxChoice::GetCount() const
343 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
345 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
347 GList
*child
= menu_shell
->children
;
356 void wxChoice::SetSelection( int n
)
358 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
361 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
364 void wxChoice::ApplyWidgetStyle()
368 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
370 gtk_widget_set_style( m_widget
, m_widgetStyle
);
371 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
373 GList
*child
= menu_shell
->children
;
376 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
378 GtkBin
*bin
= GTK_BIN( child
->data
);
379 GtkWidget
*label
= (GtkWidget
*) NULL
;
383 label
= BUTTON_CHILD(m_widget
);
385 gtk_widget_set_style( label
, m_widgetStyle
);
391 size_t wxChoice::GtkAppendHelper(GtkWidget
*menu
, const wxString
& item
)
393 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( wxGTK_CONV( item
) );
398 // sorted control, need to insert at the correct index
399 index
= m_strings
->Add(item
);
401 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
405 m_clientList
.Insert( m_clientList
.Item(index
- 1),
410 m_clientList
.Insert( (wxObject
*) NULL
);
415 // normal control, just append
416 gtk_menu_append( GTK_MENU(menu
), menu_item
);
418 m_clientList
.Append( (wxObject
*) NULL
);
420 // don't call wxChoice::GetCount() from here because it doesn't work
421 // if we're called from ctor (and GtkMenuShell is still NULL)
422 index
= m_clientList
.GetCount() - 1;
425 if (GTK_WIDGET_REALIZED(m_widget
))
427 gtk_widget_realize( menu_item
);
428 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
430 if (m_widgetStyle
) ApplyWidgetStyle();
433 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
434 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
436 gtk_widget_show( menu_item
);
438 // return the index of the item in the control
442 wxSize
wxChoice::DoGetBestSize() const
444 wxSize
ret( wxControl::DoGetBestSize() );
446 // we know better our horizontal extent: it depends on the longest string
452 size_t count
= GetCount();
453 for ( size_t n
= 0; n
< count
; n
++ )
455 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
, &m_font
);
460 // add extra for the choice "=" button
462 // VZ: I don't know how to get the right value, it seems to be in
463 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
464 // to it - maybe we can use gtk_option_menu_size_request() for this
467 // This default value works only for the default GTK+ theme (i.e.
468 // no theme at all) (FIXME)
469 static const int widthChoiceIndicator
= 35;
470 ret
.x
+= widthChoiceIndicator
;
473 // but not less than the minimal width
477 ret
.y
= 16 + gdk_char_height(GET_STYLE_FONT( m_widget
->style
), 'H' );
482 #endif // wxUSE_CHOICE