native_menu_macos.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**************************************************************************/
  2. /* native_menu_macos.h */
  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. #ifndef NATIVE_MENU_MACOS_H
  31. #define NATIVE_MENU_MACOS_H
  32. #include "core/templates/hash_map.h"
  33. #include "core/templates/rid_owner.h"
  34. #include "servers/display/native_menu.h"
  35. #import <AppKit/AppKit.h>
  36. #import <ApplicationServices/ApplicationServices.h>
  37. class NativeMenuMacOS : public NativeMenu {
  38. GDCLASS(NativeMenuMacOS, NativeMenu)
  39. struct MenuData {
  40. NSMenu *menu = nullptr;
  41. Callable open_cb;
  42. Callable close_cb;
  43. bool is_open = false;
  44. bool is_system = false;
  45. };
  46. mutable RID_PtrOwner<MenuData> menus;
  47. HashMap<NSMenu *, RID> menu_lookup;
  48. NSMenu *main_menu_ns = nullptr;
  49. NSMenu *application_menu_ns = nullptr;
  50. NSMenu *window_menu_ns = nullptr;
  51. NSMenu *help_menu_ns = nullptr;
  52. NSMenu *dock_menu_ns = nullptr;
  53. RID main_menu;
  54. RID application_menu;
  55. RID window_menu;
  56. RID help_menu;
  57. RID dock_menu;
  58. int _get_system_menu_start(const NSMenu *p_menu) const;
  59. int _get_system_menu_count(const NSMenu *p_menu) const;
  60. bool _is_menu_opened(NSMenu *p_menu) const;
  61. NSMenuItem *_menu_add_item(NSMenu *p_menu, const String &p_label, Key p_accel, int p_index, int *r_out);
  62. public:
  63. void _register_system_menus(NSMenu *p_main_menu, NSMenu *p_application_menu, NSMenu *p_window_menu, NSMenu *p_help_menu, NSMenu *p_dock_menu);
  64. NSMenu *_get_dock_menu();
  65. void _menu_need_update(NSMenu *p_menu);
  66. void _menu_open(NSMenu *p_menu);
  67. void _menu_close(NSMenu *p_menu);
  68. void _menu_close_cb(const RID &p_rid);
  69. virtual bool has_feature(Feature p_feature) const override;
  70. virtual bool has_system_menu(SystemMenus p_menu_id) const override;
  71. virtual RID get_system_menu(SystemMenus p_menu_id) const override;
  72. virtual RID create_menu() override;
  73. virtual bool has_menu(const RID &p_rid) const override;
  74. virtual void free_menu(const RID &p_rid) override;
  75. NSMenu *get_native_menu_handle(const RID &p_rid);
  76. virtual Size2 get_size(const RID &p_rid) const override;
  77. virtual void popup(const RID &p_rid, const Vector2i &p_position) override;
  78. virtual void set_interface_direction(const RID &p_rid, bool p_is_rtl) override;
  79. virtual void set_popup_open_callback(const RID &p_rid, const Callable &p_callback) override;
  80. virtual Callable get_popup_open_callback(const RID &p_rid) const override;
  81. virtual void set_popup_close_callback(const RID &p_rid, const Callable &p_callback) override;
  82. virtual Callable get_popup_close_callback(const RID &p_rid) const override;
  83. virtual void set_minimum_width(const RID &p_rid, float p_width) override;
  84. virtual float get_minimum_width(const RID &p_rid) const override;
  85. virtual bool is_opened(const RID &p_rid) const override;
  86. virtual int add_submenu_item(const RID &p_rid, const String &p_label, const RID &p_submenu_rid, const Variant &p_tag = Variant(), int p_index = -1) override;
  87. virtual int add_item(const RID &p_rid, const String &p_label, const Callable &p_callback = Callable(), const Callable &p_key_callback = Callable(), const Variant &p_tag = Variant(), Key p_accel = Key::NONE, int p_index = -1) override;
  88. virtual int add_check_item(const RID &p_rid, const String &p_label, const Callable &p_callback = Callable(), const Callable &p_key_callback = Callable(), const Variant &p_tag = Variant(), Key p_accel = Key::NONE, int p_index = -1) override;
  89. virtual int add_icon_item(const RID &p_rid, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback = Callable(), const Callable &p_key_callback = Callable(), const Variant &p_tag = Variant(), Key p_accel = Key::NONE, int p_index = -1) override;
  90. virtual int add_icon_check_item(const RID &p_rid, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback = Callable(), const Callable &p_key_callback = Callable(), const Variant &p_tag = Variant(), Key p_accel = Key::NONE, int p_index = -1) override;
  91. virtual int add_radio_check_item(const RID &p_rid, const String &p_label, const Callable &p_callback = Callable(), const Callable &p_key_callback = Callable(), const Variant &p_tag = Variant(), Key p_accel = Key::NONE, int p_index = -1) override;
  92. virtual int add_icon_radio_check_item(const RID &p_rid, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback = Callable(), const Callable &p_key_callback = Callable(), const Variant &p_tag = Variant(), Key p_accel = Key::NONE, int p_index = -1) override;
  93. virtual int add_multistate_item(const RID &p_rid, const String &p_label, int p_max_states, int p_default_state, const Callable &p_callback = Callable(), const Callable &p_key_callback = Callable(), const Variant &p_tag = Variant(), Key p_accel = Key::NONE, int p_index = -1) override;
  94. virtual int add_separator(const RID &p_rid, int p_index = -1) override;
  95. virtual int find_item_index_with_text(const RID &p_rid, const String &p_text) const override;
  96. virtual int find_item_index_with_tag(const RID &p_rid, const Variant &p_tag) const override;
  97. virtual bool is_item_checked(const RID &p_rid, int p_idx) const override;
  98. virtual bool is_item_checkable(const RID &p_rid, int p_idx) const override;
  99. virtual bool is_item_radio_checkable(const RID &p_rid, int p_idx) const override;
  100. virtual Callable get_item_callback(const RID &p_rid, int p_idx) const override;
  101. virtual Callable get_item_key_callback(const RID &p_rid, int p_idx) const override;
  102. virtual Variant get_item_tag(const RID &p_rid, int p_idx) const override;
  103. virtual String get_item_text(const RID &p_rid, int p_idx) const override;
  104. virtual RID get_item_submenu(const RID &p_rid, int p_idx) const override;
  105. virtual Key get_item_accelerator(const RID &p_rid, int p_idx) const override;
  106. virtual bool is_item_disabled(const RID &p_rid, int p_idx) const override;
  107. virtual bool is_item_hidden(const RID &p_rid, int p_idx) const override;
  108. virtual String get_item_tooltip(const RID &p_rid, int p_idx) const override;
  109. virtual int get_item_state(const RID &p_rid, int p_idx) const override;
  110. virtual int get_item_max_states(const RID &p_rid, int p_idx) const override;
  111. virtual Ref<Texture2D> get_item_icon(const RID &p_rid, int p_idx) const override;
  112. virtual int get_item_indentation_level(const RID &p_rid, int p_idx) const override;
  113. virtual void set_item_checked(const RID &p_rid, int p_idx, bool p_checked) override;
  114. virtual void set_item_checkable(const RID &p_rid, int p_idx, bool p_checkable) override;
  115. virtual void set_item_radio_checkable(const RID &p_rid, int p_idx, bool p_checkable) override;
  116. virtual void set_item_callback(const RID &p_rid, int p_idx, const Callable &p_callback) override;
  117. virtual void set_item_key_callback(const RID &p_rid, int p_idx, const Callable &p_key_callback) override;
  118. virtual void set_item_hover_callbacks(const RID &p_rid, int p_idx, const Callable &p_callback) override;
  119. virtual void set_item_tag(const RID &p_rid, int p_idx, const Variant &p_tag) override;
  120. virtual void set_item_text(const RID &p_rid, int p_idx, const String &p_text) override;
  121. virtual void set_item_submenu(const RID &p_rid, int p_idx, const RID &p_submenu_rid) override;
  122. virtual void set_item_accelerator(const RID &p_rid, int p_idx, Key p_keycode) override;
  123. virtual void set_item_disabled(const RID &p_rid, int p_idx, bool p_disabled) override;
  124. virtual void set_item_hidden(const RID &p_rid, int p_idx, bool p_hidden) override;
  125. virtual void set_item_tooltip(const RID &p_rid, int p_idx, const String &p_tooltip) override;
  126. virtual void set_item_state(const RID &p_rid, int p_idx, int p_state) override;
  127. virtual void set_item_max_states(const RID &p_rid, int p_idx, int p_max_states) override;
  128. virtual void set_item_icon(const RID &p_rid, int p_idx, const Ref<Texture2D> &p_icon) override;
  129. virtual void set_item_indentation_level(const RID &p_rid, int p_idx, int p_level) override;
  130. virtual int get_item_count(const RID &p_rid) const override;
  131. virtual bool is_system_menu(const RID &p_rid) const override;
  132. virtual void remove_item(const RID &p_rid, int p_idx) override;
  133. virtual void clear(const RID &p_rid) override;
  134. NativeMenuMacOS();
  135. ~NativeMenuMacOS();
  136. };
  137. #endif // NATIVE_MENU_MACOS_H