native_menu_macos.mm 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /**************************************************************************/
  2. /* native_menu_macos.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "native_menu_macos.h"
  31. #include "display_server_macos.h"
  32. #include "godot_menu_item.h"
  33. #include "key_mapping_macos.h"
  34. #include "scene/resources/image_texture.h"
  35. 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) {
  36. {
  37. MenuData *md = memnew(MenuData);
  38. md->menu = p_main_menu;
  39. md->is_system = true;
  40. main_menu = menus.make_rid(md);
  41. main_menu_ns = p_main_menu;
  42. menu_lookup[md->menu] = main_menu;
  43. }
  44. {
  45. MenuData *md = memnew(MenuData);
  46. md->menu = p_application_menu;
  47. md->is_system = true;
  48. application_menu = menus.make_rid(md);
  49. application_menu_ns = p_application_menu;
  50. menu_lookup[md->menu] = application_menu;
  51. }
  52. {
  53. MenuData *md = memnew(MenuData);
  54. md->menu = p_window_menu;
  55. md->is_system = true;
  56. window_menu = menus.make_rid(md);
  57. window_menu_ns = p_window_menu;
  58. menu_lookup[md->menu] = window_menu;
  59. }
  60. {
  61. MenuData *md = memnew(MenuData);
  62. md->menu = p_help_menu;
  63. md->is_system = true;
  64. help_menu = menus.make_rid(md);
  65. help_menu_ns = p_help_menu;
  66. menu_lookup[md->menu] = help_menu;
  67. }
  68. {
  69. MenuData *md = memnew(MenuData);
  70. md->menu = p_dock_menu;
  71. md->is_system = true;
  72. dock_menu = menus.make_rid(md);
  73. dock_menu_ns = p_dock_menu;
  74. menu_lookup[md->menu] = dock_menu;
  75. }
  76. }
  77. NSMenu *NativeMenuMacOS::_get_dock_menu() {
  78. MenuData *md = menus.get_or_null(dock_menu);
  79. if (md) {
  80. return md->menu;
  81. }
  82. return nullptr;
  83. }
  84. void NativeMenuMacOS::_menu_open(NSMenu *p_menu) {
  85. if (menu_lookup.has(p_menu)) {
  86. MenuData *md = menus.get_or_null(menu_lookup[p_menu]);
  87. if (md) {
  88. md->is_open = true;
  89. if (md->open_cb.is_valid()) {
  90. Variant ret;
  91. Callable::CallError ce;
  92. md->open_cb.callp(nullptr, 0, ret, ce);
  93. if (ce.error != Callable::CallError::CALL_OK) {
  94. ERR_PRINT(vformat("Failed to execute menu open callback: %s.", Variant::get_callable_error_text(md->open_cb, nullptr, 0, ce)));
  95. }
  96. }
  97. }
  98. }
  99. }
  100. void NativeMenuMacOS::_menu_close(NSMenu *p_menu) {
  101. if (menu_lookup.has(p_menu)) {
  102. MenuData *md = menus.get_or_null(menu_lookup[p_menu]);
  103. if (md) {
  104. md->is_open = false;
  105. if (md->close_cb.is_valid()) {
  106. Variant ret;
  107. Callable::CallError ce;
  108. md->close_cb.callp(nullptr, 0, ret, ce);
  109. if (ce.error != Callable::CallError::CALL_OK) {
  110. ERR_PRINT(vformat("Failed to execute menu close callback: %s.", Variant::get_callable_error_text(md->close_cb, nullptr, 0, ce)));
  111. }
  112. }
  113. }
  114. }
  115. }
  116. bool NativeMenuMacOS::_is_menu_opened(NSMenu *p_menu) const {
  117. if (menu_lookup.has(p_menu)) {
  118. const MenuData *md = menus.get_or_null(menu_lookup[p_menu]);
  119. if (md && md->is_open) {
  120. return true;
  121. }
  122. }
  123. for (NSInteger i = (p_menu == [NSApp mainMenu]) ? 1 : 0; i < [p_menu numberOfItems]; i++) {
  124. const NSMenuItem *menu_item = [p_menu itemAtIndex:i];
  125. if ([menu_item submenu]) {
  126. if (_is_menu_opened([menu_item submenu])) {
  127. return true;
  128. }
  129. }
  130. }
  131. return false;
  132. }
  133. int NativeMenuMacOS::_get_system_menu_start(const NSMenu *p_menu) const {
  134. if (p_menu == [NSApp mainMenu]) { // Skip Apple menu.
  135. return 1;
  136. }
  137. if (p_menu == application_menu_ns || p_menu == window_menu_ns || p_menu == help_menu_ns) {
  138. int count = [p_menu numberOfItems];
  139. for (int i = 0; i < count; i++) {
  140. NSMenuItem *menu_item = [p_menu itemAtIndex:i];
  141. if (menu_item.tag == MENU_TAG_START) {
  142. return i + 1;
  143. }
  144. }
  145. }
  146. return 0;
  147. }
  148. int NativeMenuMacOS::_get_system_menu_count(const NSMenu *p_menu) const {
  149. if (p_menu == [NSApp mainMenu]) { // Skip Apple, Window and Help menu.
  150. return [p_menu numberOfItems] - 3;
  151. }
  152. if (p_menu == application_menu_ns || p_menu == window_menu_ns || p_menu == help_menu_ns) {
  153. int start = 0;
  154. int count = [p_menu numberOfItems];
  155. for (int i = 0; i < count; i++) {
  156. NSMenuItem *menu_item = [p_menu itemAtIndex:i];
  157. if (menu_item.tag == MENU_TAG_START) {
  158. start = i + 1;
  159. }
  160. if (menu_item.tag == MENU_TAG_END) {
  161. return i - start;
  162. }
  163. }
  164. }
  165. return [p_menu numberOfItems];
  166. }
  167. bool NativeMenuMacOS::has_feature(Feature p_feature) const {
  168. switch (p_feature) {
  169. case FEATURE_GLOBAL_MENU:
  170. case FEATURE_POPUP_MENU:
  171. case FEATURE_OPEN_CLOSE_CALLBACK:
  172. case FEATURE_HOVER_CALLBACK:
  173. case FEATURE_KEY_CALLBACK:
  174. return true;
  175. default:
  176. return false;
  177. }
  178. }
  179. bool NativeMenuMacOS::has_system_menu(SystemMenus p_menu_id) const {
  180. switch (p_menu_id) {
  181. case MAIN_MENU_ID:
  182. case APPLICATION_MENU_ID:
  183. case WINDOW_MENU_ID:
  184. case HELP_MENU_ID:
  185. case DOCK_MENU_ID:
  186. return true;
  187. default:
  188. return false;
  189. }
  190. }
  191. RID NativeMenuMacOS::get_system_menu(SystemMenus p_menu_id) const {
  192. switch (p_menu_id) {
  193. case MAIN_MENU_ID:
  194. return main_menu;
  195. case APPLICATION_MENU_ID:
  196. return application_menu;
  197. case WINDOW_MENU_ID:
  198. return window_menu;
  199. case HELP_MENU_ID:
  200. return help_menu;
  201. case DOCK_MENU_ID:
  202. return dock_menu;
  203. default:
  204. return RID();
  205. }
  206. }
  207. RID NativeMenuMacOS::create_menu() {
  208. MenuData *md = memnew(MenuData);
  209. md->menu = [[NSMenu alloc] initWithTitle:@""];
  210. [md->menu setAutoenablesItems:NO];
  211. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  212. if (ds) {
  213. ds->set_menu_delegate(md->menu);
  214. }
  215. RID rid = menus.make_rid(md);
  216. menu_lookup[md->menu] = rid;
  217. return rid;
  218. }
  219. bool NativeMenuMacOS::has_menu(const RID &p_rid) const {
  220. return menus.owns(p_rid);
  221. }
  222. void NativeMenuMacOS::free_menu(const RID &p_rid) {
  223. MenuData *md = menus.get_or_null(p_rid);
  224. if (md && !md->is_system) {
  225. clear(p_rid);
  226. menus.free(p_rid);
  227. menu_lookup.erase(md->menu);
  228. md->menu = nullptr;
  229. memdelete(md);
  230. }
  231. }
  232. NSMenu *NativeMenuMacOS::get_native_menu_handle(const RID &p_rid) {
  233. MenuData *md = menus.get_or_null(p_rid);
  234. ERR_FAIL_NULL_V(md, nullptr);
  235. return md->menu;
  236. }
  237. Size2 NativeMenuMacOS::get_size(const RID &p_rid) const {
  238. const MenuData *md = menus.get_or_null(p_rid);
  239. ERR_FAIL_NULL_V(md, Size2());
  240. return Size2(md->menu.size.width, md->menu.size.height) * DisplayServer::get_singleton()->screen_get_max_scale();
  241. }
  242. void NativeMenuMacOS::popup(const RID &p_rid, const Vector2i &p_position) {
  243. const MenuData *md = menus.get_or_null(p_rid);
  244. ERR_FAIL_NULL(md);
  245. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  246. if (ds) {
  247. Point2i position = p_position;
  248. // macOS native y-coordinate relative to _get_screens_origin() is negative,
  249. // Godot passes a positive value.
  250. position.y *= -1;
  251. position += ds->_get_screens_origin();
  252. position /= ds->screen_get_max_scale();
  253. [md->menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(position.x, position.y) inView:nil];
  254. }
  255. }
  256. void NativeMenuMacOS::set_interface_direction(const RID &p_rid, bool p_is_rtl) {
  257. MenuData *md = menus.get_or_null(p_rid);
  258. ERR_FAIL_NULL(md);
  259. md->menu.userInterfaceLayoutDirection = p_is_rtl ? NSUserInterfaceLayoutDirectionRightToLeft : NSUserInterfaceLayoutDirectionLeftToRight;
  260. }
  261. void NativeMenuMacOS::set_popup_open_callback(const RID &p_rid, const Callable &p_callback) {
  262. MenuData *md = menus.get_or_null(p_rid);
  263. ERR_FAIL_NULL(md);
  264. md->open_cb = p_callback;
  265. }
  266. Callable NativeMenuMacOS::get_popup_open_callback(const RID &p_rid) const {
  267. const MenuData *md = menus.get_or_null(p_rid);
  268. ERR_FAIL_NULL_V(md, Callable());
  269. return md->open_cb;
  270. }
  271. void NativeMenuMacOS::set_popup_close_callback(const RID &p_rid, const Callable &p_callback) {
  272. MenuData *md = menus.get_or_null(p_rid);
  273. ERR_FAIL_NULL(md);
  274. md->close_cb = p_callback;
  275. }
  276. Callable NativeMenuMacOS::get_popup_close_callback(const RID &p_rid) const {
  277. const MenuData *md = menus.get_or_null(p_rid);
  278. ERR_FAIL_NULL_V(md, Callable());
  279. return md->close_cb;
  280. }
  281. void NativeMenuMacOS::set_minimum_width(const RID &p_rid, float p_width) {
  282. MenuData *md = menus.get_or_null(p_rid);
  283. ERR_FAIL_NULL(md);
  284. md->menu.minimumWidth = p_width / DisplayServer::get_singleton()->screen_get_max_scale();
  285. }
  286. float NativeMenuMacOS::get_minimum_width(const RID &p_rid) const {
  287. const MenuData *md = menus.get_or_null(p_rid);
  288. ERR_FAIL_NULL_V(md, 0.0);
  289. return md->menu.minimumWidth * DisplayServer::get_singleton()->screen_get_max_scale();
  290. }
  291. 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) {
  292. MenuData *md = menus.get_or_null(p_rid);
  293. MenuData *md_sub = menus.get_or_null(p_submenu_rid);
  294. ERR_FAIL_NULL_V(md, -1);
  295. ERR_FAIL_NULL_V(md_sub, -1);
  296. ERR_FAIL_COND_V_MSG(md->menu == md_sub->menu, -1, "Can't set submenu to self!");
  297. 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!");
  298. NSMenuItem *menu_item;
  299. int item_start = _get_system_menu_start(md->menu);
  300. int item_count = _get_system_menu_count(md->menu);
  301. if (p_index < 0) {
  302. p_index = item_start + item_count;
  303. } else {
  304. p_index += item_start;
  305. p_index = CLAMP(p_index, item_start, item_start + item_count);
  306. }
  307. menu_item = [md->menu insertItemWithTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()] action:nil keyEquivalent:@"" atIndex:p_index];
  308. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  309. obj->callback = Callable();
  310. obj->key_callback = Callable();
  311. obj->meta = p_tag;
  312. obj->checkable_type = CHECKABLE_TYPE_NONE;
  313. obj->max_states = 0;
  314. obj->state = 0;
  315. [menu_item setRepresentedObject:obj];
  316. [md_sub->menu setTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()]];
  317. [md->menu setSubmenu:md_sub->menu forItem:menu_item];
  318. return p_index - item_start;
  319. }
  320. NSMenuItem *NativeMenuMacOS::_menu_add_item(NSMenu *p_menu, const String &p_label, Key p_accel, int p_index, int *r_out) {
  321. if (p_menu) {
  322. String keycode = KeyMappingMacOS::keycode_get_native_string(p_accel & KeyModifierMask::CODE_MASK);
  323. NSMenuItem *menu_item;
  324. int item_start = _get_system_menu_start(p_menu);
  325. int item_count = _get_system_menu_count(p_menu);
  326. if (p_index < 0) {
  327. p_index = item_start + item_count;
  328. } else {
  329. p_index += item_start;
  330. p_index = CLAMP(p_index, item_start, item_start + item_count);
  331. }
  332. 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];
  333. *r_out = p_index - item_start;
  334. return menu_item;
  335. }
  336. return nullptr;
  337. }
  338. 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) {
  339. MenuData *md = menus.get_or_null(p_rid);
  340. ERR_FAIL_NULL_V(md, -1);
  341. int out = -1;
  342. NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
  343. if (menu_item) {
  344. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  345. obj->callback = p_callback;
  346. obj->key_callback = p_key_callback;
  347. obj->meta = p_tag;
  348. obj->checkable_type = CHECKABLE_TYPE_NONE;
  349. obj->max_states = 0;
  350. obj->state = 0;
  351. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  352. [menu_item setRepresentedObject:obj];
  353. }
  354. return out;
  355. }
  356. 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) {
  357. MenuData *md = menus.get_or_null(p_rid);
  358. ERR_FAIL_NULL_V(md, -1);
  359. int out = -1;
  360. NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
  361. if (menu_item) {
  362. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  363. obj->callback = p_callback;
  364. obj->key_callback = p_key_callback;
  365. obj->meta = p_tag;
  366. obj->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
  367. obj->max_states = 0;
  368. obj->state = 0;
  369. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  370. [menu_item setRepresentedObject:obj];
  371. }
  372. return out;
  373. }
  374. 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) {
  375. MenuData *md = menus.get_or_null(p_rid);
  376. ERR_FAIL_NULL_V(md, -1);
  377. int out = -1;
  378. NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
  379. if (menu_item) {
  380. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  381. obj->callback = p_callback;
  382. obj->key_callback = p_key_callback;
  383. obj->meta = p_tag;
  384. obj->checkable_type = CHECKABLE_TYPE_NONE;
  385. obj->max_states = 0;
  386. obj->state = 0;
  387. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  388. if (ds && p_icon.is_valid()) {
  389. obj->img = p_icon->get_image();
  390. obj->img = obj->img->duplicate();
  391. if (obj->img->is_compressed()) {
  392. obj->img->decompress();
  393. }
  394. NSImage *image = ds->_convert_to_nsimg(obj->img);
  395. [image setSize:NSMakeSize(16, 16)];
  396. [menu_item setImage:image];
  397. }
  398. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  399. [menu_item setRepresentedObject:obj];
  400. }
  401. return out;
  402. }
  403. 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) {
  404. MenuData *md = menus.get_or_null(p_rid);
  405. ERR_FAIL_NULL_V(md, -1);
  406. int out = -1;
  407. NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
  408. if (menu_item) {
  409. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  410. obj->callback = p_callback;
  411. obj->key_callback = p_key_callback;
  412. obj->meta = p_tag;
  413. obj->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
  414. obj->max_states = 0;
  415. obj->state = 0;
  416. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  417. if (ds && p_icon.is_valid()) {
  418. obj->img = p_icon->get_image();
  419. obj->img = obj->img->duplicate();
  420. if (obj->img->is_compressed()) {
  421. obj->img->decompress();
  422. }
  423. NSImage *image = ds->_convert_to_nsimg(obj->img);
  424. [image setSize:NSMakeSize(16, 16)];
  425. [menu_item setImage:image];
  426. }
  427. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  428. [menu_item setRepresentedObject:obj];
  429. }
  430. return out;
  431. }
  432. 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) {
  433. MenuData *md = menus.get_or_null(p_rid);
  434. ERR_FAIL_NULL_V(md, -1);
  435. int out = -1;
  436. NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
  437. if (menu_item) {
  438. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  439. obj->callback = p_callback;
  440. obj->key_callback = p_key_callback;
  441. obj->meta = p_tag;
  442. obj->checkable_type = CHECKABLE_TYPE_RADIO_BUTTON;
  443. obj->max_states = 0;
  444. obj->state = 0;
  445. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  446. [menu_item setRepresentedObject:obj];
  447. }
  448. return out;
  449. }
  450. 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) {
  451. MenuData *md = menus.get_or_null(p_rid);
  452. ERR_FAIL_NULL_V(md, -1);
  453. int out = -1;
  454. NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
  455. if (menu_item) {
  456. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  457. obj->callback = p_callback;
  458. obj->key_callback = p_key_callback;
  459. obj->meta = p_tag;
  460. obj->checkable_type = CHECKABLE_TYPE_RADIO_BUTTON;
  461. obj->max_states = 0;
  462. obj->state = 0;
  463. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  464. if (ds && p_icon.is_valid()) {
  465. obj->img = p_icon->get_image();
  466. obj->img = obj->img->duplicate();
  467. if (obj->img->is_compressed()) {
  468. obj->img->decompress();
  469. }
  470. NSImage *image = ds->_convert_to_nsimg(obj->img);
  471. [image setSize:NSMakeSize(16, 16)];
  472. [menu_item setImage:image];
  473. }
  474. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  475. [menu_item setRepresentedObject:obj];
  476. }
  477. return out;
  478. }
  479. 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) {
  480. MenuData *md = menus.get_or_null(p_rid);
  481. ERR_FAIL_NULL_V(md, -1);
  482. int out = -1;
  483. NSMenuItem *menu_item = _menu_add_item(md->menu, p_label, p_accel, p_index, &out);
  484. if (menu_item) {
  485. GodotMenuItem *obj = [[GodotMenuItem alloc] init];
  486. obj->callback = p_callback;
  487. obj->key_callback = p_key_callback;
  488. obj->meta = p_tag;
  489. obj->checkable_type = CHECKABLE_TYPE_NONE;
  490. obj->max_states = p_max_states;
  491. obj->state = p_default_state;
  492. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
  493. [menu_item setRepresentedObject:obj];
  494. }
  495. return out;
  496. }
  497. int NativeMenuMacOS::add_separator(const RID &p_rid, int p_index) {
  498. MenuData *md = menus.get_or_null(p_rid);
  499. ERR_FAIL_NULL_V(md, -1);
  500. if (md->menu == [NSApp mainMenu]) { // Do not add separators into main menu.
  501. return -1;
  502. }
  503. int item_start = _get_system_menu_start(md->menu);
  504. int item_count = _get_system_menu_count(md->menu);
  505. if (p_index < 0) {
  506. p_index = item_start + item_count;
  507. } else {
  508. p_index += item_start;
  509. p_index = CLAMP(p_index, item_start, item_start + item_count);
  510. }
  511. [md->menu insertItem:[NSMenuItem separatorItem] atIndex:p_index];
  512. return p_index - item_start;
  513. }
  514. int NativeMenuMacOS::find_item_index_with_text(const RID &p_rid, const String &p_text) const {
  515. const MenuData *md = menus.get_or_null(p_rid);
  516. ERR_FAIL_NULL_V(md, -1);
  517. int item_start = _get_system_menu_start(md->menu);
  518. int index = [md->menu indexOfItemWithTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
  519. if (index >= 0) {
  520. return index - item_start;
  521. }
  522. return -1;
  523. }
  524. int NativeMenuMacOS::find_item_index_with_tag(const RID &p_rid, const Variant &p_tag) const {
  525. const MenuData *md = menus.get_or_null(p_rid);
  526. ERR_FAIL_NULL_V(md, -1);
  527. int item_start = _get_system_menu_start(md->menu);
  528. int item_count = _get_system_menu_count(md->menu);
  529. for (NSInteger i = item_start; i < item_start + item_count; i++) {
  530. const NSMenuItem *menu_item = [md->menu itemAtIndex:i];
  531. if (menu_item) {
  532. const GodotMenuItem *obj = [menu_item representedObject];
  533. if (obj && obj->meta == p_tag) {
  534. return i - item_start;
  535. }
  536. }
  537. }
  538. return -1;
  539. }
  540. bool NativeMenuMacOS::is_item_checked(const RID &p_rid, int p_idx) const {
  541. ERR_FAIL_COND_V(p_idx < 0, false);
  542. const MenuData *md = menus.get_or_null(p_rid);
  543. ERR_FAIL_NULL_V(md, false);
  544. int item_start = _get_system_menu_start(md->menu);
  545. int item_count = _get_system_menu_count(md->menu);
  546. p_idx += item_start;
  547. ERR_FAIL_COND_V(p_idx >= item_start + item_count, false);
  548. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  549. if (menu_item) {
  550. return ([menu_item state] == NSControlStateValueOn);
  551. }
  552. return false;
  553. }
  554. bool NativeMenuMacOS::is_item_checkable(const RID &p_rid, int p_idx) const {
  555. ERR_FAIL_COND_V(p_idx < 0, false);
  556. const MenuData *md = menus.get_or_null(p_rid);
  557. ERR_FAIL_NULL_V(md, false);
  558. int item_start = _get_system_menu_start(md->menu);
  559. int item_count = _get_system_menu_count(md->menu);
  560. p_idx += item_start;
  561. ERR_FAIL_COND_V(p_idx >= item_start + item_count, false);
  562. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  563. if (menu_item) {
  564. GodotMenuItem *obj = [menu_item representedObject];
  565. if (obj) {
  566. return obj->checkable_type == CHECKABLE_TYPE_CHECK_BOX;
  567. }
  568. }
  569. return false;
  570. }
  571. bool NativeMenuMacOS::is_item_radio_checkable(const RID &p_rid, int p_idx) const {
  572. ERR_FAIL_COND_V(p_idx < 0, false);
  573. const MenuData *md = menus.get_or_null(p_rid);
  574. ERR_FAIL_NULL_V(md, false);
  575. int item_start = _get_system_menu_start(md->menu);
  576. int item_count = _get_system_menu_count(md->menu);
  577. p_idx += item_start;
  578. ERR_FAIL_COND_V(p_idx >= item_start + item_count, false);
  579. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  580. if (menu_item) {
  581. GodotMenuItem *obj = [menu_item representedObject];
  582. if (obj) {
  583. return obj->checkable_type == CHECKABLE_TYPE_RADIO_BUTTON;
  584. }
  585. }
  586. return false;
  587. }
  588. Callable NativeMenuMacOS::get_item_callback(const RID &p_rid, int p_idx) const {
  589. ERR_FAIL_COND_V(p_idx < 0, Callable());
  590. const MenuData *md = menus.get_or_null(p_rid);
  591. ERR_FAIL_NULL_V(md, Callable());
  592. int item_start = _get_system_menu_start(md->menu);
  593. int item_count = _get_system_menu_count(md->menu);
  594. p_idx += item_start;
  595. ERR_FAIL_COND_V(p_idx >= item_start + item_count, Callable());
  596. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  597. if (menu_item) {
  598. GodotMenuItem *obj = [menu_item representedObject];
  599. if (obj) {
  600. return obj->callback;
  601. }
  602. }
  603. return Callable();
  604. }
  605. Callable NativeMenuMacOS::get_item_key_callback(const RID &p_rid, int p_idx) const {
  606. ERR_FAIL_COND_V(p_idx < 0, Callable());
  607. const MenuData *md = menus.get_or_null(p_rid);
  608. ERR_FAIL_NULL_V(md, Callable());
  609. int item_start = _get_system_menu_start(md->menu);
  610. int item_count = _get_system_menu_count(md->menu);
  611. p_idx += item_start;
  612. ERR_FAIL_COND_V(p_idx >= item_start + item_count, Callable());
  613. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  614. if (menu_item) {
  615. GodotMenuItem *obj = [menu_item representedObject];
  616. if (obj) {
  617. return obj->key_callback;
  618. }
  619. }
  620. return Callable();
  621. }
  622. Variant NativeMenuMacOS::get_item_tag(const RID &p_rid, int p_idx) const {
  623. ERR_FAIL_COND_V(p_idx < 0, Variant());
  624. const MenuData *md = menus.get_or_null(p_rid);
  625. ERR_FAIL_NULL_V(md, Variant());
  626. int item_start = _get_system_menu_start(md->menu);
  627. int item_count = _get_system_menu_count(md->menu);
  628. p_idx += item_start;
  629. ERR_FAIL_COND_V(p_idx >= item_start + item_count, Variant());
  630. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  631. if (menu_item) {
  632. GodotMenuItem *obj = [menu_item representedObject];
  633. if (obj) {
  634. return obj->meta;
  635. }
  636. }
  637. return Variant();
  638. }
  639. String NativeMenuMacOS::get_item_text(const RID &p_rid, int p_idx) const {
  640. ERR_FAIL_COND_V(p_idx < 0, String());
  641. const MenuData *md = menus.get_or_null(p_rid);
  642. ERR_FAIL_NULL_V(md, String());
  643. int item_start = _get_system_menu_start(md->menu);
  644. int item_count = _get_system_menu_count(md->menu);
  645. p_idx += item_start;
  646. ERR_FAIL_COND_V(p_idx >= item_start + item_count, String());
  647. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  648. if (menu_item) {
  649. return String::utf8([[menu_item title] UTF8String]);
  650. }
  651. return String();
  652. }
  653. RID NativeMenuMacOS::get_item_submenu(const RID &p_rid, int p_idx) const {
  654. ERR_FAIL_COND_V(p_idx < 0, RID());
  655. const MenuData *md = menus.get_or_null(p_rid);
  656. ERR_FAIL_NULL_V(md, RID());
  657. int item_start = _get_system_menu_start(md->menu);
  658. int item_count = _get_system_menu_count(md->menu);
  659. p_idx += item_start;
  660. ERR_FAIL_COND_V(p_idx >= item_start + item_count, RID());
  661. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  662. if (menu_item) {
  663. NSMenu *sub_menu = [menu_item submenu];
  664. if (sub_menu && menu_lookup.has(sub_menu)) {
  665. return menu_lookup[sub_menu];
  666. }
  667. }
  668. return RID();
  669. }
  670. Key NativeMenuMacOS::get_item_accelerator(const RID &p_rid, int p_idx) const {
  671. ERR_FAIL_COND_V(p_idx < 0, Key::NONE);
  672. const MenuData *md = menus.get_or_null(p_rid);
  673. ERR_FAIL_NULL_V(md, Key::NONE);
  674. int item_start = _get_system_menu_start(md->menu);
  675. int item_count = _get_system_menu_count(md->menu);
  676. p_idx += item_start;
  677. ERR_FAIL_COND_V(p_idx >= item_start + item_count, Key::NONE);
  678. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  679. if (menu_item) {
  680. String ret = String::utf8([[menu_item keyEquivalent] UTF8String]);
  681. Key keycode = find_keycode(ret);
  682. NSUInteger mask = [menu_item keyEquivalentModifierMask];
  683. if (mask & NSEventModifierFlagControl) {
  684. keycode |= KeyModifierMask::CTRL;
  685. }
  686. if (mask & NSEventModifierFlagOption) {
  687. keycode |= KeyModifierMask::ALT;
  688. }
  689. if (mask & NSEventModifierFlagShift) {
  690. keycode |= KeyModifierMask::SHIFT;
  691. }
  692. if (mask & NSEventModifierFlagCommand) {
  693. keycode |= KeyModifierMask::META;
  694. }
  695. if (mask & NSEventModifierFlagNumericPad) {
  696. keycode |= KeyModifierMask::KPAD;
  697. }
  698. return keycode;
  699. }
  700. return Key::NONE;
  701. }
  702. bool NativeMenuMacOS::is_item_disabled(const RID &p_rid, int p_idx) const {
  703. ERR_FAIL_COND_V(p_idx < 0, false);
  704. const MenuData *md = menus.get_or_null(p_rid);
  705. ERR_FAIL_NULL_V(md, false);
  706. int item_start = _get_system_menu_start(md->menu);
  707. int item_count = _get_system_menu_count(md->menu);
  708. p_idx += item_start;
  709. ERR_FAIL_COND_V(p_idx >= item_start + item_count, false);
  710. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  711. if (menu_item) {
  712. return ![menu_item isEnabled];
  713. }
  714. return false;
  715. }
  716. bool NativeMenuMacOS::is_item_hidden(const RID &p_rid, int p_idx) const {
  717. ERR_FAIL_COND_V(p_idx < 0, false);
  718. const MenuData *md = menus.get_or_null(p_rid);
  719. ERR_FAIL_NULL_V(md, false);
  720. int item_start = _get_system_menu_start(md->menu);
  721. int item_count = _get_system_menu_count(md->menu);
  722. p_idx += item_start;
  723. ERR_FAIL_COND_V(p_idx >= item_start + item_count, false);
  724. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  725. if (menu_item) {
  726. return [menu_item isHidden];
  727. }
  728. return false;
  729. }
  730. String NativeMenuMacOS::get_item_tooltip(const RID &p_rid, int p_idx) const {
  731. ERR_FAIL_COND_V(p_idx < 0, String());
  732. const MenuData *md = menus.get_or_null(p_rid);
  733. ERR_FAIL_NULL_V(md, String());
  734. int item_start = _get_system_menu_start(md->menu);
  735. int item_count = _get_system_menu_count(md->menu);
  736. p_idx += item_start;
  737. ERR_FAIL_COND_V(p_idx >= item_start + item_count, String());
  738. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  739. if (menu_item) {
  740. return String::utf8([[menu_item toolTip] UTF8String]);
  741. }
  742. return String();
  743. }
  744. int NativeMenuMacOS::get_item_state(const RID &p_rid, int p_idx) const {
  745. ERR_FAIL_COND_V(p_idx < 0, 0);
  746. const MenuData *md = menus.get_or_null(p_rid);
  747. ERR_FAIL_NULL_V(md, 0);
  748. int item_start = _get_system_menu_start(md->menu);
  749. int item_count = _get_system_menu_count(md->menu);
  750. p_idx += item_start;
  751. ERR_FAIL_COND_V(p_idx >= item_start + item_count, 0);
  752. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  753. if (menu_item) {
  754. GodotMenuItem *obj = [menu_item representedObject];
  755. if (obj) {
  756. return obj->state;
  757. }
  758. }
  759. return 0;
  760. }
  761. int NativeMenuMacOS::get_item_max_states(const RID &p_rid, int p_idx) const {
  762. ERR_FAIL_COND_V(p_idx < 0, 0);
  763. const MenuData *md = menus.get_or_null(p_rid);
  764. ERR_FAIL_NULL_V(md, 0);
  765. int item_start = _get_system_menu_start(md->menu);
  766. int item_count = _get_system_menu_count(md->menu);
  767. p_idx += item_start;
  768. ERR_FAIL_COND_V(p_idx >= item_start + item_count, 0);
  769. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  770. if (menu_item) {
  771. GodotMenuItem *obj = [menu_item representedObject];
  772. if (obj) {
  773. return obj->max_states;
  774. }
  775. }
  776. return 0;
  777. }
  778. Ref<Texture2D> NativeMenuMacOS::get_item_icon(const RID &p_rid, int p_idx) const {
  779. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture2D>());
  780. const MenuData *md = menus.get_or_null(p_rid);
  781. ERR_FAIL_NULL_V(md, Ref<Texture2D>());
  782. int item_start = _get_system_menu_start(md->menu);
  783. int item_count = _get_system_menu_count(md->menu);
  784. p_idx += item_start;
  785. ERR_FAIL_COND_V(p_idx >= item_start + item_count, Ref<Texture2D>());
  786. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  787. if (menu_item) {
  788. GodotMenuItem *obj = [menu_item representedObject];
  789. if (obj) {
  790. if (obj->img.is_valid()) {
  791. return ImageTexture::create_from_image(obj->img);
  792. }
  793. }
  794. }
  795. return Ref<Texture2D>();
  796. }
  797. int NativeMenuMacOS::get_item_indentation_level(const RID &p_rid, int p_idx) const {
  798. ERR_FAIL_COND_V(p_idx < 0, 0);
  799. const MenuData *md = menus.get_or_null(p_rid);
  800. ERR_FAIL_NULL_V(md, 0);
  801. int item_start = _get_system_menu_start(md->menu);
  802. int item_count = _get_system_menu_count(md->menu);
  803. p_idx += item_start;
  804. ERR_FAIL_COND_V(p_idx >= item_start + item_count, 0);
  805. const NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  806. if (menu_item) {
  807. return [menu_item indentationLevel];
  808. }
  809. return 0;
  810. }
  811. void NativeMenuMacOS::set_item_checked(const RID &p_rid, int p_idx, bool p_checked) {
  812. ERR_FAIL_COND(p_idx < 0);
  813. MenuData *md = menus.get_or_null(p_rid);
  814. ERR_FAIL_NULL(md);
  815. int item_start = _get_system_menu_start(md->menu);
  816. int item_count = _get_system_menu_count(md->menu);
  817. p_idx += item_start;
  818. ERR_FAIL_COND(p_idx >= item_start + item_count);
  819. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  820. if (menu_item) {
  821. if (p_checked) {
  822. [menu_item setState:NSControlStateValueOn];
  823. } else {
  824. [menu_item setState:NSControlStateValueOff];
  825. }
  826. }
  827. }
  828. void NativeMenuMacOS::set_item_checkable(const RID &p_rid, int p_idx, bool p_checkable) {
  829. ERR_FAIL_COND(p_idx < 0);
  830. MenuData *md = menus.get_or_null(p_rid);
  831. ERR_FAIL_NULL(md);
  832. int item_start = _get_system_menu_start(md->menu);
  833. int item_count = _get_system_menu_count(md->menu);
  834. p_idx += item_start;
  835. ERR_FAIL_COND(p_idx >= item_start + item_count);
  836. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  837. if (menu_item) {
  838. GodotMenuItem *obj = [menu_item representedObject];
  839. ERR_FAIL_NULL(obj);
  840. obj->checkable_type = (p_checkable) ? CHECKABLE_TYPE_CHECK_BOX : CHECKABLE_TYPE_NONE;
  841. }
  842. }
  843. void NativeMenuMacOS::set_item_radio_checkable(const RID &p_rid, int p_idx, bool p_checkable) {
  844. ERR_FAIL_COND(p_idx < 0);
  845. MenuData *md = menus.get_or_null(p_rid);
  846. ERR_FAIL_NULL(md);
  847. int item_start = _get_system_menu_start(md->menu);
  848. int item_count = _get_system_menu_count(md->menu);
  849. p_idx += item_start;
  850. ERR_FAIL_COND(p_idx >= item_start + item_count);
  851. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  852. if (menu_item) {
  853. GodotMenuItem *obj = [menu_item representedObject];
  854. ERR_FAIL_NULL(obj);
  855. obj->checkable_type = (p_checkable) ? CHECKABLE_TYPE_RADIO_BUTTON : CHECKABLE_TYPE_NONE;
  856. }
  857. }
  858. void NativeMenuMacOS::set_item_callback(const RID &p_rid, int p_idx, const Callable &p_callback) {
  859. ERR_FAIL_COND(p_idx < 0);
  860. MenuData *md = menus.get_or_null(p_rid);
  861. ERR_FAIL_NULL(md);
  862. int item_start = _get_system_menu_start(md->menu);
  863. int item_count = _get_system_menu_count(md->menu);
  864. p_idx += item_start;
  865. ERR_FAIL_COND(p_idx >= item_start + item_count);
  866. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  867. if (menu_item) {
  868. GodotMenuItem *obj = [menu_item representedObject];
  869. ERR_FAIL_NULL(obj);
  870. obj->callback = p_callback;
  871. }
  872. }
  873. void NativeMenuMacOS::set_item_key_callback(const RID &p_rid, int p_idx, const Callable &p_key_callback) {
  874. ERR_FAIL_COND(p_idx < 0);
  875. MenuData *md = menus.get_or_null(p_rid);
  876. ERR_FAIL_NULL(md);
  877. int item_start = _get_system_menu_start(md->menu);
  878. int item_count = _get_system_menu_count(md->menu);
  879. p_idx += item_start;
  880. ERR_FAIL_COND(p_idx >= item_start + item_count);
  881. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  882. if (menu_item) {
  883. GodotMenuItem *obj = [menu_item representedObject];
  884. ERR_FAIL_NULL(obj);
  885. obj->key_callback = p_key_callback;
  886. }
  887. }
  888. void NativeMenuMacOS::set_item_hover_callbacks(const RID &p_rid, int p_idx, const Callable &p_callback) {
  889. ERR_FAIL_COND(p_idx < 0);
  890. MenuData *md = menus.get_or_null(p_rid);
  891. ERR_FAIL_NULL(md);
  892. int item_start = _get_system_menu_start(md->menu);
  893. int item_count = _get_system_menu_count(md->menu);
  894. p_idx += item_start;
  895. ERR_FAIL_COND(p_idx >= item_start + item_count);
  896. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  897. if (menu_item) {
  898. GodotMenuItem *obj = [menu_item representedObject];
  899. ERR_FAIL_NULL(obj);
  900. obj->hover_callback = p_callback;
  901. }
  902. }
  903. void NativeMenuMacOS::set_item_tag(const RID &p_rid, int p_idx, const Variant &p_tag) {
  904. ERR_FAIL_COND(p_idx < 0);
  905. MenuData *md = menus.get_or_null(p_rid);
  906. ERR_FAIL_NULL(md);
  907. int item_start = _get_system_menu_start(md->menu);
  908. int item_count = _get_system_menu_count(md->menu);
  909. p_idx += item_start;
  910. ERR_FAIL_COND(p_idx >= item_start + item_count);
  911. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  912. if (menu_item) {
  913. GodotMenuItem *obj = [menu_item representedObject];
  914. ERR_FAIL_NULL(obj);
  915. obj->meta = p_tag;
  916. }
  917. }
  918. void NativeMenuMacOS::set_item_text(const RID &p_rid, int p_idx, const String &p_text) {
  919. ERR_FAIL_COND(p_idx < 0);
  920. MenuData *md = menus.get_or_null(p_rid);
  921. ERR_FAIL_NULL(md);
  922. int item_start = _get_system_menu_start(md->menu);
  923. int item_count = _get_system_menu_count(md->menu);
  924. p_idx += item_start;
  925. ERR_FAIL_COND(p_idx >= item_start + item_count);
  926. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  927. if (menu_item) {
  928. [menu_item setTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
  929. NSMenu *sub_menu = [menu_item submenu];
  930. if (sub_menu) {
  931. [sub_menu setTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
  932. }
  933. }
  934. }
  935. void NativeMenuMacOS::set_item_submenu(const RID &p_rid, int p_idx, const RID &p_submenu_rid) {
  936. ERR_FAIL_COND(p_idx < 0);
  937. MenuData *md = menus.get_or_null(p_rid);
  938. ERR_FAIL_NULL(md);
  939. if (p_submenu_rid.is_valid()) {
  940. MenuData *md_sub = menus.get_or_null(p_submenu_rid);
  941. ERR_FAIL_NULL(md_sub);
  942. ERR_FAIL_COND_MSG(md->menu == md_sub->menu, "Can't set submenu to self!");
  943. ERR_FAIL_COND_MSG([md_sub->menu supermenu], "Can't set submenu to menu that is already a submenu of some other menu!");
  944. int item_start = _get_system_menu_start(md->menu);
  945. int item_count = _get_system_menu_count(md->menu);
  946. p_idx += item_start;
  947. ERR_FAIL_COND(p_idx >= item_start + item_count);
  948. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  949. if (menu_item) {
  950. [md->menu setSubmenu:md_sub->menu forItem:menu_item];
  951. }
  952. } else {
  953. int item_start = _get_system_menu_start(md->menu);
  954. int item_count = _get_system_menu_count(md->menu);
  955. p_idx += item_start;
  956. ERR_FAIL_COND(p_idx >= item_start + item_count);
  957. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  958. if (menu_item) {
  959. if ([menu_item submenu] && _is_menu_opened([menu_item submenu])) {
  960. ERR_PRINT("Can't remove open menu!");
  961. return;
  962. }
  963. [md->menu setSubmenu:nil forItem:menu_item];
  964. }
  965. }
  966. }
  967. void NativeMenuMacOS::set_item_accelerator(const RID &p_rid, int p_idx, Key p_keycode) {
  968. ERR_FAIL_COND(p_idx < 0);
  969. MenuData *md = menus.get_or_null(p_rid);
  970. ERR_FAIL_NULL(md);
  971. int item_start = _get_system_menu_start(md->menu);
  972. int item_count = _get_system_menu_count(md->menu);
  973. p_idx += item_start;
  974. ERR_FAIL_COND(p_idx >= item_start + item_count);
  975. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  976. if (menu_item) {
  977. if (p_keycode == Key::NONE) {
  978. [menu_item setKeyEquivalent:@""];
  979. } else {
  980. [menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_keycode)];
  981. String keycode = KeyMappingMacOS::keycode_get_native_string(p_keycode & KeyModifierMask::CODE_MASK);
  982. [menu_item setKeyEquivalent:[NSString stringWithUTF8String:keycode.utf8().get_data()]];
  983. }
  984. }
  985. }
  986. void NativeMenuMacOS::set_item_disabled(const RID &p_rid, int p_idx, bool p_disabled) {
  987. ERR_FAIL_COND(p_idx < 0);
  988. MenuData *md = menus.get_or_null(p_rid);
  989. ERR_FAIL_NULL(md);
  990. int item_start = _get_system_menu_start(md->menu);
  991. int item_count = _get_system_menu_count(md->menu);
  992. p_idx += item_start;
  993. ERR_FAIL_COND(p_idx >= item_start + item_count);
  994. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  995. if (menu_item) {
  996. [menu_item setEnabled:(!p_disabled)];
  997. }
  998. }
  999. void NativeMenuMacOS::set_item_hidden(const RID &p_rid, int p_idx, bool p_hidden) {
  1000. ERR_FAIL_COND(p_idx < 0);
  1001. MenuData *md = menus.get_or_null(p_rid);
  1002. ERR_FAIL_NULL(md);
  1003. int item_start = _get_system_menu_start(md->menu);
  1004. int item_count = _get_system_menu_count(md->menu);
  1005. p_idx += item_start;
  1006. ERR_FAIL_COND(p_idx >= item_start + item_count);
  1007. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  1008. if (menu_item) {
  1009. [menu_item setHidden:p_hidden];
  1010. }
  1011. }
  1012. void NativeMenuMacOS::set_item_tooltip(const RID &p_rid, int p_idx, const String &p_tooltip) {
  1013. ERR_FAIL_COND(p_idx < 0);
  1014. MenuData *md = menus.get_or_null(p_rid);
  1015. ERR_FAIL_NULL(md);
  1016. int item_start = _get_system_menu_start(md->menu);
  1017. int item_count = _get_system_menu_count(md->menu);
  1018. p_idx += item_start;
  1019. ERR_FAIL_COND(p_idx >= item_start + item_count);
  1020. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  1021. if (menu_item) {
  1022. [menu_item setToolTip:[NSString stringWithUTF8String:p_tooltip.utf8().get_data()]];
  1023. }
  1024. }
  1025. void NativeMenuMacOS::set_item_state(const RID &p_rid, int p_idx, int p_state) {
  1026. ERR_FAIL_COND(p_idx < 0);
  1027. MenuData *md = menus.get_or_null(p_rid);
  1028. ERR_FAIL_NULL(md);
  1029. int item_start = _get_system_menu_start(md->menu);
  1030. int item_count = _get_system_menu_count(md->menu);
  1031. p_idx += item_start;
  1032. ERR_FAIL_COND(p_idx >= item_start + item_count);
  1033. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  1034. if (menu_item) {
  1035. GodotMenuItem *obj = [menu_item representedObject];
  1036. ERR_FAIL_NULL(obj);
  1037. obj->state = p_state;
  1038. }
  1039. }
  1040. void NativeMenuMacOS::set_item_max_states(const RID &p_rid, int p_idx, int p_max_states) {
  1041. ERR_FAIL_COND(p_idx < 0);
  1042. MenuData *md = menus.get_or_null(p_rid);
  1043. ERR_FAIL_NULL(md);
  1044. int item_start = _get_system_menu_start(md->menu);
  1045. int item_count = _get_system_menu_count(md->menu);
  1046. p_idx += item_start;
  1047. ERR_FAIL_COND(p_idx >= item_start + item_count);
  1048. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  1049. if (menu_item) {
  1050. GodotMenuItem *obj = [menu_item representedObject];
  1051. ERR_FAIL_NULL(obj);
  1052. obj->max_states = p_max_states;
  1053. }
  1054. }
  1055. void NativeMenuMacOS::set_item_icon(const RID &p_rid, int p_idx, const Ref<Texture2D> &p_icon) {
  1056. ERR_FAIL_COND(p_idx < 0);
  1057. MenuData *md = menus.get_or_null(p_rid);
  1058. ERR_FAIL_NULL(md);
  1059. int item_start = _get_system_menu_start(md->menu);
  1060. int item_count = _get_system_menu_count(md->menu);
  1061. p_idx += item_start;
  1062. ERR_FAIL_COND(p_idx >= item_start + item_count);
  1063. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  1064. if (menu_item) {
  1065. GodotMenuItem *obj = [menu_item representedObject];
  1066. ERR_FAIL_NULL(obj);
  1067. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  1068. if (ds && p_icon.is_valid()) {
  1069. obj->img = p_icon->get_image();
  1070. obj->img = obj->img->duplicate();
  1071. if (obj->img->is_compressed()) {
  1072. obj->img->decompress();
  1073. }
  1074. NSImage *image = ds->_convert_to_nsimg(obj->img);
  1075. [image setSize:NSMakeSize(16, 16)];
  1076. [menu_item setImage:image];
  1077. } else {
  1078. obj->img = Ref<Image>();
  1079. [menu_item setImage:nil];
  1080. }
  1081. }
  1082. }
  1083. void NativeMenuMacOS::set_item_indentation_level(const RID &p_rid, int p_idx, int p_level) {
  1084. ERR_FAIL_COND(p_idx < 0);
  1085. MenuData *md = menus.get_or_null(p_rid);
  1086. ERR_FAIL_NULL(md);
  1087. int item_start = _get_system_menu_start(md->menu);
  1088. int item_count = _get_system_menu_count(md->menu);
  1089. p_idx += item_start;
  1090. ERR_FAIL_COND(p_idx >= item_start + item_count);
  1091. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  1092. if (menu_item) {
  1093. [menu_item setIndentationLevel:p_level];
  1094. }
  1095. }
  1096. int NativeMenuMacOS::get_item_count(const RID &p_rid) const {
  1097. const MenuData *md = menus.get_or_null(p_rid);
  1098. ERR_FAIL_NULL_V(md, 0);
  1099. return _get_system_menu_count(md->menu);
  1100. }
  1101. bool NativeMenuMacOS::is_system_menu(const RID &p_rid) const {
  1102. const MenuData *md = menus.get_or_null(p_rid);
  1103. ERR_FAIL_NULL_V(md, false);
  1104. return md->is_system;
  1105. }
  1106. void NativeMenuMacOS::remove_item(const RID &p_rid, int p_idx) {
  1107. ERR_FAIL_COND(p_idx < 0);
  1108. MenuData *md = menus.get_or_null(p_rid);
  1109. ERR_FAIL_NULL(md);
  1110. int item_start = _get_system_menu_start(md->menu);
  1111. int item_count = _get_system_menu_count(md->menu);
  1112. p_idx += item_start;
  1113. ERR_FAIL_COND(p_idx >= item_start + item_count);
  1114. NSMenuItem *menu_item = [md->menu itemAtIndex:p_idx];
  1115. if ([menu_item submenu] && _is_menu_opened([menu_item submenu])) {
  1116. ERR_PRINT("Can't remove open menu!");
  1117. return;
  1118. }
  1119. [md->menu removeItemAtIndex:p_idx];
  1120. }
  1121. void NativeMenuMacOS::clear(const RID &p_rid) {
  1122. MenuData *md = menus.get_or_null(p_rid);
  1123. ERR_FAIL_NULL(md);
  1124. ERR_FAIL_COND_MSG(_is_menu_opened(md->menu), "Can't remove open menu!");
  1125. if (p_rid == application_menu || p_rid == window_menu || p_rid == help_menu) {
  1126. int start = _get_system_menu_start(md->menu);
  1127. int count = _get_system_menu_count(md->menu);
  1128. for (int i = start + count - 1; i >= start; i--) {
  1129. [md->menu removeItemAtIndex:i];
  1130. }
  1131. } else {
  1132. [md->menu removeAllItems];
  1133. }
  1134. if (p_rid == main_menu) {
  1135. // Restore Apple, Window and Help menu.
  1136. MenuData *md_app = menus.get_or_null(application_menu);
  1137. if (md_app) {
  1138. NSMenuItem *menu_item = [md->menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
  1139. [md->menu setSubmenu:md_app->menu forItem:menu_item];
  1140. }
  1141. MenuData *md_win = menus.get_or_null(window_menu);
  1142. if (md_win) {
  1143. NSMenuItem *menu_item = [md->menu addItemWithTitle:@"Window" action:nil keyEquivalent:@""];
  1144. [md->menu setSubmenu:md_win->menu forItem:menu_item];
  1145. }
  1146. MenuData *md_hlp = menus.get_or_null(help_menu);
  1147. if (md_hlp) {
  1148. NSMenuItem *menu_item = [md->menu addItemWithTitle:@"Help" action:nil keyEquivalent:@""];
  1149. [md->menu setSubmenu:md_hlp->menu forItem:menu_item];
  1150. }
  1151. }
  1152. }
  1153. NativeMenuMacOS::NativeMenuMacOS() {}
  1154. NativeMenuMacOS::~NativeMenuMacOS() {
  1155. if (main_menu.is_valid()) {
  1156. MenuData *md = menus.get_or_null(main_menu);
  1157. if (md) {
  1158. clear(main_menu);
  1159. menus.free(main_menu);
  1160. menu_lookup.erase(md->menu);
  1161. md->menu = nullptr;
  1162. main_menu_ns = nullptr;
  1163. memdelete(md);
  1164. }
  1165. }
  1166. if (application_menu.is_valid()) {
  1167. MenuData *md = menus.get_or_null(application_menu);
  1168. if (md) {
  1169. clear(application_menu);
  1170. menus.free(application_menu);
  1171. menu_lookup.erase(md->menu);
  1172. md->menu = nullptr;
  1173. application_menu_ns = nullptr;
  1174. memdelete(md);
  1175. }
  1176. }
  1177. if (window_menu.is_valid()) {
  1178. MenuData *md = menus.get_or_null(window_menu);
  1179. if (md) {
  1180. clear(window_menu);
  1181. menus.free(window_menu);
  1182. menu_lookup.erase(md->menu);
  1183. md->menu = nullptr;
  1184. window_menu_ns = nullptr;
  1185. memdelete(md);
  1186. }
  1187. }
  1188. if (help_menu.is_valid()) {
  1189. MenuData *md = menus.get_or_null(help_menu);
  1190. if (md) {
  1191. clear(help_menu);
  1192. menus.free(help_menu);
  1193. menu_lookup.erase(md->menu);
  1194. md->menu = nullptr;
  1195. help_menu_ns = nullptr;
  1196. memdelete(md);
  1197. }
  1198. }
  1199. if (dock_menu.is_valid()) {
  1200. MenuData *md = menus.get_or_null(dock_menu);
  1201. if (md) {
  1202. clear(dock_menu);
  1203. menus.free(dock_menu);
  1204. menu_lookup.erase(md->menu);
  1205. md->menu = nullptr;
  1206. dock_menu_ns = nullptr;
  1207. memdelete(md);
  1208. }
  1209. }
  1210. }