callbacks.c 5.73 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>
Moritz Lipp's avatar
Moritz Lipp committed
10

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

gboolean
20
cb_destroy(GtkWidget* UNUSED(widget), gpointer UNUSED(data))
Moritz Lipp's avatar
Moritz Lipp committed
21
{
22
  gtk_main_quit();
Moritz Lipp's avatar
Moritz Lipp committed
23
24
  return TRUE;
}
Moritz Lipp's avatar
Moritz Lipp committed
25
26
27
28
29

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

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

  char* buffer = girara_buffer_get(session);

Moritz Lipp's avatar
Moritz Lipp committed
36
  if (buffer) {
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
Update    
Moritz Lipp committed
48
  if (!zathura || !zathura->document || !zathura->document->pages || !zathura->ui.page_view) {
49
50
51
    return;
  }

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

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

65
66
67
68
69
70
  GdkRectangle center;
  center.x = view_rect.x + (view_rect.width + 1) / 2;
  center.y = view_rect.y + (view_rect.height + 1) / 2;
  center.height = center.width = 2*page_padding + 1;

  bool updated = false;
71
  /* find page that fits */
Moritz Lipp's avatar
Moritz Lipp committed
72
  for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++)
73
  {
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
82
83
    GdkRectangle page_rect;
    page_rect.x = offset.x;
    page_rect.y = offset.y;
    page_rect.width = page->width * zathura->document->scale;
    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
89
90
      if (updated == false && gdk_rectangle_intersect(&center, &page_rect, NULL) == TRUE) {
        zathura->document->current_page_number = page_id;
        updated = true;
      }
91
92
    } else {
      page->visible = false;
93
94
    }
  }
95
96

  statusbar_page_number_update(zathura);
97
}
98
99

void
100
cb_pages_per_row_value_changed(girara_session_t* UNUSED(session), const char* UNUSED(name), girara_setting_type_t UNUSED(type), void* value, void* data)
101
{
102
103
104
105
  g_return_if_fail(value != NULL);

  int pages_per_row = *(int*) value;
  zathura_t* zathura = data;
106

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

  page_view_set_mode(zathura, pages_per_row);
}
113
114

void
Moritz Lipp's avatar
Moritz Lipp committed
115
cb_index_row_activated(GtkTreeView* tree_view, GtkTreePath* path,
Sebastian Ramacher's avatar
Sebastian Ramacher committed
116
    GtkTreeViewColumn* UNUSED(column), void* data)
117
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
118
  zathura_t* zathura = data;
119
120
121
122
123
124
125
126
127
128
129
130
  if (tree_view == NULL || zathura == NULL || zathura->ui.session == NULL) {
    return;
  }

  GtkTreeModel  *model;
  GtkTreeIter   iter;

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

  if(gtk_tree_model_get_iter(model, &iter, path))
  {
    zathura_index_element_t* index_element;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
131
    gtk_tree_model_get(model, &iter, 2, &index_element, -1);
132
133
134
135
136
137

    if (index_element == NULL) {
      return;
    }

    if (index_element->type == ZATHURA_LINK_TO_PAGE) {
Moritz Lipp's avatar
Moritz Lipp committed
138
      sc_toggle_index(zathura->ui.session, NULL, NULL, 0);
139
      page_set_delayed(zathura, index_element->target.page_number);
140
    } else if (index_element->type == ZATHURA_LINK_EXTERNAL) {
Moritz Lipp's avatar
Moritz Lipp committed
141
      if (girara_xdg_open(index_element->target.uri) == false) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
142
        girara_notify(zathura->ui.session, GIRARA_ERROR, "Failed to run xdg-open.");
143
      }
144
145
146
147
148
    }
  }

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

Moritz Lipp's avatar
Moritz Lipp committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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;

  char* input = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
  if (input == NULL) {
    goto error_ret;
  } else if (strlen(input) == 0) {
    goto error_free;
  }

  int index = atoi(input);
  if (index == 0 && g_strcmp0(input, "0") != 0) {
    girara_notify(session, GIRARA_WARNING, "Invalid input '%s' given.", input);
    goto error_free;
  }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
170
  index = index-1;
Moritz Lipp's avatar
Moritz Lipp committed
171
172
173
174
175
176
177
178
179

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

180
181
    g_object_set(page->drawing_area, "draw-links", FALSE, NULL);

Moritz Lipp's avatar
Moritz Lipp committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    zathura_link_t* link = zathura_page_view_link_get(ZATHURA_PAGE_VIEW(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;
      break;
    }
  }

  if (invalid_index == true) {
    girara_notify(session, GIRARA_WARNING, "Invalid index '%s' given.", input);
  }

  g_free(input);

  return TRUE;

error_free:

  g_free(input);

error_ret:

  return FALSE;
}