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