OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/extensions/media_galleries_dialog_views.h" |
| 6 |
| 7 #include "chrome/browser/ui/views/constrained_window_views.h" |
| 8 #include "grit/generated_resources.h" |
| 9 #include "ui/base/l10n/l10n_util.h" |
| 10 #include "ui/views/controls/button/checkbox.h" |
| 11 #include "ui/views/controls/button/text_button.h" |
| 12 #include "ui/views/controls/label.h" |
| 13 #include "ui/views/layout/box_layout.h" |
| 14 #include "ui/views/layout/grid_layout.h" |
| 15 #include "ui/views/layout/layout_constants.h" |
| 16 #include "ui/views/view.h" |
| 17 |
| 18 namespace chrome { |
| 19 |
| 20 typedef MediaGalleriesDialogController::KnownGalleryPermissions |
| 21 GalleryPermissions; |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Heading font size correction. |
| 26 #if defined(CROS_FONTS_USING_BCI) |
| 27 const int kHeadingFontSizeDelta = 0; |
| 28 #else |
| 29 const int kHeadingFontSizeDelta = 1; |
| 30 #endif |
| 31 |
| 32 const int kContentWidth = 450; |
| 33 |
| 34 } // namespace |
| 35 |
| 36 MediaGalleriesDialogViews::MediaGalleriesDialogViews( |
| 37 MediaGalleriesDialogController* controller) |
| 38 : controller_(controller), |
| 39 window_(NULL), |
| 40 contents_(new views::View()), |
| 41 checkbox_container_(NULL), |
| 42 add_gallery_(NULL), |
| 43 confirm_available_(false), |
| 44 accepted_(false) { |
| 45 InitChildViews(); |
| 46 |
| 47 // Ownership of |contents_| is handed off by this call. |window_| will take |
| 48 // care of deleting itself after calling DeleteDelegate(). |
| 49 window_ = new ConstrainedWindowViews(controller->tab_contents(), this); |
| 50 } |
| 51 |
| 52 MediaGalleriesDialogViews::~MediaGalleriesDialogViews() {} |
| 53 |
| 54 void MediaGalleriesDialogViews::InitChildViews() { |
| 55 // Layout. |
| 56 views::GridLayout* layout = new views::GridLayout(contents_); |
| 57 layout->SetInsets(views::kPanelVertMargin, views::kPanelHorizMargin, |
| 58 0, views::kPanelHorizMargin); |
| 59 int column_set_id = 0; |
| 60 views::ColumnSet* columns = layout->AddColumnSet(column_set_id); |
| 61 columns->AddColumn(views::GridLayout::LEADING, |
| 62 views::GridLayout::LEADING, |
| 63 1, |
| 64 views::GridLayout::FIXED, |
| 65 kContentWidth, |
| 66 0); |
| 67 contents_->SetLayoutManager(layout); |
| 68 |
| 69 // Header text. |
| 70 views::Label* header = new views::Label(controller_->GetHeader()); |
| 71 header->SetFont(header->font().DeriveFont(kHeadingFontSizeDelta, |
| 72 gfx::Font::BOLD)); |
| 73 header->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 74 layout->StartRow(0, column_set_id); |
| 75 layout->AddView(header); |
| 76 |
| 77 // Message text. |
| 78 views::Label* subtext = new views::Label(controller_->GetSubtext()); |
| 79 subtext->SetMultiLine(true); |
| 80 subtext->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 81 layout->StartRowWithPadding(0, column_set_id, |
| 82 0, views::kRelatedControlVerticalSpacing); |
| 83 layout->AddView(subtext); |
| 84 |
| 85 // Checkboxes. |
| 86 checkbox_container_ = new views::View(); |
| 87 checkbox_container_->SetLayoutManager( |
| 88 new views::BoxLayout(views::BoxLayout::kVertical, |
| 89 views::kPanelHorizIndentation, |
| 90 0, |
| 91 views::kRelatedControlSmallVerticalSpacing)); |
| 92 layout->StartRowWithPadding(0, column_set_id, |
| 93 0, views::kRelatedControlVerticalSpacing); |
| 94 layout->AddView(checkbox_container_); |
| 95 |
| 96 const GalleryPermissions& permissions = controller_->permissions(); |
| 97 for (GalleryPermissions::const_iterator iter = permissions.begin(); |
| 98 iter != permissions.end(); ++iter) { |
| 99 AddOrUpdateGallery(&iter->second.pref_info, iter->second.allowed); |
| 100 if (iter->second.allowed) |
| 101 confirm_available_ = true; |
| 102 } |
| 103 |
| 104 // Add Gallery button. |
| 105 add_gallery_ = new views::NativeTextButton( |
| 106 this, l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY)); |
| 107 layout->StartRowWithPadding(0, column_set_id, |
| 108 0, views::kRelatedControlVerticalSpacing); |
| 109 layout->AddView(add_gallery_); |
| 110 } |
| 111 |
| 112 void MediaGalleriesDialogViews::UpdateGallery( |
| 113 const MediaGalleryPrefInfo* gallery, |
| 114 bool permitted) { |
| 115 // After adding a new checkbox, we have to update the size of the dialog. |
| 116 if (AddOrUpdateGallery(gallery, permitted)) |
| 117 GetWidget()->SetSize(GetWidget()->non_client_view()->GetPreferredSize()); |
| 118 } |
| 119 |
| 120 bool MediaGalleriesDialogViews::AddOrUpdateGallery( |
| 121 const MediaGalleryPrefInfo* gallery, |
| 122 bool permitted) { |
| 123 CheckboxMap::iterator iter = checkbox_map_.find(gallery); |
| 124 if (iter != checkbox_map_.end()) { |
| 125 iter->second->SetChecked(permitted); |
| 126 return false; |
| 127 } |
| 128 |
| 129 views::Checkbox* checkbox = new views::Checkbox(gallery->display_name); |
| 130 checkbox->set_listener(this); |
| 131 checkbox->SetTooltipText(gallery->path.LossyDisplayName()); |
| 132 checkbox_container_->AddChildView(checkbox); |
| 133 checkbox->SetChecked(permitted); |
| 134 checkbox_map_[gallery] = checkbox; |
| 135 |
| 136 return true; |
| 137 } |
| 138 |
| 139 void MediaGalleriesDialogViews::DeleteDelegate() { |
| 140 controller_->DialogFinished(accepted_); |
| 141 } |
| 142 |
| 143 views::Widget* MediaGalleriesDialogViews::GetWidget() { |
| 144 return contents_->GetWidget(); |
| 145 } |
| 146 |
| 147 const views::Widget* MediaGalleriesDialogViews::GetWidget() const { |
| 148 return contents_->GetWidget(); |
| 149 } |
| 150 |
| 151 views::View* MediaGalleriesDialogViews::GetContentsView() { |
| 152 return contents_; |
| 153 } |
| 154 |
| 155 string16 MediaGalleriesDialogViews::GetDialogButtonLabel( |
| 156 ui::DialogButton button) const { |
| 157 return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK ? |
| 158 IDS_MEDIA_GALLERIES_DIALOG_CONFIRM : |
| 159 IDS_MEDIA_GALLERIES_DIALOG_CANCEL); |
| 160 } |
| 161 |
| 162 bool MediaGalleriesDialogViews::IsDialogButtonEnabled( |
| 163 ui::DialogButton button) const { |
| 164 return button != ui::DIALOG_BUTTON_OK || confirm_available_; |
| 165 } |
| 166 |
| 167 bool MediaGalleriesDialogViews::Cancel() { |
| 168 return true; |
| 169 } |
| 170 |
| 171 bool MediaGalleriesDialogViews::Accept() { |
| 172 accepted_ = true; |
| 173 return true; |
| 174 } |
| 175 |
| 176 void MediaGalleriesDialogViews::ButtonPressed(views::Button* sender, |
| 177 const ui::Event& event) { |
| 178 confirm_available_ = true; |
| 179 GetWidget()->client_view()->AsDialogClientView()->UpdateDialogButtons(); |
| 180 |
| 181 if (sender == add_gallery_) { |
| 182 controller_->OnAddFolderClicked(); |
| 183 return; |
| 184 } |
| 185 |
| 186 for (CheckboxMap::iterator iter = checkbox_map_.begin(); |
| 187 iter != checkbox_map_.end(); ++iter) { |
| 188 if (sender == iter->second) { |
| 189 controller_->GalleryToggled( |
| 190 iter->first, static_cast<views::Checkbox*>(sender)->checked()); |
| 191 return; |
| 192 } |
| 193 } |
| 194 |
| 195 NOTREACHED(); |
| 196 } |
| 197 |
| 198 // MediaGalleriesDialogViewsController ----------------------------------------- |
| 199 |
| 200 // static |
| 201 MediaGalleriesDialog* MediaGalleriesDialog::Create( |
| 202 MediaGalleriesDialogController* controller) { |
| 203 return new MediaGalleriesDialogViews(controller); |
| 204 } |
| 205 |
| 206 } // namespace chrome |
OLD | NEW |