resolution-calibrate-dialog.c 6.85 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Sven Neumann's avatar
Sven Neumann committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Sven Neumann's avatar
Sven Neumann committed
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
Sven Neumann's avatar
Sven Neumann committed
7 8 9 10 11 12 13 14
 * (at your option) any later version.
 *
 * This program 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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Sven Neumann's avatar
Sven Neumann committed
16
 */
Sven Neumann's avatar
Sven Neumann committed
17

Sven Neumann's avatar
Sven Neumann committed
18 19
#include "config.h"

20
#include <gegl.h>
Sven Neumann's avatar
Sven Neumann committed
21 22
#include <gtk/gtk.h>

23
#include "libgimpmath/gimpmath.h"
24
#include "libgimpbase/gimpbase.h"
25
#include "libgimpwidgets/gimpwidgets.h"
26

Michael Natterer's avatar
Michael Natterer committed
27
#include "resolution-calibrate-dialog.h"
Sven Neumann's avatar
Sven Neumann committed
28

29
#include "gimp-intl.h"
Sven Neumann's avatar
Sven Neumann committed
30

31

Sven Neumann's avatar
Sven Neumann committed
32 33 34 35 36 37
static GtkWidget *calibrate_entry = NULL;
static gdouble    calibrate_xres  = 1.0;
static gdouble    calibrate_yres  = 1.0;
static gint       ruler_width     = 1;
static gint       ruler_height    = 1;

Michael Natterer's avatar
Michael Natterer committed
38

Sven Neumann's avatar
Sven Neumann committed
39 40
/**
 * resolution_calibrate_dialog:
Michael Natterer's avatar
Michael Natterer committed
41
 * @resolution_entry: a #GimpSizeEntry to connect the dialog to
42
 * @icon_name:        an optional icon-name for the upper left corner
Michael Natterer's avatar
Michael Natterer committed
43 44 45
 *
 * Displays a dialog that allows the user to interactively determine
 * her monitor resolution. This dialog runs it's own GTK main loop and
46
 * is connected to a #GimpSizeEntry handling the resolution to be set.
Michael Natterer's avatar
Michael Natterer committed
47
 **/
Sven Neumann's avatar
Sven Neumann committed
48
void
49 50
resolution_calibrate_dialog (GtkWidget   *resolution_entry,
                             const gchar *icon_name)
Sven Neumann's avatar
Sven Neumann committed
51
{
52
  GtkWidget    *dialog;
Simon Budig's avatar
Simon Budig committed
53
  GtkWidget    *grid;
54 55 56 57
  GtkWidget    *vbox;
  GtkWidget    *hbox;
  GtkWidget    *ruler;
  GtkWidget    *label;
58
  GdkRectangle  workarea;
Sven Neumann's avatar
Sven Neumann committed
59 60

  g_return_if_fail (GIMP_IS_SIZE_ENTRY (resolution_entry));
61
  g_return_if_fail (gtk_widget_get_realized (resolution_entry));
62

Sven Neumann's avatar
Sven Neumann committed
63 64
  /*  this dialog can only exist once  */
  if (calibrate_entry)
65 66
    return;

Sven Neumann's avatar
Sven Neumann committed
67
  dialog = gimp_dialog_new (_("Calibrate Monitor Resolution"),
68
                            "gimp-resolution-calibration",
69
                            gtk_widget_get_toplevel (resolution_entry),
70
                            GTK_DIALOG_DESTROY_WITH_PARENT,
71
                            NULL, NULL,
Sven Neumann's avatar
Sven Neumann committed
72

73 74
                            _("_Cancel"), GTK_RESPONSE_CANCEL,
                            _("_OK"),     GTK_RESPONSE_OK,
75

76
                            NULL);
Sven Neumann's avatar
Sven Neumann committed
77

78
  gimp_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
79 80 81 82
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

83
  gdk_monitor_get_workarea (gimp_widget_get_monitor (dialog), &workarea);
84

85 86
  ruler_width  = workarea.width  - 300 - (workarea.width  % 100);
  ruler_height = workarea.height - 300 - (workarea.height % 100);
Sven Neumann's avatar
Sven Neumann committed
87

Simon Budig's avatar
Simon Budig committed
88 89
  grid = gtk_grid_new ();
  gtk_container_set_border_width (GTK_CONTAINER (grid), 12);
90
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
Simon Budig's avatar
Simon Budig committed
91 92
                      grid, TRUE, TRUE, 0);
  gtk_widget_show (grid);
93

94
  if (icon_name)
95
    {
96 97
      GtkWidget *image = gtk_image_new_from_icon_name (icon_name,
                                                       GTK_ICON_SIZE_DIALOG);
98

Simon Budig's avatar
Simon Budig committed
99
      gtk_grid_attach (GTK_GRID (grid), image, 0, 0, 1, 1);
100 101 102
      gtk_widget_show (image);
    }

103
  ruler = gimp_ruler_new (GTK_ORIENTATION_HORIZONTAL);
104
  gtk_widget_set_size_request (ruler, ruler_width, 32);
105
  gimp_ruler_set_range (GIMP_RULER (ruler), 0, ruler_width, ruler_width);
Simon Budig's avatar
Simon Budig committed
106
  gtk_grid_attach (GTK_GRID (grid), ruler, 1, 0, 2, 1);
Sven Neumann's avatar
Sven Neumann committed
107 108
  gtk_widget_show (ruler);

109
  ruler = gimp_ruler_new (GTK_ORIENTATION_VERTICAL);
110
  gtk_widget_set_size_request (ruler, 32, ruler_height);
111
  gimp_ruler_set_range (GIMP_RULER (ruler), 0, ruler_height, ruler_height);
Simon Budig's avatar
Simon Budig committed
112
  gtk_grid_attach (GTK_GRID (grid), ruler, 0, 1, 1, 2);
Sven Neumann's avatar
Sven Neumann committed
113 114
  gtk_widget_show (ruler);

Michael Natterer's avatar
Michael Natterer committed
115
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
Simon Budig's avatar
Simon Budig committed
116
  gtk_grid_attach (GTK_GRID (grid), vbox, 1, 1, 1, 1);
Sven Neumann's avatar
Sven Neumann committed
117
  gtk_widget_show (vbox);
118 119

  label =
120
    gtk_label_new (_("Measure the rulers and enter their lengths:"));
Sven Neumann's avatar
Sven Neumann committed
121
  gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
122
  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
123 124 125 126
  gimp_label_set_attributes (GTK_LABEL (label),
                             PANGO_ATTR_SCALE,  PANGO_SCALE_LARGE,
                             PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD,
                             -1);
Sven Neumann's avatar
Sven Neumann committed
127 128 129
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

Michael Natterer's avatar
Michael Natterer committed
130
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
Sven Neumann's avatar
Sven Neumann committed
131 132 133
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

134
  calibrate_xres =
135
    gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (resolution_entry), 0);
136
  calibrate_yres =
137
    gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (resolution_entry), 1);
Sven Neumann's avatar
Sven Neumann committed
138 139 140

  calibrate_entry =
    gimp_coordinates_new  (GIMP_UNIT_INCH, "%p",
141 142 143 144 145 146 147 148 149 150 151 152 153 154
                           FALSE, FALSE, 10,
                           GIMP_SIZE_ENTRY_UPDATE_SIZE,
                           FALSE,
                           FALSE,
                           _("_Horizontal:"),
                           ruler_width,
                           calibrate_xres,
                           1, GIMP_MAX_IMAGE_SIZE,
                           0, 0,
                           _("_Vertical:"),
                           ruler_height,
                           calibrate_yres,
                           1, GIMP_MAX_IMAGE_SIZE,
                           0, 0);
Sven Neumann's avatar
Sven Neumann committed
155
  gtk_widget_hide (GTK_WIDGET (GIMP_COORDINATES_CHAINBUTTON (calibrate_entry)));
156
  g_signal_connect (dialog, "destroy",
157 158
                    G_CALLBACK (gtk_widget_destroyed),
                    &calibrate_entry);
Sven Neumann's avatar
Sven Neumann committed
159 160 161

  gtk_box_pack_end (GTK_BOX (hbox), calibrate_entry, FALSE, FALSE, 0);
  gtk_widget_show (calibrate_entry);
Michael Natterer's avatar
Michael Natterer committed
162

Sven Neumann's avatar
Sven Neumann committed
163 164
  gtk_widget_show (dialog);

165
  switch (gimp_dialog_run (GIMP_DIALOG (dialog)))
166 167 168 169 170 171 172 173 174 175 176 177 178 179
    {
    case GTK_RESPONSE_OK:
      {
        GtkWidget *chain_button;
        gdouble    x, y;

        x = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (calibrate_entry), 0);
        y = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (calibrate_entry), 1);

        calibrate_xres = (gdouble) ruler_width  * calibrate_xres / x;
        calibrate_yres = (gdouble) ruler_height * calibrate_yres / y;

        chain_button = GIMP_COORDINATES_CHAINBUTTON (resolution_entry);

180 181 182
        if (ABS (x - y) > GIMP_MIN_RESOLUTION)
          gimp_chain_button_set_active (GIMP_CHAIN_BUTTON (chain_button),
                                        FALSE);
183 184 185 186 187 188 189 190 191 192 193 194

        gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (resolution_entry),
                                    0, calibrate_xres);
        gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (resolution_entry),
                                    1, calibrate_yres);
      }

    default:
      break;
    }

  gtk_widget_destroy (dialog);
Sven Neumann's avatar
Sven Neumann committed
195
}