[poppler] 2 commits - glib/demo glib/poppler-page.cc glib/poppler-page.h glib/reference

Carlos Garcia Campos carlosgc at kemper.freedesktop.org
Thu Jul 8 08:20:36 PDT 2010


 glib/demo/Makefile.am               |    2 
 glib/demo/main.c                    |    4 
 glib/demo/selections.c              |  687 ++++++++++++++++++++++++++++++++++++
 glib/demo/selections.h              |   31 +
 glib/demo/text.c                    |    9 
 glib/poppler-page.cc                |   43 +-
 glib/poppler-page.h                 |    3 
 glib/reference/poppler-sections.txt |    1 
 8 files changed, 759 insertions(+), 21 deletions(-)

New commits:
commit 550684731356a66753ec19f5a8ca4b572db6d2d5
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Thu Jul 8 17:16:02 2010 +0200

    [glib-demo] Add selections demo

diff --git a/glib/demo/Makefile.am b/glib/demo/Makefile.am
index fef1d5c..cf9e991 100644
--- a/glib/demo/Makefile.am
+++ b/glib/demo/Makefile.am
@@ -40,6 +40,8 @@ poppler_glib_demo_SOURCES = 			\
 	print.c					\
 	render.h				\
 	render.c				\
+	selections.h				\
+	selections.c				\
 	text.h					\
 	text.c					\
 	transitions.h				\
diff --git a/glib/demo/main.c b/glib/demo/main.c
index 206d29b..4b9a536 100644
--- a/glib/demo/main.c
+++ b/glib/demo/main.c
@@ -36,6 +36,7 @@
 #include "text.h"
 #include "find.h"
 #include "print.h"
+#include "selections.h"
 
 enum {
 	PGD_TITLE_COLUMN,
@@ -53,6 +54,7 @@ static const PopplerGlibDemo demo_list[] = {
 	{ "Info",             pgd_info_create_widget },
 	{ "Fonts",            pgd_fonts_create_widget },
 	{ "Render",           pgd_render_create_widget },
+	{ "Selections",       pgd_selections_create_widget },
 	{ "Page Info",        pgd_page_create_widget },
 	{ "Outline",          pgd_outline_create_widget },
 	{ "Links",            pgd_links_create_widget },
@@ -64,7 +66,7 @@ static const PopplerGlibDemo demo_list[] = {
 	{ "Layers",           pgd_layers_create_widget },
 	{ "Text",             pgd_text_create_widget },
 	{ "Find",             pgd_find_create_widget },
-	{ "Print",             pgd_print_create_widget }
+	{ "Print",            pgd_print_create_widget }
 };
 
 static void
diff --git a/glib/demo/selections.c b/glib/demo/selections.c
new file mode 100644
index 0000000..4dc9fb6
--- /dev/null
+++ b/glib/demo/selections.c
@@ -0,0 +1,687 @@
+/*
+ * Copyright (C) 2010 Carlos Garcia Campos  <carlosgc at gnome.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 2, 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "config.h"
+
+#include <gtk/gtk.h>
+#include <cairo.h>
+
+#include "selections.h"
+
+typedef struct {
+	PopplerDocument      *doc;
+
+	/* Properties */
+	gint                  page_index;
+	gdouble               scale;
+
+	GtkWidget            *swindow;
+	GtkWidget            *darea;
+	GtkWidget            *fg_color_button;
+	GtkWidget            *bg_color_button;
+
+	PopplerPage          *page;
+	cairo_surface_t      *surface;
+
+	GdkPoint              start;
+	GdkPoint              stop;
+	PopplerRectangle      doc_area;
+	cairo_surface_t      *selection_surface;
+	PopplerSelectionStyle style;
+	PopplerColor          glyph_color;
+	PopplerColor          background_color;
+	guint                 selections_idle;
+	GdkRegion            *selection_region;
+	GdkRegion            *selected_region;
+	GdkCursorType         cursor;
+	gchar                *selected_text;
+} PgdSelectionsDemo;
+
+static void
+pgd_selections_clear_selections (PgdSelectionsDemo *demo)
+{
+	demo->start.x = -1;
+
+	if (demo->selection_surface) {
+		cairo_surface_destroy (demo->selection_surface);
+		demo->selection_surface = NULL;
+	}
+
+	if (demo->selection_region) {
+		gdk_region_destroy (demo->selection_region);
+		demo->selection_region = NULL;
+	}
+
+	if (demo->selected_text) {
+		g_free (demo->selected_text);
+		demo->selected_text = NULL;
+	}
+
+	if (demo->selected_region) {
+		gdk_region_destroy (demo->selected_region);
+		demo->selected_region = NULL;
+	}
+}
+
+static void
+pgd_selections_free (PgdSelectionsDemo *demo)
+{
+	if (!demo)
+		return;
+
+	if (demo->selections_idle > 0) {
+		g_source_remove (demo->selections_idle);
+		demo->selections_idle = 0;
+	}
+
+	if (demo->doc) {
+		g_object_unref (demo->doc);
+		demo->doc = NULL;
+	}
+
+	if (demo->page) {
+		g_object_unref (demo->page);
+		demo->page = NULL;
+	}
+
+	if (demo->surface) {
+		cairo_surface_destroy (demo->surface);
+		demo->surface = NULL;
+	}
+
+	pgd_selections_clear_selections (demo);
+
+	g_free (demo);
+}
+
+static GdkRegion *
+pgd_selections_create_region (GList *region)
+{
+	GdkRegion *retval = gdk_region_new ();
+	GList     *l;
+
+	for (l = region; l; l = g_list_next (l)) {
+		PopplerRectangle *rectangle;
+		GdkRectangle      r;
+
+		rectangle = (PopplerRectangle *)l->data;
+
+		r.x = (gint) rectangle->x1;
+		r.y = (gint) rectangle->y1;
+		r.width  = (gint) (rectangle->x2 - rectangle->x1);
+		r.height = (gint) (rectangle->y2 - rectangle->y1);
+		gdk_region_union_with_rect (retval, &r);
+
+		poppler_rectangle_free (rectangle);
+	}
+
+	return retval;
+}
+
+static void
+pgd_selections_update_selection_region (PgdSelectionsDemo *demo)
+{
+	GList *region;
+	PopplerRectangle area = { 0, 0, 0, 0 };
+
+	if (demo->selection_region)
+		gdk_region_destroy (demo->selection_region);
+
+	poppler_page_get_size (demo->page, &area.x2, &area.y2);
+	region = poppler_page_get_selection_region (demo->page,
+						    1.0,
+						    POPPLER_SELECTION_GLYPH,
+						    &area);
+	demo->selection_region = pgd_selections_create_region (region);
+	g_list_free (region);
+}
+
+static void
+pgd_selections_update_seleted_text (PgdSelectionsDemo *demo)
+{
+	GList *region;
+	gchar *text;
+
+	if (demo->selected_region)
+		gdk_region_destroy (demo->selected_region);
+	region = poppler_page_get_selection_region (demo->page,
+						    1.0,
+						    demo->style,
+						    &demo->doc_area);
+	demo->selected_region = pgd_selections_create_region (region);
+	g_list_free (region);
+
+	if (demo->selected_text)
+		g_free (demo->selected_text);
+	demo->selected_text = NULL;
+
+	text = poppler_page_get_selected_text (demo->page,
+					       demo->style,
+					       &demo->doc_area);
+	if (text) {
+		demo->selected_text = g_utf8_normalize (text, -1, G_NORMALIZE_NFKC);
+		g_free (text);
+	}
+}
+
+static void
+pgd_selections_update_cursor (PgdSelectionsDemo *demo,
+			      GdkCursorType      cursor_type)
+{
+	GdkWindow *window = gtk_widget_get_window (demo->darea);
+	GdkCursor *cursor = NULL;
+
+	if (cursor_type == demo->cursor)
+		return;
+
+	if (cursor_type != GDK_LAST_CURSOR) {
+		cursor = gdk_cursor_new_for_display (gtk_widget_get_display (demo->darea),
+						     cursor_type);
+	}
+
+	demo->cursor = cursor_type;
+
+	gdk_window_set_cursor (window, cursor);
+	gdk_flush ();
+	if (cursor)
+		gdk_cursor_unref (cursor);
+}
+
+static gboolean
+pgd_selections_render_selections (PgdSelectionsDemo *demo)
+{
+	PopplerRectangle doc_area;
+	gdouble page_width, page_height;
+	cairo_t *cr;
+
+	if (!demo->page || demo->start.x == -1) {
+		demo->selections_idle = 0;
+
+		return FALSE;
+	}
+
+	poppler_page_get_size (demo->page, &page_width, &page_height);
+	page_width *= demo->scale;
+	page_height *= demo->scale;
+
+	doc_area.x1 = demo->start.x / demo->scale;
+	doc_area.y1 = demo->start.y / demo->scale;
+	doc_area.x2 = demo->stop.x / demo->scale;
+	doc_area.y2 = demo->stop.y / demo->scale;
+
+	if (demo->selection_surface)
+		cairo_surface_destroy (demo->selection_surface);
+	demo->selection_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+							      page_width, page_height);
+	cr = cairo_create (demo->selection_surface);
+	if (demo->scale != 1.0)
+		cairo_scale (cr, demo->scale, demo->scale);
+	poppler_page_render_selection (demo->page, cr,
+				       &doc_area, &demo->doc_area,
+				       demo->style,
+				       &demo->glyph_color,
+				       &demo->background_color);
+	cairo_destroy (cr);
+
+	demo->doc_area = doc_area;
+	gtk_widget_queue_draw (demo->darea);
+
+	demo->selections_idle = 0;
+
+	return FALSE;
+}
+
+static gboolean
+pgd_selections_drawing_area_expose (GtkWidget         *area,
+				    GdkEventExpose    *event,
+				    PgdSelectionsDemo *demo)
+{
+	cairo_t *cr;
+
+	if (!demo->surface)
+		return FALSE;
+
+	gdk_window_clear (gtk_widget_get_window (area));
+
+	cr = gdk_cairo_create (gtk_widget_get_window (area));
+
+	cairo_save (cr);
+	cairo_set_source_surface (cr, demo->surface, 0, 0);
+	cairo_paint (cr);
+	cairo_restore (cr);
+
+	if (demo->selection_surface) {
+		cairo_set_source_surface (cr, demo->selection_surface, 0, 0);
+		cairo_paint (cr);
+	}
+
+	cairo_destroy (cr);
+
+	return TRUE;
+}
+
+static gboolean
+pgd_selections_drawing_area_button_press (GtkWidget         *area,
+					  GdkEventButton    *event,
+					  PgdSelectionsDemo *demo)
+{
+	if (!demo->page)
+		return FALSE;
+
+	if (event->button != 1)
+		return FALSE;
+
+	demo->start.x = event->x;
+	demo->start.y = event->y;
+	demo->stop = demo->start;
+
+	switch (event->type) {
+	case GDK_2BUTTON_PRESS:
+		demo->style = POPPLER_SELECTION_WORD;
+		break;
+	case GDK_3BUTTON_PRESS:
+		demo->style = POPPLER_SELECTION_LINE;
+		break;
+	default:
+		demo->style = POPPLER_SELECTION_GLYPH;
+	}
+
+	pgd_selections_render_selections (demo);
+
+	return TRUE;
+}
+
+static gboolean
+pgd_selections_drawing_area_motion_notify (GtkWidget         *area,
+					   GdkEventMotion    *event,
+					   PgdSelectionsDemo *demo)
+{
+	if (!demo->page)
+		return FALSE;
+
+	if (demo->start.x != -1) {
+		demo->stop.x = event->x;
+		demo->stop.y = event->y;
+		if (demo->selections_idle == 0) {
+			demo->selections_idle =
+				g_idle_add ((GSourceFunc)pgd_selections_render_selections,
+					    demo);
+		}
+	} else {
+		gboolean over_text;
+
+		over_text = gdk_region_point_in (demo->selection_region,
+						 event->x / demo->scale,
+						 event->y / demo->scale);
+		pgd_selections_update_cursor (demo, over_text ? GDK_XTERM : GDK_LAST_CURSOR);
+	}
+
+	return TRUE;
+}
+
+static gboolean
+pgd_selections_drawing_area_button_release (GtkWidget         *area,
+					    GdkEventButton    *event,
+					    PgdSelectionsDemo *demo)
+{
+	if (!demo->page)
+		return FALSE;
+
+	if (event->button != 1)
+		return FALSE;
+
+	if (demo->start.x != -1)
+		pgd_selections_update_seleted_text (demo);
+
+	demo->start.x = -1;
+
+	if (demo->selections_idle > 0) {
+		g_source_remove (demo->selections_idle);
+		demo->selections_idle = 0;
+	}
+
+	return TRUE;
+}
+
+static void
+pgd_selections_drawing_area_realize (GtkWidget         *area,
+				     PgdSelectionsDemo *demo)
+{
+	GtkStyle *style = gtk_widget_get_style (area);
+
+	gtk_widget_add_events (area,
+			       GDK_POINTER_MOTION_HINT_MASK |
+			       GDK_BUTTON1_MOTION_MASK |
+			       GDK_BUTTON_PRESS_MASK |
+			       GDK_BUTTON_RELEASE_MASK);
+	g_object_set (area, "has-tooltip", TRUE, NULL);
+
+	gtk_color_button_set_color (GTK_COLOR_BUTTON (demo->fg_color_button),
+				    &style->text[GTK_STATE_SELECTED]);
+	gtk_color_button_set_color (GTK_COLOR_BUTTON (demo->bg_color_button),
+				    &style->base[GTK_STATE_SELECTED]);
+}
+
+static gboolean
+pgd_selections_drawing_area_query_tooltip (GtkWidget         *area,
+					   gint               x,
+					   gint               y,
+					   gboolean           keyboard_mode,
+					   GtkTooltip        *tooltip,
+					   PgdSelectionsDemo *demo)
+{
+	gboolean over_selection;
+
+	if (!demo->selected_text)
+		return FALSE;
+
+	over_selection = gdk_region_point_in (demo->selected_region,
+					      x / demo->scale,
+					      y / demo->scale);
+
+	if (over_selection) {
+		GdkRectangle selection_area;
+
+		gdk_region_get_clipbox (demo->selected_region, &selection_area);
+		selection_area.x *= demo->scale;
+		selection_area.y *= demo->scale;
+		selection_area.width *= demo->scale;
+		selection_area.height *= demo->scale;
+
+		gtk_tooltip_set_text (tooltip, demo->selected_text);
+		gtk_tooltip_set_tip_area (tooltip, &selection_area);
+
+		return TRUE;
+	}
+
+	return FALSE;
+}
+
+static void
+pgd_selections_render (GtkButton         *button,
+		       PgdSelectionsDemo *demo)
+{
+	gdouble  page_width, page_height;
+	cairo_t *cr;
+
+	if (!demo->page)
+		demo->page = poppler_document_get_page (demo->doc, demo->page_index);
+
+	if (!demo->page)
+		return;
+
+	pgd_selections_clear_selections (demo);
+	pgd_selections_update_selection_region (demo);
+
+	if (demo->surface)
+		cairo_surface_destroy (demo->surface);
+	demo->surface = NULL;
+
+	poppler_page_get_size (demo->page, &page_width, &page_height);
+	page_width *= demo->scale;
+	page_height *= demo->scale;
+
+	demo->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+						    page_width, page_height);
+	cr = cairo_create (demo->surface);
+
+	cairo_save (cr);
+
+	if (demo->scale != 1.0)
+		cairo_scale (cr, demo->scale, demo->scale);
+
+	poppler_page_render (demo->page, cr);
+	cairo_restore (cr);
+
+	cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
+	cairo_set_source_rgb (cr, 1., 1., 1.);
+	cairo_paint (cr);
+
+	cairo_destroy (cr);
+
+	gtk_widget_set_size_request (demo->darea, page_width, page_height);
+	gtk_widget_queue_draw (demo->darea);
+}
+
+static void
+pgd_selections_page_selector_value_changed (GtkSpinButton     *spinbutton,
+					    PgdSelectionsDemo *demo)
+{
+	demo->page_index = (gint)gtk_spin_button_get_value (spinbutton) - 1;
+	if (demo->page)
+		g_object_unref (demo->page);
+	demo->page = NULL;
+}
+
+static void
+pgd_selections_scale_selector_value_changed (GtkSpinButton     *spinbutton,
+					     PgdSelectionsDemo *demo)
+{
+	demo->scale = gtk_spin_button_get_value (spinbutton);
+}
+
+static void
+pgd_selections_fg_color_changed (GtkColorButton    *button,
+				 GParamSpec        *pspec,
+				 PgdSelectionsDemo *demo)
+{
+	GdkColor color;
+
+	gtk_color_button_get_color (GTK_COLOR_BUTTON (button), &color);
+	demo->glyph_color.red = color.red;
+	demo->glyph_color.green = color.green;
+	demo->glyph_color.blue = color.blue;
+}
+
+static void
+pgd_selections_bg_color_changed (GtkColorButton    *button,
+				 GParamSpec        *pspec,
+				 PgdSelectionsDemo *demo)
+{
+	GdkColor color;
+
+	gtk_color_button_get_color (GTK_COLOR_BUTTON (button), &color);
+	demo->background_color.red = color.red;
+	demo->background_color.green = color.green;
+	demo->background_color.blue = color.blue;
+}
+
+GtkWidget *
+pgd_selections_properties_selector_create (PgdSelectionsDemo *demo)
+{
+	GtkWidget *hbox, *vbox;
+	GtkWidget *label;
+	GtkWidget *page_hbox, *page_selector;
+	GtkWidget *scale_hbox, *scale_selector;
+	GtkWidget *rotate_hbox, *rotate_selector;
+	GtkWidget *color_hbox;
+	GtkWidget *button;
+	gint       n_pages;
+	gchar     *str;
+
+	n_pages = poppler_document_get_n_pages (demo->doc);
+
+	vbox = gtk_vbox_new (FALSE, 6);
+
+	hbox = gtk_hbox_new (FALSE, 12);
+	gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
+	gtk_widget_show (hbox);
+
+	page_hbox = gtk_hbox_new (FALSE, 6);
+
+	label = gtk_label_new ("Page:");
+	gtk_box_pack_start (GTK_BOX (page_hbox), label, TRUE, TRUE, 0);
+	gtk_widget_show (label);
+
+	page_selector = gtk_spin_button_new_with_range (1, n_pages, 1);
+	g_signal_connect (G_OBJECT (page_selector), "value-changed",
+			  G_CALLBACK (pgd_selections_page_selector_value_changed),
+			  (gpointer)demo);
+	gtk_box_pack_start (GTK_BOX (page_hbox), page_selector, TRUE, TRUE, 0);
+	gtk_widget_show (page_selector);
+
+	str = g_strdup_printf ("of %d", n_pages);
+	label = gtk_label_new (str);
+	gtk_box_pack_start (GTK_BOX (page_hbox), label, TRUE, TRUE, 0);
+	gtk_widget_show (label);
+	g_free (str);
+
+	gtk_box_pack_start (GTK_BOX (hbox), page_hbox, FALSE, TRUE, 0);
+	gtk_widget_show (page_hbox);
+
+	scale_hbox = gtk_hbox_new (FALSE, 6);
+
+	label = gtk_label_new ("Scale:");
+	gtk_box_pack_start (GTK_BOX (scale_hbox), label, TRUE, TRUE, 0);
+	gtk_widget_show (label);
+
+	scale_selector = gtk_spin_button_new_with_range (0, 10.0, 0.1);
+	gtk_spin_button_set_value (GTK_SPIN_BUTTON (scale_selector), 1.0);
+	g_signal_connect (G_OBJECT (scale_selector), "value-changed",
+			  G_CALLBACK (pgd_selections_scale_selector_value_changed),
+			  (gpointer)demo);
+	gtk_box_pack_start (GTK_BOX (scale_hbox), scale_selector, TRUE, TRUE, 0);
+	gtk_widget_show (scale_selector);
+
+	gtk_box_pack_start (GTK_BOX (hbox), scale_hbox, FALSE, TRUE, 0);
+	gtk_widget_show (scale_hbox);
+
+	rotate_hbox = gtk_hbox_new (FALSE, 6);
+
+	label = gtk_label_new ("Rotate:");
+	gtk_box_pack_start (GTK_BOX (rotate_hbox), label, TRUE, TRUE, 0);
+	gtk_widget_show (label);
+
+	rotate_selector = gtk_combo_box_new_text ();
+	gtk_combo_box_append_text (GTK_COMBO_BOX (rotate_selector), "0");
+	gtk_combo_box_append_text (GTK_COMBO_BOX (rotate_selector), "90");
+	gtk_combo_box_append_text (GTK_COMBO_BOX (rotate_selector), "180");
+	gtk_combo_box_append_text (GTK_COMBO_BOX (rotate_selector), "270");
+	gtk_combo_box_set_active (GTK_COMBO_BOX (rotate_selector), 0);
+#if 0
+	g_signal_connect (G_OBJECT (rotate_selector), "changed",
+			  G_CALLBACK (pgd_selections_rotate_selector_changed),
+			  (gpointer)demo);
+#endif
+	gtk_box_pack_start (GTK_BOX (rotate_hbox), rotate_selector, TRUE, TRUE, 0);
+	gtk_widget_show (rotate_selector);
+
+	gtk_box_pack_start (GTK_BOX (hbox), rotate_hbox, FALSE, TRUE, 0);
+	gtk_widget_show (rotate_hbox);
+
+	hbox = gtk_hbox_new (FALSE, 12);
+	gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
+	gtk_widget_show (hbox);
+
+	color_hbox = gtk_hbox_new (FALSE, 6);
+
+	label = gtk_label_new ("Foreground Color:");
+	gtk_box_pack_start (GTK_BOX (color_hbox), label, TRUE, TRUE, 0);
+	gtk_widget_show (label);
+
+	demo->fg_color_button = gtk_color_button_new ();
+	g_signal_connect (demo->fg_color_button, "notify::color",
+			  G_CALLBACK (pgd_selections_fg_color_changed),
+			  (gpointer)demo);
+	gtk_box_pack_start (GTK_BOX (color_hbox), demo->fg_color_button, TRUE, TRUE, 0);
+	gtk_widget_show (demo->fg_color_button);
+
+	gtk_box_pack_start (GTK_BOX (hbox), color_hbox, FALSE, TRUE, 0);
+	gtk_widget_show (color_hbox);
+
+	color_hbox = gtk_hbox_new (FALSE, 6);
+
+	label = gtk_label_new ("Background Color:");
+	gtk_box_pack_start (GTK_BOX (color_hbox), label, TRUE, TRUE, 0);
+	gtk_widget_show (label);
+
+	demo->bg_color_button = gtk_color_button_new ();
+	g_signal_connect (demo->bg_color_button, "notify::color",
+			  G_CALLBACK (pgd_selections_bg_color_changed),
+			  (gpointer)demo);
+	gtk_box_pack_start (GTK_BOX (color_hbox), demo->bg_color_button, TRUE, TRUE, 0);
+	gtk_widget_show (demo->bg_color_button);
+
+	gtk_box_pack_start (GTK_BOX (hbox), color_hbox, FALSE, TRUE, 0);
+	gtk_widget_show (color_hbox);
+
+	button = gtk_button_new_with_label ("Render");
+	g_signal_connect (G_OBJECT (button), "clicked",
+			  G_CALLBACK (pgd_selections_render),
+			  (gpointer)demo);
+	gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, TRUE, 0);
+	gtk_widget_show (button);
+
+	return vbox;
+}
+
+GtkWidget *
+pgd_selections_create_widget (PopplerDocument *document)
+{
+	PgdSelectionsDemo *demo;
+	GtkWidget         *vbox, *hbox;
+
+	demo = g_new0 (PgdSelectionsDemo, 1);
+
+	demo->doc = g_object_ref (document);
+	demo->scale = 1.0;
+	demo->cursor = GDK_LAST_CURSOR;
+
+	pgd_selections_clear_selections (demo);
+
+	vbox = gtk_vbox_new (FALSE, 6);
+
+	hbox = pgd_selections_properties_selector_create (demo);
+	gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 6);
+	gtk_widget_show (hbox);
+
+	demo->darea = gtk_drawing_area_new ();
+	g_signal_connect (demo->darea, "realize",
+			  G_CALLBACK (pgd_selections_drawing_area_realize),
+			  (gpointer)demo);
+	g_signal_connect (demo->darea, "expose_event",
+			  G_CALLBACK (pgd_selections_drawing_area_expose),
+			  (gpointer)demo);
+	g_signal_connect (demo->darea, "button_press_event",
+			  G_CALLBACK (pgd_selections_drawing_area_button_press),
+			  (gpointer)demo);
+	g_signal_connect (demo->darea, "motion_notify_event",
+			  G_CALLBACK (pgd_selections_drawing_area_motion_notify),
+			  (gpointer)demo);
+	g_signal_connect (demo->darea, "button_release_event",
+			  G_CALLBACK (pgd_selections_drawing_area_button_release),
+			  (gpointer)demo);
+	g_signal_connect (demo->darea, "query_tooltip",
+			  G_CALLBACK (pgd_selections_drawing_area_query_tooltip),
+			  (gpointer)demo);
+	demo->swindow = gtk_scrolled_window_new (NULL, NULL);
+	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (demo->swindow),
+					GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+	gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (demo->swindow),
+					       demo->darea);
+	gtk_widget_show (demo->darea);
+
+	gtk_box_pack_start (GTK_BOX (vbox), demo->swindow, TRUE, TRUE, 0);
+	gtk_widget_show (demo->swindow);
+
+	g_object_weak_ref (G_OBJECT (demo->swindow),
+			   (GWeakNotify)pgd_selections_free,
+			   (gpointer)demo);
+
+	return vbox;
+}
diff --git a/glib/demo/selections.h b/glib/demo/selections.h
new file mode 100644
index 0000000..61f8349
--- /dev/null
+++ b/glib/demo/selections.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2010 Carlos Garcia Campos  <carlosgc at gnome.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 2, 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <gtk/gtk.h>
+#include <poppler.h>
+
+#ifndef _SELECTIONS_H_
+#define _SELECTIONS_H_
+
+G_BEGIN_DECLS
+
+GtkWidget *pgd_selections_create_widget (PopplerDocument *document);
+
+G_END_DECLS
+
+#endif /* _SELECTIONS_H_ */
commit b257428150e2c13dcc24fd8f75e4ee2c679ab414
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Thu Jul 8 14:45:29 2010 +0200

    [glib] Add poppler_page_get_selected_text()
    
    And change poppler_page_get_text() to return the text of the whole
    page. For consistency with poppler_page_get_selection_region()
    get_seletect_text() doesn't invert the y coords as get_text() did. This
    change breaks the API. Users of poppler_page_get_text should:
    
     - Use the new poppler_page_get_text() if they want the text of the
       whole page
    
     - Use poppler_page_get_selected_text() if they want the text of the
       selected area. In this case they shouldn't invert the y coords anymore
       before calling the method.

diff --git a/glib/demo/text.c b/glib/demo/text.c
index b7a5c91..28e90ae 100644
--- a/glib/demo/text.c
+++ b/glib/demo/text.c
@@ -69,10 +69,8 @@ pgd_text_get_text (GtkWidget   *button,
 		   PgdTextDemo *demo)
 {
 	PopplerPage      *page;
-	PopplerRectangle  rect;
 	PopplerRectangle *recs = NULL;
 	guint             n_recs;
-	gdouble           width, height;
 	gchar            *text;
 	GTimer           *timer;
 	gint              i;
@@ -83,13 +81,8 @@ pgd_text_get_text (GtkWidget   *button,
 
 	gtk_list_store_clear (demo->model);
 
-	poppler_page_get_size (page, &width, &height);
-	rect.x1 = rect.y1 = 0;
-	rect.x2 = width;
-	rect.y2 = height;
-
 	timer = g_timer_new ();
-	text = poppler_page_get_text (page, POPPLER_SELECTION_GLYPH, &rect);
+	text = poppler_page_get_text (page);
 	g_timer_stop (timer);
 
 	if (text) {
diff --git a/glib/poppler-page.cc b/glib/poppler-page.cc
index 031edc7..5b35d8c 100644
--- a/glib/poppler-page.cc
+++ b/glib/poppler-page.cc
@@ -912,23 +912,23 @@ poppler_page_selection_region_free (GList *region)
 }
 
 /**
- * poppler_page_get_text:
+ * poppler_page_get_selected_text:
  * @page: a #PopplerPage
  * @style: a #PopplerSelectionStyle
  * @selection: the #PopplerRectangle including the text
- * 
+ *
  * Retrieves the contents of the specified @selection as text.
- * 
+ *
  * Return value: a pointer to the contents of the @selection
  *               as a string
+ * Since: 0.16
  **/
 char *
-poppler_page_get_text (PopplerPage          *page,
-		       PopplerSelectionStyle style,
-		       PopplerRectangle     *selection)
+poppler_page_get_selected_text (PopplerPage          *page,
+				PopplerSelectionStyle style,
+				PopplerRectangle     *selection)
 {
   GooString *sel_text;
-  double height;
   char *result;
   TextPage *text;
   SelectionStyle selection_style = selectionStyleGlyph;
@@ -937,11 +937,10 @@ poppler_page_get_text (PopplerPage          *page,
   g_return_val_if_fail (POPPLER_IS_PAGE (page), FALSE);
   g_return_val_if_fail (selection != NULL, NULL);
 
-  poppler_page_get_size (page, NULL, &height);
   pdf_selection.x1 = selection->x1;
-  pdf_selection.y1 = height - selection->y2;
+  pdf_selection.y1 = selection->y1;
   pdf_selection.x2 = selection->x2;
-  pdf_selection.y2 = height - selection->y1;
+  pdf_selection.y2 = selection->y2;
 
   switch (style)
     {
@@ -965,6 +964,28 @@ poppler_page_get_text (PopplerPage          *page,
 }
 
 /**
+ * poppler_page_get_text:
+ * @page: a #PopplerPage
+ *
+ * Retrieves the text of @page.
+ *
+ * Return value: a pointer to the text of the @page
+ *               as a string
+ * Since: 0.16
+ **/
+char *
+poppler_page_get_text (PopplerPage *page)
+{
+  PopplerRectangle rectangle = {0, 0, 0, 0};
+
+  g_return_val_if_fail (POPPLER_IS_PAGE (page), FALSE);
+
+  poppler_page_get_size (page, &rectangle.x2, &rectangle.y2);
+
+  return poppler_page_get_selected_text (page, POPPLER_SELECTION_GLYPH, &rectangle);
+}
+
+/**
  * poppler_page_find_text:
  * @page: a #PopplerPage
  * @text: the text to search for (UTF-8 encoded)
@@ -1908,7 +1929,7 @@ poppler_page_get_crop_box (PopplerPage *page, PopplerRectangle *rect)
  * This array must be freed with g_free () when done.
  *
  * The position in the array represents an offset in the text returned by
- * poppler_page_get_text
+ * poppler_page_get_text()
  *
  * Return value: %TRUE if the page contains text, %FALSE otherwise
  *
diff --git a/glib/poppler-page.h b/glib/poppler-page.h
index 785c48f..6af7d89 100644
--- a/glib/poppler-page.h
+++ b/glib/poppler-page.h
@@ -94,7 +94,8 @@ GList     	      *poppler_page_find_text            (PopplerPage        *page,
 							  const  char        *text);
 void                   poppler_page_render_to_ps         (PopplerPage        *page,
 							  PopplerPSFile      *ps_file);
-char                  *poppler_page_get_text             (PopplerPage        *page,
+char                  *poppler_page_get_text             (PopplerPage        *page);
+char                  *poppler_page_get_selected_text    (PopplerPage        *page,
 							  PopplerSelectionStyle style,
 							  PopplerRectangle   *selection);
 GList                 *poppler_page_get_selection_region (PopplerPage        *page,
diff --git a/glib/reference/poppler-sections.txt b/glib/reference/poppler-sections.txt
index 012eb28..ab91b9a 100644
--- a/glib/reference/poppler-sections.txt
+++ b/glib/reference/poppler-sections.txt
@@ -31,6 +31,7 @@ poppler_page_render_selection
 poppler_page_render_selection_to_pixbuf
 poppler_page_get_selection_region
 poppler_page_selection_region_free
+poppler_page_get_selected_text
 poppler_page_find_text
 poppler_page_get_text
 poppler_page_get_text_layout


More information about the poppler mailing list