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 "gdkscreenprivate.h"
27 #include "gdkrectangle.h"
28 #include "gdkwindow.h"
34 * @Short_description: Object representing a physical screen
37 * #GdkScreen objects are the GDK representation of a physical screen. It is used
38 * throughout GDK and GTK+ to specify which screen the top level windows
39 * are to be displayed on.
40 * It is also used to query the screen specification and default settings such as
41 * the screen width (gdk_screen_get_width()), etc.
43 * Note that a screen may consist of multiple monitors which are merged to
44 * form a large screen area.
48 static void gdk_screen_finalize (GObject *object);
49 static void gdk_screen_set_property (GObject *object,
53 static void gdk_screen_get_property (GObject *object,
73 static guint signals[LAST_SIGNAL] = { 0 };
75 G_DEFINE_TYPE (GdkScreen, gdk_screen, G_TYPE_OBJECT)
78 gdk_screen_class_init (GdkScreenClass *klass)
80 GObjectClass *object_class = G_OBJECT_CLASS (klass);
82 object_class->finalize = gdk_screen_finalize;
83 object_class->set_property = gdk_screen_set_property;
84 object_class->get_property = gdk_screen_get_property;
86 g_object_class_install_property (object_class,
88 g_param_spec_pointer ("font-options",
90 P_("The default font options for the screen"),
91 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
92 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
94 g_object_class_install_property (object_class,
96 g_param_spec_double ("resolution",
97 P_("Font resolution"),
98 P_("The resolution for fonts on the screen"),
102 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|
103 G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
106 * GdkScreen::size-changed:
107 * @screen: the object on which the signal is emitted
109 * The ::size-changed signal is emitted when the pixel width or
110 * height of a screen changes.
114 signals[SIZE_CHANGED] =
115 g_signal_new (g_intern_static_string ("size-changed"),
116 G_OBJECT_CLASS_TYPE (klass),
118 G_STRUCT_OFFSET (GdkScreenClass, size_changed),
120 g_cclosure_marshal_VOID__VOID,
125 * GdkScreen::composited-changed:
126 * @screen: the object on which the signal is emitted
128 * The ::composited-changed signal is emitted when the composited
129 * status of the screen changes
133 signals[COMPOSITED_CHANGED] =
134 g_signal_new (g_intern_static_string ("composited-changed"),
135 G_OBJECT_CLASS_TYPE (klass),
137 G_STRUCT_OFFSET (GdkScreenClass, composited_changed),
139 g_cclosure_marshal_VOID__VOID,
144 * GdkScreen::monitors-changed:
145 * @screen: the object on which the signal is emitted
147 * The ::monitors-changed signal is emitted when the number, size
148 * or position of the monitors attached to the screen change.
150 * Only for X11 and OS X for now. A future implementation for Win32
151 * may be a possibility.
155 signals[MONITORS_CHANGED] =
156 g_signal_new (g_intern_static_string ("monitors-changed"),
157 G_OBJECT_CLASS_TYPE (klass),
159 G_STRUCT_OFFSET (GdkScreenClass, monitors_changed),
161 g_cclosure_marshal_VOID__VOID,
167 gdk_screen_init (GdkScreen *screen)
169 screen->resolution = -1.;
173 gdk_screen_finalize (GObject *object)
175 GdkScreen *screen = GDK_SCREEN (object);
177 if (screen->font_options)
178 cairo_font_options_destroy (screen->font_options);
180 G_OBJECT_CLASS (gdk_screen_parent_class)->finalize (object);
184 _gdk_screen_close (GdkScreen *screen)
186 g_return_if_fail (GDK_IS_SCREEN (screen));
190 screen->closed = TRUE;
191 g_object_run_dispose (G_OBJECT (screen));
195 /* Fallback used when the monitor "at" a point or window
199 get_nearest_monitor (GdkScreen *screen,
203 gint num_monitors, i;
204 gint nearest_dist = G_MAXINT;
205 gint nearest_monitor = 0;
207 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
209 num_monitors = gdk_screen_get_n_monitors (screen);
211 for (i = 0; i < num_monitors; i++)
213 GdkRectangle monitor;
214 gint dist_x, dist_y, dist;
216 gdk_screen_get_monitor_geometry (screen, i, &monitor);
219 dist_x = monitor.x - x;
220 else if (x >= monitor.x + monitor.width)
221 dist_x = x - (monitor.x + monitor.width) + 1;
226 dist_y = monitor.y - y;
227 else if (y >= monitor.y + monitor.height)
228 dist_y = y - (monitor.y + monitor.height) + 1;
232 dist = dist_x + dist_y;
233 if (dist < nearest_dist)
240 return nearest_monitor;
244 * gdk_screen_get_monitor_at_point:
245 * @screen: a #GdkScreen.
246 * @x: the x coordinate in the virtual screen.
247 * @y: the y coordinate in the virtual screen.
249 * Returns the monitor number in which the point (@x,@y) is located.
251 * Returns: the monitor number in which the point (@x,@y) lies, or
252 * a monitor close to (@x,@y) if the point is not in any monitor.
257 gdk_screen_get_monitor_at_point (GdkScreen *screen,
261 gint num_monitors, i;
263 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
265 num_monitors = gdk_screen_get_n_monitors (screen);
267 for (i=0;i<num_monitors;i++)
269 GdkRectangle monitor;
271 gdk_screen_get_monitor_geometry (screen, i, &monitor);
273 if (x >= monitor.x &&
274 x < monitor.x + monitor.width &&
276 y < (monitor.y + monitor.height))
280 return get_nearest_monitor (screen, x, y);
284 * gdk_screen_get_monitor_at_window:
285 * @screen: a #GdkScreen.
286 * @window: a #GdkWindow
287 * @returns: the monitor number in which most of @window is located,
288 * or if @window does not intersect any monitors, a monitor,
291 * Returns the number of the monitor in which the largest area of the
292 * bounding rectangle of @window resides.
297 gdk_screen_get_monitor_at_window (GdkScreen *screen,
300 gint num_monitors, i, area = 0, screen_num = -1;
301 GdkRectangle win_rect;
303 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
305 gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width,
307 gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
308 num_monitors = gdk_screen_get_n_monitors (screen);
310 for (i=0;i<num_monitors;i++)
312 GdkRectangle tmp_monitor, intersect;
314 gdk_screen_get_monitor_geometry (screen, i, &tmp_monitor);
315 gdk_rectangle_intersect (&win_rect, &tmp_monitor, &intersect);
317 if (intersect.width * intersect.height > area)
319 area = intersect.width * intersect.height;
326 return get_nearest_monitor (screen,
327 win_rect.x + win_rect.width / 2,
328 win_rect.y + win_rect.height / 2);
334 * Returns the width of the default screen in pixels.
336 * Return value: the width of the default screen in pixels.
339 gdk_screen_width (void)
341 return gdk_screen_get_width (gdk_screen_get_default ());
347 * Returns the height of the default screen in pixels.
349 * Return value: the height of the default screen in pixels.
352 gdk_screen_height (void)
354 return gdk_screen_get_height (gdk_screen_get_default ());
358 * gdk_screen_width_mm:
360 * Returns the width of the default screen in millimeters.
361 * Note that on many X servers this value will not be correct.
363 * Return value: the width of the default screen in millimeters,
364 * though it is not always correct.
367 gdk_screen_width_mm (void)
369 return gdk_screen_get_width_mm (gdk_screen_get_default ());
373 * gdk_screen_height_mm:
375 * Returns the height of the default screen in millimeters.
376 * Note that on many X servers this value will not be correct.
378 * Return value: the height of the default screen in millimeters,
379 * though it is not always correct.
382 gdk_screen_height_mm (void)
384 return gdk_screen_get_height_mm (gdk_screen_get_default ());
388 * gdk_screen_set_font_options:
389 * @screen: a #GdkScreen
390 * @options: (allow-none): a #cairo_font_options_t, or %NULL to unset any
391 * previously set default font options.
393 * Sets the default font options for the screen. These
394 * options will be set on any #PangoContext's newly created
395 * with gdk_pango_context_get_for_screen(). Changing the
396 * default set of font options does not affect contexts that
397 * have already been created.
402 gdk_screen_set_font_options (GdkScreen *screen,
403 const cairo_font_options_t *options)
405 g_return_if_fail (GDK_IS_SCREEN (screen));
407 if (screen->font_options != options)
409 if (screen->font_options)
410 cairo_font_options_destroy (screen->font_options);
413 screen->font_options = cairo_font_options_copy (options);
415 screen->font_options = NULL;
417 g_object_notify (G_OBJECT (screen), "font-options");
422 * gdk_screen_get_font_options:
423 * @screen: a #GdkScreen
425 * Gets any options previously set with gdk_screen_set_font_options().
427 * Return value: the current font options, or %NULL if no default
428 * font options have been set.
432 const cairo_font_options_t *
433 gdk_screen_get_font_options (GdkScreen *screen)
435 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
437 return screen->font_options;
441 * gdk_screen_set_resolution:
442 * @screen: a #GdkScreen
443 * @dpi: the resolution in "dots per inch". (Physical inches aren't actually
444 * involved; the terminology is conventional.)
446 * Sets the resolution for font handling on the screen. This is a
447 * scale factor between points specified in a #PangoFontDescription
448 * and cairo units. The default value is 96, meaning that a 10 point
449 * font will be 13 units high. (10 * 96. / 72. = 13.3).
454 gdk_screen_set_resolution (GdkScreen *screen,
457 g_return_if_fail (GDK_IS_SCREEN (screen));
462 if (screen->resolution != dpi)
464 screen->resolution = dpi;
466 g_object_notify (G_OBJECT (screen), "resolution");
471 * gdk_screen_get_resolution:
472 * @screen: a #GdkScreen
474 * Gets the resolution for font handling on the screen; see
475 * gdk_screen_set_resolution() for full details.
477 * Return value: the current resolution, or -1 if no resolution
483 gdk_screen_get_resolution (GdkScreen *screen)
485 g_return_val_if_fail (GDK_IS_SCREEN (screen), -1.0);
487 return screen->resolution;
491 gdk_screen_get_property (GObject *object,
496 GdkScreen *screen = GDK_SCREEN (object);
500 case PROP_FONT_OPTIONS:
501 g_value_set_pointer (value, (gpointer) gdk_screen_get_font_options (screen));
503 case PROP_RESOLUTION:
504 g_value_set_double (value, gdk_screen_get_resolution (screen));
507 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
513 gdk_screen_set_property (GObject *object,
518 GdkScreen *screen = GDK_SCREEN (object);
522 case PROP_FONT_OPTIONS:
523 gdk_screen_set_font_options (screen, g_value_get_pointer (value));
525 case PROP_RESOLUTION:
526 gdk_screen_set_resolution (screen, g_value_get_double (value));
529 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
535 * gdk_screen_get_display:
536 * @screen: a #GdkScreen
538 * Gets the display to which the @screen belongs.
540 * Returns: (transfer none): the display to which @screen belongs
545 gdk_screen_get_display (GdkScreen *screen)
547 return GDK_SCREEN_GET_CLASS(screen)->get_display (screen);
552 * gdk_screen_get_width:
553 * @screen: a #GdkScreen
555 * Gets the width of @screen in pixels
557 * Returns: the width of @screen in pixels.
562 gdk_screen_get_width (GdkScreen *screen)
564 return GDK_SCREEN_GET_CLASS(screen)->get_width (screen);
568 * gdk_screen_get_height:
569 * @screen: a #GdkScreen
571 * Gets the height of @screen in pixels
573 * Returns: the height of @screen in pixels.
578 gdk_screen_get_height (GdkScreen *screen)
580 return GDK_SCREEN_GET_CLASS(screen)->get_height (screen);
584 * gdk_screen_get_width_mm:
585 * @screen: a #GdkScreen
587 * Gets the width of @screen in millimeters.
588 * Note that on some X servers this value will not be correct.
590 * Returns: the width of @screen in millimeters.
595 gdk_screen_get_width_mm (GdkScreen *screen)
597 return GDK_SCREEN_GET_CLASS(screen)->get_width_mm (screen);
601 * gdk_screen_get_height_mm:
602 * @screen: a #GdkScreen
604 * Returns the height of @screen in millimeters.
605 * Note that on some X servers this value will not be correct.
607 * Returns: the heigth of @screen in millimeters.
612 gdk_screen_get_height_mm (GdkScreen *screen)
614 return GDK_SCREEN_GET_CLASS(screen)->get_height_mm (screen);
618 * gdk_screen_get_number:
619 * @screen: a #GdkScreen
621 * Gets the index of @screen among the screens in the display
622 * to which it belongs. (See gdk_screen_get_display())
629 gdk_screen_get_number (GdkScreen *screen)
631 return GDK_SCREEN_GET_CLASS(screen)->get_number (screen);
635 * gdk_screen_get_root_window:
636 * @screen: a #GdkScreen
638 * Gets the root window of @screen.
640 * Returns: (transfer none): the root window
645 gdk_screen_get_root_window (GdkScreen *screen)
647 return GDK_SCREEN_GET_CLASS(screen)->get_root_window (screen);
651 * gdk_screen_get_n_monitors:
652 * @screen: a #GdkScreen
654 * Returns the number of monitors which @screen consists of.
656 * Returns: number of monitors which @screen consists of
661 gdk_screen_get_n_monitors (GdkScreen *screen)
663 return GDK_SCREEN_GET_CLASS(screen)->get_n_monitors (screen);
667 * gdk_screen_get_primary_monitor:
668 * @screen: a #GdkScreen.
670 * Gets the primary monitor for @screen. The primary monitor
671 * is considered the monitor where the 'main desktop' lives.
672 * While normal application windows typically allow the window
673 * manager to place the windows, specialized desktop applications
674 * such as panels should place themselves on the primary monitor.
676 * If no primary monitor is configured by the user, the return value
677 * will be 0, defaulting to the first monitor.
679 * Returns: An integer index for the primary monitor, or 0 if none is configured.
684 gdk_screen_get_primary_monitor (GdkScreen *screen)
686 return GDK_SCREEN_GET_CLASS(screen)->get_primary_monitor (screen);
690 * gdk_screen_get_monitor_width_mm:
691 * @screen: a #GdkScreen
692 * @monitor_num: number of the monitor, between 0 and gdk_screen_get_n_monitors (screen)
694 * Gets the width in millimeters of the specified monitor, if available.
696 * Returns: the width of the monitor, or -1 if not available
701 gdk_screen_get_monitor_width_mm (GdkScreen *screen,
704 return GDK_SCREEN_GET_CLASS(screen)->get_monitor_width_mm (screen, monitor_num);
708 * gdk_screen_get_monitor_height_mm:
709 * @screen: a #GdkScreen
710 * @monitor_num: number of the monitor, between 0 and gdk_screen_get_n_monitors (screen)
712 * Gets the height in millimeters of the specified monitor.
714 * Returns: the height of the monitor, or -1 if not available
719 gdk_screen_get_monitor_height_mm (GdkScreen *screen,
722 return GDK_SCREEN_GET_CLASS(screen)->get_monitor_height_mm (screen, monitor_num);
726 * gdk_screen_get_monitor_plug_name:
727 * @screen: a #GdkScreen
728 * @monitor_num: number of the monitor, between 0 and gdk_screen_get_n_monitors (screen)
730 * Returns the output name of the specified monitor.
731 * Usually something like VGA, DVI, or TV, not the actual
732 * product name of the display device.
734 * Returns: a newly-allocated string containing the name of the monitor,
735 * or %NULL if the name cannot be determined
740 gdk_screen_get_monitor_plug_name (GdkScreen *screen,
743 return GDK_SCREEN_GET_CLASS(screen)->get_monitor_plug_name (screen, monitor_num);
747 * gdk_screen_get_monitor_geometry:
748 * @screen: a #GdkScreen
749 * @monitor_num: the monitor number, between 0 and gdk_screen_get_n_monitors (screen)
750 * @dest: (out) (allow-none): a #GdkRectangle to be filled with the monitor geometry
752 * Retrieves the #GdkRectangle representing the size and position of
753 * the individual monitor within the entire screen area.
755 * Note that the size of the entire screen area can be retrieved via
756 * gdk_screen_get_width() and gdk_screen_get_height().
761 gdk_screen_get_monitor_geometry (GdkScreen *screen,
765 GDK_SCREEN_GET_CLASS(screen)->get_monitor_geometry (screen, monitor_num, dest);
769 * gdk_screen_list_visuals:
770 * @screen: the relevant #GdkScreen.
772 * Lists the available visuals for the specified @screen.
773 * A visual describes a hardware image data format.
774 * For example, a visual might support 24-bit color, or 8-bit color,
775 * and might expect pixels to be in a certain format.
777 * Call g_list_free() on the return value when you're finished with it.
779 * Return value: (transfer container) (element-type GdkVisual):
780 * a list of visuals; the list must be freed, but not its contents
785 gdk_screen_list_visuals (GdkScreen *screen)
787 return GDK_SCREEN_GET_CLASS(screen)->list_visuals (screen);
791 * gdk_screen_get_system_visual:
792 * @screen: a #GdkScreen.
794 * Get the system's default visual for @screen.
795 * This is the visual for the root window of the display.
796 * The return value should not be freed.
798 * Return value: (transfer none): the system visual
803 gdk_screen_get_system_visual (GdkScreen * screen)
805 return GDK_SCREEN_GET_CLASS(screen)->get_system_visual (screen);
809 * gdk_screen_get_rgba_visual:
810 * @screen: a #GdkScreen
812 * Gets a visual to use for creating windows with an alpha channel.
813 * The windowing system on which GTK+ is running
814 * may not support this capability, in which case %NULL will
815 * be returned. Even if a non-%NULL value is returned, its
816 * possible that the window's alpha channel won't be honored
817 * when displaying the window on the screen: in particular, for
818 * X an appropriate windowing manager and compositing manager
819 * must be running to provide appropriate display.
821 * This functionality is not implemented in the Windows backend.
823 * For setting an overall opacity for a top-level window, see
824 * gdk_window_set_opacity().
826 * Return value: (transfer none): a visual to use for windows with an
827 * alpha channel or %NULL if the capability is not available.
832 gdk_screen_get_rgba_visual (GdkScreen *screen)
834 return GDK_SCREEN_GET_CLASS(screen)->get_rgba_visual (screen);
838 * gdk_screen_is_composited:
839 * @screen: a #GdkScreen
841 * Returns whether windows with an RGBA visual can reasonably
842 * be expected to have their alpha channel drawn correctly on
845 * On X11 this function returns whether a compositing manager is
846 * compositing @screen.
848 * Return value: Whether windows with RGBA visuals can reasonably be
849 * expected to have their alpha channels drawn correctly on the screen.
854 gdk_screen_is_composited (GdkScreen *screen)
856 return GDK_SCREEN_GET_CLASS(screen)->is_composited (screen);
860 * gdk_screen_make_display_name:
861 * @screen: a #GdkScreen
863 * Determines the name to pass to gdk_display_open() to get
864 * a #GdkDisplay with this screen as the default screen.
866 * Return value: a newly allocated string, free with g_free()
871 gdk_screen_make_display_name (GdkScreen *screen)
873 return GDK_SCREEN_GET_CLASS(screen)->make_display_name (screen);
877 * gdk_screen_get_active_window:
878 * @screen: a #GdkScreen
880 * Returns the screen's currently active window.
882 * On X11, this is done by inspecting the _NET_ACTIVE_WINDOW property
883 * on the root window, as described in the <ulink
884 * url="http://www.freedesktop.org/Standards/wm-spec">Extended Window
885 * Manager Hints</ulink>. If there is no currently currently active
886 * window, or the window manager does not support the
887 * _NET_ACTIVE_WINDOW hint, this function returns %NULL.
889 * On other platforms, this function may return %NULL, depending on whether
890 * it is implementable on that platform.
892 * The returned window should be unrefed using g_object_unref() when
895 * Return value: (transfer full): the currently active window, or %NULL.
900 gdk_screen_get_active_window (GdkScreen *screen)
902 return GDK_SCREEN_GET_CLASS(screen)->get_active_window (screen);
906 * gdk_screen_get_window_stack:
907 * @screen: a #GdkScreen
909 * Returns a #GList of #GdkWindow<!-- -->s representing the current
912 * On X11, this is done by inspecting the _NET_CLIENT_LIST_STACKING
913 * property on the root window, as described in the <ulink
914 * url="http://www.freedesktop.org/Standards/wm-spec">Extended Window
915 * Manager Hints</ulink>. If the window manager does not support the
916 * _NET_CLIENT_LIST_STACKING hint, this function returns %NULL.
918 * On other platforms, this function may return %NULL, depending on whether
919 * it is implementable on that platform.
921 * The returned list is newly allocated and owns references to the
922 * windows it contains, so it should be freed using g_list_free() and
923 * its windows unrefed using g_object_unref() when no longer needed.
925 * Return value: (transfer full) (element-type GdkWindow):
926 * a list of #GdkWindow<!-- -->s for the current window stack,
932 gdk_screen_get_window_stack (GdkScreen *screen)
934 return GDK_SCREEN_GET_CLASS(screen)->get_window_stack (screen);
938 * gdk_screen_broadcast_client_message:
939 * @screen: the #GdkScreen where the event will be broadcasted.
940 * @event: the #GdkEvent.
942 * On X11, sends an X ClientMessage event to all toplevel windows on
945 * Toplevel windows are determined by checking for the WM_STATE property,
946 * as described in the Inter-Client Communication Conventions Manual (ICCCM).
947 * If no windows are found with the WM_STATE property set, the message is
948 * sent to all children of the root window.
950 * On Windows, broadcasts a message registered with the name
951 * GDK_WIN32_CLIENT_MESSAGE to all top-level windows. The amount of
952 * data is limited to one long, i.e. four bytes.
957 gdk_screen_broadcast_client_message (GdkScreen *screen,
960 return GDK_SCREEN_GET_CLASS(screen)->broadcast_client_message (screen, event);
964 * gdk_screen_get_setting:
965 * @screen: the #GdkScreen where the setting is located
966 * @name: the name of the setting
967 * @value: location to store the value of the setting
969 * Retrieves a desktop-wide setting such as double-click time
970 * for the #GdkScreen @screen.
972 * FIXME needs a list of valid settings here, or a link to
975 * Returns: %TRUE if the setting existed and a value was stored
976 * in @value, %FALSE otherwise.
981 gdk_screen_get_setting (GdkScreen *screen,
985 return GDK_SCREEN_GET_CLASS(screen)->get_setting (screen, name, value);