]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
provide WM with hint about splash window's purpose under wxGTK2
[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
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
14f355c2 11#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
12#pragma implementation "choice.h"
13#endif
14
1e6feb95 15#include "wx/defs.h"
c801d85f 16
ce4169a4
RR
17#if wxUSE_CHOICE
18
1e6feb95 19#include "wx/choice.h"
2da2f941 20#include "wx/arrstr.h"
1e6feb95 21
9e691f46 22#include "wx/gtk/private.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
584ad2a3
MB
76bool wxChoice::Create( wxWindow *parent, wxWindowID id,
77 const wxPoint &pos, const wxSize &size,
78 const wxArrayString& choices,
79 long style, const wxValidator& validator,
80 const wxString &name )
81{
82 wxCArrayString chs(choices);
83
84 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
85 style, validator, name );
86}
87
debe6624 88bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
89 const wxPoint &pos, const wxSize &size,
90 int n, const wxString choices[],
91 long style, const wxValidator& validator, const wxString &name )
c801d85f 92{
fd0eed64 93 m_needParent = TRUE;
034be888
RR
94#if (GTK_MINOR_VERSION > 0)
95 m_acceptsFocus = TRUE;
96#endif
29006414 97
4dcaf11a
RR
98 if (!PreCreation( parent, pos, size ) ||
99 !CreateBase( parent, id, pos, size, style, validator, name ))
100 {
223d09f6 101 wxFAIL_MSG( wxT("wxChoice creation failed") );
e01c8145 102 return FALSE;
4dcaf11a 103 }
6de97a3b 104
fd0eed64 105 m_widget = gtk_option_menu_new();
29006414 106
e01c8145
VZ
107 if ( style & wxCB_SORT )
108 {
109 // if our m_strings != NULL, DoAppend() will check for it and insert
110 // items in the correct order
111 m_strings = new wxSortedArrayString;
112 }
113
fd0eed64 114 GtkWidget *menu = gtk_menu_new();
29006414 115
fd0eed64
RR
116 for (int i = 0; i < n; i++)
117 {
243dbf1a 118 GtkAddHelper(menu, i, choices[i]);
fd0eed64 119 }
e01c8145 120
fd0eed64 121 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 122
f03fc89f 123 m_parent->DoAddChild( this );
29006414 124
abdeb9e7
RD
125 PostCreation(size);
126 SetBestSize(size); // need this too because this is a wxControlWithItems
29006414 127
fd0eed64 128 return TRUE;
6de97a3b 129}
29006414 130
fd0eed64
RR
131wxChoice::~wxChoice()
132{
f5e27805 133 Clear();
e01c8145
VZ
134
135 delete m_strings;
fd0eed64
RR
136}
137
9abe166a 138int wxChoice::DoAppend( const wxString &item )
fd0eed64 139{
2ee3ee1b 140 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 141
fd0eed64 142 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 143
243dbf1a
VZ
144 return GtkAddHelper(menu, GetCount(), item);
145}
146
147int wxChoice::DoInsert( const wxString &item, int pos )
148{
149 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
150 wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
151
152 if (pos == GetCount())
153 return DoAppend(item);
154
155 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
156
157 return GtkAddHelper(menu, pos, item);
fd0eed64 158}
f96aa4d9 159
6c8a980f 160void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 161{
2ee3ee1b 162 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 163
222ed1d6 164 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 165 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 166
f5e27805 167 node->SetData( (wxObject*) clientData );
fd0eed64
RR
168}
169
6c8a980f 170void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 171{
2ee3ee1b 172 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 173
222ed1d6 174 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 175 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 176
b1d4dd7a 177 return node->GetData();
6de97a3b 178}
fd0eed64 179
6c8a980f 180void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 181{
2ee3ee1b 182 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 183
222ed1d6 184 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 185 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 186
edc973b1 187 // wxItemContainer already deletes data for us
29006414 188
fd0eed64
RR
189 node->SetData( (wxObject*) clientData );
190}
191
6c8a980f 192wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 193{
2ee3ee1b 194 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 195
222ed1d6 196 wxList::compatibility_iterator node = m_clientList.Item( n );
9abe166a 197 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 198 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 199
b1d4dd7a 200 return (wxClientData*) node->GetData();
fd0eed64 201}
29006414 202
fd0eed64 203void wxChoice::Clear()
c801d85f 204{
223d09f6 205 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 206
fd0eed64
RR
207 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
208 GtkWidget *menu = gtk_menu_new();
209 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 210
6c8a980f
VZ
211 if ( HasClientObjectData() )
212 {
213 // destroy the data (due to Robert's idea of using wxList<wxObject>
214 // and not wxList<wxClientData> we can't just say
215 // m_clientList.DeleteContents(TRUE) - this would crash!
222ed1d6 216 wxList::compatibility_iterator node = m_clientList.GetFirst();
6c8a980f
VZ
217 while ( node )
218 {
b1d4dd7a
RL
219 delete (wxClientData *)node->GetData();
220 node = node->GetNext();
6c8a980f
VZ
221 }
222 }
d6538e2c 223 m_clientList.Clear();
2ee3ee1b
VZ
224
225 if ( m_strings )
226 m_strings->Clear();
6de97a3b 227}
c801d85f 228
645420d8 229void wxChoice::Delete( int n )
2f6407b9 230{
645420d8
VZ
231 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
232
233 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
e2380ce1 234 // in 2.0), hence this dumb implementation -- still better than nothing
645420d8
VZ
235 int i,
236 count = GetCount();
237
238 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
239
e2380ce1
VZ
240 const bool hasClientData = m_clientDataItemsType != wxClientData_None;
241 const bool hasObjectData = m_clientDataItemsType == wxClientData_Object;
242
243 wxList::compatibility_iterator node = m_clientList.GetFirst();
244
645420d8 245 wxArrayString items;
e2380ce1 246 wxArrayPtrVoid itemsData;
645420d8
VZ
247 items.Alloc(count);
248 for ( i = 0; i < count; i++ )
249 {
250 if ( i != n )
e2380ce1 251 {
645420d8 252 items.Add(GetString(i));
e2380ce1
VZ
253 if ( hasClientData )
254 {
255 // also save the client data
256 itemsData.Add(node->GetData());
257 }
258 }
259 else // need to delete the client object too
260 {
261 if ( hasObjectData )
262 {
263 delete (wxClientData *)node->GetData();
264 }
265 }
266
267 if ( hasClientData )
268 {
269 node = node->GetNext();
270 }
271 }
272
273 if ( hasObjectData )
274 {
275 // prevent Clear() from destroying all client data
276 m_clientDataItemsType = wxClientData_None;
645420d8
VZ
277 }
278
279 Clear();
280
281 for ( i = 0; i < count - 1; i++ )
282 {
283 Append(items[i]);
e2380ce1
VZ
284
285 if ( hasObjectData )
286 SetClientObject(i, (wxClientData *)itemsData[i]);
287 else if ( hasClientData )
f6bc4102 288 SetClientData(i, itemsData[i]);
645420d8 289 }
2f6407b9
RR
290}
291
c801d85f
KB
292int wxChoice::FindString( const wxString &string ) const
293{
223d09f6 294 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
295
296 // If you read this code once and you think you understand
297 // it, then you are very wrong. Robert Roebling.
29006414 298
fd0eed64
RR
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)
303 {
304 GtkBin *bin = GTK_BIN( child->data );
305 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
306 if (bin->child)
307 label = GTK_LABEL(bin->child);
308 if (!label)
309 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 310
223d09f6 311 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
e2380ce1 312
2e1d7104
RR
313#ifdef __WXGTK20__
314 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
315#else
316 wxString tmp( label->label );
317#endif
fab591c5 318 if (string == tmp)
9e691f46 319 return count;
29006414 320
9e691f46
VZ
321 child = child->next;
322 count++;
fd0eed64 323 }
29006414 324
fd0eed64 325 return -1;
6de97a3b 326}
c801d85f 327
9abe166a 328int wxChoice::GetSelection() const
c801d85f 329{
223d09f6 330 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
e2380ce1 331
2e1d7104 332#ifdef __WXGTK20__
fd0eed64 333
2e1d7104 334 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
e2380ce1 335
2e1d7104 336#else
fd0eed64
RR
337 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
338 int count = 0;
e2380ce1 339
fd0eed64
RR
340 GList *child = menu_shell->children;
341 while (child)
342 {
343 GtkBin *bin = GTK_BIN( child->data );
344 if (!bin->child) return count;
345 child = child->next;
346 count++;
347 }
29006414 348
fd0eed64 349 return -1;
2e1d7104 350#endif
6de97a3b 351}
c801d85f 352
a912c184 353void wxChoice::SetString( int n, const wxString& str )
6c8a980f
VZ
354{
355 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
356
34b5e560
RR
357 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
358 int count = 0;
359 GList *child = menu_shell->children;
360 while (child)
361 {
362 GtkBin *bin = GTK_BIN( child->data );
363 if (count == n)
364 {
365 GtkLabel *label = (GtkLabel *) NULL;
366 if (bin->child)
367 label = GTK_LABEL(bin->child);
368 if (!label)
369 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
370
371 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
372
373 gtk_label_set_text( label, wxGTK_CONV( str ) );
374
375 return;
376 }
377 child = child->next;
378 count++;
379 }
6c8a980f
VZ
380}
381
debe6624 382wxString wxChoice::GetString( int n ) const
c801d85f 383{
223d09f6 384 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
385
386 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
387 int count = 0;
388 GList *child = menu_shell->children;
389 while (child)
c801d85f 390 {
fd0eed64
RR
391 GtkBin *bin = GTK_BIN( child->data );
392 if (count == n)
393 {
394 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
395 if (bin->child)
396 label = GTK_LABEL(bin->child);
397 if (!label)
398 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 399
223d09f6 400 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 401
2e1d7104
RR
402#ifdef __WXGTK20__
403 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
404#else
405 return wxString( label->label );
406#endif
fd0eed64
RR
407 }
408 child = child->next;
409 count++;
6de97a3b 410 }
29006414 411
223d09f6 412 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 413
223d09f6 414 return wxT("");
6de97a3b 415}
c801d85f 416
9abe166a 417int wxChoice::GetCount() const
c801d85f 418{
223d09f6 419 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
420
421 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
422 int count = 0;
423 GList *child = menu_shell->children;
424 while (child)
425 {
426 count++;
427 child = child->next;
428 }
429 return count;
6de97a3b 430}
c801d85f 431
debe6624 432void wxChoice::SetSelection( int n )
c801d85f 433{
223d09f6 434 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 435
fd0eed64
RR
436 int tmp = n;
437 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 438}
c801d85f 439
f40fdaa3 440void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 441{
fd0eed64 442 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 443
f40fdaa3
VS
444 gtk_widget_modify_style( m_widget, style );
445 gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style );
29006414 446
fd0eed64
RR
447 GList *child = menu_shell->children;
448 while (child)
449 {
f40fdaa3 450 gtk_widget_modify_style( GTK_WIDGET( child->data ), style );
29006414 451
fd0eed64
RR
452 GtkBin *bin = GTK_BIN( child->data );
453 GtkWidget *label = (GtkWidget *) NULL;
9e691f46
VZ
454 if (bin->child)
455 label = bin->child;
456 if (!label)
457 label = BUTTON_CHILD(m_widget);
29006414 458
f40fdaa3 459 gtk_widget_modify_style( label, style );
29006414 460
fd0eed64
RR
461 child = child->next;
462 }
f96aa4d9
RR
463}
464
243dbf1a 465int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
e01c8145 466{
243dbf1a
VZ
467 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
468
fab591c5 469 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
e01c8145
VZ
470
471 size_t index;
472 if ( m_strings )
473 {
474 // sorted control, need to insert at the correct index
475 index = m_strings->Add(item);
476
477 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
478
479 if ( index )
480 {
481 m_clientList.Insert( m_clientList.Item(index - 1),
482 (wxObject*) NULL );
483 }
484 else
485 {
6c8a980f 486 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
487 }
488 }
489 else
490 {
243dbf1a
VZ
491 // don't call wxChoice::GetCount() from here because it doesn't work
492 // if we're called from ctor (and GtkMenuShell is still NULL)
493
e01c8145 494 // normal control, just append
243dbf1a
VZ
495 if (pos == (int)m_clientList.GetCount())
496 {
e01c8145 497 gtk_menu_append( GTK_MENU(menu), menu_item );
e01c8145 498 m_clientList.Append( (wxObject*) NULL );
11e1c70d 499 index = m_clientList.GetCount() - 1;
243dbf1a
VZ
500 }
501 else
502 {
503 gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
504 m_clientList.Insert( pos, (wxObject*) NULL );
505 index = pos;
506 }
e01c8145
VZ
507 }
508
509 if (GTK_WIDGET_REALIZED(m_widget))
510 {
511 gtk_widget_realize( menu_item );
512 gtk_widget_realize( GTK_BIN(menu_item)->child );
513
f40fdaa3 514 ApplyWidgetStyle();
e01c8145
VZ
515 }
516
517 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
518 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
519
520 gtk_widget_show( menu_item );
521
522 // return the index of the item in the control
523 return index;
524}
525
f68586e5
VZ
526wxSize wxChoice::DoGetBestSize() const
527{
db434467 528 wxSize ret( wxControl::DoGetBestSize() );
29f54b9b
VZ
529
530 // we know better our horizontal extent: it depends on the longest string
531 // we have
532 ret.x = 0;
533 if ( m_widget )
534 {
60d85ccb 535 int width;
29f54b9b
VZ
536 size_t count = GetCount();
537 for ( size_t n = 0; n < count; n++ )
538 {
2b1ff57f 539 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
29f54b9b
VZ
540 if ( width > ret.x )
541 ret.x = width;
542 }
543
df336b7f
VZ
544 // add extra for the choice "=" button
545
546 // VZ: I don't know how to get the right value, it seems to be in
547 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
548 // to it - maybe we can use gtk_option_menu_size_request() for this
549 // somehow?
550 //
551 // This default value works only for the default GTK+ theme (i.e.
552 // no theme at all) (FIXME)
553 static const int widthChoiceIndicator = 35;
554 ret.x += widthChoiceIndicator;
29f54b9b
VZ
555 }
556
557 // but not less than the minimal width
558 if ( ret.x < 80 )
559 ret.x = 80;
560
ebbb22bd
RR
561 // If this request_size is called with no entries then
562 // the returned height is wrong. Give it a reasonable
563 // default value.
564 if (ret.y <= 18)
565 ret.y = 8 + GetCharHeight();
9e691f46 566
9f884528 567 CacheBestSize(ret);
db434467 568 return ret;
f68586e5
VZ
569}
570
2b904684
RR
571bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
572{
573#ifdef __WXGTK20__
574 return GTK_BUTTON(m_widget)->event_window;
575#else
576 return (window == m_widget->window);
577#endif
578}
579
9d522606
RD
580// static
581wxVisualAttributes
582wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
583{
584 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new);
585}
586
2b904684 587
df336b7f
VZ
588#endif // wxUSE_CHOICE
589