OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 | 4 |
5 #include "chrome/browser/chromeos/main_menu.h" | 5 #include "chrome/browser/chromeos/main_menu.h" |
6 | 6 |
7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
| 8 #include "base/message_loop.h" |
8 #include "chrome/browser/browser.h" | 9 #include "chrome/browser/browser.h" |
9 #include "chrome/browser/renderer_host/render_view_host.h" | 10 #include "chrome/browser/renderer_host/render_view_host.h" |
10 #include "chrome/browser/renderer_host/render_view_host_factory.h" | 11 #include "chrome/browser/renderer_host/render_view_host_factory.h" |
11 #include "chrome/browser/renderer_host/render_widget_host_view.h" | 12 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
12 #include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" | 13 #include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" |
13 #include "chrome/browser/renderer_host/site_instance.h" | 14 #include "chrome/browser/renderer_host/site_instance.h" |
| 15 #include "chrome/browser/tab_contents/tab_contents.h" |
14 #include "chrome/browser/tab_contents/render_view_host_delegate_helper.h" | 16 #include "chrome/browser/tab_contents/render_view_host_delegate_helper.h" |
15 #include "grit/app_resources.h" | 17 #include "grit/app_resources.h" |
16 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
17 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
18 #include "views/controls/image_view.h" | 20 #include "views/controls/image_view.h" |
19 #include "views/widget/widget_gtk.h" | 21 #include "views/widget/widget_gtk.h" |
20 | 22 |
21 static const int kRendererX = 0; | 23 static const int kRendererX = 0; |
22 static const int kRendererY = 25; | 24 static const int kRendererY = 25; |
23 static const int kRendererWidth = 250; | 25 static const int kRendererWidth = 250; |
24 static const int kRendererHeight = 400; | 26 static const int kRendererHeight = 400; |
25 | 27 |
26 // URL of the page to load. | 28 // URL of the page to load. |
27 static const char kMenuURL[] = "http://goto.ext.google.com/crux-menu"; | 29 static const char kMenuURL[] = "http://goto.ext.google.com/crux-menu"; |
28 | 30 |
29 // static | 31 // static |
30 void MainMenu::Show(Browser* browser) { | 32 void MainMenu::Show(Browser* browser) { |
31 (new MainMenu(browser))->ShowImpl(); | 33 (new MainMenu(browser))->ShowImpl(); |
32 } | 34 } |
33 | 35 |
| 36 MainMenu::~MainMenu() { |
| 37 popup_->Close(); |
| 38 menu_rvh_->Shutdown(); |
| 39 } |
| 40 |
34 MainMenu::MainMenu(Browser* browser) | 41 MainMenu::MainMenu(Browser* browser) |
35 : browser_(browser), | 42 : browser_(browser), |
36 popup_(NULL), | 43 popup_(NULL), |
37 site_instance_(NULL), | 44 site_instance_(NULL), |
38 menu_rvh_(NULL), | 45 menu_rvh_(NULL), |
39 rwhv_(NULL), | 46 rwhv_(NULL), |
40 child_rvh_(NULL) { | 47 ALLOW_THIS_IN_INITIALIZER_LIST(tab_contents_delegate_(this)) { |
41 } | |
42 | |
43 MainMenu::~MainMenu() { | |
44 gdk_pointer_ungrab(GDK_CURRENT_TIME); | |
45 popup_->Close(); | |
46 menu_rvh_->Shutdown(); | |
47 if (child_rvh_) | |
48 child_rvh_->Shutdown(); | |
49 } | 48 } |
50 | 49 |
51 void MainMenu::ShowImpl() { | 50 void MainMenu::ShowImpl() { |
52 SkBitmap* drop_down_image = ResourceBundle::GetSharedInstance(). | 51 SkBitmap* drop_down_image = ResourceBundle::GetSharedInstance(). |
53 GetBitmapNamed(IDR_MAIN_MENU_BUTTON_DROP_DOWN); | 52 GetBitmapNamed(IDR_MAIN_MENU_BUTTON_DROP_DOWN); |
54 views::ImageView* background_view = new views::ImageView(); | 53 views::ImageView* background_view = new views::ImageView(); |
55 background_view->SetImage(drop_down_image); | 54 background_view->SetImage(drop_down_image); |
56 | 55 |
57 views::WidgetGtk* menu_popup = | 56 views::WidgetGtk* menu_popup = |
58 new views::WidgetGtk(views::WidgetGtk::TYPE_POPUP); | 57 new views::WidgetGtk(views::WidgetGtk::TYPE_POPUP); |
(...skipping 18 matching lines...) Expand all Loading... |
77 kRendererWidth, kRendererHeight); | 76 kRendererWidth, kRendererHeight); |
78 rwhv_->SetSize(gfx::Size(kRendererWidth, kRendererHeight)); | 77 rwhv_->SetSize(gfx::Size(kRendererWidth, kRendererHeight)); |
79 menu_rvh_->NavigateToURL(menu_url); | 78 menu_rvh_->NavigateToURL(menu_url); |
80 menu_popup->Show(); | 79 menu_popup->Show(); |
81 | 80 |
82 GtkWidget* rwhv_widget = rwhv_->GetNativeView(); | 81 GtkWidget* rwhv_widget = rwhv_->GetNativeView(); |
83 gtk_widget_realize(rwhv_widget); | 82 gtk_widget_realize(rwhv_widget); |
84 g_signal_connect(rwhv_widget, "button-press-event", | 83 g_signal_connect(rwhv_widget, "button-press-event", |
85 G_CALLBACK(CallButtonPressEvent), this); | 84 G_CALLBACK(CallButtonPressEvent), this); |
86 // Do a mouse grab on the renderer widget host view's widget so that we can | 85 // Do a mouse grab on the renderer widget host view's widget so that we can |
87 // close the popup if the user clicks anywhere else. | 86 // close the popup if the user clicks anywhere else. And do a keyboard |
| 87 // grab so that we get all key events. |
88 gdk_pointer_grab(rwhv_widget->window, FALSE, | 88 gdk_pointer_grab(rwhv_widget->window, FALSE, |
89 static_cast<GdkEventMask>( | 89 static_cast<GdkEventMask>( |
90 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | | 90 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | |
91 GDK_POINTER_MOTION_MASK), | 91 GDK_POINTER_MOTION_MASK), |
92 NULL, NULL, GDK_CURRENT_TIME); | 92 NULL, NULL, GDK_CURRENT_TIME); |
| 93 gdk_keyboard_grab(rwhv_widget->window, FALSE, GDK_CURRENT_TIME); |
| 94 } |
| 95 |
| 96 void MainMenu::Delete(bool now) { |
| 97 gdk_keyboard_ungrab(GDK_CURRENT_TIME); |
| 98 gdk_pointer_ungrab(GDK_CURRENT_TIME); |
| 99 // Hide the popup immediately. We don't close it as it contains the |
| 100 // renderwidgethostview, which hasn't been shutdown yet. |
| 101 popup_->Hide(); |
| 102 if (now) |
| 103 delete this; |
| 104 else |
| 105 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 106 } |
| 107 |
| 108 // static |
| 109 gboolean MainMenu::CallButtonPressEvent(GtkWidget* widget, |
| 110 GdkEventButton* event, |
| 111 MainMenu* menu) { |
| 112 return menu->OnButtonPressEvent(widget, event); |
93 } | 113 } |
94 | 114 |
95 gboolean MainMenu::OnButtonPressEvent(GtkWidget* widget, | 115 gboolean MainMenu::OnButtonPressEvent(GtkWidget* widget, |
96 GdkEventButton* event) { | 116 GdkEventButton* event) { |
97 if (event->x < 0 || event->y < 0 || | 117 if (event->x < 0 || event->y < 0 || |
98 event->x >= widget->allocation.width || | 118 event->x >= widget->allocation.width || |
99 event->y >= widget->allocation.height) { | 119 event->y >= widget->allocation.height) { |
100 // The user clicked outside the bounds of the menu, delete the main which | 120 // The user clicked outside the bounds of the menu, delete the main which |
101 // results in closing it. | 121 // results in closing it. |
102 delete this; | 122 Delete(true); |
103 } | 123 } |
104 return FALSE; | 124 return FALSE; |
105 } | 125 } |
106 | 126 |
107 void MainMenu::RequestOpenURL(const GURL& url, | |
108 const GURL& referrer, | |
109 WindowOpenDisposition disposition) { | |
110 browser_->OpenURL(url, referrer, NEW_FOREGROUND_TAB, PageTransition::LINK); | |
111 delete this; | |
112 } | |
113 | |
114 void MainMenu::CreateNewWindow(int route_id, | 127 void MainMenu::CreateNewWindow(int route_id, |
115 base::WaitableEvent* modal_dialog_event) { | 128 base::WaitableEvent* modal_dialog_event) { |
116 if (child_rvh_) { | 129 if (pending_contents_.get()) { |
117 // We should never get here. If we do, it indicates a child render view | |
118 // host was created and we didn't get a subsequent RequestOpenURL. | |
119 NOTREACHED(); | 130 NOTREACHED(); |
120 return; | 131 return; |
121 } | 132 } |
122 DCHECK(!child_rvh_); | 133 |
123 child_rvh_ = RenderViewHostFactory::Create( | 134 helper_.CreateNewWindow(route_id, modal_dialog_event, browser_->profile(), |
124 site_instance_, this, route_id, modal_dialog_event); | 135 site_instance_, |
| 136 DOMUIFactory::GetDOMUIType(GURL(kMenuURL))); |
| 137 pending_contents_.reset(helper_.GetCreatedWindow(route_id)); |
| 138 pending_contents_->set_delegate(&tab_contents_delegate_); |
125 } | 139 } |
| 140 |
| 141 void MainMenu::ShowCreatedWindow(int route_id, |
| 142 WindowOpenDisposition disposition, |
| 143 const gfx::Rect& initial_pos, |
| 144 bool user_gesture, |
| 145 const GURL& creator_url) { |
| 146 if (disposition == NEW_POPUP) { |
| 147 pending_contents_->set_delegate(NULL); |
| 148 browser_->GetSelectedTabContents()->AddNewContents( |
| 149 pending_contents_.release(), disposition, initial_pos, user_gesture, |
| 150 creator_url); |
| 151 Delete(false); |
| 152 } |
| 153 } |
| 154 |
| 155 void MainMenu::TabContentsDelegateImpl::OpenURLFromTab( |
| 156 TabContents* source, |
| 157 const GURL& url, |
| 158 const GURL& referrer, |
| 159 WindowOpenDisposition disposition, |
| 160 PageTransition::Type transition) { |
| 161 menu_->browser_->OpenURL(url, referrer, NEW_FOREGROUND_TAB, |
| 162 PageTransition::LINK); |
| 163 menu_->Delete(true); |
| 164 } |
OLD | NEW |