Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5972 lines
162 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 0
  1770. if (!ISDELIMETERAW((*(ptext + (ichLineEnd - 1)*ped->cbChar))) && ISDELIMETERAW((*(ptext + ichLineEnd*ped->cbChar)))) #ERROR
  1771. if ((*(ptext + ichLineEnd - 1) != ' ' &&
  1772. *(ptext + ichLineEnd - 1) != VK_TAB) &&
  1773. (*(ptext + ichLineEnd) == ' ' ||
  1774. *(ptext + ichLineEnd) == VK_TAB))
  1775. #endif
  1776. if (AWCOMPARECHAR(ped,ptext + ichLineEnd * ped->cbChar, ' ') ||
  1777. AWCOMPARECHAR(ped,ptext + ichLineEnd * ped->cbChar, VK_TAB))
  1778. {
  1779. //
  1780. // Swallow the space at the end of a line.
  1781. //
  1782. if (ichLineEnd < ped->cch)
  1783. {
  1784. ichLineEnd++;
  1785. }
  1786. }
  1787. //
  1788. // Skip over crlf or crcrlf if it exists. Thus, ichLineEnd is the first
  1789. // character in the next line.
  1790. //
  1791. ichLineEndBeforeCRLF = ichLineEnd;
  1792. if (ped->fAnsi)
  1793. {
  1794. if (ichLineEnd < ped->cch && *(ptext + ichLineEnd) == 0x0D)
  1795. {
  1796. ichLineEnd += 2;
  1797. }
  1798. //
  1799. // Skip over CRCRLF
  1800. //
  1801. if (ichLineEnd < ped->cch && *(ptext + ichLineEnd) == 0x0A)
  1802. {
  1803. ichLineEnd++;
  1804. }
  1805. }
  1806. else
  1807. {
  1808. if (ichLineEnd < ped->cch && *(((LPWSTR)ptext) + ichLineEnd) == 0x0D)
  1809. {
  1810. ichLineEnd += 2;
  1811. }
  1812. //
  1813. // Skip over CRCRLF
  1814. //
  1815. if (ichLineEnd < ped->cch && *(((LPWSTR)ptext) + ichLineEnd) == 0x0A)
  1816. {
  1817. ichLineEnd++;
  1818. TraceMsg(TF_STANDARD, "Edit: Skip over CRCRLF");
  1819. }
  1820. }
  1821. #if DBG
  1822. if (ichLineEnd > ped->cch)
  1823. {
  1824. TraceMsg(TF_STANDARD, "Edit: ichLineEnd (%d)> ped->cch (%d)", ichLineEnd, ped->cch);
  1825. }
  1826. #endif
  1827. //
  1828. // Now, increment iLine, allocate space for the next line, and set its
  1829. // starting point
  1830. //
  1831. iLine++;
  1832. if (!fUserTyping || (iLine > ped->cLines - 1) || (ped->chLines[iLine] != ichLineEnd))
  1833. {
  1834. //
  1835. // The line break occured in a different place than before.
  1836. //
  1837. if (!fLineBroken)
  1838. {
  1839. //
  1840. // Since we haven't broken a line before, just set the min
  1841. // break line.
  1842. //
  1843. fLineBroken = TRUE;
  1844. if (ichLineEndBeforeCRLF == ichLineEnd)
  1845. {
  1846. minCchBreak = maxCchBreak = (ichLineEnd ? ichLineEnd - 1 : 0);
  1847. }
  1848. else
  1849. {
  1850. minCchBreak = maxCchBreak = ichLineEndBeforeCRLF;
  1851. }
  1852. }
  1853. maxCchBreak = max(maxCchBreak, ichLineEnd);
  1854. Edit_Unlock(ped);
  1855. //
  1856. // Now insert the new line into the array
  1857. //
  1858. if (!EditML_InsertchLine(ped, iLine, ichLineEnd, (BOOL)(cchDelta != 0)))
  1859. {
  1860. goto EndUp;
  1861. }
  1862. ptext = Edit_Lock(ped);
  1863. }
  1864. else
  1865. {
  1866. maxCchBreak = ped->chLines[iLine];
  1867. //
  1868. // Quick escape
  1869. //
  1870. goto UnlockAndEndUp;
  1871. }
  1872. ichLineStart = ichLineEnd;
  1873. }
  1874. if (iLine != ped->cLines)
  1875. {
  1876. TraceMsg(TF_STANDARD, "Edit: chLines[%d] is being cleared.", iLine);
  1877. ped->cLines = iLine;
  1878. ped->chLines[ped->cLines] = 0;
  1879. }
  1880. //
  1881. // Note that we incremented iLine towards the end of the while loop so, the
  1882. // index, iLine, is actually equal to the line count
  1883. //
  1884. if (ped->cch && AWCOMPARECHAR(ped, ptext + (ped->cch - 1)*ped->cbChar, 0x0A) &&
  1885. ped->chLines[ped->cLines - 1] < ped->cch)
  1886. {
  1887. //
  1888. // Make sure last line has no crlf in it
  1889. //
  1890. if (!fLineBroken)
  1891. {
  1892. //
  1893. // Since we haven't broken a line before, just set the min break
  1894. // line.
  1895. //
  1896. fLineBroken = TRUE;
  1897. minCchBreak = ped->cch - 1;
  1898. }
  1899. maxCchBreak = max(maxCchBreak, ichLineEnd);
  1900. Edit_Unlock(ped);
  1901. EditML_InsertchLine(ped, iLine, ped->cch, FALSE);
  1902. EditML_SanityCheck(ped);
  1903. }
  1904. else
  1905. {
  1906. UnlockAndEndUp:
  1907. Edit_Unlock(ped);
  1908. }
  1909. EndUp:
  1910. Edit_ReleaseDC(ped, hdc, TRUE);
  1911. if (pll)
  1912. {
  1913. *pll = minCchBreak;
  1914. }
  1915. if (phl)
  1916. {
  1917. *phl = maxCchBreak;
  1918. }
  1919. UpdateScroll:
  1920. EditML_Scroll(ped, FALSE, ML_REFRESH, 0, TRUE);
  1921. EditML_Scroll(ped, TRUE, ML_REFRESH, 0, TRUE);
  1922. EditML_SanityCheck(ped);
  1923. return;
  1924. }
  1925. //---------------------------------------------------------------------------//
  1926. //
  1927. // EditML_Paint()
  1928. //
  1929. // Response to WM_PAINT message.
  1930. //
  1931. VOID EditML_Paint(PED ped, HDC hdc, LPRECT lprc)
  1932. {
  1933. HFONT hOldFont;
  1934. ICH imin;
  1935. ICH imax;
  1936. HBRUSH hbr;
  1937. BOOL fNeedDelete = FALSE;
  1938. //
  1939. // Do we need to draw the border ourself for old apps?
  1940. //
  1941. if (ped->fFlatBorder)
  1942. {
  1943. RECT rcT;
  1944. ULONG ulStyle;
  1945. INT cxBorder;
  1946. INT cyBorder;
  1947. INT cxFrame;
  1948. INT cyFrame;
  1949. ulStyle = GET_STYLE(ped);
  1950. cxBorder = GetSystemMetrics(SM_CXBORDER);
  1951. cyBorder = GetSystemMetrics(SM_CYBORDER);
  1952. cxFrame = GetSystemMetrics(SM_CXFRAME);
  1953. cyFrame = GetSystemMetrics(SM_CYFRAME);
  1954. GetClientRect(ped->hwnd, &rcT);
  1955. if (ulStyle & WS_SIZEBOX)
  1956. {
  1957. InflateRect(&rcT, cxBorder - cxFrame, cyBorder - cyFrame);
  1958. }
  1959. DrawFrame(hdc, &rcT, 1, DF_WINDOWFRAME);
  1960. }
  1961. Edit_SetClip(ped, hdc, (BOOL) (ped->xOffset == 0));
  1962. if (ped->hFont)
  1963. {
  1964. hOldFont = SelectObject(hdc, ped->hFont);
  1965. }
  1966. if (!lprc)
  1967. {
  1968. //
  1969. // no partial rect given -- draw all text
  1970. //
  1971. imin = 0;
  1972. imax = ped->cch;
  1973. }
  1974. else
  1975. {
  1976. //
  1977. // only draw pertinent text
  1978. //
  1979. imin = (ICH) EditML_MouseToIch(ped, hdc, ((LPPOINT) &lprc->left), NULL) - 1;
  1980. if (imin == -1)
  1981. {
  1982. imin = 0;
  1983. }
  1984. //
  1985. // HACK_ALERT:
  1986. // The 3 is required here because, EditML_MouseToIch() returns decremented
  1987. // value; We must fix EditML_MouseToIch.
  1988. //
  1989. imax = (ICH) EditML_MouseToIch(ped, hdc, ((LPPOINT) &lprc->right), NULL) + 3;
  1990. if (imax > ped->cch)
  1991. {
  1992. imax = ped->cch;
  1993. }
  1994. }
  1995. hbr = Edit_GetBrush(ped, hdc, &fNeedDelete);
  1996. if (hbr)
  1997. {
  1998. RECT rc;
  1999. GetClientRect(ped->hwnd, &rc);
  2000. FillRect(hdc, &rc, hbr);
  2001. if (fNeedDelete)
  2002. {
  2003. DeleteObject(hbr);
  2004. }
  2005. }
  2006. EditML_DrawText(ped, hdc, imin, imax, FALSE);
  2007. if (ped->hFont)
  2008. {
  2009. SelectObject(hdc, hOldFont);
  2010. }
  2011. }
  2012. //---------------------------------------------------------------------------//
  2013. //
  2014. // EditML_KeyDown AorW
  2015. //
  2016. // Handles cursor movement and other VIRT KEY stuff. keyMods allows
  2017. // us to make EditML_KeyDownHandler calls and specify if the modifier keys (shift
  2018. // and control) are up or down. If keyMods == 0, we get the keyboard state
  2019. // using GetKeyState(VK_SHIFT) etc. Otherwise, the bits in keyMods define the
  2020. // state of the shift and control keys.
  2021. //
  2022. VOID EditML_KeyDown(PED ped, UINT virtKeyCode, int keyMods)
  2023. {
  2024. HDC hdc;
  2025. BOOL prevLine;
  2026. POINT mousePt;
  2027. int defaultDlgId;
  2028. int iScrollAmt;
  2029. //
  2030. // Variables we will use for redrawing the updated text
  2031. //
  2032. //
  2033. // new selection is specified by newMinSel, newMaxSel
  2034. //
  2035. ICH newMaxSel = ped->ichMaxSel;
  2036. ICH newMinSel = ped->ichMinSel;
  2037. //
  2038. // Flags for drawing the updated text
  2039. //
  2040. BOOL changeSelection = FALSE;
  2041. //
  2042. // Comparisons we do often
  2043. //
  2044. BOOL MinEqMax = (newMaxSel == newMinSel);
  2045. BOOL MinEqCar = (ped->ichCaret == newMinSel);
  2046. BOOL MaxEqCar = (ped->ichCaret == newMaxSel);
  2047. //
  2048. // State of shift and control keys.
  2049. //
  2050. int scState;
  2051. if (ped->fMouseDown)
  2052. {
  2053. //
  2054. // If we are in the middle of a mousedown command, don't do anything.
  2055. //
  2056. return;
  2057. }
  2058. if (ped->hwndBalloon)
  2059. {
  2060. Edit_HideBalloonTip(ped->hwnd);
  2061. }
  2062. scState = Edit_GetModKeys(keyMods);
  2063. switch (virtKeyCode)
  2064. {
  2065. case VK_ESCAPE:
  2066. if (ped->fInDialogBox)
  2067. {
  2068. //
  2069. // This condition is removed because, if the dialogbox does not
  2070. // have a CANCEL button and if ESC is hit when focus is on a
  2071. // ML edit control the dialogbox must close whether it has cancel
  2072. // button or not to be consistent with SL edit control;
  2073. // DefDlgProc takes care of the disabled CANCEL button case.
  2074. // Fix for Bug #4123 -- 02/07/91 -- SANKAR --
  2075. //
  2076. #if 0
  2077. if (GetDlgItem(ped->hwndParent, IDCANCEL))
  2078. #endif
  2079. //
  2080. // User hit ESC...Send a close message (which in turn sends a
  2081. // cancelID to the app in DefDialogProc...
  2082. //
  2083. PostMessage(ped->hwndParent, WM_CLOSE, 0, 0L);
  2084. }
  2085. return;
  2086. case VK_RETURN:
  2087. if (ped->fInDialogBox)
  2088. {
  2089. //
  2090. // If this multiline edit control is in a dialog box, then we want
  2091. // the RETURN key to be sent to the default dialog button (if there
  2092. // is one). CTRL-RETURN will insert a RETURN into the text. Note
  2093. // that CTRL-RETURN automatically translates into a linefeed (0x0A)
  2094. // and in the EditML_CharHandler, we handle this as if a return was
  2095. // entered.
  2096. //
  2097. if (scState != CTRLDOWN)
  2098. {
  2099. if (GET_STYLE(ped) & ES_WANTRETURN)
  2100. {
  2101. //
  2102. // This edit control wants cr to be inserted so break out of
  2103. // case.
  2104. //
  2105. return;
  2106. }
  2107. defaultDlgId = (int)(DWORD)LOWORD(SendMessage(ped->hwndParent,
  2108. DM_GETDEFID, 0, 0L));
  2109. if (defaultDlgId)
  2110. {
  2111. HWND hwnd = GetDlgItem(ped->hwndParent, defaultDlgId);
  2112. if (hwnd)
  2113. {
  2114. SendMessage(ped->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwnd, 1L);
  2115. if (!ped->fFocus)
  2116. {
  2117. PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0L);
  2118. }
  2119. }
  2120. }
  2121. }
  2122. return;
  2123. }
  2124. break;
  2125. case VK_TAB:
  2126. //
  2127. // If this multiline edit control is in a dialog box, then we want the
  2128. // TAB key to take you to the next control, shift TAB to take you to the
  2129. // previous control. We always want CTRL-TAB to insert a tab into the
  2130. // edit control regardless of weather or not we're in a dialog box.
  2131. //
  2132. if (scState == CTRLDOWN)
  2133. {
  2134. EditML_Char(ped, virtKeyCode, keyMods);
  2135. }
  2136. else if (ped->fInDialogBox)
  2137. {
  2138. SendMessage(ped->hwndParent, WM_NEXTDLGCTL, scState == SHFTDOWN, 0L);
  2139. }
  2140. return;
  2141. case VK_LEFT:
  2142. //
  2143. // If the caret isn't at the beginning, we can move left
  2144. //
  2145. if (ped->ichCaret)
  2146. {
  2147. //
  2148. // Get new caret pos.
  2149. //
  2150. if (scState & CTRLDOWN)
  2151. {
  2152. //
  2153. // Move caret word left
  2154. //
  2155. Edit_Word(ped, ped->ichCaret, TRUE, &ped->ichCaret, NULL);
  2156. }
  2157. else
  2158. {
  2159. if (ped->pLpkEditCallout)
  2160. {
  2161. ped->ichCaret = Edit_MoveSelectionRestricted(ped, ped->ichCaret, TRUE);
  2162. }
  2163. else
  2164. {
  2165. //
  2166. // Move caret char left
  2167. //
  2168. ped->ichCaret = Edit_MoveSelection(ped, ped->ichCaret, TRUE);
  2169. }
  2170. }
  2171. //
  2172. // Get new selection
  2173. //
  2174. if (scState & SHFTDOWN)
  2175. {
  2176. if (MaxEqCar && !MinEqMax)
  2177. {
  2178. //
  2179. // Reduce selection
  2180. //
  2181. newMaxSel = ped->ichCaret;
  2182. UserAssert(newMinSel == ped->ichMinSel);
  2183. }
  2184. else
  2185. {
  2186. //
  2187. // Extend selection
  2188. //
  2189. newMinSel = ped->ichCaret;
  2190. }
  2191. }
  2192. else
  2193. {
  2194. //
  2195. // Clear selection
  2196. //
  2197. newMaxSel = newMinSel = ped->ichCaret;
  2198. }
  2199. changeSelection = TRUE;
  2200. }
  2201. else
  2202. {
  2203. //
  2204. // If the user tries to move left and we are at the 0th
  2205. // character and there is a selection, then cancel the
  2206. // selection.
  2207. //
  2208. if ( (ped->ichMaxSel != ped->ichMinSel) &&
  2209. !(scState & SHFTDOWN) )
  2210. {
  2211. changeSelection = TRUE;
  2212. newMaxSel = newMinSel = ped->ichCaret;
  2213. }
  2214. }
  2215. break;
  2216. case VK_RIGHT:
  2217. //
  2218. // If the caret isn't at the end, we can move right.
  2219. //
  2220. if (ped->ichCaret < ped->cch)
  2221. {
  2222. //
  2223. // Get new caret pos.
  2224. //
  2225. if (scState & CTRLDOWN)
  2226. {
  2227. //
  2228. // Move caret word right
  2229. //
  2230. Edit_Word(ped, ped->ichCaret, FALSE, NULL, &ped->ichCaret);
  2231. }
  2232. else
  2233. {
  2234. //
  2235. // Move caret char right
  2236. //
  2237. if (ped->pLpkEditCallout)
  2238. {
  2239. ped->ichCaret = Edit_MoveSelectionRestricted(ped, ped->ichCaret, FALSE);
  2240. }
  2241. else
  2242. {
  2243. ped->ichCaret = Edit_MoveSelection(ped, ped->ichCaret, FALSE);
  2244. }
  2245. }
  2246. //
  2247. // Get new selection.
  2248. //
  2249. if (scState & SHFTDOWN)
  2250. {
  2251. if (MinEqCar && !MinEqMax)
  2252. {
  2253. //
  2254. // Reduce selection
  2255. //
  2256. newMinSel = ped->ichCaret;
  2257. UserAssert(newMaxSel == ped->ichMaxSel);
  2258. }
  2259. else
  2260. {
  2261. //
  2262. // Extend selection
  2263. //
  2264. newMaxSel = ped->ichCaret;
  2265. }
  2266. }
  2267. else
  2268. {
  2269. //
  2270. // Clear selection
  2271. //
  2272. newMaxSel = newMinSel = ped->ichCaret;
  2273. }
  2274. changeSelection = TRUE;
  2275. }
  2276. else
  2277. {
  2278. //
  2279. // If the user tries to move right and we are at the last
  2280. // character and there is a selection, then cancel the
  2281. // selection.
  2282. //
  2283. if ( (ped->ichMaxSel != ped->ichMinSel) &&
  2284. !(scState & SHFTDOWN) )
  2285. {
  2286. newMaxSel = newMinSel = ped->ichCaret;
  2287. changeSelection = TRUE;
  2288. }
  2289. }
  2290. break;
  2291. case VK_UP:
  2292. case VK_DOWN:
  2293. if (ped->cLines - 1 != ped->iCaretLine &&
  2294. ped->ichCaret == ped->chLines[ped->iCaretLine + 1])
  2295. {
  2296. prevLine = TRUE;
  2297. }
  2298. else
  2299. {
  2300. prevLine = FALSE;
  2301. }
  2302. hdc = Edit_GetDC(ped, TRUE);
  2303. EditML_IchToXYPos(ped, hdc, ped->ichCaret, prevLine, &mousePt);
  2304. Edit_ReleaseDC(ped, hdc, TRUE);
  2305. mousePt.y += 1 + (virtKeyCode == VK_UP ? -ped->lineHeight : ped->lineHeight);
  2306. if (!(scState & CTRLDOWN))
  2307. {
  2308. //
  2309. // Send fake mouse messages to handle this
  2310. // If VK_SHIFT is down, extend selection & move caret up/down
  2311. // 1 line. Otherwise, clear selection & move caret.
  2312. //
  2313. EditML_MouseMotion(ped, WM_LBUTTONDOWN,
  2314. !(scState & SHFTDOWN) ? 0 : MK_SHIFT, &mousePt);
  2315. EditML_MouseMotion(ped, WM_LBUTTONUP,
  2316. !(scState & SHFTDOWN) ? 0 : MK_SHIFT, &mousePt);
  2317. }
  2318. break;
  2319. case VK_HOME:
  2320. //
  2321. // Update caret.
  2322. //
  2323. if (scState & CTRLDOWN)
  2324. {
  2325. //
  2326. // Move caret to beginning of text.
  2327. //
  2328. ped->ichCaret = 0;
  2329. }
  2330. else
  2331. {
  2332. //
  2333. // Move caret to beginning of line.
  2334. //
  2335. ped->ichCaret = ped->chLines[ped->iCaretLine];
  2336. }
  2337. //
  2338. // Update selection.
  2339. //
  2340. newMinSel = ped->ichCaret;
  2341. if (scState & SHFTDOWN)
  2342. {
  2343. if (MaxEqCar && !MinEqMax)
  2344. {
  2345. if (scState & CTRLDOWN)
  2346. {
  2347. newMaxSel = ped->ichMinSel;
  2348. }
  2349. else
  2350. {
  2351. newMinSel = ped->ichMinSel;
  2352. newMaxSel = ped->ichCaret;
  2353. }
  2354. }
  2355. }
  2356. else
  2357. {
  2358. //
  2359. // Clear selection
  2360. //
  2361. newMaxSel = ped->ichCaret;
  2362. }
  2363. changeSelection = TRUE;
  2364. break;
  2365. case VK_END:
  2366. //
  2367. // Update caret.
  2368. //
  2369. if (scState & CTRLDOWN)
  2370. {
  2371. //
  2372. // Move caret to end of text.
  2373. //
  2374. ped->ichCaret = ped->cch;
  2375. }
  2376. else
  2377. {
  2378. //
  2379. // Move caret to end of line.
  2380. //
  2381. ped->ichCaret = ped->chLines[ped->iCaretLine] +
  2382. EditML_Line(ped, ped->iCaretLine);
  2383. }
  2384. //
  2385. // Update selection.
  2386. //
  2387. newMaxSel = ped->ichCaret;
  2388. if (scState & SHFTDOWN)
  2389. {
  2390. if (MinEqCar && !MinEqMax)
  2391. {
  2392. //
  2393. // Reduce selection
  2394. //
  2395. if (scState & CTRLDOWN)
  2396. {
  2397. newMinSel = ped->ichMaxSel;
  2398. }
  2399. else
  2400. {
  2401. newMinSel = ped->ichCaret;
  2402. newMaxSel = ped->ichMaxSel;
  2403. }
  2404. }
  2405. }
  2406. else
  2407. {
  2408. //
  2409. // Clear selection
  2410. //
  2411. newMinSel = ped->ichCaret;
  2412. }
  2413. changeSelection = TRUE;
  2414. break;
  2415. //
  2416. // FE_IME // EC_INSERT_COMPOSITION_CHAR : EditML_KeyDown() : VK_HANJA support
  2417. //
  2418. case VK_HANJA:
  2419. if ( HanjaKeyHandler( ped ) )
  2420. {
  2421. changeSelection = TRUE;
  2422. newMinSel = ped->ichCaret;
  2423. newMaxSel = ped->ichCaret + (ped->fAnsi ? 2 : 1);
  2424. }
  2425. break;
  2426. case VK_PRIOR:
  2427. case VK_NEXT:
  2428. if (!(scState & CTRLDOWN))
  2429. {
  2430. //
  2431. // Vertical scroll by one visual screen
  2432. //
  2433. hdc = Edit_GetDC(ped, TRUE);
  2434. EditML_IchToXYPos(ped, hdc, ped->ichCaret, FALSE, &mousePt);
  2435. Edit_ReleaseDC(ped, hdc, TRUE);
  2436. mousePt.y += 1;
  2437. SendMessage(ped->hwnd, WM_VSCROLL, virtKeyCode == VK_PRIOR ? SB_PAGEUP : SB_PAGEDOWN, 0L);
  2438. //
  2439. // Move the cursor there
  2440. //
  2441. EditML_MouseMotion(ped, WM_LBUTTONDOWN, !(scState & SHFTDOWN) ? 0 : MK_SHIFT, &mousePt);
  2442. EditML_MouseMotion(ped, WM_LBUTTONUP, !(scState & SHFTDOWN) ? 0 : MK_SHIFT, &mousePt);
  2443. }
  2444. else
  2445. {
  2446. //
  2447. // Horizontal scroll by one screenful minus one char
  2448. //
  2449. iScrollAmt = ((ped->rcFmt.right - ped->rcFmt.left) / ped->aveCharWidth) - 1;
  2450. if (virtKeyCode == VK_PRIOR)
  2451. {
  2452. //
  2453. // For previous page
  2454. //
  2455. iScrollAmt *= -1;
  2456. }
  2457. SendMessage(ped->hwnd, WM_HSCROLL, MAKELONG(EM_LINESCROLL, iScrollAmt), 0);
  2458. break;
  2459. }
  2460. break;
  2461. case VK_DELETE:
  2462. if (ped->fReadOnly)
  2463. {
  2464. break;
  2465. }
  2466. switch (scState)
  2467. {
  2468. case NONEDOWN:
  2469. //
  2470. // Clear selection. If no selection, delete (clear) character
  2471. // right
  2472. //
  2473. if ((ped->ichMaxSel < ped->cch) && (ped->ichMinSel == ped->ichMaxSel))
  2474. {
  2475. //
  2476. // Move cursor forwards and send a backspace message...
  2477. //
  2478. if (ped->pLpkEditCallout)
  2479. {
  2480. ped->ichMinSel = ped->ichCaret;
  2481. ped->ichMaxSel = Edit_MoveSelectionRestricted(ped, ped->ichCaret, FALSE);
  2482. }
  2483. else
  2484. {
  2485. ped->ichCaret = Edit_MoveSelection(ped, ped->ichCaret, FALSE);
  2486. ped->ichMaxSel = ped->ichMinSel = ped->ichCaret;
  2487. }
  2488. goto DeleteAnotherChar;
  2489. }
  2490. break;
  2491. case SHFTDOWN:
  2492. //
  2493. // CUT selection ie. remove and copy to clipboard, or if no
  2494. // selection, delete (clear) character left.
  2495. //
  2496. if (ped->ichMinSel == ped->ichMaxSel)
  2497. {
  2498. goto DeleteAnotherChar;
  2499. }
  2500. else
  2501. {
  2502. SendMessage(ped->hwnd, WM_CUT, (UINT)0, 0L);
  2503. }
  2504. break;
  2505. case CTRLDOWN:
  2506. //
  2507. // Clear selection, or delete to end of line if no selection
  2508. //
  2509. if ((ped->ichMaxSel < ped->cch) && (ped->ichMinSel == ped->ichMaxSel))
  2510. {
  2511. ped->ichMaxSel = ped->ichCaret = ped->chLines[ped->iCaretLine] +
  2512. EditML_Line(ped, ped->iCaretLine);
  2513. }
  2514. break;
  2515. }
  2516. if (!(scState & SHFTDOWN) && (ped->ichMinSel != ped->ichMaxSel))
  2517. {
  2518. DeleteAnotherChar:
  2519. if (Is400Compat(UserGetVersion()))
  2520. {
  2521. EditML_Char(ped, VK_BACK, 0);
  2522. }
  2523. else
  2524. {
  2525. SendMessage(ped->hwnd, WM_CHAR, VK_BACK, 0);
  2526. }
  2527. }
  2528. //
  2529. // No need to update text or selection since BACKSPACE message does it
  2530. // for us.
  2531. //
  2532. break;
  2533. case VK_INSERT:
  2534. if (scState == CTRLDOWN || scState == SHFTDOWN)
  2535. {
  2536. //
  2537. // if CTRLDOWN Copy current selection to clipboard
  2538. //
  2539. //
  2540. // if SHFTDOWN Paste clipboard
  2541. //
  2542. SendMessage(ped->hwnd, (UINT)(scState == CTRLDOWN ? WM_COPY : WM_PASTE), 0, 0);
  2543. }
  2544. break;
  2545. }
  2546. if (changeSelection)
  2547. {
  2548. hdc = Edit_GetDC(ped, FALSE);
  2549. EditML_ChangeSelection(ped, hdc, newMinSel, newMaxSel);
  2550. //
  2551. // Set the caret's line
  2552. //
  2553. ped->iCaretLine = EditML_IchToLine(ped, ped->ichCaret);
  2554. if (virtKeyCode == VK_END &&
  2555. // Next line: Win95 Bug#11822, EditControl repaint (Sankar)
  2556. (ped->ichCaret == ped->chLines[ped->iCaretLine]) &&
  2557. ped->ichCaret < ped->cch &&
  2558. ped->fWrap && ped->iCaretLine > 0)
  2559. {
  2560. LPSTR pText = Edit_Lock(ped);
  2561. //
  2562. // Handle moving to the end of a word wrapped line. This keeps the
  2563. // cursor from falling to the start of the next line if we have word
  2564. // wrapped and there is no CRLF.
  2565. //
  2566. if ( ped->fAnsi )
  2567. {
  2568. if (*(WORD UNALIGNED *)(pText + ped->chLines[ped->iCaretLine] - 2) != 0x0A0D)
  2569. {
  2570. ped->iCaretLine--;
  2571. }
  2572. }
  2573. else
  2574. {
  2575. if (*(DWORD UNALIGNED *)(pText +
  2576. (ped->chLines[ped->iCaretLine] - 2)*ped->cbChar) != 0x000A000D)
  2577. {
  2578. ped->iCaretLine--;
  2579. }
  2580. }
  2581. Edit_Unlock(ped);
  2582. }
  2583. //
  2584. // Since drawtext sets the caret position
  2585. //
  2586. EditML_SetCaretPosition(ped, hdc);
  2587. Edit_ReleaseDC(ped, hdc, FALSE);
  2588. //
  2589. // Make sure we can see the cursor
  2590. //
  2591. EditML_EnsureCaretVisible(ped);
  2592. }
  2593. }
  2594. //---------------------------------------------------------------------------//
  2595. //
  2596. // EditML_Char
  2597. //
  2598. // Handles character and virtual key input
  2599. //
  2600. VOID EditML_Char(PED ped, DWORD keyValue, int keyMods)
  2601. {
  2602. WCHAR keyPress;
  2603. BOOL updateText = FALSE;
  2604. //
  2605. // keyValue is either:
  2606. // a Virtual Key (eg: VK_TAB, VK_ESCAPE, VK_BACK)
  2607. // a character (Unicode or "ANSI")
  2608. //
  2609. if (ped->fAnsi)
  2610. {
  2611. keyPress = LOBYTE(keyValue);
  2612. }
  2613. else
  2614. {
  2615. keyPress = LOWORD(keyValue);
  2616. }
  2617. if (ped->fMouseDown || keyPress == VK_ESCAPE)
  2618. {
  2619. //
  2620. // If we are in the middle of a mousedown command, don't do anything.
  2621. // Also, just ignore it if we get a translated escape key which happens
  2622. // with multiline edit controls in a dialog box.
  2623. //
  2624. return;
  2625. }
  2626. Edit_InOutReconversionMode(ped, FALSE);
  2627. {
  2628. int scState;
  2629. scState = Edit_GetModKeys(keyMods);
  2630. if (ped->fInDialogBox && scState != CTRLDOWN)
  2631. {
  2632. //
  2633. // If this multiline edit control is in a dialog box, then we want the
  2634. // TAB key to take you to the next control, shift TAB to take you to the
  2635. // previous control, and CTRL-TAB to insert a tab into the edit control.
  2636. // We moved the focus when we received the keydown message so we will
  2637. // ignore the TAB key now unless the ctrl key is down. Also, we want
  2638. // CTRL-RETURN to insert a return into the text and RETURN to be sent to
  2639. // the default button.
  2640. //
  2641. if (keyPress == VK_TAB || (keyPress == VK_RETURN && !(GET_STYLE(ped) & ES_WANTRETURN)))
  2642. {
  2643. return;
  2644. }
  2645. }
  2646. //
  2647. // Allow CTRL+C to copy from a read only edit control
  2648. // Ignore all other keys in read only controls
  2649. //
  2650. if ((ped->fReadOnly) && !((keyPress == 3) && (scState == CTRLDOWN)))
  2651. {
  2652. return;
  2653. }
  2654. }
  2655. switch (keyPress)
  2656. {
  2657. case 0x0A:
  2658. // linefeed
  2659. keyPress = VK_RETURN;
  2660. //
  2661. // FALL THRU
  2662. //
  2663. case VK_RETURN:
  2664. case VK_TAB:
  2665. case VK_BACK:
  2666. DeleteSelection:
  2667. if (EditML_DeleteText(ped))
  2668. {
  2669. updateText = TRUE;
  2670. }
  2671. break;
  2672. default:
  2673. if (keyPress >= TEXT(' '))
  2674. {
  2675. //
  2676. // If this is in [a-z],[A-Z] and we are an ES_NUMBER
  2677. // edit field, bail.
  2678. //
  2679. if (Is400Compat(UserGetVersion()) && GET_STYLE(ped) & ES_NUMBER)
  2680. {
  2681. if (!Edit_IsCharNumeric(ped, keyPress))
  2682. {
  2683. Edit_ShowBalloonTipWrap(ped->hwnd, IDS_NUMERIC_TITLE, IDS_NUMERIC_MSG, TTI_ERROR);
  2684. goto IllegalChar;
  2685. }
  2686. }
  2687. goto DeleteSelection;
  2688. }
  2689. break;
  2690. }
  2691. //
  2692. // Handle key codes
  2693. //
  2694. switch(keyPress)
  2695. {
  2696. UINT msg;
  2697. // Ctrl+Z == Undo
  2698. case 26:
  2699. msg = WM_UNDO;
  2700. goto SendEditingMessage;
  2701. break;
  2702. // Ctrl+X == Cut
  2703. case 24:
  2704. if (ped->ichMinSel == ped->ichMaxSel)
  2705. {
  2706. goto IllegalChar;
  2707. }
  2708. else
  2709. {
  2710. msg = WM_CUT;
  2711. goto SendEditingMessage;
  2712. }
  2713. break;
  2714. // Ctrl+C == Copy
  2715. case 3:
  2716. msg = WM_COPY;
  2717. goto SendEditingMessage;
  2718. break;
  2719. // Ctrl+V == Paste
  2720. case 22:
  2721. msg = WM_PASTE;
  2722. SendEditingMessage:
  2723. SendMessage(ped->hwnd, msg, 0, 0L);
  2724. break;
  2725. case VK_BACK:
  2726. //
  2727. // Delete any selected text or delete character left if no sel
  2728. //
  2729. if (!updateText && ped->ichMinSel)
  2730. {
  2731. //
  2732. // There was no selection to delete so we just delete
  2733. // character left if available
  2734. //
  2735. ped->ichMinSel = Edit_MoveSelection(ped, ped->ichCaret, TRUE);
  2736. EditML_DeleteText(ped);
  2737. }
  2738. break;
  2739. default:
  2740. if (keyPress == VK_RETURN)
  2741. {
  2742. if (ped->fAnsi)
  2743. {
  2744. keyValue = 0x0A0D;
  2745. }
  2746. else
  2747. {
  2748. keyValue = 0x000A000D;
  2749. }
  2750. }
  2751. if ( keyPress >= TEXT(' ')
  2752. || keyPress == VK_RETURN
  2753. || keyPress == VK_TAB
  2754. || keyPress == 0x1E // RS - Unicode block separator
  2755. || keyPress == 0x1F // US - Unicode segment separator
  2756. )
  2757. {
  2758. // Don't hide the cursor if someone has capture.
  2759. if (GetCapture() == NULL)
  2760. {
  2761. SetCursor(NULL);
  2762. }
  2763. if (ped->fAnsi)
  2764. {
  2765. //
  2766. // check if it's a leading byte of double byte character
  2767. //
  2768. if (Edit_IsDBCSLeadByte(ped,(BYTE)keyPress))
  2769. {
  2770. int DBCSkey;
  2771. DBCSkey = DbcsCombine(ped->hwnd, keyPress);
  2772. if ( DBCSkey != 0)
  2773. {
  2774. keyValue = DBCSkey;
  2775. }
  2776. }
  2777. EditML_InsertText(ped, (LPSTR)&keyValue, HIBYTE(keyValue) ? 2 : 1, TRUE);
  2778. }
  2779. else
  2780. {
  2781. EditML_InsertText(ped, (LPSTR)&keyValue, HIWORD(keyValue) ? 2 : 1, TRUE);
  2782. }
  2783. }
  2784. else
  2785. {
  2786. IllegalChar:
  2787. MessageBeep(0);
  2788. }
  2789. break;
  2790. }
  2791. }
  2792. //---------------------------------------------------------------------------//
  2793. //
  2794. // EditML_PasteText AorW
  2795. //
  2796. // Pastes a line of text from the clipboard into the edit control
  2797. // starting at ped->ichCaret. Updates ichMaxSel and ichMinSel to point to the
  2798. // end of the inserted text. Notifies the parent if space cannot be
  2799. // allocated. Returns how many characters were inserted.
  2800. //
  2801. ICH EditML_PasteText(PED ped)
  2802. {
  2803. HANDLE hData;
  2804. LPSTR lpchClip;
  2805. ICH cchAdded = 0;
  2806. HCURSOR hCursorOld;
  2807. #ifdef UNDO_CLEANUP // #ifdef Added in Chicago - johnl
  2808. if (!ped->fAutoVScroll)
  2809. {
  2810. //
  2811. // Empty the undo buffer if this edit control limits the amount of text
  2812. // the user can add to the window rect. This is so that we can undo this
  2813. // operation if doing in causes us to exceed the window boundaries.
  2814. //
  2815. Edit_EmptyUndo(ped);
  2816. }
  2817. #endif
  2818. hCursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
  2819. if (!OpenClipboard(ped->hwnd))
  2820. {
  2821. goto PasteExitNoCloseClip;
  2822. }
  2823. if (!(hData = GetClipboardData(ped->fAnsi ? CF_TEXT : CF_UNICODETEXT)) ||
  2824. (GlobalFlags(hData) == GMEM_INVALID_HANDLE))
  2825. {
  2826. TraceMsg(TF_STANDARD, "Edit: EditML_PasteText(): couldn't get a valid handle(%x)", hData);
  2827. goto PasteExit;
  2828. }
  2829. //
  2830. // See if any text should be deleted
  2831. //
  2832. EditML_DeleteText(ped);
  2833. lpchClip = GlobalLock(hData);
  2834. if (lpchClip == NULL)
  2835. {
  2836. TraceMsg(TF_STANDARD, "Edit: EditML_PasteText: USERGLOBALLOCK(%x) failed.", hData);
  2837. goto PasteExit;
  2838. }
  2839. //
  2840. // Get the length of the addition.
  2841. //
  2842. if (ped->fAnsi)
  2843. {
  2844. cchAdded = strlen(lpchClip);
  2845. }
  2846. else
  2847. {
  2848. cchAdded = wcslen((LPWSTR)lpchClip);
  2849. }
  2850. //
  2851. // Insert the text (EditML_InsertText checks line length)
  2852. //
  2853. cchAdded = EditML_InsertText(ped, lpchClip, cchAdded, FALSE);
  2854. GlobalUnlock(hData);
  2855. PasteExit:
  2856. CloseClipboard();
  2857. PasteExitNoCloseClip:
  2858. SetCursor(hCursorOld);
  2859. return cchAdded;
  2860. }
  2861. //---------------------------------------------------------------------------//
  2862. VOID EditML_MouseMotion(PED ped, UINT message, UINT virtKeyDown, LPPOINT mousePt)
  2863. {
  2864. BOOL fChangedSel = FALSE;
  2865. UINT dtScroll = GetDoubleClickTime() / 5;
  2866. HDC hdc = Edit_GetDC(ped, TRUE);
  2867. ICH ichMaxSel = ped->ichMaxSel;
  2868. ICH ichMinSel = ped->ichMinSel;
  2869. ICH mouseCch;
  2870. ICH mouseLine;
  2871. int i, j;
  2872. LONG ll, lh;
  2873. mouseCch = EditML_MouseToIch(ped, hdc, mousePt, &mouseLine);
  2874. //
  2875. // Save for timer
  2876. //
  2877. ped->ptPrevMouse = *mousePt;
  2878. ped->prevKeys = virtKeyDown;
  2879. switch (message)
  2880. {
  2881. case WM_LBUTTONDBLCLK:
  2882. //
  2883. // if shift key is down, extend selection to word we double clicked on
  2884. // else clear current selection and select word.
  2885. // LiZ -- 5/5/93
  2886. //
  2887. if (ped->fAnsi && ped->fDBCS)
  2888. {
  2889. LPSTR pText = Edit_Lock(ped);
  2890. Edit_Word(ped,ped->ichCaret,
  2891. Edit_IsDBCSLeadByte(ped, *(pText+(ped->ichCaret)))
  2892. ? FALSE :
  2893. (ped->ichCaret == ped->chLines[ped->iCaretLine]
  2894. ? FALSE : TRUE), &ll, &lh);
  2895. Edit_Unlock(ped);
  2896. }
  2897. else
  2898. {
  2899. Edit_Word(ped, mouseCch, !(mouseCch == ped->chLines[mouseLine]), &ll, &lh);
  2900. }
  2901. if (!(virtKeyDown & MK_SHIFT))
  2902. {
  2903. //
  2904. // If shift key isn't down, move caret to mouse point and clear
  2905. // old selection
  2906. //
  2907. ichMinSel = ll;
  2908. ichMaxSel = ped->ichCaret = lh;
  2909. }
  2910. else
  2911. {
  2912. //
  2913. // Shiftkey is down so we want to maintain the current selection
  2914. // (if any) and just extend or reduce it
  2915. //
  2916. if (ped->ichMinSel == ped->ichCaret)
  2917. {
  2918. ichMinSel = ped->ichCaret = ll;
  2919. Edit_Word(ped, ichMaxSel, TRUE, &ll, &lh);
  2920. }
  2921. else
  2922. {
  2923. ichMaxSel = ped->ichCaret = lh;
  2924. Edit_Word(ped, ichMinSel, FALSE, &ll, &lh);
  2925. }
  2926. }
  2927. ped->ichStartMinSel = ll;
  2928. ped->ichStartMaxSel = lh;
  2929. goto InitDragSelect;
  2930. case WM_MOUSEMOVE:
  2931. if (ped->fMouseDown)
  2932. {
  2933. //
  2934. // Set the system timer to automatically scroll when mouse is
  2935. // outside of the client rectangle. Speed of scroll depends on
  2936. // distance from window.
  2937. //
  2938. i = mousePt->y < 0 ? -mousePt->y : mousePt->y - ped->rcFmt.bottom;
  2939. j = dtScroll - ((UINT)i << 4);
  2940. if (j < 1)
  2941. {
  2942. j = 1;
  2943. }
  2944. SetTimer(ped->hwnd, IDSYS_SCROLL, (UINT)j, NULL);
  2945. fChangedSel = TRUE;
  2946. //
  2947. // Extend selection, move caret right
  2948. //
  2949. if (ped->ichStartMinSel || ped->ichStartMaxSel)
  2950. {
  2951. //
  2952. // We're in WORD SELECT mode
  2953. //
  2954. BOOL fReverse = (mouseCch <= ped->ichStartMinSel);
  2955. Edit_Word(ped, mouseCch, !fReverse, &ll, &lh);
  2956. if (fReverse)
  2957. {
  2958. ichMinSel = ped->ichCaret = ll;
  2959. ichMaxSel = ped->ichStartMaxSel;
  2960. }
  2961. else
  2962. {
  2963. ichMinSel = ped->ichStartMinSel;
  2964. ichMaxSel = ped->ichCaret = lh;
  2965. }
  2966. }
  2967. else if ((ped->ichMinSel == ped->ichCaret) &&
  2968. (ped->ichMinSel != ped->ichMaxSel))
  2969. {
  2970. //
  2971. // Reduce selection extent
  2972. //
  2973. ichMinSel = ped->ichCaret = mouseCch;
  2974. }
  2975. else
  2976. {
  2977. //
  2978. // Extend selection extent
  2979. //
  2980. ichMaxSel = ped->ichCaret = mouseCch;
  2981. }
  2982. ped->iCaretLine = mouseLine;
  2983. }
  2984. break;
  2985. case WM_LBUTTONDOWN:
  2986. ll = lh = mouseCch;
  2987. if (!(virtKeyDown & MK_SHIFT))
  2988. {
  2989. //
  2990. // If shift key isn't down, move caret to mouse point and clear
  2991. // old selection
  2992. //
  2993. ichMinSel = ichMaxSel = ped->ichCaret = mouseCch;
  2994. }
  2995. else
  2996. {
  2997. //
  2998. // Shiftkey is down so we want to maintain the current selection
  2999. // (if any) and just extend or reduce it
  3000. //
  3001. if (ped->ichMinSel == ped->ichCaret)
  3002. {
  3003. ichMinSel = ped->ichCaret = mouseCch;
  3004. }
  3005. else
  3006. {
  3007. ichMaxSel = ped->ichCaret = mouseCch;
  3008. }
  3009. }
  3010. ped->ichStartMinSel = ped->ichStartMaxSel = 0;
  3011. InitDragSelect:
  3012. ped->iCaretLine = mouseLine;
  3013. ped->fMouseDown = FALSE;
  3014. SetCapture(ped->hwnd);
  3015. ped->fMouseDown = TRUE;
  3016. fChangedSel = TRUE;
  3017. //
  3018. // Set the timer so that we can scroll automatically when the mouse
  3019. // is moved outside the window rectangle.
  3020. //
  3021. SetTimer(ped->hwnd, IDSYS_SCROLL, dtScroll, NULL);
  3022. break;
  3023. case WM_LBUTTONUP:
  3024. if (ped->fMouseDown)
  3025. {
  3026. //
  3027. // Kill the timer so that we don't do auto mouse moves anymore
  3028. //
  3029. KillTimer(ped->hwnd, IDSYS_SCROLL);
  3030. ReleaseCapture();
  3031. EditML_SetCaretPosition(ped, hdc);
  3032. ped->fMouseDown = FALSE;
  3033. }
  3034. break;
  3035. }
  3036. if (fChangedSel)
  3037. {
  3038. EditML_ChangeSelection(ped, hdc, ichMinSel, ichMaxSel);
  3039. EditML_EnsureCaretVisible(ped);
  3040. }
  3041. Edit_ReleaseDC(ped, hdc, TRUE);
  3042. if (!ped->fFocus && (message == WM_LBUTTONDOWN))
  3043. {
  3044. //
  3045. // If we don't have the focus yet, get it
  3046. //
  3047. SetFocus(ped->hwnd);
  3048. }
  3049. }
  3050. //---------------------------------------------------------------------------//
  3051. LONG EditML_Scroll(PED ped, BOOL fVertical, int cmd, int iAmt, BOOL fRedraw)
  3052. {
  3053. SCROLLINFO si;
  3054. int dx = 0;
  3055. int dy = 0;
  3056. BOOL fIncludeLeftMargin;
  3057. int newPos;
  3058. int oldPos;
  3059. BOOL fUp = FALSE;
  3060. UINT wFlag;
  3061. DWORD dwTime = 0;
  3062. if (fRedraw && (cmd != ML_REFRESH))
  3063. {
  3064. UpdateWindow(ped->hwnd);
  3065. }
  3066. if (ped->pLpkEditCallout && ped->fRtoLReading && !fVertical
  3067. && ped->maxPixelWidth > ped->rcFmt.right - ped->rcFmt.left)
  3068. {
  3069. //
  3070. // Horizontal scoll of a right oriented window with a scrollbar.
  3071. // Map the logical xOffset to visual coordinates.
  3072. //
  3073. oldPos = ped->maxPixelWidth
  3074. - ((int)ped->xOffset + ped->rcFmt.right - ped->rcFmt.left);
  3075. }
  3076. else
  3077. {
  3078. oldPos = (int) (fVertical ? ped->ichScreenStart : ped->xOffset);
  3079. }
  3080. fIncludeLeftMargin = (ped->xOffset == 0);
  3081. switch (cmd)
  3082. {
  3083. case ML_REFRESH:
  3084. newPos = oldPos;
  3085. break;
  3086. case EM_GETTHUMB:
  3087. return oldPos;
  3088. case SB_THUMBTRACK:
  3089. case SB_THUMBPOSITION:
  3090. //
  3091. // If the edit contains more than 0xFFFF lines
  3092. // it means that the scrolbar can return a position
  3093. // that cannot fit in a WORD (16 bits), so use
  3094. // GetScrollInfo (which is slower) in this case.
  3095. //
  3096. if (ped->cLines < 0xFFFF)
  3097. {
  3098. newPos = iAmt;
  3099. }
  3100. else
  3101. {
  3102. SCROLLINFO si;
  3103. si.cbSize = sizeof(SCROLLINFO);
  3104. si.fMask = SIF_TRACKPOS;
  3105. GetScrollInfo( ped->hwnd, SB_VERT, &si);
  3106. newPos = si.nTrackPos;
  3107. }
  3108. break;
  3109. case SB_TOP: // == SB_LEFT
  3110. newPos = 0;
  3111. break;
  3112. case SB_BOTTOM: // == SB_RIGHT
  3113. if (fVertical)
  3114. {
  3115. newPos = ped->cLines;
  3116. }
  3117. else
  3118. {
  3119. newPos = ped->maxPixelWidth;
  3120. }
  3121. break;
  3122. case SB_PAGEUP: // == SB_PAGELEFT
  3123. fUp = TRUE;
  3124. case SB_PAGEDOWN: // == SB_PAGERIGHT
  3125. if (fVertical)
  3126. {
  3127. iAmt = ped->ichLinesOnScreen - 1;
  3128. }
  3129. else
  3130. {
  3131. iAmt = (ped->rcFmt.right - ped->rcFmt.left) - 1;
  3132. }
  3133. if (iAmt == 0)
  3134. {
  3135. iAmt++;
  3136. }
  3137. if (fUp)
  3138. {
  3139. iAmt = -iAmt;
  3140. }
  3141. goto AddDelta;
  3142. case SB_LINEUP: // == SB_LINELEFT
  3143. fUp = TRUE;
  3144. case SB_LINEDOWN: // == SB_LINERIGHT
  3145. dwTime = iAmt;
  3146. iAmt = 1;
  3147. if (fUp)
  3148. {
  3149. iAmt = -iAmt;
  3150. }
  3151. // | |
  3152. // | FALL THRU |
  3153. // V V
  3154. case EM_LINESCROLL:
  3155. if (!fVertical)
  3156. {
  3157. iAmt *= ped->aveCharWidth;
  3158. }
  3159. AddDelta:
  3160. newPos = oldPos + iAmt;
  3161. break;
  3162. default:
  3163. return(0L);
  3164. }
  3165. if (fVertical)
  3166. {
  3167. if (si.nMax = ped->cLines)
  3168. {
  3169. si.nMax--;
  3170. }
  3171. if (!ped->hwndParent ||
  3172. TestWF(ped->hwndParent, WFWIN40COMPAT))
  3173. {
  3174. si.nPage = ped->ichLinesOnScreen;
  3175. }
  3176. else
  3177. {
  3178. si.nPage = 0;
  3179. }
  3180. wFlag = WS_VSCROLL;
  3181. }
  3182. else
  3183. {
  3184. si.nMax = ped->maxPixelWidth;
  3185. si.nPage = ped->rcFmt.right - ped->rcFmt.left;
  3186. wFlag = WS_HSCROLL;
  3187. }
  3188. if (TESTFLAG(GET_STYLE(ped), wFlag))
  3189. {
  3190. si.cbSize = sizeof(SCROLLINFO);
  3191. si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
  3192. si.nMin = 0;
  3193. si.nPos = newPos;
  3194. newPos = SetScrollInfo(ped->hwnd, fVertical ? SB_VERT : SB_HORZ,
  3195. &si, fRedraw);
  3196. }
  3197. else
  3198. {
  3199. //
  3200. // BOGUS -- this is duped code from ScrollBar code
  3201. // but it's for the case when we want to limit the position without
  3202. // actually having the scroll bar
  3203. //
  3204. int iMaxPos;
  3205. //
  3206. // Clip page to 0, range + 1
  3207. //
  3208. si.nPage = max(min((int)si.nPage, si.nMax + 1), 0);
  3209. iMaxPos = si.nMax - (si.nPage ? si.nPage - 1 : 0);
  3210. newPos = min(max(newPos, 0), iMaxPos);
  3211. }
  3212. oldPos -= newPos;
  3213. if (!oldPos)
  3214. {
  3215. return 0;
  3216. }
  3217. if (ped->pLpkEditCallout && ped->fRtoLReading && !fVertical
  3218. && ped->maxPixelWidth > ped->rcFmt.right - ped->rcFmt.left)
  3219. {
  3220. //
  3221. // Map visual oldPos and newPos back to logical coordinates
  3222. //
  3223. newPos = ped->maxPixelWidth
  3224. - (newPos + ped->rcFmt.right - ped->rcFmt.left);
  3225. oldPos = -oldPos;
  3226. if (newPos<0)
  3227. {
  3228. //
  3229. // Compensate for scroll bar returning pos > max-page
  3230. //
  3231. oldPos += newPos;
  3232. newPos=0;
  3233. }
  3234. }
  3235. if (fVertical)
  3236. {
  3237. ped->ichScreenStart = newPos;
  3238. dy = oldPos * ped->lineHeight;
  3239. }
  3240. else
  3241. {
  3242. ped->xOffset = newPos;
  3243. dx = oldPos;
  3244. }
  3245. if (cmd != SB_THUMBTRACK)
  3246. {
  3247. //
  3248. // We don't want to notify the parent of thumbtracking since they might
  3249. // try to set the thumb position to something bogus.
  3250. // NOTEPAD used to be guilty of this -- but I rewrote it so it's not.
  3251. // The question is WHO ELSE does this? (jeffbog)
  3252. //
  3253. Edit_NotifyParent(ped, fVertical ? EN_VSCROLL : EN_HSCROLL);
  3254. }
  3255. if (fRedraw && IsWindowVisible(ped->hwnd))
  3256. {
  3257. RECT rc;
  3258. RECT rcUpdate;
  3259. RECT rcClipRect;
  3260. HDC hdc;
  3261. HBRUSH hbr = NULL;
  3262. BOOL fNeedDelete = FALSE;
  3263. GetClientRect(ped->hwnd, &rc);
  3264. CopyRect(&rcClipRect, &ped->rcFmt);
  3265. if (fVertical)
  3266. {
  3267. rcClipRect.left -= ped->wLeftMargin;
  3268. rcClipRect.right += ped->wRightMargin;
  3269. }
  3270. IntersectRect(&rc, &rc, &rcClipRect);
  3271. rc.bottom++;
  3272. //
  3273. // Chicago has this HideCaret but there doesn't appear to be a
  3274. // corresponding ShowCaret, so we lose the Caret under NT when the
  3275. // EC scrolls - Johnl
  3276. //
  3277. // HideCaret(ped->hwnd);
  3278. hdc = Edit_GetDC(ped, FALSE);
  3279. Edit_SetClip(ped, hdc, fIncludeLeftMargin);
  3280. if (ped->hFont)
  3281. {
  3282. SelectObject(hdc, ped->hFont);
  3283. }
  3284. hbr = Edit_GetBrush(ped, hdc, &fNeedDelete);
  3285. if (hbr && fNeedDelete)
  3286. {
  3287. DeleteObject(hbr);
  3288. }
  3289. if (ped->pLpkEditCallout && !fVertical)
  3290. {
  3291. //
  3292. // Horizontal scroll with complex script support
  3293. //
  3294. int xFarOffset = ped->xOffset + ped->rcFmt.right - ped->rcFmt.left;
  3295. rc = ped->rcFmt;
  3296. if (dwTime != 0)
  3297. {
  3298. ScrollWindowEx(ped->hwnd, ped->fRtoLReading ? -dx : dx, dy, NULL, NULL, NULL,
  3299. &rcUpdate, MAKELONG(SW_SMOOTHSCROLL | SW_SCROLLCHILDREN, dwTime));
  3300. }
  3301. else
  3302. {
  3303. ScrollDC(hdc, ped->fRtoLReading ? -dx : dx, dy,
  3304. &rc, &rc, NULL, &rcUpdate);
  3305. }
  3306. //
  3307. // Handle margins: Blank if clipped by horizontal scrolling,
  3308. // display otherwise.
  3309. //
  3310. if (ped->wLeftMargin)
  3311. {
  3312. rc.left = ped->rcFmt.left - ped->wLeftMargin;
  3313. rc.right = ped->rcFmt.left;
  3314. if ( (ped->format != ES_LEFT) // Always display margin for centred or far-aligned text
  3315. || // Display LTR left margin if first character fully visible
  3316. (!ped->fRtoLReading && ped->xOffset == 0)
  3317. || // Display RTL left margin if last character fully visible
  3318. (ped->fRtoLReading && xFarOffset >= ped->maxPixelWidth))
  3319. {
  3320. UnionRect(&rcUpdate, &rcUpdate, &rc);
  3321. }
  3322. else
  3323. {
  3324. ExtTextOutW(hdc, rc.left, rc.top,
  3325. ETO_CLIPPED | ETO_OPAQUE | ETO_GLYPH_INDEX,
  3326. &rc, L"", 0, 0L);
  3327. }
  3328. }
  3329. if (ped->wRightMargin)
  3330. {
  3331. rc.left = ped->rcFmt.right;
  3332. rc.right = ped->rcFmt.right + ped->wRightMargin;
  3333. if ( (ped->format != ES_LEFT) // Always display margin for centred or far-aligned text
  3334. || // Display RTL right margin if first character fully visible
  3335. (ped->fRtoLReading && ped->xOffset == 0)
  3336. || // Display LTR right margin if last character fully visible
  3337. (!ped->fRtoLReading && xFarOffset >= ped->maxPixelWidth))
  3338. {
  3339. UnionRect(&rcUpdate, &rcUpdate, &rc);
  3340. }
  3341. else
  3342. {
  3343. ExtTextOutW(hdc, rc.left, rc.top,
  3344. ETO_CLIPPED | ETO_OPAQUE | ETO_GLYPH_INDEX,
  3345. &rc, L"", 0, 0L);
  3346. }
  3347. }
  3348. }
  3349. else
  3350. {
  3351. if (dwTime != 0)
  3352. {
  3353. ScrollWindowEx(ped->hwnd, dx, dy, NULL, NULL, NULL,
  3354. &rcUpdate, MAKELONG(SW_SMOOTHSCROLL | SW_SCROLLCHILDREN, dwTime));
  3355. }
  3356. else
  3357. {
  3358. ScrollDC(hdc, dx, dy, &rc, &rc, NULL, &rcUpdate);
  3359. }
  3360. //
  3361. // If we need to wipe out the left margin area
  3362. //
  3363. if (ped->wLeftMargin && !fVertical)
  3364. {
  3365. //
  3366. // Calculate the rectangle to be wiped out
  3367. //
  3368. rc.right = rc.left;
  3369. rc.left = max(0, ped->rcFmt.left - (LONG)ped->wLeftMargin);
  3370. if (rc.left < rc.right)
  3371. {
  3372. if (fIncludeLeftMargin && (ped->xOffset != 0))
  3373. {
  3374. ExtTextOutW(hdc, rc.left, rc.top, ETO_CLIPPED | ETO_OPAQUE,
  3375. &rc, L"", 0, 0L);
  3376. }
  3377. else
  3378. {
  3379. if((!fIncludeLeftMargin) && (ped->xOffset == 0))
  3380. {
  3381. UnionRect(&rcUpdate, &rcUpdate, &rc);
  3382. }
  3383. }
  3384. }
  3385. }
  3386. }
  3387. EditML_SetCaretPosition(ped,hdc);
  3388. Edit_ReleaseDC(ped, hdc, FALSE);
  3389. InvalidateRect(ped->hwnd, &rcUpdate, TRUE);
  3390. UpdateWindow(ped->hwnd);
  3391. }
  3392. return MAKELONG(-oldPos, 1);
  3393. }
  3394. //---------------------------------------------------------------------------//
  3395. //
  3396. // EditML_SetFocus AorW
  3397. //
  3398. // Gives the edit control the focus and notifies the parent
  3399. // EN_SETFOCUS.
  3400. //
  3401. void EditML_SetFocus(PED ped)
  3402. {
  3403. HDC hdc;
  3404. INT cxCaret;
  3405. SystemParametersInfo(SPI_GETCARETWIDTH, 0, (LPVOID)&cxCaret, 0);
  3406. if (!ped->fFocus)
  3407. {
  3408. ped->fFocus = TRUE;
  3409. InvalidateRect(ped->hwnd, NULL, TRUE);
  3410. hdc = Edit_GetDC(ped, TRUE);
  3411. //
  3412. // Draw the caret. We need to do this even if the window is hidden
  3413. // because in dlg box initialization time we may set the focus to a
  3414. // hidden edit control window. If we don't create the caret etc, it will
  3415. // never end up showing properly.
  3416. //
  3417. if (ped->pLpkEditCallout)
  3418. {
  3419. ped->pLpkEditCallout->EditCreateCaret((PED0)ped, hdc, cxCaret, ped->lineHeight, 0);
  3420. }
  3421. else
  3422. {
  3423. CreateCaret(ped->hwnd, (HBITMAP)NULL, cxCaret, ped->lineHeight);
  3424. }
  3425. ShowCaret(ped->hwnd);
  3426. EditML_SetCaretPosition(ped, hdc);
  3427. //
  3428. // Show the current selection. Only if the selection was hidden when we
  3429. // lost the focus, must we invert (show) it.
  3430. //
  3431. if (!ped->fNoHideSel && ped->ichMinSel != ped->ichMaxSel &&
  3432. IsWindowVisible(ped->hwnd))
  3433. {
  3434. EditML_DrawText(ped, hdc, ped->ichMinSel, ped->ichMaxSel, TRUE);
  3435. }
  3436. Edit_ReleaseDC(ped, hdc, TRUE);
  3437. }
  3438. #if 0
  3439. EditML_EnsureCaretVisible(ped);
  3440. #endif
  3441. //
  3442. // Notify parent we have the focus
  3443. //
  3444. Edit_NotifyParent(ped, EN_SETFOCUS);
  3445. }
  3446. //---------------------------------------------------------------------------//
  3447. //
  3448. // EditML_KillFocus AorW
  3449. //
  3450. // The edit control loses the focus and notifies the parent via
  3451. // EN_KILLFOCUS.
  3452. //
  3453. VOID EditML_KillFocus(PED ped)
  3454. {
  3455. HDC hdc;
  3456. //
  3457. // Reset the wheel delta count.
  3458. //
  3459. if (ped->fFocus)
  3460. {
  3461. ped->fFocus = 0;
  3462. //
  3463. // Do this only if we still have the focus. But we always notify the
  3464. // parent that we lost the focus whether or not we originally had the
  3465. // focus.
  3466. //
  3467. //
  3468. // Hide the current selection if needed
  3469. //
  3470. #ifdef _USE_DRAW_THEME_TEXT_
  3471. if (((!ped->fNoHideSel && ped->ichMinSel != ped->ichMaxSel &&
  3472. IsWindowVisible(ped->hwnd))) || ped->hTheme)
  3473. {
  3474. hdc = Edit_GetDC(ped, FALSE);
  3475. if ( !ped->hTheme )
  3476. {
  3477. EditML_DrawText(ped, hdc, ped->ichMinSel, ped->ichMaxSel, TRUE);
  3478. }
  3479. else
  3480. {
  3481. InvalidateRect(ped->hwnd, NULL, TRUE);
  3482. }
  3483. Edit_ReleaseDC(ped, hdc, FALSE);
  3484. }
  3485. #else
  3486. if (((!ped->fNoHideSel && ped->ichMinSel != ped->ichMaxSel &&
  3487. IsWindowVisible(ped->hwnd))))
  3488. {
  3489. hdc = Edit_GetDC(ped, FALSE);
  3490. EditML_DrawText(ped, hdc, ped->ichMinSel, ped->ichMaxSel, TRUE);
  3491. Edit_ReleaseDC(ped, hdc, FALSE);
  3492. }
  3493. #endif // _USE_DRAW_THEME_TEXT_
  3494. //
  3495. // Destroy the caret
  3496. //
  3497. DestroyCaret();
  3498. }
  3499. //
  3500. // Notify parent that we lost the focus.
  3501. //
  3502. Edit_NotifyParent(ped, EN_KILLFOCUS);
  3503. }
  3504. //---------------------------------------------------------------------------//
  3505. //
  3506. // EditML_EnsureCaretVisible AorW
  3507. //
  3508. // Scrolls the caret into the visible region.
  3509. // Returns TRUE if scrolling was done else return s FALSE.
  3510. //
  3511. BOOL EditML_EnsureCaretVisible(PED ped)
  3512. {
  3513. UINT iLineMax;
  3514. int xposition;
  3515. BOOL fPrevLine;
  3516. HDC hdc;
  3517. BOOL fVScroll = FALSE;
  3518. BOOL fHScroll = FALSE;
  3519. if (IsWindowVisible(ped->hwnd))
  3520. {
  3521. int iAmt;
  3522. int iFmtWidth = ped->rcFmt.right - ped->rcFmt.left;
  3523. if (ped->fAutoVScroll)
  3524. {
  3525. iLineMax = ped->ichScreenStart + ped->ichLinesOnScreen - 1;
  3526. if (fVScroll = (ped->iCaretLine > iLineMax))
  3527. {
  3528. iAmt = iLineMax;
  3529. }
  3530. else if (fVScroll = (ped->iCaretLine < ped->ichScreenStart))
  3531. {
  3532. iAmt = ped->ichScreenStart;
  3533. }
  3534. if (fVScroll)
  3535. {
  3536. EditML_Scroll(ped, TRUE, EM_LINESCROLL, ped->iCaretLine - iAmt, TRUE);
  3537. }
  3538. }
  3539. if (ped->fAutoHScroll && ((int) ped->maxPixelWidth > iFmtWidth))
  3540. {
  3541. POINT pt;
  3542. //
  3543. // Get the current position of the caret in pixels
  3544. //
  3545. if ((UINT) (ped->cLines - 1) != ped->iCaretLine &&
  3546. ped->ichCaret == ped->chLines[ped->iCaretLine + 1])
  3547. {
  3548. fPrevLine = TRUE;
  3549. }
  3550. else
  3551. {
  3552. fPrevLine = FALSE;
  3553. }
  3554. hdc = Edit_GetDC(ped,TRUE);
  3555. EditML_IchToXYPos(ped, hdc, ped->ichCaret, fPrevLine, &pt);
  3556. Edit_ReleaseDC(ped, hdc, TRUE);
  3557. xposition = pt.x;
  3558. //
  3559. // Remember, EditML_IchToXYPos returns coordinates with respect to the
  3560. // top left pixel displayed on the screen. Thus, if xPosition < 0,
  3561. // it means xPosition is less than current ped->xOffset.
  3562. //
  3563. iFmtWidth /= 3;
  3564. if (fHScroll = (xposition < ped->rcFmt.left))
  3565. {
  3566. //
  3567. // scroll to the left
  3568. //
  3569. iAmt = ped->rcFmt.left + iFmtWidth;
  3570. }
  3571. else if (fHScroll = (xposition > ped->rcFmt.right))
  3572. {
  3573. //
  3574. // scroll to the right
  3575. //
  3576. iAmt = ped->rcFmt.right - iFmtWidth;
  3577. }
  3578. if (fHScroll)
  3579. {
  3580. EditML_Scroll(ped, FALSE, EM_LINESCROLL, (xposition - iAmt) / ped->aveCharWidth, TRUE);
  3581. }
  3582. }
  3583. }
  3584. return fVScroll;
  3585. }
  3586. //---------------------------------------------------------------------------//
  3587. //
  3588. // EditML_WndProc
  3589. //
  3590. // Class procedure for all multi line edit controls.
  3591. // Dispatches all messages to the appropriate handlers which are named
  3592. // as follows:
  3593. //
  3594. // EditSL_ (single line) prefixes all single line edit control procedures while
  3595. // Edit_ (edit control) prefixes all common handlers.
  3596. //
  3597. // The EditML_WndProc only handles messages specific to multi line edit
  3598. // controls.
  3599. //
  3600. LRESULT EditML_WndProc(PED ped, UINT message, WPARAM wParam, LPARAM lParam)
  3601. {
  3602. HDC hdc;
  3603. PAINTSTRUCT ps;
  3604. LPRECT lprc;
  3605. POINT pt;
  3606. DWORD windowstyle;
  3607. static INT scWheelDelta;
  3608. switch (message)
  3609. {
  3610. case WM_INPUTLANGCHANGE:
  3611. if (ped && ped->fFocus && ped->pLpkEditCallout)
  3612. {
  3613. INT cxCaret;
  3614. SystemParametersInfo(SPI_GETCARETWIDTH, 0, (LPVOID)&cxCaret, 0);
  3615. HideCaret(ped->hwnd);
  3616. hdc = Edit_GetDC(ped, TRUE);
  3617. DestroyCaret();
  3618. ped->pLpkEditCallout->EditCreateCaret((PED0)ped, hdc, cxCaret, ped->lineHeight, (UINT)lParam);
  3619. EditML_SetCaretPosition(ped, hdc);
  3620. Edit_ReleaseDC(ped, hdc, TRUE);
  3621. ShowCaret(ped->hwnd);
  3622. }
  3623. goto PassToDefaultWindowProc;
  3624. case WM_STYLECHANGED:
  3625. if (ped && ped->pLpkEditCallout)
  3626. {
  3627. switch (wParam)
  3628. {
  3629. case GWL_STYLE:
  3630. Edit_UpdateFormat(ped,
  3631. ((LPSTYLESTRUCT)lParam)->styleNew,
  3632. GET_EXSTYLE(ped));
  3633. return 1L;
  3634. case GWL_EXSTYLE:
  3635. Edit_UpdateFormat(ped,
  3636. GET_STYLE(ped),
  3637. ((LPSTYLESTRUCT)lParam)->styleNew);
  3638. return 1L;
  3639. }
  3640. }
  3641. goto PassToDefaultWindowProc;
  3642. case WM_CHAR:
  3643. //
  3644. // wParam - the value of the key
  3645. // lParam - modifiers, repeat count etc (not used)
  3646. //
  3647. EditML_Char(ped, (UINT)wParam, 0);
  3648. break;
  3649. case WM_ERASEBKGND:
  3650. {
  3651. RECT rc;
  3652. GetClientRect(ped->hwnd, &rc);
  3653. #ifdef _USE_DRAW_THEME_TEXT_
  3654. if (!ped->hTheme)
  3655. #endif // _USE_DRAW_THEME_TEXT_
  3656. {
  3657. HBRUSH hbr = NULL;
  3658. BOOL fNeedDelete = FALSE;
  3659. hbr = Edit_GetBrush(ped, (HDC)wParam, &fNeedDelete);
  3660. if (hbr)
  3661. {
  3662. FillRect((HDC)wParam, &rc, hbr);
  3663. if (fNeedDelete)
  3664. {
  3665. DeleteObject(hbr);
  3666. }
  3667. }
  3668. }
  3669. #ifdef _USE_DRAW_THEME_TEXT_
  3670. else
  3671. {
  3672. HRESULT hr;
  3673. INT iStateId = Edit_GetStateId(ped);
  3674. hr = DrawThemeBackground(ped->hTheme, (HDC)wParam, EP_EDITTEXT, iStateId, &rc, 0);
  3675. }
  3676. #endif // _USE_DRAW_THEME_TEXT_
  3677. return TRUE;
  3678. }
  3679. case WM_GETDLGCODE:
  3680. {
  3681. LONG code = DLGC_WANTCHARS | DLGC_HASSETSEL | DLGC_WANTARROWS | DLGC_WANTALLKEYS;
  3682. //
  3683. // !!! JEFFBOG HACK !!!
  3684. // Only set Dialog Box Flag if GETDLGCODE message is generated by
  3685. // IsDialogMessage -- if so, the lParam will be a pointer to the
  3686. // message structure passed to IsDialogMessage; otherwise, lParam
  3687. // will be NULL. Reason for the HACK alert: the wParam & lParam
  3688. // for GETDLGCODE is still not clearly defined and may end up
  3689. // changing in a way that would throw this off
  3690. //
  3691. if (lParam)
  3692. {
  3693. // Mark ML edit ctrl as in a dialog box
  3694. ped->fInDialogBox = TRUE;
  3695. }
  3696. //
  3697. // If this is a WM_SYSCHAR message generated by the UNDO keystroke
  3698. // we want this message so we can EAT IT in "case WM_SYSCHAR:"
  3699. //
  3700. if (lParam && (((LPMSG)lParam)->message == WM_SYSCHAR) &&
  3701. ((DWORD)((LPMSG)lParam)->lParam & SYS_ALTERNATE) &&
  3702. ((WORD)wParam == VK_BACK))
  3703. {
  3704. code |= DLGC_WANTMESSAGE;
  3705. }
  3706. return code;
  3707. }
  3708. case EM_SCROLL:
  3709. message = WM_VSCROLL;
  3710. //
  3711. // FALL THROUGH
  3712. //
  3713. case WM_HSCROLL:
  3714. case WM_VSCROLL:
  3715. return EditML_Scroll(ped, (message==WM_VSCROLL), LOWORD(wParam), HIWORD(wParam), TRUE);
  3716. case WM_MOUSEWHEEL:
  3717. {
  3718. UINT ucWheelScrollLines;
  3719. //
  3720. // Don't handle zoom and datazoom.
  3721. //
  3722. if (wParam & (MK_SHIFT | MK_CONTROL))
  3723. {
  3724. goto PassToDefaultWindowProc;
  3725. }
  3726. scWheelDelta -= (short) HIWORD(wParam);
  3727. windowstyle = GET_STYLE(ped);
  3728. SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, (LPVOID)&ucWheelScrollLines, 0);
  3729. if ( abs(scWheelDelta) >= WHEEL_DELTA &&
  3730. ucWheelScrollLines > 0 &&
  3731. (windowstyle & (WS_VSCROLL | WS_HSCROLL)))
  3732. {
  3733. int cLineScroll;
  3734. BOOL fVert;
  3735. int cPage;
  3736. if (windowstyle & WS_VSCROLL)
  3737. {
  3738. fVert = TRUE;
  3739. cPage = ped->ichLinesOnScreen;
  3740. }
  3741. else
  3742. {
  3743. fVert = FALSE;
  3744. cPage = (ped->rcFmt.right - ped->rcFmt.left) / ped->aveCharWidth;
  3745. }
  3746. //
  3747. // Limit a roll of one (1) WHEEL_DELTA to scroll one (1) page.
  3748. //
  3749. cLineScroll = (int) min(
  3750. (UINT) (max(1, (cPage - 1))),
  3751. ucWheelScrollLines);
  3752. cLineScroll *= (scWheelDelta / WHEEL_DELTA);
  3753. UserAssert(cLineScroll != 0);
  3754. scWheelDelta = scWheelDelta % WHEEL_DELTA;
  3755. EditML_Scroll(ped, fVert, EM_LINESCROLL, cLineScroll, TRUE);
  3756. }
  3757. break;
  3758. }
  3759. case WM_KEYDOWN:
  3760. //
  3761. // wParam - virt keycode of the given key
  3762. // lParam - modifiers such as repeat count etc. (not used)
  3763. //
  3764. EditML_KeyDown(ped, (UINT)wParam, 0);
  3765. break;
  3766. case WM_KILLFOCUS:
  3767. //
  3768. // wParam - handle of the window that receives the input focus
  3769. // lParam - not used
  3770. //
  3771. scWheelDelta = 0;
  3772. EditML_KillFocus(ped);
  3773. break;
  3774. case WM_CAPTURECHANGED:
  3775. //
  3776. // wParam -- unused
  3777. // lParam -- hwnd of window gaining capture.
  3778. //
  3779. if (ped->fMouseDown)
  3780. {
  3781. //
  3782. // We don't change the caret pos here. If this is happening
  3783. // due to button up, then we'll change the pos in the
  3784. // handler after ReleaseCapture(). Otherwise, just end
  3785. // gracefully because someone else has stolen capture out
  3786. // from under us.
  3787. //
  3788. ped->fMouseDown = FALSE;
  3789. KillTimer(ped->hwnd, IDSYS_SCROLL);
  3790. }
  3791. break;
  3792. case WM_SYSTIMER:
  3793. //
  3794. // This allows us to automatically scroll if the user holds the mouse
  3795. // outside the edit control window. We simulate mouse moves at timer
  3796. // intervals set in MouseMotionHandler.
  3797. //
  3798. if (ped->fMouseDown)
  3799. {
  3800. EditML_MouseMotion(ped, WM_MOUSEMOVE, ped->prevKeys, &ped->ptPrevMouse);
  3801. }
  3802. break;
  3803. case WM_MBUTTONDOWN:
  3804. EnterReaderMode(ped->hwnd);
  3805. break;
  3806. case WM_MOUSEMOVE:
  3807. UserAssert(ped->fMouseDown);
  3808. //
  3809. // FALL THROUGH
  3810. //
  3811. case WM_LBUTTONDBLCLK:
  3812. case WM_LBUTTONDOWN:
  3813. case WM_LBUTTONUP:
  3814. //
  3815. // wParam - contains a value that indicates which virtual keys are down
  3816. // lParam - contains x and y coords of the mouse cursor
  3817. //
  3818. POINTSTOPOINT(pt, lParam);
  3819. EditML_MouseMotion(ped, message, (UINT)wParam, &pt);
  3820. break;
  3821. case WM_CREATE:
  3822. //
  3823. // wParam - handle to window being created
  3824. // lParam - points to a CREATESTRUCT that contains copies of parameters
  3825. // passed to the CreateWindow function.
  3826. //
  3827. return EditML_Create(ped, (LPCREATESTRUCT)lParam);
  3828. case WM_PRINTCLIENT:
  3829. EditML_Paint(ped, (HDC) wParam, NULL);
  3830. break;
  3831. case WM_PAINT:
  3832. //
  3833. // wParam - can be hdc from subclassed paint
  3834. // lParam - not used
  3835. //
  3836. if (wParam)
  3837. {
  3838. hdc = (HDC)wParam;
  3839. lprc = NULL;
  3840. }
  3841. else
  3842. {
  3843. hdc = BeginPaint(ped->hwnd, &ps);
  3844. lprc = &ps.rcPaint;
  3845. }
  3846. if (IsWindowVisible(ped->hwnd))
  3847. {
  3848. EditML_Paint(ped, hdc, lprc);
  3849. }
  3850. if (!wParam)
  3851. {
  3852. EndPaint(ped->hwnd, &ps);
  3853. }
  3854. break;
  3855. case WM_PASTE:
  3856. //
  3857. // wParam - not used
  3858. // lParam - not used
  3859. //
  3860. if (!ped->fReadOnly)
  3861. {
  3862. EditML_PasteText(ped);
  3863. }
  3864. break;
  3865. case WM_SETFOCUS:
  3866. //
  3867. // wParam - handle of window that loses the input focus (may be NULL)
  3868. // lParam - not used
  3869. //
  3870. EditML_SetFocus(ped);
  3871. break;
  3872. case WM_SIZE:
  3873. //
  3874. // wParam - defines the type of resizing fullscreen, sizeiconic,
  3875. // sizenormal etc.
  3876. // lParam - new width in LOWORD, new height in HIGHWORD of client area
  3877. //
  3878. Edit_Size(ped, NULL, TRUE);
  3879. break;
  3880. case EM_FMTLINES:
  3881. //
  3882. // wParam - indicates disposition of end-of-line chars. If non
  3883. // zero, the chars CR CR LF are placed at the end of a word
  3884. // wrapped line. If wParam is zero, the end of line chars are
  3885. // removed. This is only done when the user gets a handle (via
  3886. // EM_GETHANDLE) to the text. lParam - not used.
  3887. //
  3888. if (wParam)
  3889. {
  3890. EditML_InsertCrCrLf(ped);
  3891. }
  3892. else
  3893. {
  3894. EditML_StripCrCrLf(ped);
  3895. }
  3896. EditML_BuildchLines(ped, 0, 0, FALSE, NULL, NULL);
  3897. return (LONG)(wParam != 0);
  3898. case EM_GETHANDLE:
  3899. //
  3900. // wParam - not used
  3901. // lParam - not used
  3902. //
  3903. //
  3904. // Returns a handle to the edit control's text.
  3905. //
  3906. //
  3907. // Null terminate the string. Note that we are guaranteed to have the
  3908. // memory for the NULL since Edit_InsertText allocates an extra
  3909. // WCHAR for the NULL terminator.
  3910. //
  3911. if (ped->fAnsi)
  3912. {
  3913. *(Edit_Lock(ped) + ped->cch) = 0;
  3914. }
  3915. else
  3916. {
  3917. *((LPWSTR)Edit_Lock(ped) + ped->cch) = 0;
  3918. }
  3919. Edit_Unlock(ped);
  3920. return ((LRESULT)ped->hText);
  3921. case EM_GETLINE:
  3922. //
  3923. // wParam - line number to copy (0 is first line)
  3924. // lParam - buffer to copy text to. First WORD is max # of bytes to
  3925. // copy
  3926. //
  3927. return EditML_GetLine(ped, (ICH)wParam, (ICH)*(WORD UNALIGNED *)lParam, (LPSTR)lParam);
  3928. case EM_LINEFROMCHAR:
  3929. //
  3930. // wParam - Contains the index value for the desired char in the text
  3931. // of the edit control. These are 0 based.
  3932. // lParam - not used
  3933. //
  3934. return (LRESULT)EditML_IchToLine(ped, (ICH)wParam);
  3935. case EM_LINEINDEX:
  3936. //
  3937. // wParam - specifies the desired line number where the number of the
  3938. // first line is 0. If linenumber = 0, the line with the caret is used.
  3939. // lParam - not used.
  3940. // This function return s the number of character positions that occur
  3941. // preceeding the first char in a given line.
  3942. //
  3943. return (LRESULT)EditML_LineIndex(ped, (ICH)wParam);
  3944. case EM_LINELENGTH:
  3945. //
  3946. // wParam - specifies the character index of a character in the
  3947. // specified line, where the first line is 0. If -1, the length
  3948. // of the current line (with the caret) is return ed not including the
  3949. // length of any selected text.
  3950. // lParam - not used
  3951. //
  3952. return (LRESULT)EditML_LineLength(ped, (ICH)wParam);
  3953. case EM_LINESCROLL:
  3954. //
  3955. // wParam - not used
  3956. // lParam - Contains the number of lines and char positions to scroll
  3957. //
  3958. EditML_Scroll(ped, TRUE, EM_LINESCROLL, (INT)lParam, TRUE);
  3959. EditML_Scroll(ped, FALSE, EM_LINESCROLL, (INT)wParam, TRUE);
  3960. break;
  3961. case EM_REPLACESEL:
  3962. //
  3963. // wParam - flag for 4.0+ apps saying whether to clear undo
  3964. // lParam - Points to a null terminated replacement text.
  3965. //
  3966. EditML_ReplaceSel(ped, (LPSTR)lParam);
  3967. if (!ped->f40Compat || !wParam)
  3968. {
  3969. Edit_EmptyUndo(Pundo(ped));
  3970. }
  3971. break;
  3972. case EM_SETHANDLE:
  3973. //
  3974. // wParam - contains a handle to the text buffer
  3975. // lParam - not used
  3976. //
  3977. EditML_SetHandle(ped, (HANDLE)wParam);
  3978. break;
  3979. case EM_SETRECT:
  3980. case EM_SETRECTNP:
  3981. //
  3982. // wParamLo -- not used
  3983. // lParam -- LPRECT with new formatting area
  3984. //
  3985. Edit_Size(ped, (LPRECT) lParam, (message != EM_SETRECTNP));
  3986. break;
  3987. case EM_SETSEL:
  3988. //
  3989. // wParam - Under 3.1, specifies if we should scroll caret into
  3990. // view or not. 0 == scroll into view. 1 == don't scroll
  3991. // lParam - starting pos in lowword ending pos in high word
  3992. //
  3993. // Under Win32, wParam is the starting pos, lParam is the
  3994. // ending pos, and the caret is not scrolled into view.
  3995. // The message EM_SCROLLCARET forces the caret to be scrolled
  3996. // into view.
  3997. //
  3998. EditML_SetSelection(ped, TRUE, (ICH)wParam, (ICH)lParam);
  3999. break;
  4000. case EM_SCROLLCARET:
  4001. //
  4002. // Scroll caret into view
  4003. //
  4004. EditML_EnsureCaretVisible(ped);
  4005. break;
  4006. case EM_GETFIRSTVISIBLELINE:
  4007. //
  4008. // Returns the first visible line for multiline edit controls.
  4009. //
  4010. return (LONG)ped->ichScreenStart;
  4011. case WM_SYSKEYDOWN:
  4012. if (((WORD)wParam == VK_BACK) && ((DWORD)lParam & SYS_ALTERNATE))
  4013. {
  4014. SendMessage(ped->hwnd, EM_UNDO, 0, 0L);
  4015. break;
  4016. }
  4017. goto PassToDefaultWindowProc;
  4018. case WM_UNDO:
  4019. case EM_UNDO:
  4020. return EditML_Undo(ped);
  4021. case EM_SETTABSTOPS:
  4022. //
  4023. // This sets the tab stop positions for multiline edit controls.
  4024. // wParam - Number of tab stops
  4025. // lParam - Far ptr to a UINT array containing the Tab stop positions
  4026. //
  4027. return EditML_SetTabStops(ped, (int)wParam, (LPINT)lParam);
  4028. case EM_POSFROMCHAR:
  4029. //
  4030. // wParam -- char index in text
  4031. // lParam -- not used
  4032. // This function returns the (x,y) position of the character
  4033. //
  4034. case EM_CHARFROMPOS:
  4035. //
  4036. // wParam -- unused
  4037. // lParam -- pt in client coordinates
  4038. // This function returns
  4039. // LOWORD: the position of the closest character
  4040. // to the passed in point. Beware of
  4041. // points not actually in the edit client...
  4042. // HIWORD: the index of the line the char is on
  4043. //
  4044. {
  4045. LONG xyPos;
  4046. LONG line;
  4047. hdc = Edit_GetDC(ped, TRUE);
  4048. if (message == EM_POSFROMCHAR)
  4049. {
  4050. EditML_IchToXYPos(ped, hdc, (ICH)wParam, FALSE, &pt);
  4051. xyPos = MAKELONG(pt.x, pt.y);
  4052. }
  4053. else
  4054. {
  4055. POINTSTOPOINT(pt, lParam);
  4056. xyPos = EditML_MouseToIch(ped, hdc, &pt, &line);
  4057. xyPos = MAKELONG(xyPos, line);
  4058. }
  4059. Edit_ReleaseDC(ped, hdc, TRUE);
  4060. return (LRESULT)xyPos;
  4061. }
  4062. case WM_SETREDRAW:
  4063. DefWindowProc(ped->hwnd, message, wParam, lParam);
  4064. if (wParam)
  4065. {
  4066. //
  4067. // Backwards compatability hack needed so that winraid's edit
  4068. // controls work fine.
  4069. //
  4070. RedrawWindow(ped->hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME);
  4071. }
  4072. break;
  4073. default:
  4074. PassToDefaultWindowProc:
  4075. return DefWindowProc(ped->hwnd, message, wParam, lParam);
  4076. }
  4077. return TRUE;
  4078. }
  4079. //---------------------------------------------------------------------------//
  4080. //
  4081. // EditML_DrawText AorW
  4082. //
  4083. // This function draws all the characters between ichStart and ichEnd for
  4084. // the given Multiline Edit Control.
  4085. //
  4086. // This function divides the block of text between ichStart and ichEnd
  4087. // into lines and each line into strips of text based on the selection
  4088. // attributes. It calls Edit_TabTheTextOut() to draw each strip.
  4089. // This takes care of the Negative A anc C widths of the current font, if
  4090. // it has any, on either side of each strip of text.
  4091. //
  4092. // NOTE: If the language pack is loaded the text is not divided into strips,
  4093. // nor is selection highlighting performed here. Whole lines are passed
  4094. // to the language pack to display with tab expansion and selection
  4095. // highlighting. (Since the language pack supports scripts with complex
  4096. // character re-ordering rules, only it can do this).
  4097. //
  4098. VOID EditML_DrawText(PED ped, HDC hdc, ICH ichStart, ICH ichEnd, BOOL fSelChange)
  4099. {
  4100. DWORD textColorSave;
  4101. DWORD bkColorSave;
  4102. PSTR pText;
  4103. UINT wCurLine;
  4104. UINT wEndLine;
  4105. INT xOffset;
  4106. ICH LengthToDraw;
  4107. ICH CurStripLength;
  4108. ICH ichAttrib, ichNewStart;
  4109. ICH ExtraLengthForNegA;
  4110. ICH ichT;
  4111. INT iRemainingLengthInLine;
  4112. INT xStPos, xClipStPos, xClipEndPos, yPos;
  4113. BOOL fFirstLineOfBlock = TRUE;
  4114. BOOL fDrawEndOfLineStrip = FALSE;
  4115. BOOL fDrawOnSameLine = FALSE;
  4116. BOOL fSelected = FALSE;
  4117. BOOL fLineBegins = FALSE;
  4118. STRIPINFO NegCInfo;
  4119. POINT pt;
  4120. HBRUSH hbr = NULL;
  4121. BOOL fNeedDelete = FALSE;
  4122. //
  4123. // Just return if nothing to draw
  4124. //
  4125. if (!ped->ichLinesOnScreen)
  4126. {
  4127. return;
  4128. }
  4129. hbr = Edit_GetBrush(ped, hdc, &fNeedDelete);
  4130. if (hbr && fNeedDelete)
  4131. {
  4132. DeleteObject(hbr);
  4133. }
  4134. //
  4135. // Adjust the value of ichStart such that we need to draw only those lines
  4136. // visible on the screen.
  4137. //
  4138. if ((UINT)ichStart < (UINT)ped->chLines[ped->ichScreenStart])
  4139. {
  4140. ichStart = ped->chLines[ped->ichScreenStart];
  4141. if (ichStart > ichEnd)
  4142. {
  4143. return;
  4144. }
  4145. }
  4146. //
  4147. // Adjust the value of ichEnd such that we need to draw only those lines
  4148. // visible on the screen.
  4149. //
  4150. wCurLine = min(ped->ichScreenStart+ped->ichLinesOnScreen,ped->cLines-1);
  4151. ichT = ped->chLines[wCurLine] + EditML_Line(ped, wCurLine);
  4152. ichEnd = min(ichEnd, ichT);
  4153. wCurLine = EditML_IchToLine(ped, ichStart); // Starting line.
  4154. wEndLine = EditML_IchToLine(ped, ichEnd); // Ending line.
  4155. UserAssert(ped->chLines[wCurLine] <= ped->cch + 1);
  4156. UserAssert(ped->chLines[wEndLine] <= ped->cch + 1);
  4157. if (fSelChange && (GetBkMode(hdc) != OPAQUE))
  4158. {
  4159. //
  4160. // if changing selection on a transparent edit control, just
  4161. // draw those lines from scratch
  4162. //
  4163. RECT rcStrip;
  4164. CopyRect(&rcStrip, &ped->rcFmt);
  4165. rcStrip.left -= ped->wLeftMargin;
  4166. if (ped->pLpkEditCallout)
  4167. {
  4168. rcStrip.right += ped->wRightMargin;
  4169. }
  4170. rcStrip.top += (wCurLine - ped->ichScreenStart) * ped->lineHeight;
  4171. rcStrip.bottom = rcStrip.top + ((wEndLine - wCurLine) + 1) * ped->lineHeight;
  4172. InvalidateRect(ped->hwnd, &rcStrip, TRUE);
  4173. return;
  4174. }
  4175. //
  4176. // If it is either centered or right-justified, then draw the whole lines.
  4177. // Also draw whole lines if the language pack is handling line layout.
  4178. //
  4179. if ((ped->format != ES_LEFT) || (ped->pLpkEditCallout))
  4180. {
  4181. ichStart = ped->chLines[wCurLine];
  4182. ichEnd = ped->chLines[wEndLine] + EditML_Line(ped, wEndLine);
  4183. }
  4184. pText = Edit_Lock(ped);
  4185. HideCaret(ped->hwnd);
  4186. //
  4187. // If ichStart stays on Second byte of DBCS, we have to
  4188. // adjust it. LiZ -- 5/5/93
  4189. //
  4190. if (ped->fAnsi && ped->fDBCS)
  4191. {
  4192. ichStart = Edit_AdjustIch( ped, pText, ichStart );
  4193. }
  4194. UserAssert(ichStart <= ped->cch);
  4195. UserAssert(ichEnd <= ped->cch);
  4196. while (ichStart <= ichEnd)
  4197. {
  4198. //
  4199. // Pass whole lines to the language pack to display with selection
  4200. // marking and tab expansion.
  4201. //
  4202. if (ped->pLpkEditCallout)
  4203. {
  4204. ped->pLpkEditCallout->EditDrawText(
  4205. (PED0)ped, hdc, pText + ped->cbChar*ichStart,
  4206. EditML_Line(ped, wCurLine),
  4207. (INT)ped->ichMinSel - (INT)ichStart, (INT)ped->ichMaxSel - (INT)ichStart,
  4208. EditML_IchToYPos(ped, ichStart, FALSE));
  4209. }
  4210. else
  4211. {
  4212. //
  4213. // xStPos: The starting Position where the string must be drawn.
  4214. // xClipStPos: The starting position for the clipping rect for the block.
  4215. // xClipEndPos: The ending position for the clipping rect for the block.
  4216. //
  4217. //
  4218. // Calculate the xyPos of starting point of the block.
  4219. //
  4220. EditML_IchToXYPos(ped, hdc, ichStart, FALSE, &pt);
  4221. xClipStPos = xStPos = pt.x;
  4222. yPos = pt.y;
  4223. //
  4224. // The attributes of the block is the same as that of ichStart.
  4225. //
  4226. ichAttrib = ichStart;
  4227. //
  4228. // If the current font has some negative C widths and if this is the
  4229. // begining of a block, we must start drawing some characters before the
  4230. // block to account for the negative C widths of the strip before the
  4231. // current strip; In this case, reset ichStart and xStPos.
  4232. //
  4233. if (fFirstLineOfBlock && ped->wMaxNegC)
  4234. {
  4235. fFirstLineOfBlock = FALSE;
  4236. ichNewStart = max(((int)(ichStart - ped->wMaxNegCcharPos)), ((int)ped->chLines[wCurLine]));
  4237. //
  4238. // If ichStart needs to be changed, then change xStPos also accordingly.
  4239. //
  4240. if (ichNewStart != ichStart)
  4241. {
  4242. if (ped->fAnsi && ped->fDBCS)
  4243. {
  4244. //
  4245. // Adjust DBCS alignment...
  4246. //
  4247. ichNewStart = Edit_AdjustIchNext( ped, pText, ichNewStart );
  4248. }
  4249. EditML_IchToXYPos(ped, hdc, ichStart = ichNewStart, FALSE, &pt);
  4250. xStPos = pt.x;
  4251. }
  4252. }
  4253. //
  4254. // Calc the number of characters remaining to be drawn in the current line.
  4255. //
  4256. iRemainingLengthInLine = EditML_Line(ped, wCurLine) -
  4257. (ichStart - ped->chLines[wCurLine]);
  4258. //
  4259. // If this is the last line of a block, we may not have to draw all the
  4260. // remaining lines; We must draw only upto ichEnd.
  4261. //
  4262. if (wCurLine == wEndLine)
  4263. {
  4264. LengthToDraw = ichEnd - ichStart;
  4265. }
  4266. else
  4267. {
  4268. LengthToDraw = iRemainingLengthInLine;
  4269. }
  4270. //
  4271. // Find out how many pixels we indent the line for non-left-justified
  4272. // formats
  4273. //
  4274. if (ped->format != ES_LEFT)
  4275. {
  4276. xOffset = EditML_CalcXOffset(ped, hdc, wCurLine);
  4277. }
  4278. else
  4279. {
  4280. xOffset = -((int)(ped->xOffset));
  4281. }
  4282. //
  4283. // Check if this is the begining of a line.
  4284. //
  4285. if (ichAttrib == ped->chLines[wCurLine])
  4286. {
  4287. fLineBegins = TRUE;
  4288. xClipStPos = ped->rcFmt.left - ped->wLeftMargin;
  4289. }
  4290. //
  4291. // The following loop divides this 'wCurLine' into strips based on the
  4292. // selection attributes and draw them strip by strip.
  4293. //
  4294. do
  4295. {
  4296. //
  4297. // If ichStart is pointing at CRLF or CRCRLF, then iRemainingLength
  4298. // could have become negative because MLLine does not include
  4299. // CR and LF at the end of a line.
  4300. //
  4301. if (iRemainingLengthInLine < 0) // If Current line is completed,
  4302. {
  4303. break; // go on to the next line.
  4304. }
  4305. //
  4306. // Check if a part of the block is selected and if we need to
  4307. // show it with a different attribute.
  4308. //
  4309. if (!(ped->ichMinSel == ped->ichMaxSel ||
  4310. ichAttrib >= ped->ichMaxSel ||
  4311. ichEnd < ped->ichMinSel ||
  4312. (!ped->fNoHideSel && !ped->fFocus)))
  4313. {
  4314. //
  4315. // OK! There is a selection somewhere in this block!
  4316. // Check if this strip has selection attribute.
  4317. //
  4318. if (ichAttrib < ped->ichMinSel)
  4319. {
  4320. fSelected = FALSE; // This strip is not selected
  4321. // Calculate the length of this strip with normal attribute.
  4322. CurStripLength = min(ichStart+LengthToDraw, ped->ichMinSel)-ichStart;
  4323. fLineBegins = FALSE;
  4324. }
  4325. else
  4326. {
  4327. //
  4328. // The current strip has the selection attribute.
  4329. //
  4330. if (fLineBegins) // Is it the first part of a line?
  4331. {
  4332. //
  4333. // Then, draw the left margin area with normal attribute.
  4334. //
  4335. fSelected = FALSE;
  4336. CurStripLength = 0;
  4337. xClipStPos = ped->rcFmt.left - ped->wLeftMargin;
  4338. fLineBegins = FALSE;
  4339. }
  4340. else
  4341. {
  4342. //
  4343. // Else, draw the strip with selection attribute.
  4344. //
  4345. fSelected = TRUE;
  4346. CurStripLength = min(ichStart+LengthToDraw, ped->ichMaxSel)-ichStart;
  4347. //
  4348. // Select in the highlight colors.
  4349. //
  4350. bkColorSave = SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
  4351. if (!ped->fDisabled)
  4352. {
  4353. textColorSave = SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
  4354. }
  4355. }
  4356. }
  4357. }
  4358. else
  4359. {
  4360. //
  4361. // The whole strip has no selection attributes.
  4362. //
  4363. CurStripLength = LengthToDraw;
  4364. }
  4365. //
  4366. // Other than the current strip, do we still have anything
  4367. // left to be drawn in the current line?
  4368. //
  4369. fDrawOnSameLine = (LengthToDraw != CurStripLength);
  4370. //
  4371. // When we draw this strip, we need to draw some more characters
  4372. // beyond the end of this strip to account for the negative A
  4373. // widths of the characters that follow this strip.
  4374. //
  4375. ExtraLengthForNegA = min(iRemainingLengthInLine-CurStripLength, ped->wMaxNegAcharPos);
  4376. //
  4377. // The blank strip at the end of the line needs to be drawn with
  4378. // normal attribute irrespective of whether the line has selection
  4379. // attribute or not. Hence, if the last strip of the line has selection
  4380. // attribute, then this blank strip needs to be drawn separately.
  4381. // Else, we can draw the blank strip along with the last strip.
  4382. //
  4383. //
  4384. // Is this the last strip of the current line?
  4385. //
  4386. if (iRemainingLengthInLine == (int)CurStripLength)
  4387. {
  4388. if (fSelected) // Does this strip have selection attribute?
  4389. {
  4390. //
  4391. // Then we need to draw the end of line strip separately.
  4392. //
  4393. fDrawEndOfLineStrip = TRUE; // Draw the end of line strip.
  4394. EditML_IchToXYPos(ped, hdc, ichStart+CurStripLength, TRUE, &pt);
  4395. xClipEndPos = pt.x;
  4396. }
  4397. else
  4398. {
  4399. //
  4400. // Set the xClipEndPos to a big value sothat the blank
  4401. // strip will be drawn automatically when the last strip
  4402. // is drawn.
  4403. //
  4404. xClipEndPos = MAXCLIPENDPOS;
  4405. }
  4406. }
  4407. else
  4408. {
  4409. //
  4410. // This is not the last strip of this line; So, set the ending
  4411. // clip position accurately.
  4412. //
  4413. EditML_IchToXYPos(ped, hdc, ichStart+CurStripLength, FALSE, &pt);
  4414. xClipEndPos = pt.x;
  4415. }
  4416. //
  4417. // Draw the current strip starting from xStPos, clipped to the area
  4418. // between xClipStPos and xClipEndPos. Obtain "NegCInfo" and use it
  4419. // in drawing the next strip.
  4420. //
  4421. Edit_TabTheTextOut(hdc, xClipStPos, xClipEndPos,
  4422. xStPos, yPos, (LPSTR)(pText+ichStart*ped->cbChar),
  4423. CurStripLength+ExtraLengthForNegA, ichStart, ped,
  4424. ped->rcFmt.left+xOffset, fSelected ? ECT_SELECTED : ECT_NORMAL, &NegCInfo);
  4425. if (fSelected)
  4426. {
  4427. //
  4428. // If this strip was selected, then the next strip won't have
  4429. // selection attribute
  4430. //
  4431. fSelected = FALSE;
  4432. SetBkColor(hdc, bkColorSave);
  4433. if (!ped->fDisabled)
  4434. {
  4435. SetTextColor(hdc, textColorSave);
  4436. }
  4437. }
  4438. //
  4439. // Do we have one more strip to draw on the current line?
  4440. //
  4441. if (fDrawOnSameLine || fDrawEndOfLineStrip)
  4442. {
  4443. int iLastDrawnLength;
  4444. //
  4445. // Next strip's attribute is decided based on the char at ichAttrib
  4446. //
  4447. ichAttrib = ichStart + CurStripLength;
  4448. //
  4449. // When drawing the next strip, start at a few chars before
  4450. // the actual start to account for the Neg 'C' of the strip
  4451. // just drawn.
  4452. //
  4453. iLastDrawnLength = CurStripLength +ExtraLengthForNegA - NegCInfo.nCount;
  4454. //
  4455. // Adjust DBCS alignment...
  4456. //
  4457. if (ped->fAnsi && ped->fDBCS)
  4458. {
  4459. ichNewStart = Edit_AdjustIch(ped,pText,ichStart+iLastDrawnLength);
  4460. iLastDrawnLength = ichNewStart - ichStart;
  4461. ichStart = ichNewStart;
  4462. }
  4463. else
  4464. {
  4465. ichStart += iLastDrawnLength;
  4466. }
  4467. LengthToDraw -= iLastDrawnLength;
  4468. iRemainingLengthInLine -= iLastDrawnLength;
  4469. //
  4470. // The start of clip rect for the next strip.
  4471. //
  4472. xStPos = NegCInfo.XStartPos;
  4473. xClipStPos = xClipEndPos;
  4474. }
  4475. //
  4476. // Draw the blank strip at the end of line seperately, if required.
  4477. //
  4478. if (fDrawEndOfLineStrip)
  4479. {
  4480. Edit_TabTheTextOut(hdc, xClipStPos, MAXCLIPENDPOS, xStPos, yPos,
  4481. (LPSTR)(pText+ichStart*ped->cbChar), LengthToDraw, ichStart,
  4482. ped, ped->rcFmt.left+xOffset, ECT_NORMAL, &NegCInfo);
  4483. fDrawEndOfLineStrip = FALSE;
  4484. }
  4485. }
  4486. while(fDrawOnSameLine); // do while loop ends here.
  4487. }
  4488. // Let us move on to the next line of this block to be drawn.
  4489. wCurLine++;
  4490. if (ped->cLines > wCurLine)
  4491. {
  4492. ichStart = ped->chLines[wCurLine];
  4493. }
  4494. else
  4495. {
  4496. ichStart = ichEnd+1; // We have reached the end of the text.
  4497. }
  4498. } // while loop ends here
  4499. Edit_Unlock(ped);
  4500. ShowCaret(ped->hwnd);
  4501. EditML_SetCaretPosition(ped, hdc);
  4502. }
  4503. //---------------------------------------------------------------------------//
  4504. //
  4505. // Multi-Line Support Routines called Rarely
  4506. //---------------------------------------------------------------------------//
  4507. //
  4508. // EditML_InsertCrCrLf AorW
  4509. //
  4510. // Inserts CR CR LF characters into the text at soft (word-wrap) line
  4511. // breaks. CR LF (hard) line breaks are unaffected. Assumes that the text
  4512. // has already been formatted ie. ped->chLines is where we want the line
  4513. // breaks to occur. Note that ped->chLines is not updated to reflect the
  4514. // movement of text by the addition of CR CR LFs. Returns TRUE if successful
  4515. // else notify parent and return FALSE if the memory couldn't be allocated.
  4516. //
  4517. BOOL EditML_InsertCrCrLf(PED ped)
  4518. {
  4519. ICH dch;
  4520. ICH li;
  4521. ICH lineSize;
  4522. unsigned char *pchText;
  4523. unsigned char *pchTextNew;
  4524. if (!ped->fWrap || !ped->cch)
  4525. {
  4526. //
  4527. // There are no soft line breaks if word-wrapping is off or if no chars
  4528. //
  4529. return TRUE;
  4530. }
  4531. //
  4532. // Calc an upper bound on the number of additional characters we will be
  4533. // adding to the text when we insert CR CR LFs.
  4534. //
  4535. dch = 3 * ped->cLines;
  4536. if (!LocalReAlloc(ped->hText, (ped->cch + dch) * ped->cbChar, 0))
  4537. {
  4538. Edit_NotifyParent(ped, EN_ERRSPACE);
  4539. return FALSE;
  4540. }
  4541. ped->cchAlloc = ped->cch + dch;
  4542. //
  4543. // Move the text up dch bytes and then copy it back down, inserting the CR
  4544. // CR LF's as necessary.
  4545. //
  4546. pchTextNew = pchText = Edit_Lock(ped);
  4547. pchText += dch * ped->cbChar;
  4548. //
  4549. // We will use dch to keep track of how many chars we add to the text
  4550. //
  4551. dch = 0;
  4552. //
  4553. // Copy the text up dch bytes to pchText. This will shift all indices in
  4554. // ped->chLines up by dch bytes.
  4555. //
  4556. memmove(pchText, pchTextNew, ped->cch * ped->cbChar);
  4557. //
  4558. // Now copy chars from pchText down to pchTextNew and insert CRCRLF at soft
  4559. // line breaks.
  4560. //
  4561. if (ped->fAnsi)
  4562. {
  4563. for (li = 0; li < ped->cLines - 1; li++)
  4564. {
  4565. lineSize = ped->chLines[li + 1] - ped->chLines[li];
  4566. memmove(pchTextNew, pchText, lineSize);
  4567. pchTextNew += lineSize;
  4568. pchText += lineSize;
  4569. //
  4570. // If last character in newly copied line is not a line feed, then we
  4571. // need to add the CR CR LF triple to the end
  4572. //
  4573. if (*(pchTextNew - 1) != 0x0A)
  4574. {
  4575. *pchTextNew++ = 0x0D;
  4576. *pchTextNew++ = 0x0D;
  4577. *pchTextNew++ = 0x0A;
  4578. dch += 3;
  4579. }
  4580. }
  4581. //
  4582. // Now move the last line up. It won't have any line breaks in it...
  4583. //
  4584. memmove(pchTextNew, pchText, ped->cch - ped->chLines[ped->cLines - 1]);
  4585. }
  4586. else
  4587. {
  4588. LPWSTR pwchTextNew = (LPWSTR)pchTextNew;
  4589. for (li = 0; li < ped->cLines - 1; li++)
  4590. {
  4591. lineSize = ped->chLines[li + 1] - ped->chLines[li];
  4592. memmove(pwchTextNew, pchText, lineSize * sizeof(WCHAR));
  4593. pwchTextNew += lineSize;
  4594. pchText += lineSize * sizeof(WCHAR);
  4595. //
  4596. // If last character in newly copied line is not a line feed, then we
  4597. // need to add the CR CR LF triple to the end
  4598. //
  4599. if (*(pwchTextNew - 1) != 0x0A)
  4600. {
  4601. *pwchTextNew++ = 0x0D;
  4602. *pwchTextNew++ = 0x0D;
  4603. *pwchTextNew++ = 0x0A;
  4604. dch += 3;
  4605. }
  4606. }
  4607. //
  4608. // Now move the last line up. It won't have any line breaks in it...
  4609. //
  4610. memmove(pwchTextNew, pchText,
  4611. (ped->cch - ped->chLines[ped->cLines - 1]) * sizeof(WCHAR));
  4612. }
  4613. Edit_Unlock(ped);
  4614. if (dch)
  4615. {
  4616. //
  4617. // Update number of characters in text handle
  4618. //
  4619. ped->cch += dch;
  4620. //
  4621. // So that the next time we do anything with the text, we can strip the
  4622. // CRCRLFs
  4623. //
  4624. ped->fStripCRCRLF = TRUE;
  4625. return TRUE;
  4626. }
  4627. return FALSE;
  4628. }
  4629. //---------------------------------------------------------------------------//
  4630. //
  4631. // EditML_StripCrCrLf AorW
  4632. //
  4633. // Strips the CR CR LF character combination from the text. This
  4634. // shows the soft (word wrapped) line breaks. CR LF (hard) line breaks are
  4635. // unaffected.
  4636. //
  4637. void EditML_StripCrCrLf(PED ped)
  4638. {
  4639. if (ped->cch)
  4640. {
  4641. if (ped->fAnsi)
  4642. {
  4643. unsigned char *pchSrc;
  4644. unsigned char *pchDst;
  4645. unsigned char *pchLast;
  4646. pchSrc = pchDst = Edit_Lock(ped);
  4647. pchLast = pchSrc + ped->cch;
  4648. while (pchSrc < pchLast)
  4649. {
  4650. if ( (pchSrc[0] == 0x0D)
  4651. && (pchSrc[1] == 0x0D)
  4652. && (pchSrc[2] == 0x0A)
  4653. )
  4654. {
  4655. pchSrc += 3;
  4656. ped->cch -= 3;
  4657. }
  4658. else
  4659. {
  4660. *pchDst++ = *pchSrc++;
  4661. }
  4662. }
  4663. }
  4664. else
  4665. {
  4666. LPWSTR pwchSrc;
  4667. LPWSTR pwchDst;
  4668. LPWSTR pwchLast;
  4669. pwchSrc = pwchDst = (LPWSTR)Edit_Lock(ped);
  4670. pwchLast = pwchSrc + ped->cch;
  4671. while (pwchSrc < pwchLast)
  4672. {
  4673. if ( (pwchSrc[0] == 0x0D)
  4674. && (pwchSrc[1] == 0x0D)
  4675. && (pwchSrc[2] == 0x0A)
  4676. )
  4677. {
  4678. pwchSrc += 3;
  4679. ped->cch -= 3;
  4680. }
  4681. else
  4682. {
  4683. *pwchDst++ = *pwchSrc++;
  4684. }
  4685. }
  4686. }
  4687. Edit_Unlock(ped);
  4688. //
  4689. // Make sure we don't have any values past the last character
  4690. //
  4691. if (ped->ichCaret > ped->cch)
  4692. {
  4693. ped->ichCaret = ped->cch;
  4694. }
  4695. if (ped->ichMinSel > ped->cch)
  4696. {
  4697. ped->ichMinSel = ped->cch;
  4698. }
  4699. if (ped->ichMaxSel > ped->cch)
  4700. {
  4701. ped->ichMaxSel = ped->cch;
  4702. }
  4703. }
  4704. }
  4705. //---------------------------------------------------------------------------//
  4706. //
  4707. // EditML_SetHandle AorW
  4708. //
  4709. // Sets the ped to contain the given handle.
  4710. //
  4711. void EditML_SetHandle(PED ped, HANDLE hNewText)
  4712. {
  4713. ICH newCch;
  4714. ped->cch = ped->cchAlloc =
  4715. (ICH)LocalSize(ped->hText = hNewText) / ped->cbChar;
  4716. ped->fEncoded = FALSE;
  4717. if (ped->cch)
  4718. {
  4719. //
  4720. // We have to do it this way in case the app gives us a zero size handle
  4721. //
  4722. if (ped->fAnsi)
  4723. {
  4724. ped->cch = strlen(Edit_Lock(ped));
  4725. }
  4726. else
  4727. {
  4728. ped->cch = wcslen((LPWSTR)Edit_Lock(ped));
  4729. }
  4730. Edit_Unlock(ped);
  4731. }
  4732. newCch = (ICH)(ped->cch + CCHALLOCEXTRA);
  4733. //
  4734. // We do this LocalReAlloc in case the app changed the size of the handle
  4735. //
  4736. if (LocalReAlloc(ped->hText, newCch*ped->cbChar, 0))
  4737. {
  4738. ped->cchAlloc = newCch;
  4739. }
  4740. Edit_ResetTextInfo(ped);
  4741. }
  4742. //---------------------------------------------------------------------------//
  4743. //
  4744. // EditML_GetLine AorW
  4745. //
  4746. // Copies maxCchToCopy bytes of line lineNumber to the buffer
  4747. // lpBuffer. The string is not zero terminated.
  4748. //
  4749. // Returns number of characters copied
  4750. //
  4751. LONG EditML_GetLine(PED ped, ICH lineNumber, ICH maxCchToCopy, LPSTR lpBuffer)
  4752. {
  4753. PSTR pText;
  4754. ICH cchLen;
  4755. if (lineNumber > ped->cLines - 1)
  4756. {
  4757. TraceMsg(TF_STANDARD,
  4758. "Edit: Invalid parameter \"lineNumber\" (%ld) to EditML_GetLine",
  4759. lineNumber);
  4760. return 0L;
  4761. }
  4762. cchLen = EditML_Line(ped, lineNumber);
  4763. maxCchToCopy = min(cchLen, maxCchToCopy);
  4764. if (maxCchToCopy)
  4765. {
  4766. pText = Edit_Lock(ped) +
  4767. ped->chLines[lineNumber] * ped->cbChar;
  4768. memmove(lpBuffer, pText, maxCchToCopy*ped->cbChar);
  4769. Edit_Unlock(ped);
  4770. }
  4771. return maxCchToCopy;
  4772. }
  4773. //---------------------------------------------------------------------------//
  4774. //
  4775. // EditML_LineIndex AorW
  4776. //
  4777. // This function return s the number of character positions that occur
  4778. // preceeding the first char in a given line.
  4779. //
  4780. ICH EditML_LineIndex( PED ped, ICH iLine)
  4781. {
  4782. if (iLine == -1)
  4783. {
  4784. iLine = ped->iCaretLine;
  4785. }
  4786. if (iLine < ped->cLines)
  4787. {
  4788. return ped->chLines[iLine];
  4789. }
  4790. else
  4791. {
  4792. TraceMsg(TF_STANDARD,
  4793. "Edit: Invalid parameter \"iLine\" (%ld) to EditML_LineIndex",
  4794. iLine);
  4795. return (ICH)-1;
  4796. }
  4797. }
  4798. //---------------------------------------------------------------------------//
  4799. //
  4800. // EditML_LineLength AorW
  4801. //
  4802. // if ich = -1, return the length of the lines containing the current
  4803. // selection but not including the selection. Otherwise, return the length of
  4804. // the line containing ich.
  4805. //
  4806. ICH EditML_LineLength(PED ped, ICH ich)
  4807. {
  4808. ICH il1, il2;
  4809. ICH temp;
  4810. if (ich != 0xFFFFFFFF)
  4811. {
  4812. return (EditML_Line(ped, EditML_IchToLine(ped, ich)));
  4813. }
  4814. //
  4815. // Find length of lines corresponding to current selection
  4816. //
  4817. il1 = EditML_IchToLine(ped, ped->ichMinSel);
  4818. il2 = EditML_IchToLine(ped, ped->ichMaxSel);
  4819. if (il1 == il2)
  4820. {
  4821. return (EditML_Line(ped, il1) - (ped->ichMaxSel - ped->ichMinSel));
  4822. }
  4823. temp = ped->ichMinSel - ped->chLines[il1];
  4824. temp += EditML_Line(ped, il2);
  4825. temp -= (ped->ichMaxSel - ped->chLines[il2]);
  4826. return temp;
  4827. }
  4828. //---------------------------------------------------------------------------//
  4829. //
  4830. // EditML_SetSelection AorW
  4831. //
  4832. // Sets the selection to the points given and puts the cursor at
  4833. // ichMaxSel.
  4834. //
  4835. VOID EditML_SetSelection(PED ped, BOOL fDoNotScrollCaret, ICH ichMinSel, ICH ichMaxSel)
  4836. {
  4837. HDC hdc;
  4838. if (ichMinSel == 0xFFFFFFFF)
  4839. {
  4840. //
  4841. // Set no selection if we specify -1
  4842. //
  4843. ichMinSel = ichMaxSel = ped->ichCaret;
  4844. }
  4845. //
  4846. // Since these are unsigned, we don't check if they are greater than 0.
  4847. //
  4848. ichMinSel = min(ped->cch, ichMinSel);
  4849. ichMaxSel = min(ped->cch, ichMaxSel);
  4850. #ifdef FE_SB // EditML_SetSelectionHander()
  4851. //
  4852. // To avoid position to half of DBCS, check and ajust position if necessary
  4853. //
  4854. // We check ped->fDBCS and ped->fAnsi though Edit_AdjustIch checks these bits
  4855. // at first. We're worrying about the overhead of Edit_Lock and Edit_Unlock.
  4856. //
  4857. if ( ped->fDBCS && ped->fAnsi )
  4858. {
  4859. PSTR pText;
  4860. pText = Edit_Lock(ped);
  4861. ichMinSel = Edit_AdjustIch( ped, pText, ichMinSel );
  4862. ichMaxSel = Edit_AdjustIch( ped, pText, ichMaxSel );
  4863. Edit_Unlock(ped);
  4864. }
  4865. #endif // FE_SB
  4866. //
  4867. // Set the caret's position to be at ichMaxSel.
  4868. //
  4869. ped->ichCaret = ichMaxSel;
  4870. ped->iCaretLine = EditML_IchToLine(ped, ped->ichCaret);
  4871. hdc = Edit_GetDC(ped, FALSE);
  4872. EditML_ChangeSelection(ped, hdc, ichMinSel, ichMaxSel);
  4873. EditML_SetCaretPosition(ped, hdc);
  4874. Edit_ReleaseDC(ped, hdc, FALSE);
  4875. #ifdef FE_SB // EditML_SetSelectionHander()
  4876. if (!fDoNotScrollCaret)
  4877. {
  4878. EditML_EnsureCaretVisible(ped);
  4879. }
  4880. //
  4881. // #ifdef KOREA is history, with FE_SB (FarEast Single Binary).
  4882. //
  4883. #else
  4884. #ifdef KOREA
  4885. //
  4886. // Extra parameter specified interim character mode
  4887. //
  4888. EditML_EnsureCaretVisible(ped,NULL);
  4889. #else
  4890. if (!fDoNotScrollCaret)
  4891. {
  4892. EditML_EnsureCaretVisible(ped);
  4893. }
  4894. #endif
  4895. #endif // FE_SB
  4896. }
  4897. //---------------------------------------------------------------------------//
  4898. //
  4899. // EditML_SetTabStops AorW
  4900. //
  4901. // This sets the tab stop positions set by the App by sending
  4902. // a EM_SETTABSTOPS message.
  4903. //
  4904. // nTabPos : Number of tab stops set by the caller
  4905. // lpTabStops: array of tab stop positions in Dialog units.
  4906. //
  4907. // Returns:
  4908. // TRUE if successful
  4909. // FALSE if memory allocation error.
  4910. //
  4911. BOOL EditML_SetTabStops(PED ped, int nTabPos, LPINT lpTabStops)
  4912. {
  4913. int *pTabStops;
  4914. //
  4915. // Check if tab positions already exist
  4916. //
  4917. if (!ped->pTabStops)
  4918. {
  4919. //
  4920. // Check if the caller wants the new tab positions
  4921. //
  4922. if (nTabPos)
  4923. {
  4924. //
  4925. // Allocate the array of tab stops
  4926. //
  4927. pTabStops = (LPINT)UserLocalAlloc(HEAP_ZERO_MEMORY, (nTabPos + 1) * sizeof(int));
  4928. if (!pTabStops)
  4929. {
  4930. return FALSE;
  4931. }
  4932. }
  4933. else
  4934. {
  4935. //
  4936. // No stops then and no stops now!
  4937. //
  4938. return TRUE;
  4939. }
  4940. }
  4941. else
  4942. {
  4943. //
  4944. // Check if the caller wants the new tab positions
  4945. //
  4946. if (nTabPos)
  4947. {
  4948. //
  4949. // Check if the number of tab positions is different
  4950. //
  4951. if (ped->pTabStops[0] != nTabPos)
  4952. {
  4953. //
  4954. // Yes! So ReAlloc to new size
  4955. //
  4956. pTabStops = (LPINT)UserLocalReAlloc(ped->pTabStops, (nTabPos + 1) * sizeof(int), 0);
  4957. if (!pTabStops)
  4958. {
  4959. return FALSE;
  4960. }
  4961. }
  4962. else
  4963. {
  4964. pTabStops = ped->pTabStops;
  4965. }
  4966. }
  4967. else
  4968. {
  4969. //
  4970. // Caller wants to remove all the tab stops; So, release
  4971. //
  4972. if (!UserLocalFree(ped->pTabStops))
  4973. {
  4974. return FALSE;
  4975. }
  4976. ped->pTabStops = NULL;
  4977. goto RedrawAndReturn;
  4978. }
  4979. }
  4980. //
  4981. // Copy the new tab stops onto the tab stop array after converting the
  4982. // dialog co-ordinates into the pixel co-ordinates
  4983. //
  4984. ped->pTabStops = pTabStops;
  4985. //
  4986. // First element contains the count
  4987. //
  4988. *pTabStops++ = nTabPos;
  4989. while (nTabPos--)
  4990. {
  4991. //
  4992. // aveCharWidth must be used instead of cxSysCharWidth.
  4993. // Fix for Bug #3871 --SANKAR-- 03/14/91
  4994. //
  4995. *pTabStops++ = MultDiv(*lpTabStops++, ped->aveCharWidth, 4);
  4996. }
  4997. RedrawAndReturn:
  4998. //
  4999. // Because the tabstops have changed, we need to recompute the
  5000. // maxPixelWidth. Otherwise, horizontal scrolls will have problems.
  5001. // Fix for Bug #6042 - 3/15/94
  5002. //
  5003. EditML_BuildchLines(ped, 0, 0, FALSE, NULL, NULL);
  5004. //
  5005. // Caret may have changed line by the line recalc above.
  5006. //
  5007. EditML_UpdateiCaretLine(ped);
  5008. EditML_EnsureCaretVisible(ped);
  5009. //
  5010. // Also, we need to redraw the whole window.
  5011. //
  5012. InvalidateRect(ped->hwnd, NULL, TRUE);
  5013. return TRUE;
  5014. }
  5015. //---------------------------------------------------------------------------//
  5016. //
  5017. // EditML_Undo AorW
  5018. //
  5019. // Handles Undo for multiline edit controls.
  5020. //
  5021. BOOL EditML_Undo(PED ped)
  5022. {
  5023. HANDLE hDeletedText = ped->hDeletedText;
  5024. BOOL fDelete = (BOOL)(ped->undoType & UNDO_DELETE);
  5025. ICH cchDeleted = ped->cchDeleted;
  5026. ICH ichDeleted = ped->ichDeleted;
  5027. if (ped->undoType == UNDO_NONE)
  5028. {
  5029. //
  5030. // No undo...
  5031. //
  5032. return FALSE;
  5033. }
  5034. ped->hDeletedText = NULL;
  5035. ped->cchDeleted = 0;
  5036. ped->ichDeleted = (ICH)-1;
  5037. ped->undoType &= ~UNDO_DELETE;
  5038. if (ped->undoType == UNDO_INSERT)
  5039. {
  5040. ped->undoType = UNDO_NONE;
  5041. //
  5042. // Set the selection to the inserted text
  5043. //
  5044. EditML_SetSelection(ped, FALSE, ped->ichInsStart, ped->ichInsEnd);
  5045. ped->ichInsStart = ped->ichInsEnd = (ICH)-1;
  5046. //
  5047. // Now send a backspace to delete and save it in the undo buffer...
  5048. //
  5049. SendMessage(ped->hwnd, WM_CHAR, (WPARAM)VK_BACK, 0L);
  5050. }
  5051. if (fDelete)
  5052. {
  5053. //
  5054. // Insert deleted chars
  5055. //
  5056. //
  5057. // Set the selection to the inserted text
  5058. //
  5059. EditML_SetSelection(ped, FALSE, ichDeleted, ichDeleted);
  5060. EditML_InsertText(ped, hDeletedText, cchDeleted, FALSE);
  5061. GlobalFree(hDeletedText);
  5062. EditML_SetSelection(ped, FALSE, ichDeleted, ichDeleted + cchDeleted);
  5063. }
  5064. return TRUE;
  5065. }
  5066. //---------------------------------------------------------------------------//
  5067. //
  5068. // EditML_Create AorW
  5069. //
  5070. // Creates the edit control for the window hwnd by allocating memory
  5071. // as required from the application's heap. Notifies parent if no memory
  5072. // error (after cleaning up if needed). Returns TRUE if no error else return s
  5073. // -1.
  5074. //
  5075. LONG EditML_Create(PED ped, LPCREATESTRUCT lpCreateStruct)
  5076. {
  5077. LONG windowStyle;
  5078. LPWSTR lpszName;
  5079. //
  5080. // Get values from the window instance data structure and put them in the
  5081. // ped so that we can access them easier
  5082. //
  5083. windowStyle = GET_STYLE(ped);
  5084. //
  5085. // Do the standard creation stuff
  5086. //
  5087. if (!Edit_Create(ped, windowStyle))
  5088. {
  5089. return -1;
  5090. }
  5091. //
  5092. // Allocate line start array in local heap and lock it down
  5093. //
  5094. ped->chLines = (LPICH)LocalAlloc(LPTR, 2 * sizeof(int));
  5095. if (ped->chLines == NULL)
  5096. {
  5097. return -1;
  5098. }
  5099. //
  5100. // Call it one line of text...
  5101. //
  5102. ped->cLines = 1;
  5103. //
  5104. // If app wants WS_VSCROLL or WS_HSCROLL, it automatically gets AutoVScroll
  5105. // or AutoHScroll.
  5106. //
  5107. if ((windowStyle & ES_AUTOVSCROLL) || (windowStyle & WS_VSCROLL))
  5108. {
  5109. ped->fAutoVScroll = 1;
  5110. }
  5111. if (ped->format != ES_LEFT)
  5112. {
  5113. //
  5114. // If user wants right or center justified text, then we turn off
  5115. // AUTOHSCROLL and WS_HSCROLL since non-left styles don't make sense
  5116. // otherwise.
  5117. //
  5118. windowStyle &= ~WS_HSCROLL;
  5119. ClearWindowState(ped->hwnd, WS_HSCROLL);
  5120. ped->fAutoHScroll = FALSE;
  5121. }
  5122. else if (windowStyle & WS_HSCROLL)
  5123. {
  5124. ped->fAutoHScroll = TRUE;
  5125. }
  5126. ped->fWrap = (!ped->fAutoHScroll && !(windowStyle & WS_HSCROLL));
  5127. //
  5128. // Max # chars we will allow user to enter
  5129. //
  5130. ped->cchTextMax = MAXTEXT;
  5131. //
  5132. // Set the default font to be the system font.
  5133. //
  5134. if ( !Edit_SetFont(ped, NULL, FALSE) )
  5135. {
  5136. // If setting the font fails, our textmetrics can potentially be left
  5137. // unitialized. Fail to create the control.
  5138. return -1;
  5139. }
  5140. //
  5141. // Set the window text if needed and notify parent if not enough memory to
  5142. // set the initial text.
  5143. //
  5144. #if 0
  5145. if ((ULONG_PTR)lpCreateStruct->lpszName > gHighestUserAddress)
  5146. {
  5147. lpszName = REBASEPTR(ped->pwnd, (PVOID)lpCreateStruct->lpszName);
  5148. }
  5149. else
  5150. #endif
  5151. {
  5152. lpszName = (LPWSTR)lpCreateStruct->lpszName;
  5153. }
  5154. if (!Edit_SetEditText(ped, (LPSTR)lpszName))
  5155. {
  5156. return -1;
  5157. }
  5158. return TRUE;
  5159. }