PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/code_comm/applemac.c

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