1 // Copyright 2012 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.
5 #ifndef CHROME_BROWSER_UI_GTK_BROWSER_WINDOW_GTK_H_
6 #define CHROME_BROWSER_UI_GTK_BROWSER_WINDOW_GTK_H_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/prefs/pref_member.h"
16 #include "base/timer/timer.h"
17 #include "build/build_config.h"
18 #include "chrome/browser/devtools/devtools_window.h"
19 #include "chrome/browser/extensions/extension_keybinding_registry.h"
20 #include "chrome/browser/infobars/infobar_container.h"
21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/ui/host_desktop.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
24 #include "ui/base/gtk/gtk_floating_container.h"
25 #include "ui/base/gtk/gtk_signal.h"
26 #include "ui/base/ui_base_types.h"
27 #include "ui/base/x/active_window_watcher_x_observer.h"
28 #include "ui/base/x/x11_util.h"
29 #include "ui/gfx/rect.h"
33 class BrowserTitlebar;
34 class BrowserToolbarGtk;
36 class DownloadShelfGtk;
37 class ExtensionKeybindingRegistryGtk;
39 class FullscreenExitBubbleGtk;
41 class InfoBarContainerGtk;
42 class InstantOverlayControllerGtk;
44 class StatusBubbleGtk;
45 class TabContentsContainerGtk;
49 class PasswordGenerator;
52 namespace extensions {
53 class ActiveTabPermissionGranter;
57 namespace user_prefs {
58 class PrefRegistrySyncable;
61 // An implementation of BrowserWindow for GTK. Cross-platform code will interact
62 // with this object when it needs to manipulate the window.
63 class BrowserWindowGtk
64 : public BrowserWindow,
65 public content::NotificationObserver,
66 public TabStripModelObserver,
67 public ui::ActiveWindowWatcherXObserver,
68 public InfoBarContainer::Delegate,
69 public extensions::ExtensionKeybindingRegistry::Delegate {
71 explicit BrowserWindowGtk(Browser* browser);
72 virtual ~BrowserWindowGtk();
74 // Separating initialization from constructor.
77 // Overridden from BrowserWindow:
78 virtual void Show() OVERRIDE;
79 virtual void ShowInactive() OVERRIDE;
80 virtual void Hide() OVERRIDE;
81 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
82 virtual void Close() OVERRIDE;
83 virtual void Activate() OVERRIDE;
84 virtual void Deactivate() OVERRIDE;
85 virtual bool IsActive() const OVERRIDE;
86 virtual void FlashFrame(bool flash) OVERRIDE;
87 virtual bool IsAlwaysOnTop() const OVERRIDE;
88 virtual void SetAlwaysOnTop(bool always_on_top) OVERRIDE;
89 virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
90 virtual BrowserWindowTesting* GetBrowserWindowTesting() OVERRIDE;
91 virtual StatusBubble* GetStatusBubble() OVERRIDE;
92 virtual void UpdateTitleBar() OVERRIDE;
93 virtual void BookmarkBarStateChanged(
94 BookmarkBar::AnimateChangeType change_type) OVERRIDE;
95 virtual void UpdateDevTools() OVERRIDE;
96 virtual void UpdateLoadingAnimations(bool should_animate) OVERRIDE;
97 virtual void SetStarredState(bool is_starred) OVERRIDE;
98 virtual void SetTranslateIconToggled(bool is_lit) OVERRIDE;
99 virtual void OnActiveTabChanged(content::WebContents* old_contents,
100 content::WebContents* new_contents,
102 int reason) OVERRIDE;
103 virtual void ZoomChangedForActiveTab(bool can_show_bubble) OVERRIDE;
104 virtual gfx::Rect GetRestoredBounds() const OVERRIDE;
105 virtual ui::WindowShowState GetRestoredState() const OVERRIDE;
106 virtual gfx::Rect GetBounds() const OVERRIDE;
107 virtual bool IsMaximized() const OVERRIDE;
108 virtual bool IsMinimized() const OVERRIDE;
109 virtual void Maximize() OVERRIDE;
110 virtual void Minimize() OVERRIDE;
111 virtual void Restore() OVERRIDE;
112 virtual void EnterFullscreen(
113 const GURL& url, FullscreenExitBubbleType type) OVERRIDE;
114 virtual void ExitFullscreen() OVERRIDE;
115 virtual void UpdateFullscreenExitBubbleContent(
117 FullscreenExitBubbleType bubble_type) OVERRIDE;
118 virtual bool ShouldHideUIForFullscreen() const OVERRIDE;
119 virtual bool IsFullscreen() const OVERRIDE;
120 virtual bool IsFullscreenBubbleVisible() const OVERRIDE;
121 virtual LocationBar* GetLocationBar() const OVERRIDE;
122 virtual void SetFocusToLocationBar(bool select_all) OVERRIDE;
123 virtual void UpdateReloadStopState(bool is_loading, bool force) OVERRIDE;
124 virtual void UpdateToolbar(content::WebContents* contents) OVERRIDE;
125 virtual void FocusToolbar() OVERRIDE;
126 virtual void FocusAppMenu() OVERRIDE;
127 virtual void FocusBookmarksToolbar() OVERRIDE;
128 virtual void FocusInfobars() OVERRIDE;
129 virtual void RotatePaneFocus(bool forwards) OVERRIDE;
130 virtual bool IsBookmarkBarVisible() const OVERRIDE;
131 virtual bool IsBookmarkBarAnimating() const OVERRIDE;
132 virtual bool IsTabStripEditable() const OVERRIDE;
133 virtual bool IsToolbarVisible() const OVERRIDE;
134 virtual gfx::Rect GetRootWindowResizerRect() const OVERRIDE;
135 virtual void ConfirmAddSearchProvider(TemplateURL* template_url,
136 Profile* profile) OVERRIDE;
137 virtual void ShowUpdateChromeDialog() OVERRIDE;
138 virtual void ShowBookmarkBubble(const GURL& url,
139 bool already_bookmarked) OVERRIDE;
140 virtual void ShowBookmarkAppBubble(
141 const WebApplicationInfo& web_app_info,
142 const std::string& extension_id) OVERRIDE;
143 virtual void ShowTranslateBubble(content::WebContents* contents,
144 TranslateTabHelper::TranslateStep step,
145 TranslateErrors::Type error_type) OVERRIDE;
146 #if defined(ENABLE_ONE_CLICK_SIGNIN)
147 virtual void ShowOneClickSigninBubble(
148 OneClickSigninBubbleType type,
149 const base::string16& email,
150 const base::string16& error_message,
151 const StartSyncCallback& start_sync_callback) OVERRIDE;
153 virtual bool IsDownloadShelfVisible() const OVERRIDE;
154 virtual DownloadShelf* GetDownloadShelf() OVERRIDE;
155 virtual void ConfirmBrowserCloseWithPendingDownloads(
157 Browser::DownloadClosePreventionType dialog_type,
159 const base::Callback<void(bool)>& callback) OVERRIDE;
160 virtual void UserChangedTheme() OVERRIDE;
161 virtual int GetExtraRenderViewHeight() const OVERRIDE;
162 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE;
163 virtual void ShowWebsiteSettings(Profile* profile,
164 content::WebContents* web_contents,
166 const content::SSLStatus& ssl) OVERRIDE;
167 virtual void ShowAppMenu() OVERRIDE;
168 virtual bool PreHandleKeyboardEvent(
169 const content::NativeWebKeyboardEvent& event,
170 bool* is_keyboard_shortcut) OVERRIDE;
171 virtual void HandleKeyboardEvent(
172 const content::NativeWebKeyboardEvent& event) OVERRIDE;
173 virtual void Cut() OVERRIDE;
174 virtual void Copy() OVERRIDE;
175 virtual void Paste() OVERRIDE;
176 virtual WindowOpenDisposition GetDispositionForPopupBounds(
177 const gfx::Rect& bounds) OVERRIDE;
178 virtual FindBar* CreateFindBar() OVERRIDE;
179 virtual web_modal::WebContentsModalDialogHost*
180 GetWebContentsModalDialogHost() OVERRIDE;
181 virtual void ShowAvatarBubble(content::WebContents* web_contents,
182 const gfx::Rect& rect) OVERRIDE;
183 virtual void ShowAvatarBubbleFromAvatarButton() OVERRIDE;
184 virtual void ShowPasswordGenerationBubble(
185 const gfx::Rect& rect,
186 const autofill::PasswordForm& form,
187 autofill::PasswordGenerator* password_generator) OVERRIDE;
188 virtual int GetRenderViewHeightInsetWithDetachedBookmarkBar() OVERRIDE;
190 // Overridden from NotificationObserver:
191 virtual void Observe(int type,
192 const content::NotificationSource& source,
193 const content::NotificationDetails& details) OVERRIDE;
195 // Overridden from TabStripModelObserver:
196 virtual void TabDetachedAt(content::WebContents* contents,
199 // Overridden from ActiveWindowWatcherXObserver.
200 virtual void ActiveWindowChanged(GdkWindow* active_window) OVERRIDE;
202 // Overridden from InfoBarContainer::Delegate:
203 virtual SkColor GetInfoBarSeparatorColor() const OVERRIDE;
204 virtual void InfoBarContainerStateChanged(bool is_animating) OVERRIDE;
205 virtual bool DrawInfoBarArrows(int* x) const OVERRIDE;
207 // Overridden from ExtensionKeybindingRegistry::Delegate:
208 virtual extensions::ActiveTabPermissionGranter*
209 GetActiveTabPermissionGranter() OVERRIDE;
211 // Accessor for the tab strip.
212 TabStripGtk* tabstrip() const { return tabstrip_.get(); }
214 void OnDebouncedBoundsChanged();
216 // Request the underlying window to unmaximize.
219 // Returns false if we're not ready to close yet. E.g., a tab may have an
220 // onbeforeunload handler that prevents us from closing.
221 bool CanClose() const;
223 // Returns whether to draw the content drop shadow on the sides and bottom
224 // of the browser window. When false, we still draw a shadow on the top of
225 // the toolbar (under the tab strip), but do not round the top corners.
226 bool ShouldDrawContentDropShadow() const;
228 bool ShouldShowWindowIcon() const;
230 // Add the find bar widget to the window hierarchy.
231 void AddFindBar(FindBarGtk* findbar);
233 // Reset the mouse cursor to the default cursor if it was set to something
234 // else for the custom frame.
235 void ResetCustomFrameCursor();
237 // Returns the BrowserWindowGtk registered with |window|.
238 static BrowserWindowGtk* GetBrowserWindowForNativeWindow(
239 gfx::NativeWindow window);
241 // Retrieves the GtkWindow associated with |xid|, which is the X Window
242 // ID of the top-level X window of this object.
243 static GtkWindow* GetBrowserWindowForXID(XID xid);
245 Browser* browser() const { return browser_.get(); }
247 GtkWindow* window() const { return window_; }
249 BrowserTitlebar* titlebar() const { return titlebar_.get(); }
251 GtkWidget* titlebar_widget() const;
253 BrowserToolbarGtk* GetToolbar() { return toolbar_.get(); }
255 gfx::Rect bounds() const { return bounds_; }
257 // Returns the tab we're currently displaying in the tab contents container.
258 content::WebContents* GetDisplayedTab();
260 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
262 // Tells GTK that the toolbar area is invalidated and needs redrawing. We
263 // have this method as a hack because GTK doesn't queue the toolbar area for
264 // redraw when it should.
265 void QueueToolbarRedraw();
267 // Get the position where the infobar arrow should be anchored in
268 // |relative_to| coordinates. This is the middle of the omnibox location icon.
269 int GetXPositionOfLocationIcon(GtkWidget* relative_to);
271 // Show or hide the bookmark bar.
272 void MaybeShowBookmarkBar(bool animate);
275 virtual void DestroyBrowser() OVERRIDE;
277 // Checks to see if the mouse pointer at |x|, |y| is over the border of the
278 // custom frame (a spot that should trigger a window resize). Returns true if
279 // it should and sets |edge|.
280 bool GetWindowEdge(int x, int y, GdkWindowEdge* edge);
282 // Returns the window shape for the window with |width| and |height|.
283 // The caller is responsible for destroying the region if non-null region is
285 GdkRegion* GetWindowShape(int width, int height) const;
287 // Save the window position in the prefs.
288 void SaveWindowPosition();
290 // Sets the default size for the window and the way the user is allowed to
292 void SetGeometryHints();
294 // Returns |true| if we should use the custom frame.
295 bool UseCustomFrame() const;
297 // Invalidate window to force repaint.
298 void InvalidateWindow();
300 // Top level window. NULL after the window starts closing.
302 // Determines whether window was shown.
303 bool window_has_shown_;
304 // GtkAlignment that holds the interior components of the chromium window.
305 // This is used to draw the custom frame border and content shadow. Owned by
307 GtkWidget* window_container_;
308 // VBox that holds everything (tabs, toolbar, bookmarks bar, tab contents).
309 // Owned by window_container_.
310 GtkWidget* window_vbox_;
311 // VBox that holds everything below the toolbar. Owned by
312 // render_area_floating_container_.
313 GtkWidget* render_area_vbox_;
314 // Floating container that holds the render area. It is needed to position
315 // the findbar. Owned by render_area_event_box_.
316 GtkWidget* render_area_floating_container_;
317 // EventBox that holds render_area_floating_container_. Owned by window_vbox_.
318 GtkWidget* render_area_event_box_;
319 // Border between toolbar and render area. Owned by render_area_vbox_.
320 GtkWidget* toolbar_border_;
322 scoped_ptr<Browser> browser_;
325 // Connect to signals on |window_|.
326 void ConnectHandlersToSignals();
328 // Create the various UI components.
331 // Set up background color of the window (depends on if we're incognito or
333 void SetBackgroundColor();
335 // Applies the window shape to if we're in custom drawing mode.
336 void UpdateWindowShape(int width, int height);
338 // Connect accelerators that aren't connected to menu items (like ctrl-o,
340 void ConnectAccelerators();
342 // Whether we should draw the tab background instead of the theme_frame
343 // background because this window is a popup.
344 bool UsingCustomPopupFrame() const;
346 // Draws the normal custom frame using theme_frame.
347 void DrawCustomFrame(cairo_t* cr, GtkWidget* widget, GdkEventExpose* event);
349 // Draws the tab image as the frame so we can write legible text.
350 void DrawPopupFrame(cairo_t* cr, GtkWidget* widget, GdkEventExpose* event);
352 // Draws the border, including resizable corners, for the custom frame.
353 void DrawCustomFrameBorder(GtkWidget* widget);
355 // Change whether we're showing the custom blue frame.
356 // Must be called once at startup.
357 // Triggers relayout of the content.
358 void UpdateCustomFrame();
360 // Set the bounds of the current window. If |exterior| is true, set the size
361 // of the window itself, otherwise set the bounds of the web contents.
362 // If |move| is true, set the position of the window, otherwise leave the
363 // position to the WM.
364 void SetBoundsImpl(const gfx::Rect& bounds, bool exterior, bool move);
366 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnConfigure,
368 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnWindowState,
369 GdkEventWindowState*);
370 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnMainWindowDeleteEvent,
372 CHROMEGTK_CALLBACK_0(BrowserWindowGtk, void, OnMainWindowDestroy);
373 // Callback for when the custom frame alignment needs to be redrawn.
374 // The content area includes the toolbar and web page but not the tab strip.
375 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnCustomFrameExpose,
378 // A helper method that draws the shadow above the toolbar and in the frame
379 // border during an expose.
380 void DrawContentShadow(cairo_t* cr);
382 // The background frame image needs to be offset by the size of the top of
383 // the window to the top of the tabs when the full skyline isn't displayed
385 int GetVerticalOffset();
387 // Returns which frame image we should use based on the window's current
388 // activation state / incognito state.
389 int GetThemeFrameResource();
391 // Invalidate all the widgets that need to redraw when the infobar draw state
393 void InvalidateInfoBarBits();
395 // When the location icon moves, we have to redraw the arrow.
396 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, void, OnLocationIconSizeAllocate,
399 // Used to draw the infobar arrow and drop shadow. This is connected to
400 // multiple widgets' expose events because it overlaps several widgets.
401 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnExposeDrawInfobarBits,
404 // Used to draw the infobar bits for the bookmark bar. When the bookmark
405 // bar is in floating mode, it has to draw a drop shadow only; otherwise
406 // it is responsible for its portion of the arrow as well as some shadowing.
407 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnBookmarkBarExpose,
410 // Callback for "size-allocate" signal on bookmark bar; this is relevant
411 // because when the bookmark bar changes dimensions, the infobar arrow has to
412 // change its shape, and we need to queue appropriate redraws.
413 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, void, OnBookmarkBarSizeAllocate,
416 // Callback for accelerator activation. |user_data| stores the command id
417 // of the matched accelerator.
418 static gboolean OnGtkAccelerator(GtkAccelGroup* accel_group,
419 GObject* acceleratable,
421 GdkModifierType modifier,
424 // Key press event callback.
425 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnKeyPress, GdkEventKey*);
427 // Mouse move and mouse button press callbacks.
428 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnMouseMoveEvent,
430 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnButtonPressEvent,
433 // Tracks focus state of browser.
434 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnFocusIn,
436 CHROMEGTK_CALLBACK_1(BrowserWindowGtk, gboolean, OnFocusOut,
439 // Callback for the loading animation(s) associated with this window.
440 void LoadingAnimationCallback();
442 // Shows UI elements for supported window features.
443 void ShowSupportedWindowFeatures();
445 // Hides UI elements for unsupported window features.
446 void HideUnsupportedWindowFeatures();
448 // Helper functions that query |browser_| concerning support for UI features
449 // in this window. (For example, a popup window might not support a tabstrip).
450 bool IsTabStripSupported() const;
451 bool IsToolbarSupported() const;
452 bool IsBookmarkBarSupported() const;
454 // Put the bookmark bar where it belongs.
455 void PlaceBookmarkBar(bool is_floating);
457 // Decides if we should draw the frame as if the window is active.
458 bool DrawFrameAsActive() const;
460 // Updates devtools window for given contents. This method will show docked
461 // devtools window for inspected |contents| that has docked devtools
462 // and hide it for NULL or not inspected |contents|. It will also make
463 // sure devtools window size and position are restored for given tab.
464 void UpdateDevToolsForContents(content::WebContents* contents);
466 // Shows docked devtools.
467 void ShowDevToolsContainer();
469 // Hides docked devtools.
470 void HideDevToolsContainer();
472 // Called when the preference changes.
473 void OnUseCustomChromeFrameChanged();
475 // Determine whether we use should default to native decorations or the custom
476 // frame based on the currently-running window manager.
477 static bool GetCustomFramePrefDefault();
479 // Handler for |devtools_floating_container_|'s "set-floating-position"
481 static void OnDevToolsContainerSetFloatingPosition(
482 GtkFloatingContainer* container, GtkAllocation* allocation,
483 BrowserWindowGtk* browser_window);
485 // The position and size of the current window.
488 // The configure bounds of the current window, used to figure out whether to
489 // ignore later configure events. See OnConfigure() for more information.
490 gfx::Rect configure_bounds_;
492 // The position and size of the non-maximized, non-fullscreen window.
493 gfx::Rect restored_bounds_;
495 GdkWindowState state_;
497 // Controls a hidden GtkMenuBar that we keep updated so GNOME can take a look
498 // inside "our menu bar" and present it in the top panel, akin to Mac OS.
499 scoped_ptr<GlobalMenuBar> global_menu_bar_;
501 // The container for the titlebar + tab strip.
502 scoped_ptr<BrowserTitlebar> titlebar_;
504 // The object that manages all of the widgets in the toolbar.
505 scoped_ptr<BrowserToolbarGtk> toolbar_;
507 // The object that manages the bookmark bar. This will be NULL if the
508 // bookmark bar is not supported.
509 scoped_ptr<BookmarkBarGtk> bookmark_bar_;
511 // The download shelf view (view at the bottom of the page).
512 scoped_ptr<DownloadShelfGtk> download_shelf_;
514 // The status bubble manager. Always non-NULL.
515 scoped_ptr<StatusBubbleGtk> status_bubble_;
517 // A container that manages the GtkWidget*s that are the webpage display
518 // (along with associated infobars, shelves, and other things that are part
519 // of the content area).
520 scoped_ptr<TabContentsContainerGtk> contents_container_;
522 // A container that manages the GtkWidget*s of developer tools for the
523 // selected tab contents.
524 scoped_ptr<TabContentsContainerGtk> devtools_container_;
526 // The Extension Keybinding Registry responsible for registering listeners for
527 // accelerators that are sent to the window, that are destined to be turned
528 // into events and sent to the extension.
529 scoped_ptr<ExtensionKeybindingRegistryGtk> extension_keybinding_registry_;
531 // Docked devtools window instance. NULL when current tab is not inspected
532 // or is inspected with undocked version of DevToolsWindow.
533 DevToolsWindow* devtools_window_;
535 // Resizing strategy of contents_container_ inside
536 // devtools_floating_container_. Non-empty only if docked devtools is visible.
537 DevToolsContentsResizingStrategy contents_resizing_strategy_;
539 // Floating container for devtools_container_ and contents_container_.
540 // Owned by render_area_vbox_.
541 GtkWidget* devtools_floating_container_;
543 // The tab strip. Always non-NULL.
544 scoped_ptr<TabStripGtk> tabstrip_;
546 // The container for info bars. Always non-NULL.
547 scoped_ptr<InfoBarContainerGtk> infobar_container_;
549 // The timer used to update frames for the Loading Animation.
550 base::RepeatingTimer<BrowserWindowGtk> loading_animation_timer_;
552 // The timer used to save the window position for session restore.
553 base::OneShotTimer<BrowserWindowGtk> window_configure_debounce_timer_;
555 // Whether the custom chrome frame pref is set. Normally you want to use
556 // UseCustomFrame() above to determine whether to use the custom frame or
558 BooleanPrefMember use_custom_frame_pref_;
560 // The current window cursor. We set it to a resize cursor when over the
561 // custom frame border. We set it to NULL if we want the default cursor.
562 GdkCursor* frame_cursor_;
564 // True if the window manager thinks the window is active. Not all window
565 // managers keep track of this state (_NET_ACTIVE_WINDOW), in which case
566 // this will always be true.
569 // Optionally maximize or minimize the window after we call
570 // BrowserWindow::Show for the first time. This is to work around a compiz
572 ui::WindowShowState show_state_after_show_;
574 // If true, don't call gdk_window_raise() when we get a click in the title
575 // bar or window border. This is to work around a compiz bug.
576 bool suppress_window_raise_;
578 // The accelerator group used to handle accelerators, owned by this object.
579 GtkAccelGroup* accel_group_;
581 // Set to true while this BrowserWindowGtk is fullscreened. This is needed
582 // because GTK cannot ensure requests to fullscreen the window will be honored
583 // by all window managers; and therefore bit-testing |state_| is not a
584 // reliable "is fullscreened" test. http://crbug.com/286545
587 scoped_ptr<FullscreenExitBubbleGtk> fullscreen_exit_bubble_;
589 content::NotificationRegistrar registrar_;
591 DISALLOW_COPY_AND_ASSIGN(BrowserWindowGtk);
594 #endif // CHROME_BROWSER_UI_GTK_BROWSER_WINDOW_GTK_H_