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