Index: postscript.c
===================================================================
RCS file: /cvs/gnome/gimp/plug-ins/common/postscript.c,v
retrieving revision 1.98
diff -u -p -r1.98 postscript.c
--- postscript.c	2 Jul 2005 19:58:49 -0000	1.98
+++ postscript.c	13 Jul 2005 17:01:57 -0000
@@ -257,9 +257,11 @@ static void   dither_grey      (guchar  
 
 /* Dialog-handling */
 
-static gboolean  load_dialog               (void);
-static void      load_pages_entry_callback (GtkWidget *widget,
-                                            gpointer   data);
+static gint32    countPSPages              (const gchar *filename);
+static gboolean  load_dialog               (const gchar *filename,
+                                            gboolean     loadPDF);
+static void      load_pages_entry_callback (GtkWidget   *widget,
+                                            gpointer     data);
 
 typedef struct
 {
@@ -292,6 +294,7 @@ static void compress_packbits (int      
 static guint32 ascii85_buf;
 static int ascii85_len = 0;
 static int ascii85_linewidth = 0;
+static GimpPageSelectorTarget ps_pagemode = GIMP_PAGE_SELECTOR_TARGET_IMAGES;
 
 static void
 ascii85_init (void)
@@ -761,7 +764,7 @@ run (const gchar      *name,
           /*  Possibly retrieve data  */
           gimp_get_data ("file_ps_load", &plvals);
 
-          if (! load_dialog ())
+          if (! load_dialog (param[1].data.d_string,!strcmp (name, "file_pdf_load")))
 	    status = GIMP_PDB_CANCEL;
           break;
 
@@ -1070,6 +1073,25 @@ load_image (const gchar *filename)
 
   ps_close (ifp, ChildPid);
 
+  if (ps_pagemode == GIMP_PAGE_SELECTOR_TARGET_LAYERS)
+  {
+    for (k = 0; k < n_images; k++)
+    {
+      if (k == 0)
+      {
+        image_ID = image_list[0];
+      }
+      else
+      {
+        gint32 current_layer = 0;
+        current_layer = gimp_layer_new_from_drawable (gimp_image_get_active_drawable(image_list[k]), image_ID);
+        gimp_layer_add_alpha (current_layer);
+        gimp_image_add_layer (image_ID, current_layer, -1);
+      }
+    }
+  }
+  else
+  {
   /* Display images in reverse order. The last will be displayed by GIMP itself*/
   if (l_run_mode != GIMP_RUN_NONINTERACTIVE)
     {
@@ -1078,6 +1100,8 @@ load_image (const gchar *filename)
     }
 
   image_ID = (n_images > 0) ? image_list[0] : -1;
+  }
+
   g_free (image_list);
 
   return image_ID;
@@ -2904,8 +2928,44 @@ save_rgb (FILE   *ofp,
 
 /*  Load interface functions  */
 
+static gint32
+countPSPages (const gchar *filename)
+{
+  FILE *psfile = NULL;
+  gchar *cLine = NULL;
+  gint32 nPages = 0;
+
+  if (!g_file_test (filename, G_FILE_TEST_EXISTS))
+  {
+    g_warning ("File does not exist!");
+    return 0;
+  }
+
+  if (strncmp (g_utf8_strup (filename+(strlen(filename)-3),3),"EPS",3) == 0)
+    return 1;
+
+  psfile = g_fopen (filename, "r");
+  if (psfile == NULL)
+    g_warning ("Could not open file!");
+  else
+  {
+    cLine = g_malloc0 (1024 * sizeof(gchar));
+    while (nPages == 0 && !feof (psfile))
+    {
+      fgets(cLine,1023,psfile);
+      if (strncmp(cLine+2,"Pages:",6) == 0)
+        sscanf (cLine+strlen("%%Pages:"),"%d",&nPages);
+    }
+    g_free (cLine);
+    fclose (psfile);
+  }
+
+  return nPages;
+}
+
 static gboolean
-load_dialog (void)
+load_dialog (const gchar *filename,
+             gboolean loadPDF)
 {
   GtkWidget *dialog;
   GtkWidget *main_vbox;
@@ -2915,10 +2975,17 @@ load_dialog (void)
   GtkWidget *table;
   GtkWidget *spinbutton;
   GtkObject *adj;
-  GtkWidget *entry;
+  GtkWidget *entry = NULL;
   GtkWidget *toggle;
+  GtkWidget *selector = NULL;
+  gint32 page_count;
+  gchar *range = NULL;
   gboolean   run;
 
+  page_count = countPSPages(filename);
+  if (page_count == 0)
+    return FALSE;
+
   gimp_ui_init ("ps", FALSE);
 
   dialog = gimp_dialog_new (_("Load PostScript"), "ps",
@@ -2938,9 +3005,18 @@ load_dialog (void)
   main_vbox = gtk_vbox_new (FALSE, 12);
   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), main_vbox,
-                      FALSE, FALSE, 0);
+                      TRUE, TRUE, 0);
   gtk_widget_show (main_vbox);
 
+  if (page_count > 1)
+  {
+    selector = gimp_page_selector_new ();
+    gtk_box_pack_start (GTK_BOX (main_vbox), selector, TRUE, TRUE, 0);
+    gimp_page_selector_set_n_pages (GIMP_PAGE_SELECTOR (selector), page_count);
+    gimp_page_selector_set_target (GIMP_PAGE_SELECTOR (selector), ps_pagemode);
+    gtk_widget_show (selector);
+  }
+
   hbox = gtk_hbox_new (TRUE, 12);
   gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
   gtk_widget_show (hbox);
@@ -2986,17 +3062,20 @@ load_dialog (void)
                     G_CALLBACK (gimp_int_adjustment_update),
                     &plvals.height);
 
-  entry = gtk_entry_new ();
-  gtk_widget_set_size_request (entry, 80, -1);
-  gtk_entry_set_text (GTK_ENTRY (entry), plvals.pages);
-  gimp_table_attach_aligned (GTK_TABLE (table), 0, 3,
-			     _("Pages:"), 0.0, 0.5,
-			     entry, 1, FALSE);
-  g_signal_connect (entry, "changed",
-                    G_CALLBACK (load_pages_entry_callback),
-                    NULL);
-  gimp_help_set_help_data (GTK_WIDGET (entry),
-                           _("Pages to load (e.g.: 1-4 or 1,3,5-7)"), NULL);
+  if (loadPDF)
+  {
+    entry = gtk_entry_new ();
+    gtk_widget_set_size_request (entry, 80, -1);
+    gtk_entry_set_text (GTK_ENTRY (entry), plvals.pages);
+    gimp_table_attach_aligned (GTK_TABLE (table), 0, 3,
+	 		       _("Pages:"), 0.0, 0.5,
+			       entry, 1, FALSE);
+    g_signal_connect (entry, "changed",
+                      G_CALLBACK (load_pages_entry_callback),
+                      NULL);
+    gimp_help_set_help_data (GTK_WIDGET (entry),
+                             _("Pages to load (e.g.: 1-4 or 1,3,5-7)"), NULL);
+  }
 
   toggle = gtk_check_button_new_with_label (_("Try Bounding Box"));
   gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
@@ -3056,6 +3135,31 @@ load_dialog (void)
 
   run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
 
+  if (page_count > 1)
+  {
+    range = gimp_page_selector_get_selected_range (GIMP_PAGE_SELECTOR (selector));
+
+    if (strlen(range) < 1)
+    {
+      gimp_page_selector_select_all (GIMP_PAGE_SELECTOR (selector));
+      range = gimp_page_selector_get_selected_range (GIMP_PAGE_SELECTOR (selector));
+    }
+
+    gsize nelem = sizeof (plvals.pages);
+    strncpy (plvals.pages, range, nelem);
+    plvals.pages[strlen(range)] = '\0';
+
+    ps_pagemode = gimp_page_selector_get_target (GIMP_PAGE_SELECTOR (selector));
+  }
+  else if (loadPDF)
+    ps_pagemode = GIMP_PAGE_SELECTOR_TARGET_IMAGES;
+  else
+  {
+    strncpy (plvals.pages, "1", 1);
+    plvals.pages[1] = '\0';
+    ps_pagemode = GIMP_PAGE_SELECTOR_TARGET_IMAGES;
+  }
+
   gtk_widget_destroy (dialog);
 
   return run;
