[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
JnLlnd
Posts: 513
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

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

06 Jan 2023, 22:06

I updated my example script above using the A_CaretX/Y variables as suggested by ahk7. 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.

In the end, this has little to do with the Edit library. Sorry jballi for for hijacking your thread...
: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: 513
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

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

05 Feb 2023, 19:02

@jballi Any plan to migrate the Edit.ahk library to AHK v2?
: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: 728
Joined: 29 Sep 2013, 17:34

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

09 Feb 2023, 05:58

JnLlnd wrote:
05 Feb 2023, 19:02
@jballi Any plan to migrate the Edit.ahk library to AHK v2?
Short answer: Eventually.

I'm fairly sure that converting the Edit project to AutoHotkey 2.0+ will take considerable time and effort. Converting the library and add-ons shouldn't be that difficult but converting all the examples and all of the libraries and add-ons that the examples include will be a major project. I have not been able to wrap my head around all of the work that would be involved. I also haven't identified how I will create a development environment for v2.0+ that will not interfere with 1.1+.
User avatar
JnLlnd
Posts: 513
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

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

09 Feb 2023, 08:24

I understand. I have the same challenges with my apps. I won't all convert them but I think there will be mid-term benefits to convert Quick Clipboard Editor to v2, especially the future development described here by Lexikos. But there is no ruwh.

Regarding your development environment it depends what tools you use. Having v1.1 and v2 installed on the same machine is easy and well supported. With SciTE4Autohotkey, it is also easy to switch from one version to the other.
: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: 513
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

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

20 Mar 2023, 16:45

Here is a piece of code to move a line up or down using the Edit library commands. If you have suggestion to improve it, you are welcome.

Code: Select all

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

Gui, Add, Edit, w200 h150 -Wrap +hwndMyEdit, % "12345678`nABCDEFGH`nabcdefgh`n12345678`nABCDEFGH`nabcdefgh`n"
Gui, Show
Edit_SetSel(MyEdit, -1)
return

^Up::Gosub, MoveLineUp
^Down::Gosub, MoveLineDown

;---------------------
MoveLineUp:
MoveLineDown:

if Edit_IsWordWrap(MyEdit) ; works only if word wrap is turned off
	return

intSelStart := Edit_GetSel(MyEdit)
intSelLine := Edit_LineFromChar(MyEdit, intSelStart) ; line of selection start, zero-based
; check if move is out of bound
if (A_ThisLabel = "MoveLineUp" and intSelLine < 1
	or (A_ThisLabel = "MoveLineDown" and intSelLine + 3 > Edit_GetLineCount(MyEdit)))
	return
	
Critical, On
GuiControl, -Redraw, %MyEdit%

intLineStart := Edit_LineIndex(MyEdit)
intLineEnd := Edit_LineIndex(MyEdit, intSelLine + 1) ; includes inding CRLF
Edit_SetSel(MyEdit, intLineStart, intLineEnd)
strMovedLine := Edit_GetSelText(MyEdit)
Edit_Clear(MyEdit) ; clear selected moved text

intDestInsert := Edit_LineIndex(MyEdit, intSelLine + (A_ThisLabel = "MoveLineUp" ? -1 : 1))
Edit_SetSel(MyEdit, intDestInsert, intDestInsert) ; insertion point for moved text
Edit_ReplaceSel(MyEdit, strMovedLine) ; insert moved text
Edit_SetSel(MyEdit, intDestInsert, intDestInsert + StrLen(strMovedLine) - 1) ; select moved text (excluding CRLF)

GuiControl, +Redraw, %MyEdit%
Critical, Off
Edit_ScrollCaret(MyEdit) ; make sure the leftmost position of the selection is visible

return
;---------------------


;---------------------
GuiClose:
ExitApp
;---------------------
: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: 591
Joined: 06 Nov 2013, 16:35

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

20 Mar 2023, 17:14

@JnLlnd I think that is already done by using Edit_BlockMove (up and down)
User avatar
JnLlnd
Posts: 513
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

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

20 Mar 2023, 17:30

@ahk7: Ah... I forgot these additional functions. It was fun to code but I could have save these few hours for something else... 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: 728
Joined: 29 Sep 2013, 17:34

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

04 Feb 2024, 21:30

v3.0
Major new release. See the Release Notes section of the first post for the details.

This version only runs on AutoHotkey v1.1+. AutoHotkey v2.0+ is not supported. With the exception of bug fixes, this will be the last version that uses AutoHotkey v1.1+. Future versions of this project (if any) will require AutoHotkey v2.0.
User avatar
JnLlnd
Posts: 513
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

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

05 Feb 2024, 18:58

Thanks for this major update @jballi . Can't wait to review all these improvements.
: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: 513
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

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

16 Mar 2024, 22:31

Hi @jballi . Just a follow up on v3 release. I had no issue with existing functions and the only changes I had to do were about functions that were moved to Edit_Util.ahk.

I took the opportunity to add the spell checker to my editor. It's very well done and easy to implement. I considered using the new Zoom feature but will wait because my app could be used on Win 7.

Thanks again for all this!
: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: 728
Joined: 29 Sep 2013, 17:34

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

17 Mar 2024, 10:09

Thank you for your interest. I'm glad that you find the Edit library useful.

About the Zoom feature...

For Windows 10+, Zoom is a feature that is built-in to the Edit control. Once enabled, the user will be able to Zoom in and out without any other programming.

The key is the Edit_EnableZoom function. After the Edit control has been created, just call it once to enable Zoom. Once enabled, the user can use Ctrl+WheelUp and Ctrl+WheelDown to zoom in and out. The other Zoom functions in the Edit library are included just in case you want to add some additional Zoom-related functionality but they are not required.

So, what happens when you call Edit_EnableZoom on a Windows 7 machine? To be honest, I don't know. I don't have access to a Window 7 machine anymore. I'm betting that nothing bad happens but you would have to give it a try to see. If it does nothing on Windows 7 then it should be safe to call on all versions of Windows but it would only work on Windows 10+.

Them be my thoughts. Good luck!
User avatar
JnLlnd
Posts: 513
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

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

17 Mar 2024, 12:17

I've tested Edit_EnableZoom and it worked well on my Win 10 system. I don't have access to a Win 7 system myself to test it. I would assume the function does nothing.

I already implemented a Zoom feature in my app using an UpDown control and Ctrl+WheelUp and Ctrl+WheelDown hotkeys to increase/decrease the font size. I'd prefer to use the new zoom function but I'd have to keep the old way as an alternative for Win7 users.
: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: 591
Joined: 06 Nov 2013, 16:35

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

29 Mar 2024, 12:14

I needed it quickly so cobbled together a trim whitespace function, either selected text or all text in the edit control and trim leading, trailing, or both (default is both). Undo works, but caret position or keeping text selected is out the window :)

Code: Select all

Edit_Trim(hEdit)    ; trim leading+trailing
Edit_Trim(hEdit,1) ; trim leading
Edit_Trim(hEdit,2) ; trim trailing

Code: Select all

;-----------------------------
;
; Function: Edit_Trim
;
; Description:
;
;   Trim leading/trailing white space. If nothing is selected, the entire text is trimmed.
;
; Options:
;   1: Leading
;   2: Trailing
;   3: Leading & Trailing (default)
;
; Type:
;
;   Add-on function.
;
; Calls To Other Functions:
;
; * <Edit_TextIsSelected>
; * <Edit_GetSelText>
; * <Edit_GetText>
; * <Edit_ReplaceSel>
; * <Edit_ScrollCaret>
; * <Edit_SelectAll>
; * <Edit_SetSel>
;
; Remarks:
;
; * Undo can be used to reverse this action.
;
;-------------------------------------------------------------------------------
Edit_Trim(hEdit,trim=3)
    {
    ;-- Initialize
    l_LastSelectedLineIncludesEOL:=False

    If Edit_TextIsSelected(hEdit)
       l_SelectedText:=Edit_GetSelText(hEdit)
    else
       l_SelectedText:=Edit_GetText(hEdit)

    ;[========]
    ;[  Trim  ]
    ;[========]
    If (trim = 3) ; leading & trailing
       trimfunc:="Trim"
    else If (trim = 2) ; trailing
       trimfunc:="RTrim"
    else ; leading
       trimfunc:="LTrim"

    loop, parse, l_SelectedText, `n, `r
       l_NewText .= %trimfunc%(A_LoopField, " `t`r`n") "`r`n"
    l_NewText:=SubStr(l_NewText,1, -1)

    ;-- Replace selected text with new text
    If Edit_TextIsSelected(hEdit)
       Edit_ReplaceSel(hEdit,l_NewText)
    else {
       Edit_SelectAll(hEdit)
       Edit_ReplaceSel(hEdit,l_NewText)
       }

    ;[==============]
    ;[  Reposition  ]
    ;[==============]
    Edit_ScrollCaret(hEdit)
        ;-- This keeps the original selection in view

    Edit_LineScroll(hEdit,"Left")
        ;-- Fixes a display problem that sometimes occurs if adding lines that
        ;   are wider than the control
    }
User avatar
jballi
Posts: 728
Joined: 29 Sep 2013, 17:34

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

29 Mar 2024, 14:56

Looks interesting. I'll give a try. Thanks!
chogus
Posts: 2
Joined: 04 Jun 2024, 20:27

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

04 Jun 2024, 20:37

Hi jballi,

First of all, thank you for all the work you've put into this library, it has done wonders for the work I do daily. I had a two questions I was hoping you or anyone else could help with. After selecting a block of text, I've noticed that I'm having trouble selecting the intended text with the following methods Edit_FindText and Edit_SetSel. It seems as if I need to build in some kind of offset by the number of line carriages I have.

Code: Select all

StringPos := Edit_FindText(pscribehwnd, "NODULES:")
;not sure why but we have to subtract the line breaks
TrueStringPos := StringPos - Edit_LineFromChar(pscribehwnd, StringPos)
Edit_SetSel(pscribehwnd, TrueStringPos,-1)
Edit_ReplaceSel(pscribehwnd, "String I want to send")
In addition, every once in awhile I'll get this critical error with the Edit_ReplaceSel function:
COM returned an unexpected error code: Details are RPC_E_CANTCALLOUT_ININPUTSYNCCALL.

I think it may have to do with other processes trying to access the RichEdit I'm trying to manipulate with your library, and I was wondering if there were any methods in the library or functions you might be aware of that would allow me to check if it's okay to manipulate the RichEdit before actually executing.

Thank you again for the wonderful work you do.


[Mod edit: Added [code][/code] tags. Please use them yourself when posting code.]
User avatar
jballi
Posts: 728
Joined: 29 Sep 2013, 17:34

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

06 Jun 2024, 10:34

@chogus, thank you for your interest. I'm sorry that you are experiencing problems using the Edit library.

Debugging complex remote problems can be very difficult and often impossible. However, you gave a clue that could explain most of the problems.

Many of the Edit control messages will also work on the RichEdit control but there are many exceptions and differences. The Edit library should not be used on a RichEdit control. As you are discovering, some of the Edit library functions work as expected, but others do not. I can't say for sure that the COM error you are experiencing is caused by sending unsupported messages to the RichEdit control but I wouldn't rule it out.

I did a quick search and found that there are AutoHotkey libraries that are designed to be used on the RichEdit control. I have never used them so I won't link to them here. Just do a search for RichEdit and hopefully you will find a library that will work for you. Good luck!
chogus
Posts: 2
Joined: 04 Jun 2024, 20:27

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

06 Jun 2024, 10:59

Hi jballi,

Thank you for the thoughtful reply. I will say the functions in your library that do work are amazing and will continue to use them, but will take your suggestion and look into RichEdit libraries. Thanks again!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 136 guests