diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-20 23:26:02 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-20 23:26:02 +0000 |
commit | 30d0effa65df25f28ff1cc3c20754ecc58d93e27 (patch) | |
tree | 01f5fdbf49ed195d3d0987e8f3b5814cb4dfe4cf /chrome/browser/chromeos | |
parent | b37cf45c82c39ffb659c9867343375fee874b345 (diff) | |
download | chromium_src-30d0effa65df25f28ff1cc3c20754ecc58d93e27.zip chromium_src-30d0effa65df25f28ff1cc3c20754ecc58d93e27.tar.gz chromium_src-30d0effa65df25f28ff1cc3c20754ecc58d93e27.tar.bz2 |
Changes the main menu from displaying a static image to loading a page.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/208034
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26668 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos')
-rw-r--r-- | chrome/browser/chromeos/main_menu.cc | 125 | ||||
-rw-r--r-- | chrome/browser/chromeos/main_menu.h | 117 |
2 files changed, 242 insertions, 0 deletions
diff --git a/chrome/browser/chromeos/main_menu.cc b/chrome/browser/chromeos/main_menu.cc new file mode 100644 index 0000000..753c97f --- /dev/null +++ b/chrome/browser/chromeos/main_menu.cc @@ -0,0 +1,125 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/chromeos/main_menu.h" + +#include "app/resource_bundle.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/renderer_host/render_view_host.h" +#include "chrome/browser/renderer_host/render_view_host_factory.h" +#include "chrome/browser/renderer_host/render_widget_host_view.h" +#include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" +#include "chrome/browser/renderer_host/site_instance.h" +#include "chrome/browser/tab_contents/render_view_host_delegate_helper.h" +#include "grit/app_resources.h" +#include "grit/generated_resources.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "views/controls/image_view.h" +#include "views/widget/widget_gtk.h" + +static const int kRendererX = 0; +static const int kRendererY = 25; +static const int kRendererWidth = 250; +static const int kRendererHeight = 400; + +// URL of the page to load. +static const char kMenuURL[] = "http://goto.ext.google.com/crux-menu"; + +// static +void MainMenu::Show(Browser* browser) { + (new MainMenu(browser))->ShowImpl(); +} + +MainMenu::MainMenu(Browser* browser) + : browser_(browser), + popup_(NULL), + site_instance_(NULL), + menu_rvh_(NULL), + rwhv_(NULL), + child_rvh_(NULL) { +} + +MainMenu::~MainMenu() { + gdk_pointer_ungrab(GDK_CURRENT_TIME); + popup_->Close(); + menu_rvh_->Shutdown(); + if (child_rvh_) + child_rvh_->Shutdown(); +} + +void MainMenu::ShowImpl() { + SkBitmap* drop_down_image = ResourceBundle::GetSharedInstance(). + GetBitmapNamed(IDR_MAIN_MENU_BUTTON_DROP_DOWN); + views::ImageView* background_view = new views::ImageView(); + background_view->SetImage(drop_down_image); + + views::WidgetGtk* menu_popup = + new views::WidgetGtk(views::WidgetGtk::TYPE_POPUP); + popup_ = menu_popup; + // The background image has transparency, so we make the window transparent. + menu_popup->MakeTransparent(); + menu_popup->Init(NULL, gfx::Rect(0, 0, drop_down_image->width(), + drop_down_image->height())); + + menu_popup->SetContentsView(background_view); + + GURL menu_url(kMenuURL); + site_instance_ = SiteInstance::CreateSiteInstanceForURL(browser_->profile(), + menu_url); + menu_rvh_ = new RenderViewHost(site_instance_, this, MSG_ROUTING_NONE, NULL); + + rwhv_ = new RenderWidgetHostViewGtk(menu_rvh_); + rwhv_->InitAsChild(); + menu_rvh_->CreateRenderView(); + menu_popup->AddChild(rwhv_->GetNativeView()); + menu_popup->PositionChild(rwhv_->GetNativeView(), kRendererX, kRendererY, + kRendererWidth, kRendererHeight); + rwhv_->SetSize(gfx::Size(kRendererWidth, kRendererHeight)); + menu_rvh_->NavigateToURL(menu_url); + menu_popup->Show(); + + GtkWidget* rwhv_widget = rwhv_->GetNativeView(); + gtk_widget_realize(rwhv_widget); + g_signal_connect(rwhv_widget, "button-press-event", + G_CALLBACK(CallButtonPressEvent), this); + // Do a mouse grab on the renderer widget host view's widget so that we can + // close the popup if the user clicks anywhere else. + gdk_pointer_grab(rwhv_widget->window, FALSE, + static_cast<GdkEventMask>( + GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | + GDK_POINTER_MOTION_MASK), + NULL, NULL, GDK_CURRENT_TIME); +} + +gboolean MainMenu::OnButtonPressEvent(GtkWidget* widget, + GdkEventButton* event) { + if (event->x < 0 || event->y < 0 || + event->x >= widget->allocation.width || + event->y >= widget->allocation.height) { + // The user clicked outside the bounds of the menu, delete the main which + // results in closing it. + delete this; + } + return FALSE; +} + +void MainMenu::RequestOpenURL(const GURL& url, + const GURL& referrer, + WindowOpenDisposition disposition) { + browser_->OpenURL(url, referrer, NEW_FOREGROUND_TAB, PageTransition::LINK); + delete this; +} + +void MainMenu::CreateNewWindow(int route_id, + base::WaitableEvent* modal_dialog_event) { + if (child_rvh_) { + // We should never get here. If we do, it indicates a child render view + // host was created and we didn't get a subsequent RequestOpenURL. + NOTREACHED(); + return; + } + DCHECK(!child_rvh_); + child_rvh_ = RenderViewHostFactory::Create( + site_instance_, this, route_id, modal_dialog_event); +} diff --git a/chrome/browser/chromeos/main_menu.h b/chrome/browser/chromeos/main_menu.h new file mode 100644 index 0000000..d475ce5 --- /dev/null +++ b/chrome/browser/chromeos/main_menu.h @@ -0,0 +1,117 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_CHROMEOS_MAIN_MENU_H_ +#define CHROME_BROWSER_CHROMEOS_MAIN_MENU_H_ + +#include <gtk/gtk.h> + +#include "chrome/browser/renderer_host/render_view_host_delegate.h" + +class Browser; +class RenderWidgetHostViewGtk; +class SiteInstance; + +namespace views { +class Widget; +} + +// MainMenu manages shoing the main menu. The menu is currently an HTML page. +// When the user clicks a link on the page a new tab is added to the current +// browser and the menu is hidden. +// +// To show the menu invoke Show. +// +// MainMenu creates a RenderViewHost and corresponding RenderWidgetHostView +// to display the html page. MainMenu acts as the RenderViewHostDelegate for +// the RenderViewHost. Additionally when the user clicks a link a new window +// is created (child_rvh_). MainMenu is set as the RenderViewHostDelegate of +// the child_rvh_ so that it can receive the request to open the url +// (RequestOpenURL). +// +// When a new url is opened, or the user clicks outsides the bounds of the +// widget the menu is closed. +// +// MainMenu manages its own lifetime. +class MainMenu : public RenderViewHostDelegate, + public RenderViewHostDelegate::View { + public: + // Shows the menu. + static void Show(Browser* browser); + + private: + explicit MainMenu(Browser* browser); + ~MainMenu(); + + void ShowImpl(); + + // Callback from button presses on the render widget host view. Clicks + // outside the widget resulting in closing the menu. + static gboolean CallButtonPressEvent(GtkWidget* widget, + GdkEventButton* event, + MainMenu* menu) { + return menu->OnButtonPressEvent(widget, event); + } + gboolean OnButtonPressEvent(GtkWidget* widget, + GdkEventButton* event); + + // RenderViewHostDelegate overrides. + virtual int GetBrowserWindowID() const { + return -1; + } + virtual ViewType::Type GetRenderViewType() const { + return ViewType::INVALID; + } + virtual RenderViewHostDelegate::View* GetViewDelegate() { + return this; + } + virtual void RequestOpenURL(const GURL& url, + const GURL& referrer, + WindowOpenDisposition disposition); + + // RenderViewHostDelegate::View overrides. + virtual void CreateNewWindow(int route_id, + base::WaitableEvent* modal_dialog_event); + virtual void CreateNewWidget(int route_id, bool activatable) {} + virtual void ShowCreatedWindow(int route_id, + WindowOpenDisposition disposition, + const gfx::Rect& initial_pos, + bool user_gesture, + const GURL& creator_url) {} + virtual void ShowCreatedWidget(int route_id, + const gfx::Rect& initial_pos) {} + virtual void ShowContextMenu(const ContextMenuParams& params) {} + virtual void StartDragging(const WebDropData& drop_data, + WebKit::WebDragOperationsMask allowed_ops) {} + virtual void UpdateDragCursor(WebKit::WebDragOperation operation) {} + virtual void GotFocus() {} + virtual void TakeFocus(bool reverse) {} + virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {} + virtual void HandleMouseEvent() {} + virtual void HandleMouseLeave() {} + virtual void UpdatePreferredWidth(int pref_width) {} + + // The currently active browser. We use this to open urls. + Browser* browser_; + + // The widget displaying the rwvh_. + views::Widget* popup_; + + // SiteInstance for the RenderViewHosts we create. + SiteInstance* site_instance_; + + // RenderViewHost for the menu. + RenderViewHost* menu_rvh_; + + // RenderWidgetHostView from the menu_rvh_. + RenderWidgetHostViewGtk* rwhv_; + + // If the user clicks an item in the menu a child RenderViewHost is opened. + // This is that child. + RenderViewHost* child_rvh_; + + DISALLOW_COPY_AND_ASSIGN(MainMenu); +}; + +#endif // CHROME_BROWSER_CHROMEOS_CHROMEOS_VERSION_LOADER_H_ |