Run Tizen Webapps in single process mode
[platform/framework/web/crosswalk-tizen.git] / atom / browser / api / atom_api_menu.cc
1 // Copyright (c) 2013 GitHub, Inc.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
4
5 #include "atom/browser/api/atom_api_menu.h"
6
7 #include "atom/browser/native_window.h"
8 #include "atom/common/native_mate_converters/accelerator_converter.h"
9 #include "atom/common/native_mate_converters/callback.h"
10 #include "atom/common/native_mate_converters/image_converter.h"
11 #include "atom/common/native_mate_converters/string16_converter.h"
12 #include "native_mate/constructor.h"
13 #include "native_mate/dictionary.h"
14 #include "native_mate/object_template_builder.h"
15 #include "tizen/common/env_variables.h"
16
17 #include "atom/common/node_includes.h"
18
19 namespace atom {
20
21 namespace api {
22
23 Menu::Menu(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
24     : model_(new AtomMenuModel(this)),
25       parent_(nullptr) {
26   InitWith(isolate, wrapper);
27 }
28
29 Menu::~Menu() {
30 }
31
32 void Menu::AfterInit(v8::Isolate* isolate) {
33   mate::Dictionary wrappable(isolate, GetWrapper());
34   mate::Dictionary delegate;
35   if (!wrappable.Get("delegate", &delegate))
36     return;
37
38   delegate.Get("isCommandIdChecked", &is_checked_);
39   delegate.Get("isCommandIdEnabled", &is_enabled_);
40   delegate.Get("isCommandIdVisible", &is_visible_);
41   delegate.Get("getAcceleratorForCommandId", &get_accelerator_);
42   delegate.Get("executeCommand", &execute_command_);
43   delegate.Get("menuWillShow", &menu_will_show_);
44 }
45
46 bool Menu::IsCommandIdChecked(int command_id) const {
47   return is_checked_.Run(command_id);
48 }
49
50 bool Menu::IsCommandIdEnabled(int command_id) const {
51   return is_enabled_.Run(command_id);
52 }
53
54 bool Menu::IsCommandIdVisible(int command_id) const {
55   return is_visible_.Run(command_id);
56 }
57
58 bool Menu::GetAcceleratorForCommandIdWithParams(
59     int command_id,
60     bool use_default_accelerator,
61     ui::Accelerator* accelerator) const {
62   if (!::tizen::is_single_process)
63     v8::Locker locker(isolate());
64   v8::HandleScope handle_scope(isolate());
65   v8::Local<v8::Value> val = get_accelerator_.Run(
66       command_id, use_default_accelerator);
67   return mate::ConvertFromV8(isolate(), val, accelerator);
68 }
69
70 void Menu::ExecuteCommand(int command_id, int flags) {
71   execute_command_.Run(
72       mate::internal::CreateEventFromFlags(isolate(), flags),
73       command_id);
74 }
75
76 void Menu::MenuWillShow(ui::SimpleMenuModel* source) {
77   menu_will_show_.Run();
78 }
79
80 void Menu::InsertItemAt(
81     int index, int command_id, const base::string16& label) {
82   model_->InsertItemAt(index, command_id, label);
83 }
84
85 void Menu::InsertSeparatorAt(int index) {
86   model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
87 }
88
89 void Menu::InsertCheckItemAt(int index,
90                              int command_id,
91                              const base::string16& label) {
92   model_->InsertCheckItemAt(index, command_id, label);
93 }
94
95 void Menu::InsertRadioItemAt(int index,
96                              int command_id,
97                              const base::string16& label,
98                              int group_id) {
99   model_->InsertRadioItemAt(index, command_id, label, group_id);
100 }
101
102 void Menu::InsertSubMenuAt(int index,
103                            int command_id,
104                            const base::string16& label,
105                            Menu* menu) {
106   menu->parent_ = this;
107   model_->InsertSubMenuAt(index, command_id, label, menu->model_.get());
108 }
109
110 void Menu::SetIcon(int index, const gfx::Image& image) {
111   model_->SetIcon(index, image);
112 }
113
114 void Menu::SetSublabel(int index, const base::string16& sublabel) {
115   model_->SetSublabel(index, sublabel);
116 }
117
118 void Menu::SetRole(int index, const base::string16& role) {
119   model_->SetRole(index, role);
120 }
121
122 void Menu::Clear() {
123   model_->Clear();
124 }
125
126 int Menu::GetIndexOfCommandId(int command_id) {
127   return model_->GetIndexOfCommandId(command_id);
128 }
129
130 int Menu::GetItemCount() const {
131   return model_->GetItemCount();
132 }
133
134 int Menu::GetCommandIdAt(int index) const {
135   return model_->GetCommandIdAt(index);
136 }
137
138 base::string16 Menu::GetLabelAt(int index) const {
139   return model_->GetLabelAt(index);
140 }
141
142 base::string16 Menu::GetSublabelAt(int index) const {
143   return model_->GetSublabelAt(index);
144 }
145
146 bool Menu::IsItemCheckedAt(int index) const {
147   return model_->IsItemCheckedAt(index);
148 }
149
150 bool Menu::IsEnabledAt(int index) const {
151   return model_->IsEnabledAt(index);
152 }
153
154 bool Menu::IsVisibleAt(int index) const {
155   return model_->IsVisibleAt(index);
156 }
157
158 // static
159 void Menu::BuildPrototype(v8::Isolate* isolate,
160                           v8::Local<v8::FunctionTemplate> prototype) {
161   prototype->SetClassName(mate::StringToV8(isolate, "Menu"));
162   mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
163       .MakeDestroyable()
164       .SetMethod("insertItem", &Menu::InsertItemAt)
165       .SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
166       .SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
167       .SetMethod("insertSeparator", &Menu::InsertSeparatorAt)
168       .SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
169       .SetMethod("setIcon", &Menu::SetIcon)
170       .SetMethod("setSublabel", &Menu::SetSublabel)
171       .SetMethod("setRole", &Menu::SetRole)
172       .SetMethod("clear", &Menu::Clear)
173       .SetMethod("getIndexOfCommandId", &Menu::GetIndexOfCommandId)
174       .SetMethod("getItemCount", &Menu::GetItemCount)
175       .SetMethod("getCommandIdAt", &Menu::GetCommandIdAt)
176       .SetMethod("getLabelAt", &Menu::GetLabelAt)
177       .SetMethod("getSublabelAt", &Menu::GetSublabelAt)
178       .SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
179       .SetMethod("isEnabledAt", &Menu::IsEnabledAt)
180       .SetMethod("isVisibleAt", &Menu::IsVisibleAt)
181       .SetMethod("popupAt", &Menu::PopupAt)
182       .SetMethod("closePopupAt", &Menu::ClosePopupAt);
183 }
184
185 }  // namespace api
186
187 }  // namespace atom
188
189
190 namespace {
191
192 using atom::api::Menu;
193
194 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
195                 v8::Local<v8::Context> context, void* priv) {
196   v8::Isolate* isolate = context->GetIsolate();
197   Menu::SetConstructor(isolate, base::Bind(&Menu::New));
198
199   mate::Dictionary dict(isolate, exports);
200   dict.Set("Menu", Menu::GetConstructor(isolate)->GetFunction());
201 #if defined(OS_MACOSX)
202   dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu);
203   dict.SetMethod("sendActionToFirstResponder",
204                  &Menu::SendActionToFirstResponder);
205 #endif
206 }
207
208 }  // namespace
209
210 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_menu, Initialize)