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"
33 static void gdk_screen_finalize (GObject *object);
34 static void gdk_screen_set_property (GObject *object,
38 static void gdk_screen_get_property (GObject *object,
58 static guint signals[LAST_SIGNAL] = { 0 };
60 G_DEFINE_TYPE (GdkScreen, gdk_screen, G_TYPE_OBJECT)
63 gdk_screen_class_init (GdkScreenClass *klass)
65 GObjectClass *object_class = G_OBJECT_CLASS (klass);
67 object_class->finalize = gdk_screen_finalize;
68 object_class->set_property = gdk_screen_set_property;
69 object_class->get_property = gdk_screen_get_property;
71 g_object_class_install_property (object_class,
73 g_param_spec_pointer ("font-options",
75 P_("The default font options for the screen"),
76 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
77 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
79 g_object_class_install_property (object_class,
81 g_param_spec_double ("resolution",
82 P_("Font resolution"),
83 P_("The resolution for fonts on the screen"),
87 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
88 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
91 * GdkScreen::size-changed:
92 * @screen: the object on which the signal is emitted
94 * The ::size-changed signal is emitted when the pixel width or
95 * height of a screen changes.
99 signals[SIZE_CHANGED] =
100 g_signal_new (g_intern_static_string ("size-changed"),
101 G_OBJECT_CLASS_TYPE (klass),
103 G_STRUCT_OFFSET (GdkScreenClass, size_changed),
105 g_cclosure_marshal_VOID__VOID,
110 * GdkScreen::composited-changed:
111 * @screen: the object on which the signal is emitted
113 * The ::composited-changed signal is emitted when the composited
114 * status of the screen changes
118 signals[COMPOSITED_CHANGED] =
119 g_signal_new (g_intern_static_string ("composited-changed"),
120 G_OBJECT_CLASS_TYPE (klass),
122 G_STRUCT_OFFSET (GdkScreenClass, composited_changed),
124 g_cclosure_marshal_VOID__VOID,
129 * GdkScreen::monitors-changed:
130 * @screen: the object on which the signal is emitted
132 * The ::monitors-changed signal is emitted when the number, size
133 * or position of the monitors attached to the screen change.
135 * Only for X11 and OS X for now. A future implementation for Win32
136 * may be a possibility.
140 signals[MONITORS_CHANGED] =
141 g_signal_new (g_intern_static_string ("monitors-changed"),
142 G_OBJECT_CLASS_TYPE (klass),
144 G_STRUCT_OFFSET (GdkScreenClass, monitors_changed),
146 g_cclosure_marshal_VOID__VOID,
152 gdk_screen_init (GdkScreen *screen)
154 screen->resolution = -1.;
158 gdk_screen_finalize (GObject *object)
160 GdkScreen *screen = GDK_SCREEN (object);
162 if (screen->font_options)
163 cairo_font_options_destroy (screen->font_options);
165 G_OBJECT_CLASS (gdk_screen_parent_class)->finalize (object);
169 _gdk_screen_close (GdkScreen *screen)
171 g_return_if_fail (GDK_IS_SCREEN (screen));
175 screen->closed = TRUE;
176 g_object_run_dispose (G_OBJECT (screen));
180 /* Fallback used when the monitor "at" a point or window
184 get_nearest_monitor (GdkScreen *screen,
188 gint num_monitors, i;
189 gint nearest_dist = G_MAXINT;
190 gint nearest_monitor = 0;
192 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
194 num_monitors = gdk_screen_get_n_monitors (screen);
196 for (i = 0; i < num_monitors; i++)
198 GdkRectangle monitor;
199 gint dist_x, dist_y, dist;
201 gdk_screen_get_monitor_geometry (screen, i, &monitor);
204 dist_x = monitor.x - x;
205 else if (x >= monitor.x + monitor.width)
206 dist_x = x - (monitor.x + monitor.width) + 1;
211 dist_y = monitor.y - y;
212 else if (y >= monitor.y + monitor.height)
213 dist_y = y - (monitor.y + monitor.height) + 1;
217 dist = dist_x + dist_y;
218 if (dist < nearest_dist)
225 return nearest_monitor;
229 * gdk_screen_get_monitor_at_point:
230 * @screen: a #GdkScreen.
231 * @x: the x coordinate in the virtual screen.
232 * @y: the y coordinate in the virtual screen.
234 * Returns the monitor number in which the point (@x,@y) is located.
236 * Returns: the monitor number in which the point (@x,@y) lies, or
237 * a monitor close to (@x,@y) if the point is not in any monitor.
242 gdk_screen_get_monitor_at_point (GdkScreen *screen,
246 gint num_monitors, i;
248 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
250 num_monitors = gdk_screen_get_n_monitors (screen);
252 for (i=0;i<num_monitors;i++)
254 GdkRectangle monitor;
256 gdk_screen_get_monitor_geometry (screen, i, &monitor);
258 if (x >= monitor.x &&
259 x < monitor.x + monitor.width &&
261 y < (monitor.y + monitor.height))
265 return get_nearest_monitor (screen, x, y);
269 * gdk_screen_get_monitor_at_window:
270 * @screen: a #GdkScreen.
271 * @window: a #GdkWindow
272 * @returns: the monitor number in which most of @window is located,
273 * or if @window does not intersect any monitors, a monitor,
276 * Returns the number of the monitor in which the largest area of the
277 * bounding rectangle of @window resides.
282 gdk_screen_get_monitor_at_window (GdkScreen *screen,
285 gint num_monitors, i, area = 0, screen_num = -1;
286 GdkRectangle win_rect;
288 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
290 gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width,
291 &win_rect.height, NULL);
292 gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
293 num_monitors = gdk_screen_get_n_monitors (screen);
295 for (i=0;i<num_monitors;i++)
297 GdkRectangle tmp_monitor, intersect;
299 gdk_screen_get_monitor_geometry (screen, i, &tmp_monitor);
300 gdk_rectangle_intersect (&win_rect, &tmp_monitor, &intersect);
302 if (intersect.width * intersect.height > area)
304 area = intersect.width * intersect.height;
311 return get_nearest_monitor (screen,
312 win_rect.x + win_rect.width / 2,
313 win_rect.y + win_rect.height / 2);
319 * Returns the width of the default screen in pixels.
321 * Return value: the width of the default screen in pixels.
324 gdk_screen_width (void)
326 return gdk_screen_get_width (gdk_screen_get_default ());
332 * Returns the height of the default screen in pixels.
334 * Return value: the height of the default screen in pixels.
337 gdk_screen_height (void)
339 return gdk_screen_get_height (gdk_screen_get_default ());
343 * gdk_screen_width_mm:
345 * Returns the width of the default screen in millimeters.
346 * Note that on many X servers this value will not be correct.
348 * Return value: the width of the default screen in millimeters,
349 * though it is not always correct.
352 gdk_screen_width_mm (void)
354 return gdk_screen_get_width_mm (gdk_screen_get_default ());
358 * gdk_screen_height_mm:
360 * Returns the height of the default screen in millimeters.
361 * Note that on many X servers this value will not be correct.
363 * Return value: the height of the default screen in millimeters,
364 * though it is not always correct.
367 gdk_screen_height_mm (void)
369 return gdk_screen_get_height_mm (gdk_screen_get_default ());
373 * gdk_screen_set_font_options:
374 * @screen: a #GdkScreen
375 * @options: (allow-none): a #cairo_font_options_t, or %NULL to unset any
376 * previously set default font options.
378 * Sets the default font options for the screen. These
379 * options will be set on any #PangoContext's newly created
380 * with gdk_pango_context_get_for_screen(). Changing the
381 * default set of font options does not affect contexts that
382 * have already been created.
387 gdk_screen_set_font_options (GdkScreen *screen,
388 const cairo_font_options_t *options)
390 g_return_if_fail (GDK_IS_SCREEN (screen));
392 if (screen->font_options != options)
394 if (screen->font_options)
395 cairo_font_options_destroy (screen->font_options);
398 screen->font_options = cairo_font_options_copy (options);
400 screen->font_options = NULL;
402 g_object_notify (G_OBJECT (screen), "font-options");
407 * gdk_screen_get_font_options:
408 * @screen: a #GdkScreen
410 * Gets any options previously set with gdk_screen_set_font_options().
412 * Return value: the current font options, or %NULL if no default
413 * font options have been set.
417 const cairo_font_options_t *
418 gdk_screen_get_font_options (GdkScreen *screen)
420 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
422 return screen->font_options;
426 * gdk_screen_set_resolution:
427 * @screen: a #GdkScreen
428 * @dpi: the resolution in "dots per inch". (Physical inches aren't actually
429 * involved; the terminology is conventional.)
431 * Sets the resolution for font handling on the screen. This is a
432 * scale factor between points specified in a #PangoFontDescription
433 * and cairo units. The default value is 96, meaning that a 10 point
434 * font will be 13 units high. (10 * 96. / 72. = 13.3).
439 gdk_screen_set_resolution (GdkScreen *screen,
442 g_return_if_fail (GDK_IS_SCREEN (screen));
447 if (screen->resolution != dpi)
449 screen->resolution = dpi;
451 g_object_notify (G_OBJECT (screen), "resolution");
456 * gdk_screen_get_resolution:
457 * @screen: a #GdkScreen
459 * Gets the resolution for font handling on the screen; see
460 * gdk_screen_set_resolution() for full details.
462 * Return value: the current resolution, or -1 if no resolution
468 gdk_screen_get_resolution (GdkScreen *screen)
470 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1.0);
472 return screen->resolution;
476 gdk_screen_get_property (GObject *object,
481 GdkScreen *screen = GDK_SCREEN (object);
485 case PROP_FONT_OPTIONS:
486 g_value_set_pointer (value, (gpointer) gdk_screen_get_font_options (screen));
488 case PROP_RESOLUTION:
489 g_value_set_double (value, gdk_screen_get_resolution (screen));
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 gdk_screen_set_property (GObject *object,
503 GdkScreen *screen = GDK_SCREEN (object);
507 case PROP_FONT_OPTIONS:
508 gdk_screen_set_font_options (screen, g_value_get_pointer (value));
510 case PROP_RESOLUTION:
511 gdk_screen_set_resolution (screen, g_value_get_double (value));
514 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);