home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Programming / Programming Languages / UCB Logo 3.0 / sources / MiniEdit Folder / mini.windows.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-09  |  7.5 KB  |  346 lines  |  [TEXT/KAHL]

  1. /*********************************************************************
  2.  
  3.     mini.windows.c
  4.     
  5.     window functions for MiniEdit
  6.     
  7. *********************************************************************/
  8.  
  9. /*
  10.  
  11. #include <QuickDraw.h>
  12. #include <MacTypes.h>
  13. #include <WindowMgr.h>
  14. #include <TextEdit.h>
  15. #include <ControlMgr.h>
  16. #include <EventMgr.h>
  17.  
  18. */
  19.  
  20. #include "MiniEdit.h"
  21.  
  22. extern WindowRecord     wRecord;
  23. extern WindowPtr     myWindow;
  24. extern ControlHandle vScroll;
  25. extern TEHandle      TEH;
  26. extern int             linesInFolder;
  27.  
  28. static pascal Boolean    ClickLoopProc(void);
  29. static pascal Boolean    WordBreakProc(char *text, int charPos);
  30.  
  31. SetUpWindows()
  32. {
  33.     Rect    destRect, viewRect;
  34.     FontInfo    myInfo;
  35.     int        height;
  36.     Rect    vScrollRect;
  37.  
  38.     
  39.     SetPort((myWindow = GetNewWindow( windowID, &wRecord, (WindowPtr) -1L )));
  40.     TextFont(4);
  41.     TextSize(9);
  42.     vScrollRect = (*myWindow).portRect;
  43.     vScrollRect.left = vScrollRect.right-15;
  44.     vScrollRect.right += 1;
  45.     vScrollRect.bottom -= 14;
  46.     vScrollRect.top -= 1;
  47.     vScroll = NewControl( myWindow, &vScrollRect, "\p", 1, 0, 0, 0,
  48.         scrollBarProc, 0L);
  49.  
  50.     viewRect = myWindow->portRect; /* thePort->portRect; */
  51.     viewRect.right -= SBarWidth;
  52.     viewRect.bottom -= SBarWidth;
  53.     InsetRect(&viewRect, 4, 4);
  54.     TEH = TENew( &viewRect, &viewRect );
  55.     
  56.     SetWordBreak(WordBreakProc, TEH);
  57.     SetClikLoop(ClickLoopProc, TEH);
  58.  
  59.     SetView(myWindow);
  60. }
  61.  
  62. AdjustText ()
  63.  
  64. {
  65.     int        oldScroll, newScroll, delta;
  66.     
  67.     oldScroll = (**TEH).viewRect.top - (**TEH).destRect.top;
  68.     newScroll = GetCtlValue(vScroll) * (**TEH).lineHeight;
  69.     delta = oldScroll - newScroll;
  70.     if (delta != 0)
  71.       TEScroll(0, delta, TEH);
  72. }
  73.  
  74.  
  75. SetVScroll()
  76. {
  77.     register int    n;
  78.     
  79.     n = (**TEH).nLines-linesInFolder;
  80.  
  81.     if ((**TEH).teLength > 0 && (*((**TEH).hText))[(**TEH).teLength-1]=='\r')
  82.         n++;
  83.  
  84.     SetCtlMax(vScroll, n > 0 ? n : 0);
  85. }
  86.  
  87. ShowSelect()
  88.  
  89. {
  90.     register    int        topLine, bottomLine, theLine;
  91.     
  92.     SetVScroll();
  93.     AdjustText();
  94.     
  95.     topLine = GetCtlValue(vScroll);
  96.     bottomLine = topLine + linesInFolder;
  97.     
  98.     if ((**TEH).selStart < (**TEH).lineStarts[topLine] ||
  99.             (**TEH).selStart >= (**TEH).lineStarts[bottomLine]) {
  100.         for (theLine = 0; (**TEH).selStart >= (**TEH).lineStarts[theLine]; theLine++)
  101.             ;
  102.         SetCtlValue(vScroll, theLine - linesInFolder / 2);
  103.         AdjustText();
  104.     }
  105. }
  106.  
  107. SetView(w)
  108. WindowPtr w;
  109. {
  110.     (**TEH).viewRect = w->portRect;
  111.     (**TEH).viewRect.right -= SBarWidth;
  112.     (**TEH).viewRect.bottom -= SBarWidth;
  113.     InsetRect(&(**TEH).viewRect, 4, 4);
  114.     linesInFolder = ((**TEH).viewRect.bottom-(**TEH).viewRect.top)/(**TEH).lineHeight;
  115.     (**TEH).viewRect.bottom = (**TEH).viewRect.top + (**TEH).lineHeight*linesInFolder;
  116.     (**TEH).destRect.right = (**TEH).viewRect.right;
  117.     TECalText(TEH);
  118. }
  119.  
  120. UpdateWindow(theWindow)
  121. WindowPtr    theWindow;
  122. {
  123.     GrafPtr    savePort;
  124.     
  125.     GetPort( &savePort );
  126.     SetPort( theWindow );
  127.     BeginUpdate( theWindow );
  128.     EraseRect(&theWindow->portRect);
  129.     DrawControls( theWindow );
  130.     DrawGrowIcon( theWindow );
  131.     TEUpdate( &theWindow->portRect, TEH );
  132.     EndUpdate( theWindow );
  133.     SetPort( savePort );
  134. }
  135.  
  136. pascal void ScrollProc(theControl, theCode)
  137. ControlHandle    theControl;
  138. int                theCode;
  139. {
  140.     int    pageSize;
  141.     int    scrollAmt;
  142.     
  143.     if (theCode == 0)
  144.         return ;
  145.     
  146.     pageSize = ((**TEH).viewRect.bottom-(**TEH).viewRect.top) / 
  147.             (**TEH).lineHeight - 1;
  148.             
  149.     switch (theCode) {
  150.         case inUpButton: 
  151.             scrollAmt = -1;
  152.             break;
  153.         case inDownButton: 
  154.             scrollAmt = 1;
  155.             break;
  156.         case inPageUp: 
  157.             scrollAmt = -pageSize;
  158.             break;
  159.         case inPageDown: 
  160.             scrollAmt = pageSize;
  161.             break;
  162.         }
  163.     SetCtlValue( theControl, GetCtlValue(theControl)+scrollAmt );
  164.     AdjustText();
  165.  
  166. }
  167.  
  168. DoContent(theWindow, theEvent)
  169. WindowPtr    theWindow;
  170. EventRecord    *theEvent;
  171. {
  172.     int                cntlCode;
  173.     ControlHandle     theControl;
  174.     int                pageSize;
  175.     GrafPtr            savePort;
  176.     
  177.     GetPort(&savePort);
  178.     SetPort(theWindow);
  179.     GlobalToLocal( &theEvent->where );
  180.     if ((cntlCode = FindControl(theEvent->where, theWindow, &theControl)) == 0) {
  181.         if (PtInRect( theEvent->where, &(**TEH).viewRect ))
  182.             TEClick( theEvent->where, (theEvent->modifiers & shiftKey )!=0, TEH);
  183.     }
  184.     else if (cntlCode == inThumb) {
  185.         TrackControl(theControl, theEvent->where, 0L);
  186.         AdjustText();
  187.     }
  188.     else
  189.         TrackControl(theControl, theEvent->where, &ScrollProc);
  190.  
  191.     SetPort(savePort);
  192. }
  193.  
  194. MyGrowWindow( w, p )
  195. WindowPtr w;
  196. Point p;
  197. {
  198.     GrafPtr    savePort;
  199.     long    theResult;
  200.     int        oScroll;
  201.     Rect     r, oView;
  202.     
  203.     GetPort( &savePort );
  204.     SetPort( w );
  205.  
  206.     SetRect(&r, 80, 80, 2000, 2000); /* screenBits.bounds.right, screenBits.bounds.bottom */
  207.     theResult = GrowWindow( w, p, &r );
  208.     if (theResult == 0)
  209.       return;
  210.     SizeWindow( w, LoWord(theResult), HiWord(theResult), 1);
  211.  
  212.     InvalRect(&w->portRect);
  213.     oView = (**TEH).viewRect;
  214.     oScroll = GetCtlValue(vScroll);
  215.     
  216.     SetView(w);
  217.     HidePen();
  218.     MoveControl(vScroll, w->portRect.right - SBarWidth, w->portRect.top-1);
  219.     SizeControl(vScroll, SBarWidth+1, w->portRect.bottom - w->portRect.top-(SBarWidth-2));
  220.     ShowPen();
  221.  
  222.  
  223.     SetVScroll();
  224.     AdjustText();
  225.     
  226.     SetPort( savePort );
  227. }
  228.  
  229. MySizeWindow( w, h, v )
  230. WindowPtr w;
  231. int h, v;
  232. {
  233.     GrafPtr    savePort;
  234.     int        oScroll;
  235.     Rect     r, oView;
  236.     
  237.     GetPort( &savePort );
  238.     SetPort( w );
  239.  
  240.     SizeWindow( w, h, v, 1);
  241.  
  242.     InvalRect(&w->portRect);
  243.     oView = (**TEH).viewRect;
  244.     oScroll = GetCtlValue(vScroll);
  245.     
  246.     SetView(w);
  247.     HidePen();
  248.     MoveControl(vScroll, w->portRect.right - SBarWidth, w->portRect.top-1);
  249.     SizeControl(vScroll, SBarWidth+1, w->portRect.bottom - w->portRect.top-(SBarWidth-2));
  250.     ShowPen();
  251.  
  252.     SetVScroll();
  253.     AdjustText();
  254.     
  255.     SetPort( savePort );
  256. }
  257.  
  258. CloseMyWindow()
  259. {
  260.     HideWindow( myWindow );
  261.     TESetSelect( 0, (**TEH).teLength, TEH );
  262.     TEDelete( TEH );
  263.     SetVScroll();
  264. }
  265.  
  266. /*=============================================================================
  267.  TEClickLoop
  268.  
  269.         Function for auto-scrolling text
  270.  =============================================================================*/
  271.         
  272. static pascal Boolean    ClickLoopProc()
  273. {
  274.     Point        mouseLoc;
  275.     RgnHandle    theRegion;
  276.  
  277.     theRegion = NewRgn();
  278.     GetClip(theRegion);
  279.     ClipRect(&myWindow->portRect);
  280.     
  281.     GetMouse(&mouseLoc);
  282.     if (mouseLoc.v < 0) {
  283.         SetCtlValue( vScroll, GetCtlValue(vScroll)-1 );
  284.         AdjustText();
  285.     }
  286.     if (mouseLoc.v > (myWindow->portRect.bottom - 14)) {
  287.         SetCtlValue( vScroll, GetCtlValue(vScroll)+1 );
  288.         AdjustText();
  289.     }
  290.     
  291.     SetClip(theRegion);
  292.     DisposeRgn(theRegion);
  293.     return(TRUE);
  294. }
  295.  
  296. /*=============================================================================
  297.  TEWordBreak
  298.  
  299.  =============================================================================*/
  300.         
  301. static pascal Boolean    WordBreakProc(text, charPos)
  302. char    *text;
  303. int        charPos;
  304. {
  305.     char    theChar, unbold;
  306.     
  307.     theChar = text[charPos];
  308.     if (theChar < 0)
  309.         theChar += 128;
  310.     unbold = theChar - 128;
  311.     
  312.     if (theChar == '.' || theChar == '_' || theChar == '"' || theChar == ':' || theChar == '?' ||
  313.         unbold  == '.' || unbold  == '_' || unbold ==  '"' || unbold  == ':' || unbold  == '?')
  314.         return (FALSE);    /* don't break on these typical logo-word characters */
  315.     
  316.     if (theChar <= 47)    /* non-printing characters and symbols */
  317.         return(TRUE);
  318.     if (theChar <= 57)    /* digits */
  319.         return(FALSE);
  320.     if (theChar <= 64)    /* symbols */
  321.         return(TRUE);
  322.     if (theChar <= 90)    /* capital letters */
  323.         return(FALSE);
  324.     if (theChar <= 96)    /* symbols */
  325.         return(TRUE);
  326.     if (theChar <= 122)    /* lower-case letters */
  327.         return(FALSE);
  328.     if (theChar <= 127)    /* symbols */
  329.         return(TRUE);
  330.     if (theChar <= 159)    /* foreign letters */
  331.         return(FALSE);
  332.     if (theChar <= 175)    /* bold symbols */
  333.         return(TRUE);
  334.     if (theChar <= 185)    /* bold digits */
  335.         return(FALSE);
  336.     if (theChar <= 192)    /* bold symbols */
  337.         return(TRUE);
  338.     if (theChar <= 218)    /* bold capital letters */
  339.         return(FALSE);
  340.     if (theChar <= 224)    /* bold symbols */
  341.         return(TRUE);
  342.     if (theChar <= 250)    /* bold lower-case letters */
  343.         return(FALSE);
  344.     return(TRUE);        /* bold symbols */
  345. }
  346.