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

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: sync 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
« no previous file with comments | « ui/gfx/screen_aurax11.cc ('k') | ui/gfx/screen_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(view);
80 DCHECK(GTK_IS_WINDOW(top_level)); 62 DCHECK(GTK_IS_WINDOW(top_level));
81 GtkWindow* window = GTK_WINDOW(top_level); 63 GtkWindow* window = GTK_WINDOW(top_level);
82 screen = gtk_window_get_screen(window); 64 screen = gtk_window_get_screen(window);
83 monitor_num = gdk_screen_get_monitor_at_window( 65 monitor_num = gdk_screen_get_monitor_at_window(
84 screen, 66 screen,
85 gtk_widget_get_window(top_level)); 67 gtk_widget_get_window(top_level));
86 } 68 }
87 GdkRectangle bounds; 69 GdkRectangle bounds;
88 gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds); 70 gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds);
89 return gfx::Rect(bounds); 71 return gfx::Rect(bounds);
90 } 72 }
91 73
74 } // namespace
75
76 namespace gfx {
77
92 // static 78 // static
93 gfx::Rect Screen::GetMonitorWorkAreaNearestPoint(const gfx::Point& point) { 79 gfx::Point Screen::GetCursorScreenPoint() {
94 // TODO(jamiewalch): Restrict this to the work area of the monitor. 80 gint x, y;
95 return GetMonitorAreaNearestPoint(point); 81 gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL);
82 return gfx::Point(x, y);
96 } 83 }
97 84
98 // static 85 // 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() { 86 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() {
133 GdkWindow* window = gdk_window_at_pointer(NULL, NULL); 87 GdkWindow* window = gdk_window_at_pointer(NULL, NULL);
134 if (!window) 88 if (!window)
135 return NULL; 89 return NULL;
136 90
137 gpointer data = NULL; 91 gpointer data = NULL;
138 gdk_window_get_user_data(window, &data); 92 gdk_window_get_user_data(window, &data);
139 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data); 93 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data);
140 if (!widget) 94 if (!widget)
141 return NULL; 95 return NULL;
142 widget = gtk_widget_get_toplevel(widget); 96 widget = gtk_widget_get_toplevel(widget);
143 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL; 97 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL;
144 } 98 }
145 99
146 // static 100 // static
147 gfx::Size Screen::GetPrimaryMonitorSize() { 101 gfx::Monitor Screen::GetMonitorNearestWindow(gfx::NativeView view) {
148 GdkScreen* screen = gdk_screen_get_default(); 102 gfx::Rect bounds = GetMonitorAreaNearestWindow(view);
149 return gfx::Size(gdk_screen_get_width(screen), gdk_screen_get_height(screen)); 103 // Do not use the _NET_WORKAREA here, this is supposed to be an area on a
104 // specific monitor, and _NET_WORKAREA is a hint from the WM that generally
105 // spans across all monitors. This would make the work area larger than the
106 // monitor.
107 // TODO(danakj) This is a work-around as there is no standard way to get this
108 // area, but it is a rect that we should be computing. The standard means
109 // to compute this rect would be to watch all windows with
110 // _NET_WM_STRUT(_PARTIAL) hints, and subtract their space from the physical
111 // area of the monitor to construct a work area.
112 // TODO(oshima): Implement ID and Observer.
113 return gfx::Monitor(0, 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 // TODO(oshima): Implement ID and Observer.
123 return gfx::Monitor(0, gfx::Rect(bounds));
124 }
125
126 // static
127 gfx::Monitor Screen::GetPrimaryMonitor() {
128 gfx::Rect bounds = NativePrimaryMonitorBounds();
129 // TODO(oshima): Implement ID and Observer.
130 gfx::Monitor monitor(0, bounds);
131 gfx::Rect rect;
132 if (GetScreenWorkArea(&rect)) {
133 monitor.set_work_area(rect.Intersect(bounds));
134 } else {
135 // Return the best we've got.
136 monitor.set_work_area(bounds);
137 }
138 return monitor;
139 }
140
141 // static
142 gfx::Monitor Screen::GetMonitorMatching(const gfx::Rect& match_rect) {
143 // TODO(thestig) Implement multi-monitor support.
144 return GetPrimaryMonitor();
145 }
146
147 // static
153 int Screen::GetNumMonitors() { 148 int Screen::GetNumMonitors() {
154 // This query is kinda bogus for Linux -- do we want number of X screens? 149 // 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. 150 // The number of monitors Xinerama has? We'll just use whatever GDK uses.
156 GdkScreen* screen = gdk_screen_get_default(); 151 GdkScreen* screen = gdk_screen_get_default();
157 return gdk_screen_get_n_monitors(screen); 152 return gdk_screen_get_n_monitors(screen);
158 } 153 }
159 154
160 } // namespace gfx 155 } // namespace gfx
OLDNEW
« no previous file with comments | « 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