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