native_menu_windows.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /**************************************************************************/
  2. /* native_menu_windows.cpp */
  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_windows.h"
  31. #include "display_server_windows.h"
  32. #include "scene/resources/image_texture.h"
  33. HBITMAP NativeMenuWindows::_make_bitmap(const Ref<Image> &p_img) const {
  34. p_img->convert(Image::FORMAT_RGBA8);
  35. Vector2i texture_size = p_img->get_size();
  36. UINT image_size = texture_size.width * texture_size.height;
  37. COLORREF *buffer = nullptr;
  38. BITMAPV5HEADER bi;
  39. ZeroMemory(&bi, sizeof(bi));
  40. bi.bV5Size = sizeof(bi);
  41. bi.bV5Width = texture_size.width;
  42. bi.bV5Height = -texture_size.height;
  43. bi.bV5Planes = 1;
  44. bi.bV5BitCount = 32;
  45. bi.bV5Compression = BI_BITFIELDS;
  46. bi.bV5RedMask = 0x00ff0000;
  47. bi.bV5GreenMask = 0x0000ff00;
  48. bi.bV5BlueMask = 0x000000ff;
  49. bi.bV5AlphaMask = 0xff000000;
  50. HDC dc = GetDC(nullptr);
  51. HBITMAP bitmap = CreateDIBSection(dc, reinterpret_cast<BITMAPINFO *>(&bi), DIB_RGB_COLORS, reinterpret_cast<void **>(&buffer), nullptr, 0);
  52. for (UINT index = 0; index < image_size; index++) {
  53. int row_index = floor(index / texture_size.width);
  54. int column_index = (index % int(texture_size.width));
  55. const Color &c = p_img->get_pixel(column_index, row_index);
  56. *(buffer + index) = c.to_argb32();
  57. }
  58. ReleaseDC(nullptr, dc);
  59. return bitmap;
  60. }
  61. void NativeMenuWindows::_menu_activate(HMENU p_menu, int p_index) const {
  62. if (menu_lookup.has(p_menu)) {
  63. MenuData *md = menus.get_or_null(menu_lookup[p_menu]);
  64. if (md) {
  65. int count = GetMenuItemCount(md->menu);
  66. if (p_index >= 0 && p_index < count) {
  67. MENUITEMINFOW item;
  68. ZeroMemory(&item, sizeof(item));
  69. item.cbSize = sizeof(item);
  70. item.fMask = MIIM_STATE | MIIM_DATA;
  71. if (GetMenuItemInfoW(md->menu, p_index, true, &item)) {
  72. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  73. if (item_data) {
  74. if (item_data->callback.is_valid()) {
  75. Variant ret;
  76. Callable::CallError ce;
  77. const Variant *args[1] = { &item_data->meta };
  78. item_data->callback.callp(args, 1, ret, ce);
  79. if (ce.error != Callable::CallError::CALL_OK) {
  80. ERR_PRINT(vformat("Failed to execute menu callback: %s.", Variant::get_callable_error_text(item_data->callback, args, 1, ce)));
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. bool NativeMenuWindows::has_feature(Feature p_feature) const {
  90. switch (p_feature) {
  91. // case FEATURE_GLOBAL_MENU:
  92. // case FEATURE_OPEN_CLOSE_CALLBACK:
  93. // case FEATURE_HOVER_CALLBACK:
  94. // case FEATURE_KEY_CALLBACK:
  95. case FEATURE_POPUP_MENU:
  96. return true;
  97. default:
  98. return false;
  99. }
  100. }
  101. bool NativeMenuWindows::has_system_menu(SystemMenus p_menu_id) const {
  102. return false;
  103. }
  104. RID NativeMenuWindows::get_system_menu(SystemMenus p_menu_id) const {
  105. return RID();
  106. }
  107. RID NativeMenuWindows::create_menu() {
  108. MenuData *md = memnew(MenuData);
  109. md->menu = CreatePopupMenu();
  110. MENUINFO menu_info;
  111. ZeroMemory(&menu_info, sizeof(menu_info));
  112. menu_info.cbSize = sizeof(menu_info);
  113. menu_info.fMask = MIM_STYLE;
  114. menu_info.dwStyle = MNS_NOTIFYBYPOS;
  115. SetMenuInfo(md->menu, &menu_info);
  116. RID rid = menus.make_rid(md);
  117. menu_lookup[md->menu] = rid;
  118. return rid;
  119. }
  120. bool NativeMenuWindows::has_menu(const RID &p_rid) const {
  121. return menus.owns(p_rid);
  122. }
  123. void NativeMenuWindows::free_menu(const RID &p_rid) {
  124. MenuData *md = menus.get_or_null(p_rid);
  125. if (md) {
  126. clear(p_rid);
  127. DestroyMenu(md->menu);
  128. menus.free(p_rid);
  129. menu_lookup.erase(md->menu);
  130. memdelete(md);
  131. }
  132. }
  133. Size2 NativeMenuWindows::get_size(const RID &p_rid) const {
  134. const MenuData *md = menus.get_or_null(p_rid);
  135. ERR_FAIL_NULL_V(md, Size2());
  136. Size2 size;
  137. int count = GetMenuItemCount(md->menu);
  138. for (int i = 0; i < count; i++) {
  139. RECT rect;
  140. if (GetMenuItemRect(NULL, md->menu, i, &rect)) {
  141. size.x = MAX(size.x, rect.right - rect.left);
  142. size.y += rect.bottom - rect.top;
  143. }
  144. }
  145. return size;
  146. }
  147. void NativeMenuWindows::popup(const RID &p_rid, const Vector2i &p_position) {
  148. const MenuData *md = menus.get_or_null(p_rid);
  149. ERR_FAIL_NULL(md);
  150. HWND hwnd = (HWND)DisplayServer::get_singleton()->window_get_native_handle(DisplayServer::WINDOW_HANDLE, DisplayServer::MAIN_WINDOW_ID);
  151. UINT flags = TPM_HORIZONTAL | TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_VERPOSANIMATION;
  152. if (md->is_rtl) {
  153. flags |= TPM_LAYOUTRTL;
  154. }
  155. SetForegroundWindow(hwnd);
  156. TrackPopupMenuEx(md->menu, flags, p_position.x, p_position.y, hwnd, nullptr);
  157. PostMessage(hwnd, WM_NULL, 0, 0);
  158. }
  159. void NativeMenuWindows::set_interface_direction(const RID &p_rid, bool p_is_rtl) {
  160. MenuData *md = menus.get_or_null(p_rid);
  161. ERR_FAIL_NULL(md);
  162. if (md->is_rtl == p_is_rtl) {
  163. return;
  164. }
  165. md->is_rtl = p_is_rtl;
  166. }
  167. void NativeMenuWindows::set_popup_open_callback(const RID &p_rid, const Callable &p_callback) {
  168. // Not supported.
  169. }
  170. Callable NativeMenuWindows::get_popup_open_callback(const RID &p_rid) const {
  171. // Not supported.
  172. return Callable();
  173. }
  174. void NativeMenuWindows::set_popup_close_callback(const RID &p_rid, const Callable &p_callback) {
  175. // Not supported.
  176. }
  177. Callable NativeMenuWindows::get_popup_close_callback(const RID &p_rid) const {
  178. // Not supported.
  179. return Callable();
  180. }
  181. void NativeMenuWindows::set_minimum_width(const RID &p_rid, float p_width) {
  182. // Not supported.
  183. }
  184. float NativeMenuWindows::get_minimum_width(const RID &p_rid) const {
  185. // Not supported.
  186. return 0.f;
  187. }
  188. bool NativeMenuWindows::is_opened(const RID &p_rid) const {
  189. // Not supported.
  190. return false;
  191. }
  192. int NativeMenuWindows::add_submenu_item(const RID &p_rid, const String &p_label, const RID &p_submenu_rid, const Variant &p_tag, int p_index) {
  193. MenuData *md = menus.get_or_null(p_rid);
  194. MenuData *md_sub = menus.get_or_null(p_submenu_rid);
  195. ERR_FAIL_NULL_V(md, -1);
  196. ERR_FAIL_NULL_V(md_sub, -1);
  197. ERR_FAIL_COND_V_MSG(md->menu == md_sub->menu, -1, "Can't set submenu to self!");
  198. if (p_index == -1) {
  199. p_index = GetMenuItemCount(md->menu);
  200. } else {
  201. p_index = CLAMP(p_index, 0, GetMenuItemCount(md->menu));
  202. }
  203. MenuItemData *item_data = memnew(MenuItemData);
  204. item_data->meta = p_tag;
  205. item_data->checkable_type = CHECKABLE_TYPE_NONE;
  206. item_data->max_states = 0;
  207. item_data->state = 0;
  208. Char16String label = p_label.utf16();
  209. MENUITEMINFOW item;
  210. ZeroMemory(&item, sizeof(item));
  211. item.cbSize = sizeof(item);
  212. item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA | MIIM_SUBMENU;
  213. item.fType = MFT_STRING;
  214. item.hSubMenu = md_sub->menu;
  215. item.dwItemData = (ULONG_PTR)item_data;
  216. item.dwTypeData = (LPWSTR)label.get_data();
  217. if (!InsertMenuItemW(md->menu, p_index, true, &item)) {
  218. memdelete(item_data);
  219. return -1;
  220. }
  221. return p_index;
  222. }
  223. int NativeMenuWindows::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) {
  224. const MenuData *md = menus.get_or_null(p_rid);
  225. ERR_FAIL_NULL_V(md, -1);
  226. if (p_index == -1) {
  227. p_index = GetMenuItemCount(md->menu);
  228. } else {
  229. p_index = CLAMP(p_index, 0, GetMenuItemCount(md->menu));
  230. }
  231. MenuItemData *item_data = memnew(MenuItemData);
  232. item_data->callback = p_callback;
  233. item_data->meta = p_tag;
  234. item_data->checkable_type = CHECKABLE_TYPE_NONE;
  235. item_data->max_states = 0;
  236. item_data->state = 0;
  237. Char16String label = p_label.utf16();
  238. MENUITEMINFOW item;
  239. ZeroMemory(&item, sizeof(item));
  240. item.cbSize = sizeof(item);
  241. item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA;
  242. item.fType = MFT_STRING;
  243. item.dwItemData = (ULONG_PTR)item_data;
  244. item.dwTypeData = (LPWSTR)label.get_data();
  245. if (!InsertMenuItemW(md->menu, p_index, true, &item)) {
  246. memdelete(item_data);
  247. return -1;
  248. }
  249. return p_index;
  250. }
  251. int NativeMenuWindows::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) {
  252. const MenuData *md = menus.get_or_null(p_rid);
  253. ERR_FAIL_NULL_V(md, -1);
  254. if (p_index == -1) {
  255. p_index = GetMenuItemCount(md->menu);
  256. } else {
  257. p_index = CLAMP(p_index, 0, GetMenuItemCount(md->menu));
  258. }
  259. MenuItemData *item_data = memnew(MenuItemData);
  260. item_data->callback = p_callback;
  261. item_data->meta = p_tag;
  262. item_data->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
  263. item_data->max_states = 0;
  264. item_data->state = 0;
  265. Char16String label = p_label.utf16();
  266. MENUITEMINFOW item;
  267. ZeroMemory(&item, sizeof(item));
  268. item.cbSize = sizeof(item);
  269. item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA;
  270. item.fType = MFT_STRING;
  271. item.dwItemData = (ULONG_PTR)item_data;
  272. item.dwTypeData = (LPWSTR)label.get_data();
  273. if (!InsertMenuItemW(md->menu, p_index, true, &item)) {
  274. memdelete(item_data);
  275. return -1;
  276. }
  277. return p_index;
  278. }
  279. int NativeMenuWindows::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) {
  280. const MenuData *md = menus.get_or_null(p_rid);
  281. ERR_FAIL_NULL_V(md, -1);
  282. if (p_index == -1) {
  283. p_index = GetMenuItemCount(md->menu);
  284. } else {
  285. p_index = CLAMP(p_index, 0, GetMenuItemCount(md->menu));
  286. }
  287. MenuItemData *item_data = memnew(MenuItemData);
  288. item_data->callback = p_callback;
  289. item_data->meta = p_tag;
  290. item_data->checkable_type = CHECKABLE_TYPE_NONE;
  291. item_data->max_states = 0;
  292. item_data->state = 0;
  293. if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) {
  294. item_data->img = p_icon->get_image();
  295. item_data->img = item_data->img->duplicate();
  296. if (item_data->img->is_compressed()) {
  297. item_data->img->decompress();
  298. }
  299. item_data->bmp = _make_bitmap(item_data->img);
  300. }
  301. Char16String label = p_label.utf16();
  302. MENUITEMINFOW item;
  303. ZeroMemory(&item, sizeof(item));
  304. item.cbSize = sizeof(item);
  305. item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA | MIIM_BITMAP;
  306. item.fType = MFT_STRING;
  307. item.dwItemData = (ULONG_PTR)item_data;
  308. item.dwTypeData = (LPWSTR)label.get_data();
  309. item.hbmpItem = item_data->bmp;
  310. if (!InsertMenuItemW(md->menu, p_index, true, &item)) {
  311. memdelete(item_data);
  312. return -1;
  313. }
  314. return p_index;
  315. }
  316. int NativeMenuWindows::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) {
  317. const MenuData *md = menus.get_or_null(p_rid);
  318. ERR_FAIL_NULL_V(md, -1);
  319. if (p_index == -1) {
  320. p_index = GetMenuItemCount(md->menu);
  321. } else {
  322. p_index = CLAMP(p_index, 0, GetMenuItemCount(md->menu));
  323. }
  324. MenuItemData *item_data = memnew(MenuItemData);
  325. item_data->callback = p_callback;
  326. item_data->meta = p_tag;
  327. item_data->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
  328. item_data->max_states = 0;
  329. item_data->state = 0;
  330. if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) {
  331. item_data->img = p_icon->get_image();
  332. item_data->img = item_data->img->duplicate();
  333. if (item_data->img->is_compressed()) {
  334. item_data->img->decompress();
  335. }
  336. item_data->bmp = _make_bitmap(item_data->img);
  337. }
  338. Char16String label = p_label.utf16();
  339. MENUITEMINFOW item;
  340. ZeroMemory(&item, sizeof(item));
  341. item.cbSize = sizeof(item);
  342. item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA | MIIM_BITMAP;
  343. item.fType = MFT_STRING;
  344. item.dwItemData = (ULONG_PTR)item_data;
  345. item.dwTypeData = (LPWSTR)label.get_data();
  346. item.hbmpItem = item_data->bmp;
  347. if (!InsertMenuItemW(md->menu, p_index, true, &item)) {
  348. memdelete(item_data);
  349. return -1;
  350. }
  351. return p_index;
  352. }
  353. int NativeMenuWindows::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) {
  354. const MenuData *md = menus.get_or_null(p_rid);
  355. ERR_FAIL_NULL_V(md, -1);
  356. if (p_index == -1) {
  357. p_index = GetMenuItemCount(md->menu);
  358. } else {
  359. p_index = CLAMP(p_index, 0, GetMenuItemCount(md->menu));
  360. }
  361. MenuItemData *item_data = memnew(MenuItemData);
  362. item_data->callback = p_callback;
  363. item_data->meta = p_tag;
  364. item_data->checkable_type = CHECKABLE_TYPE_RADIO_BUTTON;
  365. item_data->max_states = 0;
  366. item_data->state = 0;
  367. Char16String label = p_label.utf16();
  368. MENUITEMINFOW item;
  369. ZeroMemory(&item, sizeof(item));
  370. item.cbSize = sizeof(item);
  371. item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA;
  372. item.fType = MFT_STRING | MFT_RADIOCHECK;
  373. item.dwItemData = (ULONG_PTR)item_data;
  374. item.dwTypeData = (LPWSTR)label.get_data();
  375. if (!InsertMenuItemW(md->menu, p_index, true, &item)) {
  376. memdelete(item_data);
  377. return -1;
  378. }
  379. return p_index;
  380. }
  381. int NativeMenuWindows::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) {
  382. const MenuData *md = menus.get_or_null(p_rid);
  383. ERR_FAIL_NULL_V(md, -1);
  384. if (p_index == -1) {
  385. p_index = GetMenuItemCount(md->menu);
  386. } else {
  387. p_index = CLAMP(p_index, 0, GetMenuItemCount(md->menu));
  388. }
  389. MenuItemData *item_data = memnew(MenuItemData);
  390. item_data->callback = p_callback;
  391. item_data->meta = p_tag;
  392. item_data->checkable_type = CHECKABLE_TYPE_RADIO_BUTTON;
  393. item_data->max_states = 0;
  394. item_data->state = 0;
  395. if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) {
  396. item_data->img = p_icon->get_image();
  397. item_data->img = item_data->img->duplicate();
  398. if (item_data->img->is_compressed()) {
  399. item_data->img->decompress();
  400. }
  401. item_data->bmp = _make_bitmap(item_data->img);
  402. }
  403. Char16String label = p_label.utf16();
  404. MENUITEMINFOW item;
  405. ZeroMemory(&item, sizeof(item));
  406. item.cbSize = sizeof(item);
  407. item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA | MIIM_BITMAP;
  408. item.fType = MFT_STRING | MFT_RADIOCHECK;
  409. item.dwItemData = (ULONG_PTR)item_data;
  410. item.dwTypeData = (LPWSTR)label.get_data();
  411. item.hbmpItem = item_data->bmp;
  412. if (!InsertMenuItemW(md->menu, p_index, true, &item)) {
  413. memdelete(item_data);
  414. return -1;
  415. }
  416. return p_index;
  417. }
  418. int NativeMenuWindows::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) {
  419. const MenuData *md = menus.get_or_null(p_rid);
  420. ERR_FAIL_NULL_V(md, -1);
  421. if (p_index == -1) {
  422. p_index = GetMenuItemCount(md->menu);
  423. } else {
  424. p_index = CLAMP(p_index, 0, GetMenuItemCount(md->menu));
  425. }
  426. MenuItemData *item_data = memnew(MenuItemData);
  427. item_data->callback = p_callback;
  428. item_data->meta = p_tag;
  429. item_data->checkable_type = CHECKABLE_TYPE_NONE;
  430. item_data->max_states = p_max_states;
  431. item_data->state = p_default_state;
  432. Char16String label = p_label.utf16();
  433. MENUITEMINFOW item;
  434. ZeroMemory(&item, sizeof(item));
  435. item.cbSize = sizeof(item);
  436. item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA;
  437. item.fType = MFT_STRING;
  438. item.dwItemData = (ULONG_PTR)item_data;
  439. item.dwTypeData = (LPWSTR)label.get_data();
  440. if (!InsertMenuItemW(md->menu, p_index, true, &item)) {
  441. memdelete(item_data);
  442. return -1;
  443. }
  444. return p_index;
  445. }
  446. int NativeMenuWindows::add_separator(const RID &p_rid, int p_index) {
  447. const MenuData *md = menus.get_or_null(p_rid);
  448. ERR_FAIL_NULL_V(md, -1);
  449. if (p_index == -1) {
  450. p_index = GetMenuItemCount(md->menu);
  451. } else {
  452. p_index = CLAMP(p_index, 0, GetMenuItemCount(md->menu));
  453. }
  454. MenuItemData *item_data = memnew(MenuItemData);
  455. item_data->checkable_type = CHECKABLE_TYPE_NONE;
  456. item_data->max_states = 0;
  457. item_data->state = 0;
  458. MENUITEMINFOW item;
  459. ZeroMemory(&item, sizeof(item));
  460. item.cbSize = sizeof(item);
  461. item.fMask = MIIM_FTYPE | MIIM_DATA;
  462. item.fType = MFT_SEPARATOR;
  463. item.dwItemData = (ULONG_PTR)item_data;
  464. if (!InsertMenuItemW(md->menu, p_index, true, &item)) {
  465. memdelete(item_data);
  466. return -1;
  467. }
  468. return p_index;
  469. }
  470. int NativeMenuWindows::find_item_index_with_text(const RID &p_rid, const String &p_text) const {
  471. const MenuData *md = menus.get_or_null(p_rid);
  472. ERR_FAIL_NULL_V(md, -1);
  473. MENUITEMINFOW item;
  474. int count = GetMenuItemCount(md->menu);
  475. for (int i = 0; i < count; i++) {
  476. ZeroMemory(&item, sizeof(item));
  477. item.cbSize = sizeof(item);
  478. item.fMask = MIIM_STRING;
  479. item.dwTypeData = nullptr;
  480. if (GetMenuItemInfoW(md->menu, i, true, &item)) {
  481. item.cch++;
  482. Char16String str;
  483. str.resize(item.cch);
  484. item.dwTypeData = (LPWSTR)str.ptrw();
  485. if (GetMenuItemInfoW(md->menu, i, true, &item)) {
  486. if (String::utf16((const char16_t *)str.get_data()) == p_text) {
  487. return i;
  488. }
  489. }
  490. }
  491. }
  492. return -1;
  493. }
  494. int NativeMenuWindows::find_item_index_with_tag(const RID &p_rid, const Variant &p_tag) const {
  495. const MenuData *md = menus.get_or_null(p_rid);
  496. ERR_FAIL_NULL_V(md, -1);
  497. MENUITEMINFOW item;
  498. int count = GetMenuItemCount(md->menu);
  499. for (int i = 0; i < count; i++) {
  500. ZeroMemory(&item, sizeof(item));
  501. item.cbSize = sizeof(item);
  502. item.fMask = MIIM_DATA;
  503. if (GetMenuItemInfoW(md->menu, i, true, &item)) {
  504. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  505. if (item_data) {
  506. if (item_data->meta == p_tag) {
  507. return i;
  508. }
  509. }
  510. }
  511. }
  512. return -1;
  513. }
  514. bool NativeMenuWindows::is_item_checked(const RID &p_rid, int p_idx) const {
  515. ERR_FAIL_COND_V(p_idx < 0, false);
  516. const MenuData *md = menus.get_or_null(p_rid);
  517. ERR_FAIL_NULL_V(md, false);
  518. int count = GetMenuItemCount(md->menu);
  519. ERR_FAIL_COND_V(p_idx >= count, false);
  520. MENUITEMINFOW item;
  521. ZeroMemory(&item, sizeof(item));
  522. item.cbSize = sizeof(item);
  523. item.fMask = MIIM_STATE | MIIM_DATA;
  524. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  525. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  526. if (item_data) {
  527. return item_data->checked;
  528. }
  529. }
  530. return false;
  531. }
  532. bool NativeMenuWindows::is_item_checkable(const RID &p_rid, int p_idx) const {
  533. ERR_FAIL_COND_V(p_idx < 0, false);
  534. const MenuData *md = menus.get_or_null(p_rid);
  535. ERR_FAIL_NULL_V(md, false);
  536. int count = GetMenuItemCount(md->menu);
  537. ERR_FAIL_COND_V(p_idx >= count, false);
  538. MENUITEMINFOW item;
  539. ZeroMemory(&item, sizeof(item));
  540. item.cbSize = sizeof(item);
  541. item.fMask = MIIM_DATA;
  542. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  543. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  544. if (item_data) {
  545. return item_data->checkable_type == CHECKABLE_TYPE_CHECK_BOX;
  546. }
  547. }
  548. return false;
  549. }
  550. bool NativeMenuWindows::is_item_radio_checkable(const RID &p_rid, int p_idx) const {
  551. ERR_FAIL_COND_V(p_idx < 0, false);
  552. const MenuData *md = menus.get_or_null(p_rid);
  553. ERR_FAIL_NULL_V(md, false);
  554. int count = GetMenuItemCount(md->menu);
  555. ERR_FAIL_COND_V(p_idx >= count, false);
  556. MENUITEMINFOW item;
  557. ZeroMemory(&item, sizeof(item));
  558. item.cbSize = sizeof(item);
  559. item.fMask = MIIM_DATA;
  560. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  561. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  562. if (item_data) {
  563. return item_data->checkable_type == CHECKABLE_TYPE_RADIO_BUTTON;
  564. }
  565. }
  566. return false;
  567. }
  568. Callable NativeMenuWindows::get_item_callback(const RID &p_rid, int p_idx) const {
  569. ERR_FAIL_COND_V(p_idx < 0, Callable());
  570. const MenuData *md = menus.get_or_null(p_rid);
  571. ERR_FAIL_NULL_V(md, Callable());
  572. int count = GetMenuItemCount(md->menu);
  573. ERR_FAIL_COND_V(p_idx >= count, Callable());
  574. MENUITEMINFOW item;
  575. ZeroMemory(&item, sizeof(item));
  576. item.cbSize = sizeof(item);
  577. item.fMask = MIIM_DATA;
  578. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  579. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  580. if (item_data) {
  581. return item_data->callback;
  582. }
  583. }
  584. return Callable();
  585. }
  586. Callable NativeMenuWindows::get_item_key_callback(const RID &p_rid, int p_idx) const {
  587. // Not supported.
  588. return Callable();
  589. }
  590. Variant NativeMenuWindows::get_item_tag(const RID &p_rid, int p_idx) const {
  591. ERR_FAIL_COND_V(p_idx < 0, Variant());
  592. const MenuData *md = menus.get_or_null(p_rid);
  593. ERR_FAIL_NULL_V(md, Variant());
  594. int count = GetMenuItemCount(md->menu);
  595. ERR_FAIL_COND_V(p_idx >= count, Variant());
  596. MENUITEMINFOW item;
  597. ZeroMemory(&item, sizeof(item));
  598. item.cbSize = sizeof(item);
  599. item.fMask = MIIM_DATA;
  600. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  601. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  602. if (item_data) {
  603. return item_data->meta;
  604. }
  605. }
  606. return Variant();
  607. }
  608. String NativeMenuWindows::get_item_text(const RID &p_rid, int p_idx) const {
  609. ERR_FAIL_COND_V(p_idx < 0, String());
  610. const MenuData *md = menus.get_or_null(p_rid);
  611. ERR_FAIL_NULL_V(md, String());
  612. int count = GetMenuItemCount(md->menu);
  613. ERR_FAIL_COND_V(p_idx >= count, String());
  614. MENUITEMINFOW item;
  615. ZeroMemory(&item, sizeof(item));
  616. item.cbSize = sizeof(item);
  617. item.fMask = MIIM_STRING;
  618. item.dwTypeData = nullptr;
  619. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  620. item.cch++;
  621. Char16String str;
  622. str.resize(item.cch);
  623. item.dwTypeData = (LPWSTR)str.ptrw();
  624. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  625. return String::utf16((const char16_t *)str.get_data());
  626. }
  627. }
  628. return String();
  629. }
  630. RID NativeMenuWindows::get_item_submenu(const RID &p_rid, int p_idx) const {
  631. ERR_FAIL_COND_V(p_idx < 0, RID());
  632. const MenuData *md = menus.get_or_null(p_rid);
  633. ERR_FAIL_NULL_V(md, RID());
  634. int count = GetMenuItemCount(md->menu);
  635. ERR_FAIL_COND_V(p_idx >= count, RID());
  636. MENUITEMINFOW item;
  637. ZeroMemory(&item, sizeof(item));
  638. item.cbSize = sizeof(item);
  639. item.fMask = MIIM_SUBMENU;
  640. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  641. if (menu_lookup.has(item.hSubMenu)) {
  642. return menu_lookup[item.hSubMenu];
  643. }
  644. }
  645. return RID();
  646. }
  647. Key NativeMenuWindows::get_item_accelerator(const RID &p_rid, int p_idx) const {
  648. // Not supported.
  649. return Key::NONE;
  650. }
  651. bool NativeMenuWindows::is_item_disabled(const RID &p_rid, int p_idx) const {
  652. ERR_FAIL_COND_V(p_idx < 0, false);
  653. const MenuData *md = menus.get_or_null(p_rid);
  654. ERR_FAIL_NULL_V(md, false);
  655. int count = GetMenuItemCount(md->menu);
  656. ERR_FAIL_COND_V(p_idx >= count, false);
  657. MENUITEMINFOW item;
  658. ZeroMemory(&item, sizeof(item));
  659. item.cbSize = sizeof(item);
  660. item.fMask = MIIM_STATE;
  661. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  662. return (item.fState & MFS_DISABLED) == MFS_DISABLED;
  663. }
  664. return false;
  665. }
  666. bool NativeMenuWindows::is_item_hidden(const RID &p_rid, int p_idx) const {
  667. // Not supported.
  668. return false;
  669. }
  670. String NativeMenuWindows::get_item_tooltip(const RID &p_rid, int p_idx) const {
  671. // Not supported.
  672. return String();
  673. }
  674. int NativeMenuWindows::get_item_state(const RID &p_rid, int p_idx) const {
  675. ERR_FAIL_COND_V(p_idx < 0, -1);
  676. const MenuData *md = menus.get_or_null(p_rid);
  677. ERR_FAIL_NULL_V(md, -1);
  678. int count = GetMenuItemCount(md->menu);
  679. ERR_FAIL_COND_V(p_idx >= count, -1);
  680. MENUITEMINFOW item;
  681. ZeroMemory(&item, sizeof(item));
  682. item.cbSize = sizeof(item);
  683. item.fMask = MIIM_DATA;
  684. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  685. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  686. if (item_data) {
  687. return item_data->state;
  688. }
  689. }
  690. return -1;
  691. }
  692. int NativeMenuWindows::get_item_max_states(const RID &p_rid, int p_idx) const {
  693. ERR_FAIL_COND_V(p_idx < 0, -1);
  694. const MenuData *md = menus.get_or_null(p_rid);
  695. ERR_FAIL_NULL_V(md, -1);
  696. int count = GetMenuItemCount(md->menu);
  697. ERR_FAIL_COND_V(p_idx >= count, -1);
  698. MENUITEMINFOW item;
  699. ZeroMemory(&item, sizeof(item));
  700. item.cbSize = sizeof(item);
  701. item.fMask = MIIM_DATA;
  702. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  703. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  704. if (item_data) {
  705. return item_data->max_states;
  706. }
  707. }
  708. return -1;
  709. }
  710. Ref<Texture2D> NativeMenuWindows::get_item_icon(const RID &p_rid, int p_idx) const {
  711. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture2D>());
  712. const MenuData *md = menus.get_or_null(p_rid);
  713. ERR_FAIL_NULL_V(md, Ref<Texture2D>());
  714. int count = GetMenuItemCount(md->menu);
  715. ERR_FAIL_COND_V(p_idx >= count, Ref<Texture2D>());
  716. MENUITEMINFOW item;
  717. ZeroMemory(&item, sizeof(item));
  718. item.cbSize = sizeof(item);
  719. item.fMask = MIIM_DATA;
  720. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  721. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  722. if (item_data) {
  723. return ImageTexture::create_from_image(item_data->img);
  724. }
  725. }
  726. return Ref<Texture2D>();
  727. }
  728. int NativeMenuWindows::get_item_indentation_level(const RID &p_rid, int p_idx) const {
  729. // Not supported.
  730. return 0;
  731. }
  732. void NativeMenuWindows::set_item_checked(const RID &p_rid, int p_idx, bool p_checked) {
  733. ERR_FAIL_COND(p_idx < 0);
  734. const MenuData *md = menus.get_or_null(p_rid);
  735. ERR_FAIL_NULL(md);
  736. int count = GetMenuItemCount(md->menu);
  737. ERR_FAIL_COND(p_idx >= count);
  738. MENUITEMINFOW item;
  739. ZeroMemory(&item, sizeof(item));
  740. item.cbSize = sizeof(item);
  741. item.fMask = MIIM_STATE | MIIM_DATA;
  742. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  743. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  744. if (item_data) {
  745. item_data->checked = p_checked;
  746. if (p_checked) {
  747. item.fState |= MFS_CHECKED;
  748. } else {
  749. item.fState &= ~MFS_CHECKED;
  750. }
  751. }
  752. SetMenuItemInfoW(md->menu, p_idx, true, &item);
  753. }
  754. }
  755. void NativeMenuWindows::set_item_checkable(const RID &p_rid, int p_idx, bool p_checkable) {
  756. ERR_FAIL_COND(p_idx < 0);
  757. const MenuData *md = menus.get_or_null(p_rid);
  758. ERR_FAIL_NULL(md);
  759. int count = GetMenuItemCount(md->menu);
  760. ERR_FAIL_COND(p_idx >= count);
  761. MENUITEMINFOW item;
  762. ZeroMemory(&item, sizeof(item));
  763. item.cbSize = sizeof(item);
  764. item.fMask = MIIM_FTYPE | MIIM_DATA;
  765. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  766. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  767. if (item_data) {
  768. item.fType &= ~MFT_RADIOCHECK;
  769. item_data->checkable_type = (p_checkable) ? CHECKABLE_TYPE_CHECK_BOX : CHECKABLE_TYPE_NONE;
  770. SetMenuItemInfoW(md->menu, p_idx, true, &item);
  771. }
  772. }
  773. }
  774. void NativeMenuWindows::set_item_radio_checkable(const RID &p_rid, int p_idx, bool p_checkable) {
  775. ERR_FAIL_COND(p_idx < 0);
  776. const MenuData *md = menus.get_or_null(p_rid);
  777. ERR_FAIL_NULL(md);
  778. int count = GetMenuItemCount(md->menu);
  779. ERR_FAIL_COND(p_idx >= count);
  780. MENUITEMINFOW item;
  781. ZeroMemory(&item, sizeof(item));
  782. item.cbSize = sizeof(item);
  783. item.fMask = MIIM_FTYPE | MIIM_DATA;
  784. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  785. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  786. if (item_data) {
  787. if (p_checkable) {
  788. item.fType |= MFT_RADIOCHECK;
  789. item_data->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
  790. } else {
  791. item.fType &= ~MFT_RADIOCHECK;
  792. item_data->checkable_type = CHECKABLE_TYPE_NONE;
  793. }
  794. SetMenuItemInfoW(md->menu, p_idx, true, &item);
  795. }
  796. }
  797. }
  798. void NativeMenuWindows::set_item_callback(const RID &p_rid, int p_idx, const Callable &p_callback) {
  799. ERR_FAIL_COND(p_idx < 0);
  800. const MenuData *md = menus.get_or_null(p_rid);
  801. ERR_FAIL_NULL(md);
  802. int count = GetMenuItemCount(md->menu);
  803. ERR_FAIL_COND(p_idx >= count);
  804. MENUITEMINFOW item;
  805. ZeroMemory(&item, sizeof(item));
  806. item.cbSize = sizeof(item);
  807. item.fMask = MIIM_DATA;
  808. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  809. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  810. if (item_data) {
  811. item_data->callback = p_callback;
  812. }
  813. }
  814. }
  815. void NativeMenuWindows::set_item_key_callback(const RID &p_rid, int p_idx, const Callable &p_key_callback) {
  816. // Not supported.
  817. }
  818. void NativeMenuWindows::set_item_hover_callbacks(const RID &p_rid, int p_idx, const Callable &p_callback) {
  819. // Not supported.
  820. }
  821. void NativeMenuWindows::set_item_tag(const RID &p_rid, int p_idx, const Variant &p_tag) {
  822. ERR_FAIL_COND(p_idx < 0);
  823. const MenuData *md = menus.get_or_null(p_rid);
  824. ERR_FAIL_NULL(md);
  825. int count = GetMenuItemCount(md->menu);
  826. ERR_FAIL_COND(p_idx >= count);
  827. MENUITEMINFOW item;
  828. ZeroMemory(&item, sizeof(item));
  829. item.cbSize = sizeof(item);
  830. item.fMask = MIIM_DATA;
  831. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  832. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  833. if (item_data) {
  834. item_data->meta = p_tag;
  835. }
  836. }
  837. }
  838. void NativeMenuWindows::set_item_text(const RID &p_rid, int p_idx, const String &p_text) {
  839. ERR_FAIL_COND(p_idx < 0);
  840. const MenuData *md = menus.get_or_null(p_rid);
  841. ERR_FAIL_NULL(md);
  842. int count = GetMenuItemCount(md->menu);
  843. ERR_FAIL_COND(p_idx >= count);
  844. Char16String label = p_text.utf16();
  845. MENUITEMINFOW item;
  846. ZeroMemory(&item, sizeof(item));
  847. item.cbSize = sizeof(item);
  848. item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA;
  849. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  850. item.dwTypeData = (LPWSTR)label.get_data();
  851. SetMenuItemInfoW(md->menu, p_idx, true, &item);
  852. }
  853. }
  854. void NativeMenuWindows::set_item_submenu(const RID &p_rid, int p_idx, const RID &p_submenu_rid) {
  855. ERR_FAIL_COND(p_idx < 0);
  856. const MenuData *md = menus.get_or_null(p_rid);
  857. ERR_FAIL_NULL(md);
  858. int count = GetMenuItemCount(md->menu);
  859. ERR_FAIL_COND(p_idx >= count);
  860. MenuData *md_sub = menus.get_or_null(p_submenu_rid);
  861. ERR_FAIL_COND_MSG(md->menu == md_sub->menu, "Can't set submenu to self!");
  862. MENUITEMINFOW item;
  863. ZeroMemory(&item, sizeof(item));
  864. item.cbSize = sizeof(item);
  865. item.fMask = MIIM_SUBMENU;
  866. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  867. if (p_submenu_rid.is_valid()) {
  868. item.hSubMenu = md_sub->menu;
  869. } else {
  870. item.hSubMenu = 0;
  871. }
  872. SetMenuItemInfoW(md->menu, p_idx, true, &item);
  873. }
  874. }
  875. void NativeMenuWindows::set_item_accelerator(const RID &p_rid, int p_idx, Key p_keycode) {
  876. // Not supported.
  877. }
  878. void NativeMenuWindows::set_item_disabled(const RID &p_rid, int p_idx, bool p_disabled) {
  879. ERR_FAIL_COND(p_idx < 0);
  880. const MenuData *md = menus.get_or_null(p_rid);
  881. ERR_FAIL_NULL(md);
  882. int count = GetMenuItemCount(md->menu);
  883. ERR_FAIL_COND(p_idx >= count);
  884. MENUITEMINFOW item;
  885. ZeroMemory(&item, sizeof(item));
  886. item.cbSize = sizeof(item);
  887. item.fMask = MIIM_STATE;
  888. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  889. if (p_disabled) {
  890. item.fState |= MFS_DISABLED;
  891. } else {
  892. item.fState &= ~MFS_DISABLED;
  893. }
  894. SetMenuItemInfoW(md->menu, p_idx, true, &item);
  895. }
  896. }
  897. void NativeMenuWindows::set_item_hidden(const RID &p_rid, int p_idx, bool p_hidden) {
  898. // Not supported.
  899. }
  900. void NativeMenuWindows::set_item_tooltip(const RID &p_rid, int p_idx, const String &p_tooltip) {
  901. // Not supported.
  902. }
  903. void NativeMenuWindows::set_item_state(const RID &p_rid, int p_idx, int p_state) {
  904. ERR_FAIL_COND(p_idx < 0);
  905. const MenuData *md = menus.get_or_null(p_rid);
  906. ERR_FAIL_NULL(md);
  907. int count = GetMenuItemCount(md->menu);
  908. ERR_FAIL_COND(p_idx >= count);
  909. MENUITEMINFOW item;
  910. ZeroMemory(&item, sizeof(item));
  911. item.cbSize = sizeof(item);
  912. item.fMask = MIIM_DATA;
  913. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  914. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  915. if (item_data) {
  916. item_data->state = p_state;
  917. }
  918. }
  919. }
  920. void NativeMenuWindows::set_item_max_states(const RID &p_rid, int p_idx, int p_max_states) {
  921. ERR_FAIL_COND(p_idx < 0);
  922. const MenuData *md = menus.get_or_null(p_rid);
  923. ERR_FAIL_NULL(md);
  924. int count = GetMenuItemCount(md->menu);
  925. ERR_FAIL_COND(p_idx >= count);
  926. MENUITEMINFOW item;
  927. ZeroMemory(&item, sizeof(item));
  928. item.cbSize = sizeof(item);
  929. item.fMask = MIIM_DATA;
  930. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  931. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  932. if (item_data) {
  933. item_data->max_states = p_max_states;
  934. }
  935. }
  936. }
  937. void NativeMenuWindows::set_item_icon(const RID &p_rid, int p_idx, const Ref<Texture2D> &p_icon) {
  938. ERR_FAIL_COND(p_idx < 0);
  939. const MenuData *md = menus.get_or_null(p_rid);
  940. ERR_FAIL_NULL(md);
  941. int count = GetMenuItemCount(md->menu);
  942. ERR_FAIL_COND(p_idx >= count);
  943. MENUITEMINFOW item;
  944. ZeroMemory(&item, sizeof(item));
  945. item.cbSize = sizeof(item);
  946. item.fMask = MIIM_DATA | MIIM_BITMAP;
  947. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  948. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  949. if (item_data) {
  950. if (item_data->bmp) {
  951. DeleteObject(item_data->bmp);
  952. }
  953. if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) {
  954. item_data->img = p_icon->get_image();
  955. item_data->img = item_data->img->duplicate();
  956. if (item_data->img->is_compressed()) {
  957. item_data->img->decompress();
  958. }
  959. item_data->bmp = _make_bitmap(item_data->img);
  960. } else {
  961. item_data->img = Ref<Image>();
  962. item_data->bmp = 0;
  963. }
  964. item.hbmpItem = item_data->bmp;
  965. SetMenuItemInfoW(md->menu, p_idx, true, &item);
  966. }
  967. }
  968. }
  969. void NativeMenuWindows::set_item_indentation_level(const RID &p_rid, int p_idx, int p_level) {
  970. // Not supported.
  971. }
  972. int NativeMenuWindows::get_item_count(const RID &p_rid) const {
  973. const MenuData *md = menus.get_or_null(p_rid);
  974. ERR_FAIL_NULL_V(md, 0);
  975. return GetMenuItemCount(md->menu);
  976. }
  977. bool NativeMenuWindows::is_system_menu(const RID &p_rid) const {
  978. return false;
  979. }
  980. void NativeMenuWindows::remove_item(const RID &p_rid, int p_idx) {
  981. ERR_FAIL_COND(p_idx < 0);
  982. const MenuData *md = menus.get_or_null(p_rid);
  983. ERR_FAIL_NULL(md);
  984. int count = GetMenuItemCount(md->menu);
  985. ERR_FAIL_COND(p_idx >= count);
  986. MENUITEMINFOW item;
  987. ZeroMemory(&item, sizeof(item));
  988. item.cbSize = sizeof(item);
  989. item.fMask = MIIM_DATA;
  990. if (GetMenuItemInfoW(md->menu, p_idx, true, &item)) {
  991. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  992. if (item_data) {
  993. if (item_data->bmp) {
  994. DeleteObject(item_data->bmp);
  995. }
  996. memdelete(item_data);
  997. }
  998. }
  999. RemoveMenu(md->menu, p_idx, MF_BYPOSITION);
  1000. }
  1001. void NativeMenuWindows::clear(const RID &p_rid) {
  1002. const MenuData *md = menus.get_or_null(p_rid);
  1003. ERR_FAIL_NULL(md);
  1004. MENUITEMINFOW item;
  1005. int count = GetMenuItemCount(md->menu);
  1006. for (int i = 0; i < count; i++) {
  1007. ZeroMemory(&item, sizeof(item));
  1008. item.cbSize = sizeof(item);
  1009. item.fMask = MIIM_DATA;
  1010. if (GetMenuItemInfoW(md->menu, 0, true, &item)) {
  1011. MenuItemData *item_data = (MenuItemData *)item.dwItemData;
  1012. if (item_data) {
  1013. if (item_data->bmp) {
  1014. DeleteObject(item_data->bmp);
  1015. }
  1016. memdelete(item_data);
  1017. }
  1018. }
  1019. RemoveMenu(md->menu, 0, MF_BYPOSITION);
  1020. }
  1021. }
  1022. NativeMenuWindows::NativeMenuWindows() {}
  1023. NativeMenuWindows::~NativeMenuWindows() {}