15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/infobars/translate_message_infobar.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/translate/translate_infobar_delegate.h" 8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/views/controls/button/label_button.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/label.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TranslateMessageInfoBar::TranslateMessageInfoBar( 12a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) scoped_ptr<TranslateInfoBarDelegate> delegate) 13a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) : TranslateInfoBarBase(delegate.Pass()), 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) label_(NULL), 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) button_(NULL) { 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TranslateMessageInfoBar::~TranslateMessageInfoBar() { 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslateMessageInfoBar::Layout() { 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TranslateInfoBarBase::Layout(); 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Size label_size = label_->GetPreferredSize(); 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) label_->SetBounds(StartX(), OffsetY(label_size), 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::min(label_size.width(), 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::max(0, EndX() - StartX() - ContentMinimumWidth())), 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) label_size.height()); 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (button_) { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Size button_size = button_->GetPreferredSize(); 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) button_->SetBounds(label_->bounds().right() + kButtonInLabelSpacing, 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) OffsetY(button_size), button_size.width(), button_size.height()); 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void TranslateMessageInfoBar::ViewHierarchyChanged( 38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) const ViewHierarchyChangedDetails& details) { 39a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) if (details.is_add && (details.child == this) && (label_ == NULL)) { 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TranslateInfoBarDelegate* delegate = GetDelegate(); 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) label_ = CreateLabel(delegate->GetMessageInfoBarText()); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AddChildView(label_); 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 button_text(delegate->GetMessageInfoBarButtonText()); 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!button_text.empty()) { 46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) button_ = CreateLabelButton(this, button_text, false); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AddChildView(button_); 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This must happen after adding all other children so InfoBarView can ensure 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the close button is the last child. 53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) TranslateInfoBarBase::ViewHierarchyChanged(details); 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslateMessageInfoBar::ButtonPressed(views::Button* sender, 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ui::Event& event) { 58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!owner()) 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; // We're closing; don't call anything, it might access the owner. 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (sender == button_) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetDelegate()->MessageInfoBarButtonPressed(); 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TranslateInfoBarBase::ButtonPressed(sender, event); 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TranslateMessageInfoBar::ContentMinimumWidth() const { 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return (button_ != NULL) ? 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (button_->GetPreferredSize().width() + kButtonInLabelSpacing) : 0; 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 70