1 // Copyright (c) 2011 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/ui/views/notifications/balloon_view_host.h" 6 7 #include "chrome/browser/notifications/balloon.h" 8 #include "content/browser/renderer_host/render_view_host.h" 9 #include "content/browser/renderer_host/render_widget_host_view.h" 10 #if defined(OS_WIN) 11 #include "chrome/browser/renderer_host/render_widget_host_view_win.h" 12 #endif 13 #if defined(OS_LINUX) 14 #if defined(TOUCH_UI) 15 #include "chrome/browser/renderer_host/render_widget_host_view_views.h" 16 #else 17 #include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" 18 #endif 19 #endif 20 #include "views/widget/widget.h" 21 #if defined(OS_WIN) 22 #include "views/widget/widget_win.h" 23 #endif 24 #if defined(OS_LINUX) 25 #include "views/widget/widget_gtk.h" 26 #endif 27 28 class BalloonViewHostView : public views::NativeViewHost { 29 public: 30 explicit BalloonViewHostView(BalloonViewHost* host) 31 : host_(host), 32 initialized_(false) { 33 } 34 35 virtual void ViewHierarchyChanged(bool is_add, 36 views::View* parent, 37 views::View* child) { 38 NativeViewHost::ViewHierarchyChanged(is_add, parent, child); 39 if (is_add && GetWidget() && !initialized_) { 40 initialized_ = true; 41 host_->Init(GetWidget()->GetNativeView()); 42 } 43 } 44 45 private: 46 // The host owns this object. 47 BalloonViewHost* host_; 48 49 bool initialized_; 50 }; 51 52 BalloonViewHost::BalloonViewHost(Balloon* balloon) 53 : BalloonHost(balloon) { 54 native_host_ = new BalloonViewHostView(this); 55 } 56 57 void BalloonViewHost::Init(gfx::NativeView parent_native_view) { 58 parent_native_view_ = parent_native_view; 59 BalloonHost::Init(); 60 } 61 62 void BalloonViewHost::InitRenderWidgetHostView() { 63 DCHECK(render_view_host_); 64 65 render_widget_host_view_ = 66 RenderWidgetHostView::CreateViewForWidget(render_view_host_); 67 68 // TODO(johnnyg): http://crbug.com/23954. Need a cross-platform solution. 69 #if defined(OS_WIN) 70 RenderWidgetHostViewWin* view_win = 71 static_cast<RenderWidgetHostViewWin*>(render_widget_host_view_); 72 73 // Create the HWND. 74 HWND hwnd = view_win->Create(parent_native_view_); 75 view_win->ShowWindow(SW_SHOW); 76 native_host_->Attach(hwnd); 77 #elif defined(OS_LINUX) 78 #if defined(TOUCH_UI) 79 RenderWidgetHostViewViews* view_views = 80 static_cast<RenderWidgetHostViewViews*>(render_widget_host_view_); 81 view_views->InitAsChild(); 82 native_host_->AttachToView(view_views); 83 #else 84 RenderWidgetHostViewGtk* view_gtk = 85 static_cast<RenderWidgetHostViewGtk*>(render_widget_host_view_); 86 view_gtk->InitAsChild(); 87 native_host_->Attach(view_gtk->native_view()); 88 #endif 89 #else 90 NOTIMPLEMENTED(); 91 #endif 92 } 93 94 RenderWidgetHostView* BalloonViewHost::render_widget_host_view() const { 95 return render_widget_host_view_; 96 } 97