home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / IRIT / IRITS.ZIP / WINDOWS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-05  |  15.9 KB  |  472 lines

  1. /*****************************************************************************
  2. *   "Irit" - the 3d polygonal solid modeller.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.2, Mar. 1990   *
  5. ******************************************************************************
  6. * Module to handle the windows used by the solid modeller.             *
  7. *****************************************************************************/
  8.  
  9. #ifdef __MSDOS__
  10. #include <graphics.h>
  11. #include <alloc.h>
  12. #include <conio.h>
  13. #endif /* __MSDOS__ */
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include "program.h"
  19. #include "graphgng.h"
  20. #include "windowsl.h"
  21. #include "windowsg.h"
  22. #include "viewobjg.h"
  23.  
  24. #ifndef __MSDOS__
  25. #include "xgraphic.h"
  26. #endif /* __MSDOS__ */
  27.  
  28. #ifdef __MSDOS__
  29.  
  30. InputWindowStruct InputWindow =                    /* Input window. */
  31. {   (int) INPUT_WINDOW_NAME,
  32.     IW_MIN_X, IW_MAX_X, IW_MIN_Y, IW_MAX_Y,
  33.     IW_FRAME_COLOR, IW_LINE_COLOR, IW_TEXT_COLOR,
  34.     IW_NUM_OF_LINES,
  35.     IW_FIRST_LINE_X, IW_FIRST_LINE_Y, IW_DIFF_Y_LINE
  36. };
  37.  
  38. StatusWindowStruct StatusWindow =               /* Status window. */
  39. {   (int) STATUS_WINDOW_NAME,
  40.     SW_MIN_X, SW_MAX_X, SW_MIN_Y, SW_MAX_Y,
  41.     SW_FRACE_COLOR, SW_LINE_COLOR, SW_TEXT_COLOR,
  42.     SW_NUM_OF_LINES,
  43.     SW_FIRST_LINE_X, SW_FIRST_LINE_Y, SW_DIFF_Y_LINE
  44. };
  45.  
  46. ViewWindowStruct ViewWindow =                     /* View window. */
  47. {   (int) VIEW_WINDOW_NAME,
  48.     VW_MIN_X, VW_MAX_X, VW_MIN_Y, VW_MAX_Y,
  49.     VW_FRACE_COLOR, VW_LINE_COLOR, VW_TEXT_COLOR,
  50.     VW_NUM_OF_LINES,
  51.     VW_FIRST_LINE_X, VW_FIRST_LINE_Y, VW_DIFF_Y_LINE
  52. };
  53.  
  54. struct GenerWindowStruct *GlblInputWindow, *GlblStatusWindow, *GlblViewWindow;
  55.  
  56. /* Set it to FALSE if the status window is used for other purpose (such as */
  57. /* for interactive transformation (INTERACT command).               */
  58. static int UpdateStatusWindow = FALSE;
  59.  
  60. #else
  61. #define IRIT_PROMPT    "Irit> "
  62. #endif /* __MSDOS__ */
  63.  
  64. static void WndwDrawWindowFrame(GenerWindowStruct *Window);
  65.  
  66. /*****************************************************************************
  67. *  Routine to setup all the window to zero state, assuming graphic mode.     *
  68. *****************************************************************************/
  69. void WndwSetUpAllWndws(void)
  70. {
  71.     int i;
  72.     char Line[LINE_LEN_LONG];
  73.  
  74.     GGClearAllScreen();
  75.  
  76.     WndwDrawAllWndwFrames();             /* Draw the windows frames. */
  77.  
  78. #ifdef __MSDOS__
  79.     for (i=0; i<InputWindow.NumOfLines; i++)      /* Clear all text in them. */
  80.     strcpy(InputWindow.Lines[i], "");
  81.     for (i=0; i<StatusWindow.NumOfLines; i++)
  82.     strcpy(StatusWindow.Lines[i], "");
  83.     for (i=0; i<ViewWindow.NumOfLines; i++)
  84.     strcpy(ViewWindow.Lines[i], "");
  85.  
  86.     strcpy(StatusWindow.Lines[0], "Irit");
  87.     strcpy(StatusWindow.Lines[2], VERSION);
  88.     strcpy(StatusWindow.Lines[5], "Core Left");
  89.  
  90.     /* Display the entry messages on the view window: */
  91.     for (i=0; i<(78 - strlen(PrgmHeader)) / 2; i++) Line[i] = ' '; Line[i] = 0;
  92.     strcat(Line, PrgmHeader);
  93.     WndwInputWindowPutStr(Line, NO_COLOR);
  94.     WndwInputWindowPutStr("", NO_COLOR);
  95.  
  96.     for (i=0; i<(78 - strlen(CopyRight)) / 2; i++) Line[i] = ' '; Line[i] = 0;
  97.     strcat(Line, CopyRight);
  98.     WndwInputWindowPutStr(Line, NO_COLOR);
  99.     WndwInputWindowPutStr("", NO_COLOR);
  100.  
  101.     for (i=0; i<(78 - strlen(AuthorName)) / 2; i++) Line[i] = ' '; Line[i] = 0;
  102.     strcat(Line, AuthorName);
  103.     WndwInputWindowPutStr(Line, NO_COLOR);
  104.  
  105.     UpdateStatusWindow = TRUE;
  106.     WndwStatusWindowDraw();  /* Update the status window - beginning status. */
  107.  
  108.     GlblInputWindow =  (GenerWindowStruct *) &InputWindow;
  109.     GlblStatusWindow = (GenerWindowStruct *) &StatusWindow;
  110.     GlblViewWindow =   (GenerWindowStruct *) &ViewWindow;
  111. #endif /* __MSDOS__ */
  112. }
  113.  
  114. /*****************************************************************************
  115. *  Routine to draw all the frames of all the windows defined in the system:  *
  116. *****************************************************************************/
  117. void WndwDrawAllWndwFrames(void)
  118. {
  119. #ifdef __MSDOS__
  120.     WndwDrawWindowFrame((GenerWindowStruct *) &InputWindow);
  121.     WndwDrawWindowFrame((GenerWindowStruct *) &StatusWindow);
  122.     WndwDrawWindowFrame((GenerWindowStruct *) &ViewWindow);
  123. #endif /* __MSDOS__ */
  124. }
  125.  
  126. /*****************************************************************************
  127. *  Routine to draw a frame for the window given:                 *
  128. *****************************************************************************/
  129. static void WndwDrawWindowFrame(GenerWindowStruct *Window)
  130. {
  131.     int i;
  132.  
  133.     RealType MinX = Window -> MinX, MaxX = Window -> MaxX,
  134.          MinY = Window -> MinY, MaxY = Window -> MaxY,
  135.          Dx = FRAME_X_WIDTH / 10.0, Dy = FRAME_Y_WIDTH / 10.0;
  136.  
  137.     GGMySetColor(Window->FrameColor);
  138.  
  139.     for (i=0; i<10; i++) {
  140.     GGMyMove(MinX, MinY);
  141.     GGMyDraw(MinX, MaxY);
  142.     GGMyDraw(MaxX, MaxY);
  143.     GGMyDraw(MaxX, MinY);
  144.     GGMyDraw(MinX, MinY);
  145.  
  146.     MinX -= Dx;
  147.     MinY -= Dy;
  148.     MaxX += Dx;
  149.     MaxY += Dy;
  150.     }
  151. }
  152.  
  153. /*****************************************************************************
  154. *  Routine to clear the given window interior:                     *
  155. *****************************************************************************/
  156. void WndwClearWindow(GenerWindowStruct *Window)
  157. {
  158.     GGClearWindow(Window -> MinX, Window -> MinY,
  159.           Window -> MaxX, Window -> MaxY, Window -> WindowName);
  160. }
  161.  
  162. /*****************************************************************************
  163. *  Routine to wait for user keystroke while drawing blinking cursor:         *
  164. * No data is returned - used as pause only.                     *
  165. *****************************************************************************/
  166. void WndwPause(RealType *R)
  167. {
  168. #ifdef __MSDOS__
  169.     if (!APX_EQ(*R, 0.0)) while (kbhit()) getch();         /* Flush stdin. */
  170.  
  171.     WndwInputWindowPause(InputWindow.FirstLineX,
  172.              InputWindow.FirstLineY - InputWindow.DiffYLine *
  173.                           InputWindow.NumOfLines);
  174. #else
  175.     fprintf(stderr, "Type return to continue:");
  176.     getchar();
  177. #endif /* __MSDOS__ */
  178. }
  179.  
  180. /*****************************************************************************
  181. *  Routine to wait for user keystroke while drawing blinking cursor and         *
  182. * returning that keystroke.                             *
  183. *****************************************************************************/
  184. char WndwInputWindowPause(double X, double Y)
  185. {
  186. #ifdef __MSDOS__
  187.     int i;
  188.  
  189.     GGWindowViewPort(InputWindow.MinX, InputWindow.MinY,
  190.              InputWindow.MaxX, InputWindow.MaxY,
  191.              (int) INPUT_WINDOW_NAME);
  192.  
  193.     do {
  194.     GGMySetColor(MAGENTA);
  195.     if (!kbhit()) GGXYPutStr(X, Y, "\x0A");
  196.     for (i=0; i<DelayConstant; i++) if (kbhit()) break;
  197.     GGMySetColor(BLACK);
  198.     GGXYPutStr(X, Y, "\x0A");
  199.  
  200.     GGMySetColor(MAGENTA);
  201.     if (!kbhit()) GGXYPutStr(X, Y, "\x09");
  202.     for (i=0; i<DelayConstant; i++) if (kbhit()) break;
  203.     GGMySetColor(BLACK);
  204.     GGXYPutStr(X, Y, "\x09");
  205.     } while (!kbhit());
  206.  
  207.     return getch();                 /* Discard the typed chars. */
  208. #else
  209.     fprintf(stderr, "Type return to continue:");
  210.     getchar();
  211. #endif /* __MSDOS__ */
  212. }
  213.  
  214. /*****************************************************************************
  215. *  Routine to handle the text on the Input Window. This routine calls the    *
  216. * regular WndwInputWindowPutStr routine (below) but pauses every screen full *
  217. * of data. if Reset is TRUE then number of lines on screen count is cleared. *
  218. *  Note it is exactly the same as WndwInputWindowPutStr in non MSDOS system. *
  219. *****************************************************************************/
  220. void WndwInputWindowPutStrFS(char *str, int Color, int Reset)
  221. {
  222. #ifdef __MSDOS__
  223.     static LineCount = 0;
  224.  
  225.     if (Reset) LineCount = 0;
  226.  
  227.     if (LineCount == GlblInputWindow -> NumOfLines) {           /* Pause. */
  228.     WndwInputWindowPause(InputWindow.MaxX - 0.04,
  229.                  InputWindow.FirstLineY -
  230.                  InputWindow.DiffYLine *
  231.                  InputWindow.NumOfLines);
  232.     LineCount = 0;
  233.     }
  234.     LineCount++;
  235.  
  236. #endif /* __MSDOS__ */
  237.  
  238.     WndwInputWindowPutStr(str, Color);
  239. }
  240.  
  241. /*****************************************************************************
  242. *  Routine to toggle the input window log file printing. If turned on, test  *
  243. * is made if file has been opened and if not open it.                 *
  244. *****************************************************************************/
  245. void WndwLogPrint(RealType *Set)
  246. {
  247.     char s[LINE_LEN];
  248.  
  249.     if (APX_EQ(*Set, 0.0)) {
  250.     PrintLogFile = FALSE;
  251.     fflush(LogFile);
  252.     return;
  253.     }
  254.  
  255.     if (LogFile == NULL) {              /* Not open yet - open it now: */
  256.     if ((LogFile = fopen(LogFileName, "w")) == NULL) {
  257.         sprintf(s, "Failed to open log file \"%s\"", LogFileName);
  258.         WndwInputWindowPutStr(s, RED);
  259.         return;
  260.     }
  261.     PrintLogFile = TRUE;
  262.     }
  263. }
  264.  
  265. /*****************************************************************************
  266. *  Routine to handle the text on the Input Window. This window echoes all    *
  267. * the input steam - the input parser input. Errors or information is also    *
  268. * displayed in this window.                             *
  269. *****************************************************************************/
  270. void WndwInputWindowPutStr(char *str, int Color)
  271. {
  272.     char c;
  273.     int i;
  274.  
  275.     for (i=0; i<strlen(str); i++)
  276.     if (str[i] == TAB) str[i] = ' ';        /* Strip off tabs... */
  277.     for (i=strlen(str); isspace(str[--i]););         /* Strip off CR/LF. */
  278.     if (str[i+1] != 0) str[i+1] = 0;
  279.  
  280.     if (PrintLogFile) fprintf(LogFile, "%s\n", str);
  281.  
  282. #ifdef __MSDOS__
  283.     if (kbhit())            /* Test and handle Control S/Q protocol. */
  284.     if ((c = getch()) == '\x13')                /* Its Ctrl - s. */
  285.         while (getch() != '\x11');               /* Wait for Ctrl - q. */
  286.     else ungetch(c);
  287.  
  288.     for (i=1; i<InputWindow.NumOfLines; i++)
  289.     strcpy(InputWindow.Lines[i-1], InputWindow.Lines[i]);
  290.     strncpy(InputWindow.Lines[InputWindow.NumOfLines - 1], str, LINE_LEN-1);
  291.  
  292.     /* Now update the screen content itself: */
  293.     WndwClearWindow((GenerWindowStruct *) &InputWindow);
  294.     GGMySetColor(InputWindow.TextColor);
  295.     for (i=0; i<InputWindow.NumOfLines; i++) {
  296.     if (i == InputWindow.NumOfLines-1 && Color != NO_COLOR)
  297.         GGMySetColor(Color);    /* Make last line in given color */
  298.     GGXYPutStr(InputWindow.FirstLineX,
  299.            InputWindow.FirstLineY - InputWindow.DiffYLine * i,
  300.            InputWindow.Lines[i]);
  301.     }
  302. #else
  303.     printf("%s\n", str);
  304. #endif /* __MSDOS__ */
  305. }
  306.  
  307. /*****************************************************************************
  308. *  Routine to handle reading one line from stdin into string Str, with max.  *
  309. * length Length in the Input Window.                         *
  310. *  Note Str may be non empty and can be used to fix wrong entry.         *
  311. *****************************************************************************/
  312. void WndwInputWindowGetStr(char *Str, int Length)
  313. {
  314. #ifdef __MSDOS__
  315.     GGWindowViewPort(InputWindow.MinX, InputWindow.MinY,
  316.              InputWindow.MaxX, InputWindow.MaxY,
  317.              (int) INPUT_WINDOW_NAME);
  318.  
  319.     GGGetGraphicLine(InputWindow.FirstLineX,
  320.              InputWindow.FirstLineY - InputWindow.DiffYLine *
  321.                           InputWindow.NumOfLines,
  322.              Str, Length, WHITE);
  323. #else
  324.     printf(IRIT_PROMPT);
  325.     fgets(Str, Length - 1, stdin);
  326.     if (feof(stdin)) MyExit(0);    /* Eof typed on keyboard (usually CtrlD). */
  327.     if (Str[strlen(Str) - 1] < ' ') Str[strlen(Str) - 1] = 0;   /* No CR/LF. */
  328.     puts(Str);
  329. #endif /* __MSDOS__ */
  330. }
  331.  
  332. #ifdef __MSDOS__
  333.  
  334. /*****************************************************************************
  335. *  Routine to claim/reclaim control on the status window, so that it would   *
  336. * not be updated. Used by other modules to display data on the status window *
  337. * temporary, such as the INTERACT command - objects transformations.         *
  338. *****************************************************************************/
  339. void WndwClaimStatus(void)
  340. {
  341.     UpdateStatusWindow = FALSE;
  342. }
  343.  
  344. void WndwReclaimStatus(void)
  345. {
  346.     UpdateStatusWindow = TRUE;
  347. }
  348.  
  349. /*****************************************************************************
  350. *  Routine to handle the status window static data - clear it and print the  *
  351. * static data. Note it calls in the end to print dynamic data also...         *
  352. *****************************************************************************/
  353. void WndwStatusWindowDraw(void)
  354. {
  355.     int i;
  356.     struct textsettingstype oldtext;
  357.  
  358.     if (!UpdateStatusWindow) return;
  359.  
  360.     gettextsettings(&oldtext);
  361.     settextjustify(CENTER_TEXT, CENTER_TEXT);       /* Draw strings centered. */
  362.  
  363.     /* Now update the screen content itself: */
  364.     WndwClearWindow((GenerWindowStruct *) &StatusWindow);
  365.  
  366.     GGMySetColor(StatusWindow.TextColor);
  367.  
  368.     for (i=0; i<StatusWindow.NumOfLines; i++) {
  369.     GGXYPutStr((StatusWindow.MinX + StatusWindow.MaxX) / 2.0,
  370.            StatusWindow.FirstLineY - StatusWindow.DiffYLine * i,
  371.            StatusWindow.Lines[i]);
  372.     }
  373.  
  374.     WndwStatusWindowUpdate();
  375.  
  376.     settextjustify(oldtext.horiz, oldtext.vert);
  377. }
  378.  
  379. /*****************************************************************************
  380. *  Routine to handle the status window dynamic update - only reprint new.    *
  381. *****************************************************************************/
  382. void WndwStatusWindowUpdate(void)
  383. {
  384.     static char *AdvanceChar = "-\\|/";
  385.     static int AdvanceCount = 0;
  386.     double x, y;
  387.     struct textsettingstype oldtext;
  388.  
  389.     if (!UpdateStatusWindow) return;
  390.  
  391.     gettextsettings(&oldtext);
  392.     settextjustify(CENTER_TEXT, CENTER_TEXT);       /* Draw strings centered. */
  393.  
  394.     /* Preper the new data to display: */
  395.     AdvanceCount = (AdvanceCount + 1) & 0x03;
  396.     sprintf(StatusWindow.Lines[7], "%c %3dk %c",
  397.     AdvanceChar[AdvanceCount],
  398.     (int) (coreleft()/1024L),
  399.     AdvanceChar[AdvanceCount]);
  400.  
  401.     GGMySetColor(StatusWindow.TextColor);
  402.  
  403.     x = (StatusWindow.MinX + StatusWindow.MaxX) / 2.0;
  404.     y = StatusWindow.FirstLineY - StatusWindow.DiffYLine * 7;
  405.  
  406.     /* Make sure the area is cleared before reprint: */
  407.     GGClearWindow(x - CORE_SIZE_X, y - CORE_SIZE_Y,
  408.           x + CORE_SIZE_X, y + CORE_SIZE_Y, STATUS_WINDOW_NAME);
  409.  
  410.     GGXYPutStr(x, y, StatusWindow.Lines[7]);
  411.  
  412.     settextjustify(oldtext.horiz, oldtext.vert);
  413. }
  414.  
  415. #endif /* __MSDOS__ */
  416.  
  417. /*****************************************************************************
  418. *  Routine to handle the view on the View Window. This routine displays      *
  419. * object(s) and prints its/their name also.                     *
  420. * If ClearWindow is not zero then the window is cleared first.             *
  421. *****************************************************************************/
  422. void WndwViewGeomObject(ObjectStruct *PObjList, RealType *ClearWindow)
  423. {
  424.     int i, ListNum = 0;
  425.     static int NumOfObjects = 0;/* Hold number of objects currently visible. */
  426.     static char Names[OBJ_NAME_LEN][VIEW_WNDW_MAX_OBJ_NAMES];
  427.     char Line[LINE_LEN];
  428.     struct ObjectStruct *PObj;
  429.  
  430.     if (!IS_OLST_OBJ(PObjList))
  431.     FatalError("WndwViewGeomObject: Not object list object!\n");
  432.  
  433.     if (!APX_EQ(*ClearWindow, 0.0)) {
  434. #ifdef __MSDOS__
  435.     WndwClearWindow((GenerWindowStruct *) &ViewWindow);  /* & view port. */
  436. #else
  437.         GGClearAllScreen();
  438. #endif /* __MSDOS__ */
  439.     NumOfObjects = 0;
  440.     }
  441.  
  442.     while ((PObj = PObjList -> U.PObjList[ListNum]) != NULL &&
  443.        ListNum++ < MAX_OBJ_LIST) {
  444.     if (!IS_GEOM_OBJ(PObj)) {
  445.         sprintf(Line, "Cannt display none geometric object %s, ignored",
  446.         PObj -> Name);
  447.         WndwInputWindowPutStr(Line, RED);
  448.         continue;
  449.     }
  450.     if (NumOfObjects < VIEW_WNDW_MAX_OBJ_NAMES) {
  451.         for (i=0; i<NumOfObjects; i++)
  452.         if (strcmp(Names[i], PObj->Name) == 0) break;
  453.         if (i >= NumOfObjects) { /* Its a new name - add it to top list. */
  454.         strcpy(Names[NumOfObjects++], PObj->Name);
  455.         GGMySetColor(GET_OBJECT_COLOR(PObj));
  456. #ifdef __MSDOS__
  457.         GGXYPutStr(ViewWindow.MinX +
  458.                    (ViewWindow.MaxX - ViewWindow.MinX) *
  459.                    NumOfObjects / (VIEW_WNDW_MAX_OBJ_NAMES + 1),
  460.                ViewWindow.MaxY - 0.05, PObj -> Name);
  461. #else
  462.         GGXYPutStr(((RealType) (2 * NumOfObjects)) /
  463.                        (VIEW_WNDW_MAX_OBJ_NAMES + 1.0) - 1.0,
  464.                0.9, PObj -> Name);
  465.  
  466. #endif /* _MSDOS__ */
  467.         }
  468.     }
  469.         ViewGeomObject(PObj);              /* And finally display it. */
  470.     }
  471. }
  472.