]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
added helpext.cpp and regenerated the makefiles
[wxWidgets.git] / src / gtk / choice.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: choice.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
29006414 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "choice.h"
13#endif
14
15#include "wx/choice.h"
16
ce4169a4
RR
17#if wxUSE_CHOICE
18
83624f79
RR
19#include "gdk/gdk.h"
20#include "gtk/gtk.h"
21
acfd422a
RR
22//-----------------------------------------------------------------------------
23// idle system
24//-----------------------------------------------------------------------------
25
26extern void wxapp_install_idle_handler();
27extern bool g_isIdle;
28
66bd6b93
RR
29//-----------------------------------------------------------------------------
30// data
31//-----------------------------------------------------------------------------
32
33extern bool g_blockEventsOnDrag;
34
c801d85f 35//-----------------------------------------------------------------------------
e1e955e1 36// "activate"
c801d85f
KB
37//-----------------------------------------------------------------------------
38
66bd6b93 39static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 40{
4dcaf11a
RR
41 if (g_isIdle)
42 wxapp_install_idle_handler();
acfd422a 43
a2053b27 44 if (!choice->m_hasVMT) return;
29006414 45
acfd422a 46 if (g_blockEventsOnDrag) return;
29006414 47
acfd422a
RR
48 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
49 event.SetInt( choice->GetSelection() );
50 event.SetString( choice->GetStringSelection() );
51 event.SetEventObject(choice);
52 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 53}
c801d85f 54
e1e955e1
RR
55//-----------------------------------------------------------------------------
56// wxChoice
c801d85f
KB
57//-----------------------------------------------------------------------------
58
7f4dc78d 59IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f 60
fd0eed64 61wxChoice::wxChoice()
c801d85f 62{
6de97a3b 63}
c801d85f 64
debe6624 65bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
66 const wxPoint &pos, const wxSize &size,
67 int n, const wxString choices[],
68 long style, const wxValidator& validator, const wxString &name )
c801d85f 69{
fd0eed64 70 m_needParent = TRUE;
034be888
RR
71#if (GTK_MINOR_VERSION > 0)
72 m_acceptsFocus = TRUE;
73#endif
29006414 74
4dcaf11a
RR
75 if (!PreCreation( parent, pos, size ) ||
76 !CreateBase( parent, id, pos, size, style, validator, name ))
77 {
223d09f6 78 wxFAIL_MSG( wxT("wxChoice creation failed") );
4dcaf11a
RR
79 return FALSE;
80 }
6de97a3b 81
fd0eed64 82 m_widget = gtk_option_menu_new();
29006414
VZ
83
84 wxSize newSize(size);
85 if (newSize.x == -1)
86 newSize.x = 80;
87 if (newSize.y == -1)
88 newSize.y = 26;
fd0eed64 89 SetSize( newSize.x, newSize.y );
29006414 90
fd0eed64 91 GtkWidget *menu = gtk_menu_new();
29006414 92
fd0eed64
RR
93 for (int i = 0; i < n; i++)
94 {
d6538e2c 95 m_clientList.Append( (wxObject*) NULL );
29006414 96
93c5dd39 97 GtkWidget *item = gtk_menu_item_new_with_label( choices[i].mbc_str() );
fd0eed64 98 gtk_menu_append( GTK_MENU(menu), item );
29006414 99
fd0eed64 100 gtk_widget_show( item );
29006414
VZ
101
102 gtk_signal_connect( GTK_OBJECT( item ), "activate",
fd0eed64
RR
103 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
104 }
105 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 106
f03fc89f 107 m_parent->DoAddChild( this );
29006414 108
fd0eed64 109 PostCreation();
29006414 110
fd0eed64
RR
111 SetBackgroundColour( parent->GetBackgroundColour() );
112 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 113 SetFont( parent->GetFont() );
f96aa4d9 114
fd0eed64 115 Show( TRUE );
29006414 116
fd0eed64 117 return TRUE;
6de97a3b 118}
29006414 119
fd0eed64
RR
120wxChoice::~wxChoice()
121{
f5e27805 122 Clear();
fd0eed64
RR
123}
124
9abe166a 125int wxChoice::DoAppend( const wxString &item )
fd0eed64 126{
9abe166a 127 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
29006414 128
fd0eed64 129 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
93c5dd39 130 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
29006414 131
fd0eed64 132 gtk_menu_append( GTK_MENU(menu), menu_item );
29006414 133
2b07d713
RR
134 if (GTK_WIDGET_REALIZED(m_widget))
135 {
136 gtk_widget_realize( menu_item );
137 gtk_widget_realize( GTK_BIN(menu_item)->child );
29006414 138
2b07d713
RR
139 if (m_widgetStyle) ApplyWidgetStyle();
140 }
29006414
VZ
141
142 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
fd0eed64 143 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
29006414 144
fd0eed64 145 gtk_widget_show( menu_item );
fd0eed64 146
d6538e2c 147 m_clientList.Append( (wxObject*) NULL );
29006414 148
9abe166a 149 // return the index of the item in the control
d6538e2c 150 return GetCount() - 1;
fd0eed64 151}
f96aa4d9 152
9abe166a 153void wxChoice::DoSetClientData( int n, void* clientData )
fd0eed64 154{
223d09f6 155 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
29006414 156
d6538e2c 157 wxNode *node = m_clientList.Nth( n );
9abe166a 158 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetClientData") );
29006414 159
f5e27805 160 node->SetData( (wxObject*) clientData );
fd0eed64
RR
161}
162
9abe166a 163void* wxChoice::DoGetClientData( int n ) const
fd0eed64 164{
223d09f6 165 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
29006414 166
d6538e2c 167 wxNode *node = m_clientList.Nth( n );
9abe166a 168 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetClientData") );
29006414 169
f5e27805 170 return node->Data();
6de97a3b 171}
fd0eed64 172
9abe166a 173void wxChoice::DoSetClientObject( int n, wxClientData* clientData )
fd0eed64 174{
223d09f6 175 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
29006414 176
d6538e2c 177 wxNode *node = m_clientList.Nth( n );
9abe166a 178 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetClientObject") );
29006414 179
fd0eed64 180 wxClientData *cd = (wxClientData*) node->Data();
9abe166a 181 delete cd;
29006414 182
fd0eed64
RR
183 node->SetData( (wxObject*) clientData );
184}
185
9abe166a 186wxClientData* wxChoice::DoGetClientObject( int n ) const
fd0eed64 187{
223d09f6 188 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid combobox") );
29006414 189
d6538e2c 190 wxNode *node = m_clientList.Nth( n );
9abe166a
VZ
191 wxCHECK_MSG( node, (wxClientData *)NULL,
192 wxT("invalid index in wxChoice::DoGetClientObject") );
29006414 193
fd0eed64
RR
194 return (wxClientData*) node->Data();
195}
29006414 196
fd0eed64 197void wxChoice::Clear()
c801d85f 198{
223d09f6 199 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 200
fd0eed64
RR
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 );
29006414 204
d6538e2c 205 if (m_clientDataItemsType == ClientData_Object)
fd0eed64 206 {
d6538e2c
RR
207 wxNode *node = m_clientList.First();
208 while (node)
209 {
210 wxClientData *cd = (wxClientData*)node->Data();
211 if (cd) delete cd;
212 node = node->Next();
213 }
fd0eed64 214 }
d6538e2c 215 m_clientList.Clear();
6de97a3b 216}
c801d85f 217
2f6407b9
RR
218void wxChoice::Delete( int WXUNUSED(n) )
219{
223d09f6 220 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
2f6407b9
RR
221}
222
c801d85f
KB
223int wxChoice::FindString( const wxString &string ) const
224{
223d09f6 225 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
226
227 // If you read this code once and you think you understand
228 // it, then you are very wrong. Robert Roebling.
29006414 229
fd0eed64
RR
230 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
231 int count = 0;
232 GList *child = menu_shell->children;
233 while (child)
234 {
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 );
29006414 239
223d09f6 240 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 241
dcf924a3 242 if (string == wxString(label->label,*wxConvCurrent))
29006414
VZ
243 return count;
244
fd0eed64
RR
245 child = child->next;
246 count++;
247 }
29006414 248
fd0eed64 249 return -1;
6de97a3b 250}
c801d85f 251
9abe166a 252int wxChoice::GetSelection() const
c801d85f 253{
223d09f6 254 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
255
256 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
257 int count = 0;
258 GList *child = menu_shell->children;
259 while (child)
260 {
261 GtkBin *bin = GTK_BIN( child->data );
262 if (!bin->child) return count;
263 child = child->next;
264 count++;
265 }
29006414 266
fd0eed64 267 return -1;
6de97a3b 268}
c801d85f 269
debe6624 270wxString wxChoice::GetString( int n ) const
c801d85f 271{
223d09f6 272 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
273
274 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
275 int count = 0;
276 GList *child = menu_shell->children;
277 while (child)
c801d85f 278 {
fd0eed64
RR
279 GtkBin *bin = GTK_BIN( child->data );
280 if (count == n)
281 {
282 GtkLabel *label = (GtkLabel *) NULL;
283 if (bin->child) label = GTK_LABEL(bin->child);
284 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 285
223d09f6 286 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 287
dcf924a3 288 return wxString(label->label,*wxConvCurrent);
fd0eed64
RR
289 }
290 child = child->next;
291 count++;
6de97a3b 292 }
29006414 293
223d09f6 294 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 295
223d09f6 296 return wxT("");
6de97a3b 297}
c801d85f 298
9abe166a 299int wxChoice::GetCount() const
c801d85f 300{
223d09f6 301 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
302
303 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
304 int count = 0;
305 GList *child = menu_shell->children;
306 while (child)
307 {
308 count++;
309 child = child->next;
310 }
311 return count;
6de97a3b 312}
c801d85f 313
debe6624 314void wxChoice::SetSelection( int n )
c801d85f 315{
223d09f6 316 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 317
fd0eed64
RR
318 int tmp = n;
319 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 320}
c801d85f 321
953704c1
RR
322void wxChoice::DisableEvents()
323{
324/*
325 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
326 GList *child = menu_shell->children;
327 while (child)
328 {
329 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
330 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
331
332 child = child->next;
333 }
334*/
335}
336
337void wxChoice::EnableEvents()
338{
339/*
340 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
341 GList *child = menu_shell->children;
342 while (child)
343 {
344 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
345 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
346
347 child = child->next;
348 }
349*/
350}
351
58614078 352void wxChoice::ApplyWidgetStyle()
868a2826 353{
fd0eed64 354 SetWidgetStyle();
29006414 355
fd0eed64 356 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 357
fd0eed64
RR
358 gtk_widget_set_style( m_widget, m_widgetStyle );
359 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 360
fd0eed64
RR
361 GList *child = menu_shell->children;
362 while (child)
363 {
364 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 365
fd0eed64
RR
366 GtkBin *bin = GTK_BIN( child->data );
367 GtkWidget *label = (GtkWidget *) NULL;
368 if (bin->child) label = bin->child;
369 if (!label) label = GTK_BUTTON(m_widget)->child;
29006414 370
fd0eed64 371 gtk_widget_set_style( label, m_widgetStyle );
29006414 372
fd0eed64
RR
373 child = child->next;
374 }
f96aa4d9
RR
375}
376
ce4169a4 377#endif