| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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/gtk/html_dialog_gtk.h" | |
| 6 | |
| 7 #include <gtk/gtk.h> | |
| 8 | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/browser_window.h" | |
| 11 #include "chrome/browser/dom_ui/html_dialog_ui.h" | |
| 12 #include "chrome/browser/gtk/gtk_util.h" | |
| 13 #include "chrome/browser/gtk/tab_contents_container_gtk.h" | |
| 14 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 15 #include "chrome/browser/ui/browser.h" | |
| 16 #include "chrome/common/native_web_keyboard_event.h" | |
| 17 #include "ipc/ipc_message.h" | |
| 18 | |
| 19 // static | |
| 20 void HtmlDialogGtk::ShowHtmlDialogGtk(Browser* browser, | |
| 21 HtmlDialogUIDelegate* delegate, | |
| 22 gfx::NativeWindow parent_window) { | |
| 23 HtmlDialogGtk* html_dialog = | |
| 24 new HtmlDialogGtk(browser->profile(), delegate, parent_window); | |
| 25 html_dialog->InitDialog(); | |
| 26 } | |
| 27 | |
| 28 //////////////////////////////////////////////////////////////////////////////// | |
| 29 // HtmlDialogGtk, public: | |
| 30 | |
| 31 HtmlDialogGtk::HtmlDialogGtk(Profile* profile, | |
| 32 HtmlDialogUIDelegate* delegate, | |
| 33 gfx::NativeWindow parent_window) | |
| 34 : HtmlDialogTabContentsDelegate(profile), | |
| 35 delegate_(delegate), | |
| 36 parent_window_(parent_window), | |
| 37 dialog_(NULL) { | |
| 38 } | |
| 39 | |
| 40 HtmlDialogGtk::~HtmlDialogGtk() { | |
| 41 } | |
| 42 | |
| 43 //////////////////////////////////////////////////////////////////////////////// | |
| 44 // HtmlDialogUIDelegate implementation: | |
| 45 | |
| 46 bool HtmlDialogGtk::IsDialogModal() const { | |
| 47 return delegate_ ? delegate_->IsDialogModal() : false; | |
| 48 } | |
| 49 | |
| 50 std::wstring HtmlDialogGtk::GetDialogTitle() const { | |
| 51 return delegate_ ? delegate_->GetDialogTitle() : L""; | |
| 52 } | |
| 53 | |
| 54 GURL HtmlDialogGtk::GetDialogContentURL() const { | |
| 55 if (delegate_) | |
| 56 return delegate_->GetDialogContentURL(); | |
| 57 else | |
| 58 return GURL(); | |
| 59 } | |
| 60 | |
| 61 void HtmlDialogGtk::GetDOMMessageHandlers( | |
| 62 std::vector<DOMMessageHandler*>* handlers) const { | |
| 63 if (delegate_) | |
| 64 delegate_->GetDOMMessageHandlers(handlers); | |
| 65 else | |
| 66 handlers->clear(); | |
| 67 } | |
| 68 | |
| 69 void HtmlDialogGtk::GetDialogSize(gfx::Size* size) const { | |
| 70 if (delegate_) | |
| 71 delegate_->GetDialogSize(size); | |
| 72 else | |
| 73 *size = gfx::Size(); | |
| 74 } | |
| 75 | |
| 76 std::string HtmlDialogGtk::GetDialogArgs() const { | |
| 77 if (delegate_) | |
| 78 return delegate_->GetDialogArgs(); | |
| 79 else | |
| 80 return std::string(); | |
| 81 } | |
| 82 | |
| 83 void HtmlDialogGtk::OnDialogClosed(const std::string& json_retval) { | |
| 84 DCHECK(dialog_); | |
| 85 | |
| 86 Detach(); | |
| 87 if (delegate_) { | |
| 88 HtmlDialogUIDelegate* dialog_delegate = delegate_; | |
| 89 delegate_ = NULL; // We will not communicate further with the delegate. | |
| 90 dialog_delegate->OnDialogClosed(json_retval); | |
| 91 } | |
| 92 gtk_widget_destroy(dialog_); | |
| 93 delete this; | |
| 94 } | |
| 95 | |
| 96 bool HtmlDialogGtk::ShouldShowDialogTitle() const { | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 //////////////////////////////////////////////////////////////////////////////// | |
| 101 // TabContentsDelegate implementation: | |
| 102 | |
| 103 void HtmlDialogGtk::MoveContents(TabContents* source, const gfx::Rect& pos) { | |
| 104 // The contained web page wishes to resize itself. We let it do this because | |
| 105 // if it's a dialog we know about, we trust it not to be mean to the user. | |
| 106 } | |
| 107 | |
| 108 void HtmlDialogGtk::ToolbarSizeChanged(TabContents* source, | |
| 109 bool is_animating) { | |
| 110 // Ignored. | |
| 111 } | |
| 112 | |
| 113 // A simplified version of BrowserWindowGtk::HandleKeyboardEvent(). | |
| 114 // We don't handle global keyboard shortcuts here, but that's fine since | |
| 115 // they're all browser-specific. (This may change in the future.) | |
| 116 void HtmlDialogGtk::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) { | |
| 117 GdkEventKey* os_event = event.os_event; | |
| 118 if (!os_event || event.type == WebKit::WebInputEvent::Char) | |
| 119 return; | |
| 120 | |
| 121 // To make sure the default key bindings can still work, such as Escape to | |
| 122 // close the dialog. | |
| 123 gtk_bindings_activate_event(GTK_OBJECT(dialog_), os_event); | |
| 124 } | |
| 125 | |
| 126 //////////////////////////////////////////////////////////////////////////////// | |
| 127 // HtmlDialogGtk: | |
| 128 | |
| 129 void HtmlDialogGtk::InitDialog() { | |
| 130 tab_contents_.reset( | |
| 131 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL, NULL)); | |
| 132 tab_contents_->set_delegate(this); | |
| 133 | |
| 134 // This must be done before loading the page; see the comments in | |
| 135 // HtmlDialogUI. | |
| 136 HtmlDialogUI::GetPropertyAccessor().SetProperty(tab_contents_->property_bag(), | |
| 137 this); | |
| 138 | |
| 139 tab_contents_->controller().LoadURL(GetDialogContentURL(), | |
| 140 GURL(), PageTransition::START_PAGE); | |
| 141 GtkDialogFlags flags = GTK_DIALOG_NO_SEPARATOR; | |
| 142 if (delegate_->IsDialogModal()) | |
| 143 flags = static_cast<GtkDialogFlags>(flags | GTK_DIALOG_MODAL); | |
| 144 | |
| 145 dialog_ = gtk_dialog_new_with_buttons( | |
| 146 WideToUTF8(delegate_->GetDialogTitle()).c_str(), | |
| 147 parent_window_, | |
| 148 flags, | |
| 149 NULL); | |
| 150 | |
| 151 g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseThunk), this); | |
| 152 | |
| 153 tab_contents_container_.reset(new TabContentsContainerGtk(NULL)); | |
| 154 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), | |
| 155 tab_contents_container_->widget(), TRUE, TRUE, 0); | |
| 156 | |
| 157 tab_contents_container_->SetTabContents(tab_contents_.get()); | |
| 158 | |
| 159 gfx::Size dialog_size; | |
| 160 delegate_->GetDialogSize(&dialog_size); | |
| 161 | |
| 162 gtk_widget_set_size_request(GTK_WIDGET(tab_contents_container_->widget()), | |
| 163 dialog_size.width(), | |
| 164 dialog_size.height()); | |
| 165 | |
| 166 gtk_widget_show_all(dialog_); | |
| 167 } | |
| 168 | |
| 169 void HtmlDialogGtk::OnResponse(GtkWidget* dialog, int response_id) { | |
| 170 OnDialogClosed(std::string()); | |
| 171 } | |
| OLD | NEW |