home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 418.lha / ListWindowPackage / test.c < prev    next >
C/C++ Source or Header  |  1990-09-10  |  5KB  |  206 lines

  1. /* test.c - program to demonstrate/test the ListWindow Package */
  2. /*          by Paul T. Miller                                  */
  3. /*                (Link with listwin_pak.o and graphics_pak.o) */
  4.  
  5. #include <intuition/intuition.h>
  6. #include "graphics_pak.h"
  7. #include "listwin_pak.h"
  8.  
  9. #define SCREEN_WIDTH    640
  10. #define SCREEN_HEIGHT   200
  11. #define SCREEN_DEPTH    2
  12. #define SCREEN_TITLE    "ListWindow Test"
  13. #define WIN_X           0
  14. #define WIN_Y           11
  15. #define WIN_W           223
  16. #define WIN_H           85
  17. #define MIN_WIDTH       100
  18. #define MIN_HEIGHT      66
  19. #define MAX_WIDTH       350
  20. #define MAX_HEIGHT      SCREEN_HEIGHT
  21.  
  22. struct NewWindow nw;
  23.  
  24. struct Screen *screen = NULL;
  25. struct Window *listwin = NULL;
  26.  
  27. UBYTE *namelist[] = {
  28.    "Long Sword", "Short Sword", "Dagger", "Javelin", "Club", "Mace",
  29.    "Broad Sword", "Axe", "Bow", "Sling-Shot", "Spear", "Staple Gun",
  30.    "Shotgun", "Meat Cleaver", "Scyth"
  31. };
  32.  
  33. UBYTE listbuffer[40];      /* this is where a selected name will be copied */
  34.  
  35. SHORT names = 15;
  36.  
  37. void main(void);
  38. void abort(char *);
  39. void setup(void);
  40. void setup_display(void);
  41. void closedown(void);
  42. void close_display(void);
  43. void handle_input(void);
  44.  
  45. void main()
  46. {
  47.    setup();
  48.    handle_input();
  49. }
  50.  
  51. void abort(txt)
  52. char *txt;
  53. {
  54.    puts(txt);
  55.    closedown();
  56.    exit(0);
  57. }
  58.  
  59. void setup()
  60. {
  61.    OpenLibraries(GFXBASE|INTUITIONBASE);     /* graphics_pak.o */
  62.  
  63.    /* we want the listwindow to respond to SHIFT+key scrolling */
  64.    InitListWindowPackage();
  65.  
  66.    setup_display();
  67. }
  68.  
  69. void closedown()
  70. {
  71.    CloseListWindowPackage();     /* we initialized it, so deinitialize it */
  72.    close_display();
  73.    CloseLibraries();                         /* graphics_pak.o */
  74. }
  75.  
  76. void setup_display()
  77. {
  78.    static struct NewScreen ns;
  79.    static UWORD colortable[] = {0x0000, 0x0FFF, 0x000D, 0x0E90};
  80.    ULONG flags, IDCMPflags;
  81.  
  82.    setmem(&ns, sizeof(struct NewScreen), 0);
  83.  
  84.    ns.Width = SCREEN_WIDTH;
  85.    ns.Height = SCREEN_HEIGHT;
  86.    ns.Depth = SCREEN_DEPTH;
  87.    ns.DetailPen = 0;
  88.    ns.BlockPen = 1;
  89.    ns.ViewModes = HIRES;
  90.    ns.Type = CUSTOMSCREEN;
  91.    ns.Font = NULL;
  92.    ns.DefaultTitle = SCREEN_TITLE;
  93.  
  94.    screen = (struct Screen *)OpenScreen(&ns);
  95.    if (!screen)
  96.       abort("Can't open Custom screen.");
  97.  
  98.    LoadRGB4(&screen->ViewPort, &colortable[0], 1L<<SCREEN_DEPTH);
  99.  
  100.    setmem(&nw, sizeof(struct NewWindow), 0L);
  101.  
  102.    IDCMPflags = RAWKEY | GADGETUP | GADGETDOWN | NEWSIZE | CLOSEWINDOW;
  103.  
  104.    flags = WINDOWDRAG | WINDOWDEPTH | SMART_REFRESH | WINDOWSIZING |
  105.            WINDOWCLOSE | ACTIVATE;
  106.  
  107.    /* set up your list-window like any other window. */
  108.    nw.LeftEdge = WIN_X;
  109.    nw.TopEdge = WIN_Y;
  110.    nw.Width = WIN_W;
  111.    nw.Height = WIN_H;
  112.    nw.DetailPen = -1;
  113.    nw.BlockPen = -1;
  114.    nw.IDCMPFlags = IDCMPflags;
  115.    nw.Flags = flags;
  116.    nw.Title = "A List Window";
  117.    nw.FirstGadget = NULL;
  118.    nw.Screen = screen;
  119.    nw.Type = CUSTOMSCREEN;
  120.    nw.MinWidth = MIN_WIDTH;         /* if you need to change the size, */
  121.    nw.MinHeight = MIN_HEIGHT;       /* be sure to set these */
  122.    nw.MaxWidth = MAX_WIDTH;
  123.    nw.MaxHeight = MAX_HEIGHT;
  124.  
  125.    /* just call OpenListWindow(), just like OpenWindow() */
  126.  
  127.    listwin = (struct Window *)OpenListWindow(&nw);
  128.    if (!listwin)
  129.       abort("Can't open List Window.");
  130.  
  131.    /* Now initialize your list window  with your name list */
  132.    InitListWindow(listwin, namelist, names, 0);
  133. }
  134.  
  135. void close_display()
  136. {
  137.    if (listwin) CloseListWindow(listwin);
  138.    if (screen) CloseScreen(screen);
  139. }
  140.  
  141. void handle_input(void)
  142. {
  143.    struct IntuiMessage *message, copy;
  144.    ULONG class;
  145.    USHORT code, qualifier;
  146.    struct Gadget *gadget;
  147.    struct Window *win;
  148.    SHORT num;
  149.  
  150.    while (1)
  151.    {
  152.       WaitPort(listwin->UserPort);
  153.       while (message = (struct IntuiMessage *)GetMsg(listwin->UserPort))
  154.       {
  155.          class = message->Class;
  156.          code = message->Code;
  157.          qualifier = message->Qualifier;
  158.          win = message->IDCMPWindow;
  159.          gadget = (struct Gadget *)message->IAddress;
  160.  
  161.          /* make a copy of the message to send to the ListWindow handler */
  162.          movmem(message, ©, sizeof(struct IntuiMessage));
  163.  
  164.          ReplyMsg((struct Message *)message);
  165.  
  166.          /* check to see if the message was directed to the list-window */
  167.          if (win == listwin)
  168.          {
  169.             /* if so, pass a pointer to the copied message, and a pointer to
  170.                the buffer for a possible selected name */
  171.  
  172.             num = HandleListWindow(©, listbuffer);
  173.             switch (num)
  174.             {
  175.                case CLOSE_LISTWINDOW:  /* won't get this if no CLOSEGADGET */
  176.                   abort("");
  177.                   break;
  178.                case GOT_NAME:       /* name selected, or select-scrolled */
  179.                   printf("%s", listbuffer);
  180.  
  181.                   /* use your own routines to find the actual data accessed
  182.                      via the selected name */
  183.  
  184.                   for (num = 0; num < names; num++)
  185.                      if (strcmp(listbuffer, namelist[num]) == NULL)
  186.                         printf("  namelist[%d] = %s\n", num, namelist[num]);
  187.                   break;
  188.             }
  189.          }
  190.  
  191.          /* now handle your other windows, or special-case handling of the
  192.             list window */
  193.  
  194.          switch (class)
  195.          {
  196.             case CLOSEWINDOW:
  197.                break;
  198.             case MENUPICK:
  199.                break;
  200.             case RAWKEY:
  201.                break;
  202.          }
  203.       }
  204.    }
  205. }
  206.