home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_5.ZIP / WINSRC.ZIP / WINDSP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  11.8 KB  |  399 lines

  1. //    Zinc Interface Library - WINDSP.CPP
  2. //    COPYRIGHT (C) 1990, 1991.  All Rights Reserved.
  3. //    Zinc Software Incorporated.  Pleasant Grove, Utah  USA
  4.  
  5. #include "ui_win.hpp"
  6.  
  7. const int pSin[] = { 0, 871, 1736, 2588, 3420, 4226, 5000, 5736, 6428,
  8.     7071, 7660, 8191, 8660, 9063, 9397, 9659, 9848, 9962, 10000 };
  9.  
  10. DWORD MapColor(const UI_PALETTE *palette, int foreground)
  11. {
  12.     DWORD color = foreground ? palette->colorForeground : palette->colorBackground;
  13.  
  14.     // If RGB color specified then mask extra RGB bit, otherwise get the 
  15.     // system color from the index given. (Colors found in [COLORS] in WIN.INI)
  16.     if (color & 0x80000000L)
  17.         color &= ~0x80000000L;
  18.     else
  19.         color = GetSysColor((int)color);
  20.     return (color);
  21. }
  22.  
  23. UI_MSWINDOWS_DISPLAY::UI_MSWINDOWS_DISPLAY(HANDLE _hInstance, HANDLE _hPrevInstance, int _nCmdShow):
  24.     UI_DISPLAY(FALSE, (4 * LOWORD(GetDialogBaseUnits())) / 4, (14 * HIWORD(GetDialogBaseUnits())) / 8)
  25. {
  26.     WNDCLASS wndClass;
  27.  
  28.     hInstance = _hInstance;
  29.     hPrevInstance = _hPrevInstance;
  30.     nCmdShow = _nCmdShow;
  31.  
  32.     // Get the maximum screen size in pixels.
  33.     lines = GetSystemMetrics(SM_CYSCREEN);
  34.     columns = GetSystemMetrics(SM_CXSCREEN);
  35.     installed = TRUE;
  36.  
  37.     // Register the MS Windows class for generic Zinc windows.
  38.     if ( !hPrevInstance )
  39.     {
  40.         wndClass.style = CS_HREDRAW | CS_VREDRAW;
  41.         wndClass.lpfnWndProc = MSWindowsProc;
  42.         wndClass.cbClsExtra = 0;
  43.  
  44.         // Reserve extra bytes for each window object to point
  45.         // to the Zinc class structure.
  46.         wndClass.cbWndExtra = sizeof( UIW_WINDOW * );
  47.         wndClass.hInstance = hInstance;
  48.         wndClass.hIcon = LoadIcon( 0, IDI_APPLICATION );
  49.         wndClass.hCursor = LoadCursor( 0, IDC_ARROW );
  50.         wndClass.hbrBackground = COLOR_WINDOW+1;
  51.         wndClass.lpszMenuName = NULL;
  52.         wndClass.lpszClassName = "UIW_WINDOW";
  53.  
  54.         if (!RegisterClass(&wndClass))
  55.             installed = FALSE;
  56.     }
  57. }
  58.  
  59. UI_MSWINDOWS_DISPLAY::~UI_MSWINDOWS_DISPLAY(void)
  60. {
  61. }
  62.  
  63. #pragma argsused
  64. void UI_MSWINDOWS_DISPLAY::Bitmap(SCREENID screenID, int column, int line,
  65.     int bitmapWidth, int bitmapHeight, const UCHAR *bitmapArray,
  66.     const UI_PALETTE *palette, const UI_REGION *clipRegion)
  67. {
  68.     if (!screenID)
  69.         return;
  70.  
  71.     HDC hDC = GetDC(screenID);
  72.  
  73.     UI_REGION region;
  74.     region.left = column;
  75.     region.top = line;
  76.     region.right = column + bitmapWidth - 1;
  77.     region.bottom = line + bitmapHeight - 1;
  78.     if (clipRegion)
  79.     {
  80.         region.left = Max(region.left, clipRegion->left);
  81.         region.top = Max(region.top, clipRegion->top);
  82.         region.right = Min(region.right, clipRegion->right);
  83.         region.bottom = Min(region.bottom, clipRegion->bottom);
  84.     }
  85.  
  86.     for (int row = region.top; row <= region.bottom; row++)
  87.     {
  88.         UCHAR *pixel = (UCHAR *)(bitmapArray + (row - region.top) * bitmapWidth);
  89.         for (int column = region.left; column <= region.right; column++, pixel++)
  90.             SetPixel(hDC, column, row, *pixel);
  91.     }
  92.  
  93.     ReleaseDC(screenID, hDC);
  94. }
  95.  
  96. // Ellipse can be used to draw circles, ellipses, arcs, pie slices, etc.
  97. // using the foreground color for the border and background color to fill.
  98. #pragma argsused
  99. void UI_MSWINDOWS_DISPLAY::Ellipse(SCREENID screenID, int column, int line, int startAngle,
  100.     int endAngle, int xRadius, int yRadius, const UI_PALETTE *palette,
  101.     int fill, int xor, const UI_REGION *clipRegion)
  102. {
  103.     if (!screenID)
  104.         return;
  105.  
  106.     HBRUSH oldBrush;
  107.  
  108.     HDC hDC = GetDC(screenID);
  109.     int oldROP2 = SetROP2(hDC, (xor == TRUE) ? R2_XORPEN : R2_COPYPEN);
  110.  
  111.     // Set up the coordinates.
  112.     DWORD topLeft = GetDCOrg(hDC);
  113.     int left = column - xRadius - LOWORD(topLeft);
  114.     int top =  line - yRadius - HIWORD(topLeft);
  115.     int right =  column + xRadius - LOWORD(topLeft);
  116.     int bottom =  line + yRadius - HIWORD(topLeft);
  117.  
  118.     // Compute points to start and stop.
  119.     int startX = 0;
  120.     int startY = 0;
  121.     int endX = 0;
  122.     int endY = 0;
  123.     if (startAngle <= 90)
  124.     {
  125.         startX = column - LOWORD(topLeft) + (int)(1L * pSin[(90 - startAngle) / 5] * xRadius / 10000L);
  126.         startY = line - HIWORD(topLeft) + (int)(1L * pSin[startAngle / 5] * yRadius / 10000L);
  127.     }
  128.     else if (startAngle <= 180)
  129.     {
  130.         startX = column - LOWORD(topLeft) - (int)(1L * pSin[(startAngle - 90) / 5] * xRadius / 10000L);
  131.         startY = line - HIWORD(topLeft) + (int)(1L * pSin[(180 - startAngle) / 5] * yRadius / 10000L);
  132.     }
  133.     else if (startAngle <= 270)
  134.     {
  135.         startX = column - LOWORD(topLeft) - (int)(1L * pSin[(270 - startAngle) / 5] * xRadius / 10000L);
  136.         startY = line - HIWORD(topLeft) - (int)(1L * pSin[(startAngle - 180) / 5] * yRadius / 10000L);
  137.     }
  138.     else if (startAngle <= 360)
  139.     {
  140.         startX = column - LOWORD(topLeft) + (int)(1L * pSin[(startAngle - 270) / 5] * xRadius / 10000L);
  141.         startY = line - HIWORD(topLeft) - (int)(1L * pSin[(360 - startAngle) / 5] * yRadius / 10000L);
  142.     }
  143.  
  144.     if (endAngle <= 90)
  145.     {
  146.         endX = column - LOWORD(topLeft) + (int)(1L * pSin[(90 - endAngle) / 5] * xRadius / 10000L);
  147.         endY = line - HIWORD(topLeft) + (int)(1L * pSin[endAngle / 5] * yRadius / 10000L);
  148.     }
  149.     else if (endAngle <= 180)
  150.     {
  151.         endX = column - LOWORD(topLeft) - (int)(1L * pSin[(endAngle - 90) / 5] * xRadius / 10000L);
  152.         endY = line - HIWORD(topLeft) + (int)(1L * pSin[(180 - endAngle) / 5] * yRadius / 10000L);
  153.     }
  154.     else if (endAngle <= 270)
  155.     {
  156.         endX = column - LOWORD(topLeft) - (int)(1L * pSin[(270 - endAngle) / 5] * xRadius / 10000L);
  157.         endY = line - HIWORD(topLeft) - (int)(1L * pSin[(endAngle - 180) / 5] * yRadius / 10000L);
  158.     }
  159.     else if (endAngle <= 360)
  160.     {
  161.         endX = column - LOWORD(topLeft) + (int)(1L * pSin[(endAngle - 270) / 5] * xRadius / 10000L);
  162.         endY = line - HIWORD(topLeft) - (int)(1L * pSin[(360 - endAngle) / 5] * yRadius / 10000L);
  163.     }
  164.  
  165.     HANDLE oldPen = SelectObject(hDC, CreatePen(PS_SOLID, 1, MapColor(palette, TRUE)));
  166.  
  167.     if (fill)
  168.     {
  169.         // Draw an filled ellipse.
  170.         if (palette->hatchStyle == HS_NONE)
  171.             oldBrush = SelectObject(hDC, CreateSolidBrush(MapColor(palette, FALSE)));
  172.         else
  173.             oldBrush = SelectObject(hDC, CreateHatchBrush(palette->hatchStyle, MapColor(palette, FALSE)));
  174.         if (startAngle == 0 && endAngle == 360)
  175.             ::Ellipse(hDC, left, top, right, bottom);
  176.         else
  177.             Pie(hDC, left, top, right, bottom, endX, endY, startX, startY);
  178.  
  179.         DeleteObject(SelectObject(hDC, oldBrush));
  180.     }
  181.     else
  182.         // Draw a line ellipse.
  183.         Arc(hDC, left, top, right, bottom, endX, endY, startX, startY);
  184.  
  185.     SetROP2(hDC, oldROP2);
  186.     DeleteObject(SelectObject(hDC, oldPen));
  187.     ReleaseDC(screenID, hDC);
  188. }
  189.  
  190. #pragma argsused
  191. void UI_MSWINDOWS_DISPLAY::Line(SCREENID screenID, int column1, int line1, int column2, int line2,
  192.     const UI_PALETTE *palette, int width, int xor, const UI_REGION *clipRegion)
  193. {
  194.     if (!screenID)
  195.         return;
  196.  
  197.     HDC hDC = GetDC(screenID);
  198.     int oldROP2 = SetROP2(hDC, (xor == TRUE) ? R2_XORPEN : R2_COPYPEN);
  199.  
  200.     // Set up the proper region.
  201.     DWORD topLeft = GetDCOrg(hDC);
  202.     column1 -= LOWORD(topLeft);
  203.     line1 -= HIWORD(topLeft);
  204.     column2 -= LOWORD(topLeft);
  205.     line2 -= HIWORD(topLeft);
  206.  
  207.     // Draw the line using the background palette color.
  208.     HANDLE oldPen = SelectObject(hDC, CreatePen(PS_SOLID, width, MapColor(palette, TRUE)));
  209.     MoveTo(hDC, column1, line1);
  210.     LineTo(hDC, column2, line2);
  211.  
  212.     SetROP2(hDC, oldROP2);
  213.     DeleteObject(SelectObject(hDC, oldPen));
  214.     ReleaseDC(screenID, hDC);
  215. }
  216.  
  217. #pragma argsused
  218. void UI_MSWINDOWS_DISPLAY::Polygon(SCREENID screenID, int numPoints,
  219.     const int *polygonPoints, const UI_PALETTE *palette, int fill, int xor,
  220.     const UI_REGION *clipRegion)
  221. {
  222.     if (!screenID)
  223.         return;
  224.  
  225.     HBRUSH oldBrush;
  226.  
  227.     HDC hDC = GetDC(screenID);
  228.     int oldROP2 = SetROP2(hDC, (xor == TRUE) ? R2_XORPEN : R2_COPYPEN);
  229.  
  230.     // Set up the coordinates.
  231.     DWORD topLeft = GetDCOrg(hDC);
  232.     LPPOINT tPolygon = new POINT[numPoints];
  233.     for (int i = 0; i < numPoints; i++)
  234.     {
  235.         tPolygon[i].x = polygonPoints[i*2] - LOWORD(topLeft);
  236.         tPolygon[i].y = polygonPoints[i*2+1] - HIWORD(topLeft);
  237.     }
  238.  
  239.     HANDLE oldPen = SelectObject(hDC, CreatePen(PS_SOLID, 1, MapColor(palette, TRUE)));
  240.  
  241.     if (fill)
  242.     {
  243.         // Draw an filled polygon.
  244.         if (palette->hatchStyle == HS_NONE)
  245.             oldBrush = SelectObject(hDC, CreateSolidBrush(MapColor(palette, FALSE)));
  246.         else
  247.             oldBrush = SelectObject(hDC, CreateHatchBrush(palette->hatchStyle, MapColor(palette, FALSE)));
  248.         ::Polygon(hDC, tPolygon, numPoints);
  249.         DeleteObject(SelectObject(hDC, oldBrush));
  250.     }
  251.     else
  252.         // Draw a line polygon.
  253.         Polyline(hDC, tPolygon, numPoints);
  254.  
  255.     delete tPolygon;
  256.  
  257.     SetROP2(hDC, oldROP2);
  258.     DeleteObject(SelectObject(hDC, oldPen));
  259.     ReleaseDC(screenID, hDC);
  260. }
  261.  
  262. #pragma argsused
  263. void UI_MSWINDOWS_DISPLAY::Rectangle(SCREENID screenID, int left, int top, int right,
  264.     int bottom,    const UI_PALETTE *palette, int width, int fill, int xor,
  265.     const UI_REGION *clipRegion)
  266. {
  267.     if (!screenID)
  268.         return;
  269.  
  270.     HBRUSH oldBrush;
  271.  
  272.     HDC hDC = GetDC(screenID);
  273.     int oldROP2 = SetROP2(hDC, (xor == TRUE) ? R2_XORPEN : R2_COPYPEN);
  274.  
  275.     // Set up the coordinates.
  276.     DWORD topLeft = GetDCOrg(hDC);
  277.     left -= LOWORD(topLeft);
  278.     top -= HIWORD(topLeft);
  279.     right -= LOWORD(topLeft);
  280.     bottom -= HIWORD(topLeft);
  281.  
  282.     HANDLE oldPen = SelectObject(hDC, CreatePen(PS_SOLID, width, MapColor(palette, (width == 0) ? FALSE : TRUE)));
  283.  
  284.     if (fill)
  285.     {
  286.         // Increase bottom and right by one (Rectangle does not include bottom
  287.         // and right lines).
  288.         right++;
  289.         bottom++;
  290.  
  291.         // Draw an filled rectangle.
  292.         if (palette->hatchStyle == HS_NONE)
  293.             oldBrush = SelectObject(hDC, CreateSolidBrush(MapColor(palette, FALSE)));
  294.         else
  295.             oldBrush = SelectObject(hDC, CreateHatchBrush(palette->hatchStyle, MapColor(palette, FALSE)));
  296.         ::Rectangle(hDC, left, top, right, bottom);
  297.         DeleteObject(SelectObject(hDC, oldBrush));
  298.     }
  299.     else
  300.     {
  301.         // Draw a line rectangle.
  302.         MoveTo(hDC, left, top);
  303.         LineTo(hDC, right, top);
  304.         LineTo(hDC, right, bottom);
  305.         LineTo(hDC, left, bottom);
  306.         LineTo(hDC, left, top);
  307.     }
  308.  
  309.     SetROP2(hDC, oldROP2);
  310.     DeleteObject(SelectObject(hDC, oldPen));
  311.     ReleaseDC(screenID, hDC);
  312. }
  313.  
  314. #pragma argsused
  315. void UI_MSWINDOWS_DISPLAY::RectangleXORDiff(const UI_REGION &oldRegion,
  316.     const UI_REGION &newRegion)
  317. {
  318.     // Stub for MS Windows.
  319. }
  320.  
  321. #pragma argsused
  322. void UI_MSWINDOWS_DISPLAY::RegionConvert(UI_REGION ®ion, USHORT *oldFlags, USHORT newFlags)
  323. {
  324.     if (FlagSet(*oldFlags, newFlags))
  325.         return;
  326.  
  327.     // These variables handle Text<-->Graphics coordinate problems.
  328.     *oldFlags |= newFlags;
  329.  
  330.     region.left *= cellWidth;
  331.     region.top *= cellHeight;
  332.     region.right = (region.right + 1) * cellWidth - 1;
  333.     region.bottom = (region.bottom + 1) * cellHeight - 1;
  334. }
  335.  
  336. #pragma argsused
  337. void UI_MSWINDOWS_DISPLAY::RegionMove(const UI_REGION &oldRegion, int newColumn, int newLine,
  338.     SCREENID oldScreenID, SCREENID newScreenID)
  339. {
  340.     // Stub ?
  341. }
  342.  
  343. #pragma argsused
  344. void UI_MSWINDOWS_DISPLAY::Text(SCREENID screenID, int left, int top,
  345.     const char *text, const UI_PALETTE *palette, int length,
  346.     int fill, int xor, const UI_REGION *clipRegion)
  347. {
  348.     if (!screenID)
  349.         return;
  350.  
  351.     // Set up the coordinates.
  352.     HDC hDC = GetDC(screenID);
  353.     int oldROP2 = SetROP2(hDC, (xor == TRUE) ? R2_XORPEN : R2_COPYPEN);
  354.  
  355.     DWORD topLeft = GetDCOrg(hDC);
  356.     left -= LOWORD(topLeft);
  357.     top -= HIWORD(topLeft);
  358.  
  359.     // Set up the text colors.
  360.     SetTextColor(hDC, MapColor(palette, TRUE));
  361.     SetBkColor(hDC, MapColor(palette, FALSE));
  362.  
  363.     // Draw the text.
  364.     if (length == -1)
  365.         length = lstrlen((LPSTR)text);
  366.     TextOut(hDC, left, top, (LPSTR)text, length);
  367.  
  368.     SetROP2(hDC, oldROP2);
  369.     ReleaseDC(screenID, hDC);
  370. }
  371.  
  372. int UI_MSWINDOWS_DISPLAY::TextHeight(const char *string, SCREENID screenID)
  373. {
  374.     if (!screenID)
  375.         return (-1);
  376.  
  377.     HDC hDC = GetDC(screenID);
  378.     
  379.     // Get the height of the string.
  380.     DWORD textExtent = GetTextExtent(hDC, (LPSTR)string, ui_strlen(string));
  381.     ReleaseDC(screenID, hDC);
  382.  
  383.     return (HIWORD(textExtent));
  384. }
  385.  
  386. int UI_MSWINDOWS_DISPLAY::TextWidth(const char *string, SCREENID screenID)
  387. {
  388.     if (!screenID)
  389.         return (-1);
  390.  
  391.     HDC hDC = GetDC(screenID);
  392.  
  393.     // Get the width of the string.
  394.     DWORD textExtent = GetTextExtent(hDC, (LPSTR)string, ui_strlen(string));
  395.     ReleaseDC(screenID, hDC);
  396.  
  397.     return (LOWORD(textExtent));
  398. }
  399.