]> www.fi.muni.cz Git - evince.git/blobdiff - shell/ev-utils.c
[dualscreen] fix crash on ctrl+w and fix control window closing
[evince.git] / shell / ev-utils.c
index ba560d53f2effc42fda4f71c90640150f3cb768c..8c9bae5501617027009d9be62379e668bc136ad4 100644 (file)
@@ -14,7 +14,7 @@
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  */
 
 
 #include "ev-utils.h"
 #include "ev-file-helpers.h"
-#include <math.h>
 
-#define PRINT_CONFIG_FILENAME  "ev-print-config.xml"
+#include <string.h>
+#include <math.h>
+#include <glib/gi18n.h>
 
 typedef struct
 {
@@ -186,9 +187,8 @@ ev_pixbuf_add_shadow (GdkPixbuf *src, int size,
  * the region code.
  */
 void
-ev_print_region_contents (GdkRegion *region)
+ev_print_region_contents (cairo_region_t *region)
 {
-       GdkRectangle *rectangles = NULL;
        gint n_rectangles, i;
 
        if (region == NULL) {
@@ -197,108 +197,193 @@ ev_print_region_contents (GdkRegion *region)
        }
 
        g_print ("<region %p>\n", region);
-       gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
+       n_rectangles = cairo_region_num_rectangles (region);
        for (i = 0; i < n_rectangles; i++) {
+               GdkRectangle rect;
+
+               cairo_region_get_rectangle (region, i, &rect);
                g_print ("\t(%d %d, %d %d) [%dx%d]\n",
-                        rectangles[i].x,
-                        rectangles[i].y,
-                        rectangles[i].x + rectangles[i].width,
-                        rectangles[i].y + rectangles[i].height,
-                        rectangles[i].width,
-                        rectangles[i].height);
+                        rect.x,
+                        rect.y,
+                        rect.x + rect.width,
+                        rect.y + rect.height,
+                        rect.width,
+                        rect.height);
        }
-       g_free (rectangles);
 }
 
-#ifdef WITH_GNOME_PRINT
-gboolean
-using_pdf_printer (GnomePrintConfig *config)
+static void
+ev_gui_sanitise_popup_position (GtkMenu *menu,
+                               GtkWidget *widget,
+                               gint *x,
+                               gint *y)
 {
-       const guchar *driver;
+       GdkScreen *screen = gtk_widget_get_screen (widget);
+       gint monitor_num;
+       GdkRectangle monitor;
+       GtkRequisition req;
 
-       driver = gnome_print_config_get (
-               config, (const guchar *)"Settings.Engine.Backend.Driver");
+       g_return_if_fail (widget != NULL);
 
-       if (driver) {
-               if (!strcmp ((const gchar *)driver, "gnome-print-pdf"))
-                       return TRUE;
-               else
-                       return FALSE;
-       }
+       gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
 
-       return FALSE;
-}
+       monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
+       gtk_menu_set_monitor (menu, monitor_num);
+       gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
 
-gboolean
-using_postscript_printer (GnomePrintConfig *config)
-{
-       const guchar *driver;
-       const guchar *transport;
-
-       driver = gnome_print_config_get (
-               config, (const guchar *)"Settings.Engine.Backend.Driver");
-
-       transport = gnome_print_config_get (
-               config, (const guchar *)"Settings.Transport.Backend");
-
-       if (driver) {
-               if (!strcmp ((const gchar *)driver, "gnome-print-ps"))
-                       return TRUE;
-               else
-                       return FALSE;
-       } else  if (transport) { /* these transports default to PostScript */
-               if (!strcmp ((const gchar *)transport, "CUPS"))
-                       return TRUE;
-               else if (!strcmp ((const gchar *)transport, "LPD"))
-                       return TRUE;
-               else if (!strcmp ((const gchar *)transport, "PAPI"))
-                       return TRUE;
-       }
-
-       return FALSE;
+       *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
+       *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
 }
 
-GnomePrintConfig *
-load_print_config_from_file (void)
+void
+ev_gui_menu_position_tree_selection (GtkMenu   *menu,
+                                    gint      *x,
+                                    gint      *y,
+                                    gboolean  *push_in,
+                                    gpointer  user_data)
 {
-       GnomePrintConfig *print_config = NULL;
-       char *file_name, *contents = NULL;
+       GtkTreeSelection *selection;
+       GList *selected_rows;
+       GtkTreeModel *model;
+       GtkTreeView *tree_view = GTK_TREE_VIEW (user_data);
+       GtkWidget *widget = GTK_WIDGET (user_data);
+       GtkRequisition req;
+       GtkAllocation allocation;
+       GdkRectangle visible;
+
+       gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
+       gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
+       gtk_widget_get_allocation (widget, &allocation);
+
+       *x += (allocation.width - req.width) / 2;
+
+       /* Add on height for the treeview title */
+       gtk_tree_view_get_visible_rect (tree_view, &visible);
+       *y += allocation.height - visible.height;
+
+       selection = gtk_tree_view_get_selection (tree_view);
+       selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);
+       if (selected_rows)
+       {
+               GdkRectangle cell_rect;
 
-       file_name = g_build_filename (ev_dot_dir (), PRINT_CONFIG_FILENAME,
-                                     NULL);
+               gtk_tree_view_get_cell_area (tree_view, selected_rows->data,
+                                            NULL, &cell_rect);
 
-       if (g_file_get_contents (file_name, &contents, NULL, NULL)) {
-               print_config = gnome_print_config_from_string (contents, 0);
-               g_free (contents);
-       }
+               *y += CLAMP (cell_rect.y + cell_rect.height, 0, visible.height);
 
-       if (print_config == NULL) {
-               print_config = gnome_print_config_default ();
+               g_list_foreach (selected_rows, (GFunc)gtk_tree_path_free, NULL);
+               g_list_free (selected_rows);
        }
 
-       g_free (file_name);
-
-       return print_config;
+       ev_gui_sanitise_popup_position (menu, widget, x, y);
 }
 
-void
-save_print_config_to_file (GnomePrintConfig *config)
+/**
+ * get_num_monitors: Get the number of user monitors.
+ * @window: optional GtkWindow to look at.
+ *
+ * Returns: Number of monitors, -1 if uncertain situation (like multiple screens)
+ */
+gint 
+get_num_monitors (GtkWindow *window)
 {
-       char *file_name, *str;
-
-       g_return_if_fail (config != NULL);
+       GdkDisplay *display; 
+       GdkScreen *screen;
+       gint num_screen;
+       
+       display = gdk_display_get_default ();
+       num_screen = gdk_display_get_n_screens (display);
+       
+       if (num_screen != 1)
+               return -1;
+       
+       if (window)
+               screen = gtk_window_get_screen (window);
+       else
+               screen = gdk_display_get_screen (display, 0);
+
+       return gdk_screen_get_n_monitors (screen);
+}
 
-       str = gnome_print_config_to_string (config, 0);
-       if (str == NULL) return;
+void           
+file_chooser_dialog_add_writable_pixbuf_formats (GtkFileChooser *chooser)
+{
+       GSList *pixbuf_formats = NULL;
+       GSList *iter;
+       GtkFileFilter *filter;
+       int i;
+  
+       filter = gtk_file_filter_new();
+       gtk_file_filter_set_name (filter, _("By extension"));
+       g_object_set_data (G_OBJECT(filter), "pixbuf-format", NULL);
+       gtk_file_chooser_add_filter (chooser, filter);
+
+       pixbuf_formats = gdk_pixbuf_get_formats ();
+
+       for (iter = pixbuf_formats; iter; iter = iter->next) {
+               GdkPixbufFormat *format = iter->data;
+
+               gchar *description, *name, *extensions;
+               gchar **extension_list, **mime_types;
+
+               if (gdk_pixbuf_format_is_disabled (format) ||
+                   !gdk_pixbuf_format_is_writable (format))
+                           continue;
+
+               name = gdk_pixbuf_format_get_description (format);
+               extension_list = gdk_pixbuf_format_get_extensions (format);
+               extensions = g_strjoinv (", ", extension_list);
+               g_strfreev (extension_list);
+               description = g_strdup_printf ("%s (%s)", name, extensions);
+
+               filter = gtk_file_filter_new ();
+               gtk_file_filter_set_name (filter, description);
+               g_object_set_data (G_OBJECT (filter), "pixbuf-format", format);
+               gtk_file_chooser_add_filter (chooser, filter);
+
+               g_free (description);
+               g_free (extensions);
+               g_free (name);
+
+               mime_types = gdk_pixbuf_format_get_mime_types (format);
+               for (i = 0; mime_types[i] != 0; i++)
+                       gtk_file_filter_add_mime_type (filter, mime_types[i]);
+               g_strfreev (mime_types);
+       }
 
-       file_name = g_build_filename (ev_dot_dir (),
-                                     PRINT_CONFIG_FILENAME,
-                                     NULL);
+       g_slist_free (pixbuf_formats);
+}
 
-       g_file_set_contents (file_name, str, -1, NULL);
+GdkPixbufFormat*
+get_gdk_pixbuf_format_by_extension (gchar *uri)
+{
+       GSList *pixbuf_formats = NULL;
+       GSList *iter;
+       int i;
+
+       pixbuf_formats = gdk_pixbuf_get_formats ();
+
+       for (iter = pixbuf_formats; iter; iter = iter->next) {
+               gchar **extension_list;
+               GdkPixbufFormat *format = iter->data;
+               
+               if (gdk_pixbuf_format_is_disabled (format) ||
+                   !gdk_pixbuf_format_is_writable (format))
+                           continue;
+
+               extension_list = gdk_pixbuf_format_get_extensions (format);
+
+               for (i = 0; extension_list[i] != 0; i++) {
+                       if (g_str_has_suffix (uri, extension_list[i])) {
+                               g_slist_free (pixbuf_formats);
+                               g_strfreev (extension_list);
+                               return format;
+                       }
+               }
+               g_strfreev (extension_list);
+       }
 
-       g_free (file_name);
-       g_free (str);
+       g_slist_free (pixbuf_formats);
+       return NULL;
 }
-#endif /* WITH_GNOME_PRINT */
-