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