summaryrefslogtreecommitdiffstats
path: root/views/examples/textfield_example.h
blob: ec7c625e9561e9a246c0df1bbf91ab68eaa0c4a6 (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
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef VIEWS_EXAMPLES_TEXTFIELD_EXAMPLE_H_
#define VIEWS_EXAMPLES_TEXTFIELD_EXAMPLE_H_

#include "base/string_util.h"
#include "views/controls/button/text_button.h"
#include "views/controls/label.h"
#include "views/controls/tabbed_pane/tabbed_pane.h"
#include "views/controls/textfield/textfield.h"
#include "views/examples/example_base.h"

namespace examples {

using views::Textfield;

// TextfieldExample mimics login screen.
class TextfieldExample : protected ExampleBase,
                         private Textfield::Controller,
                         private views::ButtonListener {
 public:
  TextfieldExample(views::TabbedPane* tabbed_pane, views::Label* message)
      : ExampleBase(message),
        name_(new Textfield()),
        password_(new Textfield(Textfield::STYLE_PASSWORD)),
        show_password_(new views::TextButton(this, L"Show password")),
        clear_all_(new views::TextButton(this, L"Clear All")) {
    name_->SetController(this);
    password_->SetController(this);

    views::View* container = new views::View();
    tabbed_pane->AddTab(L"Textfield", container);
    views::GridLayout* layout = new views::GridLayout(container);
    container->SetLayoutManager(layout);

    views::ColumnSet* column_set = layout->AddColumnSet(0);
    column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL,
                          0.2f, views::GridLayout::USE_PREF, 0, 0);
    column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
                          0.8f, views::GridLayout::USE_PREF, 0, 0);
    layout->StartRow(0, 0);
    layout->AddView(new views::Label(L"Name:"));
    layout->AddView(name_);
    layout->StartRow(0, 0);
    layout->AddView(new views::Label(L"Password:"));
    layout->AddView(password_);
    layout->StartRow(0, 0);
    layout->AddView(show_password_);
    layout->StartRow(0, 0);
    layout->AddView(clear_all_);
  }

  virtual ~TextfieldExample() {}

 private:
  // Textfield::Controller implementations:
  // This method is called whenever the text in the field changes.
  virtual void ContentsChanged(Textfield* sender,
                               const string16& new_contents) {
    if (sender == name_) {
      PrintStatus(L"Name [%ls]", UTF16ToWideHack(new_contents).c_str());
    } else if (sender == password_) {
      PrintStatus(L"Password [%ls]", UTF16ToWideHack(new_contents).c_str());
    }
  }

  // Let the control handle keystrokes.
  virtual bool HandleKeystroke(Textfield* sender,
                               const Textfield::Keystroke& keystroke) {
    return false;
  }

  // ButtonListner implementation.
  virtual void ButtonPressed(views::Button* sender, const views::Event& event) {
    if (sender == show_password_) {
      PrintStatus(L"Password [%ls]",
                  UTF16ToWideHack(password_->text()).c_str());
    } else if (sender == clear_all_) {
      string16 empty;
      name_->SetText(empty);
      password_->SetText(empty);
    }
  }

  // Textfields for name and password.
  views::Textfield* name_;
  views::Textfield* password_;

  // Buttons to show password text and to clear the textfields.
  views::TextButton* show_password_;
  views::TextButton* clear_all_;

  DISALLOW_COPY_AND_ASSIGN(TextfieldExample);
};

}  // namespace examples

#endif  // VIEWS_EXAMPLES_TEXTFIELD_EXAMPLE_H_