home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / UserInterface / CSimpleTextView.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  10.0 KB  |  494 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "CWASTEEdit.h"
  20. #include "CSimpleTextView.h"
  21.  
  22. //#include <LTextModel.h>
  23. //#include <LTextSelection.h>
  24. //#include <LTextEditHandler.h>
  25. //#include <LTextElemAEOM.h>
  26. //#include <VStyleSet.h>
  27. //#include <VOTextModel.h>
  28.  
  29. //#include <StClasses.h>
  30. #include <UCursor.h>
  31.  
  32. #include "CSpellChecker.h"
  33.  
  34. #include "resgui.h"
  35.  
  36. //#include "Textension.h"
  37.  
  38. #pragma global_optimizer off
  39.  
  40.  
  41. Boolean                             CSimpleTextView::sInitialized     = false;
  42. Boolean                             CSimpleTextView::sHasTSM                 = false;
  43. WETSMPreUpdateUPP             CSimpleTextView::sPreUpdateUPP     = NewWETSMPreUpdateProc( CSimpleTextView::TSMPreUpdate );
  44. CSimpleTextView             *CSimpleTextView::sTargetView     = NULL;
  45.  
  46.  
  47. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  48. // ÑÑÑ     Construction/Destruction
  49. #pragma mark --- Construction/Destruction ---
  50.  
  51.  
  52. CSimpleTextView::CSimpleTextView()
  53.     {
  54.  
  55.         if ( !sInitialized )
  56.             Initialize();
  57.  
  58.         Assert_( false );    //    Must use parameters
  59.  
  60.     }
  61.  
  62.  
  63. CSimpleTextView::CSimpleTextView( LStream *inStream )
  64.  
  65.     :    CWASTEEdit( inStream )
  66.  
  67.     {
  68.         
  69.         if ( !sInitialized )
  70.             Initialize();
  71.  
  72. /*            
  73.         mTextParms = NULL;
  74.         
  75.         try
  76.             {
  77.             
  78.                 // This is the first run at a new text scheme.
  79.                 // The streaming parameters will probably change
  80.                 mTextParms = new SSimpleTextParms;
  81.                 inStream->ReadData(&mTextParms->attributes, sizeof(Uint16));
  82.                 inStream->ReadData(&mTextParms->wrappingMode, sizeof(Uint16));
  83.                 inStream->ReadData(&mTextParms->traitsID, sizeof(ResIDT));
  84.                 inStream->ReadData(&mTextParms->textID, sizeof(ResIDT));
  85.                 inStream->ReadData(&mTextParms->pageWidth, sizeof(Int32));
  86.                 inStream->ReadData(&mTextParms->pageHeight, sizeof(Int32));
  87.                 inStream->ReadData(&mTextParms->margins, sizeof(Rect));
  88.             
  89.             }
  90.         
  91.         catch ( ... )
  92.             {
  93.             
  94.                 // this is either a streaming error or we couldn't get the memory for
  95.                 // a text parm structure, in which case you're totally snookered.
  96.                 delete mTextParms;
  97.                 throw;
  98.             
  99.             }
  100. */            
  101.     }
  102.  
  103.  
  104. CSimpleTextView::~CSimpleTextView()
  105.     {
  106.     
  107.     }
  108.  
  109.  
  110. // Wrap fix page size.
  111. //  turn word wrap off
  112. //  set image size
  113.  
  114. // Wrap to frame
  115.  
  116. void 
  117. CSimpleTextView::FinishCreateSelf( void )
  118.     {
  119.  
  120. //    Assert_( mTextParms != NULL );
  121.  
  122. //  Install a pre-update UPP so we can focus the view 
  123. //  correctly before the TSM does it's magic.
  124.  
  125.         if ( sHasTSM )
  126.             WESetInfo( weTSMPreUpdate, &sPreUpdateUPP, GetWEHandle() );
  127.  
  128. //  Turn this feature off. In CWASTEEdit, unfortunately, it is always
  129. //  turned on.
  130.  
  131.         WEFeatureFlag( weFOutlineHilite, weBitClear, GetWEHandle() );
  132.         
  133. /*    
  134.         switch (mTextParms->wrappingMode)
  135.             {
  136.                 
  137.                 case wrapMode_WrapToFrame:
  138.  
  139.                     mTextParms->attributes |= textAttr_WordWrap;
  140.                     
  141.                 break;
  142.                 
  143.                 case wrapMode_FixedPage:
  144.                     
  145.                     Assert_(false);    // not yet implemented
  146.                     
  147.                 break;
  148.                 
  149.                 case wrapMode_FixedPageWrap:
  150.                     
  151.                     mTextParms->attributes &= ~textAttr_WordWrap;
  152.                     ResizeImageTo(mTextParms->pageWidth, mTextParms->pageHeight, false);
  153.                     
  154.                 break;
  155.                 
  156.             }
  157. */
  158.             
  159.         CWASTEEdit::FinishCreateSelf();
  160.  
  161.     }
  162.  
  163.  
  164. /*
  165. void CSimpleTextView::BuildTextObjects(LModelObject *inSuperModel)
  166. {
  167.     Assert_(mTextParms != NULL);
  168.     VTextView::BuildTextObjects(inSuperModel);
  169.  
  170.     StRecalculator    change(mTextEngine);
  171.  
  172.     mTextEngine->SetAttributes(mTextParms->attributes);
  173.  
  174.     SetInitialTraits(mTextParms->traitsID);
  175.     if (mTextParms->textID != 0)
  176.         SetInitialText(mTextParms->textID);
  177.         
  178.     mTextEngine->SetAttributes(mTextParms->attributes);    //    restore editable/selectable bits
  179.  
  180.     if (!(mTextParms->attributes & textAttr_Selectable))
  181.         SetSelection(NULL);
  182.         
  183.     mTextEngine->SetTextMargins(mTextParms->margins);
  184.     
  185.     VOTextModel* theModel = dynamic_cast<VOTextModel*>(GetTextModel());
  186.     theModel->GetStyleManager().SetBehavior(setBehavior_Ignore);
  187.     
  188.     delete mTextParms;
  189.     mTextParms = NULL;
  190. }
  191. */
  192.  
  193.  
  194. /*
  195. void CSimpleTextView::NoteOverNewThing(LManipulator *inThing)
  196. {
  197.     if (inThing)
  198.         {
  199.         switch(inThing->ItemType())
  200.             {
  201.             case kManipulator:
  202.                 UCursor::Reset();    //    ?
  203.                 break;
  204.  
  205.             case kSelection:
  206.                 if (mEventHandler && ((LDataDragEventHandler *)mEventHandler)->GetStartsDataDrags())
  207.                     {
  208.                     CursHandle theCursH = ::GetCursor(131);
  209.                     if (theCursH != nil)
  210.                         ::SetCursor(*theCursH);
  211.                     break;
  212.                     }
  213.                 else
  214.                     {
  215.                     //    fall through
  216.                     }
  217.  
  218.             case kSelectableItem:
  219.                 UCursor::Tick(cu_IBeam);    //    ?
  220.                 break;
  221.  
  222.             }
  223.         }
  224.     else
  225.         {
  226.         UCursor::Reset();
  227.         }
  228. }
  229. */
  230.  
  231.  
  232. Boolean 
  233. CSimpleTextView::ObeyCommand( CommandT inCommand, void *ioParam )
  234.     {
  235.     
  236.         switch (inCommand)
  237.             {
  238.         
  239.         #ifdef MOZ_SPELLCHK
  240.                 case cmd_CheckSpelling:
  241.                     do_spellcheck(NULL, NULL, this);
  242.                     return true;
  243.                     break;
  244.         #endif // MOZ_SPELLCHK
  245.                 
  246.                 default:
  247.                     return CWASTEEdit::ObeyCommand(inCommand, ioParam);
  248.             }
  249.     
  250.     }
  251.  
  252.  
  253. void
  254. CSimpleTextView::ClickSelf( const SMouseDownEvent &inMouseDown )
  255.     {
  256.     
  257. //  Make sure the target is set or WASTE will do weird things
  258. //  when you drag from it if it's not the target. (especially
  259. //  if you are typing in a non-Roman script.
  260.  
  261.         if ( !IsTarget() )
  262.             SwitchTarget( this );
  263.     
  264.         CWASTEEdit::ClickSelf( inMouseDown );
  265.         
  266.     }
  267.  
  268.  
  269. void
  270. CSimpleTextView::BeTarget()
  271.     {
  272.     
  273.         OSErr    result = noErr;
  274.         
  275.         sTargetView = this;
  276.         
  277.         if ( sHasTSM )
  278.             {
  279.             
  280.                 result = WEInstallTSMHandlers();
  281.                 Assert_( result == noErr );
  282.                 
  283.             }
  284.             
  285.         CWASTEEdit::BeTarget();
  286.             
  287.     }
  288.     
  289.     
  290. void
  291. CSimpleTextView::DontBeTarget()
  292.     {
  293.     
  294.         OSErr    result = noErr;
  295.         
  296.         sTargetView = NULL;
  297.         
  298.         if ( sHasTSM )
  299.             {
  300.             
  301.                 result = WERemoveTSMHandlers();
  302.                 Assert_( result == noErr );
  303.                 
  304.             }
  305.             
  306.         CWASTEEdit::DontBeTarget();
  307.     
  308.     }
  309.     
  310.     
  311. void CSimpleTextView::FindCommandStatus(     CommandT     inCommand, 
  312.                                                                                     Boolean     &outEnabled, 
  313.                                                                                     Boolean        &outUsesMark, 
  314.                                                                                     Char16        &outMark, 
  315.                                                                                     Str255         outName)
  316.     {
  317.     
  318.         switch (inCommand)
  319.             {
  320.         #ifdef MOZ_SPELLCHK
  321.                 case cmd_CheckSpelling:
  322.                     outEnabled = true;
  323.                     break;
  324.         #endif // MOZ_SPELLCHK
  325.                 
  326.                 default:
  327.                     CWASTEEdit::FindCommandStatus(inCommand, outEnabled, 
  328.                                             outUsesMark, outMark, outName);
  329.             }
  330.     
  331.     }
  332.  
  333.  
  334. void    CSimpleTextView::Save( const FSSpec    &inFileSpec )
  335.     {
  336.     
  337.         LFileStream    *file = new LFileStream( inFileSpec );
  338.     
  339.         if ( file != NULL ) 
  340.             {
  341.             
  342.                 file->CreateNewFile(emSignature, 'TEXT');
  343.                 file->OpenDataFork(fsRdWrPerm);
  344.             
  345. //  write the text data
  346.             
  347.                 Handle msgBody = GetTextHandle();
  348.                 UInt32 textLength = GetTextLength();
  349.             
  350.                 StHandleLocker lock( msgBody );
  351.                 file->WriteData( *msgBody, textLength );
  352.  
  353. //  Close File
  354.  
  355.                 file->CloseDataFork();    
  356.     
  357.             }
  358.             
  359.     }
  360.  
  361.  
  362. pascal void
  363. CSimpleTextView::TSMPreUpdate( WEReference inWE )
  364.     {
  365.     
  366. //  AHHHH!!! Why didn't these wankers allow us to pass in a context
  367. //  pointer? Now I have to maintain this stupid sTargetView item.
  368.  
  369.         Assert_( sTargetView != NULL );
  370.         
  371.         if ( sTargetView != NULL )
  372.             sTargetView->FocusDraw();    
  373.     
  374.     }
  375.     
  376.     
  377. #pragma mark --- Initialization ---
  378.  
  379.  
  380. void CSimpleTextView::SetInitialTraits(ResIDT inTextTraitsID)
  381. {
  382.     TextTraitsRecord    traits;
  383.     TextTraitsH                traitsH = UTextTraits::LoadTextTraits(inTextTraitsID);
  384.     TextStyle                    theTextStyle;
  385.     
  386.     if (traitsH)
  387.         traits = **traitsH;
  388.     else
  389.         UTextTraits::LoadSystemTraits(traits);
  390.  
  391. //    LStyleSet* defaultStyle = mTextEngine->GetDefaultStyleSet();
  392. //    defaultStyle->SetTextTraits(traits);
  393.         
  394.     theTextStyle.tsFont = traits.fontNumber;
  395.     theTextStyle.tsSize = traits.size;
  396.     theTextStyle.tsFace = traits.style;
  397.     theTextStyle.tsColor = traits.color;
  398.             
  399.     SetStyle( weDoAll, &theTextStyle );
  400.  
  401. //    SPoint32 theScrollUnit;
  402. //    theScrollUnit.v = defaultStyle->GetHeight();
  403. //    theScrollUnit.h = theScrollUnit.v;
  404. //    SetScrollUnit(theScrollUnit);
  405.  
  406. }
  407.  
  408.  
  409. void CSimpleTextView::SetInitialText(ResIDT inTextID)
  410. {
  411.  
  412.     StResource    textRsrc('TEXT', inTextID, false);
  413.  
  414.     if (textRsrc.mResourceH != nil)
  415.         {
  416.  
  417. //    mTextEngine->TextReplaceByHandle(LTextEngine::sTextAll, textRsrc.mResourceH);
  418. //    ((LTextSelection *)mSelection)->SetSelectionRange(LTextEngine::sTextStart);
  419.  
  420.             UseText( textRsrc.mResourceH );
  421.             SetSelection( 0, 0 );
  422.  
  423.         }
  424. }
  425.  
  426.  
  427. Boolean
  428. CSimpleTextView::IsReadOnly()
  429.     {
  430.     
  431.         Boolean        bResult            = false;
  432.         SInt16        flagStatus     = 0;
  433.         
  434.         flagStatus = FeatureFlag( weFReadOnly, weBitTest );
  435.         
  436.         if ( flagStatus > 0 )
  437.             bResult = true;
  438.             
  439.         return bResult;
  440.     
  441.     }
  442.  
  443.  
  444. void
  445. CSimpleTextView::SetReadOnly( const Boolean inFlag )
  446.     {
  447.     
  448.         if ( inFlag )
  449.         
  450.             FeatureFlag( weFReadOnly, weBitSet );
  451.         
  452.         else FeatureFlag( weFReadOnly, weBitClear );
  453.     
  454.     }
  455.     
  456.     
  457. void
  458. CSimpleTextView::Initialize()
  459.     {
  460.  
  461. //  Here we check for TSM presence.
  462.     
  463.         OSErr        result                        = noErr;
  464.         SInt32    gestaltResponse        = 0;
  465.         
  466.         Assert_( sInitialized == false );
  467.         
  468.         if ( sInitialized == false )
  469.             {
  470.             
  471.                 sInitialized = true;
  472.                 
  473.                  result = ::Gestalt( gestaltTSMgrVersion, &gestaltResponse );
  474.                   
  475.                 if ( (result == noErr) && (gestaltResponse >= 1) ) 
  476.                     {
  477.                         
  478.                         result = ::Gestalt( gestaltTSMTEAttr, &gestaltResponse );
  479.                 
  480.                         if ( (result == noErr) && ((gestaltResponse >> gestaltTSMTEPresent) & 1) )    
  481.                             {
  482.                             
  483.                                 sHasTSM = true;
  484.                                 
  485.                             }
  486.                             
  487.                     }
  488.                     
  489.             }
  490.             
  491.     }
  492.     
  493.     
  494.