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