home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Applications / ZX Loader 1.0.4 / ZX Loader source / Console.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-26  |  7.7 KB  |  324 lines  |  [TEXT/MMCC]

  1.  
  2. /*    CONSOLE Window Handler
  3.     Copyright (C) 1993-1994 G.Woigk
  4.     
  5.     This file is part of the ZX LOADER program and it is free software
  6.     See Application.c for details
  7.             
  8.     This program is distributed in the hope that it will be useful,
  9.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  11.  
  12.     22.11.93:    Start of work on this file
  13.     28.6.94:    Check for 'Console open' modified
  14.     1.12.94:    Bilingual enhancement added (switch is in application.h)
  15. */
  16.  
  17. #include    "kio.h"
  18. #include    "mem.h"
  19. #include    "application.h"
  20. #include    "eventloop.h"
  21. #include    "console.h"
  22.  
  23. //    With any call of any function the console window is opened automatically
  24. //    line[] is used for window refresh
  25. #define        maxLines    40        
  26. #define        border    2            // border between text and window border
  27. #define        COFF    if((--crsr)==0)Crsr()
  28. #define        CON        if((crsr++)==0)Crsr()
  29. #define        COLD    if(crsr>0)Crsr()
  30. #define        titel    "\pLogbuch"
  31. #define        font    "\pMonaco"
  32. #define        fontSize    9    
  33. #define        tabWidth    8
  34. #define        conWindowID    128        // default 'WIND' resourceID
  35.  
  36. short        fontHeight;            // line spacing := size*4/3 will be calculated
  37. short        fontWidth;            // medium character width will be calculated
  38. WindowPtr    conWindow    = nil;
  39. short        col,row;            // cursor location (character coordinates)
  40. short        cols,rows;            // text columns & rows fitting in the window
  41. Rect        box;                // the windows contents rectangle in local coordinates
  42. short        crsr        = 0;    // is the cursor blob displayed? (yes, if crsr==1)
  43. Str255        line[maxLines+1];    // used for refresh
  44.  
  45.  
  46. // ----- prototypes ---------------------------------------------------------
  47. BBox ();
  48. ScrollScreen ();
  49. Crsr();
  50.  
  51.  
  52. // ----- calculate bounding rectangle of console window in local coord. -----
  53. BBox ()                // test for scroll bars needed ?
  54. {
  55.     memcpy ( &box, &conWindow->portRect, 8);
  56.     box.bottom -= (box.top+border+border); 
  57.     box.right  -= (box.left+border+border); 
  58.     box.left=0; 
  59.     box.top=0;
  60.     rows=box.bottom/fontHeight; if (rows>=maxLines) rows=maxLines-1;
  61.     cols=box.right/fontWidth;   if (cols>255) cols=255;
  62.     SetOrigin(-border,-border);
  63. }
  64.  
  65. // ----- scroll screen up one line --------------------------
  66. ScrollScreen ()
  67. {    RgnHandle h,u;
  68.     h = NewRgn();
  69.     u = NewRgn();
  70.     ScrollRect ( &box, 0, -fontHeight, h );    // scroll up
  71.     OffsetRgn ( ((WindowRecord*)conWindow)->updateRgn, 0, -fontHeight );
  72.     box.top=box.bottom-fontHeight;            // freshly unhidden line
  73.     RectRgn ( u, &box);                        // convert to Rgn
  74.     DiffRgn    ( h,u,h );                        // subtract new line from revealed Rgn
  75.     if (!EmptyRgn(h)) InvalRgn(h);            // something hidden revealed => refresh!
  76.     DisposeRgn(h);
  77.     DisposeRgn(u);
  78.     memmove ( line[0], line[1], sizeof(Str255)*(maxLines-1) );
  79.     line[maxLines-1][0]=0;
  80.     box.top=0;
  81. }
  82.  
  83. // ----- show/hide cursor blob -------------------------------
  84. Crsr()        // UP: show / hide blob depending on crsr-flag
  85. {    Point p;
  86.     Char c;
  87.     if (crsr) TextMode(notSrcCopy);
  88.     p=conWindow->pnLoc;
  89.     if (col>=line[row][0]) c=' '; else c=line[row][col+1];
  90.     DrawChar(c);
  91.     conWindow->pnLoc=p;
  92.     if (crsr) TextMode(srcCopy);
  93. }
  94.  
  95. ConCrsr(Boolean f)
  96. {
  97.     if ( (crsr==1)==f ) return;
  98.     crsr=f;
  99.     if (!conWindow) return;
  100.     SetPort(conWindow);
  101.     COLD;
  102. }
  103.  
  104. // ----- Clear/Open Console Window ---------------------------
  105. ConCLS()
  106. {    short i;
  107.     if (!conWindow)
  108.     {
  109.         if (conWindowID) conWindow = GetNewWindow (conWindowID,nil,(WindowPtr)-1);
  110.         if (!conWindow)
  111.         {    
  112.             box.left  = 20;
  113.             box.top   = 50;
  114.             box.right = box.left + 400;
  115.             box.bottom= box.top  + 200;
  116.             conWindow = NewWindow (nil, &box, titel, TRUE, 0, (WindowPtr)-1, TRUE, 0);
  117.             if (!conWindow) DoAbort("\pCould not create console window","\p","\p");
  118.         }
  119.     }
  120.         
  121.     SetPort ( conWindow );
  122.     ShowWindow ( conWindow );
  123.     SelectWindow ( conWindow );
  124.     GetFNum(font,&i);
  125.     TextFont(i);
  126.     TextMode(srcCopy);
  127.     TextSize(fontSize);
  128.     fontHeight=fontSize*4/3;
  129.     fontWidth=CharWidth('e');
  130.     for (i=0; i<maxLines; i++) line[i][0]=0;
  131.     BBox();
  132.     EraseRect(&box);
  133.     col=0;row=0;
  134.     MoveTo(0,fontSize);
  135.     COLD;
  136. }
  137.  
  138. // ----- Close Console Window --------------------------------
  139. ConClose ()
  140. {    
  141.     if (conWindow) DisposeWindow ( conWindow );
  142.     conWindow=nil;
  143. }
  144.  
  145. // ----- move cursor to begin of next line ----- may scroll ---------
  146. ConNL ()
  147. {    
  148.     if (!conWindow) ConCLS();
  149.     SetPort ( conWindow );
  150.     COFF;
  151.     if ( row+1>=rows )    ScrollScreen();
  152.     else                Move ( 0,fontHeight ), row++;
  153.     conWindow->pnLoc.h=0;
  154.     col=0;
  155.     CON;
  156. }
  157.  
  158. // ----- print a character ----- no control codes ------ may scroll ---------
  159. ConChar ( Char c )
  160. {    Char z;
  161.     Rect b;
  162.     if (!conWindow) ConCLS();
  163.     SetPort ( conWindow );
  164.     COFF;
  165.     if ( (col)>=cols ) ConNL();
  166.     DrawChar(c);col++;
  167.     z=line[row][col];
  168.     line[row][col]=c;
  169.     if (col>line[row][0]) line[row][0]=col;
  170.     else if (CharWidth(c)!=CharWidth(z)) 
  171.     {    b.left  = conWindow->pnLoc.h;
  172.         b.top   = fontHeight*row;
  173.         b.bottom= b.top+fontHeight;
  174.         b.right = box.right;
  175.         InvalRect(&b);
  176.     }
  177.     CON;
  178. }
  179.  
  180. // ----- print a string ----- no control codes ----- may scroll ------------
  181. ConPrint ( Str255 eng, Str255 ger )
  182. {    Rect b;
  183.     Char *s;
  184.     if (!conWindow) ConCLS();
  185.     SetPort (conWindow);
  186. #if german
  187.     if (ger) s=ger; else s=eng;
  188. #else
  189.     if (eng) s=eng; else s=ger;
  190. #endif
  191.  
  192.     if (col+s[0]>cols)
  193.     {    short n;
  194.         Str255 ss;
  195.         n=s[0]-(cols-col);    // characters in next line
  196.         ss[0]=cols-col;        // characters in this line
  197.         memcpy ( ss+1, s+1, ss[0] );
  198.         ConPrint(ss,0);
  199.         ConNL();
  200.         memcpy ( ss+1, s+1+ss[0], n );
  201.         ss[0]=n;
  202.         ConPrint(ss,0);
  203.         return;
  204.     }
  205.     
  206.     COFF;
  207.     DrawString(s);
  208.     memcpy ( line[row]+col+1, s+1, s[0] );
  209.     col=col+s[0];
  210.     if (col>line[row][0]) line[row][0]=col;
  211.     else
  212.     {    b.left  = conWindow->pnLoc.h;
  213.         b.top   = fontHeight*row;
  214.         b.bottom= b.top+fontHeight;
  215.         b.right = box.right;
  216.         InvalRect(&b);
  217.     }
  218.     CON;
  219. }
  220.  
  221. // ----- move cursor LEFT ------ stop at left border ------------------------
  222. ConLeft()
  223. {    char c;
  224.     if ( (!conWindow) || (col==0) ) return;
  225.     SetPort (conWindow);
  226.     COFF;
  227.     Move( -CharWidth(line[row][col--]),0 );
  228.     CON;
  229. }
  230.  
  231. // ----- move cursor RIGHT ----- stop at right border -----------------------
  232. ConRight()
  233. {    
  234.     if ( (!conWindow) || col>=cols ) return;
  235.     SetPort (conWindow);
  236.     COFF;
  237.     if (col>=line[row][0]) ConChar(' '); 
  238.     else Move ( CharWidth(line[row][++col]),0 );
  239.     CON;
  240. }
  241.  
  242. // ----- move cursor to next TAB position -----------------------------------
  243. ConTab()
  244. {    
  245.     ConHLocate ( (col+tabWidth)/tabWidth*tabWidth );
  246. }
  247.  
  248. // ----- locate cursor HORIZONTALLY -----------------------------------------
  249. ConHLocate(short h)
  250. {    short n;
  251.     if (!conWindow) return;
  252.     SetPort (conWindow);
  253.     COFF;
  254.     n=line[row][0];
  255.     if (h>n)
  256.     {    ConHLocate(n);
  257.         if (h>cols) h=cols;
  258.         for ( h-=n; h; h-- ) ConChar(' ');
  259.     }
  260.     else
  261.     {    if (h<0) h=0;
  262.         line[row][0]=h;
  263.         MoveTo ( StringWidth(line[row]), conWindow->pnLoc.v );
  264.         line[row][0]=n;
  265.         col=h;
  266.     }
  267.     CON;
  268. }
  269.  
  270. // ----- locate cursor ------------------------------------------------------
  271. ConLocate (short z, short s)
  272. {    
  273.     if (!conWindow) return;
  274.     SetPort (conWindow);
  275.     COFF;
  276.     if (z<0) row=0; else if (z>=rows) row=rows-1; else row=z;
  277.     MoveTo ( 0, fontSize+row*fontHeight );
  278.     col=0; ConHLocate (s);
  279.     CON;
  280. }
  281.  
  282. // ----- clear to end of line -----------------------------------------------
  283. ConCLEOL()
  284. {    Rect b;
  285.     if (!conWindow) return;
  286.     SetPort (conWindow);
  287.     line[row][0]=col;
  288.     b.left  = conWindow->pnLoc.h;
  289.     b.top   = fontHeight*row;
  290.     b.bottom= b.top+fontHeight;
  291.     b.right = box.right;
  292.     EraseRect(&b);
  293.     COLD;
  294. }
  295.  
  296. // ----- refresh screen -----------------------------------------------------
  297. ConRfsh ()
  298. {    Point p;
  299.     int i;
  300.     if (!conWindow) return;            // this should never happen
  301.     SetPort ( conWindow );
  302.     BBox();                            // recalculate contents box
  303.     EraseRect(&box);
  304.     if (row>=rows) {};                // more action needed after resize window ...
  305.     p = conWindow->pnLoc;
  306.     MoveTo(0,fontSize);
  307.     for ( i=0; i<rows; i++ )
  308.     {
  309.         DrawString(line[i]);        
  310.         conWindow->pnLoc.h=0;
  311.         conWindow->pnLoc.v += fontHeight;
  312.     }
  313.     conWindow->pnLoc = p;
  314.     COLD;
  315. }
  316.  
  317. // -----    Print string and move cursor to begin of next line ------------------------
  318. ConMsg(Str255 eng, Str255 ger) 
  319. {    ConPrint(eng,ger);
  320.     ConNL();
  321. }
  322.  
  323.  
  324.