Implementing SmartCopy

The following program implements sort of a smart copy in Visual FoxPro. When something is selected, the selected
text is copied. Otherwise the entire current line. So far, only Ctrl+C is implemented.
*==========================================================
* Implements SmartCopy. When nothing is highlighted, the
* entire line is copied.
*==========================================================

*--------------------------------------------------------
* Remove existing keyboard short cut
*--------------------------------------------------------
Release Bar _med_copy of _medit
DEFINE BAR _med_copy OF _medit PROMPT "\<Copy" ;
After _med_cut ;
PICTRES _med_copy ;
MESSAGE "Copies the selection onto the Clipboard"

*--------------------------------------------------------
* Invisible popup to capture short cut key.
*--------------------------------------------------------
Local lcFile
lcFile = Sys(16)
Define Popup SmartCopy
Define Bar 1 of SmartCopy prompt "Copy" Key Ctrl+C
On Selection Bar 1 of SmartCopy ;
do HandleCopy in "&lcFile"


*==========================================================
* Copies the entire line if nothing is selected, default
* behavior otherwise.
*==========================================================
Procedure HandleCopy

*--------------------------------------------------------
* Check if we need to execute the default behavior
*--------------------------------------------------------
Local llDefault
_Cliptext = ""
Sys(1500,"_med_copy","_medit")
Doevents
llDefault = not Empty(_Cliptext)

*--------------------------------------------------------
* Make sure, FoxTools.Fll is loaded.
*--------------------------------------------------------
If not "FOXTOOLS.FLL" $ Upper(Set("Library"))
Set Library to (Home()+"FoxTools.Fll") Additive
Endif

*--------------------------------------------------------
* Find current window handle
*--------------------------------------------------------
Local lnWHandle, laEnv[25]
If not m.llDefault
lnWHandle = _WOnTop()
If m.lnWHandle <= 0
llDefault = .T.
Else
Try
_EdGetEnv( m.lnWHandle, @laEnv )
llDefault = (laEnv[25] < 0)
Catch
llDefault = .T.
EndTry
EndIf
EndIf

*--------------------------------------------------------
* Get the current line content
*--------------------------------------------------------
Local lnCurPos, lnLineNo, lnStart, lnEnd
If not m.llDefault
lnCurPos = _EdGetPos( m.lnWHandle )
lnLineNo = _EdGetLNum( m.lnWHandle, m.lnCurPos )
lnStart = _EdGetLPos( m.lnWHandle, m.lnLineNo )
lnEnd = _EdGetLPos( m.lnWHandle, m.lnLineNo+1 )
If m.lnStart < m.lnEnd
lnEnd = m.lnEnd - 1
_Cliptext = _EdGetStr(m.lnWHandle,m.lnStart,m.lnEnd)
Endif
_EdSetPos(m.lnWHandle, m.lnCurPos)
EndIf

EndProc