OLD | NEW |
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" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 screen = gtk_window_get_screen(window); | 64 screen = gtk_window_get_screen(window); |
65 monitor_num = gdk_screen_get_monitor_at_window( | 65 monitor_num = gdk_screen_get_monitor_at_window( |
66 screen, | 66 screen, |
67 gtk_widget_get_window(top_level)); | 67 gtk_widget_get_window(top_level)); |
68 } | 68 } |
69 GdkRectangle bounds; | 69 GdkRectangle bounds; |
70 gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds); | 70 gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds); |
71 return gfx::Rect(bounds); | 71 return gfx::Rect(bounds); |
72 } | 72 } |
73 | 73 |
| 74 class ScreenGtk : public gfx::Screen { |
| 75 public: |
| 76 ScreenGtk() { |
| 77 } |
| 78 |
| 79 virtual ~ScreenGtk() { |
| 80 } |
| 81 |
| 82 virtual bool IsDIPEnabled() OVERRIDE { |
| 83 return false; |
| 84 } |
| 85 |
| 86 virtual gfx::Point GetCursorScreenPoint() OVERRIDE { |
| 87 gint x, y; |
| 88 gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL); |
| 89 return gfx::Point(x, y); |
| 90 } |
| 91 |
| 92 // Returns the window under the cursor. |
| 93 virtual gfx::NativeWindow GetWindowAtCursorScreenPoint() OVERRIDE { |
| 94 GdkWindow* window = gdk_window_at_pointer(NULL, NULL); |
| 95 if (!window) |
| 96 return NULL; |
| 97 |
| 98 gpointer data = NULL; |
| 99 gdk_window_get_user_data(window, &data); |
| 100 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data); |
| 101 if (!widget) |
| 102 return NULL; |
| 103 widget = gtk_widget_get_toplevel(widget); |
| 104 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL; |
| 105 } |
| 106 |
| 107 // Returns the number of displays. |
| 108 // Mirrored displays are excluded; this method is intended to return the |
| 109 // number of distinct, usable displays. |
| 110 virtual int GetNumDisplays() OVERRIDE { |
| 111 // This query is kinda bogus for Linux -- do we want number of X screens? |
| 112 // The number of monitors Xinerama has? We'll just use whatever GDK uses. |
| 113 GdkScreen* screen = gdk_screen_get_default(); |
| 114 return gdk_screen_get_n_monitors(screen); |
| 115 } |
| 116 |
| 117 // Returns the display nearest the specified window. |
| 118 virtual gfx::Display GetDisplayNearestWindow( |
| 119 gfx::NativeView view) const OVERRIDE { |
| 120 gfx::Rect bounds = GetMonitorAreaNearestWindow(view); |
| 121 // Do not use the _NET_WORKAREA here, this is supposed to be an area on a |
| 122 // specific monitor, and _NET_WORKAREA is a hint from the WM that |
| 123 // generally spans across all monitors. This would make the work area |
| 124 // larger than the monitor. |
| 125 // TODO(danakj) This is a work-around as there is no standard way to get |
| 126 // this area, but it is a rect that we should be computing. The standard |
| 127 // means to compute this rect would be to watch all windows with |
| 128 // _NET_WM_STRUT(_PARTIAL) hints, and subtract their space from the |
| 129 // physical area of the display to construct a work area. |
| 130 // TODO(oshima): Implement ID and Observer. |
| 131 return gfx::Display(0, bounds); |
| 132 } |
| 133 |
| 134 // Returns the the display nearest the specified point. |
| 135 virtual gfx::Display GetDisplayNearestPoint( |
| 136 const gfx::Point& point) const OVERRIDE { |
| 137 GdkScreen* screen = gdk_screen_get_default(); |
| 138 gint monitor = gdk_screen_get_monitor_at_point( |
| 139 screen, point.x(), point.y()); |
| 140 GdkRectangle bounds; |
| 141 gdk_screen_get_monitor_geometry(screen, monitor, &bounds); |
| 142 // TODO(oshima): Implement ID and Observer. |
| 143 return gfx::Display(0, gfx::Rect(bounds)); |
| 144 } |
| 145 |
| 146 // Returns the display that most closely intersects the provided bounds. |
| 147 virtual gfx::Display GetDisplayMatching( |
| 148 const gfx::Rect& match_rect) const OVERRIDE { |
| 149 // TODO(thestig) Implement multi-monitor support. |
| 150 return GetPrimaryDisplay(); |
| 151 } |
| 152 |
| 153 // Returns the primary display. |
| 154 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE { |
| 155 gfx::Rect bounds = NativePrimaryMonitorBounds(); |
| 156 // TODO(oshima): Implement ID and Observer. |
| 157 gfx::Display display(0, bounds); |
| 158 gfx::Rect rect; |
| 159 if (GetScreenWorkArea(&rect)) { |
| 160 display.set_work_area(rect.Intersect(bounds)); |
| 161 } else { |
| 162 // Return the best we've got. |
| 163 display.set_work_area(bounds); |
| 164 } |
| 165 return display; |
| 166 } |
| 167 |
| 168 private: |
| 169 DISALLOW_COPY_AND_ASSIGN(ScreenGtk); |
| 170 }; |
| 171 |
74 } // namespace | 172 } // namespace |
75 | 173 |
76 namespace gfx { | 174 namespace gfx { |
77 | 175 |
78 // static | 176 Screen* CreateNativeScreen() { |
79 bool Screen::IsDIPEnabled() { | 177 return new ScreenGtk; |
80 return false; | |
81 } | |
82 | |
83 // static | |
84 gfx::Point Screen::GetCursorScreenPoint() { | |
85 gint x, y; | |
86 gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL); | |
87 return gfx::Point(x, y); | |
88 } | |
89 | |
90 // static | |
91 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { | |
92 GdkWindow* window = gdk_window_at_pointer(NULL, NULL); | |
93 if (!window) | |
94 return NULL; | |
95 | |
96 gpointer data = NULL; | |
97 gdk_window_get_user_data(window, &data); | |
98 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data); | |
99 if (!widget) | |
100 return NULL; | |
101 widget = gtk_widget_get_toplevel(widget); | |
102 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL; | |
103 } | |
104 | |
105 // static | |
106 gfx::Display Screen::GetDisplayNearestWindow(gfx::NativeView view) { | |
107 gfx::Rect bounds = GetMonitorAreaNearestWindow(view); | |
108 // Do not use the _NET_WORKAREA here, this is supposed to be an area on a | |
109 // specific monitor, and _NET_WORKAREA is a hint from the WM that generally | |
110 // spans across all monitors. This would make the work area larger than the | |
111 // monitor. | |
112 // TODO(danakj) This is a work-around as there is no standard way to get this | |
113 // area, but it is a rect that we should be computing. The standard means | |
114 // to compute this rect would be to watch all windows with | |
115 // _NET_WM_STRUT(_PARTIAL) hints, and subtract their space from the physical | |
116 // area of the display to construct a work area. | |
117 // TODO(oshima): Implement ID and Observer. | |
118 return gfx::Display(0, bounds); | |
119 } | |
120 | |
121 // static | |
122 gfx::Display Screen::GetDisplayNearestPoint(const gfx::Point& point) { | |
123 GdkScreen* screen = gdk_screen_get_default(); | |
124 gint monitor = gdk_screen_get_monitor_at_point(screen, point.x(), point.y()); | |
125 GdkRectangle bounds; | |
126 gdk_screen_get_monitor_geometry(screen, monitor, &bounds); | |
127 // TODO(oshima): Implement ID and Observer. | |
128 return gfx::Display(0, gfx::Rect(bounds)); | |
129 } | |
130 | |
131 // static | |
132 gfx::Display Screen::GetDisplayMatching(const gfx::Rect& match_rect) { | |
133 // TODO(thestig) Implement multi-monitor support. | |
134 return GetPrimaryDisplay(); | |
135 } | |
136 | |
137 // static | |
138 gfx::Display Screen::GetPrimaryDisplay() { | |
139 gfx::Rect bounds = NativePrimaryMonitorBounds(); | |
140 // TODO(oshima): Implement ID and Observer. | |
141 gfx::Display display(0, bounds); | |
142 gfx::Rect rect; | |
143 if (GetScreenWorkArea(&rect)) { | |
144 display.set_work_area(rect.Intersect(bounds)); | |
145 } else { | |
146 // Return the best we've got. | |
147 display.set_work_area(bounds); | |
148 } | |
149 return display; | |
150 } | |
151 | |
152 // static | |
153 int Screen::GetNumDisplays() { | |
154 // 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. | |
156 GdkScreen* screen = gdk_screen_get_default(); | |
157 return gdk_screen_get_n_monitors(screen); | |
158 } | 178 } |
159 | 179 |
160 } // namespace gfx | 180 } // namespace gfx |
OLD | NEW |