]> git.donarmstrong.com Git - xournal.git/blobdiff - src/xo-file.c
Image patch
[xournal.git] / src / xo-file.c
index 2b3f361df7e7cac08cf89dc0a83e7cc4c49144b5..129ba87873f8cf920491278c554178bfb9cccbf9 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This software is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 #include <libgnomecanvas/libgnomecanvas.h>
 #include <zlib.h>
 #include <math.h>
-#include <gdk/gdkx.h>
-#include <X11/Xlib.h>
 #include <locale.h>
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <poppler/glib/poppler.h>
 
+#ifndef WIN32
+ #include <gdk/gdkx.h>
+ #include <X11/Xlib.h>
+#endif
+
 #include "xournal.h"
 #include "xo-interface.h"
 #include "xo-support.h"
@@ -25,8 +43,9 @@
 #include "xo-misc.h"
 #include "xo-file.h"
 #include "xo-paint.h"
+#include "xo-image.h"
 
-const char *tool_names[NUM_TOOLS] = {"pen", "eraser", "highlighter", "text", "", "selectrect", "vertspace", "hand"};
+const char *tool_names[NUM_TOOLS] = {"pen", "eraser", "highlighter", "text", "", "selectrect", "vertspace", "hand", "image"};
 const char *color_names[COLOR_MAX] = {"black", "blue", "red", "green",
    "gray", "lightblue", "lightgreen", "magenta", "orange", "yellow", "white"};
 const char *bgtype_names[3] = {"solid", "pixmap", "pdf"};
@@ -68,6 +87,47 @@ void chk_attach_names(void)
   }
 }
 
+/* Write image to file: returns true on success, false on error.
+   The image is written as a base64 encoded PNG. */
+
+gboolean write_image(gzFile f, Item *item)
+{
+  gchar *base64_str;
+
+  if (item->image_png == NULL) {
+    if (!gdk_pixbuf_save_to_buffer(item->image, &item->image_png, &item->image_png_len, "png", NULL, NULL)) {
+      item->image_png_len = 0;       // failed for some reason, so forget it
+      return FALSE;
+    }
+  }
+
+  base64_str = g_base64_encode(item->image_png, item->image_png_len);
+  gzputs(f, base64_str);
+  g_free(base64_str);
+  return TRUE;
+}
+
+// create pixbuf from base64 encoded PNG, or return NULL on failure
+
+GdkPixbuf *read_pixbuf(const gchar *base64_str, gsize base64_strlen)
+{
+  gchar *base64_str2;
+  gchar *png_buf;
+  gsize png_buflen;
+  GdkPixbuf *pixbuf;
+
+  // We have to copy the string in order to null terminate it, sigh.
+  base64_str2 = g_memdup(base64_str, base64_strlen+1);
+  base64_str2[base64_strlen] = 0;
+  png_buf = g_base64_decode(base64_str2, &png_buflen);
+
+  pixbuf = pixbuf_from_buffer(png_buf, png_buflen);
+
+  g_free(png_buf);
+  g_free(base64_str2);
+  return pixbuf;
+}
+
 // saves the journal to a file: returns true on success, false on error
 
 gboolean save_journal(const char *filename)
@@ -83,7 +143,7 @@ gboolean save_journal(const char *filename)
   GList *pagelist, *layerlist, *itemlist, *list;
   GtkWidget *dialog;
   
-  f = gzopen(filename, "w");
+  f = gzopen(filename, "wb");
   if (f==NULL) return FALSE;
   chk_attach_names();
 
@@ -119,7 +179,7 @@ gboolean save_journal(const char *filename)
           if (!gdk_pixbuf_save(pg->bg->pixbuf, tmpfn, "png", NULL, NULL)) {
             dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
               GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, 
-              "Could not write background '%s'. Continuing anyway.", tmpfn);
+              _("Could not write background '%s'. Continuing anyway."), tmpfn);
             gtk_dialog_run(GTK_DIALOG(dialog));
             gtk_widget_destroy(dialog);
           }
@@ -143,7 +203,7 @@ gboolean save_journal(const char *filename)
           success = FALSE;
           if (bgpdf.status != STATUS_NOT_INIT && bgpdf.file_contents != NULL)
           {
-            tmpf = fopen(tmpfn, "w");
+            tmpf = fopen(tmpfn, "wb");
             if (tmpf != NULL && fwrite(bgpdf.file_contents, 1, bgpdf.file_length, tmpf) == bgpdf.file_length)
               success = TRUE;
             fclose(tmpf);
@@ -151,7 +211,7 @@ gboolean save_journal(const char *filename)
           if (!success) {
             dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
               GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, 
-              "Could not write background '%s'. Continuing anyway.", tmpfn);
+              _("Could not write background '%s'. Continuing anyway."), tmpfn);
             gtk_dialog_run(GTK_DIALOG(dialog));
             gtk_widget_destroy(dialog);
           }
@@ -196,9 +256,17 @@ gboolean save_journal(const char *filename)
           else
             gzprintf(f, "#%08x", item->brush.color_rgba);
           tmpstr = g_markup_escape_text(item->text, -1);
-          gzprintf(f, "\">%s</text>\n", tmpstr);
+          gzputs(f, "\">");
+          gzputs(f, tmpstr); // gzprintf() can't handle > 4095 bytes
+          gzputs(f, "</text>\n");
           g_free(tmpstr);
         }
+        if (item->type == ITEM_IMAGE) {
+          gzprintf(f, "<image left=\"%.2f\" top=\"%.2f\" right=\"%.2f\" bottom=\"%.2f\">", 
+            item->bbox.left, item->bbox.top, item->bbox.right, item->bbox.bottom);
+          if (!write_image(f, item)) success = FALSE;
+          gzprintf(f, "</image>\n");
+        }
       }
       gzprintf(f, "</layer>\n");
     }
@@ -219,6 +287,7 @@ gboolean close_journal(void)
   
   // free everything...
   reset_selection();
+  reset_recognizer();
   clear_redo_stack();
   clear_undo_stack();
 
@@ -231,10 +300,16 @@ gboolean close_journal(void)
 }
 
 // sanitize a string containing floats, in case it may have , instead of .
+// also replace Windows-produced 1.#J by inf
 
 void cleanup_numeric(char *s)
 {
-  while (*s!=0) { if (*s==',') *s='.'; s++; }
+  while (*s!=0) { 
+    if (*s==',') *s='.'; 
+    if (*s=='1' && s[1]=='.' && s[2]=='#' && s[3]=='J') 
+      { *s='i'; s[1]='n'; s[2]='f'; s[3]=' '; }
+    s++; 
+  }
 }
 
 // the XML parser functions for open_journal()
@@ -248,7 +323,7 @@ struct Background *tmpBg_pdf;
 
 GError *xoj_invalid(void)
 {
-  return g_error_new(G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "Invalid file contents");
+  return g_error_new(G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, _("Invalid file contents"));
 }
 
 void xoj_parser_start_element(GMarkupParseContext *context,
@@ -342,7 +417,7 @@ void xoj_parser_start_element(GMarkupParseContext *context,
           }
         // there's also the case of hex (#rrggbbaa) colors
         if (tmpPage->bg->color_no == COLOR_OTHER && **attribute_values == '#') {
-          tmpPage->bg->color_rgba = strtol(*attribute_values + 1, &ptr, 16);
+          tmpPage->bg->color_rgba = strtoul(*attribute_values + 1, &ptr, 16);
           if (*ptr!=0) *error = xoj_invalid();
         }
         has_attr |= 2;
@@ -398,7 +473,7 @@ void xoj_parser_start_element(GMarkupParseContext *context,
             if (tmpPage->bg->pixbuf == NULL) {
               dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
                 GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, 
-                "Could not open background '%s'. Setting background to white.",
+                _("Could not open background '%s'. Setting background to white."),
                 tmpbg_filename);
               gtk_dialog_run(GTK_DIALOG(dialog));
               gtk_widget_destroy(dialog);
@@ -484,7 +559,7 @@ void xoj_parser_start_element(GMarkupParseContext *context,
           }
         // there's also the case of hex (#rrggbbaa) colors
         if (tmpItem->brush.color_no == COLOR_OTHER && **attribute_values == '#') {
-          tmpItem->brush.color_rgba = strtol(*attribute_values + 1, &ptr, 16);
+          tmpItem->brush.color_rgba = strtoul(*attribute_values + 1, &ptr, 16);
           if (*ptr!=0) *error = xoj_invalid();
         }
         has_attr |= 2;
@@ -563,7 +638,7 @@ void xoj_parser_start_element(GMarkupParseContext *context,
           }
         // there's also the case of hex (#rrggbbaa) colors
         if (tmpItem->brush.color_no == COLOR_OTHER && **attribute_values == '#') {
-          tmpItem->brush.color_rgba = strtol(*attribute_values + 1, &ptr, 16);
+          tmpItem->brush.color_rgba = strtoul(*attribute_values + 1, &ptr, 16);
           if (*ptr!=0) *error = xoj_invalid();
         }
         has_attr |= 16;
@@ -574,6 +649,56 @@ void xoj_parser_start_element(GMarkupParseContext *context,
     }
     if (has_attr!=31) *error = xoj_invalid();
   }
+  else if (!strcmp(element_name, "image")) { // start of a image item
+    if (tmpLayer == NULL || tmpItem != NULL) {
+      *error = xoj_invalid();
+      return;
+    }
+    tmpItem = (struct Item *)g_malloc0(sizeof(struct Item));
+    tmpItem->type = ITEM_IMAGE;
+    tmpItem->canvas_item = NULL;
+    tmpItem->image=NULL;
+    tmpItem->image_png = NULL;
+    tmpItem->image_png_len = 0;
+    tmpLayer->items = g_list_append(tmpLayer->items, tmpItem);
+    tmpLayer->nitems++;
+    // scan for x, y
+    has_attr = 0;
+    while (*attribute_names!=NULL) {
+      if (!strcmp(*attribute_names, "left")) {
+        if (has_attr & 1) *error = xoj_invalid();
+        cleanup_numeric((gchar *)*attribute_values);
+        tmpItem->bbox.left = g_ascii_strtod(*attribute_values, &ptr);
+        if (ptr == *attribute_values) *error = xoj_invalid();
+        has_attr |= 1;
+      }
+      else if (!strcmp(*attribute_names, "top")) {
+        if (has_attr & 2) *error = xoj_invalid();
+        cleanup_numeric((gchar *)*attribute_values);
+        tmpItem->bbox.top = g_ascii_strtod(*attribute_values, &ptr);
+        if (ptr == *attribute_values) *error = xoj_invalid();
+        has_attr |= 2;
+      }
+      else if (!strcmp(*attribute_names, "right")) {
+        if (has_attr & 4) *error = xoj_invalid();
+        cleanup_numeric((gchar *)*attribute_values);
+        tmpItem->bbox.right = g_ascii_strtod(*attribute_values, &ptr);
+        if (ptr == *attribute_values) *error = xoj_invalid();
+        has_attr |= 4;
+      }
+      else if (!strcmp(*attribute_names, "bottom")) {
+        if (has_attr & 8) *error = xoj_invalid();
+        cleanup_numeric((gchar *)*attribute_values);
+        tmpItem->bbox.bottom = g_ascii_strtod(*attribute_values, &ptr);
+        if (ptr == *attribute_values) *error = xoj_invalid();
+        has_attr |= 8;
+      }
+      else *error = xoj_invalid();
+      attribute_names++;
+      attribute_values++;
+    }
+    if (has_attr!=15) *error = xoj_invalid();
+  }
 }
 
 void xoj_parser_end_element(GMarkupParseContext *context,
@@ -609,6 +734,13 @@ void xoj_parser_end_element(GMarkupParseContext *context,
     }
     tmpItem = NULL;
   }
+  if (!strcmp(element_name, "image")) {
+    if (tmpItem == NULL) {
+      *error = xoj_invalid();
+      return;
+    }
+    tmpItem = NULL;
+  }
 }
 
 void xoj_parser_text(GMarkupParseContext *context,
@@ -629,7 +761,7 @@ void xoj_parser_text(GMarkupParseContext *context,
       if (ptr == text) break;
       text_len -= (ptr - text);
       text = ptr;
-      if (!finite(ui.cur_path.coords[n])) {
+      if (!finite_sized(ui.cur_path.coords[n])) {
         if (n>=2) ui.cur_path.coords[n] = ui.cur_path.coords[n-2];
         else ui.cur_path.coords[n] = 0;
       }
@@ -646,6 +778,9 @@ void xoj_parser_text(GMarkupParseContext *context,
     g_memmove(tmpItem->text, text, text_len);
     tmpItem->text[text_len]=0;
   }
+  if (!strcmp(element_name, "image")) {
+    tmpItem->image = read_pixbuf(text, text_len);
+  }
 }
 
 gboolean user_wants_second_chance(char **filename)
@@ -656,24 +791,29 @@ gboolean user_wants_second_chance(char **filename)
 
   dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
     GTK_MESSAGE_ERROR, GTK_BUTTONS_YES_NO, 
-    "Could not open background '%s'.\nSelect another file?",
+    _("Could not open background '%s'.\nSelect another file?"),
     *filename);
   response = gtk_dialog_run(GTK_DIALOG(dialog));
   gtk_widget_destroy(dialog);
   if (response != GTK_RESPONSE_YES) return FALSE;
-  dialog = gtk_file_chooser_dialog_new("Open PDF", GTK_WINDOW (winMain),
+  dialog = gtk_file_chooser_dialog_new(_("Open PDF"), GTK_WINDOW (winMain),
      GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
      GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
-
+#ifdef FILE_DIALOG_SIZE_BUGFIX
+  gtk_window_set_default_size(GTK_WINDOW(dialog), 500, 400);
+#endif
+  
   filt_all = gtk_file_filter_new();
-  gtk_file_filter_set_name(filt_all, "All files");
+  gtk_file_filter_set_name(filt_all, _("All files"));
   gtk_file_filter_add_pattern(filt_all, "*");
   filt_pdf = gtk_file_filter_new();
-  gtk_file_filter_set_name(filt_pdf, "PDF files");
+  gtk_file_filter_set_name(filt_pdf, _("PDF files"));
   gtk_file_filter_add_pattern(filt_pdf, "*.pdf");
   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filt_pdf);
   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filt_all);
 
+  if (ui.default_path!=NULL) gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (dialog), ui.default_path);
+
   if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_OK) {
     gtk_widget_destroy(dialog);
     return FALSE;
@@ -696,11 +836,25 @@ gboolean open_journal(char *filename)
   gzFile f;
   char buffer[1000];
   int len;
-  gchar *tmpfn;
+  gchar *tmpfn, *tmpfn2, *p, *q;
   gboolean maybe_pdf;
   
-  f = gzopen(filename, "r");
+  tmpfn = g_strdup_printf("%s.xoj", filename);
+  if (ui.autoload_pdf_xoj && g_file_test(tmpfn, G_FILE_TEST_EXISTS) &&
+      (g_str_has_suffix(filename, ".pdf") || g_str_has_suffix(filename, ".PDF")))
+  {
+    valid = open_journal(tmpfn);
+    g_free(tmpfn);
+    return valid;
+  }
+  g_free(tmpfn);
+
+  f = gzopen(filename, "rb");
   if (f==NULL) return FALSE;
+  if (filename[0]=='/') {
+    if (ui.default_path != NULL) g_free(ui.default_path);
+    ui.default_path = g_path_get_dirname(filename);
+  }
   
   context = g_markup_parse_context_new(&parser, 0, NULL, NULL);
   valid = TRUE;
@@ -755,7 +909,21 @@ gboolean open_journal(char *filename)
     else
       tmpfn = g_strdup(tmpBg_pdf->filename->s);
     valid = init_bgpdf(tmpfn, FALSE, tmpBg_pdf->file_domain);
-    // in case the file name became invalid
+    // if file name is invalid: first try in xoj file's directory
+    if (!valid && tmpBg_pdf->file_domain != DOMAIN_ATTACH) {
+      p = g_path_get_dirname(filename);
+      q = g_path_get_basename(tmpfn);
+      tmpfn2 = g_strdup_printf("%s/%s", p, q);
+      g_free(p); g_free(q);
+      valid = init_bgpdf(tmpfn2, FALSE, tmpBg_pdf->file_domain);
+      if (valid) {  // change the file name...
+        printf("substituting %s -> %s\n", tmpfn, tmpfn2);
+        g_free(tmpBg_pdf->filename->s);
+        tmpBg_pdf->filename->s = tmpfn2;
+      }
+      else g_free(tmpfn2);
+    }
+    // if file name is invalid: next prompt user
     if (!valid && tmpBg_pdf->file_domain != DOMAIN_ATTACH)
       if (user_wants_second_chance(&tmpfn)) {
         valid = init_bgpdf(tmpfn, FALSE, tmpBg_pdf->file_domain);
@@ -769,7 +937,7 @@ gboolean open_journal(char *filename)
       bgpdf.filename = refstring_ref(tmpBg_pdf->filename);
     } else {
       dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
-        GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Could not open background '%s'.",
+        GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not open background '%s'."),
         tmpfn);
       gtk_dialog_run(GTK_DIALOG(dialog));
       gtk_widget_destroy(dialog);
@@ -830,7 +998,7 @@ GList *attempt_load_gv_bg(char *filename)
   char *pipename;
   int buflen, remnlen, file_pageno;
   
-  f = fopen(filename, "r");
+  f = fopen(filename, "rb");
   if (f == NULL) return NULL;
   buf = g_malloc(BUFSIZE); // a reasonable buffer size
   if (fread(buf, 1, 4, f) !=4 ||
@@ -842,7 +1010,7 @@ GList *attempt_load_gv_bg(char *filename)
   
   fclose(f);
   pipename = g_strdup_printf(GS_CMDLINE, (double)GS_BITMAP_DPI, filename);
-  gs_pipe = popen(pipename, "r");
+  gs_pipe = popen(pipename, "rb");
   g_free(pipename);
   
   bg_list = NULL;
@@ -888,6 +1056,7 @@ GList *attempt_load_gv_bg(char *filename)
 
 struct Background *attempt_screenshot_bg(void)
 {
+#ifndef WIN32
   struct Background *bg;
   GdkPixbuf *pix;
   XEvent x_event;
@@ -924,6 +1093,10 @@ struct Background *attempt_screenshot_bg(void)
   bg->filename = new_refstring(NULL);
   bg->file_domain = DOMAIN_ATTACH;
   return bg;
+#else
+  // not implemented under WIN32
+  return FALSE;
+#endif
 }
 
 /************** pdf annotation ***************/
@@ -952,11 +1125,13 @@ gboolean bgpdf_scheduler_callback(gpointer data)
   PopplerPage *pdfpage;
   gdouble height, width;
   int scaled_height, scaled_width;
+  GdkPixmap *pixmap;
+  cairo_t *cr;
 
   // if all requests have been cancelled, remove ourselves from main loop
   if (bgpdf.requests == NULL) { bgpdf.pid = 0; return FALSE; }
   if (bgpdf.status == STATUS_NOT_INIT)
-    { printf("BGPDF not initialized??\n"); bgpdf.pid = 0; return FALSE; }
+    { printf("DEBUG: BGPDF not initialized??\n"); bgpdf.pid = 0; return FALSE; }
 
   req = (struct BgPdfRequest *)bgpdf.requests->data;
 
@@ -964,16 +1139,31 @@ gboolean bgpdf_scheduler_callback(gpointer data)
   pixbuf = NULL;
   pdfpage = poppler_document_get_page(bgpdf.document, req->pageno-1);
   if (pdfpage) {
-//    printf("Processing request for page %d at %f dpi\n", req->pageno, req->dpi);
+//    printf("DEBUG: Processing request for page %d at %f dpi\n", req->pageno, req->dpi);
     set_cursor_busy(TRUE);
     poppler_page_get_size(pdfpage, &width, &height);
     scaled_width = (int) (req->dpi * width/72);
     scaled_height = (int) (req->dpi * height/72);
-    pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
-                FALSE, 8, scaled_width, scaled_height);
-    poppler_page_render_to_pixbuf(
+
+    if (ui.poppler_force_cairo) { // poppler -> cairo -> pixmap -> pixbuf
+      pixmap = gdk_pixmap_new(GTK_WIDGET(canvas)->window, scaled_width, scaled_height, -1);
+      cr = gdk_cairo_create(pixmap);
+      cairo_set_source_rgb(cr, 1., 1., 1.);
+      cairo_paint(cr);
+      cairo_scale(cr, scaled_width/width, scaled_height/height);
+      poppler_page_render(pdfpage, cr);
+      cairo_destroy(cr);
+      pixbuf = gdk_pixbuf_get_from_drawable(NULL, GDK_DRAWABLE(pixmap),
+        NULL, 0, 0, 0, 0, scaled_width, scaled_height);
+      g_object_unref(pixmap);
+    }
+    else { // directly poppler -> pixbuf: faster, but bitmap font bug
+      pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
+                 FALSE, 8, scaled_width, scaled_height);
+      wrapper_poppler_page_render_to_pixbuf(
                 pdfpage, 0, 0, scaled_width, scaled_height,
                 req->dpi/72, 0, pixbuf);
+    }
     g_object_unref(pdfpage);
     set_cursor_busy(FALSE);
   }
@@ -996,7 +1186,7 @@ gboolean bgpdf_scheduler_callback(gpointer data)
   } else { // failure
     if (!bgpdf.has_failed) {
       dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
-        GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Unable to render one or more PDF pages.");
+        GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Unable to render one or more PDF pages."));
       gtk_dialog_run(GTK_DIALOG(dialog));
       gtk_widget_destroy(dialog);
     }
@@ -1011,17 +1201,17 @@ gboolean bgpdf_scheduler_callback(gpointer data)
 
 /* make a request */
 
-void add_bgpdf_request(int pageno, double zoom)
+gboolean add_bgpdf_request(int pageno, double zoom)
 {
   struct BgPdfRequest *req, *cmp_req;
   GList *list;
-  
+
   if (bgpdf.status == STATUS_NOT_INIT)
-    return; // don't accept requests
+    return FALSE; // don't accept requests
   req = g_new(struct BgPdfRequest, 1);
   req->pageno = pageno;
   req->dpi = 72*zoom;
-//  printf("Enqueuing request for page %d at %f dpi\n", pageno, req->dpi);
+//  printf("DEBUG: Enqueuing request for page %d at %f dpi\n", pageno, req->dpi);
 
   // cancel any request this may supersede
   for (list = bgpdf.requests; list != NULL; ) {
@@ -1033,6 +1223,7 @@ void add_bgpdf_request(int pageno, double zoom)
   // make the request
   bgpdf.requests = g_list_append(bgpdf.requests, req);
   if (!bgpdf.pid) bgpdf.pid = g_idle_add(bgpdf_scheduler_callback, NULL);
+  return TRUE;
 }
 
 /* shutdown the PDF reader */
@@ -1081,6 +1272,7 @@ gboolean init_bgpdf(char *pdfname, gboolean create_pages, int file_domain)
   struct Page *pg;
   PopplerPage *pdfpage;
   gdouble width, height;
+  gchar *uri;
   
   if (bgpdf.status != STATUS_NOT_INIT) return FALSE;
   
@@ -1100,8 +1292,22 @@ gboolean init_bgpdf(char *pdfname, gboolean create_pages, int file_domain)
   bgpdf.pid = 0;
   bgpdf.has_failed = FALSE;
 
-  bgpdf.document = poppler_document_new_from_data(bgpdf.file_contents, bgpdf.file_length, NULL, NULL);
-  if (bgpdf.document == NULL) shutdown_bgpdf();
+/* poppler_document_new_from_data() starts at 0.6.1, but we want to
+   be compatible with poppler 0.5.4 = latest in CentOS as of sept 2009 */
+  uri = g_filename_to_uri(pdfname, NULL, NULL);
+  if (!uri) uri = g_strdup_printf("file://%s", pdfname);
+  bgpdf.document = poppler_document_new_from_file(uri, NULL, NULL);
+  g_free(uri);
+/*    with poppler 0.6.1 or later, can replace the above 4 lines by:
+  bgpdf.document = poppler_document_new_from_data(bgpdf.file_contents, 
+                          bgpdf.file_length, NULL, NULL);
+*/
+  if (bgpdf.document == NULL) { shutdown_bgpdf(); return FALSE; }
+  
+  if (pdfname[0]=='/' && ui.filename == NULL) {
+    if (ui.default_path!=NULL) g_free(ui.default_path);
+    ui.default_path = g_path_get_dirname(pdfname);
+  }
 
   if (!create_pages) return TRUE; // we're done
   
@@ -1204,7 +1410,8 @@ void update_mru_menu(void)
   
   for (i=0; i<MRU_SIZE; i++) {
     if (ui.mru[i]!=NULL) {
-      tmp = g_strdup_printf("_%d %s", i+1, g_basename(ui.mru[i]));
+      tmp = g_strdup_printf("_%d %s", i+1,
+               g_strjoinv("__", g_strsplit_set(g_basename(ui.mru[i]),"_",-1)));
       gtk_label_set_text_with_mnemonic(GTK_LABEL(gtk_bin_get_child(GTK_BIN(ui.mrumenu[i]))),
           tmp);
       g_free(tmp);
@@ -1283,16 +1490,19 @@ void init_config_default(void)
   ui.scrollbar_step_increment = 30;
   ui.zoom_step_increment = 1;
   ui.zoom_step_factor = 1.5;
-  ui.antialias_bg = TRUE;
   ui.progressive_bg = TRUE;
   ui.print_ruling = TRUE;
   ui.default_unit = UNIT_CM;
   ui.default_path = NULL;
+  ui.default_image = NULL;
   ui.default_font_name = g_strdup(DEFAULT_FONT);
   ui.default_font_size = DEFAULT_FONT_SIZE;
   ui.pressure_sensitivity = FALSE;
   ui.width_minimum_multiplier = 0.0;
   ui.width_maximum_multiplier = 1.25;
+  ui.button_switch_mapping = FALSE;
+  ui.autoload_pdf_xoj = FALSE;
+  ui.poppler_force_cairo = FALSE;
   
   // the default UI vertical order
   ui.vertical_order[0][0] = 1; 
@@ -1330,6 +1540,11 @@ void init_config_default(void)
   PDFTOPPM_PRINTING_DPI = 150;
   
   ui.hiliter_opacity = 0.5;
+  
+#if GTK_CHECK_VERSION(2,10,0)
+  ui.print_settings = NULL;
+#endif
+  
 }
 
 #if GLIB_CHECK_VERSION(2,6,0)
@@ -1379,217 +1594,235 @@ void save_config_to_file(void)
       &ui.window_default_width, &ui.window_default_height);
 
   update_keyval("general", "display_dpi",
-    " the display resolution, in pixels per inch",
+    _(" the display resolution, in pixels per inch"),
     g_strdup_printf("%.2f", DEFAULT_ZOOM*72));
   update_keyval("general", "initial_zoom",
-    " the initial zoom level, in percent",
+    _(" the initial zoom level, in percent"),
     g_strdup_printf("%.2f", 100*ui.zoom/DEFAULT_ZOOM));
   update_keyval("general", "window_maximize",
-    " maximize the window at startup (true/false)",
+    _(" maximize the window at startup (true/false)"),
     g_strdup(ui.maximize_at_start?"true":"false"));
   update_keyval("general", "window_fullscreen",
-    " start in full screen mode (true/false)",
+    _(" start in full screen mode (true/false)"),
     g_strdup(ui.fullscreen?"true":"false"));
   update_keyval("general", "window_width",
-    " the window width in pixels (when not maximized)",
+    _(" the window width in pixels (when not maximized)"),
     g_strdup_printf("%d", ui.window_default_width));
   update_keyval("general", "window_height",
-    " the window height in pixels",
+    _(" the window height in pixels"),
     g_strdup_printf("%d", ui.window_default_height));
   update_keyval("general", "scrollbar_speed",
-    " scrollbar step increment (in pixels)",
+    _(" scrollbar step increment (in pixels)"),
     g_strdup_printf("%d", ui.scrollbar_step_increment));
   update_keyval("general", "zoom_dialog_increment",
-    " the step increment in the zoom dialog box",
+    _(" the step increment in the zoom dialog box"),
     g_strdup_printf("%d", ui.zoom_step_increment));
   update_keyval("general", "zoom_step_factor",
-    " the multiplicative factor for zoom in/out",
+    _(" the multiplicative factor for zoom in/out"),
     g_strdup_printf("%.3f", ui.zoom_step_factor));
   update_keyval("general", "view_continuous",
-    " document view (true = continuous, false = single page)",
+    _(" document view (true = continuous, false = single page)"),
     g_strdup(ui.view_continuous?"true":"false"));
   update_keyval("general", "use_xinput",
-    " use XInput extensions (true/false)",
+    _(" use XInput extensions (true/false)"),
     g_strdup(ui.allow_xinput?"true":"false"));
   update_keyval("general", "discard_corepointer",
-    " discard Core Pointer events in XInput mode (true/false)",
+    _(" discard Core Pointer events in XInput mode (true/false)"),
     g_strdup(ui.discard_corepointer?"true":"false"));
   update_keyval("general", "use_erasertip",
-    " always map eraser tip to eraser (true/false)",
+    _(" always map eraser tip to eraser (true/false)"),
     g_strdup(ui.use_erasertip?"true":"false"));
+  update_keyval("general", "buttons_switch_mappings",
+    _(" buttons 2 and 3 switch mappings instead of drawing (useful for some tablets) (true/false)"),
+    g_strdup(ui.button_switch_mapping?"true":"false"));
+  update_keyval("general", "autoload_pdf_xoj",
+    _(" automatically load filename.pdf.xoj instead of filename.pdf (true/false)"),
+    g_strdup(ui.autoload_pdf_xoj?"true":"false"));
   update_keyval("general", "default_path",
-    " default path for open/save (leave blank for current directory)",
+    _(" default path for open/save (leave blank for current directory)"),
     g_strdup((ui.default_path!=NULL)?ui.default_path:""));
   update_keyval("general", "pressure_sensitivity",
-     " use pressure sensitivity to control pen stroke width (true/false)",
+     _(" use pressure sensitivity to control pen stroke width (true/false)"),
      g_strdup(ui.pressure_sensitivity?"true":"false"));
   update_keyval("general", "width_minimum_multiplier",
-     " minimum width multiplier",
+     _(" minimum width multiplier"),
      g_strdup_printf("%.2f", ui.width_minimum_multiplier));
   update_keyval("general", "width_maximum_multiplier",
-     " maximum width multiplier",
+     _(" maximum width multiplier"),
      g_strdup_printf("%.2f", ui.width_maximum_multiplier));
   update_keyval("general", "interface_order",
-    " interface components from top to bottom\n valid values: drawarea menu main_toolbar pen_toolbar statusbar",
+    _(" interface components from top to bottom\n valid values: drawarea menu main_toolbar pen_toolbar statusbar"),
     verbose_vertical_order(ui.vertical_order[0]));
   update_keyval("general", "interface_fullscreen",
-    " interface components in fullscreen mode, from top to bottom",
+    _(" interface components in fullscreen mode, from top to bottom"),
     verbose_vertical_order(ui.vertical_order[1]));
   update_keyval("general", "interface_lefthanded",
-    " interface has left-handed scrollbar (true/false)",
+    _(" interface has left-handed scrollbar (true/false)"),
     g_strdup(ui.left_handed?"true":"false"));
   update_keyval("general", "shorten_menus",
-    " hide some unwanted menu or toolbar items (true/false)",
+    _(" hide some unwanted menu or toolbar items (true/false)"),
     g_strdup(ui.shorten_menus?"true":"false"));
   update_keyval("general", "shorten_menu_items",
-    " interface items to hide (customize at your own risk!)\n see source file xo-interface.c for a list of item names",
+    _(" interface items to hide (customize at your own risk!)\n see source file xo-interface.c for a list of item names"),
     g_strdup(ui.shorten_menu_items));
   update_keyval("general", "highlighter_opacity",
-    " highlighter opacity (0 to 1, default 0.5)\n warning: opacity level is not saved in xoj files!",
+    _(" highlighter opacity (0 to 1, default 0.5)\n warning: opacity level is not saved in xoj files!"),
     g_strdup_printf("%.2f", ui.hiliter_opacity));
   update_keyval("general", "autosave_prefs",
-    " auto-save preferences on exit (true/false)",
+    _(" auto-save preferences on exit (true/false)"),
     g_strdup(ui.auto_save_prefs?"true":"false"));
+  update_keyval("general", "poppler_force_cairo",
+    _(" force PDF rendering through cairo (slower but nicer) (true/false)"),
+    g_strdup(ui.poppler_force_cairo?"true":"false"));
 
   update_keyval("paper", "width",
-    " the default page width, in points (1/72 in)",
+    _(" the default page width, in points (1/72 in)"),
     g_strdup_printf("%.2f", ui.default_page.width));
   update_keyval("paper", "height",
-    " the default page height, in points (1/72 in)",
+    _(" the default page height, in points (1/72 in)"),
     g_strdup_printf("%.2f", ui.default_page.height));
   update_keyval("paper", "color",
-    " the default paper color",
-    g_strdup(bgcolor_names[ui.default_page.bg->color_no]));
+    _(" the default paper color"),
+    (ui.default_page.bg->color_no>=0)?
+    g_strdup(bgcolor_names[ui.default_page.bg->color_no]):
+    g_strdup_printf("#%08x", ui.default_page.bg->color_rgba));
   update_keyval("paper", "style",
-    " the default paper style (plain, lined, ruled, or graph)",
+    _(" the default paper style (plain, lined, ruled, or graph)"),
     g_strdup(bgstyle_names[ui.default_page.bg->ruling]));
   update_keyval("paper", "apply_all",
-    " apply paper style changes to all pages (true/false)",
+    _(" apply paper style changes to all pages (true/false)"),
     g_strdup(ui.bg_apply_all_pages?"true":"false"));
   update_keyval("paper", "default_unit",
-    " preferred unit (cm, in, px, pt)",
+    _(" preferred unit (cm, in, px, pt)"),
     g_strdup(unit_names[ui.default_unit]));
   update_keyval("paper", "print_ruling",
-    " include paper ruling when printing or exporting to PDF (true/false)",
+    _(" include paper ruling when printing or exporting to PDF (true/false)"),
     g_strdup(ui.print_ruling?"true":"false"));
-  update_keyval("paper", "antialias_bg",
-    " antialiased bitmap backgrounds (true/false)",
-    g_strdup(ui.antialias_bg?"true":"false"));
   update_keyval("paper", "progressive_bg",
-    " just-in-time update of page backgrounds (true/false)",
+    _(" just-in-time update of page backgrounds (true/false)"),
     g_strdup(ui.progressive_bg?"true":"false"));
   update_keyval("paper", "gs_bitmap_dpi",
-    " bitmap resolution of PS/PDF backgrounds rendered using ghostscript (dpi)",
+    _(" bitmap resolution of PS/PDF backgrounds rendered using ghostscript (dpi)"),
     g_strdup_printf("%d", GS_BITMAP_DPI));
   update_keyval("paper", "pdftoppm_printing_dpi",
-    " bitmap resolution of PDF backgrounds when printing with libgnomeprint (dpi)",
+    _(" bitmap resolution of PDF backgrounds when printing with libgnomeprint (dpi)"),
     g_strdup_printf("%d", PDFTOPPM_PRINTING_DPI));
 
   update_keyval("tools", "startup_tool",
-    " selected tool at startup (pen, eraser, highlighter, selectrect, vertspace, hand)",
+    _(" selected tool at startup (pen, eraser, highlighter, selectrect, vertspace, hand)"),
     g_strdup(tool_names[ui.startuptool]));
   update_keyval("tools", "pen_color",
-    " default pen color",
-    g_strdup(color_names[ui.default_brushes[TOOL_PEN].color_no]));
+    _(" default pen color"),
+    (ui.default_brushes[TOOL_PEN].color_no>=0)?
+    g_strdup(color_names[ui.default_brushes[TOOL_PEN].color_no]):
+    g_strdup_printf("#%08x", ui.default_brushes[TOOL_PEN].color_rgba));
   update_keyval("tools", "pen_thickness",
-    " default pen thickness (fine = 1, medium = 2, thick = 3)",
+    _(" default pen thickness (fine = 1, medium = 2, thick = 3)"),
     g_strdup_printf("%d", ui.default_brushes[TOOL_PEN].thickness_no));
   update_keyval("tools", "pen_ruler",
-    " default pen is in ruler mode (true/false)",
+    _(" default pen is in ruler mode (true/false)"),
     g_strdup(ui.default_brushes[TOOL_PEN].ruler?"true":"false"));
   update_keyval("tools", "pen_recognizer",
-    " default pen is in shape recognizer mode (true/false)",
+    _(" default pen is in shape recognizer mode (true/false)"),
     g_strdup(ui.default_brushes[TOOL_PEN].recognizer?"true":"false"));
   update_keyval("tools", "eraser_thickness",
-    " default eraser thickness (fine = 1, medium = 2, thick = 3)",
+    _(" default eraser thickness (fine = 1, medium = 2, thick = 3)"),
     g_strdup_printf("%d", ui.default_brushes[TOOL_ERASER].thickness_no));
   update_keyval("tools", "eraser_mode",
-    " default eraser mode (standard = 0, whiteout = 1, strokes = 2)",
+    _(" default eraser mode (standard = 0, whiteout = 1, strokes = 2)"),
     g_strdup_printf("%d", ui.default_brushes[TOOL_ERASER].tool_options));
   update_keyval("tools", "highlighter_color",
-    " default highlighter color",
-    g_strdup(color_names[ui.default_brushes[TOOL_HIGHLIGHTER].color_no]));
+    _(" default highlighter color"),
+    (ui.default_brushes[TOOL_HIGHLIGHTER].color_no>=0)?
+    g_strdup(color_names[ui.default_brushes[TOOL_HIGHLIGHTER].color_no]):
+    g_strdup_printf("#%08x", ui.default_brushes[TOOL_HIGHLIGHTER].color_rgba));
   update_keyval("tools", "highlighter_thickness",
-    " default highlighter thickness (fine = 1, medium = 2, thick = 3)",
+    _(" default highlighter thickness (fine = 1, medium = 2, thick = 3)"),
     g_strdup_printf("%d", ui.default_brushes[TOOL_HIGHLIGHTER].thickness_no));
   update_keyval("tools", "highlighter_ruler",
-    " default highlighter is in ruler mode (true/false)",
+    _(" default highlighter is in ruler mode (true/false)"),
     g_strdup(ui.default_brushes[TOOL_HIGHLIGHTER].ruler?"true":"false"));
   update_keyval("tools", "highlighter_recognizer",
-    " default highlighter is in shape recognizer mode (true/false)",
+    _(" default highlighter is in shape recognizer mode (true/false)"),
     g_strdup(ui.default_brushes[TOOL_HIGHLIGHTER].recognizer?"true":"false"));
   update_keyval("tools", "btn2_tool",
-    " button 2 tool (pen, eraser, highlighter, text, selectrect, vertspace, hand)",
+    _(" button 2 tool (pen, eraser, highlighter, text, selectrect, vertspace, hand)"),
     g_strdup(tool_names[ui.toolno[1]]));
   update_keyval("tools", "btn2_linked",
-    " button 2 brush linked to primary brush (true/false) (overrides all other settings)",
+    _(" button 2 brush linked to primary brush (true/false) (overrides all other settings)"),
     g_strdup((ui.linked_brush[1]==BRUSH_LINKED)?"true":"false"));
   update_keyval("tools", "btn2_color",
-    " button 2 brush color (for pen or highlighter only)",
-    g_strdup((ui.toolno[1]<NUM_STROKE_TOOLS)?
-               color_names[ui.brushes[1][ui.toolno[1]].color_no]:"white"));
+    _(" button 2 brush color (for pen or highlighter only)"),
+    (ui.toolno[1]<NUM_STROKE_TOOLS)?
+      ((ui.brushes[1][ui.toolno[1]].color_no>=0)?
+       g_strdup(color_names[ui.brushes[1][ui.toolno[1]].color_no]):
+       g_strdup_printf("#%08x", ui.brushes[1][ui.toolno[1]].color_rgba)):
+      g_strdup("white"));
   update_keyval("tools", "btn2_thickness",
-    " button 2 brush thickness (pen, eraser, or highlighter only)",
+    _(" button 2 brush thickness (pen, eraser, or highlighter only)"),
     g_strdup_printf("%d", (ui.toolno[1]<NUM_STROKE_TOOLS)?
                             ui.brushes[1][ui.toolno[1]].thickness_no:0));
   update_keyval("tools", "btn2_ruler",
-    " button 2 ruler mode (true/false) (for pen or highlighter only)",
+    _(" button 2 ruler mode (true/false) (for pen or highlighter only)"),
     g_strdup(((ui.toolno[1]<NUM_STROKE_TOOLS)?
               ui.brushes[1][ui.toolno[1]].ruler:FALSE)?"true":"false"));
   update_keyval("tools", "btn2_recognizer",
-    " button 2 shape recognizer mode (true/false) (pen or highlighter only)",
+    _(" button 2 shape recognizer mode (true/false) (pen or highlighter only)"),
     g_strdup(((ui.toolno[1]<NUM_STROKE_TOOLS)?
               ui.brushes[1][ui.toolno[1]].recognizer:FALSE)?"true":"false"));
   update_keyval("tools", "btn2_erasermode",
-    " button 2 eraser mode (eraser only)",
+    _(" button 2 eraser mode (eraser only)"),
     g_strdup_printf("%d", ui.brushes[1][TOOL_ERASER].tool_options));
   update_keyval("tools", "btn3_tool",
-    " button 3 tool (pen, eraser, highlighter, text, selectrect, vertspace, hand)",
+    _(" button 3 tool (pen, eraser, highlighter, text, selectrect, vertspace, hand)"),
     g_strdup(tool_names[ui.toolno[2]]));
   update_keyval("tools", "btn3_linked",
-    " button 3 brush linked to primary brush (true/false) (overrides all other settings)",
+    _(" button 3 brush linked to primary brush (true/false) (overrides all other settings)"),
     g_strdup((ui.linked_brush[2]==BRUSH_LINKED)?"true":"false"));
   update_keyval("tools", "btn3_color",
-    " button 3 brush color (for pen or highlighter only)",
-    g_strdup((ui.toolno[2]<NUM_STROKE_TOOLS)?
-               color_names[ui.brushes[2][ui.toolno[2]].color_no]:"white"));
+    _(" button 3 brush color (for pen or highlighter only)"),
+    (ui.toolno[2]<NUM_STROKE_TOOLS)?
+      ((ui.brushes[2][ui.toolno[2]].color_no>=0)?
+       g_strdup(color_names[ui.brushes[2][ui.toolno[2]].color_no]):
+       g_strdup_printf("#%08x", ui.brushes[2][ui.toolno[2]].color_rgba)):
+      g_strdup("white"));
   update_keyval("tools", "btn3_thickness",
-    " button 3 brush thickness (pen, eraser, or highlighter only)",
+    _(" button 3 brush thickness (pen, eraser, or highlighter only)"),
     g_strdup_printf("%d", (ui.toolno[2]<NUM_STROKE_TOOLS)?
                             ui.brushes[2][ui.toolno[2]].thickness_no:0));
   update_keyval("tools", "btn3_ruler",
-    " button 3 ruler mode (true/false) (for pen or highlighter only)",
+    _(" button 3 ruler mode (true/false) (for pen or highlighter only)"),
     g_strdup(((ui.toolno[2]<NUM_STROKE_TOOLS)?
               ui.brushes[2][ui.toolno[2]].ruler:FALSE)?"true":"false"));
   update_keyval("tools", "btn3_recognizer",
-    " button 3 shape recognizer mode (true/false) (pen or highlighter only)",
+    _(" button 3 shape recognizer mode (true/false) (pen or highlighter only)"),
     g_strdup(((ui.toolno[2]<NUM_STROKE_TOOLS)?
               ui.brushes[2][ui.toolno[2]].recognizer:FALSE)?"true":"false"));
   update_keyval("tools", "btn3_erasermode",
-    " button 3 eraser mode (eraser only)",
+    _(" button 3 eraser mode (eraser only)"),
     g_strdup_printf("%d", ui.brushes[2][TOOL_ERASER].tool_options));
 
   update_keyval("tools", "pen_thicknesses",
-    " thickness of the various pens (in points, 1 pt = 1/72 in)",
+    _(" thickness of the various pens (in points, 1 pt = 1/72 in)"),
     g_strdup_printf("%.2f;%.2f;%.2f;%.2f;%.2f", 
       predef_thickness[TOOL_PEN][0], predef_thickness[TOOL_PEN][1],
       predef_thickness[TOOL_PEN][2], predef_thickness[TOOL_PEN][3],
       predef_thickness[TOOL_PEN][4]));
   update_keyval("tools", "eraser_thicknesses",
-    " thickness of the various erasers (in points, 1 pt = 1/72 in)",
+    _(" thickness of the various erasers (in points, 1 pt = 1/72 in)"),
     g_strdup_printf("%.2f;%.2f;%.2f", 
       predef_thickness[TOOL_ERASER][1], predef_thickness[TOOL_ERASER][2],
       predef_thickness[TOOL_ERASER][3]));
   update_keyval("tools", "highlighter_thicknesses",
-    " thickness of the various highlighters (in points, 1 pt = 1/72 in)",
+    _(" thickness of the various highlighters (in points, 1 pt = 1/72 in)"),
     g_strdup_printf("%.2f;%.2f;%.2f", 
       predef_thickness[TOOL_HIGHLIGHTER][1], predef_thickness[TOOL_HIGHLIGHTER][2],
       predef_thickness[TOOL_HIGHLIGHTER][3]));
   update_keyval("tools", "default_font",
-    " name of the default font",
+    _(" name of the default font"),
     g_strdup(ui.default_font_name));
   update_keyval("tools", "default_font_size",
-    " default font size",
+    _(" default font size"),
     g_strdup_printf("%.1f", ui.default_font_size));
 
   buf = g_key_file_to_data(ui.config_data, NULL, NULL);
@@ -1669,6 +1902,28 @@ gboolean parse_keyval_enum(const gchar *group, const gchar *key, int *val, const
   return FALSE;
 }
 
+gboolean parse_keyval_enum_color(const gchar *group, const gchar *key, int *val, guint *val_rgba, 
+                                 const char **names, const guint *predef_rgba, int n)
+{
+  gchar *ret;
+  int i;
+  
+  ret = g_key_file_get_value(ui.config_data, group, key, NULL);
+  if (ret==NULL) return FALSE;
+  for (i=0; i<n; i++) {
+    if (!names[i][0]) continue; // "" is for invalid values
+    if (!g_ascii_strcasecmp(ret, names[i]))
+      { *val = i; *val_rgba = predef_rgba[i]; g_free(ret); return TRUE; }
+  }
+  if (ret[0]=='#') {
+    *val = COLOR_OTHER;
+    *val_rgba = strtoul(ret+1, NULL, 16);
+    g_free(ret);
+    return TRUE;
+  }
+  return FALSE;
+}
+
 gboolean parse_keyval_boolean(const gchar *group, const gchar *key, gboolean *val)
 {
   gchar *ret;
@@ -1745,9 +2000,9 @@ void load_config_from_file(void)
     g_key_file_free(ui.config_data);
     ui.config_data = g_key_file_new();
     g_key_file_set_comment(ui.config_data, NULL, NULL, 
-           " Xournal configuration file.\n"
+         _(" Xournal configuration file.\n"
            " This file is generated automatically upon saving preferences.\n"
-           " Use caution when editing this file manually.\n", NULL);
+           " Use caution when editing this file manually.\n"), NULL);
     return;
   }
 
@@ -1768,6 +2023,8 @@ void load_config_from_file(void)
   parse_keyval_boolean("general", "use_xinput", &ui.allow_xinput);
   parse_keyval_boolean("general", "discard_corepointer", &ui.discard_corepointer);
   parse_keyval_boolean("general", "use_erasertip", &ui.use_erasertip);
+  parse_keyval_boolean("general", "buttons_switch_mappings", &ui.button_switch_mapping);
+  parse_keyval_boolean("general", "autoload_pdf_xoj", &ui.autoload_pdf_xoj);
   parse_keyval_string("general", "default_path", &ui.default_path);
   parse_keyval_boolean("general", "pressure_sensitivity", &ui.pressure_sensitivity);
   parse_keyval_float("general", "width_minimum_multiplier", &ui.width_minimum_multiplier, 0., 10.);
@@ -1781,15 +2038,16 @@ void load_config_from_file(void)
     if (str!=NULL) { g_free(ui.shorten_menu_items); ui.shorten_menu_items = str; }
   parse_keyval_float("general", "highlighter_opacity", &ui.hiliter_opacity, 0., 1.);
   parse_keyval_boolean("general", "autosave_prefs", &ui.auto_save_prefs);
+  parse_keyval_boolean("general", "poppler_force_cairo", &ui.poppler_force_cairo);
   
   parse_keyval_float("paper", "width", &ui.default_page.width, 1., 5000.);
   parse_keyval_float("paper", "height", &ui.default_page.height, 1., 5000.);
-  parse_keyval_enum("paper", "color", &(ui.default_page.bg->color_no), bgcolor_names, COLOR_MAX);
-  ui.default_page.bg->color_rgba = predef_bgcolors_rgba[ui.default_page.bg->color_no];
+  parse_keyval_enum_color("paper", "color", 
+     &(ui.default_page.bg->color_no), &(ui.default_page.bg->color_rgba), 
+     bgcolor_names, predef_bgcolors_rgba, COLOR_MAX);
   parse_keyval_enum("paper", "style", &(ui.default_page.bg->ruling), bgstyle_names, 4);
   parse_keyval_boolean("paper", "apply_all", &ui.bg_apply_all_pages);
   parse_keyval_enum("paper", "default_unit", &ui.default_unit, unit_names, 4);
-  parse_keyval_boolean("paper", "antialias_bg", &ui.antialias_bg);
   parse_keyval_boolean("paper", "progressive_bg", &ui.progressive_bg);
   parse_keyval_boolean("paper", "print_ruling", &ui.print_ruling);
   parse_keyval_int("paper", "gs_bitmap_dpi", &GS_BITMAP_DPI, 1, 1200);
@@ -1797,13 +2055,17 @@ void load_config_from_file(void)
 
   parse_keyval_enum("tools", "startup_tool", &ui.startuptool, tool_names, NUM_TOOLS);
   ui.toolno[0] = ui.startuptool;
-  parse_keyval_enum("tools", "pen_color", &(ui.brushes[0][TOOL_PEN].color_no), color_names, COLOR_MAX);
+  parse_keyval_enum_color("tools", "pen_color", 
+     &(ui.brushes[0][TOOL_PEN].color_no), &(ui.brushes[0][TOOL_PEN].color_rgba),
+     color_names, predef_colors_rgba, COLOR_MAX);
   parse_keyval_int("tools", "pen_thickness", &(ui.brushes[0][TOOL_PEN].thickness_no), 0, 4);
   parse_keyval_boolean("tools", "pen_ruler", &(ui.brushes[0][TOOL_PEN].ruler));
   parse_keyval_boolean("tools", "pen_recognizer", &(ui.brushes[0][TOOL_PEN].recognizer));
   parse_keyval_int("tools", "eraser_thickness", &(ui.brushes[0][TOOL_ERASER].thickness_no), 1, 3);
   parse_keyval_int("tools", "eraser_mode", &(ui.brushes[0][TOOL_ERASER].tool_options), 0, 2);
-  parse_keyval_enum("tools", "highlighter_color", &(ui.brushes[0][TOOL_HIGHLIGHTER].color_no), color_names, COLOR_MAX);
+  parse_keyval_enum_color("tools", "highlighter_color", 
+     &(ui.brushes[0][TOOL_HIGHLIGHTER].color_no), &(ui.brushes[0][TOOL_HIGHLIGHTER].color_rgba),
+     color_names, predef_colors_rgba, COLOR_MAX);
   parse_keyval_int("tools", "highlighter_thickness", &(ui.brushes[0][TOOL_HIGHLIGHTER].thickness_no), 0, 4);
   parse_keyval_boolean("tools", "highlighter_ruler", &(ui.brushes[0][TOOL_HIGHLIGHTER].ruler));
   parse_keyval_boolean("tools", "highlighter_recognizer", &(ui.brushes[0][TOOL_HIGHLIGHTER].recognizer));
@@ -1822,7 +2084,9 @@ void load_config_from_file(void)
     if (ui.toolno[1]==TOOL_PEN || ui.toolno[1]==TOOL_HIGHLIGHTER) {
       parse_keyval_boolean("tools", "btn2_ruler", &(ui.brushes[1][ui.toolno[1]].ruler));
       parse_keyval_boolean("tools", "btn2_recognizer", &(ui.brushes[1][ui.toolno[1]].recognizer));
-      parse_keyval_enum("tools", "btn2_color", &(ui.brushes[1][ui.toolno[1]].color_no), color_names, COLOR_MAX);
+      parse_keyval_enum_color("tools", "btn2_color", 
+         &(ui.brushes[1][ui.toolno[1]].color_no), &(ui.brushes[1][ui.toolno[1]].color_rgba), 
+         color_names, predef_colors_rgba, COLOR_MAX);
     }
     if (ui.toolno[1]<NUM_STROKE_TOOLS)
       parse_keyval_int("tools", "btn2_thickness", &(ui.brushes[1][ui.toolno[1]].thickness_no), 0, 4);
@@ -1833,7 +2097,9 @@ void load_config_from_file(void)
     if (ui.toolno[2]==TOOL_PEN || ui.toolno[2]==TOOL_HIGHLIGHTER) {
       parse_keyval_boolean("tools", "btn3_ruler", &(ui.brushes[2][ui.toolno[2]].ruler));
       parse_keyval_boolean("tools", "btn3_recognizer", &(ui.brushes[2][ui.toolno[2]].recognizer));
-      parse_keyval_enum("tools", "btn3_color", &(ui.brushes[2][ui.toolno[2]].color_no), color_names, COLOR_MAX);
+      parse_keyval_enum_color("tools", "btn3_color", 
+         &(ui.brushes[2][ui.toolno[2]].color_no), &(ui.brushes[2][ui.toolno[2]].color_rgba), 
+         color_names, predef_colors_rgba, COLOR_MAX);
     }
     if (ui.toolno[2]<NUM_STROKE_TOOLS)
       parse_keyval_int("tools", "btn3_thickness", &(ui.brushes[2][ui.toolno[2]].thickness_no), 0, 4);