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 <vector> |
| 8 |
7 #include "app/gfx/insets.h" | 9 #include "app/gfx/insets.h" |
8 #include "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
| 11 #include "base/command_line.h" |
9 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/string_util.h" |
10 #include "chrome/browser/browser.h" | 14 #include "chrome/browser/browser.h" |
11 #include "chrome/browser/renderer_host/render_view_host.h" | 15 #include "chrome/browser/renderer_host/render_view_host.h" |
12 #include "chrome/browser/renderer_host/render_view_host_factory.h" | 16 #include "chrome/browser/renderer_host/render_view_host_factory.h" |
13 #include "chrome/browser/renderer_host/render_widget_host_view.h" | 17 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
14 #include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" | 18 #include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" |
15 #include "chrome/browser/renderer_host/site_instance.h" | 19 #include "chrome/browser/renderer_host/site_instance.h" |
16 #include "chrome/browser/tab_contents/tab_contents.h" | 20 #include "chrome/browser/tab_contents/tab_contents.h" |
17 #include "chrome/browser/tab_contents/render_view_host_delegate_helper.h" | 21 #include "chrome/browser/tab_contents/render_view_host_delegate_helper.h" |
18 #include "grit/app_resources.h" | 22 #include "grit/app_resources.h" |
19 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
20 #include "third_party/skia/include/core/SkBitmap.h" | 24 #include "third_party/skia/include/core/SkBitmap.h" |
21 #include "views/background.h" | 25 #include "views/background.h" |
22 #include "views/painter.h" | 26 #include "views/painter.h" |
| 27 #include "views/screen.h" |
23 #include "views/widget/root_view.h" | 28 #include "views/widget/root_view.h" |
24 #include "views/widget/widget_gtk.h" | 29 #include "views/widget/widget_gtk.h" |
25 | 30 |
26 // Initial size of the renderer. This is contained within a window whose size | 31 // Initial size of the renderer. This is contained within a window whose size |
27 // is set to the size of the image IDR_MAIN_MENU_BUTTON_DROP_DOWN. | 32 // is set to the size of the image IDR_MAIN_MENU_BUTTON_DROP_DOWN. |
28 static const int kRendererX = 0; | 33 static const int kRendererX = 0; |
29 static const int kRendererY = 25; | 34 static const int kRendererY = 25; |
30 static const int kRendererWidth = 250; | 35 static const int kRendererWidth = 250; |
31 static const int kRendererHeight = 400; | 36 static const int kRendererHeight = 400; |
32 | 37 |
33 // Insets defining the regions that are stretched and titled to create the | 38 // Insets defining the regions that are stretched and titled to create the |
34 // background of the popup. These constants are fed into | 39 // background of the popup. These constants are fed into |
35 // Painter::CreateImagePainter as the insets, see it for details. | 40 // Painter::CreateImagePainter as the insets, see it for details. |
36 static const int kBackgroundImageTop = 27; | 41 static const int kBackgroundImageTop = 27; |
37 static const int kBackgroundImageLeft = 85; | 42 static const int kBackgroundImageLeft = 85; |
38 static const int kBackgroundImageBottom = 10; | 43 static const int kBackgroundImageBottom = 10; |
39 static const int kBackgroundImageRight = 8; | 44 static const int kBackgroundImageRight = 8; |
40 | 45 |
41 // URL of the page to load. | 46 // Command line switch for specifying url of the page. |
| 47 static const wchar_t kURLSwitch[] = L"main-menu-url"; |
| 48 |
| 49 // Command line switch for specifying the size of the main menu. The default is |
| 50 // full screen. |
| 51 static const wchar_t kMenuSizeSwitch[] = L"main-menu-size"; |
| 52 |
| 53 // URL of the page to load. This is ignored if kURLSwitch is specified. |
42 static const char kMenuURL[] = "http://goto.ext.google.com/crux-menu"; | 54 static const char kMenuURL[] = "http://goto.ext.google.com/crux-menu"; |
43 | 55 |
| 56 // Returns the size of the popup. By default the popup is sized slightly |
| 57 // larger than full screen, but can be overriden by the command line switch |
| 58 // kMenuSizeSwitch. |
| 59 static gfx::Size GetPopupSize() { |
| 60 std::wstring cl_size = |
| 61 CommandLine::ForCurrentProcess()->GetSwitchValue(kMenuSizeSwitch); |
| 62 if (!cl_size.empty()) { |
| 63 std::vector<std::string> chunks; |
| 64 SplitString(WideToUTF8(cl_size), 'x', &chunks); |
| 65 if (chunks.size() == 2) |
| 66 return gfx::Size(StringToInt(chunks[0]), StringToInt(chunks[1])); |
| 67 } |
| 68 |
| 69 gfx::Size size = |
| 70 views::Screen::GetMonitorAreaNearestPoint(gfx::Point(0, 0)).size(); |
| 71 // When full screen we don't want to see the drop shadow. Adjust the width |
| 72 // so the drop shadow ends up off screen. |
| 73 size.Enlarge(kBackgroundImageRight, kBackgroundImageBottom); |
| 74 return size; |
| 75 } |
| 76 |
| 77 // Returns the size for the renderer widget host view given the specified |
| 78 // size of the popup. |
| 79 static gfx::Size CalculateRWHVSize(const gfx::Size& popup_size) { |
| 80 return gfx::Size(popup_size.width() - kRendererX - kBackgroundImageRight, |
| 81 popup_size.height() - kRendererY - kBackgroundImageBottom); |
| 82 } |
| 83 |
| 84 // Returns the URL of the menu. |
| 85 static GURL GetMenuURL() { |
| 86 std::wstring url_string = |
| 87 CommandLine::ForCurrentProcess()->GetSwitchValue(kURLSwitch); |
| 88 if (!url_string.empty()) |
| 89 return GURL(WideToUTF8(url_string)); |
| 90 return GURL(kMenuURL); |
| 91 } |
| 92 |
44 // static | 93 // static |
45 void MainMenu::Show(Browser* browser) { | 94 void MainMenu::Show(Browser* browser) { |
46 (new MainMenu(browser))->ShowImpl(); | 95 (new MainMenu(browser))->ShowImpl(); |
47 } | 96 } |
48 | 97 |
49 MainMenu::~MainMenu() { | 98 MainMenu::~MainMenu() { |
50 popup_->Close(); | 99 popup_->Close(); |
51 menu_rvh_->Shutdown(); | 100 menu_rvh_->Shutdown(); |
52 } | 101 } |
53 | 102 |
54 MainMenu::MainMenu(Browser* browser) | 103 MainMenu::MainMenu(Browser* browser) |
55 : browser_(browser), | 104 : browser_(browser), |
56 popup_(NULL), | 105 popup_(NULL), |
57 site_instance_(NULL), | 106 site_instance_(NULL), |
58 menu_rvh_(NULL), | 107 menu_rvh_(NULL), |
59 rwhv_(NULL), | 108 rwhv_(NULL), |
60 ALLOW_THIS_IN_INITIALIZER_LIST(tab_contents_delegate_(this)) { | 109 ALLOW_THIS_IN_INITIALIZER_LIST(tab_contents_delegate_(this)) { |
61 } | 110 } |
62 | 111 |
63 void MainMenu::ShowImpl() { | 112 void MainMenu::ShowImpl() { |
64 SkBitmap* drop_down_image = ResourceBundle::GetSharedInstance(). | 113 SkBitmap* drop_down_image = ResourceBundle::GetSharedInstance(). |
65 GetBitmapNamed(IDR_MAIN_MENU_BUTTON_DROP_DOWN); | 114 GetBitmapNamed(IDR_MAIN_MENU_BUTTON_DROP_DOWN); |
66 | 115 |
67 views::WidgetGtk* menu_popup = | 116 views::WidgetGtk* menu_popup = |
68 new views::WidgetGtk(views::WidgetGtk::TYPE_POPUP); | 117 new views::WidgetGtk(views::WidgetGtk::TYPE_POPUP); |
69 popup_ = menu_popup; | 118 popup_ = menu_popup; |
70 // The background image has transparency, so we make the window transparent. | 119 // The background image has transparency, so we make the window transparent. |
71 menu_popup->MakeTransparent(); | 120 menu_popup->MakeTransparent(); |
72 menu_popup->Init(NULL, gfx::Rect(0, 0, drop_down_image->width(), | 121 gfx::Size popup_size = GetPopupSize(); |
73 drop_down_image->height())); | 122 menu_popup->Init(NULL, gfx::Rect(0, 0, popup_size.width(), |
| 123 popup_size.height())); |
74 | 124 |
75 views::Painter* painter = views::Painter::CreateImagePainter( | 125 views::Painter* painter = views::Painter::CreateImagePainter( |
76 *drop_down_image, | 126 *drop_down_image, |
77 gfx::Insets(kBackgroundImageTop, kBackgroundImageLeft, | 127 gfx::Insets(kBackgroundImageTop, kBackgroundImageLeft, |
78 kBackgroundImageBottom, kBackgroundImageRight)); | 128 kBackgroundImageBottom, kBackgroundImageRight)); |
79 menu_popup->GetRootView()->set_background( | 129 menu_popup->GetRootView()->set_background( |
80 views::Background::CreateBackgroundPainter(true, painter)); | 130 views::Background::CreateBackgroundPainter(true, painter)); |
81 | 131 |
82 GURL menu_url(kMenuURL); | 132 GURL menu_url(GetMenuURL()); |
83 site_instance_ = SiteInstance::CreateSiteInstanceForURL(browser_->profile(), | 133 site_instance_ = SiteInstance::CreateSiteInstanceForURL(browser_->profile(), |
84 menu_url); | 134 menu_url); |
85 menu_rvh_ = new RenderViewHost(site_instance_, this, MSG_ROUTING_NONE); | 135 menu_rvh_ = new RenderViewHost(site_instance_, this, MSG_ROUTING_NONE); |
86 | 136 |
87 rwhv_ = new RenderWidgetHostViewGtk(menu_rvh_); | 137 rwhv_ = new RenderWidgetHostViewGtk(menu_rvh_); |
88 rwhv_->InitAsChild(); | 138 rwhv_->InitAsChild(); |
89 menu_rvh_->CreateRenderView(); | 139 menu_rvh_->CreateRenderView(); |
90 menu_popup->AddChild(rwhv_->GetNativeView()); | 140 menu_popup->AddChild(rwhv_->GetNativeView()); |
| 141 gfx::Size rwhv_size = CalculateRWHVSize(popup_size); |
91 menu_popup->PositionChild(rwhv_->GetNativeView(), kRendererX, kRendererY, | 142 menu_popup->PositionChild(rwhv_->GetNativeView(), kRendererX, kRendererY, |
92 kRendererWidth, kRendererHeight); | 143 rwhv_size.width(), rwhv_size.height()); |
93 rwhv_->SetSize(gfx::Size(kRendererWidth, kRendererHeight)); | 144 rwhv_->SetSize(rwhv_size); |
94 menu_rvh_->NavigateToURL(menu_url); | 145 menu_rvh_->NavigateToURL(menu_url); |
95 menu_popup->Show(); | 146 menu_popup->Show(); |
96 | 147 |
97 GtkWidget* rwhv_widget = rwhv_->GetNativeView(); | 148 GtkWidget* rwhv_widget = rwhv_->GetNativeView(); |
98 gtk_widget_realize(rwhv_widget); | 149 gtk_widget_realize(rwhv_widget); |
99 g_signal_connect(rwhv_widget, "button-press-event", | 150 g_signal_connect(rwhv_widget, "button-press-event", |
100 G_CALLBACK(CallButtonPressEvent), this); | 151 G_CALLBACK(CallButtonPressEvent), this); |
101 // Do a mouse grab on the renderer widget host view's widget so that we can | 152 // Do a mouse grab on the renderer widget host view's widget so that we can |
102 // close the popup if the user clicks anywhere else. And do a keyboard | 153 // close the popup if the user clicks anywhere else. And do a keyboard |
103 // grab so that we get all key events. | 154 // grab so that we get all key events. |
(...skipping 30 matching lines...) Expand all Loading... |
134 event->x >= widget->allocation.width || | 185 event->x >= widget->allocation.width || |
135 event->y >= widget->allocation.height) { | 186 event->y >= widget->allocation.height) { |
136 // The user clicked outside the bounds of the menu, delete the main which | 187 // The user clicked outside the bounds of the menu, delete the main which |
137 // results in closing it. | 188 // results in closing it. |
138 Delete(true); | 189 Delete(true); |
139 } | 190 } |
140 return FALSE; | 191 return FALSE; |
141 } | 192 } |
142 | 193 |
143 void MainMenu::RequestMove(const gfx::Rect& new_bounds) { | 194 void MainMenu::RequestMove(const gfx::Rect& new_bounds) { |
144 SkBitmap* drop_down_image = ResourceBundle::GetSharedInstance(). | |
145 GetBitmapNamed(IDR_MAIN_MENU_BUTTON_DROP_DOWN); | |
146 int new_w = drop_down_image->width() + (new_bounds.width() - kRendererWidth); | |
147 int new_h = drop_down_image->height() + | |
148 (new_bounds.height() - kRendererHeight); | |
149 // Invoking PositionChild results in a gtk signal that triggers attempting to | 195 // Invoking PositionChild results in a gtk signal that triggers attempting to |
150 // to resize the window. We need to set the size request so that it resizes | 196 // to resize the window. We need to set the size request so that it resizes |
151 // correctly when this happens. | 197 // correctly when this happens. |
152 gtk_widget_set_size_request(popup_->GetNativeView(), new_w, new_h); | 198 gtk_widget_set_size_request(popup_->GetNativeView(), |
| 199 new_bounds.width(), new_bounds.height()); |
| 200 gfx::Size rwhv_size = CalculateRWHVSize(new_bounds.size()); |
153 popup_->PositionChild(rwhv_->GetNativeView(), kRendererX, kRendererY, | 201 popup_->PositionChild(rwhv_->GetNativeView(), kRendererX, kRendererY, |
154 new_bounds.width(), new_bounds.height()); | 202 rwhv_size.width(), rwhv_size.height()); |
155 popup_->SetBounds(gfx::Rect(new_bounds.x(), new_bounds.y(), new_w, new_h)); | 203 popup_->SetBounds(new_bounds); |
156 rwhv_->SetSize(new_bounds.size()); | 204 rwhv_->SetSize(rwhv_size); |
157 } | 205 } |
158 | 206 |
159 void MainMenu::CreateNewWindow(int route_id) { | 207 void MainMenu::CreateNewWindow(int route_id) { |
160 if (pending_contents_.get()) { | 208 if (pending_contents_.get()) { |
161 NOTREACHED(); | 209 NOTREACHED(); |
162 return; | 210 return; |
163 } | 211 } |
164 | 212 |
165 helper_.CreateNewWindow(route_id, browser_->profile(), site_instance_, | 213 helper_.CreateNewWindow(route_id, browser_->profile(), site_instance_, |
166 DOMUIFactory::GetDOMUIType(GURL(kMenuURL)), NULL); | 214 DOMUIFactory::GetDOMUIType(GURL(GetMenuURL())), |
| 215 NULL); |
167 pending_contents_.reset(helper_.GetCreatedWindow(route_id)); | 216 pending_contents_.reset(helper_.GetCreatedWindow(route_id)); |
168 pending_contents_->set_delegate(&tab_contents_delegate_); | 217 pending_contents_->set_delegate(&tab_contents_delegate_); |
169 } | 218 } |
170 | 219 |
171 void MainMenu::ShowCreatedWindow(int route_id, | 220 void MainMenu::ShowCreatedWindow(int route_id, |
172 WindowOpenDisposition disposition, | 221 WindowOpenDisposition disposition, |
173 const gfx::Rect& initial_pos, | 222 const gfx::Rect& initial_pos, |
174 bool user_gesture, | 223 bool user_gesture, |
175 const GURL& creator_url) { | 224 const GURL& creator_url) { |
176 if (disposition == NEW_POPUP) { | 225 if (disposition == NEW_POPUP) { |
177 pending_contents_->set_delegate(NULL); | 226 pending_contents_->set_delegate(NULL); |
178 browser_->GetSelectedTabContents()->AddNewContents( | 227 browser_->GetSelectedTabContents()->AddNewContents( |
179 pending_contents_.release(), disposition, initial_pos, user_gesture, | 228 pending_contents_.release(), disposition, initial_pos, user_gesture, |
180 creator_url); | 229 creator_url); |
181 Delete(false); | 230 Delete(false); |
182 } | 231 } |
183 } | 232 } |
184 | 233 |
185 void MainMenu::TabContentsDelegateImpl::OpenURLFromTab( | 234 void MainMenu::TabContentsDelegateImpl::OpenURLFromTab( |
186 TabContents* source, | 235 TabContents* source, |
187 const GURL& url, | 236 const GURL& url, |
188 const GURL& referrer, | 237 const GURL& referrer, |
189 WindowOpenDisposition disposition, | 238 WindowOpenDisposition disposition, |
190 PageTransition::Type transition) { | 239 PageTransition::Type transition) { |
191 menu_->browser_->OpenURL(url, referrer, NEW_FOREGROUND_TAB, | 240 menu_->browser_->OpenURL(url, referrer, NEW_FOREGROUND_TAB, |
192 PageTransition::LINK); | 241 PageTransition::LINK); |
193 menu_->Delete(true); | 242 menu_->Delete(true); |
194 } | 243 } |
OLD | NEW |