Using the C Math Library | ![]() ![]() |
Sending Output to a GUI
When you write a program that runs in a graphical windowed environment, you may want to display printed messages in an informational dialog box. The next two sections illustrate how to provide an alternate print handler under the X Window System and Microsoft Windows.
Each example demonstrates the interface between the MATLAB C Math Library and the windowing system. In particular, the examples do not demonstrate how to write a complete, working program.
Each example assumes that you know how to write a program for a particular windowing system. The code contained in each example is incomplete. For example, application start up and initialization code is missing. Consult your windowing system's documentation if you need more information than the examples provide.
Each example presents a simple alternative output mechanism. 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.
Note
If you use an alternate print handler, you must call mlfSetPrintHandler before calling other library routines. Otherwise the library uses the default print handler to display messages.
|
Example - Writing Output to X Windows/Motif
The Motif Library provides a MessageDialog
widget, which this example uses to display text messages. The MessageDialog
widget consists of a message text area placed above a row of three buttons labeled OK, Cancel, and Help.
The message box is a modal dialog box; once it displays, you must dismiss it before the application will accept other input. However, because the MessageDialog
is a child of the application and not the root window, other applications 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.*
/ mlfSetPrintHandler(PopupMessageBox); /*
The rest of your 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. This code is not shown, since it is common to most applications, and can be found in your X Windows reference guide. main
then creates the MessageDialog
object that is used by the print handling routine. Finally, main
calls mlfSetPrintHandler
to inform the library that it should use PopupMessageBox
instead of the default print handler. If this were a complete application, the main routine would also contain calls to other routines or code to perform computations.
Example - Writing Output to Microsoft Windows
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, your application will not accept other 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
, is responsible for placing the message into the MessageBox
and then posting the box to the screen. The second, main
, which in addition to creating and starting the Microsoft Windows application (that code is not shown) calls mlfSetPrintHandler
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, provide window procedure*
/ /*
Fill in your own code here.*
/ /*
Create application main window*
/ window = CreateWindowEx( /*
your specification*
/ ); /*
Set print handler*
/ mlfSetPrintHandler(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.
![]() | Providing Your Own Print Handler | Library Routines | ![]() |