[libgepub/widget-pagination] First attempt of real pagination in the widget
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgepub/widget-pagination] First attempt of real pagination in the widget
- Date: Fri, 1 Jul 2016 19:40:55 +0000 (UTC)
commit 848d51bb7f3903bbbe8c0fdd2ef0e65fc59bc598
Author: Daniel Garcia Moreno <danigm wadobo com>
Date: Fri Jul 1 21:39:26 2016 +0200
First attempt of real pagination in the widget
libgepub/gepub-widget.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++
libgepub/gepub-widget.h | 4 ++
tests/test-gepub.c | 35 +++++++++++++++--
3 files changed, 134 insertions(+), 4 deletions(-)
---
diff --git a/libgepub/gepub-widget.c b/libgepub/gepub-widget.c
index 7e7a239..5d5a846 100644
--- a/libgepub/gepub-widget.c
+++ b/libgepub/gepub-widget.c
@@ -26,6 +26,8 @@ struct _GepubWidget {
WebKitWebView parent;
GepubDoc *doc;
+ gboolean paginate;
+ gint page;
};
struct _GepubWidgetClass {
@@ -35,6 +37,7 @@ struct _GepubWidgetClass {
enum {
PROP_0,
PROP_DOC,
+ PROP_PAGINATE,
NUM_PROPS
};
@@ -87,6 +90,9 @@ gepub_widget_set_property (GObject *object,
case PROP_DOC:
gepub_widget_set_doc (widget, g_value_get_object (value));
break;
+ case PROP_PAGINATE:
+ gepub_widget_set_pagination (widget, g_value_get_boolean (value));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -105,6 +111,9 @@ gepub_widget_get_property (GObject *object,
case PROP_DOC:
g_value_set_object (value, gepub_widget_get_doc (widget));
break;
+ case PROP_PAGINATE:
+ g_value_set_boolean (value, widget->paginate);
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -156,6 +165,13 @@ gepub_widget_class_init (GepubWidgetClass *klass)
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
+ properties[PROP_PAGINATE] =
+ g_param_spec_boolean ("paginate",
+ "paginate",
+ "If the widget should paginate",
+ FALSE,
+ G_PARAM_READWRITE);
+
g_object_class_install_properties (object_class, NUM_PROPS, properties);
}
@@ -184,6 +200,48 @@ gepub_widget_get_doc (GepubWidget *widget)
}
static void
+paginate_cb (WebKitWebView *web_view,
+ WebKitLoadEvent load_event,
+ gpointer user_data)
+{
+
+ GepubWidget *widget = GEPUB_WIDGET (web_view);
+
+ if (load_event == WEBKIT_LOAD_FINISHED) {
+ const gchar *script = "function initialize() { "
+ "var d = document.querySelector('body');"
+ "var ourH = window.innerHeight - 40; "
+ "var ourW = window.innerWidth - 20; "
+ "var fullH = d.offsetHeight; "
+ "var pageCount = Math.floor(fullH/ourH)+1;"
+ "var newW = pageCount * ourW; "
+ "d.style.height = ourH+'px';"
+ "d.style.width = newW+'px';"
+ "d.style.WebkitColumnCount = pageCount;"
+ "d.style.WebkitColumnGap = '20px';"
+ "d.style.overflow = 'hidden';"
+ "window.currentPage = 0; "
+ "};"
+ "function next() { "
+ "var ourW = window.innerWidth - 10; "
+ "window.currentPage += 1; "
+ "window.scroll(ourW * window.currentPage, 0); "
+ "};"
+ "function prev() { "
+ "var ourW = window.innerWidth - 10; "
+ "window.currentPage -= 1; "
+ "window.scroll(ourW * window.currentPage, 0); "
+ "};"
+ "initialize();";
+
+ if (widget->paginate) {
+ webkit_web_view_run_javascript(web_view, "document.querySelector('body').style.margin =
'20px';", NULL, NULL, NULL);
+ webkit_web_view_run_javascript(web_view, script, NULL, NULL, NULL);
+ }
+ }
+}
+
+static void
reload_current_chapter (GepubWidget *widget)
{
GBytes *current;
@@ -194,6 +252,8 @@ reload_current_chapter (GepubWidget *widget)
gepub_doc_get_current_mime (widget->doc),
"UTF-8", NULL);
+ g_signal_connect (widget, "load-changed", G_CALLBACK (paginate_cb), NULL);
+
g_bytes_unref (current);
}
@@ -228,3 +288,42 @@ gepub_widget_set_doc (GepubWidget *widget,
g_object_notify_by_pspec (G_OBJECT (widget), properties[PROP_DOC]);
}
+
+/**
+ * gepub_widget_set_pagination:
+ * @widget: a #GepubWidget
+ * @p: true if the widget should paginate
+ *
+ * Enable or disable pagination
+ */
+void
+gepub_widget_set_pagination (GepubWidget *widget,
+ gboolean p)
+{
+ widget->paginate = p;
+ reload_current_chapter (widget);
+}
+
+/**
+ * gepub_widget_page_next:
+ * @widget: a #GepubWidget
+ *
+ * Change the page to the next
+ */
+void
+gepub_widget_page_next (GepubWidget *widget)
+{
+ webkit_web_view_run_javascript(WEBKIT_WEB_VIEW (widget), "next();", NULL, NULL, NULL);
+}
+
+/**
+ * gepub_widget_page_prev:
+ * @widget: a #GepubWidget
+ *
+ * Change the page to the prev
+ */
+void
+gepub_widget_page_prev (GepubWidget *widget)
+{
+ webkit_web_view_run_javascript(WEBKIT_WEB_VIEW (widget), "prev();", NULL, NULL, NULL);
+}
diff --git a/libgepub/gepub-widget.h b/libgepub/gepub-widget.h
index b77cc6a..5c4ce84 100644
--- a/libgepub/gepub-widget.h
+++ b/libgepub/gepub-widget.h
@@ -46,6 +46,10 @@ GepubDoc *gepub_widget_get_doc (GepubWidget *wid
void gepub_widget_set_doc (GepubWidget *widget,
GepubDoc *doc);
+void gepub_widget_set_pagination (GepubWidget *widget, gboolean p);
+void gepub_widget_page_next (GepubWidget *widget);
+void gepub_widget_page_prev (GepubWidget *widget);
+
G_END_DECLS
#endif /* __GEPUB_WIDGET_H__ */
diff --git a/tests/test-gepub.c b/tests/test-gepub.c
index 20048d4..4aa5f29 100644
--- a/tests/test-gepub.c
+++ b/tests/test-gepub.c
@@ -69,11 +69,19 @@ void
button_pressed (GtkButton *button, GepubWidget *widget)
{
GepubDoc *doc = gepub_widget_get_doc (widget);
+ printf("CLICKED %s\n", gtk_button_get_label (button));
- if (!strcmp (gtk_button_get_label (button), "prev")) {
+ if (!strcmp (gtk_button_get_label (button), "< chapter")) {
gepub_doc_go_prev (doc);
- } else {
+ } else if (!strcmp (gtk_button_get_label (button), "chapter >")) {
gepub_doc_go_next (doc);
+ } else if (!strcmp (gtk_button_get_label (button), "< page")) {
+ gepub_widget_page_prev (widget);
+ } else if (!strcmp (gtk_button_get_label (button), "page >")) {
+ gepub_widget_page_next (widget);
+ } else if (!strcmp (gtk_button_get_label (button), "paginated")) {
+ gboolean b = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
+ gepub_widget_set_pagination (widget, b);
}
update_text (doc);
print_replaced_text (doc);
@@ -245,6 +253,11 @@ main (int argc, char **argv)
GtkWidget *b_next;
GtkWidget *b_prev;
+ GtkWidget *b_next2;
+ GtkWidget *b_prev2;
+
+ GtkWidget *paginate;
+
GtkTextBuffer *buffer;
GepubDoc *doc;
@@ -285,12 +298,26 @@ main (int argc, char **argv)
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
- b_prev = gtk_button_new_with_label ("prev");
+ b_prev = gtk_button_new_with_label ("< chapter");
g_signal_connect (b_prev, "clicked", (GCallback)button_pressed, GEPUB_WIDGET (widget));
- b_next = gtk_button_new_with_label ("next");
+ b_next = gtk_button_new_with_label ("chapter >");
g_signal_connect (b_next, "clicked", (GCallback)button_pressed, GEPUB_WIDGET (widget));
+
+ b_prev2 = gtk_button_new_with_label ("< page");
+ g_signal_connect (b_prev2, "clicked", (GCallback)button_pressed, GEPUB_WIDGET (widget));
+ b_next2 = gtk_button_new_with_label ("page >");
+ g_signal_connect (b_next2, "clicked", (GCallback)button_pressed, GEPUB_WIDGET (widget));
+
+ paginate = gtk_check_button_new_with_label ("paginated");
+ g_signal_connect (paginate, "clicked", (GCallback)button_pressed, GEPUB_WIDGET (widget));
+
gtk_container_add (GTK_CONTAINER (hbox), b_prev);
gtk_container_add (GTK_CONTAINER (hbox), b_next);
+
+ gtk_container_add (GTK_CONTAINER (hbox), b_prev2);
+ gtk_container_add (GTK_CONTAINER (hbox), b_next2);
+ gtk_container_add (GTK_CONTAINER (hbox), paginate);
+
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 5);
gtk_box_pack_start (GTK_BOX (vbox), scrolled, TRUE, TRUE, 5);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]