home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / resolution_calibrate.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  9.5 KB  |  275 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <gtk/gtk.h>
  22.  
  23. #include "apptypes.h"
  24.  
  25. #include "appenv.h"
  26. #include "gdisplay_ops.h"
  27. #include "gimprc.h"
  28. #include "gimpui.h"
  29. #include "resolution_calibrate.h"
  30. #include "unitrc.h"
  31.  
  32. #include "libgimp/gimpenv.h"
  33. #include "libgimp/gimplimits.h"
  34. #include "libgimp/gimpmath.h"
  35.  
  36. #include "libgimp/gimpintl.h"
  37.  
  38. #define SET_STYLE(widget, style)  if (style) gtk_widget_set_style (widget, style)
  39.  
  40. static GtkWidget *calibrate_entry = NULL;
  41. static gdouble    calibrate_xres  = 1.0;
  42. static gdouble    calibrate_yres  = 1.0;
  43. static gint       ruler_width     = 1;
  44. static gint       ruler_height    = 1;
  45.  
  46. static void
  47. resolution_calibrate_ok (GtkWidget *button,
  48.              gpointer   data)
  49. {
  50.   GtkWidget *resolution_entry;
  51.   GtkWidget *chain_button;
  52.   gdouble x, y;
  53.  
  54.   resolution_entry = gtk_object_get_user_data (GTK_OBJECT (data));
  55.  
  56.   x = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (calibrate_entry), 0);
  57.   y = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (calibrate_entry), 1);
  58.  
  59.   calibrate_xres = (gdouble)ruler_width  * calibrate_xres / x;
  60.   calibrate_yres = (gdouble)ruler_height * calibrate_yres / y;
  61.   
  62.   chain_button = gtk_object_get_data (GTK_OBJECT (resolution_entry), "chain_button");
  63.   if (chain_button && 
  64.       ABS (x -y) > GIMP_MIN_RESOLUTION)
  65.     gimp_chain_button_set_active (GIMP_CHAIN_BUTTON (chain_button), FALSE);
  66.  
  67.   gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (resolution_entry), 0, calibrate_xres);
  68.   gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (resolution_entry), 1, calibrate_yres);
  69.  
  70.   gtk_widget_destroy (GTK_WIDGET (data));
  71. }
  72.  
  73. /**
  74.  * resolution_calibrate_dialog:
  75.  * @resolution_entry: a GimpSizeEntry to connect the dialog to
  76.  * @dialog_style: a GtkStyle for the main dialog (used by the user_installation_dialog)
  77.  * @ruler_style: a GtkStyle for the rulers and the entry area (used by the 
  78.  *               user_installation_dialog)
  79.  * @expose_callback: an "expose_event" handler used by the user_installation_dialog
  80.  * 
  81.  * Displays a dialog that allows the user to interactively determine her monitor
  82.  * resolution. This dialog runs it's own GTK main loop and is connected to a 
  83.  * GimpSizeEntry handling the resolution to be set. The style and callback parameters
  84.  * are supposed to be only used by the user_installation_dialog.
  85.  */
  86. void
  87. resolution_calibrate_dialog (GtkWidget     *resolution_entry,
  88.                  GtkStyle      *dialog_style,
  89.                  GtkStyle      *ruler_style,
  90.                  GtkSignalFunc  expose_callback)
  91. {
  92.   GtkWidget *dialog;
  93.   GtkWidget *table;
  94.   GtkWidget *ebox;
  95.   GtkWidget *vbox;
  96.   GtkWidget *hbox;
  97.   GtkWidget *darea;
  98.   GtkWidget *ruler;
  99.   GtkWidget *label;
  100.   GList     *list;
  101.  
  102.   g_return_if_fail (GIMP_IS_SIZE_ENTRY (resolution_entry));
  103.   
  104.   /*  this dialog can only exist once  */
  105.   if (calibrate_entry)
  106.     return;  
  107.   
  108.   dialog = gimp_dialog_new (_("Calibrate Monitor Resolution"),
  109.                 "calibrate_resolution",
  110.                 NULL, NULL,
  111.                 GTK_WIN_POS_CENTER,
  112.                 FALSE, FALSE, FALSE,
  113.  
  114.                 _("OK"), resolution_calibrate_ok,
  115.                 NULL, NULL, NULL, TRUE, FALSE,
  116.                 _("Cancel"), gtk_widget_destroy,
  117.                 NULL, 1, NULL, FALSE, TRUE,
  118.  
  119.                 NULL);
  120.  
  121.   gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
  122.               GTK_SIGNAL_FUNC (gtk_main_quit),
  123.               NULL);
  124.   gtk_object_set_user_data (GTK_OBJECT (dialog), resolution_entry);
  125.   gtk_signal_connect_object_while_alive (GTK_OBJECT (resolution_entry), "destroy",
  126.                      GTK_SIGNAL_FUNC (gtk_widget_destroy),
  127.                      GTK_OBJECT (dialog));
  128.   gtk_signal_connect_object_while_alive (GTK_OBJECT (resolution_entry), "unmap",
  129.                      GTK_SIGNAL_FUNC (gtk_widget_destroy),
  130.                      GTK_OBJECT (dialog));
  131.  
  132.   SET_STYLE (dialog, dialog_style);
  133.   gimp_dialog_set_icon (GTK_WINDOW (dialog));
  134.   gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), 8);
  135.  
  136.   ruler_width  = gdk_screen_width ();
  137.   ruler_height = gdk_screen_height ();
  138.  
  139.   ruler_width  = ruler_width - 300 - (ruler_width % 100);
  140.   ruler_height = ruler_height - 300 - (ruler_height % 100);
  141.  
  142.   table = gtk_table_new (4, 4, FALSE);
  143.   gtk_container_set_border_width (GTK_CONTAINER (table), 16);
  144.   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), table);
  145.   gtk_widget_show (table);
  146.   
  147.   ruler = gtk_hruler_new ();
  148.   SET_STYLE (ruler, ruler_style);
  149.   gtk_widget_set_usize (ruler, ruler_width, 32);
  150.   gtk_ruler_set_range (GTK_RULER (ruler), 0, ruler_width, 0, ruler_width);
  151.   gtk_table_attach (GTK_TABLE (table), ruler, 1, 3, 0, 1,
  152.             GTK_SHRINK, GTK_SHRINK, 0, 0);
  153.   gtk_widget_show (ruler);
  154.  
  155.   ruler = gtk_vruler_new ();
  156.   SET_STYLE (ruler, ruler_style);
  157.   gtk_widget_set_usize (ruler, 32, ruler_height);
  158.   gtk_ruler_set_range (GTK_RULER (ruler), 0, ruler_height, 0, ruler_height);
  159.   gtk_table_attach (GTK_TABLE (table), ruler, 0, 1, 1, 3,
  160.             GTK_SHRINK, GTK_SHRINK, 0, 0);
  161.   gtk_widget_show (ruler);
  162.  
  163.   ebox = gtk_event_box_new ();
  164.   SET_STYLE (ebox, ruler_style);  
  165.   gtk_table_attach (GTK_TABLE (table), ebox, 1, 2, 1, 2,
  166.             GTK_SHRINK, GTK_SHRINK, 0, 0);
  167.   gtk_widget_show (ebox);
  168.  
  169.   table = gtk_table_new (3, 3, FALSE);
  170.   gtk_container_add (GTK_CONTAINER (ebox), table);
  171.   gtk_widget_show (table);
  172.  
  173.   darea = gtk_drawing_area_new ();
  174.   SET_STYLE (darea, dialog_style);  
  175.   gtk_drawing_area_size (GTK_DRAWING_AREA (darea), 16, 16);
  176.   if (expose_callback)
  177.   gtk_signal_connect_after (GTK_OBJECT (darea), "expose_event",
  178.                 GTK_SIGNAL_FUNC (expose_callback),
  179.                 (gpointer) GTK_CORNER_TOP_LEFT);
  180.   gtk_table_attach (GTK_TABLE (table), darea, 0, 1, 0, 1,
  181.             GTK_SHRINK, GTK_SHRINK, 0, 0);
  182.   gtk_widget_show (darea);
  183.  
  184.   darea = gtk_drawing_area_new ();
  185.   SET_STYLE (darea, dialog_style);  
  186.   gtk_drawing_area_size (GTK_DRAWING_AREA (darea), 16, 16);
  187.   if (expose_callback)
  188.     gtk_signal_connect_after (GTK_OBJECT (darea), "expose_event",
  189.                   GTK_SIGNAL_FUNC (expose_callback),
  190.                   (gpointer) GTK_CORNER_BOTTOM_LEFT);
  191.   gtk_table_attach (GTK_TABLE (table), darea, 0, 1, 2, 3,
  192.             GTK_SHRINK, GTK_SHRINK, 0, 0);
  193.   gtk_widget_show (darea);
  194.  
  195.   darea = gtk_drawing_area_new ();
  196.   SET_STYLE (darea, dialog_style);  
  197.   gtk_drawing_area_size (GTK_DRAWING_AREA (darea), 16, 16);
  198.   if (expose_callback)
  199.     gtk_signal_connect_after (GTK_OBJECT (darea), "expose_event",
  200.                   GTK_SIGNAL_FUNC (expose_callback),
  201.                   (gpointer) GTK_CORNER_TOP_RIGHT);
  202.   gtk_table_attach (GTK_TABLE (table), darea, 2, 3, 0, 1,
  203.             GTK_SHRINK, GTK_SHRINK, 0, 0);
  204.   gtk_widget_show (darea);
  205.  
  206.   darea = gtk_drawing_area_new ();
  207.   SET_STYLE (darea, dialog_style);  
  208.   gtk_drawing_area_size (GTK_DRAWING_AREA (darea), 16, 16);
  209.   if (expose_callback)
  210.     gtk_signal_connect_after (GTK_OBJECT (darea), "expose_event",
  211.                   GTK_SIGNAL_FUNC (expose_callback),
  212.                   (gpointer) GTK_CORNER_BOTTOM_RIGHT);
  213.   gtk_table_attach (GTK_TABLE (table), darea, 2, 3, 2, 3,
  214.             GTK_SHRINK, GTK_SHRINK, 0, 0);
  215.   gtk_widget_show (darea);
  216.  
  217.   vbox = gtk_vbox_new (FALSE, 16);
  218.   gtk_table_attach_defaults (GTK_TABLE (table), vbox, 1, 2, 1, 2);
  219.   gtk_widget_show (vbox);
  220.   
  221.   label = gtk_label_new (_("Measure the rulers and enter their lengths below."));
  222.   SET_STYLE (label, ruler_style);
  223.   gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
  224.   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  225.   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  226.   gtk_widget_show (label);
  227.  
  228.   hbox = gtk_hbox_new (FALSE, 0);
  229.   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  230.   gtk_widget_show (hbox);
  231.  
  232.   calibrate_xres = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (resolution_entry), 0);
  233.   calibrate_yres = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (resolution_entry), 1);
  234.  
  235.   calibrate_entry =
  236.     gimp_coordinates_new  (GIMP_UNIT_INCH, "%p",
  237.                FALSE, FALSE, 75,
  238.                GIMP_SIZE_ENTRY_UPDATE_SIZE,
  239.                FALSE,
  240.                FALSE,
  241.                _("Horizontal:"),
  242.                ruler_width,
  243.                calibrate_xres,
  244.                1, GIMP_MAX_IMAGE_SIZE,
  245.                0, 0,
  246.                _("Vertical:"),
  247.                ruler_height,
  248.                calibrate_yres,
  249.                1, GIMP_MAX_IMAGE_SIZE,
  250.                0, 0);
  251.   gtk_widget_hide (GTK_WIDGET (GIMP_COORDINATES_CHAINBUTTON (calibrate_entry)));
  252.   gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
  253.               GTK_SIGNAL_FUNC (gtk_widget_destroyed),
  254.               &calibrate_entry);
  255.  
  256.   if (ruler_style)
  257.     {
  258.       for (list = GTK_TABLE (calibrate_entry)->children;
  259.        list;
  260.        list = g_list_next (list))
  261.     {
  262.       GtkTableChild *child = (GtkTableChild *) list->data;
  263.       
  264.       if (child && GTK_IS_LABEL (child->widget))
  265.         SET_STYLE (GTK_WIDGET (child->widget), ruler_style);
  266.     }
  267.     }
  268.   gtk_box_pack_end (GTK_BOX (hbox), calibrate_entry, FALSE, FALSE, 0);
  269.   gtk_widget_show (calibrate_entry);
  270.   
  271.   gtk_widget_show (dialog);
  272.  
  273.   gtk_main ();
  274. }
  275.