callbacks.c 8.46 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), gpointer UNUSED(data))
Moritz Lipp's avatar
Moritz Lipp committed
22
{
23
  gtk_main_quit();
Moritz Lipp's avatar
Moritz Lipp committed
24 25
  return TRUE;
}
Moritz Lipp's avatar
Moritz Lipp committed
26 27

void
28
cb_buffer_changed(girara_session_t* session)
Moritz Lipp's avatar
Moritz Lipp committed
29 30
{
  g_return_if_fail(session != NULL);
Moritz Lipp's avatar
Moritz Lipp committed
31 32 33
  g_return_if_fail(session->global.data != NULL);

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

  char* buffer = girara_buffer_get(session);
Moritz Lipp's avatar
Moritz Lipp committed
36
  if (buffer != NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
37
    girara_statusbar_item_set_text(session, zathura->ui.statusbar.buffer, buffer);
Moritz Lipp's avatar
Moritz Lipp committed
38
    free(buffer);
Moritz Lipp's avatar
Moritz Lipp committed
39
  } else {
Moritz Lipp's avatar
Moritz Lipp committed
40
    girara_statusbar_item_set_text(session, zathura->ui.statusbar.buffer, "");
41
  }
Moritz Lipp's avatar
Moritz Lipp committed
42
}
43 44

void
45
cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpointer data)
46
{
Moritz Lipp's avatar
Moritz Lipp committed
47
  zathura_t* zathura = data;
Moritz Lipp's avatar
Moritz Lipp committed
48 49
  if (zathura == NULL || zathura->document == NULL || zathura->document->pages == NULL
      || zathura->ui.page_widget == NULL) {
50 51 52
    return;
  }

53 54 55 56
  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;
57
  /* get current adjustment values */
Moritz Lipp's avatar
Moritz Lipp committed
58
  view_rect.y      = gtk_adjustment_get_value(view_vadjustment);
59
  view_rect.height = gtk_adjustment_get_page_size(view_vadjustment);
Moritz Lipp's avatar
Moritz Lipp committed
60 61
  view_rect.x      = gtk_adjustment_get_value(view_hadjustment);
  view_rect.width  = gtk_adjustment_get_page_size(view_hadjustment);
62 63 64

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

66 67 68
  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
69
  center.height = center.width = (2 * page_padding) + 1;
70 71

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

76 77
    page_offset_t offset;
    page_calculate_offset(page, &offset);
78

79 80 81
    GdkRectangle page_rect;
    page_rect.x = offset.x;
    page_rect.y = offset.y;
Moritz Lipp's avatar
Moritz Lipp committed
82
    page_rect.width  = page->width  * zathura->document->scale;
83
    page_rect.height = page->height * zathura->document->scale;
84

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

  statusbar_page_number_update(zathura);
98
}
99 100

void
101
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))
102
{
103
  g_return_if_fail(value != NULL);
104 105 106
  g_return_if_fail(session != NULL);
  g_return_if_fail(session->global.data != NULL);
  zathura_t* zathura = session->global.data;
107 108

  int pages_per_row = *(int*) value;
109

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

114
  page_widget_set_mode(zathura, pages_per_row);
115 116 117 118 119

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

void
Moritz Lipp's avatar
Moritz Lipp committed
123
cb_index_row_activated(GtkTreeView* tree_view, GtkTreePath* path,
Sebastian Ramacher's avatar
Sebastian Ramacher committed
124
    GtkTreeViewColumn* UNUSED(column), void* data)
125
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
126
  zathura_t* zathura = data;
127 128 129 130 131 132 133 134 135
  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
136
  if(gtk_tree_model_get_iter(model, &iter, path)) {
137
    zathura_index_element_t* index_element;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
138
    gtk_tree_model_get(model, &iter, 2, &index_element, -1);
139 140 141 142 143 144

    if (index_element == NULL) {
      return;
    }

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

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

Moritz Lipp's avatar
Moritz Lipp committed
157 158 159 160 161 162 163
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
164
  bool eval = true;
Moritz Lipp's avatar
Moritz Lipp committed
165 166

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

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

  /* 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;
    }

189 190
    g_object_set(page->drawing_area, "draw-links", FALSE, NULL);

Moritz Lipp's avatar
Moritz Lipp committed
191 192 193 194 195 196 197 198 199 200 201 202 203
    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
204 205 206 207
      }
    }
  }

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

  g_free(input);

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

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
224 225 226
  switch (event) {
    case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
    case G_FILE_MONITOR_EVENT_CREATED:
227
      gdk_threads_enter();
Moritz Lipp's avatar
Moritz Lipp committed
228
      sc_reload(session, NULL, NULL, 0);
229
      gdk_threads_leave();
Moritz Lipp's avatar
Moritz Lipp committed
230 231 232
      break;
    default:
      return;
Moritz Lipp's avatar
Moritz Lipp committed
233 234
  }
}
235

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

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

252 253 254
  return FALSE;
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
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);
    }

279
    gdk_threads_add_idle(password_dialog, dialog);
280 281 282 283
    return false;
  }

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

  g_free(input);

  return true;

error_free:

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

error_ret:

  return false;
}
304 305

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

312 313
  static int height = -1;
  static int width = -1;
314

315 316 317 318
  /* 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
319 320

    width  = allocation->width;
321 322 323 324
    height = allocation->height;
  }

  return false;
325
}