OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/window/dialog_client_view.h" | 5 #include "ui/views/window/dialog_client_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "ui/events/keycodes/keyboard_codes.h" | 10 #include "ui/events/keycodes/keyboard_codes.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 row_bounds->set_width(row_bounds->width() - kRelatedButtonHSpacing); | 47 row_bounds->set_width(row_bounds->width() - kRelatedButtonHSpacing); |
48 } | 48 } |
49 | 49 |
50 } // namespace | 50 } // namespace |
51 | 51 |
52 /////////////////////////////////////////////////////////////////////////////// | 52 /////////////////////////////////////////////////////////////////////////////// |
53 // DialogClientView, public: | 53 // DialogClientView, public: |
54 | 54 |
55 DialogClientView::DialogClientView(Widget* owner, View* contents_view) | 55 DialogClientView::DialogClientView(Widget* owner, View* contents_view) |
56 : ClientView(owner, contents_view), | 56 : ClientView(owner, contents_view), |
57 button_row_insets_(gfx::Insets(0, | |
sky
2016/02/23 17:56:15
you shouldn't need the extra gfx::Insets here, tha
Evan Stade
2016/02/23 19:02:36
Done.
| |
58 kButtonHEdgeMarginNew, | |
59 kButtonVEdgeMarginNew, | |
60 kButtonHEdgeMarginNew)), | |
57 ok_button_(NULL), | 61 ok_button_(NULL), |
58 cancel_button_(NULL), | 62 cancel_button_(NULL), |
59 extra_view_(NULL), | 63 extra_view_(NULL), |
60 footnote_view_(NULL), | |
61 delegate_allowed_close_(false) {} | 64 delegate_allowed_close_(false) {} |
62 | 65 |
63 DialogClientView::~DialogClientView() { | 66 DialogClientView::~DialogClientView() { |
64 } | 67 } |
65 | 68 |
66 void DialogClientView::AcceptWindow() { | 69 void DialogClientView::AcceptWindow() { |
67 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. | 70 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. |
68 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept(false)) { | 71 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept(false)) { |
69 delegate_allowed_close_ = true; | 72 delegate_allowed_close_ = true; |
70 GetWidget()->Close(); | 73 GetWidget()->Close(); |
71 } | 74 } |
72 } | 75 } |
73 | 76 |
74 void DialogClientView::CancelWindow() { | 77 void DialogClientView::CancelWindow() { |
75 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. | 78 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. |
76 if (!delegate_allowed_close_ && GetDialogDelegate()->Cancel()) { | 79 if (!delegate_allowed_close_ && GetDialogDelegate()->Cancel()) { |
77 delegate_allowed_close_ = true; | 80 delegate_allowed_close_ = true; |
78 GetWidget()->Close(); | 81 GetWidget()->Close(); |
79 } | 82 } |
80 } | 83 } |
81 | 84 |
82 void DialogClientView::UpdateDialogButtons() { | 85 void DialogClientView::UpdateDialogButtons() { |
83 const int buttons = GetDialogDelegate()->GetDialogButtons(); | 86 const int buttons = GetDialogDelegate()->GetDialogButtons(); |
84 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); | |
85 | 87 |
86 if (buttons & ui::DIALOG_BUTTON_OK) { | 88 if (buttons & ui::DIALOG_BUTTON_OK) { |
87 if (!ok_button_) { | 89 if (!ok_button_) { |
88 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); | 90 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); |
89 if (!(buttons & ui::DIALOG_BUTTON_CANCEL)) | |
90 ok_button_->AddAccelerator(escape); | |
91 AddChildView(ok_button_); | 91 AddChildView(ok_button_); |
92 } | 92 } |
93 | 93 |
94 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); | 94 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); |
95 } else if (ok_button_) { | 95 } else if (ok_button_) { |
96 delete ok_button_; | 96 delete ok_button_; |
97 ok_button_ = NULL; | 97 ok_button_ = NULL; |
98 } | 98 } |
99 | 99 |
100 if (buttons & ui::DIALOG_BUTTON_CANCEL) { | 100 if (buttons & ui::DIALOG_BUTTON_CANCEL) { |
101 if (!cancel_button_) { | 101 if (!cancel_button_) { |
102 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); | 102 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); |
103 cancel_button_->AddAccelerator(escape); | |
104 AddChildView(cancel_button_); | 103 AddChildView(cancel_button_); |
105 } | 104 } |
106 | 105 |
107 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); | 106 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); |
108 } else if (cancel_button_) { | 107 } else if (cancel_button_) { |
109 delete cancel_button_; | 108 delete cancel_button_; |
110 cancel_button_ = NULL; | 109 cancel_button_ = NULL; |
111 } | 110 } |
112 | |
113 // Use the escape key to close the window if there are no dialog buttons. | |
114 if (!has_dialog_buttons()) | |
115 AddAccelerator(escape); | |
116 else | |
117 ResetAccelerators(); | |
118 } | 111 } |
119 | 112 |
120 /////////////////////////////////////////////////////////////////////////////// | 113 /////////////////////////////////////////////////////////////////////////////// |
121 // DialogClientView, ClientView overrides: | 114 // DialogClientView, ClientView overrides: |
122 | 115 |
123 bool DialogClientView::CanClose() { | 116 bool DialogClientView::CanClose() { |
124 // If the dialog is closing but no Accept or Cancel action has been performed | 117 // If the dialog is closing but no Accept or Cancel action has been performed |
125 // before, it's a Close action. | 118 // before, it's a Close action. |
126 if (!delegate_allowed_close_) | 119 if (!delegate_allowed_close_) |
127 delegate_allowed_close_ = GetDialogDelegate()->Close(); | 120 delegate_allowed_close_ = GetDialogDelegate()->Close(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 const gfx::Insets insets = GetButtonRowInsets(); | 153 const gfx::Insets insets = GetButtonRowInsets(); |
161 size.Enlarge(insets.width(), insets.height()); | 154 size.Enlarge(insets.width(), insets.height()); |
162 } | 155 } |
163 | 156 |
164 // Increase the size as needed to fit the contents view. | 157 // Increase the size as needed to fit the contents view. |
165 // NOTE: The contents view is not inset on the top or side client view edges. | 158 // NOTE: The contents view is not inset on the top or side client view edges. |
166 gfx::Size contents_size = contents_view()->GetPreferredSize(); | 159 gfx::Size contents_size = contents_view()->GetPreferredSize(); |
167 size.Enlarge(0, contents_size.height()); | 160 size.Enlarge(0, contents_size.height()); |
168 size.set_width(std::max(size.width(), contents_size.width())); | 161 size.set_width(std::max(size.width(), contents_size.width())); |
169 | 162 |
170 // Increase the size as needed to fit the footnote view. | |
171 if (ShouldShow(footnote_view_)) { | |
172 gfx::Size footnote_size = footnote_view_->GetPreferredSize(); | |
173 if (!footnote_size.IsEmpty()) | |
174 size.set_width(std::max(size.width(), footnote_size.width())); | |
175 | |
176 int footnote_height = footnote_view_->GetHeightForWidth(size.width()); | |
177 size.Enlarge(0, footnote_height); | |
178 } | |
179 | |
180 return size; | 163 return size; |
181 } | 164 } |
182 | 165 |
183 void DialogClientView::Layout() { | 166 void DialogClientView::Layout() { |
184 gfx::Rect bounds = GetContentsBounds(); | 167 gfx::Rect bounds = GetContentsBounds(); |
185 | 168 |
186 // Layout the footnote view. | |
187 if (ShouldShow(footnote_view_)) { | |
188 const int height = footnote_view_->GetHeightForWidth(bounds.width()); | |
189 footnote_view_->SetBounds(bounds.x(), bounds.bottom() - height, | |
190 bounds.width(), height); | |
191 if (height != 0) | |
192 bounds.Inset(0, 0, 0, height); | |
193 } | |
194 | |
195 // Layout the row containing the buttons and the extra view. | 169 // Layout the row containing the buttons and the extra view. |
196 if (has_dialog_buttons() || ShouldShow(extra_view_)) { | 170 if (has_dialog_buttons() || ShouldShow(extra_view_)) { |
197 bounds.Inset(GetButtonRowInsets()); | 171 bounds.Inset(GetButtonRowInsets()); |
198 const int height = GetButtonsAndExtraViewRowHeight(); | 172 const int height = GetButtonsAndExtraViewRowHeight(); |
199 gfx::Rect row_bounds(bounds.x(), bounds.bottom() - height, | 173 gfx::Rect row_bounds(bounds.x(), bounds.bottom() - height, |
200 bounds.width(), height); | 174 bounds.width(), height); |
201 if (kIsOkButtonOnLeftSide) { | 175 if (kIsOkButtonOnLeftSide) { |
202 LayoutButton(cancel_button_, &row_bounds); | 176 LayoutButton(cancel_button_, &row_bounds); |
203 LayoutButton(ok_button_, &row_bounds); | 177 LayoutButton(ok_button_, &row_bounds); |
204 } else { | 178 } else { |
(...skipping 30 matching lines...) Expand all Loading... | |
235 GetWidget()->Close(); | 209 GetWidget()->Close(); |
236 return true; | 210 return true; |
237 } | 211 } |
238 | 212 |
239 void DialogClientView::ViewHierarchyChanged( | 213 void DialogClientView::ViewHierarchyChanged( |
240 const ViewHierarchyChangedDetails& details) { | 214 const ViewHierarchyChangedDetails& details) { |
241 ClientView::ViewHierarchyChanged(details); | 215 ClientView::ViewHierarchyChanged(details); |
242 if (details.is_add && details.child == this) { | 216 if (details.is_add && details.child == this) { |
243 UpdateDialogButtons(); | 217 UpdateDialogButtons(); |
244 CreateExtraView(); | 218 CreateExtraView(); |
245 CreateFootnoteView(); | 219 // Use the escape key to close the window. |
220 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | |
246 } else if (!details.is_add && details.child != this) { | 221 } else if (!details.is_add && details.child != this) { |
247 if (details.child == ok_button_) | 222 if (details.child == ok_button_) |
248 ok_button_ = NULL; | 223 ok_button_ = NULL; |
249 if (details.child == cancel_button_) | 224 if (details.child == cancel_button_) |
250 cancel_button_ = NULL; | 225 cancel_button_ = NULL; |
251 } | 226 } |
252 } | 227 } |
253 | 228 |
254 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 229 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
255 // The old dialog style needs an explicit background color, while the new | 230 // The old dialog style needs an explicit background color, while the new |
(...skipping 23 matching lines...) Expand all Loading... | |
279 } | 254 } |
280 | 255 |
281 //////////////////////////////////////////////////////////////////////////////// | 256 //////////////////////////////////////////////////////////////////////////////// |
282 // DialogClientView, protected: | 257 // DialogClientView, protected: |
283 | 258 |
284 DialogClientView::DialogClientView(View* contents_view) | 259 DialogClientView::DialogClientView(View* contents_view) |
285 : ClientView(NULL, contents_view), | 260 : ClientView(NULL, contents_view), |
286 ok_button_(NULL), | 261 ok_button_(NULL), |
287 cancel_button_(NULL), | 262 cancel_button_(NULL), |
288 extra_view_(NULL), | 263 extra_view_(NULL), |
289 footnote_view_(NULL), | |
290 delegate_allowed_close_(false) {} | 264 delegate_allowed_close_(false) {} |
291 | 265 |
292 DialogDelegate* DialogClientView::GetDialogDelegate() const { | 266 DialogDelegate* DialogClientView::GetDialogDelegate() const { |
293 return GetWidget()->widget_delegate()->AsDialogDelegate(); | 267 return GetWidget()->widget_delegate()->AsDialogDelegate(); |
294 } | 268 } |
295 | 269 |
296 void DialogClientView::CreateExtraView() { | 270 void DialogClientView::CreateExtraView() { |
297 if (extra_view_) | 271 if (extra_view_) |
298 return; | 272 return; |
299 | 273 |
300 extra_view_ = GetDialogDelegate()->CreateExtraView(); | 274 extra_view_ = GetDialogDelegate()->CreateExtraView(); |
301 if (extra_view_) { | 275 if (extra_view_) { |
302 extra_view_->SetGroup(kButtonGroup); | 276 extra_view_->SetGroup(kButtonGroup); |
303 AddChildView(extra_view_); | 277 AddChildView(extra_view_); |
304 } | 278 } |
305 } | 279 } |
306 | 280 |
307 void DialogClientView::CreateFootnoteView() { | |
308 if (footnote_view_) | |
309 return; | |
310 | |
311 footnote_view_ = GetDialogDelegate()->CreateFootnoteView(); | |
312 if (footnote_view_) | |
313 AddChildView(footnote_view_); | |
314 } | |
315 | |
316 void DialogClientView::ChildPreferredSizeChanged(View* child) { | 281 void DialogClientView::ChildPreferredSizeChanged(View* child) { |
317 if (child == footnote_view_ || child == extra_view_) | 282 if (child == extra_view_) |
318 Layout(); | 283 Layout(); |
319 } | 284 } |
320 | 285 |
321 void DialogClientView::ChildVisibilityChanged(View* child) { | 286 void DialogClientView::ChildVisibilityChanged(View* child) { |
322 ChildPreferredSizeChanged(child); | 287 ChildPreferredSizeChanged(child); |
323 } | 288 } |
324 | 289 |
325 //////////////////////////////////////////////////////////////////////////////// | 290 //////////////////////////////////////////////////////////////////////////////// |
326 // DialogClientView, private: | 291 // DialogClientView, private: |
327 | 292 |
(...skipping 27 matching lines...) Expand all Loading... | |
355 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { | 320 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { |
356 int extra_view_height = ShouldShow(extra_view_) ? | 321 int extra_view_height = ShouldShow(extra_view_) ? |
357 extra_view_->GetPreferredSize().height() : 0; | 322 extra_view_->GetPreferredSize().height() : 0; |
358 int buttons_height = std::max( | 323 int buttons_height = std::max( |
359 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, | 324 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, |
360 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); | 325 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); |
361 return std::max(extra_view_height, buttons_height); | 326 return std::max(extra_view_height, buttons_height); |
362 } | 327 } |
363 | 328 |
364 gfx::Insets DialogClientView::GetButtonRowInsets() const { | 329 gfx::Insets DialogClientView::GetButtonRowInsets() const { |
365 // NOTE: The insets only apply to the buttons, extra view, and footnote view. | 330 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() |
366 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() : | 331 : button_row_insets_; |
367 gfx::Insets(0, kButtonHEdgeMarginNew, | |
368 kButtonVEdgeMarginNew, kButtonHEdgeMarginNew); | |
369 } | 332 } |
370 | 333 |
371 } // namespace views | 334 } // namespace views |
OLD | NEW |