Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

549 lines
15 KiB

  1. /************************************************************/
  2. /* Windows Write, Copyright 1985-1992 Microsoft Corporation */
  3. /************************************************************/
  4. /* picture2.c -- MW format and display routines for pictures */
  5. //#define NOGDICAPMASKS
  6. #define NOWINMESSAGES
  7. #define NOVIRTUALKEYCODES
  8. #define NOWINSTYLES
  9. #define NOCLIPBOARD
  10. #define NOCTLMGR
  11. #define NOSYSMETRICS
  12. #define NOMENUS
  13. #define NOICON
  14. #define NOKEYSTATE
  15. //#define NOATOM
  16. #define NOCREATESTRUCT
  17. #define NODRAWTEXT
  18. #define NOFONT
  19. #define NOMB
  20. #define NOMENUS
  21. #define NOOPENFILE
  22. #define NOREGION
  23. #define NOSCROLL
  24. #define NOSOUND
  25. #define NOWH
  26. #define NOWINOFFSETS
  27. #define NOWNDCLASS
  28. #define NOCOMM
  29. #include <windows.h>
  30. #include "mw.h"
  31. #define NOKCCODES
  32. #include "ch.h"
  33. #include "docdefs.h"
  34. #include "fmtdefs.h"
  35. #include "dispdefs.h"
  36. #include "cmddefs.h"
  37. #include "propdefs.h"
  38. #include "stcdefs.h"
  39. #include "wwdefs.h"
  40. #include "filedefs.h"
  41. #include "editdefs.h"
  42. /* #include "str.h" */
  43. #include "prmdefs.h"
  44. /* #include "fkpdefs.h" */
  45. /* #include "macro.h" */
  46. #include "winddefs.h"
  47. #if defined(OLE)
  48. #include "obj.h"
  49. #endif
  50. extern typeCP cpMacCur;
  51. extern int docCur;
  52. extern int vfSelHidden;
  53. extern struct WWD rgwwd[];
  54. extern int wwCur;
  55. extern int wwMac;
  56. extern struct FLI vfli;
  57. extern struct SEL selCur;
  58. extern struct WWD *pwwdCur;
  59. extern struct PAP vpapCache;
  60. extern typeCP vcpFirstParaCache;
  61. extern typeCP vcpLimParaCache;
  62. extern int vfPictSel;
  63. extern struct PAP vpapAbs;
  64. extern struct SEP vsepAbs;
  65. extern struct SEP vsepPage;
  66. extern struct DOD (**hpdocdod)[];
  67. extern unsigned cwHeapFree;
  68. extern int vfInsertOn;
  69. extern int vfPMS;
  70. extern int dxpLogInch;
  71. extern int dypLogInch;
  72. extern int dxaPrPage;
  73. extern int dyaPrPage;
  74. extern int dxpPrPage;
  75. extern int dypPrPage;
  76. extern HBRUSH hbrBkgrnd;
  77. extern long ropErase;
  78. extern int vdocBitmapCache;
  79. extern typeCP vcpBitmapCache;
  80. extern HBITMAP vhbmBitmapCache;
  81. extern HCURSOR vhcIBeam;
  82. /* Used in this module only */
  83. #ifdef DEBUG
  84. #define STATIC static
  85. #else
  86. #define STATIC
  87. #endif
  88. /* (windows naming convention for func name, not Hung.) */
  89. long GetBitmapMultipliers( hDC, dxpOrig, dypOrig, dxmmIdeal, dymmIdeal )
  90. HDC hDC;
  91. int dxpOrig, dypOrig;
  92. int dxmmIdeal, dymmIdeal;
  93. { /* Return the "best" integer bit-multiples to use when displaying a bitmap
  94. of size { dxpOrig, dypOrig } (in pixels) on device DC hDC.
  95. The "ideal size" of the bitmap is { dxmmIdeal, dymmIdeal } (in 0.1mm units);
  96. this conveys the desired aspect ratio as well.
  97. Returns the y-multiplier in the hi word, the x-multiplier in the lo word.
  98. Default/error value returned is { 1, 1 }. */
  99. typedef unsigned long ul;
  100. long lT;
  101. int cx, cy;
  102. int cxBest, cyBest;
  103. int dcx=1, dcy=1;
  104. int dxpT, dypT;
  105. int dxmmOrig, dymmOrig;
  106. int dxmmDevice = GetDeviceCaps( hDC, HORZSIZE ) * 10;
  107. int dymmDevice = GetDeviceCaps( hDC, VERTSIZE ) * 10;
  108. int dxpDevice = GetDeviceCaps( hDC, HORZRES );
  109. int dypDevice = GetDeviceCaps( hDC, VERTRES );
  110. int cxMac, cyMac;
  111. int pctAspectBest, pctSizeBest;
  112. /* Compute scale factor (dcx, dcy, our minimum scale multiple) */
  113. if (GetDeviceCaps( hDC, RASTERCAPS ) & RC_SCALING)
  114. {
  115. POINT pt;
  116. pt.x = pt.y = 0; /* Just in case */
  117. Escape( hDC, GETSCALINGFACTOR, 0, (LPSTR) NULL, (LPSTR) (LPPOINT) &pt );
  118. dcx = 1 << pt.x;
  119. dcy = 1 << pt.y;
  120. }
  121. /* Compute size of unscaled picture on hDC in 0.1 mm units */
  122. if (dxpDevice <= 0 || dypDevice <= 0)
  123. goto Error;
  124. dxmmOrig = MultDiv( dxpOrig, dxmmDevice, dxpDevice );
  125. dymmOrig = MultDiv( dypOrig, dymmDevice, dypDevice );
  126. /* Ideal size not supplied; return 1,1 (times device multipliers) */
  127. if (dxmmIdeal <= 0 || dymmIdeal <= 0)
  128. {
  129. goto Error;
  130. }
  131. /* Compute absolute maximums for cx, cy */
  132. /* 2nd term of min restricts search space by refusing to consider
  133. more tham one size above the ideal */
  134. if (dxmmOrig <= 0 || dymmOrig <= 0)
  135. goto Error;
  136. cxMac = min ( (dxmmDevice / dxmmOrig) + 1, (dxmmIdeal / dxmmOrig) + 2 );
  137. cyMac = min ( (dymmDevice / dymmOrig) + 1, (dymmIdeal / dymmOrig) + 2 );
  138. /* Search all possible multiplies to see what would be best */
  139. cxBest = dcx;
  140. cyBest = dcy;
  141. pctAspectBest = pctSizeBest = 32767;
  142. for ( cx = dcx ; cx < cxMac; cx += dcx )
  143. for ( cy = dcy ; cy < cyMac; cy += dcy )
  144. {
  145. int dxmm = dxmmOrig * cx;
  146. int dymm = dymmOrig * cy;
  147. int pctAspect = PctDiffUl( (ul) dxmmIdeal * (ul) dymm,
  148. (ul) dymmIdeal * (ul) dxmm );
  149. int pctSize = PctDiffUl( (ul) dxmmIdeal * (ul) dymmIdeal,
  150. (ul)dxmm * (ul)dymm );
  151. /* ??? Strategy for loss on one, gain on the other ??? */
  152. if (pctAspect <= pctAspectBest && pctSize <= pctSizeBest )
  153. {
  154. cxBest = cx;
  155. cyBest = cy;
  156. pctAspectBest = pctAspect;
  157. pctSizeBest = pctSize;
  158. }
  159. }
  160. Assert( cxBest > 0 && cyBest > 0 );
  161. return MAKELONG( cxBest, cyBest );
  162. Error:
  163. return MAKELONG( dcx, dcy );
  164. }
  165. int PctDiffUl( ul1, ul2 )
  166. unsigned long ul1, ul2;
  167. { /* Return a number that is proportional to the percentage
  168. of difference between the two numbers */
  169. /* Will not work for > 0x7fffffff */
  170. #define dulMaxPrec 1000 /* # of "grains" of response possible */
  171. unsigned long ulAvg = (ul1 >> 1) + (ul2 >> 1);
  172. unsigned long ulDiff = (ul1 > ul2) ? ul1 - ul2 : ul2 - ul1;
  173. if (ulAvg == 0)
  174. return (ul1 == ul2) ? 0 : dulMaxPrec;
  175. if (ulDiff > 0xFFFFFFFF / dulMaxPrec)
  176. return dulMaxPrec;
  177. return (int) ((ulDiff * dulMaxPrec) / ulAvg);
  178. }
  179. int PxlConvert( mm, val, pxlDeviceRes, milDeviceRes )
  180. int mm;
  181. int val;
  182. int pxlDeviceRes;
  183. int milDeviceRes;
  184. { /* Return the # of pixels spanned by val, a measurement in coordinates
  185. appropriate to mapping mode mm. pxlDeviceRes gives the resolution
  186. of the device in pixels, along the axis of val. milDeviceRes gives
  187. the same resolution measurement, but in millimeters.
  188. returns 0 on error */
  189. typedef unsigned long ul;
  190. ul ulMaxInt = 32767L; /* Should be a constant, but as of 7/12/85,
  191. CMERGE generates incorrect code for the
  192. ul division if we use a constant */
  193. ul ulPxl;
  194. ul ulDenom;
  195. unsigned wMult=1;
  196. unsigned wDiv=1;
  197. if (milDeviceRes == 0)
  198. { /* to make sure we don't get divide-by-0 */
  199. return 0;
  200. }
  201. switch ( mm ) {
  202. case MM_LOMETRIC:
  203. wDiv = 10;
  204. break;
  205. case MM_HIMETRIC:
  206. wDiv = 100;
  207. break;
  208. case MM_TWIPS:
  209. wMult = 25;
  210. wDiv = 1440;
  211. break;
  212. case MM_LOENGLISH:
  213. wMult = 25;
  214. wDiv = 100;
  215. break;
  216. case MM_HIENGLISH:
  217. wMult = 25;
  218. wDiv = 1000;
  219. break;
  220. case MM_BITMAP:
  221. case MM_OLE:
  222. case MM_TEXT:
  223. return val;
  224. default:
  225. Assert( FALSE ); /* Bad mapping mode */
  226. case MM_ISOTROPIC:
  227. case MM_ANISOTROPIC:
  228. /* These picture types have no original size */
  229. return 0;
  230. }
  231. /* Add Denominator - 1 to Numerator, to avoid rounding down */
  232. ulDenom = (ul) wDiv * (ul) milDeviceRes;
  233. ulPxl = ((ul) ((ul) wMult * (ul) val * (ul) pxlDeviceRes) + ulDenom - 1) /
  234. ulDenom;
  235. return (ulPxl > ulMaxInt) ? 0 : (int) ulPxl;
  236. }
  237. /* F O R M A T G R A P H I C S */
  238. FormatGraphics(doc, cp, ichCp, cpMac, flm)
  239. int doc;
  240. typeCP cp;
  241. int ichCp;
  242. typeCP cpMac;
  243. int flm;
  244. { /* Format a line of graphics */
  245. CHAR rgch[10];
  246. int cch;
  247. int dypSize;
  248. int dxpSize;
  249. int dxaText;
  250. int dxa;
  251. struct PICINFOX picInfo;
  252. int fPrinting = flm & flmPrinting;
  253. GetPicInfo(cp, cpMac, doc, &picInfo);
  254. /* Compute the size of the pict in device pixels */
  255. if (picInfo.mfp.mm == MM_BITMAP && ((picInfo.dxaSize == 0) ||
  256. (picInfo.dyaSize == 0)))
  257. {
  258. GetBitmapSize( &dxpSize, &dypSize, &picInfo, fPrinting);
  259. }
  260. #if defined(OLE)
  261. else if (picInfo.mfp.mm == MM_OLE)
  262. {
  263. dxpSize = DxpFromDxa( picInfo.dxaSize, fPrinting );
  264. dypSize = DypFromDya( picInfo.dyaSize, fPrinting );
  265. dxpSize = MultDiv( dxpSize, picInfo.mx, mxMultByOne );
  266. dypSize = MultDiv( dypSize, picInfo.my, myMultByOne );
  267. }
  268. #endif
  269. else
  270. {
  271. dxpSize = DxpFromDxa( picInfo.dxaSize, fPrinting );
  272. dypSize = DypFromDya( picInfo.dyaSize, fPrinting );
  273. }
  274. if (fPrinting)
  275. {
  276. /* If we are printing, then the picture consists of a single
  277. band. */
  278. vfli.cpMac = vcpLimParaCache;
  279. vfli.ichCpMac = 0;
  280. vfli.dypLine = dypSize;
  281. }
  282. else if ((ichCp + 2) * dypPicSizeMin > dypSize)
  283. {
  284. /* Last band of picture. NOTE: last band is always WIDER than
  285. dypPicSizeMin */
  286. vfli.cpMac = vcpLimParaCache;
  287. vfli.ichCpMac = 0;
  288. #ifdef CASHMERE
  289. vfli.dypLine = dypSize - max(0, dypSize / dypPicSizeMin - 1) *
  290. dypPicSizeMin + DypFromDya( vpapAbs.dyaAfter, FALSE );
  291. #else /* not CASHMERE */
  292. vfli.dypLine = dypSize - max(0, dypSize / dypPicSizeMin - 1) *
  293. dypPicSizeMin;
  294. #endif /* not CASHMERE */
  295. }
  296. else
  297. {
  298. vfli.ichCpMac = vfli.ichCpMin + 1;
  299. vfli.cpMac = vfli.cpMin;
  300. vfli.dypLine = dypPicSizeMin;
  301. }
  302. #ifdef CASHMERE
  303. if (ichCp == 0) /* Add in the 'space before' field. */
  304. {
  305. vfli.dypLine += DypFromDya( vpapAbs.dyaBefore, fPrinting );
  306. }
  307. #endif /* CASHMERE */
  308. vfli.dypFont = vfli.dypLine;
  309. dxaText = vsepAbs.dxaText;
  310. switch (vpapAbs.jc)
  311. {
  312. case jcLeft:
  313. case jcBoth:
  314. dxa = picInfo.dxaOffset;
  315. break;
  316. case jcCenter:
  317. dxa = (dxaText - (int)vpapAbs.dxaRight + (int)vpapAbs.dxaLeft -
  318. DxaFromDxp( dxpSize, fPrinting )) >> 1;
  319. break;
  320. case jcRight:
  321. dxa = dxaText - (int)vpapAbs.dxaRight -
  322. DxaFromDxp( dxpSize, fPrinting );
  323. break;
  324. }
  325. vfli.xpLeft = DxpFromDxa( max( (int)vpapAbs.dxaLeft, dxa ), fPrinting );
  326. #ifdef BOGUSBL
  327. vfli.xpReal = imin( dxpSize + vfli.xpLeft,
  328. DxpFromDxa( dxaText - vpapAbs.dxaRight, fPrinting );
  329. #else /* Don't crunch the picture to fit the margins */
  330. vfli.xpReal = dxpSize + vfli.xpLeft;
  331. #endif
  332. vfli.fGraphics = true;
  333. }
  334. GetPicInfo(cp, cpMac, doc, ppicInfo)
  335. typeCP cp, cpMac;
  336. int doc;
  337. struct PICINFOX *ppicInfo;
  338. { /* Fetch the header structure for a picture at cp into *ppicInfo.
  339. Supports the OLD file format (which used cbOldSize); always returns
  340. the NEW PICINFO structure. */
  341. int cch;
  342. FetchRgch(&cch, ppicInfo, doc, cp, cpMac, cchPICINFOX);
  343. if (ppicInfo->mfp.mm & MM_EXTENDED)
  344. {
  345. ppicInfo->mfp.mm &= ~MM_EXTENDED;
  346. }
  347. else
  348. { /* Old file format -- fill out extended fields */
  349. ppicInfo->cbSize = ppicInfo->cbOldSize;
  350. ppicInfo->cbHeader = cchOldPICINFO;
  351. }
  352. /* Fill in defaults for extended fields that are not present in the file */
  353. /* These are: mx, my Added 9/19/85 by bryanl */
  354. if (BStructMember( PICINFOX, my ) >= ppicInfo->cbHeader )
  355. { /* Scaling multipliers not present */
  356. ppicInfo->mx = mxMultByOne;
  357. ppicInfo->my = myMultByOne;
  358. }
  359. if (ppicInfo->dyaSize < 0)
  360. /* 3.1 beta III bug, wrote negative height values */
  361. {
  362. ppicInfo->dyaSize = -ppicInfo->dyaSize;
  363. #ifdef DEBUG
  364. OutputDebugString("Negative object height found!\n\r");
  365. #endif
  366. }
  367. }
  368. GetBitmapSize( pdxp, pdyp, ppicInfo, fPrinting )
  369. int *pdxp, *pdyp;
  370. struct PICINFOX *ppicInfo;
  371. int fPrinting;
  372. { /* Compute the appropriate display or printing (depending on fPrinting)
  373. size of the bitmap described by the passed PICINFOX structure.
  374. The interesting fields are:
  375. ppicInfo->bm.bmWidth, bmHeight Bitmap size in pixels
  376. ppicInfo->mfp.xExt, yExt Desired size in 0.1 mm
  377. Return the results through *pdxp, *pdyp. */
  378. long GetBitmapMultipliers();
  379. extern HDC vhDCPrinter;
  380. extern int dxaPrPage, dxpPrPage, dyaPrPage, dypPrPage;
  381. long lT;
  382. int cx, cy;
  383. int dxpT, dypT;
  384. int dxpOrig = ppicInfo->bm.bmWidth;
  385. int dypOrig = ppicInfo->bm.bmHeight;
  386. int dxmmIdeal = ppicInfo->mfp.xExt;
  387. int dymmIdeal = ppicInfo->mfp.yExt;
  388. Assert(vhDCPrinter);
  389. /* Scale for printer */
  390. lT = GetBitmapMultipliers( vhDCPrinter, dxpOrig, dypOrig, dxmmIdeal, dymmIdeal );
  391. cx = LOWORD( lT );
  392. cy = HIWORD( lT );
  393. dxpT = cx * dxpOrig;
  394. dypT = cy * dypOrig;
  395. if (!fPrinting)
  396. { /* Re-scale for screen */
  397. dxpT = DxpFromDxa( DxaFromDxp( dxpT, TRUE ), FALSE );
  398. dypT = DypFromDya( DyaFromDyp( dypT, TRUE ), FALSE );
  399. }
  400. /* apply the user's "ideal multiple" of the computed size */
  401. dxpT = MultDiv( dxpT, ppicInfo->mx, mxMultByOne );
  402. dypT = MultDiv( dypT, ppicInfo->my, myMultByOne );
  403. *pdxp = dxpT;
  404. *pdyp = dypT;
  405. return;
  406. }
  407. int DxpFromDxa( dxa, fPrinter )
  408. int dxa;
  409. int fPrinter;
  410. { /* Given twips for an x-axis measurement, return printer
  411. or logical screen pixels */
  412. extern int dxpPrPage, dxaPrPage;
  413. extern int dxpLogInch;
  414. if (fPrinter)
  415. return MultDiv( dxa, dxpPrPage, dxaPrPage );
  416. else
  417. return MultDiv( dxa, dxpLogInch, czaInch );
  418. }
  419. int DxaFromDxp( dxp, fPrinter )
  420. int dxp;
  421. int fPrinter;
  422. { /* Given printer or logical screen pixels for an x-axis measurement,
  423. return twips */
  424. extern int dxpPrPage, dxaPrPage;
  425. extern int dxpLogInch;
  426. if (fPrinter)
  427. return MultDiv( dxp, dxaPrPage, dxpPrPage );
  428. else
  429. return MultDiv( dxp, czaInch, dxpLogInch );
  430. }
  431. int DypFromDya( dya, fPrinter )
  432. int dya;
  433. int fPrinter;
  434. { /* Given twips for a y-axis measurement, return printer or logical screen
  435. pixels */
  436. extern int dypPrPage, dyaPrPage;
  437. extern int dypLogInch;
  438. if (fPrinter)
  439. return MultDiv( dya, dypPrPage, dyaPrPage );
  440. else
  441. return MultDiv( dya, dypLogInch, czaInch );
  442. }
  443. int DyaFromDyp( dyp, fPrinter )
  444. int dyp;
  445. int fPrinter;
  446. { /* Given printer or logical screen pixels for a y-axis measurement,
  447. return twips */
  448. extern int dypPrPage, dyaPrPage;
  449. extern int dypLogInch;
  450. if (fPrinter)
  451. return MultDiv( dyp, dyaPrPage, dypPrPage );
  452. else
  453. return MultDiv( dyp, czaInch, dypLogInch );
  454. }
  455.