在 Motif 中创建选项菜单

Creating an option menu in Motif

我正在尝试使用 Motif 工具包创建一个选项菜单。

如果我尝试这样做,它会起作用:

#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>

Widget make_menu_item(char *item_name, Widget menu);

/* XT/XM RELATED VARIABLES */
XtAppContext context;
XmStringCharSet char_set = XmSTRING_DEFAULT_CHARSET;

/* WIDGETS */
Widget toplevel; 
Widget form;
Widget pulldown_menu;
Widget option_menu;
Widget option_item[100];
int option_items = 0;

int main(int argc, char *argv[]) {

    Arg al[10];
    int ac;

    /* CREATE TOP SHELL WIDGET */
    toplevel = XtAppInitialize(&context, "", NULL, 0, &argc, argv, NULL, NULL, 0);

    /* RESIZE TOP LEVEL*/
    ac = 0;
    XtSetArg(al[ac], XmNheight, 900); ac++;
    XtSetArg(al[ac], XmNwidth, 600); ac++;
    XtSetValues(toplevel, al, ac);

    /* PULLDOWN MENU */
    ac = 0;
    pulldown_menu = XmCreatePulldownMenu(toplevel, "pulldown_menu", al, ac);

    ac = 0;
    XtSetArg(al[ac], XmNsubMenuId, pulldown_menu); ac++;
    XtSetArg(al[ac], XmNlabelString, XmStringCreate("Pulldown Menu", char_set)); ac++;
    option_menu = XmCreateOptionMenu(toplevel, "option_menu", al, ac);
    XtManageChild(option_menu);
    make_menu_item("FOO", pulldown_menu);
    make_menu_item("BAR", pulldown_menu);

    /* REALIZE TOPLEVEL */
    XtRealizeWidget(toplevel);
    XtAppMainLoop(context);

    return 0;

}

Widget make_menu_item(char *item_name,  Widget menu) {

    int ac;
    Arg al[10];
    Widget item;

    ac = 0;
    XtSetArg(al[ac], XmNlabelString, XmStringCreateLtoR(item_name, char_set)); ac++;
    item = XmCreatePushButton(menu, item_name, al, ac);
    XtManageChild(item);
    /*XtAddCallback(item, XmNactivateCallback, menuCB, client_data);*/
    XtSetSensitive(item, True);
    return(item);

}

但是,当我尝试使用 form 作为 PulldownMenu 和 OptionMenu 的父项时,它不起作用。 如何将选项菜单附加到表单?为什么在这种情况下不显示选项菜单?

#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>

Widget make_menu_item(char *item_name, Widget menu);

/* XT/XM RELATED VARIABLES */
XtAppContext context;
XmStringCharSet char_set = XmSTRING_DEFAULT_CHARSET;

/* WIDGETS */
Widget toplevel; 
Widget form;
Widget pulldown_menu;
Widget option_menu;
Widget option_item[100];
int option_items = 0;

int main(int argc, char *argv[]) {

    Arg al[10];
    int ac;

    /* CREATE TOP SHELL WIDGET */
    toplevel = XtAppInitialize(&context, "", NULL, 0, &argc, argv, NULL, NULL, 0);

    /* RESIZE TOP LEVEL*/
    ac = 0;
    XtSetArg(al[ac], XmNheight, 900); ac++;
    XtSetArg(al[ac], XmNwidth, 600); ac++;
    XtSetValues(toplevel, al, ac);

    /* CREATE FORM MANAGER WIDGET */
    form = XmCreateForm(toplevel, "form", al, ac);

    /* PULLDOWN MENU */
    ac = 0;
    pulldown_menu = XmCreatePulldownMenu(form, "pulldown_menu", al, ac);

    ac = 0;
    XtSetArg(al[ac], XmNsubMenuId, pulldown_menu); ac++;
    XtSetArg(al[ac], XmNlabelString, XmStringCreate("Pulldown Menu", char_set)); ac++;
    option_menu = XmCreateOptionMenu(form, "option_menu", al, ac);
    XtManageChild(option_menu);
    make_menu_item("FOO", pulldown_menu);
    make_menu_item("BAR", pulldown_menu);

    /* REALIZE TOPLEVEL */
    XtRealizeWidget(toplevel);
    XtAppMainLoop(context);

    return 0;

}

Widget make_menu_item(char *item_name,  Widget menu) {

    int ac;
    Arg al[10];
    Widget item;

    ac = 0;
    XtSetArg(al[ac], XmNlabelString, XmStringCreateLtoR(item_name, char_set)); ac++;
    item = XmCreatePushButton(menu, item_name, al, ac);
    XtManageChild(item);
    /*XtAddCallback(item, XmNactivateCallback, menuCB, client_data);*/
    XtSetSensitive(item, True);
    return(item);

}

在行后添加:

XtManageChild(option_menu);

管理表单的代码:

XtManageChild(窗体);

这将显示选项菜单。