summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/extensions/media_galleries_dialog_views.cc
blob: 6e2d68c488648e6b1bcd42b0286f196077583f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/extensions/media_galleries_dialog_views.h"

#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/browser/ui/views/constrained_window_views.h"
#include "chrome/common/chrome_switches.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/text_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/view.h"

namespace chrome {

typedef MediaGalleriesDialogController::KnownGalleryPermissions
    GalleryPermissions;

namespace {

// Heading font size correction.
const int kHeadingFontSizeDelta = 1;

// A border with zero left inset.
class MediaGalleriesCheckboxBorder : public views::TextButtonNativeThemeBorder {
 public:
  explicit MediaGalleriesCheckboxBorder(views::NativeThemeDelegate* delegate)
      : views::TextButtonNativeThemeBorder(delegate) {}
  virtual ~MediaGalleriesCheckboxBorder() {}

  virtual void GetInsets(gfx::Insets* insets) const OVERRIDE {
    views::TextButtonNativeThemeBorder::GetInsets(insets);
    insets->Set(insets->top(), 0, insets->bottom(), insets->right());
  }
};

}  // namespace

MediaGalleriesDialogViews::MediaGalleriesDialogViews(
    MediaGalleriesDialogController* controller)
    : controller_(controller),
      window_(NULL),
      contents_(new views::View()),
      checkbox_container_(NULL),
      add_gallery_container_(NULL),
      confirm_available_(false),
      accepted_(false),
      // Enable this once chrome style constrained windows work on shell
      // windows. http://crbug.com/156694
      enable_chrome_style_(UseChromeStyleDialogs()) {
  InitChildViews();

  // Ownership of |contents_| is handed off by this call. |window_| will take
  // care of deleting itself after calling DeleteDelegate().
  window_ = new ConstrainedWindowViews(
      controller->web_contents(), this,
      enable_chrome_style_,
      ConstrainedWindowViews::DEFAULT_INSETS);
}

MediaGalleriesDialogViews::~MediaGalleriesDialogViews() {}

void MediaGalleriesDialogViews::InitChildViews() {
  // Layout.
  views::GridLayout* layout = new views::GridLayout(contents_);
  if (!enable_chrome_style_) {
    layout->SetInsets(views::kPanelVertMargin, views::kPanelHorizMargin,
                      0, views::kPanelHorizMargin);
  }
  int column_set_id = 0;
  views::ColumnSet* columns = layout->AddColumnSet(column_set_id);
  columns->AddColumn(views::GridLayout::LEADING,
                     views::GridLayout::LEADING,
                     1,
                     views::GridLayout::FIXED,
                     views::Widget::GetLocalizedContentsWidth(
                         IDS_MEDIA_GALLERIES_DIALOG_CONTENT_WIDTH_CHARS),
                     0);
  contents_->SetLayoutManager(layout);

  // Header text.
  if (!enable_chrome_style_) {
    views::Label* header = new views::Label(controller_->GetHeader());
    header->SetFont(header->font().DeriveFont(kHeadingFontSizeDelta,
                                              gfx::Font::BOLD));
    header->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
    layout->StartRow(0, column_set_id);
    layout->AddView(header);
  }

  // Message text.
  views::Label* subtext = new views::Label(controller_->GetSubtext());
  subtext->SetMultiLine(true);
  subtext->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  layout->StartRowWithPadding(0, column_set_id,
                              0, views::kRelatedControlVerticalSpacing);
  layout->AddView(subtext);

  // Checkboxes.
  checkbox_container_ = new views::View();
  checkbox_container_->SetLayoutManager(
      new views::BoxLayout(views::BoxLayout::kVertical,
                           0, 0,
                           views::kRelatedControlSmallVerticalSpacing));
  layout->StartRowWithPadding(0, column_set_id,
                              0, views::kRelatedControlVerticalSpacing);
  layout->AddView(checkbox_container_);

  const GalleryPermissions& permissions = controller_->permissions();
  for (GalleryPermissions::const_iterator iter = permissions.begin();
       iter != permissions.end(); ++iter) {
    AddOrUpdateGallery(&iter->second.pref_info, iter->second.allowed);
  }
  confirm_available_ = controller_->HasPermittedGalleries();

  layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
}

void MediaGalleriesDialogViews::UpdateGallery(
    const MediaGalleryPrefInfo* gallery,
    bool permitted) {
  // After adding a new checkbox, we have to update the size of the dialog.
  if (AddOrUpdateGallery(gallery, permitted))
    GetWidget()->SetSize(GetWidget()->non_client_view()->GetPreferredSize());
}

bool MediaGalleriesDialogViews::AddOrUpdateGallery(
    const MediaGalleryPrefInfo* gallery,
    bool permitted) {
  CheckboxMap::iterator iter = checkbox_map_.find(gallery);
  if (iter != checkbox_map_.end()) {
    iter->second->SetChecked(permitted);
    return false;
  }

  views::Checkbox* checkbox = new views::Checkbox(gallery->display_name);
  checkbox->set_border(new MediaGalleriesCheckboxBorder(checkbox));
  checkbox->set_listener(this);
  checkbox->SetTooltipText(gallery->AbsolutePath().LossyDisplayName());
  checkbox_container_->AddChildView(checkbox);
  checkbox->SetChecked(permitted);
  checkbox_map_[gallery] = checkbox;

  return true;
}

string16 MediaGalleriesDialogViews::GetWindowTitle() const {
  return controller_->GetHeader();
}

bool MediaGalleriesDialogViews::ShouldShowWindowTitle() const {
  return enable_chrome_style_;
}

void MediaGalleriesDialogViews::DeleteDelegate() {
  controller_->DialogFinished(accepted_);
}

views::Widget* MediaGalleriesDialogViews::GetWidget() {
  return contents_->GetWidget();
}

const views::Widget* MediaGalleriesDialogViews::GetWidget() const {
  return contents_->GetWidget();
}

views::View* MediaGalleriesDialogViews::GetContentsView() {
  return contents_;
}

string16 MediaGalleriesDialogViews::GetDialogButtonLabel(
    ui::DialogButton button) const {
  return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK ?
      IDS_MEDIA_GALLERIES_DIALOG_CONFIRM :
      IDS_MEDIA_GALLERIES_DIALOG_CANCEL);
}

bool MediaGalleriesDialogViews::IsDialogButtonEnabled(
    ui::DialogButton button) const {
  return button != ui::DIALOG_BUTTON_OK || confirm_available_;
}

bool MediaGalleriesDialogViews::UseChromeStyle() const {
  return enable_chrome_style_;
}

views::View* MediaGalleriesDialogViews::GetExtraView() {
  if (!add_gallery_container_) {
    views::View* button = new views::NativeTextButton(this,
        l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY));
    add_gallery_container_ = new views::View();
    add_gallery_container_->SetLayoutManager(
        new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
    add_gallery_container_->AddChildView(button);
  }

  return add_gallery_container_;
}

bool MediaGalleriesDialogViews::Cancel() {
  return true;
}

bool MediaGalleriesDialogViews::Accept() {
  accepted_ = true;
  return true;
}

void MediaGalleriesDialogViews::ButtonPressed(views::Button* sender,
                                              const ui::Event& event) {
  confirm_available_ = true;
  GetWidget()->client_view()->AsDialogClientView()->UpdateDialogButtons();

  if (sender->parent() == add_gallery_container_) {
    controller_->OnAddFolderClicked();
    return;
  }

  for (CheckboxMap::iterator iter = checkbox_map_.begin();
       iter != checkbox_map_.end(); ++iter) {
    if (sender == iter->second) {
      controller_->DidToggleGallery(
          iter->first, static_cast<views::Checkbox*>(sender)->checked());
      return;
    }
  }

  NOTREACHED();
}

// MediaGalleriesDialogViewsController -----------------------------------------

// static
MediaGalleriesDialog* MediaGalleriesDialog::Create(
    MediaGalleriesDialogController* controller) {
  return new MediaGalleriesDialogViews(controller);
}

}  // namespace chrome