home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / lang / listmana.sha / test.c < prev    next >
C/C++ Source or Header  |  1986-07-10  |  12KB  |  510 lines

  1. /*
  2.  *    test, an example program which uses the List Manager
  3.  *    converted from the Pascal program provided on the 12/85 Apple
  4.  *    supplement 1 disk (a Lisa-only disk) to Aztec C 1.06G by
  5.  *    Dale Carstensen
  6.  *    1504 S Sage
  7.  *    Los Alamos, NM 87544
  8.  *    (505)662-3691
  9.  *    June 16, 1986
  10.  */
  11.  
  12. #include <quickdraw.h>
  13. #include <desk.h>
  14. #include <dialog.h>
  15. #include <event.h>
  16. #include <font.h>
  17. #include <list.h>
  18. #include <menu.h>
  19. #include <packages.h>
  20. #include <resource.h>
  21. #include <segment.h>
  22. #include <textedit.h>
  23. #include <toolutil.h>
  24.  
  25. pascal void MacsBug() = 0xa9ff;
  26.  
  27. #define NIL 0L
  28.  
  29. #define appleMenu    1
  30. #define fileMenu    256
  31. #define editMenu    257
  32. #define mungeMenu    258
  33. #define selectMenu    259
  34. #define makeNewMenu    260
  35. #define lastMenu    6
  36.  
  37. #define len255    255
  38.  
  39. MenuHandle    myMenus[lastMenu + 1];
  40. Rect        dragRect,r,b,sizeLimits;
  41. Boolean        doneFlag,temp,sHoriz,sVert,groBox,drawIt;
  42. EventRecord    myEvent;
  43. int            code,refNum,i,j;
  44. WindowRecord    wRecord;
  45. WindowPtr    myWindow,whichWindow;
  46. int            theMenu,theItem;
  47. ListHandle    myList;
  48. Cell        c,cc;
  49. char        CellName[len255],cutStr[len255];
  50. int            cutStrLen;
  51. Handle        h1,h2;
  52. OSErr        x;
  53. Point        cSize;
  54. int            theMark;
  55. int            tFlag;
  56. long        tempFlags, newSize;
  57.  
  58. void
  59. CellValue(c, h, v)
  60. Cell    *c;
  61. {
  62.     c->h = h;
  63.     c->v = v;
  64. } /* end of CellValue() */
  65.  
  66. void
  67. ClrAllCells()
  68. {
  69.     Cell    c;
  70.  
  71.     CellValue(&c,0,0);
  72.     while (LGetSelect(TRUE,&c,myList)) /* get next selected cell */
  73.         LSetSelect(FALSE,pass(c),myList);    /* and deselect it */
  74. } /* end of ClrAllCells() */
  75.  
  76. void
  77. SetCell(c)
  78. Cell    *c;
  79. {
  80.     char    iStr[len255],jStr[len255];
  81.  
  82.     NumToString((long)c->h,iStr);
  83.     ptoc(iStr);
  84.     NumToString((long)c->v,jStr);
  85.     ptoc(jStr);
  86.     strcpy(CellName, "Cell ");
  87.     strcat(CellName, jStr);
  88.     strcat(CellName, ",");
  89.     strcat(CellName, iStr);
  90.     LSetCell(CellName, strlen(CellName), pass(*c), myList);
  91. } /* end of SetCell() */
  92.  
  93. void
  94. SetCellValues()  /* number all the cells in the array*/
  95. {
  96.     Cell    c;
  97.  
  98.     CellValue(&c,0,0);
  99.     do {
  100.         SetCell(&c);
  101.     } while (LNextCell(TRUE,TRUE,&c,myList));    /* increment v and h */
  102. } /* end of SetCellValues() */
  103.  
  104. int
  105. CountSelections()
  106. {
  107.     Cell    c;
  108.     int        i;
  109.  
  110.     CellValue(&c,0,0);
  111.     i = 0;
  112.     do {
  113.         if (LGetSelect(TRUE,&c,myList))
  114.             i++;
  115.     } while (LNextCell(TRUE,TRUE,&c,myList));
  116.     return(i);
  117. } /* end of CountSelections() */
  118.  
  119. void
  120. FlipSelections() /* A little razzle-dazzle to test double-click */
  121. {
  122.     Cell    c;
  123.     int        i,flipStrLen;
  124.     char    flipStr[len255];
  125.     char    a;
  126.     Boolean    b;
  127.  
  128.     CellValue(&c,0,0);
  129.     /* It appears maybe the programmer should choose between the
  130.      * while (LGetSelect... or the LLastClick, depending on whether
  131.      * the double click should affect all selections, or only one,
  132.      * but it didn't say in the Pascal source.  The while was
  133.      * commented out.
  134.      */
  135. /*    while (LGetSelect(TRUE,&c,myList)) */
  136.         *((long *)(&c)) = LLastClick(myList);
  137.     {
  138.         flipStrLen = len255;
  139.         LGetCell(flipStr + 1, &flipStrLen, pass(c), myList);
  140.         for (i = 1; i <= flipStrLen / 2; i++) {
  141.             a = flipStr[i];
  142.             flipStr[i] = flipStr[flipStrLen-i+1];
  143.             flipStr[flipStrLen-i+1] = a;
  144.         };
  145.         LSetCell(flipStr + 1, flipStrLen, pass(c), myList);
  146.         b = LNextCell(TRUE,TRUE,&c,myList);
  147.     };
  148. } /* end of FlipSelections() */
  149.  
  150. void
  151. SetUpMenus() /* Once-only initialization for menus */
  152. {
  153.     int    i;
  154.  
  155.     InitMenus(); /* initialize Menu Manager */
  156.     myMenus[1] = GetMenu(appleMenu);
  157.     AddResMenu(myMenus[1],'DRVR'); /* desk accessories */
  158.     myMenus[2] = GetMenu(fileMenu);
  159.     myMenus[3] = GetMenu(editMenu);
  160.     myMenus[4] = GetMenu(mungeMenu);
  161.     myMenus[5] = GetMenu(selectMenu);
  162.     myMenus[6] = GetMenu(makeNewMenu);
  163.     for (i = 1; i <= lastMenu; i++)
  164.         InsertMenu(myMenus[i], 0);
  165.     DrawMenuBar();
  166. } /* end of SetUpMenus() */
  167.  
  168. void
  169. CloseTheList()
  170. {
  171.     if (myList != NIL) {
  172.         LDispose(myList);
  173.         CloseWindow(myWindow);
  174.         myList = NIL;
  175.         myWindow = NIL;
  176.     };
  177. } /* end of closeTheList() */
  178.  
  179. void
  180. OpenNewList()
  181. {
  182.     myWindow = GetNewWindow(256,&wRecord,-1L);
  183.     SetPort(myWindow);
  184.     BlockMove(&myWindow->portRect, &r, (long)sizeof(Rect));
  185.     /* because drawGrowIcon draws lines for both scrollbars, we must
  186.      * leave room for both of them if we have a grow box (oh well...)
  187.      */
  188.     if (sVert || groBox)
  189.         r.right = r.right-15;      /* room for vert scroll bar */
  190.     if (sHoriz || groBox)
  191.         r.bottom = r.bottom-15;   /* leave room for horiz scroll bar */
  192.     SetRect(&b,0,0,10,50);            /* make room for 10*50 items */
  193.     cSize.v = (r.bottom-r.top) / 16;       /* 16 rows       */
  194.     cSize.h = (r.right-r.left) / 5;        /* and 5 columns */
  195.     /* make a list with content r, bounds b, proc 0, and two scrollbars;
  196.      *    drawing false
  197.      */
  198.     myList=LNew(&r,&b,pass(cSize),0,myWindow,FALSE,groBox,sHoriz,sVert);
  199.     SetCellValues();            /* number all the cells */
  200.     LDoDraw(drawIt,myList);        /* set draw state for update */
  201. } /* end of OpenNewList() */
  202.  
  203. void
  204. DoCommand(mResult)
  205. long    mResult;
  206. {
  207.     char    name[len255];
  208.     int        i,temp;
  209.     Cell    c;
  210.     Boolean    b;
  211.  
  212.     theMenu = (mResult >> 16) & 0xFFFF;
  213.     theItem = mResult & 0xFFFF;
  214.     switch (theMenu) {
  215.         case appleMenu:
  216.             GetItem(myMenus[1],theItem,name);
  217.             refNum = OpenDeskAcc(name);
  218.             break;
  219.         case fileMenu:
  220.             doneFlag = TRUE; /* Quit */
  221.             break;
  222.         case editMenu:
  223.             if (!SystemEdit(theItem-1)) {
  224.                 SetPort(myWindow);
  225.                 CellValue(&cc,0,0);
  226.                 switch (theItem) {
  227.                     case 3:    /*cut*/
  228.                         while (LGetSelect(TRUE,&cc,myList)) {
  229.                             cutStrLen = len255; /*cut up to 255 chars*/
  230.                             LGetCell(cutStr+1,&cutStrLen,pass(cc),myList);
  231.                             LClrCell(pass(cc),myList);
  232.                             b = LNextCell(TRUE,TRUE,&cc,myList);
  233.                         };
  234.                         break;
  235.                     case 4:    /*copy*/
  236.                         if (LGetSelect(TRUE,&cc,myList)) {
  237.                             cutStrLen = len255; /*copy up to 255 chars*/
  238.                             LGetCell(cutStr+1,&cutStrLen,pass(cc),myList);
  239.                         };
  240.                         break;
  241.                     case 5:    /*paste*/
  242.                         while (LGetSelect(TRUE,&cc,myList)) {
  243.                             LSetCell(cutStr+1,cutStrLen,pass(cc),myList);
  244.                             b = LNextCell(TRUE,TRUE,&cc,myList);
  245.                         };
  246.                         break;
  247.                     case 6:    /* find first */
  248.                         if (LSearch(cutStr+1,cutStrLen,NIL,
  249.                                 &cc,myList)) {
  250.                             ClrAllCells();
  251.                             LSetSelect(TRUE,pass(cc),myList);
  252.                             LAutoScroll(myList);
  253.                         };
  254.                         break;
  255.                     case 7:    /* find all */
  256.                         ClrAllCells();
  257.                         while (LSearch(cutStr+1,cutStrLen,NIL,&cc,myList)) { 
  258.                             LSetSelect(TRUE,pass(cc),myList);
  259.                             b = LNextCell(TRUE,TRUE,&cc,myList);
  260.                         };
  261.                         break;
  262.                 }; /* of theItem */
  263.             }; /* of editMenu */
  264.             break;
  265.         case mungeMenu:
  266.             CellValue(&c,0,0); /*search for first selected cell*/
  267.             if (!LGetSelect(TRUE,&c,myList))
  268.                 CellValue(&c,(*myList)->dataBounds.right,
  269.                     (*myList)->dataBounds.bottom);
  270.             switch (theItem) {
  271.                 case 1:        /* Add Row */
  272.                     c.h = CountSelections();
  273.                     if (c.h == 0)
  274.                         c.h = 1;
  275.                     c.v = LAddRow(c.h,c.v,myList);
  276.                     c.h = 0;
  277.                     do {   /* increment h */
  278.                         SetCell(&c);
  279.                     } while (LNextCell(TRUE,FALSE,&c,myList));
  280.                     break;
  281.                 case 2:        /* Delete Row */
  282.                     LDelRow(CountSelections(),c.v,myList);
  283.                     break;
  284.                 case 3:        /* Add Column */
  285.                     c.v = CountSelections();
  286.                     if (c.v == 0)
  287.                         c.v = 1;
  288.                     c.h = LAddColumn(c.v,c.h,myList);
  289.                     c.v = 0;
  290.                     do {   /* increment v */
  291.                         SetCell(&c);
  292.                     } while (LNextCell(FALSE,TRUE,&c,myList));
  293.                     break;
  294.                 case 4:        /* Delete Column */
  295.                     LDelColumn(CountSelections(),c.h,myList);
  296.                     break;
  297.                 case 5:        /* Clear Selections */
  298.                     ClrAllCells();
  299.                     break;
  300.                 case 6:        /* Renumber Cells */
  301.                     LDoDraw(FALSE,myList);
  302.                     SetCellValues();
  303.                     LDoDraw(drawIt,myList);
  304.                     InvalRect(&myWindow->portRect);
  305.                     break;
  306.                 case 7:        /* Update List */
  307.                     InvalRect(&myWindow->portRect);
  308.                     break;
  309.             }; /*of theItem */
  310.             break;
  311.         case selectMenu:
  312.             GetItemMark(myMenus[5],theItem,&theMark);
  313.             tempFlags = (*myList)->selFlags;
  314.             if (theMark == checkMark) {
  315.                 theMark = '\0';
  316.         /*        ClearBit(tempFlags,theItem-1); */
  317.                 tempFlags &= ~(1 << (theItem-1));
  318.             }
  319.             else {
  320.                 theMark = checkMark;
  321.         /*        SetBit(tempFlags,theItem-1); */
  322.                 tempFlags |= 1 << (theItem-1);
  323.             };
  324.             SetItemMark(myMenus[5],theItem,theMark);
  325.             tFlag = tempFlags & 0xFFFF;
  326.             (*myList)->selFlags = (Byte)tFlag;
  327.             break;
  328.         case makeNewMenu:
  329.             GetItemMark(myMenus[6],theItem,&theMark);
  330.             switch (theItem) {
  331.                 case 1:        /* New List */
  332.                     CloseTheList();
  333.                     OpenNewList();
  334.                     break;
  335.                 case 2:        /* Horizontal Scroll */
  336.                     if (theMark == checkMark) {
  337.                         theMark = '\0';
  338.                         sHoriz = FALSE;
  339.                     }
  340.                     else {
  341.                         theMark = checkMark;
  342.                         sHoriz = TRUE;
  343.                     };
  344.                     break;
  345.                 case 3:        /* Vertical Scroll */
  346.                     if (theMark == checkMark) {
  347.                         theMark = '\0';
  348.                         sVert = FALSE;
  349.                     }
  350.                     else {
  351.                         theMark = checkMark;
  352.                         sVert = TRUE;
  353.                     };
  354.                     break;
  355.                 case 4:        /* Grow Box */
  356.                     if (theMark == checkMark) {
  357.                         theMark = '\0';
  358.                         groBox = FALSE;
  359.                     }
  360.                     else {
  361.                         theMark = checkMark;
  362.                         groBox = TRUE;
  363.                     };
  364.                     break;
  365.                 case 5:        /* Drawing On */
  366.                     if (theMark == checkMark) {
  367.                         theMark = '\0';
  368.                         drawIt = FALSE;
  369.                         LDoDraw(drawIt,myList);
  370.                     }
  371.                     else {
  372.                         theMark = checkMark;
  373.                         drawIt = TRUE;
  374.                         LDoDraw(drawIt,myList);
  375.                     };
  376.                     break;
  377.             }; /* theItem switch*/
  378.             SetItemMark(myMenus[6],theItem,theMark);
  379.             break;
  380.     }; /* of menu switch */
  381.  
  382.     HiliteMenu(0);
  383.  
  384. } /* end of DoCommand() */
  385.  
  386. main()
  387. {
  388.     InitGraf(&thePort);
  389.     InitFonts();
  390.     FlushEvents(everyEvent,0);
  391.     InitWindows();
  392.     SetUpMenus();
  393.     TEInit();
  394.     InitDialogs(NIL);
  395.     InitCursor();
  396.  
  397.     InitPack(0);       /* so list manager package gets initialized */
  398.     h1 = GetResource('PACK',0);
  399.     /* Note -- if you should want to un-comment the following, Aztec
  400.      * C version 1.06G does not support MoveHHi -- it may be trivial
  401.      * for the 128K ROMs (just pop the return address to A1, pop the
  402.      * argument to A0, trap $A064, JMP (A1) -- maybe).  It appears
  403.      * the 64K ROMs version of MoveHHi is about 500 bytes of code.
  404.      */
  405. /*    MoveHHi(h1); HLock(h1); if you want to move it out of the way */
  406.  
  407.     sizeLimits.left = 60;       /* set minimum window size to 60x40*/
  408.     sizeLimits.top = 40;
  409.     sizeLimits.right = 500;     /* and max size to 500x300 */
  410.     sizeLimits.bottom = 300;
  411.  
  412.     doneFlag = FALSE;
  413.     groBox = FALSE;
  414.     sHoriz = FALSE;
  415.     sVert = FALSE;
  416.     drawIt = FALSE;
  417.     cutStr[0] = '\0';
  418.     cutStrLen = 0; /* init string for cut/copy/paste */
  419.     SetRect(&dragRect,4,24,
  420.         screenBits.bounds.right-4,screenBits.bounds.bottom-4);
  421.  
  422.     OpenNewList(); /* make a new window (myWindow)
  423.                     * and a new list (myList)
  424.                     */
  425.  
  426.     do {
  427.         SystemTask();
  428.         if (GetNextEvent(everyEvent,&myEvent)) {
  429.             switch (myEvent.what) {
  430.                 case mouseDown:
  431.                     code = FindWindow(pass(myEvent.where),&whichWindow);
  432.                     switch (code) {
  433.                         case inMenuBar:
  434.                             DoCommand(MenuSelect(&myEvent.where));
  435.                             break;
  436.                         case inSysWindow:
  437.                             SystemClick(&myEvent,whichWindow);
  438.                             break;
  439.                         case inDrag:
  440.                             DragWindow(whichWindow,
  441.                                 pass(myEvent.where),&dragRect);
  442.                             break;
  443.                         case inGoAway:
  444.                             if (TrackGoAway(myWindow,pass(myEvent.where)))
  445.                                 CloseTheList();
  446.                             break;
  447.                         case inContent:
  448.                         case inGrow:
  449.                             if (code == inGrow && groBox == TRUE) {
  450.                                 newSize = GrowWindow(whichWindow,
  451.                                     pass(myEvent.where),&sizeLimits);
  452.                                 if (newSize != 0) {
  453.                                     i = newSize & 0xFFFF;
  454.                                     j = (newSize >> 16) & 0xFFFF;
  455.                                     SizeWindow(whichWindow,i,j,TRUE);
  456.                                     if (sHoriz || groBox)
  457.                                         i = i - 15;
  458.                                     if (sVert || groBox)
  459.                                         j = j - 15;
  460.                                     LSize(i,j,myList);
  461.                                     if (groBox)
  462.                                         DrawGrowIcon(myWindow);
  463.                                 };
  464.                             }
  465.                             else {
  466.                                 if (whichWindow != FrontWindow())
  467.                                     SelectWindow(whichWindow);
  468.                                 else {
  469.                                     GlobalToLocal(&myEvent.where);
  470.                                     if (LClick(pass(myEvent.where),
  471.                                             myEvent.modifiers, myList)) {
  472.                                         FlipSelections();
  473.                                           /*and invalidate next dclick */
  474.                                         (*myList)->clikTime -=
  475.                                             GetDblTime();
  476.                                     };
  477.                                 };
  478.                             };
  479.                             break;
  480.                     }; /* of code switch */
  481.                     break;
  482.                 case keyDown:
  483.                 case autoKey:
  484.                     if (myWindow == FrontWindow())
  485.                         ;
  486.                     break;
  487.                 case activateEvt:
  488.                     LActivate(myEvent.modifiers & 1, myList);
  489.                     if (groBox)
  490.                         DrawGrowIcon(myWindow);
  491.                     break;
  492.                 case updateEvt:
  493.                     if (myWindow != NIL) {
  494.                         SetPort(myWindow);
  495.                         BeginUpdate(myWindow);
  496.                         if (groBox)
  497.                             DrawGrowIcon(myWindow);
  498.                         LUpdate(thePort->visRgn,myList);
  499.                         EndUpdate(myWindow);
  500.                     };
  501.                     break;
  502.             }; /* of event switch */
  503.         }; /*
  504.  end if(GetNextEvent...) */
  505.     } while (!doneFlag);
  506.  
  507.     CloseTheList();
  508.     ExitToShell();
  509. } /* end of main() */
  510.