]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
Added PNM image handler
[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 {
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
fd0eed64 290 return -1;
6de97a3b 291}
c801d85f 292
debe6624 293wxString wxChoice::GetString( int n ) const
c801d85f 294{
93c5dd39 295 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
fd0eed64
RR
296
297 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
298 int count = 0;
299 GList *child = menu_shell->children;
300 while (child)
c801d85f 301 {
fd0eed64
RR
302 GtkBin *bin = GTK_BIN( child->data );
303 if (count == n)
304 {
305 GtkLabel *label = (GtkLabel *) NULL;
306 if (bin->child) label = GTK_LABEL(bin->child);
307 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 308
93c5dd39 309 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414 310
dcf924a3 311 return wxString(label->label,*wxConvCurrent);
fd0eed64
RR
312 }
313 child = child->next;
314 count++;
6de97a3b 315 }
29006414 316
93c5dd39 317 wxFAIL_MSG( _T("wxChoice: invalid index in GetString()") );
29006414 318
002f4218 319 return _T("");
6de97a3b 320}
c801d85f 321
fd0eed64 322wxString wxChoice::GetStringSelection() const
c801d85f 323{
93c5dd39 324 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
f96aa4d9 325
fd0eed64 326 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 327
93c5dd39 328 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414 329
dcf924a3 330 return wxString(label->label,*wxConvCurrent);
6de97a3b 331}
c801d85f 332
fd0eed64 333int wxChoice::Number() const
c801d85f 334{
93c5dd39 335 wxCHECK_MSG( m_widget != NULL, 0, _T("invalid choice") );
fd0eed64
RR
336
337 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
338 int count = 0;
339 GList *child = menu_shell->children;
340 while (child)
341 {
342 count++;
343 child = child->next;
344 }
345 return count;
6de97a3b 346}
c801d85f 347
debe6624 348void wxChoice::SetColumns( int WXUNUSED(n) )
c801d85f 349{
6de97a3b 350}
c801d85f 351
debe6624 352void wxChoice::SetSelection( int n )
c801d85f 353{
93c5dd39 354 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 355
fd0eed64
RR
356 int tmp = n;
357 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 358}
c801d85f
KB
359
360void wxChoice::SetStringSelection( const wxString &string )
361{
93c5dd39 362 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 363
fd0eed64
RR
364 int n = FindString( string );
365 if (n != -1) SetSelection( n );
6de97a3b 366}
c801d85f 367
953704c1
RR
368void wxChoice::DisableEvents()
369{
370/*
371 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
372 GList *child = menu_shell->children;
373 while (child)
374 {
375 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
376 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
377
378 child = child->next;
379 }
380*/
381}
382
383void wxChoice::EnableEvents()
384{
385/*
386 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
387 GList *child = menu_shell->children;
388 while (child)
389 {
390 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
391 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
392
393 child = child->next;
394 }
395*/
396}
397
58614078 398void wxChoice::ApplyWidgetStyle()
868a2826 399{
fd0eed64 400 SetWidgetStyle();
29006414 401
fd0eed64 402 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 403
fd0eed64
RR
404 gtk_widget_set_style( m_widget, m_widgetStyle );
405 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 406
fd0eed64
RR
407 GList *child = menu_shell->children;
408 while (child)
409 {
410 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 411
fd0eed64
RR
412 GtkBin *bin = GTK_BIN( child->data );
413 GtkWidget *label = (GtkWidget *) NULL;
414 if (bin->child) label = bin->child;
415 if (!label) label = GTK_BUTTON(m_widget)->child;
29006414 416
fd0eed64 417 gtk_widget_set_style( label, m_widgetStyle );
29006414 418
fd0eed64
RR
419 child = child->next;
420 }
f96aa4d9
RR
421}
422
ce4169a4 423#endif