home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / MacZoop 1.1 / More Classes / ZTextWindow.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-01  |  5.2 KB  |  344 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            ObjectMacZapp        -- a standard Mac OOP application template
  5. *
  6. *
  7. *
  8. *            ZTextWindow.cpp        -- a window that displays text files (uses TextEdit)
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1996, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21.  
  22.  
  23. #include    "ZTextWindow.h"
  24. #include    "ZErrors.h"
  25. #include    "ZDefines.h"
  26.  
  27.  
  28. ZTextWindow::ZTextWindow(ZCommander* aBoss, short windID, Boolean allowEditing)
  29.     : ZScroller(aBoss, windID, TRUE, TRUE)
  30. {
  31.     itsText = NULL;
  32.     isEditable = allowEditing;
  33. }
  34.  
  35. ZTextWindow::~ZTextWindow()
  36. {
  37.     if (itsText)
  38.         TEDispose(itsText);
  39. }
  40.  
  41.  
  42. void    ZTextWindow::InitZWindow()
  43. {
  44.     inherited::InitZWindow();
  45.     
  46.     MakeTextEdit();
  47. }
  48.  
  49. void    ZTextWindow::DrawContent()
  50. {
  51.     Rect    r;
  52.     
  53.     GetContentRect(&r);
  54.     TEUpdate(&r, itsText);
  55. }
  56.  
  57. void    ZTextWindow::ClickContent(Point mouse, short modifiers)
  58. {
  59.     if (isEditable)
  60.         TEClick(mouse, (modifiers & shiftKey), itsText);
  61. }
  62.  
  63. void    ZTextWindow::Activate()
  64. {
  65.     inherited::Activate();
  66.     TEActivate(itsText);
  67. }
  68.  
  69. void    ZTextWindow::Deactivate()
  70. {
  71.     TEDeactivate(itsText);
  72.     inherited::Deactivate();
  73. }
  74.  
  75.  
  76. void    ZTextWindow::Idle()
  77. {
  78.     Rect        content;
  79.     Point        mouse;
  80.     CursHandle    aCursor;
  81.     
  82.     if (isEditable)
  83.     {
  84.         TEIdle( itsText);
  85.         
  86.         GetContentRect(&content);
  87.         Focus();
  88.         GetMouse(&mouse);
  89.         
  90.         if (PtInRect(mouse, &content))
  91.         {
  92.             aCursor = GetCursor(iBeamCursor);
  93.             SetCursor(*aCursor);
  94.         }
  95.         else
  96.             SetCursor(&qd.arrow);
  97.     }
  98. }
  99.  
  100.  
  101. void    ZTextWindow::SetSize(short width, short height)
  102. {
  103.     inherited::SetSize(width,height);
  104.     RecalText();
  105.     Draw();
  106. }
  107.  
  108.  
  109. void    ZTextWindow::Zoom(short partCode)
  110. {
  111.     inherited::Zoom(partCode);
  112.     RecalText();
  113.     Draw();
  114. }
  115.  
  116.  
  117.  
  118. void    ZTextWindow::Scroll(short dH, short dV)
  119. {
  120.     TEPinScroll(dH, dV, itsText);
  121. }
  122.  
  123.  
  124.  
  125. void    ZTextWindow::Type(char theChar)
  126. {
  127.     if (isEditable)
  128.     {
  129.         TEKey(theChar, itsText);
  130.         
  131.         dirty = TRUE;
  132.     }
  133. }
  134.  
  135.  
  136. void    ZTextWindow::MakeTextEdit()
  137. {
  138.     Rect    destRect, viewRect;
  139.     
  140.     // the dest rect is the scrollbounds, but that hasn't been set yet.
  141.     // the view rect is the content area
  142.     
  143.     Focus();
  144.     
  145.     TextFont(monaco);
  146.     TextSize(9);
  147.     
  148.     emSpace = CharWidth('m');
  149.     GetContentRect(&viewRect);
  150.     
  151.     destRect = viewRect;
  152.     destRect.right = destRect.left + (emSpace * 255);
  153.     
  154.     FailNIL(itsText = TEStylNew(&destRect, &viewRect));
  155.     
  156.     SetBounds(&destRect);
  157.     RecalText();
  158. }
  159.  
  160.  
  161.  
  162. void    ZTextWindow::HandleCommand(short menuID, short itemID)
  163. {
  164.     if (menuID == kEditMenu && isEditable)
  165.     {
  166.         switch (itemID)
  167.         {
  168.             case kCutCmd:
  169.                 TECut( itsText );
  170.                 dirty = TRUE;
  171.                 break;
  172.             case kCopyCmd:
  173.                 TECopy( itsText );
  174.                 break;
  175.             case kPasteCmd:
  176.                 TEPaste( itsText );
  177.                 dirty = TRUE;
  178.                 break;
  179.             case kClearCmd:
  180.                 TEDelete( itsText );
  181.                 dirty = TRUE;
  182.                 break;
  183.         }
  184.     }
  185.  
  186.     inherited::HandleCommand(menuID, itemID);
  187. }
  188.  
  189.  
  190.  
  191. void    ZTextWindow::UpdateMenus()
  192. {
  193.     if (isEditable)
  194.     {
  195.         MenuHandle    editM = GetMHandle(kEditMenu);
  196.         
  197.         if (editM)
  198.         {
  199.             EnableItem(editM, kCutCmd);
  200.             EnableItem(editM, kCopyCmd);
  201.             EnableItem(editM, kClearCmd);
  202.         
  203.             EnableItem(editM, kPasteCmd);
  204.         }
  205.     }
  206.  
  207.     inherited::UpdateMenus();
  208. }
  209.  
  210.  
  211. void    ZTextWindow::GetContentRect(Rect* contents)
  212. {
  213.     inherited::GetContentRect(contents);
  214.     
  215.     contents->top += 2;
  216.     contents->left += 2;
  217. }
  218.  
  219.  
  220. void    ZTextWindow::OpenFile(OSType fType)
  221. {
  222.     FInfo    fi;
  223.     short    refNum;
  224.     long    pSize;
  225.     Handle    temp = NULL;
  226.  
  227.     if (macFile.vRefNum != kNoFile)
  228.     {
  229.         FailOSErr(FSpGetFInfo(&macFile, &fi));
  230.         
  231.         if (fi.fdType != 'TEXT')
  232.             FailOSErr(paramErr);
  233.             
  234.         FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum ));
  235.         
  236.         try
  237.         {
  238.             FailOSErr(GetEOF(refNum, &pSize));
  239.             if (pSize > kMaxTextSize)
  240.                 FailOSErr(kTextFileTooBigErr);
  241.                 
  242.             FailNIL(temp = NewHandle(pSize));    
  243.             
  244.             HLock(temp);
  245.             FailOSErr(FSRead(refNum, &pSize, *temp));
  246.             
  247.             // set the text in text edit
  248.             
  249.             TESetText(*temp, pSize, itsText);
  250.             HUnlock(temp);
  251.             
  252.             DisposeHandle(temp);
  253.             FSClose(refNum);
  254.         }
  255.         catch(OSErr err)
  256.         {
  257.             FSClose(refNum);
  258.             
  259.             if (temp)
  260.             {
  261.                 HUnlock(temp);
  262.                 DisposeHandle(temp);
  263.             }
  264.             throw err;
  265.         }
  266.     }    
  267.     inherited::OpenFile( fType );
  268.     
  269.     // set up the bounds, etc
  270.     
  271.     RecalText();
  272.     Draw();
  273. }
  274.  
  275.  
  276.  
  277. void    ZTextWindow::SaveFile()
  278. {
  279.     short        refNum;
  280.     long        pSize;
  281.     OSErr        theErr;
  282.     Handle        text = NULL;
  283.     char        tState;
  284.     
  285.     if (macFile.vRefNum != kNoFile)
  286.     {
  287.         theErr = FSpOpenDF(&macFile, fsCurPerm, &refNum);
  288.         
  289.         if (theErr == fnfErr)
  290.         {
  291.             FailOSErr(FSpCreate(&macFile, gMacZoopSignature, 'TEXT', 0));
  292.             FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum));
  293.         }
  294.         else
  295.             FailOSErr(theErr);
  296.         
  297.         try
  298.         {
  299.             FailNIL(text = (*itsText)->hText);
  300.             
  301.             pSize = GetHandleSize(text);
  302.                 
  303.             tState = HGetState(text);
  304.             HLock(text);
  305.             FailOSErr(FSWrite(refNum, &pSize, *text));
  306.             HSetState(text, tState);
  307.             
  308.             FailOSErr(SetEOF(refNum, pSize));
  309.             FSClose(refNum);    
  310.         }
  311.         catch(OSErr err)
  312.         {
  313.             FSClose(refNum);
  314.             if (text)
  315.                 HSetState(text, tState);
  316.             throw err;
  317.         }
  318.         inherited::SaveFile();
  319.     }
  320. }
  321.  
  322.  
  323. void    ZTextWindow::RecalText()
  324. {
  325.     Rect    content,tBounds;
  326.     short     textHeight;
  327.     short    lineHeight;
  328.     
  329.     GetContentRect(&content);
  330.     (*itsText)->viewRect = content;
  331.     
  332.     TECalText(itsText);    
  333.     
  334.     textHeight = TEGetHeight(0, 32767 , itsText);
  335.     
  336.     GetBounds(&tBounds);
  337.     tBounds.bottom = textHeight;
  338.     SetBounds(&tBounds);
  339.     
  340.     (*itsText)->destRect = tBounds;
  341.     lineHeight = TEGetHeight(1, 1, itsText);
  342.     SetScrollAmount( emSpace, lineHeight);
  343. }
  344.