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