]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-presence-chooser.c
Editing mode on click
[empathy.git] / libempathy-gtk / empathy-presence-chooser.c
index 32f32acb07b95935c8b37f72c761296a414a38db..74eefee2ab70061fecad059a493fbbff24cfb8ae 100644 (file)
@@ -1,6 +1,7 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2005-2007 Imendio AB
+ * Copyright (C) 2009 Collabora Ltd.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -20,6 +21,7 @@
  * Authors: Richard Hult <richard@imendio.com>
  *          Martyn Russell <martyn@imendio.com>
  *          Xavier Claessens <xclaesse@gmail.com>
+ *          Davyd Madeley <davyd.madeley@collabora.co.uk>
  */
 
 #include "config.h"
@@ -59,6 +61,7 @@ typedef struct {
        int          block_changed;
 
        McPresence   state;
+       int          previous_type;
 
        McPresence   flash_state_1;
        McPresence   flash_state_2;
@@ -127,6 +130,7 @@ enum
        COL_STATE,
        COL_STATUS_TEXT,
        COL_DISPLAY_MARKUP,
+       COL_STATUS_CUSTOMISABLE,
        COL_TYPE,
        N_COLUMNS
 };
@@ -148,6 +152,7 @@ create_model (void)
                        MC_TYPE_PRESENCE,       /* COL_STATE */
                        G_TYPE_STRING,          /* COL_STATUS_TEXT */
                        G_TYPE_STRING,          /* COL_DISPLAY_MARKUP */
+                       G_TYPE_BOOLEAN,         /* COL_STATUS_CUSTOMISABLE */
                        G_TYPE_INT);            /* COL_TYPE */
        
        GtkTreeIter iter;
@@ -165,6 +170,7 @@ create_model (void)
                                COL_STATE, states[i],
                                COL_STATUS_TEXT, status,
                                COL_DISPLAY_MARKUP, status,
+                               COL_STATUS_CUSTOMISABLE, states[i+1],
                                COL_TYPE, ENTRY_TYPE_BUILTIN,
                                -1);
 
@@ -178,6 +184,7 @@ create_model (void)
                                                COL_STATE, states[i],
                                                COL_STATUS_TEXT, l->data,
                                                COL_DISPLAY_MARKUP, l->data,
+                                               COL_STATUS_CUSTOMISABLE, TRUE,
                                                COL_TYPE, ENTRY_TYPE_SAVED,
                                                -1);
                        }
@@ -189,6 +196,7 @@ create_model (void)
                                        COL_STATE, states[i],
                                        COL_STATUS_TEXT, "",
                                        COL_DISPLAY_MARKUP, "<i>Custom Message...</i>",
+                                       COL_STATUS_CUSTOMISABLE, TRUE,
                                        COL_TYPE, ENTRY_TYPE_CUSTOM,
                                        -1);
                }
@@ -260,7 +268,14 @@ set_status_editing (EmpathyPresenceChooser *self, gboolean editing)
                                GTK_ENTRY_ICON_PRIMARY,
                                TRUE);
 
-               // FIXME - move the focus somewhere
+               /* attempt to get the toplevel for this widget */
+               GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
+               if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window))
+               {
+                       /* unset the focus */
+                       gtk_window_set_focus (GTK_WINDOW (window), NULL);
+               }
+               gtk_editable_set_position (GTK_EDITABLE (entry), 0);
 
                priv->editing_status = FALSE;
        }
@@ -303,6 +318,13 @@ ui_set_custom_state (EmpathyPresenceChooser *self,
        priv->block_set_editing--;
 }
 
+static void
+reset_status (EmpathyPresenceChooser *self)
+{
+       /* recover the status that was unset */
+       presence_chooser_presence_changed_cb (self);
+}
+
 static void
 entry_icon_release_cb (EmpathyPresenceChooser  *self,
                       GtkEntryIconPosition      icon_pos,
@@ -332,7 +354,7 @@ entry_key_press_event_cb (EmpathyPresenceChooser    *self,
        {
                /* the user pressed Escape, undo the editing */
                set_status_editing (self, FALSE);
-               presence_chooser_presence_changed_cb (self);
+               reset_status (self);
 
                return TRUE;
        }
@@ -340,6 +362,39 @@ entry_key_press_event_cb (EmpathyPresenceChooser   *self,
        return FALSE; /* send this event elsewhere */
 }
 
+static gboolean
+entry_button_press_event_cb (EmpathyPresenceChooser *self,
+                             GdkEventButton         *event,
+                            GtkWidget              *entry)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+
+       if (!priv->editing_status && event->button == 1)
+       {
+               set_status_editing (self, TRUE);
+               gtk_widget_grab_focus (entry);
+               gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
+
+               return TRUE;
+       }
+
+       return FALSE;
+}
+
+static void
+text_changed_cb (EmpathyPresenceChooser *self, gpointer user_data)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+
+       if (priv->block_changed) return;
+
+       /* the combo is being edited to a custom entry */
+       if (!priv->editing_status)
+       {
+               set_status_editing (self, TRUE);
+       }
+}
+
 static void
 changed_cb (GtkComboBox *self, gpointer user_data)
 {
@@ -349,37 +404,37 @@ changed_cb (GtkComboBox *self, gpointer user_data)
 
        GtkTreeIter iter;
        char *icon_name;
+       McPresence new_state;
+       gboolean customisable = TRUE;
        int type = -1;
 
        GtkTreeModel *model = gtk_combo_box_get_model (self);
        if (!gtk_combo_box_get_active_iter (self, &iter))
        {
-               /* the combo is being edited to a custom entry */
-               if (!priv->editing_status)
-               {
-                       set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
-               }
                return;
        }
 
        gtk_tree_model_get (model, &iter,
                        COL_STATE_ICON_NAME, &icon_name,
-                       COL_STATE, &priv->state,
+                       COL_STATE, &new_state,
+                       COL_STATUS_CUSTOMISABLE, &customisable,
                        COL_TYPE, &type,
                        -1);
 
        GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
 
+       /* some types of status aren't editable, set the editability of the
+        * entry appropriately. Unless we're just about to reset it anyway,
+        * in which case, don't fiddle with it */
+       if (type != ENTRY_TYPE_EDIT_CUSTOM)
+       {
+               gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
+               priv->state = new_state;
+       }
+
        if (type == ENTRY_TYPE_EDIT_CUSTOM)
        {
-               /* recover the status that was unset because COL_STATUS_TEXT
-                * is "". Unfortunately if you try and set COL_STATUS_TEXT to
-                * NULL, it generates a g_critical. I wonder if there is a
-                * better way around this. */
-               const char *status = empathy_idle_get_status (priv->idle);
-               priv->block_set_editing++;
-               gtk_entry_set_text (GTK_ENTRY (entry), status);
-               priv->block_set_editing--;
+               reset_status (EMPATHY_PRESENCE_CHOOSER (self));
 
                /* attempt to get the toplevel for this widget */
                GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
@@ -396,10 +451,19 @@ changed_cb (GtkComboBox *self, gpointer user_data)
                                GTK_ENTRY_ICON_PRIMARY,
                                icon_name);
 
+               /* preseed the status */
+               if (priv->previous_type == ENTRY_TYPE_BUILTIN)
+               {
+                       gtk_entry_set_text (GTK_ENTRY (entry), "");
+               }
+               else
+               {
+                       const char *status = empathy_idle_get_status (priv->idle);
+                       gtk_entry_set_text (GTK_ENTRY (entry), status);
+               }
+
                /* grab the focus */
                gtk_widget_grab_focus (entry);
-
-               set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
        }
        else
        {
@@ -417,6 +481,10 @@ changed_cb (GtkComboBox *self, gpointer user_data)
                g_free (status);
        }
 
+       if (type != ENTRY_TYPE_EDIT_CUSTOM)
+       {
+               priv->previous_type = type;
+       }
        g_free (icon_name);
 }
 
@@ -433,6 +501,20 @@ combo_row_separator_func (GtkTreeModel     *model,
        return (type == ENTRY_TYPE_SEPARATOR);
 }
 
+static gboolean
+focus_out_cb (EmpathyPresenceChooser *chooser, GdkEventFocus *event,
+              GtkEntry *entry)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
+
+       if (priv->editing_status)
+       {
+               // entry_activate_cb (chooser, entry);
+       }
+
+       return FALSE;
+}
+
 static void
 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
 {
@@ -462,7 +544,9 @@ empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
        g_signal_connect_object (entry, "key-press-event",
                        G_CALLBACK (entry_key_press_event_cb), chooser,
                        G_CONNECT_SWAPPED);
-       // FIXME - should this also happen when the user presses TAB ?
+       g_signal_connect_object (entry, "button-press-event",
+                       G_CALLBACK (entry_button_press_event_cb), chooser,
+                       G_CONNECT_SWAPPED);
 
        GtkCellRenderer *renderer;
        gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
@@ -486,6 +570,10 @@ empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
                        G_CALLBACK (popup_shown_cb), NULL);
        g_signal_connect (chooser, "changed",
                        G_CALLBACK (changed_cb), NULL);
+       g_signal_connect_swapped (entry, "changed",
+                       G_CALLBACK (text_changed_cb), chooser);
+       g_signal_connect_swapped (entry, "focus-out-event",
+                       G_CALLBACK (focus_out_cb), chooser);
 
        priv->idle = empathy_idle_dup_singleton ();
        presence_chooser_presence_changed_cb (chooser);
@@ -559,7 +647,9 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
                                COL_TYPE, &m_type,
                                -1);
 
-               if (m_type == ENTRY_TYPE_CUSTOM)
+               if (m_type == ENTRY_TYPE_CUSTOM ||
+                   m_type == ENTRY_TYPE_SEPARATOR ||
+                   m_type == ENTRY_TYPE_EDIT_CUSTOM)
                {
                        continue;
                }
@@ -590,9 +680,7 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
 
        if (match)
        {
-               priv->block_changed++;
                gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
-               priv->block_changed--;
        }
        else
        {