Scintilla controls: set text works only if #NoEnv is on Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Scintilla controls: set text works only if #NoEnv is on

19 Nov 2017, 01:34

- I've been working on some functions for getting/setting the text in controls, and I noticed that JEE_SciSetTextAlt and JEE_SciPasteAlt only work if #NoEnv is on.
- They were working in my main script, but not in another script that didn't have #NoEnv set.
- I'm mentioning this in case anyone can figure out why this might happen, or in case it relates to anyone else's issues with a script.
- Warning: JEE_SciSetTextAlt erases any text in the current Scintilla control, and JEE_SciPasteAlt would overwrite the selected text.

Code: Select all

#NoEnv
q:: ;get/set Scintilla text e.g. test on Notepad2 or Notepad++
ControlGet, hCtl, Hwnd,, Scintilla1, A
vText := Chr(8730) "abcde" Chr(8730)
JEE_SciSetTextAlt(hCtl, vText)
JEE_SciPasteAlt(hCtl, vText)
MsgBox, % JEE_SciGetTextAlt(hCtl)
. "`r`n" JEE_SciGetTextAlt(hCtl, "CP0")
. "`r`n" JEE_SciGetTextAlt(hCtl, "UTF-16")
return

;==================================================

JEE_SciGetTextAlt(hCtl, vEnc:="UTF-8")
{
	ControlGetText, vText,, % "ahk_id " hCtl
	return StrGet(&vText, vEnc)
}

;==================================================

;appears to need #NoEnv to work
JEE_SciSetTextAlt(hCtl, vText)
{
	vSize := StrPut(vText, "UTF-8") + 2
	VarSetCapacity(vUtf8, vSize, 0)
	StrPut(vText, &vUtf8, "UTF-8")
	ControlSetText,, % vUtf8, % "ahk_id " hCtl
}

;==================================================

;appears to need #NoEnv to work
JEE_SciPasteAlt(hCtl, vText)
{
	vSize := StrPut(vText, "UTF-8") + 2
	VarSetCapacity(vUtf8, vSize, 0)
	StrPut(vText, &vUtf8, "UTF-8")
	Control, EditPaste, % vUtf8,, % "ahk_id " hCtl
}

;==================================================
Last edited by jeeswg on 19 Nov 2017, 12:39, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Scintilla controls: set text works only if #NoEnv is on

19 Nov 2017, 12:01

Interesting, looks like a bug. However, ControlSetText and Control don't have to work with any controls, as I understand, they are desighned for Windows native controls. For Scintilla there are its messages: SCI_GETTEXT, SCI_SETTEXT, SCI_ADDTEXT. Also, using UTF-8 encoding is not quite correct. The code page can be different, it depends on user preferences, so you need to figure out the code page with SCI_GETCODEPAGE.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Scintilla controls: set text works only if #NoEnv is on

19 Nov 2017, 12:07

- Yes, you're right, there is no reason why these methods *should* work, but it happens that they do, if you use #NoEnv.
- I will be sharing some scripts that get/set a Scintilla control's text, in the *right* way, via SCI_GETTEXT and SCI_SETTEXT.
- Thanks for the info re. SCI_ADDTEXT, and I meant to look for something like SCI_GETCODEPAGE so thanks for that as well.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: Scintilla controls: set text works only if #NoEnv is on

19 Nov 2017, 18:44

Try checking if any of part of your script shares a name with an Environment Variable.
Please excuse my spelling I am dyslexic.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Scintilla controls: set text works only if #NoEnv is on  Topic is solved

19 Nov 2017, 19:56

As per the documentation on varsetcapacity, you need to update the variables internally stored string length, that is, varsetcapacity(vUtf8,-1). Otherwise it is considered a blank variable, and then, if you did not specify #noenv, it will be checked if it is an env. variable, it isn't, and "" is passed to the command. With #noenv specified, the command will just pass on the pointer to the variable's string buffer, which is a null terminated string, in a WM_SETTEXT message to the control. You could also send WM_SETTEXT directly to the control, passing &vUtf8.

Cheers :wave:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Scintilla controls: set text works only if #NoEnv is on

19 Nov 2017, 21:12

Great, so to fix it I just needed to add VarSetCapacity(vUtf8, -1) after each StrPut line. Although, just to state again, this was very hacky, and not really the proper way to get/set the text. Since using ControlGetText typically retrieved one character, I tried a few things, hence these 3 functions. The hacky approach is far shorter in lines than the official approach that I will share soon in my control get/set text library.

@Capn Odin: Yes, I had thought that possibly a variable might be an issue, I kept looking up and down the script, but not seeing a candidate variable, and it appears ultimately that there wasn't one.
@Helgef: Cheers. :beer:
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jameswrightesq, wpulford and 431 guests