]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
GetFirst/NextChild() now use wxTreeItemIdValue instead of long
[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
9e691f46 21#include "wx/gtk/private.h"
83624f79 22
acfd422a
RR
23//-----------------------------------------------------------------------------
24// idle system
25//-----------------------------------------------------------------------------
26
27extern void wxapp_install_idle_handler();
28extern bool g_isIdle;
29
66bd6b93
RR
30//-----------------------------------------------------------------------------
31// data
32//-----------------------------------------------------------------------------
33
34extern bool g_blockEventsOnDrag;
35
c801d85f 36//-----------------------------------------------------------------------------
e1e955e1 37// "activate"
c801d85f
KB
38//-----------------------------------------------------------------------------
39
66bd6b93 40static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 41{
e01c8145 42 if (g_isIdle)
4dcaf11a 43 wxapp_install_idle_handler();
acfd422a 44
a2053b27 45 if (!choice->m_hasVMT) return;
29006414 46
acfd422a 47 if (g_blockEventsOnDrag) return;
29006414 48
acfd422a 49 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
6c8a980f
VZ
50 int n = choice->GetSelection();
51
52 event.SetInt( n );
acfd422a
RR
53 event.SetString( choice->GetStringSelection() );
54 event.SetEventObject(choice);
6c8a980f
VZ
55
56 if ( choice->HasClientObjectData() )
57 event.SetClientObject( choice->GetClientObject(n) );
58 else if ( choice->HasClientUntypedData() )
59 event.SetClientData( choice->GetClientData(n) );
60
acfd422a 61 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 62}
c801d85f 63
e1e955e1
RR
64//-----------------------------------------------------------------------------
65// wxChoice
c801d85f
KB
66//-----------------------------------------------------------------------------
67
7f4dc78d 68IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f 69
fd0eed64 70wxChoice::wxChoice()
c801d85f 71{
e01c8145 72 m_strings = (wxSortedArrayString *)NULL;
6de97a3b 73}
c801d85f 74
debe6624 75bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
76 const wxPoint &pos, const wxSize &size,
77 int n, const wxString choices[],
78 long style, const wxValidator& validator, const wxString &name )
c801d85f 79{
fd0eed64 80 m_needParent = TRUE;
034be888
RR
81#if (GTK_MINOR_VERSION > 0)
82 m_acceptsFocus = TRUE;
83#endif
29006414 84
4dcaf11a
RR
85 if (!PreCreation( parent, pos, size ) ||
86 !CreateBase( parent, id, pos, size, style, validator, name ))
87 {
223d09f6 88 wxFAIL_MSG( wxT("wxChoice creation failed") );
e01c8145 89 return FALSE;
4dcaf11a 90 }
6de97a3b 91
fd0eed64 92 m_widget = gtk_option_menu_new();
29006414 93
e01c8145
VZ
94 if ( style & wxCB_SORT )
95 {
96 // if our m_strings != NULL, DoAppend() will check for it and insert
97 // items in the correct order
98 m_strings = new wxSortedArrayString;
99 }
100
fd0eed64 101 GtkWidget *menu = gtk_menu_new();
29006414 102
fd0eed64
RR
103 for (int i = 0; i < n; i++)
104 {
243dbf1a 105 GtkAddHelper(menu, i, choices[i]);
fd0eed64 106 }
e01c8145 107
fd0eed64 108 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 109
f03fc89f 110 m_parent->DoAddChild( this );
29006414 111
fd0eed64 112 PostCreation();
29006414 113
db434467
RR
114 SetFont( parent->GetFont() );
115
df336b7f 116 SetBestSize(size);
db434467 117
fd0eed64
RR
118 SetBackgroundColour( parent->GetBackgroundColour() );
119 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 120
fd0eed64 121 Show( TRUE );
29006414 122
fd0eed64 123 return TRUE;
6de97a3b 124}
29006414 125
fd0eed64
RR
126wxChoice::~wxChoice()
127{
f5e27805 128 Clear();
e01c8145
VZ
129
130 delete m_strings;
fd0eed64
RR
131}
132
9abe166a 133int wxChoice::DoAppend( const wxString &item )
fd0eed64 134{
2ee3ee1b 135 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 136
fd0eed64 137 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 138
243dbf1a
VZ
139 return GtkAddHelper(menu, GetCount(), item);
140}
141
142int wxChoice::DoInsert( const wxString &item, int pos )
143{
144 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
145 wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
146
147 if (pos == GetCount())
148 return DoAppend(item);
149
150 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
151
152 return GtkAddHelper(menu, pos, item);
fd0eed64 153}
f96aa4d9 154
6c8a980f 155void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 156{
2ee3ee1b 157 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 158
222ed1d6 159 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 160 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 161
f5e27805 162 node->SetData( (wxObject*) clientData );
fd0eed64
RR
163}
164
6c8a980f 165void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 166{
2ee3ee1b 167 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 168
222ed1d6 169 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 170 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 171
b1d4dd7a 172 return node->GetData();
6de97a3b 173}
fd0eed64 174
6c8a980f 175void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 176{
2ee3ee1b 177 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 178
222ed1d6 179 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 180 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 181
edc973b1 182 // wxItemContainer already deletes data for us
29006414 183
fd0eed64
RR
184 node->SetData( (wxObject*) clientData );
185}
186
6c8a980f 187wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 188{
2ee3ee1b 189 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 190
222ed1d6 191 wxList::compatibility_iterator node = m_clientList.Item( n );
9abe166a 192 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 193 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 194
b1d4dd7a 195 return (wxClientData*) node->GetData();
fd0eed64 196}
29006414 197
fd0eed64 198void wxChoice::Clear()
c801d85f 199{
223d09f6 200 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 201
fd0eed64
RR
202 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
203 GtkWidget *menu = gtk_menu_new();
204 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 205
6c8a980f
VZ
206 if ( HasClientObjectData() )
207 {
208 // destroy the data (due to Robert's idea of using wxList<wxObject>
209 // and not wxList<wxClientData> we can't just say
210 // m_clientList.DeleteContents(TRUE) - this would crash!
222ed1d6 211 wxList::compatibility_iterator node = m_clientList.GetFirst();
6c8a980f
VZ
212 while ( node )
213 {
b1d4dd7a
RL
214 delete (wxClientData *)node->GetData();
215 node = node->GetNext();
6c8a980f
VZ
216 }
217 }
d6538e2c 218 m_clientList.Clear();
2ee3ee1b
VZ
219
220 if ( m_strings )
221 m_strings->Clear();
6de97a3b 222}
c801d85f 223
645420d8 224void wxChoice::Delete( int n )
2f6407b9 225{
645420d8
VZ
226 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
227
228 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
229 // in 2.0), hence this dump implementation - still better than nothing
230 int i,
231 count = GetCount();
232
233 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
234
235 wxArrayString items;
236 items.Alloc(count);
237 for ( i = 0; i < count; i++ )
238 {
239 if ( i != n )
240 items.Add(GetString(i));
241 }
242
243 Clear();
244
245 for ( i = 0; i < count - 1; i++ )
246 {
247 Append(items[i]);
248 }
2f6407b9
RR
249}
250
c801d85f
KB
251int wxChoice::FindString( const wxString &string ) const
252{
223d09f6 253 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
254
255 // If you read this code once and you think you understand
256 // it, then you are very wrong. Robert Roebling.
29006414 257
fd0eed64
RR
258 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
259 int count = 0;
260 GList *child = menu_shell->children;
261 while (child)
262 {
263 GtkBin *bin = GTK_BIN( child->data );
264 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
265 if (bin->child)
266 label = GTK_LABEL(bin->child);
267 if (!label)
268 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 269
223d09f6 270 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
fab591c5 271
2e1d7104
RR
272#ifdef __WXGTK20__
273 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
274#else
275 wxString tmp( label->label );
276#endif
fab591c5 277 if (string == tmp)
9e691f46 278 return count;
29006414 279
9e691f46
VZ
280 child = child->next;
281 count++;
fd0eed64 282 }
29006414 283
fd0eed64 284 return -1;
6de97a3b 285}
c801d85f 286
9abe166a 287int wxChoice::GetSelection() const
c801d85f 288{
223d09f6 289 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
2e1d7104
RR
290
291#ifdef __WXGTK20__
fd0eed64 292
2e1d7104
RR
293 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
294
295#else
fd0eed64
RR
296 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
297 int count = 0;
2e1d7104 298
fd0eed64
RR
299 GList *child = menu_shell->children;
300 while (child)
301 {
302 GtkBin *bin = GTK_BIN( child->data );
303 if (!bin->child) return count;
304 child = child->next;
305 count++;
306 }
29006414 307
fd0eed64 308 return -1;
2e1d7104 309#endif
6de97a3b 310}
c801d85f 311
6c8a980f
VZ
312void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
313{
314 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
315
316 wxFAIL_MSG(wxT("not implemented"));
317}
318
debe6624 319wxString wxChoice::GetString( int n ) const
c801d85f 320{
223d09f6 321 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
322
323 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
324 int count = 0;
325 GList *child = menu_shell->children;
326 while (child)
c801d85f 327 {
fd0eed64
RR
328 GtkBin *bin = GTK_BIN( child->data );
329 if (count == n)
330 {
331 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
332 if (bin->child)
333 label = GTK_LABEL(bin->child);
334 if (!label)
335 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 336
223d09f6 337 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 338
2e1d7104
RR
339#ifdef __WXGTK20__
340 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
341#else
342 return wxString( label->label );
343#endif
fd0eed64
RR
344 }
345 child = child->next;
346 count++;
6de97a3b 347 }
29006414 348
223d09f6 349 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 350
223d09f6 351 return wxT("");
6de97a3b 352}
c801d85f 353
9abe166a 354int wxChoice::GetCount() const
c801d85f 355{
223d09f6 356 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
357
358 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
359 int count = 0;
360 GList *child = menu_shell->children;
361 while (child)
362 {
363 count++;
364 child = child->next;
365 }
366 return count;
6de97a3b 367}
c801d85f 368
debe6624 369void wxChoice::SetSelection( int n )
c801d85f 370{
223d09f6 371 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 372
fd0eed64
RR
373 int tmp = n;
374 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 375}
c801d85f 376
58614078 377void wxChoice::ApplyWidgetStyle()
868a2826 378{
fd0eed64 379 SetWidgetStyle();
29006414 380
fd0eed64 381 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 382
fd0eed64
RR
383 gtk_widget_set_style( m_widget, m_widgetStyle );
384 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 385
fd0eed64
RR
386 GList *child = menu_shell->children;
387 while (child)
388 {
389 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 390
fd0eed64
RR
391 GtkBin *bin = GTK_BIN( child->data );
392 GtkWidget *label = (GtkWidget *) NULL;
9e691f46
VZ
393 if (bin->child)
394 label = bin->child;
395 if (!label)
396 label = BUTTON_CHILD(m_widget);
29006414 397
fd0eed64 398 gtk_widget_set_style( label, m_widgetStyle );
29006414 399
fd0eed64
RR
400 child = child->next;
401 }
f96aa4d9
RR
402}
403
243dbf1a 404int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
e01c8145 405{
243dbf1a
VZ
406 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
407
fab591c5 408 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
e01c8145
VZ
409
410 size_t index;
411 if ( m_strings )
412 {
413 // sorted control, need to insert at the correct index
414 index = m_strings->Add(item);
415
416 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
417
418 if ( index )
419 {
420 m_clientList.Insert( m_clientList.Item(index - 1),
421 (wxObject*) NULL );
422 }
423 else
424 {
6c8a980f 425 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
426 }
427 }
428 else
429 {
243dbf1a
VZ
430 // don't call wxChoice::GetCount() from here because it doesn't work
431 // if we're called from ctor (and GtkMenuShell is still NULL)
432
e01c8145 433 // normal control, just append
243dbf1a
VZ
434 if (pos == (int)m_clientList.GetCount())
435 {
e01c8145 436 gtk_menu_append( GTK_MENU(menu), menu_item );
e01c8145 437 m_clientList.Append( (wxObject*) NULL );
11e1c70d 438 index = m_clientList.GetCount() - 1;
243dbf1a
VZ
439 }
440 else
441 {
442 gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
443 m_clientList.Insert( pos, (wxObject*) NULL );
444 index = pos;
445 }
e01c8145
VZ
446 }
447
448 if (GTK_WIDGET_REALIZED(m_widget))
449 {
450 gtk_widget_realize( menu_item );
451 gtk_widget_realize( GTK_BIN(menu_item)->child );
452
453 if (m_widgetStyle) ApplyWidgetStyle();
454 }
455
456 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
457 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
458
459 gtk_widget_show( menu_item );
460
461 // return the index of the item in the control
462 return index;
463}
464
f68586e5
VZ
465wxSize wxChoice::DoGetBestSize() const
466{
db434467 467 wxSize ret( wxControl::DoGetBestSize() );
29f54b9b
VZ
468
469 // we know better our horizontal extent: it depends on the longest string
470 // we have
471 ret.x = 0;
472 if ( m_widget )
473 {
60d85ccb 474 int width;
29f54b9b
VZ
475 size_t count = GetCount();
476 for ( size_t n = 0; n < count; n++ )
477 {
60d85ccb 478 GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font );
29f54b9b
VZ
479 if ( width > ret.x )
480 ret.x = width;
481 }
482
df336b7f
VZ
483 // add extra for the choice "=" button
484
485 // VZ: I don't know how to get the right value, it seems to be in
486 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
487 // to it - maybe we can use gtk_option_menu_size_request() for this
488 // somehow?
489 //
490 // This default value works only for the default GTK+ theme (i.e.
491 // no theme at all) (FIXME)
492 static const int widthChoiceIndicator = 35;
493 ret.x += widthChoiceIndicator;
29f54b9b
VZ
494 }
495
496 // but not less than the minimal width
497 if ( ret.x < 80 )
498 ret.x = 80;
499
288059b2 500 ret.y = 16 + GetCharHeight();
9e691f46 501
db434467 502 return ret;
f68586e5
VZ
503}
504
2b904684
RR
505bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
506{
507#ifdef __WXGTK20__
508 return GTK_BUTTON(m_widget)->event_window;
509#else
510 return (window == m_widget->window);
511#endif
512}
513
514
df336b7f
VZ
515#endif // wxUSE_CHOICE
516