home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / CDTools / ClassAct / Examples / Palette / PaletteExample.c < prev    next >
C/C++ Source or Header  |  1997-07-09  |  4KB  |  163 lines

  1.  
  2. /**
  3.  **  PaletteExample.c -- Palette class example.
  4.  **
  5.  **  This is a simple example testing some of the capabilities of the
  6.  **  Palette gadget class.
  7.  **
  8.  **  This code opens a simple window and then creates a Palette gadget.
  9.  **
  10.  **  Note that we are not using window or layout class here, we are 
  11.  **  using the gadget in a fairly direct form, but that's perfectly legal.
  12.  **
  13.  **/
  14.  
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <intuition/intuition.h>
  19. #include <intuition/gadgetclass.h>
  20. #include <libraries/gadtools.h>
  21. #include <utility/tagitem.h>
  22. #include <clib/dos_protos.h>
  23. #include <clib/exec_protos.h>
  24. #include <clib/intuition_protos.h>
  25. #include <clib/gadtools_protos.h>
  26. #include <gadgets/palette.h>
  27. #include <proto/palette.h>
  28. #include <stdio.h>
  29.  
  30. /* Function prototypes.
  31.  */
  32. struct ClassLibrary * OpenClass(STRPTR, ULONG);
  33.  
  34. /* Global variables.
  35.  */
  36. struct ClassLibrary *PaletteBase;
  37. struct Gadget *palette_gad;
  38.  
  39.  
  40. /* This is the start of our programme.
  41.  */
  42. main()
  43. {
  44.     struct Screen *screen = NULL;
  45.  
  46.     /* We'll just open up on the Workbench screen, and use its screen font.
  47.      */
  48.     if (screen = LockPubScreen("Workbench"))
  49.     {
  50.         struct Window *win = NULL;
  51.  
  52.         /* Open the window, note how we size the window to perfectly fit
  53.          * all the gadgets.
  54.          */
  55.         if (win = OpenWindowTags(NULL,
  56.             WA_Left, 0,
  57.             WA_Top, screen->Font->ta_YSize + 3,
  58.             WA_Width, 200,
  59.             WA_Height, (screen->WBorTop) + 5 + screen->Font->ta_YSize + 100,
  60.             WA_IDCMP, IDCMP_GADGETUP | IDCMP_REFRESHWINDOW |
  61.                         IDCMP_CLOSEWINDOW | IDCMP_GADGETDOWN,
  62.             WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  63.                         WFLG_SIZEGADGET | WFLG_ACTIVATE | WFLG_SMART_REFRESH,
  64.             WA_Title, "Palette Demo",
  65.             WA_MinWidth, 60,
  66.             WA_MinHeight, (screen->WBorTop) + 5 + screen->Font->ta_YSize + 60,
  67.             WA_MaxWidth, -1,
  68.             WA_MaxHeight, -1,
  69.             TAG_DONE))
  70.         {
  71.             PutStr("Creating Palette class\n");
  72.             if (PaletteBase = OpenClass("gadgets/palette.gadget", 0))
  73.             {
  74.                 PutStr("Creating Palette gadget 1\n");
  75.                 if (palette_gad = (struct Gadget *)NewObject(PALETTE_GetClass(), NULL,
  76.                                                         GA_ID, 2,
  77.                                                         GA_Top, (win->BorderTop) + 5,
  78.                                                         GA_Left, 10,
  79.                                                         GA_RelWidth, -36,
  80.                                                         GA_RelHeight, -(win->BorderTop + win->BorderBottom + 10),
  81.                                                         GA_RelVerify, TRUE,
  82.                                                         PALETTE_NumColours, 1 << screen->RastPort.BitMap->Depth,
  83.                                                         TAG_END))
  84.                 {
  85.                     struct IntuiMessage *imsg;
  86.                     BOOL ok = TRUE;
  87.         
  88.                     AddGList(win, palette_gad, -1, -1, NULL);
  89.                     RefreshGList(palette_gad, win, NULL, -1);
  90.  
  91.                     /* Just wait around until the close gadget is pressed.
  92.                      */
  93.                     while (ok)
  94.                     {
  95.                         struct Gadget *gadget;
  96.         
  97.                         WaitPort(win->UserPort);
  98.                         while (imsg = (struct IntuiMessage *)GetMsg(win->UserPort))
  99.                         {
  100.                             switch(imsg->Class)
  101.                             {
  102.                                 case IDCMP_CLOSEWINDOW:
  103.                                     ok = FALSE;
  104.                                     break;
  105.         
  106.                                 case IDCMP_GADGETUP:
  107.                                     gadget = (struct Gadget *)imsg->IAddress;
  108.                                     Printf("Gadget: %ld  Code: %ld\n",
  109.                                     (LONG)gadget->GadgetID, (LONG)imsg->Code );
  110.         
  111.                                     break;
  112.         
  113.                                 default:
  114.                                     break;
  115.                             }
  116.                             ReplyMsg((struct Message *)imsg);
  117.                         }
  118.                     }
  119.                     RemoveGList(win, palette_gad, -1);
  120.                     DisposeObject(palette_gad);
  121.                 }
  122.                 else
  123.                     PutStr("ERROR: Couldn't create Palette gadget\n");
  124.  
  125.                 /* Free the class.
  126.                  */
  127.                 PutStr("Freeing Palette class\n");
  128.                 CloseLibrary((struct Library *)PaletteBase);
  129.             }
  130.             else
  131.                 PutStr("ERROR: Couldn't create Palette class\n");
  132.  
  133.             CloseWindow(win);
  134.         }
  135.         else
  136.             PutStr("ERROR: Couldn't open window\n");
  137.  
  138.         UnlockPubScreen(0, screen);
  139.     }
  140.     else
  141.         PutStr("ERROR: Couldn't lock public screen\n");
  142. }
  143.  
  144.  
  145. /* Open a class library.
  146.  */
  147. struct ClassLibrary * OpenClass(STRPTR name, ULONG version)
  148. {
  149.     struct Library *retval;
  150.     UBYTE buffer[256];
  151.  
  152.     if ((retval = OpenLibrary(name, version)) == NULL)
  153.     {
  154.         sprintf (buffer, ":classes/%s", name);
  155.         if ((retval = OpenLibrary(buffer, version)) == NULL)
  156.         {
  157.             sprintf(buffer, "classes/%s", name);
  158.             retval = OpenLibrary(buffer, version);
  159.         }
  160.     }
  161.     return((struct ClassLibrary *)retval);
  162. }
  163.