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() */
26 #include "gdkwindow.h"
27 #include "gdkscreen.h"
31 static void gdk_screen_finalize (GObject *object);
32 static void gdk_screen_set_property (GObject *object,
36 static void gdk_screen_get_property (GObject *object,
56 static guint signals[LAST_SIGNAL] = { 0 };
58 G_DEFINE_TYPE (GdkScreen, gdk_screen, G_TYPE_OBJECT)
61 gdk_screen_class_init (GdkScreenClass *klass)
63 GObjectClass *object_class = G_OBJECT_CLASS (klass);
65 object_class->finalize = gdk_screen_finalize;
66 object_class->set_property = gdk_screen_set_property;
67 object_class->get_property = gdk_screen_get_property;
69 g_object_class_install_property (object_class,
71 g_param_spec_pointer ("font-options",
73 P_("The default font options for the screen"),
74 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
75 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
77 g_object_class_install_property (object_class,
79 g_param_spec_double ("resolution",
80 P_("Font resolution"),
81 P_("The resolution for fonts on the screen"),
85 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
86 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
89 * GdkScreen::size-changed:
90 * @screen: the object on which the signal is emitted
92 * The ::size-changed signal is emitted when the pixel width or
93 * height of a screen changes.
97 signals[SIZE_CHANGED] =
98 g_signal_new (g_intern_static_string ("size-changed"),
99 G_OBJECT_CLASS_TYPE (klass),
101 G_STRUCT_OFFSET (GdkScreenClass, size_changed),
103 g_cclosure_marshal_VOID__VOID,
108 * GdkScreen::composited-changed:
109 * @screen: the object on which the signal is emitted
111 * The ::composited-changed signal is emitted when the composited
112 * status of the screen changes
116 signals[COMPOSITED_CHANGED] =
117 g_signal_new (g_intern_static_string ("composited-changed"),
118 G_OBJECT_CLASS_TYPE (klass),
120 G_STRUCT_OFFSET (GdkScreenClass, composited_changed),
122 g_cclosure_marshal_VOID__VOID,
127 * GdkScreen::monitors-changed:
128 * @screen: the object on which the signal is emitted
130 * The ::monitors-changed signal is emitted when the number, size
131 * or position of the monitors attached to the screen change.
133 * Only for X11 and OS X for now. A future implementation for Win32
134 * may be a possibility.
138 signals[MONITORS_CHANGED] =
139 g_signal_new (g_intern_static_string ("monitors-changed"),
140 G_OBJECT_CLASS_TYPE (klass),
142 G_STRUCT_OFFSET (GdkScreenClass, monitors_changed),
144 g_cclosure_marshal_VOID__VOID,
150 gdk_screen_init (GdkScreen *screen)
152 screen->resolution = -1.;
156 gdk_screen_finalize (GObject *object)
158 GdkScreen *screen = GDK_SCREEN (object);
160 if (screen->font_options)
161 cairo_font_options_destroy (screen->font_options);
163 G_OBJECT_CLASS (gdk_screen_parent_class)->finalize (object);
167 _gdk_screen_close (GdkScreen *screen)
169 g_return_if_fail (GDK_IS_SCREEN (screen));
173 screen->closed = TRUE;
174 g_object_run_dispose (G_OBJECT (screen));
178 /* Fallback used when the monitor "at" a point or window
182 get_nearest_monitor (GdkScreen *screen,
186 gint num_monitors, i;
187 gint nearest_dist = G_MAXINT;
188 gint nearest_monitor = 0;
190 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
192 num_monitors = gdk_screen_get_n_monitors (screen);
194 for (i = 0; i < num_monitors; i++)
196 GdkRectangle monitor;
197 gint dist_x, dist_y, dist;
199 gdk_screen_get_monitor_geometry (screen, i, &monitor);
202 dist_x = monitor.x - x;
203 else if (x >= monitor.x + monitor.width)
204 dist_x = x - (monitor.x + monitor.width) + 1;
209 dist_y = monitor.y - y;
210 else if (y >= monitor.y + monitor.height)
211 dist_y = y - (monitor.y + monitor.height) + 1;
215 dist = dist_x + dist_y;
216 if (dist < nearest_dist)
223 return nearest_monitor;
227 * gdk_screen_get_monitor_at_point:
228 * @screen: a #GdkScreen.
229 * @x: the x coordinate in the virtual screen.
230 * @y: the y coordinate in the virtual screen.
232 * Returns the monitor number in which the point (@x,@y) is located.
234 * Returns: the monitor number in which the point (@x,@y) lies, or
235 * a monitor close to (@x,@y) if the point is not in any monitor.
240 gdk_screen_get_monitor_at_point (GdkScreen *screen,
244 gint num_monitors, i;
246 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
248 num_monitors = gdk_screen_get_n_monitors (screen);
250 for (i=0;i<num_monitors;i++)
252 GdkRectangle monitor;
254 gdk_screen_get_monitor_geometry (screen, i, &monitor);
256 if (x >= monitor.x &&
257 x < monitor.x + monitor.width &&
259 y < (monitor.y + monitor.height))
263 return get_nearest_monitor (screen, x, y);
267 * gdk_screen_get_monitor_at_window:
268 * @screen: a #GdkScreen.
269 * @window: a #GdkWindow
270 * @returns: the monitor number in which most of @window is located,
271 * or if @window does not intersect any monitors, a monitor,
274 * Returns the number of the monitor in which the largest area of the
275 * bounding rectangle of @window resides.
280 gdk_screen_get_monitor_at_window (GdkScreen *screen,
283 gint num_monitors, i, area = 0, screen_num = -1;
284 GdkRectangle win_rect;
286 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
288 gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width,
289 &win_rect.height, NULL);
290 gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
291 num_monitors = gdk_screen_get_n_monitors (screen);
293 for (i=0;i<num_monitors;i++)
295 GdkRectangle tmp_monitor, intersect;
297 gdk_screen_get_monitor_geometry (screen, i, &tmp_monitor);
298 gdk_rectangle_intersect (&win_rect, &tmp_monitor, &intersect);
300 if (intersect.width * intersect.height > area)
302 area = intersect.width * intersect.height;
309 return get_nearest_monitor (screen,
310 win_rect.x + win_rect.width / 2,
311 win_rect.y + win_rect.height / 2);
317 * Returns the width of the default screen in pixels.
319 * Return value: the width of the default screen in pixels.
322 gdk_screen_width (void)
324 return gdk_screen_get_width (gdk_screen_get_default ());
330 * Returns the height of the default screen in pixels.
332 * Return value: the height of the default screen in pixels.
335 gdk_screen_height (void)
337 return gdk_screen_get_height (gdk_screen_get_default ());
341 * gdk_screen_width_mm:
343 * Returns the width of the default screen in millimeters.
344 * Note that on many X servers this value will not be correct.
346 * Return value: the width of the default screen in millimeters,
347 * though it is not always correct.
350 gdk_screen_width_mm (void)
352 return gdk_screen_get_width_mm (gdk_screen_get_default ());
356 * gdk_screen_height_mm:
358 * Returns the height of the default screen in millimeters.
359 * Note that on many X servers this value will not be correct.
361 * Return value: the height of the default screen in millimeters,
362 * though it is not always correct.
365 gdk_screen_height_mm (void)
367 return gdk_screen_get_height_mm (gdk_screen_get_default ());
371 * gdk_screen_set_font_options:
372 * @screen: a #GdkScreen
373 * @options: (allow-none): a #cairo_font_options_t, or %NULL to unset any
374 * previously set default font options.
376 * Sets the default font options for the screen. These
377 * options will be set on any #PangoContext's newly created
378 * with gdk_pango_context_get_for_screen(). Changing the
379 * default set of font options does not affect contexts that
380 * have already been created.
385 gdk_screen_set_font_options (GdkScreen *screen,
386 const cairo_font_options_t *options)
388 g_return_if_fail (GDK_IS_SCREEN (screen));
390 if (screen->font_options != options)
392 if (screen->font_options)
393 cairo_font_options_destroy (screen->font_options);
396 screen->font_options = cairo_font_options_copy (options);
398 screen->font_options = NULL;
400 g_object_notify (G_OBJECT (screen), "font-options");
405 * gdk_screen_get_font_options:
406 * @screen: a #GdkScreen
408 * Gets any options previously set with gdk_screen_set_font_options().
410 * Return value: the current font options, or %NULL if no default
411 * font options have been set.
415 const cairo_font_options_t *
416 gdk_screen_get_font_options (GdkScreen *screen)
418 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
420 return screen->font_options;
424 * gdk_screen_set_resolution:
425 * @screen: a #GdkScreen
426 * @dpi: the resolution in "dots per inch". (Physical inches aren't actually
427 * involved; the terminology is conventional.)
429 * Sets the resolution for font handling on the screen. This is a
430 * scale factor between points specified in a #PangoFontDescription
431 * and cairo units. The default value is 96, meaning that a 10 point
432 * font will be 13 units high. (10 * 96. / 72. = 13.3).
437 gdk_screen_set_resolution (GdkScreen *screen,
440 g_return_if_fail (GDK_IS_SCREEN (screen));
445 if (screen->resolution != dpi)
447 screen->resolution = dpi;
449 g_object_notify (G_OBJECT (screen), "resolution");
454 * gdk_screen_get_resolution:
455 * @screen: a #GdkScreen
457 * Gets the resolution for font handling on the screen; see
458 * gdk_screen_set_resolution() for full details.
460 * Return value: the current resolution, or -1 if no resolution
466 gdk_screen_get_resolution (GdkScreen *screen)
468 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1.0);
470 return screen->resolution;
474 gdk_screen_get_property (GObject *object,
479 GdkScreen *screen = GDK_SCREEN (object);
483 case PROP_FONT_OPTIONS:
484 g_value_set_pointer (value, (gpointer) gdk_screen_get_font_options (screen));
486 case PROP_RESOLUTION:
487 g_value_set_double (value, gdk_screen_get_resolution (screen));
490 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
496 gdk_screen_set_property (GObject *object,
501 GdkScreen *screen = GDK_SCREEN (object);
505 case PROP_FONT_OPTIONS:
506 gdk_screen_set_font_options (screen, g_value_get_pointer (value));
508 case PROP_RESOLUTION:
509 gdk_screen_set_resolution (screen, g_value_get_double (value));
512 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);