| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "views/controls/message_box_view.h" | |
| 6 | |
| 7 #include "base/i18n/rtl.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "ui/base/accessibility/accessible_view_state.h" | |
| 11 #include "ui/base/clipboard/clipboard.h" | |
| 12 #include "ui/base/clipboard/scoped_clipboard_writer.h" | |
| 13 #include "ui/base/message_box_flags.h" | |
| 14 #include "ui/views/controls/button/checkbox.h" | |
| 15 #include "ui/views/controls/label.h" | |
| 16 #include "ui/views/controls/textfield/textfield.h" | |
| 17 #include "ui/views/layout/grid_layout.h" | |
| 18 #include "ui/views/layout/layout_constants.h" | |
| 19 #include "ui/views/widget/widget.h" | |
| 20 #include "ui/views/window/client_view.h" | |
| 21 #include "views/controls/image_view.h" | |
| 22 #include "views/views_delegate.h" | |
| 23 | |
| 24 const int kDefaultMessageWidth = 320; | |
| 25 | |
| 26 namespace views { | |
| 27 | |
| 28 /////////////////////////////////////////////////////////////////////////////// | |
| 29 // MessageBoxView, public: | |
| 30 | |
| 31 MessageBoxView::MessageBoxView(int dialog_flags, | |
| 32 const string16& message, | |
| 33 const string16& default_prompt, | |
| 34 int message_width) | |
| 35 : message_label_(new Label(message)), | |
| 36 prompt_field_(NULL), | |
| 37 icon_(NULL), | |
| 38 checkbox_(NULL), | |
| 39 message_width_(message_width) { | |
| 40 Init(dialog_flags, default_prompt); | |
| 41 } | |
| 42 | |
| 43 MessageBoxView::MessageBoxView(int dialog_flags, | |
| 44 const string16& message, | |
| 45 const string16& default_prompt) | |
| 46 : message_label_(new Label(message)), | |
| 47 prompt_field_(NULL), | |
| 48 icon_(NULL), | |
| 49 checkbox_(NULL), | |
| 50 message_width_(kDefaultMessageWidth) { | |
| 51 Init(dialog_flags, default_prompt); | |
| 52 } | |
| 53 | |
| 54 MessageBoxView::~MessageBoxView() {} | |
| 55 | |
| 56 string16 MessageBoxView::GetInputText() { | |
| 57 return prompt_field_ ? prompt_field_->text() : string16(); | |
| 58 } | |
| 59 | |
| 60 bool MessageBoxView::IsCheckBoxSelected() { | |
| 61 return checkbox_ ? checkbox_->checked() : false; | |
| 62 } | |
| 63 | |
| 64 void MessageBoxView::SetIcon(const SkBitmap& icon) { | |
| 65 if (!icon_) | |
| 66 icon_ = new ImageView(); | |
| 67 icon_->SetImage(icon); | |
| 68 icon_->SetBounds(0, 0, icon.width(), icon.height()); | |
| 69 ResetLayoutManager(); | |
| 70 } | |
| 71 | |
| 72 void MessageBoxView::SetCheckBoxLabel(const string16& label) { | |
| 73 if (!checkbox_) | |
| 74 checkbox_ = new Checkbox(label); | |
| 75 else | |
| 76 checkbox_->SetText(label); | |
| 77 ResetLayoutManager(); | |
| 78 } | |
| 79 | |
| 80 void MessageBoxView::SetCheckBoxSelected(bool selected) { | |
| 81 if (!checkbox_) | |
| 82 return; | |
| 83 checkbox_->SetChecked(selected); | |
| 84 } | |
| 85 | |
| 86 void MessageBoxView::GetAccessibleState(ui::AccessibleViewState* state) { | |
| 87 state->role = ui::AccessibilityTypes::ROLE_ALERT; | |
| 88 } | |
| 89 | |
| 90 /////////////////////////////////////////////////////////////////////////////// | |
| 91 // MessageBoxView, View overrides: | |
| 92 | |
| 93 void MessageBoxView::ViewHierarchyChanged(bool is_add, | |
| 94 View* parent, | |
| 95 View* child) { | |
| 96 if (child == this && is_add) { | |
| 97 if (prompt_field_) | |
| 98 prompt_field_->SelectAll(); | |
| 99 | |
| 100 GetWidget()->NotifyAccessibilityEvent( | |
| 101 this, ui::AccessibilityTypes::EVENT_ALERT, true); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 bool MessageBoxView::AcceleratorPressed(const ui::Accelerator& accelerator) { | |
| 106 // We only accepts Ctrl-C. | |
| 107 DCHECK(accelerator.key_code() == 'C' && accelerator.IsCtrlDown()); | |
| 108 | |
| 109 // We must not intercept Ctrl-C when we have a text box and it's focused. | |
| 110 if (prompt_field_ && prompt_field_->HasFocus()) | |
| 111 return false; | |
| 112 | |
| 113 if (!ViewsDelegate::views_delegate) | |
| 114 return false; | |
| 115 | |
| 116 ui::Clipboard* clipboard = ViewsDelegate::views_delegate->GetClipboard(); | |
| 117 if (!clipboard) | |
| 118 return false; | |
| 119 | |
| 120 ui::ScopedClipboardWriter scw(clipboard); | |
| 121 scw.WriteText(message_label_->GetText()); | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 125 /////////////////////////////////////////////////////////////////////////////// | |
| 126 // MessageBoxView, private: | |
| 127 | |
| 128 void MessageBoxView::Init(int dialog_flags, | |
| 129 const string16& default_prompt) { | |
| 130 message_label_->SetMultiLine(true); | |
| 131 message_label_->SetAllowCharacterBreak(true); | |
| 132 if (dialog_flags & ui::MessageBoxFlags::kAutoDetectAlignment) { | |
| 133 // Determine the alignment and directionality based on the first character | |
| 134 // with strong directionality. | |
| 135 base::i18n::TextDirection direction = | |
| 136 base::i18n::GetFirstStrongCharacterDirection( | |
| 137 message_label_->GetText()); | |
| 138 Label::Alignment alignment; | |
| 139 if (direction == base::i18n::RIGHT_TO_LEFT) | |
| 140 alignment = Label::ALIGN_RIGHT; | |
| 141 else | |
| 142 alignment = Label::ALIGN_LEFT; | |
| 143 // In addition, we should set the RTL alignment mode as | |
| 144 // AUTO_DETECT_ALIGNMENT so that the alignment will not be flipped around | |
| 145 // in RTL locales. | |
| 146 message_label_->set_rtl_alignment_mode(Label::AUTO_DETECT_ALIGNMENT); | |
| 147 message_label_->SetHorizontalAlignment(alignment); | |
| 148 } else { | |
| 149 message_label_->SetHorizontalAlignment(Label::ALIGN_LEFT); | |
| 150 } | |
| 151 | |
| 152 if (dialog_flags & ui::MessageBoxFlags::kFlagHasPromptField) { | |
| 153 prompt_field_ = new Textfield; | |
| 154 prompt_field_->SetText(default_prompt); | |
| 155 } | |
| 156 | |
| 157 ResetLayoutManager(); | |
| 158 } | |
| 159 | |
| 160 void MessageBoxView::ResetLayoutManager() { | |
| 161 // Initialize the Grid Layout Manager used for this dialog box. | |
| 162 GridLayout* layout = GridLayout::CreatePanel(this); | |
| 163 SetLayoutManager(layout); | |
| 164 | |
| 165 gfx::Size icon_size; | |
| 166 if (icon_) | |
| 167 icon_size = icon_->GetPreferredSize(); | |
| 168 | |
| 169 // Add the column set for the message displayed at the top of the dialog box. | |
| 170 // And an icon, if one has been set. | |
| 171 const int message_column_view_set_id = 0; | |
| 172 ColumnSet* column_set = layout->AddColumnSet(message_column_view_set_id); | |
| 173 if (icon_) { | |
| 174 column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0, | |
| 175 GridLayout::FIXED, icon_size.width(), | |
| 176 icon_size.height()); | |
| 177 column_set->AddPaddingColumn(0, kUnrelatedControlHorizontalSpacing); | |
| 178 } | |
| 179 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
| 180 GridLayout::FIXED, message_width_, 0); | |
| 181 | |
| 182 // Column set for prompt Textfield, if one has been set. | |
| 183 const int textfield_column_view_set_id = 1; | |
| 184 if (prompt_field_) { | |
| 185 column_set = layout->AddColumnSet(textfield_column_view_set_id); | |
| 186 if (icon_) { | |
| 187 column_set->AddPaddingColumn( | |
| 188 0, icon_size.width() + kUnrelatedControlHorizontalSpacing); | |
| 189 } | |
| 190 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
| 191 GridLayout::USE_PREF, 0, 0); | |
| 192 } | |
| 193 | |
| 194 // Column set for checkbox, if one has been set. | |
| 195 const int checkbox_column_view_set_id = 2; | |
| 196 if (checkbox_) { | |
| 197 column_set = layout->AddColumnSet(checkbox_column_view_set_id); | |
| 198 if (icon_) { | |
| 199 column_set->AddPaddingColumn( | |
| 200 0, icon_size.width() + kUnrelatedControlHorizontalSpacing); | |
| 201 } | |
| 202 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
| 203 GridLayout::USE_PREF, 0, 0); | |
| 204 } | |
| 205 | |
| 206 layout->StartRow(0, message_column_view_set_id); | |
| 207 if (icon_) | |
| 208 layout->AddView(icon_); | |
| 209 | |
| 210 layout->AddView(message_label_); | |
| 211 | |
| 212 if (prompt_field_) { | |
| 213 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
| 214 layout->StartRow(0, textfield_column_view_set_id); | |
| 215 layout->AddView(prompt_field_); | |
| 216 } | |
| 217 | |
| 218 if (checkbox_) { | |
| 219 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
| 220 layout->StartRow(0, checkbox_column_view_set_id); | |
| 221 layout->AddView(checkbox_); | |
| 222 } | |
| 223 | |
| 224 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
| 225 } | |
| 226 | |
| 227 } // namespace views | |
| OLD | NEW |