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())
35 if (g_blockEventsOnDrag
)
38 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
39 event
.SetInt( choice
->GetSelection() );
40 event
.SetString( choice
->GetStringSelection() );
41 event
.SetEventObject(choice
);
42 choice
->GetEventHandler()->ProcessEvent(event
);
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
55 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
56 const wxPoint
&pos
, const wxSize
&size
,
57 int n
, const wxString choices
[],
58 long style
, const wxValidator
& validator
, const wxString
&name
)
62 PreCreation( parent
, id
, pos
, size
, style
, name
);
64 SetValidator( validator
);
66 m_widget
= gtk_option_menu_new();
73 SetSize( newSize
.x
, newSize
.y
);
75 GtkWidget
*menu
= gtk_menu_new();
77 for (int i
= 0; i
< n
; i
++)
79 m_clientDataList
.Append( (wxObject
*) NULL
);
80 m_clientObjectList
.Append( (wxObject
*) NULL
);
82 GtkWidget
*item
= gtk_menu_item_new_with_label( choices
[i
] );
83 gtk_menu_append( GTK_MENU(menu
), item
);
85 gtk_widget_realize( item
);
86 gtk_widget_realize( GTK_BIN(item
)->child
);
88 gtk_widget_show( item
);
90 gtk_signal_connect( GTK_OBJECT( item
), "activate",
91 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
93 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
95 m_parent
->AddChild( this );
97 (m_parent
->m_insertCallback
)( m_parent
, this );
101 SetBackgroundColour( parent
->GetBackgroundColour() );
102 SetForegroundColour( parent
->GetForegroundColour() );
103 SetFont( parent
->GetFont() );
110 wxChoice::~wxChoice()
115 void wxChoice::AppendCommon( const wxString
&item
)
117 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
119 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
120 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
);
122 gtk_menu_append( GTK_MENU(menu
), menu_item
);
124 gtk_widget_realize( menu_item
);
125 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
127 if (m_widgetStyle
) ApplyWidgetStyle();
129 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
130 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
132 gtk_widget_show( menu_item
);
135 void wxChoice::Append( const wxString
&item
)
137 m_clientDataList
.Append( (wxObject
*) NULL
);
138 m_clientObjectList
.Append( (wxObject
*) NULL
);
140 AppendCommon( item
);
143 void wxChoice::Append( const wxString
&item
, void *clientData
)
145 m_clientDataList
.Append( (wxObject
*) clientData
);
146 m_clientObjectList
.Append( (wxObject
*) NULL
);
148 AppendCommon( item
);
151 void wxChoice::Append( const wxString
&item
, wxClientData
*clientData
)
153 m_clientObjectList
.Append( (wxObject
*) clientData
);
154 m_clientDataList
.Append( (wxObject
*) NULL
);
156 AppendCommon( item
);
159 void wxChoice::SetClientData( int n
, void* clientData
)
161 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
163 wxNode
*node
= m_clientDataList
.Nth( n
);
166 node
->SetData( (wxObject
*) clientData
);
169 void* wxChoice::GetClientData( int n
)
171 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
173 wxNode
*node
= m_clientDataList
.Nth( n
);
174 if (!node
) return NULL
;
179 void wxChoice::SetClientObject( int n
, wxClientData
* clientData
)
181 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
183 wxNode
*node
= m_clientObjectList
.Nth( n
);
186 wxClientData
*cd
= (wxClientData
*) node
->Data();
189 node
->SetData( (wxObject
*) clientData
);
192 wxClientData
* wxChoice::GetClientObject( int n
)
194 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, "invalid combobox" );
196 wxNode
*node
= m_clientObjectList
.Nth( n
);
197 if (!node
) return (wxClientData
*) NULL
;
199 return (wxClientData
*) node
->Data();
202 void wxChoice::Clear()
204 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
206 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
207 GtkWidget
*menu
= gtk_menu_new();
208 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
210 wxNode
*node
= m_clientObjectList
.First();
213 wxClientData
*cd
= (wxClientData
*)node
->Data();
217 m_clientObjectList
.Clear();
219 m_clientDataList
.Clear();
222 void wxChoice::Delete( int WXUNUSED(n
) )
224 wxFAIL_MSG( "wxChoice:Delete not implemented" );
227 int wxChoice::FindString( const wxString
&string
) const
229 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
231 // If you read this code once and you think you understand
232 // it, then you are very wrong. Robert Roebling.
234 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
236 GList
*child
= menu_shell
->children
;
239 GtkBin
*bin
= GTK_BIN( child
->data
);
240 GtkLabel
*label
= (GtkLabel
*) NULL
;
241 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
242 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
244 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
246 if (string
== label
->label
)
256 int wxChoice::GetColumns() const
261 int wxChoice::GetSelection()
263 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
265 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
267 GList
*child
= menu_shell
->children
;
270 GtkBin
*bin
= GTK_BIN( child
->data
);
271 if (!bin
->child
) return count
;
276 wxFAIL_MSG( "wxChoice: no selection" );
281 wxString
wxChoice::GetString( int n
) const
283 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
285 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
287 GList
*child
= menu_shell
->children
;
290 GtkBin
*bin
= GTK_BIN( child
->data
);
293 GtkLabel
*label
= (GtkLabel
*) NULL
;
294 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
295 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
297 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
305 wxFAIL_MSG( "wxChoice: invalid index in GetString()" );
310 wxString
wxChoice::GetStringSelection() const
312 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
314 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
316 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
321 int wxChoice::Number() const
323 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid choice" );
325 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
327 GList
*child
= menu_shell
->children
;
336 void wxChoice::SetColumns( int WXUNUSED(n
) )
340 void wxChoice::SetSelection( int n
)
342 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
345 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
347 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
350 void wxChoice::SetStringSelection( const wxString
&string
)
352 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
354 int n
= FindString( string
);
355 if (n
!= -1) SetSelection( n
);
358 void wxChoice::ApplyWidgetStyle()
362 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
364 gtk_widget_set_style( m_widget
, m_widgetStyle
);
365 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
367 GList
*child
= menu_shell
->children
;
370 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
372 GtkBin
*bin
= GTK_BIN( child
->data
);
373 GtkWidget
*label
= (GtkWidget
*) NULL
;
374 if (bin
->child
) label
= bin
->child
;
375 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
377 gtk_widget_set_style( label
, m_widgetStyle
);