4 * Copyright 2001 Sun Microsystems Inc.
6 * Erwann Chenede <erwann.chenede@sun.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 #include "gdk.h" /* For gdk_rectangle_intersect() */
27 #include "gdkwindow.h"
28 #include "gdkscreen.h"
32 static void gdk_screen_class_init (GdkScreenClass *klass);
33 static void gdk_screen_init (GdkScreen *screen);
34 static void gdk_screen_dispose (GObject *object);
35 static void gdk_screen_finalize (GObject *object);
36 static void gdk_screen_set_property (GObject *object,
40 static void gdk_screen_get_property (GObject *object,
58 static guint signals[LAST_SIGNAL] = { 0 };
60 static gpointer parent_class = NULL;
63 gdk_screen_get_type (void)
65 static GType object_type = 0;
69 static const GTypeInfo object_info =
71 sizeof (GdkScreenClass),
73 (GBaseFinalizeFunc) NULL,
74 (GClassInitFunc) gdk_screen_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
79 (GInstanceInitFunc) gdk_screen_init,
82 object_type = g_type_register_static (G_TYPE_OBJECT,
83 g_intern_static_string ("GdkScreen"), &object_info, 0);
90 gdk_screen_class_init (GdkScreenClass *klass)
92 GObjectClass *object_class = G_OBJECT_CLASS (klass);
94 parent_class = g_type_class_peek_parent (klass);
96 object_class->dispose = gdk_screen_dispose;
97 object_class->finalize = gdk_screen_finalize;
98 object_class->set_property = gdk_screen_set_property;
99 object_class->get_property = gdk_screen_get_property;
101 g_object_class_install_property (object_class,
103 g_param_spec_pointer ("font-options",
105 P_("The default font options for the screen"),
106 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
107 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
109 g_object_class_install_property (object_class,
111 g_param_spec_double ("resolution",
112 P_("Font resolution"),
113 P_("The resolution for fonts on the screen"),
117 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
118 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
121 * GdkScreen::size-changed:
122 * @screen: the object on which the signal is emitted
124 * The ::size_changed signal is emitted when the pixel width or
125 * height of a screen changes.
129 signals[SIZE_CHANGED] =
130 g_signal_new (g_intern_static_string ("size_changed"),
131 G_OBJECT_CLASS_TYPE (klass),
133 G_STRUCT_OFFSET (GdkScreenClass, size_changed),
135 g_cclosure_marshal_VOID__VOID,
141 gdk_screen_init (GdkScreen *screen)
143 screen->resolution = -1.;
147 gdk_screen_dispose (GObject *object)
149 GdkScreen *screen = GDK_SCREEN (object);
152 for (i = 0; i < 32; ++i)
154 if (screen->exposure_gcs[i])
156 g_object_unref (screen->exposure_gcs[i]);
157 screen->exposure_gcs[i] = NULL;
160 if (screen->normal_gcs[i])
162 g_object_unref (screen->normal_gcs[i]);
163 screen->normal_gcs[i] = NULL;
167 G_OBJECT_CLASS (parent_class)->dispose (object);
171 gdk_screen_finalize (GObject *object)
173 GdkScreen *screen = GDK_SCREEN (object);
175 if (screen->font_options)
176 cairo_font_options_destroy (screen->font_options);
178 G_OBJECT_CLASS (parent_class)->finalize (object);
182 _gdk_screen_close (GdkScreen *screen)
184 g_return_if_fail (GDK_IS_SCREEN (screen));
188 screen->closed = TRUE;
189 g_object_run_dispose (G_OBJECT (screen));
193 /* Fallback used when the monitor "at" a point or window
197 get_nearest_monitor (GdkScreen *screen,
201 gint num_monitors, i;
202 gint nearest_dist = G_MAXINT;
203 gint nearest_monitor = 0;
205 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
207 num_monitors = gdk_screen_get_n_monitors (screen);
209 for (i = 0; i < num_monitors; i++)
211 GdkRectangle monitor;
214 gdk_screen_get_monitor_geometry (screen, i, &monitor);
217 dist_x = monitor.x - x;
218 else if (x >= monitor.x + monitor.width)
219 dist_x = x - (monitor.x + monitor.width) + 1;
224 dist_y = monitor.y - y;
225 else if (y >= monitor.y + monitor.height)
226 dist_y = y - (monitor.y + monitor.height) + 1;
230 if (MIN (dist_x, dist_y) < nearest_dist)
232 nearest_dist = MIN (dist_x, dist_y);
237 return nearest_monitor;
241 * gdk_screen_get_monitor_at_point:
242 * @screen: a #GdkScreen.
243 * @x: the x coordinate in the virtual screen.
244 * @y: the y coordinate in the virtual screen.
246 * Returns the monitor number in which the point (@x,@y) is located.
248 * Returns: the monitor number in which the point (@x,@y) lies, or
249 * a monitor close to (@x,@y) if the point is not in any monitor.
254 gdk_screen_get_monitor_at_point (GdkScreen *screen,
258 gint num_monitors, i;
260 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
262 num_monitors = gdk_screen_get_n_monitors (screen);
264 for (i=0;i<num_monitors;i++)
266 GdkRectangle monitor;
268 gdk_screen_get_monitor_geometry (screen, i, &monitor);
270 if (x >= monitor.x &&
271 x < monitor.x + monitor.width &&
273 y < (monitor.y + monitor.height))
277 return get_nearest_monitor (screen, x, y);
281 * gdk_screen_get_monitor_at_window:
282 * @screen: a #GdkScreen.
283 * @window: a #GdkWindow
284 * @returns: the monitor number in which most of @window is located,
285 * or if @window does not intersect any monitors, a monitor,
288 * Returns the number of the monitor in which the largest area of the
289 * bounding rectangle of @window resides.
294 gdk_screen_get_monitor_at_window (GdkScreen *screen,
297 gint num_monitors, i, area = 0, screen_num = -1;
298 GdkRectangle win_rect;
299 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
301 gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width,
302 &win_rect.height, NULL);
303 gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
304 num_monitors = gdk_screen_get_n_monitors (screen);
306 for (i=0;i<num_monitors;i++)
308 GdkRectangle tmp_monitor, intersect;
310 gdk_screen_get_monitor_geometry (screen, i, &tmp_monitor);
311 gdk_rectangle_intersect (&win_rect, &tmp_monitor, &intersect);
313 if (intersect.width * intersect.height > area)
315 area = intersect.width * intersect.height;
322 return get_nearest_monitor (screen,
323 win_rect.x + win_rect.width / 2,
324 win_rect.y + win_rect.height / 2);
330 * Returns the width of the default screen in pixels.
332 * Return value: the width of the default screen in pixels.
335 gdk_screen_width (void)
337 return gdk_screen_get_width (gdk_screen_get_default());
343 * Returns the height of the default screen in pixels.
345 * Return value: the height of the default screen in pixels.
348 gdk_screen_height (void)
350 return gdk_screen_get_height (gdk_screen_get_default());
354 * gdk_screen_width_mm:
356 * Returns the width of the default screen in millimeters.
357 * Note that on many X servers this value will not be correct.
359 * Return value: the width of the default screen in millimeters,
360 * though it is not always correct.
363 gdk_screen_width_mm (void)
365 return gdk_screen_get_width_mm (gdk_screen_get_default());
369 * gdk_screen_height_mm:
371 * Returns the height of the default screen in millimeters.
372 * Note that on many X servers this value will not be correct.
374 * Return value: the height of the default screen in millimeters,
375 * though it is not always correct.
378 gdk_screen_height_mm (void)
380 return gdk_screen_get_height_mm (gdk_screen_get_default ());
384 * gdk_screen_set_font_options:
385 * @screen: a #GdkScreen
386 * @options: a #cairo_font_options_t, or %NULL to unset any
387 * previously set default font options.
389 * Sets the default font options for the screen. These
390 * options will be set on any #PangoContext's newly created
391 * with gdk_pango_context_get_for_screen(). Changing the
392 * default set of font options does not affect contexts that
393 * have already been created.
398 gdk_screen_set_font_options (GdkScreen *screen,
399 const cairo_font_options_t *options)
401 g_return_if_fail (GDK_IS_SCREEN (screen));
403 if (screen->font_options != options)
405 if (screen->font_options)
406 cairo_font_options_destroy (screen->font_options);
409 screen->font_options = cairo_font_options_copy (options);
411 screen->font_options = NULL;
413 g_object_notify (G_OBJECT (screen), "font-options");
418 * gdk_screen_get_font_options:
419 * @screen: a #GdkScreen
421 * Gets any options previously set with gdk_screen_set_font_options().
423 * Return value: the current font options, or %NULL if no default
424 * font options have been set.
428 const cairo_font_options_t *
429 gdk_screen_get_font_options (GdkScreen *screen)
431 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
433 return screen->font_options;
437 * gdk_screen_set_resolution:
438 * @screen: a #GdkScreen
439 * @dpi: the resolution in "dots per inch". (Physical inches aren't actually
440 * involved; the terminology is conventional.)
442 * Sets the resolution for font handling on the screen. This is a
443 * scale factor between points specified in a #PangoFontDescription
444 * and cairo units. The default value is 96, meaning that a 10 point
445 * font will be 13 units high. (10 * 96. / 72. = 13.3).
450 gdk_screen_set_resolution (GdkScreen *screen,
453 g_return_if_fail (GDK_IS_SCREEN (screen));
458 if (screen->resolution != dpi)
460 screen->resolution = dpi;
462 g_object_notify (G_OBJECT (screen), "resolution");
467 * gdk_screen_get_resolution:
468 * @screen: a #GdkScreen
470 * Gets the resolution for font handling on the screen; see
471 * gdk_screen_set_resolution() for full details.
473 * Return value: the current resolution, or -1 if no resolution
479 gdk_screen_get_resolution (GdkScreen *screen)
481 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1.);
483 return screen->resolution;
487 gdk_screen_get_property (GObject *object,
492 GdkScreen *screen = GDK_SCREEN (object);
496 case PROP_FONT_OPTIONS:
497 g_value_set_pointer (value, gdk_screen_get_font_options (screen));
499 case PROP_RESOLUTION:
500 g_value_set_double (value, gdk_screen_get_resolution (screen));
503 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
509 gdk_screen_set_property (GObject *object,
514 GdkScreen *screen = GDK_SCREEN (object);
518 case PROP_FONT_OPTIONS:
519 gdk_screen_set_font_options (screen, g_value_get_pointer (value));
521 case PROP_RESOLUTION:
522 gdk_screen_set_resolution (screen, g_value_get_double (value));
525 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
530 #define __GDK_SCREEN_C__
531 #include "gdkaliasdef.c"