[gtk+] gtk-builder-tool: Expand the preview command



commit 073f8bc44bd5fae222d1d94cba31c872a8502826
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Jan 31 12:57:50 2016 -0500

    gtk-builder-tool: Expand the preview command
    
    Make the preview command parse options properly, turn the ID into
    an --id=ID option, and add a --css=FILE option that allows to
    specify a css file to use for previewing.
    
    This is useful for e.g. previewing the reftest .ui files with
    their corresponding .css.

 docs/reference/gtk/gtk-builder-tool.xml |   22 +++++-
 gtk/gtk-builder-tool.c                  |  116 +++++++++++++++++++++++--------
 2 files changed, 105 insertions(+), 33 deletions(-)
---
diff --git a/docs/reference/gtk/gtk-builder-tool.xml b/docs/reference/gtk/gtk-builder-tool.xml
index be8113b..375cdb9 100644
--- a/docs/reference/gtk/gtk-builder-tool.xml
+++ b/docs/reference/gtk/gtk-builder-tool.xml
@@ -31,6 +31,7 @@
 <cmdsynopsis>
 <command>gtk-builder-tool</command>
 <arg choice="opt"><replaceable>COMMAND</replaceable></arg>
+<arg choice="opt" rep="repeat"><replaceable>OPTION</replaceable></arg>
 <arg choice="plain"><replaceable>FILE</replaceable></arg>
 </cmdsynopsis>
 </refsynopsisdiv>
@@ -63,9 +64,24 @@
       <listitem><para>Lists all the named objects that are created in the .ui file.</para></listitem>
     </varlistentry>
     <varlistentry>
-    <term><option>preview</option> <arg choice="opt"><replaceable>ID</replaceable></arg></term>
-      <listitem><para>Preview the object with the given ID. If ID is not specified,
-                try to find a suitable object to preview.</para></listitem>
+    <term><option>preview</option></term>
+      <listitem><para>Preview the .ui file. This command accepts options
+                to specify the ID of an object and a .css file to use.</para></listitem>
+    </varlistentry>
+  </variablelist>
+</refsect1>
+
+<refsect1><title>Preview Options</title>
+  <para>The <option>preview</option> command accepts the following options:</para>
+  <variablelist>
+    <varlistentry>
+    <term><option>--id=<arg choice="plain">ID</arg></option></term>
+      <listitem><para>The ID of the object to preview. If not specified,
+                gtk-builder-tool will choose a suitable object on its own.</para></listitem>
+    </varlistentry>
+    <varlistentry>
+    <term><option>--css=<arg choice="plain">FILE</arg></option></term>
+      <listitem><para>Load style information from the given .css file.</para></listitem>
     </varlistentry>
   </variablelist>
 </refsect1>
diff --git a/gtk/gtk-builder-tool.c b/gtk/gtk-builder-tool.c
index bc18b38..8a42e4b 100644
--- a/gtk/gtk-builder-tool.c
+++ b/gtk/gtk-builder-tool.c
@@ -799,14 +799,31 @@ set_window_title (GtkWindow  *window,
 }
 
 static void
-do_preview (const char *filename,
-            const char *id)
+preview_file (const char *filename,
+              const char *id,
+              const char *cssfile)
 {
   GtkBuilder *builder;
   GError *error = NULL;
   GObject *object;
   GtkWidget *window;
 
+  if (cssfile)
+    {
+      GtkCssProvider *provider;
+
+      provider = gtk_css_provider_new ();
+      if (!gtk_css_provider_load_from_path (provider, cssfile, &error))
+        {
+          g_printerr ("%s\n", error->message);
+          exit (1);
+        }
+
+      gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
+                                                 GTK_STYLE_PROVIDER (provider),
+                                                 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+    }
+
   builder = gtk_builder_new ();
   if (!gtk_builder_add_from_file (builder, filename, &error))
     {
@@ -886,23 +903,74 @@ do_preview (const char *filename,
 }
 
 static void
+do_preview (int          *argc,
+            const char ***argv)
+{
+  GOptionContext *context;
+  char *id = NULL;
+  char *css = NULL;
+  char **filenames = NULL;
+  const GOptionEntry entries[] = {
+    { "id", 0, 0, G_OPTION_ARG_STRING, &id, NULL, NULL },
+    { "css", 0, 0, G_OPTION_ARG_FILENAME, &css, NULL, NULL },
+    { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, NULL }
+  };
+  GError *error = NULL;
+
+  context = g_option_context_new (NULL);
+  g_option_context_set_help_enabled (context, FALSE);
+  g_option_context_add_main_entries (context, entries, NULL);
+
+  if (!g_option_context_parse (context, argc, (char ***)argv, &error))
+    {
+      g_printerr ("%s\n", error->message);
+      g_error_free (error);
+      exit (1);
+    }
+
+  g_option_context_free (context);
+
+  if (filenames == NULL)
+    {
+      g_printerr ("No .ui file specified\n");
+      exit (1);
+    }
+
+  if (g_strv_length (filenames) > 1)
+    {
+      g_printerr ("Can only preview a single .ui file\n");
+      exit (1);
+    }
+
+  preview_file (filenames[0], id, css);
+
+  g_strfreev (filenames);
+  g_free (id);
+  g_free (css);
+}
+
+static void
 usage (void)
 {
   g_print (_("Usage:\n"
              "  gtk-builder-tool [COMMAND] FILE\n"
              "\n"
              "Commands:\n"
-             "  validate      Validate the file\n"
-             "  simplify      Simplify the file\n"
-             "  enumerate     List all named objects\n"
-             "  preview [ID]  Preview the named object\n"
+             "  validate           Validate the file\n"
+             "  simplify           Simplify the file\n"
+             "  enumerate          List all named objects\n"
+             "  preview [OPTIONS]  Preview the file\n"
+             "\n"
+             "Preview Options:\n"
+             "  --id=ID            Preview only the named object\n"
+             "  --css=FILE         Use style from CSS file\n"
              "\n"
              "Perform various tasks on GtkBuilder .ui files.\n"));
   exit (1);
 }
 
 int
-main (int argc, char *argv[])
+main (int argc, const char *argv[])
 {
   g_set_prgname ("gtk-builder-tool");
 
@@ -916,29 +984,17 @@ main (int argc, char *argv[])
   if (strcmp (argv[2], "--help") == 0)
     usage ();
 
-  if (strcmp (argv[1], "validate") == 0)
-    do_validate (argv[2]);
-  else if (strcmp (argv[1], "simplify") == 0)
-    do_simplify (argv[2]);
-  else if (strcmp (argv[1], "enumerate") == 0)
-    do_enumerate (argv[2]);
-  else if (strcmp (argv[1], "preview") == 0)
-    {
-      const char *filename, *id;
-
-      if (argc > 3)
-        {
-          id = argv[2];
-          filename = argv[3];
-        }
-      else
-        {
-          id = NULL;
-          filename = argv[2];
-        }
-
-      do_preview (filename, id);
-    }
+  argv++;
+  argc--;
+
+  if (strcmp (argv[0], "validate") == 0)
+    do_validate (argv[1]);
+  else if (strcmp (argv[0], "simplify") == 0)
+    do_simplify (argv[1]);
+  else if (strcmp (argv[0], "enumerate") == 0)
+    do_enumerate (argv[1]);
+  else if (strcmp (argv[0], "preview") == 0)
+    do_preview (&argc, &argv);
   else
     usage ();
 


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