| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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/views/input_window_dialog_win.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "views/grid_layout.h" |
| 11 #include "views/standard_layout.h" |
| 12 #include "grit/generated_resources.h" |
| 13 |
| 14 // Width to make the text field, in pixels. |
| 15 static const int kTextfieldWidth = 200; |
| 16 |
| 17 InputWindowDialogWin::InputWindowDialogWin(gfx::NativeWindow parent, |
| 18 const std::wstring& window_title, |
| 19 const std::wstring& label, |
| 20 const std::wstring& contents, |
| 21 Delegate* delegate) |
| 22 : window_title_(window_title), |
| 23 label_(label), |
| 24 contents_(contents), |
| 25 delegate_(delegate) { |
| 26 window_ = views::Window::CreateChromeWindow(parent, gfx::Rect(), |
| 27 new ContentView(this)); |
| 28 window_->GetClientView()->AsDialogClientView()->UpdateDialogButtons(); |
| 29 } |
| 30 |
| 31 InputWindowDialogWin::~InputWindowDialogWin() { |
| 32 } |
| 33 |
| 34 void InputWindowDialogWin::Show() { |
| 35 window_->Show(); |
| 36 } |
| 37 |
| 38 void InputWindowDialogWin::Close() { |
| 39 window_->Close(); |
| 40 } |
| 41 |
| 42 /////////////////////////////////////////////////////////////////////////////// |
| 43 // ContentView, views::DialogDelegate implementation: |
| 44 |
| 45 bool ContentView::IsDialogButtonEnabled( |
| 46 MessageBoxFlags::DialogButton button) const { |
| 47 if (button == MessageBoxFlags::DIALOGBUTTON_OK && |
| 48 !delegate_->delegate()->IsValid(text_field_->text())) { |
| 49 return false; |
| 50 } |
| 51 return true; |
| 52 } |
| 53 |
| 54 bool ContentView::Accept() { |
| 55 delegate_->delegate()->InputAccepted(text_field_->text()); |
| 56 return true; |
| 57 } |
| 58 |
| 59 bool ContentView::Cancel() { |
| 60 delegate_->delegate()->InputCanceled(); |
| 61 return true; |
| 62 } |
| 63 |
| 64 std::wstring ContentView::GetWindowTitle() const { |
| 65 return delegate_->window_title(); |
| 66 } |
| 67 |
| 68 /////////////////////////////////////////////////////////////////////////////// |
| 69 // ContentView, views::Textfield::Controller implementation: |
| 70 |
| 71 void ContentView::ContentsChanged(views::Textfield* sender, |
| 72 const std::wstring& new_contents) { |
| 73 GetDialogClientView()->UpdateDialogButtons(); |
| 74 } |
| 75 |
| 76 /////////////////////////////////////////////////////////////////////////////// |
| 77 // ContentView, protected: |
| 78 |
| 79 void ContentView::ViewHierarchyChanged(bool is_add, |
| 80 views::View* parent, |
| 81 views::View* child) { |
| 82 if (is_add && child == this) |
| 83 InitControlLayout(); |
| 84 } |
| 85 |
| 86 /////////////////////////////////////////////////////////////////////////////// |
| 87 // ContentView, private: |
| 88 |
| 89 void ContentView::InitControlLayout() { |
| 90 text_field_ = new views::Textfield; |
| 91 text_field_->SetText(delegate_->contents()); |
| 92 text_field_->SetController(this); |
| 93 |
| 94 using views::ColumnSet; |
| 95 using views::GridLayout; |
| 96 |
| 97 // TODO(sky): Vertical alignment should be baseline. |
| 98 GridLayout* layout = CreatePanelGridLayout(this); |
| 99 SetLayoutManager(layout); |
| 100 |
| 101 ColumnSet* c1 = layout->AddColumnSet(0); |
| 102 c1->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, |
| 103 GridLayout::USE_PREF, 0, 0); |
| 104 c1->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); |
| 105 c1->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, |
| 106 GridLayout::USE_PREF, kTextfieldWidth, kTextfieldWidth); |
| 107 |
| 108 layout->StartRow(0, 0); |
| 109 views::Label* label = new views::Label(delegate_->label()); |
| 110 layout->AddView(label); |
| 111 layout->AddView(text_field_); |
| 112 |
| 113 MessageLoop::current()->PostTask(FROM_HERE, |
| 114 focus_grabber_factory_.NewRunnableMethod( |
| 115 &ContentView::FocusFirstFocusableControl)); |
| 116 } |
| 117 |
| 118 void ContentView::FocusFirstFocusableControl() { |
| 119 text_field_->SelectAll(); |
| 120 text_field_->RequestFocus(); |
| 121 } |
| 122 |
| 123 // static |
| 124 InputWindowDialog* InputWindowDialog::Create(HWND parent, |
| 125 const std::wstring& window_title, |
| 126 const std::wstring& label, |
| 127 const std::wstring& contents, |
| 128 Delegate* delegate) { |
| 129 return new InputWindowDialogWin(parent, |
| 130 window_title, |
| 131 label, |
| 132 contents, |
| 133 delegate); |
| 134 } |
| 135 |
| OLD | NEW |