]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
fixed crash when return is pressed in a spinctrl inside a parent which is not a GtkWindow
[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
1e6feb95 15#include "wx/defs.h"
c801d85f 16
ce4169a4
RR
17#if wxUSE_CHOICE
18
1e6feb95
VZ
19#include "wx/choice.h"
20
071a2d78
RR
21#include <gdk/gdk.h>
22#include <gtk/gtk.h>
83624f79 23
acfd422a
RR
24//-----------------------------------------------------------------------------
25// idle system
26//-----------------------------------------------------------------------------
27
28extern void wxapp_install_idle_handler();
29extern bool g_isIdle;
30
66bd6b93
RR
31//-----------------------------------------------------------------------------
32// data
33//-----------------------------------------------------------------------------
34
35extern bool g_blockEventsOnDrag;
36
c801d85f 37//-----------------------------------------------------------------------------
e1e955e1 38// "activate"
c801d85f
KB
39//-----------------------------------------------------------------------------
40
66bd6b93 41static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 42{
e01c8145 43 if (g_isIdle)
4dcaf11a 44 wxapp_install_idle_handler();
acfd422a 45
a2053b27 46 if (!choice->m_hasVMT) return;
29006414 47
acfd422a 48 if (g_blockEventsOnDrag) return;
29006414 49
acfd422a 50 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
6c8a980f
VZ
51 int n = choice->GetSelection();
52
53 event.SetInt( n );
acfd422a
RR
54 event.SetString( choice->GetStringSelection() );
55 event.SetEventObject(choice);
6c8a980f
VZ
56
57 if ( choice->HasClientObjectData() )
58 event.SetClientObject( choice->GetClientObject(n) );
59 else if ( choice->HasClientUntypedData() )
60 event.SetClientData( choice->GetClientData(n) );
61
acfd422a 62 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 63}
c801d85f 64
e1e955e1
RR
65//-----------------------------------------------------------------------------
66// wxChoice
c801d85f
KB
67//-----------------------------------------------------------------------------
68
7f4dc78d 69IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f 70
fd0eed64 71wxChoice::wxChoice()
c801d85f 72{
e01c8145 73 m_strings = (wxSortedArrayString *)NULL;
6de97a3b 74}
c801d85f 75
debe6624 76bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
77 const wxPoint &pos, const wxSize &size,
78 int n, const wxString choices[],
79 long style, const wxValidator& validator, const wxString &name )
c801d85f 80{
fd0eed64 81 m_needParent = TRUE;
034be888
RR
82#if (GTK_MINOR_VERSION > 0)
83 m_acceptsFocus = TRUE;
84#endif
29006414 85
4dcaf11a
RR
86 if (!PreCreation( parent, pos, size ) ||
87 !CreateBase( parent, id, pos, size, style, validator, name ))
88 {
223d09f6 89 wxFAIL_MSG( wxT("wxChoice creation failed") );
e01c8145 90 return FALSE;
4dcaf11a 91 }
6de97a3b 92
fd0eed64 93 m_widget = gtk_option_menu_new();
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
db434467
RR
115 SetFont( parent->GetFont() );
116
df336b7f 117 SetBestSize(size);
db434467 118
fd0eed64
RR
119 SetBackgroundColour( parent->GetBackgroundColour() );
120 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 121
fd0eed64 122 Show( TRUE );
29006414 123
fd0eed64 124 return TRUE;
6de97a3b 125}
29006414 126
fd0eed64
RR
127wxChoice::~wxChoice()
128{
f5e27805 129 Clear();
e01c8145
VZ
130
131 delete m_strings;
fd0eed64
RR
132}
133
9abe166a 134int wxChoice::DoAppend( const wxString &item )
fd0eed64 135{
2ee3ee1b 136 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 137
fd0eed64 138 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 139
071a2d78 140 return GtkAppendHelper(menu, item);
fd0eed64 141}
f96aa4d9 142
6c8a980f 143void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 144{
2ee3ee1b 145 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 146
d6538e2c 147 wxNode *node = m_clientList.Nth( n );
6c8a980f 148 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 149
f5e27805 150 node->SetData( (wxObject*) clientData );
fd0eed64
RR
151}
152
6c8a980f 153void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 154{
2ee3ee1b 155 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 156
d6538e2c 157 wxNode *node = m_clientList.Nth( n );
6c8a980f 158 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 159
f5e27805 160 return node->Data();
6de97a3b 161}
fd0eed64 162
6c8a980f 163void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 164{
2ee3ee1b 165 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 166
d6538e2c 167 wxNode *node = m_clientList.Nth( n );
6c8a980f 168 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 169
fd0eed64 170 wxClientData *cd = (wxClientData*) node->Data();
9abe166a 171 delete cd;
29006414 172
fd0eed64
RR
173 node->SetData( (wxObject*) clientData );
174}
175
6c8a980f 176wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 177{
2ee3ee1b 178 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 179
d6538e2c 180 wxNode *node = m_clientList.Nth( n );
9abe166a 181 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 182 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 183
fd0eed64
RR
184 return (wxClientData*) node->Data();
185}
29006414 186
fd0eed64 187void wxChoice::Clear()
c801d85f 188{
223d09f6 189 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 190
fd0eed64
RR
191 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
192 GtkWidget *menu = gtk_menu_new();
193 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 194
6c8a980f
VZ
195 if ( HasClientObjectData() )
196 {
197 // destroy the data (due to Robert's idea of using wxList<wxObject>
198 // and not wxList<wxClientData> we can't just say
199 // m_clientList.DeleteContents(TRUE) - this would crash!
200 wxNode *node = m_clientList.First();
201 while ( node )
202 {
203 delete (wxClientData *)node->Data();
204 node = node->Next();
205 }
206 }
d6538e2c 207 m_clientList.Clear();
2ee3ee1b
VZ
208
209 if ( m_strings )
210 m_strings->Clear();
6de97a3b 211}
c801d85f 212
2f6407b9
RR
213void wxChoice::Delete( int WXUNUSED(n) )
214{
223d09f6 215 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
2f6407b9
RR
216}
217
c801d85f
KB
218int wxChoice::FindString( const wxString &string ) const
219{
223d09f6 220 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
221
222 // If you read this code once and you think you understand
223 // it, then you are very wrong. Robert Roebling.
29006414 224
fd0eed64
RR
225 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
226 int count = 0;
227 GList *child = menu_shell->children;
228 while (child)
229 {
230 GtkBin *bin = GTK_BIN( child->data );
231 GtkLabel *label = (GtkLabel *) NULL;
232 if (bin->child) label = GTK_LABEL(bin->child);
233 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 234
223d09f6 235 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 236
dcf924a3 237 if (string == wxString(label->label,*wxConvCurrent))
29006414
VZ
238 return count;
239
fd0eed64
RR
240 child = child->next;
241 count++;
242 }
29006414 243
fd0eed64 244 return -1;
6de97a3b 245}
c801d85f 246
9abe166a 247int wxChoice::GetSelection() const
c801d85f 248{
223d09f6 249 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
250
251 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
252 int count = 0;
253 GList *child = menu_shell->children;
254 while (child)
255 {
256 GtkBin *bin = GTK_BIN( child->data );
257 if (!bin->child) return count;
258 child = child->next;
259 count++;
260 }
29006414 261
fd0eed64 262 return -1;
6de97a3b 263}
c801d85f 264
6c8a980f
VZ
265void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
266{
267 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
268
269 wxFAIL_MSG(wxT("not implemented"));
270}
271
debe6624 272wxString wxChoice::GetString( int n ) const
c801d85f 273{
223d09f6 274 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
275
276 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
277 int count = 0;
278 GList *child = menu_shell->children;
279 while (child)
c801d85f 280 {
fd0eed64
RR
281 GtkBin *bin = GTK_BIN( child->data );
282 if (count == n)
283 {
284 GtkLabel *label = (GtkLabel *) NULL;
285 if (bin->child) label = GTK_LABEL(bin->child);
286 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 287
223d09f6 288 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 289
dcf924a3 290 return wxString(label->label,*wxConvCurrent);
fd0eed64
RR
291 }
292 child = child->next;
293 count++;
6de97a3b 294 }
29006414 295
223d09f6 296 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 297
223d09f6 298 return wxT("");
6de97a3b 299}
c801d85f 300
9abe166a 301int wxChoice::GetCount() const
c801d85f 302{
223d09f6 303 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
304
305 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
306 int count = 0;
307 GList *child = menu_shell->children;
308 while (child)
309 {
310 count++;
311 child = child->next;
312 }
313 return count;
6de97a3b 314}
c801d85f 315
debe6624 316void wxChoice::SetSelection( int n )
c801d85f 317{
223d09f6 318 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 319
fd0eed64
RR
320 int tmp = n;
321 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 322}
c801d85f 323
58614078 324void wxChoice::ApplyWidgetStyle()
868a2826 325{
fd0eed64 326 SetWidgetStyle();
29006414 327
fd0eed64 328 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 329
fd0eed64
RR
330 gtk_widget_set_style( m_widget, m_widgetStyle );
331 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 332
fd0eed64
RR
333 GList *child = menu_shell->children;
334 while (child)
335 {
336 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 337
fd0eed64
RR
338 GtkBin *bin = GTK_BIN( child->data );
339 GtkWidget *label = (GtkWidget *) NULL;
340 if (bin->child) label = bin->child;
341 if (!label) label = GTK_BUTTON(m_widget)->child;
29006414 342
fd0eed64 343 gtk_widget_set_style( label, m_widgetStyle );
29006414 344
fd0eed64
RR
345 child = child->next;
346 }
f96aa4d9
RR
347}
348
071a2d78 349size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item)
e01c8145
VZ
350{
351 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
352
353 size_t index;
354 if ( m_strings )
355 {
356 // sorted control, need to insert at the correct index
357 index = m_strings->Add(item);
358
359 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
360
361 if ( index )
362 {
363 m_clientList.Insert( m_clientList.Item(index - 1),
364 (wxObject*) NULL );
365 }
366 else
367 {
6c8a980f 368 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
369 }
370 }
371 else
372 {
373 // normal control, just append
374 gtk_menu_append( GTK_MENU(menu), menu_item );
375
376 m_clientList.Append( (wxObject*) NULL );
377
378 // don't call wxChoice::GetCount() from here because it doesn't work
379 // if we're called from ctor (and GtkMenuShell is still NULL)
11e1c70d 380 index = m_clientList.GetCount() - 1;
e01c8145
VZ
381 }
382
383 if (GTK_WIDGET_REALIZED(m_widget))
384 {
385 gtk_widget_realize( menu_item );
386 gtk_widget_realize( GTK_BIN(menu_item)->child );
387
388 if (m_widgetStyle) ApplyWidgetStyle();
389 }
390
391 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
392 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
393
394 gtk_widget_show( menu_item );
395
396 // return the index of the item in the control
397 return index;
398}
399
f68586e5
VZ
400wxSize wxChoice::DoGetBestSize() const
401{
db434467 402 wxSize ret( wxControl::DoGetBestSize() );
29f54b9b
VZ
403
404 // we know better our horizontal extent: it depends on the longest string
405 // we have
406 ret.x = 0;
407 if ( m_widget )
408 {
409 GdkFont *font = m_font.GetInternalFont();
410
411 wxCoord width;
412 size_t count = GetCount();
413 for ( size_t n = 0; n < count; n++ )
414 {
415 width = (wxCoord)gdk_string_width(font, GetString(n).mbc_str());
416 if ( width > ret.x )
417 ret.x = width;
418 }
419
df336b7f
VZ
420 // add extra for the choice "=" button
421
422 // VZ: I don't know how to get the right value, it seems to be in
423 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
424 // to it - maybe we can use gtk_option_menu_size_request() for this
425 // somehow?
426 //
427 // This default value works only for the default GTK+ theme (i.e.
428 // no theme at all) (FIXME)
429 static const int widthChoiceIndicator = 35;
430 ret.x += widthChoiceIndicator;
29f54b9b
VZ
431 }
432
433 // but not less than the minimal width
434 if ( ret.x < 80 )
435 ret.x = 80;
436
db434467
RR
437 ret.y = 16 + gdk_char_height( m_widget->style->font, 'H' );
438 return ret;
f68586e5
VZ
439}
440
df336b7f
VZ
441#endif // wxUSE_CHOICE
442