diff --git a/browser/api/atom_api_menu.cc b/browser/api/atom_api_menu.cc index 3c82c58f71cd..43406666e93f 100644 --- a/browser/api/atom_api_menu.cc +++ b/browser/api/atom_api_menu.cc @@ -4,10 +4,30 @@ #include "browser/api/atom_api_menu.h" +#define UNWRAP_MEMNU_AND_CHECK \ + Menu* self = ObjectWrap::Unwrap(args.This()); \ + if (self == NULL) \ + return node::ThrowError("Menu is already destroyed") + namespace atom { namespace api { +namespace { + +// Converts a V8 value to a string16. +string16 V8ValueToUTF16(v8::Handle value) { + v8::String::Value s(value); + return string16(reinterpret_cast(*s), s.length()); +} + +// Converts string16 to V8 String. +v8::Handle UTF16ToV8Value(const string16& s) { + return v8::String::New(reinterpret_cast(s.data()), s.size()); +} + +} // namespace + Menu::Menu(v8::Handle wrapper) : EventEmitter(wrapper), model_(new ui::SimpleMenuModel(this)) { @@ -44,6 +64,194 @@ v8::Handle Menu::New(const v8::Arguments &args) { return args.This(); } +// static +v8::Handle Menu::InsertItem(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + + if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString()) + return node::ThrowTypeError("Bad argument"); + + int index = args[0]->IntegerValue(); + + if (index < 0) + self->model_->AddItem(args[1]->IntegerValue(), V8ValueToUTF16(args[2])); + else + self->model_->InsertItemAt( + index, args[1]->IntegerValue(), V8ValueToUTF16(args[2])); + + return v8::Undefined(); +} + +// static +v8::Handle Menu::InsertCheckItem(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + + if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString()) + return node::ThrowTypeError("Bad argument"); + + int index = args[0]->IntegerValue(); + + if (index < 0) + self->model_->AddCheckItem(args[1]->IntegerValue(), V8ValueToUTF16(args[2])); + else + self->model_->InsertCheckItemAt( + index, args[1]->IntegerValue(), V8ValueToUTF16(args[2])); + + return v8::Undefined(); +} + +// static +v8::Handle Menu::InsertRadioItem(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + + if (!args[0]->IsNumber() || + !args[1]->IsNumber() || + !args[2]->IsString() || + !args[3]->IsNumber()) + return node::ThrowTypeError("Bad argument"); + + int index = args[0]->IntegerValue(); + int command_id = args[1]->IntegerValue(); + int group_id = args[3]->IntegerValue(); + + if (index < 0) + self->model_->AddRadioItem(command_id, V8ValueToUTF16(args[2]), group_id); + else + self->model_->InsertRadioItemAt( + index, command_id, V8ValueToUTF16(args[2]), group_id); + + return v8::Undefined(); +} + +// static +v8::Handle Menu::InsertSeparator(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + + if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString()) + return node::ThrowTypeError("Bad argument"); + + int index = args[0]->IntegerValue(); + + if (index < 0) + self->model_->AddSeparator(ui::NORMAL_SEPARATOR); + else + self->model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR); + + return v8::Undefined(); +} + +// static +v8::Handle Menu::InsertSubMenu(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + + // FIXME should rely on js code to keep a reference of submenu and check + // the constructor type of menu object. + + if (!args[0]->IsNumber() || + !args[1]->IsNumber() || + !args[2]->IsString() || + !args[3]->IsObject()) + return node::ThrowTypeError("Bad argument"); + + Menu* submenu = ObjectWrap::Unwrap(args[3]->ToObject()); + if (!submenu) + return node::ThrowTypeError("The submenu is already destroyed"); + + int index = args[0]->IntegerValue(); + int command_id = args[1]->IntegerValue(); + + if (index < 0) + self->model_->AddSubMenu( + command_id, V8ValueToUTF16(args[2]), submenu->model_.get()); + else + self->model_->InsertSubMenuAt( + index, command_id, V8ValueToUTF16(args[2]), submenu->model_.get()); + + return v8::Undefined(); +} + +// static +v8::Handle Menu::SetIcon(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + + if (!args[0]->IsNumber() || !args[1]->IsString()) + return node::ThrowTypeError("Bad argument"); + + // FIXME use webkit_glue's image decoder here. + + return v8::Undefined(); +} + +// static +v8::Handle Menu::SetSublabel(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + + if (!args[0]->IsNumber() || !args[1]->IsString()) + return node::ThrowTypeError("Bad argument"); + + self->model_->SetSublabel(args[0]->IntegerValue(), V8ValueToUTF16(args[1])); + + return v8::Undefined(); +} + +// static +v8::Handle Menu::Clear(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + + self->model_->Clear(); + + return v8::Undefined(); +} + +// static +v8::Handle Menu::GetIndexOfCommandId(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + return v8::Integer::New(self->model_->GetIndexOfCommandId(args[0]->IntegerValue())); +} + +// static +v8::Handle Menu::GetItemCount(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + return v8::Integer::New(self->model_->GetItemCount()); +} + +// static +v8::Handle Menu::GetCommandIdAt(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + return v8::Integer::New(self->model_->GetCommandIdAt(args[0]->IntegerValue())); +} + +// static +v8::Handle Menu::GetLabelAt(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + return UTF16ToV8Value(self->model_->GetLabelAt(args[0]->IntegerValue())); +} + +// static +v8::Handle Menu::GetSublabelAt(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + return UTF16ToV8Value(self->model_->GetSublabelAt(args[0]->IntegerValue())); +} + +// static +v8::Handle Menu::IsItemCheckedAt(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + return v8::Boolean::New( + self->model_->IsItemCheckedAt(args[0]->IntegerValue())); +} + +// static +v8::Handle Menu::IsEnabledAt(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + return v8::Boolean::New(self->model_->IsEnabledAt(args[0]->IntegerValue())); +} + +// static +v8::Handle Menu::IsVisibleAt(const v8::Arguments &args) { + UNWRAP_MEMNU_AND_CHECK; + return v8::Boolean::New(self->model_->IsVisibleAt(args[0]->IntegerValue())); +} + // static void Menu::Initialize(v8::Handle target) { v8::HandleScope scope; @@ -52,6 +260,23 @@ void Menu::Initialize(v8::Handle target) { t->InstanceTemplate()->SetInternalFieldCount(1); t->SetClassName(v8::String::NewSymbol("Menu")); + NODE_SET_PROTOTYPE_METHOD(t, "insertItem", InsertItem); + NODE_SET_PROTOTYPE_METHOD(t, "insertCheckItem", InsertCheckItem); + NODE_SET_PROTOTYPE_METHOD(t, "insertRadioItem", InsertRadioItem); + NODE_SET_PROTOTYPE_METHOD(t, "insertSeparator", InsertSeparator); + NODE_SET_PROTOTYPE_METHOD(t, "insertSubMenu", InsertSubMenu); + NODE_SET_PROTOTYPE_METHOD(t, "setIcon", SetIcon); + NODE_SET_PROTOTYPE_METHOD(t, "setSublabel", SetSublabel); + NODE_SET_PROTOTYPE_METHOD(t, "clear", Clear); + NODE_SET_PROTOTYPE_METHOD(t, "getIndexOfCommandId", GetIndexOfCommandId); + NODE_SET_PROTOTYPE_METHOD(t, "getItemCount", GetItemCount); + NODE_SET_PROTOTYPE_METHOD(t, "getCommandIdAt", GetCommandIdAt); + NODE_SET_PROTOTYPE_METHOD(t, "getLabelAt", GetLabelAt); + NODE_SET_PROTOTYPE_METHOD(t, "getSublabelAt", GetSublabelAt); + NODE_SET_PROTOTYPE_METHOD(t, "isItemCheckedAt", IsItemCheckedAt); + NODE_SET_PROTOTYPE_METHOD(t, "isEnabledAt", IsEnabledAt); + NODE_SET_PROTOTYPE_METHOD(t, "isVisibleAt", IsVisibleAt); + target->Set(v8::String::NewSymbol("Menu"), t->GetFunction()); } diff --git a/browser/api/atom_api_menu.h b/browser/api/atom_api_menu.h index aaabc92e2d1c..96514ab71e18 100644 --- a/browser/api/atom_api_menu.h +++ b/browser/api/atom_api_menu.h @@ -38,6 +38,26 @@ class Menu : public EventEmitter, private: static v8::Handle New(const v8::Arguments &args); + static v8::Handle InsertItem(const v8::Arguments &args); + static v8::Handle InsertCheckItem(const v8::Arguments &args); + static v8::Handle InsertRadioItem(const v8::Arguments &args); + static v8::Handle InsertSeparator(const v8::Arguments &args); + static v8::Handle InsertSubMenu(const v8::Arguments &args); + + static v8::Handle SetIcon(const v8::Arguments &args); + static v8::Handle SetSublabel(const v8::Arguments &args); + + static v8::Handle Clear(const v8::Arguments &args); + + static v8::Handle GetIndexOfCommandId(const v8::Arguments &args); + static v8::Handle GetItemCount(const v8::Arguments &args); + static v8::Handle GetCommandIdAt(const v8::Arguments &args); + static v8::Handle GetLabelAt(const v8::Arguments &args); + static v8::Handle GetSublabelAt(const v8::Arguments &args); + static v8::Handle IsItemCheckedAt(const v8::Arguments &args); + static v8::Handle IsEnabledAt(const v8::Arguments &args); + static v8::Handle IsVisibleAt(const v8::Arguments &args); + DISALLOW_COPY_AND_ASSIGN(Menu); };