Skip to content

Instantly share code, notes, and snippets.

@GiuseppeChillemi
Created October 18, 2023 22:38
Show Gist options
  • Save GiuseppeChillemi/f042e28e310b24179ce60fdfeeef9e36 to your computer and use it in GitHub Desktop.
Save GiuseppeChillemi/f042e28e310b24179ce60fdfeeef9e36 to your computer and use it in GitHub Desktop.
Set-Caret does not work
Red [
title: "NoteManager Evolution"
needs: 'View
]
#system [
#import [
"user32.dll" stdcall [
SendMessage: "SendMessageW" [
hWnd [handle!]
msg [integer!]
wParam [integer!]
lParam [integer!]
return: [handle!]
]
]
]
get-char: func [
p [byte-ptr!]
unit [integer!]
return: [integer!]
/local
p4 [int-ptr!]
][
switch unit [
Latin1 [as-integer p/value]
UCS-2 [(as-integer p/2) << 8 + p/1]
UCS-4 [p4: as int-ptr! p p4/value]
]
]
sniff-chars: func [
p [byte-ptr!]
tail [byte-ptr!]
unit [integer!]
quote [int-ptr!]
nl [int-ptr!]
/local
cp [integer!]
p4 [int-ptr!]
][
while [p < tail][
cp: switch unit [
Latin1 [as-integer p/value]
UCS-2 [(as-integer p/2) << 8 + p/1]
UCS-4 [p4: as int-ptr! p p4/value]
]
switch cp [
#"^"" [quote/value: quote/value + 1] ;"
#"^/" [nl/value: nl/value + 1]
default [0]
]
p: p + unit
]
]
adjust-selection: func [
str [red-string!]
bgn [int-ptr!]
end [int-ptr!]
inc [integer!] ;-- +1 to increase, -1 to decrease
/local
quote [integer!]
nl [integer!]
unit [integer!]
unit-b [integer!]
cp [integer!]
size [integer!]
s [series!]
head [byte-ptr!]
tail [byte-ptr!]
p [byte-ptr!]
p-bgn [byte-ptr!]
p-end [byte-ptr!]
][
assert bgn/value <= end/value
if TYPE_OF(str) <> TYPE_STRING [exit]
s: GET_BUFFER(str)
unit: GET_UNIT(s)
unit-b: log-b unit
head: (as byte-ptr! s/offset) + (str/head << unit-b)
tail: as byte-ptr! s/tail
either inc > 0 [
p-bgn: head + (bgn/1 << unit-b)
p-end: head + (end/1 << unit-b)
quote: 0 nl: 0
sniff-chars head p-bgn unit :quote :nl
bgn/1: bgn/1 + nl
sniff-chars p-bgn p-end unit :quote :nl
end/1: end/1 + nl
][
p: head
while [p < tail] [
cp: get-char p unit
if cp = as-integer #"^/" [
size: (as-integer p - head) >> unit-b
case [
size >= end/1 [break]
size >= bgn/1 [end/1: end/1 - 1]
true [bgn/1: bgn/1 - 1 end/1: end/1 - 1]
]
]
p: p + unit
]
]
]
]
get-caret1: routine [handle [handle!] text [string!] return: [integer!] /local a b] [
a: 0 b: 0
SendMessage as handle! handle/value B0h as-integer :a as-integer :b
adjust-selection text :a :b -1
b
]
set-caret1: routine [handle [handle!] text [string!] where [integer!] /local junk] [
junk: where
adjust-selection text :where :junk 1
SendMessage as handle! handle/value B1h where where
]
get-caret: func [face [object!]][get-caret1 face/state/1 face/text]
set-caret: func [face [object!] pos [integer!]][set-caret1 face/state/1 face/text :pos]
tttt: {
a
b
c
d
e
f
ifuweghfukwhef
wefwefw
wfefwefwefwe
fwe
gt
ebh
ertb
etbertbetbetbetb
}
caret-pos: 15
view [
aa: area no-border wrap focus tttt
button "Works" [
view compose [text (to-string get-caret aa)]
set-caret aa caret-pos
set-focus aa
]
button "Doesn't work" [
set-caret aa caret-pos
set-focus aa
]
button "Doesn't Work2" [
set-caret aa caret-pos
view compose [text (to-string get-caret aa)]
set-focus aa
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment