]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
Move code removing "-psn_xxx" command line arguments to common code.
[wxWidgets.git] / src / gtk1 / choice.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
3cbab641 2// Name: src/gtk1/choice.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
01111366 5// Copyright: (c) 1998 Robert Roebling
65571936 6// Licence: wxWindows licence
c801d85f
KB
7/////////////////////////////////////////////////////////////////////////////
8
8228b893 9#include "wx/wxprec.h"
c801d85f 10
ce4169a4
RR
11#if wxUSE_CHOICE
12
1e6feb95 13#include "wx/choice.h"
aaa6d89a
WS
14
15#ifndef WX_PRECOMP
16 #include "wx/arrstr.h"
17#endif
1e6feb95 18
3cbab641 19#include "wx/gtk1/private.h"
83624f79 20
acfd422a
RR
21//-----------------------------------------------------------------------------
22// idle system
23//-----------------------------------------------------------------------------
24
25extern void wxapp_install_idle_handler();
26extern bool g_isIdle;
27
66bd6b93
RR
28//-----------------------------------------------------------------------------
29// data
30//-----------------------------------------------------------------------------
31
32extern bool g_blockEventsOnDrag;
33
c801d85f 34//-----------------------------------------------------------------------------
e1e955e1 35// "activate"
c801d85f
KB
36//-----------------------------------------------------------------------------
37
865bb325 38extern "C" {
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
5f3565a2
RR
48 int selection = wxNOT_FOUND;
49
5f3565a2
RR
50 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(choice->GetHandle()) ) );
51 int count = 0;
52
53 GList *child = menu_shell->children;
54 while (child)
55 {
56 GtkBin *bin = GTK_BIN( child->data );
57 if (!bin->child)
58 {
914ca1d8 59 selection = count;
5f3565a2
RR
60 break;
61 }
62 child = child->next;
63 count++;
64 }
3cbab641 65
5f3565a2
RR
66 choice->m_selection_hack = selection;
67
ce7fe42e 68 wxCommandEvent event(wxEVT_CHOICE, choice->GetId() );
6c8a980f
VZ
69 int n = choice->GetSelection();
70
71 event.SetInt( n );
acfd422a
RR
72 event.SetString( choice->GetStringSelection() );
73 event.SetEventObject(choice);
6c8a980f
VZ
74
75 if ( choice->HasClientObjectData() )
76 event.SetClientObject( choice->GetClientObject(n) );
77 else if ( choice->HasClientUntypedData() )
78 event.SetClientData( choice->GetClientData(n) );
79
937013e0 80 choice->HandleWindowEvent(event);
6de97a3b 81}
865bb325 82}
c801d85f 83
e1e955e1
RR
84//-----------------------------------------------------------------------------
85// wxChoice
c801d85f
KB
86//-----------------------------------------------------------------------------
87
fd0eed64 88wxChoice::wxChoice()
c801d85f 89{
d3b9f782 90 m_strings = NULL;
6de97a3b 91}
c801d85f 92
584ad2a3
MB
93bool wxChoice::Create( wxWindow *parent, wxWindowID id,
94 const wxPoint &pos, const wxSize &size,
95 const wxArrayString& choices,
96 long style, const wxValidator& validator,
97 const wxString &name )
98{
99 wxCArrayString chs(choices);
100
101 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
102 style, validator, name );
103}
104
debe6624 105bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
106 const wxPoint &pos, const wxSize &size,
107 int n, const wxString choices[],
108 long style, const wxValidator& validator, const wxString &name )
c801d85f 109{
0a164d4c 110 m_needParent = true;
034be888 111#if (GTK_MINOR_VERSION > 0)
0a164d4c 112 m_acceptsFocus = true;
034be888 113#endif
29006414 114
4dcaf11a
RR
115 if (!PreCreation( parent, pos, size ) ||
116 !CreateBase( parent, id, pos, size, style, validator, name ))
117 {
223d09f6 118 wxFAIL_MSG( wxT("wxChoice creation failed") );
0a164d4c 119 return false;
4dcaf11a 120 }
6de97a3b 121
fd0eed64 122 m_widget = gtk_option_menu_new();
29006414 123
e01c8145
VZ
124 if ( style & wxCB_SORT )
125 {
a236aa20 126 // if our m_strings != NULL, Append() will check for it and insert
e01c8145
VZ
127 // items in the correct order
128 m_strings = new wxSortedArrayString;
129 }
130
16edee16
RR
131 // begin with no selection
132 m_selection_hack = wxNOT_FOUND;
133
fd0eed64 134 GtkWidget *menu = gtk_menu_new();
29006414 135
3b69b47c 136 for (unsigned int i = 0; i < (unsigned int)n; i++)
fd0eed64 137 {
243dbf1a 138 GtkAddHelper(menu, i, choices[i]);
fd0eed64 139 }
e01c8145 140
fd0eed64 141 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 142
f03fc89f 143 m_parent->DoAddChild( this );
29006414 144
abdeb9e7 145 PostCreation(size);
170acdc9 146 SetInitialSize(size); // need this too because this is a wxControlWithItems
29006414 147
0a164d4c 148 return true;
6de97a3b 149}
29006414 150
fd0eed64
RR
151wxChoice::~wxChoice()
152{
f5e27805 153 Clear();
e01c8145
VZ
154
155 delete m_strings;
fd0eed64
RR
156}
157
a236aa20
VZ
158int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
159 unsigned int pos,
160 void **clientData, wxClientDataType type)
fd0eed64 161{
2ee3ee1b 162 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 163
a236aa20 164 const unsigned int count = items.GetCount();
243dbf1a 165
a236aa20 166 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
243dbf1a 167
a236aa20
VZ
168 for ( unsigned int i = 0; i < count; ++i, ++pos )
169 {
170 int n = GtkAddHelper(menu, pos, items[i]);
171 if ( n == wxNOT_FOUND )
172 return n;
243dbf1a 173
a236aa20
VZ
174 AssignNewItemClientData(n, clientData, i, type);
175 }
243dbf1a 176
16edee16 177 // if the item to insert is at or before the selection, and the selection is valid
aa61d352 178 if (((int)pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
16edee16 179 {
a236aa20
VZ
180 // move the selection forward
181 m_selection_hack += count;
16edee16
RR
182 }
183
a236aa20 184 return pos - 1;
fd0eed64 185}
f96aa4d9 186
aa61d352 187void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 188{
2ee3ee1b 189 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 190
222ed1d6 191 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 192 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 193
f5e27805 194 node->SetData( (wxObject*) clientData );
fd0eed64
RR
195}
196
aa61d352 197void* wxChoice::DoGetItemClientData(unsigned int n) const
fd0eed64 198{
2ee3ee1b 199 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 200
222ed1d6 201 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 202 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 203
b1d4dd7a 204 return node->GetData();
6de97a3b 205}
fd0eed64 206
a236aa20 207void wxChoice::DoClear()
c801d85f 208{
223d09f6 209 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 210
fd0eed64
RR
211 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
212 GtkWidget *menu = gtk_menu_new();
213 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 214
d6538e2c 215 m_clientList.Clear();
2ee3ee1b
VZ
216
217 if ( m_strings )
218 m_strings->Clear();
16edee16
RR
219
220 // begin with no selection
221 m_selection_hack = wxNOT_FOUND;
6de97a3b 222}
c801d85f 223
a236aa20 224void wxChoice::DoDeleteOneItem(unsigned int n)
2f6407b9 225{
645420d8
VZ
226 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
227
9a83f860 228 wxCHECK_RET( IsValid(n), wxT("invalid index in wxChoice::Delete") );
645420d8 229
16edee16 230 // if the item to delete is before the selection, and the selection is valid
aa61d352 231 if (((int)n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
16edee16
RR
232 {
233 // move the selection back one
234 m_selection_hack--;
235 }
aa61d352 236 else if ((int)n == m_selection_hack)
16edee16
RR
237 {
238 // invalidate the selection
239 m_selection_hack = wxNOT_FOUND;
240 }
241
a236aa20
VZ
242 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
243 // in 2.0), hence this dumb implementation -- still better than nothing
244 const unsigned int count = GetCount();
e2380ce1
VZ
245
246 wxList::compatibility_iterator node = m_clientList.GetFirst();
247
645420d8 248 wxArrayString items;
e2380ce1 249 wxArrayPtrVoid itemsData;
645420d8 250 items.Alloc(count);
a236aa20 251 for ( unsigned i = 0; i < count; i++, node = node->GetNext() )
645420d8
VZ
252 {
253 if ( i != n )
e2380ce1 254 {
645420d8 255 items.Add(GetString(i));
a236aa20 256 itemsData.Add(node->GetData());
e2380ce1
VZ
257 }
258 }
259
a236aa20 260 wxChoice::DoClear();
645420d8 261
a236aa20
VZ
262 void ** const data = &itemsData[0];
263 if ( HasClientObjectData() )
5c33522f 264 Append(items, reinterpret_cast<wxClientData **>(data));
a236aa20
VZ
265 else
266 Append(items, data);
2f6407b9
RR
267}
268
11e62fe6 269int wxChoice::FindString( const wxString &string, bool bCase ) const
c801d85f 270{
11e62fe6 271 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
fd0eed64
RR
272
273 // If you read this code once and you think you understand
274 // it, then you are very wrong. Robert Roebling.
29006414 275
fd0eed64
RR
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)
280 {
281 GtkBin *bin = GTK_BIN( child->data );
d3b9f782 282 GtkLabel *label = NULL;
9e691f46
VZ
283 if (bin->child)
284 label = GTK_LABEL(bin->child);
285 if (!label)
286 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 287
223d09f6 288 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
e2380ce1 289
2e1d7104 290 wxString tmp( label->label );
3cbab641
MR
291
292 if (string.IsSameAs( tmp, bCase ))
9e691f46 293 return count;
29006414 294
9e691f46
VZ
295 child = child->next;
296 count++;
fd0eed64 297 }
29006414 298
0a164d4c 299 return wxNOT_FOUND;
6de97a3b 300}
c801d85f 301
9abe166a 302int wxChoice::GetSelection() const
c801d85f 303{
11e62fe6 304 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
e2380ce1 305
16edee16
RR
306 return m_selection_hack;
307
6de97a3b 308}
c801d85f 309
aa61d352 310void wxChoice::SetString(unsigned int n, const wxString& str )
6c8a980f
VZ
311{
312 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
313
34b5e560 314 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
aa61d352 315 unsigned int count = 0;
34b5e560
RR
316 GList *child = menu_shell->children;
317 while (child)
318 {
319 GtkBin *bin = GTK_BIN( child->data );
320 if (count == n)
321 {
d3b9f782 322 GtkLabel *label = NULL;
34b5e560
RR
323 if (bin->child)
324 label = GTK_LABEL(bin->child);
325 if (!label)
326 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
327
328 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
329
0a164d4c
WS
330 gtk_label_set_text( label, wxGTK_CONV( str ) );
331
34b5e560
RR
332 return;
333 }
334 child = child->next;
335 count++;
336 }
6c8a980f
VZ
337}
338
aa61d352 339wxString wxChoice::GetString(unsigned int n) const
c801d85f 340{
0a164d4c 341 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid choice") );
fd0eed64
RR
342
343 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
aa61d352 344 unsigned int count = 0;
fd0eed64
RR
345 GList *child = menu_shell->children;
346 while (child)
c801d85f 347 {
fd0eed64
RR
348 GtkBin *bin = GTK_BIN( child->data );
349 if (count == n)
350 {
d3b9f782 351 GtkLabel *label = NULL;
9e691f46
VZ
352 if (bin->child)
353 label = GTK_LABEL(bin->child);
354 if (!label)
355 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 356
223d09f6 357 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 358
2e1d7104 359 return wxString( label->label );
fd0eed64
RR
360 }
361 child = child->next;
362 count++;
6de97a3b 363 }
29006414 364
223d09f6 365 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 366
0a164d4c 367 return wxEmptyString;
6de97a3b 368}
c801d85f 369
aa61d352 370unsigned int wxChoice::GetCount() const
c801d85f 371{
223d09f6 372 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
373
374 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
aa61d352 375 unsigned int count = 0;
fd0eed64
RR
376 GList *child = menu_shell->children;
377 while (child)
378 {
379 count++;
380 child = child->next;
381 }
382 return count;
6de97a3b 383}
c801d85f 384
debe6624 385void wxChoice::SetSelection( int n )
c801d85f 386{
223d09f6 387 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 388
fd0eed64
RR
389 int tmp = n;
390 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
16edee16
RR
391
392 // set the local selection variable manually
aa61d352 393 if (IsValid((unsigned int)n))
16edee16
RR
394 {
395 // a valid selection has been made
396 m_selection_hack = n;
397 }
398 else if ((n == wxNOT_FOUND) || (GetCount() == 0))
399 {
400 // invalidates the selection if there are no items
401 // or if it is specifically set to wxNOT_FOUND
402 m_selection_hack = wxNOT_FOUND;
403 }
404 else
405 {
406 // this selects the first item by default if the selection is out of bounds
407 m_selection_hack = 0;
408 }
6de97a3b 409}
c801d85f 410
f40fdaa3 411void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 412{
fd0eed64 413 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 414
f40fdaa3
VS
415 gtk_widget_modify_style( m_widget, style );
416 gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style );
29006414 417
fd0eed64
RR
418 GList *child = menu_shell->children;
419 while (child)
420 {
f40fdaa3 421 gtk_widget_modify_style( GTK_WIDGET( child->data ), style );
29006414 422
fd0eed64 423 GtkBin *bin = GTK_BIN( child->data );
d3b9f782 424 GtkWidget *label = NULL;
9e691f46
VZ
425 if (bin->child)
426 label = bin->child;
427 if (!label)
428 label = BUTTON_CHILD(m_widget);
29006414 429
f40fdaa3 430 gtk_widget_modify_style( label, style );
29006414 431
fd0eed64
RR
432 child = child->next;
433 }
f96aa4d9
RR
434}
435
aa61d352 436int wxChoice::GtkAddHelper(GtkWidget *menu, unsigned int pos, const wxString& item)
e01c8145 437{
8228b893 438 wxCHECK_MSG(pos<=m_clientList.GetCount(), -1, wxT("invalid index"));
243dbf1a 439
fab591c5 440 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
e01c8145
VZ
441
442 size_t index;
443 if ( m_strings )
444 {
445 // sorted control, need to insert at the correct index
446 index = m_strings->Add(item);
447
448 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
449
450 if ( index )
451 {
452 m_clientList.Insert( m_clientList.Item(index - 1),
d3b9f782 453 NULL );
e01c8145
VZ
454 }
455 else
456 {
d3b9f782 457 m_clientList.Insert( NULL );
e01c8145
VZ
458 }
459 }
460 else
461 {
243dbf1a
VZ
462 // don't call wxChoice::GetCount() from here because it doesn't work
463 // if we're called from ctor (and GtkMenuShell is still NULL)
464
e01c8145 465 // normal control, just append
035b6a79 466 if (pos == m_clientList.GetCount())
243dbf1a 467 {
0a164d4c 468 gtk_menu_append( GTK_MENU(menu), menu_item );
d3b9f782 469 m_clientList.Append( NULL );
0a164d4c 470 index = m_clientList.GetCount() - 1;
243dbf1a
VZ
471 }
472 else
473 {
474 gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
d3b9f782 475 m_clientList.Insert( pos, NULL );
243dbf1a
VZ
476 index = pos;
477 }
e01c8145
VZ
478 }
479
480 if (GTK_WIDGET_REALIZED(m_widget))
481 {
482 gtk_widget_realize( menu_item );
483 gtk_widget_realize( GTK_BIN(menu_item)->child );
484
f40fdaa3 485 ApplyWidgetStyle();
e01c8145
VZ
486 }
487
9ddf4854
RR
488 // The best size of a wxChoice should probably
489 // be changed everytime the control has been
490 // changed, but at least after adding an item
491 // it has to change. Adapted from Matt Ownby.
492 InvalidateBestSize();
0a164d4c 493
58b907f6 494 gtk_signal_connect_after( GTK_OBJECT( menu_item ), "activate",
e01c8145
VZ
495 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
496
497 gtk_widget_show( menu_item );
498
499 // return the index of the item in the control
500 return index;
501}
502
f68586e5
VZ
503wxSize wxChoice::DoGetBestSize() const
504{
db434467 505 wxSize ret( wxControl::DoGetBestSize() );
29f54b9b
VZ
506
507 // we know better our horizontal extent: it depends on the longest string
508 // we have
509 ret.x = 0;
510 if ( m_widget )
511 {
60d85ccb 512 int width;
aa61d352
VZ
513 unsigned int count = GetCount();
514 for ( unsigned int n = 0; n < count; n++ )
29f54b9b 515 {
aa61d352 516 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
29f54b9b
VZ
517 if ( width > ret.x )
518 ret.x = width;
519 }
520
df336b7f
VZ
521 // add extra for the choice "=" button
522
523 // VZ: I don't know how to get the right value, it seems to be in
524 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
525 // to it - maybe we can use gtk_option_menu_size_request() for this
526 // somehow?
527 //
528 // This default value works only for the default GTK+ theme (i.e.
529 // no theme at all) (FIXME)
530 static const int widthChoiceIndicator = 35;
531 ret.x += widthChoiceIndicator;
29f54b9b
VZ
532 }
533
534 // but not less than the minimal width
535 if ( ret.x < 80 )
536 ret.x = 80;
537
ebbb22bd
RR
538 // If this request_size is called with no entries then
539 // the returned height is wrong. Give it a reasonable
540 // default value.
541 if (ret.y <= 18)
542 ret.y = 8 + GetCharHeight();
9e691f46 543
9f884528 544 CacheBestSize(ret);
db434467 545 return ret;
f68586e5
VZ
546}
547
2b904684
RR
548bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
549{
2b904684 550 return (window == m_widget->window);
2b904684
RR
551}
552
9d522606
RD
553// static
554wxVisualAttributes
555wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
556{
557 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new);
558}
559
2b904684 560
df336b7f 561#endif // wxUSE_CHOICE