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