summaryrefslogtreecommitdiffstats
path: root/views/examples/menu_example.h
blob: 91c32a47ea502c3057b721d8d003d70de28b3371 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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_MENU_EXAMPLE_H_
#define VIEWS_EXAMPLES_MENU_EXAMPLE_H_
#pragma once

#include <set>

#include "app/menus/simple_menu_model.h"
#include "base/utf_string_conversions.h"
#include "views/controls/button/menu_button.h"
#include "views/controls/menu/menu_2.h"
#include "views/controls/menu/view_menu_delegate.h"
#include "views/controls/button/text_button.h"
#include "views/examples/example_base.h"
#include "views/fill_layout.h"

namespace examples {

class ExampleMenuContents : public menus::SimpleMenuModel,
                            public menus::SimpleMenuModel::Delegate {
  enum {
    kGroupMakeDecision,
  };

  enum {
    kCommandDoSomething,
    kCommandSelectAscii,
    kCommandSelectUtf8,
    kCommandSelectUtf16,
    kCommandCheckApple,
    kCommandCheckOrange,
    kCommandCheckKiwi,
    kCommandGoHome,
  };

 public:
  ExampleMenuContents() :
      ALLOW_THIS_IN_INITIALIZER_LIST(menus::SimpleMenuModel(this)),
      current_encoding_command_id_(kCommandSelectAscii) {

    AddItem(kCommandDoSomething, WideToUTF16(L"Do Something"));
    AddSeparator();
    AddRadioItem(kCommandSelectAscii,
                 WideToUTF16(L"ASCII"), kGroupMakeDecision);
    AddRadioItem(kCommandSelectUtf8,
                 WideToUTF16(L"UTF-8"), kGroupMakeDecision);
    AddRadioItem(kCommandSelectUtf16,
                 WideToUTF16(L"UTF-16"), kGroupMakeDecision);
    AddSeparator();
    AddCheckItem(kCommandCheckApple, WideToUTF16(L"Apple"));
    AddCheckItem(kCommandCheckOrange, WideToUTF16(L"Orange"));
    AddCheckItem(kCommandCheckKiwi, WideToUTF16(L"Kiwi"));
    AddSeparator();
    AddItem(kCommandGoHome, WideToUTF16(L"Go Home"));

    submenu_.reset(new menus::SimpleMenuModel(this));
    submenu_->AddItem(kCommandDoSomething, WideToUTF16(L"Do Something 2"));
    AddSubMenu(-1, ASCIIToUTF16("Submenu"), submenu_.get());
    menu_.reset(new views::Menu2(this));
  }

  void RunMenuAt(const gfx::Point& point) {
    menu_->RunMenuAt(point, views::Menu2::ALIGN_TOPRIGHT);
  }

  // menus::SimpleMenuModel::Delegate implementation.
  virtual bool IsCommandIdChecked(int command_id) const {
    // Radio items.
    if (command_id == current_encoding_command_id_) {
      return true;
    }

    // Check items.
    if (checked_fruits_.find(command_id) != checked_fruits_.end()) {
      return true;
    }
    return false;
  }

  virtual bool IsCommandIdEnabled(int command_id) const {
    // All commands are enabled except for kCommandGoHome.
    return command_id != kCommandGoHome;
  }

  virtual bool GetAcceleratorForCommandId(
      int command_id,
      menus::Accelerator* accelerator) {
    // We don't use this in the example.
    return false;
  }

  virtual void ExecuteCommand(int command_id) {
    switch (command_id) {
      case kCommandDoSomething: {
        LOG(INFO) << "Done something";
        break;
      }

      // Radio items.
      case kCommandSelectAscii: {
        current_encoding_command_id_ = kCommandSelectAscii;
        LOG(INFO) << "Selected ASCII";
        break;
      }
      case kCommandSelectUtf8: {
        current_encoding_command_id_ = kCommandSelectUtf8;
        LOG(INFO) << "Selected UTF-8";
        break;
      }
      case kCommandSelectUtf16: {
        current_encoding_command_id_ = kCommandSelectUtf16;
        LOG(INFO) << "Selected UTF-16";
        break;
      }

      // Check items.
      case kCommandCheckApple:
      case kCommandCheckOrange:
      case kCommandCheckKiwi: {
        // Print what fruit is checked.
        const char* checked_fruit = "";
        if (command_id == kCommandCheckApple) {
          checked_fruit = "Apple";
        } else if (command_id == kCommandCheckOrange) {
          checked_fruit = "Orange";
        } else if (command_id == kCommandCheckKiwi) {
          checked_fruit = "Kiwi";
        }
        LOG(INFO) << "Checked " << checked_fruit;

        // Update the check status.
        std::set<int>::iterator iter = checked_fruits_.find(command_id);
        if (iter == checked_fruits_.end()) {
          checked_fruits_.insert(command_id);
        } else {
          checked_fruits_.erase(iter);
        }
        break;
      }
    }
  }

 private:
  scoped_ptr<views::Menu2> menu_;
  scoped_ptr<menus::SimpleMenuModel> submenu_;
  std::set<int> checked_fruits_;
  int current_encoding_command_id_;

  DISALLOW_COPY_AND_ASSIGN(ExampleMenuContents);
};

class ExampleMenuButton : public views::MenuButton,
                          public views::ViewMenuDelegate {
 public:
  ExampleMenuButton(const std::wstring& test,
                    bool show_menu_marker)
      : ALLOW_THIS_IN_INITIALIZER_LIST(
          views::MenuButton(NULL, test, this, show_menu_marker)) {
  }

 private:
  // views::ViewMenuDelegate implementation.
  virtual void RunMenu(views::View* source, const gfx::Point& point) {
    if (menu_contents_ == NULL) {
      menu_contents_.reset(new ExampleMenuContents);
    }
    menu_contents_->RunMenuAt(point);
  }

  scoped_ptr<ExampleMenuContents> menu_contents_;
  DISALLOW_COPY_AND_ASSIGN(ExampleMenuButton);
};

// MenuExample demonstrates how to use the Menu class.
class MenuExample : public ExampleBase {
 public:
  explicit MenuExample(ExamplesMain* main)
      : ExampleBase(main) {
  }

  virtual ~MenuExample() {}

  virtual std::wstring GetExampleTitle() {
    return L"Menu";
  }

  virtual void CreateExampleView(views::View* container) {
    // views::Menu2 is not a sub class of View, hence we cannot directly
    // add to the continer. Instead, we add a button to open a menu.
    const bool show_menu_marker = true;
    ExampleMenuButton* menu_button = new ExampleMenuButton(
        L"Open a menu", show_menu_marker);
    container->SetLayoutManager(new views::FillLayout);
    container->AddChildView(menu_button);
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(MenuExample);
};

}  // namespace examples

#endif  // VIEWS_EXAMPLES_MENU_EXAMPLE_H_