home *** CD-ROM | disk | FTP | other *** search
/ YPA: Your Privacy Assured / YPA.ISO / other_goodies / utilities / amigaguid.lha / AmigaGuide / AG_V39 / Source / aghelp.c < prev    next >
C/C++ Source or Header  |  1993-01-08  |  14KB  |  532 lines

  1. /* aghelp.c
  2.  *
  3.  * (c) Copyright 1992 Commodore-Amiga, Inc.  All rights reserved.
  4.  *
  5.  * This software is provided as-is and is subject to change; no warranties
  6.  * are made.  All use is at your own risk.  No liability or responsibility
  7.  * is assumed.
  8.  *
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <exec/execbase.h>
  14. #include <exec/libraries.h>
  15. #include <intuition/screens.h>
  16. #include <intuition/intuition.h>
  17. #include <graphics/gfx.h>
  18. #include <graphics/text.h>
  19. #include <libraries/amigaguide.h>
  20. #include <libraries/gadtools.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23.  
  24. #include <clib/alib_protos.h>
  25. #include <clib/amigaguide_protos.h>
  26. #include <clib/exec_protos.h>
  27. #include <clib/gadtools_protos.h>
  28. #include <clib/graphics_protos.h>
  29. #include <clib/intuition_protos.h>
  30.  
  31. #include <pragmas/amigaguide_pragmas.h>
  32. #include <pragmas/exec_pragmas.h>
  33. #include <pragmas/gadtools_pragmas.h>
  34. #include <pragmas/graphics_pragmas.h>
  35. #include <pragmas/intuition_pragmas.h>
  36.  
  37. /*****************************************************************************/
  38.  
  39. #define    DB(x)    ;
  40.  
  41. /*****************************************************************************/
  42.  
  43. struct AppInfo
  44. {
  45.     struct Screen    *ai_Screen;        /* Screen that our application will open on */
  46.     APTR         ai_VI;            /* GadTools visual info */
  47.     struct Window    *ai_Window;        /* Window pointer */
  48.     struct Menu        *ai_Menu;        /* Menus */
  49.  
  50.     /* Gadgets that we want to set attributes on */
  51.     struct Gadget    *ai_GStatus;        /* Status window */
  52.  
  53.     /* Help related information */
  54.     AMIGAGUIDECONTEXT     ai_AmigaGuide;        /* Pointer to the AmigaGuide context */
  55.     struct NewAmigaGuide ai_NAG;        /* Used to start AmigaGuide */
  56.     LONG         ai_Region;        /* Region that the mouse if over */
  57.  
  58.     /* Control information */
  59.     BOOL         ai_Done;        /* Done yet? */
  60. };
  61.  
  62. /*****************************************************************************/
  63.  
  64. extern struct Library *SysBase;
  65. extern struct Library *DOSBase;
  66. struct Library *AmigaGuideBase;
  67. struct Library *GadToolsBase;
  68. struct Library *GfxBase;
  69. struct Library *IntuitionBase;
  70.  
  71. /*****************************************************************************/
  72.  
  73. /* Context ID's to be sent to AmigaGuide */
  74. STRPTR context[] =
  75. {
  76.     "MAIN",
  77.     "STATUS",
  78.     "LISTVIEW",
  79.     "ACCEPT",
  80.     "CANCEL",
  81.     "QUIT",
  82.     NULL
  83. };
  84.  
  85. /*****************************************************************************/
  86.  
  87. #define    MCMD_MAIN    0
  88. #define    MCMD_STATUS    1
  89. #define    MCMD_LISTVIEW    2
  90. #define    MCMD_ACCEPT    3
  91. #define    MCMD_CANCEL    4
  92. #define    MCMD_QUIT    5
  93.  
  94. /*****************************************************************************/
  95.  
  96. /* Simple little prompts to display within the status window */
  97. STRPTR quickhelp[] =
  98. {
  99.     "AGHelp main window.",
  100.     "Quick-Help or status window.",
  101.     "List of absolutely nothing.",
  102.     "Accept changes.",
  103.     "Cancel changes.",
  104.     "",
  105.     "Window sizing gadget.",
  106.     "Window drag bar.",
  107.     "Screen drag bar.",
  108.     "Window depth gadget.",
  109.     "Screen depth gadget.",
  110.     "Window zoom gadget.",
  111.     "",
  112.     "Window close gadget.",
  113.     NULL
  114. };
  115.  
  116. #define    MAX_REGION    14
  117.  
  118. /*****************************************************************************/
  119.  
  120. struct TextAttr topaz8 = {"topaz.font", 8,};
  121.  
  122. /*****************************************************************************/
  123.  
  124. #define    IDCMP_FLAGS    IDCMP_RAWKEY | IDCMP_CLOSEWINDOW | IDCMP_MENUPICK | IDCMP_MENUHELP | \
  125.             IDCMP_GADGETUP | IDCMP_MOUSEMOVE | IDCMP_GADGETHELP
  126.  
  127. /*****************************************************************************/
  128.  
  129. #define    V(x)    ((void *)(x))
  130.  
  131. struct NewMenu main_menu[] =
  132. {
  133.     {NM_TITLE,    "Project",},
  134.     {NM_ITEM,    "Close Window", "Q",    NULL, NULL, V(MCMD_QUIT),},
  135.     {NM_END,},
  136. };
  137.  
  138. /*****************************************************************************/
  139. /*****************************************************************************/
  140. /*****************************************************************************/
  141.  
  142. VOID DisplayError (LONG err)
  143. {
  144.     printf ("%s\n", GetAmigaGuideString (err));
  145. }
  146.  
  147. /*****************************************************************************/
  148.  
  149. void HandleEvents (struct AppInfo * ai)
  150. {
  151.     struct Window *win = ai->ai_Window;
  152.     struct AmigaGuideMsg *agm;
  153.     struct IntuiMessage *imsg;
  154.     struct MenuItem *item;
  155.     ULONG sigr = 0L;
  156.     ULONG sigi = 0L;
  157.     ULONG sigb = 0L;
  158.     UWORD selection;
  159.     STRPTR label;
  160.     LONG region;
  161.     LONG qhelp;
  162.  
  163.     /* Show that we're not done running the application yet */
  164.     ai->ai_Done = FALSE;
  165.  
  166.     /* Get our signal bits */
  167.     sigb = AmigaGuideSignal (ai->ai_AmigaGuide);
  168.     sigi = (1L << win->UserPort->mp_SigBit);
  169.  
  170.     /* Clear the AmigaGuide context */
  171.     SetAmigaGuideContext (ai->ai_AmigaGuide, 0L, NULL);
  172.  
  173.     /* Continue until done */
  174.     while (!(ai->ai_Done))
  175.     {
  176.     /* Wait for something to happen */
  177.     sigr = Wait (sigb | sigi);
  178.  
  179.     /* Pull Intuition & GadTools messages */
  180.     while (imsg = GT_GetIMsg (win->UserPort))
  181.     {
  182.         switch (imsg->Class)
  183.         {
  184.         case IDCMP_CLOSEWINDOW:
  185.             ai->ai_Done = TRUE;
  186.             break;
  187.  
  188.         case IDCMP_MENUPICK:
  189.             selection = imsg->Code;
  190.             while (selection != MENUNULL)
  191.             {
  192.             item = ItemAddress (win->MenuStrip, selection);
  193.  
  194.             switch ((LONG)MENU_USERDATA (item))
  195.             {
  196.                 case MCMD_QUIT:
  197.                 ai->ai_Done = TRUE;
  198.                 break;
  199.             }
  200.  
  201.             selection = item->NextSelect;
  202.             }
  203.             break;
  204.  
  205.         case IDCMP_MENUHELP:
  206.             if (item = ItemAddress (win->MenuStrip, imsg->Code))
  207.             {
  208.             /* Display the node */
  209.             SendAmigaGuideCmd (ai->ai_AmigaGuide, NULL,
  210.                        AGA_Context, (LONG) MENU_USERDATA (item),
  211.                        TAG_DONE);
  212.             }
  213.  
  214.             break;
  215.  
  216.         case IDCMP_GADGETUP:
  217.             switch (((struct Gadget *)imsg->IAddress)->GadgetID)
  218.             {
  219.             case MCMD_ACCEPT:
  220.             case MCMD_CANCEL:
  221.                 ai->ai_Done = TRUE;
  222.                 break;
  223.             }
  224.             break;
  225.  
  226.         case IDCMP_GADGETHELP:
  227.             qhelp = region = -1;
  228.             if (imsg->IAddress == NULL)
  229.             {
  230.             /* Not over our window */
  231.             DB (printf ("not over our window\n"));
  232.             }
  233.             else if (imsg->IAddress == (APTR) win)
  234.             {
  235.             /* Over our window */
  236.             DB (printf ("over window\n"));
  237.             qhelp = region = 0;
  238.             }
  239.             else
  240.             {
  241.  
  242.             /*
  243.              * Detect system gadgets.  Figure out by looking at
  244.              * system-gadget-type bits in GadgetType field:
  245.              */
  246.             LONG sysgtype = ((struct Gadget *) imsg->IAddress)->GadgetType & 0xF0;
  247.  
  248.             /* Set the region */
  249.             qhelp = (sysgtype >> 4) + 5;
  250.             region = HTFC_SYSGADS + sysgtype;
  251.  
  252.             switch (sysgtype)
  253.             {
  254.                 case GTYP_SIZING:
  255.                 DB (printf ("Gadget Help for window sizing gadget\n"));
  256.                 break;
  257.  
  258.                 case GTYP_WDRAGGING:
  259.                 DB (printf ("Gadget Help for window drag-bar\n"));
  260.                 break;
  261.  
  262.                 case GTYP_WUPFRONT:
  263.                 DB (printf ("Gadget Help for window depth gadget\n"));
  264.                 break;
  265.  
  266.                 case GTYP_WDOWNBACK:
  267.                 DB (printf ("Gadget Help for window zoom gadget\n"));
  268.                 break;
  269.  
  270.                 case GTYP_CLOSE:
  271.                 DB (printf ("Gadget Help for window close gadget\n"));
  272.                 break;
  273.  
  274.                 case 0:
  275.  
  276.                 /*
  277.                  * In this example, we only have one gadget, so
  278.                  * we know which one it is.  Normally, you'd
  279.                  * have to figure that out here, using the
  280.                  * usual techniques you already use for other
  281.                  * gadget messages.
  282.                  */
  283.                 DB (printf ("Gadget id %d, code %d\n",
  284.                     ((struct Gadget *)imsg->IAddress)->GadgetID, imsg->Code));
  285.                 qhelp = region = (LONG) ((struct Gadget *)imsg->IAddress)->GadgetID;
  286.                 break;
  287.  
  288.                 default:
  289.                 DB (printf ("Gadget Help on some other system gadget\n"));
  290.                 break;
  291.             }
  292.             }
  293.  
  294.             /* Remember the region */
  295.             ai->ai_Region = region;
  296.  
  297.             /* Range check the region */
  298.             label = NULL;
  299.             if ((qhelp >= 0) && (qhelp < MAX_REGION))
  300.             label = quickhelp[qhelp];
  301.  
  302.             /* Update the quick-help information */
  303.             GT_SetGadgetAttrs (ai->ai_GStatus, ai->ai_Window, NULL, GTTX_Text, label, TAG_DONE);
  304.             break;
  305.  
  306.         case IDCMP_RAWKEY:
  307.             /* Help key pressed */
  308.             if (imsg->Code == 95)
  309.             {
  310.             /* Display the node */
  311.             SendAmigaGuideCmd (ai->ai_AmigaGuide, NULL,
  312.                        AGA_Context, ai->ai_Region,
  313.                        TAG_DONE);
  314.             }
  315.             break;
  316.         }
  317.  
  318.         /* Reply to the message */
  319.         GT_ReplyIMsg (imsg);
  320.     }
  321.  
  322.     /* Process AmigaGuide messages */
  323.     if (sigr & sigb)
  324.     {
  325.         /* process amigaguide messages */
  326.         while (agm = GetAmigaGuideMsg (ai->ai_AmigaGuide))
  327.         {
  328.         /* check message types */
  329.         switch (agm->agm_Type)
  330.         {
  331.             /* AmigaGuide is ready for us */
  332.             case ActiveToolID:
  333.             break;
  334.  
  335.             /* This is a reply to our cmd */
  336.             case ToolCmdReplyID:
  337.             if (agm->agm_Pri_Ret)
  338.                 DisplayError (agm->agm_Sec_Ret);
  339.             break;
  340.  
  341.             /* This is a status message */
  342.             case ToolStatusID:
  343.             if (agm->agm_Pri_Ret)
  344.                 DisplayError (agm->agm_Sec_Ret);
  345.             break;
  346.  
  347.             /* Shutdown message */
  348.             case ShutdownMsgID:
  349.             if (agm->agm_Pri_Ret)
  350.                 DisplayError (agm->agm_Sec_Ret);
  351.             break;
  352.  
  353.             default:
  354.             break;
  355.         }
  356.  
  357.         /* Reply to the message */
  358.         ReplyAmigaGuideMsg (agm);
  359.         }
  360.     }
  361.     }
  362. }
  363.  
  364. /*****************************************************************************/
  365.  
  366. void main (void)
  367. {
  368.     struct ExecBase *SysBase = (*((struct ExecBase **) 4));
  369.     struct Gadget *anchor = NULL;
  370.     struct NewGadget ng;
  371.     struct AppInfo *ai;
  372.     struct Gadget *gad;
  373.  
  374.     if (SysBase->LibNode.lib_Version < 39)
  375.     {
  376.     printf ("requires V39\n");
  377.     return;
  378.     }
  379.  
  380.     if (ai = AllocVec (sizeof (struct AppInfo), MEMF_CLEAR))
  381.     {
  382.     /* Open the ROM libraries */
  383.     if (IntuitionBase = OpenLibrary ("intuition.library", 39))
  384.     {
  385.         GadToolsBase = OpenLibrary ("gadtools.library", 39);
  386.         GfxBase = OpenLibrary ("graphics.library", 39);
  387.  
  388.         /* Open AmigaGuide */
  389.         if (AmigaGuideBase = OpenLibrary ("amigaguide.library", 39))
  390.         {
  391.         /* Lock the default public screen */
  392.         if (ai->ai_Screen = LockPubScreen (NULL))
  393.         {
  394.             /* Obtain the screen visual information for GadTools */
  395.             if (ai->ai_VI = GetVisualInfoA (ai->ai_Screen, NULL))
  396.             {
  397.             /* Initialize the global data */
  398.             ai->ai_Region = -1;
  399.  
  400.             anchor = NULL;
  401.             gad = CreateContext (&anchor);
  402.  
  403.             /* Set up the constant stuff */
  404.             ng.ng_TextAttr   = &topaz8;
  405.             ng.ng_VisualInfo = ai->ai_VI;
  406.  
  407.             /* Create a status window */
  408.             ng.ng_LeftEdge   = ai->ai_Screen->WBorLeft + 4;
  409.             ng.ng_TopEdge    = ai->ai_Screen->BarHeight + 1 + 2;
  410.             ng.ng_Width      = 312;
  411.             ng.ng_Height     = 12;
  412.             ng.ng_GadgetText = NULL;
  413.             ng.ng_GadgetID   = MCMD_STATUS;
  414.             ng.ng_Flags      = NULL;
  415.             ng.ng_UserData   = NULL;
  416.             gad = CreateGadget (TEXT_KIND, gad, &ng,
  417.                         GTTX_Justification,    GTJ_CENTER,
  418.                         GTTX_Border,    TRUE,
  419.                         TAG_DONE);
  420.             ai->ai_GStatus = gad;
  421.  
  422.             /* Create a list view */
  423.             ng.ng_LeftEdge   = ai->ai_Screen->WBorLeft + 4;
  424.             ng.ng_TopEdge   += ng.ng_Height + 4;
  425.             ng.ng_Height     = 64;
  426.             ng.ng_GadgetText = NULL;
  427.             ng.ng_GadgetID   = MCMD_LISTVIEW;
  428.             gad = CreateGadget (LISTVIEW_KIND, gad, &ng,
  429.                         GTLV_ReadOnly,    TRUE,
  430.                         GTLV_ScrollWidth,    18,
  431.                         TAG_DONE);
  432.  
  433.             /* Create an Accept button */
  434.             ng.ng_LeftEdge   = ai->ai_Screen->WBorLeft + 4;
  435.             ng.ng_TopEdge    = ai->ai_Screen->BarHeight + 1 + 84;
  436.             ng.ng_Width      = 87;
  437.             ng.ng_Height     = 12;
  438.             ng.ng_GadgetText = "Accept";
  439.             ng.ng_GadgetID   = MCMD_ACCEPT;
  440.             gad = CreateGadget (BUTTON_KIND, gad, &ng, TAG_DONE);
  441.  
  442.             /* Create a Cancel button */
  443.             ng.ng_LeftEdge   = ai->ai_Screen->WBorLeft + 229;
  444.             ng.ng_GadgetText = "Cancel";
  445.             ng.ng_GadgetID   = MCMD_CANCEL;
  446.             gad = CreateGadget (BUTTON_KIND, gad, &ng, TAG_DONE);
  447.  
  448.             /* Open the window */
  449.             if (ai->ai_Window = OpenWindowTags (NULL,
  450.                                 WA_InnerWidth,    320,
  451.                                 WA_InnerHeight,    100,
  452.                                 WA_Gadgets,        anchor,
  453.                                 WA_IDCMP,        IDCMP_FLAGS,
  454.                                 WA_Title,        (ULONG) "AmigaGuide Demo",
  455.                                 WA_PubScreen,    ai->ai_Screen,
  456.                                 WA_MenuHelp,    TRUE,
  457.                                 WA_AutoAdjust,    TRUE,
  458.                                 WA_DragBar,        TRUE,
  459.                                 WA_DepthGadget,    TRUE,
  460.                                 WA_CloseGadget,    TRUE,
  461.                                 WA_SmartRefresh,    TRUE,
  462.                                 WA_NewLookMenus,    TRUE,
  463.                                 WA_Activate,    TRUE,
  464.                                 TAG_DONE))
  465.             {
  466.                 /* Create the menus */
  467.                 if (ai->ai_Menu = CreateMenus (main_menu, TAG_DONE))
  468.                 if (LayoutMenus (ai->ai_Menu, ai->ai_VI,GTMN_NewLookMenus,TRUE,TAG_DONE))
  469.                     SetMenuStrip (ai->ai_Window, ai->ai_Menu);
  470.  
  471.                 /* Turn on gadget help */
  472.                 HelpControl (ai->ai_Window, HC_GADGETHELP);
  473.  
  474.                 /* Remember the AppInfo */
  475.                 ai->ai_Window->UserData = (APTR) ai;
  476.  
  477.                 ai->ai_NAG.nag_Flags = HTF_NOACTIVATE;
  478.  
  479.                 /* Set the application base name */
  480.                 ai->ai_NAG.nag_BaseName = "AGHelp";
  481.  
  482.                 /* Set the document name */
  483.                 ai->ai_NAG.nag_Name = "aghelp.guide";
  484.  
  485.                 /* establish the base name to use for hypertext ARexx port */
  486.                 ai->ai_NAG.nag_ClientPort = "AGAPP_HELP";
  487.  
  488.                 /* Set up the context table */
  489.                 ai->ai_NAG.nag_Context = context;
  490.  
  491.                 /* Open the help system */
  492.                 ai->ai_AmigaGuide = OpenAmigaGuideAsync (&ai->ai_NAG, NULL);
  493.  
  494.                 /* Take care of business */
  495.                 HandleEvents (ai);
  496.  
  497.                 /* Shutdown the help system */
  498.                 CloseAmigaGuide (ai->ai_AmigaGuide);
  499.  
  500.                 /* Clear the menu strip */
  501.                 ClearMenuStrip (ai->ai_Window);
  502.  
  503.                 /* Free the menus */
  504.                 FreeMenus (ai->ai_Menu);
  505.  
  506.                 /* Close the application window */
  507.                 CloseWindow (ai->ai_Window);
  508.             }
  509.  
  510.             /* Free the gadgets */
  511.             FreeGadgets (anchor);
  512.  
  513.             /* Free the GadTools visual info */
  514.             FreeVisualInfo (ai->ai_VI);
  515.             }
  516.  
  517.             /* Unlock the default public screen */
  518.             UnlockPubScreen (NULL, ai->ai_Screen);
  519.         }
  520.         /* Close AmigaGuide */
  521.         CloseLibrary (AmigaGuideBase);
  522.         }
  523.  
  524.         /* Close the ROM libraries */
  525.         CloseLibrary (GfxBase);
  526.         CloseLibrary (GadToolsBase);
  527.         CloseLibrary (IntuitionBase);
  528.     }
  529.     FreeVec (ai);
  530.     }
  531. }
  532.