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.
5 #include "ui/views/controls/message_box_view.h"
7 #include "base/i18n/rtl.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/accessibility/ax_view_state.h"
12 #include "ui/base/clipboard/clipboard.h"
13 #include "ui/base/clipboard/scoped_clipboard_writer.h"
14 #include "ui/views/controls/button/checkbox.h"
15 #include "ui/views/controls/label.h"
16 #include "ui/views/controls/link.h"
17 #include "ui/views/controls/scroll_view.h"
18 #include "ui/views/controls/textfield/textfield.h"
19 #include "ui/views/layout/box_layout.h"
20 #include "ui/views/layout/grid_layout.h"
21 #include "ui/views/layout/layout_constants.h"
22 #include "ui/views/widget/widget.h"
23 #include "ui/views/window/client_view.h"
24 #include "ui/views/window/dialog_delegate.h"
28 const int kDefaultMessageWidth
= 320;
30 // Paragraph separators are defined in
31 // http://www.unicode.org/Public/6.0.0/ucd/extracted/DerivedBidiClass.txt
33 // # Bidi_Class=Paragraph_Separator
35 // 000A ; B # Cc <control-000A>
36 // 000D ; B # Cc <control-000D>
37 // 001C..001E ; B # Cc [3] <control-001C>..<control-001E>
38 // 0085 ; B # Cc <control-0085>
39 // 2029 ; B # Zp PARAGRAPH SEPARATOR
40 bool IsParagraphSeparator(base::char16 c
) {
41 return ( c
== 0x000A || c
== 0x000D || c
== 0x001C || c
== 0x001D ||
42 c
== 0x001E || c
== 0x0085 || c
== 0x2029);
45 // Splits |text| into a vector of paragraphs.
46 // Given an example "\nabc\ndef\n\n\nhij\n", the split results should be:
47 // "", "abc", "def", "", "", "hij", and "".
48 void SplitStringIntoParagraphs(const base::string16
& text
,
49 std::vector
<base::string16
>* paragraphs
) {
53 for (size_t i
= 0; i
< text
.length(); ++i
) {
54 if (IsParagraphSeparator(text
[i
])) {
55 paragraphs
->push_back(text
.substr(start
, i
- start
));
59 paragraphs
->push_back(text
.substr(start
, text
.length() - start
));
66 ///////////////////////////////////////////////////////////////////////////////
67 // MessageBoxView, public:
69 MessageBoxView::InitParams::InitParams(const base::string16
& message
)
70 : options(NO_OPTIONS
),
72 message_width(kDefaultMessageWidth
),
73 inter_row_vertical_spacing(kRelatedControlVerticalSpacing
) {}
75 MessageBoxView::InitParams::~InitParams() {
78 MessageBoxView::MessageBoxView(const InitParams
& params
)
79 : prompt_field_(NULL
),
82 message_width_(params
.message_width
) {
86 MessageBoxView::~MessageBoxView() {}
88 base::string16
MessageBoxView::GetInputText() {
89 return prompt_field_
? prompt_field_
->text() : base::string16();
92 bool MessageBoxView::IsCheckBoxSelected() {
93 return checkbox_
? checkbox_
->checked() : false;
96 void MessageBoxView::SetCheckBoxLabel(const base::string16
& label
) {
98 checkbox_
= new Checkbox(label
);
100 checkbox_
->SetText(label
);
101 ResetLayoutManager();
104 void MessageBoxView::SetCheckBoxSelected(bool selected
) {
107 checkbox_
->SetChecked(selected
);
110 void MessageBoxView::SetLink(const base::string16
& text
,
111 LinkListener
* listener
) {
120 link_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
122 link_
->SetText(text
);
123 link_
->set_listener(listener
);
125 ResetLayoutManager();
128 void MessageBoxView::GetAccessibleState(ui::AXViewState
* state
) {
129 state
->role
= ui::AX_ROLE_ALERT
;
132 ///////////////////////////////////////////////////////////////////////////////
133 // MessageBoxView, View overrides:
135 void MessageBoxView::ViewHierarchyChanged(
136 const ViewHierarchyChangedDetails
& details
) {
137 if (details
.child
== this && details
.is_add
) {
139 prompt_field_
->SelectAll(true);
141 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT
, true);
145 bool MessageBoxView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
146 // We only accepts Ctrl-C.
147 DCHECK(accelerator
.key_code() == 'C' && accelerator
.IsCtrlDown());
149 // We must not intercept Ctrl-C when we have a text box and it's focused.
150 if (prompt_field_
&& prompt_field_
->HasFocus())
153 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
157 ui::ScopedClipboardWriter
scw(clipboard
, ui::CLIPBOARD_TYPE_COPY_PASTE
);
158 base::string16 text
= message_labels_
[0]->text();
159 for (size_t i
= 1; i
< message_labels_
.size(); ++i
)
160 text
+= message_labels_
[i
]->text();
165 ///////////////////////////////////////////////////////////////////////////////
166 // MessageBoxView, private:
168 void MessageBoxView::Init(const InitParams
& params
) {
169 if (params
.options
& DETECT_DIRECTIONALITY
) {
170 std::vector
<base::string16
> texts
;
171 SplitStringIntoParagraphs(params
.message
, &texts
);
172 for (size_t i
= 0; i
< texts
.size(); ++i
) {
173 Label
* message_label
= new Label(texts
[i
]);
174 // Avoid empty multi-line labels, which have a height of 0.
175 message_label
->SetMultiLine(!texts
[i
].empty());
176 message_label
->SetAllowCharacterBreak(true);
177 message_label
->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD
);
178 message_labels_
.push_back(message_label
);
181 Label
* message_label
= new Label(params
.message
);
182 message_label
->SetMultiLine(true);
183 message_label
->SetAllowCharacterBreak(true);
184 message_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
185 message_labels_
.push_back(message_label
);
188 if (params
.options
& HAS_PROMPT_FIELD
) {
189 prompt_field_
= new Textfield
;
190 prompt_field_
->SetText(params
.default_prompt
);
193 inter_row_vertical_spacing_
= params
.inter_row_vertical_spacing
;
195 ResetLayoutManager();
198 void MessageBoxView::ResetLayoutManager() {
199 // Initialize the Grid Layout Manager used for this dialog box.
200 GridLayout
* layout
= GridLayout::CreatePanel(this);
201 SetLayoutManager(layout
);
203 // Add the column set for the message displayed at the top of the dialog box.
204 const int message_column_view_set_id
= 0;
205 ColumnSet
* column_set
= layout
->AddColumnSet(message_column_view_set_id
);
206 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
207 GridLayout::FIXED
, message_width_
, 0);
209 // Column set for extra elements, if any.
210 const int extra_column_view_set_id
= 1;
211 if (prompt_field_
|| checkbox_
|| link_
) {
212 column_set
= layout
->AddColumnSet(extra_column_view_set_id
);
213 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
214 GridLayout::USE_PREF
, 0, 0);
217 const int kMaxScrollViewHeight
= 600;
218 views::View
* message_contents
= new views::View();
219 message_contents
->SetLayoutManager(
220 new views::BoxLayout(views::BoxLayout::kVertical
, 0, 0, 0));
221 for (size_t i
= 0; i
< message_labels_
.size(); ++i
)
222 message_contents
->AddChildView(message_labels_
[i
]);
223 ScrollView
* scroll_view
= new views::ScrollView();
224 scroll_view
->ClipHeightTo(0, kMaxScrollViewHeight
);
225 scroll_view
->SetContents(message_contents
);
226 layout
->StartRow(0, message_column_view_set_id
);
227 layout
->AddView(scroll_view
);
230 layout
->AddPaddingRow(0, inter_row_vertical_spacing_
);
231 layout
->StartRow(0, extra_column_view_set_id
);
232 layout
->AddView(prompt_field_
);
236 layout
->AddPaddingRow(0, inter_row_vertical_spacing_
);
237 layout
->StartRow(0, extra_column_view_set_id
);
238 layout
->AddView(checkbox_
);
242 layout
->AddPaddingRow(0, inter_row_vertical_spacing_
);
243 layout
->StartRow(0, extra_column_view_set_id
);
244 layout
->AddView(link_
);