shortcuts.c 31.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

Moritz Lipp's avatar
Moritz Lipp committed
21
bool
22
sc_abort(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
23
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
24
{
Moritz Lipp's avatar
Moritz Lipp committed
25
  g_return_val_if_fail(session != NULL, false);
26 27 28
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

29 30 31
  bool clear_search = true;
  girara_setting_get(session, "abort-clear-search", &clear_search);

Moritz Lipp's avatar
Moritz Lipp committed
32
  if (zathura->document != NULL) {
33
    unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
34 35
    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
36 37 38 39
      if (page == NULL) {
        continue;
      }

40
      g_object_set(zathura_page_get_widget(zathura, page), "draw-links", FALSE, NULL);
41 42 43
      if (clear_search) {
        g_object_set(zathura_page_get_widget(zathura, page), "search-results", NULL, NULL);
      }
44 45
    }
  }
Moritz Lipp's avatar
Moritz Lipp committed
46

Moritz Lipp's avatar
Moritz Lipp committed
47
  girara_mode_set(session, session->modes.normal);
48
  girara_sc_abort(session, NULL, NULL, 0);
Moritz Lipp's avatar
Moritz Lipp committed
49

Moritz Lipp's avatar
Moritz Lipp committed
50
  return false;
51 52
}

53 54 55
bool
sc_adjust_window(girara_session_t* session, girara_argument_t* argument,
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
56
{
57 58 59 60
  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
61

62 63
  unsigned int pages_per_row = 1;
  girara_setting_get(session, "pages-per-row", &pages_per_row);
Moritz Lipp's avatar
Moritz Lipp committed
64

65 66 67
  unsigned int first_page_column = 1;
  girara_setting_get(session, "first-page-column", &first_page_column);

68
  if (zathura->ui.page_widget == NULL || zathura->document == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
69 70 71
    goto error_ret;
  }

72
  float old_zoom = zathura_document_get_scale(zathura->document);
73 74
  zathura_document_set_adjust_mode(zathura->document, argument->n);
  if (argument->n == ZATHURA_ADJUST_NONE) {
75 76 77
    /* there is nothing todo */
    goto error_ret;
  }
78

Moritz Lipp's avatar
Moritz Lipp committed
79
  /* get window size */
80 81
  GtkAllocation allocation;
  gtk_widget_get_allocation(session->gtk.view, &allocation);
Moritz Lipp's avatar
Moritz Lipp committed
82 83
  double width  = allocation.width;
  double height = allocation.height;
Moritz Lipp's avatar
Moritz Lipp committed
84

85 86 87 88 89 90 91
  /* correct view size */
  if (gtk_widget_get_visible(GTK_WIDGET(session->gtk.inputbar)) == true) {
    gtk_widget_get_allocation(session->gtk.inputbar, &allocation);
    width  += allocation.width;
    height += allocation.height;
  }

Moritz Lipp's avatar
Moritz Lipp committed
92
  /* calculate total width and max-height */
93 94 95 96
  double total_width  = 0;
  double total_height = 0;
  double max_height   = 0;
  double max_width    = 0;
Moritz Lipp's avatar
Moritz Lipp committed
97

98
  unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
99
  for (unsigned int page_id = 0; page_id < pages_per_row; page_id++) {
100
    if (page_id == number_of_pages) {
Moritz Lipp's avatar
Moritz Lipp committed
101 102 103
      break;
    }

104
    zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
Moritz Lipp's avatar
Moritz Lipp committed
105
    if (page == NULL) {
106
      goto error_ret;
Moritz Lipp's avatar
Moritz Lipp committed
107 108
    }

109 110 111 112 113 114 115 116 117
    unsigned int page_height = zathura_page_get_height(page);
    unsigned int page_width  = zathura_page_get_width(page);

    if (page_height > max_height) {
      max_height = page_height;
    }

    if (page_width > max_width) {
      max_width = page_width;
Moritz Lipp's avatar
Moritz Lipp committed
118 119
    }

120 121
    total_width  += page_width;
    total_height += page_height;
Moritz Lipp's avatar
Moritz Lipp committed
122 123
  }

124
  unsigned int rotation = zathura_document_get_rotation(zathura->document);
125
  double page_ratio     = max_height / total_width;
Moritz Lipp's avatar
Moritz Lipp committed
126 127 128
  double window_ratio   = height / width;

  if (rotation == 90 || rotation == 270) {
129
    page_ratio = max_width / total_height;
Moritz Lipp's avatar
Moritz Lipp committed
130 131
  }

132 133 134 135 136 137 138 139 140
  switch (argument->n) {
    case ZATHURA_ADJUST_WIDTH:
      if (rotation == 0 || rotation == 180) {
        zathura_document_set_scale(zathura->document, width / total_width);
      } else {
        zathura_document_set_scale(zathura->document, width / total_height);
      }
      break;
    case ZATHURA_ADJUST_BESTFIT:
Moritz Lipp's avatar
Moritz Lipp committed
141
      if (rotation == 0 || rotation == 180) {
Moritz Lipp's avatar
Moritz Lipp committed
142
        if (page_ratio < window_ratio) {
Moritz Lipp's avatar
Moritz Lipp committed
143
          zathura_document_set_scale(zathura->document, width / total_width);
144
        } else {
145
          zathura_document_set_scale(zathura->document, height / max_height);
146 147
        }
      } else {
Moritz Lipp's avatar
Moritz Lipp committed
148
        if (page_ratio < window_ratio) {
Moritz Lipp's avatar
Moritz Lipp committed
149
          zathura_document_set_scale(zathura->document, width / total_height);
150
        } else {
151
          zathura_document_set_scale(zathura->document, height / max_width);
152 153 154 155 156
        }
      }
      break;
    default:
      goto error_ret;
Moritz Lipp's avatar
Moritz Lipp committed
157 158
  }

159
  /* keep position */
160
  readjust_view_after_zooming(zathura, old_zoom, false);
161

Moritz Lipp's avatar
Moritz Lipp committed
162 163 164 165
  /* re-render all pages */
  render_all(zathura);

error_ret:
Moritz Lipp's avatar
Moritz Lipp committed
166

167
  return false;
168 169
}

Moritz Lipp's avatar
Moritz Lipp committed
170
bool
Moritz Lipp's avatar
Moritz Lipp committed
171 172
sc_change_mode(girara_session_t* session, girara_argument_t* argument,
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
173
{
Moritz Lipp's avatar
Moritz Lipp committed
174 175 176 177
  g_return_val_if_fail(session != NULL, false);

  girara_mode_set(session, argument->n);

Moritz Lipp's avatar
Moritz Lipp committed
178
  return false;
179 180
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
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) {
205 206 207 208
      const char* file_path = zathura_document_get_path(zathura->document);
      if (file_path == NULL) {
        return false;
      }
209

210 211 212 213 214
      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);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

      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
235
bool
236
sc_follow(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
237
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
238
{
Moritz Lipp's avatar
Moritz Lipp committed
239
  g_return_val_if_fail(session != NULL, false);
240 241 242
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

Moritz Lipp's avatar
Moritz Lipp committed
243
  if (zathura->document == NULL || zathura->ui.session == NULL) {
244 245 246
    return false;
  }

Moritz Lipp's avatar
Moritz Lipp committed
247
  /* set pages to draw links */
Moritz Lipp's avatar
Moritz Lipp committed
248
  bool show_links = false;
Moritz Lipp's avatar
Moritz Lipp committed
249
  unsigned int page_offset = 0;
250 251 252
  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);
253 254 255 256
    if (page == NULL) {
      continue;
    }

257
    GtkWidget* page_widget = zathura_page_get_widget(zathura, page);
Moritz Lipp's avatar
Moritz Lipp committed
258 259 260
    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);
261

Moritz Lipp's avatar
Moritz Lipp committed
262
      int number_of_links = 0;
Moritz Lipp's avatar
Moritz Lipp committed
263
      g_object_get(page_widget, "number-of-links", &number_of_links, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
264 265 266
      if (number_of_links != 0) {
        show_links = true;
      }
Moritz Lipp's avatar
Moritz Lipp committed
267
      g_object_set(page_widget, "offset-links", page_offset, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
268
      page_offset += number_of_links;
Moritz Lipp's avatar
Moritz Lipp committed
269
    } else {
Moritz Lipp's avatar
Moritz Lipp committed
270
      g_object_set(page_widget, "draw-links", FALSE, NULL);
271 272 273
    }
  }

Moritz Lipp's avatar
Moritz Lipp committed
274
  /* ask for input */
Moritz Lipp's avatar
Moritz Lipp committed
275
  if (show_links == true) {
Moritz Lipp's avatar
Moritz Lipp committed
276
    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
277
  }
Moritz Lipp's avatar
Moritz Lipp committed
278

Moritz Lipp's avatar
Moritz Lipp committed
279 280
  return false;
}
281

Moritz Lipp's avatar
Moritz Lipp committed
282
bool
Moritz Lipp's avatar
Moritz Lipp committed
283
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
284
{
Moritz Lipp's avatar
Moritz Lipp committed
285 286 287 288 289
  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);
290

Moritz Lipp's avatar
Moritz Lipp committed
291
  if (t != 0) {
292
    /* add offset */
293
    unsigned int page_offset = zathura_document_get_page_offset(zathura->document);
294 295
    if (page_offset > 0) {
      t += page_offset;
296 297
    }

Moritz Lipp's avatar
Moritz Lipp committed
298 299
    page_set(zathura, t - 1);
  } else if (argument->n == TOP) {
Moritz Lipp's avatar
Moritz Lipp committed
300
    page_set(zathura, 0);
Moritz Lipp's avatar
Moritz Lipp committed
301
  } else if (argument->n == BOTTOM) {
302
    page_set(zathura, zathura_document_get_number_of_pages(zathura->document) - 1);
303 304
  }

Moritz Lipp's avatar
Moritz Lipp committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  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
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  static int x = 0;
  static int y = 0;

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

  switch (event->type) {
    /* scroll */
    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);

    /* drag */
    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));
      y_adj =gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(session->gtk.view));

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

Moritz Lipp's avatar
Moritz Lipp committed
352 353 354 355 356 357 358
      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;

    /* unhandled events */
    default:
      break;
Moritz Lipp's avatar
Moritz Lipp committed
359 360
  }

Moritz Lipp's avatar
Moritz Lipp committed
361
  return false;
362 363
}

Moritz Lipp's avatar
Moritz Lipp committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377
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 */
378 379 380 381 382 383 384 385 386
  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
387 388
  }

389
  return sc_zoom(session, argument, NULL, t);
390 391
}

Moritz Lipp's avatar
Moritz Lipp committed
392
bool
Moritz Lipp's avatar
Moritz Lipp committed
393 394
sc_navigate(girara_session_t* session, girara_argument_t* argument,
    girara_event_t* UNUSED(event), unsigned int t)
395
{
Moritz Lipp's avatar
Moritz Lipp committed
396 397 398 399 400
  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
401

402 403
  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
404

405 406 407
  bool scroll_wrap = false;
  girara_setting_get(session, "scroll-wrap", &scroll_wrap);

408 409 410 411 412 413 414 415
  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
416
  t = (t == 0) ? (unsigned int) offset : t;
Moritz Lipp's avatar
Moritz Lipp committed
417
  if (argument->n == NEXT) {
418 419 420 421 422
    if (scroll_wrap == true) {
      new_page = new_page + t;
    } else {
      new_page = (new_page + t) % number_of_pages;
    }
Moritz Lipp's avatar
Moritz Lipp committed
423
  } else if (argument->n == PREVIOUS) {
424 425 426 427 428 429 430 431 432
    if (scroll_wrap == true) {
      new_page = new_page - t;
    } else {
      new_page = (new_page + number_of_pages - t) % number_of_pages;
    }
  }

  if (scroll_wrap == true && (new_page < 0 || new_page >= number_of_pages)) {
    return false;
Moritz Lipp's avatar
Moritz Lipp committed
433 434
  }

435
  page_set(zathura, new_page);
Moritz Lipp's avatar
Moritz Lipp committed
436

Moritz Lipp's avatar
Moritz Lipp committed
437
  return false;
438 439
}

Moritz Lipp's avatar
Moritz Lipp committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
bool
sc_print(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) {
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
    return false;
  }

  print(zathura);

  return true;
}

Moritz Lipp's avatar
Moritz Lipp committed
458
bool
459
sc_recolor(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
460
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
461
{
Moritz Lipp's avatar
Moritz Lipp committed
462
  g_return_val_if_fail(session != NULL, false);
Moritz Lipp's avatar
Moritz Lipp committed
463

Moritz Lipp's avatar
Moritz Lipp committed
464 465 466 467
  bool value = false;
  girara_setting_get(session, "recolor", &value);
  value = !value;
  girara_setting_set(session, "recolor", &value);
Moritz Lipp's avatar
Moritz Lipp committed
468

Moritz Lipp's avatar
Moritz Lipp committed
469
  return false;
470 471
}

Moritz Lipp's avatar
Moritz Lipp committed
472
bool
473
sc_reload(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
474
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
475
{
Moritz Lipp's avatar
Moritz Lipp committed
476
  g_return_val_if_fail(session != NULL, false);
Moritz Lipp's avatar
Moritz Lipp committed
477 478 479
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

Moritz Lipp's avatar
Moritz Lipp committed
480
  if (zathura->file_monitor.file_path == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
481 482 483 484
    return false;
  }

  /* close current document */
Moritz Lipp's avatar
Moritz Lipp committed
485
  document_close(zathura, true);
Moritz Lipp's avatar
Moritz Lipp committed
486 487

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

Moritz Lipp's avatar
Moritz Lipp committed
490
  return false;
491 492
}

Moritz Lipp's avatar
Moritz Lipp committed
493
bool
494
sc_rotate(girara_session_t* session, girara_argument_t* argument,
Moritz Lipp's avatar
Moritz Lipp committed
495
    girara_event_t* UNUSED(event), unsigned int t)
496
{
Moritz Lipp's avatar
Moritz Lipp committed
497 498 499 500
  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
501

502
  unsigned int page_number = zathura_document_get_current_page_number(zathura->document);
503

504 505 506 507 508
  int angle = 90;
  if (argument != NULL && argument->n == ROTATE_CCW) {
    angle = 270;
  }

Moritz Lipp's avatar
Moritz Lipp committed
509
  /* update rotate value */
Moritz Lipp's avatar
Moritz Lipp committed
510
  t = (t == 0) ? 1 : t;
511
  unsigned int rotation = zathura_document_get_rotation(zathura->document);
Moritz Lipp's avatar
Moritz Lipp committed
512
  zathura_document_set_rotation(zathura->document, (rotation + angle * t) % 360);
Moritz Lipp's avatar
Moritz Lipp committed
513

514 515 516 517
  /* 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
518
  /* render all pages again */
519
  render_all(zathura);
Moritz Lipp's avatar
Moritz Lipp committed
520

521 522
  page_set_delayed(zathura, page_number);

Moritz Lipp's avatar
Moritz Lipp committed
523
  return false;
524 525
}

Moritz Lipp's avatar
Moritz Lipp committed
526
bool
Moritz Lipp's avatar
Moritz Lipp committed
527 528
sc_scroll(girara_session_t* session, girara_argument_t* argument,
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
529
{
Moritz Lipp's avatar
Moritz Lipp committed
530 531 532 533
  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);
534 535 536
  if (zathura->document == NULL) {
    return false;
  }
Moritz Lipp's avatar
Moritz Lipp committed
537

Moritz Lipp's avatar
Moritz Lipp committed
538
  GtkAdjustment* adjustment = NULL;
539
  if ( (argument->n == LEFT) || (argument->n == FULL_LEFT) || (argument->n == HALF_LEFT) ||
Moritz Lipp's avatar
Moritz Lipp committed
540
  (argument->n == RIGHT) || (argument->n == FULL_RIGHT) || (argument->n == HALF_RIGHT)) {
Moritz Lipp's avatar
Moritz Lipp committed
541
    adjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(session->gtk.view));
Moritz Lipp's avatar
Moritz Lipp committed
542
  } else {
Moritz Lipp's avatar
Moritz Lipp committed
543
    adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(session->gtk.view));
Moritz Lipp's avatar
Moritz Lipp committed
544
  }
Moritz Lipp's avatar
Moritz Lipp committed
545

546 547 548 549

  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;
550
  zathura->global.update_page_number = true;
551

552 553
  float scroll_step = 40;
  girara_setting_get(session, "scroll-step", &scroll_step);
Julian Orth's avatar
Julian Orth committed
554 555 556 557 558
  float scroll_hstep = -1;
  girara_setting_get(session, "scroll-hstep", &scroll_hstep);
  if (scroll_hstep < 0) {
    scroll_hstep = scroll_step;
  }
559 560 561
  int padding = 1;
  girara_setting_get(session, "page-padding", &padding);

562
  gdouble new_value;
Moritz Lipp's avatar
Moritz Lipp committed
563

Moritz Lipp's avatar
Moritz Lipp committed
564
  switch(argument->n) {
Moritz Lipp's avatar
Moritz Lipp committed
565
    case FULL_UP:
566
    case FULL_LEFT:
567
      new_value = value - view_size - padding;
Moritz Lipp's avatar
Moritz Lipp committed
568 569
      break;
    case FULL_DOWN:
570
    case FULL_RIGHT:
571
      new_value = value + view_size + padding;
Moritz Lipp's avatar
Moritz Lipp committed
572 573
      break;
    case HALF_UP:
574
    case HALF_LEFT:
575
      new_value = value - ((view_size + padding) / 2);
Moritz Lipp's avatar
Moritz Lipp committed
576 577
      break;
    case HALF_DOWN:
578
    case HALF_RIGHT:
579
      new_value = value + ((view_size + padding) / 2);
Moritz Lipp's avatar
Moritz Lipp committed
580 581
      break;
    case LEFT:
Julian Orth's avatar
Julian Orth committed
582 583
      new_value = value - scroll_hstep;
      break;
Moritz Lipp's avatar
Moritz Lipp committed
584
    case UP:
585
      new_value = value - scroll_step;
Moritz Lipp's avatar
Moritz Lipp committed
586 587
      break;
    case RIGHT:
Julian Orth's avatar
Julian Orth committed
588 589
      new_value = value + scroll_hstep;
      break;
Moritz Lipp's avatar
Moritz Lipp committed
590
    case DOWN:
591
      new_value = value + scroll_step;
Moritz Lipp's avatar
Moritz Lipp committed
592 593 594 595 596 597 598 599
      break;
    case TOP:
      new_value = 0;
      break;
    case BOTTOM:
      new_value = max;
      break;
    default:
Moritz Lipp's avatar
Moritz Lipp committed
600
      new_value = value;
Moritz Lipp's avatar
Moritz Lipp committed
601 602
  }

603
  set_adjustment(adjustment, new_value);
Moritz Lipp's avatar
Moritz Lipp committed
604

Moritz Lipp's avatar
Moritz Lipp committed
605
  return false;
606 607
}

Moritz Lipp's avatar
Moritz Lipp committed
608
bool
Moritz Lipp's avatar
Moritz Lipp committed
609 610
sc_search(girara_session_t* session, girara_argument_t* argument,
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
611
{
Moritz Lipp's avatar
Moritz Lipp committed
612 613 614 615 616
  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
617

618 619
  const int num_pages = zathura_document_get_number_of_pages(zathura->document);
  const int cur_page  = zathura_document_get_current_page_number(zathura->document);
620 621
  int diff = argument->n == FORWARD ? 1 : -1;

Sebastian Ramacher's avatar
Sebastian Ramacher committed
622 623 624
  zathura_page_t* target_page = NULL;
  int target_idx = 0;

625 626
  for (int page_id = 0; page_id < num_pages; ++page_id) {
    int tmp = cur_page + diff * page_id;
627
    zathura_page_t* page = zathura_document_get_page(zathura->document, (tmp + num_pages) % num_pages);
628 629 630 631
    if (page == NULL) {
      continue;
    }

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

634
    int num_search_results = 0, current = -1;
Moritz Lipp's avatar
Moritz Lipp committed
635
    g_object_get(page_widget, "search-current", &current,
636 637 638 639 640 641 642
        "search-length", &num_search_results, NULL);
    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
643 644
      target_page = page;
      target_idx = current + 1;
645
    } else if (diff == -1 && current > 0) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
646 647
      target_page = page;
      target_idx = current - 1;
648 649
    } else {
      /* the next result is on a different page */
Moritz Lipp's avatar
Moritz Lipp committed
650
      g_object_set(page_widget, "search-current", -1, NULL);
651 652 653

      for (int npage_id = 1; page_id < num_pages; ++npage_id) {
        int ntmp = cur_page + diff * (page_id + npage_id);
654 655
        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));
656
        GtkWidget* npage_page_widget = zathura_page_get_widget(zathura, npage);
Moritz Lipp's avatar
Moritz Lipp committed
657
        g_object_get(npage_page_widget, "search-length", &num_search_results, NULL);
658
        if (num_search_results != 0) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
659 660
          target_page = npage;
          target_idx = diff == 1 ? 0 : num_search_results - 1;
661 662 663 664
          break;
        }
      }
    }
665

666 667
    break;
  }
Moritz Lipp's avatar
Moritz Lipp committed
668

Sebastian Ramacher's avatar
Sebastian Ramacher committed
669 670
  if (target_page != NULL) {
    girara_list_t* results = NULL;
671
    GtkWidget* page_widget = zathura_page_get_widget(zathura, target_page);
Moritz Lipp's avatar
Moritz Lipp committed
672 673
    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
674 675 676 677

    zathura_rectangle_t* rect = girara_list_nth(results, target_idx);
    zathura_rectangle_t rectangle = recalc_rectangle(target_page, *rect);
    page_offset_t offset;
678
    page_calculate_offset(zathura, target_page, &offset);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
679 680 681

    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;
682
    set_adjustment(view_vadjustment, y);
Julian Orth's avatar
Julian Orth committed
683 684 685 686 687 688 689 690

    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
691 692
  }

Moritz Lipp's avatar
Moritz Lipp committed
693
  return false;
694 695
}

Moritz Lipp's avatar
Moritz Lipp committed
696
bool
697
sc_navigate_index(girara_session_t* session, girara_argument_t* argument,
Moritz Lipp's avatar
Moritz Lipp committed
698
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
699
{
Moritz Lipp's avatar
Moritz Lipp committed
700 701 702 703 704 705
  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
706 707 708 709 710 711 712 713
  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
714
  if (path == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
715 716 717 718 719 720 721 722 723 724 725
    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:
726
      if (gtk_tree_path_prev(path) == FALSE) {
727 728 729
        /* 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
730 731 732 733 734 735 736 737 738 739 740 741
      } 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,
            gtk_tree_model_iter_n_children(model, &iter)-1);
          gtk_tree_path_free(path);
          path = gtk_tree_model_get_path(model, &child_iter);
        }
      }
      break;
    case COLLAPSE:
742
      if (gtk_tree_view_collapse_row(tree_view, path) == FALSE
Moritz Lipp's avatar
Moritz Lipp committed
743 744 745 746 747 748
        && gtk_tree_path_get_depth(path) > 1) {
        gtk_tree_path_up(path);
        gtk_tree_view_collapse_row(tree_view, path);
      }
      break;
    case DOWN:
749
      if (gtk_tree_view_row_expanded(tree_view, path) == TRUE) {
Moritz Lipp's avatar
Moritz Lipp committed
750 751 752 753 754 755 756 757 758 759 760 761 762
        gtk_tree_path_down(path);
      } else {
        do {
          gtk_tree_model_get_iter(model, &iter, path);
          if (gtk_tree_model_iter_next(model, &iter)) {
            path = gtk_tree_model_get_path(model, &iter);
            break;
          }
        } while((is_valid_path = (gtk_tree_path_get_depth(path) > 1))
          && gtk_tree_path_up(path));
      }
      break;
    case EXPAND:
763
      if (gtk_tree_view_expand_row(tree_view, path, FALSE)) {
Moritz Lipp's avatar
Moritz Lipp committed
764 765 766
        gtk_tree_path_down(path);
      }
      break;
767 768 769 770 771 772 773 774 775
    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
776 777 778 779 780 781 782 783 784 785 786
    case SELECT:
      cb_index_row_activated(tree_view, path, NULL, zathura);
      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
787
  return false;
788 789
}

Moritz Lipp's avatar
Moritz Lipp committed
790
bool
791
sc_toggle_index(girara_session_t* session, girara_argument_t* UNUSED(argument),
Moritz Lipp's avatar
Moritz Lipp committed
792
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
793
{
Moritz Lipp's avatar
Moritz Lipp committed
794 795 796
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
797 798 799
  if (zathura->document == NULL) {
    return false;
  }
800 801 802 803 804

  girara_tree_node_t* document_index = NULL;
  GtkWidget* treeview                = NULL;
  GtkTreeModel* model                = NULL;
  GtkCellRenderer* renderer          = NULL;
805
  GtkCellRenderer* renderer2         = NULL;
806

807
  if (zathura->ui.index == NULL) {
808
    /* create new index widget */
809
    zathura->ui.index = gtk_scrolled_window_new(NULL, NULL);
810

811
    if (zathura->ui.index == NULL) {
812 813 814
      goto error_ret;
    }

815
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(zathura->ui.index),
816 817 818
      GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

    /* create index */
819
    document_index = zathura_document_index_generate(zathura->document, NULL);
820
    if (document_index == NULL) {
821
      girara_notify(session, GIRARA_WARNING, _("This document does not contain any index"));
822 823 824
      goto error_free;
    }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
825
    model = GTK_TREE_MODEL(gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    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;
    }

842 843 844 845 846
    renderer2 = gtk_cell_renderer_text_new();
    if (renderer2 == NULL) {
      goto error_free;
    }

847 848 849 850 851
    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);
852
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW (treeview), 1, "Target", renderer2, "text", 1, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
853

854 855 856
    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);
857
    gtk_tree_view_column_set_alignment(gtk_tree_view_get_column(GTK_TREE_VIEW(treeview), 1), 1.0f);
858
    gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview), gtk_tree_path_new_first(), NULL, FALSE);
859
    g_signal_connect(G_OBJECT(treeview), "row-activated", G_CALLBACK(cb_index_row_activated), zathura);
860

861
    gtk_container_add(GTK_CONTAINER(zathura->ui.index), treeview);
862 863 864
    gtk_widget_show(treeview);
  }

Moritz Lipp's avatar
Moritz Lipp committed
865 866 867
  static double vvalue = 0;
  static double hvalue = 0;

868
  if (gtk_widget_get_visible(GTK_WIDGET(zathura->ui.index))) {
869
    girara_set_view(session, zathura->ui.page_widget_alignment);
870
    gtk_widget_hide(GTK_WIDGET(zathura->ui.index));
871
    girara_mode_set(zathura->ui.session, zathura->modes.normal);
Moritz Lipp's avatar
Moritz Lipp committed
872 873

    /* reset adjustment */
874
    position_set_delayed(zathura, hvalue, vvalue);
875
  } else {
Moritz Lipp's avatar
Moritz Lipp committed
876
    /* save adjustment */
877 878 879
    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
880 881 882
    vvalue = gtk_adjustment_get_value(vadjustment);
    hvalue = gtk_adjustment_get_value(hadjustment);

883 884
    girara_set_view(session, zathura->ui.index);
    gtk_widget_show(GTK_WIDGET(zathura->ui.index));
885
    girara_mode_set(zathura->ui.session, zathura->modes.index);
886 887 888 889 890 891
  }

  return false;

error_free:

892 893 894
  if (zathura->ui.index != NULL) {
    g_object_ref_sink(zathura->ui.index);
    zathura->ui.index = NULL;
895 896 897 898 899
  }

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

901 902
error_ret:

Moritz Lipp's avatar
Moritz Lipp committed
903
  return false;
904 905
}

Moritz Lipp's avatar
Moritz Lipp committed
906
bool
907
sc_toggle_fullscreen(girara_session_t* session, girara_argument_t*
Moritz Lipp's avatar
Moritz Lipp committed
908
    UNUSED(argument), girara_event_t* UNUSED(event), unsigned int UNUSED(t))
909
{
Moritz Lipp's avatar
Moritz Lipp committed
910
  g_return_val_if_fail(session != NULL, false);
911 912
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
913

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

919
  static bool fullscreen = false;
Moritz Lipp's avatar
Moritz Lipp committed
920
  static int pages_per_row = 1;
921
  static int first_page_column = 1;
922
  static double zoom = 1.0;
Moritz Lipp's avatar
Moritz Lipp committed
923 924 925 926 927

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

928 929 930
    /* reset first page column */
    girara_setting_set(session, "first-page-column", &first_page_column);

Moritz Lipp's avatar
Moritz Lipp committed
931 932 933 934
    /* show status bar */
    gtk_widget_show(GTK_WIDGET(session->gtk.statusbar));

    /* set full screen */
935
    gtk_window_unfullscreen(GTK_WINDOW(session->gtk.window));
936 937

    /* reset scale */
938
    zathura_document_set_scale(zathura->document, zoom);
939
    render_all(zathura);
940
    page_set_delayed(zathura, zathura_document_get_current_page_number(zathura->document));
941
  } else {
Moritz Lipp's avatar
Moritz Lipp committed
942
    /* backup pages per row */
943
    girara_setting_get(session, "pages-per-row", &pages_per_row);
Moritz Lipp's avatar
Moritz Lipp committed
944

945 946 947
    /* backup first page column */
    girara_setting_get(session, "first-page-column", &first_page_column);

Moritz Lipp's avatar
Moritz Lipp committed
948 949 950 951
    /* set single view */
    int int_value = 1;
    girara_setting_set(session, "pages-per-row", &int_value);

952
    /* back up zoom */
953
    zoom = zathura_document_get_scale(zathura->document);
954

Moritz Lipp's avatar
Moritz Lipp committed
955
    /* adjust window */
956
    girara_argument_t argument = { ZATHURA_ADJUST_BESTFIT, NULL };
Moritz Lipp's avatar
Moritz Lipp committed
957
    sc_adjust_window(session, &argument, NULL, 0);
Moritz Lipp's avatar
Moritz Lipp committed
958 959 960 961 962 963

    /* hide status and inputbar */
    gtk_widget_hide(GTK_WIDGET(session->gtk.inputbar));
    gtk_widget_hide(GTK_WIDGET(session->gtk.statusbar));

    /* set full screen */
964
    gtk_window_fullscreen(GTK_WINDOW(session->gtk.window));
965
    page_set_delayed(zathura, zathura_document_get_current_page_number(zathura->document));
966 967 968 969
  }

  fullscreen = fullscreen ? false : true;

Moritz Lipp's avatar
Moritz Lipp committed
970
  return false;
971 972
}

Moritz Lipp's avatar
Moritz Lipp committed
973
bool
Moritz Lipp's avatar
Moritz Lipp committed
974 975
sc_quit(girara_session_t* session, girara_argument_t* UNUSED(argument),
    girara_event_t* UNUSED(event), unsigned int UNUSED(t))
976
{
Moritz Lipp's avatar
Moritz Lipp committed
977 978
  g_return_val_if_fail(session != NULL, false);

Moritz Lipp's avatar
Moritz Lipp committed
979
  girara_argument_t arg = { GIRARA_HIDE, NULL };
Moritz Lipp's avatar
Moritz Lipp committed
980
  girara_isc_completion(session, &arg, NULL, 0);
Moritz Lipp's avatar
Moritz Lipp committed
981 982

  cb_destroy(NULL, NULL);
983

Moritz Lipp's avatar
Moritz Lipp committed
984
  return false;
985 986
}

Moritz Lipp's avatar
Moritz Lipp committed
987
bool
Moritz Lipp's avatar
Moritz Lipp committed
988
sc_zoom(girara_session_t* session, girara_argument_t* argument, girara_event_t*
Moritz Lipp's avatar
Moritz Lipp committed
989
    UNUSED(event), unsigned int t)
990
{
Moritz Lipp's avatar
Moritz Lipp committed
991 992 993 994 995
  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);
996

997
  zathura_document_set_adjust_mode(zathura->document, ZATHURA_ADJUST_NONE);
998

999
  /* retreive zoom step value */
1000 1001
  int value = 1;
  girara_setting_get(zathura->ui.session, "zoom-step", &value);
1002

1003 1004
  int nt = (t == 0) ? 1 : t;
  float zoom_step = value / 100.0f * nt;
1005
  float old_zoom = zathura_document_get_scale(zathura->document);
1006

Moritz Lipp's avatar
Moritz Lipp committed
1007
  /* specify new zoom value */
1008
  if (argument->n == ZOOM_IN) {
1009
    zathura_document_set_scale(zathura->document, old_zoom + zoom_step);
1010
  } else if (argument->n == ZOOM_OUT) {
1011
    zathura_document_set_scale(zathura->document, old_zoom - zoom_step);
Moritz Lipp's avatar
Moritz Lipp committed
1012
  } else if (argument->n == ZOOM_SPECIFIC) {
1013 1014 1015 1016 1017
    if (t == 0) {
      zathura_document_set_scale(zathura->document, 1.0f);
    } else {
      zathura_document_set_scale(zathura->document, t / 100.0f);
    }
1018
  } else {
1019
    zathura_document_set_scale(zathura->document, 1.0f);
1020 1021
  }

Moritz Lipp's avatar
Moritz Lipp committed
1022
  /* zoom limitations */
Moritz Lipp's avatar
Moritz Lipp committed
1023 1024 1025 1026 1027 1028 1029 1030
  int zoom_min_int = 10;
  int zoom_max_int = 1000;
  girara_setting_get(session, "zoom-min", &zoom_min_int);
  girara_setting_get(session, "zoom-max", &zoom_max_int);

  float zoom_min = zoom_min_int * 0.01f;
  float zoom_max = zoom_max_int * 0.01f;

1031 1032
  float scale = zathura_document_get_scale(zathura->document);
  if (scale < zoom_min) {
1033
    zathura_document_set_scale(zathura->document, zoom_min);
Moritz Lipp's avatar