]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk1/combobox.cpp
another attempt to improve combobox behaviour
[wxWidgets.git] / src / gtk1 / combobox.cpp
index 7fd217fda342388270ff71edad84f80c775622e2..5ab1cd7b75e30c432b5323ea3ffb802a5d9b13f0 100644 (file)
@@ -29,10 +29,14 @@ extern bool   g_blockEventsOnDrag;
 // "select"
 //-----------------------------------------------------------------------------
 
-static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
+static void
+gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
 {
-    if (!combo->HasVMT()) return;
-    if (g_blockEventsOnDrag) return;
+    if (!combo->HasVMT())
+        return;
+
+    if (g_blockEventsOnDrag)
+        return;
 
     if (combo->m_alreadySent)
     {
@@ -42,12 +46,13 @@ static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox
 
     combo->m_alreadySent = TRUE;
 
-    wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId());
+    wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
     event.SetInt( combo->GetSelection() );
-    wxString tmp( combo->GetStringSelection() );
-    event.SetString( WXSTRINGCAST(tmp) );
-    event.SetEventObject(combo);
-    combo->GetEventHandler()->ProcessEvent(event);
+    event.SetString( copystring(combo->GetStringSelection()) );
+    event.SetEventObject( combo );
+    combo->GetEventHandler()->ProcessEvent( event );
+
+    delete [] event.GetString();
 }
 
 //-----------------------------------------------------------------------------
@@ -57,11 +62,12 @@ static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox
 static void
 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
 {
-    wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->m_windowId );
+    wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
     event.SetString( copystring(combo->GetValue()) );
     event.SetEventObject( combo );
     combo->GetEventHandler()->ProcessEvent( event );
-    delete[] event.GetString();
+
+    delete [] event.GetString();
 }
 
 //-----------------------------------------------------------------------------
@@ -72,6 +78,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
 
 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
     EVT_SIZE(wxComboBox::OnSize)
+    EVT_CHAR(wxComboBox::OnChar)
 END_EVENT_TABLE()
 
 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
@@ -90,9 +97,14 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
 
     m_widget = gtk_combo_new();
 
+    // make it more useable
+    gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget), TRUE);
+
     wxSize newSize = size;
-    if (newSize.x == -1) newSize.x = 100;
-    if (newSize.y == -1) newSize.y = 26;
+    if (newSize.x == -1)
+        newSize.x = 100;
+    if (newSize.y == -1)
+        newSize.y = 26;
     SetSize( newSize.x, newSize.y );
 
     GtkWidget *list = GTK_COMBO(m_widget)->list;
@@ -134,6 +146,7 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
 
     SetBackgroundColour( parent->GetBackgroundColour() );
     SetForegroundColour( parent->GetForegroundColour() );
+    SetFont( parent->GetFont() );
 
     Show( TRUE );
 
@@ -301,14 +314,13 @@ int wxComboBox::FindString( const wxString &item )
     {
         GtkBin *bin = GTK_BIN( child->data );
         GtkLabel *label = GTK_LABEL( bin->child );
-        if (item == label->label) return count;
+        if (item == label->label)
+            return count;
         count++;
         child = child->next;
     }
 
-    wxFAIL_MSG( "wxComboBox: string not found" );
-
-    return -1;
+    return wxNOT_FOUND;
 }
 
 int wxComboBox::GetSelection() const
@@ -341,17 +353,20 @@ wxString wxComboBox::GetString( int n ) const
 
     GtkWidget *list = GTK_COMBO(m_widget)->list;
 
+    wxString str;
     GList *child = g_list_nth( GTK_LIST(list)->children, n );
     if (child)
     {
         GtkBin *bin = GTK_BIN( child->data );
         GtkLabel *label = GTK_LABEL( bin->child );
-        return label->label;
+        str = label->label;
+    }
+    else
+    {
+        wxFAIL_MSG( "wxComboBox: wrong index" );
     }
 
-    wxFAIL_MSG( "wxComboBox: wrong index" );
-
-    return "";
+    return str;
 }
 
 wxString wxComboBox::GetStringSelection() const
@@ -502,14 +517,54 @@ void wxComboBox::Remove(long from, long to)
     gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
 }
 
-void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
+void wxComboBox::SetSelection( long from, long to )
 {
-    wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
+    GtkWidget *entry = GTK_COMBO(m_widget)->entry;
+    gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
 }
 
-void wxComboBox::SetEditable( bool WXUNUSED(editable) )
+void wxComboBox::SetEditable( bool editable )
 {
-    wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
+    GtkWidget *entry = GTK_COMBO(m_widget)->entry;
+    gtk_entry_set_editable( GTK_ENTRY(entry), editable );
+}
+
+void wxComboBox::OnChar( wxKeyEvent &event )
+{
+    if ( event.KeyCode() == WXK_RETURN )
+    {
+        wxString value = GetValue();
+
+        if ( Number() == 0 )
+        {
+            // make Enter generate "selected" event if there is only one item
+            // in the combobox - without it, it's impossible to select it at
+            // all!
+            wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
+            event.SetInt( 0 );
+            event.SetString( (char *)value.c_str() );
+            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);
+
+                // and generate the selected event for it
+                wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
+                event.SetInt( Number() - 1 );
+                event.SetString( (char *)value.c_str() );
+                event.SetEventObject( this );
+                GetEventHandler()->ProcessEvent( event );
+            }
+            //else: do nothing, this will open the listbox
+        }
+    }
+
+    event.Skip();
 }
 
 void wxComboBox::OnSize( wxSizeEvent &event )