2019-02-07 09:12:09 +00:00
|
|
|
/* Copyright 2015 Red Hat, Inc.
|
|
|
|
*
|
2023-07-21 11:05:25 +00:00
|
|
|
* GTK is free software; you can redistribute it and/or modify it
|
2019-02-07 09:12:09 +00:00
|
|
|
* 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
|
2023-07-21 11:05:25 +00:00
|
|
|
* License along with GTK; see the file COPYING. If not,
|
2019-02-07 09:12:09 +00:00
|
|
|
* see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Matthias Clasen
|
|
|
|
*/
|
|
|
|
|
2022-04-17 15:59:06 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2019-02-07 09:12:09 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2023-03-18 02:04:25 +00:00
|
|
|
#include <glib/gi18n-lib.h>
|
2019-02-07 09:12:09 +00:00
|
|
|
#include <glib/gprintf.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include "gtkbuilderprivate.h"
|
2020-05-18 10:04:15 +00:00
|
|
|
#include "gtk-builder-tool.h"
|
2022-10-19 16:29:39 +00:00
|
|
|
#include "fake-scope.h"
|
2019-02-07 09:12:09 +00:00
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
static const char *
|
2020-09-25 14:43:24 +00:00
|
|
|
object_get_id (GObject *object)
|
2019-02-07 09:12:09 +00:00
|
|
|
{
|
|
|
|
if (GTK_IS_BUILDABLE (object))
|
2020-09-25 14:43:24 +00:00
|
|
|
return gtk_buildable_get_buildable_id (GTK_BUILDABLE (object));
|
2019-02-07 09:12:09 +00:00
|
|
|
else
|
2020-09-25 14:43:24 +00:00
|
|
|
return g_object_get_data (object, "gtk-builder-id");
|
2019-02-07 09:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-05-18 10:04:15 +00:00
|
|
|
do_enumerate (int *argc, const char ***argv)
|
2019-02-07 09:12:09 +00:00
|
|
|
{
|
2022-10-19 16:29:39 +00:00
|
|
|
FakeScope *scope;
|
2019-02-07 09:12:09 +00:00
|
|
|
GtkBuilder *builder;
|
|
|
|
GError *error = NULL;
|
2020-07-24 13:54:49 +00:00
|
|
|
int ret;
|
2019-02-07 09:12:09 +00:00
|
|
|
GSList *list, *l;
|
2022-10-19 16:29:39 +00:00
|
|
|
gboolean callbacks = FALSE;
|
2022-04-17 15:59:06 +00:00
|
|
|
char **filenames = NULL;
|
|
|
|
GOptionContext *context;
|
|
|
|
const GOptionEntry entries[] = {
|
2022-10-19 16:29:39 +00:00
|
|
|
{ "callbacks", 0, 0, G_OPTION_ARG_NONE, &callbacks, "Also print callbacks", NULL },
|
2022-04-17 15:59:06 +00:00
|
|
|
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
|
|
|
|
{ NULL, }
|
|
|
|
};
|
|
|
|
|
|
|
|
g_set_prgname ("gtk4-builder-tool enumerate");
|
|
|
|
context = g_option_context_new (NULL);
|
|
|
|
g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
|
|
|
|
g_option_context_add_main_entries (context, entries, NULL);
|
2022-10-19 16:29:39 +00:00
|
|
|
g_option_context_set_summary (context, _("Print all named objects."));
|
2022-04-17 15:59:06 +00:00
|
|
|
|
|
|
|
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);
|
2019-02-07 09:12:09 +00:00
|
|
|
|
2022-04-17 15:59:06 +00:00
|
|
|
if (filenames == NULL)
|
|
|
|
{
|
|
|
|
g_printerr ("No .ui file specified\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_strv_length (filenames) > 1)
|
|
|
|
{
|
|
|
|
g_printerr ("Can only enumerate a single .ui file\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2019-02-07 09:12:09 +00:00
|
|
|
|
|
|
|
builder = gtk_builder_new ();
|
2022-10-19 16:29:39 +00:00
|
|
|
scope = fake_scope_new ();
|
|
|
|
gtk_builder_set_scope (builder, GTK_BUILDER_SCOPE (scope));
|
|
|
|
|
2022-04-17 15:59:06 +00:00
|
|
|
ret = gtk_builder_add_from_file (builder, filenames[0], &error);
|
2019-02-07 09:12:09 +00:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2022-10-19 16:29:39 +00:00
|
|
|
if (callbacks)
|
|
|
|
g_print ("Objects:\n");
|
|
|
|
|
2019-02-07 09:12:09 +00:00
|
|
|
list = gtk_builder_get_objects (builder);
|
|
|
|
for (l = list; l; l = l->next)
|
|
|
|
{
|
2022-10-19 16:29:39 +00:00
|
|
|
GObject *object = l->data;
|
|
|
|
const char *name = object_get_id (object);
|
2019-02-07 09:12:09 +00:00
|
|
|
if (g_str_has_prefix (name, "___") && g_str_has_suffix (name, "___"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
g_printf ("%s (%s)\n", name, g_type_name_from_instance ((GTypeInstance*)object));
|
|
|
|
}
|
|
|
|
g_slist_free (list);
|
|
|
|
|
2022-10-19 16:29:39 +00:00
|
|
|
if (callbacks)
|
|
|
|
{
|
|
|
|
GPtrArray *names;
|
|
|
|
gboolean need_prefix = TRUE;
|
|
|
|
|
|
|
|
names = fake_scope_get_callbacks (scope);
|
|
|
|
for (int i = 0; i < names->len; i++)
|
|
|
|
{
|
|
|
|
const char *name = g_ptr_array_index (names, i);
|
|
|
|
|
|
|
|
if (need_prefix)
|
|
|
|
{
|
|
|
|
need_prefix = FALSE;
|
|
|
|
g_print ("\nCallbacks:\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_print ("%s\n", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (scope);
|
2019-02-07 09:12:09 +00:00
|
|
|
g_object_unref (builder);
|
2022-04-17 15:59:06 +00:00
|
|
|
|
|
|
|
g_strfreev (filenames);
|
2019-02-07 09:12:09 +00:00
|
|
|
}
|