Jump to content
Sign in to follow this  
therks

Modifying Ctrl+Right/Left Arrow behaviour in edit control

Recommended Posts

therks

I'm not sure what this feature is commonly called, but as you hold down Ctrl and move the caret about in an edit control it will skip over chunks of the text based on what characters are present. I was wondering if anyone knows of a way that I can modify this behaviour without writing a whole new function from scratch (I've already tried that but it gets complicated and buggy when you throw the Shift key into the mix).

In case anyone isn't sure what I'm talking about, try this example:

$gui = GUICreate('', 300, 100)
GUICtrlCreateEdit("ConsoleWrite(@AutoItExe & '.' & @AutoItVersion)", 0, 0, 300, 100)
GUISetState()

Do
Until GUIGetMsg() = -3

Hold Ctrl and move the caret around with the arrows. If you start at the front and move right you'll see it jumps all the way to the first '&'. Now copy that piece of text into Scite and do the same thing and you'll see it stops at the open bracket, and the '@' before hitting the '&'. In Scite this behaviour is modified with the word.characters property (for example in the au3.properties file it has word.characters.$(au3)=$(chars.alpha)$(chars.numeric)$(chars.accented).$_@#) I'm wondering if there's something similar (ie: a property of the control) that I can use to my advantage in my own application. Barring that, if anyone has already written a function to do this kind of thing I'd be more than grateful to try it out.

Thanks for reading.

Share this post


Link to post
Share on other sites
GEOSoft

I don't have an answer for you right now but here's something you can work on while you're waiting.

http://www.therks.com/autoit/tools/Txt2$var.au3


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
therks

I'm not sure what you wanted me to do with that. Just get it working I presume? The version on the site was old, I just uploaded the updated one though. It should work fine.

Share this post


Link to post
Share on other sites
therks

So I think I have it almost perfect (in my own code that is) but the issue I have is that there's no way to determine which end of a selection is the "active" end (ie: Which end of your selection moves when you move the arrow keys) and so I run into issues when the selection is changed outside of my handling function. Anybody know if there's a way to get that information from _GUICtrlEdit_GetSel? I'm going to look into the API stuff now, but if anyone could give me a pointer that'd be awesome.

Oh, here's a reproduction of what I've got working now:

#include <GUIConstants.au3>
#include <GUIEdit.au3>
#include <ScrollBarConstants.au3>

Opt('GUIOnEventMode', 1)
$hGUIMain = GUICreate('', 200, 200)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_Handler')
$hEditCtrl = GUICtrlCreateEdit('This is text. This is more.' & @CRLF & 'A path -> C:\Path\To\File.txt', 0, 0, 200, 200)
$dm_AccelWordLeft = GUICtrlCreateDummy()
    GUICtrlSetOnEvent(-1, '_MoveCaretByWord')
$dm_AccelWordShiftLeft = GUICtrlCreateDummy()
    GUICtrlSetOnEvent(-1, '_MoveCaretByWord')
$dm_AccelWordRight = GUICtrlCreateDummy()
    GUICtrlSetOnEvent(-1, '_MoveCaretByWord')
$dm_AccelWordShiftRight = GUICtrlCreateDummy()
    GUICtrlSetOnEvent(-1, '_MoveCaretByWord')

Global $accel[4][2] = [ _
    ['^{right}', $dm_AccelWordRight ], _
    ['^{left}', $dm_AccelWordLeft ], _
    ['^+{right}', $dm_AccelWordShiftRight ], _
    ['^+{left}', $dm_AccelWordShiftLeft ] ]
GUISetAccelerators($accel, $hGUIMain)

GUISetState()

While 1
    Sleep(1)
WEnd

Func _Handler()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
EndFunc

Func _MoveCaretByWord()
    Local Enum $SEL_NONE = -1, $SEL_LEFT, $SEL_RIGHT
    If Not IsDeclared('STATIC___iSelectAnchor') Then Global $STATIC___iSelectAnchor = $SEL_NONE
    If Not IsDeclared('STATIC___aSelectionMem') Then Global $STATIC___aSelectionMem[2]

    Local $sContent = _GUICtrlEdit_GetText($hEditCtrl)
    Local $aSelection = _GUICtrlEdit_GetSel($hEditCtrl)
    Local $iSelectJump

    Switch True
        Case $aSelection[$SEL_LEFT] = $aSelection[$SEL_RIGHT]
            $STATIC___iSelectAnchor = $SEL_NONE
        Case $aSelection[$SEL_RIGHT] <> $STATIC___aSelectionMem[$SEL_RIGHT]
            $STATIC___iSelectAnchor = $aSelection[$SEL_LEFT]
        Case $aSelection[$SEL_LEFT] <> $STATIC___aSelectionMem[$SEL_LEFT]
            $STATIC___iSelectAnchor = $aSelection[$SEL_RIGHT]
    EndSwitch

    Switch @GUI_CtrlId
        Case $dm_AccelWordLeft
            $iSelectJump = _WordSearchJump($sContent, $aSelection[$SEL_LEFT], $SEL_LEFT)
            _GUICtrlEdit_SetSel($hEditCtrl, $iSelectJump, $iSelectJump)
        Case $dm_AccelWordRight
            $iSelectJump = _WordSearchJump($sContent, $aSelection[$SEL_RIGHT], $SEL_RIGHT)
            _GUICtrlEdit_SetSel($hEditCtrl, $iSelectJump, $iSelectJump)
        Case $dm_AccelWordShiftLeft
            If $aSelection[$SEL_LEFT] < $STATIC___iSelectAnchor Then
                $iSelectJump = _WordSearchJump($sContent, $aSelection[$SEL_LEFT], $SEL_LEFT)
            Else
                $iSelectJump = _WordSearchJump($sContent, $aSelection[$SEL_RIGHT], $SEL_LEFT)
            EndIf
            If $STATIC___iSelectAnchor = $SEL_NONE Then
                _GUICtrlEdit_SetSel($hEditCtrl, $aSelection[$SEL_RIGHT], $iSelectJump)
                $STATIC___iSelectAnchor = $aSelection[$SEL_RIGHT]
            Else
                _GUICtrlEdit_SetSel($hEditCtrl, $STATIC___iSelectAnchor, $iSelectJump)
            EndIf
        Case $dm_AccelWordShiftRight
            If $aSelection[$SEL_RIGHT] > $STATIC___iSelectAnchor Then
                $iSelectJump = _WordSearchJump($sContent, $aSelection[$SEL_RIGHT], $SEL_RIGHT)
            Else
                $iSelectJump = _WordSearchJump($sContent, $aSelection[$SEL_LEFT], $SEL_RIGHT)
            EndIf
            If $STATIC___iSelectAnchor = $SEL_NONE Then
                _GUICtrlEdit_SetSel($hEditCtrl, $aSelection[$SEL_LEFT], $iSelectJump)
                $STATIC___iSelectAnchor = $aSelection[$SEL_LEFT]
            Else
                _GUICtrlEdit_SetSel($hEditCtrl, $STATIC___iSelectAnchor, $iSelectJump)
            EndIf
    EndSwitch

    $STATIC___aSelectionMem = _GUICtrlEdit_GetSel($hEditCtrl)
    _GUICtrlEdit_Scroll($hEditCtrl, $SB_SCROLLCARET)
EndFunc

Func _WordSearchJump(ByRef $sText, $iPosition, $iDirection = 0)
    Local $iCharGroup = 0
    Local $aCharGroups[3] = [ '[a-zA-Z0-9]', '\s', '[^a-zA-Z0-9\s]' ]
    If $iDirection Then $iPosition += 1

    Local $sChar = StringMid($sText, $iPosition, 1)

    For $i = 0 to UBound($aCharGroups)-1
        If StringRegExp($sChar, $aCharGroups[$i]) Then
            $iCharGroup = $i
            ExitLoop
        EndIf
    Next

    If $iDirection Then
        For $i = $iPosition to StringLen($sText)
            $sChar = StringMid($sText, $i, 1)
            If Not StringRegExp($sChar, $aCharGroups[$iCharGroup]) Then ExitLoop
        Next
        Return $i-1
    Else
        For $i = $iPosition to 0 Step -1
            $sChar = StringMid($sText, $i, 1)
            If Not StringRegExp($sChar, $aCharGroups[$iCharGroup]) Then ExitLoop
        Next
        Return $i
    EndIf
EndFunc

Share this post


Link to post
Share on other sites
GEOSoft

I'm not sure what you wanted me to do with that. Just get it working I presume? The version on the site was old, I just uploaded the updated one though. It should work fine.

Does this give you a hint?

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
therks

Oh that. Yeah that's an issue with the server. It's treating it like HTML for some reason. I never was able to figure that out. I always had to just save target from the links.

*Edit: And just found out that my host has implemented mime type options. I set .au3 as text/plain and now it looks fine. Is that better George? :idea:

Edited by therks

Share this post


Link to post
Share on other sites
GEOSoft

Oh that. Yeah that's an issue with the server. It's treating it like HTML for some reason. I never was able to figure that out. I always had to just save target from the links.

*Edit: And just found out that my host has implemented mime type options. I set .au3 as text/plain and now it looks fine. Is that better George? :idea:

Much better. It was a rel bummer trying to figure that out.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×