[Library] Edit v3.0 - Update and Manage any Edit Control

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jballi
Posts: 723
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by jballi » 08 May 2021, 20:27

heather1209 wrote:
08 May 2021, 08:24
Hi jballi,

I attempted to download QuickEdit, but the download link is no longer active. Where could I get it?
Thank you for your interest. QuickEdit was released as an experimental project in 2010. At the time, it was probably downloaded a few times as a curiosity but no one showed any interest in it and so it was abandoned. I've updated the first post to indicate that the software for the QuickEdit project is no longer available. I'm sorry I couldn't be more helpful.

tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by tuzi » 11 May 2021, 23:38

Thank you jballi.

All your projects have been the most detailed, clear and easy to understand I have ever seen.

I can always learn a lot from them.

I love it! :D :D

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 27 Jan 2022, 18:48

Hi jballi,

Thank you for this Edit library. Very useful and great source of learning.

Following up on posts earlier in this thread about end-of-lines in Edit controls, am I correct saying that:
- end-of-line encoding in a Windows standard Edit control is always CR+LF
- end-of-line encoding in the variable associated to an AHK Edit control (i.e. vMyEdit) is always LF only (`n)
- because of this, processing a string from an AHK Edit control having multiple lines using Edit_GetSel() is a issue (having an offset depending on the number of lines in the control)

I'm asking because I'm using Edit_GetSelText() to get the selected text from the control in a variable names strSelected and I'd like to also put the content before and after the selection in two other variables strBefore and strAfter. I tried with the following code. It works well as long as the text stands on one single-line (for example "1122334455" ans select "33").

Image

But it returns wrong values if the text is on multiple lines as in this example (select "33"):

Image

Of course, the issue grows with the number of lines in the control.

I did not find functions in your library that would make it possible to retrieve the text before and after the selected text in variables? Am I missing something? Or would you have other suggestions?

Code: Select all

#requires AutoHotkey v1.1
#SingleInstance,Force
#NoEnv
#Include %A_ScriptDir%\Lib\Edit.ahk ; from jballi https://www.autohotkey.com/boards/viewtopic.php?f=6&t=5063

Gui, Add, Edit, vMyEdit r10 w300 +hwndEditHwnd, 11`n22`n33`n44`n55
Gui, Add, Button, gGo w100, Go
Gui, Show

return

Go:
Gui, Submit, NoHide

strSelected := Edit_GetSelText(EditHwnd)
Edit_GetSel(EditHwnd, intStart, intEnd)

strBefore := SubStr(MyEdit, 1, intStart)
strAfter := SubStr(MyEdit, intEnd + 1)

MsgBox, Start: %intStart%`nEnd: %intEnd%`n`nBefore: >%strBefore%<`n`nSelected: >%strSelected%<`n`nAfter: >%strAfter%<

return
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

User avatar
jballi
Posts: 723
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by jballi » 28 Jan 2022, 15:46

Hey JnLlnd,

Thank you for your interest.

Yes, mixing Edit library functions with built-in AutoHotkey functionality can be problematic as you have discovered. When working directly with the Edit control, it's best to stick with library functions.

The function I think you're looking for is Edit_GetTextRange. It works with the character positions that were collected by the Edit_GetSel function.

Update example.

Code: Select all

#NoEnv
#requires AutoHotkey v1.1
#SingleInstance,Force

Gui, Add, Edit, vMyEdit r10 w300 +hwndEditHwnd, 11`n22`n33`n44`n55
Gui, Add, Button, gGo w100, Go
Gui, Show
return

Go:
;;;;;Gui, Submit, NoHide

strSelected := Edit_GetSelText(EditHwnd)
Edit_GetSel(EditHwnd, intStart, intEnd)

;;;;;strBefore := SubStr(MyEdit, 1, intStart)
strBefore:=Edit_GetTextRange(EditHwnd,0,intStart)
;;;;;strAfter := SubStr(MyEdit, intEnd + 1)
strAfter :=Edit_GetTextRange(EditHwnd,intEnd)

gui +OwnDialogs
MsgBox, Start: %intStart%`nEnd: %intEnd%`n`nBefore: >%strBefore%<`n`nSelected: >%strSelected%<`n`nAfter: >%strAfter%<
return

GUIEscape:
GUIClose:
ExitApp

;;;;;#Include %A_ScriptDir%\Lib\Edit.ahk ; from jballi https://www.autohotkey.com/boards/viewtopic.php?f=6&t=5063
#Include Edit.ahk ;-- Path removed for my tests
I hope this is helpful.

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 28 Jan 2022, 16:01

Great! Let's take a closer look at all the resources in this library. Thank you jlabbi.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 30 Jan 2022, 18:14

> When working directly with the Edit control, it's best to stick with library functions.

True. Everything I wanted to do works well that way. I can do all I wanted in very simple way.

---

Finding: when parsing lines in the control with Loop, Parse, the line marker parameter must take into account that the control end lines with CR+LF. For example when processing each line of the control:

Code: Select all

Loop, Parse, strLines, `r, `n ; `r (CR) marks the end of line, then skip the following `n (LF)
	strNumberedLines  .=  "#" . A_Index . ": " . A_LoopField . "`r`n" ; end of line with both CR+LF
---

Question: The library documentation mention the "Edit control's undo queue". In my tests, it seems that this "queue" is limited to one undo. Ctrl+Z toggle the last change and won't undo previous changes. Is this how the Edit control works or am I missing something?

Thanks again for sharing this lib.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

User avatar
jballi
Posts: 723
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by jballi » 31 Jan 2022, 12:52

JnLlnd wrote:
30 Jan 2022, 18:14
Finding: when parsing lines in the control with Loop, Parse, the line marker parameter must take into account that the control end lines with CR+LF. For example when processing each line of the control:

Code: Select all

Loop, Parse, strLines, `r, `n ; `r (CR) marks the end of line, then skip the following `n (LF)
	strNumberedLines  .=  "#" . A_Index . ": " . A_LoopField . "`r`n" ; end of line with both CR+LF
You are on the right track but parsing should be done on the `n (new line) character, not the `r (carriage return). For example:

Code: Select all

Loop, Parse, strLines,`n,`r
	strNumberedLines  .=  "#" . A_Index . ": " . A_LoopField . "`r`n" ; end of line with both CR+LF
The reason: The `n (new line) character marks the true end of the line and should be identified as the delimiter. The `r (carriage return) is the extra character in this case and should be identified as the character to omit. This syntax will work on DOS (`r`n), Unix (`n), and mixed DOS and Unix text whereas your syntax will only work on DOS text.
JnLlnd wrote:
30 Jan 2022, 18:14
Question: The library documentation mention the "Edit control's undo queue". In my tests, it seems that this "queue" is limited to one undo. Ctrl+Z toggle the last change and won't undo previous changes. Is this how the Edit control works or am I missing something?
You are correct. The Edit control has an undo queue size of 1.

The documentation was extracted from the Microsoft. The EM_UNDO message was designed to work on the Edit control and Rich Edit control. The Rich Edit control (starting with Rich Edit 2.0) supports more than one undo whereas the Edit control only supports 1 undo. The documentation was written generically to support both controls. Sorry 'bout that.

I hope this is helpful.

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 31 Jan 2022, 22:12

> The `n (new line) character marks the true end of the line and should be identified as the delimiter.

Got it. Thanks.

> The documentation was written generically to support both controls.

Understood.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 04 Feb 2022, 14:16

I'm doing good progress integrating your Edit library functions and code from your examples into my new project.

But I'm having an issue using the Find dialog box from your Dl22.ahk functions. It is about the visibility (foreground or focus, not sure the best way to describe it) of the Find dialog box vs the primary gui calling the find function. See this short video, it will explain the issue better than words:

https://youtu.be/DUPvw_QxLZg

My app is using your Edit library, includes the Edit\_Functions\Dlg2.ahk library and uses large parts of your "Example - General.ahk" script. Do you have any idea what command or parameter I missed that would explain why the primary gui is loosing the focus when the Find dialog box id displayed?

Thanks.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

User avatar
jballi
Posts: 723
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by jballi » 05 Feb 2022, 02:57

Hey, JnLlnd.

Thanks for the video. It was very helpful. Luckily, this is an easy one. You just need to add the ES_NOHIDESEL style when creating the Edit control. For example:

Code: Select all

ES_NOHIDESEL:=0x100
gui Add,Edit,w500 h300 %ES_NOHIDESEL%
I hope this helps.

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 05 Feb 2022, 17:13

Yes, it helped. Thanks :-)

Find next, Find previous and Replace implemented with ease! Just found that A_DetectHiddenWindows must be off for Find and Replace dialog boxes to show correctly.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 05 Feb 2022, 23:04

> Just found that A_DetectHiddenWindows must be off for Find and Replace dialog boxes to show correctly.

Hum... A_DetectHiddenWindows was the wrong solution to the issue I had... The issue is still present with detect off.

The issue: if I show the Find and Replace (ie FR) dialog box first, it is shown correctly. But if I show the Find dialog box first, trying to show the FR dialog box after that is showing the Find dialog (instead of the FR dialog box). This is probably related to this piece of code in your example:

Code: Select all

EEGUI_Replace:
gui %$EEGUI%:Default

;-- Bounce if Find or Replace dialog is already showing
IfWinExist ahk_id %hFRDialog%
    return
It seems that the Find dialog box still exists (even if closed properly), preventing the FR dialog box to be shown.

Here is a video showing this:
https://youtu.be/ChjDKSXbqmw

Any idea what piece would be missing from the code I borrowed from your example?
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

User avatar
jballi
Posts: 723
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by jballi » 07 Feb 2022, 04:48

JnLlnd wrote:
05 Feb 2022, 23:04
The issue: if I show the Find and Replace (ie FR) dialog box first, it is shown correctly. But if I show the Find dialog box first, trying to show the FR dialog box after that is showing the Find dialog (instead of the FR dialog box). This is probably related to this piece of code in your example:
Your video shows that you are pressing the "Close" button and so I'm fairly certain the dialog is being closed. You can confirm this by ensuring that the "C" event is being sent. This is the first test in the event function (Ex: OnReplace...) for both of these dialogs in my example script. After the event function returned, you can check for the existence of the a window with the hFRDialog handle or whatever you've named it your script. If used correctly, the dialogs are owned by the parent window. If they exist, they will be showing. If the parent window is showing, you cannot hide them (assuming normal operation).

As far as the IfWinExist ahk_id %hFRDialog% test is concerned, this just stops the subroutine from trying to open up another Find or Replace dialog. These subroutines are triggered by a menu command or a hotkey. Also, the only command used to show a Find dialog is the Dlg_FindText function and the only command to show the replace dialog is Dlg_ReplaceText function.

No, something else is happening in your code that is causing the Find dialog to show. This appears to be a good old-fashioned debugging task. I wish I could be more helpful.

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 07 Feb 2022, 12:20

After hour(s?) stepping through your code and my code with SciTE4AutoHotkey debugger to find the "difference", I was quite discouraged... I then called a friend to explain and show the issue. It often helps to show an issue to someone else and bounce ideas (even if he does not know a lot about my code). After 10 minutes showing the issue, I just realized (shame on me) that a "return" instruction was missing in the EEGUI_Init: command that I added in the header of the include file containing code from you, making the next command EEGUI_Find: to be called inadvertently each time a find or replace command was called. Bug fixed!

I share this "moment of life" as an example of the benefits of verbalizing an issue with a friend or colleague to see it with new eyes...

Thanks for your feedback, jballi.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

User avatar
jballi
Posts: 723
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by jballi » 07 Feb 2022, 17:54

Thank you for sharing. All programmers go through debugging hell every once in a while. Mixing in code from a 3rd party can certainly complicate the situation. I'm glad you were able to resolve the problem.

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 09 Mar 2022, 16:52

Hi jballi,

Selecting multiple pieces of text using the Ctrl key can be done in many word processors and editors. Is is possible to implement it in the standard Edit control? Or with RichEdit?

Thanks,
Jean
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

User avatar
jballi
Posts: 723
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by jballi » 10 Mar 2022, 00:06

JnLlnd wrote:
09 Mar 2022, 16:52
Selecting multiple pieces of text using the Ctrl key can be done in many word processors and editors. Is is possible to implement it in the standard Edit control? Or with RichEdit?
Multiselect is not available for the standard Edit control. Not sure about any other control.

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 05 Jan 2023, 15:59

I'm trying to position an "always on top" Gui window over a multiline Edit control at a position that won't cover the selected text or caret position in the Edit control. For this, I need to get the Y coordinate of the caret.

In the following code, I get the number of the line containing the caret and calculate its Y position (in pixels from the top of the screen) using various info from the screen and Gui1.

My question: is there a simpler way to get this result? Thanks.

Code: Select all

#requires AutoHotkey v1.1
#SingleInstance,Force
#NoEnv

#Include %A_MyDocuments%\AutoHotkey\Lib\Edit.ahk ; from jballi (https://www.autohotkey.com/boards/viewtopic.php?f=6&t=5063)

Loop, 40
	str .= "Lorem ipsum dolor sit amet, consectetur adipiscing elit...`n"

Gui, 1:New, ToolWindow, Gui1
Gui, Font, s11
Gui, 1:Add, Edit, +HwndMyEditHwnd w400 h400, %str%
Gui, 1:Show, AutoSize
Edit_SetSel(MyEditHwnd, 748, 759)
return

!q::

WinGetPos, , Gui1Y, , , A
SysGet, TitleBarH, 4
Edit_GetPos(MyEditHwnd, , EditY)

CaretLine := Edit_LineFromChar(MyEditHwnd) + 1 ; Edit line is 0 based
LineHeight := 12 / 72 * A_ScreenDPI
CaretY := Gui1Y + EditY + TitleBarH + (CaretLine * LineHeight)
Gui2Y := CaretY + LineHeight

Gui, 2:New, ToolWindow +HwndSecondGuiHwnd
Gui, 2:Add, Text, , % "Y position of Gui1 is " . Gui1Y
	. "`nHeight of title bar is " . TitleBarH
	. "`nY position of Edit control in Gui1 is: " . EditY
	. "`n`nCaret in Gui1 is on line:" . CaretLine
	. "`nLine height is: " . LineHeight . " pixels"
	. "`nScreen Y position of Caret is " . CaretY
	. "`nGui2 under position is " . Gui2YUnder
	. "`n"

Gui, 2:Show, AutoSize y%Gui2Y%
WinSet, AlwaysOnTop, On, ahk_id %SecondGuiHwnd%
WinActivate, Gui1

return
- - - - UPDATED - - - - -

The script updated using the A_CaretX/Y variables as suggested by ahk7 below. Launch the script to create Gui1. Then, place the caret a various positions and press Alt+q to see Gui2 displayed at the best position considering my needs. You can change the calculation for intGui2X and intGui2Y if your needs differ.

Code: Select all

#requires AutoHotkey v1.1
#SingleInstance,Force
#NoEnv

#Include %A_MyDocuments%\AutoHotkey\Lib\Edit.ahk ; from jballi (https://www.autohotkey.com/boards/viewtopic.php?f=6&t=5063)

Loop, 40
	str .= "Lorem ipsum dolor sit amet, consectetur adipiscing elit...`n"

Gui, 1:New, ToolWindow, Gui1
Gui, Font, s11
Gui, 1:Add, Edit, vEdit +HwndMyEditHwnd w370 h400, %str%
Gui, 1:Show, AutoSize

Edit_SetSel(MyEditHwnd, 738, 746)

return

!q::

; refresh gui1 position
WinGetPos, Gui1X, Gui1Y, Gui1W, Gui1H, Gui1

CoordMode, Caret, Window ; Window is the default
; prevent negative numbers when caret is not visible
intCaretX := (A_CaretX < 0 ? 20 : A_CaretX)
intCaretY := (A_CaretY < 0 ? 20 : A_CaretY)

LineHeight := 11 / 72 * A_ScreenDPI ; for font size 11

Gui, 2:New, ToolWindow +HwndSecondGuiHwnd, Gui2
Gui, 2:Add, Text, , % "`nLorem ipsum dolor sit amet.`n"
Gui, 2:Show, % AutoSize Hide

WinGetPos, , , Gui2W, Gui2H, Gui2 ; get gui2 size while it is hidden

; calculate best position: 
; X -> 2 lines under the caret or above it if gui2 exceeds gui1 bottom, always within gui1 space
; Y -> to the right of the caret, always within gui1 space 
intGui2X := Gui1X + (intCaretX + Gui2W > Gui1W ? Gui1W - Gui2W : intCaretX)
intGui2Y := Gui1Y + (intCaretY + Gui2H + (LineHeight * 2) > Gui1H ? intCaretY - Gui2H - (LineHeight * 2) : intCaretY + (LineHeight * 2))

; show gui2 at best possible position
Gui, 2:Show, % "x" . intGui2X . " y" . intGui2Y
WinSet, AlwaysOnTop, On, ahk_id %SecondGuiHwnd%
WinActivate, Gui1

return
Last edited by JnLlnd on 06 Jan 2023, 22:04, edited 2 times in total.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by ahk7 » 06 Jan 2023, 12:17

Wouldn't https://www.autohotkey.com/docs/v1/Variables.htm#Caret just work, it should show the X/Y for a standard edit control pretty reliably

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: [Library] Edit v2.0 - Update and Manage any Edit Control

Post by JnLlnd » 06 Jan 2023, 12:38

LOL. I knew there would be a simpler way :-/ I did not remember these AHK variables.

Thanks ahk7.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

Post Reply

Return to “Scripts and Functions (v1)”