Graphical User Interface Example

The program below provides a simple example of the use of GIPSY's event-handling and graphical user interface facilities. It is a tested program, which you can extract from this page, compile and run. It then should produce a window like this:

To keep this first example as simple as possible, the layout of the window was done automatically by Ggi. In a serious application, the layout would normally be done explicitly by the program. This is not complicated, it only requires one extra function call per element. A second example shows how this is done.

The active elements in this program are two menus, labeled ``DRINK'' and ``FOOD'', two text input fields, an analog valuator and a push-down button, labelled ``QUIT''.

If you play with this program, which is highly recommended, you will notice that one of the text input fields and the analog valuator follow each other. This is because they share the same user input keyword.

The keywords NUMBER= and GETAL= share the same event handler function. The code in this function expects a number, so it may be interesting to experiment with this. For instance try to enter a mathematical expression. Or an illegal value. In the latter case you will see that an error message is displayed near the field in which the error was made.

Example program:

/* example1.c -XT */

#include "stddef.h"
#include "gipsyc.h"
#include "cmain.h"
#include "init.h"
#include "finis.h"
#include "userfio.h"
#include "ggi.h"

 *   Keyword handler for QUIT=
static void quit(ident id, char *key, int code, void *arg)
   bool finished=toflog(FALSE);
   (void)userflog(&finished, 1, 2, key, " ");
   if (tobool(finished)) finis_c();

 *   Keyword handler for both NUMBER= and GETAL=
static void number(ident id, char *key, int code, void *arg)
   float result;

   if (userfreal(&result, 1, 2, key, " ")==1)
      anyoutf(0, "The number %f was given for %s", result, key);

 *   Keyword handler for both DRINK= and FOOD=
 *   Note that the argument 'items' was specified by the registration calls.
static void order(ident id, char *key, int code, void *items)
   fint result;
   if (userfint(&result, 1, 2, key, " ")==1)
      anyoutf(0, "You ordered %s", ((char**)items)[result]);

 *   Main program
   static char *drinks[]={"Water","Milk","Beer","Wine","Sherry","Whisky",NULL};
   static char *foods[]={"Beans","Peas","Lettuce","Potatoes","Meat",NULL};


 *    Create elements.
   (void)GgiButton("QUIT=", "Terminate program");
   (void)GgiTextField("NUMBER=", "Type a number in this field", 15);
   (void)GgiTextField("GETAL=",  "Typ een getal\nin dit veld",  15);
   (void)GgiGauge("NUMBER=", "Produce numbers from -1 to +1", 200, -1.0, 1.0);
   (void)GgiMenu("DRINK=","Order your drink", drinks);
   (void)GgiMenu("FOOD=","Order your food", foods);

 *    Register keyword callbacks.
   (void)ScheduleKeyevent(quit,   "QUIT=",   KEYCHANGE, NULL);
   (void)ScheduleKeyevent(number, "NUMBER=", KEYCHANGE, NULL);
   (void)ScheduleKeyevent(number, "GETAL=",  KEYCHANGE, NULL);
   (void)ScheduleKeyevent(order,  "DRINK=",  KEYCHANGE, drinks);
   (void)ScheduleKeyevent(order,  "FOOD=",   KEYCHANGE, foods);

 *    Put things into action.

Programming GIPSY Maintained by J. P. Terlouw