This is the window of the example MUI application. It is very simple, it contains only one static text object and no gadgets, except for window border gadgets. Note that the number of gadgets on the right side of the window depends on user MUI settings. The source code of the application is very simple too and fits into 60 lines, including proper vertical spacing. It is cut in pieces in the article for better reading. A complete, ready to compile version is available ↑ too.
Header files for muimaster.library and intuition.library are included. Note that these libraries will be opened and closed automatically.
Global pointers for the application object and the window object. While using many global variables is considered inelegant, having globals for the most important objects is handy, especially in such a small project.
The above function creates the complete object tree for HelloWorld. The master object is the Application class instance. It has a Window object. The window root is a Group object containing one Text object. The application object has 6 attributes working as descriptors used in different places in the system. They are not required for running the program, but help integrating it with the system. The meaning of these attributes is explained in the autodoc of Application class in the SDK. The rest of the attributes are self-explaining, please refer to the autodocs of respective MUI classes for details.
The function illustrates a typical way of creating a MUI interface. The complete object tree is created in one big MUI_NewObject() call containing nested sub-calls. The order of code execution is different to the order of reading. The most nested objects are created first and passed to constructors of their parents. The application object is created last. This way of creating the application also ensures automatic error handling. If any of constructors fails, it passes NULL to a parent constructor, making it fail too and then dispose all successfully constructed child objects. Finally the application constructor fails and returns NULL. Then only two states of the application are possible: either the application is fully constructed, or it is not constructed at all. This behaviour greatly simplifies error handling.
The next step is to make notifications. This simple program contains only one notification, which terminates the program after the window close gadget is clicked. MUI maps a left mouse button click on the gadget to a change of MUIA_Window_CloseRequest attribute. The notification target is the application, the MUIM_Application_ReturnID() method then passes the quit action identifier to the main loop.
The standard main loop has been discussed in the previous chapter. The only addition is opening the window before the loop and closing it after.
Finally, the main function of the program. It builds the object tree first and checks if it succeeded. In case of a fail, the program ends immediately. After all the objects are created, notifications are added and the program enters the main loop. When the loop finishes, the application object is disposed. It also disposes all its sub-objects.