Home | History | Annotate | Download | only in infobars
      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/infobars/link_infobar.h"
      6 
      7 #include "chrome/browser/tab_contents/link_infobar_delegate.h"
      8 #include "chrome/browser/ui/views/event_utils.h"
      9 
     10 // LinkInfoBarDelegate --------------------------------------------------------
     11 
     12 InfoBar* LinkInfoBarDelegate::CreateInfoBar() {
     13   return new LinkInfoBar(this);
     14 }
     15 
     16 // LinkInfoBar ----------------------------------------------------------------
     17 
     18 LinkInfoBar::LinkInfoBar(LinkInfoBarDelegate* delegate)
     19     : InfoBarView(delegate),
     20       label_1_(NULL),
     21       link_(NULL),
     22       label_2_(NULL) {
     23 }
     24 
     25 LinkInfoBar::~LinkInfoBar() {
     26 }
     27 
     28 void LinkInfoBar::Layout() {
     29   InfoBarView::Layout();
     30 
     31   // TODO(pkasting): This isn't perfect; there are points when we should elide a
     32   // view because its subsequent view will be too small to show an ellipsis.
     33   gfx::Size label_1_size = label_1_->GetPreferredSize();
     34   int available_width = EndX() - StartX();
     35   label_1_->SetBounds(StartX(), OffsetY(label_1_size),
     36       std::min(label_1_size.width(), available_width), label_1_size.height());
     37   available_width = std::max(0, available_width - label_1_size.width());
     38 
     39   gfx::Size link_size = link_->GetPreferredSize();
     40   link_->SetBounds(label_1_->bounds().right(), OffsetY(link_size),
     41       std::min(link_size.width(), available_width), link_size.height());
     42   available_width = std::max(0, available_width - link_size.width());
     43 
     44   gfx::Size label_2_size = label_2_->GetPreferredSize();
     45   label_2_->SetBounds(link_->bounds().right(), OffsetY(label_2_size),
     46       std::min(label_2_size.width(), available_width), label_2_size.height());
     47 }
     48 
     49 void LinkInfoBar::ViewHierarchyChanged(bool is_add, View* parent, View* child) {
     50   if (is_add && (child == this) && (label_1_ == NULL)) {
     51     LinkInfoBarDelegate* delegate = GetDelegate();
     52     size_t offset;
     53     string16 message_text = delegate->GetMessageTextWithOffset(&offset);
     54     DCHECK_NE(string16::npos, offset);
     55     label_1_ = CreateLabel(message_text.substr(0, offset));
     56     AddChildView(label_1_);
     57 
     58     link_ = CreateLink(delegate->GetLinkText(), this,
     59                        background()->get_color());
     60     AddChildView(link_);
     61 
     62     label_2_ = CreateLabel(message_text.substr(offset));
     63     AddChildView(label_2_);
     64   }
     65 
     66   // This must happen after adding all other children so InfoBarView can ensure
     67   // the close button is the last child.
     68   InfoBarView::ViewHierarchyChanged(is_add, parent, child);
     69 }
     70 
     71 void LinkInfoBar::LinkActivated(views::Link* source, int event_flags) {
     72   DCHECK(link_ != NULL);
     73   DCHECK_EQ(link_, source);
     74   if (GetDelegate()->LinkClicked(
     75       event_utils::DispositionFromEventFlags(event_flags)))
     76     RemoveInfoBar();
     77 }
     78 
     79 LinkInfoBarDelegate* LinkInfoBar::GetDelegate() {
     80   return delegate()->AsLinkInfoBarDelegate();
     81 }
     82