home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / editors / eedraw / src / ed / eecreate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-03  |  11.1 KB  |  320 lines

  1. /*****************************************************************************
  2. *   Program to draw EE diagrams.                         *
  3. *                                         *
  4. * This module creates new structs.                         *
  5. *                                         *
  6. * Written by:  Gershon Elber            IBM PC Ver 1.0,    Oct. 1989    *
  7. *****************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12. #include <string.h>
  13. #include <dos.h>
  14. #include <dir.h>
  15. #include <alloc.h>
  16. #include <time.h>
  17. #include "Program.h"
  18. #include "EEModify.h"
  19. #include "EECreate.h"
  20. #include "EERedraw.h"
  21. #include "EELayer.h"
  22. #include "Primary.h"    /* used for basic default layer numbers */
  23.  
  24. static DrawPolylineStruct
  25.     *NewPolylineStruct = NULL;
  26.  
  27. static BooleanType CreateNewPolylineStructAux(DrawPolylineStruct *NewStruct,
  28.     int TypeFlag);
  29.  
  30. /*****************************************************************************
  31. * Routine called async. from the intr_lib tool kit when waiting for input    *
  32. * events. Tests if cursor is out of the Active window and if so pan it and   *
  33. * move the cursor to the center.                         *
  34. *****************************************************************************/
  35. void AutoPanActiveWindow(void)
  36. {
  37.     int DrawingWidth;
  38.     IntrCursorShapeStruct Cursor;
  39.  
  40.     if (!EEAutoPan) return;
  41.  
  42.     if (EEActiveBBox -> Xmax < GRCurrentCursorX) {       /* To the right. */
  43.     DrawingWidth = GRInvMapX(EEActiveBBox -> Xmax) -
  44.                        GRInvMapX(EEActiveBBox -> Xmin);/* In drawing space. */
  45.         IntrWndwUpdatePanning(EEActiveWindow -> IntrLibWindowID,
  46.                       DrawingWidth / 2, 0);
  47.     GRCurrentCursorX = (EEActiveBBox -> Xmax + EEActiveBBox -> Xmin) / 2;
  48.     }
  49.     else if (EEActiveBBox -> Xmin > GRCurrentCursorX) {     /* To the left. */
  50.     DrawingWidth = GRInvMapX(EEActiveBBox -> Xmax) -
  51.                        GRInvMapX(EEActiveBBox -> Xmin);/* In drawing space. */
  52.         IntrWndwUpdatePanning(EEActiveWindow -> IntrLibWindowID,
  53.                       -DrawingWidth / 2, 0);
  54.     GRCurrentCursorX = (EEActiveBBox -> Xmax + EEActiveBBox -> Xmin) / 2;
  55.     }
  56.     else if (EEActiveBBox -> Ymax < GRCurrentCursorY) {   /* To the bottom. */
  57.     DrawingWidth = GRInvMapY(EEActiveBBox -> Ymax) -
  58.                        GRInvMapY(EEActiveBBox -> Ymin);/* In drawing space. */
  59.         IntrWndwUpdatePanning(EEActiveWindow -> IntrLibWindowID,
  60.                       0, DrawingWidth / 2);
  61.     GRCurrentCursorY = (EEActiveBBox -> Ymax + EEActiveBBox -> Ymin) / 2;
  62.     }
  63.     else if (EEActiveBBox -> Ymin > GRCurrentCursorY) {      /* To the top. */
  64.     DrawingWidth = GRInvMapY(EEActiveBBox -> Ymax) -
  65.                        GRInvMapY(EEActiveBBox -> Ymin);/* In drawing space. */
  66.         IntrWndwUpdatePanning(EEActiveWindow -> IntrLibWindowID,
  67.                       0, -DrawingWidth / 2);
  68.     GRCurrentCursorY = (EEActiveBBox -> Ymax + EEActiveBBox -> Ymin) / 2;
  69.     }
  70.     else
  71.     return;
  72.  
  73.     IntrWndwPop(EEActiveWindow -> IntrLibWindowID, TRUE, FALSE);
  74.  
  75.     if (NewPolylineStruct != NULL) {
  76.     /* Find out where new position is in screen space. */
  77.         Cursor = *IntrGetCursorType();
  78.         Cursor.LastX = GRMapX(NewPolylineStruct -> Points[NewPolylineStruct ->
  79.                                 NumOfPoints * 2 - 2]);
  80.         Cursor.LastY = GRMapY(NewPolylineStruct -> Points[NewPolylineStruct ->
  81.                                 NumOfPoints * 2 - 1]);
  82.     IntrSetCursorType(&Cursor);
  83.     }
  84.  
  85.     IntrInputFlush();   /* Any other events occuring outside active window. */
  86. }
  87.  
  88. /*****************************************************************************
  89. * Routine to create new polyline struct.                     *
  90. *****************************************************************************/
  91. DrawGenericStruct *CreateNewPolylineStruct(int Width,int TypeFlag)
  92. {
  93.     DrawPolylineStruct *NewStruct =
  94.     (DrawPolylineStruct *) MyMalloc(sizeof(DrawPolylineStruct));
  95.  
  96.     NewStruct -> StructType = DRAW_POLYLINE_STRUCT_TYPE;
  97.     NewStruct -> Width = Width;
  98.  
  99.     if (CreateNewPolylineStructAux(NewStruct,TypeFlag))
  100.     return (DrawGenericStruct *) NewStruct;
  101.     else {
  102.     MyFree((VoidPtr) NewStruct);
  103.     return NULL;
  104.     }
  105. }
  106.  
  107. /*****************************************************************************
  108. * Aux routine to create new polyline struct.                     *
  109. *****************************************************************************/
  110. static BooleanType CreateNewPolylineStructAux(DrawPolylineStruct *NewStruct,
  111.     int TypeFlag)
  112. {
  113.     int x, y, *TempPoints,
  114.         AllocatedNumOfPoints = 5,
  115.         Quit = FALSE;
  116.     IntrCursorShapeStruct Cursor;
  117.     IntrEventType Event;
  118.  
  119.     NewStruct -> Pnext = NULL;
  120.     NewStruct -> NumOfPoints = 0;
  121.     NewStruct -> Points = (int *)
  122.     MyMalloc(sizeof(int) * AllocatedNumOfPoints * 2);
  123.  
  124.     /* Put the structure in the global struct list, so automatic panning     */
  125.     /* will draw it as well. It will be removed from EEDrawList before exit. */
  126.     NewStruct -> Pnext = EEActiveWindow -> EEDrawList;
  127.     EEActiveWindow -> EEDrawList = (DrawGenericStruct *) NewStruct;
  128.  
  129.     IntrPushCursorType();
  130.     Cursor.CursorType = INTR_CURSOR_CROSS;
  131.     Cursor.LastHV = EEHVLineDrawing;
  132.     Cursor.LastX = Cursor.LastY = 0;
  133.     IntrSetCursorType(&Cursor);
  134.  
  135.     IntrDrawMessage("Pick Polyline point:", EEPopUpForeColor, EEPopUpBackColor);
  136.  
  137.     PutCursorInActiveWindow();
  138.     IntrSetIdleFunction(AutoPanActiveWindow);
  139.  
  140.     NewPolylineStruct = NewStruct;            /* Make it globally available. */
  141.  
  142.     while (!Quit) {
  143.     Event = IntrGetEventWaitSA(&x, &y);
  144.     Cursor.LastX = x;
  145.     Cursor.LastY = y;
  146.     x = EE_SNAP(GRInvMapX(x));
  147.     y = EE_SNAP(GRInvMapY(y));
  148.  
  149.     if (NewStruct -> NumOfPoints > 0) {
  150.         if(TypeFlag==FALSE)
  151.             NewStruct -> Layer = ReturnCurrentLayer();
  152.         else
  153.         NewStruct -> Layer = LAYER_WIRE;
  154.         if (EEHVLineDrawing) {
  155.             /* Coerce the line to vertical or horizontal one: */
  156.             if (ABS(x - NewStruct -> Points[NewStruct -> NumOfPoints * 2 - 2]) <
  157.             ABS(y - NewStruct -> Points[NewStruct -> NumOfPoints * 2 - 1]))
  158.             x = NewStruct -> Points[NewStruct -> NumOfPoints * 2 - 2];
  159.             else
  160.             y = NewStruct -> Points[NewStruct -> NumOfPoints * 2 - 1];
  161.         }
  162.         }
  163.  
  164.     switch (Event) {
  165.         case INTR_EVNT_ABORT:
  166.         if (NewStruct -> NumOfPoints > 0) {
  167.             RedrawPolylineStruct(NewStruct, GR_COPY_PUT, EE_ERASE_COLOR);
  168.             if (--NewStruct -> NumOfPoints > 0) {
  169.             RedrawPolylineStruct(NewStruct, GR_COPY_PUT, EE_DRAW_COLOR);
  170.             Cursor.LastX = GRMapX(NewStruct ->
  171.                                 Points[NewStruct -> NumOfPoints * 2 - 2]);
  172.             Cursor.LastY = GRMapY(NewStruct ->
  173.                                 Points[NewStruct -> NumOfPoints * 2 - 1]);
  174.             }
  175.         }
  176.         else
  177.             Quit = TRUE;
  178.         break;
  179.         case INTR_EVNT_SELECT:
  180.         if (NewStruct -> NumOfPoints > 0 &&
  181.             NewStruct -> Points[NewStruct -> NumOfPoints * 2 - 2] == x &&
  182.             NewStruct -> Points[NewStruct -> NumOfPoints * 2 - 1] == y) {
  183.             /* If same point as last one - quit, we are done. */
  184.             Quit = TRUE;
  185.             break;
  186.         }
  187.  
  188.         if (NewStruct -> NumOfPoints >= AllocatedNumOfPoints) {
  189.             /* Reallocate into the double the space: */
  190.             TempPoints = (int *)
  191.             MyMalloc(sizeof(int) * AllocatedNumOfPoints * 4);
  192.             GEN_COPY(TempPoints, NewStruct -> Points,
  193.                 sizeof(int) * AllocatedNumOfPoints * 2);
  194.             MyFree((VoidPtr) NewStruct -> Points);
  195.             AllocatedNumOfPoints *= 2;
  196.             NewStruct -> Points = TempPoints;
  197.         }
  198.         NewStruct -> Points[NewStruct -> NumOfPoints * 2] = x;
  199.         NewStruct -> Points[NewStruct -> NumOfPoints++ * 2 + 1] = y;
  200.         Cursor.LastX = GRMapX(x);
  201.         Cursor.LastY = GRMapY(y);
  202.  
  203.         break;
  204.         case INTR_EVNT_MIDDLE_BUTTON:
  205.         Quit = TRUE;
  206.         break;
  207.     }
  208.  
  209.     if (NewStruct -> NumOfPoints > 0) {             /* Lets see it. */
  210.         Cursor.CursorType = INTR_CURSOR_CROSS_LAST;
  211.         IntrSetCursorType(&Cursor);
  212.         RedrawPolylineStruct(NewStruct, GR_COPY_PUT, 
  213.         ReturnLayerColor(NewStruct -> Layer));
  214. /*         RedrawPolylineStruct(NewStruct, GR_COPY_PUT, EE_DRAW_COLOR); */
  215.     }
  216.     else {
  217.         Cursor.CursorType = INTR_CURSOR_CROSS;
  218.         IntrSetCursorType(&Cursor);
  219.         }
  220.     }
  221.  
  222.     NewPolylineStruct = NULL;
  223.  
  224.     IntrSetIdleFunction(NULL);
  225.  
  226.     IntrEraseMessage();
  227.     IntrPopCursorType();
  228.  
  229.     /* Remove from global list. */
  230.     EEActiveWindow -> EEDrawList = EEActiveWindow -> EEDrawList -> Pnext;
  231.     NewStruct -> Pnext = NULL;
  232.  
  233.     if (NewStruct -> NumOfPoints < 2) {     /* Dont consider it a new polyline. */
  234.     MyFree((VoidPtr) NewStruct -> Points);
  235.     NewStruct -> Points = NULL;
  236.     return FALSE;
  237.     }
  238.     else
  239.     return TRUE;
  240. }
  241.  
  242. /*****************************************************************************
  243. * Routine to create new connection struct.                     *
  244. *****************************************************************************/
  245. DrawGenericStruct *CreateNewConnectionStruct(void)
  246. {
  247.     IntrCursorShapeStruct Cursor;
  248.     DrawConnectionStruct *NewStruct =
  249.     (DrawConnectionStruct *) MyMalloc(sizeof(DrawConnectionStruct));
  250.  
  251.     NewStruct -> StructType = DRAW_CONNECTION_STRUCT_TYPE;
  252.     NewStruct -> Pnext = NULL;
  253.  
  254.     IntrDrawMessage("Pick Connection point:", EEPopUpForeColor, EEPopUpBackColor);
  255.  
  256.     IntrPushCursorType();
  257.     Cursor.CursorType = INTR_CURSOR_CROSS;
  258.     IntrSetCursorType(&Cursor);
  259.  
  260.     PutCursorInActiveWindow();
  261.     IntrSetIdleFunction(AutoPanActiveWindow);
  262.  
  263.     if (IntrGetEventWaitSA(&NewStruct -> PosX, &NewStruct -> PosY) ==
  264.                             INTR_EVNT_SELECT) {
  265.     NewStruct -> Layer = LAYER_WIRE;    /* Use Wire Layer */
  266.     IntrSetIdleFunction(NULL);
  267.     NewStruct -> PosX = EE_SNAP(GRInvMapX(NewStruct -> PosX));
  268.     NewStruct -> PosY = EE_SNAP(GRInvMapY(NewStruct -> PosY));
  269.     RedrawConnectionStruct(NewStruct, GR_COPY_PUT,
  270.         ReturnLayerColor(NewStruct -> Layer));
  271.     /* RedrawConnectionStruct(NewStruct, GR_COPY_PUT, EE_DRAW_COLOR); */
  272.     IntrEraseMessage();
  273.     IntrPopCursorType();
  274.     return (DrawGenericStruct *) NewStruct;
  275.     }
  276.     else {
  277.     IntrSetIdleFunction(NULL);
  278.     MyFree((VoidPtr) NewStruct);
  279.     IntrEraseMessage();
  280.     IntrPopCursorType();
  281.     return NULL;
  282.     }
  283. }
  284.  
  285. /*****************************************************************************
  286. * Routine to create new text struct.                         *
  287. *****************************************************************************/
  288. DrawGenericStruct *CreateNewTextStruct(void)
  289. {
  290.     char Text[LINE_LEN];
  291.     DrawTextStruct *NewStruct =
  292.     (DrawTextStruct *) MyMalloc(sizeof(DrawTextStruct));
  293.  
  294.     NewStruct -> StructType = DRAW_TEXT_STRUCT_TYPE;
  295.     NewStruct -> Scale = EETextScale;
  296.     NewStruct -> Pnext = NULL;
  297.  
  298.     Text[0] = 0;
  299.     IntrQueryLine("Text:", Text, LINE_LEN - 1, EEPopUpFrameColor,
  300.                   EEPopUpBackColor, EEPopUpForeColor, EEWindowsFrameWidth,
  301.                   INTR_WNDW_PLACE_CENTER);
  302.  
  303.     NewStruct -> PosX = NewStruct -> PosY = -1;
  304.     if (strlen(Text) == 0 ||
  305.     !PlaceString(Text, &NewStruct -> Orient, NewStruct -> Scale,
  306.              &NewStruct -> PosX, &NewStruct -> PosY,
  307.                      INTR_WNDW_PLACE_CENTER)) {
  308.     MyFree((VoidPtr) NewStruct);
  309.     return NULL;
  310.     }
  311.     else {
  312.     NewStruct -> Layer = ReturnCurrentLayer();
  313.     NewStruct -> Text = strdup(Text);
  314.     RedrawTextStruct(NewStruct, GR_COPY_PUT, 
  315.         ReturnLayerColor(NewStruct -> Layer));
  316.     /* RedrawTextStruct(NewStruct, GR_COPY_PUT, EE_DRAW_COLOR); */
  317.     return (DrawGenericStruct *) NewStruct;
  318.     }
  319. }
  320.