[bijiben/wip/sadiq/rewrite: 4/15] Add note base class
- From: Mohammed Sadiq <pksadiq src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [bijiben/wip/sadiq/rewrite: 4/15] Add note base class
- Date: Sat, 10 Mar 2018 07:14:30 +0000 (UTC)
commit 7d67cbe705633539e29365ef1e930203343c3658
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date: Sun Feb 25 11:57:23 2018 +0530
Add note base class
src/notes/bjb-note.c | 477 ++++++++++++++++++++++++++++++++++++++++++++++++++
src/notes/bjb-note.h | 57 ++++++
2 files changed, 534 insertions(+), 0 deletions(-)
---
diff --git a/src/notes/bjb-note.c b/src/notes/bjb-note.c
new file mode 100644
index 0000000..640d4ce
--- /dev/null
+++ b/src/notes/bjb-note.c
@@ -0,0 +1,477 @@
+/* bjb-note.c
+ *
+ * Copyright 2012 Pierre-Yves LUYTEN <py luyten fr>
+ * Copyright 2018 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 as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "bjb-note"
+
+#include "config.h"
+
+#include "bjb-trace.h"
+
+#include "bjb-note.h"
+
+/**
+ * SECTION: bjb-note
+ * @title: BjbNote
+ * @short_description: The base class for Notes
+ */
+
+typedef struct
+{
+ gchar *content;
+
+ /*
+ * Content length is cached in associated GtkTextBuffer (ie, for open
+ * notes), so it's simpler to check if content length as changed
+ * before doing strcmp() to check if the note has changed.
+ */
+ gint content_length;
+
+ GdkPixbuf *icon;
+ GdkPixbuf *thumbnail;
+} BjbNotePrivate;
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (BjbNote, bjb_note, BJB_TYPE_ITEM)
+
+enum {
+ PROP_0,
+ PROP_CONTENT,
+ PROP_RAW_CONTENT,
+ N_PROPS
+};
+
+static GParamSpec *properties[N_PROPS];
+
+static void
+bjb_note_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ BjbNote *self = (BjbNote *)object;
+
+ switch (prop_id)
+ {
+ case PROP_CONTENT:
+ g_value_set_string (value, bjb_note_get_text_content (self));
+ break;
+
+ case PROP_RAW_CONTENT:
+ g_value_set_string (value, bjb_note_get_raw_content (self));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+bjb_note_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ BjbNote *self = (BjbNote *)object;
+
+ switch (prop_id)
+ {
+ case PROP_CONTENT:
+ bjb_note_set_text_content (self, g_value_get_string (value));
+ break;
+
+ case PROP_RAW_CONTENT:
+ bjb_note_set_raw_content (self, g_value_get_string (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+bjb_note_finalize (GObject *object)
+{
+ BjbNote *self = (BjbNote *)object;
+ BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+ BJB_ENTRY;
+
+ g_clear_pointer (&priv->content, g_free);
+
+ G_OBJECT_CLASS (bjb_note_parent_class)->finalize (object);
+
+ BJB_EXIT;
+}
+
+static cairo_surface_t *
+bjb_note_create_cairo_surface (BjbNote *self,
+ int scale,
+ int width,
+ int height,
+ cairo_t **cr)
+{
+ cairo_surface_t *surface;
+ GdkRGBA rgba;
+
+ surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+ width * scale,
+ height * scale);
+ cairo_surface_set_device_scale (surface, scale, scale);
+ *cr = cairo_create (surface);
+
+ cairo_rectangle (*cr, 0, 0, width, height);
+ bjb_item_get_rgba (BJB_ITEM (self), &rgba);
+ gdk_cairo_set_source_rgba (*cr, &rgba);
+
+ cairo_fill (*cr);
+
+ cairo_set_source_rgba (*cr, 0.3, 0.3, 0.3, 1);
+ cairo_set_line_width (*cr, 1 * scale);
+ cairo_rectangle (*cr, 0, 0, width, height);
+ cairo_stroke (*cr);
+
+ return surface;
+}
+
+static GdkPixbuf *
+bjb_note_get_icon (BjbItem *item,
+ int scale,
+ gboolean force_update)
+{
+ BjbNote *self = (BjbNote *)item;
+ BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+ cairo_surface_t *surface;
+ cairo_t *cr;
+
+ g_assert (BJB_IS_NOTE (self));
+
+ if (priv->icon != NULL && !force_update)
+ return priv->icon;
+
+ g_clear_object (&priv->icon);
+
+ surface = bjb_note_create_cairo_surface (self, scale,
+ BJB_ICON_WIDTH,
+ BJB_ICON_HEIGHT,
+ &cr);
+ priv->icon = gdk_pixbuf_get_from_surface (surface, 0, 0,
+ BJB_ICON_WIDTH,
+ BJB_ICON_HEIGHT);
+
+ cairo_destroy (cr);
+ cairo_surface_destroy (surface);
+
+ return priv->icon;
+}
+
+static GdkPixbuf *
+bjb_note_get_thumbnail (BjbItem *item,
+ int scale,
+ gboolean force_update)
+{
+ BjbNote *self = (BjbNote *)item;
+ BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+ g_autofree gchar *content = NULL;
+ cairo_surface_t *surface;
+ cairo_t *cr;
+ GdkRGBA rgba;
+
+ g_assert (BJB_IS_NOTE (self));
+
+ if (priv->thumbnail != NULL && !force_update)
+ return priv->thumbnail;
+
+ g_clear_object (&priv->thumbnail);
+
+ surface = bjb_note_create_cairo_surface (self, scale,
+ BJB_THUMBNAIL_WIDTH,
+ BJB_THUMBNAIL_HEIGHT,
+ &cr);
+ bjb_item_get_rgba (item, &rgba);
+
+ content = g_strconcat ("<b>",
+ bjb_item_get_title (BJB_ITEM (self)),
+ "</b>\n\n",
+ bjb_note_get_text_content (self),
+ NULL);
+ if (content != NULL)
+ {
+ PangoLayout *layout;
+ PangoFontDescription *desc;
+
+ cairo_translate (cr, 10, 10);
+ layout = pango_cairo_create_layout (cr);
+
+ pango_layout_set_width (layout, 180000);
+ pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
+ pango_layout_set_height (layout, 180000);
+
+ pango_layout_set_markup (layout, content, -1);
+ desc = pango_font_description_from_string ("Purusa 10");
+ pango_layout_set_font_description (layout, desc);
+ pango_font_description_free (desc);
+
+ if(rgba.red < 0.5)
+ cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
+ else
+ cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
+
+ pango_cairo_update_layout (cr, layout);
+ pango_cairo_show_layout (cr, layout);
+
+ g_object_unref (layout);
+ }
+
+ priv->thumbnail = gdk_pixbuf_get_from_surface (surface, 0, 0,
+ BJB_THUMBNAIL_WIDTH,
+ BJB_THUMBNAIL_HEIGHT);
+
+ cairo_destroy (cr);
+ cairo_surface_destroy (surface);
+
+ return priv->thumbnail;
+};
+
+static gchar *
+bjb_note_real_get_text_content (BjbNote *self)
+{
+ BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+ g_assert (BJB_IS_NOTE (self));
+
+ return g_strdup (priv->content);
+}
+
+static void
+bjb_note_real_set_text_content (BjbNote *self,
+ const gchar *content)
+{
+ BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+ g_assert (BJB_IS_NOTE (self));
+
+ g_free (priv->content);
+ priv->content = g_strdup (content);
+}
+
+static GtkTextBuffer *
+bjb_note_real_get_buffer (BjbNote *self)
+{
+ GtkTextBuffer *buffer;
+ g_autofree gchar *title = NULL;
+ g_autofree gchar *raw_content;
+ GtkTextIter iter;
+
+ g_assert (BJB_IS_NOTE (self));
+
+ title = bjb_item_get_title (BJB_ITEM (self));
+ raw_content = bjb_note_get_raw_content (self);
+ buffer = gtk_text_buffer_new (NULL);
+
+ gtk_text_buffer_create_tag (buffer, "bold",
+ "weight", PANGO_WEIGHT_BOLD, NULL);
+
+ gtk_text_buffer_get_start_iter (buffer, &iter);
+ gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
+ title, -1,
+ "bold", NULL);
+ gtk_text_buffer_insert (buffer, &iter, "\n", -1);
+ gtk_text_buffer_insert (buffer, &iter, raw_content, -1);
+
+ return buffer;
+}
+
+static void
+bjb_note_class_init (BjbNoteClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ BjbItemClass *item_class = BJB_ITEM_CLASS (klass);
+
+ object_class->get_property = bjb_note_get_property;
+ object_class->set_property = bjb_note_set_property;
+ object_class->finalize = bjb_note_finalize;
+
+ item_class->get_icon = bjb_note_get_icon;
+ item_class->get_thumbnail = bjb_note_get_thumbnail;
+
+ klass->get_text_content = bjb_note_real_get_text_content;
+ klass->set_raw_content = bjb_note_real_set_text_content;
+ klass->get_buffer = bjb_note_real_get_buffer;
+
+ properties[PROP_CONTENT] =
+ g_param_spec_string ("content",
+ "Content",
+ "The content of the note",
+ NULL,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+ /**
+ * BjbNote:raw-content:
+ *
+ * The raw-content of the note may include XML/markdown content,
+ * or whatever is used to keep the note formatted.
+ */
+ properties[PROP_RAW_CONTENT] =
+ g_param_spec_string ("raw-content",
+ "Raw Content",
+ "The raw content of the note",
+ NULL,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+ g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+bjb_note_init (BjbNote *self)
+{
+ BJB_ENTRY;
+ BJB_EXIT;
+}
+
+/**
+ * bjb_note_get_text_content:
+ * @self: a #BjbNote
+ *
+ * Get the plain content of the note, without any formatting tags, if any
+ *
+ * Returns (transfer full) (nullable): the plain text content of the note
+ */
+gchar *
+bjb_note_get_text_content (BjbNote *self)
+{
+ gchar *content;
+
+ BJB_ENTRY;
+
+ g_return_val_if_fail (BJB_IS_NOTE (self), NULL);
+
+ content = BJB_NOTE_GET_CLASS (self)->get_text_content (self);
+
+ BJB_RETURN (content);
+}
+
+/**
+ * bjb_note_set_text_content:
+ * @self: a #BjbNote
+ * @content: the text content to set
+ *
+ * Set the plain content of the note, with no formatting tags, if any
+ */
+void
+bjb_note_set_text_content (BjbNote *self,
+ const gchar *content)
+{
+ BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+ BJB_ENTRY;
+
+ g_return_if_fail (BJB_IS_NOTE (self));
+
+ g_free (priv->content);
+ priv->content = g_strdup (content);
+
+ BJB_EXIT;
+}
+
+/**
+ * bjb_note_get_raw_content:
+ * @self: a #BjbNote
+ *
+ * Get the raw content, may contain XML tags, or whatever is used for
+ * formatting
+ *
+ * Returns (transfer full) (nullable): the raw text content of the note
+ */
+gchar *
+bjb_note_get_raw_content (BjbNote *self)
+{
+ gchar *content;
+
+ BJB_ENTRY;
+
+ g_return_val_if_fail (BJB_IS_NOTE (self), NULL);
+
+ content = BJB_NOTE_GET_CLASS (self)->get_raw_content (self);
+
+ BJB_RETURN (content);
+}
+
+/**
+ * bjb_note_set_raw_content:
+ * @self: a #BjbNote
+ * @content: the raw content to set
+ *
+ * Set the raw content of the note, with all formatting tags, if any
+ */
+void
+bjb_note_set_raw_content (BjbNote *self,
+ const gchar *content)
+{
+ BJB_ENTRY;
+
+ g_return_if_fail (BJB_IS_NOTE (self));
+
+ BJB_NOTE_GET_CLASS (self)->set_raw_content (self, content);
+
+ BJB_EXIT;
+}
+
+/**
+ * bjb_note_set_content_from_buffer:
+ * @self: a #BjbNote
+ * @buffer: a #GtkTextBuffer from which text has to be extracted
+ *
+ * Set the content of the note from buffer
+ */
+void
+bjb_note_set_content_from_buffer (BjbNote *self,
+ GtkTextBuffer *buffer)
+{
+ BJB_ENTRY;
+
+ g_return_if_fail (BJB_IS_NOTE (self));
+
+ BJB_NOTE_GET_CLASS (self)->set_content_from_buffer (self, buffer);
+
+ BJB_EXIT;
+}
+
+/**
+ * bjb_note_get_buffer:
+ * @self: a #BjbNote
+ *
+ * Create a new #GtkTextBuffer from #BjbNote.
+ * @self should have title and content set.
+ *
+ * Returns: (transfer full): A buffer containing note content
+ */
+GtkTextBuffer *
+bjb_note_get_buffer (BjbNote *self)
+{
+ GtkTextBuffer *buffer;
+
+ BJB_ENTRY;
+
+ g_return_val_if_fail (BJB_IS_NOTE (self), NULL);
+
+ buffer = BJB_NOTE_GET_CLASS (self)->get_buffer (self);
+
+ BJB_RETURN (buffer);
+}
diff --git a/src/notes/bjb-note.h b/src/notes/bjb-note.h
new file mode 100644
index 0000000..adeac26
--- /dev/null
+++ b/src/notes/bjb-note.h
@@ -0,0 +1,57 @@
+/* bjb-note.h
+ *
+ * Copyright 2018 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 as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <glib-object.h>
+#include "bjb-item.h"
+
+G_BEGIN_DECLS
+
+#define BJB_TYPE_NOTE (bjb_note_get_type ())
+
+G_DECLARE_DERIVABLE_TYPE (BjbNote, bjb_note, BJB, NOTE, BjbItem)
+
+struct _BjbNoteClass
+{
+ BjbItemClass parent_class;
+
+ gchar *(*get_text_content) (BjbNote *self);
+ gchar *(*get_raw_content) (BjbNote *self);
+ void (*set_raw_content) (BjbNote *self,
+ const gchar *content);
+ void (*set_content_from_buffer) (BjbNote *self,
+ GtkTextBuffer *buffer);
+ GtkTextBuffer *(*get_buffer) (BjbNote *self);
+};
+
+gchar *bjb_note_get_text_content (BjbNote *self);
+void bjb_note_set_text_content (BjbNote *self,
+ const gchar *content);
+
+gchar *bjb_note_get_raw_content (BjbNote *self);
+void bjb_note_set_raw_content (BjbNote *self,
+ const gchar *content);
+
+void bjb_note_set_content_from_buffer (BjbNote *self,
+ GtkTextBuffer *buffer);
+GtkTextBuffer *bjb_note_get_buffer (BjbNote *self);
+
+G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]