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.

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.

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().

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