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 "gdk.h" /* For gdk_rectangle_intersect() */
28 #include "gdkwindow.h"
29 #include "gdkscreen.h"
31 static void gdk_screen_class_init (GdkScreenClass *klass);
32 static void gdk_screen_dispose (GObject *object);
40 static guint signals[LAST_SIGNAL] = { 0 };
42 static gpointer parent_class = NULL;
45 gdk_screen_get_type (void)
47 static GType object_type = 0;
51 static const GTypeInfo object_info =
53 sizeof (GdkScreenClass),
55 (GBaseFinalizeFunc) NULL,
56 (GClassInitFunc) gdk_screen_class_init,
57 NULL, /* class_finalize */
58 NULL, /* class_data */
61 (GInstanceInitFunc) NULL,
64 object_type = g_type_register_static (G_TYPE_OBJECT,
65 "GdkScreen", &object_info, 0);
72 gdk_screen_class_init (GdkScreenClass *klass)
74 GObjectClass *object_class = G_OBJECT_CLASS (klass);
76 parent_class = g_type_class_peek_parent (klass);
78 object_class->dispose = gdk_screen_dispose;
81 * GdkScreen::size-changed:
82 * @screen: the object on which the signal is emitted
84 * The ::size_changed signal is emitted when the pixel width or
85 * height of a screen changes.
89 signals[SIZE_CHANGED] =
90 g_signal_new ("size_changed",
91 G_OBJECT_CLASS_TYPE (klass),
93 G_STRUCT_OFFSET (GdkScreenClass, size_changed),
95 g_cclosure_marshal_VOID__VOID,
101 gdk_screen_dispose (GObject *object)
103 GdkScreen *screen = GDK_SCREEN (object);
106 for (i = 0; i < 32; ++i)
108 if (screen->exposure_gcs[i])
109 g_object_unref (screen->exposure_gcs[i]);
111 if (screen->normal_gcs[i])
112 g_object_unref (screen->normal_gcs[i]);
115 G_OBJECT_CLASS (parent_class)->dispose (object);
119 _gdk_screen_close (GdkScreen *screen)
121 g_return_if_fail (GDK_IS_SCREEN (screen));
125 screen->closed = TRUE;
126 g_object_run_dispose (G_OBJECT (screen));
130 /* Fallback used when the monitor "at" a point or window
134 get_nearest_monitor (GdkScreen *screen,
138 gint num_monitors, i;
139 gint nearest_dist = G_MAXINT;
140 gint nearest_monitor = 0;
142 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
144 num_monitors = gdk_screen_get_n_monitors (screen);
146 for (i = 0; i < num_monitors; i++)
148 GdkRectangle monitor;
151 gdk_screen_get_monitor_geometry (screen, i, &monitor);
154 dist_x = monitor.x - x;
155 else if (x >= monitor.x + monitor.width)
156 dist_x = x - (monitor.x + monitor.width) + 1;
161 dist_y = monitor.y - y;
162 else if (y >= monitor.y + monitor.height)
163 dist_y = y - (monitor.y + monitor.height) + 1;
167 if (MIN (dist_x, dist_y) < nearest_dist)
169 nearest_dist = MIN (dist_x, dist_y);
174 return nearest_monitor;
178 * gdk_screen_get_monitor_at_point:
179 * @screen: a #GdkScreen.
180 * @x: the x coordinate in the virtual screen.
181 * @y: the y coordinate in the virtual screen.
183 * Returns the monitor number in which the point (@x,@y) is located.
185 * Returns: the monitor number in which the point (@x,@y) lies, or
186 * a monitor close to (@x,@y) if the point is not in any monitor.
191 gdk_screen_get_monitor_at_point (GdkScreen *screen,
195 gint num_monitors, i;
197 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
199 num_monitors = gdk_screen_get_n_monitors (screen);
201 for (i=0;i<num_monitors;i++)
203 GdkRectangle monitor;
205 gdk_screen_get_monitor_geometry (screen, i, &monitor);
207 if (x >= monitor.x &&
208 x < monitor.x + monitor.width &&
210 y < (monitor.y + monitor.height))
214 return get_nearest_monitor (screen, x, y);
218 * gdk_screen_get_monitor_at_window:
219 * @screen: a #GdkScreen.
220 * @window: a #GdkWindow
221 * @returns: the monitor number in which most of @window is located,
222 * or if @window does not intersect any monitors, a monitor,
225 * Returns the number of the monitor in which the largest area of the
226 * bounding rectangle of @window resides.
231 gdk_screen_get_monitor_at_window (GdkScreen *screen,
234 gint num_monitors, i, area = 0, screen_num = -1;
235 GdkRectangle win_rect;
236 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
238 gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width,
239 &win_rect.height, NULL);
240 gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
241 num_monitors = gdk_screen_get_n_monitors (screen);
243 for (i=0;i<num_monitors;i++)
245 GdkRectangle tmp_monitor, intersect;
247 gdk_screen_get_monitor_geometry (screen, i, &tmp_monitor);
248 gdk_rectangle_intersect (&win_rect, &tmp_monitor, &intersect);
250 if (intersect.width * intersect.height > area)
252 area = intersect.width * intersect.height;
259 return get_nearest_monitor (screen,
260 win_rect.x + win_rect.width / 2,
261 win_rect.y + win_rect.height / 2);
267 * Returns the width of the default screen in pixels.
269 * Return value: the width of the default screen in pixels.
272 gdk_screen_width (void)
274 return gdk_screen_get_width (gdk_screen_get_default());
280 * Returns the height of the default screen in pixels.
282 * Return value: the height of the default screen in pixels.
285 gdk_screen_height (void)
287 return gdk_screen_get_height (gdk_screen_get_default());
291 * gdk_screen_width_mm:
293 * Returns the width of the default screen in millimeters.
294 * Note that on many X servers this value will not be correct.
296 * Return value: the width of the default screen in millimeters,
297 * though it is not always correct.
300 gdk_screen_width_mm (void)
302 return gdk_screen_get_width_mm (gdk_screen_get_default());
306 * gdk_screen_height_mm:
308 * Returns the height of the default screen in millimeters.
309 * Note that on many X servers this value will not be correct.
311 * Return value: the height of the default screen in millimeters,
312 * though it is not always correct.
315 gdk_screen_height_mm (void)
317 return gdk_screen_get_height_mm (gdk_screen_get_default ());