home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Resources / System / BoingBag1 / Contributions / Workbench / RexxArpLib3p6 / src / sreq / simpmsg.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  7KB  |  347 lines

  1. /** simpmsg.c
  2.  *
  3.  *
  4.  *    SimpleMsgWindow(1.0)    ARP Programmers Manual   SimpleMsgWindow(1.0)
  5.  *
  6.  *
  7.  *
  8.  *    NAME
  9.  *   SimpleMsgWindow -- Open a simple message window fitting a requester.
  10.  *
  11.  *    SYNOPSIS
  12.  *   Windowptr = SimpleMsgWindow( SimpleRequester )
  13.  *
  14.  *    FUNCTION
  15.  *   This function opens a window and displays a set of text lines.
  16.  *   The window is auto-sizing: it determines its size based on the
  17.  *   number of non-NULL strings. The window does not have an IDCMP
  18.  *   and comes only with a drag bar.
  19.  *
  20.  *    INPUTS
  21.  *   SimpleRequester -- A pointer to an initialized SimpleRequester
  22.  *   structure. Only applicable fields are used.
  23.  *
  24.  *    RESULT
  25.  *   Windowptr   -- A pointer to the window. NULL on error. (See
  26.  *                  SimpleRequester structure for error code).
  27.  *
  28.  *    ADDITIONAL CONSIDERATIONS
  29.  *   The window may be closed using the fuction CloseMsgWindow.
  30.  *   Since the window does not have an IDCMP port, no special steps
  31.  *   need to be taken.
  32.  *
  33.  *    BUGS
  34.  *   None reported sofar.
  35.  *
  36.  *    AUTHOR
  37.  *   W.G.J. Langeveld (WGL)
  38.  *
  39.  *
  40. **/
  41. #include <exec/types.h>
  42. #include <exec/io.h>
  43. #include <exec/memory.h>
  44. #include <libraries/dos.h>
  45. #include <intuition/intuition.h>
  46. #include <graphics/gfxbase.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <functions.h>
  51.  
  52. #include <simpreq.h>
  53. #include "simpdefs.h"
  54.  
  55. extern struct IntuitionBase *IntuitionBase;
  56. extern struct GfxBase *GfxBase;
  57.  
  58. struct Window *SimpleMsgWindow ( struct SimpleRequester *s )
  59. {
  60.     return(SimpleMsgWIDCMP(s, NULL));
  61. }
  62.  
  63. struct Window *SimpleMsgWIDCMP ( struct SimpleRequester *s, long IDCMP )
  64. {
  65.     WORD           vsize, i, hsize, temp;
  66.     struct Window *w;
  67.     /*
  68.      *   Return if no request, otherwise initialize
  69.      */
  70.     if (s == NULL)
  71.         return(NULL);
  72.     
  73.     s->sr_ErrorCode = SR_SUCCESS;
  74.     /*
  75.      *   Get the dimensions of the window.
  76.      *   vsize is the height, hsize the width of the window.
  77.      *
  78.      *   First, make hsize the length of the longest prompt line, and increment
  79.      *   vsize along the way.
  80.      */
  81.     hsize = MINWINWIDTH;
  82.     for (i = 0, vsize = MINWINHEIGHT; s->sr_Prompt[i] && i < SR_MAXPROMPTS;
  83.     i++, vsize += LINEHEIGHT) 
  84.     {
  85.         if ((temp = CHARWIDTH * strlen(s->sr_Prompt[i])) > hsize)
  86.             hsize = temp;
  87.     }
  88.     /*
  89.      *   Add space for the vertical borders of the requester.
  90.      */
  91.     hsize += 2 * VBORDER;
  92.     /*
  93.      *   Add a bit more space, and we have the window height in vsize.
  94.      */
  95.     vsize += VSKIP;
  96.     /*
  97.      *   Open the window
  98.      */
  99.     w = OpenSReqWindow(s, hsize, vsize, IDCMP, WINDOWDRAG+WINDOWDEPTH);
  100.     
  101.     if (w == NULL)
  102.         goto cleanup;
  103.     /*
  104.      *   Put up the prompts.
  105.      */
  106.     UpdateMsgWindow(w, s);
  107.     /*
  108.      *   Clean up the mess.
  109.      */
  110.     cleanup:
  111.     
  112.     return(w);
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119. /**
  120.  *
  121.  *
  122.  *    UpdateMsgWindow(1.0)    ARP Programmers Manual   UpdateMsgWindow(1.0)
  123.  *
  124.  *
  125.  *
  126.  *    NAME
  127.  *   UpdateMsgWindow -- Update a simple message window.
  128.  *
  129.  *    SYNOPSIS
  130.  *   result = UpdateMsgWindow(WindowPtr, SimpleRequester)
  131.  *    D0        A0        A1
  132.  *
  133.  *    FUNCTION
  134.  *   This function updates a window with a set of text lines.
  135.  *   The window should have been opened previously with
  136.  *   SimpleMsgWindow() and it is up to you to make sure that the
  137.  *   new set of lines will fit in the window.
  138.  *
  139.  *    INPUTS
  140.  *   Windowptr       -- A pointer to a previously opened window
  141.  *   SimpleRequester -- A pointer to an initialized SimpleRequester
  142.  *                      structure. Only applicable fields are used.
  143.  *
  144.  *    RESULT
  145.  *   result -- 0 if an error was detected. In that case the error
  146.  *             value may be obtained from the SimpleRequester structure.
  147.  *             1 on success.
  148.  *
  149.  *    ADDITIONAL CONSIDERATIONS
  150.  *
  151.  *    BUGS
  152.  *   None reported sofar.
  153.  *
  154.  *    AUTHOR
  155.  *   W.G.J. Langeveld (WGL)
  156.  *
  157.  *
  158. **/
  159. long UpdateMsgWindow ( struct Window *w, struct SimpleRequester *s )
  160. {
  161.     struct RastPort *rp;
  162.     struct TextFont *font;
  163.     long             result = 0L;
  164.     WORD             vpos,
  165.                      i,
  166.                      j;
  167.     ULONG             apen,
  168.                      bpen;
  169.     LONG             splen;
  170.     char            *spaces = NULL;
  171.     
  172.     if (w == NULL)
  173.         return(result);
  174.     
  175.     result = 1L;
  176.     /*
  177.      *   rp is the RastPort
  178.      */
  179.     rp = w->RPort;
  180.     /*
  181.      *   Set font.
  182.      */
  183.     font = (struct TextFont *) OpenFont(Topaz80Equiv());
  184.     if (font == NULL) 
  185.     {
  186.         s->sr_ErrorCode = SR_NOFONT;
  187.         result = 0L;
  188.         goto cleanup;
  189.     }
  190.     
  191.     SetFont(rp, font);
  192.     /*
  193.      *   Get the pens.
  194.      */
  195.     if (s->sr_PenList) 
  196.     {
  197.         apen = s->sr_PenList->reqp_PromptPen;
  198.         bpen = s->sr_PenList->reqp_BackPen;
  199.     }
  200.     else
  201.     {
  202.         apen = DEFPROMPEN;
  203.         bpen = DEFBACKPEN;
  204.     }
  205.     /*
  206.      *   Add the prompts
  207.      */
  208.     SetAPen(rp, (long) apen);
  209.     SetBPen(rp, (long) bpen);
  210.     SetDrMd(rp, JAM2);
  211.     
  212.     splen = (w->Width - 2 * VBORDER) / 8 + 1;
  213.     
  214.     spaces = AllocMem(256L, MEMF_CLEAR);
  215.     if (spaces == NULL) 
  216.     {
  217.         s->sr_ErrorCode = SR_OUTOFMEMORY;
  218.         result = 0L;
  219.         goto cleanup;
  220.     }
  221.     
  222.     vpos = FIRSTLINEY;
  223.     for (i = 0; s->sr_Prompt[i] && i < SR_MAXPROMPTS; i++) 
  224.     {
  225.         Move(rp, (long) VBORDER, (long) vpos);
  226.         
  227.         for (j = 0; j < splen; j++)
  228.             spaces[j] = ' ';
  229.         strcpy(spaces, s->sr_Prompt[i]);
  230.         
  231.         j = strlen(s->sr_Prompt[i]);
  232.         if (j) 
  233.         {
  234.             spaces[j] = ' ';
  235.             Text(rp, spaces, (long) (splen - 1) );
  236.         }
  237.         
  238.         vpos += LINEHEIGHT;
  239.     }
  240.     /*
  241.      *   Clean up the mess.
  242.      */
  243.     cleanup:
  244.     if (font)
  245.         CloseFont(font);
  246.     if (spaces)
  247.         FreeMem(spaces, 256L);
  248.     
  249.     return(result);
  250. }
  251.  
  252. /**
  253.  *
  254.  *
  255.  *    CloseMsgWindow(1.0)    ARP Programmers Manual   CloseMsgWindow(1.0)
  256.  *
  257.  *
  258.  *
  259.  *    NAME
  260.  *   CloseMsgWindow -- Close a simple message window.
  261.  *
  262.  *    SYNOPSIS
  263.  *   result = CloseMsgWindow(WindowPtr)
  264.  *    D0        A0
  265.  *
  266.  *    FUNCTION
  267.  *   This function closes a window.
  268.  *
  269.  *    INPUTS
  270.  *   Windowptr       -- A pointer to a previously opened window
  271.  *
  272.  *    RESULT
  273.  *   result -- NULL if success, Pointer to Window if failure.
  274.  *
  275.  *    ADDITIONAL CONSIDERATIONS
  276.  *   Provided to avoid having to open the libraries
  277.  *
  278.  *    BUGS
  279.  *   None reported sofar.
  280.  *
  281.  *    AUTHOR
  282.  *   W.G.J. Langeveld (WGL)
  283.  *
  284.  *
  285. **/
  286. struct Window *CloseMsgWindow ( struct Window *w )
  287. {
  288.     /*
  289.      *   Well, close it.
  290.      */
  291.     CloseWindow(w);
  292.     w = NULL;
  293.     
  294.     return(w);
  295. }
  296.  
  297. /**
  298.  *
  299.  *   See if the default GfxBase font is "Topaz-80 compatible". If so,
  300.  *   return a pointer to that font's TextAttr structure to the caller.
  301.  *
  302. **/
  303. struct TextAttr *Topaz80Equiv ( void )
  304. {
  305.     static char   fontname[40] = "topaz.font";
  306.     static struct TextAttr plaintext = 
  307.     {
  308.         fontname, 8, 0, 0
  309.     };
  310.     
  311.     struct TextFont *font;
  312.     /*
  313.      *   Make sure the font pointer won't change during this operation
  314.      */
  315.     Forbid();
  316.     font = GfxBase->DefaultFont;
  317.     if (font) 
  318.     {
  319.         /*
  320.          *   Now check if it has intolerable flags or style or size
  321.          */
  322.         if (font->tf_Style & (FSF_UNDERLINED | FSF_BOLD | FSF_ITALIC |
  323.             FSF_EXTENDED   | FSF_COLORFONT)) font = NULL;
  324.         if (font->tf_Flags & (FPF_REVPATH | FPF_PROPORTIONAL))
  325.             font = NULL;
  326.         if ((font->tf_YSize != 8) || (font->tf_XSize != 8))
  327.             font = NULL;
  328.     }
  329.     /*
  330.      *   If we have a font and it is of the right type, copy it into the
  331.      *   TextAttr struct for future use.
  332.      */
  333.     if (font)
  334.         strcpy(fontname, font->tf_Message.mn_Node.ln_Name);
  335.     Permit();
  336.     /*
  337.      *   Now load the font into memory.
  338.      */
  339.     font = OpenFont(&plaintext);
  340.     if (font == NULL)
  341.         OpenDiskFont(&plaintext);
  342.     if (font)
  343.         CloseFont(font);
  344.     
  345.     return(&plaintext);
  346. }
  347.