/** listreq.c
*
*   A GadTools List requester
*
*   Willy Langeveld, Early 1992.
*
**/
/*
*   Standard includes for SAS C.
*/
#include <exec/types.h>
#include <exec/exec.h>
#include <intuition/intuitionbase.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/gfxbase.h>
#include <graphics/text.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <proto/all.h>
/*
*   The layout of the requester
*/
#define MARGIN           10
#define GADGET_SPACING    5
/*
*   The library we need
*/
extern struct Library *GadToolsBase;
/*
*   Use Topaz-80 font.
*/
static struct TextAttr topaz80 = { "topaz.font", 8, 0, 0 };
/*
*   Prototype
*/
struct Node *ListRequest(struct Screen *, struct List *, long, long, long, long);
/*
*   quitflag values
*/
#define NOTDONE  0
#define NOCHANGE 1
#define CHANGE   2


/**
*
*
**/
struct Node *ListRequest(scr, list, x, y, width, height)
struct Screen *scr;
struct List *list;
long x, y, width, height;
{
   struct VisualInfo *vi = NULL;
   struct Gadget *glist = NULL, *gad, *gadget;
   struct NewGadget ng;
   struct Window *w = NULL;
   long top, quitflag = NOTDONE;
   ULONG class;
   WORD code, selected;
   struct IntuiMessage *msg;
   struct Node *n = NULL;
/*
*   Check args
*/
   if (width < 80)  width = 80;
   if (height < 70) height = 70;
/*
*   Get screen info
*/
   if (scr) {
      if (((scr->Flags & SCREENTYPE) == PUBLICSCREEN) ||
          ((scr->Flags & SCREENTYPE) == CUSTOMSCREEN)   ) {
         vi = GetVisualInfoA(scr, NULL);
      }
   }
/*
*   Otherwise get access to the WorkBench screen
*/
   if (vi == NULL) {
      scr = LockPubScreen(NULL);
      if (scr == NULL) goto cleanup;

      vi = GetVisualInfoA(scr, NULL);
      UnlockPubScreen(NULL, scr);
   }

   if (vi == NULL) goto cleanup;
/*
*   Now open up the window
*/
   w = OpenWindowTags(NULL,
                      WA_Left,         (ULONG) x,
                      WA_Top,          (ULONG) y,
                      WA_Width,        (ULONG) width,
                      WA_Height,       (ULONG) height,
                      WA_IDCMP,        (ULONG) LISTVIEWIDCMP | CLOSEWINDOW,
                      WA_Flags,        (ULONG) WINDOWCLOSE  | SMART_REFRESH |
                                               WINDOWDRAG   | WINDOWDEPTH   |
                                               ACTIVATE,
                      WA_Title,        (ULONG) "Select item:",
                      WA_CustomScreen, (ULONG) scr,
                      TAG_DONE);

   if (w == NULL) goto cleanup;
/*
*   Create the GadTools context
*/
   gad = CreateContext(&glist);
   if (gad == NULL) goto cleanup;
/*
*   A palette gadget
*/
   top = w->BorderTop + GADGET_SPACING;

   ng.ng_LeftEdge   = MARGIN;
   ng.ng_TopEdge    = top;
   ng.ng_Width      = width - 2 * MARGIN;
   ng.ng_Height     = height - top - GADGET_SPACING;
   ng.ng_GadgetText = NULL;
   ng.ng_GadgetID   = 1;
   ng.ng_Flags      = 0;
   ng.ng_TextAttr   = &topaz80;
   ng.ng_VisualInfo = vi;
   gad = CreateGadget(LISTVIEW_KIND, gad, &ng,
                      GTLV_Labels,       list,
                      GTLV_ScrollWidth,  18,
                      TAG_DONE);
/*
*   Add the gadget list to the window.
*/
   AddGList(w, glist, -1, -1, NULL);
   RefreshGList(glist, w, NULL, -1);
   GT_RefreshWindow(w, NULL);
/*
*   Wait for the user to do things
*/
   while (quitflag == NOTDONE) {
      WaitPort(w->UserPort);
      while (msg = GT_GetIMsg(w->UserPort)) {
         class  = msg->Class;
         code   = (WORD) msg->Code;
         gadget = (struct Gadget *) msg->IAddress;

         GT_ReplyIMsg(msg);

         if (class == GADGETUP) {
            selected = code;
            quitflag = CHANGE;
         }
         else if (class == CLOSEWINDOW) {
            quitflag = NOCHANGE;
         }
      }
   }
/*
*   This sequence shuts down the list requester
*/
cleanup:
   if (w)     CloseWindow(w);
   if (glist) FreeGadgets(glist);
   if (vi)    FreeVisualInfo(vi);

   if (quitflag == CHANGE) {
      if (list) {
         n = list->lh_Head;
         while (selected--) n = n->ln_Succ;
      }
   }

   return(n);
}



