]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/choice.cpp
removed called to deprecated wxPixelData::UseAlpha() which does nothing now
[wxWidgets.git] / src / gtk / choice.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/choice.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#include "wx/wxprec.h"
11
12#if wxUSE_CHOICE
13
14#include "wx/choice.h"
15
16#ifndef WX_PRECOMP
17 #include "wx/arrstr.h"
18#endif
19
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
28#include "wx/gtk/private.h"
29
30//-----------------------------------------------------------------------------
31// data
32//-----------------------------------------------------------------------------
33
34extern bool g_blockEventsOnDrag;
35
36//-----------------------------------------------------------------------------
37// "activate"
38//-----------------------------------------------------------------------------
39
40extern "C" {
41static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
42{
43 if (!choice->m_hasVMT) return;
44
45 if (g_blockEventsOnDrag) return;
46
47 int selection = wxNOT_FOUND;
48
49 selection = gtk_option_menu_get_history( GTK_OPTION_MENU(choice->GetHandle()) );
50
51 choice->m_selection_hack = selection;
52
53 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
54 int n = choice->GetSelection();
55
56 event.SetInt( n );
57 event.SetString( choice->GetStringSelection() );
58 event.SetEventObject(choice);
59
60 if ( choice->HasClientObjectData() )
61 event.SetClientObject( choice->GetClientObject(n) );
62 else if ( choice->HasClientUntypedData() )
63 event.SetClientData( choice->GetClientData(n) );
64
65 choice->GetEventHandler()->ProcessEvent(event);
66}
67}
68
69//-----------------------------------------------------------------------------
70// wxChoice
71//-----------------------------------------------------------------------------
72
73IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
74
75wxChoice::wxChoice()
76{
77 m_strings = (wxSortedArrayString *)NULL;
78}
79
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
92bool wxChoice::Create( wxWindow *parent, wxWindowID id,
93 const wxPoint &pos, const wxSize &size,
94 int n, const wxString choices[],
95 long style, const wxValidator& validator, const wxString &name )
96{
97 if (!PreCreation( parent, pos, size ) ||
98 !CreateBase( parent, id, pos, size, style, validator, name ))
99 {
100 wxFAIL_MSG( wxT("wxChoice creation failed") );
101 return false;
102 }
103
104 m_widget = gtk_option_menu_new();
105
106 if ( style & wxCB_SORT )
107 {
108 // if our m_strings != NULL, DoAppend() will check for it and insert
109 // items in the correct order
110 m_strings = new wxSortedArrayString;
111 }
112
113 // begin with no selection
114 m_selection_hack = wxNOT_FOUND;
115
116 GtkWidget *menu = gtk_menu_new();
117
118 for (unsigned int i = 0; i < (unsigned int)n; i++)
119 {
120 GtkAddHelper(menu, i, choices[i]);
121 }
122
123 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
124
125 m_parent->DoAddChild( this );
126
127 PostCreation(size);
128 SetInitialSize(size); // need this too because this is a wxControlWithItems
129
130 return true;
131}
132
133wxChoice::~wxChoice()
134{
135 Clear();
136
137 delete m_strings;
138}
139
140int wxChoice::DoAppend( const wxString &item )
141{
142 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
143
144 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
145
146 return GtkAddHelper(menu, GetCount(), item);
147}
148
149int wxChoice::DoInsert(const wxString &item, unsigned int pos)
150{
151 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
152 wxCHECK_MSG( IsValidInsert(pos), -1, wxT("invalid index"));
153
154 if (pos == GetCount())
155 return DoAppend(item);
156
157 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
158
159 // if the item to insert is at or before the selection, and the selection is valid
160 if (((int)pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
161 {
162 // move the selection forward one
163 m_selection_hack++;
164 }
165
166 return GtkAddHelper(menu, pos, item);
167}
168
169void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
170{
171 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
172
173 wxList::compatibility_iterator node = m_clientList.Item( n );
174 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
175
176 node->SetData( (wxObject*) clientData );
177}
178
179void* wxChoice::DoGetItemClientData(unsigned int n) const
180{
181 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
182
183 wxList::compatibility_iterator node = m_clientList.Item( n );
184 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
185
186 return node->GetData();
187}
188
189void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
190{
191 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
192
193 wxList::compatibility_iterator node = m_clientList.Item( n );
194 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
195
196 // wxItemContainer already deletes data for us
197
198 node->SetData( (wxObject*) clientData );
199}
200
201wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
202{
203 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
204
205 wxList::compatibility_iterator node = m_clientList.Item( n );
206 wxCHECK_MSG( node, (wxClientData *)NULL,
207 wxT("invalid index in wxChoice::DoGetItemClientObject") );
208
209 return (wxClientData*) node->GetData();
210}
211
212void wxChoice::Clear()
213{
214 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
215
216 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
217 GtkWidget *menu = gtk_menu_new();
218 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
219
220 if ( HasClientObjectData() )
221 {
222 // destroy the data (due to Robert's idea of using wxList<wxObject>
223 // and not wxList<wxClientData> we can't just say
224 // m_clientList.DeleteContents(true) - this would crash!
225 wxList::compatibility_iterator node = m_clientList.GetFirst();
226 while ( node )
227 {
228 delete (wxClientData *)node->GetData();
229 node = node->GetNext();
230 }
231 }
232 m_clientList.Clear();
233
234 if ( m_strings )
235 m_strings->Clear();
236
237 // begin with no selection
238 m_selection_hack = wxNOT_FOUND;
239}
240
241void wxChoice::Delete(unsigned int n)
242{
243 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
244 wxCHECK_RET( IsValid(n), _T("invalid index in wxChoice::Delete") );
245
246 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
247 // in 2.0), hence this dumb implementation -- still better than nothing
248 unsigned int i;
249 const unsigned int count = GetCount();
250
251 // if the item to delete is before the selection, and the selection is valid
252 if (((int)n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
253 {
254 // move the selection back one
255 m_selection_hack--;
256 }
257 else if ((int)n == m_selection_hack)
258 {
259 // invalidate the selection
260 m_selection_hack = wxNOT_FOUND;
261 }
262
263 const bool hasClientData = m_clientDataItemsType != wxClientData_None;
264 const bool hasObjectData = m_clientDataItemsType == wxClientData_Object;
265
266 wxList::compatibility_iterator node = m_clientList.GetFirst();
267
268 wxArrayString items;
269 wxArrayPtrVoid itemsData;
270 items.Alloc(count);
271 for ( i = 0; i < count; i++ )
272 {
273 if ( i != n )
274 {
275 items.Add(GetString(i));
276 if ( hasClientData )
277 {
278 // also save the client data
279 itemsData.Add(node->GetData());
280 }
281 }
282 else // need to delete the client object too
283 {
284 if ( hasObjectData )
285 {
286 delete (wxClientData *)node->GetData();
287 }
288 }
289
290 if ( hasClientData )
291 {
292 node = node->GetNext();
293 }
294 }
295
296 if ( hasObjectData )
297 {
298 // prevent Clear() from destroying all client data
299 m_clientDataItemsType = wxClientData_None;
300 }
301
302 Clear();
303
304 for ( i = 0; i < count - 1; i++ )
305 {
306 Append(items[i]);
307
308 if ( hasObjectData )
309 SetClientObject(i, (wxClientData *)itemsData[i]);
310 else if ( hasClientData )
311 SetClientData(i, itemsData[i]);
312 }
313}
314
315int wxChoice::FindString( const wxString &string, bool bCase ) const
316{
317 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
318
319 // If you read this code once and you think you understand
320 // it, then you are very wrong. Robert Roebling.
321
322 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
323 int count = 0;
324 GList *child = menu_shell->children;
325 while (child)
326 {
327 GtkBin *bin = GTK_BIN( child->data );
328 GtkLabel *label = (GtkLabel *) NULL;
329 if (bin->child)
330 label = GTK_LABEL(bin->child);
331 if (!label)
332 label = GTK_LABEL(GTK_BIN(m_widget)->child);
333
334 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
335
336 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
337 if (string.IsSameAs( tmp, bCase ))
338 return count;
339
340 child = child->next;
341 count++;
342 }
343
344 return wxNOT_FOUND;
345}
346
347int wxChoice::GetSelection() const
348{
349 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
350
351 return m_selection_hack;
352
353}
354
355void wxChoice::SetString(unsigned int n, const wxString& str)
356{
357 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
358
359 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
360 unsigned int count = 0;
361 GList *child = menu_shell->children;
362 while (child)
363 {
364 GtkBin *bin = GTK_BIN( child->data );
365 if (count == n)
366 {
367 GtkLabel *label = (GtkLabel *) NULL;
368 if (bin->child)
369 label = GTK_LABEL(bin->child);
370 if (!label)
371 label = GTK_LABEL(GTK_BIN(m_widget)->child);
372
373 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
374
375 gtk_label_set_text( label, wxGTK_CONV( str ) );
376
377 InvalidateBestSize();
378
379 return;
380 }
381 child = child->next;
382 count++;
383 }
384}
385
386wxString wxChoice::GetString(unsigned int n) const
387{
388 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid choice") );
389
390 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
391 unsigned int count = 0;
392 GList *child = menu_shell->children;
393 while (child)
394 {
395 GtkBin *bin = GTK_BIN( child->data );
396 if (count == n)
397 {
398 GtkLabel *label = (GtkLabel *) NULL;
399 if (bin->child)
400 label = GTK_LABEL(bin->child);
401 if (!label)
402 label = GTK_LABEL(GTK_BIN(m_widget)->child);
403
404 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
405
406 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
407 }
408 child = child->next;
409 count++;
410 }
411
412 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
413
414 return wxEmptyString;
415}
416
417unsigned int wxChoice::GetCount() const
418{
419 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
420
421 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
422 unsigned int count = 0;
423 GList *child = menu_shell->children;
424 while (child)
425 {
426 count++;
427 child = child->next;
428 }
429 return count;
430}
431
432void wxChoice::SetSelection( int n )
433{
434 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
435
436 int tmp = n;
437 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
438
439 // set the local selection variable manually
440 if ((n >= 0) && ((unsigned int)n < GetCount()))
441 {
442 // a valid selection has been made
443 m_selection_hack = n;
444 }
445 else if ((n == wxNOT_FOUND) || (GetCount() == 0))
446 {
447 // invalidates the selection if there are no items
448 // or if it is specifically set to wxNOT_FOUND
449 m_selection_hack = wxNOT_FOUND;
450 }
451 else
452 {
453 // this selects the first item by default if the selection is out of bounds
454 m_selection_hack = 0;
455 }
456}
457
458void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
459{
460 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
461
462 gtk_widget_modify_style( m_widget, style );
463 gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style );
464
465 GList *child = menu_shell->children;
466 while (child)
467 {
468 gtk_widget_modify_style( GTK_WIDGET( child->data ), style );
469
470 GtkBin *bin = GTK_BIN( child->data );
471 GtkWidget *label = (GtkWidget *) NULL;
472 if (bin->child)
473 label = bin->child;
474 if (!label)
475 label = GTK_BIN(m_widget)->child;
476
477 gtk_widget_modify_style( label, style );
478
479 child = child->next;
480 }
481}
482
483int wxChoice::GtkAddHelper(GtkWidget *menu, unsigned int pos, const wxString& item)
484{
485 wxCHECK_MSG(pos<=m_clientList.GetCount(), -1, wxT("invalid index"));
486
487 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
488
489 unsigned int index;
490 if ( m_strings )
491 {
492 // sorted control, need to insert at the correct index
493 index = m_strings->Add(item);
494
495 gtk_menu_shell_insert( GTK_MENU_SHELL(menu), menu_item, index );
496
497 if ( index )
498 {
499 m_clientList.Insert( m_clientList.Item(index - 1),
500 (wxObject*) NULL );
501 }
502 else
503 {
504 m_clientList.Insert( (wxObject*) NULL );
505 }
506 }
507 else
508 {
509 // don't call wxChoice::GetCount() from here because it doesn't work
510 // if we're called from ctor (and GtkMenuShell is still NULL)
511
512 // normal control, just append
513 if (pos == m_clientList.GetCount())
514 {
515 gtk_menu_shell_append( GTK_MENU_SHELL(menu), menu_item );
516 m_clientList.Append( (wxObject*) NULL );
517 index = m_clientList.GetCount() - 1;
518 }
519 else
520 {
521 gtk_menu_shell_insert( GTK_MENU_SHELL(menu), menu_item, pos );
522 m_clientList.Insert( pos, (wxObject*) NULL );
523 index = pos;
524 }
525 }
526
527 if (GTK_WIDGET_REALIZED(m_widget))
528 {
529 gtk_widget_realize( menu_item );
530 gtk_widget_realize( GTK_BIN(menu_item)->child );
531
532 ApplyWidgetStyle();
533 }
534
535 // The best size of a wxChoice should probably
536 // be changed everytime the control has been
537 // changed, but at least after adding an item
538 // it has to change. Adapted from Matt Ownby.
539 InvalidateBestSize();
540
541 g_signal_connect_after (menu_item, "activate",
542 G_CALLBACK (gtk_choice_clicked_callback),
543 this);
544
545 gtk_widget_show( menu_item );
546
547 // return the index of the item in the control
548 return index;
549}
550
551wxSize wxChoice::DoGetBestSize() const
552{
553 wxSize ret( wxControl::DoGetBestSize() );
554
555 // we know better our horizontal extent: it depends on the longest string
556 // we have
557 ret.x = 0;
558 if ( m_widget )
559 {
560 int width;
561 unsigned int count = GetCount();
562 for ( unsigned int n = 0; n < count; n++ )
563 {
564 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
565 if ( width > ret.x )
566 ret.x = width;
567 }
568
569 // add extra for the choice "=" button
570
571 // VZ: I don't know how to get the right value, it seems to be in
572 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
573 // to it - maybe we can use gtk_option_menu_size_request() for this
574 // somehow?
575 //
576 // This default value works only for the default GTK+ theme (i.e.
577 // no theme at all) (FIXME)
578 static const int widthChoiceIndicator = 35;
579 ret.x += widthChoiceIndicator;
580 }
581
582 // but not less than the minimal width
583 if ( ret.x < 80 )
584 ret.x = 80;
585
586 // If this request_size is called with no entries then
587 // the returned height is wrong. Give it a reasonable
588 // default value.
589 if (ret.y <= 18)
590 ret.y = 8 + GetCharHeight();
591
592 CacheBestSize(ret);
593 return ret;
594}
595
596GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
597{
598 return GTK_BUTTON(m_widget)->event_window;
599}
600
601// static
602wxVisualAttributes
603wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
604{
605 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new);
606}
607
608
609#endif // wxUSE_CHOICE