home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / iv-59 / adc.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  21KB  |  614 lines

  1. ;/*
  2. sc link ignore=73 adc.c
  3. quit
  4.  
  5. Copyright (c) 1991 Commodore-Amiga, Inc.
  6.  
  7. This example is provided in electronic form by Commodore-Amiga,
  8. Inc. for use with the Amiga Mail Volume II technical publication.
  9. Amiga Mail Volume II contains additional information on the correct
  10. usage of the techniques and operating system functions presented in
  11. these examples.  The source and executable code of these examples may
  12. only be distributed in free electronic form, via bulletin board or
  13. as part of a fully non-commercial and freely redistributable
  14. diskette.  Both the source and executable code (including comments)
  15. must be included, without modification, in any copy.  This example
  16. may not be published in printed form or distributed with any
  17. commercial product. However, the programming techniques and support
  18. routines set forth in these examples may be used in the development
  19. of original executable software products for Commodore Amiga
  20. computers.
  21.  
  22. All other rights reserved.
  23.  
  24. This example is provided "as-is" and is subject to change; no
  25. warranties are made.  All use is at your own risk. No liability or
  26. responsibility is assumed.
  27. */
  28.  
  29.  
  30. /****** AppWorkbench.demo/ADC *************************************************
  31.  
  32.     NAME
  33.         ADC -- AppWorkbench - Complete and contained demo of how to use
  34.                the AppWorkbench Interface.
  35.  
  36.     AUTHOR
  37.         Fred Mitchell, Copyright © 1991 Commodore-Amiga, Inc.
  38.  
  39.     VERSION
  40.         3.00    08 Jul 1991 - All known problems are rectified.
  41.  
  42.     FUNCTION
  43.         Demonstrates use of AppIcons, AppMenu, and AppWindow.
  44.  
  45.     COMPILE
  46.         sc link adc
  47.  
  48. ******************************************************************************/
  49.  
  50. /* Includes */
  51. #include <exec/types.h>
  52. #include <intuition/intuition.h>
  53. #include <workbench/icon.h>
  54. #include <workbench/workbench.h>
  55. #include <workbench/startup.h>
  56. #include <dos.h>
  57. #include <dos/dos.h>
  58. #include <dos/dosextens.h>
  59. #include <dos/dostags.h>
  60.  
  61. /* Defines  */
  62. #define ASIZE   500     /* size of the arrays */
  63.  
  64. /* Structs, externs, and prototypes */
  65.  
  66. struct AppWindow *awin = NULL;
  67. struct Window *win = NULL;
  68.  
  69. struct AppIcon *aicon = NULL;
  70.  
  71. struct AppMenuItem *amenu[ASIZE];
  72. char *appName[ASIZE];
  73. short  amicount = NULL;
  74.  
  75. struct Remember *key = NULL;
  76. struct MsgPort *aport = NULL;   /* message port to be used with appicon system */
  77.  
  78. struct Library  *GfxBase,
  79.                 *IntuitionBase,
  80.                 *IconBase,
  81.                 *WorkbenchBase,
  82.                 *GadToolsBase;
  83.  
  84. extern struct Library *DOSBase, *SysBase;
  85.  
  86. /* Prototypes   */
  87.  
  88. void _main(void);
  89. void ProcessIcon(struct Window *win,
  90.                  struct AppMessage *am);
  91. UBYTE *BSTRtoSTR(ULONG b);
  92. void ShutDown(void);
  93. void warning(char *s);
  94. void warning2(char *r, char *s);
  95. void fatal(char *s);
  96. void fatal2(char *r, char *s);
  97. struct Window *MakeWindow(struct Remember **key);
  98. void UpdateWindow(struct Window *win);
  99. void FreePid(struct ProcID *pid);
  100.  
  101. /* Icon data    */
  102. USHORT    chip    BlackHoleGadData1[] = {
  103. /* Plane 0 */
  104.     0x0000,0x01ff,0xf087,0xff00,0x0000,0x0000,0x01ff,0xb807,
  105.     0xfc00,0x0000,0x0000,0x007f,0xbc07,0x7c00,0x0000,0x0000,
  106.     0x000f,0xfc03,0xfc00,0x0000,0x0000,0x0007,0xfe23,0xfc00,
  107.     0x0000,0x0000,0x0001,0xfe03,0xfc00,0x0000,0x0000,0x0220,
  108.     0x7f07,0xfc00,0x0000,0x0000,0x0000,0x7f03,0xfc00,0x0000,
  109.     0x0000,0x0000,0x3f07,0x9800,0x0000,0x0000,0x0000,0x3f03,
  110.     0xf800,0x0000,0x0000,0x7ff0,0x0e07,0xf000,0x0000,0x0000,
  111.     0xfffc,0x0e87,0xf000,0x0000,0x0033,0xeffe,0x0e1f,0xf000,
  112.     0x0000,0x00ff,0xf7ff,0x7d0f,0xc200,0x3000,0x01ff,0xffff,
  113.     0x3c9f,0xc000,0x3000,0x03ff,0xffff,0xabff,0x8000,0x7000,
  114.     0x07ff,0x7e53,0xbeff,0x0000,0xf000,0x0f3e,0x7807,0xf7fe,
  115.     0x0871,0xf000,0x13ff,0xe007,0x5b00,0x00e2,0xe000,0x01f3,
  116.     0xc018,0xffd0,0x01e7,0xc000,0x03a3,0x800d,0x5b60,0x03ff,
  117.     0xe400,0x07c7,0x087f,0xf7c0,0x1f7e,0x7800,0x0780,0x00ff,
  118.     0xbee5,0x3ffc,0xf000,0x0700,0x01f9,0xca7f,0xffff,0xe000,
  119.     0x0600,0x03f8,0x9eff,0xffff,0xc000,0x0600,0x27f4,0x3f7f,
  120.     0xf7fe,0x8000,0x0000,0x0ff0,0xf83f,0xfbfc,0x0000,0x0000,
  121.     0x0ff0,0x781f,0xff80,0x0000,0x0000,0x0fe0,0x7a07,0xff00,
  122.     0x0000,0x0000,0x0fe0,0x7c00,0x0000,0x0000,0x0000,0x1ae0,
  123.     0x7f00,0x0000,0x0000,0x0000,0x3fe0,0x7f00,0x2000,0x0000,
  124.     0x0000,0x3fe0,0x3f84,0x0000,0x0000,0x0000,0x1fc0,0x3fc0,
  125.     0x0000,0x0000,0x0000,0x3fe4,0x3ff0,0x0000,0x0000,0x0000,
  126.     0x3fe0,0x3fff,0x0000,0x0000,0x0000,0x3ff0,0x3eff,0x8000,
  127.     0x0000,0x0000,0x7ff0,0x9dff,0xc000,0x0000,
  128. /* Plane 1 */
  129.     0x0000,0x003c,0xf027,0x9f00,0x0000,0x0000,0x007f,0xf807,
  130.     0xfc00,0x0000,0x0000,0x0057,0xcc43,0x7c00,0x0000,0x0000,
  131.     0x0003,0xcd01,0xfc00,0x0000,0x0000,0x0100,0x0e01,0xec00,
  132.     0x0000,0x0000,0x0000,0x0e01,0xfc00,0x0000,0x0000,0x0080,
  133.     0x0701,0xfc00,0x0000,0x0000,0x0000,0x4101,0xfc00,0x0000,
  134.     0x0000,0x0080,0x2103,0xf800,0x0000,0x0000,0x0002,0x1943,
  135.     0x3800,0x0000,0x0000,0x7ff0,0x2986,0x0000,0x0000,0x0040,
  136.     0xf88e,0x5984,0x1000,0x0000,0x0033,0xf801,0x090c,0xf000,
  137.     0x0000,0x00fe,0xf800,0xfa68,0xc000,0x3000,0x01ff,0xf000,
  138.     0xd3b8,0xd000,0x3000,0x03ff,0xf87e,0x54f0,0x8500,0x7000,
  139.     0x07ff,0x704f,0x41e3,0x0000,0xf000,0x0f3e,0x600f,0x0802,
  140.     0x0071,0xf000,0x03df,0xc000,0xa4fe,0x00f3,0xe000,0x01b3,
  141.     0x805f,0x0071,0x00e6,0xc000,0x03e7,0x8032,0xa4e0,0x01fd,
  142.     0xe000,0x07c7,0x0060,0x0848,0x037a,0x7800,0x0780,0x00cf,
  143.     0xc179,0x07fc,0xf000,0x0700,0x518b,0xb5bf,0x0fff,0xe000,
  144.     0x0600,0x071c,0xe500,0x07ff,0xc000,0x0600,0x0712,0x4f80,
  145.     0x0fbe,0x8000,0x0000,0x0f10,0x9c40,0x0ffc,0x0000,0x0000,
  146.     0x0c10,0x9e38,0x8f81,0x0000,0x0000,0x0902,0x8847,0xff00,
  147.     0x0000,0x0000,0x0f80,0x4c00,0x8000,0x0000,0x0000,0x1fa0,
  148.     0x4700,0x0000,0x0000,0x0000,0x3fc0,0x4100,0x8000,0x0000,
  149.     0x0000,0x3fc0,0x0000,0x0000,0x0000,0x0000,0x1fc0,0x3880,
  150.     0x4000,0x0000,0x0000,0x37c0,0x6180,0x0000,0x0000,0x0000,
  151.     0x3fc1,0x21e3,0x0000,0x0000,0x0000,0x3fd0,0x37ff,0x8000,
  152.     0x0000,0x0000,0x7f82,0x1ffc,0xc000,0x0000,
  153. };
  154.  
  155. USHORT    chip    BlackHoleGadData2[] = {
  156. /* Plane 0 */
  157.     0x000b,0xfc00,0x9ff0,0x7fc0,0x0000,0x0043,0xc090,0x0fe5,
  158.     0x1380,0x0000,0x000f,0x8003,0x0370,0x0300,0x0000,0x0027,
  159.     0x0080,0x0362,0x03c0,0x0000,0x0002,0x0fcc,0x5ee0,0x07c0,
  160.     0x0000,0x000c,0x1ffa,0x4ef0,0x07e0,0x2000,0x0008,0x3efe,
  161.     0x00fc,0x2365,0x0000,0x0018,0x7fff,0xc075,0xa020,0x0000,
  162.     0x403c,0x0f2b,0xc0bc,0x0034,0x0000,0x24de,0x098f,0x44f0,
  163.     0x0019,0x0000,0x817e,0x1387,0xc0e3,0x001c,0x4000,0x0064,
  164.     0x0380,0xc073,0x3a1f,0x0000,0x0000,0xc311,0x63c1,0x9e0e,
  165.     0x0000,0x8080,0x0501,0xe6c1,0xfe06,0x0200,0x0100,0x2a01,
  166.     0xff80,0xfe06,0x4400,0x0180,0x09c1,0xdf00,0x5405,0x0000,
  167.     0x2548,0x0db1,0xfd04,0x2801,0x0800,0x0384,0x97ff,0xd700,
  168.     0xf801,0x0000,0xa200,0x1fd6,0xffe3,0x7201,0x8800,0x2302,
  169.     0x3e8f,0xbbe6,0xf081,0x8800,0x2300,0xbd07,0xfedf,0xf000,
  170.     0x8a00,0x0100,0x7e0b,0xd7ff,0xd243,0x8000,0x2100,0xe843,
  171.     0x7f1b,0x7025,0x4800,0x0140,0xd403,0xf707,0x0003,0x0000,
  172.     0x44c0,0xfe0d,0xff00,0x8801,0x0000,0x80c1,0xfe3f,0x8b01,
  173.     0xc002,0x0200,0x00c1,0xf36c,0x0621,0xa600,0x0000,0x01f1,
  174.     0x9b7c,0x0e03,0x004c,0x0000,0x0471,0x81be,0x0fc3,0x00f9,
  175.     0x0200,0x0131,0x837a,0x8bb7,0x40f6,0x4800,0x0040,0x837e,
  176.     0x07dd,0xe1f8,0x0400,0x0009,0x8a5a,0x01fa,0xfef0,0x0000,
  177.     0x014f,0x907c,0x41ff,0xf860,0x0000,0x080f,0xc03f,0xc4bf,
  178.     0xf0c0,0x0000,0x0007,0xc09a,0xc447,0xe180,0x0000,0x0005,
  179.     0xa01d,0x9004,0x0110,0x0000,0x0007,0xf03f,0xf110,0x0348,
  180.     0x0000,0x0007,0xfebf,0xfa04,0x7f80,0x0000,
  181. /* Plane 1 */
  182.     0x0083,0x3c7f,0x9c70,0x7fc0,0x0000,0x0007,0xce7f,0x8c6a,
  183.     0xd380,0x0000,0x001f,0xbfed,0x003f,0xc200,0x0000,0x0007,
  184.     0x70fc,0x02ef,0xe3c0,0x0000,0x0002,0xcfcc,0x5d8b,0xe7c0,
  185.     0x0000,0x0005,0x9824,0x0a17,0xe7e0,0x6000,0x000b,0x3886,
  186.     0x5e0f,0xd360,0x0000,0x0018,0x7841,0xdf9d,0xf824,0x0000,
  187.     0x003c,0x0e91,0xd684,0x78b4,0x0000,0x23de,0x6889,0x7ab0,
  188.     0x18dc,0x0000,0x416e,0xf374,0x16a1,0x00dd,0x4000,0x0064,
  189.     0xe379,0x9eb7,0x3a5e,0x0000,0x8001,0x634d,0x7981,0x9e4e,
  190.     0x0000,0x8003,0xc574,0xa65f,0xf266,0x0000,0x0023,0xe93d,
  191.     0xbfbf,0x62f6,0x4400,0x09e7,0xe9c5,0x003b,0xf5f1,0x2000,
  192.     0x4767,0xc931,0x01eb,0x39e1,0x0000,0x07f3,0x9e4a,0x291c,
  193.     0x09c1,0x0000,0x8267,0xd430,0x00c3,0x9341,0x9800,0x836d,
  194.     0xa086,0x446e,0x176d,0x8200,0x3305,0xa366,0x009a,0x57cc,
  195.     0x8200,0x0107,0x41fa,0x2827,0xfb9f,0xc000,0x010f,0xbb9b,
  196.     0x0113,0x3fcd,0xc400,0x091e,0x8fb1,0x017f,0x0fcf,0x2000,
  197.     0x44d8,0x81f9,0xfb79,0x0f88,0x0000,0x00c1,0xfebb,0x224d,
  198.     0xcf80,0x0200,0x00c1,0xf36a,0x26dd,0xad00,0x0200,0x00f1,
  199.     0x9be1,0xf93f,0x0e4c,0x0000,0x0571,0x8133,0xba5f,0x1ee9,
  200.     0x0400,0x0071,0xb323,0x3027,0x5cf7,0x8800,0x0040,0xbb53,
  201.     0xf72e,0xe1f8,0x0000,0x0049,0xbe65,0xf144,0x3ef0,0x0000,
  202.     0x000f,0x8f40,0xac82,0x3860,0x0000,0x0c0c,0xdf23,0x40d8,
  203.     0x30c0,0x0000,0x0004,0xcfd5,0xc447,0xe180,0x0000,0x0004,
  204.     0xa7d4,0x905c,0x0120,0x0000,0x0007,0xf7f9,0xf1fd,0x83c0,
  205.     0x0000,0x0007,0xf930,0x9ff8,0x7c80,0x0000,
  206. };
  207.  
  208. struct    Image    BlackHoleGadI1 = {
  209.     0, 0,                /* Top Corner */
  210.     71, 38, 2,            /* Width, Height, Depth */
  211.     &BlackHoleGadData1[0],        /* Image Data */
  212.     0x0003, 0x0000,            /* PlanePick,PlaneOnOff */
  213.     NULL                /* Next Image */
  214. };
  215.  
  216. struct    Image    BlackHoleGadI2 = {
  217.     0, 0,                /* Top Corner */
  218.     71, 38, 2,            /* Width, Height, Depth */
  219.     &BlackHoleGadData2[0],        /* Image Data */
  220.     0x0003, 0x0000,            /* PlanePick,PlaneOnOff */
  221.     NULL                /* Next Image */
  222. };
  223.  
  224. struct Gadget gad =
  225.    {
  226.    NULL,            /* Next Gadget           */
  227.    NULL,NULL,        /* Left Edge, Top Edge       */
  228.    71,38,            /* Width, Height       */
  229.    GFLG_GADGHIMAGE, /* Flags           */
  230.    NULL,            /* Activation  */
  231.    NULL,            /* Gadget Type           */
  232.    (APTR)&BlackHoleGadI1,   /* Gadget Render       */
  233.    (APTR)&BlackHoleGadI2,   /* Select Render       */
  234.    NULL,                    /* Gadget Text           */
  235.    NULL,                    /* Mutual Exclude       */
  236.    NULL,                    /* Special Info           */
  237.    };
  238.  
  239. void main()
  240. {
  241.     /* Automatic variables  */
  242.     ULONG sig;
  243.     struct DiskObject *dob = NULL;
  244.     struct AppMessage *am;
  245.     struct IntuiMessage *tim, im;
  246.     struct FileInfoBlock *fib = NULL;
  247.     BOOL alive = TRUE;
  248.  
  249.     if (!(IntuitionBase = (void *) OpenLibrary("intuition.library", 37)))
  250.         fatal("Can't open Intuition.library");
  251.  
  252.     if (!(GfxBase = (void *) OpenLibrary("graphics.library", 37)))
  253.         fatal("Can't open graphics.library");
  254.  
  255.     if (!(IconBase = (void *) OpenLibrary("icon.library", 37)))
  256.         fatal("Can't open icon.library");
  257.  
  258.     if (!(WorkbenchBase = (void *) OpenLibrary("workbench.library", 37)))
  259.         fatal("Can't open workbench.library");
  260.  
  261.     fib = (struct FileInfoBlock *) calloc(sizeof(*fib), 1);  /* allocate the FileInfoBlock */
  262.  
  263.     win = MakeWindow(&key);
  264.     aport = (void *) CreatePort(NULL, 0);
  265.     awin = (struct AppWindow *) AddAppWindow(0, win, win, aport, NULL);    /* setup appwindow */
  266.     if (!win || !aport || !awin)
  267.         fatal2("ADC: General Failure.", "Could be Low Memory condition.");
  268.  
  269.     /* setup disk object    */
  270.     dob = (struct DiskObject *) calloc(sizeof(*dob), 1);
  271.     dob->do_Magic = NULL;
  272.     dob->do_Version = NULL;
  273.     dob->do_Gadget = gad;
  274.     dob->do_Type = NULL;
  275.     dob->do_DefaultTool = NULL;
  276.     dob->do_ToolTypes = NULL;
  277.     dob->do_CurrentX = dob->do_CurrentY = NO_ICON_POSITION;
  278.     dob->do_DrawerData = NULL;
  279.     dob->do_ToolWindow = NULL;
  280.     dob->do_StackSize = 0;
  281.  
  282.     if (!(aicon = (struct AppIcon *) AddAppIcon(NULL, NULL, "Application", aport, NULL, dob, TAG_END)))
  283.         fatal("ADC: Could not create AppIcon");
  284.     amenu[amicount++] = (void *) AddAppMenuItem(NULL, "Quit", "Quit AppDemo", aport, NULL);
  285.  
  286.     while (alive)
  287.     {
  288.         sig = (1 << aport->mp_SigBit) |
  289.                 ((win) ? (1 << win->UserPort->mp_SigBit) : NULL);
  290.         sig = Wait(sig);
  291.  
  292.         /* App Messages   */
  293.         if (sig & (1 << aport->mp_SigBit)) /* an app signal */
  294.         {
  295.             while (am = (struct AppMessage *) GetMsg(aport))
  296.             {
  297.                 switch(am->am_Type)
  298.                 {
  299.                 case AMTYPE_APPWINDOW:   /* something was dropped in the window  */
  300.                     ProcessIcon(win, am);
  301.                     break;
  302.  
  303.                 case AMTYPE_APPICON:     /* icon was double-clicked */
  304.                     if (!am->am_NumArgs) /* Double-Clicked  */
  305.                     {
  306.                         if (!win) /* is our (app) window open? */
  307.                         {
  308.                             if (!(win = MakeWindow(&key)))
  309.                             {
  310.                                 warning("ADC: Cannot open window!");
  311.                                 break;
  312.                             }
  313.  
  314.                             if (!(awin = (struct AppWindow *) AddAppWindow(NULL, NULL, win, aport, NULL)))
  315.                             {
  316.                                 warning("ADC: Cannot make window an AppWindow!");
  317.                                 CloseWindow(win); win = NULL;
  318.                             }
  319.                         }
  320.                         WindowToFront(win);
  321.                         ActivateWindow(win);
  322.                         UpdateWindow(win);
  323.                     }
  324.                     else /* Icons dropped on AppIcon    */
  325.                         ProcessIcon(win, am);
  326.  
  327.                     break;
  328.  
  329.                 case AMTYPE_APPMENUITEM: /* menu item was selected */
  330.                     if (!stricmp(am->am_UserData, "quit"))
  331.                         alive = FALSE;
  332.  
  333.                     else /* we have something real */
  334.                     {
  335.                         SystemTags(am->am_UserData, SYS_Input, Open("nil:", MODE_NEWFILE),
  336.                                                     SYS_Output, Open("CON:////Application_Output/CLOSE/AUTO/WAIT", MODE_NEWFILE),
  337.                                                     SYS_Asynch, TRUE,
  338.                                                     NP_StackSize, 30000,    /* Let's be generous    */
  339.                                                     NP_Priority, 0,
  340.                                                     NP_Cli, FALSE,
  341.                                                     TAG_END, NULL);
  342.                     }
  343.                     break;
  344.  
  345.                 default:
  346.                     break;
  347.                 }
  348.                 ReplyMsg(am);
  349.             }
  350.         }
  351.  
  352.         /* Window Messages    */
  353.         if (win && (sig & (1 << win->UserPort->mp_SigBit))) /* an IDCMP signal */
  354.         {
  355.             while (win && (tim = (struct IntuiMessage*) GetMsg(win->UserPort)))
  356.             {
  357.                 im = *tim;
  358.                 ReplyMsg(tim);
  359.                 switch (im.Class)
  360.                 {
  361.                 case CLOSEWINDOW:
  362.                     UpdateWindow(win);
  363.                     RemoveAppWindow(awin); awin = NULL;
  364.                     CloseWindow(win); win = NULL;
  365.                     break;
  366.  
  367.                 case NEWSIZE:
  368.                 case REFRESHWINDOW:
  369.                     UpdateWindow(win);
  370.                     break;
  371.  
  372.                 default:
  373.                     break;
  374.                 }
  375.             }
  376.         }
  377.     }
  378.  
  379.     ShutDown();
  380. }
  381.  
  382. void ProcessIcon(struct Window *win, struct AppMessage *am)
  383. {
  384.     UBYTE buf[256], *name;  /* general work buffer & pointer */
  385.     struct FileInfoBlock *fib = (struct FileInfoBlock *) calloc(1, sizeof(*fib));
  386.     int i;
  387.  
  388.     if (fib)
  389.     {
  390.         for (i = 0; i < am->am_NumArgs; ++i)    /* until list exhausted */
  391.         {
  392.             if (am->am_ArgList[i].wa_Lock)
  393.             {
  394.                 if (Examine(am->am_ArgList[i].wa_Lock, fib))
  395.                 {
  396.                     if (!NameFromLock(am->am_ArgList[i].wa_Lock, buf, sizeof(buf)))
  397.                         fatal("ADC: GetPath failed!!");
  398.  
  399.                     if (buf[strlen(buf)-1] != ':') /* do we need to add a slash?    */
  400.                     {
  401.                         buf[strlen(buf)+1] = NULL;
  402.                         buf[strlen(buf)] = '/';
  403.                     }
  404.  
  405.                     /* append name to path
  406.                     */
  407.                     if (amicount < ASIZE)
  408.                     {
  409.                         appName[amicount] = name = (char*) strdup(am->am_ArgList[i].wa_Name);
  410.                         strcat(buf, am->am_ArgList[i].wa_Name);
  411.                         amenu[amicount] = (struct AppMenu*) AddAppMenuItem(amicount, strdup(buf), name, aport, NULL);
  412.                         ++amicount;
  413.                     }
  414.                     else
  415.                         warning2("ADC: Array Overflow - Cannot add", am->am_ArgList[i].wa_Name);
  416.                 }
  417.                 else
  418.                     warning2("ADCCant find directory for", am->am_ArgList[i].wa_Name);
  419.             }
  420.             else
  421.                 warning2("ADC: Entry Without Lock!!", am->am_ArgList[i].wa_Name);
  422.         }
  423.         UpdateWindow(win);
  424.  
  425.         free(fib);
  426.     }
  427. }
  428.  
  429. UBYTE *BSTRtoSTR(b)
  430. ULONG b;
  431. {
  432.     UBYTE *bstr, *str, *s;
  433.     int c;
  434.  
  435.     bstr = BADDR(b);
  436.     str = s = (UBYTE*) calloc(1, (c = *bstr++) + 1);
  437.     while(c--)
  438.         *s++ = *bstr++;
  439.  
  440.     return str;
  441. }
  442.  
  443. void ShutDown()
  444. {
  445.     int i;
  446.  
  447.     for (i = 0; i < amicount; ++i)
  448.         if (!RemoveAppMenuItem(amenu[i]))
  449.             warning("ADC: RemoveAppMenuItem failed!");
  450.     amicount = 0;
  451.  
  452.     if (aicon)
  453.     {
  454.         if (!RemoveAppIcon(aicon))
  455.             warning("ADC: RemoveAppIcon failed!");
  456.         aicon = NULL;
  457.     }
  458.  
  459.     if (awin)
  460.     {
  461.         if (!RemoveAppWindow(awin))
  462.             warning("ADC: RemoveAppWindow Failed!");
  463.         awin = NULL;
  464.     }
  465.     if (win)
  466.         CloseWindow(win), win = NULL;
  467.  
  468.     if (aport)
  469.         DeletePort(aport), aport = NULL;
  470.  
  471.     FreeRemember(&key, TRUE);
  472.  
  473.     if (GfxBase)        CloseLibrary(GfxBase),       GfxBase = NULL;
  474.     if (IntuitionBase)  CloseLibrary(IntuitionBase), IntuitionBase = NULL;
  475.     if (IconBase)       CloseLibrary(IconBase),      IconBase = NULL;
  476.     if (WorkbenchBase)  CloseLibrary(WorkbenchBase), WorkbenchBase = NULL;
  477.  
  478.     exit(0);
  479. }
  480.  
  481. struct NewWindow newwin = {
  482.     100, 100,   /* LeftEdge, TopEdge */
  483.     300, 150,   /* Width, Height */
  484.     1, 2,       /* DetailPen, BlockPen */
  485.     CLOSEWINDOW | NEWSIZE | MOUSEBUTTONS | VANILLAKEY,  /* IDCMPFlags */
  486.     WINDOWSIZING | WINDOWDRAG | WINDOWCLOSE | WINDOWDEPTH | SMART_REFRESH | GIMMEZEROZERO, /* Flags */
  487.     NULL,       /* FirstGadget */
  488.     NULL,       /* CheckMark */
  489.     NULL,       /* Title */
  490.     NULL,       /* Screen */
  491.     NULL,       /* BitMap */
  492.     50, 50, -1, -1, /* MinWidth, MinHeight, MaxWidth, MaxHeight */
  493.     WBENCHSCREEN    /* Type */
  494.     };
  495.  
  496. struct Window *MakeWindow(key) /* standard window creation */
  497. struct Remember **key;
  498. {
  499.     struct Window *win;
  500.  
  501.     if (win = (struct Window*) OpenWindow(&newwin))
  502.         SetWindowTitles(win, "AppDemo", "Application Launcher by Fred Mitchell");
  503.  
  504.     return win;
  505. }
  506.  
  507. void UpdateWindow(win)  /* display list of programs in window */
  508. struct Window *win;
  509. {
  510.     short height, i;
  511.  
  512.     if (win)
  513.     {
  514.         SetRast(win->RPort, 0);
  515.         SetAPen(win->RPort, 1);
  516.         SetDrMd(win->RPort, JAM1);
  517.         height = win->RPort->Font->tf_YSize + 2;
  518.  
  519.         for (i = 1; i < amicount; ++i)
  520.         {
  521.             Move(win->RPort, 0, height * i);
  522.             Text(win->RPort, appName[i], strlen(appName[i]));
  523.         }
  524.  
  525.         /* update newwindow structure with new size/location    */
  526.         newwin.LeftEdge = win->LeftEdge;
  527.         newwin.TopEdge  = win->TopEdge;
  528.         newwin.Width    = win->Width;
  529.         newwin.Height   = win->Height;
  530.     }
  531. }
  532.  
  533. void FreePid(pid)   /* free the list of ProcessID's */
  534. struct ProcID *pid;
  535. {
  536.     if (pid->nextID)
  537.         FreePid(pid->nextID);
  538.  
  539.     /* free(pid); ...*/
  540. }
  541.  
  542. /* Warning and Fatal Routines   */
  543.  
  544. static int _GetResponse(struct Window *win)
  545. {
  546.     int ret = (int) win;
  547.  
  548.     if ((BOOL) win != 0 && (BOOL) win != 1)
  549.     {
  550.         while ((ret = SysReqHandler(win, NULL, TRUE)) == -2)
  551.             ;
  552.         FreeSysRequest(win);
  553.     }
  554.     return ret;
  555. }
  556.  
  557. void fatal(m)  /* Program bug detected - could be fatal! */
  558. char *m;    /* message */
  559. {
  560.     struct EasyStruct es;
  561.  
  562.     es.es_StructSize = sizeof(es);
  563.     es.es_Flags = NULL;
  564.     es.es_Title = "<FATAL ERROR>";
  565.     es.es_TextFormat = "Fatal Error: %s";
  566.     es.es_GadgetFormat = "OK";
  567.  
  568.     _GetResponse((struct Window*) BuildEasyRequest(NULL, &es, NULL, m));
  569.     ShutDown();
  570. }
  571.  
  572. void fatal2(m, n)  /* Program bug detected - could be fatal! */
  573. char *m, *n;    /* message */
  574. {
  575.     struct EasyStruct es;
  576.  
  577.     es.es_StructSize = sizeof(es);
  578.     es.es_Flags = NULL;
  579.     es.es_Title = "<FATAL ERROR>";
  580.     es.es_TextFormat = "Fatal Error: %s\n%s";
  581.     es.es_GadgetFormat = "OK";
  582.  
  583.     _GetResponse((struct Window*) BuildEasyRequest(NULL, &es, NULL, m, n));
  584.     ShutDown();
  585. }
  586.  
  587. void warning(m) /* Far less serious - non-fatal error */
  588. char *m;    /* message */
  589. {
  590.     struct EasyStruct es;
  591.  
  592.     es.es_StructSize = sizeof(es);
  593.     es.es_Flags = NULL;
  594.     es.es_Title = "Warning";
  595.     es.es_TextFormat = "Error: %s";
  596.     es.es_GadgetFormat = "Continue";
  597.  
  598.     _GetResponse((struct Window*) BuildEasyRequest(NULL, &es, NULL, m));
  599. }
  600.  
  601. void warning2(m, n) /* Far less serious - non-fatal error */
  602. char *m, *n;    /* messages */
  603. {
  604.     struct EasyStruct es;
  605.  
  606.     es.es_StructSize = sizeof(es);
  607.     es.es_Flags = NULL;
  608.     es.es_Title = "Warning";
  609.     es.es_TextFormat = "Error: %s\n%s";
  610.     es.es_GadgetFormat = "Continue";
  611.  
  612.     _GetResponse((struct Window*) BuildEasyRequest(NULL, &es, NULL, m, n));
  613. }
  614.