]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkaccellabel.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkaccellabel.c
index 5f8c5396e22fbb31a1eb50b686f581d0bdf9a40d..d8bb7e96fa4069247059cc3183863c7fdc3af6ec 100644 (file)
@@ -15,9 +15,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
@@ -106,6 +104,9 @@ struct _GtkAccelLabelPrivate
   gchar         *accel_string;       /* has set function */
   guint          accel_padding;      /* should be style property? */
   guint16        accel_string_width; /* seems to be private */
+
+  guint           accel_key;         /* manual accel key specification if != 0 */
+  GdkModifierType accel_mods;
 };
 
 static void         gtk_accel_label_set_property (GObject            *object,
@@ -445,7 +446,6 @@ gtk_accel_label_draw (GtkWidget *widget,
 
       gtk_style_context_save (context);
       gtk_style_context_add_class (context, GTK_STYLE_CLASS_ACCELERATOR);
-      gtk_style_context_set_state (context, gtk_widget_get_state_flags (widget));
 
       gtk_render_layout (context, cr, x, y, accel_layout);
       gtk_style_context_restore (context);
@@ -836,12 +836,13 @@ _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
 #endif
       seen_mod = TRUE;
     }
-  if (seen_mod)
-    g_string_append (gstring, klass->mod_separator);
   
   ch = gdk_keyval_to_unicode (accelerator_key);
   if (ch && ch < 0x80 && (g_unichar_isgraph (ch) || ch == ' '))
     {
+      if (seen_mod)
+        g_string_append (gstring, klass->mod_separator);
+
       switch (ch)
        {
        case ' ':
@@ -862,6 +863,9 @@ _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
       tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
       if (tmp != NULL)
        {
+          if (seen_mod)
+            g_string_append (gstring, klass->mod_separator);
+
          if (tmp[0] != 0 && tmp[1] == 0)
            g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
          else
@@ -906,26 +910,52 @@ gtk_accel_label_refetch (GtkAccelLabel *accel_label)
                 "gtk-enable-accels", &enable_accels,
                 NULL);
 
-  if (enable_accels && accel_label->priv->accel_closure)
+  if (enable_accels && (accel_label->priv->accel_closure || accel_label->priv->accel_key))
     {
-      GtkAccelKey *key = gtk_accel_group_find (accel_label->priv->accel_group, find_accel, accel_label->priv->accel_closure);
+      gboolean have_accel = FALSE;
+      guint accel_key;
+      GdkModifierType accel_mods;
+
+      /* First check for a manual accel set with _set_accel() */
+      if (accel_label->priv->accel_key)
+        {
+          accel_mods = accel_label->priv->accel_mods;
+          accel_key = accel_label->priv->accel_key;
+          have_accel = TRUE;
+        }
 
-      if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
+      /* If we don't have a hardcoded value, check the accel group */
+      if (!have_accel)
+        {
+          GtkAccelKey *key;
+
+          key = gtk_accel_group_find (accel_label->priv->accel_group, find_accel, accel_label->priv->accel_closure);
+
+          if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
+            {
+              accel_key = key->accel_key;
+              accel_mods = key->accel_mods;
+              have_accel = TRUE;
+            }
+        }
+
+      /* If we found a key using either method, set it */
+      if (have_accel)
        {
          GtkAccelLabelClass *klass;
          gchar *tmp;
 
          klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
-         tmp = _gtk_accel_label_class_get_accelerator_label (klass,
-                                                             key->accel_key,
-                                                             key->accel_mods);
+         tmp = _gtk_accel_label_class_get_accelerator_label (klass, accel_key, accel_mods);
          accel_label->priv->accel_string = g_strconcat ("   ", tmp, NULL);
          g_free (tmp);
        }
-      if (!accel_label->priv->accel_string)
-       accel_label->priv->accel_string = g_strdup ("-/-");
+
+      else
+        /* Otherwise we have a closure with no key.  Show "-/-". */
+        accel_label->priv->accel_string = g_strdup ("-/-");
     }
-  
+
   if (!accel_label->priv->accel_string)
     accel_label->priv->accel_string = g_strdup ("");
 
@@ -933,3 +963,30 @@ gtk_accel_label_refetch (GtkAccelLabel *accel_label)
 
   return FALSE;
 }
+
+/**
+ * gtk_accel_label_set_accel:
+ * @accel_label: a #GtkAccelLabel
+ * @accelerator_key: a keyval, or 0
+ * @accelerator_mods: the modifier mask for the accel
+ *
+ * Manually sets a keyval and modifier mask as the accelerator rendered
+ * by @accel_label.
+ *
+ * If a keyval and modifier are explicitly set then these values are
+ * used regardless of any associated accel closure or widget.
+ *
+ * Providing an @accelerator_key of 0 removes the manual setting.
+ *
+ * Since: 3.6
+ */
+void
+gtk_accel_label_set_accel (GtkAccelLabel   *accel_label,
+                           guint            accelerator_key,
+                           GdkModifierType  accelerator_mods)
+{
+  accel_label->priv->accel_key = accelerator_key;
+  accel_label->priv->accel_mods = accelerator_mods;
+
+  gtk_accel_label_reset (accel_label);
+}