Files
Mikhail Tyukinandwinesync b6925bdd6f [RICHED20] Sync to Wine-10.0 (#8078)
[RICHED20] Sync to Wine-10.0
Sync wine/rbtree.h to Wine-10.0 as changes in riched20 require this. Also added a define to allow directx code to compile.
Sync riched20 to Wine-10.0 and its tests using the script.

Signed-off-by: Francois Gouget <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 9f72c5601173728c52626342a1ec6c9a4c70670f by Francois Gouget <[email protected]>

Update WINESYNC.txt

Update precomp.h

fix build

[WINESYNC]: riched20 is now in sync with wine-staging wine-10.0

[WINESYNC] riched20: Only call ME_SendRequestResize when control is in-place active.

Prevent GetClientRect from being called when not in-place active.

wine commit id e61db557ab8473a490c84301dbb4b4665b6c9c9f by Charlotte Pabst <[email protected]>

[WINESYNC] riched20: Exit from editor_ensure_visible when control is not in-place active.

According to microsoft documentation, ScrollWindowEx, SetScrollPos and SetScrollRange may only
be called while the control is in-place active.

This fixes a segmentation fault in an application.

wine commit id dafaadcb4addd9af334433666a4bc2e7aa6eb9ff by Charlotte Pabst <[email protected]>

[WINESYNC] riched20/tests: Test that ScrollWindowEx and GetClientRect are only called when control is in-place active.

wine commit id f1301cedbea51be107017bb8545956bcfd02b610 by Charlotte Pabst <[email protected]>

Update editor.c

[WINESYNC] riched20: Release IME input context when done using it.

wine commit id 3f51fb96c162e8d3314627b3db25564fa7d9d419 by Zhiyi Zhang <[email protected]>

[WINESYNC] riched20: In para_set_fmt protect against out of bound cTabStop values.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=56730

wine commit id 7b2ff977739df25252d46552d2447af50c23040e by Fabian Maurer <[email protected]>

Update editor.c

[WINESYNC] riched20: Don't write past end of buffer.

Signed-off-by: Eric Pouech <[email protected]>

wine commit id 5cbe3a6cb186cffed20e3fea5f88abd0880d3914 by Eric Pouech <[email protected]>

[WINESYNC] riched20: Don't crash when no OLE obj is present.

This was generating a lot of crashes in riched20 tests.

Signed-off-by: Eric Pouech <[email protected]>

wine commit id 015aae30cf2217164f631d1c03abfc9ea63bb43d by Eric Pouech <[email protected]>

Update editor.c

[WINESYNC] riched20: Use CRT allocation functions.

wine commit id 42579f1dcd2f14e765e2f53ceb5b46fff5623f10 by Alex Henrie <[email protected]>

[WINESYNC] riched20/tests: Make the editor paste test failures more informative and unique.

Knowing the content of the edit field is more useful than knowing the
strcmp() result. The text field may contain carriage returns and
linefeeds so use wine_dbgstr_a() so they are clearly visible in the
failure message.
Also prefix the ok messages with a unique string to indicate which
test_WM_PASTE() test failed.

wine commit id b8950d04e439f649f2167477a36ee453ad8b81ef by Francois Gouget <[email protected]>

[WINESYNC] riched20: Don't call wrap_marked_paras_dc() before the editor is fully initialized.

Today, CreateTextServices() sometimes triggers an assertion failure in
select_style().

When ME_MakeEditor() calls ME_MakeFirstParagraph(), the editor
(ME_TextEditor) is not in a fully initialized state.  For example, the
font cache (pFontCache) is not fully initialized, which sometimes makes
select_style() believe that the cache slots are fully occupied.

Fix this by delaying the call to wrap_marked_paras_dc() until the editor
is fully initialized.

Also, delay the call to ITextHost::TxReleaseDC() until after
wrap_marked_paras_dc(), since we need the device context a bit longer.

Fixes: b70eb32c5f3e506181d9e1e9fbef62e9baf3674d

wine commit id 548d7179e3b2b9961321a4df50644c9b3874a295 by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Factor out device context acquisition from ME_MakeFirstParagraph.

This lets ME_MakeEditor() reuse the device context throughout the editor
initialization process.

wine commit id 03036f42e1afa9c79246dcee6b9fe876286b1aa4 by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Don't call ME_UpdateRepaint inside set_selection().

This partially reverts c5daf6a30f21 (riched20: Call ME_UpdateRepaint
instead of editor_ensure_visible in set_selection.)

Fixes: c5daf6a30f21acd20263692b278acba49e014cc1

wine commit id ac5110131493b8b05795e8825285e41ca40ab6f1 by Jinoh Kang <[email protected]>

[WINESYNC] riched20/tests: Add tests for change notifications caused by EM_SETSEL{,EX}.

wine commit id c35926e49825f7a3d48ba826085fe1ae3d2a7e27 by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Wrap marked paragraphs at Text Services initialization.

Today, CreateTextServices() returns an Rich Edit object without row
start and end marks, which are expected to exist by many Rich Edit
operations as well as EM_* message handlers.

This leads to a crash when certain messages (e.g., EM_SCROLLCARET) are
sent to the Rich Edit object via ITextServices::TxSendMessage(), unless
ME_WrapMarkedParagraphs() has been called beforehand.

Fix this by calling wrap_marked_paras_dc() early in the initialization
process.

This is not a problem for windowed Rich Edit controls, which already
calls ME_WrapMarkedParagraphs() before the user or application starts
interacting with it.

wine commit id b70eb32c5f3e506181d9e1e9fbef62e9baf3674d by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Use nameless unions/structs.

wine commit id 26826d3af8b1969de389f3d6702d7c8b805126e1 by Alexandre Julliard <[email protected]>

Update editor.h

[WINESYNC] riched20: Remove DECLSPEC_HIDDEN usage.

wine commit id c04ab166498b5841ecde620a9bd0b74969b01596 by Alistair Leslie-Hughes <[email protected]>

[WINESYNC] riched20: Call ME_UpdateRepaint instead of editor_ensure_visible in set_selection.

To prevent cases where ME_WrapMarkedParagraphs has not been called causing a NULL
pointer when editor_ensure_visible tries to get the row from the cursor.

wine commit id c5daf6a30f21acd20263692b278acba49e014cc1 by Santino Mazza <[email protected]>

[WINESYNC] riched20: Update paragraph position in marked tree.

nCharOfs is the key for paragraphs added to the marked tree
If it is updated, re-add the entry to update its position

wine commit id f1aa3f37ef326b7cbf8fc94e982c9f0b398181c4 by Daniel Lehman <[email protected]>

Update editor.c

[WINESYNC] riched20: Update the editor IME position on GCS_RESULTSTR.

So that a GCS_RESULTSTR followed by GCS_COMPSTR, without interruping the
composition, begins inserting the new composition text after the result
instead of before it.

wine commit id de45bc33a73eac5dbc3502b4a43cd48399507c73 by Rémi Bernon <[email protected]>

[WINESYNC] riched20/tests: Fix a typo in a comment.

wine commit id 145e3c758b9594596835e291b2fcb3c80e4c0479 by Francois Gouget <[email protected]>

[WINESYNC] riched20/tests: Don't specify DT_WORDBREAK in _check_txgetnaturalsize().

Today, test_TxGetNaturalSize() creates a pop-up window with a fixed size
(extent) of 100 x 100.  The test function then proceeds to compute the
natural size of rich edit control that fits the sample text
"TestSomeText" and compare it to the RECT calculated by DrawText.

Apparently, this test fails if the width of the sample text
"TestSomeText" exceeds the width of the test window's client area.  In
this case, DrawText() with DT_WORDBREAK breaks the text into the two
lines due to text wrapping; however, Rich Edit's
ITextServices::TxGetNaturalSize implementation does not seem to perform
text wrapping on overflow.  This results in extent mismatch.

(Note that the test may either succeed or fail depending on the current
font used for DEFAULT_GUI_FONT, which defaults to Tahoma according to
Microsoft's documentation.  To reproduce this failure, it may be
necessary to switch fonts or change the width of the pop-up window.)

Fix this by omitting the DT_WORDBREAK flag for the DrawText() call in
_check_txgetnaturalsize().

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=54637

wine commit id f54a1dac64c89aea3fe02ae8c0d6eef43a0aa117 by Jinoh Kang <[email protected]>

Update editstr.h

[WINESYNC] riched20: Implement ITextDocument::Freeze and ITextDocument::Unfreeze.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=54617

wine commit id 3fe15349d39ccf31026a92eae30f83f65c91cb1f by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Don't assume that TxDraw preserves the device context's brush selection.

Today, RichEditWndProc_common assumes that ITextServices::TxDraw
preserves the brush selection of the given device context.  However,
this invariant may be broken by misbehaving embedded OLE objects in the
text document.

Fix this by not assuming that the return value of the second
SelectObject() call equals the brush passed to the first SelectObject()
call in RichEditWndProc_common's WM_PAINT / WM_PRINTCLIENT case.

wine commit id 2027be7e0370c92595f126a0b0dd167a635d50a2 by Jinoh Kang <[email protected]>

[WINESYNC] riched20/tests: Test for ITextDocument::Freeze and ITextDocument::Unfreeze.

wine commit id 3105fad82b9bae390fcd710a2fc10a18988b4622 by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Don't set para unless committing cursor move in ME_MoveCursorWords.

Introduce a temporary variable, `other_para`, so that we don't end up
returning incorrect para (with stale run) inside the output cursor.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=54564

wine commit id 7e28fa5c558f1417f8f033cb843424778bbfb8b8 by Jinoh Kang <[email protected]>

[WINESYNC] riched20/tests: Test for word movement across paragraph boundary.

wine commit id 21d25b17c2cae8eb2d2376ce42b76e3f61eb0bba by Jinoh Kang <[email protected]>

[WINESYNC] riched20/tests: Fix the EM_GETSELTEXT tests in Hindi on Windows.

On Windows EM_GETSELTEXT fails with an invalid parameter error in the
Hindi locale but works in most other locales, including UTF-8 ones.
So check for this issue and adjust the expected results accordingly.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=54559
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=54565

wine commit id 37cd13abdf5d91c7b3f25b39fa14976bc3a3e3a3 by Francois Gouget <[email protected]>

[WINESYNC] riched20/tests: EM_GETSELTEXT's wparam is not used and should be 0.

wine commit id baa41970647a7e548701a72bea2e4aa2a61ca27e by Francois Gouget <[email protected]>

[WINESYNC] riched20/tests: Add context to the EM_FORMATRANGE tests.

wine commit id 020145248371bbfd2e473e79ba873e8313dde72d by Francois Gouget <[email protected]>

[WINESYNC] riched20/tests: Check clipboard content after destroying the editor window.

Destroying the editor window should render all the formats so they are
still available after.

wine commit id b5b5b0b365639b889a0240d4992e16174b428f75 by Francois Gouget <[email protected]>

[WINESYNC] riched20/tests: Add retry loop around OpenClipboard() to avoid access denial.

Reusing existing helper from user32 & comctl32.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=47888
Signed-off-by: Eric Pouech <[email protected]>

wine commit id 25743d46f4103c7345c955b87b4134a065fb5f1d by Eric Pouech <[email protected]>

[WINESYNC] riched20/tests: Ensure clipboard has been pasted before testing the result.

The patch does reduce error failures locally.

For the record:
- WM_PASTE in undo operations haven't been transformed (as it requires
  UNDO stack manipulation in a coherent way across platforms)

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=47888
Signed-off-by: Eric Pouech <[email protected]>

wine commit id 4fd5252f635817b431673accaba718c7481f48f6 by Eric Pouech <[email protected]>

[WINESYNC] riched20: Support SFF_SELECTION when streaming in plain text.

Signed-off-by: Eric Pouech <[email protected]>

wine commit id 1b7a918bd7ec8bc2c51bcb76525b56b17c5ff0c0 by Eric Pouech <[email protected]>

[WINESYNC] riched20/tests: Add tests when pasting in plain text mode.

Signed-off-by: Eric Pouech <[email protected]>

wine commit id 82fa1048b5cc7d9c6a2a289403f25adbdd8158c7 by Eric Pouech <[email protected]>

[WINESYNC] riched20: Obtain the composition start index after deleting selection.

Today, the Rich Edit control handles the WM_IME_STARTCOMPOSITION message
by the computing the composition start position (imeStartIndex) _before_
calling ME_DeleteSelection(), which shifts the character positions after
the range of deletion.  If the selection were not empty, imeStartIndex
immediately becomes stale, since it does not take into account the
number of deleted characters before it.

Fix this by computing imeStartIndex after the ME_DeleteSelection() call.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=54318

wine commit id 8c43577f0f9b4422d3641457c595c3998bd54a18 by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Call GlobalReAlloc with GMEM_MOVEABLE flag.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=53474

wine commit id dfd44bb979ee2052eceff094083147e9879fa807 by Rémi Bernon <[email protected]>

[WINESYNC] riched20: Fix failing test in UTF8 locales.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=52875
Signed-off-by: Eric Pouech <[email protected]>

wine commit id 5902cfd2990261189ce9af27981a95ac12fa2a5d by Eric Pouech <[email protected]>

[WINESYNC] riched20: Don't emit EN_CHANGE events during window creation.

wine commit id 5c997de8dc1e4885266fa884a2fb74f840e3bedc by Evan Tang <[email protected]>

[WINESYNC] riched20: Add test for EN_CHANGE events during window creation.

GenoPro isn't very happy if it receives one

wine commit id 0b069106b0e0f141d6925d3e10166be89fc59145 by Evan Tang <[email protected]>

[WINESYNC] riched20: Move test class creation to a helper function.

wine commit id f9083961ed187777d660796f7136addc04fff8b0 by Evan Tang <[email protected]>

[WINESYNC] riched20/tests: Use ARRAY_SIZE instead of open coding it.

wine commit id 90b6288f54afdc4430dac146779a92b89f4d1219 by Michael Stefaniuc <[email protected]>

[WINESYNC] riched20/test: Add scrolling test for richole.

Signed-off-by: Santino Mazza <[email protected]>

wine commit id 059c70d736baa164a1b246e949acd006cf83ab5a by Santino Mazza <[email protected]>

[WINESYNC] riched20: Ensure that cursor is visible when selecting text.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=40444
Signed-off-by: Santino Mazza <[email protected]>

wine commit id 25209c162fc209124b369abbad55d915d31909b2 by Santino Mazza <[email protected]>

[WINESYNC] riched20/tests: When selection is out of view, the scrollbar should move.

Signed-off-by: Santino Mazza <[email protected]>

wine commit id ee131e9eba7dad090f0baf4a73fab2d62dc73b52 by Santino Mazza <[email protected]>

[WINESYNC] riched20: Remove duplicate code.

Signed-off-by: David Kahurani <[email protected]>

wine commit id 3588479e1a674e5f43f1acb9e7c0aa8cecf98f25 by David Kahurani <[email protected]>

[WINESYNC] riched20: Implement ITextDocument::Undo and ITextDocument::Redo.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=53289
Signed-off-by: Jinoh Kang <[email protected]>

wine commit id ded8227774570a1ea320dc34f2de0033f85b3ce7 by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Add undo control state variable to ME_TextEditor.

Signed-off-by: Jinoh Kang <[email protected]>

wine commit id 40c54854e61d8adfca21e2fa9260712e4f5453e9 by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Factor out undo-ignored status check.

Signed-off-by: Jinoh Kang <[email protected]>

wine commit id 0cd86e010e41eebdc1a4c142f9cc6a4948554d0e by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Ensure MEPF_COMPLEX is unset when in password input mode.

Otherwise, Wine may crash while attempting to fetch the script shaping
information, which does not exist since shape_para() was not called.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=53335
Signed-off-by: Jinoh Kang <[email protected]>

wine commit id f77f12ba3b1dad9ebdcb116c66b137e9103a0d1b by Jinoh Kang <[email protected]>

[WINESYNC] riched20/tests: Test for ITextDocument::Undo and ITextDocument::Redo.

Signed-off-by: Jinoh Kang <[email protected]>

wine commit id 73284ea18891a152dce989ffff59e2c527f94c73 by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Commit and wrap paragraphs after changing text properties.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=53170
Signed-off-by: Jinoh Kang <[email protected]>

wine commit id 0974f047b57357252ec94bea119e5291250a446c by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Draw OLE objects with IViewObject implementation.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=52752
Signed-off-by: Jinoh Kang <[email protected]>

wine commit id ecc607c6da96a5c2e8988f3021e98b442fe24c12 by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Fetch extent from object if REOBJECT::sizel is unset.

Signed-off-by: Jinoh Kang <[email protected]>

wine commit id 164897e6c94d3eee4798f82a730472a5a847f9b2 by Jinoh Kang <[email protected]>

[WINESYNC] riched20/tests: Add tests for drawing embedded OLE objects.

Signed-off-by: Jinoh Kang <[email protected]>

wine commit id a7d29d1625f7e02df4fbf59ef4e4a999d39edde0 by Jinoh Kang <[email protected]>

[WINESYNC] riched20/tests: Fix the spelling of a comment.

Signed-off-by: Francois Gouget <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id f9fa5f9e523b45766f715c6981c6a6ab5f183157 by Francois Gouget <[email protected]>

[WINESYNC] riched20: Implement callback to IRichEditOleCallback_QueryInsertObject.

Signed-off-by: Jinoh Kang <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 10667b39aad6710668f60c1ba8f10e50aeb87242 by Jinoh Kang <[email protected]>

[WINESYNC] riched20/tests: Add tests for IRichEditOleCallback_QueryInsertObject.

Signed-off-by: Jinoh Kang <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 95b4072cc774ba7a7eabb0d4c114b933b7d71105 by Jinoh Kang <[email protected]>

Update richole.c

[WINESYNC] riched20: Set REOBJECT's cp correctly in IRichEditOle::GetObject.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=52705
Signed-off-by: Jinoh Kang <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 194ba06c403fd72fbfb95048385c9ca4838af169 by Jinoh Kang <[email protected]>

[WINESYNC] riched20: Add back-reference to outer ME_Run from struct re_object.

Signed-off-by: Jinoh Kang <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 05796c4e2a4acb91e6035c536b4d0e1f7e16896d by Jinoh Kang <[email protected]>

fix

[WINESYNC] riched20/tests: Enable compilation with long types.

Signed-off-by: Eric Pouech <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 9e90cecdc603d79c6332a03884ed8d4851ef2e88 by Eric Pouech <[email protected]>

[WINESYNC] riched20/tests: Use correct integral type.

Signed-off-by: Eric Pouech <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 8b66db027d4112bfd4287b03d5c7b6f33994adc3 by Eric Pouech <[email protected]>

fix build, sync rbtree.h

[WINESYNC] riched20: Enable compilation with long types.

Signed-off-by: Eric Pouech <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 42d0e56643740a0eac5913a9f176e789457cb475 by Eric Pouech <[email protected]>

[WINESYNC] riched20/tests: Add a test to see what richedit class flavours should be available.

Signed-off-by: Dmitry Timoshkov <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id d54a0e2bea4548bb5d9c0cca9f918d7c46591e23 by Dmitry Timoshkov <[email protected]>

[WINESYNC] riched20: Use correct integral type.

Signed-off-by: Eric Pouech <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 63a643f7f42ecd0c2625a5f5704b5b9647349e7b by Eric Pouech <[email protected]>

[WINESYNC] riched20/tests: Avoid "misleading indentation" warnings.

Signed-off-by: Fabian Maurer <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 2d6fad7bda2f2e21a871f405a6b0c3e70ebf6a6a by Fabian Maurer <[email protected]>

[WINESYNC] include: Remove the wine_ prefix on rbtree functions.

Add defines for frequently-used functions.

For compatibility with vkd3d.

Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 3527230c461b6baba9c29e69b575ada85bdbbc43 by Alexandre Julliard <[email protected]>

[WINESYNC] riched20: Defer releasing the host if inside a notify callback.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=51521
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 8138159f20d6453443861570689e0f14018051da by Huw Davies <[email protected]>

[WINESYNC] riched20: Return the nearest char pos if the coords are outside the format rect.

For the two tests that remain todo_wine, the results differ between
riched20 and msftedit, with Wine's implementation matching msftedit.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=52041
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 363508454d5f8f1fc5916bda47e6429756b7dc8f by Huw Davies <[email protected]>

[WINESYNC] riched20: Check font_cache in select_style() too.

Signed-off-by: Serge Gautherie <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 177f808aa86e5442be1a0a92ddccc4904bf2e743 by Serge Gautherie <[email protected]>

[WINESYNC] riched20: Use the standard va_list instead of __ms_va_list.

Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 5b15bf33255bd373cbf26ec373d16a8234ec6b1c by Alexandre Julliard <[email protected]>

[WINESYNC] riched20: Use regular allocation helpers for text services objects.

Signed-off-by: Nikolay Sivov <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 2064c206c0b67edb8524f572166b974ba9cbd6fe by Nikolay Sivov <[email protected]>

[WINESYNC] riched20: Remove New() allocation macro.

Signed-off-by: Nikolay Sivov <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 1d5c9defb19fca6cfb1107384fc2325d391726d6 by Nikolay Sivov <[email protected]>

[WINESYNC] riched20: Don't create custom heap.

Signed-off-by: Dmitry Timoshkov <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 8bff437e9d5e047e689d8b32f62995785c0cd48e by Dmitry Timoshkov <[email protected]>

[WINESYNC] riched20/tests: Don't cast NULL.

Signed-off-by: Michael Stefaniuc <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id b701c3df99fbd6d4df7eaa11327bb11db95d8676 by Michael Stefaniuc <[email protected]>

[WINESYNC] riched20/tests: Add more tests for testing EM_GETTEXTEX with OLE objects.

Signed-off-by: Jactry Zeng <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 40988d6c3886d0f7a914c45ea68264bfc8540859 by Jactry Zeng <[email protected]>

Update richole.c

[WINESYNC] riched20: Allow to insert OLE object at specified offset.

Signed-off-by: Jactry Zeng <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id 8c894382dde5b9da88b0e9f23a753d5e851a43d6 by Jactry Zeng <[email protected]>

Update richole.c

[WINESYNC] riched20/tests: Lock hGlobal before use.

Signed-off-by: Jactry Zeng <[email protected]>
Signed-off-by: Huw Davies <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>

wine commit id f9c8c95856ded333be35ff41ab40a246bfe345b6 by Jactry Zeng <[email protected]>

Co-authored-by: winesync <[email protected]>
2025-06-08 15:05:27 -07:00

1531 lines
43 KiB
C

/*
* RichEdit - Caret and selection functions.
*
* Copyright 2004 by Krzysztof Foltman
* Copyright 2005 by Phil Krylov
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "editor.h"
WINE_DEFAULT_DEBUG_CHANNEL(richedit);
void ME_SetCursorToStart(ME_TextEditor *editor, ME_Cursor *cursor)
{
cursor->para = editor_first_para( editor );
cursor->run = para_first_run( cursor->para );
cursor->nOffset = 0;
}
static void ME_SetCursorToEnd(ME_TextEditor *editor, ME_Cursor *cursor, BOOL final_eop)
{
cursor->para = para_prev( editor_end_para( editor ) );
cursor->run = para_end_run( cursor->para );
cursor->nOffset = final_eop ? cursor->run->len : 0;
}
int ME_GetSelectionOfs(ME_TextEditor *editor, LONG *from, LONG *to)
{
*from = ME_GetCursorOfs(&editor->pCursors[0]);
*to = ME_GetCursorOfs(&editor->pCursors[1]);
if (*from > *to)
{
LONG tmp = *from;
*from = *to;
*to = tmp;
return 1;
}
return 0;
}
int ME_GetSelection(ME_TextEditor *editor, ME_Cursor **from, ME_Cursor **to)
{
int from_ofs = ME_GetCursorOfs( &editor->pCursors[0] );
int to_ofs = ME_GetCursorOfs( &editor->pCursors[1] );
BOOL swap = (from_ofs > to_ofs);
if (from_ofs == to_ofs)
{
/* If cursor[0] is at the beginning of a run and cursor[1] at the end
of the prev run then we need to swap. */
if (editor->pCursors[0].nOffset < editor->pCursors[1].nOffset)
swap = TRUE;
}
if (!swap)
{
*from = &editor->pCursors[0];
*to = &editor->pCursors[1];
return 0;
} else {
*from = &editor->pCursors[1];
*to = &editor->pCursors[0];
return 1;
}
}
int ME_GetTextLength(ME_TextEditor *editor)
{
ME_Cursor cursor;
ME_SetCursorToEnd(editor, &cursor, FALSE);
return ME_GetCursorOfs(&cursor);
}
int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
{
int length;
if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
return E_INVALIDARG;
if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
return E_INVALIDARG;
length = ME_GetTextLength(editor);
if ((editor->props & TXTBIT_MULTILINE)
&& (how->flags & GTL_USECRLF)
&& !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
length += editor->nParagraphs - 1;
if (how->flags & GTL_NUMBYTES ||
(how->flags & GTL_PRECISE && /* GTL_PRECISE seems to imply GTL_NUMBYTES */
!(how->flags & GTL_NUMCHARS))) /* unless GTL_NUMCHARS is given */
{
CPINFO cpinfo;
if (how->codepage == 1200)
return length * 2;
if (how->flags & GTL_PRECISE)
FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
if (GetCPInfo(how->codepage, &cpinfo))
return length * cpinfo.MaxCharSize;
ERR("Invalid codepage %u\n", how->codepage);
return E_INVALIDARG;
}
return length;
}
/******************************************************************
* set_selection_cursors
*
* Updates the selection cursors.
*
* Note that this does not invalidate either the old or the new selections.
*/
int set_selection_cursors(ME_TextEditor *editor, int from, int to)
{
int selectionEnd = 0;
const int len = ME_GetTextLength(editor);
/* all negative values are effectively the same */
if (from < 0)
from = -1;
if (to < 0)
to = -1;
/* select all */
if (from == 0 && to == -1)
{
ME_SetCursorToStart(editor, &editor->pCursors[1]);
ME_SetCursorToEnd(editor, &editor->pCursors[0], TRUE);
return len + 1;
}
/* if both values are equal and also out of bound, that means to */
/* put the selection at the end of the text */
if ((from == to) && (to < 0 || to > len))
{
selectionEnd = 1;
}
else
{
/* if from is negative and to is positive then selection is */
/* deselected and caret moved to end of the current selection */
if (from < 0)
{
LONG start, end;
ME_GetSelectionOfs(editor, &start, &end);
if (start != end)
{
if (end > len)
{
editor->pCursors[0].nOffset = 0;
end --;
}
editor->pCursors[1] = editor->pCursors[0];
}
return end;
}
/* adjust to if it's a negative value */
if (to < 0)
to = len + 1;
/* flip from and to if they are reversed */
if (from>to)
{
int tmp = from;
from = to;
to = tmp;
}
/* after fiddling with the values, we find from > len && to > len */
if (from > len)
selectionEnd = 1;
/* special case with to too big */
else if (to > len)
to = len + 1;
}
if (selectionEnd)
{
ME_SetCursorToEnd(editor, &editor->pCursors[0], FALSE);
editor->pCursors[1] = editor->pCursors[0];
return len;
}
cursor_from_char_ofs( editor, from, &editor->pCursors[1] );
editor->pCursors[0] = editor->pCursors[1];
ME_MoveCursorChars(editor, &editor->pCursors[0], to - from, FALSE);
/* Selection is not allowed in the middle of an end paragraph run. */
if (editor->pCursors[1].run->nFlags & MERF_ENDPARA)
editor->pCursors[1].nOffset = 0;
if (editor->pCursors[0].run->nFlags & MERF_ENDPARA)
{
if (to > len)
editor->pCursors[0].nOffset = editor->pCursors[0].run->len;
else
editor->pCursors[0].nOffset = 0;
}
return to;
}
void cursor_coords( ME_TextEditor *editor, ME_Cursor *cursor,
int *x, int *y, int *height )
{
ME_Row *row;
ME_Run *run = cursor->run;
ME_Paragraph *para = cursor->para;
ME_Run *size_run = run, *prev;
ME_Context c;
int run_x;
HDC hdc = ITextHost_TxGetDC( editor->texthost );
assert(~para->nFlags & MEPF_REWRAP);
row = row_from_cursor( cursor );
ME_InitContext( &c, editor, hdc );
if (!cursor->nOffset && (prev = run_prev( run ))) size_run = prev;
run_x = ME_PointFromCharContext( &c, run, cursor->nOffset, TRUE );
*height = size_run->nAscent + size_run->nDescent;
*x = c.rcView.left + run->pt.x + run_x - editor->horz_si.nPos;
*y = c.rcView.top + para->pt.y + row->nBaseline
+ run->pt.y - size_run->nAscent - editor->vert_si.nPos;
ME_DestroyContext(&c);
ITextHost_TxReleaseDC( editor->texthost, hdc );
return;
}
void create_caret(ME_TextEditor *editor)
{
int x, y, height;
cursor_coords( editor, &editor->pCursors[0], &x, &y, &height );
ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
editor->caret_height = height;
editor->caret_hidden = TRUE;
}
void show_caret(ME_TextEditor *editor)
{
ITextHost_TxShowCaret(editor->texthost, TRUE);
editor->caret_hidden = FALSE;
}
void hide_caret(ME_TextEditor *editor)
{
/* calls to HideCaret are cumulative; do so only once */
if (!editor->caret_hidden)
{
ITextHost_TxShowCaret(editor->texthost, FALSE);
editor->caret_hidden = TRUE;
}
}
void update_caret(ME_TextEditor *editor)
{
int x, y, height;
if (!editor->bHaveFocus) return;
if (!ME_IsSelection(editor))
{
cursor_coords( editor, &editor->pCursors[0], &x, &y, &height );
if (height != editor->caret_height) create_caret(editor);
x = min(x, editor->rcFormat.right-1);
ITextHost_TxSetCaretPos(editor->texthost, x, y);
show_caret(editor);
}
else
hide_caret(editor);
#ifdef __REACTOS__
if (ImmIsIME(GetKeyboardLayout(0)))
{
HIMC hIMC = ImmGetContext(editor->hWnd);
if (hIMC)
{
CHARFORMAT2W fmt;
LOGFONTW lf;
COMPOSITIONFORM CompForm;
POINT pt = { x, y };
CompForm.ptCurrentPos = pt;
if (editor->styleFlags & ES_MULTILINE)
{
CompForm.dwStyle = CFS_RECT;
CompForm.rcArea = editor->rcFormat;
}
else
{
CompForm.dwStyle = CFS_POINT;
SetRectEmpty(&CompForm.rcArea);
}
ImmSetCompositionWindow(hIMC, &CompForm);
fmt.cbSize = sizeof(fmt);
ME_GetSelectionCharFormat(editor, &fmt);
ZeroMemory(&lf, sizeof(lf));
lf.lfCharSet = DEFAULT_CHARSET;
if (fmt.dwMask & CFM_SIZE)
{
HDC hdc = CreateCompatibleDC(NULL);
lf.lfHeight = -MulDiv(fmt.yHeight, GetDeviceCaps(hdc, LOGPIXELSY), 1440);
DeleteDC(hdc);
}
if (fmt.dwMask & CFM_CHARSET)
lf.lfCharSet = fmt.bCharSet;
if (fmt.dwMask & CFM_FACE)
lstrcpynW(lf.lfFaceName, fmt.szFaceName, ARRAY_SIZE(lf.lfFaceName));
ImmSetCompositionFontW(hIMC, &lf);
ImmReleaseContext(editor->hWnd, hIMC);
}
}
#endif
}
BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
int nChars, BOOL bForce)
{
ME_Cursor c = *start;
int nOfs = ME_GetCursorOfs(start), text_len = ME_GetTextLength( editor );
int shift = 0;
int totalChars = nChars;
ME_Paragraph *start_para;
BOOL delete_all = FALSE;
/* Prevent deletion past last end of paragraph run. */
nChars = min(nChars, text_len - nOfs);
if (nChars == text_len) delete_all = TRUE;
start_para = c.para;
if (!bForce)
{
table_protect_partial_deletion( editor, &c, &nChars );
if (nChars == 0) return FALSE;
}
while (nChars > 0)
{
ME_Run *run;
cursor_from_char_ofs( editor, nOfs + nChars, &c );
if (!c.nOffset)
{
/* We aren't deleting anything in this run, so we will go back to the
* last run we are deleting text in. */
c.run = run_prev_all_paras( c.run );
c.para = c.run->para;
c.nOffset = c.run->len;
}
run = c.run;
if (run->nFlags & MERF_ENDPARA)
{
int eollen = c.run->len;
BOOL keepFirstParaFormat;
if (!para_next( para_next( c.para ) )) return TRUE;
keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
run->nCharOfs);
if (!editor->bEmulateVersion10) /* v4.1 */
{
ME_Paragraph *this_para = run->para;
ME_Paragraph *next_para = para_next( this_para );
/* The end of paragraph before a table row is only deleted if there
* is nothing else on the line before it. */
if (this_para == start_para && next_para->nFlags & MEPF_ROWSTART)
{
/* If the paragraph will be empty, then it should be deleted, however
* it still might have text right now which would inherit the
* MEPF_STARTROW property if we joined it right now.
* Instead we will delete it after the preceding text is deleted. */
if (nOfs > this_para->nCharOfs)
{
/* Skip this end of line. */
nChars -= (eollen < nChars) ? eollen : nChars;
continue;
}
keepFirstParaFormat = TRUE;
}
}
para_join( editor, c.para, keepFirstParaFormat );
/* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
ME_CheckCharOffsets(editor);
nChars -= (eollen < nChars) ? eollen : nChars;
continue;
}
else
{
ME_Cursor cursor;
int nCharsToDelete = min(nChars, c.nOffset);
int i;
c.nOffset -= nCharsToDelete;
para_mark_rewrap( editor, c.run->para );
cursor = c;
/* nChars is the number of characters that should be deleted from the
PRECEDING runs (these BEFORE cursor.pRun)
nCharsToDelete is a number of chars to delete from THIS run */
nChars -= nCharsToDelete;
shift -= nCharsToDelete;
TRACE("Deleting %d (remaining %d) chars at %d in %s (%d)\n",
nCharsToDelete, nChars, c.nOffset,
debugstr_run( run ), run->len);
/* nOfs is a character offset (from the start of the document
to the current (deleted) run */
add_undo_insert_run( editor, nOfs + nChars, get_text( run, c.nOffset ), nCharsToDelete, run->nFlags, run->style );
ME_StrDeleteV(run->para->text, run->nCharOfs + c.nOffset, nCharsToDelete);
run->len -= nCharsToDelete;
TRACE("Post deletion string: %s (%d)\n", debugstr_run( run ), run->len);
TRACE("Shift value: %d\n", shift);
/* update cursors (including c) */
for (i=-1; i<editor->nCursors; i++) {
ME_Cursor *pThisCur = editor->pCursors + i;
if (i == -1) pThisCur = &c;
if (pThisCur->run == cursor.run) {
if (pThisCur->nOffset > cursor.nOffset) {
if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
pThisCur->nOffset = cursor.nOffset;
else
pThisCur->nOffset -= nCharsToDelete;
assert(pThisCur->nOffset >= 0);
assert(pThisCur->nOffset <= run->len);
}
if (pThisCur->nOffset == run->len)
{
pThisCur->run = run_next( pThisCur->run );
assert( pThisCur->run );
pThisCur->nOffset = 0;
}
}
}
/* c = updated data now */
if (c.run == cursor.run) c.run->nCharOfs -= shift;
editor_propagate_char_ofs( editor, NULL, c.run, shift );
if (!cursor.run->len)
{
TRACE("Removing empty run\n");
ME_Remove( run_get_di( cursor.run ));
ME_DestroyDisplayItem( run_get_di( cursor.run ));
}
shift = 0;
continue;
}
}
if (delete_all) editor_set_default_para_fmt( editor, &start_para->fmt );
return TRUE;
}
BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
{
assert(nCursor>=0 && nCursor<editor->nCursors);
/* text operations set modified state */
editor->nModifyStep = 1;
return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
nChars, FALSE);
}
static struct re_object* create_re_object(const REOBJECT *reo, ME_Run *run)
{
struct re_object *reobj = malloc(sizeof(*reobj));
if (!reobj)
{
WARN("Fail to allocate re_object.\n");
return NULL;
}
ME_CopyReObject(&reobj->obj, reo, REO_GETOBJ_ALL_INTERFACES);
reobj->run = run;
return reobj;
}
HRESULT editor_insert_oleobj(ME_TextEditor *editor, const REOBJECT *reo)
{
ME_Run *run, *prev;
const WCHAR space = ' ';
struct re_object *reobj_prev = NULL;
ME_Cursor *cursor, cursor_from_ofs;
ME_Style *style;
HRESULT hr;
SIZEL extent;
if (editor->lpOleCallback)
{
hr = IRichEditOleCallback_QueryInsertObject(editor->lpOleCallback, (LPCLSID)&reo->clsid, reo->pstg, REO_CP_SELECTION);
if (hr != S_OK)
return hr;
}
extent = reo->sizel;
if (!extent.cx && !extent.cy && reo->poleobj)
{
hr = IOleObject_GetExtent( reo->poleobj, DVASPECT_CONTENT, &extent );
if (FAILED(hr))
{
extent.cx = 0;
extent.cy = 0;
}
}
if (reo->cp == REO_CP_SELECTION)
cursor = editor->pCursors;
else
{
cursor_from_char_ofs( editor, reo->cp, &cursor_from_ofs );
cursor = &cursor_from_ofs;
}
style = style_get_insert_style( editor, cursor );
if (ME_IsSelection(editor))
ME_DeleteSelection(editor);
run = run_insert( editor, cursor, style, &space, 1, MERF_GRAPHICS );
run->reobj = create_re_object( reo, run );
run->reobj->obj.sizel = extent;
prev = run;
while ((prev = run_prev_all_paras( prev )))
{
if (prev->reobj)
{
reobj_prev = prev->reobj;
break;
}
}
if (reobj_prev)
list_add_after(&reobj_prev->entry, &run->reobj->entry);
else
list_add_head(&editor->reobj_list, &run->reobj->entry);
ME_ReleaseStyle( style );
return S_OK;
}
void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
{
const WCHAR space = ' ';
ME_Cursor *cursor = editor->pCursors + nCursor;
ME_Style *style = style_get_insert_style( editor, cursor );
/* FIXME no no no */
if (ME_IsSelection(editor))
ME_DeleteSelection(editor);
run_insert( editor, cursor, style, &space, 1, MERF_ENDROW );
ME_ReleaseStyle( style );
}
void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
const WCHAR *str, int len, ME_Style *style)
{
const WCHAR *pos;
ME_Cursor *cursor = editor->pCursors + nCursor;
int oldLen;
/* FIXME really HERE ? */
if (ME_IsSelection(editor))
ME_DeleteSelection(editor);
oldLen = ME_GetTextLength(editor);
/* text operations set modified state */
editor->nModifyStep = 1;
assert(style);
if (len == -1) len = lstrlenW( str );
/* grow the text limit to fit our text */
if (editor->nTextLimit < oldLen + len) editor->nTextLimit = oldLen + len;
pos = str;
while (len)
{
/* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
while (pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
pos++;
if (pos != str) /* handle text */
run_insert( editor, cursor, style, str, pos - str, 0 );
else if (*pos == '\t') /* handle tabs */
{
const WCHAR tab = '\t';
run_insert( editor, cursor, style, &tab, 1, MERF_TAB );
pos++;
}
else /* handle EOLs */
{
ME_Run *end_run, *run, *prev;
ME_Paragraph *new_para;
int eol_len = 0;
/* Check if new line is allowed for this control */
if (!(editor->props & TXTBIT_MULTILINE))
break;
/* Find number of CR and LF in end of paragraph run */
if (*pos =='\r')
{
if (len > 1 && pos[1] == '\n')
eol_len = 2;
else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
eol_len = 3;
else
eol_len = 1;
}
else
{
assert(*pos == '\n');
eol_len = 1;
}
pos += eol_len;
if (!editor->bEmulateVersion10 && eol_len == 3)
{
/* handle special \r\r\n sequence (richedit 2.x and higher only) */
const WCHAR space = ' ';
run_insert( editor, cursor, style, &space, 1, 0 );
}
else
{
const WCHAR cr = '\r', *eol_str = str;
if (!editor->bEmulateVersion10)
{
eol_str = &cr;
eol_len = 1;
}
if (cursor->nOffset == cursor->run->len)
{
run = run_next( cursor->run );
if (!run) run = cursor->run;
}
else
{
if (cursor->nOffset) run_split( editor, cursor );
run = cursor->run;
}
new_para = para_split( editor, run, style, eol_str, eol_len, 0 );
end_run = para_end_run( para_prev( new_para ) );
/* Move any cursors that were at the end of the previous run to the beginning of the new para */
prev = run_prev( end_run );
if (prev)
{
int i;
for (i = 0; i < editor->nCursors; i++)
{
if (editor->pCursors[i].run == prev &&
editor->pCursors[i].nOffset == prev->len)
{
editor->pCursors[i].para = new_para;
editor->pCursors[i].run = run;
editor->pCursors[i].nOffset = 0;
}
}
}
}
}
len -= pos - str;
str = pos;
}
}
/* Move the cursor nRelOfs characters (either forwards or backwards)
* If final_eop is TRUE, allow moving the cursor to the end of the final eop.
*
* returns the actual number of characters moved.
**/
int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs, BOOL final_eop)
{
cursor->nOffset += nRelOfs;
if (cursor->nOffset < 0)
{
cursor->nOffset += cursor->run->nCharOfs;
if (cursor->nOffset >= 0)
{
/* new offset in the same paragraph */
do {
cursor->run = run_prev( cursor->run );
} while (cursor->nOffset < cursor->run->nCharOfs);
cursor->nOffset -= cursor->run->nCharOfs;
return nRelOfs;
}
cursor->nOffset += cursor->para->nCharOfs;
if (cursor->nOffset <= 0)
{
/* moved to the start of the text */
nRelOfs -= cursor->nOffset;
ME_SetCursorToStart(editor, cursor);
return nRelOfs;
}
/* new offset in a previous paragraph */
do {
cursor->para = para_prev( cursor->para );
} while (cursor->nOffset < cursor->para->nCharOfs);
cursor->nOffset -= cursor->para->nCharOfs;
cursor->run = para_end_run( cursor->para );
while (cursor->nOffset < cursor->run->nCharOfs)
cursor->run = run_prev( cursor->run );
cursor->nOffset -= cursor->run->nCharOfs;
}
else if (cursor->nOffset >= cursor->run->len)
{
ME_Paragraph *next_para;
int new_offset;
new_offset = ME_GetCursorOfs(cursor);
next_para = para_next( cursor->para );
if (new_offset < next_para->nCharOfs)
{
/* new offset in the same paragraph */
do {
cursor->nOffset -= cursor->run->len;
cursor->run = run_next( cursor->run );
} while (cursor->nOffset >= cursor->run->len);
return nRelOfs;
}
if (new_offset >= ME_GetTextLength(editor) + (final_eop ? 1 : 0))
{
/* new offset at the end of the text */
ME_SetCursorToEnd(editor, cursor, final_eop);
nRelOfs -= new_offset - (ME_GetTextLength(editor) + (final_eop ? 1 : 0));
return nRelOfs;
}
/* new offset in a following paragraph */
do {
cursor->para = next_para;
next_para = para_next( next_para );
} while (new_offset >= next_para->nCharOfs);
cursor->nOffset = new_offset - cursor->para->nCharOfs;
cursor->run = para_first_run( cursor->para );
while (cursor->nOffset >= cursor->run->len)
{
cursor->nOffset -= cursor->run->len;
cursor->run = run_next( cursor->run );
}
} /* else new offset is in the same run */
return nRelOfs;
}
BOOL
ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
{
ME_Run *run = cursor->run, *other_run;
ME_Paragraph *para = cursor->para;
int nOffset = cursor->nOffset;
if (nRelOfs == -1)
{
/* Backward movement */
while (TRUE)
{
nOffset = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_MOVEWORDLEFT );
if (nOffset) break;
other_run = run_prev( run );
if (other_run)
{
if (ME_CallWordBreakProc( editor, get_text( other_run, 0 ), other_run->len, other_run->len - 1, WB_ISDELIMITER )
&& !(run->nFlags & MERF_ENDPARA)
&& !(cursor->run == run && cursor->nOffset == 0)
&& !ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, 0, WB_ISDELIMITER ))
break;
run = other_run;
nOffset = other_run->len;
}
else
{
if (cursor->run == run && cursor->nOffset == 0)
{
para = run->para;
/* Skip empty start of table row paragraph */
if (para_prev( para ) && para_prev( para )->nFlags & MEPF_ROWSTART)
para = para_prev( para );
/* Paragraph breaks are treated as separate words */
if (!para_prev( para )) return FALSE;
para = para_prev( para );
run = para_end_run( para );
}
break;
}
}
}
else
{
/* Forward movement */
BOOL last_delim = FALSE;
while (TRUE)
{
if (last_delim && !ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_ISDELIMITER ))
break;
nOffset = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_MOVEWORDRIGHT );
if (nOffset < run->len) break;
other_run = run_next( run );
if (other_run)
{
last_delim = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset - 1, WB_ISDELIMITER );
run = other_run;
nOffset = 0;
}
else
{
ME_Paragraph *other_para = para_next( para );
if (!para_next( other_para ))
{
if (cursor->run == run) return FALSE;
nOffset = 0;
break;
}
if (other_para->nFlags & MEPF_ROWSTART) other_para = para_next( other_para );
if (cursor->run == run) {
para = other_para;
run = para_first_run( para );
}
nOffset = 0;
break;
}
}
}
cursor->para = para;
cursor->run = run;
cursor->nOffset = nOffset;
return TRUE;
}
static void
ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
{
/* pCursor[0] is the end of the selection
* pCursor[1] is the start of the selection (or the position selection anchor)
* pCursor[2] and [3] are the selection anchors that are backed up
* so they are kept when the selection changes for drag selection.
*/
editor->nSelectionType = selectionType;
switch(selectionType)
{
case stPosition:
break;
case stWord:
ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
editor->pCursors[1] = editor->pCursors[0];
ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
break;
case stParagraph:
editor->pCursors[1] = editor->pCursors[0];
editor->pCursors[0].run = para_end_run( editor->pCursors[0].para );
editor->pCursors[0].para = editor->pCursors[0].run->para;
editor->pCursors[0].nOffset = editor->pCursors[0].run->len;
editor->pCursors[1].run = para_first_run( editor->pCursors[1].para );
editor->pCursors[1].nOffset = 0;
break;
case stLine:
{
ME_Row *row = row_from_cursor( editor->pCursors );
row_first_cursor( row, editor->pCursors + 1 );
row_end_cursor( row, editor->pCursors, TRUE );
break;
}
case stDocument:
/* Select everything with cursor anchored from the start of the text */
ME_SetCursorToStart(editor, &editor->pCursors[1]);
ME_SetCursorToEnd(editor, &editor->pCursors[0], TRUE);
break;
default: assert(0);
}
/* Store the anchor positions for extending the selection. */
editor->pCursors[2] = editor->pCursors[0];
editor->pCursors[3] = editor->pCursors[1];
}
int ME_GetCursorOfs(const ME_Cursor *cursor)
{
return cursor->para->nCharOfs + cursor->run->nCharOfs + cursor->nOffset;
}
/* Helper function for cursor_from_virtual_coords() to find paragraph within tables */
static ME_Paragraph *pixel_pos_in_table_row( int x, int y, ME_Paragraph *para )
{
ME_Cell *cell, *next_cell;
assert( para->nFlags & MEPF_ROWSTART );
cell = table_row_first_cell( para );
assert( cell );
/* find the cell we are in */
while ((next_cell = cell_next( cell )) != NULL)
{
if (x < next_cell->pt.x)
{
para = cell_first_para( cell );
/* Found the cell, but there might be multiple paragraphs in
* the cell, so need to search down the cell for the paragraph. */
while (cell == para_cell( para ))
{
if (y < para->pt.y + para->nHeight)
{
if (para->nFlags & MEPF_ROWSTART) return pixel_pos_in_table_row( x, y, para );
else return para;
}
para = para_next( para );
}
/* Past the end of the cell, so go back to the last cell paragraph */
return para_prev( para );
}
cell = next_cell;
}
/* Return table row delimiter */
para = table_row_end( para );
assert( para->nFlags & MEPF_ROWEND );
assert( para->fmt.dwMask & PFM_TABLEROWDELIMITER );
assert( para->fmt.wEffects & PFE_TABLEROWDELIMITER );
return para;
}
static BOOL row_cursor( ME_TextEditor *editor, ME_Row *row, int x,
ME_Cursor *cursor )
{
ME_Run *run, *last;
BOOL exact = TRUE;
if (x < row->pt.x)
{
x = row->pt.x;
exact = FALSE;
}
run = row_first_run( row );
assert( run );
cursor->nOffset = 0;
do
{
if (x >= run->pt.x && x < run->pt.x + run->nWidth)
{
cursor->nOffset = ME_CharFromPoint( editor, x - run->pt.x, run, TRUE, TRUE );
cursor->run = run;
cursor->para = run->para;
return exact;
}
last = run;
run = row_next_run( row, run );
} while (run);
run = last;
cursor->run = run;
cursor->para = run->para;
return FALSE;
}
/* Finds the run and offset from the pixel position.
*
* x & y are pixel positions in virtual coordinates into the rich edit control,
* so client coordinates must first be adjusted by the scroll position.
*
* If final_eop is TRUE consider the final end-of-paragraph.
*
* returns TRUE if the result was exactly under the cursor, otherwise returns
* FALSE, and result is set to the closest position to the coordinates.
*/
static BOOL cursor_from_virtual_coords( ME_TextEditor *editor, int x, int y,
ME_Cursor *result, BOOL final_eop )
{
ME_Paragraph *para = editor_first_para( editor );
ME_Row *row = NULL, *next_row;
BOOL isExact = TRUE;
x -= editor->rcFormat.left;
y -= editor->rcFormat.top;
/* find paragraph */
for (; para_next( para ); para = para_next( para ))
{
if (y < para->pt.y + para->nHeight)
{
if (para->nFlags & MEPF_ROWSTART)
para = pixel_pos_in_table_row( x, y, para );
y -= para->pt.y;
row = para_first_row( para );
break;
}
else if (para->nFlags & MEPF_ROWSTART)
{
para = table_row_end( para );
}
}
/* find row */
while (row)
{
if (y < row->pt.y + row->nHeight) break;
next_row = row_next( row );
if (!next_row) break;
row = next_row;
}
if (!row && !final_eop && para_prev( para ))
{
/* The position is below the last paragraph, so the last row will be used
* rather than the end of the text, so the x position will be used to
* determine the offset closest to the pixel position. */
isExact = FALSE;
row = para_end_row( para_prev( para ) );
}
if (row) return row_cursor( editor, row, x, result ) && isExact;
ME_SetCursorToEnd(editor, result, TRUE);
return FALSE;
}
/* Sets the cursor to the position closest to the pixel position
*
* x & y are pixel positions in client coordinates.
*
* return TRUE if the run is directly under the pixel
* position, FALSE if it not.
*/
BOOL cursor_from_coords( ME_TextEditor *editor, int x, int y, ME_Cursor *cursor )
{
x += editor->horz_si.nPos;
y += editor->vert_si.nPos;
return cursor_from_virtual_coords( editor, x, y, cursor, FALSE );
}
/* Extends the selection with a word, line, or paragraph selection type.
*
* The selection is anchored by editor->pCursors[2-3] such that the text
* between the anchors will remain selected, and one end will be extended.
*
* editor->pCursors[0] should have the position to extend the selection to
* before this function is called.
*
* Nothing will be done if editor->nSelectionType equals stPosition.
*/
static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
{
ME_Cursor tmp_cursor;
int curOfs, anchorStartOfs, anchorEndOfs;
if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
return;
curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
tmp_cursor = editor->pCursors[0];
editor->pCursors[0] = editor->pCursors[2];
editor->pCursors[1] = editor->pCursors[3];
if (curOfs < anchorStartOfs)
{
/* Extend the left side of selection */
editor->pCursors[1] = tmp_cursor;
switch (editor->nSelectionType)
{
case stWord:
ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
break;
case stLine:
{
ME_Row *row = row_from_cursor( editor->pCursors + 1 );
row_first_cursor( row, editor->pCursors + 1 );
break;
}
case stParagraph:
editor->pCursors[1].run = para_first_run( editor->pCursors[1].para );
editor->pCursors[1].nOffset = 0;
break;
default:
break;
}
}
else if (curOfs >= anchorEndOfs)
{
/* Extend the right side of selection */
editor->pCursors[0] = tmp_cursor;
switch (editor->nSelectionType)
{
case stWord:
ME_MoveCursorWords( editor, &editor->pCursors[0], +1 );
break;
case stLine:
{
ME_Row *row = row_from_cursor( editor->pCursors );
row_end_cursor( row, editor->pCursors, TRUE );
break;
}
case stParagraph:
editor->pCursors[0].run = para_end_run( editor->pCursors[0].para );
editor->pCursors[0].para = editor->pCursors[0].run->para;
editor->pCursors[0].nOffset = editor->pCursors[0].run->len;
break;
default:
break;
}
}
}
void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
{
ME_Cursor tmp_cursor;
BOOL is_selection = FALSE, is_shift;
editor->nUDArrowX = -1;
x += editor->horz_si.nPos;
y += editor->vert_si.nPos;
tmp_cursor = editor->pCursors[0];
is_selection = ME_IsSelection(editor);
is_shift = GetKeyState(VK_SHIFT) < 0;
cursor_from_virtual_coords( editor, x, y, &editor->pCursors[0], FALSE );
if (x >= editor->rcFormat.left || is_shift)
{
if (clickNum > 1)
{
editor->pCursors[1] = editor->pCursors[0];
if (is_shift) {
if (x >= editor->rcFormat.left)
ME_SelectByType(editor, stWord);
else
ME_SelectByType(editor, stParagraph);
} else if (clickNum % 2 == 0) {
ME_SelectByType(editor, stWord);
} else {
ME_SelectByType(editor, stParagraph);
}
}
else if (!is_shift)
{
editor->nSelectionType = stPosition;
editor->pCursors[1] = editor->pCursors[0];
}
else if (!is_selection)
{
editor->nSelectionType = stPosition;
editor->pCursors[1] = tmp_cursor;
}
else if (editor->nSelectionType != stPosition)
{
ME_ExtendAnchorSelection(editor);
}
}
else
{
if (clickNum < 2) {
ME_SelectByType(editor, stLine);
} else if (clickNum % 2 == 0 || is_shift) {
ME_SelectByType(editor, stParagraph);
} else {
ME_SelectByType(editor, stDocument);
}
}
ME_InvalidateSelection(editor);
update_caret(editor);
ME_SendSelChange(editor);
}
void ME_MouseMove(ME_TextEditor *editor, int x, int y)
{
ME_Cursor tmp_cursor;
if (editor->nSelectionType == stDocument)
return;
x += editor->horz_si.nPos;
y += editor->vert_si.nPos;
tmp_cursor = editor->pCursors[0];
/* FIXME: do something with the return value of cursor_from_virtual_coords */
cursor_from_virtual_coords( editor, x, y, &tmp_cursor, TRUE );
ME_InvalidateSelection(editor);
editor->pCursors[0] = tmp_cursor;
ME_ExtendAnchorSelection(editor);
if (editor->nSelectionType != stPosition &&
memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
/* The scroll the cursor towards the other end, since it was the one
* extended by ME_ExtendAnchorSelection */
editor_ensure_visible( editor, &editor->pCursors[1] );
else
editor_ensure_visible( editor, &editor->pCursors[0] );
ME_InvalidateSelection(editor);
update_caret(editor);
ME_SendSelChange(editor);
}
static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
{
ME_Run *run = pCursor->run;
int x;
if (editor->nUDArrowX != -1)
x = editor->nUDArrowX;
else
{
x = run->pt.x;
x += ME_PointFromChar( editor, run, pCursor->nOffset, TRUE );
editor->nUDArrowX = x;
}
return x;
}
static void cursor_move_line( ME_TextEditor *editor, ME_Cursor *cursor, BOOL up, BOOL extend )
{
ME_Paragraph *old_para = cursor->para, *new_para;
ME_Row *row = row_from_cursor( cursor );
int x = ME_GetXForArrow( editor, cursor );
if (up)
{
/* start of the previous row */
row = row_prev_all_paras( row );
if (!row)
{
if (extend) ME_SetCursorToStart( editor, cursor );
return;
}
new_para = row_para( row );
if (old_para->nFlags & MEPF_ROWEND ||
(para_cell( old_para ) && para_cell( old_para ) != para_cell( new_para )))
{
/* Brought out of a cell */
new_para = para_prev( table_row_start( old_para ));
if (!new_para) return; /* At the top, so don't go anywhere. */
row = para_first_row( new_para );
}
if (new_para->nFlags & MEPF_ROWEND)
{
/* Brought into a table row */
ME_Cell *cell = table_row_end_cell( new_para );
while (x < cell->pt.x && cell_prev( cell ))
cell = cell_prev( cell );
if (cell_next( cell )) /* else - we are still at the end of the row */
row = para_end_row( cell_end_para( cell ) );
}
}
else
{
/* start of the next row */
row = row_next_all_paras( row );
if (!row)
{
if (extend) ME_SetCursorToEnd( editor, cursor, TRUE );
return;
}
new_para = row_para( row );
if (old_para->nFlags & MEPF_ROWSTART ||
(para_cell( old_para ) && para_cell( old_para ) != para_cell( new_para )))
{
/* Brought out of a cell */
new_para = para_next( table_row_end( old_para ) );
if (!para_next( new_para )) return; /* At the bottom, so don't go anywhere. */
row = para_first_row( new_para );
}
if (new_para->nFlags & MEPF_ROWSTART)
{
/* Brought into a table row */
ME_Cell *cell = table_row_first_cell( new_para );
while (cell_next( cell ) && x >= cell_next( cell )->pt.x)
cell = cell_next( cell );
row = para_first_row( cell_first_para( cell ) );
}
}
if (!row) return;
row_cursor( editor, row, x, cursor );
}
static void ME_ArrowPageUp( ME_TextEditor *editor, ME_Cursor *cursor )
{
ME_Row *row = para_first_row( editor_first_para( editor ) ), *last_row;
int x, yd, old_scroll_pos = editor->vert_si.nPos;
if (editor->vert_si.nPos < row->nHeight)
{
ME_SetCursorToStart( editor, cursor );
/* Native clears seems to clear this x value on page up at the top
* of the text, but not on page down at the end of the text.
* Doesn't make sense, but we try to be bug for bug compatible. */
editor->nUDArrowX = -1;
}
else
{
x = ME_GetXForArrow( editor, cursor );
row = row_from_cursor( cursor );
ME_ScrollUp( editor, editor->sizeWindow.cy );
/* Only move the cursor by the amount scrolled. */
yd = cursor->para->pt.y + row->pt.y + editor->vert_si.nPos - old_scroll_pos;
last_row = row;
while ((row = row_prev_all_paras( row )))
{
if (row_para( row )->pt.y + row->pt.y < yd) break;
last_row = row;
}
row_cursor( editor, last_row, x, cursor );
}
}
static void ME_ArrowPageDown( ME_TextEditor *editor, ME_Cursor *cursor )
{
ME_Row *row = para_end_row( para_prev( editor_end_para( editor ) ) ), *last_row;
int x, yd, old_scroll_pos = editor->vert_si.nPos;
x = ME_GetXForArrow( editor, cursor );
if (editor->vert_si.nPos >= row_para( row )->pt.y + row->pt.y - editor->sizeWindow.cy)
ME_SetCursorToEnd( editor, cursor, FALSE );
else
{
row = row_from_cursor( cursor );
/* For native richedit controls:
* v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
* v4.1 can scroll past this position here. */
ME_ScrollDown( editor, editor->sizeWindow.cy );
/* Only move the cursor by the amount scrolled. */
yd = cursor->para->pt.y + row->pt.y + editor->vert_si.nPos - old_scroll_pos;
last_row = row;
while ((row = row_next_all_paras( row )))
{
if (row_para( row )->pt.y + row->pt.y >= yd) break;
last_row = row;
}
row_cursor( editor, last_row, x, cursor );
}
}
static void ME_ArrowHome( ME_TextEditor *editor, ME_Cursor *cursor )
{
ME_Row *row = row_from_cursor( cursor );
row_first_cursor( row, cursor );
}
static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
{
ME_SetCursorToStart(editor, pCursor);
}
static void ME_ArrowEnd( ME_TextEditor *editor, ME_Cursor *cursor )
{
ME_Row *row = row_from_cursor( cursor );
row_end_cursor( row, cursor, FALSE );
}
static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
{
ME_SetCursorToEnd(editor, pCursor, FALSE);
}
BOOL ME_IsSelection(ME_TextEditor *editor)
{
return editor->pCursors[0].run != editor->pCursors[1].run ||
editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
}
void ME_DeleteSelection(ME_TextEditor *editor)
{
LONG from, to;
int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
int nEndCursor = nStartCursor ^ 1;
ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
editor->pCursors[nEndCursor] = editor->pCursors[nStartCursor];
}
ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
{
return style_get_insert_style( editor, editor->pCursors );
}
void ME_SendSelChange(ME_TextEditor *editor)
{
SELCHANGE sc;
sc.nmhdr.hwndFrom = NULL;
sc.nmhdr.idFrom = 0;
sc.nmhdr.code = EN_SELCHANGE;
ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
sc.seltyp = SEL_EMPTY;
if (sc.chrg.cpMin != sc.chrg.cpMax)
sc.seltyp |= SEL_TEXT;
if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
sc.seltyp |= SEL_MULTICHAR;
if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
{
ME_ClearTempStyle(editor);
editor->notified_cr = sc.chrg;
if (editor->nEventMask & ENM_SELCHANGE)
{
TRACE("cpMin=%ld cpMax=%ld seltyp=%d (%s %s)\n",
sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
(sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
(sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
}
}
}
BOOL
ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
{
int nCursor = 0;
ME_Cursor *p = &editor->pCursors[nCursor];
ME_Cursor tmp_curs = *p;
BOOL success = FALSE;
ME_CheckCharOffsets(editor);
switch(nVKey) {
case VK_LEFT:
if (ctrl)
success = ME_MoveCursorWords(editor, &tmp_curs, -1);
else
success = ME_MoveCursorChars(editor, &tmp_curs, -1, extend);
break;
case VK_RIGHT:
if (ctrl)
success = ME_MoveCursorWords(editor, &tmp_curs, +1);
else
success = ME_MoveCursorChars(editor, &tmp_curs, +1, extend);
break;
case VK_UP:
cursor_move_line( editor, &tmp_curs, TRUE, extend );
break;
case VK_DOWN:
cursor_move_line( editor, &tmp_curs, FALSE, extend );
break;
case VK_PRIOR:
ME_ArrowPageUp(editor, &tmp_curs);
break;
case VK_NEXT:
ME_ArrowPageDown(editor, &tmp_curs);
break;
case VK_HOME: {
if (ctrl)
ME_ArrowCtrlHome(editor, &tmp_curs);
else
ME_ArrowHome(editor, &tmp_curs);
break;
}
case VK_END:
if (ctrl)
ME_ArrowCtrlEnd(editor, &tmp_curs);
else
ME_ArrowEnd(editor, &tmp_curs);
break;
}
if (!extend)
editor->pCursors[1] = tmp_curs;
*p = tmp_curs;
ME_InvalidateSelection(editor);
ME_Repaint(editor);
hide_caret(editor);
editor_ensure_visible( editor, &tmp_curs );
update_caret(editor);
ME_SendSelChange(editor);
return success;
}