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

Moritz Lipp's avatar
Moritz Lipp committed
3
4
#include <stdlib.h>

Moritz Lipp's avatar
Moritz Lipp committed
5
6
#include "callbacks.h"
#include "config.h"
Moritz Lipp's avatar
Moritz Lipp committed
7
#include "ft/document.h"
Moritz Lipp's avatar
Moritz Lipp committed
8
#include "shortcuts.h"
Moritz Lipp's avatar
Moritz Lipp committed
9
#include "zathura.h"
Moritz Lipp's avatar
Moritz Lipp committed
10
#include "utils.h"
Moritz Lipp's avatar
Moritz Lipp committed
11

Moritz Lipp's avatar
Moritz Lipp committed
12
13
14
/* function implementation */
bool
init_zathura()
15
{
16
  /* UI */
Moritz Lipp's avatar
Moritz Lipp committed
17
  if(!(Zathura.UI.session = girara_session_create())) {
Moritz Lipp's avatar
Moritz Lipp committed
18
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
19
  }
20

Moritz Lipp's avatar
Moritz Lipp committed
21
  if(!girara_session_init(Zathura.UI.session)) {
Moritz Lipp's avatar
Moritz Lipp committed
22
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
23
  }
24

Moritz Lipp's avatar
Moritz Lipp committed
25
26
27
28
  Zathura.UI.statusbar.file        = NULL;
  Zathura.UI.statusbar.buffer      = NULL;
  Zathura.UI.statusbar.page_number = NULL;
  Zathura.UI.drawing_area          = NULL;
29
30
31
32
33
34
35
36
37
38
  Zathura.UI.page_view             = NULL;

  /* page view */
  Zathura.UI.page_view = gtk_vbox_new(FALSE, 0);
  if(!Zathura.UI.page_view) {
    goto error_free;
  }

  gtk_widget_show(Zathura.UI.page_view);
  gtk_box_set_spacing(GTK_BOX(Zathura.UI.page_view), 0);
Moritz Lipp's avatar
Moritz Lipp committed
39

40
  /* statusbar */
Moritz Lipp's avatar
Moritz Lipp committed
41
42
  Zathura.UI.statusbar.file = girara_statusbar_item_add(Zathura.UI.session, TRUE, TRUE, TRUE, NULL);
  if(!Zathura.UI.statusbar.file) {
Moritz Lipp's avatar
Moritz Lipp committed
43
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
44
45
  }

Moritz Lipp's avatar
Moritz Lipp committed
46
47
  Zathura.UI.statusbar.buffer = girara_statusbar_item_add(Zathura.UI.session, FALSE, FALSE, FALSE, NULL);
  if(!Zathura.UI.statusbar.buffer) {
Moritz Lipp's avatar
Moritz Lipp committed
48
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
49
50
51
52
  }

  Zathura.UI.statusbar.page_number = girara_statusbar_item_add(Zathura.UI.session, FALSE, FALSE, FALSE, NULL);
  if(!Zathura.UI.statusbar.page_number) {
Moritz Lipp's avatar
Moritz Lipp committed
53
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
54
55
56
57
  }

  girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.file, "[No Name]");

Moritz Lipp's avatar
Moritz Lipp committed
58
59
60
  /* signals */
  g_signal_connect(G_OBJECT(Zathura.UI.session->gtk.window), "destroy", G_CALLBACK(cb_destroy), NULL);

61
62
63
  GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(Zathura.UI.session->gtk.view));
  g_signal_connect(G_OBJECT(view_vadjustment), "value-changed", G_CALLBACK(cb_view_vadjustment_value_changed), NULL);

Moritz Lipp's avatar
Moritz Lipp committed
64
65
66
  /* girara events */
  Zathura.UI.session->events.buffer_changed = buffer_changed;

Moritz Lipp's avatar
Moritz Lipp committed
67
68
69
  /* configuration */
  config_load_default();

Moritz Lipp's avatar
Moritz Lipp committed
70
  return true;
Moritz Lipp's avatar
Moritz Lipp committed
71
72
73
74
75
76
77

error_free:

  if(Zathura.UI.drawing_area) {
    g_object_unref(Zathura.UI.drawing_area);
  }

78
79
80
81
  if(Zathura.UI.page_view) {
    g_object_unref(Zathura.UI.page_view);
  }

Moritz Lipp's avatar
Moritz Lipp committed
82
83
84
85
86
  girara_session_destroy(Zathura.UI.session);

error_out:

  return false;
87
88
}

Moritz Lipp's avatar
Moritz Lipp committed
89
90
91
92
bool
document_open(const char* path, const char* password)
{
  if(!path) {
93
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
94
95
96
97
98
  }

  zathura_document_t* document = zathura_document_open(path, password);

  if(!document) {
99
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
100
101
102
103
  }

  Zathura.document = document;

104
  /* create blank pages */
Moritz Lipp's avatar
Moritz Lipp committed
105
106
  for(unsigned int i = 0; i < document->number_of_pages; i++)
  {
107
108
    /* create blank page */
    GtkWidget* image = page_blank(document->pages[i]->width, document->pages[i]->height);
Moritz Lipp's avatar
Moritz Lipp committed
109
110

    if(!image) {
111
      goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
112
113
    }

114
115
    /* pack to page view */
    gtk_box_pack_start(GTK_BOX(Zathura.UI.page_view), image, TRUE,  TRUE, 0);
Moritz Lipp's avatar
Moritz Lipp committed
116
117
  }

118
  girara_set_view(Zathura.UI.session, Zathura.UI.page_view);
Moritz Lipp's avatar
Moritz Lipp committed
119

120
121
122
123
  /* first page */
  if(!page_set(0)) {
    goto error_free;
  }
Moritz Lipp's avatar
Moritz Lipp committed
124
125

  return true;
126
127
128
129
130
131
132
133

error_free:

  zathura_document_free(document);

error_out:

  return false;
Moritz Lipp's avatar
Moritz Lipp committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
}

bool
document_close()
{
  if(!Zathura.document) {
    return false;
  }

  zathura_document_free(Zathura.document);

  return true;
}

Moritz Lipp's avatar
Moritz Lipp committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
bool
page_render(zathura_page_t* page)
{
  GtkWidget* image = zathura_page_render(page);
  if(!image) {
    goto error_out;
  }

  /* draw new rendered page */
  if(!girara_set_view(Zathura.UI.session, image)) {
    goto error_out;
  }

  return true;

error_out:

  return false;
}

Moritz Lipp's avatar
Moritz Lipp committed
168
169
170
bool
page_set(unsigned int page_id)
{
171
  if(!Zathura.document || !Zathura.document->pages) {
Moritz Lipp's avatar
Moritz Lipp committed
172
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
173
174
175
  }

  if(page_id >= Zathura.document->number_of_pages) {
Moritz Lipp's avatar
Moritz Lipp committed
176
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
177
178
  }

179
  /* render page */
180
  zathura_page_t* page = Zathura.document->pages[page_id];
Moritz Lipp's avatar
Moritz Lipp committed
181

182
183
184
185
  if(!page) {
    goto error_out;
  }

186
187
  GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(Zathura.UI.session->gtk.view));
  gtk_adjustment_set_value(view_vadjustment, page->offset);
188
189

  /* update page number */
Moritz Lipp's avatar
Moritz Lipp committed
190
191
192
193
194
195
196
  Zathura.document->current_page_number = page_id;

  char* page_number = g_strdup_printf("[%d/%d]", page_id + 1, Zathura.document->number_of_pages);
  girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.page_number, page_number);
  g_free(page_number);

  return true;
Moritz Lipp's avatar
Moritz Lipp committed
197
198
199
200

error_out:

  return false;
Moritz Lipp's avatar
Moritz Lipp committed
201
202
}

Moritz Lipp's avatar
Moritz Lipp committed
203
/* main function */
204
int main(int argc, char* argv[])
Moritz Lipp's avatar
Moritz Lipp committed
205
{
206
207
  g_thread_init(NULL);
  gdk_threads_init();
Moritz Lipp's avatar
Moritz Lipp committed
208
209
  gtk_init(&argc, &argv);

Moritz Lipp's avatar
Moritz Lipp committed
210
211
212
213
  if(!init_zathura()) {
    printf("error: coult not initialize zathura\n");
    return -1;
  }
Moritz Lipp's avatar
Moritz Lipp committed
214
215

  if(argc > 1) {
Moritz Lipp's avatar
Moritz Lipp committed
216
217
    if(!document_open(argv[1], NULL)) {
      printf("error: could not open document\n");
Moritz Lipp's avatar
Moritz Lipp committed
218
219
      return -1;
    }
220
  }
Moritz Lipp's avatar
Moritz Lipp committed
221

Moritz Lipp's avatar
Moritz Lipp committed
222
223
224
  gdk_threads_enter();
  gtk_main();
  gdk_threads_leave();
225

Moritz Lipp's avatar
Moritz Lipp committed
226
227
  return 0;
}