2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) any later version.
7 * This software is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <libgnomecanvas/libgnomecanvas.h>
31 #include <glib/gstdio.h>
32 #include <poppler/glib/poppler.h>
40 #include "xo-interface.h"
41 #include "xo-support.h"
42 #include "xo-callbacks.h"
47 const char *tool_names[NUM_TOOLS] = {"pen", "eraser", "highlighter", "text", "", "selectrect", "vertspace", "hand"};
48 const char *color_names[COLOR_MAX] = {"black", "blue", "red", "green",
49 "gray", "lightblue", "lightgreen", "magenta", "orange", "yellow", "white"};
50 const char *bgtype_names[3] = {"solid", "pixmap", "pdf"};
51 const char *bgcolor_names[COLOR_MAX] = {"", "blue", "pink", "green",
52 "", "", "", "", "orange", "yellow", "white"};
53 const char *bgstyle_names[4] = {"plain", "lined", "ruled", "graph"};
54 const char *file_domain_names[3] = {"absolute", "attach", "clone"};
55 const char *unit_names[4] = {"cm", "in", "px", "pt"};
56 int PDFTOPPM_PRINTING_DPI, GS_BITMAP_DPI;
58 // creates a new empty journal
60 void new_journal(void)
63 journal.pages = g_list_append(NULL, new_page(&ui.default_page));
64 journal.last_attach_no = 0;
67 ui.cur_page = (struct Page *) journal.pages->data;
68 ui.cur_layer = (struct Layer *) ui.cur_page->layers->data;
71 update_file_name(NULL);
74 // check attachment names
76 void chk_attach_names(void)
79 struct Background *bg;
81 for (list = journal.pages; list!=NULL; list = list->next) {
82 bg = ((struct Page *)list->data)->bg;
83 if (bg->type == BG_SOLID || bg->file_domain != DOMAIN_ATTACH ||
84 bg->filename->s != NULL) continue;
85 bg->filename->s = g_strdup_printf("bg_%d.png", ++journal.last_attach_no);
89 // saves the journal to a file: returns true on success, false on error
91 gboolean save_journal(const char *filename)
94 struct Page *pg, *tmppg;
101 GList *pagelist, *layerlist, *itemlist, *list;
104 f = gzopen(filename, "wb");
105 if (f==NULL) return FALSE;
108 setlocale(LC_NUMERIC, "C");
110 gzprintf(f, "<?xml version=\"1.0\" standalone=\"no\"?>\n"
111 "<xournal version=\"" VERSION "\">\n"
112 "<title>Xournal document - see http://math.mit.edu/~auroux/software/xournal/</title>\n");
113 for (pagelist = journal.pages; pagelist!=NULL; pagelist = pagelist->next) {
114 pg = (struct Page *)pagelist->data;
115 gzprintf(f, "<page width=\"%.2f\" height=\"%.2f\">\n", pg->width, pg->height);
116 gzprintf(f, "<background type=\"%s\" ", bgtype_names[pg->bg->type]);
117 if (pg->bg->type == BG_SOLID) {
118 gzputs(f, "color=\"");
119 if (pg->bg->color_no >= 0) gzputs(f, bgcolor_names[pg->bg->color_no]);
120 else gzprintf(f, "#%08x", pg->bg->color_rgba);
121 gzprintf(f, "\" style=\"%s\" ", bgstyle_names[pg->bg->ruling]);
123 else if (pg->bg->type == BG_PIXMAP) {
125 for (list = journal.pages, i = 0; list!=pagelist; list = list->next, i++) {
126 tmppg = (struct Page *)list->data;
127 if (tmppg->bg->type == BG_PIXMAP &&
128 tmppg->bg->pixbuf == pg->bg->pixbuf &&
129 tmppg->bg->filename == pg->bg->filename)
130 { is_clone = i; break; }
133 gzprintf(f, "domain=\"clone\" filename=\"%d\" ", is_clone);
135 if (pg->bg->file_domain == DOMAIN_ATTACH) {
136 tmpfn = g_strdup_printf("%s.%s", filename, pg->bg->filename->s);
137 if (!gdk_pixbuf_save(pg->bg->pixbuf, tmpfn, "png", NULL, NULL)) {
138 dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
139 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
140 _("Could not write background '%s'. Continuing anyway."), tmpfn);
141 gtk_dialog_run(GTK_DIALOG(dialog));
142 gtk_widget_destroy(dialog);
146 tmpstr = g_markup_escape_text(pg->bg->filename->s, -1);
147 gzprintf(f, "domain=\"%s\" filename=\"%s\" ",
148 file_domain_names[pg->bg->file_domain], tmpstr);
152 else if (pg->bg->type == BG_PDF) {
154 for (list = journal.pages; list!=pagelist; list = list->next) {
155 tmppg = (struct Page *)list->data;
156 if (tmppg->bg->type == BG_PDF) { is_clone = 1; break; }
159 if (pg->bg->file_domain == DOMAIN_ATTACH) {
160 tmpfn = g_strdup_printf("%s.%s", filename, pg->bg->filename->s);
162 if (bgpdf.status != STATUS_NOT_INIT && bgpdf.file_contents != NULL)
164 tmpf = fopen(tmpfn, "wb");
165 if (tmpf != NULL && fwrite(bgpdf.file_contents, 1, bgpdf.file_length, tmpf) == bgpdf.file_length)
170 dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
171 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
172 _("Could not write background '%s'. Continuing anyway."), tmpfn);
173 gtk_dialog_run(GTK_DIALOG(dialog));
174 gtk_widget_destroy(dialog);
178 tmpstr = g_markup_escape_text(pg->bg->filename->s, -1);
179 gzprintf(f, "domain=\"%s\" filename=\"%s\" ",
180 file_domain_names[pg->bg->file_domain], tmpstr);
183 gzprintf(f, "pageno=\"%d\" ", pg->bg->file_page_seq);
186 for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
187 layer = (struct Layer *)layerlist->data;
188 gzprintf(f, "<layer>\n");
189 for (itemlist = layer->items; itemlist!=NULL; itemlist = itemlist->next) {
190 item = (struct Item *)itemlist->data;
191 if (item->type == ITEM_STROKE) {
192 gzprintf(f, "<stroke tool=\"%s\" color=\"",
193 tool_names[item->brush.tool_type]);
194 if (item->brush.color_no >= 0)
195 gzputs(f, color_names[item->brush.color_no]);
197 gzprintf(f, "#%08x", item->brush.color_rgba);
198 gzprintf(f, "\" width=\"%.2f", item->brush.thickness);
199 if (item->brush.variable_width)
200 for (i=0;i<item->path->num_points-1;i++)
201 gzprintf(f, " %.2f", item->widths[i]);
202 gzprintf(f, "\">\n");
203 for (i=0;i<2*item->path->num_points;i++)
204 gzprintf(f, "%.2f ", item->path->coords[i]);
205 gzprintf(f, "\n</stroke>\n");
207 if (item->type == ITEM_TEXT) {
208 tmpstr = g_markup_escape_text(item->font_name, -1);
209 gzprintf(f, "<text font=\"%s\" size=\"%.2f\" x=\"%.2f\" y=\"%.2f\" color=\"",
210 tmpstr, item->font_size, item->bbox.left, item->bbox.top);
212 if (item->brush.color_no >= 0)
213 gzputs(f, color_names[item->brush.color_no]);
215 gzprintf(f, "#%08x", item->brush.color_rgba);
216 tmpstr = g_markup_escape_text(item->text, -1);
218 gzputs(f, tmpstr); // gzprintf() can't handle > 4095 bytes
219 gzputs(f, "</text>\n");
223 gzprintf(f, "</layer>\n");
225 gzprintf(f, "</page>\n");
227 gzprintf(f, "</xournal>\n");
229 setlocale(LC_NUMERIC, "");
234 // closes a journal: returns true on success, false on abort
236 gboolean close_journal(void)
238 if (!ok_to_close()) return FALSE;
240 // free everything...
247 delete_journal(&journal);
250 /* note: various members of ui and journal are now in invalid states,
251 use new_journal() to reinitialize them */
254 // sanitize a string containing floats, in case it may have , instead of .
255 // also replace Windows-produced 1.#J by inf
257 void cleanup_numeric(char *s)
261 if (*s=='1' && s[1]=='.' && s[2]=='#' && s[3]=='J')
262 { *s='i'; s[1]='n'; s[2]='f'; s[3]=' '; }
267 // the XML parser functions for open_journal()
269 struct Journal tmpJournal;
270 struct Page *tmpPage;
271 struct Layer *tmpLayer;
272 struct Item *tmpItem;
274 struct Background *tmpBg_pdf;
276 GError *xoj_invalid(void)
278 return g_error_new(G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, _("Invalid file contents"));
281 void xoj_parser_start_element(GMarkupParseContext *context,
282 const gchar *element_name, const gchar **attribute_names,
283 const gchar **attribute_values, gpointer user_data, GError **error)
287 struct Background *tmpbg;
288 char *tmpbg_filename;
292 if (!strcmp(element_name, "title") || !strcmp(element_name, "xournal")) {
293 if (tmpPage != NULL) {
294 *error = xoj_invalid();
297 // nothing special to do
299 else if (!strcmp(element_name, "page")) { // start of a page
300 if (tmpPage != NULL) {
301 *error = xoj_invalid();
304 tmpPage = (struct Page *)g_malloc(sizeof(struct Page));
305 tmpPage->layers = NULL;
306 tmpPage->nlayers = 0;
307 tmpPage->group = NULL;
308 tmpPage->bg = g_new(struct Background, 1);
309 tmpPage->bg->type = -1;
310 tmpPage->bg->canvas_item = NULL;
311 tmpPage->bg->pixbuf = NULL;
312 tmpPage->bg->filename = NULL;
313 tmpJournal.pages = g_list_append(tmpJournal.pages, tmpPage);
315 // scan for height and width attributes
317 while (*attribute_names!=NULL) {
318 if (!strcmp(*attribute_names, "width")) {
319 if (has_attr & 1) *error = xoj_invalid();
320 cleanup_numeric((gchar *)*attribute_values);
321 tmpPage->width = g_ascii_strtod(*attribute_values, &ptr);
322 if (ptr == *attribute_values) *error = xoj_invalid();
325 else if (!strcmp(*attribute_names, "height")) {
326 if (has_attr & 2) *error = xoj_invalid();
327 cleanup_numeric((gchar *)*attribute_values);
328 tmpPage->height = g_ascii_strtod(*attribute_values, &ptr);
329 if (ptr == *attribute_values) *error = xoj_invalid();
332 else *error = xoj_invalid();
336 if (has_attr!=3) *error = xoj_invalid();
338 else if (!strcmp(element_name, "background")) {
339 if (tmpPage == NULL || tmpLayer !=NULL || tmpPage->bg->type >= 0) {
340 *error = xoj_invalid();
344 while (*attribute_names!=NULL) {
345 if (!strcmp(*attribute_names, "type")) {
346 if (has_attr) *error = xoj_invalid();
348 if (!strcmp(*attribute_values, bgtype_names[i]))
349 tmpPage->bg->type = i;
350 if (tmpPage->bg->type < 0) *error = xoj_invalid();
352 if (tmpPage->bg->type == BG_PDF) {
353 if (tmpBg_pdf == NULL) tmpBg_pdf = tmpPage->bg;
356 tmpPage->bg->filename = refstring_ref(tmpBg_pdf->filename);
357 tmpPage->bg->file_domain = tmpBg_pdf->file_domain;
361 else if (!strcmp(*attribute_names, "color")) {
362 if (tmpPage->bg->type != BG_SOLID) *error = xoj_invalid();
363 if (has_attr & 2) *error = xoj_invalid();
364 tmpPage->bg->color_no = COLOR_OTHER;
365 for (i=0; i<COLOR_MAX; i++)
366 if (!strcmp(*attribute_values, bgcolor_names[i])) {
367 tmpPage->bg->color_no = i;
368 tmpPage->bg->color_rgba = predef_bgcolors_rgba[i];
370 // there's also the case of hex (#rrggbbaa) colors
371 if (tmpPage->bg->color_no == COLOR_OTHER && **attribute_values == '#') {
372 tmpPage->bg->color_rgba = strtoul(*attribute_values + 1, &ptr, 16);
373 if (*ptr!=0) *error = xoj_invalid();
377 else if (!strcmp(*attribute_names, "style")) {
378 if (tmpPage->bg->type != BG_SOLID) *error = xoj_invalid();
379 if (has_attr & 4) *error = xoj_invalid();
380 tmpPage->bg->ruling = -1;
382 if (!strcmp(*attribute_values, bgstyle_names[i]))
383 tmpPage->bg->ruling = i;
384 if (tmpPage->bg->ruling < 0) *error = xoj_invalid();
387 else if (!strcmp(*attribute_names, "domain")) {
388 if (tmpPage->bg->type <= BG_SOLID || (has_attr & 8))
389 { *error = xoj_invalid(); return; }
390 tmpPage->bg->file_domain = -1;
392 if (!strcmp(*attribute_values, file_domain_names[i]))
393 tmpPage->bg->file_domain = i;
394 if (tmpPage->bg->file_domain < 0)
395 { *error = xoj_invalid(); return; }
398 else if (!strcmp(*attribute_names, "filename")) {
399 if (tmpPage->bg->type <= BG_SOLID || (has_attr != 9))
400 { *error = xoj_invalid(); return; }
401 if (tmpPage->bg->file_domain == DOMAIN_CLONE) {
402 // filename is a page number
403 i = strtol(*attribute_values, &ptr, 10);
404 if (ptr == *attribute_values || i < 0 || i > tmpJournal.npages-2)
405 { *error = xoj_invalid(); return; }
406 tmpbg = ((struct Page *)g_list_nth_data(tmpJournal.pages, i))->bg;
407 if (tmpbg->type != tmpPage->bg->type)
408 { *error = xoj_invalid(); return; }
409 tmpPage->bg->filename = refstring_ref(tmpbg->filename);
410 tmpPage->bg->pixbuf = tmpbg->pixbuf;
411 if (tmpbg->pixbuf!=NULL) gdk_pixbuf_ref(tmpbg->pixbuf);
412 tmpPage->bg->file_domain = tmpbg->file_domain;
415 tmpPage->bg->filename = new_refstring(*attribute_values);
416 if (tmpPage->bg->type == BG_PIXMAP) {
417 if (tmpPage->bg->file_domain == DOMAIN_ATTACH) {
418 tmpbg_filename = g_strdup_printf("%s.%s", tmpFilename, *attribute_values);
419 if (sscanf(*attribute_values, "bg_%d.png", &i) == 1)
420 if (i > tmpJournal.last_attach_no)
421 tmpJournal.last_attach_no = i;
423 else tmpbg_filename = g_strdup(*attribute_values);
424 tmpPage->bg->pixbuf = gdk_pixbuf_new_from_file(tmpbg_filename, NULL);
425 if (tmpPage->bg->pixbuf == NULL) {
426 dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
427 GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
428 _("Could not open background '%s'. Setting background to white."),
430 gtk_dialog_run(GTK_DIALOG(dialog));
431 gtk_widget_destroy(dialog);
432 tmpPage->bg->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 1, 1);
433 gdk_pixbuf_fill(tmpPage->bg->pixbuf, 0xffffffff); // solid white
435 g_free(tmpbg_filename);
440 else if (!strcmp(*attribute_names, "pageno")) {
441 if (tmpPage->bg->type != BG_PDF || (has_attr & 32))
442 { *error = xoj_invalid(); return; }
443 tmpPage->bg->file_page_seq = strtol(*attribute_values, &ptr, 10);
444 if (ptr == *attribute_values) *error = xoj_invalid();
447 else *error = xoj_invalid();
451 if (tmpPage->bg->type < 0) *error = xoj_invalid();
452 if (tmpPage->bg->type == BG_SOLID && has_attr != 7) *error = xoj_invalid();
453 if (tmpPage->bg->type == BG_PIXMAP && has_attr != 25) *error = xoj_invalid();
454 if (tmpPage->bg->type == BG_PDF && has_attr != 57) *error = xoj_invalid();
456 else if (!strcmp(element_name, "layer")) { // start of a layer
457 if (tmpPage == NULL || tmpLayer != NULL) {
458 *error = xoj_invalid();
461 tmpLayer = (struct Layer *)g_malloc(sizeof(struct Layer));
462 tmpLayer->items = NULL;
463 tmpLayer->nitems = 0;
464 tmpLayer->group = NULL;
465 tmpPage->layers = g_list_append(tmpPage->layers, tmpLayer);
468 else if (!strcmp(element_name, "stroke")) { // start of a stroke
469 if (tmpLayer == NULL || tmpItem != NULL) {
470 *error = xoj_invalid();
473 tmpItem = (struct Item *)g_malloc(sizeof(struct Item));
474 tmpItem->type = ITEM_STROKE;
475 tmpItem->path = NULL;
476 tmpItem->canvas_item = NULL;
477 tmpItem->widths = NULL;
478 tmpLayer->items = g_list_append(tmpLayer->items, tmpItem);
480 // scan for tool, color, and width attributes
482 while (*attribute_names!=NULL) {
483 if (!strcmp(*attribute_names, "width")) {
484 if (has_attr & 1) *error = xoj_invalid();
485 cleanup_numeric((gchar *)*attribute_values);
486 tmpItem->brush.thickness = g_ascii_strtod(*attribute_values, &ptr);
487 if (ptr == *attribute_values) *error = xoj_invalid();
490 realloc_cur_widths(i+1);
491 ui.cur_widths[i] = g_ascii_strtod(ptr, &tmpptr);
492 if (tmpptr == ptr) break;
496 tmpItem->brush.variable_width = (i>0);
498 tmpItem->brush.variable_width = TRUE;
499 tmpItem->widths = (gdouble *) g_memdup(ui.cur_widths, i*sizeof(gdouble));
500 ui.cur_path.num_points = i+1;
504 else if (!strcmp(*attribute_names, "color")) {
505 if (has_attr & 2) *error = xoj_invalid();
506 tmpItem->brush.color_no = COLOR_OTHER;
507 for (i=0; i<COLOR_MAX; i++)
508 if (!strcmp(*attribute_values, color_names[i])) {
509 tmpItem->brush.color_no = i;
510 tmpItem->brush.color_rgba = predef_colors_rgba[i];
512 // there's also the case of hex (#rrggbbaa) colors
513 if (tmpItem->brush.color_no == COLOR_OTHER && **attribute_values == '#') {
514 tmpItem->brush.color_rgba = strtoul(*attribute_values + 1, &ptr, 16);
515 if (*ptr!=0) *error = xoj_invalid();
519 else if (!strcmp(*attribute_names, "tool")) {
520 if (has_attr & 4) *error = xoj_invalid();
521 tmpItem->brush.tool_type = -1;
522 for (i=0; i<NUM_STROKE_TOOLS; i++)
523 if (!strcmp(*attribute_values, tool_names[i])) {
524 tmpItem->brush.tool_type = i;
526 if (tmpItem->brush.tool_type == -1) *error = xoj_invalid();
529 else *error = xoj_invalid();
533 if (has_attr!=7) *error = xoj_invalid();
534 // finish filling the brush info
535 tmpItem->brush.thickness_no = 0; // who cares ?
536 tmpItem->brush.tool_options = 0; // who cares ?
537 tmpItem->brush.ruler = FALSE;
538 tmpItem->brush.recognizer = FALSE;
539 if (tmpItem->brush.tool_type == TOOL_HIGHLIGHTER) {
540 if (tmpItem->brush.color_no >= 0)
541 tmpItem->brush.color_rgba &= ui.hiliter_alpha_mask;
544 else if (!strcmp(element_name, "text")) { // start of a text item
545 if (tmpLayer == NULL || tmpItem != NULL) {
546 *error = xoj_invalid();
549 tmpItem = (struct Item *)g_malloc0(sizeof(struct Item));
550 tmpItem->type = ITEM_TEXT;
551 tmpItem->canvas_item = NULL;
552 tmpLayer->items = g_list_append(tmpLayer->items, tmpItem);
554 // scan for font, size, x, y, and color attributes
556 while (*attribute_names!=NULL) {
557 if (!strcmp(*attribute_names, "font")) {
558 if (has_attr & 1) *error = xoj_invalid();
559 tmpItem->font_name = g_strdup(*attribute_values);
562 else if (!strcmp(*attribute_names, "size")) {
563 if (has_attr & 2) *error = xoj_invalid();
564 cleanup_numeric((gchar *)*attribute_values);
565 tmpItem->font_size = g_ascii_strtod(*attribute_values, &ptr);
566 if (ptr == *attribute_values) *error = xoj_invalid();
569 else if (!strcmp(*attribute_names, "x")) {
570 if (has_attr & 4) *error = xoj_invalid();
571 cleanup_numeric((gchar *)*attribute_values);
572 tmpItem->bbox.left = g_ascii_strtod(*attribute_values, &ptr);
573 if (ptr == *attribute_values) *error = xoj_invalid();
576 else if (!strcmp(*attribute_names, "y")) {
577 if (has_attr & 8) *error = xoj_invalid();
578 cleanup_numeric((gchar *)*attribute_values);
579 tmpItem->bbox.top = g_ascii_strtod(*attribute_values, &ptr);
580 if (ptr == *attribute_values) *error = xoj_invalid();
583 else if (!strcmp(*attribute_names, "color")) {
584 if (has_attr & 16) *error = xoj_invalid();
585 tmpItem->brush.color_no = COLOR_OTHER;
586 for (i=0; i<COLOR_MAX; i++)
587 if (!strcmp(*attribute_values, color_names[i])) {
588 tmpItem->brush.color_no = i;
589 tmpItem->brush.color_rgba = predef_colors_rgba[i];
591 // there's also the case of hex (#rrggbbaa) colors
592 if (tmpItem->brush.color_no == COLOR_OTHER && **attribute_values == '#') {
593 tmpItem->brush.color_rgba = strtoul(*attribute_values + 1, &ptr, 16);
594 if (*ptr!=0) *error = xoj_invalid();
598 else *error = xoj_invalid();
602 if (has_attr!=31) *error = xoj_invalid();
606 void xoj_parser_end_element(GMarkupParseContext *context,
607 const gchar *element_name, gpointer user_data, GError **error)
609 if (!strcmp(element_name, "page")) {
610 if (tmpPage == NULL || tmpLayer != NULL) {
611 *error = xoj_invalid();
614 if (tmpPage->nlayers == 0 || tmpPage->bg->type < 0) *error = xoj_invalid();
617 if (!strcmp(element_name, "layer")) {
618 if (tmpLayer == NULL || tmpItem != NULL) {
619 *error = xoj_invalid();
624 if (!strcmp(element_name, "stroke")) {
625 if (tmpItem == NULL) {
626 *error = xoj_invalid();
629 update_item_bbox(tmpItem);
632 if (!strcmp(element_name, "text")) {
633 if (tmpItem == NULL) {
634 *error = xoj_invalid();
641 void xoj_parser_text(GMarkupParseContext *context,
642 const gchar *text, gsize text_len, gpointer user_data, GError **error)
644 const gchar *element_name, *ptr;
647 element_name = g_markup_parse_context_get_element(context);
648 if (element_name == NULL) return;
649 if (!strcmp(element_name, "stroke")) {
650 cleanup_numeric((gchar *)text);
653 while (text_len > 0) {
654 realloc_cur_path(n/2 + 1);
655 ui.cur_path.coords[n] = g_ascii_strtod(text, (char **)(&ptr));
656 if (ptr == text) break;
657 text_len -= (ptr - text);
659 if (!finite_sized(ui.cur_path.coords[n])) {
660 if (n>=2) ui.cur_path.coords[n] = ui.cur_path.coords[n-2];
661 else ui.cur_path.coords[n] = 0;
666 (tmpItem->brush.variable_width && (n!=2*ui.cur_path.num_points)))
667 { *error = xoj_invalid(); return; } // wrong number of points
668 tmpItem->path = gnome_canvas_points_new(n/2);
669 g_memmove(tmpItem->path->coords, ui.cur_path.coords, n*sizeof(double));
671 if (!strcmp(element_name, "text")) {
672 tmpItem->text = g_malloc(text_len+1);
673 g_memmove(tmpItem->text, text, text_len);
674 tmpItem->text[text_len]=0;
678 gboolean user_wants_second_chance(char **filename)
681 GtkFileFilter *filt_all, *filt_pdf;
682 GtkResponseType response;
684 dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
685 GTK_MESSAGE_ERROR, GTK_BUTTONS_YES_NO,
686 _("Could not open background '%s'.\nSelect another file?"),
688 response = gtk_dialog_run(GTK_DIALOG(dialog));
689 gtk_widget_destroy(dialog);
690 if (response != GTK_RESPONSE_YES) return FALSE;
691 dialog = gtk_file_chooser_dialog_new(_("Open PDF"), GTK_WINDOW (winMain),
692 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
693 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
694 #ifdef FILE_DIALOG_SIZE_BUGFIX
695 gtk_window_set_default_size(GTK_WINDOW(dialog), 500, 400);
698 filt_all = gtk_file_filter_new();
699 gtk_file_filter_set_name(filt_all, _("All files"));
700 gtk_file_filter_add_pattern(filt_all, "*");
701 filt_pdf = gtk_file_filter_new();
702 gtk_file_filter_set_name(filt_pdf, _("PDF files"));
703 gtk_file_filter_add_pattern(filt_pdf, "*.pdf");
704 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filt_pdf);
705 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filt_all);
707 if (ui.default_path!=NULL) gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (dialog), ui.default_path);
709 if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_OK) {
710 gtk_widget_destroy(dialog);
714 *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
715 gtk_widget_destroy(dialog);
719 gboolean open_journal(char *filename)
721 const GMarkupParser parser = { xoj_parser_start_element,
722 xoj_parser_end_element,
723 xoj_parser_text, NULL, NULL};
724 GMarkupParseContext *context;
731 gchar *tmpfn, *tmpfn2, *p, *q;
734 tmpfn = g_strdup_printf("%s.xoj", filename);
735 if (ui.autoload_pdf_xoj && g_file_test(tmpfn, G_FILE_TEST_EXISTS) &&
736 (g_str_has_suffix(filename, ".pdf") || g_str_has_suffix(filename, ".PDF")))
738 valid = open_journal(tmpfn);
744 f = gzopen(filename, "rb");
745 if (f==NULL) return FALSE;
746 if (filename[0]=='/') {
747 if (ui.default_path != NULL) g_free(ui.default_path);
748 ui.default_path = g_path_get_dirname(filename);
751 context = g_markup_parse_context_new(&parser, 0, NULL, NULL);
753 tmpJournal.npages = 0;
754 tmpJournal.pages = NULL;
755 tmpJournal.last_attach_no = 0;
759 tmpFilename = filename;
764 while (valid && !gzeof(f)) {
765 len = gzread(f, buffer, 1000);
766 if (len<0) valid = FALSE;
767 if (maybe_pdf && len>=4 && !strncmp(buffer, "%PDF", 4))
768 { valid = FALSE; break; } // most likely pdf
769 else maybe_pdf = FALSE;
771 valid = g_markup_parse_context_parse(context, buffer, len, &error);
774 if (valid) valid = g_markup_parse_context_end_parse(context, &error);
775 if (tmpJournal.npages == 0) valid = FALSE;
776 g_markup_parse_context_free(context);
779 delete_journal(&tmpJournal);
780 if (!maybe_pdf) return FALSE;
781 // essentially same as on_fileNewBackground from here on
784 while (bgpdf.status != STATUS_NOT_INIT) gtk_main_iteration();
786 ui.zoom = ui.startup_zoom;
787 gnome_canvas_set_pixels_per_unit(canvas, ui.zoom);
789 return init_bgpdf(filename, TRUE, DOMAIN_ABSOLUTE);
792 ui.saved = TRUE; // force close_journal() to do its job
794 g_memmove(&journal, &tmpJournal, sizeof(struct Journal));
796 // if we need to initialize a fresh pdf loader
797 if (tmpBg_pdf!=NULL) {
798 while (bgpdf.status != STATUS_NOT_INIT) gtk_main_iteration();
799 if (tmpBg_pdf->file_domain == DOMAIN_ATTACH)
800 tmpfn = g_strdup_printf("%s.%s", filename, tmpBg_pdf->filename->s);
802 tmpfn = g_strdup(tmpBg_pdf->filename->s);
803 valid = init_bgpdf(tmpfn, FALSE, tmpBg_pdf->file_domain);
804 // if file name is invalid: first try in xoj file's directory
805 if (!valid && tmpBg_pdf->file_domain != DOMAIN_ATTACH) {
806 p = g_path_get_dirname(filename);
807 q = g_path_get_basename(tmpfn);
808 tmpfn2 = g_strdup_printf("%s/%s", p, q);
809 g_free(p); g_free(q);
810 valid = init_bgpdf(tmpfn2, FALSE, tmpBg_pdf->file_domain);
811 if (valid) { // change the file name...
812 printf("substituting %s -> %s\n", tmpfn, tmpfn2);
813 g_free(tmpBg_pdf->filename->s);
814 tmpBg_pdf->filename->s = tmpfn2;
818 // if file name is invalid: next prompt user
819 if (!valid && tmpBg_pdf->file_domain != DOMAIN_ATTACH)
820 if (user_wants_second_chance(&tmpfn)) {
821 valid = init_bgpdf(tmpfn, FALSE, tmpBg_pdf->file_domain);
822 if (valid) { // change the file name...
823 g_free(tmpBg_pdf->filename->s);
824 tmpBg_pdf->filename->s = g_strdup(tmpfn);
828 refstring_unref(bgpdf.filename);
829 bgpdf.filename = refstring_ref(tmpBg_pdf->filename);
831 dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
832 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not open background '%s'."),
834 gtk_dialog_run(GTK_DIALOG(dialog));
835 gtk_widget_destroy(dialog);
841 ui.cur_page = (struct Page *)journal.pages->data;
842 ui.layerno = ui.cur_page->nlayers-1;
843 ui.cur_layer = (struct Layer *)(g_list_last(ui.cur_page->layers)->data);
845 ui.zoom = ui.startup_zoom;
846 update_file_name(g_strdup(filename));
847 gnome_canvas_set_pixels_per_unit(canvas, ui.zoom);
850 rescale_bg_pixmaps(); // this requests the PDF pages if need be
851 gtk_adjustment_set_value(gtk_layout_get_vadjustment(GTK_LAYOUT(canvas)), 0);
855 /************ file backgrounds *************/
857 struct Background *attempt_load_pix_bg(char *filename, gboolean attach)
859 struct Background *bg;
862 pix = gdk_pixbuf_new_from_file(filename, NULL);
863 if (pix == NULL) return NULL;
865 bg = g_new(struct Background, 1);
866 bg->type = BG_PIXMAP;
867 bg->canvas_item = NULL;
869 bg->pixbuf_scale = DEFAULT_ZOOM;
871 bg->filename = new_refstring(NULL);
872 bg->file_domain = DOMAIN_ATTACH;
874 bg->filename = new_refstring(filename);
875 bg->file_domain = DOMAIN_ABSOLUTE;
880 #define BUFSIZE 65536 // a reasonable buffer size for reads from gs pipe
882 GList *attempt_load_gv_bg(char *filename)
884 struct Background *bg;
887 GdkPixbufLoader *loader;
891 int buflen, remnlen, file_pageno;
893 f = fopen(filename, "rb");
894 if (f == NULL) return NULL;
895 buf = g_malloc(BUFSIZE); // a reasonable buffer size
896 if (fread(buf, 1, 4, f) !=4 ||
897 (strncmp((char *)buf, "%!PS", 4) && strncmp((char *)buf, "%PDF", 4))) {
904 pipename = g_strdup_printf(GS_CMDLINE, (double)GS_BITMAP_DPI, filename);
905 gs_pipe = popen(pipename, "rb");
913 while (!feof(gs_pipe)) {
914 if (!remnlen) { // new page: get a BMP header ?
915 buflen = fread(buf, 1, 54, gs_pipe);
916 if (buflen < 6) buflen += fread(buf, 1, 54-buflen, gs_pipe);
917 if (buflen < 6 || buf[0]!='B' || buf[1]!='M') break; // fatal: abort
918 remnlen = (int)(buf[5]<<24) + (buf[4]<<16) + (buf[3]<<8) + (buf[2]);
919 loader = gdk_pixbuf_loader_new();
921 else buflen = fread(buf, 1, (remnlen < BUFSIZE)?remnlen:BUFSIZE, gs_pipe);
923 if (buflen == 0) break;
924 if (!gdk_pixbuf_loader_write(loader, buf, buflen, NULL)) break;
925 if (remnlen == 0) { // make a new bg
926 pix = gdk_pixbuf_loader_get_pixbuf(loader);
927 if (pix == NULL) break;
929 gdk_pixbuf_loader_close(loader, NULL);
930 g_object_unref(loader);
932 bg = g_new(struct Background, 1);
933 bg->canvas_item = NULL;
935 bg->pixbuf_scale = (GS_BITMAP_DPI/72.0);
936 bg->type = BG_PIXMAP;
937 bg->filename = new_refstring(NULL);
938 bg->file_domain = DOMAIN_ATTACH;
940 bg_list = g_list_append(bg_list, bg);
943 if (loader != NULL) gdk_pixbuf_loader_close(loader, NULL);
949 struct Background *attempt_screenshot_bg(void)
952 struct Background *bg;
957 Window x_root, x_win;
959 x_root = gdk_x11_get_default_root_xwindow();
961 if (!XGrabButton(GDK_DISPLAY(), AnyButton, AnyModifier, x_root,
962 False, ButtonReleaseMask, GrabModeAsync, GrabModeSync, None, None))
965 XWindowEvent (GDK_DISPLAY(), x_root, ButtonReleaseMask, &x_event);
966 XUngrabButton(GDK_DISPLAY(), AnyButton, AnyModifier, x_root);
968 x_win = x_event.xbutton.subwindow;
969 if (x_win == None) x_win = x_root;
971 window = gdk_window_foreign_new_for_display(gdk_display_get_default(), x_win);
973 gdk_window_get_geometry(window, &x, &y, &w, &h, NULL);
975 pix = gdk_pixbuf_get_from_drawable(NULL, window,
976 gdk_colormap_get_system(), 0, 0, 0, 0, w, h);
978 if (pix == NULL) return NULL;
980 bg = g_new(struct Background, 1);
981 bg->type = BG_PIXMAP;
982 bg->canvas_item = NULL;
984 bg->pixbuf_scale = DEFAULT_ZOOM;
985 bg->filename = new_refstring(NULL);
986 bg->file_domain = DOMAIN_ATTACH;
989 // not implemented under WIN32
994 /************** pdf annotation ***************/
996 /* cancel a request */
998 void cancel_bgpdf_request(struct BgPdfRequest *req)
1002 list_link = g_list_find(bgpdf.requests, req);
1003 if (list_link == NULL) return;
1004 // remove the request
1005 bgpdf.requests = g_list_delete_link(bgpdf.requests, list_link);
1009 /* process a bg PDF request from the queue, and recurse */
1011 gboolean bgpdf_scheduler_callback(gpointer data)
1013 struct BgPdfRequest *req;
1014 struct BgPdfPage *bgpg;
1017 PopplerPage *pdfpage;
1018 gdouble height, width;
1019 int scaled_height, scaled_width;
1023 // if all requests have been cancelled, remove ourselves from main loop
1024 if (bgpdf.requests == NULL) { bgpdf.pid = 0; return FALSE; }
1025 if (bgpdf.status == STATUS_NOT_INIT)
1026 { printf("DEBUG: BGPDF not initialized??\n"); bgpdf.pid = 0; return FALSE; }
1028 req = (struct BgPdfRequest *)bgpdf.requests->data;
1030 // use poppler to generate the page
1032 pdfpage = poppler_document_get_page(bgpdf.document, req->pageno-1);
1034 // printf("DEBUG: Processing request for page %d at %f dpi\n", req->pageno, req->dpi);
1035 set_cursor_busy(TRUE);
1036 poppler_page_get_size(pdfpage, &width, &height);
1037 scaled_width = (int) (req->dpi * width/72);
1038 scaled_height = (int) (req->dpi * height/72);
1040 if (ui.poppler_force_cairo) { // poppler -> cairo -> pixmap -> pixbuf
1041 pixmap = gdk_pixmap_new(GTK_WIDGET(canvas)->window, scaled_width, scaled_height, -1);
1042 cr = gdk_cairo_create(pixmap);
1043 cairo_set_source_rgb(cr, 1., 1., 1.);
1045 cairo_scale(cr, scaled_width/width, scaled_height/height);
1046 poppler_page_render(pdfpage, cr);
1048 pixbuf = gdk_pixbuf_get_from_drawable(NULL, GDK_DRAWABLE(pixmap),
1049 NULL, 0, 0, 0, 0, scaled_width, scaled_height);
1050 g_object_unref(pixmap);
1052 else { // directly poppler -> pixbuf: faster, but bitmap font bug
1053 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
1054 FALSE, 8, scaled_width, scaled_height);
1055 wrapper_poppler_page_render_to_pixbuf(
1056 pdfpage, 0, 0, scaled_width, scaled_height,
1057 req->dpi/72, 0, pixbuf);
1059 g_object_unref(pdfpage);
1060 set_cursor_busy(FALSE);
1063 // process the generated pixbuf...
1064 if (pixbuf != NULL) { // success
1065 while (req->pageno > bgpdf.npages) {
1066 bgpg = g_new(struct BgPdfPage, 1);
1067 bgpg->pixbuf = NULL;
1068 bgpdf.pages = g_list_append(bgpdf.pages, bgpg);
1071 bgpg = g_list_nth_data(bgpdf.pages, req->pageno-1);
1072 if (bgpg->pixbuf!=NULL) gdk_pixbuf_unref(bgpg->pixbuf);
1073 bgpg->pixbuf = pixbuf;
1074 bgpg->dpi = req->dpi;
1075 bgpg->pixel_height = scaled_height;
1076 bgpg->pixel_width = scaled_width;
1077 bgpdf_update_bg(req->pageno, bgpg); // update all pages that have this bg
1079 if (!bgpdf.has_failed) {
1080 dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
1081 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Unable to render one or more PDF pages."));
1082 gtk_dialog_run(GTK_DIALOG(dialog));
1083 gtk_widget_destroy(dialog);
1085 bgpdf.has_failed = TRUE;
1088 bgpdf.requests = g_list_delete_link(bgpdf.requests, bgpdf.requests);
1089 if (bgpdf.requests != NULL) return TRUE; // remain in the idle loop
1091 return FALSE; // we're done
1094 /* make a request */
1096 gboolean add_bgpdf_request(int pageno, double zoom)
1098 struct BgPdfRequest *req, *cmp_req;
1101 if (bgpdf.status == STATUS_NOT_INIT)
1102 return FALSE; // don't accept requests
1103 req = g_new(struct BgPdfRequest, 1);
1104 req->pageno = pageno;
1106 // printf("DEBUG: Enqueuing request for page %d at %f dpi\n", pageno, req->dpi);
1108 // cancel any request this may supersede
1109 for (list = bgpdf.requests; list != NULL; ) {
1110 cmp_req = (struct BgPdfRequest *)list->data;
1112 if (cmp_req->pageno == pageno) cancel_bgpdf_request(cmp_req);
1116 bgpdf.requests = g_list_append(bgpdf.requests, req);
1117 if (!bgpdf.pid) bgpdf.pid = g_idle_add(bgpdf_scheduler_callback, NULL);
1121 /* shutdown the PDF reader */
1123 void shutdown_bgpdf(void)
1126 struct BgPdfPage *pdfpg;
1127 struct BgPdfRequest *req;
1129 if (bgpdf.status == STATUS_NOT_INIT) return;
1131 // cancel all requests and free data structures
1132 refstring_unref(bgpdf.filename);
1133 for (list = bgpdf.pages; list != NULL; list = list->next) {
1134 pdfpg = (struct BgPdfPage *)list->data;
1135 if (pdfpg->pixbuf!=NULL) gdk_pixbuf_unref(pdfpg->pixbuf);
1138 g_list_free(bgpdf.pages);
1139 for (list = bgpdf.requests; list != NULL; list = list->next) {
1140 req = (struct BgPdfRequest *)list->data;
1143 g_list_free(bgpdf.requests);
1145 if (bgpdf.file_contents!=NULL) {
1146 g_free(bgpdf.file_contents);
1147 bgpdf.file_contents = NULL;
1149 if (bgpdf.document!=NULL) {
1150 g_object_unref(bgpdf.document);
1151 bgpdf.document = NULL;
1154 bgpdf.status = STATUS_NOT_INIT;
1158 // initialize PDF background rendering
1160 gboolean init_bgpdf(char *pdfname, gboolean create_pages, int file_domain)
1163 struct Background *bg;
1165 PopplerPage *pdfpage;
1166 gdouble width, height;
1169 if (bgpdf.status != STATUS_NOT_INIT) return FALSE;
1171 // make a copy of the file in memory and check it's a PDF
1172 if (!g_file_get_contents(pdfname, &(bgpdf.file_contents), &(bgpdf.file_length), NULL))
1174 if (bgpdf.file_length < 4 || strncmp(bgpdf.file_contents, "%PDF", 4))
1175 { g_free(bgpdf.file_contents); bgpdf.file_contents = NULL; return FALSE; }
1177 // init bgpdf data structures and open poppler document
1178 bgpdf.status = STATUS_READY;
1179 bgpdf.filename = new_refstring((file_domain == DOMAIN_ATTACH) ? "bg.pdf" : pdfname);
1180 bgpdf.file_domain = file_domain;
1183 bgpdf.requests = NULL;
1185 bgpdf.has_failed = FALSE;
1187 /* poppler_document_new_from_data() starts at 0.6.1, but we want to
1188 be compatible with poppler 0.5.4 = latest in CentOS as of sept 2009 */
1189 uri = g_filename_to_uri(pdfname, NULL, NULL);
1190 if (!uri) uri = g_strdup_printf("file://%s", pdfname);
1191 bgpdf.document = poppler_document_new_from_file(uri, NULL, NULL);
1193 /* with poppler 0.6.1 or later, can replace the above 4 lines by:
1194 bgpdf.document = poppler_document_new_from_data(bgpdf.file_contents,
1195 bgpdf.file_length, NULL, NULL);
1197 if (bgpdf.document == NULL) { shutdown_bgpdf(); return FALSE; }
1199 if (pdfname[0]=='/' && ui.filename == NULL) {
1200 if (ui.default_path!=NULL) g_free(ui.default_path);
1201 ui.default_path = g_path_get_dirname(pdfname);
1204 if (!create_pages) return TRUE; // we're done
1206 // create pages with correct sizes if requested
1207 n_pages = poppler_document_get_n_pages(bgpdf.document);
1208 for (i=1; i<=n_pages; i++) {
1209 pdfpage = poppler_document_get_page(bgpdf.document, i-1);
1210 if (!pdfpage) continue;
1211 if (journal.npages < i) {
1212 bg = g_new(struct Background, 1);
1213 bg->canvas_item = NULL;
1216 pg = (struct Page *)g_list_nth_data(journal.pages, i-1);
1220 bg->filename = refstring_ref(bgpdf.filename);
1221 bg->file_domain = bgpdf.file_domain;
1222 bg->file_page_seq = i;
1224 bg->pixbuf_scale = 0;
1225 poppler_page_get_size(pdfpage, &width, &height);
1226 g_object_unref(pdfpage);
1228 pg = new_page_with_bg(bg, width, height);
1229 journal.pages = g_list_append(journal.pages, pg);
1233 pg->height = height;
1234 make_page_clipbox(pg);
1235 update_canvas_bg(pg);
1238 update_page_stuff();
1239 rescale_bg_pixmaps(); // this actually requests the pages !!
1244 // look for all journal pages with given pdf bg, and update their bg pixmaps
1245 void bgpdf_update_bg(int pageno, struct BgPdfPage *bgpg)
1250 for (list = journal.pages; list!= NULL; list = list->next) {
1251 pg = (struct Page *)list->data;
1252 if (pg->bg->type == BG_PDF && pg->bg->file_page_seq == pageno) {
1253 if (pg->bg->pixbuf!=NULL) gdk_pixbuf_unref(pg->bg->pixbuf);
1254 pg->bg->pixbuf = gdk_pixbuf_ref(bgpg->pixbuf);
1255 pg->bg->pixel_width = bgpg->pixel_width;
1256 pg->bg->pixel_height = bgpg->pixel_height;
1257 update_canvas_bg(pg);
1262 // initialize the recent files list
1272 g_strlcpy(s, "mru0", 5);
1273 for (s[3]='0', i=0; i<MRU_SIZE; s[3]++, i++) {
1274 ui.mrumenu[i] = GET_COMPONENT(s);
1277 f = g_io_channel_new_file(ui.mrufile, "r", NULL);
1278 if (f) status = G_IO_STATUS_NORMAL;
1279 else status = G_IO_STATUS_ERROR;
1281 while (status == G_IO_STATUS_NORMAL && i<MRU_SIZE) {
1283 status = g_io_channel_read_line(f, &str, NULL, &lfptr, NULL);
1284 if (status == G_IO_STATUS_NORMAL && lfptr>0) {
1291 g_io_channel_shutdown(f, FALSE, NULL);
1292 g_io_channel_unref(f);
1297 void update_mru_menu(void)
1300 gboolean anyone = FALSE;
1303 for (i=0; i<MRU_SIZE; i++) {
1304 if (ui.mru[i]!=NULL) {
1305 tmp = g_strdup_printf("_%d %s", i+1,
1306 g_strjoinv("__", g_strsplit_set(g_basename(ui.mru[i]),"_",-1)));
1307 gtk_label_set_text_with_mnemonic(GTK_LABEL(gtk_bin_get_child(GTK_BIN(ui.mrumenu[i]))),
1310 gtk_widget_show(ui.mrumenu[i]);
1313 else gtk_widget_hide(ui.mrumenu[i]);
1315 gtk_widget_set_sensitive(GET_COMPONENT("fileRecentFiles"), anyone);
1318 void new_mru_entry(char *name)
1322 for (i=0;i<MRU_SIZE;i++)
1323 if (ui.mru[i]!=NULL && !strcmp(ui.mru[i], name)) {
1325 for (j=i+1; j<MRU_SIZE; j++) ui.mru[j-1] = ui.mru[j];
1326 ui.mru[MRU_SIZE-1]=NULL;
1328 if (ui.mru[MRU_SIZE-1]!=NULL) g_free(ui.mru[MRU_SIZE-1]);
1329 for (j=MRU_SIZE-1; j>=1; j--) ui.mru[j] = ui.mru[j-1];
1330 ui.mru[0] = g_strdup(name);
1334 void delete_mru_entry(int which)
1338 if (ui.mru[which]!=NULL) g_free(ui.mru[which]);
1339 for (i=which+1;i<MRU_SIZE;i++)
1340 ui.mru[i-1] = ui.mru[i];
1341 ui.mru[MRU_SIZE-1] = NULL;
1345 void save_mru_list(void)
1350 f = fopen(ui.mrufile, "w");
1351 if (f==NULL) return;
1352 for (i=0; i<MRU_SIZE; i++)
1353 if (ui.mru[i]!=NULL) fprintf(f, "%s\n", ui.mru[i]);
1357 void init_config_default(void)
1361 DEFAULT_ZOOM = DISPLAY_DPI_DEFAULT/72.0;
1362 ui.zoom = ui.startup_zoom = 1.0*DEFAULT_ZOOM;
1363 ui.default_page.height = 792.0;
1364 ui.default_page.width = 612.0;
1365 ui.default_page.bg->type = BG_SOLID;
1366 ui.default_page.bg->color_no = COLOR_WHITE;
1367 ui.default_page.bg->color_rgba = predef_bgcolors_rgba[COLOR_WHITE];
1368 ui.default_page.bg->ruling = RULING_LINED;
1369 ui.view_continuous = TRUE;
1370 ui.allow_xinput = TRUE;
1371 ui.discard_corepointer = FALSE;
1372 ui.left_handed = FALSE;
1373 ui.shorten_menus = FALSE;
1374 ui.shorten_menu_items = g_strdup(DEFAULT_SHORTEN_MENUS);
1375 ui.auto_save_prefs = FALSE;
1376 ui.bg_apply_all_pages = FALSE;
1377 ui.use_erasertip = FALSE;
1378 ui.window_default_width = 720;
1379 ui.window_default_height = 480;
1380 ui.maximize_at_start = FALSE;
1381 ui.fullscreen = FALSE;
1382 ui.scrollbar_step_increment = 30;
1383 ui.zoom_step_increment = 1;
1384 ui.zoom_step_factor = 1.5;
1385 ui.progressive_bg = TRUE;
1386 ui.print_ruling = TRUE;
1387 ui.default_unit = UNIT_CM;
1388 ui.default_path = NULL;
1389 ui.default_font_name = g_strdup(DEFAULT_FONT);
1390 ui.default_font_size = DEFAULT_FONT_SIZE;
1391 ui.pressure_sensitivity = FALSE;
1392 ui.width_minimum_multiplier = 0.0;
1393 ui.width_maximum_multiplier = 1.25;
1394 ui.button_switch_mapping = FALSE;
1395 ui.autoload_pdf_xoj = FALSE;
1396 ui.poppler_force_cairo = FALSE;
1398 // the default UI vertical order
1399 ui.vertical_order[0][0] = 1;
1400 ui.vertical_order[0][1] = 2;
1401 ui.vertical_order[0][2] = 3;
1402 ui.vertical_order[0][3] = 0;
1403 ui.vertical_order[0][4] = 4;
1404 ui.vertical_order[1][0] = 2;
1405 ui.vertical_order[1][1] = 3;
1406 ui.vertical_order[1][2] = 0;
1407 ui.vertical_order[1][3] = ui.vertical_order[1][4] = -1;
1409 ui.toolno[0] = ui.startuptool = TOOL_PEN;
1410 for (i=1; i<=NUM_BUTTONS; i++) {
1411 ui.toolno[i] = TOOL_ERASER;
1413 for (i=0; i<=NUM_BUTTONS; i++)
1414 ui.linked_brush[i] = BRUSH_LINKED;
1415 ui.brushes[0][TOOL_PEN].color_no = COLOR_BLACK;
1416 ui.brushes[0][TOOL_ERASER].color_no = COLOR_WHITE;
1417 ui.brushes[0][TOOL_HIGHLIGHTER].color_no = COLOR_YELLOW;
1418 for (i=0; i < NUM_STROKE_TOOLS; i++) {
1419 ui.brushes[0][i].thickness_no = THICKNESS_MEDIUM;
1420 ui.brushes[0][i].tool_options = 0;
1421 ui.brushes[0][i].ruler = FALSE;
1422 ui.brushes[0][i].recognizer = FALSE;
1423 ui.brushes[0][i].variable_width = FALSE;
1425 for (i=0; i< NUM_STROKE_TOOLS; i++)
1426 for (j=1; j<=NUM_BUTTONS; j++)
1427 g_memmove(&(ui.brushes[j][i]), &(ui.brushes[0][i]), sizeof(struct Brush));
1429 // predef_thickness is already initialized as a global variable
1430 GS_BITMAP_DPI = 144;
1431 PDFTOPPM_PRINTING_DPI = 150;
1433 ui.hiliter_opacity = 0.5;
1435 #if GTK_CHECK_VERSION(2,10,0)
1436 ui.print_settings = NULL;
1441 #if GLIB_CHECK_VERSION(2,6,0)
1443 void update_keyval(const gchar *group_name, const gchar *key,
1444 const gchar *comment, gchar *value)
1446 gboolean has_it = g_key_file_has_key(ui.config_data, group_name, key, NULL);
1447 cleanup_numeric(value);
1448 g_key_file_set_value(ui.config_data, group_name, key, value);
1450 if (!has_it) g_key_file_set_comment(ui.config_data, group_name, key, comment, NULL);
1455 const char *vorder_usernames[VBOX_MAIN_NITEMS+1] =
1456 {"drawarea", "menu", "main_toolbar", "pen_toolbar", "statusbar", NULL};
1458 gchar *verbose_vertical_order(int *order)
1460 gchar buf[80], *p; // longer than needed
1464 for (i=0; i<VBOX_MAIN_NITEMS; i++) {
1465 if (order[i]<0 || order[i]>=VBOX_MAIN_NITEMS) continue;
1466 if (p!=buf) *(p++) = ' ';
1467 p = g_stpcpy(p, vorder_usernames[order[i]]);
1469 return g_strdup(buf);
1472 void save_config_to_file(void)
1477 #if GLIB_CHECK_VERSION(2,6,0)
1478 // no support for keyval files before Glib 2.6.0
1479 if (glib_minor_version<6) return;
1481 // save some data...
1482 ui.maximize_at_start = (gdk_window_get_state(winMain->window) & GDK_WINDOW_STATE_MAXIMIZED);
1483 if (!ui.maximize_at_start && !ui.fullscreen)
1484 gdk_drawable_get_size(winMain->window,
1485 &ui.window_default_width, &ui.window_default_height);
1487 update_keyval("general", "display_dpi",
1488 _(" the display resolution, in pixels per inch"),
1489 g_strdup_printf("%.2f", DEFAULT_ZOOM*72));
1490 update_keyval("general", "initial_zoom",
1491 _(" the initial zoom level, in percent"),
1492 g_strdup_printf("%.2f", 100*ui.zoom/DEFAULT_ZOOM));
1493 update_keyval("general", "window_maximize",
1494 _(" maximize the window at startup (true/false)"),
1495 g_strdup(ui.maximize_at_start?"true":"false"));
1496 update_keyval("general", "window_fullscreen",
1497 _(" start in full screen mode (true/false)"),
1498 g_strdup(ui.fullscreen?"true":"false"));
1499 update_keyval("general", "window_width",
1500 _(" the window width in pixels (when not maximized)"),
1501 g_strdup_printf("%d", ui.window_default_width));
1502 update_keyval("general", "window_height",
1503 _(" the window height in pixels"),
1504 g_strdup_printf("%d", ui.window_default_height));
1505 update_keyval("general", "scrollbar_speed",
1506 _(" scrollbar step increment (in pixels)"),
1507 g_strdup_printf("%d", ui.scrollbar_step_increment));
1508 update_keyval("general", "zoom_dialog_increment",
1509 _(" the step increment in the zoom dialog box"),
1510 g_strdup_printf("%d", ui.zoom_step_increment));
1511 update_keyval("general", "zoom_step_factor",
1512 _(" the multiplicative factor for zoom in/out"),
1513 g_strdup_printf("%.3f", ui.zoom_step_factor));
1514 update_keyval("general", "view_continuous",
1515 _(" document view (true = continuous, false = single page)"),
1516 g_strdup(ui.view_continuous?"true":"false"));
1517 update_keyval("general", "use_xinput",
1518 _(" use XInput extensions (true/false)"),
1519 g_strdup(ui.allow_xinput?"true":"false"));
1520 update_keyval("general", "discard_corepointer",
1521 _(" discard Core Pointer events in XInput mode (true/false)"),
1522 g_strdup(ui.discard_corepointer?"true":"false"));
1523 update_keyval("general", "use_erasertip",
1524 _(" always map eraser tip to eraser (true/false)"),
1525 g_strdup(ui.use_erasertip?"true":"false"));
1526 update_keyval("general", "buttons_switch_mappings",
1527 _(" buttons 2 and 3 switch mappings instead of drawing (useful for some tablets) (true/false)"),
1528 g_strdup(ui.button_switch_mapping?"true":"false"));
1529 update_keyval("general", "autoload_pdf_xoj",
1530 _(" automatically load filename.pdf.xoj instead of filename.pdf (true/false)"),
1531 g_strdup(ui.autoload_pdf_xoj?"true":"false"));
1532 update_keyval("general", "default_path",
1533 _(" default path for open/save (leave blank for current directory)"),
1534 g_strdup((ui.default_path!=NULL)?ui.default_path:""));
1535 update_keyval("general", "pressure_sensitivity",
1536 _(" use pressure sensitivity to control pen stroke width (true/false)"),
1537 g_strdup(ui.pressure_sensitivity?"true":"false"));
1538 update_keyval("general", "width_minimum_multiplier",
1539 _(" minimum width multiplier"),
1540 g_strdup_printf("%.2f", ui.width_minimum_multiplier));
1541 update_keyval("general", "width_maximum_multiplier",
1542 _(" maximum width multiplier"),
1543 g_strdup_printf("%.2f", ui.width_maximum_multiplier));
1544 update_keyval("general", "interface_order",
1545 _(" interface components from top to bottom\n valid values: drawarea menu main_toolbar pen_toolbar statusbar"),
1546 verbose_vertical_order(ui.vertical_order[0]));
1547 update_keyval("general", "interface_fullscreen",
1548 _(" interface components in fullscreen mode, from top to bottom"),
1549 verbose_vertical_order(ui.vertical_order[1]));
1550 update_keyval("general", "interface_lefthanded",
1551 _(" interface has left-handed scrollbar (true/false)"),
1552 g_strdup(ui.left_handed?"true":"false"));
1553 update_keyval("general", "shorten_menus",
1554 _(" hide some unwanted menu or toolbar items (true/false)"),
1555 g_strdup(ui.shorten_menus?"true":"false"));
1556 update_keyval("general", "shorten_menu_items",
1557 _(" interface items to hide (customize at your own risk!)\n see source file xo-interface.c for a list of item names"),
1558 g_strdup(ui.shorten_menu_items));
1559 update_keyval("general", "highlighter_opacity",
1560 _(" highlighter opacity (0 to 1, default 0.5)\n warning: opacity level is not saved in xoj files!"),
1561 g_strdup_printf("%.2f", ui.hiliter_opacity));
1562 update_keyval("general", "autosave_prefs",
1563 _(" auto-save preferences on exit (true/false)"),
1564 g_strdup(ui.auto_save_prefs?"true":"false"));
1565 update_keyval("general", "poppler_force_cairo",
1566 _(" force PDF rendering through cairo (slower but nicer) (true/false)"),
1567 g_strdup(ui.poppler_force_cairo?"true":"false"));
1569 update_keyval("paper", "width",
1570 _(" the default page width, in points (1/72 in)"),
1571 g_strdup_printf("%.2f", ui.default_page.width));
1572 update_keyval("paper", "height",
1573 _(" the default page height, in points (1/72 in)"),
1574 g_strdup_printf("%.2f", ui.default_page.height));
1575 update_keyval("paper", "color",
1576 _(" the default paper color"),
1577 (ui.default_page.bg->color_no>=0)?
1578 g_strdup(bgcolor_names[ui.default_page.bg->color_no]):
1579 g_strdup_printf("#%08x", ui.default_page.bg->color_rgba));
1580 update_keyval("paper", "style",
1581 _(" the default paper style (plain, lined, ruled, or graph)"),
1582 g_strdup(bgstyle_names[ui.default_page.bg->ruling]));
1583 update_keyval("paper", "apply_all",
1584 _(" apply paper style changes to all pages (true/false)"),
1585 g_strdup(ui.bg_apply_all_pages?"true":"false"));
1586 update_keyval("paper", "default_unit",
1587 _(" preferred unit (cm, in, px, pt)"),
1588 g_strdup(unit_names[ui.default_unit]));
1589 update_keyval("paper", "print_ruling",
1590 _(" include paper ruling when printing or exporting to PDF (true/false)"),
1591 g_strdup(ui.print_ruling?"true":"false"));
1592 update_keyval("paper", "progressive_bg",
1593 _(" just-in-time update of page backgrounds (true/false)"),
1594 g_strdup(ui.progressive_bg?"true":"false"));
1595 update_keyval("paper", "gs_bitmap_dpi",
1596 _(" bitmap resolution of PS/PDF backgrounds rendered using ghostscript (dpi)"),
1597 g_strdup_printf("%d", GS_BITMAP_DPI));
1598 update_keyval("paper", "pdftoppm_printing_dpi",
1599 _(" bitmap resolution of PDF backgrounds when printing with libgnomeprint (dpi)"),
1600 g_strdup_printf("%d", PDFTOPPM_PRINTING_DPI));
1602 update_keyval("tools", "startup_tool",
1603 _(" selected tool at startup (pen, eraser, highlighter, selectrect, vertspace, hand)"),
1604 g_strdup(tool_names[ui.startuptool]));
1605 update_keyval("tools", "pen_color",
1606 _(" default pen color"),
1607 (ui.default_brushes[TOOL_PEN].color_no>=0)?
1608 g_strdup(color_names[ui.default_brushes[TOOL_PEN].color_no]):
1609 g_strdup_printf("#%08x", ui.default_brushes[TOOL_PEN].color_rgba));
1610 update_keyval("tools", "pen_thickness",
1611 _(" default pen thickness (fine = 1, medium = 2, thick = 3)"),
1612 g_strdup_printf("%d", ui.default_brushes[TOOL_PEN].thickness_no));
1613 update_keyval("tools", "pen_ruler",
1614 _(" default pen is in ruler mode (true/false)"),
1615 g_strdup(ui.default_brushes[TOOL_PEN].ruler?"true":"false"));
1616 update_keyval("tools", "pen_recognizer",
1617 _(" default pen is in shape recognizer mode (true/false)"),
1618 g_strdup(ui.default_brushes[TOOL_PEN].recognizer?"true":"false"));
1619 update_keyval("tools", "eraser_thickness",
1620 _(" default eraser thickness (fine = 1, medium = 2, thick = 3)"),
1621 g_strdup_printf("%d", ui.default_brushes[TOOL_ERASER].thickness_no));
1622 update_keyval("tools", "eraser_mode",
1623 _(" default eraser mode (standard = 0, whiteout = 1, strokes = 2)"),
1624 g_strdup_printf("%d", ui.default_brushes[TOOL_ERASER].tool_options));
1625 update_keyval("tools", "highlighter_color",
1626 _(" default highlighter color"),
1627 (ui.default_brushes[TOOL_HIGHLIGHTER].color_no>=0)?
1628 g_strdup(color_names[ui.default_brushes[TOOL_HIGHLIGHTER].color_no]):
1629 g_strdup_printf("#%08x", ui.default_brushes[TOOL_HIGHLIGHTER].color_rgba));
1630 update_keyval("tools", "highlighter_thickness",
1631 _(" default highlighter thickness (fine = 1, medium = 2, thick = 3)"),
1632 g_strdup_printf("%d", ui.default_brushes[TOOL_HIGHLIGHTER].thickness_no));
1633 update_keyval("tools", "highlighter_ruler",
1634 _(" default highlighter is in ruler mode (true/false)"),
1635 g_strdup(ui.default_brushes[TOOL_HIGHLIGHTER].ruler?"true":"false"));
1636 update_keyval("tools", "highlighter_recognizer",
1637 _(" default highlighter is in shape recognizer mode (true/false)"),
1638 g_strdup(ui.default_brushes[TOOL_HIGHLIGHTER].recognizer?"true":"false"));
1639 update_keyval("tools", "btn2_tool",
1640 _(" button 2 tool (pen, eraser, highlighter, text, selectrect, vertspace, hand)"),
1641 g_strdup(tool_names[ui.toolno[1]]));
1642 update_keyval("tools", "btn2_linked",
1643 _(" button 2 brush linked to primary brush (true/false) (overrides all other settings)"),
1644 g_strdup((ui.linked_brush[1]==BRUSH_LINKED)?"true":"false"));
1645 update_keyval("tools", "btn2_color",
1646 _(" button 2 brush color (for pen or highlighter only)"),
1647 (ui.toolno[1]<NUM_STROKE_TOOLS)?
1648 ((ui.brushes[1][ui.toolno[1]].color_no>=0)?
1649 g_strdup(color_names[ui.brushes[1][ui.toolno[1]].color_no]):
1650 g_strdup_printf("#%08x", ui.brushes[1][ui.toolno[1]].color_rgba)):
1652 update_keyval("tools", "btn2_thickness",
1653 _(" button 2 brush thickness (pen, eraser, or highlighter only)"),
1654 g_strdup_printf("%d", (ui.toolno[1]<NUM_STROKE_TOOLS)?
1655 ui.brushes[1][ui.toolno[1]].thickness_no:0));
1656 update_keyval("tools", "btn2_ruler",
1657 _(" button 2 ruler mode (true/false) (for pen or highlighter only)"),
1658 g_strdup(((ui.toolno[1]<NUM_STROKE_TOOLS)?
1659 ui.brushes[1][ui.toolno[1]].ruler:FALSE)?"true":"false"));
1660 update_keyval("tools", "btn2_recognizer",
1661 _(" button 2 shape recognizer mode (true/false) (pen or highlighter only)"),
1662 g_strdup(((ui.toolno[1]<NUM_STROKE_TOOLS)?
1663 ui.brushes[1][ui.toolno[1]].recognizer:FALSE)?"true":"false"));
1664 update_keyval("tools", "btn2_erasermode",
1665 _(" button 2 eraser mode (eraser only)"),
1666 g_strdup_printf("%d", ui.brushes[1][TOOL_ERASER].tool_options));
1667 update_keyval("tools", "btn3_tool",
1668 _(" button 3 tool (pen, eraser, highlighter, text, selectrect, vertspace, hand)"),
1669 g_strdup(tool_names[ui.toolno[2]]));
1670 update_keyval("tools", "btn3_linked",
1671 _(" button 3 brush linked to primary brush (true/false) (overrides all other settings)"),
1672 g_strdup((ui.linked_brush[2]==BRUSH_LINKED)?"true":"false"));
1673 update_keyval("tools", "btn3_color",
1674 _(" button 3 brush color (for pen or highlighter only)"),
1675 (ui.toolno[2]<NUM_STROKE_TOOLS)?
1676 ((ui.brushes[2][ui.toolno[2]].color_no>=0)?
1677 g_strdup(color_names[ui.brushes[2][ui.toolno[2]].color_no]):
1678 g_strdup_printf("#%08x", ui.brushes[2][ui.toolno[2]].color_rgba)):
1680 update_keyval("tools", "btn3_thickness",
1681 _(" button 3 brush thickness (pen, eraser, or highlighter only)"),
1682 g_strdup_printf("%d", (ui.toolno[2]<NUM_STROKE_TOOLS)?
1683 ui.brushes[2][ui.toolno[2]].thickness_no:0));
1684 update_keyval("tools", "btn3_ruler",
1685 _(" button 3 ruler mode (true/false) (for pen or highlighter only)"),
1686 g_strdup(((ui.toolno[2]<NUM_STROKE_TOOLS)?
1687 ui.brushes[2][ui.toolno[2]].ruler:FALSE)?"true":"false"));
1688 update_keyval("tools", "btn3_recognizer",
1689 _(" button 3 shape recognizer mode (true/false) (pen or highlighter only)"),
1690 g_strdup(((ui.toolno[2]<NUM_STROKE_TOOLS)?
1691 ui.brushes[2][ui.toolno[2]].recognizer:FALSE)?"true":"false"));
1692 update_keyval("tools", "btn3_erasermode",
1693 _(" button 3 eraser mode (eraser only)"),
1694 g_strdup_printf("%d", ui.brushes[2][TOOL_ERASER].tool_options));
1696 update_keyval("tools", "pen_thicknesses",
1697 _(" thickness of the various pens (in points, 1 pt = 1/72 in)"),
1698 g_strdup_printf("%.2f;%.2f;%.2f;%.2f;%.2f",
1699 predef_thickness[TOOL_PEN][0], predef_thickness[TOOL_PEN][1],
1700 predef_thickness[TOOL_PEN][2], predef_thickness[TOOL_PEN][3],
1701 predef_thickness[TOOL_PEN][4]));
1702 update_keyval("tools", "eraser_thicknesses",
1703 _(" thickness of the various erasers (in points, 1 pt = 1/72 in)"),
1704 g_strdup_printf("%.2f;%.2f;%.2f",
1705 predef_thickness[TOOL_ERASER][1], predef_thickness[TOOL_ERASER][2],
1706 predef_thickness[TOOL_ERASER][3]));
1707 update_keyval("tools", "highlighter_thicknesses",
1708 _(" thickness of the various highlighters (in points, 1 pt = 1/72 in)"),
1709 g_strdup_printf("%.2f;%.2f;%.2f",
1710 predef_thickness[TOOL_HIGHLIGHTER][1], predef_thickness[TOOL_HIGHLIGHTER][2],
1711 predef_thickness[TOOL_HIGHLIGHTER][3]));
1712 update_keyval("tools", "default_font",
1713 _(" name of the default font"),
1714 g_strdup(ui.default_font_name));
1715 update_keyval("tools", "default_font_size",
1716 _(" default font size"),
1717 g_strdup_printf("%.1f", ui.default_font_size));
1719 buf = g_key_file_to_data(ui.config_data, NULL, NULL);
1720 if (buf == NULL) return;
1721 f = fopen(ui.configfile, "w");
1722 if (f==NULL) { g_free(buf); return; }
1729 #if GLIB_CHECK_VERSION(2,6,0)
1730 gboolean parse_keyval_float(const gchar *group, const gchar *key, double *val, double inf, double sup)
1735 ret = g_key_file_get_value(ui.config_data, group, key, NULL);
1736 if (ret==NULL) return FALSE;
1737 conv = g_ascii_strtod(ret, &end);
1738 if (*end!=0) { g_free(ret); return FALSE; }
1740 if (conv < inf || conv > sup) return FALSE;
1745 gboolean parse_keyval_floatlist(const gchar *group, const gchar *key, double *val, int n, double inf, double sup)
1751 if (n>5) return FALSE;
1752 ret = g_key_file_get_value(ui.config_data, group, key, NULL);
1753 if (ret==NULL) return FALSE;
1755 for (i=0; i<n; i++) {
1756 conv[i] = g_ascii_strtod(end, &end);
1757 if ((i==n-1 && *end!=0) || (i<n-1 && *end!=';') ||
1758 (conv[i] < inf) || (conv[i] > sup)) { g_free(ret); return FALSE; }
1762 for (i=0; i<n; i++) val[i] = conv[i];
1766 gboolean parse_keyval_int(const gchar *group, const gchar *key, int *val, int inf, int sup)
1771 ret = g_key_file_get_value(ui.config_data, group, key, NULL);
1772 if (ret==NULL) return FALSE;
1773 conv = strtol(ret, &end, 10);
1774 if (*end!=0) { g_free(ret); return FALSE; }
1776 if (conv < inf || conv > sup) return FALSE;
1781 gboolean parse_keyval_enum(const gchar *group, const gchar *key, int *val, const char **names, int n)
1786 ret = g_key_file_get_value(ui.config_data, group, key, NULL);
1787 if (ret==NULL) return FALSE;
1788 for (i=0; i<n; i++) {
1789 if (!names[i][0]) continue; // "" is for invalid values
1790 if (!g_ascii_strcasecmp(ret, names[i]))
1791 { *val = i; g_free(ret); return TRUE; }
1796 gboolean parse_keyval_enum_color(const gchar *group, const gchar *key, int *val, guint *val_rgba,
1797 const char **names, const guint *predef_rgba, int n)
1802 ret = g_key_file_get_value(ui.config_data, group, key, NULL);
1803 if (ret==NULL) return FALSE;
1804 for (i=0; i<n; i++) {
1805 if (!names[i][0]) continue; // "" is for invalid values
1806 if (!g_ascii_strcasecmp(ret, names[i]))
1807 { *val = i; *val_rgba = predef_rgba[i]; g_free(ret); return TRUE; }
1811 *val_rgba = strtoul(ret+1, NULL, 16);
1818 gboolean parse_keyval_boolean(const gchar *group, const gchar *key, gboolean *val)
1822 ret = g_key_file_get_value(ui.config_data, group, key, NULL);
1823 if (ret==NULL) return FALSE;
1824 if (!g_ascii_strcasecmp(ret, "true"))
1825 { *val = TRUE; g_free(ret); return TRUE; }
1826 if (!g_ascii_strcasecmp(ret, "false"))
1827 { *val = FALSE; g_free(ret); return TRUE; }
1832 gboolean parse_keyval_string(const gchar *group, const gchar *key, gchar **val)
1836 ret = g_key_file_get_value(ui.config_data, group, key, NULL);
1837 if (ret==NULL) return FALSE;
1838 if (strlen(ret) == 0) {
1846 gboolean parse_keyval_vorderlist(const gchar *group, const gchar *key, int *order)
1849 int tmp[VBOX_MAIN_NITEMS];
1852 ret = g_key_file_get_value(ui.config_data, group, key, NULL);
1853 if (ret==NULL) return FALSE;
1855 for (i=0; i<VBOX_MAIN_NITEMS; i++) tmp[i] = -1;
1857 while (*p==' ') p++;
1859 if (n>VBOX_MAIN_NITEMS) return FALSE; // too many items
1860 for (i=0; i<VBOX_MAIN_NITEMS; i++) {
1861 if (!g_str_has_prefix(p, vorder_usernames[i])) continue;
1862 l = strlen(vorder_usernames[i]);
1863 if (p[l]==' '||p[l]==0) { p+=l; break; }
1865 if (i>=VBOX_MAIN_NITEMS) { g_free(ret); return FALSE; } // parse error
1868 while (*p==' ') p++;
1871 for (n=0; n<VBOX_MAIN_NITEMS; n++) order[n] = tmp[n];
1878 void load_config_from_file(void)
1885 #if GLIB_CHECK_VERSION(2,6,0)
1886 // no support for keyval files before Glib 2.6.0
1887 if (glib_minor_version<6) return;
1888 ui.config_data = g_key_file_new();
1889 if (!g_key_file_load_from_file(ui.config_data, ui.configfile,
1890 G_KEY_FILE_KEEP_COMMENTS, NULL)) {
1891 g_key_file_free(ui.config_data);
1892 ui.config_data = g_key_file_new();
1893 g_key_file_set_comment(ui.config_data, NULL, NULL,
1894 _(" Xournal configuration file.\n"
1895 " This file is generated automatically upon saving preferences.\n"
1896 " Use caution when editing this file manually.\n"), NULL);
1900 // parse keys from the keyfile to set defaults
1901 if (parse_keyval_float("general", "display_dpi", &f, 10., 500.))
1902 DEFAULT_ZOOM = f/72.0;
1903 if (parse_keyval_float("general", "initial_zoom", &f,
1904 MIN_ZOOM*100/DEFAULT_ZOOM, MAX_ZOOM*100/DEFAULT_ZOOM))
1905 ui.zoom = ui.startup_zoom = DEFAULT_ZOOM*f/100.0;
1906 parse_keyval_boolean("general", "window_maximize", &ui.maximize_at_start);
1907 parse_keyval_boolean("general", "window_fullscreen", &ui.fullscreen);
1908 parse_keyval_int("general", "window_width", &ui.window_default_width, 10, 5000);
1909 parse_keyval_int("general", "window_height", &ui.window_default_height, 10, 5000);
1910 parse_keyval_int("general", "scrollbar_speed", &ui.scrollbar_step_increment, 1, 5000);
1911 parse_keyval_int("general", "zoom_dialog_increment", &ui.zoom_step_increment, 1, 500);
1912 parse_keyval_float("general", "zoom_step_factor", &ui.zoom_step_factor, 1., 5.);
1913 parse_keyval_boolean("general", "view_continuous", &ui.view_continuous);
1914 parse_keyval_boolean("general", "use_xinput", &ui.allow_xinput);
1915 parse_keyval_boolean("general", "discard_corepointer", &ui.discard_corepointer);
1916 parse_keyval_boolean("general", "use_erasertip", &ui.use_erasertip);
1917 parse_keyval_boolean("general", "buttons_switch_mappings", &ui.button_switch_mapping);
1918 parse_keyval_boolean("general", "autoload_pdf_xoj", &ui.autoload_pdf_xoj);
1919 parse_keyval_string("general", "default_path", &ui.default_path);
1920 parse_keyval_boolean("general", "pressure_sensitivity", &ui.pressure_sensitivity);
1921 parse_keyval_float("general", "width_minimum_multiplier", &ui.width_minimum_multiplier, 0., 10.);
1922 parse_keyval_float("general", "width_maximum_multiplier", &ui.width_maximum_multiplier, 0., 10.);
1924 parse_keyval_vorderlist("general", "interface_order", ui.vertical_order[0]);
1925 parse_keyval_vorderlist("general", "interface_fullscreen", ui.vertical_order[1]);
1926 parse_keyval_boolean("general", "interface_lefthanded", &ui.left_handed);
1927 parse_keyval_boolean("general", "shorten_menus", &ui.shorten_menus);
1928 if (parse_keyval_string("general", "shorten_menu_items", &str))
1929 if (str!=NULL) { g_free(ui.shorten_menu_items); ui.shorten_menu_items = str; }
1930 parse_keyval_float("general", "highlighter_opacity", &ui.hiliter_opacity, 0., 1.);
1931 parse_keyval_boolean("general", "autosave_prefs", &ui.auto_save_prefs);
1932 parse_keyval_boolean("general", "poppler_force_cairo", &ui.poppler_force_cairo);
1934 parse_keyval_float("paper", "width", &ui.default_page.width, 1., 5000.);
1935 parse_keyval_float("paper", "height", &ui.default_page.height, 1., 5000.);
1936 parse_keyval_enum_color("paper", "color",
1937 &(ui.default_page.bg->color_no), &(ui.default_page.bg->color_rgba),
1938 bgcolor_names, predef_bgcolors_rgba, COLOR_MAX);
1939 parse_keyval_enum("paper", "style", &(ui.default_page.bg->ruling), bgstyle_names, 4);
1940 parse_keyval_boolean("paper", "apply_all", &ui.bg_apply_all_pages);
1941 parse_keyval_enum("paper", "default_unit", &ui.default_unit, unit_names, 4);
1942 parse_keyval_boolean("paper", "progressive_bg", &ui.progressive_bg);
1943 parse_keyval_boolean("paper", "print_ruling", &ui.print_ruling);
1944 parse_keyval_int("paper", "gs_bitmap_dpi", &GS_BITMAP_DPI, 1, 1200);
1945 parse_keyval_int("paper", "pdftoppm_printing_dpi", &PDFTOPPM_PRINTING_DPI, 1, 1200);
1947 parse_keyval_enum("tools", "startup_tool", &ui.startuptool, tool_names, NUM_TOOLS);
1948 ui.toolno[0] = ui.startuptool;
1949 parse_keyval_enum_color("tools", "pen_color",
1950 &(ui.brushes[0][TOOL_PEN].color_no), &(ui.brushes[0][TOOL_PEN].color_rgba),
1951 color_names, predef_colors_rgba, COLOR_MAX);
1952 parse_keyval_int("tools", "pen_thickness", &(ui.brushes[0][TOOL_PEN].thickness_no), 0, 4);
1953 parse_keyval_boolean("tools", "pen_ruler", &(ui.brushes[0][TOOL_PEN].ruler));
1954 parse_keyval_boolean("tools", "pen_recognizer", &(ui.brushes[0][TOOL_PEN].recognizer));
1955 parse_keyval_int("tools", "eraser_thickness", &(ui.brushes[0][TOOL_ERASER].thickness_no), 1, 3);
1956 parse_keyval_int("tools", "eraser_mode", &(ui.brushes[0][TOOL_ERASER].tool_options), 0, 2);
1957 parse_keyval_enum_color("tools", "highlighter_color",
1958 &(ui.brushes[0][TOOL_HIGHLIGHTER].color_no), &(ui.brushes[0][TOOL_HIGHLIGHTER].color_rgba),
1959 color_names, predef_colors_rgba, COLOR_MAX);
1960 parse_keyval_int("tools", "highlighter_thickness", &(ui.brushes[0][TOOL_HIGHLIGHTER].thickness_no), 0, 4);
1961 parse_keyval_boolean("tools", "highlighter_ruler", &(ui.brushes[0][TOOL_HIGHLIGHTER].ruler));
1962 parse_keyval_boolean("tools", "highlighter_recognizer", &(ui.brushes[0][TOOL_HIGHLIGHTER].recognizer));
1963 ui.brushes[0][TOOL_PEN].variable_width = ui.pressure_sensitivity;
1964 for (i=0; i< NUM_STROKE_TOOLS; i++)
1965 for (j=1; j<=NUM_BUTTONS; j++)
1966 g_memmove(&(ui.brushes[j][i]), &(ui.brushes[0][i]), sizeof(struct Brush));
1968 parse_keyval_enum("tools", "btn2_tool", &(ui.toolno[1]), tool_names, NUM_TOOLS);
1969 if (parse_keyval_boolean("tools", "btn2_linked", &b))
1970 ui.linked_brush[1] = b?BRUSH_LINKED:BRUSH_STATIC;
1971 parse_keyval_enum("tools", "btn3_tool", &(ui.toolno[2]), tool_names, NUM_TOOLS);
1972 if (parse_keyval_boolean("tools", "btn3_linked", &b))
1973 ui.linked_brush[2] = b?BRUSH_LINKED:BRUSH_STATIC;
1974 if (ui.linked_brush[1]!=BRUSH_LINKED) {
1975 if (ui.toolno[1]==TOOL_PEN || ui.toolno[1]==TOOL_HIGHLIGHTER) {
1976 parse_keyval_boolean("tools", "btn2_ruler", &(ui.brushes[1][ui.toolno[1]].ruler));
1977 parse_keyval_boolean("tools", "btn2_recognizer", &(ui.brushes[1][ui.toolno[1]].recognizer));
1978 parse_keyval_enum_color("tools", "btn2_color",
1979 &(ui.brushes[1][ui.toolno[1]].color_no), &(ui.brushes[1][ui.toolno[1]].color_rgba),
1980 color_names, predef_colors_rgba, COLOR_MAX);
1982 if (ui.toolno[1]<NUM_STROKE_TOOLS)
1983 parse_keyval_int("tools", "btn2_thickness", &(ui.brushes[1][ui.toolno[1]].thickness_no), 0, 4);
1984 if (ui.toolno[1]==TOOL_ERASER)
1985 parse_keyval_int("tools", "btn2_erasermode", &(ui.brushes[1][TOOL_ERASER].tool_options), 0, 2);
1987 if (ui.linked_brush[2]!=BRUSH_LINKED) {
1988 if (ui.toolno[2]==TOOL_PEN || ui.toolno[2]==TOOL_HIGHLIGHTER) {
1989 parse_keyval_boolean("tools", "btn3_ruler", &(ui.brushes[2][ui.toolno[2]].ruler));
1990 parse_keyval_boolean("tools", "btn3_recognizer", &(ui.brushes[2][ui.toolno[2]].recognizer));
1991 parse_keyval_enum_color("tools", "btn3_color",
1992 &(ui.brushes[2][ui.toolno[2]].color_no), &(ui.brushes[2][ui.toolno[2]].color_rgba),
1993 color_names, predef_colors_rgba, COLOR_MAX);
1995 if (ui.toolno[2]<NUM_STROKE_TOOLS)
1996 parse_keyval_int("tools", "btn3_thickness", &(ui.brushes[2][ui.toolno[2]].thickness_no), 0, 4);
1997 if (ui.toolno[2]==TOOL_ERASER)
1998 parse_keyval_int("tools", "btn3_erasermode", &(ui.brushes[2][TOOL_ERASER].tool_options), 0, 2);
2000 parse_keyval_floatlist("tools", "pen_thicknesses", predef_thickness[TOOL_PEN], 5, 0.01, 1000.0);
2001 parse_keyval_floatlist("tools", "eraser_thicknesses", predef_thickness[TOOL_ERASER]+1, 3, 0.01, 1000.0);
2002 parse_keyval_floatlist("tools", "highlighter_thicknesses", predef_thickness[TOOL_HIGHLIGHTER]+1, 3, 0.01, 1000.0);
2003 if (parse_keyval_string("tools", "default_font", &str))
2004 if (str!=NULL) { g_free(ui.default_font_name); ui.default_font_name = str; }
2005 parse_keyval_float("tools", "default_font_size", &ui.default_font_size, 1., 200.);