Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9087097
  • 博文数量: 1732
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19830
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1732)

文章存档

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: Android平台

2017-11-03 18:06:45


点击(此处)折叠或打开

  1. builder = gtk_builder_new ();
  2.   gtk_builder_add_from_resource (builder, "/org/gtk/exampleapp/app-menu.ui", NULL);
  3.   appmenu = (GMenuModel *)gtk_builder_get_object (builder, "appmenu");
  4.   //appmenu = G_MENU_MODEL (gtk_builder_get_object (builder, "appmenu"));
  5.   menubar = (GMenuModel *)gtk_builder_get_object (builder, "menubar");
  6.   //以下两个Api并无明确区分, 可以合并成一个菜单。
  7.   gtk_application_set_app_menu (GTK_APPLICATION (app), appmenu); //设置应用菜单。 用于多个应用的操作。 如打开一个新应用。
  8.   gtk_application_set_menubar (GTK_APPLICATION (app), menubar); //设置窗口菜单。应用内的菜单。 
  9.   
  10.   g_object_unref (builder)
系统/应用菜单采用的格式不能使用glade完成, 他拥有自己的格式(GMenuModel), 只能自己通过 XML 编写。
GMenuModel 规则 https://developer.gnome.org/gio/stable/GMenuModel.html 

点击(此处)折叠或打开

  1. The 'standard' attributes and link types have predefined names: G_MENU_ATTRIBUTE_LABEL, G_MENU_ATTRIBUTE_ACTION, G_MENU_ATTRIBUTE_TARGET, G_MENU_LINK_SECTION and G_MENU_LINK_SUBMENU.
  2. 标准属性包含该 label action target section submenu
  3. action 分为两个动作组 action group, 对于应用级的使用 app.xxx 对于窗口的使用 win.xxx
  4. action 分为 
  5.     1.无参数无返回  
  6.     2.无参数 boolean返回, 定义菜单时需要使用 toggle 或者 switch, 如复选框等菜单
  7.     3.string参数, string返回。如单选 radio menu items 框等菜单。
各个action --> app.xxx。 所有的xxx 应当被映射为 GActionEntry 数组。通过

点击(此处)折叠或打开

  1. struct GActionEntry {
      const gchar *name;  ---》                              //映射的 xxx 名字
      void (* activate) (GSimpleAction *action,     --》     //处理函数
                         GVariant      *parameter,
                         gpointer       user_data);
    
      const gchar *parameter_type;                           //传入 activate 的参数类型。
      const gchar *state;                                    //初始状态值。
      void (* change_state) (GSimpleAction *action,          //activate 执行完毕后的回调
                             GVariant      *value,
                             gpointer       user_data);
    };
  2. static GActionEntry app_entries[] = {  
  3.   { "new", activate_new, NULL, NULL, NULL },
  4.   { "open", activate_open, NULL, NULL, NULL },
  5.   { "save", activate_action, NULL, NULL, NULL },
  6.   { "save-as", activate_action, NULL, NULL, NULL },
  7.   { "quit", activate_quit, NULL, NULL, NULL },
  8.   { "dark", activate_toggle, NULL, "false", change_theme_state }
  9. }
数组通过调用 g_action_map_add_action_entries (G_ACTION_MAP (app), app_entries, G_N_ELEMENTS (app_entries), app); 实现映射。根据参数的不同,GTK会自动判断菜单的类型(普通,checkbox/radioGroup等等)

而对应的 win.xxx 

点击(此处)折叠或打开

  1. static GActionEntry win_entries[] = {
  2.   { "titlebar", activate_toggle, NULL, "false", change_titlebar_state },
  3.   { "shape", activate_radio, "s", "'oval'", change_radio_state },
  4.   { "bold", activate_toggle, NULL, "false", NULL },
  5.   { "about", activate_about, NULL, NULL, NULL },
  6.   { "file1", activate_action, NULL, NULL, NULL },
  7.   { "logo", activate_action, NULL, NULL, NULL }
  8. };
 通过 g_action_map_add_action_entries (G_ACTION_MAP (window),  win_entries, G_N_ELEMENTS (win_entries),  window); 加载给不同的窗口






  1. <?xml version="1.0"?>
  2. <interface>
  3.   <menu id="menubar">
  4.     <section
  5.      <item>   应用菜单 永远显示在第一列  &lt&gt;用于反义 <>
  6.           <attribute name="label" translatable="yes">_New</attribute>      显示 New
  7.           <attribute name="action">app.new</attribute>                     调用应用的 new 指令
  8.           <attribute name="accel">&lt;Primary&gt;n</attribute>             快捷键
  9.      </item>
  10.      <item>
  11.           <attribute name="label" translatable="yes">_Open</attribute>
  12.           <attribute name="action">app.open</attribute>
  13.      </item>
  14.      <item>
  15.           <attribute name="label" translatable="yes">_Save</attribute>
  16.           <attribute name="action">app.save</attribute>
  17.           <attribute name="accel">&lt;Primary&gt;s</attribute>
  18.      </item>
  19.      <item>
  20.          <attribute name="label" translatable="yes">Save _As...</attribute>
  21.          <attribute name="action">app.save-as</attribute>
  22.          <attribute name="accel">&lt;Primary&gt;s</attribute>
  23.      </item>
  24.    </section>
  25.    <section>
  26.      <item也是应用菜单 也是显示在第一列分成多个 section定义会产生一个分隔符而已
  27.          <attribute name="label" translatable="yes">_Quit</attribute>
  28.          <attribute name="action">app.quit</attribute>
  29.          <attribute name="accel">&lt;Primary&gt;q</attribute>
  30.      </item>
  31.    </section>
   


     //定义窗口级菜单, action 大部分用 win. 但是也可以用 app. 打头
  1.     <submenu>
  2.       <attribute name="label" translatable="yes">_Preferences</attribute显示为一个下拉菜单
  3.       <section>
  4.         <item>
  5.           <attribute name="label" translatable="yes">_Prefer Dark Theme</attribute显示为一个 checkbox 菜单
  6.           <attribute name="action">app.dark</attribute>
  7.         </item>
  8.         <item>
  9.           <attribute name="label" translatable="yes">_Hide Titlebar when maximized</attribute>显示为一个 checkbox 菜单
  10.           <attribute name="action">win.titlebar</attribute>
  11.         </item>
  12.         <submenu>
  13.           <attribute name="label" translatable="yes">_Color</attribute>
  14.           <section>
  15.             <item>
  16.               <attribute name="label" translatable="yes">_Red</attribute显示为一个 radio box 菜单
  17.               <attribute name="action">app.color</attribute>
  18.               <attribute name="target">red</attribute>
  19.               <attribute name="accel">&lt;Primary&gt;r</attribute>
  20.             </item>
  21.             <item>
  22.               <attribute name="label" translatable="yes">_Green</attribute>显示为一个 radio box 菜单
  23.               <attribute name="action">app.color</attribute>
  24.               <attribute name="target">green</attribute>
  25.               <attribute name="accel">&lt;Primary&gt;g</attribute>
  26.             </item>
  27.             <item>
  28.               <attribute name="label" translatable="yes">_Blue</attribute>显示为一个 radio box 菜单
  29.               <attribute name="action">app.color</attribute>
  30.               <attribute name="target">blue</attribute>
  31.               <attribute name="accel">&lt;Primary&gt;b</attribute>
  32.             </item>
  33.           </section>
  34.         </submenu>
  35.         <submenu>
  36.           <attribute name="label" translatable="yes">_Shape</attribute>
  37.           <section>
  38.             <item>
  39.               <attribute name="label" translatable="yes">_Square</attribute显示为一个 radio box 菜单
  40.               <attribute name="action">win.shape</attribute>
  41.               <attribute name="target">square</attribute>
  42.               <attribute name="accel">&lt;Primary&gt;s</attribute>
阅读(1608) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~