日期:2014-05-16  浏览次数:20796 次

GTK,有关创建菜单的问题,请高手解答。(来者有分)
最近在学习GTK,但是在创建菜单的时候出了些问题。问题出在使用GtkItemFactoryEntry结构的地方。
我创建了一个如下的GtkItemFactoryEntry结构:
static GtkItemFactoryEntry menu_items[] ={
  {"/文件(_F)", NULL, NULL, 0, "<Branch>"},
  {"/文件(_F)/新建", NULL, on_menu_activate, "新建", "<StockItem>", GTK_STOCK_NEW},
  {"/文件(_F)/打开", NULL, on_menu_activate, "打开", "<StockItem>", GTK_STOCK_OPEN},
  {"/文件(_F)/保存", NULL, on_menu_activate, "保存", "<StockItem>", GTK_STOCK_SAVE},
  {"/文件(_F)/另存为", NULL, on_menu_activate, "另存为", "<StockItem>", GTK_STOCK_SAVE_AS},
  {"/文件(_F)/退出", NULL, on_menu_activate, "退出", "<StockItem>", GTK_STOCK_QUIT},
  {"/编辑(_E)", NULL, NULL, 0, "<Branch>"},
  {"/编辑(_E)/剪切", NULL, on_menu_activate, "剪切", "<StockItem>", GTK_STOCK_CUT},
  {"/编辑(_E)/复制", NULL, on_menu_activate, "复制", "<StockItem>", GTK_STOCK_COPY},
  {"/编辑(_E)/粘贴", NULL, on_menu_activate, "粘贴", "<StockItem>", GTK_STOCK_PASTE},
  {"/编辑(_E)/查找", NULL, on_menu_activate, "查找", "<StockItem>", GTK_STOCK_FIND},
  {"/帮助(_H)", NULL, NULL, 0, "<LastBranch>"},
  {"/帮助(_H)/关于...", NULL, on_menu_activate, "关于", "<StockItem>", NULL}
};
程序是抄的教材上的,并没有错误,别人的也能通过。但是放在我的电脑上面就出错了。
编译的时候出现了以下错误:
error: initializer element is not constant
error: (near initialization for ‘menu_items[1].callback_action’)
在网上查了一些资料,有的GtkItemFactoryEntry结构是含有5项,有的是含有6项。
有的GtkItemFactoryEntry的结构如下:
struct _GtkItemFactoryEntry
{
  gchar *path; (1)
  gchar *accelerator; (2)

  GtkItemFactoryCallback callback; (3)
  guint callback_action; (4)

  /* possible values:
  * NULL -> "<Item>"
  * "" -> "<Item>"
  * "<Title>" -> create a title item
  * "<Item>" -> create a simple item
  * "<ImageItem>" -> create an item holding an image
  * "<StockItem>" -> create an item holding a stock image
  * "<CheckItem>" -> create a check item
  * "<ToggleItem>" -> create a toggle item
  * "<RadioItem>" -> create a radio item
  * <path> -> path of a radio item to link against
  * "<Separator>" -> create a separator
  * "<Tearoff>" -> create a tearoff separator
  * "<Branch>" -> create an item to hold sub items
  * "<LastBranch>" -> create a right justified item to hold sub items
  */
  gchar *item_type; (5)

  /* Extra data for some item types:
  * ImageItem -> pointer to inlined pixbuf stream
  * StockItem -> name of stock item
  */
  gconstpointer extra_data; (6)
};
还有一种是5项的:
struct GtkItemFactoryEntry

{

  gchar *path;

  gchar *accelerator;

  GtkItemFactoryCallback callback;

  guint callback_action;

  gchar *item_type;