blob: c58ed62ffde7ef034414e5b7039b09e48d0a5893 [file] [log] [blame]
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001// Copyright 2012 The Chromium Authors. All rights reserved.
Torne (Richard Coles)58218062012-11-14 11:43:16 +00002// 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)2a99a7e2013-03-28 15:31:22 +00007#include <gdk/gdk.h>
8#include <gtk/gtk.h>
9
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010#include "chrome/browser/ui/gtk/constrained_window_gtk.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010011#include "components/web_modal/web_contents_modal_dialog_manager.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000012#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)2a99a7e2013-03-28 15:31:22 +000015#include "content/public/browser/web_contents_view.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "ui/base/gtk/gtk_hig_constants.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000017#include "ui/base/gtk/gtk_signal.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018#include "ui/gfx/size.h"
19#include "ui/web_dialogs/web_dialog_delegate.h"
20#include "ui/web_dialogs/web_dialog_ui.h"
21
22using content::WebContents;
23using ui::WebDialogDelegate;
24using ui::WebDialogWebContentsDelegate;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010025using web_modal::WebContentsModalDialogManager;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000026
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000027namespace {
28
29class ConstrainedWebDialogDelegateGtk
30 : public ConstrainedWebDialogDelegateBase {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031 public:
32 ConstrainedWebDialogDelegateGtk(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000033 content::BrowserContext* browser_context,
34 WebDialogDelegate* delegate,
35 WebDialogWebContentsDelegate* tab_delegate)
36 : ConstrainedWebDialogDelegateBase(
Ben Murdochbb1529c2013-08-08 10:24:53 +010037 browser_context, delegate, tab_delegate),
38 window_(NULL) {}
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000039
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
54void 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
62class ConstrainedWebDialogDelegateViewGtk
63 : public ConstrainedWebDialogDelegate {
64 public:
65 ConstrainedWebDialogDelegateViewGtk(
66 content::BrowserContext* browser_context,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000067 WebDialogDelegate* delegate,
68 WebDialogWebContentsDelegate* tab_delegate);
69
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000070 virtual ~ConstrainedWebDialogDelegateViewGtk() {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +000071
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)2a99a7e2013-03-28 15:31:22 +000083 virtual void ReleaseWebContentsOnDialogClose() OVERRIDE {
84 return impl_->ReleaseWebContentsOnDialogClose();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000085 }
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010086 virtual web_modal::NativeWebContentsModalDialog GetNativeDialog() OVERRIDE {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000087 return impl_->window();
88 }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000089 virtual WebContents* GetWebContents() OVERRIDE {
90 return impl_->GetWebContents();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000091 }
92
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000093 void SetWindow(GtkWidget* window) {
94 impl_->set_window(window);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000095 }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000096
97 GtkWidget* GetWindow() {
98 return impl_->window();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000099 }
100
101 private:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000102 CHROMEGTK_CALLBACK_0(ConstrainedWebDialogDelegateViewGtk, void, OnDestroy);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000103
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000104 scoped_ptr<ConstrainedWebDialogDelegateGtk> impl_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000105
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000106 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewGtk);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000107};
108
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000109ConstrainedWebDialogDelegateViewGtk::ConstrainedWebDialogDelegateViewGtk(
110 content::BrowserContext* browser_context,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000111 WebDialogDelegate* delegate,
112 WebDialogWebContentsDelegate* tab_delegate)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000113 : impl_(new ConstrainedWebDialogDelegateGtk(
114 browser_context,
115 delegate,
116 tab_delegate)) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000117 gfx::Size dialog_size;
118 delegate->GetDialogSize(&dialog_size);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000119 GtkWidget* contents =
120 GTK_WIDGET(GetWebContents()->GetView()->GetNativeView());
121 gtk_widget_set_size_request(contents,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000122 dialog_size.width(),
123 dialog_size.height());
124
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000125 gtk_widget_show_all(contents);
126
127 g_signal_connect(contents, "destroy", G_CALLBACK(OnDestroyThunk), this);
128}
129
130void ConstrainedWebDialogDelegateViewGtk::OnDestroy(GtkWidget* widget) {
131 if (!impl_->closed_via_webui())
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100132 GetWebDialogDelegate()->OnDialogClosed(std::string());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000133 delete this;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000134}
135
136ConstrainedWebDialogDelegate* CreateConstrainedWebDialog(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000137 content::BrowserContext* browser_context,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000138 WebDialogDelegate* delegate,
139 WebDialogWebContentsDelegate* tab_delegate,
140 content::WebContents* web_contents) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000141 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)58218062012-11-14 11:43:16 +0000156 return constrained_delegate;
157}