home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / BOOPSI / GI1 / Examples / Calendar_Test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  8.4 KB  |  299 lines

  1. /*****************************************************************************
  2.  *
  3.  * COPYRIGHT: Unless otherwise noted, all files are Copyright (c) 1992-1999
  4.  * Amiga, Inc. All rights reserved.
  5.  *
  6.  * DISCLAIMER: This software is provided "as is".  No representations or
  7.  * warranties are made with respect to the accuracy, reliability,
  8.  * performance, currentness, or operation of this software, and all use is at
  9.  * your own risk. Neither Amiga nor the authors assume any responsibility
  10.  * or liability whatsoever with respect to your use of this software.
  11.  *
  12.  *****************************************************************************
  13.  * calendar_test.c
  14.  * test program for the calendar.gadget
  15.  * Written by David N. Junod
  16.  *
  17.  */
  18.  
  19. #include <dos/dos.h>
  20. #include <dos/rdargs.h>
  21. #include <exec/types.h>
  22. #include <exec/libraries.h>
  23. #include <intuition/classes.h>
  24. #include <intuition/icclass.h>
  25. #include <intuition/intuition.h>
  26. #include <intuition/gadgetclass.h>
  27. #include <intuition/intuitionbase.h>
  28. #include <gadgets/calendar.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31.  
  32. #include <clib/macros.h>
  33. #include <clib/dos_protos.h>
  34. #include <clib/exec_protos.h>
  35. #include <clib/intuition_protos.h>
  36. #include <clib/utility_protos.h>
  37.  
  38. #include <pragmas/dos_pragmas.h>
  39. #include <pragmas/exec_pragmas.h>
  40. #include <pragmas/intuition_pragmas.h>
  41. #include <pragmas/utility_pragmas.h>
  42.  
  43. /*****************************************************************************/
  44.  
  45. #define    IDCMP_FLAGS    IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY | IDCMP_GADGETUP \
  46.             | IDCMP_MOUSEMOVE | IDCMP_INTUITICKS | IDCMP_MOUSEBUTTONS \
  47.             | IDCMP_IDCMPUPDATE
  48.  
  49. /*****************************************************************************/
  50.  
  51. extern struct Library *SysBase, *DOSBase, *UtilityBase;
  52. struct Library *IntuitionBase;
  53.  
  54. /*****************************************************************************/
  55.  
  56. #define    TEMPLATE    "READONLY/S,MULTISELECT/S,LABELS/S,NOLABEL/S"
  57. #define    OPT_READONLY    0
  58. #define    OPT_MULTI    1
  59. #define    OPT_LABELS    2
  60. #define    OPT_LABEL    3
  61. #define    OPT_MAX        4
  62.  
  63. struct RDArgs *ra;
  64. LONG opts[OPT_MAX];
  65.  
  66. /*****************************************************************************/
  67.  
  68. struct ClassLibrary *openclass (STRPTR name, ULONG version);
  69. LONG DoClassLibraryMethod (struct ClassLibrary *cl, Msg msg);
  70.  
  71. /*****************************************************************************/
  72.  
  73. void main (int argc, char **argv)
  74. {
  75.     struct IntuiMessage *imsg;
  76.     struct ClassLibrary *cl;
  77.     struct gpDomain gpd;
  78.     struct Screen *scr;
  79.     struct Window *win;
  80.     struct Gadget *gad;
  81.     struct Gadget *g;
  82.     BOOL going = FALSE;
  83.     ULONG mw, w;
  84.     ULONG mh, h;
  85.     ULONG sigr;
  86.     ULONG day;
  87.  
  88.     DayLabel dlArray[31];
  89.     DayLabelP dl;
  90.     LONG i, j;
  91.     struct TagItem ti[2];
  92.  
  93.     /* Make sure that we have at least V2.04 */
  94.     if (IntuitionBase = OpenLibrary ("intuition.library", 37))
  95.     {
  96.     /* Get our arguments from the command line */
  97.     if (ra = ReadArgs (TEMPLATE, opts, NULL))
  98.     {
  99.         /* See if they tried to get away */
  100.         if (!((SIGBREAKF_CTRL_C & CheckSignal (SIGBREAKF_CTRL_C))))
  101.         going = TRUE;
  102.     }
  103.     else
  104.         PrintFault (IoErr (), NULL);
  105.  
  106.     if (going == FALSE)
  107.     {
  108.         /* Can't really go on with it all */
  109.     }
  110.     else if (cl = openclass ("gadgets/calendar.gadget", 37))
  111.     {
  112.         /* Clear the array */
  113.         for (i = 0; i < 31; i++)
  114.         for (j = 0; j < MAX_DL_PENS; j++)
  115.             dlArray[i].dl_Pens[j] = -1;
  116.  
  117.         /* Set a few of our favorite dates */
  118.         dl = &dlArray[0];
  119.         dl->dl_Flags = DLF_SELECTED;
  120.         dl = &dlArray[6];
  121.         dl->dl_Flags = DLF_DISABLED;
  122.         dl = &dlArray[12];
  123.         dl->dl_Pens[DL_TEXTPEN] = 1;
  124.         dl->dl_Pens[DL_BACKGROUNDPEN] = 3;
  125.         dl = &dlArray[19];
  126.         dl->dl_Pens[DL_TEXTPEN] = 2;
  127.         dl->dl_Pens[DL_BACKGROUNDPEN] = 4;
  128.  
  129.         /* Illegal but easy way to TEST on any screen.
  130.          * DO NOT DO THIS IN PRODUCTION CODE. */
  131.         scr = ((struct IntuitionBase *)IntuitionBase)->FirstScreen;
  132.  
  133.         /* Get the domain of the object */
  134.         gpd.MethodID  = GM_DOMAIN;
  135.         gpd.gpd_GInfo = NULL;
  136.         gpd.gpd_RPort = &scr->RastPort;
  137.         gpd.gpd_Which = GDOMAIN_MINIMUM;
  138.         ti[0].ti_Tag  = CALENDAR_Label;
  139.         ti[0].ti_Data = (opts[OPT_LABEL] ? FALSE : TRUE);
  140.         ti[1].ti_Tag  = TAG_DONE;
  141.         gpd.gpd_Attrs = ti;
  142.         if (DoClassLibraryMethod (cl, (Msg)&gpd) == 1)
  143.         {
  144.         mw = gpd.gpd_Domain.Width;
  145.         mh = gpd.gpd_Domain.Height;
  146.         Printf ("class said we have a minimum size of w=%ld and h=%ld\n", (ULONG)mw, (ULONG)mh);
  147.         }
  148.         else
  149.         {
  150.         Printf ("class had no clue as to the size to be, so we'll do it ourself!\n");
  151.  
  152.         /* Compute the size that we want to be */
  153.         mw = ((scr->RastPort.TxWidth * 3) * 7) + (6 * 2);
  154.         mh = (scr->RastPort.TxHeight * 7) + (6 * 1);
  155.         }
  156.         w = MAX (320, mw);
  157.         h = MAX (100, mh);
  158.  
  159.         /* Create the destination window */
  160.         if (win = OpenWindowTags (NULL,
  161.                       WA_Title,        "calendar.gadget Test",
  162.                       WA_InnerWidth,    w + 4,
  163.                       WA_InnerHeight,    h + 2,
  164.                       WA_IDCMP,        IDCMP_FLAGS,
  165.                       WA_Activate,    TRUE,
  166.                       WA_DragBar,    TRUE,
  167.                       WA_DepthGadget,    TRUE,
  168.                       WA_CloseGadget,    TRUE,
  169.                       WA_SimpleRefresh,    TRUE,
  170.                       WA_NoCareRefresh,    TRUE,
  171.                       WA_SizeGadget,    TRUE,
  172.                       WA_SizeBBottom,    TRUE,
  173.                       WA_AutoAdjust,    TRUE,
  174.                       WA_CustomScreen,    scr,
  175.                       WA_MinWidth,    8 + 4 + mw,
  176.                       WA_MinHeight,    scr->BarHeight + 10 + 2 + mh,
  177.                       WA_MaxWidth,    1024,
  178.                       WA_MaxHeight,    1024,
  179.                       TAG_DONE))
  180.         {
  181.         /* Create the gadget */
  182.         if (gad = NewObject (NULL, "calendar.gadget",
  183.                     GA_Left,        win->BorderLeft + 2,
  184.                     GA_Top,            win->BorderTop + 1,
  185.                     GA_RelWidth,        -(win->BorderLeft + win->BorderRight + 4),
  186.                     GA_RelHeight,        -(win->BorderTop + win->BorderBottom + 2),
  187.                     GA_TextAttr,        scr->Font,
  188.                     GA_RelVerify,        TRUE,
  189.                     GA_Immediate,        TRUE,
  190.                     GA_ReadOnly,        opts[OPT_READONLY],
  191.                     CALENDAR_Multiselect,    opts[OPT_MULTI],
  192.                     CALENDAR_Label,        (opts[OPT_LABEL] ? FALSE : TRUE),
  193.                     CALENDAR_Labels,    (opts[OPT_LABELS] ? dlArray : NULL),
  194.                     ICA_TARGET,        ICTARGET_IDCMP,
  195.                     TAG_DONE))
  196.         {
  197.             /* Add the gadget */
  198.             AddGList (win, gad, -1, 1, NULL);
  199.             RefreshGList (gad, win, NULL, -1);
  200.  
  201.             while (going)
  202.             {
  203.             sigr = Wait ((1L << win->UserPort->mp_SigBit | SIGBREAKF_CTRL_C));
  204.  
  205.             if (sigr & SIGBREAKF_CTRL_C)
  206.                 going = FALSE;
  207.  
  208.             while (imsg = (struct IntuiMessage *) GetMsg (win->UserPort))
  209.             {
  210.                 switch (imsg->Class)
  211.                 {
  212.                 case IDCMP_CLOSEWINDOW:
  213.                     going = FALSE;
  214.                     break;
  215.  
  216.                 case IDCMP_IDCMPUPDATE:
  217.                     day = GetTagData (CALENDAR_Day, 99, (struct TagItem *) imsg->IAddress);
  218.                     Printf ("%s %ld\n", ((day & 0x100) ? "clear" : "set"), (day & 0xFF));
  219.                     break;
  220.  
  221.                 case IDCMP_VANILLAKEY:
  222.                     switch (imsg->Code)
  223.                     {
  224.                     case 'd':
  225.                     case 'D':
  226.                         SetGadgetAttrs (gad, win, NULL,
  227.                                 GA_Disabled, ((gad->Flags & GFLG_DISABLED) ? 0 : 1),
  228.                                 TAG_DONE);
  229.                         break;
  230.  
  231.                     case  27:
  232.                     case 'q':
  233.                     case 'Q':
  234.                         going = FALSE;
  235.                         break;
  236.                     }
  237.                     break;
  238.  
  239.                 case IDCMP_GADGETUP:
  240.                     g = (struct Gadget *) imsg->IAddress;
  241.                     Printf ("id=%ld code=%ld\n", (ULONG)g->GadgetID, (ULONG)imsg->Code);
  242.                     break;
  243.                 }
  244.  
  245.                 ReplyMsg ((struct Message *) imsg);
  246.             }
  247.             }
  248.  
  249.             /* Delete the gadget */
  250.             RemoveGList (win, gad, 1);
  251.             DisposeObject (gad);
  252.         }
  253.  
  254.         CloseWindow (win);
  255.         }
  256.         else
  257.         Printf ("couldn't open the window\n");
  258.  
  259.         CloseLibrary ((struct Library *) cl);
  260.     }
  261.     else
  262.         Printf ("couldn't open classes:gadgets/calendar.gadget\n");
  263.  
  264.     FreeArgs (ra);
  265.  
  266.     CloseLibrary (IntuitionBase);
  267.     }
  268. }
  269.  
  270. /*****************************************************************************/
  271.  
  272. /* Try opening the class library from a number of common places */
  273. struct ClassLibrary *openclass (STRPTR name, ULONG version)
  274. {
  275.     struct ExecBase *SysBase = (*((struct ExecBase **) 4));
  276.     struct Library *retval;
  277.     UBYTE buffer[256];
  278.  
  279.     if ((retval = OpenLibrary (name, version)) == NULL)
  280.     {
  281.     sprintf (buffer, ":classes/%s", name);
  282.     if ((retval = OpenLibrary (buffer, version)) == NULL)
  283.     {
  284.         sprintf (buffer, "classes/%s", name);
  285.         retval = OpenLibrary (buffer, version);
  286.     }
  287.     }
  288.     return (struct ClassLibrary *) retval;
  289. }
  290.  
  291. /*****************************************************************************/
  292.  
  293. LONG DoClassLibraryMethod (struct ClassLibrary *cl, Msg msg)
  294. {
  295.     LONG (__asm *disp)(register __a0 Class *, register __a2 Object *, register __a1 Msg msg);
  296.     disp = cl->cl_Class->cl_Dispatcher.h_Entry;
  297.     return (*disp)(cl->cl_Class, (Object *) cl->cl_Class, msg);
  298. }
  299.