Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | |
|
jochen (gone - plz use gerrit)
2012/07/23 15:21:47
please keep this empty line
| |
| 5 #include "content/shell/shell.h" | 4 #include "content/shell/shell.h" |
| 6 | 5 |
| 6 #include "base/utf_string_conversions.h" | |
| 7 #include "ui/aura/desktop/desktop_screen.h" | |
| 8 #include "ui/aura/desktop/desktop_stacking_client.h" | |
| 9 #include "ui/aura/display_manager.h" | |
| 10 #include "ui/aura/env.h" | |
| 11 #include "ui/aura/root_window.h" | |
| 12 #include "ui/aura/single_display_manager.h" | |
| 13 #include "ui/base/accessibility/accessibility_types.h" | |
| 14 #include "ui/base/clipboard/clipboard.h" | |
| 15 #include "ui/gfx/screen.h" | |
| 16 #include "ui/aura/window.h" | |
|
jam
2012/07/23 14:39:46
nit: order
| |
| 17 #include "ui/base/resource/resource_bundle.h" | |
| 18 #include "ui/views/controls/button/text_button.h" | |
| 19 #include "ui/views/controls/textfield/textfield.h" | |
| 20 #include "ui/views/controls/textfield/textfield_controller.h" | |
| 21 #include "ui/views/controls/webview/webview.h" | |
| 22 #include "ui/views/layout/fill_layout.h" | |
| 23 #include "ui/views/layout/grid_layout.h" | |
| 24 #include "ui/views/view.h" | |
| 25 #include "ui/views/views_delegate.h" | |
| 26 #include "ui/views/widget/desktop_native_widget_helper_aura.h" | |
| 27 #include "ui/views/widget/widget.h" | |
| 28 #include "ui/views/widget/widget_delegate.h" | |
| 29 | |
| 30 namespace views{ | |
|
jochen (gone - plz use gerrit)
2012/07/23 15:21:47
space between views and {
| |
| 31 // ViewDelegate implementation for aura content shell | |
| 32 class ShellViewsDelegateAura : public ViewsDelegate { | |
| 33 public: | |
| 34 ShellViewsDelegateAura() | |
| 35 : use_transparent_windows_(false) { | |
| 36 DCHECK(!ViewsDelegate::views_delegate); | |
| 37 ViewsDelegate::views_delegate = this; | |
| 38 } | |
|
jochen (gone - plz use gerrit)
2012/07/23 15:21:47
empty lines between methods.
This class is suffic
| |
| 39 virtual ~ShellViewsDelegateAura() { | |
| 40 ViewsDelegate::views_delegate = NULL; | |
| 41 } | |
| 42 | |
| 43 void SetUseTransparentWindows(bool transparent) { | |
| 44 use_transparent_windows_ = transparent; | |
| 45 } | |
| 46 | |
| 47 // Overridden from ViewsDelegate: | |
| 48 virtual ui::Clipboard* GetClipboard() const OVERRIDE { | |
| 49 if (!clipboard_.get()) { | |
| 50 clipboard_.reset(new ui::Clipboard); | |
| 51 } | |
| 52 return clipboard_.get(); | |
| 53 } | |
| 54 virtual void SaveWindowPlacement(const Widget* window, | |
| 55 const std::string& window_name, | |
| 56 const gfx::Rect& bounds, | |
| 57 ui::WindowShowState show_state) OVERRIDE { | |
| 58 } | |
| 59 virtual bool GetSavedWindowPlacement( | |
| 60 const std::string& window_name, | |
| 61 gfx::Rect* bounds, | |
| 62 ui::WindowShowState* show_state) const OVERRIDE { | |
| 63 return false; | |
| 64 } | |
| 65 | |
| 66 virtual void NotifyAccessibilityEvent( | |
| 67 View* view, ui::AccessibilityTypes::Event event_type) OVERRIDE {} | |
| 68 | |
| 69 virtual void NotifyMenuItemFocused(const string16& menu_name, | |
| 70 const string16& menu_item_name, | |
| 71 int item_index, | |
| 72 int item_count, | |
| 73 bool has_submenu) OVERRIDE {} | |
| 74 #if defined(OS_WIN) | |
| 75 virtual HICON GetDefaultWindowIcon() const OVERRIDE { | |
| 76 return NULL; | |
| 77 } | |
| 78 #endif | |
| 79 virtual NonClientFrameView* CreateDefaultNonClientFrameView( | |
| 80 Widget* widget) OVERRIDE { | |
| 81 return NULL; | |
| 82 } | |
| 83 virtual bool UseTransparentWindows() const OVERRIDE { | |
| 84 return use_transparent_windows_; | |
| 85 } | |
| 86 virtual void AddRef() OVERRIDE {} | |
| 87 virtual void ReleaseRef() OVERRIDE {} | |
| 88 | |
| 89 virtual int GetDispositionForEvent(int event_flags) OVERRIDE { | |
| 90 return 0; | |
| 91 } | |
| 92 | |
| 93 virtual views::NativeWidgetHelperAura* CreateNativeWidgetHelper( | |
| 94 views::NativeWidgetAura* native_widget) OVERRIDE { | |
| 95 return new views::DesktopNativeWidgetHelperAura(native_widget); | |
| 96 } | |
| 97 | |
| 98 virtual content::WebContents* CreateWebContents( | |
| 99 content::BrowserContext* browser_context, | |
| 100 content::SiteInstance* site_instance) OVERRIDE { | |
| 101 return NULL; | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 mutable scoped_ptr<ui::Clipboard> clipboard_; | |
| 106 bool use_transparent_windows_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(ShellViewsDelegateAura); | |
| 109 }; | |
| 110 | |
| 111 // Maintain the UI controls and web view for content shell | |
| 112 class ShellWindowDelegateView : public WidgetDelegateView, | |
| 113 public TextfieldController, | |
| 114 public ButtonListener { | |
| 115 public: | |
| 116 enum UIControl { | |
| 117 BACK_BUTTON, | |
| 118 FORWARD_BUTTON, | |
| 119 STOP_BUTTON | |
| 120 }; | |
| 121 | |
| 122 ShellWindowDelegateView(content::Shell* shell) | |
|
jochen (gone - plz use gerrit)
2012/07/23 15:21:47
explicit
| |
| 123 : shell_(shell), | |
| 124 toolbar_view_(new View), | |
| 125 contents_view_(new View) { | |
| 126 } | |
| 127 ~ShellWindowDelegateView() {} | |
|
jochen (gone - plz use gerrit)
2012/07/23 15:21:47
virtual
| |
| 128 | |
| 129 // Update the state of UI controls | |
| 130 void SetAddressBarURL(const GURL& url); | |
|
jochen (gone - plz use gerrit)
2012/07/23 15:21:47
please separate the declaration and implementation
jam
2012/07/23 22:55:55
I actually prefer when they're inline if the class
| |
| 131 void SetWebContents(content::WebContents* web_contents); | |
| 132 void SetWindowTitle(const string16& title) { title_ = title; } | |
| 133 void EnableUIControl(UIControl control, bool is_enabled); | |
| 134 | |
| 135 private: | |
| 136 // Initialize the UI control contained in shell window | |
| 137 void InitShellWindow(); | |
| 138 | |
| 139 // Overriden from TextfieldController | |
| 140 virtual void ContentsChanged(Textfield* sender, | |
| 141 const string16& new_contents) OVERRIDE { | |
| 142 } | |
| 143 virtual bool HandleKeyEvent(Textfield* sender, | |
| 144 const KeyEvent& key_event) OVERRIDE; | |
| 145 | |
| 146 // Overriden from ButtonListener | |
| 147 virtual void ButtonPressed(Button* sender, | |
| 148 const Event& event) OVERRIDE; | |
| 149 | |
| 150 // Overriden from WidgetDelegateView | |
| 151 virtual bool CanResize() const OVERRIDE { return true; } | |
| 152 virtual bool CanMaximize() const OVERRIDE { return true; } | |
| 153 virtual string16 GetWindowTitle() const OVERRIDE { | |
| 154 return title_; | |
| 155 } | |
| 156 virtual void WindowClosing() OVERRIDE { | |
| 157 if (shell_) delete shell_; | |
| 158 } | |
| 159 virtual View* GetContentsView() OVERRIDE { return this; } | |
| 160 | |
| 161 // Overriden from View | |
| 162 virtual void ViewHierarchyChanged(bool is_add, | |
| 163 View* parent, | |
| 164 View* child) OVERRIDE { | |
| 165 if (is_add && child == this) { | |
| 166 InitShellWindow(); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 private: | |
| 171 // Hold a reference of Shell for deleting it when the window is closing | |
| 172 content::Shell* shell_; | |
| 173 | |
| 174 // Window title | |
| 175 string16 title_; | |
| 176 | |
| 177 // Toolbar view contains forward/backward/reload button and URL entry | |
| 178 View* toolbar_view_; | |
| 179 NativeTextButton* back_button_; | |
| 180 NativeTextButton* forward_button_; | |
| 181 NativeTextButton* refresh_button_; | |
| 182 NativeTextButton* stop_button_; | |
| 183 Textfield* url_entry_; | |
| 184 | |
| 185 // Contents view contains the web contents view | |
| 186 View* contents_view_; | |
| 187 WebView* web_view_; | |
| 188 | |
| 189 DISALLOW_COPY_AND_ASSIGN(ShellWindowDelegateView); | |
| 190 }; | |
| 191 | |
| 192 // ShellWindowDelegateView implelementation | |
| 193 void ShellWindowDelegateView::SetAddressBarURL(const GURL& url) { | |
| 194 url_entry_->SetText(ASCIIToUTF16(url.spec())); | |
| 195 } | |
| 196 | |
| 197 void ShellWindowDelegateView::SetWebContents( | |
| 198 content::WebContents* web_contents) { | |
| 199 contents_view_->SetLayoutManager(new FillLayout()); | |
| 200 web_view_ = new WebView(web_contents->GetBrowserContext()); | |
| 201 web_view_->SetWebContents(web_contents); | |
| 202 web_contents->Focus(); | |
| 203 contents_view_->AddChildView(web_view_); | |
| 204 Layout(); | |
| 205 } | |
| 206 | |
| 207 void ShellWindowDelegateView::EnableUIControl(UIControl control, | |
| 208 bool is_enabled) { | |
| 209 if (control == BACK_BUTTON) { | |
| 210 back_button_->SetState(is_enabled ? CustomButton::BS_NORMAL | |
| 211 : CustomButton::BS_DISABLED); | |
| 212 } else if (control == FORWARD_BUTTON) { | |
| 213 forward_button_->SetState(is_enabled ? CustomButton::BS_NORMAL | |
| 214 : CustomButton::BS_DISABLED); | |
| 215 } else if (control == STOP_BUTTON) { | |
| 216 stop_button_->SetState(is_enabled ? CustomButton::BS_NORMAL | |
| 217 : CustomButton::BS_DISABLED); | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 // Overriden from TextfieldController | |
| 222 bool ShellWindowDelegateView::HandleKeyEvent(Textfield* sender, | |
| 223 const KeyEvent& key_event) OVERRIDE { | |
| 224 if (sender == url_entry_ && key_event.key_code() == ui::VKEY_RETURN) { | |
| 225 std::string text = UTF16ToUTF8(url_entry_->text()); | |
| 226 GURL url(text); | |
| 227 if (!url.has_scheme()) { | |
| 228 url = GURL(std::string("http://") + std::string(text)); | |
| 229 url_entry_->SetText(ASCIIToUTF16(url.spec())); | |
| 230 } | |
| 231 shell_->LoadURL(url); | |
| 232 return true; | |
| 233 } | |
| 234 return false; | |
| 235 } | |
| 236 | |
| 237 // Overriden from ButtonListener | |
| 238 void ShellWindowDelegateView::ButtonPressed(Button* sender, | |
| 239 const Event& event) OVERRIDE { | |
| 240 if (sender == back_button_) | |
| 241 shell_->GoBackOrForward(-1); | |
| 242 else if (sender == forward_button_) | |
| 243 shell_->GoBackOrForward(1); | |
| 244 else if (sender == refresh_button_) | |
| 245 shell_->Reload(); | |
| 246 else if (sender == stop_button_) | |
| 247 shell_->Stop(); | |
| 248 } | |
| 249 | |
| 250 void ShellWindowDelegateView::InitShellWindow() { | |
| 251 set_background(Background::CreateStandardPanelBackground()); | |
| 252 | |
| 253 GridLayout* layout = new GridLayout(this); | |
| 254 SetLayoutManager(layout); | |
| 255 | |
| 256 ColumnSet* column_set = layout->AddColumnSet(0); | |
| 257 column_set->AddPaddingColumn(0, 2); | |
| 258 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
| 259 GridLayout::USE_PREF, 0, 0); | |
| 260 column_set->AddPaddingColumn(0, 2); | |
| 261 | |
| 262 layout->AddPaddingRow(0, 2); | |
| 263 | |
| 264 // Add toolbar buttons and URL text field | |
| 265 { | |
| 266 layout->StartRow(0, 0); | |
| 267 GridLayout* toolbar_layout = new GridLayout(toolbar_view_); | |
| 268 toolbar_view_->SetLayoutManager(toolbar_layout); | |
| 269 | |
| 270 ColumnSet* toolbar_column_set = | |
| 271 toolbar_layout->AddColumnSet(0); | |
| 272 // Back button | |
| 273 back_button_ = new NativeTextButton(this, ASCIIToUTF16("Back")); | |
| 274 gfx::Size back_button_size = back_button_->GetPreferredSize(); | |
| 275 toolbar_column_set->AddColumn(GridLayout::CENTER, | |
| 276 GridLayout::CENTER, 0, | |
| 277 GridLayout::FIXED, | |
| 278 back_button_size.width(), | |
| 279 back_button_size.width() / 2); | |
| 280 // Forward button | |
| 281 forward_button_ = new NativeTextButton(this, ASCIIToUTF16("Forward")); | |
| 282 gfx::Size forward_button_size = forward_button_->GetPreferredSize(); | |
| 283 toolbar_column_set->AddColumn(GridLayout::CENTER, | |
| 284 GridLayout::CENTER, 0, | |
| 285 GridLayout::FIXED, | |
| 286 forward_button_size.width(), | |
| 287 forward_button_size.width() / 2); | |
| 288 // Refresh button | |
| 289 refresh_button_ = new NativeTextButton(this, ASCIIToUTF16("Refresh")); | |
| 290 gfx::Size refresh_button_size = refresh_button_->GetPreferredSize(); | |
| 291 toolbar_column_set->AddColumn(GridLayout::CENTER, | |
| 292 GridLayout::CENTER, 0, | |
| 293 GridLayout::FIXED, | |
| 294 refresh_button_size.width(), | |
| 295 refresh_button_size.width() / 2); | |
| 296 // Stop button | |
| 297 stop_button_ = new NativeTextButton(this, ASCIIToUTF16("Stop")); | |
| 298 gfx::Size stop_button_size = stop_button_->GetPreferredSize(); | |
| 299 toolbar_column_set->AddColumn(GridLayout::CENTER, | |
| 300 GridLayout::CENTER, 0, | |
| 301 GridLayout::FIXED, | |
| 302 stop_button_size.width(), | |
| 303 stop_button_size.width() / 2); | |
| 304 toolbar_column_set->AddPaddingColumn(0, 2); | |
| 305 // URL entry | |
| 306 url_entry_ = new Textfield(); | |
| 307 url_entry_->SetController(this); | |
| 308 toolbar_column_set->AddColumn(GridLayout::FILL, | |
| 309 GridLayout::FILL, 1, | |
| 310 GridLayout::USE_PREF, 0, 0); | |
| 311 | |
| 312 // Fill up the first row | |
| 313 toolbar_layout->StartRow(0, 0); | |
| 314 toolbar_layout->AddView(back_button_); | |
| 315 toolbar_layout->AddView(forward_button_); | |
| 316 toolbar_layout->AddView(refresh_button_); | |
| 317 toolbar_layout->AddView(stop_button_); | |
| 318 toolbar_layout->AddView(url_entry_); | |
| 319 | |
| 320 layout->AddView(toolbar_view_); | |
| 321 } | |
| 322 | |
| 323 layout->AddPaddingRow(0, 5); | |
| 324 | |
| 325 // Add web contents view as the second row | |
| 326 { | |
| 327 layout->StartRow(1, 0); | |
| 328 layout->AddView(contents_view_); | |
| 329 } | |
| 330 | |
| 331 layout->AddPaddingRow(0, 5); | |
| 332 } | |
| 333 } // namespace views | |
| 334 | |
| 7 namespace content { | 335 namespace content { |
| 8 | 336 |
| 337 using views::ShellWindowDelegateView; | |
| 338 using views::ShellViewsDelegateAura; | |
| 339 | |
| 340 scoped_ptr<aura::client::StackingClient> Shell::stacking_client_; | |
|
jam
2012/07/23 14:39:46
only pod are allowed as statics per chrome style g
| |
| 341 scoped_ptr<views::ViewsDelegate> Shell::views_delegate_; | |
| 9 // static | 342 // static |
| 10 void Shell::PlatformInitialize() { | 343 void Shell::PlatformInitialize() { |
| 344 aura::Env::GetInstance()->SetDisplayManager(new aura::SingleDisplayManager); | |
| 345 stacking_client_.reset(new aura::DesktopStackingClient); | |
| 346 gfx::Screen::SetInstance(aura::CreateDesktopScreen()); | |
| 347 views_delegate_.reset(new ShellViewsDelegateAura); | |
| 348 | |
| 349 // Loading locale resources for Widget::CreateWindowWithBounds initialization | |
| 350 // in which CustomFrameView requires the accessible name for buttons. | |
| 351 ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("en-US"); | |
| 11 } | 352 } |
| 12 | 353 |
| 13 base::StringPiece Shell::PlatformResourceProvider(int key) { | 354 base::StringPiece Shell::PlatformResourceProvider(int key) { |
| 14 return base::StringPiece(); | 355 return base::StringPiece(); |
| 15 } | 356 } |
| 16 | 357 |
| 17 void Shell::PlatformExit() { | 358 void Shell::PlatformExit() { |
| 359 stacking_client_.reset(); | |
| 360 views_delegate_.reset(); | |
| 361 aura::Env::DeleteInstance(); | |
| 18 } | 362 } |
| 19 | 363 |
| 20 void Shell::PlatformCleanUp() { | 364 void Shell::PlatformCleanUp() { |
| 21 } | 365 } |
| 22 | 366 |
| 23 void Shell::PlatformEnableUIControl(UIControl control, bool is_enabled) { | 367 void Shell::PlatformEnableUIControl(UIControl control, bool is_enabled) { |
| 368 ShellWindowDelegateView* delegate_view = | |
| 369 static_cast<ShellWindowDelegateView*>(window_widget_->widget_delegate()); | |
| 370 if (control == BACK_BUTTON) { | |
| 371 delegate_view->EnableUIControl(ShellWindowDelegateView::BACK_BUTTON, | |
| 372 is_enabled); | |
| 373 } else if (control == FORWARD_BUTTON) { | |
| 374 delegate_view->EnableUIControl(ShellWindowDelegateView::FORWARD_BUTTON, | |
| 375 is_enabled); | |
| 376 } else if (control == STOP_BUTTON) { | |
| 377 delegate_view->EnableUIControl(ShellWindowDelegateView::STOP_BUTTON, | |
| 378 is_enabled); | |
| 379 } | |
| 24 } | 380 } |
| 25 | 381 |
| 26 void Shell::PlatformSetAddressBarURL(const GURL& url) { | 382 void Shell::PlatformSetAddressBarURL(const GURL& url) { |
| 383 ShellWindowDelegateView* delegate_view = | |
| 384 static_cast<ShellWindowDelegateView*>(window_widget_->widget_delegate()); | |
| 385 delegate_view->SetAddressBarURL(url); | |
| 27 } | 386 } |
| 28 | 387 |
| 29 void Shell::PlatformSetIsLoading(bool loading) { | 388 void Shell::PlatformSetIsLoading(bool loading) { |
| 30 } | 389 } |
| 31 | 390 |
| 32 void Shell::PlatformCreateWindow(int width, int height) { | 391 void Shell::PlatformCreateWindow(int width, int height) { |
| 392 window_widget_ = | |
| 393 views::Widget::CreateWindowWithBounds(new ShellWindowDelegateView(this), | |
| 394 gfx::Rect(0, 0, width, height)); | |
| 395 window_ = window_widget_->GetNativeWindow(); | |
| 396 window_widget_->Show(); | |
| 33 } | 397 } |
| 34 | 398 |
| 35 void Shell::PlatformSetContents() { | 399 void Shell::PlatformSetContents() { |
| 400 ShellWindowDelegateView* delegate_view = | |
| 401 static_cast<ShellWindowDelegateView*>(window_widget_->widget_delegate()); | |
| 402 delegate_view->SetWebContents(web_contents_.get()); | |
| 36 } | 403 } |
| 37 | 404 |
| 38 void Shell::PlatformResizeSubViews() { | 405 void Shell::PlatformResizeSubViews() { |
| 39 } | 406 } |
| 40 | 407 |
| 41 void Shell::Close() { | 408 void Shell::Close() { |
| 409 window_widget_->Close(); | |
| 42 } | 410 } |
| 43 | 411 |
| 44 void Shell::PlatformSetTitle(const string16& title) { | 412 void Shell::PlatformSetTitle(const string16& title) { |
| 413 ShellWindowDelegateView* delegate_view = | |
| 414 static_cast<ShellWindowDelegateView*>(window_widget_->widget_delegate()); | |
| 415 delegate_view->SetWindowTitle(title); | |
| 416 window_widget_->UpdateWindowTitle(); | |
| 45 } | 417 } |
| 46 | 418 |
| 47 } // namespace content | 419 } // namespace content |
| OLD | NEW |