Difference between revisions of ""Hello world!" in MUI"

From MorphOS Library

m (Code style.)
m (minor readability fixes)
Line 2: Line 2:
  
  
[[File:helloworld.png|left]] 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, a complete, ready to compile version [http://krashan.ppa.pl/morphzone_tutorials/helloworld_mui.c is available] too.
+
[[File:helloworld.png|left]] 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 [http://krashan.ppa.pl/morphzone_tutorials/helloworld_mui.c is available] too.
  
 
  #include <proto/muimaster.h>
 
  #include <proto/muimaster.h>
Line 33: Line 33:
 
  }
 
  }
 
   
 
   
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 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 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.
+
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.
  
 
  void notifications(void)
 
  void notifications(void)
Line 43: Line 43:
 
  }
 
  }
  
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.
+
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.
  
 
  void main_loop(void)
 
  void main_loop(void)
Line 76: Line 76:
 
  }
 
  }
  
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.
+
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.

Revision as of 07:28, 19 November 2010

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 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.
#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 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.

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

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)
{
  App = build_gui();

  if (App)
  {
    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 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.