home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / stv_427.lzh / STV / source / window.c < prev   
C/C++ Source or Header  |  1991-01-09  |  5KB  |  197 lines

  1. /*********************************
  2. *  WINDOW  09/29/90
  3. *  Source file for STV
  4. *  © Copyright 1990 Timm Martin
  5. *  All Rights Reserved Worldwide
  6. **********************************/
  7.  
  8. #include <exec/types.h>
  9. #include <functions.h>
  10. #include <intuition/intuition.h>
  11. #include "func.h"
  12. #include "main.h"
  13.  
  14. /*************
  15. *  FONT USED
  16. **************/
  17.  
  18. struct TextAttr text_attr =  /* GLOBAL */
  19. {
  20.   /* STRPTR ta_Name */ LATER,
  21.   /* UWORD ta_YSize */ LATER,
  22.   /* UBYTE ta_Style */ FS_NORMAL,
  23.   /* UBYTE ta_Flags */ NULL,
  24. };
  25.  
  26. /********************
  27. *  WINDOW STRUCTURE
  28. *********************/
  29.  
  30. #define WINDOW_TOP 2
  31.  
  32. #define MIN_WIDTH 300
  33. #define MIN_HEIGHT 30
  34.  
  35. extern struct Gadget vprop_gadget;  /* gadget.c */
  36.  
  37. struct NewWindow new_window =
  38. {
  39.   /* SHORT           LeftEdge    */ LATER,
  40.   /* SHORT           TopEdge     */ LATER,
  41.   /* SHORT           Width       */ LATER,
  42.   /* SHORT           Height      */ LATER,
  43.   /* UBYTE           DetailPen   */ BLUE,
  44.   /* UBYTE           BlockPen    */ WHITE,
  45.   /* ULONG           IDCMPFlags  */ CLOSEWINDOW|GADGETDOWN|GADGETUP|MENUPICK|MOUSEBUTTONS|RAWKEY|REFRESHWINDOW,
  46.   /* ULONG           Flags       */ ACTIVATE|SMART_REFRESH|WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|WINDOWSIZING,
  47.   /* struct Gadget * FirstGadget */ LATER,
  48.   /* struct Image *  CheckMark   */ NULL,
  49.   /* UBYTE *         Title       */ NULL,
  50.   /* struct Screen * Screen      */ LATER,
  51.   /* struct BitMap * BitMap      */ NULL,
  52.   /* SHORT           MinWidth    */ MIN_WIDTH,
  53.   /* SHORT           MinHeight   */ MIN_HEIGHT,
  54.   /* USHORT          MaxWidth    */ 1024,
  55.   /* USHORT          MaxHeight   */ 1024,
  56.   /* USHORT          Type        */ LATER,
  57. };
  58.  
  59. struct Window   *win = NULL;  /* GLOBAL */
  60. struct RastPort *rp;          /* GLOBAL */
  61.  
  62. #define LEFT   new_window.LeftEdge
  63. #define TOP    new_window.TopEdge
  64. #define WIDTH  new_window.Width
  65. #define HEIGHT new_window.Height
  66.  
  67. /****************
  68. *  WINDOW CLOSE
  69. *****************/
  70.  
  71. /*
  72. This procedure closes the text window if opened.  It removes the gadgets
  73. beforehand so Intuition doesn't try to free the memory associated with my
  74. fake system gadgets.
  75. */
  76.  
  77. void window_close( void )
  78. {
  79.   if (win)
  80.   {
  81.     ClearMenuStrip( win );
  82.     CloseWindow( win );
  83.   }
  84. }
  85.  
  86. /***************
  87. *  WINDOW OPEN
  88. ****************/
  89.  
  90. /*
  91. This procedure opens the window.
  92. */
  93.  
  94. void window_open( void )
  95. {
  96.   int height;            /* height of screen in which window will open */
  97.   struct Screen screen;  /* to grab Workbench screen data */
  98.   int width;             /* width of screen in which window will open */
  99.  
  100.   /* if custom screen desired */
  101.   if (arguments.Screen)
  102.   {
  103.     width  = arguments.Screen->Width;
  104.     height = arguments.Screen->Height;
  105.     new_window.Screen = arguments.Screen;
  106.     new_window.Type   = CUSTOMSCREEN;
  107.   }
  108.   /* else opening it on the Workbench screen */
  109.   else
  110.   {
  111.     GetScreenData( &screen, (long)sizeof(struct Screen), WBENCHSCREEN, NULL );
  112.     width  = screen.Width;
  113.     height = screen.Height;
  114.     new_window.Type = WBENCHSCREEN;
  115.   }
  116.  
  117.   /* either use defaults or specified left and top edges */
  118.   LEFT = arguments.Left >= 0 ? arguments.Left : 0;
  119.   TOP  = arguments.Top  >= 0 ? arguments.Top  : WINDOW_TOP;
  120.  
  121.   /* left and top edge have priority -- make sure they are valid */
  122.   if (LEFT + MIN_WIDTH > width)
  123.     LEFT = width - MIN_WIDTH;
  124.   if (TOP + MIN_HEIGHT > height)
  125.     TOP = height - MIN_HEIGHT;
  126.  
  127.   /* either use default or specified width and height */
  128.   WIDTH  = arguments.Width  > 0 ? arguments.Width  : width;
  129.   HEIGHT = arguments.Height > 0 ? arguments.Height : height;
  130.  
  131.   /* now make sure the width and height are not too large */
  132.   if (LEFT + WIDTH > width)
  133.     WIDTH = width - LEFT;
  134.   if (TOP + HEIGHT > height)
  135.     HEIGHT = height - TOP;
  136.  
  137.   /* if window opened OK */
  138.   if (win = OpenWindow( &new_window ))
  139.   {
  140.     SET_POINTER;
  141.  
  142.     rp = win->RPort;
  143.  
  144.     /* grab font information */
  145.     stats.Font       = rp->Font;
  146.     stats.TextWidth  = rp->TxWidth;
  147.     stats.TextHeight = rp->TxHeight;
  148.     stats.Baseline   = rp->TxBaseline;
  149.  
  150.     /* prepare the TextAttr structure */
  151.     text_attr.ta_Name  = (STRPTR)stats.Font->tf_Message.mn_Node.ln_Name;
  152.     text_attr.ta_YSize = stats.TextHeight;
  153.  
  154.     /* prevent this sucker from crashing */
  155.     WindowLimits( win, 0L, (long)(win->BorderTop + stats.TextHeight + 4), 0L, 0L );
  156.  
  157.     gadget_add();
  158.     menu_add();
  159.  
  160.     /* adjust dimensions based on window size */
  161.     window_resized();
  162.   }
  163.   else
  164.     program_end( "window" );
  165. }
  166.  
  167. /******************
  168. *  WINDOW RESIZED
  169. *******************/
  170.  
  171. /*
  172. This procedure redraws the window border and recalculates all of the
  173. values dependent on the window width and height.
  174. */
  175.  
  176. void window_resized( void )
  177. {
  178.   /* start just inside left and top border */
  179.   stats.LeftEdge = win->BorderLeft + 1;
  180.   stats.TopEdge  = win->BorderTop + 1;
  181.  
  182.   /* calculate new number of rows and columns based on window size */
  183.   stats.Cols = (win->Width - stats.LeftEdge - win->BorderRight) /
  184.                stats.TextWidth;
  185.   stats.Rows = (win->Height - stats.TopEdge - win->BorderBottom) /
  186.                stats.TextHeight;
  187.  
  188.   /* find right and bottom edges based on text */
  189.   stats.RightEdge = stats.LeftEdge + stats.Cols * stats.TextWidth - 1;
  190.   stats.BottomEdge = stats.TopEdge + stats.Rows * stats.TextHeight - 1;
  191.  
  192.   stats.VCenter = ((stats.BottomEdge - stats.TopEdge) >> 1) + stats.TopEdge;
  193.  
  194.   /* makes sure Top is still valid after resizing window */
  195.   line_check();
  196. }
  197.