]> git.donarmstrong.com Git - xournal.git/blobdiff - src/xo-misc.c
fix linker flags (#3208984); evdev coordinate fix (#3244118)
[xournal.git] / src / xo-misc.c
index cc4fdc5ff7cfc1bcc58ee7b64329d88cdf08d551..4f288e5ff6c03de1d709f2b378cfa03c8df1acd9 100644 (file)
@@ -7,7 +7,6 @@
 #include <gtk/gtk.h>
 #include <libgnomecanvas/libgnomecanvas.h>
 #include <gdk/gdkkeysyms.h>
-#include <X11/Xlib.h>
 
 #include "xournal.h"
 #include "xo-interface.h"
@@ -374,7 +373,7 @@ void fix_xinput_coords(GdkEvent *event)
 
 #ifdef ENABLE_XINPUT_BUGFIX
   // fix broken events with the core pointer's location
-  if (!finite(axes[0]) || !finite(axes[1]) || (axes[0]==0. && axes[1]==0.)) {
+  if (!finite(axes[0]) || !finite(axes[1]) || axes[0]==0. || axes[1]==0.) {
     gdk_window_get_pointer(GTK_WIDGET(canvas)->window, &ix, &iy, NULL);
     *px = ix + sx; 
     *py = iy + sy;
@@ -414,12 +413,24 @@ void fix_xinput_coords(GdkEvent *event)
 
 double get_pressure_multiplier(GdkEvent *event)
 {
+  double *axes;
   double rawpressure;
+  GdkDevice *device;
+
+  if (event->type == GDK_MOTION_NOTIFY) {
+    axes = event->motion.axes;
+    device = event->motion.device;
+  }
+  else {
+    axes = event->button.axes;
+    device = event->button.device;
+  }
   
-  if (event->button.device == gdk_device_get_core_pointer()
-      || event->button.device->num_axes <= 2) return 1.0;
+  if (device == gdk_device_get_core_pointer()
+      || device->num_axes <= 2) return 1.0;
 
-  rawpressure = event->button.axes[2]/(event->button.device->axes[2].max - event->button.device->axes[2].min);
+  rawpressure = axes[2]/(device->axes[2].max - device->axes[2].min);
+  if (!finite(rawpressure)) return 1.0;
 
   return ((1-rawpressure)*ui.width_minimum_multiplier + rawpressure*ui.width_maximum_multiplier);
 }
@@ -653,7 +664,7 @@ void rescale_bg_pixmaps(void)
     // in progressive mode we scale only visible pages
     if (ui.progressive_bg && !is_visible(pg)) continue;
 
-    if (pg->bg->type == BG_PIXMAP && pg->bg->canvas_item!=NULL) { // do the rescaling ourselves
+    if (pg->bg->type == BG_PIXMAP && pg->bg->canvas_item!=NULL) {
       g_object_get(G_OBJECT(pg->bg->canvas_item), "pixbuf", &pix, NULL);
       if (pix!=pg->bg->pixbuf)
         gnome_canvas_item_set(pg->bg->canvas_item, "pixbuf", pg->bg->pixbuf, NULL);
@@ -675,8 +686,8 @@ void rescale_bg_pixmaps(void)
       // request an asynchronous update to a better pixmap if needed
       zoom_to_request = MIN(ui.zoom, MAX_SAFE_RENDER_DPI/72.0);
       if (pg->bg->pixbuf_scale == zoom_to_request) continue;
-      add_bgpdf_request(pg->bg->file_page_seq, zoom_to_request);
-      pg->bg->pixbuf_scale = zoom_to_request;
+      if (add_bgpdf_request(pg->bg->file_page_seq, zoom_to_request))
+        pg->bg->pixbuf_scale = zoom_to_request;
     }
   }
 }
@@ -1104,12 +1115,9 @@ void update_mappings_menu_linkings(void)
 void update_mappings_menu(void)
 {
   gtk_widget_set_sensitive(GET_COMPONENT("optionsButtonMappings"), ui.use_xinput);
-  gtk_widget_set_sensitive(GET_COMPONENT("optionsDiscardCoreEvents"), ui.use_xinput);
   gtk_widget_set_sensitive(GET_COMPONENT("optionsPressureSensitive"), ui.use_xinput);
   gtk_check_menu_item_set_active(
     GTK_CHECK_MENU_ITEM(GET_COMPONENT("optionsButtonMappings")), ui.use_erasertip);
-  gtk_check_menu_item_set_active(
-    GTK_CHECK_MENU_ITEM(GET_COMPONENT("optionsDiscardCoreEvents")), ui.discard_corepointer);
   gtk_check_menu_item_set_active(
     GTK_CHECK_MENU_ITEM(GET_COMPONENT("optionsPressureSensitive")), ui.pressure_sensitivity);
 
@@ -1281,6 +1289,8 @@ void update_page_stuff(void)
   gtk_combo_box_set_active(layerbox, ui.cur_page->nlayers-1-ui.layerno);
   ui.in_update_page_stuff = FALSE;
   
+  gtk_container_forall(GTK_CONTAINER(layerbox), unset_flags, (gpointer)GTK_CAN_FOCUS);
+  
   // update the paper-style menu radio buttons
   
   if (ui.view_continuous)
@@ -1318,7 +1328,7 @@ void update_page_stuff(void)
         break;
       default:
         gtk_check_menu_item_set_active(
-          GTK_CHECK_MENU_ITEM(GET_COMPONENT("papercolorOther")), TRUE);
+          GTK_CHECK_MENU_ITEM(GET_COMPONENT("papercolorNA")), TRUE);
         break;
     }
     switch (ui.cur_page->bg->ruling) {
@@ -1492,7 +1502,6 @@ void process_color_activate(GtkMenuItem *menuitem, int color_no, guint color_rgb
   }
 
   if (ui.cur_mapping != 0 && !ui.button_switch_mapping) return; // not user-generated
-  reset_focus();
 
   if (ui.cur_item_type == ITEM_TEXT)
     recolor_temp_text(color_no, color_rgba);
@@ -1535,7 +1544,6 @@ void process_thickness_activate(GtkMenuItem *menuitem, int tool, int val)
   if (ui.cur_mapping != 0 && !ui.button_switch_mapping) return; // not user-generated
 
   if (ui.selection != NULL && GTK_OBJECT_TYPE(menuitem) != GTK_TYPE_RADIO_MENU_ITEM) {
-    reset_focus();
     rethicken_selection(val);
     update_thickness_buttons();
   }
@@ -1549,7 +1557,6 @@ void process_thickness_activate(GtkMenuItem *menuitem, int tool, int val)
     which_mapping = ui.cur_mapping;
   else which_mapping = 0;
   if (ui.brushes[which_mapping][tool].thickness_no == val) return;
-  reset_focus();
   end_text();
   ui.brushes[which_mapping][tool].thickness_no = val;
   ui.brushes[which_mapping][tool].thickness = predef_thickness[tool][val];
@@ -1562,16 +1569,18 @@ void process_thickness_activate(GtkMenuItem *menuitem, int tool, int val)
   update_cursor();
 }
 
-void process_papercolor_activate(GtkMenuItem *menuitem, int color)
+void process_papercolor_activate(GtkMenuItem *menuitem, int color, guint rgba)
 {
   struct Page *pg;
   GList *pglist;
   gboolean hasdone;
 
-  if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM (menuitem)))
-    return;
+  if (GTK_OBJECT_TYPE(menuitem) == GTK_TYPE_RADIO_MENU_ITEM) {
+    if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM (menuitem)))
+      return;
+  }
 
-  if ((ui.cur_page->bg->type != BG_SOLID) || ui.bg_apply_all_pages)
+  if ((ui.cur_page->bg->type != BG_SOLID) || ui.bg_apply_all_pages || color == COLOR_OTHER)
     gtk_check_menu_item_set_active(
       GTK_CHECK_MENU_ITEM(GET_COMPONENT("papercolorNA")), TRUE);
 
@@ -1579,7 +1588,7 @@ void process_papercolor_activate(GtkMenuItem *menuitem, int color)
   hasdone = FALSE;
   for (pglist = journal.pages; pglist!=NULL; pglist = pglist->next) {
     if (ui.bg_apply_all_pages) pg = (struct Page *)pglist->data;
-    if (pg->bg->type == BG_SOLID && pg->bg->color_no != color) {
+    if (pg->bg->type == BG_SOLID && pg->bg->color_rgba != rgba) {
       prepare_new_undo();
       if (hasdone) undo->multiop |= MULTIOP_CONT_UNDO;
       undo->multiop |= MULTIOP_CONT_REDO;
@@ -1590,7 +1599,7 @@ void process_papercolor_activate(GtkMenuItem *menuitem, int color)
       undo->bg->canvas_item = NULL;
 
       pg->bg->color_no = color;
-      pg->bg->color_rgba = predef_bgcolors_rgba[color];
+      pg->bg->color_rgba = rgba;
       update_canvas_bg(pg);
     }
     if (!ui.bg_apply_all_pages) break;
@@ -1650,9 +1659,12 @@ gboolean ok_to_close(void)
 
   if (ui.saved) return TRUE;
   dialog = gtk_message_dialog_new(GTK_WINDOW (winMain), GTK_DIALOG_DESTROY_WITH_PARENT,
-    GTK_MESSAGE_WARNING, GTK_BUTTONS_YES_NO, _("Save changes to '%s'?"),
+    GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE, _("Save changes to '%s'?"),
     (ui.filename!=NULL) ? ui.filename:_("Untitled"));
+  gtk_dialog_add_button(GTK_DIALOG (dialog), GTK_STOCK_DISCARD, GTK_RESPONSE_NO);
+  gtk_dialog_add_button(GTK_DIALOG (dialog), GTK_STOCK_SAVE, GTK_RESPONSE_YES);
   gtk_dialog_add_button(GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
+  gtk_dialog_set_default_response(GTK_DIALOG (dialog), GTK_RESPONSE_YES);
   response = gtk_dialog_run(GTK_DIALOG (dialog));
   gtk_widget_destroy(dialog);
   if (response == GTK_RESPONSE_CANCEL || response == GTK_RESPONSE_DELETE_EVENT) 
@@ -1665,15 +1677,13 @@ gboolean ok_to_close(void)
 }
 
 // send the focus back to the appropriate widget
+
 void reset_focus(void)
 {
-/*
   if (ui.cur_item_type == ITEM_TEXT)
     gtk_widget_grab_focus(ui.cur_item->widget);
   else
     gtk_widget_grab_focus(GTK_WIDGET(canvas));
-  reset_recognizer();
-*/
 }
 
 // selection / clipboard stuff
@@ -1830,7 +1840,6 @@ void process_mapping_activate(GtkMenuItem *menuitem, int m, int tool)
   if (ui.toolno[m] == tool) return;
   switch_mapping(0);
   end_text();
-  reset_focus();
     
   ui.toolno[m] = tool;
   if (ui.linked_brush[m] == BRUSH_COPIED) {
@@ -2018,7 +2027,6 @@ void hide_unimplemented(void)
 {
   gtk_widget_hide(GET_COMPONENT("filePrintOptions"));
   gtk_widget_hide(GET_COMPONENT("journalFlatten"));  
-  gtk_widget_hide(GET_COMPONENT("papercolorOther")); 
   gtk_widget_hide(GET_COMPONENT("toolsSelectRegion"));
   gtk_widget_hide(GET_COMPONENT("buttonSelectRegion"));
   gtk_widget_hide(GET_COMPONENT("button2SelectRegion"));
@@ -2034,22 +2042,40 @@ void hide_unimplemented(void)
   if (gtk_check_version(2, 10, 0)) {
     gtk_widget_hide(GET_COMPONENT("filePrint"));
   }  
+  
+  /* screenshot feature doesn't work yet in Win32 */
+#ifdef WIN32
+  gtk_widget_hide(GET_COMPONENT("journalScreenshot"));
+#endif
 }  
 
 // toggle fullscreen mode
 void do_fullscreen(gboolean active)
 {
   end_text();
-  reset_focus();
   ui.fullscreen = active;
   gtk_check_menu_item_set_active(
     GTK_CHECK_MENU_ITEM(GET_COMPONENT("viewFullscreen")), ui.fullscreen);
   gtk_toggle_tool_button_set_active(
     GTK_TOGGLE_TOOL_BUTTON(GET_COMPONENT("buttonFullscreen")), ui.fullscreen);
 
-  if (ui.fullscreen) gtk_window_fullscreen(GTK_WINDOW(winMain));
-  else gtk_window_unfullscreen(GTK_WINDOW(winMain));
-  
+  if (ui.fullscreen) {
+#ifdef WIN32
+    gtk_window_get_size(GTK_WINDOW(winMain), &ui.pre_fullscreen_width, &ui.pre_fullscreen_height);
+    gtk_widget_set_size_request(GTK_WIDGET(winMain), gdk_screen_width(),
+                                                     gdk_screen_height());
+#endif
+    gtk_window_fullscreen(GTK_WINDOW(winMain));
+  }
+  else {
+#ifdef WIN32
+    gtk_widget_set_size_request(GTK_WIDGET(winMain), -1, -1);
+    gtk_window_resize(GTK_WINDOW(winMain), ui.pre_fullscreen_width,
+                                           ui.pre_fullscreen_height);
+#endif
+    gtk_window_unfullscreen(GTK_WINDOW(winMain));
+  }
+
   update_vbox_order(ui.vertical_order[ui.fullscreen?1:0]);
 }
 
@@ -2115,23 +2141,12 @@ gboolean fix_extended_events (GtkWidget *widget, GdkEvent *event,
 }
 */
 
-// disable xinput when layer combo box is popped up, to avoid GTK+ 2.17 crash
-
-gboolean combobox_popup_disable_xinput (GtkWidget *widget, GdkEvent *event,
-                                   gpointer user_data)
-{
-  gboolean is_shown;
-  
-  g_object_get(G_OBJECT(widget), "popup-shown", &is_shown, NULL);
-  gtk_widget_set_extension_events(GTK_WIDGET (canvas), 
-       (ui.use_xinput && !is_shown)?GDK_EXTENSION_EVENTS_ALL:GDK_EXTENSION_EVENTS_NONE);
-}
 
 /* When enter is pressed into page spinbox, send focus back to canvas. */
 
 gboolean handle_activate_signal(GtkWidget *widget, gpointer user_data)
 {
-  gtk_widget_grab_focus(GTK_WIDGET(canvas));
+  reset_focus();
   return FALSE;
 }
 
@@ -2141,6 +2156,134 @@ void unset_flags(GtkWidget *w, gpointer flag)
 {
   GTK_WIDGET_UNSET_FLAGS(w, (GtkWidgetFlags)flag);
   if(GTK_IS_CONTAINER(w))
-    gtk_container_foreach(GTK_CONTAINER(w), unset_flags, flag);
+    gtk_container_forall(GTK_CONTAINER(w), unset_flags, flag);
+}
+
+/* reset focus when a key or button press event reaches someone, or when the
+   page-number spin button should relinquish control... */
+
+gboolean intercept_activate_events(GtkWidget *w, GdkEvent *ev, gpointer data)
+{
+  if (w == GET_COMPONENT("hbox1")) {
+    /* the event won't be processed since the hbox1 doesn't know what to do with it,
+       so we might as well kill it and avoid confusing ourselves when it gets
+       propagated further ... */
+    return TRUE;
+  }
+  if (w == GET_COMPONENT("spinPageNo")) {
+    /* we let the spin button take care of itself, and don't steal its focus,
+       unless the user presses Esc or Tab (in those cases we intervene) */
+    if (ev->type != GDK_KEY_PRESS) return FALSE;
+    if (ev->key.keyval == GDK_Escape) 
+       gtk_spin_button_set_value(GTK_SPIN_BUTTON(w), ui.pageno+1); // abort
+    else if (ev->key.keyval != GDK_Tab && ev->key.keyval != GDK_ISO_Left_Tab)
+       return FALSE; // let the spin button process it
+  }
+
+  // otherwise, we want to make sure the canvas or text item gets focus back...
+  reset_focus();  
+  return FALSE;
+}
+
+void install_focus_hooks(GtkWidget *w, gpointer data)
+{
+  if (w == NULL) return;
+  g_signal_connect(w, "key-press-event", G_CALLBACK(intercept_activate_events), data);
+  g_signal_connect(w, "button-press-event", G_CALLBACK(intercept_activate_events), data);
+  if (GTK_IS_MENU_ITEM(w)) {
+    g_signal_connect(w, "activate", G_CALLBACK(intercept_activate_events), data);
+    install_focus_hooks(gtk_menu_item_get_submenu(GTK_MENU_ITEM(w)), data);
+  }
+  if(GTK_IS_CONTAINER(w))
+    gtk_container_forall(GTK_CONTAINER(w), install_focus_hooks, data);
+}
+
+// wrapper for missing poppler functions (defunct poppler-gdk api)
+
+static void
+wrapper_copy_cairo_surface_to_pixbuf (cairo_surface_t *surface,
+                             GdkPixbuf       *pixbuf)
+{
+  int cairo_width, cairo_height, cairo_rowstride;
+  unsigned char *pixbuf_data, *dst, *cairo_data;
+  int pixbuf_rowstride, pixbuf_n_channels;
+  unsigned int *src;
+  int x, y;
+
+  cairo_width = cairo_image_surface_get_width (surface);
+  cairo_height = cairo_image_surface_get_height (surface);
+  cairo_rowstride = cairo_image_surface_get_stride (surface);
+  cairo_data = cairo_image_surface_get_data (surface);
+
+  pixbuf_data = gdk_pixbuf_get_pixels (pixbuf);
+  pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
+  pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
+
+  if (cairo_width > gdk_pixbuf_get_width (pixbuf))
+    cairo_width = gdk_pixbuf_get_width (pixbuf);
+  if (cairo_height > gdk_pixbuf_get_height (pixbuf))
+    cairo_height = gdk_pixbuf_get_height (pixbuf);
+  for (y = 0; y < cairo_height; y++)
+    {
+      src = (unsigned int *) (cairo_data + y * cairo_rowstride);
+      dst = pixbuf_data + y * pixbuf_rowstride;
+      for (x = 0; x < cairo_width; x++) 
+       {
+         dst[0] = (*src >> 16) & 0xff;
+         dst[1] = (*src >> 8) & 0xff; 
+         dst[2] = (*src >> 0) & 0xff;
+         if (pixbuf_n_channels == 4)
+             dst[3] = (*src >> 24) & 0xff;
+         dst += pixbuf_n_channels;
+         src++;
+       }
+    }
+}      
+
+void
+wrapper_poppler_page_render_to_pixbuf (PopplerPage *page,
+                              int src_x, int src_y,
+                              int src_width, int src_height,
+                              double scale,
+                              int rotation,
+                              GdkPixbuf *pixbuf)
+{
+  cairo_t *cr;
+  cairo_surface_t *surface;
+
+  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+                                       src_width, src_height);
+  cr = cairo_create (surface);
+  cairo_save (cr);
+  switch (rotation) {
+  case 90:
+         cairo_translate (cr, src_x + src_width, -src_y);
+         break;
+  case 180:
+         cairo_translate (cr, src_x + src_width, src_y + src_height);
+         break;
+  case 270:
+         cairo_translate (cr, -src_x, src_y + src_height);
+         break;
+  default:
+         cairo_translate (cr, -src_x, -src_y);
+  }
+
+  if (scale != 1.0)
+         cairo_scale (cr, scale, scale);
+
+  if (rotation != 0)
+         cairo_rotate (cr, rotation * G_PI / 180.0);
+
+  poppler_page_render (page, cr);
+  cairo_restore (cr);
+
+  cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
+  cairo_set_source_rgb (cr, 1., 1., 1.);
+  cairo_paint (cr);
+
+  cairo_destroy (cr);
+
+  wrapper_copy_cairo_surface_to_pixbuf (surface, pixbuf);
+  cairo_surface_destroy (surface);
 }
-        
\ No newline at end of file