数组通过调用 g_action_map_add_action_entries (G_ACTION_MAP (app), app_entries, G_N_ELEMENTS (app_entries), app); 实现映射。根据参数的不同,GTK会自动判断菜单的类型(普通,checkbox/radioGroup等等)
通过 g_action_map_add_action_entries (G_ACTION_MAP (window), win_entries, G_N_ELEMENTS (win_entries), window); 加载给不同的窗口
-
<?xml version="1.0"?>
-
<interface>
-
<menu id="menubar">
-
<section>
-
<item> 应用菜单 永远显示在第一列 < >用于反义 <>
-
<attribute name="label" translatable="yes">_New</attribute> 显示 New
-
<attribute name="action">app.new</attribute> 调用应用的 new 指令
-
<attribute name="accel"><Primary>n</attribute> 快捷键
-
</item>
-
<item>
-
<attribute name="label" translatable="yes">_Open</attribute>
-
<attribute name="action">app.open</attribute>
-
</item>
-
<item>
-
<attribute name="label" translatable="yes">_Save</attribute>
-
<attribute name="action">app.save</attribute>
-
<attribute name="accel"><Primary>s</attribute>
-
</item>
-
<item>
-
<attribute name="label" translatable="yes">Save _As...</attribute>
-
<attribute name="action">app.save-as</attribute>
-
<attribute name="accel"><Primary>s</attribute>
-
</item>
-
</section>
-
<section>
-
<item> 也是应用菜单 也是显示在第一列, 分成多个 section定义会产生一个分隔符而已
-
<attribute name="label" translatable="yes">_Quit</attribute>
-
<attribute name="action">app.quit</attribute>
-
<attribute name="accel"><Primary>q</attribute>
-
</item>
-
</section>
//定义窗口级菜单, action 大部分用 win. 但是也可以用 app. 打头
-
<submenu>
-
<attribute name="label" translatable="yes">_Preferences</attribute> 显示为一个下拉菜单
-
<section>
-
<item>
-
<attribute name="label" translatable="yes">_Prefer Dark Theme</attribute> 显示为一个 checkbox 菜单
-
<attribute name="action">app.dark</attribute>
-
</item>
-
<item>
-
<attribute name="label" translatable="yes">_Hide Titlebar when maximized</attribute>显示为一个 checkbox 菜单
-
<attribute name="action">win.titlebar</attribute>
-
</item>
-
<submenu>
-
<attribute name="label" translatable="yes">_Color</attribute>
-
<section>
-
<item>
-
<attribute name="label" translatable="yes">_Red</attribute> 显示为一个 radio box 菜单
-
<attribute name="action">app.color</attribute>
-
<attribute name="target">red</attribute>
-
<attribute name="accel"><Primary>r</attribute>
-
</item>
-
<item>
-
<attribute name="label" translatable="yes">_Green</attribute>显示为一个 radio box 菜单
-
<attribute name="action">app.color</attribute>
-
<attribute name="target">green</attribute>
-
<attribute name="accel"><Primary>g</attribute>
-
</item>
-
<item>
-
<attribute name="label" translatable="yes">_Blue</attribute>显示为一个 radio box 菜单
-
<attribute name="action">app.color</attribute>
-
<attribute name="target">blue</attribute>
-
<attribute name="accel"><Primary>b</attribute>
-
</item>
-
</section>
-
</submenu>
-
<submenu>
-
<attribute name="label" translatable="yes">_Shape</attribute>
-
<section>
-
<item>
-
<attribute name="label" translatable="yes">_Square</attribute> 显示为一个 radio box 菜单
-
<attribute name="action">win.shape</attribute>
-
<attribute name="target">square</attribute>
-
<attribute name="accel"><Primary>s</attribute>
-