Source code of Windows XP (NT5)
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.

418 lines
7.8 KiB

  1. /************************************************************/
  2. /* Windows Write, Copyright 1985-1992 Microsoft Corporation */
  3. /************************************************************/
  4. /* util2.c -- less frequently used utility routines */
  5. #define NOVIRTUALKEYCODES
  6. #define NOCTLMGR
  7. #define NOWINMESSAGES
  8. #define NOWINSTYLES
  9. #define NOCLIPBOARD
  10. #define NOGDICAPMASKS
  11. #define NOSYSMETRICS
  12. #define NOMENUS
  13. #define NOCOMM
  14. #define NOSOUND
  15. #include <windows.h>
  16. #include "mw.h"
  17. #include "doslib.h"
  18. #include "str.h"
  19. #include "machdefs.h"
  20. #include "cmddefs.h"
  21. #include "propdefs.h"
  22. #include "fkpdefs.h"
  23. #include "docdefs.h"
  24. #include "debug.h"
  25. #include "editdefs.h"
  26. #include "wwdefs.h"
  27. #define NOKCCODES
  28. #include "ch.h"
  29. extern struct DOD (**hpdocdod)[];
  30. extern HANDLE hMmwModInstance;
  31. extern CHAR vchDecimal; /* "decimal point" character */
  32. extern int viDigits;
  33. extern BOOL vbLZero;
  34. CHAR *PchFillPchId(PCH, int, int);
  35. FillStId(st, idpmt, cchIn)
  36. CHAR *st;
  37. IDPMT idpmt;
  38. int cchIn;
  39. { /* load string from resource file to buffer, the string is zero
  40. terminated, make it into a st, i.e. cch (excluding '\0' is stored
  41. in the 1st byte of the string) */
  42. int cch = LoadString(hMmwModInstance, idpmt, (LPSTR)&st[1], cchIn-1);
  43. Assert(cch != 0);
  44. st[0] = cch;
  45. } /* FillStId */
  46. CHAR *PchFillPchId(sz, idstr, cchIn)
  47. register CHAR * sz;
  48. register int idstr;
  49. int cchIn;
  50. { /*
  51. Description: load string from resource file to buffer, the
  52. string loaded is zero terminated
  53. Returns: pointer to '\0' last loaded
  54. */
  55. int cch = LoadString(hMmwModInstance, idstr, (LPSTR)sz, cchIn);
  56. /* Note: cch does not include the '\0' */
  57. {
  58. char msg[80];
  59. if (cch == 0)
  60. {
  61. wsprintf(msg,"bad resource id: 0x%x\n\r",idstr);
  62. OutputDebugString(msg);
  63. }
  64. Assert(cch != 0);
  65. }
  66. return(sz + cch);
  67. } /* end of PchFillPchId */
  68. int FDirtyDoc(doc)
  69. register int doc;
  70. { /* Return true if killing this doc would lose editing */
  71. register struct DOD *pdod;
  72. return ((pdod = &(**hpdocdod)[doc])->fDirty && pdod->cref == 1);
  73. } /* end of F D i r t y D o c */
  74. int ncvtu(n, ppch)
  75. register int n;
  76. CHAR **ppch;
  77. {
  78. register int cch = 0;
  79. if (n < 0)
  80. {
  81. *(*ppch)++ = '-';
  82. n = -n;
  83. ++cch;
  84. }
  85. if (n >= 10)
  86. {
  87. cch += ncvtu(n / 10, ppch);
  88. n %= 10;
  89. }
  90. else if ((n == 0) && !vbLZero) // then no leading zero
  91. return 0;
  92. *(*ppch)++ = '0' + n;
  93. return cch + 1;
  94. } /* end of n c v t uR */
  95. HANDLE HszGlobalCreate( sz )
  96. CHAR *sz;
  97. { /* Create handle for string in global windows heap. return the handle. */
  98. HANDLE h;
  99. LPCH lpch;
  100. int cch=CchSz( sz );
  101. if ((h=GlobalAlloc( GMEM_MOVEABLE, (LONG)cch )) != NULL)
  102. {
  103. if ((lpch = GlobalLock( h )) != NULL )
  104. {
  105. bltbx( (LPSTR) sz, lpch, cch );
  106. GlobalUnlock( h );
  107. }
  108. else
  109. {
  110. GlobalFree( h );
  111. return NULL;
  112. }
  113. }
  114. return h;
  115. }
  116. #ifdef DEBUG
  117. fnScribble( dchPos, ch )
  118. int dchPos;
  119. CHAR ch;
  120. { /* Scribble a char dchPos char positions from the UR screen corner */
  121. /* We create a special device context to avoid interfering with the */
  122. /* ones MEMO uses */
  123. extern struct WWD rgwwd[];
  124. static unsigned dxpScribbleChar=0;
  125. static unsigned dypScribbleChar;
  126. static unsigned ypScribble;
  127. int xp = wwdCurrentDoc.xpMac - (dxpScribbleChar * (dchPos+1));
  128. int ilevel = SaveDC( wwdCurrentDoc.hDC );
  129. SelectObject( wwdCurrentDoc.hDC, GetStockObject(ANSI_FIXED_FONT) );
  130. if ( dxpScribbleChar == 0 )
  131. { /* First time through */
  132. TEXTMETRIC tm;
  133. GetTextMetrics( wwdCurrentDoc.hDC, (LPTEXTMETRIC)&tm );
  134. dxpScribbleChar = tm.tmAveCharWidth;
  135. dypScribbleChar = tm.tmHeight + tm.tmInternalLeading;
  136. ypScribble = (dypScribbleChar >> 2) + wwdCurrentDoc.ypMin;
  137. }
  138. PatBlt( wwdCurrentDoc.hDC, xp, ypScribble, dxpScribbleChar, dypScribbleChar,
  139. WHITENESS );
  140. TextOut( wwdCurrentDoc.hDC, xp, ypScribble, (LPSTR) &ch, 1 );
  141. RestoreDC( wwdCurrentDoc.hDC, ilevel );
  142. }
  143. #endif /* DEBUG */
  144. /* original util3.c starts from here */
  145. #define iMaxOver10 3276
  146. extern int utCur;
  147. /* Must agree with cmddefs.h */
  148. extern CHAR *mputsz[];
  149. /* Must agree with cmddefs.h */
  150. unsigned mputczaUt[utMax] =
  151. {
  152. czaInch, czaCm, czaP10, czaP12, czaPoint,
  153. czaLine
  154. };
  155. int FZaFromSs(pza, ss, cch, ut)
  156. int *pza;
  157. CHAR ss[];
  158. int cch,
  159. ut;
  160. { /* Return za in *pza from string representation in ss. True if valid za */
  161. long lza = 0;
  162. register CHAR *pch = ss;
  163. register CHAR *pchMac = &ss[cch];
  164. int ch;
  165. unsigned czaUt;
  166. int fNeg;
  167. if (cch <= 0)
  168. return false;
  169. switch (*--pchMac)
  170. { /* Check for units */
  171. case 'n': /* inch */
  172. if (*--pchMac != 'i')
  173. goto NoUnits;
  174. case '"': /* inch */
  175. ut = utInch;
  176. break;
  177. #ifdef CASHMERE /* units such as pt, pt12, pt10 */
  178. case '0': /* pt10 */
  179. if (*--pchMac != '1' || *--pchMac != 'p')
  180. goto NoUnits;
  181. ut = utP10;
  182. break;
  183. case '2': /* pt12 */
  184. if (*--pchMac != '1' || *--pchMac != 'p')
  185. goto NoUnits;
  186. ut = utP12;
  187. break;
  188. case 'i': /* line */
  189. if (*--pchMac != 'l')
  190. goto NoUnits;
  191. ut = utLine;
  192. break;
  193. case 't': /* pt */
  194. if (*--pchMac != 'p')
  195. goto NoUnits;
  196. ut = utPoint;
  197. break;
  198. #endif /* CASHMERE */
  199. case 'm': /* cm */
  200. if (*--pchMac != 'c')
  201. goto NoUnits;
  202. ut = utCm;
  203. break;
  204. default:
  205. ++pchMac;
  206. break;
  207. NoUnits:
  208. pchMac = &ss[cch];
  209. }
  210. while (pch < pchMac && *(pchMac - 1) == chSpace)
  211. --pchMac;
  212. czaUt = mputczaUt[ut];
  213. /* extract leading blanks */
  214. while (*pch == ' ')
  215. pch++;
  216. fNeg = *pch == '-';
  217. if (fNeg) ++pch; /* skip past minus sign */
  218. while ((ch = *pch++) != vchDecimal)
  219. {
  220. if ((ch < '0' || ch > '9') || lza >= iMaxOver10)
  221. return false;
  222. lza = lza * 10 + (ch - '0') * czaUt;
  223. if (pch >= pchMac)
  224. goto GotNum;
  225. }
  226. while (pch < pchMac)
  227. {
  228. ch = *pch++;
  229. if (ch < '0' || ch > '9')
  230. return false;
  231. lza += ((ch - '0') * czaUt + 5) / 10;
  232. czaUt = (czaUt + 5) / 10;
  233. }
  234. GotNum:
  235. if (lza > ((long) (22 * czaInch)))
  236. return false;
  237. *pza = fNeg ? (int) -lza : (int) lza;
  238. return true;
  239. }
  240. int
  241. CchExpZa(ppch, za, ut, cchMax)
  242. CHAR **ppch;
  243. int ut, cchMax;
  244. register int za;
  245. { /* Stuff the expansion of linear measure za in unit ut into pch.
  246. Return # of chars stuffed. Don't exceed cchMax. */
  247. register int cch = 0;
  248. unsigned czaUt;
  249. int zu;
  250. /* If not in point mode and even half line, display as half lines v. points */
  251. if (ut == utPoint && utCur != utPoint &&
  252. (za / (czaLine / 2) * (czaLine / 2)) == za)
  253. ut = utLine;
  254. czaUt = mputczaUt[ut];
  255. if (cchMax < cchMaxNum)
  256. return 0;
  257. if (za < 0)
  258. { /* Output minus sign and make positive */
  259. *(*ppch)++ = '-';
  260. za = -za;
  261. cch++;
  262. }
  263. /* round off to two decimal places */
  264. za += czaUt / 200;
  265. zu = za / czaUt; /* Get integral part */
  266. cch += ncvtu(zu, ppch); /* Expand integral part */
  267. za -= zu * czaUt; /* Retain fraction part */
  268. if (((za *= 10) >= czaUt || za * 10 >= czaUt) && (viDigits > 0))
  269. { /* Check *10 first because of possible overflow */
  270. zu = za / czaUt;
  271. *(*ppch)++ = vchDecimal;
  272. cch++;
  273. *(*ppch)++ = '0' + zu;
  274. cch++;
  275. zu = ((za - zu * czaUt) * 10) / czaUt;
  276. if ((zu != 0) && (viDigits > 1))
  277. {
  278. *(*ppch)++ = '0' + zu;
  279. cch++;
  280. }
  281. }
  282. if (cch <= 1)
  283. /* force zeroes */
  284. {
  285. if ((cch == 0) && vbLZero) // then no leading zero
  286. {
  287. *(*ppch)++ = '0';
  288. cch++;
  289. }
  290. *(*ppch)++ = vchDecimal;
  291. cch++;
  292. if (viDigits > 0)
  293. {
  294. *(*ppch)++ = '0';
  295. cch++;
  296. }
  297. if (viDigits > 1)
  298. {
  299. *(*ppch)++ = '0';
  300. cch++;
  301. }
  302. }
  303. cch += CchStuff(ppch, mputsz[ut], cchMax - cch);
  304. return cch;
  305. }
  306. #ifdef KEEPALL /* Use FPdxaFromItDxa2Id */
  307. int DxaFromSt(st, ut)
  308. register CHAR *st;
  309. int ut;
  310. {
  311. int za;
  312. if (*st > 0 && FZaFromSs(&za, st+1, *st, ut)) /* see util.c */
  313. return za;
  314. else
  315. return valNil;
  316. }
  317. int DxaFromItem(it)
  318. int it;
  319. {
  320. int za;
  321. register CHAR stBuf[32];
  322. GetItTextValue(it, stBuf);
  323. if (*stBuf > 0 && FZaFromSs(&za, stBuf+1, *stBuf, utCur)) /* see util.c */
  324. return (za == valNil) ? 0 : za;
  325. else
  326. return valNil;
  327. }
  328. #endif
  329. int CchStuff(ppch, sz, cchMax)
  330. CHAR **ppch, sz[];
  331. int cchMax;
  332. {
  333. register int cch = 0;
  334. register CHAR *pch = *ppch;
  335. while (cchMax-- > 0 && (*pch = *sz++) != 0)
  336. {
  337. cch++;
  338. pch++;
  339. }
  340. if (cchMax < 0)
  341. bltbyte("...", pch - 3, 3);
  342. *ppch = pch;
  343. return cch;
  344. }
  345.