"Hello world!" in MUI

From MorphOS Library

Revision as of 21:37, 16 November 2010 by Krashan (talk | contribs) (Link added.)

Grzegorz Kraszewski


Helloworld.png
This is the window of the example MUI application. It is very simple, it contains only one static text object and no gadgets, except of window border gadgets. Note that the number of gadgets on the right side of 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, here is the ready to compile version.
#include <proto/muimaster.h>
#include <proto/intuition.h>

Header files for muimaster.library and intuition.library are included. Note that these libraries will be opened and closed automatically.

Object *App, *Win;

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.

Object* build_gui(void)
{
  App = MUI_NewObject(MUIC_Application,
    MUIA_Application_Author, (ULONG)"Grzegorz Kraszewski",
    MUIA_Application_Base, (ULONG)"HELLOWORLD",
    MUIA_Application_Copyright, (ULONG)"© 2010 Grzegorz Kraszewski",
    MUIA_Application_Description, (ULONG)"Hello World in MUI.",
    MUIA_Application_Title, (ULONG)"Hello World",
    MUIA_Application_Version, (ULONG)"$VER: HelloWorld 1.0 (16.11.2010)",
    MUIA_Application_Window, (ULONG)(Win = MUI_NewObject(MUIC_Window,
      MUIA_Window_Title, (ULONG)"Hello World",
      MUIA_Window_RootObject, MUI_NewObject(MUIC_Group,
        MUIA_Group_Child, MUI_NewObject(MUIC_Text,
          MUIA_Text_Contents, (ULONG)"Hello world!",
        TAG_END),
      TAG_END),
    TAG_END)),
  TAG_END);
}

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 attributes is self-explaining, please refer to the autodocs of respective MUI classes for details.

The function illustrates a typical way of creating 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 than order of reading. The most nested objects are created first and passed to constructors of their parents. The application object is created as the last one. This way of creating the application ensures also automatic error handling. If any of constructors fails, it passes NULL to a parent constructor, making it to fail too and to dispose all succesfully constructed children. 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. It greatly simplifies error handling.

void notifications(void)
{
  DoMethod(Win, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, App, 2,
   MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
}

The next step is to make notifications. This simple program contains only one notification, which terminates program after clicking the window close gadget. 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 passes the quit action identifier to the main loop.

void main_loop(void)
{
  ULONG signals = 0;

  set(Win, MUIA_Window_Open, TRUE);
			 
  while (DoMethod(App, MUIM_Application_NewInput, &signals) != MUIV_Application_ReturnID_Quit)
  {
    signals = Wait(signals | SIGBREAKF_CTRL_C);
    if (signals & SIGBREAKF_CTRL_C) break;
  }

  set(Win, MUIA_Window_Open, FALSE);
}

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.

int main(void)
{
  if (App = build_gui())
  {
    notifications();
    main_loop();
    MUI_DisposeObject(App);
  }

  return 0;
}

Finally the main function of the program. It builds the object tree first and checks if it succeeded. In case of 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.