Using the C++ Math Library | ![]() ![]() |
Output to a GUI
The next two sections illustrate how to provide an alternate print handler under the X Window System and Microsoft Windows. When you write a program that runs in a graphical windowed environment, you can display printed messages in informational dialog boxes.
These examples present a simple alternative output mechanism and demonstrate the interface between the MATLAB C++ Math Library and each of the windowing systems. There are other output options as well, for example, sending output to a window or portion of a window inside an application. The code in these examples should serve as a solid foundation for writing more complex output routines.
The examples assume that you know how to write a program for a particular windowing system and, therefore, omit code that is common to such programs, for example, the application start-up and initialization code is missing. Please consult your windowing system's documentation if you need more information than the examples provide.
Note
If you use an alternate print handler, you must call mwSetPrintHandler() before calling other library routines. Otherwise the library uses the default print handler to display messages. |
X Windows System/Motif Example
The Motif Library provides a MessageDialog
widget that this example uses to display text messages. The MessageDialog
widget consists of a message text area placed above a row of three buttons: OK, Cancel, and Help.
The MessageDialog
box is a modal dialog box; while it is posted, this application will not accept input. You must press the OK button to dismiss the MessageDialog
dialog box before you can do anything else. However, since the MessageDialog
is a child of the application, and not the root window, other applications will continue to operate normally.
/* X-Windows/Motif Example */ /*
List other X include files here*
/ #include <Xm/Xm.h> #include <Xm/X11.h> #include <Xm/MessageB.h> static Widget message_dialog = 0; /*
The alternate print handler*
/ void PopupMessageBox(const char *message) { Arg args[1]; XtSetArg(args[0], XmNmessageString, message); XtSetValues(message_dialog, args, 1); XtPopup(message_dialog, XtGrabExclusive); } main() { /* Start X application. Insert your own code here. */ main_window = XtAppInitialize( /* your code */ ); /* Create the message box widget as a child of */ /* the main application window. */ message_dialog = XmCreateMessageDialog(main_window, "MATLAB Message", 0, 0); /* Set the print handler */ mwSetPrintHandler(PopupMessageBox); /* The rest of the program */ }
This example declares two functions: PopupMessageBox()
and main()
. PopupMessageBox
is the print handler and is called every time the library needs to display a text message. It places the message text into the MessageDialog
widget and makes the dialog box visible.
The second routine, main()
, first creates and initializes the X Window system application. That code is not shown but can be found in an X Windows reference guide. main()
then creates the MessageDialog
object used by the print handling routine. Finally, main()
calls mwSetPrintHandler()
to make the library call PopupMessageBox()
instead of the default print handler. If this were a real application, the main routine would continue with calls to other routines or code to perform computations.
Microsoft Windows Example
This example uses the Microsoft Windows MessageBox
dialog box. This dialog box contains an "information" icon, the message text, and a single OK button. The MessageBox
is a Windows modal dialog box; while it is posted, no other application will accept input. You must press the OK button to dismiss the MessageBox
dialog box before you can do anything else.
This example declares two functions. The first, PopupMessageBox()
, places the message into the message box and then posts the box to the screen. The second, main()
, creates and starts the Windows application (that code is not shown), and then calls mwSetPrintHandler()
to set the print handling routine to PopupMessageBox()
.
/* Microsoft Windows example */ static HWND window; static LPCSTR title = "Message from MATLAB"; /* The alternate print handler */ void PopupMessageBox(const char *message) { MessageBox(window, (LPCTSTR)message, title, MB_ICONINFORMATION); } main() { /* Register window class and provide window procedure. */ /* Fill in your own code here. */ /* Create application main window. */ window = CreateWindowEx( /* Whatever */ ); /* Set print handler. */ mwSetPrintHandler(PopupMessageBox); /* The rest of the program ... */ }
This example does no real processing. If it were a real program, the main routine would contain calls to other routines or perform computations of its own.
![]() | Using the Print Handler to Print Your Own Messages | Handling Exceptions | ![]() |