annots.c 46 KB
Newer Older
Inigo Martínez's avatar
Inigo Martínez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (C) 2008 Inigo Martinez <inigomartinez@gmail.com>
 *
 * 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 <string.h>

#include "annots.h"
#include "utils.h"

enum {
    ANNOTS_TYPE_COLUMN,
Inigo Martínez's avatar
Inigo Martínez committed
27
    ANNOTS_COLOR_COLUMN,
28 29 30
    ANNOTS_FLAG_INVISIBLE_COLUMN,
    ANNOTS_FLAG_HIDDEN_COLUMN,
    ANNOTS_FLAG_PRINT_COLUMN,
Inigo Martínez's avatar
Inigo Martínez committed
31 32 33 34
    ANNOTS_COLUMN,
    N_COLUMNS
};

35 36 37 38 39 40
enum {
    SELECTED_TYPE_COLUMN,
    SELECTED_LABEL_COLUMN,
    SELECTED_N_COLUMNS
};

41 42 43 44 45 46
typedef struct
{
  const guint  type;
  const gchar *label;
} Annotations;

Albert Astals Cid's avatar
Albert Astals Cid committed
47
static const Annotations supported_annots[] = {
48 49 50 51 52 53 54 55 56 57
    { POPPLER_ANNOT_TEXT,       "Text" },
    { POPPLER_ANNOT_LINE,       "Line" },
    { POPPLER_ANNOT_SQUARE,     "Square" },
    { POPPLER_ANNOT_CIRCLE,     "Circle" },
    { POPPLER_ANNOT_HIGHLIGHT,  "Highlight" },
    { POPPLER_ANNOT_UNDERLINE,  "Underline" },
    { POPPLER_ANNOT_SQUIGGLY,   "Squiggly" },
    { POPPLER_ANNOT_STRIKE_OUT, "Strike Out" },
};

58 59 60 61 62 63 64
typedef enum {
    MODE_NORMAL,  /* Regular use as pointer in the page */
    MODE_ADD,     /* To add simple annotations */
    MODE_EDIT,    /* To move/edit an annotation */
    MODE_DRAWING  /* To add annotations that require mouse interactions */
} ModeType;

Inigo Martínez's avatar
Inigo Martínez committed
65 66 67
typedef struct {
    PopplerDocument *doc;
    PopplerPage     *page;
68
    PopplerAnnot    *active_annot;
Inigo Martínez's avatar
Inigo Martínez committed
69

70
    GtkWidget       *tree_view;
Inigo Martínez's avatar
Inigo Martínez committed
71
    GtkListStore    *model;
72
    GtkWidget       *darea;
Inigo Martínez's avatar
Inigo Martínez committed
73 74
    GtkWidget       *annot_view;
    GtkWidget       *timer_label;
75
    GtkWidget       *remove_button;
76 77
    GtkWidget       *type_selector;
    GtkWidget       *main_box;
Inigo Martínez's avatar
Inigo Martínez committed
78 79

    gint             num_page;
80 81
    gint             annot_type;
    ModeType         mode;
82 83

    cairo_surface_t *surface;
84
    GdkRGBA          annot_color;
85 86 87 88

    GdkPoint         start;
    GdkPoint         stop;
    GdkCursorType    cursor;
89
    guint            annotations_idle;
Inigo Martínez's avatar
Inigo Martínez committed
90 91
} PgdAnnotsDemo;

92 93
static void pgd_annots_viewer_queue_redraw (PgdAnnotsDemo *demo);

Inigo Martínez's avatar
Inigo Martínez committed
94 95 96 97 98 99
static void
pgd_annots_free (PgdAnnotsDemo *demo)
{
    if (!demo)
        return;

100 101 102 103 104
    if (demo->annotations_idle > 0) {
        g_source_remove (demo->annotations_idle);
        demo->annotations_idle = 0;
    }

Inigo Martínez's avatar
Inigo Martínez committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    if (demo->doc) {
        g_object_unref (demo->doc);
        demo->doc = NULL;
    }

    if (demo->page) {
        g_object_unref (demo->page);
        demo->page = NULL;
    }

    if (demo->model) {
        g_object_unref (demo->model);
        demo->model = NULL;
    }

    g_free (demo);
}

static GtkWidget *
pgd_annot_view_new (void)
{
    GtkWidget  *frame, *label;

    frame = gtk_frame_new (NULL);
    gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
    label = gtk_label_new (NULL);
131
    gtk_label_set_markup (GTK_LABEL (label), "<b>Annotation Properties</b>");
Inigo Martínez's avatar
Inigo Martínez committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    gtk_frame_set_label_widget (GTK_FRAME (frame), label);
    gtk_widget_show (label);

    return frame;
}

const gchar *
get_annot_type (PopplerAnnot *poppler_annot)
{
    switch (poppler_annot_get_annot_type (poppler_annot))
    {
      case POPPLER_ANNOT_TEXT:
        return "Text";
      case POPPLER_ANNOT_LINK:
        return "Link";
      case POPPLER_ANNOT_FREE_TEXT:
        return "Free Text";
      case POPPLER_ANNOT_LINE:
        return "Line";
      case POPPLER_ANNOT_SQUARE:
        return "Square";
      case POPPLER_ANNOT_CIRCLE:
        return "Circle";
      case POPPLER_ANNOT_POLYGON:
        return "Polygon";
      case POPPLER_ANNOT_POLY_LINE:
        return "Poly Line";
      case POPPLER_ANNOT_HIGHLIGHT:
        return "Highlight";
      case POPPLER_ANNOT_UNDERLINE:
        return "Underline";
      case POPPLER_ANNOT_SQUIGGLY:
        return "Squiggly";
      case POPPLER_ANNOT_STRIKE_OUT:
        return "Strike Out";
      case POPPLER_ANNOT_STAMP:
        return "Stamp";
      case POPPLER_ANNOT_CARET:
        return "Caret";
      case POPPLER_ANNOT_INK:
        return "Ink";
      case POPPLER_ANNOT_POPUP:
        return "Popup";
      case POPPLER_ANNOT_FILE_ATTACHMENT:
        return "File Attachment";
      case POPPLER_ANNOT_SOUND:
        return "Sound";
      case POPPLER_ANNOT_MOVIE:
        return "Movie";
      case POPPLER_ANNOT_WIDGET:
        return "Widget";
      case POPPLER_ANNOT_SCREEN:
        return "Screen";
      case POPPLER_ANNOT_PRINTER_MARK:
        return "Printer Mark";
      case POPPLER_ANNOT_TRAP_NET:
        return "Trap Net";
      case POPPLER_ANNOT_WATERMARK:
        return "Watermark";
      case POPPLER_ANNOT_3D:
        return "3D";
      default:
        break;
  }

  return "Unknown";
}

Inigo Martínez's avatar
Inigo Martínez committed
200 201 202 203 204 205
GdkPixbuf *
get_annot_color (PopplerAnnot *poppler_annot)
{
    PopplerColor *poppler_color;

    if ((poppler_color = poppler_annot_get_color (poppler_annot))) {
206 207 208 209 210 211
        GdkPixbuf *pixbuf_tmp, *pixbuf;

        pixbuf_tmp = pgd_pixbuf_new_for_color (poppler_color);
        pixbuf = gdk_pixbuf_scale_simple(pixbuf_tmp, 16, 16, GDK_INTERP_BILINEAR);
        g_object_unref (pixbuf_tmp);

Inigo Martínez's avatar
Inigo Martínez committed
212 213 214 215 216 217 218 219 220 221 222 223
        g_free (poppler_color);

        return pixbuf;
    }

    return NULL;
}

gchar *
get_markup_date (PopplerAnnotMarkup *poppler_annot)
{
    GDate *date;
224 225
    struct tm t;
    time_t timet;
Inigo Martínez's avatar
Inigo Martínez committed
226

227 228 229
    date = poppler_annot_markup_get_date (poppler_annot);
    if (!date)
	    return NULL;
Inigo Martínez's avatar
Inigo Martínez committed
230

231 232
    g_date_to_struct_tm (date, &t);
    g_date_free (date);
Inigo Martínez's avatar
Inigo Martínez committed
233

234 235
    timet = mktime (&t);
    return timet == (time_t) - 1 ? NULL : pgd_format_date (timet);
Inigo Martínez's avatar
Inigo Martínez committed
236 237
}

Inigo Martínez's avatar
Inigo Martínez committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
const gchar *
get_markup_reply_to (PopplerAnnotMarkup *poppler_annot)
{
    switch (poppler_annot_markup_get_reply_to (poppler_annot))
    {
      case POPPLER_ANNOT_MARKUP_REPLY_TYPE_R:
        return "Type R";
      case POPPLER_ANNOT_MARKUP_REPLY_TYPE_GROUP:
        return "Type Group";
      default:
        break;
    }

  return "Unknown";
}

const gchar *
get_markup_external_data (PopplerAnnotMarkup *poppler_annot)
{
    switch (poppler_annot_markup_get_external_data (poppler_annot))
    {
      case POPPLER_ANNOT_EXTERNAL_DATA_MARKUP_3D:
        return "Markup 3D";
      default:
        break;
    }

  return "Unknown";
}

Inigo Martínez's avatar
Inigo Martínez committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
const gchar *
get_text_state (PopplerAnnotText *poppler_annot)
{
    switch (poppler_annot_text_get_state (poppler_annot))
    {
      case POPPLER_ANNOT_TEXT_STATE_MARKED:
        return "Marked";
      case POPPLER_ANNOT_TEXT_STATE_UNMARKED:
        return "Unmarked";
      case POPPLER_ANNOT_TEXT_STATE_ACCEPTED:
        return "Accepted";
      case POPPLER_ANNOT_TEXT_STATE_REJECTED:
        return "Rejected";
      case POPPLER_ANNOT_TEXT_STATE_CANCELLED:
        return "Cancelled";
      case POPPLER_ANNOT_TEXT_STATE_COMPLETED:
        return "Completed";
      case POPPLER_ANNOT_TEXT_STATE_NONE:
        return "None";
      case POPPLER_ANNOT_TEXT_STATE_UNKNOWN:
        return "Unknown";
      default:
        break;
    }

  return "Unknown";
}

const gchar *
get_free_text_quadding (PopplerAnnotFreeText *poppler_annot)
{
    switch (poppler_annot_free_text_get_quadding (poppler_annot))
    {
      case POPPLER_ANNOT_FREE_TEXT_QUADDING_LEFT_JUSTIFIED:
        return "Left Justified";
      case POPPLER_ANNOT_FREE_TEXT_QUADDING_CENTERED:
        return "Centered";
      case POPPLER_ANNOT_FREE_TEXT_QUADDING_RIGHT_JUSTIFIED:
        return "Right Justified";
      default:
        break;
    }

  return "Unknown";
}

gchar *
get_free_text_callout_line (PopplerAnnotFreeText *poppler_annot)
{
    PopplerAnnotCalloutLine *callout;
    gchar *text;
    
    if ((callout = poppler_annot_free_text_get_callout_line (poppler_annot))) {
        text = g_strdup_printf ("%f,%f,%f,%f", callout->x1,
                                               callout->y1,
                                               callout->x2,
                                               callout->y2);
        if (callout->multiline)
            text = g_strdup_printf ("%s,%f,%f", text,
                                                callout->x3,
                                                callout->y3);

        return text;
    }
    
    return NULL;
}

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
static void
pgd_annots_update_cursor (PgdAnnotsDemo *demo,
                          GdkCursorType  cursor_type)
{
    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->main_box),
                                             cursor_type);
     }

     demo->cursor = cursor_type;

     gdk_window_set_cursor (gtk_widget_get_window (demo->main_box), cursor);
353
     gdk_display_flush (gtk_widget_get_display (demo->main_box));
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
     if (cursor)
         g_object_unref (cursor);
}

static void
pgd_annots_start_add_annot (GtkWidget     *button,
                            PgdAnnotsDemo *demo)
{
    GtkTreeModel *model;
    GtkTreeIter   iter;

    gtk_combo_box_get_active_iter (GTK_COMBO_BOX (demo->type_selector), &iter);
    model = gtk_combo_box_get_model (GTK_COMBO_BOX (demo->type_selector));
    gtk_tree_model_get (model, &iter,
                        SELECTED_TYPE_COLUMN, &(demo->annot_type),
                        -1);

    demo->mode = MODE_ADD;
    pgd_annots_update_cursor (demo, GDK_TCROSS);
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
static void
pgd_annots_remove_annot (GtkWidget     *button,
                         PgdAnnotsDemo *demo)
{
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeIter   iter;

    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (demo->tree_view));

    if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
        PopplerAnnot *annot;

        gtk_tree_model_get (model, &iter,
                            ANNOTS_COLUMN, &annot,
                           -1);
        poppler_page_remove_annot (demo->page, annot);
        g_object_unref (annot);
        gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
394 395

        pgd_annots_viewer_queue_redraw (demo);
396 397 398
    }
}

Inigo Martínez's avatar
Inigo Martínez committed
399 400 401 402 403 404
static void
pgd_annot_view_set_annot_markup (GtkWidget          *table,
                                 PopplerAnnotMarkup *markup,
                                 gint               *row)
{
    gchar *text;
405
    PopplerRectangle rect;
Inigo Martínez's avatar
Inigo Martínez committed
406 407

    text = poppler_annot_markup_get_label (markup);
408
    pgd_table_add_property (GTK_GRID (table), "<b>Label:</b>", text, row);
Inigo Martínez's avatar
Inigo Martínez committed
409 410
    g_free (text);

411
    if (poppler_annot_markup_has_popup (markup)) {
412
	    pgd_table_add_property (GTK_GRID (table), "<b>Popup is open:</b>",
413
				    poppler_annot_markup_get_popup_is_open (markup) ? "Yes" : "No", row);
Inigo Martínez's avatar
Inigo Martínez committed
414

415
	    poppler_annot_markup_get_popup_rectangle (markup, &rect);
416 417
	    text = g_strdup_printf ("X1: %.2f, Y1: %.2f, X2: %.2f, Y2: %.2f",
				    rect.x1, rect.y1, rect.x2, rect.y2);
418
	    pgd_table_add_property (GTK_GRID (table), "<b>Popup Rectangle:</b>", text, row);
419 420 421
	    g_free (text);
    }

Inigo Martínez's avatar
Inigo Martínez committed
422
    text = g_strdup_printf ("%f", poppler_annot_markup_get_opacity (markup));
423
    pgd_table_add_property (GTK_GRID (table), "<b>Opacity:</b>", text, row);
Inigo Martínez's avatar
Inigo Martínez committed
424 425 426
    g_free (text);

    text = get_markup_date (markup);
427
    pgd_table_add_property (GTK_GRID (table), "<b>Date:</b>", text, row);
Inigo Martínez's avatar
Inigo Martínez committed
428 429 430
    g_free (text);

    text = poppler_annot_markup_get_subject (markup);
431
    pgd_table_add_property (GTK_GRID (table), "<b>Subject:</b>", text, row);
Inigo Martínez's avatar
Inigo Martínez committed
432 433
    g_free (text);

434
    pgd_table_add_property (GTK_GRID (table), "<b>Reply To:</b>", get_markup_reply_to (markup), row);
Inigo Martínez's avatar
Inigo Martínez committed
435

436
    pgd_table_add_property (GTK_GRID (table), "<b>External Data:</b>", get_markup_external_data (markup), row);
Inigo Martínez's avatar
Inigo Martínez committed
437 438 439 440 441 442 443
}

static void
pgd_annot_view_set_annot_text (GtkWidget        *table,
                               PopplerAnnotText *annot,
                               gint             *row)
{
Inigo Martínez's avatar
Inigo Martínez committed
444 445
    gchar *text;

446
    pgd_table_add_property (GTK_GRID (table), "<b>Is open:</b>",
Inigo Martínez's avatar
Inigo Martínez committed
447 448
                            poppler_annot_text_get_is_open (annot) ? "Yes" : "No", row);

Inigo Martínez's avatar
Inigo Martínez committed
449
    text = poppler_annot_text_get_icon (annot);
450
    pgd_table_add_property (GTK_GRID (table), "<b>Icon:</b>", text, row);
Inigo Martínez's avatar
Inigo Martínez committed
451
    g_free (text);
Inigo Martínez's avatar
Inigo Martínez committed
452

453
    pgd_table_add_property (GTK_GRID (table), "<b>State:</b>", get_text_state (annot), row);
Inigo Martínez's avatar
Inigo Martínez committed
454 455
}

456 457 458 459 460 461 462 463
static void
pgd_annot_color_changed (GtkButton     *button,
                         GParamSpec    *pspec,
                         PgdAnnotsDemo *demo)
{
    gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (button), &demo->annot_color);
}

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
static void
pgd_annot_view_set_annot_text_markup (GtkWidget              *table,
                                      PopplerAnnotTextMarkup *annot,
                                      gint                   *row)
{
    gint                  i;
    gchar                *text = NULL;
    gchar                *prev_text = NULL;
    GArray               *quads_array = NULL;
    PopplerQuadrilateral  quadrilateral;

    quads_array = poppler_annot_text_markup_get_quadrilaterals (annot);

    prev_text = g_strdup ("");
    for (i = 0; i < quads_array->len; i++) {
        quadrilateral = g_array_index (quads_array, PopplerQuadrilateral, i);

        text = g_strdup_printf ("%s%2d:(%.2f,%.2f) (%.2f,%.2f)\n"
                                "    (%.2f,%.2f) (%.2f,%.2f)\n",
                                prev_text, i+1,
                                quadrilateral.p1.x, quadrilateral.p1.y,
                                quadrilateral.p2.x, quadrilateral.p2.y,
                                quadrilateral.p3.x, quadrilateral.p3.y,
                                quadrilateral.p4.x, quadrilateral.p4.y);
        g_free (prev_text);
        prev_text = text;
    }

    text = g_strchomp (text);
    pgd_table_add_property (GTK_GRID (table), "<b>Quadrilaterals:</b>", text, row);

    g_array_free (quads_array, TRUE);
    g_free (text);
}


Inigo Martínez's avatar
Inigo Martínez committed
500 501 502 503 504 505 506
static void
pgd_annot_view_set_annot_free_text (GtkWidget            *table,
                                    PopplerAnnotFreeText *annot,
                                    gint                 *row)
{
    gchar *text;

507
    pgd_table_add_property (GTK_GRID (table), "<b>Quadding:</b>", get_free_text_quadding (annot), row);
Inigo Martínez's avatar
Inigo Martínez committed
508 509

    text = get_free_text_callout_line (annot);
510
    pgd_table_add_property (GTK_GRID (table), "<b>Callout:</b>", text, row);
Inigo Martínez's avatar
Inigo Martínez committed
511 512 513
    g_free (text);
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
static void
pgd_annots_file_attachment_save_dialog_response (GtkFileChooser    *file_chooser,
						 gint               response,
						 PopplerAttachment *attachment)
{
    gchar  *filename;
    GError *error = NULL;

    if (response != GTK_RESPONSE_ACCEPT) {
        g_object_unref (attachment);
	gtk_widget_destroy (GTK_WIDGET (file_chooser));
	return;
    }

    filename = gtk_file_chooser_get_filename (file_chooser);
    if (!poppler_attachment_save (attachment, filename, &error)) {
        g_warning ("%s", error->message);
	g_error_free (error);
    }
    g_free (filename);
    g_object_unref (attachment);
    gtk_widget_destroy (GTK_WIDGET (file_chooser));
}

static void
pgd_annot_save_file_attachment_button_clicked (GtkButton                  *button,
					       PopplerAnnotFileAttachment *annot)
{
    GtkWidget         *file_chooser;
    PopplerAttachment *attachment;

    attachment = poppler_annot_file_attachment_get_attachment (annot);
    if (!attachment)
        return;

    file_chooser = gtk_file_chooser_dialog_new ("Save attachment",
						GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (button))),
						GTK_FILE_CHOOSER_ACTION_SAVE,
552 553
						"_Cancel", GTK_RESPONSE_CANCEL,
						"_Save", GTK_RESPONSE_ACCEPT,
554 555 556 557 558 559 560 561 562 563 564 565 566 567
						NULL);
    gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (file_chooser), attachment->name);
    g_signal_connect (G_OBJECT (file_chooser), "response",
		      G_CALLBACK (pgd_annots_file_attachment_save_dialog_response),
		      (gpointer) attachment);
    gtk_widget_show (file_chooser);
}

static void
pgd_annot_view_set_annot_file_attachment (GtkWidget                  *table,
					  PopplerAnnotFileAttachment *annot,
					  gint                       *row)
{
    GtkWidget *button;
568 569 570
    gchar *text;

    text = poppler_annot_file_attachment_get_name (annot);
571
    pgd_table_add_property (GTK_GRID (table), "<b>Attachment Name:</b>", text, row);
572
    g_free (text);
573 574 575 576 577

    button = gtk_button_new_with_label ("Save Attachment");
    g_signal_connect (G_OBJECT (button), "clicked",
		      G_CALLBACK (pgd_annot_save_file_attachment_button_clicked),
		      (gpointer)annot);
578
    pgd_table_add_property_with_custom_widget (GTK_GRID (table), "<b>File Attachment:</b>", button, row);
579 580 581 582
    gtk_widget_show (button);

}

583 584 585 586 587 588 589 590 591
static void
pgd_annot_view_set_annot_movie (GtkWidget         *table,
				PopplerAnnotMovie *annot,
				gint              *row)
{
    GtkWidget *movie_view;
    gchar *text;

    text = poppler_annot_movie_get_title (annot);
592
    pgd_table_add_property (GTK_GRID (table), "<b>Movie Title:</b>", text, row);
593 594 595 596
    g_free (text);

    movie_view = pgd_movie_view_new ();
    pgd_movie_view_set_movie (movie_view, poppler_annot_movie_get_movie (annot));
597
    pgd_table_add_property_with_custom_widget (GTK_GRID (table), "<b>Movie:</b>", movie_view, row);
598 599 600
    gtk_widget_show (movie_view);
}

601 602 603 604 605 606 607 608 609
static void
pgd_annot_view_set_annot_screen (GtkWidget          *table,
				 PopplerAnnotScreen *annot,
				 gint               *row)
{
    GtkWidget *action_view;

    action_view = pgd_action_view_new (NULL);
    pgd_action_view_set_action (action_view, poppler_annot_screen_get_action (annot));
610
    pgd_table_add_property_with_custom_widget (GTK_GRID (table), "<b>Action:</b>", action_view, row);
611 612 613
    gtk_widget_show (action_view);
}

Inigo Martínez's avatar
Inigo Martínez committed
614
static void
615 616
pgd_annot_view_set_annot (PgdAnnotsDemo *demo,
                          PopplerAnnot  *annot)
Inigo Martínez's avatar
Inigo Martínez committed
617 618 619
{
    GtkWidget  *table;
    gint        row = 0;
620
    gchar      *text;
621
    time_t      timet;
622
    PopplerRectangle rect;
Inigo Martínez's avatar
Inigo Martínez committed
623

624 625 626
    table = gtk_bin_get_child (GTK_BIN (demo->annot_view));
    if (table) {
        gtk_container_remove (GTK_CONTAINER (demo->annot_view), table);
Inigo Martínez's avatar
Inigo Martínez committed
627 628 629 630 631
    }

    if (!annot)
        return;

632
    table = gtk_grid_new ();
633 634 635 636 637 638 639 640 641
    gtk_widget_set_margin_top (table, 5);
    gtk_widget_set_margin_bottom (table, 5);
#if GTK_CHECK_VERSION(3, 12, 0)
    gtk_widget_set_margin_start (table, 8);
    gtk_widget_set_margin_end (table, 5);
#else
    gtk_widget_set_margin_left (table, 8);
    gtk_widget_set_margin_right (table, 5);
#endif
642 643
    gtk_grid_set_column_spacing (GTK_GRID (table), 6);
    gtk_grid_set_row_spacing (GTK_GRID (table), 6);
Inigo Martínez's avatar
Inigo Martínez committed
644 645

    text = poppler_annot_get_contents (annot);
646
    pgd_table_add_property (GTK_GRID (table), "<b>Contents:</b>", text, &row);
Inigo Martínez's avatar
Inigo Martínez committed
647 648 649
    g_free (text);

    text = poppler_annot_get_name (annot);
650
    pgd_table_add_property (GTK_GRID (table), "<b>Name:</b>", text, &row);
Inigo Martínez's avatar
Inigo Martínez committed
651 652 653
    g_free (text);

    text = poppler_annot_get_modified (annot);
654 655 656 657
    if (poppler_date_parse (text, &timet)) {
	    g_free (text);
	    text = pgd_format_date (timet);
    }
658
    pgd_table_add_property (GTK_GRID (table), "<b>Modified:</b>", text, &row);
Inigo Martínez's avatar
Inigo Martínez committed
659 660
    g_free (text);

661 662 663
    poppler_annot_get_rectangle (annot, &rect);
    text = g_strdup_printf ("(%.2f;%.2f) (%.2f;%.2f)", rect.x1, rect.y1, rect.x2, rect.y2);
    pgd_table_add_property (GTK_GRID (table), "<b>Coords:</b>", text, &row);
664 665
    g_free (text);

Inigo Martínez's avatar
Inigo Martínez committed
666 667
    if (POPPLER_IS_ANNOT_MARKUP (annot))
        pgd_annot_view_set_annot_markup (table, POPPLER_ANNOT_MARKUP (annot), &row);
Inigo Martínez's avatar
Inigo Martínez committed
668

Inigo Martínez's avatar
Inigo Martínez committed
669 670 671 672 673
    switch (poppler_annot_get_annot_type (annot))
    {
        case POPPLER_ANNOT_TEXT:
          pgd_annot_view_set_annot_text (table, POPPLER_ANNOT_TEXT (annot), &row);
          break;
674 675 676 677 678 679
        case POPPLER_ANNOT_HIGHLIGHT:
        case POPPLER_ANNOT_UNDERLINE:
        case POPPLER_ANNOT_SQUIGGLY:
        case POPPLER_ANNOT_STRIKE_OUT:
          pgd_annot_view_set_annot_text_markup (table, POPPLER_ANNOT_TEXT_MARKUP (annot), &row);
          break;
Inigo Martínez's avatar
Inigo Martínez committed
680 681 682
        case POPPLER_ANNOT_FREE_TEXT:
          pgd_annot_view_set_annot_free_text (table, POPPLER_ANNOT_FREE_TEXT (annot), &row);
          break;
683 684 685
        case POPPLER_ANNOT_FILE_ATTACHMENT:
	  pgd_annot_view_set_annot_file_attachment (table, POPPLER_ANNOT_FILE_ATTACHMENT (annot), &row);
	  break;
686 687
        case POPPLER_ANNOT_MOVIE:
	  pgd_annot_view_set_annot_movie (table, POPPLER_ANNOT_MOVIE (annot), &row);
688 689 690 691
	  break;
        case POPPLER_ANNOT_SCREEN:
	  pgd_annot_view_set_annot_screen (table, POPPLER_ANNOT_SCREEN (annot), &row);
	  break;
Inigo Martínez's avatar
Inigo Martínez committed
692 693
        default:
          break;
Inigo Martínez's avatar
Inigo Martínez committed
694 695
    }

696
    gtk_container_add (GTK_CONTAINER (demo->annot_view), table);
Inigo Martínez's avatar
Inigo Martínez committed
697 698 699
    gtk_widget_show (table);
}

700 701 702
static void
pgd_annots_add_annot_to_model (PgdAnnotsDemo *demo,
                               PopplerAnnot *annot,
703 704
                               PopplerRectangle area,
                               gboolean selected)
705
{
706 707 708
    GtkTreeIter      iter;
    GdkPixbuf        *pixbuf;
    PopplerAnnotFlag  flags;
709 710 711 712 713 714 715 716 717 718 719 720 721 722

    pixbuf = get_annot_color (annot);
    flags = poppler_annot_get_flags (annot);

    gtk_list_store_append (demo->model, &iter);
    gtk_list_store_set (demo->model, &iter,
                        ANNOTS_TYPE_COLUMN, get_annot_type (annot),
                        ANNOTS_COLOR_COLUMN, pixbuf,
                        ANNOTS_FLAG_INVISIBLE_COLUMN, (flags & POPPLER_ANNOT_FLAG_INVISIBLE),
                        ANNOTS_FLAG_HIDDEN_COLUMN, (flags & POPPLER_ANNOT_FLAG_HIDDEN),
                        ANNOTS_FLAG_PRINT_COLUMN, (flags & POPPLER_ANNOT_FLAG_PRINT),
                        ANNOTS_COLUMN, annot,
                        -1);

723 724 725 726 727 728 729 730
    if (selected) {
        GtkTreePath *path;

        path = gtk_tree_model_get_path (GTK_TREE_MODEL (demo->model), &iter);
        gtk_tree_view_set_cursor (GTK_TREE_VIEW (demo->tree_view), path, NULL, FALSE);
        gtk_tree_path_free (path);
    }

731 732 733 734
    if (pixbuf)
        g_object_unref (pixbuf);
}

Inigo Martínez's avatar
Inigo Martínez committed
735
static void
736
pgd_annots_get_annots (PgdAnnotsDemo *demo)
Inigo Martínez's avatar
Inigo Martínez committed
737 738 739 740 741 742
{
    GList       *mapping, *l;
    gint         n_fields;
    GTimer      *timer;

    gtk_list_store_clear (demo->model);
743
    pgd_annot_view_set_annot (demo, NULL);
Inigo Martínez's avatar
Inigo Martínez committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761

    if (demo->page) {
        g_object_unref (demo->page);
        demo->page = NULL;
    }

    demo->page = poppler_document_get_page (demo->doc, demo->num_page);
    if (!demo->page)
        return;

    timer = g_timer_new ();
    mapping = poppler_page_get_annot_mapping (demo->page);
    g_timer_stop (timer);

    n_fields = g_list_length (mapping);
    if (n_fields > 0) {
        gchar *str;

762
        str = g_strdup_printf ("<i>%d annotations found in %.4f seconds</i>",
Inigo Martínez's avatar
Inigo Martínez committed
763 764 765 766
                               n_fields, g_timer_elapsed (timer, NULL));
        gtk_label_set_markup (GTK_LABEL (demo->timer_label), str);
        g_free (str);
    } else {
767
        gtk_label_set_markup (GTK_LABEL (demo->timer_label), "<i>No annotations found</i>");
Inigo Martínez's avatar
Inigo Martínez committed
768 769 770 771 772 773 774 775
    }

    g_timer_destroy (timer);

    for (l = mapping; l; l = g_list_next (l)) {
        PopplerAnnotMapping *amapping;

        amapping = (PopplerAnnotMapping *) l->data;
776 777
        pgd_annots_add_annot_to_model (demo, amapping->annot,
                                       amapping->area, FALSE);
Inigo Martínez's avatar
Inigo Martínez committed
778 779 780 781 782 783 784 785 786 787
    }

    poppler_page_free_annot_mapping (mapping);
}

static void
pgd_annots_page_selector_value_changed (GtkSpinButton *spinbutton,
                                        PgdAnnotsDemo *demo)
{
    demo->num_page = (gint) gtk_spin_button_get_value (spinbutton) - 1;
788 789
    pgd_annots_viewer_queue_redraw (demo);
    pgd_annots_get_annots (demo);
Inigo Martínez's avatar
Inigo Martínez committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
}

static void
pgd_annots_selection_changed (GtkTreeSelection *treeselection,
                              PgdAnnotsDemo    *demo)
{
    GtkTreeModel *model;
    GtkTreeIter   iter;

    if (gtk_tree_selection_get_selected (treeselection, &model, &iter)) {
        PopplerAnnot *annot;

        gtk_tree_model_get (model, &iter,
                            ANNOTS_COLUMN, &annot,
                           -1);
805
        pgd_annot_view_set_annot (demo, annot);
Inigo Martínez's avatar
Inigo Martínez committed
806
        g_object_unref (annot);
807 808

        gtk_widget_set_sensitive (demo->remove_button, TRUE);
809 810
    } else {
        pgd_annot_view_set_annot (demo, NULL);
811
        gtk_widget_set_sensitive (demo->remove_button, FALSE);
Inigo Martínez's avatar
Inigo Martínez committed
812 813 814
    }
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
static void
pgd_annots_flags_toggled (GtkCellRendererToggle *renderer,
			  gchar *path_str,
			  PgdAnnotsDemo *demo,
			  gint column,
			  PopplerAnnotFlag flag_bit)
{
    GtkTreeIter  iter;
    gboolean fixed;
    PopplerAnnot *annot;
    PopplerAnnotFlag flags;
    GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
    GtkTreeModel *model =  GTK_TREE_MODEL (demo->model);

    gtk_tree_model_get_iter (model, &iter, path);
    gtk_tree_model_get (model, &iter, column, &fixed, ANNOTS_COLUMN, &annot,-1);

    fixed ^= 1;
    flags = poppler_annot_get_flags (annot);

    if (fixed)
        flags |= flag_bit;
    else
        flags &= ~flag_bit;

    poppler_annot_set_flags (annot, flags);
    gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, fixed, -1);

    pgd_annot_view_set_annot (demo, annot);
    gtk_tree_path_free (path);
845 846

    pgd_annots_viewer_queue_redraw (demo);
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
}

static void
pgd_annots_hidden_flag_toggled (GtkCellRendererToggle *renderer,
				gchar *path_str,
				PgdAnnotsDemo *demo)
{
    pgd_annots_flags_toggled (renderer, path_str, demo, ANNOTS_FLAG_HIDDEN_COLUMN, POPPLER_ANNOT_FLAG_HIDDEN);
}

static void
pgd_annots_print_flag_toggled (GtkCellRendererToggle *renderer,
                               gchar *path_str,
                               PgdAnnotsDemo *demo)
{
    pgd_annots_flags_toggled (renderer, path_str, demo, ANNOTS_FLAG_PRINT_COLUMN, POPPLER_ANNOT_FLAG_PRINT);
}

static void
pgd_annots_invisible_flag_toggled (GtkCellRendererToggle *renderer,
                                   gchar *path_str,
                                   PgdAnnotsDemo *demo)
{
    pgd_annots_flags_toggled (renderer, path_str, demo, ANNOTS_FLAG_INVISIBLE_COLUMN, POPPLER_ANNOT_FLAG_INVISIBLE);
}

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
static inline void
pgd_annots_set_poppler_quad_from_rectangle (PopplerQuadrilateral *quad,
                                            PopplerRectangle     *rect)
{
    quad->p1.x = rect->x1;
    quad->p1.y = rect->y1;
    quad->p2.x = rect->x2;
    quad->p2.y = rect->y1;
    quad->p3.x = rect->x1;
    quad->p3.y = rect->y2;
    quad->p4.x = rect->x2;
    quad->p4.y = rect->y2;
}

static GArray *
pgd_annots_create_quads_array_for_rectangle (PopplerRectangle *rect)
{
    GArray               *quads_array;
    PopplerQuadrilateral *quad;

    quads_array = g_array_sized_new (FALSE, FALSE,
                                     sizeof (PopplerQuadrilateral),
                                     1);
    g_array_set_size (quads_array, 1);

    quad = &g_array_index (quads_array, PopplerQuadrilateral, 0);
    pgd_annots_set_poppler_quad_from_rectangle (quad, rect);

    return quads_array;
}

904
static void
905
pgd_annots_add_annot (PgdAnnotsDemo *demo)
906
{
907 908 909 910
    PopplerRectangle rect;
    PopplerColor     color;
    PopplerAnnot    *annot;
    gdouble          height;
911

912
    g_assert (demo->mode == MODE_ADD);
913

914
    poppler_page_get_size (demo->page, NULL, &height);
915

916 917 918 919
    rect.x1 = demo->start.x;
    rect.y1 = height - demo->start.y;
    rect.x2 = demo->stop.x;
    rect.y2 = height - demo->stop.y;
920

921 922 923 924
    color.red = CLAMP ((guint) (demo->annot_color.red * 65535), 0, 65535);
    color.green = CLAMP ((guint) (demo->annot_color.green * 65535), 0, 65535);
    color.blue = CLAMP ((guint) (demo->annot_color.blue * 65535), 0, 65535);

925 926 927
    switch (demo->annot_type) {
        case POPPLER_ANNOT_TEXT:
            annot = poppler_annot_text_new (demo->doc, &rect);
928

929 930 931 932 933 934 935 936 937
            break;
        case POPPLER_ANNOT_LINE: {
            PopplerPoint start, end;

            start.x = rect.x1;
            start.y = rect.y1;
            end.x = rect.x2;
            end.y = rect.y2;

938
            annot = poppler_annot_line_new (demo->doc, &rect, &start, &end);
939
        }
940
            break;
941 942 943 944 945 946
        case POPPLER_ANNOT_SQUARE:
            annot = poppler_annot_square_new (demo->doc, &rect);
            break;
        case POPPLER_ANNOT_CIRCLE:
            annot = poppler_annot_circle_new (demo->doc, &rect);
            break;
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
        case POPPLER_ANNOT_HIGHLIGHT: {
            GArray *quads_array;

            quads_array = pgd_annots_create_quads_array_for_rectangle (&rect);
            annot = poppler_annot_text_markup_new_highlight (demo->doc, &rect, quads_array);
            g_array_free (quads_array, TRUE);
        }
            break;
        case POPPLER_ANNOT_UNDERLINE: {
            GArray *quads_array;

            quads_array = pgd_annots_create_quads_array_for_rectangle (&rect);
            annot = poppler_annot_text_markup_new_underline (demo->doc, &rect, quads_array);
            g_array_free (quads_array, TRUE);
        }
            break;
        case POPPLER_ANNOT_SQUIGGLY: {
            GArray *quads_array;

            quads_array = pgd_annots_create_quads_array_for_rectangle (&rect);
            annot = poppler_annot_text_markup_new_squiggly (demo->doc, &rect, quads_array);
            g_array_free (quads_array, TRUE);
        }
            break;
        case POPPLER_ANNOT_STRIKE_OUT: {
            GArray *quads_array;

            quads_array = pgd_annots_create_quads_array_for_rectangle (&rect);
            annot = poppler_annot_text_markup_new_strikeout (demo->doc, &rect, quads_array);
            g_array_free (quads_array, TRUE);
        }
            break;
979 980 981
        default:
            g_assert_not_reached ();
    }
982

983 984
    demo->active_annot = annot;

985
    poppler_annot_set_color (annot, &color);
986
    poppler_page_add_annot (demo->page, annot);
987
    pgd_annots_add_annot_to_model (demo, annot, rect, TRUE);
988 989
    g_object_unref (annot);
}
990

991 992 993 994
static void
pgd_annots_finish_add_annot (PgdAnnotsDemo *demo)
{
    g_assert (demo->mode == MODE_ADD || demo->mode == MODE_DRAWING);
995

996
    demo->mode = MODE_NORMAL;
997
    demo->start.x = -1;
998
    pgd_annots_update_cursor (demo, GDK_LAST_CURSOR);
999
    pgd_annots_viewer_queue_redraw (demo);
1000 1001

    gtk_label_set_text (GTK_LABEL (demo->timer_label), NULL);
1002 1003
}

1004 1005 1006 1007 1008 1009 1010
static void
pgd_annots_update_selected_text (PgdAnnotsDemo *demo)
{
    PopplerRectangle      doc_area, *rects = NULL, *r = NULL;
    gdouble               width, height;
    GArray               *quads_array = NULL;
    guint                 n_rects;
1011
    gint                  i, lines = 0;
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    GList                 *l_rects = NULL, *list;

    poppler_page_get_size (demo->page, &width, &height);

    doc_area.x1 = demo->start.x;
    doc_area.y1 = demo->start.y;
    doc_area.x2 = demo->stop.x;
    doc_area.y2 = demo->stop.y;

    if (! poppler_page_get_text_layout_for_area (demo->page, &doc_area,
                                                 &rects, &n_rects))
        return;

    r = g_slice_new (PopplerRectangle);
    r->x1 = G_MAXDOUBLE; r->y1 = G_MAXDOUBLE;
    r->x2 = G_MINDOUBLE; r->y2 = G_MINDOUBLE;

    for (i = 0; i < n_rects; i++) {
        /* Check if the rectangle belongs to the same line.
           On a new line, start a new target rectangle.
           On the same line, make an union of rectangles at
           the same line */
        if (ABS(r->y2 - rects[i].y2) > 0.0001) {
            if (i > 0)
                l_rects = g_list_append (l_rects, r);
            r = g_slice_new (PopplerRectangle);
            r->x1 = rects[i].x1;
1039
            r->y1 = rects[i].y1;
1040
            r->x2 = rects[i].x2;
1041
            r->y2 = rects[i].y2;
1042 1043 1044
            lines++;
        } else {
            r->x1 = MIN(r->x1, rects[i].x1);
1045
            r->y1 = MIN(r->y1, rects[i].y1);
1046
            r->x2 = MAX(r->x2, rects[i].x2);
1047
            r->y2 = MAX(r->y2, rects[i].y2);
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
        }
    }

    l_rects = g_list_append (l_rects, r);
    l_rects = g_list_reverse (l_rects);

    quads_array = g_array_sized_new (TRUE, TRUE,
                                     sizeof (PopplerQuadrilateral),
                                     lines);
    g_array_set_size (quads_array, lines);

    for (list = l_rects, i = 0; list; list = list->next, i++) {
        PopplerQuadrilateral *quad;

        quad = &g_array_index (quads_array, PopplerQuadrilateral, i);
        r = (PopplerRectangle *)list->data;
1064 1065 1066 1067 1068 1069 1070 1071
        quad->p1.x = r->x1;
        quad->p1.y = height - r->y1;
        quad->p2.x = r->x2;
        quad->p2.y = height - r->y1;
        quad->p3.x = r->x1;
        quad->p3.y = height - r->y2;
        quad->p4.x = r->x2;
        quad->p4.y = height - r->y2;
1072 1073 1074 1075 1076 1077 1078 1079 1080
        g_slice_free (PopplerRectangle, r);
    }

    poppler_annot_text_markup_set_quadrilaterals (POPPLER_ANNOT_TEXT_MARKUP (demo->active_annot), quads_array);
    g_array_free (quads_array, TRUE);
    g_free (rects);
    g_list_free (l_rects);
}

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
/* Render area */
static cairo_surface_t *
pgd_annots_render_page (PgdAnnotsDemo *demo)
{
    cairo_t *cr;
    PopplerPage *page;
    gdouble width, height;
    cairo_surface_t *surface = NULL;

    page = poppler_document_get_page (demo->doc, demo->num_page);
    if (!page)
        return NULL;

    poppler_page_get_size (page, &width, &height);
    gtk_widget_set_size_request (demo->darea, width, height);

    surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
                                          width, height);
    cr = cairo_create (surface);

    cairo_save (cr);
    cairo_set_source_rgb (cr, 1, 1, 1);
    cairo_rectangle (cr, 0, 0, width, height);
    cairo_fill (cr);
    cairo_restore (cr);

    cairo_save (cr);
    poppler_page_render (page, cr);
    cairo_restore (cr);

    cairo_destroy (cr);
    g_object_unref (page);

    return surface;
}

static gboolean
pgd_annots_view_drawing_area_draw (GtkWidget   *area,
                                   cairo_t     *cr,
                                   PgdAnnotsDemo *demo)
{
    if (demo->num_page == -1)
        return FALSE;

    if (!demo->surface) {
        demo->surface = pgd_annots_render_page (demo);
        if (!demo->surface)
            return FALSE;
    }

    cairo_set_source_surface (cr, demo->surface, 0, 0);
    cairo_paint (cr);

    return TRUE;
}

static gboolean
pgd_annots_viewer_redraw (PgdAnnotsDemo *demo)
{
    cairo_surface_destroy (demo->surface);
    demo->surface = NULL;

    gtk_widget_queue_draw (demo->darea);

1145 1146
    demo->annotations_idle = 0;

1147 1148 1149 1150 1151 1152
    return FALSE;
}

static void
pgd_annots_viewer_queue_redraw (PgdAnnotsDemo *demo)
{
1153 1154 1155
    if (demo->annotations_idle == 0)
        demo->annotations_idle = g_idle_add ((GSourceFunc)pgd_annots_viewer_redraw,
                                             demo);
1156 1157
}

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
static void
pgd_annots_drawing_area_realize (GtkWidget     *area,
                                 PgdAnnotsDemo *demo)
{
    gtk_widget_add_events (area,
                           GDK_POINTER_MOTION_HINT_MASK |
                           GDK_BUTTON1_MOTION_MASK |
                           GDK_BUTTON_PRESS_MASK |
                           GDK_BUTTON_RELEASE_MASK);
}

static gboolean
pgd_annots_drawing_area_button_press (GtkWidget      *area,
                                      GdkEventButton *event,
                                      PgdAnnotsDemo  *demo)
{
    if (!demo->page || demo->mode != MODE_ADD || event->button != 1)
        return FALSE;

    demo->start.x = event->x;
    demo->start.y = event->y;
    demo->stop = demo->start;

    pgd_annots_add_annot (demo);
    pgd_annots_viewer_queue_redraw (demo);
    demo->mode = MODE_DRAWING;

    return TRUE;
}

1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
static gboolean
pgd_annots_drawing_area_motion_notify (GtkWidget      *area,
                                       GdkEventMotion *event,
                                       PgdAnnotsDemo  *demo)
{
    PopplerRectangle rect;
    PopplerPoint start, end;
    gdouble width, height;

    if (!demo->page || demo->mode != MODE_DRAWING || demo->start.x == -1)
        return FALSE;

    demo->stop.x = event->x;
    demo->stop.y = event->y;

    poppler_page_get_size (demo->page, &width, &height);

    /* Keep the drawing within the page */
    demo->stop.x = CLAMP (demo->stop.x, 0, width);
    demo->stop.y = CLAMP (demo->stop.y, 0, height);

    rect.x1 = start.x = demo->start.x;
    rect.y1 = start.y = height - demo->start.y;
    rect.x2 = end.x = demo->stop.x;
    rect.y2 = end.y = height - demo->stop.y;

    poppler_annot_set_rectangle (demo->active_annot, &rect);

    if (demo->annot_type == POPPLER_ANNOT_LINE)
        poppler_annot_line_set_vertices (POPPLER_ANNOT_LINE (demo->active_annot),
                                         &start, &end);

1220 1221 1222
    if (POPPLER_IS_ANNOT_TEXT_MARKUP (demo->active_annot))
        pgd_annots_update_selected_text (demo);

1223 1224 1225 1226 1227 1228
    pgd_annot_view_set_annot (demo, demo->active_annot);
    pgd_annots_viewer_queue_redraw (demo);

    return TRUE;
}

1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
static gboolean
pgd_annots_drawing_area_button_release (GtkWidget      *area,
                                        GdkEventButton *event,
                                        PgdAnnotsDemo  *demo)
{
    if (!demo->page || demo->mode != MODE_DRAWING || event->button != 1)
        return FALSE;

    pgd_annots_finish_add_annot (demo);

    return TRUE;
}

1242
/* Main UI */
Inigo Martínez's avatar
Inigo Martínez committed
1243 1244 1245 1246 1247
GtkWidget *
pgd_annots_create_widget (PopplerDocument *document)
{
    PgdAnnotsDemo    *demo;
    GtkWidget        *label;
1248
    GtkWidget        *vbox, *vbox2;
Inigo Martínez's avatar
Inigo Martínez committed
1249
    GtkWidget        *button;
1250
    GtkWidget        *hbox, *page_selector;
Inigo Martínez's avatar
Inigo Martínez committed
1251 1252 1253 1254
    GtkWidget        *hpaned;
    GtkWidget        *swindow, *treeview;
    GtkTreeSelection *selection;
    GtkCellRenderer  *renderer;
1255
    GtkTreeViewColumn *column;
1256 1257
    GtkListStore     *model;
    GtkTreeIter       iter;
Inigo Martínez's avatar
Inigo Martínez committed
1258 1259
    gchar            *str;
    gint              n_pages;
William Bader's avatar
William Bader committed
1260
    gint              i;
Inigo Martínez's avatar
Inigo Martínez committed
1261 1262 1263 1264

    demo = g_new0 (PgdAnnotsDemo, 1);

    demo->doc = g_object_ref (document);
1265 1266
    demo->cursor = GDK_LAST_CURSOR;
    demo->mode = MODE_NORMAL;
Inigo Martínez's avatar
Inigo Martínez committed
1267 1268 1269

    n_pages = poppler_document_get_n_pages (document);

1270
    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
1271
    vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
Inigo Martínez's avatar
Inigo Martínez committed
1272

1273
    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
Inigo Martínez's avatar
Inigo Martínez committed
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291

    label = gtk_label_new ("Page:");
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, 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_annots_page_selector_value_changed),
                      (gpointer) demo);
    gtk_box_pack_start (GTK_BOX (hbox), page_selector, FALSE, 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 (hbox), label, FALSE, TRUE, 0);
    gtk_widget_show (label);
    g_free (str);

1292
    demo->remove_button = gtk_button_new_with_mnemonic ("_Remove");
1293 1294 1295 1296 1297 1298 1299
    gtk_widget_set_sensitive (demo->remove_button, FALSE);
    g_signal_connect (G_OBJECT (demo->remove_button), "clicked",
                      G_CALLBACK (pgd_annots_remove_annot),
                      (gpointer) demo);
    gtk_box_pack_end (GTK_BOX (hbox), demo->remove_button, FALSE, FALSE, 6);
    gtk_widget_show (demo->remove_button);

1300 1301
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);

1302
    button = gtk_button_new_with_mnemonic ("_Add");
1303
    g_signal_connect (G_OBJECT (button), "clicked",
1304 1305
                      G_CALLBACK (pgd_annots_start_add_annot),
                      (gpointer) demo);
1306 1307 1308
    gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
    gtk_widget_show (button);

1309 1310
    model = gtk_list_store_new(SELECTED_N_COLUMNS,
                               G_TYPE_INT, G_TYPE_STRING);
1311

William Bader's avatar
William Bader committed
1312
    for (i = 0; i < G_N_ELEMENTS (supported_annots); i++) {
1313 1314 1315 1316 1317 1318
        gtk_list_store_append (model, &iter);
        gtk_list_store_set (model, &iter,
                            SELECTED_TYPE_COLUMN, supported_annots[i].type,
                            SELECTED_LABEL_COLUMN, supported_annots[i].label,
                            -1);
    }
1319

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
    demo->type_selector = gtk_combo_box_new_with_model (GTK_TREE_MODEL (model));
    g_object_unref (model);

    renderer = gtk_cell_renderer_text_new ();
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (demo->type_selector), renderer, TRUE);
    gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (demo->type_selector), renderer,
                                    "text", SELECTED_LABEL_COLUMN,
                                    NULL);
    gtk_combo_box_set_active (GTK_COMBO_BOX (demo->type_selector), 0);
    gtk_box_pack_end (GTK_BOX (hbox), demo->type_selector, FALSE, FALSE, 0);
    gtk_widget_show (demo->type_selector);

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
    button = gtk_color_button_new ();
    demo->annot_color.red = 65535;
    demo->annot_color.alpha = 1.0;
    gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button), &demo->annot_color);
    g_signal_connect (button, "notify::color",
                      G_CALLBACK (pgd_annot_color_changed),
                      (gpointer)demo);
    gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, TRUE, 0);
    gtk_widget_show (button);

Inigo Martínez's avatar
Inigo Martínez committed
1342 1343 1344
    gtk_widget_show (hbox);

    demo->timer_label = gtk_label_new (NULL);
1345
    gtk_label_set_markup (GTK_LABEL (demo->timer_label), "<i>No annotations found</i>");
Inigo Martínez's avatar
Inigo Martínez committed
1346 1347 1348 1349
    g_object_set (G_OBJECT (demo->timer_label), "xalign", 1.0, NULL);
    gtk_box_pack_start (GTK_BOX (vbox), demo->timer_label, FALSE, TRUE, 0);
    gtk_widget_show (demo->timer_label);

1350
    hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
Inigo Martínez's avatar
Inigo Martínez committed
1351 1352 1353 1354 1355 1356 1357 1358 1359

    demo->annot_view = pgd_annot_view_new ();

    swindow = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
                                    GTK_POLICY_AUTOMATIC,
                                    GTK_POLICY_AUTOMATIC);

    demo->model = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING,
1360 1361 1362
                                      GDK_TYPE_PIXBUF, G_TYPE_BOOLEAN,
				      G_TYPE_BOOLEAN, G_TYPE_BOOLEAN,
				      G_TYPE_OBJECT);
Inigo Martínez's avatar
Inigo Martínez committed
1363
    treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (demo->model));
1364
    demo->tree_view = treeview;
Inigo Martínez's avatar
Inigo Martínez committed
1365

1366 1367 1368
    column = gtk_tree_view_column_new ();
    gtk_tree_view_column_set_title (column, "Type");
    gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
Inigo Martínez's avatar
Inigo Martínez committed
1369

Inigo Martínez's avatar
Inigo Martínez committed
1370
    renderer = gtk_cell_renderer_pixbuf_new ();
1371 1372 1373 1374 1375 1376
    gtk_tree_view_column_pack_start (column, renderer, TRUE);
    gtk_tree_view_column_add_attribute (column, renderer, "pixbuf", ANNOTS_COLOR_COLUMN);

    renderer = gtk_cell_renderer_text_new ();
    gtk_tree_view_column_pack_start (column, renderer, TRUE);
    gtk_tree_view_column_add_attribute (column, renderer, "text", ANNOTS_TYPE_COLUMN);
Inigo Martínez's avatar
Inigo Martínez committed
1377

1378
    renderer = gtk_cell_renderer_toggle_new ();
1379 1380 1381
    g_signal_connect (renderer, "toggled",
		      G_CALLBACK (pgd_annots_invisible_flag_toggled),
		      (gpointer) demo);
1382 1383 1384 1385 1386 1387 1388
    gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
                                                 ANNOTS_FLAG_INVISIBLE_COLUMN, "Invisible",
                                                 renderer,
                                                 "active", ANNOTS_FLAG_INVISIBLE_COLUMN,
                                                 NULL);

    renderer = gtk_cell_renderer_toggle_new ();
1389 1390 1391
    g_signal_connect (renderer, "toggled",
                      G_CALLBACK (pgd_annots_hidden_flag_toggled),
                      (gpointer) demo);
1392 1393 1394 1395 1396 1397 1398
    gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
                                                 ANNOTS_FLAG_HIDDEN_COLUMN, "Hidden",
                                                 renderer,
                                                 "active", ANNOTS_FLAG_HIDDEN_COLUMN,
                                                 NULL);

    renderer = gtk_cell_renderer_toggle_new ();
1399 1400 1401
    g_signal_connect (renderer, "toggled",
                      G_CALLBACK (pgd_annots_print_flag_toggled),
                      (gpointer) demo);
1402 1403 1404 1405 1406 1407
    gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
                                                 ANNOTS_FLAG_PRINT_COLUMN, "Print",
                                                 renderer,
                                                 "active", ANNOTS_FLAG_PRINT_COLUMN,
                                                 NULL);

Inigo Martínez's avatar
Inigo Martínez committed
1408 1409 1410 1411 1412
    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
    g_signal_connect (G_OBJECT (selection), "changed",
                      G_CALLBACK (pgd_annots_selection_changed),
                      (gpointer) demo);

1413
    /* Annotation's list */
Inigo Martínez's avatar
Inigo Martínez committed
1414 1415 1416
    gtk_container_add (GTK_CONTAINER (swindow), treeview);
    gtk_widget_show (treeview);

1417
    gtk_box_pack_start (GTK_BOX (vbox2), swindow, TRUE, TRUE, 0);
Inigo Martínez's avatar
Inigo Martínez committed
1418 1419
    gtk_widget_show (swindow);

1420
    /* Annotation Properties */
1421 1422 1423 1424 1425
    swindow = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
                                    GTK_POLICY_AUTOMATIC,
                                    GTK_POLICY_AUTOMATIC);
    gtk_container_add (GTK_CONTAINER (swindow), demo->annot_view);
Inigo Martínez's avatar
Inigo Martínez committed
1426
    gtk_widget_show (demo->annot_view);
1427 1428 1429 1430
    gtk_widget_show (swindow);

    gtk_box_pack_start (GTK_BOX (vbox2), swindow, TRUE, TRUE, 6);
    gtk_widget_show (swindow);
Inigo Martínez's avatar
Inigo Martínez committed
1431

1432 1433 1434 1435 1436 1437 1438 1439
    gtk_paned_add1 (GTK_PANED (hpaned), vbox2);
    gtk_widget_show (vbox2);

    /* Demo Area (Render) */
    demo->darea = gtk_drawing_area_new ();
    g_signal_connect (demo->darea, "draw",
                      G_CALLBACK (pgd_annots_view_drawing_area_draw),
                      demo);
1440 1441 1442 1443 1444 1445
    g_signal_connect (demo->darea, "realize",
                      G_CALLBACK (pgd_annots_drawing_area_realize),
                      (gpointer)demo);
    g_signal_connect (demo->darea, "button_press_event",
                      G_CALLBACK (pgd_annots_drawing_area_button_press),
                      (gpointer)demo);
1446 1447 1448
    g_signal_connect (demo->darea, "motion_notify_event",
                      G_CALLBACK (pgd_annots_drawing_area_motion_notify),
                      (gpointer)demo);
1449 1450 1451
    g_signal_connect (demo->darea, "button_release_event",
                      G_CALLBACK (pgd_annots_drawing_area_button_release),
                      (gpointer)demo);
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463

    swindow = gtk_scrolled_window_new (NULL, NULL);
#if GTK_CHECK_VERSION(3, 7, 8)
    gtk_container_add(GTK_CONTAINER(swindow), demo->darea);
#else
    gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (swindow), demo->darea);
#endif
    gtk_widget_show (demo->darea);

    gtk_paned_add2 (GTK_PANED (hpaned), swindow);
    gtk_widget_show (swindow);

Inigo Martínez's avatar
Inigo Martínez committed
1464 1465 1466 1467 1468 1469 1470 1471 1472
    gtk_paned_set_position (GTK_PANED (hpaned), 300);

    gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
    gtk_widget_show (hpaned);

    g_object_weak_ref (G_OBJECT (vbox),
                       (GWeakNotify)pgd_annots_free,
                       demo);

1473 1474 1475
    pgd_annots_viewer_queue_redraw (demo);
    pgd_annots_get_annots (demo);

1476 1477
    demo->main_box = vbox;

Inigo Martínez's avatar
Inigo Martínez committed
1478 1479
    return vbox;
}