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.

5948 lines
167 KiB

  1. #include "ctlspriv.h"
  2. #pragma hdrstop
  3. #include "usrctl32.h"
  4. #include "edit.h"
  5. //---------------------------------------------------------------------------//
  6. //
  7. // Number of lines to bump when reallocating index buffer
  8. //
  9. #define LINEBUMP 32
  10. //
  11. // Used for ML scroll updates
  12. //
  13. #define ML_REFRESH 0xffffffff
  14. //---------------------------------------------------------------------------//
  15. //
  16. // Forwards
  17. //
  18. ICH EditML_Line(PED, ICH);
  19. VOID EditML_ShiftchLines(PED, ICH, int);
  20. VOID EditML_Char(PED, DWORD, int);
  21. VOID EditML_MouseMotion(PED, UINT, UINT, LPPOINT);
  22. BOOL EditML_EnsureCaretVisible(PED);
  23. VOID EditML_DrawText(PED, HDC, ICH, ICH, BOOL);
  24. BOOL EditML_InsertCrCrLf(PED);
  25. VOID EditML_StripCrCrLf(PED);
  26. VOID EditML_SetHandle(PED, HANDLE);
  27. LONG EditML_GetLine(PED, ICH, ICH, LPSTR);
  28. ICH EditML_LineIndex(PED, ICH);
  29. ICH EditML_LineLength(PED, ICH);
  30. VOID EditML_SetSelection(PED, BOOL, ICH, ICH);
  31. BOOL EditML_SetTabStops(PED, int, LPINT);
  32. BOOL EditML_Undo(PED);
  33. LONG EditML_Create(PED, LPCREATESTRUCT);
  34. //---------------------------------------------------------------------------//
  35. __inline void EditML_SanityCheck(PED ped)
  36. {
  37. UNREFERENCED_PARAMETER(ped); // For free build
  38. UserAssert(ped->cch >= ped->chLines[ped->cLines - 1]);
  39. }
  40. //---------------------------------------------------------------------------//
  41. //
  42. // EditML_GetLineWidth
  43. //
  44. // Returns the max width in a line. Edit_TabTheTextOut() ensures that max
  45. // width won't overflow.
  46. //
  47. UINT EditML_GetLineWidth(HDC hdc, LPSTR lpstr, int nCnt, PED ped)
  48. {
  49. return Edit_TabTheTextOut(hdc, 0, 0, 0, 0, lpstr, nCnt, 0, ped, 0, ECT_CALC, NULL);
  50. }
  51. //---------------------------------------------------------------------------//
  52. //
  53. // EditML_Size
  54. //
  55. // Handles resizing of the edit control window and updating thereof.
  56. //
  57. // Sets the edit field's formatting area given the passed in "client area".
  58. // We fudge it if it doesn't seem reasonable.
  59. //
  60. VOID EditML_Size(PED ped, BOOL fRedraw)
  61. {
  62. //
  63. // Calculate the # of lines we can fit in our rectangle.
  64. //
  65. ped->ichLinesOnScreen = (ped->rcFmt.bottom - ped->rcFmt.top) / ped->lineHeight;
  66. //
  67. // Make the format rectangle height an integral number of lines
  68. //
  69. ped->rcFmt.bottom = ped->rcFmt.top + ped->ichLinesOnScreen * ped->lineHeight;
  70. //
  71. // Rebuild the line array
  72. //
  73. if (ped->fWrap)
  74. {
  75. EditML_BuildchLines(ped, 0, 0, FALSE, NULL, NULL);
  76. EditML_UpdateiCaretLine(ped);
  77. }
  78. else
  79. {
  80. EditML_Scroll(ped, TRUE, ML_REFRESH, 0, fRedraw);
  81. EditML_Scroll(ped, FALSE, ML_REFRESH, 0, fRedraw);
  82. }
  83. }
  84. //---------------------------------------------------------------------------//
  85. //
  86. // EditML_CalcXOffset
  87. //
  88. // Calculates the horizontal offset (indent) required for centered
  89. // and right justified lines.
  90. //
  91. int EditML_CalcXOffset(PED ped, HDC hdc, int lineNumber)
  92. {
  93. PSTR pText;
  94. ICH lineLength;
  95. ICH lineWidth;
  96. if (ped->format == ES_LEFT)
  97. {
  98. return 0;
  99. }
  100. lineLength = EditML_Line(ped, lineNumber);
  101. if (lineLength)
  102. {
  103. pText = Edit_Lock(ped) + ped->chLines[lineNumber] * ped->cbChar;
  104. hdc = Edit_GetDC(ped, TRUE);
  105. lineWidth = EditML_GetLineWidth(hdc, pText, lineLength, ped);
  106. Edit_ReleaseDC(ped, hdc, TRUE);
  107. Edit_Unlock(ped);
  108. }
  109. else
  110. {
  111. lineWidth = 0;
  112. }
  113. //
  114. // If a SPACE or a TAB was eaten at the end of a line by EditML_BuildchLines
  115. // to prevent a delimiter appearing at the begining of a line, the
  116. // the following calculation will become negative causing this bug.
  117. // So, now, we take zero in such cases.
  118. // Fix for Bug #3566 --01/31/91-- SANKAR --
  119. //
  120. lineWidth = max(0, (int)(ped->rcFmt.right-ped->rcFmt.left-lineWidth));
  121. if (ped->format == ES_CENTER)
  122. {
  123. return (lineWidth / 2);
  124. }
  125. if (ped->format == ES_RIGHT)
  126. {
  127. //
  128. // Subtract 1 so that the 1 pixel wide cursor will be in the visible
  129. // region on the very right side of the screen.
  130. //
  131. return max(0, (int)(lineWidth-1));
  132. }
  133. return 0;
  134. }
  135. //---------------------------------------------------------------------------//
  136. //
  137. // Edit_MoveSelection AorW
  138. //
  139. // Moves the selection character in the direction indicated. Assumes
  140. // you are starting at a legal point, we decrement/increment the ich. Then,
  141. // This decrements/increments it some more to get past CRLFs...
  142. //
  143. ICH Edit_MoveSelection(PED ped, ICH ich, BOOL fLeft)
  144. {
  145. if (fLeft && ich > 0)
  146. {
  147. //
  148. // Move left
  149. //
  150. ich = Edit_PrevIch( ped, NULL, ich );
  151. if (ich)
  152. {
  153. if (ped->fAnsi)
  154. {
  155. LPSTR pText;
  156. //
  157. // Check for CRLF or CRCRLF
  158. //
  159. pText = Edit_Lock(ped) + ich;
  160. //
  161. // Move before CRLF or CRCRLF
  162. //
  163. if (*(WORD UNALIGNED *)(pText - 1) == 0x0A0D)
  164. {
  165. ich--;
  166. if (ich && *(pText - 2) == 0x0D)
  167. ich--;
  168. }
  169. Edit_Unlock(ped);
  170. }
  171. else
  172. {
  173. LPWSTR pwText;
  174. //
  175. // Check for CRLF or CRCRLF
  176. //
  177. pwText = (LPWSTR)Edit_Lock(ped) + ich;
  178. //
  179. // Move before CRLF or CRCRLF
  180. //
  181. if (*(pwText - 1) == 0x0D && *pwText == 0x0A)
  182. {
  183. ich--;
  184. if (ich && *(pwText - 2) == 0x0D)
  185. ich--;
  186. }
  187. Edit_Unlock(ped);
  188. }
  189. }
  190. }
  191. else if (!fLeft && ich < ped->cch)
  192. {
  193. //
  194. // Move right.
  195. //
  196. ich = Edit_NextIch( ped, NULL, ich );
  197. if (ich < ped->cch)
  198. {
  199. if (ped->fAnsi)
  200. {
  201. LPSTR pText;
  202. pText = Edit_Lock(ped) + ich;
  203. //
  204. // Move after CRLF
  205. //
  206. if (*(WORD UNALIGNED *)(pText - 1) == 0x0A0D)
  207. {
  208. ich++;
  209. }
  210. else
  211. {
  212. //
  213. // Check for CRCRLF
  214. //
  215. if (ich && *(WORD UNALIGNED *)pText == 0x0A0D && *(pText - 1) == 0x0D)
  216. {
  217. ich += 2;
  218. }
  219. }
  220. Edit_Unlock(ped);
  221. }
  222. else
  223. {
  224. LPWSTR pwText;
  225. pwText = (LPWSTR)Edit_Lock(ped) + ich;
  226. //
  227. // Move after CRLF
  228. //
  229. if (*(pwText - 1) == 0x0D && *pwText == 0x0A)
  230. {
  231. ich++;
  232. }
  233. else
  234. {
  235. //
  236. // Check for CRCRLF
  237. //
  238. if (ich && *(pwText - 1) == 0x0D && *pwText == 0x0D &&
  239. *(pwText + 1) == 0x0A)
  240. {
  241. ich += 2;
  242. }
  243. }
  244. Edit_Unlock(ped);
  245. }
  246. }
  247. }
  248. return ich;
  249. }
  250. //---------------------------------------------------------------------------//
  251. //
  252. // Edit_MoveSelectionRestricted AorW
  253. //
  254. // Moves the selection like Edit_MoveSelection, but also obeys limitations
  255. // imposed by some languages such as Thai, where the cursor cannot stop
  256. // between a character and it's attached vowel or tone marks.
  257. //
  258. // Only called if the language pack is loaded.
  259. //
  260. ICH Edit_MoveSelectionRestricted(PED ped, ICH ich, BOOL fLeft)
  261. {
  262. PSTR pText;
  263. HDC hdc;
  264. ICH ichResult;
  265. pText = Edit_Lock(ped);
  266. hdc = Edit_GetDC(ped, TRUE);
  267. ichResult = ped->pLpkEditCallout->EditMoveSelection((PED0)ped, hdc, pText, ich, fLeft);
  268. Edit_ReleaseDC(ped, hdc, TRUE);
  269. Edit_Unlock(ped);
  270. return ichResult;
  271. }
  272. //---------------------------------------------------------------------------//
  273. //
  274. // EditML_SetCaretPosition AorW
  275. //
  276. // If the window has the focus, find where the caret belongs and move
  277. // it there.
  278. //
  279. VOID EditML_SetCaretPosition(PED ped, HDC hdc)
  280. {
  281. POINT position;
  282. BOOL prevLine;
  283. int x = -20000;
  284. int y = -20000;
  285. //
  286. // We will only position the caret if we have the focus since we don't want
  287. // to move the caret while another window could own it.
  288. //
  289. if (!ped->fFocus || !IsWindowVisible(ped->hwnd))
  290. {
  291. return;
  292. }
  293. //
  294. // Find the position of the caret
  295. //
  296. if (!ped->fCaretHidden &&
  297. ((ICH) ped->iCaretLine >= ped->ichScreenStart) &&
  298. ((ICH) ped->iCaretLine < (ped->ichScreenStart + ped->ichLinesOnScreen)))
  299. {
  300. RECT rcRealFmt;
  301. if (ped->f40Compat)
  302. {
  303. GetClientRect(ped->hwnd, &rcRealFmt);
  304. IntersectRect(&rcRealFmt, &rcRealFmt, &ped->rcFmt);
  305. }
  306. else
  307. {
  308. CopyRect(&rcRealFmt, &ped->rcFmt);
  309. }
  310. if (ped->cLines - 1 != ped->iCaretLine && ped->ichCaret == ped->chLines[ped->iCaretLine + 1])
  311. {
  312. prevLine = TRUE;
  313. }
  314. else
  315. {
  316. prevLine = FALSE;
  317. }
  318. EditML_IchToXYPos(ped, hdc, ped->ichCaret, prevLine, &position);
  319. if ( (position.y >= rcRealFmt.top) &&
  320. (position.y <= rcRealFmt.bottom - ped->lineHeight))
  321. {
  322. int xPos = position.x;
  323. int cxCaret;
  324. SystemParametersInfo(SPI_GETCARETWIDTH, 0, (LPVOID)&cxCaret, 0);
  325. if (ped->fWrap ||
  326. ((xPos > (rcRealFmt.left - cxCaret)) &&
  327. (xPos <= rcRealFmt.right)))
  328. {
  329. //
  330. // Make sure the caret is in the visible region if word
  331. // wrapping. This is so that the caret will be visible if the
  332. // line ends with a space.
  333. //
  334. x = max(xPos, rcRealFmt.left);
  335. x = min(x, rcRealFmt.right - cxCaret);
  336. y = position.y;
  337. }
  338. }
  339. }
  340. if (ped->pLpkEditCallout)
  341. {
  342. SetCaretPos(x + ped->iCaretOffset, y);
  343. }
  344. else
  345. {
  346. SetCaretPos(x, y);
  347. }
  348. //
  349. // FE_IME : EditML_SetCaretPosition -- ImmSetCompositionWindow(CFS_RECT)
  350. //
  351. if (g_fIMMEnabled && ImmIsIME(GetKeyboardLayout(0)))
  352. {
  353. if (x != -20000 && y != -20000)
  354. {
  355. Edit_ImmSetCompositionWindow(ped, x, y);
  356. }
  357. }
  358. }
  359. //---------------------------------------------------------------------------//
  360. //
  361. // EditML_Line
  362. //
  363. // Returns the length of the line (cch) given by lineNumber ignoring any
  364. // CRLFs in the line.
  365. //
  366. ICH EditML_Line(PED ped, ICH lineNumber)
  367. {
  368. ICH result;
  369. if (lineNumber >= ped->cLines)
  370. {
  371. return 0;
  372. }
  373. if (lineNumber == ped->cLines - 1)
  374. {
  375. //
  376. // Since we can't have a CRLF on the last line
  377. //
  378. return (ped->cch - ped->chLines[ped->cLines - 1]);
  379. }
  380. else
  381. {
  382. result = ped->chLines[lineNumber + 1] - ped->chLines[lineNumber];
  383. TraceMsg(TF_STANDARD, "Edit: MLLine result=%d", result);
  384. //
  385. // Now check for CRLF or CRCRLF at end of line
  386. //
  387. if (result > 1)
  388. {
  389. if (ped->fAnsi)
  390. {
  391. LPSTR pText;
  392. pText = Edit_Lock(ped) + ped->chLines[lineNumber + 1] - 2;
  393. if (*(WORD UNALIGNED *)pText == 0x0A0D)
  394. {
  395. result -= 2;
  396. if (result && *(--pText) == 0x0D)
  397. {
  398. //
  399. // In case there was a CRCRLF
  400. //
  401. result--;
  402. }
  403. }
  404. }
  405. else
  406. {
  407. LPWSTR pwText;
  408. pwText = (LPWSTR)Edit_Lock(ped) + (ped->chLines[lineNumber + 1] - 2);
  409. if (*(DWORD UNALIGNED *)pwText == 0x000A000D)
  410. {
  411. result = result - 2;
  412. if (result && *(--pwText) == 0x0D)
  413. {
  414. //
  415. // In case there was a CRCRLF
  416. //
  417. result--;
  418. }
  419. }
  420. }
  421. Edit_Unlock(ped);
  422. }
  423. }
  424. return result;
  425. }
  426. //---------------------------------------------------------------------------//
  427. //
  428. // EditML_IchToLine AorW
  429. //
  430. // Returns the line number (starting from 0) which contains the given
  431. // character index. If ich is -1, return the line the first char in the
  432. // selection is on (the caret if no selection)
  433. //
  434. INT EditML_IchToLine(PED ped, ICH ich)
  435. {
  436. int iLo, iHi, iLine;
  437. iLo = 0;
  438. iHi = ped->cLines;
  439. if (ich == (ICH)-1)
  440. {
  441. ich = ped->ichMinSel;
  442. }
  443. while (iLo < iHi - 1)
  444. {
  445. iLine = max((iHi - iLo)/2, 1) + iLo;
  446. if (ped->chLines[iLine] > ich)
  447. {
  448. iHi = iLine;
  449. }
  450. else
  451. {
  452. iLo = iLine;
  453. }
  454. }
  455. return iLo;
  456. }
  457. //---------------------------------------------------------------------------//
  458. //
  459. // EditML_IchToYPos
  460. //
  461. // Given an ich, return its y coordinate with respect to the top line
  462. // displayed in the window. If prevLine is TRUE and if the ich is at the
  463. // beginning of the line, return the y coordinate of the
  464. // previous line (if it is not a CRLF).
  465. //
  466. // Added for the LPK (3Dec96) - with an LPK installed, calculating X position is
  467. // a far more processor intensive job. Where only the Y position is required
  468. // this routine should be called instead of EditML_IchToXYPos.
  469. //
  470. // Called only when LPK installed.
  471. //
  472. INT EditML_IchToYPos( PED ped, ICH ich, BOOL prevLine)
  473. {
  474. int iline;
  475. int yPosition;
  476. PSTR pText;
  477. //
  478. // Determine what line the character is on
  479. //
  480. iline = EditML_IchToLine(ped, ich);
  481. //
  482. // Calc. the yPosition now. Note that this may change by the height of one
  483. // char if the prevLine flag is set and the ICH is at the beginning of a line.
  484. //
  485. yPosition = (iline - ped->ichScreenStart) * ped->lineHeight + ped->rcFmt.top;
  486. pText = Edit_Lock(ped);
  487. if (prevLine && iline && (ich == ped->chLines[iline]) &&
  488. (!AWCOMPARECHAR(ped, pText + (ich - 2) * ped->cbChar, 0x0D) ||
  489. !AWCOMPARECHAR(ped, pText + (ich - 1) * ped->cbChar, 0x0A)))
  490. {
  491. //
  492. // First char in the line. We want Y position of the previous
  493. // line if we aren't at the 0th line.
  494. //
  495. iline--;
  496. yPosition = yPosition - ped->lineHeight;
  497. }
  498. Edit_Unlock(ped);
  499. return yPosition;
  500. }
  501. //---------------------------------------------------------------------------//
  502. //
  503. // EditML_IchToXYPos
  504. //
  505. // Given an ich, return its x,y coordinates with respect to the top
  506. // left character displayed in the window. Returns the coordinates of the top
  507. // left position of the char. If prevLine is TRUE then if the ich is at the
  508. // beginning of the line, we will return the coordinates to the right of the
  509. // last char on the previous line (if it is not a CRLF).
  510. //
  511. VOID EditML_IchToXYPos(PED ped, HDC hdc, ICH ich, BOOL prevLine, LPPOINT ppt)
  512. {
  513. int iline;
  514. ICH cch;
  515. int xPosition, yPosition;
  516. int xOffset;
  517. //
  518. // For horizontal scroll displacement on left justified text and
  519. // for indent on centered or right justified text
  520. //
  521. PSTR pText, pTextStart, pLineStart;
  522. //
  523. // Determine what line the character is on
  524. //
  525. iline = EditML_IchToLine(ped, ich);
  526. //
  527. // Calc. the yPosition now. Note that this may change by the height of one
  528. // char if the prevLine flag is set and the ICH is at the beginning of a line.
  529. //
  530. yPosition = (iline - ped->ichScreenStart) * ped->lineHeight + ped->rcFmt.top;
  531. //
  532. // Now determine the xPosition of the character
  533. //
  534. pTextStart = Edit_Lock(ped);
  535. if (prevLine && iline && (ich == ped->chLines[iline]) &&
  536. (!AWCOMPARECHAR(ped, pTextStart + (ich - 2) * ped->cbChar, 0x0D) ||
  537. !AWCOMPARECHAR(ped, pTextStart + (ich - 1) * ped->cbChar, 0x0A)))
  538. {
  539. //
  540. // First char in the line. We want text extent upto end of the previous
  541. // line if we aren't at the 0th line.
  542. //
  543. iline--;
  544. yPosition = yPosition - ped->lineHeight;
  545. pLineStart = pTextStart + ped->chLines[iline] * ped->cbChar;
  546. //
  547. // Note that we are taking the position in front of any CRLFs in the
  548. // text.
  549. //
  550. cch = EditML_Line(ped, iline);
  551. }
  552. else
  553. {
  554. pLineStart = pTextStart + ped->chLines[iline] * ped->cbChar;
  555. pText = pTextStart + ich * ped->cbChar;
  556. //
  557. // Strip off CRLF or CRCRLF. Note that we may be pointing to a CR but in
  558. // which case we just want to strip off a single CR or 2 CRs.
  559. //
  560. //
  561. // We want pText to point to the first CR at the end of the line if
  562. // there is one. Thus, we will get an xPosition to the right of the last
  563. // visible char on the line otherwise we will be to the left of
  564. // character ich.
  565. //
  566. //
  567. // Check if we at the end of text
  568. //
  569. if (ich < ped->cch)
  570. {
  571. if (ped->fAnsi)
  572. {
  573. if (ich && *(WORD UNALIGNED *)(pText - 1) == 0x0A0D)
  574. {
  575. pText--;
  576. if (ich > 2 && *(pText - 1) == 0x0D)
  577. {
  578. pText--;
  579. }
  580. }
  581. }
  582. else
  583. {
  584. LPWSTR pwText = (LPWSTR)pText;
  585. if (ich && *(DWORD UNALIGNED *)(pwText - 1) == 0x000A000D)
  586. {
  587. pwText--;
  588. if (ich > 2 && *(pwText - 1) == 0x0D)
  589. {
  590. pwText--;
  591. }
  592. }
  593. pText = (LPSTR)pwText;
  594. }
  595. }
  596. if (pText < pLineStart)
  597. {
  598. pText = pLineStart;
  599. }
  600. cch = (ICH)(pText - pLineStart)/ped->cbChar;
  601. }
  602. //
  603. // Find out how many pixels we indent the line for funny formats
  604. //
  605. if (ped->pLpkEditCallout)
  606. {
  607. //
  608. // Must find position at start of character offset cch from start of line.
  609. // This depends on the layout and the reading order
  610. //
  611. xPosition = ped->pLpkEditCallout->EditIchToXY(
  612. (PED0)ped, hdc, pLineStart, EditML_Line(ped, iline), cch);
  613. }
  614. else
  615. {
  616. if (ped->format != ES_LEFT)
  617. {
  618. xOffset = EditML_CalcXOffset(ped, hdc, iline);
  619. }
  620. else
  621. {
  622. xOffset = -(int)ped->xOffset;
  623. }
  624. xPosition = ped->rcFmt.left + xOffset +
  625. EditML_GetLineWidth(hdc, pLineStart, cch, ped);
  626. }
  627. Edit_Unlock(ped);
  628. ppt->x = xPosition;
  629. ppt->y = yPosition;
  630. return;
  631. }
  632. //---------------------------------------------------------------------------//
  633. //
  634. // EditML_MouseToIch AorW
  635. //
  636. // Returns the closest cch to where the mouse point is. Also optionally
  637. // returns lineindex in pline (So that we can tell if we are at the beginning
  638. // of the line or end of the previous line.)
  639. //
  640. ICH EditML_MouseToIch(PED ped, HDC hdc, LPPOINT mousePt, LPICH pline)
  641. {
  642. int xOffset;
  643. LPSTR pLineStart;
  644. int height = mousePt->y;
  645. int line; //WASINT
  646. int width = mousePt->x;
  647. ICH cch;
  648. ICH cLineLength;
  649. ICH cLineLengthNew;
  650. ICH cLineLengthHigh;
  651. ICH cLineLengthLow;
  652. ICH cLineLengthTemp;
  653. int textWidth;
  654. int iCurWidth;
  655. int lastHighWidth, lastLowWidth;
  656. //
  657. // First determine which line the mouse is pointing to.
  658. //
  659. line = ped->ichScreenStart;
  660. if (height <= ped->rcFmt.top)
  661. {
  662. //
  663. // Either return 0 (the very first line, or one line before the top line
  664. // on the screen. Note that these are signed mins and maxes since we
  665. // don't expect (or allow) more than 32K lines.
  666. //
  667. line = max(0, line-1);
  668. }
  669. else if (height >= ped->rcFmt.bottom)
  670. {
  671. //
  672. // Are we below the last line displayed
  673. //
  674. line = min(line+(int)ped->ichLinesOnScreen, (int)(ped->cLines-1));
  675. }
  676. else
  677. {
  678. //
  679. // We are somewhere on a line visible on screen
  680. //
  681. line = min(line + (int)((height - ped->rcFmt.top) / ped->lineHeight),
  682. (int)(ped->cLines - 1));
  683. }
  684. //
  685. // Now determine what horizontal character the mouse is pointing to.
  686. //
  687. pLineStart = Edit_Lock(ped) + ped->chLines[line] * ped->cbChar;
  688. cLineLength = EditML_Line(ped, line); // Length is sans CRLF or CRCRLF
  689. TraceMsg(TF_STANDARD, "Edit: EditML_Line(ped=%x, line=%d) returned %d", ped, line, cLineLength);
  690. UserAssert((int)cLineLength >= 0);
  691. //
  692. // If the language pack is loaded, visual and logical character order
  693. // may differ.
  694. //
  695. if (ped->pLpkEditCallout)
  696. {
  697. //
  698. // Use the language pack to find the character nearest the cursor.
  699. //
  700. cch = ped->chLines[line] + ped->pLpkEditCallout->EditMouseToIch
  701. ((PED0)ped, hdc, pLineStart, cLineLength, width);
  702. }
  703. else
  704. {
  705. //
  706. // xOffset will be a negative value for center and right justified lines.
  707. // ie. We will just displace the lines left by the amount of indent for
  708. // right and center justification. Note that ped->xOffset will be 0 for
  709. // these lines since we don't support horizontal scrolling with them.
  710. //
  711. if (ped->format != ES_LEFT)
  712. {
  713. xOffset = EditML_CalcXOffset(ped, hdc, line);
  714. }
  715. else
  716. {
  717. //
  718. // So that we handle a horizontally scrolled window for left justified
  719. // text.
  720. //
  721. xOffset = 0;
  722. }
  723. width = width - xOffset;
  724. //
  725. // The code below is tricky... I depend on the fact that ped->xOffset is 0
  726. // for right and center justified lines
  727. //
  728. //
  729. // Now find out how many chars fit in the given width
  730. //
  731. if (width >= ped->rcFmt.right)
  732. {
  733. //
  734. // Return 1+last char in line or one plus the last char visible
  735. //
  736. cch = Edit_CchInWidth(ped, hdc, pLineStart, cLineLength,
  737. ped->rcFmt.right - ped->rcFmt.left + ped->xOffset, TRUE);
  738. //
  739. // Consider DBCS in case of width >= ped->rcFmt.right
  740. //
  741. // Since Edit_CchInWidth and EditML_LineLength takes care of DBCS, we only need to
  742. // worry about if the last character is a double byte character or not.
  743. //
  744. // cch = ped->chLines[line] + min( Edit_NextIch(ped, pLineStart, cch), cLineLength);
  745. //
  746. // we need to adjust the position. LiZ -- 5/5/93
  747. //
  748. if (ped->fAnsi && ped->fDBCS)
  749. {
  750. ICH cch2 = min(cch+1,cLineLength);
  751. if (Edit_AdjustIch(ped, pLineStart, cch2) != cch2)
  752. {
  753. //
  754. // Displayed character on the right edge is DBCS
  755. //
  756. cch = min(cch+2,cLineLength);
  757. }
  758. else
  759. {
  760. cch = cch2;
  761. }
  762. cch += ped->chLines[line];
  763. }
  764. else
  765. {
  766. cch = ped->chLines[line] + min(cch + 1, cLineLength);
  767. }
  768. }
  769. else if (width <= ped->rcFmt.left + ped->aveCharWidth / 2)
  770. {
  771. //
  772. // Return first char in line or one minus first char visible. Note that
  773. // ped->xOffset is 0 for right and centered text so we will just return
  774. // the first char in the string for them. (Allow a avecharwidth/2
  775. // positioning border so that the user can be a little off...
  776. //
  777. cch = Edit_CchInWidth(ped, hdc, pLineStart, cLineLength, ped->xOffset, TRUE);
  778. if (cch)
  779. {
  780. cch--;
  781. }
  782. cch = Edit_AdjustIch( ped, pLineStart, cch );
  783. cch += ped->chLines[line];
  784. }
  785. else
  786. {
  787. if (cLineLength == 0)
  788. {
  789. cch = ped->chLines[line];
  790. goto edUnlock;
  791. }
  792. iCurWidth = width + ped->xOffset - ped->rcFmt.left;
  793. //
  794. // If the user clicked past the end of the text, return the last character
  795. //
  796. lastHighWidth = EditML_GetLineWidth(hdc, pLineStart, cLineLength, ped);
  797. if (lastHighWidth <= iCurWidth)
  798. {
  799. cLineLengthNew = cLineLength;
  800. goto edAdjust;
  801. }
  802. //
  803. // Now the mouse is somewhere on the visible portion of the text
  804. // remember cch contains the length of the line.
  805. //
  806. cLineLengthLow = 0;
  807. cLineLengthHigh = cLineLength + 1;
  808. lastLowWidth = 0;
  809. while (cLineLengthLow < cLineLengthHigh - 1)
  810. {
  811. cLineLengthNew = (cLineLengthHigh + cLineLengthLow) / 2;
  812. if (ped->fAnsi && ped->fDBCS)
  813. {
  814. //
  815. // EditML_GetLineWidth returns meaningless value for truncated DBCS.
  816. //
  817. cLineLengthTemp = Edit_AdjustIch(ped, pLineStart, cLineLengthNew);
  818. textWidth = EditML_GetLineWidth(hdc, pLineStart, cLineLengthTemp, ped);
  819. }
  820. else
  821. {
  822. textWidth = EditML_GetLineWidth(hdc, pLineStart, cLineLengthNew, ped);
  823. }
  824. if (textWidth > iCurWidth)
  825. {
  826. cLineLengthHigh = cLineLengthNew;
  827. lastHighWidth = textWidth;
  828. }
  829. else
  830. {
  831. cLineLengthLow = cLineLengthNew;
  832. lastLowWidth = textWidth;
  833. }
  834. }
  835. //
  836. // When the while ends, you can't know the exact desired position.
  837. // Try to see if the mouse pointer was on the farest half
  838. // of the char we got and if so, adjust cch.
  839. //
  840. if (cLineLengthLow == cLineLengthNew)
  841. {
  842. //
  843. // Need to compare with lastHighWidth
  844. //
  845. if ((lastHighWidth - iCurWidth) < (iCurWidth - textWidth))
  846. {
  847. cLineLengthNew++;
  848. }
  849. }
  850. else
  851. {
  852. //
  853. // Need to compare with lastLowHigh
  854. //
  855. if ((iCurWidth - lastLowWidth) < (textWidth - iCurWidth))
  856. {
  857. cLineLengthNew--;
  858. }
  859. }
  860. edAdjust:
  861. cLineLength = Edit_AdjustIch( ped, pLineStart, cLineLengthNew );
  862. cch = ped->chLines[line] + cLineLength;
  863. }
  864. }
  865. edUnlock:
  866. Edit_Unlock(ped);
  867. if (pline)
  868. {
  869. *pline = line;
  870. }
  871. return cch;
  872. }
  873. //---------------------------------------------------------------------------//
  874. //
  875. // EditML_ChangeSelection AorW
  876. //
  877. // Changes the current selection to have the specified starting and
  878. // ending values. Properly highlights the new selection and unhighlights
  879. // anything deselected. If NewMinSel and NewMaxSel are out of order, we swap
  880. // them. Doesn't update the caret position.
  881. //
  882. VOID EditML_ChangeSelection(PED ped, HDC hdc, ICH ichNewMinSel, ICH ichNewMaxSel)
  883. {
  884. ICH temp;
  885. ICH ichOldMinSel, ichOldMaxSel;
  886. if (ichNewMinSel > ichNewMaxSel)
  887. {
  888. temp = ichNewMinSel;
  889. ichNewMinSel = ichNewMaxSel;
  890. ichNewMaxSel = temp;
  891. }
  892. ichNewMinSel = min(ichNewMinSel, ped->cch);
  893. ichNewMaxSel = min(ichNewMaxSel, ped->cch);
  894. //
  895. // Save the current selection
  896. //
  897. ichOldMinSel = ped->ichMinSel;
  898. ichOldMaxSel = ped->ichMaxSel;
  899. //
  900. // Set new selection
  901. //
  902. ped->ichMinSel = ichNewMinSel;
  903. ped->ichMaxSel = ichNewMaxSel;
  904. //
  905. // This finds the XOR of the old and new selection regions and redraws it.
  906. // There is nothing to repaint if we aren't visible or our selection
  907. // is hidden.
  908. //
  909. if (IsWindowVisible(ped->hwnd) && (ped->fFocus || ped->fNoHideSel))
  910. {
  911. SELBLOCK Blk[2];
  912. int i;
  913. if (ped->fFocus)
  914. {
  915. HideCaret(ped->hwnd);
  916. }
  917. Blk[0].StPos = ichOldMinSel;
  918. Blk[0].EndPos = ichOldMaxSel;
  919. Blk[1].StPos = ped->ichMinSel;
  920. Blk[1].EndPos = ped->ichMaxSel;
  921. if (Edit_CalcChangeSelection(ped, ichOldMinSel, ichOldMaxSel, (LPSELBLOCK)&Blk[0], (LPSELBLOCK)&Blk[1]))
  922. {
  923. //
  924. // Paint both Blk[0] and Blk[1], if they exist
  925. //
  926. for (i = 0; i < 2; i++)
  927. {
  928. if (Blk[i].StPos != 0xFFFFFFFF)
  929. EditML_DrawText(ped, hdc, Blk[i].StPos, Blk[i].EndPos, TRUE);
  930. }
  931. }
  932. //
  933. // Update caret.
  934. //
  935. EditML_SetCaretPosition(ped, hdc);
  936. if (ped->fFocus)
  937. {
  938. ShowCaret(ped->hwnd);
  939. }
  940. }
  941. }
  942. //---------------------------------------------------------------------------//
  943. //
  944. // EditML_UpdateiCaretLine AorW
  945. //
  946. // This updates the ped->iCaretLine field from the ped->ichCaret;
  947. // Also, when the caret gets to the beginning of next line, pop it up to
  948. // the end of current line when inserting text;
  949. //
  950. VOID EditML_UpdateiCaretLine(PED ped)
  951. {
  952. PSTR pText;
  953. ped->iCaretLine = EditML_IchToLine(ped, ped->ichCaret);
  954. //
  955. // If caret gets to beginning of next line, pop it up to end of current line
  956. // when inserting text.
  957. //
  958. pText = Edit_Lock(ped) +
  959. (ped->ichCaret - 1) * ped->cbChar;
  960. if (ped->iCaretLine && ped->chLines[ped->iCaretLine] == ped->ichCaret &&
  961. (!AWCOMPARECHAR(ped, pText - ped->cbChar, 0x0D) ||
  962. !AWCOMPARECHAR(ped, pText, 0x0A)))
  963. {
  964. ped->iCaretLine--;
  965. }
  966. Edit_Unlock(ped);
  967. }
  968. //---------------------------------------------------------------------------//
  969. //
  970. // EditML_InsertText AorW
  971. //
  972. // Adds up to cchInsert characters from lpText to the ped starting at
  973. // ichCaret. If the ped only allows a maximum number of characters, then we
  974. // will only add that many characters to the ped. The number of characters
  975. // actually added is return ed (could be 0). If we can't allocate the required
  976. // space, we notify the parent with EN_ERRSPACE and no characters are added.
  977. // do some stuff faster since we will be getting only one or two chars of input.
  978. //
  979. ICH EditML_InsertText(PED ped, LPSTR lpText, ICH cchInsert, BOOL fUserTyping)
  980. {
  981. HDC hdc;
  982. ICH validCch = cchInsert;
  983. ICH oldCaret = ped->ichCaret;
  984. int oldCaretLine = ped->iCaretLine;
  985. BOOL fCRLF = FALSE;
  986. LONG ll, hl;
  987. POINT xyPosInitial;
  988. POINT xyPosFinal;
  989. HWND hwndSave = ped->hwnd;
  990. UNDO undo;
  991. ICH validCchTemp;
  992. xyPosInitial.x=0;
  993. xyPosInitial.y=0;
  994. xyPosFinal.x=0;
  995. xyPosFinal.y=0;
  996. if (validCch == 0)
  997. {
  998. return 0;
  999. }
  1000. if (ped->cchTextMax <= ped->cch)
  1001. {
  1002. //
  1003. // When the max chars is reached already, notify parent
  1004. // Fix for Bug #4183 -- 02/06/91 -- SANKAR --
  1005. //
  1006. Edit_NotifyParent(ped,EN_MAXTEXT);
  1007. return 0;
  1008. }
  1009. //
  1010. // Limit the amount of text we add
  1011. //
  1012. validCch = min(validCch, ped->cchTextMax - ped->cch);
  1013. //
  1014. // Make sure we don't split a CRLF in half
  1015. //
  1016. if (validCch)
  1017. {
  1018. if (ped->fAnsi)
  1019. {
  1020. if (*(WORD UNALIGNED *)(lpText + validCch - 1) == 0x0A0D)
  1021. {
  1022. validCch--;
  1023. }
  1024. }
  1025. else
  1026. {
  1027. if (*(DWORD UNALIGNED *)(lpText + (validCch - 1) * ped->cbChar) == 0x000A000D)
  1028. {
  1029. validCch--;
  1030. }
  1031. }
  1032. }
  1033. if (!validCch)
  1034. {
  1035. //
  1036. // When the max chars is reached already, notify parent
  1037. // Fix for Bug #4183 -- 02/06/91 -- SANKAR --
  1038. //
  1039. Edit_NotifyParent(ped,EN_MAXTEXT);
  1040. return 0;
  1041. }
  1042. if (validCch == 2)
  1043. {
  1044. if (ped->fAnsi)
  1045. {
  1046. if (*(WORD UNALIGNED *)lpText == 0x0A0D)
  1047. {
  1048. fCRLF = TRUE;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. if (*(DWORD UNALIGNED *)lpText == 0x000A000D)
  1054. {
  1055. fCRLF = TRUE;
  1056. }
  1057. }
  1058. }
  1059. //
  1060. // Save current undo state always, but clear it out only if !AutoVScroll
  1061. //
  1062. Edit_SaveUndo(Pundo(ped), (PUNDO)&undo, !ped->fAutoVScroll);
  1063. hdc = Edit_GetDC(ped, FALSE);
  1064. //
  1065. // We only need the y position. Since with an LPK loaded
  1066. // calculating the x position is an intensive job, just
  1067. // call EditML_IchToYPos.
  1068. //
  1069. if (ped->cch)
  1070. {
  1071. if (ped->pLpkEditCallout)
  1072. {
  1073. xyPosInitial.y = EditML_IchToYPos(ped, ped->cch-1, FALSE);
  1074. }
  1075. else
  1076. {
  1077. EditML_IchToXYPos(ped, hdc, ped->cch - 1, FALSE, &xyPosInitial);
  1078. }
  1079. }
  1080. //
  1081. // Insert the text
  1082. //
  1083. validCchTemp = validCch; // may not be needed, but just for precautions..
  1084. if (!Edit_InsertText(ped, lpText, &validCchTemp))
  1085. {
  1086. //
  1087. // Restore previous undo buffer if it was cleared
  1088. //
  1089. if (!ped->fAutoVScroll)
  1090. {
  1091. Edit_SaveUndo((PUNDO)&undo, Pundo(ped), FALSE);
  1092. }
  1093. Edit_ReleaseDC(ped, hdc, FALSE);
  1094. Edit_NotifyParent(ped, EN_ERRSPACE);
  1095. return 0;
  1096. }
  1097. #if DBG
  1098. if (validCch != validCchTemp)
  1099. {
  1100. //
  1101. // All characters in lpText has not been inserted to ped.
  1102. // This could happen when cch is close to cchMax.
  1103. // Better revisit this after NT5 ships.
  1104. //
  1105. TraceMsg(TF_STANDARD, "Edit: EditML_InsertText: validCch is changed (%x -> %x) in Edit_InsertText.",
  1106. validCch, validCchTemp);
  1107. }
  1108. #endif
  1109. //
  1110. // Note that ped->ichCaret is updated by Edit_InsertText
  1111. //
  1112. EditML_BuildchLines(ped, (ICH)oldCaretLine, (int)validCch, fCRLF?(BOOL)FALSE:fUserTyping, &ll, &hl);
  1113. if (ped->cch)
  1114. {
  1115. //
  1116. // We only need the y position. Since with an LPK loaded
  1117. // calculating the x position is an intensive job, just
  1118. // call EditML_IchToYPos.
  1119. if (ped->pLpkEditCallout)
  1120. {
  1121. xyPosFinal.y = EditML_IchToYPos(ped, ped->cch-1, FALSE);
  1122. }
  1123. else
  1124. {
  1125. EditML_IchToXYPos(ped, hdc, ped->cch - 1, FALSE,&xyPosFinal);
  1126. }
  1127. }
  1128. if (xyPosFinal.y < xyPosInitial.y && ((ICH)ped->ichScreenStart) + ped->ichLinesOnScreen >= ped->cLines - 1)
  1129. {
  1130. RECT rc;
  1131. CopyRect((LPRECT)&rc, (LPRECT)&ped->rcFmt);
  1132. rc.top = xyPosFinal.y + ped->lineHeight;
  1133. if (ped->pLpkEditCallout)
  1134. {
  1135. int xFarOffset = ped->xOffset + ped->rcFmt.right - ped->rcFmt.left;
  1136. //
  1137. // Include left or right margins in display unless clipped
  1138. // by horizontal scrolling.
  1139. //
  1140. if (ped->wLeftMargin)
  1141. {
  1142. if (!(ped->format == ES_LEFT // Only ES_LEFT (Nearside alignment) can get clipped
  1143. && ( (!ped->fRtoLReading && ped->xOffset > 0) // LTR and first char not fully in view
  1144. || ( ped->fRtoLReading && xFarOffset < ped->maxPixelWidth)))) //RTL and last char not fully in view
  1145. {
  1146. rc.left -= ped->wLeftMargin;
  1147. }
  1148. }
  1149. //
  1150. // Process right margin
  1151. //
  1152. if (ped->wRightMargin)
  1153. {
  1154. if (!(ped->format == ES_LEFT // Only ES_LEFT (Nearside alignment) can get clipped
  1155. && (( ped->fRtoLReading && ped->xOffset > 0) // RTL and first char not fully in view
  1156. || (!ped->fRtoLReading && xFarOffset < ped->maxPixelWidth)))) // LTR and last char not fully in view
  1157. {
  1158. rc.right += ped->wRightMargin;
  1159. }
  1160. }
  1161. }
  1162. InvalidateRect(ped->hwnd, (LPRECT)&rc, TRUE);
  1163. }
  1164. if (!ped->fAutoVScroll)
  1165. {
  1166. if (ped->ichLinesOnScreen < ped->cLines)
  1167. {
  1168. EditML_Undo(ped);
  1169. Edit_EmptyUndo(Pundo(ped));
  1170. Edit_SaveUndo(&undo, Pundo(ped), FALSE);
  1171. MessageBeep(0);
  1172. Edit_ReleaseDC(ped, hdc, FALSE);
  1173. //
  1174. // When the max lines is reached already, notify parent
  1175. // Fix for Bug #7586 -- 10/14/91 -- SANKAR --
  1176. //
  1177. Edit_NotifyParent(ped,EN_MAXTEXT);
  1178. return 0;
  1179. }
  1180. else
  1181. {
  1182. Edit_EmptyUndo(&undo);
  1183. }
  1184. }
  1185. if (fUserTyping && ped->fWrap)
  1186. {
  1187. //
  1188. // To avoid oldCaret points intermediate of DBCS character,
  1189. // adjust oldCaret position if necessary.
  1190. //
  1191. // !!!CR If EditML_BuildchLines() returns reasonable value ( and I think
  1192. // it does), we don't probably need this. Check this out later.
  1193. //
  1194. if (ped->fDBCS && ped->fAnsi)
  1195. {
  1196. oldCaret = Edit_AdjustIch(ped,
  1197. Edit_Lock(ped),
  1198. min((ICH)LOWORD(ll),oldCaret));
  1199. }
  1200. else
  1201. {
  1202. oldCaret = min((ICH)LOWORD(ll), oldCaret);
  1203. }
  1204. }
  1205. //
  1206. // Update ped->iCaretLine properly.
  1207. //
  1208. EditML_UpdateiCaretLine(ped);
  1209. Edit_NotifyParent(ped, EN_UPDATE);
  1210. //
  1211. // Make sure window still exists.
  1212. //
  1213. if (!IsWindow(hwndSave))
  1214. {
  1215. return 0;
  1216. }
  1217. if (IsWindowVisible(ped->hwnd))
  1218. {
  1219. //
  1220. // If the current font has negative A widths, we may have to start
  1221. // drawing a few characters before the oldCaret position.
  1222. //
  1223. if (ped->wMaxNegAcharPos)
  1224. {
  1225. int iLine = EditML_IchToLine(ped, oldCaret);
  1226. oldCaret = max( ((int)(oldCaret - ped->wMaxNegAcharPos)),
  1227. ((int)(ped->chLines[iLine])));
  1228. }
  1229. //
  1230. // Redraw to end of screen/text if CRLF or large insert
  1231. //
  1232. if (fCRLF || !fUserTyping)
  1233. {
  1234. //
  1235. // Redraw to end of screen/text if crlf or large insert.
  1236. //
  1237. EditML_DrawText(ped, hdc, (fUserTyping ? oldCaret : 0), ped->cch, FALSE);
  1238. }
  1239. else
  1240. {
  1241. EditML_DrawText(ped, hdc, oldCaret, max(ped->ichCaret, (ICH)hl), FALSE);
  1242. }
  1243. }
  1244. Edit_ReleaseDC(ped, hdc, FALSE);
  1245. //
  1246. // Make sure we can see the cursor
  1247. //
  1248. EditML_EnsureCaretVisible(ped);
  1249. ped->fDirty = TRUE;
  1250. Edit_NotifyParent(ped, EN_CHANGE);
  1251. if (validCch < cchInsert)
  1252. {
  1253. Edit_NotifyParent(ped, EN_MAXTEXT);
  1254. }
  1255. if (validCch)
  1256. {
  1257. NotifyWinEvent(EVENT_OBJECT_VALUECHANGE, ped->hwnd, OBJID_CLIENT, INDEXID_CONTAINER);
  1258. }
  1259. //
  1260. // Make sure the window still exists.
  1261. //
  1262. return IsWindow(hwndSave) ? validCch : 0;
  1263. }
  1264. //---------------------------------------------------------------------------//
  1265. //
  1266. // EditML_ReplaceSel
  1267. //
  1268. // Replaces currently selected text with the passed in text, WITH UNDO
  1269. // CAPABILITIES.
  1270. //
  1271. VOID EditML_ReplaceSel(PED ped, LPSTR lpText)
  1272. {
  1273. ICH cchText;
  1274. //
  1275. // Delete text, which will put it into the clean undo buffer.
  1276. //
  1277. Edit_EmptyUndo(Pundo(ped));
  1278. EditML_DeleteText(ped);
  1279. //
  1280. // B#3356
  1281. // Some apps do "clear" by selecting all of the text, then replacing
  1282. // it with "", in which case EditML_InsertText() will return 0. But that
  1283. // doesn't mean failure...
  1284. //
  1285. if ( ped->fAnsi )
  1286. {
  1287. cchText = strlen(lpText);
  1288. }
  1289. else
  1290. {
  1291. cchText = wcslen((LPWSTR)lpText);
  1292. }
  1293. if (cchText)
  1294. {
  1295. BOOL fFailed;
  1296. UNDO undo;
  1297. HWND hwndSave;
  1298. //
  1299. // B#1385,1427
  1300. // Save undo buffer, but DO NOT CLEAR IT. We want to restore it
  1301. // if insertion fails due to OOM.
  1302. //
  1303. Edit_SaveUndo(Pundo(ped), (PUNDO)&undo, FALSE);
  1304. hwndSave = ped->hwnd;
  1305. fFailed = (BOOL) !EditML_InsertText(ped, lpText, cchText, FALSE);
  1306. if (!IsWindow(hwndSave))
  1307. {
  1308. return;
  1309. }
  1310. if (fFailed)
  1311. {
  1312. //
  1313. // UNDO the previous edit
  1314. //
  1315. Edit_SaveUndo((PUNDO)&undo, Pundo(ped), FALSE);
  1316. EditML_Undo(ped);
  1317. }
  1318. }
  1319. }
  1320. //---------------------------------------------------------------------------//
  1321. //
  1322. // EditML_DeleteText AorW
  1323. //
  1324. // Deletes the characters between ichMin and ichMax. Returns the
  1325. // number of characters we deleted.
  1326. //
  1327. ICH EditML_DeleteText(PED ped)
  1328. {
  1329. ICH minSel = ped->ichMinSel;
  1330. ICH maxSel = ped->ichMaxSel;
  1331. ICH cchDelete;
  1332. HDC hdc;
  1333. int minSelLine;
  1334. int maxSelLine;
  1335. POINT xyPos;
  1336. RECT rc;
  1337. BOOL fFastDelete = FALSE;
  1338. LONG hl;
  1339. INT cchcount = 0;
  1340. //
  1341. // Get what line the min selection is on so that we can start rebuilding the
  1342. // text from there if we delete anything.
  1343. //
  1344. minSelLine = EditML_IchToLine(ped, minSel);
  1345. maxSelLine = EditML_IchToLine(ped, maxSel);
  1346. //
  1347. // Calculate fFastDelete and cchcount
  1348. //
  1349. if (ped->fAnsi && ped->fDBCS)
  1350. {
  1351. if ((ped->fAutoVScroll) &&
  1352. (minSelLine == maxSelLine) &&
  1353. (ped->chLines[minSelLine] != minSel) &&
  1354. (Edit_NextIch(ped,NULL,minSel) == maxSel))
  1355. {
  1356. fFastDelete = TRUE;
  1357. cchcount = ((maxSel - minSel) == 1) ? 0 : -1;
  1358. }
  1359. }
  1360. else if (((maxSel - minSel) == 1) && (minSelLine == maxSelLine) && (ped->chLines[minSelLine] != minSel))
  1361. {
  1362. fFastDelete = ped->fAutoVScroll ? TRUE : FALSE;
  1363. }
  1364. cchDelete = Edit_DeleteText(ped);
  1365. if (!cchDelete)
  1366. {
  1367. return 0;
  1368. }
  1369. //
  1370. // Start building lines at minsel line since caretline may be at the max sel
  1371. // point.
  1372. //
  1373. if (fFastDelete)
  1374. {
  1375. //
  1376. // cchcount is (-1) if it's a double byte character
  1377. //
  1378. EditML_ShiftchLines(ped, minSelLine + 1, -2 + cchcount);
  1379. EditML_BuildchLines(ped, minSelLine, 1, TRUE, NULL, &hl);
  1380. }
  1381. else
  1382. {
  1383. EditML_BuildchLines(ped, max(minSelLine-1,0), -(int)cchDelete, FALSE, NULL, NULL);
  1384. }
  1385. EditML_UpdateiCaretLine(ped);
  1386. Edit_NotifyParent(ped, EN_UPDATE);
  1387. if (IsWindowVisible(ped->hwnd))
  1388. {
  1389. //
  1390. // Now update the screen to reflect the deletion
  1391. //
  1392. hdc = Edit_GetDC(ped, FALSE);
  1393. //
  1394. // Otherwise just redraw starting at the line we just entered
  1395. //
  1396. minSelLine = max(minSelLine-1,0);
  1397. EditML_DrawText(ped, hdc, ped->chLines[minSelLine], fFastDelete ? hl : ped->cch, FALSE);
  1398. CopyRect(&rc, &ped->rcFmt);
  1399. rc.left -= ped->wLeftMargin;
  1400. rc.right += ped->wRightMargin;
  1401. if (ped->cch)
  1402. {
  1403. //
  1404. // Clear from end of text to end of window.
  1405. //
  1406. // We only need the y position. Since with an LPK loaded
  1407. // calculating the x position is an intensive job, just
  1408. // call EditML_IchToYPos.
  1409. //
  1410. if (ped->pLpkEditCallout)
  1411. {
  1412. xyPos.y = EditML_IchToYPos(ped, ped->cch, FALSE);
  1413. }
  1414. else
  1415. {
  1416. EditML_IchToXYPos(ped, hdc, ped->cch, FALSE, &xyPos);
  1417. }
  1418. rc.top = xyPos.y + ped->lineHeight;
  1419. }
  1420. InvalidateRect(ped->hwnd, &rc, TRUE);
  1421. Edit_ReleaseDC(ped, hdc, FALSE);
  1422. EditML_EnsureCaretVisible(ped);
  1423. }
  1424. ped->fDirty = TRUE;
  1425. Edit_NotifyParent(ped, EN_CHANGE);
  1426. if (cchDelete)
  1427. {
  1428. NotifyWinEvent(EVENT_OBJECT_VALUECHANGE, ped->hwnd, OBJID_CLIENT, INDEXID_CONTAINER);
  1429. }
  1430. return cchDelete;
  1431. }
  1432. //---------------------------------------------------------------------------//
  1433. //
  1434. // EditML_InsertchLine AorW
  1435. //
  1436. // Inserts the line iline and sets its starting character index to be
  1437. // ich. All the other line indices are moved up. Returns TRUE if successful
  1438. // else FALSE and notifies the parent that there was no memory.
  1439. //
  1440. BOOL EditML_InsertchLine(PED ped, ICH iLine, ICH ich, BOOL fUserTyping)
  1441. {
  1442. DWORD dwSize;
  1443. if (fUserTyping && iLine < ped->cLines)
  1444. {
  1445. ped->chLines[iLine] = ich;
  1446. return TRUE;
  1447. }
  1448. dwSize = (ped->cLines + 2) * sizeof(int);
  1449. if (dwSize > UserLocalSize(ped->chLines))
  1450. {
  1451. LPICH hResult;
  1452. //
  1453. // Grow the line index buffer
  1454. //
  1455. dwSize += LINEBUMP * sizeof(int);
  1456. hResult = (LPICH)UserLocalReAlloc(ped->chLines, dwSize, 0);
  1457. if (!hResult)
  1458. {
  1459. Edit_NotifyParent(ped, EN_ERRSPACE);
  1460. return FALSE;
  1461. }
  1462. ped->chLines = hResult;
  1463. }
  1464. //
  1465. // Move indices starting at iLine up
  1466. //
  1467. if (ped->cLines != iLine)
  1468. {
  1469. RtlMoveMemory(&ped->chLines[iLine + 1], &ped->chLines[iLine],
  1470. (ped->cLines - iLine)*SIZEOF(INT));
  1471. }
  1472. ped->cLines++;
  1473. ped->chLines[iLine] = ich;
  1474. return TRUE;
  1475. }
  1476. //---------------------------------------------------------------------------//
  1477. //
  1478. // EditML_ShiftchLines AorW
  1479. //
  1480. // Move the starting index of all lines iLine or greater by delta
  1481. // bytes.
  1482. //
  1483. void EditML_ShiftchLines(PED ped, ICH iLine, int delta)
  1484. {
  1485. if (iLine < ped->cLines)
  1486. {
  1487. //
  1488. // Just add delta to the starting point of each line after iLine
  1489. //
  1490. for (; iLine < ped->cLines; iLine++)
  1491. {
  1492. ped->chLines[iLine] += delta;
  1493. }
  1494. }
  1495. }
  1496. //---------------------------------------------------------------------------//
  1497. //
  1498. // EditML_BuildchLines AorW
  1499. //
  1500. // Rebuilds the start of line array (ped->chLines) starting at line
  1501. // number ichLine.
  1502. //
  1503. void EditML_BuildchLines( PED ped, ICH iLine, int cchDelta, BOOL fUserTyping, PLONG pll, PLONG phl)
  1504. {
  1505. PSTR ptext; // Starting address of the text
  1506. //
  1507. // We keep these ICH's so that we can Unlock ped->hText when we have to grow
  1508. // the chlines array. With large text handles, it becomes a problem if we
  1509. // have a locked block in the way.
  1510. //
  1511. ICH ichLineStart;
  1512. ICH ichLineEnd;
  1513. ICH ichLineEndBeforeCRLF;
  1514. ICH ichCRLF;
  1515. ICH cch;
  1516. HDC hdc;
  1517. BOOL fLineBroken = FALSE; // Initially, no new line breaks are made
  1518. ICH minCchBreak;
  1519. ICH maxCchBreak;
  1520. BOOL fOnDelimiter;
  1521. if (!ped->cch)
  1522. {
  1523. ped->maxPixelWidth = 0;
  1524. ped->xOffset = 0;
  1525. ped->ichScreenStart = 0;
  1526. ped->cLines = 1;
  1527. if (pll)
  1528. {
  1529. *pll = 0;
  1530. }
  1531. if (phl)
  1532. {
  1533. *phl = 0;
  1534. }
  1535. goto UpdateScroll;
  1536. }
  1537. if (fUserTyping && cchDelta)
  1538. {
  1539. EditML_ShiftchLines(ped, iLine + 1, cchDelta);
  1540. }
  1541. hdc = Edit_GetDC(ped, TRUE);
  1542. if (!iLine && !cchDelta && !fUserTyping)
  1543. {
  1544. //
  1545. // Reset maxpixelwidth only if we will be running through the whole
  1546. // text. Better too long than too short.
  1547. //
  1548. ped->maxPixelWidth = 0;
  1549. //
  1550. // Reset number of lines in text since we will be running through all
  1551. // the text anyway...
  1552. //
  1553. ped->cLines = 1;
  1554. }
  1555. //
  1556. // Set min and max line built to be the starting line
  1557. //
  1558. minCchBreak = maxCchBreak = (cchDelta ? ped->chLines[iLine] : 0);
  1559. ptext = Edit_Lock(ped);
  1560. ichCRLF = ichLineStart = ped->chLines[iLine];
  1561. while (ichLineStart < ped->cch)
  1562. {
  1563. if (ichLineStart >= ichCRLF)
  1564. {
  1565. ichCRLF = ichLineStart;
  1566. //
  1567. // Move ichCRLF ahead to either the first CR or to the end of text.
  1568. //
  1569. if (ped->fAnsi)
  1570. {
  1571. while (ichCRLF < ped->cch)
  1572. {
  1573. if (*(ptext + ichCRLF) == 0x0D)
  1574. {
  1575. if (*(ptext + ichCRLF + 1) == 0x0A ||
  1576. *(WORD UNALIGNED *)(ptext + ichCRLF + 1) == 0x0A0D)
  1577. {
  1578. break;
  1579. }
  1580. }
  1581. ichCRLF++;
  1582. }
  1583. }
  1584. else
  1585. {
  1586. LPWSTR pwtext = (LPWSTR)ptext;
  1587. while (ichCRLF < ped->cch)
  1588. {
  1589. if (*(pwtext + ichCRLF) == 0x0D)
  1590. {
  1591. if (*(pwtext + ichCRLF + 1) == 0x0A ||
  1592. *(DWORD UNALIGNED *)(pwtext + ichCRLF + 1) == 0x000A000D)
  1593. {
  1594. break;
  1595. }
  1596. }
  1597. ichCRLF++;
  1598. }
  1599. }
  1600. }
  1601. if (!ped->fWrap)
  1602. {
  1603. UINT LineWidth;
  1604. //
  1605. // If we are not word wrapping, line breaks are signified by CRLF.
  1606. //
  1607. //
  1608. // If we cut off the line at MAXLINELENGTH, we should
  1609. // adjust ichLineEnd.
  1610. //
  1611. if ((ichCRLF - ichLineStart) <= MAXLINELENGTH)
  1612. {
  1613. ichLineEnd = ichCRLF;
  1614. }
  1615. else
  1616. {
  1617. ichLineEnd = ichLineStart + MAXLINELENGTH;
  1618. if (ped->fAnsi && ped->fDBCS)
  1619. {
  1620. ichLineEnd = Edit_AdjustIch( ped, (PSTR)ptext, ichLineEnd);
  1621. }
  1622. }
  1623. //
  1624. // We will keep track of what the longest line is for the horizontal
  1625. // scroll bar thumb positioning.
  1626. //
  1627. if (ped->pLpkEditCallout)
  1628. {
  1629. LineWidth = ped->pLpkEditCallout->EditGetLineWidth(
  1630. (PED0)ped, hdc, ptext + ichLineStart*ped->cbChar,
  1631. ichLineEnd - ichLineStart);
  1632. }
  1633. else
  1634. {
  1635. LineWidth = EditML_GetLineWidth(hdc, ptext + ichLineStart * ped->cbChar,
  1636. ichLineEnd - ichLineStart,
  1637. ped);
  1638. }
  1639. ped->maxPixelWidth = max(ped->maxPixelWidth,(int)LineWidth);
  1640. }
  1641. else
  1642. {
  1643. //
  1644. // Check if the width of the edit control is non-zero;
  1645. // a part of the fix for Bug #7402 -- SANKAR -- 01/21/91 --
  1646. //
  1647. if(ped->rcFmt.right > ped->rcFmt.left)
  1648. {
  1649. //
  1650. // Find the end of the line based solely on text extents
  1651. //
  1652. if (ped->pLpkEditCallout)
  1653. {
  1654. ichLineEnd = ichLineStart +
  1655. ped->pLpkEditCallout->EditCchInWidth(
  1656. (PED0)ped, hdc, ptext + ped->cbChar*ichLineStart,
  1657. ichCRLF - ichLineStart,
  1658. ped->rcFmt.right - ped->rcFmt.left);
  1659. }
  1660. else
  1661. {
  1662. if (ped->fAnsi)
  1663. {
  1664. ichLineEnd = ichLineStart +
  1665. Edit_CchInWidth(ped, hdc,
  1666. ptext + ichLineStart,
  1667. ichCRLF - ichLineStart,
  1668. ped->rcFmt.right - ped->rcFmt.left,
  1669. TRUE);
  1670. }
  1671. else
  1672. {
  1673. ichLineEnd = ichLineStart +
  1674. Edit_CchInWidth(ped, hdc,
  1675. (LPSTR)((LPWSTR)ptext + ichLineStart),
  1676. ichCRLF - ichLineStart,
  1677. ped->rcFmt.right - ped->rcFmt.left,
  1678. TRUE);
  1679. }
  1680. }
  1681. }
  1682. else
  1683. {
  1684. ichLineEnd = ichLineStart;
  1685. }
  1686. if (ichLineEnd == ichLineStart && ichCRLF - ichLineStart)
  1687. {
  1688. //
  1689. // Maintain a minimum of one char per line
  1690. // Since it might be a double byte char, so calling Edit_NextIch.
  1691. //
  1692. ichLineEnd = Edit_NextIch(ped, NULL, ichLineEnd);
  1693. }
  1694. //
  1695. // Now starting from ichLineEnd, if we are not at a hard line break,
  1696. // then if we are not at a space AND the char before us is
  1697. // not a space,(OR if we are at a CR) we will look word left for the
  1698. // start of the word to break at.
  1699. // This change was done for TWO reasons:
  1700. // 1. If we are on a delimiter, no need to look word left to break at.
  1701. // 2. If the previous char is a delimter, we can break at current char.
  1702. // Change done by -- SANKAR --01/31/91--
  1703. //
  1704. if (ichLineEnd != ichCRLF)
  1705. {
  1706. if(ped->lpfnNextWord)
  1707. {
  1708. fOnDelimiter = (CALLWORDBREAKPROC(*ped->lpfnNextWord, ptext,
  1709. ichLineEnd, ped->cch, WB_ISDELIMITER) ||
  1710. CALLWORDBREAKPROC(*ped->lpfnNextWord, ptext, ichLineEnd - 1,
  1711. ped->cch, WB_ISDELIMITER));
  1712. //
  1713. // This change was done for FOUR reasons:
  1714. //
  1715. // 1. If we are on a delimiter, no need to look word left to break at.
  1716. // 2. If we are on a double byte character, we can break at current char.
  1717. // 3. If the previous char is a delimter, we can break at current char.
  1718. // 4. If the previous char is a double byte character, we can break at current char.
  1719. //
  1720. }
  1721. else if (ped->fAnsi)
  1722. {
  1723. fOnDelimiter = (ISDELIMETERA(*(ptext + ichLineEnd)) ||
  1724. Edit_IsDBCSLeadByte(ped, *(ptext + ichLineEnd)));
  1725. if (!fOnDelimiter)
  1726. {
  1727. PSTR pPrev = Edit_AnsiPrev(ped,ptext,ptext+ichLineEnd);
  1728. fOnDelimiter = ISDELIMETERA(*pPrev) ||
  1729. Edit_IsDBCSLeadByte(ped,*pPrev);
  1730. }
  1731. }
  1732. else
  1733. {
  1734. fOnDelimiter = (ISDELIMETERW(*((LPWSTR)ptext + ichLineEnd)) ||
  1735. Edit_IsFullWidth(CP_ACP,*((LPWSTR)ptext + ichLineEnd)) ||
  1736. ISDELIMETERW(*((LPWSTR)ptext + ichLineEnd - 1)) ||
  1737. Edit_IsFullWidth(CP_ACP,*((LPWSTR)ptext + ichLineEnd - 1)));
  1738. }
  1739. if (!fOnDelimiter ||
  1740. (ped->fAnsi && *(ptext + ichLineEnd) == 0x0D) ||
  1741. (!ped->fAnsi && *((LPWSTR)ptext + ichLineEnd) == 0x0D))
  1742. {
  1743. if (ped->lpfnNextWord != NULL)
  1744. {
  1745. cch = CALLWORDBREAKPROC(*ped->lpfnNextWord, (LPSTR)ptext, ichLineEnd,
  1746. ped->cch, WB_LEFT);
  1747. }
  1748. else
  1749. {
  1750. ped->fCalcLines = TRUE;
  1751. Edit_Word(ped, ichLineEnd, TRUE, &cch, NULL);
  1752. ped->fCalcLines = FALSE;
  1753. }
  1754. if (cch > ichLineStart)
  1755. {
  1756. ichLineEnd = cch;
  1757. }
  1758. //
  1759. // Now, if the above test fails, it means the word left goes
  1760. // back before the start of the line ie. a word is longer
  1761. // than a line on the screen. So, we just fit as much of
  1762. // the word on the line as possible. Thus, we use the
  1763. // pLineEnd we calculated solely on width at the beginning
  1764. // of this else block...
  1765. //
  1766. }
  1767. }
  1768. }
  1769. if (AWCOMPARECHAR(ped,ptext + ichLineEnd * ped->cbChar, ' ') ||
  1770. AWCOMPARECHAR(ped,ptext + ichLineEnd * ped->cbChar, VK_TAB))
  1771. {
  1772. //
  1773. // Swallow the space at the end of a line.
  1774. //
  1775. if (ichLineEnd < ped->cch)
  1776. {
  1777. ichLineEnd++;
  1778. }
  1779. }
  1780. //
  1781. // Skip over crlf or crcrlf if it exists. Thus, ichLineEnd is the first
  1782. // character in the next line.
  1783. //
  1784. ichLineEndBeforeCRLF = ichLineEnd;
  1785. if (ped->fAnsi)
  1786. {
  1787. if (ichLineEnd < ped->cch && *(ptext + ichLineEnd) == 0x0D)
  1788. {
  1789. ichLineEnd += 2;
  1790. }
  1791. //
  1792. // Skip over CRCRLF
  1793. //
  1794. if (ichLineEnd < ped->cch && *(ptext + ichLineEnd) == 0x0A)
  1795. {
  1796. ichLineEnd++;
  1797. }
  1798. }
  1799. else
  1800. {
  1801. if (ichLineEnd < ped->cch && *(((LPWSTR)ptext) + ichLineEnd) == 0x0D)
  1802. {
  1803. ichLineEnd += 2;
  1804. }
  1805. //
  1806. // Skip over CRCRLF
  1807. //
  1808. if (ichLineEnd < ped->cch && *(((LPWSTR)ptext) + ichLineEnd) == 0x0A)
  1809. {
  1810. ichLineEnd++;
  1811. TraceMsg(TF_STANDARD, "Edit: Skip over CRCRLF");
  1812. }
  1813. }
  1814. #if DBG
  1815. if (ichLineEnd > ped->cch)
  1816. {
  1817. TraceMsg(TF_STANDARD, "Edit: ichLineEnd (%d)> ped->cch (%d)", ichLineEnd, ped->cch);
  1818. }
  1819. #endif
  1820. //
  1821. // Now, increment iLine, allocate space for the next line, and set its
  1822. // starting point
  1823. //
  1824. iLine++;
  1825. if (!fUserTyping || (iLine > ped->cLines - 1) || (ped->chLines[iLine] != ichLineEnd))
  1826. {
  1827. //
  1828. // The line break occured in a different place than before.
  1829. //
  1830. if (!fLineBroken)
  1831. {
  1832. //
  1833. // Since we haven't broken a line before, just set the min
  1834. // break line.
  1835. //
  1836. fLineBroken = TRUE;
  1837. if (ichLineEndBeforeCRLF == ichLineEnd)
  1838. {
  1839. minCchBreak = maxCchBreak = (ichLineEnd ? ichLineEnd - 1 : 0);
  1840. }
  1841. else
  1842. {
  1843. minCchBreak = maxCchBreak = ichLineEndBeforeCRLF;
  1844. }
  1845. }
  1846. maxCchBreak = max(maxCchBreak, ichLineEnd);
  1847. Edit_Unlock(ped);
  1848. //
  1849. // Now insert the new line into the array
  1850. //
  1851. if (!EditML_InsertchLine(ped, iLine, ichLineEnd, (BOOL)(cchDelta != 0)))
  1852. {
  1853. goto EndUp;
  1854. }
  1855. ptext = Edit_Lock(ped);
  1856. }
  1857. else
  1858. {
  1859. maxCchBreak = ped->chLines[iLine];
  1860. //
  1861. // Quick escape
  1862. //
  1863. goto UnlockAndEndUp;
  1864. }
  1865. ichLineStart = ichLineEnd;
  1866. }
  1867. if (iLine != ped->cLines)
  1868. {
  1869. TraceMsg(TF_STANDARD, "Edit: chLines[%d] is being cleared.", iLine);
  1870. ped->cLines = iLine;
  1871. ped->chLines[ped->cLines] = 0;
  1872. }
  1873. //
  1874. // Note that we incremented iLine towards the end of the while loop so, the
  1875. // index, iLine, is actually equal to the line count
  1876. //
  1877. if (ped->cch && AWCOMPARECHAR(ped, ptext + (ped->cch - 1)*ped->cbChar, 0x0A) &&
  1878. ped->chLines[ped->cLines - 1] < ped->cch)
  1879. {
  1880. //
  1881. // Make sure last line has no crlf in it
  1882. //
  1883. if (!fLineBroken)
  1884. {
  1885. //
  1886. // Since we haven't broken a line before, just set the min break
  1887. // line.
  1888. //
  1889. fLineBroken = TRUE;
  1890. minCchBreak = ped->cch - 1;
  1891. }
  1892. maxCchBreak = max(maxCchBreak, ichLineEnd);
  1893. Edit_Unlock(ped);
  1894. EditML_InsertchLine(ped, iLine, ped->cch, FALSE);
  1895. EditML_SanityCheck(ped);
  1896. }
  1897. else
  1898. {
  1899. UnlockAndEndUp:
  1900. Edit_Unlock(ped);
  1901. }
  1902. EndUp:
  1903. Edit_ReleaseDC(ped, hdc, TRUE);
  1904. if (pll)
  1905. {
  1906. *pll = minCchBreak;
  1907. }
  1908. if (phl)
  1909. {
  1910. *phl = maxCchBreak;
  1911. }
  1912. UpdateScroll:
  1913. EditML_Scroll(ped, FALSE, ML_REFRESH, 0, TRUE);
  1914. EditML_Scroll(ped, TRUE, ML_REFRESH, 0, TRUE);
  1915. EditML_SanityCheck(ped);
  1916. return;
  1917. }
  1918. //---------------------------------------------------------------------------//
  1919. //
  1920. // EditML_Paint()
  1921. //
  1922. // Response to WM_PAINT message.
  1923. //
  1924. VOID EditML_Paint(PED ped, HDC hdc, LPRECT lprc)
  1925. {
  1926. HFONT hOldFont;
  1927. ICH imin;
  1928. ICH imax;
  1929. HBRUSH hbr;
  1930. BOOL fNeedDelete = FALSE;
  1931. //
  1932. // Do we need to draw the border ourself for old apps?
  1933. //
  1934. if (ped->fFlatBorder)
  1935. {
  1936. RECT rcT;
  1937. ULONG ulStyle;
  1938. INT cxBorder;
  1939. INT cyBorder;
  1940. INT cxFrame;
  1941. INT cyFrame;
  1942. ulStyle = GET_STYLE(ped);
  1943. cxBorder = GetSystemMetrics(SM_CXBORDER);
  1944. cyBorder = GetSystemMetrics(SM_CYBORDER);
  1945. cxFrame = GetSystemMetrics(SM_CXFRAME);
  1946. cyFrame = GetSystemMetrics(SM_CYFRAME);
  1947. GetClientRect(ped->hwnd, &rcT);
  1948. if (ulStyle & WS_SIZEBOX)
  1949. {
  1950. InflateRect(&rcT, cxBorder - cxFrame, cyBorder - cyFrame);
  1951. }
  1952. DrawFrame(hdc, &rcT, 1, DF_WINDOWFRAME);
  1953. }
  1954. Edit_SetClip(ped, hdc, (BOOL) (ped->xOffset == 0));
  1955. if (ped->hFont)
  1956. {
  1957. hOldFont = SelectObject(hdc, ped->hFont);
  1958. }
  1959. if (!lprc)
  1960. {
  1961. //
  1962. // no partial rect given -- draw all text
  1963. //
  1964. imin = 0;
  1965. imax = ped->cch;
  1966. }
  1967. else
  1968. {
  1969. //
  1970. // only draw pertinent text
  1971. //
  1972. imin = (ICH) EditML_MouseToIch(ped, hdc, ((LPPOINT) &lprc->left), NULL) - 1;
  1973. if (imin == -1)
  1974. {
  1975. imin = 0;
  1976. }
  1977. //
  1978. // HACK_ALERT:
  1979. // The 3 is required here because, EditML_MouseToIch() returns decremented
  1980. // value; We must fix EditML_MouseToIch.
  1981. //
  1982. imax = (ICH) EditML_MouseToIch(ped, hdc, ((LPPOINT) &lprc->right), NULL) + 3;
  1983. if (imax > ped->cch)
  1984. {
  1985. imax = ped->cch;
  1986. }
  1987. }
  1988. hbr = Edit_GetBrush(ped, hdc, &fNeedDelete);
  1989. if (hbr)
  1990. {
  1991. RECT rc;
  1992. GetClientRect(ped->hwnd, &rc);
  1993. FillRect(hdc, &rc, hbr);
  1994. if (fNeedDelete)
  1995. {
  1996. DeleteObject(hbr);
  1997. }
  1998. }
  1999. EditML_DrawText(ped, hdc, imin, imax, FALSE);
  2000. if (ped->hFont)
  2001. {
  2002. SelectObject(hdc, hOldFont);
  2003. }
  2004. }
  2005. //---------------------------------------------------------------------------//
  2006. //
  2007. // EditML_KeyDown AorW
  2008. //
  2009. // Handles cursor movement and other VIRT KEY stuff. keyMods allows
  2010. // us to make EditML_KeyDownHandler calls and specify if the modifier keys (shift
  2011. // and control) are up or down. If keyMods == 0, we get the keyboard state
  2012. // using GetKeyState(VK_SHIFT) etc. Otherwise, the bits in keyMods define the
  2013. // state of the shift and control keys.
  2014. //
  2015. VOID EditML_KeyDown(PED ped, UINT virtKeyCode, int keyMods)
  2016. {
  2017. HDC hdc;
  2018. BOOL prevLine;
  2019. POINT mousePt;
  2020. int defaultDlgId;
  2021. int iScrollAmt;
  2022. //
  2023. // Variables we will use for redrawing the updated text
  2024. //
  2025. //
  2026. // new selection is specified by newMinSel, newMaxSel
  2027. //
  2028. ICH newMaxSel = ped->ichMaxSel;
  2029. ICH newMinSel = ped->ichMinSel;
  2030. //
  2031. // Flags for drawing the updated text
  2032. //
  2033. BOOL changeSelection = FALSE;
  2034. //
  2035. // Comparisons we do often
  2036. //
  2037. BOOL MinEqMax = (newMaxSel == newMinSel);
  2038. BOOL MinEqCar = (ped->ichCaret == newMinSel);
  2039. BOOL MaxEqCar = (ped->ichCaret == newMaxSel);
  2040. //
  2041. // State of shift and control keys.
  2042. //
  2043. int scState;
  2044. if (ped->fMouseDown)
  2045. {
  2046. //
  2047. // If we are in the middle of a mousedown command, don't do anything.
  2048. //
  2049. return;
  2050. }
  2051. if (ped->hwndBalloon)
  2052. {
  2053. Edit_HideBalloonTip(ped->hwnd);
  2054. }
  2055. scState = Edit_GetModKeys(keyMods);
  2056. switch (virtKeyCode)
  2057. {
  2058. case VK_ESCAPE:
  2059. if (ped->fInDialogBox)
  2060. {
  2061. //
  2062. // This condition is removed because, if the dialogbox does not
  2063. // have a CANCEL button and if ESC is hit when focus is on a
  2064. // ML edit control the dialogbox must close whether it has cancel
  2065. // button or not to be consistent with SL edit control;
  2066. // DefDlgProc takes care of the disabled CANCEL button case.
  2067. // Fix for Bug #4123 -- 02/07/91 -- SANKAR --
  2068. //
  2069. // User hit ESC...Send a close message (which in turn sends a
  2070. // cancelID to the app in DefDialogProc...
  2071. //
  2072. PostMessage(ped->hwndParent, WM_CLOSE, 0, 0L);
  2073. }
  2074. return;
  2075. case VK_RETURN:
  2076. if (ped->fInDialogBox)
  2077. {
  2078. //
  2079. // If this multiline edit control is in a dialog box, then we want
  2080. // the RETURN key to be sent to the default dialog button (if there
  2081. // is one). CTRL-RETURN will insert a RETURN into the text. Note
  2082. // that CTRL-RETURN automatically translates into a linefeed (0x0A)
  2083. // and in the EditML_CharHandler, we handle this as if a return was
  2084. // entered.
  2085. //
  2086. if (scState != CTRLDOWN)
  2087. {
  2088. if (GET_STYLE(ped) & ES_WANTRETURN)
  2089. {
  2090. //
  2091. // This edit control wants cr to be inserted so break out of
  2092. // case.
  2093. //
  2094. return;
  2095. }
  2096. defaultDlgId = (int)(DWORD)LOWORD(SendMessage(ped->hwndParent,
  2097. DM_GETDEFID, 0, 0L));
  2098. if (defaultDlgId)
  2099. {
  2100. HWND hwnd = GetDlgItem(ped->hwndParent, defaultDlgId);
  2101. if (hwnd)
  2102. {
  2103. SendMessage(ped->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwnd, 1L);
  2104. if (!ped->fFocus)
  2105. {
  2106. PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0L);
  2107. }
  2108. }
  2109. }
  2110. }
  2111. return;
  2112. }
  2113. break;
  2114. case VK_TAB:
  2115. //
  2116. // If this multiline edit control is in a dialog box, then we want the
  2117. // TAB key to take you to the next control, shift TAB to take you to the
  2118. // previous control. We always want CTRL-TAB to insert a tab into the
  2119. // edit control regardless of weather or not we're in a dialog box.
  2120. //
  2121. if (scState == CTRLDOWN)
  2122. {
  2123. EditML_Char(ped, virtKeyCode, keyMods);
  2124. }
  2125. else if (ped->fInDialogBox)
  2126. {
  2127. SendMessage(ped->hwndParent, WM_NEXTDLGCTL, scState == SHFTDOWN, 0L);
  2128. }
  2129. return;
  2130. case VK_LEFT:
  2131. //
  2132. // If the caret isn't at the beginning, we can move left
  2133. //
  2134. if (ped->ichCaret)
  2135. {
  2136. //
  2137. // Get new caret pos.
  2138. //
  2139. if (scState & CTRLDOWN)
  2140. {
  2141. //
  2142. // Move caret word left
  2143. //
  2144. Edit_Word(ped, ped->ichCaret, TRUE, &ped->ichCaret, NULL);
  2145. }
  2146. else
  2147. {
  2148. if (ped->pLpkEditCallout)
  2149. {
  2150. ped->ichCaret = Edit_MoveSelectionRestricted(ped, ped->ichCaret, TRUE);
  2151. }
  2152. else
  2153. {
  2154. //
  2155. // Move caret char left
  2156. //
  2157. ped->ichCaret = Edit_MoveSelection(ped, ped->ichCaret, TRUE);
  2158. }
  2159. }
  2160. //
  2161. // Get new selection
  2162. //
  2163. if (scState & SHFTDOWN)
  2164. {
  2165. if (MaxEqCar && !MinEqMax)
  2166. {
  2167. //
  2168. // Reduce selection
  2169. //
  2170. newMaxSel = ped->ichCaret;
  2171. UserAssert(newMinSel == ped->ichMinSel);
  2172. }
  2173. else
  2174. {
  2175. //
  2176. // Extend selection
  2177. //
  2178. newMinSel = ped->ichCaret;
  2179. }
  2180. }
  2181. else
  2182. {
  2183. //
  2184. // Clear selection
  2185. //
  2186. newMaxSel = newMinSel = ped->ichCaret;
  2187. }
  2188. changeSelection = TRUE;
  2189. }
  2190. else
  2191. {
  2192. //
  2193. // If the user tries to move left and we are at the 0th
  2194. // character and there is a selection, then cancel the
  2195. // selection.
  2196. //
  2197. if ( (ped->ichMaxSel != ped->ichMinSel) &&
  2198. !(scState & SHFTDOWN) )
  2199. {
  2200. changeSelection = TRUE;
  2201. newMaxSel = newMinSel = ped->ichCaret;
  2202. }
  2203. }
  2204. break;
  2205. case VK_RIGHT:
  2206. //
  2207. // If the caret isn't at the end, we can move right.
  2208. //
  2209. if (ped->ichCaret < ped->cch)
  2210. {
  2211. //
  2212. // Get new caret pos.
  2213. //
  2214. if (scState & CTRLDOWN)
  2215. {
  2216. //
  2217. // Move caret word right
  2218. //
  2219. Edit_Word(ped, ped->ichCaret, FALSE, NULL, &ped->ichCaret);
  2220. }
  2221. else
  2222. {
  2223. //
  2224. // Move caret char right
  2225. //
  2226. if (ped->pLpkEditCallout)
  2227. {
  2228. ped->ichCaret = Edit_MoveSelectionRestricted(ped, ped->ichCaret, FALSE);
  2229. }
  2230. else
  2231. {
  2232. ped->ichCaret = Edit_MoveSelection(ped, ped->ichCaret, FALSE);
  2233. }
  2234. }
  2235. //
  2236. // Get new selection.
  2237. //
  2238. if (scState & SHFTDOWN)
  2239. {
  2240. if (MinEqCar && !MinEqMax)
  2241. {
  2242. //
  2243. // Reduce selection
  2244. //
  2245. newMinSel = ped->ichCaret;
  2246. UserAssert(newMaxSel == ped->ichMaxSel);
  2247. }
  2248. else
  2249. {
  2250. //
  2251. // Extend selection
  2252. //
  2253. newMaxSel = ped->ichCaret;
  2254. }
  2255. }
  2256. else
  2257. {
  2258. //
  2259. // Clear selection
  2260. //
  2261. newMaxSel = newMinSel = ped->ichCaret;
  2262. }
  2263. changeSelection = TRUE;
  2264. }
  2265. else
  2266. {
  2267. //
  2268. // If the user tries to move right and we are at the last
  2269. // character and there is a selection, then cancel the
  2270. // selection.
  2271. //
  2272. if ( (ped->ichMaxSel != ped->ichMinSel) &&
  2273. !(scState & SHFTDOWN) )
  2274. {
  2275. newMaxSel = newMinSel = ped->ichCaret;
  2276. changeSelection = TRUE;
  2277. }
  2278. }
  2279. break;
  2280. case VK_UP:
  2281. case VK_DOWN:
  2282. if (ped->cLines - 1 != ped->iCaretLine &&
  2283. ped->ichCaret == ped->chLines[ped->iCaretLine + 1])
  2284. {
  2285. prevLine = TRUE;
  2286. }
  2287. else
  2288. {
  2289. prevLine = FALSE;
  2290. }
  2291. hdc = Edit_GetDC(ped, TRUE);
  2292. EditML_IchToXYPos(ped, hdc, ped->ichCaret, prevLine, &mousePt);
  2293. Edit_ReleaseDC(ped, hdc, TRUE);
  2294. mousePt.y += 1 + (virtKeyCode == VK_UP ? -ped->lineHeight : ped->lineHeight);
  2295. if (!(scState & CTRLDOWN))
  2296. {
  2297. //
  2298. // Send fake mouse messages to handle this
  2299. // If VK_SHIFT is down, extend selection & move caret up/down
  2300. // 1 line. Otherwise, clear selection & move caret.
  2301. //
  2302. EditML_MouseMotion(ped, WM_LBUTTONDOWN,
  2303. !(scState & SHFTDOWN) ? 0 : MK_SHIFT, &mousePt);
  2304. EditML_MouseMotion(ped, WM_LBUTTONUP,
  2305. !(scState & SHFTDOWN) ? 0 : MK_SHIFT, &mousePt);
  2306. }
  2307. break;
  2308. case VK_HOME:
  2309. //
  2310. // Update caret.
  2311. //
  2312. if (scState & CTRLDOWN)
  2313. {
  2314. //
  2315. // Move caret to beginning of text.
  2316. //
  2317. ped->ichCaret = 0;
  2318. }
  2319. else
  2320. {
  2321. //
  2322. // Move caret to beginning of line.
  2323. //
  2324. ped->ichCaret = ped->chLines[ped->iCaretLine];
  2325. }
  2326. //
  2327. // Update selection.
  2328. //
  2329. newMinSel = ped->ichCaret;
  2330. if (scState & SHFTDOWN)
  2331. {
  2332. if (MaxEqCar && !MinEqMax)
  2333. {
  2334. if (scState & CTRLDOWN)
  2335. {
  2336. newMaxSel = ped->ichMinSel;
  2337. }
  2338. else
  2339. {
  2340. newMinSel = ped->ichMinSel;
  2341. newMaxSel = ped->ichCaret;
  2342. }
  2343. }
  2344. }
  2345. else
  2346. {
  2347. //
  2348. // Clear selection
  2349. //
  2350. newMaxSel = ped->ichCaret;
  2351. }
  2352. changeSelection = TRUE;
  2353. break;
  2354. case VK_END:
  2355. //
  2356. // Update caret.
  2357. //
  2358. if (scState & CTRLDOWN)
  2359. {
  2360. //
  2361. // Move caret to end of text.
  2362. //
  2363. ped->ichCaret = ped->cch;
  2364. }
  2365. else
  2366. {
  2367. //
  2368. // Move caret to end of line.
  2369. //
  2370. ped->ichCaret = ped->chLines[ped->iCaretLine] +
  2371. EditML_Line(ped, ped->iCaretLine);
  2372. }
  2373. //
  2374. // Update selection.
  2375. //
  2376. newMaxSel = ped->ichCaret;
  2377. if (scState & SHFTDOWN)
  2378. {
  2379. if (MinEqCar && !MinEqMax)
  2380. {
  2381. //
  2382. // Reduce selection
  2383. //
  2384. if (scState & CTRLDOWN)
  2385. {
  2386. newMinSel = ped->ichMaxSel;
  2387. }
  2388. else
  2389. {
  2390. newMinSel = ped->ichCaret;
  2391. newMaxSel = ped->ichMaxSel;
  2392. }
  2393. }
  2394. }
  2395. else
  2396. {
  2397. //
  2398. // Clear selection
  2399. //
  2400. newMinSel = ped->ichCaret;
  2401. }
  2402. changeSelection = TRUE;
  2403. break;
  2404. //
  2405. // FE_IME // EC_INSERT_COMPOSITION_CHAR : EditML_KeyDown() : VK_HANJA support
  2406. //
  2407. case VK_HANJA:
  2408. if ( HanjaKeyHandler( ped ) )
  2409. {
  2410. changeSelection = TRUE;
  2411. newMinSel = ped->ichCaret;
  2412. newMaxSel = ped->ichCaret + (ped->fAnsi ? 2 : 1);
  2413. }
  2414. break;
  2415. case VK_PRIOR:
  2416. case VK_NEXT:
  2417. if (!(scState & CTRLDOWN))
  2418. {
  2419. //
  2420. // Vertical scroll by one visual screen
  2421. //
  2422. hdc = Edit_GetDC(ped, TRUE);
  2423. EditML_IchToXYPos(ped, hdc, ped->ichCaret, FALSE, &mousePt);
  2424. Edit_ReleaseDC(ped, hdc, TRUE);
  2425. mousePt.y += 1;
  2426. SendMessage(ped->hwnd, WM_VSCROLL, virtKeyCode == VK_PRIOR ? SB_PAGEUP : SB_PAGEDOWN, 0L);
  2427. //
  2428. // Move the cursor there
  2429. //
  2430. EditML_MouseMotion(ped, WM_LBUTTONDOWN, !(scState & SHFTDOWN) ? 0 : MK_SHIFT, &mousePt);
  2431. EditML_MouseMotion(ped, WM_LBUTTONUP, !(scState & SHFTDOWN) ? 0 : MK_SHIFT, &mousePt);
  2432. }
  2433. else
  2434. {
  2435. //
  2436. // Horizontal scroll by one screenful minus one char
  2437. //
  2438. iScrollAmt = ((ped->rcFmt.right - ped->rcFmt.left) / ped->aveCharWidth) - 1;
  2439. if (virtKeyCode == VK_PRIOR)
  2440. {
  2441. //
  2442. // For previous page
  2443. //
  2444. iScrollAmt *= -1;
  2445. }
  2446. SendMessage(ped->hwnd, WM_HSCROLL, MAKELONG(EM_LINESCROLL, iScrollAmt), 0);
  2447. break;
  2448. }
  2449. break;
  2450. case VK_DELETE:
  2451. if (ped->fReadOnly)
  2452. {
  2453. break;
  2454. }
  2455. switch (scState)
  2456. {
  2457. case NONEDOWN:
  2458. //
  2459. // Clear selection. If no selection, delete (clear) character
  2460. // right
  2461. //
  2462. if ((ped->ichMaxSel < ped->cch) && (ped->ichMinSel == ped->ichMaxSel))
  2463. {
  2464. //
  2465. // Move cursor forwards and send a backspace message...
  2466. //
  2467. if (ped->pLpkEditCallout)
  2468. {
  2469. ped->ichMinSel = ped->ichCaret;
  2470. ped->ichMaxSel = Edit_MoveSelectionRestricted(ped, ped->ichCaret, FALSE);
  2471. }
  2472. else
  2473. {
  2474. ped->ichCaret = Edit_MoveSelection(ped, ped->ichCaret, FALSE);
  2475. ped->ichMaxSel = ped->ichMinSel = ped->ichCaret;
  2476. }
  2477. goto DeleteAnotherChar;
  2478. }
  2479. break;
  2480. case SHFTDOWN:
  2481. //
  2482. // CUT selection ie. remove and copy to clipboard, or if no
  2483. // selection, delete (clear) character left.
  2484. //
  2485. if (ped->ichMinSel == ped->ichMaxSel)
  2486. {
  2487. goto DeleteAnotherChar;
  2488. }
  2489. else
  2490. {
  2491. SendMessage(ped->hwnd, WM_CUT, (UINT)0, 0L);
  2492. }
  2493. break;
  2494. case CTRLDOWN:
  2495. //
  2496. // Clear selection, or delete to end of line if no selection
  2497. //
  2498. if ((ped->ichMaxSel < ped->cch) && (ped->ichMinSel == ped->ichMaxSel))
  2499. {
  2500. ped->ichMaxSel = ped->ichCaret = ped->chLines[ped->iCaretLine] +
  2501. EditML_Line(ped, ped->iCaretLine);
  2502. }
  2503. break;
  2504. }
  2505. if (!(scState & SHFTDOWN) && (ped->ichMinSel != ped->ichMaxSel))
  2506. {
  2507. DeleteAnotherChar:
  2508. if (Is400Compat(UserGetVersion()))
  2509. {
  2510. EditML_Char(ped, VK_BACK, 0);
  2511. }
  2512. else
  2513. {
  2514. SendMessage(ped->hwnd, WM_CHAR, VK_BACK, 0);
  2515. }
  2516. }
  2517. //
  2518. // No need to update text or selection since BACKSPACE message does it
  2519. // for us.
  2520. //
  2521. break;
  2522. case VK_INSERT:
  2523. if (scState == CTRLDOWN || scState == SHFTDOWN)
  2524. {
  2525. //
  2526. // if CTRLDOWN Copy current selection to clipboard
  2527. //
  2528. //
  2529. // if SHFTDOWN Paste clipboard
  2530. //
  2531. SendMessage(ped->hwnd, (UINT)(scState == CTRLDOWN ? WM_COPY : WM_PASTE), 0, 0);
  2532. }
  2533. break;
  2534. }
  2535. if (changeSelection)
  2536. {
  2537. hdc = Edit_GetDC(ped, FALSE);
  2538. EditML_ChangeSelection(ped, hdc, newMinSel, newMaxSel);
  2539. //
  2540. // Set the caret's line
  2541. //
  2542. ped->iCaretLine = EditML_IchToLine(ped, ped->ichCaret);
  2543. if (virtKeyCode == VK_END &&
  2544. // Next line: Win95 Bug#11822, EditControl repaint (Sankar)
  2545. (ped->ichCaret == ped->chLines[ped->iCaretLine]) &&
  2546. ped->ichCaret < ped->cch &&
  2547. ped->fWrap && ped->iCaretLine > 0)
  2548. {
  2549. LPSTR pText = Edit_Lock(ped);
  2550. //
  2551. // Handle moving to the end of a word wrapped line. This keeps the
  2552. // cursor from falling to the start of the next line if we have word
  2553. // wrapped and there is no CRLF.
  2554. //
  2555. if ( ped->fAnsi )
  2556. {
  2557. if (*(WORD UNALIGNED *)(pText + ped->chLines[ped->iCaretLine] - 2) != 0x0A0D)
  2558. {
  2559. ped->iCaretLine--;
  2560. }
  2561. }
  2562. else
  2563. {
  2564. if (*(DWORD UNALIGNED *)(pText +
  2565. (ped->chLines[ped->iCaretLine] - 2)*ped->cbChar) != 0x000A000D)
  2566. {
  2567. ped->iCaretLine--;
  2568. }
  2569. }
  2570. Edit_Unlock(ped);
  2571. }
  2572. //
  2573. // Since drawtext sets the caret position
  2574. //
  2575. EditML_SetCaretPosition(ped, hdc);
  2576. Edit_ReleaseDC(ped, hdc, FALSE);
  2577. //
  2578. // Make sure we can see the cursor
  2579. //
  2580. EditML_EnsureCaretVisible(ped);
  2581. }
  2582. }
  2583. //---------------------------------------------------------------------------//
  2584. //
  2585. // EditML_Char
  2586. //
  2587. // Handles character and virtual key input
  2588. //
  2589. VOID EditML_Char(PED ped, DWORD keyValue, int keyMods)
  2590. {
  2591. WCHAR keyPress;
  2592. BOOL updateText = FALSE;
  2593. //
  2594. // keyValue is either:
  2595. // a Virtual Key (eg: VK_TAB, VK_ESCAPE, VK_BACK)
  2596. // a character (Unicode or "ANSI")
  2597. //
  2598. if (ped->fAnsi)
  2599. {
  2600. keyPress = LOBYTE(keyValue);
  2601. }
  2602. else
  2603. {
  2604. keyPress = LOWORD(keyValue);
  2605. }
  2606. if (ped->fMouseDown || keyPress == VK_ESCAPE)
  2607. {
  2608. //
  2609. // If we are in the middle of a mousedown command, don't do anything.
  2610. // Also, just ignore it if we get a translated escape key which happens
  2611. // with multiline edit controls in a dialog box.
  2612. //
  2613. return;
  2614. }
  2615. Edit_InOutReconversionMode(ped, FALSE);
  2616. {
  2617. int scState;
  2618. scState = Edit_GetModKeys(keyMods);
  2619. if (ped->fInDialogBox && scState != CTRLDOWN)
  2620. {
  2621. //
  2622. // If this multiline edit control is in a dialog box, then we want the
  2623. // TAB key to take you to the next control, shift TAB to take you to the
  2624. // previous control, and CTRL-TAB to insert a tab into the edit control.
  2625. // We moved the focus when we received the keydown message so we will
  2626. // ignore the TAB key now unless the ctrl key is down. Also, we want
  2627. // CTRL-RETURN to insert a return into the text and RETURN to be sent to
  2628. // the default button.
  2629. //
  2630. if (keyPress == VK_TAB || (keyPress == VK_RETURN && !(GET_STYLE(ped) & ES_WANTRETURN)))
  2631. {
  2632. return;
  2633. }
  2634. }
  2635. //
  2636. // Allow CTRL+C to copy from a read only edit control
  2637. // Ignore all other keys in read only controls
  2638. //
  2639. if ((ped->fReadOnly) && !((keyPress == 3) && (scState == CTRLDOWN)))
  2640. {
  2641. return;
  2642. }
  2643. }
  2644. switch (keyPress)
  2645. {
  2646. case 0x0A:
  2647. // linefeed
  2648. keyPress = VK_RETURN;
  2649. //
  2650. // FALL THRU
  2651. //
  2652. case VK_RETURN:
  2653. case VK_TAB:
  2654. case VK_BACK:
  2655. DeleteSelection:
  2656. if (EditML_DeleteText(ped))
  2657. {
  2658. updateText = TRUE;
  2659. }
  2660. break;
  2661. default:
  2662. if (keyPress >= TEXT(' '))
  2663. {
  2664. //
  2665. // If this is in [a-z],[A-Z] and we are an ES_NUMBER
  2666. // edit field, bail.
  2667. //
  2668. if (Is400Compat(UserGetVersion()) && GET_STYLE(ped) & ES_NUMBER)
  2669. {
  2670. if (!Edit_IsCharNumeric(ped, keyPress))
  2671. {
  2672. Edit_ShowBalloonTipWrap(ped->hwnd, IDS_NUMERIC_TITLE, IDS_NUMERIC_MSG, TTI_ERROR);
  2673. goto IllegalChar;
  2674. }
  2675. }
  2676. goto DeleteSelection;
  2677. }
  2678. break;
  2679. }
  2680. //
  2681. // Handle key codes
  2682. //
  2683. switch(keyPress)
  2684. {
  2685. UINT msg;
  2686. // Ctrl+Z == Undo
  2687. case 26:
  2688. msg = WM_UNDO;
  2689. goto SendEditingMessage;
  2690. break;
  2691. // Ctrl+X == Cut
  2692. case 24:
  2693. if (ped->ichMinSel == ped->ichMaxSel)
  2694. {
  2695. goto IllegalChar;
  2696. }
  2697. else
  2698. {
  2699. msg = WM_CUT;
  2700. goto SendEditingMessage;
  2701. }
  2702. break;
  2703. // Ctrl+C == Copy
  2704. case 3:
  2705. msg = WM_COPY;
  2706. goto SendEditingMessage;
  2707. break;
  2708. // Ctrl+V == Paste
  2709. case 22:
  2710. msg = WM_PASTE;
  2711. SendEditingMessage:
  2712. SendMessage(ped->hwnd, msg, 0, 0L);
  2713. break;
  2714. case VK_BACK:
  2715. //
  2716. // Delete any selected text or delete character left if no sel
  2717. //
  2718. if (!updateText && ped->ichMinSel)
  2719. {
  2720. //
  2721. // There was no selection to delete so we just delete
  2722. // character left if available
  2723. //
  2724. ped->ichMinSel = Edit_MoveSelection(ped, ped->ichCaret, TRUE);
  2725. EditML_DeleteText(ped);
  2726. }
  2727. break;
  2728. default:
  2729. if (keyPress == VK_RETURN)
  2730. {
  2731. if (ped->fAnsi)
  2732. {
  2733. keyValue = 0x0A0D;
  2734. }
  2735. else
  2736. {
  2737. keyValue = 0x000A000D;
  2738. }
  2739. }
  2740. if ( keyPress >= TEXT(' ')
  2741. || keyPress == VK_RETURN
  2742. || keyPress == VK_TAB
  2743. || keyPress == 0x1E // RS - Unicode block separator
  2744. || keyPress == 0x1F // US - Unicode segment separator
  2745. )
  2746. {
  2747. // Don't hide the cursor if someone has capture.
  2748. if (GetCapture() == NULL)
  2749. {
  2750. SetCursor(NULL);
  2751. }
  2752. if (ped->fAnsi)
  2753. {
  2754. //
  2755. // check if it's a leading byte of double byte character
  2756. //
  2757. if (Edit_IsDBCSLeadByte(ped,(BYTE)keyPress))
  2758. {
  2759. int DBCSkey;
  2760. DBCSkey = DbcsCombine(ped->hwnd, keyPress);
  2761. if ( DBCSkey != 0)
  2762. {
  2763. keyValue = DBCSkey;
  2764. }
  2765. }
  2766. EditML_InsertText(ped, (LPSTR)&keyValue, HIBYTE(keyValue) ? 2 : 1, TRUE);
  2767. }
  2768. else
  2769. {
  2770. EditML_InsertText(ped, (LPSTR)&keyValue, HIWORD(keyValue) ? 2 : 1, TRUE);
  2771. }
  2772. }
  2773. else
  2774. {
  2775. IllegalChar:
  2776. MessageBeep(0);
  2777. }
  2778. break;
  2779. }
  2780. }
  2781. //---------------------------------------------------------------------------//
  2782. //
  2783. // EditML_PasteText AorW
  2784. //
  2785. // Pastes a line of text from the clipboard into the edit control
  2786. // starting at ped->ichCaret. Updates ichMaxSel and ichMinSel to point to the
  2787. // end of the inserted text. Notifies the parent if space cannot be
  2788. // allocated. Returns how many characters were inserted.
  2789. //
  2790. ICH EditML_PasteText(PED ped)
  2791. {
  2792. HANDLE hData;
  2793. LPSTR lpchClip;
  2794. ICH cchAdded = 0;
  2795. HCURSOR hCursorOld;
  2796. #ifdef UNDO_CLEANUP // #ifdef Added in Chicago - johnl
  2797. if (!ped->fAutoVScroll)
  2798. {
  2799. //
  2800. // Empty the undo buffer if this edit control limits the amount of text
  2801. // the user can add to the window rect. This is so that we can undo this
  2802. // operation if doing in causes us to exceed the window boundaries.
  2803. //
  2804. Edit_EmptyUndo(ped);
  2805. }
  2806. #endif
  2807. hCursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
  2808. if (!OpenClipboard(ped->hwnd))
  2809. {
  2810. goto PasteExitNoCloseClip;
  2811. }
  2812. if (!(hData = GetClipboardData(ped->fAnsi ? CF_TEXT : CF_UNICODETEXT)) ||
  2813. (GlobalFlags(hData) == GMEM_INVALID_HANDLE))
  2814. {
  2815. TraceMsg(TF_STANDARD, "Edit: EditML_PasteText(): couldn't get a valid handle(%x)", hData);
  2816. goto PasteExit;
  2817. }
  2818. //
  2819. // See if any text should be deleted
  2820. //
  2821. EditML_DeleteText(ped);
  2822. lpchClip = GlobalLock(hData);
  2823. if (lpchClip == NULL)
  2824. {
  2825. TraceMsg(TF_STANDARD, "Edit: EditML_PasteText: USERGLOBALLOCK(%x) failed.", hData);
  2826. goto PasteExit;
  2827. }
  2828. //
  2829. // Get the length of the addition.
  2830. //
  2831. if (ped->fAnsi)
  2832. {
  2833. cchAdded = strlen(lpchClip);
  2834. }
  2835. else
  2836. {
  2837. cchAdded = wcslen((LPWSTR)lpchClip);
  2838. }
  2839. //
  2840. // Insert the text (EditML_InsertText checks line length)
  2841. //
  2842. cchAdded = EditML_InsertText(ped, lpchClip, cchAdded, FALSE);
  2843. GlobalUnlock(hData);
  2844. PasteExit:
  2845. CloseClipboard();
  2846. PasteExitNoCloseClip:
  2847. SetCursor(hCursorOld);
  2848. return cchAdded;
  2849. }
  2850. //---------------------------------------------------------------------------//
  2851. VOID EditML_MouseMotion(PED ped, UINT message, UINT virtKeyDown, LPPOINT mousePt)
  2852. {
  2853. BOOL fChangedSel = FALSE;
  2854. UINT dtScroll = GetDoubleClickTime() / 5;
  2855. HDC hdc = Edit_GetDC(ped, TRUE);
  2856. ICH ichMaxSel = ped->ichMaxSel;
  2857. ICH ichMinSel = ped->ichMinSel;
  2858. ICH mouseCch;
  2859. ICH mouseLine;
  2860. int i, j;
  2861. LONG ll, lh;
  2862. mouseCch = EditML_MouseToIch(ped, hdc, mousePt, &mouseLine);
  2863. //
  2864. // Save for timer
  2865. //
  2866. ped->ptPrevMouse = *mousePt;
  2867. ped->prevKeys = virtKeyDown;
  2868. switch (message)
  2869. {
  2870. case WM_LBUTTONDBLCLK:
  2871. //
  2872. // if shift key is down, extend selection to word we double clicked on
  2873. // else clear current selection and select word.
  2874. // LiZ -- 5/5/93
  2875. //
  2876. if (ped->fAnsi && ped->fDBCS)
  2877. {
  2878. LPSTR pText = Edit_Lock(ped);
  2879. Edit_Word(ped,ped->ichCaret,
  2880. Edit_IsDBCSLeadByte(ped, *(pText+(ped->ichCaret)))
  2881. ? FALSE :
  2882. (ped->ichCaret == ped->chLines[ped->iCaretLine]
  2883. ? FALSE : TRUE), &ll, &lh);
  2884. Edit_Unlock(ped);
  2885. }
  2886. else
  2887. {
  2888. Edit_Word(ped, mouseCch, !(mouseCch == ped->chLines[mouseLine]), &ll, &lh);
  2889. }
  2890. if (!(virtKeyDown & MK_SHIFT))
  2891. {
  2892. //
  2893. // If shift key isn't down, move caret to mouse point and clear
  2894. // old selection
  2895. //
  2896. ichMinSel = ll;
  2897. ichMaxSel = ped->ichCaret = lh;
  2898. }
  2899. else
  2900. {
  2901. //
  2902. // Shiftkey is down so we want to maintain the current selection
  2903. // (if any) and just extend or reduce it
  2904. //
  2905. if (ped->ichMinSel == ped->ichCaret)
  2906. {
  2907. ichMinSel = ped->ichCaret = ll;
  2908. Edit_Word(ped, ichMaxSel, TRUE, &ll, &lh);
  2909. }
  2910. else
  2911. {
  2912. ichMaxSel = ped->ichCaret = lh;
  2913. Edit_Word(ped, ichMinSel, FALSE, &ll, &lh);
  2914. }
  2915. }
  2916. ped->ichStartMinSel = ll;
  2917. ped->ichStartMaxSel = lh;
  2918. goto InitDragSelect;
  2919. case WM_MOUSEMOVE:
  2920. if (ped->fMouseDown)
  2921. {
  2922. //
  2923. // Set the system timer to automatically scroll when mouse is
  2924. // outside of the client rectangle. Speed of scroll depends on
  2925. // distance from window.
  2926. //
  2927. i = mousePt->y < 0 ? -mousePt->y : mousePt->y - ped->rcFmt.bottom;
  2928. j = dtScroll - ((UINT)i << 4);
  2929. if (j < 1)
  2930. {
  2931. j = 1;
  2932. }
  2933. SetTimer(ped->hwnd, IDSYS_SCROLL, (UINT)j, NULL);
  2934. fChangedSel = TRUE;
  2935. //
  2936. // Extend selection, move caret right
  2937. //
  2938. if (ped->ichStartMinSel || ped->ichStartMaxSel)
  2939. {
  2940. //
  2941. // We're in WORD SELECT mode
  2942. //
  2943. BOOL fReverse = (mouseCch <= ped->ichStartMinSel);
  2944. Edit_Word(ped, mouseCch, !fReverse, &ll, &lh);
  2945. if (fReverse)
  2946. {
  2947. ichMinSel = ped->ichCaret = ll;
  2948. ichMaxSel = ped->ichStartMaxSel;
  2949. }
  2950. else
  2951. {
  2952. ichMinSel = ped->ichStartMinSel;
  2953. ichMaxSel = ped->ichCaret = lh;
  2954. }
  2955. }
  2956. else if ((ped->ichMinSel == ped->ichCaret) &&
  2957. (ped->ichMinSel != ped->ichMaxSel))
  2958. {
  2959. //
  2960. // Reduce selection extent
  2961. //
  2962. ichMinSel = ped->ichCaret = mouseCch;
  2963. }
  2964. else
  2965. {
  2966. //
  2967. // Extend selection extent
  2968. //
  2969. ichMaxSel = ped->ichCaret = mouseCch;
  2970. }
  2971. ped->iCaretLine = mouseLine;
  2972. }
  2973. break;
  2974. case WM_LBUTTONDOWN:
  2975. ll = lh = mouseCch;
  2976. if (!(virtKeyDown & MK_SHIFT))
  2977. {
  2978. //
  2979. // If shift key isn't down, move caret to mouse point and clear
  2980. // old selection
  2981. //
  2982. ichMinSel = ichMaxSel = ped->ichCaret = mouseCch;
  2983. }
  2984. else
  2985. {
  2986. //
  2987. // Shiftkey is down so we want to maintain the current selection
  2988. // (if any) and just extend or reduce it
  2989. //
  2990. if (ped->ichMinSel == ped->ichCaret)
  2991. {
  2992. ichMinSel = ped->ichCaret = mouseCch;
  2993. }
  2994. else
  2995. {
  2996. ichMaxSel = ped->ichCaret = mouseCch;
  2997. }
  2998. }
  2999. ped->ichStartMinSel = ped->ichStartMaxSel = 0;
  3000. InitDragSelect:
  3001. ped->iCaretLine = mouseLine;
  3002. ped->fMouseDown = FALSE;
  3003. SetCapture(ped->hwnd);
  3004. ped->fMouseDown = TRUE;
  3005. fChangedSel = TRUE;
  3006. //
  3007. // Set the timer so that we can scroll automatically when the mouse
  3008. // is moved outside the window rectangle.
  3009. //
  3010. SetTimer(ped->hwnd, IDSYS_SCROLL, dtScroll, NULL);
  3011. break;
  3012. case WM_LBUTTONUP:
  3013. if (ped->fMouseDown)
  3014. {
  3015. //
  3016. // Kill the timer so that we don't do auto mouse moves anymore
  3017. //
  3018. KillTimer(ped->hwnd, IDSYS_SCROLL);
  3019. ReleaseCapture();
  3020. EditML_SetCaretPosition(ped, hdc);
  3021. ped->fMouseDown = FALSE;
  3022. }
  3023. break;
  3024. }
  3025. if (fChangedSel)
  3026. {
  3027. EditML_ChangeSelection(ped, hdc, ichMinSel, ichMaxSel);
  3028. EditML_EnsureCaretVisible(ped);
  3029. }
  3030. Edit_ReleaseDC(ped, hdc, TRUE);
  3031. if (!ped->fFocus && (message == WM_LBUTTONDOWN))
  3032. {
  3033. //
  3034. // If we don't have the focus yet, get it
  3035. //
  3036. SetFocus(ped->hwnd);
  3037. }
  3038. }
  3039. //---------------------------------------------------------------------------//
  3040. LONG EditML_Scroll(PED ped, BOOL fVertical, int cmd, int iAmt, BOOL fRedraw)
  3041. {
  3042. SCROLLINFO si;
  3043. int dx = 0;
  3044. int dy = 0;
  3045. BOOL fIncludeLeftMargin;
  3046. int newPos;
  3047. int oldPos;
  3048. BOOL fUp = FALSE;
  3049. UINT wFlag;
  3050. DWORD dwTime = 0;
  3051. if (fRedraw && (cmd != ML_REFRESH))
  3052. {
  3053. UpdateWindow(ped->hwnd);
  3054. }
  3055. if (ped->pLpkEditCallout && ped->fRtoLReading && !fVertical
  3056. && ped->maxPixelWidth > ped->rcFmt.right - ped->rcFmt.left)
  3057. {
  3058. //
  3059. // Horizontal scoll of a right oriented window with a scrollbar.
  3060. // Map the logical xOffset to visual coordinates.
  3061. //
  3062. oldPos = ped->maxPixelWidth
  3063. - ((int)ped->xOffset + ped->rcFmt.right - ped->rcFmt.left);
  3064. }
  3065. else
  3066. {
  3067. oldPos = (int) (fVertical ? ped->ichScreenStart : ped->xOffset);
  3068. }
  3069. fIncludeLeftMargin = (ped->xOffset == 0);
  3070. switch (cmd)
  3071. {
  3072. case ML_REFRESH:
  3073. newPos = oldPos;
  3074. break;
  3075. case EM_GETTHUMB:
  3076. return oldPos;
  3077. case SB_THUMBTRACK:
  3078. case SB_THUMBPOSITION:
  3079. //
  3080. // If the edit contains more than 0xFFFF lines
  3081. // it means that the scrolbar can return a position
  3082. // that cannot fit in a WORD (16 bits), so use
  3083. // GetScrollInfo (which is slower) in this case.
  3084. //
  3085. if (ped->cLines < 0xFFFF)
  3086. {
  3087. newPos = iAmt;
  3088. }
  3089. else
  3090. {
  3091. SCROLLINFO si;
  3092. si.cbSize = sizeof(SCROLLINFO);
  3093. si.fMask = SIF_TRACKPOS;
  3094. GetScrollInfo( ped->hwnd, SB_VERT, &si);
  3095. newPos = si.nTrackPos;
  3096. }
  3097. break;
  3098. case SB_TOP: // == SB_LEFT
  3099. newPos = 0;
  3100. break;
  3101. case SB_BOTTOM: // == SB_RIGHT
  3102. if (fVertical)
  3103. {
  3104. newPos = ped->cLines;
  3105. }
  3106. else
  3107. {
  3108. newPos = ped->maxPixelWidth;
  3109. }
  3110. break;
  3111. case SB_PAGEUP: // == SB_PAGELEFT
  3112. fUp = TRUE;
  3113. case SB_PAGEDOWN: // == SB_PAGERIGHT
  3114. if (fVertical)
  3115. {
  3116. iAmt = ped->ichLinesOnScreen - 1;
  3117. }
  3118. else
  3119. {
  3120. iAmt = (ped->rcFmt.right - ped->rcFmt.left) - 1;
  3121. }
  3122. if (iAmt == 0)
  3123. {
  3124. iAmt++;
  3125. }
  3126. if (fUp)
  3127. {
  3128. iAmt = -iAmt;
  3129. }
  3130. goto AddDelta;
  3131. case SB_LINEUP: // == SB_LINELEFT
  3132. fUp = TRUE;
  3133. case SB_LINEDOWN: // == SB_LINERIGHT
  3134. dwTime = iAmt;
  3135. iAmt = 1;
  3136. if (fUp)
  3137. {
  3138. iAmt = -iAmt;
  3139. }
  3140. // | |
  3141. // | FALL THRU |
  3142. // V V
  3143. case EM_LINESCROLL:
  3144. if (!fVertical)
  3145. {
  3146. iAmt *= ped->aveCharWidth;
  3147. }
  3148. AddDelta:
  3149. newPos = oldPos + iAmt;
  3150. break;
  3151. default:
  3152. return(0L);
  3153. }
  3154. if (fVertical)
  3155. {
  3156. if (si.nMax = ped->cLines)
  3157. {
  3158. si.nMax--;
  3159. }
  3160. if (!ped->hwndParent ||
  3161. TestWF(ped->hwndParent, WFWIN40COMPAT))
  3162. {
  3163. si.nPage = ped->ichLinesOnScreen;
  3164. }
  3165. else
  3166. {
  3167. si.nPage = 0;
  3168. }
  3169. wFlag = WS_VSCROLL;
  3170. }
  3171. else
  3172. {
  3173. si.nMax = ped->maxPixelWidth;
  3174. si.nPage = ped->rcFmt.right - ped->rcFmt.left;
  3175. wFlag = WS_HSCROLL;
  3176. }
  3177. if (TESTFLAG(GET_STYLE(ped), wFlag))
  3178. {
  3179. si.cbSize = sizeof(SCROLLINFO);
  3180. si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
  3181. si.nMin = 0;
  3182. si.nPos = newPos;
  3183. newPos = SetScrollInfo(ped->hwnd, fVertical ? SB_VERT : SB_HORZ,
  3184. &si, fRedraw);
  3185. }
  3186. else
  3187. {
  3188. //
  3189. // BOGUS -- this is duped code from ScrollBar code
  3190. // but it's for the case when we want to limit the position without
  3191. // actually having the scroll bar
  3192. //
  3193. int iMaxPos;
  3194. //
  3195. // Clip page to 0, range + 1
  3196. //
  3197. si.nPage = max(min((int)si.nPage, si.nMax + 1), 0);
  3198. iMaxPos = si.nMax - (si.nPage ? si.nPage - 1 : 0);
  3199. newPos = min(max(newPos, 0), iMaxPos);
  3200. }
  3201. oldPos -= newPos;
  3202. if (!oldPos)
  3203. {
  3204. return 0;
  3205. }
  3206. if (ped->pLpkEditCallout && ped->fRtoLReading && !fVertical
  3207. && ped->maxPixelWidth > ped->rcFmt.right - ped->rcFmt.left)
  3208. {
  3209. //
  3210. // Map visual oldPos and newPos back to logical coordinates
  3211. //
  3212. newPos = ped->maxPixelWidth
  3213. - (newPos + ped->rcFmt.right - ped->rcFmt.left);
  3214. oldPos = -oldPos;
  3215. if (newPos<0)
  3216. {
  3217. //
  3218. // Compensate for scroll bar returning pos > max-page
  3219. //
  3220. oldPos += newPos;
  3221. newPos=0;
  3222. }
  3223. }
  3224. if (fVertical)
  3225. {
  3226. ped->ichScreenStart = newPos;
  3227. dy = oldPos * ped->lineHeight;
  3228. }
  3229. else
  3230. {
  3231. ped->xOffset = newPos;
  3232. dx = oldPos;
  3233. }
  3234. if (cmd != SB_THUMBTRACK)
  3235. {
  3236. //
  3237. // We don't want to notify the parent of thumbtracking since they might
  3238. // try to set the thumb position to something bogus.
  3239. // NOTEPAD used to be guilty of this -- but I rewrote it so it's not.
  3240. // The question is WHO ELSE does this? (jeffbog)
  3241. //
  3242. Edit_NotifyParent(ped, fVertical ? EN_VSCROLL : EN_HSCROLL);
  3243. }
  3244. if (fRedraw && IsWindowVisible(ped->hwnd))
  3245. {
  3246. RECT rc;
  3247. RECT rcUpdate;
  3248. RECT rcClipRect;
  3249. HDC hdc;
  3250. HBRUSH hbr = NULL;
  3251. BOOL fNeedDelete = FALSE;
  3252. GetClientRect(ped->hwnd, &rc);
  3253. CopyRect(&rcClipRect, &ped->rcFmt);
  3254. if (fVertical)
  3255. {
  3256. rcClipRect.left -= ped->wLeftMargin;
  3257. rcClipRect.right += ped->wRightMargin;
  3258. }
  3259. IntersectRect(&rc, &rc, &rcClipRect);
  3260. rc.bottom++;
  3261. //
  3262. // Chicago has this HideCaret but there doesn't appear to be a
  3263. // corresponding ShowCaret, so we lose the Caret under NT when the
  3264. // EC scrolls - Johnl
  3265. //
  3266. // HideCaret(ped->hwnd);
  3267. hdc = Edit_GetDC(ped, FALSE);
  3268. Edit_SetClip(ped, hdc, fIncludeLeftMargin);
  3269. if (ped->hFont)
  3270. {
  3271. SelectObject(hdc, ped->hFont);
  3272. }
  3273. hbr = Edit_GetBrush(ped, hdc, &fNeedDelete);
  3274. if (hbr && fNeedDelete)
  3275. {
  3276. DeleteObject(hbr);
  3277. }
  3278. if (ped->pLpkEditCallout && !fVertical)
  3279. {
  3280. //
  3281. // Horizontal scroll with complex script support
  3282. //
  3283. int xFarOffset = ped->xOffset + ped->rcFmt.right - ped->rcFmt.left;
  3284. rc = ped->rcFmt;
  3285. if (dwTime != 0)
  3286. {
  3287. ScrollWindowEx(ped->hwnd, ped->fRtoLReading ? -dx : dx, dy, NULL, NULL, NULL,
  3288. &rcUpdate, MAKELONG(SW_SMOOTHSCROLL | SW_SCROLLCHILDREN, dwTime));
  3289. }
  3290. else
  3291. {
  3292. ScrollDC(hdc, ped->fRtoLReading ? -dx : dx, dy,
  3293. &rc, &rc, NULL, &rcUpdate);
  3294. }
  3295. //
  3296. // Handle margins: Blank if clipped by horizontal scrolling,
  3297. // display otherwise.
  3298. //
  3299. if (ped->wLeftMargin)
  3300. {
  3301. rc.left = ped->rcFmt.left - ped->wLeftMargin;
  3302. rc.right = ped->rcFmt.left;
  3303. if ( (ped->format != ES_LEFT) // Always display margin for centred or far-aligned text
  3304. || // Display LTR left margin if first character fully visible
  3305. (!ped->fRtoLReading && ped->xOffset == 0)
  3306. || // Display RTL left margin if last character fully visible
  3307. (ped->fRtoLReading && xFarOffset >= ped->maxPixelWidth))
  3308. {
  3309. UnionRect(&rcUpdate, &rcUpdate, &rc);
  3310. }
  3311. else
  3312. {
  3313. ExtTextOutW(hdc, rc.left, rc.top,
  3314. ETO_CLIPPED | ETO_OPAQUE | ETO_GLYPH_INDEX,
  3315. &rc, L"", 0, 0L);
  3316. }
  3317. }
  3318. if (ped->wRightMargin)
  3319. {
  3320. rc.left = ped->rcFmt.right;
  3321. rc.right = ped->rcFmt.right + ped->wRightMargin;
  3322. if ( (ped->format != ES_LEFT) // Always display margin for centred or far-aligned text
  3323. || // Display RTL right margin if first character fully visible
  3324. (ped->fRtoLReading && ped->xOffset == 0)
  3325. || // Display LTR right margin if last character fully visible
  3326. (!ped->fRtoLReading && xFarOffset >= ped->maxPixelWidth))
  3327. {
  3328. UnionRect(&rcUpdate, &rcUpdate, &rc);
  3329. }
  3330. else
  3331. {
  3332. ExtTextOutW(hdc, rc.left, rc.top,
  3333. ETO_CLIPPED | ETO_OPAQUE | ETO_GLYPH_INDEX,
  3334. &rc, L"", 0, 0L);
  3335. }
  3336. }
  3337. }
  3338. else
  3339. {
  3340. if (dwTime != 0)
  3341. {
  3342. ScrollWindowEx(ped->hwnd, dx, dy, NULL, NULL, NULL,
  3343. &rcUpdate, MAKELONG(SW_SMOOTHSCROLL | SW_SCROLLCHILDREN, dwTime));
  3344. }
  3345. else
  3346. {
  3347. ScrollDC(hdc, dx, dy, &rc, &rc, NULL, &rcUpdate);
  3348. }
  3349. //
  3350. // If we need to wipe out the left margin area
  3351. //
  3352. if (ped->wLeftMargin && !fVertical)
  3353. {
  3354. //
  3355. // Calculate the rectangle to be wiped out
  3356. //
  3357. rc.right = rc.left;
  3358. rc.left = max(0, ped->rcFmt.left - (LONG)ped->wLeftMargin);
  3359. if (rc.left < rc.right)
  3360. {
  3361. if (fIncludeLeftMargin && (ped->xOffset != 0))
  3362. {
  3363. ExtTextOutW(hdc, rc.left, rc.top, ETO_CLIPPED | ETO_OPAQUE,
  3364. &rc, L"", 0, 0L);
  3365. }
  3366. else
  3367. {
  3368. if((!fIncludeLeftMargin) && (ped->xOffset == 0))
  3369. {
  3370. UnionRect(&rcUpdate, &rcUpdate, &rc);
  3371. }
  3372. }
  3373. }
  3374. }
  3375. }
  3376. EditML_SetCaretPosition(ped,hdc);
  3377. Edit_ReleaseDC(ped, hdc, FALSE);
  3378. InvalidateRect(ped->hwnd, &rcUpdate, TRUE);
  3379. UpdateWindow(ped->hwnd);
  3380. }
  3381. return MAKELONG(-oldPos, 1);
  3382. }
  3383. //---------------------------------------------------------------------------//
  3384. //
  3385. // EditML_SetFocus AorW
  3386. //
  3387. // Gives the edit control the focus and notifies the parent
  3388. // EN_SETFOCUS.
  3389. //
  3390. void EditML_SetFocus(PED ped)
  3391. {
  3392. HDC hdc;
  3393. INT cxCaret;
  3394. SystemParametersInfo(SPI_GETCARETWIDTH, 0, (LPVOID)&cxCaret, 0);
  3395. if (!ped->fFocus)
  3396. {
  3397. ped->fFocus = TRUE;
  3398. InvalidateRect(ped->hwnd, NULL, TRUE);
  3399. hdc = Edit_GetDC(ped, TRUE);
  3400. //
  3401. // Draw the caret. We need to do this even if the window is hidden
  3402. // because in dlg box initialization time we may set the focus to a
  3403. // hidden edit control window. If we don't create the caret etc, it will
  3404. // never end up showing properly.
  3405. //
  3406. if (ped->pLpkEditCallout)
  3407. {
  3408. ped->pLpkEditCallout->EditCreateCaret((PED0)ped, hdc, cxCaret, ped->lineHeight, 0);
  3409. }
  3410. else
  3411. {
  3412. CreateCaret(ped->hwnd, (HBITMAP)NULL, cxCaret, ped->lineHeight);
  3413. }
  3414. ShowCaret(ped->hwnd);
  3415. EditML_SetCaretPosition(ped, hdc);
  3416. //
  3417. // Show the current selection. Only if the selection was hidden when we
  3418. // lost the focus, must we invert (show) it.
  3419. //
  3420. if (!ped->fNoHideSel && ped->ichMinSel != ped->ichMaxSel &&
  3421. IsWindowVisible(ped->hwnd))
  3422. {
  3423. EditML_DrawText(ped, hdc, ped->ichMinSel, ped->ichMaxSel, TRUE);
  3424. }
  3425. Edit_ReleaseDC(ped, hdc, TRUE);
  3426. }
  3427. //
  3428. // Notify parent we have the focus
  3429. //
  3430. Edit_NotifyParent(ped, EN_SETFOCUS);
  3431. }
  3432. //---------------------------------------------------------------------------//
  3433. //
  3434. // EditML_KillFocus AorW
  3435. //
  3436. // The edit control loses the focus and notifies the parent via
  3437. // EN_KILLFOCUS.
  3438. //
  3439. VOID EditML_KillFocus(PED ped)
  3440. {
  3441. HDC hdc;
  3442. //
  3443. // Reset the wheel delta count.
  3444. //
  3445. if (ped->fFocus)
  3446. {
  3447. ped->fFocus = 0;
  3448. //
  3449. // Do this only if we still have the focus. But we always notify the
  3450. // parent that we lost the focus whether or not we originally had the
  3451. // focus.
  3452. //
  3453. //
  3454. // Hide the current selection if needed
  3455. //
  3456. #ifdef _USE_DRAW_THEME_TEXT_
  3457. if (((!ped->fNoHideSel && ped->ichMinSel != ped->ichMaxSel &&
  3458. IsWindowVisible(ped->hwnd))) || ped->hTheme)
  3459. {
  3460. hdc = Edit_GetDC(ped, FALSE);
  3461. if ( !ped->hTheme )
  3462. {
  3463. EditML_DrawText(ped, hdc, ped->ichMinSel, ped->ichMaxSel, TRUE);
  3464. }
  3465. else
  3466. {
  3467. InvalidateRect(ped->hwnd, NULL, TRUE);
  3468. }
  3469. Edit_ReleaseDC(ped, hdc, FALSE);
  3470. }
  3471. #else
  3472. if (((!ped->fNoHideSel && ped->ichMinSel != ped->ichMaxSel &&
  3473. IsWindowVisible(ped->hwnd))))
  3474. {
  3475. hdc = Edit_GetDC(ped, FALSE);
  3476. EditML_DrawText(ped, hdc, ped->ichMinSel, ped->ichMaxSel, TRUE);
  3477. Edit_ReleaseDC(ped, hdc, FALSE);
  3478. }
  3479. #endif // _USE_DRAW_THEME_TEXT_
  3480. //
  3481. // Destroy the caret
  3482. //
  3483. DestroyCaret();
  3484. }
  3485. //
  3486. // Notify parent that we lost the focus.
  3487. //
  3488. Edit_NotifyParent(ped, EN_KILLFOCUS);
  3489. }
  3490. //---------------------------------------------------------------------------//
  3491. //
  3492. // EditML_EnsureCaretVisible AorW
  3493. //
  3494. // Scrolls the caret into the visible region.
  3495. // Returns TRUE if scrolling was done else return s FALSE.
  3496. //
  3497. BOOL EditML_EnsureCaretVisible(PED ped)
  3498. {
  3499. UINT iLineMax;
  3500. int xposition;
  3501. BOOL fPrevLine;
  3502. HDC hdc;
  3503. BOOL fVScroll = FALSE;
  3504. BOOL fHScroll = FALSE;
  3505. if (IsWindowVisible(ped->hwnd))
  3506. {
  3507. int iAmt;
  3508. int iFmtWidth = ped->rcFmt.right - ped->rcFmt.left;
  3509. if (ped->fAutoVScroll)
  3510. {
  3511. iLineMax = ped->ichScreenStart + ped->ichLinesOnScreen - 1;
  3512. if (fVScroll = (ped->iCaretLine > iLineMax))
  3513. {
  3514. iAmt = iLineMax;
  3515. }
  3516. else if (fVScroll = (ped->iCaretLine < ped->ichScreenStart))
  3517. {
  3518. iAmt = ped->ichScreenStart;
  3519. }
  3520. if (fVScroll)
  3521. {
  3522. EditML_Scroll(ped, TRUE, EM_LINESCROLL, ped->iCaretLine - iAmt, TRUE);
  3523. }
  3524. }
  3525. if (ped->fAutoHScroll && ((int) ped->maxPixelWidth > iFmtWidth))
  3526. {
  3527. POINT pt;
  3528. //
  3529. // Get the current position of the caret in pixels
  3530. //
  3531. if ((UINT) (ped->cLines - 1) != ped->iCaretLine &&
  3532. ped->ichCaret == ped->chLines[ped->iCaretLine + 1])
  3533. {
  3534. fPrevLine = TRUE;
  3535. }
  3536. else
  3537. {
  3538. fPrevLine = FALSE;
  3539. }
  3540. hdc = Edit_GetDC(ped,TRUE);
  3541. EditML_IchToXYPos(ped, hdc, ped->ichCaret, fPrevLine, &pt);
  3542. Edit_ReleaseDC(ped, hdc, TRUE);
  3543. xposition = pt.x;
  3544. //
  3545. // Remember, EditML_IchToXYPos returns coordinates with respect to the
  3546. // top left pixel displayed on the screen. Thus, if xPosition < 0,
  3547. // it means xPosition is less than current ped->xOffset.
  3548. //
  3549. iFmtWidth /= 3;
  3550. if (fHScroll = (xposition < ped->rcFmt.left))
  3551. {
  3552. //
  3553. // scroll to the left
  3554. //
  3555. iAmt = ped->rcFmt.left + iFmtWidth;
  3556. }
  3557. else if (fHScroll = (xposition > ped->rcFmt.right))
  3558. {
  3559. //
  3560. // scroll to the right
  3561. //
  3562. iAmt = ped->rcFmt.right - iFmtWidth;
  3563. }
  3564. if (fHScroll)
  3565. {
  3566. EditML_Scroll(ped, FALSE, EM_LINESCROLL, (xposition - iAmt) / ped->aveCharWidth, TRUE);
  3567. }
  3568. }
  3569. }
  3570. return fVScroll;
  3571. }
  3572. //---------------------------------------------------------------------------//
  3573. //
  3574. // EditML_WndProc
  3575. //
  3576. // Class procedure for all multi line edit controls.
  3577. // Dispatches all messages to the appropriate handlers which are named
  3578. // as follows:
  3579. //
  3580. // EditSL_ (single line) prefixes all single line edit control procedures while
  3581. // Edit_ (edit control) prefixes all common handlers.
  3582. //
  3583. // The EditML_WndProc only handles messages specific to multi line edit
  3584. // controls.
  3585. //
  3586. LRESULT EditML_WndProc(PED ped, UINT message, WPARAM wParam, LPARAM lParam)
  3587. {
  3588. HDC hdc;
  3589. PAINTSTRUCT ps;
  3590. LPRECT lprc;
  3591. POINT pt;
  3592. DWORD windowstyle;
  3593. static INT scWheelDelta;
  3594. switch (message)
  3595. {
  3596. case WM_INPUTLANGCHANGE:
  3597. if (ped && ped->fFocus && ped->pLpkEditCallout)
  3598. {
  3599. INT cxCaret;
  3600. SystemParametersInfo(SPI_GETCARETWIDTH, 0, (LPVOID)&cxCaret, 0);
  3601. HideCaret(ped->hwnd);
  3602. hdc = Edit_GetDC(ped, TRUE);
  3603. DestroyCaret();
  3604. ped->pLpkEditCallout->EditCreateCaret((PED0)ped, hdc, cxCaret, ped->lineHeight, (UINT)lParam);
  3605. EditML_SetCaretPosition(ped, hdc);
  3606. Edit_ReleaseDC(ped, hdc, TRUE);
  3607. ShowCaret(ped->hwnd);
  3608. }
  3609. goto PassToDefaultWindowProc;
  3610. case WM_STYLECHANGED:
  3611. if (ped && ped->pLpkEditCallout)
  3612. {
  3613. switch (wParam)
  3614. {
  3615. case GWL_STYLE:
  3616. Edit_UpdateFormat(ped,
  3617. ((LPSTYLESTRUCT)lParam)->styleNew,
  3618. GET_EXSTYLE(ped));
  3619. return 1L;
  3620. case GWL_EXSTYLE:
  3621. Edit_UpdateFormat(ped,
  3622. GET_STYLE(ped),
  3623. ((LPSTYLESTRUCT)lParam)->styleNew);
  3624. return 1L;
  3625. }
  3626. }
  3627. goto PassToDefaultWindowProc;
  3628. case WM_CHAR:
  3629. //
  3630. // wParam - the value of the key
  3631. // lParam - modifiers, repeat count etc (not used)
  3632. //
  3633. EditML_Char(ped, (UINT)wParam, 0);
  3634. break;
  3635. case WM_ERASEBKGND:
  3636. {
  3637. RECT rc;
  3638. GetClientRect(ped->hwnd, &rc);
  3639. #ifdef _USE_DRAW_THEME_TEXT_
  3640. if (!ped->hTheme)
  3641. #endif // _USE_DRAW_THEME_TEXT_
  3642. {
  3643. HBRUSH hbr = NULL;
  3644. BOOL fNeedDelete = FALSE;
  3645. hbr = Edit_GetBrush(ped, (HDC)wParam, &fNeedDelete);
  3646. if (hbr)
  3647. {
  3648. FillRect((HDC)wParam, &rc, hbr);
  3649. if (fNeedDelete)
  3650. {
  3651. DeleteObject(hbr);
  3652. }
  3653. }
  3654. }
  3655. #ifdef _USE_DRAW_THEME_TEXT_
  3656. else
  3657. {
  3658. HRESULT hr;
  3659. INT iStateId = Edit_GetStateId(ped);
  3660. hr = DrawThemeBackground(ped->hTheme, (HDC)wParam, EP_EDITTEXT, iStateId, &rc, 0);
  3661. }
  3662. #endif // _USE_DRAW_THEME_TEXT_
  3663. return TRUE;
  3664. }
  3665. case WM_GETDLGCODE:
  3666. {
  3667. LONG code = DLGC_WANTCHARS | DLGC_HASSETSEL | DLGC_WANTARROWS | DLGC_WANTALLKEYS;
  3668. //
  3669. // !!! JEFFBOG HACK !!!
  3670. // Only set Dialog Box Flag if GETDLGCODE message is generated by
  3671. // IsDialogMessage -- if so, the lParam will be a pointer to the
  3672. // message structure passed to IsDialogMessage; otherwise, lParam
  3673. // will be NULL. Reason for the HACK alert: the wParam & lParam
  3674. // for GETDLGCODE is still not clearly defined and may end up
  3675. // changing in a way that would throw this off
  3676. //
  3677. if (lParam)
  3678. {
  3679. // Mark ML edit ctrl as in a dialog box
  3680. ped->fInDialogBox = TRUE;
  3681. }
  3682. //
  3683. // If this is a WM_SYSCHAR message generated by the UNDO keystroke
  3684. // we want this message so we can EAT IT in "case WM_SYSCHAR:"
  3685. //
  3686. if (lParam && (((LPMSG)lParam)->message == WM_SYSCHAR) &&
  3687. ((DWORD)((LPMSG)lParam)->lParam & SYS_ALTERNATE) &&
  3688. ((WORD)wParam == VK_BACK))
  3689. {
  3690. code |= DLGC_WANTMESSAGE;
  3691. }
  3692. return code;
  3693. }
  3694. case EM_SCROLL:
  3695. message = WM_VSCROLL;
  3696. //
  3697. // FALL THROUGH
  3698. //
  3699. case WM_HSCROLL:
  3700. case WM_VSCROLL:
  3701. return EditML_Scroll(ped, (message==WM_VSCROLL), LOWORD(wParam), HIWORD(wParam), TRUE);
  3702. case WM_MOUSEWHEEL:
  3703. {
  3704. UINT ucWheelScrollLines;
  3705. //
  3706. // Don't handle zoom and datazoom.
  3707. //
  3708. if (wParam & (MK_SHIFT | MK_CONTROL))
  3709. {
  3710. goto PassToDefaultWindowProc;
  3711. }
  3712. scWheelDelta -= (short) HIWORD(wParam);
  3713. windowstyle = GET_STYLE(ped);
  3714. SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, (LPVOID)&ucWheelScrollLines, 0);
  3715. if ( abs(scWheelDelta) >= WHEEL_DELTA &&
  3716. ucWheelScrollLines > 0 &&
  3717. (windowstyle & (WS_VSCROLL | WS_HSCROLL)))
  3718. {
  3719. int cLineScroll;
  3720. BOOL fVert;
  3721. int cPage;
  3722. if (windowstyle & WS_VSCROLL)
  3723. {
  3724. fVert = TRUE;
  3725. cPage = ped->ichLinesOnScreen;
  3726. }
  3727. else
  3728. {
  3729. fVert = FALSE;
  3730. cPage = (ped->rcFmt.right - ped->rcFmt.left) / ped->aveCharWidth;
  3731. }
  3732. //
  3733. // Limit a roll of one (1) WHEEL_DELTA to scroll one (1) page.
  3734. //
  3735. cLineScroll = (int) min(
  3736. (UINT) (max(1, (cPage - 1))),
  3737. ucWheelScrollLines);
  3738. cLineScroll *= (scWheelDelta / WHEEL_DELTA);
  3739. UserAssert(cLineScroll != 0);
  3740. scWheelDelta = scWheelDelta % WHEEL_DELTA;
  3741. EditML_Scroll(ped, fVert, EM_LINESCROLL, cLineScroll, TRUE);
  3742. }
  3743. break;
  3744. }
  3745. case WM_KEYDOWN:
  3746. //
  3747. // wParam - virt keycode of the given key
  3748. // lParam - modifiers such as repeat count etc. (not used)
  3749. //
  3750. EditML_KeyDown(ped, (UINT)wParam, 0);
  3751. break;
  3752. case WM_KILLFOCUS:
  3753. //
  3754. // wParam - handle of the window that receives the input focus
  3755. // lParam - not used
  3756. //
  3757. scWheelDelta = 0;
  3758. EditML_KillFocus(ped);
  3759. break;
  3760. case WM_CAPTURECHANGED:
  3761. //
  3762. // wParam -- unused
  3763. // lParam -- hwnd of window gaining capture.
  3764. //
  3765. if (ped->fMouseDown)
  3766. {
  3767. //
  3768. // We don't change the caret pos here. If this is happening
  3769. // due to button up, then we'll change the pos in the
  3770. // handler after ReleaseCapture(). Otherwise, just end
  3771. // gracefully because someone else has stolen capture out
  3772. // from under us.
  3773. //
  3774. ped->fMouseDown = FALSE;
  3775. KillTimer(ped->hwnd, IDSYS_SCROLL);
  3776. }
  3777. break;
  3778. case WM_SYSTIMER:
  3779. //
  3780. // This allows us to automatically scroll if the user holds the mouse
  3781. // outside the edit control window. We simulate mouse moves at timer
  3782. // intervals set in MouseMotionHandler.
  3783. //
  3784. if (ped->fMouseDown)
  3785. {
  3786. EditML_MouseMotion(ped, WM_MOUSEMOVE, ped->prevKeys, &ped->ptPrevMouse);
  3787. }
  3788. break;
  3789. case WM_MBUTTONDOWN:
  3790. EnterReaderMode(ped->hwnd);
  3791. break;
  3792. case WM_MOUSEMOVE:
  3793. UserAssert(ped->fMouseDown);
  3794. //
  3795. // FALL THROUGH
  3796. //
  3797. case WM_LBUTTONDBLCLK:
  3798. case WM_LBUTTONDOWN:
  3799. case WM_LBUTTONUP:
  3800. //
  3801. // wParam - contains a value that indicates which virtual keys are down
  3802. // lParam - contains x and y coords of the mouse cursor
  3803. //
  3804. POINTSTOPOINT(pt, lParam);
  3805. EditML_MouseMotion(ped, message, (UINT)wParam, &pt);
  3806. break;
  3807. case WM_CREATE:
  3808. //
  3809. // wParam - handle to window being created
  3810. // lParam - points to a CREATESTRUCT that contains copies of parameters
  3811. // passed to the CreateWindow function.
  3812. //
  3813. return EditML_Create(ped, (LPCREATESTRUCT)lParam);
  3814. case WM_PRINTCLIENT:
  3815. EditML_Paint(ped, (HDC) wParam, NULL);
  3816. break;
  3817. case WM_PAINT:
  3818. //
  3819. // wParam - can be hdc from subclassed paint
  3820. // lParam - not used
  3821. //
  3822. if (wParam)
  3823. {
  3824. hdc = (HDC)wParam;
  3825. lprc = NULL;
  3826. }
  3827. else
  3828. {
  3829. hdc = BeginPaint(ped->hwnd, &ps);
  3830. lprc = &ps.rcPaint;
  3831. }
  3832. if (IsWindowVisible(ped->hwnd))
  3833. {
  3834. EditML_Paint(ped, hdc, lprc);
  3835. }
  3836. if (!wParam)
  3837. {
  3838. EndPaint(ped->hwnd, &ps);
  3839. }
  3840. break;
  3841. case WM_PASTE:
  3842. //
  3843. // wParam - not used
  3844. // lParam - not used
  3845. //
  3846. if (!ped->fReadOnly)
  3847. {
  3848. EditML_PasteText(ped);
  3849. }
  3850. break;
  3851. case WM_SETFOCUS:
  3852. //
  3853. // wParam - handle of window that loses the input focus (may be NULL)
  3854. // lParam - not used
  3855. //
  3856. EditML_SetFocus(ped);
  3857. break;
  3858. case WM_SIZE:
  3859. //
  3860. // wParam - defines the type of resizing fullscreen, sizeiconic,
  3861. // sizenormal etc.
  3862. // lParam - new width in LOWORD, new height in HIGHWORD of client area
  3863. //
  3864. Edit_Size(ped, NULL, TRUE);
  3865. break;
  3866. case EM_FMTLINES:
  3867. //
  3868. // wParam - indicates disposition of end-of-line chars. If non
  3869. // zero, the chars CR CR LF are placed at the end of a word
  3870. // wrapped line. If wParam is zero, the end of line chars are
  3871. // removed. This is only done when the user gets a handle (via
  3872. // EM_GETHANDLE) to the text. lParam - not used.
  3873. //
  3874. if (wParam)
  3875. {
  3876. EditML_InsertCrCrLf(ped);
  3877. }
  3878. else
  3879. {
  3880. EditML_StripCrCrLf(ped);
  3881. }
  3882. EditML_BuildchLines(ped, 0, 0, FALSE, NULL, NULL);
  3883. return (LONG)(wParam != 0);
  3884. case EM_GETHANDLE:
  3885. //
  3886. // wParam - not used
  3887. // lParam - not used
  3888. //
  3889. //
  3890. // Returns a handle to the edit control's text.
  3891. //
  3892. //
  3893. // Null terminate the string. Note that we are guaranteed to have the
  3894. // memory for the NULL since Edit_InsertText allocates an extra
  3895. // WCHAR for the NULL terminator.
  3896. //
  3897. if (ped->fAnsi)
  3898. {
  3899. *(Edit_Lock(ped) + ped->cch) = 0;
  3900. }
  3901. else
  3902. {
  3903. *((LPWSTR)Edit_Lock(ped) + ped->cch) = 0;
  3904. }
  3905. Edit_Unlock(ped);
  3906. return ((LRESULT)ped->hText);
  3907. case EM_GETLINE:
  3908. //
  3909. // wParam - line number to copy (0 is first line)
  3910. // lParam - buffer to copy text to. First WORD is max # of bytes to
  3911. // copy
  3912. //
  3913. return EditML_GetLine(ped, (ICH)wParam, (ICH)*(WORD UNALIGNED *)lParam, (LPSTR)lParam);
  3914. case EM_LINEFROMCHAR:
  3915. //
  3916. // wParam - Contains the index value for the desired char in the text
  3917. // of the edit control. These are 0 based.
  3918. // lParam - not used
  3919. //
  3920. return (LRESULT)EditML_IchToLine(ped, (ICH)wParam);
  3921. case EM_LINEINDEX:
  3922. //
  3923. // wParam - specifies the desired line number where the number of the
  3924. // first line is 0. If linenumber = 0, the line with the caret is used.
  3925. // lParam - not used.
  3926. // This function return s the number of character positions that occur
  3927. // preceeding the first char in a given line.
  3928. //
  3929. return (LRESULT)EditML_LineIndex(ped, (ICH)wParam);
  3930. case EM_LINELENGTH:
  3931. //
  3932. // wParam - specifies the character index of a character in the
  3933. // specified line, where the first line is 0. If -1, the length
  3934. // of the current line (with the caret) is return ed not including the
  3935. // length of any selected text.
  3936. // lParam - not used
  3937. //
  3938. return (LRESULT)EditML_LineLength(ped, (ICH)wParam);
  3939. case EM_LINESCROLL:
  3940. //
  3941. // wParam - not used
  3942. // lParam - Contains the number of lines and char positions to scroll
  3943. //
  3944. EditML_Scroll(ped, TRUE, EM_LINESCROLL, (INT)lParam, TRUE);
  3945. EditML_Scroll(ped, FALSE, EM_LINESCROLL, (INT)wParam, TRUE);
  3946. break;
  3947. case EM_REPLACESEL:
  3948. //
  3949. // wParam - flag for 4.0+ apps saying whether to clear undo
  3950. // lParam - Points to a null terminated replacement text.
  3951. //
  3952. EditML_ReplaceSel(ped, (LPSTR)lParam);
  3953. if (!ped->f40Compat || !wParam)
  3954. {
  3955. Edit_EmptyUndo(Pundo(ped));
  3956. }
  3957. break;
  3958. case EM_SETHANDLE:
  3959. //
  3960. // wParam - contains a handle to the text buffer
  3961. // lParam - not used
  3962. //
  3963. EditML_SetHandle(ped, (HANDLE)wParam);
  3964. break;
  3965. case EM_SETRECT:
  3966. case EM_SETRECTNP:
  3967. //
  3968. // wParamLo -- not used
  3969. // lParam -- LPRECT with new formatting area
  3970. //
  3971. Edit_Size(ped, (LPRECT) lParam, (message != EM_SETRECTNP));
  3972. break;
  3973. case EM_SETSEL:
  3974. //
  3975. // wParam - Under 3.1, specifies if we should scroll caret into
  3976. // view or not. 0 == scroll into view. 1 == don't scroll
  3977. // lParam - starting pos in lowword ending pos in high word
  3978. //
  3979. // Under Win32, wParam is the starting pos, lParam is the
  3980. // ending pos, and the caret is not scrolled into view.
  3981. // The message EM_SCROLLCARET forces the caret to be scrolled
  3982. // into view.
  3983. //
  3984. EditML_SetSelection(ped, TRUE, (ICH)wParam, (ICH)lParam);
  3985. break;
  3986. case EM_SCROLLCARET:
  3987. //
  3988. // Scroll caret into view
  3989. //
  3990. EditML_EnsureCaretVisible(ped);
  3991. break;
  3992. case EM_GETFIRSTVISIBLELINE:
  3993. //
  3994. // Returns the first visible line for multiline edit controls.
  3995. //
  3996. return (LONG)ped->ichScreenStart;
  3997. case WM_SYSKEYDOWN:
  3998. if (((WORD)wParam == VK_BACK) && ((DWORD)lParam & SYS_ALTERNATE))
  3999. {
  4000. SendMessage(ped->hwnd, EM_UNDO, 0, 0L);
  4001. break;
  4002. }
  4003. goto PassToDefaultWindowProc;
  4004. case WM_UNDO:
  4005. case EM_UNDO:
  4006. return EditML_Undo(ped);
  4007. case EM_SETTABSTOPS:
  4008. //
  4009. // This sets the tab stop positions for multiline edit controls.
  4010. // wParam - Number of tab stops
  4011. // lParam - Far ptr to a UINT array containing the Tab stop positions
  4012. //
  4013. return EditML_SetTabStops(ped, (int)wParam, (LPINT)lParam);
  4014. case EM_POSFROMCHAR:
  4015. //
  4016. // wParam -- char index in text
  4017. // lParam -- not used
  4018. // This function returns the (x,y) position of the character
  4019. //
  4020. case EM_CHARFROMPOS:
  4021. //
  4022. // wParam -- unused
  4023. // lParam -- pt in client coordinates
  4024. // This function returns
  4025. // LOWORD: the position of the closest character
  4026. // to the passed in point. Beware of
  4027. // points not actually in the edit client...
  4028. // HIWORD: the index of the line the char is on
  4029. //
  4030. {
  4031. LONG xyPos;
  4032. LONG line;
  4033. hdc = Edit_GetDC(ped, TRUE);
  4034. if (message == EM_POSFROMCHAR)
  4035. {
  4036. EditML_IchToXYPos(ped, hdc, (ICH)wParam, FALSE, &pt);
  4037. xyPos = MAKELONG(pt.x, pt.y);
  4038. }
  4039. else
  4040. {
  4041. POINTSTOPOINT(pt, lParam);
  4042. xyPos = EditML_MouseToIch(ped, hdc, &pt, &line);
  4043. xyPos = MAKELONG(xyPos, line);
  4044. }
  4045. Edit_ReleaseDC(ped, hdc, TRUE);
  4046. return (LRESULT)xyPos;
  4047. }
  4048. case WM_SETREDRAW:
  4049. DefWindowProc(ped->hwnd, message, wParam, lParam);
  4050. if (wParam)
  4051. {
  4052. //
  4053. // Backwards compatability hack needed so that winraid's edit
  4054. // controls work fine.
  4055. //
  4056. RedrawWindow(ped->hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME);
  4057. }
  4058. break;
  4059. default:
  4060. PassToDefaultWindowProc:
  4061. return DefWindowProc(ped->hwnd, message, wParam, lParam);
  4062. }
  4063. return TRUE;
  4064. }
  4065. //---------------------------------------------------------------------------//
  4066. //
  4067. // EditML_DrawText AorW
  4068. //
  4069. // This function draws all the characters between ichStart and ichEnd for
  4070. // the given Multiline Edit Control.
  4071. //
  4072. // This function divides the block of text between ichStart and ichEnd
  4073. // into lines and each line into strips of text based on the selection
  4074. // attributes. It calls Edit_TabTheTextOut() to draw each strip.
  4075. // This takes care of the Negative A anc C widths of the current font, if
  4076. // it has any, on either side of each strip of text.
  4077. //
  4078. // NOTE: If the language pack is loaded the text is not divided into strips,
  4079. // nor is selection highlighting performed here. Whole lines are passed
  4080. // to the language pack to display with tab expansion and selection
  4081. // highlighting. (Since the language pack supports scripts with complex
  4082. // character re-ordering rules, only it can do this).
  4083. //
  4084. VOID EditML_DrawText(PED ped, HDC hdc, ICH ichStart, ICH ichEnd, BOOL fSelChange)
  4085. {
  4086. DWORD textColorSave;
  4087. DWORD bkColorSave;
  4088. PSTR pText;
  4089. UINT wCurLine;
  4090. UINT wEndLine;
  4091. INT xOffset;
  4092. ICH LengthToDraw;
  4093. ICH CurStripLength;
  4094. ICH ichAttrib, ichNewStart;
  4095. ICH ExtraLengthForNegA;
  4096. ICH ichT;
  4097. INT iRemainingLengthInLine;
  4098. INT xStPos, xClipStPos, xClipEndPos, yPos;
  4099. BOOL fFirstLineOfBlock = TRUE;
  4100. BOOL fDrawEndOfLineStrip = FALSE;
  4101. BOOL fDrawOnSameLine = FALSE;
  4102. BOOL fSelected = FALSE;
  4103. BOOL fLineBegins = FALSE;
  4104. STRIPINFO NegCInfo;
  4105. POINT pt;
  4106. HBRUSH hbr = NULL;
  4107. BOOL fNeedDelete = FALSE;
  4108. //
  4109. // Just return if nothing to draw
  4110. //
  4111. if (!ped->ichLinesOnScreen)
  4112. {
  4113. return;
  4114. }
  4115. hbr = Edit_GetBrush(ped, hdc, &fNeedDelete);
  4116. if (hbr && fNeedDelete)
  4117. {
  4118. DeleteObject(hbr);
  4119. }
  4120. //
  4121. // Adjust the value of ichStart such that we need to draw only those lines
  4122. // visible on the screen.
  4123. //
  4124. if ((UINT)ichStart < (UINT)ped->chLines[ped->ichScreenStart])
  4125. {
  4126. ichStart = ped->chLines[ped->ichScreenStart];
  4127. if (ichStart > ichEnd)
  4128. {
  4129. return;
  4130. }
  4131. }
  4132. //
  4133. // Adjust the value of ichEnd such that we need to draw only those lines
  4134. // visible on the screen.
  4135. //
  4136. wCurLine = min(ped->ichScreenStart+ped->ichLinesOnScreen,ped->cLines-1);
  4137. ichT = ped->chLines[wCurLine] + EditML_Line(ped, wCurLine);
  4138. ichEnd = min(ichEnd, ichT);
  4139. wCurLine = EditML_IchToLine(ped, ichStart); // Starting line.
  4140. wEndLine = EditML_IchToLine(ped, ichEnd); // Ending line.
  4141. UserAssert(ped->chLines[wCurLine] <= ped->cch + 1);
  4142. UserAssert(ped->chLines[wEndLine] <= ped->cch + 1);
  4143. if (fSelChange && (GetBkMode(hdc) != OPAQUE))
  4144. {
  4145. //
  4146. // if changing selection on a transparent edit control, just
  4147. // draw those lines from scratch
  4148. //
  4149. RECT rcStrip;
  4150. CopyRect(&rcStrip, &ped->rcFmt);
  4151. rcStrip.left -= ped->wLeftMargin;
  4152. if (ped->pLpkEditCallout)
  4153. {
  4154. rcStrip.right += ped->wRightMargin;
  4155. }
  4156. rcStrip.top += (wCurLine - ped->ichScreenStart) * ped->lineHeight;
  4157. rcStrip.bottom = rcStrip.top + ((wEndLine - wCurLine) + 1) * ped->lineHeight;
  4158. InvalidateRect(ped->hwnd, &rcStrip, TRUE);
  4159. return;
  4160. }
  4161. //
  4162. // If it is either centered or right-justified, then draw the whole lines.
  4163. // Also draw whole lines if the language pack is handling line layout.
  4164. //
  4165. if ((ped->format != ES_LEFT) || (ped->pLpkEditCallout))
  4166. {
  4167. ichStart = ped->chLines[wCurLine];
  4168. ichEnd = ped->chLines[wEndLine] + EditML_Line(ped, wEndLine);
  4169. }
  4170. pText = Edit_Lock(ped);
  4171. HideCaret(ped->hwnd);
  4172. //
  4173. // If ichStart stays on Second byte of DBCS, we have to
  4174. // adjust it. LiZ -- 5/5/93
  4175. //
  4176. if (ped->fAnsi && ped->fDBCS)
  4177. {
  4178. ichStart = Edit_AdjustIch( ped, pText, ichStart );
  4179. }
  4180. UserAssert(ichStart <= ped->cch);
  4181. UserAssert(ichEnd <= ped->cch);
  4182. while (ichStart <= ichEnd)
  4183. {
  4184. //
  4185. // Pass whole lines to the language pack to display with selection
  4186. // marking and tab expansion.
  4187. //
  4188. if (ped->pLpkEditCallout)
  4189. {
  4190. ped->pLpkEditCallout->EditDrawText(
  4191. (PED0)ped, hdc, pText + ped->cbChar*ichStart,
  4192. EditML_Line(ped, wCurLine),
  4193. (INT)ped->ichMinSel - (INT)ichStart, (INT)ped->ichMaxSel - (INT)ichStart,
  4194. EditML_IchToYPos(ped, ichStart, FALSE));
  4195. }
  4196. else
  4197. {
  4198. //
  4199. // xStPos: The starting Position where the string must be drawn.
  4200. // xClipStPos: The starting position for the clipping rect for the block.
  4201. // xClipEndPos: The ending position for the clipping rect for the block.
  4202. //
  4203. //
  4204. // Calculate the xyPos of starting point of the block.
  4205. //
  4206. EditML_IchToXYPos(ped, hdc, ichStart, FALSE, &pt);
  4207. xClipStPos = xStPos = pt.x;
  4208. yPos = pt.y;
  4209. //
  4210. // The attributes of the block is the same as that of ichStart.
  4211. //
  4212. ichAttrib = ichStart;
  4213. //
  4214. // If the current font has some negative C widths and if this is the
  4215. // begining of a block, we must start drawing some characters before the
  4216. // block to account for the negative C widths of the strip before the
  4217. // current strip; In this case, reset ichStart and xStPos.
  4218. //
  4219. if (fFirstLineOfBlock && ped->wMaxNegC)
  4220. {
  4221. fFirstLineOfBlock = FALSE;
  4222. ichNewStart = max(((int)(ichStart - ped->wMaxNegCcharPos)), ((int)ped->chLines[wCurLine]));
  4223. //
  4224. // If ichStart needs to be changed, then change xStPos also accordingly.
  4225. //
  4226. if (ichNewStart != ichStart)
  4227. {
  4228. if (ped->fAnsi && ped->fDBCS)
  4229. {
  4230. //
  4231. // Adjust DBCS alignment...
  4232. //
  4233. ichNewStart = Edit_AdjustIchNext( ped, pText, ichNewStart );
  4234. }
  4235. EditML_IchToXYPos(ped, hdc, ichStart = ichNewStart, FALSE, &pt);
  4236. xStPos = pt.x;
  4237. }
  4238. }
  4239. //
  4240. // Calc the number of characters remaining to be drawn in the current line.
  4241. //
  4242. iRemainingLengthInLine = EditML_Line(ped, wCurLine) -
  4243. (ichStart - ped->chLines[wCurLine]);
  4244. //
  4245. // If this is the last line of a block, we may not have to draw all the
  4246. // remaining lines; We must draw only upto ichEnd.
  4247. //
  4248. if (wCurLine == wEndLine)
  4249. {
  4250. LengthToDraw = ichEnd - ichStart;
  4251. }
  4252. else
  4253. {
  4254. LengthToDraw = iRemainingLengthInLine;
  4255. }
  4256. //
  4257. // Find out how many pixels we indent the line for non-left-justified
  4258. // formats
  4259. //
  4260. if (ped->format != ES_LEFT)
  4261. {
  4262. xOffset = EditML_CalcXOffset(ped, hdc, wCurLine);
  4263. }
  4264. else
  4265. {
  4266. xOffset = -((int)(ped->xOffset));
  4267. }
  4268. //
  4269. // Check if this is the begining of a line.
  4270. //
  4271. if (ichAttrib == ped->chLines[wCurLine])
  4272. {
  4273. fLineBegins = TRUE;
  4274. xClipStPos = ped->rcFmt.left - ped->wLeftMargin;
  4275. }
  4276. //
  4277. // The following loop divides this 'wCurLine' into strips based on the
  4278. // selection attributes and draw them strip by strip.
  4279. //
  4280. do
  4281. {
  4282. //
  4283. // If ichStart is pointing at CRLF or CRCRLF, then iRemainingLength
  4284. // could have become negative because MLLine does not include
  4285. // CR and LF at the end of a line.
  4286. //
  4287. if (iRemainingLengthInLine < 0) // If Current line is completed,
  4288. {
  4289. break; // go on to the next line.
  4290. }
  4291. //
  4292. // Check if a part of the block is selected and if we need to
  4293. // show it with a different attribute.
  4294. //
  4295. if (!(ped->ichMinSel == ped->ichMaxSel ||
  4296. ichAttrib >= ped->ichMaxSel ||
  4297. ichEnd < ped->ichMinSel ||
  4298. (!ped->fNoHideSel && !ped->fFocus)))
  4299. {
  4300. //
  4301. // OK! There is a selection somewhere in this block!
  4302. // Check if this strip has selection attribute.
  4303. //
  4304. if (ichAttrib < ped->ichMinSel)
  4305. {
  4306. fSelected = FALSE; // This strip is not selected
  4307. // Calculate the length of this strip with normal attribute.
  4308. CurStripLength = min(ichStart+LengthToDraw, ped->ichMinSel)-ichStart;
  4309. fLineBegins = FALSE;
  4310. }
  4311. else
  4312. {
  4313. //
  4314. // The current strip has the selection attribute.
  4315. //
  4316. if (fLineBegins) // Is it the first part of a line?
  4317. {
  4318. //
  4319. // Then, draw the left margin area with normal attribute.
  4320. //
  4321. fSelected = FALSE;
  4322. CurStripLength = 0;
  4323. xClipStPos = ped->rcFmt.left - ped->wLeftMargin;
  4324. fLineBegins = FALSE;
  4325. }
  4326. else
  4327. {
  4328. //
  4329. // Else, draw the strip with selection attribute.
  4330. //
  4331. fSelected = TRUE;
  4332. CurStripLength = min(ichStart+LengthToDraw, ped->ichMaxSel)-ichStart;
  4333. //
  4334. // Select in the highlight colors.
  4335. //
  4336. bkColorSave = SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
  4337. if (!ped->fDisabled)
  4338. {
  4339. textColorSave = SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
  4340. }
  4341. }
  4342. }
  4343. }
  4344. else
  4345. {
  4346. //
  4347. // The whole strip has no selection attributes.
  4348. //
  4349. CurStripLength = LengthToDraw;
  4350. }
  4351. //
  4352. // Other than the current strip, do we still have anything
  4353. // left to be drawn in the current line?
  4354. //
  4355. fDrawOnSameLine = (LengthToDraw != CurStripLength);
  4356. //
  4357. // When we draw this strip, we need to draw some more characters
  4358. // beyond the end of this strip to account for the negative A
  4359. // widths of the characters that follow this strip.
  4360. //
  4361. ExtraLengthForNegA = min(iRemainingLengthInLine-CurStripLength, ped->wMaxNegAcharPos);
  4362. //
  4363. // The blank strip at the end of the line needs to be drawn with
  4364. // normal attribute irrespective of whether the line has selection
  4365. // attribute or not. Hence, if the last strip of the line has selection
  4366. // attribute, then this blank strip needs to be drawn separately.
  4367. // Else, we can draw the blank strip along with the last strip.
  4368. //
  4369. //
  4370. // Is this the last strip of the current line?
  4371. //
  4372. if (iRemainingLengthInLine == (int)CurStripLength)
  4373. {
  4374. if (fSelected) // Does this strip have selection attribute?
  4375. {
  4376. //
  4377. // Then we need to draw the end of line strip separately.
  4378. //
  4379. fDrawEndOfLineStrip = TRUE; // Draw the end of line strip.
  4380. EditML_IchToXYPos(ped, hdc, ichStart+CurStripLength, TRUE, &pt);
  4381. xClipEndPos = pt.x;
  4382. }
  4383. else
  4384. {
  4385. //
  4386. // Set the xClipEndPos to a big value sothat the blank
  4387. // strip will be drawn automatically when the last strip
  4388. // is drawn.
  4389. //
  4390. xClipEndPos = MAXCLIPENDPOS;
  4391. }
  4392. }
  4393. else
  4394. {
  4395. //
  4396. // This is not the last strip of this line; So, set the ending
  4397. // clip position accurately.
  4398. //
  4399. EditML_IchToXYPos(ped, hdc, ichStart+CurStripLength, FALSE, &pt);
  4400. xClipEndPos = pt.x;
  4401. }
  4402. //
  4403. // Draw the current strip starting from xStPos, clipped to the area
  4404. // between xClipStPos and xClipEndPos. Obtain "NegCInfo" and use it
  4405. // in drawing the next strip.
  4406. //
  4407. Edit_TabTheTextOut(hdc, xClipStPos, xClipEndPos,
  4408. xStPos, yPos, (LPSTR)(pText+ichStart*ped->cbChar),
  4409. CurStripLength+ExtraLengthForNegA, ichStart, ped,
  4410. ped->rcFmt.left+xOffset, fSelected ? ECT_SELECTED : ECT_NORMAL, &NegCInfo);
  4411. if (fSelected)
  4412. {
  4413. //
  4414. // If this strip was selected, then the next strip won't have
  4415. // selection attribute
  4416. //
  4417. fSelected = FALSE;
  4418. SetBkColor(hdc, bkColorSave);
  4419. if (!ped->fDisabled)
  4420. {
  4421. SetTextColor(hdc, textColorSave);
  4422. }
  4423. }
  4424. //
  4425. // Do we have one more strip to draw on the current line?
  4426. //
  4427. if (fDrawOnSameLine || fDrawEndOfLineStrip)
  4428. {
  4429. int iLastDrawnLength;
  4430. //
  4431. // Next strip's attribute is decided based on the char at ichAttrib
  4432. //
  4433. ichAttrib = ichStart + CurStripLength;
  4434. //
  4435. // When drawing the next strip, start at a few chars before
  4436. // the actual start to account for the Neg 'C' of the strip
  4437. // just drawn.
  4438. //
  4439. iLastDrawnLength = CurStripLength +ExtraLengthForNegA - NegCInfo.nCount;
  4440. //
  4441. // Adjust DBCS alignment...
  4442. //
  4443. if (ped->fAnsi && ped->fDBCS)
  4444. {
  4445. ichNewStart = Edit_AdjustIch(ped,pText,ichStart+iLastDrawnLength);
  4446. iLastDrawnLength = ichNewStart - ichStart;
  4447. ichStart = ichNewStart;
  4448. }
  4449. else
  4450. {
  4451. ichStart += iLastDrawnLength;
  4452. }
  4453. LengthToDraw -= iLastDrawnLength;
  4454. iRemainingLengthInLine -= iLastDrawnLength;
  4455. //
  4456. // The start of clip rect for the next strip.
  4457. //
  4458. xStPos = NegCInfo.XStartPos;
  4459. xClipStPos = xClipEndPos;
  4460. }
  4461. //
  4462. // Draw the blank strip at the end of line seperately, if required.
  4463. //
  4464. if (fDrawEndOfLineStrip)
  4465. {
  4466. Edit_TabTheTextOut(hdc, xClipStPos, MAXCLIPENDPOS, xStPos, yPos,
  4467. (LPSTR)(pText+ichStart*ped->cbChar), LengthToDraw, ichStart,
  4468. ped, ped->rcFmt.left+xOffset, ECT_NORMAL, &NegCInfo);
  4469. fDrawEndOfLineStrip = FALSE;
  4470. }
  4471. }
  4472. while(fDrawOnSameLine); // do while loop ends here.
  4473. }
  4474. // Let us move on to the next line of this block to be drawn.
  4475. wCurLine++;
  4476. if (ped->cLines > wCurLine)
  4477. {
  4478. ichStart = ped->chLines[wCurLine];
  4479. }
  4480. else
  4481. {
  4482. ichStart = ichEnd+1; // We have reached the end of the text.
  4483. }
  4484. } // while loop ends here
  4485. Edit_Unlock(ped);
  4486. ShowCaret(ped->hwnd);
  4487. EditML_SetCaretPosition(ped, hdc);
  4488. }
  4489. //---------------------------------------------------------------------------//
  4490. //
  4491. // Multi-Line Support Routines called Rarely
  4492. //---------------------------------------------------------------------------//
  4493. //
  4494. // EditML_InsertCrCrLf AorW
  4495. //
  4496. // Inserts CR CR LF characters into the text at soft (word-wrap) line
  4497. // breaks. CR LF (hard) line breaks are unaffected. Assumes that the text
  4498. // has already been formatted ie. ped->chLines is where we want the line
  4499. // breaks to occur. Note that ped->chLines is not updated to reflect the
  4500. // movement of text by the addition of CR CR LFs. Returns TRUE if successful
  4501. // else notify parent and return FALSE if the memory couldn't be allocated.
  4502. //
  4503. BOOL EditML_InsertCrCrLf(PED ped)
  4504. {
  4505. ICH dch;
  4506. ICH li;
  4507. ICH lineSize;
  4508. unsigned char *pchText;
  4509. unsigned char *pchTextNew;
  4510. if (!ped->fWrap || !ped->cch)
  4511. {
  4512. //
  4513. // There are no soft line breaks if word-wrapping is off or if no chars
  4514. //
  4515. return TRUE;
  4516. }
  4517. //
  4518. // Calc an upper bound on the number of additional characters we will be
  4519. // adding to the text when we insert CR CR LFs.
  4520. //
  4521. dch = 3 * ped->cLines;
  4522. if (!LocalReAlloc(ped->hText, (ped->cch + dch) * ped->cbChar, 0))
  4523. {
  4524. Edit_NotifyParent(ped, EN_ERRSPACE);
  4525. return FALSE;
  4526. }
  4527. ped->cchAlloc = ped->cch + dch;
  4528. //
  4529. // Move the text up dch bytes and then copy it back down, inserting the CR
  4530. // CR LF's as necessary.
  4531. //
  4532. pchTextNew = pchText = Edit_Lock(ped);
  4533. pchText += dch * ped->cbChar;
  4534. //
  4535. // We will use dch to keep track of how many chars we add to the text
  4536. //
  4537. dch = 0;
  4538. //
  4539. // Copy the text up dch bytes to pchText. This will shift all indices in
  4540. // ped->chLines up by dch bytes.
  4541. //
  4542. memmove(pchText, pchTextNew, ped->cch * ped->cbChar);
  4543. //
  4544. // Now copy chars from pchText down to pchTextNew and insert CRCRLF at soft
  4545. // line breaks.
  4546. //
  4547. if (ped->fAnsi)
  4548. {
  4549. for (li = 0; li < ped->cLines - 1; li++)
  4550. {
  4551. lineSize = ped->chLines[li + 1] - ped->chLines[li];
  4552. memmove(pchTextNew, pchText, lineSize);
  4553. pchTextNew += lineSize;
  4554. pchText += lineSize;
  4555. //
  4556. // If last character in newly copied line is not a line feed, then we
  4557. // need to add the CR CR LF triple to the end
  4558. //
  4559. if (*(pchTextNew - 1) != 0x0A)
  4560. {
  4561. *pchTextNew++ = 0x0D;
  4562. *pchTextNew++ = 0x0D;
  4563. *pchTextNew++ = 0x0A;
  4564. dch += 3;
  4565. }
  4566. }
  4567. //
  4568. // Now move the last line up. It won't have any line breaks in it...
  4569. //
  4570. memmove(pchTextNew, pchText, ped->cch - ped->chLines[ped->cLines - 1]);
  4571. }
  4572. else
  4573. {
  4574. LPWSTR pwchTextNew = (LPWSTR)pchTextNew;
  4575. for (li = 0; li < ped->cLines - 1; li++)
  4576. {
  4577. lineSize = ped->chLines[li + 1] - ped->chLines[li];
  4578. memmove(pwchTextNew, pchText, lineSize * sizeof(WCHAR));
  4579. pwchTextNew += lineSize;
  4580. pchText += lineSize * sizeof(WCHAR);
  4581. //
  4582. // If last character in newly copied line is not a line feed, then we
  4583. // need to add the CR CR LF triple to the end
  4584. //
  4585. if (*(pwchTextNew - 1) != 0x0A)
  4586. {
  4587. *pwchTextNew++ = 0x0D;
  4588. *pwchTextNew++ = 0x0D;
  4589. *pwchTextNew++ = 0x0A;
  4590. dch += 3;
  4591. }
  4592. }
  4593. //
  4594. // Now move the last line up. It won't have any line breaks in it...
  4595. //
  4596. memmove(pwchTextNew, pchText,
  4597. (ped->cch - ped->chLines[ped->cLines - 1]) * sizeof(WCHAR));
  4598. }
  4599. Edit_Unlock(ped);
  4600. if (dch)
  4601. {
  4602. //
  4603. // Update number of characters in text handle
  4604. //
  4605. ped->cch += dch;
  4606. //
  4607. // So that the next time we do anything with the text, we can strip the
  4608. // CRCRLFs
  4609. //
  4610. ped->fStripCRCRLF = TRUE;
  4611. return TRUE;
  4612. }
  4613. return FALSE;
  4614. }
  4615. //---------------------------------------------------------------------------//
  4616. //
  4617. // EditML_StripCrCrLf AorW
  4618. //
  4619. // Strips the CR CR LF character combination from the text. This
  4620. // shows the soft (word wrapped) line breaks. CR LF (hard) line breaks are
  4621. // unaffected.
  4622. //
  4623. void EditML_StripCrCrLf(PED ped)
  4624. {
  4625. if (ped->cch)
  4626. {
  4627. if (ped->fAnsi)
  4628. {
  4629. unsigned char *pchSrc;
  4630. unsigned char *pchDst;
  4631. unsigned char *pchLast;
  4632. pchSrc = pchDst = Edit_Lock(ped);
  4633. pchLast = pchSrc + ped->cch;
  4634. while (pchSrc < pchLast)
  4635. {
  4636. if ( (pchSrc[0] == 0x0D)
  4637. && (pchSrc[1] == 0x0D)
  4638. && (pchSrc[2] == 0x0A)
  4639. )
  4640. {
  4641. pchSrc += 3;
  4642. ped->cch -= 3;
  4643. }
  4644. else
  4645. {
  4646. *pchDst++ = *pchSrc++;
  4647. }
  4648. }
  4649. }
  4650. else
  4651. {
  4652. LPWSTR pwchSrc;
  4653. LPWSTR pwchDst;
  4654. LPWSTR pwchLast;
  4655. pwchSrc = pwchDst = (LPWSTR)Edit_Lock(ped);
  4656. pwchLast = pwchSrc + ped->cch;
  4657. while (pwchSrc < pwchLast)
  4658. {
  4659. if ( (pwchSrc[0] == 0x0D)
  4660. && (pwchSrc[1] == 0x0D)
  4661. && (pwchSrc[2] == 0x0A)
  4662. )
  4663. {
  4664. pwchSrc += 3;
  4665. ped->cch -= 3;
  4666. }
  4667. else
  4668. {
  4669. *pwchDst++ = *pwchSrc++;
  4670. }
  4671. }
  4672. }
  4673. Edit_Unlock(ped);
  4674. //
  4675. // Make sure we don't have any values past the last character
  4676. //
  4677. if (ped->ichCaret > ped->cch)
  4678. {
  4679. ped->ichCaret = ped->cch;
  4680. }
  4681. if (ped->ichMinSel > ped->cch)
  4682. {
  4683. ped->ichMinSel = ped->cch;
  4684. }
  4685. if (ped->ichMaxSel > ped->cch)
  4686. {
  4687. ped->ichMaxSel = ped->cch;
  4688. }
  4689. }
  4690. }
  4691. //---------------------------------------------------------------------------//
  4692. //
  4693. // EditML_SetHandle AorW
  4694. //
  4695. // Sets the ped to contain the given handle.
  4696. //
  4697. void EditML_SetHandle(PED ped, HANDLE hNewText)
  4698. {
  4699. ICH newCch;
  4700. ped->cch = ped->cchAlloc =
  4701. (ICH)LocalSize(ped->hText = hNewText) / ped->cbChar;
  4702. ped->fEncoded = FALSE;
  4703. if (ped->cch)
  4704. {
  4705. //
  4706. // We have to do it this way in case the app gives us a zero size handle
  4707. //
  4708. if (ped->fAnsi)
  4709. {
  4710. ped->cch = strlen(Edit_Lock(ped));
  4711. }
  4712. else
  4713. {
  4714. ped->cch = wcslen((LPWSTR)Edit_Lock(ped));
  4715. }
  4716. Edit_Unlock(ped);
  4717. }
  4718. newCch = (ICH)(ped->cch + CCHALLOCEXTRA);
  4719. //
  4720. // We do this LocalReAlloc in case the app changed the size of the handle
  4721. //
  4722. if (LocalReAlloc(ped->hText, newCch*ped->cbChar, 0))
  4723. {
  4724. ped->cchAlloc = newCch;
  4725. }
  4726. Edit_ResetTextInfo(ped);
  4727. }
  4728. //---------------------------------------------------------------------------//
  4729. //
  4730. // EditML_GetLine AorW
  4731. //
  4732. // Copies maxCchToCopy bytes of line lineNumber to the buffer
  4733. // lpBuffer. The string is not zero terminated.
  4734. //
  4735. // Returns number of characters copied
  4736. //
  4737. LONG EditML_GetLine(PED ped, ICH lineNumber, ICH maxCchToCopy, LPSTR lpBuffer)
  4738. {
  4739. PSTR pText;
  4740. ICH cchLen;
  4741. if (lineNumber > ped->cLines - 1)
  4742. {
  4743. TraceMsg(TF_STANDARD,
  4744. "Edit: Invalid parameter \"lineNumber\" (%ld) to EditML_GetLine",
  4745. lineNumber);
  4746. return 0L;
  4747. }
  4748. cchLen = EditML_Line(ped, lineNumber);
  4749. maxCchToCopy = min(cchLen, maxCchToCopy);
  4750. if (maxCchToCopy)
  4751. {
  4752. pText = Edit_Lock(ped) +
  4753. ped->chLines[lineNumber] * ped->cbChar;
  4754. memmove(lpBuffer, pText, maxCchToCopy*ped->cbChar);
  4755. Edit_Unlock(ped);
  4756. }
  4757. return maxCchToCopy;
  4758. }
  4759. //---------------------------------------------------------------------------//
  4760. //
  4761. // EditML_LineIndex AorW
  4762. //
  4763. // This function return s the number of character positions that occur
  4764. // preceeding the first char in a given line.
  4765. //
  4766. ICH EditML_LineIndex( PED ped, ICH iLine)
  4767. {
  4768. if (iLine == -1)
  4769. {
  4770. iLine = ped->iCaretLine;
  4771. }
  4772. if (iLine < ped->cLines)
  4773. {
  4774. return ped->chLines[iLine];
  4775. }
  4776. else
  4777. {
  4778. TraceMsg(TF_STANDARD,
  4779. "Edit: Invalid parameter \"iLine\" (%ld) to EditML_LineIndex",
  4780. iLine);
  4781. return (ICH)-1;
  4782. }
  4783. }
  4784. //---------------------------------------------------------------------------//
  4785. //
  4786. // EditML_LineLength AorW
  4787. //
  4788. // if ich = -1, return the length of the lines containing the current
  4789. // selection but not including the selection. Otherwise, return the length of
  4790. // the line containing ich.
  4791. //
  4792. ICH EditML_LineLength(PED ped, ICH ich)
  4793. {
  4794. ICH il1, il2;
  4795. ICH temp;
  4796. if (ich != 0xFFFFFFFF)
  4797. {
  4798. return (EditML_Line(ped, EditML_IchToLine(ped, ich)));
  4799. }
  4800. //
  4801. // Find length of lines corresponding to current selection
  4802. //
  4803. il1 = EditML_IchToLine(ped, ped->ichMinSel);
  4804. il2 = EditML_IchToLine(ped, ped->ichMaxSel);
  4805. if (il1 == il2)
  4806. {
  4807. return (EditML_Line(ped, il1) - (ped->ichMaxSel - ped->ichMinSel));
  4808. }
  4809. temp = ped->ichMinSel - ped->chLines[il1];
  4810. temp += EditML_Line(ped, il2);
  4811. temp -= (ped->ichMaxSel - ped->chLines[il2]);
  4812. return temp;
  4813. }
  4814. //---------------------------------------------------------------------------//
  4815. //
  4816. // EditML_SetSelection AorW
  4817. //
  4818. // Sets the selection to the points given and puts the cursor at
  4819. // ichMaxSel.
  4820. //
  4821. VOID EditML_SetSelection(PED ped, BOOL fDoNotScrollCaret, ICH ichMinSel, ICH ichMaxSel)
  4822. {
  4823. HDC hdc;
  4824. if (ichMinSel == 0xFFFFFFFF)
  4825. {
  4826. //
  4827. // Set no selection if we specify -1
  4828. //
  4829. ichMinSel = ichMaxSel = ped->ichCaret;
  4830. }
  4831. //
  4832. // Since these are unsigned, we don't check if they are greater than 0.
  4833. //
  4834. ichMinSel = min(ped->cch, ichMinSel);
  4835. ichMaxSel = min(ped->cch, ichMaxSel);
  4836. #ifdef FE_SB // EditML_SetSelectionHander()
  4837. //
  4838. // To avoid position to half of DBCS, check and ajust position if necessary
  4839. //
  4840. // We check ped->fDBCS and ped->fAnsi though Edit_AdjustIch checks these bits
  4841. // at first. We're worrying about the overhead of Edit_Lock and Edit_Unlock.
  4842. //
  4843. if ( ped->fDBCS && ped->fAnsi )
  4844. {
  4845. PSTR pText;
  4846. pText = Edit_Lock(ped);
  4847. ichMinSel = Edit_AdjustIch( ped, pText, ichMinSel );
  4848. ichMaxSel = Edit_AdjustIch( ped, pText, ichMaxSel );
  4849. Edit_Unlock(ped);
  4850. }
  4851. #endif // FE_SB
  4852. //
  4853. // Set the caret's position to be at ichMaxSel.
  4854. //
  4855. ped->ichCaret = ichMaxSel;
  4856. ped->iCaretLine = EditML_IchToLine(ped, ped->ichCaret);
  4857. hdc = Edit_GetDC(ped, FALSE);
  4858. EditML_ChangeSelection(ped, hdc, ichMinSel, ichMaxSel);
  4859. EditML_SetCaretPosition(ped, hdc);
  4860. Edit_ReleaseDC(ped, hdc, FALSE);
  4861. #ifdef FE_SB // EditML_SetSelectionHander()
  4862. if (!fDoNotScrollCaret)
  4863. {
  4864. EditML_EnsureCaretVisible(ped);
  4865. }
  4866. //
  4867. // #ifdef KOREA is history, with FE_SB (FarEast Single Binary).
  4868. //
  4869. #else
  4870. #ifdef KOREA
  4871. //
  4872. // Extra parameter specified interim character mode
  4873. //
  4874. EditML_EnsureCaretVisible(ped,NULL);
  4875. #else
  4876. if (!fDoNotScrollCaret)
  4877. {
  4878. EditML_EnsureCaretVisible(ped);
  4879. }
  4880. #endif
  4881. #endif // FE_SB
  4882. }
  4883. //---------------------------------------------------------------------------//
  4884. //
  4885. // EditML_SetTabStops AorW
  4886. //
  4887. // This sets the tab stop positions set by the App by sending
  4888. // a EM_SETTABSTOPS message.
  4889. //
  4890. // nTabPos : Number of tab stops set by the caller
  4891. // lpTabStops: array of tab stop positions in Dialog units.
  4892. //
  4893. // Returns:
  4894. // TRUE if successful
  4895. // FALSE if memory allocation error.
  4896. //
  4897. BOOL EditML_SetTabStops(PED ped, int nTabPos, LPINT lpTabStops)
  4898. {
  4899. int *pTabStops;
  4900. //
  4901. // Check if tab positions already exist
  4902. //
  4903. if (!ped->pTabStops)
  4904. {
  4905. //
  4906. // Check if the caller wants the new tab positions
  4907. //
  4908. if (nTabPos)
  4909. {
  4910. //
  4911. // Allocate the array of tab stops
  4912. //
  4913. pTabStops = (LPINT)UserLocalAlloc(HEAP_ZERO_MEMORY, (nTabPos + 1) * sizeof(int));
  4914. if (!pTabStops)
  4915. {
  4916. return FALSE;
  4917. }
  4918. }
  4919. else
  4920. {
  4921. //
  4922. // No stops then and no stops now!
  4923. //
  4924. return TRUE;
  4925. }
  4926. }
  4927. else
  4928. {
  4929. //
  4930. // Check if the caller wants the new tab positions
  4931. //
  4932. if (nTabPos)
  4933. {
  4934. //
  4935. // Check if the number of tab positions is different
  4936. //
  4937. if (ped->pTabStops[0] != nTabPos)
  4938. {
  4939. //
  4940. // Yes! So ReAlloc to new size
  4941. //
  4942. pTabStops = (LPINT)UserLocalReAlloc(ped->pTabStops, (nTabPos + 1) * sizeof(int), 0);
  4943. if (!pTabStops)
  4944. {
  4945. return FALSE;
  4946. }
  4947. }
  4948. else
  4949. {
  4950. pTabStops = ped->pTabStops;
  4951. }
  4952. }
  4953. else
  4954. {
  4955. //
  4956. // Caller wants to remove all the tab stops; So, release
  4957. //
  4958. if (!UserLocalFree(ped->pTabStops))
  4959. {
  4960. return FALSE;
  4961. }
  4962. ped->pTabStops = NULL;
  4963. goto RedrawAndReturn;
  4964. }
  4965. }
  4966. //
  4967. // Copy the new tab stops onto the tab stop array after converting the
  4968. // dialog co-ordinates into the pixel co-ordinates
  4969. //
  4970. ped->pTabStops = pTabStops;
  4971. //
  4972. // First element contains the count
  4973. //
  4974. *pTabStops++ = nTabPos;
  4975. while (nTabPos--)
  4976. {
  4977. //
  4978. // aveCharWidth must be used instead of cxSysCharWidth.
  4979. // Fix for Bug #3871 --SANKAR-- 03/14/91
  4980. //
  4981. *pTabStops++ = MultDiv(*lpTabStops++, ped->aveCharWidth, 4);
  4982. }
  4983. RedrawAndReturn:
  4984. //
  4985. // Because the tabstops have changed, we need to recompute the
  4986. // maxPixelWidth. Otherwise, horizontal scrolls will have problems.
  4987. // Fix for Bug #6042 - 3/15/94
  4988. //
  4989. EditML_BuildchLines(ped, 0, 0, FALSE, NULL, NULL);
  4990. //
  4991. // Caret may have changed line by the line recalc above.
  4992. //
  4993. EditML_UpdateiCaretLine(ped);
  4994. EditML_EnsureCaretVisible(ped);
  4995. //
  4996. // Also, we need to redraw the whole window.
  4997. //
  4998. InvalidateRect(ped->hwnd, NULL, TRUE);
  4999. return TRUE;
  5000. }
  5001. //---------------------------------------------------------------------------//
  5002. //
  5003. // EditML_Undo AorW
  5004. //
  5005. // Handles Undo for multiline edit controls.
  5006. //
  5007. BOOL EditML_Undo(PED ped)
  5008. {
  5009. HANDLE hDeletedText = ped->hDeletedText;
  5010. BOOL fDelete = (BOOL)(ped->undoType & UNDO_DELETE);
  5011. ICH cchDeleted = ped->cchDeleted;
  5012. ICH ichDeleted = ped->ichDeleted;
  5013. if (ped->undoType == UNDO_NONE)
  5014. {
  5015. //
  5016. // No undo...
  5017. //
  5018. return FALSE;
  5019. }
  5020. ped->hDeletedText = NULL;
  5021. ped->cchDeleted = 0;
  5022. ped->ichDeleted = (ICH)-1;
  5023. ped->undoType &= ~UNDO_DELETE;
  5024. if (ped->undoType == UNDO_INSERT)
  5025. {
  5026. ped->undoType = UNDO_NONE;
  5027. //
  5028. // Set the selection to the inserted text
  5029. //
  5030. EditML_SetSelection(ped, FALSE, ped->ichInsStart, ped->ichInsEnd);
  5031. ped->ichInsStart = ped->ichInsEnd = (ICH)-1;
  5032. //
  5033. // Now send a backspace to delete and save it in the undo buffer...
  5034. //
  5035. SendMessage(ped->hwnd, WM_CHAR, (WPARAM)VK_BACK, 0L);
  5036. }
  5037. if (fDelete)
  5038. {
  5039. //
  5040. // Insert deleted chars
  5041. //
  5042. //
  5043. // Set the selection to the inserted text
  5044. //
  5045. EditML_SetSelection(ped, FALSE, ichDeleted, ichDeleted);
  5046. EditML_InsertText(ped, hDeletedText, cchDeleted, FALSE);
  5047. GlobalFree(hDeletedText);
  5048. EditML_SetSelection(ped, FALSE, ichDeleted, ichDeleted + cchDeleted);
  5049. }
  5050. return TRUE;
  5051. }
  5052. //---------------------------------------------------------------------------//
  5053. //
  5054. // EditML_Create AorW
  5055. //
  5056. // Creates the edit control for the window hwnd by allocating memory
  5057. // as required from the application's heap. Notifies parent if no memory
  5058. // error (after cleaning up if needed). Returns TRUE if no error else return s
  5059. // -1.
  5060. //
  5061. LONG EditML_Create(PED ped, LPCREATESTRUCT lpCreateStruct)
  5062. {
  5063. LONG windowStyle;
  5064. LPWSTR lpszName;
  5065. //
  5066. // Get values from the window instance data structure and put them in the
  5067. // ped so that we can access them easier
  5068. //
  5069. windowStyle = GET_STYLE(ped);
  5070. //
  5071. // Do the standard creation stuff
  5072. //
  5073. if (!Edit_Create(ped, windowStyle))
  5074. {
  5075. return -1;
  5076. }
  5077. //
  5078. // Allocate line start array in local heap and lock it down
  5079. //
  5080. ped->chLines = (LPICH)LocalAlloc(LPTR, 2 * sizeof(int));
  5081. if (ped->chLines == NULL)
  5082. {
  5083. return -1;
  5084. }
  5085. //
  5086. // Call it one line of text...
  5087. //
  5088. ped->cLines = 1;
  5089. //
  5090. // If app wants WS_VSCROLL or WS_HSCROLL, it automatically gets AutoVScroll
  5091. // or AutoHScroll.
  5092. //
  5093. if ((windowStyle & ES_AUTOVSCROLL) || (windowStyle & WS_VSCROLL))
  5094. {
  5095. ped->fAutoVScroll = 1;
  5096. }
  5097. if (ped->format != ES_LEFT)
  5098. {
  5099. //
  5100. // If user wants right or center justified text, then we turn off
  5101. // AUTOHSCROLL and WS_HSCROLL since non-left styles don't make sense
  5102. // otherwise.
  5103. //
  5104. windowStyle &= ~WS_HSCROLL;
  5105. ClearWindowState(ped->hwnd, WS_HSCROLL);
  5106. ped->fAutoHScroll = FALSE;
  5107. }
  5108. else if (windowStyle & WS_HSCROLL)
  5109. {
  5110. ped->fAutoHScroll = TRUE;
  5111. }
  5112. ped->fWrap = (!ped->fAutoHScroll && !(windowStyle & WS_HSCROLL));
  5113. //
  5114. // Max # chars we will allow user to enter
  5115. //
  5116. ped->cchTextMax = MAXTEXT;
  5117. //
  5118. // Set the default font to be the system font.
  5119. //
  5120. if ( !Edit_SetFont(ped, NULL, FALSE) )
  5121. {
  5122. // If setting the font fails, our textmetrics can potentially be left
  5123. // unitialized. Fail to create the control.
  5124. return -1;
  5125. }
  5126. //
  5127. // Set the window text if needed and notify parent if not enough memory to
  5128. // set the initial text.
  5129. //
  5130. lpszName = (LPWSTR)lpCreateStruct->lpszName;
  5131. if (!Edit_SetEditText(ped, (LPSTR)lpszName))
  5132. {
  5133. return -1;
  5134. }
  5135. return TRUE;
  5136. }