Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: ui/gfx/screen_gtk.cc

Issue 9960042: Refactor screen/monitor so that gfx::Screen returns monitor object. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/screen.h" 5 #include "ui/gfx/screen.h"
6 6
7 #include <gdk/gdkx.h> 7 #include <gdk/gdkx.h>
8 #include <gtk/gtk.h> 8 #include <gtk/gtk.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "ui/gfx/monitor.h"
11 12
12 namespace { 13 namespace {
13 14
14 bool GetScreenWorkArea(gfx::Rect* out_rect) { 15 bool GetScreenWorkArea(gfx::Rect* out_rect) {
15 gboolean ok; 16 gboolean ok;
16 guchar* raw_data = NULL; 17 guchar* raw_data = NULL;
17 gint data_len = 0; 18 gint data_len = 0;
18 ok = gdk_property_get(gdk_get_default_root_window(), // a gdk window 19 ok = gdk_property_get(gdk_get_default_root_window(), // a gdk window
19 gdk_atom_intern("_NET_WORKAREA", FALSE), // property 20 gdk_atom_intern("_NET_WORKAREA", FALSE), // property
20 gdk_atom_intern("CARDINAL", FALSE), // property type 21 gdk_atom_intern("CARDINAL", FALSE), // property type
(...skipping 18 matching lines...) Expand all
39 gint x = data[0]; 40 gint x = data[0];
40 gint y = data[1]; 41 gint y = data[1];
41 gint width = data[2]; 42 gint width = data[2];
42 gint height = data[3]; 43 gint height = data[3];
43 g_free(raw_data); 44 g_free(raw_data);
44 45
45 out_rect->SetRect(x, y, width, height); 46 out_rect->SetRect(x, y, width, height);
46 return true; 47 return true;
47 } 48 }
48 49
49 } // namespace 50 gfx::Rect NativePrimaryMonitorBounds() {
50 51 GdkScreen* screen = gdk_screen_get_default();
51 namespace gfx { 52 GdkRectangle rect;
52 53 gdk_screen_get_monitor_geometry(screen, 0, &rect);
53 // static 54 return gfx::Rect(rect);
54 gfx::Point Screen::GetCursorScreenPoint() {
55 gint x, y;
56 gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL);
57 return gfx::Point(x, y);
58 } 55 }
59 56
60 // static 57 gfx::Rect GetMonitorAreaNearestWindow(gfx::NativeView view) {
61 gfx::Rect Screen::GetMonitorWorkAreaNearestWindow(gfx::NativeView view) {
62 // Do not use the _NET_WORKAREA here, this is supposed to be an area on a
63 // specific monitor, and _NET_WORKAREA is a hint from the WM that generally
64 // spans across all monitors. This would make the work area larger than the
65 // monitor.
66 // TODO(danakj) This is a work-around as there is no standard way to get this
67 // area, but it is a rect that we should be computing. The standard means
68 // to compute this rect would be to watch all windows with
69 // _NET_WM_STRUT(_PARTIAL) hints, and subtract their space from the physical
70 // area of the monitor to construct a work area.
71 return GetMonitorAreaNearestWindow(view);
72 }
73
74 // static
75 gfx::Rect Screen::GetMonitorAreaNearestWindow(gfx::NativeView view) {
76 GdkScreen* screen = gdk_screen_get_default(); 58 GdkScreen* screen = gdk_screen_get_default();
77 gint monitor_num = 0; 59 gint monitor_num = 0;
78 if (view) { 60 if (view) {
79 GtkWidget* top_level = gtk_widget_get_toplevel(view); 61 GtkWidget* top_level = gtk_widget_get_toplevel(
62 const_cast<gfx::NativeView>(view));
80 DCHECK(GTK_IS_WINDOW(top_level)); 63 DCHECK(GTK_IS_WINDOW(top_level));
81 GtkWindow* window = GTK_WINDOW(top_level); 64 GtkWindow* window = GTK_WINDOW(top_level);
82 screen = gtk_window_get_screen(window); 65 screen = gtk_window_get_screen(window);
83 monitor_num = gdk_screen_get_monitor_at_window( 66 monitor_num = gdk_screen_get_monitor_at_window(
84 screen, 67 screen,
85 gtk_widget_get_window(top_level)); 68 gtk_widget_get_window(top_level));
86 } 69 }
87 GdkRectangle bounds; 70 GdkRectangle bounds;
88 gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds); 71 gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds);
89 return gfx::Rect(bounds); 72 return gfx::Rect(bounds);
90 } 73 }
91 74
75 } // namespace
76
77 namespace gfx {
78
92 // static 79 // static
93 gfx::Rect Screen::GetMonitorWorkAreaNearestPoint(const gfx::Point& point) { 80 gfx::Point Screen::GetCursorScreenPoint() {
94 // TODO(jamiewalch): Restrict this to the work area of the monitor. 81 gint x, y;
95 return GetMonitorAreaNearestPoint(point); 82 gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL);
83 return gfx::Point(x, y);
96 } 84 }
97 85
98 // static 86 // static
99 gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) {
100 GdkScreen* screen = gdk_screen_get_default();
101 gint monitor = gdk_screen_get_monitor_at_point(screen, point.x(), point.y());
102 GdkRectangle bounds;
103 gdk_screen_get_monitor_geometry(screen, monitor, &bounds);
104 return gfx::Rect(bounds);
105 }
106
107 // static
108 gfx::Rect Screen::GetPrimaryMonitorWorkArea() {
109 gfx::Rect rect;
110 if (GetScreenWorkArea(&rect))
111 return rect.Intersect(GetPrimaryMonitorBounds());
112
113 // Return the best we've got.
114 return GetPrimaryMonitorBounds();
115 }
116
117 // static
118 gfx::Rect Screen::GetPrimaryMonitorBounds() {
119 GdkScreen* screen = gdk_screen_get_default();
120 GdkRectangle rect;
121 gdk_screen_get_monitor_geometry(screen, 0, &rect);
122 return gfx::Rect(rect);
123 }
124
125 // static
126 gfx::Rect Screen::GetMonitorWorkAreaMatching(const gfx::Rect& match_rect) {
127 // TODO(thestig) Implement multi-monitor support.
128 return GetPrimaryMonitorWorkArea();
129 }
130
131 // static
132 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { 87 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() {
133 GdkWindow* window = gdk_window_at_pointer(NULL, NULL); 88 GdkWindow* window = gdk_window_at_pointer(NULL, NULL);
134 if (!window) 89 if (!window)
135 return NULL; 90 return NULL;
136 91
137 gpointer data = NULL; 92 gpointer data = NULL;
138 gdk_window_get_user_data(window, &data); 93 gdk_window_get_user_data(window, &data);
139 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data); 94 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data);
140 if (!widget) 95 if (!widget)
141 return NULL; 96 return NULL;
142 widget = gtk_widget_get_toplevel(widget); 97 widget = gtk_widget_get_toplevel(widget);
143 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL; 98 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL;
144 } 99 }
145 100
146 // static 101 // static
147 gfx::Size Screen::GetPrimaryMonitorSize() { 102 gfx::Monitor Screen::GetMonitorNearestWindow(gfx::NativeView view) {
148 GdkScreen* screen = gdk_screen_get_default(); 103 gfx::Rect bounds = GetMonitorAreaNearestWindow(view);
149 return gfx::Size(gdk_screen_get_width(screen), gdk_screen_get_height(screen)); 104 // Do not use the _NET_WORKAREA here, this is supposed to be an area on a
105 // specific monitor, and _NET_WORKAREA is a hint from the WM that generally
106 // spans across all monitors. This would make the work area larger than the
107 // monitor.
108 // TODO(danakj) This is a work-around as there is no standard way to get this
109 // area, but it is a rect that we should be computing. The standard means
110 // to compute this rect would be to watch all windows with
111 // _NET_WM_STRUT(_PARTIAL) hints, and subtract their space from the physical
112 // area of the monitor to construct a work area.
113 return gfx::Monitor(bounds);
150 } 114 }
151 115
152 // static 116 // static
117 gfx::Monitor Screen::GetMonitorNearestPoint(const gfx::Point& point) {
118 GdkScreen* screen = gdk_screen_get_default();
119 gint monitor = gdk_screen_get_monitor_at_point(screen, point.x(), point.y());
120 GdkRectangle bounds;
121 gdk_screen_get_monitor_geometry(screen, monitor, &bounds);
122 return gfx::Monitor(gfx::Rect(bounds));
123 }
124
125 // static
126 gfx::Monitor Screen::GetPrimaryMonitor() {
127 gfx::Rect bounds = NativePrimaryMonitorBounds();
128 gfx::Monitor monitor(bounds);
129 gfx::Rect rect;
130 if (GetScreenWorkArea(&rect)) {
131 monitor.set_work_area(rect.Intersect(bounds));
132 } else {
133 // Return the best we've got.
134 monitor.set_work_area(bounds);
135 }
136 return monitor;
137 }
138
139 // static
140 gfx::Monitor Screen::GetMonitorMatching(const gfx::Rect& match_rect) {
141 // TODO(thestig) Implement multi-monitor support.
142 return GetPrimaryMonitor();
143 }
144
145 // static
153 int Screen::GetNumMonitors() { 146 int Screen::GetNumMonitors() {
154 // This query is kinda bogus for Linux -- do we want number of X screens? 147 // This query is kinda bogus for Linux -- do we want number of X screens?
155 // The number of monitors Xinerama has? We'll just use whatever GDK uses. 148 // The number of monitors Xinerama has? We'll just use whatever GDK uses.
156 GdkScreen* screen = gdk_screen_get_default(); 149 GdkScreen* screen = gdk_screen_get_default();
157 return gdk_screen_get_n_monitors(screen); 150 return gdk_screen_get_n_monitors(screen);
158 } 151 }
159 152
160 } // namespace gfx 153 } // namespace gfx
OLDNEW
« ui/gfx/screen_aura.cc ('K') | « ui/gfx/screen_aurax11.cc ('k') | ui/gfx/screen_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698