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.
24 #include "gdk.h" /* For gdk_rectangle_intersect() */
26 #include "gdkinternals.h"
27 #include "gdkwindow.h"
28 #include "gdkscreen.h"
30 static void gdk_screen_class_init (GdkScreenClass *klass);
38 static guint signals[LAST_SIGNAL] = { 0 };
41 gdk_screen_get_type (void)
43 static GType object_type = 0;
47 static const GTypeInfo object_info =
49 sizeof (GdkScreenClass),
51 (GBaseFinalizeFunc) NULL,
52 (GClassInitFunc) gdk_screen_class_init,
53 NULL, /* class_finalize */
54 NULL, /* class_data */
57 (GInstanceInitFunc) NULL,
60 object_type = g_type_register_static (G_TYPE_OBJECT,
61 "GdkScreen", &object_info, 0);
68 gdk_screen_class_init (GdkScreenClass *klass)
70 signals[SIZE_CHANGED] =
71 g_signal_new ("size_changed",
72 G_OBJECT_CLASS_TYPE (klass),
74 G_STRUCT_OFFSET (GdkScreenClass, size_changed),
76 g_cclosure_marshal_VOID__VOID,
82 _gdk_screen_close (GdkScreen *screen)
84 g_return_if_fail (GDK_IS_SCREEN (screen));
88 screen->closed = TRUE;
89 g_object_run_dispose (G_OBJECT (screen));
93 /* Fallback used when the monitor "at" a point or window
97 get_nearest_monitor (GdkScreen *screen,
101 gint num_monitors, i;
102 gint nearest_dist = G_MAXINT;
103 gint nearest_monitor = 0;
105 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
107 num_monitors = gdk_screen_get_n_monitors (screen);
109 for (i = 0; i < num_monitors; i++)
111 GdkRectangle monitor;
114 gdk_screen_get_monitor_geometry (screen, i, &monitor);
117 dist_x = monitor.x - x;
118 else if (x >= monitor.x + monitor.width)
119 dist_x = x - (monitor.x + monitor.width) + 1;
124 dist_y = monitor.y - y;
125 else if (y >= monitor.y + monitor.height)
126 dist_y = y - (monitor.y + monitor.height) + 1;
130 if (MIN (dist_x, dist_y) < nearest_dist)
132 nearest_dist = MIN (dist_x, dist_y);
137 return nearest_monitor;
141 * gdk_screen_get_monitor_at_point:
142 * @screen: a #GdkScreen.
143 * @x: the x coordinate in the virtual screen.
144 * @y: the y coordinate in the virtual screen.
146 * Returns the monitor number in which the point (@x,@y) is located.
148 * Returns: the monitor number in which the point (@x,@y) lies, or
149 * a monitor close to (@x,@y) if the point is not in any monitor.
154 gdk_screen_get_monitor_at_point (GdkScreen *screen,
158 gint num_monitors, i;
160 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
162 num_monitors = gdk_screen_get_n_monitors (screen);
164 for (i=0;i<num_monitors;i++)
166 GdkRectangle monitor;
168 gdk_screen_get_monitor_geometry (screen, i, &monitor);
170 if (x >= monitor.x &&
171 x < monitor.x + monitor.width &&
173 y < (monitor.y + monitor.height))
177 return get_nearest_monitor (screen, x, y);
181 * gdk_screen_get_monitor_at_window:
182 * @screen: a #GdkScreen.
183 * @window: a #GdkWindow
184 * @returns: the monitor number in which most of @window is located,
185 * or if @window does not intersect any monitors, a monitor,
188 * Returns the number of the monitor in which the largest area of the
189 * bounding rectangle of @window resides.
194 gdk_screen_get_monitor_at_window (GdkScreen *screen,
197 gint num_monitors, i, area = 0, screen_num = -1;
198 GdkRectangle win_rect;
199 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
201 gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width,
202 &win_rect.height, NULL);
203 gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
204 num_monitors = gdk_screen_get_n_monitors (screen);
206 for (i=0;i<num_monitors;i++)
208 GdkRectangle tmp_monitor, intersect;
210 gdk_screen_get_monitor_geometry (screen, i, &tmp_monitor);
211 gdk_rectangle_intersect (&win_rect, &tmp_monitor, &intersect);
213 if (intersect.width * intersect.height > area)
215 area = intersect.width * intersect.height;
222 return get_nearest_monitor (screen,
223 win_rect.x + win_rect.width / 2,
224 win_rect.y + win_rect.height / 2);
230 * Returns the width of the default screen in pixels.
232 * Return value: the width of the default screen in pixels.
235 gdk_screen_width (void)
237 return gdk_screen_get_width (gdk_screen_get_default());
243 * Returns the height of the default screen in pixels.
245 * Return value: the height of the default screen in pixels.
248 gdk_screen_height (void)
250 return gdk_screen_get_height (gdk_screen_get_default());
254 * gdk_screen_width_mm:
256 * Returns the width of the default screen in millimeters.
257 * Note that on many X servers this value will not be correct.
259 * Return value: the width of the default screen in millimeters,
260 * though it is not always correct.
263 gdk_screen_width_mm (void)
265 return gdk_screen_get_width_mm (gdk_screen_get_default());
269 * gdk_screen_height_mm:
271 * Returns the height of the default screen in millimeters.
272 * Note that on many X servers this value will not be correct.
274 * Return value: the height of the default screen in millimeters,
275 * though it is not always correct.
278 gdk_screen_height_mm (void)
280 return gdk_screen_get_height_mm (gdk_screen_get_default ());