Solved

Is XtAppAddWorkProc the proper way to launch a Popup at startup?  Getting crashes...

Posted on 2004-09-02
2
502 Views
Last Modified: 2010-05-18
When my app starts it reads a user config file and based on the contents of that file may need to launch a dialog to get user inputs before proceeding.  In Java I can run initialization blocks which include dialogs prior to launching the main window GUI with .show().  How can I do this in Motif?  I tried using XtAppAddWorkProc as in the following code, but what I get is a dialog which pops up and disappears without even waiting for a user response.  (All this code runs OK when I don't use the WorkProc).

Widget toplevel;

main (int argc, char *argv[])
{
  XtAppContext app;
  Widget main_window;

  XtSetLanguageProc (NULL, NULL, NULL);
  toplevel = XtVaAppInitialize (&app, "App", NULL, 0, &argc, argv, NULL,
              XmNtitle,    "...",
              NULL);
  main_window = XtVaCreateManagedWidget ("main_window",
               xmMainWindowWidgetClass, toplevel,
               NULL);

    create_menu_bar (main_window);
    create_forms (main_window);
 
    XtRealizeWidget (toplevel);

    XtAppAddWorkProc (app, run_startup, (XtPointer) toplevel);

    XtAppMainLoop (app);
}

static Boolean run_startup (XtPointer client_data)
{
  static done=false;
  Widget dialog;

  if (!done) {
      dialog = XmCreateQuestionDialog (toplevel, "notice", NULL, 0);
      XtVaSetValues (dialog,
                 XmNdialogTitle, XmStringCreateLocalized ("Response"),
                 XmNmessageString, XmStringCreateLocalized ("Question to user"),
                 XmNokLabelString, XmStringCreateLocalized ("Yes"),
                 XmNcancelLabelString, XmStringCreateLocalized ("No"),
                 NULL);
      XtAddCallback (dialog, XmNokCallback, question_cb, NULL);
      XtAddCallback (dialog, XmNcancelCallback, question_cb, NULL);
      XtUnmanageChild (XmMessageBoxGetChild (dialog, XmDIALOG_HELP_BUTTON));
      XtManageChild (dialog);
      XtPopup (XtParent (dialog), XtGrabNone);
  }
  done=true;

  return True;
}

void question_cb (Widget parent, XtPointer client_data, XtPointer call_data)
{
    XmAnyCallbackStruct *cbs = (XmAnyCallbackStruct *) call_data;

     switch (cbs->reason) {
    case XmCR_OK :
      strcpy (Ans,"Y");
      break;
    case XmCR_CANCEL :
      strcpy (Ans,"N");
      XtPopdown (XtParent (parent));
      break;
    case XmCR_ACTIVATE :
      break;
    case XmCR_HELP :
    }
}
0
Comment
Question by:jimdgar
2 Comments
 
LVL 11

Accepted Solution

by:
griessh earned 500 total points
ID: 11966834
Hi jimdgar,

No need to use a workproc, they are just ugly beasts sunning in the background. Just run you program like this

#include <Xm/Xm.h>
#include <Xm/MainW.h>
#include <Xm/RowColumn.h>
#include <Xm/MessageB.h>

Widget toplevel;
void question_cb (Widget parent, XtPointer client_data, XtPointer call_data);

main (int argc, char *argv[])
{
  XtAppContext app;
  Widget main_window;

  XtSetLanguageProc (NULL, NULL, NULL);
  toplevel = XtVaAppInitialize (&app, "App", NULL, 0, &argc, argv, NULL,
            XmNtitle,    "...",
            NULL);
  main_window = XtVaCreateManagedWidget ("main_window",
             xmMainWindowWidgetClass, toplevel,
             NULL);

//    create_menu_bar (main_window);
//    create_forms (main_window);

    XtRealizeWidget (toplevel);


    Widget dialog = XmCreateQuestionDialog (toplevel, "notice", NULL, 0);
    XtVaSetValues (dialog,
               XmNdialogTitle, XmStringCreateLocalized ("Response"),
               XmNmessageString, XmStringCreateLocalized ("Question to user"),
               XmNokLabelString, XmStringCreateLocalized ("Yes"),
               XmNcancelLabelString, XmStringCreateLocalized ("No"),
               NULL);
    XtAddCallback (dialog, XmNokCallback, question_cb, NULL);
    XtAddCallback (dialog, XmNcancelCallback, question_cb, NULL);
    XtUnmanageChild (XmMessageBoxGetChild (dialog, XmDIALOG_HELP_BUTTON));
    XtManageChild (dialog);
    XtPopup (XtParent (dialog), XtGrabNone);

    XtAppMainLoop (app);
}


void question_cb (Widget parent, XtPointer client_data, XtPointer call_data)
{
    XmAnyCallbackStruct *cbs = (XmAnyCallbackStruct *) call_data;

    switch (cbs->reason) {
    case XmCR_OK :
//      strcpy (Ans,"Y");
      break;
    case XmCR_CANCEL :
//      strcpy (Ans,"N");
      XtPopdown (XtParent (parent));
      break;
    case XmCR_ACTIVATE :
      break;
    case XmCR_HELP :
      break;
    }
}


======
Werner
0
 

Author Comment

by:jimdgar
ID: 11967990
I tried that method previously and it didn't work, but now that I try your short example it's OK.  Obviously something else is wrong with my full code.

Thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now