[gnome-builder] gstyle-palette: new palette from text buffer



commit e739c90119162c9e88ef5bc18b7c968ac213f8db
Author: Sebastien Lafargue <slafargue gnome org>
Date:   Tue Jul 19 21:39:11 2016 +0200

    gstyle-palette: new palette from text buffer
    
    add a gstyle_palette_new_from_buffer constructor.

 contrib/gstyle/gstyle-palette.c |   72 +++++++++++++++++++++++++++++++++++++++
 contrib/gstyle/gstyle-palette.h |    6 +++
 2 files changed, 78 insertions(+), 0 deletions(-)
---
diff --git a/contrib/gstyle/gstyle-palette.c b/contrib/gstyle/gstyle-palette.c
index fec744d..0a927d0 100644
--- a/contrib/gstyle/gstyle-palette.c
+++ b/contrib/gstyle/gstyle-palette.c
@@ -21,6 +21,7 @@
 #include <glib/gi18n.h>
 #include <string.h>
 
+#include "gstyle-color-item.h"
 #include "gstyle-private.h"
 #include "gstyle-palette.h"
 
@@ -42,6 +43,8 @@ struct _GstylePalette
 
 static void gstyle_palette_list_model_iface_init (GListModelInterface *iface);
 
+static guint generated_count = 0;
+
 G_DEFINE_QUARK (gstyle_palette_error, gstyle_palette_error)
 
 G_DEFINE_TYPE_WITH_CODE (GstylePalette, gstyle_palette, G_TYPE_OBJECT,
@@ -753,6 +756,75 @@ gstyle_palette_new_from_file (GFile         *file,
 }
 
 /**
+ * gstyle_palette_new_from_buffer:
+ * @buffer: a #GtkTextBUffer
+ * @begin: (nullable): a begin #GtkTextIter
+ * @end: (nullable): a end #GtkTextIter
+ * @cancellable: A #GCancellable
+ * @error: (nullable): a #GError location or %NULL
+ *
+ * Create a new #GstylePalette from a text buffer.
+ * if @begin is %NULL, the buffer start iter is used.
+ * if @end is %NULL, the buffer end is used.
+ *
+ * Returns: A #GstylePalette or %NULL if an error occur.
+ */
+GstylePalette *
+gstyle_palette_new_from_buffer (GtkTextBuffer  *buffer,
+                                GtkTextIter    *begin,
+                                GtkTextIter    *end,
+                                GCancellable   *cancellable,
+                                GError        **error)
+{
+  g_autofree gchar *text = NULL;
+  GstylePalette *palette = NULL;
+  g_autofree gchar *name = NULL;
+  GtkTextIter real_begin, real_end;
+  GtkTextIter buffer_begin, buffer_end;
+  GstyleColorItem *item;
+  GstyleColor *color;
+  GPtrArray *items;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
+  g_return_val_if_fail (begin == NULL || gtk_text_iter_get_buffer (begin) == buffer, NULL);
+  g_return_val_if_fail (end == NULL || gtk_text_iter_get_buffer (end) == buffer, NULL);
+
+  gtk_text_buffer_get_bounds (buffer, &buffer_begin, &buffer_end);
+  real_begin = (begin == NULL) ? buffer_begin : *begin;
+  real_end = (end == NULL) ? buffer_end : *end;
+
+  text = gtk_text_buffer_get_slice (buffer, &real_begin, &real_end, FALSE);
+  items = gstyle_color_parse (text);
+  if (items == NULL)
+    {
+      g_set_error (error, GSTYLE_PALETTE_ERROR, GSTYLE_PALETTE_ERROR_PARSE,
+                   _("failed to parse\n"));
+      return NULL;
+    }
+
+  if (items->len > 0)
+    {
+      name = g_strdup_printf ("%s %i", _("Generated"), ++generated_count);
+      palette = g_object_new (GSTYLE_TYPE_PALETTE,
+                              "id", NULL,
+                              "name", name,
+                              "file", NULL,
+                              NULL);
+
+      for (gint i = 0; i < items->len; ++i)
+        {
+          item = g_ptr_array_index (items, i);
+          color = (GstyleColor *)gstyle_color_item_get_color (item);
+          gstyle_palette_add (palette, color, &tmp_error);
+        }
+    }
+
+  g_ptr_array_free (items, TRUE);
+  return palette;
+}
+
+/**
  * gstyle_palette_save_to_xml:
  * @self: a #GstylePalette
  * @file: a #GFile
diff --git a/contrib/gstyle/gstyle-palette.h b/contrib/gstyle/gstyle-palette.h
index aea4202..798a343 100644
--- a/contrib/gstyle/gstyle-palette.h
+++ b/contrib/gstyle/gstyle-palette.h
@@ -21,6 +21,7 @@
 
 #include <glib-object.h>
 #include <gio/gio.h>
+#include <gtk/gtk.h>
 
 #include <libxml/xmlreader.h>
 #include <libxml/parser.h>
@@ -48,6 +49,11 @@ typedef enum
 GQuark              gstyle_palette_error_quark           (void);
 
 GstylePalette      *gstyle_palette_new                   (void);
+GstylePalette      *gstyle_palette_new_from_buffer       (GtkTextBuffer  *buffer,
+                                                          GtkTextIter    *begin,
+                                                          GtkTextIter    *end,
+                                                          GCancellable   *cancellable,
+                                                          GError        **error);
 GstylePalette      *gstyle_palette_new_from_file         (GFile          *file,
                                                           GCancellable   *cancellable,
                                                           GError        **error);


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