| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/ui/window_sizer.h" | 5 #include "chrome/browser/ui/window_sizer.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_list.h" | 11 #include "chrome/browser/ui/browser_list.h" |
| 12 #include "chrome/browser/ui/browser_window.h" | 12 #include "chrome/browser/ui/browser_window.h" |
| 13 #include "ui/gfx/screen.h" |
| 13 | 14 |
| 14 // Used to pad the default new window size. On Windows, this is also used for | 15 // Used to pad the default new window size. On Windows, this is also used for |
| 15 // positioning new windows (each window is offset from the previous one). | 16 // positioning new windows (each window is offset from the previous one). |
| 16 // Since we don't position windows, it's only used for the default new window | 17 // Since we don't position windows, it's only used for the default new window |
| 17 // size. | 18 // size. |
| 18 const int WindowSizer::kWindowTilePixels = 10; | 19 const int WindowSizer::kWindowTilePixels = 10; |
| 19 | 20 |
| 20 // An implementation of WindowSizer::MonitorInfoProvider that gets the actual | |
| 21 // monitor information from X via GDK. | |
| 22 class DefaultMonitorInfoProvider : public WindowSizer::MonitorInfoProvider { | |
| 23 public: | |
| 24 DefaultMonitorInfoProvider() { } | |
| 25 | |
| 26 virtual gfx::Rect GetPrimaryMonitorWorkArea() const { | |
| 27 gfx::Rect rect; | |
| 28 if (GetScreenWorkArea(&rect)) | |
| 29 return rect.Intersect(GetPrimaryMonitorBounds()); | |
| 30 | |
| 31 // Return the best we've got. | |
| 32 return GetPrimaryMonitorBounds(); | |
| 33 } | |
| 34 | |
| 35 virtual gfx::Rect GetPrimaryMonitorBounds() const { | |
| 36 GdkScreen* screen = gdk_screen_get_default(); | |
| 37 GdkRectangle rect; | |
| 38 gdk_screen_get_monitor_geometry(screen, 0, &rect); | |
| 39 return gfx::Rect(rect); | |
| 40 } | |
| 41 | |
| 42 virtual gfx::Rect GetMonitorWorkAreaMatching( | |
| 43 const gfx::Rect& match_rect) const { | |
| 44 // TODO(thestig) Implement multi-monitor support. | |
| 45 return GetPrimaryMonitorWorkArea(); | |
| 46 } | |
| 47 | |
| 48 void UpdateWorkAreas() { | |
| 49 // TODO(thestig) Implement multi-monitor support. | |
| 50 work_areas_.clear(); | |
| 51 work_areas_.push_back(GetPrimaryMonitorBounds()); | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 // Get the available screen space as a gfx::Rect, or return false if | |
| 56 // if it's unavailable (i.e. the window manager doesn't support | |
| 57 // retrieving this). | |
| 58 // TODO(thestig) Use _NET_CURRENT_DESKTOP here as well? | |
| 59 bool GetScreenWorkArea(gfx::Rect* out_rect) const { | |
| 60 gboolean ok; | |
| 61 guchar* raw_data = NULL; | |
| 62 gint data_len = 0; | |
| 63 ok = gdk_property_get(gdk_get_default_root_window(), // a gdk window | |
| 64 gdk_atom_intern("_NET_WORKAREA", FALSE), // property | |
| 65 gdk_atom_intern("CARDINAL", FALSE), // property type | |
| 66 0, // byte offset into property | |
| 67 0xff, // property length to retrieve | |
| 68 false, // delete property after retrieval? | |
| 69 NULL, // returned property type | |
| 70 NULL, // returned data format | |
| 71 &data_len, // returned data len | |
| 72 &raw_data); // returned data | |
| 73 if (!ok) | |
| 74 return false; | |
| 75 | |
| 76 // We expect to get four longs back: x, y, width, height. | |
| 77 if (data_len < static_cast<gint>(4 * sizeof(glong))) { | |
| 78 NOTREACHED(); | |
| 79 g_free(raw_data); | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 glong* data = reinterpret_cast<glong*>(raw_data); | |
| 84 gint x = data[0]; | |
| 85 gint y = data[1]; | |
| 86 gint width = data[2]; | |
| 87 gint height = data[3]; | |
| 88 g_free(raw_data); | |
| 89 | |
| 90 out_rect->SetRect(x, y, width, height); | |
| 91 return true; | |
| 92 } | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(DefaultMonitorInfoProvider); | |
| 95 }; | |
| 96 | |
| 97 // static | |
| 98 WindowSizer::MonitorInfoProvider* | |
| 99 WindowSizer::CreateDefaultMonitorInfoProvider() { | |
| 100 return new DefaultMonitorInfoProvider(); | |
| 101 } | |
| 102 | |
| 103 // static | 21 // static |
| 104 gfx::Point WindowSizer::GetDefaultPopupOrigin(const gfx::Size& size) { | 22 gfx::Point WindowSizer::GetDefaultPopupOrigin(const gfx::Size& size) { |
| 105 scoped_ptr<MonitorInfoProvider> provider(CreateDefaultMonitorInfoProvider()); | 23 gfx::Rect monitor_bounds = gfx::Screen::GetPrimaryMonitorWorkArea(); |
| 106 gfx::Rect monitor_bounds = provider->GetPrimaryMonitorWorkArea(); | |
| 107 gfx::Point corner(monitor_bounds.x(), monitor_bounds.y()); | 24 gfx::Point corner(monitor_bounds.x(), monitor_bounds.y()); |
| 108 if (Browser* browser = BrowserList::GetLastActive()) { | 25 if (Browser* browser = BrowserList::GetLastActive()) { |
| 109 GtkWindow* window = | 26 GtkWindow* window = |
| 110 reinterpret_cast<GtkWindow*>(browser->window()->GetNativeHandle()); | 27 reinterpret_cast<GtkWindow*>(browser->window()->GetNativeHandle()); |
| 111 int x = 0, y = 0; | 28 int x = 0, y = 0; |
| 112 gtk_window_get_position(window, &x, &y); | 29 gtk_window_get_position(window, &x, &y); |
| 113 // Limit to not overflow the work area right and bottom edges. | 30 // Limit to not overflow the work area right and bottom edges. |
| 114 gfx::Point limit( | 31 gfx::Point limit( |
| 115 std::min(x + kWindowTilePixels, monitor_bounds.right() - size.width()), | 32 std::min(x + kWindowTilePixels, monitor_bounds.right() - size.width()), |
| 116 std::min(y + kWindowTilePixels, | 33 std::min(y + kWindowTilePixels, |
| 117 monitor_bounds.bottom() - size.height())); | 34 monitor_bounds.bottom() - size.height())); |
| 118 // Adjust corner to now overflow the work area left and top edges, so | 35 // Adjust corner to now overflow the work area left and top edges, so |
| 119 // that if a popup does not fit the title-bar is remains visible. | 36 // that if a popup does not fit the title-bar is remains visible. |
| 120 corner = gfx::Point( | 37 corner = gfx::Point( |
| 121 std::max(corner.x(), limit.x()), | 38 std::max(corner.x(), limit.x()), |
| 122 std::max(corner.y(), limit.y())); | 39 std::max(corner.y(), limit.y())); |
| 123 } | 40 } |
| 124 return corner; | 41 return corner; |
| 125 } | 42 } |
| OLD | NEW |