resolution-calibrate-dialog.c 9.81 KB
Newer Older
Sven Neumann's avatar
Sven Neumann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
Sven Neumann's avatar
Sven Neumann committed
18

Sven Neumann's avatar
Sven Neumann committed
19
20
#include "config.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
30

#include "libgimp/gimpintl.h"

31

32
33
#define SET_STYLE(widget, style) \
        if (style) gtk_widget_modify_style (widget, style)
Sven Neumann's avatar
Sven Neumann committed
34

Michael Natterer's avatar
Michael Natterer committed
35

Sven Neumann's avatar
Sven Neumann committed
36
37
38
39
40
41
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
42

Sven Neumann's avatar
Sven Neumann committed
43
44
45
46
47
48
49
50
static void
resolution_calibrate_ok (GtkWidget *button,
			 gpointer   data)
{
  GtkWidget *resolution_entry;
  GtkWidget *chain_button;
  gdouble x, y;

51
  resolution_entry = g_object_get_data (G_OBJECT (data), "resolution-entry");
Sven Neumann's avatar
Sven Neumann committed
52
53
54
55
56
57
58

  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;
  
59
60
  chain_button = g_object_get_data (G_OBJECT (resolution_entry), 
                                    "chain_button");
Sven Neumann's avatar
Sven Neumann committed
61
62
63
64
  if (chain_button && 
      ABS (x -y) > GIMP_MIN_RESOLUTION)
    gimp_chain_button_set_active (GIMP_CHAIN_BUTTON (chain_button), FALSE);

65
66
67
68
  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);
Sven Neumann's avatar
Sven Neumann committed
69
70
71
72
73
74

  gtk_widget_destroy (GTK_WIDGET (data));
}

/**
 * resolution_calibrate_dialog:
Michael Natterer's avatar
Michael Natterer committed
75
 * @resolution_entry: a #GimpSizeEntry to connect the dialog to
76
 * @pixbuf:           an optional #GdkPixbuf for the upper left corner
Michael Natterer's avatar
Michael Natterer committed
77
78
79
80
81
82
83
84
85
86
87
88
89
 * @dialog_style:     a #GtkStyle for the main dialog (used by the
 *                    user_installation_dialog)
 * @ruler_style:      a #GtkStyle for the rulers and the entry area
 *                    (used by the user_installation_dialog)
 * @expose_callback:  an "expose_event" handler used by the
 *                    user_installation_dialog
 *
 * Displays a dialog that allows the user to interactively determine
 * her monitor resolution. This dialog runs it's own GTK main loop and
 * is connected to a #GimpSizeEntry handling the resolution to be
 * set. The style and callback parameters are supposed to be only used
 * by the user_installation_dialog.
 **/
Sven Neumann's avatar
Sven Neumann committed
90
void
91
resolution_calibrate_dialog (GtkWidget  *resolution_entry,
92
                             GdkPixbuf  *pixbuf,
93
94
95
			     GtkRcStyle *dialog_style,
			     GtkRcStyle *ruler_style,
			     GCallback   expose_callback)
Sven Neumann's avatar
Sven Neumann committed
96
97
98
99
100
101
102
103
104
105
106
107
{
  GtkWidget *dialog;
  GtkWidget *table;
  GtkWidget *ebox;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *darea;
  GtkWidget *ruler;
  GtkWidget *label;
  GList     *list;

  g_return_if_fail (GIMP_IS_SIZE_ENTRY (resolution_entry));
108
  g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
Sven Neumann's avatar
Sven Neumann committed
109
110
111
112
113
114
115
116
117
118
119
  
  /*  this dialog can only exist once  */
  if (calibrate_entry)
    return;  
  
  dialog = gimp_dialog_new (_("Calibrate Monitor Resolution"),
			    "calibrate_resolution",
			    NULL, NULL,
			    GTK_WIN_POS_CENTER,
			    FALSE, FALSE, FALSE,

120
			    GTK_STOCK_CANCEL, gtk_widget_destroy,
Sven Neumann's avatar
Sven Neumann committed
121
122
			    NULL, 1, NULL, FALSE, TRUE,

123
124
125
			    GTK_STOCK_OK, resolution_calibrate_ok,
			    NULL, NULL, NULL, TRUE, FALSE,

Sven Neumann's avatar
Sven Neumann committed
126
127
			    NULL);

128
129
130
  g_signal_connect (G_OBJECT (dialog), "destroy",
                    G_CALLBACK (gtk_main_quit),
                    NULL);
131
  g_object_set_data (G_OBJECT (dialog), "resolution-entry", resolution_entry);
132
133
134
135
136
137
  g_signal_connect_object (G_OBJECT (resolution_entry), "destroy",
                           G_CALLBACK (gtk_widget_destroy),
                           G_OBJECT (dialog), G_CONNECT_SWAPPED);
  g_signal_connect_object (G_OBJECT (resolution_entry), "unmap",
                           G_CALLBACK (gtk_widget_destroy),
                           G_OBJECT (dialog), G_CONNECT_SWAPPED);
Sven Neumann's avatar
Sven Neumann committed
138
139
140
141
142
143
144

  SET_STYLE (dialog, dialog_style);
  gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), 8);

  ruler_width  = gdk_screen_width ();
  ruler_height = gdk_screen_height ();

145
  ruler_width  = ruler_width  - 300 - (ruler_width  % 100);
Sven Neumann's avatar
Sven Neumann committed
146
147
148
  ruler_height = ruler_height - 300 - (ruler_height % 100);

  table = gtk_table_new (4, 4, FALSE);
149
  gtk_container_set_border_width (GTK_CONTAINER (table), 8);
Sven Neumann's avatar
Sven Neumann committed
150
151
152
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), table);
  gtk_widget_show (table);
  
153
154
155
156
157
158
159
160
  if (pixbuf)
    {
      GtkWidget *image = gtk_image_new_from_pixbuf (pixbuf);
      gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 1,
                        GTK_SHRINK, GTK_SHRINK, 4, 4);
      gtk_widget_show (image);
    }

Sven Neumann's avatar
Sven Neumann committed
161
162
  ruler = gtk_hruler_new ();
  SET_STYLE (ruler, ruler_style);
163
  gtk_widget_set_size_request (ruler, ruler_width, 32);
Sven Neumann's avatar
Sven Neumann committed
164
165
166
167
168
169
170
  gtk_ruler_set_range (GTK_RULER (ruler), 0, ruler_width, 0, ruler_width);
  gtk_table_attach (GTK_TABLE (table), ruler, 1, 3, 0, 1,
		    GTK_SHRINK, GTK_SHRINK, 0, 0);
  gtk_widget_show (ruler);

  ruler = gtk_vruler_new ();
  SET_STYLE (ruler, ruler_style);
171
  gtk_widget_set_size_request (ruler, 32, ruler_height);
Sven Neumann's avatar
Sven Neumann committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  gtk_ruler_set_range (GTK_RULER (ruler), 0, ruler_height, 0, ruler_height);
  gtk_table_attach (GTK_TABLE (table), ruler, 0, 1, 1, 3,
		    GTK_SHRINK, GTK_SHRINK, 0, 0);
  gtk_widget_show (ruler);

  ebox = gtk_event_box_new ();
  SET_STYLE (ebox, ruler_style);  
  gtk_table_attach (GTK_TABLE (table), ebox, 1, 2, 1, 2,
		    GTK_SHRINK, GTK_SHRINK, 0, 0);
  gtk_widget_show (ebox);

  table = gtk_table_new (3, 3, FALSE);
  gtk_container_add (GTK_CONTAINER (ebox), table);
  gtk_widget_show (table);

  darea = gtk_drawing_area_new ();
  SET_STYLE (darea, dialog_style);  
189
  gtk_widget_set_size_request (darea, 16, 16);
Sven Neumann's avatar
Sven Neumann committed
190
  if (expose_callback)
191
192
193
    g_signal_connect (G_OBJECT (darea), "expose_event",
                      G_CALLBACK (expose_callback),
                      GINT_TO_POINTER (GTK_CORNER_TOP_LEFT));
Sven Neumann's avatar
Sven Neumann committed
194
195
196
197
198
199
  gtk_table_attach (GTK_TABLE (table), darea, 0, 1, 0, 1,
		    GTK_SHRINK, GTK_SHRINK, 0, 0);
  gtk_widget_show (darea);

  darea = gtk_drawing_area_new ();
  SET_STYLE (darea, dialog_style);  
200
  gtk_widget_set_size_request (darea, 16, 16);
Sven Neumann's avatar
Sven Neumann committed
201
  if (expose_callback)
202
203
204
    g_signal_connect (G_OBJECT (darea), "expose_event",
                      G_CALLBACK (expose_callback),
                      GINT_TO_POINTER (GTK_CORNER_BOTTOM_LEFT));
Sven Neumann's avatar
Sven Neumann committed
205
206
207
208
209
210
  gtk_table_attach (GTK_TABLE (table), darea, 0, 1, 2, 3,
		    GTK_SHRINK, GTK_SHRINK, 0, 0);
  gtk_widget_show (darea);

  darea = gtk_drawing_area_new ();
  SET_STYLE (darea, dialog_style);  
211
  gtk_widget_set_size_request (darea, 16, 16);
Sven Neumann's avatar
Sven Neumann committed
212
  if (expose_callback)
213
214
215
    g_signal_connect (G_OBJECT (darea), "expose_event",
                      G_CALLBACK (expose_callback),
                      GINT_TO_POINTER (GTK_CORNER_TOP_RIGHT));
Sven Neumann's avatar
Sven Neumann committed
216
217
218
219
220
221
  gtk_table_attach (GTK_TABLE (table), darea, 2, 3, 0, 1,
		    GTK_SHRINK, GTK_SHRINK, 0, 0);
  gtk_widget_show (darea);

  darea = gtk_drawing_area_new ();
  SET_STYLE (darea, dialog_style);  
222
  gtk_widget_set_size_request (darea, 16, 16);
Sven Neumann's avatar
Sven Neumann committed
223
  if (expose_callback)
224
225
226
    g_signal_connect (G_OBJECT (darea), "expose_event",
                      G_CALLBACK (expose_callback),
                      GINT_TO_POINTER (GTK_CORNER_BOTTOM_RIGHT));
Sven Neumann's avatar
Sven Neumann committed
227
228
229
230
231
232
233
234
  gtk_table_attach (GTK_TABLE (table), darea, 2, 3, 2, 3,
		    GTK_SHRINK, GTK_SHRINK, 0, 0);
  gtk_widget_show (darea);

  vbox = gtk_vbox_new (FALSE, 16);
  gtk_table_attach_defaults (GTK_TABLE (table), vbox, 1, 2, 1, 2);
  gtk_widget_show (vbox);
  
235
236
  label = 
    gtk_label_new (_("Measure the rulers and enter their lengths below."));
Sven Neumann's avatar
Sven Neumann committed
237
238
239
240
241
242
243
244
245
246
  SET_STYLE (label, ruler_style);
  gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  hbox = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

247
248
249
250
  calibrate_xres = 
    gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (resolution_entry), 0);
  calibrate_yres = 
    gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (resolution_entry), 1);
Sven Neumann's avatar
Sven Neumann committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

  calibrate_entry =
    gimp_coordinates_new  (GIMP_UNIT_INCH, "%p",
			   FALSE, FALSE, 75,
			   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);
  gtk_widget_hide (GTK_WIDGET (GIMP_COORDINATES_CHAINBUTTON (calibrate_entry)));
269
270
271
  g_signal_connect (G_OBJECT (dialog), "destroy",
                    G_CALLBACK (gtk_widget_destroyed),
                    &calibrate_entry);
Sven Neumann's avatar
Sven Neumann committed
272
273
274
275
276
277
278
279

  if (ruler_style)
    {
      for (list = GTK_TABLE (calibrate_entry)->children;
	   list;
	   list = g_list_next (list))
	{
	  GtkTableChild *child = (GtkTableChild *) list->data;
Michael Natterer's avatar
Michael Natterer committed
280

Sven Neumann's avatar
Sven Neumann committed
281
282
283
284
285
286
	  if (child && GTK_IS_LABEL (child->widget))
	    SET_STYLE (GTK_WIDGET (child->widget), ruler_style);
	}
    }
  gtk_box_pack_end (GTK_BOX (hbox), calibrate_entry, FALSE, FALSE, 0);
  gtk_widget_show (calibrate_entry);
Michael Natterer's avatar
Michael Natterer committed
287

Sven Neumann's avatar
Sven Neumann committed
288
289
290
291
  gtk_widget_show (dialog);

  gtk_main ();
}