Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/ui/webui/constrained_web_dialog_delegate_base.h" |
| 6 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 7 | #include <gdk/gdk.h> |
| 8 | #include <gtk/gtk.h> |
| 9 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | #include "chrome/browser/ui/gtk/constrained_window_gtk.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 11 | #include "components/web_modal/web_contents_modal_dialog_manager.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 12 | #include "content/public/browser/notification_source.h" |
| 13 | #include "content/public/browser/render_view_host.h" |
| 14 | #include "content/public/browser/web_contents.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 15 | #include "content/public/browser/web_contents_view.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 16 | #include "ui/base/gtk/gtk_hig_constants.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 17 | #include "ui/base/gtk/gtk_signal.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | #include "ui/gfx/size.h" |
| 19 | #include "ui/web_dialogs/web_dialog_delegate.h" |
| 20 | #include "ui/web_dialogs/web_dialog_ui.h" |
| 21 | |
| 22 | using content::WebContents; |
| 23 | using ui::WebDialogDelegate; |
| 24 | using ui::WebDialogWebContentsDelegate; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 25 | using web_modal::WebContentsModalDialogManager; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 26 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | class ConstrainedWebDialogDelegateGtk |
| 30 | : public ConstrainedWebDialogDelegateBase { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | public: |
| 32 | ConstrainedWebDialogDelegateGtk( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 33 | content::BrowserContext* browser_context, |
| 34 | WebDialogDelegate* delegate, |
| 35 | WebDialogWebContentsDelegate* tab_delegate) |
| 36 | : ConstrainedWebDialogDelegateBase( |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 37 | browser_context, delegate, tab_delegate), |
| 38 | window_(NULL) {} |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 39 | |
| 40 | // WebDialogWebContentsDelegate interface. |
| 41 | virtual void CloseContents(WebContents* source) OVERRIDE { |
| 42 | gtk_widget_destroy(window_); |
| 43 | } |
| 44 | |
| 45 | void set_window(GtkWidget* window) { window_ = window; } |
| 46 | GtkWidget* window() const { return window_; } |
| 47 | |
| 48 | private: |
| 49 | GtkWidget* window_; |
| 50 | |
| 51 | DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateGtk); |
| 52 | }; |
| 53 | |
| 54 | void SetBackgroundColor(GtkWidget* widget, const GdkColor &color) { |
| 55 | gtk_widget_modify_base(widget, GTK_STATE_NORMAL, &color); |
| 56 | gtk_widget_modify_fg(widget, GTK_STATE_NORMAL, &color); |
| 57 | gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, &color); |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | |
| 62 | class ConstrainedWebDialogDelegateViewGtk |
| 63 | : public ConstrainedWebDialogDelegate { |
| 64 | public: |
| 65 | ConstrainedWebDialogDelegateViewGtk( |
| 66 | content::BrowserContext* browser_context, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 67 | WebDialogDelegate* delegate, |
| 68 | WebDialogWebContentsDelegate* tab_delegate); |
| 69 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 70 | virtual ~ConstrainedWebDialogDelegateViewGtk() {} |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 71 | |
| 72 | // ConstrainedWebDialogDelegate interface |
| 73 | virtual const WebDialogDelegate* |
| 74 | GetWebDialogDelegate() const OVERRIDE { |
| 75 | return impl_->GetWebDialogDelegate(); |
| 76 | } |
| 77 | virtual WebDialogDelegate* GetWebDialogDelegate() OVERRIDE { |
| 78 | return impl_->GetWebDialogDelegate(); |
| 79 | } |
| 80 | virtual void OnDialogCloseFromWebUI() OVERRIDE { |
| 81 | return impl_->OnDialogCloseFromWebUI(); |
| 82 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 83 | virtual void ReleaseWebContentsOnDialogClose() OVERRIDE { |
| 84 | return impl_->ReleaseWebContentsOnDialogClose(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 85 | } |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 86 | virtual web_modal::NativeWebContentsModalDialog GetNativeDialog() OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 87 | return impl_->window(); |
| 88 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 89 | virtual WebContents* GetWebContents() OVERRIDE { |
| 90 | return impl_->GetWebContents(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 93 | void SetWindow(GtkWidget* window) { |
| 94 | impl_->set_window(window); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 95 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 96 | |
| 97 | GtkWidget* GetWindow() { |
| 98 | return impl_->window(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 102 | CHROMEGTK_CALLBACK_0(ConstrainedWebDialogDelegateViewGtk, void, OnDestroy); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 103 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 104 | scoped_ptr<ConstrainedWebDialogDelegateGtk> impl_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 105 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 106 | DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewGtk); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 109 | ConstrainedWebDialogDelegateViewGtk::ConstrainedWebDialogDelegateViewGtk( |
| 110 | content::BrowserContext* browser_context, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 111 | WebDialogDelegate* delegate, |
| 112 | WebDialogWebContentsDelegate* tab_delegate) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 113 | : impl_(new ConstrainedWebDialogDelegateGtk( |
| 114 | browser_context, |
| 115 | delegate, |
| 116 | tab_delegate)) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 117 | gfx::Size dialog_size; |
| 118 | delegate->GetDialogSize(&dialog_size); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 119 | GtkWidget* contents = |
| 120 | GTK_WIDGET(GetWebContents()->GetView()->GetNativeView()); |
| 121 | gtk_widget_set_size_request(contents, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 122 | dialog_size.width(), |
| 123 | dialog_size.height()); |
| 124 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 125 | gtk_widget_show_all(contents); |
| 126 | |
| 127 | g_signal_connect(contents, "destroy", G_CALLBACK(OnDestroyThunk), this); |
| 128 | } |
| 129 | |
| 130 | void ConstrainedWebDialogDelegateViewGtk::OnDestroy(GtkWidget* widget) { |
| 131 | if (!impl_->closed_via_webui()) |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 132 | GetWebDialogDelegate()->OnDialogClosed(std::string()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 133 | delete this; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 137 | content::BrowserContext* browser_context, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 138 | WebDialogDelegate* delegate, |
| 139 | WebDialogWebContentsDelegate* tab_delegate, |
| 140 | content::WebContents* web_contents) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 141 | ConstrainedWebDialogDelegateViewGtk* constrained_delegate = |
| 142 | new ConstrainedWebDialogDelegateViewGtk( |
| 143 | browser_context, delegate, tab_delegate); |
| 144 | GtkWidget* window = |
| 145 | CreateWebContentsModalDialogGtk( |
| 146 | constrained_delegate->GetWebContents()->GetView()->GetNativeView(), |
| 147 | constrained_delegate->GetWebContents()->GetView()-> |
| 148 | GetContentNativeView()); |
| 149 | SetBackgroundColor(window, ui::kGdkWhite); |
| 150 | constrained_delegate->SetWindow(window); |
| 151 | |
| 152 | WebContentsModalDialogManager* web_contents_modal_dialog_manager = |
| 153 | WebContentsModalDialogManager::FromWebContents(web_contents); |
| 154 | web_contents_modal_dialog_manager->ShowDialog(window); |
| 155 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 156 | return constrained_delegate; |
| 157 | } |