1

I want a message box for Linux similar to the Windows one: It should pop up, display some text, and when the user clicks the Ok button, it should disappear and return control to the calling function.

The message box should work even if there is no application window yet at all. So it creates an application context, ties a dialog via XmCreate*Dialog to it, and when the user clicks the dialogs Ok button, tells the app contexts main loop to exit.

Would this work like intended? Would this automatically destroy all widgets and the app context that got created in the process (if no, how would that have to be done?)?

Here's the code:

static XtAppContext appContext; static Widget topWidget; void XmCloseMsgBox (Widget w, XtPointer clientData, XmPushButtonCallbackStruct *cbs) { appContext.exit_flag = 1; } void XmMessageBox (const char* pszMsg, bool bError) { Widget msgBox; XmString xmString = XmStringCreateLocalized (const_cast<char*>(pszMsg)); Arg args [1]; topWidget = XtVaAppInitialize (&appContext, "Application Name", NULL, 0, &gameData.app.argC, gameData.app.argV, NULL, NULL); // setup message box text XtSetArg (args [0], XmNmessageString, xmString); // create and label message box xMsgBox = bError ? XmCreateErrorDialog (topWidget, "Error", args, 1) : XmCreateWarningDialog (topWidget, "Warning", args, 1); // remove text resource XmStringFree (xmString); // remove help and cancel buttons XtUnmanageChild (XmMessageBoxGetChild (xMsgBox, XmDIALOG_CANCEL_BUTTON)); XtUnmanageChild (XmMessageBoxGetChild (xMsgBox, XmDIALOG_HELP_BUTTON)); XtAddCallback (xMsgBox, XmNokCallback, XmCloseMsgBox, NULL); XtRealizeWidget (topWidget); // display message box //XtManageChild (xMsgBox); XtAppMainLoop (app); }
4

1 回答 1

1

我认为以下代码可能是我要求的:

XtRealizeWidget (topWid);
// display message box
appContext.exit_flag = 0;
XtAppMainLoop (app); 
while (!appContext.exit_flag) // wait in case XtAppMainLoop has its own thread
    ;
XtUnrealizeWidget (topWid); // destroy topWid and all its children
于 2009-09-06T22:34:16.290 回答