1// Copyright (c) 2011 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#include "chrome/browser/ui/webui/constrained_html_ui.h"
6
7#include "chrome/browser/ui/gtk/constrained_window_gtk.h"
8#include "chrome/browser/ui/gtk/gtk_util.h"
9#include "chrome/browser/ui/gtk/tab_contents_container_gtk.h"
10#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
11#include "chrome/browser/ui/webui/html_dialog_tab_contents_delegate.h"
12#include "chrome/browser/ui/webui/html_dialog_ui.h"
13#include "content/browser/renderer_host/render_view_host.h"
14#include "content/browser/tab_contents/tab_contents.h"
15#include "content/common/notification_source.h"
16#include "ui/gfx/rect.h"
17
18class ConstrainedHtmlDelegateGtk : public ConstrainedWindowGtkDelegate,
19                                   public HtmlDialogTabContentsDelegate,
20                                   public ConstrainedHtmlUIDelegate {
21 public:
22  ConstrainedHtmlDelegateGtk(Profile* profile,
23                             HtmlDialogUIDelegate* delegate);
24
25  virtual ~ConstrainedHtmlDelegateGtk();
26
27  // ConstrainedWindowGtkDelegate ----------------------------------------------
28  virtual GtkWidget* GetWidgetRoot() {
29    return tab_contents_container_.widget();
30  }
31  virtual GtkWidget* GetFocusWidget() {
32    return tab_.tab_contents()->GetContentNativeView();
33  }
34  virtual void DeleteDelegate() {
35    html_delegate_->OnWindowClosed();
36    html_delegate_->OnDialogClosed("");
37    delete this;
38  }
39
40  // ConstrainedHtmlDelegate ---------------------------------------------
41  virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate();
42  virtual void OnDialogClose();
43  virtual bool GetBackgroundColor(GdkColor* color) {
44    *color = gtk_util::kGdkWhite;
45    return true;
46  }
47
48  // HtmlDialogTabContentsDelegate ---------------------------------------------
49  void MoveContents(TabContents* source, const gfx::Rect& pos) {}
50  void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {}
51
52  void set_window(ConstrainedWindow* window) {
53    window_ = window;
54  }
55
56 private:
57  TabContentsWrapper tab_;
58  TabContentsContainerGtk tab_contents_container_;
59  HtmlDialogUIDelegate* html_delegate_;
60
61  // The constrained window that owns |this|. It's saved here because it needs
62  // to be closed in response to the WebUI OnDialogClose callback.
63  ConstrainedWindow* window_;
64};
65
66ConstrainedHtmlDelegateGtk::ConstrainedHtmlDelegateGtk(
67    Profile* profile,
68    HtmlDialogUIDelegate* delegate)
69    : HtmlDialogTabContentsDelegate(profile),
70      tab_(new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL, NULL)),
71      tab_contents_container_(NULL),
72      html_delegate_(delegate),
73      window_(NULL) {
74  tab_.tab_contents()->set_delegate(this);
75
76  // Set |this| as a property on the tab contents so that the ConstrainedHtmlUI
77  // can get a reference to |this|.
78  ConstrainedHtmlUI::GetPropertyAccessor().SetProperty(
79      tab_.tab_contents()->property_bag(), this);
80
81  tab_.tab_contents()->controller().LoadURL(
82      delegate->GetDialogContentURL(), GURL(), PageTransition::START_PAGE);
83  tab_contents_container_.SetTab(&tab_);
84
85  gfx::Size dialog_size;
86  delegate->GetDialogSize(&dialog_size);
87  gtk_widget_set_size_request(GTK_WIDGET(tab_contents_container_.widget()),
88                              dialog_size.width(),
89                              dialog_size.height());
90
91  gtk_widget_show_all(GetWidgetRoot());
92}
93
94ConstrainedHtmlDelegateGtk::~ConstrainedHtmlDelegateGtk() {
95}
96
97HtmlDialogUIDelegate*
98    ConstrainedHtmlDelegateGtk::GetHtmlDialogUIDelegate() {
99  return html_delegate_;
100}
101
102void ConstrainedHtmlDelegateGtk::OnDialogClose() {
103  window_->CloseConstrainedWindow();
104}
105
106// static
107ConstrainedWindow* ConstrainedHtmlUI::CreateConstrainedHtmlDialog(
108    Profile* profile,
109    HtmlDialogUIDelegate* delegate,
110    TabContents* overshadowed) {
111  ConstrainedHtmlDelegateGtk* constrained_delegate =
112      new ConstrainedHtmlDelegateGtk(profile, delegate);
113  ConstrainedWindow* constrained_window =
114      overshadowed->CreateConstrainedDialog(constrained_delegate);
115  constrained_delegate->set_window(constrained_window);
116  return constrained_window;
117}
118