CefMenuModel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Supports creation and modification of menus. See cef_menu_id_t for the
  10. /// command ids that have default implementations. All user-defined command ids
  11. /// should be between MENU_ID_USER_FIRST and MENU_ID_USER_LAST. The methods of
  12. /// this class can only be accessed on the browser process the UI thread.
  13. /// </summary>
  14. public sealed unsafe partial class CefMenuModel
  15. {
  16. /// <summary>
  17. /// Create a new MenuModel with the specified |delegate|.
  18. /// </summary>
  19. public static CefMenuModel Create(CefMenuModelDelegate handler)
  20. {
  21. return CefMenuModel.FromNative(
  22. cef_menu_model_t.create(handler.ToNative())
  23. );
  24. }
  25. /// <summary>
  26. /// Returns true if this menu is a submenu.
  27. /// </summary>
  28. public bool IsSubMenu
  29. {
  30. get
  31. {
  32. return cef_menu_model_t.is_sub_menu(_self) != 0;
  33. }
  34. }
  35. /// <summary>
  36. /// Clears the menu. Returns true on success.
  37. /// </summary>
  38. public bool Clear()
  39. {
  40. return cef_menu_model_t.clear(_self) != 0;
  41. }
  42. /// <summary>
  43. /// Returns the number of items in this menu.
  44. /// </summary>
  45. public int Count
  46. {
  47. get { return cef_menu_model_t.get_count(_self); }
  48. }
  49. /// <summary>
  50. /// Add a separator to the menu. Returns true on success.
  51. /// </summary>
  52. public bool AddSeparator()
  53. {
  54. return cef_menu_model_t.add_separator(_self) != 0;
  55. }
  56. /// <summary>
  57. /// Add an item to the menu. Returns true on success.
  58. /// </summary>
  59. public bool AddItem(int commandId, string label)
  60. {
  61. fixed (char* label_str = label)
  62. {
  63. var n_label = new cef_string_t(label_str, label.Length);
  64. return cef_menu_model_t.add_item(_self, commandId, &n_label) != 0;
  65. }
  66. }
  67. /// <summary>
  68. /// Add a check item to the menu. Returns true on success.
  69. /// </summary>
  70. public bool AddCheckItem(int commandId, string label)
  71. {
  72. fixed (char* label_str = label)
  73. {
  74. var n_label = new cef_string_t(label_str, label.Length);
  75. return cef_menu_model_t.add_check_item(_self, commandId, &n_label) != 0;
  76. }
  77. }
  78. /// <summary>
  79. /// Add a radio item to the menu. Only a single item with the specified
  80. /// |group_id| can be checked at a time. Returns true on success.
  81. /// </summary>
  82. public bool AddRadioItem(int commandId, string label, int groupId)
  83. {
  84. fixed (char* label_str = label)
  85. {
  86. var n_label = new cef_string_t(label_str, label.Length);
  87. return cef_menu_model_t.add_radio_item(_self, commandId, &n_label, groupId) != 0;
  88. }
  89. }
  90. /// <summary>
  91. /// Add a sub-menu to the menu. The new sub-menu is returned.
  92. /// </summary>
  93. public CefMenuModel AddSubMenu(int commandId, string label)
  94. {
  95. fixed (char* label_str = label)
  96. {
  97. var n_label = new cef_string_t(label_str, label.Length);
  98. return CefMenuModel.FromNative(
  99. cef_menu_model_t.add_sub_menu(_self, commandId, &n_label)
  100. );
  101. }
  102. }
  103. /// <summary>
  104. /// Insert a separator in the menu at the specified |index|. Returns true on
  105. /// success.
  106. /// </summary>
  107. public bool InsertSeparatorAt(int index)
  108. {
  109. return cef_menu_model_t.insert_separator_at(_self, index) != 0;
  110. }
  111. /// <summary>
  112. /// Insert an item in the menu at the specified |index|. Returns true on
  113. /// success.
  114. /// </summary>
  115. public bool InsertItemAt(int index, int commandId, string label)
  116. {
  117. fixed (char* label_str = label)
  118. {
  119. var n_label = new cef_string_t(label_str, label.Length);
  120. return cef_menu_model_t.insert_item_at(_self, index, commandId, &n_label) != 0;
  121. }
  122. }
  123. /// <summary>
  124. /// Insert a check item in the menu at the specified |index|. Returns true on
  125. /// success.
  126. /// </summary>
  127. public bool InsertCheckItemAt(int index, int commandId, string label)
  128. {
  129. fixed (char* label_str = label)
  130. {
  131. var n_label = new cef_string_t(label_str, label.Length);
  132. return cef_menu_model_t.insert_check_item_at(_self, index, commandId, &n_label) != 0;
  133. }
  134. }
  135. /// <summary>
  136. /// Insert a radio item in the menu at the specified |index|. Only a single
  137. /// item with the specified |group_id| can be checked at a time. Returns true
  138. /// on success.
  139. /// </summary>
  140. public bool InsertRadioItemAt(int index, int commandId, string label, int groupId)
  141. {
  142. fixed (char* label_str = label)
  143. {
  144. var n_label = new cef_string_t(label_str, label.Length);
  145. return cef_menu_model_t.insert_radio_item_at(_self, index, commandId, &n_label, groupId) != 0;
  146. }
  147. }
  148. /// <summary>
  149. /// Insert a sub-menu in the menu at the specified |index|. The new sub-menu
  150. /// is returned.
  151. /// </summary>
  152. public CefMenuModel InsertSubMenuAt(int index, int commandId, string label)
  153. {
  154. fixed (char* label_str = label)
  155. {
  156. var n_label = new cef_string_t(label_str, label.Length);
  157. return CefMenuModel.FromNative(
  158. cef_menu_model_t.add_sub_menu(_self, commandId, &n_label)
  159. );
  160. }
  161. }
  162. /// <summary>
  163. /// Removes the item with the specified |commandId|. Returns true on success.
  164. /// </summary>
  165. public bool Remove(int commandId)
  166. {
  167. return cef_menu_model_t.remove(_self, commandId) != 0;
  168. }
  169. /// <summary>
  170. /// Removes the item at the specified |index|. Returns true on success.
  171. /// </summary>
  172. public bool RemoveAt(int index)
  173. {
  174. return cef_menu_model_t.remove_at(_self, index) != 0;
  175. }
  176. /// <summary>
  177. /// Returns the index associated with the specified |commandId| or -1 if not
  178. /// found due to the command id not existing in the menu.
  179. /// </summary>
  180. public int GetIndexOf(int commandId)
  181. {
  182. return cef_menu_model_t.get_index_of(_self, commandId);
  183. }
  184. /// <summary>
  185. /// Returns the command id at the specified |index| or -1 if not found due to
  186. /// invalid range or the index being a separator.
  187. /// </summary>
  188. public int GetCommandIdAt(int index)
  189. {
  190. return cef_menu_model_t.get_command_id_at(_self, index);
  191. }
  192. /// <summary>
  193. /// Sets the command id at the specified |index|. Returns true on success.
  194. /// </summary>
  195. public bool SetCommandIdAt(int index, int commandId)
  196. {
  197. return cef_menu_model_t.set_command_id_at(_self, index, commandId) != 0;
  198. }
  199. /// <summary>
  200. /// Returns the label for the specified |commandId| or empty if not found.
  201. /// </summary>
  202. public string GetLabel(int commandId)
  203. {
  204. var n_result = cef_menu_model_t.get_label(_self, commandId);
  205. return cef_string_userfree.ToString(n_result);
  206. }
  207. /// <summary>
  208. /// Returns the label at the specified |index| or empty if not found due to
  209. /// invalid range or the index being a separator.
  210. /// </summary>
  211. public string GetLabelAt(int index)
  212. {
  213. var n_result = cef_menu_model_t.get_label_at(_self, index);
  214. return cef_string_userfree.ToString(n_result);
  215. }
  216. /// <summary>
  217. /// Sets the label for the specified |commandId|. Returns true on success.
  218. /// </summary>
  219. public bool SetLabel(int commandId, string label)
  220. {
  221. fixed (char* label_str = label)
  222. {
  223. var n_label = new cef_string_t(label_str, label.Length);
  224. return cef_menu_model_t.set_label(_self, commandId, &n_label) != 0;
  225. }
  226. }
  227. /// <summary>
  228. /// Set the label at the specified |index|. Returns true on success.
  229. /// </summary>
  230. public bool SetLabelAt(int index, string label)
  231. {
  232. fixed (char* label_str = label)
  233. {
  234. var n_label = new cef_string_t(label_str, label.Length);
  235. return cef_menu_model_t.set_label_at(_self, index, &n_label) != 0;
  236. }
  237. }
  238. /// <summary>
  239. /// Returns the item type for the specified |commandId|.
  240. /// </summary>
  241. public CefMenuItemType GetItemType(int commandId)
  242. {
  243. return cef_menu_model_t.get_type(_self, commandId);
  244. }
  245. /// <summary>
  246. /// Returns the item type at the specified |index|.
  247. /// </summary>
  248. public CefMenuItemType GetItemTypeAt(int index)
  249. {
  250. return cef_menu_model_t.get_type_at(_self, index);
  251. }
  252. /// <summary>
  253. /// Returns the group id for the specified |commandId| or -1 if invalid.
  254. /// </summary>
  255. public int GetGroupId(int commandId)
  256. {
  257. return cef_menu_model_t.get_group_id(_self, commandId);
  258. }
  259. /// <summary>
  260. /// Returns the group id at the specified |index| or -1 if invalid.
  261. /// </summary>
  262. public int GetGroupIdAt(int index)
  263. {
  264. return cef_menu_model_t.get_group_id_at(_self, index);
  265. }
  266. /// <summary>
  267. /// Sets the group id for the specified |commandId|. Returns true on success.
  268. /// </summary>
  269. public bool SetGroupId(int commandId, int groupId)
  270. {
  271. return cef_menu_model_t.set_group_id(_self, commandId, groupId) != 0;
  272. }
  273. /// <summary>
  274. /// Sets the group id at the specified |index|. Returns true on success.
  275. /// </summary>
  276. public bool SetGroupIdAt(int index, int groupId)
  277. {
  278. return cef_menu_model_t.set_group_id_at(_self, index, groupId) != 0;
  279. }
  280. /// <summary>
  281. /// Returns the submenu for the specified |commandId| or empty if invalid.
  282. /// </summary>
  283. public CefMenuModel GetSubMenu(int commandId)
  284. {
  285. return CefMenuModel.FromNativeOrNull(
  286. cef_menu_model_t.get_sub_menu(_self, commandId)
  287. );
  288. }
  289. /// <summary>
  290. /// Returns the submenu at the specified |index| or empty if invalid.
  291. /// </summary>
  292. public CefMenuModel GetSubMenuAt(int index)
  293. {
  294. return CefMenuModel.FromNativeOrNull(
  295. cef_menu_model_t.get_sub_menu_at(_self, index)
  296. );
  297. }
  298. /// <summary>
  299. /// Returns true if the specified |commandId| is visible.
  300. /// </summary>
  301. public bool IsVisible(int commandId)
  302. {
  303. return cef_menu_model_t.is_visible(_self, commandId) != 0;
  304. }
  305. /// <summary>
  306. /// Returns true if the specified |index| is visible.
  307. /// </summary>
  308. public bool IsVisibleAt(int index)
  309. {
  310. return cef_menu_model_t.is_visible(_self, index) != 0;
  311. }
  312. /// <summary>
  313. /// Change the visibility of the specified |commandId|. Returns true on
  314. /// success.
  315. /// </summary>
  316. public bool SetVisible(int commandId, bool visible)
  317. {
  318. return cef_menu_model_t.set_visible(_self, commandId, visible ? 1 : 0) != 0;
  319. }
  320. /// <summary>
  321. /// Change the visibility at the specified |index|. Returns true on success.
  322. /// </summary>
  323. public bool SetVisibleAt(int index, bool visible)
  324. {
  325. return cef_menu_model_t.set_visible(_self, index, visible ? 1 : 0) != 0;
  326. }
  327. /// <summary>
  328. /// Returns true if the specified |commandId| is enabled.
  329. /// </summary>
  330. public bool IsEnabled(int commandId)
  331. {
  332. return cef_menu_model_t.is_enabled(_self, commandId) != 0;
  333. }
  334. /// <summary>
  335. /// Returns true if the specified |index| is enabled.
  336. /// </summary>
  337. public bool IsEnabledAt(int index)
  338. {
  339. return cef_menu_model_t.is_enabled_at(_self, index) != 0;
  340. }
  341. /// <summary>
  342. /// Change the enabled status of the specified |commandId|. Returns true on
  343. /// success.
  344. /// </summary>
  345. public bool SetEnabled(int commandId, bool enabled)
  346. {
  347. return cef_menu_model_t.set_enabled(_self, commandId, enabled ? 1 : 0) != 0;
  348. }
  349. /// <summary>
  350. /// Change the enabled status at the specified |index|. Returns true on
  351. /// success.
  352. /// </summary>
  353. public bool SetEnabledAt(int index, bool enabled)
  354. {
  355. return cef_menu_model_t.set_enabled_at(_self, index, enabled ? 1 : 0) != 0;
  356. }
  357. /// <summary>
  358. /// Returns true if the specified |commandId| is checked. Only applies to
  359. /// check and radio items.
  360. /// </summary>
  361. public bool IsChecked(int commandId)
  362. {
  363. return cef_menu_model_t.is_checked(_self, commandId) != 0;
  364. }
  365. /// <summary>
  366. /// Returns true if the specified |index| is checked. Only applies to check
  367. /// and radio items.
  368. /// </summary>
  369. public bool IsCheckedAt(int index)
  370. {
  371. return cef_menu_model_t.is_checked_at(_self, index) != 0;
  372. }
  373. /// <summary>
  374. /// Check the specified |commandId|. Only applies to check and radio items.
  375. /// Returns true on success.
  376. /// </summary>
  377. public bool SetChecked(int commandId, bool value)
  378. {
  379. return cef_menu_model_t.set_checked(_self, commandId, value ? 1 : 0) != 0;
  380. }
  381. /// <summary>
  382. /// Check the specified |index|. Only applies to check and radio items. Returns
  383. /// true on success.
  384. /// </summary>
  385. public bool SetCheckedAt(int index, bool value)
  386. {
  387. return cef_menu_model_t.set_checked_at(_self, index, value ? 1 : 0) != 0;
  388. }
  389. /// <summary>
  390. /// Returns true if the specified |commandId| has a keyboard accelerator
  391. /// assigned.
  392. /// </summary>
  393. public bool HasAccelerator(int commandId)
  394. {
  395. return cef_menu_model_t.has_accelerator(_self, commandId) != 0;
  396. }
  397. /// <summary>
  398. /// Returns true if the specified |index| has a keyboard accelerator assigned.
  399. /// </summary>
  400. public bool HasAcceleratorAt(int index)
  401. {
  402. return cef_menu_model_t.has_accelerator(_self, index) != 0;
  403. }
  404. /// <summary>
  405. /// Set the keyboard accelerator for the specified |commandId|. |key_code| can
  406. /// be any virtual key or character value. Returns true on success.
  407. /// </summary>
  408. public bool SetAccelerator(int commandId, int keyCode, bool shiftPressed, bool ctrlPressed, bool altPressed)
  409. {
  410. return cef_menu_model_t.set_accelerator(
  411. _self,
  412. commandId,
  413. keyCode,
  414. shiftPressed ? 1 : 0,
  415. ctrlPressed ? 1 : 0,
  416. altPressed ? 1 : 0
  417. ) != 0;
  418. }
  419. /// <summary>
  420. /// Set the keyboard accelerator at the specified |index|. |key_code| can be
  421. /// any virtual key or character value. Returns true on success.
  422. /// </summary>
  423. public bool SetAcceleratorAt(int index, int keyCode, bool shiftPressed, bool ctrlPressed, bool altPressed)
  424. {
  425. return cef_menu_model_t.set_accelerator_at(
  426. _self,
  427. index,
  428. keyCode,
  429. shiftPressed ? 1 : 0,
  430. ctrlPressed ? 1 : 0,
  431. altPressed ? 1 : 0
  432. ) != 0;
  433. }
  434. /// <summary>
  435. /// Remove the keyboard accelerator for the specified |commandId|. Returns
  436. /// true on success.
  437. /// </summary>
  438. public bool RemoveAccelerator(int commandId)
  439. {
  440. return cef_menu_model_t.remove_accelerator(_self, commandId) != 0;
  441. }
  442. /// <summary>
  443. /// Remove the keyboard accelerator at the specified |index|. Returns true on
  444. /// success.
  445. /// </summary>
  446. public bool RemoveAcceleratorAt(int index)
  447. {
  448. return cef_menu_model_t.remove_accelerator_at(_self, index) != 0;
  449. }
  450. /// <summary>
  451. /// Retrieves the keyboard accelerator for the specified |commandId|. Returns
  452. /// true on success.
  453. /// </summary>
  454. public bool GetAccelerator(int commandId, out int keyCode, out bool shiftPressed, out bool ctrlPressed, out bool altPressed)
  455. {
  456. int n_keyCode;
  457. int n_shiftPressed;
  458. int n_ctrlPressed;
  459. int n_altPressed;
  460. var result = cef_menu_model_t.get_accelerator(_self, commandId, &n_keyCode, &n_shiftPressed, &n_ctrlPressed, &n_altPressed) != 0;
  461. keyCode = n_keyCode;
  462. shiftPressed = n_shiftPressed != 0;
  463. ctrlPressed = n_ctrlPressed != 0;
  464. altPressed = n_altPressed != 0;
  465. return result;
  466. }
  467. /// <summary>
  468. /// Retrieves the keyboard accelerator for the specified |index|. Returns true
  469. /// on success.
  470. /// </summary>
  471. public bool GetAcceleratorAt(int index, out int keyCode, out bool shiftPressed, out bool ctrlPressed, out bool altPressed)
  472. {
  473. int n_keyCode;
  474. int n_shiftPressed;
  475. int n_ctrlPressed;
  476. int n_altPressed;
  477. var result = cef_menu_model_t.get_accelerator_at(_self, index, &n_keyCode, &n_shiftPressed, &n_ctrlPressed, &n_altPressed) != 0;
  478. keyCode = n_keyCode;
  479. shiftPressed = n_shiftPressed != 0;
  480. ctrlPressed = n_ctrlPressed != 0;
  481. altPressed = n_altPressed != 0;
  482. return result;
  483. }
  484. /// <summary>
  485. /// Set the explicit color for |command_id| and |color_type| to |color|.
  486. /// Specify a |color| value of 0 to remove the explicit color. If no explicit
  487. /// color or default color is set for |color_type| then the system color will
  488. /// be used. Returns true on success.
  489. /// </summary>
  490. public bool SetColor(int commandId, CefMenuColorType colorType, uint color)
  491. {
  492. return cef_menu_model_t.set_color(_self, commandId, colorType, color) != 0;
  493. }
  494. /// <summary>
  495. /// Set the explicit color for |command_id| and |index| to |color|. Specify a
  496. /// |color| value of 0 to remove the explicit color. Specify an |index| value
  497. /// of -1 to set the default color for items that do not have an explicit
  498. /// color set. If no explicit color or default color is set for |color_type|
  499. /// then the system color will be used. Returns true on success.
  500. /// </summary>
  501. public bool SetColorAt(int index, CefMenuColorType colorType, uint color)
  502. {
  503. return cef_menu_model_t.set_color_at(_self, index, colorType, color) != 0;
  504. }
  505. /// <summary>
  506. /// Returns in |color| the color that was explicitly set for |command_id| and
  507. /// |color_type|. If a color was not set then 0 will be returned in |color|.
  508. /// Returns true on success.
  509. /// </summary>
  510. public bool GetColor(int commandId, CefMenuColorType colorType, out uint color)
  511. {
  512. uint n_color;
  513. var result = cef_menu_model_t.get_color(_self, commandId, colorType, &n_color) != 0;
  514. color = n_color;
  515. return result;
  516. }
  517. /// <summary>
  518. /// Returns in |color| the color that was explicitly set for |command_id| and
  519. /// |color_type|. Specify an |index| value of -1 to return the default color
  520. /// in |color|. If a color was not set then 0 will be returned in |color|.
  521. /// Returns true on success.
  522. /// </summary>
  523. public bool GetColorAt(int index, CefMenuColorType colorType, out uint color)
  524. {
  525. uint n_color;
  526. var result = cef_menu_model_t.get_color_at(_self, index, colorType, &n_color) != 0;
  527. color = n_color;
  528. return result;
  529. }
  530. /// <summary>
  531. /// Sets the font list for the specified |command_id|. If |font_list| is empty
  532. /// the system font will be used. Returns true on success. The format is
  533. /// "&lt;FONT_FAMILY_LIST&gt;,[STYLES] &lt;SIZE&gt;", where:
  534. /// - FONT_FAMILY_LIST is a comma-separated list of font family names,
  535. /// - STYLES is an optional space-separated list of style names (case-sensitive
  536. /// "Bold" and "Italic" are supported), and
  537. /// - SIZE is an integer font size in pixels with the suffix "px".
  538. /// Here are examples of valid font description strings:
  539. /// - "Arial, Helvetica, Bold Italic 14px"
  540. /// - "Arial, 14px"
  541. /// </summary>
  542. public bool SetFontList(int commandId, string fontList)
  543. {
  544. fixed (char* fontList_str = fontList)
  545. {
  546. var n_fontList = new cef_string_t(fontList_str, fontList != null ? fontList.Length : 0);
  547. return cef_menu_model_t.set_font_list(_self, commandId, &n_fontList) != 0;
  548. }
  549. }
  550. /// <summary>
  551. /// Sets the font list for the specified |index|. Specify an |index| value of
  552. /// -1 to set the default font. If |font_list| is empty the system font will
  553. /// be used. Returns true on success. The format is
  554. /// "&lt;FONT_FAMILY_LIST&gt;,[STYLES] &lt;SIZE&gt;", where:
  555. /// - FONT_FAMILY_LIST is a comma-separated list of font family names,
  556. /// - STYLES is an optional space-separated list of style names (case-sensitive
  557. /// "Bold" and "Italic" are supported), and
  558. /// - SIZE is an integer font size in pixels with the suffix "px".
  559. /// Here are examples of valid font description strings:
  560. /// - "Arial, Helvetica, Bold Italic 14px"
  561. /// - "Arial, 14px"
  562. /// </summary>
  563. public bool SetFontListAt(int index, string fontList)
  564. {
  565. fixed (char* fontList_str = fontList)
  566. {
  567. var n_fontList = new cef_string_t(fontList_str, fontList != null ? fontList.Length : 0);
  568. return cef_menu_model_t.set_font_list_at(_self, index, &n_fontList) != 0;
  569. }
  570. }
  571. }
  572. }