- if ( m_hasFocus )
- {
- // don't do anything if we already have focus
- return;
- }
-
- gtk_widget_grab_focus( m_focusWidget );
-}
-
-int wxComboBox::DoAppend( const wxString &item )
-{
- wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- gtk_combo_box_append_text( combobox, wxGTK_CONV( item ) );
- }
- else
-#endif
- {
- DisableEvents();
-
- GtkWidget *list = GTK_COMBO(m_widget)->list;
- GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
-
- gtk_container_add( GTK_CONTAINER(list), list_item );
-
- if (GTK_WIDGET_REALIZED(m_widget))
- {
- gtk_widget_realize( list_item );
- gtk_widget_realize( GTK_BIN(list_item)->child );
- }
-
- // Apply current widget style to the new list_item
- GtkRcStyle *style = CreateWidgetStyle();
- if (style)
- {
- gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
- GtkBin *bin = GTK_BIN( list_item );
- GtkWidget *label = GTK_WIDGET( bin->child );
- gtk_widget_modify_style( label, style );
- gtk_rc_style_unref( style );
- }
-
- gtk_widget_show( list_item );
-
- EnableEvents();
- }
-
- const unsigned int count = GetCount();
-
- if ( m_clientDataList.GetCount() < count )
- m_clientDataList.Append( (wxObject*) NULL );
- if ( m_clientObjectList.GetCount() < count )
- m_clientObjectList.Append( (wxObject*) NULL );
-
- InvalidateBestSize();
-
- return count - 1;
-}
-
-int wxComboBox::DoInsert(const wxString &item, unsigned int pos)
-{
- wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1,
- wxT("can't insert into sorted list"));
-
- wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
- wxCHECK_MSG( IsValidInsert(pos), -1, wxT("invalid index") );
-
- unsigned int count = GetCount();
-
- if (pos == count)
- return Append(item);
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- gtk_combo_box_insert_text( combobox, pos, wxGTK_CONV( item ) );
- }
- else
-#endif
- {
- DisableEvents();
-
- GtkWidget *list = GTK_COMBO(m_widget)->list;
- GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
-
- GList *gitem_list = g_list_alloc ();
- gitem_list->data = list_item;
- gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
-
- if (GTK_WIDGET_REALIZED(m_widget))
- {
- gtk_widget_realize( list_item );
- gtk_widget_realize( GTK_BIN(list_item)->child );
-
- ApplyWidgetStyle();
- }
-
- gtk_widget_show( list_item );
-
- EnableEvents();
- }
-
- count = GetCount();
-
- if ( m_clientDataList.GetCount() < count )
- m_clientDataList.Insert( pos, (wxObject*) NULL );
- if ( m_clientObjectList.GetCount() < count )
- m_clientObjectList.Insert( pos, (wxObject*) NULL );
-
- InvalidateBestSize();
-
- return pos;
-}
-
-void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- wxList::compatibility_iterator node = m_clientDataList.Item( n );
- if (!node) return;
-
- node->SetData( (wxObject*) clientData );
-}
-
-void* wxComboBox::DoGetItemClientData(unsigned int n) const
-{
- wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
-
- wxList::compatibility_iterator node = m_clientDataList.Item( n );
-
- return node ? node->GetData() : NULL;
-}
-
-void wxComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- wxList::compatibility_iterator node = m_clientObjectList.Item( n );
- if (!node) return;
-
- // wxItemContainer already deletes data for us
-
- node->SetData( (wxObject*) clientData );
-}
-
-wxClientData* wxComboBox::DoGetItemClientObject(unsigned int n) const
-{
- wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
-
- wxList::compatibility_iterator node = m_clientObjectList.Item( n );
-
- return node ? (wxClientData*) node->GetData() : NULL;
-}
-
-void wxComboBox::Clear()
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- DisableEvents();
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- const unsigned int count = GetCount();
- for (unsigned int i = 0; i < count; i++)
- gtk_combo_box_remove_text( combobox, 0 );
- }
- else // GTK+ < 2.4.0
-#endif // __WXGTK24__
- {
- GtkWidget *list = GTK_COMBO(m_widget)->list;
- gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
- }
-
- wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
- while (node)
- {
- wxClientData *cd = (wxClientData*)node->GetData();
- delete cd;
- node = node->GetNext();
- }
- m_clientObjectList.Clear();
-
- m_clientDataList.Clear();
-
- EnableEvents();
-
- InvalidateBestSize();
-}
-
-void wxComboBox::Delete(unsigned int n)
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- wxCHECK_RET( IsValid(n), wxT("invalid index") );
-
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- gtk_combo_box_remove_text( combobox, n );
- }
- else
-#endif
- {
- GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
-
- GList *child = g_list_nth( listbox->children, n );
-
- if (!child)
- {
- wxFAIL_MSG(wxT("wrong index"));
- return;
- }
-
- DisableEvents();
-
- GList *list = g_list_append( (GList*) NULL, child->data );
- gtk_list_remove_items( listbox, list );
- g_list_free( list );
-
- EnableEvents();
- }
-
- wxList::compatibility_iterator node = m_clientObjectList.Item( n );
- if (node)
- {
- wxClientData *cd = (wxClientData*)node->GetData();
- if (cd) delete cd;
- m_clientObjectList.Erase( node );
- }
-
- node = m_clientDataList.Item( n );
- if (node)
- m_clientDataList.Erase( node );
-
- InvalidateBestSize();
-}
-
-void wxComboBox::SetString(unsigned int n, const wxString &text)
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- wxCHECK_RET( IsValid(n), wxT("invalid index") );
-
- GtkTreeModel *model = gtk_combo_box_get_model( combobox );
- GtkTreeIter iter;
- if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
- {
- GValue value = { 0, };
- g_value_init( &value, G_TYPE_STRING );
- g_value_set_string( &value, wxGTK_CONV( text ) );
- gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value );
- g_value_unset( &value );
- }
- }
- else
-#endif
- {
- GtkWidget *list = GTK_COMBO(m_widget)->list;
-
- GList *child = g_list_nth( GTK_LIST(list)->children, n );
- if (child)
- {
- GtkBin *bin = GTK_BIN( child->data );
- GtkLabel *label = GTK_LABEL( bin->child );
- gtk_label_set_text(label, wxGTK_CONV(text));
- }
- else
- {
- wxFAIL_MSG( wxT("wxComboBox: wrong index") );
- }
- }
-
- InvalidateBestSize();
-}
-
-int wxComboBox::FindString( const wxString &item, bool bCase ) const
-{
- wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- GtkTreeModel* model = gtk_combo_box_get_model( combobox );
- GtkTreeIter iter;
- gtk_tree_model_get_iter_first( model, &iter );
- if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
- return -1;
- int count = 0;
- do
- {
- GValue value = { 0, };
- gtk_tree_model_get_value( model, &iter, 0, &value );
- wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
- g_value_unset( &value );
-
- if (item.IsSameAs( str, bCase ) )
- return count;
-
- count++;
-
- } while (gtk_tree_model_iter_next( model, &iter ));
- }
- else
-#endif
- {
- GtkWidget *list = GTK_COMBO(m_widget)->list;
-
- GList *child = GTK_LIST(list)->children;
- int count = 0;
- while (child)
- {
- GtkBin *bin = GTK_BIN( child->data );
- GtkLabel *label = GTK_LABEL( bin->child );
- wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
-
- if (item.IsSameAs( str , bCase ) )
- return count;
-
- count++;
- child = child->next;
- }
- }
-
- return wxNOT_FOUND;
-}
-
-int wxComboBox::GetSelection() const
-{
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- return gtk_combo_box_get_active( combobox );
- }
- else
-#endif
- // if the popup is currently opened, use the selection as it had been
- // before it dropped down
- return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
- : g_SelectionBeforePopup;
-}
-
-int wxComboBox::GetCurrentSelection() const
-{
- wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- return gtk_combo_box_get_active( combobox );
- }
- else
-#endif
- {
- GtkWidget *list = GTK_COMBO(m_widget)->list;
-
- GList *selection = GTK_LIST(list)->selection;
- if (selection)
- {
- GList *child = GTK_LIST(list)->children;
- int count = 0;
- while (child)
- {
- if (child->data == selection->data) return count;
- count++;
- child = child->next;
- }
- }
- }
-
- return -1;
-}
-
-wxString wxComboBox::GetString(unsigned int n) const
-{
- wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
-
- wxString str;
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- GtkTreeModel *model = gtk_combo_box_get_model( combobox );
- GtkTreeIter iter;
- if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
- {
- GValue value = { 0, };
- gtk_tree_model_get_value( model, &iter, 0, &value );
- wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
- g_value_unset( &value );
- return tmp;
- }
- }
- else
-#endif
- {
- GtkWidget *list = GTK_COMBO(m_widget)->list;
-
- GList *child = g_list_nth( GTK_LIST(list)->children, n );
- if (child)
- {
- GtkBin *bin = GTK_BIN( child->data );
- GtkLabel *label = GTK_LABEL( bin->child );
- str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
- }
- else
- {
- wxFAIL_MSG( wxT("wxComboBox: wrong index") );
- }
- }
-
- return str;
-}
-
-wxString wxComboBox::GetStringSelection() const
-{
- wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- int sel = gtk_combo_box_get_active( combobox );
- if (sel == -1)
- return wxEmptyString;
- return GetString(sel);
- }
- else
-#endif
- {
- GtkWidget *list = GTK_COMBO(m_widget)->list;
-
- GList *selection = GTK_LIST(list)->selection;
- if (selection)
- {
- GtkBin *bin = GTK_BIN( selection->data );
- GtkLabel *label = GTK_LABEL( bin->child );
- wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
- return tmp;
- }
-
- wxFAIL_MSG( wxT("wxComboBox: no selection") );
- }
-
- return wxEmptyString;
-}
-
-unsigned int wxComboBox::GetCount() const
-{
- wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- GtkTreeModel* model = gtk_combo_box_get_model( combobox );
- GtkTreeIter iter;
- gtk_tree_model_get_iter_first( model, &iter );
- if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
- return 0;
- unsigned int ret = 1;
- while (gtk_tree_model_iter_next( model, &iter ))
- ret++;
- return ret;
- }
- else
-#endif
- {
- GtkWidget *list = GTK_COMBO(m_widget)->list;
-
- GList *child = GTK_LIST(list)->children;
- unsigned int count = 0;
- while (child)
- {
- count++;
- child = child->next;
- }
- return count;
- }
-
- return 0;
-}
-
-void wxComboBox::SetSelection( int n )
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- DisableEvents();
-
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- {
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- gtk_combo_box_set_active( combobox, n );
- }
- else
-#endif
- {
- GtkWidget *list = GTK_COMBO(m_widget)->list;
- gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
- gtk_list_select_item( GTK_LIST(list), n );
- m_prevSelection = n;
- }
-
- EnableEvents();
-}
-
-wxString wxComboBox::GetValue() const
-{
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
-
-#if 0
- for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
- {
- wxChar c = tmp[i];
- printf( "%d ", (int) (c) );
- }
- printf( "\n" );
-#endif
-
- return tmp;
-}
-
-void wxComboBox::SetValue( const wxString& value )
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- wxString tmp;
- if (!value.IsNull()) tmp = value;
- gtk_entry_set_text( entry, wxGTK_CONV( tmp ) );
-
- InvalidateBestSize();
-}
-
-void wxComboBox::Copy()
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- gtk_editable_copy_clipboard(GTK_EDITABLE(entry));
-}
-
-void wxComboBox::Cut()
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- gtk_editable_cut_clipboard(GTK_EDITABLE(entry));
-}
-
-void wxComboBox::Paste()
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- gtk_editable_paste_clipboard(GTK_EDITABLE(entry));
-}
-
-void wxComboBox::Undo()
-{
- // TODO
-}
-
-void wxComboBox::Redo()
-{
- // TODO
-}
-
-void wxComboBox::SelectAll()
-{
- SetSelection(0, GetLastPosition());
-}
-
-bool wxComboBox::CanUndo() const
-{
- // TODO
- return false;
-}
-
-bool wxComboBox::CanRedo() const
-{
- // TODO
- return false;
-}
-
-bool wxComboBox::HasSelection() const
-{
- long from, to;
- GetSelection(&from, &to);
- return from != to;
-}
-
-bool wxComboBox::CanCopy() const
-{
- // Can copy if there's a selection
- return HasSelection();
-}
-
-bool wxComboBox::CanCut() const
-{
- return CanCopy() && IsEditable();
-}
-
-bool wxComboBox::CanPaste() const
-{
- // TODO: check for text on the clipboard
- return IsEditable() ;
-}
-
-bool wxComboBox::IsEditable() const
-{
- return !HasFlag(wxCB_READONLY);
-}
-
-
-void wxComboBox::SetInsertionPoint( long pos )
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- if ( pos == GetLastPosition() )
- pos = -1;
-
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- gtk_entry_set_position( entry, (int)pos );
-}
-
-long wxComboBox::GetInsertionPoint() const
-{
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- return (long) gtk_editable_get_position(GTK_EDITABLE(entry));
-}
-
-wxTextPos wxComboBox::GetLastPosition() const
-{
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- int pos = entry->text_length;
- return (long) pos-1;
-}
-
-void wxComboBox::Replace( long from, long to, const wxString& value )
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
- if (value.IsNull()) return;
- gint pos = (gint)to;
-
-#if wxUSE_UNICODE
- wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
- gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
-#else
- gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.length(), &pos );
-#endif
-}
-
-void wxComboBox::SetSelection( long from, long to )
-{
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
-}
-
-void wxComboBox::GetSelection( long* from, long* to ) const
-{
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- if (IsEditable())
- {
- GtkEditable *editable = GTK_EDITABLE(entry);
- gint start, end;
- gtk_editable_get_selection_bounds(editable, & start, & end);
- *from = start;
- *to = end;
- }
-}
-
-void wxComboBox::SetEditable( bool editable )
-{
- GtkEntry *entry = NULL;
-#ifdef __WXGTK24__
- if (!gtk_check_version(2,4,0))
- entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
- else
-#endif
- entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
-
- gtk_entry_set_editable( GTK_ENTRY(entry), editable );