Class RichEdit - update on 2015-04-14 (v0.1.05.00)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

14 May 2015, 20:25

Oh - if I use a 1 instead of appname then the control will display....
Now it works.

However, if I add gMessage as one of the options, then the control dissappears.

Code: Select all

Message:
	guicontrolget, myedit
	msgbox, Contents %myedit%
return
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

15 May 2015, 01:08

Hi BGM,

New RichEdit() expects that the Gui the control shall be added to is specified by name, number, or even HWND in the first parameter. As you were told in your other thread, your variable appname contains a string which isn't used as a Gui name and Gui, +Hwnd%appname% won't store the HWND in appname but in a variable named by the content of appname, SimpleRichEdit in this case. So if you want to use appname, you have to use a double-deref: MyEdit := new richedit(%appname%,"w75 vmyedit gMessage", false).

Your second problem is caused by the fact, that you use icentical names for the RichEdit object returned by New RichEdit() and the control's variable ( I never tried to pass a vName within the options and am still wondering that it works at all). Soguicontrolget, myedit will overwrite the RichEdit object MyEdit destroying the object and the control.

The following seems to work:

Code: Select all

#NoEnv
SetBatchLines, -1
#include Class_RichEdit.ahk

appname := "SimpleRichEdit"
gosub, mainui
return
;------------------------------
mainui:
    Gui, +Hwnd%appname%
    MyRichEdit := new richedit(%appname%,"w75 vmyedit gMessage", false)
    MyRichEdit.AlignText("RIGHT")
    Gui, Show, h200 w300 center,%appname%
Return
;------------------------------
Message:
    guicontrolget, myedit
    ToolTip, My contents are %myedit%
return
;------------------------------
GuiClose:
ExitApp
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

15 May 2015, 09:12

Just Me - thank you for the explanation. For the appname, I was suspecting I had the same problem as I did in the other thread...

Now, I used vmyedit in the control options so that I could get AutoXYWH to work on it - and it does work. In fact, using the v-name seems to work just like you'd think! I can even get and send values through it. However, I am glad that you pointed out to me that I was overwriting the entire control. I should have realized that, in fact.

Thank you!
(Now I just have to figure out how to colourize things...)

So, I'm looking through samples and I see a call to ToggleFontStyle, but I can't find that function anywhere to see what it does.
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

15 May 2015, 12:10

Okay!

Here is a working SIMPLE example of using the RichText Class for folks who just want the basics.

It demonstrates:
  • How to add the control
  • How to use ToggleFontStyle (which I did find in the class file)
  • How to colourize using richtext tags
  • How to insert and clear text content
  • How to fetch the content using the v-variable in the control's options
screenshot_AutoHotkey (AutoHotkey Unicode 32-bit)_005.png
screenshot_AutoHotkey (AutoHotkey Unicode 32-bit)_005.png (5.35 KiB) Viewed 5712 times

Code: Select all

#NoEnv
SetBatchLines, -1
#include Class_RichEdit.ahk

appname := "SimpleRichEdit"
gosub, mainui
gosub, loadhotkeys
return
;------------------------------
mainui:
   Gui, +Hwnd%appname%
    MyRichEdit := new richedit(%appname%,"w250 vmyedit", true)
    MyRichEdit.AlignText("RIGHT")
    Gui, Show, h120 w270 center,%appname%
Return
;------------------------------
Message:
   guicontrolget, myedit
    ToolTip, My contents are %myedit%
    settimer, removemessage, 1000
return
RemoveMessage:
	settimer, removemessage, off
	tooltip
return
;------------------------------
LoadHotkeys:
	hotkey, IfWinActive, % "ahk_id " %appname% ; <------
	hotkey, ^b, MakeBold
	hotkey, ^+b, MakeBold_alt
	hotkey, ^g, MakeColoured
	hotkey, ^t, Message
	hotkey, ^q, Clear
return
;------------------------------
;For syntax using the richtext tags:
;http://www.pindari.com/rtf1.html

;Demonstrate using the class' built-in ToggleFontStyle for the current selection
MakeBold:
	MyRichEdit.ToggleFontStyle("B")
return
;Demonstrate setting of tag-styled text
MakeBold_alt:
	MyRichEdit.SetText("{\rtf My Name is \b1 " . appname . "\b0}", ["SELECTION"])
return

MakeColoured:
	colortable := "{\colortbl `;\red0\green176\blue80;\red0\green77\blue187`;\red255\green0\blue0`;}"
	greentext := "\cf1 green text,\cf0"
	bluetext := "\cf2  blue text,\cf0"
	redtext := "\cf3  red text\cf0"	
	MyRichEdit.SetText("{\rtf " . colortable . "Colouized text: " . greentext . bluetext . redtext . ".}", ["SELECTION"])
return
Clear:
	MyRichEdit.SetText("")
return
;------------------------------
GuiClose:
ExitApp
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

16 May 2015, 04:18

Hi just me, It looks like the hyperlinks are not properly saved to rtf - when opened or pasted to Word, they appear black and not underlined.
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

16 May 2015, 05:36

Did you turn AutoURLDetection on? If so, I don't know what to do.
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

16 May 2015, 07:12

Yes, auto url is on. The link is properly displayed in the control but not when loaded to office apps.
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

16 May 2015, 09:11

Perhaps there is a similar setting for Office?
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

16 May 2015, 12:38

Now I see that it works in WordPad but not in Office 2013. Maybe it works in older applications and RTF specification has changed in new Office... I also noticed that in Office 2013 not all the pasted font colors are correct.
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

23 May 2015, 09:32

On the richtext control, if I want to remove the sunken look and just have a flat white box,
is this right? It doesn't seem to have any effect.

Code: Select all

hEdit1 := new richedit("mainui"," y0 w75 h24 vEdit1 gAction", false)
hEdit1.AlignText("RIGHT")
Control, ExStyle, -E0x200, , ahk_id %hEdit1%
In the contructor for the control class, I see these styles added:
WS_EX_CLIENTEDGE := 0x200
WS_EX_STATICEDGE := 0x20000
ES_SUNKEN := 0x4000

So, I've tried this:

Code: Select all

Control, ExStyle, ^0x200, , ahk_id %hEdit1% 
Control, ExStyle, ^0x20000, , ahk_id %hEdit1%
Control, ExStyle, ^0x4000, , ahk_id %hEdit1%
and I've tried adding -0x200 -0x20000 -0x4000 to the control declaration.

Using Winspy++ I see that the offending style is WS_EX_STATICEDGE
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

23 May 2015, 09:51

Uh, never mind; I solved it:

Control, ExStyle, ^0x20000, , % ahk_id %hEdit1%
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

23 May 2015, 21:44

In the library, there are two functions:
ReplaceSel(Text := "") { ; Replaces the selected text with the specified text.
and
SetText(ByRef Text := "", Mode := "") { ; Replaces the selection or the whole content of the control.

The first one appears to only replace the selection, as it says.
But the second one, if I am not mistaken, shouldn't replace the selection, but rather always the entire text (despite the comment) - am I right?

[update]
Okay, I am right. The comment should read:
Replaces the whole content of the control.
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

25 May 2015, 09:08

When I use
hedit1.AlignText(2), it moves the text down by one pixel
in other words, without AlignText, the text is up one pixel
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

25 May 2015, 10:27

BGM wrote:Okay, I am right. The comment should read:
Replaces the whole content of the control.
EM_SETTEXTEX
When I use
hedit1.AlignText(2), it moves the text down by one pixel
in other words, without AlignText, the text is up one pixel
This may be right, but it isn't done by the script, it's done by the control.
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

25 May 2015, 10:51

Just Me - so I have to manually compensate? Wouldn't it better to compensate within the libarary control's code?
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

25 May 2015, 13:10

All the script does is sending messages to the control. The script isn't liable for what the control does when receiving messages with correct parameters. Also, my old eyes cannot confirm your observation. Do you still fill the control with predefined RTF? Then this might be the reason.
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

25 May 2015, 13:51

Never mind! I can't reproduce it now!

Actually, I think that it's just this:
when I set the height to the same as a text label control and line them up on the same y axis, THEN it is off by one pixel - at least one of them is off.
I don't have time right now, but I'll post an example soon.
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

28 May 2015, 22:56

I am using AutoHotkey 1.1.22.00 Unicode on Windows 7 Pro x64, and have a richtext control next to a regular ahk text control (a label), using Arial font.
The text label will display a unicode character, such as 0x0180 which is a funny ƀ character, but the richtext control does not display it.

Is there a trick to make the richtext control display unicode characters?
When I paste in a unicode character, it displays a ? instead.
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

29 May 2015, 00:14

RE.PNG
RE.PNG (4.05 KiB) Viewed 5529 times
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

29 May 2015, 09:50

Oh - yeah, I forgot about that. I guess there's no trick.
(Sorry, I know I am a nuisance)

I figured it out - when I use the rtf colour coding it is messing up the letters that are being displayed.
If I don't use the rtf codes then it displays perfectly.

So, as it turned out, you can only use ansi in the rtf code - I was trying to use a unicode character.
Unicode characters have to be described in the rtf code by their code-numbers.
I have been using hmyEdit.gettext() to fetch the text from the control and then to wrap it with rtf codes and then using hmyEdit.settext() to replace it - and this works - except for unicode chars.
So I guess I have to find a way to change all the unicode chars in the variable into their codepoints.

This fixed it (with GeekDude's help).
It changes all the unicode characters in the string to their rtf unicode code. When you settext() this back to the richtext control the characters are shown properly.

Code: Select all

	loop, parse, whatstring
	{
		if(asc(a_loopfield) > 0xFF){
			thischar := "\u" . asc(a_loopfield) . "?" 
		}else{
			thischar := a_loopfield
		}
		newstring .= thischar
	}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 123 guests