]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
revert updating GTK size hints when window decorations change, it messes up min size...
[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
0a164d4c 130 return true;
6de97a3b 131}
29006414 132
fd0eed64
RR
133wxChoice::~wxChoice()
134{
f5e27805 135 Clear();
e01c8145
VZ
136
137 delete m_strings;
fd0eed64
RR
138}
139
a236aa20
VZ
140int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
141 unsigned int pos,
142 void **clientData, wxClientDataType type)
fd0eed64 143{
2ee3ee1b 144 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 145
a236aa20 146 const unsigned int count = items.GetCount();
243dbf1a 147
a236aa20 148 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
243dbf1a 149
a236aa20
VZ
150 for ( unsigned int i = 0; i < count; ++i, ++pos )
151 {
152 int n = GtkAddHelper(menu, pos, items[i]);
153 if ( n == wxNOT_FOUND )
154 return n;
243dbf1a 155
a236aa20
VZ
156 AssignNewItemClientData(n, clientData, i, type);
157 }
243dbf1a 158
16edee16 159 // if the item to insert is at or before the selection, and the selection is valid
aa61d352 160 if (((int)pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
16edee16 161 {
a236aa20
VZ
162 // move the selection forward
163 m_selection_hack += count;
16edee16
RR
164 }
165
261a9107
RR
166 // We must set the selection so that it can be read back even if
167 // the user has not modified it since GTK+ will then select the
168 // first item so well return 0.
169 if ((count > 0) && (m_selection_hack==wxNOT_FOUND))
170 m_selection_hack = 0;
171
a236aa20 172 return pos - 1;
fd0eed64 173}
f96aa4d9 174
aa61d352 175void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 176{
a236aa20 177 m_clientData[n] = clientData;
fd0eed64
RR
178}
179
aa61d352 180void* wxChoice::DoGetItemClientData(unsigned int n) const
fd0eed64 181{
a236aa20 182 return m_clientData[n];
fd0eed64 183}
29006414 184
a236aa20 185void wxChoice::DoClear()
c801d85f 186{
223d09f6 187 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 188
fd0eed64
RR
189 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
190 GtkWidget *menu = gtk_menu_new();
191 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 192
a236aa20 193 m_clientData.Clear();
2ee3ee1b
VZ
194
195 if ( m_strings )
196 m_strings->Clear();
16edee16
RR
197
198 // begin with no selection
199 m_selection_hack = wxNOT_FOUND;
6de97a3b 200}
c801d85f 201
a236aa20 202void wxChoice::DoDeleteOneItem(unsigned int n)
2f6407b9 203{
645420d8 204 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
8228b893 205 wxCHECK_RET( IsValid(n), _T("invalid index in wxChoice::Delete") );
645420d8 206
16edee16 207 // if the item to delete is before the selection, and the selection is valid
aa61d352 208 if (((int)n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
16edee16
RR
209 {
210 // move the selection back one
211 m_selection_hack--;
212 }
aa61d352 213 else if ((int)n == m_selection_hack)
16edee16
RR
214 {
215 // invalidate the selection
216 m_selection_hack = wxNOT_FOUND;
217 }
218
a236aa20
VZ
219 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
220 // in 2.0), hence this dumb implementation -- still better than nothing
221 const unsigned int count = GetCount();
e2380ce1 222
645420d8 223 wxArrayString items;
e2380ce1 224 wxArrayPtrVoid itemsData;
ba8c878f
VZ
225 items.Alloc(count - 1);
226 itemsData.Alloc(count - 1);
a236aa20 227 for ( unsigned i = 0; i < count; i++ )
645420d8 228 {
aa61d352 229 if ( i != n )
e2380ce1 230 {
645420d8 231 items.Add(GetString(i));
a236aa20 232 itemsData.Add(m_clientData[i]);
e2380ce1
VZ
233 }
234 }
235
a236aa20 236 wxChoice::DoClear();
e2380ce1 237
ba8c878f
VZ
238 if ( count > 1 )
239 {
240 void ** const data = &itemsData[0];
241 if ( HasClientObjectData() )
242 Append(items, wx_reinterpret_cast(wxClientData **, data));
243 else
244 Append(items, data);
245 }
246 //else: the control is now empty, nothing to append
2f6407b9
RR
247}
248
11e62fe6 249int wxChoice::FindString( const wxString &string, bool bCase ) const
c801d85f 250{
11e62fe6 251 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
fd0eed64
RR
252
253 // If you read this code once and you think you understand
254 // it, then you are very wrong. Robert Roebling.
29006414 255
fd0eed64
RR
256 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
257 int count = 0;
258 GList *child = menu_shell->children;
259 while (child)
260 {
261 GtkBin *bin = GTK_BIN( child->data );
262 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
263 if (bin->child)
264 label = GTK_LABEL(bin->child);
265 if (!label)
afa7bd1e 266 label = GTK_LABEL(GTK_BIN(m_widget)->child);
29006414 267
223d09f6 268 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
e2380ce1 269
2e1d7104 270 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
11e62fe6 271 if (string.IsSameAs( tmp, bCase ))
9e691f46 272 return count;
29006414 273
9e691f46
VZ
274 child = child->next;
275 count++;
fd0eed64 276 }
29006414 277
0a164d4c 278 return wxNOT_FOUND;
6de97a3b 279}
c801d85f 280
9abe166a 281int wxChoice::GetSelection() const
c801d85f 282{
11e62fe6 283 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
e2380ce1 284
16edee16
RR
285 return m_selection_hack;
286
6de97a3b 287}
c801d85f 288
aa61d352 289void wxChoice::SetString(unsigned int n, const wxString& str)
6c8a980f
VZ
290{
291 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
292
34b5e560 293 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
aa61d352 294 unsigned int count = 0;
34b5e560
RR
295 GList *child = menu_shell->children;
296 while (child)
297 {
298 GtkBin *bin = GTK_BIN( child->data );
299 if (count == n)
300 {
301 GtkLabel *label = (GtkLabel *) NULL;
302 if (bin->child)
303 label = GTK_LABEL(bin->child);
304 if (!label)
afa7bd1e 305 label = GTK_LABEL(GTK_BIN(m_widget)->child);
34b5e560
RR
306
307 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
308
0a164d4c 309 gtk_label_set_text( label, wxGTK_CONV( str ) );
b3f62dd5
RR
310
311 InvalidateBestSize();
0a164d4c 312
34b5e560
RR
313 return;
314 }
315 child = child->next;
316 count++;
317 }
6c8a980f
VZ
318}
319
aa61d352 320wxString wxChoice::GetString(unsigned int n) const
c801d85f 321{
0a164d4c 322 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid choice") );
fd0eed64
RR
323
324 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
aa61d352 325 unsigned int count = 0;
fd0eed64
RR
326 GList *child = menu_shell->children;
327 while (child)
c801d85f 328 {
fd0eed64
RR
329 GtkBin *bin = GTK_BIN( child->data );
330 if (count == n)
331 {
332 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
333 if (bin->child)
334 label = GTK_LABEL(bin->child);
335 if (!label)
afa7bd1e 336 label = GTK_LABEL(GTK_BIN(m_widget)->child);
29006414 337
223d09f6 338 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 339
2e1d7104 340 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
fd0eed64
RR
341 }
342 child = child->next;
343 count++;
6de97a3b 344 }
29006414 345
223d09f6 346 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 347
0a164d4c 348 return wxEmptyString;
6de97a3b 349}
c801d85f 350
aa61d352 351unsigned int wxChoice::GetCount() const
c801d85f 352{
223d09f6 353 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
354
355 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
aa61d352 356 unsigned int count = 0;
fd0eed64
RR
357 GList *child = menu_shell->children;
358 while (child)
359 {
360 count++;
361 child = child->next;
362 }
363 return count;
6de97a3b 364}
c801d85f 365
debe6624 366void wxChoice::SetSelection( int n )
c801d85f 367{
223d09f6 368 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 369
fd0eed64
RR
370 int tmp = n;
371 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
16edee16
RR
372
373 // set the local selection variable manually
aa61d352 374 if ((n >= 0) && ((unsigned int)n < GetCount()))
16edee16
RR
375 {
376 // a valid selection has been made
377 m_selection_hack = n;
378 }
379 else if ((n == wxNOT_FOUND) || (GetCount() == 0))
380 {
381 // invalidates the selection if there are no items
382 // or if it is specifically set to wxNOT_FOUND
383 m_selection_hack = wxNOT_FOUND;
384 }
385 else
386 {
387 // this selects the first item by default if the selection is out of bounds
388 m_selection_hack = 0;
389 }
6de97a3b 390}
c801d85f 391
f40fdaa3 392void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 393{
fd0eed64 394 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 395
f40fdaa3
VS
396 gtk_widget_modify_style( m_widget, style );
397 gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style );
29006414 398
fd0eed64
RR
399 GList *child = menu_shell->children;
400 while (child)
401 {
f40fdaa3 402 gtk_widget_modify_style( GTK_WIDGET( child->data ), style );
29006414 403
fd0eed64
RR
404 GtkBin *bin = GTK_BIN( child->data );
405 GtkWidget *label = (GtkWidget *) NULL;
9e691f46
VZ
406 if (bin->child)
407 label = bin->child;
408 if (!label)
afa7bd1e 409 label = GTK_BIN(m_widget)->child;
29006414 410
f40fdaa3 411 gtk_widget_modify_style( label, style );
29006414 412
fd0eed64
RR
413 child = child->next;
414 }
f96aa4d9
RR
415}
416
aa61d352 417int wxChoice::GtkAddHelper(GtkWidget *menu, unsigned int pos, const wxString& item)
e01c8145 418{
a236aa20 419 wxCHECK_MSG(pos<=m_clientData.GetCount(), -1, wxT("invalid index"));
243dbf1a 420
fab591c5 421 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
e01c8145 422
e01c8145
VZ
423 if ( m_strings )
424 {
425 // sorted control, need to insert at the correct index
a236aa20 426 pos = m_strings->Add(item);
e01c8145 427 }
a236aa20
VZ
428
429 // don't call wxChoice::GetCount() from here because it doesn't work
430 // if we're called from ctor (and GtkMenuShell is still NULL)
431 if (pos == m_clientData.GetCount())
432 gtk_menu_shell_append( GTK_MENU_SHELL(menu), menu_item );
e01c8145 433 else
a236aa20 434 gtk_menu_shell_insert( GTK_MENU_SHELL(menu), menu_item, pos );
243dbf1a 435
a236aa20 436 m_clientData.Insert( NULL, pos );
e01c8145
VZ
437
438 if (GTK_WIDGET_REALIZED(m_widget))
439 {
440 gtk_widget_realize( menu_item );
441 gtk_widget_realize( GTK_BIN(menu_item)->child );
442
f40fdaa3 443 ApplyWidgetStyle();
e01c8145
VZ
444 }
445
9ddf4854
RR
446 // The best size of a wxChoice should probably
447 // be changed everytime the control has been
448 // changed, but at least after adding an item
449 // it has to change. Adapted from Matt Ownby.
450 InvalidateBestSize();
0a164d4c 451
9fa72bd2
MR
452 g_signal_connect_after (menu_item, "activate",
453 G_CALLBACK (gtk_choice_clicked_callback),
454 this);
e01c8145
VZ
455
456 gtk_widget_show( menu_item );
457
458 // return the index of the item in the control
a236aa20 459 return pos;
e01c8145
VZ
460}
461
f68586e5
VZ
462wxSize wxChoice::DoGetBestSize() const
463{
db434467 464 wxSize ret( wxControl::DoGetBestSize() );
29f54b9b
VZ
465
466 // we know better our horizontal extent: it depends on the longest string
467 // we have
468 ret.x = 0;
469 if ( m_widget )
470 {
60d85ccb 471 int width;
aa61d352
VZ
472 unsigned int count = GetCount();
473 for ( unsigned int n = 0; n < count; n++ )
29f54b9b 474 {
2b1ff57f 475 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
29f54b9b
VZ
476 if ( width > ret.x )
477 ret.x = width;
478 }
479
df336b7f
VZ
480 // add extra for the choice "=" button
481
482 // VZ: I don't know how to get the right value, it seems to be in
483 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
484 // to it - maybe we can use gtk_option_menu_size_request() for this
485 // somehow?
486 //
487 // This default value works only for the default GTK+ theme (i.e.
488 // no theme at all) (FIXME)
489 static const int widthChoiceIndicator = 35;
490 ret.x += widthChoiceIndicator;
29f54b9b
VZ
491 }
492
493 // but not less than the minimal width
494 if ( ret.x < 80 )
495 ret.x = 80;
496
ebbb22bd
RR
497 // If this request_size is called with no entries then
498 // the returned height is wrong. Give it a reasonable
499 // default value.
500 if (ret.y <= 18)
501 ret.y = 8 + GetCharHeight();
9e691f46 502
9f884528 503 CacheBestSize(ret);
db434467 504 return ret;
f68586e5
VZ
505}
506
ef5c70f9 507GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2b904684 508{
2b904684 509 return GTK_BUTTON(m_widget)->event_window;
2b904684
RR
510}
511
9d522606
RD
512// static
513wxVisualAttributes
514wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
515{
516 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new);
517}
518
a73554d4 519
df336b7f 520#endif // wxUSE_CHOICE