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"
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 GtkAppendHelper(menu
, choices
[i
]);
109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
111 m_parent
->DoAddChild( this );
115 SetFont( parent
->GetFont() );
117 wxSize
size_best( DoGetBestSize() );
118 wxSize
new_size( size
);
119 if (new_size
.x
== -1)
120 new_size
.x
= size_best
.x
;
121 if (new_size
.y
== -1)
122 new_size
.y
= size_best
.y
;
123 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
124 SetSize( new_size
.x
, new_size
.y
);
126 SetBackgroundColour( parent
->GetBackgroundColour() );
127 SetForegroundColour( parent
->GetForegroundColour() );
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 GtkAppendHelper(menu
, item
);
150 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
152 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
154 wxNode
*node
= m_clientList
.Nth( n
);
155 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
157 node
->SetData( (wxObject
*) clientData
);
160 void* wxChoice::DoGetItemClientData( int n
) const
162 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid choice control") );
164 wxNode
*node
= m_clientList
.Nth( n
);
165 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
170 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
172 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice control") );
174 wxNode
*node
= m_clientList
.Nth( n
);
175 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
177 wxClientData
*cd
= (wxClientData
*) node
->Data();
180 node
->SetData( (wxObject
*) clientData
);
183 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
185 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") );
187 wxNode
*node
= m_clientList
.Nth( n
);
188 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
189 wxT("invalid index in wxChoice::DoGetItemClientObject") );
191 return (wxClientData
*) node
->Data();
194 void wxChoice::Clear()
196 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
198 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
199 GtkWidget
*menu
= gtk_menu_new();
200 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
202 if ( HasClientObjectData() )
204 // destroy the data (due to Robert's idea of using wxList<wxObject>
205 // and not wxList<wxClientData> we can't just say
206 // m_clientList.DeleteContents(TRUE) - this would crash!
207 wxNode
*node
= m_clientList
.First();
210 delete (wxClientData
*)node
->Data();
214 m_clientList
.Clear();
220 void wxChoice::Delete( int WXUNUSED(n
) )
222 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
225 int wxChoice::FindString( const wxString
&string
) const
227 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
229 // If you read this code once and you think you understand
230 // it, then you are very wrong. Robert Roebling.
232 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
234 GList
*child
= menu_shell
->children
;
237 GtkBin
*bin
= GTK_BIN( child
->data
);
238 GtkLabel
*label
= (GtkLabel
*) NULL
;
239 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
240 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
242 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
244 if (string
== wxString(label
->label
,*wxConvCurrent
))
254 int wxChoice::GetSelection() const
256 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid choice") );
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 if (!bin
->child
) return count
;
272 void wxChoice::SetString( int WXUNUSED(n
), const wxString
& WXUNUSED(string
) )
274 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
276 wxFAIL_MSG(wxT("not implemented"));
279 wxString
wxChoice::GetString( int n
) const
281 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid choice") );
283 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
285 GList
*child
= menu_shell
->children
;
288 GtkBin
*bin
= GTK_BIN( child
->data
);
291 GtkLabel
*label
= (GtkLabel
*) NULL
;
292 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
293 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
295 wxASSERT_MSG( label
!= NULL
, wxT("wxChoice: invalid label") );
297 return wxString(label
->label
,*wxConvCurrent
);
303 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
308 int wxChoice::GetCount() const
310 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid choice") );
312 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
314 GList
*child
= menu_shell
->children
;
323 void wxChoice::SetSelection( int n
)
325 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid choice") );
328 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
331 void wxChoice::ApplyWidgetStyle()
335 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
337 gtk_widget_set_style( m_widget
, m_widgetStyle
);
338 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
340 GList
*child
= menu_shell
->children
;
343 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
345 GtkBin
*bin
= GTK_BIN( child
->data
);
346 GtkWidget
*label
= (GtkWidget
*) NULL
;
347 if (bin
->child
) label
= bin
->child
;
348 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
350 gtk_widget_set_style( label
, m_widgetStyle
);
356 size_t wxChoice::GtkAppendHelper(GtkWidget
*menu
, const wxString
& item
)
358 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
.mbc_str() );
363 // sorted control, need to insert at the correct index
364 index
= m_strings
->Add(item
);
366 gtk_menu_insert( GTK_MENU(menu
), menu_item
, index
);
370 m_clientList
.Insert( m_clientList
.Item(index
- 1),
375 m_clientList
.Insert( (wxObject
*) NULL
);
380 // normal control, just append
381 gtk_menu_append( GTK_MENU(menu
), menu_item
);
383 m_clientList
.Append( (wxObject
*) NULL
);
385 // don't call wxChoice::GetCount() from here because it doesn't work
386 // if we're called from ctor (and GtkMenuShell is still NULL)
387 index
= m_clientList
.GetCount() - 1;
390 if (GTK_WIDGET_REALIZED(m_widget
))
392 gtk_widget_realize( menu_item
);
393 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
395 if (m_widgetStyle
) ApplyWidgetStyle();
398 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
399 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
401 gtk_widget_show( menu_item
);
403 // return the index of the item in the control
407 wxSize
wxChoice::DoGetBestSize() const
409 wxSize
ret( wxControl::DoGetBestSize() );
410 if (ret
.x
< 80) ret
.x
= 80;
411 ret
.y
= 16 + gdk_char_height( m_widget
->style
->font
, 'H' );