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

  1. /* nodehost.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.  * Written by David N. Junod
  10.  *
  11.  * Example of a Dynamic Node Host.  This example is useful for determining
  12.  * what nodes an AmigaGuide database is calling when it brings up the
  13.  * "Can't locate node" requester.
  14.  *
  15.  * Compile with:
  16.  *
  17.  * lc -L+lib:debug.lib -cfist -ms -v -y nodehost
  18.  *
  19.  */
  20.  
  21. #include <exec/types.h>
  22. #include <intuition/intuition.h>
  23. #include <intuition/screens.h>
  24. #include <utility/hooks.h>
  25. #include <utility/tagitem.h>
  26. #include <libraries/amigaguide.h>
  27. #include <dos/dos.h>
  28. #include <dos/dosextens.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32.  
  33. #include <clib/exec_protos.h>
  34. #include <clib/dos_protos.h>
  35. #include <clib/intuition_protos.h>
  36. #include <clib/graphics_protos.h>
  37. #include <clib/utility_protos.h>
  38. #include <clib/amigaguide_protos.h>
  39.  
  40. #include <pragmas/exec_pragmas.h>
  41. #include <pragmas/dos_pragmas.h>
  42. #include <pragmas/intuition_pragmas.h>
  43. #include <pragmas/graphics_pragmas.h>
  44. #include <pragmas/utility_pragmas.h>
  45. #include <pragmas/amigaguide_pragmas.h>
  46.  
  47. /*****************************************************************************/
  48.  
  49. #define    DB(x)    x
  50.  
  51. #define    ASM    __asm __saveds
  52. #define    REG(x)    register __ ## x
  53.  
  54. /*****************************************************************************/
  55.  
  56. extern void kprintf (void *,...);
  57. ULONG ASM dispatchAmigaGuideHost (REG (a0) struct Hook * h, REG (a2) STRPTR db, REG (a1) Msg msg);
  58.  
  59. /*****************************************************************************/
  60.  
  61. extern struct Library *SysBase, *DOSBase;
  62. struct Library *IntuitionBase, *GfxBase, *UtilityBase, *AmigaGuideBase;
  63.  
  64. /*****************************************************************************/
  65.  
  66. main (int argc, char **argv)
  67. {
  68.     struct Hook hook;
  69.     APTR hh;
  70.  
  71.     if (IntuitionBase = OpenLibrary ("intuition.library", 39))
  72.     {
  73.     GfxBase = OpenLibrary ("graphics.library", 39);
  74.     UtilityBase = OpenLibrary ("utility.library", 39);
  75.  
  76.     if (AmigaGuideBase = OpenLibrary ("amigaguide.library", 39))
  77.     {
  78.         /* Initialize the hook */
  79.         hook.h_Entry = dispatchAmigaGuideHost;
  80.  
  81.         /* Add the AmigaGuideHost to the system */
  82.         if (hh = AddAmigaGuideHostA (&hook, "ExampleHost", NULL))
  83.         {
  84.         printf ("Added AmigaGuideHost 0x%lx\n", hh);
  85.  
  86.         /* Wait until we're told to quit */
  87.         Wait (SIGBREAKF_CTRL_C);
  88.  
  89.         printf ("Remove AmigaGuideHost 0x%lx", hh);
  90.  
  91.         /* Try removing the host */
  92.         while (RemoveAmigaGuideHostA (hh, NULL) > 0)
  93.         {
  94.             /* Wait a while */
  95.             printf (".");
  96.             Delay (250);
  97.         }
  98.         printf ("\n");
  99.         }
  100.         else
  101.         {
  102.         printf ("Couldn't add AmigaGuideHost\n");
  103.         }
  104.  
  105.         /* close the library */
  106.         CloseLibrary (AmigaGuideBase);
  107.     }
  108.     else
  109.     {
  110.         printf ("couldn't open amigaguide.library V39\n");
  111.     }
  112.  
  113.     CloseLibrary (UtilityBase);
  114.     CloseLibrary (GfxBase);
  115.     CloseLibrary (IntuitionBase);
  116.     }
  117.     else
  118.     {
  119.     printf ("requires 3.0\n");
  120.     }
  121. }
  122.  
  123. /*****************************************************************************/
  124.  
  125. #define    TEMP_NODE "This AmigaGuideHost is an example, that can also\nbe used as a debugging tool.\n"
  126. #define    LINK "Link: "
  127.  
  128. /*****************************************************************************/
  129.  
  130. struct TextAttr TOPAZ8  = {"topaz.font", 8, NULL, NULL};
  131. struct TextAttr TOPAZ8B = {"topaz.font", 8, FSF_BOLD, NULL};
  132.  
  133. /*****************************************************************************/
  134.  
  135. /* We really need the screen, rectangle, and pen spec. */
  136. VOID Display (struct opNodeIO * onm)
  137. {
  138.     struct TagItem *attrs = onm->onm_Attrs;
  139.     struct IntuiText it1 = {NULL};
  140.     struct IntuiText it2 = {NULL};
  141.     struct Rectangle *rect = NULL;
  142.     struct NewWindow nw = {NULL};
  143.     struct Screen *scr = NULL;
  144.     struct IntuiMessage *msg;
  145.     struct Window *win;
  146.     UWORD *pens = NULL;
  147.     BOOL going = TRUE;
  148.     LONG width = 0L;
  149.     WORD w = 640;
  150.     WORD h = 200;
  151.     WORD dif;
  152.  
  153.     /* Get attributes, could be NULL */
  154.     if (attrs)
  155.     {
  156.     scr = (struct Screen *) GetTagData (HTNA_Screen, NULL, attrs);
  157.     pens = (UWORD *) GetTagData (HTNA_Pens, NULL, attrs);
  158.     rect = (struct Rectangle *) GetTagData (HTNA_Rectangle, NULL, attrs);
  159.     }
  160.  
  161.     /* Prepare the IntuiText */
  162.     it1.FrontPen = it2.FrontPen = ((pens) ? pens[SHADOWPEN] : 1);
  163.     it1.DrawMode = it2.DrawMode = JAM1;
  164.     it1.ITextFont = &TOPAZ8;
  165.     it2.ITextFont = &TOPAZ8B;
  166.     it1.IText = LINK;
  167.     it2.IText = onm->onm_Node;
  168.  
  169.     /* Get the width of the first string */
  170.     width = IntuiTextLength (&it1);
  171.     it2.LeftEdge = (SHORT) width;
  172.  
  173.     /* Add in the length of the node name */
  174.     width += IntuiTextLength (&it2);
  175.  
  176.     /* Link the text */
  177.     it1.NextText = &it2;
  178.  
  179.     /* Prepare the window */
  180.     nw.IDCMPFlags = IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS;
  181.     nw.Flags = WFLG_BORDERLESS | WFLG_SMART_REFRESH | WFLG_NOCAREREFRESH \
  182.       |WFLG_ACTIVATE;
  183.     nw.Width = 8 + width + 8;
  184.     nw.Height = 16;
  185.     nw.Screen = scr;
  186.     nw.Type = (scr) ? CUSTOMSCREEN : WBENCHSCREEN;
  187.  
  188.     /* Cache the screen size */
  189.     if (scr)
  190.     {
  191.     w = scr->Width;
  192.     h = scr->Height;
  193.     }
  194.  
  195.     /* See if we have a open help window */
  196.     if (rect)
  197.     {
  198.     /* Center the window within the help window */
  199.     nw.LeftEdge = rect->MinX + ((rect->MaxX - nw.Width) / 2);
  200.     nw.TopEdge = rect->MinY + ((rect->MaxY - nw.Height) / 2);
  201.     }
  202.     /* No help window, so go off the screen */
  203.     else if (scr)
  204.     {
  205.  
  206.     /*
  207.      * Center the window horizontally under the mouse and place it
  208.      * vertically over the mouse position.
  209.      */
  210.     nw.LeftEdge = scr->MouseX - (nw.Width / 2);
  211.     nw.TopEdge = scr->MouseY - (nw.Height - 2);
  212.  
  213.     /* Make sure the window can open */
  214.     nw.LeftEdge = (nw.LeftEdge < 0) ? 0 : nw.LeftEdge;
  215.     nw.TopEdge = (nw.TopEdge < 0) ? 0 : nw.TopEdge;
  216.     }
  217.  
  218.     /* Make sure window is on-screen */
  219.     dif = (nw.LeftEdge + nw.Width) - w;
  220.     nw.LeftEdge = (dif > 0) ? nw.LeftEdge - dif : nw.LeftEdge;
  221.     dif = (nw.TopEdge + nw.Height) - h;
  222.     nw.TopEdge = (dif > 0) ? nw.TopEdge - dif : nw.TopEdge;
  223.  
  224.     /* Open the temporary window */
  225.     if (win = OpenWindow (&nw))
  226.     {
  227.     /* Clear the window background */
  228.     SetAPen (win->RPort, ((pens) ? pens[SHADOWPEN] : 1));
  229.     RectFill (win->RPort, 0, 0, (win->Width - 1), (win->Height - 1));
  230.     SetAPen (win->RPort, ((pens) ? pens[SHINEPEN] : 2));
  231.     RectFill (win->RPort, 1, 1, (win->Width - 2), (win->Height - 2));
  232.  
  233.     /* Print the text */
  234.     PrintIText (win->RPort, &it1, 8, 4);
  235.  
  236.     /* Keep on going til the going gets tough */
  237.     while (going)
  238.     {
  239.         /* Wait around for something eventful */
  240.         Wait (1L << win->UserPort->mp_SigBit);
  241.  
  242.         /* Pull each message and handle it */
  243.         while (msg = (struct IntuiMessage *) GetMsg (win->UserPort))
  244.         {
  245.         switch (msg->Class)
  246.         {
  247.             case IDCMP_MOUSEBUTTONS:
  248.             /* Stop if we were touched */
  249.             if (msg->Code == SELECTDOWN)
  250.             {
  251.                 going = FALSE;
  252.             }
  253.             break;
  254.  
  255.             case IDCMP_VANILLAKEY:
  256.             /* Stop on significant keypress */
  257.             if ((msg->Code == 27) || (msg->Code == 13))
  258.             {
  259.                 going = FALSE;
  260.             }
  261.             break;
  262.         }
  263.  
  264.         ReplyMsg ((struct Message *) msg);
  265.         }
  266.     }
  267.  
  268.     /* Close the window */
  269.     CloseWindow (win);
  270.     }
  271. }
  272.  
  273. /*****************************************************************************/
  274.  
  275. /* This is your AmigaGuideHost dispatch hook.  It will never run on your own process. */
  276. ULONG ASM dispatchAmigaGuideHost (REG (a0) struct Hook * h, REG (a2) STRPTR db, REG (a1) Msg msg)
  277. {
  278.     struct opNodeIO *onm = (struct opNodeIO *) msg;
  279.     struct opFindHost *ofh;
  280.     ULONG retval = 0;
  281.  
  282.     switch (msg->MethodID)
  283.     {
  284.         /* Does this node belong to you? */
  285.     case HM_FINDNODE:
  286.         ofh = (struct opFindHost *) msg;
  287.         DB (kprintf ("Find [%s] in %s\n", ofh->ofh_Node, db));
  288.  
  289.         /* See if they want to find our table of contents */
  290.         if ((Stricmp (ofh->ofh_Node, "main")) == 0)
  291.         {
  292.         /*
  293.          * Return TRUE to indicate that it's your node, otherwise
  294.          * return FALSE.
  295.          */
  296.         DB (kprintf ("found main\n"));
  297.         retval = TRUE;
  298.         }
  299.         else
  300.         {
  301.         /* Display the name of the node */
  302.         Display (onm);
  303.  
  304.         /*
  305.          * Return TRUE to indicate that it's your node, otherwise
  306.          * return FALSE.
  307.          */
  308.         retval = FALSE;
  309.         DB (kprintf ("didn't find\n"));
  310.         }
  311.         break;
  312.  
  313.         /* Open a node. */
  314.     case HM_OPENNODE:
  315.         DB (kprintf ("Open [%s] in %s\n", onm->onm_Node, db));
  316.  
  317.         /* See if they want to display our table of contents */
  318.         if ((Stricmp (onm->onm_Node, "main")) == 0)
  319.         {
  320.         /* Provide the contents of the node */
  321.         onm->onm_DocBuffer = TEMP_NODE;
  322.         onm->onm_BuffLen = strlen (TEMP_NODE);
  323.         }
  324.         else
  325.         {
  326.         /* Display the name of the node */
  327.         Display (onm);
  328.  
  329.         /*
  330.          * Indicate that we want the node removed from our database,
  331.          * and that we handled the display of the node
  332.          */
  333.         onm->onm_Flags |= (HTNF_CLEAN | HTNF_DONE);
  334.         }
  335.  
  336.         /* Indicate that we were able to open the node */
  337.         retval = TRUE;
  338.         break;
  339.  
  340.         /* Close a node, that has no users. */
  341.     case HM_CLOSENODE:
  342.         DB (kprintf ("Close [%s] in %s\n", onm->onm_Node, db));
  343.  
  344.         /* Indicate that we were able to close the node */
  345.         retval = TRUE;
  346.         break;
  347.  
  348.         /* Free any extra memory */
  349.     case HM_EXPUNGE:
  350.         DB (kprintf ("Expunge [%s]\n", db));
  351.         break;
  352.  
  353.     default:
  354.         DB (kprintf ("Unknown method %ld\n", msg->MethodID));
  355.         break;
  356.     }
  357.  
  358.     return (retval);
  359. }
  360.