OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/input_window_dialog.h" | 5 #include "chrome/browser/ui/input_window_dialog.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/task.h" | 9 #include "base/task.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "grit/generated_resources.h" | 11 #include "grit/generated_resources.h" |
12 #include "views/controls/label.h" | 12 #include "views/controls/label.h" |
13 #include "views/controls/textfield/textfield.h" | 13 #include "views/controls/textfield/textfield.h" |
14 #include "views/controls/textfield/textfield_controller.h" | 14 #include "views/controls/textfield/textfield_controller.h" |
15 #include "views/layout/grid_layout.h" | 15 #include "views/layout/grid_layout.h" |
16 #include "views/layout/layout_constants.h" | 16 #include "views/layout/layout_constants.h" |
17 #include "views/widget/widget.h" | 17 #include "views/widget/widget.h" |
18 #include "views/window/dialog_delegate.h" | 18 #include "views/window/dialog_delegate.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Width of the text field, in pixels. | 22 // Width of the text field, in pixels. |
23 const int kTextfieldWidth = 200; | 23 const int kTextfieldWidth = 200; |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 // The Windows implementation of the cross platform input dialog interface. | 27 // The Windows implementation of the cross platform input dialog interface. |
28 class WinInputWindowDialog : public InputWindowDialog { | 28 class InputWindowDialogWin : public InputWindowDialog { |
29 public: | 29 public: |
30 WinInputWindowDialog(gfx::NativeWindow parent, | 30 InputWindowDialogWin(gfx::NativeWindow parent, |
31 const string16& window_title, | 31 const string16& window_title, |
32 const string16& label, | 32 const string16& label, |
33 const string16& contents, | 33 const string16& contents, |
34 Delegate* delegate); | 34 Delegate* delegate); |
35 virtual ~WinInputWindowDialog(); | 35 virtual ~InputWindowDialogWin(); |
36 | 36 |
37 // Overridden from InputWindowDialog: | 37 // Overridden from InputWindowDialog: |
38 virtual void Show() OVERRIDE; | 38 virtual void Show() OVERRIDE; |
39 virtual void Close() OVERRIDE; | 39 virtual void Close() OVERRIDE; |
40 | 40 |
41 const string16& window_title() const { return window_title_; } | 41 const string16& window_title() const { return window_title_; } |
42 const string16& label() const { return label_; } | 42 const string16& label() const { return label_; } |
43 const string16& contents() const { return contents_; } | 43 const string16& contents() const { return contents_; } |
44 | 44 |
45 InputWindowDialog::Delegate* delegate() { return delegate_.get(); } | 45 InputWindowDialog::Delegate* delegate() { return delegate_.get(); } |
46 | 46 |
47 private: | 47 private: |
48 // Our chrome views window. | 48 // Our chrome views window. |
49 views::Widget* window_; | 49 views::Widget* window_; |
50 | 50 |
51 // Strings to feed to the on screen window. | 51 // Strings to feed to the on screen window. |
52 string16 window_title_; | 52 string16 window_title_; |
53 string16 label_; | 53 string16 label_; |
54 string16 contents_; | 54 string16 contents_; |
55 | 55 |
56 // Our delegate. Consumes the window's output. | 56 // Our delegate. Consumes the window's output. |
57 scoped_ptr<InputWindowDialog::Delegate> delegate_; | 57 scoped_ptr<InputWindowDialog::Delegate> delegate_; |
58 }; | 58 }; |
59 | 59 |
60 // ContentView, as the name implies, is the content view for the InputWindow. | 60 // ContentView, as the name implies, is the content view for the InputWindow. |
61 // It registers accelerators that accept/cancel the input. | 61 // It registers accelerators that accept/cancel the input. |
62 class ContentView : public views::DialogDelegateView, | 62 class ContentView : public views::DialogDelegateView, |
63 public views::TextfieldController { | 63 public views::TextfieldController { |
64 public: | 64 public: |
65 explicit ContentView(WinInputWindowDialog* delegate); | 65 explicit ContentView(InputWindowDialogWin* delegate); |
66 | 66 |
67 // views::DialogDelegateView: | 67 // views::DialogDelegateView: |
68 virtual bool IsDialogButtonEnabled( | 68 virtual bool IsDialogButtonEnabled( |
69 MessageBoxFlags::DialogButton button) const OVERRIDE; | 69 ui::MessageBoxFlags::DialogButton button) const OVERRIDE; |
70 virtual bool Accept() OVERRIDE; | 70 virtual bool Accept() OVERRIDE; |
71 virtual bool Cancel() OVERRIDE; | 71 virtual bool Cancel() OVERRIDE; |
72 virtual void DeleteDelegate() OVERRIDE; | 72 virtual void DeleteDelegate() OVERRIDE; |
73 virtual string16 GetWindowTitle() const OVERRIDE; | 73 virtual string16 GetWindowTitle() const OVERRIDE; |
74 virtual bool IsModal() const OVERRIDE; | 74 virtual bool IsModal() const OVERRIDE; |
75 virtual views::View* GetContentsView() OVERRIDE; | 75 virtual views::View* GetContentsView() OVERRIDE; |
76 | 76 |
77 // views::TextfieldController: | 77 // views::TextfieldController: |
78 virtual void ContentsChanged(views::Textfield* sender, | 78 virtual void ContentsChanged(views::Textfield* sender, |
79 const string16& new_contents) OVERRIDE; | 79 const string16& new_contents) OVERRIDE; |
(...skipping 11 matching lines...) Expand all Loading... |
91 void InitControlLayout(); | 91 void InitControlLayout(); |
92 | 92 |
93 // Sets focus to the first focusable element within the dialog. | 93 // Sets focus to the first focusable element within the dialog. |
94 void FocusFirstFocusableControl(); | 94 void FocusFirstFocusableControl(); |
95 | 95 |
96 // The Textfield that the user can type into. | 96 // The Textfield that the user can type into. |
97 views::Textfield* text_field_; | 97 views::Textfield* text_field_; |
98 | 98 |
99 // The delegate that the ContentView uses to communicate changes to the | 99 // The delegate that the ContentView uses to communicate changes to the |
100 // caller. | 100 // caller. |
101 WinInputWindowDialog* delegate_; | 101 InputWindowDialogWin* delegate_; |
102 | 102 |
103 // Helps us set focus to the first Textfield in the window. | 103 // Helps us set focus to the first Textfield in the window. |
104 ScopedRunnableMethodFactory<ContentView> focus_grabber_factory_; | 104 ScopedRunnableMethodFactory<ContentView> focus_grabber_factory_; |
105 | 105 |
106 DISALLOW_COPY_AND_ASSIGN(ContentView); | 106 DISALLOW_COPY_AND_ASSIGN(ContentView); |
107 }; | 107 }; |
108 | 108 |
109 /////////////////////////////////////////////////////////////////////////////// | 109 /////////////////////////////////////////////////////////////////////////////// |
110 // ContentView | 110 // ContentView |
111 ContentView::ContentView(WinInputWindowDialog* delegate) | 111 ContentView::ContentView(InputWindowDialogWin* delegate) |
112 : delegate_(delegate), | 112 : delegate_(delegate), |
113 ALLOW_THIS_IN_INITIALIZER_LIST(focus_grabber_factory_(this)) { | 113 ALLOW_THIS_IN_INITIALIZER_LIST(focus_grabber_factory_(this)) { |
114 DCHECK(delegate_); | 114 DCHECK(delegate_); |
115 } | 115 } |
116 | 116 |
117 /////////////////////////////////////////////////////////////////////////////// | 117 /////////////////////////////////////////////////////////////////////////////// |
118 // ContentView, views::DialogDelegate implementation: | 118 // ContentView, views::DialogDelegate implementation: |
119 | 119 |
120 bool ContentView::IsDialogButtonEnabled( | 120 bool ContentView::IsDialogButtonEnabled( |
121 MessageBoxFlags::DialogButton button) const { | 121 ui::MessageBoxFlags::DialogButton button) const { |
122 if (button == MessageBoxFlags::DIALOGBUTTON_OK && | 122 if (button == ui::MessageBoxFlags::DIALOGBUTTON_OK && |
123 !delegate_->delegate()->IsValid(text_field_->text())) { | 123 !delegate_->delegate()->IsValid(text_field_->text())) { |
124 return false; | 124 return false; |
125 } | 125 } |
126 return true; | 126 return true; |
127 } | 127 } |
128 | 128 |
129 bool ContentView::Accept() { | 129 bool ContentView::Accept() { |
130 delegate_->delegate()->InputAccepted(text_field_->text()); | 130 delegate_->delegate()->InputAccepted(text_field_->text()); |
131 return true; | 131 return true; |
132 } | 132 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 views::View* child) { | 173 views::View* child) { |
174 if (is_add && child == this) | 174 if (is_add && child == this) |
175 InitControlLayout(); | 175 InitControlLayout(); |
176 } | 176 } |
177 | 177 |
178 /////////////////////////////////////////////////////////////////////////////// | 178 /////////////////////////////////////////////////////////////////////////////// |
179 // ContentView, private: | 179 // ContentView, private: |
180 | 180 |
181 void ContentView::InitControlLayout() { | 181 void ContentView::InitControlLayout() { |
182 text_field_ = new views::Textfield; | 182 text_field_ = new views::Textfield; |
183 text_field_->SetText(UTF16ToWideHack(delegate_->contents())); | 183 text_field_->SetText(delegate_->contents()); |
184 text_field_->SetController(this); | 184 text_field_->SetController(this); |
185 | 185 |
186 using views::GridLayout; | |
187 | |
188 // TODO(sky): Vertical alignment should be baseline. | 186 // TODO(sky): Vertical alignment should be baseline. |
189 GridLayout* layout = GridLayout::CreatePanel(this); | 187 views::GridLayout* layout = views::GridLayout::CreatePanel(this); |
190 SetLayoutManager(layout); | 188 SetLayoutManager(layout); |
191 | 189 |
192 views::ColumnSet* c1 = layout->AddColumnSet(0); | 190 views::ColumnSet* c1 = layout->AddColumnSet(0); |
193 c1->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, | 191 c1->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, |
194 GridLayout::USE_PREF, 0, 0); | 192 views::GridLayout::USE_PREF, 0, 0); |
195 c1->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | 193 c1->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); |
196 c1->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, | 194 c1->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 1, |
197 GridLayout::USE_PREF, kTextfieldWidth, kTextfieldWidth); | 195 views::GridLayout::USE_PREF, kTextfieldWidth, kTextfieldWidth); |
198 | 196 |
199 layout->StartRow(0, 0); | 197 layout->StartRow(0, 0); |
200 views::Label* label = new views::Label(delegate_->label()); | 198 views::Label* label = new views::Label(delegate_->label()); |
201 layout->AddView(label); | 199 layout->AddView(label); |
202 layout->AddView(text_field_); | 200 layout->AddView(text_field_); |
203 | 201 |
204 MessageLoop::current()->PostTask(FROM_HERE, | 202 MessageLoop::current()->PostTask(FROM_HERE, |
205 focus_grabber_factory_.NewRunnableMethod( | 203 focus_grabber_factory_.NewRunnableMethod( |
206 &ContentView::FocusFirstFocusableControl)); | 204 &ContentView::FocusFirstFocusableControl)); |
207 } | 205 } |
208 | 206 |
209 void ContentView::FocusFirstFocusableControl() { | 207 void ContentView::FocusFirstFocusableControl() { |
210 text_field_->SelectAll(); | 208 text_field_->SelectAll(); |
211 text_field_->RequestFocus(); | 209 text_field_->RequestFocus(); |
212 } | 210 } |
213 | 211 |
214 WinInputWindowDialog::WinInputWindowDialog(gfx::NativeWindow parent, | 212 InputWindowDialogWin::InputWindowDialogWin(gfx::NativeWindow parent, |
215 const string16& window_title, | 213 const string16& window_title, |
216 const string16& label, | 214 const string16& label, |
217 const string16& contents, | 215 const string16& contents, |
218 Delegate* delegate) | 216 Delegate* delegate) |
219 : window_title_(window_title), | 217 : window_title_(window_title), |
220 label_(label), | 218 label_(label), |
221 contents_(contents), | 219 contents_(contents), |
222 delegate_(delegate) { | 220 delegate_(delegate) { |
223 window_ = views::Widget::CreateWindowWithParent(new ContentView(this), | 221 window_ = views::Widget::CreateWindowWithParent(new ContentView(this), |
224 parent); | 222 parent); |
225 window_->client_view()->AsDialogClientView()->UpdateDialogButtons(); | 223 window_->client_view()->AsDialogClientView()->UpdateDialogButtons(); |
226 } | 224 } |
227 | 225 |
228 WinInputWindowDialog::~WinInputWindowDialog() { | 226 InputWindowDialogWin::~InputWindowDialogWin() { |
229 } | 227 } |
230 | 228 |
231 void WinInputWindowDialog::Show() { | 229 void InputWindowDialogWin::Show() { |
232 window_->Show(); | 230 window_->Show(); |
233 } | 231 } |
234 | 232 |
235 void WinInputWindowDialog::Close() { | 233 void InputWindowDialogWin::Close() { |
236 window_->Close(); | 234 window_->Close(); |
237 } | 235 } |
238 | 236 |
239 // static | 237 // static |
240 InputWindowDialog* InputWindowDialog::Create(gfx::NativeWindow parent, | 238 InputWindowDialog* InputWindowDialog::Create(gfx::NativeWindow parent, |
241 const string16& window_title, | 239 const string16& window_title, |
242 const string16& label, | 240 const string16& label, |
243 const string16& contents, | 241 const string16& contents, |
244 Delegate* delegate) { | 242 Delegate* delegate, |
245 return new WinInputWindowDialog( | 243 ButtonType type) { |
| 244 return new InputWindowDialogWin( |
246 parent, window_title, label, contents, delegate); | 245 parent, window_title, label, contents, delegate); |
247 } | 246 } |
OLD | NEW |