An Introduction to MUI Programming

In this tutorial, I hope to explain the basics of MUI programming. I'll be concentrating on C/C++, although what I say can be applied to other languages.

The thing about MUI is that it makes programming interfaces incredibly easy - far easier than the standard AmigaOS way. The hardest part is taking the plunge and deciding you want to learn - after that, you'll find yourself learning at a rapid rate. Later on, you'll come across custom classes, and admittedly, you may be put off by these when you see them, as it makes programming different somewhat to what you may have been used to (they're very object orientated), but nonetheless, I recommend learning MUI over the standard routines for both its power and ease of use.

You're probably familiar with the way MUI works from a user's point of view, and you should bear this in mind when programming. Every window in an MUI applicaton can be resized, and the interface is resized accordingly. As a programmer, you don't have to worry about this. In fact, designing interfaces is simply a matter of listing the gadgets in turn, and letting MUI lay them out - you certainly don't have to specify individual coordinate positions. This makes designing, and also editing your interfaces extremely easy.

Another feature of MUI applications is that the user decides what screen to open on, and this screen will be a public one. This means that you shouldn't assume that you are on a particular screen, and you may be sharing with other programs (indeed, you may be on the Workbench). In particular, you should be careful about using and setting colour indices. You should use the standard routines (ObtainBestPen(), FindColor(), etc) to get the colour you want, or give you an unused pen if there isn't such a colour - but we'll cover this in more detail in a later tutorial.

What Do You Need?

You should get hold of the developers' archive for MUI, available on Aminet (and should be on most CoverCDs). Follow the instructions for your chosen language, installing the necessary includes.

Also, in the examples folder is a file 'demo.h'. I found this to be a useful file to have for all MUI programs you wrote, so I copied this to my Includes folder too, and you should do also for the benefit of this tutorial. I also suggest renaming it to something more descriptive ('mui_header.h' for the purposes of this tutorial).

One thing I had trouble with was using MUI with C++ (with StormC, at least - C mode was fine). If anyone else is having trouble, then do the following. The first concerns the 'demo.h' file I said about. The line:

static VOID fail(APTR app,char *str)
for compatibility with C++ should read:
static VOID fail(Object *app,char *str)
If you are still having problems, then check out the file mui.h which was installed to Include/libraries/. At the end of the 'Macro' definitions, there's a bit where it says 'Controlling Objects'. Just after that is some code enclosed in '#ifndef __cplusplus' and '#endif'. I have no idea why this should be (can anyone enlighten me?) I wanted to use the 'get' and 'set' routines in C++, so I just commented out the two said lines, and everything worked fine.

Lastly, some changes are necessary for StormC (both C or C++), regarding the Register macros. These are defined in 'demo.h', but not for Storm C. The ones for Storm C are:
#define REG(x) register __## x
#define ASM
#define SAVEDS

Hope that works...

Your First MUI Program

Now let's finally look at an MUI program. We'll be using plain C, so you don't need to worry about the above for now (but you do need to copy 'demo.h' to your Includes folder, and rename it to 'mui_header.h'. The source for this program is under the file 'mui_dc7tut1.c'.

Compile it, and you'll see it opens up a window with a few gadgets in it. The source code is commented, so shouldn't be too hard to understand. The key point is that the window is all defined in the bit following on from 'app = ApplicationObject,' - in fact, you generally define all the windows for an application beforehand, even if they are not initially open.

The code in this section (where the windows are defined) is not 'proper' C code, since it is using macros (defined in Include/libraries/mui.h) to make things even easier. You just lay out your objects, using things like VGroup, HGroup, Child and ColGroup() to lay out things the way you want them. Note that we have the pointers STR_example and CY_example for pointers to the string and cycle gadgets respectively. Although unused in this example, you would need them if you wanted to refer to the gadgets (read or set the contents, for example).

The initial 'MUIA_Application_' tags just set up some necessary information about your program.

Learning More...

The thing about MUI is that you can generally learn by just looking at examples, so be sure to check out all the files in the Examples folder in the MUI developers' archive, and of course the Autodocs contain all you need to know.

In future tutorials, I'll be looking at the aspects of MUI which aren't too well covered by the documentation then...

Mark Harman