2015-04-19 01:03:52 +00:00
|
|
|
|
/* Copyright 2015 Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* GTK+ is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* GLib 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
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with GTK+; see the file COPYING. If not,
|
|
|
|
|
* see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
* Author: Matthias Clasen
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2016-11-13 03:29:02 +00:00
|
|
|
|
#include <errno.h>
|
2015-04-27 22:39:22 +00:00
|
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
2016-04-15 17:37:49 +00:00
|
|
|
|
#include <glib/gprintf.h>
|
2016-11-13 03:29:02 +00:00
|
|
|
|
#include <glib/gstdio.h>
|
2015-04-19 01:03:52 +00:00
|
|
|
|
#include <gtk/gtk.h>
|
2015-04-30 05:18:48 +00:00
|
|
|
|
#include "gtkbuilderprivate.h"
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
GtkBuilder *builder;
|
|
|
|
|
GList *classes;
|
|
|
|
|
gboolean packing;
|
|
|
|
|
gboolean packing_started;
|
2015-05-02 16:13:35 +00:00
|
|
|
|
gboolean cell_packing;
|
|
|
|
|
gboolean cell_packing_started;
|
2015-05-02 18:39:35 +00:00
|
|
|
|
gint in_child;
|
|
|
|
|
gint child_started;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
gchar **attribute_names;
|
|
|
|
|
gchar **attribute_values;
|
|
|
|
|
GString *value;
|
|
|
|
|
gboolean unclosed_starttag;
|
|
|
|
|
gint indent;
|
2016-11-13 03:29:02 +00:00
|
|
|
|
char *input_filename;
|
|
|
|
|
char *output_filename;
|
|
|
|
|
FILE *output;
|
2015-04-30 05:18:48 +00:00
|
|
|
|
} MyParserData;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
2016-05-06 17:42:30 +00:00
|
|
|
|
static void
|
|
|
|
|
canonicalize_key (gchar *key)
|
|
|
|
|
{
|
|
|
|
|
gchar *p;
|
|
|
|
|
|
|
|
|
|
for (p = key; *p != 0; p++)
|
|
|
|
|
{
|
|
|
|
|
gchar c = *p;
|
|
|
|
|
|
|
|
|
|
/* We may meet something like AtkObject::accessible-name */
|
|
|
|
|
if (c == ':' && ((p > key && p[-1] == ':') || p[1] == ':'))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (c != '-' &&
|
|
|
|
|
(c < '0' || c > '9') &&
|
|
|
|
|
(c < 'A' || c > 'Z') &&
|
|
|
|
|
(c < 'a' || c > 'z'))
|
|
|
|
|
*p = '-';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-09 20:47:02 +00:00
|
|
|
|
static GParamSpec *
|
|
|
|
|
get_property_pspec (MyParserData *data,
|
|
|
|
|
const gchar *class_name,
|
|
|
|
|
const gchar *property_name)
|
2015-04-19 01:03:52 +00:00
|
|
|
|
{
|
|
|
|
|
GType type;
|
|
|
|
|
GObjectClass *class;
|
|
|
|
|
GParamSpec *pspec;
|
|
|
|
|
gchar *canonical_name;
|
|
|
|
|
|
|
|
|
|
type = g_type_from_name (class_name);
|
|
|
|
|
if (type == G_TYPE_INVALID)
|
2015-09-09 20:47:02 +00:00
|
|
|
|
return NULL;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
|
|
|
|
class = g_type_class_ref (type);
|
|
|
|
|
canonical_name = g_strdup (property_name);
|
2016-05-06 17:42:30 +00:00
|
|
|
|
canonicalize_key (canonical_name);
|
2015-04-19 01:03:52 +00:00
|
|
|
|
if (data->packing)
|
|
|
|
|
pspec = gtk_container_class_find_child_property (class, canonical_name);
|
2015-05-02 16:13:35 +00:00
|
|
|
|
else if (data->cell_packing)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *cell_class;
|
|
|
|
|
|
|
|
|
|
/* We're just assuming that the cell layout is using a GtkCellAreaBox. */
|
|
|
|
|
cell_class = g_type_class_ref (GTK_TYPE_CELL_AREA_BOX);
|
|
|
|
|
pspec = gtk_cell_area_class_find_cell_property (GTK_CELL_AREA_CLASS (cell_class), canonical_name);
|
|
|
|
|
g_type_class_unref (cell_class);
|
|
|
|
|
}
|
2015-04-19 01:03:52 +00:00
|
|
|
|
else
|
|
|
|
|
pspec = g_object_class_find_property (class, canonical_name);
|
|
|
|
|
g_free (canonical_name);
|
|
|
|
|
g_type_class_unref (class);
|
|
|
|
|
|
2015-09-09 20:47:02 +00:00
|
|
|
|
return pspec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
value_is_default (MyParserData *data,
|
|
|
|
|
const gchar *class_name,
|
|
|
|
|
const gchar *property_name,
|
|
|
|
|
const gchar *value_string)
|
|
|
|
|
{
|
|
|
|
|
GValue value = { 0, };
|
|
|
|
|
gboolean ret;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
GParamSpec *pspec;
|
|
|
|
|
|
|
|
|
|
pspec = get_property_pspec (data, class_name, property_name);
|
|
|
|
|
|
2015-04-19 01:03:52 +00:00
|
|
|
|
if (pspec == NULL)
|
|
|
|
|
{
|
2015-04-27 22:39:22 +00:00
|
|
|
|
if (data->packing)
|
|
|
|
|
g_printerr (_("Packing property %s::%s not found\n"), class_name, property_name);
|
2015-05-02 16:13:35 +00:00
|
|
|
|
else if (data->cell_packing)
|
|
|
|
|
g_printerr (_("Cell property %s::%s not found\n"), class_name, property_name);
|
2015-04-27 22:39:22 +00:00
|
|
|
|
else
|
|
|
|
|
g_printerr (_("Property %s::%s not found\n"), class_name, property_name);
|
2015-04-19 01:03:52 +00:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
else if (g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspec), G_TYPE_OBJECT))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
2015-04-27 22:39:22 +00:00
|
|
|
|
if (!gtk_builder_value_from_string (data->builder, pspec, value_string, &value, &error))
|
2015-04-19 01:03:52 +00:00
|
|
|
|
{
|
2016-10-03 16:20:03 +00:00
|
|
|
|
g_printerr (_("Couldn’t parse value for %s::%s: %s\n"), class_name, property_name, error->message);
|
2015-04-19 01:03:52 +00:00
|
|
|
|
g_error_free (error);
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
ret = g_param_value_defaults (pspec, &value);
|
|
|
|
|
|
|
|
|
|
g_value_reset (&value);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-09 20:47:02 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
property_is_boolean (MyParserData *data,
|
|
|
|
|
const gchar *class_name,
|
|
|
|
|
const gchar *property_name)
|
|
|
|
|
{
|
|
|
|
|
GParamSpec *pspec;
|
|
|
|
|
|
|
|
|
|
pspec = get_property_pspec (data, class_name, property_name);
|
|
|
|
|
if (pspec)
|
|
|
|
|
return G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_BOOLEAN;
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const gchar *
|
|
|
|
|
canonical_boolean_value (MyParserData *data,
|
|
|
|
|
const gchar *string)
|
|
|
|
|
{
|
|
|
|
|
GValue value = G_VALUE_INIT;
|
|
|
|
|
gboolean b = FALSE;
|
|
|
|
|
|
|
|
|
|
if (gtk_builder_value_from_string_type (data->builder, G_TYPE_BOOLEAN, string, &value, NULL))
|
|
|
|
|
b = g_value_get_boolean (&value);
|
|
|
|
|
|
|
|
|
|
return b ? "1" : "0";
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-02 11:35:35 +00:00
|
|
|
|
/* A number of properties unfortunately can't be omitted even
|
|
|
|
|
* if they are nominally set to their default value. In many
|
|
|
|
|
* cases, this is due to subclasses not overriding the default
|
|
|
|
|
* value from the superclass.
|
|
|
|
|
*/
|
|
|
|
|
static gboolean
|
|
|
|
|
needs_explicit_setting (MyParserData *data,
|
2015-09-09 20:47:02 +00:00
|
|
|
|
const gchar *class_name,
|
|
|
|
|
const gchar *property_name)
|
2015-05-02 11:35:35 +00:00
|
|
|
|
{
|
|
|
|
|
struct _Prop {
|
|
|
|
|
const char *class;
|
|
|
|
|
const char *property;
|
|
|
|
|
gboolean packing;
|
|
|
|
|
} props[] = {
|
2015-05-02 14:16:55 +00:00
|
|
|
|
{ "GtkAboutDialog", "program-name", 0 },
|
|
|
|
|
{ "GtkCalendar", "year", 0 },
|
|
|
|
|
{ "GtkCalendar", "month", 0 },
|
|
|
|
|
{ "GtkCalendar", "day", 0 },
|
|
|
|
|
{ "GtkPlacesSidebar", "show-desktop", 0 },
|
2015-05-02 11:35:35 +00:00
|
|
|
|
{ "GtkRadioButton", "draw-indicator", 0 },
|
|
|
|
|
{ "GtkGrid", "left-attach", 1 },
|
|
|
|
|
{ "GtkGrid", "top-attach", 1 },
|
2015-06-25 11:01:26 +00:00
|
|
|
|
{ "GtkWidget", "hexpand", 0 },
|
|
|
|
|
{ "GtkWidget", "vexpand", 0 },
|
2015-09-14 13:38:08 +00:00
|
|
|
|
{ "GtkVBox", "expand", 1 },
|
2015-05-02 11:35:35 +00:00
|
|
|
|
{ NULL, NULL, 0 }
|
|
|
|
|
};
|
|
|
|
|
gchar *canonical_name;
|
|
|
|
|
gboolean found;
|
|
|
|
|
gint k;
|
|
|
|
|
|
|
|
|
|
canonical_name = g_strdup (property_name);
|
|
|
|
|
g_strdelimit (canonical_name, "_", '-');
|
|
|
|
|
|
|
|
|
|
found = FALSE;
|
|
|
|
|
for (k = 0; props[k].class; k++)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp (class_name, props[k].class) == 0 &&
|
|
|
|
|
strcmp (canonical_name, props[k].property) == 0 &&
|
|
|
|
|
data->packing == props[k].packing)
|
|
|
|
|
{
|
|
|
|
|
found = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_free (canonical_name);
|
|
|
|
|
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-02 18:41:02 +00:00
|
|
|
|
static void
|
|
|
|
|
maybe_start_packing (MyParserData *data)
|
|
|
|
|
{
|
|
|
|
|
if (data->packing)
|
|
|
|
|
{
|
|
|
|
|
if (!data->packing_started)
|
|
|
|
|
{
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "%*s<packing>\n", data->indent, "");
|
2015-05-02 18:41:02 +00:00
|
|
|
|
data->indent += 2;
|
|
|
|
|
data->packing_started = TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
maybe_start_cell_packing (MyParserData *data)
|
|
|
|
|
{
|
|
|
|
|
if (data->cell_packing)
|
|
|
|
|
{
|
|
|
|
|
if (!data->cell_packing_started)
|
|
|
|
|
{
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "%*s<cell-packing>\n", data->indent, "");
|
2015-05-02 18:41:02 +00:00
|
|
|
|
data->indent += 2;
|
|
|
|
|
data->cell_packing_started = TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-02 18:39:35 +00:00
|
|
|
|
static void
|
|
|
|
|
maybe_start_child (MyParserData *data)
|
|
|
|
|
{
|
|
|
|
|
if (data->in_child > 0)
|
|
|
|
|
{
|
|
|
|
|
if (data->child_started < data->in_child)
|
|
|
|
|
{
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "%*s<child>\n", data->indent, "");
|
2015-05-02 18:39:35 +00:00
|
|
|
|
data->indent += 2;
|
|
|
|
|
data->child_started += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 01:03:52 +00:00
|
|
|
|
static void
|
2015-04-30 05:18:48 +00:00
|
|
|
|
maybe_emit_property (MyParserData *data)
|
2015-04-19 01:03:52 +00:00
|
|
|
|
{
|
|
|
|
|
gint i;
|
|
|
|
|
gboolean bound;
|
2015-05-02 17:15:18 +00:00
|
|
|
|
gboolean translatable;
|
2015-05-02 18:44:15 +00:00
|
|
|
|
gchar *escaped;
|
2015-09-09 20:47:02 +00:00
|
|
|
|
const gchar *class_name;
|
|
|
|
|
const gchar *property_name;
|
|
|
|
|
const gchar *value_string;
|
|
|
|
|
|
|
|
|
|
class_name = (const gchar *)data->classes->data;
|
|
|
|
|
property_name = "";
|
|
|
|
|
value_string = (const gchar *)data->value->str;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
2015-05-02 17:15:18 +00:00
|
|
|
|
bound = FALSE;
|
|
|
|
|
translatable = FALSE;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
for (i = 0; data->attribute_names[i]; i++)
|
|
|
|
|
{
|
2015-05-02 18:44:15 +00:00
|
|
|
|
if (strcmp (data->attribute_names[i], "bind-source") == 0 ||
|
|
|
|
|
strcmp (data->attribute_names[i], "bind_source") == 0)
|
|
|
|
|
bound = TRUE;
|
|
|
|
|
else if (strcmp (data->attribute_names[i], "translatable") == 0)
|
2015-05-02 17:15:18 +00:00
|
|
|
|
translatable = TRUE;
|
2015-09-09 20:50:49 +00:00
|
|
|
|
else if (strcmp (data->attribute_names[i], "name") == 0)
|
|
|
|
|
property_name = (const gchar *)data->attribute_values[i];
|
2015-05-02 17:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-09 20:50:49 +00:00
|
|
|
|
if (!translatable &&
|
|
|
|
|
!bound &&
|
|
|
|
|
!needs_explicit_setting (data, class_name, property_name))
|
2015-05-02 17:15:18 +00:00
|
|
|
|
{
|
|
|
|
|
for (i = 0; data->attribute_names[i]; i++)
|
2015-04-19 01:03:52 +00:00
|
|
|
|
{
|
2015-05-02 17:15:18 +00:00
|
|
|
|
if (strcmp (data->attribute_names[i], "name") == 0)
|
|
|
|
|
{
|
|
|
|
|
if (data->classes == NULL)
|
|
|
|
|
break;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
2015-09-09 20:47:02 +00:00
|
|
|
|
if (value_is_default (data, class_name, property_name, value_string))
|
2015-05-02 17:15:18 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2015-04-19 01:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-02 18:41:02 +00:00
|
|
|
|
maybe_start_packing (data);
|
|
|
|
|
maybe_start_cell_packing (data);
|
2015-05-02 16:13:35 +00:00
|
|
|
|
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "%*s<property", data->indent, "");
|
2015-04-19 01:03:52 +00:00
|
|
|
|
for (i = 0; data->attribute_names[i]; i++)
|
|
|
|
|
{
|
2015-05-02 18:44:15 +00:00
|
|
|
|
if (!translatable &&
|
2015-05-02 18:41:02 +00:00
|
|
|
|
(strcmp (data->attribute_names[i], "comments") == 0 ||
|
|
|
|
|
strcmp (data->attribute_names[i], "context") == 0))
|
2015-05-02 18:44:15 +00:00
|
|
|
|
continue;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
2015-09-14 03:24:05 +00:00
|
|
|
|
escaped = g_markup_escape_text (data->attribute_values[i], -1);
|
2016-05-06 17:42:30 +00:00
|
|
|
|
|
|
|
|
|
if (strcmp (data->attribute_names[i], "name") == 0)
|
|
|
|
|
canonicalize_key (escaped);
|
|
|
|
|
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, " %s=\"%s\"", data->attribute_names[i], escaped);
|
2015-09-14 03:24:05 +00:00
|
|
|
|
g_free (escaped);
|
2015-04-19 01:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bound)
|
|
|
|
|
{
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "/>\n");
|
2015-04-19 01:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, ">");
|
2015-09-09 20:47:02 +00:00
|
|
|
|
if (property_is_boolean (data, class_name, property_name))
|
|
|
|
|
{
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "%s", canonical_boolean_value (data, value_string));
|
2015-09-09 20:47:02 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
escaped = g_markup_escape_text (value_string, -1);
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "%s", escaped);
|
2015-09-09 20:47:02 +00:00
|
|
|
|
g_free (escaped);
|
|
|
|
|
}
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "</property>\n");
|
2015-04-19 01:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2015-04-30 05:18:48 +00:00
|
|
|
|
maybe_close_starttag (MyParserData *data)
|
2015-04-19 01:03:52 +00:00
|
|
|
|
{
|
|
|
|
|
if (data->unclosed_starttag)
|
|
|
|
|
{
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, ">\n");
|
2015-04-19 01:03:52 +00:00
|
|
|
|
data->unclosed_starttag = FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
stack_is (GMarkupParseContext *context,
|
|
|
|
|
...)
|
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
gchar *s, *p;
|
|
|
|
|
const GSList *stack;
|
|
|
|
|
|
|
|
|
|
stack = g_markup_parse_context_get_element_stack (context);
|
|
|
|
|
|
|
|
|
|
va_start (args, context);
|
|
|
|
|
s = va_arg (args, gchar *);
|
|
|
|
|
while (s)
|
|
|
|
|
{
|
|
|
|
|
if (stack == NULL)
|
|
|
|
|
{
|
|
|
|
|
va_end (args);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = (gchar *)stack->data;
|
|
|
|
|
if (strcmp (s, p) != 0)
|
|
|
|
|
{
|
|
|
|
|
va_end (args);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s = va_arg (args, gchar *);
|
|
|
|
|
stack = stack->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
va_end (args);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
start_element (GMarkupParseContext *context,
|
|
|
|
|
const gchar *element_name,
|
|
|
|
|
const gchar **attribute_names,
|
|
|
|
|
const gchar **attribute_values,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
gint i;
|
|
|
|
|
gchar *escaped;
|
2015-04-30 05:18:48 +00:00
|
|
|
|
MyParserData *data = user_data;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
|
|
|
|
maybe_close_starttag (data);
|
|
|
|
|
|
|
|
|
|
if (strcmp (element_name, "property") == 0)
|
|
|
|
|
{
|
|
|
|
|
g_assert (data->attribute_names == NULL);
|
|
|
|
|
g_assert (data->attribute_values == NULL);
|
|
|
|
|
g_assert (data->value == NULL);
|
|
|
|
|
|
|
|
|
|
data->attribute_names = g_strdupv ((gchar **)attribute_names);
|
|
|
|
|
data->attribute_values = g_strdupv ((gchar **)attribute_values);
|
|
|
|
|
data->value = g_string_new ("");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp (element_name, "packing") == 0)
|
|
|
|
|
{
|
|
|
|
|
data->packing = TRUE;
|
|
|
|
|
data->packing_started = FALSE;
|
|
|
|
|
|
2015-05-02 16:13:35 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp (element_name, "cell-packing") == 0)
|
|
|
|
|
{
|
|
|
|
|
data->cell_packing = TRUE;
|
|
|
|
|
data->cell_packing_started = FALSE;
|
|
|
|
|
|
2015-04-19 01:03:52 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2015-05-02 18:39:35 +00:00
|
|
|
|
else if (strcmp (element_name, "child") == 0)
|
|
|
|
|
{
|
|
|
|
|
data->in_child += 1;
|
|
|
|
|
|
|
|
|
|
if (attribute_names[0] == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
data->child_started += 1;
|
|
|
|
|
}
|
2015-04-19 01:03:52 +00:00
|
|
|
|
else if (strcmp (element_name, "attribute") == 0)
|
|
|
|
|
{
|
|
|
|
|
/* attribute in label has no content */
|
|
|
|
|
if (data->classes == NULL ||
|
|
|
|
|
strcmp ((gchar *)data->classes->data, "GtkLabel") != 0)
|
|
|
|
|
data->value = g_string_new ("");
|
|
|
|
|
}
|
|
|
|
|
else if (stack_is (context, "item", "items", NULL) ||
|
|
|
|
|
stack_is (context, "action-widget", "action-widgets", NULL) ||
|
|
|
|
|
stack_is (context, "mime-type", "mime-types", NULL) ||
|
|
|
|
|
stack_is (context, "pattern", "patterns", NULL) ||
|
|
|
|
|
stack_is (context, "application", "applications", NULL) ||
|
|
|
|
|
stack_is (context, "col", "row", "data", NULL) ||
|
|
|
|
|
stack_is (context, "mark", "marks", NULL) ||
|
|
|
|
|
stack_is (context, "action", "accessibility", NULL))
|
|
|
|
|
{
|
|
|
|
|
data->value = g_string_new ("");
|
|
|
|
|
}
|
2015-05-02 18:39:35 +00:00
|
|
|
|
else if (strcmp (element_name, "placeholder") == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-04-19 01:03:52 +00:00
|
|
|
|
else if (strcmp (element_name, "object") == 0 ||
|
|
|
|
|
strcmp (element_name, "template") == 0)
|
|
|
|
|
{
|
2015-05-02 18:39:35 +00:00
|
|
|
|
maybe_start_child (data);
|
|
|
|
|
|
2015-04-19 01:03:52 +00:00
|
|
|
|
for (i = 0; attribute_names[i]; i++)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp (attribute_names[i], "class") == 0)
|
|
|
|
|
{
|
|
|
|
|
data->classes = g_list_prepend (data->classes,
|
|
|
|
|
g_strdup (attribute_values[i]));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "%*s<%s", data->indent, "", element_name);
|
2015-04-19 01:03:52 +00:00
|
|
|
|
for (i = 0; attribute_names[i]; i++)
|
|
|
|
|
{
|
|
|
|
|
escaped = g_markup_escape_text (attribute_values[i], -1);
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, " %s=\"%s\"", attribute_names[i], escaped);
|
2015-04-19 01:03:52 +00:00
|
|
|
|
g_free (escaped);
|
|
|
|
|
}
|
|
|
|
|
data->unclosed_starttag = TRUE;
|
|
|
|
|
data->indent += 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
end_element (GMarkupParseContext *context,
|
|
|
|
|
const gchar *element_name,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
2015-04-30 05:18:48 +00:00
|
|
|
|
MyParserData *data = user_data;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
|
|
|
|
if (strcmp (element_name, "property") == 0)
|
|
|
|
|
{
|
|
|
|
|
maybe_emit_property (data);
|
|
|
|
|
|
|
|
|
|
g_clear_pointer (&data->attribute_names, g_strfreev);
|
|
|
|
|
g_clear_pointer (&data->attribute_values, g_strfreev);
|
|
|
|
|
g_string_free (data->value, TRUE);
|
|
|
|
|
data->value = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp (element_name, "packing") == 0)
|
|
|
|
|
{
|
|
|
|
|
data->packing = FALSE;
|
|
|
|
|
if (!data->packing_started)
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-05-02 16:13:35 +00:00
|
|
|
|
else if (strcmp (element_name, "cell-packing") == 0)
|
|
|
|
|
{
|
|
|
|
|
data->cell_packing = FALSE;
|
|
|
|
|
if (!data->cell_packing_started)
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-05-02 18:39:35 +00:00
|
|
|
|
else if (strcmp (element_name, "child") == 0)
|
|
|
|
|
{
|
|
|
|
|
data->in_child -= 1;
|
|
|
|
|
if (data->child_started == data->in_child)
|
|
|
|
|
return;
|
|
|
|
|
data->child_started -= 1;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp (element_name, "placeholder") == 0)
|
2015-04-19 01:03:52 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp (element_name, "object") == 0 ||
|
|
|
|
|
strcmp (element_name, "template") == 0)
|
|
|
|
|
{
|
|
|
|
|
g_free (data->classes->data);
|
|
|
|
|
data->classes = g_list_delete_link (data->classes, data->classes);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-02 18:39:35 +00:00
|
|
|
|
if (data->value != NULL)
|
|
|
|
|
{
|
|
|
|
|
gchar *escaped;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
2015-05-02 18:39:35 +00:00
|
|
|
|
if (data->unclosed_starttag)
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, ">");
|
2015-05-02 18:39:35 +00:00
|
|
|
|
|
|
|
|
|
escaped = g_markup_escape_text (data->value->str, -1);
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "%s</%s>\n", escaped, element_name);
|
2015-05-02 18:39:35 +00:00
|
|
|
|
g_free (escaped);
|
|
|
|
|
|
|
|
|
|
g_string_free (data->value, TRUE);
|
|
|
|
|
data->value = NULL;
|
|
|
|
|
}
|
2015-04-19 01:03:52 +00:00
|
|
|
|
else
|
2015-05-02 18:39:35 +00:00
|
|
|
|
{
|
|
|
|
|
if (data->unclosed_starttag)
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "/>\n");
|
2015-05-02 18:39:35 +00:00
|
|
|
|
else
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "%*s</%s>\n", data->indent - 2, "", element_name);
|
2015-05-02 18:39:35 +00:00
|
|
|
|
}
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
2015-05-02 18:39:35 +00:00
|
|
|
|
data->indent -= 2;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
data->unclosed_starttag = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
text (GMarkupParseContext *context,
|
|
|
|
|
const gchar *text,
|
|
|
|
|
gsize text_len,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
2015-04-30 05:18:48 +00:00
|
|
|
|
MyParserData *data = user_data;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
|
|
|
|
if (data->value)
|
|
|
|
|
{
|
|
|
|
|
g_string_append_len (data->value, text, text_len);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
passthrough (GMarkupParseContext *context,
|
|
|
|
|
const gchar *text,
|
|
|
|
|
gsize text_len,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
2015-04-30 05:18:48 +00:00
|
|
|
|
MyParserData *data = user_data;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
|
|
|
|
maybe_close_starttag (data);
|
|
|
|
|
|
2016-11-13 03:29:02 +00:00
|
|
|
|
g_fprintf (data->output, "%*s%s\n", data->indent, "", text);
|
2015-04-19 01:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GMarkupParser parser = {
|
|
|
|
|
start_element,
|
|
|
|
|
end_element,
|
|
|
|
|
text,
|
|
|
|
|
passthrough,
|
|
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-28 03:31:03 +00:00
|
|
|
|
static void
|
2016-11-13 03:29:02 +00:00
|
|
|
|
do_simplify (int *argc,
|
|
|
|
|
const char ***argv)
|
2015-04-19 01:03:52 +00:00
|
|
|
|
{
|
|
|
|
|
GMarkupParseContext *context;
|
|
|
|
|
gchar *buffer;
|
2015-04-30 05:18:48 +00:00
|
|
|
|
MyParserData data;
|
2016-11-13 03:29:02 +00:00
|
|
|
|
gboolean replace = FALSE;
|
|
|
|
|
char **filenames = NULL;
|
|
|
|
|
GOptionContext *ctx;
|
|
|
|
|
const GOptionEntry entries[] = {
|
|
|
|
|
{ "replace", 0, 0, G_OPTION_ARG_NONE, &replace, NULL, NULL },
|
|
|
|
|
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, NULL },
|
|
|
|
|
{ NULL, }
|
|
|
|
|
};
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
ctx = g_option_context_new (NULL);
|
|
|
|
|
g_option_context_set_help_enabled (ctx, FALSE);
|
|
|
|
|
g_option_context_add_main_entries (ctx, entries, NULL);
|
|
|
|
|
|
|
|
|
|
if (!g_option_context_parse (ctx, argc, (char ***)argv, &error))
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_option_context_free (ctx);
|
|
|
|
|
|
|
|
|
|
if (filenames == NULL)
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("No .ui file specified\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (g_strv_length (filenames) > 1)
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("Can only simplify a single .ui file\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.input_filename = filenames[0];
|
|
|
|
|
data.output_filename = NULL;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
|
2016-11-13 03:29:02 +00:00
|
|
|
|
if (replace)
|
|
|
|
|
{
|
|
|
|
|
int fd;
|
|
|
|
|
fd = g_file_open_tmp ("gtk-builder-tool-XXXXXX", &data.output_filename, NULL);
|
|
|
|
|
data.output = fdopen (fd, "w");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
data.output = stdout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!g_file_get_contents (filenames[0], &buffer, NULL, &error))
|
2015-04-19 01:03:52 +00:00
|
|
|
|
{
|
2016-10-03 16:20:03 +00:00
|
|
|
|
g_printerr (_("Can’t load file: %s\n"), error->message);
|
2015-04-28 03:31:03 +00:00
|
|
|
|
exit (1);
|
2015-04-19 01:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.builder = gtk_builder_new ();
|
|
|
|
|
data.classes = NULL;
|
|
|
|
|
data.attribute_names = NULL;
|
|
|
|
|
data.attribute_values = NULL;
|
|
|
|
|
data.value = NULL;
|
|
|
|
|
data.packing = FALSE;
|
|
|
|
|
data.packing_started = FALSE;
|
2015-05-02 16:13:35 +00:00
|
|
|
|
data.cell_packing = FALSE;
|
|
|
|
|
data.cell_packing_started = FALSE;
|
2015-05-02 18:39:35 +00:00
|
|
|
|
data.in_child = 0;
|
|
|
|
|
data.child_started = 0;
|
2015-04-19 01:03:52 +00:00
|
|
|
|
data.unclosed_starttag = FALSE;
|
|
|
|
|
data.indent = 0;
|
|
|
|
|
|
|
|
|
|
context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, &data, NULL);
|
|
|
|
|
if (!g_markup_parse_context_parse (context, buffer, -1, &error))
|
|
|
|
|
{
|
2016-10-03 16:20:03 +00:00
|
|
|
|
g_printerr (_("Can’t parse file: %s\n"), error->message);
|
2015-04-28 03:31:03 +00:00
|
|
|
|
exit (1);
|
2015-04-27 22:25:09 +00:00
|
|
|
|
}
|
2016-11-13 03:29:02 +00:00
|
|
|
|
|
|
|
|
|
fclose (data.output);
|
|
|
|
|
|
|
|
|
|
if (data.output_filename)
|
|
|
|
|
{
|
|
|
|
|
char *content;
|
|
|
|
|
gsize length;
|
|
|
|
|
|
|
|
|
|
if (!g_file_get_contents (data.output_filename, &content, &length, &error))
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("Failed to read %s: %s\n", data.output_filename, error->message);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!g_file_set_contents (data.input_filename, content, length, &error))
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("Failed to write %s: %s\n", data.input_filename, error->message);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-27 22:25:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-30 05:18:48 +00:00
|
|
|
|
static GType
|
|
|
|
|
make_fake_type (const gchar *type_name,
|
|
|
|
|
const gchar *parent_name)
|
|
|
|
|
{
|
|
|
|
|
GType parent_type;
|
|
|
|
|
GTypeQuery query;
|
|
|
|
|
|
|
|
|
|
parent_type = g_type_from_name (parent_name);
|
|
|
|
|
if (parent_type == G_TYPE_INVALID)
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("Failed to lookup template parent type %s\n", parent_name);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_type_query (parent_type, &query);
|
|
|
|
|
return g_type_register_static_simple (parent_type,
|
|
|
|
|
type_name,
|
|
|
|
|
query.class_size,
|
|
|
|
|
NULL,
|
|
|
|
|
query.instance_size,
|
|
|
|
|
NULL,
|
|
|
|
|
0);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-28 03:31:03 +00:00
|
|
|
|
static void
|
2015-04-30 05:18:48 +00:00
|
|
|
|
do_validate_template (const gchar *filename,
|
|
|
|
|
const gchar *type_name,
|
|
|
|
|
const gchar *parent_name)
|
2015-04-27 22:25:09 +00:00
|
|
|
|
{
|
2015-04-30 05:18:48 +00:00
|
|
|
|
GType template_type;
|
|
|
|
|
GtkWidget *widget;
|
2015-04-27 22:25:09 +00:00
|
|
|
|
GtkBuilder *builder;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gint ret;
|
|
|
|
|
|
2015-04-30 05:18:48 +00:00
|
|
|
|
/* Only make a fake type if it doesn't exist yet.
|
|
|
|
|
* This lets us e.g. validate the GtkFileChooserWidget template.
|
|
|
|
|
*/
|
|
|
|
|
template_type = g_type_from_name (type_name);
|
|
|
|
|
if (template_type == G_TYPE_INVALID)
|
|
|
|
|
template_type = make_fake_type (type_name, parent_name);
|
|
|
|
|
|
|
|
|
|
widget = g_object_new (template_type, NULL);
|
|
|
|
|
if (!widget)
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("Failed to create an instance of the template type %s\n", type_name);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-27 22:25:09 +00:00
|
|
|
|
builder = gtk_builder_new ();
|
2015-04-30 05:18:48 +00:00
|
|
|
|
ret = gtk_builder_extend_with_template (builder, widget, template_type, " ", 1, &error);
|
|
|
|
|
if (ret)
|
|
|
|
|
ret = gtk_builder_add_from_file (builder, filename, &error);
|
2015-04-27 22:25:09 +00:00
|
|
|
|
g_object_unref (builder);
|
|
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("%s\n", error->message);
|
2015-04-28 03:31:03 +00:00
|
|
|
|
exit (1);
|
2015-04-19 01:03:52 +00:00
|
|
|
|
}
|
2015-04-27 22:25:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-30 05:18:48 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
parse_template_error (const gchar *message,
|
|
|
|
|
gchar **class_name,
|
|
|
|
|
gchar **parent_name)
|
|
|
|
|
{
|
|
|
|
|
gchar *p;
|
|
|
|
|
|
|
|
|
|
if (!strstr (message, "Not expecting to handle a template"))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
p = strstr (message, "(class '");
|
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
*class_name = g_strdup (p + strlen ("(class '"));
|
|
|
|
|
p = strstr (*class_name, "'");
|
|
|
|
|
if (p)
|
|
|
|
|
*p = '\0';
|
|
|
|
|
}
|
|
|
|
|
p = strstr (message, ", parent '");
|
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
*parent_name = g_strdup (p + strlen (", parent '"));
|
|
|
|
|
p = strstr (*parent_name, "'");
|
|
|
|
|
if (p)
|
|
|
|
|
*p = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
do_validate (const gchar *filename)
|
|
|
|
|
{
|
|
|
|
|
GtkBuilder *builder;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gint ret;
|
|
|
|
|
gchar *class_name = NULL;
|
|
|
|
|
gchar *parent_name = NULL;
|
|
|
|
|
|
|
|
|
|
builder = gtk_builder_new ();
|
|
|
|
|
ret = gtk_builder_add_from_file (builder, filename, &error);
|
|
|
|
|
g_object_unref (builder);
|
|
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
|
{
|
|
|
|
|
if (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_UNHANDLED_TAG) &&
|
|
|
|
|
parse_template_error (error->message, &class_name, &parent_name))
|
|
|
|
|
{
|
|
|
|
|
do_validate_template (filename, class_name, parent_name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-30 11:11:49 +00:00
|
|
|
|
static const gchar *
|
|
|
|
|
object_get_name (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
if (GTK_IS_BUILDABLE (object))
|
|
|
|
|
return gtk_buildable_get_name (GTK_BUILDABLE (object));
|
|
|
|
|
else
|
|
|
|
|
return g_object_get_data (object, "gtk-builder-name");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
do_enumerate (const gchar *filename)
|
|
|
|
|
{
|
|
|
|
|
GtkBuilder *builder;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gint ret;
|
|
|
|
|
GSList *list, *l;
|
|
|
|
|
GObject *object;
|
|
|
|
|
const gchar *name;
|
|
|
|
|
|
|
|
|
|
builder = gtk_builder_new ();
|
|
|
|
|
ret = gtk_builder_add_from_file (builder, filename, &error);
|
|
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list = gtk_builder_get_objects (builder);
|
|
|
|
|
for (l = list; l; l = l->next)
|
|
|
|
|
{
|
|
|
|
|
object = l->data;
|
|
|
|
|
name = object_get_name (object);
|
|
|
|
|
if (g_str_has_prefix (name, "___") && g_str_has_suffix (name, "___"))
|
|
|
|
|
continue;
|
|
|
|
|
|
2016-04-15 17:37:49 +00:00
|
|
|
|
g_printf ("%s (%s)\n", name, g_type_name_from_instance ((GTypeInstance*)object));
|
2015-04-30 11:11:49 +00:00
|
|
|
|
}
|
|
|
|
|
g_slist_free (list);
|
|
|
|
|
|
|
|
|
|
g_object_unref (builder);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-29 14:23:41 +00:00
|
|
|
|
static void
|
|
|
|
|
set_window_title (GtkWindow *window,
|
|
|
|
|
const char *filename,
|
|
|
|
|
const char *id)
|
|
|
|
|
{
|
|
|
|
|
gchar *name;
|
|
|
|
|
gchar *title;
|
|
|
|
|
|
|
|
|
|
name = g_path_get_basename (filename);
|
|
|
|
|
|
|
|
|
|
if (id)
|
|
|
|
|
title = g_strdup_printf ("%s in %s", id, name);
|
|
|
|
|
else
|
|
|
|
|
title = g_strdup (name);
|
|
|
|
|
|
|
|
|
|
gtk_window_set_title (window, title);
|
|
|
|
|
|
|
|
|
|
g_free (title);
|
|
|
|
|
g_free (name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-01-31 17:57:50 +00:00
|
|
|
|
preview_file (const char *filename,
|
|
|
|
|
const char *id,
|
|
|
|
|
const char *cssfile)
|
2016-01-29 14:23:41 +00:00
|
|
|
|
{
|
|
|
|
|
GtkBuilder *builder;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
GObject *object;
|
|
|
|
|
GtkWidget *window;
|
|
|
|
|
|
2016-01-31 17:57:50 +00:00
|
|
|
|
if (cssfile)
|
|
|
|
|
{
|
|
|
|
|
GtkCssProvider *provider;
|
|
|
|
|
|
|
|
|
|
provider = gtk_css_provider_new ();
|
2016-10-17 14:09:12 +00:00
|
|
|
|
gtk_css_provider_load_from_path (provider, cssfile);
|
2016-01-31 17:57:50 +00:00
|
|
|
|
|
|
|
|
|
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
|
|
|
|
|
GTK_STYLE_PROVIDER (provider),
|
|
|
|
|
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-29 14:23:41 +00:00
|
|
|
|
builder = gtk_builder_new ();
|
|
|
|
|
if (!gtk_builder_add_from_file (builder, filename, &error))
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object = NULL;
|
|
|
|
|
|
|
|
|
|
if (id)
|
|
|
|
|
{
|
|
|
|
|
object = gtk_builder_get_object (builder, id);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GSList *objects, *l;
|
|
|
|
|
|
|
|
|
|
objects = gtk_builder_get_objects (builder);
|
|
|
|
|
for (l = objects; l; l = l->next)
|
|
|
|
|
{
|
|
|
|
|
GObject *obj = l->data;
|
|
|
|
|
|
|
|
|
|
if (GTK_IS_WINDOW (obj))
|
|
|
|
|
{
|
|
|
|
|
object = obj;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (GTK_IS_WIDGET (obj))
|
|
|
|
|
{
|
|
|
|
|
if (object == NULL)
|
|
|
|
|
object = obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g_slist_free (objects);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (object == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (id)
|
|
|
|
|
g_printerr ("No object with ID '%s' found\n", id);
|
|
|
|
|
else
|
|
|
|
|
g_printerr ("No previewable object found\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!GTK_IS_WIDGET (object))
|
|
|
|
|
{
|
|
|
|
|
g_printerr ("Objects of type %s can't be previewed\n", G_OBJECT_TYPE_NAME (object));
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GTK_IS_WINDOW (object))
|
|
|
|
|
window = GTK_WIDGET (object);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GtkWidget *widget = GTK_WIDGET (object);
|
|
|
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
|
|
|
|
|
|
if (GTK_IS_BUILDABLE (object))
|
|
|
|
|
id = gtk_buildable_get_name (GTK_BUILDABLE (object));
|
|
|
|
|
|
|
|
|
|
set_window_title (GTK_WINDOW (window), filename, id);
|
|
|
|
|
|
|
|
|
|
g_object_ref (widget);
|
|
|
|
|
if (gtk_widget_get_parent (widget) != NULL)
|
|
|
|
|
gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), widget);
|
|
|
|
|
g_object_unref (widget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gtk_window_present (GTK_WINDOW (window));
|
|
|
|
|
|
|
|
|
|
gtk_main ();
|
|
|
|
|
|
|
|
|
|
g_object_unref (builder);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-31 17:57:50 +00:00
|
|
|
|
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 },
|
2016-11-13 03:29:02 +00:00
|
|
|
|
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, NULL },
|
|
|
|
|
{ NULL, }
|
2016-01-31 17:57:50 +00:00
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-27 22:25:09 +00:00
|
|
|
|
static void
|
|
|
|
|
usage (void)
|
|
|
|
|
{
|
2015-04-28 03:31:03 +00:00
|
|
|
|
g_print (_("Usage:\n"
|
|
|
|
|
" gtk-builder-tool [COMMAND] FILE\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Commands:\n"
|
2016-01-31 17:57:50 +00:00
|
|
|
|
" validate Validate the file\n"
|
2016-11-13 03:29:02 +00:00
|
|
|
|
" simplify [OPTIONS] Simplify the file\n"
|
2016-01-31 17:57:50 +00:00
|
|
|
|
" enumerate List all named objects\n"
|
|
|
|
|
" preview [OPTIONS] Preview the file\n"
|
|
|
|
|
"\n"
|
2016-11-13 03:29:02 +00:00
|
|
|
|
"Simplify Options:\n"
|
|
|
|
|
" --replace Replace the file\n"
|
|
|
|
|
"\n"
|
2016-01-31 17:57:50 +00:00
|
|
|
|
"Preview Options:\n"
|
|
|
|
|
" --id=ID Preview only the named object\n"
|
|
|
|
|
" --css=FILE Use style from CSS file\n"
|
2015-04-28 03:31:03 +00:00
|
|
|
|
"\n"
|
2015-04-30 11:11:49 +00:00
|
|
|
|
"Perform various tasks on GtkBuilder .ui files.\n"));
|
2015-04-27 22:25:09 +00:00
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2016-01-31 17:57:50 +00:00
|
|
|
|
main (int argc, const char *argv[])
|
2015-04-27 22:25:09 +00:00
|
|
|
|
{
|
2015-04-27 22:39:22 +00:00
|
|
|
|
g_set_prgname ("gtk-builder-tool");
|
|
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
|
gtk_init ();
|
2015-04-27 22:25:09 +00:00
|
|
|
|
|
|
|
|
|
gtk_test_register_all_types ();
|
|
|
|
|
|
2015-04-28 03:31:03 +00:00
|
|
|
|
if (argc < 3)
|
2015-04-27 22:25:09 +00:00
|
|
|
|
usage ();
|
|
|
|
|
|
2016-01-31 16:01:46 +00:00
|
|
|
|
if (strcmp (argv[2], "--help") == 0)
|
|
|
|
|
usage ();
|
|
|
|
|
|
2016-01-31 17:57:50 +00:00
|
|
|
|
argv++;
|
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
|
|
if (strcmp (argv[0], "validate") == 0)
|
|
|
|
|
do_validate (argv[1]);
|
|
|
|
|
else if (strcmp (argv[0], "simplify") == 0)
|
2016-11-13 03:29:02 +00:00
|
|
|
|
do_simplify (&argc, &argv);
|
2016-01-31 17:57:50 +00:00
|
|
|
|
else if (strcmp (argv[0], "enumerate") == 0)
|
|
|
|
|
do_enumerate (argv[1]);
|
|
|
|
|
else if (strcmp (argv[0], "preview") == 0)
|
|
|
|
|
do_preview (&argc, &argv);
|
2015-04-28 03:31:03 +00:00
|
|
|
|
else
|
|
|
|
|
usage ();
|
2015-04-27 22:25:09 +00:00
|
|
|
|
|
2015-04-19 01:03:52 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|