passing Widget variables as arguments - Motif

This is a discussion on passing Widget variables as arguments - Motif ; I'm new to Motif programming and am trying to clean up and extend an existing Motif-based program written in C. If a variable is declared in a calling program as Widget w ; and I want to pass the variable ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: passing Widget variables as arguments

  1. passing Widget variables as arguments


    I'm new to Motif programming and am trying to clean up and extend an
    existing Motif-based program written in C.


    If a variable is declared in a calling program as

    Widget w ;

    and I want to pass the variable to a function that includes lines like

    void Test(Widget w)
    {
    ....
    w = XmCreatePulldownMenu(foo, name, NULL, 0);
    ....
    }


    so that the initialized value of w is successfully returned to the
    calling program, how is this properly done? I've been assuming that w
    is treated as a pointer to some kind of structure, so that the above
    syntax would work as intended. Instead, I find that the contents of w
    aren't surviving the trip back to the calling program.

    If I embed the XmCreatePulldownMenu call "in line" in the calling
    program, it works as intended, so it appears to be only an argument-
    passing issue that is causing me problems.

    Thanks for any advice.


  2. Re: passing Widget variables as arguments

    Qzectb wrote:
    > I'm new to Motif programming and am trying to clean up and extend an
    > existing Motif-based program written in C.
    >
    >
    > If a variable is declared in a calling program as
    >
    > Widget w ;
    >
    > and I want to pass the variable to a function that includes lines like
    >
    > void Test(Widget w)
    > {
    > ...
    > w = XmCreatePulldownMenu(foo, name, NULL, 0);
    > ...
    > }
    >
    >
    > so that the initialized value of w is successfully returned to the
    > calling program, how is this properly done? I've been assuming that w
    > is treated as a pointer to some kind of structure, so that the above
    > syntax would work as intended. Instead, I find that the contents of w
    > aren't surviving the trip back to the calling program.
    >
    > If I embed the XmCreatePulldownMenu call "in line" in the calling
    > program, it works as intended, so it appears to be only an argument-
    > passing issue that is causing me problems.
    >
    > Thanks for any advice.
    >


    That should work, provided foo is a global menubar widget and name is a
    global character string. You can't see the widget on the menubar
    because you haven't populated it with anything. But it's there. Try
    compiling this and then look at the widget tree with editres; you'll
    see it:

    #include
    #include
    #include

    Widget foo;
    char name[] = "name";

    void test(Widget);

    int main(int argc, char * argv[])
    {
    Widget toplevel, form, w;
    int ac;
    Arg av[5];

    XtAppContext app;

    toplevel = XtAppInitialize(&app, "Test", NULL, 0,
    &argc, argv, NULL, NULL, 0);
    form = XtVaCreateManagedWidget("form",
    xmFormWidgetClass, toplevel,
    XmNheight, 100,
    XmNwidth, 100,
    NULL);

    ac = 0;
    XtSetArg(av[ac], XmNrightAttachment, XmATTACH_WIDGET); ac++;
    XtSetArg(av[ac], XmNrightWidget, form); ac++;
    XtSetArg(av[ac], XmNleftAttachment, XmATTACH_WIDGET); ac++;
    XtSetArg(av[ac], XmNleftWidget, form); ac++;
    foo = XmCreateMenuBar (form, "menu_bar", av, ac);
    XtManageChild(foo);

    test(w);

    XtAddEventHandler ( toplevel, (EventMask) 0, True,
    (XtEventHandler) _XEditResCheckMessages, NULL );
    XtSetLanguageProc(NULL, NULL, NULL);
    XtRealizeWidget(toplevel);
    XtAppMainLoop(app);

    return 0;
    }

    void test(Widget w)
    {
    w = XmCreatePulldownMenu(foo, name, NULL, 0);
    }

  3. Re: passing Widget variables as arguments


    "Qzectb" wrote in message
    news:1182830090.292447.91920@w5g2000hsg.googlegrou ps.com...
    >
    > I'm new to Motif programming and am trying to clean up and extend an
    > existing Motif-based program written in C.
    >
    >
    > If a variable is declared in a calling program as
    >
    > Widget w ;
    >
    > and I want to pass the variable to a function that includes lines like
    >
    > void Test(Widget w)
    > {
    > ...
    > w = XmCreatePulldownMenu(foo, name, NULL, 0);
    > ...
    > }
    >
    >
    > so that the initialized value of w is successfully returned to the
    > calling program, how is this properly done? I've been assuming that w
    > is treated as a pointer to some kind of structure, so that the above
    > syntax would work as intended. Instead, I find that the contents of w
    > aren't surviving the trip back to the calling program.
    >
    > If I embed the XmCreatePulldownMenu call "in line" in the calling
    > program, it works as intended, so it appears to be only an argument-
    > passing issue that is causing me problems.
    >
    > Thanks for any advice.
    >


    Passing w as a variable like it is done above is not of any value. The local
    copy of w in function Test is assigned a value, but the w in the calling
    module remains unchanged. After the call to Test, the widget is no
    longer available for any modifications or other use.

    A much better way is:

    Widget Test( Widget foo, String name )
    {
    Widget wm;
    wm = XmCreatePulldownMenu(foo, name, NULL, 0);
    return wm;
    }

    then call it from some other module as:
    Widget w;
    ...
    w = Test( foo, name );
    ...

    Now the w in the calling program is available for future use, such as adding
    other items to its menu, or changing sensitivity of existing menu items.
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Aero Stability and Controls Computing



  4. Re: passing Widget variables as arguments

    Fred Kleinschmidt wrote:
    >
    > Passing w as a variable like it is done above is not of any value. The local
    > copy of w in function Test is assigned a value, but the w in the calling
    > module remains unchanged. After the call to Test, the widget is no
    > longer available for any modifications or other use.
    >


    Bad form though it may be, the widget will persist if it is parented to
    a global...

+ Reply to Thread