udfStrCompact
str udfStrCompact (str, int, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrCompact (strString, intWidth, intMode)
If strString == "" Then Return ""
If intMode == 0 Then Return strString
strEllipsis = "..."
intLenEllipsis = 3
strDelims = "\ "

strPre = ""
strMid = strString
strPost = ""

blnIsPath = !!(intMode & 4)

If blnIsPath ; Split path string into PathRoot, FolderPath, FileBaseName.
   hdlBB = BinaryAlloc (260) ; MAX_PATH = 260
   BinaryPokeStr (hdlBB, 0, strString)
   intResult = DllCall ("SHLWAPI.DLL", long : "PathStripToRootA", lpbinary : hdlBB)
   strPre = "" ; PathRoot.
   If intResult Then strPre = BinaryPeekStr (hdlBB, 0, 260)
   hdlBB = BinaryFree (hdlBB)
   strMid = StrSub (FilePath (strString), StrLen (strPre) + 1, -1) ; FolderPath.
   strPost = FileBaseName (strString) ; FileBaseName.
EndIf

intLenPre = StrLen (strPre)
intLenMid = StrLen (strMid)
intLenPost = StrLen (strPost)
intWidth = intWidth - intLenEllipsis - intLenPre - intLenPost

Switch @TRUE
Case (intMode & 3) == 3 ; Ellipsis in the middle.
   intPosL = intWidth - (intWidth / 2) - 1
   intPosR = intLenMid - intPosL
   If intMode == 3 Then Return StrSub (strMid, 1, intPosL) : strEllipsis : StrSub (strMid, intPosR, -1)
   If intMode == 7 Then Return strPre : StrSub (strMid, 1, intPosL) : strEllipsis : StrSub (strMid, intPosR, -1) : strPost
   If (intMode == 11) || (intMode == 15)
      intPosL = StrScan (strMid, strDelims, intPosL, @BACKSCAN)
      intPosR = StrScan (strMid, strDelims, intPosR, @FWDSCAN)
      Return strPre : StrSub (strMid, 1, intPosL) : strEllipsis : StrSub (strMid, intPosR, -1) : strPost
   EndIf
   Break
Case (intMode & 2) == 2 ; Ellipsis at the right side.
   strMid = StrSub (strMid, 1, intWidth)
   If intMode == 2 Then Return strMid : strEllipsis
   If intMode == 6 Then Return strPre : strMid : strEllipsis : strPost
   If (intMode == 10) || (intMode == 14)
      intPosL = StrScan (strMid, strDelims, intWidth, @BACKSCAN) - 1
      Return strPre : StrSub (strMid, 1, intPosL) : strEllipsis : strPost
   EndIf
   Break
Case (intMode & 1) == 1 ; Ellipsis at the left side.
   strMid = StrSub (strMid, intLenMid - intWidth + 1, -1)
   If intMode == 1 Then Return strEllipsis : strMid
   If intMode == 5 Then Return strPre : strEllipsis : strMid : strPost
   If (intMode == 9) || (intMode == 13)
      intPosR = StrScan (strMid, strDelims, 1, @FWDSCAN) + 1
      Return strPre : strEllipsis : StrSub (strMid, intPosR, -1) : strPost
   EndIf
   Break
EndSwitch
Return strEllipsis
;..........................................................................................................................................
; This UDF udfStrCompact removes characters from a given string
; to make the remaining string fragment fit into a given width.
; The removed part of the text will be replaced by an ellipsis "..." (here: three point characters).
;
; The trimming function can be adjusted by option.
; Trim at the left boundary, trim at the right boundary, trim in the middle of the string.
;
; Basic options for manipulating normal text strings.
; intMode = 1 : Left boundary.
; intMode = 2 : Right boundary.
; intMode = 3 : Middle.
;
; Extended options.
; intMode = 4 : Path. Given string should be treated as path, components are delimited by backslash.
;               Preserves as much as possible of the drive and filename information.
; intMode = 8 : Word. Given string should be treated as a list of words delimited by a space character.

; Basic and extended options can be OR combined, e. g.
; intMode = 1|4   : Given string is a path string and should be trimmed at the left boundary.
; intMode = 1|4|8 : Given string is a path string and should be trimmed at the left boundary
;                   but respecting path delimiters as word boundaries.
;
; Detlev Dalitz.20090628.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.


strStringText = "The quick brown fox jumps over the lazy dog." ; 44 chars.
strStringPath = "A:\The\quick\brown fox\jumps over\the\lazy dog.txt" ; 50 chars.
intWidth = 30


;Default mode.
intMode = 0
strCompact0 = udfStrCompact (strStringText, intWidth, intMode) ; "The quick brown fox jumps over the lazy dog."
BreakPoint


; Normal mode.
; Left.
intMode = 1
strCompact1 = udfStrCompact (strStringText, intWidth, intMode) ; "...ox jumps over the lazy dog."
BreakPoint

; Right.
intMode = 2
strCompact2 = udfStrCompact (strStringText, intWidth, intMode) ; "The quick brown fox jumps o..."
BreakPoint

; Middle.
intMode = 3
strCompact3 = udfStrCompact (strStringText, intWidth, intMode) ; "The quick bro... the lazy dog."
BreakPoint


; Word mode.
; Left.
intMode = 1 | 8
strCompact18 = udfStrCompact (strStringText, intWidth, intMode) ; "...jumps over the lazy dog."
BreakPoint

; Right.
intMode = 2 | 8
strCompact28 = udfStrCompact (strStringText, intWidth, intMode) ; "The quick brown fox jumps..."
BreakPoint

; Middle.
intMode = 3 | 8
strCompact38 = udfStrCompact (strStringText, intWidth, intMode) ; "The quick ... the lazy dog."
BreakPoint


; Path mode.
; Left.
intMode = 1 | 4
strCompact14 = udfStrCompact (strStringPath, intWidth, intMode) ; "A:\...ps over\the\lazy dog.txt"
BreakPoint

; Right.
intMode = 2 | 4
strCompact24 = udfStrCompact (strStringPath, intWidth, intMode) ; "A:\The\quick\br...lazy dog.txt"
BreakPoint

; Middle.
intMode = 3 | 4
strCompact34 = udfStrCompact (strStringPath, intWidth, intMode) ; "A:\The\q...r\the\lazy dog.txt"
BreakPoint


; Path and Word mode.
; Left.
intMode = 1 | 4 | 8
strCompact18 = udfStrCompact (strStringPath, intWidth, intMode) ; "A:\...over\the\lazy dog.txt"
BreakPoint

; Right.
intMode = 2 | 4 | 8
strCompact28 = udfStrCompact (strStringPath, intWidth, intMode) ; "A:\The\quick...lazy dog.txt"
BreakPoint

; Middle.
intMode = 3 | 4 | 8
strCompact38 = udfStrCompact (strStringPath, intWidth, intMode) ; "A:\The\...\the\lazy dog.txt"
BreakPoint

Exit