Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/validation_message_bubble.h" | 5 #include "chrome/browser/ui/validation_message_bubble.h" |
|
sky
2013/04/30 13:53:21
newline between 5/6.
tkent
2013/05/01 03:15:02
Done.
| |
| 6 #include "chrome/browser/ui/views/validation_message_bubble_delegate.h" | |
| 7 #include "grit/theme_resources.h" | |
| 8 #include "ui/base/resource/resource_bundle.h" | |
| 9 #include "ui/views/controls/image_view.h" | |
| 10 #include "ui/views/controls/label.h" | |
| 11 #include "ui/views/widget/widget.h" | |
| 12 | |
| 13 const int kWindowMinWidth = 64; | |
|
sky
2013/04/30 13:53:21
These belong in a namespace or static.
tkent
2013/05/01 03:15:02
Made them static.
BTW, why is it necessary? "const
| |
| 14 const int kWindowMaxWidth = 256; | |
| 15 const int kPadding = 0; | |
| 16 const int kIconTextMargin = 8; | |
| 17 const int kTextVerticalMargin = 4; | |
| 18 | |
| 19 ValidationMessageBubbleDelegate::ValidationMessageBubbleDelegate( | |
|
sky
2013/04/30 13:53:21
Why is the ValidateMessageBubbleDelegate implement
tkent
2013/05/01 03:15:02
Moved ValidationMessageBubbleDelegate functions to
| |
| 20 const gfx::Rect& anchor_in_screen, | |
| 21 const string16& main_text, | |
| 22 const string16& sub_text) { | |
| 23 set_use_focusless(true); | |
| 24 set_arrow(views::BubbleBorder::TOP_LEFT); | |
| 25 set_anchor_rect(anchor_in_screen); | |
| 26 | |
| 27 ResourceBundle& bundle = ResourceBundle::GetSharedInstance(); | |
| 28 views::ImageView* icon = new views::ImageView(); | |
| 29 icon->SetImage(*bundle.GetImageSkiaNamed(IDR_INPUT_ALERT)); | |
| 30 gfx::Size size = icon->GetPreferredSize(); | |
| 31 icon->SetBounds(kPadding, kPadding, size.width(), size.height()); | |
| 32 AddChildView(icon); | |
| 33 | |
| 34 views::Label* label = new views::Label(main_text); | |
| 35 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 36 label->SetFont(bundle.GetFont(ResourceBundle::MediumFont)); | |
| 37 label->set_directionality_mode(views::Label::AUTO_DETECT_DIRECTIONALITY); | |
| 38 int text_start_x = kPadding + size.width() + kIconTextMargin; | |
| 39 int min_available = kWindowMinWidth - text_start_x - kPadding; | |
| 40 int max_available = kWindowMaxWidth - text_start_x - kPadding; | |
| 41 int label_width = label->GetPreferredSize().width(); | |
| 42 label->SetMultiLine(true); | |
| 43 AddChildView(label); | |
| 44 | |
| 45 views::Label* sub_label = NULL; | |
| 46 if (!sub_text.empty()) { | |
| 47 sub_label = new views::Label(sub_text); | |
| 48 sub_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 49 sub_label->set_directionality_mode( | |
| 50 views::Label::AUTO_DETECT_DIRECTIONALITY); | |
| 51 label_width = std::max(label_width, sub_label->GetPreferredSize().width()); | |
| 52 sub_label->SetMultiLine(true); | |
| 53 AddChildView(sub_label); | |
| 54 } | |
| 55 | |
| 56 if (label_width < min_available) | |
| 57 label_width = min_available; | |
| 58 else if (label_width > max_available) | |
| 59 label_width = max_available; | |
| 60 label->SetBounds(text_start_x, kPadding, | |
| 61 label_width, label->GetHeightForWidth(label_width)); | |
| 62 int content_bottom = kPadding + label->height(); | |
| 63 | |
| 64 if (sub_label) { | |
| 65 sub_label->SetBounds(text_start_x, | |
| 66 content_bottom + kTextVerticalMargin, | |
| 67 label_width, | |
| 68 sub_label->GetHeightForWidth(label_width)); | |
| 69 content_bottom += kTextVerticalMargin + sub_label->height(); | |
| 70 } | |
| 71 | |
| 72 width_ = text_start_x + label_width + kPadding; | |
| 73 height_ = content_bottom + kPadding; | |
| 74 } | |
| 75 | |
| 76 gfx::Size ValidationMessageBubbleDelegate::GetPreferredSize() { | |
| 77 return gfx::Size(width_, height_); | |
| 78 } | |
| 79 | |
| 80 void ValidationMessageBubbleDelegate::DeleteDelegate() { | |
| 81 delete this; | |
| 82 } | |
| 83 | |
| 84 ValidationMessageBubbleDelegate::~ValidationMessageBubbleDelegate() {} | |
| 85 | |
| 86 namespace { | |
| 87 | |
| 88 class ValidationMessageBubbleImpl : public chrome::ValidationMessageBubble { | |
| 89 public: | |
| 90 ValidationMessageBubbleImpl(const gfx::Rect& anchor_in_screen, | |
| 91 const string16& main_text, | |
| 92 const string16& sub_text); | |
| 93 | |
| 94 virtual ~ValidationMessageBubbleImpl() { | |
| 95 delegate_->GetWidget()->Hide(); | |
| 96 } | |
| 97 | |
| 98 private: | |
| 99 ValidationMessageBubbleDelegate* delegate_; | |
| 100 | |
| 101 DISALLOW_COPY_AND_ASSIGN(ValidationMessageBubbleImpl); | |
| 102 }; | |
| 103 | |
| 104 // A ValidationMessageBubble implementation for Views. | |
| 105 ValidationMessageBubbleImpl::ValidationMessageBubbleImpl( | |
| 106 const gfx::Rect& anchor_in_screen, | |
| 107 const string16& main_text, | |
| 108 const string16& sub_text) { | |
| 109 // The delegate_ will kill itself in ValidationMessageBubbleDelegate:: | |
| 110 // DeleteDelegate. | |
| 111 delegate_ = new ValidationMessageBubbleDelegate( | |
| 112 anchor_in_screen, main_text, sub_text); | |
| 113 views::BubbleDelegateView::CreateBubble(delegate_); | |
| 114 delegate_->GetWidget()->Show(); | |
| 115 } | |
| 116 | |
| 117 } // namespace | |
| 6 | 118 |
| 7 namespace chrome { | 119 namespace chrome { |
| 8 | 120 |
| 9 scoped_ptr<ValidationMessageBubble> ValidationMessageBubble::CreateAndShow( | 121 scoped_ptr<ValidationMessageBubble> ValidationMessageBubble::CreateAndShow( |
| 10 content::RenderWidgetHost* widget_host, | 122 content::RenderWidgetHost*, |
| 11 const gfx::Rect& anchor_in_screen, | 123 const gfx::Rect& anchor_in_screen, |
| 12 const string16& main_text, | 124 const string16& main_text, |
| 13 const string16& sub_text) { | 125 const string16& sub_text) { |
| 14 // TODO(tkent): Implement this and enable it. crbug.com/235719. | 126 scoped_ptr<ValidationMessageBubble> bubble( |
| 15 return scoped_ptr<ValidationMessageBubble>().Pass(); | 127 new ValidationMessageBubbleImpl(anchor_in_screen, main_text, sub_text)); |
| 128 return bubble.Pass(); | |
| 16 } | 129 } |
| 17 | 130 |
| 18 } | 131 } // namespace chrome |
| OLD | NEW |