]> git.sesse.net Git - casparcg/blob - dependencies64/cef/linux/tests/cefclient/browser/browser_window_std_gtk.cc
251b9c7bdc463d5845aff086eff193566a716ff0
[casparcg] / dependencies64 / cef / linux / tests / cefclient / browser / browser_window_std_gtk.cc
1 // Copyright (c) 2015 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/browser_window_std_gtk.h"
6
7 #include <gtk/gtk.h>
8 #include <gdk/gdk.h>
9 #include <gdk/gdkx.h>
10
11 #include <X11/Xlib.h>
12 #undef Success     // Definition conflicts with cef_message_router.h
13 #undef RootWindow  // Definition conflicts with root_window.h
14
15 #include "include/base/cef_logging.h"
16 #include "tests/cefclient/browser/client_handler_std.h"
17 #include "tests/shared/browser/main_message_loop.h"
18
19 namespace client {
20
21 namespace {
22
23 ::Window GetXWindowForWidget(GtkWidget* widget) {
24   // The GTK window must be visible before we can retrieve the XID.
25   ::Window xwindow = GDK_WINDOW_XID(gtk_widget_get_window(widget));
26   DCHECK(xwindow);
27   return xwindow;
28 }
29
30 void SetXWindowVisible(::Window xwindow, bool visible) {
31   ::Display* xdisplay = cef_get_xdisplay();
32
33   // Retrieve the atoms required by the below XChangeProperty call.
34   const char* kAtoms[] = {
35     "_NET_WM_STATE",
36     "ATOM",
37     "_NET_WM_STATE_HIDDEN"
38   };
39   Atom atoms[3];
40   int result = XInternAtoms(xdisplay, const_cast<char**>(kAtoms), 3, false,
41                             atoms);
42   if (!result)
43     NOTREACHED();
44
45   if (!visible) {
46     // Set the hidden property state value.
47     scoped_ptr<Atom[]> data(new Atom[1]);
48     data[0] = atoms[2];
49
50     XChangeProperty(xdisplay,
51                     xwindow,
52                     atoms[0],  // name
53                     atoms[1],  // type
54                     32,  // size in bits of items in 'value'
55                     PropModeReplace,
56                     reinterpret_cast<const unsigned char*>(data.get()),
57                     1);  // num items
58   } else {
59     // Set an empty array of property state values.
60     XChangeProperty(xdisplay,
61                     xwindow,
62                     atoms[0],  // name
63                     atoms[1],  // type
64                     32,  // size in bits of items in 'value'
65                     PropModeReplace,
66                     NULL,
67                     0);  // num items
68   }
69 }
70
71 void SetXWindowBounds(::Window xwindow,
72                       int x, int y, size_t width, size_t height) {
73   ::Display* xdisplay = cef_get_xdisplay();
74   XWindowChanges changes = {0};
75   changes.x = x;
76   changes.y = y;
77   changes.width = static_cast<int>(width);
78   changes.height = static_cast<int>(height);
79   XConfigureWindow(xdisplay, xwindow,
80                    CWX | CWY | CWHeight | CWWidth, &changes);
81 }
82
83 }  // namespace
84
85 BrowserWindowStdGtk::BrowserWindowStdGtk(Delegate* delegate,
86                                          const std::string& startup_url)
87     : BrowserWindow(delegate) {
88   client_handler_ = new ClientHandlerStd(this, startup_url);
89 }
90
91 void BrowserWindowStdGtk::CreateBrowser(
92     ClientWindowHandle parent_handle,
93     const CefRect& rect,
94     const CefBrowserSettings& settings,
95     CefRefPtr<CefRequestContext> request_context) {
96   REQUIRE_MAIN_THREAD();
97
98   CefWindowInfo window_info;
99   window_info.SetAsChild(GetXWindowForWidget(parent_handle), rect);
100
101   CefBrowserHost::CreateBrowser(window_info, client_handler_,
102                                 client_handler_->startup_url(),
103                                 settings, request_context);
104 }
105
106 void BrowserWindowStdGtk::GetPopupConfig(CefWindowHandle temp_handle,
107                                          CefWindowInfo& windowInfo,
108                                          CefRefPtr<CefClient>& client,
109                                          CefBrowserSettings& settings) {
110   // Note: This method may be called on any thread.
111   // The window will be properly sized after the browser is created.
112   windowInfo.SetAsChild(temp_handle, CefRect());
113   client = client_handler_;
114 }
115
116 void BrowserWindowStdGtk::ShowPopup(ClientWindowHandle parent_handle,
117                                     int x, int y, size_t width, size_t height) {
118   REQUIRE_MAIN_THREAD();
119
120   if (browser_) {
121     ::Window parent_xwindow = GetXWindowForWidget(parent_handle);
122     ::Display* xdisplay = cef_get_xdisplay();
123     ::Window xwindow = browser_->GetHost()->GetWindowHandle();
124     DCHECK(xwindow);
125
126     XReparentWindow(xdisplay, xwindow, parent_xwindow, x, y);
127     
128     SetXWindowBounds(xwindow, x, y, width, height);
129     SetXWindowVisible(xwindow, true);
130   }
131 }
132
133 void BrowserWindowStdGtk::Show() {
134   REQUIRE_MAIN_THREAD();
135
136   if (browser_) {
137     ::Window xwindow = browser_->GetHost()->GetWindowHandle();
138     DCHECK(xwindow);
139     SetXWindowVisible(xwindow, true);
140   }
141 }
142
143 void BrowserWindowStdGtk::Hide() {
144   REQUIRE_MAIN_THREAD();
145
146   if (browser_) {
147     ::Window xwindow = browser_->GetHost()->GetWindowHandle();
148     DCHECK(xwindow);
149     SetXWindowVisible(xwindow, false);
150   }
151 }
152
153 void BrowserWindowStdGtk::SetBounds(int x, int y, size_t width, size_t height) {
154   REQUIRE_MAIN_THREAD();
155
156   if (browser_) {
157     ::Window xwindow = browser_->GetHost()->GetWindowHandle();
158     DCHECK(xwindow);
159     SetXWindowBounds(xwindow, x, y, width, height);
160   }
161 }
162
163 void BrowserWindowStdGtk::SetFocus(bool focus) {
164   REQUIRE_MAIN_THREAD();
165
166   if (browser_)
167     browser_->GetHost()->SetFocus(focus);
168 }
169
170 ClientWindowHandle BrowserWindowStdGtk::GetWindowHandle() const {
171   REQUIRE_MAIN_THREAD();
172
173   // There is no GtkWidget* representation of this object.
174   NOTREACHED();
175   return NULL;
176 }
177
178
179 }  // namespace client