Commit 3d1a0703 authored by Jente Hidskes's avatar Jente Hidskes

Use resource files to define UI and menus

parent 9b7660fd
......@@ -19,6 +19,9 @@ AC_PROG_CC_STDC
LT_PREREQ(2.2.6)
LT_INIT(disable-static)
# Resources
AC_PATH_PROG(GLIB_COMPILE_RESOURCES, glib-compile-resources)
# Internationalization
IT_PROG_INTLTOOL(0.50.1)
AM_GLIB_GNU_GETTEXT
......
......@@ -6,11 +6,27 @@ desktop_in_files = gcolor3.desktop.in.in
desktopdir = $(datadir)/applications
desktop_DATA = $(desktop_in_files:.desktop.in.in=.desktop)
# Resource files
GRESDEPS = $1 $(shell $(GLIB_COMPILE_RESOURCES) --generate-dependencies --sourcedir $(dir $1) $1)
GRESGEN = $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) --target="$@" --sourcedir="$(dir $<)" --generate-source "$<"
gcolor3_resource_deps = $(call GRESDEPS,gcolor3.gresource.xml)
gcolor3-resources.c: $(gcolor3_resource_deps)
$(GRESGEN)
BUILT_SOURCES = gcolor3-resources.c
EXTRA_DIST = \
$(desktop_in_files)
$(desktop_in_files) \
gcolor3.gresource.xml \
$(gcolor3_resource_deps)
CLEANFILES = \
$(BUILT_SOURCES)
DISTCLEANFILES = \
$(desktop_DATA)
$(desktop_DATA) \
$(BUILT_SOURCES)
-include $(top_srcdir)/git.mk
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/unia/gcolor3/">
<!-- Using this alias, GtkApplication will automatically pick it up for us. -->
<file alias="gtk/menus.ui" preprocess="xml-stripblanks">menus.ui</file>
<file alias="window.ui" preprocess="xml-stripblanks">window.ui</file>
</gresource>
</gresources>
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<menu id="app-menu">
<section>
<item>
<attribute name="action">app.about</attribute>
<attribute name="label" translatable="yes">_About</attribute>
</item>
<item>
<attribute name="action">app.quit</attribute>
<attribute name="label" translatable="yes">_Quit</attribute>
<attribute name="accel">&lt;Primary&gt;q</attribute>
</item>
</section>
</menu>
</interface>
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
<requires lib="gtk+" version="3.16"/>
<object class="GtkImage" id="button_startup">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">document-save-symbolic</property>
</object>
<object class="GtkListStore" id="liststore">
<columns>
<!-- column-name pixbuf -->
<column type="GdkPixbuf"/>
<!-- column-name column1 -->
<column type="gchararray"/>
<!-- column-name text -->
<column type="gchararray"/>
</columns>
</object>
<template class="Gcolor3Window" parent="GtkApplicationWindow">
<property name="width_request">697</property>
<property name="height_request">274</property>
<property name="can_focus">False</property>
<property name="border_width">6</property>
<property name="resizable">False</property>
<property name="window_position">center</property>
<child>
<object class="GtkStack" id="stack">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="transition_type">slide-left-right</property>
<signal name="notify::visible-child" handler="gcolor3_window_stack_changed" object="Gcolor3Window" swapped="no"/>
<child>
<object class="GtkColorSelection" id="picker">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<signal name="color-changed" handler="gcolor3_window_picker_changed" object="Gcolor3Window" swapped="no"/>
</object>
<packing>
<property name="name">picker</property>
<property name="title" translatable="yes">Picker</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scroll">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="vexpand">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="tree">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">liststore</property>
<property name="search_column">2</property>
<property name="show_expanders">False</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="selection">
<signal name="changed" handler="gcolor3_window_selection_changed" object="Gcolor3Window" swapped="no"/>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="name">saved-colors</property>
<property name="title" translatable="yes">Saved Colors</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<child type="titlebar">
<object class="GtkHeaderBar" id="headerbar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="has_subtitle">False</property>
<property name="show_close_button">True</property>
<child>
<object class="GtkStackSwitcher" id="switcher">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stack">stack</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="can_default">True</property>
<property name="has_default">True</property>
<property name="receives_default">False</property>
<property name="action_name">win.save</property>
<property name="image">button_startup</property>
<property name="always_show_image">True</property>
</object>
<packing>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkRevealer" id="revealer">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="transition_type">slide-left</property>
<property name="transition_duration">400</property>
<property name="reveal_child">True</property>
<child>
<object class="GtkEntry" id="entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="activates_default">True</property>
<property name="placeholder_text" translatable="yes">Color name...</property>
</object>
</child>
</object>
<packing>
<property name="pack_type">end</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</template>
</interface>
# List of source files containing translatable strings.
# Please keep this list sorted alphabetically.
[encoding: UTF-8]
data/gcolor3.desktop.in.in
[type: gettext/glade]data/menus.ui
[type: gettext/glade]data/window.ui
src/main.c
src/gcolor3-application.c
src/gcolor3-window.c
......@@ -4,7 +4,9 @@
bin_PROGRAMS = \
gcolor3
# FIXME: make dist fails on resources if Gcolor3 hasn't been make'd yet.
gcolor3_SOURCES = \
../data/gcolor3-resources.c \
main.c \
gcolor3-application.c \
gcolor3-window.c \
......
......@@ -71,22 +71,6 @@ static GActionEntry app_entries[] = {
{ "quit", gcolor3_application_action_quit, NULL, NULL, NULL },
};
static void
gcolor3_application_init_app_menu (GtkApplication *application)
{
GMenu *menu;
g_action_map_add_action_entries (G_ACTION_MAP (application),
app_entries, G_N_ELEMENTS (app_entries),
application);
menu = g_menu_new ();
g_menu_append (menu, _("About"), "app.about");
g_menu_append (menu, _("Quit"), "app.quit");
gtk_application_set_app_menu (application, G_MENU_MODEL (menu));
g_object_unref (menu);
}
static void
gcolor3_application_init_accelerators (GtkApplication *application)
{
......@@ -142,7 +126,10 @@ gcolor3_application_startup (GApplication *application)
gtk_window_set_default_icon_name ("gcolor3");
g_set_application_name (_("Color picker"));
gcolor3_application_init_app_menu (GTK_APPLICATION (app));
g_action_map_add_action_entries (G_ACTION_MAP (application),
app_entries, G_N_ELEMENTS (app_entries),
application);
gcolor3_application_init_accelerators (GTK_APPLICATION (app));
gcolor3_application_load_colors (app);
......
......@@ -35,12 +35,6 @@
/* Copyright years. */
#define COPYRIGHT "2013-2016"
/* The duration of the revealer's animation. */
#define REVEALER_DURATION 400
/* The border inside the window. */
#define BORDER_WIDTH 6
enum {
COLOR_PIXBUF,
COLOR_VALUE,
......@@ -49,15 +43,16 @@ enum {
};
struct _Gcolor3WindowPrivate {
GtkWidget *headerbar;
GtkWidget *button;
GtkWidget *revealer;
GtkWidget *entry;
GtkWidget *stack;
GtkWidget *picker;
GtkWidget *tree;
GtkTreeSelection *selection;
GKeyFile *colors; /* Do not free: owned by Gcolor3Application! */
GtkTreeSelection *selection;
GdkColor current;
};
......@@ -295,95 +290,60 @@ gcolor3_window_selection_changed (GtkTreeSelection *selection, gpointer user_dat
}
static void
gcolor3_window_construct_ui (Gcolor3Window *window)
gcolor3_window_finalize (GObject *object)
{
Gcolor3WindowPrivate *priv;
gchar *hex;
priv = gcolor3_window_get_instance_private (GCOLOR3_WINDOW (object));
hex = hex_value (&priv->current);
g_printf ("%s\n", hex);
g_free (hex);
G_OBJECT_CLASS (gcolor3_window_parent_class)->finalize (object);
}
static void
gcolor3_window_class_init (Gcolor3WindowClass *gcolor3_window_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (gcolor3_window_class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (gcolor3_window_class);
object_class->finalize = gcolor3_window_finalize;
gtk_widget_class_set_template_from_resource (widget_class, "/org/unia/gcolor3/window.ui");
gtk_widget_class_bind_template_child_private (widget_class, Gcolor3Window, headerbar);
gtk_widget_class_bind_template_child_private (widget_class, Gcolor3Window, button);
gtk_widget_class_bind_template_child_private (widget_class, Gcolor3Window, revealer);
gtk_widget_class_bind_template_child_private (widget_class, Gcolor3Window, entry);
gtk_widget_class_bind_template_child_private (widget_class, Gcolor3Window, stack);
gtk_widget_class_bind_template_child_private (widget_class, Gcolor3Window, picker);
gtk_widget_class_bind_template_child_private (widget_class, Gcolor3Window, tree);
gtk_widget_class_bind_template_child_private (widget_class, Gcolor3Window, selection);
gtk_widget_class_bind_template_callback (widget_class, gcolor3_window_stack_changed);
gtk_widget_class_bind_template_callback (widget_class, gcolor3_window_picker_changed);
gtk_widget_class_bind_template_callback (widget_class, gcolor3_window_selection_changed);
}
static void
gcolor3_window_setup_tree_view (Gcolor3Window *window)
{
Gcolor3WindowPrivate *priv;
GtkListStore *liststore;
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
GtkWidget *headerbar;
GtkWidget *switcher;
GtkWidget *scroll;
GtkTreeViewColumn *column;
priv = gcolor3_window_get_instance_private (window);
headerbar = gtk_header_bar_new ();
gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (headerbar), TRUE);
gtk_header_bar_set_title (GTK_HEADER_BAR (headerbar), g_get_application_name ());
gtk_window_set_titlebar (GTK_WINDOW (window), headerbar);
gtk_widget_show (headerbar);
priv->button = gtk_button_new ();
gtk_header_bar_pack_end (GTK_HEADER_BAR (headerbar), priv->button);
gtk_actionable_set_action_name (GTK_ACTIONABLE (priv->button), "win.save");
/* Make default, so that the GtkEntry later on can use it. */
gtk_widget_set_can_default (priv->button, TRUE);
gtk_widget_grab_default (priv->button);
gtk_widget_show (priv->button);
priv->revealer = gtk_revealer_new ();
gtk_revealer_set_transition_duration (GTK_REVEALER (priv->revealer), REVEALER_DURATION);
gtk_revealer_set_transition_type (GTK_REVEALER (priv->revealer),
GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT);
gtk_header_bar_pack_end (GTK_HEADER_BAR (headerbar), priv->revealer);
gtk_widget_show (priv->revealer);
priv->entry = gtk_entry_new ();
/* Activate default widget on enter, set above. */
gtk_entry_set_activates_default (GTK_ENTRY (priv->entry), TRUE);
gtk_entry_set_placeholder_text (GTK_ENTRY (priv->entry), _("Color name..."));
gtk_container_add (GTK_CONTAINER (priv->revealer), priv->entry);
gtk_widget_show (priv->entry);
priv->stack = gtk_stack_new ();
gtk_stack_set_transition_type (GTK_STACK (priv->stack),
GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT_RIGHT);
g_signal_connect (priv->stack, "notify::visible-child",
G_CALLBACK (gcolor3_window_stack_changed), window);
gtk_container_add (GTK_CONTAINER (window), priv->stack);
gtk_widget_show (priv->stack);
switcher = gtk_stack_switcher_new ();
gtk_stack_switcher_set_stack (GTK_STACK_SWITCHER (switcher), GTK_STACK (priv->stack));
gtk_header_bar_pack_start (GTK_HEADER_BAR (headerbar), switcher);
gtk_widget_show (switcher);
priv->picker = gtk_color_selection_new ();
g_signal_connect (priv->picker, "color-changed",
G_CALLBACK (gcolor3_window_picker_changed), window);
/* Call the above callback to initialise the GtkEntry and to prevent
* saving #000000 when saving the white color right away. */
gcolor3_window_picker_changed (GTK_COLOR_SELECTION (priv->picker), window);
gtk_stack_add_titled (GTK_STACK (priv->stack), priv->picker, "picker", _("Picker"));
gtk_widget_show (priv->picker);
scroll = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_set_vexpand (scroll, TRUE);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll),
GTK_SHADOW_IN);
gtk_stack_add_titled (GTK_STACK (priv->stack), scroll, "saved-colors", _("Saved colors"));
gtk_widget_show (scroll);
liststore = gtk_list_store_new (N_COLUMNS,
GDK_TYPE_PIXBUF,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING);
priv->tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (liststore));
g_object_unref (liststore);
gtk_tree_view_set_search_column (GTK_TREE_VIEW (priv->tree), COLOR_NAME);
gtk_container_add (GTK_CONTAINER (scroll), priv->tree);
gtk_widget_show (priv->tree);
renderer = gtk_cell_renderer_pixbuf_new ();
column = gtk_tree_view_column_new_with_attributes (_("Color"),
renderer,
"pixbuf",
COLOR_PIXBUF,
NULL);
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_column_pack_start (column, renderer, TRUE);
gtk_tree_view_column_set_attributes (column,
......@@ -401,63 +361,28 @@ gcolor3_window_construct_ui (Gcolor3Window *window)
NULL);
gtk_tree_view_column_set_sort_column_id (column, COLOR_NAME);
gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree), column);
priv->selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree));
gtk_tree_selection_set_mode (priv->selection, GTK_SELECTION_SINGLE);
g_signal_connect (priv->selection, "changed",
G_CALLBACK (gcolor3_window_selection_changed), window);
}
static GObject *
gcolor3_window_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_params)
{
GObject *object;
object = G_OBJECT_CLASS (gcolor3_window_parent_class)->constructor
(type, n_construct_properties, construct_params);
gcolor3_window_construct_ui (GCOLOR3_WINDOW (object));
return object;
}
static void
gcolor3_window_finalize (GObject *object)
gcolor3_window_init (Gcolor3Window *window)
{
Gcolor3WindowPrivate *priv;
gchar *hex;
priv = gcolor3_window_get_instance_private (GCOLOR3_WINDOW (object));
hex = hex_value (&priv->current);
g_printf ("%s\n", hex);
g_free (hex);
priv = gcolor3_window_get_instance_private (window);
G_OBJECT_CLASS (gcolor3_window_parent_class)->finalize (object);
}
gtk_widget_init_template (GTK_WIDGET (window));
gtk_header_bar_set_title (GTK_HEADER_BAR (priv->headerbar), g_get_application_name ());
static void
gcolor3_window_class_init (Gcolor3WindowClass *gcolor3_window_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (gcolor3_window_class);
object_class->constructor = gcolor3_window_constructor;
object_class->finalize = gcolor3_window_finalize;
}
static void
gcolor3_window_init (Gcolor3Window *window)
{
gtk_container_set_border_width (GTK_CONTAINER (window), BORDER_WIDTH);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
/* Call the callback to initialise the GtkEntry and to prevent
* saving #000000 when saving the white color right away. */
gcolor3_window_picker_changed (GTK_COLOR_SELECTION (priv->picker), window);
g_action_map_add_action_entries (G_ACTION_MAP (window),
window_actions,
G_N_ELEMENTS (window_actions),
window);
gcolor3_window_setup_tree_view (window);
}
Gcolor3Window *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment