#include "exec/types.h"
#include <stdio.h>
#include <stdlib.h>
#include <intuition/intuition.h>
#include <graphics/gfxmacros.h>
#include <devices/input.h>
#include <devices/inputevent.h>

// this routine provided by the programmer
void displayroutine(struct RastPort *DRrastport,struct ListNode *list,short y)
{
struct Window *DRwindow;
DRwindow = (struct Window *)list;
    if (DRwindow->Title) 
      {
      SetAPen(DRrastport,2);
      Move(DRrastport,2,y+6);
      Text(DRrastport,DRwindow->Title,strlen(DRwindow->Title));
      }
}

#include "poplist.hxx"	// header file
#include "poplist.h"   	// methods
#include "gone.h"  		// window defines 
// see comments for C++ in file poplist.h
BOOL Debug = 0;  // set to 1 to print debug # to stdio
main()
   
/*      The main program.  Open an Intuition window in Workbench
        with two gadgets, one to send a window to the front, the
        second to send it to the back.

        When a gadget is clicked on, call ScrollingPopUpMenu() and
        pass it the list of Workbench windows.  If the user selects
        one of the windows, either call WindowToFront(window) or
        WindowToBack(window), depending on which gadget was clicked.
*/
{
    PopUpList popup_list;  
	 if (popup_list.openflag == 0)
		{
		Gripe("Unable to open list manager.");
		exit(20);
		}
		
    struct IntuiMessage *message;
    struct Window *window;
    struct Window *selectwindow;
    long miclass;  /* code*/
    struct Gadget *gadget;
    window =(struct Window *) OpenWindow(&wNewWindowStructure1);
    if (window) {
        for (;;) {
            message = 0;
            while (!message) {
                WaitPort(window->UserPort);
                message = (struct IntuiMessage *) GetMsg(window->UserPort);
            }
            miclass = message->Class;
            gadget = (struct Gadget *) message->IAddress;
            ReplyMsg(message);
            if (miclass == CLOSEWINDOW) break;
            selectwindow = (struct Window *)popup_list.ScrollingPopUpMenu(window->WScreen,(struct ListNode *)window->WScreen->FirstWindow,(WORD)8,(WORD)100,(WORD)4);
            if (miclass == GADGETDOWN) {
                if (gadget->GadgetID == 1) {
                    if (selectwindow) WindowToFront(selectwindow);
                }
            else if (gadget->GadgetID == 2) {
               if (selectwindow) WindowToBack(selectwindow);
                }
            }
        }
        CloseWindow(window);
    }
exit(20);
}
