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.
26 #include "gdkscreen.h"
28 #include "gdkrectangle.h"
29 #include "gdkwindow.h"
35 * @Short_description: Object representing a physical screen
38 * #GdkScreen objects are the GDK representation of a physical screen. It is used
39 * throughout GDK and GTK+ to specify which screen the top level windows
40 * are to be displayed on.
41 * It is also used to query the screen specification and default settings such as
42 * the screen width (gdk_screen_get_width()), etc.
44 * Note that a screen may consist of multiple monitors which are merged to
45 * form a large screen area.
49 static void gdk_screen_finalize (GObject *object);
50 static void gdk_screen_set_property (GObject *object,
54 static void gdk_screen_get_property (GObject *object,
74 static guint signals[LAST_SIGNAL] = { 0 };
76 G_DEFINE_TYPE (GdkScreen, gdk_screen, G_TYPE_OBJECT)
79 gdk_screen_class_init (GdkScreenClass *klass)
81 GObjectClass *object_class = G_OBJECT_CLASS (klass);
83 object_class->finalize = gdk_screen_finalize;
84 object_class->set_property = gdk_screen_set_property;
85 object_class->get_property = gdk_screen_get_property;
87 g_object_class_install_property (object_class,
89 g_param_spec_pointer ("font-options",
91 P_("The default font options for the screen"),
92 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
93 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
95 g_object_class_install_property (object_class,
97 g_param_spec_double ("resolution",
98 P_("Font resolution"),
99 P_("The resolution for fonts on the screen"),
103 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
104 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
107 * GdkScreen::size-changed:
108 * @screen: the object on which the signal is emitted
110 * The ::size-changed signal is emitted when the pixel width or
111 * height of a screen changes.
115 signals[SIZE_CHANGED] =
116 g_signal_new (g_intern_static_string ("size-changed"),
117 G_OBJECT_CLASS_TYPE (klass),
119 G_STRUCT_OFFSET (GdkScreenClass, size_changed),
121 g_cclosure_marshal_VOID__VOID,
126 * GdkScreen::composited-changed:
127 * @screen: the object on which the signal is emitted
129 * The ::composited-changed signal is emitted when the composited
130 * status of the screen changes
134 signals[COMPOSITED_CHANGED] =
135 g_signal_new (g_intern_static_string ("composited-changed"),
136 G_OBJECT_CLASS_TYPE (klass),
138 G_STRUCT_OFFSET (GdkScreenClass, composited_changed),
140 g_cclosure_marshal_VOID__VOID,
145 * GdkScreen::monitors-changed:
146 * @screen: the object on which the signal is emitted
148 * The ::monitors-changed signal is emitted when the number, size
149 * or position of the monitors attached to the screen change.
151 * Only for X11 and OS X for now. A future implementation for Win32
152 * may be a possibility.
156 signals[MONITORS_CHANGED] =
157 g_signal_new (g_intern_static_string ("monitors-changed"),
158 G_OBJECT_CLASS_TYPE (klass),
160 G_STRUCT_OFFSET (GdkScreenClass, monitors_changed),
162 g_cclosure_marshal_VOID__VOID,
168 gdk_screen_init (GdkScreen *screen)
170 screen->resolution = -1.;
174 gdk_screen_finalize (GObject *object)
176 GdkScreen *screen = GDK_SCREEN (object);
178 if (screen->font_options)
179 cairo_font_options_destroy (screen->font_options);
181 G_OBJECT_CLASS (gdk_screen_parent_class)->finalize (object);
185 _gdk_screen_close (GdkScreen *screen)
187 g_return_if_fail (GDK_IS_SCREEN (screen));
191 screen->closed = TRUE;
192 g_object_run_dispose (G_OBJECT (screen));
196 /* Fallback used when the monitor "at" a point or window
200 get_nearest_monitor (GdkScreen *screen,
204 gint num_monitors, i;
205 gint nearest_dist = G_MAXINT;
206 gint nearest_monitor = 0;
208 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
210 num_monitors = gdk_screen_get_n_monitors (screen);
212 for (i = 0; i < num_monitors; i++)
214 GdkRectangle monitor;
215 gint dist_x, dist_y, dist;
217 gdk_screen_get_monitor_geometry (screen, i, &monitor);
220 dist_x = monitor.x - x;
221 else if (x >= monitor.x + monitor.width)
222 dist_x = x - (monitor.x + monitor.width) + 1;
227 dist_y = monitor.y - y;
228 else if (y >= monitor.y + monitor.height)
229 dist_y = y - (monitor.y + monitor.height) + 1;
233 dist = dist_x + dist_y;
234 if (dist < nearest_dist)
241 return nearest_monitor;
245 * gdk_screen_get_monitor_at_point:
246 * @screen: a #GdkScreen.
247 * @x: the x coordinate in the virtual screen.
248 * @y: the y coordinate in the virtual screen.
250 * Returns the monitor number in which the point (@x,@y) is located.
252 * Returns: the monitor number in which the point (@x,@y) lies, or
253 * a monitor close to (@x,@y) if the point is not in any monitor.
258 gdk_screen_get_monitor_at_point (GdkScreen *screen,
262 gint num_monitors, i;
264 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
266 num_monitors = gdk_screen_get_n_monitors (screen);
268 for (i=0;i<num_monitors;i++)
270 GdkRectangle monitor;
272 gdk_screen_get_monitor_geometry (screen, i, &monitor);
274 if (x >= monitor.x &&
275 x < monitor.x + monitor.width &&
277 y < (monitor.y + monitor.height))
281 return get_nearest_monitor (screen, x, y);
285 * gdk_screen_get_monitor_at_window:
286 * @screen: a #GdkScreen.
287 * @window: a #GdkWindow
288 * @returns: the monitor number in which most of @window is located,
289 * or if @window does not intersect any monitors, a monitor,
292 * Returns the number of the monitor in which the largest area of the
293 * bounding rectangle of @window resides.
298 gdk_screen_get_monitor_at_window (GdkScreen *screen,
301 gint num_monitors, i, area = 0, screen_num = -1;
302 GdkRectangle win_rect;
304 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
306 gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width,
307 &win_rect.height, NULL);
308 gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
309 num_monitors = gdk_screen_get_n_monitors (screen);
311 for (i=0;i<num_monitors;i++)
313 GdkRectangle tmp_monitor, intersect;
315 gdk_screen_get_monitor_geometry (screen, i, &tmp_monitor);
316 gdk_rectangle_intersect (&win_rect, &tmp_monitor, &intersect);
318 if (intersect.width * intersect.height > area)
320 area = intersect.width * intersect.height;
327 return get_nearest_monitor (screen,
328 win_rect.x + win_rect.width / 2,
329 win_rect.y + win_rect.height / 2);
335 * Returns the width of the default screen in pixels.
337 * Return value: the width of the default screen in pixels.
340 gdk_screen_width (void)
342 return gdk_screen_get_width (gdk_screen_get_default ());
348 * Returns the height of the default screen in pixels.
350 * Return value: the height of the default screen in pixels.
353 gdk_screen_height (void)
355 return gdk_screen_get_height (gdk_screen_get_default ());
359 * gdk_screen_width_mm:
361 * Returns the width of the default screen in millimeters.
362 * Note that on many X servers this value will not be correct.
364 * Return value: the width of the default screen in millimeters,
365 * though it is not always correct.
368 gdk_screen_width_mm (void)
370 return gdk_screen_get_width_mm (gdk_screen_get_default ());
374 * gdk_screen_height_mm:
376 * Returns the height of the default screen in millimeters.
377 * Note that on many X servers this value will not be correct.
379 * Return value: the height of the default screen in millimeters,
380 * though it is not always correct.
383 gdk_screen_height_mm (void)
385 return gdk_screen_get_height_mm (gdk_screen_get_default ());
389 * gdk_screen_set_font_options:
390 * @screen: a #GdkScreen
391 * @options: (allow-none): a #cairo_font_options_t, or %NULL to unset any
392 * previously set default font options.
394 * Sets the default font options for the screen. These
395 * options will be set on any #PangoContext's newly created
396 * with gdk_pango_context_get_for_screen(). Changing the
397 * default set of font options does not affect contexts that
398 * have already been created.
403 gdk_screen_set_font_options (GdkScreen *screen,
404 const cairo_font_options_t *options)
406 g_return_if_fail (GDK_IS_SCREEN (screen));
408 if (screen->font_options != options)
410 if (screen->font_options)
411 cairo_font_options_destroy (screen->font_options);
414 screen->font_options = cairo_font_options_copy (options);
416 screen->font_options = NULL;
418 g_object_notify (G_OBJECT (screen), "font-options");
423 * gdk_screen_get_font_options:
424 * @screen: a #GdkScreen
426 * Gets any options previously set with gdk_screen_set_font_options().
428 * Return value: the current font options, or %NULL if no default
429 * font options have been set.
433 const cairo_font_options_t *
434 gdk_screen_get_font_options (GdkScreen *screen)
436 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
438 return screen->font_options;
442 * gdk_screen_set_resolution:
443 * @screen: a #GdkScreen
444 * @dpi: the resolution in "dots per inch". (Physical inches aren't actually
445 * involved; the terminology is conventional.)
447 * Sets the resolution for font handling on the screen. This is a
448 * scale factor between points specified in a #PangoFontDescription
449 * and cairo units. The default value is 96, meaning that a 10 point
450 * font will be 13 units high. (10 * 96. / 72. = 13.3).
455 gdk_screen_set_resolution (GdkScreen *screen,
458 g_return_if_fail (GDK_IS_SCREEN (screen));
463 if (screen->resolution != dpi)
465 screen->resolution = dpi;
467 g_object_notify (G_OBJECT (screen), "resolution");
472 * gdk_screen_get_resolution:
473 * @screen: a #GdkScreen
475 * Gets the resolution for font handling on the screen; see
476 * gdk_screen_set_resolution() for full details.
478 * Return value: the current resolution, or -1 if no resolution
484 gdk_screen_get_resolution (GdkScreen *screen)
486 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1.0);
488 return screen->resolution;
492 gdk_screen_get_property (GObject *object,
497 GdkScreen *screen = GDK_SCREEN (object);
501 case PROP_FONT_OPTIONS:
502 g_value_set_pointer (value, (gpointer) gdk_screen_get_font_options (screen));
504 case PROP_RESOLUTION:
505 g_value_set_double (value, gdk_screen_get_resolution (screen));
508 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
514 gdk_screen_set_property (GObject *object,
519 GdkScreen *screen = GDK_SCREEN (object);
523 case PROP_FONT_OPTIONS:
524 gdk_screen_set_font_options (screen, g_value_get_pointer (value));
526 case PROP_RESOLUTION:
527 gdk_screen_set_resolution (screen, g_value_get_double (value));
530 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);