Hello...
I want to do something with an edit control but I have no idea on how to do it...

I would like to change the mouse cursor when it is over an url an launch this url (like in some irc clients)...
Does somebody have any idea on how to know what is word the mouse is over ?

Thanks.
Posted on 2002-02-19 10:27:23 by JCP
hm, i did something like this with own-drawn bmp
buttons... maybe you can use this for links, too...



.DATA

UrlRect rect <X,Y,X+WIDTH,Y+HEIGHT>
flag db 0

.CODE

.ELSEIF uMsg == WM_MOUSEMOVE

mov ecx,lParam
movzx eax,cx
shr ecx,16

invoke PtInRect,addr UrlRect, eax, ecx
test eax,eax
jz _nope

cmp flag,0
jnz _quit ; check if the mouse hovers
; the first time over our url...
mov flag,1
invoke SetCursor,... ; nope, so let's set a flag and
; set a new cursor shape
ret

_nope: cmp flag,1 ; mouse shape is changed but
jnz _quit ; the mouse is out of our url region?

mov flag,0 ; so clear the flag and set the
invoke SetCursor,... ; old shape

_quit: ret


EDIT: this is written from mind so i bet you've to play
around to get it working... look in your api ref to know
how to change the mouse shape... good luck...
Posted on 2002-02-19 10:49:27 by mob
It is nice thanks but it doesn't seem to apply to my problem :

In my edit box, I have text (may vary), and what I want is, when the mouse is over an url text , show a different cursor and launch the site on click...

I don't know if I'm very clear :(
Posted on 2002-02-19 10:53:25 by JCP
hm a text-area? ok, this makes the whole thing ALOT
harder... don't know if there is some easy way, maybe
richedit can do something for you?
Posted on 2002-02-19 11:06:50 by mob
Yes, in a text area... in fact, like a webbrowser, except it is not really hyperlink nor html webpage (plain txt).

Thanks for the richedit advice, I thought about it, too, but I can't really use it here (or at last I much prefer avoid it)... :(
Posted on 2002-02-19 11:14:55 by JCP
please forgive me if this is totaly wrong...

you can use EM_POSFROMCHAR to get the coordinates of
a char in your edit-box... i would try something like this:
search for occurences of [---text---] before you copy the
text into your edit-box (you should use the brackets to
indicate a link) and store the index of every link... copy
the whole mess into your edit-box and create an array
with the X,Y values and the text lenght/width... (use
EM_POSFROMCHAR for this purpose)... now you can check
if the mouse hovers over a link via WM_MOUSEMOVE
(use this proc above... all you have to do is make it more
flexible with a loop the goes through your link_rect array)

uhm hey, thats interesting :) if you give me your code
i can try it for you coz i never wrote something like this
before...
Posted on 2002-02-19 11:37:19 by mob
Mob,

very good idea, thank you...
It think it is the simplest way...

I will maybe try to do without using the brackets... (by inserting some "prefixes" like http://, or mailto...) but the code will be more complex... ;)

if you give me your code
i can try it for you coz i never wrote something like this
before...


Thanks... there's no code at this moment, I will begin from a simple window with an edit on it... I just thought about this for one of my programs...
I will try to do it this week-end as I'm running out of time the others days (work)... but if come up with something, I will let you know... ;)


(If you or somebody else has other ideas, please feel free to post).
Posted on 2002-02-19 11:49:23 by JCP
ok, hold your breath for this giant code/pseudo-code jumble...



edit-box text

123456789
___________
1: |bla bla |
2: | this |
3: | IS a|
4: | [LINK] |
-----------

the index of the link is: 9*3+1 = 28
the lenght is 6 say the height is 22pixel
and the width is 22pixel * 6 = 132pixel

.DATA?

myrect STRUCT
_left DD ?
_right DD ?
_top DD ?
_bottom DD ?
_lenght DD ?
_index DD ?
myrect ENDS

links myrect 100 dup (<?>) ; make room for say 100...
; links
.CODE
assume edi: PTR myrect
mov edi, offset links
mov ebx, offset TextBuffer

loop:

; // if a link was found then fill up your array
; // with the character-index (ebx) and the lenght

mov [edi]._index, ebx
mov [edi]._lenght, the_lenght
add edi, sizeof(myrect) ;next one

; // jump to loop until you reach the end of your text...

; // copy everything into the text-box

; // MSG handler

.ELSEIF uMsg == WM_MOUSEMOVE

mov edi, offset links

_l00p: cmp [edi]._index,0 ; unused -> last one?
jnz _exit

invoke SendDlgMessage,hWnd,DIALOGBOX_ID,/ ;refresh coz
WM_POSFROMCHAR,edi,[edi]._index ;of The scrollbar

mov ecx,edi
movzx eax,cx
shr ecx,16

invoke PtInRect,EDI, eax, ecx
test eax,eax
jz _nope

cmp flag,0
jnz _quit ; check if the mouse hovers
; the first time over our url...
mov flag,1
invoke SetCursor,... ; nope, so let's set a flag and
; set a new cursor shape
ret

_nope: cmp flag,1 ; mouse shape is changed but
jnz _quit ; the mouse is out of our url region?

mov flag,0 ; so clear the flag and set the
invoke SetCursor,... ; old shape

_quit: add edi, sizeof(myrect)
jmp _l00p

_exit: ret


hm this is what i would try to do... :stupid:
Posted on 2002-02-19 12:14:42 by mob
Thanks for this pseudo-pseudo code snippet. ;)

I have some questions about it, but I think I will see when I will try to code it...

How do you store the lenght of the url ?
In pixels right ?
Then you need to use GetTextExtentPoint32 or similar ?

If you end up with a working snippet before me, please let me know. ;)

Thanks...
Posted on 2002-02-20 01:34:55 by JCP
hm, i would at first use a font like courier (fixed-size-font)...
you can set the font and the font-size with CreateFont and
WM_SETFONT... so if it's a fixed-size you can calculate your
link width easily with STRINGLEN*FONTWIDTH i think...
you have to store the lenght in pixels right, but you have
to add it to your rect structure.

say we filled up the struct with something like
this: link <100,100,0,0,6,155> ;x,y,top,bottom,len,index
now we need to fix the two zeros to obtain a fully working
rect for PtInRect... say we know the the height and width
of our choosen font is... 22... the new updated struc will
look like: link <X, Y, X+(6*22), Y+22, LEN(6), INDEX>
-> link <100, 100, 232, 122, 6, 155> ... if the y value is
negative the url should be (api-ref) outside of the client
area...

if you ask me this sounds very adventuresome :stupid:
Posted on 2002-02-20 03:51:22 by mob
you could also use GetTextMetrics to get the current font metrics selected in the device context
Posted on 2002-02-20 05:26:27 by savage
Readiosys,
here is another option for you, it may turn out tidier:

- use EM_CHARFROMPOS to get the char they are pointing to
- retrieve the preceding and following chars until you hit the first whitespace each way (urls cannot have whitespace in them)
- see if the sequence of chars you have identified fits the rules of a url (i.e. starts with http/ftp, it may be a four section ip number, if it is a text url (not numbers) then check it has a valid ccTLD (.com, .nz, etc))
- if it fits the criteria for a url, change the text color via EM_SETSEL or EM_SETRECT, and change the cursor
- when the mouse moves, reset the color of any text you set, and the mouse cursor.
- launching the url is simple: do a ShellExecute(Ex) on the sequence of chars you have identified as a url.

I think using this logic(?) will make it easy to set your code up into small discrete functions, and keep your code looking good :grin:
Posted on 2002-02-20 17:48:23 by sluggy
yeah but it will be slower... on every mousemove you'll
end up backward-scanning for "http" occourences... but
you're right, the code would look MUCH nicer :) btw, how
can you access the text stored in a edit-box? i only know
wm_gettext or em_getline(/index)... can i use the window
handle to reach the text-buffer in any way? if that would
be the case your em_charfrompos method would be a
perfect solution (it's still slower but it would be far more
flexible)...
Posted on 2002-02-21 03:27:19 by mob
YOA this is great... (besides it's just a quik unoptimized buggy hack)
Posted on 2002-02-21 07:06:36 by mob
grarg... damn msg board...
Posted on 2002-02-21 07:37:14 by mob
hi mob, your "quik unoptimized buggy" was true. its buggy. btw, i had re-write only part of the code. but because i'm still dont have msdn ref here, i cant proceed more further. i just stump thinking about what sluggy said:


if it fits the criteria for a url, change the text color via EM_SETSEL or EM_SETRECT, and change the cursor


well, is it true i can colours a separated text? i had search the board and found no sample about that. just see changing whole editbox color with WM_CTLblah. could any of you make an example here, mob, or sluggy or other?

thanks
Posted on 2003-01-19 20:14:32 by dion
as you can see i wrote this from mind and then tried to get it working.
this is not the normal behaviour to write asm code :)

mh this is a normal edit control... i don't know if it supports custom
painting but i doubt that. you can do it for sure with richedit. maybe
there are a few subclassing hints for the normal one... however,
TBD did something like that...

link

BTW. i tested this demo only in w2k... do you have any problems
with it? does it crash?
Posted on 2003-01-20 04:29:34 by mob
Hi,

rich edit control since version 2.0 has some build-in support for URLs (EN_LINK notification, CFE_LINK). Wouldnt it be better to use that?

Japheth
Posted on 2003-01-20 05:51:16 by japheth
thank mob, i'll check that one. i'm adding another url text in the string and your app got crash in seconds after i moving mouse around.

japheth : i know richedit would do it. but as other reason risen, i had feeling that richedit was somewhat heavy to load. i just refer it to wordpad app for example. i just want a simple like editbox, meanwhile i dont need to hold large text too.
Posted on 2003-01-20 20:29:59 by dion
hi mob, TBD's sample was using RichEdit, not editbox :(
Posted on 2003-01-21 01:35:19 by dion