summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/input_window.cc
blob: f4c560e06fa748174184838bcaa20716cde1adc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "chrome/browser/views/input_window.h"

#include "chrome/browser/standard_layout.h"
#include "chrome/common/l10n_util.h"
#include "chrome/views/grid_layout.h"
#include "chrome/views/label.h"
#include "chrome/views/text_field.h"
#include "chrome/views/window.h"
#include "generated_resources.h"

// Width to make the text field, in pixels.
static const int kTextFieldWidth = 200;

// ContentView ----------------------------------------------------------------

// ContentView, as the name implies, is the content view for the InputWindow.
// It registers accelerators that accept/cancel the input.

class ContentView : public ChromeViews::View,
                    public ChromeViews::DialogDelegate,
                    public ChromeViews::TextField::Controller {
 public:
  explicit ContentView(InputWindowDelegate* delegate)
      : delegate_(delegate),
        focus_grabber_factory_(this) {
    DCHECK(delegate_);
  }

  // ChromeViews::DialogDelegate overrides:
  virtual bool IsDialogButtonEnabled(DialogButton button) const;
  virtual bool Accept();
  virtual bool Cancel();
  virtual void WindowClosing();
  virtual std::wstring GetWindowTitle() const;
  virtual bool IsModal() const { return true; }
  virtual ChromeViews::View* GetContentsView();

  // ChromeViews::TextField::Controller overrides:
  virtual void ContentsChanged(ChromeViews::TextField* sender,
                               const std::wstring& new_contents);
  virtual void HandleKeystroke(ChromeViews::TextField*, UINT, TCHAR, UINT,
                               UINT) {}

 protected:
  // ChromeViews::View overrides:
  virtual void ViewHierarchyChanged(bool is_add, ChromeViews::View* parent,
                                    ChromeViews::View* child);

 private:
  // Set up dialog controls and layout.
  void InitControlLayout();

  // Sets focus to the first focusable element within the dialog.
  void FocusFirstFocusableControl();

  // The TextField that the user can type into.
  ChromeViews::TextField* text_field_;

  // The delegate that the ContentView uses to communicate changes to the
  // caller.
  InputWindowDelegate* delegate_;

  // Helps us set focus to the first TextField in the window.
  ScopedRunnableMethodFactory<ContentView> focus_grabber_factory_;

  DISALLOW_EVIL_CONSTRUCTORS(ContentView);
};

///////////////////////////////////////////////////////////////////////////////
// ContentView, ChromeViews::DialogDelegate implementation:

bool ContentView::IsDialogButtonEnabled(DialogButton button) const {
  if (button == DIALOGBUTTON_OK && !delegate_->IsValid(text_field_->GetText()))
    return false;
  return true;
}

bool ContentView::Accept() {
  delegate_->InputAccepted(text_field_->GetText());
  return true;
}

bool ContentView::Cancel() {
  delegate_->InputCanceled();
  return true;
}

void ContentView::WindowClosing() {
  delegate_->WindowClosing();
}

std::wstring ContentView::GetWindowTitle() const {
  return delegate_->GetWindowTitle();
}

ChromeViews::View* ContentView::GetContentsView() {
  return this;
}

///////////////////////////////////////////////////////////////////////////////
// ContentView, ChromeViews::TextField::Controller implementation:

void ContentView::ContentsChanged(ChromeViews::TextField* sender,
                                  const std::wstring& new_contents) {
  window()->UpdateDialogButtons();
}

///////////////////////////////////////////////////////////////////////////////
// ContentView, protected:

void ContentView::ViewHierarchyChanged(bool is_add,
                                       ChromeViews::View* parent,
                                       ChromeViews::View* child) {
  if (is_add && child == this)
    InitControlLayout();
}

///////////////////////////////////////////////////////////////////////////////
// ContentView, private:

void ContentView::InitControlLayout() {
  text_field_ = new ChromeViews::TextField;
  text_field_->SetText(delegate_->GetTextFieldContents());
  text_field_->SetController(this);

  using ChromeViews::ColumnSet;
  using ChromeViews::GridLayout;

  // TODO(sky): Vertical alignment should be baseline.
  GridLayout* layout = CreatePanelGridLayout(this);
  SetLayoutManager(layout);

  ColumnSet* c1 = layout->AddColumnSet(0);
  c1->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
                GridLayout::USE_PREF, 0, 0);
  c1->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
  c1->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
                GridLayout::USE_PREF, kTextFieldWidth, kTextFieldWidth);

  layout->StartRow(0, 0);
  ChromeViews::Label* label =
      new ChromeViews::Label(delegate_->GetTextFieldLabel());
  layout->AddView(label);
  layout->AddView(text_field_);

  MessageLoop::current()->PostTask(FROM_HERE,
      focus_grabber_factory_.NewRunnableMethod(
          &ContentView::FocusFirstFocusableControl));
}

void ContentView::FocusFirstFocusableControl() {
  text_field_->SelectAll();
  text_field_->RequestFocus();
}

ChromeViews::Window* CreateInputWindow(HWND parent_hwnd,
                                       InputWindowDelegate* delegate) {
  ChromeViews::Window* window =
      ChromeViews::Window::CreateChromeWindow(parent_hwnd, gfx::Rect(),
                                              new ContentView(delegate));
  window->UpdateDialogButtons();
  return window;
}