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.

375 lines
8.9 KiB

  1. #include "hwxobj.h"
  2. #include "memmgr.h"
  3. #include "cmnhdr.h"
  4. #ifdef UNDER_CE // Windows CE Stub for unsupported APIs
  5. #include "stub_ce.h"
  6. #endif // UNDER_CE
  7. // implementation of CHwxStroke class
  8. CHwxStroke::CHwxStroke(BOOL bForward,long lSize = 32):CHwxObject(NULL)
  9. {
  10. m_bForward = bForward;
  11. m_nSize = lSize;
  12. m_pStroke = NULL;
  13. m_pCurStroke = NULL;
  14. m_nStroke = 0;
  15. m_ppt = NULL;
  16. m_cpt = 0;
  17. m_max = 0;
  18. m_hPen = NULL;
  19. }
  20. CHwxStroke::~CHwxStroke()
  21. {
  22. if ( m_ppt )
  23. {
  24. // delete [] m_ppt;
  25. MemFree((void *)m_ppt);
  26. m_ppt = NULL;
  27. m_cpt = 0;
  28. m_max = 0;
  29. }
  30. DeleteAllStroke();
  31. if ( m_hPen )
  32. {
  33. DeleteObject(m_hPen);
  34. m_hPen = NULL;
  35. }
  36. }
  37. BOOL CHwxStroke::Initialize(TCHAR * pClsName)
  38. {
  39. BOOL bRet = CHwxObject::Initialize(pClsName);
  40. if ( bRet )
  41. {
  42. m_hPen = CreatePen(PS_SOLID,3,GetSysColor(COLOR_WINDOWTEXT));
  43. if ( !m_hPen )
  44. bRet = FALSE;
  45. }
  46. return bRet;
  47. }
  48. BOOL
  49. CHwxStroke::ResetPen(VOID)
  50. {
  51. if(m_hPen) {
  52. ::DeleteObject(m_hPen);
  53. m_hPen = ::CreatePen(PS_SOLID,3,GetSysColor(COLOR_WINDOWTEXT));
  54. }
  55. return TRUE;
  56. }
  57. BOOL CHwxStroke::AddPoint(POINT pt)
  58. {
  59. if ( m_cpt >= m_max )
  60. {
  61. if ( !growPointBuffer() )
  62. {
  63. return FALSE;
  64. }
  65. }
  66. m_ppt[m_cpt++] = pt;
  67. return TRUE;
  68. }
  69. BOOL CHwxStroke::AddBoxStroke(int nLogBox,int nCurBox, int nBoxHeight)
  70. {
  71. // PSTROKE pst = (PSTROKE) new BYTE[sizeof(STROKE) + m_cpt * sizeof(POINT)];
  72. PSTROKE pst = (PSTROKE)MemAlloc(sizeof(STROKE) + m_cpt * sizeof(POINT));
  73. if (!pst)
  74. return FALSE;
  75. pst->cpt = m_cpt;
  76. pst->iBox = nLogBox;
  77. pst->xLeft = nCurBox * nBoxHeight;
  78. pst->pNext = NULL;
  79. memcpy(pst->apt, m_ppt, m_cpt*sizeof(POINT));
  80. m_cpt = 0;
  81. if ( m_bForward )
  82. {
  83. pst->pNext = m_pStroke;
  84. m_pStroke = pst;
  85. }
  86. else
  87. {
  88. PSTROKE pstrPrev = m_pStroke;
  89. if (pstrPrev == (PSTROKE) NULL)
  90. m_pStroke = pst;
  91. else
  92. {
  93. while (pstrPrev->pNext != (PSTROKE) NULL)
  94. pstrPrev = pstrPrev->pNext;
  95. pstrPrev->pNext = pst;
  96. }
  97. }
  98. m_pCurStroke = pst;
  99. m_nStroke++;
  100. return TRUE;
  101. }
  102. void CHwxStroke::EraseCurrentStroke()
  103. {
  104. PSTROKE pstrPrev = (PSTROKE) NULL;
  105. PSTROKE pstr = m_pStroke;
  106. if ( !pstr )
  107. return;
  108. if ( m_bForward )
  109. {
  110. // delete at the beginning of the list
  111. m_pStroke = pstr->pNext;
  112. pstr->pNext = (PSTROKE)NULL;
  113. m_pCurStroke = m_pStroke;
  114. }
  115. else
  116. {
  117. // delete at the end of the list
  118. while (pstr->pNext)
  119. {
  120. pstrPrev = pstr;
  121. pstr = pstr->pNext;
  122. }
  123. if (pstrPrev == (PSTROKE) NULL)
  124. m_pStroke = (PSTROKE) NULL;
  125. else
  126. pstrPrev->pNext = (PSTROKE) NULL;
  127. m_pCurStroke = pstrPrev;
  128. }
  129. // delete [] pstr;
  130. MemFree((void *)pstr);
  131. m_nStroke--;
  132. }
  133. void CHwxStroke::DeleteAllStroke()
  134. {
  135. PSTROKE pstr = m_pStroke;
  136. PSTROKE ptmp;
  137. m_pStroke = m_pCurStroke = (PSTROKE) NULL;
  138. m_nStroke = 0;
  139. while( pstr )
  140. {
  141. ptmp = pstr->pNext;
  142. pstr->pNext = (PSTROKE) NULL;
  143. // delete [] pstr;
  144. MemFree((void *)pstr);
  145. pstr = ptmp;
  146. }
  147. }
  148. void CHwxStroke::DrawStroke(HDC hdc,int nPts,BOOL bEntire)
  149. {
  150. #ifdef UNDER_CE // does not support SPI_SETNONCLIENTMETRICS on WM_SETTINGCHANGE
  151. if(bEntire){
  152. ResetPen();
  153. }
  154. #endif // UNDER_CE
  155. PSTROKE pstr;
  156. HPEN hOldPen = (HPEN)SelectObject(hdc,m_hPen);
  157. if ( bEntire )
  158. {
  159. pstr = m_pStroke;
  160. while (pstr)
  161. {
  162. Polyline(hdc, pstr->apt, pstr->cpt);
  163. pstr = pstr->pNext;
  164. }
  165. }
  166. else
  167. {
  168. if ( nPts == -2 ) // draw the current stroke just added
  169. {
  170. if ( m_pCurStroke )
  171. Polyline(hdc, m_pCurStroke->apt, m_pCurStroke->cpt);
  172. }
  173. else if ( nPts == -1 ) // draw the entire point buffer
  174. {
  175. if ( m_ppt )
  176. Polyline(hdc, m_ppt, m_cpt);
  177. }
  178. else
  179. { // draw the partial of the point buffer
  180. if ( m_ppt && nPts < m_cpt )
  181. Polyline(hdc,&m_ppt[m_cpt-nPts],nPts);
  182. }
  183. }
  184. SelectObject(hdc,hOldPen);
  185. }
  186. void CHwxStroke::GetUpdateRect(RECT * prc)
  187. {
  188. if ( prc && m_ppt )
  189. {
  190. RECT rc;
  191. int x,y;
  192. if (m_cpt < 2)
  193. x = 0, y = 0;
  194. else
  195. x = m_cpt - 2, y = m_cpt - 1;
  196. if (m_ppt[x].x < m_ppt[y].x)
  197. rc.left = m_ppt[x].x, rc.right = m_ppt[y].x;
  198. else
  199. rc.left = m_ppt[y].x, rc.right = m_ppt[x].x;
  200. if (m_ppt[x].y < m_ppt[y].y)
  201. rc.top = m_ppt[x].y, rc.bottom = m_ppt[y].y;
  202. else
  203. rc.top = m_ppt[y].y, rc.bottom = m_ppt[x].y;
  204. rc.left -= 1;
  205. rc.top -= 1;
  206. rc.right += 1;
  207. rc.bottom += 1;
  208. *prc = rc;
  209. }
  210. }
  211. PSTROKE CHwxStroke::CopyCurrentStroke()
  212. {
  213. if ( !m_pCurStroke )
  214. return NULL;
  215. // PSTROKE pst = (PSTROKE) new BYTE[sizeof(STROKE) + m_pCurStroke->cpt * sizeof(POINT)];
  216. PSTROKE pst = (PSTROKE)MemAlloc(sizeof(STROKE) + m_pCurStroke->cpt * sizeof(POINT));
  217. if ( !pst )
  218. return NULL;
  219. pst->cpt = m_pCurStroke->cpt;
  220. pst->iBox = m_pCurStroke->iBox;
  221. pst->xLeft = m_pCurStroke->xLeft;
  222. memcpy(pst->apt, m_pCurStroke->apt, m_pCurStroke->cpt*sizeof(POINT));
  223. pst->pNext = NULL;
  224. return pst;
  225. }
  226. // This function is used to copy ink from MB to CAC.
  227. // Their stroke orders are different.
  228. CHwxStroke & CHwxStroke::operator=(CHwxStroke & stroke)
  229. {
  230. // if ( *this != stroke )
  231. // {
  232. this->DeleteAllStroke();
  233. this->resetPointBuffer();
  234. if ( m_pStroke = stroke.dupStroke() )
  235. {
  236. if ( m_bForward )
  237. {
  238. m_pCurStroke = m_pStroke;
  239. }
  240. else
  241. {
  242. PSTROKE pst = m_pStroke;
  243. while ( pst->pNext )
  244. pst = pst->pNext;
  245. m_pCurStroke = pst;
  246. }
  247. m_nStroke = stroke.GetNumStrokes();
  248. }
  249. else
  250. {
  251. m_pCurStroke = (PSTROKE)NULL;
  252. m_nStroke = 0;
  253. }
  254. // }
  255. return *this;
  256. }
  257. BOOL CHwxStroke::growPointBuffer()
  258. {
  259. // POINT *ppnt = (POINT *) new BYTE[sizeof(POINT) * (m_nSize + m_max)];
  260. POINT *ppnt = (POINT *)MemAlloc(sizeof(POINT) * (m_nSize + m_max));
  261. if (ppnt == (POINT *) NULL)
  262. return FALSE;
  263. if (m_ppt != (POINT *) NULL)
  264. {
  265. memcpy(ppnt, m_ppt, m_max * sizeof(POINT));
  266. // delete [] m_ppt;
  267. MemFree((void *)m_ppt);
  268. }
  269. m_ppt = ppnt;
  270. m_max += m_nSize;
  271. return TRUE;
  272. }
  273. void CHwxStroke::resetPointBuffer()
  274. {
  275. if ( m_max && m_ppt )
  276. memset(m_ppt, '\0', m_max * sizeof(POINT));
  277. m_cpt = 0;
  278. }
  279. // This function copies the ink in reverse order
  280. // and only supports the transition from MB to CAC.
  281. PSTROKE CHwxStroke::dupStroke()
  282. {
  283. PSTROKE pstr = (PSTROKE)NULL;
  284. PSTROKE pstrHead = (PSTROKE)NULL;
  285. PSTROKE pCurr;
  286. if ( m_pStroke )
  287. {
  288. pCurr = m_pStroke;
  289. while (pCurr)
  290. {
  291. // if ((pstr = (PSTROKE) new BYTE[sizeof(STROKE) + pCurr->cpt * sizeof(POINT)]) == (PSTROKE) NULL)
  292. if ((pstr = (PSTROKE)MemAlloc(sizeof(STROKE) + pCurr->cpt * sizeof(POINT))) == (PSTROKE) NULL)
  293. {
  294. break;
  295. }
  296. pstr->pNext = (STROKE *) NULL;
  297. pstr->cpt = pCurr->cpt;
  298. pstr->iBox = pCurr->iBox;
  299. pstr->xLeft = pCurr->xLeft;
  300. memcpy(pstr->apt, pCurr->apt, pCurr->cpt * sizeof(POINT));
  301. pstr->pNext = pstrHead;
  302. pstrHead = pstr;
  303. pCurr = pCurr->pNext;
  304. }
  305. }
  306. return pstrHead;
  307. }
  308. // This function adjusts points relative to the box
  309. // when copying ink from MB to CAC.
  310. void CHwxStroke::ScaleInkXY(long x,long y)
  311. {
  312. if ( m_pStroke )
  313. {
  314. PSTROKE pCurr;
  315. int i;
  316. pCurr = m_pStroke;
  317. if ( x )
  318. {
  319. while (pCurr)
  320. {
  321. for( i = 0; i < pCurr->cpt; i++ )
  322. {
  323. pCurr->apt[i].x -= x;
  324. }
  325. pCurr = pCurr->pNext;
  326. }
  327. }
  328. if ( y )
  329. {
  330. pCurr = m_pStroke;
  331. while (pCurr)
  332. {
  333. for( i = 0; i < pCurr->cpt; i++ )
  334. {
  335. pCurr->apt[i].y -= y;
  336. }
  337. pCurr = pCurr->pNext;
  338. }
  339. }
  340. }
  341. }