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"
31 static void gdk_screen_class_init (GdkScreenClass *klass);
32 static void gdk_screen_init (GdkScreen *screen);
33 static void gdk_screen_dispose (GObject *object);
34 static void gdk_screen_finalize (GObject *object);
42 static guint signals[LAST_SIGNAL] = { 0 };
44 static gpointer parent_class = NULL;
47 gdk_screen_get_type (void)
49 static GType object_type = 0;
53 static const GTypeInfo object_info =
55 sizeof (GdkScreenClass),
56 (GBaseInitFunc) gdk_screen_init,
57 (GBaseFinalizeFunc) NULL,
58 (GClassInitFunc) gdk_screen_class_init,
59 NULL, /* class_finalize */
60 NULL, /* class_data */
63 (GInstanceInitFunc) NULL,
66 object_type = g_type_register_static (G_TYPE_OBJECT,
67 "GdkScreen", &object_info, 0);
74 gdk_screen_class_init (GdkScreenClass *klass)
76 GObjectClass *object_class = G_OBJECT_CLASS (klass);
78 parent_class = g_type_class_peek_parent (klass);
80 object_class->dispose = gdk_screen_dispose;
81 object_class->finalize = gdk_screen_finalize;
84 * GdkScreen::size-changed:
85 * @screen: the object on which the signal is emitted
87 * The ::size_changed signal is emitted when the pixel width or
88 * height of a screen changes.
92 signals[SIZE_CHANGED] =
93 g_signal_new ("size_changed",
94 G_OBJECT_CLASS_TYPE (klass),
96 G_STRUCT_OFFSET (GdkScreenClass, size_changed),
98 g_cclosure_marshal_VOID__VOID,
104 gdk_screen_init (GdkScreen *screen)
106 screen->resolution = -1;
110 gdk_screen_dispose (GObject *object)
112 GdkScreen *screen = GDK_SCREEN (object);
115 for (i = 0; i < 32; ++i)
117 if (screen->exposure_gcs[i])
118 g_object_unref (screen->exposure_gcs[i]);
120 if (screen->normal_gcs[i])
121 g_object_unref (screen->normal_gcs[i]);
124 G_OBJECT_CLASS (parent_class)->dispose (object);
128 gdk_screen_finalize (GObject *object)
130 GdkScreen *screen = GDK_SCREEN (object);
132 if (screen->font_options)
133 cairo_font_options_destroy (screen->font_options);
135 G_OBJECT_CLASS (parent_class)->finalize (object);
139 _gdk_screen_close (GdkScreen *screen)
141 g_return_if_fail (GDK_IS_SCREEN (screen));
145 screen->closed = TRUE;
146 g_object_run_dispose (G_OBJECT (screen));
150 /* Fallback used when the monitor "at" a point or window
154 get_nearest_monitor (GdkScreen *screen,
158 gint num_monitors, i;
159 gint nearest_dist = G_MAXINT;
160 gint nearest_monitor = 0;
162 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
164 num_monitors = gdk_screen_get_n_monitors (screen);
166 for (i = 0; i < num_monitors; i++)
168 GdkRectangle monitor;
171 gdk_screen_get_monitor_geometry (screen, i, &monitor);
174 dist_x = monitor.x - x;
175 else if (x >= monitor.x + monitor.width)
176 dist_x = x - (monitor.x + monitor.width) + 1;
181 dist_y = monitor.y - y;
182 else if (y >= monitor.y + monitor.height)
183 dist_y = y - (monitor.y + monitor.height) + 1;
187 if (MIN (dist_x, dist_y) < nearest_dist)
189 nearest_dist = MIN (dist_x, dist_y);
194 return nearest_monitor;
198 * gdk_screen_get_monitor_at_point:
199 * @screen: a #GdkScreen.
200 * @x: the x coordinate in the virtual screen.
201 * @y: the y coordinate in the virtual screen.
203 * Returns the monitor number in which the point (@x,@y) is located.
205 * Returns: the monitor number in which the point (@x,@y) lies, or
206 * a monitor close to (@x,@y) if the point is not in any monitor.
211 gdk_screen_get_monitor_at_point (GdkScreen *screen,
215 gint num_monitors, i;
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;
225 gdk_screen_get_monitor_geometry (screen, i, &monitor);
227 if (x >= monitor.x &&
228 x < monitor.x + monitor.width &&
230 y < (monitor.y + monitor.height))
234 return get_nearest_monitor (screen, x, y);
238 * gdk_screen_get_monitor_at_window:
239 * @screen: a #GdkScreen.
240 * @window: a #GdkWindow
241 * @returns: the monitor number in which most of @window is located,
242 * or if @window does not intersect any monitors, a monitor,
245 * Returns the number of the monitor in which the largest area of the
246 * bounding rectangle of @window resides.
251 gdk_screen_get_monitor_at_window (GdkScreen *screen,
254 gint num_monitors, i, area = 0, screen_num = -1;
255 GdkRectangle win_rect;
256 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
258 gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width,
259 &win_rect.height, NULL);
260 gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
261 num_monitors = gdk_screen_get_n_monitors (screen);
263 for (i=0;i<num_monitors;i++)
265 GdkRectangle tmp_monitor, intersect;
267 gdk_screen_get_monitor_geometry (screen, i, &tmp_monitor);
268 gdk_rectangle_intersect (&win_rect, &tmp_monitor, &intersect);
270 if (intersect.width * intersect.height > area)
272 area = intersect.width * intersect.height;
279 return get_nearest_monitor (screen,
280 win_rect.x + win_rect.width / 2,
281 win_rect.y + win_rect.height / 2);
287 * Returns the width of the default screen in pixels.
289 * Return value: the width of the default screen in pixels.
292 gdk_screen_width (void)
294 return gdk_screen_get_width (gdk_screen_get_default());
300 * Returns the height of the default screen in pixels.
302 * Return value: the height of the default screen in pixels.
305 gdk_screen_height (void)
307 return gdk_screen_get_height (gdk_screen_get_default());
311 * gdk_screen_width_mm:
313 * Returns the width of the default screen in millimeters.
314 * Note that on many X servers this value will not be correct.
316 * Return value: the width of the default screen in millimeters,
317 * though it is not always correct.
320 gdk_screen_width_mm (void)
322 return gdk_screen_get_width_mm (gdk_screen_get_default());
326 * gdk_screen_height_mm:
328 * Returns the height of the default screen in millimeters.
329 * Note that on many X servers this value will not be correct.
331 * Return value: the height of the default screen in millimeters,
332 * though it is not always correct.
335 gdk_screen_height_mm (void)
337 return gdk_screen_get_height_mm (gdk_screen_get_default ());
341 * gdk_screen_set_font_options_libgtk_only:
342 * @screen: a #GdkScreen
343 * @options: a #cairo_font_options_t, or %NULL to unset any
344 * previously set default font options.
346 * Sets the default font options for the screen. These
347 * options will be set on any #PangoContext's newly created
348 * with gdk_pango_context_get_for_screen(). Changing the
349 * default set of font options does not affect contexts that
350 * have already been created.
352 * This function is not part of the GDK public API and is only
356 gdk_screen_set_font_options_libgtk_only (GdkScreen *screen,
357 const cairo_font_options_t *options)
359 g_return_if_fail (GDK_IS_SCREEN (screen));
361 if (screen->font_options)
362 cairo_font_options_destroy (screen->font_options);
365 screen->font_options = cairo_font_options_copy (options);
367 screen->font_options = NULL;
371 * gdk_screen_get_font_options_libgtk_only:
372 * @screen: a #GdkScreen
374 * Gets any options previously set with gdk_screen_set_font_options_libgtk_only().
376 * Return value: the current font options, or %NULL if no default
377 * font options have been set.
379 const cairo_font_options_t *
380 gdk_screen_get_font_options_libgtk_only (GdkScreen *screen)
382 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
384 return screen->font_options;
388 * gdk_screen_set_resolution_libgtk_only:
389 * @screen: a #GdkScreen
390 * @dpi: the resolution in "dots per inch". (Physical inches aren't actually
391 * involved; the terminology is conventional.)
393 * Sets the resolution for font handling on the screen. This is a
394 * scale factor between points specified in a #PangoFontDescription
395 * and cairo units. The default value is 96, meaning that a 10 point
396 * font will be 13 units high. (10 * 96. / 72. = 13.3).
398 * This function is not part of the GDK public API and is only
402 gdk_screen_set_resolution_libgtk_only (GdkScreen *screen,
405 g_return_if_fail (GDK_IS_SCREEN (screen));
408 screen->resolution = dpi;
410 screen->resolution = -1;
414 * gdk_screen_get_resolution_libgtk_only:
415 * @screen: a #GdkScreen
417 * Gets the resolution for font handling on the screen; see
418 * gdk_screen_set_resolution_libgtk_only() for full details.
420 * Return value: the current resolution, or -1 if no resolution
424 gdk_screen_get_resolution_libgtk_only (GdkScreen *screen)
426 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
428 return screen->resolution;
431 #define __GDK_SCREEN_C__
432 #include "gdkaliasdef.c"