Home | History | Annotate | Download | only in views
      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/profiles/profile.h"
      8 #include "chrome/browser/ui/gtk/constrained_window_gtk.h"
      9 #include "chrome/browser/ui/gtk/gtk_util.h"
     10 #include "chrome/browser/ui/views/tab_contents/tab_contents_container.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/tab_contents/tab_contents.h"
     14 #include "ui/gfx/rect.h"
     15 #include "views/widget/widget_gtk.h"
     16 
     17 // ConstrainedHtmlDelegateGtk works with ConstrainedWindowGtk to present
     18 // a TabContents in a ContraintedHtmlUI.
     19 class ConstrainedHtmlDelegateGtk : public views::WidgetGtk,
     20                                    public ConstrainedHtmlUIDelegate,
     21                                    public ConstrainedWindowDelegate,
     22                                    public HtmlDialogTabContentsDelegate {
     23  public:
     24   ConstrainedHtmlDelegateGtk(Profile* profile,
     25                              HtmlDialogUIDelegate* delegate);
     26   ~ConstrainedHtmlDelegateGtk();
     27 
     28   // ConstrainedHtmlUIDelegate interface.
     29   virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate();
     30   virtual void OnDialogClose();
     31   virtual bool GetBackgroundColor(GdkColor* color) {
     32     *color = gtk_util::kGdkWhite;
     33     return true;
     34   }
     35 
     36   // ConstrainedWindowGtkDelegate implementation.
     37   virtual GtkWidget* GetWidgetRoot() {
     38     return GetNativeView();
     39   }
     40   virtual GtkWidget* GetFocusWidget() {
     41     return html_tab_contents_.GetContentNativeView();
     42   }
     43   virtual void DeleteDelegate() {
     44     html_delegate_->OnWindowClosed();
     45     html_delegate_->OnDialogClosed("");
     46     tab_container_->ChangeTabContents(NULL);
     47   }
     48   virtual bool ShouldHaveBorderPadding() const {
     49     return false;
     50   }
     51 
     52   // HtmlDialogTabContentsDelegate interface.
     53   void MoveContents(TabContents* source, const gfx::Rect& pos) {}
     54   void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {}
     55 
     56   void set_window(ConstrainedWindow* window) {
     57     window_ = window;
     58   }
     59 
     60  private:
     61   TabContents html_tab_contents_;
     62   TabContentsContainer* tab_container_;
     63 
     64   HtmlDialogUIDelegate* html_delegate_;
     65 
     66   // The constrained window that owns |this|.  Saved so we can close it later.
     67   ConstrainedWindow* window_;
     68 };
     69 
     70 ConstrainedHtmlDelegateGtk::ConstrainedHtmlDelegateGtk(
     71     Profile* profile,
     72     HtmlDialogUIDelegate* delegate)
     73     : views::WidgetGtk(views::WidgetGtk::TYPE_CHILD),
     74       HtmlDialogTabContentsDelegate(profile),
     75       html_tab_contents_(profile, NULL, MSG_ROUTING_NONE, NULL, NULL),
     76       tab_container_(NULL),
     77       html_delegate_(delegate),
     78       window_(NULL) {
     79   CHECK(delegate);
     80   html_tab_contents_.set_delegate(this);
     81 
     82   // Set |this| as a property so the ConstrainedHtmlUI can retrieve it.
     83   ConstrainedHtmlUI::GetPropertyAccessor().SetProperty(
     84       html_tab_contents_.property_bag(), this);
     85   html_tab_contents_.controller().LoadURL(delegate->GetDialogContentURL(),
     86                                           GURL(),
     87                                           PageTransition::START_PAGE);
     88 
     89   Init(NULL, gfx::Rect());
     90 
     91   tab_container_ = new TabContentsContainer;
     92   SetContentsView(tab_container_);
     93   tab_container_->ChangeTabContents(&html_tab_contents_);
     94 
     95   gfx::Size dialog_size;
     96   html_delegate_->GetDialogSize(&dialog_size);
     97   gtk_widget_set_size_request(GetWidgetRoot(),
     98                               dialog_size.width(),
     99                               dialog_size.height());
    100 }
    101 
    102 ConstrainedHtmlDelegateGtk::~ConstrainedHtmlDelegateGtk() {
    103 }
    104 
    105 HtmlDialogUIDelegate* ConstrainedHtmlDelegateGtk::GetHtmlDialogUIDelegate() {
    106   return html_delegate_;
    107 }
    108 
    109 void ConstrainedHtmlDelegateGtk::OnDialogClose() {
    110   window_->CloseConstrainedWindow();
    111 }
    112 
    113 // static
    114 ConstrainedWindow* ConstrainedHtmlUI::CreateConstrainedHtmlDialog(
    115     Profile* profile,
    116     HtmlDialogUIDelegate* delegate,
    117     TabContents* container) {
    118   ConstrainedHtmlDelegateGtk* constrained_delegate =
    119       new ConstrainedHtmlDelegateGtk(profile, delegate);
    120   ConstrainedWindow* constrained_window =
    121       container->CreateConstrainedDialog(constrained_delegate);
    122   constrained_delegate->set_window(constrained_window);
    123   return constrained_window;
    124 }
    125