1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_UI_GTK_APPS_NATIVE_APP_WINDOW_GTK_H_
6#define CHROME_BROWSER_UI_GTK_APPS_NATIVE_APP_WINDOW_GTK_H_
7
8#include <gtk/gtk.h>
9
10#include "apps/native_app_window.h"
11#include "apps/shell_window.h"
12#include "base/observer_list.h"
13#include "base/timer/timer.h"
14#include "chrome/browser/ui/gtk/extensions/extension_view_gtk.h"
15#include "third_party/skia/include/core/SkRegion.h"
16#include "ui/base/gtk/gtk_signal.h"
17#include "ui/base/x/active_window_watcher_x_observer.h"
18#include "ui/base/x/x11_atom_cache.h"
19#include "ui/gfx/rect.h"
20
21class ExtensionKeybindingRegistryGtk;
22class Profile;
23
24namespace extensions {
25class Extension;
26}
27
28class NativeAppWindowGtk : public apps::NativeAppWindow,
29                           public ExtensionViewGtk::Container,
30                           public ui::ActiveWindowWatcherXObserver {
31 public:
32  NativeAppWindowGtk(apps::ShellWindow* shell_window,
33                     const apps::ShellWindow::CreateParams& params);
34
35  // ui::BaseWindow implementation.
36  virtual bool IsActive() const OVERRIDE;
37  virtual bool IsMaximized() const OVERRIDE;
38  virtual bool IsMinimized() const OVERRIDE;
39  virtual bool IsFullscreen() const OVERRIDE;
40  virtual bool IsDetached() const OVERRIDE;
41  virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
42  virtual gfx::Rect GetRestoredBounds() const OVERRIDE;
43  virtual ui::WindowShowState GetRestoredState() const OVERRIDE;
44  virtual gfx::Rect GetBounds() const OVERRIDE;
45  virtual void Show() OVERRIDE;
46  virtual void ShowInactive() OVERRIDE;
47  virtual void Hide() OVERRIDE;
48  virtual void Close() OVERRIDE;
49  virtual void Activate() OVERRIDE;
50  virtual void Deactivate() OVERRIDE;
51  virtual void Maximize() OVERRIDE;
52  virtual void Minimize() OVERRIDE;
53  virtual void Restore() OVERRIDE;
54  virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
55  virtual void FlashFrame(bool flash) OVERRIDE;
56  virtual bool IsAlwaysOnTop() const OVERRIDE;
57
58  // ActiveWindowWatcherXObserver implementation.
59  virtual void ActiveWindowChanged(GdkWindow* active_window) OVERRIDE;
60
61 private:
62  // NativeAppWindow implementation.
63  virtual void SetFullscreen(bool fullscreen) OVERRIDE;
64  virtual bool IsFullscreenOrPending() const OVERRIDE;
65  virtual void UpdateWindowIcon() OVERRIDE;
66  virtual void UpdateWindowTitle() OVERRIDE;
67  virtual void HandleKeyboardEvent(
68      const content::NativeWebKeyboardEvent& event) OVERRIDE;
69  virtual void UpdateDraggableRegions(
70      const std::vector<extensions::DraggableRegion>& regions) OVERRIDE;
71  virtual void RenderViewHostChanged() OVERRIDE;
72  virtual gfx::Insets GetFrameInsets() const OVERRIDE;
73
74  // web_modal::WebContentsModalDialogHost implementation.
75  virtual gfx::NativeView GetHostView() const OVERRIDE;
76  virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
77  virtual void AddObserver(
78      web_modal::WebContentsModalDialogHostObserver* observer) OVERRIDE;
79  virtual void RemoveObserver(
80      web_modal::WebContentsModalDialogHostObserver* observer) OVERRIDE;
81
82  content::WebContents* web_contents() const {
83    return shell_window_->web_contents();
84  }
85  const extensions::Extension* extension() const {
86    return shell_window_->extension();
87  }
88
89  virtual ~NativeAppWindowGtk();
90
91  // If the point (|x|, |y|) is within the resize border area of the window,
92  // returns true and sets |edge| to the appropriate GdkWindowEdge value.
93  // Otherwise, returns false.
94  bool GetWindowEdge(int x, int y, GdkWindowEdge* edge);
95
96  CHROMEGTK_CALLBACK_1(NativeAppWindowGtk, gboolean, OnMainWindowDeleteEvent,
97                       GdkEvent*);
98  CHROMEGTK_CALLBACK_1(NativeAppWindowGtk, gboolean, OnConfigure,
99                       GdkEventConfigure*);
100  CHROMEGTK_CALLBACK_1(NativeAppWindowGtk, gboolean, OnWindowState,
101                       GdkEventWindowState*);
102  CHROMEGTK_CALLBACK_1(NativeAppWindowGtk, gboolean, OnMouseMoveEvent,
103                       GdkEventMotion*);
104  CHROMEGTK_CALLBACK_1(NativeAppWindowGtk, gboolean, OnButtonPress,
105                       GdkEventButton*);
106  // Callback for PropertyChange XEvents.
107  CHROMEG_CALLBACK_1(NativeAppWindowGtk, GdkFilterReturn,
108                     OnXEvent, GdkXEvent*, GdkEvent*);
109
110  void OnConfigureDebounced();
111
112  apps::ShellWindow* shell_window_;  // weak - ShellWindow owns NativeAppWindow.
113
114  GtkWindow* window_;
115  GdkWindowState state_;
116
117  // True if the window manager thinks the window is active.  Not all window
118  // managers keep track of this state (_NET_ACTIVE_WINDOW), in which case
119  // this will always be true.
120  bool is_active_;
121
122  // The position and size of the current window.
123  gfx::Rect bounds_;
124
125  // The position and size of the non-maximized, non-fullscreen window.
126  gfx::Rect restored_bounds_;
127
128  // True if the RVH is in fullscreen mode. The window may not actually be in
129  // fullscreen, however: some WMs don't support fullscreen.
130  bool content_thinks_its_fullscreen_;
131
132  // The region is treated as title bar, can be dragged to move
133  // and double clicked to maximize.
134  scoped_ptr<SkRegion> draggable_region_;
135
136  // If true, don't call gdk_window_raise() when we get a click in the title
137  // bar or window border.  This is to work around a compiz bug.
138  bool suppress_window_raise_;
139
140  // True if the window shows without frame.
141  bool frameless_;
142
143  // True if the window should be resizable by the user.
144  bool resizable_;
145
146  // The current window cursor.  We set it to a resize cursor when over the
147  // custom frame border.  We set it to NULL if we want the default cursor.
148  GdkCursor* frame_cursor_;
149
150  // The timer used to save the window position for session restore.
151  base::OneShotTimer<NativeAppWindowGtk> window_configure_debounce_timer_;
152
153  // The Extension Keybinding Registry responsible for registering listeners for
154  // accelerators that are sent to the window, that are destined to be turned
155  // into events and sent to the extension.
156  scoped_ptr<ExtensionKeybindingRegistryGtk> extension_keybinding_registry_;
157
158  // Observers to be notified when any web contents modal dialog requires
159  // updating its dimensions.
160  ObserverList<web_modal::WebContentsModalDialogHostObserver> observer_list_;
161
162  ui::X11AtomCache atom_cache_;
163
164  // True if we listen for the XEvent.
165  bool is_x_event_listened_;
166
167  DISALLOW_COPY_AND_ASSIGN(NativeAppWindowGtk);
168};
169
170#endif  // CHROME_BROWSER_UI_GTK_APPS_NATIVE_APP_WINDOW_GTK_H_
171