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); 
}
Razzupaltuff
  • 2,250
  • 2
  • 21
  • 37
  • As it happens, your last question http://stackoverflow.com/questions/1386319/ho-to-kill-xtappmainloop-motif was answered fairly promptly, but you haven't upvoted it, accepted it, or left a comment requesting more help. In fact, you don't seem to have ever accepted an answer to any of your questions. Do you feel like you are not getting helpful answers? – DigitalRoss Sep 06 '09 at 21:45
  • Actually the main question with the above code I have is what happens to topWidget and appContext when I set appContext.exit_flag = 1. Do I have to return them to Motif somehow (how), or will they be released automatically? That is something hard to try out - unlike testing whether the above code displays a message box – Razzupaltuff Sep 06 '09 at 21:57
  • I thought I cannot upvote with < 100 reputation. Upvoted and marked your reply in the other question. – Razzupaltuff Sep 06 '09 at 22:00
  • To answer your question: The only helpful answer (so I hope) so far was yours. I am looking for a lightweight message box and would like to be able to avoid having to link half a dozen or more extra libraries to my application just for something as apparently simple as a message box. It's pain in the neck to achieve something like that on Linux with acceptable overhead. I hope using Motif does not mean I have to wade through extra libs ... – Razzupaltuff Sep 06 '09 at 22:06
  • I agree, using more giant GUI libraries to solve a simple problem would bother me too. I'm sure Motif will get the job done, in its day it was top-of-the-line. And thanks for the feedback! I will do my best to get you some more good answers. – DigitalRoss Sep 06 '09 at 22:20
  • If Motif gets the job done I'd be satisfied. Actually I need the message box for some diagnostic output when my program determines critical flaws in its setup. Printing to stderr will go unnoticed if a user launched it via a desktop starter or something like openSuSE's application menu. I have devised a different solution using a non editable text widget that allows scrolling to be able to display more text. – Razzupaltuff Sep 06 '09 at 22:38
  • If everything works, I will present it here. I had found one or two questions similar to mine on the inet, and both ended with their author saying "found the solution, thanks and goodbye", which really made me gnash my teeth in frustration. Why the heck can't such people at least post a good pointer to their solution? – Razzupaltuff Sep 06 '09 at 22:40

1 Answers1

1

I think the following code could be what I asked for:

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
Razzupaltuff
  • 2,250
  • 2
  • 21
  • 37