Simple general purpose pop-ups

The following example program shows how two of Ggi's general purpose pop-up elements can be used.
     The simplest is the element for simple yes/no questions which is created by calling GgiVerify(). It is different from most Ggi elements because it does not require any event handling in the application. It can be called as a function which immediately returns the outcome of the question. In the example this function is used to ask the user whether he really wants to terminate the program.

    
    
    

    
    
    
     The other element, created by GgiPrompter(), allows the user to answer a question by typing input in a text field. The input typed can then be accepted or rejected by the application by calling GgiPrompter() again with some special arguments. The user has the option to cancel the input request. In the example the element is used to request the name of a file to be written. For simplicity of the example, this is not a real filename but simply a text. If this text begins with ``exist'', we assume that the file exists and that the user should be asked whether it may be overwritten.

     The also exists a similar pop-up, created by GgiPlotPrompter(), which allows the user, besides of specifying a file name, to choose an available PGPLOT hardcopy device.
More powerful and complex general purpose pop-ups, e.g. for selecting files or specifying input sets, subsets and boxes, are discussed in the next page.

Example program:

/* example9.c -XT */

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

/*
 *  Button keyword handler for quitting.
 */
static void quit(ident id, char *key, int code, void *arg)
{
   bool button=toflog(FALSE);
    
   (void)userflog(&button, 1, 2, key, " ");
   if (tobool(button)) {
      wkeyf(key);                                         /* reset button    */
      if (GgiVerify("Really quit?", "Yes, quit", "No, continue")) {
         finis_c();
      }
   }
}

/*
 *  Button keyword handler creating the "file name" prompter.
 */
static void promptuser(ident id, char *key, int code, void *arg)
{
   bool button=toflog(FALSE);
    
   (void)userflog(&button, 1, 2, key, " ");
   if (tobool(button)) {
      GgiPrompter("FILENAME=", "Enter file name:");       /* create prompter */
      wkeyf(key);                                         /* reset button    */
   }
}

/*
 *  Keyword handler receiving the resulting "file name".
 */
static void getresult(ident id, char *key, int code, void *arg)
{
   static bool confirmed=FALSE;
   fint nread;
   fchar text;
   char ctext[128];

   text.a = ctext; text.l = 127;

   nread = userftext(text, 2, key, " ");
   ctext[nread] = '\0';
    
   if (nread) {
      if (strncmp(ctext, "exist", 4 )) {
         GgiPrompter("ACCEPT", NULL);      /* accept "non-existing" filename */
         confirmed = FALSE;
      } else if (!confirmed) {
         GgiPrompter("AGAIN", "OK to overwrite?");  /* ask user confirmation */
         confirmed = TRUE;
      } else {
         GgiPrompter("ACCEPT", NULL);           /* accept after confirmation */
         confirmed = FALSE;
      }
   } else {
      confirmed = FALSE;                            /* user pressed "CANCEL" */
   }
}


MAIN_PROGRAM_ENTRY
{
   ident button, quitbtn;
   
   init_c();
   
   GgiAutoLayout(FALSE);
   GgiPostponeRealize(TRUE); 
   
   button  = GgiButton("FILE=", "File name entry");
   quitbtn = GgiButton("QUIT=", "Terminate program");

   GgiSetPosition(button,  0, NULL,   0, NULL);
   GgiSetPosition(quitbtn, 0, button, 0, NULL);

   GgiRealize();

   (void)ScheduleKeyevent(promptuser, "FILE=", KEYCHANGE, NULL);
   (void)ScheduleKeyevent(quit, "QUIT=", KEYCHANGE, NULL);
   (void)ScheduleKeyevent(getresult, "FILENAME="  , KEYCHANGE, NULL);

   MainLoop();
}

Programming GIPSY Maintained by J. P. Terlouw