OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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/views/infobars/infobar_container.h" |
| 6 |
| 7 #include "chrome/browser/infobar_delegate.h" |
| 8 #include "chrome/browser/tab_contents.h" |
| 9 #include "chrome/browser/views/frame/browser_view.h" |
| 10 #include "chrome/browser/views/infobars/infobars.h" |
| 11 #include "chrome/common/notification_types.h" |
| 12 |
| 13 // InfoBarContainer, public: --------------------------------------------------- |
| 14 |
| 15 InfoBarContainer::InfoBarContainer(BrowserView* browser_view) |
| 16 : browser_view_(browser_view), |
| 17 tab_contents_(NULL) { |
| 18 |
| 19 } |
| 20 |
| 21 InfoBarContainer::~InfoBarContainer() { |
| 22 ChangeTabContents(NULL); |
| 23 } |
| 24 |
| 25 void InfoBarContainer::ChangeTabContents(TabContents* contents) { |
| 26 if (tab_contents_) { |
| 27 NotificationService::current()->RemoveObserver( |
| 28 this, NOTIFY_TAB_CONTENTS_INFOBAR_ADDED, |
| 29 Source<TabContents>(tab_contents_)); |
| 30 NotificationService::current()->RemoveObserver( |
| 31 this, NOTIFY_TAB_CONTENTS_INFOBAR_REMOVED, |
| 32 Source<TabContents>(tab_contents_)); |
| 33 } |
| 34 tab_contents_ = contents; |
| 35 if (tab_contents_) { |
| 36 UpdateInfoBars(); |
| 37 NotificationService::current()->AddObserver( |
| 38 this, NOTIFY_TAB_CONTENTS_INFOBAR_ADDED, |
| 39 Source<TabContents>(tab_contents_)); |
| 40 NotificationService::current()->AddObserver( |
| 41 this, NOTIFY_TAB_CONTENTS_INFOBAR_REMOVED, |
| 42 Source<TabContents>(tab_contents_)); |
| 43 } |
| 44 } |
| 45 |
| 46 void InfoBarContainer::InfoBarAnimated(bool completed) { |
| 47 browser_view_->SelectedTabToolbarSizeChanged(!completed); |
| 48 } |
| 49 |
| 50 void InfoBarContainer::RemoveDelegate(InfoBarDelegate* delegate) { |
| 51 tab_contents_->RemoveInfoBar(delegate); |
| 52 } |
| 53 |
| 54 // InfoBarContainer, views::View overrides: ------------------------------------ |
| 55 |
| 56 gfx::Size InfoBarContainer::GetPreferredSize() { |
| 57 // We do not have a preferred width (we will expand to fit the available width |
| 58 // of the BrowserView). Our preferred height is the sum of the preferred |
| 59 // heights of the InfoBars contained within us. |
| 60 int height = 0; |
| 61 for (int i = 0; i < GetChildViewCount(); ++i) |
| 62 height += GetChildViewAt(i)->GetPreferredSize().height(); |
| 63 return gfx::Size(0, height); |
| 64 } |
| 65 |
| 66 void InfoBarContainer::Layout() { |
| 67 int top = 0; |
| 68 for (int i = 0; i < GetChildViewCount(); ++i) { |
| 69 views::View* child = GetChildViewAt(i); |
| 70 gfx::Size ps = child->GetPreferredSize(); |
| 71 child->SetBounds(0, top, width(), ps.height()); |
| 72 top += ps.height(); |
| 73 } |
| 74 } |
| 75 |
| 76 void InfoBarContainer::ViewHierarchyChanged(bool is_add, |
| 77 views::View* parent, |
| 78 views::View* child) { |
| 79 if (parent == this && child->GetParent() == this) { |
| 80 // An InfoBar child was added or removed. Tell the BrowserView it needs to |
| 81 // re-layout since our preferred size will have changed. |
| 82 browser_view_->SelectedTabToolbarSizeChanged(false); |
| 83 } |
| 84 } |
| 85 |
| 86 // InfoBarContainer, NotificationObserver implementation: ---------------------- |
| 87 |
| 88 void InfoBarContainer::Observe(NotificationType type, |
| 89 const NotificationSource& source, |
| 90 const NotificationDetails& details) { |
| 91 if (type == NOTIFY_TAB_CONTENTS_INFOBAR_ADDED) { |
| 92 AddInfoBar(Details<InfoBarDelegate>(details).ptr()); |
| 93 } else if (type == NOTIFY_TAB_CONTENTS_INFOBAR_REMOVED) { |
| 94 RemoveInfoBar(Details<InfoBarDelegate>(details).ptr()); |
| 95 } else { |
| 96 NOTREACHED(); |
| 97 } |
| 98 } |
| 99 |
| 100 // InfoBarContainer, private: -------------------------------------------------- |
| 101 |
| 102 void InfoBarContainer::UpdateInfoBars() { |
| 103 // Clear out all the old child views. |
| 104 RemoveAllChildViews(true); |
| 105 |
| 106 for (size_t i = 0; i < tab_contents_->infobar_delegate_count(); ++i) { |
| 107 InfoBarDelegate* delegate = tab_contents_->GetInfoBarDelegateAt(i); |
| 108 InfoBar* infobar = delegate->CreateInfoBar(); |
| 109 infobar->set_container(this); |
| 110 infobar->Open(); |
| 111 AddChildView(infobar); |
| 112 } |
| 113 } |
| 114 |
| 115 void InfoBarContainer::AddInfoBar(InfoBarDelegate* delegate) { |
| 116 InfoBar* infobar = delegate->CreateInfoBar(); |
| 117 infobar->set_container(this); |
| 118 infobar->AnimateOpen(); |
| 119 AddChildView(infobar); |
| 120 } |
| 121 |
| 122 void InfoBarContainer::RemoveInfoBar(InfoBarDelegate* delegate) { |
| 123 size_t index = 0; |
| 124 for (; index < tab_contents_->infobar_delegate_count(); ++index) { |
| 125 if (tab_contents_->GetInfoBarDelegateAt(index) == delegate) |
| 126 break; |
| 127 } |
| 128 |
| 129 // The View will be removed once the Close animation completes. |
| 130 static_cast<InfoBar*>(GetChildViewAt(index))->AnimateClose(); |
| 131 } |
OLD | NEW |