callbacks.c 9.02 KB
Newer Older
Moritz Lipp's avatar
Moritz Lipp committed
1
/* See LICENSE file for license and copyright information */
Moritz Lipp's avatar
Moritz Lipp committed
2

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

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

gboolean
21
cb_destroy(GtkWidget* UNUSED(widget), zathura_t* zathura)
Moritz Lipp's avatar
Moritz Lipp committed
22
{
23 24 25 26 27
  if (zathura != NULL && zathura->document != NULL) {
    zathura_document_free(zathura->document);
    zathura->document = NULL;
  }

28
  gtk_main_quit();
Moritz Lipp's avatar
Moritz Lipp committed
29 30
  return TRUE;
}
Moritz Lipp's avatar
Moritz Lipp committed
31 32

void
33
cb_buffer_changed(girara_session_t* session)
Moritz Lipp's avatar
Moritz Lipp committed
34 35
{
  g_return_if_fail(session != NULL);
Moritz Lipp's avatar
Moritz Lipp committed
36 37 38
  g_return_if_fail(session->global.data != NULL);

  zathura_t* zathura = session->global.data;
Moritz Lipp's avatar
Moritz Lipp committed
39 40

  char* buffer = girara_buffer_get(session);
Moritz Lipp's avatar
Moritz Lipp committed
41
  if (buffer != NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
42
    girara_statusbar_item_set_text(session, zathura->ui.statusbar.buffer, buffer);
Moritz Lipp's avatar
Moritz Lipp committed
43
    free(buffer);
Moritz Lipp's avatar
Moritz Lipp committed
44
  } else {
Moritz Lipp's avatar
Moritz Lipp committed
45
    girara_statusbar_item_set_text(session, zathura->ui.statusbar.buffer, "");
46
  }
Moritz Lipp's avatar
Moritz Lipp committed
47
}
48 49

void
50
cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpointer data)
51
{
Moritz Lipp's avatar
Moritz Lipp committed
52
  zathura_t* zathura = data;
Moritz Lipp's avatar
Moritz Lipp committed
53 54
  if (zathura == NULL || zathura->document == NULL || zathura->document->pages == NULL
      || zathura->ui.page_widget == NULL) {
55 56 57
    return;
  }

58 59 60 61
  GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
  GtkAdjustment* view_hadjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));

  GdkRectangle view_rect;
62
  /* get current adjustment values */
63
  view_rect.y      = 0;
64
  view_rect.height = gtk_adjustment_get_page_size(view_vadjustment);
65
  view_rect.x      = 0;
Moritz Lipp's avatar
Moritz Lipp committed
66
  view_rect.width  = gtk_adjustment_get_page_size(view_hadjustment);
67 68 69

  int page_padding = 1;
  girara_setting_get(zathura->ui.session, "page-padding", &page_padding);
70

71 72 73
  GdkRectangle center;
  center.x = view_rect.x + (view_rect.width + 1) / 2;
  center.y = view_rect.y + (view_rect.height + 1) / 2;
Moritz Lipp's avatar
Moritz Lipp committed
74
  center.height = center.width = (2 * page_padding) + 1;
75 76

  bool updated = false;
77
  /* find page that fits */
Moritz Lipp's avatar
Moritz Lipp committed
78
  for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++) {
Moritz Lipp's avatar
Moritz Lipp committed
79
    zathura_page_t* page = zathura->document->pages[page_id];
Moritz Lipp's avatar
Moritz Lipp committed
80

81
    GdkRectangle page_rect;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
82 83
    gtk_widget_translate_coordinates(page->drawing_area,
        zathura->ui.session->gtk.view, 0, 0, &page_rect.x, &page_rect.y);
Moritz Lipp's avatar
Moritz Lipp committed
84
    page_rect.width  = page->width  * zathura->document->scale;
85
    page_rect.height = page->height * zathura->document->scale;
86

87
    if (gdk_rectangle_intersect(&view_rect, &page_rect, NULL) == TRUE) {
88
      page->visible = true;
89 90
      if (zathura->global.update_page_number == true && updated == false
          && gdk_rectangle_intersect(&center, &page_rect, NULL) == TRUE) {
91 92 93
        zathura->document->current_page_number = page_id;
        updated = true;
      }
94 95
    } else {
      page->visible = false;
96 97
    }
  }
98 99

  statusbar_page_number_update(zathura);
100
}
101 102

void
103
cb_pages_per_row_value_changed(girara_session_t* session, const char* UNUSED(name), girara_setting_type_t UNUSED(type), void* value, void* UNUSED(data))
104
{
105
  g_return_if_fail(value != NULL);
106 107 108
  g_return_if_fail(session != NULL);
  g_return_if_fail(session->global.data != NULL);
  zathura_t* zathura = session->global.data;
109 110

  int pages_per_row = *(int*) value;
111

Moritz Lipp's avatar
Moritz Lipp committed
112
  if (pages_per_row < 1) {
113
    pages_per_row = 1;
Moritz Lipp's avatar
Moritz Lipp committed
114
  }
115

116
  page_widget_set_mode(zathura, pages_per_row);
117 118 119 120 121

  if (zathura->document != NULL) {
    unsigned int current_page = zathura->document->current_page_number;
    page_set_delayed(zathura, current_page);
  }
122
}
123 124

void
Moritz Lipp's avatar
Moritz Lipp committed
125
cb_index_row_activated(GtkTreeView* tree_view, GtkTreePath* path,
Sebastian Ramacher's avatar
Sebastian Ramacher committed
126
    GtkTreeViewColumn* UNUSED(column), void* data)
127
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
128
  zathura_t* zathura = data;
129 130 131 132 133 134 135 136 137
  if (tree_view == NULL || zathura == NULL || zathura->ui.session == NULL) {
    return;
  }

  GtkTreeModel  *model;
  GtkTreeIter   iter;

  g_object_get(tree_view, "model", &model, NULL);

Moritz Lipp's avatar
Moritz Lipp committed
138
  if(gtk_tree_model_get_iter(model, &iter, path)) {
139
    zathura_index_element_t* index_element;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
140
    gtk_tree_model_get(model, &iter, 2, &index_element, -1);
141 142 143 144 145 146

    if (index_element == NULL) {
      return;
    }

    if (index_element->type == ZATHURA_LINK_TO_PAGE) {
Moritz Lipp's avatar
Moritz Lipp committed
147
      sc_toggle_index(zathura->ui.session, NULL, NULL, 0);
148
      page_set_delayed(zathura, index_element->target.page_number);
149
    } else if (index_element->type == ZATHURA_LINK_EXTERNAL) {
Moritz Lipp's avatar
Moritz Lipp committed
150
      if (girara_xdg_open(index_element->target.uri) == false) {
151
        girara_notify(zathura->ui.session, GIRARA_ERROR, _("Failed to run xdg-open."));
152
      }
153 154 155 156 157
    }
  }

  g_object_unref(model);
}
Moritz Lipp's avatar
Moritz Lipp committed
158

Moritz Lipp's avatar
Moritz Lipp committed
159 160 161 162 163 164 165
bool
cb_sc_follow(GtkEntry* entry, girara_session_t* session)
{
  g_return_val_if_fail(session != NULL, FALSE);
  g_return_val_if_fail(session->global.data != NULL, FALSE);

  zathura_t* zathura = session->global.data;
Moritz Lipp's avatar
Moritz Lipp committed
166
  bool eval = true;
Moritz Lipp's avatar
Moritz Lipp committed
167 168

  char* input = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
Moritz Lipp's avatar
Moritz Lipp committed
169 170
  if (input == NULL || strlen(input) == 0) {
    eval = false;
Moritz Lipp's avatar
Moritz Lipp committed
171 172
  }

Moritz Lipp's avatar
Moritz Lipp committed
173 174 175 176
  int index = 0;
  if (eval == true) {
    index = atoi(input);
    if (index == 0 && g_strcmp0(input, "0") != 0) {
177
      girara_notify(session, GIRARA_WARNING, _("Invalid input '%s' given."), input);
Moritz Lipp's avatar
Moritz Lipp committed
178 179 180
      eval = false;
    }
    index = index - 1;
Moritz Lipp's avatar
Moritz Lipp committed
181 182 183 184 185 186 187 188 189 190
  }

  /* set pages to draw links */
  bool invalid_index = true;
  for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++) {
    zathura_page_t* page = zathura->document->pages[page_id];
    if (page == NULL || page->visible == false) {
      continue;
    }

191 192
    g_object_set(page->drawing_area, "draw-links", FALSE, NULL);

Moritz Lipp's avatar
Moritz Lipp committed
193 194 195 196 197 198 199 200 201 202 203 204 205
    if (eval == true) {
      zathura_link_t* link = zathura_page_widget_link_get(ZATHURA_PAGE(page->drawing_area), index);
      if (link != NULL) {
        switch (link->type) {
          case ZATHURA_LINK_TO_PAGE:
            page_set_delayed(zathura, link->target.page_number);
            break;
          case ZATHURA_LINK_EXTERNAL:
            girara_xdg_open(link->target.value);
            break;
        }

        invalid_index = false;
Moritz Lipp's avatar
Moritz Lipp committed
206 207 208 209
      }
    }
  }

Moritz Lipp's avatar
Moritz Lipp committed
210
  if (eval == true && invalid_index == true) {
211
    girara_notify(session, GIRARA_WARNING, _("Invalid index '%s' given."), input);
Moritz Lipp's avatar
Moritz Lipp committed
212 213 214 215
  }

  g_free(input);

Moritz Lipp's avatar
Moritz Lipp committed
216
  return (eval == TRUE) ? TRUE : FALSE;
Moritz Lipp's avatar
Moritz Lipp committed
217
}
Moritz Lipp's avatar
Moritz Lipp committed
218 219 220 221 222 223 224 225

void
cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), GFileMonitorEvent event, girara_session_t* session)
{
  g_return_if_fail(monitor  != NULL);
  g_return_if_fail(file     != NULL);
  g_return_if_fail(session  != NULL);

Moritz Lipp's avatar
Moritz Lipp committed
226 227 228
  switch (event) {
    case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
    case G_FILE_MONITOR_EVENT_CREATED:
229
      gdk_threads_enter();
Moritz Lipp's avatar
Moritz Lipp committed
230
      sc_reload(session, NULL, NULL, 0);
231
      gdk_threads_leave();
Moritz Lipp's avatar
Moritz Lipp committed
232 233 234
      break;
    default:
      return;
Moritz Lipp's avatar
Moritz Lipp committed
235 236
  }
}
237

238 239 240 241
static gboolean
password_dialog(gpointer data)
{
  zathura_password_dialog_info_t* dialog = data;
Moritz Lipp's avatar
Moritz Lipp committed
242

243
  if (dialog != NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
244 245 246 247 248 249 250 251
    girara_dialog(
        dialog->zathura->ui.session,
        "Incorrect password. Enter password:",
        true,
        NULL,
        (girara_callback_inputbar_activate_t) cb_password_dialog,
        dialog
      );
252
  }
Moritz Lipp's avatar
Moritz Lipp committed
253

254 255 256
  return FALSE;
}

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
bool
cb_password_dialog(GtkEntry* entry, zathura_password_dialog_info_t* dialog)
{
  if (entry == NULL || dialog == NULL) {
    goto error_ret;
  }

  if (dialog->path == NULL) {
    free(dialog);
    goto error_ret;
  }

  if (dialog->zathura == NULL) {
    goto error_free;
  }

  char* input = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);

  /* no or empty password: ask again */
  if (input == NULL || strlen(input) == 0) {
    if (input != NULL) {
      g_free(input);
    }

281
    gdk_threads_add_idle(password_dialog, dialog);
282 283 284 285
    return false;
  }

  /* try to open document again */
286
  if (document_open(dialog->zathura, dialog->path, input) == false) {
287
    gdk_threads_add_idle(password_dialog, dialog);
288 289 290 291
  } else {
    g_free(dialog->path);
    free(dialog);
  }
292 293 294 295 296 297 298 299 300 301 302 303 304 305

  g_free(input);

  return true;

error_free:

    g_free(dialog->path);
    free(dialog);

error_ret:

  return false;
}
306 307

bool
308
cb_view_resized(GtkWidget* UNUSED(widget), GtkAllocation* allocation, zathura_t* zathura)
309 310 311 312 313
{
  if (zathura == NULL || zathura->document == NULL) {
    return false;
  }

314 315
  static int height = -1;
  static int width = -1;
316

317 318 319 320
  /* adjust only if the allocation changed */
  if (width != allocation->width || height != allocation->height) {
    girara_argument_t argument = { zathura->document->adjust_mode, NULL };
    sc_adjust_window(zathura->ui.session, &argument, NULL, 0);
Moritz Lipp's avatar
Moritz Lipp committed
321 322

    width  = allocation->width;
323 324 325 326
    height = allocation->height;
  }

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

void
cb_setting_recolor_change(girara_session_t* session, const char* name,
    girara_setting_type_t UNUSED(type), void* value, void* UNUSED(data))
{
  g_return_if_fail(value != NULL);
  g_return_if_fail(session != NULL);
  g_return_if_fail(session->global.data != NULL);
  g_return_if_fail(name != NULL);
  zathura_t* zathura = session->global.data;

  bool bool_value = *((bool*) value);

  if (zathura->global.recolor != bool_value) {
    zathura->global.recolor = bool_value;
    render_all(zathura);
  }
}