| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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 "chrome/browser/input_window_dialog.h" | |
| 6 | |
| 7 #include "app/l10n_util.h" | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/task.h" | |
| 11 #include "views/grid_layout.h" | |
| 12 #include "views/controls/label.h" | |
| 13 #include "views/controls/textfield/textfield.h" | |
| 14 #include "views/standard_layout.h" | |
| 15 #include "views/window/dialog_delegate.h" | |
| 16 #include "views/window/window.h" | |
| 17 #include "grit/generated_resources.h" | |
| 18 | |
| 19 // Width to make the text field, in pixels. | |
| 20 static const int kTextfieldWidth = 200; | |
| 21 | |
| 22 class ContentView; | |
| 23 | |
| 24 // The Windows implementation of the cross platform input dialog interface. | |
| 25 class WinInputWindowDialog : public InputWindowDialog { | |
| 26 public: | |
| 27 WinInputWindowDialog(HWND parent, | |
| 28 const std::wstring& window_title, | |
| 29 const std::wstring& label, | |
| 30 const std::wstring& contents, | |
| 31 Delegate* delegate); | |
| 32 virtual ~WinInputWindowDialog(); | |
| 33 | |
| 34 virtual void Show(); | |
| 35 virtual void Close(); | |
| 36 | |
| 37 const std::wstring& window_title() const { return window_title_; } | |
| 38 const std::wstring& label() const { return label_; } | |
| 39 const std::wstring& contents() const { return contents_; } | |
| 40 | |
| 41 InputWindowDialog::Delegate* delegate() { return delegate_.get(); } | |
| 42 | |
| 43 private: | |
| 44 // Our chrome views window. | |
| 45 views::Window* window_; | |
| 46 | |
| 47 // Strings to feed to the on screen window. | |
| 48 std::wstring window_title_; | |
| 49 std::wstring label_; | |
| 50 std::wstring contents_; | |
| 51 | |
| 52 // Our delegate. Consumes the window's output. | |
| 53 scoped_ptr<InputWindowDialog::Delegate> delegate_; | |
| 54 }; | |
| 55 | |
| 56 // ContentView, as the name implies, is the content view for the InputWindow. | |
| 57 // It registers accelerators that accept/cancel the input. | |
| 58 class ContentView : public views::View, | |
| 59 public views::DialogDelegate, | |
| 60 public views::Textfield::Controller { | |
| 61 public: | |
| 62 explicit ContentView(WinInputWindowDialog* delegate) | |
| 63 : delegate_(delegate), | |
| 64 ALLOW_THIS_IN_INITIALIZER_LIST(focus_grabber_factory_(this)) { | |
| 65 DCHECK(delegate_); | |
| 66 } | |
| 67 | |
| 68 // views::DialogDelegate overrides: | |
| 69 virtual bool IsDialogButtonEnabled( | |
| 70 MessageBoxFlags::DialogButton button) const; | |
| 71 virtual bool Accept(); | |
| 72 virtual bool Cancel(); | |
| 73 virtual void DeleteDelegate(); | |
| 74 virtual std::wstring GetWindowTitle() const; | |
| 75 virtual bool IsModal() const { return true; } | |
| 76 virtual views::View* GetContentsView(); | |
| 77 | |
| 78 // views::Textfield::Controller overrides: | |
| 79 virtual void ContentsChanged(views::Textfield* sender, | |
| 80 const std::wstring& new_contents); | |
| 81 virtual bool HandleKeystroke(views::Textfield*, | |
| 82 const views::Textfield::Keystroke&) { | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 protected: | |
| 87 // views::View overrides: | |
| 88 virtual void ViewHierarchyChanged(bool is_add, views::View* parent, | |
| 89 views::View* child); | |
| 90 | |
| 91 private: | |
| 92 // Set up dialog controls and layout. | |
| 93 void InitControlLayout(); | |
| 94 | |
| 95 // Sets focus to the first focusable element within the dialog. | |
| 96 void FocusFirstFocusableControl(); | |
| 97 | |
| 98 // The Textfield that the user can type into. | |
| 99 views::Textfield* text_field_; | |
| 100 | |
| 101 // The delegate that the ContentView uses to communicate changes to the | |
| 102 // caller. | |
| 103 WinInputWindowDialog* delegate_; | |
| 104 | |
| 105 // Helps us set focus to the first Textfield in the window. | |
| 106 ScopedRunnableMethodFactory<ContentView> focus_grabber_factory_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(ContentView); | |
| 109 }; | |
| 110 | |
| 111 /////////////////////////////////////////////////////////////////////////////// | |
| 112 // ContentView, views::DialogDelegate implementation: | |
| 113 | |
| 114 bool ContentView::IsDialogButtonEnabled( | |
| 115 MessageBoxFlags::DialogButton button) const { | |
| 116 if (button == MessageBoxFlags::DIALOGBUTTON_OK && | |
| 117 !delegate_->delegate()->IsValid(text_field_->text())) { | |
| 118 return false; | |
| 119 } | |
| 120 return true; | |
| 121 } | |
| 122 | |
| 123 bool ContentView::Accept() { | |
| 124 delegate_->delegate()->InputAccepted(text_field_->text()); | |
| 125 return true; | |
| 126 } | |
| 127 | |
| 128 bool ContentView::Cancel() { | |
| 129 delegate_->delegate()->InputCanceled(); | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 void ContentView::DeleteDelegate() { | |
| 134 delete delegate_; | |
| 135 } | |
| 136 | |
| 137 std::wstring ContentView::GetWindowTitle() const { | |
| 138 return delegate_->window_title(); | |
| 139 } | |
| 140 | |
| 141 views::View* ContentView::GetContentsView() { | |
| 142 return this; | |
| 143 } | |
| 144 | |
| 145 /////////////////////////////////////////////////////////////////////////////// | |
| 146 // ContentView, views::Textfield::Controller implementation: | |
| 147 | |
| 148 void ContentView::ContentsChanged(views::Textfield* sender, | |
| 149 const std::wstring& new_contents) { | |
| 150 GetDialogClientView()->UpdateDialogButtons(); | |
| 151 } | |
| 152 | |
| 153 /////////////////////////////////////////////////////////////////////////////// | |
| 154 // ContentView, protected: | |
| 155 | |
| 156 void ContentView::ViewHierarchyChanged(bool is_add, | |
| 157 views::View* parent, | |
| 158 views::View* child) { | |
| 159 if (is_add && child == this) | |
| 160 InitControlLayout(); | |
| 161 } | |
| 162 | |
| 163 /////////////////////////////////////////////////////////////////////////////// | |
| 164 // ContentView, private: | |
| 165 | |
| 166 void ContentView::InitControlLayout() { | |
| 167 text_field_ = new views::Textfield; | |
| 168 text_field_->SetText(delegate_->contents()); | |
| 169 text_field_->SetController(this); | |
| 170 | |
| 171 using views::ColumnSet; | |
| 172 using views::GridLayout; | |
| 173 | |
| 174 // TODO(sky): Vertical alignment should be baseline. | |
| 175 GridLayout* layout = CreatePanelGridLayout(this); | |
| 176 SetLayoutManager(layout); | |
| 177 | |
| 178 ColumnSet* c1 = layout->AddColumnSet(0); | |
| 179 c1->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, | |
| 180 GridLayout::USE_PREF, 0, 0); | |
| 181 c1->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | |
| 182 c1->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, | |
| 183 GridLayout::USE_PREF, kTextfieldWidth, kTextfieldWidth); | |
| 184 | |
| 185 layout->StartRow(0, 0); | |
| 186 views::Label* label = new views::Label(delegate_->label()); | |
| 187 layout->AddView(label); | |
| 188 layout->AddView(text_field_); | |
| 189 | |
| 190 MessageLoop::current()->PostTask(FROM_HERE, | |
| 191 focus_grabber_factory_.NewRunnableMethod( | |
| 192 &ContentView::FocusFirstFocusableControl)); | |
| 193 } | |
| 194 | |
| 195 void ContentView::FocusFirstFocusableControl() { | |
| 196 text_field_->SelectAll(); | |
| 197 text_field_->RequestFocus(); | |
| 198 } | |
| 199 | |
| 200 WinInputWindowDialog::WinInputWindowDialog(HWND parent, | |
| 201 const std::wstring& window_title, | |
| 202 const std::wstring& label, | |
| 203 const std::wstring& contents, | |
| 204 Delegate* delegate) | |
| 205 : window_title_(window_title), | |
| 206 label_(label), | |
| 207 contents_(contents), | |
| 208 delegate_(delegate) { | |
| 209 window_ = views::Window::CreateChromeWindow(parent, gfx::Rect(), | |
| 210 new ContentView(this)); | |
| 211 window_->GetClientView()->AsDialogClientView()->UpdateDialogButtons(); | |
| 212 } | |
| 213 | |
| 214 WinInputWindowDialog::~WinInputWindowDialog() { | |
| 215 } | |
| 216 | |
| 217 void WinInputWindowDialog::Show() { | |
| 218 window_->Show(); | |
| 219 } | |
| 220 | |
| 221 void WinInputWindowDialog::Close() { | |
| 222 window_->Close(); | |
| 223 } | |
| 224 | |
| 225 // static | |
| 226 InputWindowDialog* InputWindowDialog::Create(HWND parent, | |
| 227 const std::wstring& window_title, | |
| 228 const std::wstring& label, | |
| 229 const std::wstring& contents, | |
| 230 Delegate* delegate) { | |
| 231 return new WinInputWindowDialog(parent, | |
| 232 window_title, | |
| 233 label, | |
| 234 contents, | |
| 235 delegate); | |
| 236 } | |
| OLD | NEW |