1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "choice.h"
15 #include "wx/choice.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern bool g_blockEventsOnDrag
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
32 if (!choice
->HasVMT()) return;
33 if (g_blockEventsOnDrag
) return;
35 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
36 event
.SetInt( choice
->GetSelection() );
37 wxString
tmp( choice
->GetStringSelection() );
38 event
.SetString( WXSTRINGCAST(tmp
) );
39 event
.SetEventObject(choice
);
40 choice
->GetEventHandler()->ProcessEvent(event
);
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
53 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
54 const wxPoint
&pos
, const wxSize
&size
,
55 int n
, const wxString choices
[],
56 long style
, const wxValidator
& validator
, const wxString
&name
)
60 PreCreation( parent
, id
, pos
, size
, style
, name
);
62 SetValidator( validator
);
64 m_widget
= gtk_option_menu_new();
66 wxSize newSize
= size
;
67 if (newSize
.x
== -1) newSize
.x
= 80;
68 if (newSize
.y
== -1) newSize
.y
= 26;
69 SetSize( newSize
.x
, newSize
.y
);
71 GtkWidget
*menu
= gtk_menu_new();
73 for (int i
= 0; i
< n
; i
++)
75 m_clientDataList
.Append( (wxObject
*) NULL
);
76 m_clientObjectList
.Append( (wxObject
*) NULL
);
78 GtkWidget
*item
= gtk_menu_item_new_with_label( choices
[i
] );
79 gtk_menu_append( GTK_MENU(menu
), item
);
81 gtk_widget_realize( item
);
82 gtk_widget_realize( GTK_BIN(item
)->child
);
84 gtk_widget_show( item
);
86 gtk_signal_connect( GTK_OBJECT( item
), "activate",
87 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
89 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
91 m_parent
->AddChild( this );
93 (m_parent
->m_insertCallback
)( m_parent
, this );
97 SetBackgroundColour( parent
->GetBackgroundColour() );
98 SetForegroundColour( parent
->GetForegroundColour() );
99 SetFont( parent
->GetFont() );
106 wxChoice::~wxChoice()
111 void wxChoice::AppendCommon( const wxString
&item
)
113 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
115 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
116 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
);
118 gtk_menu_append( GTK_MENU(menu
), menu_item
);
120 gtk_widget_realize( menu_item
);
121 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
123 if (m_widgetStyle
) ApplyWidgetStyle();
125 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
126 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
128 gtk_widget_show( menu_item
);
131 void wxChoice::Append( const wxString
&item
)
133 m_clientDataList
.Append( (wxObject
*) NULL
);
134 m_clientObjectList
.Append( (wxObject
*) NULL
);
136 AppendCommon( item
);
139 void wxChoice::Append( const wxString
&item
, void *clientData
)
141 m_clientDataList
.Append( (wxObject
*) clientData
);
142 m_clientObjectList
.Append( (wxObject
*) NULL
);
144 AppendCommon( item
);
147 void wxChoice::Append( const wxString
&item
, wxClientData
*clientData
)
149 m_clientObjectList
.Append( (wxObject
*) clientData
);
150 m_clientDataList
.Append( (wxObject
*) NULL
);
152 AppendCommon( item
);
155 void wxChoice::SetClientData( int n
, void* clientData
)
157 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
159 wxNode
*node
= m_clientDataList
.Nth( n
);
162 node
->SetData( (wxObject
*) clientData
);
165 void* wxChoice::GetClientData( int n
)
167 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
169 wxNode
*node
= m_clientDataList
.Nth( n
);
170 if (!node
) return NULL
;
175 void wxChoice::SetClientObject( int n
, wxClientData
* clientData
)
177 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
179 wxNode
*node
= m_clientObjectList
.Nth( n
);
182 wxClientData
*cd
= (wxClientData
*) node
->Data();
185 node
->SetData( (wxObject
*) clientData
);
188 wxClientData
* wxChoice::GetClientObject( int n
)
190 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, "invalid combobox" );
192 wxNode
*node
= m_clientObjectList
.Nth( n
);
193 if (!node
) return (wxClientData
*) NULL
;
195 return (wxClientData
*) node
->Data();
198 void wxChoice::Clear()
200 wxCHECK_RET( m_widget
!= NULL
, "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 wxNode
*node
= m_clientObjectList
.First();
209 wxClientData
*cd
= (wxClientData
*)node
->Data();
213 m_clientObjectList
.Clear();
215 m_clientDataList
.Clear();
218 void wxChoice::Delete( int WXUNUSED(n
) )
220 wxFAIL_MSG( "wxChoice:Delete not implemented" );
223 int wxChoice::FindString( const wxString
&string
) const
225 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
227 // If you read this code once and you think you understand
228 // it, then you are very wrong. Robert Roebling.
230 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
232 GList
*child
= menu_shell
->children
;
235 GtkBin
*bin
= GTK_BIN( child
->data
);
236 GtkLabel
*label
= (GtkLabel
*) NULL
;
237 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
238 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
240 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
242 if (string
== label
->label
) return count
;
247 wxFAIL_MSG( "wxChoice: string not found" );
252 int wxChoice::GetColumns() const
257 int wxChoice::GetSelection()
259 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
261 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
263 GList
*child
= menu_shell
->children
;
266 GtkBin
*bin
= GTK_BIN( child
->data
);
267 if (!bin
->child
) return count
;
272 wxFAIL_MSG( "wxChoice: no selection" );
277 wxString
wxChoice::GetString( int n
) const
279 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
281 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
283 GList
*child
= menu_shell
->children
;
286 GtkBin
*bin
= GTK_BIN( child
->data
);
289 GtkLabel
*label
= (GtkLabel
*) NULL
;
290 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
291 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
293 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
301 wxFAIL_MSG( "wxChoice: string not found" );
306 wxString
wxChoice::GetStringSelection() const
308 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
310 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
312 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
317 int wxChoice::Number() const
319 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid choice" );
321 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
323 GList
*child
= menu_shell
->children
;
332 void wxChoice::SetColumns( int WXUNUSED(n
) )
336 void wxChoice::SetSelection( int n
)
338 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
341 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
343 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
346 void wxChoice::SetStringSelection( const wxString
&string
)
348 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
350 int n
= FindString( string
);
351 if (n
!= -1) SetSelection( n
);
354 void wxChoice::ApplyWidgetStyle()
358 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
360 gtk_widget_set_style( m_widget
, m_widgetStyle
);
361 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
363 GList
*child
= menu_shell
->children
;
366 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
368 GtkBin
*bin
= GTK_BIN( child
->data
);
369 GtkWidget
*label
= (GtkWidget
*) NULL
;
370 if (bin
->child
) label
= bin
->child
;
371 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
373 gtk_widget_set_style( label
, m_widgetStyle
);