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"
30 static void gdk_screen_class_init (GdkScreenClass *klass);
31 static void gdk_screen_dispose (GObject *object);
39 static guint signals[LAST_SIGNAL] = { 0 };
41 static gpointer parent_class = NULL;
44 gdk_screen_get_type (void)
46 static GType object_type = 0;
50 static const GTypeInfo object_info =
52 sizeof (GdkScreenClass),
54 (GBaseFinalizeFunc) NULL,
55 (GClassInitFunc) gdk_screen_class_init,
56 NULL, /* class_finalize */
57 NULL, /* class_data */
60 (GInstanceInitFunc) NULL,
63 object_type = g_type_register_static (G_TYPE_OBJECT,
64 "GdkScreen", &object_info, 0);
71 gdk_screen_class_init (GdkScreenClass *klass)
73 GObjectClass *object_class = G_OBJECT_CLASS (klass);
75 parent_class = g_type_class_peek_parent (klass);
77 object_class->dispose = gdk_screen_dispose;
80 * GdkScreen::size-changed:
81 * @screen: the object on which the signal is emitted
83 * The ::size_changed signal is emitted when the pixel width or
84 * height of a screen changes.
88 signals[SIZE_CHANGED] =
89 g_signal_new ("size_changed",
90 G_OBJECT_CLASS_TYPE (klass),
92 G_STRUCT_OFFSET (GdkScreenClass, size_changed),
94 g_cclosure_marshal_VOID__VOID,
100 gdk_screen_dispose (GObject *object)
102 GdkScreen *screen = GDK_SCREEN (object);
105 for (i = 0; i < 32; ++i)
107 if (screen->exposure_gcs[i])
108 g_object_unref (screen->exposure_gcs[i]);
110 if (screen->normal_gcs[i])
111 g_object_unref (screen->normal_gcs[i]);
114 G_OBJECT_CLASS (parent_class)->dispose (object);
118 _gdk_screen_close (GdkScreen *screen)
120 g_return_if_fail (GDK_IS_SCREEN (screen));
124 screen->closed = TRUE;
125 g_object_run_dispose (G_OBJECT (screen));
129 /* Fallback used when the monitor "at" a point or window
133 get_nearest_monitor (GdkScreen *screen,
137 gint num_monitors, i;
138 gint nearest_dist = G_MAXINT;
139 gint nearest_monitor = 0;
141 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
143 num_monitors = gdk_screen_get_n_monitors (screen);
145 for (i = 0; i < num_monitors; i++)
147 GdkRectangle monitor;
150 gdk_screen_get_monitor_geometry (screen, i, &monitor);
153 dist_x = monitor.x - x;
154 else if (x >= monitor.x + monitor.width)
155 dist_x = x - (monitor.x + monitor.width) + 1;
160 dist_y = monitor.y - y;
161 else if (y >= monitor.y + monitor.height)
162 dist_y = y - (monitor.y + monitor.height) + 1;
166 if (MIN (dist_x, dist_y) < nearest_dist)
168 nearest_dist = MIN (dist_x, dist_y);
173 return nearest_monitor;
177 * gdk_screen_get_monitor_at_point:
178 * @screen: a #GdkScreen.
179 * @x: the x coordinate in the virtual screen.
180 * @y: the y coordinate in the virtual screen.
182 * Returns the monitor number in which the point (@x,@y) is located.
184 * Returns: the monitor number in which the point (@x,@y) lies, or
185 * a monitor close to (@x,@y) if the point is not in any monitor.
190 gdk_screen_get_monitor_at_point (GdkScreen *screen,
194 gint num_monitors, i;
196 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
198 num_monitors = gdk_screen_get_n_monitors (screen);
200 for (i=0;i<num_monitors;i++)
202 GdkRectangle monitor;
204 gdk_screen_get_monitor_geometry (screen, i, &monitor);
206 if (x >= monitor.x &&
207 x < monitor.x + monitor.width &&
209 y < (monitor.y + monitor.height))
213 return get_nearest_monitor (screen, x, y);
217 * gdk_screen_get_monitor_at_window:
218 * @screen: a #GdkScreen.
219 * @window: a #GdkWindow
220 * @returns: the monitor number in which most of @window is located,
221 * or if @window does not intersect any monitors, a monitor,
224 * Returns the number of the monitor in which the largest area of the
225 * bounding rectangle of @window resides.
230 gdk_screen_get_monitor_at_window (GdkScreen *screen,
233 gint num_monitors, i, area = 0, screen_num = -1;
234 GdkRectangle win_rect;
235 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
237 gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width,
238 &win_rect.height, NULL);
239 gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
240 num_monitors = gdk_screen_get_n_monitors (screen);
242 for (i=0;i<num_monitors;i++)
244 GdkRectangle tmp_monitor, intersect;
246 gdk_screen_get_monitor_geometry (screen, i, &tmp_monitor);
247 gdk_rectangle_intersect (&win_rect, &tmp_monitor, &intersect);
249 if (intersect.width * intersect.height > area)
251 area = intersect.width * intersect.height;
258 return get_nearest_monitor (screen,
259 win_rect.x + win_rect.width / 2,
260 win_rect.y + win_rect.height / 2);
266 * Returns the width of the default screen in pixels.
268 * Return value: the width of the default screen in pixels.
271 gdk_screen_width (void)
273 return gdk_screen_get_width (gdk_screen_get_default());
279 * Returns the height of the default screen in pixels.
281 * Return value: the height of the default screen in pixels.
284 gdk_screen_height (void)
286 return gdk_screen_get_height (gdk_screen_get_default());
290 * gdk_screen_width_mm:
292 * Returns the width of the default screen in millimeters.
293 * Note that on many X servers this value will not be correct.
295 * Return value: the width of the default screen in millimeters,
296 * though it is not always correct.
299 gdk_screen_width_mm (void)
301 return gdk_screen_get_width_mm (gdk_screen_get_default());
305 * gdk_screen_height_mm:
307 * Returns the height of the default screen in millimeters.
308 * Note that on many X servers this value will not be correct.
310 * Return value: the height of the default screen in millimeters,
311 * though it is not always correct.
314 gdk_screen_height_mm (void)
316 return gdk_screen_get_height_mm (gdk_screen_get_default ());