| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/window_sizer.h" | 5 #include "chrome/browser/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/browser.h" | 10 #include "chrome/browser/browser.h" |
| 11 | 11 |
| 12 // How much horizontal and vertical offset there is between newly | 12 // How much horizontal and vertical offset there is between newly |
| 13 // opened windows. We don't care on Linux since the window manager generally | 13 // opened windows. We don't care on Linux since the window manager generally |
| 14 // does a good job of window placement. | 14 // does a good job of window placement. |
| 15 const int WindowSizer::kWindowTilePixels = 0; | 15 const int WindowSizer::kWindowTilePixels = 0; |
| 16 | 16 |
| 17 // An implementation of WindowSizer::MonitorInfoProvider that gets the actual | 17 // An implementation of WindowSizer::MonitorInfoProvider that gets the actual |
| 18 // monitor information from X via GDK. | 18 // monitor information from X via GDK. |
| 19 class DefaultMonitorInfoProvider : public WindowSizer::MonitorInfoProvider { | 19 class DefaultMonitorInfoProvider : public WindowSizer::MonitorInfoProvider { |
| 20 public: | 20 public: |
| 21 DefaultMonitorInfoProvider() { } | 21 DefaultMonitorInfoProvider() { } |
| 22 | 22 |
| 23 virtual gfx::Rect GetPrimaryMonitorWorkArea() const { | 23 virtual gfx::Rect GetPrimaryMonitorWorkArea() const { |
| 24 gfx::Rect rect = GetScreenWorkArea(); | 24 gfx::Rect rect; |
| 25 return rect.Intersect(GetPrimaryMonitorBounds()); | 25 if (GetScreenWorkArea(&rect)) |
| 26 return rect.Intersect(GetPrimaryMonitorBounds()); |
| 27 |
| 28 // Return the best we've got. |
| 29 return GetPrimaryMonitorBounds(); |
| 26 } | 30 } |
| 27 | 31 |
| 28 virtual gfx::Rect GetPrimaryMonitorBounds() const { | 32 virtual gfx::Rect GetPrimaryMonitorBounds() const { |
| 29 GdkScreen* screen = gdk_screen_get_default(); | 33 GdkScreen* screen = gdk_screen_get_default(); |
| 30 GdkRectangle rect; | 34 GdkRectangle rect; |
| 31 gdk_screen_get_monitor_geometry(screen, 0, &rect); | 35 gdk_screen_get_monitor_geometry(screen, 0, &rect); |
| 32 return gfx::Rect(rect); | 36 return gfx::Rect(rect); |
| 33 } | 37 } |
| 34 | 38 |
| 35 virtual gfx::Rect GetMonitorWorkAreaMatching( | 39 virtual gfx::Rect GetMonitorWorkAreaMatching( |
| 36 const gfx::Rect& match_rect) const { | 40 const gfx::Rect& match_rect) const { |
| 37 // TODO(thestig) Implement multi-monitor support. | 41 // TODO(thestig) Implement multi-monitor support. |
| 38 return GetPrimaryMonitorWorkArea(); | 42 return GetPrimaryMonitorWorkArea(); |
| 39 } | 43 } |
| 40 | 44 |
| 41 virtual gfx::Point GetBoundsOffsetMatching( | 45 virtual gfx::Point GetBoundsOffsetMatching( |
| 42 const gfx::Rect& match_rect) const { | 46 const gfx::Rect& match_rect) const { |
| 43 // TODO(thestig) Implement multi-monitor support. | 47 // TODO(thestig) Implement multi-monitor support. |
| 44 return GetPrimaryMonitorWorkArea().origin(); | 48 return GetPrimaryMonitorWorkArea().origin(); |
| 45 } | 49 } |
| 46 | 50 |
| 47 void UpdateWorkAreas() { | 51 void UpdateWorkAreas() { |
| 48 // TODO(thestig) Implement multi-monitor support. | 52 // TODO(thestig) Implement multi-monitor support. |
| 49 work_areas_.clear(); | 53 work_areas_.clear(); |
| 50 work_areas_.push_back(GetPrimaryMonitorBounds()); | 54 work_areas_.push_back(GetPrimaryMonitorBounds()); |
| 51 } | 55 } |
| 52 | 56 |
| 53 private: | 57 private: |
| 54 gfx::Rect GetScreenWorkArea() const { | 58 // Get the available screen space as a gfx::Rect, or return false if |
| 55 gboolean r; | 59 // if it's unavailable (i.e. the window manager doesn't support |
| 56 guchar* raw_data; | 60 // retrieving this). |
| 57 gint data_len; | 61 bool GetScreenWorkArea(gfx::Rect* out_rect) const { |
| 58 r = gdk_property_get(gdk_get_default_root_window(), // a gdk window | 62 gboolean ok; |
| 59 gdk_atom_intern("_NET_WORKAREA", FALSE), // property | 63 guchar* raw_data = NULL; |
| 60 gdk_atom_intern("CARDINAL", FALSE), // property type | 64 gint data_len = 0; |
| 61 0, // byte offset into property | 65 ok = gdk_property_get(gdk_get_default_root_window(), // a gdk window |
| 62 0xff, // property length to retrieve | 66 gdk_atom_intern("_NET_WORKAREA", FALSE), // property |
| 63 false, // delete property after retrieval? | 67 gdk_atom_intern("CARDINAL", FALSE), // property type |
| 64 NULL, // returned property type | 68 0, // byte offset into property |
| 65 NULL, // returned data format | 69 0xff, // property length to retrieve |
| 66 &data_len, // returned data len | 70 false, // delete property after retrieval? |
| 67 &raw_data); // returned data | 71 NULL, // returned property type |
| 68 CHECK(r); | 72 NULL, // returned data format |
| 69 CHECK(data_len >= 16); | 73 &data_len, // returned data len |
| 74 &raw_data); // returned data |
| 75 if (!ok) |
| 76 return false; |
| 77 |
| 78 // We expect to get four longs back: x1, y1, x2, y2. |
| 79 if (data_len != 4 * sizeof(glong)) { |
| 80 NOTREACHED(); |
| 81 g_free(raw_data); |
| 82 return false; |
| 83 } |
| 84 |
| 70 glong* data = reinterpret_cast<glong*>(raw_data); | 85 glong* data = reinterpret_cast<glong*>(raw_data); |
| 71 gint x = data[0]; | 86 gint x = data[0]; |
| 72 gint y = data[1]; | 87 gint y = data[1]; |
| 73 gint width = data[0] + data[2]; | 88 gint width = data[0] + data[2]; |
| 74 gint height = data[1] + data[3]; | 89 gint height = data[1] + data[3]; |
| 75 g_free(data); | 90 g_free(raw_data); |
| 76 return gfx::Rect(x, y, width, height); | 91 |
| 92 out_rect->SetRect(x, y, width, height); |
| 93 return true; |
| 77 } | 94 } |
| 78 | 95 |
| 79 DISALLOW_COPY_AND_ASSIGN(DefaultMonitorInfoProvider); | 96 DISALLOW_COPY_AND_ASSIGN(DefaultMonitorInfoProvider); |
| 80 }; | 97 }; |
| 81 | 98 |
| 82 // static | 99 // static |
| 83 WindowSizer::MonitorInfoProvider* | 100 WindowSizer::MonitorInfoProvider* |
| 84 WindowSizer::CreateDefaultMonitorInfoProvider() { | 101 WindowSizer::CreateDefaultMonitorInfoProvider() { |
| 85 return new DefaultMonitorInfoProvider(); | 102 return new DefaultMonitorInfoProvider(); |
| 86 } | 103 } |
| OLD | NEW |