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