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"
17 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
21 extern bool g_blockEventsOnDrag
;
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 static void gtk_choice_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
29 if (!choice
->HasVMT()) return;
30 if (g_blockEventsOnDrag
) return;
32 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() );
33 event
.SetInt( choice
->GetSelection() );
34 wxString
tmp( choice
->GetStringSelection() );
35 event
.SetString( WXSTRINGCAST(tmp
) );
36 event
.SetEventObject(choice
);
37 choice
->GetEventHandler()->ProcessEvent(event
);
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
)
50 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
51 const wxPoint
&pos
, const wxSize
&size
,
52 int n
, const wxString choices
[],
53 long style
, const wxValidator
& validator
, const wxString
&name
)
57 PreCreation( parent
, id
, pos
, size
, style
, name
);
59 SetValidator( validator
);
61 m_widget
= gtk_option_menu_new();
63 wxSize newSize
= size
;
64 if (newSize
.x
== -1) newSize
.x
= 80;
65 if (newSize
.y
== -1) newSize
.y
= 26;
66 SetSize( newSize
.x
, newSize
.y
);
68 GtkWidget
*menu
= gtk_menu_new();
70 for (int i
= 0; i
< n
; i
++)
72 m_clientDataList
.Append( (wxObject
*) NULL
);
74 GtkWidget
*item
= gtk_menu_item_new_with_label( choices
[i
] );
75 gtk_menu_append( GTK_MENU(menu
), item
);
77 gtk_widget_realize( item
);
78 gtk_widget_realize( GTK_BIN(item
)->child
);
80 gtk_widget_show( item
);
82 gtk_signal_connect( GTK_OBJECT( item
), "activate",
83 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
85 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
87 m_parent
->AddChild( this );
89 (m_parent
->m_insertCallback
)( m_parent
, this );
93 SetBackgroundColour( parent
->GetBackgroundColour() );
94 SetForegroundColour( parent
->GetForegroundColour() );
101 wxChoice::~wxChoice()
103 wxNode
*node
= m_clientDataList
.First();
106 wxClientData
*cd
= (wxClientData
*)node
->Data();
110 m_clientDataList
.Clear();
113 void wxChoice::AppendCommon( const wxString
&item
)
115 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
117 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
118 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
);
120 gtk_menu_append( GTK_MENU(menu
), menu_item
);
122 gtk_widget_realize( menu_item
);
123 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
125 if (m_widgetStyle
) ApplyWidgetStyle();
127 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
128 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
130 gtk_widget_show( menu_item
);
133 void wxChoice::Append( const wxString
&item
)
135 m_clientDataList
.Append( (wxObject
*)NULL
);
137 AppendCommon( item
);
140 void wxChoice::Append( const wxString
&item
, void *clientData
)
143 m_clientDataList
.Append( (wxObject
*) new wxVoidClientData( clientData
) );
145 m_clientDataList
.Append( (wxObject
*)NULL
);
147 AppendCommon( item
);
150 void wxChoice::Append( const wxString
&item
, wxClientData
*clientData
)
152 m_clientDataList
.Append( (wxObject
*) clientData
);
154 AppendCommon( item
);
157 void wxChoice::SetClientData( int n
, void* clientData
)
159 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
161 wxNode
*node
= m_clientDataList
.Nth( n
);
164 wxClientData
*cd
= (wxClientData
*) node
->Data();
168 node
->SetData( (wxObject
*) new wxVoidClientData(clientData
) );
170 node
->SetData( (wxObject
*) NULL
);
173 void* wxChoice::GetClientData( int n
)
175 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
177 wxNode
*node
= m_clientDataList
.Nth( n
);
178 if (!node
) return NULL
;
180 wxVoidClientData
*cd
= (wxVoidClientData
*) node
->Data();
182 return cd
->GetData();
187 void wxChoice::SetClientObject( int n
, wxClientData
* clientData
)
189 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
191 wxNode
*node
= m_clientDataList
.Nth( n
);
194 wxClientData
*cd
= (wxClientData
*) node
->Data();
197 node
->SetData( (wxObject
*) clientData
);
200 wxClientData
* wxChoice::GetClientObject( int n
)
202 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
204 wxNode
*node
= m_clientDataList
.Nth( n
);
205 if (!node
) return (wxClientData
*) NULL
;
207 return (wxClientData
*) node
->Data();
211 void wxChoice::Clear()
213 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
215 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
216 GtkWidget
*menu
= gtk_menu_new();
217 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
219 wxNode
*node
= m_clientDataList
.First();
222 wxClientData
*cd
= (wxClientData
*)node
->Data();
226 m_clientDataList
.Clear();
229 void wxChoice::Delete( int WXUNUSED(n
) )
231 wxFAIL_MSG( "wxChoice:Delete not implemented" );
234 int wxChoice::FindString( const wxString
&string
) const
236 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
238 // If you read this code once and you think you understand
239 // it, then you are very wrong. Robert Roebling.
241 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
243 GList
*child
= menu_shell
->children
;
246 GtkBin
*bin
= GTK_BIN( child
->data
);
247 GtkLabel
*label
= (GtkLabel
*) NULL
;
248 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
249 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
251 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
253 if (string
== label
->label
) return count
;
258 wxFAIL_MSG( "wxChoice: string not found" );
263 int wxChoice::GetColumns() const
268 int wxChoice::GetSelection()
270 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
272 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
274 GList
*child
= menu_shell
->children
;
277 GtkBin
*bin
= GTK_BIN( child
->data
);
278 if (!bin
->child
) return count
;
283 wxFAIL_MSG( "wxChoice: no selection" );
288 wxString
wxChoice::GetString( int n
) const
290 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
292 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
294 GList
*child
= menu_shell
->children
;
297 GtkBin
*bin
= GTK_BIN( child
->data
);
300 GtkLabel
*label
= (GtkLabel
*) NULL
;
301 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
302 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
304 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
312 wxFAIL_MSG( "wxChoice: string not found" );
317 wxString
wxChoice::GetStringSelection() const
319 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
321 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
323 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
328 int wxChoice::Number() const
330 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid choice" );
332 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
334 GList
*child
= menu_shell
->children
;
343 void wxChoice::SetColumns( int WXUNUSED(n
) )
347 void wxChoice::SetSelection( int n
)
349 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
352 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
354 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
357 void wxChoice::SetStringSelection( const wxString
&string
)
359 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
361 int n
= FindString( string
);
362 if (n
!= -1) SetSelection( n
);
365 void wxChoice::ApplyWidgetStyle()
369 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
371 gtk_widget_set_style( m_widget
, m_widgetStyle
);
372 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
374 GList
*child
= menu_shell
->children
;
377 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
379 GtkBin
*bin
= GTK_BIN( child
->data
);
380 GtkWidget
*label
= (GtkWidget
*) NULL
;
381 if (bin
->child
) label
= bin
->child
;
382 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
384 gtk_widget_set_style( label
, m_widgetStyle
);