1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "tests/cefclient/browser/window_test_runner_gtk.h"
6
7 #include <gtk/gtk.h>
8
9 #include "include/wrapper/cef_helpers.h"
10 #include "tests/cefclient/browser/root_window.h"
11 #include "tests/cefclient/browser/util_gtk.h"
12 #include "tests/shared/browser/main_message_loop.h"
13
14 namespace client {
15 namespace window_test {
16
17 namespace {
18
GetWindow(CefRefPtr<CefBrowser> browser)19 GtkWindow* GetWindow(CefRefPtr<CefBrowser> browser) {
20 scoped_refptr<RootWindow> root_window =
21 RootWindow::GetForBrowser(browser->GetIdentifier());
22 if (root_window) {
23 GtkWindow* window = GTK_WINDOW(root_window->GetWindowHandle());
24 if (!window)
25 LOG(ERROR) << "No GtkWindow for browser";
26 return window;
27 }
28 return nullptr;
29 }
30
IsMaximized(GtkWindow * window)31 bool IsMaximized(GtkWindow* window) {
32 GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
33 gint state = gdk_window_get_state(gdk_window);
34 return (state & GDK_WINDOW_STATE_MAXIMIZED) ? true : false;
35 }
36
SetPosImpl(CefRefPtr<CefBrowser> browser,int x,int y,int width,int height)37 void SetPosImpl(CefRefPtr<CefBrowser> browser,
38 int x,
39 int y,
40 int width,
41 int height) {
42 REQUIRE_MAIN_THREAD();
43 ScopedGdkThreadsEnter scoped_gdk_threads;
44
45 GtkWindow* window = GetWindow(browser);
46 if (!window)
47 return;
48 GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
49
50 // Make sure the window isn't minimized or maximized.
51 if (IsMaximized(window))
52 gtk_window_unmaximize(window);
53 else
54 gtk_window_present(window);
55
56 // Retrieve information about the display that contains the window.
57 GdkScreen* screen = gdk_screen_get_default();
58 gint monitor = gdk_screen_get_monitor_at_window(screen, gdk_window);
59 GdkRectangle rect;
60 gdk_screen_get_monitor_geometry(screen, monitor, &rect);
61
62 // Make sure the window is inside the display.
63 CefRect display_rect(rect.x, rect.y, rect.width, rect.height);
64 CefRect window_rect(x, y, width, height);
65 WindowTestRunner::ModifyBounds(display_rect, window_rect);
66
67 gdk_window_move_resize(gdk_window, window_rect.x, window_rect.y,
68 window_rect.width, window_rect.height);
69 }
70
MinimizeImpl(CefRefPtr<CefBrowser> browser)71 void MinimizeImpl(CefRefPtr<CefBrowser> browser) {
72 REQUIRE_MAIN_THREAD();
73 ScopedGdkThreadsEnter scoped_gdk_threads;
74
75 GtkWindow* window = GetWindow(browser);
76 if (!window)
77 return;
78
79 // Unmaximize the window before minimizing so restore behaves correctly.
80 if (IsMaximized(window))
81 gtk_window_unmaximize(window);
82
83 gtk_window_iconify(window);
84 }
85
MaximizeImpl(CefRefPtr<CefBrowser> browser)86 void MaximizeImpl(CefRefPtr<CefBrowser> browser) {
87 REQUIRE_MAIN_THREAD();
88 ScopedGdkThreadsEnter scoped_gdk_threads;
89
90 GtkWindow* window = GetWindow(browser);
91 if (!window)
92 return;
93 gtk_window_maximize(window);
94 }
95
RestoreImpl(CefRefPtr<CefBrowser> browser)96 void RestoreImpl(CefRefPtr<CefBrowser> browser) {
97 REQUIRE_MAIN_THREAD();
98 ScopedGdkThreadsEnter scoped_gdk_threads;
99
100 GtkWindow* window = GetWindow(browser);
101 if (!window)
102 return;
103 if (IsMaximized(window))
104 gtk_window_unmaximize(window);
105 else
106 gtk_window_present(window);
107 }
108
109 } // namespace
110
WindowTestRunnerGtk()111 WindowTestRunnerGtk::WindowTestRunnerGtk() {}
112
SetPos(CefRefPtr<CefBrowser> browser,int x,int y,int width,int height)113 void WindowTestRunnerGtk::SetPos(CefRefPtr<CefBrowser> browser,
114 int x,
115 int y,
116 int width,
117 int height) {
118 MAIN_POST_CLOSURE(base::BindOnce(SetPosImpl, browser, x, y, width, height));
119 }
120
Minimize(CefRefPtr<CefBrowser> browser)121 void WindowTestRunnerGtk::Minimize(CefRefPtr<CefBrowser> browser) {
122 MAIN_POST_CLOSURE(base::BindOnce(MinimizeImpl, browser));
123 }
124
Maximize(CefRefPtr<CefBrowser> browser)125 void WindowTestRunnerGtk::Maximize(CefRefPtr<CefBrowser> browser) {
126 MAIN_POST_CLOSURE(base::BindOnce(MaximizeImpl, browser));
127 }
128
Restore(CefRefPtr<CefBrowser> browser)129 void WindowTestRunnerGtk::Restore(CefRefPtr<CefBrowser> browser) {
130 MAIN_POST_CLOSURE(base::BindOnce(RestoreImpl, browser));
131 }
132
133 } // namespace window_test
134 } // namespace client
135