• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CEF_TESTS_CEFCLIENT_BROWSER_ROOT_WINDOW_GTK_H_
6 #define CEF_TESTS_CEFCLIENT_BROWSER_ROOT_WINDOW_GTK_H_
7 #pragma once
8 
9 #include <gtk/gtk.h>
10 #include <memory>
11 #include <string>
12 
13 #include "tests/cefclient/browser/browser_window.h"
14 #include "tests/cefclient/browser/root_window.h"
15 
16 namespace client {
17 
18 // GTK implementation of a top-level native window in the browser process.
19 // The methods of this class must be called on the main thread unless otherwise
20 // indicated.
21 class RootWindowGtk : public RootWindow, public BrowserWindow::Delegate {
22  public:
23   // Constructor may be called on any thread.
24   RootWindowGtk();
25   ~RootWindowGtk();
26 
27   // RootWindow methods.
28   void Init(RootWindow::Delegate* delegate,
29             std::unique_ptr<RootWindowConfig> config,
30             const CefBrowserSettings& settings) override;
31   void InitAsPopup(RootWindow::Delegate* delegate,
32                    bool with_controls,
33                    bool with_osr,
34                    const CefPopupFeatures& popupFeatures,
35                    CefWindowInfo& windowInfo,
36                    CefRefPtr<CefClient>& client,
37                    CefBrowserSettings& settings) override;
38   void Show(ShowMode mode) override;
39   void Hide() override;
40   void SetBounds(int x, int y, size_t width, size_t height) override;
41   void Close(bool force) override;
42   void SetDeviceScaleFactor(float device_scale_factor) override;
43   float GetDeviceScaleFactor() const override;
44   CefRefPtr<CefBrowser> GetBrowser() const override;
45   ClientWindowHandle GetWindowHandle() const override;
46   bool WithWindowlessRendering() const override;
47   bool WithExtension() const override;
48 
49  private:
50   void CreateBrowserWindow(const std::string& startup_url);
51   void CreateRootWindow(const CefBrowserSettings& settings,
52                         bool initially_hidden);
53 
54   // BrowserWindow::Delegate methods.
55   void OnBrowserCreated(CefRefPtr<CefBrowser> browser) override;
56   void OnBrowserWindowClosing() override;
57   void OnBrowserWindowDestroyed() override;
58   void OnSetAddress(const std::string& url) override;
59   void OnSetTitle(const std::string& title) override;
60   void OnSetFullscreen(bool fullscreen) override;
61   void OnAutoResize(const CefSize& new_size) override;
62   void OnSetLoadingState(bool isLoading,
63                          bool canGoBack,
64                          bool canGoForward) override;
65   void OnSetDraggableRegions(
66       const std::vector<CefDraggableRegion>& regions) override;
67 
68   void NotifyMoveOrResizeStarted();
69   void NotifySetFocus();
70   void NotifyVisibilityChange(bool show);
71   void NotifyMenuBarHeight(int height);
72   void NotifyContentBounds(int x, int y, int width, int height);
73   void NotifyLoadURL(const std::string& url);
74   void NotifyButtonClicked(int id);
75   void NotifyMenuItem(int id);
76   void NotifyForceClose();
77   void NotifyCloseBrowser();
78   void NotifyDestroyedIfDone(bool window_destroyed, bool browser_destroyed);
79 
80   GtkWidget* CreateMenuBar();
81   GtkWidget* CreateMenu(GtkWidget* menu_bar, const char* text);
82   GtkWidget* AddMenuEntry(GtkWidget* menu_widget, const char* text, int id);
83 
84   // Signal handlers for the top-level GTK window.
85   static gboolean WindowFocusIn(GtkWidget* widget,
86                                 GdkEventFocus* event,
87                                 RootWindowGtk* self);
88   static gboolean WindowState(GtkWidget* widget,
89                               GdkEventWindowState* event,
90                               RootWindowGtk* self);
91   static gboolean WindowConfigure(GtkWindow* window,
92                                   GdkEvent* event,
93                                   RootWindowGtk* self);
94   static void WindowDestroy(GtkWidget* widget, RootWindowGtk* self);
95   static gboolean WindowDelete(GtkWidget* widget,
96                                GdkEvent* event,
97                                RootWindowGtk* self);
98 
99   // Signal handlers for the GTK Vbox containing all UX elements.
100   static void GridSizeAllocated(GtkWidget* widget,
101                                 GtkAllocation* allocation,
102                                 RootWindowGtk* self);
103 
104   // Signal handlers for the GTK menu bar.
105   static void MenubarSizeAllocated(GtkWidget* widget,
106                                    GtkAllocation* allocation,
107                                    RootWindowGtk* self);
108   static gboolean MenuItemActivated(GtkWidget* widget, RootWindowGtk* self);
109 
110   // Signal handlers for the GTK toolbar.
111   static void ToolbarSizeAllocated(GtkWidget* widget,
112                                    GtkAllocation* allocation,
113                                    RootWindowGtk* self);
114   static void BackButtonClicked(GtkButton* button, RootWindowGtk* self);
115   static void ForwardButtonClicked(GtkButton* button, RootWindowGtk* self);
116   static void StopButtonClicked(GtkButton* button, RootWindowGtk* self);
117   static void ReloadButtonClicked(GtkButton* button, RootWindowGtk* self);
118 
119   // Signal handlers for the GTK URL entry field.
120   static void URLEntryActivate(GtkEntry* entry, RootWindowGtk* self);
121   static gboolean URLEntryButtonPress(GtkWidget* widget,
122                                       GdkEventButton* event,
123                                       RootWindowGtk* self);
124 
125   // After initialization all members are only accessed on the main thread.
126   // Members set during initialization.
127   bool with_controls_;
128   bool always_on_top_;
129   bool with_osr_;
130   bool with_extension_;
131   bool is_popup_;
132   CefRect start_rect_;
133   std::unique_ptr<BrowserWindow> browser_window_;
134   bool initialized_;
135 
136   // Main window.
137   GtkWidget* window_;
138 
139   // Buttons.
140   GtkToolItem* back_button_;
141   GtkToolItem* forward_button_;
142   GtkToolItem* reload_button_;
143   GtkToolItem* stop_button_;
144 
145   // URL text field.
146   GtkWidget* url_entry_;
147 
148   // Height of UX controls that affect browser window placement.
149   int toolbar_height_;
150   int menubar_height_;
151 
152   CefRect browser_bounds_;
153 
154   bool window_destroyed_;
155   bool browser_destroyed_;
156 
157   // Members only accessed on the UI thread because they're needed for
158   // WindowDelete.
159   bool force_close_;
160   bool is_closing_;
161 
162   DISALLOW_COPY_AND_ASSIGN(RootWindowGtk);
163 };
164 
165 }  // namespace client
166 
167 #endif  // CEF_TESTS_CEFCLIENT_BROWSER_ROOT_WINDOW_GTK_H_
168