[bijiben/wip/sadiq/modernize: 4/8] empty-results-box: Use GtkBuilder templates
- From: Mohammed Sadiq <pksadiq src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [bijiben/wip/sadiq/modernize: 4/8] empty-results-box: Use GtkBuilder templates
- Date: Wed, 1 Nov 2017 11:06:52 +0000 (UTC)
commit 68bd31195b1b1d039b9c10f8943c8be1b6ff0efc
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date: Sat Oct 28 15:23:04 2017 +0530
empty-results-box: Use GtkBuilder templates
Move UI code to xml GtkBuilder templates. Also, use
G_DECLARE_FINAL_TYPE to remove lots of boilerplate code.
This is a part of effort to clean up codebase and make the code
more maintainable.
https://bugzilla.gnome.org/show_bug.cgi?id=789696
po/POTFILES.in | 1 +
src/bjb-empty-results-box.c | 118 ++++++++----------------------------
src/bjb-empty-results-box.h | 38 +-----------
src/bjb.gresource.xml | 4 +
src/resources/empty-results-box.ui | 50 +++++++++++++++
5 files changed, 83 insertions(+), 128 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 86821f7..01df1cd 100755
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -25,4 +25,5 @@ src/libbiji/biji-notebook.c
src/libbiji/provider/biji-local-provider.c
src/resources/app-menu.ui
src/resources/editor-toolbar.ui
+src/resources/empty-results-box.ui
src/resources/settings-dialog.ui
diff --git a/src/bjb-empty-results-box.c b/src/bjb-empty-results-box.c
index fc00759..8db843b 100644
--- a/src/bjb-empty-results-box.c
+++ b/src/bjb-empty-results-box.c
@@ -1,6 +1,7 @@
/*
* Bijiben
* Copyright © 2012, 2013 Red Hat, Inc.
+ * Copyright 2017 Mohammed Sadiq <sadiq sadiqpk org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -30,86 +31,18 @@
#include "bjb-empty-results-box.h"
-G_DEFINE_TYPE (BjbEmptyResultsBox, bjb_empty_results_box, GTK_TYPE_GRID);
-struct _BjbEmptyResultsBoxPrivate
+struct _BjbEmptyResultsBox
{
+ GtkGrid parent_instance;
+
GtkWidget *primary_label;
GtkWidget *image;
GtkLabel *details_label;
BjbEmptyResultsBoxType type;
};
-
-static void
-bjb_empty_results_box_constructed (GObject *object)
-{
- BjbEmptyResultsBox *self;
- BjbEmptyResultsBoxPrivate *priv;
- GtkStyleContext *context;
- GtkWidget *labels_grid;
- const gchar *label;
- gchar *markup;
- GFile *note_icon_file;
- GIcon *icon;
-
- G_OBJECT_CLASS (bjb_empty_results_box_parent_class)->constructed (object);
- self = BJB_EMPTY_RESULTS_BOX (object);
- priv = self->priv;
-
- gtk_widget_set_halign (GTK_WIDGET (self), GTK_ALIGN_CENTER);
- gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE);
- gtk_widget_set_valign (GTK_WIDGET (self), GTK_ALIGN_CENTER);
- gtk_widget_set_vexpand (GTK_WIDGET (self), TRUE);
-
- gtk_orientable_set_orientation (GTK_ORIENTABLE (self), GTK_ORIENTATION_HORIZONTAL);
- gtk_grid_set_column_spacing (GTK_GRID (self), 12);
-
- context = gtk_widget_get_style_context (GTK_WIDGET (self));
- gtk_style_context_add_class (context, "dim-label");
-
- note_icon_file = g_file_new_for_uri ("resource://org/gnome/bijiben/note-symbolic.svg");
- icon = g_file_icon_new (note_icon_file);
- g_object_unref (note_icon_file);
-
- priv->image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
- g_object_unref (icon);
-
- gtk_container_add (GTK_CONTAINER (self), priv->image);
-
- labels_grid = gtk_grid_new ();
- gtk_orientable_set_orientation (GTK_ORIENTABLE (labels_grid), GTK_ORIENTATION_VERTICAL);
- gtk_grid_set_row_spacing (GTK_GRID (labels_grid), 0);
- gtk_widget_set_valign (labels_grid, GTK_ALIGN_START);
- gtk_container_add (GTK_CONTAINER (self), labels_grid);
-
- label = _("No notes");
- markup = g_markup_printf_escaped ("<big><b>%s</b></big>", label);
-
- priv->primary_label = gtk_label_new (NULL);
- gtk_label_set_markup (GTK_LABEL (priv->primary_label), markup);
- g_free (markup);
-
- gtk_widget_set_halign (priv->primary_label, GTK_ALIGN_START);
- gtk_widget_set_vexpand (priv->primary_label, TRUE);
- gtk_label_set_use_markup (GTK_LABEL (priv->primary_label), TRUE);
- gtk_container_add (GTK_CONTAINER (labels_grid), priv->primary_label);
-
-
- self->priv->type = BJB_EMPTY_RESULTS_TYPE;
- label = "";
- self->priv->details_label = GTK_LABEL (gtk_label_new (label));
- gtk_label_set_use_markup (GTK_LABEL (self->priv->details_label), TRUE);
- gtk_widget_set_halign (priv->primary_label, GTK_ALIGN_START);
- // xalign: 0,
- // max_width_chars: 24,
- // wrap: true
-
- gtk_container_add (GTK_CONTAINER (labels_grid), GTK_WIDGET (self->priv->details_label));
-
- gtk_widget_set_valign (priv->primary_label, GTK_ALIGN_CENTER);
- gtk_widget_show_all (GTK_WIDGET (self));
-}
+G_DEFINE_TYPE (BjbEmptyResultsBox, bjb_empty_results_box, GTK_TYPE_GRID);
void
bjb_empty_results_box_set_type (BjbEmptyResultsBox *self,
@@ -119,26 +52,26 @@ bjb_empty_results_box_set_type (BjbEmptyResultsBox *self,
g_return_if_fail (BJB_IS_EMPTY_RESULTS_BOX (self));
- if (type == self->priv->type)
+ if (type == self->type)
return;
switch (type)
{
case BJB_EMPTY_RESULTS_NO_NOTE:
gtk_label_set_label (
- self->priv->details_label,
+ self->details_label,
_("Press the New button to create a note."));
- gtk_widget_show (GTK_WIDGET (self->priv->details_label));
- gtk_widget_show (self->priv->image);
+ gtk_widget_show (GTK_WIDGET (self->details_label));
+ gtk_widget_show (self->image);
break;
case BJB_EMPTY_RESULTS_NO_RESULTS:
gtk_label_set_label (
- self->priv->details_label, NULL);
+ self->details_label, NULL);
- gtk_widget_hide (GTK_WIDGET (self->priv->details_label));
- gtk_widget_hide (self->priv->image);
+ gtk_widget_hide (GTK_WIDGET (self->details_label));
+ gtk_widget_hide (self->image);
break;
@@ -151,14 +84,14 @@ bjb_empty_results_box_set_type (BjbEmptyResultsBox *self,
case BJB_EMPTY_RESULTS_TRACKER:
label = _("Oops");
gtk_label_set_label (
- GTK_LABEL (self->priv->primary_label), label);
+ GTK_LABEL (self->primary_label), label);
gtk_label_set_label (
- self->priv->details_label,
+ self->details_label,
_("Please install 'Tracker' then restart the application."));
- gtk_widget_show (GTK_WIDGET (self->priv->details_label));
- gtk_widget_show (self->priv->image);
+ gtk_widget_show (GTK_WIDGET (self->details_label));
+ gtk_widget_show (self->image);
break;
case BJB_EMPTY_RESULTS_TYPE:
@@ -166,26 +99,28 @@ bjb_empty_results_box_set_type (BjbEmptyResultsBox *self,
break;
}
- self->priv->type = type;
+ self->type = type;
}
static void
bjb_empty_results_box_init (BjbEmptyResultsBox *self)
{
- self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
- BJB_TYPE_EMPTY_RESULTS_BOX,
- BjbEmptyResultsBoxPrivate);
-}
+ gtk_icon_theme_add_resource_path (gtk_icon_theme_get_default (),
+ "/org/gnome/bijiben/icons");
+ gtk_widget_init_template (GTK_WIDGET (self));
+}
static void
bjb_empty_results_box_class_init (BjbEmptyResultsBoxClass *klass)
{
- GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
- g_type_class_add_private (klass, sizeof (BjbEmptyResultsBoxPrivate));
+ gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/bijiben/ui/empty-results-box.ui");
- object_class->constructed = bjb_empty_results_box_constructed;
+ gtk_widget_class_bind_template_child (widget_class, BjbEmptyResultsBox, primary_label);
+ gtk_widget_class_bind_template_child (widget_class, BjbEmptyResultsBox, details_label);
+ gtk_widget_class_bind_template_child (widget_class, BjbEmptyResultsBox, image);
}
@@ -194,4 +129,3 @@ bjb_empty_results_box_new (void)
{
return g_object_new (BJB_TYPE_EMPTY_RESULTS_BOX, NULL);
}
-
diff --git a/src/bjb-empty-results-box.h b/src/bjb-empty-results-box.h
index c08132c..7741789 100644
--- a/src/bjb-empty-results-box.h
+++ b/src/bjb-empty-results-box.h
@@ -1,6 +1,7 @@
/*
* Bijiben
* Copyright © 2012, 2013 Red Hat, Inc.
+ * Copyright 2017 Mohammed Sadiq <sadiq sadiqpk org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -39,42 +40,7 @@ typedef enum {
#define BJB_TYPE_EMPTY_RESULTS_BOX (bjb_empty_results_box_get_type ())
-#define BJB_EMPTY_RESULTS_BOX(obj) \
- (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
- BJB_TYPE_EMPTY_RESULTS_BOX, BjbEmptyResultsBox))
-
-#define BJB_EMPTY_RESULTS_BOX_CLASS(klass) \
- (G_TYPE_CHECK_CLASS_CAST ((klass), \
- BJB_TYPE_EMPTY_RESULTS_BOX, BjbEmptyResultsBoxClass))
-
-#define BJB_IS_EMPTY_RESULTS_BOX(obj) \
- (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
- BJB_TYPE_EMPTY_RESULTS_BOX))
-
-#define BJB_IS_EMPTY_RESULTS_BOX_CLASS(klass) \
- (G_TYPE_CHECK_CLASS_TYPE ((klass), \
- BJB_TYPE_EMPTY_RESULTS_BOX))
-
-#define BJB_EMPTY_RESULTS_BOX_GET_CLASS(obj) \
- (G_TYPE_INSTANCE_GET_CLASS ((obj), \
- BJB_TYPE_EMPTY_RESULTS_BOX, BjbEmptyResultsBoxClass))
-
-typedef struct _BjbEmptyResultsBox BjbEmptyResultsBox;
-typedef struct _BjbEmptyResultsBoxClass BjbEmptyResultsBoxClass;
-typedef struct _BjbEmptyResultsBoxPrivate BjbEmptyResultsBoxPrivate;
-
-struct _BjbEmptyResultsBox
-{
- GtkGrid parent_instance;
- BjbEmptyResultsBoxPrivate *priv;
-};
-
-struct _BjbEmptyResultsBoxClass
-{
- GtkGridClass parent_class;
-};
-
-GType bjb_empty_results_box_get_type (void) G_GNUC_CONST;
+G_DECLARE_FINAL_TYPE (BjbEmptyResultsBox, bjb_empty_results_box, BJB, EMPTY_RESULTS_BOX, GtkGrid)
GtkWidget *bjb_empty_results_box_new (void);
diff --git a/src/bjb.gresource.xml b/src/bjb.gresource.xml
index 690c927..df3f768 100644
--- a/src/bjb.gresource.xml
+++ b/src/bjb.gresource.xml
@@ -4,6 +4,9 @@
<file alias="Adwaita.css">resources/Adwaita.css</file>
<file alias="editor-toolbar.ui" compressed="true"
preprocess="xml-stripblanks">resources/editor-toolbar.ui</file>
<file alias="thumbnail-frame.png">resources/thumbnail-frame.png</file>
+ </gresource>
+
+ <gresource prefix="/org/gnome/bijiben/icons">
<file alias="note-symbolic.svg">resources/note-symbolic.svg</file>
</gresource>
@@ -13,5 +16,6 @@
<gresource prefix="/org/gnome/bijiben/ui">
<file alias="settings-dialog.ui">resources/settings-dialog.ui</file>
+ <file alias="empty-results-box.ui">resources/empty-results-box.ui</file>
</gresource>
</gresources>
diff --git a/src/resources/empty-results-box.ui b/src/resources/empty-results-box.ui
new file mode 100644
index 0000000..0653a6c
--- /dev/null
+++ b/src/resources/empty-results-box.ui
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+ <template class="BjbEmptyResultsBox" parent="GtkGrid">
+ <property name="visible">True</property>
+ <property name="halign">center</property>
+ <property name="valign">center</property>
+ <property name="hexpand">True</property>
+ <property name="vexpand">True</property>
+ <property name="column-spacing">12</property>
+ <style>
+ <class name="dim-label" />
+ </style>
+
+ <child>
+ <object class="GtkImage" id="image">
+ <property name="visible">True</property>
+ <property name="icon-name">note-symbolic</property>
+ <property name="pixel-size">48</property>
+ </object>
+ </child>
+
+ <child>
+ <object class="GtkGrid">
+ <property name="visible">True</property>
+ <property name="row-spacing">0</property>
+ <property name="valign">start</property>
+ <property name="orientation">vertical</property>
+ <child>
+ <object class="GtkLabel" id="primary_label">
+ <property name="visible">True</property>
+ <property name="halign">start</property>
+ <property name="vexpand">True</property>
+ <property name="label" translatable="yes">No notes</property>
+ <attributes>
+ <attribute name="weight" value="bold"/>
+ <attribute name="scale" value="1.2"/>
+ </attributes>
+ </object>
+ </child>
+ <child>
+ <object class="GtkLabel" id="details_label">
+ <property name="halign">start</property>
+ <property name="visible">True</property>
+ </object>
+ </child>
+ </object>
+ </child>
+
+ </template>
+</interface>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]