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
)
61 #if (GTK_MINOR_VERSION > 0)
62 m_acceptsFocus
= TRUE
;
65 PreCreation( parent
, id
, pos
, size
, style
, name
);
67 SetValidator( validator
);
69 m_widget
= gtk_option_menu_new();
76 SetSize( newSize
.x
, newSize
.y
);
78 GtkWidget
*menu
= gtk_menu_new();
80 for (int i
= 0; i
< n
; i
++)
82 m_clientDataList
.Append( (wxObject
*) NULL
);
83 m_clientObjectList
.Append( (wxObject
*) NULL
);
85 GtkWidget
*item
= gtk_menu_item_new_with_label( choices
[i
] );
86 gtk_menu_append( GTK_MENU(menu
), item
);
88 gtk_widget_realize( item
);
89 gtk_widget_realize( GTK_BIN(item
)->child
);
91 gtk_widget_show( item
);
93 gtk_signal_connect( GTK_OBJECT( item
), "activate",
94 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
96 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
98 m_parent
->AddChild( this );
100 (m_parent
->m_insertCallback
)( m_parent
, this );
104 SetBackgroundColour( parent
->GetBackgroundColour() );
105 SetForegroundColour( parent
->GetForegroundColour() );
106 SetFont( parent
->GetFont() );
113 wxChoice::~wxChoice()
118 void wxChoice::AppendCommon( const wxString
&item
)
120 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
122 GtkWidget
*menu
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) );
123 GtkWidget
*menu_item
= gtk_menu_item_new_with_label( item
);
125 gtk_menu_append( GTK_MENU(menu
), menu_item
);
127 gtk_widget_realize( menu_item
);
128 gtk_widget_realize( GTK_BIN(menu_item
)->child
);
130 if (m_widgetStyle
) ApplyWidgetStyle();
132 gtk_signal_connect( GTK_OBJECT( menu_item
), "activate",
133 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this );
135 gtk_widget_show( menu_item
);
138 void wxChoice::Append( const wxString
&item
)
140 m_clientDataList
.Append( (wxObject
*) NULL
);
141 m_clientObjectList
.Append( (wxObject
*) NULL
);
143 AppendCommon( item
);
146 void wxChoice::Append( const wxString
&item
, void *clientData
)
148 m_clientDataList
.Append( (wxObject
*) clientData
);
149 m_clientObjectList
.Append( (wxObject
*) NULL
);
151 AppendCommon( item
);
154 void wxChoice::Append( const wxString
&item
, wxClientData
*clientData
)
156 m_clientObjectList
.Append( (wxObject
*) clientData
);
157 m_clientDataList
.Append( (wxObject
*) NULL
);
159 AppendCommon( item
);
162 void wxChoice::SetClientData( int n
, void* clientData
)
164 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
166 wxNode
*node
= m_clientDataList
.Nth( n
);
169 node
->SetData( (wxObject
*) clientData
);
172 void* wxChoice::GetClientData( int n
)
174 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
176 wxNode
*node
= m_clientDataList
.Nth( n
);
177 if (!node
) return NULL
;
182 void wxChoice::SetClientObject( int n
, wxClientData
* clientData
)
184 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
186 wxNode
*node
= m_clientObjectList
.Nth( n
);
189 wxClientData
*cd
= (wxClientData
*) node
->Data();
192 node
->SetData( (wxObject
*) clientData
);
195 wxClientData
* wxChoice::GetClientObject( int n
)
197 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, "invalid combobox" );
199 wxNode
*node
= m_clientObjectList
.Nth( n
);
200 if (!node
) return (wxClientData
*) NULL
;
202 return (wxClientData
*) node
->Data();
205 void wxChoice::Clear()
207 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
209 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) );
210 GtkWidget
*menu
= gtk_menu_new();
211 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu
);
213 wxNode
*node
= m_clientObjectList
.First();
216 wxClientData
*cd
= (wxClientData
*)node
->Data();
220 m_clientObjectList
.Clear();
222 m_clientDataList
.Clear();
225 void wxChoice::Delete( int WXUNUSED(n
) )
227 wxFAIL_MSG( "wxChoice:Delete not implemented" );
230 int wxChoice::FindString( const wxString
&string
) const
232 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
234 // If you read this code once and you think you understand
235 // it, then you are very wrong. Robert Roebling.
237 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
239 GList
*child
= menu_shell
->children
;
242 GtkBin
*bin
= GTK_BIN( child
->data
);
243 GtkLabel
*label
= (GtkLabel
*) NULL
;
244 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
245 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
247 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
249 if (string
== label
->label
)
259 int wxChoice::GetColumns() const
264 int wxChoice::GetSelection()
266 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid choice" );
268 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
270 GList
*child
= menu_shell
->children
;
273 GtkBin
*bin
= GTK_BIN( child
->data
);
274 if (!bin
->child
) return count
;
279 wxFAIL_MSG( "wxChoice: no selection" );
284 wxString
wxChoice::GetString( int n
) const
286 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
288 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
290 GList
*child
= menu_shell
->children
;
293 GtkBin
*bin
= GTK_BIN( child
->data
);
296 GtkLabel
*label
= (GtkLabel
*) NULL
;
297 if (bin
->child
) label
= GTK_LABEL(bin
->child
);
298 if (!label
) label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
300 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
308 wxFAIL_MSG( "wxChoice: invalid index in GetString()" );
313 wxString
wxChoice::GetStringSelection() const
315 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid choice" );
317 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(m_widget
)->child
);
319 wxASSERT_MSG( label
!= NULL
, "wxChoice: invalid label" );
324 int wxChoice::Number() const
326 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid choice" );
328 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
330 GList
*child
= menu_shell
->children
;
339 void wxChoice::SetColumns( int WXUNUSED(n
) )
343 void wxChoice::SetSelection( int n
)
345 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
348 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp
);
350 gtk_choice_clicked_callback( (GtkWidget
*) NULL
, this );
353 void wxChoice::SetStringSelection( const wxString
&string
)
355 wxCHECK_RET( m_widget
!= NULL
, "invalid choice" );
357 int n
= FindString( string
);
358 if (n
!= -1) SetSelection( n
);
361 void wxChoice::ApplyWidgetStyle()
365 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) );
367 gtk_widget_set_style( m_widget
, m_widgetStyle
);
368 gtk_widget_set_style( GTK_WIDGET( menu_shell
), m_widgetStyle
);
370 GList
*child
= menu_shell
->children
;
373 gtk_widget_set_style( GTK_WIDGET( child
->data
), m_widgetStyle
);
375 GtkBin
*bin
= GTK_BIN( child
->data
);
376 GtkWidget
*label
= (GtkWidget
*) NULL
;
377 if (bin
->child
) label
= bin
->child
;
378 if (!label
) label
= GTK_BUTTON(m_widget
)->child
;
380 gtk_widget_set_style( label
, m_widgetStyle
);