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() );
105 wxChoice::~wxChoice()
110 void wxChoice::AppendCommon( const wxString
&item
)
112 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
114 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
115 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
);
117 gtk_menu_append( GTK_MENU(menu
), menu_item
);
119 gtk_widget_realize( menu_item
);
120 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
122 if (m_widgetStyle
) ApplyWidgetStyle();
124 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
125 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
127 gtk_widget_show( menu_item
);
130 void wxChoice::Append( const wxString
&item
)
132 m_clientDataList
.Append( (wxObject
*) NULL
);
133 m_clientObjectList
.Append( (wxObject
*) NULL
);
135 AppendCommon( item
);
138 void wxChoice::Append( const wxString
&item
, void *clientData
)
140 m_clientDataList
.Append( (wxObject
*) clientData
);
141 m_clientObjectList
.Append( (wxObject
*) NULL
);
143 AppendCommon( item
);
146 void wxChoice::Append( const wxString
&item
, wxClientData
*clientData
)
148 m_clientObjectList
.Append( (wxObject
*) clientData
);
149 m_clientDataList
.Append( (wxObject
*) NULL
);
151 AppendCommon( item
);
154 void wxChoice::SetClientData( int n
, void* clientData
)
156 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
158 wxNode
*node
= m_clientDataList
.Nth( n
);
161 node
->SetData( (wxObject
*) clientData
);
164 void* wxChoice::GetClientData( int n
)
166 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
168 wxNode
*node
= m_clientDataList
.Nth( n
);
169 if (!node
) return NULL
;
174 void wxChoice::SetClientObject( int n
, wxClientData
* clientData
)
176 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
178 wxNode
*node
= m_clientObjectList
.Nth( n
);
181 wxClientData
*cd
= (wxClientData
*) node
->Data();
184 node
->SetData( (wxObject
*) clientData
);
187 wxClientData
* wxChoice::GetClientObject( int n
)
189 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, "invalid combobox" );
191 wxNode
*node
= m_clientObjectList
.Nth( n
);
192 if (!node
) return (wxClientData
*) NULL
;
194 return (wxClientData
*) node
->Data();
197 void wxChoice::Clear()
199 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
201 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
202 GtkWidget
*menu
= gtk_menu_new();
203 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
205 wxNode
*node
= m_clientObjectList
.First();
208 wxClientData
*cd
= (wxClientData
*)node
->Data();
212 m_clientObjectList
.Clear();
214 m_clientDataList
.Clear();
217 void wxChoice::Delete( int WXUNUSED(n
) )
219 wxFAIL_MSG( "wxChoice:Delete not implemented" );
222 int wxChoice::FindString( const wxString
&string
) const
224 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
226 // If you read this code once and you think you understand
227 // it, then you are very wrong. Robert Roebling.
229 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
231 GList
*child
= menu_shell
->children
;
234 GtkBin
*bin
= GTK_BIN( child
->data
);
235 GtkLabel
*label
= (GtkLabel
*) NULL
;
236 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
237 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
239 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
241 if (string
== label
->label
) return count
;
246 wxFAIL_MSG( "wxChoice: string not found" );
251 int wxChoice::GetColumns() const
256 int wxChoice::GetSelection()
258 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
260 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
262 GList
*child
= menu_shell
->children
;
265 GtkBin
*bin
= GTK_BIN( child
->data
);
266 if (!bin
->child
) return count
;
271 wxFAIL_MSG( "wxChoice: no selection" );
276 wxString
wxChoice::GetString( int n
) const
278 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
280 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
282 GList
*child
= menu_shell
->children
;
285 GtkBin
*bin
= GTK_BIN( child
->data
);
288 GtkLabel
*label
= (GtkLabel
*) NULL
;
289 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
290 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
292 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
300 wxFAIL_MSG( "wxChoice: string not found" );
305 wxString
wxChoice::GetStringSelection() const
307 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
309 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
311 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
316 int wxChoice::Number() const
318 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid choice" );
320 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
322 GList
*child
= menu_shell
->children
;
331 void wxChoice::SetColumns( int WXUNUSED(n
) )
335 void wxChoice::SetSelection( int n
)
337 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
340 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
342 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
345 void wxChoice::SetStringSelection( const wxString
&string
)
347 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
349 int n
= FindString( string
);
350 if (n
!= -1) SetSelection( n
);
353 void wxChoice::ApplyWidgetStyle()
357 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
359 gtk_widget_set_style( m_widget
, m_widgetStyle
);
360 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
362 GList
*child
= menu_shell
->children
;
365 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
367 GtkBin
*bin
= GTK_BIN( child
->data
);
368 GtkWidget
*label
= (GtkWidget
*) NULL
;
369 if (bin
->child
) label
= bin
->child
;
370 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
372 gtk_widget_set_style( label
, m_widgetStyle
);