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

Post your working scripts, libraries and tools
iPhilip
Posts: 409
Joined: 02 Oct 2013, 12:21

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

10 Mar 2015, 23:34

Hi jballi,

Thank you for this great work. I am trying to get a script to interface with an edit control from another script. The following first script creates an edit control:

Code: Select all

Gui, +HwndhGui
Gui, Add, Edit, w100 h100, this is text
Gui, Show, AutoSize, Gui_title
ControlSend, Edit1, {Home}, ahk_id %hGui%
Return
The following second incomplete script is supposed to search for text in the first edit control, but doesn't work:

Code: Select all

hGui := WinExist("Gui_title ahk_class AutoHotkeyGUI")
ControlGet, hEdit, Hwnd,, Edit1, ahk_id %hGui%
hFindDialog := Dlg_FindText(hGui,"","is","OnFind")
Return

OnFind(hDialog,p_Event,p_Flags,p_SearchText,Dummy="") {
   global hEdit
   if p_Event = C
      Return
   FindPos := Edit_FindText(hEdit,p_SearchText)
   MsgBox %FindPos%
   Edit_SetSel(hEdit,FindPos,FindPos+StrLen(p_SearchText))
}

GuiClose:
ExitApp

#include Edit.ahk
#include Dlg2.ahk
Of course, if I combine the two scripts into one, it works. If you could give a hint or two on how to make this work, I would greatly appreciate it.

- iPhilip
P.S.: In actuality, the edit control is generated by a separate application which I cannot control.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
jballi
Posts: 568
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

11 Mar 2015, 05:42

OK, I finally figured out the problem. It took me a while because my brain was focused on other projects.

The problem appears to be with the Dlg2 library, not with the Edit library but I'll cover it here nevertheless.

The Dlg_FindReplaceText function uses the AutoHotkey OnMessage command to monitor for messages from the Find dialog. The OnMessage command only monitors messages used in it's own environment and so messages sent to a window in another environment are never trapped. That's why the OnFind function never fires when using your two script example.

The workaround is to specify a window in the current environment when calling the Dlg_FindText function. The Dlg_GetScriptDebugWindow function was designed specifically for this type of problem. Since the Find message is sent to a local window, the OnMessage command will correctly trap the message when it is sent. The drawback is that the Find dialog is not owned by the window that contains the Edit control and because of that, the dialog does not necessarily display in a logical location.

I hope this helps. I wrote a custom FindGUI function to help deal with this kind of problem. It doesn't fix all problems but it can help with dialog positioning. If you're interested, I'm sure I can track it down for you.
iPhilip
Posts: 409
Joined: 02 Oct 2013, 12:21

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

11 Mar 2015, 21:47

Thank you, jballi!!! This modified version of the second script now works:

Code: Select all

hGui := Dlg_GetScriptDebugWindow()
ControlGet, hEdit, Hwnd,, Edit1, Gui_title ahk_class AutoHotkeyGUI
hFindDialog := Dlg_FindText(hGui,"","is","OnFind")
Return

OnFind(hDialog,p_Event,p_Flags,p_SearchText,Dummy="") {
   global hEdit
   if p_Event = C
      Return
   FindPos := Edit_FindText(hEdit,p_SearchText)
   MsgBox %FindPos%
   Edit_SetSel(hEdit,FindPos,FindPos+StrLen(p_SearchText))
}

GuiClose:
ExitApp

#include Edit.ahk
#include Dlg2.ahk
I looked around for the FinGUI function you mentioned but could not find it. When you have a chance, I would appreciate getting a link to it.

- iPhilip
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
iPhilip
Posts: 409
Joined: 02 Oct 2013, 12:21

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

12 Mar 2015, 02:43

Hi jballi,

I did some further testing and found an issue with non-Gui windows. For example, after opening a Notepad window and typing "This is a test" in the window, the following script:

Code: Select all

#NoEnv
#SingleInstance Force

ControlGet, hEdit, Hwnd,, Edit1, ahk_class Notepad
hGui := Dlg_GetScriptDebugWindow()
Return   ; comment this line to make the script work reliably

#f::   ; comment this line to make the script work reliably
hFindDialog := Dlg_FindText(hGui,"","is","OnFind")
Return

OnFind(hDialog,p_Event,p_Flags,p_SearchText,Dummy="") {
   global hEdit
   if p_Event = C
      Return
   FindPos := Edit_FindText(hEdit,p_SearchText)
   MsgBox |%FindPos%| |%p_SearchText%|
}

#include Edit.ahk
#include Dlg2.ahk
doesn't work reliably (p_SearchText is blank) unless the Return and the #f:: lines are commented out. I am not sure what's happening but I wonder if it has to do with the handler registration.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
jballi
Posts: 568
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

12 Mar 2015, 05:48

I couldn't get it to fail.

A couple of things to try...

First of all, move the ControlGet, hEdit, Hwnd,, Edit1, ahk_class Notepad statement to immediately after the hotkey definition so that the handle to Notepad's Edit control is collected every time. That way, you can open and close Notepad at any time and script will still work.

Something like this:

Code: Select all

#NoEnv
#SingleInstance Force

hGui := Dlg_GetScriptDebugWindow()
Return   ; comment this line to make the script work reliably

#f::
ControlGet, hEdit, Hwnd,, Edit1, ahk_class Notepad
hFindDialog := Dlg_FindText(hGui,"","is","OnFind")
Return

OnFind(hDialog,p_Event,p_Flags,p_SearchText,Dummy="") {
   global hEdit
   if p_Event = C
      Return
   FindPos := Edit_FindText(hEdit,p_SearchText)
   MsgBox |%FindPos%| |%p_SearchText%|
}

#include Edit.ahk
#include Dlg2.ahk
The "p_SearchText is blank" and "reliably" key words are hints that sometimes you might be pressing Ctrl+F instead of Window+F to call the script. Since Notepad also uses the standard Find dialog, it may appear that the script is working but instead you are just calling the standard Find command. In these cases the "Find What" edit field will be blank. Of course, this may not be not it at all.

Anyway, some things to try. Let me know how it goes.
iPhilip
Posts: 409
Joined: 02 Oct 2013, 12:21

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

19 Mar 2015, 21:05

Hi jballi,

I followed your recommendations and found a stable solution by moving the ControlGet, hEdit, Hwnd,, Edit1, ahk_class Notepad statement so that the handle to Notepad's Edit control is collected every time and changing the hotkey to avoid the Windows key modifier (not sure why that made a difference but it could be that my keyboard is showing its age :problem: ). Here is the modified code that works:

Code: Select all

FormatTime, Time
FileAppend, Script loaded at %Time%`n`n, log.txt
hGui := Dlg_GetScriptDebugWindow()
Return

^6::
FileAppend, Hotkey %A_ThisHotkey% initiated by user`n, log.txt
IfWinNotExist ahk_id %hFindDialog%
{  ControlGet, hEdit, Hwnd,, Edit1, ahk_class Notepad
   hFindDialog := Dlg_FindText(hGui,"","is","OnFind")
}
WinActivate ahk_id %hFindDialog%
Return

OnFind(hDialog,p_Event,p_Flags,p_SearchText,Dummy="") {
   global hEdit
   if p_Event = C
      Return
   Edit_GetSel(hEdit,Dummy,EndPos)
   FindPos := Edit_FindText(hEdit,p_SearchText,EndPos)
   if FindPos = -1
      FindPos := Edit_FindText(hEdit,p_SearchText)
   Edit_SetSel(hEdit,FindPos,FindPos+StrLen(p_SearchText))
   if p_SearchText =
   {  FileAppend, FindPos:%FindPos%`nhEdit:%hEdit%`np_SearchText:<empty>`nReloading script ..., log.txt
      FileAppend, `n`n, log.txt
      MsgBox p_SearchText is empty!`nThe script will reload.
      Reload
      Return
   }
   FileAppend, FindPos:%FindPos%`nhEdit:%hEdit%`np_SearchText:%p_SearchText%`n, log.txt
}

#include Edit.ahk
#include Dlg2.ahk
Now if you could post the FinGUI function you mentioned ...

Thank you again for your help.
- iPhilip
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
jballi
Posts: 568
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

20 Mar 2015, 07:06

iPhilip, I will PM you a copy of the project that includes the FindGUI function.
iPhilip
Posts: 409
Joined: 02 Oct 2013, 12:21

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

20 Mar 2015, 13:26

Hi jballi

Edit_FindText() returns the wrong position when the edit control uses `r`n as newline delimiters instead of `n.
I implemented a workaround for myself by replacing `r`n with `n, but you might consider adding something to your function to deal with those situations.

- iPhilip
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
jballi
Posts: 568
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

20 Mar 2015, 15:32

iPhilip wrote:Hi jballi

Edit_FindText() returns the wrong position when the edit control uses `r`n as newline delimiters instead of `n.
I implemented a workaround for myself by replacing `r`n with `n, but you might consider adding something to your function to deal with those situations.

- iPhilip
I have a hunch that there may be confusion as to the position of a character or string in a file versus the position of the same in an Edit control.

The standard Edit control uses CR+LF (carriage return and line feed) to delimit each line. The Edit control will convert text to this format if necessary. So if one string contains "1`n2" and another string contains "1`r`n2", both will contain 4 characters, i.e. "1`r`n2", after it has been loaded to the Edit control. This rule is true no matter how the data is loaded to the Edit control -- file, paste, messages, etc. There are rare exceptions to this rule but there is nothing in your message to indicate that you are seeing any of these exceptions.

The Edit_FindText function returns the position of a string within the Edit control. It searches the control as though it were one long string. Even if it were possible to use different line delimiters within the control, it would still return the correct position of the found string.

If you are still able to duplicate the error even with this new information, please let me know. I will try to duplicate the problem using your test data.
iPhilip
Posts: 409
Joined: 02 Oct 2013, 12:21

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

20 Mar 2015, 20:43

Hi jballi,

You are right ... as usual. The problem I was having is not with the Edit_FindText() function but with the Edit_SetSel() function, which I was calling after Edit_FindText(). It turns out that the problem has to do with the fact that the edit control I am interacting with is a rich edit control, not a regular edit control. Still, I am able to get your Edit_FindText() and Edit_SetSel() functions to work with the rich edit control by getting all the text, replacing `r`n with `n, finding the location of the text, and then using the Edit_SetSel function to select the text. For sake of clarity, given a rich edit control whose content is 1`r`n2`r`n3, while this code in an OnFind function does NOT work:

Code: Select all

   p_SearchText := "2"
   FindPos := Edit_FindText(hEdit,p_SearchText)
   Edit_SetSel(hEdit,FindPos,FindPos+StrLen(p_SearchText))
This actually works:

Code: Select all

   p_SearchText := "2"
   FullText := Edit_GetText(hEdit,-1)
   FullText := RegExReplace(FullText, "`r`n", "`n")
   FindPos := Instr(FullText,p_SearchText)-1
   Edit_SetSel(hEdit,FindPos,FindPos+StrLen(p_SearchText))
I realize that rich edit controls are beyond the scope of your library but I wanted to let you know that your Edit_FindText() function is just fine and share this solution in case others find it helpful.

- iPhilip
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
Edd
Posts: 42
Joined: 16 Aug 2014, 16:45

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

29 Dec 2015, 18:05

Hello jballi, is there a way to not show any horizontal/vertical scrollbars while the text is on the visible area and when the text is longer than the height or vertical size of the textbox show the respective scrollbars?

Thank you.
User avatar
jballi
Posts: 568
Joined: 29 Sep 2013, 17:34

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

30 Dec 2015, 01:03

Edd wrote:Hello jballi, is there a way to not show any horizontal/vertical scrollbars while the text is on the visible area and when the text is longer than the height or vertical size of the textbox show the respective scrollbars?

Thank you.
Maybe, but I can't imagine doing it without a lot of overhead. Getting a script to hide and/or show the scroll bars is easy. Determining when to do is not.

If you really want to make this happen, PM me and we can discuss some ideas on how this might be accomplished.
User avatar
jballi
Posts: 568
Joined: 29 Sep 2013, 17:34

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

09 May 2016, 07:11

v2.0
Minor bug fix, several enhancements, several new functions, and new and updated examples. See the Release Notes section of the first post for the details.

This and future versions of the library will only run on AutoHotkey v1.1+.
ahk7
Posts: 198
Joined: 06 Nov 2013, 16:35

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

19 Feb 2017, 05:13

Have you considered preparing an easy to #include Editor?

Your "Example - General.ahk" is actually a decent editor which would enhance many scripts with its useful features (word wrap, convert case, find/replace etc) but looking at the code it is not easy to #include in an existing script.

I imagine something like a function you can call with the text you want to edit and the menu options you would like to have available (for example the option to disable the File-menu and include some buttons (OK & Cancel)).
User avatar
jballi
Posts: 568
Joined: 29 Sep 2013, 17:34

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

19 Feb 2017, 20:47

ahk7 wrote:Have you considered preparing an easy to #include Editor?

Your "Example - General.ahk" is actually a decent editor which would enhance many scripts with its useful features (word wrap, convert case, find/replace etc) but looking at the code it is not easy to #include in an existing script.

I imagine something like a function you can call with the text you want to edit and the menu options you would like to have available (for example the option to disable the File-menu and include some buttons (OK & Cancel)).
A pop-up editor window. Interesting idea.

I have some experience writing functions that manage a pop-up window so I can see myself writing something like this. OTOH, I just don't see much call for this function. A window with just a multi-line edit control will do 95%+ of what users need to do in an edit window. The rest is gravy. In addition, I can see the need for a large amount of code to be included in order to get the function to work. The Edit, Fnt, and Dlg2 libraries to start. The Spell library (and the files that go along with it) if you want to include Spell Check. And so on.

Do you have a specific requirement or idea of how/where you would use this? It would be helpful to know how you envision this being used.

Thank you for your interest.
ahk7
Posts: 198
Joined: 06 Nov 2013, 16:35

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

20 Feb 2017, 14:27

I'd imagine being able to do something like this:

Editor(MyText,[variadic?-]options-for-dimensions-menus-buttons-EditorTitle-etc-or-othersolution-INI-perhaps)

Practical case I have at hand is my Clipboard script, where I can edit items from the clipboard history,
at the moment it just uses a simple edit control but it has no features (find, replace, word wrap, upper, lower etc etc)

I would be useful for me to have these Editor functions available without too much of a hassle, I've been
hacking away at "Example - General" and have made some progress but the code ain't pretty and I would
still like to add some resizing for example using AutoXYWH(). So its a long way off from being OK.
And if you have improvements to the code I can do a nice game of "spot the difference" - having it
ready out of the box solves that of course and gives some flexibility from project to project (menus, buttons, etc)

Preview of my hack-job here https://github.com/hi5/CL3/issues/5 - showing the trimmed down menus, the buttons
(SAVE & CANCEL) and the current simple edit control beneath it which I'd like to replace with the improved editor.

Edit: I wouldn't mind "all the includes" because I don't have to edit them, just put them in my lib and forget about it
(until bugfixes and other improvements emerge but that takes a few seconds)
User avatar
jballi
Posts: 568
Joined: 29 Sep 2013, 17:34

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

21 Feb 2017, 15:16

Interesting. I will put some thought into it.
SOTE
Posts: 741
Joined: 15 Jun 2015, 06:21

Re: [Library] Edit v1.4 - Update and Manage any Edit Control

05 Jan 2018, 20:43

jNizM wrote:nice work jballi
but why you dont use github or something?
Appears to be exactly what happened. The Dropbox account is no longer active, and the files can't be downloaded. I think even Google Drive is superior to Dropbox, because they give you a significant amount of free space, and they are a much larger and more stable company that is less likely to go anywhere.

Of course, Github is the 1st option.

Any backup of the files or alternative download location?
iPhilip
Posts: 409
Joined: 02 Oct 2013, 12:21

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

05 Jan 2018, 21:22

Hi SOTE,

The OP has a link under "The Code" (Project: Edit.zip) that works for me.

Cheers!
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Google [Bot] and 34 guests