home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Muddweller 1.2 / source code / Main / CommandView.cp < prev    next >
Encoding:
Text File  |  1994-02-16  |  11.4 KB  |  477 lines  |  [TEXT/MPS ]

  1. /* CommandView - Implementation of TCommandView                               */
  2.  
  3. #include "CommandView.h"
  4.  
  5.  
  6.         // • Toolbox
  7. #ifndef __VALUES__
  8. #include "Values.h"
  9. #endif
  10.  
  11.  
  12.         // • Implementation use
  13. #ifndef __GLOBALS__
  14. #include "Globals.h"
  15. #endif
  16.  
  17. #ifndef __MUDDOC__
  18. #include "MUDDoc.h"
  19. #endif
  20.  
  21. #ifndef __USizerView__
  22. #include "USizerView.h"
  23. #endif
  24.  
  25.  
  26. //------------------------------------------------------------------------------
  27.  
  28. #pragma segment MACommandRes
  29.  
  30. class TSendCmd: public TCommand {
  31. public:
  32.     virtual pascal void DoIt (void);
  33.     virtual pascal void ISendCmd (TCommandView *itsView,CmdNumber itsCmdNumber);
  34. };
  35.  
  36.  
  37. pascal void TSendCmd::DoIt (void)
  38. {
  39.     switch (fCmdNumber) {
  40.     case cReturnKey:
  41.         ((TCommandView *) fView)->SendLine ();
  42.         break;
  43.     case cUpKey:
  44.         ((TCommandView *) fView)->ScrollHist (TRUE);
  45.         break;
  46.     case cDownKey:
  47.         ((TCommandView *) fView)->ScrollHist (FALSE);
  48.         break;
  49.     }
  50. }
  51.  
  52.  
  53. pascal void TSendCmd::ISendCmd (TCommandView *itsView, CmdNumber itsCmdNumber)
  54. {
  55.     ICommand (itsCmdNumber, NULL, itsView, NULL);
  56.     fCanUndo = FALSE;
  57.     fCausesChange = FALSE;
  58. }
  59.  
  60. //------------------------------------------------------------------------------
  61.  
  62. #pragma segment SMUDDocRes
  63.  
  64. class TCmdScrollBar: public TSScrollBar {
  65. public:
  66.     virtual pascal void Activate (Boolean entering);
  67. };
  68.  
  69.  
  70. pascal void TCmdScrollBar::Activate (Boolean )
  71. {
  72.     inherited::Activate (FALSE);
  73. }
  74.  
  75.  
  76. //------------------------------------------------------------------------------
  77.  
  78. #pragma segment SMUDDocRes
  79.  
  80. class TCmdScroller: public TScroller {
  81. public:
  82.     virtual pascal void CreateTemplateScrollBar (VHSelect itsDirection);
  83. };
  84.  
  85.  
  86. pascal void CreateHandler (short , long , void *thisObject)
  87. {
  88.     ((TCmdScrollBar *) thisObject)->Free ();
  89. }
  90.  
  91.  
  92. pascal void TCmdScroller::CreateTemplateScrollBar (VHSelect itsDirection)
  93. {
  94.     FailInfo fi;
  95.     TCmdScrollBar *aCmdScrollBar;
  96.  
  97.     CatchFailures (&fi, CreateHandler, this);
  98.     aCmdScrollBar = (TCmdScrollBar *) DoCreateViews (fDocument, fSuperView,
  99.         kCmdScrlID, &gZeroVPt);
  100.     aCmdScrollBar->fDirection = itsDirection;
  101.     aCmdScrollBar->fShown = IsShown ();
  102.     aCmdScrollBar->AttachScroller (this);
  103.     Success (&fi);
  104. }
  105.  
  106.  
  107. //------------------------------------------------------------------------------
  108.  
  109. #pragma segment SMUDDocRes
  110.  
  111. pascal void TCommandView::CalcMinSize (VPoint *minSize)
  112. {
  113.     inherited::CalcMinSize (minSize);
  114.     if (minSize->v < fSuperView->fSize.v) minSize->v = fSuperView->fSize.v;
  115. }
  116.  
  117. //------------------------------------------------------------------------------
  118.  
  119. #pragma segment SMUDDocRes
  120.  
  121. pascal Boolean TCommandView::ClikLoop (void)
  122. {
  123.     Boolean result;
  124.     TextStyle aTextStyle;
  125.     
  126.     result = inherited::ClikLoop ();
  127.     aTextStyle = fTextStyle;
  128.     SetPortTextStyle (&aTextStyle);
  129.     return result;
  130. }
  131.  
  132. //------------------------------------------------------------------------------
  133.  
  134. #pragma segment SMUDDocRes
  135.  
  136. pascal TCommand *TCommandView::DoKeyCommand (short ch, short aKeyCode,
  137.         EventInfo *info)
  138. {
  139.     TSendCmd *aSendCmd;
  140.     TLogWindow *aLogWindow;
  141.     MHandle mh;
  142.     TMacro *mac;
  143.  
  144.     mh = ((TMUDDoc *) fMyDocument)->fMacroList;
  145.     while (mh) {
  146.         if (((aKeyCode & 0xff) == (**mh).mCode) &&
  147.                 (info->theShiftKey == (**mh).mShift) &&
  148.                 (info->theOptionKey == (**mh).mOption) &&
  149.                 (info->theControlKey == (**mh).mControl))
  150.             break;
  151.         mh = (**mh).mNext;
  152.     }
  153.     if (mh) {
  154.         if (gDisableMenus || !((TMUDDoc *) fMyDocument)->fConnected ||
  155.                 ((TMUDDoc *) fMyDocument)->fClosing)
  156.             gApplication->Beep (0);
  157.         else {
  158.             mac = new TMacro;
  159.             FailNIL (mac);
  160.             mac->IMacro (fMyDocument, mh);
  161.             if (((TMUDDoc *) fMyDocument)->fRunList == mac) mac->DoIdle ();
  162.         }
  163.         return NULL;
  164.     } else {
  165.         switch (ch) {
  166.         case chReturn:
  167.         case chEnter:
  168.             if (info->theOptionKey)
  169.                 return inherited::DoKeyCommand (chReturn, aKeyCode, info);
  170.             else if (gDisableMenus || !((TMUDDoc *) fMyDocument)->fConnected ||
  171.                     ((TMUDDoc *) fMyDocument)->fClosing) {
  172.                 gApplication->Beep (0);
  173.                 return NULL;
  174.             } else {
  175.                 aSendCmd = new TSendCmd;
  176.                 FailNIL (aSendCmd);
  177.                 aSendCmd->ISendCmd (this, cReturnKey);
  178.                 return aSendCmd;
  179.             }
  180.         case chUp:
  181.             if (info->theOptionKey)
  182.                 return inherited::DoKeyCommand (ch, aKeyCode, info);
  183.             else {
  184.                 aSendCmd = new TSendCmd;
  185.                 FailNIL (aSendCmd);
  186.                 aSendCmd->ISendCmd (this, cUpKey);
  187.                 return aSendCmd;
  188.             }
  189.         case chDown:
  190.             if (info->theOptionKey)
  191.                 return inherited::DoKeyCommand (ch, aKeyCode, info);
  192.             else {
  193.                 aSendCmd = new TSendCmd;
  194.                 FailNIL (aSendCmd);
  195.                 aSendCmd->ISendCmd (this, cDownKey);
  196.                 return aSendCmd;
  197.             }
  198.         case chHome:
  199.         case chEnd:
  200.         case chPageDown:
  201.         case chPageUp:
  202.             aLogWindow = (TLogWindow *) GetWindow ();
  203.             return aLogWindow->fLogView->DoKeyCommand (ch, aKeyCode, info);
  204.         default:
  205.             return inherited::DoKeyCommand (ch, aKeyCode, info);
  206.         }
  207.     }
  208. }
  209.  
  210.  
  211. //------------------------------------------------------------------------------
  212.  
  213. #pragma segment TERes
  214.  
  215. pascal TCommand *TCommandView::DoMouseCommand (Point *theMouse, EventInfo *info,
  216.         Point *hysteresis)
  217. {
  218.     TLogWindow *aLogWindow;
  219.     
  220.     aLogWindow = (TLogWindow *) GetWindow ();
  221.     if (aLogWindow->fTarget != this) {
  222.         if (!info->theShiftKey && Focus ()) TESetSelect (0, 0, fHTE);
  223.         aLogWindow->SetTarget (this);
  224.     }
  225.     return inherited::DoMouseCommand (theMouse, info, hysteresis);
  226. }
  227.  
  228. //------------------------------------------------------------------------------
  229.  
  230. #pragma segment MAClose
  231.  
  232. pascal void TCommandView::Free (void)
  233. {
  234.     Handle *hp;
  235.     
  236.     if (fHandList != NULL ) {
  237.         hp = *fHandList;
  238.         for (int i = fMaxLines; i > 0; i--) DisposIfHandle (*hp++);
  239.         DisposIfHandle (fHandList);
  240.     }
  241.     inherited::Free ();
  242. }
  243.  
  244. //------------------------------------------------------------------------------
  245.  
  246. #pragma segment AOpen
  247.  
  248. pascal void TCommandView::IRes (TDocument *itsDocument, TView *itsSuperView,
  249.         Ptr *itsParams)
  250. {
  251.     Handle *hp;
  252.     Rect dest;
  253.     
  254.     fHandList = NULL;
  255.     fCurIndex = 0;
  256.     fFirstIndex = 0;
  257.     fMaxLines = 0;
  258.     inherited::IRes (NULL, itsSuperView, itsParams);
  259.         // fDocument = NULL prevents typing commands from setting fChangeCount
  260.     fMyDocument = itsDocument;
  261.     fInset.top = 2;
  262.     fInset.left = 2;
  263.     fInset.bottom = 1;
  264.     fInset.right = 2;
  265.     dest.top = fInset.top;
  266.     dest.left = fInset.left;
  267.     dest.bottom = (short) (fSize.v - fInset.bottom);
  268.     dest.right = (short) (fSize.h - fInset.right);
  269.     StuffTERects (&dest);
  270.     fShowIndex = 0;
  271.     fMaxLines = ((TMUDDoc *) itsDocument)->fHistSize + 1;
  272.     fHandList = (Handle **) NewPermHandle ((long) fMaxLines * sizeof (Handle));
  273.     FailNIL (fHandList);
  274.     hp = *fHandList;
  275.     for (int i = fMaxLines; i > 0; i--) *hp++ = NULL;
  276. }
  277.  
  278. //------------------------------------------------------------------------------
  279.  
  280. #pragma segment SMUDDocRes
  281.  
  282. pascal void TCommandView::RevealRect (VRect *rectToReveal, Point minToSee,
  283.         Boolean redraw)
  284. {
  285.     minToSee.h += 2;
  286.     minToSee.v += 2;
  287.     inherited::RevealRect (rectToReveal, minToSee, redraw);
  288. }
  289.  
  290. //------------------------------------------------------------------------------
  291.  
  292. #pragma segment SMUDDocRes
  293.  
  294. pascal void TCommandView::ScrollHist (Boolean scrollUp)
  295. {
  296.     Boolean showIt;
  297.     Handle theChars;
  298.     long len;
  299.     int oldShowIndex;
  300.     Rect area;
  301.     
  302.     CommitLastCommand ();
  303.     oldShowIndex = fShowIndex;
  304.     showIt = FALSE;
  305.     if (scrollUp) {
  306.         if (fShowIndex != fFirstIndex) {
  307.             fShowIndex = (fShowIndex + fMaxLines - 1) % fMaxLines;
  308.             showIt = TRUE;
  309.         }
  310.     } else {
  311.         if (fShowIndex != fCurIndex) {
  312.             fShowIndex = (fShowIndex + 1) % fMaxLines;
  313.             showIt = TRUE;
  314.         }
  315.     }
  316.     if (showIt) {
  317.         SetText ("\p"); // text moves to fSavedTEHandle
  318.         if (oldShowIndex == fCurIndex) {
  319.             DisposIfHandle (*(*fHandList + fCurIndex));
  320.             *(*fHandList + fCurIndex) = fSavedTEHandle;
  321.             fSavedTEHandle = NULL;
  322.         } else {
  323.             DisposIfHandle (fSavedTEHandle);
  324.             fSavedTEHandle = NULL;
  325.         }
  326.         theChars = *(*fHandList + fShowIndex);
  327.         if (theChars != NULL)
  328.             FailOSErr (HandToHand (&theChars));
  329.         else {
  330.             theChars = NewPermHandle (0);
  331.             FailNIL (theChars);
  332.         }
  333.         StuffText (theChars);
  334.         len = GetHandleSize (theChars);
  335.         SetSelect ((short) len, (short) len, fHTE);
  336.         RecalcText ();
  337.         SynchView (kDontRedraw);
  338.         ScrollSelectionIntoView ();
  339.         if (Focus () && IsVisible ()) {
  340.             GetQDExtent (&area);
  341.             EraseRect (&area);
  342.             DrawContents ();
  343.             ValidRect (&area);
  344.         }
  345.     }
  346. }
  347.  
  348. //------------------------------------------------------------------------------
  349.  
  350. #pragma segment SMUDDocRes
  351.  
  352. pascal void TCommandView::SendLine (void)
  353. {
  354.     Handle theChars;
  355.     unsigned char *theLine, *oldLine, *cp, ch;
  356.     SignedByte oldState;
  357.     Boolean drop;
  358.     long len, i;
  359.     
  360.     CommitLastCommand ();
  361.     theChars = ExtractText ();
  362.     len = GetHandleSize (theChars);
  363.     oldState = HGetState (theChars);
  364.     HLock (theChars);
  365.     ((TMUDDoc *) fMyDocument)->Send ((unsigned char *) *theChars, len);
  366.     HSetState (theChars, oldState);
  367.     ch = chReturn;
  368.     ((TMUDDoc *) fMyDocument)->Send (&ch, 1);
  369.     drop = TRUE;
  370.     cp = (unsigned char *) *theChars;
  371.     i = len;
  372.     while (drop && (i-- > 0)) drop = (*cp++ == ' ');
  373.     if (!drop && (fCurIndex != fFirstIndex)) {
  374.         drop = (len == GetHandleSize (*(*fHandList + (fCurIndex + fMaxLines -1)
  375.             % fMaxLines)));
  376.         if (drop) {
  377.             theLine = (unsigned char *) *theChars;
  378.             oldLine = (unsigned char *) **(*fHandList + (fCurIndex + fMaxLines
  379.                 - 1) % fMaxLines);
  380.             i = len;
  381.             while (drop && (i-- > 0)) drop = (*theLine++ == *oldLine++);
  382.         }
  383.     }
  384.     SetText ("\p"); // text moves to fSavedTEHandle
  385.     ForceRedraw ();
  386.     if (Focus ())
  387.         TESetSelect (0, 0, fHTE);
  388.     else
  389.         SetSelect (0, 0, fHTE);
  390.     RecalcText ();
  391.     SynchView (kDontRedraw);
  392.     SetIdleFreq (0);
  393.     if (!drop) {
  394.         DisposIfHandle (*(*fHandList + fCurIndex));
  395.         *(*fHandList + fCurIndex) = fSavedTEHandle;
  396.         fSavedTEHandle = NULL;
  397.         fCurIndex = (fCurIndex + 1) % fMaxLines;
  398.         if (fCurIndex == fFirstIndex) {
  399.             fFirstIndex = (fFirstIndex + 1) % fMaxLines;
  400.             DisposIfHandle (*(*fHandList + fCurIndex));
  401.             *(*fHandList + fCurIndex) = NULL;
  402.         }
  403.     } else {
  404.         DisposIfHandle (fSavedTEHandle);
  405.         fSavedTEHandle = NULL;
  406.         DisposIfHandle (*(*fHandList + fCurIndex));
  407.         *(*fHandList + fCurIndex) = NULL;
  408.     }
  409.     fShowIndex = fCurIndex;
  410. }
  411.  
  412. //------------------------------------------------------------------------------
  413.  
  414. #pragma segment SMUDDocRes
  415.  
  416. pascal void TCommandView::SetBaseStyle (TextStyle *theStyle, Boolean redraw)
  417. {
  418.     SetOneStyle (0, (**fHTE).teLength, doAll, theStyle, redraw);
  419.     fTextStyle = *theStyle;
  420. }
  421.  
  422. //------------------------------------------------------------------------------
  423.  
  424. #pragma segment SMUDDocRes
  425.  
  426. pascal void TCommandView::SetHistSize (long lines)
  427. {
  428.     Handle *hp, **newHList;
  429.     int i, j;
  430.     
  431.     lines += 1;
  432.     if (fMaxLines != lines) {
  433.         newHList = (Handle **) NewPermHandle (lines * sizeof (Handle));
  434.         FailNIL (newHList);
  435.         hp = *newHList;
  436.         for (i = (int) lines; i > 0; i--) *hp++ = NULL;
  437.         if (lines <= (fCurIndex + fMaxLines - fFirstIndex) % fMaxLines) {
  438.             i = (fCurIndex + fMaxLines - (int) lines + 1) % fMaxLines;
  439.             while (fFirstIndex != i) {
  440.                 DisposIfHandle (*(*fHandList + fFirstIndex));
  441.                 fFirstIndex = (fFirstIndex + 1) % fMaxLines;
  442.             }
  443.         }
  444.         i = fFirstIndex;
  445.         j = 0;
  446.         while (TRUE) {
  447.             *(*newHList + j) = *(*fHandList + i);
  448.             if (i == fCurIndex) break;
  449.             i = (i + 1) % fMaxLines;
  450.             j++;
  451.         }
  452.         DisposIfHandle (fHandList);
  453.         fHandList = newHList;
  454.         fShowIndex = j - (fCurIndex + fMaxLines - fShowIndex) % fMaxLines;
  455.         if (fShowIndex < 0) fShowIndex = 0;
  456.         fFirstIndex = 0;
  457.         fCurIndex = j;
  458.         fMaxLines = (int) lines;
  459.     }
  460. }
  461.  
  462. //------------------------------------------------------------------------------
  463.  
  464. #pragma segment MAInit
  465.  
  466. void InitCommandView (void)
  467. {
  468.     if (gDeadStripSuppression) {
  469.         TCommandView *aCommandView = new TCommandView;
  470.         TCmdScroller *aCmdScroller = new TCmdScroller;
  471.         TCmdScrollBar *aCmdScrollBar = new TCmdScrollBar;
  472.     }
  473.     gNonPanes->AddElement (GetClassIDFromName ("\pTCmdScrollBar"));
  474. }
  475.  
  476. //------------------------------------------------------------------------------
  477.