[gnome-builder/wip/chergert/shortcuts: 5/24] wip: make a keyboard looking like thing



commit 80275fec44c7be1cb8e53e008a7f57f8c79abfe3
Author: Christian Hergert <christian hergert me>
Date:   Wed Aug 26 01:34:00 2015 -0700

    wip: make a keyboard looking like thing
    
    Still need to get those keys to align as a grid though!

 data/theme/Adwaita-dark.css           |   10 ++
 data/theme/Adwaita.css                |    9 ++
 src/Makefile.am                       |    2 +
 src/keybindings/gb-accel-label.c      |  197 +++++++++++++++++++++++++++++++++
 src/keybindings/gb-accel-label.h      |   37 ++++++
 src/keybindings/gb-shortcuts-window.c |   16 ++-
 6 files changed, 264 insertions(+), 7 deletions(-)
---
diff --git a/data/theme/Adwaita-dark.css b/data/theme/Adwaita-dark.css
index 95fe202..e88014e 100644
--- a/data/theme/Adwaita-dark.css
+++ b/data/theme/Adwaita-dark.css
@@ -36,3 +36,13 @@ GbTree.dim-label {
 GbTree:backdrop.dim-label {
   color: #6d6f70;
 }
+
+
+GbAccelLabel GtkFrame {
+  background-color: #222;
+  border: 1px solid #cecece;
+  border-color: #555753;
+  border-radius: 5px;
+  color: #eeeeec;
+  padding: 3px 8 3 8;
+}
diff --git a/data/theme/Adwaita.css b/data/theme/Adwaita.css
index ee35d6f..b1af1cb 100644
--- a/data/theme/Adwaita.css
+++ b/data/theme/Adwaita.css
@@ -42,3 +42,12 @@ GbTree.dim-label {
 GbTree:backdrop.dim-label {
   color: #b6b7b4;
 }
+
+
+GbAccelLabel GtkFrame {
+  color: #555753;
+  border-radius: 5px;
+  padding: 3px 8 3 8;
+  background-color: #fefefe;
+  border: 1px solid #cecece;
+}
diff --git a/src/Makefile.am b/src/Makefile.am
index d74050f..d35b785 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -58,6 +58,8 @@ libgnome_builder_la_SOURCES = \
        html/gb-html-document.h \
        html/gb-html-view.c \
        html/gb-html-view.h \
+       keybindings/gb-accel-label.c \
+       keybindings/gb-accel-label.h \
        keybindings/gb-keybindings.c \
        keybindings/gb-keybindings.h \
        keybindings/gb-shortcuts-window.c \
diff --git a/src/keybindings/gb-accel-label.c b/src/keybindings/gb-accel-label.c
new file mode 100644
index 0000000..0ded358
--- /dev/null
+++ b/src/keybindings/gb-accel-label.c
@@ -0,0 +1,197 @@
+/* gb-accel-label.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gb-accel-label.h"
+#include "gb-widget.h"
+
+struct _GbAccelLabel
+{
+  GtkBox  parent_instance;
+
+  gchar  *accelerator;
+};
+
+G_DEFINE_TYPE (GbAccelLabel, gb_accel_label, GTK_TYPE_BOX)
+
+enum {
+  PROP_0,
+  PROP_ACCELERATOR,
+  LAST_PROP
+};
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+
+static void
+gb_accel_label_rebuild (GbAccelLabel *self)
+{
+  g_auto(GStrv) keys = NULL;
+  g_autofree gchar *label = NULL;
+  GdkModifierType modifier = 0;
+  guint key = 0;
+  guint i;
+
+  g_assert (GB_IS_ACCEL_LABEL (self));
+
+  gtk_container_foreach (GTK_CONTAINER (self), (GtkCallback)gtk_widget_destroy, NULL);
+
+  if (self->accelerator == NULL)
+    return;
+
+  gtk_accelerator_parse (self->accelerator, &key, &modifier);
+  if ((key == 0) && (modifier == 0))
+    return;
+
+  label = gtk_accelerator_get_label (key, modifier);
+  if (label == NULL)
+    return;
+
+  keys = g_strsplit (label, "+", 0);
+
+  for (i = 0; keys [i]; i++)
+    {
+      GtkFrame *frame;
+      GtkLabel *disp;
+
+      if (i > 0)
+        {
+          GtkLabel *plus;
+
+          plus = g_object_new (GTK_TYPE_LABEL,
+                               "label", "+",
+                               "visible", TRUE,
+                               NULL);
+          gb_widget_add_style_class (GTK_WIDGET (plus), "dim-label");
+          gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (plus));
+        }
+
+      frame = g_object_new (GTK_TYPE_FRAME,
+                            "visible", TRUE,
+                            NULL);
+      gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (frame));
+
+      disp = g_object_new (GTK_TYPE_LABEL,
+                           "label", keys [i],
+                           "visible", TRUE,
+                           NULL);
+      gtk_container_add (GTK_CONTAINER (frame), GTK_WIDGET (disp));
+    }
+}
+
+static void
+gb_accel_label_finalize (GObject *object)
+{
+  GbAccelLabel *self = (GbAccelLabel *)object;
+
+  g_clear_pointer (&self->accelerator, g_free);
+
+  G_OBJECT_CLASS (gb_accel_label_parent_class)->finalize (object);
+}
+
+static void
+gb_accel_label_get_property (GObject    *object,
+                             guint       prop_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
+{
+  GbAccelLabel *self = GB_ACCEL_LABEL (object);
+
+  switch (prop_id)
+    {
+    case PROP_ACCELERATOR:
+      g_value_set_string (value, gb_accel_label_get_accelerator (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_accel_label_set_property (GObject      *object,
+                             guint         prop_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+  GbAccelLabel *self = GB_ACCEL_LABEL (object);
+
+  switch (prop_id)
+    {
+    case PROP_ACCELERATOR:
+      gb_accel_label_set_accelerator (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_accel_label_class_init (GbAccelLabelClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gb_accel_label_finalize;
+  object_class->get_property = gb_accel_label_get_property;
+  object_class->set_property = gb_accel_label_set_property;
+
+  gParamSpecs [PROP_ACCELERATOR] =
+    g_param_spec_string ("accelerator",
+                         "Accelerator",
+                         "Accelerator",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, LAST_PROP, gParamSpecs);
+}
+
+static void
+gb_accel_label_init (GbAccelLabel *self)
+{
+  gtk_box_set_spacing (GTK_BOX (self), 6);
+}
+
+GtkWidget *
+gb_accel_label_new (const gchar *accelerator)
+{
+  return g_object_new (GB_TYPE_ACCEL_LABEL,
+                       "accelerator", accelerator,
+                       NULL);
+}
+
+const gchar *
+gb_accel_label_get_accelerator (GbAccelLabel *self)
+{
+  g_return_val_if_fail (GB_IS_ACCEL_LABEL (self), NULL);
+
+  return self->accelerator;
+}
+
+void
+gb_accel_label_set_accelerator (GbAccelLabel *self,
+                                const gchar  *accelerator)
+{
+  g_return_if_fail (GB_IS_ACCEL_LABEL (self));
+
+  if (g_strcmp0 (accelerator, self->accelerator) != 0)
+    {
+      g_free (self->accelerator);
+      self->accelerator = g_strdup (accelerator);
+      gb_accel_label_rebuild (self);
+      g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_ACCELERATOR]);
+    }
+}
diff --git a/src/keybindings/gb-accel-label.h b/src/keybindings/gb-accel-label.h
new file mode 100644
index 0000000..f4a9694
--- /dev/null
+++ b/src/keybindings/gb-accel-label.h
@@ -0,0 +1,37 @@
+/* gb-accel-label.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GB_ACCEL_LABEL_H
+#define GB_ACCEL_LABEL_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_ACCEL_LABEL (gb_accel_label_get_type())
+
+G_DECLARE_FINAL_TYPE (GbAccelLabel, gb_accel_label, GB, ACCEL_LABEL, GtkBox)
+
+GtkWidget   *gb_accel_label_new             (const gchar  *accelerator);
+const gchar *gb_accel_label_get_accelerator (GbAccelLabel *self);
+void         gb_accel_label_set_accelerator (GbAccelLabel *self,
+                                             const gchar  *accelerator);
+
+G_END_DECLS
+
+#endif /* GB_ACCEL_LABEL_H */
diff --git a/src/keybindings/gb-shortcuts-window.c b/src/keybindings/gb-shortcuts-window.c
index c899800..7b399cb 100644
--- a/src/keybindings/gb-shortcuts-window.c
+++ b/src/keybindings/gb-shortcuts-window.c
@@ -18,6 +18,7 @@
 
 #include <glib/gi18n.h>
 
+#include "gb-accel-label.h"
 #include "gb-shortcuts-window.h"
 
 struct _GbShortcutsWindow
@@ -115,8 +116,9 @@ gb_shortcuts_window_build (GbShortcutsWindow *self)
     g_autofree gchar *title = g_strdup_printf ("%u", ++page_count); \
     GtkBox *page; \
     page = g_object_new (GTK_TYPE_BOX, \
-                         "homogeneous", TRUE, \
+                         "homogeneous", FALSE, \
                          "orientation", GTK_ORIENTATION_HORIZONTAL, \
+                         "spacing", 24, \
                          "visible", TRUE, \
                          NULL); \
     _columns \
@@ -143,7 +145,7 @@ gb_shortcuts_window_build (GbShortcutsWindow *self)
     GtkBox *title_label; \
     group = g_object_new (GTK_TYPE_BOX, \
                           "orientation", GTK_ORIENTATION_VERTICAL, \
-                          "spacing", 11, \
+                          "spacing", 10, \
                           "visible", TRUE, \
                           NULL); \
     gtk_container_add (GTK_CONTAINER (column), GTK_WIDGET (group)); \
@@ -159,18 +161,18 @@ gb_shortcuts_window_build (GbShortcutsWindow *self)
 #define SHORTCUT(_accel, _desc) \
   { \
     GtkBox *shortcut; \
-    GtkLabel *accel; \
+    GbAccelLabel *accel; \
     GtkLabel *desc; \
     shortcut = g_object_new (GTK_TYPE_BOX, \
                              "orientation", GTK_ORIENTATION_HORIZONTAL, \
-                             "spacing", 11, \
+                             "spacing", 10, \
                              "visible", TRUE, \
                              NULL); \
     gtk_container_add (GTK_CONTAINER (group), GTK_WIDGET (shortcut)); \
-    accel = g_object_new (GTK_TYPE_LABEL, \
-                          "label", _accel, \
+    accel = g_object_new (GB_TYPE_ACCEL_LABEL, \
+                          "accelerator", _accel, \
+                          "halign", GTK_ALIGN_START, \
                           "visible", TRUE, \
-                          "xalign", 0.0f, \
                           NULL); \
     gtk_size_group_add_widget (size_group, GTK_WIDGET (accel)); \
     gtk_container_add (GTK_CONTAINER (shortcut), GTK_WIDGET (accel)); \


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]