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_dispose (GObject *object);
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->dispose = gdk_screen_dispose;
68 object_class->finalize = gdk_screen_finalize;
69 object_class->set_property = gdk_screen_set_property;
70 object_class->get_property = gdk_screen_get_property;
72 g_object_class_install_property (object_class,
74 g_param_spec_pointer ("font-options",
76 P_("The default font options for the screen"),
77 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
78 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
80 g_object_class_install_property (object_class,
82 g_param_spec_double ("resolution",
83 P_("Font resolution"),
84 P_("The resolution for fonts on the screen"),
88 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
89 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
92 * GdkScreen::size-changed:
93 * @screen: the object on which the signal is emitted
95 * The ::size-changed signal is emitted when the pixel width or
96 * height of a screen changes.
100 signals[SIZE_CHANGED] =
101 g_signal_new (g_intern_static_string ("size-changed"),
102 G_OBJECT_CLASS_TYPE (klass),
104 G_STRUCT_OFFSET (GdkScreenClass, size_changed),
106 g_cclosure_marshal_VOID__VOID,
111 * GdkScreen::composited-changed:
112 * @screen: the object on which the signal is emitted
114 * The ::composited-changed signal is emitted when the composited
115 * status of the screen changes
119 signals[COMPOSITED_CHANGED] =
120 g_signal_new (g_intern_static_string ("composited-changed"),
121 G_OBJECT_CLASS_TYPE (klass),
123 G_STRUCT_OFFSET (GdkScreenClass, composited_changed),
125 g_cclosure_marshal_VOID__VOID,
130 * GdkScreen::monitors-changed:
131 * @screen: the object on which the signal is emitted
133 * The ::monitors-changed signal is emitted when the number, size
134 * or position of the monitors attached to the screen change.
136 * Only for X11 and OS X for now. A future implementation for Win32
137 * may be a possibility.
141 signals[MONITORS_CHANGED] =
142 g_signal_new (g_intern_static_string ("monitors-changed"),
143 G_OBJECT_CLASS_TYPE (klass),
145 G_STRUCT_OFFSET (GdkScreenClass, monitors_changed),
147 g_cclosure_marshal_VOID__VOID,
153 gdk_screen_init (GdkScreen *screen)
155 screen->resolution = -1.;
159 gdk_screen_dispose (GObject *object)
161 GdkScreen *screen = GDK_SCREEN (object);
164 for (i = 0; i < 32; ++i)
166 if (screen->exposure_gcs[i])
168 g_object_unref (screen->exposure_gcs[i]);
169 screen->exposure_gcs[i] = NULL;
172 if (screen->normal_gcs[i])
174 g_object_unref (screen->normal_gcs[i]);
175 screen->normal_gcs[i] = NULL;
179 G_OBJECT_CLASS (gdk_screen_parent_class)->dispose (object);
183 gdk_screen_finalize (GObject *object)
185 GdkScreen *screen = GDK_SCREEN (object);
187 if (screen->font_options)
188 cairo_font_options_destroy (screen->font_options);
190 G_OBJECT_CLASS (gdk_screen_parent_class)->finalize (object);
194 _gdk_screen_close (GdkScreen *screen)
196 g_return_if_fail (GDK_IS_SCREEN (screen));
200 screen->closed = TRUE;
201 g_object_run_dispose (G_OBJECT (screen));
205 /* Fallback used when the monitor "at" a point or window
209 get_nearest_monitor (GdkScreen *screen,
213 gint num_monitors, i;
214 gint nearest_dist = G_MAXINT;
215 gint nearest_monitor = 0;
217 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
219 num_monitors = gdk_screen_get_n_monitors (screen);
221 for (i = 0; i < num_monitors; i++)
223 GdkRectangle monitor;
224 gint dist_x, dist_y, dist;
226 gdk_screen_get_monitor_geometry (screen, i, &monitor);
229 dist_x = monitor.x - x;
230 else if (x >= monitor.x + monitor.width)
231 dist_x = x - (monitor.x + monitor.width) + 1;
236 dist_y = monitor.y - y;
237 else if (y >= monitor.y + monitor.height)
238 dist_y = y - (monitor.y + monitor.height) + 1;
242 dist = dist_x + dist_y;
243 if (dist < nearest_dist)
250 return nearest_monitor;
254 * gdk_screen_get_monitor_at_point:
255 * @screen: a #GdkScreen.
256 * @x: the x coordinate in the virtual screen.
257 * @y: the y coordinate in the virtual screen.
259 * Returns the monitor number in which the point (@x,@y) is located.
261 * Returns: the monitor number in which the point (@x,@y) lies, or
262 * a monitor close to (@x,@y) if the point is not in any monitor.
267 gdk_screen_get_monitor_at_point (GdkScreen *screen,
271 gint num_monitors, i;
273 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
275 num_monitors = gdk_screen_get_n_monitors (screen);
277 for (i=0;i<num_monitors;i++)
279 GdkRectangle monitor;
281 gdk_screen_get_monitor_geometry (screen, i, &monitor);
283 if (x >= monitor.x &&
284 x < monitor.x + monitor.width &&
286 y < (monitor.y + monitor.height))
290 return get_nearest_monitor (screen, x, y);
294 * gdk_screen_get_monitor_at_window:
295 * @screen: a #GdkScreen.
296 * @window: a #GdkWindow
297 * @returns: the monitor number in which most of @window is located,
298 * or if @window does not intersect any monitors, a monitor,
301 * Returns the number of the monitor in which the largest area of the
302 * bounding rectangle of @window resides.
307 gdk_screen_get_monitor_at_window (GdkScreen *screen,
310 gint num_monitors, i, area = 0, screen_num = -1;
311 GdkRectangle win_rect;
313 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
315 gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width,
316 &win_rect.height, NULL);
317 gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
318 num_monitors = gdk_screen_get_n_monitors (screen);
320 for (i=0;i<num_monitors;i++)
322 GdkRectangle tmp_monitor, intersect;
324 gdk_screen_get_monitor_geometry (screen, i, &tmp_monitor);
325 gdk_rectangle_intersect (&win_rect, &tmp_monitor, &intersect);
327 if (intersect.width * intersect.height > area)
329 area = intersect.width * intersect.height;
336 return get_nearest_monitor (screen,
337 win_rect.x + win_rect.width / 2,
338 win_rect.y + win_rect.height / 2);
344 * Returns the width of the default screen in pixels.
346 * Return value: the width of the default screen in pixels.
349 gdk_screen_width (void)
351 return gdk_screen_get_width (gdk_screen_get_default ());
357 * Returns the height of the default screen in pixels.
359 * Return value: the height of the default screen in pixels.
362 gdk_screen_height (void)
364 return gdk_screen_get_height (gdk_screen_get_default ());
368 * gdk_screen_width_mm:
370 * Returns the width of the default screen in millimeters.
371 * Note that on many X servers this value will not be correct.
373 * Return value: the width of the default screen in millimeters,
374 * though it is not always correct.
377 gdk_screen_width_mm (void)
379 return gdk_screen_get_width_mm (gdk_screen_get_default ());
383 * gdk_screen_height_mm:
385 * Returns the height of the default screen in millimeters.
386 * Note that on many X servers this value will not be correct.
388 * Return value: the height of the default screen in millimeters,
389 * though it is not always correct.
392 gdk_screen_height_mm (void)
394 return gdk_screen_get_height_mm (gdk_screen_get_default ());
398 * gdk_screen_set_font_options:
399 * @screen: a #GdkScreen
400 * @options: (allow-none): a #cairo_font_options_t, or %NULL to unset any
401 * previously set default font options.
403 * Sets the default font options for the screen. These
404 * options will be set on any #PangoContext's newly created
405 * with gdk_pango_context_get_for_screen(). Changing the
406 * default set of font options does not affect contexts that
407 * have already been created.
412 gdk_screen_set_font_options (GdkScreen *screen,
413 const cairo_font_options_t *options)
415 g_return_if_fail (GDK_IS_SCREEN (screen));
417 if (screen->font_options != options)
419 if (screen->font_options)
420 cairo_font_options_destroy (screen->font_options);
423 screen->font_options = cairo_font_options_copy (options);
425 screen->font_options = NULL;
427 g_object_notify (G_OBJECT (screen), "font-options");
432 * gdk_screen_get_font_options:
433 * @screen: a #GdkScreen
435 * Gets any options previously set with gdk_screen_set_font_options().
437 * Return value: the current font options, or %NULL if no default
438 * font options have been set.
442 const cairo_font_options_t *
443 gdk_screen_get_font_options (GdkScreen *screen)
445 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
447 return screen->font_options;
451 * gdk_screen_set_resolution:
452 * @screen: a #GdkScreen
453 * @dpi: the resolution in "dots per inch". (Physical inches aren't actually
454 * involved; the terminology is conventional.)
456 * Sets the resolution for font handling on the screen. This is a
457 * scale factor between points specified in a #PangoFontDescription
458 * and cairo units. The default value is 96, meaning that a 10 point
459 * font will be 13 units high. (10 * 96. / 72. = 13.3).
464 gdk_screen_set_resolution (GdkScreen *screen,
467 g_return_if_fail (GDK_IS_SCREEN (screen));
472 if (screen->resolution != dpi)
474 screen->resolution = dpi;
476 g_object_notify (G_OBJECT (screen), "resolution");
481 * gdk_screen_get_resolution:
482 * @screen: a #GdkScreen
484 * Gets the resolution for font handling on the screen; see
485 * gdk_screen_set_resolution() for full details.
487 * Return value: the current resolution, or -1 if no resolution
493 gdk_screen_get_resolution (GdkScreen *screen)
495 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1.0);
497 return screen->resolution;
501 gdk_screen_get_property (GObject *object,
506 GdkScreen *screen = GDK_SCREEN (object);
510 case PROP_FONT_OPTIONS:
511 g_value_set_pointer (value, (gpointer) gdk_screen_get_font_options (screen));
513 case PROP_RESOLUTION:
514 g_value_set_double (value, gdk_screen_get_resolution (screen));
517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
523 gdk_screen_set_property (GObject *object,
528 GdkScreen *screen = GDK_SCREEN (object);
532 case PROP_FONT_OPTIONS:
533 gdk_screen_set_font_options (screen, g_value_get_pointer (value));
535 case PROP_RESOLUTION:
536 gdk_screen_set_resolution (screen, g_value_get_double (value));
539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
544 #define __GDK_SCREEN_C__
545 #include "gdkaliasdef.c"