]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
I had to remove (Robin's) makefile creation clause
[wxWidgets.git] / src / gtk1 / 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 {
78 wxFAIL_MSG( _T("wxChoice creation failed") );
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 {
95 m_clientDataList.Append( (wxObject*) NULL );
f5e27805 96 m_clientObjectList.Append( (wxObject*) NULL );
29006414 97
93c5dd39 98 GtkWidget *item = gtk_menu_item_new_with_label( choices[i].mbc_str() );
fd0eed64 99 gtk_menu_append( GTK_MENU(menu), item );
29006414 100
fd0eed64 101 gtk_widget_show( item );
29006414
VZ
102
103 gtk_signal_connect( GTK_OBJECT( item ), "activate",
fd0eed64
RR
104 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
105 }
106 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 107
f03fc89f 108 m_parent->DoAddChild( this );
29006414 109
fd0eed64 110 PostCreation();
29006414 111
fd0eed64
RR
112 SetBackgroundColour( parent->GetBackgroundColour() );
113 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 114 SetFont( parent->GetFont() );
f96aa4d9 115
fd0eed64 116 Show( TRUE );
29006414 117
fd0eed64 118 return TRUE;
6de97a3b 119}
29006414 120
fd0eed64
RR
121wxChoice::~wxChoice()
122{
f5e27805 123 Clear();
fd0eed64
RR
124}
125
126void wxChoice::AppendCommon( const wxString &item )
127{
93c5dd39 128 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
29006414 129
fd0eed64 130 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
93c5dd39 131 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
29006414 132
fd0eed64 133 gtk_menu_append( GTK_MENU(menu), menu_item );
29006414 134
2b07d713
RR
135 if (GTK_WIDGET_REALIZED(m_widget))
136 {
137 gtk_widget_realize( menu_item );
138 gtk_widget_realize( GTK_BIN(menu_item)->child );
29006414 139
2b07d713
RR
140 if (m_widgetStyle) ApplyWidgetStyle();
141 }
29006414
VZ
142
143 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
fd0eed64 144 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
29006414 145
fd0eed64
RR
146 gtk_widget_show( menu_item );
147}
148
c801d85f
KB
149void wxChoice::Append( const wxString &item )
150{
f5e27805
RR
151 m_clientDataList.Append( (wxObject*) NULL );
152 m_clientObjectList.Append( (wxObject*) NULL );
29006414 153
fd0eed64
RR
154 AppendCommon( item );
155}
156
157void wxChoice::Append( const wxString &item, void *clientData )
158{
f5e27805
RR
159 m_clientDataList.Append( (wxObject*) clientData );
160 m_clientObjectList.Append( (wxObject*) NULL );
29006414 161
fd0eed64
RR
162 AppendCommon( item );
163}
164
165void wxChoice::Append( const wxString &item, wxClientData *clientData )
166{
f5e27805
RR
167 m_clientObjectList.Append( (wxObject*) clientData );
168 m_clientDataList.Append( (wxObject*) NULL );
29006414 169
fd0eed64
RR
170 AppendCommon( item );
171}
f96aa4d9 172
fd0eed64
RR
173void wxChoice::SetClientData( int n, void* clientData )
174{
93c5dd39 175 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
29006414 176
fd0eed64
RR
177 wxNode *node = m_clientDataList.Nth( n );
178 if (!node) return;
29006414 179
f5e27805 180 node->SetData( (wxObject*) clientData );
fd0eed64
RR
181}
182
183void* wxChoice::GetClientData( int n )
184{
93c5dd39 185 wxCHECK_MSG( m_widget != NULL, NULL, _T("invalid combobox") );
29006414 186
fd0eed64
RR
187 wxNode *node = m_clientDataList.Nth( n );
188 if (!node) return NULL;
29006414 189
f5e27805 190 return node->Data();
6de97a3b 191}
fd0eed64
RR
192
193void wxChoice::SetClientObject( int n, wxClientData* clientData )
194{
93c5dd39 195 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
29006414 196
f5e27805 197 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64 198 if (!node) return;
29006414 199
fd0eed64
RR
200 wxClientData *cd = (wxClientData*) node->Data();
201 if (cd) delete cd;
29006414 202
fd0eed64
RR
203 node->SetData( (wxObject*) clientData );
204}
205
206wxClientData* wxChoice::GetClientObject( int n )
207{
93c5dd39 208 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, _T("invalid combobox") );
29006414 209
f5e27805 210 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64 211 if (!node) return (wxClientData*) NULL;
29006414 212
fd0eed64
RR
213 return (wxClientData*) node->Data();
214}
29006414 215
fd0eed64 216void wxChoice::Clear()
c801d85f 217{
93c5dd39 218 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 219
fd0eed64
RR
220 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
221 GtkWidget *menu = gtk_menu_new();
222 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 223
f5e27805 224 wxNode *node = m_clientObjectList.First();
fd0eed64
RR
225 while (node)
226 {
227 wxClientData *cd = (wxClientData*)node->Data();
228 if (cd) delete cd;
229 node = node->Next();
230 }
f5e27805 231 m_clientObjectList.Clear();
29006414 232
fd0eed64 233 m_clientDataList.Clear();
6de97a3b 234}
c801d85f 235
2f6407b9
RR
236void wxChoice::Delete( int WXUNUSED(n) )
237{
93c5dd39 238 wxFAIL_MSG( _T("wxChoice:Delete not implemented") );
2f6407b9
RR
239}
240
c801d85f
KB
241int wxChoice::FindString( const wxString &string ) const
242{
93c5dd39 243 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid choice") );
fd0eed64
RR
244
245 // If you read this code once and you think you understand
246 // it, then you are very wrong. Robert Roebling.
29006414 247
fd0eed64
RR
248 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
249 int count = 0;
250 GList *child = menu_shell->children;
251 while (child)
252 {
253 GtkBin *bin = GTK_BIN( child->data );
254 GtkLabel *label = (GtkLabel *) NULL;
255 if (bin->child) label = GTK_LABEL(bin->child);
256 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 257
93c5dd39 258 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414 259
dcf924a3 260 if (string == wxString(label->label,*wxConvCurrent))
29006414
VZ
261 return count;
262
fd0eed64
RR
263 child = child->next;
264 count++;
265 }
29006414 266
fd0eed64 267 return -1;
6de97a3b 268}
c801d85f 269
fd0eed64 270int wxChoice::GetColumns() const
c801d85f 271{
fd0eed64 272 return 1;
6de97a3b 273}
c801d85f 274
fd0eed64 275int wxChoice::GetSelection()
c801d85f 276{
93c5dd39 277 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid choice") );
fd0eed64
RR
278
279 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
280 int count = 0;
281 GList *child = menu_shell->children;
282 while (child)
283 {
284 GtkBin *bin = GTK_BIN( child->data );
285 if (!bin->child) return count;
286 child = child->next;
287 count++;
288 }
29006414 289
93c5dd39 290 wxFAIL_MSG( _T("wxChoice: no selection") );
29006414 291
fd0eed64 292 return -1;
6de97a3b 293}
c801d85f 294
debe6624 295wxString wxChoice::GetString( int n ) const
c801d85f 296{
93c5dd39 297 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
fd0eed64
RR
298
299 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
300 int count = 0;
301 GList *child = menu_shell->children;
302 while (child)
c801d85f 303 {
fd0eed64
RR
304 GtkBin *bin = GTK_BIN( child->data );
305 if (count == n)
306 {
307 GtkLabel *label = (GtkLabel *) NULL;
308 if (bin->child) label = GTK_LABEL(bin->child);
309 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 310
93c5dd39 311 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414 312
dcf924a3 313 return wxString(label->label,*wxConvCurrent);
fd0eed64
RR
314 }
315 child = child->next;
316 count++;
6de97a3b 317 }
29006414 318
93c5dd39 319 wxFAIL_MSG( _T("wxChoice: invalid index in GetString()") );
29006414 320
002f4218 321 return _T("");
6de97a3b 322}
c801d85f 323
fd0eed64 324wxString wxChoice::GetStringSelection() const
c801d85f 325{
93c5dd39 326 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
f96aa4d9 327
fd0eed64 328 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 329
93c5dd39 330 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414 331
dcf924a3 332 return wxString(label->label,*wxConvCurrent);
6de97a3b 333}
c801d85f 334
fd0eed64 335int wxChoice::Number() const
c801d85f 336{
93c5dd39 337 wxCHECK_MSG( m_widget != NULL, 0, _T("invalid choice") );
fd0eed64
RR
338
339 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
340 int count = 0;
341 GList *child = menu_shell->children;
342 while (child)
343 {
344 count++;
345 child = child->next;
346 }
347 return count;
6de97a3b 348}
c801d85f 349
debe6624 350void wxChoice::SetColumns( int WXUNUSED(n) )
c801d85f 351{
6de97a3b 352}
c801d85f 353
debe6624 354void wxChoice::SetSelection( int n )
c801d85f 355{
93c5dd39 356 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 357
fd0eed64
RR
358 int tmp = n;
359 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 360}
c801d85f
KB
361
362void wxChoice::SetStringSelection( const wxString &string )
363{
93c5dd39 364 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 365
fd0eed64
RR
366 int n = FindString( string );
367 if (n != -1) SetSelection( n );
6de97a3b 368}
c801d85f 369
953704c1
RR
370void wxChoice::DisableEvents()
371{
372/*
373 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
374 GList *child = menu_shell->children;
375 while (child)
376 {
377 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
378 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
379
380 child = child->next;
381 }
382*/
383}
384
385void wxChoice::EnableEvents()
386{
387/*
388 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
389 GList *child = menu_shell->children;
390 while (child)
391 {
392 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
393 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
394
395 child = child->next;
396 }
397*/
398}
399
58614078 400void wxChoice::ApplyWidgetStyle()
868a2826 401{
fd0eed64 402 SetWidgetStyle();
29006414 403
fd0eed64 404 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 405
fd0eed64
RR
406 gtk_widget_set_style( m_widget, m_widgetStyle );
407 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 408
fd0eed64
RR
409 GList *child = menu_shell->children;
410 while (child)
411 {
412 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 413
fd0eed64
RR
414 GtkBin *bin = GTK_BIN( child->data );
415 GtkWidget *label = (GtkWidget *) NULL;
416 if (bin->child) label = bin->child;
417 if (!label) label = GTK_BUTTON(m_widget)->child;
29006414 418
fd0eed64 419 gtk_widget_set_style( label, m_widgetStyle );
29006414 420
fd0eed64
RR
421 child = child->next;
422 }
f96aa4d9
RR
423}
424
ce4169a4 425#endif