This source file includes following definitions.
- GetScreenWorkArea
- GetDisplayForMonitorNum
- GetMonitorAreaNearestWindow
- IsDIPEnabled
- GetCursorScreenPoint
- GetWindowUnderCursor
- GetWindowAtScreenPoint
- GetNumDisplays
- GetAllDisplays
- GetDisplayNearestWindow
- GetDisplayNearestPoint
- GetDisplayMatching
- GetPrimaryDisplay
- AddObserver
- RemoveObserver
- CreateNativeScreen
#include "ui/gfx/screen.h"
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include "base/logging.h"
#include "ui/gfx/display.h"
namespace {
bool GetScreenWorkArea(gfx::Rect* out_rect) {
gboolean ok;
guchar* raw_data = NULL;
gint data_len = 0;
ok = gdk_property_get(gdk_get_default_root_window(),
gdk_atom_intern("_NET_WORKAREA", FALSE),
gdk_atom_intern("CARDINAL", FALSE),
0,
0xff,
false,
NULL,
NULL,
&data_len,
&raw_data);
if (!ok)
return false;
if (data_len < static_cast<gint>(4 * sizeof(glong))) {
NOTREACHED();
g_free(raw_data);
return false;
}
glong* data = reinterpret_cast<glong*>(raw_data);
gint x = data[0];
gint y = data[1];
gint width = data[2];
gint height = data[3];
g_free(raw_data);
out_rect->SetRect(x, y, width, height);
return true;
}
gfx::Display GetDisplayForMonitorNum(GdkScreen* screen, gint monitor_num) {
GdkRectangle bounds;
gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds);
gfx::Display display(monitor_num, gfx::Rect(bounds));
if (gdk_screen_get_primary_monitor(screen) == monitor_num) {
gfx::Rect rect;
if (GetScreenWorkArea(&rect))
display.set_work_area(gfx::IntersectRects(rect, display.bounds()));
}
return display;
}
gfx::Display GetMonitorAreaNearestWindow(gfx::NativeView view) {
GdkScreen* screen = gdk_screen_get_default();
gint monitor_num = 0;
if (view && GTK_IS_WINDOW(view)) {
GtkWidget* top_level = gtk_widget_get_toplevel(view);
DCHECK(GTK_IS_WINDOW(top_level));
GtkWindow* window = GTK_WINDOW(top_level);
screen = gtk_window_get_screen(window);
monitor_num = gdk_screen_get_monitor_at_window(
screen,
gtk_widget_get_window(top_level));
}
return GetDisplayForMonitorNum(screen, monitor_num);
}
class ScreenGtk : public gfx::Screen {
public:
ScreenGtk() {
}
virtual ~ScreenGtk() {
}
virtual bool IsDIPEnabled() OVERRIDE {
return false;
}
virtual gfx::Point GetCursorScreenPoint() OVERRIDE {
gint x, y;
gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL);
return gfx::Point(x, y);
}
virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
GdkWindow* window = gdk_window_at_pointer(NULL, NULL);
if (!window)
return NULL;
gpointer data = NULL;
gdk_window_get_user_data(window, &data);
GtkWidget* widget = reinterpret_cast<GtkWidget*>(data);
if (!widget)
return NULL;
widget = gtk_widget_get_toplevel(widget);
return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL;
}
virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point)
OVERRIDE {
NOTIMPLEMENTED();
return NULL;
}
virtual int GetNumDisplays() const OVERRIDE {
GdkScreen* screen = gdk_screen_get_default();
return gdk_screen_get_n_monitors(screen);
}
virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
GdkScreen* screen = gdk_screen_get_default();
gint num_of_displays = gdk_screen_get_n_monitors(screen);
std::vector<gfx::Display> all_displays;
for (gint i = 0; i < num_of_displays; ++i)
all_displays.push_back(GetDisplayForMonitorNum(screen, i));
return all_displays;
}
virtual gfx::Display GetDisplayNearestWindow(
gfx::NativeView view) const OVERRIDE {
return GetMonitorAreaNearestWindow(view);
}
virtual gfx::Display GetDisplayNearestPoint(
const gfx::Point& point) const OVERRIDE {
GdkScreen* screen = gdk_screen_get_default();
gint monitor = gdk_screen_get_monitor_at_point(
screen, point.x(), point.y());
return GetDisplayForMonitorNum(screen, monitor);
}
virtual gfx::Display GetDisplayMatching(
const gfx::Rect& match_rect) const OVERRIDE {
std::vector<gfx::Display> displays = GetAllDisplays();
gfx::Display maxIntersectDisplay;
gfx::Rect maxIntersection;
for (std::vector<gfx::Display>::iterator it = displays.begin();
it != displays.end(); ++it) {
gfx::Rect displayIntersection = it->bounds();
displayIntersection.Intersect(match_rect);
if (displayIntersection.size().GetArea() >
maxIntersection.size().GetArea()) {
maxIntersectDisplay = *it;
maxIntersection = displayIntersection;
}
}
return maxIntersectDisplay.is_valid() ?
maxIntersectDisplay : GetPrimaryDisplay();
}
virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
GdkScreen* screen = gdk_screen_get_default();
gint primary_monitor_index = gdk_screen_get_primary_monitor(screen);
return GetDisplayForMonitorNum(screen, primary_monitor_index);
}
virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {
}
virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {
}
private:
DISALLOW_COPY_AND_ASSIGN(ScreenGtk);
};
}
namespace gfx {
Screen* CreateNativeScreen() {
return new ScreenGtk;
}
}