]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
debug check for infinite loop in InitializeClasses() added
[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
071a2d78
RR
19#include <gdk/gdk.h>
20#include <gtk/gtk.h>
83624f79 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{
e01c8145 41 if (g_isIdle)
4dcaf11a 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 48 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
6c8a980f
VZ
49 int n = choice->GetSelection();
50
51 event.SetInt( n );
acfd422a
RR
52 event.SetString( choice->GetStringSelection() );
53 event.SetEventObject(choice);
6c8a980f
VZ
54
55 if ( choice->HasClientObjectData() )
56 event.SetClientObject( choice->GetClientObject(n) );
57 else if ( choice->HasClientUntypedData() )
58 event.SetClientData( choice->GetClientData(n) );
59
acfd422a 60 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 61}
c801d85f 62
e1e955e1
RR
63//-----------------------------------------------------------------------------
64// wxChoice
c801d85f
KB
65//-----------------------------------------------------------------------------
66
7f4dc78d 67IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f 68
fd0eed64 69wxChoice::wxChoice()
c801d85f 70{
e01c8145 71 m_strings = (wxSortedArrayString *)NULL;
6de97a3b 72}
c801d85f 73
debe6624 74bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
75 const wxPoint &pos, const wxSize &size,
76 int n, const wxString choices[],
77 long style, const wxValidator& validator, const wxString &name )
c801d85f 78{
fd0eed64 79 m_needParent = TRUE;
034be888
RR
80#if (GTK_MINOR_VERSION > 0)
81 m_acceptsFocus = TRUE;
82#endif
29006414 83
4dcaf11a
RR
84 if (!PreCreation( parent, pos, size ) ||
85 !CreateBase( parent, id, pos, size, style, validator, name ))
86 {
223d09f6 87 wxFAIL_MSG( wxT("wxChoice creation failed") );
e01c8145 88 return FALSE;
4dcaf11a 89 }
6de97a3b 90
fd0eed64 91 m_widget = gtk_option_menu_new();
29006414 92
f68586e5 93 SetSizeOrDefault( size );
29006414 94
e01c8145
VZ
95 if ( style & wxCB_SORT )
96 {
97 // if our m_strings != NULL, DoAppend() will check for it and insert
98 // items in the correct order
99 m_strings = new wxSortedArrayString;
100 }
101
fd0eed64 102 GtkWidget *menu = gtk_menu_new();
29006414 103
fd0eed64
RR
104 for (int i = 0; i < n; i++)
105 {
071a2d78 106 GtkAppendHelper(menu, choices[i]);
fd0eed64 107 }
e01c8145 108
fd0eed64 109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 110
f03fc89f 111 m_parent->DoAddChild( this );
29006414 112
fd0eed64 113 PostCreation();
29006414 114
fd0eed64
RR
115 SetBackgroundColour( parent->GetBackgroundColour() );
116 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 117 SetFont( parent->GetFont() );
f96aa4d9 118
fd0eed64 119 Show( TRUE );
29006414 120
fd0eed64 121 return TRUE;
6de97a3b 122}
29006414 123
fd0eed64
RR
124wxChoice::~wxChoice()
125{
f5e27805 126 Clear();
e01c8145
VZ
127
128 delete m_strings;
fd0eed64
RR
129}
130
9abe166a 131int wxChoice::DoAppend( const wxString &item )
fd0eed64 132{
2ee3ee1b 133 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 134
fd0eed64 135 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 136
071a2d78 137 return GtkAppendHelper(menu, item);
fd0eed64 138}
f96aa4d9 139
6c8a980f 140void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 141{
2ee3ee1b 142 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 143
d6538e2c 144 wxNode *node = m_clientList.Nth( n );
6c8a980f 145 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 146
f5e27805 147 node->SetData( (wxObject*) clientData );
fd0eed64
RR
148}
149
6c8a980f 150void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 151{
2ee3ee1b 152 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 153
d6538e2c 154 wxNode *node = m_clientList.Nth( n );
6c8a980f 155 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 156
f5e27805 157 return node->Data();
6de97a3b 158}
fd0eed64 159
6c8a980f 160void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 161{
2ee3ee1b 162 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 163
d6538e2c 164 wxNode *node = m_clientList.Nth( n );
6c8a980f 165 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 166
fd0eed64 167 wxClientData *cd = (wxClientData*) node->Data();
9abe166a 168 delete cd;
29006414 169
fd0eed64
RR
170 node->SetData( (wxObject*) clientData );
171}
172
6c8a980f 173wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 174{
2ee3ee1b 175 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 176
d6538e2c 177 wxNode *node = m_clientList.Nth( n );
9abe166a 178 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 179 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 180
fd0eed64
RR
181 return (wxClientData*) node->Data();
182}
29006414 183
fd0eed64 184void wxChoice::Clear()
c801d85f 185{
223d09f6 186 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 187
fd0eed64
RR
188 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
189 GtkWidget *menu = gtk_menu_new();
190 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 191
6c8a980f
VZ
192 if ( HasClientObjectData() )
193 {
194 // destroy the data (due to Robert's idea of using wxList<wxObject>
195 // and not wxList<wxClientData> we can't just say
196 // m_clientList.DeleteContents(TRUE) - this would crash!
197 wxNode *node = m_clientList.First();
198 while ( node )
199 {
200 delete (wxClientData *)node->Data();
201 node = node->Next();
202 }
203 }
d6538e2c 204 m_clientList.Clear();
2ee3ee1b
VZ
205
206 if ( m_strings )
207 m_strings->Clear();
6de97a3b 208}
c801d85f 209
2f6407b9
RR
210void wxChoice::Delete( int WXUNUSED(n) )
211{
223d09f6 212 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
2f6407b9
RR
213}
214
c801d85f
KB
215int wxChoice::FindString( const wxString &string ) const
216{
223d09f6 217 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
218
219 // If you read this code once and you think you understand
220 // it, then you are very wrong. Robert Roebling.
29006414 221
fd0eed64
RR
222 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
223 int count = 0;
224 GList *child = menu_shell->children;
225 while (child)
226 {
227 GtkBin *bin = GTK_BIN( child->data );
228 GtkLabel *label = (GtkLabel *) NULL;
229 if (bin->child) label = GTK_LABEL(bin->child);
230 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 231
223d09f6 232 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 233
dcf924a3 234 if (string == wxString(label->label,*wxConvCurrent))
29006414
VZ
235 return count;
236
fd0eed64
RR
237 child = child->next;
238 count++;
239 }
29006414 240
fd0eed64 241 return -1;
6de97a3b 242}
c801d85f 243
9abe166a 244int wxChoice::GetSelection() const
c801d85f 245{
223d09f6 246 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
247
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 if (!bin->child) return count;
255 child = child->next;
256 count++;
257 }
29006414 258
fd0eed64 259 return -1;
6de97a3b 260}
c801d85f 261
6c8a980f
VZ
262void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
263{
264 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
265
266 wxFAIL_MSG(wxT("not implemented"));
267}
268
debe6624 269wxString wxChoice::GetString( int n ) const
c801d85f 270{
223d09f6 271 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
272
273 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
274 int count = 0;
275 GList *child = menu_shell->children;
276 while (child)
c801d85f 277 {
fd0eed64
RR
278 GtkBin *bin = GTK_BIN( child->data );
279 if (count == n)
280 {
281 GtkLabel *label = (GtkLabel *) NULL;
282 if (bin->child) label = GTK_LABEL(bin->child);
283 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 284
223d09f6 285 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 286
dcf924a3 287 return wxString(label->label,*wxConvCurrent);
fd0eed64
RR
288 }
289 child = child->next;
290 count++;
6de97a3b 291 }
29006414 292
223d09f6 293 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 294
223d09f6 295 return wxT("");
6de97a3b 296}
c801d85f 297
9abe166a 298int wxChoice::GetCount() const
c801d85f 299{
223d09f6 300 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
301
302 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
303 int count = 0;
304 GList *child = menu_shell->children;
305 while (child)
306 {
307 count++;
308 child = child->next;
309 }
310 return count;
6de97a3b 311}
c801d85f 312
debe6624 313void wxChoice::SetSelection( int n )
c801d85f 314{
223d09f6 315 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 316
fd0eed64
RR
317 int tmp = n;
318 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 319}
c801d85f 320
58614078 321void wxChoice::ApplyWidgetStyle()
868a2826 322{
fd0eed64 323 SetWidgetStyle();
29006414 324
fd0eed64 325 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 326
fd0eed64
RR
327 gtk_widget_set_style( m_widget, m_widgetStyle );
328 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 329
fd0eed64
RR
330 GList *child = menu_shell->children;
331 while (child)
332 {
333 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 334
fd0eed64
RR
335 GtkBin *bin = GTK_BIN( child->data );
336 GtkWidget *label = (GtkWidget *) NULL;
337 if (bin->child) label = bin->child;
338 if (!label) label = GTK_BUTTON(m_widget)->child;
29006414 339
fd0eed64 340 gtk_widget_set_style( label, m_widgetStyle );
29006414 341
fd0eed64
RR
342 child = child->next;
343 }
f96aa4d9
RR
344}
345
071a2d78 346size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item)
e01c8145
VZ
347{
348 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
349
350 size_t index;
351 if ( m_strings )
352 {
353 // sorted control, need to insert at the correct index
354 index = m_strings->Add(item);
355
356 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
357
358 if ( index )
359 {
360 m_clientList.Insert( m_clientList.Item(index - 1),
361 (wxObject*) NULL );
362 }
363 else
364 {
6c8a980f 365 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
366 }
367 }
368 else
369 {
370 // normal control, just append
371 gtk_menu_append( GTK_MENU(menu), menu_item );
372
373 m_clientList.Append( (wxObject*) NULL );
374
375 // don't call wxChoice::GetCount() from here because it doesn't work
376 // if we're called from ctor (and GtkMenuShell is still NULL)
11e1c70d 377 index = m_clientList.GetCount() - 1;
e01c8145
VZ
378 }
379
380 if (GTK_WIDGET_REALIZED(m_widget))
381 {
382 gtk_widget_realize( menu_item );
383 gtk_widget_realize( GTK_BIN(menu_item)->child );
384
385 if (m_widgetStyle) ApplyWidgetStyle();
386 }
387
388 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
389 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
390
391 gtk_widget_show( menu_item );
392
393 // return the index of the item in the control
394 return index;
395}
396
f68586e5
VZ
397wxSize wxChoice::DoGetBestSize() const
398{
399 return wxSize(80, 26);
400}
401
ce4169a4 402#endif