[tepl] TabLabel: basic implementation



commit f1f39af67f3e5b6870921c25e97d6490f0c8ff31
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Aug 5 13:13:02 2017 +0200

    TabLabel: basic implementation

 docs/reference/tepl-3.0-sections.txt |   17 +++
 docs/reference/tepl-docs.xml.in      |    1 +
 po/POTFILES.in                       |    1 +
 tepl/Makefile.am                     |    2 +
 tepl/tepl-tab-label.c                |  243 ++++++++++++++++++++++++++++++++++
 tepl/tepl-tab-label.h                |   64 +++++++++
 tepl/tepl-types.h                    |    1 +
 tepl/tepl.h                          |    1 +
 8 files changed, 330 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/tepl-3.0-sections.txt b/docs/reference/tepl-3.0-sections.txt
index c5e001e..7a6b9c3 100644
--- a/docs/reference/tepl-3.0-sections.txt
+++ b/docs/reference/tepl-3.0-sections.txt
@@ -486,6 +486,23 @@ tepl_tab_group_get_type
 </SECTION>
 
 <SECTION>
+<FILE>tab-label</FILE>
+TeplTabLabel
+tepl_tab_label_new
+tepl_tab_label_get_tab
+<SUBSECTION Standard>
+TEPL_IS_TAB_LABEL
+TEPL_IS_TAB_LABEL_CLASS
+TEPL_TAB_LABEL
+TEPL_TAB_LABEL_CLASS
+TEPL_TAB_LABEL_GET_CLASS
+TEPL_TYPE_TAB_LABEL
+TeplTabLabelClass
+TeplTabLabelPrivate
+tepl_tab_label_get_type
+</SECTION>
+
+<SECTION>
 <FILE>notebook</FILE>
 TeplNotebook
 tepl_notebook_new
diff --git a/docs/reference/tepl-docs.xml.in b/docs/reference/tepl-docs.xml.in
index 3ffddcd..39b0891 100644
--- a/docs/reference/tepl-docs.xml.in
+++ b/docs/reference/tepl-docs.xml.in
@@ -50,6 +50,7 @@
       <xi:include href="xml/tab-group.xml"/>
       <xi:include href="xml/notebook.xml"/>
       <xi:include href="xml/tab.xml"/>
+      <xi:include href="xml/tab-label.xml"/>
       <xi:include href="xml/view.xml"/>
       <xi:include href="xml/buffer.xml"/>
     </chapter>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 7dbbe53..5218572 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -31,5 +31,6 @@ tepl/tepl-notebook.c
 tepl/tepl-signal-group.c
 tepl/tepl-tab.c
 tepl/tepl-tab-group.c
+tepl/tepl-tab-label.c
 tepl/tepl-utils.c
 tepl/tepl-view.c
diff --git a/tepl/Makefile.am b/tepl/Makefile.am
index 3843fac..f0b6b59 100644
--- a/tepl/Makefile.am
+++ b/tepl/Makefile.am
@@ -32,6 +32,7 @@ tepl_public_headers =                         \
        tepl-types.h                            \
        tepl-tab.h                              \
        tepl-tab-group.h                        \
+       tepl-tab-label.h                        \
        tepl-utils.h                            \
        tepl-view.h
 
@@ -55,6 +56,7 @@ tepl_public_c_files =                         \
        tepl-notebook.c                         \
        tepl-tab.c                              \
        tepl-tab-group.c                        \
+       tepl-tab-label.c                        \
        tepl-utils.c                            \
        tepl-view.c
 
diff --git a/tepl/tepl-tab-label.c b/tepl/tepl-tab-label.c
new file mode 100644
index 0000000..3d34bf3
--- /dev/null
+++ b/tepl/tepl-tab-label.c
@@ -0,0 +1,243 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl 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.1 of the License, or (at your
+ * option) any later version.
+ *
+ * Tepl 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 this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tepl-tab-label.h"
+#include "tepl-tab.h"
+#include "tepl-buffer.h"
+#include "tepl-signal-group.h"
+
+/**
+ * SECTION:tab-label
+ * @Short_description: A #TeplTab label, to use with #GtkNotebook
+ * @Title: TeplTabLabel
+ *
+ * #TeplTabLabel is the label/title of a #TeplTab, suitable for #GtkNotebook.
+ */
+
+struct _TeplTabLabelPrivate
+{
+       TeplTab *tab;
+       TeplSignalGroup *buffer_signal_group;
+
+       GtkLabel *label;
+};
+
+enum
+{
+       PROP_0,
+       PROP_TAB,
+       N_PROPERTIES
+};
+
+static GParamSpec *properties[N_PROPERTIES];
+
+G_DEFINE_TYPE_WITH_PRIVATE (TeplTabLabel, tepl_tab_label, GTK_TYPE_GRID)
+
+static void
+update_label (TeplTabLabel *tab_label)
+{
+       TeplBuffer *buffer;
+       gchar *short_title;
+
+       buffer = tepl_tab_get_buffer (tab_label->priv->tab);
+       short_title = tepl_buffer_get_short_title (buffer);
+
+       gtk_label_set_text (tab_label->priv->label, short_title);
+
+       g_free (short_title);
+}
+
+static void
+buffer_short_title_notify_cb (TeplBuffer   *buffer,
+                             GParamSpec   *pspec,
+                             TeplTabLabel *tab_label)
+{
+       update_label (tab_label);
+}
+
+static void
+buffer_changed (TeplTabLabel *tab_label)
+{
+       TeplBuffer *buffer;
+
+       _tepl_signal_group_clear (&tab_label->priv->buffer_signal_group);
+
+       buffer = tepl_tab_get_buffer (tab_label->priv->tab);
+       tab_label->priv->buffer_signal_group = _tepl_signal_group_new (G_OBJECT (buffer));
+
+       _tepl_signal_group_add (tab_label->priv->buffer_signal_group,
+                               g_signal_connect (buffer,
+                                                 "notify::tepl-short-title",
+                                                 G_CALLBACK (buffer_short_title_notify_cb),
+                                                 tab_label));
+
+       update_label (tab_label);
+}
+
+static void
+buffer_notify_cb (GtkTextView  *view,
+                 GParamSpec   *pspec,
+                 TeplTabLabel *tab_label)
+{
+       buffer_changed (tab_label);
+}
+
+static void
+set_tab (TeplTabLabel *tab_label,
+        TeplTab      *tab)
+{
+       TeplView *view;
+
+       g_return_if_fail (TEPL_IS_TAB (tab));
+
+       g_assert (tab_label->priv->tab == NULL);
+       tab_label->priv->tab = g_object_ref (tab);
+
+       view = tepl_tab_get_view (tab);
+       g_signal_connect_object (view,
+                                "notify::buffer",
+                                G_CALLBACK (buffer_notify_cb),
+                                tab_label,
+                                0);
+
+       buffer_changed (tab_label);
+}
+
+static void
+tepl_tab_label_get_property (GObject    *object,
+                             guint       prop_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
+{
+       TeplTabLabel *tab_label = TEPL_TAB_LABEL (object);
+
+       switch (prop_id)
+       {
+               case PROP_TAB:
+                       g_value_set_object (value, tepl_tab_label_get_tab (tab_label));
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+tepl_tab_label_set_property (GObject      *object,
+                             guint         prop_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+       TeplTabLabel *tab_label = TEPL_TAB_LABEL (object);
+
+       switch (prop_id)
+       {
+               case PROP_TAB:
+                       set_tab (tab_label, g_value_get_object (value));
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+tepl_tab_label_dispose (GObject *object)
+{
+       TeplTabLabel *tab_label = TEPL_TAB_LABEL (object);
+
+       g_clear_object (&tab_label->priv->tab);
+       _tepl_signal_group_clear (&tab_label->priv->buffer_signal_group);
+
+       G_OBJECT_CLASS (tepl_tab_label_parent_class)->dispose (object);
+}
+
+static void
+tepl_tab_label_class_init (TeplTabLabelClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->get_property = tepl_tab_label_get_property;
+       object_class->set_property = tepl_tab_label_set_property;
+       object_class->dispose = tepl_tab_label_dispose;
+
+       /**
+        * TeplTabLabel:tab:
+        *
+        * The associated #TeplTab. #TeplTabLabel has a strong reference to the
+        * #TeplTab.
+        *
+        * Since: 3.0
+        */
+       properties[PROP_TAB] =
+               g_param_spec_object ("tab",
+                                    "tab",
+                                    "",
+                                    TEPL_TYPE_TAB,
+                                    G_PARAM_READWRITE |
+                                    G_PARAM_CONSTRUCT_ONLY |
+                                    G_PARAM_STATIC_STRINGS);
+
+       g_object_class_install_properties (object_class, N_PROPERTIES, properties);
+}
+
+static void
+tepl_tab_label_init (TeplTabLabel *tab_label)
+{
+       tab_label->priv = tepl_tab_label_get_instance_private (tab_label);
+
+       tab_label->priv->label = GTK_LABEL (gtk_label_new (NULL));
+       gtk_widget_show (GTK_WIDGET (tab_label->priv->label));
+       gtk_container_add (GTK_CONTAINER (tab_label),
+                          GTK_WIDGET (tab_label->priv->label));
+}
+
+/**
+ * tepl_tab_label_new:
+ * @tab: a #TeplTab.
+ *
+ * Returns: (transfer floating): a new #TeplTabLabel.
+ * Since: 3.0
+ */
+GtkWidget *
+tepl_tab_label_new (TeplTab *tab)
+{
+       g_return_val_if_fail (TEPL_IS_TAB (tab), NULL);
+
+       return g_object_new (TEPL_TYPE_TAB_LABEL,
+                            "tab", tab,
+                            NULL);
+}
+
+/**
+ * tepl_tab_label_get_tab:
+ * @tab_label: a #TeplTabLabel.
+ *
+ * Returns: (transfer none): the #TeplTabLabel:tab.
+ * Since: 3.0
+ */
+TeplTab *
+tepl_tab_label_get_tab (TeplTabLabel *tab_label)
+{
+       g_return_val_if_fail (TEPL_IS_TAB_LABEL (tab_label), NULL);
+
+       return tab_label->priv->tab;
+}
diff --git a/tepl/tepl-tab-label.h b/tepl/tepl-tab-label.h
new file mode 100644
index 0000000..0edb2e5
--- /dev/null
+++ b/tepl/tepl-tab-label.h
@@ -0,0 +1,64 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl 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.1 of the License, or (at your
+ * option) any later version.
+ *
+ * Tepl 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 this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TEPL_TAB_LABEL_H
+#define TEPL_TAB_LABEL_H
+
+#if !defined (TEPL_H_INSIDE) && !defined (TEPL_COMPILATION)
+#error "Only <tepl/tepl.h> can be included directly."
+#endif
+
+#include <gtk/gtk.h>
+#include <tepl/tepl-types.h>
+
+G_BEGIN_DECLS
+
+#define TEPL_TYPE_TAB_LABEL             (tepl_tab_label_get_type ())
+#define TEPL_TAB_LABEL(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEPL_TYPE_TAB_LABEL, 
TeplTabLabel))
+#define TEPL_TAB_LABEL_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), TEPL_TYPE_TAB_LABEL, 
TeplTabLabelClass))
+#define TEPL_IS_TAB_LABEL(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEPL_TYPE_TAB_LABEL))
+#define TEPL_IS_TAB_LABEL_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), TEPL_TYPE_TAB_LABEL))
+#define TEPL_TAB_LABEL_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), TEPL_TYPE_TAB_LABEL, 
TeplTabLabelClass))
+
+typedef struct _TeplTabLabelClass    TeplTabLabelClass;
+typedef struct _TeplTabLabelPrivate  TeplTabLabelPrivate;
+
+struct _TeplTabLabel
+{
+       GtkGrid parent;
+
+       TeplTabLabelPrivate *priv;
+};
+
+struct _TeplTabLabelClass
+{
+       GtkGridClass parent_class;
+
+       gpointer padding[12];
+};
+
+GType          tepl_tab_label_get_type         (void);
+
+GtkWidget *    tepl_tab_label_new              (TeplTab *tab);
+
+TeplTab *      tepl_tab_label_get_tab          (TeplTabLabel *tab_label);
+
+G_END_DECLS
+
+#endif /* TEPL_TAB_LABEL_H */
diff --git a/tepl/tepl-types.h b/tepl/tepl-types.h
index feae7c1..26fdf9a 100644
--- a/tepl/tepl-types.h
+++ b/tepl/tepl-types.h
@@ -43,6 +43,7 @@ typedef struct _TeplInfoBar                   TeplInfoBar;
 typedef struct _TeplNotebook                   TeplNotebook;
 typedef struct _TeplTab                                TeplTab;
 typedef struct _TeplTabGroup                   TeplTabGroup;
+typedef struct _TeplTabLabel                   TeplTabLabel;
 typedef struct _TeplView                       TeplView;
 
 G_END_DECLS
diff --git a/tepl/tepl.h b/tepl/tepl.h
index bf78137..131c5b5 100644
--- a/tepl/tepl.h
+++ b/tepl/tepl.h
@@ -46,6 +46,7 @@
 #include <tepl/tepl-notebook.h>
 #include <tepl/tepl-tab.h>
 #include <tepl/tepl-tab-group.h>
+#include <tepl/tepl-tab-label.h>
 #include <tepl/tepl-utils.h>
 #include <tepl/tepl-view.h>
 


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