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