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