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.

408 lines
9.2 KiB

  1. /*************************************************************************
  2. **
  3. ** OLE 2 Sample Code
  4. **
  5. ** outltxtl.c
  6. **
  7. ** This file contains TextLine methods and related support functions.
  8. **
  9. ** (c) Copyright Microsoft Corp. 1992 - 1993 All Rights Reserved
  10. **
  11. *************************************************************************/
  12. #include "outline.h"
  13. OLEDBGDATA
  14. extern LPOUTLINEAPP g_lpApp;
  15. /* TextLine_Create
  16. * ---------------
  17. *
  18. * Create a text line object and return the pointer
  19. */
  20. LPTEXTLINE TextLine_Create(HDC hDC, UINT nTab, LPSTR lpszText)
  21. {
  22. LPTEXTLINE lpTextLine;
  23. lpTextLine=(LPTEXTLINE) New((DWORD)sizeof(TEXTLINE));
  24. if (lpTextLine == NULL) {
  25. OleDbgAssertSz(lpTextLine!=NULL,"Error allocating TextLine");
  26. return NULL;
  27. }
  28. TextLine_Init(lpTextLine, nTab, hDC);
  29. if (lpszText) {
  30. lpTextLine->m_nLength = lstrlen(lpszText);
  31. lstrcpy((LPSTR)lpTextLine->m_szText, lpszText);
  32. } else {
  33. lpTextLine->m_nLength = 0;
  34. lpTextLine->m_szText[0] = '\0';
  35. }
  36. TextLine_CalcExtents(lpTextLine, hDC);
  37. return(lpTextLine);
  38. }
  39. /* TextLine_Init
  40. * -------------
  41. *
  42. * Calculate the width/height of a text line object.
  43. */
  44. void TextLine_Init(LPTEXTLINE lpTextLine, int nTab, HDC hDC)
  45. {
  46. Line_Init((LPLINE)lpTextLine, nTab, hDC); // init the base class fields
  47. ((LPLINE)lpTextLine)->m_lineType = TEXTLINETYPE;
  48. lpTextLine->m_nLength = 0;
  49. lpTextLine->m_szText[0] = '\0';
  50. }
  51. /* TextLine_Delete
  52. * ---------------
  53. *
  54. * Delete the TextLine structure
  55. */
  56. void TextLine_Delete(LPTEXTLINE lpTextLine)
  57. {
  58. Delete((LPVOID)lpTextLine);
  59. }
  60. /* TextLine_Edit
  61. * -------------
  62. *
  63. * Edit the text line object.
  64. *
  65. * Returns TRUE if line was changed
  66. * FALSE if the line was NOT changed
  67. */
  68. BOOL TextLine_Edit(LPTEXTLINE lpLine, HWND hWndDoc, HDC hDC)
  69. {
  70. #if defined( USE_FRAMETOOLS )
  71. LPFRAMETOOLS lptb = OutlineApp_GetFrameTools(g_lpApp);
  72. #endif
  73. BOOL fStatus = FALSE;
  74. #if defined( USE_FRAMETOOLS )
  75. FrameTools_FB_GetEditText(lptb, lpLine->m_szText, sizeof(lpLine->m_szText));
  76. #else
  77. if (! InputTextDlg(hWndDoc, lpLine->m_szText, "Edit Line"))
  78. return FALSE;
  79. #endif
  80. lpLine->m_nLength = lstrlen(lpLine->m_szText);
  81. TextLine_CalcExtents(lpLine, hDC);
  82. fStatus = TRUE;
  83. return fStatus;
  84. }
  85. /* TextLine_CalcExtents
  86. * --------------------
  87. *
  88. * Calculate the width/height of a text line object.
  89. */
  90. void TextLine_CalcExtents(LPTEXTLINE lpTextLine, HDC hDC)
  91. {
  92. SIZE size;
  93. LPLINE lpLine = (LPLINE)lpTextLine;
  94. if (lpTextLine->m_nLength) {
  95. GetTextExtentPoint(hDC, lpTextLine->m_szText,
  96. lpTextLine->m_nLength, &size);
  97. lpLine->m_nWidthInHimetric=size.cx;
  98. lpLine->m_nHeightInHimetric=size.cy;
  99. } else {
  100. // we still need to calculate proper height even for NULL string
  101. TEXTMETRIC tm;
  102. GetTextMetrics(hDC, &tm);
  103. // required to set height
  104. lpLine->m_nHeightInHimetric = tm.tmHeight;
  105. lpLine->m_nWidthInHimetric = 0;
  106. }
  107. #if defined( _DEBUG )
  108. {
  109. RECT rc;
  110. rc.left = 0;
  111. rc.top = 0;
  112. rc.right = XformWidthInHimetricToPixels(hDC,
  113. lpLine->m_nWidthInHimetric);
  114. rc.bottom = XformHeightInHimetricToPixels(hDC,
  115. lpLine->m_nHeightInHimetric);
  116. OleDbgOutRect3("TextLine_CalcExtents", (LPRECT)&rc);
  117. }
  118. #endif
  119. }
  120. /* TextLine_SetHeightInHimetric
  121. * ----------------------------
  122. *
  123. * Set the height of a textline object.
  124. */
  125. void TextLine_SetHeightInHimetric(LPTEXTLINE lpTextLine, int nHeight)
  126. {
  127. if (!lpTextLine)
  128. return;
  129. ((LPLINE)lpTextLine)->m_nHeightInHimetric = nHeight;
  130. }
  131. /* TextLine_GetTextLen
  132. * -------------------
  133. *
  134. * Return length of string of the TextLine (not considering the tab level).
  135. */
  136. int TextLine_GetTextLen(LPTEXTLINE lpTextLine)
  137. {
  138. return lstrlen((LPSTR)lpTextLine->m_szText);
  139. }
  140. /* TextLine_GetTextData
  141. * --------------------
  142. *
  143. * Return the string of the TextLine (not considering the tab level).
  144. */
  145. void TextLine_GetTextData(LPTEXTLINE lpTextLine, LPSTR lpszBuf)
  146. {
  147. lstrcpy(lpszBuf, (LPSTR)lpTextLine->m_szText);
  148. }
  149. /* TextLine_GetOutlineData
  150. * -----------------------
  151. *
  152. * Return the CF_OUTLINE format data for the TextLine.
  153. */
  154. BOOL TextLine_GetOutlineData(LPTEXTLINE lpTextLine, LPTEXTLINE lpBuf)
  155. {
  156. TextLine_Copy((LPTEXTLINE)lpTextLine, lpBuf);
  157. return TRUE;
  158. }
  159. /* TextLine_Draw
  160. * -------------
  161. *
  162. * Draw a text line object on a DC.
  163. * Parameters:
  164. * hDC - DC to which the line will be drawn
  165. * lpRect - the object rectangle in logical coordinates
  166. * lpRectWBounds - bounding rect of the metafile underneath hDC
  167. * (NULL if hDC is not a metafile DC)
  168. * this is used by ContainerLine_Draw to draw the OLE obj
  169. * fHighlight - TRUE use selection highlight text color
  170. */
  171. void TextLine_Draw(
  172. LPTEXTLINE lpTextLine,
  173. HDC hDC,
  174. LPRECT lpRect,
  175. LPRECT lpRectWBounds,
  176. BOOL fHighlight
  177. )
  178. {
  179. RECT rc;
  180. int nBkMode;
  181. COLORREF clrefOld;
  182. if (!lpTextLine)
  183. return;
  184. rc = *lpRect;
  185. rc.left += ((LPLINE)lpTextLine)->m_nTabWidthInHimetric;
  186. rc.right += ((LPLINE)lpTextLine)->m_nTabWidthInHimetric;
  187. nBkMode = SetBkMode(hDC, TRANSPARENT);
  188. if (fHighlight) {
  189. /*Get proper txt colors */
  190. clrefOld = SetTextColor(hDC,GetSysColor(COLOR_HIGHLIGHTTEXT));
  191. }
  192. else {
  193. clrefOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
  194. }
  195. ExtTextOut(
  196. hDC,
  197. rc.left,
  198. rc.top,
  199. ETO_CLIPPED,
  200. (LPRECT)&rc,
  201. lpTextLine->m_szText,
  202. lpTextLine->m_nLength,
  203. (LPINT) NULL /* default char spacing */
  204. );
  205. SetTextColor(hDC, clrefOld);
  206. SetBkMode(hDC, nBkMode);
  207. }
  208. /* TextLine_DrawSelHilight
  209. * -----------------------
  210. *
  211. * Handles selection of textline
  212. */
  213. void TextLine_DrawSelHilight(LPTEXTLINE lpTextLine, HDC hDC, LPRECT lpRect, UINT itemAction, UINT itemState)
  214. {
  215. if (itemAction & ODA_SELECT) {
  216. // check if there is a selection state change, ==> invert rect
  217. if (itemState & ODS_SELECTED) {
  218. if (!((LPLINE)lpTextLine)->m_fSelected) {
  219. ((LPLINE)lpTextLine)->m_fSelected = TRUE;
  220. InvertRect(hDC, (LPRECT)lpRect);
  221. }
  222. } else {
  223. if (((LPLINE)lpTextLine)->m_fSelected) {
  224. ((LPLINE)lpTextLine)->m_fSelected = FALSE;
  225. InvertRect(hDC, lpRect);
  226. }
  227. }
  228. } else if (itemAction & ODA_DRAWENTIRE) {
  229. ((LPLINE)lpTextLine)->m_fSelected=((itemState & ODS_SELECTED) ? TRUE : FALSE);
  230. InvertRect(hDC, lpRect);
  231. }
  232. }
  233. /* TextLine_Copy
  234. * -------------
  235. *
  236. * Duplicate a textline
  237. */
  238. BOOL TextLine_Copy(LPTEXTLINE lpSrcLine, LPTEXTLINE lpDestLine)
  239. {
  240. _fmemcpy(lpDestLine, lpSrcLine, sizeof(TEXTLINE));
  241. return TRUE;
  242. }
  243. /* TextLine_CopyToDoc
  244. * ------------------
  245. *
  246. * Copy a textline to another Document (usually ClipboardDoc)
  247. */
  248. BOOL TextLine_CopyToDoc(LPTEXTLINE lpSrcLine, LPOUTLINEDOC lpDestDoc, int nIndex)
  249. {
  250. LPTEXTLINE lpDestLine;
  251. BOOL fStatus = FALSE;
  252. lpDestLine = (LPTEXTLINE) New((DWORD)sizeof(TEXTLINE));
  253. if (lpDestLine == NULL) {
  254. OleDbgAssertSz(lpDestLine!=NULL,"Error allocating TextLine");
  255. return FALSE;
  256. }
  257. if (TextLine_Copy(lpSrcLine, lpDestLine)) {
  258. OutlineDoc_AddLine(lpDestDoc, (LPLINE)lpDestLine, nIndex);
  259. fStatus = TRUE;
  260. }
  261. return fStatus;
  262. }
  263. /* TextLine_SaveToStg
  264. * ------------------
  265. *
  266. * Save a textline into a storage
  267. *
  268. * Return TRUE if successful, FALSE otherwise
  269. */
  270. BOOL TextLine_SaveToStm(LPTEXTLINE lpTextLine, LPSTREAM lpLLStm)
  271. {
  272. HRESULT hrErr;
  273. ULONG nWritten;
  274. USHORT nLengthOnDisk;
  275. nLengthOnDisk = (USHORT) lpTextLine->m_nLength;
  276. hrErr = lpLLStm->lpVtbl->Write(
  277. lpLLStm,
  278. (LPVOID)&nLengthOnDisk,
  279. sizeof(nLengthOnDisk),
  280. &nWritten
  281. );
  282. if (hrErr != NOERROR) {
  283. OleDbgOutHResult("Write TextLine data (1) returned", hrErr);
  284. return FALSE;
  285. }
  286. hrErr = lpLLStm->lpVtbl->Write(
  287. lpLLStm,
  288. (LPVOID)lpTextLine->m_szText,
  289. lpTextLine->m_nLength,
  290. &nWritten
  291. );
  292. if (hrErr != NOERROR) {
  293. OleDbgOutHResult("Write TextLine data (2) returned", hrErr);
  294. return FALSE;
  295. }
  296. return TRUE;
  297. }
  298. /* TextLine_LoadFromStg
  299. * --------------------
  300. *
  301. * Load a textline from storage
  302. */
  303. LPLINE TextLine_LoadFromStg(LPSTORAGE lpSrcStg, LPSTREAM lpLLStm, LPOUTLINEDOC lpDestDoc)
  304. {
  305. HRESULT hrErr;
  306. ULONG nRead;
  307. LPTEXTLINE lpTextLine;
  308. USHORT nLengthOnDisk;
  309. lpTextLine=(LPTEXTLINE) New((DWORD)sizeof(TEXTLINE));
  310. if (lpTextLine == NULL) {
  311. OleDbgAssertSz(lpTextLine!=NULL,"Error allocating TextLine");
  312. return NULL;
  313. }
  314. TextLine_Init(lpTextLine, 0, NULL);
  315. hrErr = lpLLStm->lpVtbl->Read(
  316. lpLLStm,
  317. (LPVOID)&nLengthOnDisk,
  318. sizeof(nLengthOnDisk),
  319. &nRead
  320. );
  321. if (hrErr != NOERROR) {
  322. OleDbgOutHResult("Read TextLine data (1) returned", hrErr);
  323. return NULL;
  324. }
  325. lpTextLine->m_nLength = (UINT) nLengthOnDisk;
  326. OleDbgAssert(lpTextLine->m_nLength < sizeof(lpTextLine->m_szText));
  327. hrErr = lpLLStm->lpVtbl->Read(
  328. lpLLStm,
  329. (LPVOID)&lpTextLine->m_szText,
  330. lpTextLine->m_nLength,
  331. &nRead
  332. );
  333. if (hrErr != NOERROR) {
  334. OleDbgOutHResult("Read TextLine data (1) returned", hrErr);
  335. return NULL;
  336. }
  337. lpTextLine->m_szText[lpTextLine->m_nLength] = '\0'; // add str terminator
  338. return (LPLINE)lpTextLine;
  339. }