home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Science / RasMol2 / applemac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-24  |  11.6 KB  |  543 lines  |  [TEXT/KAHL]

  1. /* applemac.c
  2.  * RasMol2 Molecular Graphics
  3.  * Roger Sayle, October 1994
  4.  * Version 2.5
  5.  */
  6. #include <QuickDraw.h>
  7. #include <Controls.h>
  8. #include <Palettes.h>
  9. #include <Windows.h>
  10. #include <Menus.h>
  11. #include <Fonts.h>
  12.  
  13. #ifdef __CONDITIONALMACROS__
  14. #include <Printing.h>
  15. #else
  16. #include <PrintTraps.h>
  17. #endif
  18. #include <ToolUtils.h>
  19. #include <Memory.h>
  20. #include <Types.h>
  21. #include <Scrap.h>
  22.  
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25.  
  26. #define GRAPHICS
  27. #include "rasmol.h"
  28. #include "graphics.h"
  29. #include "molecule.h"
  30. #include "transfor.h"
  31. #include "render.h"
  32.  
  33.  
  34. static PixPatHandle BackHand;
  35. static PaletteHandle CMap;
  36. static PixMap *PixelMap;
  37. static int CMapClean;
  38.  
  39.  
  40. int CreateImage()
  41. {
  42.     register Long size;
  43.     
  44.     if( FBuffer ) _ffree(FBuffer);
  45.     size = (Long)XRange*YRange*sizeof(Pixel);
  46.     FBuffer = (Pixel*)_fmalloc( size+32 );
  47.  
  48. #ifdef EIGHTBIT
  49.     PixelMap->rowBytes = XRange | 0x8000;
  50. #else
  51.     PixelMap->rowBytes = (XRange<<2) | 0x8000;
  52. #endif
  53.  
  54.     PixelMap->baseAddr = (Ptr)FBuffer;
  55.     PixelMap->bounds.right = XRange;
  56.     PixelMap->bounds.bottom = YRange;
  57.     return( (int)FBuffer );
  58. }
  59.  
  60.  
  61. void TransferImage()
  62. {
  63.     register PixMapHandle pmHand;
  64.     register GDHandle gdHand;
  65.     register int mode;
  66.     GrafPtr savePort;
  67.     Rect rect;
  68.    
  69.     GetPort(&savePort);
  70.     SetPort(CanvWin);
  71.    
  72.     ForeColor(blackColor);
  73.     BackColor(whiteColor);
  74.    
  75.     rect.top = 0;   rect.bottom = YRange;
  76.     rect.left = 0;  rect.right = XRange;
  77.  
  78.     gdHand = GetMaxDevice(&CanvWin->portRect);
  79.     pmHand = (**gdHand).gdPMap;
  80.     if( (**pmHand).pixelSize < (sizeof(Pixel)<<3) )
  81.     {   mode = srcCopy+ditherCopy;
  82.     } else mode = srcCopy;
  83.     
  84.     /* ClipRect(&rect); */
  85.     CopyBits((BitMap*)PixelMap,
  86.              (BitMap*)&CanvWin->portBits,
  87.              &rect,&rect,mode,(RgnHandle)0);
  88.  
  89.     BackColor(blackColor);
  90.     SetPort(savePort);
  91. }
  92.  
  93.  
  94. void ClearImage()
  95. {
  96.     GrafPtr savePort;
  97.     RGBColor col;
  98.     Rect rect;
  99.  
  100.     GetPort(&savePort);
  101.     SetPort(CanvWin);
  102.     
  103.     rect.bottom = CanvWin->portRect.bottom-15;
  104.     rect.right = CanvWin->portRect.right-15;
  105.     rect.left = 0;    
  106.     rect.top = 0;
  107.     
  108.     col.red   = BackR<<8 | BackR;
  109.     col.green = BackG<<8 | BackG;
  110.     col.blue  = BackB<<8 | BackB;
  111.     
  112.     MakeRGBPat(BackHand,&col);
  113.     FillCRect(&rect,BackHand);
  114.     SetPort(savePort);
  115. }
  116.  
  117.  
  118. static PicHandle CreateMacPicture()
  119. {
  120.     GrafPtr savePort;
  121.     RgnHandle saveRgn;
  122.     PicHandle pict;
  123.     Rect rect;
  124.  
  125.     saveRgn = NewRgn();
  126.     GetPort(&savePort);
  127.     SetPort(CanvWin);
  128.     GetClip( saveRgn );
  129.     
  130.     rect.top = 0;   rect.bottom = YRange;
  131.     rect.left = 0;  rect.right = XRange;
  132.     pict = OpenPicture(&rect);
  133.   
  134.     ForeColor(blackColor);
  135.     BackColor(whiteColor);
  136.    
  137.     ClipRect( &rect );
  138.     CopyBits((BitMap*)PixelMap,
  139.              (BitMap*)&CanvWin->portBits,
  140.              &rect,&rect,srcCopy,(RgnHandle)0);
  141.  
  142.     ClosePicture();
  143.     
  144.     SetClip(saveRgn);
  145.     BackColor(blackColor);
  146.     DisposeRgn(saveRgn);
  147.     SetPort(savePort);
  148.     return pict;
  149. }
  150.  
  151.  
  152. int PrintImage()
  153. {
  154.     register int xsize,ysize;
  155.     register int high,wide;
  156.     
  157.     TPrStatus prStatus;
  158.     TPPrPort printPort;
  159.     GrafPtr savePort;
  160.     PicHandle pict;
  161.     short prErr;
  162.     Rect *page;
  163.     Rect rect;
  164.  
  165.     PrOpen();
  166.     if( !PrintHand )
  167.     {   PrintHand = (THPrint)NewHandle(sizeof(TPrint));
  168.         PrintDefault(PrintHand);
  169.     }
  170.  
  171.     if( PrJobDialog(PrintHand) )
  172.     {   pict = CreateMacPicture();
  173.         printPort = PrOpenDoc(PrintHand,0,0);
  174.         GetPort(&savePort);    
  175.         SetPort(&printPort->gPort);
  176.         
  177.         PrOpenPage( printPort, 0 );
  178.         page = &(**PrintHand).prInfo.rPage; 
  179.         wide = page->right - page->left;
  180.         high = page->bottom - page->top;
  181.         
  182.         xsize = XRange;
  183.         ysize = YRange;
  184.         if( xsize > wide )
  185.         {   ysize = (int)(((Long)ysize*wide)/xsize);
  186.             xsize = wide;
  187.         }
  188.         if( ysize > high )
  189.         {   xsize = (int)(((Long)xsize*high)/ysize);
  190.             ysize = high;
  191.         }
  192.         
  193.         rect.top  = page->top  + (high-ysize)>>1;
  194.         rect.left = page->left + (wide-xsize)>>1;
  195.         rect.bottom = rect.top + ysize;
  196.         rect.right = rect.left + xsize;
  197.         
  198.         DrawPicture( pict, &rect );
  199.         PrClosePage( printPort );
  200.         
  201.         SetPort(savePort);
  202.         PrCloseDoc(printPort);
  203.         KillPicture(pict);
  204.         
  205.         if( !(prErr = PrError()) )
  206.             if( (*PrintHand)->prJob.bJDocLoop == bSpoolLoop )
  207.             {   PrPicFile(PrintHand,0,0,0,&prStatus);
  208.                 prErr = PrError();
  209.             }
  210.          
  211.     } else prErr = False;  /* Cancel Print */
  212.  
  213.     PrClose();
  214.     return( !prErr );
  215. }
  216.  
  217. int ClipboardImage()
  218. {
  219.     register long clipErr;
  220.     register long length;
  221.     PicHandle pict;
  222.     
  223.     register int i;
  224.     register FILE *fp;
  225.     
  226.     ZeroScrap();
  227.     pict = CreateMacPicture();
  228.     
  229.     HLock((Handle)pict);
  230.     length = (long)GetHandleSize((Handle)pict);
  231.     clipErr = PutScrap(length,'PICT',(Ptr)*pict);
  232.     HUnlock((Handle)pict);
  233.     
  234.     KillPicture(pict);   
  235.     return( !clipErr );
  236. }
  237.  
  238.  
  239. void AllocateColourMap()
  240. #ifdef EIGHTBIT
  241.     register PixMapHandle pmHand;
  242.     register GDHandle gdHand;
  243.     register CTabHandle cmap;
  244.     register int i;
  245.     RGBColor col;
  246.     
  247.     ULut[0] = True;
  248.     ULut[255] = True;
  249.     
  250.     HLock((Handle)CMap);
  251.     cmap = PixelMap->pmTable;
  252.     (**cmap).ctSeed = GetCTSeed();
  253.     for( i=0; i<256; i++ )
  254.     {   if( ULut[i] )
  255.         {   col.red   = RLut[i]<<8 | RLut[i];
  256.             col.green = GLut[i]<<8 | GLut[i];
  257.             col.blue  = BLut[i]<<8 | BLut[i];
  258.             Lut[i] = i;
  259.         } else col.red = col.green = col.blue = 0;
  260.         (**cmap).ctTable[i].rgb = col;
  261.         SetEntryColor(CMap,i,&col);
  262.     }
  263.     HUnlock((Handle)CMap);
  264.     
  265.     gdHand = GetMaxDevice(&CanvWin->portRect);
  266.     pmHand = (**gdHand).gdPMap;
  267.     if( (**pmHand).pixelSize >= 8 )
  268.     {   ActivatePalette(CanvWin);
  269.         CMapClean = False;
  270.     }
  271.     FBClear = False;
  272. #endif
  273. }
  274.  
  275.  
  276. void UpdateScrollBars()
  277. {
  278.     register int pos;
  279.     
  280.     pos = 50+(int)(50.0*DialValue[1]);
  281.     SetCtlValue(HScroll,pos);
  282.     
  283.     pos = 50+(int)(50.0*DialValue[0]);
  284.     SetCtlValue(VScroll,pos);
  285. }
  286.  
  287.  
  288. void SetMouseMode( mode )
  289.     int mode;
  290. {
  291.     MouseMode = mode;
  292. }
  293.  
  294.  
  295. int LookUpColour( name, r, g, b )
  296.     char *name; int *r, *g, *b;
  297. {
  298.     return( False );
  299. }
  300.  
  301.  
  302. void EnableMenus( flag )
  303.     int flag;
  304. {
  305.     register int i;
  306.     MenuHandle hand;
  307.     long offset;
  308.     
  309.     /* File Menu */
  310.     hand = GetMHandle(141);
  311.     if( flag && !Database )
  312.     {   EnableItem(hand,1);
  313.     } else DisableItem(hand,1);
  314.     
  315.     if( Database && flag )
  316.     {   EnableItem(hand,2);
  317.         EnableItem(hand,3);
  318.         EnableItem(hand,5);
  319.         EnableItem(hand,6);
  320.     } else
  321.     {   DisableItem(hand,2);
  322.         DisableItem(hand,3);
  323.         DisableItem(hand,5);
  324.         DisableItem(hand,6);
  325.     }
  326.     
  327.     /* Edit Menu */
  328.     hand = GetMHandle(142);
  329.     if( Database )
  330.     {   EnableItem(hand,3);
  331.     } else DisableItem(hand,3);
  332.     
  333.     if( flag && (GetScrap(0,'TEXT',&offset)>0) )
  334.     {   EnableItem(hand,4);
  335.     } else DisableItem(hand,4);
  336.     
  337.     if( flag && Database )
  338.     {   EnableItem(hand,7);
  339.     } else DisableItem(hand,7);
  340.     
  341.     
  342.     /* Middle Menus */
  343.     if( Database && flag )
  344.     {   EnableItem(GetMHandle(143),0);
  345.         EnableItem(GetMHandle(144),0);
  346.         EnableItem(GetMHandle(145),0);
  347.     } else
  348.     {   DisableItem(GetMHandle(143),0);
  349.         DisableItem(GetMHandle(144),0);
  350.         DisableItem(GetMHandle(145),0);
  351.     }
  352.     
  353.     /* Export Menu */
  354.     hand = GetMHandle(146);
  355.     if( Database )
  356.     {   EnableItem(hand,0);
  357.     } else DisableItem(hand,0);
  358.     DisableMenu = !flag;
  359.     DrawMenuBar();
  360. }
  361.  
  362.  
  363. static void DefineMenus()
  364. {
  365.     MenuHandle hand;
  366.     register long region;
  367.     register int i;
  368.     
  369.     /* Apple Menu */
  370.     hand = GetMenu(140);
  371.     AddResMenu(hand,'DRVR');
  372.     InsertMenu(hand,0);
  373.     
  374.     for( i=141; i<148; i++ )
  375.         InsertMenu( GetMenu(i), 0 );
  376.         
  377.     /* if( GetEnvirons(smRegionCode) == verUS ) */
  378.     /* SetItem(GetMHandle(144),0,'\pColors");   */
  379.     EnableMenus( True );
  380. }
  381.  
  382.  
  383. int OpenDisplay( x, y )
  384.     int x, y;
  385. {
  386. #ifndef EIGHTBIT
  387.     register CTabHandle cmap;
  388. #endif
  389.     register int i;
  390.     Rect rect;
  391.     
  392.     UseHourGlass = True;
  393.     MouseMode = MMRasMol;
  394.     DisableMenu = False;
  395.  
  396.     for( i=0; i<8; i++ )
  397.         DialValue[i] = 0.0;
  398.     
  399.     ULut[0] = ULut[255] = True;
  400.     RLut[255] = GLut[255] = BLut[255] = 0;
  401.     RLut[0] = GLut[0] = BLut[0] = 255;
  402.     
  403.     XRange = x;   WRange = XRange>>1;
  404.     YRange = y;   HRange = YRange>>1;
  405.     Range = MinFun(XRange,YRange);
  406.     
  407.     CanvWin = GetNewCWindow(150,0,(WindowPtr)-1);
  408.     SetPort(CanvWin);
  409.     
  410.     SizeWindow(CanvWin,x+15,y+15,TRUE);
  411.     ShowWindow(CanvWin);
  412.     
  413.     /* Load Cursors */
  414.     CanvCursor = GetCursor(160);
  415.     CmndCursor = GetCursor(1);
  416.     WaitCursor = GetCursor(4);   
  417.     
  418.     /* Create Scroll Bars */
  419.     rect.left = -1;  rect.right = x+1;  
  420.     rect.top = y;    rect.bottom = y+16;
  421.     HScroll = NewControl(CanvWin,&rect,"\p",TRUE,
  422.                          50,0,100,scrollBarProc,0L);
  423.     
  424.     rect.left = x;   rect.right = x+16;
  425.     rect.top = -1;   rect.bottom = y+1;
  426.     VScroll = NewControl(CanvWin,&rect,"\p",TRUE,
  427.                          50,0,100,scrollBarProc,0L);
  428.     
  429.     DrawGrowIcon(CanvWin);
  430.     
  431.     
  432.     /* PixMap! */
  433.     PixelMap = (PixMap*)NewPtr(sizeof(PixMap));
  434.     if( !PixelMap ) return( True );
  435.     
  436.     /* 72.0 DPI Resolution! */
  437.     PixelMap->hRes = 72;
  438.     PixelMap->vRes = 72;
  439.     PixelMap->bounds.left = 0;
  440.     PixelMap->bounds.top = 0;
  441.     PixelMap->cmpSize = 8;
  442.     
  443.     PixelMap->planeBytes = 0;
  444.     PixelMap->pmReserved = 0;
  445.     PixelMap->pmVersion = 0;
  446.     PixelMap->packType = 0;
  447.     PixelMap->packSize = 0;
  448.     
  449. #ifdef EIGHTBIT
  450.     /* Indexed PixMap */
  451.     PixelMap->pixelSize = 8;
  452.     PixelMap->pixelType = 0;
  453.     PixelMap->cmpCount = 1;
  454.     
  455.     PixelMap->pmTable = GetCTable(8);
  456.     (**PixelMap->pmTable).ctSeed = GetCTSeed();
  457.     
  458.     CMap = NewPalette(256,(CTabHandle)0,pmTolerant,0);
  459.     SetPalette(CanvWin,CMap,True);
  460.     CMapClean = True;
  461. #else
  462.     /* Direct PixMap */
  463.     PixelMap->pixelSize = 32;
  464.     PixelMap->cmpSize = 8;
  465.     PixelMap->pixelType = RGBDirect;
  466.     PixelMap->cmpCount = 3;
  467.     
  468.     i = sizeof(ColorTable) - sizeof(CSpecArray);
  469.     cmap = (CTabHandle)NewHandle(i);
  470.     (**cmap).ctSeed = 32;
  471.     (**cmap).ctFlags = 0;
  472.     (**cmap).ctSize = 0;
  473.     PixelMap->pmTable = cmap;
  474. #endif
  475.     
  476.     /* Initialise Palette! */
  477.     for( i=1; i<255; i++ )
  478.         ULut[i] = False;
  479.     AllocateColourMap();
  480.     
  481.     PrintHand = (THPrint)NULL;
  482.     BackHand = NewPixPat();
  483.     DefineMenus();
  484.     return(False);
  485. }
  486.  
  487. void CloseDisplay()
  488. {
  489. }
  490.  
  491.  
  492. void BeginWait()
  493. {
  494.     register WindowPtr win;
  495.     
  496.     if( UseHourGlass )
  497.     {   win = FrontWindow();
  498.         if( win==CanvWin || win==CmndWin )
  499.             SetCursor(*WaitCursor);
  500.     }
  501. }
  502.  
  503. #ifdef __CONDITIONALMACROS__
  504. #define ArrowCursor   SetCursor(&qd.arrow)
  505. #else
  506. #define ArrowCursor   SetCursor(&arrow)
  507. #endif
  508.  
  509. void EndWait()
  510. {
  511.     register WindowPtr win;
  512.     GrafPtr savePort;
  513.     Point pos;
  514.     
  515.     /* if( UseHourGlass )? */
  516.     
  517.     win = FrontWindow();
  518.     if( win==CanvWin )
  519.     {   GetPort(&savePort);
  520.         SetPort(CanvWin);
  521.         GetMouse(&pos);
  522.         
  523.         if( (pos.h>0) && (pos.v>0) &&
  524.             (pos.v<CanvWin->portRect.bottom-15) &&
  525.             (pos.h<CanvWin->portRect.right-15) )
  526.         {   SetCursor(*CanvCursor);
  527.         } else ArrowCursor;
  528.         SetPort(savePort);
  529.     } else if( win==CmndWin )
  530.     {   GetPort(&savePort);
  531.         SetPort(CmndWin);
  532.         GetMouse(&pos);
  533.         
  534.         if( (pos.h>0) && (pos.v>0) &&
  535.             (pos.v<CmndWin->portRect.bottom) &&
  536.             (pos.h<CmndWin->portRect.right-15) )
  537.         {   SetCursor(*CmndCursor);
  538.         } else ArrowCursor;
  539.         SetPort(savePort);
  540.     }
  541. }
  542.