123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371 |
- /**************************************************************************/
- /* native_menu_macos.mm */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /**************************************************************************/
- #include "native_menu_macos.h"
- #include "display_server_macos.h"
- #include "godot_menu_item.h"
- #include "key_mapping_macos.h"
- #include "scene/resources/image_texture.h"
- void NativeMenuMacOS::_register_system_menus(NSMenu *p_main_menu, NSMenu *p_application_menu, NSMenu *p_window_menu, NSMenu *p_help_menu, NSMenu *p_dock_menu) {
- {
- MenuData *md = memnew(MenuData);
- md->menu = p_main_menu;
- md->is_system = true;
- main_menu = menus.make_rid(md);
- main_menu_ns = p_main_menu;
- menu_lookup[md->menu] = main_menu;
- }
- {
- MenuData *md = memnew(MenuData);
- md->menu = p_application_menu;
- md->is_system = true;
- application_menu = menus.make_rid(md);
- application_menu_ns = p_application_menu;
- menu_lookup[md->menu] = application_menu;
- }
- {
- MenuData *md = memnew(MenuData);
- md->menu = p_window_menu;
- md->is_system = true;
- window_menu = menus.make_rid(md);
- window_menu_ns = p_window_menu;
- menu_lookup[md->menu] = window_menu;
- }
- {
- MenuData *md = memnew(MenuData);
- md->menu = p_help_menu;
- md->is_system = true;
- help_menu = menus.make_rid(md);
- help_menu_ns = p_help_menu;
- menu_lookup[md->menu] = help_menu;
- }
- {
- MenuData *md = memnew(MenuData);
- md->menu = p_dock_menu;
- md->is_system = true;
- dock_menu = menus.make_rid(md);
- dock_menu_ns = p_dock_menu;
- menu_lookup[md->menu] = dock_menu;
- }
- }
- NSMenu *NativeMenuMacOS::_get_dock_menu() {
- MenuData *md = menus.get_or_null(dock_menu);
- if (md) {
- return md->menu;
- }
- return nullptr;
- }
- void NativeMenuMacOS::_menu_open(NSMenu *p_menu) {
- if (menu_lookup.has(p_menu)) {
- MenuData *md = menus.get_or_null(menu_lookup[p_menu]);
- if (md) {
- md->is_open = true;
- if (md->open_cb.is_valid()) {
- Variant ret;
- Callable::CallError ce;
- md->open_cb.callp(nullptr, 0, ret, ce);
- if (ce.error != Callable::CallError::CALL_OK) {
- ERR_PRINT(vformat("Failed to execute menu open callback: %s.", Variant::get_callable_error_text(md->open_cb, nullptr, 0, ce)));
- }
- }
- }
- }
- }
- void NativeMenuMacOS::_menu_close(NSMenu *p_menu) {
- if (menu_lookup.has(p_menu)) {
- MenuData *md = menus.get_or_null(menu_lookup[p_menu]);
- if (md) {
- md->is_open = false;
- if (md->close_cb.is_valid()) {
- Variant ret;
- Callable::CallError ce;
- md->close_cb.callp(nullptr, 0, ret, ce);
- if (ce.error != Callable::CallError::CALL_OK) {
- ERR_PRINT(vformat("Failed to execute menu close callback: %s.", Variant::get_callable_error_text(md->close_cb, nullptr, 0, ce)));
- }
- }
- }
- }
- }
- bool NativeMenuMacOS::_is_menu_opened(NSMenu *p_menu) const {
- if (menu_lookup.has(p_menu)) {
- const MenuData *md = menus.get_or_null(menu_lookup[p_menu]);
- if (md && md->is_open) {
- return true;
- }
- }
- for (NSInteger i = (p_menu == [NSApp mainMenu]) ? 1 : 0; i < [p_menu numberOfItems]; i++) {
- const NSMenuItem *menu_item = [p_menu itemAtIndex:i];
- if ([menu_item submenu]) {
- if (_is_menu_opened([menu_item submenu])) {
- return true;
- }
- }
- }
- return false;
- }
- int NativeMenuMacOS::_get_system_menu_start(const NSMenu *p_menu) const {
- if (p_menu == [NSApp mainMenu]) { // Skip Apple menu.
- return 1;
- }
- if (p_menu == application_menu_ns || p_menu == window_menu_ns || p_menu == help_menu_ns) {
- int count = [p_menu numberOfItems];
- for (int i = 0; i < count; i++) {
- NSMenuItem *menu_item = [p_menu itemAtIndex:i];
- if (menu_item.tag == MENU_TAG_START) {
- return i + 1;
- }
- }
- }
- return 0;
- }
- int NativeMenuMacOS::_get_system_menu_count(const NSMenu *p_menu) const {
- if (p_menu == [NSApp mainMenu]) { // Skip Apple, Window and Help menu.
- return [p_menu numberOfItems] - 3;
- }
- if (p_menu == application_menu_ns || p_menu == window_menu_ns || p_menu == help_menu_ns) {
- int start = 0;
- int count = [p_menu numberOfItems];
- for (int i = 0; i < count; i++) {
- NSMenuItem *menu_item = [p_menu itemAtIndex:i];
- if (menu_item.tag == MENU_TAG_START) {
- start = i + 1;
- }
- if (menu_item.tag == MENU_TAG_END) {
- return i - start;
- }
- }
- }
- return [p_menu numberOfItems];
- }
- bool NativeMenuMacOS::has_feature(Feature p_feature) const {
- switch (p_feature) {
- case FEATURE_GLOBAL_MENU:
- case FEATURE_POPUP_MENU:
- case FEATURE_OPEN_CLOSE_CALLBACK:
- case FEATURE_HOVER_CALLBACK:
- case FEATURE_KEY_CALLBACK:
- return true;
- default:
- return false;
- }
- }
- bool NativeMenuMacOS::has_system_menu(SystemMenus p_menu_id) const {
- switch (p_menu_id) {
- case MAIN_MENU_ID:
- case APPLICATION_MENU_ID:
- case WINDOW_MENU_ID:
- case HELP_MENU_ID:
- case DOCK_MENU_ID:
- return true;
- default:
- return false;
- }
- }
- RID NativeMenuMacOS::get_system_menu(SystemMenus p_menu_id) const {
- switch (p_menu_id) {
- case MAIN_MENU_ID:
- return main_menu;
- case APPLICATION_MENU_ID:
- return application_menu;
- case WINDOW_MENU_ID:
- return window_menu;
- case HELP_MENU_ID:
- return help_menu;
- case DOCK_MENU_ID:
- return dock_menu;
- default:
- return RID();
- }
- }
- RID NativeMenuMacOS::create_menu() {
- MenuData *md = memnew(MenuData);
- md->menu = [[NSMenu alloc] initWithTitle:@""];
- [md->menu setAutoenablesItems:NO];
- DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
- if (ds) {
- ds->set_menu_delegate(md->menu);
- }
- RID rid = menus.make_rid(md);
- menu_lookup[md->menu] = rid;
- return rid;
- }
- bool NativeMenuMacOS::has_menu(const RID &p_rid) const {
- return menus.owns(p_rid);
- }
- void NativeMenuMacOS::free_menu(const RID &p_rid) {
- MenuData *md = menus.get_or_null(p_rid);
- if (md && !md->is_system) {
- clear(p_rid);
- menus.free(p_rid);
- menu_lookup.erase(md->menu);
- md->menu = nullptr;
- memdelete(md);
- }
- }
- NSMenu *NativeMenuMacOS::get_native_menu_handle(const RID &p_rid) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, nullptr);
- return md->menu;
- }
- Size2 NativeMenuMacOS::get_size(const RID &p_rid) const {
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, Size2());
- return Size2(md->menu.size.width, md->menu.size.height) * DisplayServer::get_singleton()->screen_get_max_scale();
- }
- void NativeMenuMacOS::popup(const RID &p_rid, const Vector2i &p_position) {
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
- if (ds) {
- Point2i position = p_position;
- // macOS native y-coordinate relative to _get_screens_origin() is negative,
- // Godot passes a positive value.
- position.y *= -1;
- position += ds->_get_screens_origin();
- position /= ds->screen_get_max_scale();
- [md->menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(position.x, position.y) inView:nil];
- }
- }
- void NativeMenuMacOS::set_interface_direction(const RID &p_rid, bool p_is_rtl) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- md->menu.userInterfaceLayoutDirection = p_is_rtl ? NSUserInterfaceLayoutDirectionRightToLeft : NSUserInterfaceLayoutDirectionLeftToRight;
- }
- void NativeMenuMacOS::set_popup_open_callback(const RID &p_rid, const Callable &p_callback) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- md->open_cb = p_callback;
- }
- Callable NativeMenuMacOS::get_popup_open_callback(const RID &p_rid) const {
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, Callable());
- return md->open_cb;
- }
- void NativeMenuMacOS::set_popup_close_callback(const RID &p_rid, const Callable &p_callback) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- md->close_cb = p_callback;
- }
- Callable NativeMenuMacOS::get_popup_close_callback(const RID &p_rid) const {
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, Callable());
- return md->close_cb;
- }
- void NativeMenuMacOS::set_minimum_width(const RID &p_rid, float p_width) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- md->menu.minimumWidth = p_width / DisplayServer::get_singleton()->screen_get_max_scale();
- }
- float NativeMenuMacOS::get_minimum_width(const RID &p_rid) const {
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, 0.0);
- return md->menu.minimumWidth * DisplayServer::get_singleton()->screen_get_max_scale();
- }
- int NativeMenuMacOS::add_submenu_item(const RID &p_rid, const String &p_label, const RID &p_submenu_rid, const Variant &p_tag, int p_index) {
- MenuData *md = menus.get_or_null(p_rid);
- MenuData *md_sub = menus.get_or_null(p_submenu_rid);
- ERR_FAIL_NULL_V(md, -1);
- ERR_FAIL_NULL_V(md_sub, -1);
- ERR_FAIL_COND_V_MSG(md->menu == md_sub->menu, -1, "Can't set submenu to self!");
- ERR_FAIL_COND_V_MSG([md_sub->menu supermenu], -1, "Can't set submenu to menu that is already a submenu of some other menu!");
- NSMenuItem *menu_item;
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- if (p_index < 0) {
- p_index = item_start + item_count;
- } else {
- p_index += item_start;
- p_index = CLAMP(p_index, item_start, item_start + item_count);
- }
- menu_item = [md->menu insertItemWithTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()] action:nil keyEquivalent:@"" atIndex:p_index];
- GodotMenuItem *obj = [[GodotMenuItem alloc] init];
- obj->callback = Callable();
- obj->key_callback = Callable();
- obj->meta = p_tag;
- obj->checkable_type = CHECKABLE_TYPE_NONE;
- obj->max_states = 0;
- obj->state = 0;
- [menu_item setRepresentedObject:obj];
- [md_sub->menu setTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()]];
- [md->menu setSubmenu:md_sub->menu forItem:menu_item];
- return p_index - item_start;
- }
- NSMenuItem *NativeMenuMacOS::_menu_add_item(NSMenu *p_menu, const String &p_label, Key p_accel, int p_index, int *r_out) {
- if (p_menu) {
- String keycode = KeyMappingMacOS::keycode_get_native_string(p_accel & KeyModifierMask::CODE_MASK);
- NSMenuItem *menu_item;
- int item_start = _get_system_menu_start(p_menu);
- int item_count = _get_system_menu_count(p_menu);
- if (p_index < 0) {
- p_index = item_start + item_count;
- } else {
- p_index += item_start;
- p_index = CLAMP(p_index, item_start, item_start + item_count);
- }
- menu_item = [p_menu insertItemWithTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()] action:@selector(globalMenuCallback:) keyEquivalent:[NSString stringWithUTF8String:keycode.utf8().get_data()] atIndex:p_index];
- *r_out = p_index - item_start;
- return menu_item;
- }
- return nullptr;
- }
- int NativeMenuMacOS::add_item(const RID &p_rid, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, -1);
- int out = -1;
- NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
- if (menu_item) {
- GodotMenuItem *obj = [[GodotMenuItem alloc] init];
- obj->callback = p_callback;
- obj->key_callback = p_key_callback;
- obj->meta = p_tag;
- obj->checkable_type = CHECKABLE_TYPE_NONE;
- obj->max_states = 0;
- obj->state = 0;
- [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
- [menu_item setRepresentedObject:obj];
- }
- return out;
- }
- int NativeMenuMacOS::add_check_item(const RID &p_rid, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, -1);
- int out = -1;
- NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
- if (menu_item) {
- GodotMenuItem *obj = [[GodotMenuItem alloc] init];
- obj->callback = p_callback;
- obj->key_callback = p_key_callback;
- obj->meta = p_tag;
- obj->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
- obj->max_states = 0;
- obj->state = 0;
- [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
- [menu_item setRepresentedObject:obj];
- }
- return out;
- }
- int NativeMenuMacOS::add_icon_item(const RID &p_rid, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, -1);
- int out = -1;
- NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
- if (menu_item) {
- GodotMenuItem *obj = [[GodotMenuItem alloc] init];
- obj->callback = p_callback;
- obj->key_callback = p_key_callback;
- obj->meta = p_tag;
- obj->checkable_type = CHECKABLE_TYPE_NONE;
- obj->max_states = 0;
- obj->state = 0;
- DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
- if (ds && p_icon.is_valid()) {
- obj->img = p_icon->get_image();
- obj->img = obj->img->duplicate();
- if (obj->img->is_compressed()) {
- obj->img->decompress();
- }
- NSImage *image = ds->_convert_to_nsimg(obj->img);
- [image setSize:NSMakeSize(16, 16)];
- [menu_item setImage:image];
- }
- [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
- [menu_item setRepresentedObject:obj];
- }
- return out;
- }
- int NativeMenuMacOS::add_icon_check_item(const RID &p_rid, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, -1);
- int out = -1;
- NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
- if (menu_item) {
- GodotMenuItem *obj = [[GodotMenuItem alloc] init];
- obj->callback = p_callback;
- obj->key_callback = p_key_callback;
- obj->meta = p_tag;
- obj->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
- obj->max_states = 0;
- obj->state = 0;
- DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
- if (ds && p_icon.is_valid()) {
- obj->img = p_icon->get_image();
- obj->img = obj->img->duplicate();
- if (obj->img->is_compressed()) {
- obj->img->decompress();
- }
- NSImage *image = ds->_convert_to_nsimg(obj->img);
- [image setSize:NSMakeSize(16, 16)];
- [menu_item setImage:image];
- }
- [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
- [menu_item setRepresentedObject:obj];
- }
- return out;
- }
- int NativeMenuMacOS::add_radio_check_item(const RID &p_rid, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, -1);
- int out = -1;
- NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
- if (menu_item) {
- GodotMenuItem *obj = [[GodotMenuItem alloc] init];
- obj->callback = p_callback;
- obj->key_callback = p_key_callback;
- obj->meta = p_tag;
- obj->checkable_type = CHECKABLE_TYPE_RADIO_BUTTON;
- obj->max_states = 0;
- obj->state = 0;
- [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
- [menu_item setRepresentedObject:obj];
- }
- return out;
- }
- int NativeMenuMacOS::add_icon_radio_check_item(const RID &p_rid, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, -1);
- int out = -1;
- NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
- if (menu_item) {
- GodotMenuItem *obj = [[GodotMenuItem alloc] init];
- obj->callback = p_callback;
- obj->key_callback = p_key_callback;
- obj->meta = p_tag;
- obj->checkable_type = CHECKABLE_TYPE_RADIO_BUTTON;
- obj->max_states = 0;
- obj->state = 0;
- DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
- if (ds && p_icon.is_valid()) {
- obj->img = p_icon->get_image();
- obj->img = obj->img->duplicate();
- if (obj->img->is_compressed()) {
- obj->img->decompress();
- }
- NSImage *image = ds->_convert_to_nsimg(obj->img);
- [image setSize:NSMakeSize(16, 16)];
- [menu_item setImage:image];
- }
- [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
- [menu_item setRepresentedObject:obj];
- }
- return out;
- }
- int NativeMenuMacOS::add_multistate_item(const RID &p_rid, const String &p_label, int p_max_states, int p_default_state, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, -1);
- int out = -1;
- NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
- if (menu_item) {
- GodotMenuItem *obj = [[GodotMenuItem alloc] init];
- obj->callback = p_callback;
- obj->key_callback = p_key_callback;
- obj->meta = p_tag;
- obj->checkable_type = CHECKABLE_TYPE_NONE;
- obj->max_states = p_max_states;
- obj->state = p_default_state;
- [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
- [menu_item setRepresentedObject:obj];
- }
- return out;
- }
- int NativeMenuMacOS::add_separator(const RID &p_rid, int p_index) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, -1);
- if (md->menu == [NSApp mainMenu]) { // Do not add separators into main menu.
- return -1;
- }
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- if (p_index < 0) {
- p_index = item_start + item_count;
- } else {
- p_index += item_start;
- p_index = CLAMP(p_index, item_start, item_start + item_count);
- }
- [md->menu insertItem:[NSMenuItem separatorItem] atIndex:p_index];
- return p_index - item_start;
- }
- int NativeMenuMacOS::find_item_index_with_text(const RID &p_rid, const String &p_text) const {
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, -1);
- int item_start = _get_system_menu_start(md->menu);
- int index = [md->menu indexOfItemWithTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
- if (index >= 0) {
- return index - item_start;
- }
- return -1;
- }
- int NativeMenuMacOS::find_item_index_with_tag(const RID &p_rid, const Variant &p_tag) const {
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, -1);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- for (NSInteger i = item_start; i < item_start + item_count; i++) {
- const NSMenuItem *menu_item = [md->menu itemAtIndex:i];
- if (menu_item) {
- const GodotMenuItem *obj = [menu_item representedObject];
- if (obj && obj->meta == p_tag) {
- return i - item_start;
- }
- }
- }
- return -1;
- }
- bool NativeMenuMacOS::is_item_checked(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, false);
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, false);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, false);
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- return ([menu_item state] == NSControlStateValueOn);
- }
- return false;
- }
- bool NativeMenuMacOS::is_item_checkable(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, false);
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, false);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, false);
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- if (obj) {
- return obj->checkable_type == CHECKABLE_TYPE_CHECK_BOX;
- }
- }
- return false;
- }
- bool NativeMenuMacOS::is_item_radio_checkable(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, false);
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, false);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, false);
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- if (obj) {
- return obj->checkable_type == CHECKABLE_TYPE_RADIO_BUTTON;
- }
- }
- return false;
- }
- Callable NativeMenuMacOS::get_item_callback(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, Callable());
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, Callable());
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, Callable());
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- if (obj) {
- return obj->callback;
- }
- }
- return Callable();
- }
- Callable NativeMenuMacOS::get_item_key_callback(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, Callable());
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, Callable());
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, Callable());
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- if (obj) {
- return obj->key_callback;
- }
- }
- return Callable();
- }
- Variant NativeMenuMacOS::get_item_tag(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, Variant());
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, Variant());
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, Variant());
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- if (obj) {
- return obj->meta;
- }
- }
- return Variant();
- }
- String NativeMenuMacOS::get_item_text(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, String());
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, String());
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, String());
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- return String::utf8([[menu_item title] UTF8String]);
- }
- return String();
- }
- RID NativeMenuMacOS::get_item_submenu(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, RID());
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, RID());
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, RID());
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- NSMenu *sub_menu = [menu_item submenu];
- if (sub_menu && menu_lookup.has(sub_menu)) {
- return menu_lookup[sub_menu];
- }
- }
- return RID();
- }
- Key NativeMenuMacOS::get_item_accelerator(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, Key::NONE);
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, Key::NONE);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, Key::NONE);
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- String ret = String::utf8([[menu_item keyEquivalent] UTF8String]);
- Key keycode = find_keycode(ret);
- NSUInteger mask = [menu_item keyEquivalentModifierMask];
- if (mask & NSEventModifierFlagControl) {
- keycode |= KeyModifierMask::CTRL;
- }
- if (mask & NSEventModifierFlagOption) {
- keycode |= KeyModifierMask::ALT;
- }
- if (mask & NSEventModifierFlagShift) {
- keycode |= KeyModifierMask::SHIFT;
- }
- if (mask & NSEventModifierFlagCommand) {
- keycode |= KeyModifierMask::META;
- }
- if (mask & NSEventModifierFlagNumericPad) {
- keycode |= KeyModifierMask::KPAD;
- }
- return keycode;
- }
- return Key::NONE;
- }
- bool NativeMenuMacOS::is_item_disabled(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, false);
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, false);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, false);
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- return ![menu_item isEnabled];
- }
- return false;
- }
- bool NativeMenuMacOS::is_item_hidden(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, false);
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, false);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, false);
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- return [menu_item isHidden];
- }
- return false;
- }
- String NativeMenuMacOS::get_item_tooltip(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, String());
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, String());
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, String());
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- return String::utf8([[menu_item toolTip] UTF8String]);
- }
- return String();
- }
- int NativeMenuMacOS::get_item_state(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, 0);
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, 0);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, 0);
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- if (obj) {
- return obj->state;
- }
- }
- return 0;
- }
- int NativeMenuMacOS::get_item_max_states(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, 0);
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, 0);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, 0);
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- if (obj) {
- return obj->max_states;
- }
- }
- return 0;
- }
- Ref<Texture2D> NativeMenuMacOS::get_item_icon(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, Ref<Texture2D>());
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, Ref<Texture2D>());
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, Ref<Texture2D>());
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- if (obj) {
- if (obj->img.is_valid()) {
- return ImageTexture::create_from_image(obj->img);
- }
- }
- }
- return Ref<Texture2D>();
- }
- int NativeMenuMacOS::get_item_indentation_level(const RID &p_rid, int p_idx) const {
- ERR_FAIL_COND_V(p_idx < 0, 0);
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, 0);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND_V(p_idx >= item_start + item_count, 0);
- const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- return [menu_item indentationLevel];
- }
- return 0;
- }
- void NativeMenuMacOS::set_item_checked(const RID &p_rid, int p_idx, bool p_checked) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- if (p_checked) {
- [menu_item setState:NSControlStateValueOn];
- } else {
- [menu_item setState:NSControlStateValueOff];
- }
- }
- }
- void NativeMenuMacOS::set_item_checkable(const RID &p_rid, int p_idx, bool p_checkable) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- ERR_FAIL_NULL(obj);
- obj->checkable_type = (p_checkable) ? CHECKABLE_TYPE_CHECK_BOX : CHECKABLE_TYPE_NONE;
- }
- }
- void NativeMenuMacOS::set_item_radio_checkable(const RID &p_rid, int p_idx, bool p_checkable) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- ERR_FAIL_NULL(obj);
- obj->checkable_type = (p_checkable) ? CHECKABLE_TYPE_RADIO_BUTTON : CHECKABLE_TYPE_NONE;
- }
- }
- void NativeMenuMacOS::set_item_callback(const RID &p_rid, int p_idx, const Callable &p_callback) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- ERR_FAIL_NULL(obj);
- obj->callback = p_callback;
- }
- }
- void NativeMenuMacOS::set_item_key_callback(const RID &p_rid, int p_idx, const Callable &p_key_callback) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- ERR_FAIL_NULL(obj);
- obj->key_callback = p_key_callback;
- }
- }
- void NativeMenuMacOS::set_item_hover_callbacks(const RID &p_rid, int p_idx, const Callable &p_callback) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- ERR_FAIL_NULL(obj);
- obj->hover_callback = p_callback;
- }
- }
- void NativeMenuMacOS::set_item_tag(const RID &p_rid, int p_idx, const Variant &p_tag) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- ERR_FAIL_NULL(obj);
- obj->meta = p_tag;
- }
- }
- void NativeMenuMacOS::set_item_text(const RID &p_rid, int p_idx, const String &p_text) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- [menu_item setTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
- NSMenu *sub_menu = [menu_item submenu];
- if (sub_menu) {
- [sub_menu setTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
- }
- }
- }
- void NativeMenuMacOS::set_item_submenu(const RID &p_rid, int p_idx, const RID &p_submenu_rid) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- if (p_submenu_rid.is_valid()) {
- MenuData *md_sub = menus.get_or_null(p_submenu_rid);
- ERR_FAIL_NULL(md_sub);
- ERR_FAIL_COND_MSG(md->menu == md_sub->menu, "Can't set submenu to self!");
- ERR_FAIL_COND_MSG([md_sub->menu supermenu], "Can't set submenu to menu that is already a submenu of some other menu!");
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- [md->menu setSubmenu:md_sub->menu forItem:menu_item];
- }
- } else {
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- if ([menu_item submenu] && _is_menu_opened([menu_item submenu])) {
- ERR_PRINT("Can't remove open menu!");
- return;
- }
- [md->menu setSubmenu:nil forItem:menu_item];
- }
- }
- }
- void NativeMenuMacOS::set_item_accelerator(const RID &p_rid, int p_idx, Key p_keycode) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- if (p_keycode == Key::NONE) {
- [menu_item setKeyEquivalent:@""];
- } else {
- [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_keycode)];
- String keycode = KeyMappingMacOS::keycode_get_native_string(p_keycode & KeyModifierMask::CODE_MASK);
- [menu_item setKeyEquivalent:[NSString stringWithUTF8String:keycode.utf8().get_data()]];
- }
- }
- }
- void NativeMenuMacOS::set_item_disabled(const RID &p_rid, int p_idx, bool p_disabled) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- [menu_item setEnabled:(!p_disabled)];
- }
- }
- void NativeMenuMacOS::set_item_hidden(const RID &p_rid, int p_idx, bool p_hidden) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- [menu_item setHidden:p_hidden];
- }
- }
- void NativeMenuMacOS::set_item_tooltip(const RID &p_rid, int p_idx, const String &p_tooltip) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- [menu_item setToolTip:[NSString stringWithUTF8String:p_tooltip.utf8().get_data()]];
- }
- }
- void NativeMenuMacOS::set_item_state(const RID &p_rid, int p_idx, int p_state) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- ERR_FAIL_NULL(obj);
- obj->state = p_state;
- }
- }
- void NativeMenuMacOS::set_item_max_states(const RID &p_rid, int p_idx, int p_max_states) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- ERR_FAIL_NULL(obj);
- obj->max_states = p_max_states;
- }
- }
- void NativeMenuMacOS::set_item_icon(const RID &p_rid, int p_idx, const Ref<Texture2D> &p_icon) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- GodotMenuItem *obj = [menu_item representedObject];
- ERR_FAIL_NULL(obj);
- DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
- if (ds && p_icon.is_valid()) {
- obj->img = p_icon->get_image();
- obj->img = obj->img->duplicate();
- if (obj->img->is_compressed()) {
- obj->img->decompress();
- }
- NSImage *image = ds->_convert_to_nsimg(obj->img);
- [image setSize:NSMakeSize(16, 16)];
- [menu_item setImage:image];
- } else {
- obj->img = Ref<Image>();
- [menu_item setImage:nil];
- }
- }
- }
- void NativeMenuMacOS::set_item_indentation_level(const RID &p_rid, int p_idx, int p_level) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if (menu_item) {
- [menu_item setIndentationLevel:p_level];
- }
- }
- int NativeMenuMacOS::get_item_count(const RID &p_rid) const {
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, 0);
- return _get_system_menu_count(md->menu);
- }
- bool NativeMenuMacOS::is_system_menu(const RID &p_rid) const {
- const MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL_V(md, false);
- return md->is_system;
- }
- void NativeMenuMacOS::remove_item(const RID &p_rid, int p_idx) {
- ERR_FAIL_COND(p_idx < 0);
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- int item_start = _get_system_menu_start(md->menu);
- int item_count = _get_system_menu_count(md->menu);
- p_idx += item_start;
- ERR_FAIL_COND(p_idx >= item_start + item_count);
- NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
- if ([menu_item submenu] && _is_menu_opened([menu_item submenu])) {
- ERR_PRINT("Can't remove open menu!");
- return;
- }
- [md->menu removeItemAtIndex:p_idx];
- }
- void NativeMenuMacOS::clear(const RID &p_rid) {
- MenuData *md = menus.get_or_null(p_rid);
- ERR_FAIL_NULL(md);
- ERR_FAIL_COND_MSG(_is_menu_opened(md->menu), "Can't remove open menu!");
- if (p_rid == application_menu || p_rid == window_menu || p_rid == help_menu) {
- int start = _get_system_menu_start(md->menu);
- int count = _get_system_menu_count(md->menu);
- for (int i = start + count - 1; i >= start; i--) {
- [md->menu removeItemAtIndex:i];
- }
- } else {
- [md->menu removeAllItems];
- }
- if (p_rid == main_menu) {
- // Restore Apple, Window and Help menu.
- MenuData *md_app = menus.get_or_null(application_menu);
- if (md_app) {
- NSMenuItem *menu_item = [md->menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
- [md->menu setSubmenu:md_app->menu forItem:menu_item];
- }
- MenuData *md_win = menus.get_or_null(window_menu);
- if (md_win) {
- NSMenuItem *menu_item = [md->menu addItemWithTitle:@"Window" action:nil keyEquivalent:@""];
- [md->menu setSubmenu:md_win->menu forItem:menu_item];
- }
- MenuData *md_hlp = menus.get_or_null(help_menu);
- if (md_hlp) {
- NSMenuItem *menu_item = [md->menu addItemWithTitle:@"Help" action:nil keyEquivalent:@""];
- [md->menu setSubmenu:md_hlp->menu forItem:menu_item];
- }
- }
- }
- NativeMenuMacOS::NativeMenuMacOS() {}
- NativeMenuMacOS::~NativeMenuMacOS() {
- if (main_menu.is_valid()) {
- MenuData *md = menus.get_or_null(main_menu);
- if (md) {
- clear(main_menu);
- menus.free(main_menu);
- menu_lookup.erase(md->menu);
- md->menu = nullptr;
- main_menu_ns = nullptr;
- memdelete(md);
- }
- }
- if (application_menu.is_valid()) {
- MenuData *md = menus.get_or_null(application_menu);
- if (md) {
- clear(application_menu);
- menus.free(application_menu);
- menu_lookup.erase(md->menu);
- md->menu = nullptr;
- application_menu_ns = nullptr;
- memdelete(md);
- }
- }
- if (window_menu.is_valid()) {
- MenuData *md = menus.get_or_null(window_menu);
- if (md) {
- clear(window_menu);
- menus.free(window_menu);
- menu_lookup.erase(md->menu);
- md->menu = nullptr;
- window_menu_ns = nullptr;
- memdelete(md);
- }
- }
- if (help_menu.is_valid()) {
- MenuData *md = menus.get_or_null(help_menu);
- if (md) {
- clear(help_menu);
- menus.free(help_menu);
- menu_lookup.erase(md->menu);
- md->menu = nullptr;
- help_menu_ns = nullptr;
- memdelete(md);
- }
- }
- if (dock_menu.is_valid()) {
- MenuData *md = menus.get_or_null(dock_menu);
- if (md) {
- clear(dock_menu);
- menus.free(dock_menu);
- menu_lookup.erase(md->menu);
- md->menu = nullptr;
- dock_menu_ns = nullptr;
- memdelete(md);
- }
- }
- }
|