]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/combobox.cpp
Fix DrawRotatedtext to behave more like wxMSW: take into account
[wxWidgets.git] / src / gtk / combobox.cpp
index a03df573b1ae36ffca69b4307e01d23c0a0a916e..43f76581b634df122a8d2f0e6fea6d0d76e887f9 100644 (file)
@@ -63,6 +63,8 @@ gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
         GtkWidget *list = GTK_COMBO(combo->m_widget)->list;
         gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection );
     }
+    else if ((curSelection >= 0) && (combo->GetString(curSelection) == combo->GetValue()))
+        return;
 
     combo->m_prevSelection = curSelection;
 
@@ -85,6 +87,13 @@ gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
 
     if (!combo->m_hasVMT) return;
 
+    // avoids double events when the GetValue = one of the selections
+    if (combo->m_alreadySent)
+    {
+        combo->m_alreadySent = FALSE;
+        return;
+    }
+
     wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
     event.SetString( combo->GetValue() );
     event.SetEventObject( combo );
@@ -128,7 +137,6 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
     // and case-sensitive
     gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
 
-
     GtkWidget *list = GTK_COMBO(m_widget)->list;
 
 #ifndef __WXGTK20__
@@ -197,18 +205,29 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
 
 wxComboBox::~wxComboBox()
 {
-    wxNode *node = m_clientObjectList.First();
+    wxNode *node = m_clientObjectList.GetFirst();
     while (node)
     {
-        wxClientData *cd = (wxClientData*)node->Data();
+        wxClientData *cd = (wxClientData*)node->GetData();
         if (cd) delete cd;
-        node = node->Next();
+        node = node->GetNext();
     }
     m_clientObjectList.Clear();
 
     m_clientDataList.Clear();
 }
 
+void wxComboBox::SetFocus()
+{
+    if ( m_hasFocus )
+    {
+        // don't do anything if we already have focus
+        return;
+    }
+
+    gtk_widget_grab_focus( m_focusWidget );
+}
+
 void wxComboBox::AppendCommon( const wxString &item )
 {
     wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
@@ -261,7 +280,7 @@ void wxComboBox::SetClientData( int n, void* clientData )
 {
     wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
 
-    wxNode *node = m_clientDataList.Nth( n );
+    wxNode *node = m_clientDataList.Item( n );
     if (!node) return;
 
     node->SetData( (wxObject*) clientData );
@@ -271,20 +290,20 @@ void* wxComboBox::GetClientData( int n )
 {
     wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
 
-    wxNode *node = m_clientDataList.Nth( n );
+    wxNode *node = m_clientDataList.Item( n );
     if (!node) return NULL;
 
-    return node->Data();
+    return node->GetData();
 }
 
 void wxComboBox::SetClientObject( int n, wxClientData* clientData )
 {
     wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
 
-    wxNode *node = m_clientObjectList.Nth( n );
+    wxNode *node = m_clientObjectList.Item( n );
     if (!node) return;
 
-    wxClientData *cd = (wxClientData*) node->Data();
+    wxClientData *cd = (wxClientData*) node->GetData();
     if (cd) delete cd;
 
     node->SetData( (wxObject*) clientData );
@@ -294,10 +313,10 @@ wxClientData* wxComboBox::GetClientObject( int n )
 {
     wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
 
-    wxNode *node = m_clientObjectList.Nth( n );
+    wxNode *node = m_clientObjectList.Item( n );
     if (!node) return (wxClientData*) NULL;
 
-    return (wxClientData*) node->Data();
+    return (wxClientData*) node->GetData();
 }
 
 void wxComboBox::Clear()
@@ -307,12 +326,12 @@ void wxComboBox::Clear()
     GtkWidget *list = GTK_COMBO(m_widget)->list;
     gtk_list_clear_items( GTK_LIST(list), 0, Number() );
 
-    wxNode *node = m_clientObjectList.First();
+    wxNode *node = m_clientObjectList.GetFirst();
     while (node)
     {
-        wxClientData *cd = (wxClientData*)node->Data();
+        wxClientData *cd = (wxClientData*)node->GetData();
         if (cd) delete cd;
-        node = node->Next();
+        node = node->GetNext();
     }
     m_clientObjectList.Clear();
 
@@ -337,15 +356,15 @@ void wxComboBox::Delete( int n )
     gtk_list_remove_items( listbox, list );
     g_list_free( list );
 
-    wxNode *node = m_clientObjectList.Nth( n );
+    wxNode *node = m_clientObjectList.Item( n );
     if (node)
     {
-        wxClientData *cd = (wxClientData*)node->Data();
+        wxClientData *cd = (wxClientData*)node->GetData();
         if (cd) delete cd;
         m_clientObjectList.DeleteNode( node );
     }
 
-    node = m_clientDataList.Nth( n );
+    node = m_clientDataList.Item( n );
     if (node)
     {
         m_clientDataList.DeleteNode( node );
@@ -364,8 +383,14 @@ int wxComboBox::FindString( const wxString &item )
     {
         GtkBin *bin = GTK_BIN( child->data );
         GtkLabel *label = GTK_LABEL( bin->child );
-        if (item == wxString(label->label,*wxConvCurrent))
+#ifdef __WXGTK20__
+        wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
+#else
+        wxString str( label->label );
+#endif
+        if (item == str)
             return count;
+            
         count++;
         child = child->next;
     }
@@ -408,7 +433,7 @@ wxString wxComboBox::GetString( int n ) const
         GtkBin *bin = GTK_BIN( child->data );
         GtkLabel *label = GTK_LABEL( bin->child );
 #ifdef __WXGTK20__
-        str = wxGTK_CONV_BACK( gtk_label_get_text( label) );
+        str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
 #else
         str = wxString( label->label );
 #endif
@@ -431,7 +456,12 @@ wxString wxComboBox::GetStringSelection() const
     if (selection)
     {
         GtkBin *bin = GTK_BIN( selection->data );
-        wxString tmp = wxString(GTK_LABEL( bin->child )->label,*wxConvCurrent);
+        GtkLabel *label = GTK_LABEL( bin->child );
+#ifdef __WXGTK20__
+        wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
+#else
+        wxString tmp( label->label );
+#endif
         return tmp;
     }
 
@@ -592,57 +622,31 @@ void wxComboBox::SetEditable( bool editable )
 
 void wxComboBox::OnChar( wxKeyEvent &event )
 {
-    if ( event.KeyCode() == WXK_RETURN )
+    if ( event.GetKeyCode() == WXK_RETURN )
     {
         wxString value = GetValue();
+        int selection = GetSelection();
 
-        if ( Number() == 0 )
+        // note that gtk automatically selects an item if its in the list
+        // so you don't have to call FindString
+        if ((selection >= 0) && (GetString(selection) == value))
         {
-            // make Enter generate "selected" event if there is only one item
-            // in the combobox - without it, it's impossible to select it at
-            // all!
+            // make Enter generate "selected" event if it equals an item
             wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
-            event.SetInt( 0 );
+            event.SetInt( selection );
             event.SetString( value );
             event.SetEventObject( this );
             GetEventHandler()->ProcessEvent( event );
         }
         else
         {
-            // add the item to the list if it's not there yet
-            if ( FindString(value) == wxNOT_FOUND )
-            {
-                Append(value);
-                SetStringSelection(value);
-
-                // and generate the selected event for it
-                wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
-                event.SetInt( Number() - 1 );
-                event.SetString( value );
-                event.SetEventObject( this );
-                GetEventHandler()->ProcessEvent( event );
-            }
-
-            // This will invoke the dialog default action, such
-            // as the clicking the default button.
-
-            wxWindow *top_frame = m_parent;
-            while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
-            top_frame = top_frame->GetParent();
-    
-            if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
-            {
-                GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
-
-                if (window->default_widget)
-                {
-                    gtk_widget_activate (window->default_widget);
-                    return;
-                }
-            }
-            
-            return;
+            wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId());
+            event.SetString(value);
+            event.SetInt(selection);
+            event.SetEventObject( this );
+            GetEventHandler()->ProcessEvent( event );
         }
+        return;
     }
     
     event.Skip();
@@ -728,13 +732,11 @@ wxSize wxComboBox::DoGetBestSize() const
     ret.x = 0;
     if ( m_widget )
     {
-        GdkFont *font = m_font.GetInternalFont();
-
-        wxCoord width;
-        size_t count = Number();
+        int width;
+        size_t count = GetCount();
         for ( size_t n = 0; n < count; n++ )
         {
-            width = (wxCoord)gdk_string_width(font, wxGTK_CONV( GetString(n) ) );
+            GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font );
             if ( width > ret.x )
                 ret.x = width;
         }