| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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/views/download_item_view.h" | 5 #include "chrome/browser/views/download_item_view.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 drop_down_state_(NORMAL), | 66 drop_down_state_(NORMAL), |
| 67 drop_down_pressed_(false), | 67 drop_down_pressed_(false), |
| 68 status_text_(l10n_util::GetString(IDS_DOWNLOAD_STATUS_STARTING)), | 68 status_text_(l10n_util::GetString(IDS_DOWNLOAD_STATUS_STARTING)), |
| 69 show_status_text_(true), | 69 show_status_text_(true), |
| 70 dragging_(false), | 70 dragging_(false), |
| 71 starting_drag_(false), | 71 starting_drag_(false), |
| 72 warning_icon_(NULL), | 72 warning_icon_(NULL), |
| 73 save_button_(NULL), | 73 save_button_(NULL), |
| 74 discard_button_(NULL), | 74 discard_button_(NULL), |
| 75 dangerous_download_label_(NULL), | 75 dangerous_download_label_(NULL), |
| 76 dangerous_download_label_sized_(false), | 76 dangerous_download_label_sized_(false) { |
| 77 cached_button_size_(0, 0) { | |
| 78 // TODO(idana) Bug# 1163334 | 77 // TODO(idana) Bug# 1163334 |
| 79 // | 78 // |
| 80 // We currently do not mirror each download item on the download shelf (even | 79 // We currently do not mirror each download item on the download shelf (even |
| 81 // though the download shelf itself is mirrored and the items appear from | 80 // though the download shelf itself is mirrored and the items appear from |
| 82 // right to left on RTL UIs). | 81 // right to left on RTL UIs). |
| 83 // | 82 // |
| 84 // We explicitly disable mirroring for the item because the code that draws | 83 // We explicitly disable mirroring for the item because the code that draws |
| 85 // the download progress animation relies on the View's UI layout setting | 84 // the download progress animation relies on the View's UI layout setting |
| 86 // when positioning the animation so we should make sure that code doesn't | 85 // when positioning the animation so we should make sure that code doesn't |
| 87 // treat our View as a mirrored View. | 86 // treat our View as a mirrored View. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 kVerticalTextPadding + font_.height(), | 170 kVerticalTextPadding + font_.height(), |
| 172 2 * kVerticalPadding + | 171 2 * kVerticalPadding + |
| 173 normal_body_image_set_.top_left->height() + | 172 normal_body_image_set_.top_left->height() + |
| 174 normal_body_image_set_.bottom_left->height()); | 173 normal_body_image_set_.bottom_left->height()); |
| 175 | 174 |
| 176 if (download_util::kSmallProgressIconSize > box_height_) | 175 if (download_util::kSmallProgressIconSize > box_height_) |
| 177 box_y_ = (download_util::kSmallProgressIconSize - box_height_) / 2; | 176 box_y_ = (download_util::kSmallProgressIconSize - box_height_) / 2; |
| 178 else | 177 else |
| 179 box_y_ = kVerticalPadding; | 178 box_y_ = kVerticalPadding; |
| 180 | 179 |
| 181 CSize size; | 180 gfx::Size size = GetPreferredSize(); |
| 182 GetPreferredSize(&size); | 181 drop_down_x_ = size.width() - normal_drop_down_image_set_.top->width(); |
| 183 drop_down_x_ = size.cx - normal_drop_down_image_set_.top->width(); | |
| 184 | 182 |
| 185 body_hover_animation_.reset(new SlideAnimation(this)); | 183 body_hover_animation_.reset(new SlideAnimation(this)); |
| 186 drop_hover_animation_.reset(new SlideAnimation(this)); | 184 drop_hover_animation_.reset(new SlideAnimation(this)); |
| 187 | 185 |
| 188 if (download->safety_state() == DownloadItem::DANGEROUS) { | 186 if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 189 body_state_ = DANGEROUS; | 187 body_state_ = DANGEROUS; |
| 190 drop_down_state_ = DANGEROUS; | 188 drop_down_state_ = DANGEROUS; |
| 191 | 189 |
| 192 warning_icon_ = rb.GetBitmapNamed(IDR_WARNING); | 190 warning_icon_ = rb.GetBitmapNamed(IDR_WARNING); |
| 193 save_button_ = new ChromeViews::NativeButton( | 191 save_button_ = new ChromeViews::NativeButton( |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 // In dangerous mode we have to layout our buttons. | 294 // In dangerous mode we have to layout our buttons. |
| 297 void DownloadItemView::Layout() { | 295 void DownloadItemView::Layout() { |
| 298 if (IsDangerousMode()) { | 296 if (IsDangerousMode()) { |
| 299 SizeLabelToMinWidth(); | 297 SizeLabelToMinWidth(); |
| 300 int x = kLeftPadding + dangerous_mode_body_image_set_.top_left->width() + | 298 int x = kLeftPadding + dangerous_mode_body_image_set_.top_left->width() + |
| 301 warning_icon_->width() + kLabelPadding; | 299 warning_icon_->width() + kLabelPadding; |
| 302 int y = (height() - dangerous_download_label_->height()) / 2; | 300 int y = (height() - dangerous_download_label_->height()) / 2; |
| 303 dangerous_download_label_->SetBounds(x, y, | 301 dangerous_download_label_->SetBounds(x, y, |
| 304 dangerous_download_label_->width(), | 302 dangerous_download_label_->width(), |
| 305 dangerous_download_label_->height()); | 303 dangerous_download_label_->height()); |
| 306 CSize button_size; | 304 gfx::Size button_size = GetButtonSize(); |
| 307 GetButtonSize(&button_size); | |
| 308 x += dangerous_download_label_->width() + kLabelPadding; | 305 x += dangerous_download_label_->width() + kLabelPadding; |
| 309 y = (height() - button_size.cy) / 2; | 306 y = (height() - button_size.height()) / 2; |
| 310 save_button_->SetBounds(x, y, button_size.cx, button_size.cy); | 307 save_button_->SetBounds(x, y, button_size.width(), button_size.height()); |
| 311 x += button_size.cx + kButtonPadding; | 308 x += button_size.width() + kButtonPadding; |
| 312 discard_button_->SetBounds(x, y, button_size.cx, button_size.cy); | 309 discard_button_->SetBounds(x, y, button_size.width(), |
| 310 button_size.height()); |
| 313 } | 311 } |
| 314 } | 312 } |
| 315 | 313 |
| 316 void DownloadItemView::DidChangeBounds(const CRect& previous, | 314 void DownloadItemView::DidChangeBounds(const CRect& previous, |
| 317 const CRect& current) { | 315 const CRect& current) { |
| 318 Layout(); | 316 Layout(); |
| 319 } | 317 } |
| 320 | 318 |
| 321 void DownloadItemView::ButtonPressed(ChromeViews::NativeButton* sender) { | 319 void DownloadItemView::ButtonPressed(ChromeViews::NativeButton* sender) { |
| 322 if (sender == discard_button_) { | 320 if (sender == discard_button_) { |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 dangerous_download_label_ = NULL; | 541 dangerous_download_label_ = NULL; |
| 544 | 542 |
| 545 // We need to load the icon now that the download_ has the real path. | 543 // We need to load the icon now that the download_ has the real path. |
| 546 LoadIcon(); | 544 LoadIcon(); |
| 547 | 545 |
| 548 // Force the shelf to layout again as our size has changed. | 546 // Force the shelf to layout again as our size has changed. |
| 549 parent_->Layout(); | 547 parent_->Layout(); |
| 550 parent_->SchedulePaint(); | 548 parent_->SchedulePaint(); |
| 551 } | 549 } |
| 552 | 550 |
| 553 void DownloadItemView::GetPreferredSize(CSize* out) { | 551 gfx::Size DownloadItemView::GetPreferredSize() { |
| 554 int width, height; | 552 int width, height; |
| 555 if (IsDangerousMode()) { | 553 if (IsDangerousMode()) { |
| 556 width = kLeftPadding + dangerous_mode_body_image_set_.top_left->width(); | 554 width = kLeftPadding + dangerous_mode_body_image_set_.top_left->width(); |
| 557 width += warning_icon_->width() + kLabelPadding; | 555 width += warning_icon_->width() + kLabelPadding; |
| 558 width += dangerous_download_label_->width() + kLabelPadding; | 556 width += dangerous_download_label_->width() + kLabelPadding; |
| 559 CSize button_size; | 557 gfx::Size button_size = GetButtonSize(); |
| 560 GetButtonSize(&button_size); | 558 width += button_size.width() * 2 + kButtonPadding; |
| 561 width += button_size.cx * 2 + kButtonPadding; | |
| 562 width += dangerous_mode_body_image_set_.top_right->width(); | 559 width += dangerous_mode_body_image_set_.top_right->width(); |
| 563 height = std::max<int>(2 * kVerticalPadding + 2 * font_.height() + | 560 height = std::max<int>(2 * kVerticalPadding + 2 * font_.height() + |
| 564 kVerticalTextPadding, | 561 kVerticalTextPadding, |
| 565 2 * kVerticalPadding + warning_icon_->height()); | 562 2 * kVerticalPadding + warning_icon_->height()); |
| 566 height = std::max<int>(height, 2 * kVerticalPadding + button_size.cy); | 563 height = std::max<int>(height, |
| 564 2 * kVerticalPadding + button_size.height()); |
| 567 } else { | 565 } else { |
| 568 width = kLeftPadding + normal_body_image_set_.top_left->width(); | 566 width = kLeftPadding + normal_body_image_set_.top_left->width(); |
| 569 width += download_util::kSmallProgressIconSize; | 567 width += download_util::kSmallProgressIconSize; |
| 570 width += kTextWidth; | 568 width += kTextWidth; |
| 571 width += normal_body_image_set_.top_right->width(); | 569 width += normal_body_image_set_.top_right->width(); |
| 572 width += normal_drop_down_image_set_.top->width(); | 570 width += normal_drop_down_image_set_.top->width(); |
| 573 height = std::max<int>(2 * kVerticalPadding + 2 * font_.height() + | 571 height = std::max<int>(2 * kVerticalPadding + 2 * font_.height() + |
| 574 kVerticalTextPadding, | 572 kVerticalTextPadding, |
| 575 download_util::kSmallProgressIconSize); | 573 download_util::kSmallProgressIconSize); |
| 576 } | 574 } |
| 577 out->cx = width; | 575 return gfx::Size(width, height); |
| 578 out->cy = height; | |
| 579 } | 576 } |
| 580 | 577 |
| 581 void DownloadItemView::OnMouseExited(const ChromeViews::MouseEvent& event) { | 578 void DownloadItemView::OnMouseExited(const ChromeViews::MouseEvent& event) { |
| 582 // Mouse should not activate us in dangerous mode. | 579 // Mouse should not activate us in dangerous mode. |
| 583 if (IsDangerousMode()) | 580 if (IsDangerousMode()) |
| 584 return; | 581 return; |
| 585 | 582 |
| 586 SetState(NORMAL, drop_down_pressed_ ? PUSHED : NORMAL); | 583 SetState(NORMAL, drop_down_pressed_ ? PUSHED : NORMAL); |
| 587 body_hover_animation_->Hide(); | 584 body_hover_animation_->Hide(); |
| 588 drop_hover_animation_->Hide(); | 585 drop_hover_animation_->Hide(); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 GetParent()->SchedulePaint(); | 718 GetParent()->SchedulePaint(); |
| 722 } | 719 } |
| 723 | 720 |
| 724 void DownloadItemView::LoadIcon() { | 721 void DownloadItemView::LoadIcon() { |
| 725 IconManager* im = g_browser_process->icon_manager(); | 722 IconManager* im = g_browser_process->icon_manager(); |
| 726 im->LoadIcon(download_->full_path(), IconLoader::SMALL, | 723 im->LoadIcon(download_->full_path(), IconLoader::SMALL, |
| 727 &icon_consumer_, | 724 &icon_consumer_, |
| 728 NewCallback(this, &DownloadItemView::OnExtractIconComplete)); | 725 NewCallback(this, &DownloadItemView::OnExtractIconComplete)); |
| 729 } | 726 } |
| 730 | 727 |
| 731 void DownloadItemView::GetButtonSize(CSize* size) { | 728 gfx::Size DownloadItemView::GetButtonSize() { |
| 732 DCHECK(save_button_ && discard_button_); | 729 DCHECK(save_button_ && discard_button_); |
| 730 gfx::Size size; |
| 731 |
| 733 // We cache the size when successfully retrieved, not for performance reasons | 732 // We cache the size when successfully retrieved, not for performance reasons |
| 734 // but because if this DownloadItemView is being animated while the tab is | 733 // but because if this DownloadItemView is being animated while the tab is |
| 735 // not showing, the native buttons are not parented and their preferred size | 734 // not showing, the native buttons are not parented and their preferred size |
| 736 // is 0, messing-up the layout. | 735 // is 0, messing-up the layout. |
| 737 if (cached_button_size_.cx != 0) { | 736 if (cached_button_size_.width() != 0) |
| 738 *size = cached_button_size_; | 737 size = cached_button_size_; |
| 739 } | |
| 740 | 738 |
| 741 CSize tmp_size; | 739 size = save_button_->GetMinimumSize(); |
| 742 save_button_->GetMinimumSize(size); | 740 gfx::Size discard_size = discard_button_->GetMinimumSize(); |
| 743 discard_button_->GetMinimumSize(&tmp_size); | |
| 744 | 741 |
| 745 size->cx = std::max(size->cx, tmp_size.cx); | 742 size.SetSize(std::max(size.width(), discard_size.width()), |
| 746 size->cy = std::max(size->cy, tmp_size.cy); | 743 std::max(size.height(), discard_size.height())); |
| 747 | 744 |
| 748 if (size->cx != 0) { | 745 if (size.width() != 0) |
| 749 cached_button_size_.cx = size->cx; | 746 cached_button_size_ = size; |
| 750 cached_button_size_.cy = size->cy; | 747 |
| 751 } | 748 return size; |
| 752 } | 749 } |
| 753 | 750 |
| 754 // This method computes the miminum width of the label for diplaying its text | 751 // This method computes the miminum width of the label for diplaying its text |
| 755 // on 2 lines. It just breaks the string in 2 lines on the spaces and keeps the | 752 // on 2 lines. It just breaks the string in 2 lines on the spaces and keeps the |
| 756 // configuration with minimum width. | 753 // configuration with minimum width. |
| 757 void DownloadItemView::SizeLabelToMinWidth() { | 754 void DownloadItemView::SizeLabelToMinWidth() { |
| 758 if (dangerous_download_label_sized_) | 755 if (dangerous_download_label_sized_) |
| 759 return; | 756 return; |
| 760 | 757 |
| 761 std::wstring text = dangerous_download_label_->GetText(); | 758 std::wstring text = dangerous_download_label_->GetText(); |
| 762 TrimWhitespace(text, TRIM_ALL, &text); | 759 TrimWhitespace(text, TRIM_ALL, &text); |
| 763 DCHECK_EQ(std::wstring::npos, text.find(L"\n")); | 760 DCHECK_EQ(std::wstring::npos, text.find(L"\n")); |
| 764 | 761 |
| 765 // Make the label big so that GetPreferredSize() is not constrained by the | 762 // Make the label big so that GetPreferredSize() is not constrained by the |
| 766 // current width. | 763 // current width. |
| 767 dangerous_download_label_->SetBounds(0, 0, 1000, 1000); | 764 dangerous_download_label_->SetBounds(0, 0, 1000, 1000); |
| 768 | 765 |
| 769 CSize size(0, 0); | 766 gfx::Size size; |
| 770 int min_width = -1; | 767 int min_width = -1; |
| 771 int sp_index = text.find(L" "); | 768 int sp_index = text.find(L" "); |
| 772 while (sp_index != std::wstring::npos) { | 769 while (sp_index != std::wstring::npos) { |
| 773 text.replace(sp_index, 1, L"\n"); | 770 text.replace(sp_index, 1, L"\n"); |
| 774 dangerous_download_label_->SetText(text); | 771 dangerous_download_label_->SetText(text); |
| 775 dangerous_download_label_->GetPreferredSize(&size); | 772 size = dangerous_download_label_->GetPreferredSize(); |
| 776 | 773 |
| 777 if (min_width == -1) | 774 if (min_width == -1) |
| 778 min_width = size.cx; | 775 min_width = size.width(); |
| 779 | 776 |
| 780 // If thw width is growing again, it means we passed the optimal width spot. | 777 // If the width is growing again, it means we passed the optimal width spot. |
| 781 if (size.cx > min_width) | 778 if (size.width() > min_width) |
| 782 break; | 779 break; |
| 783 else | 780 else |
| 784 min_width = size.cx; | 781 min_width = size.width(); |
| 785 | 782 |
| 786 // Restore the string. | 783 // Restore the string. |
| 787 text.replace(sp_index, 1, L" "); | 784 text.replace(sp_index, 1, L" "); |
| 788 | 785 |
| 789 sp_index = text.find(L" ", sp_index + 1); | 786 sp_index = text.find(L" ", sp_index + 1); |
| 790 } | 787 } |
| 791 | 788 |
| 792 // If we have a line with no space, we won't cut it. | 789 // If we have a line with no space, we won't cut it. |
| 793 if (min_width == -1) | 790 if (min_width == -1) |
| 794 dangerous_download_label_->GetPreferredSize(&size); | 791 size = dangerous_download_label_->GetPreferredSize(); |
| 795 | 792 |
| 796 dangerous_download_label_->SetBounds(0, 0, size.cx, size.cy); | 793 dangerous_download_label_->SetBounds(gfx::Point(), size); |
| 797 dangerous_download_label_sized_ = true; | 794 dangerous_download_label_sized_ = true; |
| 798 } | 795 } |
| OLD | NEW |