home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / ViewTools.cp < prev    next >
Encoding:
Text File  |  1994-03-06  |  7.1 KB  |  296 lines  |  [TEXT/MPS ]

  1. // Copyright © 1992 Peter Speck, speck@dat.ruc.dk. All rights reserved.
  2. // ViewTools.h
  3.  
  4. #include "ViewTools.h"
  5. #include "Tools.h"
  6.  
  7. #pragma segment MyViewTools
  8.  
  9. #define qDebugDDR qDebug & 0
  10.  
  11. void DirectDeltaRows(TGridView *gv, 
  12.         short beforeRow, short numOfRows, short aHeight)
  13. {
  14. #if qDebugDDR
  15.     fprintf(stderr, "DDR: TGridView at $%lx, fNumOfRows = %hd\n", long(gv), gv->fNumOfRows);
  16.     fprintf(stderr, "-    beforeRow = %hd, numOfRows = %ld, aHeight = %hd\n", beforeRow, long(numOfRows), aHeight);
  17. #endif
  18.     if (!numOfRows)
  19.         return;
  20.     gv->Update();
  21.     gv->Focus();
  22.     if (beforeRow > gv->fNumOfRows)
  23.     {
  24. #if qDebugDDR
  25.         fprintf(stderr, "beforeRow > gv->fNumOfRows\n");
  26. #endif
  27.         if (numOfRows > 0)
  28.             gv->InsRowBefore(beforeRow, numOfRows, aHeight);
  29. #if qDebug
  30.             ProgramBreak("beforeRow > fNumOfRows && numOfRows < 0");
  31. #endif
  32.         return;
  33.     }
  34.     short cFromRow, cToRow, cNoMoveRows;
  35.     if (numOfRows > 0)
  36.     {
  37.         cFromRow = beforeRow;
  38.         cToRow   = beforeRow + numOfRows;
  39.         cNoMoveRows = gv->fNumOfRows - beforeRow + 1;
  40.     }
  41.     else
  42.     {
  43.         cToRow   = beforeRow;
  44.         cFromRow = beforeRow - numOfRows;
  45.         cNoMoveRows = gv->fNumOfRows - beforeRow + numOfRows + 1;
  46.     }
  47. #if qDebugDDR
  48.     fprintf(stderr, "DDR: cFromRow = %hd, cToRow = %hd, cNoMoveRows = %hd\n", cFromRow, cToRow, cNoMoveRows);
  49. #endif
  50.     if (!cNoMoveRows)
  51.     {
  52. #if qDebugDDR
  53.         fprintf(stderr, "DDR: no rows to ScrollRect\n");
  54. #endif
  55.         if (numOfRows > 0)
  56.             gv->InsRowBefore(beforeRow, numOfRows, aHeight);
  57.         else
  58.         {
  59.             CRect r(1, beforeRow, gv->fNumOfCols, beforeRow-numOfRows-1);
  60. #if qDebugDDR
  61.             fprintf(stderr, "Clears selection for %s\n", (char*)r);
  62. #endif
  63.             gv->SetSelectionRect(r, true, true, false);
  64.             gv->DelRowAt(beforeRow, -numOfRows);
  65.         }
  66.         return;
  67.     }
  68. #if qDebugDDR
  69.     fprintf(stderr, "DDR: Old view size: %s\n", (char*)gv->fSize.Copy());
  70. #endif
  71.     if (numOfRows > 0)
  72.     {
  73.         VPoint oldSize(0, gv->fSize.v);
  74.         gv->fRowHeights->InsertItems(beforeRow, numOfRows, aHeight);
  75.         gv->fNumOfRows += numOfRows;
  76.         gv->AdjustFrame();
  77.         gv->Focus();
  78.         VRect validRect(oldSize, gv->fSize);
  79.         gv->ValidateVRect(validRect);
  80.         gv->InvalidateFocus(); // as updateRgn has changed
  81.         gv->Focus();
  82. #if qDebugDDR
  83.         fprintf(stderr, "DDR: New view size: %s\n", (char*)gv->fSize.Copy());
  84. #endif
  85.     }
  86.     VRect moveFromVRect, moveToVRect;
  87.     gv->RowToVRect(cFromRow, cNoMoveRows, moveFromVRect);
  88.     gv->RowToVRect(cToRow,   cNoMoveRows,   moveToVRect);
  89. #if qDebugDDR
  90.     fprintf(stderr, "DDR: moveFromVRect = %s\n", (char*) moveFromVRect);
  91.     fprintf(stderr, "DDR: moveToVRect   = %s\n", (char*) moveToVRect);
  92. #endif
  93.     VRect beforeRowRect;
  94.     gv->RowToVRect(beforeRow, 1, beforeRowRect);
  95. #if qDebugDDR
  96.     fprintf(stderr, "DDR: beforeRowRect = %s\n", (char*)beforeRowRect);
  97. #endif
  98.     VRect visible;
  99.     gv->GetVisibleRect(visible);
  100. #if qDebugDDR
  101.     fprintf(stderr, "DDR: visible = %s\n", (char*) visible);
  102. #endif
  103.     if (visible.bottom < beforeRowRect.top)
  104.     {
  105. #if qDebugDDR
  106.         fprintf(stderr, "beforeRowRect is not visible\n");
  107. #endif
  108.     }
  109.     else
  110.     {
  111.         VRect visibleMoveRect;
  112.         visibleMoveRect = (moveFromVRect | moveToVRect) & visible;
  113. #if qDebugDDR
  114.         fprintf(stderr, "DDR: visibleMoveRect = %s\n", (char*)visibleMoveRect);
  115. #endif
  116. #if qDebugDDR
  117.         if (visibleMoveRect.Empty())
  118.             fprintf(stderr, "DDR: visibleMoveRect was empty, should not have been it\n");
  119. #endif
  120.         CRect qdMoveRect;
  121.         gv->ViewToQDRect(visibleMoveRect, qdMoveRect);
  122. #if qDebugDDR
  123.         fprintf(stderr, "DDR: qdMoveRect = %s\n", (char*)qdMoveRect);
  124. #endif
  125.         CPoint delta;
  126.         if (numOfRows > 0)
  127.             delta = CPoint(0, numOfRows * aHeight);
  128.         else
  129.         {
  130.             VRect vr;
  131.             gv->RowToVRect(beforeRow, -numOfRows, vr);
  132.             delta = CPoint(0, -short(vr.GetLength(vSel)));
  133.         }
  134. #if qDebugDDR
  135.         fprintf(stderr, "DDR: delta = %s\n", (char*)delta);
  136. #endif
  137.         if (delta.v)
  138.         {
  139.             CTemporaryRegion updateRgn;
  140.             ScrollRect(qdMoveRect, delta.h, delta.v, updateRgn);
  141.             gv->InvalidateRegion(updateRgn);
  142.         }
  143.     }
  144.     if (gv->IsAnyCellSelected())
  145.     {
  146.         CTemporaryRegion moveRgn;
  147.         CopyRgn(gv->fSelections, moveRgn);
  148.         CTemporaryRegion tmpRgn;
  149.         SetRectRgn(tmpRgn, 1, cFromRow, gv->fNumOfCols + 1, cFromRow + cNoMoveRows);
  150.         SectRgn(tmpRgn, moveRgn, moveRgn);
  151.         OffsetRgn(moveRgn, 0, numOfRows);
  152.  
  153.         SetRectRgn(tmpRgn, 1, 1, gv->fNumOfCols + 1, beforeRow);
  154.         SectRgn(tmpRgn, gv->fSelections, gv->fSelections);
  155.         
  156.         UnionRgn(moveRgn, gv->fSelections, gv->fSelections);
  157.  
  158.         CopyRgn(gv->fHLRegion, moveRgn);
  159.         SetRectRgn(tmpRgn, 1, cFromRow, gv->fNumOfCols + 1, cFromRow + cNoMoveRows);
  160.         SectRgn(tmpRgn, moveRgn, moveRgn);
  161.         OffsetRgn(moveRgn, 0, numOfRows);
  162.  
  163.         SetRectRgn(tmpRgn, 1, 1, gv->fNumOfCols + 1, beforeRow);
  164.         SectRgn(tmpRgn, gv->fHLRegion, gv->fHLRegion);
  165.         
  166.         UnionRgn(moveRgn, gv->fHLRegion, gv->fHLRegion);
  167.     }
  168.  
  169.     if (numOfRows < 0)
  170.     {
  171.         gv->fRowHeights->DeleteItems(beforeRow, -numOfRows);
  172.         gv->fNumOfRows += numOfRows;
  173.         gv->AdjustFrame();
  174.         gv->Focus();
  175. #if qDebugDDR
  176.         fprintf(stderr, "DDR: New view size: %s\n", (char*)gv->fSize.Copy());
  177. #endif
  178.     }
  179. #if qDebugDDR
  180.     fprintf(stderr, "DDR: done\n");
  181. #endif
  182. }
  183.  
  184. //-----------------------------
  185. Boolean DoGridViewKey(TGridView *gv, char ch)
  186. {
  187.     GridCell aCell;
  188.     switch (ch)
  189.     {
  190.         case chUp:
  191.             if (gv->IsAnyCellSelected())
  192.             {
  193.                 aCell = gv->FirstSelectedCell();
  194.                 aCell.v--;
  195.             }
  196.             else
  197.                 aCell = GridCell(1, gv->fNumOfRows);
  198.             break;
  199.  
  200.         case chDown:
  201.             if (gv->IsAnyCellSelected())
  202.             {
  203.                 aCell = gv->LastSelectedCell();
  204.                 aCell.v++;
  205.             }
  206.             else
  207.                 aCell = GridCell(1, 1);
  208.             break;
  209.     
  210.         case chLeft:
  211.             if (gv->IsAnyCellSelected())
  212.             {
  213.                 aCell = gv->FirstSelectedCell();
  214.                 aCell.h--;
  215.             }
  216.             else
  217.                 aCell = GridCell(gv->fNumOfCols, 1);
  218.             break;
  219.  
  220.         case chRight:
  221.             if (gv->IsAnyCellSelected())
  222.             {
  223.                 aCell = gv->LastSelectedCell();
  224.                 aCell.h++;
  225.             }
  226.             else
  227.                 aCell = GridCell(1, 1);
  228.             break;
  229.             
  230.         default:
  231.             return false;
  232.     }    
  233.     if (aCell.h < 1 || aCell.h > gv->fNumOfCols)
  234.         return true;
  235.     if (aCell.v < 1 || aCell.v > gv->fNumOfRows)
  236.         return true;
  237.     gv->SelectCell(aCell, false, true, true);
  238.     gv->ScrollSelectionIntoView(kRedraw);
  239.     gv->Focus();
  240.     gv->Update();
  241.     return true;
  242. }
  243.  
  244. IDType MyPoseModally(TWindow *&window)
  245. {
  246.     FailInfo fi;
  247.     if (fi.Try())
  248.     {
  249.         IDType id = window->PoseModally();
  250.         fi.Success();
  251.         return id;
  252.     }
  253.     else // fail
  254.     {
  255.         window = nil; // TWindow::PoseModally frees the window
  256.         fi.ReSignal();
  257.     }
  258. }
  259.  
  260. //========================================================================
  261. class TDoEventActionBehavior : public TBehavior
  262. {
  263. public:
  264.     TDoEventActionBehavior();
  265.     virtual pascal void DoEvent(EventNumber eventNumber, TEventHandler* source, TEvent* event);
  266.     PlainDoEvent fFunc;
  267.     void *fUserItem;
  268. };
  269.  
  270. TDoEventActionBehavior::TDoEventActionBehavior()
  271. {
  272. }
  273.  
  274. pascal void TDoEventActionBehavior::DoEvent(EventNumber eventNumber, TEventHandler* source, TEvent* event)
  275. {
  276. #if qDebug
  277.     if (source)
  278.         fprintf(stderr, "DDDD: eventNumber = %ld, id = %s\n", eventNumber, OSType2String(source->fIdentifier));
  279. #endif
  280.     inherited::DoEvent(eventNumber, source, event);
  281.     TView *view = (TView*)fOwner;
  282.     FailNonObject(view);
  283.     if (mCheckBoxHit == 4)
  284.         fFunc(fUserItem, view, eventNumber, source, event);
  285. }
  286.  
  287. void AddActionBehaviour(TView *view, PlainDoEvent func, void *useritem)
  288. {
  289.     FailNonObject(view);
  290.     TDoEventActionBehavior *be = new TDoEventActionBehavior();
  291.     be->IBehavior('AcBe');
  292.     be->fFunc = func;
  293.     be->fUserItem = useritem;
  294.     view->AddBehavior(be);
  295. }
  296.