initial import
[vuplus_webkit] / Source / WebCore / platform / gtk / PlatformScreenGtk.cpp
1 /*
2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
4  * Copyright (C) 2008 Christian Dywan <christian@imendio.com>
5  * Copyright (C) 2008 Collabora Ltd.
6  * Copyright (C) 2009 Holger Hans Peter Freyther
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "PlatformScreen.h"
33
34 #include "GtkVersioning.h"
35 #include "HostWindow.h"
36 #include "ScrollView.h"
37 #include "Widget.h"
38
39 #include <gtk/gtk.h>
40
41 #if PLATFORM(X11)
42 #include <gdk/gdkx.h>
43 #include <X11/Xatom.h>
44 #endif
45
46 namespace WebCore {
47
48 static GtkWidget* getToplevel(GtkWidget* widget)
49 {
50     GtkWidget* toplevel = gtk_widget_get_toplevel(widget);
51     return gtk_widget_is_toplevel(toplevel) ? toplevel : 0;
52 }
53
54 static GdkVisual* getVisual(Widget* widget)
55 {
56     if (!widget)
57         return 0;
58
59     GtkWidget* container = GTK_WIDGET(widget->root()->hostWindow()->platformPageClient());
60     if (!container) {
61         GdkScreen* screen = gdk_screen_get_default();
62         return screen ? gdk_screen_get_system_visual(screen) : 0;
63     }
64
65     if (!gtk_widget_get_realized(container))
66         container = getToplevel(container);
67     return container ? gdk_window_get_visual(gtk_widget_get_window(container)) : 0;
68 }
69
70 int screenDepth(Widget* widget)
71 {
72     GdkVisual* visual = getVisual(widget);
73     if (!visual)
74         return 24;
75     return gdk_visual_get_depth(visual);
76 }
77
78 int screenDepthPerComponent(Widget* widget)
79 {
80     GdkVisual* visual = getVisual(widget);
81     if (!visual)
82         return 8;
83
84     return gdk_visual_get_bits_per_rgb(visual);
85 }
86
87 bool screenIsMonochrome(Widget* widget)
88 {
89     return screenDepth(widget) < 2;
90 }
91
92
93 static GdkScreen* getScreen(GtkWidget* widget)
94 {
95     return gtk_widget_has_screen(widget) ? gtk_widget_get_screen(widget) : gdk_screen_get_default();
96 }
97
98 FloatRect screenRect(Widget* widget)
99 {
100     if (!widget)
101         return FloatRect();
102
103     GtkWidget* container = GTK_WIDGET(widget->root()->hostWindow()->platformPageClient());
104     if (container)
105         container = getToplevel(container);
106
107     GdkScreen* screen = container ? getScreen(container) : gdk_screen_get_default();
108     if (!screen)
109         return FloatRect();
110
111     gint monitor = container ? gdk_screen_get_monitor_at_window(screen, gtk_widget_get_window(container)) : 0;
112
113     GdkRectangle geometry;
114     gdk_screen_get_monitor_geometry(screen, monitor, &geometry);
115
116     return FloatRect(geometry.x, geometry.y, geometry.width, geometry.height);
117 }
118
119 FloatRect screenAvailableRect(Widget* widget)
120 {
121     if (!widget)
122         return FloatRect();
123
124 #if PLATFORM(X11)
125     GtkWidget* container = GTK_WIDGET(widget->root()->hostWindow()->platformPageClient());
126     if (!container)
127         return FloatRect();
128
129     if (!gtk_widget_get_realized(container))
130         return screenRect(widget);
131
132     GdkWindow* rootWindow = gtk_widget_get_root_window(container);
133     GdkDisplay* display = gdk_window_get_display(rootWindow);
134     Atom xproperty = gdk_x11_get_xatom_by_name_for_display(display, "_NET_WORKAREA");
135
136     Atom retType;
137     int retFormat;
138     long *workAreaPos = NULL;
139     unsigned long retNItems;
140     unsigned long retAfter;
141     int xRes = XGetWindowProperty(GDK_DISPLAY_XDISPLAY(display), GDK_WINDOW_XWINDOW(rootWindow), xproperty,
142         0, 4, FALSE, XA_CARDINAL, &retType, &retFormat, &retNItems, &retAfter, (guchar**)&workAreaPos);
143
144     FloatRect rect;
145     if (xRes == Success && workAreaPos != NULL && retType == XA_CARDINAL && retNItems == 4 && retFormat == 32) {
146         rect = FloatRect(workAreaPos[0], workAreaPos[1], workAreaPos[2], workAreaPos[3]);
147         // rect contains the available space in the whole screen not just in the monitor
148         // containing the widget, so we intersect it with the monitor rectangle.
149         rect.intersect(screenRect(widget));
150     } else
151         rect = screenRect(widget);
152
153     if (workAreaPos)
154         XFree(workAreaPos);
155
156     return rect;
157 #else
158     return screenRect(widget);
159 #endif
160 }
161
162 } // namespace WebCore