shortcuts.c 37.8 KB
Newer Older
1 2
/* See LICENSE file for license and copyright information */

3 4 5 6
#include <girara/session.h>
#include <girara/settings.h>
#include <girara/datastructures.h>
#include <girara/shortcuts.h>
7
#include <girara/utils.h>
Moritz Lipp's avatar
Moritz Lipp committed
8
#include <gtk/gtk.h>
9
#include <glib/gi18n.h>
Moritz Lipp's avatar
Moritz Lipp committed
10 11

#include "callbacks.h"
12
#include "shortcuts.h"
13
#include "document.h"
Moritz Lipp's avatar
Moritz Lipp committed
14
#include "zathura.h"
15
#include "render.h"
16
#include "utils.h"
Moritz Lipp's avatar
Moritz Lipp committed
17
#include "page.h"
Moritz Lipp's avatar
Moritz Lipp committed
18
#include "print.h"
19
#include "page-widget.h"
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/* Helper function; see sc_display_link and sc_follow. */
static bool
draw_links(zathura_t* zathura)
{
  /* set pages to draw links */
  bool show_links = false;
  unsigned int page_offset = 0;
  unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
  for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
    zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
    if (page == NULL) {
      continue;
    }

    GtkWidget* page_widget = zathura_page_get_widget(zathura, page);
    g_object_set(page_widget, "search-results", NULL, NULL);
    if (zathura_page_get_visibility(page) == true) {
      g_object_set(page_widget, "draw-links", TRUE, NULL);

      int number_of_links = 0;
      g_object_get(page_widget, "number-of-links", &number_of_links, NULL);
      if (number_of_links != 0) {
        show_links = true;
      }
      g_object_set(page_widget, "offset-links", page_offset, NULL);
      page_offset += number_of_links;
    } else {
      g_object_set(page_widget, "draw-links", FALSE, NULL);
    }
  }
  return show_links;
}

Moritz Lipp's avatar
Moritz Lipp committed
54
bool
55
sc_abort(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
56
         girara_event_t* UNUSED(event), unsigned int UNUSED(t))
57
{
Moritz Lipp's avatar
Moritz Lipp committed
58
  g_return_val_if_fail(session != NULL, false);
59 60 61
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

62 63 64
  bool clear_search = true;
  girara_setting_get(session, "abort-clear-search", &clear_search);

Moritz Lipp's avatar
Moritz Lipp committed
65
  if (zathura->document != NULL) {
66
    unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
67 68
    for (unsigned int page_id = 0; page_id < number_of_pages; ++page_id) {
      zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
Moritz Lipp's avatar
Moritz Lipp committed
69 70 71 72
      if (page == NULL) {
        continue;
      }

73
      g_object_set(zathura_page_get_widget(zathura, page), "draw-links", FALSE, NULL);
74
      if (clear_search == true) {
75 76
        g_object_set(zathura_page_get_widget(zathura, page), "search-results", NULL, NULL);
      }
77 78
    }
  }
Moritz Lipp's avatar
Moritz Lipp committed
79

Moritz Lipp's avatar
Moritz Lipp committed
80
  girara_mode_set(session, session->modes.normal);
81
  girara_sc_abort(session, NULL, NULL, 0);
Moritz Lipp's avatar
Moritz Lipp committed
82

Moritz Lipp's avatar
Moritz Lipp committed
83
  return false;
84 85
}

86 87
bool
sc_adjust_window(girara_session_t* session, girara_argument_t* argument,
Moritz Lipp's avatar
Moritz Lipp committed
88
                 girara_event_t* UNUSED(event), unsigned int UNUSED(t))
89
{
90 91 92 93
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(argument != NULL, false);
Moritz Lipp's avatar
Moritz Lipp committed
94

95 96
  unsigned int pages_per_row = 1;
  girara_setting_get(session, "pages-per-row", &pages_per_row);
Moritz Lipp's avatar
Moritz Lipp committed
97

98 99 100
  unsigned int first_page_column = 1;
  girara_setting_get(session, "first-page-column", &first_page_column);

101 102 103
  int padding = 1;
  girara_setting_get(zathura->ui.session, "page-padding", &padding);

104
  if (zathura->ui.page_widget == NULL || zathura->document == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
105 106 107
    goto error_ret;
  }

108
  float old_zoom = zathura_document_get_scale(zathura->document);
109 110
  zathura_document_set_adjust_mode(zathura->document, argument->n);
  if (argument->n == ZATHURA_ADJUST_NONE) {
111 112 113
    /* there is nothing todo */
    goto error_ret;
  }
114

Moritz Lipp's avatar
Moritz Lipp committed
115
  /* get window size */
116 117
  GtkAllocation allocation;
  gtk_widget_get_allocation(session->gtk.view, &allocation);
118 119
  unsigned int width  = allocation.width;
  unsigned int height = allocation.height;
Moritz Lipp's avatar
Moritz Lipp committed
120

121 122 123 124 125
  /* scrollbar spacing */
  gint spacing;
  gtk_widget_style_get(session->gtk.view, "scrollbar_spacing", &spacing, NULL);
  width -= spacing;

126 127 128 129 130 131
  /* correct view size */
  if (gtk_widget_get_visible(GTK_WIDGET(session->gtk.inputbar)) == true) {
    gtk_widget_get_allocation(session->gtk.inputbar, &allocation);
    height += allocation.height;
  }

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
  double scale = 1.0;
  unsigned int cell_height = 0, cell_width = 0;
  unsigned int document_height = 0, document_width = 0;

  zathura_document_set_scale(zathura->document, scale);
  zathura_document_get_cell_size(zathura->document, &cell_height, &cell_width);
  zathura_get_document_size(zathura, cell_height, cell_width,
                            &document_height, &document_width);

  double page_ratio   = (double)cell_height / (double)document_width;
  double window_ratio = (double)height / (double)width;

  if (argument->n == ZATHURA_ADJUST_WIDTH ||
      (argument->n == ZATHURA_ADJUST_BESTFIT && page_ratio < window_ratio)) {
    scale = (double)(width - (pages_per_row - 1) * padding) /
            (double)(pages_per_row * cell_width);
    zathura_document_set_scale(zathura->document, scale);

    bool show_scrollbars = false;
    girara_setting_get(session, "show-scrollbars", &show_scrollbars);

    if (show_scrollbars) {
      /* If the document is taller than the view, there's a vertical
       * scrollbar; we need to substract its width from the view's width. */
      zathura_get_document_size(zathura, cell_height, cell_width,
                                &document_height, &document_width);
      if (height < document_height) {
        GtkWidget* vscrollbar = gtk_scrolled_window_get_vscrollbar(
            GTK_SCROLLED_WINDOW(session->gtk.view));

        if (vscrollbar != NULL) {
          GtkRequisition requisition;
          gtk_widget_get_requisition(vscrollbar, &requisition);
          if (0 < requisition.width && (unsigned)requisition.width < width) {
            width -= requisition.width;
            scale = (double)(width - (pages_per_row - 1) * padding) /
                    (double)(pages_per_row * cell_width);
            zathura_document_set_scale(zathura->document, scale);
          }
        }
      }
Moritz Lipp's avatar
Moritz Lipp committed
173 174
    }
  }
175 176 177
  else if (argument->n == ZATHURA_ADJUST_BESTFIT) {
    scale = (double)height / (double)cell_height;
    zathura_document_set_scale(zathura->document, scale);
Moritz Lipp's avatar
Moritz Lipp committed
178
  }
179 180
  else {
    goto error_ret;
Moritz Lipp's avatar
Moritz Lipp committed
181 182
  }

183
  /* keep position */
184
  readjust_view_after_zooming(zathura, old_zoom, false);
185

Moritz Lipp's avatar
Moritz Lipp committed
186 187 188 189
  /* re-render all pages */
  render_all(zathura);

error_ret:
Moritz Lipp's avatar
Moritz Lipp committed
190

191
  return false;
192 193
}

Moritz Lipp's avatar
Moritz Lipp committed
194
bool
Moritz Lipp's avatar
Moritz Lipp committed
195
sc_change_mode(girara_session_t* session, girara_argument_t* argument,
Moritz Lipp's avatar
Moritz Lipp committed
196
               girara_event_t* UNUSED(event), unsigned int UNUSED(t))
197
{
Moritz Lipp's avatar
Moritz Lipp committed
198 199 200 201
  g_return_val_if_fail(session != NULL, false);

  girara_mode_set(session, argument->n);

Moritz Lipp's avatar
Moritz Lipp committed
202
  return false;
203 204
}

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
bool
sc_display_link(girara_session_t* session, girara_argument_t* UNUSED(argument),
                girara_event_t* UNUSED(event), unsigned int UNUSED(t))
{
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

  if (zathura->document == NULL || zathura->ui.session == NULL) {
    return false;
  }

  bool show_links = draw_links(zathura);

  /* ask for input */
  if (show_links) {
    girara_dialog(zathura->ui.session, "Display link:", FALSE, NULL,
        (girara_callback_inputbar_activate_t) cb_sc_display_link,
        zathura->ui.session);
  }

  return false;
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
bool
sc_focus_inputbar(girara_session_t* session, girara_argument_t* argument, girara_event_t* UNUSED(event), unsigned int UNUSED(t))
{
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->gtk.inputbar_entry != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(argument != NULL, false);

  if (gtk_widget_get_visible(GTK_WIDGET(session->gtk.inputbar)) == false) {
    gtk_widget_show(GTK_WIDGET(session->gtk.inputbar));
  }

  if (gtk_widget_get_visible(GTK_WIDGET(session->gtk.notification_area)) == true) {
    gtk_widget_hide(GTK_WIDGET(session->gtk.notification_area));
  }

  gtk_widget_grab_focus(GTK_WIDGET(session->gtk.inputbar_entry));

  if (argument->data != NULL) {
    gtk_entry_set_text(session->gtk.inputbar_entry, (char*) argument->data);

    /* append filepath */
    if (argument->n == APPEND_FILEPATH && zathura->document != NULL) {
253 254 255 256
      const char* file_path = zathura_document_get_path(zathura->document);
      if (file_path == NULL) {
        return false;
      }
257

258 259 260 261 262
      char* path = g_path_get_dirname(file_path);
      char* escaped = girara_escape_string(path);
      char* tmp  = g_strdup_printf("%s%s/", (char*) argument->data, (g_strcmp0(path, "/") == 0) ? "" : escaped);
      g_free(path);
      g_free(escaped);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

      gtk_entry_set_text(session->gtk.inputbar_entry, tmp);
      g_free(tmp);
    }

    /* we save the X clipboard that will be clear by "grab_focus" */
    gchar* x_clipboard_text = gtk_clipboard_wait_for_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY));

    gtk_editable_set_position(GTK_EDITABLE(session->gtk.inputbar_entry), -1);

    if (x_clipboard_text != NULL) {
      /* we reset the X clipboard with saved text */
      gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), x_clipboard_text, -1);
      g_free(x_clipboard_text);
    }
  }

  return true;
}

Moritz Lipp's avatar
Moritz Lipp committed
283
bool
284
sc_follow(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
285
          girara_event_t* UNUSED(event), unsigned int UNUSED(t))
286
{
Moritz Lipp's avatar
Moritz Lipp committed
287
  g_return_val_if_fail(session != NULL, false);
288 289 290
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

Moritz Lipp's avatar
Moritz Lipp committed
291
  if (zathura->document == NULL || zathura->ui.session == NULL) {
292 293 294
    return false;
  }

295
  bool show_links = draw_links(zathura);
296

Moritz Lipp's avatar
Moritz Lipp committed
297
  /* ask for input */
Moritz Lipp's avatar
Moritz Lipp committed
298
  if (show_links == true) {
Moritz Lipp's avatar
Moritz Lipp committed
299
    girara_dialog(zathura->ui.session, "Follow link:", FALSE, NULL, (girara_callback_inputbar_activate_t) cb_sc_follow, zathura->ui.session);
Moritz Lipp's avatar
Moritz Lipp committed
300
  }
Moritz Lipp's avatar
Moritz Lipp committed
301

Moritz Lipp's avatar
Moritz Lipp committed
302 303
  return false;
}
304

Moritz Lipp's avatar
Moritz Lipp committed
305
bool
Moritz Lipp's avatar
Moritz Lipp committed
306
sc_goto(girara_session_t* session, girara_argument_t* argument, girara_event_t* UNUSED(event), unsigned int t)
Moritz Lipp's avatar
Moritz Lipp committed
307
{
Moritz Lipp's avatar
Moritz Lipp committed
308 309 310 311 312
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(argument != NULL, false);
  g_return_val_if_fail(zathura->document != NULL, false);
313

314
  zathura_jumplist_save(zathura);
Moritz Lipp's avatar
Moritz Lipp committed
315
  if (t != 0) {
316
    /* add offset */
317
    unsigned int page_offset = zathura_document_get_page_offset(zathura->document);
318 319
    if (page_offset > 0) {
      t += page_offset;
320 321
    }

322
    page_set(zathura, t-1);
Moritz Lipp's avatar
Moritz Lipp committed
323
  } else if (argument->n == TOP) {
Moritz Lipp's avatar
Moritz Lipp committed
324
    page_set(zathura, 0);
Moritz Lipp's avatar
Moritz Lipp committed
325
  } else if (argument->n == BOTTOM) {
326
    page_set(zathura, zathura_document_get_number_of_pages(zathura->document) - 1);
327 328
  }

329 330
  zathura_jumplist_add(zathura);

Moritz Lipp's avatar
Moritz Lipp committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  return false;
}

bool
sc_mouse_scroll(girara_session_t* session, girara_argument_t* argument, girara_event_t* event, unsigned int t)
{
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(argument != NULL, false);
  g_return_val_if_fail(event != NULL, false);

  if (zathura->document == NULL) {
    return false;
  }

Moritz Lipp's avatar
Moritz Lipp committed
347 348 349 350 351 352 353
  static int x = 0;
  static int y = 0;

  GtkAdjustment* x_adj = NULL;
  GtkAdjustment* y_adj = NULL;

  switch (event->type) {
Moritz Lipp's avatar
Moritz Lipp committed
354
      /* scroll */
Moritz Lipp's avatar
Moritz Lipp committed
355 356 357 358 359 360
    case GIRARA_EVENT_SCROLL_UP:
    case GIRARA_EVENT_SCROLL_DOWN:
    case GIRARA_EVENT_SCROLL_LEFT:
    case GIRARA_EVENT_SCROLL_RIGHT:
      return sc_scroll(session, argument, NULL, t);

Moritz Lipp's avatar
Moritz Lipp committed
361
      /* drag */
Moritz Lipp's avatar
Moritz Lipp committed
362 363 364 365 366 367 368 369 370 371
    case GIRARA_EVENT_BUTTON_PRESS:
      x = event->x;
      y = event->y;
      break;
    case GIRARA_EVENT_BUTTON_RELEASE:
      x = 0;
      y = 0;
      break;
    case GIRARA_EVENT_MOTION_NOTIFY:
      x_adj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(session->gtk.view));
372
      y_adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(session->gtk.view));
Moritz Lipp's avatar
Moritz Lipp committed
373 374 375 376

      if (x_adj == NULL || y_adj == NULL) {
        return false;
      }
Moritz Lipp's avatar
Moritz Lipp committed
377

Moritz Lipp's avatar
Moritz Lipp committed
378 379 380 381
      set_adjustment(x_adj, gtk_adjustment_get_value(x_adj) - (event->x - x));
      set_adjustment(y_adj, gtk_adjustment_get_value(y_adj) - (event->y - y));
      break;

Moritz Lipp's avatar
Moritz Lipp committed
382
      /* unhandled events */
Moritz Lipp's avatar
Moritz Lipp committed
383 384
    default:
      break;
Moritz Lipp's avatar
Moritz Lipp committed
385 386
  }

Moritz Lipp's avatar
Moritz Lipp committed
387
  return false;
388 389
}

Moritz Lipp's avatar
Moritz Lipp committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403
bool
sc_mouse_zoom(girara_session_t* session, girara_argument_t* argument, girara_event_t* event, unsigned int t)
{
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(argument != NULL, false);
  g_return_val_if_fail(event != NULL, false);

  if (zathura->document == NULL) {
    return false;
  }

  /* scroll event */
404 405 406 407 408 409 410 411 412
  switch (event->type) {
    case GIRARA_EVENT_SCROLL_UP:
      argument->n = ZOOM_IN;
      break;
    case GIRARA_EVENT_SCROLL_DOWN:
      argument->n = ZOOM_OUT;
      break;
    default:
      return false;
Moritz Lipp's avatar
Moritz Lipp committed
413 414
  }

415
  return sc_zoom(session, argument, NULL, t);
416 417
}

Moritz Lipp's avatar
Moritz Lipp committed
418
bool
Moritz Lipp's avatar
Moritz Lipp committed
419
sc_navigate(girara_session_t* session, girara_argument_t* argument,
Moritz Lipp's avatar
Moritz Lipp committed
420
            girara_event_t* UNUSED(event), unsigned int t)
421
{
Moritz Lipp's avatar
Moritz Lipp committed
422 423 424 425 426
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(argument != NULL, false);
  g_return_val_if_fail(zathura->document != NULL, false);
Moritz Lipp's avatar
Moritz Lipp committed
427

428 429
  int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
  int new_page        = zathura_document_get_current_page_number(zathura->document);
Moritz Lipp's avatar
Moritz Lipp committed
430

431 432 433
  bool scroll_wrap = false;
  girara_setting_get(session, "scroll-wrap", &scroll_wrap);

434 435 436 437 438 439 440 441
  bool columns_per_row_offset = false;
  girara_setting_get(session, "advance-pages-per-row", &columns_per_row_offset);

  int offset = 1;
  if (columns_per_row_offset == true) {
    girara_setting_get(session, "pages-per-row", &offset);
  }

Moritz Lipp's avatar
Moritz Lipp committed
442
  t = (t == 0) ? (unsigned int) offset : t;
Moritz Lipp's avatar
Moritz Lipp committed
443
  if (argument->n == NEXT) {
Benoît Knecht's avatar
Benoît Knecht committed
444
    if (scroll_wrap == false) {
445 446 447 448
      new_page = new_page + t;
    } else {
      new_page = (new_page + t) % number_of_pages;
    }
Moritz Lipp's avatar
Moritz Lipp committed
449
  } else if (argument->n == PREVIOUS) {
Benoît Knecht's avatar
Benoît Knecht committed
450
    if (scroll_wrap == false) {
451 452 453 454 455 456
      new_page = new_page - t;
    } else {
      new_page = (new_page + number_of_pages - t) % number_of_pages;
    }
  }

Benoît Knecht's avatar
Benoît Knecht committed
457
  if ((new_page < 0 || new_page >= number_of_pages) && !scroll_wrap) {
458
    return false;
Moritz Lipp's avatar
Moritz Lipp committed
459 460
  }

461
  page_set(zathura, new_page);
Moritz Lipp's avatar
Moritz Lipp committed
462

Moritz Lipp's avatar
Moritz Lipp committed
463
  return false;
464 465
}

Moritz Lipp's avatar
Moritz Lipp committed
466 467
bool
sc_print(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
468
         girara_event_t* UNUSED(event), unsigned int UNUSED(t))
Moritz Lipp's avatar
Moritz Lipp committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
{
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

  if (zathura->document == NULL) {
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
    return false;
  }

  print(zathura);

  return true;
}

Moritz Lipp's avatar
Moritz Lipp committed
484
bool
485
sc_recolor(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
486
           girara_event_t* UNUSED(event), unsigned int UNUSED(t))
487
{
Moritz Lipp's avatar
Moritz Lipp committed
488
  g_return_val_if_fail(session != NULL, false);
Moritz Lipp's avatar
Moritz Lipp committed
489

Moritz Lipp's avatar
Moritz Lipp committed
490 491 492 493
  bool value = false;
  girara_setting_get(session, "recolor", &value);
  value = !value;
  girara_setting_set(session, "recolor", &value);
Moritz Lipp's avatar
Moritz Lipp committed
494

Moritz Lipp's avatar
Moritz Lipp committed
495
  return false;
496 497
}

Moritz Lipp's avatar
Moritz Lipp committed
498
bool
499
sc_reload(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
500
          girara_event_t* UNUSED(event), unsigned int UNUSED(t))
501
{
Moritz Lipp's avatar
Moritz Lipp committed
502
  g_return_val_if_fail(session != NULL, false);
Moritz Lipp's avatar
Moritz Lipp committed
503 504 505
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

Moritz Lipp's avatar
Moritz Lipp committed
506
  if (zathura->file_monitor.file_path == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
507 508 509 510
    return false;
  }

  /* close current document */
Moritz Lipp's avatar
Moritz Lipp committed
511
  document_close(zathura, true);
Moritz Lipp's avatar
Moritz Lipp committed
512 513

  /* reopen document */
Moritz Lipp's avatar
Moritz Lipp committed
514
  document_open(zathura, zathura->file_monitor.file_path, zathura->file_monitor.password);
Moritz Lipp's avatar
Moritz Lipp committed
515

Moritz Lipp's avatar
Moritz Lipp committed
516
  return false;
517 518
}

Moritz Lipp's avatar
Moritz Lipp committed
519
bool
520
sc_rotate(girara_session_t* session, girara_argument_t* argument,
Moritz Lipp's avatar
Moritz Lipp committed
521
          girara_event_t* UNUSED(event), unsigned int t)
522
{
Moritz Lipp's avatar
Moritz Lipp committed
523 524 525 526
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(zathura->document != NULL, false);
Moritz Lipp's avatar
Moritz Lipp committed
527

528
  unsigned int page_number = zathura_document_get_current_page_number(zathura->document);
529

530 531 532 533 534
  int angle = 90;
  if (argument != NULL && argument->n == ROTATE_CCW) {
    angle = 270;
  }

Moritz Lipp's avatar
Moritz Lipp committed
535
  /* update rotate value */
Moritz Lipp's avatar
Moritz Lipp committed
536
  t = (t == 0) ? 1 : t;
537
  unsigned int rotation = zathura_document_get_rotation(zathura->document);
Moritz Lipp's avatar
Moritz Lipp committed
538
  zathura_document_set_rotation(zathura->document, (rotation + angle * t) % 360);
Moritz Lipp's avatar
Moritz Lipp committed
539

540 541 542 543
  /* update scale */
  girara_argument_t new_argument = { zathura_document_get_adjust_mode(zathura->document), NULL };
  sc_adjust_window(zathura->ui.session, &new_argument, NULL, 0);

Moritz Lipp's avatar
Moritz Lipp committed
544
  /* render all pages again */
545
  render_all(zathura);
Moritz Lipp's avatar
Moritz Lipp committed
546

547 548
  page_set_delayed(zathura, page_number);

Moritz Lipp's avatar
Moritz Lipp committed
549
  return false;
550 551
}

Moritz Lipp's avatar
Moritz Lipp committed
552
bool
Moritz Lipp's avatar
Moritz Lipp committed
553
sc_scroll(girara_session_t* session, girara_argument_t* argument,
Moritz Lipp's avatar
Moritz Lipp committed
554
          girara_event_t* UNUSED(event), unsigned int UNUSED(t))
555
{
Moritz Lipp's avatar
Moritz Lipp committed
556 557 558 559
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(argument != NULL, false);
560 561 562
  if (zathura->document == NULL) {
    return false;
  }
Moritz Lipp's avatar
Moritz Lipp committed
563

Moritz Lipp's avatar
Moritz Lipp committed
564
  GtkAdjustment* adjustment = NULL;
565
  if ( (argument->n == LEFT) || (argument->n == FULL_LEFT) || (argument->n == HALF_LEFT) ||
Moritz Lipp's avatar
Moritz Lipp committed
566
       (argument->n == RIGHT) || (argument->n == FULL_RIGHT) || (argument->n == HALF_RIGHT)) {
Moritz Lipp's avatar
Moritz Lipp committed
567
    adjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(session->gtk.view));
Moritz Lipp's avatar
Moritz Lipp committed
568
  } else {
Moritz Lipp's avatar
Moritz Lipp committed
569
    adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(session->gtk.view));
Moritz Lipp's avatar
Moritz Lipp committed
570
  }
Moritz Lipp's avatar
Moritz Lipp committed
571

572 573 574
  gdouble view_size                  = gtk_adjustment_get_page_size(adjustment);
  gdouble value                      = gtk_adjustment_get_value(adjustment);
  gdouble max                        = gtk_adjustment_get_upper(adjustment) - view_size;
575
  zathura->global.update_page_number = true;
576

577 578
  float scroll_step = 40;
  girara_setting_get(session, "scroll-step", &scroll_step);
Julian Orth's avatar
Julian Orth committed
579 580 581 582 583
  float scroll_hstep = -1;
  girara_setting_get(session, "scroll-hstep", &scroll_hstep);
  if (scroll_hstep < 0) {
    scroll_hstep = scroll_step;
  }
584
  float scroll_full_overlap = 0.0;
585
  girara_setting_get(session, "scroll-full-overlap", &scroll_full_overlap);
586 587
  bool scroll_page_aware = false;
  girara_setting_get(session, "scroll-page-aware", &scroll_page_aware);
588

589 590 591
  bool scroll_wrap = false;
  girara_setting_get(session, "scroll-wrap", &scroll_wrap);

592 593 594
  int padding = 1;
  girara_setting_get(session, "page-padding", &padding);

595
  gdouble new_value;
Moritz Lipp's avatar
Moritz Lipp committed
596

Moritz Lipp's avatar
Moritz Lipp committed
597
  switch(argument->n) {
Moritz Lipp's avatar
Moritz Lipp committed
598
    case FULL_UP:
599
    case FULL_LEFT:
600
      new_value = value - (1.0 - scroll_full_overlap) * view_size - padding;
Moritz Lipp's avatar
Moritz Lipp committed
601 602
      break;
    case FULL_DOWN:
603
    case FULL_RIGHT:
604
      new_value = value + (1.0 - scroll_full_overlap) * view_size + padding;
Moritz Lipp's avatar
Moritz Lipp committed
605 606
      break;
    case HALF_UP:
607
    case HALF_LEFT:
608
      new_value = value - ((view_size + padding) / 2);
Moritz Lipp's avatar
Moritz Lipp committed
609 610
      break;
    case HALF_DOWN:
611
    case HALF_RIGHT:
612
      new_value = value + ((view_size + padding) / 2);
Moritz Lipp's avatar
Moritz Lipp committed
613 614
      break;
    case LEFT:
Julian Orth's avatar
Julian Orth committed
615 616
      new_value = value - scroll_hstep;
      break;
Moritz Lipp's avatar
Moritz Lipp committed
617
    case UP:
618
      new_value = value - scroll_step;
Moritz Lipp's avatar
Moritz Lipp committed
619 620
      break;
    case RIGHT:
Julian Orth's avatar
Julian Orth committed
621 622
      new_value = value + scroll_hstep;
      break;
Moritz Lipp's avatar
Moritz Lipp committed
623
    case DOWN:
624
      new_value = value + scroll_step;
Moritz Lipp's avatar
Moritz Lipp committed
625 626 627 628 629 630 631 632
      break;
    case TOP:
      new_value = 0;
      break;
    case BOTTOM:
      new_value = max;
      break;
    default:
Moritz Lipp's avatar
Moritz Lipp committed
633
      new_value = value;
Moritz Lipp's avatar
Moritz Lipp committed
634 635
  }

636 637 638 639 640 641 642
  if (scroll_wrap == true) {
    if (new_value < 0)
      new_value = max;
    else if (new_value > max)
      new_value = 0;
  }

Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
643
  if (scroll_page_aware == true) {
644 645 646 647 648 649 650 651 652 653 654
    int page_offset;
    double page_size;

    {
      unsigned int page_id = zathura_document_get_current_page_number(zathura->document);
      zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
      page_offset_t offset;
      page_calculate_offset(zathura, page, &offset);

      double scale = zathura_document_get_scale(zathura->document);

Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
655 656
      if ((argument->n == LEFT) || (argument->n == FULL_LEFT) || (argument->n == HALF_LEFT) ||
          (argument->n == RIGHT) || (argument->n == FULL_RIGHT) || (argument->n == HALF_RIGHT)) {
657 658 659 660 661 662 663 664 665 666
        page_offset = offset.x;
        page_size = zathura_page_get_width(page) * scale;
      } else {
        page_offset = offset.y;
        page_size = zathura_page_get_height(page) * scale;
      }

      page_offset -= padding / 2;
      page_size   += padding;
    }
Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
667 668 669 670 671

    if ((argument->n == FULL_DOWN) || (argument->n == HALF_DOWN) ||
        (argument->n == FULL_RIGHT) || (argument->n == HALF_RIGHT)) {
      if ((page_offset > value) &&
          (page_offset < value + view_size)) {
672
        new_value = page_offset;
Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
673 674
      } else if ((page_offset <= value) &&
                 (page_offset + page_size < value + view_size)) {
675
        new_value = page_offset + page_size + 1;
Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
676 677
      } else if ((page_offset <= value) &&
                 (page_offset + page_size < new_value + view_size)) {
678 679
        new_value = page_offset + page_size - view_size + 1;
      }
Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
680 681 682 683
    } else if ((argument->n == FULL_UP) || (argument->n == HALF_UP) ||
               (argument->n == FULL_LEFT) || (argument->n == HALF_LEFT)) {
      if ((page_offset + 1 >= value) &&
          (page_offset < value + view_size)) {
684
        new_value = page_offset - view_size;
Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
685 686
      } else if ((page_offset <= value) &&
                 (page_offset + page_size + 1 < value + view_size)) {
687
        new_value = page_offset + page_size - view_size;
Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
688 689
      } else if ((page_offset <= value) &&
                 (page_offset > new_value)) {
690 691
        new_value = page_offset;
      }
Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
692
    }
693 694
  }

695
  set_adjustment(adjustment, new_value);
Moritz Lipp's avatar
Moritz Lipp committed
696

Moritz Lipp's avatar
Moritz Lipp committed
697
  return false;
698 699
}

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

bool
sc_jumplist(girara_session_t* session, girara_argument_t* argument, girara_event_t* UNUSED(event), unsigned int UNUSED(t))
{
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(argument != NULL, false);
  g_return_val_if_fail(zathura->document != NULL, false);

  zathura_jump_t* jump = NULL;
  switch(argument->n) {
    case FORWARD:
      zathura_jumplist_save(zathura);
      zathura_jumplist_forward(zathura);
      jump = zathura_jumplist_current(zathura);
      break;

    case BACKWARD:
      zathura_jumplist_save(zathura);
      zathura_jumplist_backward(zathura);
      jump = zathura_jumplist_current(zathura);
      break;
  }

725 726 727 728 729
  if (jump != NULL) {
    page_set(zathura, jump->page);
    const double s = zathura_document_get_scale(zathura->document);
    position_set_delayed(zathura, jump->x * s, jump->y * s);
  }
730 731 732 733

  return false;
}

Moritz Lipp's avatar
Moritz Lipp committed
734
bool
Moritz Lipp's avatar
Moritz Lipp committed
735
sc_search(girara_session_t* session, girara_argument_t* argument,
Moritz Lipp's avatar
Moritz Lipp committed
736
          girara_event_t* UNUSED(event), unsigned int UNUSED(t))
737
{
Moritz Lipp's avatar
Moritz Lipp committed
738 739 740 741 742
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(argument != NULL, false);
  g_return_val_if_fail(zathura->document != NULL, false);
Moritz Lipp's avatar
Moritz Lipp committed
743

744 745
  const int num_pages = zathura_document_get_number_of_pages(zathura->document);
  const int cur_page  = zathura_document_get_current_page_number(zathura->document);
746

747
  int diff = argument->n == FORWARD ? 1 : -1;
748 749
  if (zathura->global.search_direction == BACKWARD)
    diff = -diff;
750

Sebastian Ramacher's avatar
Sebastian Ramacher committed
751 752 753
  zathura_page_t* target_page = NULL;
  int target_idx = 0;

754 755
  for (int page_id = 0; page_id < num_pages; ++page_id) {
    int tmp = cur_page + diff * page_id;
756
    zathura_page_t* page = zathura_document_get_page(zathura->document, (tmp + num_pages) % num_pages);
757 758 759 760
    if (page == NULL) {
      continue;
    }

761
    GtkWidget* page_widget = zathura_page_get_widget(zathura, page);
Moritz Lipp's avatar
Moritz Lipp committed
762

763
    int num_search_results = 0, current = -1;
Moritz Lipp's avatar
Moritz Lipp committed
764
    g_object_get(page_widget, "search-current", &current,
Moritz Lipp's avatar
Moritz Lipp committed
765
                 "search-length", &num_search_results, NULL);
766 767 768 769 770 771
    if (num_search_results == 0 || current == -1) {
      continue;
    }

    if (diff == 1 && current < num_search_results - 1) {
      /* the next result is on the same page */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
772 773
      target_page = page;
      target_idx = current + 1;
774
    } else if (diff == -1 && current > 0) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
775 776
      target_page = page;
      target_idx = current - 1;
777 778
    } else {
      /* the next result is on a different page */
779 780
      zathura_jumplist_save(zathura);

Moritz Lipp's avatar
Moritz Lipp committed
781
      g_object_set(page_widget, "search-current", -1, NULL);
782 783 784

      for (int npage_id = 1; page_id < num_pages; ++npage_id) {
        int ntmp = cur_page + diff * (page_id + npage_id);
785 786
        zathura_page_t* npage = zathura_document_get_page(zathura->document, (ntmp + 2*num_pages) % num_pages);
        zathura_document_set_current_page_number(zathura->document, zathura_page_get_index(npage));
787
        GtkWidget* npage_page_widget = zathura_page_get_widget(zathura, npage);
Moritz Lipp's avatar
Moritz Lipp committed
788
        g_object_get(npage_page_widget, "search-length", &num_search_results, NULL);
789
        if (num_search_results != 0) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
790 791
          target_page = npage;
          target_idx = diff == 1 ? 0 : num_search_results - 1;
792 793 794
          break;
        }
      }
795 796

      zathura_jumplist_add(zathura);
797
    }
798

799 800
    break;
  }
Moritz Lipp's avatar
Moritz Lipp committed
801

Sebastian Ramacher's avatar
Sebastian Ramacher committed
802 803
  if (target_page != NULL) {
    girara_list_t* results = NULL;
804
    GtkWidget* page_widget = zathura_page_get_widget(zathura, target_page);
Moritz Lipp's avatar
Moritz Lipp committed
805 806
    g_object_set(page_widget, "search-current", target_idx, NULL);
    g_object_get(page_widget, "search-results", &results, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
807 808 809 810

    zathura_rectangle_t* rect = girara_list_nth(results, target_idx);
    zathura_rectangle_t rectangle = recalc_rectangle(target_page, *rect);
    page_offset_t offset;
811
    page_calculate_offset(zathura, target_page, &offset);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
812 813 814

    GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
    int y = offset.y - gtk_adjustment_get_page_size(view_vadjustment) / 2 + rectangle.y1;
815
    set_adjustment(view_vadjustment, y);
Julian Orth's avatar
Julian Orth committed
816 817 818 819 820 821 822 823

    bool search_hadjust = true;
    girara_setting_get(session, "search-hadjust", &search_hadjust);
    if (search_hadjust == true) {
      GtkAdjustment* view_hadjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
      int x = offset.x - gtk_adjustment_get_page_size(view_hadjustment) / 2 + rectangle.x1;
      set_adjustment(view_hadjustment, x);
    }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
824 825
  }

Moritz Lipp's avatar
Moritz Lipp committed
826
  return false;
827 828
}

Moritz Lipp's avatar
Moritz Lipp committed
829
bool
830
sc_navigate_index(girara_session_t* session, girara_argument_t* argument,
Moritz Lipp's avatar
Moritz Lipp committed
831
                  girara_event_t* UNUSED(event), unsigned int UNUSED(t))
832
{
Moritz Lipp's avatar
Moritz Lipp committed
833 834 835 836 837 838
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  g_return_val_if_fail(argument != NULL, false);
  g_return_val_if_fail(zathura->document != NULL, false);

Moritz Lipp's avatar
Moritz Lipp committed
839 840 841 842 843 844 845 846
  if(zathura->ui.index == NULL) {
    return false;
  }

  GtkTreeView *tree_view = gtk_container_get_children(GTK_CONTAINER(zathura->ui.index))->data;
  GtkTreePath *path;

  gtk_tree_view_get_cursor(tree_view, &path, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
847
  if (path == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
848 849 850 851 852 853 854 855 856 857 858
    return false;
  }

  GtkTreeModel *model = gtk_tree_view_get_model(tree_view);
  GtkTreeIter   iter;
  GtkTreeIter   child_iter;

  gboolean is_valid_path = TRUE;

  switch(argument->n) {
    case UP:
859
      if (gtk_tree_path_prev(path) == FALSE) {
860 861 862
        /* For some reason gtk_tree_path_up returns TRUE although we're not
         * moving anywhere. */
        is_valid_path = gtk_tree_path_up(path) && (gtk_tree_path_get_depth(path) > 0);
Moritz Lipp's avatar
Moritz Lipp committed
863 864 865 866 867
      } else { /* row above */
        while(gtk_tree_view_row_expanded(tree_view, path)) {
          gtk_tree_model_get_iter(model, &iter, path);
          /* select last child */
          gtk_tree_model_iter_nth_child(model, &child_iter, &iter,
Moritz Lipp's avatar
Moritz Lipp committed
868
                                        gtk_tree_model_iter_n_children(model, &iter)-1);
Moritz Lipp's avatar
Moritz Lipp committed
869 870 871 872 873 874
          gtk_tree_path_free(path);
          path = gtk_tree_model_get_path(model, &child_iter);
        }
      }
      break;
    case COLLAPSE:
875
      if (gtk_tree_view_collapse_row(tree_view, path) == FALSE
Moritz Lipp's avatar
Moritz Lipp committed
876
          && gtk_tree_path_get_depth(path) > 1) {
Moritz Lipp's avatar
Moritz Lipp committed
877 878 879 880 881
        gtk_tree_path_up(path);
        gtk_tree_view_collapse_row(tree_view, path);
      }
      break;
    case DOWN:
882
      if (gtk_tree_view_row_expanded(tree_view, path) == TRUE) {
Moritz Lipp's avatar
Moritz Lipp committed
883 884 885 886 887
        gtk_tree_path_down(path);
      } else {
        do {
          gtk_tree_model_get_iter(model, &iter, path);
          if (gtk_tree_model_iter_next(model, &iter)) {
888
            gtk_tree_path_free(path);
Moritz Lipp's avatar
Moritz Lipp committed
889 890 891 892
            path = gtk_tree_model_get_path(model, &iter);
            break;
          }
        } while((is_valid_path = (gtk_tree_path_get_depth(path) > 1))
Moritz Lipp's avatar
Moritz Lipp committed
893
                && gtk_tree_path_up(path));
Moritz Lipp's avatar
Moritz Lipp committed
894 895 896
      }
      break;
    case EXPAND:
897
      if (gtk_tree_view_expand_row(tree_view, path, FALSE)) {
Moritz Lipp's avatar
Moritz Lipp committed
898 899 900
        gtk_tree_path_down(path);
      }
      break;
901 902 903 904 905 906 907 908 909
    case EXPAND_ALL:
      gtk_tree_view_expand_all(tree_view);
      break;
    case COLLAPSE_ALL:
      gtk_tree_view_collapse_all(tree_view);
      gtk_tree_path_free(path);
      path = gtk_tree_path_new_first();
      gtk_tree_view_set_cursor(tree_view, path, NULL, FALSE);
      break;
Moritz Lipp's avatar
Moritz Lipp committed
910 911
    case SELECT:
      cb_index_row_activated(tree_view, path, NULL, zathura);
912
      gtk_tree_path_free(path);
Moritz Lipp's avatar
Moritz Lipp committed
913 914 915 916 917 918 919 920 921
      return false;
  }

  if (is_valid_path) {
    gtk_tree_view_set_cursor(tree_view, path, NULL, FALSE);
  }

  gtk_tree_path_free(path);

Moritz Lipp's avatar
Moritz Lipp committed
922
  return false;
923 924
}

Moritz Lipp's avatar
Moritz Lipp committed
925
bool
926
sc_toggle_index(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
927
                girara_event_t* UNUSED(event), unsigned int UNUSED(t))
928
{
Moritz Lipp's avatar
Moritz Lipp committed
929 930 931
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
932 933 934
  if (zathura->document == NULL) {
    return false;
  }
935 936 937 938 939

  girara_tree_node_t* document_index = NULL;
  GtkWidget* treeview                = NULL;
  GtkTreeModel* model                = NULL;
  GtkCellRenderer* renderer          = NULL;
940
  GtkCellRenderer* renderer2         = NULL;
941

942
  if (zathura->ui.index == NULL) {
943
    /* create new index widget */
944
    zathura->ui.index = gtk_scrolled_window_new(NULL, NULL);
945

946
    if (zathura->ui.index == NULL) {
947 948 949
      goto error_ret;
    }

950
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(zathura->ui.index),
Moritz Lipp's avatar
Moritz Lipp committed
951
                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
952 953

    /* create index */
954
    document_index = zathura_document_index_generate(zathura->document, NULL);
955
    if (document_index == NULL) {
956
      girara_notify(session, GIRARA_WARNING, _("This document does not contain any index"));
957 958 959
      goto error_free;
    }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
960
    model = GTK_TREE_MODEL(gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
    if (model == NULL) {
      goto error_free;
    }

    treeview = gtk_tree_view_new_with_model(model);
    if (treeview == NULL) {
      goto error_free;
    }

    g_object_unref(model);

    renderer = gtk_cell_renderer_text_new();
    if (renderer == NULL) {
      goto error_free;
    }

977 978 979 980 981
    renderer2 = gtk_cell_renderer_text_new();
    if (renderer2 == NULL) {
      goto error_free;
    }

982 983 984 985 986
    document_index_build(model, NULL, document_index);
    girara_node_free(document_index);

    /* setup widget */
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW (treeview), 0, "Title", renderer, "markup", 0, NULL);
987
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW (treeview), 1, "Target", renderer2, "text", 1, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
988

989 990 991
    gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
    g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
    g_object_set(G_OBJECT(gtk_tree_view_get_column(GTK_TREE_VIEW(treeview), 0)), "expand", TRUE, NULL);
992
    gtk_tree_view_column_set_alignment(gtk_tree_view_get_column(GTK_TREE_VIEW(treeview), 1), 1.0f);
993
    gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview), gtk_tree_path_new_first(), NULL, FALSE);
994
    g_signal_connect(G_OBJECT(treeview), "row-activated", G_CALLBACK(cb_index_row_activated), zathura);
995

996
    gtk_container_add(GTK_CONTAINER(zathura->ui.index), treeview);
997 998 999
    gtk_widget_show(treeview);
  }

Moritz Lipp's avatar
Moritz Lipp committed
1000 1001 1002
  static double vvalue = 0;
  static double hvalue = 0;

1003
  if (gtk_widget_get_visible(GTK_WIDGET(zathura->ui.index))) {
1004
    girara_set_view(session, zathura->ui.page_widget_alignment);
1005
    gtk_widget_hide(GTK_WIDGET(zathura->ui.index));
1006
    girara_mode_set(zathura->ui.session, zathura->modes.normal);
Moritz Lipp's avatar
Moritz Lipp committed
1007 1008

    /* reset adjustment */
1009
    position_set_delayed(zathura, hvalue, vvalue);
1010
  } else {
Moritz Lipp's avatar
Moritz Lipp committed
1011
    /* save adjustment */
1012 1013 1014
    GtkAdjustment* vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(session->gtk.view));
    GtkAdjustment* hadjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(session->gtk.view));

Moritz Lipp's avatar
Moritz Lipp committed
1015 1016 1017
    vvalue = gtk_adjustment_get_value(vadjustment);
    hvalue = gtk_adjustment_get_value(hadjustment);

1018 1019 1020
    /* save current position to the jumplist */
    zathura_jumplist_save(zathura);

1021 1022
    girara_set_view(session, zathura->ui.index);
    gtk_widget_show(GTK_WIDGET(zathura->ui.index));
1023
    girara_mode_set(zathura->ui.session, zathura->modes.index);
1024 1025 1026 1027 1028 1029
  }

  return false;

error_free:

1030 1031 1032
  if (zathura->ui.index != NULL) {
    g_object_ref_sink(zathura->ui.index);
    zathura->ui.index = NULL;
1033 1034 1035 1036 1037
  }

  if (document_index != NULL) {
    girara_node_free(document_index);
  }
Moritz Lipp's avatar
Moritz Lipp committed
1038

1039 1040
error_ret:

Moritz Lipp's avatar
Moritz Lipp committed
1041
  return false;
1042 1043
}

Moritz Lipp's avatar
Moritz Lipp committed
1044 1045
bool
sc_toggle_page_mode(girara_session_t* session, girara_argument_t*
Moritz Lipp's avatar
Moritz Lipp committed
1046
                    UNUSED(argument), girara_event_t* UNUSED(event), unsigned int UNUSED(t))
Moritz Lipp's avatar
Moritz Lipp committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
{
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

  if (zathura->document == NULL) {
    girara_notify(session, GIRARA_WARNING, _("No document opened."));
    return false;
  }

  int pages_per_row = 1;
  girara_setting_get(zathura->ui.session, "pages-per-row", &pages_per_row);

  static int tmp = 2;
  int value = 1;
  if (pages_per_row == 1) {
    value = tmp;
  } else {
    tmp = pages_per_row;
  }

  girara_setting_set(zathura->ui.session, "pages-per-row", &value);

  return true;
}

Moritz Lipp's avatar
Moritz Lipp committed
1073
bool
1074
sc_toggle_fullscreen(girara_session_t* session, girara_argument_t*
Moritz Lipp's avatar
Moritz Lipp committed
1075
                     UNUSED(argument), girara_event_t* UNUSED(event), unsigned int UNUSED(t))
1076
{
Moritz Lipp's avatar
Moritz Lipp committed
1077
  g_return_val_if_fail(session != NULL, false);
1078 1079
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
1080

1081 1082 1083 1084 1085
  if (zathura->document == NULL) {
    girara_notify(session, GIRARA_WARNING, _("No document opened."));
    return false;
  }

1086
  static bool fullscreen = false;
Moritz Lipp's avatar
Moritz Lipp committed
1087
  static int pages_per_row = 1;
1088
  static int first_page_column = 1;
1089
  static double zoom = 1.0;
Moritz Lipp's avatar
Moritz Lipp committed
1090 1091 1092 1093 1094

  if (fullscreen == true) {
    /* reset pages per row */
    girara_setting_set(session, "pages-per-row", &pages_per_row);

1095 1096 1097
    /* reset first page column */
    girara_setting_set(session, "first-page-column", &first_page_column);

Moritz Lipp's avatar
Moritz Lipp committed
1098 1099 1100 1101
    /* show status bar */
    gtk_widget_show(GTK_WIDGET(session->gtk.statusbar));

    /* set full screen */
1102
    gtk_window_unfullscreen(GTK_WINDOW(session->gtk.window));
1103 1104

    /* reset scale */
1105
    zathura_document_set_scale(zathura->document, zoom);
1106
    render_all(zathura);
1107
    page_set_delayed(zathura, zathura_document_get_current_page_number(zathura->document));
1108 1109 1110

    /* setm ode */
    girara_mode_set(session, zathura