home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / iv-125 / newgadgets.c < prev   
C/C++ Source or Header  |  1993-05-19  |  17KB  |  552 lines

  1. ;/* newgadgets.c -- execute me to compile me
  2. sc data=near nominc strmer streq nostkchk saveds ign=73 newgadgets
  3. slink FROM LIB:c.o newgadgets.o TO newgadgets LIB LIB:sc.lib LIB:amiga.lib
  4. quit
  5.  
  6. This code is only designed to demonstrate some of the new 3.0 GadTools
  7. features. It does not handle all of the IDCMP events and other conditions like
  8. allocating pens for the palette gadget that a good user interface should.
  9.  
  10. Note also, that it takes advantage of the SAS 6.0 C compiler.
  11.  
  12. */
  13.  
  14.  
  15. /* (c)  Copyright 1993 Commodore-Amiga, Inc.   All rights reserved. */
  16. /* The information contained herein is subject to change without    */
  17. /* notice, and is provided "as is" without warranty of any kind,    */
  18. /* either expressed or implied.  The entire risk as to the use of   */
  19. /* this information is assumed by the user.                         */
  20.  
  21.  
  22. #include <exec/types.h>
  23. #include <exec/memory.h>
  24. #include <exec/nodes.h>
  25. #include <intuition/intuition.h>
  26. #include <intuition/intuitionbase.h>
  27. #include <intuition/gadgetclass.h>
  28. #include <libraries/diskfont.h>
  29. #include <libraries/gadtools.h>
  30.  
  31. #include <clib/intuition_protos.h>
  32. #include <clib/exec_protos.h>
  33. #include <clib/dos_protos.h>
  34. #include <clib/alib_protos.h>
  35. #include <clib/diskfont_protos.h>
  36. #include <clib/graphics_protos.h>
  37. #include <clib/gadtools_protos.h>
  38.  
  39. #include <stdio.h>
  40.  
  41. #ifdef LATTICE
  42. int
  43. CXBRK (void)
  44. {
  45.   return (0);
  46. }                               /* Disable Lattice CTRL/C handling */
  47.  
  48. int
  49. chkabort (void)
  50. {
  51.   return (0);
  52. }                               /* really */
  53.  
  54. #endif
  55.  
  56. /* declare the library base pointers */
  57. extern struct IntuitionBase *IntuitionBase;
  58. extern struct Library *DiskfontBase, *GfxBase, *GadToolsBase;
  59.  
  60. /* lots of gadgets used here */
  61. struct Gadget *glist = NULL, *stringgad, *textgad, *checkgad, *mxgad, *palgad, *listgad,
  62.  *intgad, *numgad, *slid1gad, *slid2gad, *slid3gad;
  63.  
  64. /* assign the gadget ids even though we're not using them */
  65. #define MYGAD_STRING        69
  66. #define MYGAD_TEXT          70
  67. #define MYGAD_CHECK         71
  68. #define MYGAD_MX            72
  69. #define MYGAD_PAL           73
  70. #define MYGAD_LIST          74
  71. #define MYGAD_INT           75
  72. #define MYGAD_NUM           76
  73. #define MYGAD_SLIDERLEFT    77
  74. #define MYGAD_SLIDERCENTER  78
  75. #define MYGAD_SLIDERRIGHT   79
  76.  
  77. struct List *DaList;            /* the listview list */
  78.  
  79. UBYTE colors[7] =
  80. {1, 2, 15, 6, 0, 3, 8};   /* the palette colortable */
  81.  
  82.  
  83. /*
  84.  *  Create the listview list
  85.  */
  86.  
  87. BOOL
  88. CreateDaList (UBYTE * names[])
  89. {
  90.   struct Node *DaListNode;
  91.   USHORT i;
  92.   BOOL okay = TRUE;
  93.  
  94.   if (DaList = AllocMem (sizeof (struct List), MEMF_FAST))
  95.   {
  96.     NewList (DaList);
  97.  
  98.     for (i = 0; names[i] != NULL && okay == TRUE; i++)
  99.     {
  100.       if (DaListNode = AllocMem (sizeof (struct Node), MEMF_FAST))
  101.       {
  102.         DaListNode->ln_Name = names[i];
  103.         DaListNode->ln_Type = 100L;
  104.         DaListNode->ln_Pri = 0;
  105.         AddTail (DaList, DaListNode);
  106.       }
  107.       else
  108.       {
  109.         printf ("Could not allocate memory for node\n");
  110.         okay = FALSE;
  111.       }
  112.     }
  113.   }
  114.   else
  115.   {
  116.     printf ("Could not allocate memory for list\n");
  117.     okay = FALSE;
  118.   }
  119.  
  120.   return (okay);
  121. }
  122.  
  123.  
  124. /*
  125.  * Create the gadgets.
  126.  *   The gadgets are more or less positioned relative to the window size
  127.  *   and each other.  This is not extremely sophisticated, so don't expect
  128.  *   them to look good on a lores screen.
  129.  */
  130.  
  131. struct Gadget *
  132. CreateGads (struct Screen *scr, struct VisualInfo *vi,
  133.             struct TextAttr *gadfont)
  134. {
  135.   struct Gadget *gad;
  136.   struct NewGadget ng;
  137.   UBYTE *BassLines[] =
  138.   {"Always", "Fine", "Do", "Boys", "Good", NULL};       /* mx choices */
  139.   UBYTE *BassSpaces[] =
  140.   {"Grass", "Eat", "Cows", "All", NULL};        /* listview choices */
  141.  
  142.   if (gad = CreateContext (&glist))
  143.   {
  144.     /* create an MX gadget */
  145.     ng.ng_TextAttr = gadfont;
  146.     ng.ng_VisualInfo = vi;
  147.     ng.ng_LeftEdge = 80 + scr->WBorLeft;
  148.     ng.ng_TopEdge = 30 + scr->WBorTop + (scr->Font->ta_YSize + 1);
  149.     ng.ng_Width = 50;
  150.     ng.ng_Height = 30;
  151.     ng.ng_GadgetText = "Bass Lines";
  152.     ng.ng_GadgetID = MYGAD_MX;
  153.     ng.ng_Flags = 0;
  154.     mxgad = gad = CreateGadget (MX_KIND, gad, &ng,
  155.                                 GTMX_TitlePlace, PLACETEXT_ABOVE,
  156.                                 GTMX_Labels, BassLines, GTMX_Scaled, TRUE,
  157.                                 GTMX_Spacing, gadfont->ta_YSize + 1, TAG_END);
  158.     /* create a slider gadget */
  159.     ng.ng_TopEdge = scr->Height - 170;
  160.     ng.ng_Height = 10;
  161.     ng.ng_Width = 100;
  162.     ng.ng_GadgetText = "Left Justified";
  163.     ng.ng_GadgetID = MYGAD_SLIDERLEFT;
  164.     ng.ng_Flags = PLACETEXT_ABOVE;
  165.     slid1gad = gad = CreateGadget (SLIDER_KIND, gad, &ng,
  166.                                    GTSL_MaxLevelLen, 6,
  167.                                    GTSL_MaxPixelLen, 64,
  168.                                    GTSL_Max, 512,
  169.                                    GTSL_Justification, GTJ_LEFT,
  170.                                    GTSL_LevelFormat, "%lx",
  171.                                    GTSL_LevelPlace, PLACETEXT_BELOW,
  172.                                    TAG_END);
  173.  
  174.     /* create a slider gadget */
  175.     ng.ng_TopEdge = ng.ng_TopEdge + 55;
  176.     ng.ng_GadgetText = "Centered";
  177.     ng.ng_GadgetID = MYGAD_SLIDERCENTER;
  178.     slid2gad = gad = CreateGadget (SLIDER_KIND, gad, &ng,
  179.                                    GTSL_MaxLevelLen, 6,
  180.                                    GTSL_MaxPixelLen, 64,
  181.                                    GTSL_Max, 512,
  182.                                    GTSL_LevelFormat, "%lx",
  183.                                    GTSL_Justification, GTJ_CENTER,
  184.                                    GTSL_LevelPlace, PLACETEXT_BELOW,
  185.                                    TAG_END);
  186.  
  187.     /* create a slider gadget */
  188.     ng.ng_TopEdge = ng.ng_TopEdge + 55;
  189.     ng.ng_GadgetText = "Right Justified";
  190.     ng.ng_GadgetID = MYGAD_SLIDERRIGHT;
  191.     slid3gad = gad = CreateGadget (SLIDER_KIND, gad, &ng,
  192.                                    GTSL_MaxLevelLen, 6,
  193.                                    GTSL_MaxPixelLen, 64,
  194.                                    GTSL_Max, 512,
  195.                                    GTSL_LevelFormat, "%lx",
  196.                                    GTSL_Justification, GTJ_RIGHT,
  197.                                    GTSL_LevelPlace, PLACETEXT_BELOW,
  198.                                    TAG_END);
  199.  
  200.     /* Set these in case the listview cannot be created */
  201.     ng.ng_LeftEdge = 240 + scr->WBorLeft;
  202.     ng.ng_TopEdge = 30 + scr->WBorTop + (scr->Font->ta_YSize + 1);
  203.  
  204.     if (CreateDaList (BassSpaces))
  205.     {
  206.       /* create a listview gadget */
  207.       ng.ng_LeftEdge = 240 + scr->WBorLeft;
  208.       ng.ng_TopEdge = 30 + scr->WBorTop + (scr->Font->ta_YSize + 1);
  209.       ng.ng_Width = 100;
  210.       ng.ng_Height = 30 + gadfont->ta_YSize + 14;
  211.       ng.ng_GadgetText = "Bass Spaces";
  212.       ng.ng_GadgetID = MYGAD_LIST;
  213.       listgad = gad = CreateGadget (LISTVIEW_KIND, gad, &ng,
  214.                                     GTLV_ShowSelected, NULL,
  215.                                     GTLV_Selected, 2,
  216.                                     GTLV_MakeVisible, 2,
  217.                                     GTLV_Labels, DaList,
  218.                                     TAG_END);
  219.     }
  220.  
  221.     /* create a checkbox gadget */
  222.     ng.ng_LeftEdge = ng.ng_LeftEdge + 30;
  223.     ng.ng_TopEdge = ng.ng_TopEdge + 130;
  224.     ng.ng_Width = 50;
  225.     ng.ng_Height = 30;
  226.     ng.ng_GadgetText = "BigCheck";
  227.     ng.ng_GadgetID = MYGAD_TEXT;
  228.     ng.ng_Flags = PLACETEXT_ABOVE;
  229.     checkgad = gad = CreateGadget (CHECKBOX_KIND, gad, &ng,
  230.                                    GTCB_Scaled, TRUE, TAG_END);
  231.  
  232.  
  233.     /* create a string gadget */
  234.     ng.ng_LeftEdge = scr->Width - scr->WBorRight - 190;
  235.     ng.ng_TopEdge = ng.ng_TopEdge - 130;
  236.     ng.ng_Width = 150;
  237.     ng.ng_Height = gadfont->ta_YSize + 14;
  238.     ng.ng_GadgetText = "String Immediate";
  239.     ng.ng_GadgetID = MYGAD_STRING;
  240.     stringgad = gad = CreateGadget (STRING_KIND, gad, &ng,
  241.                                     GA_Immediate, TRUE, TAG_END);
  242.  
  243.     /* create a text gadget */
  244.     ng.ng_TopEdge = ng.ng_TopEdge + 55;
  245.     ng.ng_Height = gadfont->ta_YSize + 2;
  246.     ng.ng_GadgetText = "Echo After Click";
  247.     ng.ng_GadgetID = MYGAD_TEXT;
  248.     textgad = gad = CreateGadget (TEXT_KIND, gad, &ng,
  249.                                   GTTX_Border, TRUE,
  250.                                   GTTX_FrontPen, 3,
  251.                                   GTTX_BackPen, 2,
  252.                                   GTTX_Clipped, TRUE,
  253.                                   TAG_END);
  254.  
  255.     /* create a palette gadget */
  256.     ng.ng_TopEdge = ng.ng_TopEdge + 80;
  257.     ng.ng_Width = 100;
  258.     ng.ng_Height = 30;
  259.     ng.ng_GadgetText = "Odd Colors";
  260.     ng.ng_GadgetID = MYGAD_PAL;
  261.     palgad = gad = CreateGadget (PALETTE_KIND, gad, &ng,
  262.                                  GTPA_ColorTable, colors,
  263.                                  GTPA_NumColors, 7,
  264.                                  GTPA_Color, 2,
  265.                                  GTPA_IndicatorWidth, 21,
  266.                                  TAG_END);
  267.  
  268.     /* create an integer gadget */
  269.     ng.ng_Width = 150;
  270.     ng.ng_LeftEdge = scr->Width + scr->WBorRight - 190;
  271.     ng.ng_TopEdge = scr->Height - 170;
  272.     ng.ng_Height = gadfont->ta_YSize + 14;
  273.     ng.ng_GadgetText = "Number, please";
  274.     ng.ng_GadgetID = MYGAD_INT;
  275.     intgad = gad = CreateGadget (INTEGER_KIND, gad, &ng,
  276.                                  GTIN_MaxChars, 14,
  277.                                  TAG_END);
  278.  
  279.     /* create a number gadget */
  280.     ng.ng_TopEdge = ng.ng_TopEdge + 60;
  281.     ng.ng_Width = 100;
  282.     ng.ng_Height = gadfont->ta_YSize + 2;
  283.     ng.ng_GadgetText = "Echo Number";
  284.     ng.ng_GadgetID = MYGAD_NUM;
  285.     numgad = gad = CreateGadget (NUMBER_KIND, gad, &ng,
  286.                                  GTNM_FrontPen, 3,
  287.                                  GTNM_Format, "%04ld",
  288.                                  GTNM_Clipped, TRUE,
  289.                                  GTNM_Number, 0xffff1,
  290.                                  TAG_END);
  291.   }
  292.   else
  293.     printf ("Could not create context\n");
  294.  
  295.   return (gad);
  296. }
  297.  
  298. /*
  299.  *  Create Menu.
  300.  */
  301.  
  302. struct Menu *
  303. CreateDaMenu (struct NewMenu *themenu, struct VisualInfo *vi)
  304. {
  305.   struct Menu *menusready;
  306.  
  307.   if (menusready = CreateMenus (themenu, TAG_END))
  308.     LayoutMenus (menusready, vi, GTMN_NewLookMenus, TRUE, TAG_END);
  309.   else
  310.     printf ("Could not create menus\n");
  311.  
  312.   return (menusready);
  313. }
  314.  
  315.  
  316. /*
  317.  *  Process menu events.
  318.  */
  319.  
  320. BOOL
  321. ProcessDaMenu (USHORT menunumber, struct Window * win, struct Menu * menustrip,
  322.                struct VisualInfo * vi)
  323. {
  324.   USHORT menunum, itemnum, subnum;
  325.   BOOL closeit = FALSE;
  326.   menunum = MENUNUM (menunumber);
  327.   itemnum = ITEMNUM (menunumber);
  328.   subnum = SUBNUM (menunumber);
  329.  
  330.   switch (menunum)
  331.   {
  332.   case 0:                       /* project menu */
  333.     closeit = TRUE;
  334.     break;
  335.   case 1:                       /* justify text menu */
  336.     switch (itemnum)
  337.     {
  338.     case 0:                     /* left justify string */
  339.       GT_SetGadgetAttrs (textgad, win, NULL,
  340.                          GTTX_Justification, GTJ_LEFT,
  341.                          GTTX_Text, "Left", TAG_END);
  342.       break;
  343.     case 2:                     /* center justify string */
  344.       GT_SetGadgetAttrs (textgad, win, NULL,
  345.                          GTTX_Justification, GTJ_CENTER,
  346.                          GTTX_Text, "Center", TAG_END);
  347.       break;
  348.     case 4:                     /* right justify string */
  349.       GT_SetGadgetAttrs (textgad, win, NULL,
  350.                          GTTX_Justification, GTJ_RIGHT,
  351.                          GTTX_Text, "Right", TAG_END);
  352.     }
  353.   }
  354.  
  355.   return (closeit);
  356. }
  357.  
  358.  
  359. /*
  360.  *  Process IDCMP events
  361.  *    Again, this is very incomplete.  You would do a lot more if this were a
  362.  *    real application.
  363.  */
  364.  
  365. void
  366. ProcessEvents (struct Window *win, struct Menu *menustrip, struct VisualInfo *vi)
  367. {
  368.   struct IntuiMessage *msg;
  369.   ULONG msgclass;
  370.   LONG num;
  371.   BOOL Closeflag = FALSE;
  372.   STRPTR holdstring;
  373.   USHORT menunumber;
  374.  
  375.   while (!Closeflag)
  376.   {
  377.     Wait (1 << win->UserPort->mp_SigBit);
  378.  
  379.     while ((!Closeflag) && (msg = GT_GetIMsg (win->UserPort)))
  380.     {
  381.       msgclass = msg->Class;
  382.  
  383.       switch (msgclass)
  384.       {
  385.       case IDCMP_CLOSEWINDOW:
  386.         Closeflag = TRUE;
  387.         break;
  388.  
  389.       case IDCMP_GADGETDOWN:
  390.         if (msg->IAddress == stringgad)
  391.         {
  392.           holdstring = ((struct StringInfo *) (stringgad->SpecialInfo))->Buffer;
  393.           GT_SetGadgetAttrs (textgad, win, NULL,
  394.                              GTTX_Text, holdstring, TAG_END);
  395.         }
  396.         break;
  397.  
  398.       case IDCMP_GADGETUP:
  399.         if (msg->IAddress == intgad)
  400.         {
  401.           num = ((struct StringInfo *) (intgad->SpecialInfo))->LongInt;
  402.           GT_SetGadgetAttrs (numgad, win, NULL, GTNM_Number, num,
  403.                              GTNM_Justification, GTJ_RIGHT, TAG_END);
  404.         }
  405.         break;
  406.  
  407.       case IDCMP_MENUPICK:
  408.         menunumber = msg->Code;
  409.         while (menunumber != MENUNULL && !Closeflag)
  410.         {
  411.           Closeflag = ProcessDaMenu (menunumber, win, menustrip, vi);
  412.           menunumber = (ItemAddress (menustrip, menunumber))->NextSelect;
  413.         }
  414.       }
  415.       GT_ReplyIMsg (msg);
  416.     }
  417.   }
  418. }
  419.  
  420. /*
  421.  *  Free the memory used for the listview.
  422.  */
  423.  
  424. void
  425. FreeDaMemory (void)
  426. {
  427.   struct Node *freenode, *nextnode;
  428.  
  429.   freenode = (struct Node *) DaList->lh_Head;
  430.   while (nextnode = (struct Node *) freenode->ln_Succ)
  431.   {
  432.     FreeMem (freenode, sizeof (struct Node));
  433.     freenode = nextnode;
  434.   }
  435.   FreeMem (DaList, sizeof (struct List));
  436. }
  437.  
  438.  
  439. /*
  440.  *  main().
  441.  *    With SAS 6.0, you don't have to open libraries, so we can get straight
  442.  *    to work.
  443.  */
  444.  
  445. void
  446. main (void)
  447. {
  448.   struct Screen *Gadscreen;
  449.   struct Window *Gadwindow;
  450.   struct VisualInfo *vi;
  451.   struct TextFont *Appfont;
  452.   struct Menu *menuptr;
  453.  
  454.   /* use a font you like */
  455.   struct TextAttr nicefont =
  456.   {
  457.     "diamond.font",             /*  STRPTR  ta_Name     name of the font */
  458.     15,                         /*  UWORD   ta_YSize    height of the font */
  459.     FS_NORMAL,                  /*  UBYTE   ta_Style    intrinsic font style */
  460.     FPF_DISKFONT                /*  UBYTE   ta_Flags    font preferences and flags */
  461.   };
  462.  
  463.   struct NewMenu Gadmenu[] =
  464.   {
  465.     {NM_TITLE, "Project", 0, 0, 0, 0,},
  466.     {NM_ITEM, "Quit", "Q", 0, 0, 0,},
  467.  
  468.     {NM_TITLE, "Justify Text", 0, 0, 0, 0,},
  469.     {NM_ITEM, "Left", "L", 0, 0, 0,},
  470.     {NM_ITEM, NM_BARLABEL, 0, 0, 0, 0,},
  471.     {NM_ITEM, "Center", "M", 0, 0, 0,},
  472.     {NM_ITEM, NM_BARLABEL, 0, 0, 0, 0,},
  473.     {NM_ITEM, "Right", "R", 0, 0, 0,},
  474.     {NM_END, NULL, 0, 0, 0, 0,},
  475.   };
  476.  
  477.   if (Appfont = OpenDiskFont (&nicefont))
  478.   {
  479.     if (Gadscreen = OpenScreenTags (NULL,
  480.                                     SA_Left, 0,
  481.                                     SA_Top, 0,
  482.                                     SA_LikeWorkbench, TRUE,
  483.                                     SA_Font, &nicefont,
  484.                                     SA_Title, "Some New GadTools Features",
  485.                                     TAG_DONE))
  486.     {
  487.       if ((vi = GetVisualInfo (Gadscreen, TAG_END)) != NULL)
  488.       {
  489.         if (CreateGads (Gadscreen, vi, &nicefont) != NULL)
  490.         {
  491.           if (menuptr = CreateDaMenu (Gadmenu, vi))
  492.           {
  493.             if (Gadwindow = OpenWindowTags (NULL,
  494.                                             WA_Left, Gadscreen->LeftEdge,
  495.                                     WA_Top, Gadscreen->TopEdge + Gadscreen->BarHeight,
  496.                                   WA_Height, Gadscreen->Height - Gadscreen->BarHeight,
  497.                                             WA_Width, Gadscreen->Width,
  498.                                 WA_MinWidth, Gadscreen->Height - Gadscreen->BarHeight,
  499.                                             WA_MinHeight, Gadscreen->Width,
  500.                                             WA_Gadgets, glist,
  501.                                             WA_Title, "Lots of Gadgets",
  502.                                             WA_CustomScreen, Gadscreen,
  503.                                     WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_ACTIVEWINDOW |
  504.                                             IDCMP_GADGETDOWN | IDCMP_MENUPICK |
  505.                                       IDCMP_GADGETHELP | IDCMP_GADGETUP | SLIDERIDCMP,
  506.                                        WA_Flags, WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  507.                                             WFLG_ACTIVATE,
  508.                                             WA_NewLookMenus, TRUE,
  509.                                             WA_DragBar, TRUE,
  510.                                             WA_CloseGadget, TRUE,
  511.                                             TAG_DONE))
  512.             {
  513.               if (SetMenuStrip (Gadwindow, menuptr))
  514.               {
  515.                 GT_RefreshWindow (Gadwindow, NULL);
  516.  
  517.                 ProcessEvents (Gadwindow, menuptr, vi);
  518.  
  519.                 ClearMenuStrip (Gadwindow);
  520.  
  521.                 FreeMenus (menuptr);
  522.  
  523.                 CloseWindow (Gadwindow);
  524.               }
  525.             }
  526.           }
  527.         }
  528.         else
  529.           printf ("Could not create gadgets\n");
  530.  
  531.         if (DaList)
  532.           FreeDaMemory ();
  533.  
  534.         FreeGadgets (glist);
  535.  
  536.         FreeVisualInfo (vi);
  537.       }
  538.  
  539.       else
  540.         printf ("Could not get visual info\n");
  541.  
  542.       CloseScreen (Gadscreen);
  543.     }
  544.     else
  545.       printf ("Could not open screen\n");
  546.  
  547.     CloseFont (Appfont);
  548.   }
  549.   else
  550.     printf ("Could not open %s\n", nicefont.ta_Name);
  551. }
  552.