1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      Robert Roebling 
   6 // Copyright:   (c) 1998 Robert Roebling 
   7 // Licence:     wxWindows licence 
   8 ///////////////////////////////////////////////////////////////////////////// 
  11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) 
  12 #pragma implementation "choice.h" 
  19 #include "wx/choice.h" 
  20 #include "wx/arrstr.h" 
  22 #include "wx/gtk/private.h" 
  24 //----------------------------------------------------------------------------- 
  26 //----------------------------------------------------------------------------- 
  28 extern void wxapp_install_idle_handler(); 
  31 //----------------------------------------------------------------------------- 
  33 //----------------------------------------------------------------------------- 
  35 extern bool   g_blockEventsOnDrag
; 
  37 //----------------------------------------------------------------------------- 
  39 //----------------------------------------------------------------------------- 
  41 static void gtk_choice_clicked_callback( GtkWidget 
*WXUNUSED(widget
), wxChoice 
*choice 
) 
  44       wxapp_install_idle_handler(); 
  46     if (!choice
->m_hasVMT
) return; 
  48     if (g_blockEventsOnDrag
) return; 
  50     wxCommandEvent 
event(wxEVT_COMMAND_CHOICE_SELECTED
, choice
->GetId() ); 
  51     int n 
= choice
->GetSelection(); 
  54     event
.SetString( choice
->GetStringSelection() ); 
  55     event
.SetEventObject(choice
); 
  57     if ( choice
->HasClientObjectData() ) 
  58         event
.SetClientObject( choice
->GetClientObject(n
) ); 
  59     else if ( choice
->HasClientUntypedData() ) 
  60         event
.SetClientData( choice
->GetClientData(n
) ); 
  62     choice
->GetEventHandler()->ProcessEvent(event
); 
  65 //----------------------------------------------------------------------------- 
  67 //----------------------------------------------------------------------------- 
  69 IMPLEMENT_DYNAMIC_CLASS(wxChoice
,wxControl
) 
  73     m_strings 
= (wxSortedArrayString 
*)NULL
; 
  76 bool wxChoice::Create( wxWindow 
*parent
, wxWindowID id
, 
  77                        const wxPoint 
&pos
, const wxSize 
&size
, 
  78                        const wxArrayString
& choices
, 
  79                        long style
, const wxValidator
& validator
, 
  80                        const wxString 
&name 
) 
  82     wxCArrayString 
chs(choices
); 
  84     return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(), 
  85                    style
, validator
, name 
); 
  88 bool wxChoice::Create( wxWindow 
*parent
, wxWindowID id
, 
  89                        const wxPoint 
&pos
, const wxSize 
&size
, 
  90                        int n
, const wxString choices
[], 
  91                        long style
, const wxValidator
& validator
, const wxString 
&name 
) 
  94 #if (GTK_MINOR_VERSION > 0) 
  95     m_acceptsFocus 
= TRUE
; 
  98     if (!PreCreation( parent
, pos
, size 
) || 
  99         !CreateBase( parent
, id
, pos
, size
, style
, validator
, name 
)) 
 101         wxFAIL_MSG( wxT("wxChoice creation failed") ); 
 105     m_widget 
= gtk_option_menu_new(); 
 107     if ( style 
& wxCB_SORT 
) 
 109         // if our m_strings != NULL, DoAppend() will check for it and insert 
 110         // items in the correct order 
 111         m_strings 
= new wxSortedArrayString
; 
 114     GtkWidget 
*menu 
= gtk_menu_new(); 
 116     for (int i 
= 0; i 
< n
; i
++) 
 118         GtkAddHelper(menu
, i
, choices
[i
]); 
 121     gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu 
); 
 123     m_parent
->DoAddChild( this ); 
 135 wxChoice::~wxChoice() 
 142 int wxChoice::DoAppend( const wxString 
&item 
) 
 144     wxCHECK_MSG( m_widget 
!= NULL
, -1, wxT("invalid choice control") ); 
 146     GtkWidget 
*menu 
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ); 
 148     return GtkAddHelper(menu
, GetCount(), item
); 
 151 int wxChoice::DoInsert( const wxString 
&item
, int pos 
) 
 153     wxCHECK_MSG( m_widget 
!= NULL
, -1, wxT("invalid choice control") ); 
 154     wxCHECK_MSG( (pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index")); 
 156     if (pos 
== GetCount()) 
 157         return DoAppend(item
); 
 159     GtkWidget 
*menu 
= gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ); 
 161     return GtkAddHelper(menu
, pos
, item
); 
 164 void wxChoice::DoSetItemClientData( int n
, void* clientData 
) 
 166     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid choice control") ); 
 168     wxList::compatibility_iterator node 
= m_clientList
.Item( n 
); 
 169     wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") ); 
 171     node
->SetData( (wxObject
*) clientData 
); 
 174 void* wxChoice::DoGetItemClientData( int n 
) const 
 176     wxCHECK_MSG( m_widget 
!= NULL
, NULL
, wxT("invalid choice control") ); 
 178     wxList::compatibility_iterator node 
= m_clientList
.Item( n 
); 
 179     wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") ); 
 181     return node
->GetData(); 
 184 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData 
) 
 186     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid choice control") ); 
 188     wxList::compatibility_iterator node 
= m_clientList
.Item( n 
); 
 189     wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") ); 
 191     // wxItemContainer already deletes data for us 
 193     node
->SetData( (wxObject
*) clientData 
); 
 196 wxClientData
* wxChoice::DoGetItemClientObject( int n 
) const 
 198     wxCHECK_MSG( m_widget 
!= NULL
, (wxClientData
*) NULL
, wxT("invalid choice control") ); 
 200     wxList::compatibility_iterator node 
= m_clientList
.Item( n 
); 
 201     wxCHECK_MSG( node
, (wxClientData 
*)NULL
, 
 202                  wxT("invalid index in wxChoice::DoGetItemClientObject") ); 
 204     return (wxClientData
*) node
->GetData(); 
 207 void wxChoice::Clear() 
 209     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid choice") ); 
 211     gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget
) ); 
 212     GtkWidget 
*menu 
= gtk_menu_new(); 
 213     gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget
), menu 
); 
 215     if ( HasClientObjectData() ) 
 217         // destroy the data (due to Robert's idea of using wxList<wxObject> 
 218         // and not wxList<wxClientData> we can't just say 
 219         // m_clientList.DeleteContents(TRUE) - this would crash! 
 220         wxList::compatibility_iterator node 
= m_clientList
.GetFirst(); 
 223             delete (wxClientData 
*)node
->GetData(); 
 224             node 
= node
->GetNext(); 
 227     m_clientList
.Clear(); 
 233 void wxChoice::Delete( int n 
) 
 235     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid choice") ); 
 237     // VZ: apparently GTK+ doesn't have a built-in function to do it (not even 
 238     //     in 2.0), hence this dumb implementation -- still better than nothing 
 242     wxCHECK_RET( n 
>= 0 && n 
< count
, _T("invalid index in wxChoice::Delete") ); 
 244     const bool hasClientData 
= m_clientDataItemsType 
!= wxClientData_None
; 
 245     const bool hasObjectData 
= m_clientDataItemsType 
== wxClientData_Object
; 
 247     wxList::compatibility_iterator node 
= m_clientList
.GetFirst(); 
 250     wxArrayPtrVoid itemsData
; 
 252     for ( i 
= 0; i 
< count
; i
++ ) 
 256             items
.Add(GetString(i
)); 
 259                 // also save the client data 
 260                 itemsData
.Add(node
->GetData()); 
 263         else // need to delete the client object too 
 267                 delete (wxClientData 
*)node
->GetData(); 
 273             node 
= node
->GetNext(); 
 279         // prevent Clear() from destroying all client data 
 280         m_clientDataItemsType 
= wxClientData_None
; 
 285     for ( i 
= 0; i 
< count 
- 1; i
++ ) 
 290             SetClientObject(i
, (wxClientData 
*)itemsData
[i
]); 
 291         else if ( hasClientData 
) 
 292             SetClientData(i
, itemsData
[i
]); 
 296 int wxChoice::FindString( const wxString 
&string 
) const 
 298     wxCHECK_MSG( m_widget 
!= NULL
, -1, wxT("invalid choice") ); 
 300     // If you read this code once and you think you understand 
 301     // it, then you are very wrong. Robert Roebling. 
 303     GtkMenuShell 
*menu_shell 
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) ); 
 305     GList 
*child 
= menu_shell
->children
; 
 308         GtkBin 
*bin 
= GTK_BIN( child
->data 
); 
 309         GtkLabel 
*label 
= (GtkLabel 
*) NULL
; 
 311             label 
= GTK_LABEL(bin
->child
); 
 313             label 
= GTK_LABEL( BUTTON_CHILD(m_widget
) ); 
 315         wxASSERT_MSG( label 
!= NULL 
, wxT("wxChoice: invalid label") ); 
 318          wxString 
tmp( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) ); 
 320          wxString 
tmp( label
->label 
); 
 332 int wxChoice::GetSelection() const 
 334     wxCHECK_MSG( m_widget 
!= NULL
, -1, wxT("invalid choice") ); 
 338     return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget
) ); 
 341     GtkMenuShell 
*menu_shell 
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) ); 
 344     GList 
*child 
= menu_shell
->children
; 
 347         GtkBin 
*bin 
= GTK_BIN( child
->data 
); 
 348         if (!bin
->child
) return count
; 
 357 void wxChoice::SetString( int n
, const wxString
& str 
) 
 359     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid choice") ); 
 361     GtkMenuShell 
*menu_shell 
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) ); 
 363     GList 
*child 
= menu_shell
->children
; 
 366         GtkBin 
*bin 
= GTK_BIN( child
->data 
); 
 369             GtkLabel 
*label 
= (GtkLabel 
*) NULL
; 
 371                 label 
= GTK_LABEL(bin
->child
); 
 373                 label 
= GTK_LABEL( BUTTON_CHILD(m_widget
) ); 
 375             wxASSERT_MSG( label 
!= NULL 
, wxT("wxChoice: invalid label") ); 
 377             gtk_label_set_text( label
, wxGTK_CONV( str 
) );  
 386 wxString 
wxChoice::GetString( int n 
) const 
 388     wxCHECK_MSG( m_widget 
!= NULL
, wxT(""), wxT("invalid choice") ); 
 390     GtkMenuShell 
*menu_shell 
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) ); 
 392     GList 
*child 
= menu_shell
->children
; 
 395         GtkBin 
*bin 
= GTK_BIN( child
->data 
); 
 398             GtkLabel 
*label 
= (GtkLabel 
*) NULL
; 
 400                 label 
= GTK_LABEL(bin
->child
); 
 402                 label 
= GTK_LABEL( BUTTON_CHILD(m_widget
) ); 
 404             wxASSERT_MSG( label 
!= NULL 
, wxT("wxChoice: invalid label") ); 
 407             return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label
) ) ); 
 409             return wxString( label
->label 
); 
 416     wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") ); 
 421 int wxChoice::GetCount() const 
 423     wxCHECK_MSG( m_widget 
!= NULL
, 0, wxT("invalid choice") ); 
 425     GtkMenuShell 
*menu_shell 
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) ); 
 427     GList 
*child 
= menu_shell
->children
; 
 436 void wxChoice::SetSelection( int n 
) 
 438     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid choice") ); 
 441     gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget
), (gint
)tmp 
); 
 444 void wxChoice::ApplyWidgetStyle() 
 448     GtkMenuShell 
*menu_shell 
= GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget
) ) ); 
 450     gtk_widget_set_style( m_widget
, m_widgetStyle 
); 
 451     gtk_widget_set_style( GTK_WIDGET( menu_shell 
), m_widgetStyle 
); 
 453     GList 
*child 
= menu_shell
->children
; 
 456         gtk_widget_set_style( GTK_WIDGET( child
->data 
), m_widgetStyle 
); 
 458         GtkBin 
*bin 
= GTK_BIN( child
->data 
); 
 459         GtkWidget 
*label 
= (GtkWidget 
*) NULL
; 
 463             label 
= BUTTON_CHILD(m_widget
); 
 465         gtk_widget_set_style( label
, m_widgetStyle 
); 
 471 int wxChoice::GtkAddHelper(GtkWidget 
*menu
, int pos
, const wxString
& item
) 
 473     wxCHECK_MSG((pos
>=0) && (pos
<=(int)m_clientList
.GetCount()), -1, wxT("invalid index")); 
 475     GtkWidget 
*menu_item 
= gtk_menu_item_new_with_label( wxGTK_CONV( item 
) ); 
 480         // sorted control, need to insert at the correct index 
 481         index 
= m_strings
->Add(item
); 
 483         gtk_menu_insert( GTK_MENU(menu
), menu_item
, index 
); 
 487             m_clientList
.Insert( m_clientList
.Item(index 
- 1), 
 492             m_clientList
.Insert( (wxObject
*) NULL 
); 
 497         // don't call wxChoice::GetCount() from here because it doesn't work 
 498         // if we're called from ctor (and GtkMenuShell is still NULL) 
 500         // normal control, just append 
 501         if (pos 
== (int)m_clientList
.GetCount()) 
 503         gtk_menu_append( GTK_MENU(menu
), menu_item 
); 
 504         m_clientList
.Append( (wxObject
*) NULL 
); 
 505         index 
= m_clientList
.GetCount() - 1; 
 509             gtk_menu_insert( GTK_MENU(menu
), menu_item
, pos 
); 
 510             m_clientList
.Insert( pos
, (wxObject
*) NULL 
); 
 515     if (GTK_WIDGET_REALIZED(m_widget
)) 
 517         gtk_widget_realize( menu_item 
); 
 518         gtk_widget_realize( GTK_BIN(menu_item
)->child 
); 
 520         if (m_widgetStyle
) ApplyWidgetStyle(); 
 523     gtk_signal_connect( GTK_OBJECT( menu_item 
), "activate", 
 524       GTK_SIGNAL_FUNC(gtk_choice_clicked_callback
), (gpointer
*)this ); 
 526     gtk_widget_show( menu_item 
); 
 528     // return the index of the item in the control 
 532 wxSize 
wxChoice::DoGetBestSize() const 
 534     wxSize 
ret( wxControl::DoGetBestSize() ); 
 536     // we know better our horizontal extent: it depends on the longest string 
 542         size_t count 
= GetCount(); 
 543         for ( size_t n 
= 0; n 
< count
; n
++ ) 
 545             GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
, &m_font 
); 
 550         // add extra for the choice "=" button 
 552         // VZ: I don't know how to get the right value, it seems to be in 
 553         //     GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get 
 554         //     to it - maybe we can use gtk_option_menu_size_request() for this 
 557         //     This default value works only for the default GTK+ theme (i.e. 
 558         //     no theme at all) (FIXME) 
 559         static const int widthChoiceIndicator 
= 35; 
 560         ret
.x 
+= widthChoiceIndicator
; 
 563     // but not less than the minimal width 
 567     ret
.y 
= 16 + GetCharHeight(); 
 572 bool wxChoice::IsOwnGtkWindow( GdkWindow 
*window 
) 
 575     return GTK_BUTTON(m_widget
)->event_window
; 
 577     return (window 
== m_widget
->window
); 
 582 #endif // wxUSE_CHOICE